cardbus.c revision 331722
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/11/sys/dev/cardbus/cardbus.c 331722 2018-03-29 02:50:57Z eadler $");
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;
60SYSCTL_INT(_hw_cardbus, OID_AUTO, debug, CTLFLAG_RWTUN,
61    &cardbus_debug, 0, "CardBus debug");
62
63int    cardbus_cis_debug = 0;
64SYSCTL_INT(_hw_cardbus, OID_AUTO, cis_debug, CTLFLAG_RWTUN,
65    &cardbus_cis_debug, 0, "CardBus CIS debug");
66
67#define	DPRINTF(a) if (cardbus_debug) printf a
68#define	DEVPRINTF(x) if (cardbus_debug) device_printf x
69
70static int	cardbus_attach(device_t cbdev);
71static int	cardbus_attach_card(device_t cbdev);
72static int	cardbus_detach(device_t cbdev);
73static int	cardbus_detach_card(device_t cbdev);
74static void	cardbus_device_setup_regs(pcicfgregs *cfg);
75static void	cardbus_driver_added(device_t cbdev, driver_t *driver);
76static int	cardbus_probe(device_t cbdev);
77static int	cardbus_read_ivar(device_t cbdev, device_t child, int which,
78		    uintptr_t *result);
79
80/************************************************************************/
81/* Probe/Attach								*/
82/************************************************************************/
83
84static int
85cardbus_probe(device_t cbdev)
86{
87	device_set_desc(cbdev, "CardBus bus");
88	return (0);
89}
90
91static int
92cardbus_attach(device_t cbdev)
93{
94	struct cardbus_softc *sc;
95#ifdef PCI_RES_BUS
96	int rid;
97#endif
98
99	sc = device_get_softc(cbdev);
100	sc->sc_dev = cbdev;
101#ifdef PCI_RES_BUS
102	rid = 0;
103	sc->sc_bus = bus_alloc_resource(cbdev, PCI_RES_BUS, &rid,
104	    pcib_get_bus(cbdev), pcib_get_bus(cbdev), 1, 0);
105	if (sc->sc_bus == NULL) {
106		device_printf(cbdev, "failed to allocate bus number\n");
107		return (ENXIO);
108	}
109#else
110	device_printf(cbdev, "Your bus numbers may be AFU\n");
111#endif
112	return (0);
113}
114
115static int
116cardbus_detach(device_t cbdev)
117{
118#ifdef PCI_RES_BUS
119	struct cardbus_softc *sc;
120#endif
121
122	cardbus_detach_card(cbdev);
123#ifdef PCI_RES_BUS
124	sc = device_get_softc(cbdev);
125	device_printf(cbdev, "Freeing up the allocatd bus\n");
126	(void)bus_release_resource(cbdev, PCI_RES_BUS, 0, sc->sc_bus);
127#endif
128	return (0);
129}
130
131static int
132cardbus_suspend(device_t self)
133{
134
135	cardbus_detach_card(self);
136	return (0);
137}
138
139static int
140cardbus_resume(device_t self)
141{
142
143	return (0);
144}
145
146/************************************************************************/
147/* Attach/Detach card							*/
148/************************************************************************/
149
150static void
151cardbus_device_setup_regs(pcicfgregs *cfg)
152{
153	device_t dev = cfg->dev;
154	int i;
155
156	/*
157	 * Some cards power up with garbage in their BARs.  This
158	 * code clears all that junk out.
159	 */
160	for (i = 0; i < PCIR_MAX_BAR_0; i++)
161		pci_write_config(dev, PCIR_BAR(i), 0, 4);
162
163	cfg->intline =
164	    pci_get_irq(device_get_parent(device_get_parent(dev)));
165	pci_write_config(dev, PCIR_INTLINE, cfg->intline, 1);
166	pci_write_config(dev, PCIR_CACHELNSZ, 0x08, 1);
167	pci_write_config(dev, PCIR_LATTIMER, 0xa8, 1);
168	pci_write_config(dev, PCIR_MINGNT, 0x14, 1);
169	pci_write_config(dev, PCIR_MAXLAT, 0x14, 1);
170}
171
172static struct pci_devinfo *
173cardbus_alloc_devinfo(device_t dev)
174{
175	struct cardbus_devinfo *dinfo;
176
177	dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
178	return (&dinfo->pci);
179}
180
181static int
182cardbus_attach_card(device_t cbdev)
183{
184	device_t brdev = device_get_parent(cbdev);
185	device_t child;
186	int bus, domain, slot, func;
187	int cardattached = 0;
188	int cardbusfunchigh = 0;
189	struct cardbus_softc *sc;
190
191	sc = device_get_softc(cbdev);
192	cardbus_detach_card(cbdev); /* detach existing cards */
193	POWER_DISABLE_SOCKET(brdev, cbdev); /* Turn the socket off first */
194	POWER_ENABLE_SOCKET(brdev, cbdev);
195	domain = pcib_get_domain(cbdev);
196	bus = pcib_get_bus(cbdev);
197	slot = 0;
198	/* For each function, set it up and try to attach a driver to it */
199	for (func = 0; func <= cardbusfunchigh; func++) {
200		struct cardbus_devinfo *dinfo;
201
202		dinfo = (struct cardbus_devinfo *)
203		    pci_read_device(brdev, cbdev, domain, bus, slot, func);
204		if (dinfo == NULL)
205			continue;
206		if (dinfo->pci.cfg.mfdev)
207			cardbusfunchigh = PCI_FUNCMAX;
208
209		child = device_add_child(cbdev, NULL, -1);
210		if (child == NULL) {
211			DEVPRINTF((cbdev, "Cannot add child!\n"));
212			pci_freecfg((struct pci_devinfo *)dinfo);
213			continue;
214		}
215		dinfo->pci.cfg.dev = child;
216		resource_list_init(&dinfo->pci.resources);
217		device_set_ivars(child, dinfo);
218		cardbus_device_create(sc, dinfo, cbdev, child);
219		if (cardbus_do_cis(cbdev, child) != 0)
220			DEVPRINTF((cbdev, "Warning: Bogus CIS ignored\n"));
221		pci_cfg_save(dinfo->pci.cfg.dev, &dinfo->pci, 0);
222		pci_cfg_restore(dinfo->pci.cfg.dev, &dinfo->pci);
223		cardbus_device_setup_regs(&dinfo->pci.cfg);
224		pci_add_resources(cbdev, child, 1, dinfo->mprefetchable);
225		pci_print_verbose(&dinfo->pci);
226		if (device_probe_and_attach(child) == 0)
227			cardattached++;
228		else
229			pci_cfg_save(dinfo->pci.cfg.dev, &dinfo->pci, 1);
230	}
231	if (cardattached > 0)
232		return (0);
233/*	POWER_DISABLE_SOCKET(brdev, cbdev); */
234	return (ENOENT);
235}
236
237static void
238cardbus_child_deleted(device_t cbdev, device_t child)
239{
240	struct cardbus_devinfo *dinfo = device_get_ivars(child);
241
242	if (dinfo->pci.cfg.dev != child)
243		device_printf(cbdev, "devinfo dev mismatch\n");
244	cardbus_device_destroy(dinfo);
245	pci_child_deleted(cbdev, child);
246}
247
248static int
249cardbus_detach_card(device_t cbdev)
250{
251	int err = 0;
252
253	err = bus_generic_detach(cbdev);
254	if (err)
255		return (err);
256	err = device_delete_children(cbdev);
257	if (err)
258		return (err);
259
260	POWER_DISABLE_SOCKET(device_get_parent(cbdev), cbdev);
261	return (err);
262}
263
264static void
265cardbus_driver_added(device_t cbdev, driver_t *driver)
266{
267	int numdevs;
268	device_t *devlist;
269	device_t dev;
270	int i;
271	struct cardbus_devinfo *dinfo;
272
273	DEVICE_IDENTIFY(driver, cbdev);
274	if (device_get_children(cbdev, &devlist, &numdevs) != 0)
275		return;
276
277	/*
278	 * If there are no drivers attached, but there are children,
279	 * then power the card up.
280	 */
281	for (i = 0; i < numdevs; i++) {
282		dev = devlist[i];
283		if (device_get_state(dev) != DS_NOTPRESENT)
284		    break;
285	}
286	if (i > 0 && i == numdevs)
287		POWER_ENABLE_SOCKET(device_get_parent(cbdev), cbdev);
288	for (i = 0; i < numdevs; i++) {
289		dev = devlist[i];
290		if (device_get_state(dev) != DS_NOTPRESENT)
291			continue;
292		dinfo = device_get_ivars(dev);
293		pci_print_verbose(&dinfo->pci);
294		if (bootverbose)
295			printf("pci%d:%d:%d:%d: reprobing on driver added\n",
296			    dinfo->pci.cfg.domain, dinfo->pci.cfg.bus,
297			    dinfo->pci.cfg.slot, dinfo->pci.cfg.func);
298		pci_cfg_restore(dinfo->pci.cfg.dev, &dinfo->pci);
299		if (device_probe_and_attach(dev) != 0)
300			pci_cfg_save(dev, &dinfo->pci, 1);
301	}
302	free(devlist, M_TEMP);
303}
304
305/************************************************************************/
306/* Other Bus Methods							*/
307/************************************************************************/
308
309static int
310cardbus_read_ivar(device_t cbdev, device_t child, int which, uintptr_t *result)
311{
312	struct cardbus_devinfo *dinfo;
313	pcicfgregs *cfg;
314
315	dinfo = device_get_ivars(child);
316	cfg = &dinfo->pci.cfg;
317
318	switch (which) {
319	case PCI_IVAR_ETHADDR:
320		/*
321		 * The generic accessor doesn't deal with failure, so
322		 * we set the return value, then return an error.
323		 */
324		if (dinfo->fepresent & (1 << PCCARD_TPLFE_TYPE_LAN_NID)) {
325			*((uint8_t **) result) = dinfo->funce.lan.nid;
326			break;
327		}
328		*((uint8_t **) result) = NULL;
329		return (EINVAL);
330	default:
331		return (pci_read_ivar(cbdev, child, which, result));
332	}
333	return 0;
334}
335
336static device_method_t cardbus_methods[] = {
337	/* Device interface */
338	DEVMETHOD(device_probe,		cardbus_probe),
339	DEVMETHOD(device_attach,	cardbus_attach),
340	DEVMETHOD(device_detach,	cardbus_detach),
341	DEVMETHOD(device_suspend,	cardbus_suspend),
342	DEVMETHOD(device_resume,	cardbus_resume),
343
344	/* Bus interface */
345	DEVMETHOD(bus_child_deleted,	cardbus_child_deleted),
346	DEVMETHOD(bus_get_dma_tag,	bus_generic_get_dma_tag),
347	DEVMETHOD(bus_read_ivar,	cardbus_read_ivar),
348	DEVMETHOD(bus_driver_added,	cardbus_driver_added),
349	DEVMETHOD(bus_rescan,		bus_null_rescan),
350
351	/* Card Interface */
352	DEVMETHOD(card_attach_card,	cardbus_attach_card),
353	DEVMETHOD(card_detach_card,	cardbus_detach_card),
354
355	/* PCI interface */
356	DEVMETHOD(pci_alloc_devinfo,	cardbus_alloc_devinfo),
357
358	{0,0}
359};
360
361DEFINE_CLASS_1(cardbus, cardbus_driver, cardbus_methods,
362    sizeof(struct cardbus_softc), pci_driver);
363
364static devclass_t cardbus_devclass;
365
366DRIVER_MODULE(cardbus, cbb, cardbus_driver, cardbus_devclass, 0, 0);
367MODULE_VERSION(cardbus, 1);
368