cardbus.c revision 330938
1/*-
2 * Copyright (c) 2003-2008 M. Warner Losh.  All Rights Reserved.
3 * Copyright (c) 2000,2001 Jonathan Chen.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: stable/10/sys/dev/cardbus/cardbus.c 330938 2018-03-14 19:04:40Z jhb $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/malloc.h>
33#include <sys/module.h>
34#include <sys/kernel.h>
35#include <sys/sysctl.h>
36
37#include <sys/bus.h>
38#include <machine/bus.h>
39#include <sys/rman.h>
40#include <machine/resource.h>
41
42#include <sys/pciio.h>
43#include <dev/pci/pcivar.h>
44#include <dev/pci/pcireg.h>
45#include <dev/pci/pci_private.h>
46
47#include <dev/cardbus/cardbusreg.h>
48#include <dev/cardbus/cardbusvar.h>
49#include <dev/cardbus/cardbus_cis.h>
50#include <dev/pccard/pccard_cis.h>
51#include <dev/pccard/pccardvar.h>
52
53#include "power_if.h"
54#include "pcib_if.h"
55
56/* sysctl vars */
57static SYSCTL_NODE(_hw, OID_AUTO, cardbus, CTLFLAG_RD, 0, "CardBus parameters");
58
59int    cardbus_debug = 0;
60TUNABLE_INT("hw.cardbus.debug", &cardbus_debug);
61SYSCTL_INT(_hw_cardbus, OID_AUTO, debug, CTLFLAG_RW,
62    &cardbus_debug, 0,
63  "CardBus debug");
64
65int    cardbus_cis_debug = 0;
66TUNABLE_INT("hw.cardbus.cis_debug", &cardbus_cis_debug);
67SYSCTL_INT(_hw_cardbus, OID_AUTO, cis_debug, CTLFLAG_RW,
68    &cardbus_cis_debug, 0,
69  "CardBus CIS debug");
70
71#define	DPRINTF(a) if (cardbus_debug) printf a
72#define	DEVPRINTF(x) if (cardbus_debug) device_printf x
73
74static int	cardbus_attach(device_t cbdev);
75static int	cardbus_attach_card(device_t cbdev);
76static int	cardbus_detach(device_t cbdev);
77static int	cardbus_detach_card(device_t cbdev);
78static void	cardbus_device_setup_regs(pcicfgregs *cfg);
79static void	cardbus_driver_added(device_t cbdev, driver_t *driver);
80static int	cardbus_probe(device_t cbdev);
81static int	cardbus_read_ivar(device_t cbdev, device_t child, int which,
82		    uintptr_t *result);
83
84/************************************************************************/
85/* Probe/Attach								*/
86/************************************************************************/
87
88static int
89cardbus_probe(device_t cbdev)
90{
91	device_set_desc(cbdev, "CardBus bus");
92	return (0);
93}
94
95static int
96cardbus_attach(device_t cbdev)
97{
98	struct cardbus_softc *sc;
99#ifdef PCI_RES_BUS
100	int rid;
101#endif
102
103	sc = device_get_softc(cbdev);
104	sc->sc_dev = cbdev;
105#ifdef PCI_RES_BUS
106	rid = 0;
107	sc->sc_bus = bus_alloc_resource(cbdev, PCI_RES_BUS, &rid,
108	    pcib_get_bus(cbdev), pcib_get_bus(cbdev), 1, 0);
109	if (sc->sc_bus == NULL) {
110		device_printf(cbdev, "failed to allocate bus number\n");
111		return (ENXIO);
112	}
113#endif
114	return (0);
115}
116
117static int
118cardbus_detach(device_t cbdev)
119{
120#ifdef PCI_RES_BUS
121	struct cardbus_softc *sc;
122#endif
123
124	cardbus_detach_card(cbdev);
125#ifdef PCI_RES_BUS
126	sc = device_get_softc(cbdev);
127	(void)bus_release_resource(cbdev, PCI_RES_BUS, 0, sc->sc_bus);
128#endif
129	return (0);
130}
131
132static int
133cardbus_suspend(device_t self)
134{
135
136	cardbus_detach_card(self);
137	return (0);
138}
139
140static int
141cardbus_resume(device_t self)
142{
143
144	return (0);
145}
146
147/************************************************************************/
148/* Attach/Detach card							*/
149/************************************************************************/
150
151static void
152cardbus_device_setup_regs(pcicfgregs *cfg)
153{
154	device_t dev = cfg->dev;
155	int i;
156
157	/*
158	 * Some cards power up with garbage in their BARs.  This
159	 * code clears all that junk out.
160	 */
161	for (i = 0; i < PCIR_MAX_BAR_0; i++)
162		pci_write_config(dev, PCIR_BAR(i), 0, 4);
163
164	cfg->intline =
165	    pci_get_irq(device_get_parent(device_get_parent(dev)));
166	pci_write_config(dev, PCIR_INTLINE, cfg->intline, 1);
167	pci_write_config(dev, PCIR_CACHELNSZ, 0x08, 1);
168	pci_write_config(dev, PCIR_LATTIMER, 0xa8, 1);
169	pci_write_config(dev, PCIR_MINGNT, 0x14, 1);
170	pci_write_config(dev, PCIR_MAXLAT, 0x14, 1);
171}
172
173static int
174cardbus_attach_card(device_t cbdev)
175{
176	device_t brdev = device_get_parent(cbdev);
177	device_t child;
178	int bus, domain, slot, func;
179	int cardattached = 0;
180	int cardbusfunchigh = 0;
181	struct cardbus_softc *sc;
182
183	sc = device_get_softc(cbdev);
184	cardbus_detach_card(cbdev); /* detach existing cards */
185	POWER_ENABLE_SOCKET(brdev, cbdev);
186	domain = pcib_get_domain(cbdev);
187	bus = pcib_get_bus(cbdev);
188	slot = 0;
189	/* For each function, set it up and try to attach a driver to it */
190	for (func = 0; func <= cardbusfunchigh; func++) {
191		struct cardbus_devinfo *dinfo;
192
193		dinfo = (struct cardbus_devinfo *)
194		    pci_read_device(brdev, domain, bus, slot, func,
195			sizeof(struct cardbus_devinfo));
196		if (dinfo == NULL)
197			continue;
198		if (dinfo->pci.cfg.mfdev)
199			cardbusfunchigh = PCI_FUNCMAX;
200
201		child = device_add_child(cbdev, NULL, -1);
202		if (child == NULL) {
203			DEVPRINTF((cbdev, "Cannot add child!\n"));
204			pci_freecfg((struct pci_devinfo *)dinfo);
205			continue;
206		}
207		dinfo->pci.cfg.dev = child;
208		resource_list_init(&dinfo->pci.resources);
209		device_set_ivars(child, dinfo);
210		cardbus_device_create(sc, dinfo, cbdev, child);
211		if (cardbus_do_cis(cbdev, child) != 0)
212			DEVPRINTF((cbdev, "Warning: Bogus CIS ignored\n"));
213		pci_cfg_save(dinfo->pci.cfg.dev, &dinfo->pci, 0);
214		pci_cfg_restore(dinfo->pci.cfg.dev, &dinfo->pci);
215		cardbus_device_setup_regs(&dinfo->pci.cfg);
216		pci_add_resources(cbdev, child, 1, dinfo->mprefetchable);
217		pci_print_verbose(&dinfo->pci);
218		if (device_probe_and_attach(child) == 0)
219			cardattached++;
220		else
221			pci_cfg_save(dinfo->pci.cfg.dev, &dinfo->pci, 1);
222	}
223	if (cardattached > 0)
224		return (0);
225/*	POWER_DISABLE_SOCKET(brdev, cbdev); */
226	return (ENOENT);
227}
228
229static void
230cardbus_child_deleted(device_t cbdev, device_t child)
231{
232	struct cardbus_devinfo *dinfo = device_get_ivars(child);
233
234	if (dinfo->pci.cfg.dev != child)
235		device_printf(cbdev, "devinfo dev mismatch\n");
236	cardbus_device_destroy(dinfo);
237	pci_child_deleted(cbdev, child);
238}
239
240static int
241cardbus_detach_card(device_t cbdev)
242{
243	int err = 0;
244
245	err = bus_generic_detach(cbdev);
246	if (err)
247		return (err);
248	err = device_delete_children(cbdev);
249	if (err)
250		return (err);
251
252	POWER_DISABLE_SOCKET(device_get_parent(cbdev), cbdev);
253	return (err);
254}
255
256static void
257cardbus_driver_added(device_t cbdev, driver_t *driver)
258{
259	int numdevs;
260	device_t *devlist;
261	device_t dev;
262	int i;
263	struct cardbus_devinfo *dinfo;
264
265	DEVICE_IDENTIFY(driver, cbdev);
266	if (device_get_children(cbdev, &devlist, &numdevs) != 0)
267		return;
268
269	/*
270	 * If there are no drivers attached, but there are children,
271	 * then power the card up.
272	 */
273	for (i = 0; i < numdevs; i++) {
274		dev = devlist[i];
275		if (device_get_state(dev) != DS_NOTPRESENT)
276		    break;
277	}
278	if (i > 0 && i == numdevs)
279		POWER_ENABLE_SOCKET(device_get_parent(cbdev), cbdev);
280	for (i = 0; i < numdevs; i++) {
281		dev = devlist[i];
282		if (device_get_state(dev) != DS_NOTPRESENT)
283			continue;
284		dinfo = device_get_ivars(dev);
285		pci_print_verbose(&dinfo->pci);
286		if (bootverbose)
287			printf("pci%d:%d:%d:%d: reprobing on driver added\n",
288			    dinfo->pci.cfg.domain, dinfo->pci.cfg.bus,
289			    dinfo->pci.cfg.slot, dinfo->pci.cfg.func);
290		pci_cfg_restore(dinfo->pci.cfg.dev, &dinfo->pci);
291		if (device_probe_and_attach(dev) != 0)
292			pci_cfg_save(dev, &dinfo->pci, 1);
293	}
294	free(devlist, M_TEMP);
295}
296
297/************************************************************************/
298/* Other Bus Methods							*/
299/************************************************************************/
300
301static int
302cardbus_read_ivar(device_t cbdev, device_t child, int which, uintptr_t *result)
303{
304	struct cardbus_devinfo *dinfo;
305	pcicfgregs *cfg;
306
307	dinfo = device_get_ivars(child);
308	cfg = &dinfo->pci.cfg;
309
310	switch (which) {
311	case PCI_IVAR_ETHADDR:
312		/*
313		 * The generic accessor doesn't deal with failure, so
314		 * we set the return value, then return an error.
315		 */
316		if (dinfo->fepresent & (1 << PCCARD_TPLFE_TYPE_LAN_NID)) {
317			*((uint8_t **) result) = dinfo->funce.lan.nid;
318			break;
319		}
320		*((uint8_t **) result) = NULL;
321		return (EINVAL);
322	default:
323		return (pci_read_ivar(cbdev, child, which, result));
324	}
325	return 0;
326}
327
328static device_method_t cardbus_methods[] = {
329	/* Device interface */
330	DEVMETHOD(device_probe,		cardbus_probe),
331	DEVMETHOD(device_attach,	cardbus_attach),
332	DEVMETHOD(device_detach,	cardbus_detach),
333	DEVMETHOD(device_suspend,	cardbus_suspend),
334	DEVMETHOD(device_resume,	cardbus_resume),
335
336	/* Bus interface */
337	DEVMETHOD(bus_child_deleted,	cardbus_child_deleted),
338	DEVMETHOD(bus_get_dma_tag,	bus_generic_get_dma_tag),
339	DEVMETHOD(bus_read_ivar,	cardbus_read_ivar),
340	DEVMETHOD(bus_driver_added,	cardbus_driver_added),
341
342	/* Card Interface */
343	DEVMETHOD(card_attach_card,	cardbus_attach_card),
344	DEVMETHOD(card_detach_card,	cardbus_detach_card),
345
346	{0,0}
347};
348
349DEFINE_CLASS_1(cardbus, cardbus_driver, cardbus_methods,
350    sizeof(struct cardbus_softc), pci_driver);
351
352static devclass_t cardbus_devclass;
353
354DRIVER_MODULE(cardbus, cbb, cardbus_driver, cardbus_devclass, 0, 0);
355MODULE_VERSION(cardbus, 1);
356