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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/types.h> 30 #include <sys/sunndi.h> 31 #include <sys/ddifm_impl.h> 32 #include <sys/fm/util.h> 33 #include <sys/fm/protocol.h> 34 #include <sys/fm/io/pci.h> 35 #include <sys/fm/io/ddi.h> 36 #include <sys/pci.h> 37 #include <sys/pci_impl.h> 38 #include <sys/epm.h> 39 40 41 int 42 pci_config_setup(dev_info_t *dip, ddi_acc_handle_t *handle) 43 { 44 caddr_t cfgaddr; 45 ddi_device_acc_attr_t attr; 46 47 attr.devacc_attr_version = DDI_DEVICE_ATTR_V0; 48 attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC; 49 attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC; 50 51 /* Check for fault management capabilities */ 52 if (DDI_FM_ACC_ERR_CAP(ddi_fm_capable(dip))) 53 attr.devacc_attr_access = DDI_FLAGERR_ACC; 54 55 return (ddi_regs_map_setup(dip, 0, &cfgaddr, 0, 0, &attr, handle)); 56 } 57 58 void 59 pci_config_teardown(ddi_acc_handle_t *handle) 60 { 61 ddi_regs_map_free(handle); 62 } 63 64 /* 65 * pci_ereport_setup, pci_ereport_teardown, pci_ereport_post: 66 * Interfaces to be used by ereport capable PCI device drivers to setup, 67 * teardown, and post generic PCI error reports. This is to guarantee a 68 * consistant error report model for all PCI devices. Please see 69 * PSARC/2004/391. 70 */ 71 72 typedef struct pci_erpt { 73 caddr_t pci_cfg_addr; /* Config space address */ 74 ddi_acc_handle_t pci_cfg_hdl; /* Config space access handle */ 75 } pci_erpt_t; 76 77 pci_fm_err_t pci_err_tbl[] = { 78 PCI_DET_PERR, PCI_STAT_PERROR, NULL, 79 PCI_MDPE, PCI_STAT_S_PERROR, PCI_TARG_MDPE, 80 PCI_SIG_SERR, PCI_STAT_S_SYSERR, NULL, 81 PCI_MA, PCI_STAT_R_MAST_AB, PCI_TARG_MA, 82 PCI_REC_TA, PCI_STAT_R_TARG_AB, PCI_TARG_REC_TA, 83 PCI_SIG_TA, PCI_STAT_S_TARG_AB, NULL, 84 NULL, NULL, 85 }; 86 87 pci_fm_err_t pci_bdg_err_tbl[] = { 88 PCI_DET_PERR, PCI_STAT_PERROR, NULL, 89 PCI_MDPE, PCI_STAT_S_PERROR, NULL, 90 PCI_REC_SERR, PCI_STAT_S_SYSERR, NULL, 91 PCI_MA, PCI_STAT_R_MAST_AB, NULL, 92 PCI_REC_TA, PCI_STAT_R_TARG_AB, NULL, 93 PCI_SIG_TA, PCI_STAT_S_TARG_AB, NULL, 94 NULL, NULL, 95 }; 96 void 97 pci_ereport_setup(dev_info_t *dip) 98 { 99 struct dev_info *devi = DEVI(dip); 100 struct i_ddi_fmhdl *fmhdl = devi->devi_fmhdl; 101 pci_erpt_t *erpt_p; 102 ddi_acc_hdl_t *hp; 103 104 if (!DDI_FM_EREPORT_CAP(ddi_fm_capable(dip))) { 105 i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL, DDI_SLEEP); 106 return; 107 } 108 109 ASSERT(fmhdl); 110 ASSERT(fmhdl->fh_bus_specific == NULL); 111 112 if ((erpt_p = kmem_zalloc(sizeof (pci_erpt_t), KM_SLEEP)) == NULL) 113 return; 114 115 if (pci_config_setup(dip, &erpt_p->pci_cfg_hdl) == DDI_SUCCESS) { 116 hp = impl_acc_hdl_get(erpt_p->pci_cfg_hdl); 117 erpt_p->pci_cfg_addr = (caddr_t)hp->ah_addr; 118 fmhdl->fh_bus_specific = (void *)erpt_p; 119 } 120 } 121 122 void 123 pci_ereport_teardown(dev_info_t *dip) 124 { 125 struct i_ddi_fmhdl *fmhdl = DEVI(dip)->devi_fmhdl; 126 pci_erpt_t *erpt_p; 127 128 if (!DDI_FM_EREPORT_CAP(ddi_fm_capable(dip))) { 129 i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL, DDI_SLEEP); 130 } 131 132 ASSERT(fmhdl); 133 134 erpt_p = (pci_erpt_t *)fmhdl->fh_bus_specific; 135 if (erpt_p == NULL) 136 return; 137 138 pci_config_teardown(&erpt_p->pci_cfg_hdl); 139 kmem_free(erpt_p, sizeof (pci_erpt_t)); 140 fmhdl->fh_bus_specific = NULL; 141 } 142 143 void 144 pci_ereport_post(dev_info_t *dip, ddi_fm_error_t *derr, uint16_t *status) 145 { 146 struct i_ddi_fmhdl *fmhdl; 147 pci_erpt_t *erpt_p; 148 char buf[FM_MAX_CLASS]; 149 uint16_t cfg_comm = 0xffff; 150 uint16_t cfg_stat = 0xffff; 151 int i; 152 fmhdl = DEVI(dip)->devi_fmhdl; 153 154 if (!DDI_FM_EREPORT_CAP(ddi_fm_capable(dip))) { 155 i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL, DDI_NOSLEEP); 156 return; 157 } 158 159 ASSERT(fmhdl); 160 161 derr->fme_ena = derr->fme_ena ? derr->fme_ena : fm_ena_generate(0, 162 FM_ENA_FMT1); 163 164 erpt_p = (pci_erpt_t *)fmhdl->fh_bus_specific; 165 if (erpt_p == NULL) { 166 i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL, DDI_NOSLEEP); 167 return; 168 } 169 170 if ((cfg_stat = ddi_get16(erpt_p->pci_cfg_hdl, 171 (uint16_t *)(erpt_p->pci_cfg_addr + PCI_CONF_STAT))) == 0xffff) { 172 (void) snprintf(buf, FM_MAX_CLASS, "%s.%s", 173 PCI_ERROR_SUBCLASS, PCI_NR); 174 ddi_fm_ereport_post(dip, buf, derr->fme_ena, DDI_NOSLEEP, 175 FM_VERSION, DATA_TYPE_UINT8, 0, NULL); 176 goto done; 177 } 178 if ((cfg_comm = ddi_get16(erpt_p->pci_cfg_hdl, 179 (uint16_t *)(erpt_p->pci_cfg_addr + PCI_CONF_COMM))) == 0xffff) { 180 (void) snprintf(buf, FM_MAX_CLASS, "%s.%s", 181 PCI_ERROR_SUBCLASS, PCI_NR); 182 ddi_fm_ereport_post(dip, buf, derr->fme_ena, DDI_NOSLEEP, 183 FM_VERSION, DATA_TYPE_UINT8, 0, NULL); 184 goto done; 185 } 186 187 if (derr->fme_flag == DDI_FM_ERR_UNEXPECTED) { 188 for (i = 0; pci_err_tbl[i].err_class != NULL; i++) { 189 if (cfg_stat & pci_err_tbl[i].reg_bit) { 190 191 /* 192 * Generate an ereport for this error bit. 193 */ 194 (void) snprintf(buf, FM_MAX_CLASS, "%s.%s", 195 PCI_ERROR_SUBCLASS, 196 pci_err_tbl[i].err_class); 197 ddi_fm_ereport_post(dip, buf, derr->fme_ena, 198 DDI_NOSLEEP, FM_VERSION, DATA_TYPE_UINT8, 0, 199 PCI_CONFIG_STATUS, DATA_TYPE_UINT16, 200 cfg_stat, PCI_CONFIG_COMMAND, 201 DATA_TYPE_UINT16, cfg_comm, NULL); 202 203 /* 204 * Generate a corresponding ereport on behalf 205 * of the target (the parent dip) of the 206 * transaction. 207 */ 208 if (pci_err_tbl[i].terr_class != NULL && 209 DDI_FM_EREPORT_CAP(ddi_fm_capable( 210 (dev_info_t *)DEVI(dip)->devi_parent))) { 211 (void) snprintf(buf, FM_MAX_CLASS, 212 "%s.%s", PCI_ERROR_SUBCLASS, 213 pci_err_tbl[i].terr_class); 214 ddi_fm_ereport_post((dev_info_t *) 215 DEVI(dip)->devi_parent, buf, 216 derr->fme_ena, DDI_NOSLEEP, 217 FM_VERSION, DATA_TYPE_UINT8, 0, 218 NULL); 219 } 220 } 221 } 222 } 223 224 /* 225 * Clear error bits 226 */ 227 ddi_put16(erpt_p->pci_cfg_hdl, 228 (uint16_t *)(erpt_p->pci_cfg_addr + PCI_CONF_STAT), 229 (uint16_t)cfg_stat); 230 done: 231 if (status != NULL) 232 *status = cfg_stat; 233 } 234 235 /* 236 * Generic pci-pci bridge error report function 237 */ 238 void 239 pci_bdg_ereport_post(dev_info_t *dip, ddi_fm_error_t *derr, uint16_t *status) 240 { 241 struct i_ddi_fmhdl *fmhdl; 242 pci_erpt_t *erpt_p; 243 char buf[FM_MAX_CLASS]; 244 uint16_t bdg_ctrl = 0xffff; 245 uint16_t cfg_sec_stat = 0xffff; 246 int i; 247 248 if (!DDI_FM_EREPORT_CAP(ddi_fm_capable(dip))) { 249 i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL, DDI_NOSLEEP); 250 return; 251 } 252 253 fmhdl = DEVI(dip)->devi_fmhdl; 254 255 ASSERT(fmhdl); 256 257 derr->fme_ena = derr->fme_ena ? derr->fme_ena : fm_ena_generate(0, 258 FM_ENA_FMT1); 259 260 erpt_p = (pci_erpt_t *)fmhdl->fh_bus_specific; 261 if (erpt_p == NULL) { 262 i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL, DDI_NOSLEEP); 263 return; 264 } 265 266 if ((cfg_sec_stat = ddi_get16(erpt_p->pci_cfg_hdl, 267 (uint16_t *)(erpt_p->pci_cfg_addr + PCI_BCNF_SEC_STATUS))) 268 == 0xffff) { 269 (void) snprintf(buf, FM_MAX_CLASS, "%s.%s", PCI_ERROR_SUBCLASS, 270 PCI_NR); 271 ddi_fm_ereport_post(dip, buf, derr->fme_ena, DDI_NOSLEEP, 272 FM_VERSION, DATA_TYPE_UINT8, 0, NULL); 273 goto done; 274 } 275 276 if ((bdg_ctrl = ddi_get16(erpt_p->pci_cfg_hdl, 277 (uint16_t *)(erpt_p->pci_cfg_addr + PCI_BCNF_BCNTRL))) == 0xffff) { 278 (void) snprintf(buf, FM_MAX_CLASS, "%s.%s", PCI_ERROR_SUBCLASS, 279 PCI_NR); 280 ddi_fm_ereport_post(dip, buf, derr->fme_ena, DDI_NOSLEEP, 281 FM_VERSION, DATA_TYPE_UINT8, 0, NULL); 282 goto done; 283 } 284 285 if (derr->fme_flag == DDI_FM_ERR_UNEXPECTED) { 286 if (bdg_ctrl & PCI_BCNF_BCNTRL_DTO_STAT) { 287 (void) snprintf(buf, FM_MAX_CLASS, "%s.%s", 288 PCI_ERROR_SUBCLASS, PCI_DTO); 289 ddi_fm_ereport_post(dip, buf, derr->fme_ena, 290 DDI_NOSLEEP, FM_VERSION, DATA_TYPE_UINT8, 0, 291 PCI_SEC_CONFIG_STATUS, DATA_TYPE_UINT16, 292 cfg_sec_stat, PCI_BCNTRL, DATA_TYPE_UINT16, 293 bdg_ctrl, NULL); 294 } 295 296 for (i = 0; pci_bdg_err_tbl[i].err_class != NULL; i++) { 297 if (cfg_sec_stat & pci_bdg_err_tbl[i].reg_bit) { 298 (void) snprintf(buf, FM_MAX_CLASS, "%s.%s-%s", 299 PCI_ERROR_SUBCLASS, PCI_SEC_ERROR_SUBCLASS, 300 pci_bdg_err_tbl[i].err_class); 301 ddi_fm_ereport_post(dip, buf, derr->fme_ena, 302 DDI_NOSLEEP, FM_VERSION, DATA_TYPE_UINT8, 0, 303 PCI_SEC_CONFIG_STATUS, DATA_TYPE_UINT16, 304 cfg_sec_stat, PCI_BCNTRL, DATA_TYPE_UINT16, 305 bdg_ctrl, NULL); 306 } 307 } 308 } 309 310 /* 311 * Clear error bits 312 */ 313 ddi_put16(erpt_p->pci_cfg_hdl, (uint16_t *) 314 (erpt_p->pci_cfg_addr + PCI_BCNF_SEC_STATUS), 315 (uint16_t)cfg_sec_stat); 316 ddi_put16(erpt_p->pci_cfg_hdl, (uint16_t *) 317 (erpt_p->pci_cfg_addr + PCI_BCNF_BCNTRL), 318 (uint16_t)bdg_ctrl); 319 320 done: 321 if (status != NULL) 322 *status = cfg_sec_stat; 323 } 324 325 /* 326 * Generic pci-pci bridge error analysis function 327 */ 328 int 329 pci_bdg_check_status(dev_info_t *dip, ddi_fm_error_t *derr, 330 uint16_t pci_cfg_stat, uint16_t pci_cfg_sec_stat) 331 { 332 int ret; 333 int fatal = 0; 334 int nonfatal = 0; 335 int unknown = 0; 336 337 if (derr->fme_flag == DDI_FM_ERR_POKE) { 338 /* 339 * special case for pokes - we only consider master abort 340 * and target abort as nonfatal. Sserr with no master abort is 341 * fatal, but master/target abort can come in on separate 342 * instance, so return unknown and parent will determine if 343 * nonfatal (if another child returned nonfatal - ie master 344 * or target abort) or fatal otherwise 345 */ 346 if (pci_cfg_sec_stat & (PCI_STAT_R_TARG_AB | 347 PCI_STAT_R_MAST_AB)) 348 nonfatal++; 349 if (pci_cfg_stat & PCI_STAT_S_SYSERR) 350 unknown++; 351 } else if (derr->fme_flag == DDI_FM_ERR_UNEXPECTED) { 352 /* 353 * Only sserr on primary bus is considered fatal. 354 * In all other conditions, the bridge has been able to notify 355 * the initiator of the error condition, so let the initiator 356 * (be it the host for PIO or the leaf device for DMA) handle it 357 */ 358 if (pci_cfg_stat & PCI_STAT_S_SYSERR) 359 fatal++; 360 if (pci_cfg_stat & (PCI_STAT_PERROR | 361 PCI_STAT_R_MAST_AB | PCI_STAT_S_PERROR | 362 PCI_STAT_R_TARG_AB | PCI_STAT_S_TARG_AB)) 363 nonfatal++; 364 if (pci_cfg_sec_stat & (PCI_STAT_R_TARG_AB | 365 PCI_STAT_S_SYSERR | PCI_STAT_R_MAST_AB | PCI_STAT_S_PERROR | 366 PCI_STAT_PERROR | PCI_STAT_S_TARG_AB)) 367 nonfatal++; 368 } 369 370 /* 371 * now check children below the bridge 372 */ 373 ret = ndi_fm_handler_dispatch(dip, NULL, derr); 374 if (ret == DDI_FM_FATAL) 375 fatal++; 376 else if (ret == DDI_FM_NONFATAL) 377 nonfatal++; 378 else if (ret == DDI_FM_UNKNOWN) 379 unknown++; 380 381 return (fatal ? DDI_FM_FATAL : (nonfatal ? DDI_FM_NONFATAL : 382 (unknown ? DDI_FM_UNKNOWN : DDI_FM_OK))); 383 } 384 385 #ifdef _LP64 386 uint8_t 387 pci_config_get8(ddi_acc_handle_t handle, off_t offset) 388 #else /* _ILP32 */ 389 uint8_t 390 pci_config_getb(ddi_acc_handle_t handle, off_t offset) 391 #endif 392 { 393 caddr_t cfgaddr; 394 ddi_acc_hdl_t *hp; 395 396 hp = impl_acc_hdl_get(handle); 397 cfgaddr = hp->ah_addr + offset; 398 return (ddi_get8(handle, (uint8_t *)cfgaddr)); 399 } 400 401 #ifdef _LP64 402 uint16_t 403 pci_config_get16(ddi_acc_handle_t handle, off_t offset) 404 #else /* _ILP32 */ 405 uint16_t 406 pci_config_getw(ddi_acc_handle_t handle, off_t offset) 407 #endif 408 { 409 caddr_t cfgaddr; 410 ddi_acc_hdl_t *hp; 411 412 hp = impl_acc_hdl_get(handle); 413 cfgaddr = hp->ah_addr + offset; 414 return (ddi_get16(handle, (uint16_t *)cfgaddr)); 415 } 416 417 #ifdef _LP64 418 uint32_t 419 pci_config_get32(ddi_acc_handle_t handle, off_t offset) 420 #else /* _ILP32 */ 421 uint32_t 422 pci_config_getl(ddi_acc_handle_t handle, off_t offset) 423 #endif 424 { 425 caddr_t cfgaddr; 426 ddi_acc_hdl_t *hp; 427 428 hp = impl_acc_hdl_get(handle); 429 cfgaddr = hp->ah_addr + offset; 430 return (ddi_get32(handle, (uint32_t *)cfgaddr)); 431 } 432 433 #ifdef _LP64 434 uint64_t 435 pci_config_get64(ddi_acc_handle_t handle, off_t offset) 436 #else /* _ILP32 */ 437 uint64_t 438 pci_config_getll(ddi_acc_handle_t handle, off_t offset) 439 #endif 440 { 441 caddr_t cfgaddr; 442 ddi_acc_hdl_t *hp; 443 444 hp = impl_acc_hdl_get(handle); 445 cfgaddr = hp->ah_addr + offset; 446 return (ddi_get64(handle, (uint64_t *)cfgaddr)); 447 } 448 449 #ifdef _LP64 450 void 451 pci_config_put8(ddi_acc_handle_t handle, off_t offset, uint8_t value) 452 #else /* _ILP32 */ 453 void 454 pci_config_putb(ddi_acc_handle_t handle, off_t offset, uint8_t value) 455 #endif 456 { 457 caddr_t cfgaddr; 458 ddi_acc_hdl_t *hp; 459 460 hp = impl_acc_hdl_get(handle); 461 cfgaddr = hp->ah_addr + offset; 462 ddi_put8(handle, (uint8_t *)cfgaddr, value); 463 } 464 465 #ifdef _LP64 466 void 467 pci_config_put16(ddi_acc_handle_t handle, off_t offset, uint16_t value) 468 #else /* _ILP32 */ 469 void 470 pci_config_putw(ddi_acc_handle_t handle, off_t offset, uint16_t value) 471 #endif 472 { 473 caddr_t cfgaddr; 474 ddi_acc_hdl_t *hp; 475 476 hp = impl_acc_hdl_get(handle); 477 cfgaddr = hp->ah_addr + offset; 478 ddi_put16(handle, (uint16_t *)cfgaddr, value); 479 } 480 481 #ifdef _LP64 482 void 483 pci_config_put32(ddi_acc_handle_t handle, off_t offset, uint32_t value) 484 #else /* _ILP32 */ 485 void 486 pci_config_putl(ddi_acc_handle_t handle, off_t offset, uint32_t value) 487 #endif 488 { 489 caddr_t cfgaddr; 490 ddi_acc_hdl_t *hp; 491 492 hp = impl_acc_hdl_get(handle); 493 cfgaddr = hp->ah_addr + offset; 494 ddi_put32(handle, (uint32_t *)cfgaddr, value); 495 } 496 497 #ifdef _LP64 498 void 499 pci_config_put64(ddi_acc_handle_t handle, off_t offset, uint64_t value) 500 #else /* _ILP32 */ 501 void 502 pci_config_putll(ddi_acc_handle_t handle, off_t offset, uint64_t value) 503 #endif 504 { 505 caddr_t cfgaddr; 506 ddi_acc_hdl_t *hp; 507 508 hp = impl_acc_hdl_get(handle); 509 cfgaddr = hp->ah_addr + offset; 510 ddi_put64(handle, (uint64_t *)cfgaddr, value); 511 } 512 513 /*ARGSUSED*/ 514 int 515 pci_report_pmcap(dev_info_t *dip, int cap, void *arg) 516 { 517 return (DDI_SUCCESS); 518 } 519 520 /* 521 * Note about saving and restoring config space. 522 * PCI devices have only upto 256 bytes of config space while PCI Express 523 * devices can have upto 4k config space. In case of PCI Express device, 524 * we save all 4k config space and restore it even if it doesn't make use 525 * of all 4k. But some devices don't respond to reads to non-existent 526 * registers within the config space. To avoid any panics, we use ddi_peek 527 * to do the reads. A bit mask is used to indicate which words of the 528 * config space are accessible. While restoring the config space, only those 529 * readable words are restored. We do all this in 32 bit size words. 530 */ 531 #define INDEX_SHIFT 3 532 #define BITMASK 0x7 533 534 static uint32_t pci_save_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf, 535 pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp); 536 static void pci_restore_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf, 537 pci_cap_save_desc_t *cap_descp, uint32_t elements); 538 static uint32_t pci_generic_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, 539 uint32_t *regbuf, uint32_t nwords); 540 static uint32_t pci_msi_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, 541 uint32_t *regbuf, uint32_t notused); 542 static uint32_t pci_pcix_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, 543 uint32_t *regbuf, uint32_t notused); 544 static uint32_t pci_pcie_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, 545 uint32_t *regbuf, uint32_t notused); 546 static void pci_fill_buf(ddi_acc_handle_t confhdl, uint16_t cap_ptr, 547 uint32_t *regbuf, uint32_t nwords); 548 static uint32_t cap_walk_and_save(ddi_acc_handle_t confhdl, uint32_t *regbuf, 549 pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp, int xspace); 550 static void pci_pmcap_check(ddi_acc_handle_t confhdl, uint32_t *regbuf, 551 uint16_t pmcap_offset); 552 553 /* 554 * Table below specifies the number of registers to be saved for each PCI 555 * capability. pci_generic_save saves the number of words specified in the 556 * table. Any special considerations will be taken care by the capability 557 * specific save function e.g. use pci_msi_save to save registers associated 558 * with MSI capability. PCI_UNKNOWN_SIZE indicates that number of registers 559 * to be saved is variable and will be determined by the specific save function. 560 * Currently we save/restore all the registers associated with the capability 561 * including read only registers. Regsiters are saved and restored in 32 bit 562 * size words. 563 */ 564 static pci_cap_entry_t pci_cap_table[] = { 565 {PCI_CAP_ID_PM, PCI_PMCAP_NDWORDS, pci_generic_save}, 566 {PCI_CAP_ID_AGP, PCI_AGP_NDWORDS, pci_generic_save}, 567 {PCI_CAP_ID_SLOT_ID, PCI_SLOTID_NDWORDS, pci_generic_save}, 568 {PCI_CAP_ID_MSI_X, PCI_MSIX_NDWORDS, pci_generic_save}, 569 {PCI_CAP_ID_MSI, PCI_CAP_SZUNKNOWN, pci_msi_save}, 570 {PCI_CAP_ID_PCIX, PCI_CAP_SZUNKNOWN, pci_pcix_save}, 571 {PCI_CAP_ID_PCI_E, PCI_CAP_SZUNKNOWN, pci_pcie_save}, 572 /* 573 * {PCI_CAP_ID_cPCI_CRC, 0, NULL}, 574 * {PCI_CAP_ID_VPD, 0, NULL}, 575 * {PCI_CAP_ID_cPCI_HS, 0, NULL}, 576 * {PCI_CAP_ID_PCI_HOTPLUG, 0, NULL}, 577 * {PCI_CAP_ID_AGP_8X, 0, NULL}, 578 * {PCI_CAP_ID_SECURE_DEV, 0, NULL}, 579 */ 580 {PCI_CAP_NEXT_PTR_NULL, 0, NULL} 581 }; 582 583 /* 584 * Save the configuration registers for cdip as a property 585 * so that it persists after detach/uninitchild. 586 */ 587 int 588 pci_save_config_regs(dev_info_t *dip) 589 { 590 ddi_acc_handle_t confhdl; 591 pci_config_header_state_t *chsp; 592 pci_cap_save_desc_t *pci_cap_descp; 593 int ret; 594 uint32_t i, ncaps, nwords; 595 uint32_t *regbuf, *p; 596 uint8_t *maskbuf; 597 size_t maskbufsz, regbufsz, capbufsz; 598 ddi_acc_hdl_t *hp; 599 off_t offset = 0; 600 uint8_t cap_ptr, cap_id; 601 int pcie = 0; 602 603 if (pci_config_setup(dip, &confhdl) != DDI_SUCCESS) { 604 cmn_err(CE_WARN, "%s%d can't get config handle", 605 ddi_driver_name(dip), ddi_get_instance(dip)); 606 607 return (DDI_FAILURE); 608 } 609 /* 610 * Determine if it is a pci express device. If it is, save entire 611 * 4k config space treating it as a array of 32 bit integers. 612 * If it is not, do it in a usual PCI way. 613 */ 614 cap_ptr = pci_config_get8(confhdl, PCI_BCNF_CAP_PTR); 615 /* 616 * Walk the capabilities searching for pci express capability 617 */ 618 while (cap_ptr != PCI_CAP_NEXT_PTR_NULL) { 619 cap_id = pci_config_get8(confhdl, 620 cap_ptr + PCI_CAP_ID); 621 if (cap_id == PCI_CAP_ID_PCI_E) { 622 pcie = 1; 623 break; 624 } 625 cap_ptr = pci_config_get8(confhdl, 626 cap_ptr + PCI_CAP_NEXT_PTR); 627 } 628 629 if (pcie) { 630 /* PCI express device. Can have data in all 4k space */ 631 regbuf = (uint32_t *)kmem_zalloc((size_t)PCIE_CONF_HDR_SIZE, 632 KM_SLEEP); 633 p = regbuf; 634 /* 635 * Allocate space for mask. 636 * mask size is 128 bytes (4096 / 4 / 8 ) 637 */ 638 maskbufsz = (size_t)((PCIE_CONF_HDR_SIZE/ sizeof (uint32_t)) >> 639 INDEX_SHIFT); 640 maskbuf = (uint8_t *)kmem_zalloc(maskbufsz, KM_SLEEP); 641 hp = impl_acc_hdl_get(confhdl); 642 for (i = 0; i < (PCIE_CONF_HDR_SIZE / sizeof (uint32_t)); i++) { 643 if (ddi_peek32(dip, (int32_t *)(hp->ah_addr + offset), 644 (int32_t *)p) == DDI_SUCCESS) { 645 /* it is readable register. set the bit */ 646 maskbuf[i >> INDEX_SHIFT] |= 647 (uint8_t)(1 << (i & BITMASK)); 648 } 649 p++; 650 offset += sizeof (uint32_t); 651 } 652 653 if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, 654 SAVED_CONFIG_REGS_MASK, (uchar_t *)maskbuf, 655 maskbufsz)) != DDI_PROP_SUCCESS) { 656 cmn_err(CE_WARN, "couldn't create %s property while" 657 "saving config space for %s@%d\n", 658 SAVED_CONFIG_REGS_MASK, ddi_driver_name(dip), 659 ddi_get_instance(dip)); 660 } else if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, 661 dip, SAVED_CONFIG_REGS, (uchar_t *)regbuf, 662 (size_t)PCIE_CONF_HDR_SIZE)) != DDI_PROP_SUCCESS) { 663 (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, 664 SAVED_CONFIG_REGS_MASK); 665 cmn_err(CE_WARN, "%s%d can't update prop %s", 666 ddi_driver_name(dip), ddi_get_instance(dip), 667 SAVED_CONFIG_REGS); 668 } 669 670 kmem_free(maskbuf, (size_t)maskbufsz); 671 kmem_free(regbuf, (size_t)PCIE_CONF_HDR_SIZE); 672 } else { 673 regbuf = (uint32_t *)kmem_zalloc((size_t)PCI_CONF_HDR_SIZE, 674 KM_SLEEP); 675 chsp = (pci_config_header_state_t *)regbuf; 676 677 chsp->chs_command = pci_config_get16(confhdl, PCI_CONF_COMM); 678 chsp->chs_header_type = pci_config_get8(confhdl, 679 PCI_CONF_HEADER); 680 if ((chsp->chs_header_type & PCI_HEADER_TYPE_M) == 681 PCI_HEADER_ONE) 682 chsp->chs_bridge_control = 683 pci_config_get16(confhdl, PCI_BCNF_BCNTRL); 684 chsp->chs_cache_line_size = pci_config_get8(confhdl, 685 PCI_CONF_CACHE_LINESZ); 686 chsp->chs_latency_timer = pci_config_get8(confhdl, 687 PCI_CONF_LATENCY_TIMER); 688 if ((chsp->chs_header_type & PCI_HEADER_TYPE_M) == 689 PCI_HEADER_ONE) { 690 chsp->chs_sec_latency_timer = 691 pci_config_get8(confhdl, PCI_BCNF_LATENCY_TIMER); 692 } 693 694 chsp->chs_base0 = pci_config_get32(confhdl, PCI_CONF_BASE0); 695 chsp->chs_base1 = pci_config_get32(confhdl, PCI_CONF_BASE1); 696 chsp->chs_base2 = pci_config_get32(confhdl, PCI_CONF_BASE2); 697 chsp->chs_base3 = pci_config_get32(confhdl, PCI_CONF_BASE3); 698 chsp->chs_base4 = pci_config_get32(confhdl, PCI_CONF_BASE4); 699 chsp->chs_base5 = pci_config_get32(confhdl, PCI_CONF_BASE5); 700 701 /* 702 * Allocate maximum space required for capability descriptions. 703 * The maximum number of capabilties saved is the number of 704 * capabilities listed in the pci_cap_table. 705 */ 706 ncaps = (sizeof (pci_cap_table) / sizeof (pci_cap_entry_t)); 707 capbufsz = ncaps * sizeof (pci_cap_save_desc_t); 708 pci_cap_descp = (pci_cap_save_desc_t *)kmem_zalloc( 709 capbufsz, KM_SLEEP); 710 p = (uint32_t *)((caddr_t)regbuf + 711 sizeof (pci_config_header_state_t)); 712 nwords = pci_save_caps(confhdl, p, pci_cap_descp, &ncaps); 713 regbufsz = sizeof (pci_config_header_state_t) + 714 nwords * sizeof (uint32_t); 715 716 if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, 717 SAVED_CONFIG_REGS, (uchar_t *)regbuf, regbufsz)) != 718 DDI_PROP_SUCCESS) { 719 cmn_err(CE_WARN, "%s%d can't update prop %s", 720 ddi_driver_name(dip), ddi_get_instance(dip), 721 SAVED_CONFIG_REGS); 722 } else if (ncaps) { 723 ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, 724 SAVED_CONFIG_REGS_CAPINFO, (uchar_t *)pci_cap_descp, 725 ncaps * sizeof (pci_cap_save_desc_t)); 726 if (ret != DDI_PROP_SUCCESS) 727 (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, 728 SAVED_CONFIG_REGS); 729 } 730 kmem_free(regbuf, (size_t)PCI_CONF_HDR_SIZE); 731 kmem_free(pci_cap_descp, capbufsz); 732 } 733 pci_config_teardown(&confhdl); 734 735 if (ret != DDI_PROP_SUCCESS) 736 return (DDI_FAILURE); 737 738 return (DDI_SUCCESS); 739 } 740 741 /* 742 * Saves registers associated with PCI capabilities. 743 * Returns number of 32 bit words saved. 744 * Number of capabilities saved is returned in ncapsp. 745 */ 746 static uint32_t 747 pci_save_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf, 748 pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp) 749 { 750 return (cap_walk_and_save(confhdl, regbuf, cap_descp, ncapsp, 0)); 751 } 752 753 static uint32_t 754 cap_walk_and_save(ddi_acc_handle_t confhdl, uint32_t *regbuf, 755 pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp, int xspace) 756 { 757 pci_cap_entry_t *pci_cap_entp; 758 uint16_t cap_id, offset; 759 uint32_t words_saved = 0, nwords = 0; 760 uint16_t cap_ptr = PCI_CAP_NEXT_PTR_NULL; 761 762 *ncapsp = 0; 763 if (!xspace) 764 cap_ptr = pci_config_get8(confhdl, PCI_BCNF_CAP_PTR); 765 /* 766 * Walk the capabilities 767 */ 768 while (cap_ptr != PCI_CAP_NEXT_PTR_NULL) { 769 cap_id = CAP_ID(confhdl, cap_ptr, xspace); 770 /* Search for this cap id in our table */ 771 if (!xspace) 772 pci_cap_entp = pci_cap_table; 773 while (pci_cap_entp->cap_id != PCI_CAP_NEXT_PTR_NULL && 774 pci_cap_entp->cap_id != cap_id) 775 pci_cap_entp++; 776 777 offset = cap_ptr; 778 cap_ptr = NEXT_CAP(confhdl, cap_ptr, xspace); 779 /* 780 * If this cap id is not found in the table, there is nothing 781 * to save. 782 */ 783 if (pci_cap_entp->cap_id == PCI_CAP_NEXT_PTR_NULL) 784 continue; 785 if (pci_cap_entp->cap_save_func) { 786 if ((nwords = pci_cap_entp->cap_save_func(confhdl, 787 offset, regbuf, pci_cap_entp->cap_ndwords))) { 788 cap_descp->cap_nregs = nwords; 789 cap_descp->cap_offset = offset; 790 cap_descp->cap_id = cap_id; 791 regbuf += nwords; 792 cap_descp++; 793 words_saved += nwords; 794 (*ncapsp)++; 795 } 796 } 797 798 } 799 return (words_saved); 800 } 801 802 static void 803 pci_fill_buf(ddi_acc_handle_t confhdl, uint16_t cap_ptr, 804 uint32_t *regbuf, uint32_t nwords) 805 { 806 int i; 807 808 for (i = 0; i < nwords; i++) { 809 *regbuf = pci_config_get32(confhdl, cap_ptr); 810 regbuf++; 811 cap_ptr += 4; 812 } 813 } 814 815 static uint32_t 816 pci_generic_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf, 817 uint32_t nwords) 818 { 819 pci_fill_buf(confhdl, cap_ptr, regbuf, nwords); 820 return (nwords); 821 } 822 823 /*ARGSUSED*/ 824 static uint32_t 825 pci_msi_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf, 826 uint32_t notused) 827 { 828 uint32_t nwords = PCI_MSI_MIN_WORDS; 829 uint16_t msi_ctrl; 830 831 /* Figure out how many registers to be saved */ 832 msi_ctrl = pci_config_get16(confhdl, cap_ptr + PCI_MSI_CTRL); 833 /* If 64 bit address capable add one word */ 834 if (msi_ctrl & PCI_MSI_64BIT_MASK) 835 nwords++; 836 /* If per vector masking capable, add two more words */ 837 if (msi_ctrl & PCI_MSI_PVM_MASK) 838 nwords += 2; 839 pci_fill_buf(confhdl, cap_ptr, regbuf, nwords); 840 841 return (nwords); 842 } 843 844 /*ARGSUSED*/ 845 static uint32_t 846 pci_pcix_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf, 847 uint32_t notused) 848 { 849 uint32_t nwords = PCI_PCIX_MIN_WORDS; 850 uint16_t pcix_command; 851 852 /* Figure out how many registers to be saved */ 853 pcix_command = pci_config_get16(confhdl, cap_ptr + PCI_PCIX_COMMAND); 854 /* If it is version 1 or version 2, add 4 words */ 855 if (((pcix_command & PCI_PCIX_VER_MASK) == PCI_PCIX_VER_1) || 856 ((pcix_command & PCI_PCIX_VER_MASK) == PCI_PCIX_VER_2)) 857 nwords += 4; 858 pci_fill_buf(confhdl, cap_ptr, regbuf, nwords); 859 860 return (nwords); 861 } 862 863 /*ARGSUSED*/ 864 static uint32_t 865 pci_pcie_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf, 866 uint32_t notused) 867 { 868 return (0); 869 } 870 871 static void 872 pci_pmcap_check(ddi_acc_handle_t confhdl, uint32_t *regbuf, 873 uint16_t pmcap_offset) 874 { 875 uint16_t pmcsr; 876 uint16_t pmcsr_offset = pmcap_offset + PCI_PMCSR; 877 uint32_t *saved_pmcsrp = (uint32_t *)((caddr_t)regbuf + PCI_PMCSR); 878 879 /* 880 * Copy the power state bits from the PMCSR to our saved copy. 881 * This is to make sure that we don't change the D state when 882 * we restore config space of the device. 883 */ 884 pmcsr = pci_config_get16(confhdl, pmcsr_offset); 885 (*saved_pmcsrp) &= ~PCI_PMCSR_STATE_MASK; 886 (*saved_pmcsrp) |= (pmcsr & PCI_PMCSR_STATE_MASK); 887 } 888 889 static void 890 pci_restore_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf, 891 pci_cap_save_desc_t *cap_descp, uint32_t elements) 892 { 893 int i, j; 894 uint16_t offset; 895 896 for (i = 0; i < (elements / sizeof (pci_cap_save_desc_t)); i++) { 897 offset = cap_descp->cap_offset; 898 if (cap_descp->cap_id == PCI_CAP_ID_PM) 899 pci_pmcap_check(confhdl, regbuf, offset); 900 for (j = 0; j < cap_descp->cap_nregs; j++) { 901 pci_config_put32(confhdl, offset, *regbuf); 902 regbuf++; 903 offset += 4; 904 } 905 cap_descp++; 906 } 907 } 908 909 /* 910 * Restore config_regs from a single devinfo node. 911 */ 912 int 913 pci_restore_config_regs(dev_info_t *dip) 914 { 915 ddi_acc_handle_t confhdl; 916 pci_config_header_state_t *chs_p; 917 pci_cap_save_desc_t *cap_descp; 918 uint32_t elements, i; 919 uint8_t *maskbuf; 920 uint32_t *regbuf, *p; 921 off_t offset = 0; 922 923 if (pci_config_setup(dip, &confhdl) != DDI_SUCCESS) { 924 cmn_err(CE_WARN, "%s%d can't get config handle", 925 ddi_driver_name(dip), ddi_get_instance(dip)); 926 return (DDI_FAILURE); 927 } 928 929 if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 930 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS_MASK, 931 (uchar_t **)&maskbuf, &elements) == DDI_PROP_SUCCESS) { 932 933 if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 934 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS, 935 (uchar_t **)®buf, &elements) != DDI_PROP_SUCCESS) { 936 goto restoreconfig_err; 937 } 938 ASSERT(elements == PCIE_CONF_HDR_SIZE); 939 /* pcie device and has 4k config space saved */ 940 p = regbuf; 941 for (i = 0; i < PCIE_CONF_HDR_SIZE / sizeof (uint32_t); i++) { 942 /* If the word is readable then restore it */ 943 if (maskbuf[i >> INDEX_SHIFT] & 944 (uint8_t)(1 << (i & BITMASK))) 945 pci_config_put32(confhdl, offset, *p); 946 p++; 947 offset += sizeof (uint32_t); 948 } 949 ddi_prop_free(regbuf); 950 ddi_prop_free(maskbuf); 951 if (ndi_prop_remove(DDI_DEV_T_NONE, dip, 952 SAVED_CONFIG_REGS_MASK) != DDI_PROP_SUCCESS) { 953 cmn_err(CE_WARN, "%s%d can't remove prop %s", 954 ddi_driver_name(dip), ddi_get_instance(dip), 955 SAVED_CONFIG_REGS_MASK); 956 } 957 } else { 958 if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 959 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS, 960 (uchar_t **)®buf, &elements) != DDI_PROP_SUCCESS) { 961 962 pci_config_teardown(&confhdl); 963 return (DDI_FAILURE); 964 } 965 966 chs_p = (pci_config_header_state_t *)regbuf; 967 pci_config_put16(confhdl, PCI_CONF_COMM, 968 chs_p->chs_command); 969 if ((chs_p->chs_header_type & PCI_HEADER_TYPE_M) == 970 PCI_HEADER_ONE) { 971 pci_config_put16(confhdl, PCI_BCNF_BCNTRL, 972 chs_p->chs_bridge_control); 973 } 974 pci_config_put8(confhdl, PCI_CONF_CACHE_LINESZ, 975 chs_p->chs_cache_line_size); 976 pci_config_put8(confhdl, PCI_CONF_LATENCY_TIMER, 977 chs_p->chs_latency_timer); 978 if ((chs_p->chs_header_type & PCI_HEADER_TYPE_M) == 979 PCI_HEADER_ONE) 980 pci_config_put8(confhdl, PCI_BCNF_LATENCY_TIMER, 981 chs_p->chs_sec_latency_timer); 982 983 pci_config_put32(confhdl, PCI_CONF_BASE0, chs_p->chs_base0); 984 pci_config_put32(confhdl, PCI_CONF_BASE1, chs_p->chs_base1); 985 pci_config_put32(confhdl, PCI_CONF_BASE2, chs_p->chs_base2); 986 pci_config_put32(confhdl, PCI_CONF_BASE3, chs_p->chs_base3); 987 pci_config_put32(confhdl, PCI_CONF_BASE4, chs_p->chs_base4); 988 pci_config_put32(confhdl, PCI_CONF_BASE5, chs_p->chs_base5); 989 990 if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 991 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, 992 SAVED_CONFIG_REGS_CAPINFO, 993 (uchar_t **)&cap_descp, &elements) == DDI_PROP_SUCCESS) { 994 /* 995 * PCI capability related regsiters are saved. 996 * Restore them based on the description. 997 */ 998 p = (uint32_t *)((caddr_t)regbuf + 999 sizeof (pci_config_header_state_t)); 1000 pci_restore_caps(confhdl, p, cap_descp, elements); 1001 ddi_prop_free(cap_descp); 1002 } 1003 1004 ddi_prop_free(regbuf); 1005 } 1006 1007 /* 1008 * Make sure registers are flushed 1009 */ 1010 (void) pci_config_get32(confhdl, PCI_CONF_BASE5); 1011 1012 1013 if (ndi_prop_remove(DDI_DEV_T_NONE, dip, SAVED_CONFIG_REGS) != 1014 DDI_PROP_SUCCESS) { 1015 cmn_err(CE_WARN, "%s%d can't remove prop %s", 1016 ddi_driver_name(dip), ddi_get_instance(dip), 1017 SAVED_CONFIG_REGS); 1018 } 1019 1020 pci_config_teardown(&confhdl); 1021 1022 return (DDI_SUCCESS); 1023 1024 restoreconfig_err: 1025 ddi_prop_free(maskbuf); 1026 if (ndi_prop_remove(DDI_DEV_T_NONE, dip, SAVED_CONFIG_REGS_MASK) != 1027 DDI_PROP_SUCCESS) { 1028 cmn_err(CE_WARN, "%s%d can't remove prop %s", 1029 ddi_driver_name(dip), ddi_get_instance(dip), 1030 SAVED_CONFIG_REGS_MASK); 1031 } 1032 pci_config_teardown(&confhdl); 1033 return (DDI_FAILURE); 1034 } 1035