1 /* 2 * megaraid_sas.c: source for mega_sas driver 3 * 4 * MegaRAID device driver for SAS controllers 5 * Copyright (c) 2005-2008, LSI Logic Corporation. 6 * All rights reserved. 7 * 8 * Version: 9 * Author: 10 * Rajesh Prabhakaran<Rajesh.Prabhakaran@lsil.com> 11 * Seokmann Ju 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions are met: 15 * 16 * 1. Redistributions of source code must retain the above copyright notice, 17 * this list of conditions and the following disclaimer. 18 * 19 * 2. Redistributions in binary form must reproduce the above copyright notice, 20 * this list of conditions and the following disclaimer in the documentation 21 * and/or other materials provided with the distribution. 22 * 23 * 3. Neither the name of the author nor the names of its contributors may be 24 * used to endorse or promote products derived from this software without 25 * specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 35 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 37 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 38 * DAMAGE. 39 */ 40 41 /* 42 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 43 * Use is subject to license terms. 44 */ 45 46 #include <sys/types.h> 47 #include <sys/param.h> 48 #include <sys/file.h> 49 #include <sys/errno.h> 50 #include <sys/open.h> 51 #include <sys/cred.h> 52 #include <sys/modctl.h> 53 #include <sys/conf.h> 54 #include <sys/devops.h> 55 #include <sys/cmn_err.h> 56 #include <sys/kmem.h> 57 #include <sys/stat.h> 58 #include <sys/mkdev.h> 59 #include <sys/pci.h> 60 #include <sys/scsi/scsi.h> 61 #include <sys/ddi.h> 62 #include <sys/sunddi.h> 63 #include <sys/atomic.h> 64 #include <sys/signal.h> 65 66 #include "megaraid_sas.h" 67 68 /* 69 * FMA header files 70 */ 71 #include <sys/ddifm.h> 72 #include <sys/fm/protocol.h> 73 #include <sys/fm/util.h> 74 #include <sys/fm/io/ddi.h> 75 76 /* 77 * Local static data 78 */ 79 static void *megasas_state = NULL; 80 static int debug_level_g = CL_ANN; 81 82 #pragma weak scsi_hba_open 83 #pragma weak scsi_hba_close 84 #pragma weak scsi_hba_ioctl 85 86 static ddi_dma_attr_t megasas_generic_dma_attr = { 87 DMA_ATTR_V0, /* dma_attr_version */ 88 0, /* low DMA address range */ 89 0xFFFFFFFFU, /* high DMA address range */ 90 0xFFFFFFFFU, /* DMA counter register */ 91 8, /* DMA address alignment */ 92 0x07, /* DMA burstsizes */ 93 1, /* min DMA size */ 94 0xFFFFFFFFU, /* max DMA size */ 95 0xFFFFFFFFU, /* segment boundary */ 96 MEGASAS_MAX_SGE_CNT, /* dma_attr_sglen */ 97 512, /* granularity of device */ 98 0 /* bus specific DMA flags */ 99 }; 100 101 int32_t megasas_max_cap_maxxfer = 0x1000000; 102 103 /* 104 * cb_ops contains base level routines 105 */ 106 static struct cb_ops megasas_cb_ops = { 107 megasas_open, /* open */ 108 megasas_close, /* close */ 109 nodev, /* strategy */ 110 nodev, /* print */ 111 nodev, /* dump */ 112 nodev, /* read */ 113 nodev, /* write */ 114 megasas_ioctl, /* ioctl */ 115 nodev, /* devmap */ 116 nodev, /* mmap */ 117 nodev, /* segmap */ 118 nochpoll, /* poll */ 119 nodev, /* cb_prop_op */ 120 0, /* streamtab */ 121 D_NEW | D_HOTPLUG, /* cb_flag */ 122 CB_REV, /* cb_rev */ 123 nodev, /* cb_aread */ 124 nodev /* cb_awrite */ 125 }; 126 127 /* 128 * dev_ops contains configuration routines 129 */ 130 static struct dev_ops megasas_ops = { 131 DEVO_REV, /* rev, */ 132 0, /* refcnt */ 133 megasas_getinfo, /* getinfo */ 134 nulldev, /* identify */ 135 nulldev, /* probe */ 136 megasas_attach, /* attach */ 137 megasas_detach, /* detach */ 138 megasas_reset, /* reset */ 139 &megasas_cb_ops, /* char/block ops */ 140 NULL /* bus ops */ 141 }; 142 143 char _depends_on[] = "misc/scsi"; 144 145 static struct modldrv modldrv = { 146 &mod_driverops, /* module type - driver */ 147 MEGASAS_VERSION, 148 &megasas_ops, /* driver ops */ 149 }; 150 151 static struct modlinkage modlinkage = { 152 MODREV_1, /* ml_rev - must be MODREV_1 */ 153 &modldrv, /* ml_linkage */ 154 NULL /* end of driver linkage */ 155 }; 156 157 static struct ddi_device_acc_attr endian_attr = { 158 DDI_DEVICE_ATTR_V0, 159 DDI_STRUCTURE_LE_ACC, 160 DDI_STRICTORDER_ACC 161 }; 162 163 164 /* 165 * ************************************************************************** * 166 * * 167 * common entry points - for loadable kernel modules * 168 * * 169 * ************************************************************************** * 170 */ 171 172 /* 173 * _init - initialize a loadable module 174 * @void 175 * 176 * The driver should perform any one-time resource allocation or data 177 * initialization during driver loading in _init(). For example, the driver 178 * should initialize any mutexes global to the driver in this routine. 179 * The driver should not, however, use _init() to allocate or initialize 180 * anything that has to do with a particular instance of the device. 181 * Per-instance initialization must be done in attach(). 182 */ 183 int 184 _init(void) 185 { 186 int ret; 187 188 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 189 190 ret = ddi_soft_state_init(&megasas_state, 191 sizeof (struct megasas_instance), 0); 192 193 if (ret != 0) { 194 con_log(CL_ANN, (CE_WARN, "megaraid: could not init state")); 195 return (ret); 196 } 197 198 if ((ret = scsi_hba_init(&modlinkage)) != 0) { 199 con_log(CL_ANN, (CE_WARN, "megaraid: could not init scsi hba")); 200 ddi_soft_state_fini(&megasas_state); 201 return (ret); 202 } 203 204 ret = mod_install(&modlinkage); 205 206 if (ret != 0) { 207 con_log(CL_ANN, (CE_WARN, "megaraid: mod_install failed")); 208 scsi_hba_fini(&modlinkage); 209 ddi_soft_state_fini(&megasas_state); 210 } 211 212 return (ret); 213 } 214 215 /* 216 * _info - returns information about a loadable module. 217 * @void 218 * 219 * _info() is called to return module information. This is a typical entry 220 * point that does predefined role. It simply calls mod_info(). 221 */ 222 int 223 _info(struct modinfo *modinfop) 224 { 225 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 226 227 return (mod_info(&modlinkage, modinfop)); 228 } 229 230 /* 231 * _fini - prepare a loadable module for unloading 232 * @void 233 * 234 * In _fini(), the driver should release any resources that were allocated in 235 * _init(). The driver must remove itself from the system module list. 236 */ 237 int 238 _fini(void) 239 { 240 int ret; 241 242 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 243 244 if ((ret = mod_remove(&modlinkage)) != 0) 245 return (ret); 246 247 scsi_hba_fini(&modlinkage); 248 249 ddi_soft_state_fini(&megasas_state); 250 251 return (ret); 252 } 253 254 255 /* 256 * ************************************************************************** * 257 * * 258 * common entry points - for autoconfiguration * 259 * * 260 * ************************************************************************** * 261 */ 262 /* 263 * attach - adds a device to the system as part of initialization 264 * @dip: 265 * @cmd: 266 * 267 * The kernel calls a driver's attach() entry point to attach an instance of 268 * a device (for MegaRAID, it is instance of a controller) or to resume 269 * operation for an instance of a device that has been suspended or has been 270 * shut down by the power management framework 271 * The attach() entry point typically includes the following types of 272 * processing: 273 * - allocate a soft-state structure for the device instance (for MegaRAID, 274 * controller instance) 275 * - initialize per-instance mutexes 276 * - initialize condition variables 277 * - register the device's interrupts (for MegaRAID, controller's interrupts) 278 * - map the registers and memory of the device instance (for MegaRAID, 279 * controller instance) 280 * - create minor device nodes for the device instance (for MegaRAID, 281 * controller instance) 282 * - report that the device instance (for MegaRAID, controller instance) has 283 * attached 284 */ 285 static int 286 megasas_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 287 { 288 int instance_no; 289 int nregs; 290 uint8_t added_isr_f = 0; 291 uint8_t added_soft_isr_f = 0; 292 uint8_t create_devctl_node_f = 0; 293 uint8_t create_scsi_node_f = 0; 294 uint8_t create_ioc_node_f = 0; 295 uint8_t tran_alloc_f = 0; 296 uint8_t irq; 297 uint16_t vendor_id; 298 uint16_t device_id; 299 uint16_t subsysvid; 300 uint16_t subsysid; 301 uint16_t command; 302 303 scsi_hba_tran_t *tran; 304 ddi_dma_attr_t tran_dma_attr; 305 struct megasas_instance *instance; 306 307 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 308 309 /* CONSTCOND */ 310 ASSERT(NO_COMPETING_THREADS); 311 312 instance_no = ddi_get_instance(dip); 313 314 /* 315 * Since we know that some instantiations of this device can be 316 * plugged into slave-only SBus slots, check to see whether this is 317 * one such. 318 */ 319 if (ddi_slaveonly(dip) == DDI_SUCCESS) { 320 con_log(CL_ANN, (CE_WARN, 321 "mega%d: Device in slave-only slot, unused", instance_no)); 322 return (DDI_FAILURE); 323 } 324 325 switch (cmd) { 326 case DDI_ATTACH: 327 con_log(CL_DLEVEL1, (CE_NOTE, "megasas: DDI_ATTACH")); 328 /* allocate the soft state for the instance */ 329 if (ddi_soft_state_zalloc(megasas_state, instance_no) 330 != DDI_SUCCESS) { 331 con_log(CL_ANN, (CE_WARN, 332 "mega%d: Failed to allocate soft state", 333 instance_no)); 334 335 return (DDI_FAILURE); 336 } 337 338 instance = (struct megasas_instance *)ddi_get_soft_state 339 (megasas_state, instance_no); 340 341 if (instance == NULL) { 342 con_log(CL_ANN, (CE_WARN, 343 "mega%d: Bad soft state", instance_no)); 344 345 ddi_soft_state_free(megasas_state, instance_no); 346 347 return (DDI_FAILURE); 348 } 349 350 bzero((caddr_t)instance, 351 sizeof (struct megasas_instance)); 352 353 instance->func_ptr = kmem_zalloc( 354 sizeof (struct megasas_func_ptr), KM_SLEEP); 355 ASSERT(instance->func_ptr); 356 357 /* Setup the PCI configuration space handles */ 358 if (pci_config_setup(dip, &instance->pci_handle) != 359 DDI_SUCCESS) { 360 con_log(CL_ANN, (CE_WARN, 361 "mega%d: pci config setup failed ", 362 instance_no)); 363 364 kmem_free(instance->func_ptr, 365 sizeof (struct megasas_func_ptr)); 366 ddi_soft_state_free(megasas_state, instance_no); 367 368 return (DDI_FAILURE); 369 } 370 371 if (ddi_dev_nregs(dip, &nregs) != DDI_SUCCESS) { 372 con_log(CL_ANN, (CE_WARN, 373 "megaraid: failed to get registers.")); 374 375 pci_config_teardown(&instance->pci_handle); 376 kmem_free(instance->func_ptr, 377 sizeof (struct megasas_func_ptr)); 378 ddi_soft_state_free(megasas_state, instance_no); 379 380 return (DDI_FAILURE); 381 } 382 383 vendor_id = pci_config_get16(instance->pci_handle, 384 PCI_CONF_VENID); 385 device_id = pci_config_get16(instance->pci_handle, 386 PCI_CONF_DEVID); 387 388 subsysvid = pci_config_get16(instance->pci_handle, 389 PCI_CONF_SUBVENID); 390 subsysid = pci_config_get16(instance->pci_handle, 391 PCI_CONF_SUBSYSID); 392 393 pci_config_put16(instance->pci_handle, PCI_CONF_COMM, 394 (pci_config_get16(instance->pci_handle, 395 PCI_CONF_COMM) | PCI_COMM_ME)); 396 irq = pci_config_get8(instance->pci_handle, 397 PCI_CONF_ILINE); 398 399 con_log(CL_DLEVEL1, (CE_CONT, "megasas%d: " 400 "0x%x:0x%x 0x%x:0x%x, irq:%d drv-ver:%s\n", 401 instance_no, vendor_id, device_id, subsysvid, 402 subsysid, irq, MEGASAS_VERSION)); 403 404 /* enable bus-mastering */ 405 command = pci_config_get16(instance->pci_handle, 406 PCI_CONF_COMM); 407 408 if (!(command & PCI_COMM_ME)) { 409 command |= PCI_COMM_ME; 410 411 pci_config_put16(instance->pci_handle, 412 PCI_CONF_COMM, command); 413 414 con_log(CL_ANN, (CE_CONT, "megaraid%d: " 415 "enable bus-mastering\n", instance_no)); 416 } else { 417 con_log(CL_DLEVEL1, (CE_CONT, "megaraid%d: " 418 "bus-mastering already set\n", instance_no)); 419 } 420 421 /* initialize function pointers */ 422 if ((device_id == PCI_DEVICE_ID_LSI_1078) || 423 (device_id == PCI_DEVICE_ID_LSI_1078DE)) { 424 con_log(CL_DLEVEL1, (CE_CONT, "megasas%d: " 425 "1078R/DE detected\n", instance_no)); 426 instance->func_ptr->read_fw_status_reg = 427 read_fw_status_reg_ppc; 428 instance->func_ptr->issue_cmd = issue_cmd_ppc; 429 instance->func_ptr->issue_cmd_in_sync_mode = 430 issue_cmd_in_sync_mode_ppc; 431 instance->func_ptr->issue_cmd_in_poll_mode = 432 issue_cmd_in_poll_mode_ppc; 433 instance->func_ptr->enable_intr = 434 enable_intr_ppc; 435 instance->func_ptr->disable_intr = 436 disable_intr_ppc; 437 instance->func_ptr->intr_ack = intr_ack_ppc; 438 } else { 439 con_log(CL_DLEVEL1, (CE_CONT, "megasas%d: " 440 "1064/8R detected\n", instance_no)); 441 instance->func_ptr->read_fw_status_reg = 442 read_fw_status_reg_xscale; 443 instance->func_ptr->issue_cmd = 444 issue_cmd_xscale; 445 instance->func_ptr->issue_cmd_in_sync_mode = 446 issue_cmd_in_sync_mode_xscale; 447 instance->func_ptr->issue_cmd_in_poll_mode = 448 issue_cmd_in_poll_mode_xscale; 449 instance->func_ptr->enable_intr = 450 enable_intr_xscale; 451 instance->func_ptr->disable_intr = 452 disable_intr_xscale; 453 instance->func_ptr->intr_ack = 454 intr_ack_xscale; 455 } 456 457 instance->baseaddress = pci_config_get32( 458 instance->pci_handle, PCI_CONF_BASE0); 459 instance->baseaddress &= 0x0fffc; 460 461 instance->dip = dip; 462 instance->vendor_id = vendor_id; 463 instance->device_id = device_id; 464 instance->subsysvid = subsysvid; 465 instance->subsysid = subsysid; 466 467 /* Initialize FMA */ 468 instance->fm_capabilities = ddi_prop_get_int( 469 DDI_DEV_T_ANY, instance->dip, DDI_PROP_CANSLEEP 470 | DDI_PROP_DONTPASS, "fm-capable", 471 DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE 472 | DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE); 473 474 megasas_fm_init(instance); 475 476 /* setup the mfi based low level driver */ 477 if (init_mfi(instance) != DDI_SUCCESS) { 478 con_log(CL_ANN, (CE_WARN, "megaraid: " 479 "could not initialize the low level driver")); 480 481 goto fail_attach; 482 } 483 484 /* 485 * Allocate the interrupt blocking cookie. 486 * It represents the information the framework 487 * needs to block interrupts. This cookie will 488 * be used by the locks shared accross our ISR. 489 * These locks must be initialized before we 490 * register our ISR. 491 * ddi_add_intr(9F) 492 */ 493 if (ddi_get_iblock_cookie(dip, 0, 494 &instance->iblock_cookie) != DDI_SUCCESS) { 495 496 goto fail_attach; 497 } 498 499 if (ddi_get_soft_iblock_cookie(dip, DDI_SOFTINT_HIGH, 500 &instance->soft_iblock_cookie) != DDI_SUCCESS) { 501 502 goto fail_attach; 503 } 504 505 /* 506 * Initialize the driver mutexes common to 507 * normal/high level isr 508 */ 509 if (ddi_intr_hilevel(dip, 0)) { 510 instance->isr_level = HIGH_LEVEL_INTR; 511 mutex_init(&instance->cmd_pool_mtx, 512 "cmd_pool_mtx", MUTEX_DRIVER, 513 instance->soft_iblock_cookie); 514 mutex_init(&instance->cmd_pend_mtx, 515 "cmd_pend_mtx", MUTEX_DRIVER, 516 instance->soft_iblock_cookie); 517 } else { 518 /* 519 * Initialize the driver mutexes 520 * specific to soft-isr 521 */ 522 instance->isr_level = NORMAL_LEVEL_INTR; 523 mutex_init(&instance->cmd_pool_mtx, 524 "cmd_pool_mtx", MUTEX_DRIVER, 525 instance->iblock_cookie); 526 mutex_init(&instance->cmd_pend_mtx, 527 "cmd_pend_mtx", MUTEX_DRIVER, 528 instance->iblock_cookie); 529 } 530 531 mutex_init(&instance->completed_pool_mtx, 532 "completed_pool_mtx", MUTEX_DRIVER, 533 instance->iblock_cookie); 534 mutex_init(&instance->int_cmd_mtx, "int_cmd_mtx", 535 MUTEX_DRIVER, instance->iblock_cookie); 536 mutex_init(&instance->aen_cmd_mtx, "aen_cmd_mtx", 537 MUTEX_DRIVER, instance->iblock_cookie); 538 mutex_init(&instance->abort_cmd_mtx, "abort_cmd_mtx", 539 MUTEX_DRIVER, instance->iblock_cookie); 540 541 cv_init(&instance->int_cmd_cv, NULL, CV_DRIVER, NULL); 542 cv_init(&instance->abort_cmd_cv, NULL, CV_DRIVER, NULL); 543 544 INIT_LIST_HEAD(&instance->completed_pool_list); 545 546 /* Register our isr. */ 547 if (ddi_add_intr(dip, 0, NULL, NULL, megasas_isr, 548 (caddr_t)instance) != DDI_SUCCESS) { 549 con_log(CL_ANN, (CE_WARN, 550 " ISR did not register")); 551 552 goto fail_attach; 553 } 554 555 added_isr_f = 1; 556 557 /* Register our soft-isr for highlevel interrupts. */ 558 if (instance->isr_level == HIGH_LEVEL_INTR) { 559 if (ddi_add_softintr(dip, DDI_SOFTINT_HIGH, 560 &instance->soft_intr_id, NULL, NULL, 561 megasas_softintr, (caddr_t)instance) != 562 DDI_SUCCESS) { 563 con_log(CL_ANN, (CE_WARN, 564 " Software ISR did not register")); 565 566 goto fail_attach; 567 } 568 569 added_soft_isr_f = 1; 570 } 571 572 /* Allocate a transport structure */ 573 tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP); 574 575 if (tran == NULL) { 576 con_log(CL_ANN, (CE_WARN, 577 "scsi_hba_tran_alloc failed")); 578 goto fail_attach; 579 } 580 581 tran_alloc_f = 1; 582 583 instance->tran = tran; 584 585 tran->tran_hba_private = instance; 586 tran->tran_tgt_private = NULL; 587 tran->tran_tgt_init = megasas_tran_tgt_init; 588 tran->tran_tgt_probe = scsi_hba_probe; 589 tran->tran_tgt_free = (void (*)())NULL; 590 tran->tran_init_pkt = megasas_tran_init_pkt; 591 tran->tran_start = megasas_tran_start; 592 tran->tran_abort = megasas_tran_abort; 593 tran->tran_reset = megasas_tran_reset; 594 tran->tran_bus_reset = megasas_tran_bus_reset; 595 tran->tran_getcap = megasas_tran_getcap; 596 tran->tran_setcap = megasas_tran_setcap; 597 tran->tran_destroy_pkt = megasas_tran_destroy_pkt; 598 tran->tran_dmafree = megasas_tran_dmafree; 599 tran->tran_sync_pkt = megasas_tran_sync_pkt; 600 tran->tran_reset_notify = NULL; 601 tran->tran_quiesce = megasas_tran_quiesce; 602 tran->tran_unquiesce = megasas_tran_unquiesce; 603 604 tran_dma_attr = megasas_generic_dma_attr; 605 tran_dma_attr.dma_attr_sgllen = instance->max_num_sge; 606 607 /* Attach this instance of the hba */ 608 if (scsi_hba_attach_setup(dip, &tran_dma_attr, tran, 0) 609 != DDI_SUCCESS) { 610 con_log(CL_ANN, (CE_WARN, 611 "scsi_hba_attach failed\n")); 612 613 goto fail_attach; 614 } 615 616 /* create devctl node for cfgadm command */ 617 if (ddi_create_minor_node(dip, "devctl", 618 S_IFCHR, INST2DEVCTL(instance_no), 619 DDI_NT_SCSI_NEXUS, 0) == DDI_FAILURE) { 620 con_log(CL_ANN, (CE_WARN, 621 "megaraid: failed to create devctl node.")); 622 623 goto fail_attach; 624 } 625 626 create_devctl_node_f = 1; 627 628 /* create scsi node for cfgadm command */ 629 if (ddi_create_minor_node(dip, "scsi", S_IFCHR, 630 INST2SCSI(instance_no), 631 DDI_NT_SCSI_ATTACHMENT_POINT, 0) == 632 DDI_FAILURE) { 633 con_log(CL_ANN, (CE_WARN, 634 "megaraid: failed to create scsi node.")); 635 636 goto fail_attach; 637 } 638 639 create_scsi_node_f = 1; 640 641 (void) sprintf(instance->iocnode, "%d:lsirdctl", 642 instance_no); 643 644 /* 645 * Create a node for applications 646 * for issuing ioctl to the driver. 647 */ 648 if (ddi_create_minor_node(dip, instance->iocnode, 649 S_IFCHR, INST2LSIRDCTL(instance_no), 650 DDI_PSEUDO, 0) == DDI_FAILURE) { 651 con_log(CL_ANN, (CE_WARN, 652 "megaraid: failed to create ioctl node.")); 653 654 goto fail_attach; 655 } 656 657 create_ioc_node_f = 1; 658 659 /* enable interrupt */ 660 instance->func_ptr->enable_intr(instance); 661 662 /* initiate AEN */ 663 if (start_mfi_aen(instance)) { 664 con_log(CL_ANN, (CE_WARN, 665 "megaraid: failed to initiate AEN.")); 666 goto fail_initiate_aen; 667 } 668 669 con_log(CL_DLEVEL1, (CE_NOTE, 670 "AEN started for instance %d.", instance_no)); 671 672 /* Finally! We are on the air. */ 673 ddi_report_dev(dip); 674 675 if (megasas_check_acc_handle(instance->regmap_handle) != 676 DDI_SUCCESS) { 677 goto fail_attach; 678 } 679 if (megasas_check_acc_handle(instance->pci_handle) != 680 DDI_SUCCESS) { 681 goto fail_attach; 682 } 683 break; 684 case DDI_PM_RESUME: 685 con_log(CL_ANN, (CE_NOTE, 686 "megasas: DDI_PM_RESUME")); 687 break; 688 case DDI_RESUME: 689 con_log(CL_ANN, (CE_NOTE, 690 "megasas: DDI_RESUME")); 691 break; 692 default: 693 con_log(CL_ANN, (CE_WARN, 694 "megasas: invalid attach cmd=%x", cmd)); 695 return (DDI_FAILURE); 696 } 697 698 return (DDI_SUCCESS); 699 700 fail_initiate_aen: 701 fail_attach: 702 if (create_devctl_node_f) { 703 ddi_remove_minor_node(dip, "devctl"); 704 } 705 706 if (create_scsi_node_f) { 707 ddi_remove_minor_node(dip, "scsi"); 708 } 709 710 if (create_ioc_node_f) { 711 ddi_remove_minor_node(dip, instance->iocnode); 712 } 713 714 if (tran_alloc_f) { 715 scsi_hba_tran_free(tran); 716 } 717 718 719 if (added_soft_isr_f) { 720 ddi_remove_softintr(instance->soft_intr_id); 721 } 722 723 if (added_isr_f) { 724 ddi_remove_intr(dip, 0, instance->iblock_cookie); 725 } 726 727 megasas_fm_ereport(instance, DDI_FM_DEVICE_NO_RESPONSE); 728 ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST); 729 730 megasas_fm_fini(instance); 731 732 pci_config_teardown(&instance->pci_handle); 733 734 ddi_soft_state_free(megasas_state, instance_no); 735 736 con_log(CL_ANN, (CE_NOTE, 737 "megasas: return failure from mega_attach\n")); 738 739 return (DDI_FAILURE); 740 } 741 742 /* 743 * getinfo - gets device information 744 * @dip: 745 * @cmd: 746 * @arg: 747 * @resultp: 748 * 749 * The system calls getinfo() to obtain configuration information that only 750 * the driver knows. The mapping of minor numbers to device instance is 751 * entirely under the control of the driver. The system sometimes needs to ask 752 * the driver which device a particular dev_t represents. 753 * Given the device number return the devinfo pointer from the scsi_device 754 * structure. 755 */ 756 /*ARGSUSED*/ 757 static int 758 megasas_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp) 759 { 760 int rval; 761 int megasas_minor = getminor((dev_t)arg); 762 763 struct megasas_instance *instance; 764 765 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 766 767 switch (cmd) { 768 case DDI_INFO_DEVT2DEVINFO: 769 instance = (struct megasas_instance *) 770 ddi_get_soft_state(megasas_state, 771 MINOR2INST(megasas_minor)); 772 773 if (instance == NULL) { 774 *resultp = NULL; 775 rval = DDI_FAILURE; 776 } else { 777 *resultp = instance->dip; 778 rval = DDI_SUCCESS; 779 } 780 break; 781 case DDI_INFO_DEVT2INSTANCE: 782 *resultp = (void *)instance; 783 rval = DDI_SUCCESS; 784 break; 785 default: 786 *resultp = NULL; 787 rval = DDI_FAILURE; 788 } 789 790 return (rval); 791 } 792 793 /* 794 * detach - detaches a device from the system 795 * @dip: pointer to the device's dev_info structure 796 * @cmd: type of detach 797 * 798 * A driver's detach() entry point is called to detach an instance of a device 799 * that is bound to the driver. The entry point is called with the instance of 800 * the device node to be detached and with DDI_DETACH, which is specified as 801 * the cmd argument to the entry point. 802 * This routine is called during driver unload. We free all the allocated 803 * resources and call the corresponding LLD so that it can also release all 804 * its resources. 805 */ 806 static int 807 megasas_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 808 { 809 int instance_no; 810 811 struct megasas_instance *instance; 812 813 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 814 815 /* CONSTCOND */ 816 ASSERT(NO_COMPETING_THREADS); 817 818 instance_no = ddi_get_instance(dip); 819 820 instance = (struct megasas_instance *)ddi_get_soft_state(megasas_state, 821 instance_no); 822 823 if (!instance) { 824 con_log(CL_ANN, (CE_WARN, 825 "megasas:%d could not get instance in detach", 826 instance_no)); 827 828 return (DDI_FAILURE); 829 } 830 831 con_log(CL_ANN, (CE_NOTE, 832 "megasas%d: detaching device 0x%4x:0x%4x:0x%4x:0x%4x\n", 833 instance_no, instance->vendor_id, instance->device_id, 834 instance->subsysvid, instance->subsysid)); 835 836 switch (cmd) { 837 case DDI_DETACH: 838 con_log(CL_ANN, (CE_NOTE, 839 "megasas_detach: DDI_DETACH\n")); 840 841 if (scsi_hba_detach(dip) != DDI_SUCCESS) { 842 con_log(CL_ANN, (CE_WARN, 843 "megasas:%d failed to detach", 844 instance_no)); 845 846 return (DDI_FAILURE); 847 } 848 849 scsi_hba_tran_free(instance->tran); 850 851 if (abort_aen_cmd(instance, instance->aen_cmd)) { 852 con_log(CL_ANN, (CE_WARN, "megasas_detach: " 853 "failed to abort prevous AEN command\n")); 854 855 return (DDI_FAILURE); 856 } 857 858 instance->func_ptr->disable_intr(instance); 859 860 if (instance->isr_level == HIGH_LEVEL_INTR) { 861 ddi_remove_softintr(instance->soft_intr_id); 862 } 863 864 ddi_remove_intr(dip, 0, instance->iblock_cookie); 865 866 free_space_for_mfi(instance); 867 868 megasas_fm_fini(instance); 869 870 pci_config_teardown(&instance->pci_handle); 871 872 kmem_free(instance->func_ptr, 873 sizeof (struct megasas_func_ptr)); 874 875 ddi_soft_state_free(megasas_state, instance_no); 876 break; 877 case DDI_PM_SUSPEND: 878 con_log(CL_ANN, (CE_NOTE, 879 "megasas_detach: DDI_PM_SUSPEND\n")); 880 881 break; 882 case DDI_SUSPEND: 883 con_log(CL_ANN, (CE_NOTE, 884 "megasas_detach: DDI_SUSPEND\n")); 885 886 break; 887 default: 888 con_log(CL_ANN, (CE_WARN, 889 "invalid detach command:0x%x", cmd)); 890 return (DDI_FAILURE); 891 } 892 893 return (DDI_SUCCESS); 894 } 895 896 /* 897 * ************************************************************************** * 898 * * 899 * common entry points - for character driver types * 900 * * 901 * ************************************************************************** * 902 */ 903 /* 904 * open - gets access to a device 905 * @dev: 906 * @openflags: 907 * @otyp: 908 * @credp: 909 * 910 * Access to a device by one or more application programs is controlled 911 * through the open() and close() entry points. The primary function of 912 * open() is to verify that the open request is allowed. 913 */ 914 static int 915 megasas_open(dev_t *dev, int openflags, int otyp, cred_t *credp) 916 { 917 int rval = 0; 918 919 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 920 921 /* Check root permissions */ 922 if (drv_priv(credp) != 0) { 923 con_log(CL_ANN, (CE_WARN, 924 "megaraid: Non-root ioctl access tried!")); 925 return (EPERM); 926 } 927 928 /* Verify we are being opened as a character device */ 929 if (otyp != OTYP_CHR) { 930 con_log(CL_ANN, (CE_WARN, 931 "megaraid: ioctl node must be a char node\n")); 932 return (EINVAL); 933 } 934 935 if (ddi_get_soft_state(megasas_state, MINOR2INST(getminor(*dev))) 936 == NULL) { 937 return (ENXIO); 938 } 939 940 if (scsi_hba_open) { 941 rval = scsi_hba_open(dev, openflags, otyp, credp); 942 } 943 944 return (rval); 945 } 946 947 /* 948 * close - gives up access to a device 949 * @dev: 950 * @openflags: 951 * @otyp: 952 * @credp: 953 * 954 * close() should perform any cleanup necessary to finish using the minor 955 * device, and prepare the device (and driver) to be opened again. 956 */ 957 static int 958 megasas_close(dev_t dev, int openflags, int otyp, cred_t *credp) 959 { 960 int rval = 0; 961 962 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 963 964 /* no need for locks! */ 965 966 if (scsi_hba_close) { 967 rval = scsi_hba_close(dev, openflags, otyp, credp); 968 } 969 970 return (rval); 971 } 972 973 /* 974 * ioctl - performs a range of I/O commands for character drivers 975 * @dev: 976 * @cmd: 977 * @arg: 978 * @mode: 979 * @credp: 980 * @rvalp: 981 * 982 * ioctl() routine must make sure that user data is copied into or out of the 983 * kernel address space explicitly using copyin(), copyout(), ddi_copyin(), 984 * and ddi_copyout(), as appropriate. 985 * This is a wrapper routine to serialize access to the actual ioctl routine. 986 * ioctl() should return 0 on success, or the appropriate error number. The 987 * driver may also set the value returned to the calling process through rvalp. 988 */ 989 static int 990 megasas_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, 991 int *rvalp) 992 { 993 int rval = 0; 994 995 struct megasas_instance *instance; 996 struct megasas_ioctl ioctl; 997 struct megasas_aen aen; 998 999 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1000 1001 instance = ddi_get_soft_state(megasas_state, MINOR2INST(getminor(dev))); 1002 1003 if (instance == NULL) { 1004 /* invalid minor number */ 1005 con_log(CL_ANN, (CE_WARN, "megaraid: adapter not found.")); 1006 return (ENXIO); 1007 } 1008 1009 switch ((uint_t)cmd) { 1010 case MEGASAS_IOCTL_FIRMWARE: 1011 if (ddi_copyin((void *) arg, &ioctl, 1012 sizeof (struct megasas_ioctl), mode)) { 1013 con_log(CL_ANN, (CE_WARN, "megasas_ioctl: " 1014 "ERROR IOCTL copyin")); 1015 return (EFAULT); 1016 } 1017 1018 if (ioctl.control_code == MR_DRIVER_IOCTL_COMMON) { 1019 rval = handle_drv_ioctl(instance, &ioctl, mode); 1020 } else { 1021 rval = handle_mfi_ioctl(instance, &ioctl, mode); 1022 } 1023 1024 if (ddi_copyout((void *) &ioctl, (void *)arg, 1025 (sizeof (struct megasas_ioctl) - 1), mode)) { 1026 con_log(CL_ANN, (CE_WARN, 1027 "megasas_ioctl: copy_to_user failed\n")); 1028 rval = 1; 1029 } 1030 1031 break; 1032 case MEGASAS_IOCTL_AEN: 1033 if (ddi_copyin((void *) arg, &aen, 1034 sizeof (struct megasas_aen), mode)) { 1035 con_log(CL_ANN, (CE_WARN, 1036 "megasas_ioctl: ERROR AEN copyin")); 1037 return (EFAULT); 1038 } 1039 1040 rval = handle_mfi_aen(instance, &aen); 1041 1042 if (ddi_copyout((void *) &aen, (void *)arg, 1043 sizeof (struct megasas_aen), mode)) { 1044 con_log(CL_ANN, (CE_WARN, 1045 "megasas_ioctl: copy_to_user failed\n")); 1046 rval = 1; 1047 } 1048 1049 break; 1050 default: 1051 rval = scsi_hba_ioctl(dev, cmd, arg, 1052 mode, credp, rvalp); 1053 1054 con_log(CL_DLEVEL1, (CE_NOTE, "megasas_ioctl: " 1055 "scsi_hba_ioctl called, ret = %x.", rval)); 1056 } 1057 1058 return (rval); 1059 } 1060 1061 /* 1062 * ************************************************************************** * 1063 * * 1064 * common entry points - for block driver types * 1065 * * 1066 * ************************************************************************** * 1067 */ 1068 /* 1069 * reset - TBD 1070 * @dip: 1071 * @cmd: 1072 * 1073 * TBD 1074 */ 1075 /*ARGSUSED*/ 1076 static int 1077 megasas_reset(dev_info_t *dip, ddi_reset_cmd_t cmd) 1078 { 1079 int instance_no; 1080 1081 struct megasas_instance *instance; 1082 1083 instance_no = ddi_get_instance(dip); 1084 instance = (struct megasas_instance *)ddi_get_soft_state 1085 (megasas_state, instance_no); 1086 1087 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1088 1089 if (!instance) { 1090 con_log(CL_ANN, (CE_WARN, 1091 "megaraid:%d could not get adapter in reset", 1092 instance_no)); 1093 return (DDI_FAILURE); 1094 } 1095 1096 con_log(CL_ANN, (CE_NOTE, "flushing cache for instance %d ..", 1097 instance_no)); 1098 1099 flush_cache(instance); 1100 1101 return (DDI_SUCCESS); 1102 } 1103 1104 1105 /* 1106 * ************************************************************************** * 1107 * * 1108 * entry points (SCSI HBA) * 1109 * * 1110 * ************************************************************************** * 1111 */ 1112 /* 1113 * tran_tgt_init - initialize a target device instance 1114 * @hba_dip: 1115 * @tgt_dip: 1116 * @tran: 1117 * @sd: 1118 * 1119 * The tran_tgt_init() entry point enables the HBA to allocate and initialize 1120 * any per-target resources. tran_tgt_init() also enables the HBA to qualify 1121 * the device's address as valid and supportable for that particular HBA. 1122 * By returning DDI_FAILURE, the instance of the target driver for that device 1123 * is not probed or attached. 1124 */ 1125 /*ARGSUSED*/ 1126 static int 1127 megasas_tran_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip, 1128 scsi_hba_tran_t *tran, struct scsi_device *sd) 1129 { 1130 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1131 1132 return (DDI_SUCCESS); 1133 } 1134 1135 /* 1136 * tran_init_pkt - allocate & initialize a scsi_pkt structure 1137 * @ap: 1138 * @pkt: 1139 * @bp: 1140 * @cmdlen: 1141 * @statuslen: 1142 * @tgtlen: 1143 * @flags: 1144 * @callback: 1145 * 1146 * The tran_init_pkt() entry point allocates and initializes a scsi_pkt 1147 * structure and DMA resources for a target driver request. The 1148 * tran_init_pkt() entry point is called when the target driver calls the 1149 * SCSA function scsi_init_pkt(). Each call of the tran_init_pkt() entry point 1150 * is a request to perform one or more of three possible services: 1151 * - allocation and initialization of a scsi_pkt structure 1152 * - allocation of DMA resources for data transfer 1153 * - reallocation of DMA resources for the next portion of the data transfer 1154 */ 1155 static struct scsi_pkt * 1156 megasas_tran_init_pkt(struct scsi_address *ap, register struct scsi_pkt *pkt, 1157 struct buf *bp, int cmdlen, int statuslen, int tgtlen, 1158 int flags, int (*callback)(), caddr_t arg) 1159 { 1160 struct scsa_cmd *acmd; 1161 struct megasas_instance *instance; 1162 struct scsi_pkt *new_pkt; 1163 1164 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1165 1166 instance = ADDR2MEGA(ap); 1167 1168 /* step #1 : pkt allocation */ 1169 if (pkt == NULL) { 1170 pkt = scsi_hba_pkt_alloc(instance->dip, ap, cmdlen, statuslen, 1171 tgtlen, sizeof (struct scsa_cmd), callback, arg); 1172 if (pkt == NULL) { 1173 return (NULL); 1174 } 1175 1176 acmd = PKT2CMD(pkt); 1177 1178 /* 1179 * Initialize the new pkt - we redundantly initialize 1180 * all the fields for illustrative purposes. 1181 */ 1182 acmd->cmd_pkt = pkt; 1183 acmd->cmd_flags = 0; 1184 acmd->cmd_scblen = statuslen; 1185 acmd->cmd_cdblen = cmdlen; 1186 acmd->cmd_dmahandle = NULL; 1187 acmd->cmd_ncookies = 0; 1188 acmd->cmd_cookie = 0; 1189 acmd->cmd_cookiecnt = 0; 1190 acmd->cmd_nwin = 0; 1191 1192 pkt->pkt_address = *ap; 1193 pkt->pkt_comp = (void (*)())NULL; 1194 pkt->pkt_flags = 0; 1195 pkt->pkt_time = 0; 1196 pkt->pkt_resid = 0; 1197 pkt->pkt_state = 0; 1198 pkt->pkt_statistics = 0; 1199 pkt->pkt_reason = 0; 1200 new_pkt = pkt; 1201 } else { 1202 acmd = PKT2CMD(pkt); 1203 new_pkt = NULL; 1204 } 1205 1206 /* step #2 : dma allocation/move */ 1207 if (bp && bp->b_bcount != 0) { 1208 if (acmd->cmd_dmahandle == NULL) { 1209 if (megasas_dma_alloc(instance, pkt, bp, flags, 1210 callback) == -1) { 1211 if (new_pkt) { 1212 scsi_hba_pkt_free(ap, new_pkt); 1213 } 1214 1215 return ((struct scsi_pkt *)NULL); 1216 } 1217 } else { 1218 if (megasas_dma_move(instance, pkt, bp) == -1) { 1219 return ((struct scsi_pkt *)NULL); 1220 } 1221 } 1222 } 1223 1224 return (pkt); 1225 } 1226 1227 /* 1228 * tran_start - transport a SCSI command to the addressed target 1229 * @ap: 1230 * @pkt: 1231 * 1232 * The tran_start() entry point for a SCSI HBA driver is called to transport a 1233 * SCSI command to the addressed target. The SCSI command is described 1234 * entirely within the scsi_pkt structure, which the target driver allocated 1235 * through the HBA driver's tran_init_pkt() entry point. If the command 1236 * involves a data transfer, DMA resources must also have been allocated for 1237 * the scsi_pkt structure. 1238 * 1239 * Return Values : 1240 * TRAN_BUSY - request queue is full, no more free scbs 1241 * TRAN_ACCEPT - pkt has been submitted to the instance 1242 */ 1243 static int 1244 megasas_tran_start(struct scsi_address *ap, register struct scsi_pkt *pkt) 1245 { 1246 uchar_t cmd_done = 0; 1247 1248 struct megasas_instance *instance = ADDR2MEGA(ap); 1249 struct megasas_cmd *cmd; 1250 1251 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d:SCSI CDB[0]=0x%x", 1252 __func__, __LINE__, pkt->pkt_cdbp[0])); 1253 1254 pkt->pkt_reason = CMD_CMPLT; 1255 *pkt->pkt_scbp = STATUS_GOOD; /* clear arq scsi_status */ 1256 1257 cmd = build_cmd(instance, ap, pkt, &cmd_done); 1258 1259 /* 1260 * Check if the command is already completed by the mega_build_cmd() 1261 * routine. In which case the busy_flag would be clear and scb will be 1262 * NULL and appropriate reason provided in pkt_reason field 1263 */ 1264 if (cmd_done) { 1265 if (((pkt->pkt_flags & FLAG_NOINTR) == 0) && pkt->pkt_comp) { 1266 (*pkt->pkt_comp)(pkt); 1267 } 1268 pkt->pkt_reason = CMD_CMPLT; 1269 pkt->pkt_scbp[0] = STATUS_GOOD; 1270 pkt->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET 1271 | STATE_SENT_CMD; 1272 return (TRAN_ACCEPT); 1273 } 1274 1275 if (cmd == NULL) { 1276 return (TRAN_BUSY); 1277 } 1278 1279 if ((pkt->pkt_flags & FLAG_NOINTR) == 0) { 1280 if (instance->fw_outstanding > instance->max_fw_cmds) { 1281 con_log(CL_ANN, (CE_CONT, "megasas:Firmware busy")); 1282 return_mfi_pkt(instance, cmd); 1283 return (TRAN_BUSY); 1284 } 1285 1286 /* Syncronize the Cmd frame for the controller */ 1287 (void) ddi_dma_sync(cmd->frame_dma_obj.dma_handle, 0, 0, 1288 DDI_DMA_SYNC_FORDEV); 1289 1290 instance->func_ptr->issue_cmd(cmd, instance); 1291 1292 } else { 1293 struct megasas_header *hdr = &cmd->frame->hdr; 1294 1295 cmd->sync_cmd = MEGASAS_TRUE; 1296 1297 instance->func_ptr-> issue_cmd_in_poll_mode(instance, cmd); 1298 1299 pkt->pkt_reason = CMD_CMPLT; 1300 pkt->pkt_statistics = 0; 1301 pkt->pkt_state |= STATE_XFERRED_DATA | STATE_GOT_STATUS; 1302 1303 switch (hdr->cmd_status) { 1304 case MFI_STAT_OK: 1305 pkt->pkt_scbp[0] = STATUS_GOOD; 1306 break; 1307 1308 case MFI_STAT_SCSI_DONE_WITH_ERROR: 1309 1310 pkt->pkt_reason = CMD_CMPLT; 1311 pkt->pkt_statistics = 0; 1312 1313 ((struct scsi_status *)pkt->pkt_scbp)->sts_chk = 1; 1314 break; 1315 1316 case MFI_STAT_DEVICE_NOT_FOUND: 1317 pkt->pkt_reason = CMD_DEV_GONE; 1318 pkt->pkt_statistics = STAT_DISCON; 1319 break; 1320 1321 default: 1322 ((struct scsi_status *)pkt->pkt_scbp)->sts_busy = 1; 1323 } 1324 1325 return_mfi_pkt(instance, cmd); 1326 (void) megasas_common_check(instance, cmd); 1327 1328 if (pkt->pkt_comp) { 1329 (*pkt->pkt_comp)(pkt); 1330 } 1331 1332 } 1333 1334 return (TRAN_ACCEPT); 1335 } 1336 1337 /* 1338 * tran_abort - Abort any commands that are currently in transport 1339 * @ap: 1340 * @pkt: 1341 * 1342 * The tran_abort() entry point for a SCSI HBA driver is called to abort any 1343 * commands that are currently in transport for a particular target. This entry 1344 * point is called when a target driver calls scsi_abort(). The tran_abort() 1345 * entry point should attempt to abort the command denoted by the pkt 1346 * parameter. If the pkt parameter is NULL, tran_abort() should attempt to 1347 * abort all outstanding commands in the transport layer for the particular 1348 * target or logical unit. 1349 */ 1350 /*ARGSUSED*/ 1351 static int 1352 megasas_tran_abort(struct scsi_address *ap, struct scsi_pkt *pkt) 1353 { 1354 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1355 1356 /* aborting command not supported by H/W */ 1357 1358 return (DDI_FAILURE); 1359 } 1360 1361 /* 1362 * tran_reset - reset either the SCSI bus or target 1363 * @ap: 1364 * @level: 1365 * 1366 * The tran_reset() entry point for a SCSI HBA driver is called to reset either 1367 * the SCSI bus or a particular SCSI target device. This entry point is called 1368 * when a target driver calls scsi_reset(). The tran_reset() entry point must 1369 * reset the SCSI bus if level is RESET_ALL. If level is RESET_TARGET, just the 1370 * particular target or logical unit must be reset. 1371 */ 1372 /*ARGSUSED*/ 1373 static int 1374 megasas_tran_reset(struct scsi_address *ap, int level) 1375 { 1376 struct megasas_instance *instance = ADDR2MEGA(ap); 1377 1378 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1379 1380 if (wait_for_outstanding(instance)) { 1381 return (DDI_FAILURE); 1382 } else { 1383 return (DDI_SUCCESS); 1384 } 1385 } 1386 1387 /* 1388 * tran_bus_reset - reset the SCSI bus 1389 * @dip: 1390 * @level: 1391 * 1392 * The tran_bus_reset() vector in the scsi_hba_tran structure should be 1393 * initialized during the HBA driver's attach(). The vector should point to 1394 * an HBA entry point that is to be called when a user initiates a bus reset. 1395 * Implementation is hardware specific. If the HBA driver cannot reset the 1396 * SCSI bus without affecting the targets, the driver should fail RESET_BUS 1397 * or not initialize this vector. 1398 */ 1399 /*ARGSUSED*/ 1400 static int 1401 megasas_tran_bus_reset(dev_info_t *dip, int level) 1402 { 1403 int instance_no = ddi_get_instance(dip); 1404 1405 struct megasas_instance *instance = ddi_get_soft_state(megasas_state, 1406 instance_no); 1407 1408 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1409 1410 if (wait_for_outstanding(instance)) { 1411 return (DDI_FAILURE); 1412 } else { 1413 return (DDI_SUCCESS); 1414 } 1415 } 1416 1417 /* 1418 * tran_getcap - get one of a set of SCSA-defined capabilities 1419 * @ap: 1420 * @cap: 1421 * @whom: 1422 * 1423 * The target driver can request the current setting of the capability for a 1424 * particular target by setting the whom parameter to nonzero. A whom value of 1425 * zero indicates a request for the current setting of the general capability 1426 * for the SCSI bus or for adapter hardware. The tran_getcap() should return -1 1427 * for undefined capabilities or the current value of the requested capability. 1428 */ 1429 /*ARGSUSED*/ 1430 static int 1431 megasas_tran_getcap(struct scsi_address *ap, char *cap, int whom) 1432 { 1433 int rval = 0; 1434 1435 struct megasas_instance *instance = ADDR2MEGA(ap); 1436 1437 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1438 1439 /* we do allow inquiring about capabilities for other targets */ 1440 if (cap == NULL) { 1441 return (-1); 1442 } 1443 1444 switch (scsi_hba_lookup_capstr(cap)) { 1445 case SCSI_CAP_DMA_MAX: 1446 /* Limit to 16MB max transfer */ 1447 rval = megasas_max_cap_maxxfer; 1448 break; 1449 case SCSI_CAP_MSG_OUT: 1450 rval = 1; 1451 break; 1452 case SCSI_CAP_DISCONNECT: 1453 rval = 0; 1454 break; 1455 case SCSI_CAP_SYNCHRONOUS: 1456 rval = 0; 1457 break; 1458 case SCSI_CAP_WIDE_XFER: 1459 rval = 1; 1460 break; 1461 case SCSI_CAP_TAGGED_QING: 1462 rval = 1; 1463 break; 1464 case SCSI_CAP_UNTAGGED_QING: 1465 rval = 1; 1466 break; 1467 case SCSI_CAP_PARITY: 1468 rval = 1; 1469 break; 1470 case SCSI_CAP_INITIATOR_ID: 1471 rval = instance->init_id; 1472 break; 1473 case SCSI_CAP_ARQ: 1474 rval = 1; 1475 break; 1476 case SCSI_CAP_LINKED_CMDS: 1477 rval = 0; 1478 break; 1479 case SCSI_CAP_RESET_NOTIFICATION: 1480 rval = 1; 1481 break; 1482 case SCSI_CAP_GEOMETRY: 1483 rval = -1; 1484 1485 break; 1486 default: 1487 con_log(CL_DLEVEL2, (CE_NOTE, "Default cap coming 0x%x", 1488 scsi_hba_lookup_capstr(cap))); 1489 rval = -1; 1490 break; 1491 } 1492 1493 return (rval); 1494 } 1495 1496 /* 1497 * tran_setcap - set one of a set of SCSA-defined capabilities 1498 * @ap: 1499 * @cap: 1500 * @value: 1501 * @whom: 1502 * 1503 * The target driver might request that the new value be set for a particular 1504 * target by setting the whom parameter to nonzero. A whom value of zero 1505 * means that request is to set the new value for the SCSI bus or for adapter 1506 * hardware in general. 1507 * The tran_setcap() should return the following values as appropriate: 1508 * - -1 for undefined capabilities 1509 * - 0 if the HBA driver cannot set the capability to the requested value 1510 * - 1 if the HBA driver is able to set the capability to the requested value 1511 */ 1512 /*ARGSUSED*/ 1513 static int 1514 megasas_tran_setcap(struct scsi_address *ap, char *cap, int value, int whom) 1515 { 1516 int rval = 1; 1517 1518 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1519 1520 /* We don't allow setting capabilities for other targets */ 1521 if (cap == NULL || whom == 0) { 1522 return (-1); 1523 } 1524 1525 switch (scsi_hba_lookup_capstr(cap)) { 1526 case SCSI_CAP_DMA_MAX: 1527 case SCSI_CAP_MSG_OUT: 1528 case SCSI_CAP_PARITY: 1529 case SCSI_CAP_LINKED_CMDS: 1530 case SCSI_CAP_RESET_NOTIFICATION: 1531 case SCSI_CAP_DISCONNECT: 1532 case SCSI_CAP_SYNCHRONOUS: 1533 case SCSI_CAP_UNTAGGED_QING: 1534 case SCSI_CAP_WIDE_XFER: 1535 case SCSI_CAP_INITIATOR_ID: 1536 case SCSI_CAP_ARQ: 1537 /* 1538 * None of these are settable via 1539 * the capability interface. 1540 */ 1541 break; 1542 case SCSI_CAP_TAGGED_QING: 1543 rval = 1; 1544 break; 1545 case SCSI_CAP_SECTOR_SIZE: 1546 rval = 1; 1547 break; 1548 1549 case SCSI_CAP_TOTAL_SECTORS: 1550 rval = 1; 1551 break; 1552 default: 1553 rval = -1; 1554 break; 1555 } 1556 1557 return (rval); 1558 } 1559 1560 /* 1561 * tran_destroy_pkt - deallocate scsi_pkt structure 1562 * @ap: 1563 * @pkt: 1564 * 1565 * The tran_destroy_pkt() entry point is the HBA driver function that 1566 * deallocates scsi_pkt structures. The tran_destroy_pkt() entry point is 1567 * called when the target driver calls scsi_destroy_pkt(). The 1568 * tran_destroy_pkt() entry point must free any DMA resources that have been 1569 * allocated for the packet. An implicit DMA synchronization occurs if the 1570 * DMA resources are freed and any cached data remains after the completion 1571 * of the transfer. 1572 */ 1573 static void 1574 megasas_tran_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 1575 { 1576 struct scsa_cmd *acmd = PKT2CMD(pkt); 1577 1578 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1579 1580 if (acmd->cmd_flags & CFLAG_DMAVALID) { 1581 acmd->cmd_flags &= ~CFLAG_DMAVALID; 1582 1583 (void) ddi_dma_unbind_handle(acmd->cmd_dmahandle); 1584 1585 ddi_dma_free_handle(&acmd->cmd_dmahandle); 1586 1587 acmd->cmd_dmahandle = NULL; 1588 } 1589 1590 /* free the pkt */ 1591 scsi_hba_pkt_free(ap, pkt); 1592 } 1593 1594 /* 1595 * tran_dmafree - deallocates DMA resources 1596 * @ap: 1597 * @pkt: 1598 * 1599 * The tran_dmafree() entry point deallocates DMAQ resources that have been 1600 * allocated for a scsi_pkt structure. The tran_dmafree() entry point is 1601 * called when the target driver calls scsi_dmafree(). The tran_dmafree() must 1602 * free only DMA resources allocated for a scsi_pkt structure, not the 1603 * scsi_pkt itself. When DMA resources are freed, a DMA synchronization is 1604 * implicitly performed. 1605 */ 1606 /*ARGSUSED*/ 1607 static void 1608 megasas_tran_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt) 1609 { 1610 register struct scsa_cmd *acmd = PKT2CMD(pkt); 1611 1612 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1613 1614 if (acmd->cmd_flags & CFLAG_DMAVALID) { 1615 acmd->cmd_flags &= ~CFLAG_DMAVALID; 1616 1617 (void) ddi_dma_unbind_handle(acmd->cmd_dmahandle); 1618 1619 ddi_dma_free_handle(&acmd->cmd_dmahandle); 1620 1621 acmd->cmd_dmahandle = NULL; 1622 } 1623 } 1624 1625 /* 1626 * tran_sync_pkt - synchronize the DMA object allocated 1627 * @ap: 1628 * @pkt: 1629 * 1630 * The tran_sync_pkt() entry point synchronizes the DMA object allocated for 1631 * the scsi_pkt structure before or after a DMA transfer. The tran_sync_pkt() 1632 * entry point is called when the target driver calls scsi_sync_pkt(). If the 1633 * data transfer direction is a DMA read from device to memory, tran_sync_pkt() 1634 * must synchronize the CPU's view of the data. If the data transfer direction 1635 * is a DMA write from memory to device, tran_sync_pkt() must synchronize the 1636 * device's view of the data. 1637 */ 1638 /*ARGSUSED*/ 1639 static void 1640 megasas_tran_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 1641 { 1642 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1643 1644 /* 1645 * following 'ddi_dma_sync()' API call 1646 * already called for each I/O in the ISR 1647 */ 1648 #if 0 1649 int i; 1650 1651 register struct scsa_cmd *acmd = PKT2CMD(pkt); 1652 1653 if (acmd->cmd_flags & CFLAG_DMAVALID) { 1654 (void) ddi_dma_sync(acmd->cmd_dmahandle, acmd->cmd_dma_offset, 1655 acmd->cmd_dma_len, (acmd->cmd_flags & CFLAG_DMASEND) ? 1656 DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU); 1657 } 1658 #endif 1659 } 1660 1661 /*ARGSUSED*/ 1662 static int 1663 megasas_tran_quiesce(dev_info_t *dip) 1664 { 1665 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1666 1667 return (1); 1668 } 1669 1670 /*ARGSUSED*/ 1671 static int 1672 megasas_tran_unquiesce(dev_info_t *dip) 1673 { 1674 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1675 1676 return (1); 1677 } 1678 1679 /* 1680 * megasas_isr(caddr_t) 1681 * 1682 * The Interrupt Service Routine 1683 * 1684 * Collect status for all completed commands and do callback 1685 * 1686 */ 1687 static uint_t 1688 megasas_isr(struct megasas_instance *instance) 1689 { 1690 int need_softintr; 1691 uint32_t producer; 1692 uint32_t consumer; 1693 uint32_t context; 1694 1695 struct megasas_cmd *cmd; 1696 1697 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1698 1699 ASSERT(instance); 1700 if (!instance->func_ptr->intr_ack(instance)) { 1701 return (DDI_INTR_UNCLAIMED); 1702 } 1703 1704 (void) ddi_dma_sync(instance->mfi_internal_dma_obj.dma_handle, 1705 0, 0, DDI_DMA_SYNC_FORCPU); 1706 1707 if (megasas_check_dma_handle(instance->mfi_internal_dma_obj.dma_handle) 1708 != DDI_SUCCESS) { 1709 megasas_fm_ereport(instance, DDI_FM_DEVICE_NO_RESPONSE); 1710 ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST); 1711 return (DDI_INTR_UNCLAIMED); 1712 } 1713 1714 producer = *instance->producer; 1715 consumer = *instance->consumer; 1716 1717 con_log(CL_ANN1, (CE_CONT, " producer %x consumer %x ", 1718 producer, consumer)); 1719 1720 mutex_enter(&instance->completed_pool_mtx); 1721 1722 while (consumer != producer) { 1723 context = instance->reply_queue[consumer]; 1724 cmd = instance->cmd_list[context]; 1725 mlist_add_tail(&cmd->list, &instance->completed_pool_list); 1726 1727 consumer++; 1728 if (consumer == (instance->max_fw_cmds + 1)) { 1729 consumer = 0; 1730 } 1731 } 1732 1733 mutex_exit(&instance->completed_pool_mtx); 1734 1735 *instance->consumer = consumer; 1736 (void) ddi_dma_sync(instance->mfi_internal_dma_obj.dma_handle, 1737 0, 0, DDI_DMA_SYNC_FORDEV); 1738 1739 if (instance->softint_running) { 1740 need_softintr = 0; 1741 } else { 1742 need_softintr = 1; 1743 } 1744 1745 if (instance->isr_level == HIGH_LEVEL_INTR) { 1746 if (need_softintr) { 1747 ddi_trigger_softintr(instance->soft_intr_id); 1748 } 1749 } else { 1750 /* 1751 * Not a high-level interrupt, therefore call the soft level 1752 * interrupt explicitly 1753 */ 1754 (void) megasas_softintr(instance); 1755 } 1756 1757 return (DDI_INTR_CLAIMED); 1758 } 1759 1760 1761 /* 1762 * ************************************************************************** * 1763 * * 1764 * libraries * 1765 * * 1766 * ************************************************************************** * 1767 */ 1768 /* 1769 * get_mfi_pkt : Get a command from the free pool 1770 */ 1771 static struct megasas_cmd * 1772 get_mfi_pkt(struct megasas_instance *instance) 1773 { 1774 mlist_t *head = &instance->cmd_pool_list; 1775 struct megasas_cmd *cmd = NULL; 1776 1777 mutex_enter(&instance->cmd_pool_mtx); 1778 ASSERT(mutex_owned(&instance->cmd_pool_mtx)); 1779 1780 if (!mlist_empty(head)) { 1781 /* LINTED E_BAD_PTR_CAST_ALIGN */ 1782 cmd = mlist_entry(head->next, struct megasas_cmd, list); 1783 mlist_del_init(head->next); 1784 } 1785 if (cmd != NULL) 1786 cmd->pkt = NULL; 1787 mutex_exit(&instance->cmd_pool_mtx); 1788 1789 return (cmd); 1790 } 1791 1792 /* 1793 * return_mfi_pkt : Return a cmd to free command pool 1794 */ 1795 static void 1796 return_mfi_pkt(struct megasas_instance *instance, struct megasas_cmd *cmd) 1797 { 1798 mutex_enter(&instance->cmd_pool_mtx); 1799 ASSERT(mutex_owned(&instance->cmd_pool_mtx)); 1800 1801 mlist_add(&cmd->list, &instance->cmd_pool_list); 1802 1803 mutex_exit(&instance->cmd_pool_mtx); 1804 } 1805 1806 /* 1807 * destroy_mfi_frame_pool 1808 */ 1809 static void 1810 destroy_mfi_frame_pool(struct megasas_instance *instance) 1811 { 1812 int i; 1813 uint32_t max_cmd = instance->max_fw_cmds; 1814 1815 struct megasas_cmd *cmd; 1816 1817 /* return all frames to pool */ 1818 for (i = 0; i < max_cmd; i++) { 1819 1820 cmd = instance->cmd_list[i]; 1821 1822 if (cmd->frame_dma_obj_status == DMA_OBJ_ALLOCATED) 1823 (void) mega_free_dma_obj(instance, cmd->frame_dma_obj); 1824 1825 cmd->frame_dma_obj_status = DMA_OBJ_FREED; 1826 } 1827 1828 } 1829 1830 /* 1831 * create_mfi_frame_pool 1832 */ 1833 static int 1834 create_mfi_frame_pool(struct megasas_instance *instance) 1835 { 1836 int i = 0; 1837 int cookie_cnt; 1838 uint16_t max_cmd; 1839 uint16_t sge_sz; 1840 uint32_t sgl_sz; 1841 uint32_t tot_frame_size; 1842 1843 struct megasas_cmd *cmd; 1844 1845 max_cmd = instance->max_fw_cmds; 1846 1847 sge_sz = sizeof (struct megasas_sge64); 1848 1849 /* calculated the number of 64byte frames required for SGL */ 1850 sgl_sz = sge_sz * instance->max_num_sge; 1851 tot_frame_size = sgl_sz + MEGAMFI_FRAME_SIZE + SENSE_LENGTH; 1852 1853 con_log(CL_DLEVEL3, (CE_NOTE, "create_mfi_frame_pool: " 1854 "sgl_sz %x tot_frame_size %x", sgl_sz, tot_frame_size)); 1855 1856 while (i < max_cmd) { 1857 cmd = instance->cmd_list[i]; 1858 1859 cmd->frame_dma_obj.size = tot_frame_size; 1860 cmd->frame_dma_obj.dma_attr = megasas_generic_dma_attr; 1861 cmd->frame_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 1862 cmd->frame_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU; 1863 cmd->frame_dma_obj.dma_attr.dma_attr_sgllen = 1; 1864 cmd->frame_dma_obj.dma_attr.dma_attr_align = 64; 1865 1866 1867 cookie_cnt = mega_alloc_dma_obj(instance, &cmd->frame_dma_obj); 1868 1869 if (cookie_cnt == -1 || cookie_cnt > 1) { 1870 con_log(CL_ANN, (CE_WARN, 1871 "create_mfi_frame_pool: could not alloc.")); 1872 return (DDI_FAILURE); 1873 } 1874 1875 bzero(cmd->frame_dma_obj.buffer, tot_frame_size); 1876 1877 cmd->frame_dma_obj_status = DMA_OBJ_ALLOCATED; 1878 cmd->frame = (union megasas_frame *)cmd->frame_dma_obj.buffer; 1879 cmd->frame_phys_addr = 1880 cmd->frame_dma_obj.dma_cookie[0].dmac_address; 1881 1882 cmd->sense = (uint8_t *)(((unsigned long) 1883 cmd->frame_dma_obj.buffer) + 1884 tot_frame_size - SENSE_LENGTH); 1885 cmd->sense_phys_addr = 1886 cmd->frame_dma_obj.dma_cookie[0].dmac_address + 1887 tot_frame_size - SENSE_LENGTH; 1888 1889 if (!cmd->frame || !cmd->sense) { 1890 con_log(CL_ANN, (CE_NOTE, 1891 "megasas: pci_pool_alloc failed \n")); 1892 1893 return (-ENOMEM); 1894 } 1895 1896 cmd->frame->io.context = cmd->index; 1897 i++; 1898 1899 con_log(CL_DLEVEL3, (CE_NOTE, "[%x]-%x", 1900 cmd->frame->io.context, cmd->frame_phys_addr)); 1901 } 1902 1903 return (DDI_SUCCESS); 1904 } 1905 1906 /* 1907 * free_additional_dma_buffer 1908 */ 1909 static void 1910 free_additional_dma_buffer(struct megasas_instance *instance) 1911 { 1912 if (instance->mfi_internal_dma_obj.status == DMA_OBJ_ALLOCATED) { 1913 (void) mega_free_dma_obj(instance, 1914 instance->mfi_internal_dma_obj); 1915 instance->mfi_internal_dma_obj.status = DMA_OBJ_FREED; 1916 } 1917 1918 if (instance->mfi_evt_detail_obj.status == DMA_OBJ_ALLOCATED) { 1919 (void) mega_free_dma_obj(instance, 1920 instance->mfi_evt_detail_obj); 1921 instance->mfi_evt_detail_obj.status = DMA_OBJ_FREED; 1922 } 1923 } 1924 1925 /* 1926 * alloc_additional_dma_buffer 1927 */ 1928 static int 1929 alloc_additional_dma_buffer(struct megasas_instance *instance) 1930 { 1931 uint32_t reply_q_sz; 1932 uint32_t internal_buf_size = PAGESIZE*2; 1933 1934 /* max cmds plus 1 + producer & consumer */ 1935 reply_q_sz = sizeof (uint32_t) * (instance->max_fw_cmds + 1 + 2); 1936 1937 instance->mfi_internal_dma_obj.size = internal_buf_size; 1938 instance->mfi_internal_dma_obj.dma_attr = megasas_generic_dma_attr; 1939 instance->mfi_internal_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 1940 instance->mfi_internal_dma_obj.dma_attr.dma_attr_count_max = 1941 0xFFFFFFFFU; 1942 instance->mfi_internal_dma_obj.dma_attr.dma_attr_sgllen = 1; 1943 1944 if (mega_alloc_dma_obj(instance, &instance->mfi_internal_dma_obj) 1945 != 1) { 1946 con_log(CL_ANN, (CE_WARN, "megaraid: could not alloc reply Q")); 1947 return (DDI_FAILURE); 1948 } 1949 1950 bzero(instance->mfi_internal_dma_obj.buffer, internal_buf_size); 1951 1952 instance->mfi_internal_dma_obj.status |= DMA_OBJ_ALLOCATED; 1953 1954 instance->producer = (uint32_t *)((unsigned long) 1955 instance->mfi_internal_dma_obj.buffer); 1956 instance->consumer = (uint32_t *)((unsigned long) 1957 instance->mfi_internal_dma_obj.buffer + 4); 1958 instance->reply_queue = (uint32_t *)((unsigned long) 1959 instance->mfi_internal_dma_obj.buffer + 8); 1960 instance->internal_buf = (caddr_t)(((unsigned long) 1961 instance->mfi_internal_dma_obj.buffer) + reply_q_sz + 8); 1962 instance->internal_buf_dmac_add = 1963 instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address + 1964 reply_q_sz; 1965 instance->internal_buf_size = internal_buf_size - 1966 (reply_q_sz + 8); 1967 1968 /* allocate evt_detail */ 1969 instance->mfi_evt_detail_obj.size = sizeof (struct megasas_evt_detail); 1970 instance->mfi_evt_detail_obj.dma_attr = megasas_generic_dma_attr; 1971 instance->mfi_evt_detail_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 1972 instance->mfi_evt_detail_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU; 1973 instance->mfi_evt_detail_obj.dma_attr.dma_attr_sgllen = 1; 1974 instance->mfi_evt_detail_obj.dma_attr.dma_attr_align = 1; 1975 1976 if (mega_alloc_dma_obj(instance, &instance->mfi_evt_detail_obj) != 1) { 1977 con_log(CL_ANN, (CE_WARN, "alloc_additional_dma_buffer: " 1978 "could not data transfer buffer alloc.")); 1979 return (DDI_FAILURE); 1980 } 1981 1982 bzero(instance->mfi_evt_detail_obj.buffer, 1983 sizeof (struct megasas_evt_detail)); 1984 1985 instance->mfi_evt_detail_obj.status |= DMA_OBJ_ALLOCATED; 1986 1987 return (DDI_SUCCESS); 1988 } 1989 1990 /* 1991 * free_space_for_mfi 1992 */ 1993 static void 1994 free_space_for_mfi(struct megasas_instance *instance) 1995 { 1996 int i; 1997 uint32_t max_cmd = instance->max_fw_cmds; 1998 1999 /* already freed */ 2000 if (instance->cmd_list == NULL) { 2001 return; 2002 } 2003 2004 free_additional_dma_buffer(instance); 2005 2006 /* first free the MFI frame pool */ 2007 destroy_mfi_frame_pool(instance); 2008 2009 /* free all the commands in the cmd_list */ 2010 for (i = 0; i < instance->max_fw_cmds; i++) { 2011 kmem_free(instance->cmd_list[i], 2012 sizeof (struct megasas_cmd)); 2013 2014 instance->cmd_list[i] = NULL; 2015 } 2016 2017 /* free the cmd_list buffer itself */ 2018 kmem_free(instance->cmd_list, 2019 sizeof (struct megasas_cmd *) * max_cmd); 2020 2021 instance->cmd_list = NULL; 2022 2023 INIT_LIST_HEAD(&instance->cmd_pool_list); 2024 } 2025 2026 /* 2027 * alloc_space_for_mfi 2028 */ 2029 static int 2030 alloc_space_for_mfi(struct megasas_instance *instance) 2031 { 2032 int i; 2033 uint32_t max_cmd; 2034 size_t sz; 2035 2036 struct megasas_cmd *cmd; 2037 2038 max_cmd = instance->max_fw_cmds; 2039 sz = sizeof (struct megasas_cmd *) * max_cmd; 2040 2041 /* 2042 * instance->cmd_list is an array of struct megasas_cmd pointers. 2043 * Allocate the dynamic array first and then allocate individual 2044 * commands. 2045 */ 2046 instance->cmd_list = kmem_zalloc(sz, KM_SLEEP); 2047 ASSERT(instance->cmd_list); 2048 2049 for (i = 0; i < max_cmd; i++) { 2050 instance->cmd_list[i] = kmem_zalloc(sizeof (struct megasas_cmd), 2051 KM_SLEEP); 2052 ASSERT(instance->cmd_list[i]); 2053 } 2054 2055 INIT_LIST_HEAD(&instance->cmd_pool_list); 2056 2057 /* add all the commands to command pool (instance->cmd_pool) */ 2058 for (i = 0; i < max_cmd; i++) { 2059 cmd = instance->cmd_list[i]; 2060 cmd->index = i; 2061 2062 mlist_add_tail(&cmd->list, &instance->cmd_pool_list); 2063 } 2064 2065 /* create a frame pool and assign one frame to each cmd */ 2066 if (create_mfi_frame_pool(instance)) { 2067 con_log(CL_ANN, (CE_NOTE, "error creating frame DMA pool\n")); 2068 return (DDI_FAILURE); 2069 } 2070 2071 /* create a frame pool and assign one frame to each cmd */ 2072 if (alloc_additional_dma_buffer(instance)) { 2073 con_log(CL_ANN, (CE_NOTE, "error creating frame DMA pool\n")); 2074 return (DDI_FAILURE); 2075 } 2076 2077 return (DDI_SUCCESS); 2078 } 2079 2080 /* 2081 * get_ctrl_info 2082 */ 2083 static int 2084 get_ctrl_info(struct megasas_instance *instance, 2085 struct megasas_ctrl_info *ctrl_info) 2086 { 2087 int ret = 0; 2088 2089 struct megasas_cmd *cmd; 2090 struct megasas_dcmd_frame *dcmd; 2091 struct megasas_ctrl_info *ci; 2092 2093 cmd = get_mfi_pkt(instance); 2094 2095 if (!cmd) { 2096 con_log(CL_ANN, (CE_WARN, 2097 "Failed to get a cmd for ctrl info\n")); 2098 return (DDI_FAILURE); 2099 } 2100 2101 dcmd = &cmd->frame->dcmd; 2102 2103 ci = (struct megasas_ctrl_info *)instance->internal_buf; 2104 2105 if (!ci) { 2106 con_log(CL_ANN, (CE_WARN, 2107 "Failed to alloc mem for ctrl info\n")); 2108 return_mfi_pkt(instance, cmd); 2109 return (DDI_FAILURE); 2110 } 2111 2112 (void) memset(ci, 0, sizeof (struct megasas_ctrl_info)); 2113 2114 /* for( i = 0; i < DCMD_MBOX_SZ; i++ ) dcmd->mbox.b[i] = 0; */ 2115 (void) memset(dcmd->mbox.b, 0, DCMD_MBOX_SZ); 2116 2117 dcmd->cmd = MFI_CMD_OP_DCMD; 2118 dcmd->cmd_status = MFI_CMD_STATUS_POLL_MODE; 2119 dcmd->sge_count = 1; 2120 dcmd->flags = MFI_FRAME_DIR_READ; 2121 dcmd->timeout = 0; 2122 dcmd->data_xfer_len = sizeof (struct megasas_ctrl_info); 2123 dcmd->opcode = MR_DCMD_CTRL_GET_INFO; 2124 dcmd->sgl.sge32[0].phys_addr = instance->internal_buf_dmac_add; 2125 dcmd->sgl.sge32[0].length = sizeof (struct megasas_ctrl_info); 2126 2127 cmd->frame_count = 1; 2128 2129 if (!instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) { 2130 ret = 0; 2131 (void) memcpy(ctrl_info, ci, sizeof (struct megasas_ctrl_info)); 2132 } else { 2133 con_log(CL_ANN, (CE_WARN, "get_ctrl_info: Ctrl info failed\n")); 2134 ret = -1; 2135 } 2136 2137 return_mfi_pkt(instance, cmd); 2138 if (megasas_common_check(instance, cmd) != DDI_SUCCESS) { 2139 ret = -1; 2140 } 2141 2142 return (ret); 2143 } 2144 2145 /* 2146 * abort_aen_cmd 2147 */ 2148 static int 2149 abort_aen_cmd(struct megasas_instance *instance, 2150 struct megasas_cmd *cmd_to_abort) 2151 { 2152 int ret = 0; 2153 2154 struct megasas_cmd *cmd; 2155 struct megasas_abort_frame *abort_fr; 2156 2157 cmd = get_mfi_pkt(instance); 2158 2159 if (!cmd) { 2160 con_log(CL_ANN, (CE_WARN, 2161 "Failed to get a cmd for ctrl info\n")); 2162 return (DDI_FAILURE); 2163 } 2164 2165 abort_fr = &cmd->frame->abort; 2166 2167 /* prepare and issue the abort frame */ 2168 abort_fr->cmd = MFI_CMD_OP_ABORT; 2169 abort_fr->cmd_status = MFI_CMD_STATUS_SYNC_MODE; 2170 abort_fr->flags = 0; 2171 abort_fr->abort_context = cmd_to_abort->index; 2172 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr; 2173 abort_fr->abort_mfi_phys_addr_hi = 0; 2174 2175 instance->aen_cmd->abort_aen = 1; 2176 2177 cmd->sync_cmd = MEGASAS_TRUE; 2178 cmd->frame_count = 1; 2179 2180 if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) { 2181 con_log(CL_ANN, (CE_WARN, 2182 "abort_aen_cmd: issue_cmd_in_sync_mode failed\n")); 2183 ret = -1; 2184 } else { 2185 ret = 0; 2186 } 2187 2188 instance->aen_cmd->abort_aen = 1; 2189 instance->aen_cmd = 0; 2190 2191 return_mfi_pkt(instance, cmd); 2192 (void) megasas_common_check(instance, cmd); 2193 2194 return (ret); 2195 } 2196 2197 /* 2198 * init_mfi 2199 */ 2200 static int 2201 init_mfi(struct megasas_instance *instance) 2202 { 2203 off_t reglength; 2204 struct megasas_cmd *cmd; 2205 struct megasas_ctrl_info ctrl_info; 2206 struct megasas_init_frame *init_frame; 2207 struct megasas_init_queue_info *initq_info; 2208 2209 if ((ddi_dev_regsize(instance->dip, REGISTER_SET_IO, ®length) 2210 != DDI_SUCCESS) || reglength < MINIMUM_MFI_MEM_SZ) { 2211 return (DDI_FAILURE); 2212 } 2213 2214 if (reglength > DEFAULT_MFI_MEM_SZ) { 2215 reglength = DEFAULT_MFI_MEM_SZ; 2216 con_log(CL_DLEVEL1, (CE_NOTE, 2217 "mega: register length to map is 0x%lx bytes", reglength)); 2218 } 2219 2220 if (ddi_regs_map_setup(instance->dip, REGISTER_SET_IO, 2221 &instance->regmap, 0, reglength, &endian_attr, 2222 &instance->regmap_handle) != DDI_SUCCESS) { 2223 con_log(CL_ANN, (CE_NOTE, 2224 "megaraid: couldn't map control registers")); 2225 2226 goto fail_mfi_reg_setup; 2227 } 2228 2229 /* we expect the FW state to be READY */ 2230 if (mfi_state_transition_to_ready(instance)) { 2231 con_log(CL_ANN, (CE_WARN, "megaraid: F/W is not ready")); 2232 goto fail_ready_state; 2233 } 2234 2235 /* get various operational parameters from status register */ 2236 instance->max_num_sge = 2237 (instance->func_ptr->read_fw_status_reg(instance) & 2238 0xFF0000) >> 0x10; 2239 /* 2240 * Reduce the max supported cmds by 1. This is to ensure that the 2241 * reply_q_sz (1 more than the max cmd that driver may send) 2242 * does not exceed max cmds that the FW can support 2243 */ 2244 instance->max_fw_cmds = 2245 instance->func_ptr->read_fw_status_reg(instance) & 0xFFFF; 2246 instance->max_fw_cmds = instance->max_fw_cmds - 1; 2247 2248 instance->max_num_sge = 2249 (instance->max_num_sge > MEGASAS_MAX_SGE_CNT) ? 2250 MEGASAS_MAX_SGE_CNT : instance->max_num_sge; 2251 2252 /* create a pool of commands */ 2253 if (alloc_space_for_mfi(instance)) 2254 goto fail_alloc_fw_space; 2255 2256 /* disable interrupt for initial preparation */ 2257 instance->func_ptr->disable_intr(instance); 2258 2259 /* 2260 * Prepare a init frame. Note the init frame points to queue info 2261 * structure. Each frame has SGL allocated after first 64 bytes. For 2262 * this frame - since we don't need any SGL - we use SGL's space as 2263 * queue info structure 2264 */ 2265 cmd = get_mfi_pkt(instance); 2266 2267 init_frame = (struct megasas_init_frame *)cmd->frame; 2268 initq_info = (struct megasas_init_queue_info *) 2269 ((unsigned long)init_frame + 64); 2270 2271 (void) memset(init_frame, 0, MEGAMFI_FRAME_SIZE); 2272 (void) memset(initq_info, 0, sizeof (struct megasas_init_queue_info)); 2273 2274 initq_info->init_flags = 0; 2275 2276 initq_info->reply_queue_entries = instance->max_fw_cmds + 1; 2277 2278 initq_info->producer_index_phys_addr_hi = 0; 2279 initq_info->producer_index_phys_addr_lo = 2280 instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address; 2281 2282 initq_info->consumer_index_phys_addr_hi = 0; 2283 initq_info->consumer_index_phys_addr_lo = 2284 instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address + 4; 2285 2286 initq_info->reply_queue_start_phys_addr_hi = 0; 2287 initq_info->reply_queue_start_phys_addr_lo = 2288 instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address + 8; 2289 2290 init_frame->cmd = MFI_CMD_OP_INIT; 2291 init_frame->cmd_status = MFI_CMD_STATUS_POLL_MODE; 2292 init_frame->flags = 0; 2293 init_frame->queue_info_new_phys_addr_lo = 2294 cmd->frame_phys_addr + 64; 2295 init_frame->queue_info_new_phys_addr_hi = 0; 2296 2297 init_frame->data_xfer_len = sizeof (struct megasas_init_queue_info); 2298 2299 cmd->frame_count = 1; 2300 2301 /* issue the init frame in polled mode */ 2302 if (instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) { 2303 con_log(CL_ANN, (CE_WARN, "failed to init firmware")); 2304 goto fail_fw_init; 2305 } 2306 2307 return_mfi_pkt(instance, cmd); 2308 if (megasas_common_check(instance, cmd) != DDI_SUCCESS) { 2309 goto fail_fw_init; 2310 } 2311 2312 /* gather misc FW related information */ 2313 if (!get_ctrl_info(instance, &ctrl_info)) { 2314 instance->max_sectors_per_req = ctrl_info.max_request_size; 2315 con_log(CL_ANN1, (CE_NOTE, "product name %s ld present %d", 2316 ctrl_info.product_name, ctrl_info.ld_present_count)); 2317 } else { 2318 instance->max_sectors_per_req = instance->max_num_sge * 2319 PAGESIZE / 512; 2320 } 2321 2322 if (megasas_check_acc_handle(instance->regmap_handle) != DDI_SUCCESS) { 2323 goto fail_fw_init; 2324 } 2325 2326 return (0); 2327 2328 fail_fw_init: 2329 fail_alloc_fw_space: 2330 2331 free_space_for_mfi(instance); 2332 2333 fail_ready_state: 2334 ddi_regs_map_free(&instance->regmap_handle); 2335 2336 fail_mfi_reg_setup: 2337 return (DDI_FAILURE); 2338 } 2339 2340 /* 2341 * mfi_state_transition_to_ready : Move the FW to READY state 2342 * 2343 * @reg_set : MFI register set 2344 */ 2345 static int 2346 mfi_state_transition_to_ready(struct megasas_instance *instance) 2347 { 2348 int i; 2349 uint8_t max_wait; 2350 uint32_t fw_ctrl; 2351 uint32_t fw_state; 2352 uint32_t cur_state; 2353 2354 fw_state = 2355 instance->func_ptr->read_fw_status_reg(instance) & MFI_STATE_MASK; 2356 con_log(CL_ANN1, (CE_NOTE, 2357 "mfi_state_transition_to_ready:FW state = 0x%x", fw_state)); 2358 2359 while (fw_state != MFI_STATE_READY) { 2360 con_log(CL_ANN, (CE_NOTE, 2361 "mfi_state_transition_to_ready:FW state%x", fw_state)); 2362 2363 switch (fw_state) { 2364 case MFI_STATE_FAULT: 2365 con_log(CL_ANN, (CE_NOTE, 2366 "megasas: FW in FAULT state!!")); 2367 2368 return (-ENODEV); 2369 case MFI_STATE_WAIT_HANDSHAKE: 2370 /* set the CLR bit in IMR0 */ 2371 con_log(CL_ANN, (CE_NOTE, 2372 "megasas: FW waiting for HANDSHAKE")); 2373 /* 2374 * PCI_Hot Plug: MFI F/W requires 2375 * (MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG) 2376 * to be set 2377 */ 2378 /* WR_IB_MSG_0(MFI_INIT_CLEAR_HANDSHAKE, instance); */ 2379 WR_IB_DOORBELL(MFI_INIT_CLEAR_HANDSHAKE | 2380 MFI_INIT_HOTPLUG, instance); 2381 2382 max_wait = 2; 2383 cur_state = MFI_STATE_WAIT_HANDSHAKE; 2384 break; 2385 case MFI_STATE_BOOT_MESSAGE_PENDING: 2386 /* set the CLR bit in IMR0 */ 2387 con_log(CL_ANN, (CE_NOTE, 2388 "megasas: FW state boot message pending")); 2389 /* 2390 * PCI_Hot Plug: MFI F/W requires 2391 * (MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG) 2392 * to be set 2393 */ 2394 WR_IB_DOORBELL(MFI_INIT_HOTPLUG, instance); 2395 2396 max_wait = 10; 2397 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING; 2398 break; 2399 case MFI_STATE_OPERATIONAL: 2400 /* bring it to READY state; assuming max wait 2 secs */ 2401 instance->func_ptr->disable_intr(instance); 2402 con_log(CL_ANN1, (CE_NOTE, 2403 "megasas: FW in OPERATIONAL state")); 2404 /* 2405 * PCI_Hot Plug: MFI F/W requires 2406 * (MFI_INIT_READY | MFI_INIT_MFIMODE | MFI_INIT_ABORT) 2407 * to be set 2408 */ 2409 /* WR_IB_DOORBELL(MFI_INIT_READY, instance); */ 2410 WR_IB_DOORBELL(MFI_RESET_FLAGS, instance); 2411 2412 max_wait = 10; 2413 cur_state = MFI_STATE_OPERATIONAL; 2414 break; 2415 case MFI_STATE_UNDEFINED: 2416 /* this state should not last for more than 2 seconds */ 2417 con_log(CL_ANN, (CE_NOTE, "FW state undefined\n")); 2418 2419 max_wait = 2; 2420 cur_state = MFI_STATE_UNDEFINED; 2421 break; 2422 case MFI_STATE_BB_INIT: 2423 max_wait = 2; 2424 cur_state = MFI_STATE_BB_INIT; 2425 break; 2426 case MFI_STATE_FW_INIT: 2427 max_wait = 2; 2428 cur_state = MFI_STATE_FW_INIT; 2429 break; 2430 case MFI_STATE_DEVICE_SCAN: 2431 max_wait = 10; 2432 cur_state = MFI_STATE_DEVICE_SCAN; 2433 break; 2434 default: 2435 con_log(CL_ANN, (CE_NOTE, 2436 "megasas: Unknown state 0x%x\n", fw_state)); 2437 return (-ENODEV); 2438 } 2439 2440 /* the cur_state should not last for more than max_wait secs */ 2441 for (i = 0; i < (max_wait * MILLISEC); i++) { 2442 /* fw_state = RD_OB_MSG_0(instance) & MFI_STATE_MASK; */ 2443 fw_state = 2444 instance->func_ptr->read_fw_status_reg(instance) & 2445 MFI_STATE_MASK; 2446 2447 if (fw_state == cur_state) { 2448 delay(1 * drv_usectohz(MILLISEC)); 2449 } else { 2450 break; 2451 } 2452 } 2453 2454 /* return error if fw_state hasn't changed after max_wait */ 2455 if (fw_state == cur_state) { 2456 con_log(CL_ANN, (CE_NOTE, 2457 "FW state hasn't changed in %d secs\n", max_wait)); 2458 return (-ENODEV); 2459 } 2460 }; 2461 2462 fw_ctrl = RD_IB_DOORBELL(instance); 2463 2464 con_log(CL_ANN1, (CE_NOTE, 2465 "mfi_state_transition_to_ready:FW ctrl = 0x%x", fw_ctrl)); 2466 2467 /* 2468 * Write 0xF to the doorbell register to do the following. 2469 * - Abort all outstanding commands (bit 0). 2470 * - Transition from OPERATIONAL to READY state (bit 1). 2471 * - Discard (possible) low MFA posted in 64-bit mode (bit-2). 2472 * - Set to release FW to continue running (i.e. BIOS handshake 2473 * (bit 3). 2474 */ 2475 WR_IB_DOORBELL(0xF, instance); 2476 2477 if (megasas_check_acc_handle(instance->regmap_handle) != DDI_SUCCESS) { 2478 return (-ENODEV); 2479 } 2480 return (0); 2481 } 2482 2483 /* 2484 * get_seq_num 2485 */ 2486 static int 2487 get_seq_num(struct megasas_instance *instance, 2488 struct megasas_evt_log_info *eli) 2489 { 2490 int ret = 0; 2491 2492 dma_obj_t dcmd_dma_obj; 2493 struct megasas_cmd *cmd; 2494 struct megasas_dcmd_frame *dcmd; 2495 2496 cmd = get_mfi_pkt(instance); 2497 2498 if (!cmd) { 2499 cmn_err(CE_WARN, "megasas: failed to get a cmd\n"); 2500 return (-ENOMEM); 2501 } 2502 2503 dcmd = &cmd->frame->dcmd; 2504 2505 /* allocate the data transfer buffer */ 2506 dcmd_dma_obj.size = sizeof (struct megasas_evt_log_info); 2507 dcmd_dma_obj.dma_attr = megasas_generic_dma_attr; 2508 dcmd_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 2509 dcmd_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU; 2510 dcmd_dma_obj.dma_attr.dma_attr_sgllen = 1; 2511 dcmd_dma_obj.dma_attr.dma_attr_align = 1; 2512 2513 if (mega_alloc_dma_obj(instance, &dcmd_dma_obj) != 1) { 2514 con_log(CL_ANN, (CE_WARN, 2515 "get_seq_num: could not data transfer buffer alloc.")); 2516 return (DDI_FAILURE); 2517 } 2518 2519 (void) memset(dcmd_dma_obj.buffer, 0, 2520 sizeof (struct megasas_evt_log_info)); 2521 2522 (void) memset(dcmd->mbox.b, 0, DCMD_MBOX_SZ); 2523 2524 dcmd->cmd = MFI_CMD_OP_DCMD; 2525 dcmd->cmd_status = 0; 2526 dcmd->sge_count = 1; 2527 dcmd->flags = MFI_FRAME_DIR_READ; 2528 dcmd->timeout = 0; 2529 dcmd->data_xfer_len = sizeof (struct megasas_evt_log_info); 2530 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO; 2531 dcmd->sgl.sge32[0].length = sizeof (struct megasas_evt_log_info); 2532 dcmd->sgl.sge32[0].phys_addr = dcmd_dma_obj.dma_cookie[0].dmac_address; 2533 2534 cmd->sync_cmd = MEGASAS_TRUE; 2535 cmd->frame_count = 1; 2536 2537 if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) { 2538 cmn_err(CE_WARN, "get_seq_num: " 2539 "failed to issue MR_DCMD_CTRL_EVENT_GET_INFO\n"); 2540 ret = -1; 2541 } else { 2542 /* copy the data back into callers buffer */ 2543 bcopy(dcmd_dma_obj.buffer, eli, 2544 sizeof (struct megasas_evt_log_info)); 2545 ret = 0; 2546 } 2547 2548 if (mega_free_dma_obj(instance, dcmd_dma_obj) != DDI_SUCCESS) 2549 ret = -1; 2550 2551 return_mfi_pkt(instance, cmd); 2552 if (megasas_common_check(instance, cmd) != DDI_SUCCESS) { 2553 ret = -1; 2554 } 2555 return (ret); 2556 } 2557 2558 /* 2559 * start_mfi_aen 2560 */ 2561 static int 2562 start_mfi_aen(struct megasas_instance *instance) 2563 { 2564 int ret = 0; 2565 2566 struct megasas_evt_log_info eli; 2567 union megasas_evt_class_locale class_locale; 2568 2569 /* get the latest sequence number from FW */ 2570 (void) memset(&eli, 0, sizeof (struct megasas_evt_log_info)); 2571 2572 if (get_seq_num(instance, &eli)) { 2573 cmn_err(CE_WARN, "start_mfi_aen: failed to get seq num\n"); 2574 return (-1); 2575 } 2576 2577 /* register AEN with FW for latest sequence number plus 1 */ 2578 class_locale.members.reserved = 0; 2579 class_locale.members.locale = MR_EVT_LOCALE_ALL; 2580 class_locale.members.class = MR_EVT_CLASS_CRITICAL; 2581 2582 ret = register_mfi_aen(instance, eli.newest_seq_num + 1, 2583 class_locale.word); 2584 2585 if (ret) { 2586 cmn_err(CE_WARN, "start_mfi_aen: aen registration failed\n"); 2587 return (-1); 2588 } 2589 2590 return (ret); 2591 } 2592 2593 /* 2594 * flush_cache 2595 */ 2596 static void 2597 flush_cache(struct megasas_instance *instance) 2598 { 2599 struct megasas_cmd *cmd; 2600 struct megasas_dcmd_frame *dcmd; 2601 2602 if (!(cmd = get_mfi_pkt(instance))) 2603 return; 2604 2605 dcmd = &cmd->frame->dcmd; 2606 2607 (void) memset(dcmd->mbox.b, 0, DCMD_MBOX_SZ); 2608 2609 dcmd->cmd = MFI_CMD_OP_DCMD; 2610 dcmd->cmd_status = 0x0; 2611 dcmd->sge_count = 0; 2612 dcmd->flags = MFI_FRAME_DIR_NONE; 2613 dcmd->timeout = 0; 2614 dcmd->data_xfer_len = 0; 2615 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH; 2616 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE; 2617 2618 cmd->frame_count = 1; 2619 2620 if (instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) { 2621 cmn_err(CE_WARN, 2622 "flush_cache: failed to issue MFI_DCMD_CTRL_CACHE_FLUSH\n"); 2623 } 2624 con_log(CL_DLEVEL1, (CE_NOTE, "done")); 2625 return_mfi_pkt(instance, cmd); 2626 (void) megasas_common_check(instance, cmd); 2627 } 2628 2629 /* 2630 * service_mfi_aen- Completes an AEN command 2631 * @instance: Adapter soft state 2632 * @cmd: Command to be completed 2633 * 2634 */ 2635 static void 2636 service_mfi_aen(struct megasas_instance *instance, struct megasas_cmd *cmd) 2637 { 2638 uint32_t seq_num; 2639 struct megasas_evt_detail *evt_detail = 2640 (struct megasas_evt_detail *)instance->mfi_evt_detail_obj.buffer; 2641 2642 cmd->cmd_status = cmd->frame->io.cmd_status; 2643 2644 if (cmd->cmd_status == ENODATA) { 2645 cmd->cmd_status = 0; 2646 } 2647 2648 /* 2649 * log the MFI AEN event to the sysevent queue so that 2650 * application will get noticed 2651 */ 2652 if (ddi_log_sysevent(instance->dip, DDI_VENDOR_LSI, "LSIMEGA", "SAS", 2653 NULL, NULL, DDI_NOSLEEP) != DDI_SUCCESS) { 2654 int instance_no = ddi_get_instance(instance->dip); 2655 con_log(CL_ANN, (CE_WARN, 2656 "mega%d: Failed to log AEN event", instance_no)); 2657 } 2658 2659 /* get copy of seq_num and class/locale for re-registration */ 2660 seq_num = evt_detail->seq_num; 2661 seq_num++; 2662 (void) memset(instance->mfi_evt_detail_obj.buffer, 0, 2663 sizeof (struct megasas_evt_detail)); 2664 2665 cmd->frame->dcmd.cmd_status = 0x0; 2666 cmd->frame->dcmd.mbox.w[0] = seq_num; 2667 2668 instance->aen_seq_num = seq_num; 2669 2670 cmd->frame_count = 1; 2671 2672 /* Issue the aen registration frame */ 2673 instance->func_ptr->issue_cmd(cmd, instance); 2674 } 2675 2676 /* 2677 * complete_cmd_in_sync_mode - Completes an internal command 2678 * @instance: Adapter soft state 2679 * @cmd: Command to be completed 2680 * 2681 * The issue_cmd_in_sync_mode() function waits for a command to complete 2682 * after it issues a command. This function wakes up that waiting routine by 2683 * calling wake_up() on the wait queue. 2684 */ 2685 static void 2686 complete_cmd_in_sync_mode(struct megasas_instance *instance, 2687 struct megasas_cmd *cmd) 2688 { 2689 cmd->cmd_status = cmd->frame->io.cmd_status; 2690 2691 cmd->sync_cmd = MEGASAS_FALSE; 2692 2693 if (cmd->cmd_status == ENODATA) { 2694 cmd->cmd_status = 0; 2695 } 2696 2697 cv_broadcast(&instance->int_cmd_cv); 2698 } 2699 2700 /* 2701 * megasas_softintr - The Software ISR 2702 * @param arg : HBA soft state 2703 * 2704 * called from high-level interrupt if hi-level interrupt are not there, 2705 * otherwise triggered as a soft interrupt 2706 */ 2707 static uint_t 2708 megasas_softintr(struct megasas_instance *instance) 2709 { 2710 struct scsi_pkt *pkt; 2711 struct scsa_cmd *acmd; 2712 struct megasas_cmd *cmd; 2713 struct mlist_head *pos, *next; 2714 mlist_t process_list; 2715 struct megasas_header *hdr; 2716 struct scsi_arq_status *arqstat; 2717 2718 con_log(CL_ANN1, (CE_CONT, "megasas_softintr called")); 2719 2720 ASSERT(instance); 2721 mutex_enter(&instance->completed_pool_mtx); 2722 2723 if (mlist_empty(&instance->completed_pool_list)) { 2724 mutex_exit(&instance->completed_pool_mtx); 2725 return (DDI_INTR_UNCLAIMED); 2726 } 2727 2728 instance->softint_running = 1; 2729 2730 INIT_LIST_HEAD(&process_list); 2731 mlist_splice(&instance->completed_pool_list, &process_list); 2732 INIT_LIST_HEAD(&instance->completed_pool_list); 2733 2734 mutex_exit(&instance->completed_pool_mtx); 2735 2736 /* perform all callbacks first, before releasing the SCBs */ 2737 mlist_for_each_safe(pos, next, &process_list) { 2738 /* LINTED E_BAD_PTR_CAST_ALIGN */ 2739 cmd = mlist_entry(pos, struct megasas_cmd, list); 2740 2741 /* syncronize the Cmd frame for the controller */ 2742 (void) ddi_dma_sync(cmd->frame_dma_obj.dma_handle, 2743 0, 0, DDI_DMA_SYNC_FORCPU); 2744 2745 if (megasas_check_dma_handle(cmd->frame_dma_obj.dma_handle) != 2746 DDI_SUCCESS) { 2747 megasas_fm_ereport(instance, DDI_FM_DEVICE_NO_RESPONSE); 2748 ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST); 2749 return (DDI_INTR_UNCLAIMED); 2750 } 2751 2752 hdr = &cmd->frame->hdr; 2753 2754 /* remove the internal command from the process list */ 2755 mlist_del_init(&cmd->list); 2756 2757 switch (hdr->cmd) { 2758 case MFI_CMD_OP_PD_SCSI: 2759 case MFI_CMD_OP_LD_SCSI: 2760 case MFI_CMD_OP_LD_READ: 2761 case MFI_CMD_OP_LD_WRITE: 2762 /* 2763 * MFI_CMD_OP_PD_SCSI and MFI_CMD_OP_LD_SCSI 2764 * could have been issued either through an 2765 * IO path or an IOCTL path. If it was via IOCTL, 2766 * we will send it to internal completion. 2767 */ 2768 if (cmd->sync_cmd == MEGASAS_TRUE) { 2769 complete_cmd_in_sync_mode(instance, cmd); 2770 break; 2771 } 2772 2773 /* regular commands */ 2774 acmd = cmd->cmd; 2775 pkt = CMD2PKT(acmd); 2776 2777 if (acmd->cmd_flags & CFLAG_DMAVALID) { 2778 if (acmd->cmd_flags & CFLAG_CONSISTENT) { 2779 (void) ddi_dma_sync(acmd->cmd_dmahandle, 2780 acmd->cmd_dma_offset, 2781 acmd->cmd_dma_len, 2782 DDI_DMA_SYNC_FORCPU); 2783 } 2784 } 2785 2786 pkt->pkt_reason = CMD_CMPLT; 2787 pkt->pkt_statistics = 0; 2788 pkt->pkt_state = STATE_GOT_BUS 2789 | STATE_GOT_TARGET | STATE_SENT_CMD 2790 | STATE_XFERRED_DATA | STATE_GOT_STATUS; 2791 2792 con_log(CL_ANN1, (CE_CONT, 2793 "CDB[0] = %x completed for %s: size %lx context %x", 2794 pkt->pkt_cdbp[0], ((acmd->islogical) ? "LD" : "PD"), 2795 acmd->cmd_dmacount, hdr->context)); 2796 2797 if (pkt->pkt_cdbp[0] == SCMD_INQUIRY) { 2798 struct scsi_inquiry *inq; 2799 2800 if (acmd->cmd_dmacount != 0) { 2801 bp_mapin(acmd->cmd_buf); 2802 inq = (struct scsi_inquiry *) 2803 acmd->cmd_buf->b_un.b_addr; 2804 2805 /* don't expose physical drives to OS */ 2806 if (acmd->islogical && 2807 (hdr->cmd_status == MFI_STAT_OK)) { 2808 display_scsi_inquiry( 2809 (caddr_t)inq); 2810 } else if ((hdr->cmd_status == 2811 MFI_STAT_OK) && inq->inq_dtype == 2812 DTYPE_DIRECT) { 2813 2814 display_scsi_inquiry( 2815 (caddr_t)inq); 2816 2817 /* for physical disk */ 2818 hdr->cmd_status = 2819 MFI_STAT_DEVICE_NOT_FOUND; 2820 } 2821 } 2822 } 2823 2824 switch (hdr->cmd_status) { 2825 case MFI_STAT_OK: 2826 pkt->pkt_scbp[0] = STATUS_GOOD; 2827 break; 2828 case MFI_STAT_LD_CC_IN_PROGRESS: 2829 case MFI_STAT_LD_RECON_IN_PROGRESS: 2830 /* SJ - these are not correct way */ 2831 pkt->pkt_scbp[0] = STATUS_GOOD; 2832 break; 2833 case MFI_STAT_LD_INIT_IN_PROGRESS: 2834 con_log(CL_ANN, 2835 (CE_WARN, "Initialization in Progress")); 2836 pkt->pkt_reason = CMD_TRAN_ERR; 2837 2838 break; 2839 case MFI_STAT_SCSI_DONE_WITH_ERROR: 2840 con_log(CL_ANN1, (CE_CONT, "scsi_done error")); 2841 2842 pkt->pkt_reason = CMD_CMPLT; 2843 ((struct scsi_status *) 2844 pkt->pkt_scbp)->sts_chk = 1; 2845 2846 if (pkt->pkt_cdbp[0] == SCMD_TEST_UNIT_READY) { 2847 2848 con_log(CL_ANN, 2849 (CE_WARN, "TEST_UNIT_READY fail")); 2850 2851 } else { 2852 pkt->pkt_state |= STATE_ARQ_DONE; 2853 arqstat = (void *)(pkt->pkt_scbp); 2854 arqstat->sts_rqpkt_reason = CMD_CMPLT; 2855 arqstat->sts_rqpkt_resid = 0; 2856 arqstat->sts_rqpkt_state |= 2857 STATE_GOT_BUS | STATE_GOT_TARGET 2858 | STATE_SENT_CMD 2859 | STATE_XFERRED_DATA; 2860 *(uint8_t *)&arqstat->sts_rqpkt_status = 2861 STATUS_GOOD; 2862 2863 bcopy(cmd->sense, 2864 &(arqstat->sts_sensedata), 2865 acmd->cmd_scblen - 2866 offsetof(struct scsi_arq_status, 2867 sts_sensedata)); 2868 } 2869 break; 2870 case MFI_STAT_LD_OFFLINE: 2871 case MFI_STAT_DEVICE_NOT_FOUND: 2872 con_log(CL_ANN1, (CE_CONT, 2873 "device not found error")); 2874 pkt->pkt_reason = CMD_DEV_GONE; 2875 pkt->pkt_statistics = STAT_DISCON; 2876 break; 2877 case MFI_STAT_LD_LBA_OUT_OF_RANGE: 2878 pkt->pkt_state |= STATE_ARQ_DONE; 2879 pkt->pkt_reason = CMD_CMPLT; 2880 ((struct scsi_status *) 2881 pkt->pkt_scbp)->sts_chk = 1; 2882 2883 arqstat = (void *)(pkt->pkt_scbp); 2884 arqstat->sts_rqpkt_reason = CMD_CMPLT; 2885 arqstat->sts_rqpkt_resid = 0; 2886 arqstat->sts_rqpkt_state |= STATE_GOT_BUS 2887 | STATE_GOT_TARGET | STATE_SENT_CMD 2888 | STATE_XFERRED_DATA; 2889 *(uint8_t *)&arqstat->sts_rqpkt_status = 2890 STATUS_GOOD; 2891 2892 arqstat->sts_sensedata.es_valid = 1; 2893 arqstat->sts_sensedata.es_key = 2894 KEY_ILLEGAL_REQUEST; 2895 arqstat->sts_sensedata.es_class = 2896 CLASS_EXTENDED_SENSE; 2897 2898 /* 2899 * LOGICAL BLOCK ADDRESS OUT OF RANGE: 2900 * ASC: 0x21h; ASCQ: 0x00h; 2901 */ 2902 arqstat->sts_sensedata.es_add_code = 0x21; 2903 arqstat->sts_sensedata.es_qual_code = 0x00; 2904 2905 break; 2906 2907 default: 2908 con_log(CL_ANN, (CE_CONT, "Unknown status!")); 2909 pkt->pkt_reason = CMD_TRAN_ERR; 2910 2911 break; 2912 } 2913 2914 atomic_add_16(&instance->fw_outstanding, (-1)); 2915 2916 return_mfi_pkt(instance, cmd); 2917 2918 (void) megasas_common_check(instance, cmd); 2919 2920 if (acmd->cmd_dmahandle) { 2921 if (megasas_check_dma_handle( 2922 acmd->cmd_dmahandle) != DDI_SUCCESS) { 2923 ddi_fm_service_impact(instance->dip, 2924 DDI_SERVICE_UNAFFECTED); 2925 pkt->pkt_reason = CMD_TRAN_ERR; 2926 pkt->pkt_statistics = 0; 2927 } 2928 } 2929 2930 /* Call the callback routine */ 2931 if (((pkt->pkt_flags & FLAG_NOINTR) == 0) && 2932 pkt->pkt_comp) { 2933 (*pkt->pkt_comp)(pkt); 2934 } 2935 2936 break; 2937 case MFI_CMD_OP_SMP: 2938 case MFI_CMD_OP_STP: 2939 complete_cmd_in_sync_mode(instance, cmd); 2940 break; 2941 case MFI_CMD_OP_DCMD: 2942 /* see if got an event notification */ 2943 if (cmd->frame->dcmd.opcode == 2944 MR_DCMD_CTRL_EVENT_WAIT) { 2945 if ((instance->aen_cmd == cmd) && 2946 (instance->aen_cmd->abort_aen)) { 2947 con_log(CL_ANN, (CE_WARN, 2948 "megasas_softintr: " 2949 "aborted_aen returned")); 2950 } else { 2951 service_mfi_aen(instance, cmd); 2952 } 2953 } else { 2954 complete_cmd_in_sync_mode(instance, cmd); 2955 } 2956 2957 break; 2958 case MFI_CMD_OP_ABORT: 2959 con_log(CL_ANN, (CE_WARN, "MFI_CMD_OP_ABORT complete")); 2960 /* 2961 * MFI_CMD_OP_ABORT successfully completed 2962 * in the synchronous mode 2963 */ 2964 complete_cmd_in_sync_mode(instance, cmd); 2965 break; 2966 default: 2967 megasas_fm_ereport(instance, DDI_FM_DEVICE_NO_RESPONSE); 2968 ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST); 2969 2970 if (cmd->pkt != NULL) { 2971 pkt = cmd->pkt; 2972 if (((pkt->pkt_flags & FLAG_NOINTR) == 0) && 2973 pkt->pkt_comp) { 2974 (*pkt->pkt_comp)(pkt); 2975 } 2976 } 2977 con_log(CL_ANN, (CE_WARN, "Cmd type unknown !!")); 2978 break; 2979 } 2980 } 2981 2982 instance->softint_running = 0; 2983 2984 return (DDI_INTR_CLAIMED); 2985 } 2986 2987 /* 2988 * mega_alloc_dma_obj 2989 * 2990 * Allocate the memory and other resources for an dma object. 2991 */ 2992 static int 2993 mega_alloc_dma_obj(struct megasas_instance *instance, dma_obj_t *obj) 2994 { 2995 int i; 2996 size_t alen = 0; 2997 uint_t cookie_cnt; 2998 2999 i = ddi_dma_alloc_handle(instance->dip, &obj->dma_attr, 3000 DDI_DMA_SLEEP, NULL, &obj->dma_handle); 3001 if (i != DDI_SUCCESS) { 3002 3003 switch (i) { 3004 case DDI_DMA_BADATTR : 3005 con_log(CL_ANN, (CE_WARN, 3006 "Failed ddi_dma_alloc_handle- Bad atrib")); 3007 break; 3008 case DDI_DMA_NORESOURCES : 3009 con_log(CL_ANN, (CE_WARN, 3010 "Failed ddi_dma_alloc_handle- No Resources")); 3011 break; 3012 default : 3013 con_log(CL_ANN, (CE_WARN, 3014 "Failed ddi_dma_alloc_handle :unknown %d", i)); 3015 break; 3016 } 3017 3018 return (-1); 3019 } 3020 3021 if ((ddi_dma_mem_alloc(obj->dma_handle, obj->size, &endian_attr, 3022 DDI_DMA_RDWR | DDI_DMA_STREAMING, DDI_DMA_SLEEP, NULL, 3023 &obj->buffer, &alen, &obj->acc_handle) != DDI_SUCCESS) || 3024 alen < obj->size) { 3025 3026 ddi_dma_free_handle(&obj->dma_handle); 3027 3028 con_log(CL_ANN, (CE_WARN, "Failed : ddi_dma_mem_alloc")); 3029 3030 return (-1); 3031 } 3032 3033 if (ddi_dma_addr_bind_handle(obj->dma_handle, NULL, obj->buffer, 3034 obj->size, DDI_DMA_RDWR | DDI_DMA_STREAMING, DDI_DMA_SLEEP, 3035 NULL, &obj->dma_cookie[0], &cookie_cnt) != DDI_SUCCESS) { 3036 3037 ddi_dma_mem_free(&obj->acc_handle); 3038 ddi_dma_free_handle(&obj->dma_handle); 3039 3040 con_log(CL_ANN, (CE_WARN, "Failed : ddi_dma_addr_bind_handle")); 3041 3042 return (-1); 3043 } 3044 3045 if (megasas_check_dma_handle(obj->dma_handle) != DDI_SUCCESS) { 3046 ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST); 3047 return (-1); 3048 } 3049 3050 if (megasas_check_acc_handle(obj->acc_handle) != DDI_SUCCESS) { 3051 ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST); 3052 return (-1); 3053 } 3054 3055 return (cookie_cnt); 3056 } 3057 3058 /* 3059 * mega_free_dma_obj(struct megasas_instance *, dma_obj_t) 3060 * 3061 * De-allocate the memory and other resources for an dma object, which must 3062 * have been alloated by a previous call to mega_alloc_dma_obj() 3063 */ 3064 static int 3065 mega_free_dma_obj(struct megasas_instance *instance, dma_obj_t obj) 3066 { 3067 3068 if (megasas_check_dma_handle(obj.dma_handle) != DDI_SUCCESS) { 3069 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED); 3070 return (DDI_FAILURE); 3071 } 3072 3073 if (megasas_check_acc_handle(obj.acc_handle) != DDI_SUCCESS) { 3074 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED); 3075 return (DDI_FAILURE); 3076 } 3077 3078 (void) ddi_dma_unbind_handle(obj.dma_handle); 3079 ddi_dma_mem_free(&obj.acc_handle); 3080 ddi_dma_free_handle(&obj.dma_handle); 3081 3082 return (DDI_SUCCESS); 3083 } 3084 3085 /* 3086 * megasas_dma_alloc(instance_t *, struct scsi_pkt *, struct buf *, 3087 * int, int (*)()) 3088 * 3089 * Allocate dma resources for a new scsi command 3090 */ 3091 static int 3092 megasas_dma_alloc(struct megasas_instance *instance, struct scsi_pkt *pkt, 3093 struct buf *bp, int flags, int (*callback)()) 3094 { 3095 int dma_flags; 3096 int (*cb)(caddr_t); 3097 int i; 3098 3099 ddi_dma_attr_t tmp_dma_attr = megasas_generic_dma_attr; 3100 struct scsa_cmd *acmd = PKT2CMD(pkt); 3101 3102 acmd->cmd_buf = bp; 3103 3104 if (bp->b_flags & B_READ) { 3105 acmd->cmd_flags &= ~CFLAG_DMASEND; 3106 dma_flags = DDI_DMA_READ; 3107 } else { 3108 acmd->cmd_flags |= CFLAG_DMASEND; 3109 dma_flags = DDI_DMA_WRITE; 3110 } 3111 3112 if (flags & PKT_CONSISTENT) { 3113 acmd->cmd_flags |= CFLAG_CONSISTENT; 3114 dma_flags |= DDI_DMA_CONSISTENT; 3115 } 3116 3117 if (flags & PKT_DMA_PARTIAL) { 3118 dma_flags |= DDI_DMA_PARTIAL; 3119 } 3120 3121 dma_flags |= DDI_DMA_REDZONE; 3122 3123 cb = (callback == NULL_FUNC) ? DDI_DMA_DONTWAIT : DDI_DMA_SLEEP; 3124 3125 tmp_dma_attr.dma_attr_sgllen = instance->max_num_sge; 3126 3127 if ((i = ddi_dma_alloc_handle(instance->dip, &tmp_dma_attr, 3128 cb, 0, &acmd->cmd_dmahandle)) != DDI_SUCCESS) { 3129 switch (i) { 3130 case DDI_DMA_BADATTR: 3131 bioerror(bp, EFAULT); 3132 return (-1); 3133 3134 case DDI_DMA_NORESOURCES: 3135 bioerror(bp, 0); 3136 return (-1); 3137 3138 default: 3139 con_log(CL_ANN, (CE_PANIC, "ddi_dma_alloc_handle: " 3140 "0x%x impossible\n", i)); 3141 bioerror(bp, EFAULT); 3142 return (-1); 3143 } 3144 } 3145 3146 i = ddi_dma_buf_bind_handle(acmd->cmd_dmahandle, bp, dma_flags, 3147 cb, 0, &acmd->cmd_dmacookies[0], &acmd->cmd_ncookies); 3148 3149 switch (i) { 3150 case DDI_DMA_PARTIAL_MAP: 3151 if ((dma_flags & DDI_DMA_PARTIAL) == 0) { 3152 con_log(CL_ANN, (CE_PANIC, "ddi_dma_buf_bind_handle: " 3153 "DDI_DMA_PARTIAL_MAP impossible\n")); 3154 goto no_dma_cookies; 3155 } 3156 3157 if (ddi_dma_numwin(acmd->cmd_dmahandle, &acmd->cmd_nwin) == 3158 DDI_FAILURE) { 3159 con_log(CL_ANN, (CE_PANIC, "ddi_dma_numwin failed\n")); 3160 goto no_dma_cookies; 3161 } 3162 3163 if (ddi_dma_getwin(acmd->cmd_dmahandle, acmd->cmd_curwin, 3164 &acmd->cmd_dma_offset, &acmd->cmd_dma_len, 3165 &acmd->cmd_dmacookies[0], &acmd->cmd_ncookies) == 3166 DDI_FAILURE) { 3167 3168 con_log(CL_ANN, (CE_PANIC, "ddi_dma_getwin failed\n")); 3169 goto no_dma_cookies; 3170 } 3171 3172 goto get_dma_cookies; 3173 case DDI_DMA_MAPPED: 3174 acmd->cmd_nwin = 1; 3175 acmd->cmd_dma_len = 0; 3176 acmd->cmd_dma_offset = 0; 3177 3178 get_dma_cookies: 3179 i = 0; 3180 acmd->cmd_dmacount = 0; 3181 for (;;) { 3182 acmd->cmd_dmacount += 3183 acmd->cmd_dmacookies[i++].dmac_size; 3184 3185 if (i == instance->max_num_sge || 3186 i == acmd->cmd_ncookies) 3187 break; 3188 3189 ddi_dma_nextcookie(acmd->cmd_dmahandle, 3190 &acmd->cmd_dmacookies[i]); 3191 } 3192 3193 acmd->cmd_cookie = i; 3194 acmd->cmd_cookiecnt = i; 3195 3196 acmd->cmd_flags |= CFLAG_DMAVALID; 3197 3198 if (bp->b_bcount >= acmd->cmd_dmacount) { 3199 pkt->pkt_resid = bp->b_bcount - acmd->cmd_dmacount; 3200 } else { 3201 pkt->pkt_resid = 0; 3202 } 3203 3204 return (0); 3205 case DDI_DMA_NORESOURCES: 3206 bioerror(bp, 0); 3207 break; 3208 case DDI_DMA_NOMAPPING: 3209 bioerror(bp, EFAULT); 3210 break; 3211 case DDI_DMA_TOOBIG: 3212 bioerror(bp, EINVAL); 3213 break; 3214 case DDI_DMA_INUSE: 3215 con_log(CL_ANN, (CE_PANIC, "ddi_dma_buf_bind_handle:" 3216 " DDI_DMA_INUSE impossible\n")); 3217 break; 3218 default: 3219 con_log(CL_ANN, (CE_PANIC, "ddi_dma_buf_bind_handle: " 3220 "0x%x impossible\n", i)); 3221 break; 3222 } 3223 3224 no_dma_cookies: 3225 ddi_dma_free_handle(&acmd->cmd_dmahandle); 3226 acmd->cmd_dmahandle = NULL; 3227 acmd->cmd_flags &= ~CFLAG_DMAVALID; 3228 return (-1); 3229 } 3230 3231 /* 3232 * megasas_dma_move(struct megasas_instance *, struct scsi_pkt *, struct buf *) 3233 * 3234 * move dma resources to next dma window 3235 * 3236 */ 3237 static int 3238 megasas_dma_move(struct megasas_instance *instance, struct scsi_pkt *pkt, 3239 struct buf *bp) 3240 { 3241 int i = 0; 3242 3243 struct scsa_cmd *acmd = PKT2CMD(pkt); 3244 3245 /* 3246 * If there are no more cookies remaining in this window, 3247 * must move to the next window first. 3248 */ 3249 if (acmd->cmd_cookie == acmd->cmd_ncookies) { 3250 if (acmd->cmd_curwin == acmd->cmd_nwin && acmd->cmd_nwin == 1) { 3251 return (0); 3252 } 3253 3254 /* at last window, cannot move */ 3255 if (++acmd->cmd_curwin >= acmd->cmd_nwin) { 3256 return (-1); 3257 } 3258 3259 if (ddi_dma_getwin(acmd->cmd_dmahandle, acmd->cmd_curwin, 3260 &acmd->cmd_dma_offset, &acmd->cmd_dma_len, 3261 &acmd->cmd_dmacookies[0], &acmd->cmd_ncookies) == 3262 DDI_FAILURE) { 3263 return (-1); 3264 } 3265 3266 acmd->cmd_cookie = 0; 3267 } else { 3268 /* still more cookies in this window - get the next one */ 3269 ddi_dma_nextcookie(acmd->cmd_dmahandle, 3270 &acmd->cmd_dmacookies[0]); 3271 } 3272 3273 /* get remaining cookies in this window, up to our maximum */ 3274 for (;;) { 3275 acmd->cmd_dmacount += acmd->cmd_dmacookies[i++].dmac_size; 3276 acmd->cmd_cookie++; 3277 3278 if (i == instance->max_num_sge || 3279 acmd->cmd_cookie == acmd->cmd_ncookies) { 3280 break; 3281 } 3282 3283 ddi_dma_nextcookie(acmd->cmd_dmahandle, 3284 &acmd->cmd_dmacookies[i]); 3285 } 3286 3287 acmd->cmd_cookiecnt = i; 3288 3289 if (bp->b_bcount >= acmd->cmd_dmacount) { 3290 pkt->pkt_resid = bp->b_bcount - acmd->cmd_dmacount; 3291 } else { 3292 pkt->pkt_resid = 0; 3293 } 3294 3295 return (0); 3296 } 3297 3298 /* 3299 * build_cmd 3300 */ 3301 static struct megasas_cmd * 3302 build_cmd(struct megasas_instance *instance, struct scsi_address *ap, 3303 struct scsi_pkt *pkt, uchar_t *cmd_done) 3304 { 3305 uint16_t flags = 0; 3306 uint32_t i; 3307 uint32_t context; 3308 uint32_t sge_bytes; 3309 3310 struct megasas_cmd *cmd; 3311 struct megasas_sge32 *mfi_sgl; 3312 struct scsa_cmd *acmd = PKT2CMD(pkt); 3313 struct megasas_pthru_frame *pthru; 3314 struct megasas_io_frame *ldio; 3315 3316 /* find out if this is logical or physical drive command. */ 3317 acmd->islogical = MEGADRV_IS_LOGICAL(ap); 3318 acmd->device_id = MAP_DEVICE_ID(instance, ap); 3319 *cmd_done = 0; 3320 3321 /* get the command packet */ 3322 if (!(cmd = get_mfi_pkt(instance))) { 3323 return (NULL); 3324 } 3325 3326 cmd->pkt = pkt; 3327 cmd->cmd = acmd; 3328 3329 /* lets get the command directions */ 3330 if (acmd->cmd_flags & CFLAG_DMASEND) { 3331 flags = MFI_FRAME_DIR_WRITE; 3332 3333 if (acmd->cmd_flags & CFLAG_CONSISTENT) { 3334 (void) ddi_dma_sync(acmd->cmd_dmahandle, 3335 acmd->cmd_dma_offset, acmd->cmd_dma_len, 3336 DDI_DMA_SYNC_FORDEV); 3337 } 3338 } else if (acmd->cmd_flags & ~CFLAG_DMASEND) { 3339 flags = MFI_FRAME_DIR_READ; 3340 3341 if (acmd->cmd_flags & CFLAG_CONSISTENT) { 3342 (void) ddi_dma_sync(acmd->cmd_dmahandle, 3343 acmd->cmd_dma_offset, acmd->cmd_dma_len, 3344 DDI_DMA_SYNC_FORCPU); 3345 } 3346 } else { 3347 flags = MFI_FRAME_DIR_NONE; 3348 } 3349 3350 /* flags |= MFI_FRAME_SGL64; */ 3351 3352 switch (pkt->pkt_cdbp[0]) { 3353 3354 /* 3355 * case SCMD_SYNCHRONIZE_CACHE: 3356 * flush_cache(instance); 3357 * return_mfi_pkt(instance, cmd); 3358 * *cmd_done = 1; 3359 * 3360 * return (NULL); 3361 */ 3362 3363 case SCMD_READ: 3364 case SCMD_WRITE: 3365 case SCMD_READ_G1: 3366 case SCMD_WRITE_G1: 3367 if (acmd->islogical) { 3368 ldio = (struct megasas_io_frame *)cmd->frame; 3369 3370 /* 3371 * preare the Logical IO frame: 3372 * 2nd bit is zero for all read cmds 3373 */ 3374 ldio->cmd = (pkt->pkt_cdbp[0] & 0x02) ? 3375 MFI_CMD_OP_LD_WRITE : MFI_CMD_OP_LD_READ; 3376 ldio->cmd_status = 0x0; 3377 ldio->scsi_status = 0x0; 3378 ldio->target_id = acmd->device_id; 3379 ldio->timeout = 0; 3380 ldio->reserved_0 = 0; 3381 ldio->pad_0 = 0; 3382 ldio->flags = flags; 3383 3384 /* Initialize sense Information */ 3385 bzero(cmd->sense, SENSE_LENGTH); 3386 ldio->sense_len = SENSE_LENGTH; 3387 ldio->sense_buf_phys_addr_hi = 0; 3388 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr; 3389 3390 ldio->start_lba_hi = 0; 3391 ldio->access_byte = (acmd->cmd_cdblen != 6) ? 3392 pkt->pkt_cdbp[1] : 0; 3393 ldio->sge_count = acmd->cmd_cookiecnt; 3394 mfi_sgl = (struct megasas_sge32 *)&ldio->sgl; 3395 3396 context = ldio->context; 3397 3398 if (acmd->cmd_cdblen == CDB_GROUP0) { 3399 ldio->lba_count = host_to_le16( 3400 (uint16_t)(pkt->pkt_cdbp[4])); 3401 3402 ldio->start_lba_lo = host_to_le32( 3403 ((uint32_t)(pkt->pkt_cdbp[3])) | 3404 ((uint32_t)(pkt->pkt_cdbp[2]) << 8) | 3405 ((uint32_t)((pkt->pkt_cdbp[1]) & 0x1F) 3406 << 16)); 3407 } else if (acmd->cmd_cdblen == CDB_GROUP1) { 3408 ldio->lba_count = host_to_le16( 3409 ((uint16_t)(pkt->pkt_cdbp[8])) | 3410 ((uint16_t)(pkt->pkt_cdbp[7]) << 8)); 3411 3412 ldio->start_lba_lo = host_to_le32( 3413 ((uint32_t)(pkt->pkt_cdbp[5])) | 3414 ((uint32_t)(pkt->pkt_cdbp[4]) << 8) | 3415 ((uint32_t)(pkt->pkt_cdbp[3]) << 16) | 3416 ((uint32_t)(pkt->pkt_cdbp[2]) << 24)); 3417 } else if (acmd->cmd_cdblen == CDB_GROUP2) { 3418 ldio->lba_count = host_to_le16( 3419 ((uint16_t)(pkt->pkt_cdbp[9])) | 3420 ((uint16_t)(pkt->pkt_cdbp[8]) << 8) | 3421 ((uint16_t)(pkt->pkt_cdbp[7]) << 16) | 3422 ((uint16_t)(pkt->pkt_cdbp[6]) << 24)); 3423 3424 ldio->start_lba_lo = host_to_le32( 3425 ((uint32_t)(pkt->pkt_cdbp[5])) | 3426 ((uint32_t)(pkt->pkt_cdbp[4]) << 8) | 3427 ((uint32_t)(pkt->pkt_cdbp[3]) << 16) | 3428 ((uint32_t)(pkt->pkt_cdbp[2]) << 24)); 3429 } else if (acmd->cmd_cdblen == CDB_GROUP3) { 3430 ldio->lba_count = host_to_le16( 3431 ((uint16_t)(pkt->pkt_cdbp[13])) | 3432 ((uint16_t)(pkt->pkt_cdbp[12]) << 8) | 3433 ((uint16_t)(pkt->pkt_cdbp[11]) << 16) | 3434 ((uint16_t)(pkt->pkt_cdbp[10]) << 24)); 3435 3436 ldio->start_lba_lo = host_to_le32( 3437 ((uint32_t)(pkt->pkt_cdbp[9])) | 3438 ((uint32_t)(pkt->pkt_cdbp[8]) << 8) | 3439 ((uint32_t)(pkt->pkt_cdbp[7]) << 16) | 3440 ((uint32_t)(pkt->pkt_cdbp[6]) << 24)); 3441 3442 ldio->start_lba_lo = host_to_le32( 3443 ((uint32_t)(pkt->pkt_cdbp[5])) | 3444 ((uint32_t)(pkt->pkt_cdbp[4]) << 8) | 3445 ((uint32_t)(pkt->pkt_cdbp[3]) << 16) | 3446 ((uint32_t)(pkt->pkt_cdbp[2]) << 24)); 3447 } 3448 3449 break; 3450 } 3451 /* fall through For all non-rd/wr cmds */ 3452 default: 3453 pthru = (struct megasas_pthru_frame *)cmd->frame; 3454 3455 /* prepare the DCDB frame */ 3456 pthru->cmd = (acmd->islogical) ? 3457 MFI_CMD_OP_LD_SCSI : MFI_CMD_OP_PD_SCSI; 3458 pthru->cmd_status = 0x0; 3459 pthru->scsi_status = 0x0; 3460 pthru->target_id = acmd->device_id; 3461 pthru->lun = 0; 3462 pthru->cdb_len = acmd->cmd_cdblen; 3463 pthru->timeout = 0; 3464 pthru->flags = flags; 3465 pthru->data_xfer_len = acmd->cmd_dmacount; 3466 pthru->sge_count = acmd->cmd_cookiecnt; 3467 mfi_sgl = (struct megasas_sge32 *)&pthru->sgl; 3468 3469 bzero(cmd->sense, SENSE_LENGTH); 3470 pthru->sense_len = SENSE_LENGTH; 3471 pthru->sense_buf_phys_addr_hi = 0; 3472 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr; 3473 3474 context = pthru->context; 3475 3476 bcopy(pkt->pkt_cdbp, pthru->cdb, acmd->cmd_cdblen); 3477 3478 break; 3479 } 3480 #ifdef lint 3481 context = context; 3482 #endif 3483 /* bzero(mfi_sgl, sizeof (struct megasas_sge64) * MAX_SGL); */ 3484 3485 /* prepare the scatter-gather list for the firmware */ 3486 for (i = 0; i < acmd->cmd_cookiecnt; i++, mfi_sgl++) { 3487 mfi_sgl->phys_addr = acmd->cmd_dmacookies[i].dmac_laddress; 3488 mfi_sgl->length = acmd->cmd_dmacookies[i].dmac_size; 3489 } 3490 3491 sge_bytes = sizeof (struct megasas_sge32)*acmd->cmd_cookiecnt; 3492 3493 cmd->frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) + 3494 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) + 1; 3495 3496 if (cmd->frame_count >= 8) { 3497 cmd->frame_count = 8; 3498 } 3499 3500 return (cmd); 3501 } 3502 3503 /* 3504 * wait_for_outstanding - Wait for all outstanding cmds 3505 * @instance: Adapter soft state 3506 * 3507 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to 3508 * complete all its outstanding commands. Returns error if one or more IOs 3509 * are pending after this time period. 3510 */ 3511 static int 3512 wait_for_outstanding(struct megasas_instance *instance) 3513 { 3514 int i; 3515 uint32_t wait_time = 90; 3516 3517 for (i = 0; i < wait_time; i++) { 3518 if (!instance->fw_outstanding) { 3519 break; 3520 } 3521 3522 drv_usecwait(MILLISEC); /* wait for 1000 usecs */; 3523 } 3524 3525 if (instance->fw_outstanding) { 3526 return (1); 3527 } 3528 3529 ddi_fm_acc_err_clear(instance->regmap_handle, DDI_FME_VERSION); 3530 3531 return (0); 3532 } 3533 3534 /* 3535 * issue_mfi_pthru 3536 */ 3537 static int 3538 issue_mfi_pthru(struct megasas_instance *instance, struct megasas_ioctl *ioctl, 3539 struct megasas_cmd *cmd, int mode) 3540 { 3541 void *ubuf; 3542 uint32_t kphys_addr = 0; 3543 uint32_t xferlen = 0; 3544 uint_t model; 3545 3546 dma_obj_t pthru_dma_obj; 3547 struct megasas_pthru_frame *kpthru; 3548 struct megasas_pthru_frame *pthru; 3549 3550 pthru = &cmd->frame->pthru; 3551 kpthru = (struct megasas_pthru_frame *)&ioctl->frame[0]; 3552 3553 model = ddi_model_convert_from(mode & FMODELS); 3554 if (model == DDI_MODEL_ILP32) { 3555 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_pthru: DDI_MODEL_LP32")); 3556 3557 xferlen = kpthru->sgl.sge32[0].length; 3558 3559 /* SJ! - ubuf needs to be virtual address. */ 3560 ubuf = (void *)(ulong_t)kpthru->sgl.sge32[0].phys_addr; 3561 } else { 3562 #ifdef _ILP32 3563 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_pthru: DDI_MODEL_LP32")); 3564 xferlen = kpthru->sgl.sge32[0].length; 3565 /* SJ! - ubuf needs to be virtual address. */ 3566 ubuf = (void *)(ulong_t)kpthru->sgl.sge32[0].phys_addr; 3567 #else 3568 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_pthru: DDI_MODEL_LP64")); 3569 xferlen = kpthru->sgl.sge64[0].length; 3570 /* SJ! - ubuf needs to be virtual address. */ 3571 ubuf = (void *)(ulong_t)kpthru->sgl.sge64[0].phys_addr; 3572 #endif 3573 } 3574 3575 if (xferlen) { 3576 /* means IOCTL requires DMA */ 3577 /* allocate the data transfer buffer */ 3578 pthru_dma_obj.size = xferlen; 3579 pthru_dma_obj.dma_attr = megasas_generic_dma_attr; 3580 pthru_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 3581 pthru_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU; 3582 pthru_dma_obj.dma_attr.dma_attr_sgllen = 1; 3583 pthru_dma_obj.dma_attr.dma_attr_align = 1; 3584 3585 /* allocate kernel buffer for DMA */ 3586 if (mega_alloc_dma_obj(instance, &pthru_dma_obj) != 1) { 3587 con_log(CL_ANN, (CE_WARN, "issue_mfi_pthru: " 3588 "could not data transfer buffer alloc.")); 3589 return (DDI_FAILURE); 3590 } 3591 3592 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */ 3593 if (kpthru->flags & MFI_FRAME_DIR_WRITE) { 3594 if (ddi_copyin(ubuf, (void *)pthru_dma_obj.buffer, 3595 xferlen, mode)) { 3596 con_log(CL_ANN, (CE_WARN, "issue_mfi_pthru: " 3597 "copy from user space failed\n")); 3598 return (1); 3599 } 3600 } 3601 3602 kphys_addr = pthru_dma_obj.dma_cookie[0].dmac_address; 3603 } 3604 3605 pthru->cmd = kpthru->cmd; 3606 pthru->sense_len = kpthru->sense_len; 3607 pthru->cmd_status = kpthru->cmd_status; 3608 pthru->scsi_status = kpthru->scsi_status; 3609 pthru->target_id = kpthru->target_id; 3610 pthru->lun = kpthru->lun; 3611 pthru->cdb_len = kpthru->cdb_len; 3612 pthru->sge_count = kpthru->sge_count; 3613 pthru->timeout = kpthru->timeout; 3614 pthru->data_xfer_len = kpthru->data_xfer_len; 3615 3616 pthru->sense_buf_phys_addr_hi = 0; 3617 /* pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr; */ 3618 pthru->sense_buf_phys_addr_lo = 0; 3619 3620 bcopy((void *)kpthru->cdb, (void *)pthru->cdb, pthru->cdb_len); 3621 3622 pthru->flags = kpthru->flags & ~MFI_FRAME_SGL64; 3623 pthru->sgl.sge32[0].length = xferlen; 3624 pthru->sgl.sge32[0].phys_addr = kphys_addr; 3625 3626 cmd->sync_cmd = MEGASAS_TRUE; 3627 cmd->frame_count = 1; 3628 3629 if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) { 3630 con_log(CL_ANN, (CE_WARN, 3631 "issue_mfi_pthru: fw_ioctl failed\n")); 3632 } else { 3633 if (xferlen && (kpthru->flags & MFI_FRAME_DIR_READ)) { 3634 3635 if (ddi_copyout(pthru_dma_obj.buffer, ubuf, 3636 xferlen, mode)) { 3637 con_log(CL_ANN, (CE_WARN, "issue_mfi_pthru: " 3638 "copy to user space failed\n")); 3639 return (1); 3640 } 3641 } 3642 } 3643 3644 kpthru->cmd_status = pthru->cmd_status; 3645 kpthru->scsi_status = pthru->scsi_status; 3646 3647 con_log(CL_ANN, (CE_NOTE, "issue_mfi_pthru: cmd_status %x, " 3648 "scsi_status %x\n", pthru->cmd_status, pthru->scsi_status)); 3649 3650 if (xferlen) { 3651 /* free kernel buffer */ 3652 if (mega_free_dma_obj(instance, pthru_dma_obj) != DDI_SUCCESS) 3653 return (1); 3654 } 3655 3656 return (0); 3657 } 3658 3659 /* 3660 * issue_mfi_dcmd 3661 */ 3662 static int 3663 issue_mfi_dcmd(struct megasas_instance *instance, struct megasas_ioctl *ioctl, 3664 struct megasas_cmd *cmd, int mode) 3665 { 3666 void *ubuf; 3667 uint32_t kphys_addr = 0; 3668 uint32_t xferlen = 0; 3669 uint32_t model; 3670 dma_obj_t dcmd_dma_obj; 3671 struct megasas_dcmd_frame *kdcmd; 3672 struct megasas_dcmd_frame *dcmd; 3673 3674 dcmd = &cmd->frame->dcmd; 3675 kdcmd = (struct megasas_dcmd_frame *)&ioctl->frame[0]; 3676 3677 model = ddi_model_convert_from(mode & FMODELS); 3678 if (model == DDI_MODEL_ILP32) { 3679 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_dcmd: DDI_MODEL_ILP32")); 3680 3681 xferlen = kdcmd->sgl.sge32[0].length; 3682 3683 /* SJ! - ubuf needs to be virtual address. */ 3684 ubuf = (void *)(ulong_t)kdcmd->sgl.sge32[0].phys_addr; 3685 } 3686 else 3687 { 3688 #ifdef _ILP32 3689 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_dcmd: DDI_MODEL_ILP32")); 3690 xferlen = kdcmd->sgl.sge32[0].length; 3691 /* SJ! - ubuf needs to be virtual address. */ 3692 ubuf = (void *)(ulong_t)kdcmd->sgl.sge32[0].phys_addr; 3693 #else 3694 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_dcmd: DDI_MODEL_LP64")); 3695 xferlen = kdcmd->sgl.sge64[0].length; 3696 /* SJ! - ubuf needs to be virtual address. */ 3697 ubuf = (void *)(ulong_t)dcmd->sgl.sge64[0].phys_addr; 3698 #endif 3699 } 3700 if (xferlen) { 3701 /* means IOCTL requires DMA */ 3702 /* allocate the data transfer buffer */ 3703 dcmd_dma_obj.size = xferlen; 3704 dcmd_dma_obj.dma_attr = megasas_generic_dma_attr; 3705 dcmd_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 3706 dcmd_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU; 3707 dcmd_dma_obj.dma_attr.dma_attr_sgllen = 1; 3708 dcmd_dma_obj.dma_attr.dma_attr_align = 1; 3709 3710 /* allocate kernel buffer for DMA */ 3711 if (mega_alloc_dma_obj(instance, &dcmd_dma_obj) != 1) { 3712 con_log(CL_ANN, (CE_WARN, "issue_mfi_dcmd: " 3713 "could not data transfer buffer alloc.")); 3714 return (DDI_FAILURE); 3715 } 3716 3717 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */ 3718 if (kdcmd->flags & MFI_FRAME_DIR_WRITE) { 3719 if (ddi_copyin(ubuf, (void *)dcmd_dma_obj.buffer, 3720 xferlen, mode)) { 3721 con_log(CL_ANN, (CE_WARN, "issue_mfi_dcmd: " 3722 "copy from user space failed\n")); 3723 return (1); 3724 } 3725 } 3726 3727 kphys_addr = dcmd_dma_obj.dma_cookie[0].dmac_address; 3728 } 3729 3730 dcmd->cmd = kdcmd->cmd; 3731 dcmd->cmd_status = kdcmd->cmd_status; 3732 dcmd->sge_count = kdcmd->sge_count; 3733 dcmd->timeout = kdcmd->timeout; 3734 dcmd->data_xfer_len = kdcmd->data_xfer_len; 3735 dcmd->opcode = kdcmd->opcode; 3736 3737 bcopy((void *)kdcmd->mbox.b, (void *)dcmd->mbox.b, DCMD_MBOX_SZ); 3738 3739 dcmd->flags = kdcmd->flags & ~MFI_FRAME_SGL64; 3740 dcmd->sgl.sge32[0].length = xferlen; 3741 dcmd->sgl.sge32[0].phys_addr = kphys_addr; 3742 3743 cmd->sync_cmd = MEGASAS_TRUE; 3744 cmd->frame_count = 1; 3745 3746 if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) { 3747 con_log(CL_ANN, (CE_WARN, "issue_mfi_dcmd: fw_ioctl failed\n")); 3748 } else { 3749 if (xferlen && (kdcmd->flags & MFI_FRAME_DIR_READ)) { 3750 3751 if (ddi_copyout(dcmd_dma_obj.buffer, ubuf, 3752 xferlen, mode)) { 3753 con_log(CL_ANN, (CE_WARN, "issue_mfi_dcmd: " 3754 "copy to user space failed\n")); 3755 return (1); 3756 } 3757 } 3758 } 3759 3760 kdcmd->cmd_status = dcmd->cmd_status; 3761 3762 if (xferlen) { 3763 /* free kernel buffer */ 3764 if (mega_free_dma_obj(instance, dcmd_dma_obj) != DDI_SUCCESS) 3765 return (1); 3766 } 3767 3768 return (0); 3769 } 3770 3771 /* 3772 * issue_mfi_smp 3773 */ 3774 static int 3775 issue_mfi_smp(struct megasas_instance *instance, struct megasas_ioctl *ioctl, 3776 struct megasas_cmd *cmd, int mode) 3777 { 3778 void *request_ubuf; 3779 void *response_ubuf; 3780 uint32_t request_xferlen = 0; 3781 uint32_t response_xferlen = 0; 3782 uint_t model; 3783 dma_obj_t request_dma_obj; 3784 dma_obj_t response_dma_obj; 3785 struct megasas_smp_frame *ksmp; 3786 struct megasas_smp_frame *smp; 3787 struct megasas_sge32 *sge32; 3788 #ifndef _ILP32 3789 struct megasas_sge64 *sge64; 3790 #endif 3791 3792 smp = &cmd->frame->smp; 3793 ksmp = (struct megasas_smp_frame *)&ioctl->frame[0]; 3794 3795 model = ddi_model_convert_from(mode & FMODELS); 3796 if (model == DDI_MODEL_ILP32) { 3797 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: DDI_MODEL_ILP32")); 3798 3799 sge32 = &ksmp->sgl[0].sge32[0]; 3800 response_xferlen = sge32[0].length; 3801 request_xferlen = sge32[1].length; 3802 con_log(CL_ANN, (CE_NOTE, "issue_mfi_smp: " 3803 "response_xferlen = %x, request_xferlen = %x", 3804 response_xferlen, request_xferlen)); 3805 3806 /* SJ! - ubuf needs to be virtual address. */ 3807 3808 response_ubuf = (void *)(ulong_t)sge32[0].phys_addr; 3809 request_ubuf = (void *)(ulong_t)sge32[1].phys_addr; 3810 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: " 3811 "response_ubuf = %p, request_ubuf = %p", 3812 response_ubuf, request_ubuf)); 3813 } else { 3814 #ifdef _ILP32 3815 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: DDI_MODEL_ILP32")); 3816 3817 sge32 = &ksmp->sgl[0].sge32[0]; 3818 response_xferlen = sge32[0].length; 3819 request_xferlen = sge32[1].length; 3820 con_log(CL_ANN, (CE_NOTE, "issue_mfi_smp: " 3821 "response_xferlen = %x, request_xferlen = %x", 3822 response_xferlen, request_xferlen)); 3823 3824 /* SJ! - ubuf needs to be virtual address. */ 3825 3826 response_ubuf = (void *)(ulong_t)sge32[0].phys_addr; 3827 request_ubuf = (void *)(ulong_t)sge32[1].phys_addr; 3828 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: " 3829 "response_ubuf = %p, request_ubuf = %p", 3830 response_ubuf, request_ubuf)); 3831 #else 3832 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: DDI_MODEL_LP64")); 3833 3834 sge64 = &ksmp->sgl[0].sge64[0]; 3835 response_xferlen = sge64[0].length; 3836 request_xferlen = sge64[1].length; 3837 3838 /* SJ! - ubuf needs to be virtual address. */ 3839 response_ubuf = (void *)(ulong_t)sge64[0].phys_addr; 3840 request_ubuf = (void *)(ulong_t)sge64[1].phys_addr; 3841 #endif 3842 } 3843 if (request_xferlen) { 3844 /* means IOCTL requires DMA */ 3845 /* allocate the data transfer buffer */ 3846 request_dma_obj.size = request_xferlen; 3847 request_dma_obj.dma_attr = megasas_generic_dma_attr; 3848 request_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 3849 request_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU; 3850 request_dma_obj.dma_attr.dma_attr_sgllen = 1; 3851 request_dma_obj.dma_attr.dma_attr_align = 1; 3852 3853 /* allocate kernel buffer for DMA */ 3854 if (mega_alloc_dma_obj(instance, &request_dma_obj) != 1) { 3855 con_log(CL_ANN, (CE_WARN, "issue_mfi_smp: " 3856 "could not data transfer buffer alloc.")); 3857 return (DDI_FAILURE); 3858 } 3859 3860 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */ 3861 if (ddi_copyin(request_ubuf, (void *) request_dma_obj.buffer, 3862 request_xferlen, mode)) { 3863 con_log(CL_ANN, (CE_WARN, "issue_mfi_smp: " 3864 "copy from user space failed\n")); 3865 return (1); 3866 } 3867 } 3868 3869 if (response_xferlen) { 3870 /* means IOCTL requires DMA */ 3871 /* allocate the data transfer buffer */ 3872 response_dma_obj.size = response_xferlen; 3873 response_dma_obj.dma_attr = megasas_generic_dma_attr; 3874 response_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 3875 response_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU; 3876 response_dma_obj.dma_attr.dma_attr_sgllen = 1; 3877 response_dma_obj.dma_attr.dma_attr_align = 1; 3878 3879 /* allocate kernel buffer for DMA */ 3880 if (mega_alloc_dma_obj(instance, &response_dma_obj) != 1) { 3881 con_log(CL_ANN, (CE_WARN, "issue_mfi_smp: " 3882 "could not data transfer buffer alloc.")); 3883 return (DDI_FAILURE); 3884 } 3885 3886 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */ 3887 if (ddi_copyin(response_ubuf, (void *) response_dma_obj.buffer, 3888 response_xferlen, mode)) { 3889 con_log(CL_ANN, (CE_WARN, "issue_mfi_smp: " 3890 "copy from user space failed\n")); 3891 return (1); 3892 } 3893 } 3894 3895 smp->cmd = ksmp->cmd; 3896 smp->cmd_status = ksmp->cmd_status; 3897 smp->connection_status = ksmp->connection_status; 3898 smp->sge_count = ksmp->sge_count; 3899 /* smp->context = ksmp->context; */ 3900 smp->timeout = ksmp->timeout; 3901 smp->data_xfer_len = ksmp->data_xfer_len; 3902 3903 bcopy((void *)&ksmp->sas_addr, (void *)&smp->sas_addr, 3904 sizeof (uint64_t)); 3905 3906 smp->flags = ksmp->flags & ~MFI_FRAME_SGL64; 3907 3908 model = ddi_model_convert_from(mode & FMODELS); 3909 if (model == DDI_MODEL_ILP32) { 3910 con_log(CL_ANN1, (CE_NOTE, 3911 "handle_drv_ioctl: DDI_MODEL_ILP32")); 3912 3913 sge32 = &smp->sgl[0].sge32[0]; 3914 sge32[0].length = response_xferlen; 3915 sge32[0].phys_addr = 3916 response_dma_obj.dma_cookie[0].dmac_address; 3917 sge32[1].length = request_xferlen; 3918 sge32[1].phys_addr = 3919 request_dma_obj.dma_cookie[0].dmac_address; 3920 } else { 3921 #ifdef _ILP32 3922 con_log(CL_ANN1, (CE_NOTE, 3923 "handle_drv_ioctl: DDI_MODEL_ILP32")); 3924 sge32 = &smp->sgl[0].sge32[0]; 3925 sge32[0].length = response_xferlen; 3926 sge32[0].phys_addr = 3927 response_dma_obj.dma_cookie[0].dmac_address; 3928 sge32[1].length = request_xferlen; 3929 sge32[1].phys_addr = 3930 request_dma_obj.dma_cookie[0].dmac_address; 3931 #else 3932 con_log(CL_ANN1, (CE_NOTE, 3933 "issue_mfi_smp: DDI_MODEL_LP64")); 3934 sge64 = &smp->sgl[0].sge64[0]; 3935 sge64[0].length = response_xferlen; 3936 sge64[0].phys_addr = 3937 response_dma_obj.dma_cookie[0].dmac_address; 3938 sge64[1].length = request_xferlen; 3939 sge64[1].phys_addr = 3940 request_dma_obj.dma_cookie[0].dmac_address; 3941 #endif 3942 } 3943 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: " 3944 "smp->response_xferlen = %d, smp->request_xferlen = %d " 3945 "smp->data_xfer_len = %d", sge32[0].length, sge32[1].length, 3946 smp->data_xfer_len)); 3947 3948 cmd->sync_cmd = MEGASAS_TRUE; 3949 cmd->frame_count = 1; 3950 3951 if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) { 3952 con_log(CL_ANN, (CE_WARN, 3953 "issue_mfi_smp: fw_ioctl failed\n")); 3954 } else { 3955 con_log(CL_ANN1, (CE_NOTE, 3956 "issue_mfi_smp: copy to user space\n")); 3957 3958 if (request_xferlen) { 3959 if (ddi_copyout(request_dma_obj.buffer, request_ubuf, 3960 request_xferlen, mode)) { 3961 con_log(CL_ANN, (CE_WARN, "issue_mfi_smp: " 3962 "copy to user space failed\n")); 3963 return (1); 3964 } 3965 } 3966 3967 if (response_xferlen) { 3968 if (ddi_copyout(response_dma_obj.buffer, response_ubuf, 3969 response_xferlen, mode)) { 3970 con_log(CL_ANN, (CE_WARN, "issue_mfi_smp: " 3971 "copy to user space failed\n")); 3972 return (1); 3973 } 3974 } 3975 } 3976 3977 ksmp->cmd_status = smp->cmd_status; 3978 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: smp->cmd_status = %d", 3979 smp->cmd_status)); 3980 3981 3982 if (request_xferlen) { 3983 /* free kernel buffer */ 3984 if (mega_free_dma_obj(instance, request_dma_obj) != DDI_SUCCESS) 3985 return (1); 3986 } 3987 3988 if (response_xferlen) { 3989 /* free kernel buffer */ 3990 if (mega_free_dma_obj(instance, response_dma_obj) != 3991 DDI_SUCCESS) 3992 return (1); 3993 } 3994 3995 return (0); 3996 } 3997 3998 /* 3999 * issue_mfi_stp 4000 */ 4001 static int 4002 issue_mfi_stp(struct megasas_instance *instance, struct megasas_ioctl *ioctl, 4003 struct megasas_cmd *cmd, int mode) 4004 { 4005 void *fis_ubuf; 4006 void *data_ubuf; 4007 uint32_t fis_xferlen = 0; 4008 uint32_t data_xferlen = 0; 4009 uint_t model; 4010 dma_obj_t fis_dma_obj; 4011 dma_obj_t data_dma_obj; 4012 struct megasas_stp_frame *kstp; 4013 struct megasas_stp_frame *stp; 4014 4015 stp = &cmd->frame->stp; 4016 kstp = (struct megasas_stp_frame *)&ioctl->frame[0]; 4017 4018 model = ddi_model_convert_from(mode & FMODELS); 4019 if (model == DDI_MODEL_ILP32) { 4020 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_stp: DDI_MODEL_ILP32")); 4021 4022 fis_xferlen = kstp->sgl.sge32[0].length; 4023 data_xferlen = kstp->sgl.sge32[1].length; 4024 4025 /* SJ! - ubuf needs to be virtual address. */ 4026 fis_ubuf = (void *)(ulong_t)kstp->sgl.sge32[0].phys_addr; 4027 data_ubuf = (void *)(ulong_t)kstp->sgl.sge32[1].phys_addr; 4028 } 4029 else 4030 { 4031 #ifdef _ILP32 4032 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_stp: DDI_MODEL_ILP32")); 4033 4034 fis_xferlen = kstp->sgl.sge32[0].length; 4035 data_xferlen = kstp->sgl.sge32[1].length; 4036 4037 /* SJ! - ubuf needs to be virtual address. */ 4038 fis_ubuf = (void *)(ulong_t)kstp->sgl.sge32[0].phys_addr; 4039 data_ubuf = (void *)(ulong_t)kstp->sgl.sge32[1].phys_addr; 4040 #else 4041 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_stp: DDI_MODEL_LP64")); 4042 4043 fis_xferlen = kstp->sgl.sge64[0].length; 4044 data_xferlen = kstp->sgl.sge64[1].length; 4045 4046 /* SJ! - ubuf needs to be virtual address. */ 4047 fis_ubuf = (void *)(ulong_t)kstp->sgl.sge64[0].phys_addr; 4048 data_ubuf = (void *)(ulong_t)kstp->sgl.sge64[1].phys_addr; 4049 #endif 4050 } 4051 4052 4053 if (fis_xferlen) { 4054 con_log(CL_ANN, (CE_NOTE, "issue_mfi_stp: " 4055 "fis_ubuf = %p fis_xferlen = %x", fis_ubuf, fis_xferlen)); 4056 4057 /* means IOCTL requires DMA */ 4058 /* allocate the data transfer buffer */ 4059 fis_dma_obj.size = fis_xferlen; 4060 fis_dma_obj.dma_attr = megasas_generic_dma_attr; 4061 fis_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 4062 fis_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU; 4063 fis_dma_obj.dma_attr.dma_attr_sgllen = 1; 4064 fis_dma_obj.dma_attr.dma_attr_align = 1; 4065 4066 /* allocate kernel buffer for DMA */ 4067 if (mega_alloc_dma_obj(instance, &fis_dma_obj) != 1) { 4068 con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: " 4069 "could not data transfer buffer alloc.")); 4070 return (DDI_FAILURE); 4071 } 4072 4073 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */ 4074 if (ddi_copyin(fis_ubuf, (void *)fis_dma_obj.buffer, 4075 fis_xferlen, mode)) { 4076 con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: " 4077 "copy from user space failed\n")); 4078 return (1); 4079 } 4080 } 4081 4082 if (data_xferlen) { 4083 con_log(CL_ANN, (CE_NOTE, "issue_mfi_stp: data_ubuf = %p " 4084 "data_xferlen = %x", data_ubuf, data_xferlen)); 4085 4086 /* means IOCTL requires DMA */ 4087 /* allocate the data transfer buffer */ 4088 data_dma_obj.size = data_xferlen; 4089 data_dma_obj.dma_attr = megasas_generic_dma_attr; 4090 data_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 4091 data_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU; 4092 data_dma_obj.dma_attr.dma_attr_sgllen = 1; 4093 data_dma_obj.dma_attr.dma_attr_align = 1; 4094 4095 /* allocate kernel buffer for DMA */ 4096 if (mega_alloc_dma_obj(instance, &data_dma_obj) != 1) { 4097 con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: " 4098 "could not data transfer buffer alloc.")); 4099 return (DDI_FAILURE); 4100 } 4101 4102 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */ 4103 if (ddi_copyin(data_ubuf, (void *) data_dma_obj.buffer, 4104 data_xferlen, mode)) { 4105 con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: " 4106 "copy from user space failed\n")); 4107 return (1); 4108 } 4109 } 4110 4111 stp->cmd = kstp->cmd; 4112 stp->cmd_status = kstp->cmd_status; 4113 stp->connection_status = kstp->connection_status; 4114 stp->target_id = kstp->target_id; 4115 stp->sge_count = kstp->sge_count; 4116 /* stp->context = kstp->context; */ 4117 stp->timeout = kstp->timeout; 4118 stp->data_xfer_len = kstp->data_xfer_len; 4119 4120 bcopy((void *)kstp->fis, (void *)stp->fis, 10); 4121 4122 stp->flags = kstp->flags & ~MFI_FRAME_SGL64; 4123 stp->stp_flags = kstp->stp_flags; 4124 stp->sgl.sge32[0].length = fis_xferlen; 4125 stp->sgl.sge32[0].phys_addr = fis_dma_obj.dma_cookie[0].dmac_address; 4126 stp->sgl.sge32[1].length = data_xferlen; 4127 stp->sgl.sge32[1].phys_addr = data_dma_obj.dma_cookie[0].dmac_address; 4128 4129 cmd->sync_cmd = MEGASAS_TRUE; 4130 cmd->frame_count = 1; 4131 4132 if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) { 4133 con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: fw_ioctl failed\n")); 4134 } else { 4135 4136 if (fis_xferlen) { 4137 if (ddi_copyout(fis_dma_obj.buffer, fis_ubuf, 4138 fis_xferlen, mode)) { 4139 con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: " 4140 "copy to user space failed\n")); 4141 return (1); 4142 } 4143 } 4144 4145 if (data_xferlen) { 4146 if (ddi_copyout(data_dma_obj.buffer, data_ubuf, 4147 data_xferlen, mode)) { 4148 con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: " 4149 "copy to user space failed\n")); 4150 return (1); 4151 } 4152 } 4153 } 4154 4155 kstp->cmd_status = stp->cmd_status; 4156 4157 if (fis_xferlen) { 4158 /* free kernel buffer */ 4159 if (mega_free_dma_obj(instance, fis_dma_obj) != DDI_SUCCESS) 4160 return (1); 4161 } 4162 4163 if (data_xferlen) { 4164 /* free kernel buffer */ 4165 if (mega_free_dma_obj(instance, data_dma_obj) != DDI_SUCCESS) 4166 return (1); 4167 } 4168 4169 return (0); 4170 } 4171 4172 /* 4173 * fill_up_drv_ver 4174 */ 4175 static void 4176 fill_up_drv_ver(struct megasas_drv_ver *dv) 4177 { 4178 (void) memset(dv, 0, sizeof (struct megasas_drv_ver)); 4179 4180 (void) memcpy(dv->signature, "$LSI LOGIC$", strlen("$LSI LOGIC$")); 4181 (void) memcpy(dv->os_name, "Solaris", strlen("Solaris")); 4182 (void) memcpy(dv->drv_name, "megaraid_sas", strlen("megaraid_sas")); 4183 (void) memcpy(dv->drv_ver, MEGASAS_VERSION, strlen(MEGASAS_VERSION)); 4184 (void) memcpy(dv->drv_rel_date, MEGASAS_RELDATE, 4185 strlen(MEGASAS_RELDATE)); 4186 } 4187 4188 /* 4189 * handle_drv_ioctl 4190 */ 4191 static int 4192 handle_drv_ioctl(struct megasas_instance *instance, struct megasas_ioctl *ioctl, 4193 int mode) 4194 { 4195 int i; 4196 int rval = 0; 4197 int *props = NULL; 4198 void *ubuf; 4199 4200 uint8_t *pci_conf_buf; 4201 uint32_t xferlen; 4202 uint32_t num_props; 4203 uint_t model; 4204 struct megasas_dcmd_frame *kdcmd; 4205 struct megasas_drv_ver dv; 4206 struct megasas_pci_information pi; 4207 4208 kdcmd = (struct megasas_dcmd_frame *)&ioctl->frame[0]; 4209 4210 model = ddi_model_convert_from(mode & FMODELS); 4211 if (model == DDI_MODEL_ILP32) { 4212 con_log(CL_ANN1, (CE_NOTE, 4213 "handle_drv_ioctl: DDI_MODEL_ILP32")); 4214 4215 xferlen = kdcmd->sgl.sge32[0].length; 4216 4217 /* SJ! - ubuf needs to be virtual address. */ 4218 ubuf = (void *)(ulong_t)kdcmd->sgl.sge32[0].phys_addr; 4219 } else { 4220 #ifdef _ILP32 4221 con_log(CL_ANN1, (CE_NOTE, 4222 "handle_drv_ioctl: DDI_MODEL_ILP32")); 4223 xferlen = kdcmd->sgl.sge32[0].length; 4224 /* SJ! - ubuf needs to be virtual address. */ 4225 ubuf = (void *)(ulong_t)kdcmd->sgl.sge32[0].phys_addr; 4226 #else 4227 con_log(CL_ANN1, (CE_NOTE, 4228 "handle_drv_ioctl: DDI_MODEL_LP64")); 4229 xferlen = kdcmd->sgl.sge64[0].length; 4230 /* SJ! - ubuf needs to be virtual address. */ 4231 ubuf = (void *)(ulong_t)kdcmd->sgl.sge64[0].phys_addr; 4232 #endif 4233 } 4234 con_log(CL_ANN1, (CE_NOTE, "handle_drv_ioctl: " 4235 "dataBuf=%p size=%d bytes", ubuf, xferlen)); 4236 4237 switch (kdcmd->opcode) { 4238 case MR_DRIVER_IOCTL_DRIVER_VERSION: 4239 con_log(CL_ANN1, (CE_NOTE, "handle_drv_ioctl: " 4240 "MR_DRIVER_IOCTL_DRIVER_VERSION")); 4241 4242 fill_up_drv_ver(&dv); 4243 4244 if (ddi_copyout(&dv, ubuf, xferlen, mode)) { 4245 con_log(CL_ANN, (CE_WARN, "handle_drv_ioctl: " 4246 "MR_DRIVER_IOCTL_DRIVER_VERSION : " 4247 "copy to user space failed\n")); 4248 kdcmd->cmd_status = 1; 4249 rval = 1; 4250 } else { 4251 kdcmd->cmd_status = 0; 4252 } 4253 break; 4254 case MR_DRIVER_IOCTL_PCI_INFORMATION: 4255 con_log(CL_ANN1, (CE_NOTE, "handle_drv_ioctl: " 4256 "MR_DRIVER_IOCTL_PCI_INFORMAITON")); 4257 4258 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, instance->dip, 4259 0, "reg", &props, &num_props)) { 4260 con_log(CL_ANN, (CE_WARN, "handle_drv_ioctl: " 4261 "MR_DRIVER_IOCTL_PCI_INFORMATION : " 4262 "ddi_prop_look_int_array failed\n")); 4263 rval = 1; 4264 } else { 4265 4266 pi.busNumber = (props[0] >> 16) & 0xFF; 4267 pi.deviceNumber = (props[0] >> 11) & 0x1f; 4268 pi.functionNumber = (props[0] >> 8) & 0x7; 4269 ddi_prop_free((void *)props); 4270 } 4271 4272 pci_conf_buf = (uint8_t *)&pi.pciHeaderInfo; 4273 4274 for (i = 0; i < (sizeof (struct megasas_pci_information) - 4275 offsetof(struct megasas_pci_information, pciHeaderInfo)); 4276 i++) { 4277 pci_conf_buf[i] = 4278 pci_config_get8(instance->pci_handle, i); 4279 } 4280 4281 if (ddi_copyout(&pi, ubuf, xferlen, mode)) { 4282 con_log(CL_ANN, (CE_WARN, "handle_drv_ioctl: " 4283 "MR_DRIVER_IOCTL_PCI_INFORMATION : " 4284 "copy to user space failed\n")); 4285 kdcmd->cmd_status = 1; 4286 rval = 1; 4287 } else { 4288 kdcmd->cmd_status = 0; 4289 } 4290 break; 4291 default: 4292 con_log(CL_ANN, (CE_WARN, "handle_drv_ioctl: " 4293 "invalid driver specific IOCTL opcode = 0x%x", 4294 kdcmd->opcode)); 4295 kdcmd->cmd_status = 1; 4296 rval = 1; 4297 break; 4298 } 4299 4300 return (rval); 4301 } 4302 4303 /* 4304 * handle_mfi_ioctl 4305 */ 4306 static int 4307 handle_mfi_ioctl(struct megasas_instance *instance, struct megasas_ioctl *ioctl, 4308 int mode) 4309 { 4310 int rval = 0; 4311 4312 struct megasas_header *hdr; 4313 struct megasas_cmd *cmd; 4314 4315 cmd = get_mfi_pkt(instance); 4316 4317 if (!cmd) { 4318 con_log(CL_ANN, (CE_WARN, "megasas: " 4319 "failed to get a cmd packet\n")); 4320 return (1); 4321 } 4322 4323 hdr = (struct megasas_header *)&ioctl->frame[0]; 4324 4325 switch (hdr->cmd) { 4326 case MFI_CMD_OP_DCMD: 4327 rval = issue_mfi_dcmd(instance, ioctl, cmd, mode); 4328 break; 4329 case MFI_CMD_OP_SMP: 4330 rval = issue_mfi_smp(instance, ioctl, cmd, mode); 4331 break; 4332 case MFI_CMD_OP_STP: 4333 rval = issue_mfi_stp(instance, ioctl, cmd, mode); 4334 break; 4335 case MFI_CMD_OP_LD_SCSI: 4336 case MFI_CMD_OP_PD_SCSI: 4337 rval = issue_mfi_pthru(instance, ioctl, cmd, mode); 4338 break; 4339 default: 4340 con_log(CL_ANN, (CE_WARN, "handle_mfi_ioctl: " 4341 "invalid mfi ioctl hdr->cmd = %d\n", hdr->cmd)); 4342 rval = 1; 4343 break; 4344 } 4345 4346 4347 return_mfi_pkt(instance, cmd); 4348 if (megasas_common_check(instance, cmd) != DDI_SUCCESS) 4349 rval = 1; 4350 return (rval); 4351 } 4352 4353 /* 4354 * AEN 4355 */ 4356 static int 4357 handle_mfi_aen(struct megasas_instance *instance, struct megasas_aen *aen) 4358 { 4359 int rval = 0; 4360 4361 rval = register_mfi_aen(instance, instance->aen_seq_num, 4362 aen->class_locale_word); 4363 4364 aen->cmd_status = (uint8_t)rval; 4365 4366 return (rval); 4367 } 4368 4369 static int 4370 register_mfi_aen(struct megasas_instance *instance, uint32_t seq_num, 4371 uint32_t class_locale_word) 4372 { 4373 int ret_val; 4374 4375 struct megasas_cmd *cmd; 4376 struct megasas_dcmd_frame *dcmd; 4377 union megasas_evt_class_locale curr_aen; 4378 union megasas_evt_class_locale prev_aen; 4379 4380 /* 4381 * If there an AEN pending already (aen_cmd), check if the 4382 * class_locale of that pending AEN is inclusive of the new 4383 * AEN request we currently have. If it is, then we don't have 4384 * to do anything. In other words, whichever events the current 4385 * AEN request is subscribing to, have already been subscribed 4386 * to. 4387 * 4388 * If the old_cmd is _not_ inclusive, then we have to abort 4389 * that command, form a class_locale that is superset of both 4390 * old and current and re-issue to the FW 4391 */ 4392 4393 curr_aen.word = class_locale_word; 4394 4395 if (instance->aen_cmd) { 4396 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1]; 4397 4398 /* 4399 * A class whose enum value is smaller is inclusive of all 4400 * higher values. If a PROGRESS (= -1) was previously 4401 * registered, then a new registration requests for higher 4402 * classes need not be sent to FW. They are automatically 4403 * included. 4404 * 4405 * Locale numbers don't have such hierarchy. They are bitmap 4406 * values 4407 */ 4408 if ((prev_aen.members.class <= curr_aen.members.class) && 4409 !((prev_aen.members.locale & curr_aen.members.locale) ^ 4410 curr_aen.members.locale)) { 4411 /* 4412 * Previously issued event registration includes 4413 * current request. Nothing to do. 4414 */ 4415 4416 return (0); 4417 } else { 4418 curr_aen.members.locale |= prev_aen.members.locale; 4419 4420 if (prev_aen.members.class < curr_aen.members.class) 4421 curr_aen.members.class = prev_aen.members.class; 4422 4423 ret_val = abort_aen_cmd(instance, instance->aen_cmd); 4424 4425 if (ret_val) { 4426 con_log(CL_ANN, (CE_WARN, "register_mfi_aen: " 4427 "failed to abort prevous AEN command\n")); 4428 4429 return (ret_val); 4430 } 4431 } 4432 } else { 4433 curr_aen.word = class_locale_word; 4434 } 4435 4436 cmd = get_mfi_pkt(instance); 4437 4438 if (!cmd) 4439 return (-ENOMEM); 4440 4441 dcmd = &cmd->frame->dcmd; 4442 4443 /* for(i = 0; i < DCMD_MBOX_SZ; i++) dcmd->mbox.b[i] = 0; */ 4444 (void) memset(dcmd->mbox.b, 0, DCMD_MBOX_SZ); 4445 4446 (void) memset(instance->mfi_evt_detail_obj.buffer, 0, 4447 sizeof (struct megasas_evt_detail)); 4448 4449 /* Prepare DCMD for aen registration */ 4450 dcmd->cmd = MFI_CMD_OP_DCMD; 4451 dcmd->cmd_status = 0x0; 4452 dcmd->sge_count = 1; 4453 dcmd->flags = MFI_FRAME_DIR_READ; 4454 dcmd->timeout = 0; 4455 dcmd->data_xfer_len = sizeof (struct megasas_evt_detail); 4456 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT; 4457 dcmd->mbox.w[0] = seq_num; 4458 dcmd->mbox.w[1] = curr_aen.word; 4459 dcmd->sgl.sge32[0].phys_addr = 4460 instance->mfi_evt_detail_obj.dma_cookie[0].dmac_address; 4461 dcmd->sgl.sge32[0].length = sizeof (struct megasas_evt_detail); 4462 4463 instance->aen_seq_num = seq_num; 4464 4465 /* 4466 * Store reference to the cmd used to register for AEN. When an 4467 * application wants us to register for AEN, we have to abort this 4468 * cmd and re-register with a new EVENT LOCALE supplied by that app 4469 */ 4470 instance->aen_cmd = cmd; 4471 4472 cmd->frame_count = 1; 4473 4474 /* Issue the aen registration frame */ 4475 /* atomic_add_16 (&instance->fw_outstanding, 1); */ 4476 instance->func_ptr->issue_cmd(cmd, instance); 4477 4478 return (0); 4479 } 4480 4481 static void 4482 display_scsi_inquiry(caddr_t scsi_inq) 4483 { 4484 #define MAX_SCSI_DEVICE_CODE 14 4485 int i; 4486 char inquiry_buf[256] = {0}; 4487 int len; 4488 const char *const scsi_device_types[] = { 4489 "Direct-Access ", 4490 "Sequential-Access", 4491 "Printer ", 4492 "Processor ", 4493 "WORM ", 4494 "CD-ROM ", 4495 "Scanner ", 4496 "Optical Device ", 4497 "Medium Changer ", 4498 "Communications ", 4499 "Unknown ", 4500 "Unknown ", 4501 "Unknown ", 4502 "Enclosure ", 4503 }; 4504 4505 len = 0; 4506 4507 len += snprintf(inquiry_buf + len, 265 - len, " Vendor: "); 4508 for (i = 8; i < 16; i++) { 4509 len += snprintf(inquiry_buf + len, 265 - len, "%c", 4510 scsi_inq[i]); 4511 } 4512 4513 len += snprintf(inquiry_buf + len, 265 - len, " Model: "); 4514 4515 for (i = 16; i < 32; i++) { 4516 len += snprintf(inquiry_buf + len, 265 - len, "%c", 4517 scsi_inq[i]); 4518 } 4519 4520 len += snprintf(inquiry_buf + len, 265 - len, " Rev: "); 4521 4522 for (i = 32; i < 36; i++) { 4523 len += snprintf(inquiry_buf + len, 265 - len, "%c", 4524 scsi_inq[i]); 4525 } 4526 4527 len += snprintf(inquiry_buf + len, 265 - len, "\n"); 4528 4529 4530 i = scsi_inq[0] & 0x1f; 4531 4532 4533 len += snprintf(inquiry_buf + len, 265 - len, " Type: %s ", 4534 i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] : 4535 "Unknown "); 4536 4537 4538 len += snprintf(inquiry_buf + len, 265 - len, 4539 " ANSI SCSI revision: %02x", scsi_inq[2] & 0x07); 4540 4541 if ((scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1) { 4542 len += snprintf(inquiry_buf + len, 265 - len, " CCS\n"); 4543 } else { 4544 len += snprintf(inquiry_buf + len, 265 - len, "\n"); 4545 } 4546 4547 con_log(CL_ANN1, (CE_CONT, inquiry_buf)); 4548 } 4549 4550 static int 4551 read_fw_status_reg_xscale(struct megasas_instance *instance) 4552 { 4553 return ((int)RD_OB_MSG_0(instance)); 4554 } 4555 4556 static int 4557 read_fw_status_reg_ppc(struct megasas_instance *instance) 4558 { 4559 return ((int)RD_OB_SCRATCH_PAD_0(instance)); 4560 } 4561 4562 static void 4563 issue_cmd_xscale(struct megasas_cmd *cmd, struct megasas_instance *instance) 4564 { 4565 atomic_add_16(&instance->fw_outstanding, 1); 4566 4567 /* Issue the command to the FW */ 4568 WR_IB_QPORT((host_to_le32(cmd->frame_phys_addr) >> 3) | 4569 (cmd->frame_count - 1), instance); 4570 } 4571 4572 static void 4573 issue_cmd_ppc(struct megasas_cmd *cmd, struct megasas_instance *instance) 4574 { 4575 atomic_add_16(&instance->fw_outstanding, 1); 4576 4577 /* Issue the command to the FW */ 4578 WR_IB_QPORT((host_to_le32(cmd->frame_phys_addr)) | 4579 (((cmd->frame_count - 1) << 1) | 1), instance); 4580 } 4581 4582 /* 4583 * issue_cmd_in_sync_mode 4584 */ 4585 static int 4586 issue_cmd_in_sync_mode_xscale(struct megasas_instance *instance, 4587 struct megasas_cmd *cmd) 4588 { 4589 int i; 4590 uint32_t msecs = MFI_POLL_TIMEOUT_SECS * (10 * MILLISEC); 4591 4592 cmd->cmd_status = ENODATA; 4593 4594 WR_IB_QPORT((host_to_le32(cmd->frame_phys_addr) >> 3) | 4595 (cmd->frame_count - 1), instance); 4596 4597 mutex_enter(&instance->int_cmd_mtx); 4598 4599 for (i = 0; i < msecs && (cmd->cmd_status == ENODATA); i++) { 4600 cv_wait(&instance->int_cmd_cv, &instance->int_cmd_mtx); 4601 } 4602 4603 mutex_exit(&instance->int_cmd_mtx); 4604 4605 if (i < (msecs -1)) { 4606 return (0); 4607 } else { 4608 return (1); 4609 } 4610 } 4611 4612 static int 4613 issue_cmd_in_sync_mode_ppc(struct megasas_instance *instance, 4614 struct megasas_cmd *cmd) 4615 { 4616 int i; 4617 uint32_t msecs = MFI_POLL_TIMEOUT_SECS * (10 * MILLISEC); 4618 4619 con_log(CL_ANN1, (CE_NOTE, "issue_cmd_in_sync_mode_ppc: called\n")); 4620 4621 cmd->cmd_status = ENODATA; 4622 4623 WR_IB_QPORT((host_to_le32(cmd->frame_phys_addr)) | 4624 (((cmd->frame_count - 1) << 1) | 1), instance); 4625 4626 mutex_enter(&instance->int_cmd_mtx); 4627 4628 for (i = 0; i < msecs && (cmd->cmd_status == ENODATA); i++) { 4629 cv_wait(&instance->int_cmd_cv, &instance->int_cmd_mtx); 4630 } 4631 4632 mutex_exit(&instance->int_cmd_mtx); 4633 4634 con_log(CL_ANN1, (CE_NOTE, "issue_cmd_in_sync_mode_ppc: done\n")); 4635 4636 if (i < (msecs -1)) { 4637 return (0); 4638 } else { 4639 return (1); 4640 } 4641 } 4642 4643 /* 4644 * issue_cmd_in_poll_mode 4645 */ 4646 static int 4647 issue_cmd_in_poll_mode_xscale(struct megasas_instance *instance, 4648 struct megasas_cmd *cmd) 4649 { 4650 int i; 4651 uint32_t msecs = MFI_POLL_TIMEOUT_SECS * MILLISEC; 4652 struct megasas_header *frame_hdr; 4653 4654 frame_hdr = (struct megasas_header *)cmd->frame; 4655 frame_hdr->cmd_status = MFI_CMD_STATUS_POLL_MODE; 4656 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE; 4657 4658 /* issue the frame using inbound queue port */ 4659 WR_IB_QPORT((host_to_le32(cmd->frame_phys_addr) >> 3) | 4660 (cmd->frame_count - 1), instance); 4661 4662 /* wait for cmd_status to change from 0xFF */ 4663 for (i = 0; i < msecs && (frame_hdr->cmd_status == 4664 MFI_CMD_STATUS_POLL_MODE); i++) { 4665 drv_usecwait(MILLISEC); /* wait for 1000 usecs */ 4666 } 4667 4668 if (frame_hdr->cmd_status == MFI_CMD_STATUS_POLL_MODE) { 4669 con_log(CL_ANN, (CE_NOTE, "issue_cmd_in_poll_mode: " 4670 "cmd polling timed out")); 4671 return (DDI_FAILURE); 4672 } 4673 4674 return (DDI_SUCCESS); 4675 } 4676 4677 static int 4678 issue_cmd_in_poll_mode_ppc(struct megasas_instance *instance, 4679 struct megasas_cmd *cmd) 4680 { 4681 int i; 4682 uint32_t msecs = MFI_POLL_TIMEOUT_SECS * MILLISEC; 4683 struct megasas_header *frame_hdr; 4684 4685 con_log(CL_ANN1, (CE_NOTE, "issue_cmd_in_poll_mode_ppc: called\n")); 4686 4687 frame_hdr = (struct megasas_header *)cmd->frame; 4688 frame_hdr->cmd_status = MFI_CMD_STATUS_POLL_MODE; 4689 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE; 4690 4691 /* issue the frame using inbound queue port */ 4692 WR_IB_QPORT((host_to_le32(cmd->frame_phys_addr)) | 4693 (((cmd->frame_count - 1) << 1) | 1), instance); 4694 4695 /* wait for cmd_status to change from 0xFF */ 4696 for (i = 0; i < msecs && (frame_hdr->cmd_status == 4697 MFI_CMD_STATUS_POLL_MODE); i++) { 4698 drv_usecwait(MILLISEC); /* wait for 1000 usecs */ 4699 } 4700 4701 if (frame_hdr->cmd_status == MFI_CMD_STATUS_POLL_MODE) { 4702 con_log(CL_ANN, (CE_NOTE, "issue_cmd_in_poll_mode: " 4703 "cmd polling timed out")); 4704 return (DDI_FAILURE); 4705 } 4706 4707 return (DDI_SUCCESS); 4708 } 4709 4710 static void 4711 enable_intr_xscale(struct megasas_instance *instance) 4712 { 4713 MFI_ENABLE_INTR(instance); 4714 } 4715 4716 static void 4717 enable_intr_ppc(struct megasas_instance *instance) 4718 { 4719 uint32_t mask; 4720 4721 con_log(CL_ANN1, (CE_NOTE, "enable_intr_ppc: called\n")); 4722 4723 /* WR_OB_DOORBELL_CLEAR(0xFFFFFFFF, instance); */ 4724 WR_OB_DOORBELL_CLEAR(OB_DOORBELL_CLEAR_MASK, instance); 4725 4726 /* 4727 * As 1078DE is same as 1078 chip, the interrupt mask 4728 * remains the same. 4729 */ 4730 /* WR_OB_INTR_MASK(~0x80000000, instance); */ 4731 WR_OB_INTR_MASK(~(MFI_REPLY_1078_MESSAGE_INTR), instance); 4732 4733 /* dummy read to force PCI flush */ 4734 mask = RD_OB_INTR_MASK(instance); 4735 4736 con_log(CL_ANN1, (CE_NOTE, "enable_intr_ppc: " 4737 "outbound_intr_mask = 0x%x\n", mask)); 4738 } 4739 4740 static void 4741 disable_intr_xscale(struct megasas_instance *instance) 4742 { 4743 MFI_DISABLE_INTR(instance); 4744 } 4745 4746 static void 4747 disable_intr_ppc(struct megasas_instance *instance) 4748 { 4749 uint32_t mask; 4750 4751 con_log(CL_ANN1, (CE_NOTE, "disable_intr_ppc: called\n")); 4752 4753 con_log(CL_ANN1, (CE_NOTE, "disable_intr_ppc: before : " 4754 "outbound_intr_mask = 0x%x\n", RD_OB_INTR_MASK(instance))); 4755 4756 /* WR_OB_INTR_MASK(0xFFFFFFFF, instance); */ 4757 WR_OB_INTR_MASK(OB_INTR_MASK, instance); 4758 4759 con_log(CL_ANN1, (CE_NOTE, "disable_intr_ppc: after : " 4760 "outbound_intr_mask = 0x%x\n", RD_OB_INTR_MASK(instance))); 4761 4762 /* dummy read to force PCI flush */ 4763 mask = RD_OB_INTR_MASK(instance); 4764 #ifdef lint 4765 mask = mask; 4766 #endif 4767 } 4768 4769 static int 4770 intr_ack_xscale(struct megasas_instance *instance) 4771 { 4772 uint32_t status; 4773 4774 /* check if it is our interrupt */ 4775 status = RD_OB_INTR_STATUS(instance); 4776 4777 if (!(status & MFI_OB_INTR_STATUS_MASK)) { 4778 return (DDI_INTR_UNCLAIMED); 4779 } 4780 4781 /* clear the interrupt by writing back the same value */ 4782 WR_OB_INTR_STATUS(status, instance); 4783 4784 return (DDI_INTR_CLAIMED); 4785 } 4786 4787 static int 4788 intr_ack_ppc(struct megasas_instance *instance) 4789 { 4790 uint32_t status; 4791 4792 con_log(CL_ANN1, (CE_NOTE, "intr_ack_ppc: called\n")); 4793 4794 /* check if it is our interrupt */ 4795 status = RD_OB_INTR_STATUS(instance); 4796 4797 con_log(CL_ANN1, (CE_NOTE, "intr_ack_ppc: status = 0x%x\n", status)); 4798 4799 /* 4800 * As 1078DE is same as 1078 chip, the status field 4801 * remains the same. 4802 */ 4803 if (!(status & MFI_REPLY_1078_MESSAGE_INTR)) { 4804 return (DDI_INTR_UNCLAIMED); 4805 } 4806 4807 /* clear the interrupt by writing back the same value */ 4808 WR_OB_DOORBELL_CLEAR(status, instance); 4809 4810 /* dummy READ */ 4811 status = RD_OB_INTR_STATUS(instance); 4812 4813 con_log(CL_ANN1, (CE_NOTE, "intr_ack_ppc: interrupt cleared\n")); 4814 4815 return (DDI_INTR_CLAIMED); 4816 } 4817 4818 static int 4819 megasas_common_check(struct megasas_instance *instance, 4820 struct megasas_cmd *cmd) 4821 { 4822 int ret = DDI_SUCCESS; 4823 4824 if (megasas_check_dma_handle(cmd->frame_dma_obj.dma_handle) != 4825 DDI_SUCCESS) { 4826 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED); 4827 if (cmd->pkt != NULL) { 4828 cmd->pkt->pkt_reason = CMD_TRAN_ERR; 4829 cmd->pkt->pkt_statistics = 0; 4830 } 4831 ret = DDI_FAILURE; 4832 } 4833 if (megasas_check_dma_handle(instance->mfi_internal_dma_obj.dma_handle) 4834 != DDI_SUCCESS) { 4835 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED); 4836 if (cmd->pkt != NULL) { 4837 cmd->pkt->pkt_reason = CMD_TRAN_ERR; 4838 cmd->pkt->pkt_statistics = 0; 4839 } 4840 ret = DDI_FAILURE; 4841 } 4842 if (megasas_check_dma_handle(instance->mfi_evt_detail_obj.dma_handle) != 4843 DDI_SUCCESS) { 4844 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED); 4845 if (cmd->pkt != NULL) { 4846 cmd->pkt->pkt_reason = CMD_TRAN_ERR; 4847 cmd->pkt->pkt_statistics = 0; 4848 } 4849 ret = DDI_FAILURE; 4850 } 4851 if (megasas_check_acc_handle(instance->regmap_handle) != DDI_SUCCESS) { 4852 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED); 4853 ddi_fm_acc_err_clear(instance->regmap_handle, DDI_FME_VER0); 4854 if (cmd->pkt != NULL) { 4855 cmd->pkt->pkt_reason = CMD_TRAN_ERR; 4856 cmd->pkt->pkt_statistics = 0; 4857 } 4858 ret = DDI_FAILURE; 4859 } 4860 4861 return (ret); 4862 } 4863 4864 /*ARGSUSED*/ 4865 static int 4866 megasas_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data) 4867 { 4868 /* 4869 * as the driver can always deal with an error in any dma or 4870 * access handle, we can just return the fme_status value. 4871 */ 4872 pci_ereport_post(dip, err, NULL); 4873 return (err->fme_status); 4874 } 4875 4876 static void 4877 megasas_fm_init(struct megasas_instance *instance) 4878 { 4879 /* Need to change iblock to priority for new MSI intr */ 4880 ddi_iblock_cookie_t fm_ibc; 4881 4882 /* Only register with IO Fault Services if we have some capability */ 4883 if (instance->fm_capabilities) { 4884 /* Adjust access and dma attributes for FMA */ 4885 endian_attr.devacc_attr_access = DDI_FLAGERR_ACC; 4886 megasas_generic_dma_attr.dma_attr_flags = DDI_DMA_FLAGERR; 4887 4888 /* 4889 * Register capabilities with IO Fault Services. 4890 * fm_capabilities will be updated to indicate 4891 * capabilities actually supported (not requested.) 4892 */ 4893 4894 ddi_fm_init(instance->dip, &instance->fm_capabilities, &fm_ibc); 4895 4896 /* 4897 * Initialize pci ereport capabilities if ereport 4898 * capable (should always be.) 4899 */ 4900 4901 if (DDI_FM_EREPORT_CAP(instance->fm_capabilities) || 4902 DDI_FM_ERRCB_CAP(instance->fm_capabilities)) { 4903 pci_ereport_setup(instance->dip); 4904 } 4905 4906 /* 4907 * Register error callback if error callback capable. 4908 */ 4909 if (DDI_FM_ERRCB_CAP(instance->fm_capabilities)) { 4910 ddi_fm_handler_register(instance->dip, 4911 megasas_fm_error_cb, (void*) instance); 4912 } 4913 } else { 4914 endian_attr.devacc_attr_access = DDI_DEFAULT_ACC; 4915 megasas_generic_dma_attr.dma_attr_flags = 0; 4916 } 4917 } 4918 4919 static void 4920 megasas_fm_fini(struct megasas_instance *instance) 4921 { 4922 /* Only unregister FMA capabilities if registered */ 4923 if (instance->fm_capabilities) { 4924 /* 4925 * Un-register error callback if error callback capable. 4926 */ 4927 if (DDI_FM_ERRCB_CAP(instance->fm_capabilities)) { 4928 ddi_fm_handler_unregister(instance->dip); 4929 } 4930 4931 /* 4932 * Release any resources allocated by pci_ereport_setup() 4933 */ 4934 if (DDI_FM_EREPORT_CAP(instance->fm_capabilities) || 4935 DDI_FM_ERRCB_CAP(instance->fm_capabilities)) { 4936 pci_ereport_teardown(instance->dip); 4937 } 4938 4939 /* Unregister from IO Fault Services */ 4940 ddi_fm_fini(instance->dip); 4941 4942 /* Adjust access and dma attributes for FMA */ 4943 endian_attr.devacc_attr_access = DDI_DEFAULT_ACC; 4944 megasas_generic_dma_attr.dma_attr_flags = 0; 4945 } 4946 } 4947 4948 int 4949 megasas_check_acc_handle(ddi_acc_handle_t handle) 4950 { 4951 ddi_fm_error_t de; 4952 4953 if (handle == NULL) { 4954 return (DDI_FAILURE); 4955 } 4956 4957 ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION); 4958 4959 return (de.fme_status); 4960 } 4961 4962 int 4963 megasas_check_dma_handle(ddi_dma_handle_t handle) 4964 { 4965 ddi_fm_error_t de; 4966 4967 if (handle == NULL) { 4968 return (DDI_FAILURE); 4969 } 4970 4971 ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION); 4972 4973 return (de.fme_status); 4974 } 4975 4976 void 4977 megasas_fm_ereport(struct megasas_instance *instance, char *detail) 4978 { 4979 uint64_t ena; 4980 char buf[FM_MAX_CLASS]; 4981 4982 (void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail); 4983 ena = fm_ena_generate(0, FM_ENA_FMT1); 4984 if (DDI_FM_EREPORT_CAP(instance->fm_capabilities)) { 4985 ddi_fm_ereport_post(instance->dip, buf, ena, DDI_NOSLEEP, 4986 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERSION, NULL); 4987 } 4988 } 4989