pci_user.c revision 172999
1246853Sdes/*- 2246853Sdes * Copyright (c) 1997, Stefan Esser <se@freebsd.org> 3246853Sdes * All rights reserved. 4246853Sdes * 5246853Sdes * Redistribution and use in source and binary forms, with or without 6246853Sdes * modification, are permitted provided that the following conditions 7246853Sdes * are met: 8246853Sdes * 1. Redistributions of source code must retain the above copyright 9246853Sdes * notice unmodified, this list of conditions, and the following 10246853Sdes * disclaimer. 11246853Sdes * 2. Redistributions in binary form must reproduce the above copyright 12246853Sdes * notice, this list of conditions and the following disclaimer in the 13246853Sdes * documentation and/or other materials provided with the distribution. 14246853Sdes * 15246853Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16246853Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17246853Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18246853Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19246853Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20246853Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21246853Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22246853Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23246853Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24246853Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25246853Sdes */ 26246853Sdes 27246853Sdes#include <sys/cdefs.h> 28246853Sdes__FBSDID("$FreeBSD: head/sys/dev/pci/pci_user.c 172999 2007-10-26 05:02:47Z imp $"); 29246853Sdes 30246853Sdes#include "opt_bus.h" /* XXX trim includes */ 31246853Sdes#include "opt_compat.h" 32246853Sdes 33246853Sdes#include <sys/param.h> 34246853Sdes#include <sys/systm.h> 35246853Sdes#include <sys/malloc.h> 36246853Sdes#include <sys/module.h> 37246853Sdes#include <sys/linker.h> 38246853Sdes#include <sys/fcntl.h> 39246853Sdes#include <sys/conf.h> 40246853Sdes#include <sys/kernel.h> 41246853Sdes#include <sys/proc.h> 42246853Sdes#include <sys/queue.h> 43246853Sdes#include <sys/types.h> 44246853Sdes 45246853Sdes#include <vm/vm.h> 46246853Sdes#include <vm/pmap.h> 47246853Sdes#include <vm/vm_extern.h> 48246853Sdes 49246853Sdes#include <sys/bus.h> 50246853Sdes#include <machine/bus.h> 51246853Sdes#include <sys/rman.h> 52246853Sdes#include <machine/resource.h> 53246853Sdes 54246853Sdes#include <sys/pciio.h> 55246853Sdes#include <dev/pci/pcireg.h> 56246853Sdes#include <dev/pci/pcivar.h> 57246853Sdes 58246853Sdes#include "pcib_if.h" 59246853Sdes#include "pci_if.h" 60246853Sdes 61246853Sdes/* 62246853Sdes * This is the user interface to PCI configuration space. 63246853Sdes */ 64246853Sdes 65246853Sdesstatic d_open_t pci_open; 66246853Sdesstatic d_close_t pci_close; 67246853Sdesstatic int pci_conf_match(struct pci_match_conf *matches, int num_matches, 68246853Sdes struct pci_conf *match_buf); 69246853Sdesstatic d_ioctl_t pci_ioctl; 70246853Sdes 71246853Sdesstruct cdevsw pcicdev = { 72246853Sdes .d_version = D_VERSION, 73246853Sdes .d_flags = D_NEEDGIANT, 74246853Sdes .d_open = pci_open, 75246853Sdes .d_close = pci_close, 76246853Sdes .d_ioctl = pci_ioctl, 77246853Sdes .d_name = "pci", 78246853Sdes}; 79246853Sdes 80246853Sdesstatic int 81246853Sdespci_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 82246853Sdes{ 83246853Sdes int error; 84246853Sdes 85246853Sdes if (oflags & FWRITE) { 86246853Sdes error = securelevel_gt(td->td_ucred, 0); 87246853Sdes if (error) 88246853Sdes return (error); 89246853Sdes } 90246853Sdes 91246853Sdes return (0); 92246853Sdes} 93246853Sdes 94246853Sdesstatic int 95246853Sdespci_close(struct cdev *dev, int flag, int devtype, struct thread *td) 96246853Sdes{ 97246853Sdes return 0; 98246853Sdes} 99246853Sdes 100246853Sdes/* 101246853Sdes * Match a single pci_conf structure against an array of pci_match_conf 102246853Sdes * structures. The first argument, 'matches', is an array of num_matches 103246853Sdes * pci_match_conf structures. match_buf is a pointer to the pci_conf 104246853Sdes * structure that will be compared to every entry in the matches array. 105246853Sdes * This function returns 1 on failure, 0 on success. 106246853Sdes */ 107246853Sdesstatic int 108246853Sdespci_conf_match(struct pci_match_conf *matches, int num_matches, 109246853Sdes struct pci_conf *match_buf) 110246853Sdes{ 111246853Sdes int i; 112246853Sdes 113246853Sdes if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0)) 114246853Sdes return(1); 115246853Sdes 116246853Sdes for (i = 0; i < num_matches; i++) { 117246853Sdes /* 118246853Sdes * I'm not sure why someone would do this...but... 119246853Sdes */ 120246853Sdes if (matches[i].flags == PCI_GETCONF_NO_MATCH) 121246853Sdes continue; 122246853Sdes 123246853Sdes /* 124246853Sdes * Look at each of the match flags. If it's set, do the 125246853Sdes * comparison. If the comparison fails, we don't have a 126246853Sdes * match, go on to the next item if there is one. 127246853Sdes */ 128246853Sdes if (((matches[i].flags & PCI_GETCONF_MATCH_DOMAIN) != 0) 129246853Sdes && (match_buf->pc_sel.pc_domain != 130246853Sdes matches[i].pc_sel.pc_domain)) 131246853Sdes continue; 132246853Sdes 133246853Sdes if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0) 134246853Sdes && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus)) 135246853Sdes continue; 136246853Sdes 137246853Sdes if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0) 138246853Sdes && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev)) 139246853Sdes continue; 140246853Sdes 141246853Sdes if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0) 142246853Sdes && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func)) 143246853Sdes continue; 144246853Sdes 145246853Sdes if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0) 146246853Sdes && (match_buf->pc_vendor != matches[i].pc_vendor)) 147246853Sdes continue; 148246853Sdes 149246853Sdes if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0) 150246853Sdes && (match_buf->pc_device != matches[i].pc_device)) 151246853Sdes continue; 152246853Sdes 153246853Sdes if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0) 154246853Sdes && (match_buf->pc_class != matches[i].pc_class)) 155246853Sdes continue; 156246853Sdes 157246853Sdes if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0) 158246853Sdes && (match_buf->pd_unit != matches[i].pd_unit)) 159246853Sdes continue; 160246853Sdes 161246853Sdes if (((matches[i].flags & PCI_GETCONF_MATCH_NAME) != 0) 162246853Sdes && (strncmp(matches[i].pd_name, match_buf->pd_name, 163246853Sdes sizeof(match_buf->pd_name)) != 0)) 164246853Sdes continue; 165246853Sdes 166246853Sdes return(0); 167246853Sdes } 168246853Sdes 169246853Sdes return(1); 170246853Sdes} 171246853Sdes 172246853Sdes#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 173246853Sdes defined(COMPAT_FREEBSD6) 174246853Sdes#define PRE7_COMPAT 175246853Sdes 176246853Sdestypedef enum { 177246853Sdes PCI_GETCONF_NO_MATCH_OLD = 0x00, 178246853Sdes PCI_GETCONF_MATCH_BUS_OLD = 0x01, 179246853Sdes PCI_GETCONF_MATCH_DEV_OLD = 0x02, 180246853Sdes PCI_GETCONF_MATCH_FUNC_OLD = 0x04, 181246853Sdes PCI_GETCONF_MATCH_NAME_OLD = 0x08, 182246853Sdes PCI_GETCONF_MATCH_UNIT_OLD = 0x10, 183246853Sdes PCI_GETCONF_MATCH_VENDOR_OLD = 0x20, 184246853Sdes PCI_GETCONF_MATCH_DEVICE_OLD = 0x40, 185246853Sdes PCI_GETCONF_MATCH_CLASS_OLD = 0x80 186246853Sdes} pci_getconf_flags_old; 187246853Sdes 188246853Sdesstruct pcisel_old { 189246853Sdes u_int8_t pc_bus; /* bus number */ 190246853Sdes u_int8_t pc_dev; /* device on this bus */ 191246853Sdes u_int8_t pc_func; /* function on this device */ 192246853Sdes}; 193246853Sdes 194246853Sdesstruct pci_conf_old { 195246853Sdes struct pcisel_old pc_sel; /* bus+slot+function */ 196246853Sdes u_int8_t pc_hdr; /* PCI header type */ 197246853Sdes u_int16_t pc_subvendor; /* card vendor ID */ 198246853Sdes u_int16_t pc_subdevice; /* card device ID, assigned by 199246853Sdes card vendor */ 200246853Sdes u_int16_t pc_vendor; /* chip vendor ID */ 201246853Sdes u_int16_t pc_device; /* chip device ID, assigned by 202246853Sdes chip vendor */ 203246853Sdes u_int8_t pc_class; /* chip PCI class */ 204246853Sdes u_int8_t pc_subclass; /* chip PCI subclass */ 205246853Sdes u_int8_t pc_progif; /* chip PCI programming interface */ 206246853Sdes u_int8_t pc_revid; /* chip revision ID */ 207246853Sdes char pd_name[PCI_MAXNAMELEN + 1]; /* device name */ 208 u_long pd_unit; /* device unit number */ 209}; 210 211struct 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 flags; /* Matching expression */ 219}; 220 221struct 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#define PCIOCGETCONF_OLD _IOWR('p', 1, struct pci_conf_io) 229#define PCIOCREAD_OLD _IOWR('p', 2, struct pci_io_old) 230#define PCIOCWRITE_OLD _IOWR('p', 3, struct pci_io_old) 231 232static int pci_conf_match_old(struct pci_match_conf_old *matches, 233 int num_matches, struct pci_conf *match_buf); 234 235static int 236pci_conf_match_old(struct pci_match_conf_old *matches, int num_matches, 237 struct pci_conf *match_buf) 238{ 239 int i; 240 241 if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0)) 242 return(1); 243 244 for (i = 0; i < num_matches; i++) { 245 if (match_buf->pc_sel.pc_domain != 0) 246 continue; 247 248 /* 249 * I'm not sure why someone would do this...but... 250 */ 251 if (matches[i].flags == PCI_GETCONF_NO_MATCH_OLD) 252 continue; 253 254 /* 255 * Look at each of the match flags. If it's set, do the 256 * comparison. If the comparison fails, we don't have a 257 * match, go on to the next item if there is one. 258 */ 259 if (((matches[i].flags & PCI_GETCONF_MATCH_BUS_OLD) != 0) 260 && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus)) 261 continue; 262 263 if (((matches[i].flags & PCI_GETCONF_MATCH_DEV_OLD) != 0) 264 && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev)) 265 continue; 266 267 if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC_OLD) != 0) 268 && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func)) 269 continue; 270 271 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR_OLD) != 0) 272 && (match_buf->pc_vendor != matches[i].pc_vendor)) 273 continue; 274 275 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE_OLD) != 0) 276 && (match_buf->pc_device != matches[i].pc_device)) 277 continue; 278 279 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS_OLD) != 0) 280 && (match_buf->pc_class != matches[i].pc_class)) 281 continue; 282 283 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT_OLD) != 0) 284 && (match_buf->pd_unit != matches[i].pd_unit)) 285 continue; 286 287 if (((matches[i].flags & PCI_GETCONF_MATCH_NAME_OLD) != 0) 288 && (strncmp(matches[i].pd_name, match_buf->pd_name, 289 sizeof(match_buf->pd_name)) != 0)) 290 continue; 291 292 return(0); 293 } 294 295 return(1); 296} 297 298#endif 299 300static int 301pci_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 302{ 303 device_t pcidev, brdev; 304 void *confdata; 305 const char *name; 306 struct devlist *devlist_head; 307 struct pci_conf_io *cio; 308 struct pci_devinfo *dinfo; 309 struct pci_io *io; 310 struct pci_match_conf *pattern_buf; 311 size_t confsz, iolen, pbufsz; 312 int error, ionum, i, num_patterns; 313#ifdef PRE7_COMPAT 314 struct pci_conf_old conf_old; 315 struct pci_io iodata; 316 struct pci_io_old *io_old; 317 struct pci_match_conf_old *pattern_buf_old; 318 319 io_old = NULL; 320 pattern_buf_old = NULL; 321 322 if (!(flag & FWRITE) && 323 (cmd != PCIOCGETCONF && cmd != PCIOCGETCONF_OLD)) 324 return EPERM; 325#else 326 if (!(flag & FWRITE) && cmd != PCIOCGETCONF) 327 return EPERM; 328#endif 329 330 switch(cmd) { 331#ifdef PRE7_COMPAT 332 case PCIOCGETCONF_OLD: 333 /* FALLTHROUGH */ 334#endif 335 case PCIOCGETCONF: 336 cio = (struct pci_conf_io *)data; 337 338 pattern_buf = NULL; 339 num_patterns = 0; 340 dinfo = NULL; 341 342 cio->num_matches = 0; 343 344 /* 345 * If the user specified an offset into the device list, 346 * but the list has changed since they last called this 347 * ioctl, tell them that the list has changed. They will 348 * have to get the list from the beginning. 349 */ 350 if ((cio->offset != 0) 351 && (cio->generation != pci_generation)){ 352 cio->status = PCI_GETCONF_LIST_CHANGED; 353 error = 0; 354 break; 355 } 356 357 /* 358 * Check to see whether the user has asked for an offset 359 * past the end of our list. 360 */ 361 if (cio->offset >= pci_numdevs) { 362 cio->status = PCI_GETCONF_LAST_DEVICE; 363 error = 0; 364 break; 365 } 366 367 /* get the head of the device queue */ 368 devlist_head = &pci_devq; 369 370 /* 371 * Determine how much room we have for pci_conf structures. 372 * Round the user's buffer size down to the nearest 373 * multiple of sizeof(struct pci_conf) in case the user 374 * didn't specify a multiple of that size. 375 */ 376#ifdef PRE7_COMPAT 377 if (cmd == PCIOCGETCONF_OLD) 378 confsz = sizeof(struct pci_conf_old); 379 else 380#endif 381 confsz = sizeof(struct pci_conf); 382 iolen = min(cio->match_buf_len - (cio->match_buf_len % confsz), 383 pci_numdevs * confsz); 384 385 /* 386 * Since we know that iolen is a multiple of the size of 387 * the pciconf union, it's okay to do this. 388 */ 389 ionum = iolen / confsz; 390 391 /* 392 * If this test is true, the user wants the pci_conf 393 * structures returned to match the supplied entries. 394 */ 395 if ((cio->num_patterns > 0) && (cio->num_patterns < pci_numdevs) 396 && (cio->pat_buf_len > 0)) { 397 /* 398 * pat_buf_len needs to be: 399 * num_patterns * sizeof(struct pci_match_conf) 400 * While it is certainly possible the user just 401 * allocated a large buffer, but set the number of 402 * matches correctly, it is far more likely that 403 * their kernel doesn't match the userland utility 404 * they're using. It's also possible that the user 405 * forgot to initialize some variables. Yes, this 406 * may be overly picky, but I hazard to guess that 407 * it's far more likely to just catch folks that 408 * updated their kernel but not their userland. 409 */ 410#ifdef PRE7_COMPAT 411 if (cmd == PCIOCGETCONF_OLD) 412 pbufsz = sizeof(struct pci_match_conf_old); 413 else 414#endif 415 pbufsz = sizeof(struct pci_match_conf); 416 if (cio->num_patterns * pbufsz != cio->pat_buf_len) { 417 /* The user made a mistake, return an error. */ 418 cio->status = PCI_GETCONF_ERROR; 419 error = EINVAL; 420 break; 421 } 422 423 /* 424 * Allocate a buffer to hold the patterns. 425 */ 426#ifdef PRE7_COMPAT 427 if (cmd == PCIOCGETCONF_OLD) { 428 pattern_buf_old = malloc(cio->pat_buf_len, 429 M_TEMP, M_WAITOK); 430 error = copyin(cio->patterns, 431 pattern_buf_old, cio->pat_buf_len); 432 } else 433#endif 434 { 435 pattern_buf = malloc(cio->pat_buf_len, M_TEMP, 436 M_WAITOK); 437 error = copyin(cio->patterns, pattern_buf, 438 cio->pat_buf_len); 439 } 440 if (error != 0) { 441 error = EINVAL; 442 goto getconfexit; 443 } 444 num_patterns = cio->num_patterns; 445 } else if ((cio->num_patterns > 0) 446 || (cio->pat_buf_len > 0)) { 447 /* 448 * The user made a mistake, spit out an error. 449 */ 450 cio->status = PCI_GETCONF_ERROR; 451 error = EINVAL; 452 break; 453 } 454 455 /* 456 * Go through the list of devices and copy out the devices 457 * that match the user's criteria. 458 */ 459 for (cio->num_matches = 0, error = 0, i = 0, 460 dinfo = STAILQ_FIRST(devlist_head); 461 (dinfo != NULL) && (cio->num_matches < ionum) 462 && (error == 0) && (i < pci_numdevs) && (dinfo != NULL); 463 dinfo = STAILQ_NEXT(dinfo, pci_links), i++) { 464 465 if (i < cio->offset) 466 continue; 467 468 /* Populate pd_name and pd_unit */ 469 name = NULL; 470 if (dinfo->cfg.dev && dinfo->conf.pd_name[0] == '\0') 471 name = device_get_name(dinfo->cfg.dev); 472 if (name) { 473 strncpy(dinfo->conf.pd_name, name, 474 sizeof(dinfo->conf.pd_name)); 475 dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0; 476 dinfo->conf.pd_unit = 477 device_get_unit(dinfo->cfg.dev); 478 } 479 480#ifdef PRE7_COMPAT 481 if ((cmd == PCIOCGETCONF_OLD && 482 (pattern_buf_old == NULL || 483 pci_conf_match_old(pattern_buf_old, num_patterns, 484 &dinfo->conf) == 0)) || 485 (cmd == PCIOCGETCONF && 486 (pattern_buf == NULL || 487 pci_conf_match(pattern_buf, num_patterns, 488 &dinfo->conf) == 0))) { 489#else 490 if (pattern_buf == NULL || 491 pci_conf_match(pattern_buf, num_patterns, 492 &dinfo->conf) == 0) { 493#endif 494 /* 495 * If we've filled up the user's buffer, 496 * break out at this point. Since we've 497 * got a match here, we'll pick right back 498 * up at the matching entry. We can also 499 * tell the user that there are more matches 500 * left. 501 */ 502 if (cio->num_matches >= ionum) 503 break; 504 505#ifdef PRE7_COMPAT 506 if (cmd == PCIOCGETCONF_OLD) { 507 conf_old.pc_sel.pc_bus = 508 dinfo->conf.pc_sel.pc_bus; 509 conf_old.pc_sel.pc_dev = 510 dinfo->conf.pc_sel.pc_dev; 511 conf_old.pc_sel.pc_func = 512 dinfo->conf.pc_sel.pc_func; 513 conf_old.pc_hdr = dinfo->conf.pc_hdr; 514 conf_old.pc_subvendor = 515 dinfo->conf.pc_subvendor; 516 conf_old.pc_subdevice = 517 dinfo->conf.pc_subdevice; 518 conf_old.pc_vendor = 519 dinfo->conf.pc_vendor; 520 conf_old.pc_device = 521 dinfo->conf.pc_device; 522 conf_old.pc_class = 523 dinfo->conf.pc_class; 524 conf_old.pc_subclass = 525 dinfo->conf.pc_subclass; 526 conf_old.pc_progif = 527 dinfo->conf.pc_progif; 528 conf_old.pc_revid = 529 dinfo->conf.pc_revid; 530 conf_old.pd_name[0] = '\0'; 531 conf_old.pd_unit = 0; 532 if (name) { 533 strncpy(conf_old.pd_name, name, 534 sizeof(conf_old.pd_name)); 535 conf_old. 536 pd_name[PCI_MAXNAMELEN] = 0; 537 conf_old.pd_unit = 538 dinfo->conf.pd_unit; 539 } 540 confdata = &conf_old; 541 } else 542#endif 543 confdata = &dinfo->conf; 544 /* Only if we can copy it out do we count it. */ 545 if (!(error = copyout(confdata, 546 &cio->matches[cio->num_matches], confsz))) 547 cio->num_matches++; 548 } 549 } 550 551 /* 552 * Set the pointer into the list, so if the user is getting 553 * n records at a time, where n < pci_numdevs, 554 */ 555 cio->offset = i; 556 557 /* 558 * Set the generation, the user will need this if they make 559 * another ioctl call with offset != 0. 560 */ 561 cio->generation = pci_generation; 562 563 /* 564 * If this is the last device, inform the user so he won't 565 * bother asking for more devices. If dinfo isn't NULL, we 566 * know that there are more matches in the list because of 567 * the way the traversal is done. 568 */ 569 if (dinfo == NULL) 570 cio->status = PCI_GETCONF_LAST_DEVICE; 571 else 572 cio->status = PCI_GETCONF_MORE_DEVS; 573 574getconfexit: 575 if (pattern_buf != NULL) 576 free(pattern_buf, M_TEMP); 577#ifdef PRE7_COMPAT 578 if (pattern_buf_old != NULL) 579 free(pattern_buf_old, M_TEMP); 580#endif 581 582 break; 583 584#ifdef PRE7_COMPAT 585 case PCIOCREAD_OLD: 586 case PCIOCWRITE_OLD: 587 io_old = (struct pci_io_old *)data; 588 iodata.pi_sel.pc_domain = 0; 589 iodata.pi_sel.pc_bus = io_old->pi_sel.pc_bus; 590 iodata.pi_sel.pc_dev = io_old->pi_sel.pc_dev; 591 iodata.pi_sel.pc_func = io_old->pi_sel.pc_func; 592 iodata.pi_reg = io_old->pi_reg; 593 iodata.pi_width = io_old->pi_width; 594 iodata.pi_data = io_old->pi_data; 595 data = (caddr_t)&iodata; 596 /* FALLTHROUGH */ 597#endif 598 case PCIOCREAD: 599 case PCIOCWRITE: 600 io = (struct pci_io *)data; 601 switch(io->pi_width) { 602 case 4: 603 case 2: 604 case 1: 605 /* Make sure register is in bounds and aligned. */ 606 if (io->pi_reg < 0 || 607 io->pi_reg + io->pi_width > PCI_REGMAX + 1 || 608 io->pi_reg & (io->pi_width - 1)) { 609 error = EINVAL; 610 break; 611 } 612 /* 613 * Assume that the user-level bus number is 614 * in fact the physical PCI bus number. 615 * Look up the grandparent, i.e. the bridge device, 616 * so that we can issue configuration space cycles. 617 */ 618 pcidev = pci_find_dbsf(io->pi_sel.pc_domain, 619 io->pi_sel.pc_bus, io->pi_sel.pc_dev, 620 io->pi_sel.pc_func); 621 if (pcidev) { 622 brdev = device_get_parent( 623 device_get_parent(pcidev)); 624 625#ifdef PRE7_COMPAT 626 if (cmd == PCIOCWRITE || cmd == PCIOCWRITE_OLD) 627#else 628 if (cmd == PCIOCWRITE) 629#endif 630 PCIB_WRITE_CONFIG(brdev, 631 io->pi_sel.pc_bus, 632 io->pi_sel.pc_dev, 633 io->pi_sel.pc_func, 634 io->pi_reg, 635 io->pi_data, 636 io->pi_width); 637#ifdef PRE7_COMPAT 638 else if (cmd == PCIOCREAD_OLD) 639 io_old->pi_data = 640 PCIB_READ_CONFIG(brdev, 641 io->pi_sel.pc_bus, 642 io->pi_sel.pc_dev, 643 io->pi_sel.pc_func, 644 io->pi_reg, 645 io->pi_width); 646#endif 647 else 648 io->pi_data = 649 PCIB_READ_CONFIG(brdev, 650 io->pi_sel.pc_bus, 651 io->pi_sel.pc_dev, 652 io->pi_sel.pc_func, 653 io->pi_reg, 654 io->pi_width); 655 error = 0; 656 } else { 657#ifdef COMPAT_FREEBSD4 658 if (cmd == PCIOCREAD_OLD) { 659 io_old->pi_data = -1; 660 error = 0; 661 } else 662#endif 663 error = ENODEV; 664 } 665 break; 666 default: 667 error = EINVAL; 668 break; 669 } 670 break; 671 672 default: 673 error = ENOTTY; 674 break; 675 } 676 677 return (error); 678} 679