Deleted Added
full compact
cardbus.c (140198) cardbus.c (141412)
1/*-
2 * Copyright (c) 2003 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

--- 11 unchanged lines hidden (view full) ---

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>
1/*-
2 * Copyright (c) 2003 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

--- 11 unchanged lines hidden (view full) ---

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: head/sys/dev/cardbus/cardbus.c 140198 2005-01-13 19:12:10Z imp $");
28__FBSDID("$FreeBSD: head/sys/dev/cardbus/cardbus.c 141412 2005-02-06 21:03:13Z imp $");
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

--- 5 unchanged lines hidden (view full) ---

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>
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

--- 5 unchanged lines hidden (view full) ---

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>
50#include <dev/pccard/pccardvar.h>
51
52#include "power_if.h"
53#include "pcib_if.h"
54
55/* sysctl vars */
56SYSCTL_NODE(_hw, OID_AUTO, cardbus, CTLFLAG_RD, 0, "CardBus parameters");
57

--- 8 unchanged lines hidden (view full) ---

66SYSCTL_INT(_hw_cardbus, OID_AUTO, cis_debug, CTLFLAG_RW,
67 &cardbus_cis_debug, 0,
68 "CardBus CIS debug");
69
70#define DPRINTF(a) if (cardbus_debug) printf a
71#define DEVPRINTF(x) if (cardbus_debug) device_printf x
72
73
51#include <dev/pccard/pccardvar.h>
52
53#include "power_if.h"
54#include "pcib_if.h"
55
56/* sysctl vars */
57SYSCTL_NODE(_hw, OID_AUTO, cardbus, CTLFLAG_RD, 0, "CardBus parameters");
58

