1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 */ 24 /* 25 * Copyright 2012 Garrett D'Amore <garrett@damore.org>. All rights reserved. 26 * Copyright 2019 Joyent, Inc. 27 * Copyright 2022 Oxide Computer Company 28 */ 29 30 /* 31 * Common x86 and SPARC PCI-E to PCI bus bridge nexus driver 32 * 33 * Background 34 * ---------- 35 * 36 * The PCI Express (PCIe) specification defines that all of the PCIe devices in 37 * the system are connected together in a series of different fabrics. A way to 38 * think of these fabrics is that they are small networks where there are links 39 * between different devices and switches that allow fan out or fan in of the 40 * fabric. The entry point to that fabric is called a root complex and the 41 * fabric terminates at a what is called an endpoint, which is really just PCIe 42 * terminology for the common cards that are inserted into the system (HBAs, 43 * NICs, USB, NVMe, etc.). 44 * 45 * The PCIe specification states that every link on the system has a virtual 46 * PCI-to-PCI bridge. This allows PCIe devices to still be configured the same 47 * way traditional PCI devices are to the operating system and allows them to 48 * have a traditional PCI bus, device, and function associated with them, even 49 * though there is no actual shared bus. In addition, bridges are also used to 50 * connect traditional PCI and PCI-X devices into them. 51 * 52 * The PCIe specification refers to upstream and downstream ports. Upstream 53 * ports are considered closer the root complex and downstream ports are closer 54 * to the endpoint. We can divide the devices that the bridge driver attaches to 55 * into two groups. Those that are considered upstream ports, these include root 56 * complexes and parts of PCIe switches. And downstream ports, which are the 57 * other half of PCIe switches and endpoints (which this driver does not attach 58 * to, normal hardware-specific or class-specific drivers attach to those). 59 * 60 * Interrupt Management 61 * -------------------- 62 * 63 * Upstream ports of bridges have additional things that we care about. 64 * Specifically they're the means through which we find out about: 65 * 66 * - Advanced Error Reporting (AERs) 67 * - Hotplug events 68 * - Link Bandwidth Events 69 * - Power Management Events (PME) 70 * 71 * Each of these features is an optional feature (though ones we hope are 72 * implemented). The features above are grouped into two different buckets based 73 * on which PCI capability they appear in. AER management is done through a PCI 74 * Express extended configuration header (it lives in extended PCI configuration 75 * space) called the 'Advanced Error Reporting Extended Capability'. The other 76 * events are all managed as part of the 'PCI Express Capability Structure'. 77 * This structure is found in traditional PCI configuration space. 78 * 79 * The way that the interrupts are programmed for these types of events differs 80 * a bit from the way one might expect a normal device to operate. For most 81 * devices, one allocates a number of interrupts based on a combination of what 82 * the device supports, what the OS supports per device, and the number the 83 * driver needs. Then the driver programs the device in a device-specific manner 84 * to indicate which events should trigger a specific interrupt vector. 85 * 86 * However, for both the AER and PCI capabilities, the driver has to do 87 * something different. The driver first allocates interrupts by programming the 88 * MSI or MSI-X table and then asks the device which interrupts have been 89 * assigned to these purposes. Because these events are only supported in 90 * 'upstream' devices, this does not interfere with the traditional management 91 * of MSI and MSI-X interrupts. At this time, the pcieb driver only supports the 92 * use of MSI interrupts. 93 * 94 * Once the interrupts have been allocated, we read back which vectors have been 95 * nominated by the device to cover the corresponding capability. The interrupt 96 * is allocated on a per-capability basis. Therefore, one interrupt would cover 97 * AERs, while another interrupt would cover the rest of the desired functions. 98 * 99 * To track which interrupts cover which behaviors, each driver state 100 * (pcieb_devstate_t) has a member called 'pcieb_isr_tab'. Each index represents 101 * an interrupt vector and there are a series of flags that represent the 102 * different possible interrupt sources: PCIEB_INTR_SRC_HP (hotplug), 103 * PCEIB_INTR_SRC_PME (power management event), PCIEB_INTR_SRC_AER (error 104 * reporting), PCIEB_INTR_SRC_LBW (link bandwidth). 105 * 106 * Because the hotplug, link bandwidth, and power management events all share 107 * the same vector, if an interrupt comes in, we must check all of the enabled 108 * sources that might generate this interrupt. It is highly likely that more 109 * than one will fire at the same time, for example, a hotplug event that fires 110 * because a device has been inserted or removed, will likely trigger a link 111 * bandwidth event. 112 * 113 * The pcieb driver itself does not actually have much logic to deal with and 114 * clear the interrupts in question. It generally speaking will vector most 115 * events back to the more general pcie driver or, in the case of AERs, initiate 116 * a scan of the fabric itself (also part of the pcie driver). 117 * 118 * Link Management 119 * --------------- 120 * 121 * The pcieb driver is used to take care of two different aspects of link 122 * management. The first of these, as described briefly above, is to monitor for 123 * changes to the negotiated link bandwidth. These events are managed by 124 * enabling support for the interrupts in the PCI Express Capability Structure. 125 * This is all taken care of by the pcie driver through functions like 126 * pcie_link_bw_enable(). 127 * 128 * The second aspect of link management the pcieb driver enables is the ability 129 * to retrain the link and optionally limit the speed. This is enabled through a 130 * series of private ioctls that are driven through a private userland utility, 131 * /usr/lib/pci/pcieb. Eventually, this should be more fleshed out and a more 132 * uniform interface based around the devctls that can be leveraged across 133 * different classes of devices should be used. 134 * 135 * Under the hood this basically leverages the ability of the upstream port to 136 * retrain a link by writing a bit to the PCIe link control register. See 137 * pcieb_ioctl_retrain(). From there, if the driver ever receives a request to 138 * change the maximum speed, that is updated in the card; however, it does not 139 * immediately retrain the link. A separate ioctl request is required to do so. 140 * Once the speed has been changed, regardless of whether or not it has been 141 * retrained, that fact will always be noted. 142 */ 143 144 #include <sys/sysmacros.h> 145 #include <sys/conf.h> 146 #include <sys/kmem.h> 147 #include <sys/debug.h> 148 #include <sys/modctl.h> 149 #include <sys/autoconf.h> 150 #include <sys/ddi_impldefs.h> 151 #include <sys/pci.h> 152 #include <sys/ddi.h> 153 #include <sys/sunddi.h> 154 #include <sys/sunndi.h> 155 #include <sys/fm/util.h> 156 #include <sys/pci_cap.h> 157 #include <sys/pci_impl.h> 158 #include <sys/pcie_impl.h> 159 #include <sys/open.h> 160 #include <sys/stat.h> 161 #include <sys/file.h> 162 #include <sys/promif.h> /* prom_printf */ 163 #include <sys/disp.h> 164 #include <sys/pcie_pwr.h> 165 #include <sys/hotplug/pci/pcie_hp.h> 166 #include "pcieb.h" 167 #include "pcieb_ioctl.h" 168 #ifdef PX_PLX 169 #include <io/pciex/pcieb_plx.h> 170 #endif /* PX_PLX */ 171 172 /*LINTLIBRARY*/ 173 174 /* panic flag */ 175 int pcieb_die = PF_ERR_FATAL_FLAGS; 176 int pcieb_disable_41210_wkarnd = 0; 177 178 /* flag to turn on MSI support */ 179 int pcieb_enable_msi = 1; 180 181 #if defined(DEBUG) 182 uint_t pcieb_dbg_print = 0; 183 184 static char *pcieb_debug_sym [] = { /* same sequence as pcieb_debug_bit */ 185 /* 0 */ "attach", 186 /* 1 */ "pwr", 187 /* 2 */ "intr" 188 }; 189 #endif /* DEBUG */ 190 191 static int pcieb_bus_map(dev_info_t *, dev_info_t *, ddi_map_req_t *, off_t, 192 off_t, caddr_t *); 193 static int pcieb_ctlops(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, 194 void *); 195 static int pcieb_fm_init(pcieb_devstate_t *pcieb_p); 196 static void pcieb_fm_fini(pcieb_devstate_t *pcieb_p); 197 static int pcieb_fm_init_child(dev_info_t *dip, dev_info_t *cdip, int cap, 198 ddi_iblock_cookie_t *ibc_p); 199 static int pcieb_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, 200 ddi_dma_attr_t *attr_p, int (*waitfp)(caddr_t), caddr_t arg, 201 ddi_dma_handle_t *handlep); 202 static int pcieb_dma_mctl(dev_info_t *dip, dev_info_t *rdip, 203 ddi_dma_handle_t handle, enum ddi_dma_ctlops cmd, off_t *offp, 204 size_t *lenp, caddr_t *objp, uint_t cache_flags); 205 static int pcieb_intr_ops(dev_info_t *dip, dev_info_t *rdip, 206 ddi_intr_op_t intr_op, ddi_intr_handle_impl_t *hdlp, void *result); 207 208 static struct bus_ops pcieb_bus_ops = { 209 BUSO_REV, 210 pcieb_bus_map, 211 0, 212 0, 213 0, 214 i_ddi_map_fault, 215 0, 216 pcieb_dma_allochdl, 217 ddi_dma_freehdl, 218 ddi_dma_bindhdl, 219 ddi_dma_unbindhdl, 220 ddi_dma_flush, 221 ddi_dma_win, 222 pcieb_dma_mctl, 223 pcieb_ctlops, 224 ddi_bus_prop_op, 225 ndi_busop_get_eventcookie, /* (*bus_get_eventcookie)(); */ 226 ndi_busop_add_eventcall, /* (*bus_add_eventcall)(); */ 227 ndi_busop_remove_eventcall, /* (*bus_remove_eventcall)(); */ 228 ndi_post_event, /* (*bus_post_event)(); */ 229 NULL, /* (*bus_intr_ctl)(); */ 230 NULL, /* (*bus_config)(); */ 231 NULL, /* (*bus_unconfig)(); */ 232 pcieb_fm_init_child, /* (*bus_fm_init)(); */ 233 NULL, /* (*bus_fm_fini)(); */ 234 i_ndi_busop_access_enter, /* (*bus_fm_access_enter)(); */ 235 i_ndi_busop_access_exit, /* (*bus_fm_access_exit)(); */ 236 pcie_bus_power, /* (*bus_power)(); */ 237 pcieb_intr_ops, /* (*bus_intr_op)(); */ 238 pcie_hp_common_ops /* (*bus_hp_op)(); */ 239 }; 240 241 static int pcieb_open(dev_t *, int, int, cred_t *); 242 static int pcieb_close(dev_t, int, int, cred_t *); 243 static int pcieb_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 244 static int pcieb_info(dev_info_t *, ddi_info_cmd_t, void *, void **); 245 static uint_t pcieb_intr_handler(caddr_t arg1, caddr_t arg2); 246 247 /* PM related functions */ 248 static int pcieb_pwr_setup(dev_info_t *dip); 249 static int pcieb_pwr_init_and_raise(dev_info_t *dip, pcie_pwr_t *pwr_p); 250 static void pcieb_pwr_teardown(dev_info_t *dip); 251 static int pcieb_pwr_disable(dev_info_t *dip); 252 253 /* Hotplug related functions */ 254 static void pcieb_id_props(pcieb_devstate_t *pcieb); 255 256 /* 257 * soft state pointer 258 */ 259 void *pcieb_state; 260 261 static struct cb_ops pcieb_cb_ops = { 262 pcieb_open, /* open */ 263 pcieb_close, /* close */ 264 nodev, /* strategy */ 265 nodev, /* print */ 266 nodev, /* dump */ 267 nodev, /* read */ 268 nodev, /* write */ 269 pcieb_ioctl, /* ioctl */ 270 nodev, /* devmap */ 271 nodev, /* mmap */ 272 nodev, /* segmap */ 273 nochpoll, /* poll */ 274 pcie_prop_op, /* cb_prop_op */ 275 NULL, /* streamtab */ 276 D_NEW | D_MP | D_HOTPLUG, /* Driver compatibility flag */ 277 CB_REV, /* rev */ 278 nodev, /* int (*cb_aread)() */ 279 nodev /* int (*cb_awrite)() */ 280 }; 281 282 static int pcieb_probe(dev_info_t *); 283 static int pcieb_attach(dev_info_t *devi, ddi_attach_cmd_t cmd); 284 static int pcieb_detach(dev_info_t *devi, ddi_detach_cmd_t cmd); 285 286 static struct dev_ops pcieb_ops = { 287 DEVO_REV, /* devo_rev */ 288 0, /* refcnt */ 289 pcieb_info, /* info */ 290 nulldev, /* identify */ 291 pcieb_probe, /* probe */ 292 pcieb_attach, /* attach */ 293 pcieb_detach, /* detach */ 294 nulldev, /* reset */ 295 &pcieb_cb_ops, /* driver operations */ 296 &pcieb_bus_ops, /* bus operations */ 297 pcie_power, /* power */ 298 ddi_quiesce_not_needed, /* quiesce */ 299 }; 300 301 /* 302 * Module linkage information for the kernel. 303 */ 304 305 static struct modldrv modldrv = { 306 &mod_driverops, /* Type of module */ 307 "PCIe bridge/switch driver", 308 &pcieb_ops, /* driver ops */ 309 }; 310 311 static struct modlinkage modlinkage = { 312 MODREV_1, 313 (void *)&modldrv, 314 NULL 315 }; 316 317 /* 318 * forward function declarations: 319 */ 320 static void pcieb_uninitchild(dev_info_t *); 321 static int pcieb_initchild(dev_info_t *child); 322 static void pcieb_create_ranges_prop(dev_info_t *, ddi_acc_handle_t); 323 static boolean_t pcieb_is_pcie_device_type(dev_info_t *dip); 324 325 /* interrupt related declarations */ 326 static int pcieb_msi_supported(dev_info_t *); 327 static int pcieb_intr_attach(pcieb_devstate_t *pcieb); 328 static int pcieb_intr_init(pcieb_devstate_t *pcieb_p, int intr_type); 329 static void pcieb_intr_fini(pcieb_devstate_t *pcieb_p); 330 331 int 332 _init(void) 333 { 334 int e; 335 336 if ((e = ddi_soft_state_init(&pcieb_state, sizeof (pcieb_devstate_t), 337 1)) == 0 && (e = mod_install(&modlinkage)) != 0) 338 ddi_soft_state_fini(&pcieb_state); 339 return (e); 340 } 341 342 int 343 _fini(void) 344 { 345 int e; 346 347 if ((e = mod_remove(&modlinkage)) == 0) { 348 ddi_soft_state_fini(&pcieb_state); 349 } 350 return (e); 351 } 352 353 int 354 _info(struct modinfo *modinfop) 355 { 356 return (mod_info(&modlinkage, modinfop)); 357 } 358 359 /* ARGSUSED */ 360 static int 361 pcieb_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 362 { 363 minor_t minor = getminor((dev_t)arg); 364 int instance = PCI_MINOR_NUM_TO_INSTANCE(minor); 365 pcieb_devstate_t *pcieb = ddi_get_soft_state(pcieb_state, instance); 366 int ret = DDI_SUCCESS; 367 368 switch (infocmd) { 369 case DDI_INFO_DEVT2INSTANCE: 370 *result = (void *)(intptr_t)instance; 371 break; 372 case DDI_INFO_DEVT2DEVINFO: 373 if (pcieb == NULL) { 374 ret = DDI_FAILURE; 375 break; 376 } 377 378 *result = (void *)pcieb->pcieb_dip; 379 break; 380 default: 381 ret = DDI_FAILURE; 382 break; 383 } 384 385 return (ret); 386 } 387 388 389 /*ARGSUSED*/ 390 static int 391 pcieb_probe(dev_info_t *devi) 392 { 393 return (DDI_PROBE_SUCCESS); 394 } 395 396 /* 397 * This is a workaround for an undocumented HW erratum with the 398 * multi-function, F0 and F2, Intel 41210 PCIe-to-PCI bridge. When 399 * Fn (cdip) attaches, this workaround is called to initialize Fn's 400 * sibling (sdip) with MPS/MRRS if it isn't already configured. 401 * Doing so prevents a malformed TLP panic. 402 */ 403 static void 404 pcieb_41210_mps_wkrnd(dev_info_t *cdip) 405 { 406 dev_info_t *sdip; 407 ddi_acc_handle_t cfg_hdl; 408 uint16_t cdip_dev_ctrl, cdip_mrrs_mps; 409 pcie_bus_t *cdip_bus_p = PCIE_DIP2BUS(cdip); 410 411 /* Get cdip's MPS/MRRS already setup by pcie_initchild_mps() */ 412 ASSERT(cdip_bus_p); 413 cdip_dev_ctrl = PCIE_CAP_GET(16, cdip_bus_p, PCIE_DEVCTL); 414 cdip_mrrs_mps = cdip_dev_ctrl & 415 (PCIE_DEVCTL_MAX_READ_REQ_MASK | PCIE_DEVCTL_MAX_PAYLOAD_MASK); 416 417 /* Locate sdip and set its MPS/MRRS when applicable */ 418 for (sdip = ddi_get_child(ddi_get_parent(cdip)); sdip; 419 sdip = ddi_get_next_sibling(sdip)) { 420 uint16_t sdip_dev_ctrl, sdip_mrrs_mps, cap_ptr; 421 uint32_t bus_dev_ven_id; 422 423 if (sdip == cdip || pci_config_setup(sdip, &cfg_hdl) 424 != DDI_SUCCESS) 425 continue; 426 427 /* must be an Intel 41210 bridge */ 428 bus_dev_ven_id = pci_config_get32(cfg_hdl, PCI_CONF_VENID); 429 if (!PCIEB_IS_41210_BRIDGE(bus_dev_ven_id)) { 430 pci_config_teardown(&cfg_hdl); 431 continue; 432 } 433 434 if (PCI_CAP_LOCATE(cfg_hdl, PCI_CAP_ID_PCI_E, &cap_ptr) 435 != DDI_SUCCESS) { 436 pci_config_teardown(&cfg_hdl); 437 continue; 438 } 439 440 /* get sdip's MPS/MRRS to compare to cdip's */ 441 sdip_dev_ctrl = PCI_CAP_GET16(cfg_hdl, 0, cap_ptr, 442 PCIE_DEVCTL); 443 sdip_mrrs_mps = sdip_dev_ctrl & 444 (PCIE_DEVCTL_MAX_READ_REQ_MASK | 445 PCIE_DEVCTL_MAX_PAYLOAD_MASK); 446 447 /* if sdip already attached then its MPS/MRRS is configured */ 448 if (i_ddi_devi_attached(sdip)) { 449 ASSERT(sdip_mrrs_mps == cdip_mrrs_mps); 450 pci_config_teardown(&cfg_hdl); 451 continue; 452 } 453 454 /* otherwise, update sdip's MPS/MRRS if different from cdip's */ 455 if (sdip_mrrs_mps != cdip_mrrs_mps) { 456 sdip_dev_ctrl = (sdip_dev_ctrl & 457 ~(PCIE_DEVCTL_MAX_READ_REQ_MASK | 458 PCIE_DEVCTL_MAX_PAYLOAD_MASK)) | cdip_mrrs_mps; 459 460 (void) PCI_CAP_PUT16(cfg_hdl, 0, cap_ptr, PCIE_DEVCTL, 461 sdip_dev_ctrl); 462 } 463 464 /* 465 * note: sdip's bus_mps will be updated by 466 * pcie_initchild_mps() 467 */ 468 469 pci_config_teardown(&cfg_hdl); 470 471 break; 472 } 473 } 474 475 static int 476 pcieb_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 477 { 478 int instance; 479 char device_type[8]; 480 pcieb_devstate_t *pcieb; 481 pcie_bus_t *bus_p = PCIE_DIP2UPBUS(devi); 482 ddi_acc_handle_t config_handle = bus_p->bus_cfg_hdl; 483 484 switch (cmd) { 485 case DDI_RESUME: 486 (void) pcie_pwr_resume(devi); 487 return (DDI_SUCCESS); 488 489 default: 490 return (DDI_FAILURE); 491 492 case DDI_ATTACH: 493 break; 494 } 495 496 if (!(PCIE_IS_BDG(bus_p))) { 497 PCIEB_DEBUG(DBG_ATTACH, devi, "This is not a switch or" 498 " bridge\n"); 499 return (DDI_FAILURE); 500 } 501 502 /* 503 * If PCIE_LINKCTL_LINK_DISABLE bit in the PCIe Config 504 * Space (PCIe Capability Link Control Register) is set, 505 * then do not bind the driver. 506 */ 507 if (PCIE_CAP_GET(16, bus_p, PCIE_LINKCTL) & PCIE_LINKCTL_LINK_DISABLE) 508 return (DDI_FAILURE); 509 510 /* 511 * Allocate and get soft state structure. 512 */ 513 instance = ddi_get_instance(devi); 514 if (ddi_soft_state_zalloc(pcieb_state, instance) != DDI_SUCCESS) 515 return (DDI_FAILURE); 516 pcieb = ddi_get_soft_state(pcieb_state, instance); 517 pcieb->pcieb_dip = devi; 518 519 if ((pcieb_fm_init(pcieb)) != DDI_SUCCESS) { 520 PCIEB_DEBUG(DBG_ATTACH, devi, "Failed in pcieb_fm_init\n"); 521 goto fail; 522 } 523 pcieb->pcieb_init_flags |= PCIEB_INIT_FM; 524 525 mutex_init(&pcieb->pcieb_mutex, NULL, MUTEX_DRIVER, NULL); 526 mutex_init(&pcieb->pcieb_err_mutex, NULL, MUTEX_DRIVER, 527 (void *)pcieb->pcieb_fm_ibc); 528 mutex_init(&pcieb->pcieb_peek_poke_mutex, NULL, MUTEX_DRIVER, 529 (void *)pcieb->pcieb_fm_ibc); 530 531 /* create special properties for device identification */ 532 pcieb_id_props(pcieb); 533 534 /* 535 * Power management setup. This also makes sure that switch/bridge 536 * is at D0 during attach. 537 */ 538 if (pwr_common_setup(devi) != DDI_SUCCESS) { 539 PCIEB_DEBUG(DBG_PWR, devi, "pwr_common_setup failed\n"); 540 goto fail; 541 } 542 543 if (pcieb_pwr_setup(devi) != DDI_SUCCESS) { 544 PCIEB_DEBUG(DBG_PWR, devi, "pxb_pwr_setup failed \n"); 545 goto fail; 546 } 547 548 /* 549 * Make sure the "device_type" property exists. 550 */ 551 if (pcieb_is_pcie_device_type(devi)) 552 (void) strcpy(device_type, "pciex"); 553 else 554 (void) strcpy(device_type, "pci"); 555 556 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi, 557 "device_type", device_type); 558 559 /* 560 * Check whether the "ranges" property is present. 561 * Otherwise create the ranges property by reading 562 * the configuration registers 563 */ 564 if (ddi_prop_exists(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 565 "ranges") == 0) { 566 pcieb_create_ranges_prop(devi, config_handle); 567 } 568 569 if (PCIE_IS_PCI_BDG(bus_p)) 570 pcieb_set_pci_perf_parameters(devi, config_handle); 571 572 #ifdef PX_PLX 573 pcieb_attach_plx_workarounds(pcieb); 574 #endif /* PX_PLX */ 575 576 if (pcie_init(devi, NULL) != DDI_SUCCESS) 577 goto fail; 578 579 /* Intel PCIe-to-PCI 41210 bridge workaround -- if applicable */ 580 if (pcieb_disable_41210_wkarnd == 0 && 581 PCIEB_IS_41210_BRIDGE(bus_p->bus_dev_ven_id)) 582 pcieb_41210_mps_wkrnd(devi); 583 584 /* 585 * Initialize interrupt handlers. Ignore return value. 586 */ 587 (void) pcieb_intr_attach(pcieb); 588 589 (void) pcie_hpintr_enable(devi); 590 591 (void) pcie_link_bw_enable(devi); 592 593 /* Do any platform specific workarounds needed at this time */ 594 pcieb_plat_attach_workaround(devi); 595 596 /* 597 * If this is a root port, we need to go through and at this point in 598 * time set up and initialize all fabric-wide settings such as the max 599 * packet size, tagging, etc. Since this will involve scanning the 600 * fabric, all error enabling and sw workarounds should be in place 601 * before doing this. For hotplug-capable bridges, this will happen 602 * again when a hotplug event occurs. See the pcie theory statement in 603 * uts/common/io/pciex/pcie.c for more information. 604 */ 605 if (PCIE_IS_RP(bus_p)) 606 pcie_fabric_setup(devi); 607 608 ddi_report_dev(devi); 609 return (DDI_SUCCESS); 610 611 fail: 612 (void) pcieb_detach(devi, DDI_DETACH); 613 return (DDI_FAILURE); 614 } 615 616 static int 617 pcieb_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 618 { 619 pcieb_devstate_t *pcieb; 620 int error = DDI_SUCCESS; 621 622 switch (cmd) { 623 case DDI_SUSPEND: 624 error = pcie_pwr_suspend(devi); 625 return (error); 626 627 case DDI_DETACH: 628 break; 629 630 default: 631 return (DDI_FAILURE); 632 } 633 634 pcieb = ddi_get_soft_state(pcieb_state, ddi_get_instance(devi)); 635 636 /* disable hotplug interrupt */ 637 (void) pcie_hpintr_disable(devi); 638 639 /* remove interrupt handlers */ 640 pcieb_intr_fini(pcieb); 641 642 /* uninitialize inband PCI-E HPC if present */ 643 (void) pcie_uninit(devi); 644 645 (void) ddi_prop_remove(DDI_DEV_T_NONE, devi, "device_type"); 646 647 (void) ndi_prop_remove(DDI_DEV_T_NONE, pcieb->pcieb_dip, 648 "pcie_ce_mask"); 649 650 if (pcieb->pcieb_init_flags & PCIEB_INIT_FM) 651 pcieb_fm_fini(pcieb); 652 653 pcieb_pwr_teardown(devi); 654 pwr_common_teardown(devi); 655 656 mutex_destroy(&pcieb->pcieb_peek_poke_mutex); 657 mutex_destroy(&pcieb->pcieb_err_mutex); 658 mutex_destroy(&pcieb->pcieb_mutex); 659 660 /* 661 * And finally free the per-pci soft state. 662 */ 663 ddi_soft_state_free(pcieb_state, ddi_get_instance(devi)); 664 665 return (DDI_SUCCESS); 666 } 667 668 static int 669 pcieb_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp, 670 off_t offset, off_t len, caddr_t *vaddrp) 671 { 672 dev_info_t *pdip; 673 674 if (PCIE_IS_RP(PCIE_DIP2BUS(dip)) && mp->map_handlep != NULL) { 675 ddi_acc_impl_t *hdlp = 676 (ddi_acc_impl_t *)(mp->map_handlep)->ah_platform_private; 677 678 pcieb_set_prot_scan(dip, hdlp); 679 } 680 pdip = (dev_info_t *)DEVI(dip)->devi_parent; 681 return ((DEVI(pdip)->devi_ops->devo_bus_ops->bus_map)(pdip, rdip, mp, 682 offset, len, vaddrp)); 683 } 684 685 static int 686 pcieb_ctlops(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t ctlop, 687 void *arg, void *result) 688 { 689 pci_regspec_t *drv_regp; 690 int reglen; 691 int rn; 692 int totreg; 693 pcieb_devstate_t *pcieb = ddi_get_soft_state(pcieb_state, 694 ddi_get_instance(dip)); 695 struct detachspec *ds; 696 struct attachspec *as; 697 698 switch (ctlop) { 699 case DDI_CTLOPS_REPORTDEV: 700 if (rdip == (dev_info_t *)0) 701 return (DDI_FAILURE); 702 703 if (ddi_get_parent(rdip) == dip) { 704 cmn_err(CE_CONT, "?PCIE-device: %s@%s, %s%d\n", 705 ddi_node_name(rdip), ddi_get_name_addr(rdip), 706 ddi_driver_name(rdip), ddi_get_instance(rdip)); 707 } 708 709 /* Pass it up for fabric sync */ 710 (void) ddi_ctlops(dip, rdip, ctlop, arg, result); 711 return (DDI_SUCCESS); 712 713 case DDI_CTLOPS_INITCHILD: 714 return (pcieb_initchild((dev_info_t *)arg)); 715 716 case DDI_CTLOPS_UNINITCHILD: 717 pcieb_uninitchild((dev_info_t *)arg); 718 return (DDI_SUCCESS); 719 720 case DDI_CTLOPS_SIDDEV: 721 return (DDI_SUCCESS); 722 723 case DDI_CTLOPS_REGSIZE: 724 case DDI_CTLOPS_NREGS: 725 if (rdip == (dev_info_t *)0) 726 return (DDI_FAILURE); 727 break; 728 729 case DDI_CTLOPS_PEEK: 730 case DDI_CTLOPS_POKE: 731 return (pcieb_plat_peekpoke(dip, rdip, ctlop, arg, result)); 732 case DDI_CTLOPS_ATTACH: 733 if (!pcie_is_child(dip, rdip)) 734 return (DDI_SUCCESS); 735 736 as = (struct attachspec *)arg; 737 switch (as->when) { 738 case DDI_PRE: 739 if (as->cmd == DDI_RESUME) { 740 pcie_clear_errors(rdip); 741 if (pcieb_plat_ctlops(rdip, ctlop, arg) != 742 DDI_SUCCESS) 743 return (DDI_FAILURE); 744 } 745 746 if (as->cmd == DDI_ATTACH) 747 return (pcie_pm_hold(dip)); 748 749 return (DDI_SUCCESS); 750 751 case DDI_POST: 752 if (as->cmd == DDI_ATTACH && 753 as->result != DDI_SUCCESS) { 754 /* 755 * Attach failed for the child device. The child 756 * driver may have made PM calls before the 757 * attach failed. pcie_pm_remove_child() should 758 * cleanup PM state and holds (if any) 759 * associated with the child device. 760 */ 761 return (pcie_pm_remove_child(dip, rdip)); 762 } 763 764 if (as->result == DDI_SUCCESS) { 765 pf_init(rdip, (void *)pcieb->pcieb_fm_ibc, 766 as->cmd); 767 768 (void) pcieb_plat_ctlops(rdip, ctlop, arg); 769 } 770 771 /* 772 * For empty hotplug-capable slots, we should explicitly 773 * disable the errors, so that we won't panic upon 774 * unsupported hotplug messages. 775 */ 776 if ((!ddi_prop_exists(DDI_DEV_T_ANY, rdip, 777 DDI_PROP_DONTPASS, "hotplug-capable")) || 778 ddi_get_child(rdip)) { 779 (void) pcie_postattach_child(rdip); 780 return (DDI_SUCCESS); 781 } 782 783 pcie_disable_errors(rdip); 784 785 return (DDI_SUCCESS); 786 default: 787 break; 788 } 789 return (DDI_SUCCESS); 790 791 case DDI_CTLOPS_DETACH: 792 if (!pcie_is_child(dip, rdip)) 793 return (DDI_SUCCESS); 794 795 ds = (struct detachspec *)arg; 796 switch (ds->when) { 797 case DDI_PRE: 798 pf_fini(rdip, ds->cmd); 799 return (DDI_SUCCESS); 800 801 case DDI_POST: 802 if (pcieb_plat_ctlops(rdip, ctlop, arg) != DDI_SUCCESS) 803 return (DDI_FAILURE); 804 if (ds->cmd == DDI_DETACH && 805 ds->result == DDI_SUCCESS) { 806 return (pcie_pm_remove_child(dip, rdip)); 807 } 808 return (DDI_SUCCESS); 809 default: 810 break; 811 } 812 return (DDI_SUCCESS); 813 default: 814 return (ddi_ctlops(dip, rdip, ctlop, arg, result)); 815 } 816 817 *(int *)result = 0; 818 if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, 819 DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP, "reg", (caddr_t)&drv_regp, 820 ®len) != DDI_SUCCESS) 821 return (DDI_FAILURE); 822 823 totreg = reglen / sizeof (pci_regspec_t); 824 if (ctlop == DDI_CTLOPS_NREGS) 825 *(int *)result = totreg; 826 else if (ctlop == DDI_CTLOPS_REGSIZE) { 827 rn = *(int *)arg; 828 if (rn >= totreg) { 829 kmem_free(drv_regp, reglen); 830 return (DDI_FAILURE); 831 } 832 833 *(off_t *)result = drv_regp[rn].pci_size_low | 834 ((uint64_t)drv_regp[rn].pci_size_hi << 32); 835 } 836 837 kmem_free(drv_regp, reglen); 838 return (DDI_SUCCESS); 839 } 840 841 /* 842 * name_child 843 * 844 * This function is called from init_child to name a node. It is 845 * also passed as a callback for node merging functions. 846 * 847 * return value: DDI_SUCCESS, DDI_FAILURE 848 */ 849 static int 850 pcieb_name_child(dev_info_t *child, char *name, int namelen) 851 { 852 pci_regspec_t *pci_rp; 853 uint_t device, func; 854 char **unit_addr; 855 uint_t n; 856 857 /* 858 * For .conf nodes, use unit-address property as name 859 */ 860 if (ndi_dev_is_persistent_node(child) == 0) { 861 if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, child, 862 DDI_PROP_DONTPASS, "unit-address", &unit_addr, &n) != 863 DDI_PROP_SUCCESS) { 864 cmn_err(CE_WARN, 865 "cannot find unit-address in %s.conf", 866 ddi_driver_name(child)); 867 return (DDI_FAILURE); 868 } 869 if (n != 1 || *unit_addr == NULL || **unit_addr == 0) { 870 cmn_err(CE_WARN, "unit-address property in %s.conf" 871 " not well-formed", ddi_driver_name(child)); 872 ddi_prop_free(unit_addr); 873 return (DDI_FAILURE); 874 } 875 (void) snprintf(name, namelen, "%s", *unit_addr); 876 ddi_prop_free(unit_addr); 877 return (DDI_SUCCESS); 878 } 879 880 /* 881 * Get the address portion of the node name based on 882 * the function and device number. 883 */ 884 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child, 885 DDI_PROP_DONTPASS, "reg", (int **)&pci_rp, &n) != DDI_SUCCESS) { 886 return (DDI_FAILURE); 887 } 888 889 /* copy the device identifications */ 890 device = PCI_REG_DEV_G(pci_rp[0].pci_phys_hi); 891 func = PCI_REG_FUNC_G(pci_rp[0].pci_phys_hi); 892 893 if (pcie_ari_is_enabled(ddi_get_parent(child)) 894 == PCIE_ARI_FORW_ENABLED) { 895 func = (device << 3) | func; 896 device = 0; 897 } 898 899 if (func != 0) 900 (void) snprintf(name, namelen, "%x,%x", device, func); 901 else 902 (void) snprintf(name, namelen, "%x", device); 903 904 ddi_prop_free(pci_rp); 905 return (DDI_SUCCESS); 906 } 907 908 static int 909 pcieb_initchild(dev_info_t *child) 910 { 911 char name[MAXNAMELEN]; 912 int result = DDI_FAILURE; 913 pcieb_devstate_t *pcieb = 914 (pcieb_devstate_t *)ddi_get_soft_state(pcieb_state, 915 ddi_get_instance(ddi_get_parent(child))); 916 917 /* 918 * Name the child 919 */ 920 if (pcieb_name_child(child, name, MAXNAMELEN) != DDI_SUCCESS) { 921 result = DDI_FAILURE; 922 goto done; 923 } 924 ddi_set_name_addr(child, name); 925 926 /* 927 * Pseudo nodes indicate a prototype node with per-instance 928 * properties to be merged into the real h/w device node. 929 * The interpretation of the unit-address is DD[,F] 930 * where DD is the device id and F is the function. 931 */ 932 if (ndi_dev_is_persistent_node(child) == 0) { 933 extern int pci_allow_pseudo_children; 934 935 /* 936 * Try to merge the properties from this prototype 937 * node into real h/w nodes. 938 */ 939 if (ndi_merge_node(child, pcieb_name_child) == DDI_SUCCESS) { 940 /* 941 * Merged ok - return failure to remove the node. 942 */ 943 ddi_set_name_addr(child, NULL); 944 result = DDI_FAILURE; 945 goto done; 946 } 947 948 /* workaround for ddivs to run under PCI-E */ 949 if (pci_allow_pseudo_children) { 950 result = DDI_SUCCESS; 951 goto done; 952 } 953 954 /* 955 * The child was not merged into a h/w node, 956 * but there's not much we can do with it other 957 * than return failure to cause the node to be removed. 958 */ 959 cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged", 960 ddi_driver_name(child), ddi_get_name_addr(child), 961 ddi_driver_name(child)); 962 ddi_set_name_addr(child, NULL); 963 result = DDI_NOT_WELL_FORMED; 964 goto done; 965 } 966 967 /* platform specific initchild */ 968 pcieb_plat_initchild(child); 969 970 if (pcie_pm_hold(pcieb->pcieb_dip) != DDI_SUCCESS) { 971 PCIEB_DEBUG(DBG_PWR, pcieb->pcieb_dip, 972 "INITCHILD: px_pm_hold failed\n"); 973 result = DDI_FAILURE; 974 goto done; 975 } 976 /* Any return from here must call pcie_pm_release */ 977 978 /* 979 * If configuration registers were previously saved by 980 * child (before it entered D3), then let the child do the 981 * restore to set up the config regs as it'll first need to 982 * power the device out of D3. 983 */ 984 if (ddi_prop_exists(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, 985 "config-regs-saved-by-child") == 1) { 986 PCIEB_DEBUG(DBG_PWR, ddi_get_parent(child), 987 "INITCHILD: config regs to be restored by child" 988 " for %s@%s\n", ddi_node_name(child), 989 ddi_get_name_addr(child)); 990 991 result = DDI_SUCCESS; 992 goto cleanup; 993 } 994 995 PCIEB_DEBUG(DBG_PWR, ddi_get_parent(child), 996 "INITCHILD: config regs setup for %s@%s\n", 997 ddi_node_name(child), ddi_get_name_addr(child)); 998 999 pcie_init_dom(child); 1000 1001 if (pcie_initchild(child) != DDI_SUCCESS) { 1002 result = DDI_FAILURE; 1003 pcie_fini_dom(child); 1004 goto cleanup; 1005 } 1006 1007 #ifdef PX_PLX 1008 if (pcieb_init_plx_workarounds(pcieb, child) == DDI_FAILURE) { 1009 result = DDI_FAILURE; 1010 pcie_fini_dom(child); 1011 goto cleanup; 1012 } 1013 #endif /* PX_PLX */ 1014 1015 result = DDI_SUCCESS; 1016 cleanup: 1017 pcie_pm_release(pcieb->pcieb_dip); 1018 done: 1019 return (result); 1020 } 1021 1022 static void 1023 pcieb_uninitchild(dev_info_t *dip) 1024 { 1025 1026 pcie_uninitchild(dip); 1027 1028 pcieb_plat_uninitchild(dip); 1029 1030 ddi_set_name_addr(dip, NULL); 1031 1032 /* 1033 * Strip the node to properly convert it back to prototype form 1034 */ 1035 ddi_remove_minor_node(dip, NULL); 1036 1037 ddi_prop_remove_all(dip); 1038 } 1039 1040 static boolean_t 1041 pcieb_is_pcie_device_type(dev_info_t *dip) 1042 { 1043 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip); 1044 1045 if (PCIE_IS_SW(bus_p) || PCIE_IS_RP(bus_p) || PCIE_IS_PCI2PCIE(bus_p)) 1046 return (B_TRUE); 1047 1048 return (B_FALSE); 1049 } 1050 1051 static int 1052 pcieb_intr_attach(pcieb_devstate_t *pcieb) 1053 { 1054 int intr_types; 1055 dev_info_t *dip = pcieb->pcieb_dip; 1056 1057 /* Allow platform specific code to do any initialization first */ 1058 pcieb_plat_intr_attach(pcieb); 1059 1060 /* 1061 * Initialize interrupt handlers. 1062 * If both MSI and FIXED are supported, try to attach MSI first. 1063 * If MSI fails for any reason, then try FIXED, but only allow one 1064 * type to be attached. 1065 */ 1066 if (ddi_intr_get_supported_types(dip, &intr_types) != DDI_SUCCESS) { 1067 PCIEB_DEBUG(DBG_ATTACH, dip, "ddi_intr_get_supported_types" 1068 " failed\n"); 1069 goto FAIL; 1070 } 1071 1072 if ((intr_types & DDI_INTR_TYPE_MSI) && 1073 (pcieb_msi_supported(dip) == DDI_SUCCESS)) { 1074 if (pcieb_intr_init(pcieb, DDI_INTR_TYPE_MSI) == DDI_SUCCESS) 1075 intr_types = DDI_INTR_TYPE_MSI; 1076 else { 1077 PCIEB_DEBUG(DBG_ATTACH, dip, "Unable to attach MSI" 1078 " handler\n"); 1079 } 1080 } 1081 1082 if (intr_types != DDI_INTR_TYPE_MSI) { 1083 /* 1084 * MSIs are not supported or MSI initialization failed. For Root 1085 * Ports mark this so error handling might try to fallback to 1086 * some other mechanism if available (machinecheck etc.). 1087 */ 1088 if (PCIE_IS_RP(PCIE_DIP2UPBUS(dip))) 1089 pcieb->pcieb_no_aer_msi = B_TRUE; 1090 } 1091 1092 if (intr_types & DDI_INTR_TYPE_FIXED) { 1093 if (pcieb_intr_init(pcieb, DDI_INTR_TYPE_FIXED) != 1094 DDI_SUCCESS) { 1095 PCIEB_DEBUG(DBG_ATTACH, dip, 1096 "Unable to attach INTx handler\n"); 1097 goto FAIL; 1098 } 1099 } 1100 return (DDI_SUCCESS); 1101 1102 FAIL: 1103 return (DDI_FAILURE); 1104 } 1105 1106 /* 1107 * This function initializes internally generated interrupts only. 1108 * It does not affect any interrupts generated by downstream devices 1109 * or the forwarding of them. 1110 * 1111 * Enable Device Specific Interrupts or Hotplug features here. 1112 * Enabling features may change how many interrupts are requested 1113 * by the device. If features are not enabled first, the 1114 * device might not ask for any interrupts. 1115 */ 1116 static int 1117 pcieb_intr_init(pcieb_devstate_t *pcieb, int intr_type) 1118 { 1119 dev_info_t *dip = pcieb->pcieb_dip; 1120 int nintrs, request, count, x; 1121 int intr_cap = 0; 1122 int inum = 0; 1123 int ret; 1124 pcie_bus_t *bus_p = PCIE_DIP2UPBUS(dip); 1125 uint16_t vendorid = bus_p->bus_dev_ven_id & 0xFFFF; 1126 boolean_t is_hp = B_FALSE; 1127 boolean_t is_pme = B_FALSE; 1128 boolean_t is_lbw = B_FALSE; 1129 1130 PCIEB_DEBUG(DBG_ATTACH, dip, "pcieb_intr_init: Attaching %s handler\n", 1131 (intr_type == DDI_INTR_TYPE_MSI) ? "MSI" : "INTx"); 1132 1133 request = 0; 1134 if (PCIE_IS_HOTPLUG_ENABLED(dip)) { 1135 is_hp = B_TRUE; 1136 } 1137 1138 if ((intr_type == DDI_INTR_TYPE_MSI) && PCIE_IS_RP(bus_p) && 1139 (vendorid == NVIDIA_VENDOR_ID)) { 1140 is_pme = B_TRUE; 1141 } 1142 1143 if (intr_type == DDI_INTR_TYPE_MSI && pcie_link_bw_supported(dip)) { 1144 is_lbw = B_TRUE; 1145 } 1146 1147 /* 1148 * The hot-plug, link bandwidth, and power management events all are 1149 * based on the PCI Express capability. Therefore, they all share their 1150 * own interrupt. 1151 */ 1152 if (is_hp || is_pme || is_lbw) { 1153 request++; 1154 } 1155 1156 /* 1157 * If this device is a root port, which means it can have MSI interrupts 1158 * enabled for AERs, then we need to request one. 1159 */ 1160 if (intr_type == DDI_INTR_TYPE_MSI) { 1161 if (PCIE_IS_RP(bus_p) && PCIE_HAS_AER(bus_p)) { 1162 request++; 1163 } 1164 } 1165 1166 if (request == 0) 1167 return (DDI_SUCCESS); 1168 1169 /* 1170 * Get number of supported interrupts. 1171 * 1172 * Several Bridges/Switches will not have this property set, resulting 1173 * in a FAILURE, if the device is not configured in a way that 1174 * interrupts are needed. (eg. hotplugging) 1175 */ 1176 ret = ddi_intr_get_nintrs(dip, intr_type, &nintrs); 1177 if ((ret != DDI_SUCCESS) || (nintrs == 0)) { 1178 PCIEB_DEBUG(DBG_ATTACH, dip, "ddi_intr_get_nintrs ret:%d" 1179 " req:%d\n", ret, nintrs); 1180 return (DDI_FAILURE); 1181 } 1182 1183 PCIEB_DEBUG(DBG_ATTACH, dip, "bdf 0x%x: ddi_intr_get_nintrs: nintrs %d", 1184 " request %d\n", bus_p->bus_bdf, nintrs, request); 1185 1186 if (request > nintrs) 1187 request = nintrs; 1188 1189 /* Allocate an array of interrupt handlers */ 1190 pcieb->pcieb_htable_size = sizeof (ddi_intr_handle_t) * request; 1191 pcieb->pcieb_htable = kmem_zalloc(pcieb->pcieb_htable_size, 1192 KM_SLEEP); 1193 pcieb->pcieb_init_flags |= PCIEB_INIT_HTABLE; 1194 1195 ret = ddi_intr_alloc(dip, pcieb->pcieb_htable, intr_type, inum, 1196 request, &count, DDI_INTR_ALLOC_NORMAL); 1197 if ((ret != DDI_SUCCESS) || (count == 0)) { 1198 PCIEB_DEBUG(DBG_ATTACH, dip, "ddi_intr_alloc() ret: %d ask: %d" 1199 " actual: %d\n", ret, request, count); 1200 goto FAIL; 1201 } 1202 pcieb->pcieb_init_flags |= PCIEB_INIT_ALLOC; 1203 1204 /* Save the actual number of interrupts allocated */ 1205 pcieb->pcieb_intr_count = count; 1206 if (count < request) { 1207 PCIEB_DEBUG(DBG_ATTACH, dip, "bdf 0%x: Requested Intr: %d" 1208 " Received: %d\n", bus_p->bus_bdf, request, count); 1209 } 1210 1211 /* 1212 * NVidia (MCP55 and other) chipsets have a errata that if the number 1213 * of requested MSI intrs is not allocated we have to fall back to INTx. 1214 */ 1215 if (intr_type == DDI_INTR_TYPE_MSI) { 1216 if (PCIE_IS_RP(bus_p) && (vendorid == NVIDIA_VENDOR_ID)) { 1217 if (request != count) 1218 goto FAIL; 1219 } 1220 } 1221 1222 /* Get interrupt priority */ 1223 ret = ddi_intr_get_pri(pcieb->pcieb_htable[0], 1224 &pcieb->pcieb_intr_priority); 1225 if (ret != DDI_SUCCESS) { 1226 PCIEB_DEBUG(DBG_ATTACH, dip, "ddi_intr_get_pri() ret: %d\n", 1227 ret); 1228 goto FAIL; 1229 } 1230 1231 if (pcieb->pcieb_intr_priority >= LOCK_LEVEL) { 1232 pcieb->pcieb_intr_priority = LOCK_LEVEL - 1; 1233 ret = ddi_intr_set_pri(pcieb->pcieb_htable[0], 1234 pcieb->pcieb_intr_priority); 1235 if (ret != DDI_SUCCESS) { 1236 PCIEB_DEBUG(DBG_ATTACH, dip, "ddi_intr_set_pri() ret:" 1237 " %d\n", ret); 1238 1239 goto FAIL; 1240 } 1241 } 1242 1243 mutex_init(&pcieb->pcieb_intr_mutex, NULL, MUTEX_DRIVER, NULL); 1244 1245 pcieb->pcieb_init_flags |= PCIEB_INIT_MUTEX; 1246 1247 for (count = 0; count < pcieb->pcieb_intr_count; count++) { 1248 ret = ddi_intr_add_handler(pcieb->pcieb_htable[count], 1249 pcieb_intr_handler, (caddr_t)pcieb, 1250 (caddr_t)(uintptr_t)(inum + count)); 1251 1252 if (ret != DDI_SUCCESS) { 1253 PCIEB_DEBUG(DBG_ATTACH, dip, "Cannot add " 1254 "interrupt(%d)\n", ret); 1255 break; 1256 } 1257 } 1258 1259 /* If unsucessful, remove the added handlers */ 1260 if (ret != DDI_SUCCESS) { 1261 for (x = 0; x < count; x++) { 1262 (void) ddi_intr_remove_handler(pcieb->pcieb_htable[x]); 1263 } 1264 goto FAIL; 1265 } 1266 1267 pcieb->pcieb_init_flags |= PCIEB_INIT_HANDLER; 1268 1269 (void) ddi_intr_get_cap(pcieb->pcieb_htable[0], &intr_cap); 1270 1271 /* 1272 * Get this intr lock because we are not quite ready to handle 1273 * interrupts immediately after enabling it. The MSI multi register 1274 * gets programmed in ddi_intr_enable after which we need to get the 1275 * MSI offsets for Hotplug/AER. 1276 */ 1277 mutex_enter(&pcieb->pcieb_intr_mutex); 1278 1279 if (intr_cap & DDI_INTR_FLAG_BLOCK) { 1280 (void) ddi_intr_block_enable(pcieb->pcieb_htable, 1281 pcieb->pcieb_intr_count); 1282 pcieb->pcieb_init_flags |= PCIEB_INIT_BLOCK; 1283 } else { 1284 for (count = 0; count < pcieb->pcieb_intr_count; count++) { 1285 (void) ddi_intr_enable(pcieb->pcieb_htable[count]); 1286 } 1287 } 1288 pcieb->pcieb_init_flags |= PCIEB_INIT_ENABLE; 1289 1290 /* Save the interrupt type */ 1291 pcieb->pcieb_intr_type = intr_type; 1292 1293 /* Get the MSI offset for hotplug/PME from the PCIe cap reg */ 1294 if (intr_type == DDI_INTR_TYPE_MSI) { 1295 uint16_t pcie_msi_off; 1296 pcie_msi_off = PCI_CAP_GET16(bus_p->bus_cfg_hdl, 0, 1297 bus_p->bus_pcie_off, PCIE_PCIECAP) & 1298 PCIE_PCIECAP_INT_MSG_NUM; 1299 1300 if (pcie_msi_off >= count) { 1301 PCIEB_DEBUG(DBG_ATTACH, dip, "MSI number %u in PCIe " 1302 "cap > max allocated %d\n", pcie_msi_off, count); 1303 mutex_exit(&pcieb->pcieb_intr_mutex); 1304 goto FAIL; 1305 } 1306 1307 if (is_hp) { 1308 pcieb->pcieb_isr_tab[pcie_msi_off] |= PCIEB_INTR_SRC_HP; 1309 } 1310 1311 if (is_pme) { 1312 pcieb->pcieb_isr_tab[pcie_msi_off] |= 1313 PCIEB_INTR_SRC_PME; 1314 } 1315 1316 if (is_lbw) { 1317 pcieb->pcieb_isr_tab[pcie_msi_off] |= 1318 PCIEB_INTR_SRC_LBW; 1319 } 1320 } else { 1321 /* INTx handles only Hotplug interrupts */ 1322 if (is_hp) 1323 pcieb->pcieb_isr_tab[0] |= PCIEB_INTR_SRC_HP; 1324 } 1325 1326 1327 /* 1328 * Get the MSI offset for errors from the AER Root Error status 1329 * register. 1330 */ 1331 if ((intr_type == DDI_INTR_TYPE_MSI) && PCIE_IS_RP(bus_p)) { 1332 if (PCIE_HAS_AER(bus_p)) { 1333 int aer_msi_off; 1334 aer_msi_off = (PCI_XCAP_GET32(bus_p->bus_cfg_hdl, 0, 1335 bus_p->bus_aer_off, PCIE_AER_RE_STS) >> 1336 PCIE_AER_RE_STS_MSG_NUM_SHIFT) & 1337 PCIE_AER_RE_STS_MSG_NUM_MASK; 1338 1339 if (aer_msi_off >= count) { 1340 PCIEB_DEBUG(DBG_ATTACH, dip, "MSI number %d in" 1341 " AER cap > max allocated %d\n", 1342 aer_msi_off, count); 1343 mutex_exit(&pcieb->pcieb_intr_mutex); 1344 goto FAIL; 1345 } 1346 pcieb->pcieb_isr_tab[aer_msi_off] |= PCIEB_INTR_SRC_AER; 1347 } else { 1348 /* 1349 * This RP does not have AER. Fallback to the 1350 * SERR+Machinecheck approach if available. 1351 */ 1352 pcieb->pcieb_no_aer_msi = B_TRUE; 1353 } 1354 } 1355 1356 mutex_exit(&pcieb->pcieb_intr_mutex); 1357 return (DDI_SUCCESS); 1358 1359 FAIL: 1360 pcieb_intr_fini(pcieb); 1361 return (DDI_FAILURE); 1362 } 1363 1364 static void 1365 pcieb_intr_fini(pcieb_devstate_t *pcieb) 1366 { 1367 int x; 1368 int count = pcieb->pcieb_intr_count; 1369 int flags = pcieb->pcieb_init_flags; 1370 1371 if ((flags & PCIEB_INIT_ENABLE) && 1372 (flags & PCIEB_INIT_BLOCK)) { 1373 (void) ddi_intr_block_disable(pcieb->pcieb_htable, count); 1374 flags &= ~(PCIEB_INIT_ENABLE | 1375 PCIEB_INIT_BLOCK); 1376 } 1377 1378 if (flags & PCIEB_INIT_MUTEX) 1379 mutex_destroy(&pcieb->pcieb_intr_mutex); 1380 1381 for (x = 0; x < count; x++) { 1382 if (flags & PCIEB_INIT_ENABLE) 1383 (void) ddi_intr_disable(pcieb->pcieb_htable[x]); 1384 1385 if (flags & PCIEB_INIT_HANDLER) 1386 (void) ddi_intr_remove_handler(pcieb->pcieb_htable[x]); 1387 1388 if (flags & PCIEB_INIT_ALLOC) 1389 (void) ddi_intr_free(pcieb->pcieb_htable[x]); 1390 } 1391 1392 flags &= ~(PCIEB_INIT_ENABLE | PCIEB_INIT_HANDLER | PCIEB_INIT_ALLOC | 1393 PCIEB_INIT_MUTEX); 1394 1395 if (flags & PCIEB_INIT_HTABLE) 1396 kmem_free(pcieb->pcieb_htable, pcieb->pcieb_htable_size); 1397 1398 flags &= ~PCIEB_INIT_HTABLE; 1399 1400 pcieb->pcieb_init_flags &= flags; 1401 } 1402 1403 /* 1404 * Checks if this device needs MSIs enabled or not. 1405 */ 1406 /*ARGSUSED*/ 1407 static int 1408 pcieb_msi_supported(dev_info_t *dip) 1409 { 1410 return ((pcieb_enable_msi && pcieb_plat_msi_supported(dip)) ? 1411 DDI_SUCCESS: DDI_FAILURE); 1412 } 1413 1414 /*ARGSUSED*/ 1415 static int 1416 pcieb_fm_init_child(dev_info_t *dip, dev_info_t *tdip, int cap, 1417 ddi_iblock_cookie_t *ibc) 1418 { 1419 pcieb_devstate_t *pcieb = ddi_get_soft_state(pcieb_state, 1420 ddi_get_instance(dip)); 1421 1422 ASSERT(ibc != NULL); 1423 *ibc = pcieb->pcieb_fm_ibc; 1424 1425 return (DEVI(dip)->devi_fmhdl->fh_cap | DDI_FM_ACCCHK_CAPABLE | 1426 DDI_FM_DMACHK_CAPABLE); 1427 } 1428 1429 static int 1430 pcieb_fm_init(pcieb_devstate_t *pcieb_p) 1431 { 1432 dev_info_t *dip = pcieb_p->pcieb_dip; 1433 int fm_cap = DDI_FM_EREPORT_CAPABLE; 1434 1435 /* 1436 * Request our capability level and get our parents capability 1437 * and ibc. 1438 */ 1439 ddi_fm_init(dip, &fm_cap, &pcieb_p->pcieb_fm_ibc); 1440 1441 return (DDI_SUCCESS); 1442 } 1443 1444 /* 1445 * Breakdown our FMA resources 1446 */ 1447 static void 1448 pcieb_fm_fini(pcieb_devstate_t *pcieb_p) 1449 { 1450 /* 1451 * Clean up allocated fm structures 1452 */ 1453 ddi_fm_fini(pcieb_p->pcieb_dip); 1454 } 1455 1456 static int 1457 pcieb_open(dev_t *devp, int flags, int otyp, cred_t *credp) 1458 { 1459 int inst = PCI_MINOR_NUM_TO_INSTANCE(getminor(*devp)); 1460 pcieb_devstate_t *pcieb = ddi_get_soft_state(pcieb_state, inst); 1461 int rv; 1462 1463 if (pcieb == NULL) 1464 return (ENXIO); 1465 1466 mutex_enter(&pcieb->pcieb_mutex); 1467 rv = pcie_open(pcieb->pcieb_dip, devp, flags, otyp, credp); 1468 mutex_exit(&pcieb->pcieb_mutex); 1469 1470 return (rv); 1471 } 1472 1473 static int 1474 pcieb_close(dev_t dev, int flags, int otyp, cred_t *credp) 1475 { 1476 int inst = PCI_MINOR_NUM_TO_INSTANCE(getminor(dev)); 1477 pcieb_devstate_t *pcieb = ddi_get_soft_state(pcieb_state, inst); 1478 int rv; 1479 1480 if (pcieb == NULL) 1481 return (ENXIO); 1482 1483 mutex_enter(&pcieb->pcieb_mutex); 1484 rv = pcie_close(pcieb->pcieb_dip, dev, flags, otyp, credp); 1485 mutex_exit(&pcieb->pcieb_mutex); 1486 1487 return (rv); 1488 } 1489 1490 static int 1491 pcieb_ioctl_retrain(pcieb_devstate_t *pcieb, cred_t *credp) 1492 { 1493 pcie_bus_t *bus_p = PCIE_DIP2BUS(pcieb->pcieb_dip); 1494 1495 if (drv_priv(credp) != 0) { 1496 return (EPERM); 1497 } 1498 1499 if (!PCIE_IS_PCIE(bus_p)) { 1500 return (ENOTSUP); 1501 } 1502 1503 if (!PCIE_IS_RP(bus_p) && !PCIE_IS_SWD(bus_p)) { 1504 return (ENOTSUP); 1505 } 1506 1507 return (pcie_link_retrain(pcieb->pcieb_dip)); 1508 } 1509 1510 static int 1511 pcieb_ioctl_get_speed(pcieb_devstate_t *pcieb, intptr_t arg, int mode, 1512 cred_t *credp) 1513 { 1514 pcie_bus_t *bus_p = PCIE_DIP2BUS(pcieb->pcieb_dip); 1515 pcieb_ioctl_target_speed_t pits; 1516 1517 if (drv_priv(credp) != 0) { 1518 return (EPERM); 1519 } 1520 1521 if (!PCIE_IS_PCIE(bus_p)) { 1522 return (ENOTSUP); 1523 } 1524 1525 if (!PCIE_IS_RP(bus_p) && !PCIE_IS_SWD(bus_p)) { 1526 return (ENOTSUP); 1527 } 1528 1529 pits.pits_flags = 0; 1530 pits.pits_speed = PCIEB_LINK_SPEED_UNKNOWN; 1531 1532 mutex_enter(&bus_p->bus_speed_mutex); 1533 if ((bus_p->bus_speed_flags & PCIE_LINK_F_ADMIN_TARGET) != 0) { 1534 pits.pits_flags |= PCIEB_FLAGS_ADMIN_SET; 1535 } 1536 switch (bus_p->bus_target_speed) { 1537 case PCIE_LINK_SPEED_2_5: 1538 pits.pits_speed = PCIEB_LINK_SPEED_GEN1; 1539 break; 1540 case PCIE_LINK_SPEED_5: 1541 pits.pits_speed = PCIEB_LINK_SPEED_GEN2; 1542 break; 1543 case PCIE_LINK_SPEED_8: 1544 pits.pits_speed = PCIEB_LINK_SPEED_GEN3; 1545 break; 1546 case PCIE_LINK_SPEED_16: 1547 pits.pits_speed = PCIEB_LINK_SPEED_GEN4; 1548 break; 1549 default: 1550 pits.pits_speed = PCIEB_LINK_SPEED_UNKNOWN; 1551 break; 1552 } 1553 mutex_exit(&bus_p->bus_speed_mutex); 1554 1555 if (ddi_copyout(&pits, (void *)arg, sizeof (pits), 1556 mode & FKIOCTL) != 0) { 1557 return (EFAULT); 1558 } 1559 1560 return (0); 1561 } 1562 1563 static int 1564 pcieb_ioctl_set_speed(pcieb_devstate_t *pcieb, intptr_t arg, int mode, 1565 cred_t *credp) 1566 { 1567 pcie_bus_t *bus_p = PCIE_DIP2BUS(pcieb->pcieb_dip); 1568 pcieb_ioctl_target_speed_t pits; 1569 pcie_link_speed_t speed; 1570 1571 if (drv_priv(credp) != 0) { 1572 return (EPERM); 1573 } 1574 1575 if (!PCIE_IS_PCIE(bus_p)) { 1576 return (ENOTSUP); 1577 } 1578 1579 if (!PCIE_IS_RP(bus_p) && !PCIE_IS_SWD(bus_p)) { 1580 return (ENOTSUP); 1581 } 1582 1583 if (ddi_copyin((void *)arg, &pits, sizeof (pits), 1584 mode & FKIOCTL) != 0) { 1585 return (EFAULT); 1586 } 1587 1588 if (pits.pits_flags != 0) { 1589 return (EINVAL); 1590 } 1591 1592 switch (pits.pits_speed) { 1593 case PCIEB_LINK_SPEED_GEN1: 1594 speed = PCIE_LINK_SPEED_2_5; 1595 break; 1596 case PCIEB_LINK_SPEED_GEN2: 1597 speed = PCIE_LINK_SPEED_5; 1598 break; 1599 case PCIEB_LINK_SPEED_GEN3: 1600 speed = PCIE_LINK_SPEED_8; 1601 break; 1602 case PCIEB_LINK_SPEED_GEN4: 1603 speed = PCIE_LINK_SPEED_16; 1604 break; 1605 default: 1606 return (EINVAL); 1607 } 1608 1609 return (pcie_link_set_target(pcieb->pcieb_dip, speed)); 1610 } 1611 1612 static int 1613 pcieb_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, 1614 int *rvalp) 1615 { 1616 int inst = PCI_MINOR_NUM_TO_INSTANCE(getminor(dev)); 1617 pcieb_devstate_t *pcieb = ddi_get_soft_state(pcieb_state, inst); 1618 int rv; 1619 1620 if (pcieb == NULL) 1621 return (ENXIO); 1622 1623 /* 1624 * Check if this is one of the commands that the bridge driver natively 1625 * understands. There are only a handful of such private ioctls defined 1626 * in pcieb_ioctl.h. Otherwise, this ioctl should be handled by the 1627 * general pcie driver. 1628 */ 1629 switch (cmd) { 1630 case PCIEB_IOCTL_RETRAIN: 1631 rv = pcieb_ioctl_retrain(pcieb, credp); 1632 break; 1633 case PCIEB_IOCTL_GET_TARGET_SPEED: 1634 rv = pcieb_ioctl_get_speed(pcieb, arg, mode, credp); 1635 break; 1636 case PCIEB_IOCTL_SET_TARGET_SPEED: 1637 rv = pcieb_ioctl_set_speed(pcieb, arg, mode, credp); 1638 break; 1639 default: 1640 /* To handle devctl and hotplug related ioctls */ 1641 rv = pcie_ioctl(pcieb->pcieb_dip, dev, cmd, arg, mode, credp, 1642 rvalp); 1643 break; 1644 } 1645 1646 return (rv); 1647 } 1648 1649 /* 1650 * Common interrupt handler for hotplug, PME and errors. 1651 */ 1652 static uint_t 1653 pcieb_intr_handler(caddr_t arg1, caddr_t arg2) 1654 { 1655 pcieb_devstate_t *pcieb_p = (pcieb_devstate_t *)arg1; 1656 dev_info_t *dip = pcieb_p->pcieb_dip; 1657 ddi_fm_error_t derr; 1658 int sts = 0; 1659 int ret = DDI_INTR_UNCLAIMED; 1660 int isrc; 1661 1662 if (!(pcieb_p->pcieb_init_flags & PCIEB_INIT_ENABLE)) 1663 goto FAIL; 1664 1665 mutex_enter(&pcieb_p->pcieb_intr_mutex); 1666 isrc = pcieb_p->pcieb_isr_tab[(int)(uintptr_t)arg2]; 1667 mutex_exit(&pcieb_p->pcieb_intr_mutex); 1668 1669 PCIEB_DEBUG(DBG_INTR, dip, "Received intr number %d\n", 1670 (int)(uintptr_t)arg2); 1671 1672 if (isrc == PCIEB_INTR_SRC_UNKNOWN) 1673 goto FAIL; 1674 1675 if (isrc & (PCIEB_INTR_SRC_HP | PCIEB_INTR_SRC_LBW)) 1676 ret = pcie_intr(dip); 1677 1678 if (isrc & PCIEB_INTR_SRC_PME) 1679 ret = DDI_INTR_CLAIMED; 1680 1681 /* AER Error */ 1682 if (isrc & PCIEB_INTR_SRC_AER) { 1683 /* 1684 * If MSI is shared with PME/hotplug then check Root Error 1685 * Status Reg before claiming it. For now it's ok since 1686 * we know we get 2 MSIs. 1687 */ 1688 ret = DDI_INTR_CLAIMED; 1689 bzero(&derr, sizeof (ddi_fm_error_t)); 1690 derr.fme_version = DDI_FME_VERSION; 1691 mutex_enter(&pcieb_p->pcieb_peek_poke_mutex); 1692 mutex_enter(&pcieb_p->pcieb_err_mutex); 1693 1694 pf_eh_enter(PCIE_DIP2BUS(dip)); 1695 PCIE_ROOT_EH_SRC(PCIE_DIP2PFD(dip))->intr_type = 1696 PF_INTR_TYPE_AER; 1697 1698 if ((DEVI(dip)->devi_fmhdl->fh_cap) & DDI_FM_EREPORT_CAPABLE) 1699 sts = pf_scan_fabric(dip, &derr, NULL); 1700 pf_eh_exit(PCIE_DIP2BUS(dip)); 1701 1702 mutex_exit(&pcieb_p->pcieb_err_mutex); 1703 mutex_exit(&pcieb_p->pcieb_peek_poke_mutex); 1704 if (pcieb_die & sts) 1705 fm_panic("%s-%d: PCI(-X) Express Fatal Error. (0x%x)", 1706 ddi_driver_name(dip), ddi_get_instance(dip), sts); 1707 } 1708 FAIL: 1709 return (ret); 1710 } 1711 1712 /* 1713 * Some PCI-X to PCI-E bridges do not support full 64-bit addressing on the 1714 * PCI-X side of the bridge. We build a special version of this driver for 1715 * those bridges, which uses PCIEB_ADDR_LIMIT_LO and/or PCIEB_ADDR_LIMIT_HI 1716 * to define the range of values which the chip can handle. The code below 1717 * then clamps the DMA address range supplied by the driver, preventing the 1718 * PCI-E nexus driver from allocating any memory the bridge can't deal 1719 * with. 1720 */ 1721 static int 1722 pcieb_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, 1723 ddi_dma_attr_t *attr_p, int (*waitfp)(caddr_t), caddr_t arg, 1724 ddi_dma_handle_t *handlep) 1725 { 1726 int ret; 1727 #ifdef PCIEB_BCM 1728 uint64_t lim; 1729 1730 /* 1731 * If the leaf device's limits are outside than what the Broadcom 1732 * bridge can handle, we need to clip the values passed up the chain. 1733 */ 1734 lim = attr_p->dma_attr_addr_lo; 1735 attr_p->dma_attr_addr_lo = MAX(lim, PCIEB_ADDR_LIMIT_LO); 1736 1737 lim = attr_p->dma_attr_addr_hi; 1738 attr_p->dma_attr_addr_hi = MIN(lim, PCIEB_ADDR_LIMIT_HI); 1739 1740 #endif /* PCIEB_BCM */ 1741 1742 /* 1743 * This is a software workaround to fix the Broadcom 5714/5715 PCIe-PCI 1744 * bridge prefetch bug. Intercept the DMA alloc handle request and set 1745 * PX_DMAI_FLAGS_MAP_BUFZONE flag in the handle. If this flag is set, 1746 * the px nexus driver will allocate an extra page & make it valid one, 1747 * for any DVMA request that comes from any of the Broadcom bridge child 1748 * devices. 1749 */ 1750 if ((ret = ddi_dma_allochdl(dip, rdip, attr_p, waitfp, arg, 1751 handlep)) == DDI_SUCCESS) { 1752 ddi_dma_impl_t *mp = (ddi_dma_impl_t *)*handlep; 1753 #ifdef PCIEB_BCM 1754 mp->dmai_inuse |= PX_DMAI_FLAGS_MAP_BUFZONE; 1755 #endif /* PCIEB_BCM */ 1756 /* 1757 * For a given rdip, update mp->dmai_bdf with the bdf value 1758 * of pcieb's immediate child or secondary bus-id of the 1759 * PCIe2PCI bridge. 1760 */ 1761 mp->dmai_minxfer = pcie_get_bdf_for_dma_xfer(dip, rdip); 1762 } 1763 1764 return (ret); 1765 } 1766 1767 /* 1768 * FDVMA feature is not supported for any child device of Broadcom 5714/5715 1769 * PCIe-PCI bridge due to prefetch bug. Return failure immediately, so that 1770 * these drivers will switch to regular DVMA path. 1771 */ 1772 /*ARGSUSED*/ 1773 static int 1774 pcieb_dma_mctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle, 1775 enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp, 1776 uint_t cache_flags) 1777 { 1778 int ret; 1779 1780 #ifdef PCIEB_BCM 1781 if (cmd == DDI_DMA_RESERVE) 1782 return (DDI_FAILURE); 1783 #endif /* PCIEB_BCM */ 1784 1785 if (((ret = ddi_dma_mctl(dip, rdip, handle, cmd, offp, lenp, objp, 1786 cache_flags)) == DDI_SUCCESS) && (cmd == DDI_DMA_RESERVE)) { 1787 ddi_dma_impl_t *mp = (ddi_dma_impl_t *)*objp; 1788 1789 /* 1790 * For a given rdip, update mp->dmai_bdf with the bdf value 1791 * of pcieb's immediate child or secondary bus-id of the 1792 * PCIe2PCI bridge. 1793 */ 1794 mp->dmai_minxfer = pcie_get_bdf_for_dma_xfer(dip, rdip); 1795 } 1796 1797 return (ret); 1798 } 1799 1800 static int 1801 pcieb_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op, 1802 ddi_intr_handle_impl_t *hdlp, void *result) 1803 { 1804 return (pcieb_plat_intr_ops(dip, rdip, intr_op, hdlp, result)); 1805 1806 } 1807 1808 /* 1809 * Power management related initialization specific to pcieb. 1810 * Called by pcieb_attach() 1811 */ 1812 static int 1813 pcieb_pwr_setup(dev_info_t *dip) 1814 { 1815 char *comp_array[5]; 1816 int i; 1817 ddi_acc_handle_t conf_hdl; 1818 uint16_t pmcap, cap_ptr; 1819 pcie_pwr_t *pwr_p; 1820 1821 /* Some platforms/devices may choose to disable PM */ 1822 if (pcieb_plat_pwr_disable(dip)) { 1823 (void) pcieb_pwr_disable(dip); 1824 return (DDI_SUCCESS); 1825 } 1826 1827 ASSERT(PCIE_PMINFO(dip)); 1828 pwr_p = PCIE_NEXUS_PMINFO(dip); 1829 ASSERT(pwr_p); 1830 1831 /* Code taken from pci_pci driver */ 1832 if (pci_config_setup(dip, &pwr_p->pwr_conf_hdl) != DDI_SUCCESS) { 1833 PCIEB_DEBUG(DBG_PWR, dip, "pcieb_pwr_setup: pci_config_setup " 1834 "failed\n"); 1835 return (DDI_FAILURE); 1836 } 1837 conf_hdl = pwr_p->pwr_conf_hdl; 1838 1839 /* 1840 * Walk the capabilities searching for a PM entry. 1841 */ 1842 if ((PCI_CAP_LOCATE(conf_hdl, PCI_CAP_ID_PM, &cap_ptr)) == 1843 DDI_FAILURE) { 1844 PCIEB_DEBUG(DBG_PWR, dip, "switch/bridge does not support PM. " 1845 " PCI PM data structure not found in config header\n"); 1846 pci_config_teardown(&conf_hdl); 1847 return (DDI_SUCCESS); 1848 } 1849 /* 1850 * Save offset to pmcsr for future references. 1851 */ 1852 pwr_p->pwr_pmcsr_offset = cap_ptr + PCI_PMCSR; 1853 pmcap = PCI_CAP_GET16(conf_hdl, 0, cap_ptr, PCI_PMCAP); 1854 if (pmcap & PCI_PMCAP_D1) { 1855 PCIEB_DEBUG(DBG_PWR, dip, "D1 state supported\n"); 1856 pwr_p->pwr_pmcaps |= PCIE_SUPPORTS_D1; 1857 } 1858 if (pmcap & PCI_PMCAP_D2) { 1859 PCIEB_DEBUG(DBG_PWR, dip, "D2 state supported\n"); 1860 pwr_p->pwr_pmcaps |= PCIE_SUPPORTS_D2; 1861 } 1862 1863 i = 0; 1864 comp_array[i++] = "NAME=PCIe switch/bridge PM"; 1865 comp_array[i++] = "0=Power Off (D3)"; 1866 if (pwr_p->pwr_pmcaps & PCIE_SUPPORTS_D2) 1867 comp_array[i++] = "1=D2"; 1868 if (pwr_p->pwr_pmcaps & PCIE_SUPPORTS_D1) 1869 comp_array[i++] = "2=D1"; 1870 comp_array[i++] = "3=Full Power D0"; 1871 1872 /* 1873 * Create pm-components property, if it does not exist already. 1874 */ 1875 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, dip, 1876 "pm-components", comp_array, i) != DDI_PROP_SUCCESS) { 1877 PCIEB_DEBUG(DBG_PWR, dip, "could not create pm-components " 1878 " prop\n"); 1879 pci_config_teardown(&conf_hdl); 1880 return (DDI_FAILURE); 1881 } 1882 return (pcieb_pwr_init_and_raise(dip, pwr_p)); 1883 } 1884 1885 /* 1886 * undo whatever is done in pcieb_pwr_setup. called by pcieb_detach() 1887 */ 1888 static void 1889 pcieb_pwr_teardown(dev_info_t *dip) 1890 { 1891 pcie_pwr_t *pwr_p; 1892 1893 if (!PCIE_PMINFO(dip) || !(pwr_p = PCIE_NEXUS_PMINFO(dip))) 1894 return; 1895 1896 (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, "pm-components"); 1897 if (pwr_p->pwr_conf_hdl) 1898 pci_config_teardown(&pwr_p->pwr_conf_hdl); 1899 } 1900 1901 /* 1902 * Initializes the power level and raise the power to D0, if it is 1903 * not at D0. 1904 */ 1905 static int 1906 pcieb_pwr_init_and_raise(dev_info_t *dip, pcie_pwr_t *pwr_p) 1907 { 1908 uint16_t pmcsr; 1909 int ret = DDI_SUCCESS; 1910 1911 /* 1912 * Intialize our power level from PMCSR. The common code initializes 1913 * this to UNKNOWN. There is no guarantee that we will be at full 1914 * power at attach. If we are not at D0, raise the power. 1915 */ 1916 pmcsr = pci_config_get16(pwr_p->pwr_conf_hdl, pwr_p->pwr_pmcsr_offset); 1917 pmcsr &= PCI_PMCSR_STATE_MASK; 1918 switch (pmcsr) { 1919 case PCI_PMCSR_D0: 1920 pwr_p->pwr_func_lvl = PM_LEVEL_D0; 1921 break; 1922 1923 case PCI_PMCSR_D1: 1924 pwr_p->pwr_func_lvl = PM_LEVEL_D1; 1925 break; 1926 1927 case PCI_PMCSR_D2: 1928 pwr_p->pwr_func_lvl = PM_LEVEL_D2; 1929 break; 1930 1931 case PCI_PMCSR_D3HOT: 1932 pwr_p->pwr_func_lvl = PM_LEVEL_D3; 1933 break; 1934 1935 default: 1936 break; 1937 } 1938 1939 /* Raise the power to D0. */ 1940 if (pwr_p->pwr_func_lvl != PM_LEVEL_D0 && 1941 ((ret = pm_raise_power(dip, 0, PM_LEVEL_D0)) != DDI_SUCCESS)) { 1942 /* 1943 * Read PMCSR again. If it is at D0, ignore the return 1944 * value from pm_raise_power. 1945 */ 1946 pmcsr = pci_config_get16(pwr_p->pwr_conf_hdl, 1947 pwr_p->pwr_pmcsr_offset); 1948 if ((pmcsr & PCI_PMCSR_STATE_MASK) == PCI_PMCSR_D0) 1949 ret = DDI_SUCCESS; 1950 else { 1951 PCIEB_DEBUG(DBG_PWR, dip, "pcieb_pwr_setup: could not " 1952 "raise power to D0 \n"); 1953 } 1954 } 1955 if (ret == DDI_SUCCESS) 1956 pwr_p->pwr_func_lvl = PM_LEVEL_D0; 1957 return (ret); 1958 } 1959 1960 /* 1961 * Disable PM for x86 and PLX 8532 switch. 1962 * For PLX Transitioning one port on this switch to low power causes links 1963 * on other ports on the same station to die. Due to PLX erratum #34, we 1964 * can't allow the downstream device go to non-D0 state. 1965 */ 1966 static int 1967 pcieb_pwr_disable(dev_info_t *dip) 1968 { 1969 pcie_pwr_t *pwr_p; 1970 1971 ASSERT(PCIE_PMINFO(dip)); 1972 pwr_p = PCIE_NEXUS_PMINFO(dip); 1973 ASSERT(pwr_p); 1974 PCIEB_DEBUG(DBG_PWR, dip, "pcieb_pwr_disable: disabling PM\n"); 1975 pwr_p->pwr_func_lvl = PM_LEVEL_D0; 1976 pwr_p->pwr_flags = PCIE_NO_CHILD_PM; 1977 return (DDI_SUCCESS); 1978 } 1979 1980 #ifdef DEBUG 1981 int pcieb_dbg_intr_print = 0; 1982 void 1983 pcieb_dbg(uint_t bit, dev_info_t *dip, char *fmt, ...) 1984 { 1985 va_list ap; 1986 1987 if (!pcieb_dbg_print) 1988 return; 1989 1990 if (dip) 1991 prom_printf("%s(%d): %s", ddi_driver_name(dip), 1992 ddi_get_instance(dip), pcieb_debug_sym[bit]); 1993 1994 va_start(ap, fmt); 1995 if (servicing_interrupt()) { 1996 if (pcieb_dbg_intr_print) 1997 prom_vprintf(fmt, ap); 1998 } else { 1999 prom_vprintf(fmt, ap); 2000 } 2001 2002 va_end(ap); 2003 } 2004 #endif 2005 2006 static void 2007 pcieb_id_props(pcieb_devstate_t *pcieb) 2008 { 2009 uint64_t serialid = 0; /* 40b field of EUI-64 serial no. register */ 2010 uint16_t cap_ptr; 2011 uint8_t fic = 0; /* 1 = first in chassis device */ 2012 pcie_bus_t *bus_p = PCIE_DIP2BUS(pcieb->pcieb_dip); 2013 ddi_acc_handle_t config_handle = bus_p->bus_cfg_hdl; 2014 2015 /* 2016 * Identify first in chassis. In the special case of a Sun branded 2017 * PLX device, it obviously is first in chassis. Otherwise, in the 2018 * general case, look for an Expansion Slot Register and check its 2019 * first-in-chassis bit. 2020 */ 2021 #ifdef PX_PLX 2022 uint16_t vendor_id = bus_p->bus_dev_ven_id & 0xFFFF; 2023 uint16_t device_id = bus_p->bus_dev_ven_id >> 16; 2024 if ((vendor_id == PXB_VENDOR_SUN) && 2025 ((device_id == PXB_DEVICE_PLX_PCIX) || 2026 (device_id == PXB_DEVICE_PLX_PCIE))) { 2027 fic = 1; 2028 } 2029 #endif /* PX_PLX */ 2030 if ((fic == 0) && ((PCI_CAP_LOCATE(config_handle, 2031 PCI_CAP_ID_SLOT_ID, &cap_ptr)) != DDI_FAILURE)) { 2032 uint8_t esr = PCI_CAP_GET8(config_handle, 0, 2033 cap_ptr, PCI_CAP_ID_REGS_OFF); 2034 if (PCI_CAPSLOT_FIC(esr)) 2035 fic = 1; 2036 } 2037 2038 if ((PCI_CAP_LOCATE(config_handle, 2039 PCI_CAP_XCFG_SPC(PCIE_EXT_CAP_ID_SER), &cap_ptr)) != DDI_FAILURE) { 2040 /* Serialid can be 0 thru a full 40b number */ 2041 serialid = PCI_XCAP_GET32(config_handle, 0, 2042 cap_ptr, PCIE_SER_SID_UPPER_DW); 2043 serialid <<= 32; 2044 serialid |= PCI_XCAP_GET32(config_handle, 0, 2045 cap_ptr, PCIE_SER_SID_LOWER_DW); 2046 } 2047 2048 if (fic) 2049 (void) ndi_prop_create_boolean(DDI_DEV_T_NONE, pcieb->pcieb_dip, 2050 "first-in-chassis"); 2051 if (serialid) 2052 (void) ddi_prop_update_int64(DDI_DEV_T_NONE, pcieb->pcieb_dip, 2053 "serialid#", serialid); 2054 } 2055 2056 static void 2057 pcieb_create_ranges_prop(dev_info_t *dip, 2058 ddi_acc_handle_t config_handle) 2059 { 2060 uint32_t base, limit; 2061 ppb_ranges_t ranges[PCIEB_RANGE_LEN]; 2062 uint8_t io_base_lo, io_limit_lo; 2063 uint16_t io_base_hi, io_limit_hi, mem_base, mem_limit; 2064 int i = 0, rangelen = sizeof (ppb_ranges_t)/sizeof (int); 2065 2066 io_base_lo = pci_config_get8(config_handle, PCI_BCNF_IO_BASE_LOW); 2067 io_limit_lo = pci_config_get8(config_handle, PCI_BCNF_IO_LIMIT_LOW); 2068 io_base_hi = pci_config_get16(config_handle, PCI_BCNF_IO_BASE_HI); 2069 io_limit_hi = pci_config_get16(config_handle, PCI_BCNF_IO_LIMIT_HI); 2070 mem_base = pci_config_get16(config_handle, PCI_BCNF_MEM_BASE); 2071 mem_limit = pci_config_get16(config_handle, PCI_BCNF_MEM_LIMIT); 2072 2073 /* 2074 * Create ranges for IO space 2075 */ 2076 ranges[i].size_low = ranges[i].size_high = 0; 2077 ranges[i].parent_mid = ranges[i].child_mid = ranges[i].parent_high = 0; 2078 ranges[i].child_high = ranges[i].parent_high |= 2079 (PCI_REG_REL_M | PCI_ADDR_IO); 2080 base = PCIEB_16bit_IOADDR(io_base_lo); 2081 limit = PCIEB_16bit_IOADDR(io_limit_lo); 2082 2083 if ((io_base_lo & 0xf) == PCIEB_32BIT_IO) { 2084 base = PCIEB_LADDR(base, io_base_hi); 2085 } 2086 if ((io_limit_lo & 0xf) == PCIEB_32BIT_IO) { 2087 limit = PCIEB_LADDR(limit, io_limit_hi); 2088 } 2089 2090 if ((io_base_lo & PCIEB_32BIT_IO) && (io_limit_hi > 0)) { 2091 base = PCIEB_LADDR(base, io_base_hi); 2092 limit = PCIEB_LADDR(limit, io_limit_hi); 2093 } 2094 2095 /* 2096 * Create ranges for 32bit memory space 2097 */ 2098 base = PCIEB_32bit_MEMADDR(mem_base); 2099 limit = PCIEB_32bit_MEMADDR(mem_limit); 2100 ranges[i].size_low = ranges[i].size_high = 0; 2101 ranges[i].parent_mid = ranges[i].child_mid = ranges[i].parent_high = 0; 2102 ranges[i].child_high = ranges[i].parent_high |= 2103 (PCI_REG_REL_M | PCI_ADDR_MEM32); 2104 ranges[i].child_low = ranges[i].parent_low = base; 2105 if (limit >= base) { 2106 ranges[i].size_low = limit - base + PCIEB_MEMGRAIN; 2107 i++; 2108 } 2109 2110 if (i) { 2111 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "ranges", 2112 (int *)ranges, i * rangelen); 2113 } 2114 } 2115 2116 /* 2117 * For PCI and PCI-X devices including PCIe2PCI bridge, initialize 2118 * cache-line-size and latency timer configuration registers. 2119 */ 2120 void 2121 pcieb_set_pci_perf_parameters(dev_info_t *dip, ddi_acc_handle_t cfg_hdl) 2122 { 2123 uint_t n; 2124 2125 /* Initialize cache-line-size configuration register if needed */ 2126 if (ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 2127 "cache-line-size", 0) == 0) { 2128 pci_config_put8(cfg_hdl, PCI_CONF_CACHE_LINESZ, 2129 PCIEB_CACHE_LINE_SIZE); 2130 n = pci_config_get8(cfg_hdl, PCI_CONF_CACHE_LINESZ); 2131 if (n != 0) { 2132 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 2133 "cache-line-size", n); 2134 } 2135 } 2136 2137 /* Initialize latency timer configuration registers if needed */ 2138 if (ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 2139 "latency-timer", 0) == 0) { 2140 uchar_t min_gnt, latency_timer; 2141 uchar_t header_type; 2142 2143 /* Determine the configuration header type */ 2144 header_type = pci_config_get8(cfg_hdl, PCI_CONF_HEADER); 2145 2146 if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE) { 2147 latency_timer = PCIEB_LATENCY_TIMER; 2148 pci_config_put8(cfg_hdl, PCI_BCNF_LATENCY_TIMER, 2149 latency_timer); 2150 } else { 2151 min_gnt = pci_config_get8(cfg_hdl, PCI_CONF_MIN_G); 2152 latency_timer = min_gnt * 8; 2153 } 2154 2155 pci_config_put8(cfg_hdl, PCI_CONF_LATENCY_TIMER, 2156 latency_timer); 2157 n = pci_config_get8(cfg_hdl, PCI_CONF_LATENCY_TIMER); 2158 if (n != 0) { 2159 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 2160 "latency-timer", n); 2161 } 2162 } 2163 } 2164