1 /* 2 * Copyright (c) 2000,2001 Jonathan Chen. 3 * 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 * without modification, immediately at the beginning of the file. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in 13 * the documentation and/or other materials provided with the 14 * distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 /* 32 * CIS Handling for the Cardbus Bus 33 */ 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/malloc.h> 39 40 #include <sys/bus.h> 41 #include <machine/bus.h> 42 #include <machine/resource.h> 43 #include <sys/rman.h> 44 #include <sys/endian.h> 45 46 #include <sys/pciio.h> 47 #include <dev/pci/pcivar.h> 48 #include <dev/pci/pcireg.h> 49 50 #include <dev/cardbus/cardbusreg.h> 51 #include <dev/cardbus/cardbusvar.h> 52 #include <dev/cardbus/cardbus_cis.h> 53 54 #include <dev/pccard/pccardvar.h> 55 56 extern int cardbus_cis_debug; 57 58 #define DPRINTF(a) if (cardbus_cis_debug) printf a 59 #define DEVPRINTF(x) if (cardbus_cis_debug) device_printf x 60 61 struct tuple_callbacks; 62 63 typedef int (tuple_cb) (device_t cbdev, device_t child, int id, int len, 64 uint8_t *tupledata, uint32_t start, uint32_t *off, 65 struct tuple_callbacks *info); 66 67 struct tuple_callbacks { 68 int id; 69 char *name; 70 tuple_cb *func; 71 }; 72 73 static int decode_tuple_generic(device_t cbdev, device_t child, int id, 74 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 75 struct tuple_callbacks *info); 76 static int decode_tuple_nothing(device_t cbdev, device_t child, int id, 77 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 78 struct tuple_callbacks *info); 79 static int decode_tuple_copy(device_t cbdev, device_t child, int id, 80 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 81 struct tuple_callbacks *info); 82 static int decode_tuple_linktarget(device_t cbdev, device_t child, int id, 83 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 84 struct tuple_callbacks *info); 85 static int decode_tuple_vers_1(device_t cbdev, device_t child, int id, 86 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 87 struct tuple_callbacks *info); 88 static int decode_tuple_funcid(device_t cbdev, device_t child, int id, 89 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 90 struct tuple_callbacks *info); 91 static int decode_tuple_manfid(device_t cbdev, device_t child, int id, 92 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 93 struct tuple_callbacks *info); 94 static int decode_tuple_funce(device_t cbdev, device_t child, int id, 95 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 96 struct tuple_callbacks *info); 97 static int decode_tuple_bar(device_t cbdev, device_t child, int id, 98 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 99 struct tuple_callbacks *info); 100 static int decode_tuple_unhandled(device_t cbdev, device_t child, int id, 101 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 102 struct tuple_callbacks *info); 103 static int decode_tuple_end(device_t cbdev, device_t child, int id, 104 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 105 struct tuple_callbacks *info); 106 107 static int cardbus_read_tuple_conf(device_t cbdev, device_t child, 108 uint32_t start, uint32_t *off, int *tupleid, int *len, 109 uint8_t *tupledata); 110 static int cardbus_read_tuple_mem(device_t cbdev, struct resource *res, 111 uint32_t start, uint32_t *off, int *tupleid, int *len, 112 uint8_t *tupledata); 113 static int cardbus_read_tuple(device_t cbdev, device_t child, 114 struct resource *res, uint32_t start, uint32_t *off, 115 int *tupleid, int *len, uint8_t *tupledata); 116 static void cardbus_read_tuple_finish(device_t cbdev, device_t child, 117 int rid, struct resource *res); 118 static struct resource *cardbus_read_tuple_init(device_t cbdev, device_t child, 119 uint32_t *start, int *rid); 120 static int decode_tuple(device_t cbdev, device_t child, int tupleid, 121 int len, uint8_t *tupledata, uint32_t start, 122 uint32_t *off, struct tuple_callbacks *callbacks); 123 static int cardbus_parse_cis(device_t cbdev, device_t child, 124 struct tuple_callbacks *callbacks); 125 static int barsort(const void *a, const void *b); 126 static int cardbus_alloc_resources(device_t cbdev, device_t child); 127 static void cardbus_add_map(device_t cbdev, device_t child, int reg); 128 static void cardbus_pickup_maps(device_t cbdev, device_t child); 129 130 131 #define MAKETUPLE(NAME,FUNC) { CISTPL_ ## NAME, #NAME, decode_tuple_ ## FUNC } 132 133 static char *funcnames[] = { 134 "Multi-Functioned", 135 "Memory", 136 "Serial Port", 137 "Parallel Port", 138 "Fixed Disk", 139 "Video Adaptor", 140 "Network Adaptor", 141 "AIMS", 142 "SCSI", 143 "Security" 144 }; 145 146 struct cardbus_quirk { 147 uint32_t devid; /* Vendor/device of the card */ 148 int type; 149 #define CARDBUS_QUIRK_MAP_REG 1 /* PCI map register in weird place */ 150 int arg1; 151 int arg2; 152 }; 153 154 struct cardbus_quirk cardbus_quirks[] = { 155 { 0 } 156 }; 157 158 static struct cis_tupleinfo *cisread_buf; 159 static int ncisread_buf; 160 161 /* 162 * Handler functions for various CIS tuples 163 */ 164 165 static int 166 decode_tuple_generic(device_t cbdev, device_t child, int id, 167 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 168 struct tuple_callbacks *info) 169 { 170 int i; 171 172 if (cardbus_cis_debug) { 173 if (info) 174 printf("TUPLE: %s [%d]:", info->name, len); 175 else 176 printf("TUPLE: Unknown(0x%02x) [%d]:", id, len); 177 178 for (i = 0; i < len; i++) { 179 if (i % 0x10 == 0 && len > 0x10) 180 printf("\n 0x%02x:", i); 181 printf(" %02x", tupledata[i]); 182 } 183 printf("\n"); 184 } 185 return (0); 186 } 187 188 static int 189 decode_tuple_nothing(device_t cbdev, device_t child, int id, 190 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 191 struct tuple_callbacks *info) 192 { 193 return (0); 194 } 195 196 static int 197 decode_tuple_copy(device_t cbdev, device_t child, int id, 198 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 199 struct tuple_callbacks *info) 200 { 201 struct cis_tupleinfo *tmpbuf; 202 203 tmpbuf = malloc(sizeof(struct cis_tupleinfo) * (ncisread_buf+1), 204 M_DEVBUF, M_WAITOK); 205 if (ncisread_buf > 0) { 206 memcpy(tmpbuf, cisread_buf, 207 sizeof(struct cis_tupleinfo) * ncisread_buf); 208 free(cisread_buf, M_DEVBUF); 209 } 210 cisread_buf = tmpbuf; 211 212 cisread_buf[ncisread_buf].id = id; 213 cisread_buf[ncisread_buf].len = len; 214 cisread_buf[ncisread_buf].data = malloc(len, M_DEVBUF, M_WAITOK); 215 memcpy(cisread_buf[ncisread_buf].data, tupledata, len); 216 ncisread_buf++; 217 return (0); 218 } 219 220 static int 221 decode_tuple_linktarget(device_t cbdev, device_t child, int id, 222 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 223 struct tuple_callbacks *info) 224 { 225 int i; 226 227 if (cardbus_cis_debug) { 228 printf("TUPLE: %s [%d]:", info->name, len); 229 230 for (i = 0; i < len; i++) { 231 if (i % 0x10 == 0 && len > 0x10) 232 printf("\n 0x%02x:", i); 233 printf(" %02x", tupledata[i]); 234 } 235 printf("\n"); 236 } 237 if (len != 3 || tupledata[0] != 'C' || tupledata[1] != 'I' || 238 tupledata[2] != 'S') { 239 printf("Invalid data for CIS Link Target!\n"); 240 decode_tuple_generic(cbdev, child, id, len, tupledata, 241 start, off, info); 242 return (EINVAL); 243 } 244 return (0); 245 } 246 247 static int 248 decode_tuple_vers_1(device_t cbdev, device_t child, int id, 249 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 250 struct tuple_callbacks *info) 251 { 252 int i; 253 254 if (cardbus_cis_debug) { 255 printf("Product version: %d.%d\n", tupledata[0], tupledata[1]); 256 printf("Product name: "); 257 for (i = 2; i < len; i++) { 258 if (tupledata[i] == '\0') 259 printf(" | "); 260 else if (tupledata[i] == 0xff) 261 break; 262 else 263 printf("%c", tupledata[i]); 264 } 265 printf("\n"); 266 } 267 return (0); 268 } 269 270 static int 271 decode_tuple_funcid(device_t cbdev, device_t child, int id, 272 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 273 struct tuple_callbacks *info) 274 { 275 struct cardbus_devinfo *dinfo = device_get_ivars(child); 276 int numnames = sizeof(funcnames) / sizeof(funcnames[0]); 277 int i; 278 279 if (cardbus_cis_debug) { 280 printf("Functions: "); 281 for (i = 0; i < len; i++) { 282 if (tupledata[i] < numnames) 283 printf("%s", funcnames[tupledata[i]]); 284 else 285 printf("Unknown(%d)", tupledata[i]); 286 if (i < len-1) 287 printf(", "); 288 } 289 printf("\n"); 290 } 291 if (len > 0) 292 dinfo->funcid = tupledata[0]; /* use first in list */ 293 return (0); 294 } 295 296 static int 297 decode_tuple_manfid(device_t cbdev, device_t child, int id, 298 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 299 struct tuple_callbacks *info) 300 { 301 struct cardbus_devinfo *dinfo = device_get_ivars(child); 302 int i; 303 304 if (cardbus_cis_debug) { 305 printf("Manufacturer ID: "); 306 for (i = 0; i < len; i++) 307 printf("%02x", tupledata[i]); 308 printf("\n"); 309 } 310 311 if (len == 5) { 312 dinfo->mfrid = tupledata[1] | (tupledata[2] << 8); 313 dinfo->prodid = tupledata[3] | (tupledata[4] << 8); 314 } 315 return (0); 316 } 317 318 static int 319 decode_tuple_funce(device_t cbdev, device_t child, int id, 320 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 321 struct tuple_callbacks *info) 322 { 323 struct cardbus_devinfo *dinfo = device_get_ivars(child); 324 int type, i; 325 326 if (cardbus_cis_debug) { 327 printf("Function Extension: "); 328 for (i = 0; i < len; i++) 329 printf("%02x", tupledata[i]); 330 printf("\n"); 331 } 332 if (len < 2) /* too short */ 333 return (0); 334 type = tupledata[0]; /* XXX <32 always? */ 335 switch (dinfo->funcid) { 336 case TPL_FUNC_SERIAL: 337 if (type == TPL_FUNCE_SER_UART) { /* NB: len known > 1 */ 338 dinfo->funce.sio.type = tupledata[1] & 0x1f; 339 } 340 dinfo->fepresent |= 1<<type; 341 break; 342 case TPL_FUNC_LAN: 343 switch (type) { 344 case TPL_FUNCE_LAN_TECH: 345 dinfo->funce.lan.tech = tupledata[1]; /* XXX mask? */ 346 break; 347 #if 0 348 case TPL_FUNCE_LAN_SPEED: 349 for (i = 0; i < 3; i++) { 350 if (dinfo->funce.lan.speed[i] == 0) { 351 if (len > 4) { 352 dinfo->funce.lan.speed[i] = 353 ...; 354 } 355 break; 356 } 357 } 358 break; 359 #endif 360 case TPL_FUNCE_LAN_MEDIA: 361 for (i = 0; i < 4 && dinfo->funce.lan.media[i]; i++) { 362 if (dinfo->funce.lan.media[i] == 0) { 363 /* NB: len known > 1 */ 364 dinfo->funce.lan.media[i] = 365 tupledata[1]; /*XXX? mask */ 366 break; 367 } 368 } 369 break; 370 case TPL_FUNCE_LAN_NID: 371 if (tupledata[1] > sizeof(dinfo->funce.lan.nid)) { 372 /* ignore, warning? */ 373 return (0); 374 } 375 bcopy(tupledata + 2, dinfo->funce.lan.nid, 376 tupledata[1]); 377 break; 378 case TPL_FUNCE_LAN_CONN: 379 dinfo->funce.lan.contype = tupledata[1];/*XXX mask? */ 380 break; 381 } 382 dinfo->fepresent |= 1<<type; 383 break; 384 } 385 return (0); 386 } 387 388 static int 389 decode_tuple_bar(device_t cbdev, device_t child, int id, 390 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 391 struct tuple_callbacks *info) 392 { 393 struct cardbus_devinfo *dinfo = device_get_ivars(child); 394 int type; 395 uint8_t reg; 396 uint32_t bar, pci_bar; 397 398 if (len != 6) { 399 device_printf(cbdev, "CIS BAR length not 6 (%d)\n", len); 400 return (EINVAL); 401 } 402 403 reg = *tupledata; 404 len = le32toh(*(uint32_t*)(tupledata + 2)); 405 if (reg & TPL_BAR_REG_AS) { 406 type = SYS_RES_IOPORT; 407 } else { 408 type = SYS_RES_MEMORY; 409 } 410 411 bar = reg & TPL_BAR_REG_ASI_MASK; 412 if (bar == 0) { 413 device_printf(cbdev, "Invalid BAR type 0 in CIS\n"); 414 return (EINVAL); /* XXX Return an error? */ 415 } else if (bar == 7) { 416 /* XXX Should we try to map in Option ROMs? */ 417 return (0); 418 } 419 420 /* Convert from BAR type to BAR offset */ 421 bar = CARDBUS_BASE0_REG + (bar - 1) * 4; 422 423 if (type == SYS_RES_MEMORY) { 424 if (reg & TPL_BAR_REG_PREFETCHABLE) 425 dinfo->mprefetchable |= BARBIT(bar); 426 if (reg & TPL_BAR_REG_BELOW1MB) 427 dinfo->mbelow1mb |= BARBIT(bar); 428 } 429 430 /* 431 * Sanity check the BAR length reported in the CIS with the length 432 * encoded in the PCI BAR. The latter seems to be more reliable. 433 * XXX - This probably belongs elsewhere. 434 */ 435 pci_write_config(child, bar, 0xffffffff, 4); 436 pci_bar = pci_read_config(child, bar, 4); 437 if ((pci_bar != 0x0) && (pci_bar != 0xffffffff)) { 438 if (type == SYS_RES_MEMORY) { 439 pci_bar &= ~0xf; 440 } else { 441 pci_bar &= ~0x3; 442 } 443 len = 1 << (ffs(pci_bar) - 1); 444 } 445 446 DEVPRINTF((cbdev, "Opening BAR: type=%s, bar=%02x, len=%04x%s%s\n", 447 (type == SYS_RES_MEMORY) ? "MEM" : "IO", bar, len, 448 (type == SYS_RES_MEMORY && dinfo->mprefetchable & BARBIT(bar)) ? 449 " (Prefetchable)" : "", type == SYS_RES_MEMORY ? 450 ((dinfo->mbelow1mb & BARBIT(bar)) ? " (Below 1Mb)" : "") : "")); 451 452 resource_list_add(&dinfo->pci.resources, type, bar, 0UL, ~0UL, len); 453 454 /* 455 * Mark the appropriate bit in the PCI command register so that 456 * device drivers will know which type of BARs can be used. 457 */ 458 pci_enable_io(child, type); 459 return (0); 460 } 461 462 static int 463 decode_tuple_unhandled(device_t cbdev, device_t child, int id, 464 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 465 struct tuple_callbacks *info) 466 { 467 /* Make this message suck less XXX */ 468 printf("TUPLE: %s [%d] is unhandled! Bailing...", info->name, len); 469 return (-1); 470 } 471 472 static int 473 decode_tuple_end(device_t cbdev, device_t child, int id, 474 int len, uint8_t *tupledata, uint32_t start, uint32_t *off, 475 struct tuple_callbacks *info) 476 { 477 if (cardbus_cis_debug) { 478 printf("CIS reading done\n"); 479 } 480 return (0); 481 } 482 483 /* 484 * Functions to read the a tuple from the card 485 */ 486 487 static int 488 cardbus_read_tuple_conf(device_t cbdev, device_t child, uint32_t start, 489 uint32_t *off, int *tupleid, int *len, uint8_t *tupledata) 490 { 491 int i, j; 492 uint32_t e; 493 uint32_t loc; 494 495 loc = start + *off; 496 497 e = pci_read_config(child, loc - loc % 4, 4); 498 for (j = loc % 4; j > 0; j--) 499 e >>= 8; 500 *len = 0; 501 for (i = loc, j = -2; j < *len; j++, i++) { 502 if (i % 4 == 0) 503 e = pci_read_config(child, i, 4); 504 if (j == -2) 505 *tupleid = 0xff & e; 506 else if (j == -1) 507 *len = 0xff & e; 508 else 509 tupledata[j] = 0xff & e; 510 e >>= 8; 511 } 512 *off += *len + 2; 513 return (0); 514 } 515 516 static int 517 cardbus_read_tuple_mem(device_t cbdev, struct resource *res, uint32_t start, 518 uint32_t *off, int *tupleid, int *len, uint8_t *tupledata) 519 { 520 bus_space_tag_t bt; 521 bus_space_handle_t bh; 522 int ret; 523 524 bt = rman_get_bustag(res); 525 bh = rman_get_bushandle(res); 526 527 *tupleid = bus_space_read_1(bt, bh, start + *off); 528 *len = bus_space_read_1(bt, bh, start + *off + 1); 529 bus_space_read_region_1(bt, bh, *off + start + 2, tupledata, *len); 530 ret = 0; 531 *off += *len + 2; 532 return (ret); 533 } 534 535 static int 536 cardbus_read_tuple(device_t cbdev, device_t child, struct resource *res, 537 uint32_t start, uint32_t *off, int *tupleid, int *len, 538 uint8_t *tupledata) 539 { 540 if (res == (struct resource*)~0UL) { 541 return (cardbus_read_tuple_conf(cbdev, child, start, off, 542 tupleid, len, tupledata)); 543 } else { 544 return (cardbus_read_tuple_mem(cbdev, res, start, off, 545 tupleid, len, tupledata)); 546 } 547 } 548 549 static void 550 cardbus_read_tuple_finish(device_t cbdev, device_t child, int rid, 551 struct resource *res) 552 { 553 if (res != (struct resource*)~0UL) { 554 bus_release_resource(cbdev, SYS_RES_MEMORY, rid, res); 555 pci_write_config(child, rid, 0, 4); 556 PCI_DISABLE_IO(cbdev, child, SYS_RES_MEMORY); 557 } 558 } 559 560 static struct resource * 561 cardbus_read_tuple_init(device_t cbdev, device_t child, uint32_t *start, 562 int *rid) 563 { 564 uint32_t testval; 565 uint32_t size; 566 struct resource *res; 567 568 switch (CARDBUS_CIS_SPACE(*start)) { 569 case CARDBUS_CIS_ASI_TUPLE: 570 /* CIS in PCI config space need no initialization */ 571 return ((struct resource*)~0UL); 572 case CARDBUS_CIS_ASI_BAR0: 573 case CARDBUS_CIS_ASI_BAR1: 574 case CARDBUS_CIS_ASI_BAR2: 575 case CARDBUS_CIS_ASI_BAR3: 576 case CARDBUS_CIS_ASI_BAR4: 577 case CARDBUS_CIS_ASI_BAR5: 578 *rid = CARDBUS_BASE0_REG + (CARDBUS_CIS_SPACE(*start) - 1) * 4; 579 break; 580 case CARDBUS_CIS_ASI_ROM: 581 *rid = CARDBUS_ROM_REG; 582 #if 0 583 /* 584 * This mask doesn't contain the bit that actually enables 585 * the Option ROM. 586 */ 587 pci_write_config(child, *rid, CARDBUS_ROM_ADDRMASK, 4); 588 #endif 589 break; 590 default: 591 device_printf(cbdev, "Unable to read CIS: Unknown space: %d\n", 592 CARDBUS_CIS_SPACE(*start)); 593 return (NULL); 594 } 595 596 /* figure out how much space we need */ 597 pci_write_config(child, *rid, 0xffffffff, 4); 598 testval = pci_read_config(child, *rid, 4); 599 600 /* 601 * This bit has a different meaning depending if we are dealing 602 * with a normal BAR or an Option ROM BAR. 603 */ 604 if (((testval & 0x1) == 0x1) && (*rid != CARDBUS_ROM_REG)) { 605 device_printf(cbdev, "CIS Space is IO, expecting memory.\n"); 606 return (NULL); 607 } 608 609 size = CARDBUS_MAPREG_MEM_SIZE(testval); 610 /* XXX Is this some kind of hack? */ 611 if (size < 4096) 612 size = 4096; 613 /* allocate the memory space to read CIS */ 614 res = bus_alloc_resource(cbdev, SYS_RES_MEMORY, rid, 0, ~0, size, 615 rman_make_alignment_flags(size) | RF_ACTIVE); 616 if (res == NULL) { 617 device_printf(cbdev, "Unable to allocate resource " 618 "to read CIS.\n"); 619 return (NULL); 620 } 621 pci_write_config(child, *rid, 622 rman_get_start(res) | ((*rid == CARDBUS_ROM_REG)? 623 CARDBUS_ROM_ENABLE : 0), 624 4); 625 PCI_ENABLE_IO(cbdev, child, SYS_RES_MEMORY); 626 627 /* Flip to the right ROM image if CIS is in ROM */ 628 if (CARDBUS_CIS_SPACE(*start) == CARDBUS_CIS_ASI_ROM) { 629 bus_space_tag_t bt; 630 bus_space_handle_t bh; 631 uint32_t imagesize; 632 uint32_t imagebase = 0; 633 uint32_t pcidata; 634 uint16_t romsig; 635 int romnum = 0; 636 int imagenum; 637 638 bt = rman_get_bustag(res); 639 bh = rman_get_bushandle(res); 640 641 imagenum = CARDBUS_CIS_ASI_ROM_IMAGE(*start); 642 for (romnum = 0;; romnum++) { 643 romsig = bus_space_read_2(bt, bh, 644 imagebase + CARDBUS_EXROM_SIGNATURE); 645 if (romsig != 0xaa55) { 646 device_printf(cbdev, "Bad header in rom %d: " 647 "[%x] %04x\n", romnum, imagebase + 648 CARDBUS_EXROM_SIGNATURE, romsig); 649 bus_release_resource(cbdev, SYS_RES_MEMORY, 650 *rid, res); 651 *rid = 0; 652 return (NULL); 653 } 654 655 /* 656 * If this was the Option ROM image that we were 657 * looking for, then we are done. 658 */ 659 if (romnum == imagenum) 660 break; 661 662 /* Find out where the next Option ROM image is */ 663 pcidata = imagebase + bus_space_read_2(bt, bh, 664 imagebase + CARDBUS_EXROM_DATA_PTR); 665 imagesize = bus_space_read_2(bt, bh, 666 pcidata + CARDBUS_EXROM_DATA_IMAGE_LENGTH); 667 668 if (imagesize == 0) { 669 /* 670 * XXX some ROMs seem to have this as zero, 671 * can we assume this means 1 block? 672 */ 673 device_printf(cbdev, "Warning, size of Option " 674 "ROM image %d is 0 bytes, assuming 512 " 675 "bytes.\n", romnum); 676 imagesize = 1; 677 } 678 679 /* Image size is in 512 byte units */ 680 imagesize <<= 9; 681 682 if ((bus_space_read_1(bt, bh, pcidata + 683 CARDBUS_EXROM_DATA_INDICATOR) & 0x80) != 0) { 684 device_printf(cbdev, "Cannot find CIS in " 685 "Option ROM\n"); 686 bus_release_resource(cbdev, SYS_RES_MEMORY, 687 *rid, res); 688 *rid = 0; 689 return (NULL); 690 } 691 imagebase += imagesize; 692 } 693 *start = imagebase + CARDBUS_CIS_ADDR(*start); 694 } else { 695 *start = CARDBUS_CIS_ADDR(*start); 696 } 697 698 return (res); 699 } 700 701 /* 702 * Dispatch the right handler function per tuple 703 */ 704 705 static int 706 decode_tuple(device_t cbdev, device_t child, int tupleid, int len, 707 uint8_t *tupledata, uint32_t start, uint32_t *off, 708 struct tuple_callbacks *callbacks) 709 { 710 int i; 711 for (i = 0; callbacks[i].id != CISTPL_GENERIC; i++) { 712 if (tupleid == callbacks[i].id) 713 return (callbacks[i].func(cbdev, child, tupleid, len, 714 tupledata, start, off, &callbacks[i])); 715 } 716 717 if (tupleid < CISTPL_CUSTOMSTART) { 718 device_printf(cbdev, "Undefined tuple encountered, " 719 "CIS parsing terminated\n"); 720 return (EINVAL); 721 } 722 return (callbacks[i].func(cbdev, child, tupleid, len, 723 tupledata, start, off, NULL)); 724 } 725 726 static int 727 cardbus_parse_cis(device_t cbdev, device_t child, 728 struct tuple_callbacks *callbacks) 729 { 730 uint8_t tupledata[MAXTUPLESIZE]; 731 int tupleid; 732 int len; 733 int expect_linktarget; 734 uint32_t start, off; 735 struct resource *res; 736 int rid; 737 738 bzero(tupledata, MAXTUPLESIZE); 739 expect_linktarget = TRUE; 740 if ((start = pci_read_config(child, CARDBUS_CIS_REG, 4)) == 0) 741 return (ENXIO); 742 off = 0; 743 res = cardbus_read_tuple_init(cbdev, child, &start, &rid); 744 if (res == NULL) 745 return (ENXIO); 746 747 do { 748 if (0 != cardbus_read_tuple(cbdev, child, res, start, &off, 749 &tupleid, &len, tupledata)) { 750 device_printf(cbdev, "Failed to read CIS.\n"); 751 cardbus_read_tuple_finish(cbdev, child, rid, res); 752 return (ENXIO); 753 } 754 755 if (expect_linktarget && tupleid != CISTPL_LINKTARGET) { 756 device_printf(cbdev, "Expecting link target, got 0x%x\n", 757 tupleid); 758 cardbus_read_tuple_finish(cbdev, child, rid, res); 759 return (EINVAL); 760 } 761 expect_linktarget = decode_tuple(cbdev, child, tupleid, len, 762 tupledata, start, &off, callbacks); 763 if (expect_linktarget != 0) { 764 cardbus_read_tuple_finish(cbdev, child, rid, res); 765 return (expect_linktarget); 766 } 767 } while (tupleid != CISTPL_END); 768 cardbus_read_tuple_finish(cbdev, child, rid, res); 769 return (0); 770 } 771 772 static void 773 cardbus_do_res(struct resource_list_entry *rle, device_t child, uint32_t start) 774 { 775 rle->start = start; 776 rle->end = start + rle->count - 1; 777 pci_write_config(child, rle->rid, rle->start, 4); 778 } 779 780 static int 781 barsort(const void *a, const void *b) 782 { 783 return ((*(const struct resource_list_entry * const *)b)->count - 784 (*(const struct resource_list_entry * const *)a)->count); 785 } 786 787 static int 788 cardbus_alloc_resources(device_t cbdev, device_t child) 789 { 790 struct cardbus_devinfo *dinfo = device_get_ivars(child); 791 int count; 792 struct resource_list_entry *rle; 793 struct resource_list_entry **barlist; 794 int tmp; 795 uint32_t mem_psize = 0, mem_nsize = 0, io_size = 0; 796 struct resource *res; 797 uint32_t start,end; 798 int rid, flags; 799 800 count = 0; 801 SLIST_FOREACH(rle, &dinfo->pci.resources, link) { 802 count++; 803 } 804 if (count == 0) 805 return (0); 806 barlist = malloc(sizeof(struct resource_list_entry*) * count, M_DEVBUF, 807 M_WAITOK); 808 count = 0; 809 SLIST_FOREACH(rle, &dinfo->pci.resources, link) { 810 barlist[count] = rle; 811 if (rle->type == SYS_RES_IOPORT) { 812 io_size += rle->count; 813 } else if (rle->type == SYS_RES_MEMORY) { 814 if (dinfo->mprefetchable & BARBIT(rle->rid)) 815 mem_psize += rle->count; 816 else 817 mem_nsize += rle->count; 818 } 819 count++; 820 } 821 822 /* 823 * We want to allocate the largest resource first, so that our 824 * allocated memory is packed. 825 */ 826 qsort(barlist, count, sizeof(struct resource_list_entry*), barsort); 827 828 /* Allocate prefetchable memory */ 829 flags = 0; 830 for (tmp = 0; tmp < count; tmp++) { 831 rle = barlist[tmp]; 832 if (rle->res == NULL && 833 rle->type == SYS_RES_MEMORY && 834 dinfo->mprefetchable & BARBIT(rle->rid)) { 835 flags = rman_make_alignment_flags(rle->count); 836 break; 837 } 838 } 839 if (flags > 0) { /* If any prefetchable memory is requested... */ 840 /* 841 * First we allocate one big space for all resources of this 842 * type. We do this because our parent, pccbb, needs to open 843 * a window to forward all addresses within the window, and 844 * it would be best if nobody else has resources allocated 845 * within the window. 846 * (XXX: Perhaps there might be a better way to do this?) 847 */ 848 rid = 0; 849 res = bus_alloc_resource(cbdev, SYS_RES_MEMORY, &rid, 0, 850 (dinfo->mprefetchable & dinfo->mbelow1mb)?0xFFFFF:~0UL, 851 mem_psize, flags); 852 start = rman_get_start(res); 853 end = rman_get_end(res); 854 DEVPRINTF((cbdev, "Prefetchable memory at %x-%x\n", start, end)); 855 /* 856 * Now that we know the region is free, release it and hand it 857 * out piece by piece. 858 */ 859 bus_release_resource(cbdev, SYS_RES_MEMORY, rid, res); 860 for (tmp = 0; tmp < count; tmp++) { 861 rle = barlist[tmp]; 862 if (rle->type == SYS_RES_MEMORY && 863 dinfo->mprefetchable & BARBIT(rle->rid)) { 864 cardbus_do_res(rle, child, start); 865 start += rle->count; 866 } 867 } 868 } 869 870 /* Allocate non-prefetchable memory */ 871 flags = 0; 872 for (tmp = 0; tmp < count; tmp++) { 873 rle = barlist[tmp]; 874 if (rle->type == SYS_RES_MEMORY && 875 (dinfo->mprefetchable & BARBIT(rle->rid)) == 0) { 876 flags = rman_make_alignment_flags(rle->count); 877 break; 878 } 879 } 880 if (flags > 0) { /* If any non-prefetchable memory is requested... */ 881 /* 882 * First we allocate one big space for all resources of this 883 * type. We do this because our parent, pccbb, needs to open 884 * a window to forward all addresses within the window, and 885 * it would be best if nobody else has resources allocated 886 * within the window. 887 * (XXX: Perhaps there might be a better way to do this?) 888 */ 889 rid = 0; 890 res = bus_alloc_resource(cbdev, SYS_RES_MEMORY, &rid, 0, 891 ((~dinfo->mprefetchable) & dinfo->mbelow1mb)?0xFFFFF:~0UL, 892 mem_nsize, flags); 893 start = rman_get_start(res); 894 end = rman_get_end(res); 895 DEVPRINTF((cbdev, "Non-prefetchable memory at %x-%x\n", 896 start, end)); 897 /* 898 * Now that we know the region is free, release it and hand it 899 * out piece by piece. 900 */ 901 bus_release_resource(cbdev, SYS_RES_MEMORY, rid, res); 902 for (tmp = 0; tmp < count; tmp++) { 903 rle = barlist[tmp]; 904 if (rle->type == SYS_RES_MEMORY && 905 (dinfo->mprefetchable & BARBIT(rle->rid)) == 0) { 906 cardbus_do_res(rle, child, start); 907 start += rle->count; 908 } 909 } 910 } 911 912 /* Allocate IO ports */ 913 flags = 0; 914 for (tmp = 0; tmp < count; tmp++) { 915 rle = barlist[tmp]; 916 if (rle->type == SYS_RES_IOPORT) { 917 flags = rman_make_alignment_flags(rle->count); 918 break; 919 } 920 } 921 if (flags > 0) { /* If any IO port is requested... */ 922 /* 923 * First we allocate one big space for all resources of this 924 * type. We do this because our parent, pccbb, needs to open 925 * a window to forward all addresses within the window, and 926 * it would be best if nobody else has resources allocated 927 * within the window. 928 * (XXX: Perhaps there might be a better way to do this?) 929 */ 930 rid = 0; 931 res = bus_alloc_resource(cbdev, SYS_RES_IOPORT, &rid, 0, 932 (dinfo->ibelow1mb)?0xFFFFF:~0UL, io_size, flags); 933 start = rman_get_start(res); 934 end = rman_get_end(res); 935 DEVPRINTF((cbdev, "IO port at %x-%x\n", start, end)); 936 /* 937 * Now that we know the region is free, release it and hand it 938 * out piece by piece. 939 */ 940 bus_release_resource(cbdev, SYS_RES_IOPORT, rid, res); 941 for (tmp = 0; tmp < count; tmp++) { 942 rle = barlist[tmp]; 943 if (rle->type == SYS_RES_IOPORT) { 944 cardbus_do_res(rle, child, start); 945 start += rle->count; 946 } 947 } 948 } 949 950 /* Allocate IRQ */ 951 rid = 0; 952 res = bus_alloc_resource(cbdev, SYS_RES_IRQ, &rid, 0, ~0UL, 1, 953 RF_SHAREABLE); 954 start = rman_get_start(res); 955 end = rman_get_end(res); 956 bus_release_resource(cbdev, SYS_RES_IRQ, rid, res); 957 resource_list_add(&dinfo->pci.resources, SYS_RES_IRQ, rid, start, end, 958 1); 959 dinfo->pci.cfg.intline = rman_get_start(res); 960 pci_write_config(child, PCIR_INTLINE, rman_get_start(res), 1); 961 962 free(barlist, M_DEVBUF); 963 return (0); 964 } 965 966 /* 967 * Adding a memory/io resource (sans CIS) 968 */ 969 970 static void 971 cardbus_add_map(device_t cbdev, device_t child, int reg) 972 { 973 struct cardbus_devinfo *dinfo = device_get_ivars(child); 974 struct resource_list_entry *rle; 975 uint32_t size; 976 uint32_t testval; 977 int type; 978 979 SLIST_FOREACH(rle, &dinfo->pci.resources, link) { 980 if (rle->rid == reg) 981 return; 982 } 983 984 if (reg == CARDBUS_ROM_REG) 985 testval = CARDBUS_ROM_ADDRMASK; 986 else 987 testval = ~0; 988 989 pci_write_config(child, reg, testval, 4); 990 testval = pci_read_config(child, reg, 4); 991 992 if (testval == ~0 || testval == 0) 993 return; 994 995 if ((testval & 1) == 0) 996 type = SYS_RES_MEMORY; 997 else 998 type = SYS_RES_IOPORT; 999 1000 size = CARDBUS_MAPREG_MEM_SIZE(testval); 1001 device_printf(cbdev, "Resource not specified in CIS: id=%x, size=%x\n", 1002 reg, size); 1003 resource_list_add(&dinfo->pci.resources, type, reg, 0UL, ~0UL, size); 1004 } 1005 1006 static void 1007 cardbus_pickup_maps(device_t cbdev, device_t child) 1008 { 1009 struct cardbus_devinfo *dinfo = device_get_ivars(child); 1010 struct cardbus_quirk *q; 1011 int reg; 1012 1013 /* 1014 * Try to pick up any resources that was not specified in CIS. 1015 * Some devices (eg, 3c656) does not list all resources required by 1016 * the driver in its CIS. 1017 * XXX: should we do this or use quirks? 1018 */ 1019 for (reg = 0; reg < dinfo->pci.cfg.nummaps; reg++) { 1020 cardbus_add_map(cbdev, child, PCIR_MAPS + reg * 4); 1021 } 1022 1023 for (q = &cardbus_quirks[0]; q->devid; q++) { 1024 if (q->devid == ((dinfo->pci.cfg.device << 16) | dinfo->pci.cfg.vendor) 1025 && q->type == CARDBUS_QUIRK_MAP_REG) { 1026 cardbus_add_map(cbdev, child, q->arg1); 1027 } 1028 } 1029 } 1030 1031 int 1032 cardbus_cis_read(device_t cbdev, device_t child, uint8_t id, 1033 struct cis_tupleinfo **buff, int *nret) 1034 { 1035 struct tuple_callbacks cisread_callbacks[] = { 1036 MAKETUPLE(NULL, nothing), 1037 /* first entry will be overwritten */ 1038 MAKETUPLE(NULL, nothing), 1039 MAKETUPLE(DEVICE, nothing), 1040 MAKETUPLE(LONG_LINK_CB, unhandled), 1041 MAKETUPLE(INDIRECT, unhandled), 1042 MAKETUPLE(CONFIG_CB, nothing), 1043 MAKETUPLE(CFTABLE_ENTRY_CB, nothing), 1044 MAKETUPLE(LONGLINK_MFC, unhandled), 1045 MAKETUPLE(BAR, nothing), 1046 MAKETUPLE(PWR_MGMNT, nothing), 1047 MAKETUPLE(EXTDEVICE, nothing), 1048 MAKETUPLE(CHECKSUM, nothing), 1049 MAKETUPLE(LONGLINK_A, unhandled), 1050 MAKETUPLE(LONGLINK_C, unhandled), 1051 MAKETUPLE(LINKTARGET, nothing), 1052 MAKETUPLE(NO_LINK, nothing), 1053 MAKETUPLE(VERS_1, nothing), 1054 MAKETUPLE(ALTSTR, nothing), 1055 MAKETUPLE(DEVICE_A, nothing), 1056 MAKETUPLE(JEDEC_C, nothing), 1057 MAKETUPLE(JEDEC_A, nothing), 1058 MAKETUPLE(CONFIG, nothing), 1059 MAKETUPLE(CFTABLE_ENTRY, nothing), 1060 MAKETUPLE(DEVICE_OC, nothing), 1061 MAKETUPLE(DEVICE_OA, nothing), 1062 MAKETUPLE(DEVICE_GEO, nothing), 1063 MAKETUPLE(DEVICE_GEO_A, nothing), 1064 MAKETUPLE(MANFID, nothing), 1065 MAKETUPLE(FUNCID, nothing), 1066 MAKETUPLE(FUNCE, nothing), 1067 MAKETUPLE(SWIL, nothing), 1068 MAKETUPLE(VERS_2, nothing), 1069 MAKETUPLE(FORMAT, nothing), 1070 MAKETUPLE(GEOMETRY, nothing), 1071 MAKETUPLE(BYTEORDER, nothing), 1072 MAKETUPLE(DATE, nothing), 1073 MAKETUPLE(BATTERY, nothing), 1074 MAKETUPLE(ORG, nothing), 1075 MAKETUPLE(END, end), 1076 MAKETUPLE(GENERIC, nothing), 1077 }; 1078 int ret; 1079 1080 cisread_callbacks[0].id = id; 1081 cisread_callbacks[0].name = "COPY"; 1082 cisread_callbacks[0].func = decode_tuple_copy; 1083 ncisread_buf = 0; 1084 cisread_buf = NULL; 1085 ret = cardbus_parse_cis(cbdev, child, cisread_callbacks); 1086 1087 *buff = cisread_buf; 1088 *nret = ncisread_buf; 1089 return (ret); 1090 } 1091 1092 void 1093 cardbus_cis_free(device_t cbdev, struct cis_tupleinfo *buff, int *nret) 1094 { 1095 int i; 1096 for (i = 0; i < *nret; i++) 1097 free(buff[i].data, M_DEVBUF); 1098 if (*nret > 0) 1099 free(buff, M_DEVBUF); 1100 } 1101 1102 int 1103 cardbus_do_cis(device_t cbdev, device_t child) 1104 { 1105 int ret; 1106 struct tuple_callbacks init_callbacks[] = { 1107 MAKETUPLE(NULL, generic), 1108 MAKETUPLE(DEVICE, generic), 1109 MAKETUPLE(LONG_LINK_CB, unhandled), 1110 MAKETUPLE(INDIRECT, unhandled), 1111 MAKETUPLE(CONFIG_CB, generic), 1112 MAKETUPLE(CFTABLE_ENTRY_CB, generic), 1113 MAKETUPLE(LONGLINK_MFC, unhandled), 1114 MAKETUPLE(BAR, bar), 1115 MAKETUPLE(PWR_MGMNT, generic), 1116 MAKETUPLE(EXTDEVICE, generic), 1117 MAKETUPLE(CHECKSUM, generic), 1118 MAKETUPLE(LONGLINK_A, unhandled), 1119 MAKETUPLE(LONGLINK_C, unhandled), 1120 MAKETUPLE(LINKTARGET, linktarget), 1121 MAKETUPLE(NO_LINK, generic), 1122 MAKETUPLE(VERS_1, vers_1), 1123 MAKETUPLE(ALTSTR, generic), 1124 MAKETUPLE(DEVICE_A, generic), 1125 MAKETUPLE(JEDEC_C, generic), 1126 MAKETUPLE(JEDEC_A, generic), 1127 MAKETUPLE(CONFIG, generic), 1128 MAKETUPLE(CFTABLE_ENTRY, generic), 1129 MAKETUPLE(DEVICE_OC, generic), 1130 MAKETUPLE(DEVICE_OA, generic), 1131 MAKETUPLE(DEVICE_GEO, generic), 1132 MAKETUPLE(DEVICE_GEO_A, generic), 1133 MAKETUPLE(MANFID, manfid), 1134 MAKETUPLE(FUNCID, funcid), 1135 MAKETUPLE(FUNCE, funce), 1136 MAKETUPLE(SWIL, generic), 1137 MAKETUPLE(VERS_2, generic), 1138 MAKETUPLE(FORMAT, generic), 1139 MAKETUPLE(GEOMETRY, generic), 1140 MAKETUPLE(BYTEORDER, generic), 1141 MAKETUPLE(DATE, generic), 1142 MAKETUPLE(BATTERY, generic), 1143 MAKETUPLE(ORG, generic), 1144 MAKETUPLE(END, end), 1145 MAKETUPLE(GENERIC, generic), 1146 }; 1147 1148 ret = cardbus_parse_cis(cbdev, child, init_callbacks); 1149 if (ret < 0) 1150 return (ret); 1151 cardbus_pickup_maps(cbdev, child); 1152 return (cardbus_alloc_resources(cbdev, child)); 1153 } 1154