1 /*- 2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org> 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 unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_bus.h" /* XXX trim includes */ 31 #include "opt_compat.h" 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/malloc.h> 36 #include <sys/module.h> 37 #include <sys/linker.h> 38 #include <sys/fcntl.h> 39 #include <sys/conf.h> 40 #include <sys/kernel.h> 41 #include <sys/proc.h> 42 #include <sys/queue.h> 43 #include <sys/types.h> 44 45 #include <vm/vm.h> 46 #include <vm/pmap.h> 47 #include <vm/vm_extern.h> 48 49 #include <sys/bus.h> 50 #include <machine/bus.h> 51 #include <sys/rman.h> 52 #include <machine/resource.h> 53 54 #include <sys/pciio.h> 55 #include <dev/pci/pcireg.h> 56 #include <dev/pci/pcivar.h> 57 58 #include "pcib_if.h" 59 #include "pci_if.h" 60 61 /* 62 * This is the user interface to PCI configuration space. 63 */ 64 65 static d_open_t pci_open; 66 static d_close_t pci_close; 67 static int pci_conf_match(struct pci_match_conf *matches, int num_matches, 68 struct pci_conf *match_buf); 69 static d_ioctl_t pci_ioctl; 70 71 struct cdevsw pcicdev = { 72 .d_version = D_VERSION, 73 .d_flags = D_NEEDGIANT, 74 .d_open = pci_open, 75 .d_close = pci_close, 76 .d_ioctl = pci_ioctl, 77 .d_name = "pci", 78 }; 79 80 static int 81 pci_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 82 { 83 int error; 84 85 if (oflags & FWRITE) { 86 error = securelevel_gt(td->td_ucred, 0); 87 if (error) 88 return (error); 89 } 90 91 return (0); 92 } 93 94 static int 95 pci_close(struct cdev *dev, int flag, int devtype, struct thread *td) 96 { 97 return 0; 98 } 99 100 /* 101 * Match a single pci_conf structure against an array of pci_match_conf 102 * structures. The first argument, 'matches', is an array of num_matches 103 * pci_match_conf structures. match_buf is a pointer to the pci_conf 104 * structure that will be compared to every entry in the matches array. 105 * This function returns 1 on failure, 0 on success. 106 */ 107 static int 108 pci_conf_match(struct pci_match_conf *matches, int num_matches, 109 struct pci_conf *match_buf) 110 { 111 int i; 112 113 if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0)) 114 return(1); 115 116 for (i = 0; i < num_matches; i++) { 117 /* 118 * I'm not sure why someone would do this...but... 119 */ 120 if (matches[i].flags == PCI_GETCONF_NO_MATCH) 121 continue; 122 123 /* 124 * Look at each of the match flags. If it's set, do the 125 * comparison. If the comparison fails, we don't have a 126 * match, go on to the next item if there is one. 127 */ 128 if (((matches[i].flags & PCI_GETCONF_MATCH_DOMAIN) != 0) 129 && (match_buf->pc_sel.pc_domain != 130 matches[i].pc_sel.pc_domain)) 131 continue; 132 133 if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0) 134 && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus)) 135 continue; 136 137 if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0) 138 && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev)) 139 continue; 140 141 if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0) 142 && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func)) 143 continue; 144 145 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0) 146 && (match_buf->pc_vendor != matches[i].pc_vendor)) 147 continue; 148 149 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0) 150 && (match_buf->pc_device != matches[i].pc_device)) 151 continue; 152 153 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0) 154 && (match_buf->pc_class != matches[i].pc_class)) 155 continue; 156 157 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0) 158 && (match_buf->pd_unit != matches[i].pd_unit)) 159 continue; 160 161 if (((matches[i].flags & PCI_GETCONF_MATCH_NAME) != 0) 162 && (strncmp(matches[i].pd_name, match_buf->pd_name, 163 sizeof(match_buf->pd_name)) != 0)) 164 continue; 165 166 return(0); 167 } 168 169 return(1); 170 } 171 172 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 173 defined(COMPAT_FREEBSD6) 174 #define PRE7_COMPAT 175 176 typedef enum { 177 PCI_GETCONF_NO_MATCH_OLD = 0x00, 178 PCI_GETCONF_MATCH_BUS_OLD = 0x01, 179 PCI_GETCONF_MATCH_DEV_OLD = 0x02, 180 PCI_GETCONF_MATCH_FUNC_OLD = 0x04, 181 PCI_GETCONF_MATCH_NAME_OLD = 0x08, 182 PCI_GETCONF_MATCH_UNIT_OLD = 0x10, 183 PCI_GETCONF_MATCH_VENDOR_OLD = 0x20, 184 PCI_GETCONF_MATCH_DEVICE_OLD = 0x40, 185 PCI_GETCONF_MATCH_CLASS_OLD = 0x80 186 } pci_getconf_flags_old; 187 188 struct pcisel_old { 189 u_int8_t pc_bus; /* bus number */ 190 u_int8_t pc_dev; /* device on this bus */ 191 u_int8_t pc_func; /* function on this device */ 192 }; 193 194 struct pci_conf_old { 195 struct pcisel_old pc_sel; /* bus+slot+function */ 196 u_int8_t pc_hdr; /* PCI header type */ 197 u_int16_t pc_subvendor; /* card vendor ID */ 198 u_int16_t pc_subdevice; /* card device ID, assigned by 199 card vendor */ 200 u_int16_t pc_vendor; /* chip vendor ID */ 201 u_int16_t pc_device; /* chip device ID, assigned by 202 chip vendor */ 203 u_int8_t pc_class; /* chip PCI class */ 204 u_int8_t pc_subclass; /* chip PCI subclass */ 205 u_int8_t pc_progif; /* chip PCI programming interface */ 206 u_int8_t pc_revid; /* chip revision ID */ 207 char pd_name[PCI_MAXNAMELEN + 1]; /* device name */ 208 u_long pd_unit; /* device unit number */ 209 }; 210 211 struct pci_match_conf_old { 212 struct pcisel_old pc_sel; /* bus+slot+function */ 213 char pd_name[PCI_MAXNAMELEN + 1]; /* device name */ 214 u_long pd_unit; /* Unit number */ 215 u_int16_t pc_vendor; /* PCI Vendor ID */ 216 u_int16_t pc_device; /* PCI Device ID */ 217 u_int8_t pc_class; /* PCI class */ 218 pci_getconf_flags_old flags; /* Matching expression */ 219 }; 220 221 struct pci_io_old { 222 struct pcisel_old pi_sel; /* device to operate on */ 223 int pi_reg; /* configuration register to examine */ 224 int pi_width; /* width (in bytes) of read or write */ 225 u_int32_t pi_data; /* data to write or result of read */ 226 }; 227 228 #ifdef COMPAT_FREEBSD32 229 struct pci_conf_old32 { 230 struct pcisel_old pc_sel; /* bus+slot+function */ 231 u_int8_t pc_hdr; /* PCI header type */ 232 u_int16_t pc_subvendor; /* card vendor ID */ 233 u_int16_t pc_subdevice; /* card device ID, assigned by 234 card vendor */ 235 u_int16_t pc_vendor; /* chip vendor ID */ 236 u_int16_t pc_device; /* chip device ID, assigned by 237 chip vendor */ 238 u_int8_t pc_class; /* chip PCI class */ 239 u_int8_t pc_subclass; /* chip PCI subclass */ 240 u_int8_t pc_progif; /* chip PCI programming interface */ 241 u_int8_t pc_revid; /* chip revision ID */ 242 char pd_name[PCI_MAXNAMELEN + 1]; /* device name */ 243 u_int32_t pd_unit; /* device unit number (u_long) */ 244 }; 245 246 struct pci_match_conf_old32 { 247 struct pcisel_old pc_sel; /* bus+slot+function */ 248 char pd_name[PCI_MAXNAMELEN + 1]; /* device name */ 249 u_int32_t pd_unit; /* Unit number (u_long) */ 250 u_int16_t pc_vendor; /* PCI Vendor ID */ 251 u_int16_t pc_device; /* PCI Device ID */ 252 u_int8_t pc_class; /* PCI class */ 253 pci_getconf_flags_old flags; /* Matching expression */ 254 }; 255 256 struct pci_conf_io32 { 257 u_int32_t pat_buf_len; /* pattern buffer length */ 258 u_int32_t num_patterns; /* number of patterns */ 259 u_int32_t patterns; /* pattern buffer (struct pci_match_conf_old32 *) */ 260 u_int32_t match_buf_len; /* match buffer length */ 261 u_int32_t num_matches; /* number of matches returned */ 262 u_int32_t matches; /* match buffer (struct pci_conf_old32 *) */ 263 u_int32_t offset; /* offset into device list */ 264 u_int32_t generation; /* device list generation */ 265 pci_getconf_status status; /* request status */ 266 }; 267 268 #define PCIOCGETCONF_OLD32 _IOWR('p', 1, struct pci_conf_io32) 269 #endif 270 271 #define PCIOCGETCONF_OLD _IOWR('p', 1, struct pci_conf_io) 272 #define PCIOCREAD_OLD _IOWR('p', 2, struct pci_io_old) 273 #define PCIOCWRITE_OLD _IOWR('p', 3, struct pci_io_old) 274 275 static int pci_conf_match_old(struct pci_match_conf_old *matches, 276 int num_matches, struct pci_conf *match_buf); 277 278 static int 279 pci_conf_match_old(struct pci_match_conf_old *matches, int num_matches, 280 struct pci_conf *match_buf) 281 { 282 int i; 283 284 if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0)) 285 return(1); 286 287 for (i = 0; i < num_matches; i++) { 288 if (match_buf->pc_sel.pc_domain != 0) 289 continue; 290 291 /* 292 * I'm not sure why someone would do this...but... 293 */ 294 if (matches[i].flags == PCI_GETCONF_NO_MATCH_OLD) 295 continue; 296 297 /* 298 * Look at each of the match flags. If it's set, do the 299 * comparison. If the comparison fails, we don't have a 300 * match, go on to the next item if there is one. 301 */ 302 if (((matches[i].flags & PCI_GETCONF_MATCH_BUS_OLD) != 0) 303 && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus)) 304 continue; 305 306 if (((matches[i].flags & PCI_GETCONF_MATCH_DEV_OLD) != 0) 307 && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev)) 308 continue; 309 310 if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC_OLD) != 0) 311 && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func)) 312 continue; 313 314 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR_OLD) != 0) 315 && (match_buf->pc_vendor != matches[i].pc_vendor)) 316 continue; 317 318 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE_OLD) != 0) 319 && (match_buf->pc_device != matches[i].pc_device)) 320 continue; 321 322 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS_OLD) != 0) 323 && (match_buf->pc_class != matches[i].pc_class)) 324 continue; 325 326 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT_OLD) != 0) 327 && (match_buf->pd_unit != matches[i].pd_unit)) 328 continue; 329 330 if (((matches[i].flags & PCI_GETCONF_MATCH_NAME_OLD) != 0) 331 && (strncmp(matches[i].pd_name, match_buf->pd_name, 332 sizeof(match_buf->pd_name)) != 0)) 333 continue; 334 335 return(0); 336 } 337 338 return(1); 339 } 340 341 static int 342 pci_conf_match_old32(struct pci_match_conf_old32 *matches, int num_matches, 343 struct pci_conf *match_buf) 344 { 345 int i; 346 347 if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0)) 348 return(1); 349 350 for (i = 0; i < num_matches; i++) { 351 if (match_buf->pc_sel.pc_domain != 0) 352 continue; 353 354 /* 355 * I'm not sure why someone would do this...but... 356 */ 357 if (matches[i].flags == PCI_GETCONF_NO_MATCH_OLD) 358 continue; 359 360 /* 361 * Look at each of the match flags. If it's set, do the 362 * comparison. If the comparison fails, we don't have a 363 * match, go on to the next item if there is one. 364 */ 365 if (((matches[i].flags & PCI_GETCONF_MATCH_BUS_OLD) != 0) 366 && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus)) 367 continue; 368 369 if (((matches[i].flags & PCI_GETCONF_MATCH_DEV_OLD) != 0) 370 && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev)) 371 continue; 372 373 if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC_OLD) != 0) 374 && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func)) 375 continue; 376 377 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR_OLD) != 0) 378 && (match_buf->pc_vendor != matches[i].pc_vendor)) 379 continue; 380 381 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE_OLD) != 0) 382 && (match_buf->pc_device != matches[i].pc_device)) 383 continue; 384 385 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS_OLD) != 0) 386 && (match_buf->pc_class != matches[i].pc_class)) 387 continue; 388 389 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT_OLD) != 0) 390 && ((u_int32_t)match_buf->pd_unit != matches[i].pd_unit)) 391 continue; 392 393 if (((matches[i].flags & PCI_GETCONF_MATCH_NAME_OLD) != 0) 394 && (strncmp(matches[i].pd_name, match_buf->pd_name, 395 sizeof(match_buf->pd_name)) != 0)) 396 continue; 397 398 return(0); 399 } 400 401 return(1); 402 } 403 404 #endif 405 406 static int 407 pci_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 408 { 409 device_t pcidev, brdev; 410 void *confdata; 411 const char *name; 412 struct devlist *devlist_head; 413 struct pci_conf_io *cio; 414 struct pci_devinfo *dinfo; 415 struct pci_io *io; 416 struct pci_bar_io *bio; 417 struct pci_match_conf *pattern_buf; 418 struct pci_map *pm; 419 size_t confsz, iolen, pbufsz; 420 int error, ionum, i, num_patterns; 421 #ifdef PRE7_COMPAT 422 #ifdef COMPAT_FREEBSD32 423 struct pci_conf_io32 *cio32; 424 #endif 425 struct pci_conf_old conf_old; 426 struct pci_conf_old32 conf_old32; 427 struct pci_io iodata; 428 struct pci_io_old *io_old; 429 struct pci_match_conf_old *pattern_buf_old; 430 struct pci_match_conf_old32 *pattern_buf_old32; 431 432 cio = NULL; 433 cio32 = NULL; 434 io_old = NULL; 435 pattern_buf_old = NULL; 436 pattern_buf_old32 = NULL; 437 438 if (!(flag & FWRITE) && cmd != PCIOCGETBAR && 439 cmd != PCIOCGETCONF && cmd != PCIOCGETCONF_OLD) 440 return EPERM; 441 #else 442 if (!(flag & FWRITE) && cmd != PCIOCGETBAR && cmd != PCIOCGETCONF) 443 return EPERM; 444 #endif 445 446 switch(cmd) { 447 #ifdef PRE7_COMPAT 448 case PCIOCGETCONF_OLD32: 449 cio32 = (struct pci_conf_io32 *)data; 450 cio = malloc(sizeof(struct pci_conf_io), M_TEMP, M_WAITOK); 451 cio->pat_buf_len = cio32->pat_buf_len; 452 cio->num_patterns = cio32->num_patterns; 453 cio->patterns = (void *)(uintptr_t)cio32->patterns; 454 cio->match_buf_len = cio32->match_buf_len; 455 cio->num_matches = cio32->num_matches; 456 cio->matches = (void *)(uintptr_t)cio32->matches; 457 cio->offset = cio32->offset; 458 cio->generation = cio32->generation; 459 cio->status = cio32->status; 460 cio32->num_matches = 0; 461 /* FALLTHROUGH */ 462 463 case PCIOCGETCONF_OLD: 464 /* FALLTHROUGH */ 465 #endif 466 case PCIOCGETCONF: 467 if (cio == NULL) 468 cio = (struct pci_conf_io *)data; 469 470 pattern_buf = NULL; 471 num_patterns = 0; 472 dinfo = NULL; 473 474 cio->num_matches = 0; 475 476 /* 477 * If the user specified an offset into the device list, 478 * but the list has changed since they last called this 479 * ioctl, tell them that the list has changed. They will 480 * have to get the list from the beginning. 481 */ 482 if ((cio->offset != 0) 483 && (cio->generation != pci_generation)){ 484 cio->status = PCI_GETCONF_LIST_CHANGED; 485 error = 0; 486 goto getconfexit; 487 } 488 489 /* 490 * Check to see whether the user has asked for an offset 491 * past the end of our list. 492 */ 493 if (cio->offset >= pci_numdevs) { 494 cio->status = PCI_GETCONF_LAST_DEVICE; 495 error = 0; 496 goto getconfexit; 497 } 498 499 /* get the head of the device queue */ 500 devlist_head = &pci_devq; 501 502 /* 503 * Determine how much room we have for pci_conf structures. 504 * Round the user's buffer size down to the nearest 505 * multiple of sizeof(struct pci_conf) in case the user 506 * didn't specify a multiple of that size. 507 */ 508 #ifdef PRE7_COMPAT 509 #ifdef COMPAT_FREEBSD32 510 if (cmd == PCIOCGETCONF_OLD32) 511 confsz = sizeof(struct pci_conf_old32); 512 else 513 #endif 514 if (cmd == PCIOCGETCONF_OLD) 515 confsz = sizeof(struct pci_conf_old); 516 else 517 #endif 518 confsz = sizeof(struct pci_conf); 519 iolen = min(cio->match_buf_len - (cio->match_buf_len % confsz), 520 pci_numdevs * confsz); 521 522 /* 523 * Since we know that iolen is a multiple of the size of 524 * the pciconf union, it's okay to do this. 525 */ 526 ionum = iolen / confsz; 527 528 /* 529 * If this test is true, the user wants the pci_conf 530 * structures returned to match the supplied entries. 531 */ 532 if ((cio->num_patterns > 0) && (cio->num_patterns < pci_numdevs) 533 && (cio->pat_buf_len > 0)) { 534 /* 535 * pat_buf_len needs to be: 536 * num_patterns * sizeof(struct pci_match_conf) 537 * While it is certainly possible the user just 538 * allocated a large buffer, but set the number of 539 * matches correctly, it is far more likely that 540 * their kernel doesn't match the userland utility 541 * they're using. It's also possible that the user 542 * forgot to initialize some variables. Yes, this 543 * may be overly picky, but I hazard to guess that 544 * it's far more likely to just catch folks that 545 * updated their kernel but not their userland. 546 */ 547 #ifdef PRE7_COMPAT 548 #ifdef COMPAT_FREEBSD32 549 if (cmd == PCIOCGETCONF_OLD32) 550 pbufsz = sizeof(struct pci_match_conf_old32); 551 else 552 #endif 553 if (cmd == PCIOCGETCONF_OLD) 554 pbufsz = sizeof(struct pci_match_conf_old); 555 else 556 #endif 557 pbufsz = sizeof(struct pci_match_conf); 558 if (cio->num_patterns * pbufsz != cio->pat_buf_len) { 559 /* The user made a mistake, return an error. */ 560 cio->status = PCI_GETCONF_ERROR; 561 error = EINVAL; 562 goto getconfexit; 563 } 564 565 /* 566 * Allocate a buffer to hold the patterns. 567 */ 568 #ifdef PRE7_COMPAT 569 if (cmd == PCIOCGETCONF_OLD32) { 570 pattern_buf_old32 = malloc(cio->pat_buf_len, 571 M_TEMP, M_WAITOK); 572 error = copyin(cio->patterns, 573 pattern_buf_old32, cio->pat_buf_len); 574 } else 575 if (cmd == PCIOCGETCONF_OLD) { 576 pattern_buf_old = malloc(cio->pat_buf_len, 577 M_TEMP, M_WAITOK); 578 error = copyin(cio->patterns, 579 pattern_buf_old, cio->pat_buf_len); 580 } else 581 #endif 582 { 583 pattern_buf = malloc(cio->pat_buf_len, M_TEMP, 584 M_WAITOK); 585 error = copyin(cio->patterns, pattern_buf, 586 cio->pat_buf_len); 587 } 588 if (error != 0) { 589 error = EINVAL; 590 goto getconfexit; 591 } 592 num_patterns = cio->num_patterns; 593 } else if ((cio->num_patterns > 0) 594 || (cio->pat_buf_len > 0)) { 595 /* 596 * The user made a mistake, spit out an error. 597 */ 598 cio->status = PCI_GETCONF_ERROR; 599 error = EINVAL; 600 goto getconfexit; 601 } 602 603 /* 604 * Go through the list of devices and copy out the devices 605 * that match the user's criteria. 606 */ 607 for (cio->num_matches = 0, error = 0, i = 0, 608 dinfo = STAILQ_FIRST(devlist_head); 609 (dinfo != NULL) && (cio->num_matches < ionum) 610 && (error == 0) && (i < pci_numdevs) && (dinfo != NULL); 611 dinfo = STAILQ_NEXT(dinfo, pci_links), i++) { 612 613 if (i < cio->offset) 614 continue; 615 616 /* Populate pd_name and pd_unit */ 617 name = NULL; 618 if (dinfo->cfg.dev) 619 name = device_get_name(dinfo->cfg.dev); 620 if (name) { 621 strncpy(dinfo->conf.pd_name, name, 622 sizeof(dinfo->conf.pd_name)); 623 dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0; 624 dinfo->conf.pd_unit = 625 device_get_unit(dinfo->cfg.dev); 626 } else { 627 dinfo->conf.pd_name[0] = '\0'; 628 dinfo->conf.pd_unit = 0; 629 } 630 631 #ifdef PRE7_COMPAT 632 if ((cmd == PCIOCGETCONF_OLD32 && 633 (pattern_buf_old32 == NULL || 634 pci_conf_match_old32(pattern_buf_old32, 635 num_patterns, &dinfo->conf) == 0)) || 636 (cmd == PCIOCGETCONF_OLD && 637 (pattern_buf_old == NULL || 638 pci_conf_match_old(pattern_buf_old, num_patterns, 639 &dinfo->conf) == 0)) || 640 (cmd == PCIOCGETCONF && 641 (pattern_buf == NULL || 642 pci_conf_match(pattern_buf, num_patterns, 643 &dinfo->conf) == 0))) { 644 #else 645 if (pattern_buf == NULL || 646 pci_conf_match(pattern_buf, num_patterns, 647 &dinfo->conf) == 0) { 648 #endif 649 /* 650 * If we've filled up the user's buffer, 651 * break out at this point. Since we've 652 * got a match here, we'll pick right back 653 * up at the matching entry. We can also 654 * tell the user that there are more matches 655 * left. 656 */ 657 if (cio->num_matches >= ionum) 658 break; 659 660 #ifdef PRE7_COMPAT 661 if (cmd == PCIOCGETCONF_OLD32) { 662 conf_old32.pc_sel.pc_bus = 663 dinfo->conf.pc_sel.pc_bus; 664 conf_old32.pc_sel.pc_dev = 665 dinfo->conf.pc_sel.pc_dev; 666 conf_old32.pc_sel.pc_func = 667 dinfo->conf.pc_sel.pc_func; 668 conf_old32.pc_hdr = dinfo->conf.pc_hdr; 669 conf_old32.pc_subvendor = 670 dinfo->conf.pc_subvendor; 671 conf_old32.pc_subdevice = 672 dinfo->conf.pc_subdevice; 673 conf_old32.pc_vendor = 674 dinfo->conf.pc_vendor; 675 conf_old32.pc_device = 676 dinfo->conf.pc_device; 677 conf_old32.pc_class = 678 dinfo->conf.pc_class; 679 conf_old32.pc_subclass = 680 dinfo->conf.pc_subclass; 681 conf_old32.pc_progif = 682 dinfo->conf.pc_progif; 683 conf_old32.pc_revid = 684 dinfo->conf.pc_revid; 685 strncpy(conf_old32.pd_name, 686 dinfo->conf.pd_name, 687 sizeof(conf_old32.pd_name)); 688 conf_old32.pd_name[PCI_MAXNAMELEN] = 0; 689 conf_old32.pd_unit = 690 (u_int32_t)dinfo->conf.pd_unit; 691 confdata = &conf_old32; 692 } else 693 if (cmd == PCIOCGETCONF_OLD) { 694 conf_old.pc_sel.pc_bus = 695 dinfo->conf.pc_sel.pc_bus; 696 conf_old.pc_sel.pc_dev = 697 dinfo->conf.pc_sel.pc_dev; 698 conf_old.pc_sel.pc_func = 699 dinfo->conf.pc_sel.pc_func; 700 conf_old.pc_hdr = dinfo->conf.pc_hdr; 701 conf_old.pc_subvendor = 702 dinfo->conf.pc_subvendor; 703 conf_old.pc_subdevice = 704 dinfo->conf.pc_subdevice; 705 conf_old.pc_vendor = 706 dinfo->conf.pc_vendor; 707 conf_old.pc_device = 708 dinfo->conf.pc_device; 709 conf_old.pc_class = 710 dinfo->conf.pc_class; 711 conf_old.pc_subclass = 712 dinfo->conf.pc_subclass; 713 conf_old.pc_progif = 714 dinfo->conf.pc_progif; 715 conf_old.pc_revid = 716 dinfo->conf.pc_revid; 717 strncpy(conf_old.pd_name, 718 dinfo->conf.pd_name, 719 sizeof(conf_old.pd_name)); 720 conf_old.pd_name[PCI_MAXNAMELEN] = 0; 721 conf_old.pd_unit = 722 dinfo->conf.pd_unit; 723 confdata = &conf_old; 724 } else 725 #endif 726 confdata = &dinfo->conf; 727 /* Only if we can copy it out do we count it. */ 728 if (!(error = copyout(confdata, 729 (caddr_t)cio->matches + 730 confsz * cio->num_matches, confsz))) 731 cio->num_matches++; 732 } 733 } 734 735 /* 736 * Set the pointer into the list, so if the user is getting 737 * n records at a time, where n < pci_numdevs, 738 */ 739 cio->offset = i; 740 741 /* 742 * Set the generation, the user will need this if they make 743 * another ioctl call with offset != 0. 744 */ 745 cio->generation = pci_generation; 746 747 /* 748 * If this is the last device, inform the user so he won't 749 * bother asking for more devices. If dinfo isn't NULL, we 750 * know that there are more matches in the list because of 751 * the way the traversal is done. 752 */ 753 if (dinfo == NULL) 754 cio->status = PCI_GETCONF_LAST_DEVICE; 755 else 756 cio->status = PCI_GETCONF_MORE_DEVS; 757 758 getconfexit: 759 #ifdef COMPAT_FREEBSD32 760 if (cmd == PCIOCGETCONF_OLD32) { 761 cio32->status = cio->status; 762 cio32->generation = cio->generation; 763 cio32->offset = cio->offset; 764 cio32->num_matches = cio->num_matches; 765 if (cio != NULL) 766 free(cio, M_TEMP); 767 } 768 #endif 769 770 if (pattern_buf != NULL) 771 free(pattern_buf, M_TEMP); 772 #ifdef PRE7_COMPAT 773 if (pattern_buf_old32 != NULL) 774 free(pattern_buf_old32, M_TEMP); 775 if (pattern_buf_old != NULL) 776 free(pattern_buf_old, M_TEMP); 777 #endif 778 779 break; 780 781 #ifdef PRE7_COMPAT 782 case PCIOCREAD_OLD: 783 case PCIOCWRITE_OLD: 784 io_old = (struct pci_io_old *)data; 785 iodata.pi_sel.pc_domain = 0; 786 iodata.pi_sel.pc_bus = io_old->pi_sel.pc_bus; 787 iodata.pi_sel.pc_dev = io_old->pi_sel.pc_dev; 788 iodata.pi_sel.pc_func = io_old->pi_sel.pc_func; 789 iodata.pi_reg = io_old->pi_reg; 790 iodata.pi_width = io_old->pi_width; 791 iodata.pi_data = io_old->pi_data; 792 data = (caddr_t)&iodata; 793 /* FALLTHROUGH */ 794 #endif 795 case PCIOCREAD: 796 case PCIOCWRITE: 797 io = (struct pci_io *)data; 798 switch(io->pi_width) { 799 case 4: 800 case 2: 801 case 1: 802 /* Make sure register is not negative and aligned. */ 803 if (io->pi_reg < 0 || 804 io->pi_reg & (io->pi_width - 1)) { 805 error = EINVAL; 806 break; 807 } 808 /* 809 * Assume that the user-level bus number is 810 * in fact the physical PCI bus number. 811 * Look up the grandparent, i.e. the bridge device, 812 * so that we can issue configuration space cycles. 813 */ 814 pcidev = pci_find_dbsf(io->pi_sel.pc_domain, 815 io->pi_sel.pc_bus, io->pi_sel.pc_dev, 816 io->pi_sel.pc_func); 817 if (pcidev) { 818 brdev = device_get_parent( 819 device_get_parent(pcidev)); 820 821 #ifdef PRE7_COMPAT 822 if (cmd == PCIOCWRITE || cmd == PCIOCWRITE_OLD) 823 #else 824 if (cmd == PCIOCWRITE) 825 #endif 826 PCIB_WRITE_CONFIG(brdev, 827 io->pi_sel.pc_bus, 828 io->pi_sel.pc_dev, 829 io->pi_sel.pc_func, 830 io->pi_reg, 831 io->pi_data, 832 io->pi_width); 833 #ifdef PRE7_COMPAT 834 else if (cmd == PCIOCREAD_OLD) 835 io_old->pi_data = 836 PCIB_READ_CONFIG(brdev, 837 io->pi_sel.pc_bus, 838 io->pi_sel.pc_dev, 839 io->pi_sel.pc_func, 840 io->pi_reg, 841 io->pi_width); 842 #endif 843 else 844 io->pi_data = 845 PCIB_READ_CONFIG(brdev, 846 io->pi_sel.pc_bus, 847 io->pi_sel.pc_dev, 848 io->pi_sel.pc_func, 849 io->pi_reg, 850 io->pi_width); 851 error = 0; 852 } else { 853 #ifdef COMPAT_FREEBSD4 854 if (cmd == PCIOCREAD_OLD) { 855 io_old->pi_data = -1; 856 error = 0; 857 } else 858 #endif 859 error = ENODEV; 860 } 861 break; 862 default: 863 error = EINVAL; 864 break; 865 } 866 break; 867 868 case PCIOCGETBAR: 869 bio = (struct pci_bar_io *)data; 870 871 /* 872 * Assume that the user-level bus number is 873 * in fact the physical PCI bus number. 874 */ 875 pcidev = pci_find_dbsf(bio->pbi_sel.pc_domain, 876 bio->pbi_sel.pc_bus, bio->pbi_sel.pc_dev, 877 bio->pbi_sel.pc_func); 878 if (pcidev == NULL) { 879 error = ENODEV; 880 break; 881 } 882 pm = pci_find_bar(pcidev, bio->pbi_reg); 883 if (pm == NULL) { 884 error = EINVAL; 885 break; 886 } 887 bio->pbi_base = pm->pm_value; 888 bio->pbi_length = (pci_addr_t)1 << pm->pm_size; 889 bio->pbi_enabled = pci_bar_enabled(pcidev, pm); 890 error = 0; 891 break; 892 case PCIOCATTACHED: 893 error = 0; 894 io = (struct pci_io *)data; 895 pcidev = pci_find_dbsf(io->pi_sel.pc_domain, io->pi_sel.pc_bus, 896 io->pi_sel.pc_dev, io->pi_sel.pc_func); 897 if (pcidev != NULL) 898 io->pi_data = device_is_attached(pcidev); 899 else 900 error = ENODEV; 901 break; 902 default: 903 error = ENOTTY; 904 break; 905 } 906 907 return (error); 908 } 909