--- 8 unchanged lines hidden (view full) ---

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
74
75static void cardbus_add_map(device_t cbdev, device_t child, int reg);
76static int cardbus_alloc_resources(device_t cbdev, device_t child);
74static int cardbus_attach(device_t cbdev);
75static int cardbus_attach_card(device_t cbdev);
77static int cardbus_attach(device_t cbdev);
78static int cardbus_attach_card(device_t cbdev);
79static int cardbus_barsort(const void *a, const void *b);
76static int cardbus_detach(device_t cbdev);
77static int cardbus_detach_card(device_t cbdev);
78static void cardbus_device_setup_regs(device_t brdev, int b, int s, int f,
79 pcicfgregs *cfg);
80static void cardbus_driver_added(device_t cbdev, driver_t *driver);
80static int cardbus_detach(device_t cbdev);
81static int cardbus_detach_card(device_t cbdev);
82static void cardbus_device_setup_regs(device_t brdev, int b, int s, int f,
83 pcicfgregs *cfg);
84static void cardbus_driver_added(device_t cbdev, driver_t *driver);
85static void cardbus_pickup_maps(device_t cbdev, device_t child);
81static int cardbus_probe(device_t cbdev);
82static int cardbus_read_ivar(device_t cbdev, device_t child, int which,
83 uintptr_t *result);
84static void cardbus_release_all_resources(device_t cbdev,
85 struct cardbus_devinfo *dinfo);
86static int cardbus_write_ivar(device_t cbdev, device_t child, int which,
87 uintptr_t value);
88
86static int cardbus_probe(device_t cbdev);
87static int cardbus_read_ivar(device_t cbdev, device_t child, int which,
88 uintptr_t *result);
89static void cardbus_release_all_resources(device_t cbdev,
90 struct cardbus_devinfo *dinfo);
91static int cardbus_write_ivar(device_t cbdev, device_t child, int which,
92 uintptr_t value);
93
94/*
95 * Resource allocation
96 */
97/*
98 * Adding a memory/io resource (sans CIS)
99 */
100
101static void
102cardbus_add_map(device_t cbdev, device_t child, int reg)
103{
104 struct cardbus_devinfo *dinfo = device_get_ivars(child);
105 struct resource_list_entry *rle;
106 uint32_t size;
107 uint32_t testval;
108 int type;
109
110 SLIST_FOREACH(rle, &dinfo->pci.resources, link) {
111 if (rle->rid == reg)
112 return;
113 }
114
115 if (reg == CARDBUS_ROM_REG)
116 testval = CARDBUS_ROM_ADDRMASK;
117 else
118 testval = ~0;
119
120 pci_write_config(child, reg, testval, 4);
121 testval = pci_read_config(child, reg, 4);
122
123 if (testval == ~0 || testval == 0)
124 return;
125
126 if ((testval & 1) == 0)
127 type = SYS_RES_MEMORY;
128 else
129 type = SYS_RES_IOPORT;
130
131 size = CARDBUS_MAPREG_MEM_SIZE(testval);
132 device_printf(cbdev, "Resource not specified in CIS: id=%x, size=%x\n",
133 reg, size);
134 resource_list_add(&dinfo->pci.resources, type, reg, 0UL, ~0UL, size);
135}
136
137static void
138cardbus_pickup_maps(device_t cbdev, device_t child)
139{
140 struct cardbus_devinfo *dinfo = device_get_ivars(child);
141 int reg;
142
143 /*
144 * Try to pick up any resources that was not specified in CIS.
145 * Maybe this isn't any longer necessary now that we have fixed
146 * CIS parsing and we should filter things here? XXX
147 */
148 for (reg = 0; reg < dinfo->pci.cfg.nummaps; reg++)
149 cardbus_add_map(cbdev, child, PCIR_BAR(reg));
150}
151
152static void
153cardbus_do_res(struct resource_list_entry *rle, device_t child, uint32_t start)
154{
155 rle->start = start;
156 rle->end = start + rle->count - 1;
157 pci_write_config(child, rle->rid, rle->start, 4);
158}
159
160static int
161cardbus_barsort(const void *a, const void *b)
162{
163 return ((*(const struct resource_list_entry * const *)b)->count -
164 (*(const struct resource_list_entry * const *)a)->count);
165}
166
167/* XXX this function is too long */
168static int
169cardbus_alloc_resources(device_t cbdev, device_t child)
170{
171 struct cardbus_devinfo *dinfo = device_get_ivars(child);
172 int count;
173 struct resource_list_entry *rle;
174 struct resource_list_entry **barlist;
175 int tmp;
176 uint32_t mem_psize = 0, mem_nsize = 0, io_size = 0;
177 struct resource *res;
178 uint32_t start,end;
179 int rid, flags;
180
181 count = 0;
182 SLIST_FOREACH(rle, &dinfo->pci.resources, link) {
183 count++;
184 }
185 if (count == 0)
186 return (0);
187 barlist = malloc(sizeof(struct resource_list_entry*) * count, M_DEVBUF,
188 M_WAITOK);
189 count = 0;
190 SLIST_FOREACH(rle, &dinfo->pci.resources, link) {
191 barlist[count] = rle;
192 if (rle->type == SYS_RES_IOPORT) {
193 io_size += rle->count;
194 } else if (rle->type == SYS_RES_MEMORY) {
195 if (dinfo->mprefetchable & BARBIT(rle->rid))
196 mem_psize += rle->count;
197 else
198 mem_nsize += rle->count;
199 }
200 count++;
201 }
202
203 /*
204 * We want to allocate the largest resource first, so that our
205 * allocated memory is packed.
206 */
207 qsort(barlist, count, sizeof(struct resource_list_entry*),
208 cardbus_barsort);
209
210 /* Allocate prefetchable memory */
211 flags = 0;
212 for (tmp = 0; tmp < count; tmp++) {
213 rle = barlist[tmp];
214 if (rle->res == NULL &&
215 rle->type == SYS_RES_MEMORY &&
216 dinfo->mprefetchable & BARBIT(rle->rid)) {
217 flags = rman_make_alignment_flags(rle->count);
218 break;
219 }
220 }
221 if (flags > 0) { /* If any prefetchable memory is requested... */
222 /*
223 * First we allocate one big space for all resources of this
224 * type. We do this because our parent, pccbb, needs to open
225 * a window to forward all addresses within the window, and
226 * it would be best if nobody else has resources allocated
227 * within the window.
228 * (XXX: Perhaps there might be a better way to do this?)
229 */
230 rid = 0;
231 res = bus_alloc_resource(cbdev, SYS_RES_MEMORY, &rid, 0,
232 (dinfo->mprefetchable & dinfo->mbelow1mb)?0xFFFFF:~0UL,
233 mem_psize, flags);
234 if (res == NULL) {
235 device_printf(cbdev,
236 "Can't get memory for prefetch mem\n");
237 free(barlist, M_DEVBUF);
238 return (EIO);
239 }
240 start = rman_get_start(res);
241 end = rman_get_end(res);
242 DEVPRINTF((cbdev, "Prefetchable memory at %x-%x\n", start, end));
243 /*
244 * Now that we know the region is free, release it and hand it
245 * out piece by piece.
246 */
247 bus_release_resource(cbdev, SYS_RES_MEMORY, rid, res);
248 for (tmp = 0; tmp < count; tmp++) {
249 rle = barlist[tmp];
250 if (rle->type == SYS_RES_MEMORY &&
251 dinfo->mprefetchable & BARBIT(rle->rid)) {
252 cardbus_do_res(rle, child, start);
253 start += rle->count;
254 }
255 }
256 }
257
258 /* Allocate non-prefetchable memory */
259 flags = 0;
260 for (tmp = 0; tmp < count; tmp++) {
261 rle = barlist[tmp];
262 if (rle->type == SYS_RES_MEMORY &&
263 (dinfo->mprefetchable & BARBIT(rle->rid)) == 0) {
264 flags = rman_make_alignment_flags(rle->count);
265 break;
266 }
267 }
268 if (flags > 0) { /* If any non-prefetchable memory is requested... */
269 /*
270 * First we allocate one big space for all resources of this
271 * type. We do this because our parent, pccbb, needs to open
272 * a window to forward all addresses within the window, and
273 * it would be best if nobody else has resources allocated
274 * within the window.
275 * (XXX: Perhaps there might be a better way to do this?)
276 */
277 rid = 0;
278 res = bus_alloc_resource(cbdev, SYS_RES_MEMORY, &rid, 0,
279 ((~dinfo->mprefetchable) & dinfo->mbelow1mb)?0xFFFFF:~0UL,
280 mem_nsize, flags);
281 if (res == NULL) {
282 device_printf(cbdev,
283 "Can't get memory for non-prefetch mem\n");
284 free(barlist, M_DEVBUF);
285 return (EIO);
286 }
287 start = rman_get_start(res);
288 end = rman_get_end(res);
289 DEVPRINTF((cbdev, "Non-prefetchable memory at %x-%x\n",
290 start, end));
291 /*
292 * Now that we know the region is free, release it and hand it
293 * out piece by piece.
294 */
295 bus_release_resource(cbdev, SYS_RES_MEMORY, rid, res);
296 for (tmp = 0; tmp < count; tmp++) {
297 rle = barlist[tmp];
298 if (rle->type == SYS_RES_MEMORY &&
299 (dinfo->mprefetchable & BARBIT(rle->rid)) == 0) {
300 cardbus_do_res(rle, child, start);
301 start += rle->count;
302 }
303 }
304 }
305
306 /* Allocate IO ports */
307 flags = 0;
308 for (tmp = 0; tmp < count; tmp++) {
309 rle = barlist[tmp];
310 if (rle->type == SYS_RES_IOPORT) {
311 flags = rman_make_alignment_flags(rle->count);
312 break;
313 }
314 }
315 if (flags > 0) { /* If any IO port is requested... */
316 /*
317 * First we allocate one big space for all resources of this
318 * type. We do this because our parent, pccbb, needs to open
319 * a window to forward all addresses within the window, and
320 * it would be best if nobody else has resources allocated
321 * within the window.
322 * (XXX: Perhaps there might be a better way to do this?)
323 */
324 rid = 0;
325 res = bus_alloc_resource(cbdev, SYS_RES_IOPORT, &rid, 0,
326 (dinfo->ibelow1mb)?0xFFFFF:~0UL, io_size, flags);
327 if (res == NULL) {
328 device_printf(cbdev,
329 "Can't get memory for IO ports\n");
330 free(barlist, M_DEVBUF);
331 return (EIO);
332 }
333 start = rman_get_start(res);
334 end = rman_get_end(res);
335 DEVPRINTF((cbdev, "IO port at %x-%x\n", start, end));
336 /*
337 * Now that we know the region is free, release it and hand it
338 * out piece by piece.
339 */
340 bus_release_resource(cbdev, SYS_RES_IOPORT, rid, res);
341 for (tmp = 0; tmp < count; tmp++) {
342 rle = barlist[tmp];
343 if (rle->type == SYS_RES_IOPORT) {
344 cardbus_do_res(rle, child, start);
345 start += rle->count;
346 }
347 }
348 }
349
350 /* Allocate IRQ */
351 rid = 0;
352 res = bus_alloc_resource_any(cbdev, SYS_RES_IRQ, &rid, RF_SHAREABLE);
353 if (res == NULL) {
354 device_printf(cbdev, "Can't get memory for irq\n");
355 free(barlist, M_DEVBUF);
356 return (EIO);
357 }
358 start = rman_get_start(res);
359 end = rman_get_end(res);
360 bus_release_resource(cbdev, SYS_RES_IRQ, rid, res);
361 resource_list_add(&dinfo->pci.resources, SYS_RES_IRQ, rid, start, end,
362 1);
363 dinfo->pci.cfg.intline = rman_get_start(res);
364 pci_write_config(child, PCIR_INTLINE, rman_get_start(res), 1);
365
366 free(barlist, M_DEVBUF);
367 return (0);
368}
369
89/************************************************************************/
90/* Probe/Attach */
91/************************************************************************/
92
93static int
94cardbus_probe(device_t cbdev)
95{
96 device_set_desc(cbdev, "CardBus bus");

--- 49 unchanged lines hidden (view full) ---

146 PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_MAXLAT, 0x14, 1);
147 cfg->maxlat = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_MAXLAT, 1);
148}
149
150static int
151cardbus_attach_card(device_t cbdev)
152{
153 device_t brdev = device_get_parent(cbdev);
370/************************************************************************/
371/* Probe/Attach */
372/************************************************************************/
373
374static int
375cardbus_probe(device_t cbdev)
376{
377 device_set_desc(cbdev, "CardBus bus");

--- 49 unchanged lines hidden (view full) ---

427 PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_MAXLAT, 0x14, 1);
428 cfg->maxlat = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_MAXLAT, 1);
429}
430
431static int
432cardbus_attach_card(device_t cbdev)
433{
434 device_t brdev = device_get_parent(cbdev);
435 device_t child;
154 int cardattached = 0;
155 int bus, slot, func;
156
157 cardbus_detach_card(cbdev); /* detach existing cards */
158 POWER_ENABLE_SOCKET(brdev, cbdev);
159 bus = pcib_get_bus(cbdev);
160 /* For each function, set it up and try to attach a driver to it */
161 for (slot = 0; slot <= CARDBUS_SLOTMAX; slot++) {

--- 6 unchanged lines hidden (view full) ---

168 sizeof(struct cardbus_devinfo));
169 if (dinfo == NULL)
170 continue;
171 if (dinfo->pci.cfg.mfdev)
172 cardbusfunchigh = CARDBUS_FUNCMAX;
173
174 cardbus_device_setup_regs(brdev, bus, slot, func,
175 &dinfo->pci.cfg);
436 int cardattached = 0;
437 int bus, slot, func;
438
439 cardbus_detach_card(cbdev); /* detach existing cards */
440 POWER_ENABLE_SOCKET(brdev, cbdev);
441 bus = pcib_get_bus(cbdev);
442 /* For each function, set it up and try to attach a driver to it */
443 for (slot = 0; slot <= CARDBUS_SLOTMAX; slot++) {

--- 6 unchanged lines hidden (view full) ---

450 sizeof(struct cardbus_devinfo));
451 if (dinfo == NULL)
452 continue;
453 if (dinfo->pci.cfg.mfdev)
454 cardbusfunchigh = CARDBUS_FUNCMAX;
455
456 cardbus_device_setup_regs(brdev, bus, slot, func,
457 &dinfo->pci.cfg);
176 dinfo->pci.cfg.dev = device_add_child(cbdev, NULL, -1);
177 if (!dinfo->pci.cfg.dev) {
458 child = device_add_child(cbdev, NULL, -1);
459 if (child == NULL) {
178 DEVPRINTF((cbdev, "Cannot add child!\n"));
179 pci_freecfg((struct pci_devinfo *)dinfo);
180 continue;
181 }
460 DEVPRINTF((cbdev, "Cannot add child!\n"));
461 pci_freecfg((struct pci_devinfo *)dinfo);
462 continue;
463 }
464 dinfo->pci.cfg.dev = child;
182 resource_list_init(&dinfo->pci.resources);
465 resource_list_init(&dinfo->pci.resources);
183 device_set_ivars(dinfo->pci.cfg.dev, dinfo);
184 cardbus_do_cis(cbdev, dinfo->pci.cfg.dev);
466 device_set_ivars(child, dinfo);
467 if (cardbus_do_cis(cbdev, child) != 0) {
468 DEVPRINTF((cbdev, "Can't parse cis\n"));
469 pci_freecfg((struct pci_devinfo *)dinfo);
470 continue;
471 }
472 cardbus_pickup_maps(cbdev, child);
473 cardbus_alloc_resources(cbdev, child);
185 pci_print_verbose(&dinfo->pci);
474 pci_print_verbose(&dinfo->pci);
186 if (device_probe_and_attach(dinfo->pci.cfg.dev) != 0)
475 if (device_probe_and_attach(child) != 0)
187 cardbus_release_all_resources(cbdev, dinfo);
188 else
189 cardattached++;
190 }
191 }
192
193 if (cardattached > 0)
194 return (0);

