2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000,2001 Jonathan Chen All rights reserved.
5 * Copyright (c) 2005-2008 M. Warner Losh <imp@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * CIS Handling for the Cardbus Bus
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
43 #include <machine/bus.h>
44 #include <machine/resource.h>
46 #include <sys/endian.h>
48 #include <sys/pciio.h>
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pcireg.h>
52 #include <dev/pccard/pccardvar.h>
53 #include <dev/pccard/pccard_cis.h>
55 #include <dev/cardbus/cardbusreg.h>
56 #include <dev/cardbus/cardbusvar.h>
57 #include <dev/cardbus/cardbus_cis.h>
59 extern int cardbus_cis_debug;
61 #define DPRINTF(a) if (cardbus_cis_debug) printf a
62 #define DEVPRINTF(x) if (cardbus_cis_debug) device_printf x
64 #define CIS_CONFIG_SPACE (struct resource *)~0UL
66 static int decode_tuple_generic(device_t cbdev, device_t child, int id,
67 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
68 struct tuple_callbacks *info, void *);
69 static int decode_tuple_linktarget(device_t cbdev, device_t child, int id,
70 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
71 struct tuple_callbacks *info, void *);
72 static int decode_tuple_vers_1(device_t cbdev, device_t child, int id,
73 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
74 struct tuple_callbacks *info, void *);
75 static int decode_tuple_funcid(device_t cbdev, device_t child, int id,
76 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
77 struct tuple_callbacks *info, void *);
78 static int decode_tuple_manfid(device_t cbdev, device_t child, int id,
79 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
80 struct tuple_callbacks *info, void *);
81 static int decode_tuple_funce(device_t cbdev, device_t child, int id,
82 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
83 struct tuple_callbacks *info, void *);
84 static int decode_tuple_bar(device_t cbdev, device_t child, int id,
85 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
86 struct tuple_callbacks *info, void *);
87 static int decode_tuple_unhandled(device_t cbdev, device_t child, int id,
88 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
89 struct tuple_callbacks *info, void *);
90 static int decode_tuple_end(device_t cbdev, device_t child, int id,
91 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
92 struct tuple_callbacks *info, void *);
94 static int cardbus_read_tuple_conf(device_t cbdev, device_t child,
95 uint32_t start, uint32_t *off, int *tupleid, int *len,
97 static int cardbus_read_tuple_mem(device_t cbdev, struct resource *res,
98 uint32_t start, uint32_t *off, int *tupleid, int *len,
100 static int cardbus_read_tuple(device_t cbdev, device_t child,
101 struct resource *res, uint32_t start, uint32_t *off,
102 int *tupleid, int *len, uint8_t *tupledata);
103 static void cardbus_read_tuple_finish(device_t cbdev, device_t child,
104 int rid, struct resource *res);
105 static struct resource *cardbus_read_tuple_init(device_t cbdev, device_t child,
106 uint32_t *start, int *rid);
107 static int decode_tuple(device_t cbdev, device_t child, int tupleid,
108 int len, uint8_t *tupledata, uint32_t start,
109 uint32_t *off, struct tuple_callbacks *callbacks,
112 #define MAKETUPLE(NAME,FUNC) { CISTPL_ ## NAME, #NAME, decode_tuple_ ## FUNC }
114 static char *funcnames[] = {
128 * Handler functions for various CIS tuples
132 decode_tuple_generic(device_t cbdev, device_t child, int id,
133 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
134 struct tuple_callbacks *info, void *argp)
138 if (cardbus_cis_debug) {
140 printf("TUPLE: %s [%d]:", info->name, len);
142 printf("TUPLE: Unknown(0x%02x) [%d]:", id, len);
144 for (i = 0; i < len; i++) {
145 if (i % 0x10 == 0 && len > 0x10)
146 printf("\n 0x%02x:", i);
147 printf(" %02x", tupledata[i]);
155 decode_tuple_linktarget(device_t cbdev, device_t child, int id,
156 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
157 struct tuple_callbacks *info, void *argp)
161 if (cardbus_cis_debug) {
162 printf("TUPLE: %s [%d]:", info->name, len);
164 for (i = 0; i < len; i++) {
165 if (i % 0x10 == 0 && len > 0x10)
166 printf("\n 0x%02x:", i);
167 printf(" %02x", tupledata[i]);
171 if (len != 3 || tupledata[0] != 'C' || tupledata[1] != 'I' ||
172 tupledata[2] != 'S') {
173 printf("Invalid data for CIS Link Target!\n");
174 decode_tuple_generic(cbdev, child, id, len, tupledata,
175 start, off, info, argp);
182 decode_tuple_vers_1(device_t cbdev, device_t child, int id,
183 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
184 struct tuple_callbacks *info, void *argp)
188 if (cardbus_cis_debug) {
189 printf("Product version: %d.%d\n", tupledata[0], tupledata[1]);
190 printf("Product name: ");
191 for (i = 2; i < len; i++) {
192 if (tupledata[i] == '\0')
194 else if (tupledata[i] == 0xff)
197 printf("%c", tupledata[i]);
205 decode_tuple_funcid(device_t cbdev, device_t child, int id,
206 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
207 struct tuple_callbacks *info, void *argp)
209 struct cardbus_devinfo *dinfo = device_get_ivars(child);
210 int numnames = nitems(funcnames);
213 if (cardbus_cis_debug) {
214 printf("Functions: ");
215 for (i = 0; i < len; i++) {
216 if (tupledata[i] < numnames)
217 printf("%s", funcnames[tupledata[i]]);
219 printf("Unknown(%d)", tupledata[i]);
226 dinfo->funcid = tupledata[0]; /* use first in list */
231 decode_tuple_manfid(device_t cbdev, device_t child, int id,
232 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
233 struct tuple_callbacks *info, void *argp)
235 struct cardbus_devinfo *dinfo = device_get_ivars(child);
238 if (cardbus_cis_debug) {
239 printf("Manufacturer ID: ");
240 for (i = 0; i < len; i++)
241 printf("%02x", tupledata[i]);
246 dinfo->mfrid = tupledata[1] | (tupledata[2] << 8);
247 dinfo->prodid = tupledata[3] | (tupledata[4] << 8);
253 decode_tuple_funce(device_t cbdev, device_t child, int id,
254 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
255 struct tuple_callbacks *info, void *argp)
257 struct cardbus_devinfo *dinfo = device_get_ivars(child);
260 if (cardbus_cis_debug) {
261 printf("Function Extension: ");
262 for (i = 0; i < len; i++)
263 printf("%02x", tupledata[i]);
266 if (len < 2) /* too short */
268 type = tupledata[0]; /* XXX <32 always? */
269 switch (dinfo->funcid) {
270 case PCCARD_FUNCTION_NETWORK:
272 case PCCARD_TPLFE_TYPE_LAN_NID:
273 if (tupledata[1] > sizeof(dinfo->funce.lan.nid)) {
274 /* ignore, warning? */
277 bcopy(tupledata + 2, dinfo->funce.lan.nid,
281 dinfo->fepresent |= 1<<type;
288 decode_tuple_bar(device_t cbdev, device_t child, int id,
289 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
290 struct tuple_callbacks *info, void *argp)
292 struct cardbus_devinfo *dinfo = device_get_ivars(child);
298 device_printf(cbdev, "CIS BAR length not 6 (%d)\n", len);
303 len = le32toh(*(uint32_t*)(tupledata + 2));
304 if (reg & TPL_BAR_REG_AS)
305 type = SYS_RES_IOPORT;
307 type = SYS_RES_MEMORY;
309 bar = reg & TPL_BAR_REG_ASI_MASK;
311 device_printf(cbdev, "Invalid BAR type 0 in CIS\n");
312 return (EINVAL); /* XXX Return an error? */
313 } else if (bar == 7) {
314 /* XXX Should we try to map in Option ROMs? */
318 /* Convert from BAR type to BAR offset */
319 bar = PCIR_BAR(bar - 1);
321 if (type == SYS_RES_MEMORY) {
322 if (reg & TPL_BAR_REG_PREFETCHABLE)
323 dinfo->mprefetchable |= (1 << PCI_RID2BAR(bar));
325 * The PC Card spec says we're only supposed to honor this
326 * hint when the cardbus bridge is a child of pci0 (the main
327 * bus). The PC Card spec seems to indicate that this should
328 * only be done on x86 based machines, which suggests that on
329 * non-x86 machines the addresses can be anywhere. Since the
330 * hardware can do it on non-x86 machines, it should be able
331 * to do it on x86 machines too. Therefore, we can and should
332 * ignore this hint. Furthermore, the PC Card spec recommends
333 * always allocating memory above 1MB, contradicting the other
334 * part of the PC Card spec, it seems. We make note of it,
335 * but otherwise don't use this information.
337 * Some Realtek cards have this set in their CIS, but fail
338 * to actually work when mapped this way, and experience
339 * has shown ignoring this big to be a wise choice.
341 * XXX We should cite chapter and verse for standard refs.
343 if (reg & TPL_BAR_REG_BELOW1MB)
344 dinfo->mbelow1mb |= (1 << PCI_RID2BAR(bar));
351 decode_tuple_unhandled(device_t cbdev, device_t child, int id,
352 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
353 struct tuple_callbacks *info, void *argp)
355 /* Make this message suck less XXX */
356 printf("TUPLE: %s [%d] is unhandled! Bailing...", info->name, len);
361 decode_tuple_end(device_t cbdev, device_t child, int id,
362 int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
363 struct tuple_callbacks *info, void *argp)
365 if (cardbus_cis_debug)
366 printf("CIS reading done\n");
371 * Functions to read the a tuple from the card
375 * Read CIS bytes out of the config space. We have to read it 4 bytes at a
376 * time and do the usual mask and shift to return the bytes. The standard
377 * defines the byte order to be little endian. pci_read_config converts it to
378 * host byte order. This is why we have no endian conversion functions: the
379 * shifts wind up being endian neutral. This is also why we avoid the obvious
380 * memcpy optimization.
383 cardbus_read_tuple_conf(device_t cbdev, device_t child, uint32_t start,
384 uint32_t *off, int *tupleid, int *len, uint8_t *tupledata)
392 e = pci_read_config(child, loc & ~0x3, 4);
393 e >>= 8 * (loc & 0x3);
395 for (i = loc, j = -2; j < *len; j++, i++) {
397 e = pci_read_config(child, i, 4);
403 tupledata[j] = 0xff & e;
411 * Read the CIS data out of memory. We indirect through the bus space
412 * routines to ensure proper byte ordering conversions when necessary.
415 cardbus_read_tuple_mem(device_t cbdev, struct resource *res, uint32_t start,
416 uint32_t *off, int *tupleid, int *len, uint8_t *tupledata)
420 *tupleid = bus_read_1(res, start + *off);
421 *len = bus_read_1(res, start + *off + 1);
422 bus_read_region_1(res, *off + start + 2, tupledata, *len);
429 cardbus_read_tuple(device_t cbdev, device_t child, struct resource *res,
430 uint32_t start, uint32_t *off, int *tupleid, int *len,
433 if (res == CIS_CONFIG_SPACE)
434 return (cardbus_read_tuple_conf(cbdev, child, start, off,
435 tupleid, len, tupledata));
436 return (cardbus_read_tuple_mem(cbdev, res, start, off, tupleid, len,
441 cardbus_read_tuple_finish(device_t cbdev, device_t child, int rid,
442 struct resource *res)
444 if (res != CIS_CONFIG_SPACE) {
445 bus_release_resource(child, SYS_RES_MEMORY, rid, res);
446 bus_delete_resource(child, SYS_RES_MEMORY, rid);
450 static struct resource *
451 cardbus_read_tuple_init(device_t cbdev, device_t child, uint32_t *start,
454 struct resource *res;
457 space = *start & PCIM_CIS_ASI_MASK;
459 case PCIM_CIS_ASI_CONFIG:
460 DEVPRINTF((cbdev, "CIS in PCI config space\n"));
461 /* CIS in PCI config space need no initialization */
462 return (CIS_CONFIG_SPACE);
463 case PCIM_CIS_ASI_BAR0:
464 case PCIM_CIS_ASI_BAR1:
465 case PCIM_CIS_ASI_BAR2:
466 case PCIM_CIS_ASI_BAR3:
467 case PCIM_CIS_ASI_BAR4:
468 case PCIM_CIS_ASI_BAR5:
469 *rid = PCIR_BAR(space - PCIM_CIS_ASI_BAR0);
470 DEVPRINTF((cbdev, "CIS in BAR %#x\n", *rid));
472 case PCIM_CIS_ASI_ROM:
474 DEVPRINTF((cbdev, "CIS in option rom\n"));
477 device_printf(cbdev, "Unable to read CIS: Unknown space: %d\n",
482 /* allocate the memory space to read CIS */
483 res = bus_alloc_resource_any(child, SYS_RES_MEMORY, rid,
484 rman_make_alignment_flags(4096) | RF_ACTIVE);
486 device_printf(cbdev, "Unable to allocate resource "
490 DEVPRINTF((cbdev, "CIS Mapped to %#jx\n",
491 rman_get_start(res)));
493 /* Flip to the right ROM image if CIS is in ROM */
494 if (space == PCIM_CIS_ASI_ROM) {
496 uint32_t imagebase = 0;
502 imagenum = (*start & PCIM_CIS_ROM_MASK) >> 28;
503 for (romnum = 0;; romnum++) {
504 romsig = bus_read_2(res,
505 imagebase + CARDBUS_EXROM_SIGNATURE);
506 if (romsig != 0xaa55) {
507 device_printf(cbdev, "Bad header in rom %d: "
508 "[%x] %04x\n", romnum, imagebase +
509 CARDBUS_EXROM_SIGNATURE, romsig);
510 cardbus_read_tuple_finish(cbdev, child, *rid,
517 * If this was the Option ROM image that we were
518 * looking for, then we are done.
520 if (romnum == imagenum)
523 /* Find out where the next Option ROM image is */
524 pcidata = imagebase + bus_read_2(res,
525 imagebase + CARDBUS_EXROM_DATA_PTR);
526 imagesize = bus_read_2(res,
527 pcidata + CARDBUS_EXROM_DATA_IMAGE_LENGTH);
529 if (imagesize == 0) {
531 * XXX some ROMs seem to have this as zero,
532 * can we assume this means 1 block?
534 device_printf(cbdev, "Warning, size of Option "
535 "ROM image %d is 0 bytes, assuming 512 "
540 /* Image size is in 512 byte units */
543 if ((bus_read_1(res, pcidata +
544 CARDBUS_EXROM_DATA_INDICATOR) & 0x80) != 0) {
545 device_printf(cbdev, "Cannot find CIS in "
547 cardbus_read_tuple_finish(cbdev, child, *rid,
552 imagebase += imagesize;
554 *start = imagebase + (*start & PCIM_CIS_ADDR_MASK);
556 *start = *start & PCIM_CIS_ADDR_MASK;
558 DEVPRINTF((cbdev, "CIS offset is %#x\n", *start));
564 * Dispatch the right handler function per tuple
568 decode_tuple(device_t cbdev, device_t child, int tupleid, int len,
569 uint8_t *tupledata, uint32_t start, uint32_t *off,
570 struct tuple_callbacks *callbacks, void *argp)
573 for (i = 0; callbacks[i].id != CISTPL_GENERIC; i++) {
574 if (tupleid == callbacks[i].id)
575 return (callbacks[i].func(cbdev, child, tupleid, len,
576 tupledata, start, off, &callbacks[i], argp));
578 return (callbacks[i].func(cbdev, child, tupleid, len,
579 tupledata, start, off, NULL, argp));
583 cardbus_parse_cis(device_t cbdev, device_t child,
584 struct tuple_callbacks *callbacks, void *argp)
587 int tupleid = CISTPL_NULL;
589 int expect_linktarget;
591 struct resource *res;
594 tupledata = malloc(MAXTUPLESIZE, M_DEVBUF, M_WAITOK | M_ZERO);
595 expect_linktarget = TRUE;
596 if ((start = pci_read_config(child, PCIR_CIS, 4)) == 0) {
597 DEVPRINTF((cbdev, "Warning: CIS pointer is 0: (no CIS)\n"));
598 free(tupledata, M_DEVBUF);
601 DEVPRINTF((cbdev, "CIS pointer is %#x\n", start));
603 res = cardbus_read_tuple_init(cbdev, child, &start, &rid);
605 device_printf(cbdev, "Unable to allocate resources for CIS\n");
606 free(tupledata, M_DEVBUF);
611 if (cardbus_read_tuple(cbdev, child, res, start, &off,
612 &tupleid, &len, tupledata) != 0) {
613 device_printf(cbdev, "Failed to read CIS.\n");
614 cardbus_read_tuple_finish(cbdev, child, rid, res);
615 free(tupledata, M_DEVBUF);
619 if (expect_linktarget && tupleid != CISTPL_LINKTARGET) {
620 device_printf(cbdev, "Expecting link target, got 0x%x\n",
622 cardbus_read_tuple_finish(cbdev, child, rid, res);
623 free(tupledata, M_DEVBUF);
626 expect_linktarget = decode_tuple(cbdev, child, tupleid, len,
627 tupledata, start, &off, callbacks, argp);
628 if (expect_linktarget != 0) {
629 device_printf(cbdev, "Parsing failed with %d\n",
631 cardbus_read_tuple_finish(cbdev, child, rid, res);
632 free(tupledata, M_DEVBUF);
633 return (expect_linktarget);
635 } while (tupleid != CISTPL_END);
636 cardbus_read_tuple_finish(cbdev, child, rid, res);
637 free(tupledata, M_DEVBUF);
642 cardbus_do_cis(device_t cbdev, device_t child)
644 struct tuple_callbacks init_callbacks[] = {
645 MAKETUPLE(LONGLINK_CB, unhandled),
646 MAKETUPLE(INDIRECT, unhandled),
647 MAKETUPLE(LONGLINK_MFC, unhandled),
649 MAKETUPLE(LONGLINK_A, unhandled),
650 MAKETUPLE(LONGLINK_C, unhandled),
651 MAKETUPLE(LINKTARGET, linktarget),
652 MAKETUPLE(VERS_1, vers_1),
653 MAKETUPLE(MANFID, manfid),
654 MAKETUPLE(FUNCID, funcid),
655 MAKETUPLE(FUNCE, funce),
657 MAKETUPLE(GENERIC, generic),
660 return (cardbus_parse_cis(cbdev, child, init_callbacks, NULL));