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 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * hermon.c 29 * Hermon (InfiniBand) HCA Driver attach/detach Routines 30 * 31 * Implements all the routines necessary for the attach, setup, 32 * initialization (and subsequent possible teardown and detach) of the 33 * Hermon InfiniBand HCA driver. 34 */ 35 36 #include <sys/types.h> 37 #include <sys/file.h> 38 #include <sys/open.h> 39 #include <sys/conf.h> 40 #include <sys/ddi.h> 41 #include <sys/sunddi.h> 42 #include <sys/modctl.h> 43 #include <sys/stat.h> 44 #include <sys/pci.h> 45 #include <sys/pci_cap.h> 46 #include <sys/bitmap.h> 47 #include <sys/policy.h> 48 49 #include <sys/ib/adapters/hermon/hermon.h> 50 51 /* The following works around a problem in pre-2_7_000 firmware. */ 52 #define HERMON_FW_WORKAROUND 53 54 int hermon_verbose = 0; 55 56 /* Hermon HCA State Pointer */ 57 void *hermon_statep; 58 59 int debug_vpd = 0; 60 61 /* Disable the internal error-check polling thread */ 62 int hermon_no_inter_err_chk = 0; 63 64 /* 65 * The Hermon "userland resource database" is common to instances of the 66 * Hermon HCA driver. This structure "hermon_userland_rsrc_db" contains all 67 * the necessary information to maintain it. 68 */ 69 hermon_umap_db_t hermon_userland_rsrc_db; 70 71 static int hermon_attach(dev_info_t *, ddi_attach_cmd_t); 72 static int hermon_detach(dev_info_t *, ddi_detach_cmd_t); 73 static int hermon_open(dev_t *, int, int, cred_t *); 74 static int hermon_close(dev_t, int, int, cred_t *); 75 static int hermon_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 76 77 static int hermon_drv_init(hermon_state_t *state, dev_info_t *dip, 78 int instance); 79 static void hermon_drv_fini(hermon_state_t *state); 80 static void hermon_drv_fini2(hermon_state_t *state); 81 static int hermon_isr_init(hermon_state_t *state); 82 static void hermon_isr_fini(hermon_state_t *state); 83 84 static int hermon_hw_init(hermon_state_t *state); 85 86 static void hermon_hw_fini(hermon_state_t *state, 87 hermon_drv_cleanup_level_t cleanup); 88 static int hermon_soft_state_init(hermon_state_t *state); 89 static void hermon_soft_state_fini(hermon_state_t *state); 90 static int hermon_icm_config_setup(hermon_state_t *state, 91 hermon_hw_initqueryhca_t *inithca); 92 static void hermon_icm_tables_init(hermon_state_t *state); 93 static void hermon_icm_tables_fini(hermon_state_t *state); 94 static int hermon_icm_dma_init(hermon_state_t *state); 95 static void hermon_icm_dma_fini(hermon_state_t *state); 96 static void hermon_inithca_set(hermon_state_t *state, 97 hermon_hw_initqueryhca_t *inithca); 98 static int hermon_hca_port_init(hermon_state_t *state); 99 static int hermon_hca_ports_shutdown(hermon_state_t *state, uint_t num_init); 100 static int hermon_internal_uarpg_init(hermon_state_t *state); 101 static void hermon_internal_uarpg_fini(hermon_state_t *state); 102 static int hermon_special_qp_contexts_reserve(hermon_state_t *state); 103 static void hermon_special_qp_contexts_unreserve(hermon_state_t *state); 104 static int hermon_sw_reset(hermon_state_t *state); 105 static int hermon_mcg_init(hermon_state_t *state); 106 static void hermon_mcg_fini(hermon_state_t *state); 107 static int hermon_fw_version_check(hermon_state_t *state); 108 static void hermon_device_info_report(hermon_state_t *state); 109 static int hermon_pci_capability_list(hermon_state_t *state, 110 ddi_acc_handle_t hdl); 111 static void hermon_pci_capability_vpd(hermon_state_t *state, 112 ddi_acc_handle_t hdl, uint_t offset); 113 static int hermon_pci_read_vpd(ddi_acc_handle_t hdl, uint_t offset, 114 uint32_t addr, uint32_t *data); 115 static int hermon_intr_or_msi_init(hermon_state_t *state); 116 static int hermon_add_intrs(hermon_state_t *state, int intr_type); 117 static int hermon_intr_or_msi_fini(hermon_state_t *state); 118 void hermon_pci_capability_msix(hermon_state_t *state, ddi_acc_handle_t hdl, 119 uint_t offset); 120 121 static uint64_t hermon_size_icm(hermon_state_t *state); 122 123 /* X86 fastreboot support */ 124 static ushort_t get_msix_ctrl(dev_info_t *); 125 static size_t get_msix_tbl_size(dev_info_t *); 126 static size_t get_msix_pba_size(dev_info_t *); 127 static void hermon_set_msix_info(hermon_state_t *); 128 static int hermon_intr_disable(hermon_state_t *); 129 static int hermon_quiesce(dev_info_t *); 130 131 132 /* Character/Block Operations */ 133 static struct cb_ops hermon_cb_ops = { 134 hermon_open, /* open */ 135 hermon_close, /* close */ 136 nodev, /* strategy (block) */ 137 nodev, /* print (block) */ 138 nodev, /* dump (block) */ 139 nodev, /* read */ 140 nodev, /* write */ 141 hermon_ioctl, /* ioctl */ 142 hermon_devmap, /* devmap */ 143 NULL, /* mmap */ 144 nodev, /* segmap */ 145 nochpoll, /* chpoll */ 146 ddi_prop_op, /* prop_op */ 147 NULL, /* streams */ 148 D_NEW | D_MP | 149 D_64BIT | /* D_HOTPLUG | */ 150 D_DEVMAP, /* flags */ 151 CB_REV /* rev */ 152 }; 153 154 /* Driver Operations */ 155 static struct dev_ops hermon_ops = { 156 DEVO_REV, /* struct rev */ 157 0, /* refcnt */ 158 hermon_getinfo, /* getinfo */ 159 nulldev, /* identify */ 160 nulldev, /* probe */ 161 hermon_attach, /* attach */ 162 hermon_detach, /* detach */ 163 nodev, /* reset */ 164 &hermon_cb_ops, /* cb_ops */ 165 NULL, /* bus_ops */ 166 nodev, /* power */ 167 hermon_quiesce, /* devo_quiesce */ 168 }; 169 170 /* Module Driver Info */ 171 static struct modldrv hermon_modldrv = { 172 &mod_driverops, 173 "ConnectX IB Driver", 174 &hermon_ops 175 }; 176 177 /* Module Linkage */ 178 static struct modlinkage hermon_modlinkage = { 179 MODREV_1, 180 &hermon_modldrv, 181 NULL 182 }; 183 184 /* 185 * This extern refers to the ibc_operations_t function vector that is defined 186 * in the hermon_ci.c file. 187 */ 188 extern ibc_operations_t hermon_ibc_ops; 189 190 /* 191 * _init() 192 */ 193 int 194 _init() 195 { 196 int status; 197 198 status = ddi_soft_state_init(&hermon_statep, sizeof (hermon_state_t), 199 (size_t)HERMON_INITIAL_STATES); 200 if (status != 0) { 201 return (status); 202 } 203 204 status = ibc_init(&hermon_modlinkage); 205 if (status != 0) { 206 ddi_soft_state_fini(&hermon_statep); 207 return (status); 208 } 209 210 status = mod_install(&hermon_modlinkage); 211 if (status != 0) { 212 ibc_fini(&hermon_modlinkage); 213 ddi_soft_state_fini(&hermon_statep); 214 return (status); 215 } 216 217 /* Initialize the Hermon "userland resources database" */ 218 hermon_umap_db_init(); 219 220 return (status); 221 } 222 223 224 /* 225 * _info() 226 */ 227 int 228 _info(struct modinfo *modinfop) 229 { 230 int status; 231 232 status = mod_info(&hermon_modlinkage, modinfop); 233 return (status); 234 } 235 236 237 /* 238 * _fini() 239 */ 240 int 241 _fini() 242 { 243 int status; 244 245 status = mod_remove(&hermon_modlinkage); 246 if (status != 0) { 247 return (status); 248 } 249 250 /* Destroy the Hermon "userland resources database" */ 251 hermon_umap_db_fini(); 252 253 ibc_fini(&hermon_modlinkage); 254 ddi_soft_state_fini(&hermon_statep); 255 256 return (status); 257 } 258 259 260 /* 261 * hermon_getinfo() 262 */ 263 /* ARGSUSED */ 264 static int 265 hermon_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result) 266 { 267 dev_t dev; 268 hermon_state_t *state; 269 minor_t instance; 270 271 switch (cmd) { 272 case DDI_INFO_DEVT2DEVINFO: 273 dev = (dev_t)arg; 274 instance = HERMON_DEV_INSTANCE(dev); 275 state = ddi_get_soft_state(hermon_statep, instance); 276 if (state == NULL) { 277 return (DDI_FAILURE); 278 } 279 *result = (void *)state->hs_dip; 280 return (DDI_SUCCESS); 281 282 case DDI_INFO_DEVT2INSTANCE: 283 dev = (dev_t)arg; 284 instance = HERMON_DEV_INSTANCE(dev); 285 *result = (void *)(uintptr_t)instance; 286 return (DDI_SUCCESS); 287 288 default: 289 break; 290 } 291 292 return (DDI_FAILURE); 293 } 294 295 296 /* 297 * hermon_open() 298 */ 299 /* ARGSUSED */ 300 static int 301 hermon_open(dev_t *devp, int flag, int otyp, cred_t *credp) 302 { 303 hermon_state_t *state; 304 hermon_rsrc_t *rsrcp; 305 hermon_umap_db_entry_t *umapdb, *umapdb2; 306 minor_t instance; 307 uint64_t key, value; 308 uint_t hr_indx; 309 dev_t dev; 310 int status; 311 312 instance = HERMON_DEV_INSTANCE(*devp); 313 state = ddi_get_soft_state(hermon_statep, instance); 314 if (state == NULL) { 315 return (ENXIO); 316 } 317 318 /* 319 * Only allow driver to be opened for character access, and verify 320 * whether exclusive access is allowed. 321 */ 322 if ((otyp != OTYP_CHR) || ((flag & FEXCL) && 323 secpolicy_excl_open(credp) != 0)) { 324 return (EINVAL); 325 } 326 327 /* 328 * Search for the current process PID in the "userland resources 329 * database". If it is not found, then attempt to allocate a UAR 330 * page and add the ("key", "value") pair to the database. 331 * Note: As a last step we always return a devp appropriate for 332 * the open. Either we return a new minor number (based on the 333 * instance and the UAR page index) or we return the current minor 334 * number for the given client process. 335 * 336 * We also add an entry to the database to allow for lookup from 337 * "dev_t" to the current process PID. This is necessary because, 338 * under certain circumstance, the process PID that calls the Hermon 339 * close() entry point may not be the same as the one who called 340 * open(). Specifically, this can happen if a child process calls 341 * the Hermon's open() entry point, gets a UAR page, maps it out (using 342 * mmap()), and then exits without calling munmap(). Because mmap() 343 * adds a reference to the file descriptor, at the exit of the child 344 * process the file descriptor is "inherited" by the parent (and will 345 * be close()'d by the parent's PID only when it exits). 346 * 347 * Note: We use the hermon_umap_db_find_nolock() and 348 * hermon_umap_db_add_nolock() database access routines below (with 349 * an explicit mutex_enter of the database lock - "hdl_umapdb_lock") 350 * to ensure that the multiple accesses (in this case searching for, 351 * and then adding _two_ database entries) can be done atomically. 352 */ 353 key = ddi_get_pid(); 354 mutex_enter(&hermon_userland_rsrc_db.hdl_umapdb_lock); 355 status = hermon_umap_db_find_nolock(instance, key, 356 MLNX_UMAP_UARPG_RSRC, &value, 0, NULL); 357 if (status != DDI_SUCCESS) { 358 /* 359 * If we are in 'maintenance mode', we cannot alloc a UAR page. 360 * But we still need some rsrcp value, and a mostly unique 361 * hr_indx value. So we set rsrcp to NULL for maintenance 362 * mode, and use a rolling count for hr_indx. The field 363 * 'hs_open_hr_indx' is used only in this maintenance mode 364 * condition. 365 * 366 * Otherwise, if we are in operational mode then we allocate 367 * the UAR page as normal, and use the rsrcp value and tr_indx 368 * value from that allocation. 369 */ 370 if (!HERMON_IS_OPERATIONAL(state->hs_operational_mode)) { 371 rsrcp = NULL; 372 hr_indx = state->hs_open_ar_indx++; 373 } else { 374 /* Allocate a new UAR page for this process */ 375 status = hermon_rsrc_alloc(state, HERMON_UARPG, 1, 376 HERMON_NOSLEEP, &rsrcp); 377 if (status != DDI_SUCCESS) { 378 mutex_exit( 379 &hermon_userland_rsrc_db.hdl_umapdb_lock); 380 return (EAGAIN); 381 } 382 383 hr_indx = rsrcp->hr_indx; 384 } 385 386 /* 387 * Allocate an entry to track the UAR page resource in the 388 * "userland resources database". 389 */ 390 umapdb = hermon_umap_db_alloc(instance, key, 391 MLNX_UMAP_UARPG_RSRC, (uint64_t)(uintptr_t)rsrcp); 392 if (umapdb == NULL) { 393 mutex_exit(&hermon_userland_rsrc_db.hdl_umapdb_lock); 394 /* If in "maintenance mode", don't free the rsrc */ 395 if (HERMON_IS_OPERATIONAL(state->hs_operational_mode)) { 396 hermon_rsrc_free(state, &rsrcp); 397 } 398 return (EAGAIN); 399 } 400 401 /* 402 * Create a new device number. Minor number is a function of 403 * the UAR page index (15 bits) and the device instance number 404 * (3 bits). 405 */ 406 dev = makedevice(getmajor(*devp), (hr_indx << 407 HERMON_MINORNUM_SHIFT) | instance); 408 409 /* 410 * Allocate another entry in the "userland resources database" 411 * to track the association of the device number (above) to 412 * the current process ID (in "key"). 413 */ 414 umapdb2 = hermon_umap_db_alloc(instance, dev, 415 MLNX_UMAP_PID_RSRC, (uint64_t)key); 416 if (umapdb2 == NULL) { 417 mutex_exit(&hermon_userland_rsrc_db.hdl_umapdb_lock); 418 hermon_umap_db_free(umapdb); 419 /* If in "maintenance mode", don't free the rsrc */ 420 if (HERMON_IS_OPERATIONAL(state->hs_operational_mode)) { 421 hermon_rsrc_free(state, &rsrcp); 422 } 423 return (EAGAIN); 424 } 425 426 /* Add the entries to the database */ 427 hermon_umap_db_add_nolock(umapdb); 428 hermon_umap_db_add_nolock(umapdb2); 429 430 } else { 431 /* 432 * Return the same device number as on the original open() 433 * call. This was calculated as a function of the UAR page 434 * index (top 16 bits) and the device instance number 435 */ 436 rsrcp = (hermon_rsrc_t *)(uintptr_t)value; 437 dev = makedevice(getmajor(*devp), (rsrcp->hr_indx << 438 HERMON_MINORNUM_SHIFT) | instance); 439 } 440 mutex_exit(&hermon_userland_rsrc_db.hdl_umapdb_lock); 441 442 *devp = dev; 443 444 return (0); 445 } 446 447 448 /* 449 * hermon_close() 450 */ 451 /* ARGSUSED */ 452 static int 453 hermon_close(dev_t dev, int flag, int otyp, cred_t *credp) 454 { 455 hermon_state_t *state; 456 hermon_rsrc_t *rsrcp; 457 hermon_umap_db_entry_t *umapdb; 458 hermon_umap_db_priv_t *priv; 459 minor_t instance; 460 uint64_t key, value; 461 int status, reset_status = 0; 462 463 instance = HERMON_DEV_INSTANCE(dev); 464 state = ddi_get_soft_state(hermon_statep, instance); 465 if (state == NULL) { 466 return (ENXIO); 467 } 468 469 /* 470 * Search for "dev_t" in the "userland resources database". As 471 * explained above in hermon_open(), we can't depend on using the 472 * current process ID here to do the lookup because the process 473 * that ultimately closes may not be the same one who opened 474 * (because of inheritance). 475 * So we lookup the "dev_t" (which points to the PID of the process 476 * that opened), and we remove the entry from the database (and free 477 * it up). Then we do another query based on the PID value. And when 478 * we find that database entry, we free it up too and then free the 479 * Hermon UAR page resource. 480 * 481 * Note: We use the hermon_umap_db_find_nolock() database access 482 * routine below (with an explicit mutex_enter of the database lock) 483 * to ensure that the multiple accesses (which attempt to remove the 484 * two database entries) can be done atomically. 485 * 486 * This works the same in both maintenance mode and HCA mode, except 487 * for the call to hermon_rsrc_free(). In the case of maintenance mode, 488 * this call is not needed, as it was not allocated in hermon_open() 489 * above. 490 */ 491 key = dev; 492 mutex_enter(&hermon_userland_rsrc_db.hdl_umapdb_lock); 493 status = hermon_umap_db_find_nolock(instance, key, MLNX_UMAP_PID_RSRC, 494 &value, HERMON_UMAP_DB_REMOVE, &umapdb); 495 if (status == DDI_SUCCESS) { 496 /* 497 * If the "hdb_priv" field is non-NULL, it indicates that 498 * some "on close" handling is still necessary. Call 499 * hermon_umap_db_handle_onclose_cb() to do the handling (i.e. 500 * to invoke all the registered callbacks). Then free up 501 * the resources associated with "hdb_priv" and continue 502 * closing. 503 */ 504 priv = (hermon_umap_db_priv_t *)umapdb->hdbe_common.hdb_priv; 505 if (priv != NULL) { 506 reset_status = hermon_umap_db_handle_onclose_cb(priv); 507 kmem_free(priv, sizeof (hermon_umap_db_priv_t)); 508 umapdb->hdbe_common.hdb_priv = (void *)NULL; 509 } 510 511 hermon_umap_db_free(umapdb); 512 513 /* 514 * Now do another lookup using PID as the key (copy it from 515 * "value"). When this lookup is complete, the "value" field 516 * will contain the hermon_rsrc_t pointer for the UAR page 517 * resource. 518 */ 519 key = value; 520 status = hermon_umap_db_find_nolock(instance, key, 521 MLNX_UMAP_UARPG_RSRC, &value, HERMON_UMAP_DB_REMOVE, 522 &umapdb); 523 if (status == DDI_SUCCESS) { 524 hermon_umap_db_free(umapdb); 525 /* If in "maintenance mode", don't free the rsrc */ 526 if (HERMON_IS_OPERATIONAL(state->hs_operational_mode)) { 527 rsrcp = (hermon_rsrc_t *)(uintptr_t)value; 528 hermon_rsrc_free(state, &rsrcp); 529 } 530 } 531 } 532 mutex_exit(&hermon_userland_rsrc_db.hdl_umapdb_lock); 533 return (reset_status); 534 } 535 536 537 /* 538 * hermon_attach() 539 * Context: Only called from attach() path context 540 */ 541 static int 542 hermon_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 543 { 544 hermon_state_t *state; 545 ibc_clnt_hdl_t tmp_ibtfpriv; 546 ibc_status_t ibc_status; 547 int instance; 548 int status; 549 550 #ifdef __lock_lint 551 (void) hermon_quiesce(dip); 552 #endif 553 554 switch (cmd) { 555 case DDI_ATTACH: 556 instance = ddi_get_instance(dip); 557 status = ddi_soft_state_zalloc(hermon_statep, instance); 558 if (status != DDI_SUCCESS) { 559 cmn_err(CE_NOTE, "hermon%d: driver failed to attach: " 560 "attach_ssz_fail", instance); 561 goto fail_attach_nomsg; 562 563 } 564 state = ddi_get_soft_state(hermon_statep, instance); 565 if (state == NULL) { 566 ddi_soft_state_free(hermon_statep, instance); 567 cmn_err(CE_NOTE, "hermon%d: driver failed to attach: " 568 "attach_gss_fail", instance); 569 goto fail_attach_nomsg; 570 } 571 572 /* clear the attach error buffer */ 573 HERMON_ATTACH_MSG_INIT(state->hs_attach_buf); 574 575 /* Save away devinfo and instance before hermon_fm_init() */ 576 state->hs_dip = dip; 577 state->hs_instance = instance; 578 579 hermon_fm_init(state); 580 581 /* 582 * Initialize Hermon driver and hardware. 583 * 584 * Note: If this initialization fails we may still wish to 585 * create a device node and remain operational so that Hermon 586 * firmware can be updated/flashed (i.e. "maintenance mode"). 587 * If this is the case, then "hs_operational_mode" will be 588 * equal to HERMON_MAINTENANCE_MODE. We will not attempt to 589 * attach to the IBTF or register with the IBMF (i.e. no 590 * InfiniBand interfaces will be enabled). 591 */ 592 status = hermon_drv_init(state, dip, instance); 593 if ((status != DDI_SUCCESS) && 594 (HERMON_IS_OPERATIONAL(state->hs_operational_mode))) { 595 goto fail_attach; 596 } 597 598 /* 599 * Change the Hermon FM mode 600 */ 601 if ((hermon_get_state(state) & HCA_PIO_FM) && 602 HERMON_IS_OPERATIONAL(state->hs_operational_mode)) { 603 /* 604 * Now we wait for 50ms to give an opportunity 605 * to Solaris FMA so that HW errors can be notified. 606 * Then check if there are HW errors or not. If 607 * a HW error is detected, the Hermon attachment 608 * must be failed. 609 */ 610 delay(drv_usectohz(50000)); 611 if (hermon_init_failure(state)) { 612 hermon_drv_fini(state); 613 HERMON_WARNING(state, "unable to " 614 "attach Hermon due to a HW error"); 615 HERMON_ATTACH_MSG(state->hs_attach_buf, 616 "hermon_attach_failure"); 617 goto fail_attach; 618 } 619 620 /* 621 * There seems no HW errors during the attachment, 622 * so let's change the Hermon FM state to the 623 * ereport only mode. 624 */ 625 if (hermon_fm_ereport_init(state) != DDI_SUCCESS) { 626 /* unwind the resources */ 627 hermon_drv_fini(state); 628 HERMON_ATTACH_MSG(state->hs_attach_buf, 629 "hermon_attach_failure"); 630 goto fail_attach; 631 } 632 } 633 634 /* Create the minor node for device */ 635 status = ddi_create_minor_node(dip, "devctl", S_IFCHR, instance, 636 DDI_PSEUDO, 0); 637 if (status != DDI_SUCCESS) { 638 hermon_drv_fini(state); 639 HERMON_ATTACH_MSG(state->hs_attach_buf, 640 "attach_create_mn_fail"); 641 goto fail_attach; 642 } 643 644 /* 645 * If we are in "maintenance mode", then we don't want to 646 * register with the IBTF. All InfiniBand interfaces are 647 * uninitialized, and the device is only capable of handling 648 * requests to update/flash firmware (or test/debug requests). 649 */ 650 if (HERMON_IS_OPERATIONAL(state->hs_operational_mode)) { 651 cmn_err(CE_NOTE, "!Hermon is operational\n"); 652 653 /* Attach to InfiniBand Transport Framework (IBTF) */ 654 ibc_status = ibc_attach(&tmp_ibtfpriv, 655 &state->hs_ibtfinfo); 656 if (ibc_status != IBC_SUCCESS) { 657 cmn_err(CE_CONT, "hermon_attach: ibc_attach " 658 "failed\n"); 659 ddi_remove_minor_node(dip, "devctl"); 660 hermon_drv_fini(state); 661 HERMON_ATTACH_MSG(state->hs_attach_buf, 662 "attach_ibcattach_fail"); 663 goto fail_attach; 664 } 665 666 /* 667 * Now that we've successfully attached to the IBTF, 668 * we enable all appropriate asynch and CQ events to 669 * be forwarded to the IBTF. 670 */ 671 HERMON_ENABLE_IBTF_CALLB(state, tmp_ibtfpriv); 672 673 ibc_post_attach(state->hs_ibtfpriv); 674 675 /* Register agents with IB Mgmt Framework (IBMF) */ 676 status = hermon_agent_handlers_init(state); 677 if (status != DDI_SUCCESS) { 678 (void) ibc_pre_detach(tmp_ibtfpriv, DDI_DETACH); 679 HERMON_QUIESCE_IBTF_CALLB(state); 680 if (state->hs_in_evcallb != 0) { 681 HERMON_WARNING(state, "unable to " 682 "quiesce Hermon IBTF callbacks"); 683 } 684 ibc_detach(tmp_ibtfpriv); 685 ddi_remove_minor_node(dip, "devctl"); 686 hermon_drv_fini(state); 687 HERMON_ATTACH_MSG(state->hs_attach_buf, 688 "attach_agentinit_fail"); 689 goto fail_attach; 690 } 691 } 692 693 /* Report attach in maintenance mode, if appropriate */ 694 if (!(HERMON_IS_OPERATIONAL(state->hs_operational_mode))) { 695 cmn_err(CE_NOTE, "hermon%d: driver attached " 696 "(for maintenance mode only)", state->hs_instance); 697 hermon_fm_ereport(state, HCA_IBA_ERR, HCA_ERR_DEGRADED); 698 } 699 700 /* Report that driver was loaded */ 701 ddi_report_dev(dip); 702 703 /* Send device information to log file */ 704 hermon_device_info_report(state); 705 706 /* DEBUG PRINT */ 707 cmn_err(CE_CONT, "!Hermon attach complete\n"); 708 return (DDI_SUCCESS); 709 710 case DDI_RESUME: 711 /* Add code here for DDI_RESUME XXX */ 712 return (DDI_FAILURE); 713 714 default: 715 cmn_err(CE_WARN, "hermon_attach: unknown cmd (0x%x)\n", cmd); 716 break; 717 } 718 719 fail_attach: 720 cmn_err(CE_NOTE, "hermon%d: driver failed to attach: %s", instance, 721 state->hs_attach_buf); 722 if (hermon_get_state(state) & HCA_EREPORT_FM) { 723 hermon_fm_ereport(state, HCA_SYS_ERR, HCA_ERR_SRV_LOST); 724 } 725 hermon_drv_fini2(state); 726 hermon_fm_fini(state); 727 ddi_soft_state_free(hermon_statep, instance); 728 729 fail_attach_nomsg: 730 return (DDI_FAILURE); 731 } 732 733 734 /* 735 * hermon_detach() 736 * Context: Only called from detach() path context 737 */ 738 static int 739 hermon_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 740 { 741 hermon_state_t *state; 742 ibc_clnt_hdl_t tmp_ibtfpriv; 743 ibc_status_t ibc_status; 744 int instance, status; 745 746 instance = ddi_get_instance(dip); 747 state = ddi_get_soft_state(hermon_statep, instance); 748 if (state == NULL) { 749 return (DDI_FAILURE); 750 } 751 752 switch (cmd) { 753 case DDI_DETACH: 754 /* 755 * If we are in "maintenance mode", then we do not want to 756 * do teardown for any of the InfiniBand interfaces. 757 * Specifically, this means not detaching from IBTF (we never 758 * attached to begin with) and not deregistering from IBMF. 759 */ 760 if (HERMON_IS_OPERATIONAL(state->hs_operational_mode)) { 761 /* Unregister agents from IB Mgmt Framework (IBMF) */ 762 status = hermon_agent_handlers_fini(state); 763 if (status != DDI_SUCCESS) { 764 return (DDI_FAILURE); 765 } 766 767 /* 768 * Attempt the "pre-detach" from InfiniBand Transport 769 * Framework (IBTF). At this point the IBTF is still 770 * capable of handling incoming asynch and completion 771 * events. This "pre-detach" is primarily a mechanism 772 * to notify the appropriate IBTF clients that the 773 * HCA is being removed/offlined. 774 */ 775 ibc_status = ibc_pre_detach(state->hs_ibtfpriv, cmd); 776 if (ibc_status != IBC_SUCCESS) { 777 status = hermon_agent_handlers_init(state); 778 if (status != DDI_SUCCESS) { 779 HERMON_WARNING(state, "failed to " 780 "restart Hermon agents"); 781 } 782 return (DDI_FAILURE); 783 } 784 785 /* 786 * Before we can fully detach from the IBTF we need to 787 * ensure that we have handled all outstanding event 788 * callbacks. This is accomplished by quiescing the 789 * event callback mechanism. Note: if we are unable 790 * to successfully quiesce the callbacks, then this is 791 * an indication that something has probably gone 792 * seriously wrong. We print out a warning, but 793 * continue. 794 */ 795 tmp_ibtfpriv = state->hs_ibtfpriv; 796 HERMON_QUIESCE_IBTF_CALLB(state); 797 if (state->hs_in_evcallb != 0) { 798 HERMON_WARNING(state, "unable to quiesce " 799 "Hermon IBTF callbacks"); 800 } 801 802 /* Complete the detach from the IBTF */ 803 ibc_detach(tmp_ibtfpriv); 804 } 805 806 /* Remove the minor node for device */ 807 ddi_remove_minor_node(dip, "devctl"); 808 809 /* 810 * Only call hermon_drv_fini() if we are in Hermon HCA mode. 811 * (Because if we are in "maintenance mode", then we never 812 * successfully finished init.) Only report successful 813 * detach for normal HCA mode. 814 */ 815 if (HERMON_IS_OPERATIONAL(state->hs_operational_mode)) { 816 /* Cleanup driver resources and shutdown hardware */ 817 hermon_drv_fini(state); 818 cmn_err(CE_CONT, "!Hermon driver successfully " 819 "detached\n"); 820 } 821 822 hermon_drv_fini2(state); 823 hermon_fm_fini(state); 824 ddi_soft_state_free(hermon_statep, instance); 825 826 return (DDI_SUCCESS); 827 828 case DDI_SUSPEND: 829 /* Add code here for DDI_SUSPEND XXX */ 830 return (DDI_FAILURE); 831 832 default: 833 cmn_err(CE_WARN, "hermon_detach: unknown cmd (0x%x)\n", cmd); 834 break; 835 } 836 837 return (DDI_FAILURE); 838 } 839 840 /* 841 * hermon_dma_attr_init() 842 * Context: Can be called from interrupt or base context. 843 */ 844 845 /* ARGSUSED */ 846 void 847 hermon_dma_attr_init(hermon_state_t *state, ddi_dma_attr_t *dma_attr) 848 { 849 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*dma_attr)) 850 851 dma_attr->dma_attr_version = DMA_ATTR_V0; 852 dma_attr->dma_attr_addr_lo = 0; 853 dma_attr->dma_attr_addr_hi = 0xFFFFFFFFFFFFFFFFull; 854 dma_attr->dma_attr_count_max = 0xFFFFFFFFFFFFFFFFull; 855 dma_attr->dma_attr_align = HERMON_PAGESIZE; /* default 4K */ 856 dma_attr->dma_attr_burstsizes = 0x3FF; 857 dma_attr->dma_attr_minxfer = 1; 858 dma_attr->dma_attr_maxxfer = 0xFFFFFFFFFFFFFFFFull; 859 dma_attr->dma_attr_seg = 0xFFFFFFFFFFFFFFFFull; 860 dma_attr->dma_attr_sgllen = 0x7FFFFFFF; 861 dma_attr->dma_attr_granular = 1; 862 dma_attr->dma_attr_flags = 0; 863 } 864 865 /* 866 * hermon_dma_alloc() 867 * Context: Can be called from base context. 868 */ 869 int 870 hermon_dma_alloc(hermon_state_t *state, hermon_dma_info_t *dma_info, 871 uint16_t opcode) 872 { 873 ddi_dma_handle_t dma_hdl; 874 ddi_dma_attr_t dma_attr; 875 ddi_acc_handle_t acc_hdl; 876 ddi_dma_cookie_t cookie; 877 uint64_t kaddr; 878 uint64_t real_len; 879 uint_t ccount; 880 int status; 881 882 hermon_dma_attr_init(state, &dma_attr); 883 884 /* Allocate a DMA handle */ 885 status = ddi_dma_alloc_handle(state->hs_dip, &dma_attr, DDI_DMA_SLEEP, 886 NULL, &dma_hdl); 887 if (status != DDI_SUCCESS) { 888 IBTF_DPRINTF_L2("DMA", "alloc handle failed: %d", status); 889 cmn_err(CE_CONT, "DMA alloc handle failed(status %d)", status); 890 return (DDI_FAILURE); 891 } 892 893 /* Allocate DMA memory */ 894 status = ddi_dma_mem_alloc(dma_hdl, dma_info->length, 895 &state->hs_reg_accattr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL, 896 (caddr_t *)&kaddr, (size_t *)&real_len, &acc_hdl); 897 if (status != DDI_SUCCESS) { 898 ddi_dma_free_handle(&dma_hdl); 899 IBTF_DPRINTF_L2("DMA", "memory alloc failed: %d", status); 900 cmn_err(CE_CONT, "DMA memory alloc failed(status %d)", status); 901 return (DDI_FAILURE); 902 } 903 bzero((caddr_t)(uintptr_t)kaddr, real_len); 904 905 /* Bind the memory to the handle */ 906 status = ddi_dma_addr_bind_handle(dma_hdl, NULL, 907 (caddr_t)(uintptr_t)kaddr, (size_t)real_len, DDI_DMA_RDWR | 908 DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL, &cookie, &ccount); 909 if (status != DDI_SUCCESS) { 910 ddi_dma_mem_free(&acc_hdl); 911 ddi_dma_free_handle(&dma_hdl); 912 IBTF_DPRINTF_L2("DMA", "bind handle failed: %d", status); 913 cmn_err(CE_CONT, "DMA bind handle failed(status %d)", status); 914 return (DDI_FAILURE); 915 } 916 917 /* Package the hermon_dma_info contents and return */ 918 dma_info->vaddr = kaddr; 919 dma_info->dma_hdl = dma_hdl; 920 dma_info->acc_hdl = acc_hdl; 921 922 /* Pass the mapping information to the firmware */ 923 status = hermon_map_cmd_post(state, dma_info, opcode, cookie, ccount); 924 if (status != DDI_SUCCESS) { 925 char *s; 926 hermon_dma_free(dma_info); 927 switch (opcode) { 928 case MAP_ICM: 929 s = "MAP_ICM"; 930 break; 931 case MAP_FA: 932 s = "MAP_FA"; 933 break; 934 case MAP_ICM_AUX: 935 s = "MAP_ICM_AUX"; 936 break; 937 default: 938 s = "UNKNOWN"; 939 } 940 cmn_err(CE_NOTE, "Map cmd '%s' failed, status %08x\n", 941 s, status); 942 return (DDI_FAILURE); 943 } 944 945 return (DDI_SUCCESS); 946 } 947 948 /* 949 * hermon_dma_free() 950 * Context: Can be called from base context. 951 */ 952 void 953 hermon_dma_free(hermon_dma_info_t *info) 954 { 955 /* Unbind the handles and free the memory */ 956 (void) ddi_dma_unbind_handle(info->dma_hdl); 957 ddi_dma_mem_free(&info->acc_hdl); 958 ddi_dma_free_handle(&info->dma_hdl); 959 } 960 961 /* These macros are valid for use only in hermon_icm_alloc/hermon_icm_free. */ 962 #define HERMON_ICM_ALLOC(rsrc) \ 963 hermon_icm_alloc(state, rsrc, index1, index2) 964 #define HERMON_ICM_FREE(rsrc) \ 965 hermon_icm_free(state, rsrc, index1, index2) 966 967 /* 968 * hermon_icm_alloc() 969 * Context: Can be called from base context. 970 * 971 * Only one thread can be here for a given hermon_rsrc_type_t "type". 972 */ 973 int 974 hermon_icm_alloc(hermon_state_t *state, hermon_rsrc_type_t type, 975 uint32_t index1, uint32_t index2) 976 { 977 hermon_icm_table_t *icm; 978 hermon_dma_info_t *dma_info; 979 uint8_t *bitmap; 980 int status; 981 982 if (hermon_verbose) { 983 IBTF_DPRINTF_L2("hermon", "hermon_icm_alloc: rsrc_type (0x%x) " 984 "index1/2 (0x%x/0x%x)", type, index1, index2); 985 } 986 987 icm = &state->hs_icm[type]; 988 989 switch (type) { 990 case HERMON_QPC: 991 status = HERMON_ICM_ALLOC(HERMON_CMPT_QPC); 992 if (status != DDI_SUCCESS) { 993 return (status); 994 } 995 status = HERMON_ICM_ALLOC(HERMON_RDB); 996 if (status != DDI_SUCCESS) { /* undo icm_alloc's */ 997 HERMON_ICM_FREE(HERMON_CMPT_QPC); 998 return (status); 999 } 1000 status = HERMON_ICM_ALLOC(HERMON_ALTC); 1001 if (status != DDI_SUCCESS) { /* undo icm_alloc's */ 1002 HERMON_ICM_FREE(HERMON_RDB); 1003 HERMON_ICM_FREE(HERMON_CMPT_QPC); 1004 return (status); 1005 } 1006 status = HERMON_ICM_ALLOC(HERMON_AUXC); 1007 if (status != DDI_SUCCESS) { /* undo icm_alloc's */ 1008 HERMON_ICM_FREE(HERMON_ALTC); 1009 HERMON_ICM_FREE(HERMON_RDB); 1010 HERMON_ICM_FREE(HERMON_CMPT_QPC); 1011 return (status); 1012 } 1013 break; 1014 case HERMON_SRQC: 1015 status = HERMON_ICM_ALLOC(HERMON_CMPT_SRQC); 1016 if (status != DDI_SUCCESS) { 1017 return (status); 1018 } 1019 break; 1020 case HERMON_CQC: 1021 status = HERMON_ICM_ALLOC(HERMON_CMPT_CQC); 1022 if (status != DDI_SUCCESS) { 1023 return (status); 1024 } 1025 break; 1026 case HERMON_EQC: 1027 status = HERMON_ICM_ALLOC(HERMON_CMPT_EQC); 1028 if (status != DDI_SUCCESS) { /* undo icm_alloc's */ 1029 return (status); 1030 } 1031 break; 1032 } 1033 1034 /* ensure existence of bitmap and dmainfo, sets "dma_info" */ 1035 hermon_bitmap(bitmap, dma_info, icm, index1); 1036 1037 /* Set up the DMA handle for allocation and mapping */ 1038 dma_info = icm->icm_dma[index1] + index2; 1039 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*dma_info)) 1040 dma_info->length = icm->span << icm->log_object_size; 1041 dma_info->icmaddr = icm->icm_baseaddr + 1042 (((index1 << icm->split_shift) + 1043 (index2 << icm->span_shift)) << icm->log_object_size); 1044 1045 if (hermon_verbose) { 1046 IBTF_DPRINTF_L2("hermon", "alloc DMA: " 1047 "rsrc (0x%x) index (%x, %x) " 1048 "icm_addr/len (%llx/%x) bitmap %p", type, index1, index2, 1049 (longlong_t)dma_info->icmaddr, dma_info->length, bitmap); 1050 } 1051 1052 /* Allocate and map memory for this span */ 1053 status = hermon_dma_alloc(state, dma_info, MAP_ICM); 1054 if (status != DDI_SUCCESS) { 1055 IBTF_DPRINTF_L2("hermon", "hermon_icm_alloc: DMA " 1056 "allocation failed, status 0x%x", status); 1057 switch (type) { 1058 case HERMON_QPC: 1059 HERMON_ICM_FREE(HERMON_AUXC); 1060 HERMON_ICM_FREE(HERMON_ALTC); 1061 HERMON_ICM_FREE(HERMON_RDB); 1062 HERMON_ICM_FREE(HERMON_CMPT_QPC); 1063 break; 1064 case HERMON_SRQC: 1065 HERMON_ICM_FREE(HERMON_CMPT_SRQC); 1066 break; 1067 case HERMON_CQC: 1068 HERMON_ICM_FREE(HERMON_CMPT_CQC); 1069 break; 1070 case HERMON_EQC: 1071 HERMON_ICM_FREE(HERMON_CMPT_EQC); 1072 break; 1073 } 1074 1075 return (DDI_FAILURE); 1076 } 1077 if (hermon_verbose) { 1078 IBTF_DPRINTF_L2("hermon", "hermon_icm_alloc: mapping ICM: " 1079 "rsrc_type (0x%x) index (0x%x, 0x%x) alloc length (0x%x) " 1080 "icm_addr (0x%lx)", type, index1, index2, dma_info->length, 1081 dma_info->icmaddr); 1082 } 1083 1084 /* Set the bit for this slot in the table bitmap */ 1085 HERMON_BMAP_BIT_SET(icm->icm_bitmap[index1], index2); 1086 1087 return (DDI_SUCCESS); 1088 } 1089 1090 /* 1091 * hermon_icm_free() 1092 * Context: Can be called from base context. 1093 * 1094 * ICM resources have been successfully returned from hermon_icm_alloc(). 1095 * Associated dma_info is no longer in use. Free the ICM backing memory. 1096 */ 1097 void 1098 hermon_icm_free(hermon_state_t *state, hermon_rsrc_type_t type, 1099 uint32_t index1, uint32_t index2) 1100 { 1101 hermon_icm_table_t *icm; 1102 hermon_dma_info_t *dma_info; 1103 int status; 1104 1105 icm = &state->hs_icm[type]; 1106 ASSERT(icm->icm_dma[index1][index2].icm_refcnt == 0); 1107 1108 if (hermon_verbose) { 1109 IBTF_DPRINTF_L2("hermon", "hermon_icm_free: rsrc_type (0x%x) " 1110 "index (0x%x, 0x%x)", type, index1, index2); 1111 } 1112 1113 dma_info = icm->icm_dma[index1] + index2; 1114 1115 /* The following only happens if attach() is failing. */ 1116 if (dma_info == NULL) 1117 return; 1118 1119 /* Unmap the ICM allocation, then free the backing DMA memory */ 1120 status = hermon_unmap_icm_cmd_post(state, dma_info); 1121 if (status != DDI_SUCCESS) { 1122 HERMON_WARNING(state, "UNMAP_ICM failure"); 1123 } 1124 hermon_dma_free(dma_info); 1125 1126 /* Clear the bit in the ICM table bitmap */ 1127 HERMON_BMAP_BIT_CLR(icm->icm_bitmap[index1], index2); 1128 1129 switch (type) { 1130 case HERMON_QPC: 1131 HERMON_ICM_FREE(HERMON_AUXC); 1132 HERMON_ICM_FREE(HERMON_ALTC); 1133 HERMON_ICM_FREE(HERMON_RDB); 1134 HERMON_ICM_FREE(HERMON_CMPT_QPC); 1135 break; 1136 case HERMON_SRQC: 1137 HERMON_ICM_FREE(HERMON_CMPT_SRQC); 1138 break; 1139 case HERMON_CQC: 1140 HERMON_ICM_FREE(HERMON_CMPT_CQC); 1141 break; 1142 case HERMON_EQC: 1143 HERMON_ICM_FREE(HERMON_CMPT_EQC); 1144 break; 1145 1146 } 1147 } 1148 1149 /* 1150 * hermon_drv_init() 1151 * Context: Only called from attach() path context 1152 */ 1153 /* ARGSUSED */ 1154 static int 1155 hermon_drv_init(hermon_state_t *state, dev_info_t *dip, int instance) 1156 { 1157 int status; 1158 1159 /* 1160 * Check and set the operational mode of the device. If the driver is 1161 * bound to the Hermon device in "maintenance mode", then this generally 1162 * means that either the device has been specifically jumpered to 1163 * start in this mode or the firmware boot process has failed to 1164 * successfully load either the primary or the secondary firmware 1165 * image. 1166 */ 1167 if (HERMON_IS_HCA_MODE(state->hs_dip)) { 1168 state->hs_operational_mode = HERMON_HCA_MODE; 1169 state->hs_cfg_profile_setting = HERMON_CFG_MEMFREE; 1170 1171 } else if (HERMON_IS_MAINTENANCE_MODE(state->hs_dip)) { 1172 HERMON_FMANOTE(state, HERMON_FMA_MAINT); 1173 state->hs_operational_mode = HERMON_MAINTENANCE_MODE; 1174 return (DDI_FAILURE); 1175 1176 } else { 1177 state->hs_operational_mode = 0; /* invalid operational mode */ 1178 HERMON_FMANOTE(state, HERMON_FMA_PCIID); 1179 HERMON_WARNING(state, "unexpected device type detected"); 1180 return (DDI_FAILURE); 1181 } 1182 1183 /* 1184 * Initialize the Hermon hardware. 1185 * 1186 * Note: If this routine returns an error, it is often a reasonably 1187 * good indication that something Hermon firmware-related has caused 1188 * the failure or some HW related errors have caused the failure. 1189 * (also there are few possibilities that SW (e.g. SW resource 1190 * shortage) can cause the failure, but the majority case is due to 1191 * either a firmware related error or a HW related one) In order to 1192 * give the user an opportunity (if desired) to update or reflash 1193 * the Hermon firmware image, we set "hs_operational_mode" flag 1194 * (described above) to indicate that we wish to enter maintenance 1195 * mode in case of the firmware-related issue. 1196 */ 1197 status = hermon_hw_init(state); 1198 if (status != DDI_SUCCESS) { 1199 cmn_err(CE_NOTE, "hermon%d: error during attach: %s", instance, 1200 state->hs_attach_buf); 1201 return (DDI_FAILURE); 1202 } 1203 1204 /* 1205 * Now that the ISR has been setup, arm all the EQs for event 1206 * generation. 1207 */ 1208 1209 status = hermon_eq_arm_all(state); 1210 if (status != DDI_SUCCESS) { 1211 cmn_err(CE_NOTE, "EQ Arm All failed\n"); 1212 hermon_hw_fini(state, HERMON_DRV_CLEANUP_ALL); 1213 return (DDI_FAILURE); 1214 } 1215 1216 /* test interrupts and event queues */ 1217 status = hermon_nop_post(state, 0x0, 0x0); 1218 if (status != DDI_SUCCESS) { 1219 cmn_err(CE_NOTE, "Interrupts/EQs failed\n"); 1220 hermon_hw_fini(state, HERMON_DRV_CLEANUP_ALL); 1221 return (DDI_FAILURE); 1222 } 1223 1224 /* Initialize Hermon softstate */ 1225 status = hermon_soft_state_init(state); 1226 if (status != DDI_SUCCESS) { 1227 cmn_err(CE_NOTE, "Failed to init soft state\n"); 1228 hermon_hw_fini(state, HERMON_DRV_CLEANUP_ALL); 1229 return (DDI_FAILURE); 1230 } 1231 1232 return (DDI_SUCCESS); 1233 } 1234 1235 1236 /* 1237 * hermon_drv_fini() 1238 * Context: Only called from attach() and/or detach() path contexts 1239 */ 1240 static void 1241 hermon_drv_fini(hermon_state_t *state) 1242 { 1243 /* Cleanup Hermon softstate */ 1244 hermon_soft_state_fini(state); 1245 1246 /* Cleanup Hermon resources and shutdown hardware */ 1247 hermon_hw_fini(state, HERMON_DRV_CLEANUP_ALL); 1248 } 1249 1250 1251 /* 1252 * hermon_drv_fini2() 1253 * Context: Only called from attach() and/or detach() path contexts 1254 */ 1255 static void 1256 hermon_drv_fini2(hermon_state_t *state) 1257 { 1258 if (state->hs_fm_poll_thread) { 1259 ddi_periodic_delete(state->hs_fm_poll_thread); 1260 state->hs_fm_poll_thread = NULL; 1261 } 1262 1263 /* HERMON_DRV_CLEANUP_LEVEL1 */ 1264 if (state->hs_fm_cmdhdl) { 1265 hermon_regs_map_free(state, &state->hs_fm_cmdhdl); 1266 state->hs_fm_cmdhdl = NULL; 1267 } 1268 1269 if (state->hs_reg_cmdhdl) { 1270 ddi_regs_map_free(&state->hs_reg_cmdhdl); 1271 state->hs_reg_cmdhdl = NULL; 1272 } 1273 1274 /* HERMON_DRV_CLEANUP_LEVEL0 */ 1275 if (state->hs_msix_tbl_entries) { 1276 kmem_free(state->hs_msix_tbl_entries, 1277 state->hs_msix_tbl_size); 1278 state->hs_msix_tbl_entries = NULL; 1279 } 1280 1281 if (state->hs_msix_pba_entries) { 1282 kmem_free(state->hs_msix_pba_entries, 1283 state->hs_msix_pba_size); 1284 state->hs_msix_pba_entries = NULL; 1285 } 1286 1287 if (state->hs_fm_msix_tblhdl) { 1288 hermon_regs_map_free(state, &state->hs_fm_msix_tblhdl); 1289 state->hs_fm_msix_tblhdl = NULL; 1290 } 1291 1292 if (state->hs_reg_msix_tblhdl) { 1293 ddi_regs_map_free(&state->hs_reg_msix_tblhdl); 1294 state->hs_reg_msix_tblhdl = NULL; 1295 } 1296 1297 if (state->hs_fm_msix_pbahdl) { 1298 hermon_regs_map_free(state, &state->hs_fm_msix_pbahdl); 1299 state->hs_fm_msix_pbahdl = NULL; 1300 } 1301 1302 if (state->hs_reg_msix_pbahdl) { 1303 ddi_regs_map_free(&state->hs_reg_msix_pbahdl); 1304 state->hs_reg_msix_pbahdl = NULL; 1305 } 1306 1307 if (state->hs_fm_pcihdl) { 1308 hermon_pci_config_teardown(state, &state->hs_fm_pcihdl); 1309 state->hs_fm_pcihdl = NULL; 1310 } 1311 1312 if (state->hs_reg_pcihdl) { 1313 pci_config_teardown(&state->hs_reg_pcihdl); 1314 state->hs_reg_pcihdl = NULL; 1315 } 1316 } 1317 1318 1319 /* 1320 * hermon_isr_init() 1321 * Context: Only called from attach() path context 1322 */ 1323 static int 1324 hermon_isr_init(hermon_state_t *state) 1325 { 1326 int status; 1327 int intr; 1328 1329 for (intr = 0; intr < state->hs_intrmsi_allocd; intr++) { 1330 1331 /* 1332 * Add a handler for the interrupt or MSI 1333 */ 1334 status = ddi_intr_add_handler(state->hs_intrmsi_hdl[intr], 1335 hermon_isr, (caddr_t)state, (void *)(uintptr_t)intr); 1336 if (status != DDI_SUCCESS) { 1337 return (DDI_FAILURE); 1338 } 1339 1340 /* 1341 * Enable the software interrupt. Note: depending on the value 1342 * returned in the capability flag, we have to call either 1343 * ddi_intr_block_enable() or ddi_intr_enable(). 1344 */ 1345 if (state->hs_intrmsi_cap & DDI_INTR_FLAG_BLOCK) { 1346 status = ddi_intr_block_enable( 1347 &state->hs_intrmsi_hdl[intr], 1); 1348 if (status != DDI_SUCCESS) { 1349 return (DDI_FAILURE); 1350 } 1351 } else { 1352 status = ddi_intr_enable(state->hs_intrmsi_hdl[intr]); 1353 if (status != DDI_SUCCESS) { 1354 return (DDI_FAILURE); 1355 } 1356 } 1357 } 1358 1359 /* 1360 * Now that the ISR has been enabled, defer arm_all EQs for event 1361 * generation until later, in case MSIX is enabled 1362 */ 1363 return (DDI_SUCCESS); 1364 } 1365 1366 1367 /* 1368 * hermon_isr_fini() 1369 * Context: Only called from attach() and/or detach() path contexts 1370 */ 1371 static void 1372 hermon_isr_fini(hermon_state_t *state) 1373 { 1374 int intr; 1375 1376 for (intr = 0; intr < state->hs_intrmsi_allocd; intr++) { 1377 /* Disable the software interrupt */ 1378 if (state->hs_intrmsi_cap & DDI_INTR_FLAG_BLOCK) { 1379 (void) ddi_intr_block_disable( 1380 &state->hs_intrmsi_hdl[intr], 1); 1381 } else { 1382 (void) ddi_intr_disable(state->hs_intrmsi_hdl[intr]); 1383 } 1384 1385 /* 1386 * Remove the software handler for the interrupt or MSI 1387 */ 1388 (void) ddi_intr_remove_handler(state->hs_intrmsi_hdl[intr]); 1389 } 1390 } 1391 1392 1393 /* 1394 * Sum of ICM configured values: 1395 * cMPT, dMPT, MTT, QPC, SRQC, RDB, CQC, ALTC, AUXC, EQC, MCG 1396 * 1397 */ 1398 static uint64_t 1399 hermon_size_icm(hermon_state_t *state) 1400 { 1401 hermon_hw_querydevlim_t *devlim; 1402 hermon_cfg_profile_t *cfg; 1403 uint64_t num_cmpts, num_dmpts, num_mtts; 1404 uint64_t num_qpcs, num_srqc, num_rdbs; 1405 #ifndef HERMON_FW_WORKAROUND 1406 uint64_t num_auxc; 1407 #endif 1408 uint64_t num_cqcs, num_altc; 1409 uint64_t num_eqcs, num_mcgs; 1410 uint64_t size; 1411 1412 devlim = &state->hs_devlim; 1413 cfg = state->hs_cfg_profile; 1414 /* number of respective entries */ 1415 num_cmpts = (uint64_t)0x1 << cfg->cp_log_num_cmpt; 1416 num_mtts = (uint64_t)0x1 << cfg->cp_log_num_mtt; 1417 num_dmpts = (uint64_t)0x1 << cfg->cp_log_num_dmpt; 1418 num_qpcs = (uint64_t)0x1 << cfg->cp_log_num_qp; 1419 num_srqc = (uint64_t)0x1 << cfg->cp_log_num_srq; 1420 num_rdbs = (uint64_t)0x1 << cfg->cp_log_num_rdb; 1421 num_cqcs = (uint64_t)0x1 << cfg->cp_log_num_cq; 1422 num_altc = (uint64_t)0x1 << cfg->cp_log_num_qp; 1423 #ifndef HERMON_FW_WORKAROUND 1424 num_auxc = (uint64_t)0x1 << cfg->cp_log_num_qp; 1425 #endif 1426 num_eqcs = (uint64_t)0x1 << cfg->cp_log_num_eq; 1427 num_mcgs = (uint64_t)0x1 << cfg->cp_log_num_mcg; 1428 1429 size = 1430 num_cmpts * devlim->cmpt_entry_sz + 1431 num_dmpts * devlim->dmpt_entry_sz + 1432 num_mtts * devlim->mtt_entry_sz + 1433 num_qpcs * devlim->qpc_entry_sz + 1434 num_srqc * devlim->srq_entry_sz + 1435 num_rdbs * devlim->rdmardc_entry_sz + 1436 num_cqcs * devlim->cqc_entry_sz + 1437 num_altc * devlim->altc_entry_sz + 1438 #ifdef HERMON_FW_WORKAROUND 1439 0x80000000ull + 1440 #else 1441 num_auxc * devlim->aux_entry_sz + 1442 #endif 1443 num_eqcs * devlim->eqc_entry_sz + 1444 num_mcgs * HERMON_MCGMEM_SZ(state); 1445 return (size); 1446 } 1447 1448 1449 /* 1450 * hermon_hw_init() 1451 * Context: Only called from attach() path context 1452 */ 1453 static int 1454 hermon_hw_init(hermon_state_t *state) 1455 { 1456 hermon_drv_cleanup_level_t cleanup; 1457 sm_nodeinfo_t nodeinfo; 1458 uint64_t clr_intr_offset; 1459 int status; 1460 uint32_t fw_size; /* in page */ 1461 uint64_t offset; 1462 1463 /* This is where driver initialization begins */ 1464 cleanup = HERMON_DRV_CLEANUP_LEVEL0; 1465 1466 /* Setup device access attributes */ 1467 state->hs_reg_accattr.devacc_attr_version = DDI_DEVICE_ATTR_V0; 1468 state->hs_reg_accattr.devacc_attr_endian_flags = DDI_STRUCTURE_BE_ACC; 1469 state->hs_reg_accattr.devacc_attr_dataorder = DDI_STRICTORDER_ACC; 1470 state->hs_reg_accattr.devacc_attr_access = DDI_DEFAULT_ACC; 1471 1472 /* Setup fma-protected access attributes */ 1473 state->hs_fm_accattr.devacc_attr_version = 1474 hermon_devacc_attr_version(state); 1475 state->hs_fm_accattr.devacc_attr_endian_flags = DDI_STRUCTURE_BE_ACC; 1476 state->hs_fm_accattr.devacc_attr_dataorder = DDI_STRICTORDER_ACC; 1477 /* set acc err protection type */ 1478 state->hs_fm_accattr.devacc_attr_access = 1479 hermon_devacc_attr_access(state); 1480 1481 /* Setup for PCI config read/write of HCA device */ 1482 status = hermon_pci_config_setup(state, &state->hs_fm_pcihdl); 1483 if (status != DDI_SUCCESS) { 1484 hermon_hw_fini(state, cleanup); 1485 HERMON_ATTACH_MSG(state->hs_attach_buf, 1486 "hw_init_PCI_config_space_regmap_fail"); 1487 /* This case is not the degraded one */ 1488 return (DDI_FAILURE); 1489 } 1490 1491 /* Map PCI config space and MSI-X tables/pba */ 1492 hermon_set_msix_info(state); 1493 1494 /* Map in Hermon registers (CMD, UAR, MSIX) and setup offsets */ 1495 status = hermon_regs_map_setup(state, HERMON_CMD_BAR, 1496 &state->hs_reg_cmd_baseaddr, 0, 0, &state->hs_fm_accattr, 1497 &state->hs_fm_cmdhdl); 1498 if (status != DDI_SUCCESS) { 1499 hermon_hw_fini(state, cleanup); 1500 HERMON_ATTACH_MSG(state->hs_attach_buf, 1501 "hw_init_CMD_BAR_regmap_fail"); 1502 /* This case is not the degraded one */ 1503 return (DDI_FAILURE); 1504 } 1505 1506 cleanup = HERMON_DRV_CLEANUP_LEVEL1; 1507 /* 1508 * We defer UAR-BAR mapping until later. Need to know if 1509 * blueflame mapping is to be done, and don't know that until after 1510 * we get the dev_caps, so do it right after that 1511 */ 1512 1513 /* 1514 * There is a third BAR defined for Hermon - it is for MSIX 1515 * 1516 * Will need to explore it's possible need/use w/ Mellanox 1517 * [es] Temporary mapping maybe 1518 */ 1519 1520 #ifdef HERMON_SUPPORTS_MSIX_BAR 1521 status = ddi_regs_map_setup(state->hs_dip, HERMON_MSIX_BAR, 1522 &state->hs_reg_msi_baseaddr, 0, 0, &state->hs_reg_accattr, 1523 &state->hs_reg_msihdl); 1524 if (status != DDI_SUCCESS) { 1525 hermon_hw_fini(state, cleanup); 1526 HERMON_ATTACH_MSG(state->hs_attach_buf, 1527 "hw_init_MSIX_BAR_regmap_fail"); 1528 /* This case is not the degraded one */ 1529 return (DDI_FAILURE); 1530 } 1531 #endif 1532 1533 cleanup = HERMON_DRV_CLEANUP_LEVEL2; 1534 1535 /* 1536 * Save interesting registers away. The offsets of the first two 1537 * here (HCR and sw_reset) are detailed in the PRM, the others are 1538 * derived from values in the QUERY_FW output, so we'll save them 1539 * off later. 1540 */ 1541 /* Host Command Register (HCR) */ 1542 state->hs_cmd_regs.hcr = (hermon_hw_hcr_t *) 1543 ((uintptr_t)state->hs_reg_cmd_baseaddr + HERMON_CMD_HCR_OFFSET); 1544 state->hs_cmd_toggle = 0; /* initialize it for use */ 1545 1546 /* Software Reset register (sw_reset) and semaphore */ 1547 state->hs_cmd_regs.sw_reset = (uint32_t *) 1548 ((uintptr_t)state->hs_reg_cmd_baseaddr + 1549 HERMON_CMD_SW_RESET_OFFSET); 1550 state->hs_cmd_regs.sw_semaphore = (uint32_t *) 1551 ((uintptr_t)state->hs_reg_cmd_baseaddr + 1552 HERMON_CMD_SW_SEMAPHORE_OFFSET); 1553 1554 /* Retrieve PCI device, vendor and rev IDs */ 1555 state->hs_vendor_id = HERMON_GET_VENDOR_ID(state->hs_dip); 1556 state->hs_device_id = HERMON_GET_DEVICE_ID(state->hs_dip); 1557 state->hs_revision_id = HERMON_GET_REVISION_ID(state->hs_dip); 1558 1559 /* make sure init'd before we start filling things in */ 1560 bzero(&state->hs_hcaparams, sizeof (struct hermon_hw_initqueryhca_s)); 1561 1562 /* Initialize the Phase1 configuration profile */ 1563 status = hermon_cfg_profile_init_phase1(state); 1564 if (status != DDI_SUCCESS) { 1565 hermon_hw_fini(state, cleanup); 1566 HERMON_ATTACH_MSG(state->hs_attach_buf, 1567 "hw_init_cfginit1_fail"); 1568 /* This case is not the degraded one */ 1569 return (DDI_FAILURE); 1570 } 1571 cleanup = HERMON_DRV_CLEANUP_LEVEL3; 1572 1573 /* Do a software reset of the adapter to ensure proper state */ 1574 status = hermon_sw_reset(state); 1575 if (status != HERMON_CMD_SUCCESS) { 1576 hermon_hw_fini(state, cleanup); 1577 HERMON_ATTACH_MSG(state->hs_attach_buf, 1578 "hw_init_sw_reset_fail"); 1579 /* This case is not the degraded one */ 1580 return (DDI_FAILURE); 1581 } 1582 1583 /* Initialize mailboxes */ 1584 status = hermon_rsrc_init_phase1(state); 1585 if (status != DDI_SUCCESS) { 1586 hermon_hw_fini(state, cleanup); 1587 HERMON_ATTACH_MSG(state->hs_attach_buf, 1588 "hw_init_rsrcinit1_fail"); 1589 /* This case is not the degraded one */ 1590 return (DDI_FAILURE); 1591 } 1592 cleanup = HERMON_DRV_CLEANUP_LEVEL4; 1593 1594 /* Post QUERY_FW */ 1595 status = hermon_cmn_query_cmd_post(state, QUERY_FW, 0, 0, &state->hs_fw, 1596 sizeof (hermon_hw_queryfw_t), HERMON_CMD_NOSLEEP_SPIN); 1597 if (status != HERMON_CMD_SUCCESS) { 1598 cmn_err(CE_NOTE, "QUERY_FW command failed: %08x\n", status); 1599 hermon_hw_fini(state, cleanup); 1600 HERMON_ATTACH_MSG(state->hs_attach_buf, 1601 "hw_init_query_fw_cmd_fail"); 1602 /* This case is not the degraded one */ 1603 return (DDI_FAILURE); 1604 } 1605 1606 /* Validate what/that HERMON FW version is appropriate */ 1607 1608 status = hermon_fw_version_check(state); 1609 if (status != DDI_SUCCESS) { 1610 HERMON_FMANOTE(state, HERMON_FMA_FWVER); 1611 if (state->hs_operational_mode == HERMON_HCA_MODE) { 1612 cmn_err(CE_CONT, "Unsupported Hermon FW version: " 1613 "expected: %04d.%04d.%04d, " 1614 "actual: %04d.%04d.%04d\n", 1615 HERMON_FW_VER_MAJOR, 1616 HERMON_FW_VER_MINOR, 1617 HERMON_FW_VER_SUBMINOR, 1618 state->hs_fw.fw_rev_major, 1619 state->hs_fw.fw_rev_minor, 1620 state->hs_fw.fw_rev_subminor); 1621 } else { 1622 cmn_err(CE_CONT, "Unsupported FW version: " 1623 "%04d.%04d.%04d\n", 1624 state->hs_fw.fw_rev_major, 1625 state->hs_fw.fw_rev_minor, 1626 state->hs_fw.fw_rev_subminor); 1627 } 1628 state->hs_operational_mode = HERMON_MAINTENANCE_MODE; 1629 hermon_hw_fini(state, cleanup); 1630 HERMON_ATTACH_MSG(state->hs_attach_buf, 1631 "hw_init_checkfwver_fail"); 1632 /* This case is the degraded one */ 1633 return (HERMON_CMD_BAD_NVMEM); 1634 } 1635 1636 /* 1637 * Save off the rest of the interesting registers that we'll be using. 1638 * Setup the offsets for the other registers. 1639 */ 1640 1641 /* 1642 * Hermon does the intr_offset from the BAR - technically should get the 1643 * BAR info from the response, but PRM says it's from BAR0-1, which is 1644 * for us the CMD BAR 1645 */ 1646 1647 clr_intr_offset = state->hs_fw.clr_intr_offs & HERMON_CMD_OFFSET_MASK; 1648 1649 /* Save Clear Interrupt address */ 1650 state->hs_cmd_regs.clr_intr = (uint64_t *) 1651 (uintptr_t)(state->hs_reg_cmd_baseaddr + clr_intr_offset); 1652 1653 /* 1654 * Set the error buffer also into the structure - used in hermon_event.c 1655 * to check for internal error on the HCA, not reported in eqe or 1656 * (necessarily) by interrupt 1657 */ 1658 state->hs_cmd_regs.fw_err_buf = (uint32_t *)(uintptr_t) 1659 (state->hs_reg_cmd_baseaddr + state->hs_fw.error_buf_addr); 1660 1661 /* 1662 * Invoke a polling thread to check the error buffer periodically. 1663 */ 1664 if (!hermon_no_inter_err_chk) { 1665 state->hs_fm_poll_thread = ddi_periodic_add( 1666 hermon_inter_err_chk, (void *)state, FM_POLL_INTERVAL, 1667 DDI_IPL_0); 1668 } 1669 1670 cleanup = HERMON_DRV_CLEANUP_LEVEL5; 1671 1672 /* 1673 * Allocate, map, and run the HCA Firmware. 1674 */ 1675 1676 /* Allocate memory for the firmware to load into and map it */ 1677 1678 /* get next higher power of 2 */ 1679 fw_size = 1 << highbit(state->hs_fw.fw_pages); 1680 state->hs_fw_dma.length = fw_size << HERMON_PAGESHIFT; 1681 status = hermon_dma_alloc(state, &state->hs_fw_dma, MAP_FA); 1682 if (status != DDI_SUCCESS) { 1683 cmn_err(CE_NOTE, "FW alloc failed\n"); 1684 hermon_hw_fini(state, cleanup); 1685 HERMON_ATTACH_MSG(state->hs_attach_buf, 1686 "hw_init_dma_alloc_fw_fail"); 1687 /* This case is not the degraded one */ 1688 return (DDI_FAILURE); 1689 } 1690 1691 cleanup = HERMON_DRV_CLEANUP_LEVEL6; 1692 1693 /* Invoke the RUN_FW cmd to run the firmware */ 1694 status = hermon_run_fw_cmd_post(state); 1695 if (status != DDI_SUCCESS) { 1696 cmn_err(CE_NOTE, "RUN_FW command failed: 0x%08x\n", status); 1697 if (status == HERMON_CMD_BAD_NVMEM) { 1698 state->hs_operational_mode = HERMON_MAINTENANCE_MODE; 1699 } 1700 hermon_hw_fini(state, cleanup); 1701 HERMON_ATTACH_MSG(state->hs_attach_buf, "hw_init_run_fw_fail"); 1702 /* 1703 * If the status is HERMON_CMD_BAD_NVMEM, it's likely the 1704 * firmware is corrupted, so the mode falls into the 1705 * maintenance mode. 1706 */ 1707 return (status == HERMON_CMD_BAD_NVMEM ? HERMON_CMD_BAD_NVMEM : 1708 DDI_FAILURE); 1709 } 1710 1711 1712 /* 1713 * QUERY DEVICE LIMITS/CAPABILITIES 1714 * NOTE - in Hermon, the command is changed to QUERY_DEV_CAP, 1715 * but for familiarity we have kept the structure name the 1716 * same as Tavor/Arbel 1717 */ 1718 1719 status = hermon_cmn_query_cmd_post(state, QUERY_DEV_CAP, 0, 0, 1720 &state->hs_devlim, sizeof (hermon_hw_querydevlim_t), 1721 HERMON_CMD_NOSLEEP_SPIN); 1722 if (status != HERMON_CMD_SUCCESS) { 1723 cmn_err(CE_NOTE, "QUERY_DEV_CAP command failed: 0x%08x\n", 1724 status); 1725 hermon_hw_fini(state, cleanup); 1726 HERMON_ATTACH_MSG(state->hs_attach_buf, "hw_init_devcap_fail"); 1727 /* This case is not the degraded one */ 1728 return (DDI_FAILURE); 1729 } 1730 1731 state->hs_devlim.num_rsvd_eq = max(state->hs_devlim.num_rsvd_eq, 1732 (4 * state->hs_devlim.num_rsvd_uar)); /* lesser of resvd's */ 1733 1734 /* now we have enough info to map in the UAR BAR */ 1735 /* 1736 * First, we figure out how to map the BAR for UAR - use only half if 1737 * BlueFlame is enabled - in that case the mapped length is 1/2 the 1738 * log_max_uar_sz (max__uar - 1) * 1MB ( +20). 1739 */ 1740 1741 if (state->hs_devlim.blu_flm) { /* Blue Flame Enabled */ 1742 offset = (uint64_t)1 << (state->hs_devlim.log_max_uar_sz + 20); 1743 } else { 1744 offset = 0; /* a zero length means map the whole thing */ 1745 } 1746 status = hermon_regs_map_setup(state, HERMON_UAR_BAR, 1747 &state->hs_reg_uar_baseaddr, 0, offset, &state->hs_fm_accattr, 1748 &state->hs_fm_uarhdl); 1749 if (status != DDI_SUCCESS) { 1750 HERMON_ATTACH_MSG(state->hs_attach_buf, "UAR BAR mapping"); 1751 /* This case is not the degraded one */ 1752 return (DDI_FAILURE); 1753 } 1754 1755 /* and if BlueFlame is enabled, map the other half there */ 1756 if (state->hs_devlim.blu_flm) { /* Blue Flame Enabled */ 1757 offset = (uint64_t)1 << (state->hs_devlim.log_max_uar_sz + 20); 1758 status = ddi_regs_map_setup(state->hs_dip, HERMON_UAR_BAR, 1759 &state->hs_reg_bf_baseaddr, offset, offset, 1760 &state->hs_reg_accattr, &state->hs_reg_bfhdl); 1761 if (status != DDI_SUCCESS) { 1762 HERMON_ATTACH_MSG(state->hs_attach_buf, 1763 "BlueFlame BAR mapping"); 1764 /* This case is not the degraded one */ 1765 return (DDI_FAILURE); 1766 } 1767 /* This will be used in hw_fini if we fail to init. */ 1768 state->hs_bf_offset = offset; 1769 } 1770 cleanup = HERMON_DRV_CLEANUP_LEVEL7; 1771 1772 /* Hermon has a couple of things needed for phase 2 in query port */ 1773 1774 status = hermon_cmn_query_cmd_post(state, QUERY_PORT, 0, 0x01, 1775 &state->hs_queryport, sizeof (hermon_hw_query_port_t), 1776 HERMON_CMD_NOSLEEP_SPIN); 1777 if (status != HERMON_CMD_SUCCESS) { 1778 cmn_err(CE_NOTE, "QUERY_PORT command failed: 0x%08x\n", 1779 status); 1780 hermon_hw_fini(state, cleanup); 1781 HERMON_ATTACH_MSG(state->hs_attach_buf, 1782 "hw_init_queryport_fail"); 1783 /* This case is not the degraded one */ 1784 return (DDI_FAILURE); 1785 } 1786 1787 /* Initialize the Phase2 Hermon configuration profile */ 1788 status = hermon_cfg_profile_init_phase2(state); 1789 if (status != DDI_SUCCESS) { 1790 cmn_err(CE_NOTE, "CFG phase 2 failed: 0x%08x\n", status); 1791 hermon_hw_fini(state, cleanup); 1792 HERMON_ATTACH_MSG(state->hs_attach_buf, 1793 "hw_init_cfginit2_fail"); 1794 /* This case is not the degraded one */ 1795 return (DDI_FAILURE); 1796 } 1797 1798 /* Determine and set the ICM size */ 1799 state->hs_icm_sz = hermon_size_icm(state); 1800 status = hermon_set_icm_size_cmd_post(state); 1801 if (status != DDI_SUCCESS) { 1802 cmn_err(CE_NOTE, "Hermon: SET_ICM_SIZE cmd failed: 0x%08x\n", 1803 status); 1804 hermon_hw_fini(state, cleanup); 1805 HERMON_ATTACH_MSG(state->hs_attach_buf, 1806 "hw_init_seticmsz_fail"); 1807 /* This case is not the degraded one */ 1808 return (DDI_FAILURE); 1809 } 1810 /* alloc icm aux physical memory and map it */ 1811 1812 state->hs_icma_dma.length = 1 << highbit(state->hs_icma_sz); 1813 1814 status = hermon_dma_alloc(state, &state->hs_icma_dma, MAP_ICM_AUX); 1815 if (status != DDI_SUCCESS) { 1816 cmn_err(CE_NOTE, "failed to alloc (0x%llx) bytes for ICMA\n", 1817 (longlong_t)state->hs_icma_dma.length); 1818 hermon_hw_fini(state, cleanup); 1819 HERMON_ATTACH_MSG(state->hs_attach_buf, 1820 "hw_init_dma_alloc_icm_aux_fail"); 1821 /* This case is not the degraded one */ 1822 return (DDI_FAILURE); 1823 } 1824 cleanup = HERMON_DRV_CLEANUP_LEVEL8; 1825 1826 cleanup = HERMON_DRV_CLEANUP_LEVEL9; 1827 1828 /* Allocate an array of structures to house the ICM tables */ 1829 state->hs_icm = kmem_zalloc(HERMON_NUM_ICM_RESOURCES * 1830 sizeof (hermon_icm_table_t), KM_SLEEP); 1831 1832 /* Set up the ICM address space and the INIT_HCA command input */ 1833 status = hermon_icm_config_setup(state, &state->hs_hcaparams); 1834 if (status != HERMON_CMD_SUCCESS) { 1835 cmn_err(CE_NOTE, "ICM configuration failed\n"); 1836 hermon_hw_fini(state, cleanup); 1837 HERMON_ATTACH_MSG(state->hs_attach_buf, 1838 "hw_init_icm_config_setup_fail"); 1839 /* This case is not the degraded one */ 1840 return (DDI_FAILURE); 1841 } 1842 cleanup = HERMON_DRV_CLEANUP_LEVEL10; 1843 1844 /* Initialize the adapter with the INIT_HCA cmd */ 1845 status = hermon_init_hca_cmd_post(state, &state->hs_hcaparams, 1846 HERMON_CMD_NOSLEEP_SPIN); 1847 if (status != HERMON_CMD_SUCCESS) { 1848 cmn_err(CE_NOTE, "INIT_HCA command failed: %08x\n", status); 1849 hermon_hw_fini(state, cleanup); 1850 HERMON_ATTACH_MSG(state->hs_attach_buf, "hw_init_hca_fail"); 1851 /* This case is not the degraded one */ 1852 return (DDI_FAILURE); 1853 } 1854 cleanup = HERMON_DRV_CLEANUP_LEVEL11; 1855 1856 /* Enter the second phase of init for Hermon configuration/resources */ 1857 status = hermon_rsrc_init_phase2(state); 1858 if (status != DDI_SUCCESS) { 1859 hermon_hw_fini(state, cleanup); 1860 HERMON_ATTACH_MSG(state->hs_attach_buf, 1861 "hw_init_rsrcinit2_fail"); 1862 /* This case is not the degraded one */ 1863 return (DDI_FAILURE); 1864 } 1865 cleanup = HERMON_DRV_CLEANUP_LEVEL12; 1866 1867 /* Query the adapter via QUERY_ADAPTER */ 1868 status = hermon_cmn_query_cmd_post(state, QUERY_ADAPTER, 0, 0, 1869 &state->hs_adapter, sizeof (hermon_hw_queryadapter_t), 1870 HERMON_CMD_NOSLEEP_SPIN); 1871 if (status != HERMON_CMD_SUCCESS) { 1872 cmn_err(CE_NOTE, "Hermon: QUERY_ADAPTER command failed: %08x\n", 1873 status); 1874 hermon_hw_fini(state, cleanup); 1875 HERMON_ATTACH_MSG(state->hs_attach_buf, 1876 "hw_init_query_adapter_fail"); 1877 /* This case is not the degraded one */ 1878 return (DDI_FAILURE); 1879 } 1880 1881 /* Allocate protection domain (PD) for Hermon internal use */ 1882 status = hermon_pd_alloc(state, &state->hs_pdhdl_internal, 1883 HERMON_SLEEP); 1884 if (status != DDI_SUCCESS) { 1885 cmn_err(CE_NOTE, "failed to alloc internal PD\n"); 1886 hermon_hw_fini(state, cleanup); 1887 HERMON_ATTACH_MSG(state->hs_attach_buf, 1888 "hw_init_internal_pd_alloc_fail"); 1889 /* This case is not the degraded one */ 1890 return (DDI_FAILURE); 1891 } 1892 cleanup = HERMON_DRV_CLEANUP_LEVEL13; 1893 1894 /* Setup UAR page for kernel use */ 1895 status = hermon_internal_uarpg_init(state); 1896 if (status != DDI_SUCCESS) { 1897 cmn_err(CE_NOTE, "failed to setup internal UAR\n"); 1898 hermon_hw_fini(state, cleanup); 1899 HERMON_ATTACH_MSG(state->hs_attach_buf, 1900 "hw_init_internal_uarpg_alloc_fail"); 1901 /* This case is not the degraded one */ 1902 return (DDI_FAILURE); 1903 } 1904 cleanup = HERMON_DRV_CLEANUP_LEVEL14; 1905 1906 /* Query and initialize the Hermon interrupt/MSI information */ 1907 status = hermon_intr_or_msi_init(state); 1908 if (status != DDI_SUCCESS) { 1909 cmn_err(CE_NOTE, "failed to setup INTR/MSI\n"); 1910 hermon_hw_fini(state, cleanup); 1911 HERMON_ATTACH_MSG(state->hs_attach_buf, 1912 "hw_init_intr_or_msi_init_fail"); 1913 /* This case is not the degraded one */ 1914 return (DDI_FAILURE); 1915 } 1916 cleanup = HERMON_DRV_CLEANUP_LEVEL15; 1917 1918 status = hermon_isr_init(state); /* set up the isr */ 1919 if (status != DDI_SUCCESS) { 1920 cmn_err(CE_NOTE, "failed to init isr\n"); 1921 hermon_hw_fini(state, cleanup); 1922 HERMON_ATTACH_MSG(state->hs_attach_buf, 1923 "hw_init_isrinit_fail"); 1924 /* This case is not the degraded one */ 1925 return (DDI_FAILURE); 1926 } 1927 cleanup = HERMON_DRV_CLEANUP_LEVEL16; 1928 1929 /* Setup the event queues */ 1930 status = hermon_eq_init_all(state); 1931 if (status != DDI_SUCCESS) { 1932 cmn_err(CE_NOTE, "failed to init EQs\n"); 1933 hermon_hw_fini(state, cleanup); 1934 HERMON_ATTACH_MSG(state->hs_attach_buf, 1935 "hw_init_eqinitall_fail"); 1936 /* This case is not the degraded one */ 1937 return (DDI_FAILURE); 1938 } 1939 cleanup = HERMON_DRV_CLEANUP_LEVEL17; 1940 1941 1942 1943 /* Reserve contexts for QP0 and QP1 */ 1944 status = hermon_special_qp_contexts_reserve(state); 1945 if (status != DDI_SUCCESS) { 1946 cmn_err(CE_NOTE, "failed to init special QPs\n"); 1947 hermon_hw_fini(state, cleanup); 1948 HERMON_ATTACH_MSG(state->hs_attach_buf, 1949 "hw_init_rsrv_sqp_fail"); 1950 /* This case is not the degraded one */ 1951 return (DDI_FAILURE); 1952 } 1953 cleanup = HERMON_DRV_CLEANUP_LEVEL18; 1954 1955 /* Initialize for multicast group handling */ 1956 status = hermon_mcg_init(state); 1957 if (status != DDI_SUCCESS) { 1958 cmn_err(CE_NOTE, "failed to init multicast\n"); 1959 hermon_hw_fini(state, cleanup); 1960 HERMON_ATTACH_MSG(state->hs_attach_buf, 1961 "hw_init_mcg_init_fail"); 1962 /* This case is not the degraded one */ 1963 return (DDI_FAILURE); 1964 } 1965 cleanup = HERMON_DRV_CLEANUP_LEVEL19; 1966 1967 /* Initialize the Hermon IB port(s) */ 1968 status = hermon_hca_port_init(state); 1969 if (status != DDI_SUCCESS) { 1970 cmn_err(CE_NOTE, "failed to init HCA Port\n"); 1971 hermon_hw_fini(state, cleanup); 1972 HERMON_ATTACH_MSG(state->hs_attach_buf, 1973 "hw_init_hca_port_init_fail"); 1974 /* This case is not the degraded one */ 1975 return (DDI_FAILURE); 1976 } 1977 1978 cleanup = HERMON_DRV_CLEANUP_ALL; 1979 1980 /* Determine NodeGUID and SystemImageGUID */ 1981 status = hermon_getnodeinfo_cmd_post(state, HERMON_CMD_NOSLEEP_SPIN, 1982 &nodeinfo); 1983 if (status != HERMON_CMD_SUCCESS) { 1984 cmn_err(CE_NOTE, "GetNodeInfo command failed: %08x\n", status); 1985 hermon_hw_fini(state, cleanup); 1986 HERMON_ATTACH_MSG(state->hs_attach_buf, 1987 "hw_init_getnodeinfo_cmd_fail"); 1988 /* This case is not the degraded one */ 1989 return (DDI_FAILURE); 1990 } 1991 1992 /* 1993 * If the NodeGUID value was set in OBP properties, then we use that 1994 * value. But we still print a message if the value we queried from 1995 * firmware does not match this value. 1996 * 1997 * Otherwise if OBP value is not set then we use the value from 1998 * firmware unconditionally. 1999 */ 2000 if (state->hs_cfg_profile->cp_nodeguid) { 2001 state->hs_nodeguid = state->hs_cfg_profile->cp_nodeguid; 2002 } else { 2003 state->hs_nodeguid = nodeinfo.NodeGUID; 2004 } 2005 2006 if (state->hs_nodeguid != nodeinfo.NodeGUID) { 2007 cmn_err(CE_NOTE, "!NodeGUID value queried from firmware " 2008 "does not match value set by device property"); 2009 } 2010 2011 /* 2012 * If the SystemImageGUID value was set in OBP properties, then we use 2013 * that value. But we still print a message if the value we queried 2014 * from firmware does not match this value. 2015 * 2016 * Otherwise if OBP value is not set then we use the value from 2017 * firmware unconditionally. 2018 */ 2019 if (state->hs_cfg_profile->cp_sysimgguid) { 2020 state->hs_sysimgguid = state->hs_cfg_profile->cp_sysimgguid; 2021 } else { 2022 state->hs_sysimgguid = nodeinfo.SystemImageGUID; 2023 } 2024 2025 if (state->hs_sysimgguid != nodeinfo.SystemImageGUID) { 2026 cmn_err(CE_NOTE, "!SystemImageGUID value queried from firmware " 2027 "does not match value set by device property"); 2028 } 2029 2030 /* Get NodeDescription */ 2031 status = hermon_getnodedesc_cmd_post(state, HERMON_CMD_NOSLEEP_SPIN, 2032 (sm_nodedesc_t *)&state->hs_nodedesc); 2033 if (status != HERMON_CMD_SUCCESS) { 2034 cmn_err(CE_CONT, "GetNodeDesc command failed: %08x\n", status); 2035 hermon_hw_fini(state, cleanup); 2036 HERMON_ATTACH_MSG(state->hs_attach_buf, 2037 "hw_init_getnodedesc_cmd_fail"); 2038 /* This case is not the degraded one */ 2039 return (DDI_FAILURE); 2040 } 2041 2042 return (DDI_SUCCESS); 2043 } 2044 2045 2046 /* 2047 * hermon_hw_fini() 2048 * Context: Only called from attach() and/or detach() path contexts 2049 */ 2050 static void 2051 hermon_hw_fini(hermon_state_t *state, hermon_drv_cleanup_level_t cleanup) 2052 { 2053 uint_t num_ports; 2054 int i, status; 2055 2056 2057 /* 2058 * JBDB - We might not want to run these returns in all cases of 2059 * Bad News. We should still attempt to free all of the DMA memory 2060 * resources... This needs to be worked last, after all allocations 2061 * are implemented. For now, and possibly for later, this works. 2062 */ 2063 2064 switch (cleanup) { 2065 /* 2066 * If we add more driver initialization steps that should be cleaned 2067 * up here, we need to ensure that HERMON_DRV_CLEANUP_ALL is still the 2068 * first entry (i.e. corresponds to the last init step). 2069 */ 2070 case HERMON_DRV_CLEANUP_ALL: 2071 /* Shutdown the Hermon IB port(s) */ 2072 num_ports = state->hs_cfg_profile->cp_num_ports; 2073 (void) hermon_hca_ports_shutdown(state, num_ports); 2074 /* FALLTHROUGH */ 2075 2076 case HERMON_DRV_CLEANUP_LEVEL19: 2077 /* Teardown resources used for multicast group handling */ 2078 hermon_mcg_fini(state); 2079 /* FALLTHROUGH */ 2080 2081 case HERMON_DRV_CLEANUP_LEVEL18: 2082 /* Unreserve the special QP contexts */ 2083 hermon_special_qp_contexts_unreserve(state); 2084 /* FALLTHROUGH */ 2085 2086 case HERMON_DRV_CLEANUP_LEVEL17: 2087 /* 2088 * Attempt to teardown all event queues (EQ). If we fail 2089 * here then print a warning message and return. Something 2090 * (either in HW or SW) has gone seriously wrong. 2091 */ 2092 status = hermon_eq_fini_all(state); 2093 if (status != DDI_SUCCESS) { 2094 HERMON_WARNING(state, "failed to teardown EQs"); 2095 return; 2096 } 2097 /* FALLTHROUGH */ 2098 case HERMON_DRV_CLEANUP_LEVEL16: 2099 /* Teardown Hermon interrupts */ 2100 hermon_isr_fini(state); 2101 /* FALLTHROUGH */ 2102 2103 case HERMON_DRV_CLEANUP_LEVEL15: 2104 status = hermon_intr_or_msi_fini(state); 2105 if (status != DDI_SUCCESS) { 2106 HERMON_WARNING(state, "failed to free intr/MSI"); 2107 return; 2108 } 2109 /* FALLTHROUGH */ 2110 2111 case HERMON_DRV_CLEANUP_LEVEL14: 2112 /* Free the resources for the Hermon internal UAR pages */ 2113 hermon_internal_uarpg_fini(state); 2114 /* FALLTHROUGH */ 2115 2116 case HERMON_DRV_CLEANUP_LEVEL13: 2117 /* 2118 * Free the PD that was used internally by Hermon software. If 2119 * we fail here then print a warning and return. Something 2120 * (probably software-related, but perhaps HW) has gone wrong. 2121 */ 2122 status = hermon_pd_free(state, &state->hs_pdhdl_internal); 2123 if (status != DDI_SUCCESS) { 2124 HERMON_WARNING(state, "failed to free internal PD"); 2125 return; 2126 } 2127 /* FALLTHROUGH */ 2128 2129 case HERMON_DRV_CLEANUP_LEVEL12: 2130 /* Cleanup all the phase2 resources first */ 2131 hermon_rsrc_fini(state, HERMON_RSRC_CLEANUP_ALL); 2132 /* FALLTHROUGH */ 2133 2134 case HERMON_DRV_CLEANUP_LEVEL11: 2135 /* LEVEL11 is after INIT_HCA */ 2136 /* FALLTHROUGH */ 2137 2138 2139 case HERMON_DRV_CLEANUP_LEVEL10: 2140 /* 2141 * Unmap the ICM memory area with UNMAP_ICM command. 2142 */ 2143 status = hermon_unmap_icm_cmd_post(state, NULL); 2144 if (status != DDI_SUCCESS) { 2145 cmn_err(CE_WARN, 2146 "hermon_hw_fini: failed to unmap ICM\n"); 2147 } 2148 2149 /* Free the initial ICM DMA handles */ 2150 hermon_icm_dma_fini(state); 2151 2152 /* Free the ICM table structures */ 2153 hermon_icm_tables_fini(state); 2154 2155 /* Free the ICM table handles */ 2156 kmem_free(state->hs_icm, HERMON_NUM_ICM_RESOURCES * 2157 sizeof (hermon_icm_table_t)); 2158 2159 /* FALLTHROUGH */ 2160 2161 case HERMON_DRV_CLEANUP_LEVEL9: 2162 /* 2163 * Unmap the ICM Aux memory area with UNMAP_ICM_AUX command. 2164 */ 2165 status = hermon_unmap_icm_aux_cmd_post(state); 2166 if (status != HERMON_CMD_SUCCESS) { 2167 cmn_err(CE_NOTE, 2168 "hermon_hw_fini: failed to unmap ICMA\n"); 2169 } 2170 /* FALLTHROUGH */ 2171 2172 case HERMON_DRV_CLEANUP_LEVEL8: 2173 /* 2174 * Deallocate ICM Aux DMA memory. 2175 */ 2176 hermon_dma_free(&state->hs_icma_dma); 2177 /* FALLTHROUGH */ 2178 2179 case HERMON_DRV_CLEANUP_LEVEL7: 2180 if (state->hs_fm_uarhdl) { 2181 hermon_regs_map_free(state, &state->hs_fm_uarhdl); 2182 state->hs_fm_uarhdl = NULL; 2183 } 2184 2185 if (state->hs_reg_uarhdl) { 2186 ddi_regs_map_free(&state->hs_reg_uarhdl); 2187 state->hs_reg_uarhdl = NULL; 2188 } 2189 2190 if (state->hs_bf_offset != 0 && state->hs_reg_bfhdl) { 2191 ddi_regs_map_free(&state->hs_reg_bfhdl); 2192 state->hs_reg_bfhdl = NULL; 2193 } 2194 2195 for (i = 0; i < HERMON_MAX_PORTS; i++) { 2196 if (state->hs_pkey[i]) { 2197 kmem_free(state->hs_pkey[i], (1 << 2198 state->hs_cfg_profile->cp_log_max_pkeytbl) * 2199 sizeof (ib_pkey_t)); 2200 state->hs_pkey[i] = NULL; 2201 } 2202 if (state->hs_guid[i]) { 2203 kmem_free(state->hs_guid[i], (1 << 2204 state->hs_cfg_profile->cp_log_max_gidtbl) * 2205 sizeof (ib_guid_t)); 2206 state->hs_guid[i] = NULL; 2207 } 2208 } 2209 /* FALLTHROUGH */ 2210 2211 case HERMON_DRV_CLEANUP_LEVEL6: 2212 /* 2213 * Unmap the firmware memory area with UNMAP_FA command. 2214 */ 2215 status = hermon_unmap_fa_cmd_post(state); 2216 2217 if (status != HERMON_CMD_SUCCESS) { 2218 cmn_err(CE_NOTE, 2219 "hermon_hw_fini: failed to unmap FW\n"); 2220 } 2221 2222 /* 2223 * Deallocate firmware DMA memory. 2224 */ 2225 hermon_dma_free(&state->hs_fw_dma); 2226 /* FALLTHROUGH */ 2227 2228 case HERMON_DRV_CLEANUP_LEVEL5: 2229 /* stop the poll thread */ 2230 if (state->hs_fm_poll_thread) { 2231 ddi_periodic_delete(state->hs_fm_poll_thread); 2232 state->hs_fm_poll_thread = NULL; 2233 } 2234 /* FALLTHROUGH */ 2235 2236 case HERMON_DRV_CLEANUP_LEVEL4: 2237 /* Then cleanup the phase1 resources */ 2238 hermon_rsrc_fini(state, HERMON_RSRC_CLEANUP_PHASE1_COMPLETE); 2239 /* FALLTHROUGH */ 2240 2241 case HERMON_DRV_CLEANUP_LEVEL3: 2242 /* Teardown any resources allocated for the config profile */ 2243 hermon_cfg_profile_fini(state); 2244 /* FALLTHROUGH */ 2245 2246 case HERMON_DRV_CLEANUP_LEVEL2: 2247 #ifdef HERMON_SUPPORTS_MSIX_BAR 2248 /* 2249 * unmap 3rd BAR, MSIX BAR 2250 */ 2251 if (state->hs_reg_msihdl) { 2252 ddi_regs_map_free(&state->hs_reg_msihdl); 2253 state->hs_reg_msihdl = NULL; 2254 } 2255 /* FALLTHROUGH */ 2256 #endif 2257 case HERMON_DRV_CLEANUP_LEVEL1: 2258 case HERMON_DRV_CLEANUP_LEVEL0: 2259 /* 2260 * LEVEL1 and LEVEL0 resources are freed in 2261 * hermon_drv_fini2(). 2262 */ 2263 break; 2264 2265 default: 2266 HERMON_WARNING(state, "unexpected driver cleanup level"); 2267 return; 2268 } 2269 } 2270 2271 2272 /* 2273 * hermon_soft_state_init() 2274 * Context: Only called from attach() path context 2275 */ 2276 static int 2277 hermon_soft_state_init(hermon_state_t *state) 2278 { 2279 ibt_hca_attr_t *hca_attr; 2280 uint64_t maxval, val; 2281 ibt_hca_flags_t caps = IBT_HCA_NO_FLAGS; 2282 ibt_hca_flags2_t caps2 = IBT_HCA2_NO_FLAGS; 2283 int status; 2284 int max_send_wqe_bytes; 2285 int max_recv_wqe_bytes; 2286 2287 /* 2288 * The ibc_hca_info_t struct is passed to the IBTF. This is the 2289 * routine where we initialize it. Many of the init values come from 2290 * either configuration variables or successful queries of the Hermon 2291 * hardware abilities 2292 */ 2293 state->hs_ibtfinfo.hca_ci_vers = IBCI_V3; 2294 state->hs_ibtfinfo.hca_dip = state->hs_dip; 2295 state->hs_ibtfinfo.hca_handle = (ibc_hca_hdl_t)state; 2296 state->hs_ibtfinfo.hca_ops = &hermon_ibc_ops; 2297 2298 hca_attr = kmem_zalloc(sizeof (ibt_hca_attr_t), KM_SLEEP); 2299 state->hs_ibtfinfo.hca_attr = hca_attr; 2300 2301 hca_attr->hca_fw_major_version = state->hs_fw.fw_rev_major; 2302 hca_attr->hca_fw_minor_version = state->hs_fw.fw_rev_minor; 2303 hca_attr->hca_fw_micro_version = state->hs_fw.fw_rev_subminor; 2304 2305 /* CQ interrupt moderation maximums - each limited to 16 bits */ 2306 hca_attr->hca_max_cq_mod_count = 0xFFFF; 2307 hca_attr->hca_max_cq_mod_usec = 0xFFFF; 2308 2309 /* CQ relocation to other EQs - change when multiple MSI-Xs are used */ 2310 hca_attr->hca_max_cq_handlers = 1; 2311 2312 /* 2313 * Determine HCA capabilities: 2314 * No default support for IBT_HCA_RD, IBT_HCA_RAW_MULTICAST, 2315 * IBT_HCA_ATOMICS_GLOBAL, IBT_HCA_RESIZE_CHAN, IBT_HCA_INIT_TYPE, 2316 * or IBT_HCA_SHUTDOWN_PORT 2317 * But IBT_HCA_AH_PORT_CHECK, IBT_HCA_SQD_RTS_PORT, IBT_HCA_SI_GUID, 2318 * IBT_HCA_RNR_NAK, IBT_HCA_CURRENT_QP_STATE, IBT_HCA_PORT_UP, 2319 * IBT_HCA_SRQ, IBT_HCA_RESIZE_SRQ and IBT_HCA_FMR are always 2320 * supported 2321 * All other features are conditionally supported, depending on the 2322 * status return by the Hermon HCA in QUERY_DEV_LIM. 2323 */ 2324 if (state->hs_devlim.ud_multi) { 2325 caps |= IBT_HCA_UD_MULTICAST; 2326 } 2327 if (state->hs_devlim.atomic) { 2328 caps |= IBT_HCA_ATOMICS_HCA; 2329 } 2330 if (state->hs_devlim.apm) { 2331 caps |= IBT_HCA_AUTO_PATH_MIG; 2332 } 2333 if (state->hs_devlim.pkey_v) { 2334 caps |= IBT_HCA_PKEY_CNTR; 2335 } 2336 if (state->hs_devlim.qkey_v) { 2337 caps |= IBT_HCA_QKEY_CNTR; 2338 } 2339 if (state->hs_devlim.ipoib_cksm) { 2340 caps |= IBT_HCA_CKSUM_FULL; 2341 caps2 |= IBT_HCA2_IP_CLASS; 2342 } 2343 if (state->hs_devlim.mod_wr_srq) { 2344 caps |= IBT_HCA_RESIZE_SRQ; 2345 } 2346 if (state->hs_devlim.lif) { 2347 caps |= IBT_HCA_LOCAL_INVAL_FENCE; 2348 } 2349 if (state->hs_devlim.reserved_lkey) { 2350 caps2 |= IBT_HCA2_RES_LKEY; 2351 hca_attr->hca_reserved_lkey = state->hs_devlim.rsv_lkey; 2352 } 2353 if (state->hs_devlim.local_inv && state->hs_devlim.remote_inv && 2354 state->hs_devlim.fast_reg_wr) { /* fw needs to be >= 2.6.636 */ 2355 if (state->hs_fw.fw_rev_major > 2) 2356 caps2 |= IBT_HCA2_MEM_MGT_EXT; 2357 else if (state->hs_fw.fw_rev_major == 2) 2358 if (state->hs_fw.fw_rev_minor > 6) 2359 caps2 |= IBT_HCA2_MEM_MGT_EXT; 2360 else if (state->hs_fw.fw_rev_minor == 6) 2361 if (state->hs_fw.fw_rev_subminor >= 636) 2362 caps2 |= IBT_HCA2_MEM_MGT_EXT; 2363 } 2364 if (state->hs_devlim.mps) { 2365 caps |= IBT_HCA_ZERO_BASED_VA; 2366 } 2367 if (state->hs_devlim.zb) { 2368 caps |= IBT_HCA_MULT_PAGE_SZ_MR; 2369 } 2370 caps |= (IBT_HCA_AH_PORT_CHECK | IBT_HCA_SQD_SQD_PORT | 2371 IBT_HCA_SI_GUID | IBT_HCA_RNR_NAK | IBT_HCA_CURRENT_QP_STATE | 2372 IBT_HCA_PORT_UP | IBT_HCA_RC_SRQ | IBT_HCA_UD_SRQ | IBT_HCA_FMR); 2373 2374 if (state->hs_devlim.log_max_gso_sz) { 2375 hca_attr->hca_max_lso_size = 2376 (1 << state->hs_devlim.log_max_gso_sz); 2377 /* More work needed in hermon_post_send for larger values */ 2378 hca_attr->hca_max_lso_hdr_size = 0x2c; /* IPv4 only */ 2379 } 2380 2381 caps |= IBT_HCA_WQE_SIZE_INFO; 2382 max_send_wqe_bytes = state->hs_devlim.max_desc_sz_sq; 2383 max_recv_wqe_bytes = state->hs_devlim.max_desc_sz_rq; 2384 hca_attr->hca_ud_send_sgl_sz = (max_send_wqe_bytes / 16) - 4; 2385 hca_attr->hca_conn_send_sgl_sz = (max_send_wqe_bytes / 16) - 1; 2386 hca_attr->hca_conn_rdma_sgl_overhead = 1; 2387 hca_attr->hca_recv_sgl_sz = max_recv_wqe_bytes / 16; 2388 2389 /* We choose not to support "inline" unless it improves performance */ 2390 hca_attr->hca_max_inline_size = 0; 2391 hca_attr->hca_ud_send_inline_sz = 0; 2392 hca_attr->hca_conn_send_inline_sz = 0; 2393 hca_attr->hca_conn_rdmaw_inline_overhead = 4; 2394 2395 hca_attr->hca_flags = caps; 2396 hca_attr->hca_flags2 = caps2; 2397 2398 /* 2399 * Set hca_attr's IDs 2400 */ 2401 hca_attr->hca_vendor_id = state->hs_vendor_id; 2402 hca_attr->hca_device_id = state->hs_device_id; 2403 hca_attr->hca_version_id = state->hs_revision_id; 2404 2405 /* 2406 * Determine number of available QPs and max QP size. Number of 2407 * available QPs is determined by subtracting the number of 2408 * "reserved QPs" (i.e. reserved for firmware use) from the 2409 * total number configured. 2410 */ 2411 val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_qp); 2412 hca_attr->hca_max_qp = val - ((uint64_t)1 << 2413 state->hs_devlim.log_rsvd_qp); 2414 maxval = ((uint64_t)1 << state->hs_devlim.log_max_qp_sz); 2415 val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_max_qp_sz); 2416 if (val > maxval) { 2417 kmem_free(hca_attr, sizeof (ibt_hca_attr_t)); 2418 HERMON_ATTACH_MSG(state->hs_attach_buf, 2419 "soft_state_init_maxqpsz_toobig_fail"); 2420 return (DDI_FAILURE); 2421 } 2422 /* we need to reduce this by the max space needed for headroom */ 2423 hca_attr->hca_max_qp_sz = (uint_t)val - (HERMON_QP_OH_SIZE >> 2424 HERMON_QP_WQE_LOG_MINIMUM) - 1; 2425 2426 /* 2427 * Determine max scatter-gather size in WQEs. The HCA has split 2428 * the max sgl into rec'v Q and send Q values. Use the least. 2429 * 2430 * This is mainly useful for legacy clients. Smart clients 2431 * such as IPoIB will use the IBT_HCA_WQE_SIZE_INFO sgl info. 2432 */ 2433 if (state->hs_devlim.max_sg_rq <= state->hs_devlim.max_sg_sq) { 2434 maxval = state->hs_devlim.max_sg_rq; 2435 } else { 2436 maxval = state->hs_devlim.max_sg_sq; 2437 } 2438 val = state->hs_cfg_profile->cp_wqe_max_sgl; 2439 if (val > maxval) { 2440 kmem_free(hca_attr, sizeof (ibt_hca_attr_t)); 2441 HERMON_ATTACH_MSG(state->hs_attach_buf, 2442 "soft_state_init_toomanysgl_fail"); 2443 return (DDI_FAILURE); 2444 } 2445 /* If the rounded value for max SGL is too large, cap it */ 2446 if (state->hs_cfg_profile->cp_wqe_real_max_sgl > maxval) { 2447 state->hs_cfg_profile->cp_wqe_real_max_sgl = (uint32_t)maxval; 2448 val = maxval; 2449 } else { 2450 val = state->hs_cfg_profile->cp_wqe_real_max_sgl; 2451 } 2452 2453 hca_attr->hca_max_sgl = (uint_t)val; 2454 hca_attr->hca_max_rd_sgl = 0; /* zero because RD is unsupported */ 2455 2456 /* 2457 * Determine number of available CQs and max CQ size. Number of 2458 * available CQs is determined by subtracting the number of 2459 * "reserved CQs" (i.e. reserved for firmware use) from the 2460 * total number configured. 2461 */ 2462 val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_cq); 2463 hca_attr->hca_max_cq = val - ((uint64_t)1 << 2464 state->hs_devlim.log_rsvd_cq); 2465 maxval = ((uint64_t)1 << state->hs_devlim.log_max_cq_sz); 2466 val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_max_cq_sz) - 1; 2467 if (val > maxval) { 2468 kmem_free(hca_attr, sizeof (ibt_hca_attr_t)); 2469 HERMON_ATTACH_MSG(state->hs_attach_buf, 2470 "soft_state_init_maxcqsz_toobig_fail"); 2471 return (DDI_FAILURE); 2472 } 2473 hca_attr->hca_max_cq_sz = (uint_t)val; 2474 2475 /* 2476 * Determine number of available SRQs and max SRQ size. Number of 2477 * available SRQs is determined by subtracting the number of 2478 * "reserved SRQs" (i.e. reserved for firmware use) from the 2479 * total number configured. 2480 */ 2481 val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_srq); 2482 hca_attr->hca_max_srqs = val - ((uint64_t)1 << 2483 state->hs_devlim.log_rsvd_srq); 2484 maxval = ((uint64_t)1 << state->hs_devlim.log_max_srq_sz); 2485 val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_max_srq_sz); 2486 2487 if (val > maxval) { 2488 kmem_free(hca_attr, sizeof (ibt_hca_attr_t)); 2489 HERMON_ATTACH_MSG(state->hs_attach_buf, 2490 "soft_state_init_maxsrqsz_toobig_fail"); 2491 return (DDI_FAILURE); 2492 } 2493 hca_attr->hca_max_srqs_sz = (uint_t)val; 2494 2495 val = hca_attr->hca_recv_sgl_sz - 1; /* SRQ has a list link */ 2496 maxval = state->hs_devlim.max_sg_rq - 1; 2497 if (val > maxval) { 2498 kmem_free(hca_attr, sizeof (ibt_hca_attr_t)); 2499 HERMON_ATTACH_MSG(state->hs_attach_buf, 2500 "soft_state_init_toomanysrqsgl_fail"); 2501 return (DDI_FAILURE); 2502 } 2503 hca_attr->hca_max_srq_sgl = (uint_t)val; 2504 2505 /* 2506 * Determine supported HCA page sizes 2507 * XXX 2508 * For now we simply return the system pagesize as the only supported 2509 * pagesize 2510 */ 2511 hca_attr->hca_page_sz = ((PAGESIZE == (1 << 13)) ? IBT_PAGE_8K : 2512 IBT_PAGE_4K); 2513 2514 /* 2515 * Determine number of available MemReg, MemWin, and their max size. 2516 * Number of available MRs and MWs is determined by subtracting 2517 * the number of "reserved MPTs" (i.e. reserved for firmware use) 2518 * from the total number configured for each. 2519 */ 2520 val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_dmpt); 2521 hca_attr->hca_max_memr = val - ((uint64_t)1 << 2522 state->hs_devlim.log_rsvd_dmpt); 2523 hca_attr->hca_max_mem_win = state->hs_devlim.mem_win ? (val - 2524 ((uint64_t)1 << state->hs_devlim.log_rsvd_dmpt)) : 0; 2525 maxval = state->hs_devlim.log_max_mrw_sz; 2526 val = state->hs_cfg_profile->cp_log_max_mrw_sz; 2527 if (val > maxval) { 2528 kmem_free(hca_attr, sizeof (ibt_hca_attr_t)); 2529 HERMON_ATTACH_MSG(state->hs_attach_buf, 2530 "soft_state_init_maxmrwsz_toobig_fail"); 2531 return (DDI_FAILURE); 2532 } 2533 hca_attr->hca_max_memr_len = ((uint64_t)1 << val); 2534 2535 /* Determine RDMA/Atomic properties */ 2536 val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_rdb); 2537 hca_attr->hca_max_rsc = (uint_t)val; 2538 val = state->hs_cfg_profile->cp_hca_max_rdma_in_qp; 2539 hca_attr->hca_max_rdma_in_qp = (uint8_t)val; 2540 val = state->hs_cfg_profile->cp_hca_max_rdma_out_qp; 2541 hca_attr->hca_max_rdma_out_qp = (uint8_t)val; 2542 hca_attr->hca_max_rdma_in_ee = 0; 2543 hca_attr->hca_max_rdma_out_ee = 0; 2544 2545 /* 2546 * Determine maximum number of raw IPv6 and Ether QPs. Set to 0 2547 * because neither type of raw QP is supported 2548 */ 2549 hca_attr->hca_max_ipv6_qp = 0; 2550 hca_attr->hca_max_ether_qp = 0; 2551 2552 /* Determine max number of MCGs and max QP-per-MCG */ 2553 val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_qp); 2554 hca_attr->hca_max_mcg_qps = (uint_t)val; 2555 val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_mcg); 2556 hca_attr->hca_max_mcg = (uint_t)val; 2557 val = state->hs_cfg_profile->cp_num_qp_per_mcg; 2558 hca_attr->hca_max_qp_per_mcg = (uint_t)val; 2559 2560 /* Determine max number partitions (i.e. PKeys) */ 2561 maxval = ((uint64_t)state->hs_cfg_profile->cp_num_ports << 2562 state->hs_queryport.log_max_pkey); 2563 val = ((uint64_t)state->hs_cfg_profile->cp_num_ports << 2564 state->hs_cfg_profile->cp_log_max_pkeytbl); 2565 2566 if (val > maxval) { 2567 kmem_free(hca_attr, sizeof (ibt_hca_attr_t)); 2568 HERMON_ATTACH_MSG(state->hs_attach_buf, 2569 "soft_state_init_toomanypkey_fail"); 2570 return (DDI_FAILURE); 2571 } 2572 hca_attr->hca_max_partitions = (uint16_t)val; 2573 2574 /* Determine number of ports */ 2575 maxval = state->hs_devlim.num_ports; 2576 val = state->hs_cfg_profile->cp_num_ports; 2577 if ((val > maxval) || (val == 0)) { 2578 kmem_free(hca_attr, sizeof (ibt_hca_attr_t)); 2579 HERMON_ATTACH_MSG(state->hs_attach_buf, 2580 "soft_state_init_toomanyports_fail"); 2581 return (DDI_FAILURE); 2582 } 2583 hca_attr->hca_nports = (uint8_t)val; 2584 2585 /* Copy NodeGUID and SystemImageGUID from softstate */ 2586 hca_attr->hca_node_guid = state->hs_nodeguid; 2587 hca_attr->hca_si_guid = state->hs_sysimgguid; 2588 2589 /* 2590 * Determine local ACK delay. Use the value suggested by the Hermon 2591 * hardware (from the QUERY_DEV_CAP command) 2592 */ 2593 hca_attr->hca_local_ack_delay = state->hs_devlim.ca_ack_delay; 2594 2595 /* Determine max SGID table and PKey table sizes */ 2596 val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_max_gidtbl); 2597 hca_attr->hca_max_port_sgid_tbl_sz = (uint_t)val; 2598 val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_max_pkeytbl); 2599 hca_attr->hca_max_port_pkey_tbl_sz = (uint16_t)val; 2600 2601 /* Determine max number of PDs */ 2602 maxval = ((uint64_t)1 << state->hs_devlim.log_max_pd); 2603 val = ((uint64_t)1 << state->hs_cfg_profile->cp_log_num_pd); 2604 if (val > maxval) { 2605 kmem_free(hca_attr, sizeof (ibt_hca_attr_t)); 2606 HERMON_ATTACH_MSG(state->hs_attach_buf, 2607 "soft_state_init_toomanypd_fail"); 2608 return (DDI_FAILURE); 2609 } 2610 hca_attr->hca_max_pd = (uint_t)val; 2611 2612 /* Determine max number of Address Handles (NOT IN ARBEL or HERMON) */ 2613 hca_attr->hca_max_ah = 0; 2614 2615 /* No RDDs or EECs (since Reliable Datagram is not supported) */ 2616 hca_attr->hca_max_rdd = 0; 2617 hca_attr->hca_max_eec = 0; 2618 2619 /* Initialize lock for reserved UAR page access */ 2620 mutex_init(&state->hs_uar_lock, NULL, MUTEX_DRIVER, 2621 DDI_INTR_PRI(state->hs_intrmsi_pri)); 2622 2623 /* Initialize the flash fields */ 2624 state->hs_fw_flashstarted = 0; 2625 mutex_init(&state->hs_fw_flashlock, NULL, MUTEX_DRIVER, 2626 DDI_INTR_PRI(state->hs_intrmsi_pri)); 2627 2628 /* Initialize the lock for the info ioctl */ 2629 mutex_init(&state->hs_info_lock, NULL, MUTEX_DRIVER, 2630 DDI_INTR_PRI(state->hs_intrmsi_pri)); 2631 2632 /* Initialize the AVL tree for QP number support */ 2633 hermon_qpn_avl_init(state); 2634 2635 /* Initialize the kstat info structure */ 2636 status = hermon_kstat_init(state); 2637 if (status != DDI_SUCCESS) { 2638 hermon_qpn_avl_fini(state); 2639 mutex_destroy(&state->hs_info_lock); 2640 mutex_destroy(&state->hs_fw_flashlock); 2641 mutex_destroy(&state->hs_uar_lock); 2642 kmem_free(hca_attr, sizeof (ibt_hca_attr_t)); 2643 HERMON_ATTACH_MSG(state->hs_attach_buf, 2644 "soft_state_init_kstatinit_fail"); 2645 return (DDI_FAILURE); 2646 } 2647 2648 return (DDI_SUCCESS); 2649 } 2650 2651 2652 /* 2653 * hermon_soft_state_fini() 2654 * Context: Called only from detach() path context 2655 */ 2656 static void 2657 hermon_soft_state_fini(hermon_state_t *state) 2658 { 2659 2660 /* Teardown the kstat info */ 2661 hermon_kstat_fini(state); 2662 2663 /* Teardown the AVL tree for QP number support */ 2664 hermon_qpn_avl_fini(state); 2665 2666 /* Free up info ioctl mutex */ 2667 mutex_destroy(&state->hs_info_lock); 2668 2669 /* Free up flash mutex */ 2670 mutex_destroy(&state->hs_fw_flashlock); 2671 2672 /* Free up the UAR page access mutex */ 2673 mutex_destroy(&state->hs_uar_lock); 2674 2675 /* Free up the hca_attr struct */ 2676 kmem_free(state->hs_ibtfinfo.hca_attr, sizeof (ibt_hca_attr_t)); 2677 2678 } 2679 2680 /* 2681 * hermon_icm_config_setup() 2682 * Context: Only called from attach() path context 2683 */ 2684 static int 2685 hermon_icm_config_setup(hermon_state_t *state, 2686 hermon_hw_initqueryhca_t *inithca) 2687 { 2688 hermon_hw_querydevlim_t *devlim; 2689 hermon_cfg_profile_t *cfg; 2690 hermon_icm_table_t *icm_p[HERMON_NUM_ICM_RESOURCES]; 2691 hermon_icm_table_t *icm; 2692 hermon_icm_table_t *tmp; 2693 uint64_t icm_addr; 2694 uint64_t icm_size; 2695 int status, i, j; 2696 2697 2698 /* Bring in local devlims, cfg_profile and hs_icm table list */ 2699 devlim = &state->hs_devlim; 2700 cfg = state->hs_cfg_profile; 2701 icm = state->hs_icm; 2702 2703 /* 2704 * Assign each ICM table's entry size from data in the devlims, 2705 * except for RDB and MCG sizes, which are not returned in devlims 2706 * but do have a fixed size, and the UAR context entry size, which 2707 * we determine. For this, we use the "cp_num_pgs_per_uce" value 2708 * from our hs_cfg_profile. 2709 */ 2710 icm[HERMON_CMPT].object_size = devlim->cmpt_entry_sz; 2711 icm[HERMON_CMPT_QPC].object_size = devlim->cmpt_entry_sz; 2712 icm[HERMON_CMPT_SRQC].object_size = devlim->cmpt_entry_sz; 2713 icm[HERMON_CMPT_CQC].object_size = devlim->cmpt_entry_sz; 2714 icm[HERMON_CMPT_EQC].object_size = devlim->cmpt_entry_sz; 2715 icm[HERMON_MTT].object_size = devlim->mtt_entry_sz; 2716 icm[HERMON_DMPT].object_size = devlim->dmpt_entry_sz; 2717 icm[HERMON_QPC].object_size = devlim->qpc_entry_sz; 2718 icm[HERMON_CQC].object_size = devlim->cqc_entry_sz; 2719 icm[HERMON_SRQC].object_size = devlim->srq_entry_sz; 2720 icm[HERMON_EQC].object_size = devlim->eqc_entry_sz; 2721 icm[HERMON_RDB].object_size = devlim->rdmardc_entry_sz * 2722 cfg->cp_hca_max_rdma_in_qp; 2723 icm[HERMON_MCG].object_size = HERMON_MCG_SIZE; 2724 icm[HERMON_ALTC].object_size = devlim->altc_entry_sz; 2725 icm[HERMON_AUXC].object_size = devlim->aux_entry_sz; 2726 2727 /* Assign each ICM table's log2 number of entries */ 2728 icm[HERMON_CMPT].log_num_entries = cfg->cp_log_num_cmpt; 2729 icm[HERMON_CMPT_QPC].log_num_entries = cfg->cp_log_num_qp; 2730 icm[HERMON_CMPT_SRQC].log_num_entries = cfg->cp_log_num_srq; 2731 icm[HERMON_CMPT_CQC].log_num_entries = cfg->cp_log_num_cq; 2732 icm[HERMON_CMPT_EQC].log_num_entries = HERMON_NUM_EQ_SHIFT; 2733 icm[HERMON_MTT].log_num_entries = cfg->cp_log_num_mtt; 2734 icm[HERMON_DMPT].log_num_entries = cfg->cp_log_num_dmpt; 2735 icm[HERMON_QPC].log_num_entries = cfg->cp_log_num_qp; 2736 icm[HERMON_SRQC].log_num_entries = cfg->cp_log_num_srq; 2737 icm[HERMON_CQC].log_num_entries = cfg->cp_log_num_cq; 2738 icm[HERMON_EQC].log_num_entries = HERMON_NUM_EQ_SHIFT; 2739 icm[HERMON_RDB].log_num_entries = cfg->cp_log_num_qp; 2740 icm[HERMON_MCG].log_num_entries = cfg->cp_log_num_mcg; 2741 icm[HERMON_ALTC].log_num_entries = cfg->cp_log_num_qp; 2742 icm[HERMON_AUXC].log_num_entries = cfg->cp_log_num_qp; 2743 2744 /* Initialize the ICM tables */ 2745 hermon_icm_tables_init(state); 2746 2747 /* 2748 * ICM tables must be aligned on their size in the ICM address 2749 * space. So, here we order the tables from largest total table 2750 * size to the smallest. All tables are a power of 2 in size, so 2751 * this will ensure that all tables are aligned on their own size 2752 * without wasting space in the ICM. 2753 * 2754 * In order to easily set the ICM addresses without needing to 2755 * worry about the ordering of our table indices as relates to 2756 * the hermon_rsrc_type_t enum, we will use a list of pointers 2757 * representing the tables for the sort, then assign ICM addresses 2758 * below using it. 2759 */ 2760 for (i = 0; i < HERMON_NUM_ICM_RESOURCES; i++) { 2761 icm_p[i] = &icm[i]; 2762 } 2763 for (i = HERMON_NUM_ICM_RESOURCES; i > 0; i--) { 2764 switch (i) { 2765 case HERMON_CMPT_QPC: 2766 case HERMON_CMPT_SRQC: 2767 case HERMON_CMPT_CQC: 2768 case HERMON_CMPT_EQC: 2769 continue; 2770 } 2771 for (j = 1; j < i; j++) { 2772 if (icm_p[j]->table_size > icm_p[j - 1]->table_size) { 2773 tmp = icm_p[j]; 2774 icm_p[j] = icm_p[j - 1]; 2775 icm_p[j - 1] = tmp; 2776 } 2777 } 2778 } 2779 2780 /* Initialize the ICM address and ICM size */ 2781 icm_addr = icm_size = 0; 2782 2783 /* 2784 * Set the ICM base address of each table, using our sorted 2785 * list of pointers from above. 2786 */ 2787 for (i = 0; i < HERMON_NUM_ICM_RESOURCES; i++) { 2788 j = icm_p[i]->icm_type; 2789 switch (j) { 2790 case HERMON_CMPT_QPC: 2791 case HERMON_CMPT_SRQC: 2792 case HERMON_CMPT_CQC: 2793 case HERMON_CMPT_EQC: 2794 continue; 2795 } 2796 if (icm[j].table_size) { 2797 /* 2798 * Set the ICM base address in the table, save the 2799 * ICM offset in the rsrc pool and increment the 2800 * total ICM allocation. 2801 */ 2802 icm[j].icm_baseaddr = icm_addr; 2803 if (hermon_verbose) { 2804 IBTF_DPRINTF_L2("ICMADDR", "rsrc %x @ %p" 2805 " size %llx", j, icm[j].icm_baseaddr, 2806 icm[j].table_size); 2807 } 2808 icm_size += icm[j].table_size; 2809 } 2810 2811 /* Verify that we don't exceed maximum ICM size */ 2812 if (icm_size > devlim->max_icm_size) { 2813 /* free the ICM table memory resources */ 2814 hermon_icm_tables_fini(state); 2815 cmn_err(CE_WARN, "ICM configuration exceeds maximum " 2816 "configuration: max (0x%lx) requested (0x%lx)\n", 2817 (ulong_t)devlim->max_icm_size, (ulong_t)icm_size); 2818 HERMON_ATTACH_MSG(state->hs_attach_buf, 2819 "icm_config_toobig_fail"); 2820 return (DDI_FAILURE); 2821 } 2822 2823 /* assign address to the 4 pieces of the CMPT */ 2824 if (j == HERMON_CMPT) { 2825 uint64_t cmpt_size = icm[j].table_size >> 2; 2826 #define init_cmpt_icm_baseaddr(rsrc, indx) \ 2827 icm[rsrc].icm_baseaddr = icm_addr + (indx * cmpt_size); 2828 init_cmpt_icm_baseaddr(HERMON_CMPT_QPC, 0); 2829 init_cmpt_icm_baseaddr(HERMON_CMPT_SRQC, 1); 2830 init_cmpt_icm_baseaddr(HERMON_CMPT_CQC, 2); 2831 init_cmpt_icm_baseaddr(HERMON_CMPT_EQC, 3); 2832 } 2833 2834 /* Increment the ICM address for the next table */ 2835 icm_addr += icm[j].table_size; 2836 } 2837 2838 /* Populate the structure for the INIT_HCA command */ 2839 hermon_inithca_set(state, inithca); 2840 2841 /* 2842 * Prior to invoking INIT_HCA, we must have ICM memory in place 2843 * for the reserved objects in each table. We will allocate and map 2844 * this initial ICM memory here. Note that given the assignment 2845 * of span_size above, tables that are smaller or equal in total 2846 * size to the default span_size will be mapped in full. 2847 */ 2848 status = hermon_icm_dma_init(state); 2849 if (status != DDI_SUCCESS) { 2850 /* free the ICM table memory resources */ 2851 hermon_icm_tables_fini(state); 2852 HERMON_WARNING(state, "Failed to allocate initial ICM"); 2853 HERMON_ATTACH_MSG(state->hs_attach_buf, 2854 "icm_config_dma_init_fail"); 2855 return (DDI_FAILURE); 2856 } 2857 2858 return (DDI_SUCCESS); 2859 } 2860 2861 /* 2862 * hermon_inithca_set() 2863 * Context: Only called from attach() path context 2864 */ 2865 static void 2866 hermon_inithca_set(hermon_state_t *state, hermon_hw_initqueryhca_t *inithca) 2867 { 2868 hermon_cfg_profile_t *cfg; 2869 hermon_icm_table_t *icm; 2870 int i; 2871 2872 2873 /* Populate the INIT_HCA structure */ 2874 icm = state->hs_icm; 2875 cfg = state->hs_cfg_profile; 2876 2877 /* set version */ 2878 inithca->version = 0x02; /* PRM 0.36 */ 2879 /* set cacheline - log2 in 16-byte chunks */ 2880 inithca->log2_cacheline = 0x2; /* optimized for 64 byte cache */ 2881 2882 /* we need to update the inithca info with thie UAR info too */ 2883 inithca->uar.log_max_uars = highbit(cfg->cp_log_num_uar); 2884 inithca->uar.uar_pg_sz = PAGESHIFT - HERMON_PAGESHIFT; 2885 2886 /* Set endianess */ 2887 #ifdef _LITTLE_ENDIAN 2888 inithca->big_endian = 0; 2889 #else 2890 inithca->big_endian = 1; 2891 #endif 2892 2893 /* Port Checking is on by default */ 2894 inithca->udav_port_chk = HERMON_UDAV_PORTCHK_ENABLED; 2895 2896 /* Enable IPoIB checksum */ 2897 if (state->hs_devlim.ipoib_cksm) 2898 inithca->chsum_en = 1; 2899 2900 /* Set each ICM table's attributes */ 2901 for (i = 0; i < HERMON_NUM_ICM_RESOURCES; i++) { 2902 switch (icm[i].icm_type) { 2903 case HERMON_CMPT: 2904 inithca->tpt.cmpt_baseaddr = icm[i].icm_baseaddr; 2905 break; 2906 2907 case HERMON_MTT: 2908 inithca->tpt.mtt_baseaddr = icm[i].icm_baseaddr; 2909 break; 2910 2911 case HERMON_DMPT: 2912 inithca->tpt.dmpt_baseaddr = icm[i].icm_baseaddr; 2913 inithca->tpt.log_dmpt_sz = icm[i].log_num_entries; 2914 inithca->tpt.pgfault_rnr_to = 0; /* just in case */ 2915 break; 2916 2917 case HERMON_QPC: 2918 inithca->context.log_num_qp = icm[i].log_num_entries; 2919 inithca->context.qpc_baseaddr_h = 2920 icm[i].icm_baseaddr >> 32; 2921 inithca->context.qpc_baseaddr_l = 2922 (icm[i].icm_baseaddr & 0xFFFFFFFF) >> 5; 2923 break; 2924 2925 case HERMON_CQC: 2926 inithca->context.log_num_cq = icm[i].log_num_entries; 2927 inithca->context.cqc_baseaddr_h = 2928 icm[i].icm_baseaddr >> 32; 2929 inithca->context.cqc_baseaddr_l = 2930 (icm[i].icm_baseaddr & 0xFFFFFFFF) >> 5; 2931 break; 2932 2933 case HERMON_SRQC: 2934 inithca->context.log_num_srq = icm[i].log_num_entries; 2935 inithca->context.srqc_baseaddr_h = 2936 icm[i].icm_baseaddr >> 32; 2937 inithca->context.srqc_baseaddr_l = 2938 (icm[i].icm_baseaddr & 0xFFFFFFFF) >> 5; 2939 break; 2940 2941 case HERMON_EQC: 2942 inithca->context.log_num_eq = icm[i].log_num_entries; 2943 inithca->context.eqc_baseaddr_h = 2944 icm[i].icm_baseaddr >> 32; 2945 inithca->context.eqc_baseaddr_l = 2946 (icm[i].icm_baseaddr & 0xFFFFFFFF) >> 5; 2947 break; 2948 2949 case HERMON_RDB: 2950 inithca->context.rdmardc_baseaddr_h = 2951 icm[i].icm_baseaddr >> 32; 2952 inithca->context.rdmardc_baseaddr_l = 2953 (icm[i].icm_baseaddr & 0xFFFFFFFF) >> 5; 2954 inithca->context.log_num_rdmardc = 2955 icm[i].log_num_entries; 2956 break; 2957 2958 case HERMON_MCG: 2959 inithca->multi.mc_baseaddr = icm[i].icm_baseaddr; 2960 inithca->multi.log_mc_tbl_sz = icm[i].log_num_entries; 2961 inithca->multi.log_mc_tbl_ent = 2962 highbit(HERMON_MCGMEM_SZ(state)) - 1; 2963 inithca->multi.log_mc_tbl_hash_sz = 2964 cfg->cp_log_num_mcg_hash; 2965 inithca->multi.mc_hash_fn = HERMON_MCG_DEFAULT_HASH_FN; 2966 break; 2967 2968 case HERMON_ALTC: 2969 inithca->context.altc_baseaddr = icm[i].icm_baseaddr; 2970 break; 2971 2972 case HERMON_AUXC: 2973 inithca->context.auxc_baseaddr = icm[i].icm_baseaddr; 2974 break; 2975 2976 default: 2977 break; 2978 2979 } 2980 } 2981 2982 } 2983 2984 /* 2985 * hermon_icm_tables_init() 2986 * Context: Only called from attach() path context 2987 * 2988 * Dynamic ICM breaks the various ICM tables into "span_size" chunks 2989 * to enable allocation of backing memory on demand. Arbel used a 2990 * fixed size ARBEL_ICM_SPAN_SIZE (initially was 512KB) as the 2991 * span_size for all ICM chunks. Hermon has other considerations, 2992 * so the span_size used differs from Arbel. 2993 * 2994 * The basic considerations for why Hermon differs are: 2995 * 2996 * 1) ICM memory is in units of HERMON pages. 2997 * 2998 * 2) The AUXC table is approximately 1 byte per QP. 2999 * 3000 * 3) ICM memory for AUXC, ALTC, and RDB is allocated when 3001 * the ICM memory for the corresponding QPC is allocated. 3002 * 3003 * 4) ICM memory for the CMPT corresponding to the various primary 3004 * resources (QPC, SRQC, CQC, and EQC) is allocated when the ICM 3005 * memory for the primary resource is allocated. 3006 * 3007 * One HERMON page (4KB) would typically map 4K QPs worth of AUXC. 3008 * So, the minimum chunk for the various QPC related ICM memory should 3009 * all be allocated to support the 4K QPs. Currently, this means the 3010 * amount of memory for the various QP chunks is: 3011 * 3012 * QPC 256*4K bytes 3013 * RDB 128*4K bytes 3014 * CMPT 64*4K bytes 3015 * ALTC 64*4K bytes 3016 * AUXC 1*4K bytes 3017 * 3018 * The span_size chosen for the QP resource is 4KB of AUXC entries, 3019 * or 1 HERMON_PAGESIZE worth, which is the minimum ICM mapping size. 3020 * 3021 * Other ICM resources can have their span_size be more arbitrary. 3022 * This is 4K (HERMON_ICM_SPAN), except for MTTs because they are tiny. 3023 */ 3024 3025 /* macro to make the code below cleaner */ 3026 #define init_dependent(rsrc, dep) \ 3027 icm[dep].span = icm[rsrc].span; \ 3028 icm[dep].num_spans = icm[rsrc].num_spans; \ 3029 icm[dep].split_shift = icm[rsrc].split_shift; \ 3030 icm[dep].span_mask = icm[rsrc].span_mask; \ 3031 icm[dep].span_shift = icm[rsrc].span_shift; \ 3032 icm[dep].rsrc_mask = icm[rsrc].rsrc_mask; \ 3033 if (hermon_verbose) { \ 3034 IBTF_DPRINTF_L2("hermon", "tables_init: " \ 3035 "rsrc (0x%x) size (0x%lx) span (0x%x) " \ 3036 "num_spans (0x%x)", dep, icm[dep].table_size, \ 3037 icm[dep].span, icm[dep].num_spans); \ 3038 IBTF_DPRINTF_L2("hermon", "tables_init: " \ 3039 "span_shift (0x%x) split_shift (0x%x)", \ 3040 icm[dep].span_shift, icm[dep].split_shift); \ 3041 IBTF_DPRINTF_L2("hermon", "tables_init: " \ 3042 "span_mask (0x%x) rsrc_mask (0x%x)", \ 3043 icm[dep].span_mask, icm[dep].rsrc_mask); \ 3044 } 3045 3046 static void 3047 hermon_icm_tables_init(hermon_state_t *state) 3048 { 3049 hermon_icm_table_t *icm; 3050 int i, k; 3051 uint32_t per_split; 3052 3053 3054 icm = state->hs_icm; 3055 3056 for (i = 0; i < HERMON_NUM_ICM_RESOURCES; i++) { 3057 icm[i].icm_type = i; 3058 icm[i].num_entries = 1 << icm[i].log_num_entries; 3059 icm[i].log_object_size = highbit(icm[i].object_size) - 1; 3060 icm[i].table_size = icm[i].num_entries << 3061 icm[i].log_object_size; 3062 3063 /* deal with "dependent" resource types */ 3064 switch (i) { 3065 case HERMON_AUXC: 3066 #ifdef HERMON_FW_WORKAROUND 3067 icm[i].table_size = 0x80000000ull; 3068 /* FALLTHROUGH */ 3069 #endif 3070 case HERMON_CMPT_QPC: 3071 case HERMON_RDB: 3072 case HERMON_ALTC: 3073 init_dependent(HERMON_QPC, i); 3074 continue; 3075 case HERMON_CMPT_SRQC: 3076 init_dependent(HERMON_SRQC, i); 3077 continue; 3078 case HERMON_CMPT_CQC: 3079 init_dependent(HERMON_CQC, i); 3080 continue; 3081 case HERMON_CMPT_EQC: 3082 init_dependent(HERMON_EQC, i); 3083 continue; 3084 } 3085 3086 icm[i].span = HERMON_ICM_SPAN; /* default #rsrc's in 1 span */ 3087 if (i == HERMON_MTT) /* Alloc enough MTTs to map 256MB */ 3088 icm[i].span = HERMON_ICM_SPAN * 16; 3089 icm[i].num_spans = icm[i].num_entries / icm[i].span; 3090 if (icm[i].num_spans == 0) { 3091 icm[i].span = icm[i].num_entries; 3092 per_split = 1; 3093 icm[i].num_spans = icm[i].num_entries / icm[i].span; 3094 } else { 3095 per_split = icm[i].num_spans / HERMON_ICM_SPLIT; 3096 if (per_split == 0) { 3097 per_split = 1; 3098 } 3099 } 3100 if (hermon_verbose) 3101 IBTF_DPRINTF_L2("ICM", "rsrc %x span %x num_spans %x", 3102 i, icm[i].span, icm[i].num_spans); 3103 3104 /* 3105 * Ensure a minimum table size of an ICM page, and a 3106 * maximum span size of the ICM table size. This ensures 3107 * that we don't have less than an ICM page to map, which is 3108 * impossible, and that we will map an entire table at 3109 * once if it's total size is less than the span size. 3110 */ 3111 icm[i].table_size = max(icm[i].table_size, HERMON_PAGESIZE); 3112 3113 icm[i].span_shift = 0; 3114 for (k = icm[i].span; k != 1; k >>= 1) 3115 icm[i].span_shift++; 3116 icm[i].split_shift = icm[i].span_shift; 3117 for (k = per_split; k != 1; k >>= 1) 3118 icm[i].split_shift++; 3119 icm[i].span_mask = (1 << icm[i].split_shift) - 3120 (1 << icm[i].span_shift); 3121 icm[i].rsrc_mask = (1 << icm[i].span_shift) - 1; 3122 3123 3124 /* Initialize the table lock */ 3125 mutex_init(&icm[i].icm_table_lock, NULL, MUTEX_DRIVER, 3126 DDI_INTR_PRI(state->hs_intrmsi_pri)); 3127 cv_init(&icm[i].icm_table_cv, NULL, CV_DRIVER, NULL); 3128 3129 if (hermon_verbose) { 3130 IBTF_DPRINTF_L2("hermon", "tables_init: " 3131 "rsrc (0x%x) size (0x%lx)", i, icm[i].table_size); 3132 IBTF_DPRINTF_L2("hermon", "tables_init: " 3133 "span (0x%x) num_spans (0x%x)", 3134 icm[i].span, icm[i].num_spans); 3135 IBTF_DPRINTF_L2("hermon", "tables_init: " 3136 "span_shift (0x%x) split_shift (0x%x)", 3137 icm[i].span_shift, icm[i].split_shift); 3138 IBTF_DPRINTF_L2("hermon", "tables_init: " 3139 "span_mask (0x%x) rsrc_mask (0x%x)", 3140 icm[i].span_mask, icm[i].rsrc_mask); 3141 } 3142 } 3143 3144 } 3145 3146 /* 3147 * hermon_icm_tables_fini() 3148 * Context: Only called from attach() path context 3149 * 3150 * Clean up all icm_tables. Free the bitmap and dma_info arrays. 3151 */ 3152 static void 3153 hermon_icm_tables_fini(hermon_state_t *state) 3154 { 3155 hermon_icm_table_t *icm; 3156 int nspans; 3157 int i, j; 3158 3159 3160 icm = state->hs_icm; 3161 3162 for (i = 0; i < HERMON_NUM_ICM_RESOURCES; i++) { 3163 3164 mutex_enter(&icm[i].icm_table_lock); 3165 nspans = icm[i].num_spans; 3166 3167 for (j = 0; j < HERMON_ICM_SPLIT; j++) { 3168 if (icm[i].icm_dma[j]) 3169 /* Free the ICM DMA slots */ 3170 kmem_free(icm[i].icm_dma[j], 3171 nspans * sizeof (hermon_dma_info_t)); 3172 3173 if (icm[i].icm_bitmap[j]) 3174 /* Free the table bitmap */ 3175 kmem_free(icm[i].icm_bitmap[j], 3176 (nspans + 7) / 8); 3177 } 3178 /* Destroy the table lock */ 3179 cv_destroy(&icm[i].icm_table_cv); 3180 mutex_exit(&icm[i].icm_table_lock); 3181 mutex_destroy(&icm[i].icm_table_lock); 3182 } 3183 3184 } 3185 3186 /* 3187 * hermon_icm_dma_init() 3188 * Context: Only called from attach() path context 3189 */ 3190 static int 3191 hermon_icm_dma_init(hermon_state_t *state) 3192 { 3193 hermon_icm_table_t *icm; 3194 hermon_rsrc_type_t type; 3195 int status; 3196 3197 3198 /* 3199 * This routine will allocate initial ICM DMA resources for ICM 3200 * tables that have reserved ICM objects. This is the only routine 3201 * where we should have to allocate ICM outside of hermon_rsrc_alloc(). 3202 * We need to allocate ICM here explicitly, rather than in 3203 * hermon_rsrc_alloc(), because we've not yet completed the resource 3204 * pool initialization. When the resource pools are initialized 3205 * (in hermon_rsrc_init_phase2(), see hermon_rsrc.c for more 3206 * information), resource preallocations will be invoked to match 3207 * the ICM allocations seen here. We will then be able to use the 3208 * normal allocation path. Note we don't need to set a refcnt on 3209 * these initial allocations because that will be done in the calls 3210 * to hermon_rsrc_alloc() from hermon_hw_entries_init() for the 3211 * "prealloc" objects (see hermon_rsrc.c for more information). 3212 */ 3213 for (type = 0; type < HERMON_NUM_ICM_RESOURCES; type++) { 3214 3215 /* ICM for these is allocated within hermon_icm_alloc() */ 3216 switch (type) { 3217 case HERMON_CMPT: 3218 case HERMON_CMPT_QPC: 3219 case HERMON_CMPT_SRQC: 3220 case HERMON_CMPT_CQC: 3221 case HERMON_CMPT_EQC: 3222 case HERMON_AUXC: 3223 case HERMON_ALTC: 3224 case HERMON_RDB: 3225 continue; 3226 } 3227 3228 icm = &state->hs_icm[type]; 3229 3230 mutex_enter(&icm->icm_table_lock); 3231 status = hermon_icm_alloc(state, type, 0, 0); 3232 mutex_exit(&icm->icm_table_lock); 3233 if (status != DDI_SUCCESS) { 3234 while (type--) { 3235 icm = &state->hs_icm[type]; 3236 mutex_enter(&icm->icm_table_lock); 3237 hermon_icm_free(state, type, 0, 0); 3238 mutex_exit(&icm->icm_table_lock); 3239 } 3240 return (DDI_FAILURE); 3241 } 3242 3243 if (hermon_verbose) { 3244 IBTF_DPRINTF_L2("hermon", "hermon_icm_dma_init: " 3245 "table (0x%x) index (0x%x) allocated", type, 0); 3246 } 3247 } 3248 3249 return (DDI_SUCCESS); 3250 } 3251 3252 /* 3253 * hermon_icm_dma_fini() 3254 * Context: Only called from attach() path context 3255 * 3256 * ICM has been completely unmapped. We just free the memory here. 3257 */ 3258 static void 3259 hermon_icm_dma_fini(hermon_state_t *state) 3260 { 3261 hermon_icm_table_t *icm; 3262 hermon_dma_info_t *dma_info; 3263 hermon_rsrc_type_t type; 3264 int index1, index2; 3265 3266 3267 for (type = 0; type < HERMON_NUM_ICM_RESOURCES; type++) { 3268 icm = &state->hs_icm[type]; 3269 for (index1 = 0; index1 < HERMON_ICM_SPLIT; index1++) { 3270 dma_info = icm->icm_dma[index1]; 3271 if (dma_info == NULL) 3272 continue; 3273 for (index2 = 0; index2 < icm->num_spans; index2++) { 3274 if (dma_info[index2].dma_hdl) 3275 hermon_dma_free(&dma_info[index2]); 3276 dma_info[index2].dma_hdl = NULL; 3277 } 3278 } 3279 } 3280 3281 } 3282 3283 /* 3284 * hermon_hca_port_init() 3285 * Context: Only called from attach() path context 3286 */ 3287 static int 3288 hermon_hca_port_init(hermon_state_t *state) 3289 { 3290 hermon_hw_set_port_t *portinits, *initport; 3291 hermon_cfg_profile_t *cfgprof; 3292 uint_t num_ports; 3293 int i = 0, status; 3294 uint64_t maxval, val; 3295 uint64_t sysimgguid, nodeguid, portguid; 3296 3297 3298 cfgprof = state->hs_cfg_profile; 3299 3300 /* Get number of HCA ports */ 3301 num_ports = cfgprof->cp_num_ports; 3302 3303 /* Allocate space for Hermon set port struct(s) */ 3304 portinits = (hermon_hw_set_port_t *)kmem_zalloc(num_ports * 3305 sizeof (hermon_hw_set_port_t), KM_SLEEP); 3306 3307 3308 3309 /* Post commands to initialize each Hermon HCA port */ 3310 /* 3311 * In Hermon, the process is different than in previous HCAs. 3312 * Here, you have to: 3313 * QUERY_PORT - to get basic information from the HCA 3314 * set the fields accordingly 3315 * SET_PORT - to change/set everything as desired 3316 * INIT_PORT - to bring the port up 3317 * 3318 * Needs to be done for each port in turn 3319 */ 3320 3321 for (i = 0; i < num_ports; i++) { 3322 bzero(&state->hs_queryport, sizeof (hermon_hw_query_port_t)); 3323 status = hermon_cmn_query_cmd_post(state, QUERY_PORT, 0, 3324 (i + 1), &state->hs_queryport, 3325 sizeof (hermon_hw_query_port_t), HERMON_CMD_NOSLEEP_SPIN); 3326 if (status != HERMON_CMD_SUCCESS) { 3327 cmn_err(CE_CONT, "Hermon: QUERY_PORT (port %02d) " 3328 "command failed: %08x\n", i + 1, status); 3329 goto init_ports_fail; 3330 } 3331 initport = &portinits[i]; 3332 state->hs_initport = &portinits[i]; 3333 3334 bzero(initport, sizeof (hermon_hw_query_port_t)); 3335 3336 /* 3337 * Determine whether we need to override the firmware's 3338 * default SystemImageGUID setting. 3339 */ 3340 sysimgguid = cfgprof->cp_sysimgguid; 3341 if (sysimgguid != 0) { 3342 initport->sig = 1; 3343 initport->sys_img_guid = sysimgguid; 3344 } 3345 3346 /* 3347 * Determine whether we need to override the firmware's 3348 * default NodeGUID setting. 3349 */ 3350 nodeguid = cfgprof->cp_nodeguid; 3351 if (nodeguid != 0) { 3352 initport->ng = 1; 3353 initport->node_guid = nodeguid; 3354 } 3355 3356 /* 3357 * Determine whether we need to override the firmware's 3358 * default PortGUID setting. 3359 */ 3360 portguid = cfgprof->cp_portguid[i]; 3361 if (portguid != 0) { 3362 initport->g0 = 1; 3363 initport->guid0 = portguid; 3364 } 3365 3366 /* Validate max MTU size */ 3367 maxval = state->hs_queryport.ib_mtu; 3368 val = cfgprof->cp_max_mtu; 3369 if (val > maxval) { 3370 goto init_ports_fail; 3371 } 3372 3373 /* Validate the max port width */ 3374 maxval = state->hs_queryport.ib_port_wid; 3375 val = cfgprof->cp_max_port_width; 3376 if (val > maxval) { 3377 goto init_ports_fail; 3378 } 3379 3380 /* Validate max VL cap size */ 3381 maxval = state->hs_queryport.max_vl; 3382 val = cfgprof->cp_max_vlcap; 3383 if (val > maxval) { 3384 goto init_ports_fail; 3385 } 3386 3387 /* Validate max GID table size */ 3388 maxval = ((uint64_t)1 << state->hs_queryport.log_max_gid); 3389 val = ((uint64_t)1 << cfgprof->cp_log_max_gidtbl); 3390 if (val > maxval) { 3391 goto init_ports_fail; 3392 } 3393 initport->max_guid = (uint16_t)val; 3394 initport->mg = 1; 3395 3396 /* Validate max PKey table size */ 3397 maxval = ((uint64_t)1 << state->hs_queryport.log_max_pkey); 3398 val = ((uint64_t)1 << cfgprof->cp_log_max_pkeytbl); 3399 if (val > maxval) { 3400 goto init_ports_fail; 3401 } 3402 initport->max_pkey = (uint16_t)val; 3403 initport->mp = 1; 3404 /* 3405 * Post the SET_PORT cmd to Hermon firmware. This sets 3406 * the parameters of the port. 3407 */ 3408 status = hermon_set_port_cmd_post(state, initport, i + 1, 3409 HERMON_CMD_NOSLEEP_SPIN); 3410 if (status != HERMON_CMD_SUCCESS) { 3411 cmn_err(CE_CONT, "Hermon: SET_PORT (port %02d) command " 3412 "failed: %08x\n", i + 1, status); 3413 goto init_ports_fail; 3414 } 3415 /* issue another SET_PORT cmd - performance fix/workaround */ 3416 /* XXX - need to discuss with Mellanox */ 3417 bzero(initport, sizeof (hermon_hw_query_port_t)); 3418 initport->cap_mask = 0x02500868; 3419 status = hermon_set_port_cmd_post(state, initport, i + 1, 3420 HERMON_CMD_NOSLEEP_SPIN); 3421 if (status != HERMON_CMD_SUCCESS) { 3422 cmn_err(CE_CONT, "Hermon: SET_PORT (port %02d) command " 3423 "failed: %08x\n", i + 1, status); 3424 goto init_ports_fail; 3425 } 3426 } 3427 3428 /* 3429 * Finally, do the INIT_PORT for each port in turn 3430 * When this command completes, the corresponding Hermon port 3431 * will be physically "Up" and initialized. 3432 */ 3433 for (i = 0; i < num_ports; i++) { 3434 status = hermon_init_port_cmd_post(state, i + 1, 3435 HERMON_CMD_NOSLEEP_SPIN); 3436 if (status != HERMON_CMD_SUCCESS) { 3437 cmn_err(CE_CONT, "Hermon: INIT_PORT (port %02d) " 3438 "comman failed: %08x\n", i + 1, status); 3439 goto init_ports_fail; 3440 } 3441 } 3442 3443 /* Free up the memory for Hermon port init struct(s), return success */ 3444 kmem_free(portinits, num_ports * sizeof (hermon_hw_set_port_t)); 3445 return (DDI_SUCCESS); 3446 3447 init_ports_fail: 3448 /* 3449 * Free up the memory for Hermon port init struct(s), shutdown any 3450 * successfully initialized ports, and return failure 3451 */ 3452 kmem_free(portinits, num_ports * sizeof (hermon_hw_set_port_t)); 3453 (void) hermon_hca_ports_shutdown(state, i); 3454 3455 return (DDI_FAILURE); 3456 } 3457 3458 3459 /* 3460 * hermon_hca_ports_shutdown() 3461 * Context: Only called from attach() and/or detach() path contexts 3462 */ 3463 static int 3464 hermon_hca_ports_shutdown(hermon_state_t *state, uint_t num_init) 3465 { 3466 int i, status; 3467 3468 /* 3469 * Post commands to shutdown all init'd Hermon HCA ports. Note: if 3470 * any of these commands fail for any reason, it would be entirely 3471 * unexpected and probably indicative a serious problem (HW or SW). 3472 * Although we do return void from this function, this type of failure 3473 * should not go unreported. That is why we have the warning message. 3474 */ 3475 for (i = 0; i < num_init; i++) { 3476 status = hermon_close_port_cmd_post(state, i + 1, 3477 HERMON_CMD_NOSLEEP_SPIN); 3478 if (status != HERMON_CMD_SUCCESS) { 3479 HERMON_WARNING(state, "failed to shutdown HCA port"); 3480 return (status); 3481 } 3482 } 3483 return (HERMON_CMD_SUCCESS); 3484 } 3485 3486 3487 /* 3488 * hermon_internal_uarpg_init 3489 * Context: Only called from attach() path context 3490 */ 3491 static int 3492 hermon_internal_uarpg_init(hermon_state_t *state) 3493 { 3494 int status; 3495 hermon_dbr_info_t *info; 3496 3497 3498 /* 3499 * Allocate the UAR page for kernel use. This UAR page is 3500 * the privileged UAR page through which all kernel generated 3501 * doorbells will be rung. There are a number of UAR pages 3502 * reserved by hardware at the front of the UAR BAR, indicated 3503 * by DEVCAP.num_rsvd_uar, which we have already allocated. So, 3504 * the kernel page, or UAR page index num_rsvd_uar, will be 3505 * allocated here for kernel use. 3506 */ 3507 3508 status = hermon_rsrc_alloc(state, HERMON_UARPG, 1, HERMON_SLEEP, 3509 &state->hs_uarkpg_rsrc); 3510 if (status != DDI_SUCCESS) { 3511 return (DDI_FAILURE); 3512 } 3513 3514 /* Setup pointer to kernel UAR page */ 3515 state->hs_uar = (hermon_hw_uar_t *)state->hs_uarkpg_rsrc->hr_addr; 3516 3517 /* need to set up DBr tracking as well */ 3518 status = hermon_dbr_page_alloc(state, &info); 3519 if (status != DDI_SUCCESS) { 3520 return (DDI_FAILURE); 3521 } 3522 3523 /* store the page pointer in the private area - the rest s/b done */ 3524 state->hs_kern_dbr = info->dbr_page; 3525 return (DDI_SUCCESS); 3526 } 3527 3528 3529 /* 3530 * hermon_internal_uarpg_fini 3531 * Context: Only called from attach() and/or detach() path contexts 3532 */ 3533 static void 3534 hermon_internal_uarpg_fini(hermon_state_t *state) 3535 { 3536 3537 /* Free up Hermon UAR page #1 (kernel driver doorbells) */ 3538 hermon_rsrc_free(state, &state->hs_uarkpg_rsrc); 3539 3540 } 3541 3542 3543 /* 3544 * hermon_special_qp_contexts_reserve() 3545 * Context: Only called from attach() path context 3546 */ 3547 static int 3548 hermon_special_qp_contexts_reserve(hermon_state_t *state) 3549 { 3550 hermon_rsrc_t *qp0_rsrc, *qp1_rsrc, *qp_resvd; 3551 int status; 3552 3553 3554 /* Initialize the lock used for special QP rsrc management */ 3555 mutex_init(&state->hs_spec_qplock, NULL, MUTEX_DRIVER, 3556 DDI_INTR_PRI(state->hs_intrmsi_pri)); 3557 3558 /* 3559 * Reserve contexts for QP0. These QP contexts will be setup to 3560 * act as aliases for the real QP0. Note: We are required to grab 3561 * two QPs (one per port) even if we are operating in single-port 3562 * mode. 3563 */ 3564 status = hermon_rsrc_alloc(state, HERMON_QPC, 2, 3565 HERMON_SLEEP, &qp0_rsrc); 3566 if (status != DDI_SUCCESS) { 3567 mutex_destroy(&state->hs_spec_qplock); 3568 return (DDI_FAILURE); 3569 } 3570 state->hs_spec_qp0 = qp0_rsrc; 3571 3572 /* 3573 * Reserve contexts for QP1. These QP contexts will be setup to 3574 * act as aliases for the real QP1. Note: We are required to grab 3575 * two QPs (one per port) even if we are operating in single-port 3576 * mode. 3577 */ 3578 status = hermon_rsrc_alloc(state, HERMON_QPC, 2, 3579 HERMON_SLEEP, &qp1_rsrc); 3580 if (status != DDI_SUCCESS) { 3581 hermon_rsrc_free(state, &qp0_rsrc); 3582 mutex_destroy(&state->hs_spec_qplock); 3583 return (DDI_FAILURE); 3584 } 3585 state->hs_spec_qp1 = qp1_rsrc; 3586 3587 status = hermon_rsrc_alloc(state, HERMON_QPC, 4, 3588 HERMON_SLEEP, &qp_resvd); 3589 if (status != DDI_SUCCESS) { 3590 hermon_rsrc_free(state, &qp1_rsrc); 3591 hermon_rsrc_free(state, &qp0_rsrc); 3592 mutex_destroy(&state->hs_spec_qplock); 3593 return (DDI_FAILURE); 3594 } 3595 state->hs_spec_qp_unused = qp_resvd; 3596 3597 return (DDI_SUCCESS); 3598 } 3599 3600 3601 /* 3602 * hermon_special_qp_contexts_unreserve() 3603 * Context: Only called from attach() and/or detach() path contexts 3604 */ 3605 static void 3606 hermon_special_qp_contexts_unreserve(hermon_state_t *state) 3607 { 3608 3609 /* Unreserve contexts for spec_qp_unused */ 3610 hermon_rsrc_free(state, &state->hs_spec_qp_unused); 3611 3612 /* Unreserve contexts for QP1 */ 3613 hermon_rsrc_free(state, &state->hs_spec_qp1); 3614 3615 /* Unreserve contexts for QP0 */ 3616 hermon_rsrc_free(state, &state->hs_spec_qp0); 3617 3618 /* Destroy the lock used for special QP rsrc management */ 3619 mutex_destroy(&state->hs_spec_qplock); 3620 3621 } 3622 3623 3624 /* 3625 * hermon_sw_reset() 3626 * Context: Currently called only from attach() path context 3627 */ 3628 static int 3629 hermon_sw_reset(hermon_state_t *state) 3630 { 3631 ddi_acc_handle_t hdl = hermon_get_pcihdl(state); 3632 ddi_acc_handle_t cmdhdl = hermon_get_cmdhdl(state); 3633 uint32_t reset_delay; 3634 int status, i; 3635 uint32_t sem; 3636 uint_t offset; 3637 uint32_t data32; /* for devctl & linkctl */ 3638 int loopcnt; 3639 3640 /* initialize the FMA retry loop */ 3641 hermon_pio_init(fm_loop_cnt, fm_status, fm_test); 3642 hermon_pio_init(fm_loop_cnt2, fm_status2, fm_test2); 3643 3644 /* 3645 * If the configured software reset delay is set to zero, then we 3646 * will not attempt a software reset of the Hermon device. 3647 */ 3648 reset_delay = state->hs_cfg_profile->cp_sw_reset_delay; 3649 if (reset_delay == 0) { 3650 return (DDI_SUCCESS); 3651 } 3652 3653 /* the FMA retry loop starts. */ 3654 hermon_pio_start(state, cmdhdl, pio_error, fm_loop_cnt, fm_status, 3655 fm_test); 3656 hermon_pio_start(state, hdl, pio_error2, fm_loop_cnt2, fm_status2, 3657 fm_test2); 3658 3659 /* Query the PCI capabilities of the HCA device */ 3660 /* but don't process the VPD until after reset */ 3661 status = hermon_pci_capability_list(state, hdl); 3662 if (status != DDI_SUCCESS) { 3663 cmn_err(CE_NOTE, "failed to get pci capabilities list(0x%x)\n", 3664 status); 3665 return (DDI_FAILURE); 3666 } 3667 3668 /* 3669 * Read all PCI config info (reg0...reg63). Note: According to the 3670 * Hermon software reset application note, we should not read or 3671 * restore the values in reg22 and reg23. 3672 * NOTE: For Hermon (and Arbel too) it says to restore the command 3673 * register LAST, and technically, you need to restore the 3674 * PCIE Capability "device control" and "link control" (word-sized, 3675 * at offsets 0x08 and 0x10 from the capbility ID respectively). 3676 * We hold off restoring the command register - offset 0x4 - till last 3677 */ 3678 3679 /* 1st, wait for the semaphore assure accessibility - per PRM */ 3680 status = -1; 3681 for (i = 0; i < NANOSEC/MICROSEC /* 1sec timeout */; i++) { 3682 sem = ddi_get32(cmdhdl, state->hs_cmd_regs.sw_semaphore); 3683 if (sem == 0) { 3684 status = 0; 3685 break; 3686 } 3687 drv_usecwait(1); 3688 } 3689 3690 /* Check if timeout happens */ 3691 if (status == -1) { 3692 /* 3693 * Remove this acc handle from Hermon, then log 3694 * the error. 3695 */ 3696 hermon_pci_config_teardown(state, &hdl); 3697 3698 cmn_err(CE_WARN, "hermon_sw_reset timeout: " 3699 "failed to get the semaphore(0x%p)\n", 3700 (void *)state->hs_cmd_regs.sw_semaphore); 3701 3702 hermon_fm_ereport(state, HCA_IBA_ERR, HCA_ERR_NON_FATAL); 3703 return (DDI_FAILURE); 3704 } 3705 3706 for (i = 0; i < HERMON_SW_RESET_NUMREGS; i++) { 3707 if ((i != HERMON_SW_RESET_REG22_RSVD) && 3708 (i != HERMON_SW_RESET_REG23_RSVD)) { 3709 state->hs_cfg_data[i] = pci_config_get32(hdl, i << 2); 3710 } 3711 } 3712 3713 /* 3714 * Perform the software reset (by writing 1 at offset 0xF0010) 3715 */ 3716 ddi_put32(cmdhdl, state->hs_cmd_regs.sw_reset, HERMON_SW_RESET_START); 3717 3718 /* 3719 * This delay is required so as not to cause a panic here. If the 3720 * device is accessed too soon after reset it will not respond to 3721 * config cycles, causing a Master Abort and panic. 3722 */ 3723 drv_usecwait(reset_delay); 3724 3725 /* 3726 * Poll waiting for the device to finish resetting. 3727 */ 3728 loopcnt = 100; /* 100 times @ 100 usec - total delay 10 msec */ 3729 while ((pci_config_get32(hdl, 0) & 0x0000FFFF) != PCI_VENID_MLX) { 3730 drv_usecwait(HERMON_SW_RESET_POLL_DELAY); 3731 if (--loopcnt == 0) 3732 break; /* just in case, break and go on */ 3733 } 3734 if (loopcnt == 0) 3735 cmn_err(CE_CONT, "!Never see VEND_ID - read == %X", 3736 pci_config_get32(hdl, 0)); 3737 3738 /* 3739 * Restore the config info 3740 */ 3741 for (i = 0; i < HERMON_SW_RESET_NUMREGS; i++) { 3742 if (i == 1) continue; /* skip the status/ctrl reg */ 3743 if ((i != HERMON_SW_RESET_REG22_RSVD) && 3744 (i != HERMON_SW_RESET_REG23_RSVD)) { 3745 pci_config_put32(hdl, i << 2, state->hs_cfg_data[i]); 3746 } 3747 } 3748 3749 /* 3750 * PCI Express Capability - we saved during capability list, and 3751 * we'll restore them here. 3752 */ 3753 offset = state->hs_pci_cap_offset; 3754 data32 = state->hs_pci_cap_devctl; 3755 pci_config_put32(hdl, offset + HERMON_PCI_CAP_DEV_OFFS, data32); 3756 data32 = state->hs_pci_cap_lnkctl; 3757 pci_config_put32(hdl, offset + HERMON_PCI_CAP_LNK_OFFS, data32); 3758 3759 pci_config_put32(hdl, 0x04, (state->hs_cfg_data[1] | 0x0006)); 3760 3761 /* the FMA retry loop ends. */ 3762 hermon_pio_end(state, hdl, pio_error2, fm_loop_cnt2, fm_status2, 3763 fm_test2); 3764 hermon_pio_end(state, cmdhdl, pio_error, fm_loop_cnt, fm_status, 3765 fm_test); 3766 3767 return (DDI_SUCCESS); 3768 3769 pio_error2: 3770 /* fall through */ 3771 pio_error: 3772 hermon_fm_ereport(state, HCA_SYS_ERR, HCA_ERR_NON_FATAL); 3773 return (DDI_FAILURE); 3774 } 3775 3776 3777 /* 3778 * hermon_mcg_init() 3779 * Context: Only called from attach() path context 3780 */ 3781 static int 3782 hermon_mcg_init(hermon_state_t *state) 3783 { 3784 uint_t mcg_tmp_sz; 3785 3786 3787 /* 3788 * Allocate space for the MCG temporary copy buffer. This is 3789 * used by the Attach/Detach Multicast Group code 3790 */ 3791 mcg_tmp_sz = HERMON_MCGMEM_SZ(state); 3792 state->hs_mcgtmp = kmem_zalloc(mcg_tmp_sz, KM_SLEEP); 3793 3794 /* 3795 * Initialize the multicast group mutex. This ensures atomic 3796 * access to add, modify, and remove entries in the multicast 3797 * group hash lists. 3798 */ 3799 mutex_init(&state->hs_mcglock, NULL, MUTEX_DRIVER, 3800 DDI_INTR_PRI(state->hs_intrmsi_pri)); 3801 3802 return (DDI_SUCCESS); 3803 } 3804 3805 3806 /* 3807 * hermon_mcg_fini() 3808 * Context: Only called from attach() and/or detach() path contexts 3809 */ 3810 static void 3811 hermon_mcg_fini(hermon_state_t *state) 3812 { 3813 uint_t mcg_tmp_sz; 3814 3815 3816 /* Free up the space used for the MCG temporary copy buffer */ 3817 mcg_tmp_sz = HERMON_MCGMEM_SZ(state); 3818 kmem_free(state->hs_mcgtmp, mcg_tmp_sz); 3819 3820 /* Destroy the multicast group mutex */ 3821 mutex_destroy(&state->hs_mcglock); 3822 3823 } 3824 3825 3826 /* 3827 * hermon_fw_version_check() 3828 * Context: Only called from attach() path context 3829 */ 3830 static int 3831 hermon_fw_version_check(hermon_state_t *state) 3832 { 3833 3834 uint_t hermon_fw_ver_major; 3835 uint_t hermon_fw_ver_minor; 3836 uint_t hermon_fw_ver_subminor; 3837 3838 #ifdef FMA_TEST 3839 if (hermon_test_num == -1) { 3840 return (DDI_FAILURE); 3841 } 3842 #endif 3843 3844 /* 3845 * Depending on which version of driver we have attached, and which 3846 * HCA we've attached, the firmware version checks will be different. 3847 * We set up the comparison values for both Arbel and Sinai HCAs. 3848 */ 3849 switch (state->hs_operational_mode) { 3850 case HERMON_HCA_MODE: 3851 hermon_fw_ver_major = HERMON_FW_VER_MAJOR; 3852 hermon_fw_ver_minor = HERMON_FW_VER_MINOR; 3853 hermon_fw_ver_subminor = HERMON_FW_VER_SUBMINOR; 3854 break; 3855 3856 default: 3857 return (DDI_FAILURE); 3858 } 3859 3860 /* 3861 * If FW revision major number is less than acceptable, 3862 * return failure, else if greater return success. If 3863 * the major numbers are equal than check the minor number 3864 */ 3865 if (state->hs_fw.fw_rev_major < hermon_fw_ver_major) { 3866 return (DDI_FAILURE); 3867 } else if (state->hs_fw.fw_rev_major > hermon_fw_ver_major) { 3868 return (DDI_SUCCESS); 3869 } 3870 3871 /* 3872 * Do the same check as above, except for minor revision numbers 3873 * If the minor numbers are equal than check the subminor number 3874 */ 3875 if (state->hs_fw.fw_rev_minor < hermon_fw_ver_minor) { 3876 return (DDI_FAILURE); 3877 } else if (state->hs_fw.fw_rev_minor > hermon_fw_ver_minor) { 3878 return (DDI_SUCCESS); 3879 } 3880 3881 /* 3882 * Once again we do the same check as above, except for the subminor 3883 * revision number. If the subminor numbers are equal here, then 3884 * these are the same firmware version, return success 3885 */ 3886 if (state->hs_fw.fw_rev_subminor < hermon_fw_ver_subminor) { 3887 return (DDI_FAILURE); 3888 } else if (state->hs_fw.fw_rev_subminor > hermon_fw_ver_subminor) { 3889 return (DDI_SUCCESS); 3890 } 3891 3892 return (DDI_SUCCESS); 3893 } 3894 3895 3896 /* 3897 * hermon_device_info_report() 3898 * Context: Only called from attach() path context 3899 */ 3900 static void 3901 hermon_device_info_report(hermon_state_t *state) 3902 { 3903 3904 cmn_err(CE_CONT, "?hermon%d: FW ver: %04d.%04d.%04d, " 3905 "HW rev: %02d\n", state->hs_instance, state->hs_fw.fw_rev_major, 3906 state->hs_fw.fw_rev_minor, state->hs_fw.fw_rev_subminor, 3907 state->hs_revision_id); 3908 cmn_err(CE_CONT, "?hermon%d: %64s (0x%016" PRIx64 ")\n", 3909 state->hs_instance, state->hs_nodedesc, state->hs_nodeguid); 3910 3911 } 3912 3913 3914 /* 3915 * hermon_pci_capability_list() 3916 * Context: Only called from attach() path context 3917 */ 3918 static int 3919 hermon_pci_capability_list(hermon_state_t *state, ddi_acc_handle_t hdl) 3920 { 3921 uint_t offset, data; 3922 uint32_t data32; 3923 3924 state->hs_pci_cap_offset = 0; /* make sure it's cleared */ 3925 3926 /* 3927 * Check for the "PCI Capabilities" bit in the "Status Register". 3928 * Bit 4 in this register indicates the presence of a "PCI 3929 * Capabilities" list. 3930 * 3931 * PCI-Express requires this bit to be set to 1. 3932 */ 3933 data = pci_config_get16(hdl, 0x06); 3934 if ((data & 0x10) == 0) { 3935 return (DDI_FAILURE); 3936 } 3937 3938 /* 3939 * Starting from offset 0x34 in PCI config space, find the 3940 * head of "PCI capabilities" list, and walk the list. If 3941 * capabilities of a known type are encountered (e.g. 3942 * "PCI-X Capability"), then call the appropriate handler 3943 * function. 3944 */ 3945 offset = pci_config_get8(hdl, 0x34); 3946 while (offset != 0x0) { 3947 data = pci_config_get8(hdl, offset); 3948 /* 3949 * Check for known capability types. Hermon has the 3950 * following: 3951 * o Power Mgmt (0x02) 3952 * o VPD Capability (0x03) 3953 * o PCI-E Capability (0x10) 3954 * o MSIX Capability (0x11) 3955 */ 3956 switch (data) { 3957 case 0x01: 3958 /* power mgmt handling */ 3959 break; 3960 case 0x03: 3961 3962 /* 3963 * Reading the PCIe VPD is inconsistent - that is, sometimes causes 3964 * problems on (mostly) X64, though we've also seen problems w/ Sparc 3965 * and Tavor --- so, for now until it's root caused, don't try and 3966 * read it 3967 */ 3968 #ifdef HERMON_VPD_WORKS 3969 hermon_pci_capability_vpd(state, hdl, offset); 3970 #else 3971 delay(100); 3972 hermon_pci_capability_vpd(state, hdl, offset); 3973 #endif 3974 break; 3975 case 0x10: 3976 /* 3977 * PCI Express Capability - save offset & contents 3978 * for later in reset 3979 */ 3980 state->hs_pci_cap_offset = offset; 3981 data32 = pci_config_get32(hdl, 3982 offset + HERMON_PCI_CAP_DEV_OFFS); 3983 state->hs_pci_cap_devctl = data32; 3984 data32 = pci_config_get32(hdl, 3985 offset + HERMON_PCI_CAP_LNK_OFFS); 3986 state->hs_pci_cap_lnkctl = data32; 3987 break; 3988 case 0x11: 3989 /* 3990 * MSIX support - nothing to do, taken care of in the 3991 * MSI/MSIX interrupt frameworkd 3992 */ 3993 break; 3994 default: 3995 /* just go on to the next */ 3996 break; 3997 } 3998 3999 /* Get offset of next entry in list */ 4000 offset = pci_config_get8(hdl, offset + 1); 4001 } 4002 4003 return (DDI_SUCCESS); 4004 } 4005 4006 /* 4007 * hermon_pci_read_vpd() 4008 * Context: Only called from attach() path context 4009 * utility routine for hermon_pci_capability_vpd() 4010 */ 4011 static int 4012 hermon_pci_read_vpd(ddi_acc_handle_t hdl, uint_t offset, uint32_t addr, 4013 uint32_t *data) 4014 { 4015 int retry = 40; /* retry counter for EEPROM poll */ 4016 uint32_t val; 4017 int vpd_addr = offset + 2; 4018 int vpd_data = offset + 4; 4019 4020 /* 4021 * In order to read a 32-bit value from VPD, we are to write down 4022 * the address (offset in the VPD itself) to the address register. 4023 * To signal the read, we also clear bit 31. We then poll on bit 31 4024 * and when it is set, we can then read our 4 bytes from the data 4025 * register. 4026 */ 4027 (void) pci_config_put32(hdl, offset, addr << 16); 4028 do { 4029 drv_usecwait(1000); 4030 val = pci_config_get16(hdl, vpd_addr); 4031 if (val & 0x8000) { /* flag bit set */ 4032 *data = pci_config_get32(hdl, vpd_data); 4033 return (DDI_SUCCESS); 4034 } 4035 } while (--retry); 4036 /* read of flag failed write one message but count the failures */ 4037 if (debug_vpd == 0) 4038 cmn_err(CE_NOTE, 4039 "!Failed to see flag bit after VPD addr write\n"); 4040 debug_vpd++; 4041 4042 4043 vpd_read_fail: 4044 return (DDI_FAILURE); 4045 } 4046 4047 4048 4049 /* 4050 * hermon_pci_capability_vpd() 4051 * Context: Only called from attach() path context 4052 */ 4053 static void 4054 hermon_pci_capability_vpd(hermon_state_t *state, ddi_acc_handle_t hdl, 4055 uint_t offset) 4056 { 4057 uint8_t name_length; 4058 uint8_t pn_length; 4059 int i, err = 0; 4060 int vpd_str_id = 0; 4061 int vpd_ro_desc; 4062 int vpd_ro_pn_desc; 4063 #ifdef _BIG_ENDIAN 4064 uint32_t data32; 4065 #endif /* _BIG_ENDIAN */ 4066 union { 4067 uint32_t vpd_int[HERMON_VPD_HDR_DWSIZE]; 4068 uchar_t vpd_char[HERMON_VPD_HDR_BSIZE]; 4069 } vpd; 4070 4071 4072 /* 4073 * Read in the Vital Product Data (VPD) to the extend needed 4074 * by the fwflash utility 4075 */ 4076 for (i = 0; i < HERMON_VPD_HDR_DWSIZE; i++) { 4077 err = hermon_pci_read_vpd(hdl, offset, i << 2, &vpd.vpd_int[i]); 4078 if (err != DDI_SUCCESS) { 4079 cmn_err(CE_NOTE, "!VPD read failed\n"); 4080 goto out; 4081 } 4082 } 4083 4084 #ifdef _BIG_ENDIAN 4085 /* Need to swap bytes for big endian. */ 4086 for (i = 0; i < HERMON_VPD_HDR_DWSIZE; i++) { 4087 data32 = vpd.vpd_int[i]; 4088 vpd.vpd_char[(i << 2) + 3] = 4089 (uchar_t)((data32 & 0xFF000000) >> 24); 4090 vpd.vpd_char[(i << 2) + 2] = 4091 (uchar_t)((data32 & 0x00FF0000) >> 16); 4092 vpd.vpd_char[(i << 2) + 1] = 4093 (uchar_t)((data32 & 0x0000FF00) >> 8); 4094 vpd.vpd_char[i << 2] = (uchar_t)(data32 & 0x000000FF); 4095 } 4096 #endif /* _BIG_ENDIAN */ 4097 4098 /* Check for VPD String ID Tag */ 4099 if (vpd.vpd_char[vpd_str_id] == 0x82) { 4100 /* get the product name */ 4101 name_length = (uint8_t)vpd.vpd_char[vpd_str_id + 1]; 4102 if (name_length > sizeof (state->hs_hca_name)) { 4103 cmn_err(CE_NOTE, "!VPD name too large (0x%x)\n", 4104 name_length); 4105 goto out; 4106 } 4107 (void) memcpy(state->hs_hca_name, &vpd.vpd_char[vpd_str_id + 3], 4108 name_length); 4109 state->hs_hca_name[name_length] = 0; 4110 4111 /* get the part number */ 4112 vpd_ro_desc = name_length + 3; /* read-only tag location */ 4113 vpd_ro_pn_desc = vpd_ro_desc + 3; /* P/N keyword location */ 4114 4115 /* Verify read-only tag and Part Number keyword. */ 4116 if (vpd.vpd_char[vpd_ro_desc] != 0x90 || 4117 (vpd.vpd_char[vpd_ro_pn_desc] != 'P' && 4118 vpd.vpd_char[vpd_ro_pn_desc + 1] != 'N')) { 4119 cmn_err(CE_NOTE, "!VPD Part Number not found\n"); 4120 goto out; 4121 } 4122 4123 pn_length = (uint8_t)vpd.vpd_char[vpd_ro_pn_desc + 2]; 4124 if (pn_length > sizeof (state->hs_hca_pn)) { 4125 cmn_err(CE_NOTE, "!VPD part number too large (0x%x)\n", 4126 name_length); 4127 goto out; 4128 } 4129 (void) memcpy(state->hs_hca_pn, 4130 &vpd.vpd_char[vpd_ro_pn_desc + 3], 4131 pn_length); 4132 state->hs_hca_pn[pn_length] = 0; 4133 state->hs_hca_pn_len = pn_length; 4134 cmn_err(CE_CONT, "!vpd %s\n", state->hs_hca_pn); 4135 } else { 4136 /* Wrong VPD String ID Tag */ 4137 cmn_err(CE_NOTE, "!VPD String ID Tag not found, tag: %02x\n", 4138 vpd.vpd_char[0]); 4139 goto out; 4140 } 4141 return; 4142 out: 4143 state->hs_hca_pn_len = 0; 4144 } 4145 4146 4147 4148 /* 4149 * hermon_intr_or_msi_init() 4150 * Context: Only called from attach() path context 4151 */ 4152 static int 4153 hermon_intr_or_msi_init(hermon_state_t *state) 4154 { 4155 int status; 4156 4157 4158 /* Query for the list of supported interrupt event types */ 4159 status = ddi_intr_get_supported_types(state->hs_dip, 4160 &state->hs_intr_types_avail); 4161 if (status != DDI_SUCCESS) { 4162 return (DDI_FAILURE); 4163 } 4164 4165 /* 4166 * If Hermon supports MSI-X in this system (and, if it 4167 * hasn't been overridden by a configuration variable), then 4168 * the default behavior is to use a single MSI-X. Otherwise, 4169 * fallback to using legacy interrupts. Also, if MSI-X is chosen, 4170 * but fails for whatever reasons, then next try MSI 4171 */ 4172 if ((state->hs_cfg_profile->cp_use_msi_if_avail != 0) && 4173 (state->hs_intr_types_avail & DDI_INTR_TYPE_MSIX)) { 4174 status = hermon_add_intrs(state, DDI_INTR_TYPE_MSIX); 4175 if (status == DDI_SUCCESS) { 4176 state->hs_intr_type_chosen = DDI_INTR_TYPE_MSIX; 4177 return (DDI_SUCCESS); 4178 } 4179 } 4180 4181 /* 4182 * If Hermon supports MSI in this system (and, if it 4183 * hasn't been overridden by a configuration variable), then 4184 * the default behavior is to use a single MSIX. Otherwise, 4185 * fallback to using legacy interrupts. Also, if MSI is chosen, 4186 * but fails for whatever reasons, then fallback to using legacy 4187 * interrupts. 4188 */ 4189 if ((state->hs_cfg_profile->cp_use_msi_if_avail != 0) && 4190 (state->hs_intr_types_avail & DDI_INTR_TYPE_MSI)) { 4191 status = hermon_add_intrs(state, DDI_INTR_TYPE_MSI); 4192 if (status == DDI_SUCCESS) { 4193 state->hs_intr_type_chosen = DDI_INTR_TYPE_MSI; 4194 return (DDI_SUCCESS); 4195 } 4196 } 4197 4198 /* 4199 * MSI interrupt allocation failed, or was not available. Fallback to 4200 * legacy interrupt support. 4201 */ 4202 if (state->hs_intr_types_avail & DDI_INTR_TYPE_FIXED) { 4203 status = hermon_add_intrs(state, DDI_INTR_TYPE_FIXED); 4204 if (status == DDI_SUCCESS) { 4205 state->hs_intr_type_chosen = DDI_INTR_TYPE_FIXED; 4206 return (DDI_SUCCESS); 4207 } 4208 } 4209 4210 /* 4211 * None of MSI, MSI-X, nor legacy interrupts were successful. 4212 * Return failure. 4213 */ 4214 return (DDI_FAILURE); 4215 } 4216 4217 /* 4218 * hermon_add_intrs() 4219 * Context: Only called from attach() patch context 4220 */ 4221 static int 4222 hermon_add_intrs(hermon_state_t *state, int intr_type) 4223 { 4224 int status; 4225 4226 4227 /* Get number of interrupts/MSI supported */ 4228 status = ddi_intr_get_nintrs(state->hs_dip, intr_type, 4229 &state->hs_intrmsi_count); 4230 if (status != DDI_SUCCESS) { 4231 return (DDI_FAILURE); 4232 } 4233 4234 /* Get number of available interrupts/MSI */ 4235 status = ddi_intr_get_navail(state->hs_dip, intr_type, 4236 &state->hs_intrmsi_avail); 4237 if (status != DDI_SUCCESS) { 4238 return (DDI_FAILURE); 4239 } 4240 4241 /* Ensure that we have at least one (1) usable MSI or interrupt */ 4242 if ((state->hs_intrmsi_avail < 1) || (state->hs_intrmsi_count < 1)) { 4243 return (DDI_FAILURE); 4244 } 4245 4246 /* Attempt to allocate the maximum #interrupt/MSI handles */ 4247 status = ddi_intr_alloc(state->hs_dip, &state->hs_intrmsi_hdl[0], 4248 intr_type, 0, min(HERMON_MSIX_MAX, state->hs_intrmsi_avail), 4249 &state->hs_intrmsi_allocd, DDI_INTR_ALLOC_NORMAL); 4250 if (status != DDI_SUCCESS) { 4251 return (DDI_FAILURE); 4252 } 4253 4254 /* Ensure that we have allocated at least one (1) MSI or interrupt */ 4255 if (state->hs_intrmsi_allocd < 1) { 4256 return (DDI_FAILURE); 4257 } 4258 state->hs_eq_dist = state->hs_intrmsi_allocd - 1; /* start at 0 */ 4259 4260 /* 4261 * Extract the priority for the allocated interrupt/MSI. This 4262 * will be used later when initializing certain mutexes. 4263 */ 4264 status = ddi_intr_get_pri(state->hs_intrmsi_hdl[0], 4265 &state->hs_intrmsi_pri); 4266 if (status != DDI_SUCCESS) { 4267 /* Free the allocated interrupt/MSI handle */ 4268 (void) ddi_intr_free(state->hs_intrmsi_hdl[0]); 4269 4270 return (DDI_FAILURE); 4271 } 4272 4273 /* Make sure the interrupt/MSI priority is below 'high level' */ 4274 if (state->hs_intrmsi_pri >= ddi_intr_get_hilevel_pri()) { 4275 /* Free the allocated interrupt/MSI handle */ 4276 (void) ddi_intr_free(state->hs_intrmsi_hdl[0]); 4277 4278 return (DDI_FAILURE); 4279 } 4280 4281 /* Get add'l capability information regarding interrupt/MSI */ 4282 status = ddi_intr_get_cap(state->hs_intrmsi_hdl[0], 4283 &state->hs_intrmsi_cap); 4284 if (status != DDI_SUCCESS) { 4285 /* Free the allocated interrupt/MSI handle */ 4286 (void) ddi_intr_free(state->hs_intrmsi_hdl[0]); 4287 4288 return (DDI_FAILURE); 4289 } 4290 4291 return (DDI_SUCCESS); 4292 } 4293 4294 4295 /* 4296 * hermon_intr_or_msi_fini() 4297 * Context: Only called from attach() and/or detach() path contexts 4298 */ 4299 static int 4300 hermon_intr_or_msi_fini(hermon_state_t *state) 4301 { 4302 int status; 4303 int intr; 4304 4305 for (intr = 0; intr < state->hs_intrmsi_allocd; intr++) { 4306 4307 /* Free the allocated interrupt/MSI handle */ 4308 status = ddi_intr_free(state->hs_intrmsi_hdl[intr]); 4309 if (status != DDI_SUCCESS) { 4310 return (DDI_FAILURE); 4311 } 4312 } 4313 return (DDI_SUCCESS); 4314 } 4315 4316 4317 /*ARGSUSED*/ 4318 void 4319 hermon_pci_capability_msix(hermon_state_t *state, ddi_acc_handle_t hdl, 4320 uint_t offset) 4321 { 4322 uint32_t msix_data; 4323 uint16_t msg_cntr; 4324 uint32_t t_offset; /* table offset */ 4325 uint32_t t_bir; 4326 uint32_t p_offset; /* pba */ 4327 uint32_t p_bir; 4328 int t_size; /* size in entries - each is 4 dwords */ 4329 4330 /* come in with offset pointing at the capability structure */ 4331 4332 msix_data = pci_config_get32(hdl, offset); 4333 cmn_err(CE_CONT, "Full cap structure dword = %X\n", msix_data); 4334 msg_cntr = pci_config_get16(hdl, offset+2); 4335 cmn_err(CE_CONT, "MSIX msg_control = %X\n", msg_cntr); 4336 offset += 4; 4337 msix_data = pci_config_get32(hdl, offset); /* table info */ 4338 t_offset = (msix_data & 0xFFF8) >> 3; 4339 t_bir = msix_data & 0x07; 4340 offset += 4; 4341 cmn_err(CE_CONT, " table %X --offset = %X, bir(bar) = %X\n", 4342 msix_data, t_offset, t_bir); 4343 msix_data = pci_config_get32(hdl, offset); /* PBA info */ 4344 p_offset = (msix_data & 0xFFF8) >> 3; 4345 p_bir = msix_data & 0x07; 4346 4347 cmn_err(CE_CONT, " PBA %X --offset = %X, bir(bar) = %X\n", 4348 msix_data, p_offset, p_bir); 4349 t_size = msg_cntr & 0x7FF; /* low eleven bits */ 4350 cmn_err(CE_CONT, " table size = %X entries\n", t_size); 4351 4352 offset = t_offset; /* reuse this for offset from BAR */ 4353 #ifdef HERMON_SUPPORTS_MSIX_BAR 4354 cmn_err(CE_CONT, "First 2 table entries behind BAR2 \n"); 4355 for (i = 0; i < 2; i++) { 4356 for (j = 0; j < 4; j++, offset += 4) { 4357 msix_data = ddi_get32(state->hs_reg_msihdl, 4358 (uint32_t *)((uintptr_t)state->hs_reg_msi_baseaddr 4359 + offset)); 4360 cmn_err(CE_CONT, "MSI table entry %d, dword %d == %X\n", 4361 i, j, msix_data); 4362 } 4363 } 4364 #endif 4365 4366 } 4367 4368 /* 4369 * X86 fastreboot support functions. 4370 * These functions are used to save/restore MSI-X table/PBA and also 4371 * to disable MSI-X interrupts in hermon_quiesce(). 4372 */ 4373 4374 /* Return the message control for MSI-X */ 4375 static ushort_t 4376 get_msix_ctrl(dev_info_t *dip) 4377 { 4378 ushort_t msix_ctrl = 0, caps_ctrl = 0; 4379 hermon_state_t *state = ddi_get_soft_state(hermon_statep, 4380 DEVI(dip)->devi_instance); 4381 ddi_acc_handle_t pci_cfg_hdl = hermon_get_pcihdl(state); 4382 ASSERT(pci_cfg_hdl != NULL); 4383 4384 if ((PCI_CAP_LOCATE(pci_cfg_hdl, 4385 PCI_CAP_ID_MSI_X, &caps_ctrl) == DDI_SUCCESS)) { 4386 if ((msix_ctrl = PCI_CAP_GET16(pci_cfg_hdl, NULL, caps_ctrl, 4387 PCI_MSIX_CTRL)) == PCI_CAP_EINVAL16) 4388 return (0); 4389 } 4390 ASSERT(msix_ctrl != 0); 4391 4392 return (msix_ctrl); 4393 } 4394 4395 /* Return the MSI-X table size */ 4396 static size_t 4397 get_msix_tbl_size(dev_info_t *dip) 4398 { 4399 ushort_t msix_ctrl = get_msix_ctrl(dip); 4400 ASSERT(msix_ctrl != 0); 4401 4402 return (((msix_ctrl & PCI_MSIX_TBL_SIZE_MASK) + 1) * 4403 PCI_MSIX_VECTOR_SIZE); 4404 } 4405 4406 /* Return the MSI-X PBA size */ 4407 static size_t 4408 get_msix_pba_size(dev_info_t *dip) 4409 { 4410 ushort_t msix_ctrl = get_msix_ctrl(dip); 4411 ASSERT(msix_ctrl != 0); 4412 4413 return (((msix_ctrl & PCI_MSIX_TBL_SIZE_MASK) + 64) / 64 * 8); 4414 } 4415 4416 /* Set up the MSI-X table/PBA save area */ 4417 static void 4418 hermon_set_msix_info(hermon_state_t *state) 4419 { 4420 uint_t rnumber, breg, nregs; 4421 ushort_t caps_ctrl, msix_ctrl; 4422 pci_regspec_t *rp; 4423 int reg_size, addr_space, offset, *regs_list, i; 4424 4425 /* 4426 * MSI-X BIR Index Table: 4427 * BAR indicator register (BIR) to Base Address register. 4428 */ 4429 uchar_t pci_msix_bir_index[8] = {0x10, 0x14, 0x18, 0x1c, 4430 0x20, 0x24, 0xff, 0xff}; 4431 4432 /* Fastreboot data access attribute */ 4433 ddi_device_acc_attr_t dev_attr = { 4434 0, /* version */ 4435 DDI_STRUCTURE_LE_ACC, 4436 DDI_STRICTORDER_ACC, /* attr access */ 4437 0 4438 }; 4439 4440 ddi_acc_handle_t pci_cfg_hdl = hermon_get_pcihdl(state); 4441 ASSERT(pci_cfg_hdl != NULL); 4442 4443 if ((PCI_CAP_LOCATE(pci_cfg_hdl, 4444 PCI_CAP_ID_MSI_X, &caps_ctrl) == DDI_SUCCESS)) { 4445 if ((msix_ctrl = PCI_CAP_GET16(pci_cfg_hdl, NULL, caps_ctrl, 4446 PCI_MSIX_CTRL)) == PCI_CAP_EINVAL16) 4447 return; 4448 } 4449 ASSERT(msix_ctrl != 0); 4450 4451 state->hs_msix_tbl_offset = PCI_CAP_GET32(pci_cfg_hdl, NULL, caps_ctrl, 4452 PCI_MSIX_TBL_OFFSET); 4453 4454 /* Get the BIR for MSI-X table */ 4455 breg = pci_msix_bir_index[state->hs_msix_tbl_offset & 4456 PCI_MSIX_TBL_BIR_MASK]; 4457 ASSERT(breg != 0xFF); 4458 4459 /* Set the MSI-X table offset */ 4460 state->hs_msix_tbl_offset = state->hs_msix_tbl_offset & 4461 ~PCI_MSIX_TBL_BIR_MASK; 4462 4463 /* Set the MSI-X table size */ 4464 state->hs_msix_tbl_size = ((msix_ctrl & PCI_MSIX_TBL_SIZE_MASK) + 1) * 4465 PCI_MSIX_VECTOR_SIZE; 4466 4467 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, state->hs_dip, 4468 DDI_PROP_DONTPASS, "reg", (int **)®s_list, &nregs) != 4469 DDI_PROP_SUCCESS) { 4470 return; 4471 } 4472 reg_size = sizeof (pci_regspec_t) / sizeof (int); 4473 4474 /* Check the register number for MSI-X table */ 4475 for (i = 1, rnumber = 0; i < nregs/reg_size; i++) { 4476 rp = (pci_regspec_t *)®s_list[i * reg_size]; 4477 addr_space = rp->pci_phys_hi & PCI_ADDR_MASK; 4478 offset = PCI_REG_REG_G(rp->pci_phys_hi); 4479 4480 if ((offset == breg) && ((addr_space == PCI_ADDR_MEM32) || 4481 (addr_space == PCI_ADDR_MEM64))) { 4482 rnumber = i; 4483 break; 4484 } 4485 } 4486 ASSERT(rnumber != 0); 4487 state->hs_msix_tbl_rnumber = rnumber; 4488 4489 /* Set device attribute version and access according to Hermon FM */ 4490 dev_attr.devacc_attr_version = hermon_devacc_attr_version(state); 4491 dev_attr.devacc_attr_access = hermon_devacc_attr_access(state); 4492 4493 /* Map the entire MSI-X vector table */ 4494 if (hermon_regs_map_setup(state, state->hs_msix_tbl_rnumber, 4495 (caddr_t *)&state->hs_msix_tbl_addr, state->hs_msix_tbl_offset, 4496 state->hs_msix_tbl_size, &dev_attr, 4497 &state->hs_fm_msix_tblhdl) != DDI_SUCCESS) { 4498 return; 4499 } 4500 4501 state->hs_msix_pba_offset = PCI_CAP_GET32(pci_cfg_hdl, NULL, caps_ctrl, 4502 PCI_MSIX_PBA_OFFSET); 4503 4504 /* Get the BIR for MSI-X PBA */ 4505 breg = pci_msix_bir_index[state->hs_msix_pba_offset & 4506 PCI_MSIX_PBA_BIR_MASK]; 4507 ASSERT(breg != 0xFF); 4508 4509 /* Set the MSI-X PBA offset */ 4510 state->hs_msix_pba_offset = state->hs_msix_pba_offset & 4511 ~PCI_MSIX_PBA_BIR_MASK; 4512 4513 /* Set the MSI-X PBA size */ 4514 state->hs_msix_pba_size = 4515 ((msix_ctrl & PCI_MSIX_TBL_SIZE_MASK) + 64) / 64 * 8; 4516 4517 /* Check the register number for MSI-X PBA */ 4518 for (i = 1, rnumber = 0; i < nregs/reg_size; i++) { 4519 rp = (pci_regspec_t *)®s_list[i * reg_size]; 4520 addr_space = rp->pci_phys_hi & PCI_ADDR_MASK; 4521 offset = PCI_REG_REG_G(rp->pci_phys_hi); 4522 4523 if ((offset == breg) && ((addr_space == PCI_ADDR_MEM32) || 4524 (addr_space == PCI_ADDR_MEM64))) { 4525 rnumber = i; 4526 break; 4527 } 4528 } 4529 ASSERT(rnumber != 0); 4530 state->hs_msix_pba_rnumber = rnumber; 4531 4532 /* Map in the MSI-X Pending Bit Array */ 4533 if (hermon_regs_map_setup(state, state->hs_msix_pba_rnumber, 4534 (caddr_t *)&state->hs_msix_pba_addr, state->hs_msix_pba_offset, 4535 state->hs_msix_pba_size, &dev_attr, 4536 &state->hs_fm_msix_pbahdl) != DDI_SUCCESS) { 4537 hermon_regs_map_free(state, &state->hs_fm_msix_tblhdl); 4538 state->hs_fm_msix_tblhdl = NULL; 4539 return; 4540 } 4541 4542 /* Set the MSI-X table save area */ 4543 state->hs_msix_tbl_entries = kmem_alloc(state->hs_msix_tbl_size, 4544 KM_SLEEP); 4545 4546 /* Set the MSI-X PBA save area */ 4547 state->hs_msix_pba_entries = kmem_alloc(state->hs_msix_pba_size, 4548 KM_SLEEP); 4549 } 4550 4551 /* Disable Hermon interrupts */ 4552 static int 4553 hermon_intr_disable(hermon_state_t *state) 4554 { 4555 ushort_t msix_ctrl = 0, caps_ctrl = 0; 4556 ddi_acc_handle_t pci_cfg_hdl = hermon_get_pcihdl(state); 4557 ddi_acc_handle_t msix_tblhdl = hermon_get_msix_tblhdl(state); 4558 int i, j; 4559 ASSERT(pci_cfg_hdl != NULL && msix_tblhdl != NULL); 4560 ASSERT(state->hs_intr_types_avail & 4561 (DDI_INTR_TYPE_FIXED | DDI_INTR_TYPE_MSI | DDI_INTR_TYPE_MSIX)); 4562 4563 /* 4564 * Check if MSI-X interrupts are used. If so, disable MSI-X interupts. 4565 * If not, since Hermon doesn't support MSI interrupts, assuming the 4566 * legacy interrupt is used instead, disable the legacy interrupt. 4567 */ 4568 if ((state->hs_cfg_profile->cp_use_msi_if_avail != 0) && 4569 (state->hs_intr_types_avail & DDI_INTR_TYPE_MSIX)) { 4570 4571 if ((PCI_CAP_LOCATE(pci_cfg_hdl, 4572 PCI_CAP_ID_MSI_X, &caps_ctrl) == DDI_SUCCESS)) { 4573 if ((msix_ctrl = PCI_CAP_GET16(pci_cfg_hdl, NULL, 4574 caps_ctrl, PCI_MSIX_CTRL)) == PCI_CAP_EINVAL16) 4575 return (DDI_FAILURE); 4576 } 4577 ASSERT(msix_ctrl != 0); 4578 4579 if (!(msix_ctrl & PCI_MSIX_ENABLE_BIT)) 4580 return (DDI_SUCCESS); 4581 4582 /* Clear all inums in MSI-X table */ 4583 for (i = 0; i < get_msix_tbl_size(state->hs_dip); 4584 i += PCI_MSIX_VECTOR_SIZE) { 4585 for (j = 0; j < PCI_MSIX_VECTOR_SIZE; j += 4) { 4586 char *addr = state->hs_msix_tbl_addr + i + j; 4587 ddi_put32(msix_tblhdl, 4588 (uint32_t *)(uintptr_t)addr, 0x0); 4589 } 4590 } 4591 4592 /* Disable MSI-X interrupts */ 4593 msix_ctrl &= ~PCI_MSIX_ENABLE_BIT; 4594 PCI_CAP_PUT16(pci_cfg_hdl, NULL, caps_ctrl, PCI_MSIX_CTRL, 4595 msix_ctrl); 4596 4597 } else { 4598 uint16_t cmdreg = pci_config_get16(pci_cfg_hdl, PCI_CONF_COMM); 4599 ASSERT(state->hs_intr_types_avail & DDI_INTR_TYPE_FIXED); 4600 4601 /* Disable the legacy interrupts */ 4602 cmdreg |= PCI_COMM_INTX_DISABLE; 4603 pci_config_put16(pci_cfg_hdl, PCI_CONF_COMM, cmdreg); 4604 } 4605 4606 return (DDI_SUCCESS); 4607 } 4608 4609 /* Hermon quiesce(9F) entry */ 4610 static int 4611 hermon_quiesce(dev_info_t *dip) 4612 { 4613 hermon_state_t *state = ddi_get_soft_state(hermon_statep, 4614 DEVI(dip)->devi_instance); 4615 ddi_acc_handle_t pcihdl = hermon_get_pcihdl(state); 4616 ddi_acc_handle_t cmdhdl = hermon_get_cmdhdl(state); 4617 ddi_acc_handle_t msix_tbl_hdl = hermon_get_msix_tblhdl(state); 4618 ddi_acc_handle_t msix_pba_hdl = hermon_get_msix_pbahdl(state); 4619 uint32_t sem, reset_delay = state->hs_cfg_profile->cp_sw_reset_delay; 4620 uint64_t data64; 4621 uint32_t data32; 4622 int status, i, j, loopcnt; 4623 uint_t offset; 4624 4625 ASSERT(state != NULL); 4626 4627 /* start fastreboot */ 4628 state->hs_quiescing = B_TRUE; 4629 4630 /* If it's in maintenance mode, do nothing but return with SUCCESS */ 4631 if (!HERMON_IS_OPERATIONAL(state->hs_operational_mode)) { 4632 return (DDI_SUCCESS); 4633 } 4634 4635 /* suppress Hermon FM ereports */ 4636 if (hermon_get_state(state) & HCA_EREPORT_FM) { 4637 hermon_clr_state_nolock(state, HCA_EREPORT_FM); 4638 } 4639 4640 /* Shutdown HCA ports */ 4641 if (hermon_hca_ports_shutdown(state, 4642 state->hs_cfg_profile->cp_num_ports) != HERMON_CMD_SUCCESS) { 4643 state->hs_quiescing = B_FALSE; 4644 return (DDI_FAILURE); 4645 } 4646 4647 /* Close HCA */ 4648 if (hermon_close_hca_cmd_post(state, HERMON_CMD_NOSLEEP_SPIN) != 4649 HERMON_CMD_SUCCESS) { 4650 state->hs_quiescing = B_FALSE; 4651 return (DDI_FAILURE); 4652 } 4653 4654 /* Disable interrupts */ 4655 if (hermon_intr_disable(state) != DDI_SUCCESS) { 4656 state->hs_quiescing = B_FALSE; 4657 return (DDI_FAILURE); 4658 } 4659 4660 /* 4661 * Query the PCI capabilities of the HCA device, but don't process 4662 * the VPD until after reset. 4663 */ 4664 if (hermon_pci_capability_list(state, pcihdl) != DDI_SUCCESS) { 4665 state->hs_quiescing = B_FALSE; 4666 return (DDI_FAILURE); 4667 } 4668 4669 /* 4670 * Read all PCI config info (reg0...reg63). Note: According to the 4671 * Hermon software reset application note, we should not read or 4672 * restore the values in reg22 and reg23. 4673 * NOTE: For Hermon (and Arbel too) it says to restore the command 4674 * register LAST, and technically, you need to restore the 4675 * PCIE Capability "device control" and "link control" (word-sized, 4676 * at offsets 0x08 and 0x10 from the capbility ID respectively). 4677 * We hold off restoring the command register - offset 0x4 - till last 4678 */ 4679 4680 /* 1st, wait for the semaphore assure accessibility - per PRM */ 4681 status = -1; 4682 for (i = 0; i < NANOSEC/MICROSEC /* 1sec timeout */; i++) { 4683 sem = ddi_get32(cmdhdl, state->hs_cmd_regs.sw_semaphore); 4684 if (sem == 0) { 4685 status = 0; 4686 break; 4687 } 4688 drv_usecwait(1); 4689 } 4690 4691 /* Check if timeout happens */ 4692 if (status == -1) { 4693 state->hs_quiescing = B_FALSE; 4694 return (DDI_FAILURE); 4695 } 4696 4697 /* MSI-X interrupts are used, save the MSI-X table */ 4698 if (msix_tbl_hdl && msix_pba_hdl) { 4699 /* save MSI-X table */ 4700 for (i = 0; i < get_msix_tbl_size(state->hs_dip); 4701 i += PCI_MSIX_VECTOR_SIZE) { 4702 for (j = 0; j < PCI_MSIX_VECTOR_SIZE; j += 4) { 4703 char *addr = state->hs_msix_tbl_addr + i + j; 4704 data32 = ddi_get32(msix_tbl_hdl, 4705 (uint32_t *)(uintptr_t)addr); 4706 *(uint32_t *)(uintptr_t)(state-> 4707 hs_msix_tbl_entries + i + j) = data32; 4708 } 4709 } 4710 /* save MSI-X PBA */ 4711 for (i = 0; i < get_msix_pba_size(state->hs_dip); i += 8) { 4712 char *addr = state->hs_msix_pba_addr + i; 4713 data64 = ddi_get64(msix_pba_hdl, 4714 (uint64_t *)(uintptr_t)addr); 4715 *(uint64_t *)(uintptr_t)(state-> 4716 hs_msix_pba_entries + i) = data64; 4717 } 4718 } 4719 4720 /* save PCI config space */ 4721 for (i = 0; i < HERMON_SW_RESET_NUMREGS; i++) { 4722 if ((i != HERMON_SW_RESET_REG22_RSVD) && 4723 (i != HERMON_SW_RESET_REG23_RSVD)) { 4724 state->hs_cfg_data[i] = 4725 pci_config_get32(pcihdl, i << 2); 4726 } 4727 } 4728 4729 /* SW-reset HCA */ 4730 ddi_put32(cmdhdl, state->hs_cmd_regs.sw_reset, HERMON_SW_RESET_START); 4731 4732 /* 4733 * This delay is required so as not to cause a panic here. If the 4734 * device is accessed too soon after reset it will not respond to 4735 * config cycles, causing a Master Abort and panic. 4736 */ 4737 drv_usecwait(reset_delay); 4738 4739 /* Poll waiting for the device to finish resetting */ 4740 loopcnt = 100; /* 100 times @ 100 usec - total delay 10 msec */ 4741 while ((pci_config_get32(pcihdl, 0) & 0x0000FFFF) != PCI_VENID_MLX) { 4742 drv_usecwait(HERMON_SW_RESET_POLL_DELAY); 4743 if (--loopcnt == 0) 4744 break; /* just in case, break and go on */ 4745 } 4746 if (loopcnt == 0) { 4747 state->hs_quiescing = B_FALSE; 4748 return (DDI_FAILURE); 4749 } 4750 4751 /* Restore the config info */ 4752 for (i = 0; i < HERMON_SW_RESET_NUMREGS; i++) { 4753 if (i == 1) continue; /* skip the status/ctrl reg */ 4754 if ((i != HERMON_SW_RESET_REG22_RSVD) && 4755 (i != HERMON_SW_RESET_REG23_RSVD)) { 4756 pci_config_put32(pcihdl, i << 2, state->hs_cfg_data[i]); 4757 } 4758 } 4759 4760 /* If MSI-X interrupts are used, restore the MSI-X table */ 4761 if (msix_tbl_hdl && msix_pba_hdl) { 4762 /* restore MSI-X PBA */ 4763 for (i = 0; i < get_msix_pba_size(state->hs_dip); i += 8) { 4764 char *addr = state->hs_msix_pba_addr + i; 4765 data64 = *(uint64_t *)(uintptr_t) 4766 (state->hs_msix_pba_entries + i); 4767 ddi_put64(msix_pba_hdl, 4768 (uint64_t *)(uintptr_t)addr, data64); 4769 } 4770 /* restore MSI-X table */ 4771 for (i = 0; i < get_msix_tbl_size(state->hs_dip); 4772 i += PCI_MSIX_VECTOR_SIZE) { 4773 for (j = 0; j < PCI_MSIX_VECTOR_SIZE; j += 4) { 4774 char *addr = state->hs_msix_tbl_addr + i + j; 4775 data32 = *(uint32_t *)(uintptr_t) 4776 (state->hs_msix_tbl_entries + i + j); 4777 ddi_put32(msix_tbl_hdl, 4778 (uint32_t *)(uintptr_t)addr, data32); 4779 } 4780 } 4781 } 4782 4783 /* 4784 * PCI Express Capability - we saved during capability list, and 4785 * we'll restore them here. 4786 */ 4787 offset = state->hs_pci_cap_offset; 4788 data32 = state->hs_pci_cap_devctl; 4789 pci_config_put32(pcihdl, offset + HERMON_PCI_CAP_DEV_OFFS, data32); 4790 data32 = state->hs_pci_cap_lnkctl; 4791 pci_config_put32(pcihdl, offset + HERMON_PCI_CAP_LNK_OFFS, data32); 4792 4793 /* restore the command register */ 4794 pci_config_put32(pcihdl, 0x04, (state->hs_cfg_data[1] | 0x0006)); 4795 4796 return (DDI_SUCCESS); 4797 } 4798