1 /* 2 * mr_sas.c: source for mr_sas driver 3 * 4 * Solaris MegaRAID device driver for SAS2.0 controllers 5 * Copyright (c) 2008-2012, LSI Logic Corporation. 6 * All rights reserved. 7 * 8 * Version: 9 * Author: 10 * Swaminathan K S 11 * Arun Chandrashekhar 12 * Manju R 13 * Rasheed 14 * Shakeel Bukhari 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions are met: 18 * 19 * 1. Redistributions of source code must retain the above copyright notice, 20 * this list of conditions and the following disclaimer. 21 * 22 * 2. Redistributions in binary form must reproduce the above copyright notice, 23 * this list of conditions and the following disclaimer in the documentation 24 * and/or other materials provided with the distribution. 25 * 26 * 3. Neither the name of the author nor the names of its contributors may be 27 * used to endorse or promote products derived from this software without 28 * specific prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 34 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 36 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 37 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 38 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 39 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 40 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 41 * DAMAGE. 42 */ 43 44 /* 45 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. 46 * Copyright (c) 2011 Bayard G. Bell. All rights reserved. 47 * Copyright 2012 Nexenta System, Inc. All rights reserved. 48 */ 49 50 #include <sys/types.h> 51 #include <sys/param.h> 52 #include <sys/file.h> 53 #include <sys/errno.h> 54 #include <sys/open.h> 55 #include <sys/cred.h> 56 #include <sys/modctl.h> 57 #include <sys/conf.h> 58 #include <sys/devops.h> 59 #include <sys/cmn_err.h> 60 #include <sys/kmem.h> 61 #include <sys/stat.h> 62 #include <sys/mkdev.h> 63 #include <sys/pci.h> 64 #include <sys/scsi/scsi.h> 65 #include <sys/ddi.h> 66 #include <sys/sunddi.h> 67 #include <sys/atomic.h> 68 #include <sys/signal.h> 69 #include <sys/byteorder.h> 70 #include <sys/sdt.h> 71 #include <sys/fs/dv_node.h> /* devfs_clean */ 72 73 #include "mr_sas.h" 74 75 /* 76 * FMA header files 77 */ 78 #include <sys/ddifm.h> 79 #include <sys/fm/protocol.h> 80 #include <sys/fm/util.h> 81 #include <sys/fm/io/ddi.h> 82 83 /* 84 * Local static data 85 */ 86 static void *mrsas_state = NULL; 87 static volatile boolean_t mrsas_relaxed_ordering = B_TRUE; 88 volatile int debug_level_g = CL_NONE; 89 static volatile int msi_enable = 1; 90 static volatile int ctio_enable = 1; 91 92 /* Default Timeout value to issue online controller reset */ 93 volatile int debug_timeout_g = 0xF0; /* 0xB4; */ 94 /* Simulate consecutive firmware fault */ 95 static volatile int debug_fw_faults_after_ocr_g = 0; 96 #ifdef OCRDEBUG 97 /* Simulate three consecutive timeout for an IO */ 98 static volatile int debug_consecutive_timeout_after_ocr_g = 0; 99 #endif 100 101 #pragma weak scsi_hba_open 102 #pragma weak scsi_hba_close 103 #pragma weak scsi_hba_ioctl 104 105 /* Local static prototypes. */ 106 static int mrsas_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 107 static int mrsas_attach(dev_info_t *, ddi_attach_cmd_t); 108 #ifdef __sparc 109 static int mrsas_reset(dev_info_t *, ddi_reset_cmd_t); 110 #else 111 static int mrsas_quiesce(dev_info_t *); 112 #endif 113 static int mrsas_detach(dev_info_t *, ddi_detach_cmd_t); 114 static int mrsas_open(dev_t *, int, int, cred_t *); 115 static int mrsas_close(dev_t, int, int, cred_t *); 116 static int mrsas_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 117 118 static int mrsas_tran_tgt_init(dev_info_t *, dev_info_t *, 119 scsi_hba_tran_t *, struct scsi_device *); 120 static struct scsi_pkt *mrsas_tran_init_pkt(struct scsi_address *, register 121 struct scsi_pkt *, struct buf *, int, int, int, int, 122 int (*)(), caddr_t); 123 static int mrsas_tran_start(struct scsi_address *, 124 register struct scsi_pkt *); 125 static int mrsas_tran_abort(struct scsi_address *, struct scsi_pkt *); 126 static int mrsas_tran_reset(struct scsi_address *, int); 127 static int mrsas_tran_getcap(struct scsi_address *, char *, int); 128 static int mrsas_tran_setcap(struct scsi_address *, char *, int, int); 129 static void mrsas_tran_destroy_pkt(struct scsi_address *, 130 struct scsi_pkt *); 131 static void mrsas_tran_dmafree(struct scsi_address *, struct scsi_pkt *); 132 static void mrsas_tran_sync_pkt(struct scsi_address *, struct scsi_pkt *); 133 static int mrsas_tran_quiesce(dev_info_t *dip); 134 static int mrsas_tran_unquiesce(dev_info_t *dip); 135 static uint_t mrsas_isr(); 136 static uint_t mrsas_softintr(); 137 static void mrsas_undo_resources(dev_info_t *, struct mrsas_instance *); 138 static struct mrsas_cmd *get_mfi_pkt(struct mrsas_instance *); 139 static void return_mfi_pkt(struct mrsas_instance *, 140 struct mrsas_cmd *); 141 142 static void free_space_for_mfi(struct mrsas_instance *); 143 static uint32_t read_fw_status_reg_ppc(struct mrsas_instance *); 144 static void issue_cmd_ppc(struct mrsas_cmd *, struct mrsas_instance *); 145 static int issue_cmd_in_poll_mode_ppc(struct mrsas_instance *, 146 struct mrsas_cmd *); 147 static int issue_cmd_in_sync_mode_ppc(struct mrsas_instance *, 148 struct mrsas_cmd *); 149 static void enable_intr_ppc(struct mrsas_instance *); 150 static void disable_intr_ppc(struct mrsas_instance *); 151 static int intr_ack_ppc(struct mrsas_instance *); 152 static void flush_cache(struct mrsas_instance *instance); 153 void display_scsi_inquiry(caddr_t); 154 static int start_mfi_aen(struct mrsas_instance *instance); 155 static int handle_drv_ioctl(struct mrsas_instance *instance, 156 struct mrsas_ioctl *ioctl, int mode); 157 static int handle_mfi_ioctl(struct mrsas_instance *instance, 158 struct mrsas_ioctl *ioctl, int mode); 159 static int handle_mfi_aen(struct mrsas_instance *instance, 160 struct mrsas_aen *aen); 161 static struct mrsas_cmd *build_cmd(struct mrsas_instance *, 162 struct scsi_address *, struct scsi_pkt *, uchar_t *); 163 static int alloc_additional_dma_buffer(struct mrsas_instance *); 164 static void complete_cmd_in_sync_mode(struct mrsas_instance *, 165 struct mrsas_cmd *); 166 static int mrsas_kill_adapter(struct mrsas_instance *); 167 static int mrsas_issue_init_mfi(struct mrsas_instance *); 168 static int mrsas_reset_ppc(struct mrsas_instance *); 169 static uint32_t mrsas_initiate_ocr_if_fw_is_faulty(struct mrsas_instance *); 170 static int wait_for_outstanding(struct mrsas_instance *instance); 171 static int register_mfi_aen(struct mrsas_instance *instance, 172 uint32_t seq_num, uint32_t class_locale_word); 173 static int issue_mfi_pthru(struct mrsas_instance *instance, struct 174 mrsas_ioctl *ioctl, struct mrsas_cmd *cmd, int mode); 175 static int issue_mfi_dcmd(struct mrsas_instance *instance, struct 176 mrsas_ioctl *ioctl, struct mrsas_cmd *cmd, int mode); 177 static int issue_mfi_smp(struct mrsas_instance *instance, struct 178 mrsas_ioctl *ioctl, struct mrsas_cmd *cmd, int mode); 179 static int issue_mfi_stp(struct mrsas_instance *instance, struct 180 mrsas_ioctl *ioctl, struct mrsas_cmd *cmd, int mode); 181 static int abort_aen_cmd(struct mrsas_instance *instance, 182 struct mrsas_cmd *cmd_to_abort); 183 184 static void mrsas_rem_intrs(struct mrsas_instance *instance); 185 static int mrsas_add_intrs(struct mrsas_instance *instance, int intr_type); 186 187 static void mrsas_tran_tgt_free(dev_info_t *, dev_info_t *, 188 scsi_hba_tran_t *, struct scsi_device *); 189 static int mrsas_tran_bus_config(dev_info_t *, uint_t, 190 ddi_bus_config_op_t, void *, dev_info_t **); 191 static int mrsas_parse_devname(char *, int *, int *); 192 static int mrsas_config_all_devices(struct mrsas_instance *); 193 static int mrsas_config_ld(struct mrsas_instance *, uint16_t, 194 uint8_t, dev_info_t **); 195 static int mrsas_name_node(dev_info_t *, char *, int); 196 static void mrsas_issue_evt_taskq(struct mrsas_eventinfo *); 197 static void free_additional_dma_buffer(struct mrsas_instance *); 198 static void io_timeout_checker(void *); 199 static void mrsas_fm_init(struct mrsas_instance *); 200 static void mrsas_fm_fini(struct mrsas_instance *); 201 202 static struct mrsas_function_template mrsas_function_template_ppc = { 203 .read_fw_status_reg = read_fw_status_reg_ppc, 204 .issue_cmd = issue_cmd_ppc, 205 .issue_cmd_in_sync_mode = issue_cmd_in_sync_mode_ppc, 206 .issue_cmd_in_poll_mode = issue_cmd_in_poll_mode_ppc, 207 .enable_intr = enable_intr_ppc, 208 .disable_intr = disable_intr_ppc, 209 .intr_ack = intr_ack_ppc, 210 .init_adapter = mrsas_init_adapter_ppc 211 }; 212 213 214 static struct mrsas_function_template mrsas_function_template_fusion = { 215 .read_fw_status_reg = tbolt_read_fw_status_reg, 216 .issue_cmd = tbolt_issue_cmd, 217 .issue_cmd_in_sync_mode = tbolt_issue_cmd_in_sync_mode, 218 .issue_cmd_in_poll_mode = tbolt_issue_cmd_in_poll_mode, 219 .enable_intr = tbolt_enable_intr, 220 .disable_intr = tbolt_disable_intr, 221 .intr_ack = tbolt_intr_ack, 222 .init_adapter = mrsas_init_adapter_tbolt 223 }; 224 225 226 ddi_dma_attr_t mrsas_generic_dma_attr = { 227 DMA_ATTR_V0, /* dma_attr_version */ 228 0, /* low DMA address range */ 229 0xFFFFFFFFU, /* high DMA address range */ 230 0xFFFFFFFFU, /* DMA counter register */ 231 8, /* DMA address alignment */ 232 0x07, /* DMA burstsizes */ 233 1, /* min DMA size */ 234 0xFFFFFFFFU, /* max DMA size */ 235 0xFFFFFFFFU, /* segment boundary */ 236 MRSAS_MAX_SGE_CNT, /* dma_attr_sglen */ 237 512, /* granularity of device */ 238 0 /* bus specific DMA flags */ 239 }; 240 241 int32_t mrsas_max_cap_maxxfer = 0x1000000; 242 243 /* 244 * Fix for: Thunderbolt controller IO timeout when IO write size is 1MEG, 245 * Limit size to 256K 246 */ 247 uint32_t mrsas_tbolt_max_cap_maxxfer = (512 * 512); 248 249 /* 250 * cb_ops contains base level routines 251 */ 252 static struct cb_ops mrsas_cb_ops = { 253 mrsas_open, /* open */ 254 mrsas_close, /* close */ 255 nodev, /* strategy */ 256 nodev, /* print */ 257 nodev, /* dump */ 258 nodev, /* read */ 259 nodev, /* write */ 260 mrsas_ioctl, /* ioctl */ 261 nodev, /* devmap */ 262 nodev, /* mmap */ 263 nodev, /* segmap */ 264 nochpoll, /* poll */ 265 nodev, /* cb_prop_op */ 266 0, /* streamtab */ 267 D_NEW | D_HOTPLUG, /* cb_flag */ 268 CB_REV, /* cb_rev */ 269 nodev, /* cb_aread */ 270 nodev /* cb_awrite */ 271 }; 272 273 /* 274 * dev_ops contains configuration routines 275 */ 276 static struct dev_ops mrsas_ops = { 277 DEVO_REV, /* rev, */ 278 0, /* refcnt */ 279 mrsas_getinfo, /* getinfo */ 280 nulldev, /* identify */ 281 nulldev, /* probe */ 282 mrsas_attach, /* attach */ 283 mrsas_detach, /* detach */ 284 #ifdef __sparc 285 mrsas_reset, /* reset */ 286 #else /* __sparc */ 287 nodev, 288 #endif /* __sparc */ 289 &mrsas_cb_ops, /* char/block ops */ 290 NULL, /* bus ops */ 291 NULL, /* power */ 292 #ifdef __sparc 293 ddi_quiesce_not_needed 294 #else /* __sparc */ 295 mrsas_quiesce /* quiesce */ 296 #endif /* __sparc */ 297 }; 298 299 static struct modldrv modldrv = { 300 &mod_driverops, /* module type - driver */ 301 MRSAS_VERSION, 302 &mrsas_ops, /* driver ops */ 303 }; 304 305 static struct modlinkage modlinkage = { 306 MODREV_1, /* ml_rev - must be MODREV_1 */ 307 &modldrv, /* ml_linkage */ 308 NULL /* end of driver linkage */ 309 }; 310 311 static struct ddi_device_acc_attr endian_attr = { 312 DDI_DEVICE_ATTR_V1, 313 DDI_STRUCTURE_LE_ACC, 314 DDI_STRICTORDER_ACC, 315 DDI_DEFAULT_ACC 316 }; 317 318 /* Use the LSI Fast Path for the 2208 (tbolt) commands. */ 319 unsigned int enable_fp = 1; 320 321 322 /* 323 * ************************************************************************** * 324 * * 325 * common entry points - for loadable kernel modules * 326 * * 327 * ************************************************************************** * 328 */ 329 330 /* 331 * _init - initialize a loadable module 332 * @void 333 * 334 * The driver should perform any one-time resource allocation or data 335 * initialization during driver loading in _init(). For example, the driver 336 * should initialize any mutexes global to the driver in this routine. 337 * The driver should not, however, use _init() to allocate or initialize 338 * anything that has to do with a particular instance of the device. 339 * Per-instance initialization must be done in attach(). 340 */ 341 int 342 _init(void) 343 { 344 int ret; 345 346 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 347 348 ret = ddi_soft_state_init(&mrsas_state, 349 sizeof (struct mrsas_instance), 0); 350 351 if (ret != DDI_SUCCESS) { 352 cmn_err(CE_WARN, "mr_sas: could not init state"); 353 return (ret); 354 } 355 356 if ((ret = scsi_hba_init(&modlinkage)) != DDI_SUCCESS) { 357 cmn_err(CE_WARN, "mr_sas: could not init scsi hba"); 358 ddi_soft_state_fini(&mrsas_state); 359 return (ret); 360 } 361 362 ret = mod_install(&modlinkage); 363 364 if (ret != DDI_SUCCESS) { 365 cmn_err(CE_WARN, "mr_sas: mod_install failed"); 366 scsi_hba_fini(&modlinkage); 367 ddi_soft_state_fini(&mrsas_state); 368 } 369 370 return (ret); 371 } 372 373 /* 374 * _info - returns information about a loadable module. 375 * @void 376 * 377 * _info() is called to return module information. This is a typical entry 378 * point that does predefined role. It simply calls mod_info(). 379 */ 380 int 381 _info(struct modinfo *modinfop) 382 { 383 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 384 385 return (mod_info(&modlinkage, modinfop)); 386 } 387 388 /* 389 * _fini - prepare a loadable module for unloading 390 * @void 391 * 392 * In _fini(), the driver should release any resources that were allocated in 393 * _init(). The driver must remove itself from the system module list. 394 */ 395 int 396 _fini(void) 397 { 398 int ret; 399 400 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 401 402 if ((ret = mod_remove(&modlinkage)) != DDI_SUCCESS) { 403 con_log(CL_ANN1, 404 (CE_WARN, "_fini: mod_remove() failed, error 0x%X", ret)); 405 return (ret); 406 } 407 408 scsi_hba_fini(&modlinkage); 409 con_log(CL_DLEVEL1, (CE_NOTE, "_fini: scsi_hba_fini() done.")); 410 411 ddi_soft_state_fini(&mrsas_state); 412 con_log(CL_DLEVEL1, (CE_NOTE, "_fini: ddi_soft_state_fini() done.")); 413 414 return (ret); 415 } 416 417 418 /* 419 * ************************************************************************** * 420 * * 421 * common entry points - for autoconfiguration * 422 * * 423 * ************************************************************************** * 424 */ 425 /* 426 * attach - adds a device to the system as part of initialization 427 * @dip: 428 * @cmd: 429 * 430 * The kernel calls a driver's attach() entry point to attach an instance of 431 * a device (for MegaRAID, it is instance of a controller) or to resume 432 * operation for an instance of a device that has been suspended or has been 433 * shut down by the power management framework 434 * The attach() entry point typically includes the following types of 435 * processing: 436 * - allocate a soft-state structure for the device instance (for MegaRAID, 437 * controller instance) 438 * - initialize per-instance mutexes 439 * - initialize condition variables 440 * - register the device's interrupts (for MegaRAID, controller's interrupts) 441 * - map the registers and memory of the device instance (for MegaRAID, 442 * controller instance) 443 * - create minor device nodes for the device instance (for MegaRAID, 444 * controller instance) 445 * - report that the device instance (for MegaRAID, controller instance) has 446 * attached 447 */ 448 static int 449 mrsas_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 450 { 451 int instance_no; 452 int nregs; 453 int i = 0; 454 uint8_t irq; 455 uint16_t vendor_id; 456 uint16_t device_id; 457 uint16_t subsysvid; 458 uint16_t subsysid; 459 uint16_t command; 460 off_t reglength = 0; 461 int intr_types = 0; 462 char *data; 463 464 scsi_hba_tran_t *tran; 465 ddi_dma_attr_t tran_dma_attr; 466 struct mrsas_instance *instance; 467 468 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 469 470 /* CONSTCOND */ 471 ASSERT(NO_COMPETING_THREADS); 472 473 instance_no = ddi_get_instance(dip); 474 475 /* 476 * check to see whether this device is in a DMA-capable slot. 477 */ 478 if (ddi_slaveonly(dip) == DDI_SUCCESS) { 479 cmn_err(CE_WARN, 480 "mr_sas%d: Device in slave-only slot, unused", 481 instance_no); 482 return (DDI_FAILURE); 483 } 484 485 switch (cmd) { 486 case DDI_ATTACH: 487 /* allocate the soft state for the instance */ 488 if (ddi_soft_state_zalloc(mrsas_state, instance_no) 489 != DDI_SUCCESS) { 490 cmn_err(CE_WARN, 491 "mr_sas%d: Failed to allocate soft state", 492 instance_no); 493 return (DDI_FAILURE); 494 } 495 496 instance = (struct mrsas_instance *)ddi_get_soft_state 497 (mrsas_state, instance_no); 498 499 if (instance == NULL) { 500 cmn_err(CE_WARN, 501 "mr_sas%d: Bad soft state", instance_no); 502 ddi_soft_state_free(mrsas_state, instance_no); 503 return (DDI_FAILURE); 504 } 505 506 instance->unroll.softs = 1; 507 508 /* Setup the PCI configuration space handles */ 509 if (pci_config_setup(dip, &instance->pci_handle) != 510 DDI_SUCCESS) { 511 cmn_err(CE_WARN, 512 "mr_sas%d: pci config setup failed ", 513 instance_no); 514 515 ddi_soft_state_free(mrsas_state, instance_no); 516 return (DDI_FAILURE); 517 } 518 519 if (ddi_dev_nregs(dip, &nregs) != DDI_SUCCESS) { 520 cmn_err(CE_WARN, 521 "mr_sas: failed to get registers."); 522 523 pci_config_teardown(&instance->pci_handle); 524 ddi_soft_state_free(mrsas_state, instance_no); 525 return (DDI_FAILURE); 526 } 527 528 vendor_id = pci_config_get16(instance->pci_handle, 529 PCI_CONF_VENID); 530 device_id = pci_config_get16(instance->pci_handle, 531 PCI_CONF_DEVID); 532 533 subsysvid = pci_config_get16(instance->pci_handle, 534 PCI_CONF_SUBVENID); 535 subsysid = pci_config_get16(instance->pci_handle, 536 PCI_CONF_SUBSYSID); 537 538 pci_config_put16(instance->pci_handle, PCI_CONF_COMM, 539 (pci_config_get16(instance->pci_handle, 540 PCI_CONF_COMM) | PCI_COMM_ME)); 541 irq = pci_config_get8(instance->pci_handle, 542 PCI_CONF_ILINE); 543 544 con_log(CL_DLEVEL1, (CE_CONT, "mr_sas%d: " 545 "0x%x:0x%x 0x%x:0x%x, irq:%d drv-ver:%s", 546 instance_no, vendor_id, device_id, subsysvid, 547 subsysid, irq, MRSAS_VERSION)); 548 549 /* enable bus-mastering */ 550 command = pci_config_get16(instance->pci_handle, 551 PCI_CONF_COMM); 552 553 if (!(command & PCI_COMM_ME)) { 554 command |= PCI_COMM_ME; 555 556 pci_config_put16(instance->pci_handle, 557 PCI_CONF_COMM, command); 558 559 con_log(CL_ANN, (CE_CONT, "mr_sas%d: " 560 "enable bus-mastering", instance_no)); 561 } else { 562 con_log(CL_DLEVEL1, (CE_CONT, "mr_sas%d: " 563 "bus-mastering already set", instance_no)); 564 } 565 566 /* initialize function pointers */ 567 switch (device_id) { 568 case PCI_DEVICE_ID_LSI_TBOLT: 569 case PCI_DEVICE_ID_LSI_INVADER: 570 con_log(CL_ANN, (CE_NOTE, 571 "mr_sas: 2208 T.B. device detected")); 572 573 instance->func_ptr = 574 &mrsas_function_template_fusion; 575 instance->tbolt = 1; 576 break; 577 578 case PCI_DEVICE_ID_LSI_2108VDE: 579 case PCI_DEVICE_ID_LSI_2108V: 580 con_log(CL_ANN, (CE_NOTE, 581 "mr_sas: 2108 Liberator device detected")); 582 583 instance->func_ptr = 584 &mrsas_function_template_ppc; 585 break; 586 587 default: 588 cmn_err(CE_WARN, 589 "mr_sas: Invalid device detected"); 590 591 pci_config_teardown(&instance->pci_handle); 592 ddi_soft_state_free(mrsas_state, instance_no); 593 return (DDI_FAILURE); 594 } 595 596 instance->baseaddress = pci_config_get32( 597 instance->pci_handle, PCI_CONF_BASE0); 598 instance->baseaddress &= 0x0fffc; 599 600 instance->dip = dip; 601 instance->vendor_id = vendor_id; 602 instance->device_id = device_id; 603 instance->subsysvid = subsysvid; 604 instance->subsysid = subsysid; 605 instance->instance = instance_no; 606 607 /* Initialize FMA */ 608 instance->fm_capabilities = ddi_prop_get_int( 609 DDI_DEV_T_ANY, instance->dip, DDI_PROP_DONTPASS, 610 "fm-capable", DDI_FM_EREPORT_CAPABLE | 611 DDI_FM_ACCCHK_CAPABLE | DDI_FM_DMACHK_CAPABLE 612 | DDI_FM_ERRCB_CAPABLE); 613 614 mrsas_fm_init(instance); 615 616 /* Setup register map */ 617 if ((ddi_dev_regsize(instance->dip, 618 REGISTER_SET_IO_2108, ®length) != DDI_SUCCESS) || 619 reglength < MINIMUM_MFI_MEM_SZ) { 620 goto fail_attach; 621 } 622 if (reglength > DEFAULT_MFI_MEM_SZ) { 623 reglength = DEFAULT_MFI_MEM_SZ; 624 con_log(CL_DLEVEL1, (CE_NOTE, 625 "mr_sas: register length to map is 0x%lx bytes", 626 reglength)); 627 } 628 if (ddi_regs_map_setup(instance->dip, 629 REGISTER_SET_IO_2108, &instance->regmap, 0, 630 reglength, &endian_attr, &instance->regmap_handle) 631 != DDI_SUCCESS) { 632 cmn_err(CE_WARN, 633 "mr_sas: couldn't map control registers"); 634 goto fail_attach; 635 } 636 637 instance->unroll.regs = 1; 638 639 /* 640 * Disable Interrupt Now. 641 * Setup Software interrupt 642 */ 643 instance->func_ptr->disable_intr(instance); 644 645 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0, 646 "mrsas-enable-msi", &data) == DDI_SUCCESS) { 647 if (strncmp(data, "no", 3) == 0) { 648 msi_enable = 0; 649 con_log(CL_ANN1, (CE_WARN, 650 "msi_enable = %d disabled", msi_enable)); 651 } 652 ddi_prop_free(data); 653 } 654 655 con_log(CL_DLEVEL1, (CE_NOTE, "msi_enable = %d", msi_enable)); 656 657 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0, 658 "mrsas-enable-fp", &data) == DDI_SUCCESS) { 659 if (strncmp(data, "no", 3) == 0) { 660 enable_fp = 0; 661 cmn_err(CE_NOTE, 662 "enable_fp = %d, Fast-Path disabled.\n", 663 enable_fp); 664 } 665 666 ddi_prop_free(data); 667 } 668 669 con_log(CL_DLEVEL1, (CE_NOTE, "enable_fp = %d\n", enable_fp)); 670 671 /* Check for all supported interrupt types */ 672 if (ddi_intr_get_supported_types( 673 dip, &intr_types) != DDI_SUCCESS) { 674 cmn_err(CE_WARN, 675 "ddi_intr_get_supported_types() failed"); 676 goto fail_attach; 677 } 678 679 con_log(CL_DLEVEL1, (CE_NOTE, 680 "ddi_intr_get_supported_types() ret: 0x%x", intr_types)); 681 682 /* Initialize and Setup Interrupt handler */ 683 if (msi_enable && (intr_types & DDI_INTR_TYPE_MSIX)) { 684 if (mrsas_add_intrs(instance, DDI_INTR_TYPE_MSIX) != 685 DDI_SUCCESS) { 686 cmn_err(CE_WARN, 687 "MSIX interrupt query failed"); 688 goto fail_attach; 689 } 690 instance->intr_type = DDI_INTR_TYPE_MSIX; 691 } else if (msi_enable && (intr_types & DDI_INTR_TYPE_MSI)) { 692 if (mrsas_add_intrs(instance, DDI_INTR_TYPE_MSI) != 693 DDI_SUCCESS) { 694 cmn_err(CE_WARN, 695 "MSI interrupt query failed"); 696 goto fail_attach; 697 } 698 instance->intr_type = DDI_INTR_TYPE_MSI; 699 } else if (intr_types & DDI_INTR_TYPE_FIXED) { 700 msi_enable = 0; 701 if (mrsas_add_intrs(instance, DDI_INTR_TYPE_FIXED) != 702 DDI_SUCCESS) { 703 cmn_err(CE_WARN, 704 "FIXED interrupt query failed"); 705 goto fail_attach; 706 } 707 instance->intr_type = DDI_INTR_TYPE_FIXED; 708 } else { 709 cmn_err(CE_WARN, "Device cannot " 710 "suppport either FIXED or MSI/X " 711 "interrupts"); 712 goto fail_attach; 713 } 714 715 instance->unroll.intr = 1; 716 717 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0, 718 "mrsas-enable-ctio", &data) == DDI_SUCCESS) { 719 if (strncmp(data, "no", 3) == 0) { 720 ctio_enable = 0; 721 con_log(CL_ANN1, (CE_WARN, 722 "ctio_enable = %d disabled", ctio_enable)); 723 } 724 ddi_prop_free(data); 725 } 726 727 con_log(CL_DLEVEL1, (CE_WARN, "ctio_enable = %d", ctio_enable)); 728 729 /* setup the mfi based low level driver */ 730 if (mrsas_init_adapter(instance) != DDI_SUCCESS) { 731 cmn_err(CE_WARN, "mr_sas: " 732 "could not initialize the low level driver"); 733 734 goto fail_attach; 735 } 736 737 /* Initialize all Mutex */ 738 INIT_LIST_HEAD(&instance->completed_pool_list); 739 mutex_init(&instance->completed_pool_mtx, NULL, 740 MUTEX_DRIVER, DDI_INTR_PRI(instance->intr_pri)); 741 742 mutex_init(&instance->sync_map_mtx, NULL, 743 MUTEX_DRIVER, DDI_INTR_PRI(instance->intr_pri)); 744 745 mutex_init(&instance->app_cmd_pool_mtx, NULL, 746 MUTEX_DRIVER, DDI_INTR_PRI(instance->intr_pri)); 747 748 mutex_init(&instance->config_dev_mtx, NULL, 749 MUTEX_DRIVER, DDI_INTR_PRI(instance->intr_pri)); 750 751 mutex_init(&instance->cmd_pend_mtx, NULL, 752 MUTEX_DRIVER, DDI_INTR_PRI(instance->intr_pri)); 753 754 mutex_init(&instance->ocr_flags_mtx, NULL, 755 MUTEX_DRIVER, DDI_INTR_PRI(instance->intr_pri)); 756 757 mutex_init(&instance->int_cmd_mtx, NULL, 758 MUTEX_DRIVER, DDI_INTR_PRI(instance->intr_pri)); 759 cv_init(&instance->int_cmd_cv, NULL, CV_DRIVER, NULL); 760 761 mutex_init(&instance->cmd_pool_mtx, NULL, 762 MUTEX_DRIVER, DDI_INTR_PRI(instance->intr_pri)); 763 764 mutex_init(&instance->reg_write_mtx, NULL, 765 MUTEX_DRIVER, DDI_INTR_PRI(instance->intr_pri)); 766 767 if (instance->tbolt) { 768 mutex_init(&instance->cmd_app_pool_mtx, NULL, 769 MUTEX_DRIVER, DDI_INTR_PRI(instance->intr_pri)); 770 771 mutex_init(&instance->chip_mtx, NULL, 772 MUTEX_DRIVER, DDI_INTR_PRI(instance->intr_pri)); 773 774 } 775 776 instance->unroll.mutexs = 1; 777 778 instance->timeout_id = (timeout_id_t)-1; 779 780 /* Register our soft-isr for highlevel interrupts. */ 781 instance->isr_level = instance->intr_pri; 782 if (!(instance->tbolt)) { 783 if (instance->isr_level == HIGH_LEVEL_INTR) { 784 if (ddi_add_softintr(dip, 785 DDI_SOFTINT_HIGH, 786 &instance->soft_intr_id, NULL, NULL, 787 mrsas_softintr, (caddr_t)instance) != 788 DDI_SUCCESS) { 789 cmn_err(CE_WARN, 790 "Software ISR did not register"); 791 792 goto fail_attach; 793 } 794 795 instance->unroll.soft_isr = 1; 796 797 } 798 } 799 800 instance->softint_running = 0; 801 802 /* Allocate a transport structure */ 803 tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP); 804 805 if (tran == NULL) { 806 cmn_err(CE_WARN, 807 "scsi_hba_tran_alloc failed"); 808 goto fail_attach; 809 } 810 811 instance->tran = tran; 812 instance->unroll.tran = 1; 813 814 tran->tran_hba_private = instance; 815 tran->tran_tgt_init = mrsas_tran_tgt_init; 816 tran->tran_tgt_probe = scsi_hba_probe; 817 tran->tran_tgt_free = mrsas_tran_tgt_free; 818 if (instance->tbolt) { 819 tran->tran_init_pkt = 820 mrsas_tbolt_tran_init_pkt; 821 tran->tran_start = 822 mrsas_tbolt_tran_start; 823 } else { 824 tran->tran_init_pkt = mrsas_tran_init_pkt; 825 tran->tran_start = mrsas_tran_start; 826 } 827 tran->tran_abort = mrsas_tran_abort; 828 tran->tran_reset = mrsas_tran_reset; 829 tran->tran_getcap = mrsas_tran_getcap; 830 tran->tran_setcap = mrsas_tran_setcap; 831 tran->tran_destroy_pkt = mrsas_tran_destroy_pkt; 832 tran->tran_dmafree = mrsas_tran_dmafree; 833 tran->tran_sync_pkt = mrsas_tran_sync_pkt; 834 tran->tran_quiesce = mrsas_tran_quiesce; 835 tran->tran_unquiesce = mrsas_tran_unquiesce; 836 tran->tran_bus_config = mrsas_tran_bus_config; 837 838 if (mrsas_relaxed_ordering) 839 mrsas_generic_dma_attr.dma_attr_flags |= 840 DDI_DMA_RELAXED_ORDERING; 841 842 843 tran_dma_attr = mrsas_generic_dma_attr; 844 tran_dma_attr.dma_attr_sgllen = instance->max_num_sge; 845 846 /* Attach this instance of the hba */ 847 if (scsi_hba_attach_setup(dip, &tran_dma_attr, tran, 0) 848 != DDI_SUCCESS) { 849 cmn_err(CE_WARN, 850 "scsi_hba_attach failed"); 851 852 goto fail_attach; 853 } 854 instance->unroll.tranSetup = 1; 855 con_log(CL_ANN1, 856 (CE_CONT, "scsi_hba_attach_setup() done.")); 857 858 /* create devctl node for cfgadm command */ 859 if (ddi_create_minor_node(dip, "devctl", 860 S_IFCHR, INST2DEVCTL(instance_no), 861 DDI_NT_SCSI_NEXUS, 0) == DDI_FAILURE) { 862 cmn_err(CE_WARN, 863 "mr_sas: failed to create devctl node."); 864 865 goto fail_attach; 866 } 867 868 instance->unroll.devctl = 1; 869 870 /* create scsi node for cfgadm command */ 871 if (ddi_create_minor_node(dip, "scsi", S_IFCHR, 872 INST2SCSI(instance_no), DDI_NT_SCSI_ATTACHMENT_POINT, 0) == 873 DDI_FAILURE) { 874 cmn_err(CE_WARN, 875 "mr_sas: failed to create scsi node."); 876 877 goto fail_attach; 878 } 879 880 instance->unroll.scsictl = 1; 881 882 (void) sprintf(instance->iocnode, "%d:lsirdctl", 883 instance_no); 884 885 /* 886 * Create a node for applications 887 * for issuing ioctl to the driver. 888 */ 889 if (ddi_create_minor_node(dip, instance->iocnode, 890 S_IFCHR, INST2LSIRDCTL(instance_no), DDI_PSEUDO, 0) == 891 DDI_FAILURE) { 892 cmn_err(CE_WARN, 893 "mr_sas: failed to create ioctl node."); 894 895 goto fail_attach; 896 } 897 898 instance->unroll.ioctl = 1; 899 900 /* Create a taskq to handle dr events */ 901 if ((instance->taskq = ddi_taskq_create(dip, 902 "mrsas_dr_taskq", 1, TASKQ_DEFAULTPRI, 0)) == NULL) { 903 cmn_err(CE_WARN, 904 "mr_sas: failed to create taskq "); 905 instance->taskq = NULL; 906 goto fail_attach; 907 } 908 instance->unroll.taskq = 1; 909 con_log(CL_ANN1, (CE_CONT, "ddi_taskq_create() done.")); 910 911 /* enable interrupt */ 912 instance->func_ptr->enable_intr(instance); 913 914 /* initiate AEN */ 915 if (start_mfi_aen(instance)) { 916 cmn_err(CE_WARN, 917 "mr_sas: failed to initiate AEN."); 918 goto fail_attach; 919 } 920 instance->unroll.aenPend = 1; 921 con_log(CL_ANN1, 922 (CE_CONT, "AEN started for instance %d.", instance_no)); 923 924 /* Finally! We are on the air. */ 925 ddi_report_dev(dip); 926 927 /* FMA handle checking. */ 928 if (mrsas_check_acc_handle(instance->regmap_handle) != 929 DDI_SUCCESS) { 930 goto fail_attach; 931 } 932 if (mrsas_check_acc_handle(instance->pci_handle) != 933 DDI_SUCCESS) { 934 goto fail_attach; 935 } 936 937 instance->mr_ld_list = 938 kmem_zalloc(MRDRV_MAX_LD * sizeof (struct mrsas_ld), 939 KM_SLEEP); 940 instance->unroll.ldlist_buff = 1; 941 942 #ifdef PDSUPPORT 943 if (instance->tbolt) { 944 instance->mr_tbolt_pd_max = MRSAS_TBOLT_PD_TGT_MAX; 945 instance->mr_tbolt_pd_list = 946 kmem_zalloc(MRSAS_TBOLT_GET_PD_MAX(instance) * 947 sizeof (struct mrsas_tbolt_pd), KM_SLEEP); 948 ASSERT(instance->mr_tbolt_pd_list); 949 for (i = 0; i < instance->mr_tbolt_pd_max; i++) { 950 instance->mr_tbolt_pd_list[i].lun_type = 951 MRSAS_TBOLT_PD_LUN; 952 instance->mr_tbolt_pd_list[i].dev_id = 953 (uint8_t)i; 954 } 955 956 instance->unroll.pdlist_buff = 1; 957 } 958 #endif 959 break; 960 case DDI_PM_RESUME: 961 con_log(CL_ANN, (CE_NOTE, "mr_sas: DDI_PM_RESUME")); 962 break; 963 case DDI_RESUME: 964 con_log(CL_ANN, (CE_NOTE, "mr_sas: DDI_RESUME")); 965 break; 966 default: 967 con_log(CL_ANN, 968 (CE_WARN, "mr_sas: invalid attach cmd=%x", cmd)); 969 return (DDI_FAILURE); 970 } 971 972 973 con_log(CL_DLEVEL1, 974 (CE_NOTE, "mrsas_attach() return SUCCESS instance_num %d", 975 instance_no)); 976 return (DDI_SUCCESS); 977 978 fail_attach: 979 980 mrsas_undo_resources(dip, instance); 981 982 mrsas_fm_ereport(instance, DDI_FM_DEVICE_NO_RESPONSE); 983 ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST); 984 985 mrsas_fm_fini(instance); 986 987 pci_config_teardown(&instance->pci_handle); 988 ddi_soft_state_free(mrsas_state, instance_no); 989 990 con_log(CL_ANN, (CE_WARN, "mr_sas: return failure from mrsas_attach")); 991 992 cmn_err(CE_WARN, "mrsas_attach() return FAILURE instance_num %d", 993 instance_no); 994 995 return (DDI_FAILURE); 996 } 997 998 /* 999 * getinfo - gets device information 1000 * @dip: 1001 * @cmd: 1002 * @arg: 1003 * @resultp: 1004 * 1005 * The system calls getinfo() to obtain configuration information that only 1006 * the driver knows. The mapping of minor numbers to device instance is 1007 * entirely under the control of the driver. The system sometimes needs to ask 1008 * the driver which device a particular dev_t represents. 1009 * Given the device number return the devinfo pointer from the scsi_device 1010 * structure. 1011 */ 1012 /*ARGSUSED*/ 1013 static int 1014 mrsas_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp) 1015 { 1016 int rval; 1017 int mrsas_minor = getminor((dev_t)arg); 1018 1019 struct mrsas_instance *instance; 1020 1021 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1022 1023 switch (cmd) { 1024 case DDI_INFO_DEVT2DEVINFO: 1025 instance = (struct mrsas_instance *) 1026 ddi_get_soft_state(mrsas_state, 1027 MINOR2INST(mrsas_minor)); 1028 1029 if (instance == NULL) { 1030 *resultp = NULL; 1031 rval = DDI_FAILURE; 1032 } else { 1033 *resultp = instance->dip; 1034 rval = DDI_SUCCESS; 1035 } 1036 break; 1037 case DDI_INFO_DEVT2INSTANCE: 1038 *resultp = (void *)(intptr_t) 1039 (MINOR2INST(getminor((dev_t)arg))); 1040 rval = DDI_SUCCESS; 1041 break; 1042 default: 1043 *resultp = NULL; 1044 rval = DDI_FAILURE; 1045 } 1046 1047 return (rval); 1048 } 1049 1050 /* 1051 * detach - detaches a device from the system 1052 * @dip: pointer to the device's dev_info structure 1053 * @cmd: type of detach 1054 * 1055 * A driver's detach() entry point is called to detach an instance of a device 1056 * that is bound to the driver. The entry point is called with the instance of 1057 * the device node to be detached and with DDI_DETACH, which is specified as 1058 * the cmd argument to the entry point. 1059 * This routine is called during driver unload. We free all the allocated 1060 * resources and call the corresponding LLD so that it can also release all 1061 * its resources. 1062 */ 1063 static int 1064 mrsas_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 1065 { 1066 int instance_no; 1067 1068 struct mrsas_instance *instance; 1069 1070 con_log(CL_ANN, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1071 1072 1073 /* CONSTCOND */ 1074 ASSERT(NO_COMPETING_THREADS); 1075 1076 instance_no = ddi_get_instance(dip); 1077 1078 instance = (struct mrsas_instance *)ddi_get_soft_state(mrsas_state, 1079 instance_no); 1080 1081 if (!instance) { 1082 cmn_err(CE_WARN, 1083 "mr_sas:%d could not get instance in detach", 1084 instance_no); 1085 1086 return (DDI_FAILURE); 1087 } 1088 1089 con_log(CL_ANN, (CE_NOTE, 1090 "mr_sas%d: detaching device 0x%4x:0x%4x:0x%4x:0x%4x", 1091 instance_no, instance->vendor_id, instance->device_id, 1092 instance->subsysvid, instance->subsysid)); 1093 1094 switch (cmd) { 1095 case DDI_DETACH: 1096 con_log(CL_ANN, (CE_NOTE, 1097 "mrsas_detach: DDI_DETACH")); 1098 1099 mutex_enter(&instance->config_dev_mtx); 1100 if (instance->timeout_id != (timeout_id_t)-1) { 1101 mutex_exit(&instance->config_dev_mtx); 1102 (void) untimeout(instance->timeout_id); 1103 instance->timeout_id = (timeout_id_t)-1; 1104 mutex_enter(&instance->config_dev_mtx); 1105 instance->unroll.timer = 0; 1106 } 1107 mutex_exit(&instance->config_dev_mtx); 1108 1109 if (instance->unroll.tranSetup == 1) { 1110 if (scsi_hba_detach(dip) != DDI_SUCCESS) { 1111 cmn_err(CE_WARN, 1112 "mr_sas2%d: failed to detach", 1113 instance_no); 1114 return (DDI_FAILURE); 1115 } 1116 instance->unroll.tranSetup = 0; 1117 con_log(CL_ANN1, 1118 (CE_CONT, "scsi_hba_dettach() done.")); 1119 } 1120 1121 flush_cache(instance); 1122 1123 mrsas_undo_resources(dip, instance); 1124 1125 mrsas_fm_fini(instance); 1126 1127 pci_config_teardown(&instance->pci_handle); 1128 ddi_soft_state_free(mrsas_state, instance_no); 1129 break; 1130 1131 case DDI_PM_SUSPEND: 1132 con_log(CL_ANN, (CE_NOTE, 1133 "mrsas_detach: DDI_PM_SUSPEND")); 1134 1135 break; 1136 case DDI_SUSPEND: 1137 con_log(CL_ANN, (CE_NOTE, 1138 "mrsas_detach: DDI_SUSPEND")); 1139 1140 break; 1141 default: 1142 con_log(CL_ANN, (CE_WARN, 1143 "invalid detach command:0x%x", cmd)); 1144 return (DDI_FAILURE); 1145 } 1146 1147 return (DDI_SUCCESS); 1148 } 1149 1150 1151 static void 1152 mrsas_undo_resources(dev_info_t *dip, struct mrsas_instance *instance) 1153 { 1154 int instance_no; 1155 1156 con_log(CL_ANN, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1157 1158 1159 instance_no = ddi_get_instance(dip); 1160 1161 1162 if (instance->unroll.ioctl == 1) { 1163 ddi_remove_minor_node(dip, instance->iocnode); 1164 instance->unroll.ioctl = 0; 1165 } 1166 1167 if (instance->unroll.scsictl == 1) { 1168 ddi_remove_minor_node(dip, "scsi"); 1169 instance->unroll.scsictl = 0; 1170 } 1171 1172 if (instance->unroll.devctl == 1) { 1173 ddi_remove_minor_node(dip, "devctl"); 1174 instance->unroll.devctl = 0; 1175 } 1176 1177 if (instance->unroll.tranSetup == 1) { 1178 if (scsi_hba_detach(dip) != DDI_SUCCESS) { 1179 cmn_err(CE_WARN, 1180 "mr_sas2%d: failed to detach", instance_no); 1181 return; /* DDI_FAILURE */ 1182 } 1183 instance->unroll.tranSetup = 0; 1184 con_log(CL_ANN1, (CE_CONT, "scsi_hba_dettach() done.")); 1185 } 1186 1187 if (instance->unroll.tran == 1) { 1188 scsi_hba_tran_free(instance->tran); 1189 instance->unroll.tran = 0; 1190 con_log(CL_ANN1, (CE_CONT, "scsi_hba_tran_free() done.")); 1191 } 1192 1193 if (instance->unroll.syncCmd == 1) { 1194 if (instance->tbolt) { 1195 if (abort_syncmap_cmd(instance, 1196 instance->map_update_cmd)) { 1197 cmn_err(CE_WARN, "mrsas_detach: " 1198 "failed to abort previous syncmap command"); 1199 } 1200 1201 instance->unroll.syncCmd = 0; 1202 con_log(CL_ANN1, (CE_CONT, "sync cmd aborted, done.")); 1203 } 1204 } 1205 1206 if (instance->unroll.aenPend == 1) { 1207 if (abort_aen_cmd(instance, instance->aen_cmd)) 1208 cmn_err(CE_WARN, "mrsas_detach: " 1209 "failed to abort prevous AEN command"); 1210 1211 instance->unroll.aenPend = 0; 1212 con_log(CL_ANN1, (CE_CONT, "aen cmd aborted, done.")); 1213 /* This means the controller is fully initialized and running */ 1214 /* Shutdown should be a last command to controller. */ 1215 /* shutdown_controller(); */ 1216 } 1217 1218 1219 if (instance->unroll.timer == 1) { 1220 if (instance->timeout_id != (timeout_id_t)-1) { 1221 (void) untimeout(instance->timeout_id); 1222 instance->timeout_id = (timeout_id_t)-1; 1223 1224 instance->unroll.timer = 0; 1225 } 1226 } 1227 1228 instance->func_ptr->disable_intr(instance); 1229 1230 1231 if (instance->unroll.mutexs == 1) { 1232 mutex_destroy(&instance->cmd_pool_mtx); 1233 mutex_destroy(&instance->app_cmd_pool_mtx); 1234 mutex_destroy(&instance->cmd_pend_mtx); 1235 mutex_destroy(&instance->completed_pool_mtx); 1236 mutex_destroy(&instance->sync_map_mtx); 1237 mutex_destroy(&instance->int_cmd_mtx); 1238 cv_destroy(&instance->int_cmd_cv); 1239 mutex_destroy(&instance->config_dev_mtx); 1240 mutex_destroy(&instance->ocr_flags_mtx); 1241 mutex_destroy(&instance->reg_write_mtx); 1242 1243 if (instance->tbolt) { 1244 mutex_destroy(&instance->cmd_app_pool_mtx); 1245 mutex_destroy(&instance->chip_mtx); 1246 } 1247 1248 instance->unroll.mutexs = 0; 1249 con_log(CL_ANN1, (CE_CONT, "Destroy mutex & cv, done.")); 1250 } 1251 1252 1253 if (instance->unroll.soft_isr == 1) { 1254 ddi_remove_softintr(instance->soft_intr_id); 1255 instance->unroll.soft_isr = 0; 1256 } 1257 1258 if (instance->unroll.intr == 1) { 1259 mrsas_rem_intrs(instance); 1260 instance->unroll.intr = 0; 1261 } 1262 1263 1264 if (instance->unroll.taskq == 1) { 1265 if (instance->taskq) { 1266 ddi_taskq_destroy(instance->taskq); 1267 instance->unroll.taskq = 0; 1268 } 1269 1270 } 1271 1272 /* 1273 * free dma memory allocated for 1274 * cmds/frames/queues/driver version etc 1275 */ 1276 if (instance->unroll.verBuff == 1) { 1277 (void) mrsas_free_dma_obj(instance, instance->drv_ver_dma_obj); 1278 instance->unroll.verBuff = 0; 1279 } 1280 1281 if (instance->unroll.pdlist_buff == 1) { 1282 if (instance->mr_tbolt_pd_list != NULL) { 1283 kmem_free(instance->mr_tbolt_pd_list, 1284 MRSAS_TBOLT_GET_PD_MAX(instance) * 1285 sizeof (struct mrsas_tbolt_pd)); 1286 } 1287 1288 instance->mr_tbolt_pd_list = NULL; 1289 instance->unroll.pdlist_buff = 0; 1290 } 1291 1292 if (instance->unroll.ldlist_buff == 1) { 1293 if (instance->mr_ld_list != NULL) { 1294 kmem_free(instance->mr_ld_list, MRDRV_MAX_LD 1295 * sizeof (struct mrsas_ld)); 1296 } 1297 1298 instance->mr_ld_list = NULL; 1299 instance->unroll.ldlist_buff = 0; 1300 } 1301 1302 if (instance->tbolt) { 1303 if (instance->unroll.alloc_space_mpi2 == 1) { 1304 free_space_for_mpi2(instance); 1305 instance->unroll.alloc_space_mpi2 = 0; 1306 } 1307 } else { 1308 if (instance->unroll.alloc_space_mfi == 1) { 1309 free_space_for_mfi(instance); 1310 instance->unroll.alloc_space_mfi = 0; 1311 } 1312 } 1313 1314 if (instance->unroll.regs == 1) { 1315 ddi_regs_map_free(&instance->regmap_handle); 1316 instance->unroll.regs = 0; 1317 con_log(CL_ANN1, (CE_CONT, "ddi_regs_map_free() done.")); 1318 } 1319 } 1320 1321 1322 1323 /* 1324 * ************************************************************************** * 1325 * * 1326 * common entry points - for character driver types * 1327 * * 1328 * ************************************************************************** * 1329 */ 1330 /* 1331 * open - gets access to a device 1332 * @dev: 1333 * @openflags: 1334 * @otyp: 1335 * @credp: 1336 * 1337 * Access to a device by one or more application programs is controlled 1338 * through the open() and close() entry points. The primary function of 1339 * open() is to verify that the open request is allowed. 1340 */ 1341 static int 1342 mrsas_open(dev_t *dev, int openflags, int otyp, cred_t *credp) 1343 { 1344 int rval = 0; 1345 1346 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1347 1348 /* Check root permissions */ 1349 if (drv_priv(credp) != 0) { 1350 con_log(CL_ANN, (CE_WARN, 1351 "mr_sas: Non-root ioctl access denied!")); 1352 return (EPERM); 1353 } 1354 1355 /* Verify we are being opened as a character device */ 1356 if (otyp != OTYP_CHR) { 1357 con_log(CL_ANN, (CE_WARN, 1358 "mr_sas: ioctl node must be a char node")); 1359 return (EINVAL); 1360 } 1361 1362 if (ddi_get_soft_state(mrsas_state, MINOR2INST(getminor(*dev))) 1363 == NULL) { 1364 return (ENXIO); 1365 } 1366 1367 if (scsi_hba_open) { 1368 rval = scsi_hba_open(dev, openflags, otyp, credp); 1369 } 1370 1371 return (rval); 1372 } 1373 1374 /* 1375 * close - gives up access to a device 1376 * @dev: 1377 * @openflags: 1378 * @otyp: 1379 * @credp: 1380 * 1381 * close() should perform any cleanup necessary to finish using the minor 1382 * device, and prepare the device (and driver) to be opened again. 1383 */ 1384 static int 1385 mrsas_close(dev_t dev, int openflags, int otyp, cred_t *credp) 1386 { 1387 int rval = 0; 1388 1389 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1390 1391 /* no need for locks! */ 1392 1393 if (scsi_hba_close) { 1394 rval = scsi_hba_close(dev, openflags, otyp, credp); 1395 } 1396 1397 return (rval); 1398 } 1399 1400 /* 1401 * ioctl - performs a range of I/O commands for character drivers 1402 * @dev: 1403 * @cmd: 1404 * @arg: 1405 * @mode: 1406 * @credp: 1407 * @rvalp: 1408 * 1409 * ioctl() routine must make sure that user data is copied into or out of the 1410 * kernel address space explicitly using copyin(), copyout(), ddi_copyin(), 1411 * and ddi_copyout(), as appropriate. 1412 * This is a wrapper routine to serialize access to the actual ioctl routine. 1413 * ioctl() should return 0 on success, or the appropriate error number. The 1414 * driver may also set the value returned to the calling process through rvalp. 1415 */ 1416 1417 static int 1418 mrsas_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, 1419 int *rvalp) 1420 { 1421 int rval = 0; 1422 1423 struct mrsas_instance *instance; 1424 struct mrsas_ioctl *ioctl; 1425 struct mrsas_aen aen; 1426 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1427 1428 instance = ddi_get_soft_state(mrsas_state, MINOR2INST(getminor(dev))); 1429 1430 if (instance == NULL) { 1431 /* invalid minor number */ 1432 con_log(CL_ANN, (CE_WARN, "mr_sas: adapter not found.")); 1433 return (ENXIO); 1434 } 1435 1436 ioctl = (struct mrsas_ioctl *)kmem_zalloc(sizeof (struct mrsas_ioctl), 1437 KM_SLEEP); 1438 ASSERT(ioctl); 1439 1440 switch ((uint_t)cmd) { 1441 case MRSAS_IOCTL_FIRMWARE: 1442 if (ddi_copyin((void *)arg, ioctl, 1443 sizeof (struct mrsas_ioctl), mode)) { 1444 con_log(CL_ANN, (CE_WARN, "mrsas_ioctl: " 1445 "ERROR IOCTL copyin")); 1446 kmem_free(ioctl, sizeof (struct mrsas_ioctl)); 1447 return (EFAULT); 1448 } 1449 1450 if (ioctl->control_code == MRSAS_DRIVER_IOCTL_COMMON) { 1451 rval = handle_drv_ioctl(instance, ioctl, mode); 1452 } else { 1453 rval = handle_mfi_ioctl(instance, ioctl, mode); 1454 } 1455 1456 if (ddi_copyout((void *)ioctl, (void *)arg, 1457 (sizeof (struct mrsas_ioctl) - 1), mode)) { 1458 con_log(CL_ANN, (CE_WARN, 1459 "mrsas_ioctl: copy_to_user failed")); 1460 rval = 1; 1461 } 1462 1463 break; 1464 case MRSAS_IOCTL_AEN: 1465 if (ddi_copyin((void *) arg, &aen, 1466 sizeof (struct mrsas_aen), mode)) { 1467 con_log(CL_ANN, (CE_WARN, 1468 "mrsas_ioctl: ERROR AEN copyin")); 1469 kmem_free(ioctl, sizeof (struct mrsas_ioctl)); 1470 return (EFAULT); 1471 } 1472 1473 rval = handle_mfi_aen(instance, &aen); 1474 1475 if (ddi_copyout((void *) &aen, (void *)arg, 1476 sizeof (struct mrsas_aen), mode)) { 1477 con_log(CL_ANN, (CE_WARN, 1478 "mrsas_ioctl: copy_to_user failed")); 1479 rval = 1; 1480 } 1481 1482 break; 1483 default: 1484 rval = scsi_hba_ioctl(dev, cmd, arg, 1485 mode, credp, rvalp); 1486 1487 con_log(CL_DLEVEL1, (CE_NOTE, "mrsas_ioctl: " 1488 "scsi_hba_ioctl called, ret = %x.", rval)); 1489 } 1490 1491 kmem_free(ioctl, sizeof (struct mrsas_ioctl)); 1492 return (rval); 1493 } 1494 1495 /* 1496 * ************************************************************************** * 1497 * * 1498 * common entry points - for block driver types * 1499 * * 1500 * ************************************************************************** * 1501 */ 1502 #ifdef __sparc 1503 /* 1504 * reset - TBD 1505 * @dip: 1506 * @cmd: 1507 * 1508 * TBD 1509 */ 1510 /*ARGSUSED*/ 1511 static int 1512 mrsas_reset(dev_info_t *dip, ddi_reset_cmd_t cmd) 1513 { 1514 int instance_no; 1515 1516 struct mrsas_instance *instance; 1517 1518 instance_no = ddi_get_instance(dip); 1519 instance = (struct mrsas_instance *)ddi_get_soft_state 1520 (mrsas_state, instance_no); 1521 1522 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1523 1524 if (!instance) { 1525 con_log(CL_ANN, (CE_WARN, "mr_sas:%d could not get adapter " 1526 "in reset", instance_no)); 1527 return (DDI_FAILURE); 1528 } 1529 1530 instance->func_ptr->disable_intr(instance); 1531 1532 con_log(CL_ANN1, (CE_CONT, "flushing cache for instance %d", 1533 instance_no)); 1534 1535 flush_cache(instance); 1536 1537 return (DDI_SUCCESS); 1538 } 1539 #else /* __sparc */ 1540 /*ARGSUSED*/ 1541 static int 1542 mrsas_quiesce(dev_info_t *dip) 1543 { 1544 int instance_no; 1545 1546 struct mrsas_instance *instance; 1547 1548 instance_no = ddi_get_instance(dip); 1549 instance = (struct mrsas_instance *)ddi_get_soft_state 1550 (mrsas_state, instance_no); 1551 1552 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1553 1554 if (!instance) { 1555 con_log(CL_ANN1, (CE_WARN, "mr_sas:%d could not get adapter " 1556 "in quiesce", instance_no)); 1557 return (DDI_FAILURE); 1558 } 1559 if (instance->deadadapter || instance->adapterresetinprogress) { 1560 con_log(CL_ANN1, (CE_WARN, "mr_sas:%d adapter is not in " 1561 "healthy state", instance_no)); 1562 return (DDI_FAILURE); 1563 } 1564 1565 if (abort_aen_cmd(instance, instance->aen_cmd)) { 1566 con_log(CL_ANN1, (CE_WARN, "mrsas_quiesce: " 1567 "failed to abort prevous AEN command QUIESCE")); 1568 } 1569 1570 if (instance->tbolt) { 1571 if (abort_syncmap_cmd(instance, 1572 instance->map_update_cmd)) { 1573 cmn_err(CE_WARN, 1574 "mrsas_detach: failed to abort " 1575 "previous syncmap command"); 1576 return (DDI_FAILURE); 1577 } 1578 } 1579 1580 instance->func_ptr->disable_intr(instance); 1581 1582 con_log(CL_ANN1, (CE_CONT, "flushing cache for instance %d", 1583 instance_no)); 1584 1585 flush_cache(instance); 1586 1587 if (wait_for_outstanding(instance)) { 1588 con_log(CL_ANN1, 1589 (CE_CONT, "wait_for_outstanding: return FAIL.\n")); 1590 return (DDI_FAILURE); 1591 } 1592 return (DDI_SUCCESS); 1593 } 1594 #endif /* __sparc */ 1595 1596 /* 1597 * ************************************************************************** * 1598 * * 1599 * entry points (SCSI HBA) * 1600 * * 1601 * ************************************************************************** * 1602 */ 1603 /* 1604 * tran_tgt_init - initialize a target device instance 1605 * @hba_dip: 1606 * @tgt_dip: 1607 * @tran: 1608 * @sd: 1609 * 1610 * The tran_tgt_init() entry point enables the HBA to allocate and initialize 1611 * any per-target resources. tran_tgt_init() also enables the HBA to qualify 1612 * the device's address as valid and supportable for that particular HBA. 1613 * By returning DDI_FAILURE, the instance of the target driver for that device 1614 * is not probed or attached. 1615 */ 1616 /*ARGSUSED*/ 1617 static int 1618 mrsas_tran_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip, 1619 scsi_hba_tran_t *tran, struct scsi_device *sd) 1620 { 1621 struct mrsas_instance *instance; 1622 uint16_t tgt = sd->sd_address.a_target; 1623 uint8_t lun = sd->sd_address.a_lun; 1624 dev_info_t *child = NULL; 1625 1626 con_log(CL_DLEVEL2, (CE_NOTE, "mrsas_tgt_init target %d lun %d", 1627 tgt, lun)); 1628 1629 instance = ADDR2MR(&sd->sd_address); 1630 1631 if (ndi_dev_is_persistent_node(tgt_dip) == 0) { 1632 /* 1633 * If no persistent node exists, we don't allow .conf node 1634 * to be created. 1635 */ 1636 if ((child = mrsas_find_child(instance, tgt, lun)) != NULL) { 1637 con_log(CL_DLEVEL2, 1638 (CE_NOTE, "mrsas_tgt_init find child =" 1639 " %p t = %d l = %d", (void *)child, tgt, lun)); 1640 if (ndi_merge_node(tgt_dip, mrsas_name_node) != 1641 DDI_SUCCESS) 1642 /* Create this .conf node */ 1643 return (DDI_SUCCESS); 1644 } 1645 con_log(CL_DLEVEL2, (CE_NOTE, "mrsas_tgt_init in ndi_per " 1646 "DDI_FAILURE t = %d l = %d", tgt, lun)); 1647 return (DDI_FAILURE); 1648 1649 } 1650 1651 con_log(CL_DLEVEL2, (CE_NOTE, "mrsas_tgt_init dev_dip %p tgt_dip %p", 1652 (void *)instance->mr_ld_list[tgt].dip, (void *)tgt_dip)); 1653 1654 if (tgt < MRDRV_MAX_LD && lun == 0) { 1655 if (instance->mr_ld_list[tgt].dip == NULL && 1656 strcmp(ddi_driver_name(sd->sd_dev), "sd") == 0) { 1657 mutex_enter(&instance->config_dev_mtx); 1658 instance->mr_ld_list[tgt].dip = tgt_dip; 1659 instance->mr_ld_list[tgt].lun_type = MRSAS_LD_LUN; 1660 instance->mr_ld_list[tgt].flag = MRDRV_TGT_VALID; 1661 mutex_exit(&instance->config_dev_mtx); 1662 } 1663 } 1664 1665 #ifdef PDSUPPORT 1666 else if (instance->tbolt) { 1667 if (instance->mr_tbolt_pd_list[tgt].dip == NULL) { 1668 mutex_enter(&instance->config_dev_mtx); 1669 instance->mr_tbolt_pd_list[tgt].dip = tgt_dip; 1670 instance->mr_tbolt_pd_list[tgt].flag = 1671 MRDRV_TGT_VALID; 1672 mutex_exit(&instance->config_dev_mtx); 1673 con_log(CL_ANN1, (CE_NOTE, "mrsas_tran_tgt_init:" 1674 "t%xl%x", tgt, lun)); 1675 } 1676 } 1677 #endif 1678 1679 return (DDI_SUCCESS); 1680 } 1681 1682 /*ARGSUSED*/ 1683 static void 1684 mrsas_tran_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip, 1685 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 1686 { 1687 struct mrsas_instance *instance; 1688 int tgt = sd->sd_address.a_target; 1689 int lun = sd->sd_address.a_lun; 1690 1691 instance = ADDR2MR(&sd->sd_address); 1692 1693 con_log(CL_DLEVEL2, (CE_NOTE, "tgt_free t = %d l = %d", tgt, lun)); 1694 1695 if (tgt < MRDRV_MAX_LD && lun == 0) { 1696 if (instance->mr_ld_list[tgt].dip == tgt_dip) { 1697 mutex_enter(&instance->config_dev_mtx); 1698 instance->mr_ld_list[tgt].dip = NULL; 1699 mutex_exit(&instance->config_dev_mtx); 1700 } 1701 } 1702 1703 #ifdef PDSUPPORT 1704 else if (instance->tbolt) { 1705 mutex_enter(&instance->config_dev_mtx); 1706 instance->mr_tbolt_pd_list[tgt].dip = NULL; 1707 mutex_exit(&instance->config_dev_mtx); 1708 con_log(CL_ANN1, (CE_NOTE, "tgt_free: Setting dip = NULL" 1709 "for tgt:%x", tgt)); 1710 } 1711 #endif 1712 1713 } 1714 1715 dev_info_t * 1716 mrsas_find_child(struct mrsas_instance *instance, uint16_t tgt, uint8_t lun) 1717 { 1718 dev_info_t *child = NULL; 1719 char addr[SCSI_MAXNAMELEN]; 1720 char tmp[MAXNAMELEN]; 1721 1722 (void) sprintf(addr, "%x,%x", tgt, lun); 1723 for (child = ddi_get_child(instance->dip); child; 1724 child = ddi_get_next_sibling(child)) { 1725 1726 if (ndi_dev_is_persistent_node(child) == 0) { 1727 continue; 1728 } 1729 1730 if (mrsas_name_node(child, tmp, MAXNAMELEN) != 1731 DDI_SUCCESS) { 1732 continue; 1733 } 1734 1735 if (strcmp(addr, tmp) == 0) { 1736 break; 1737 } 1738 } 1739 con_log(CL_DLEVEL2, (CE_NOTE, "mrsas_find_child: return child = %p", 1740 (void *)child)); 1741 return (child); 1742 } 1743 1744 /* 1745 * mrsas_name_node - 1746 * @dip: 1747 * @name: 1748 * @len: 1749 */ 1750 static int 1751 mrsas_name_node(dev_info_t *dip, char *name, int len) 1752 { 1753 int tgt, lun; 1754 1755 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 1756 DDI_PROP_DONTPASS, "target", -1); 1757 con_log(CL_DLEVEL2, (CE_NOTE, 1758 "mrsas_name_node: dip %p tgt %d", (void *)dip, tgt)); 1759 if (tgt == -1) { 1760 return (DDI_FAILURE); 1761 } 1762 lun = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 1763 "lun", -1); 1764 con_log(CL_DLEVEL2, 1765 (CE_NOTE, "mrsas_name_node: tgt %d lun %d", tgt, lun)); 1766 if (lun == -1) { 1767 return (DDI_FAILURE); 1768 } 1769 (void) snprintf(name, len, "%x,%x", tgt, lun); 1770 return (DDI_SUCCESS); 1771 } 1772 1773 /* 1774 * tran_init_pkt - allocate & initialize a scsi_pkt structure 1775 * @ap: 1776 * @pkt: 1777 * @bp: 1778 * @cmdlen: 1779 * @statuslen: 1780 * @tgtlen: 1781 * @flags: 1782 * @callback: 1783 * 1784 * The tran_init_pkt() entry point allocates and initializes a scsi_pkt 1785 * structure and DMA resources for a target driver request. The 1786 * tran_init_pkt() entry point is called when the target driver calls the 1787 * SCSA function scsi_init_pkt(). Each call of the tran_init_pkt() entry point 1788 * is a request to perform one or more of three possible services: 1789 * - allocation and initialization of a scsi_pkt structure 1790 * - allocation of DMA resources for data transfer 1791 * - reallocation of DMA resources for the next portion of the data transfer 1792 */ 1793 static struct scsi_pkt * 1794 mrsas_tran_init_pkt(struct scsi_address *ap, register struct scsi_pkt *pkt, 1795 struct buf *bp, int cmdlen, int statuslen, int tgtlen, 1796 int flags, int (*callback)(), caddr_t arg) 1797 { 1798 struct scsa_cmd *acmd; 1799 struct mrsas_instance *instance; 1800 struct scsi_pkt *new_pkt; 1801 1802 con_log(CL_DLEVEL1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1803 1804 instance = ADDR2MR(ap); 1805 1806 /* step #1 : pkt allocation */ 1807 if (pkt == NULL) { 1808 pkt = scsi_hba_pkt_alloc(instance->dip, ap, cmdlen, statuslen, 1809 tgtlen, sizeof (struct scsa_cmd), callback, arg); 1810 if (pkt == NULL) { 1811 return (NULL); 1812 } 1813 1814 acmd = PKT2CMD(pkt); 1815 1816 /* 1817 * Initialize the new pkt - we redundantly initialize 1818 * all the fields for illustrative purposes. 1819 */ 1820 acmd->cmd_pkt = pkt; 1821 acmd->cmd_flags = 0; 1822 acmd->cmd_scblen = statuslen; 1823 acmd->cmd_cdblen = cmdlen; 1824 acmd->cmd_dmahandle = NULL; 1825 acmd->cmd_ncookies = 0; 1826 acmd->cmd_cookie = 0; 1827 acmd->cmd_cookiecnt = 0; 1828 acmd->cmd_nwin = 0; 1829 1830 pkt->pkt_address = *ap; 1831 pkt->pkt_comp = (void (*)())NULL; 1832 pkt->pkt_flags = 0; 1833 pkt->pkt_time = 0; 1834 pkt->pkt_resid = 0; 1835 pkt->pkt_state = 0; 1836 pkt->pkt_statistics = 0; 1837 pkt->pkt_reason = 0; 1838 new_pkt = pkt; 1839 } else { 1840 acmd = PKT2CMD(pkt); 1841 new_pkt = NULL; 1842 } 1843 1844 /* step #2 : dma allocation/move */ 1845 if (bp && bp->b_bcount != 0) { 1846 if (acmd->cmd_dmahandle == NULL) { 1847 if (mrsas_dma_alloc(instance, pkt, bp, flags, 1848 callback) == DDI_FAILURE) { 1849 if (new_pkt) { 1850 scsi_hba_pkt_free(ap, new_pkt); 1851 } 1852 return ((struct scsi_pkt *)NULL); 1853 } 1854 } else { 1855 if (mrsas_dma_move(instance, pkt, bp) == DDI_FAILURE) { 1856 return ((struct scsi_pkt *)NULL); 1857 } 1858 } 1859 } 1860 1861 return (pkt); 1862 } 1863 1864 /* 1865 * tran_start - transport a SCSI command to the addressed target 1866 * @ap: 1867 * @pkt: 1868 * 1869 * The tran_start() entry point for a SCSI HBA driver is called to transport a 1870 * SCSI command to the addressed target. The SCSI command is described 1871 * entirely within the scsi_pkt structure, which the target driver allocated 1872 * through the HBA driver's tran_init_pkt() entry point. If the command 1873 * involves a data transfer, DMA resources must also have been allocated for 1874 * the scsi_pkt structure. 1875 * 1876 * Return Values : 1877 * TRAN_BUSY - request queue is full, no more free scbs 1878 * TRAN_ACCEPT - pkt has been submitted to the instance 1879 */ 1880 static int 1881 mrsas_tran_start(struct scsi_address *ap, register struct scsi_pkt *pkt) 1882 { 1883 uchar_t cmd_done = 0; 1884 1885 struct mrsas_instance *instance = ADDR2MR(ap); 1886 struct mrsas_cmd *cmd; 1887 1888 con_log(CL_DLEVEL1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 1889 if (instance->deadadapter == 1) { 1890 con_log(CL_ANN1, (CE_WARN, 1891 "mrsas_tran_start: return TRAN_FATAL_ERROR " 1892 "for IO, as the HBA doesnt take any more IOs")); 1893 if (pkt) { 1894 pkt->pkt_reason = CMD_DEV_GONE; 1895 pkt->pkt_statistics = STAT_DISCON; 1896 } 1897 return (TRAN_FATAL_ERROR); 1898 } 1899 1900 if (instance->adapterresetinprogress) { 1901 con_log(CL_ANN1, (CE_NOTE, "mrsas_tran_start: Reset flag set, " 1902 "returning mfi_pkt and setting TRAN_BUSY\n")); 1903 return (TRAN_BUSY); 1904 } 1905 1906 con_log(CL_ANN1, (CE_CONT, "chkpnt:%s:%d:SCSI CDB[0]=0x%x time:%x", 1907 __func__, __LINE__, pkt->pkt_cdbp[0], pkt->pkt_time)); 1908 1909 pkt->pkt_reason = CMD_CMPLT; 1910 *pkt->pkt_scbp = STATUS_GOOD; /* clear arq scsi_status */ 1911 1912 cmd = build_cmd(instance, ap, pkt, &cmd_done); 1913 1914 /* 1915 * Check if the command is already completed by the mrsas_build_cmd() 1916 * routine. In which case the busy_flag would be clear and scb will be 1917 * NULL and appropriate reason provided in pkt_reason field 1918 */ 1919 if (cmd_done) { 1920 pkt->pkt_reason = CMD_CMPLT; 1921 pkt->pkt_scbp[0] = STATUS_GOOD; 1922 pkt->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET 1923 | STATE_SENT_CMD; 1924 if (((pkt->pkt_flags & FLAG_NOINTR) == 0) && pkt->pkt_comp) { 1925 (*pkt->pkt_comp)(pkt); 1926 } 1927 1928 return (TRAN_ACCEPT); 1929 } 1930 1931 if (cmd == NULL) { 1932 return (TRAN_BUSY); 1933 } 1934 1935 if ((pkt->pkt_flags & FLAG_NOINTR) == 0) { 1936 if (instance->fw_outstanding > instance->max_fw_cmds) { 1937 con_log(CL_ANN, (CE_CONT, "mr_sas:Firmware busy")); 1938 DTRACE_PROBE2(start_tran_err, 1939 uint16_t, instance->fw_outstanding, 1940 uint16_t, instance->max_fw_cmds); 1941 return_mfi_pkt(instance, cmd); 1942 return (TRAN_BUSY); 1943 } 1944 1945 /* Synchronize the Cmd frame for the controller */ 1946 (void) ddi_dma_sync(cmd->frame_dma_obj.dma_handle, 0, 0, 1947 DDI_DMA_SYNC_FORDEV); 1948 con_log(CL_ANN, (CE_CONT, "issue_cmd_ppc: SCSI CDB[0]=0x%x" 1949 "cmd->index:%x\n", pkt->pkt_cdbp[0], cmd->index)); 1950 instance->func_ptr->issue_cmd(cmd, instance); 1951 1952 } else { 1953 struct mrsas_header *hdr = &cmd->frame->hdr; 1954 1955 instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd); 1956 1957 pkt->pkt_reason = CMD_CMPLT; 1958 pkt->pkt_statistics = 0; 1959 pkt->pkt_state |= STATE_XFERRED_DATA | STATE_GOT_STATUS; 1960 1961 switch (ddi_get8(cmd->frame_dma_obj.acc_handle, 1962 &hdr->cmd_status)) { 1963 case MFI_STAT_OK: 1964 pkt->pkt_scbp[0] = STATUS_GOOD; 1965 break; 1966 1967 case MFI_STAT_SCSI_DONE_WITH_ERROR: 1968 con_log(CL_ANN, (CE_CONT, 1969 "mrsas_tran_start: scsi done with error")); 1970 pkt->pkt_reason = CMD_CMPLT; 1971 pkt->pkt_statistics = 0; 1972 1973 ((struct scsi_status *)pkt->pkt_scbp)->sts_chk = 1; 1974 break; 1975 1976 case MFI_STAT_DEVICE_NOT_FOUND: 1977 con_log(CL_ANN, (CE_CONT, 1978 "mrsas_tran_start: device not found error")); 1979 pkt->pkt_reason = CMD_DEV_GONE; 1980 pkt->pkt_statistics = STAT_DISCON; 1981 break; 1982 1983 default: 1984 ((struct scsi_status *)pkt->pkt_scbp)->sts_busy = 1; 1985 } 1986 1987 (void) mrsas_common_check(instance, cmd); 1988 DTRACE_PROBE2(start_nointr_done, uint8_t, hdr->cmd, 1989 uint8_t, hdr->cmd_status); 1990 return_mfi_pkt(instance, cmd); 1991 1992 if (pkt->pkt_comp) { 1993 (*pkt->pkt_comp)(pkt); 1994 } 1995 1996 } 1997 1998 return (TRAN_ACCEPT); 1999 } 2000 2001 /* 2002 * tran_abort - Abort any commands that are currently in transport 2003 * @ap: 2004 * @pkt: 2005 * 2006 * The tran_abort() entry point for a SCSI HBA driver is called to abort any 2007 * commands that are currently in transport for a particular target. This entry 2008 * point is called when a target driver calls scsi_abort(). The tran_abort() 2009 * entry point should attempt to abort the command denoted by the pkt 2010 * parameter. If the pkt parameter is NULL, tran_abort() should attempt to 2011 * abort all outstanding commands in the transport layer for the particular 2012 * target or logical unit. 2013 */ 2014 /*ARGSUSED*/ 2015 static int 2016 mrsas_tran_abort(struct scsi_address *ap, struct scsi_pkt *pkt) 2017 { 2018 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 2019 2020 /* abort command not supported by H/W */ 2021 2022 return (DDI_FAILURE); 2023 } 2024 2025 /* 2026 * tran_reset - reset either the SCSI bus or target 2027 * @ap: 2028 * @level: 2029 * 2030 * The tran_reset() entry point for a SCSI HBA driver is called to reset either 2031 * the SCSI bus or a particular SCSI target device. This entry point is called 2032 * when a target driver calls scsi_reset(). The tran_reset() entry point must 2033 * reset the SCSI bus if level is RESET_ALL. If level is RESET_TARGET, just the 2034 * particular target or logical unit must be reset. 2035 */ 2036 /*ARGSUSED*/ 2037 static int 2038 mrsas_tran_reset(struct scsi_address *ap, int level) 2039 { 2040 struct mrsas_instance *instance = ADDR2MR(ap); 2041 2042 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 2043 2044 if (wait_for_outstanding(instance)) { 2045 con_log(CL_ANN1, 2046 (CE_CONT, "wait_for_outstanding: return FAIL.\n")); 2047 return (DDI_FAILURE); 2048 } else { 2049 return (DDI_SUCCESS); 2050 } 2051 } 2052 2053 /* 2054 * tran_getcap - get one of a set of SCSA-defined capabilities 2055 * @ap: 2056 * @cap: 2057 * @whom: 2058 * 2059 * The target driver can request the current setting of the capability for a 2060 * particular target by setting the whom parameter to nonzero. A whom value of 2061 * zero indicates a request for the current setting of the general capability 2062 * for the SCSI bus or for adapter hardware. The tran_getcap() should return -1 2063 * for undefined capabilities or the current value of the requested capability. 2064 */ 2065 /*ARGSUSED*/ 2066 static int 2067 mrsas_tran_getcap(struct scsi_address *ap, char *cap, int whom) 2068 { 2069 int rval = 0; 2070 2071 struct mrsas_instance *instance = ADDR2MR(ap); 2072 2073 con_log(CL_DLEVEL2, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 2074 2075 /* we do allow inquiring about capabilities for other targets */ 2076 if (cap == NULL) { 2077 return (-1); 2078 } 2079 2080 switch (scsi_hba_lookup_capstr(cap)) { 2081 case SCSI_CAP_DMA_MAX: 2082 if (instance->tbolt) { 2083 /* Limit to 256k max transfer */ 2084 rval = mrsas_tbolt_max_cap_maxxfer; 2085 } else { 2086 /* Limit to 16MB max transfer */ 2087 rval = mrsas_max_cap_maxxfer; 2088 } 2089 break; 2090 case SCSI_CAP_MSG_OUT: 2091 rval = 1; 2092 break; 2093 case SCSI_CAP_DISCONNECT: 2094 rval = 0; 2095 break; 2096 case SCSI_CAP_SYNCHRONOUS: 2097 rval = 0; 2098 break; 2099 case SCSI_CAP_WIDE_XFER: 2100 rval = 1; 2101 break; 2102 case SCSI_CAP_TAGGED_QING: 2103 rval = 1; 2104 break; 2105 case SCSI_CAP_UNTAGGED_QING: 2106 rval = 1; 2107 break; 2108 case SCSI_CAP_PARITY: 2109 rval = 1; 2110 break; 2111 case SCSI_CAP_INITIATOR_ID: 2112 rval = instance->init_id; 2113 break; 2114 case SCSI_CAP_ARQ: 2115 rval = 1; 2116 break; 2117 case SCSI_CAP_LINKED_CMDS: 2118 rval = 0; 2119 break; 2120 case SCSI_CAP_RESET_NOTIFICATION: 2121 rval = 1; 2122 break; 2123 case SCSI_CAP_GEOMETRY: 2124 rval = -1; 2125 2126 break; 2127 default: 2128 con_log(CL_DLEVEL2, (CE_NOTE, "Default cap coming 0x%x", 2129 scsi_hba_lookup_capstr(cap))); 2130 rval = -1; 2131 break; 2132 } 2133 2134 return (rval); 2135 } 2136 2137 /* 2138 * tran_setcap - set one of a set of SCSA-defined capabilities 2139 * @ap: 2140 * @cap: 2141 * @value: 2142 * @whom: 2143 * 2144 * The target driver might request that the new value be set for a particular 2145 * target by setting the whom parameter to nonzero. A whom value of zero 2146 * means that request is to set the new value for the SCSI bus or for adapter 2147 * hardware in general. 2148 * The tran_setcap() should return the following values as appropriate: 2149 * - -1 for undefined capabilities 2150 * - 0 if the HBA driver cannot set the capability to the requested value 2151 * - 1 if the HBA driver is able to set the capability to the requested value 2152 */ 2153 /*ARGSUSED*/ 2154 static int 2155 mrsas_tran_setcap(struct scsi_address *ap, char *cap, int value, int whom) 2156 { 2157 int rval = 1; 2158 2159 con_log(CL_DLEVEL2, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 2160 2161 /* We don't allow setting capabilities for other targets */ 2162 if (cap == NULL || whom == 0) { 2163 return (-1); 2164 } 2165 2166 switch (scsi_hba_lookup_capstr(cap)) { 2167 case SCSI_CAP_DMA_MAX: 2168 case SCSI_CAP_MSG_OUT: 2169 case SCSI_CAP_PARITY: 2170 case SCSI_CAP_LINKED_CMDS: 2171 case SCSI_CAP_RESET_NOTIFICATION: 2172 case SCSI_CAP_DISCONNECT: 2173 case SCSI_CAP_SYNCHRONOUS: 2174 case SCSI_CAP_UNTAGGED_QING: 2175 case SCSI_CAP_WIDE_XFER: 2176 case SCSI_CAP_INITIATOR_ID: 2177 case SCSI_CAP_ARQ: 2178 /* 2179 * None of these are settable via 2180 * the capability interface. 2181 */ 2182 break; 2183 case SCSI_CAP_TAGGED_QING: 2184 rval = 1; 2185 break; 2186 case SCSI_CAP_SECTOR_SIZE: 2187 rval = 1; 2188 break; 2189 2190 case SCSI_CAP_TOTAL_SECTORS: 2191 rval = 1; 2192 break; 2193 default: 2194 rval = -1; 2195 break; 2196 } 2197 2198 return (rval); 2199 } 2200 2201 /* 2202 * tran_destroy_pkt - deallocate scsi_pkt structure 2203 * @ap: 2204 * @pkt: 2205 * 2206 * The tran_destroy_pkt() entry point is the HBA driver function that 2207 * deallocates scsi_pkt structures. The tran_destroy_pkt() entry point is 2208 * called when the target driver calls scsi_destroy_pkt(). The 2209 * tran_destroy_pkt() entry point must free any DMA resources that have been 2210 * allocated for the packet. An implicit DMA synchronization occurs if the 2211 * DMA resources are freed and any cached data remains after the completion 2212 * of the transfer. 2213 */ 2214 static void 2215 mrsas_tran_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 2216 { 2217 struct scsa_cmd *acmd = PKT2CMD(pkt); 2218 2219 con_log(CL_DLEVEL2, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 2220 2221 if (acmd->cmd_flags & CFLAG_DMAVALID) { 2222 acmd->cmd_flags &= ~CFLAG_DMAVALID; 2223 2224 (void) ddi_dma_unbind_handle(acmd->cmd_dmahandle); 2225 2226 ddi_dma_free_handle(&acmd->cmd_dmahandle); 2227 2228 acmd->cmd_dmahandle = NULL; 2229 } 2230 2231 /* free the pkt */ 2232 scsi_hba_pkt_free(ap, pkt); 2233 } 2234 2235 /* 2236 * tran_dmafree - deallocates DMA resources 2237 * @ap: 2238 * @pkt: 2239 * 2240 * The tran_dmafree() entry point deallocates DMAQ resources that have been 2241 * allocated for a scsi_pkt structure. The tran_dmafree() entry point is 2242 * called when the target driver calls scsi_dmafree(). The tran_dmafree() must 2243 * free only DMA resources allocated for a scsi_pkt structure, not the 2244 * scsi_pkt itself. When DMA resources are freed, a DMA synchronization is 2245 * implicitly performed. 2246 */ 2247 /*ARGSUSED*/ 2248 static void 2249 mrsas_tran_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt) 2250 { 2251 register struct scsa_cmd *acmd = PKT2CMD(pkt); 2252 2253 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 2254 2255 if (acmd->cmd_flags & CFLAG_DMAVALID) { 2256 acmd->cmd_flags &= ~CFLAG_DMAVALID; 2257 2258 (void) ddi_dma_unbind_handle(acmd->cmd_dmahandle); 2259 2260 ddi_dma_free_handle(&acmd->cmd_dmahandle); 2261 2262 acmd->cmd_dmahandle = NULL; 2263 } 2264 } 2265 2266 /* 2267 * tran_sync_pkt - synchronize the DMA object allocated 2268 * @ap: 2269 * @pkt: 2270 * 2271 * The tran_sync_pkt() entry point synchronizes the DMA object allocated for 2272 * the scsi_pkt structure before or after a DMA transfer. The tran_sync_pkt() 2273 * entry point is called when the target driver calls scsi_sync_pkt(). If the 2274 * data transfer direction is a DMA read from device to memory, tran_sync_pkt() 2275 * must synchronize the CPU's view of the data. If the data transfer direction 2276 * is a DMA write from memory to device, tran_sync_pkt() must synchronize the 2277 * device's view of the data. 2278 */ 2279 /*ARGSUSED*/ 2280 static void 2281 mrsas_tran_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 2282 { 2283 register struct scsa_cmd *acmd = PKT2CMD(pkt); 2284 2285 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 2286 2287 if (acmd->cmd_flags & CFLAG_DMAVALID) { 2288 (void) ddi_dma_sync(acmd->cmd_dmahandle, acmd->cmd_dma_offset, 2289 acmd->cmd_dma_len, (acmd->cmd_flags & CFLAG_DMASEND) ? 2290 DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU); 2291 } 2292 } 2293 2294 /*ARGSUSED*/ 2295 static int 2296 mrsas_tran_quiesce(dev_info_t *dip) 2297 { 2298 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 2299 2300 return (1); 2301 } 2302 2303 /*ARGSUSED*/ 2304 static int 2305 mrsas_tran_unquiesce(dev_info_t *dip) 2306 { 2307 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 2308 2309 return (1); 2310 } 2311 2312 2313 /* 2314 * mrsas_isr(caddr_t) 2315 * 2316 * The Interrupt Service Routine 2317 * 2318 * Collect status for all completed commands and do callback 2319 * 2320 */ 2321 static uint_t 2322 mrsas_isr(struct mrsas_instance *instance) 2323 { 2324 int need_softintr; 2325 uint32_t producer; 2326 uint32_t consumer; 2327 uint32_t context; 2328 int retval; 2329 2330 struct mrsas_cmd *cmd; 2331 struct mrsas_header *hdr; 2332 struct scsi_pkt *pkt; 2333 2334 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 2335 ASSERT(instance); 2336 if (instance->tbolt) { 2337 mutex_enter(&instance->chip_mtx); 2338 if ((instance->intr_type == DDI_INTR_TYPE_FIXED) && 2339 !(instance->func_ptr->intr_ack(instance))) { 2340 mutex_exit(&instance->chip_mtx); 2341 return (DDI_INTR_UNCLAIMED); 2342 } 2343 retval = mr_sas_tbolt_process_outstanding_cmd(instance); 2344 mutex_exit(&instance->chip_mtx); 2345 return (retval); 2346 } else { 2347 if ((instance->intr_type == DDI_INTR_TYPE_FIXED) && 2348 !instance->func_ptr->intr_ack(instance)) { 2349 return (DDI_INTR_UNCLAIMED); 2350 } 2351 } 2352 2353 (void) ddi_dma_sync(instance->mfi_internal_dma_obj.dma_handle, 2354 0, 0, DDI_DMA_SYNC_FORCPU); 2355 2356 if (mrsas_check_dma_handle(instance->mfi_internal_dma_obj.dma_handle) 2357 != DDI_SUCCESS) { 2358 mrsas_fm_ereport(instance, DDI_FM_DEVICE_NO_RESPONSE); 2359 ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST); 2360 con_log(CL_ANN1, (CE_WARN, 2361 "mr_sas_isr(): FMA check, returning DDI_INTR_UNCLAIMED")); 2362 return (DDI_INTR_CLAIMED); 2363 } 2364 con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 2365 2366 #ifdef OCRDEBUG 2367 if (debug_consecutive_timeout_after_ocr_g == 1) { 2368 con_log(CL_ANN1, (CE_NOTE, 2369 "simulating consecutive timeout after ocr")); 2370 return (DDI_INTR_CLAIMED); 2371 } 2372 #endif 2373 2374 mutex_enter(&instance->completed_pool_mtx); 2375 mutex_enter(&instance->cmd_pend_mtx); 2376 2377 producer = ddi_get32(instance->mfi_internal_dma_obj.acc_handle, 2378 instance->producer); 2379 consumer = ddi_get32(instance->mfi_internal_dma_obj.acc_handle, 2380 instance->consumer); 2381 2382 con_log(CL_ANN, (CE_CONT, " producer %x consumer %x ", 2383 producer, consumer)); 2384 if (producer == consumer) { 2385 con_log(CL_ANN, (CE_WARN, "producer == consumer case")); 2386 DTRACE_PROBE2(isr_pc_err, uint32_t, producer, 2387 uint32_t, consumer); 2388 mutex_exit(&instance->cmd_pend_mtx); 2389 mutex_exit(&instance->completed_pool_mtx); 2390 return (DDI_INTR_CLAIMED); 2391 } 2392 2393 while (consumer != producer) { 2394 context = ddi_get32(instance->mfi_internal_dma_obj.acc_handle, 2395 &instance->reply_queue[consumer]); 2396 cmd = instance->cmd_list[context]; 2397 2398 if (cmd->sync_cmd == MRSAS_TRUE) { 2399 hdr = (struct mrsas_header *)&cmd->frame->hdr; 2400 if (hdr) { 2401 mlist_del_init(&cmd->list); 2402 } 2403 } else { 2404 pkt = cmd->pkt; 2405 if (pkt) { 2406 mlist_del_init(&cmd->list); 2407 } 2408 } 2409 2410 mlist_add_tail(&cmd->list, &instance->completed_pool_list); 2411 2412 consumer++; 2413 if (consumer == (instance->max_fw_cmds + 1)) { 2414 consumer = 0; 2415 } 2416 } 2417 ddi_put32(instance->mfi_internal_dma_obj.acc_handle, 2418 instance->consumer, consumer); 2419 mutex_exit(&instance->cmd_pend_mtx); 2420 mutex_exit(&instance->completed_pool_mtx); 2421 2422 (void) ddi_dma_sync(instance->mfi_internal_dma_obj.dma_handle, 2423 0, 0, DDI_DMA_SYNC_FORDEV); 2424 2425 if (instance->softint_running) { 2426 need_softintr = 0; 2427 } else { 2428 need_softintr = 1; 2429 } 2430 2431 if (instance->isr_level == HIGH_LEVEL_INTR) { 2432 if (need_softintr) { 2433 ddi_trigger_softintr(instance->soft_intr_id); 2434 } 2435 } else { 2436 /* 2437 * Not a high-level interrupt, therefore call the soft level 2438 * interrupt explicitly 2439 */ 2440 (void) mrsas_softintr(instance); 2441 } 2442 2443 return (DDI_INTR_CLAIMED); 2444 } 2445 2446 2447 /* 2448 * ************************************************************************** * 2449 * * 2450 * libraries * 2451 * * 2452 * ************************************************************************** * 2453 */ 2454 /* 2455 * get_mfi_pkt : Get a command from the free pool 2456 * After successful allocation, the caller of this routine 2457 * must clear the frame buffer (memset to zero) before 2458 * using the packet further. 2459 * 2460 * ***** Note ***** 2461 * After clearing the frame buffer the context id of the 2462 * frame buffer SHOULD be restored back. 2463 */ 2464 static struct mrsas_cmd * 2465 get_mfi_pkt(struct mrsas_instance *instance) 2466 { 2467 mlist_t *head = &instance->cmd_pool_list; 2468 struct mrsas_cmd *cmd = NULL; 2469 2470 mutex_enter(&instance->cmd_pool_mtx); 2471 2472 if (!mlist_empty(head)) { 2473 cmd = mlist_entry(head->next, struct mrsas_cmd, list); 2474 mlist_del_init(head->next); 2475 } 2476 if (cmd != NULL) { 2477 cmd->pkt = NULL; 2478 cmd->retry_count_for_ocr = 0; 2479 cmd->drv_pkt_time = 0; 2480 2481 } 2482 mutex_exit(&instance->cmd_pool_mtx); 2483 2484 return (cmd); 2485 } 2486 2487 static struct mrsas_cmd * 2488 get_mfi_app_pkt(struct mrsas_instance *instance) 2489 { 2490 mlist_t *head = &instance->app_cmd_pool_list; 2491 struct mrsas_cmd *cmd = NULL; 2492 2493 mutex_enter(&instance->app_cmd_pool_mtx); 2494 2495 if (!mlist_empty(head)) { 2496 cmd = mlist_entry(head->next, struct mrsas_cmd, list); 2497 mlist_del_init(head->next); 2498 } 2499 if (cmd != NULL) { 2500 cmd->pkt = NULL; 2501 cmd->retry_count_for_ocr = 0; 2502 cmd->drv_pkt_time = 0; 2503 } 2504 2505 mutex_exit(&instance->app_cmd_pool_mtx); 2506 2507 return (cmd); 2508 } 2509 /* 2510 * return_mfi_pkt : Return a cmd to free command pool 2511 */ 2512 static void 2513 return_mfi_pkt(struct mrsas_instance *instance, struct mrsas_cmd *cmd) 2514 { 2515 mutex_enter(&instance->cmd_pool_mtx); 2516 /* use mlist_add_tail for debug assistance */ 2517 mlist_add_tail(&cmd->list, &instance->cmd_pool_list); 2518 2519 mutex_exit(&instance->cmd_pool_mtx); 2520 } 2521 2522 static void 2523 return_mfi_app_pkt(struct mrsas_instance *instance, struct mrsas_cmd *cmd) 2524 { 2525 mutex_enter(&instance->app_cmd_pool_mtx); 2526 2527 mlist_add(&cmd->list, &instance->app_cmd_pool_list); 2528 2529 mutex_exit(&instance->app_cmd_pool_mtx); 2530 } 2531 void 2532 push_pending_mfi_pkt(struct mrsas_instance *instance, struct mrsas_cmd *cmd) 2533 { 2534 struct scsi_pkt *pkt; 2535 struct mrsas_header *hdr; 2536 con_log(CL_DLEVEL2, (CE_NOTE, "push_pending_pkt(): Called\n")); 2537 mutex_enter(&instance->cmd_pend_mtx); 2538 mlist_del_init(&cmd->list); 2539 mlist_add_tail(&cmd->list, &instance->cmd_pend_list); 2540 if (cmd->sync_cmd == MRSAS_TRUE) { 2541 hdr = (struct mrsas_header *)&cmd->frame->hdr; 2542 if (hdr) { 2543 con_log(CL_ANN1, (CE_CONT, 2544 "push_pending_mfi_pkt: " 2545 "cmd %p index %x " 2546 "time %llx", 2547 (void *)cmd, cmd->index, 2548 gethrtime())); 2549 /* Wait for specified interval */ 2550 cmd->drv_pkt_time = ddi_get16( 2551 cmd->frame_dma_obj.acc_handle, &hdr->timeout); 2552 if (cmd->drv_pkt_time < debug_timeout_g) 2553 cmd->drv_pkt_time = (uint16_t)debug_timeout_g; 2554 con_log(CL_ANN1, (CE_CONT, 2555 "push_pending_pkt(): " 2556 "Called IO Timeout Value %x\n", 2557 cmd->drv_pkt_time)); 2558 } 2559 if (hdr && instance->timeout_id == (timeout_id_t)-1) { 2560 instance->timeout_id = timeout(io_timeout_checker, 2561 (void *) instance, drv_usectohz(MRSAS_1_SECOND)); 2562 } 2563 } else { 2564 pkt = cmd->pkt; 2565 if (pkt) { 2566 con_log(CL_ANN1, (CE_CONT, 2567 "push_pending_mfi_pkt: " 2568 "cmd %p index %x pkt %p, " 2569 "time %llx", 2570 (void *)cmd, cmd->index, (void *)pkt, 2571 gethrtime())); 2572 cmd->drv_pkt_time = (uint16_t)debug_timeout_g; 2573 } 2574 if (pkt && instance->timeout_id == (timeout_id_t)-1) { 2575 instance->timeout_id = timeout(io_timeout_checker, 2576 (void *) instance, drv_usectohz(MRSAS_1_SECOND)); 2577 } 2578 } 2579 2580 mutex_exit(&instance->cmd_pend_mtx); 2581 2582 } 2583 2584 int 2585 mrsas_print_pending_cmds(struct mrsas_instance *instance) 2586 { 2587 mlist_t *head = &instance->cmd_pend_list; 2588 mlist_t *tmp = head; 2589 struct mrsas_cmd *cmd = NULL; 2590 struct mrsas_header *hdr; 2591 unsigned int flag = 1; 2592 struct scsi_pkt *pkt; 2593 int saved_level; 2594 int cmd_count = 0; 2595 2596 saved_level = debug_level_g; 2597 debug_level_g = CL_ANN1; 2598 2599 cmn_err(CE_NOTE, "mrsas_print_pending_cmds(): Called\n"); 2600 2601 while (flag) { 2602 mutex_enter(&instance->cmd_pend_mtx); 2603 tmp = tmp->next; 2604 if (tmp == head) { 2605 mutex_exit(&instance->cmd_pend_mtx); 2606 flag = 0; 2607 con_log(CL_ANN1, (CE_CONT, "mrsas_print_pending_cmds():" 2608 " NO MORE CMDS PENDING....\n")); 2609 break; 2610 } else { 2611 cmd = mlist_entry(tmp, struct mrsas_cmd, list); 2612 mutex_exit(&instance->cmd_pend_mtx); 2613 if (cmd) { 2614 if (cmd->sync_cmd == MRSAS_TRUE) { 2615 hdr = (struct mrsas_header *) 2616 &cmd->frame->hdr; 2617 if (hdr) { 2618 con_log(CL_ANN1, (CE_CONT, 2619 "print: cmd %p index 0x%x " 2620 "drv_pkt_time 0x%x (NO-PKT)" 2621 " hdr %p\n", (void *)cmd, 2622 cmd->index, 2623 cmd->drv_pkt_time, 2624 (void *)hdr)); 2625 } 2626 } else { 2627 pkt = cmd->pkt; 2628 if (pkt) { 2629 con_log(CL_ANN1, (CE_CONT, 2630 "print: cmd %p index 0x%x " 2631 "drv_pkt_time 0x%x pkt %p \n", 2632 (void *)cmd, cmd->index, 2633 cmd->drv_pkt_time, (void *)pkt)); 2634 } 2635 } 2636 2637 if (++cmd_count == 1) { 2638 mrsas_print_cmd_details(instance, cmd, 2639 0xDD); 2640 } else { 2641 mrsas_print_cmd_details(instance, cmd, 2642 1); 2643 } 2644 2645 } 2646 } 2647 } 2648 con_log(CL_ANN1, (CE_CONT, "mrsas_print_pending_cmds(): Done\n")); 2649 2650 2651 debug_level_g = saved_level; 2652 2653 return (DDI_SUCCESS); 2654 } 2655 2656 2657 int 2658 mrsas_complete_pending_cmds(struct mrsas_instance *instance) 2659 { 2660 2661 struct mrsas_cmd *cmd = NULL; 2662 struct scsi_pkt *pkt; 2663 struct mrsas_header *hdr; 2664 2665 struct mlist_head *pos, *next; 2666 2667 con_log(CL_ANN1, (CE_NOTE, 2668 "mrsas_complete_pending_cmds(): Called")); 2669 2670 mutex_enter(&instance->cmd_pend_mtx); 2671 mlist_for_each_safe(pos, next, &instance->cmd_pend_list) { 2672 cmd = mlist_entry(pos, struct mrsas_cmd, list); 2673 if (cmd) { 2674 pkt = cmd->pkt; 2675 if (pkt) { /* for IO */ 2676 if (((pkt->pkt_flags & FLAG_NOINTR) 2677 == 0) && pkt->pkt_comp) { 2678 pkt->pkt_reason 2679 = CMD_DEV_GONE; 2680 pkt->pkt_statistics 2681 = STAT_DISCON; 2682 con_log(CL_ANN1, (CE_CONT, 2683 "fail and posting to scsa " 2684 "cmd %p index %x" 2685 " pkt %p " 2686 "time : %llx", 2687 (void *)cmd, cmd->index, 2688 (void *)pkt, gethrtime())); 2689 (*pkt->pkt_comp)(pkt); 2690 } 2691 } else { /* for DCMDS */ 2692 if (cmd->sync_cmd == MRSAS_TRUE) { 2693 hdr = (struct mrsas_header *)&cmd->frame->hdr; 2694 con_log(CL_ANN1, (CE_CONT, 2695 "posting invalid status to application " 2696 "cmd %p index %x" 2697 " hdr %p " 2698 "time : %llx", 2699 (void *)cmd, cmd->index, 2700 (void *)hdr, gethrtime())); 2701 hdr->cmd_status = MFI_STAT_INVALID_STATUS; 2702 complete_cmd_in_sync_mode(instance, cmd); 2703 } 2704 } 2705 mlist_del_init(&cmd->list); 2706 } else { 2707 con_log(CL_ANN1, (CE_CONT, 2708 "mrsas_complete_pending_cmds:" 2709 "NULL command\n")); 2710 } 2711 con_log(CL_ANN1, (CE_CONT, 2712 "mrsas_complete_pending_cmds:" 2713 "looping for more commands\n")); 2714 } 2715 mutex_exit(&instance->cmd_pend_mtx); 2716 2717 con_log(CL_ANN1, (CE_CONT, "mrsas_complete_pending_cmds(): DONE\n")); 2718 return (DDI_SUCCESS); 2719 } 2720 2721 void 2722 mrsas_print_cmd_details(struct mrsas_instance *instance, struct mrsas_cmd *cmd, 2723 int detail) 2724 { 2725 struct scsi_pkt *pkt = cmd->pkt; 2726 Mpi2RaidSCSIIORequest_t *scsi_io = cmd->scsi_io_request; 2727 int i; 2728 int saved_level; 2729 ddi_acc_handle_t acc_handle = 2730 instance->mpi2_frame_pool_dma_obj.acc_handle; 2731 2732 if (detail == 0xDD) { 2733 saved_level = debug_level_g; 2734 debug_level_g = CL_ANN1; 2735 } 2736 2737 2738 if (instance->tbolt) { 2739 con_log(CL_ANN1, (CE_CONT, "print_cmd_details: cmd %p " 2740 "cmd->index 0x%x SMID 0x%x timer 0x%x sec\n", 2741 (void *)cmd, cmd->index, cmd->SMID, cmd->drv_pkt_time)); 2742 } else { 2743 con_log(CL_ANN1, (CE_CONT, "print_cmd_details: cmd %p " 2744 "cmd->index 0x%x timer 0x%x sec\n", 2745 (void *)cmd, cmd->index, cmd->drv_pkt_time)); 2746 } 2747 2748 if (pkt) { 2749 con_log(CL_ANN1, (CE_CONT, "scsi_pkt CDB[0]=0x%x", 2750 pkt->pkt_cdbp[0])); 2751 } else { 2752 con_log(CL_ANN1, (CE_CONT, "NO-PKT")); 2753 } 2754 2755 if ((detail == 0xDD) && instance->tbolt) { 2756 con_log(CL_ANN1, (CE_CONT, "RAID_SCSI_IO_REQUEST\n")); 2757 con_log(CL_ANN1, (CE_CONT, "DevHandle=0x%X Function=0x%X " 2758 "IoFlags=0x%X SGLFlags=0x%X DataLength=0x%X\n", 2759 ddi_get16(acc_handle, &scsi_io->DevHandle), 2760 ddi_get8(acc_handle, &scsi_io->Function), 2761 ddi_get16(acc_handle, &scsi_io->IoFlags), 2762 ddi_get16(acc_handle, &scsi_io->SGLFlags), 2763 ddi_get32(acc_handle, &scsi_io->DataLength))); 2764 2765 for (i = 0; i < 32; i++) { 2766 con_log(CL_ANN1, (CE_CONT, "CDB[%d]=0x%x ", i, 2767 ddi_get8(acc_handle, &scsi_io->CDB.CDB32[i]))); 2768 } 2769 2770 con_log(CL_ANN1, (CE_CONT, "RAID-CONTEXT\n")); 2771 con_log(CL_ANN1, (CE_CONT, "status=0x%X extStatus=0x%X " 2772 "ldTargetId=0x%X timeoutValue=0x%X regLockFlags=0x%X " 2773 "RAIDFlags=0x%X regLockRowLBA=0x%" PRIu64 2774 " regLockLength=0x%X spanArm=0x%X\n", 2775 ddi_get8(acc_handle, &scsi_io->RaidContext.status), 2776 ddi_get8(acc_handle, &scsi_io->RaidContext.extStatus), 2777 ddi_get16(acc_handle, &scsi_io->RaidContext.ldTargetId), 2778 ddi_get16(acc_handle, &scsi_io->RaidContext.timeoutValue), 2779 ddi_get8(acc_handle, &scsi_io->RaidContext.regLockFlags), 2780 ddi_get8(acc_handle, &scsi_io->RaidContext.RAIDFlags), 2781 ddi_get64(acc_handle, &scsi_io->RaidContext.regLockRowLBA), 2782 ddi_get32(acc_handle, &scsi_io->RaidContext.regLockLength), 2783 ddi_get8(acc_handle, &scsi_io->RaidContext.spanArm))); 2784 } 2785 2786 if (detail == 0xDD) { 2787 debug_level_g = saved_level; 2788 } 2789 } 2790 2791 2792 int 2793 mrsas_issue_pending_cmds(struct mrsas_instance *instance) 2794 { 2795 mlist_t *head = &instance->cmd_pend_list; 2796 mlist_t *tmp = head->next; 2797 struct mrsas_cmd *cmd = NULL; 2798 struct scsi_pkt *pkt; 2799 2800 con_log(CL_ANN1, (CE_NOTE, "mrsas_issue_pending_cmds(): Called")); 2801 while (tmp != head) { 2802 mutex_enter(&instance->cmd_pend_mtx); 2803 cmd = mlist_entry(tmp, struct mrsas_cmd, list); 2804 tmp = tmp->next; 2805 mutex_exit(&instance->cmd_pend_mtx); 2806 if (cmd) { 2807 con_log(CL_ANN1, (CE_CONT, 2808 "mrsas_issue_pending_cmds(): " 2809 "Got a cmd: cmd %p index 0x%x drv_pkt_time 0x%x ", 2810 (void *)cmd, cmd->index, cmd->drv_pkt_time)); 2811 2812 /* Reset command timeout value */ 2813 if (cmd->drv_pkt_time < debug_timeout_g) 2814 cmd->drv_pkt_time = (uint16_t)debug_timeout_g; 2815 2816 cmd->retry_count_for_ocr++; 2817 2818 cmn_err(CE_CONT, "cmd retry count = %d\n", 2819 cmd->retry_count_for_ocr); 2820 2821 if (cmd->retry_count_for_ocr > IO_RETRY_COUNT) { 2822 cmn_err(CE_WARN, "mrsas_issue_pending_cmds(): " 2823 "cmd->retry_count exceeded limit >%d\n", 2824 IO_RETRY_COUNT); 2825 mrsas_print_cmd_details(instance, cmd, 0xDD); 2826 2827 cmn_err(CE_WARN, 2828 "mrsas_issue_pending_cmds():" 2829 "Calling KILL Adapter\n"); 2830 if (instance->tbolt) 2831 mrsas_tbolt_kill_adapter(instance); 2832 else 2833 (void) mrsas_kill_adapter(instance); 2834 return (DDI_FAILURE); 2835 } 2836 2837 pkt = cmd->pkt; 2838 if (pkt) { 2839 con_log(CL_ANN1, (CE_CONT, 2840 "PENDING PKT-CMD ISSUE: cmd %p index %x " 2841 "pkt %p time %llx", 2842 (void *)cmd, cmd->index, 2843 (void *)pkt, 2844 gethrtime())); 2845 2846 } else { 2847 cmn_err(CE_CONT, 2848 "mrsas_issue_pending_cmds(): NO-PKT, " 2849 "cmd %p index 0x%x drv_pkt_time 0x%x ", 2850 (void *)cmd, cmd->index, cmd->drv_pkt_time); 2851 } 2852 2853 2854 if (cmd->sync_cmd == MRSAS_TRUE) { 2855 cmn_err(CE_CONT, "mrsas_issue_pending_cmds(): " 2856 "SYNC_CMD == TRUE \n"); 2857 instance->func_ptr->issue_cmd_in_sync_mode( 2858 instance, cmd); 2859 } else { 2860 instance->func_ptr->issue_cmd(cmd, instance); 2861 } 2862 } else { 2863 con_log(CL_ANN1, (CE_CONT, 2864 "mrsas_issue_pending_cmds: NULL command\n")); 2865 } 2866 con_log(CL_ANN1, (CE_CONT, 2867 "mrsas_issue_pending_cmds:" 2868 "looping for more commands")); 2869 } 2870 con_log(CL_ANN1, (CE_CONT, "mrsas_issue_pending_cmds(): DONE\n")); 2871 return (DDI_SUCCESS); 2872 } 2873 2874 2875 2876 /* 2877 * destroy_mfi_frame_pool 2878 */ 2879 void 2880 destroy_mfi_frame_pool(struct mrsas_instance *instance) 2881 { 2882 int i; 2883 uint32_t max_cmd = instance->max_fw_cmds; 2884 2885 struct mrsas_cmd *cmd; 2886 2887 /* return all frames to pool */ 2888 2889 for (i = 0; i < max_cmd; i++) { 2890 2891 cmd = instance->cmd_list[i]; 2892 2893 if (cmd->frame_dma_obj_status == DMA_OBJ_ALLOCATED) 2894 (void) mrsas_free_dma_obj(instance, cmd->frame_dma_obj); 2895 2896 cmd->frame_dma_obj_status = DMA_OBJ_FREED; 2897 } 2898 2899 } 2900 2901 /* 2902 * create_mfi_frame_pool 2903 */ 2904 int 2905 create_mfi_frame_pool(struct mrsas_instance *instance) 2906 { 2907 int i = 0; 2908 int cookie_cnt; 2909 uint16_t max_cmd; 2910 uint16_t sge_sz; 2911 uint32_t sgl_sz; 2912 uint32_t tot_frame_size; 2913 struct mrsas_cmd *cmd; 2914 int retval = DDI_SUCCESS; 2915 2916 max_cmd = instance->max_fw_cmds; 2917 sge_sz = sizeof (struct mrsas_sge_ieee); 2918 /* calculated the number of 64byte frames required for SGL */ 2919 sgl_sz = sge_sz * instance->max_num_sge; 2920 tot_frame_size = sgl_sz + MRMFI_FRAME_SIZE + SENSE_LENGTH; 2921 2922 con_log(CL_DLEVEL3, (CE_NOTE, "create_mfi_frame_pool: " 2923 "sgl_sz %x tot_frame_size %x", sgl_sz, tot_frame_size)); 2924 2925 while (i < max_cmd) { 2926 cmd = instance->cmd_list[i]; 2927 2928 cmd->frame_dma_obj.size = tot_frame_size; 2929 cmd->frame_dma_obj.dma_attr = mrsas_generic_dma_attr; 2930 cmd->frame_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 2931 cmd->frame_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU; 2932 cmd->frame_dma_obj.dma_attr.dma_attr_sgllen = 1; 2933 cmd->frame_dma_obj.dma_attr.dma_attr_align = 64; 2934 2935 cookie_cnt = mrsas_alloc_dma_obj(instance, &cmd->frame_dma_obj, 2936 (uchar_t)DDI_STRUCTURE_LE_ACC); 2937 2938 if (cookie_cnt == -1 || cookie_cnt > 1) { 2939 cmn_err(CE_WARN, 2940 "create_mfi_frame_pool: could not alloc."); 2941 retval = DDI_FAILURE; 2942 goto mrsas_undo_frame_pool; 2943 } 2944 2945 bzero(cmd->frame_dma_obj.buffer, tot_frame_size); 2946 2947 cmd->frame_dma_obj_status = DMA_OBJ_ALLOCATED; 2948 cmd->frame = (union mrsas_frame *)cmd->frame_dma_obj.buffer; 2949 cmd->frame_phys_addr = 2950 cmd->frame_dma_obj.dma_cookie[0].dmac_address; 2951 2952 cmd->sense = (uint8_t *)(((unsigned long) 2953 cmd->frame_dma_obj.buffer) + 2954 tot_frame_size - SENSE_LENGTH); 2955 cmd->sense_phys_addr = 2956 cmd->frame_dma_obj.dma_cookie[0].dmac_address + 2957 tot_frame_size - SENSE_LENGTH; 2958 2959 if (!cmd->frame || !cmd->sense) { 2960 cmn_err(CE_WARN, 2961 "mr_sas: pci_pool_alloc failed"); 2962 retval = ENOMEM; 2963 goto mrsas_undo_frame_pool; 2964 } 2965 2966 ddi_put32(cmd->frame_dma_obj.acc_handle, 2967 &cmd->frame->io.context, cmd->index); 2968 i++; 2969 2970 con_log(CL_DLEVEL3, (CE_NOTE, "[%x]-%x", 2971 cmd->index, cmd->frame_phys_addr)); 2972 } 2973 2974 return (DDI_SUCCESS); 2975 2976 mrsas_undo_frame_pool: 2977 if (i > 0) 2978 destroy_mfi_frame_pool(instance); 2979 2980 return (retval); 2981 } 2982 2983 /* 2984 * free_additional_dma_buffer 2985 */ 2986 static void 2987 free_additional_dma_buffer(struct mrsas_instance *instance) 2988 { 2989 if (instance->mfi_internal_dma_obj.status == DMA_OBJ_ALLOCATED) { 2990 (void) mrsas_free_dma_obj(instance, 2991 instance->mfi_internal_dma_obj); 2992 instance->mfi_internal_dma_obj.status = DMA_OBJ_FREED; 2993 } 2994 2995 if (instance->mfi_evt_detail_obj.status == DMA_OBJ_ALLOCATED) { 2996 (void) mrsas_free_dma_obj(instance, 2997 instance->mfi_evt_detail_obj); 2998 instance->mfi_evt_detail_obj.status = DMA_OBJ_FREED; 2999 } 3000 } 3001 3002 /* 3003 * alloc_additional_dma_buffer 3004 */ 3005 static int 3006 alloc_additional_dma_buffer(struct mrsas_instance *instance) 3007 { 3008 uint32_t reply_q_sz; 3009 uint32_t internal_buf_size = PAGESIZE*2; 3010 3011 /* max cmds plus 1 + producer & consumer */ 3012 reply_q_sz = sizeof (uint32_t) * (instance->max_fw_cmds + 1 + 2); 3013 3014 instance->mfi_internal_dma_obj.size = internal_buf_size; 3015 instance->mfi_internal_dma_obj.dma_attr = mrsas_generic_dma_attr; 3016 instance->mfi_internal_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 3017 instance->mfi_internal_dma_obj.dma_attr.dma_attr_count_max = 3018 0xFFFFFFFFU; 3019 instance->mfi_internal_dma_obj.dma_attr.dma_attr_sgllen = 1; 3020 3021 if (mrsas_alloc_dma_obj(instance, &instance->mfi_internal_dma_obj, 3022 (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) { 3023 cmn_err(CE_WARN, 3024 "mr_sas: could not alloc reply queue"); 3025 return (DDI_FAILURE); 3026 } 3027 3028 bzero(instance->mfi_internal_dma_obj.buffer, internal_buf_size); 3029 3030 instance->mfi_internal_dma_obj.status |= DMA_OBJ_ALLOCATED; 3031 3032 instance->producer = (uint32_t *)((unsigned long) 3033 instance->mfi_internal_dma_obj.buffer); 3034 instance->consumer = (uint32_t *)((unsigned long) 3035 instance->mfi_internal_dma_obj.buffer + 4); 3036 instance->reply_queue = (uint32_t *)((unsigned long) 3037 instance->mfi_internal_dma_obj.buffer + 8); 3038 instance->internal_buf = (caddr_t)(((unsigned long) 3039 instance->mfi_internal_dma_obj.buffer) + reply_q_sz + 8); 3040 instance->internal_buf_dmac_add = 3041 instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address + 3042 (reply_q_sz + 8); 3043 instance->internal_buf_size = internal_buf_size - 3044 (reply_q_sz + 8); 3045 3046 /* allocate evt_detail */ 3047 instance->mfi_evt_detail_obj.size = sizeof (struct mrsas_evt_detail); 3048 instance->mfi_evt_detail_obj.dma_attr = mrsas_generic_dma_attr; 3049 instance->mfi_evt_detail_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 3050 instance->mfi_evt_detail_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU; 3051 instance->mfi_evt_detail_obj.dma_attr.dma_attr_sgllen = 1; 3052 instance->mfi_evt_detail_obj.dma_attr.dma_attr_align = 1; 3053 3054 if (mrsas_alloc_dma_obj(instance, &instance->mfi_evt_detail_obj, 3055 (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) { 3056 cmn_err(CE_WARN, "alloc_additional_dma_buffer: " 3057 "could not allocate data transfer buffer."); 3058 goto mrsas_undo_internal_buff; 3059 } 3060 3061 bzero(instance->mfi_evt_detail_obj.buffer, 3062 sizeof (struct mrsas_evt_detail)); 3063 3064 instance->mfi_evt_detail_obj.status |= DMA_OBJ_ALLOCATED; 3065 3066 return (DDI_SUCCESS); 3067 3068 mrsas_undo_internal_buff: 3069 if (instance->mfi_internal_dma_obj.status == DMA_OBJ_ALLOCATED) { 3070 (void) mrsas_free_dma_obj(instance, 3071 instance->mfi_internal_dma_obj); 3072 instance->mfi_internal_dma_obj.status = DMA_OBJ_FREED; 3073 } 3074 3075 return (DDI_FAILURE); 3076 } 3077 3078 3079 void 3080 mrsas_free_cmd_pool(struct mrsas_instance *instance) 3081 { 3082 int i; 3083 uint32_t max_cmd; 3084 size_t sz; 3085 3086 /* already freed */ 3087 if (instance->cmd_list == NULL) { 3088 return; 3089 } 3090 3091 max_cmd = instance->max_fw_cmds; 3092 3093 /* size of cmd_list array */ 3094 sz = sizeof (struct mrsas_cmd *) * max_cmd; 3095 3096 /* First free each cmd */ 3097 for (i = 0; i < max_cmd; i++) { 3098 if (instance->cmd_list[i] != NULL) { 3099 kmem_free(instance->cmd_list[i], 3100 sizeof (struct mrsas_cmd)); 3101 } 3102 3103 instance->cmd_list[i] = NULL; 3104 } 3105 3106 /* Now, free cmd_list array */ 3107 if (instance->cmd_list != NULL) 3108 kmem_free(instance->cmd_list, sz); 3109 3110 instance->cmd_list = NULL; 3111 3112 INIT_LIST_HEAD(&instance->cmd_pool_list); 3113 INIT_LIST_HEAD(&instance->cmd_pend_list); 3114 if (instance->tbolt) { 3115 INIT_LIST_HEAD(&instance->cmd_app_pool_list); 3116 } else { 3117 INIT_LIST_HEAD(&instance->app_cmd_pool_list); 3118 } 3119 3120 } 3121 3122 3123 /* 3124 * mrsas_alloc_cmd_pool 3125 */ 3126 int 3127 mrsas_alloc_cmd_pool(struct mrsas_instance *instance) 3128 { 3129 int i; 3130 int count; 3131 uint32_t max_cmd; 3132 uint32_t reserve_cmd; 3133 size_t sz; 3134 3135 struct mrsas_cmd *cmd; 3136 3137 max_cmd = instance->max_fw_cmds; 3138 con_log(CL_ANN1, (CE_NOTE, "mrsas_alloc_cmd_pool: " 3139 "max_cmd %x", max_cmd)); 3140 3141 3142 sz = sizeof (struct mrsas_cmd *) * max_cmd; 3143 3144 /* 3145 * instance->cmd_list is an array of struct mrsas_cmd pointers. 3146 * Allocate the dynamic array first and then allocate individual 3147 * commands. 3148 */ 3149 instance->cmd_list = kmem_zalloc(sz, KM_SLEEP); 3150 ASSERT(instance->cmd_list); 3151 3152 /* create a frame pool and assign one frame to each cmd */ 3153 for (count = 0; count < max_cmd; count++) { 3154 instance->cmd_list[count] = 3155 kmem_zalloc(sizeof (struct mrsas_cmd), KM_SLEEP); 3156 ASSERT(instance->cmd_list[count]); 3157 } 3158 3159 /* add all the commands to command pool */ 3160 3161 INIT_LIST_HEAD(&instance->cmd_pool_list); 3162 INIT_LIST_HEAD(&instance->cmd_pend_list); 3163 INIT_LIST_HEAD(&instance->app_cmd_pool_list); 3164 3165 reserve_cmd = MRSAS_APP_RESERVED_CMDS; 3166 3167 for (i = 0; i < reserve_cmd; i++) { 3168 cmd = instance->cmd_list[i]; 3169 cmd->index = i; 3170 mlist_add_tail(&cmd->list, &instance->app_cmd_pool_list); 3171 } 3172 3173 3174 for (i = reserve_cmd; i < max_cmd; i++) { 3175 cmd = instance->cmd_list[i]; 3176 cmd->index = i; 3177 mlist_add_tail(&cmd->list, &instance->cmd_pool_list); 3178 } 3179 3180 return (DDI_SUCCESS); 3181 3182 mrsas_undo_cmds: 3183 if (count > 0) { 3184 /* free each cmd */ 3185 for (i = 0; i < count; i++) { 3186 if (instance->cmd_list[i] != NULL) { 3187 kmem_free(instance->cmd_list[i], 3188 sizeof (struct mrsas_cmd)); 3189 } 3190 instance->cmd_list[i] = NULL; 3191 } 3192 } 3193 3194 mrsas_undo_cmd_list: 3195 if (instance->cmd_list != NULL) 3196 kmem_free(instance->cmd_list, sz); 3197 instance->cmd_list = NULL; 3198 3199 return (DDI_FAILURE); 3200 } 3201 3202 3203 /* 3204 * free_space_for_mfi 3205 */ 3206 static void 3207 free_space_for_mfi(struct mrsas_instance *instance) 3208 { 3209 3210 /* already freed */ 3211 if (instance->cmd_list == NULL) { 3212 return; 3213 } 3214 3215 /* Free additional dma buffer */ 3216 free_additional_dma_buffer(instance); 3217 3218 /* Free the MFI frame pool */ 3219 destroy_mfi_frame_pool(instance); 3220 3221 /* Free all the commands in the cmd_list */ 3222 /* Free the cmd_list buffer itself */ 3223 mrsas_free_cmd_pool(instance); 3224 } 3225 3226 /* 3227 * alloc_space_for_mfi 3228 */ 3229 static int 3230 alloc_space_for_mfi(struct mrsas_instance *instance) 3231 { 3232 /* Allocate command pool (memory for cmd_list & individual commands) */ 3233 if (mrsas_alloc_cmd_pool(instance)) { 3234 cmn_err(CE_WARN, "error creating cmd pool"); 3235 return (DDI_FAILURE); 3236 } 3237 3238 /* Allocate MFI Frame pool */ 3239 if (create_mfi_frame_pool(instance)) { 3240 cmn_err(CE_WARN, "error creating frame DMA pool"); 3241 goto mfi_undo_cmd_pool; 3242 } 3243 3244 /* Allocate additional DMA buffer */ 3245 if (alloc_additional_dma_buffer(instance)) { 3246 cmn_err(CE_WARN, "error creating frame DMA pool"); 3247 goto mfi_undo_frame_pool; 3248 } 3249 3250 return (DDI_SUCCESS); 3251 3252 mfi_undo_frame_pool: 3253 destroy_mfi_frame_pool(instance); 3254 3255 mfi_undo_cmd_pool: 3256 mrsas_free_cmd_pool(instance); 3257 3258 return (DDI_FAILURE); 3259 } 3260 3261 3262 3263 /* 3264 * get_ctrl_info 3265 */ 3266 static int 3267 get_ctrl_info(struct mrsas_instance *instance, 3268 struct mrsas_ctrl_info *ctrl_info) 3269 { 3270 int ret = 0; 3271 3272 struct mrsas_cmd *cmd; 3273 struct mrsas_dcmd_frame *dcmd; 3274 struct mrsas_ctrl_info *ci; 3275 3276 if (instance->tbolt) { 3277 cmd = get_raid_msg_mfi_pkt(instance); 3278 } else { 3279 cmd = get_mfi_pkt(instance); 3280 } 3281 3282 if (!cmd) { 3283 con_log(CL_ANN, (CE_WARN, 3284 "Failed to get a cmd for ctrl info")); 3285 DTRACE_PROBE2(info_mfi_err, uint16_t, instance->fw_outstanding, 3286 uint16_t, instance->max_fw_cmds); 3287 return (DDI_FAILURE); 3288 } 3289 3290 /* Clear the frame buffer and assign back the context id */ 3291 (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame)); 3292 ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context, 3293 cmd->index); 3294 3295 dcmd = &cmd->frame->dcmd; 3296 3297 ci = (struct mrsas_ctrl_info *)instance->internal_buf; 3298 3299 if (!ci) { 3300 cmn_err(CE_WARN, 3301 "Failed to alloc mem for ctrl info"); 3302 return_mfi_pkt(instance, cmd); 3303 return (DDI_FAILURE); 3304 } 3305 3306 (void) memset(ci, 0, sizeof (struct mrsas_ctrl_info)); 3307 3308 /* for( i = 0; i < DCMD_MBOX_SZ; i++ ) dcmd->mbox.b[i] = 0; */ 3309 (void) memset(dcmd->mbox.b, 0, DCMD_MBOX_SZ); 3310 3311 ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd, MFI_CMD_OP_DCMD); 3312 ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd_status, 3313 MFI_CMD_STATUS_POLL_MODE); 3314 ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->sge_count, 1); 3315 ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->flags, 3316 MFI_FRAME_DIR_READ); 3317 ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->timeout, 0); 3318 ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->data_xfer_len, 3319 sizeof (struct mrsas_ctrl_info)); 3320 ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->opcode, 3321 MR_DCMD_CTRL_GET_INFO); 3322 ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].phys_addr, 3323 instance->internal_buf_dmac_add); 3324 ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].length, 3325 sizeof (struct mrsas_ctrl_info)); 3326 3327 cmd->frame_count = 1; 3328 3329 if (instance->tbolt) { 3330 mr_sas_tbolt_build_mfi_cmd(instance, cmd); 3331 } 3332 3333 if (!instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) { 3334 ret = 0; 3335 3336 ctrl_info->max_request_size = ddi_get32( 3337 cmd->frame_dma_obj.acc_handle, &ci->max_request_size); 3338 3339 ctrl_info->ld_present_count = ddi_get16( 3340 cmd->frame_dma_obj.acc_handle, &ci->ld_present_count); 3341 3342 ctrl_info->properties.on_off_properties = ddi_get32( 3343 cmd->frame_dma_obj.acc_handle, 3344 &ci->properties.on_off_properties); 3345 ddi_rep_get8(cmd->frame_dma_obj.acc_handle, 3346 (uint8_t *)(ctrl_info->product_name), 3347 (uint8_t *)(ci->product_name), 80 * sizeof (char), 3348 DDI_DEV_AUTOINCR); 3349 /* should get more members of ci with ddi_get when needed */ 3350 } else { 3351 cmn_err(CE_WARN, "get_ctrl_info: Ctrl info failed"); 3352 ret = -1; 3353 } 3354 3355 if (mrsas_common_check(instance, cmd) != DDI_SUCCESS) { 3356 ret = -1; 3357 } 3358 if (instance->tbolt) { 3359 return_raid_msg_mfi_pkt(instance, cmd); 3360 } else { 3361 return_mfi_pkt(instance, cmd); 3362 } 3363 3364 return (ret); 3365 } 3366 3367 /* 3368 * abort_aen_cmd 3369 */ 3370 static int 3371 abort_aen_cmd(struct mrsas_instance *instance, 3372 struct mrsas_cmd *cmd_to_abort) 3373 { 3374 int ret = 0; 3375 3376 struct mrsas_cmd *cmd; 3377 struct mrsas_abort_frame *abort_fr; 3378 3379 con_log(CL_ANN1, (CE_NOTE, "chkpnt: abort_aen:%d", __LINE__)); 3380 3381 if (instance->tbolt) { 3382 cmd = get_raid_msg_mfi_pkt(instance); 3383 } else { 3384 cmd = get_mfi_pkt(instance); 3385 } 3386 3387 if (!cmd) { 3388 con_log(CL_ANN1, (CE_WARN, 3389 "abort_aen_cmd():Failed to get a cmd for abort_aen_cmd")); 3390 DTRACE_PROBE2(abort_mfi_err, uint16_t, instance->fw_outstanding, 3391 uint16_t, instance->max_fw_cmds); 3392 return (DDI_FAILURE); 3393 } 3394 3395 /* Clear the frame buffer and assign back the context id */ 3396 (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame)); 3397 ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context, 3398 cmd->index); 3399 3400 abort_fr = &cmd->frame->abort; 3401 3402 /* prepare and issue the abort frame */ 3403 ddi_put8(cmd->frame_dma_obj.acc_handle, 3404 &abort_fr->cmd, MFI_CMD_OP_ABORT); 3405 ddi_put8(cmd->frame_dma_obj.acc_handle, &abort_fr->cmd_status, 3406 MFI_CMD_STATUS_SYNC_MODE); 3407 ddi_put16(cmd->frame_dma_obj.acc_handle, &abort_fr->flags, 0); 3408 ddi_put32(cmd->frame_dma_obj.acc_handle, &abort_fr->abort_context, 3409 cmd_to_abort->index); 3410 ddi_put32(cmd->frame_dma_obj.acc_handle, 3411 &abort_fr->abort_mfi_phys_addr_lo, cmd_to_abort->frame_phys_addr); 3412 ddi_put32(cmd->frame_dma_obj.acc_handle, 3413 &abort_fr->abort_mfi_phys_addr_hi, 0); 3414 3415 instance->aen_cmd->abort_aen = 1; 3416 3417 cmd->frame_count = 1; 3418 3419 if (instance->tbolt) { 3420 mr_sas_tbolt_build_mfi_cmd(instance, cmd); 3421 } 3422 3423 if (instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) { 3424 con_log(CL_ANN1, (CE_WARN, 3425 "abort_aen_cmd: issue_cmd_in_poll_mode failed")); 3426 ret = -1; 3427 } else { 3428 ret = 0; 3429 } 3430 3431 instance->aen_cmd->abort_aen = 1; 3432 instance->aen_cmd = 0; 3433 3434 if (instance->tbolt) { 3435 return_raid_msg_mfi_pkt(instance, cmd); 3436 } else { 3437 return_mfi_pkt(instance, cmd); 3438 } 3439 3440 atomic_add_16(&instance->fw_outstanding, (-1)); 3441 3442 return (ret); 3443 } 3444 3445 3446 static int 3447 mrsas_build_init_cmd(struct mrsas_instance *instance, 3448 struct mrsas_cmd **cmd_ptr) 3449 { 3450 struct mrsas_cmd *cmd; 3451 struct mrsas_init_frame *init_frame; 3452 struct mrsas_init_queue_info *initq_info; 3453 struct mrsas_drv_ver drv_ver_info; 3454 3455 3456 /* 3457 * Prepare a init frame. Note the init frame points to queue info 3458 * structure. Each frame has SGL allocated after first 64 bytes. For 3459 * this frame - since we don't need any SGL - we use SGL's space as 3460 * queue info structure 3461 */ 3462 cmd = *cmd_ptr; 3463 3464 3465 /* Clear the frame buffer and assign back the context id */ 3466 (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame)); 3467 ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context, 3468 cmd->index); 3469 3470 init_frame = (struct mrsas_init_frame *)cmd->frame; 3471 initq_info = (struct mrsas_init_queue_info *) 3472 ((unsigned long)init_frame + 64); 3473 3474 (void) memset(init_frame, 0, MRMFI_FRAME_SIZE); 3475 (void) memset(initq_info, 0, sizeof (struct mrsas_init_queue_info)); 3476 3477 ddi_put32(cmd->frame_dma_obj.acc_handle, &initq_info->init_flags, 0); 3478 3479 ddi_put32(cmd->frame_dma_obj.acc_handle, 3480 &initq_info->reply_queue_entries, instance->max_fw_cmds + 1); 3481 3482 ddi_put32(cmd->frame_dma_obj.acc_handle, 3483 &initq_info->producer_index_phys_addr_hi, 0); 3484 ddi_put32(cmd->frame_dma_obj.acc_handle, 3485 &initq_info->producer_index_phys_addr_lo, 3486 instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address); 3487 3488 ddi_put32(cmd->frame_dma_obj.acc_handle, 3489 &initq_info->consumer_index_phys_addr_hi, 0); 3490 ddi_put32(cmd->frame_dma_obj.acc_handle, 3491 &initq_info->consumer_index_phys_addr_lo, 3492 instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address + 4); 3493 3494 ddi_put32(cmd->frame_dma_obj.acc_handle, 3495 &initq_info->reply_queue_start_phys_addr_hi, 0); 3496 ddi_put32(cmd->frame_dma_obj.acc_handle, 3497 &initq_info->reply_queue_start_phys_addr_lo, 3498 instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address + 8); 3499 3500 ddi_put8(cmd->frame_dma_obj.acc_handle, 3501 &init_frame->cmd, MFI_CMD_OP_INIT); 3502 ddi_put8(cmd->frame_dma_obj.acc_handle, &init_frame->cmd_status, 3503 MFI_CMD_STATUS_POLL_MODE); 3504 ddi_put16(cmd->frame_dma_obj.acc_handle, &init_frame->flags, 0); 3505 ddi_put32(cmd->frame_dma_obj.acc_handle, 3506 &init_frame->queue_info_new_phys_addr_lo, 3507 cmd->frame_phys_addr + 64); 3508 ddi_put32(cmd->frame_dma_obj.acc_handle, 3509 &init_frame->queue_info_new_phys_addr_hi, 0); 3510 3511 3512 /* fill driver version information */ 3513 fill_up_drv_ver(&drv_ver_info); 3514 3515 /* allocate the driver version data transfer buffer */ 3516 instance->drv_ver_dma_obj.size = sizeof (drv_ver_info.drv_ver); 3517 instance->drv_ver_dma_obj.dma_attr = mrsas_generic_dma_attr; 3518 instance->drv_ver_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 3519 instance->drv_ver_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU; 3520 instance->drv_ver_dma_obj.dma_attr.dma_attr_sgllen = 1; 3521 instance->drv_ver_dma_obj.dma_attr.dma_attr_align = 1; 3522 3523 if (mrsas_alloc_dma_obj(instance, &instance->drv_ver_dma_obj, 3524 (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) { 3525 con_log(CL_ANN, (CE_WARN, 3526 "init_mfi : Could not allocate driver version buffer.")); 3527 return (DDI_FAILURE); 3528 } 3529 /* copy driver version to dma buffer */ 3530 (void) memset(instance->drv_ver_dma_obj.buffer, 0, 3531 sizeof (drv_ver_info.drv_ver)); 3532 ddi_rep_put8(cmd->frame_dma_obj.acc_handle, 3533 (uint8_t *)drv_ver_info.drv_ver, 3534 (uint8_t *)instance->drv_ver_dma_obj.buffer, 3535 sizeof (drv_ver_info.drv_ver), DDI_DEV_AUTOINCR); 3536 3537 3538 /* copy driver version physical address to init frame */ 3539 ddi_put64(cmd->frame_dma_obj.acc_handle, &init_frame->driverversion, 3540 instance->drv_ver_dma_obj.dma_cookie[0].dmac_address); 3541 3542 ddi_put32(cmd->frame_dma_obj.acc_handle, &init_frame->data_xfer_len, 3543 sizeof (struct mrsas_init_queue_info)); 3544 3545 cmd->frame_count = 1; 3546 3547 *cmd_ptr = cmd; 3548 3549 return (DDI_SUCCESS); 3550 } 3551 3552 3553 /* 3554 * mrsas_init_adapter_ppc - Initialize MFI interface adapter. 3555 */ 3556 int 3557 mrsas_init_adapter_ppc(struct mrsas_instance *instance) 3558 { 3559 struct mrsas_cmd *cmd; 3560 3561 /* 3562 * allocate memory for mfi adapter(cmd pool, individual commands, mfi 3563 * frames etc 3564 */ 3565 if (alloc_space_for_mfi(instance) != DDI_SUCCESS) { 3566 con_log(CL_ANN, (CE_NOTE, 3567 "Error, failed to allocate memory for MFI adapter")); 3568 return (DDI_FAILURE); 3569 } 3570 3571 /* Build INIT command */ 3572 cmd = get_mfi_pkt(instance); 3573 3574 if (mrsas_build_init_cmd(instance, &cmd) != DDI_SUCCESS) { 3575 con_log(CL_ANN, 3576 (CE_NOTE, "Error, failed to build INIT command")); 3577 3578 goto fail_undo_alloc_mfi_space; 3579 } 3580 3581 /* 3582 * Disable interrupt before sending init frame ( see linux driver code) 3583 * send INIT MFI frame in polled mode 3584 */ 3585 if (instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) { 3586 con_log(CL_ANN, (CE_WARN, "failed to init firmware")); 3587 goto fail_fw_init; 3588 } 3589 3590 if (mrsas_common_check(instance, cmd) != DDI_SUCCESS) 3591 goto fail_fw_init; 3592 return_mfi_pkt(instance, cmd); 3593 3594 if (ctio_enable && 3595 (instance->func_ptr->read_fw_status_reg(instance) & 0x04000000)) { 3596 con_log(CL_ANN, (CE_NOTE, "mr_sas: IEEE SGL's supported")); 3597 instance->flag_ieee = 1; 3598 } else { 3599 instance->flag_ieee = 0; 3600 } 3601 3602 instance->unroll.alloc_space_mfi = 1; 3603 instance->unroll.verBuff = 1; 3604 3605 return (DDI_SUCCESS); 3606 3607 3608 fail_fw_init: 3609 (void) mrsas_free_dma_obj(instance, instance->drv_ver_dma_obj); 3610 3611 fail_undo_alloc_mfi_space: 3612 return_mfi_pkt(instance, cmd); 3613 free_space_for_mfi(instance); 3614 3615 return (DDI_FAILURE); 3616 3617 } 3618 3619 /* 3620 * mrsas_init_adapter - Initialize adapter. 3621 */ 3622 int 3623 mrsas_init_adapter(struct mrsas_instance *instance) 3624 { 3625 struct mrsas_ctrl_info ctrl_info; 3626 3627 3628 /* we expect the FW state to be READY */ 3629 if (mfi_state_transition_to_ready(instance)) { 3630 con_log(CL_ANN, (CE_WARN, "mr_sas: F/W is not ready")); 3631 return (DDI_FAILURE); 3632 } 3633 3634 /* get various operational parameters from status register */ 3635 instance->max_num_sge = 3636 (instance->func_ptr->read_fw_status_reg(instance) & 3637 0xFF0000) >> 0x10; 3638 instance->max_num_sge = 3639 (instance->max_num_sge > MRSAS_MAX_SGE_CNT) ? 3640 MRSAS_MAX_SGE_CNT : instance->max_num_sge; 3641 3642 /* 3643 * Reduce the max supported cmds by 1. This is to ensure that the 3644 * reply_q_sz (1 more than the max cmd that driver may send) 3645 * does not exceed max cmds that the FW can support 3646 */ 3647 instance->max_fw_cmds = 3648 instance->func_ptr->read_fw_status_reg(instance) & 0xFFFF; 3649 instance->max_fw_cmds = instance->max_fw_cmds - 1; 3650 3651 3652 3653 /* Initialize adapter */ 3654 if (instance->func_ptr->init_adapter(instance) != DDI_SUCCESS) { 3655 con_log(CL_ANN, 3656 (CE_WARN, "mr_sas: could not initialize adapter")); 3657 return (DDI_FAILURE); 3658 } 3659 3660 /* gather misc FW related information */ 3661 instance->disable_online_ctrl_reset = 0; 3662 3663 if (!get_ctrl_info(instance, &ctrl_info)) { 3664 instance->max_sectors_per_req = ctrl_info.max_request_size; 3665 con_log(CL_ANN1, (CE_NOTE, 3666 "product name %s ld present %d", 3667 ctrl_info.product_name, ctrl_info.ld_present_count)); 3668 } else { 3669 instance->max_sectors_per_req = instance->max_num_sge * 3670 PAGESIZE / 512; 3671 } 3672 3673 if (ctrl_info.properties.on_off_properties & DISABLE_OCR_PROP_FLAG) 3674 instance->disable_online_ctrl_reset = 1; 3675 3676 return (DDI_SUCCESS); 3677 3678 } 3679 3680 3681 3682 static int 3683 mrsas_issue_init_mfi(struct mrsas_instance *instance) 3684 { 3685 struct mrsas_cmd *cmd; 3686 struct mrsas_init_frame *init_frame; 3687 struct mrsas_init_queue_info *initq_info; 3688 3689 /* 3690 * Prepare a init frame. Note the init frame points to queue info 3691 * structure. Each frame has SGL allocated after first 64 bytes. For 3692 * this frame - since we don't need any SGL - we use SGL's space as 3693 * queue info structure 3694 */ 3695 con_log(CL_ANN1, (CE_NOTE, 3696 "mrsas_issue_init_mfi: entry\n")); 3697 cmd = get_mfi_app_pkt(instance); 3698 3699 if (!cmd) { 3700 con_log(CL_ANN1, (CE_WARN, 3701 "mrsas_issue_init_mfi: get_pkt failed\n")); 3702 return (DDI_FAILURE); 3703 } 3704 3705 /* Clear the frame buffer and assign back the context id */ 3706 (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame)); 3707 ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context, 3708 cmd->index); 3709 3710 init_frame = (struct mrsas_init_frame *)cmd->frame; 3711 initq_info = (struct mrsas_init_queue_info *) 3712 ((unsigned long)init_frame + 64); 3713 3714 (void) memset(init_frame, 0, MRMFI_FRAME_SIZE); 3715 (void) memset(initq_info, 0, sizeof (struct mrsas_init_queue_info)); 3716 3717 ddi_put32(cmd->frame_dma_obj.acc_handle, &initq_info->init_flags, 0); 3718 3719 ddi_put32(cmd->frame_dma_obj.acc_handle, 3720 &initq_info->reply_queue_entries, instance->max_fw_cmds + 1); 3721 ddi_put32(cmd->frame_dma_obj.acc_handle, 3722 &initq_info->producer_index_phys_addr_hi, 0); 3723 ddi_put32(cmd->frame_dma_obj.acc_handle, 3724 &initq_info->producer_index_phys_addr_lo, 3725 instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address); 3726 ddi_put32(cmd->frame_dma_obj.acc_handle, 3727 &initq_info->consumer_index_phys_addr_hi, 0); 3728 ddi_put32(cmd->frame_dma_obj.acc_handle, 3729 &initq_info->consumer_index_phys_addr_lo, 3730 instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address + 4); 3731 3732 ddi_put32(cmd->frame_dma_obj.acc_handle, 3733 &initq_info->reply_queue_start_phys_addr_hi, 0); 3734 ddi_put32(cmd->frame_dma_obj.acc_handle, 3735 &initq_info->reply_queue_start_phys_addr_lo, 3736 instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address + 8); 3737 3738 ddi_put8(cmd->frame_dma_obj.acc_handle, 3739 &init_frame->cmd, MFI_CMD_OP_INIT); 3740 ddi_put8(cmd->frame_dma_obj.acc_handle, &init_frame->cmd_status, 3741 MFI_CMD_STATUS_POLL_MODE); 3742 ddi_put16(cmd->frame_dma_obj.acc_handle, &init_frame->flags, 0); 3743 ddi_put32(cmd->frame_dma_obj.acc_handle, 3744 &init_frame->queue_info_new_phys_addr_lo, 3745 cmd->frame_phys_addr + 64); 3746 ddi_put32(cmd->frame_dma_obj.acc_handle, 3747 &init_frame->queue_info_new_phys_addr_hi, 0); 3748 3749 ddi_put32(cmd->frame_dma_obj.acc_handle, &init_frame->data_xfer_len, 3750 sizeof (struct mrsas_init_queue_info)); 3751 3752 cmd->frame_count = 1; 3753 3754 /* issue the init frame in polled mode */ 3755 if (instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) { 3756 con_log(CL_ANN1, (CE_WARN, 3757 "mrsas_issue_init_mfi():failed to " 3758 "init firmware")); 3759 return_mfi_app_pkt(instance, cmd); 3760 return (DDI_FAILURE); 3761 } 3762 3763 if (mrsas_common_check(instance, cmd) != DDI_SUCCESS) { 3764 return_mfi_pkt(instance, cmd); 3765 return (DDI_FAILURE); 3766 } 3767 3768 return_mfi_app_pkt(instance, cmd); 3769 con_log(CL_ANN1, (CE_CONT, "mrsas_issue_init_mfi: Done")); 3770 3771 return (DDI_SUCCESS); 3772 } 3773 /* 3774 * mfi_state_transition_to_ready : Move the FW to READY state 3775 * 3776 * @reg_set : MFI register set 3777 */ 3778 int 3779 mfi_state_transition_to_ready(struct mrsas_instance *instance) 3780 { 3781 int i; 3782 uint8_t max_wait; 3783 uint32_t fw_ctrl = 0; 3784 uint32_t fw_state; 3785 uint32_t cur_state; 3786 uint32_t cur_abs_reg_val; 3787 uint32_t prev_abs_reg_val; 3788 uint32_t status; 3789 3790 cur_abs_reg_val = 3791 instance->func_ptr->read_fw_status_reg(instance); 3792 fw_state = 3793 cur_abs_reg_val & MFI_STATE_MASK; 3794 con_log(CL_ANN1, (CE_CONT, 3795 "mfi_state_transition_to_ready:FW state = 0x%x", fw_state)); 3796 3797 while (fw_state != MFI_STATE_READY) { 3798 con_log(CL_ANN, (CE_CONT, 3799 "mfi_state_transition_to_ready:FW state%x", fw_state)); 3800 3801 switch (fw_state) { 3802 case MFI_STATE_FAULT: 3803 con_log(CL_ANN, (CE_NOTE, 3804 "mr_sas: FW in FAULT state!!")); 3805 3806 return (ENODEV); 3807 case MFI_STATE_WAIT_HANDSHAKE: 3808 /* set the CLR bit in IMR0 */ 3809 con_log(CL_ANN1, (CE_NOTE, 3810 "mr_sas: FW waiting for HANDSHAKE")); 3811 /* 3812 * PCI_Hot Plug: MFI F/W requires 3813 * (MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG) 3814 * to be set 3815 */ 3816 /* WR_IB_MSG_0(MFI_INIT_CLEAR_HANDSHAKE, instance); */ 3817 if (!instance->tbolt) { 3818 WR_IB_DOORBELL(MFI_INIT_CLEAR_HANDSHAKE | 3819 MFI_INIT_HOTPLUG, instance); 3820 } else { 3821 WR_RESERVED0_REGISTER(MFI_INIT_CLEAR_HANDSHAKE | 3822 MFI_INIT_HOTPLUG, instance); 3823 } 3824 max_wait = (instance->tbolt == 1) ? 180 : 2; 3825 cur_state = MFI_STATE_WAIT_HANDSHAKE; 3826 break; 3827 case MFI_STATE_BOOT_MESSAGE_PENDING: 3828 /* set the CLR bit in IMR0 */ 3829 con_log(CL_ANN1, (CE_NOTE, 3830 "mr_sas: FW state boot message pending")); 3831 /* 3832 * PCI_Hot Plug: MFI F/W requires 3833 * (MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG) 3834 * to be set 3835 */ 3836 if (!instance->tbolt) { 3837 WR_IB_DOORBELL(MFI_INIT_HOTPLUG, instance); 3838 } else { 3839 WR_RESERVED0_REGISTER(MFI_INIT_HOTPLUG, 3840 instance); 3841 } 3842 max_wait = (instance->tbolt == 1) ? 180 : 10; 3843 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING; 3844 break; 3845 case MFI_STATE_OPERATIONAL: 3846 /* bring it to READY state; assuming max wait 2 secs */ 3847 instance->func_ptr->disable_intr(instance); 3848 con_log(CL_ANN1, (CE_NOTE, 3849 "mr_sas: FW in OPERATIONAL state")); 3850 /* 3851 * PCI_Hot Plug: MFI F/W requires 3852 * (MFI_INIT_READY | MFI_INIT_MFIMODE | MFI_INIT_ABORT) 3853 * to be set 3854 */ 3855 /* WR_IB_DOORBELL(MFI_INIT_READY, instance); */ 3856 if (!instance->tbolt) { 3857 WR_IB_DOORBELL(MFI_RESET_FLAGS, instance); 3858 } else { 3859 WR_RESERVED0_REGISTER(MFI_RESET_FLAGS, 3860 instance); 3861 3862 for (i = 0; i < (10 * 1000); i++) { 3863 status = 3864 RD_RESERVED0_REGISTER(instance); 3865 if (status & 1) { 3866 delay(1 * 3867 drv_usectohz(MILLISEC)); 3868 } else { 3869 break; 3870 } 3871 } 3872 3873 } 3874 max_wait = (instance->tbolt == 1) ? 180 : 10; 3875 cur_state = MFI_STATE_OPERATIONAL; 3876 break; 3877 case MFI_STATE_UNDEFINED: 3878 /* this state should not last for more than 2 seconds */ 3879 con_log(CL_ANN1, (CE_NOTE, "FW state undefined")); 3880 3881 max_wait = (instance->tbolt == 1) ? 180 : 2; 3882 cur_state = MFI_STATE_UNDEFINED; 3883 break; 3884 case MFI_STATE_BB_INIT: 3885 max_wait = (instance->tbolt == 1) ? 180 : 2; 3886 cur_state = MFI_STATE_BB_INIT; 3887 break; 3888 case MFI_STATE_FW_INIT: 3889 max_wait = (instance->tbolt == 1) ? 180 : 2; 3890 cur_state = MFI_STATE_FW_INIT; 3891 break; 3892 case MFI_STATE_FW_INIT_2: 3893 max_wait = 180; 3894 cur_state = MFI_STATE_FW_INIT_2; 3895 break; 3896 case MFI_STATE_DEVICE_SCAN: 3897 max_wait = 180; 3898 cur_state = MFI_STATE_DEVICE_SCAN; 3899 prev_abs_reg_val = cur_abs_reg_val; 3900 con_log(CL_NONE, (CE_NOTE, 3901 "Device scan in progress ...\n")); 3902 break; 3903 case MFI_STATE_FLUSH_CACHE: 3904 max_wait = 180; 3905 cur_state = MFI_STATE_FLUSH_CACHE; 3906 break; 3907 default: 3908 con_log(CL_ANN1, (CE_NOTE, 3909 "mr_sas: Unknown state 0x%x", fw_state)); 3910 return (ENODEV); 3911 } 3912 3913 /* the cur_state should not last for more than max_wait secs */ 3914 for (i = 0; i < (max_wait * MILLISEC); i++) { 3915 /* fw_state = RD_OB_MSG_0(instance) & MFI_STATE_MASK; */ 3916 cur_abs_reg_val = 3917 instance->func_ptr->read_fw_status_reg(instance); 3918 fw_state = cur_abs_reg_val & MFI_STATE_MASK; 3919 3920 if (fw_state == cur_state) { 3921 delay(1 * drv_usectohz(MILLISEC)); 3922 } else { 3923 break; 3924 } 3925 } 3926 if (fw_state == MFI_STATE_DEVICE_SCAN) { 3927 if (prev_abs_reg_val != cur_abs_reg_val) { 3928 continue; 3929 } 3930 } 3931 3932 /* return error if fw_state hasn't changed after max_wait */ 3933 if (fw_state == cur_state) { 3934 con_log(CL_ANN1, (CE_WARN, 3935 "FW state hasn't changed in %d secs", max_wait)); 3936 return (ENODEV); 3937 } 3938 }; 3939 3940 if (!instance->tbolt) { 3941 fw_ctrl = RD_IB_DOORBELL(instance); 3942 con_log(CL_ANN1, (CE_CONT, 3943 "mfi_state_transition_to_ready:FW ctrl = 0x%x", fw_ctrl)); 3944 3945 /* 3946 * Write 0xF to the doorbell register to do the following. 3947 * - Abort all outstanding commands (bit 0). 3948 * - Transition from OPERATIONAL to READY state (bit 1). 3949 * - Discard (possible) low MFA posted in 64-bit mode (bit-2). 3950 * - Set to release FW to continue running (i.e. BIOS handshake 3951 * (bit 3). 3952 */ 3953 WR_IB_DOORBELL(0xF, instance); 3954 } 3955 3956 if (mrsas_check_acc_handle(instance->regmap_handle) != DDI_SUCCESS) { 3957 return (EIO); 3958 } 3959 3960 return (DDI_SUCCESS); 3961 } 3962 3963 /* 3964 * get_seq_num 3965 */ 3966 static int 3967 get_seq_num(struct mrsas_instance *instance, 3968 struct mrsas_evt_log_info *eli) 3969 { 3970 int ret = DDI_SUCCESS; 3971 3972 dma_obj_t dcmd_dma_obj; 3973 struct mrsas_cmd *cmd; 3974 struct mrsas_dcmd_frame *dcmd; 3975 struct mrsas_evt_log_info *eli_tmp; 3976 if (instance->tbolt) { 3977 cmd = get_raid_msg_mfi_pkt(instance); 3978 } else { 3979 cmd = get_mfi_pkt(instance); 3980 } 3981 3982 if (!cmd) { 3983 cmn_err(CE_WARN, "mr_sas: failed to get a cmd"); 3984 DTRACE_PROBE2(seq_num_mfi_err, uint16_t, 3985 instance->fw_outstanding, uint16_t, instance->max_fw_cmds); 3986 return (ENOMEM); 3987 } 3988 3989 /* Clear the frame buffer and assign back the context id */ 3990 (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame)); 3991 ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context, 3992 cmd->index); 3993 3994 dcmd = &cmd->frame->dcmd; 3995 3996 /* allocate the data transfer buffer */ 3997 dcmd_dma_obj.size = sizeof (struct mrsas_evt_log_info); 3998 dcmd_dma_obj.dma_attr = mrsas_generic_dma_attr; 3999 dcmd_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 4000 dcmd_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU; 4001 dcmd_dma_obj.dma_attr.dma_attr_sgllen = 1; 4002 dcmd_dma_obj.dma_attr.dma_attr_align = 1; 4003 4004 if (mrsas_alloc_dma_obj(instance, &dcmd_dma_obj, 4005 (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) { 4006 cmn_err(CE_WARN, 4007 "get_seq_num: could not allocate data transfer buffer."); 4008 return (DDI_FAILURE); 4009 } 4010 4011 (void) memset(dcmd_dma_obj.buffer, 0, 4012 sizeof (struct mrsas_evt_log_info)); 4013 4014 (void) memset(dcmd->mbox.b, 0, DCMD_MBOX_SZ); 4015 4016 ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd, MFI_CMD_OP_DCMD); 4017 ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd_status, 0); 4018 ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->sge_count, 1); 4019 ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->flags, 4020 MFI_FRAME_DIR_READ); 4021 ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->timeout, 0); 4022 ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->data_xfer_len, 4023 sizeof (struct mrsas_evt_log_info)); 4024 ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->opcode, 4025 MR_DCMD_CTRL_EVENT_GET_INFO); 4026 ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].length, 4027 sizeof (struct mrsas_evt_log_info)); 4028 ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].phys_addr, 4029 dcmd_dma_obj.dma_cookie[0].dmac_address); 4030 4031 cmd->sync_cmd = MRSAS_TRUE; 4032 cmd->frame_count = 1; 4033 4034 if (instance->tbolt) { 4035 mr_sas_tbolt_build_mfi_cmd(instance, cmd); 4036 } 4037 4038 if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) { 4039 cmn_err(CE_WARN, "get_seq_num: " 4040 "failed to issue MRSAS_DCMD_CTRL_EVENT_GET_INFO"); 4041 ret = DDI_FAILURE; 4042 } else { 4043 eli_tmp = (struct mrsas_evt_log_info *)dcmd_dma_obj.buffer; 4044 eli->newest_seq_num = ddi_get32(cmd->frame_dma_obj.acc_handle, 4045 &eli_tmp->newest_seq_num); 4046 ret = DDI_SUCCESS; 4047 } 4048 4049 if (mrsas_free_dma_obj(instance, dcmd_dma_obj) != DDI_SUCCESS) 4050 ret = DDI_FAILURE; 4051 4052 if (instance->tbolt) { 4053 return_raid_msg_mfi_pkt(instance, cmd); 4054 } else { 4055 return_mfi_pkt(instance, cmd); 4056 } 4057 4058 return (ret); 4059 } 4060 4061 /* 4062 * start_mfi_aen 4063 */ 4064 static int 4065 start_mfi_aen(struct mrsas_instance *instance) 4066 { 4067 int ret = 0; 4068 4069 struct mrsas_evt_log_info eli; 4070 union mrsas_evt_class_locale class_locale; 4071 4072 /* get the latest sequence number from FW */ 4073 (void) memset(&eli, 0, sizeof (struct mrsas_evt_log_info)); 4074 4075 if (get_seq_num(instance, &eli)) { 4076 cmn_err(CE_WARN, "start_mfi_aen: failed to get seq num"); 4077 return (-1); 4078 } 4079 4080 /* register AEN with FW for latest sequence number plus 1 */ 4081 class_locale.members.reserved = 0; 4082 class_locale.members.locale = LE_16(MR_EVT_LOCALE_ALL); 4083 class_locale.members.class = MR_EVT_CLASS_INFO; 4084 class_locale.word = LE_32(class_locale.word); 4085 ret = register_mfi_aen(instance, eli.newest_seq_num + 1, 4086 class_locale.word); 4087 4088 if (ret) { 4089 cmn_err(CE_WARN, "start_mfi_aen: aen registration failed"); 4090 return (-1); 4091 } 4092 4093 4094 return (ret); 4095 } 4096 4097 /* 4098 * flush_cache 4099 */ 4100 static void 4101 flush_cache(struct mrsas_instance *instance) 4102 { 4103 struct mrsas_cmd *cmd = NULL; 4104 struct mrsas_dcmd_frame *dcmd; 4105 if (instance->tbolt) { 4106 cmd = get_raid_msg_mfi_pkt(instance); 4107 } else { 4108 cmd = get_mfi_pkt(instance); 4109 } 4110 4111 if (!cmd) { 4112 con_log(CL_ANN1, (CE_WARN, 4113 "flush_cache():Failed to get a cmd for flush_cache")); 4114 DTRACE_PROBE2(flush_cache_err, uint16_t, 4115 instance->fw_outstanding, uint16_t, instance->max_fw_cmds); 4116 return; 4117 } 4118 4119 /* Clear the frame buffer and assign back the context id */ 4120 (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame)); 4121 ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context, 4122 cmd->index); 4123 4124 dcmd = &cmd->frame->dcmd; 4125 4126 (void) memset(dcmd->mbox.b, 0, DCMD_MBOX_SZ); 4127 4128 ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd, MFI_CMD_OP_DCMD); 4129 ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd_status, 0x0); 4130 ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->sge_count, 0); 4131 ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->flags, 4132 MFI_FRAME_DIR_NONE); 4133 ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->timeout, 0); 4134 ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->data_xfer_len, 0); 4135 ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->opcode, 4136 MR_DCMD_CTRL_CACHE_FLUSH); 4137 ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->mbox.b[0], 4138 MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE); 4139 4140 cmd->frame_count = 1; 4141 4142 if (instance->tbolt) { 4143 mr_sas_tbolt_build_mfi_cmd(instance, cmd); 4144 } 4145 4146 if (instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) { 4147 con_log(CL_ANN1, (CE_WARN, 4148 "flush_cache: failed to issue MFI_DCMD_CTRL_CACHE_FLUSH")); 4149 } 4150 con_log(CL_ANN1, (CE_CONT, "flush_cache done")); 4151 if (instance->tbolt) { 4152 return_raid_msg_mfi_pkt(instance, cmd); 4153 } else { 4154 return_mfi_pkt(instance, cmd); 4155 } 4156 4157 } 4158 4159 /* 4160 * service_mfi_aen- Completes an AEN command 4161 * @instance: Adapter soft state 4162 * @cmd: Command to be completed 4163 * 4164 */ 4165 void 4166 service_mfi_aen(struct mrsas_instance *instance, struct mrsas_cmd *cmd) 4167 { 4168 uint32_t seq_num; 4169 struct mrsas_evt_detail *evt_detail = 4170 (struct mrsas_evt_detail *)instance->mfi_evt_detail_obj.buffer; 4171 int rval = 0; 4172 int tgt = 0; 4173 uint8_t dtype; 4174 #ifdef PDSUPPORT 4175 mrsas_pd_address_t *pd_addr; 4176 #endif 4177 ddi_acc_handle_t acc_handle; 4178 4179 con_log(CL_ANN, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 4180 4181 acc_handle = cmd->frame_dma_obj.acc_handle; 4182 cmd->cmd_status = ddi_get8(acc_handle, &cmd->frame->io.cmd_status); 4183 if (cmd->cmd_status == ENODATA) { 4184 cmd->cmd_status = 0; 4185 } 4186 4187 /* 4188 * log the MFI AEN event to the sysevent queue so that 4189 * application will get noticed 4190 */ 4191 if (ddi_log_sysevent(instance->dip, DDI_VENDOR_LSI, "LSIMEGA", "SAS", 4192 NULL, NULL, DDI_NOSLEEP) != DDI_SUCCESS) { 4193 int instance_no = ddi_get_instance(instance->dip); 4194 con_log(CL_ANN, (CE_WARN, 4195 "mr_sas%d: Failed to log AEN event", instance_no)); 4196 } 4197 /* 4198 * Check for any ld devices that has changed state. i.e. online 4199 * or offline. 4200 */ 4201 con_log(CL_ANN1, (CE_CONT, 4202 "AEN: code = %x class = %x locale = %x args = %x", 4203 ddi_get32(acc_handle, &evt_detail->code), 4204 evt_detail->cl.members.class, 4205 ddi_get16(acc_handle, &evt_detail->cl.members.locale), 4206 ddi_get8(acc_handle, &evt_detail->arg_type))); 4207 4208 switch (ddi_get32(acc_handle, &evt_detail->code)) { 4209 case MR_EVT_CFG_CLEARED: { 4210 for (tgt = 0; tgt < MRDRV_MAX_LD; tgt++) { 4211 if (instance->mr_ld_list[tgt].dip != NULL) { 4212 mutex_enter(&instance->config_dev_mtx); 4213 instance->mr_ld_list[tgt].flag = 4214 (uint8_t)~MRDRV_TGT_VALID; 4215 mutex_exit(&instance->config_dev_mtx); 4216 rval = mrsas_service_evt(instance, tgt, 0, 4217 MRSAS_EVT_UNCONFIG_TGT, NULL); 4218 con_log(CL_ANN1, (CE_WARN, 4219 "mr_sas: CFG CLEARED AEN rval = %d " 4220 "tgt id = %d", rval, tgt)); 4221 } 4222 } 4223 break; 4224 } 4225 4226 case MR_EVT_LD_DELETED: { 4227 tgt = ddi_get16(acc_handle, &evt_detail->args.ld.target_id); 4228 mutex_enter(&instance->config_dev_mtx); 4229 instance->mr_ld_list[tgt].flag = (uint8_t)~MRDRV_TGT_VALID; 4230 mutex_exit(&instance->config_dev_mtx); 4231 rval = mrsas_service_evt(instance, 4232 ddi_get16(acc_handle, &evt_detail->args.ld.target_id), 0, 4233 MRSAS_EVT_UNCONFIG_TGT, NULL); 4234 con_log(CL_ANN1, (CE_WARN, "mr_sas: LD DELETED AEN rval = %d " 4235 "tgt id = %d index = %d", rval, 4236 ddi_get16(acc_handle, &evt_detail->args.ld.target_id), 4237 ddi_get8(acc_handle, &evt_detail->args.ld.ld_index))); 4238 break; 4239 } /* End of MR_EVT_LD_DELETED */ 4240 4241 case MR_EVT_LD_CREATED: { 4242 rval = mrsas_service_evt(instance, 4243 ddi_get16(acc_handle, &evt_detail->args.ld.target_id), 0, 4244 MRSAS_EVT_CONFIG_TGT, NULL); 4245 con_log(CL_ANN1, (CE_WARN, "mr_sas: LD CREATED AEN rval = %d " 4246 "tgt id = %d index = %d", rval, 4247 ddi_get16(acc_handle, &evt_detail->args.ld.target_id), 4248 ddi_get8(acc_handle, &evt_detail->args.ld.ld_index))); 4249 break; 4250 } /* End of MR_EVT_LD_CREATED */ 4251 4252 #ifdef PDSUPPORT 4253 case MR_EVT_PD_REMOVED_EXT: { 4254 if (instance->tbolt) { 4255 pd_addr = &evt_detail->args.pd_addr; 4256 dtype = pd_addr->scsi_dev_type; 4257 con_log(CL_DLEVEL1, (CE_NOTE, 4258 " MR_EVT_PD_REMOVED_EXT: dtype = %x," 4259 " arg_type = %d ", dtype, evt_detail->arg_type)); 4260 tgt = ddi_get16(acc_handle, 4261 &evt_detail->args.pd.device_id); 4262 mutex_enter(&instance->config_dev_mtx); 4263 instance->mr_tbolt_pd_list[tgt].flag = 4264 (uint8_t)~MRDRV_TGT_VALID; 4265 mutex_exit(&instance->config_dev_mtx); 4266 rval = mrsas_service_evt(instance, ddi_get16( 4267 acc_handle, &evt_detail->args.pd.device_id), 4268 1, MRSAS_EVT_UNCONFIG_TGT, NULL); 4269 con_log(CL_ANN1, (CE_WARN, "mr_sas: PD_REMOVED:" 4270 "rval = %d tgt id = %d ", rval, 4271 ddi_get16(acc_handle, 4272 &evt_detail->args.pd.device_id))); 4273 } 4274 break; 4275 } /* End of MR_EVT_PD_REMOVED_EXT */ 4276 4277 case MR_EVT_PD_INSERTED_EXT: { 4278 if (instance->tbolt) { 4279 rval = mrsas_service_evt(instance, 4280 ddi_get16(acc_handle, 4281 &evt_detail->args.pd.device_id), 4282 1, MRSAS_EVT_CONFIG_TGT, NULL); 4283 con_log(CL_ANN1, (CE_WARN, "mr_sas: PD_INSERTEDi_EXT:" 4284 "rval = %d tgt id = %d ", rval, 4285 ddi_get16(acc_handle, 4286 &evt_detail->args.pd.device_id))); 4287 } 4288 break; 4289 } /* End of MR_EVT_PD_INSERTED_EXT */ 4290 4291 case MR_EVT_PD_STATE_CHANGE: { 4292 if (instance->tbolt) { 4293 tgt = ddi_get16(acc_handle, 4294 &evt_detail->args.pd.device_id); 4295 if ((evt_detail->args.pd_state.prevState == 4296 PD_SYSTEM) && 4297 (evt_detail->args.pd_state.newState != PD_SYSTEM)) { 4298 mutex_enter(&instance->config_dev_mtx); 4299 instance->mr_tbolt_pd_list[tgt].flag = 4300 (uint8_t)~MRDRV_TGT_VALID; 4301 mutex_exit(&instance->config_dev_mtx); 4302 rval = mrsas_service_evt(instance, 4303 ddi_get16(acc_handle, 4304 &evt_detail->args.pd.device_id), 4305 1, MRSAS_EVT_UNCONFIG_TGT, NULL); 4306 con_log(CL_ANN1, (CE_WARN, "mr_sas: PD_REMOVED:" 4307 "rval = %d tgt id = %d ", rval, 4308 ddi_get16(acc_handle, 4309 &evt_detail->args.pd.device_id))); 4310 break; 4311 } 4312 if ((evt_detail->args.pd_state.prevState 4313 == UNCONFIGURED_GOOD) && 4314 (evt_detail->args.pd_state.newState == PD_SYSTEM)) { 4315 rval = mrsas_service_evt(instance, 4316 ddi_get16(acc_handle, 4317 &evt_detail->args.pd.device_id), 4318 1, MRSAS_EVT_CONFIG_TGT, NULL); 4319 con_log(CL_ANN1, (CE_WARN, 4320 "mr_sas: PD_INSERTED: rval = %d " 4321 " tgt id = %d ", rval, 4322 ddi_get16(acc_handle, 4323 &evt_detail->args.pd.device_id))); 4324 break; 4325 } 4326 } 4327 break; 4328 } 4329 #endif 4330 4331 } /* End of Main Switch */ 4332 4333 /* get copy of seq_num and class/locale for re-registration */ 4334 seq_num = ddi_get32(acc_handle, &evt_detail->seq_num); 4335 seq_num++; 4336 (void) memset(instance->mfi_evt_detail_obj.buffer, 0, 4337 sizeof (struct mrsas_evt_detail)); 4338 4339 ddi_put8(acc_handle, &cmd->frame->dcmd.cmd_status, 0x0); 4340 ddi_put32(acc_handle, &cmd->frame->dcmd.mbox.w[0], seq_num); 4341 4342 instance->aen_seq_num = seq_num; 4343 4344 cmd->frame_count = 1; 4345 4346 cmd->retry_count_for_ocr = 0; 4347 cmd->drv_pkt_time = 0; 4348 4349 /* Issue the aen registration frame */ 4350 instance->func_ptr->issue_cmd(cmd, instance); 4351 } 4352 4353 /* 4354 * complete_cmd_in_sync_mode - Completes an internal command 4355 * @instance: Adapter soft state 4356 * @cmd: Command to be completed 4357 * 4358 * The issue_cmd_in_sync_mode() function waits for a command to complete 4359 * after it issues a command. This function wakes up that waiting routine by 4360 * calling wake_up() on the wait queue. 4361 */ 4362 static void 4363 complete_cmd_in_sync_mode(struct mrsas_instance *instance, 4364 struct mrsas_cmd *cmd) 4365 { 4366 cmd->cmd_status = ddi_get8(cmd->frame_dma_obj.acc_handle, 4367 &cmd->frame->io.cmd_status); 4368 4369 cmd->sync_cmd = MRSAS_FALSE; 4370 4371 con_log(CL_ANN1, (CE_NOTE, "complete_cmd_in_sync_mode called %p \n", 4372 (void *)cmd)); 4373 4374 mutex_enter(&instance->int_cmd_mtx); 4375 if (cmd->cmd_status == ENODATA) { 4376 cmd->cmd_status = 0; 4377 } 4378 cv_broadcast(&instance->int_cmd_cv); 4379 mutex_exit(&instance->int_cmd_mtx); 4380 4381 } 4382 4383 /* 4384 * Call this function inside mrsas_softintr. 4385 * mrsas_initiate_ocr_if_fw_is_faulty - Initiates OCR if FW status is faulty 4386 * @instance: Adapter soft state 4387 */ 4388 4389 static uint32_t 4390 mrsas_initiate_ocr_if_fw_is_faulty(struct mrsas_instance *instance) 4391 { 4392 uint32_t cur_abs_reg_val; 4393 uint32_t fw_state; 4394 4395 cur_abs_reg_val = instance->func_ptr->read_fw_status_reg(instance); 4396 fw_state = cur_abs_reg_val & MFI_STATE_MASK; 4397 if (fw_state == MFI_STATE_FAULT) { 4398 if (instance->disable_online_ctrl_reset == 1) { 4399 cmn_err(CE_WARN, 4400 "mrsas_initiate_ocr_if_fw_is_faulty: " 4401 "FW in Fault state, detected in ISR: " 4402 "FW doesn't support ocr "); 4403 4404 return (ADAPTER_RESET_NOT_REQUIRED); 4405 } else { 4406 con_log(CL_ANN, (CE_NOTE, 4407 "mrsas_initiate_ocr_if_fw_is_faulty: FW in Fault " 4408 "state, detected in ISR: FW supports ocr ")); 4409 4410 return (ADAPTER_RESET_REQUIRED); 4411 } 4412 } 4413 4414 return (ADAPTER_RESET_NOT_REQUIRED); 4415 } 4416 4417 /* 4418 * mrsas_softintr - The Software ISR 4419 * @param arg : HBA soft state 4420 * 4421 * called from high-level interrupt if hi-level interrupt are not there, 4422 * otherwise triggered as a soft interrupt 4423 */ 4424 static uint_t 4425 mrsas_softintr(struct mrsas_instance *instance) 4426 { 4427 struct scsi_pkt *pkt; 4428 struct scsa_cmd *acmd; 4429 struct mrsas_cmd *cmd; 4430 struct mlist_head *pos, *next; 4431 mlist_t process_list; 4432 struct mrsas_header *hdr; 4433 struct scsi_arq_status *arqstat; 4434 4435 con_log(CL_ANN1, (CE_NOTE, "mrsas_softintr() called.")); 4436 4437 ASSERT(instance); 4438 4439 mutex_enter(&instance->completed_pool_mtx); 4440 4441 if (mlist_empty(&instance->completed_pool_list)) { 4442 mutex_exit(&instance->completed_pool_mtx); 4443 return (DDI_INTR_CLAIMED); 4444 } 4445 4446 instance->softint_running = 1; 4447 4448 INIT_LIST_HEAD(&process_list); 4449 mlist_splice(&instance->completed_pool_list, &process_list); 4450 INIT_LIST_HEAD(&instance->completed_pool_list); 4451 4452 mutex_exit(&instance->completed_pool_mtx); 4453 4454 /* perform all callbacks first, before releasing the SCBs */ 4455 mlist_for_each_safe(pos, next, &process_list) { 4456 cmd = mlist_entry(pos, struct mrsas_cmd, list); 4457 4458 /* syncronize the Cmd frame for the controller */ 4459 (void) ddi_dma_sync(cmd->frame_dma_obj.dma_handle, 4460 0, 0, DDI_DMA_SYNC_FORCPU); 4461 4462 if (mrsas_check_dma_handle(cmd->frame_dma_obj.dma_handle) != 4463 DDI_SUCCESS) { 4464 mrsas_fm_ereport(instance, DDI_FM_DEVICE_NO_RESPONSE); 4465 ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST); 4466 con_log(CL_ANN1, (CE_WARN, 4467 "mrsas_softintr: " 4468 "FMA check reports DMA handle failure")); 4469 return (DDI_INTR_CLAIMED); 4470 } 4471 4472 hdr = &cmd->frame->hdr; 4473 4474 /* remove the internal command from the process list */ 4475 mlist_del_init(&cmd->list); 4476 4477 switch (ddi_get8(cmd->frame_dma_obj.acc_handle, &hdr->cmd)) { 4478 case MFI_CMD_OP_PD_SCSI: 4479 case MFI_CMD_OP_LD_SCSI: 4480 case MFI_CMD_OP_LD_READ: 4481 case MFI_CMD_OP_LD_WRITE: 4482 /* 4483 * MFI_CMD_OP_PD_SCSI and MFI_CMD_OP_LD_SCSI 4484 * could have been issued either through an 4485 * IO path or an IOCTL path. If it was via IOCTL, 4486 * we will send it to internal completion. 4487 */ 4488 if (cmd->sync_cmd == MRSAS_TRUE) { 4489 complete_cmd_in_sync_mode(instance, cmd); 4490 break; 4491 } 4492 4493 /* regular commands */ 4494 acmd = cmd->cmd; 4495 pkt = CMD2PKT(acmd); 4496 4497 if (acmd->cmd_flags & CFLAG_DMAVALID) { 4498 if (acmd->cmd_flags & CFLAG_CONSISTENT) { 4499 (void) ddi_dma_sync(acmd->cmd_dmahandle, 4500 acmd->cmd_dma_offset, 4501 acmd->cmd_dma_len, 4502 DDI_DMA_SYNC_FORCPU); 4503 } 4504 } 4505 4506 pkt->pkt_reason = CMD_CMPLT; 4507 pkt->pkt_statistics = 0; 4508 pkt->pkt_state = STATE_GOT_BUS 4509 | STATE_GOT_TARGET | STATE_SENT_CMD 4510 | STATE_XFERRED_DATA | STATE_GOT_STATUS; 4511 4512 con_log(CL_ANN, (CE_CONT, 4513 "CDB[0] = %x completed for %s: size %lx context %x", 4514 pkt->pkt_cdbp[0], ((acmd->islogical) ? "LD" : "PD"), 4515 acmd->cmd_dmacount, hdr->context)); 4516 DTRACE_PROBE3(softintr_cdb, uint8_t, pkt->pkt_cdbp[0], 4517 uint_t, acmd->cmd_cdblen, ulong_t, 4518 acmd->cmd_dmacount); 4519 4520 if (pkt->pkt_cdbp[0] == SCMD_INQUIRY) { 4521 struct scsi_inquiry *inq; 4522 4523 if (acmd->cmd_dmacount != 0) { 4524 bp_mapin(acmd->cmd_buf); 4525 inq = (struct scsi_inquiry *) 4526 acmd->cmd_buf->b_un.b_addr; 4527 4528 /* don't expose physical drives to OS */ 4529 if (acmd->islogical && 4530 (hdr->cmd_status == MFI_STAT_OK)) { 4531 display_scsi_inquiry( 4532 (caddr_t)inq); 4533 } else if ((hdr->cmd_status == 4534 MFI_STAT_OK) && inq->inq_dtype == 4535 DTYPE_DIRECT) { 4536 4537 display_scsi_inquiry( 4538 (caddr_t)inq); 4539 4540 /* for physical disk */ 4541 hdr->cmd_status = 4542 MFI_STAT_DEVICE_NOT_FOUND; 4543 } 4544 } 4545 } 4546 4547 DTRACE_PROBE2(softintr_done, uint8_t, hdr->cmd, 4548 uint8_t, hdr->cmd_status); 4549 4550 switch (hdr->cmd_status) { 4551 case MFI_STAT_OK: 4552 pkt->pkt_scbp[0] = STATUS_GOOD; 4553 break; 4554 case MFI_STAT_LD_CC_IN_PROGRESS: 4555 case MFI_STAT_LD_RECON_IN_PROGRESS: 4556 pkt->pkt_scbp[0] = STATUS_GOOD; 4557 break; 4558 case MFI_STAT_LD_INIT_IN_PROGRESS: 4559 con_log(CL_ANN, 4560 (CE_WARN, "Initialization in Progress")); 4561 pkt->pkt_reason = CMD_TRAN_ERR; 4562 4563 break; 4564 case MFI_STAT_SCSI_DONE_WITH_ERROR: 4565 con_log(CL_ANN, (CE_CONT, "scsi_done error")); 4566 4567 pkt->pkt_reason = CMD_CMPLT; 4568 ((struct scsi_status *) 4569 pkt->pkt_scbp)->sts_chk = 1; 4570 4571 if (pkt->pkt_cdbp[0] == SCMD_TEST_UNIT_READY) { 4572 con_log(CL_ANN, 4573 (CE_WARN, "TEST_UNIT_READY fail")); 4574 } else { 4575 pkt->pkt_state |= STATE_ARQ_DONE; 4576 arqstat = (void *)(pkt->pkt_scbp); 4577 arqstat->sts_rqpkt_reason = CMD_CMPLT; 4578 arqstat->sts_rqpkt_resid = 0; 4579 arqstat->sts_rqpkt_state |= 4580 STATE_GOT_BUS | STATE_GOT_TARGET 4581 | STATE_SENT_CMD 4582 | STATE_XFERRED_DATA; 4583 *(uint8_t *)&arqstat->sts_rqpkt_status = 4584 STATUS_GOOD; 4585 ddi_rep_get8( 4586 cmd->frame_dma_obj.acc_handle, 4587 (uint8_t *) 4588 &(arqstat->sts_sensedata), 4589 cmd->sense, 4590 sizeof (struct scsi_extended_sense), 4591 DDI_DEV_AUTOINCR); 4592 } 4593 break; 4594 case MFI_STAT_LD_OFFLINE: 4595 case MFI_STAT_DEVICE_NOT_FOUND: 4596 con_log(CL_ANN, (CE_CONT, 4597 "mrsas_softintr:device not found error")); 4598 pkt->pkt_reason = CMD_DEV_GONE; 4599 pkt->pkt_statistics = STAT_DISCON; 4600 break; 4601 case MFI_STAT_LD_LBA_OUT_OF_RANGE: 4602 pkt->pkt_state |= STATE_ARQ_DONE; 4603 pkt->pkt_reason = CMD_CMPLT; 4604 ((struct scsi_status *) 4605 pkt->pkt_scbp)->sts_chk = 1; 4606 4607 arqstat = (void *)(pkt->pkt_scbp); 4608 arqstat->sts_rqpkt_reason = CMD_CMPLT; 4609 arqstat->sts_rqpkt_resid = 0; 4610 arqstat->sts_rqpkt_state |= STATE_GOT_BUS 4611 | STATE_GOT_TARGET | STATE_SENT_CMD 4612 | STATE_XFERRED_DATA; 4613 *(uint8_t *)&arqstat->sts_rqpkt_status = 4614 STATUS_GOOD; 4615 4616 arqstat->sts_sensedata.es_valid = 1; 4617 arqstat->sts_sensedata.es_key = 4618 KEY_ILLEGAL_REQUEST; 4619 arqstat->sts_sensedata.es_class = 4620 CLASS_EXTENDED_SENSE; 4621 4622 /* 4623 * LOGICAL BLOCK ADDRESS OUT OF RANGE: 4624 * ASC: 0x21h; ASCQ: 0x00h; 4625 */ 4626 arqstat->sts_sensedata.es_add_code = 0x21; 4627 arqstat->sts_sensedata.es_qual_code = 0x00; 4628 4629 break; 4630 4631 default: 4632 con_log(CL_ANN, (CE_CONT, "Unknown status!")); 4633 pkt->pkt_reason = CMD_TRAN_ERR; 4634 4635 break; 4636 } 4637 4638 atomic_add_16(&instance->fw_outstanding, (-1)); 4639 4640 (void) mrsas_common_check(instance, cmd); 4641 4642 if (acmd->cmd_dmahandle) { 4643 if (mrsas_check_dma_handle( 4644 acmd->cmd_dmahandle) != DDI_SUCCESS) { 4645 ddi_fm_service_impact(instance->dip, 4646 DDI_SERVICE_UNAFFECTED); 4647 pkt->pkt_reason = CMD_TRAN_ERR; 4648 pkt->pkt_statistics = 0; 4649 } 4650 } 4651 4652 /* Call the callback routine */ 4653 if (((pkt->pkt_flags & FLAG_NOINTR) == 0) && 4654 pkt->pkt_comp) { 4655 4656 con_log(CL_DLEVEL1, (CE_NOTE, "mrsas_softintr: " 4657 "posting to scsa cmd %p index %x pkt %p " 4658 "time %llx", (void *)cmd, cmd->index, 4659 (void *)pkt, gethrtime())); 4660 (*pkt->pkt_comp)(pkt); 4661 4662 } 4663 4664 return_mfi_pkt(instance, cmd); 4665 break; 4666 4667 case MFI_CMD_OP_SMP: 4668 case MFI_CMD_OP_STP: 4669 complete_cmd_in_sync_mode(instance, cmd); 4670 break; 4671 4672 case MFI_CMD_OP_DCMD: 4673 /* see if got an event notification */ 4674 if (ddi_get32(cmd->frame_dma_obj.acc_handle, 4675 &cmd->frame->dcmd.opcode) == 4676 MR_DCMD_CTRL_EVENT_WAIT) { 4677 if ((instance->aen_cmd == cmd) && 4678 (instance->aen_cmd->abort_aen)) { 4679 con_log(CL_ANN, (CE_WARN, 4680 "mrsas_softintr: " 4681 "aborted_aen returned")); 4682 } else { 4683 atomic_add_16(&instance->fw_outstanding, 4684 (-1)); 4685 service_mfi_aen(instance, cmd); 4686 } 4687 } else { 4688 complete_cmd_in_sync_mode(instance, cmd); 4689 } 4690 4691 break; 4692 4693 case MFI_CMD_OP_ABORT: 4694 con_log(CL_ANN, (CE_NOTE, "MFI_CMD_OP_ABORT complete")); 4695 /* 4696 * MFI_CMD_OP_ABORT successfully completed 4697 * in the synchronous mode 4698 */ 4699 complete_cmd_in_sync_mode(instance, cmd); 4700 break; 4701 4702 default: 4703 mrsas_fm_ereport(instance, DDI_FM_DEVICE_NO_RESPONSE); 4704 ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST); 4705 4706 if (cmd->pkt != NULL) { 4707 pkt = cmd->pkt; 4708 if (((pkt->pkt_flags & FLAG_NOINTR) == 0) && 4709 pkt->pkt_comp) { 4710 4711 con_log(CL_ANN1, (CE_CONT, "posting to " 4712 "scsa cmd %p index %x pkt %p" 4713 "time %llx, default ", (void *)cmd, 4714 cmd->index, (void *)pkt, 4715 gethrtime())); 4716 4717 (*pkt->pkt_comp)(pkt); 4718 4719 } 4720 } 4721 con_log(CL_ANN, (CE_WARN, "Cmd type unknown !")); 4722 break; 4723 } 4724 } 4725 4726 instance->softint_running = 0; 4727 4728 return (DDI_INTR_CLAIMED); 4729 } 4730 4731 /* 4732 * mrsas_alloc_dma_obj 4733 * 4734 * Allocate the memory and other resources for an dma object. 4735 */ 4736 int 4737 mrsas_alloc_dma_obj(struct mrsas_instance *instance, dma_obj_t *obj, 4738 uchar_t endian_flags) 4739 { 4740 int i; 4741 size_t alen = 0; 4742 uint_t cookie_cnt; 4743 struct ddi_device_acc_attr tmp_endian_attr; 4744 4745 tmp_endian_attr = endian_attr; 4746 tmp_endian_attr.devacc_attr_endian_flags = endian_flags; 4747 tmp_endian_attr.devacc_attr_access = DDI_DEFAULT_ACC; 4748 4749 i = ddi_dma_alloc_handle(instance->dip, &obj->dma_attr, 4750 DDI_DMA_SLEEP, NULL, &obj->dma_handle); 4751 if (i != DDI_SUCCESS) { 4752 4753 switch (i) { 4754 case DDI_DMA_BADATTR : 4755 con_log(CL_ANN, (CE_WARN, 4756 "Failed ddi_dma_alloc_handle- Bad attribute")); 4757 break; 4758 case DDI_DMA_NORESOURCES : 4759 con_log(CL_ANN, (CE_WARN, 4760 "Failed ddi_dma_alloc_handle- No Resources")); 4761 break; 4762 default : 4763 con_log(CL_ANN, (CE_WARN, 4764 "Failed ddi_dma_alloc_handle: " 4765 "unknown status %d", i)); 4766 break; 4767 } 4768 4769 return (-1); 4770 } 4771 4772 if ((ddi_dma_mem_alloc(obj->dma_handle, obj->size, &tmp_endian_attr, 4773 DDI_DMA_RDWR | DDI_DMA_STREAMING, DDI_DMA_SLEEP, NULL, 4774 &obj->buffer, &alen, &obj->acc_handle) != DDI_SUCCESS) || 4775 alen < obj->size) { 4776 4777 ddi_dma_free_handle(&obj->dma_handle); 4778 4779 con_log(CL_ANN, (CE_WARN, "Failed : ddi_dma_mem_alloc")); 4780 4781 return (-1); 4782 } 4783 4784 if (ddi_dma_addr_bind_handle(obj->dma_handle, NULL, obj->buffer, 4785 obj->size, DDI_DMA_RDWR | DDI_DMA_STREAMING, DDI_DMA_SLEEP, 4786 NULL, &obj->dma_cookie[0], &cookie_cnt) != DDI_SUCCESS) { 4787 4788 ddi_dma_mem_free(&obj->acc_handle); 4789 ddi_dma_free_handle(&obj->dma_handle); 4790 4791 con_log(CL_ANN, (CE_WARN, "Failed : ddi_dma_addr_bind_handle")); 4792 4793 return (-1); 4794 } 4795 4796 if (mrsas_check_dma_handle(obj->dma_handle) != DDI_SUCCESS) { 4797 ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST); 4798 return (-1); 4799 } 4800 4801 if (mrsas_check_acc_handle(obj->acc_handle) != DDI_SUCCESS) { 4802 ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST); 4803 return (-1); 4804 } 4805 4806 return (cookie_cnt); 4807 } 4808 4809 /* 4810 * mrsas_free_dma_obj(struct mrsas_instance *, dma_obj_t) 4811 * 4812 * De-allocate the memory and other resources for an dma object, which must 4813 * have been alloated by a previous call to mrsas_alloc_dma_obj() 4814 */ 4815 int 4816 mrsas_free_dma_obj(struct mrsas_instance *instance, dma_obj_t obj) 4817 { 4818 4819 if ((obj.dma_handle == NULL) || (obj.acc_handle == NULL)) { 4820 return (DDI_SUCCESS); 4821 } 4822 4823 /* 4824 * NOTE: These check-handle functions fail if *_handle == NULL, but 4825 * this function succeeds because of the previous check. 4826 */ 4827 if (mrsas_check_dma_handle(obj.dma_handle) != DDI_SUCCESS) { 4828 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED); 4829 return (DDI_FAILURE); 4830 } 4831 4832 if (mrsas_check_acc_handle(obj.acc_handle) != DDI_SUCCESS) { 4833 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED); 4834 return (DDI_FAILURE); 4835 } 4836 4837 (void) ddi_dma_unbind_handle(obj.dma_handle); 4838 ddi_dma_mem_free(&obj.acc_handle); 4839 ddi_dma_free_handle(&obj.dma_handle); 4840 obj.acc_handle = NULL; 4841 return (DDI_SUCCESS); 4842 } 4843 4844 /* 4845 * mrsas_dma_alloc(instance_t *, struct scsi_pkt *, struct buf *, 4846 * int, int (*)()) 4847 * 4848 * Allocate dma resources for a new scsi command 4849 */ 4850 int 4851 mrsas_dma_alloc(struct mrsas_instance *instance, struct scsi_pkt *pkt, 4852 struct buf *bp, int flags, int (*callback)()) 4853 { 4854 int dma_flags; 4855 int (*cb)(caddr_t); 4856 int i; 4857 4858 ddi_dma_attr_t tmp_dma_attr = mrsas_generic_dma_attr; 4859 struct scsa_cmd *acmd = PKT2CMD(pkt); 4860 4861 acmd->cmd_buf = bp; 4862 4863 if (bp->b_flags & B_READ) { 4864 acmd->cmd_flags &= ~CFLAG_DMASEND; 4865 dma_flags = DDI_DMA_READ; 4866 } else { 4867 acmd->cmd_flags |= CFLAG_DMASEND; 4868 dma_flags = DDI_DMA_WRITE; 4869 } 4870 4871 if (flags & PKT_CONSISTENT) { 4872 acmd->cmd_flags |= CFLAG_CONSISTENT; 4873 dma_flags |= DDI_DMA_CONSISTENT; 4874 } 4875 4876 if (flags & PKT_DMA_PARTIAL) { 4877 dma_flags |= DDI_DMA_PARTIAL; 4878 } 4879 4880 dma_flags |= DDI_DMA_REDZONE; 4881 4882 cb = (callback == NULL_FUNC) ? DDI_DMA_DONTWAIT : DDI_DMA_SLEEP; 4883 4884 tmp_dma_attr.dma_attr_sgllen = instance->max_num_sge; 4885 tmp_dma_attr.dma_attr_addr_hi = 0xffffffffffffffffull; 4886 if (instance->tbolt) { 4887 /* OCR-RESET FIX */ 4888 tmp_dma_attr.dma_attr_count_max = 4889 (U64)mrsas_tbolt_max_cap_maxxfer; /* limit to 256K */ 4890 tmp_dma_attr.dma_attr_maxxfer = 4891 (U64)mrsas_tbolt_max_cap_maxxfer; /* limit to 256K */ 4892 } 4893 4894 if ((i = ddi_dma_alloc_handle(instance->dip, &tmp_dma_attr, 4895 cb, 0, &acmd->cmd_dmahandle)) != DDI_SUCCESS) { 4896 switch (i) { 4897 case DDI_DMA_BADATTR: 4898 bioerror(bp, EFAULT); 4899 return (DDI_FAILURE); 4900 4901 case DDI_DMA_NORESOURCES: 4902 bioerror(bp, 0); 4903 return (DDI_FAILURE); 4904 4905 default: 4906 con_log(CL_ANN, (CE_PANIC, "ddi_dma_alloc_handle: " 4907 "impossible result (0x%x)", i)); 4908 bioerror(bp, EFAULT); 4909 return (DDI_FAILURE); 4910 } 4911 } 4912 4913 i = ddi_dma_buf_bind_handle(acmd->cmd_dmahandle, bp, dma_flags, 4914 cb, 0, &acmd->cmd_dmacookies[0], &acmd->cmd_ncookies); 4915 4916 switch (i) { 4917 case DDI_DMA_PARTIAL_MAP: 4918 if ((dma_flags & DDI_DMA_PARTIAL) == 0) { 4919 con_log(CL_ANN, (CE_PANIC, "ddi_dma_buf_bind_handle: " 4920 "DDI_DMA_PARTIAL_MAP impossible")); 4921 goto no_dma_cookies; 4922 } 4923 4924 if (ddi_dma_numwin(acmd->cmd_dmahandle, &acmd->cmd_nwin) == 4925 DDI_FAILURE) { 4926 con_log(CL_ANN, (CE_PANIC, "ddi_dma_numwin failed")); 4927 goto no_dma_cookies; 4928 } 4929 4930 if (ddi_dma_getwin(acmd->cmd_dmahandle, acmd->cmd_curwin, 4931 &acmd->cmd_dma_offset, &acmd->cmd_dma_len, 4932 &acmd->cmd_dmacookies[0], &acmd->cmd_ncookies) == 4933 DDI_FAILURE) { 4934 4935 con_log(CL_ANN, (CE_PANIC, "ddi_dma_getwin failed")); 4936 goto no_dma_cookies; 4937 } 4938 4939 goto get_dma_cookies; 4940 case DDI_DMA_MAPPED: 4941 acmd->cmd_nwin = 1; 4942 acmd->cmd_dma_len = 0; 4943 acmd->cmd_dma_offset = 0; 4944 4945 get_dma_cookies: 4946 i = 0; 4947 acmd->cmd_dmacount = 0; 4948 for (;;) { 4949 acmd->cmd_dmacount += 4950 acmd->cmd_dmacookies[i++].dmac_size; 4951 4952 if (i == instance->max_num_sge || 4953 i == acmd->cmd_ncookies) 4954 break; 4955 4956 ddi_dma_nextcookie(acmd->cmd_dmahandle, 4957 &acmd->cmd_dmacookies[i]); 4958 } 4959 4960 acmd->cmd_cookie = i; 4961 acmd->cmd_cookiecnt = i; 4962 4963 acmd->cmd_flags |= CFLAG_DMAVALID; 4964 4965 if (bp->b_bcount >= acmd->cmd_dmacount) { 4966 pkt->pkt_resid = bp->b_bcount - acmd->cmd_dmacount; 4967 } else { 4968 pkt->pkt_resid = 0; 4969 } 4970 4971 return (DDI_SUCCESS); 4972 case DDI_DMA_NORESOURCES: 4973 bioerror(bp, 0); 4974 break; 4975 case DDI_DMA_NOMAPPING: 4976 bioerror(bp, EFAULT); 4977 break; 4978 case DDI_DMA_TOOBIG: 4979 bioerror(bp, EINVAL); 4980 break; 4981 case DDI_DMA_INUSE: 4982 con_log(CL_ANN, (CE_PANIC, "ddi_dma_buf_bind_handle:" 4983 " DDI_DMA_INUSE impossible")); 4984 break; 4985 default: 4986 con_log(CL_ANN, (CE_PANIC, "ddi_dma_buf_bind_handle: " 4987 "impossible result (0x%x)", i)); 4988 break; 4989 } 4990 4991 no_dma_cookies: 4992 ddi_dma_free_handle(&acmd->cmd_dmahandle); 4993 acmd->cmd_dmahandle = NULL; 4994 acmd->cmd_flags &= ~CFLAG_DMAVALID; 4995 return (DDI_FAILURE); 4996 } 4997 4998 /* 4999 * mrsas_dma_move(struct mrsas_instance *, struct scsi_pkt *, struct buf *) 5000 * 5001 * move dma resources to next dma window 5002 * 5003 */ 5004 int 5005 mrsas_dma_move(struct mrsas_instance *instance, struct scsi_pkt *pkt, 5006 struct buf *bp) 5007 { 5008 int i = 0; 5009 5010 struct scsa_cmd *acmd = PKT2CMD(pkt); 5011 5012 /* 5013 * If there are no more cookies remaining in this window, 5014 * must move to the next window first. 5015 */ 5016 if (acmd->cmd_cookie == acmd->cmd_ncookies) { 5017 if (acmd->cmd_curwin == acmd->cmd_nwin && acmd->cmd_nwin == 1) { 5018 return (DDI_SUCCESS); 5019 } 5020 5021 /* at last window, cannot move */ 5022 if (++acmd->cmd_curwin >= acmd->cmd_nwin) { 5023 return (DDI_FAILURE); 5024 } 5025 5026 if (ddi_dma_getwin(acmd->cmd_dmahandle, acmd->cmd_curwin, 5027 &acmd->cmd_dma_offset, &acmd->cmd_dma_len, 5028 &acmd->cmd_dmacookies[0], &acmd->cmd_ncookies) == 5029 DDI_FAILURE) { 5030 return (DDI_FAILURE); 5031 } 5032 5033 acmd->cmd_cookie = 0; 5034 } else { 5035 /* still more cookies in this window - get the next one */ 5036 ddi_dma_nextcookie(acmd->cmd_dmahandle, 5037 &acmd->cmd_dmacookies[0]); 5038 } 5039 5040 /* get remaining cookies in this window, up to our maximum */ 5041 for (;;) { 5042 acmd->cmd_dmacount += acmd->cmd_dmacookies[i++].dmac_size; 5043 acmd->cmd_cookie++; 5044 5045 if (i == instance->max_num_sge || 5046 acmd->cmd_cookie == acmd->cmd_ncookies) { 5047 break; 5048 } 5049 5050 ddi_dma_nextcookie(acmd->cmd_dmahandle, 5051 &acmd->cmd_dmacookies[i]); 5052 } 5053 5054 acmd->cmd_cookiecnt = i; 5055 5056 if (bp->b_bcount >= acmd->cmd_dmacount) { 5057 pkt->pkt_resid = bp->b_bcount - acmd->cmd_dmacount; 5058 } else { 5059 pkt->pkt_resid = 0; 5060 } 5061 5062 return (DDI_SUCCESS); 5063 } 5064 5065 /* 5066 * build_cmd 5067 */ 5068 static struct mrsas_cmd * 5069 build_cmd(struct mrsas_instance *instance, struct scsi_address *ap, 5070 struct scsi_pkt *pkt, uchar_t *cmd_done) 5071 { 5072 uint16_t flags = 0; 5073 uint32_t i; 5074 uint32_t context; 5075 uint32_t sge_bytes; 5076 uint32_t tmp_data_xfer_len; 5077 ddi_acc_handle_t acc_handle; 5078 struct mrsas_cmd *cmd; 5079 struct mrsas_sge64 *mfi_sgl; 5080 struct mrsas_sge_ieee *mfi_sgl_ieee; 5081 struct scsa_cmd *acmd = PKT2CMD(pkt); 5082 struct mrsas_pthru_frame *pthru; 5083 struct mrsas_io_frame *ldio; 5084 5085 /* find out if this is logical or physical drive command. */ 5086 acmd->islogical = MRDRV_IS_LOGICAL(ap); 5087 acmd->device_id = MAP_DEVICE_ID(instance, ap); 5088 *cmd_done = 0; 5089 5090 /* get the command packet */ 5091 if (!(cmd = get_mfi_pkt(instance))) { 5092 DTRACE_PROBE2(build_cmd_mfi_err, uint16_t, 5093 instance->fw_outstanding, uint16_t, instance->max_fw_cmds); 5094 return (NULL); 5095 } 5096 5097 acc_handle = cmd->frame_dma_obj.acc_handle; 5098 5099 /* Clear the frame buffer and assign back the context id */ 5100 (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame)); 5101 ddi_put32(acc_handle, &cmd->frame->hdr.context, cmd->index); 5102 5103 cmd->pkt = pkt; 5104 cmd->cmd = acmd; 5105 DTRACE_PROBE3(build_cmds, uint8_t, pkt->pkt_cdbp[0], 5106 ulong_t, acmd->cmd_dmacount, ulong_t, acmd->cmd_dma_len); 5107 5108 /* lets get the command directions */ 5109 if (acmd->cmd_flags & CFLAG_DMASEND) { 5110 flags = MFI_FRAME_DIR_WRITE; 5111 5112 if (acmd->cmd_flags & CFLAG_CONSISTENT) { 5113 (void) ddi_dma_sync(acmd->cmd_dmahandle, 5114 acmd->cmd_dma_offset, acmd->cmd_dma_len, 5115 DDI_DMA_SYNC_FORDEV); 5116 } 5117 } else if (acmd->cmd_flags & ~CFLAG_DMASEND) { 5118 flags = MFI_FRAME_DIR_READ; 5119 5120 if (acmd->cmd_flags & CFLAG_CONSISTENT) { 5121 (void) ddi_dma_sync(acmd->cmd_dmahandle, 5122 acmd->cmd_dma_offset, acmd->cmd_dma_len, 5123 DDI_DMA_SYNC_FORCPU); 5124 } 5125 } else { 5126 flags = MFI_FRAME_DIR_NONE; 5127 } 5128 5129 if (instance->flag_ieee) { 5130 flags |= MFI_FRAME_IEEE; 5131 } 5132 flags |= MFI_FRAME_SGL64; 5133 5134 switch (pkt->pkt_cdbp[0]) { 5135 5136 /* 5137 * case SCMD_SYNCHRONIZE_CACHE: 5138 * flush_cache(instance); 5139 * return_mfi_pkt(instance, cmd); 5140 * *cmd_done = 1; 5141 * 5142 * return (NULL); 5143 */ 5144 5145 case SCMD_READ: 5146 case SCMD_WRITE: 5147 case SCMD_READ_G1: 5148 case SCMD_WRITE_G1: 5149 case SCMD_READ_G4: 5150 case SCMD_WRITE_G4: 5151 case SCMD_READ_G5: 5152 case SCMD_WRITE_G5: 5153 if (acmd->islogical) { 5154 ldio = (struct mrsas_io_frame *)cmd->frame; 5155 5156 /* 5157 * preare the Logical IO frame: 5158 * 2nd bit is zero for all read cmds 5159 */ 5160 ddi_put8(acc_handle, &ldio->cmd, 5161 (pkt->pkt_cdbp[0] & 0x02) ? MFI_CMD_OP_LD_WRITE 5162 : MFI_CMD_OP_LD_READ); 5163 ddi_put8(acc_handle, &ldio->cmd_status, 0x0); 5164 ddi_put8(acc_handle, &ldio->scsi_status, 0x0); 5165 ddi_put8(acc_handle, &ldio->target_id, acmd->device_id); 5166 ddi_put16(acc_handle, &ldio->timeout, 0); 5167 ddi_put8(acc_handle, &ldio->reserved_0, 0); 5168 ddi_put16(acc_handle, &ldio->pad_0, 0); 5169 ddi_put16(acc_handle, &ldio->flags, flags); 5170 5171 /* Initialize sense Information */ 5172 bzero(cmd->sense, SENSE_LENGTH); 5173 ddi_put8(acc_handle, &ldio->sense_len, SENSE_LENGTH); 5174 ddi_put32(acc_handle, &ldio->sense_buf_phys_addr_hi, 0); 5175 ddi_put32(acc_handle, &ldio->sense_buf_phys_addr_lo, 5176 cmd->sense_phys_addr); 5177 ddi_put32(acc_handle, &ldio->start_lba_hi, 0); 5178 ddi_put8(acc_handle, &ldio->access_byte, 5179 (acmd->cmd_cdblen != 6) ? pkt->pkt_cdbp[1] : 0); 5180 ddi_put8(acc_handle, &ldio->sge_count, 5181 acmd->cmd_cookiecnt); 5182 if (instance->flag_ieee) { 5183 mfi_sgl_ieee = 5184 (struct mrsas_sge_ieee *)&ldio->sgl; 5185 } else { 5186 mfi_sgl = (struct mrsas_sge64 *)&ldio->sgl; 5187 } 5188 5189 context = ddi_get32(acc_handle, &ldio->context); 5190 5191 if (acmd->cmd_cdblen == CDB_GROUP0) { 5192 /* 6-byte cdb */ 5193 ddi_put32(acc_handle, &ldio->lba_count, ( 5194 (uint16_t)(pkt->pkt_cdbp[4]))); 5195 5196 ddi_put32(acc_handle, &ldio->start_lba_lo, ( 5197 ((uint32_t)(pkt->pkt_cdbp[3])) | 5198 ((uint32_t)(pkt->pkt_cdbp[2]) << 8) | 5199 ((uint32_t)((pkt->pkt_cdbp[1]) & 0x1F) 5200 << 16))); 5201 } else if (acmd->cmd_cdblen == CDB_GROUP1) { 5202 /* 10-byte cdb */ 5203 ddi_put32(acc_handle, &ldio->lba_count, ( 5204 ((uint16_t)(pkt->pkt_cdbp[8])) | 5205 ((uint16_t)(pkt->pkt_cdbp[7]) << 8))); 5206 5207 ddi_put32(acc_handle, &ldio->start_lba_lo, ( 5208 ((uint32_t)(pkt->pkt_cdbp[5])) | 5209 ((uint32_t)(pkt->pkt_cdbp[4]) << 8) | 5210 ((uint32_t)(pkt->pkt_cdbp[3]) << 16) | 5211 ((uint32_t)(pkt->pkt_cdbp[2]) << 24))); 5212 } else if (acmd->cmd_cdblen == CDB_GROUP5) { 5213 /* 12-byte cdb */ 5214 ddi_put32(acc_handle, &ldio->lba_count, ( 5215 ((uint32_t)(pkt->pkt_cdbp[9])) | 5216 ((uint32_t)(pkt->pkt_cdbp[8]) << 8) | 5217 ((uint32_t)(pkt->pkt_cdbp[7]) << 16) | 5218 ((uint32_t)(pkt->pkt_cdbp[6]) << 24))); 5219 5220 ddi_put32(acc_handle, &ldio->start_lba_lo, ( 5221 ((uint32_t)(pkt->pkt_cdbp[5])) | 5222 ((uint32_t)(pkt->pkt_cdbp[4]) << 8) | 5223 ((uint32_t)(pkt->pkt_cdbp[3]) << 16) | 5224 ((uint32_t)(pkt->pkt_cdbp[2]) << 24))); 5225 } else if (acmd->cmd_cdblen == CDB_GROUP4) { 5226 /* 16-byte cdb */ 5227 ddi_put32(acc_handle, &ldio->lba_count, ( 5228 ((uint32_t)(pkt->pkt_cdbp[13])) | 5229 ((uint32_t)(pkt->pkt_cdbp[12]) << 8) | 5230 ((uint32_t)(pkt->pkt_cdbp[11]) << 16) | 5231 ((uint32_t)(pkt->pkt_cdbp[10]) << 24))); 5232 5233 ddi_put32(acc_handle, &ldio->start_lba_lo, ( 5234 ((uint32_t)(pkt->pkt_cdbp[9])) | 5235 ((uint32_t)(pkt->pkt_cdbp[8]) << 8) | 5236 ((uint32_t)(pkt->pkt_cdbp[7]) << 16) | 5237 ((uint32_t)(pkt->pkt_cdbp[6]) << 24))); 5238 5239 ddi_put32(acc_handle, &ldio->start_lba_hi, ( 5240 ((uint32_t)(pkt->pkt_cdbp[5])) | 5241 ((uint32_t)(pkt->pkt_cdbp[4]) << 8) | 5242 ((uint32_t)(pkt->pkt_cdbp[3]) << 16) | 5243 ((uint32_t)(pkt->pkt_cdbp[2]) << 24))); 5244 } 5245 5246 break; 5247 } 5248 /* fall through For all non-rd/wr cmds */ 5249 default: 5250 5251 switch (pkt->pkt_cdbp[0]) { 5252 case SCMD_MODE_SENSE: 5253 case SCMD_MODE_SENSE_G1: { 5254 union scsi_cdb *cdbp; 5255 uint16_t page_code; 5256 5257 cdbp = (void *)pkt->pkt_cdbp; 5258 page_code = (uint16_t)cdbp->cdb_un.sg.scsi[0]; 5259 switch (page_code) { 5260 case 0x3: 5261 case 0x4: 5262 (void) mrsas_mode_sense_build(pkt); 5263 return_mfi_pkt(instance, cmd); 5264 *cmd_done = 1; 5265 return (NULL); 5266 } 5267 break; 5268 } 5269 default: 5270 break; 5271 } 5272 5273 pthru = (struct mrsas_pthru_frame *)cmd->frame; 5274 5275 /* prepare the DCDB frame */ 5276 ddi_put8(acc_handle, &pthru->cmd, (acmd->islogical) ? 5277 MFI_CMD_OP_LD_SCSI : MFI_CMD_OP_PD_SCSI); 5278 ddi_put8(acc_handle, &pthru->cmd_status, 0x0); 5279 ddi_put8(acc_handle, &pthru->scsi_status, 0x0); 5280 ddi_put8(acc_handle, &pthru->target_id, acmd->device_id); 5281 ddi_put8(acc_handle, &pthru->lun, 0); 5282 ddi_put8(acc_handle, &pthru->cdb_len, acmd->cmd_cdblen); 5283 ddi_put16(acc_handle, &pthru->timeout, 0); 5284 ddi_put16(acc_handle, &pthru->flags, flags); 5285 tmp_data_xfer_len = 0; 5286 for (i = 0; i < acmd->cmd_cookiecnt; i++) { 5287 tmp_data_xfer_len += acmd->cmd_dmacookies[i].dmac_size; 5288 } 5289 ddi_put32(acc_handle, &pthru->data_xfer_len, 5290 tmp_data_xfer_len); 5291 ddi_put8(acc_handle, &pthru->sge_count, acmd->cmd_cookiecnt); 5292 if (instance->flag_ieee) { 5293 mfi_sgl_ieee = (struct mrsas_sge_ieee *)&pthru->sgl; 5294 } else { 5295 mfi_sgl = (struct mrsas_sge64 *)&pthru->sgl; 5296 } 5297 5298 bzero(cmd->sense, SENSE_LENGTH); 5299 ddi_put8(acc_handle, &pthru->sense_len, SENSE_LENGTH); 5300 ddi_put32(acc_handle, &pthru->sense_buf_phys_addr_hi, 0); 5301 ddi_put32(acc_handle, &pthru->sense_buf_phys_addr_lo, 5302 cmd->sense_phys_addr); 5303 5304 context = ddi_get32(acc_handle, &pthru->context); 5305 ddi_rep_put8(acc_handle, (uint8_t *)pkt->pkt_cdbp, 5306 (uint8_t *)pthru->cdb, acmd->cmd_cdblen, DDI_DEV_AUTOINCR); 5307 5308 break; 5309 } 5310 #ifdef lint 5311 context = context; 5312 #endif 5313 /* prepare the scatter-gather list for the firmware */ 5314 if (instance->flag_ieee) { 5315 for (i = 0; i < acmd->cmd_cookiecnt; i++, mfi_sgl_ieee++) { 5316 ddi_put64(acc_handle, &mfi_sgl_ieee->phys_addr, 5317 acmd->cmd_dmacookies[i].dmac_laddress); 5318 ddi_put32(acc_handle, &mfi_sgl_ieee->length, 5319 acmd->cmd_dmacookies[i].dmac_size); 5320 } 5321 sge_bytes = sizeof (struct mrsas_sge_ieee)*acmd->cmd_cookiecnt; 5322 } else { 5323 for (i = 0; i < acmd->cmd_cookiecnt; i++, mfi_sgl++) { 5324 ddi_put64(acc_handle, &mfi_sgl->phys_addr, 5325 acmd->cmd_dmacookies[i].dmac_laddress); 5326 ddi_put32(acc_handle, &mfi_sgl->length, 5327 acmd->cmd_dmacookies[i].dmac_size); 5328 } 5329 sge_bytes = sizeof (struct mrsas_sge64)*acmd->cmd_cookiecnt; 5330 } 5331 5332 cmd->frame_count = (sge_bytes / MRMFI_FRAME_SIZE) + 5333 ((sge_bytes % MRMFI_FRAME_SIZE) ? 1 : 0) + 1; 5334 5335 if (cmd->frame_count >= 8) { 5336 cmd->frame_count = 8; 5337 } 5338 5339 return (cmd); 5340 } 5341 5342 #ifndef __sparc 5343 /* 5344 * wait_for_outstanding - Wait for all outstanding cmds 5345 * @instance: Adapter soft state 5346 * 5347 * This function waits for upto MRDRV_RESET_WAIT_TIME seconds for FW to 5348 * complete all its outstanding commands. Returns error if one or more IOs 5349 * are pending after this time period. 5350 */ 5351 static int 5352 wait_for_outstanding(struct mrsas_instance *instance) 5353 { 5354 int i; 5355 uint32_t wait_time = 90; 5356 5357 for (i = 0; i < wait_time; i++) { 5358 if (!instance->fw_outstanding) { 5359 break; 5360 } 5361 5362 drv_usecwait(MILLISEC); /* wait for 1000 usecs */; 5363 } 5364 5365 if (instance->fw_outstanding) { 5366 return (1); 5367 } 5368 5369 return (0); 5370 } 5371 #endif /* __sparc */ 5372 5373 /* 5374 * issue_mfi_pthru 5375 */ 5376 static int 5377 issue_mfi_pthru(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl, 5378 struct mrsas_cmd *cmd, int mode) 5379 { 5380 void *ubuf; 5381 uint32_t kphys_addr = 0; 5382 uint32_t xferlen = 0; 5383 uint32_t new_xfer_length = 0; 5384 uint_t model; 5385 ddi_acc_handle_t acc_handle = cmd->frame_dma_obj.acc_handle; 5386 dma_obj_t pthru_dma_obj; 5387 struct mrsas_pthru_frame *kpthru; 5388 struct mrsas_pthru_frame *pthru; 5389 int i; 5390 pthru = &cmd->frame->pthru; 5391 kpthru = (struct mrsas_pthru_frame *)&ioctl->frame[0]; 5392 5393 if (instance->adapterresetinprogress) { 5394 con_log(CL_ANN1, (CE_WARN, "issue_mfi_pthru: Reset flag set, " 5395 "returning mfi_pkt and setting TRAN_BUSY\n")); 5396 return (DDI_FAILURE); 5397 } 5398 model = ddi_model_convert_from(mode & FMODELS); 5399 if (model == DDI_MODEL_ILP32) { 5400 con_log(CL_ANN1, (CE_CONT, "issue_mfi_pthru: DDI_MODEL_LP32")); 5401 5402 xferlen = kpthru->sgl.sge32[0].length; 5403 5404 ubuf = (void *)(ulong_t)kpthru->sgl.sge32[0].phys_addr; 5405 } else { 5406 #ifdef _ILP32 5407 con_log(CL_ANN1, (CE_CONT, "issue_mfi_pthru: DDI_MODEL_LP32")); 5408 xferlen = kpthru->sgl.sge32[0].length; 5409 ubuf = (void *)(ulong_t)kpthru->sgl.sge32[0].phys_addr; 5410 #else 5411 con_log(CL_ANN1, (CE_CONT, "issue_mfi_pthru: DDI_MODEL_LP64")); 5412 xferlen = kpthru->sgl.sge64[0].length; 5413 ubuf = (void *)(ulong_t)kpthru->sgl.sge64[0].phys_addr; 5414 #endif 5415 } 5416 5417 if (xferlen) { 5418 /* means IOCTL requires DMA */ 5419 /* allocate the data transfer buffer */ 5420 /* pthru_dma_obj.size = xferlen; */ 5421 MRSAS_GET_BOUNDARY_ALIGNED_LEN(xferlen, new_xfer_length, 5422 PAGESIZE); 5423 pthru_dma_obj.size = new_xfer_length; 5424 pthru_dma_obj.dma_attr = mrsas_generic_dma_attr; 5425 pthru_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 5426 pthru_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU; 5427 pthru_dma_obj.dma_attr.dma_attr_sgllen = 1; 5428 pthru_dma_obj.dma_attr.dma_attr_align = 1; 5429 5430 /* allocate kernel buffer for DMA */ 5431 if (mrsas_alloc_dma_obj(instance, &pthru_dma_obj, 5432 (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) { 5433 con_log(CL_ANN, (CE_WARN, "issue_mfi_pthru: " 5434 "could not allocate data transfer buffer.")); 5435 return (DDI_FAILURE); 5436 } 5437 (void) memset(pthru_dma_obj.buffer, 0, xferlen); 5438 5439 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */ 5440 if (kpthru->flags & MFI_FRAME_DIR_WRITE) { 5441 for (i = 0; i < xferlen; i++) { 5442 if (ddi_copyin((uint8_t *)ubuf+i, 5443 (uint8_t *)pthru_dma_obj.buffer+i, 5444 1, mode)) { 5445 con_log(CL_ANN, (CE_WARN, 5446 "issue_mfi_pthru : " 5447 "copy from user space failed")); 5448 return (DDI_FAILURE); 5449 } 5450 } 5451 } 5452 5453 kphys_addr = pthru_dma_obj.dma_cookie[0].dmac_address; 5454 } 5455 5456 ddi_put8(acc_handle, &pthru->cmd, kpthru->cmd); 5457 ddi_put8(acc_handle, &pthru->sense_len, SENSE_LENGTH); 5458 ddi_put8(acc_handle, &pthru->cmd_status, 0); 5459 ddi_put8(acc_handle, &pthru->scsi_status, 0); 5460 ddi_put8(acc_handle, &pthru->target_id, kpthru->target_id); 5461 ddi_put8(acc_handle, &pthru->lun, kpthru->lun); 5462 ddi_put8(acc_handle, &pthru->cdb_len, kpthru->cdb_len); 5463 ddi_put8(acc_handle, &pthru->sge_count, kpthru->sge_count); 5464 ddi_put16(acc_handle, &pthru->timeout, kpthru->timeout); 5465 ddi_put32(acc_handle, &pthru->data_xfer_len, kpthru->data_xfer_len); 5466 5467 ddi_put32(acc_handle, &pthru->sense_buf_phys_addr_hi, 0); 5468 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr; 5469 /* ddi_put32(acc_handle, &pthru->sense_buf_phys_addr_lo, 0); */ 5470 5471 ddi_rep_put8(acc_handle, (uint8_t *)kpthru->cdb, (uint8_t *)pthru->cdb, 5472 pthru->cdb_len, DDI_DEV_AUTOINCR); 5473 5474 ddi_put16(acc_handle, &pthru->flags, kpthru->flags & ~MFI_FRAME_SGL64); 5475 ddi_put32(acc_handle, &pthru->sgl.sge32[0].length, xferlen); 5476 ddi_put32(acc_handle, &pthru->sgl.sge32[0].phys_addr, kphys_addr); 5477 5478 cmd->sync_cmd = MRSAS_TRUE; 5479 cmd->frame_count = 1; 5480 5481 if (instance->tbolt) { 5482 mr_sas_tbolt_build_mfi_cmd(instance, cmd); 5483 } 5484 5485 if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) { 5486 con_log(CL_ANN, (CE_WARN, 5487 "issue_mfi_pthru: fw_ioctl failed")); 5488 } else { 5489 if (xferlen && kpthru->flags & MFI_FRAME_DIR_READ) { 5490 for (i = 0; i < xferlen; i++) { 5491 if (ddi_copyout( 5492 (uint8_t *)pthru_dma_obj.buffer+i, 5493 (uint8_t *)ubuf+i, 1, mode)) { 5494 con_log(CL_ANN, (CE_WARN, 5495 "issue_mfi_pthru : " 5496 "copy to user space failed")); 5497 return (DDI_FAILURE); 5498 } 5499 } 5500 } 5501 } 5502 5503 kpthru->cmd_status = ddi_get8(acc_handle, &pthru->cmd_status); 5504 kpthru->scsi_status = ddi_get8(acc_handle, &pthru->scsi_status); 5505 5506 con_log(CL_ANN, (CE_CONT, "issue_mfi_pthru: cmd_status %x, " 5507 "scsi_status %x", kpthru->cmd_status, kpthru->scsi_status)); 5508 DTRACE_PROBE3(issue_pthru, uint8_t, kpthru->cmd, uint8_t, 5509 kpthru->cmd_status, uint8_t, kpthru->scsi_status); 5510 5511 if (kpthru->sense_len) { 5512 uint_t sense_len = SENSE_LENGTH; 5513 void *sense_ubuf = 5514 (void *)(ulong_t)kpthru->sense_buf_phys_addr_lo; 5515 if (kpthru->sense_len <= SENSE_LENGTH) { 5516 sense_len = kpthru->sense_len; 5517 } 5518 5519 for (i = 0; i < sense_len; i++) { 5520 if (ddi_copyout( 5521 (uint8_t *)cmd->sense+i, 5522 (uint8_t *)sense_ubuf+i, 1, mode)) { 5523 con_log(CL_ANN, (CE_WARN, 5524 "issue_mfi_pthru : " 5525 "copy to user space failed")); 5526 } 5527 con_log(CL_DLEVEL1, (CE_WARN, 5528 "Copying Sense info sense_buff[%d] = 0x%X", 5529 i, *((uint8_t *)cmd->sense + i))); 5530 } 5531 } 5532 (void) ddi_dma_sync(cmd->frame_dma_obj.dma_handle, 0, 0, 5533 DDI_DMA_SYNC_FORDEV); 5534 5535 if (xferlen) { 5536 /* free kernel buffer */ 5537 if (mrsas_free_dma_obj(instance, pthru_dma_obj) != DDI_SUCCESS) 5538 return (DDI_FAILURE); 5539 } 5540 5541 return (DDI_SUCCESS); 5542 } 5543 5544 /* 5545 * issue_mfi_dcmd 5546 */ 5547 static int 5548 issue_mfi_dcmd(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl, 5549 struct mrsas_cmd *cmd, int mode) 5550 { 5551 void *ubuf; 5552 uint32_t kphys_addr = 0; 5553 uint32_t xferlen = 0; 5554 uint32_t new_xfer_length = 0; 5555 uint32_t model; 5556 dma_obj_t dcmd_dma_obj; 5557 struct mrsas_dcmd_frame *kdcmd; 5558 struct mrsas_dcmd_frame *dcmd; 5559 ddi_acc_handle_t acc_handle = cmd->frame_dma_obj.acc_handle; 5560 int i; 5561 dcmd = &cmd->frame->dcmd; 5562 kdcmd = (struct mrsas_dcmd_frame *)&ioctl->frame[0]; 5563 5564 if (instance->adapterresetinprogress) { 5565 con_log(CL_ANN1, (CE_NOTE, "Reset flag set, " 5566 "returning mfi_pkt and setting TRAN_BUSY")); 5567 return (DDI_FAILURE); 5568 } 5569 model = ddi_model_convert_from(mode & FMODELS); 5570 if (model == DDI_MODEL_ILP32) { 5571 con_log(CL_ANN1, (CE_CONT, "issue_mfi_dcmd: DDI_MODEL_ILP32")); 5572 5573 xferlen = kdcmd->sgl.sge32[0].length; 5574 5575 ubuf = (void *)(ulong_t)kdcmd->sgl.sge32[0].phys_addr; 5576 } else { 5577 #ifdef _ILP32 5578 con_log(CL_ANN1, (CE_CONT, "issue_mfi_dcmd: DDI_MODEL_ILP32")); 5579 xferlen = kdcmd->sgl.sge32[0].length; 5580 ubuf = (void *)(ulong_t)kdcmd->sgl.sge32[0].phys_addr; 5581 #else 5582 con_log(CL_ANN1, (CE_CONT, "issue_mfi_dcmd: DDI_MODEL_LP64")); 5583 xferlen = kdcmd->sgl.sge64[0].length; 5584 ubuf = (void *)(ulong_t)kdcmd->sgl.sge64[0].phys_addr; 5585 #endif 5586 } 5587 if (xferlen) { 5588 /* means IOCTL requires DMA */ 5589 /* allocate the data transfer buffer */ 5590 /* dcmd_dma_obj.size = xferlen; */ 5591 MRSAS_GET_BOUNDARY_ALIGNED_LEN(xferlen, new_xfer_length, 5592 PAGESIZE); 5593 dcmd_dma_obj.size = new_xfer_length; 5594 dcmd_dma_obj.dma_attr = mrsas_generic_dma_attr; 5595 dcmd_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 5596 dcmd_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU; 5597 dcmd_dma_obj.dma_attr.dma_attr_sgllen = 1; 5598 dcmd_dma_obj.dma_attr.dma_attr_align = 1; 5599 5600 /* allocate kernel buffer for DMA */ 5601 if (mrsas_alloc_dma_obj(instance, &dcmd_dma_obj, 5602 (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) { 5603 con_log(CL_ANN, 5604 (CE_WARN, "issue_mfi_dcmd: could not " 5605 "allocate data transfer buffer.")); 5606 return (DDI_FAILURE); 5607 } 5608 (void) memset(dcmd_dma_obj.buffer, 0, xferlen); 5609 5610 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */ 5611 if (kdcmd->flags & MFI_FRAME_DIR_WRITE) { 5612 for (i = 0; i < xferlen; i++) { 5613 if (ddi_copyin((uint8_t *)ubuf + i, 5614 (uint8_t *)dcmd_dma_obj.buffer + i, 5615 1, mode)) { 5616 con_log(CL_ANN, (CE_WARN, 5617 "issue_mfi_dcmd : " 5618 "copy from user space failed")); 5619 return (DDI_FAILURE); 5620 } 5621 } 5622 } 5623 5624 kphys_addr = dcmd_dma_obj.dma_cookie[0].dmac_address; 5625 } 5626 5627 ddi_put8(acc_handle, &dcmd->cmd, kdcmd->cmd); 5628 ddi_put8(acc_handle, &dcmd->cmd_status, 0); 5629 ddi_put8(acc_handle, &dcmd->sge_count, kdcmd->sge_count); 5630 ddi_put16(acc_handle, &dcmd->timeout, kdcmd->timeout); 5631 ddi_put32(acc_handle, &dcmd->data_xfer_len, kdcmd->data_xfer_len); 5632 ddi_put32(acc_handle, &dcmd->opcode, kdcmd->opcode); 5633 5634 ddi_rep_put8(acc_handle, (uint8_t *)kdcmd->mbox.b, 5635 (uint8_t *)dcmd->mbox.b, DCMD_MBOX_SZ, DDI_DEV_AUTOINCR); 5636 5637 ddi_put16(acc_handle, &dcmd->flags, kdcmd->flags & ~MFI_FRAME_SGL64); 5638 ddi_put32(acc_handle, &dcmd->sgl.sge32[0].length, xferlen); 5639 ddi_put32(acc_handle, &dcmd->sgl.sge32[0].phys_addr, kphys_addr); 5640 5641 cmd->sync_cmd = MRSAS_TRUE; 5642 cmd->frame_count = 1; 5643 5644 if (instance->tbolt) { 5645 mr_sas_tbolt_build_mfi_cmd(instance, cmd); 5646 } 5647 5648 if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) { 5649 con_log(CL_ANN, (CE_WARN, "issue_mfi_dcmd: fw_ioctl failed")); 5650 } else { 5651 if (xferlen && (kdcmd->flags & MFI_FRAME_DIR_READ)) { 5652 for (i = 0; i < xferlen; i++) { 5653 if (ddi_copyout( 5654 (uint8_t *)dcmd_dma_obj.buffer + i, 5655 (uint8_t *)ubuf + i, 5656 1, mode)) { 5657 con_log(CL_ANN, (CE_WARN, 5658 "issue_mfi_dcmd : " 5659 "copy to user space failed")); 5660 return (DDI_FAILURE); 5661 } 5662 } 5663 } 5664 } 5665 5666 kdcmd->cmd_status = ddi_get8(acc_handle, &dcmd->cmd_status); 5667 con_log(CL_ANN, 5668 (CE_CONT, "issue_mfi_dcmd: cmd_status %x", kdcmd->cmd_status)); 5669 DTRACE_PROBE3(issue_dcmd, uint32_t, kdcmd->opcode, uint8_t, 5670 kdcmd->cmd, uint8_t, kdcmd->cmd_status); 5671 5672 if (xferlen) { 5673 /* free kernel buffer */ 5674 if (mrsas_free_dma_obj(instance, dcmd_dma_obj) != DDI_SUCCESS) 5675 return (DDI_FAILURE); 5676 } 5677 5678 return (DDI_SUCCESS); 5679 } 5680 5681 /* 5682 * issue_mfi_smp 5683 */ 5684 static int 5685 issue_mfi_smp(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl, 5686 struct mrsas_cmd *cmd, int mode) 5687 { 5688 void *request_ubuf; 5689 void *response_ubuf; 5690 uint32_t request_xferlen = 0; 5691 uint32_t response_xferlen = 0; 5692 uint32_t new_xfer_length1 = 0; 5693 uint32_t new_xfer_length2 = 0; 5694 uint_t model; 5695 dma_obj_t request_dma_obj; 5696 dma_obj_t response_dma_obj; 5697 ddi_acc_handle_t acc_handle = cmd->frame_dma_obj.acc_handle; 5698 struct mrsas_smp_frame *ksmp; 5699 struct mrsas_smp_frame *smp; 5700 struct mrsas_sge32 *sge32; 5701 #ifndef _ILP32 5702 struct mrsas_sge64 *sge64; 5703 #endif 5704 int i; 5705 uint64_t tmp_sas_addr; 5706 5707 smp = &cmd->frame->smp; 5708 ksmp = (struct mrsas_smp_frame *)&ioctl->frame[0]; 5709 5710 if (instance->adapterresetinprogress) { 5711 con_log(CL_ANN1, (CE_WARN, "Reset flag set, " 5712 "returning mfi_pkt and setting TRAN_BUSY\n")); 5713 return (DDI_FAILURE); 5714 } 5715 model = ddi_model_convert_from(mode & FMODELS); 5716 if (model == DDI_MODEL_ILP32) { 5717 con_log(CL_ANN1, (CE_CONT, "issue_mfi_smp: DDI_MODEL_ILP32")); 5718 5719 sge32 = &ksmp->sgl[0].sge32[0]; 5720 response_xferlen = sge32[0].length; 5721 request_xferlen = sge32[1].length; 5722 con_log(CL_ANN, (CE_CONT, "issue_mfi_smp: " 5723 "response_xferlen = %x, request_xferlen = %x", 5724 response_xferlen, request_xferlen)); 5725 5726 response_ubuf = (void *)(ulong_t)sge32[0].phys_addr; 5727 request_ubuf = (void *)(ulong_t)sge32[1].phys_addr; 5728 con_log(CL_ANN1, (CE_CONT, "issue_mfi_smp: " 5729 "response_ubuf = %p, request_ubuf = %p", 5730 response_ubuf, request_ubuf)); 5731 } else { 5732 #ifdef _ILP32 5733 con_log(CL_ANN1, (CE_CONT, "issue_mfi_smp: DDI_MODEL_ILP32")); 5734 5735 sge32 = &ksmp->sgl[0].sge32[0]; 5736 response_xferlen = sge32[0].length; 5737 request_xferlen = sge32[1].length; 5738 con_log(CL_ANN, (CE_CONT, "issue_mfi_smp: " 5739 "response_xferlen = %x, request_xferlen = %x", 5740 response_xferlen, request_xferlen)); 5741 5742 response_ubuf = (void *)(ulong_t)sge32[0].phys_addr; 5743 request_ubuf = (void *)(ulong_t)sge32[1].phys_addr; 5744 con_log(CL_ANN1, (CE_CONT, "issue_mfi_smp: " 5745 "response_ubuf = %p, request_ubuf = %p", 5746 response_ubuf, request_ubuf)); 5747 #else 5748 con_log(CL_ANN1, (CE_CONT, "issue_mfi_smp: DDI_MODEL_LP64")); 5749 5750 sge64 = &ksmp->sgl[0].sge64[0]; 5751 response_xferlen = sge64[0].length; 5752 request_xferlen = sge64[1].length; 5753 5754 response_ubuf = (void *)(ulong_t)sge64[0].phys_addr; 5755 request_ubuf = (void *)(ulong_t)sge64[1].phys_addr; 5756 #endif 5757 } 5758 if (request_xferlen) { 5759 /* means IOCTL requires DMA */ 5760 /* allocate the data transfer buffer */ 5761 /* request_dma_obj.size = request_xferlen; */ 5762 MRSAS_GET_BOUNDARY_ALIGNED_LEN(request_xferlen, 5763 new_xfer_length1, PAGESIZE); 5764 request_dma_obj.size = new_xfer_length1; 5765 request_dma_obj.dma_attr = mrsas_generic_dma_attr; 5766 request_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 5767 request_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU; 5768 request_dma_obj.dma_attr.dma_attr_sgllen = 1; 5769 request_dma_obj.dma_attr.dma_attr_align = 1; 5770 5771 /* allocate kernel buffer for DMA */ 5772 if (mrsas_alloc_dma_obj(instance, &request_dma_obj, 5773 (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) { 5774 con_log(CL_ANN, (CE_WARN, "issue_mfi_smp: " 5775 "could not allocate data transfer buffer.")); 5776 return (DDI_FAILURE); 5777 } 5778 (void) memset(request_dma_obj.buffer, 0, request_xferlen); 5779 5780 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */ 5781 for (i = 0; i < request_xferlen; i++) { 5782 if (ddi_copyin((uint8_t *)request_ubuf + i, 5783 (uint8_t *)request_dma_obj.buffer + i, 5784 1, mode)) { 5785 con_log(CL_ANN, (CE_WARN, "issue_mfi_smp: " 5786 "copy from user space failed")); 5787 return (DDI_FAILURE); 5788 } 5789 } 5790 } 5791 5792 if (response_xferlen) { 5793 /* means IOCTL requires DMA */ 5794 /* allocate the data transfer buffer */ 5795 /* response_dma_obj.size = response_xferlen; */ 5796 MRSAS_GET_BOUNDARY_ALIGNED_LEN(response_xferlen, 5797 new_xfer_length2, PAGESIZE); 5798 response_dma_obj.size = new_xfer_length2; 5799 response_dma_obj.dma_attr = mrsas_generic_dma_attr; 5800 response_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 5801 response_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU; 5802 response_dma_obj.dma_attr.dma_attr_sgllen = 1; 5803 response_dma_obj.dma_attr.dma_attr_align = 1; 5804 5805 /* allocate kernel buffer for DMA */ 5806 if (mrsas_alloc_dma_obj(instance, &response_dma_obj, 5807 (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) { 5808 con_log(CL_ANN, (CE_WARN, "issue_mfi_smp: " 5809 "could not allocate data transfer buffer.")); 5810 return (DDI_FAILURE); 5811 } 5812 (void) memset(response_dma_obj.buffer, 0, response_xferlen); 5813 5814 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */ 5815 for (i = 0; i < response_xferlen; i++) { 5816 if (ddi_copyin((uint8_t *)response_ubuf + i, 5817 (uint8_t *)response_dma_obj.buffer + i, 5818 1, mode)) { 5819 con_log(CL_ANN, (CE_WARN, "issue_mfi_smp: " 5820 "copy from user space failed")); 5821 return (DDI_FAILURE); 5822 } 5823 } 5824 } 5825 5826 ddi_put8(acc_handle, &smp->cmd, ksmp->cmd); 5827 ddi_put8(acc_handle, &smp->cmd_status, 0); 5828 ddi_put8(acc_handle, &smp->connection_status, 0); 5829 ddi_put8(acc_handle, &smp->sge_count, ksmp->sge_count); 5830 /* smp->context = ksmp->context; */ 5831 ddi_put16(acc_handle, &smp->timeout, ksmp->timeout); 5832 ddi_put32(acc_handle, &smp->data_xfer_len, ksmp->data_xfer_len); 5833 5834 bcopy((void *)&ksmp->sas_addr, (void *)&tmp_sas_addr, 5835 sizeof (uint64_t)); 5836 ddi_put64(acc_handle, &smp->sas_addr, tmp_sas_addr); 5837 5838 ddi_put16(acc_handle, &smp->flags, ksmp->flags & ~MFI_FRAME_SGL64); 5839 5840 model = ddi_model_convert_from(mode & FMODELS); 5841 if (model == DDI_MODEL_ILP32) { 5842 con_log(CL_ANN1, (CE_CONT, 5843 "issue_mfi_smp: DDI_MODEL_ILP32")); 5844 5845 sge32 = &smp->sgl[0].sge32[0]; 5846 ddi_put32(acc_handle, &sge32[0].length, response_xferlen); 5847 ddi_put32(acc_handle, &sge32[0].phys_addr, 5848 response_dma_obj.dma_cookie[0].dmac_address); 5849 ddi_put32(acc_handle, &sge32[1].length, request_xferlen); 5850 ddi_put32(acc_handle, &sge32[1].phys_addr, 5851 request_dma_obj.dma_cookie[0].dmac_address); 5852 } else { 5853 #ifdef _ILP32 5854 con_log(CL_ANN1, (CE_CONT, 5855 "issue_mfi_smp: DDI_MODEL_ILP32")); 5856 sge32 = &smp->sgl[0].sge32[0]; 5857 ddi_put32(acc_handle, &sge32[0].length, response_xferlen); 5858 ddi_put32(acc_handle, &sge32[0].phys_addr, 5859 response_dma_obj.dma_cookie[0].dmac_address); 5860 ddi_put32(acc_handle, &sge32[1].length, request_xferlen); 5861 ddi_put32(acc_handle, &sge32[1].phys_addr, 5862 request_dma_obj.dma_cookie[0].dmac_address); 5863 #else 5864 con_log(CL_ANN1, (CE_CONT, 5865 "issue_mfi_smp: DDI_MODEL_LP64")); 5866 sge64 = &smp->sgl[0].sge64[0]; 5867 ddi_put32(acc_handle, &sge64[0].length, response_xferlen); 5868 ddi_put64(acc_handle, &sge64[0].phys_addr, 5869 response_dma_obj.dma_cookie[0].dmac_address); 5870 ddi_put32(acc_handle, &sge64[1].length, request_xferlen); 5871 ddi_put64(acc_handle, &sge64[1].phys_addr, 5872 request_dma_obj.dma_cookie[0].dmac_address); 5873 #endif 5874 } 5875 con_log(CL_ANN1, (CE_CONT, "issue_mfi_smp : " 5876 "smp->response_xferlen = %d, smp->request_xferlen = %d " 5877 "smp->data_xfer_len = %d", ddi_get32(acc_handle, &sge32[0].length), 5878 ddi_get32(acc_handle, &sge32[1].length), 5879 ddi_get32(acc_handle, &smp->data_xfer_len))); 5880 5881 cmd->sync_cmd = MRSAS_TRUE; 5882 cmd->frame_count = 1; 5883 5884 if (instance->tbolt) { 5885 mr_sas_tbolt_build_mfi_cmd(instance, cmd); 5886 } 5887 5888 if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) { 5889 con_log(CL_ANN, (CE_WARN, 5890 "issue_mfi_smp: fw_ioctl failed")); 5891 } else { 5892 con_log(CL_ANN1, (CE_CONT, 5893 "issue_mfi_smp: copy to user space")); 5894 5895 if (request_xferlen) { 5896 for (i = 0; i < request_xferlen; i++) { 5897 if (ddi_copyout( 5898 (uint8_t *)request_dma_obj.buffer + 5899 i, (uint8_t *)request_ubuf + i, 5900 1, mode)) { 5901 con_log(CL_ANN, (CE_WARN, 5902 "issue_mfi_smp : copy to user space" 5903 " failed")); 5904 return (DDI_FAILURE); 5905 } 5906 } 5907 } 5908 5909 if (response_xferlen) { 5910 for (i = 0; i < response_xferlen; i++) { 5911 if (ddi_copyout( 5912 (uint8_t *)response_dma_obj.buffer 5913 + i, (uint8_t *)response_ubuf 5914 + i, 1, mode)) { 5915 con_log(CL_ANN, (CE_WARN, 5916 "issue_mfi_smp : copy to " 5917 "user space failed")); 5918 return (DDI_FAILURE); 5919 } 5920 } 5921 } 5922 } 5923 5924 ksmp->cmd_status = ddi_get8(acc_handle, &smp->cmd_status); 5925 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: smp->cmd_status = %d", 5926 ksmp->cmd_status)); 5927 DTRACE_PROBE2(issue_smp, uint8_t, ksmp->cmd, uint8_t, ksmp->cmd_status); 5928 5929 if (request_xferlen) { 5930 /* free kernel buffer */ 5931 if (mrsas_free_dma_obj(instance, request_dma_obj) != 5932 DDI_SUCCESS) 5933 return (DDI_FAILURE); 5934 } 5935 5936 if (response_xferlen) { 5937 /* free kernel buffer */ 5938 if (mrsas_free_dma_obj(instance, response_dma_obj) != 5939 DDI_SUCCESS) 5940 return (DDI_FAILURE); 5941 } 5942 5943 return (DDI_SUCCESS); 5944 } 5945 5946 /* 5947 * issue_mfi_stp 5948 */ 5949 static int 5950 issue_mfi_stp(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl, 5951 struct mrsas_cmd *cmd, int mode) 5952 { 5953 void *fis_ubuf; 5954 void *data_ubuf; 5955 uint32_t fis_xferlen = 0; 5956 uint32_t new_xfer_length1 = 0; 5957 uint32_t new_xfer_length2 = 0; 5958 uint32_t data_xferlen = 0; 5959 uint_t model; 5960 dma_obj_t fis_dma_obj; 5961 dma_obj_t data_dma_obj; 5962 struct mrsas_stp_frame *kstp; 5963 struct mrsas_stp_frame *stp; 5964 ddi_acc_handle_t acc_handle = cmd->frame_dma_obj.acc_handle; 5965 int i; 5966 5967 stp = &cmd->frame->stp; 5968 kstp = (struct mrsas_stp_frame *)&ioctl->frame[0]; 5969 5970 if (instance->adapterresetinprogress) { 5971 con_log(CL_ANN1, (CE_WARN, "Reset flag set, " 5972 "returning mfi_pkt and setting TRAN_BUSY\n")); 5973 return (DDI_FAILURE); 5974 } 5975 model = ddi_model_convert_from(mode & FMODELS); 5976 if (model == DDI_MODEL_ILP32) { 5977 con_log(CL_ANN1, (CE_CONT, "issue_mfi_stp: DDI_MODEL_ILP32")); 5978 5979 fis_xferlen = kstp->sgl.sge32[0].length; 5980 data_xferlen = kstp->sgl.sge32[1].length; 5981 5982 fis_ubuf = (void *)(ulong_t)kstp->sgl.sge32[0].phys_addr; 5983 data_ubuf = (void *)(ulong_t)kstp->sgl.sge32[1].phys_addr; 5984 } else { 5985 #ifdef _ILP32 5986 con_log(CL_ANN1, (CE_CONT, "issue_mfi_stp: DDI_MODEL_ILP32")); 5987 5988 fis_xferlen = kstp->sgl.sge32[0].length; 5989 data_xferlen = kstp->sgl.sge32[1].length; 5990 5991 fis_ubuf = (void *)(ulong_t)kstp->sgl.sge32[0].phys_addr; 5992 data_ubuf = (void *)(ulong_t)kstp->sgl.sge32[1].phys_addr; 5993 #else 5994 con_log(CL_ANN1, (CE_CONT, "issue_mfi_stp: DDI_MODEL_LP64")); 5995 5996 fis_xferlen = kstp->sgl.sge64[0].length; 5997 data_xferlen = kstp->sgl.sge64[1].length; 5998 5999 fis_ubuf = (void *)(ulong_t)kstp->sgl.sge64[0].phys_addr; 6000 data_ubuf = (void *)(ulong_t)kstp->sgl.sge64[1].phys_addr; 6001 #endif 6002 } 6003 6004 6005 if (fis_xferlen) { 6006 con_log(CL_ANN, (CE_CONT, "issue_mfi_stp: " 6007 "fis_ubuf = %p fis_xferlen = %x", fis_ubuf, fis_xferlen)); 6008 6009 /* means IOCTL requires DMA */ 6010 /* allocate the data transfer buffer */ 6011 /* fis_dma_obj.size = fis_xferlen; */ 6012 MRSAS_GET_BOUNDARY_ALIGNED_LEN(fis_xferlen, 6013 new_xfer_length1, PAGESIZE); 6014 fis_dma_obj.size = new_xfer_length1; 6015 fis_dma_obj.dma_attr = mrsas_generic_dma_attr; 6016 fis_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 6017 fis_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU; 6018 fis_dma_obj.dma_attr.dma_attr_sgllen = 1; 6019 fis_dma_obj.dma_attr.dma_attr_align = 1; 6020 6021 /* allocate kernel buffer for DMA */ 6022 if (mrsas_alloc_dma_obj(instance, &fis_dma_obj, 6023 (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) { 6024 con_log(CL_ANN, (CE_WARN, "issue_mfi_stp : " 6025 "could not allocate data transfer buffer.")); 6026 return (DDI_FAILURE); 6027 } 6028 (void) memset(fis_dma_obj.buffer, 0, fis_xferlen); 6029 6030 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */ 6031 for (i = 0; i < fis_xferlen; i++) { 6032 if (ddi_copyin((uint8_t *)fis_ubuf + i, 6033 (uint8_t *)fis_dma_obj.buffer + i, 1, mode)) { 6034 con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: " 6035 "copy from user space failed")); 6036 return (DDI_FAILURE); 6037 } 6038 } 6039 } 6040 6041 if (data_xferlen) { 6042 con_log(CL_ANN, (CE_CONT, "issue_mfi_stp: data_ubuf = %p " 6043 "data_xferlen = %x", data_ubuf, data_xferlen)); 6044 6045 /* means IOCTL requires DMA */ 6046 /* allocate the data transfer buffer */ 6047 /* data_dma_obj.size = data_xferlen; */ 6048 MRSAS_GET_BOUNDARY_ALIGNED_LEN(data_xferlen, new_xfer_length2, 6049 PAGESIZE); 6050 data_dma_obj.size = new_xfer_length2; 6051 data_dma_obj.dma_attr = mrsas_generic_dma_attr; 6052 data_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU; 6053 data_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU; 6054 data_dma_obj.dma_attr.dma_attr_sgllen = 1; 6055 data_dma_obj.dma_attr.dma_attr_align = 1; 6056 6057 /* allocate kernel buffer for DMA */ 6058 if (mrsas_alloc_dma_obj(instance, &data_dma_obj, 6059 (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) { 6060 con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: " 6061 "could not allocate data transfer buffer.")); 6062 return (DDI_FAILURE); 6063 } 6064 (void) memset(data_dma_obj.buffer, 0, data_xferlen); 6065 6066 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */ 6067 for (i = 0; i < data_xferlen; i++) { 6068 if (ddi_copyin((uint8_t *)data_ubuf + i, 6069 (uint8_t *)data_dma_obj.buffer + i, 1, mode)) { 6070 con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: " 6071 "copy from user space failed")); 6072 return (DDI_FAILURE); 6073 } 6074 } 6075 } 6076 6077 ddi_put8(acc_handle, &stp->cmd, kstp->cmd); 6078 ddi_put8(acc_handle, &stp->cmd_status, 0); 6079 ddi_put8(acc_handle, &stp->connection_status, 0); 6080 ddi_put8(acc_handle, &stp->target_id, kstp->target_id); 6081 ddi_put8(acc_handle, &stp->sge_count, kstp->sge_count); 6082 6083 ddi_put16(acc_handle, &stp->timeout, kstp->timeout); 6084 ddi_put32(acc_handle, &stp->data_xfer_len, kstp->data_xfer_len); 6085 6086 ddi_rep_put8(acc_handle, (uint8_t *)kstp->fis, (uint8_t *)stp->fis, 10, 6087 DDI_DEV_AUTOINCR); 6088 6089 ddi_put16(acc_handle, &stp->flags, kstp->flags & ~MFI_FRAME_SGL64); 6090 ddi_put32(acc_handle, &stp->stp_flags, kstp->stp_flags); 6091 ddi_put32(acc_handle, &stp->sgl.sge32[0].length, fis_xferlen); 6092 ddi_put32(acc_handle, &stp->sgl.sge32[0].phys_addr, 6093 fis_dma_obj.dma_cookie[0].dmac_address); 6094 ddi_put32(acc_handle, &stp->sgl.sge32[1].length, data_xferlen); 6095 ddi_put32(acc_handle, &stp->sgl.sge32[1].phys_addr, 6096 data_dma_obj.dma_cookie[0].dmac_address); 6097 6098 cmd->sync_cmd = MRSAS_TRUE; 6099 cmd->frame_count = 1; 6100 6101 if (instance->tbolt) { 6102 mr_sas_tbolt_build_mfi_cmd(instance, cmd); 6103 } 6104 6105 if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) { 6106 con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: fw_ioctl failed")); 6107 } else { 6108 6109 if (fis_xferlen) { 6110 for (i = 0; i < fis_xferlen; i++) { 6111 if (ddi_copyout( 6112 (uint8_t *)fis_dma_obj.buffer + i, 6113 (uint8_t *)fis_ubuf + i, 1, mode)) { 6114 con_log(CL_ANN, (CE_WARN, 6115 "issue_mfi_stp : copy to " 6116 "user space failed")); 6117 return (DDI_FAILURE); 6118 } 6119 } 6120 } 6121 } 6122 if (data_xferlen) { 6123 for (i = 0; i < data_xferlen; i++) { 6124 if (ddi_copyout( 6125 (uint8_t *)data_dma_obj.buffer + i, 6126 (uint8_t *)data_ubuf + i, 1, mode)) { 6127 con_log(CL_ANN, (CE_WARN, 6128 "issue_mfi_stp : copy to" 6129 " user space failed")); 6130 return (DDI_FAILURE); 6131 } 6132 } 6133 } 6134 6135 kstp->cmd_status = ddi_get8(acc_handle, &stp->cmd_status); 6136 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_stp: stp->cmd_status = %d", 6137 kstp->cmd_status)); 6138 DTRACE_PROBE2(issue_stp, uint8_t, kstp->cmd, uint8_t, kstp->cmd_status); 6139 6140 if (fis_xferlen) { 6141 /* free kernel buffer */ 6142 if (mrsas_free_dma_obj(instance, fis_dma_obj) != DDI_SUCCESS) 6143 return (DDI_FAILURE); 6144 } 6145 6146 if (data_xferlen) { 6147 /* free kernel buffer */ 6148 if (mrsas_free_dma_obj(instance, data_dma_obj) != DDI_SUCCESS) 6149 return (DDI_FAILURE); 6150 } 6151 6152 return (DDI_SUCCESS); 6153 } 6154 6155 /* 6156 * fill_up_drv_ver 6157 */ 6158 void 6159 fill_up_drv_ver(struct mrsas_drv_ver *dv) 6160 { 6161 (void) memset(dv, 0, sizeof (struct mrsas_drv_ver)); 6162 6163 (void) memcpy(dv->signature, "$LSI LOGIC$", strlen("$LSI LOGIC$")); 6164 (void) memcpy(dv->os_name, "Solaris", strlen("Solaris")); 6165 (void) memcpy(dv->drv_name, "mr_sas", strlen("mr_sas")); 6166 (void) memcpy(dv->drv_ver, MRSAS_VERSION, strlen(MRSAS_VERSION)); 6167 (void) memcpy(dv->drv_rel_date, MRSAS_RELDATE, 6168 strlen(MRSAS_RELDATE)); 6169 6170 } 6171 6172 /* 6173 * handle_drv_ioctl 6174 */ 6175 static int 6176 handle_drv_ioctl(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl, 6177 int mode) 6178 { 6179 int i; 6180 int rval = DDI_SUCCESS; 6181 int *props = NULL; 6182 void *ubuf; 6183 6184 uint8_t *pci_conf_buf; 6185 uint32_t xferlen; 6186 uint32_t num_props; 6187 uint_t model; 6188 struct mrsas_dcmd_frame *kdcmd; 6189 struct mrsas_drv_ver dv; 6190 struct mrsas_pci_information pi; 6191 6192 kdcmd = (struct mrsas_dcmd_frame *)&ioctl->frame[0]; 6193 6194 model = ddi_model_convert_from(mode & FMODELS); 6195 if (model == DDI_MODEL_ILP32) { 6196 con_log(CL_ANN1, (CE_CONT, 6197 "handle_drv_ioctl: DDI_MODEL_ILP32")); 6198 6199 xferlen = kdcmd->sgl.sge32[0].length; 6200 6201 ubuf = (void *)(ulong_t)kdcmd->sgl.sge32[0].phys_addr; 6202 } else { 6203 #ifdef _ILP32 6204 con_log(CL_ANN1, (CE_CONT, 6205 "handle_drv_ioctl: DDI_MODEL_ILP32")); 6206 xferlen = kdcmd->sgl.sge32[0].length; 6207 ubuf = (void *)(ulong_t)kdcmd->sgl.sge32[0].phys_addr; 6208 #else 6209 con_log(CL_ANN1, (CE_CONT, 6210 "handle_drv_ioctl: DDI_MODEL_LP64")); 6211 xferlen = kdcmd->sgl.sge64[0].length; 6212 ubuf = (void *)(ulong_t)kdcmd->sgl.sge64[0].phys_addr; 6213 #endif 6214 } 6215 con_log(CL_ANN1, (CE_CONT, "handle_drv_ioctl: " 6216 "dataBuf=%p size=%d bytes", ubuf, xferlen)); 6217 6218 switch (kdcmd->opcode) { 6219 case MRSAS_DRIVER_IOCTL_DRIVER_VERSION: 6220 con_log(CL_ANN1, (CE_CONT, "handle_drv_ioctl: " 6221 "MRSAS_DRIVER_IOCTL_DRIVER_VERSION")); 6222 6223 fill_up_drv_ver(&dv); 6224 6225 if (ddi_copyout(&dv, ubuf, xferlen, mode)) { 6226 con_log(CL_ANN, (CE_WARN, "handle_drv_ioctl: " 6227 "MRSAS_DRIVER_IOCTL_DRIVER_VERSION : " 6228 "copy to user space failed")); 6229 kdcmd->cmd_status = 1; 6230 rval = 1; 6231 } else { 6232 kdcmd->cmd_status = 0; 6233 } 6234 break; 6235 case MRSAS_DRIVER_IOCTL_PCI_INFORMATION: 6236 con_log(CL_ANN1, (CE_NOTE, "handle_drv_ioctl: " 6237 "MRSAS_DRIVER_IOCTL_PCI_INFORMAITON")); 6238 6239 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, instance->dip, 6240 0, "reg", &props, &num_props)) { 6241 con_log(CL_ANN, (CE_WARN, "handle_drv_ioctl: " 6242 "MRSAS_DRIVER_IOCTL_PCI_INFORMATION : " 6243 "ddi_prop_look_int_array failed")); 6244 rval = DDI_FAILURE; 6245 } else { 6246 6247 pi.busNumber = (props[0] >> 16) & 0xFF; 6248 pi.deviceNumber = (props[0] >> 11) & 0x1f; 6249 pi.functionNumber = (props[0] >> 8) & 0x7; 6250 ddi_prop_free((void *)props); 6251 } 6252 6253 pci_conf_buf = (uint8_t *)&pi.pciHeaderInfo; 6254 6255 for (i = 0; i < (sizeof (struct mrsas_pci_information) - 6256 offsetof(struct mrsas_pci_information, pciHeaderInfo)); 6257 i++) { 6258 pci_conf_buf[i] = 6259 pci_config_get8(instance->pci_handle, i); 6260 } 6261 6262 if (ddi_copyout(&pi, ubuf, xferlen, mode)) { 6263 con_log(CL_ANN, (CE_WARN, "handle_drv_ioctl: " 6264 "MRSAS_DRIVER_IOCTL_PCI_INFORMATION : " 6265 "copy to user space failed")); 6266 kdcmd->cmd_status = 1; 6267 rval = 1; 6268 } else { 6269 kdcmd->cmd_status = 0; 6270 } 6271 break; 6272 default: 6273 con_log(CL_ANN, (CE_WARN, "handle_drv_ioctl: " 6274 "invalid driver specific IOCTL opcode = 0x%x", 6275 kdcmd->opcode)); 6276 kdcmd->cmd_status = 1; 6277 rval = DDI_FAILURE; 6278 break; 6279 } 6280 6281 return (rval); 6282 } 6283 6284 /* 6285 * handle_mfi_ioctl 6286 */ 6287 static int 6288 handle_mfi_ioctl(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl, 6289 int mode) 6290 { 6291 int rval = DDI_SUCCESS; 6292 6293 struct mrsas_header *hdr; 6294 struct mrsas_cmd *cmd; 6295 6296 if (instance->tbolt) { 6297 cmd = get_raid_msg_mfi_pkt(instance); 6298 } else { 6299 cmd = get_mfi_pkt(instance); 6300 } 6301 if (!cmd) { 6302 con_log(CL_ANN, (CE_WARN, "mr_sas: " 6303 "failed to get a cmd packet")); 6304 DTRACE_PROBE2(mfi_ioctl_err, uint16_t, 6305 instance->fw_outstanding, uint16_t, instance->max_fw_cmds); 6306 return (DDI_FAILURE); 6307 } 6308 6309 /* Clear the frame buffer and assign back the context id */ 6310 (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame)); 6311 ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context, 6312 cmd->index); 6313 6314 hdr = (struct mrsas_header *)&ioctl->frame[0]; 6315 6316 switch (ddi_get8(cmd->frame_dma_obj.acc_handle, &hdr->cmd)) { 6317 case MFI_CMD_OP_DCMD: 6318 rval = issue_mfi_dcmd(instance, ioctl, cmd, mode); 6319 break; 6320 case MFI_CMD_OP_SMP: 6321 rval = issue_mfi_smp(instance, ioctl, cmd, mode); 6322 break; 6323 case MFI_CMD_OP_STP: 6324 rval = issue_mfi_stp(instance, ioctl, cmd, mode); 6325 break; 6326 case MFI_CMD_OP_LD_SCSI: 6327 case MFI_CMD_OP_PD_SCSI: 6328 rval = issue_mfi_pthru(instance, ioctl, cmd, mode); 6329 break; 6330 default: 6331 con_log(CL_ANN, (CE_WARN, "handle_mfi_ioctl: " 6332 "invalid mfi ioctl hdr->cmd = %d", hdr->cmd)); 6333 rval = DDI_FAILURE; 6334 break; 6335 } 6336 6337 if (mrsas_common_check(instance, cmd) != DDI_SUCCESS) 6338 rval = DDI_FAILURE; 6339 6340 if (instance->tbolt) { 6341 return_raid_msg_mfi_pkt(instance, cmd); 6342 } else { 6343 return_mfi_pkt(instance, cmd); 6344 } 6345 6346 return (rval); 6347 } 6348 6349 /* 6350 * AEN 6351 */ 6352 static int 6353 handle_mfi_aen(struct mrsas_instance *instance, struct mrsas_aen *aen) 6354 { 6355 int rval = 0; 6356 6357 rval = register_mfi_aen(instance, instance->aen_seq_num, 6358 aen->class_locale_word); 6359 6360 aen->cmd_status = (uint8_t)rval; 6361 6362 return (rval); 6363 } 6364 6365 static int 6366 register_mfi_aen(struct mrsas_instance *instance, uint32_t seq_num, 6367 uint32_t class_locale_word) 6368 { 6369 int ret_val; 6370 6371 struct mrsas_cmd *cmd, *aen_cmd; 6372 struct mrsas_dcmd_frame *dcmd; 6373 union mrsas_evt_class_locale curr_aen; 6374 union mrsas_evt_class_locale prev_aen; 6375 6376 con_log(CL_ANN, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 6377 /* 6378 * If there an AEN pending already (aen_cmd), check if the 6379 * class_locale of that pending AEN is inclusive of the new 6380 * AEN request we currently have. If it is, then we don't have 6381 * to do anything. In other words, whichever events the current 6382 * AEN request is subscribing to, have already been subscribed 6383 * to. 6384 * 6385 * If the old_cmd is _not_ inclusive, then we have to abort 6386 * that command, form a class_locale that is superset of both 6387 * old and current and re-issue to the FW 6388 */ 6389 6390 curr_aen.word = LE_32(class_locale_word); 6391 curr_aen.members.locale = LE_16(curr_aen.members.locale); 6392 aen_cmd = instance->aen_cmd; 6393 if (aen_cmd) { 6394 prev_aen.word = ddi_get32(aen_cmd->frame_dma_obj.acc_handle, 6395 &aen_cmd->frame->dcmd.mbox.w[1]); 6396 prev_aen.word = LE_32(prev_aen.word); 6397 prev_aen.members.locale = LE_16(prev_aen.members.locale); 6398 /* 6399 * A class whose enum value is smaller is inclusive of all 6400 * higher values. If a PROGRESS (= -1) was previously 6401 * registered, then a new registration requests for higher 6402 * classes need not be sent to FW. They are automatically 6403 * included. 6404 * 6405 * Locale numbers don't have such hierarchy. They are bitmap 6406 * values 6407 */ 6408 if ((prev_aen.members.class <= curr_aen.members.class) && 6409 !((prev_aen.members.locale & curr_aen.members.locale) ^ 6410 curr_aen.members.locale)) { 6411 /* 6412 * Previously issued event registration includes 6413 * current request. Nothing to do. 6414 */ 6415 6416 return (0); 6417 } else { 6418 curr_aen.members.locale |= prev_aen.members.locale; 6419 6420 if (prev_aen.members.class < curr_aen.members.class) 6421 curr_aen.members.class = prev_aen.members.class; 6422 6423 ret_val = abort_aen_cmd(instance, aen_cmd); 6424 6425 if (ret_val) { 6426 con_log(CL_ANN, (CE_WARN, "register_mfi_aen: " 6427 "failed to abort prevous AEN command")); 6428 6429 return (ret_val); 6430 } 6431 } 6432 } else { 6433 curr_aen.word = LE_32(class_locale_word); 6434 curr_aen.members.locale = LE_16(curr_aen.members.locale); 6435 } 6436 6437 if (instance->tbolt) { 6438 cmd = get_raid_msg_mfi_pkt(instance); 6439 } else { 6440 cmd = get_mfi_pkt(instance); 6441 } 6442 6443 if (!cmd) { 6444 DTRACE_PROBE2(mfi_aen_err, uint16_t, instance->fw_outstanding, 6445 uint16_t, instance->max_fw_cmds); 6446 return (ENOMEM); 6447 } 6448 6449 /* Clear the frame buffer and assign back the context id */ 6450 (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame)); 6451 ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context, 6452 cmd->index); 6453 6454 dcmd = &cmd->frame->dcmd; 6455 6456 /* for(i = 0; i < DCMD_MBOX_SZ; i++) dcmd->mbox.b[i] = 0; */ 6457 (void) memset(dcmd->mbox.b, 0, DCMD_MBOX_SZ); 6458 6459 (void) memset(instance->mfi_evt_detail_obj.buffer, 0, 6460 sizeof (struct mrsas_evt_detail)); 6461 6462 /* Prepare DCMD for aen registration */ 6463 ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd, MFI_CMD_OP_DCMD); 6464 ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd_status, 0x0); 6465 ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->sge_count, 1); 6466 ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->flags, 6467 MFI_FRAME_DIR_READ); 6468 ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->timeout, 0); 6469 ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->data_xfer_len, 6470 sizeof (struct mrsas_evt_detail)); 6471 ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->opcode, 6472 MR_DCMD_CTRL_EVENT_WAIT); 6473 ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->mbox.w[0], seq_num); 6474 curr_aen.members.locale = LE_16(curr_aen.members.locale); 6475 curr_aen.word = LE_32(curr_aen.word); 6476 ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->mbox.w[1], 6477 curr_aen.word); 6478 ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].phys_addr, 6479 instance->mfi_evt_detail_obj.dma_cookie[0].dmac_address); 6480 ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].length, 6481 sizeof (struct mrsas_evt_detail)); 6482 6483 instance->aen_seq_num = seq_num; 6484 6485 6486 /* 6487 * Store reference to the cmd used to register for AEN. When an 6488 * application wants us to register for AEN, we have to abort this 6489 * cmd and re-register with a new EVENT LOCALE supplied by that app 6490 */ 6491 instance->aen_cmd = cmd; 6492 6493 cmd->frame_count = 1; 6494 6495 /* Issue the aen registration frame */ 6496 /* atomic_add_16 (&instance->fw_outstanding, 1); */ 6497 if (instance->tbolt) { 6498 mr_sas_tbolt_build_mfi_cmd(instance, cmd); 6499 } 6500 instance->func_ptr->issue_cmd(cmd, instance); 6501 6502 return (0); 6503 } 6504 6505 void 6506 display_scsi_inquiry(caddr_t scsi_inq) 6507 { 6508 #define MAX_SCSI_DEVICE_CODE 14 6509 int i; 6510 char inquiry_buf[256] = {0}; 6511 int len; 6512 const char *const scsi_device_types[] = { 6513 "Direct-Access ", 6514 "Sequential-Access", 6515 "Printer ", 6516 "Processor ", 6517 "WORM ", 6518 "CD-ROM ", 6519 "Scanner ", 6520 "Optical Device ", 6521 "Medium Changer ", 6522 "Communications ", 6523 "Unknown ", 6524 "Unknown ", 6525 "Unknown ", 6526 "Enclosure ", 6527 }; 6528 6529 len = 0; 6530 6531 len += snprintf(inquiry_buf + len, 265 - len, " Vendor: "); 6532 for (i = 8; i < 16; i++) { 6533 len += snprintf(inquiry_buf + len, 265 - len, "%c", 6534 scsi_inq[i]); 6535 } 6536 6537 len += snprintf(inquiry_buf + len, 265 - len, " Model: "); 6538 6539 for (i = 16; i < 32; i++) { 6540 len += snprintf(inquiry_buf + len, 265 - len, "%c", 6541 scsi_inq[i]); 6542 } 6543 6544 len += snprintf(inquiry_buf + len, 265 - len, " Rev: "); 6545 6546 for (i = 32; i < 36; i++) { 6547 len += snprintf(inquiry_buf + len, 265 - len, "%c", 6548 scsi_inq[i]); 6549 } 6550 6551 len += snprintf(inquiry_buf + len, 265 - len, "\n"); 6552 6553 6554 i = scsi_inq[0] & 0x1f; 6555 6556 6557 len += snprintf(inquiry_buf + len, 265 - len, " Type: %s ", 6558 i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] : 6559 "Unknown "); 6560 6561 6562 len += snprintf(inquiry_buf + len, 265 - len, 6563 " ANSI SCSI revision: %02x", scsi_inq[2] & 0x07); 6564 6565 if ((scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1) { 6566 len += snprintf(inquiry_buf + len, 265 - len, " CCS\n"); 6567 } else { 6568 len += snprintf(inquiry_buf + len, 265 - len, "\n"); 6569 } 6570 6571 con_log(CL_DLEVEL2, (CE_CONT, inquiry_buf)); 6572 } 6573 6574 static void 6575 io_timeout_checker(void *arg) 6576 { 6577 struct scsi_pkt *pkt; 6578 struct mrsas_instance *instance = arg; 6579 struct mrsas_cmd *cmd = NULL; 6580 struct mrsas_header *hdr; 6581 int time = 0; 6582 int counter = 0; 6583 struct mlist_head *pos, *next; 6584 mlist_t process_list; 6585 6586 if (instance->adapterresetinprogress == 1) { 6587 con_log(CL_ANN, (CE_NOTE, "io_timeout_checker:" 6588 " reset in progress")); 6589 6590 instance->timeout_id = timeout(io_timeout_checker, 6591 (void *) instance, drv_usectohz(MRSAS_1_SECOND)); 6592 return; 6593 } 6594 6595 /* See if this check needs to be in the beginning or last in ISR */ 6596 if (mrsas_initiate_ocr_if_fw_is_faulty(instance) == 1) { 6597 cmn_err(CE_WARN, "io_timeout_checker: " 6598 "FW Fault, calling reset adapter"); 6599 cmn_err(CE_CONT, "io_timeout_checker: " 6600 "fw_outstanding 0x%X max_fw_cmds 0x%X", 6601 instance->fw_outstanding, instance->max_fw_cmds); 6602 if (instance->adapterresetinprogress == 0) { 6603 instance->adapterresetinprogress = 1; 6604 if (instance->tbolt) 6605 (void) mrsas_tbolt_reset_ppc(instance); 6606 else 6607 (void) mrsas_reset_ppc(instance); 6608 instance->adapterresetinprogress = 0; 6609 } 6610 instance->timeout_id = timeout(io_timeout_checker, 6611 (void *) instance, drv_usectohz(MRSAS_1_SECOND)); 6612 return; 6613 } 6614 6615 INIT_LIST_HEAD(&process_list); 6616 6617 mutex_enter(&instance->cmd_pend_mtx); 6618 mlist_for_each_safe(pos, next, &instance->cmd_pend_list) { 6619 cmd = mlist_entry(pos, struct mrsas_cmd, list); 6620 6621 if (cmd == NULL) { 6622 continue; 6623 } 6624 6625 if (cmd->sync_cmd == MRSAS_TRUE) { 6626 hdr = (struct mrsas_header *)&cmd->frame->hdr; 6627 if (hdr == NULL) { 6628 continue; 6629 } 6630 time = --cmd->drv_pkt_time; 6631 } else { 6632 pkt = cmd->pkt; 6633 if (pkt == NULL) { 6634 continue; 6635 } 6636 time = --cmd->drv_pkt_time; 6637 } 6638 if (time <= 0) { 6639 cmn_err(CE_WARN, "%llx: " 6640 "io_timeout_checker: TIMING OUT: pkt: %p, " 6641 "cmd %p fw_outstanding 0x%X max_fw_cmds 0x%X\n", 6642 gethrtime(), (void *)pkt, (void *)cmd, 6643 instance->fw_outstanding, instance->max_fw_cmds); 6644 6645 counter++; 6646 break; 6647 } 6648 } 6649 mutex_exit(&instance->cmd_pend_mtx); 6650 6651 if (counter) { 6652 if (instance->disable_online_ctrl_reset == 1) { 6653 cmn_err(CE_WARN, "mr_sas %d: %s(): OCR is NOT " 6654 "supported by Firmware, KILL adapter!!!", 6655 instance->instance, __func__); 6656 6657 if (instance->tbolt) 6658 mrsas_tbolt_kill_adapter(instance); 6659 else 6660 (void) mrsas_kill_adapter(instance); 6661 6662 return; 6663 } else { 6664 if (cmd->retry_count_for_ocr <= IO_RETRY_COUNT) { 6665 if (instance->adapterresetinprogress == 0) { 6666 if (instance->tbolt) { 6667 (void) mrsas_tbolt_reset_ppc( 6668 instance); 6669 } else { 6670 (void) mrsas_reset_ppc( 6671 instance); 6672 } 6673 } 6674 } else { 6675 cmn_err(CE_WARN, 6676 "io_timeout_checker: " 6677 "cmd %p cmd->index %d " 6678 "timed out even after 3 resets: " 6679 "so KILL adapter", (void *)cmd, cmd->index); 6680 6681 mrsas_print_cmd_details(instance, cmd, 0xDD); 6682 6683 if (instance->tbolt) 6684 mrsas_tbolt_kill_adapter(instance); 6685 else 6686 (void) mrsas_kill_adapter(instance); 6687 return; 6688 } 6689 } 6690 } 6691 con_log(CL_ANN, (CE_NOTE, "mrsas: " 6692 "schedule next timeout check: " 6693 "do timeout \n")); 6694 instance->timeout_id = 6695 timeout(io_timeout_checker, (void *)instance, 6696 drv_usectohz(MRSAS_1_SECOND)); 6697 } 6698 6699 static uint32_t 6700 read_fw_status_reg_ppc(struct mrsas_instance *instance) 6701 { 6702 return ((uint32_t)RD_OB_SCRATCH_PAD_0(instance)); 6703 } 6704 6705 static void 6706 issue_cmd_ppc(struct mrsas_cmd *cmd, struct mrsas_instance *instance) 6707 { 6708 struct scsi_pkt *pkt; 6709 atomic_add_16(&instance->fw_outstanding, 1); 6710 6711 pkt = cmd->pkt; 6712 if (pkt) { 6713 con_log(CL_DLEVEL1, (CE_NOTE, "%llx : issue_cmd_ppc:" 6714 "ISSUED CMD TO FW : called : cmd:" 6715 ": %p instance : %p pkt : %p pkt_time : %x\n", 6716 gethrtime(), (void *)cmd, (void *)instance, 6717 (void *)pkt, cmd->drv_pkt_time)); 6718 if (instance->adapterresetinprogress) { 6719 cmd->drv_pkt_time = (uint16_t)debug_timeout_g; 6720 con_log(CL_ANN1, (CE_NOTE, "Reset the scsi_pkt timer")); 6721 } else { 6722 push_pending_mfi_pkt(instance, cmd); 6723 } 6724 6725 } else { 6726 con_log(CL_DLEVEL1, (CE_NOTE, "%llx : issue_cmd_ppc:" 6727 "ISSUED CMD TO FW : called : cmd : %p, instance: %p" 6728 "(NO PKT)\n", gethrtime(), (void *)cmd, (void *)instance)); 6729 } 6730 6731 mutex_enter(&instance->reg_write_mtx); 6732 /* Issue the command to the FW */ 6733 WR_IB_QPORT((cmd->frame_phys_addr) | 6734 (((cmd->frame_count - 1) << 1) | 1), instance); 6735 mutex_exit(&instance->reg_write_mtx); 6736 6737 } 6738 6739 /* 6740 * issue_cmd_in_sync_mode 6741 */ 6742 static int 6743 issue_cmd_in_sync_mode_ppc(struct mrsas_instance *instance, 6744 struct mrsas_cmd *cmd) 6745 { 6746 int i; 6747 uint32_t msecs = MFI_POLL_TIMEOUT_SECS * (10 * MILLISEC); 6748 struct mrsas_header *hdr = &cmd->frame->hdr; 6749 6750 con_log(CL_ANN1, (CE_NOTE, "issue_cmd_in_sync_mode_ppc: called")); 6751 6752 if (instance->adapterresetinprogress) { 6753 cmd->drv_pkt_time = ddi_get16( 6754 cmd->frame_dma_obj.acc_handle, &hdr->timeout); 6755 if (cmd->drv_pkt_time < debug_timeout_g) 6756 cmd->drv_pkt_time = (uint16_t)debug_timeout_g; 6757 6758 con_log(CL_ANN1, (CE_NOTE, "sync_mode_ppc: " 6759 "issue and return in reset case\n")); 6760 WR_IB_QPORT((cmd->frame_phys_addr) | 6761 (((cmd->frame_count - 1) << 1) | 1), instance); 6762 6763 return (DDI_SUCCESS); 6764 } else { 6765 con_log(CL_ANN1, (CE_NOTE, "sync_mode_ppc: pushing the pkt\n")); 6766 push_pending_mfi_pkt(instance, cmd); 6767 } 6768 6769 cmd->cmd_status = ENODATA; 6770 6771 mutex_enter(&instance->reg_write_mtx); 6772 /* Issue the command to the FW */ 6773 WR_IB_QPORT((cmd->frame_phys_addr) | 6774 (((cmd->frame_count - 1) << 1) | 1), instance); 6775 mutex_exit(&instance->reg_write_mtx); 6776 6777 mutex_enter(&instance->int_cmd_mtx); 6778 for (i = 0; i < msecs && (cmd->cmd_status == ENODATA); i++) { 6779 cv_wait(&instance->int_cmd_cv, &instance->int_cmd_mtx); 6780 } 6781 mutex_exit(&instance->int_cmd_mtx); 6782 6783 con_log(CL_ANN1, (CE_NOTE, "issue_cmd_in_sync_mode_ppc: done")); 6784 6785 if (i < (msecs -1)) { 6786 return (DDI_SUCCESS); 6787 } else { 6788 return (DDI_FAILURE); 6789 } 6790 } 6791 6792 /* 6793 * issue_cmd_in_poll_mode 6794 */ 6795 static int 6796 issue_cmd_in_poll_mode_ppc(struct mrsas_instance *instance, 6797 struct mrsas_cmd *cmd) 6798 { 6799 int i; 6800 uint16_t flags; 6801 uint32_t msecs = MFI_POLL_TIMEOUT_SECS * MILLISEC; 6802 struct mrsas_header *frame_hdr; 6803 6804 con_log(CL_ANN1, (CE_NOTE, "issue_cmd_in_poll_mode_ppc: called")); 6805 6806 frame_hdr = (struct mrsas_header *)cmd->frame; 6807 ddi_put8(cmd->frame_dma_obj.acc_handle, &frame_hdr->cmd_status, 6808 MFI_CMD_STATUS_POLL_MODE); 6809 flags = ddi_get16(cmd->frame_dma_obj.acc_handle, &frame_hdr->flags); 6810 flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE; 6811 6812 ddi_put16(cmd->frame_dma_obj.acc_handle, &frame_hdr->flags, flags); 6813 6814 /* issue the frame using inbound queue port */ 6815 WR_IB_QPORT((cmd->frame_phys_addr) | 6816 (((cmd->frame_count - 1) << 1) | 1), instance); 6817 6818 /* wait for cmd_status to change from 0xFF */ 6819 for (i = 0; i < msecs && ( 6820 ddi_get8(cmd->frame_dma_obj.acc_handle, &frame_hdr->cmd_status) 6821 == MFI_CMD_STATUS_POLL_MODE); i++) { 6822 drv_usecwait(MILLISEC); /* wait for 1000 usecs */ 6823 } 6824 6825 if (ddi_get8(cmd->frame_dma_obj.acc_handle, &frame_hdr->cmd_status) 6826 == MFI_CMD_STATUS_POLL_MODE) { 6827 con_log(CL_ANN, (CE_NOTE, "issue_cmd_in_poll_mode: " 6828 "cmd polling timed out")); 6829 return (DDI_FAILURE); 6830 } 6831 6832 return (DDI_SUCCESS); 6833 } 6834 6835 static void 6836 enable_intr_ppc(struct mrsas_instance *instance) 6837 { 6838 uint32_t mask; 6839 6840 con_log(CL_ANN1, (CE_NOTE, "enable_intr_ppc: called")); 6841 6842 /* WR_OB_DOORBELL_CLEAR(0xFFFFFFFF, instance); */ 6843 WR_OB_DOORBELL_CLEAR(OB_DOORBELL_CLEAR_MASK, instance); 6844 6845 /* WR_OB_INTR_MASK(~0x80000000, instance); */ 6846 WR_OB_INTR_MASK(~(MFI_REPLY_2108_MESSAGE_INTR_MASK), instance); 6847 6848 /* dummy read to force PCI flush */ 6849 mask = RD_OB_INTR_MASK(instance); 6850 6851 con_log(CL_ANN1, (CE_NOTE, "enable_intr_ppc: " 6852 "outbound_intr_mask = 0x%x", mask)); 6853 } 6854 6855 static void 6856 disable_intr_ppc(struct mrsas_instance *instance) 6857 { 6858 uint32_t mask; 6859 6860 con_log(CL_ANN1, (CE_NOTE, "disable_intr_ppc: called")); 6861 6862 con_log(CL_ANN1, (CE_NOTE, "disable_intr_ppc: before : " 6863 "outbound_intr_mask = 0x%x", RD_OB_INTR_MASK(instance))); 6864 6865 /* WR_OB_INTR_MASK(0xFFFFFFFF, instance); */ 6866 WR_OB_INTR_MASK(OB_INTR_MASK, instance); 6867 6868 con_log(CL_ANN1, (CE_NOTE, "disable_intr_ppc: after : " 6869 "outbound_intr_mask = 0x%x", RD_OB_INTR_MASK(instance))); 6870 6871 /* dummy read to force PCI flush */ 6872 mask = RD_OB_INTR_MASK(instance); 6873 #ifdef lint 6874 mask = mask; 6875 #endif 6876 } 6877 6878 static int 6879 intr_ack_ppc(struct mrsas_instance *instance) 6880 { 6881 uint32_t status; 6882 int ret = DDI_INTR_CLAIMED; 6883 6884 con_log(CL_ANN1, (CE_NOTE, "intr_ack_ppc: called")); 6885 6886 /* check if it is our interrupt */ 6887 status = RD_OB_INTR_STATUS(instance); 6888 6889 con_log(CL_ANN1, (CE_NOTE, "intr_ack_ppc: status = 0x%x", status)); 6890 6891 if (!(status & MFI_REPLY_2108_MESSAGE_INTR)) { 6892 ret = DDI_INTR_UNCLAIMED; 6893 } 6894 6895 if (mrsas_check_acc_handle(instance->regmap_handle) != DDI_SUCCESS) { 6896 ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST); 6897 ret = DDI_INTR_UNCLAIMED; 6898 } 6899 6900 if (ret == DDI_INTR_UNCLAIMED) { 6901 return (ret); 6902 } 6903 /* clear the interrupt by writing back the same value */ 6904 WR_OB_DOORBELL_CLEAR(status, instance); 6905 6906 /* dummy READ */ 6907 status = RD_OB_INTR_STATUS(instance); 6908 6909 con_log(CL_ANN1, (CE_NOTE, "intr_ack_ppc: interrupt cleared")); 6910 6911 return (ret); 6912 } 6913 6914 /* 6915 * Marks HBA as bad. This will be called either when an 6916 * IO packet times out even after 3 FW resets 6917 * or FW is found to be fault even after 3 continuous resets. 6918 */ 6919 6920 static int 6921 mrsas_kill_adapter(struct mrsas_instance *instance) 6922 { 6923 if (instance->deadadapter == 1) 6924 return (DDI_FAILURE); 6925 6926 con_log(CL_ANN1, (CE_NOTE, "mrsas_kill_adapter: " 6927 "Writing to doorbell with MFI_STOP_ADP ")); 6928 mutex_enter(&instance->ocr_flags_mtx); 6929 instance->deadadapter = 1; 6930 mutex_exit(&instance->ocr_flags_mtx); 6931 instance->func_ptr->disable_intr(instance); 6932 WR_IB_DOORBELL(MFI_STOP_ADP, instance); 6933 (void) mrsas_complete_pending_cmds(instance); 6934 return (DDI_SUCCESS); 6935 } 6936 6937 6938 static int 6939 mrsas_reset_ppc(struct mrsas_instance *instance) 6940 { 6941 uint32_t status; 6942 uint32_t retry = 0; 6943 uint32_t cur_abs_reg_val; 6944 uint32_t fw_state; 6945 6946 con_log(CL_ANN, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__)); 6947 6948 if (instance->deadadapter == 1) { 6949 cmn_err(CE_WARN, "mrsas_reset_ppc: " 6950 "no more resets as HBA has been marked dead "); 6951 return (DDI_FAILURE); 6952 } 6953 mutex_enter(&instance->ocr_flags_mtx); 6954 instance->adapterresetinprogress = 1; 6955 mutex_exit(&instance->ocr_flags_mtx); 6956 con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: adpterresetinprogress " 6957 "flag set, time %llx", gethrtime())); 6958 6959 instance->func_ptr->disable_intr(instance); 6960 retry_reset: 6961 WR_IB_WRITE_SEQ(0, instance); 6962 WR_IB_WRITE_SEQ(4, instance); 6963 WR_IB_WRITE_SEQ(0xb, instance); 6964 WR_IB_WRITE_SEQ(2, instance); 6965 WR_IB_WRITE_SEQ(7, instance); 6966 WR_IB_WRITE_SEQ(0xd, instance); 6967 con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: magic number written " 6968 "to write sequence register\n")); 6969 delay(100 * drv_usectohz(MILLISEC)); 6970 status = RD_OB_DRWE(instance); 6971 6972 while (!(status & DIAG_WRITE_ENABLE)) { 6973 delay(100 * drv_usectohz(MILLISEC)); 6974 status = RD_OB_DRWE(instance); 6975 if (retry++ == 100) { 6976 cmn_err(CE_WARN, "mrsas_reset_ppc: DRWE bit " 6977 "check retry count %d", retry); 6978 return (DDI_FAILURE); 6979 } 6980 } 6981 WR_IB_DRWE(status | DIAG_RESET_ADAPTER, instance); 6982 delay(100 * drv_usectohz(MILLISEC)); 6983 status = RD_OB_DRWE(instance); 6984 while (status & DIAG_RESET_ADAPTER) { 6985 delay(100 * drv_usectohz(MILLISEC)); 6986 status = RD_OB_DRWE(instance); 6987 if (retry++ == 100) { 6988 cmn_err(CE_WARN, "mrsas_reset_ppc: " 6989 "RESET FAILED. KILL adapter called."); 6990 6991 (void) mrsas_kill_adapter(instance); 6992 return (DDI_FAILURE); 6993 } 6994 } 6995 con_log(CL_ANN, (CE_NOTE, "mrsas_reset_ppc: Adapter reset complete")); 6996 con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: " 6997 "Calling mfi_state_transition_to_ready")); 6998 6999 /* Mark HBA as bad, if FW is fault after 3 continuous resets */ 7000 if (mfi_state_transition_to_ready(instance) || 7001 debug_fw_faults_after_ocr_g == 1) { 7002 cur_abs_reg_val = 7003 instance->func_ptr->read_fw_status_reg(instance); 7004 fw_state = cur_abs_reg_val & MFI_STATE_MASK; 7005 7006 #ifdef OCRDEBUG 7007 con_log(CL_ANN1, (CE_NOTE, 7008 "mrsas_reset_ppc :before fake: FW is not ready " 7009 "FW state = 0x%x", fw_state)); 7010 if (debug_fw_faults_after_ocr_g == 1) 7011 fw_state = MFI_STATE_FAULT; 7012 #endif 7013 7014 con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc : FW is not ready " 7015 "FW state = 0x%x", fw_state)); 7016 7017 if (fw_state == MFI_STATE_FAULT) { 7018 /* increment the count */ 7019 instance->fw_fault_count_after_ocr++; 7020 if (instance->fw_fault_count_after_ocr 7021 < MAX_FW_RESET_COUNT) { 7022 cmn_err(CE_WARN, "mrsas_reset_ppc: " 7023 "FW is in fault after OCR count %d " 7024 "Retry Reset", 7025 instance->fw_fault_count_after_ocr); 7026 goto retry_reset; 7027 7028 } else { 7029 cmn_err(CE_WARN, "mrsas_reset_ppc: " 7030 "Max Reset Count exceeded >%d" 7031 "Mark HBA as bad, KILL adapter", 7032 MAX_FW_RESET_COUNT); 7033 7034 (void) mrsas_kill_adapter(instance); 7035 return (DDI_FAILURE); 7036 } 7037 } 7038 } 7039 /* reset the counter as FW is up after OCR */ 7040 instance->fw_fault_count_after_ocr = 0; 7041 7042 7043 ddi_put32(instance->mfi_internal_dma_obj.acc_handle, 7044 instance->producer, 0); 7045 7046 ddi_put32(instance->mfi_internal_dma_obj.acc_handle, 7047 instance->consumer, 0); 7048 7049 con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: " 7050 " after resetting produconsumer chck indexs:" 7051 "producer %x consumer %x", *instance->producer, 7052 *instance->consumer)); 7053 7054 con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: " 7055 "Calling mrsas_issue_init_mfi")); 7056 (void) mrsas_issue_init_mfi(instance); 7057 con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: " 7058 "mrsas_issue_init_mfi Done")); 7059 7060 con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: " 7061 "Calling mrsas_print_pending_cmd\n")); 7062 (void) mrsas_print_pending_cmds(instance); 7063 con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: " 7064 "mrsas_print_pending_cmd done\n")); 7065 7066 instance->func_ptr->enable_intr(instance); 7067 instance->fw_outstanding = 0; 7068 7069 con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: " 7070 "Calling mrsas_issue_pending_cmds")); 7071 (void) mrsas_issue_pending_cmds(instance); 7072 con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: " 7073 "issue_pending_cmds done.\n")); 7074 7075 con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: " 7076 "Calling aen registration")); 7077 7078 7079 instance->aen_cmd->retry_count_for_ocr = 0; 7080 instance->aen_cmd->drv_pkt_time = 0; 7081 7082 instance->func_ptr->issue_cmd(instance->aen_cmd, instance); 7083 con_log(CL_ANN1, (CE_NOTE, "Unsetting adpresetinprogress flag.\n")); 7084 7085 mutex_enter(&instance->ocr_flags_mtx); 7086 instance->adapterresetinprogress = 0; 7087 mutex_exit(&instance->ocr_flags_mtx); 7088 con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: " 7089 "adpterresetinprogress flag unset")); 7090 7091 con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc done\n")); 7092 return (DDI_SUCCESS); 7093 } 7094 7095 /* 7096 * FMA functions. 7097 */ 7098 int 7099 mrsas_common_check(struct mrsas_instance *instance, struct mrsas_cmd *cmd) 7100 { 7101 int ret = DDI_SUCCESS; 7102 7103 if (cmd != NULL && 7104 mrsas_check_dma_handle(cmd->frame_dma_obj.dma_handle) != 7105 DDI_SUCCESS) { 7106 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED); 7107 if (cmd->pkt != NULL) { 7108 cmd->pkt->pkt_reason = CMD_TRAN_ERR; 7109 cmd->pkt->pkt_statistics = 0; 7110 } 7111 ret = DDI_FAILURE; 7112 } 7113 if (mrsas_check_dma_handle(instance->mfi_internal_dma_obj.dma_handle) 7114 != DDI_SUCCESS) { 7115 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED); 7116 if (cmd != NULL && cmd->pkt != NULL) { 7117 cmd->pkt->pkt_reason = CMD_TRAN_ERR; 7118 cmd->pkt->pkt_statistics = 0; 7119 } 7120 ret = DDI_FAILURE; 7121 } 7122 if (mrsas_check_dma_handle(instance->mfi_evt_detail_obj.dma_handle) != 7123 DDI_SUCCESS) { 7124 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED); 7125 if (cmd != NULL && cmd->pkt != NULL) { 7126 cmd->pkt->pkt_reason = CMD_TRAN_ERR; 7127 cmd->pkt->pkt_statistics = 0; 7128 } 7129 ret = DDI_FAILURE; 7130 } 7131 if (mrsas_check_acc_handle(instance->regmap_handle) != DDI_SUCCESS) { 7132 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED); 7133 7134 ddi_fm_acc_err_clear(instance->regmap_handle, DDI_FME_VER0); 7135 7136 if (cmd != NULL && cmd->pkt != NULL) { 7137 cmd->pkt->pkt_reason = CMD_TRAN_ERR; 7138 cmd->pkt->pkt_statistics = 0; 7139 } 7140 ret = DDI_FAILURE; 7141 } 7142 7143 return (ret); 7144 } 7145 7146 /*ARGSUSED*/ 7147 static int 7148 mrsas_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data) 7149 { 7150 /* 7151 * as the driver can always deal with an error in any dma or 7152 * access handle, we can just return the fme_status value. 7153 */ 7154 pci_ereport_post(dip, err, NULL); 7155 return (err->fme_status); 7156 } 7157 7158 static void 7159 mrsas_fm_init(struct mrsas_instance *instance) 7160 { 7161 /* Need to change iblock to priority for new MSI intr */ 7162 ddi_iblock_cookie_t fm_ibc; 7163 7164 /* Only register with IO Fault Services if we have some capability */ 7165 if (instance->fm_capabilities) { 7166 /* Adjust access and dma attributes for FMA */ 7167 endian_attr.devacc_attr_access = DDI_FLAGERR_ACC; 7168 mrsas_generic_dma_attr.dma_attr_flags = DDI_DMA_FLAGERR; 7169 7170 /* 7171 * Register capabilities with IO Fault Services. 7172 * fm_capabilities will be updated to indicate 7173 * capabilities actually supported (not requested.) 7174 */ 7175 7176 ddi_fm_init(instance->dip, &instance->fm_capabilities, &fm_ibc); 7177 7178 /* 7179 * Initialize pci ereport capabilities if ereport 7180 * capable (should always be.) 7181 */ 7182 7183 if (DDI_FM_EREPORT_CAP(instance->fm_capabilities) || 7184 DDI_FM_ERRCB_CAP(instance->fm_capabilities)) { 7185 pci_ereport_setup(instance->dip); 7186 } 7187 7188 /* 7189 * Register error callback if error callback capable. 7190 */ 7191 if (DDI_FM_ERRCB_CAP(instance->fm_capabilities)) { 7192 ddi_fm_handler_register(instance->dip, 7193 mrsas_fm_error_cb, (void*) instance); 7194 } 7195 } else { 7196 endian_attr.devacc_attr_access = DDI_DEFAULT_ACC; 7197 mrsas_generic_dma_attr.dma_attr_flags = 0; 7198 } 7199 } 7200 7201 static void 7202 mrsas_fm_fini(struct mrsas_instance *instance) 7203 { 7204 /* Only unregister FMA capabilities if registered */ 7205 if (instance->fm_capabilities) { 7206 /* 7207 * Un-register error callback if error callback capable. 7208 */ 7209 if (DDI_FM_ERRCB_CAP(instance->fm_capabilities)) { 7210 ddi_fm_handler_unregister(instance->dip); 7211 } 7212 7213 /* 7214 * Release any resources allocated by pci_ereport_setup() 7215 */ 7216 if (DDI_FM_EREPORT_CAP(instance->fm_capabilities) || 7217 DDI_FM_ERRCB_CAP(instance->fm_capabilities)) { 7218 pci_ereport_teardown(instance->dip); 7219 } 7220 7221 /* Unregister from IO Fault Services */ 7222 ddi_fm_fini(instance->dip); 7223 7224 /* Adjust access and dma attributes for FMA */ 7225 endian_attr.devacc_attr_access = DDI_DEFAULT_ACC; 7226 mrsas_generic_dma_attr.dma_attr_flags = 0; 7227 } 7228 } 7229 7230 int 7231 mrsas_check_acc_handle(ddi_acc_handle_t handle) 7232 { 7233 ddi_fm_error_t de; 7234 7235 if (handle == NULL) { 7236 return (DDI_FAILURE); 7237 } 7238 7239 ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION); 7240 7241 return (de.fme_status); 7242 } 7243 7244 int 7245 mrsas_check_dma_handle(ddi_dma_handle_t handle) 7246 { 7247 ddi_fm_error_t de; 7248 7249 if (handle == NULL) { 7250 return (DDI_FAILURE); 7251 } 7252 7253 ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION); 7254 7255 return (de.fme_status); 7256 } 7257 7258 void 7259 mrsas_fm_ereport(struct mrsas_instance *instance, char *detail) 7260 { 7261 uint64_t ena; 7262 char buf[FM_MAX_CLASS]; 7263 7264 (void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail); 7265 ena = fm_ena_generate(0, FM_ENA_FMT1); 7266 if (DDI_FM_EREPORT_CAP(instance->fm_capabilities)) { 7267 ddi_fm_ereport_post(instance->dip, buf, ena, DDI_NOSLEEP, 7268 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERSION, NULL); 7269 } 7270 } 7271 7272 static int 7273 mrsas_add_intrs(struct mrsas_instance *instance, int intr_type) 7274 { 7275 7276 dev_info_t *dip = instance->dip; 7277 int avail, actual, count; 7278 int i, flag, ret; 7279 7280 con_log(CL_DLEVEL1, (CE_NOTE, "mrsas_add_intrs: intr_type = %x", 7281 intr_type)); 7282 7283 /* Get number of interrupts */ 7284 ret = ddi_intr_get_nintrs(dip, intr_type, &count); 7285 if ((ret != DDI_SUCCESS) || (count == 0)) { 7286 con_log(CL_ANN, (CE_WARN, "ddi_intr_get_nintrs() failed:" 7287 "ret %d count %d", ret, count)); 7288 7289 return (DDI_FAILURE); 7290 } 7291 7292 con_log(CL_DLEVEL1, (CE_NOTE, "mrsas_add_intrs: count = %d ", count)); 7293 7294 /* Get number of available interrupts */ 7295 ret = ddi_intr_get_navail(dip, intr_type, &avail); 7296 if ((ret != DDI_SUCCESS) || (avail == 0)) { 7297 con_log(CL_ANN, (CE_WARN, "ddi_intr_get_navail() failed:" 7298 "ret %d avail %d", ret, avail)); 7299 7300 return (DDI_FAILURE); 7301 } 7302 con_log(CL_DLEVEL1, (CE_NOTE, "mrsas_add_intrs: avail = %d ", avail)); 7303 7304 /* Only one interrupt routine. So limit the count to 1 */ 7305 if (count > 1) { 7306 count = 1; 7307 } 7308 7309 /* 7310 * Allocate an array of interrupt handlers. Currently we support 7311 * only one interrupt. The framework can be extended later. 7312 */ 7313 instance->intr_htable_size = count * sizeof (ddi_intr_handle_t); 7314 instance->intr_htable = kmem_zalloc(instance->intr_htable_size, 7315 KM_SLEEP); 7316 ASSERT(instance->intr_htable); 7317 7318 flag = ((intr_type == DDI_INTR_TYPE_MSI) || 7319 (intr_type == DDI_INTR_TYPE_MSIX)) ? 7320 DDI_INTR_ALLOC_STRICT : DDI_INTR_ALLOC_NORMAL; 7321 7322 /* Allocate interrupt */ 7323 ret = ddi_intr_alloc(dip, instance->intr_htable, intr_type, 0, 7324 count, &actual, flag); 7325 7326 if ((ret != DDI_SUCCESS) || (actual == 0)) { 7327 con_log(CL_ANN, (CE_WARN, "mrsas_add_intrs: " 7328 "avail = %d", avail)); 7329 goto mrsas_free_htable; 7330 } 7331 7332 if (actual < count) { 7333 con_log(CL_ANN, (CE_WARN, "mrsas_add_intrs: " 7334 "Requested = %d Received = %d", count, actual)); 7335 } 7336 instance->intr_cnt = actual; 7337 7338 /* 7339 * Get the priority of the interrupt allocated. 7340 */ 7341 if ((ret = ddi_intr_get_pri(instance->intr_htable[0], 7342 &instance->intr_pri)) != DDI_SUCCESS) { 7343 con_log(CL_ANN, (CE_WARN, "mrsas_add_intrs: " 7344 "get priority call failed")); 7345 goto mrsas_free_handles; 7346 } 7347 7348 /* 7349 * Test for high level mutex. we don't support them. 7350 */ 7351 if (instance->intr_pri >= ddi_intr_get_hilevel_pri()) { 7352 con_log(CL_ANN, (CE_WARN, "mrsas_add_intrs: " 7353 "High level interrupts not supported.")); 7354 goto mrsas_free_handles; 7355 } 7356 7357 con_log(CL_DLEVEL1, (CE_NOTE, "mrsas_add_intrs: intr_pri = 0x%x ", 7358 instance->intr_pri)); 7359 7360 /* Call ddi_intr_add_handler() */ 7361 for (i = 0; i < actual; i++) { 7362 ret = ddi_intr_add_handler(instance->intr_htable[i], 7363 (ddi_intr_handler_t *)mrsas_isr, (caddr_t)instance, 7364 (caddr_t)(uintptr_t)i); 7365 7366 if (ret != DDI_SUCCESS) { 7367 con_log(CL_ANN, (CE_WARN, "mrsas_add_intrs:" 7368 "failed %d", ret)); 7369 goto mrsas_free_handles; 7370 } 7371 7372 } 7373 7374 con_log(CL_DLEVEL1, (CE_NOTE, " ddi_intr_add_handler done")); 7375 7376 if ((ret = ddi_intr_get_cap(instance->intr_htable[0], 7377 &instance->intr_cap)) != DDI_SUCCESS) { 7378 con_log(CL_ANN, (CE_WARN, "ddi_intr_get_cap() failed %d", 7379 ret)); 7380 goto mrsas_free_handlers; 7381 } 7382 7383 if (instance->intr_cap & DDI_INTR_FLAG_BLOCK) { 7384 con_log(CL_ANN, (CE_WARN, "Calling ddi_intr_block _enable")); 7385 7386 (void) ddi_intr_block_enable(instance->intr_htable, 7387 instance->intr_cnt); 7388 } else { 7389 con_log(CL_ANN, (CE_NOTE, " calling ddi_intr_enable")); 7390 7391 for (i = 0; i < instance->intr_cnt; i++) { 7392 (void) ddi_intr_enable(instance->intr_htable[i]); 7393 con_log(CL_ANN, (CE_NOTE, "ddi intr enable returns " 7394 "%d", i)); 7395 } 7396 } 7397 7398 return (DDI_SUCCESS); 7399 7400 mrsas_free_handlers: 7401 for (i = 0; i < actual; i++) 7402 (void) ddi_intr_remove_handler(instance->intr_htable[i]); 7403 7404 mrsas_free_handles: 7405 for (i = 0; i < actual; i++) 7406 (void) ddi_intr_free(instance->intr_htable[i]); 7407 7408 mrsas_free_htable: 7409 if (instance->intr_htable != NULL) 7410 kmem_free(instance->intr_htable, instance->intr_htable_size); 7411 7412 instance->intr_htable = NULL; 7413 instance->intr_htable_size = 0; 7414 7415 return (DDI_FAILURE); 7416 7417 } 7418 7419 7420 static void 7421 mrsas_rem_intrs(struct mrsas_instance *instance) 7422 { 7423 int i; 7424 7425 con_log(CL_ANN, (CE_NOTE, "mrsas_rem_intrs called")); 7426 7427 /* Disable all interrupts first */ 7428 if (instance->intr_cap & DDI_INTR_FLAG_BLOCK) { 7429 (void) ddi_intr_block_disable(instance->intr_htable, 7430 instance->intr_cnt); 7431 } else { 7432 for (i = 0; i < instance->intr_cnt; i++) { 7433 (void) ddi_intr_disable(instance->intr_htable[i]); 7434 } 7435 } 7436 7437 /* Remove all the handlers */ 7438 7439 for (i = 0; i < instance->intr_cnt; i++) { 7440 (void) ddi_intr_remove_handler(instance->intr_htable[i]); 7441 (void) ddi_intr_free(instance->intr_htable[i]); 7442 } 7443 7444 if (instance->intr_htable != NULL) 7445 kmem_free(instance->intr_htable, instance->intr_htable_size); 7446 7447 instance->intr_htable = NULL; 7448 instance->intr_htable_size = 0; 7449 7450 } 7451 7452 static int 7453 mrsas_tran_bus_config(dev_info_t *parent, uint_t flags, 7454 ddi_bus_config_op_t op, void *arg, dev_info_t **childp) 7455 { 7456 struct mrsas_instance *instance; 7457 int config; 7458 int rval = NDI_SUCCESS; 7459 7460 char *ptr = NULL; 7461 int tgt, lun; 7462 7463 con_log(CL_ANN1, (CE_NOTE, "Bus config called for op = %x", op)); 7464 7465 if ((instance = ddi_get_soft_state(mrsas_state, 7466 ddi_get_instance(parent))) == NULL) { 7467 return (NDI_FAILURE); 7468 } 7469 7470 /* Hold nexus during bus_config */ 7471 ndi_devi_enter(parent, &config); 7472 switch (op) { 7473 case BUS_CONFIG_ONE: { 7474 7475 /* parse wwid/target name out of name given */ 7476 if ((ptr = strchr((char *)arg, '@')) == NULL) { 7477 rval = NDI_FAILURE; 7478 break; 7479 } 7480 ptr++; 7481 7482 if (mrsas_parse_devname(arg, &tgt, &lun) != 0) { 7483 rval = NDI_FAILURE; 7484 break; 7485 } 7486 7487 if (lun == 0) { 7488 rval = mrsas_config_ld(instance, tgt, lun, childp); 7489 #ifdef PDSUPPORT 7490 } else if (instance->tbolt == 1 && lun != 0) { 7491 rval = mrsas_tbolt_config_pd(instance, 7492 tgt, lun, childp); 7493 #endif 7494 } else { 7495 rval = NDI_FAILURE; 7496 } 7497 7498 break; 7499 } 7500 case BUS_CONFIG_DRIVER: 7501 case BUS_CONFIG_ALL: { 7502 7503 rval = mrsas_config_all_devices(instance); 7504 7505 rval = NDI_SUCCESS; 7506 break; 7507 } 7508 } 7509 7510 if (rval == NDI_SUCCESS) { 7511 rval = ndi_busop_bus_config(parent, flags, op, arg, childp, 0); 7512 7513 } 7514 ndi_devi_exit(parent, config); 7515 7516 con_log(CL_ANN1, (CE_NOTE, "mrsas_tran_bus_config: rval = %x", 7517 rval)); 7518 return (rval); 7519 } 7520 7521 static int 7522 mrsas_config_all_devices(struct mrsas_instance *instance) 7523 { 7524 int rval, tgt; 7525 7526 for (tgt = 0; tgt < MRDRV_MAX_LD; tgt++) { 7527 (void) mrsas_config_ld(instance, tgt, 0, NULL); 7528 7529 } 7530 7531 #ifdef PDSUPPORT 7532 /* Config PD devices connected to the card */ 7533 if (instance->tbolt) { 7534 for (tgt = 0; tgt < instance->mr_tbolt_pd_max; tgt++) { 7535 (void) mrsas_tbolt_config_pd(instance, tgt, 1, NULL); 7536 } 7537 } 7538 #endif 7539 7540 rval = NDI_SUCCESS; 7541 return (rval); 7542 } 7543 7544 static int 7545 mrsas_parse_devname(char *devnm, int *tgt, int *lun) 7546 { 7547 char devbuf[SCSI_MAXNAMELEN]; 7548 char *addr; 7549 char *p, *tp, *lp; 7550 long num; 7551 7552 /* Parse dev name and address */ 7553 (void) strcpy(devbuf, devnm); 7554 addr = ""; 7555 for (p = devbuf; *p != '\0'; p++) { 7556 if (*p == '@') { 7557 addr = p + 1; 7558 *p = '\0'; 7559 } else if (*p == ':') { 7560 *p = '\0'; 7561 break; 7562 } 7563 } 7564 7565 /* Parse target and lun */ 7566 for (p = tp = addr, lp = NULL; *p != '\0'; p++) { 7567 if (*p == ',') { 7568 lp = p + 1; 7569 *p = '\0'; 7570 break; 7571 } 7572 } 7573 if (tgt && tp) { 7574 if (ddi_strtol(tp, NULL, 0x10, &num)) { 7575 return (DDI_FAILURE); /* Can declare this as constant */ 7576 } 7577 *tgt = (int)num; 7578 } 7579 if (lun && lp) { 7580 if (ddi_strtol(lp, NULL, 0x10, &num)) { 7581 return (DDI_FAILURE); 7582 } 7583 *lun = (int)num; 7584 } 7585 return (DDI_SUCCESS); /* Success case */ 7586 } 7587 7588 static int 7589 mrsas_config_ld(struct mrsas_instance *instance, uint16_t tgt, 7590 uint8_t lun, dev_info_t **ldip) 7591 { 7592 struct scsi_device *sd; 7593 dev_info_t *child; 7594 int rval; 7595 7596 con_log(CL_DLEVEL1, (CE_NOTE, "mrsas_config_ld: t = %d l = %d", 7597 tgt, lun)); 7598 7599 if ((child = mrsas_find_child(instance, tgt, lun)) != NULL) { 7600 if (ldip) { 7601 *ldip = child; 7602 } 7603 if (instance->mr_ld_list[tgt].flag != MRDRV_TGT_VALID) { 7604 rval = mrsas_service_evt(instance, tgt, 0, 7605 MRSAS_EVT_UNCONFIG_TGT, NULL); 7606 con_log(CL_ANN1, (CE_WARN, 7607 "mr_sas: DELETING STALE ENTRY rval = %d " 7608 "tgt id = %d ", rval, tgt)); 7609 return (NDI_FAILURE); 7610 } 7611 return (NDI_SUCCESS); 7612 } 7613 7614 sd = kmem_zalloc(sizeof (struct scsi_device), KM_SLEEP); 7615 sd->sd_address.a_hba_tran = instance->tran; 7616 sd->sd_address.a_target = (uint16_t)tgt; 7617 sd->sd_address.a_lun = (uint8_t)lun; 7618 7619 if (scsi_hba_probe(sd, NULL) == SCSIPROBE_EXISTS) 7620 rval = mrsas_config_scsi_device(instance, sd, ldip); 7621 else 7622 rval = NDI_FAILURE; 7623 7624 /* sd_unprobe is blank now. Free buffer manually */ 7625 if (sd->sd_inq) { 7626 kmem_free(sd->sd_inq, SUN_INQSIZE); 7627 sd->sd_inq = (struct scsi_inquiry *)NULL; 7628 } 7629 7630 kmem_free(sd, sizeof (struct scsi_device)); 7631 con_log(CL_DLEVEL1, (CE_NOTE, "mrsas_config_ld: return rval = %d", 7632 rval)); 7633 return (rval); 7634 } 7635 7636 int 7637 mrsas_config_scsi_device(struct mrsas_instance *instance, 7638 struct scsi_device *sd, dev_info_t **dipp) 7639 { 7640 char *nodename = NULL; 7641 char **compatible = NULL; 7642 int ncompatible = 0; 7643 char *childname; 7644 dev_info_t *ldip = NULL; 7645 int tgt = sd->sd_address.a_target; 7646 int lun = sd->sd_address.a_lun; 7647 int dtype = sd->sd_inq->inq_dtype & DTYPE_MASK; 7648 int rval; 7649 7650 con_log(CL_DLEVEL1, (CE_NOTE, "mr_sas: scsi_device t%dL%d", tgt, lun)); 7651 scsi_hba_nodename_compatible_get(sd->sd_inq, NULL, dtype, 7652 NULL, &nodename, &compatible, &ncompatible); 7653 7654 if (nodename == NULL) { 7655 con_log(CL_ANN1, (CE_WARN, "mr_sas: Found no compatible driver " 7656 "for t%dL%d", tgt, lun)); 7657 rval = NDI_FAILURE; 7658 goto finish; 7659 } 7660 7661 childname = (dtype == DTYPE_DIRECT) ? "sd" : nodename; 7662 con_log(CL_DLEVEL1, (CE_NOTE, 7663 "mr_sas: Childname = %2s nodename = %s", childname, nodename)); 7664 7665 /* Create a dev node */ 7666 rval = ndi_devi_alloc(instance->dip, childname, DEVI_SID_NODEID, &ldip); 7667 con_log(CL_DLEVEL1, (CE_NOTE, 7668 "mr_sas_config_scsi_device: ndi_devi_alloc rval = %x", rval)); 7669 if (rval == NDI_SUCCESS) { 7670 if (ndi_prop_update_int(DDI_DEV_T_NONE, ldip, "target", tgt) != 7671 DDI_PROP_SUCCESS) { 7672 con_log(CL_ANN1, (CE_WARN, "mr_sas: unable to create " 7673 "property for t%dl%d target", tgt, lun)); 7674 rval = NDI_FAILURE; 7675 goto finish; 7676 } 7677 if (ndi_prop_update_int(DDI_DEV_T_NONE, ldip, "lun", lun) != 7678 DDI_PROP_SUCCESS) { 7679 con_log(CL_ANN1, (CE_WARN, "mr_sas: unable to create " 7680 "property for t%dl%d lun", tgt, lun)); 7681 rval = NDI_FAILURE; 7682 goto finish; 7683 } 7684 7685 if (ndi_prop_update_string_array(DDI_DEV_T_NONE, ldip, 7686 "compatible", compatible, ncompatible) != 7687 DDI_PROP_SUCCESS) { 7688 con_log(CL_ANN1, (CE_WARN, "mr_sas: unable to create " 7689 "property for t%dl%d compatible", tgt, lun)); 7690 rval = NDI_FAILURE; 7691 goto finish; 7692 } 7693 7694 rval = ndi_devi_online(ldip, NDI_ONLINE_ATTACH); 7695 if (rval != NDI_SUCCESS) { 7696 con_log(CL_ANN1, (CE_WARN, "mr_sas: unable to online " 7697 "t%dl%d", tgt, lun)); 7698 ndi_prop_remove_all(ldip); 7699 (void) ndi_devi_free(ldip); 7700 } else { 7701 con_log(CL_ANN1, (CE_CONT, "mr_sas: online Done :" 7702 "0 t%dl%d", tgt, lun)); 7703 } 7704 7705 } 7706 finish: 7707 if (dipp) { 7708 *dipp = ldip; 7709 } 7710 7711 con_log(CL_DLEVEL1, (CE_NOTE, 7712 "mr_sas: config_scsi_device rval = %d t%dL%d", 7713 rval, tgt, lun)); 7714 scsi_hba_nodename_compatible_free(nodename, compatible); 7715 return (rval); 7716 } 7717 7718 /*ARGSUSED*/ 7719 int 7720 mrsas_service_evt(struct mrsas_instance *instance, int tgt, int lun, int event, 7721 uint64_t wwn) 7722 { 7723 struct mrsas_eventinfo *mrevt = NULL; 7724 7725 con_log(CL_ANN1, (CE_NOTE, 7726 "mrsas_service_evt called for t%dl%d event = %d", 7727 tgt, lun, event)); 7728 7729 if ((instance->taskq == NULL) || (mrevt = 7730 kmem_zalloc(sizeof (struct mrsas_eventinfo), KM_NOSLEEP)) == NULL) { 7731 return (ENOMEM); 7732 } 7733 7734 mrevt->instance = instance; 7735 mrevt->tgt = tgt; 7736 mrevt->lun = lun; 7737 mrevt->event = event; 7738 mrevt->wwn = wwn; 7739 7740 if ((ddi_taskq_dispatch(instance->taskq, 7741 (void (*)(void *))mrsas_issue_evt_taskq, mrevt, DDI_NOSLEEP)) != 7742 DDI_SUCCESS) { 7743 con_log(CL_ANN1, (CE_NOTE, 7744 "mr_sas: Event task failed for t%dl%d event = %d", 7745 tgt, lun, event)); 7746 kmem_free(mrevt, sizeof (struct mrsas_eventinfo)); 7747 return (DDI_FAILURE); 7748 } 7749 DTRACE_PROBE3(service_evt, int, tgt, int, lun, int, event); 7750 return (DDI_SUCCESS); 7751 } 7752 7753 static void 7754 mrsas_issue_evt_taskq(struct mrsas_eventinfo *mrevt) 7755 { 7756 struct mrsas_instance *instance = mrevt->instance; 7757 dev_info_t *dip, *pdip; 7758 int circ1 = 0; 7759 char *devname; 7760 7761 con_log(CL_ANN1, (CE_NOTE, "mrsas_issue_evt_taskq: called for" 7762 " tgt %d lun %d event %d", 7763 mrevt->tgt, mrevt->lun, mrevt->event)); 7764 7765 if (mrevt->tgt < MRDRV_MAX_LD && mrevt->lun == 0) { 7766 mutex_enter(&instance->config_dev_mtx); 7767 dip = instance->mr_ld_list[mrevt->tgt].dip; 7768 mutex_exit(&instance->config_dev_mtx); 7769 #ifdef PDSUPPORT 7770 } else { 7771 mutex_enter(&instance->config_dev_mtx); 7772 dip = instance->mr_tbolt_pd_list[mrevt->tgt].dip; 7773 mutex_exit(&instance->config_dev_mtx); 7774 #endif 7775 } 7776 7777 7778 ndi_devi_enter(instance->dip, &circ1); 7779 switch (mrevt->event) { 7780 case MRSAS_EVT_CONFIG_TGT: 7781 if (dip == NULL) { 7782 7783 if (mrevt->lun == 0) { 7784 (void) mrsas_config_ld(instance, mrevt->tgt, 7785 0, NULL); 7786 #ifdef PDSUPPORT 7787 } else if (instance->tbolt) { 7788 (void) mrsas_tbolt_config_pd(instance, 7789 mrevt->tgt, 7790 1, NULL); 7791 #endif 7792 } 7793 con_log(CL_ANN1, (CE_NOTE, 7794 "mr_sas: EVT_CONFIG_TGT called:" 7795 " for tgt %d lun %d event %d", 7796 mrevt->tgt, mrevt->lun, mrevt->event)); 7797 7798 } else { 7799 con_log(CL_ANN1, (CE_NOTE, 7800 "mr_sas: EVT_CONFIG_TGT dip != NULL:" 7801 " for tgt %d lun %d event %d", 7802 mrevt->tgt, mrevt->lun, mrevt->event)); 7803 } 7804 break; 7805 case MRSAS_EVT_UNCONFIG_TGT: 7806 if (dip) { 7807 if (i_ddi_devi_attached(dip)) { 7808 7809 pdip = ddi_get_parent(dip); 7810 7811 devname = kmem_zalloc(MAXNAMELEN + 1, KM_SLEEP); 7812 (void) ddi_deviname(dip, devname); 7813 7814 (void) devfs_clean(pdip, devname + 1, 7815 DV_CLEAN_FORCE); 7816 kmem_free(devname, MAXNAMELEN + 1); 7817 } 7818 (void) ndi_devi_offline(dip, NDI_DEVI_REMOVE); 7819 con_log(CL_ANN1, (CE_NOTE, 7820 "mr_sas: EVT_UNCONFIG_TGT called:" 7821 " for tgt %d lun %d event %d", 7822 mrevt->tgt, mrevt->lun, mrevt->event)); 7823 } else { 7824 con_log(CL_ANN1, (CE_NOTE, 7825 "mr_sas: EVT_UNCONFIG_TGT dip == NULL:" 7826 " for tgt %d lun %d event %d", 7827 mrevt->tgt, mrevt->lun, mrevt->event)); 7828 } 7829 break; 7830 } 7831 kmem_free(mrevt, sizeof (struct mrsas_eventinfo)); 7832 ndi_devi_exit(instance->dip, circ1); 7833 } 7834 7835 7836 int 7837 mrsas_mode_sense_build(struct scsi_pkt *pkt) 7838 { 7839 union scsi_cdb *cdbp; 7840 uint16_t page_code; 7841 struct scsa_cmd *acmd; 7842 struct buf *bp; 7843 struct mode_header *modehdrp; 7844 7845 cdbp = (void *)pkt->pkt_cdbp; 7846 page_code = cdbp->cdb_un.sg.scsi[0]; 7847 acmd = PKT2CMD(pkt); 7848 bp = acmd->cmd_buf; 7849 if ((!bp) && bp->b_un.b_addr && bp->b_bcount && acmd->cmd_dmacount) { 7850 con_log(CL_ANN1, (CE_WARN, "Failing MODESENSE Command")); 7851 /* ADD pkt statistics as Command failed. */ 7852 return (NULL); 7853 } 7854 7855 bp_mapin(bp); 7856 bzero(bp->b_un.b_addr, bp->b_bcount); 7857 7858 switch (page_code) { 7859 case 0x3: { 7860 struct mode_format *page3p = NULL; 7861 modehdrp = (struct mode_header *)(bp->b_un.b_addr); 7862 modehdrp->bdesc_length = MODE_BLK_DESC_LENGTH; 7863 7864 page3p = (void *)((caddr_t)modehdrp + 7865 MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH); 7866 page3p->mode_page.code = 0x3; 7867 page3p->mode_page.length = 7868 (uchar_t)(sizeof (struct mode_format)); 7869 page3p->data_bytes_sect = 512; 7870 page3p->sect_track = 63; 7871 break; 7872 } 7873 case 0x4: { 7874 struct mode_geometry *page4p = NULL; 7875 modehdrp = (struct mode_header *)(bp->b_un.b_addr); 7876 modehdrp->bdesc_length = MODE_BLK_DESC_LENGTH; 7877 7878 page4p = (void *)((caddr_t)modehdrp + 7879 MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH); 7880 page4p->mode_page.code = 0x4; 7881 page4p->mode_page.length = 7882 (uchar_t)(sizeof (struct mode_geometry)); 7883 page4p->heads = 255; 7884 page4p->rpm = 10000; 7885 break; 7886 } 7887 default: 7888 break; 7889 } 7890 return (NULL); 7891 } 7892