--- 110 unchanged lines hidden (view full) ---

305 cfg = &dinfo->pci.cfg;
306
307 switch (which) {
308 case PCI_IVAR_ETHADDR:
309 /*
310 * The generic accessor doesn't deal with failure, so
311 * we set the return value, then return an error.
312 */
476 cardbus_release_all_resources(cbdev, dinfo);
477 else
478 cardattached++;
479 }
480 }
481
482 if (cardattached > 0)
483 return (0);

--- 110 unchanged lines hidden (view full) ---

594 cfg = &dinfo->pci.cfg;
595
596 switch (which) {
597 case PCI_IVAR_ETHADDR:
598 /*
599 * The generic accessor doesn't deal with failure, so
600 * we set the return value, then return an error.
601 */
313 if ((dinfo->fepresent & (1 << TPL_FUNCE_LAN_NID)) == 0) {
314 *((uint8_t **) result) = NULL;
315 return (EINVAL);
602 if (dinfo->fepresent & (1 << PCCARD_TPLFE_TYPE_LAN_NID)) {
603 *((uint8_t **) result) = dinfo->funce.lan.nid;
604 break;
316 }
605 }
317 *((uint8_t **) result) = dinfo->funce.lan.nid;
318 break;
606 *((uint8_t **) result) = NULL;
607 return (EINVAL);
319 default:
320 return (pci_read_ivar(cbdev, child, which, result));
321 }
322 return 0;
323}
324
325static int
326cardbus_write_ivar(device_t cbdev, device_t child, int which, uintptr_t value)

--- 31 unchanged lines hidden ---
608 default:
609 return (pci_read_ivar(cbdev, child, which, result));
610 }
611 return 0;
612}
613
614static int
615cardbus_write_ivar(device_t cbdev, device_t child, int which, uintptr_t value)

--- 31 unchanged lines hidden ---