Print this page
3178 Support for LSI 2208 chipset in mr_sas
   1 /*
   2  * mr_sas.c: source for mr_sas driver
   3  *
   4  * MegaRAID device driver for SAS2.0 controllers
   5  * Copyright (c) 2008-2010, LSI Logic Corporation.
   6  * All rights reserved.
   7  *
   8  * Version:
   9  * Author:

  10  *              Arun Chandrashekhar
  11  *              Manju R
  12  *              Rajesh Prabhakaran
  13  *              Seokmann Ju
  14  *
  15  * Redistribution and use in source and binary forms, with or without
  16  * modification, are permitted provided that the following conditions are met:
  17  *
  18  * 1. Redistributions of source code must retain the above copyright notice,
  19  *    this list of conditions and the following disclaimer.
  20  *
  21  * 2. Redistributions in binary form must reproduce the above copyright notice,
  22  *    this list of conditions and the following disclaimer in the documentation
  23  *    and/or other materials provided with the distribution.
  24  *
  25  * 3. Neither the name of the author nor the names of its contributors may be
  26  *    used to endorse or promote products derived from this software without
  27  *    specific prior written permission.
  28  *
  29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  32  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  33  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  34  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  35  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  36  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  37  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  38  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  39  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  40  * DAMAGE.
  41  */
  42 
  43 /*
  44  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
  45  * Copyright (c) 2011 Bayard G. Bell. All rights reserved.

  46  */
  47 
  48 #include <sys/types.h>
  49 #include <sys/param.h>
  50 #include <sys/file.h>
  51 #include <sys/errno.h>
  52 #include <sys/open.h>
  53 #include <sys/cred.h>
  54 #include <sys/modctl.h>
  55 #include <sys/conf.h>
  56 #include <sys/devops.h>
  57 #include <sys/cmn_err.h>
  58 #include <sys/kmem.h>
  59 #include <sys/stat.h>
  60 #include <sys/mkdev.h>
  61 #include <sys/pci.h>
  62 #include <sys/scsi/scsi.h>
  63 #include <sys/ddi.h>
  64 #include <sys/sunddi.h>
  65 #include <sys/atomic.h>
  66 #include <sys/signal.h>
  67 #include <sys/byteorder.h>
  68 #include <sys/sdt.h>
  69 #include <sys/fs/dv_node.h>       /* devfs_clean */
  70 
  71 #include "mr_sas.h"
  72 
  73 /*
  74  * FMA header files
  75  */
  76 #include <sys/ddifm.h>
  77 #include <sys/fm/protocol.h>
  78 #include <sys/fm/util.h>
  79 #include <sys/fm/io/ddi.h>
  80 
  81 /*
  82  * Local static data
  83  */
  84 static void     *mrsas_state = NULL;
  85 static volatile boolean_t       mrsas_relaxed_ordering = B_TRUE;
  86 static volatile int     debug_level_g = CL_NONE;
  87 static volatile int     msi_enable = 1;
  88 static volatile int     ctio_enable = 1;
  89 
  90 /* Default Timeout value to issue online controller reset */
  91 static volatile int  debug_timeout_g  = 0xB4;
  92 /* Simulate consecutive firmware fault */
  93 static volatile int  debug_fw_faults_after_ocr_g  = 0;
  94 
  95 #ifdef OCRDEBUG
  96 /* Simulate three consecutive timeout for an IO */
  97 static volatile int  debug_consecutive_timeout_after_ocr_g  = 0;
  98 #endif
  99 
 100 #pragma weak scsi_hba_open
 101 #pragma weak scsi_hba_close
 102 #pragma weak scsi_hba_ioctl
 103 
 104 static ddi_dma_attr_t mrsas_generic_dma_attr = {

























































































































 105         DMA_ATTR_V0,            /* dma_attr_version */
 106         0,                      /* low DMA address range */
 107         0xFFFFFFFFU,            /* high DMA address range */
 108         0xFFFFFFFFU,            /* DMA counter register  */
 109         8,                      /* DMA address alignment */
 110         0x07,                   /* DMA burstsizes  */
 111         1,                      /* min DMA size */
 112         0xFFFFFFFFU,            /* max DMA size */
 113         0xFFFFFFFFU,            /* segment boundary */
 114         MRSAS_MAX_SGE_CNT,      /* dma_attr_sglen */
 115         512,                    /* granularity of device */
 116         0                       /* bus specific DMA flags */
 117 };
 118 
 119 int32_t mrsas_max_cap_maxxfer = 0x1000000;
 120 
 121 /*






 122  * cb_ops contains base level routines
 123  */
 124 static struct cb_ops mrsas_cb_ops = {
 125         mrsas_open,             /* open */
 126         mrsas_close,            /* close */
 127         nodev,                  /* strategy */
 128         nodev,                  /* print */
 129         nodev,                  /* dump */
 130         nodev,                  /* read */
 131         nodev,                  /* write */
 132         mrsas_ioctl,            /* ioctl */
 133         nodev,                  /* devmap */
 134         nodev,                  /* mmap */
 135         nodev,                  /* segmap */
 136         nochpoll,               /* poll */
 137         nodev,                  /* cb_prop_op */
 138         0,                      /* streamtab  */
 139         D_NEW | D_HOTPLUG,      /* cb_flag */
 140         CB_REV,                 /* cb_rev */
 141         nodev,                  /* cb_aread */


 170 
 171 static struct modldrv modldrv = {
 172         &mod_driverops,             /* module type - driver */
 173         MRSAS_VERSION,
 174         &mrsas_ops,         /* driver ops */
 175 };
 176 
 177 static struct modlinkage modlinkage = {
 178         MODREV_1,       /* ml_rev - must be MODREV_1 */
 179         &modldrv,   /* ml_linkage */
 180         NULL            /* end of driver linkage */
 181 };
 182 
 183 static struct ddi_device_acc_attr endian_attr = {
 184         DDI_DEVICE_ATTR_V1,
 185         DDI_STRUCTURE_LE_ACC,
 186         DDI_STRICTORDER_ACC,
 187         DDI_DEFAULT_ACC
 188 };
 189 


 190 

 191 /*
 192  * ************************************************************************** *
 193  *                                                                            *
 194  *         common entry points - for loadable kernel modules                  *
 195  *                                                                            *
 196  * ************************************************************************** *
 197  */
 198 











 199 int
 200 _init(void)
 201 {
 202         int ret;
 203 
 204         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
 205 
 206         ret = ddi_soft_state_init(&mrsas_state,
 207             sizeof (struct mrsas_instance), 0);
 208 
 209         if (ret != DDI_SUCCESS) {
 210                 con_log(CL_ANN, (CE_WARN, "mr_sas: could not init state"));
 211                 return (ret);
 212         }
 213 
 214         if ((ret = scsi_hba_init(&modlinkage)) != DDI_SUCCESS) {
 215                 con_log(CL_ANN, (CE_WARN, "mr_sas: could not init scsi hba"));
 216                 ddi_soft_state_fini(&mrsas_state);
 217                 return (ret);
 218         }
 219 
 220         ret = mod_install(&modlinkage);
 221 
 222         if (ret != DDI_SUCCESS) {
 223                 con_log(CL_ANN, (CE_WARN, "mr_sas: mod_install failed"));
 224                 scsi_hba_fini(&modlinkage);
 225                 ddi_soft_state_fini(&mrsas_state);
 226         }
 227 
 228         return (ret);
 229 }
 230 







 231 int
 232 _info(struct modinfo *modinfop)
 233 {
 234         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
 235 
 236         return (mod_info(&modlinkage, modinfop));
 237 }
 238 







 239 int
 240 _fini(void)
 241 {
 242         int ret;
 243 
 244         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
 245 
 246         if ((ret = mod_remove(&modlinkage)) != DDI_SUCCESS)


 247                 return (ret);

 248 
 249         scsi_hba_fini(&modlinkage);

 250 
 251         ddi_soft_state_fini(&mrsas_state);

 252 
 253         return (ret);
 254 }
 255 
 256 
 257 /*
 258  * ************************************************************************** *
 259  *                                                                            *
 260  *               common entry points - for autoconfiguration                  *
 261  *                                                                            *
 262  * ************************************************************************** *
 263  */
 264 






















 265 static int
 266 mrsas_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
 267 {
 268         int             instance_no;
 269         int             nregs;
 270         uint8_t         added_isr_f = 0;
 271         uint8_t         added_soft_isr_f = 0;
 272         uint8_t         create_devctl_node_f = 0;
 273         uint8_t         create_scsi_node_f = 0;
 274         uint8_t         create_ioc_node_f = 0;
 275         uint8_t         tran_alloc_f = 0;
 276         uint8_t         irq;
 277         uint16_t        vendor_id;
 278         uint16_t        device_id;
 279         uint16_t        subsysvid;
 280         uint16_t        subsysid;
 281         uint16_t        command;
 282         off_t           reglength = 0;
 283         int             intr_types = 0;
 284         char            *data;
 285 
 286         scsi_hba_tran_t         *tran;
 287         ddi_dma_attr_t  tran_dma_attr;
 288         struct mrsas_instance   *instance;
 289 
 290         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
 291 
 292         /* CONSTCOND */
 293         ASSERT(NO_COMPETING_THREADS);
 294 
 295         instance_no = ddi_get_instance(dip);
 296 
 297         /*
 298          * check to see whether this device is in a DMA-capable slot.
 299          */
 300         if (ddi_slaveonly(dip) == DDI_SUCCESS) {
 301                 con_log(CL_ANN, (CE_WARN,
 302                     "mr_sas%d: Device in slave-only slot, unused",
 303                     instance_no));
 304                 return (DDI_FAILURE);
 305         }
 306 
 307         switch (cmd) {
 308                 case DDI_ATTACH:
 309                         con_log(CL_DLEVEL1, (CE_NOTE, "mr_sas: DDI_ATTACH"));
 310                         /* allocate the soft state for the instance */
 311                         if (ddi_soft_state_zalloc(mrsas_state, instance_no)
 312                             != DDI_SUCCESS) {
 313                                 con_log(CL_ANN, (CE_WARN,
 314                                     "mr_sas%d: Failed to allocate soft state",
 315                                     instance_no));
 316 
 317                                 return (DDI_FAILURE);
 318                         }
 319 
 320                         instance = (struct mrsas_instance *)ddi_get_soft_state
 321                             (mrsas_state, instance_no);
 322 
 323                         if (instance == NULL) {
 324                                 con_log(CL_ANN, (CE_WARN,
 325                                     "mr_sas%d: Bad soft state", instance_no));
 326 
 327                                 ddi_soft_state_free(mrsas_state, instance_no);
 328 
 329                                 return (DDI_FAILURE);
 330                         }
 331 
 332                         bzero((caddr_t)instance,
 333                             sizeof (struct mrsas_instance));
 334 
 335                         instance->func_ptr = kmem_zalloc(
 336                             sizeof (struct mrsas_func_ptr), KM_SLEEP);
 337                         ASSERT(instance->func_ptr);
 338 
 339                         /* Setup the PCI configuration space handles */
 340                         if (pci_config_setup(dip, &instance->pci_handle) !=
 341                             DDI_SUCCESS) {
 342                                 con_log(CL_ANN, (CE_WARN,
 343                                     "mr_sas%d: pci config setup failed ",
 344                                     instance_no));
 345 
 346                                 kmem_free(instance->func_ptr,
 347                                     sizeof (struct mrsas_func_ptr));
 348                                 ddi_soft_state_free(mrsas_state, instance_no);
 349 
 350                                 return (DDI_FAILURE);
 351                         }
 352 
 353                         if (ddi_dev_nregs(dip, &nregs) != DDI_SUCCESS) {
 354                                 con_log(CL_ANN, (CE_WARN,
 355                                     "mr_sas: failed to get registers."));
 356 
 357                                 pci_config_teardown(&instance->pci_handle);
 358                                 kmem_free(instance->func_ptr,
 359                                     sizeof (struct mrsas_func_ptr));
 360                                 ddi_soft_state_free(mrsas_state, instance_no);
 361 
 362                                 return (DDI_FAILURE);
 363                         }
 364 
 365                         vendor_id = pci_config_get16(instance->pci_handle,
 366                             PCI_CONF_VENID);
 367                         device_id = pci_config_get16(instance->pci_handle,
 368                             PCI_CONF_DEVID);
 369 
 370                         subsysvid = pci_config_get16(instance->pci_handle,
 371                             PCI_CONF_SUBVENID);
 372                         subsysid = pci_config_get16(instance->pci_handle,
 373                             PCI_CONF_SUBSYSID);
 374 
 375                         pci_config_put16(instance->pci_handle, PCI_CONF_COMM,
 376                             (pci_config_get16(instance->pci_handle,
 377                             PCI_CONF_COMM) | PCI_COMM_ME));
 378                         irq = pci_config_get8(instance->pci_handle,
 379                             PCI_CONF_ILINE);
 380 
 381                         con_log(CL_DLEVEL1, (CE_CONT, "mr_sas%d: "


 384                             subsysid, irq, MRSAS_VERSION));
 385 
 386                         /* enable bus-mastering */
 387                         command = pci_config_get16(instance->pci_handle,
 388                             PCI_CONF_COMM);
 389 
 390                         if (!(command & PCI_COMM_ME)) {
 391                                 command |= PCI_COMM_ME;
 392 
 393                                 pci_config_put16(instance->pci_handle,
 394                                     PCI_CONF_COMM, command);
 395 
 396                                 con_log(CL_ANN, (CE_CONT, "mr_sas%d: "
 397                                     "enable bus-mastering", instance_no));
 398                         } else {
 399                                 con_log(CL_DLEVEL1, (CE_CONT, "mr_sas%d: "
 400                                 "bus-mastering already set", instance_no));
 401                         }
 402 
 403                         /* initialize function pointers */
 404                         if ((device_id == PCI_DEVICE_ID_LSI_2108VDE) ||
 405                             (device_id == PCI_DEVICE_ID_LSI_2108V)) {
 406                                 con_log(CL_DLEVEL1, (CE_CONT, "mr_sas%d: "
 407                                     "2108V/DE detected", instance_no));
 408                                 instance->func_ptr->read_fw_status_reg =
 409                                     read_fw_status_reg_ppc;
 410                                 instance->func_ptr->issue_cmd = issue_cmd_ppc;
 411                                 instance->func_ptr->issue_cmd_in_sync_mode =
 412                                     issue_cmd_in_sync_mode_ppc;
 413                                 instance->func_ptr->issue_cmd_in_poll_mode =
 414                                     issue_cmd_in_poll_mode_ppc;
 415                                 instance->func_ptr->enable_intr =
 416                                     enable_intr_ppc;
 417                                 instance->func_ptr->disable_intr =
 418                                     disable_intr_ppc;
 419                                 instance->func_ptr->intr_ack = intr_ack_ppc;
 420                         } else {
 421                                 con_log(CL_ANN, (CE_WARN,
 422                                     "mr_sas: Invalid device detected"));
 423 


















 424                                 pci_config_teardown(&instance->pci_handle);
 425                                 kmem_free(instance->func_ptr,
 426                                     sizeof (struct mrsas_func_ptr));
 427                                 ddi_soft_state_free(mrsas_state, instance_no);
 428 
 429                                 return (DDI_FAILURE);
 430                         }
 431 
 432                         instance->baseaddress = pci_config_get32(
 433                             instance->pci_handle, PCI_CONF_BASE0);
 434                         instance->baseaddress &= 0x0fffc;
 435 
 436                         instance->dip                = dip;
 437                         instance->vendor_id  = vendor_id;
 438                         instance->device_id  = device_id;
 439                         instance->subsysvid  = subsysvid;
 440                         instance->subsysid   = subsysid;
 441                         instance->instance   = instance_no;
 442 
 443                         /* Initialize FMA */
 444                         instance->fm_capabilities = ddi_prop_get_int(
 445                             DDI_DEV_T_ANY, instance->dip, DDI_PROP_DONTPASS,
 446                             "fm-capable", DDI_FM_EREPORT_CAPABLE |
 447                             DDI_FM_ACCCHK_CAPABLE | DDI_FM_DMACHK_CAPABLE
 448                             | DDI_FM_ERRCB_CAPABLE);
 449 
 450                         mrsas_fm_init(instance);
 451 
 452                         /* Initialize Interrupts */
 453                         if ((ddi_dev_regsize(instance->dip,
 454                             REGISTER_SET_IO_2108, &reglength) != DDI_SUCCESS) ||
 455                             reglength < MINIMUM_MFI_MEM_SZ) {
 456                                 return (DDI_FAILURE);
 457                         }
 458                         if (reglength > DEFAULT_MFI_MEM_SZ) {
 459                                 reglength = DEFAULT_MFI_MEM_SZ;
 460                                 con_log(CL_DLEVEL1, (CE_NOTE,
 461                                     "mr_sas: register length to map is "
 462                                     "0x%lx bytes", reglength));
 463                         }
 464                         if (ddi_regs_map_setup(instance->dip,
 465                             REGISTER_SET_IO_2108, &instance->regmap, 0,
 466                             reglength, &endian_attr, &instance->regmap_handle)
 467                             != DDI_SUCCESS) {
 468                                 con_log(CL_ANN, (CE_NOTE,
 469                                     "mr_sas: couldn't map control registers"));
 470                                 goto fail_attach;
 471                         }
 472 


 473                         /*
 474                          * Disable Interrupt Now.
 475                          * Setup Software interrupt
 476                          */
 477                         instance->func_ptr->disable_intr(instance);
 478 
 479                         if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
 480                             "mrsas-enable-msi", &data) == DDI_SUCCESS) {
 481                                 if (strncmp(data, "no", 3) == 0) {
 482                                         msi_enable = 0;
 483                                         con_log(CL_ANN1, (CE_WARN,
 484                                             "msi_enable = %d disabled",
 485                                             msi_enable));
 486                                 }
 487                                 ddi_prop_free(data);
 488                         }
 489 
 490                         con_log(CL_DLEVEL1, (CE_WARN, "msi_enable = %d",
 491                             msi_enable));
 492 














 493                         /* Check for all supported interrupt types */
 494                         if (ddi_intr_get_supported_types(
 495                             dip, &intr_types) != DDI_SUCCESS) {
 496                                 con_log(CL_ANN, (CE_WARN,
 497                                     "ddi_intr_get_supported_types() failed"));
 498                                 goto fail_attach;
 499                         }
 500 
 501                         con_log(CL_DLEVEL1, (CE_NOTE,
 502                             "ddi_intr_get_supported_types() ret: 0x%x",
 503                             intr_types));
 504 
 505                         /* Initialize and Setup Interrupt handler */
 506                         if (msi_enable && (intr_types & DDI_INTR_TYPE_MSIX)) {
 507                                 if (mrsas_add_intrs(instance,
 508                                     DDI_INTR_TYPE_MSIX) != DDI_SUCCESS) {
 509                                         con_log(CL_ANN, (CE_WARN,
 510                                             "MSIX interrupt query failed"));
 511                                         goto fail_attach;
 512                                 }
 513                                 instance->intr_type = DDI_INTR_TYPE_MSIX;
 514                         } else if (msi_enable && (intr_types &
 515                             DDI_INTR_TYPE_MSI)) {
 516                                 if (mrsas_add_intrs(instance,
 517                                     DDI_INTR_TYPE_MSI) != DDI_SUCCESS) {
 518                                         con_log(CL_ANN, (CE_WARN,
 519                                             "MSI interrupt query failed"));
 520                                         goto fail_attach;
 521                                 }
 522                                 instance->intr_type = DDI_INTR_TYPE_MSI;
 523                         } else if (intr_types & DDI_INTR_TYPE_FIXED) {
 524                                 msi_enable = 0;
 525                                 if (mrsas_add_intrs(instance,
 526                                     DDI_INTR_TYPE_FIXED) != DDI_SUCCESS) {
 527                                         con_log(CL_ANN, (CE_WARN,
 528                                             "FIXED interrupt query failed"));
 529                                         goto fail_attach;
 530                                 }
 531                                 instance->intr_type = DDI_INTR_TYPE_FIXED;
 532                         } else {
 533                                 con_log(CL_ANN, (CE_WARN, "Device cannot "
 534                                     "suppport either FIXED or MSI/X "
 535                                     "interrupts"));
 536                                 goto fail_attach;
 537                         }
 538 
 539                         added_isr_f = 1;
 540 
 541                         if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
 542                             "mrsas-enable-ctio", &data) == DDI_SUCCESS) {
 543                                 if (strncmp(data, "no", 3) == 0) {
 544                                         ctio_enable = 0;
 545                                         con_log(CL_ANN1, (CE_WARN,
 546                                             "ctio_enable = %d disabled",
 547                                             ctio_enable));
 548                                 }
 549                                 ddi_prop_free(data);
 550                         }
 551 
 552                         con_log(CL_DLEVEL1, (CE_WARN, "ctio_enable = %d",
 553                             ctio_enable));
 554 
 555                         /* setup the mfi based low level driver */
 556                         if (init_mfi(instance) != DDI_SUCCESS) {
 557                                 con_log(CL_ANN, (CE_WARN, "mr_sas: "
 558                                 "could not initialize the low level driver"));
 559 
 560                                 goto fail_attach;
 561                         }
 562 
 563                         /* Initialize all Mutex */
 564                         INIT_LIST_HEAD(&instance->completed_pool_list);
 565                         mutex_init(&instance->completed_pool_mtx,
 566                             "completed_pool_mtx", MUTEX_DRIVER,
 567                             DDI_INTR_PRI(instance->intr_pri));
 568 
 569                         mutex_init(&instance->app_cmd_pool_mtx,
 570                             "app_cmd_pool_mtx", MUTEX_DRIVER,
 571                             DDI_INTR_PRI(instance->intr_pri));
 572 
 573                         mutex_init(&instance->cmd_pend_mtx, "cmd_pend_mtx",
 574                             MUTEX_DRIVER, DDI_INTR_PRI(instance->intr_pri));
 575 
 576                         mutex_init(&instance->ocr_flags_mtx, "ocr_flags_mtx",
 577                             MUTEX_DRIVER, DDI_INTR_PRI(instance->intr_pri));
 578 
 579                         mutex_init(&instance->int_cmd_mtx, "int_cmd_mtx",
 580                             MUTEX_DRIVER, DDI_INTR_PRI(instance->intr_pri));






 581                         cv_init(&instance->int_cmd_cv, NULL, CV_DRIVER, NULL);
 582 
 583                         mutex_init(&instance->cmd_pool_mtx, "cmd_pool_mtx",
 584                             MUTEX_DRIVER, DDI_INTR_PRI(instance->intr_pri));
 585 














 586                         instance->timeout_id = (timeout_id_t)-1;
 587 
 588                         /* Register our soft-isr for highlevel interrupts. */
 589                         instance->isr_level = instance->intr_pri;

 590                         if (instance->isr_level == HIGH_LEVEL_INTR) {
 591                                 if (ddi_add_softintr(dip, DDI_SOFTINT_HIGH,

 592                                     &instance->soft_intr_id, NULL, NULL,
 593                                     mrsas_softintr, (caddr_t)instance) !=
 594                                     DDI_SUCCESS) {
 595                                         con_log(CL_ANN, (CE_WARN,
 596                                             " Software ISR did not register"));
 597 
 598                                         goto fail_attach;
 599                                 }
 600 
 601                                 added_soft_isr_f = 1;

 602                         }

 603 


 604                         /* Allocate a transport structure */
 605                         tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
 606 
 607                         if (tran == NULL) {
 608                                 con_log(CL_ANN, (CE_WARN,
 609                                     "scsi_hba_tran_alloc failed"));
 610                                 goto fail_attach;
 611                         }
 612 
 613                         tran_alloc_f = 1;
 614 
 615                         instance->tran = tran;

 616 
 617                         tran->tran_hba_private       = instance;
 618                         tran->tran_tgt_init  = mrsas_tran_tgt_init;
 619                         tran->tran_tgt_probe = scsi_hba_probe;
 620                         tran->tran_tgt_free  = mrsas_tran_tgt_free;






 621                         tran->tran_init_pkt  = mrsas_tran_init_pkt;
 622                         tran->tran_start     = mrsas_tran_start;

 623                         tran->tran_abort     = mrsas_tran_abort;
 624                         tran->tran_reset     = mrsas_tran_reset;
 625                         tran->tran_getcap    = mrsas_tran_getcap;
 626                         tran->tran_setcap    = mrsas_tran_setcap;
 627                         tran->tran_destroy_pkt       = mrsas_tran_destroy_pkt;
 628                         tran->tran_dmafree   = mrsas_tran_dmafree;
 629                         tran->tran_sync_pkt  = mrsas_tran_sync_pkt;


 630                         tran->tran_bus_config        = mrsas_tran_bus_config;
 631 
 632                         if (mrsas_relaxed_ordering)
 633                                 mrsas_generic_dma_attr.dma_attr_flags |=
 634                                     DDI_DMA_RELAXED_ORDERING;
 635 
 636 
 637                         tran_dma_attr = mrsas_generic_dma_attr;
 638                         tran_dma_attr.dma_attr_sgllen = instance->max_num_sge;
 639 
 640                         /* Attach this instance of the hba */
 641                         if (scsi_hba_attach_setup(dip, &tran_dma_attr, tran, 0)
 642                             != DDI_SUCCESS) {
 643                                 con_log(CL_ANN, (CE_WARN,
 644                                     "scsi_hba_attach failed"));
 645 
 646                                 goto fail_attach;
 647                         }



 648 
 649                         /* create devctl node for cfgadm command */
 650                         if (ddi_create_minor_node(dip, "devctl",
 651                             S_IFCHR, INST2DEVCTL(instance_no),
 652                             DDI_NT_SCSI_NEXUS, 0) == DDI_FAILURE) {
 653                                 con_log(CL_ANN, (CE_WARN,
 654                                     "mr_sas: failed to create devctl node."));
 655 
 656                                 goto fail_attach;
 657                         }
 658 
 659                         create_devctl_node_f = 1;
 660 
 661                         /* create scsi node for cfgadm command */
 662                         if (ddi_create_minor_node(dip, "scsi", S_IFCHR,
 663                             INST2SCSI(instance_no),
 664                             DDI_NT_SCSI_ATTACHMENT_POINT, 0) ==
 665                             DDI_FAILURE) {
 666                                 con_log(CL_ANN, (CE_WARN,
 667                                     "mr_sas: failed to create scsi node."));
 668 
 669                                 goto fail_attach;
 670                         }
 671 
 672                         create_scsi_node_f = 1;
 673 
 674                         (void) sprintf(instance->iocnode, "%d:lsirdctl",
 675                             instance_no);
 676 
 677                         /*
 678                          * Create a node for applications
 679                          * for issuing ioctl to the driver.
 680                          */
 681                         if (ddi_create_minor_node(dip, instance->iocnode,
 682                             S_IFCHR, INST2LSIRDCTL(instance_no),
 683                             DDI_PSEUDO, 0) == DDI_FAILURE) {
 684                                 con_log(CL_ANN, (CE_WARN,
 685                                     "mr_sas: failed to create ioctl node."));
 686 
 687                                 goto fail_attach;
 688                         }
 689 
 690                         create_ioc_node_f = 1;
 691 
 692                         /* Create a taskq to handle dr events */
 693                         if ((instance->taskq = ddi_taskq_create(dip,
 694                             "mrsas_dr_taskq", 1,
 695                             TASKQ_DEFAULTPRI, 0)) == NULL) {
 696                                 con_log(CL_ANN, (CE_WARN,
 697                                     "mr_sas: failed to create taskq "));
 698                                 instance->taskq = NULL;
 699                                 goto fail_attach;
 700                         }


 701 
 702                         /* enable interrupt */
 703                         instance->func_ptr->enable_intr(instance);
 704 
 705                         /* initiate AEN */
 706                         if (start_mfi_aen(instance)) {
 707                                 con_log(CL_ANN, (CE_WARN,
 708                                     "mr_sas: failed to initiate AEN."));
 709                                 goto fail_initiate_aen;
 710                         }



 711 
 712                         con_log(CL_DLEVEL1, (CE_NOTE,
 713                             "AEN started for instance %d.", instance_no));
 714 
 715                         /* Finally! We are on the air.  */
 716                         ddi_report_dev(dip);
 717 

 718                         if (mrsas_check_acc_handle(instance->regmap_handle) !=
 719                             DDI_SUCCESS) {
 720                                 goto fail_attach;
 721                         }
 722                         if (mrsas_check_acc_handle(instance->pci_handle) !=
 723                             DDI_SUCCESS) {
 724                                 goto fail_attach;
 725                         }

 726                         instance->mr_ld_list =
 727                             kmem_zalloc(MRDRV_MAX_LD * sizeof (struct mrsas_ld),
 728                             KM_SLEEP);



















 729                         break;
 730                 case DDI_PM_RESUME:
 731                         con_log(CL_ANN, (CE_NOTE,
 732                             "mr_sas: DDI_PM_RESUME"));
 733                         break;
 734                 case DDI_RESUME:
 735                         con_log(CL_ANN, (CE_NOTE,
 736                             "mr_sas: DDI_RESUME"));
 737                         break;
 738                 default:
 739                         con_log(CL_ANN, (CE_WARN,
 740                             "mr_sas: invalid attach cmd=%x", cmd));
 741                         return (DDI_FAILURE);
 742         }
 743 




 744         return (DDI_SUCCESS);
 745 
 746 fail_initiate_aen:
 747 fail_attach:
 748         if (create_devctl_node_f) {
 749                 ddi_remove_minor_node(dip, "devctl");
 750         }
 751 
 752         if (create_scsi_node_f) {
 753                 ddi_remove_minor_node(dip, "scsi");
 754         }
 755 
 756         if (create_ioc_node_f) {
 757                 ddi_remove_minor_node(dip, instance->iocnode);
 758         }
 759 
 760         if (tran_alloc_f) {
 761                 scsi_hba_tran_free(tran);
 762         }
 763 
 764 
 765         if (added_soft_isr_f) {
 766                 ddi_remove_softintr(instance->soft_intr_id);
 767         }
 768 
 769         if (added_isr_f) {
 770                 mrsas_rem_intrs(instance);
 771         }
 772 
 773         if (instance && instance->taskq) {
 774                 ddi_taskq_destroy(instance->taskq);
 775         }
 776 
 777         mrsas_fm_ereport(instance, DDI_FM_DEVICE_NO_RESPONSE);
 778         ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST);
 779 
 780         mrsas_fm_fini(instance);
 781 
 782         pci_config_teardown(&instance->pci_handle);
 783 
 784         ddi_soft_state_free(mrsas_state, instance_no);
 785 
 786         con_log(CL_ANN, (CE_NOTE,
 787             "mr_sas: return failure from mrsas_attach"));
 788 



 789         return (DDI_FAILURE);
 790 }
 791 














 792 /*ARGSUSED*/
 793 static int
 794 mrsas_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,  void *arg, void **resultp)
 795 {
 796         int     rval;
 797         int     mrsas_minor = getminor((dev_t)arg);
 798 
 799         struct mrsas_instance   *instance;
 800 
 801         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
 802 
 803         switch (cmd) {
 804                 case DDI_INFO_DEVT2DEVINFO:
 805                         instance = (struct mrsas_instance *)
 806                             ddi_get_soft_state(mrsas_state,
 807                             MINOR2INST(mrsas_minor));
 808 
 809                         if (instance == NULL) {
 810                                 *resultp = NULL;
 811                                 rval = DDI_FAILURE;
 812                         } else {
 813                                 *resultp = instance->dip;
 814                                 rval = DDI_SUCCESS;
 815                         }
 816                         break;
 817                 case DDI_INFO_DEVT2INSTANCE:
 818                         *resultp = (void *)(intptr_t)
 819                             (MINOR2INST(getminor((dev_t)arg)));
 820                         rval = DDI_SUCCESS;
 821                         break;
 822                 default:
 823                         *resultp = NULL;
 824                         rval = DDI_FAILURE;
 825         }
 826 
 827         return (rval);
 828 }
 829 













 830 static int
 831 mrsas_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
 832 {
 833         int     instance_no;
 834 
 835         struct mrsas_instance   *instance;
 836 
 837         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
 838 

 839         /* CONSTCOND */
 840         ASSERT(NO_COMPETING_THREADS);
 841 
 842         instance_no = ddi_get_instance(dip);
 843 
 844         instance = (struct mrsas_instance *)ddi_get_soft_state(mrsas_state,
 845             instance_no);
 846 
 847         if (!instance) {
 848                 con_log(CL_ANN, (CE_WARN,
 849                     "mr_sas:%d could not get instance in detach",
 850                     instance_no));
 851 
 852                 return (DDI_FAILURE);
 853         }
 854 
 855         con_log(CL_ANN, (CE_NOTE,
 856             "mr_sas%d: detaching device 0x%4x:0x%4x:0x%4x:0x%4x",
 857             instance_no, instance->vendor_id, instance->device_id,
 858             instance->subsysvid, instance->subsysid));
 859 
 860         switch (cmd) {
 861         case DDI_DETACH:
 862                 con_log(CL_ANN, (CE_NOTE,
 863                     "mrsas_detach: DDI_DETACH"));
 864 
 865                 if (scsi_hba_detach(dip) != DDI_SUCCESS) {
 866                         con_log(CL_ANN, (CE_WARN,
 867                             "mr_sas:%d failed to detach",
 868                             instance_no));
 869 
 870                         return (DDI_FAILURE);

 871                 }

 872 
 873                 scsi_hba_tran_free(instance->tran);
 874 
 875                 flush_cache(instance);
 876 
 877                 if (abort_aen_cmd(instance, instance->aen_cmd)) {
 878                         con_log(CL_ANN, (CE_WARN, "mrsas_detach: "
 879                             "failed to abort prevous AEN command"));
 880 
 881                         return (DDI_FAILURE);
 882                 }
 883 
 884                 instance->func_ptr->disable_intr(instance);
 885 
 886                 if (instance->isr_level == HIGH_LEVEL_INTR) {
 887                         ddi_remove_softintr(instance->soft_intr_id);
 888                 }
 889 
 890                 mrsas_rem_intrs(instance);
 891 
 892                 if (instance->taskq) {
 893                         ddi_taskq_destroy(instance->taskq);
 894                 }
 895                 kmem_free(instance->mr_ld_list, MRDRV_MAX_LD
 896                     * sizeof (struct mrsas_ld));
 897                 free_space_for_mfi(instance);
 898 
 899                 mrsas_fm_fini(instance);
 900 
 901                 pci_config_teardown(&instance->pci_handle);
 902 
 903                 kmem_free(instance->func_ptr,
 904                     sizeof (struct mrsas_func_ptr));
 905 
 906                 if (instance->timeout_id != (timeout_id_t)-1) {
 907                         (void) untimeout(instance->timeout_id);
 908                         instance->timeout_id = (timeout_id_t)-1;
 909                 }
 910                 ddi_soft_state_free(mrsas_state, instance_no);
 911                 break;

 912         case DDI_PM_SUSPEND:
 913                 con_log(CL_ANN, (CE_NOTE,
 914                     "mrsas_detach: DDI_PM_SUSPEND"));
 915 
 916                 break;
 917         case DDI_SUSPEND:
 918                 con_log(CL_ANN, (CE_NOTE,
 919                     "mrsas_detach: DDI_SUSPEND"));
 920 
 921                 break;
 922         default:
 923                 con_log(CL_ANN, (CE_WARN,
 924                     "invalid detach command:0x%x", cmd));
 925                 return (DDI_FAILURE);
 926         }
 927 
 928         return (DDI_SUCCESS);
 929 }
 930 













































































































































































 931 /*
 932  * ************************************************************************** *
 933  *                                                                            *
 934  *             common entry points - for character driver types               *
 935  *                                                                            *
 936  * ************************************************************************** *
 937  */











 938 static  int
 939 mrsas_open(dev_t *dev, int openflags, int otyp, cred_t *credp)
 940 {
 941         int     rval = 0;
 942 
 943         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
 944 
 945         /* Check root permissions */
 946         if (drv_priv(credp) != 0) {
 947                 con_log(CL_ANN, (CE_WARN,
 948                     "mr_sas: Non-root ioctl access denied!"));
 949                 return (EPERM);
 950         }
 951 
 952         /* Verify we are being opened as a character device */
 953         if (otyp != OTYP_CHR) {
 954                 con_log(CL_ANN, (CE_WARN,
 955                     "mr_sas: ioctl node must be a char node"));
 956                 return (EINVAL);
 957         }
 958 
 959         if (ddi_get_soft_state(mrsas_state, MINOR2INST(getminor(*dev)))
 960             == NULL) {
 961                 return (ENXIO);
 962         }
 963 
 964         if (scsi_hba_open) {
 965                 rval = scsi_hba_open(dev, openflags, otyp, credp);
 966         }
 967 
 968         return (rval);
 969 }
 970 










 971 static  int
 972 mrsas_close(dev_t dev, int openflags, int otyp, cred_t *credp)
 973 {
 974         int     rval = 0;
 975 
 976         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
 977 
 978         /* no need for locks! */
 979 
 980         if (scsi_hba_close) {
 981                 rval = scsi_hba_close(dev, openflags, otyp, credp);
 982         }
 983 
 984         return (rval);
 985 }
 986 

















 987 static int
 988 mrsas_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
 989     int *rvalp)
 990 {
 991         int     rval = 0;
 992 
 993         struct mrsas_instance   *instance;
 994         struct mrsas_ioctl      *ioctl;
 995         struct mrsas_aen        aen;
 996         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
 997 
 998         instance = ddi_get_soft_state(mrsas_state, MINOR2INST(getminor(dev)));
 999 
1000         if (instance == NULL) {
1001                 /* invalid minor number */
1002                 con_log(CL_ANN, (CE_WARN, "mr_sas: adapter not found."));
1003                 return (ENXIO);
1004         }
1005 
1006         ioctl = (struct mrsas_ioctl *)kmem_zalloc(sizeof (struct mrsas_ioctl),


1053                 default:
1054                         rval = scsi_hba_ioctl(dev, cmd, arg,
1055                             mode, credp, rvalp);
1056 
1057                         con_log(CL_DLEVEL1, (CE_NOTE, "mrsas_ioctl: "
1058                             "scsi_hba_ioctl called, ret = %x.", rval));
1059         }
1060 
1061         kmem_free(ioctl, sizeof (struct mrsas_ioctl));
1062         return (rval);
1063 }
1064 
1065 /*
1066  * ************************************************************************** *
1067  *                                                                            *
1068  *               common entry points - for block driver types                 *
1069  *                                                                            *
1070  * ************************************************************************** *
1071  */
1072 #ifdef  __sparc







1073 /*ARGSUSED*/
1074 static int
1075 mrsas_reset(dev_info_t *dip, ddi_reset_cmd_t cmd)
1076 {
1077         int     instance_no;
1078 
1079         struct mrsas_instance   *instance;
1080 
1081         instance_no = ddi_get_instance(dip);
1082         instance = (struct mrsas_instance *)ddi_get_soft_state
1083             (mrsas_state, instance_no);
1084 
1085         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
1086 
1087         if (!instance) {
1088                 con_log(CL_ANN, (CE_WARN, "mr_sas:%d could not get adapter "
1089                     "in reset", instance_no));
1090                 return (DDI_FAILURE);
1091         }
1092 
1093         instance->func_ptr->disable_intr(instance);
1094 
1095         con_log(CL_ANN1, (CE_NOTE, "flushing cache for instance %d",
1096             instance_no));
1097 
1098         flush_cache(instance);
1099 
1100         return (DDI_SUCCESS);
1101 }
1102 #else /* __sparc */
1103 /*ARGSUSED*/
1104 static int
1105 mrsas_quiesce(dev_info_t *dip)
1106 {
1107         int     instance_no;
1108 
1109         struct mrsas_instance   *instance;
1110 
1111         instance_no = ddi_get_instance(dip);
1112         instance = (struct mrsas_instance *)ddi_get_soft_state
1113             (mrsas_state, instance_no);
1114 
1115         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
1116 
1117         if (!instance) {
1118                 con_log(CL_ANN1, (CE_WARN, "mr_sas:%d could not get adapter "
1119                     "in quiesce", instance_no));
1120                 return (DDI_FAILURE);
1121         }
1122         if (instance->deadadapter || instance->adapterresetinprogress) {
1123                 con_log(CL_ANN1, (CE_WARN, "mr_sas:%d adapter is not in "
1124                     "healthy state", instance_no));
1125                 return (DDI_FAILURE);
1126         }
1127 
1128         if (abort_aen_cmd(instance, instance->aen_cmd)) {
1129                 con_log(CL_ANN1, (CE_WARN, "mrsas_quiesce: "
1130                     "failed to abort prevous AEN command QUIESCE"));
1131         }
1132 










1133         instance->func_ptr->disable_intr(instance);
1134 
1135         con_log(CL_ANN1, (CE_NOTE, "flushing cache for instance %d",
1136             instance_no));
1137 
1138         flush_cache(instance);
1139 
1140         if (wait_for_outstanding(instance)) {


1141                 return (DDI_FAILURE);
1142         }
1143         return (DDI_SUCCESS);
1144 }
1145 #endif  /* __sparc */
1146 
1147 /*
1148  * ************************************************************************** *
1149  *                                                                            *
1150  *                          entry points (SCSI HBA)                           *
1151  *                                                                            *
1152  * ************************************************************************** *
1153  */













1154 /*ARGSUSED*/
1155 static int
1156 mrsas_tran_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1157                 scsi_hba_tran_t *tran, struct scsi_device *sd)
1158 {
1159         struct mrsas_instance *instance;
1160         uint16_t tgt = sd->sd_address.a_target;
1161         uint8_t lun = sd->sd_address.a_lun;

1162 
1163         con_log(CL_ANN1, (CE_NOTE, "mrsas_tgt_init target %d lun %d",
1164             tgt, lun));
1165 
1166         instance = ADDR2MR(&sd->sd_address);
1167 
1168         if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
1169                 (void) ndi_merge_node(tgt_dip, mrsas_name_node);
1170                 ddi_set_name_addr(tgt_dip, NULL);
1171 
1172                 con_log(CL_ANN1, (CE_NOTE, "mrsas_tgt_init in "
1173                     "ndi_dev_is_persistent_node DDI_FAILURE t = %d l = %d",
1174                     tgt, lun));









1175                 return (DDI_FAILURE);

1176         }
1177 
1178         con_log(CL_ANN1, (CE_NOTE, "mrsas_tgt_init dev_dip %p tgt_dip %p",
1179             (void *)instance->mr_ld_list[tgt].dip, (void *)tgt_dip));
1180 
1181         if (tgt < MRDRV_MAX_LD && lun == 0) {
1182                 if (instance->mr_ld_list[tgt].dip == NULL &&
1183                     strcmp(ddi_driver_name(sd->sd_dev), "sd") == 0) {

1184                         instance->mr_ld_list[tgt].dip = tgt_dip;
1185                         instance->mr_ld_list[tgt].lun_type = MRSAS_LD_LUN;


1186                 }
1187         }















1188         return (DDI_SUCCESS);
1189 }
1190 
1191 /*ARGSUSED*/
1192 static void
1193 mrsas_tran_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1194     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1195 {
1196         struct mrsas_instance *instance;
1197         int tgt = sd->sd_address.a_target;
1198         int lun = sd->sd_address.a_lun;
1199 
1200         instance = ADDR2MR(&sd->sd_address);
1201 
1202         con_log(CL_ANN1, (CE_NOTE, "tgt_free t = %d l = %d", tgt, lun));
1203 
1204         if (tgt < MRDRV_MAX_LD && lun == 0) {
1205                 if (instance->mr_ld_list[tgt].dip == tgt_dip) {

1206                         instance->mr_ld_list[tgt].dip = NULL;

1207                 }
1208         }











1209 }
1210 
1211 static dev_info_t *
1212 mrsas_find_child(struct mrsas_instance *instance, uint16_t tgt, uint8_t lun)
1213 {
1214         dev_info_t *child = NULL;
1215         char addr[SCSI_MAXNAMELEN];
1216         char tmp[MAXNAMELEN];
1217 
1218         (void) sprintf(addr, "%x,%x", tgt, lun);
1219         for (child = ddi_get_child(instance->dip); child;
1220             child = ddi_get_next_sibling(child)) {
1221 




1222                 if (mrsas_name_node(child, tmp, MAXNAMELEN) !=
1223                     DDI_SUCCESS) {
1224                         continue;
1225                 }
1226 
1227                 if (strcmp(addr, tmp) == 0) {
1228                         break;
1229                 }
1230         }
1231         con_log(CL_ANN1, (CE_NOTE, "mrsas_find_child: return child = %p",
1232             (void *)child));
1233         return (child);
1234 }
1235 






1236 static int
1237 mrsas_name_node(dev_info_t *dip, char *name, int len)
1238 {
1239         int tgt, lun;
1240 
1241         tgt = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1242             DDI_PROP_DONTPASS, "target", -1);
1243         con_log(CL_ANN1, (CE_NOTE,
1244             "mrsas_name_node: dip %p tgt %d", (void *)dip, tgt));
1245         if (tgt == -1) {
1246                 return (DDI_FAILURE);
1247         }
1248         lun = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1249             "lun", -1);
1250         con_log(CL_ANN1,
1251             (CE_NOTE, "mrsas_name_node: tgt %d lun %d", tgt, lun));
1252         if (lun == -1) {
1253                 return (DDI_FAILURE);
1254         }
1255         (void) snprintf(name, len, "%x,%x", tgt, lun);
1256         return (DDI_SUCCESS);
1257 }
1258 




















1259 static struct scsi_pkt *
1260 mrsas_tran_init_pkt(struct scsi_address *ap, register struct scsi_pkt *pkt,
1261         struct buf *bp, int cmdlen, int statuslen, int tgtlen,
1262         int flags, int (*callback)(), caddr_t arg)
1263 {
1264         struct scsa_cmd *acmd;
1265         struct mrsas_instance   *instance;
1266         struct scsi_pkt *new_pkt;
1267 
1268         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
1269 
1270         instance = ADDR2MR(ap);
1271 
1272         /* step #1 : pkt allocation */
1273         if (pkt == NULL) {
1274                 pkt = scsi_hba_pkt_alloc(instance->dip, ap, cmdlen, statuslen,
1275                     tgtlen, sizeof (struct scsa_cmd), callback, arg);
1276                 if (pkt == NULL) {
1277                         return (NULL);
1278                 }
1279 
1280                 acmd = PKT2CMD(pkt);
1281 
1282                 /*
1283                  * Initialize the new pkt - we redundantly initialize
1284                  * all the fields for illustrative purposes.
1285                  */
1286                 acmd->cmd_pkt                = pkt;
1287                 acmd->cmd_flags              = 0;
1288                 acmd->cmd_scblen     = statuslen;


1310         /* step #2 : dma allocation/move */
1311         if (bp && bp->b_bcount != 0) {
1312                 if (acmd->cmd_dmahandle == NULL) {
1313                         if (mrsas_dma_alloc(instance, pkt, bp, flags,
1314                             callback) == DDI_FAILURE) {
1315                                 if (new_pkt) {
1316                                         scsi_hba_pkt_free(ap, new_pkt);
1317                                 }
1318                                 return ((struct scsi_pkt *)NULL);
1319                         }
1320                 } else {
1321                         if (mrsas_dma_move(instance, pkt, bp) == DDI_FAILURE) {
1322                                 return ((struct scsi_pkt *)NULL);
1323                         }
1324                 }
1325         }
1326 
1327         return (pkt);
1328 }
1329 
















1330 static int
1331 mrsas_tran_start(struct scsi_address *ap, register struct scsi_pkt *pkt)
1332 {
1333         uchar_t         cmd_done = 0;
1334 
1335         struct mrsas_instance   *instance = ADDR2MR(ap);
1336         struct mrsas_cmd        *cmd;
1337 

1338         if (instance->deadadapter == 1) {
1339                 con_log(CL_ANN1, (CE_WARN,
1340                     "mrsas_tran_start: return TRAN_FATAL_ERROR "
1341                     "for IO, as the HBA doesnt take any more IOs"));
1342                 if (pkt) {
1343                         pkt->pkt_reason              = CMD_DEV_GONE;
1344                         pkt->pkt_statistics  = STAT_DISCON;
1345                 }
1346                 return (TRAN_FATAL_ERROR);
1347         }
1348 
1349         if (instance->adapterresetinprogress) {
1350                 con_log(CL_ANN1, (CE_NOTE, "Reset flag set, "
1351                     "returning mfi_pkt and setting TRAN_BUSY\n"));
1352                 return (TRAN_BUSY);
1353         }
1354 
1355         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d:SCSI CDB[0]=0x%x time:%x",
1356             __func__, __LINE__, pkt->pkt_cdbp[0], pkt->pkt_time));
1357 
1358         pkt->pkt_reason      = CMD_CMPLT;
1359         *pkt->pkt_scbp = STATUS_GOOD; /* clear arq scsi_status */
1360 
1361         cmd = build_cmd(instance, ap, pkt, &cmd_done);
1362 
1363         /*
1364          * Check if the command is already completed by the mrsas_build_cmd()
1365          * routine. In which case the busy_flag would be clear and scb will be
1366          * NULL and appropriate reason provided in pkt_reason field
1367          */
1368         if (cmd_done) {
1369                 pkt->pkt_reason = CMD_CMPLT;
1370                 pkt->pkt_scbp[0] = STATUS_GOOD;
1371                 pkt->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET
1372                     | STATE_SENT_CMD;
1373                 if (((pkt->pkt_flags & FLAG_NOINTR) == 0) && pkt->pkt_comp) {
1374                         (*pkt->pkt_comp)(pkt);
1375                 }


1377                 return (TRAN_ACCEPT);
1378         }
1379 
1380         if (cmd == NULL) {
1381                 return (TRAN_BUSY);
1382         }
1383 
1384         if ((pkt->pkt_flags & FLAG_NOINTR) == 0) {
1385                 if (instance->fw_outstanding > instance->max_fw_cmds) {
1386                         con_log(CL_ANN, (CE_CONT, "mr_sas:Firmware busy"));
1387                         DTRACE_PROBE2(start_tran_err,
1388                             uint16_t, instance->fw_outstanding,
1389                             uint16_t, instance->max_fw_cmds);
1390                         return_mfi_pkt(instance, cmd);
1391                         return (TRAN_BUSY);
1392                 }
1393 
1394                 /* Synchronize the Cmd frame for the controller */
1395                 (void) ddi_dma_sync(cmd->frame_dma_obj.dma_handle, 0, 0,
1396                     DDI_DMA_SYNC_FORDEV);
1397                 con_log(CL_ANN1, (CE_NOTE, "Push SCSI CDB[0]=0x%x"
1398                     "cmd->index:%x\n", pkt->pkt_cdbp[0], cmd->index));
1399                 instance->func_ptr->issue_cmd(cmd, instance);
1400 
1401         } else {
1402                 struct mrsas_header *hdr = &cmd->frame->hdr;
1403 
1404                 cmd->sync_cmd = MRSAS_TRUE;
1405 
1406                 instance->func_ptr-> issue_cmd_in_poll_mode(instance, cmd);
1407 
1408                 pkt->pkt_reason              = CMD_CMPLT;
1409                 pkt->pkt_statistics  = 0;
1410                 pkt->pkt_state |= STATE_XFERRED_DATA | STATE_GOT_STATUS;
1411 
1412                 switch (ddi_get8(cmd->frame_dma_obj.acc_handle,
1413                     &hdr->cmd_status)) {
1414                 case MFI_STAT_OK:
1415                         pkt->pkt_scbp[0] = STATUS_GOOD;
1416                         break;
1417 
1418                 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1419 

1420                         pkt->pkt_reason      = CMD_CMPLT;
1421                         pkt->pkt_statistics = 0;
1422 
1423                         ((struct scsi_status *)pkt->pkt_scbp)->sts_chk = 1;
1424                         break;
1425 
1426                 case MFI_STAT_DEVICE_NOT_FOUND:


1427                         pkt->pkt_reason              = CMD_DEV_GONE;
1428                         pkt->pkt_statistics  = STAT_DISCON;
1429                         break;
1430 
1431                 default:
1432                         ((struct scsi_status *)pkt->pkt_scbp)->sts_busy = 1;
1433                 }
1434 
1435                 (void) mrsas_common_check(instance, cmd);
1436                 DTRACE_PROBE2(start_nointr_done, uint8_t, hdr->cmd,
1437                     uint8_t, hdr->cmd_status);
1438                 return_mfi_pkt(instance, cmd);
1439 
1440                 if (pkt->pkt_comp) {
1441                         (*pkt->pkt_comp)(pkt);
1442                 }
1443 
1444         }
1445 
1446         return (TRAN_ACCEPT);
1447 }
1448 













1449 /*ARGSUSED*/
1450 static int
1451 mrsas_tran_abort(struct scsi_address *ap, struct scsi_pkt *pkt)
1452 {
1453         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
1454 
1455         /* abort command not supported by H/W */
1456 
1457         return (DDI_FAILURE);
1458 }
1459 











1460 /*ARGSUSED*/
1461 static int
1462 mrsas_tran_reset(struct scsi_address *ap, int level)
1463 {


1464         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
1465 
1466         /* reset command not supported by H/W */
1467 

1468         return (DDI_FAILURE);




1469 































1470 }

1471 












1472 /*ARGSUSED*/
1473 static int
1474 mrsas_tran_getcap(struct scsi_address *ap, char *cap, int whom)
1475 {
1476         int     rval = 0;
1477 
1478         struct mrsas_instance   *instance = ADDR2MR(ap);
1479 
1480         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
1481 
1482         /* we do allow inquiring about capabilities for other targets */
1483         if (cap == NULL) {
1484                 return (-1);
1485         }
1486 
1487         switch (scsi_hba_lookup_capstr(cap)) {
1488         case SCSI_CAP_DMA_MAX:




1489                 /* Limit to 16MB max transfer */
1490                 rval = mrsas_max_cap_maxxfer;

1491                 break;
1492         case SCSI_CAP_MSG_OUT:
1493                 rval = 1;
1494                 break;
1495         case SCSI_CAP_DISCONNECT:
1496                 rval = 0;
1497                 break;
1498         case SCSI_CAP_SYNCHRONOUS:
1499                 rval = 0;
1500                 break;
1501         case SCSI_CAP_WIDE_XFER:
1502                 rval = 1;
1503                 break;
1504         case SCSI_CAP_TAGGED_QING:
1505                 rval = 1;
1506                 break;
1507         case SCSI_CAP_UNTAGGED_QING:
1508                 rval = 1;
1509                 break;
1510         case SCSI_CAP_PARITY:


1519         case SCSI_CAP_LINKED_CMDS:
1520                 rval = 0;
1521                 break;
1522         case SCSI_CAP_RESET_NOTIFICATION:
1523                 rval = 1;
1524                 break;
1525         case SCSI_CAP_GEOMETRY:
1526                 rval = -1;
1527 
1528                 break;
1529         default:
1530                 con_log(CL_DLEVEL2, (CE_NOTE, "Default cap coming 0x%x",
1531                     scsi_hba_lookup_capstr(cap)));
1532                 rval = -1;
1533                 break;
1534         }
1535 
1536         return (rval);
1537 }
1538 
















1539 /*ARGSUSED*/
1540 static int
1541 mrsas_tran_setcap(struct scsi_address *ap, char *cap, int value, int whom)
1542 {
1543         int             rval = 1;
1544 
1545         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
1546 
1547         /* We don't allow setting capabilities for other targets */
1548         if (cap == NULL || whom == 0) {
1549                 return (-1);
1550         }
1551 
1552         switch (scsi_hba_lookup_capstr(cap)) {
1553                 case SCSI_CAP_DMA_MAX:
1554                 case SCSI_CAP_MSG_OUT:
1555                 case SCSI_CAP_PARITY:
1556                 case SCSI_CAP_LINKED_CMDS:
1557                 case SCSI_CAP_RESET_NOTIFICATION:
1558                 case SCSI_CAP_DISCONNECT:
1559                 case SCSI_CAP_SYNCHRONOUS:
1560                 case SCSI_CAP_UNTAGGED_QING:
1561                 case SCSI_CAP_WIDE_XFER:
1562                 case SCSI_CAP_INITIATOR_ID:
1563                 case SCSI_CAP_ARQ:
1564                         /*
1565                          * None of these are settable via


1567                          */
1568                         break;
1569                 case SCSI_CAP_TAGGED_QING:
1570                         rval = 1;
1571                         break;
1572                 case SCSI_CAP_SECTOR_SIZE:
1573                         rval = 1;
1574                         break;
1575 
1576                 case SCSI_CAP_TOTAL_SECTORS:
1577                         rval = 1;
1578                         break;
1579                 default:
1580                         rval = -1;
1581                         break;
1582         }
1583 
1584         return (rval);
1585 }
1586 













1587 static void
1588 mrsas_tran_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
1589 {
1590         struct scsa_cmd *acmd = PKT2CMD(pkt);
1591 
1592         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
1593 
1594         if (acmd->cmd_flags & CFLAG_DMAVALID) {
1595                 acmd->cmd_flags &= ~CFLAG_DMAVALID;
1596 
1597                 (void) ddi_dma_unbind_handle(acmd->cmd_dmahandle);
1598 
1599                 ddi_dma_free_handle(&acmd->cmd_dmahandle);
1600 
1601                 acmd->cmd_dmahandle = NULL;
1602         }
1603 
1604         /* free the pkt */
1605         scsi_hba_pkt_free(ap, pkt);
1606 }
1607 












1608 /*ARGSUSED*/
1609 static void
1610 mrsas_tran_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
1611 {
1612         register struct scsa_cmd *acmd = PKT2CMD(pkt);
1613 
1614         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
1615 
1616         if (acmd->cmd_flags & CFLAG_DMAVALID) {
1617                 acmd->cmd_flags &= ~CFLAG_DMAVALID;
1618 
1619                 (void) ddi_dma_unbind_handle(acmd->cmd_dmahandle);
1620 
1621                 ddi_dma_free_handle(&acmd->cmd_dmahandle);
1622 
1623                 acmd->cmd_dmahandle = NULL;
1624         }
1625 }
1626 













1627 /*ARGSUSED*/
1628 static void
1629 mrsas_tran_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
1630 {
1631         register struct scsa_cmd        *acmd = PKT2CMD(pkt);
1632 
1633         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
1634 
1635         if (acmd->cmd_flags & CFLAG_DMAVALID) {
1636                 (void) ddi_dma_sync(acmd->cmd_dmahandle, acmd->cmd_dma_offset,
1637                     acmd->cmd_dma_len, (acmd->cmd_flags & CFLAG_DMASEND) ?
1638                     DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU);
1639         }
1640 }
1641 



















1642 /*
1643  * mrsas_isr(caddr_t)
1644  *
1645  * The Interrupt Service Routine
1646  *
1647  * Collect status for all completed commands and do callback
1648  *
1649  */
1650 static uint_t
1651 mrsas_isr(struct mrsas_instance *instance)
1652 {
1653         int             need_softintr;
1654         uint32_t        producer;
1655         uint32_t        consumer;
1656         uint32_t        context;

1657 
1658         struct mrsas_cmd        *cmd;
1659         struct mrsas_header     *hdr;
1660         struct scsi_pkt         *pkt;
1661 

1662         ASSERT(instance);


1663         if ((instance->intr_type == DDI_INTR_TYPE_FIXED) &&









1664             !instance->func_ptr->intr_ack(instance)) {
1665                 return (DDI_INTR_UNCLAIMED);
1666         }

1667 
1668         (void) ddi_dma_sync(instance->mfi_internal_dma_obj.dma_handle,
1669             0, 0, DDI_DMA_SYNC_FORCPU);
1670 
1671         if (mrsas_check_dma_handle(instance->mfi_internal_dma_obj.dma_handle)
1672             != DDI_SUCCESS) {
1673                 mrsas_fm_ereport(instance, DDI_FM_DEVICE_NO_RESPONSE);
1674                 ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST);
1675                 con_log(CL_ANN1, (CE_WARN,
1676                     "mr_sas_isr(): FMA check, returning DDI_INTR_UNCLAIMED"));
1677                 return (DDI_INTR_CLAIMED);
1678         }
1679         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
1680 
1681 #ifdef OCRDEBUG
1682         if (debug_consecutive_timeout_after_ocr_g == 1) {
1683                 con_log(CL_ANN1, (CE_NOTE,
1684                 "simulating consecutive timeout after ocr"));
1685                 return (DDI_INTR_CLAIMED);
1686         }
1687 #endif
1688 
1689         mutex_enter(&instance->completed_pool_mtx);
1690         mutex_enter(&instance->cmd_pend_mtx);
1691 
1692         producer = ddi_get32(instance->mfi_internal_dma_obj.acc_handle,
1693             instance->producer);
1694         consumer = ddi_get32(instance->mfi_internal_dma_obj.acc_handle,
1695             instance->consumer);
1696 
1697         con_log(CL_ANN1, (CE_NOTE, " producer %x consumer %x ",
1698             producer, consumer));
1699         if (producer == consumer) {
1700                 con_log(CL_ANN1, (CE_WARN, "producer =  consumer case"));
1701                 DTRACE_PROBE2(isr_pc_err, uint32_t, producer,
1702                     uint32_t, consumer);
1703                 mutex_exit(&instance->cmd_pend_mtx);
1704                 mutex_exit(&instance->completed_pool_mtx);
1705                 return (DDI_INTR_CLAIMED);
1706         }
1707 
1708         while (consumer != producer) {
1709                 context = ddi_get32(instance->mfi_internal_dma_obj.acc_handle,
1710                     &instance->reply_queue[consumer]);
1711                 cmd = instance->cmd_list[context];
1712 
1713                 if (cmd->sync_cmd == MRSAS_TRUE) {
1714                 hdr = (struct mrsas_header *)&cmd->frame->hdr;
1715                 if (hdr) {
1716                         mlist_del_init(&cmd->list);
1717                 }
1718                 } else {
1719                         pkt = cmd->pkt;
1720                         if (pkt) {


1766  *                                                                            *
1767  * ************************************************************************** *
1768  */
1769 /*
1770  * get_mfi_pkt : Get a command from the free pool
1771  * After successful allocation, the caller of this routine
1772  * must clear the frame buffer (memset to zero) before
1773  * using the packet further.
1774  *
1775  * ***** Note *****
1776  * After clearing the frame buffer the context id of the
1777  * frame buffer SHOULD be restored back.
1778  */
1779 static struct mrsas_cmd *
1780 get_mfi_pkt(struct mrsas_instance *instance)
1781 {
1782         mlist_t                 *head = &instance->cmd_pool_list;
1783         struct mrsas_cmd        *cmd = NULL;
1784 
1785         mutex_enter(&instance->cmd_pool_mtx);
1786         ASSERT(mutex_owned(&instance->cmd_pool_mtx));
1787 
1788         if (!mlist_empty(head)) {
1789                 cmd = mlist_entry(head->next, struct mrsas_cmd, list);
1790                 mlist_del_init(head->next);
1791         }
1792         if (cmd != NULL) {
1793                 cmd->pkt = NULL;
1794                 cmd->retry_count_for_ocr = 0;
1795                 cmd->drv_pkt_time = 0;

1796         }
1797         mutex_exit(&instance->cmd_pool_mtx);
1798 
1799         return (cmd);
1800 }
1801 
1802 static struct mrsas_cmd *
1803 get_mfi_app_pkt(struct mrsas_instance *instance)
1804 {
1805         mlist_t                         *head = &instance->app_cmd_pool_list;
1806         struct mrsas_cmd        *cmd = NULL;
1807 
1808         mutex_enter(&instance->app_cmd_pool_mtx);
1809         ASSERT(mutex_owned(&instance->app_cmd_pool_mtx));
1810 
1811         if (!mlist_empty(head)) {
1812                 cmd = mlist_entry(head->next, struct mrsas_cmd, list);
1813                 mlist_del_init(head->next);
1814         }
1815         if (cmd != NULL)
1816                 cmd->pkt = NULL;




1817         mutex_exit(&instance->app_cmd_pool_mtx);
1818 
1819         return (cmd);
1820 }
1821 /*
1822  * return_mfi_pkt : Return a cmd to free command pool
1823  */
1824 static void
1825 return_mfi_pkt(struct mrsas_instance *instance, struct mrsas_cmd *cmd)
1826 {
1827         mutex_enter(&instance->cmd_pool_mtx);
1828         ASSERT(mutex_owned(&instance->cmd_pool_mtx));
1829         /* use mlist_add_tail for debug assistance */
1830         mlist_add_tail(&cmd->list, &instance->cmd_pool_list);
1831 
1832         mutex_exit(&instance->cmd_pool_mtx);
1833 }
1834 
1835 static void
1836 return_mfi_app_pkt(struct mrsas_instance *instance, struct mrsas_cmd *cmd)
1837 {
1838         mutex_enter(&instance->app_cmd_pool_mtx);
1839         ASSERT(mutex_owned(&instance->app_cmd_pool_mtx));
1840 
1841         mlist_add(&cmd->list, &instance->app_cmd_pool_list);
1842 
1843         mutex_exit(&instance->app_cmd_pool_mtx);
1844 }
1845 static void
1846 push_pending_mfi_pkt(struct mrsas_instance *instance, struct mrsas_cmd *cmd)
1847 {
1848         struct scsi_pkt *pkt;
1849         struct mrsas_header     *hdr;
1850         con_log(CL_ANN1, (CE_NOTE, "push_pending_pkt(): Called\n"));
1851         mutex_enter(&instance->cmd_pend_mtx);
1852         ASSERT(mutex_owned(&instance->cmd_pend_mtx));
1853         mlist_del_init(&cmd->list);
1854         mlist_add_tail(&cmd->list, &instance->cmd_pend_list);
1855         if (cmd->sync_cmd == MRSAS_TRUE) {
1856                 hdr = (struct mrsas_header *)&cmd->frame->hdr;
1857                 if (hdr) {
1858                         con_log(CL_ANN1, (CE_CONT,
1859                             "push_pending_mfi_pkt: "
1860                             "cmd %p index %x "
1861                             "time %llx",
1862                             (void *)cmd, cmd->index,
1863                             gethrtime()));
1864                         /* Wait for specified interval  */
1865                         cmd->drv_pkt_time = ddi_get16(
1866                             cmd->frame_dma_obj.acc_handle, &hdr->timeout);
1867                         if (cmd->drv_pkt_time < debug_timeout_g)
1868                                 cmd->drv_pkt_time = (uint16_t)debug_timeout_g;
1869                         con_log(CL_ANN1, (CE_CONT,
1870                             "push_pending_pkt(): "
1871                             "Called IO Timeout Value %x\n",
1872                             cmd->drv_pkt_time));


1876                             (void *) instance, drv_usectohz(MRSAS_1_SECOND));
1877                 }
1878         } else {
1879                 pkt = cmd->pkt;
1880                 if (pkt) {
1881                         con_log(CL_ANN1, (CE_CONT,
1882                             "push_pending_mfi_pkt: "
1883                             "cmd %p index %x pkt %p, "
1884                             "time %llx",
1885                             (void *)cmd, cmd->index, (void *)pkt,
1886                             gethrtime()));
1887                         cmd->drv_pkt_time = (uint16_t)debug_timeout_g;
1888                 }
1889                 if (pkt && instance->timeout_id == (timeout_id_t)-1) {
1890                         instance->timeout_id = timeout(io_timeout_checker,
1891                             (void *) instance, drv_usectohz(MRSAS_1_SECOND));
1892                 }
1893         }
1894 
1895         mutex_exit(&instance->cmd_pend_mtx);

1896 }
1897 
1898 static int
1899 mrsas_print_pending_cmds(struct mrsas_instance *instance)
1900 {
1901         mlist_t *head = &instance->cmd_pend_list;
1902         mlist_t *tmp = head;
1903         struct mrsas_cmd *cmd = NULL;
1904         struct mrsas_header     *hdr;
1905         unsigned int            flag = 1;
1906 
1907         struct scsi_pkt *pkt;
1908         con_log(CL_ANN1, (CE_NOTE,
1909             "mrsas_print_pending_cmds(): Called"));






1910         while (flag) {
1911                 mutex_enter(&instance->cmd_pend_mtx);
1912                 tmp     =       tmp->next;
1913                 if (tmp == head) {
1914                         mutex_exit(&instance->cmd_pend_mtx);
1915                         flag = 0;


1916                         break;
1917                 } else {
1918                         cmd = mlist_entry(tmp, struct mrsas_cmd, list);
1919                         mutex_exit(&instance->cmd_pend_mtx);
1920                         if (cmd) {
1921                                 if (cmd->sync_cmd == MRSAS_TRUE) {
1922                                 hdr = (struct mrsas_header *)&cmd->frame->hdr;

1923                                         if (hdr) {
1924                                         con_log(CL_ANN1, (CE_CONT,
1925                                             "print: cmd %p index %x hdr %p",
1926                                             (void *)cmd, cmd->index,



1927                                             (void *)hdr));
1928                                         }
1929                                 } else {
1930                                         pkt = cmd->pkt;
1931                                         if (pkt) {
1932                                         con_log(CL_ANN1, (CE_CONT,
1933                                             "print: cmd %p index %x "
1934                                             "pkt %p", (void *)cmd, cmd->index,
1935                                             (void *)pkt));

1936                                         }
1937                                 }







1938                         }

1939                 }
1940         }
1941         con_log(CL_ANN1, (CE_NOTE, "mrsas_print_pending_cmds(): Done\n"));





1942         return (DDI_SUCCESS);
1943 }
1944 
1945 
1946 static int
1947 mrsas_complete_pending_cmds(struct mrsas_instance *instance)
1948 {
1949 
1950         struct mrsas_cmd *cmd = NULL;
1951         struct scsi_pkt *pkt;
1952         struct mrsas_header *hdr;
1953 
1954         struct mlist_head               *pos, *next;
1955 
1956         con_log(CL_ANN1, (CE_NOTE,
1957             "mrsas_complete_pending_cmds(): Called"));
1958 
1959         mutex_enter(&instance->cmd_pend_mtx);
1960         mlist_for_each_safe(pos, next, &instance->cmd_pend_list) {
1961                 cmd = mlist_entry(pos, struct mrsas_cmd, list);
1962                 if (cmd) {
1963                         pkt = cmd->pkt;
1964                         if (pkt) { /* for IO */
1965                                 if (((pkt->pkt_flags & FLAG_NOINTR)
1966                                     == 0) && pkt->pkt_comp) {
1967                                         pkt->pkt_reason
1968                                             = CMD_DEV_GONE;
1969                                         pkt->pkt_statistics
1970                                             = STAT_DISCON;
1971                                         con_log(CL_ANN1, (CE_NOTE,
1972                                             "fail and posting to scsa "
1973                                             "cmd %p index %x"
1974                                             " pkt %p "
1975                                             "time : %llx",
1976                                             (void *)cmd, cmd->index,
1977                                             (void *)pkt, gethrtime()));
1978                                         (*pkt->pkt_comp)(pkt);
1979                                 }
1980                         } else { /* for DCMDS */
1981                                 if (cmd->sync_cmd == MRSAS_TRUE) {
1982                                 hdr = (struct mrsas_header *)&cmd->frame->hdr;
1983                                 con_log(CL_ANN1, (CE_NOTE,
1984                                     "posting invalid status to application "
1985                                     "cmd %p index %x"
1986                                     " hdr %p "
1987                                     "time : %llx",
1988                                     (void *)cmd, cmd->index,
1989                                     (void *)hdr, gethrtime()));
1990                                 hdr->cmd_status = MFI_STAT_INVALID_STATUS;
1991                                 complete_cmd_in_sync_mode(instance, cmd);
1992                                 }
1993                         }
1994                         mlist_del_init(&cmd->list);
1995                 } else {
1996                         con_log(CL_ANN1, (CE_NOTE,
1997                             "mrsas_complete_pending_cmds:"
1998                             "NULL command\n"));
1999                 }
2000                 con_log(CL_ANN1, (CE_NOTE,
2001                     "mrsas_complete_pending_cmds:"
2002                     "looping for more commands\n"));
2003         }
2004         mutex_exit(&instance->cmd_pend_mtx);
2005 
2006         con_log(CL_ANN1, (CE_NOTE, "mrsas_complete_pending_cmds(): DONE\n"));
2007         return (DDI_SUCCESS);
2008 }
2009 










2010 
2011 static int




























































2012 mrsas_issue_pending_cmds(struct mrsas_instance *instance)
2013 {
2014         mlist_t *head   =       &instance->cmd_pend_list;
2015         mlist_t *tmp    =       head->next;
2016         struct mrsas_cmd *cmd = NULL;
2017         struct scsi_pkt *pkt;
2018 
2019         con_log(CL_ANN1, (CE_NOTE, "mrsas_issue_pending_cmds(): Called"));
2020         while (tmp != head) {
2021                 mutex_enter(&instance->cmd_pend_mtx);
2022                 cmd = mlist_entry(tmp, struct mrsas_cmd, list);
2023                 tmp = tmp->next;
2024                 mutex_exit(&instance->cmd_pend_mtx);
2025                 if (cmd) {
2026                         con_log(CL_ANN1, (CE_NOTE,
2027                             "mrsas_issue_pending_cmds(): "
2028                             "Got a cmd: cmd:%p\n", (void *)cmd));






2029                         cmd->retry_count_for_ocr++;
2030                         con_log(CL_ANN1, (CE_NOTE,
2031                             "mrsas_issue_pending_cmds(): "
2032                             "cmd retry count = %d\n",
2033                             cmd->retry_count_for_ocr));
2034                         if (cmd->retry_count_for_ocr > IO_RETRY_COUNT) {
2035                                 con_log(CL_ANN1, (CE_NOTE,





2036                                     "mrsas_issue_pending_cmds():"
2037                                     "Calling Kill Adapter\n"));



2038                                 (void) mrsas_kill_adapter(instance);
2039                                 return (DDI_FAILURE);
2040                         }

2041                         pkt = cmd->pkt;
2042                         if (pkt) {
2043                                 con_log(CL_ANN1, (CE_NOTE,
2044                                     "PENDING ISSUE: cmd %p index %x "
2045                                     "pkt %p time %llx",
2046                                     (void *)cmd, cmd->index,
2047                                     (void *)pkt,
2048                                     gethrtime()));
2049 





2050                         }


2051                         if (cmd->sync_cmd == MRSAS_TRUE) {


2052                                 instance->func_ptr->issue_cmd_in_sync_mode(
2053                                     instance, cmd);
2054                         } else {
2055                                 instance->func_ptr->issue_cmd(cmd, instance);
2056                         }
2057                 } else {
2058                         con_log(CL_ANN1, (CE_NOTE,
2059                             "mrsas_issue_pending_cmds: NULL command\n"));
2060                 }
2061                 con_log(CL_ANN1, (CE_NOTE,
2062                     "mrsas_issue_pending_cmds:"
2063                     "looping for more commands"));
2064         }
2065         con_log(CL_ANN1, (CE_NOTE, "mrsas_issue_pending_cmds(): DONE\n"));
2066         return (DDI_SUCCESS);
2067 }
2068 


2069 /*
2070  * destroy_mfi_frame_pool
2071  */
2072 static void
2073 destroy_mfi_frame_pool(struct mrsas_instance *instance)
2074 {
2075         int             i;
2076         uint32_t        max_cmd = instance->max_fw_cmds;
2077 
2078         struct mrsas_cmd        *cmd;
2079 
2080         /* return all frames to pool */
2081         for (i = 0; i < max_cmd+1; i++) {
2082 


2083                 cmd = instance->cmd_list[i];
2084 
2085                 if (cmd->frame_dma_obj_status == DMA_OBJ_ALLOCATED)
2086                         (void) mrsas_free_dma_obj(instance, cmd->frame_dma_obj);
2087 
2088                 cmd->frame_dma_obj_status  = DMA_OBJ_FREED;
2089         }
2090 
2091 }
2092 
2093 /*
2094  * create_mfi_frame_pool
2095  */
2096 static int
2097 create_mfi_frame_pool(struct mrsas_instance *instance)
2098 {
2099         int             i = 0;
2100         int             cookie_cnt;
2101         uint16_t        max_cmd;
2102         uint16_t        sge_sz;
2103         uint32_t        sgl_sz;
2104         uint32_t        tot_frame_size;
2105         struct mrsas_cmd        *cmd;

2106 
2107         max_cmd = instance->max_fw_cmds;
2108 
2109         sge_sz  = sizeof (struct mrsas_sge_ieee);
2110 
2111         /* calculated the number of 64byte frames required for SGL */
2112         sgl_sz          = sge_sz * instance->max_num_sge;
2113         tot_frame_size  = sgl_sz + MRMFI_FRAME_SIZE + SENSE_LENGTH;
2114 
2115         con_log(CL_DLEVEL3, (CE_NOTE, "create_mfi_frame_pool: "
2116             "sgl_sz %x tot_frame_size %x", sgl_sz, tot_frame_size));
2117 
2118         while (i < max_cmd+1) {
2119                 cmd = instance->cmd_list[i];
2120 
2121                 cmd->frame_dma_obj.size      = tot_frame_size;
2122                 cmd->frame_dma_obj.dma_attr = mrsas_generic_dma_attr;
2123                 cmd->frame_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
2124                 cmd->frame_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
2125                 cmd->frame_dma_obj.dma_attr.dma_attr_sgllen = 1;
2126                 cmd->frame_dma_obj.dma_attr.dma_attr_align = 64;
2127 
2128 
2129                 cookie_cnt = mrsas_alloc_dma_obj(instance, &cmd->frame_dma_obj,
2130                     (uchar_t)DDI_STRUCTURE_LE_ACC);
2131 
2132                 if (cookie_cnt == -1 || cookie_cnt > 1) {
2133                         con_log(CL_ANN, (CE_WARN,
2134                             "create_mfi_frame_pool: could not alloc."));
2135                         return (DDI_FAILURE);

2136                 }
2137 
2138                 bzero(cmd->frame_dma_obj.buffer, tot_frame_size);
2139 
2140                 cmd->frame_dma_obj_status = DMA_OBJ_ALLOCATED;
2141                 cmd->frame = (union mrsas_frame *)cmd->frame_dma_obj.buffer;
2142                 cmd->frame_phys_addr =
2143                     cmd->frame_dma_obj.dma_cookie[0].dmac_address;
2144 
2145                 cmd->sense = (uint8_t *)(((unsigned long)
2146                     cmd->frame_dma_obj.buffer) +
2147                     tot_frame_size - SENSE_LENGTH);
2148                 cmd->sense_phys_addr =
2149                     cmd->frame_dma_obj.dma_cookie[0].dmac_address +
2150                     tot_frame_size - SENSE_LENGTH;
2151 
2152                 if (!cmd->frame || !cmd->sense) {
2153                         con_log(CL_ANN, (CE_NOTE,
2154                             "mr_sas: pci_pool_alloc failed"));
2155 
2156                         return (ENOMEM);
2157                 }
2158 
2159                 ddi_put32(cmd->frame_dma_obj.acc_handle,
2160                     &cmd->frame->io.context, cmd->index);
2161                 i++;
2162 
2163                 con_log(CL_DLEVEL3, (CE_NOTE, "[%x]-%x",
2164                     cmd->index, cmd->frame_phys_addr));
2165         }
2166 
2167         return (DDI_SUCCESS);






2168 }
2169 
2170 /*
2171  * free_additional_dma_buffer
2172  */
2173 static void
2174 free_additional_dma_buffer(struct mrsas_instance *instance)
2175 {
2176         if (instance->mfi_internal_dma_obj.status == DMA_OBJ_ALLOCATED) {
2177                 (void) mrsas_free_dma_obj(instance,
2178                     instance->mfi_internal_dma_obj);
2179                 instance->mfi_internal_dma_obj.status = DMA_OBJ_FREED;
2180         }
2181 
2182         if (instance->mfi_evt_detail_obj.status == DMA_OBJ_ALLOCATED) {
2183                 (void) mrsas_free_dma_obj(instance,
2184                     instance->mfi_evt_detail_obj);
2185                 instance->mfi_evt_detail_obj.status = DMA_OBJ_FREED;
2186         }
2187 }


2190  * alloc_additional_dma_buffer
2191  */
2192 static int
2193 alloc_additional_dma_buffer(struct mrsas_instance *instance)
2194 {
2195         uint32_t        reply_q_sz;
2196         uint32_t        internal_buf_size = PAGESIZE*2;
2197 
2198         /* max cmds plus 1 + producer & consumer */
2199         reply_q_sz = sizeof (uint32_t) * (instance->max_fw_cmds + 1 + 2);
2200 
2201         instance->mfi_internal_dma_obj.size = internal_buf_size;
2202         instance->mfi_internal_dma_obj.dma_attr      = mrsas_generic_dma_attr;
2203         instance->mfi_internal_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
2204         instance->mfi_internal_dma_obj.dma_attr.dma_attr_count_max =
2205             0xFFFFFFFFU;
2206         instance->mfi_internal_dma_obj.dma_attr.dma_attr_sgllen      = 1;
2207 
2208         if (mrsas_alloc_dma_obj(instance, &instance->mfi_internal_dma_obj,
2209             (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
2210                 con_log(CL_ANN, (CE_WARN,
2211                     "mr_sas: could not alloc reply queue"));
2212                 return (DDI_FAILURE);
2213         }
2214 
2215         bzero(instance->mfi_internal_dma_obj.buffer, internal_buf_size);
2216 
2217         instance->mfi_internal_dma_obj.status |= DMA_OBJ_ALLOCATED;
2218 
2219         instance->producer = (uint32_t *)((unsigned long)
2220             instance->mfi_internal_dma_obj.buffer);
2221         instance->consumer = (uint32_t *)((unsigned long)
2222             instance->mfi_internal_dma_obj.buffer + 4);
2223         instance->reply_queue = (uint32_t *)((unsigned long)
2224             instance->mfi_internal_dma_obj.buffer + 8);
2225         instance->internal_buf = (caddr_t)(((unsigned long)
2226             instance->mfi_internal_dma_obj.buffer) + reply_q_sz + 8);
2227         instance->internal_buf_dmac_add =
2228             instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address +
2229             (reply_q_sz + 8);
2230         instance->internal_buf_size = internal_buf_size -
2231             (reply_q_sz + 8);
2232 
2233         /* allocate evt_detail */
2234         instance->mfi_evt_detail_obj.size = sizeof (struct mrsas_evt_detail);
2235         instance->mfi_evt_detail_obj.dma_attr = mrsas_generic_dma_attr;
2236         instance->mfi_evt_detail_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
2237         instance->mfi_evt_detail_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
2238         instance->mfi_evt_detail_obj.dma_attr.dma_attr_sgllen = 1;
2239         instance->mfi_evt_detail_obj.dma_attr.dma_attr_align = 1;
2240 
2241         if (mrsas_alloc_dma_obj(instance, &instance->mfi_evt_detail_obj,
2242             (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
2243                 con_log(CL_ANN, (CE_WARN, "alloc_additional_dma_buffer: "
2244                     "could not allocate data transfer buffer."));
2245                 return (DDI_FAILURE);
2246         }
2247 
2248         bzero(instance->mfi_evt_detail_obj.buffer,
2249             sizeof (struct mrsas_evt_detail));
2250 
2251         instance->mfi_evt_detail_obj.status |= DMA_OBJ_ALLOCATED;
2252 
2253         return (DDI_SUCCESS);









2254 }
2255 
2256 /*
2257  * free_space_for_mfi
2258  */
2259 static void
2260 free_space_for_mfi(struct mrsas_instance *instance)
2261 {
2262         int             i;
2263         uint32_t        max_cmd = instance->max_fw_cmds;

2264 
2265         /* already freed */
2266         if (instance->cmd_list == NULL) {
2267                 return;
2268         }
2269 
2270         free_additional_dma_buffer(instance);
2271 
2272         /* first free the MFI frame pool */
2273         destroy_mfi_frame_pool(instance);
2274 
2275         /* free all the commands in the cmd_list */
2276         for (i = 0; i < instance->max_fw_cmds+1; i++) {

2277                 kmem_free(instance->cmd_list[i],
2278                     sizeof (struct mrsas_cmd));

2279 
2280                 instance->cmd_list[i] = NULL;
2281         }
2282 
2283         /* free the cmd_list buffer itself */
2284         kmem_free(instance->cmd_list,
2285             sizeof (struct mrsas_cmd *) * (max_cmd+1));
2286 
2287         instance->cmd_list = NULL;
2288 
2289         INIT_LIST_HEAD(&instance->cmd_pool_list);
2290         INIT_LIST_HEAD(&instance->app_cmd_pool_list);
2291         INIT_LIST_HEAD(&instance->cmd_pend_list);






2292 }
2293 

2294 /*
2295  * alloc_space_for_mfi
2296  */
2297 static int
2298 alloc_space_for_mfi(struct mrsas_instance *instance)
2299 {
2300         int             i;

2301         uint32_t        max_cmd;
2302         uint32_t        reserve_cmd;
2303         size_t          sz;
2304 
2305         struct mrsas_cmd        *cmd;
2306 
2307         max_cmd = instance->max_fw_cmds;


2308 
2309         /* reserve 1 more slot for flush_cache */
2310         sz = sizeof (struct mrsas_cmd *) * (max_cmd+1);
2311 


2312         /*
2313          * instance->cmd_list is an array of struct mrsas_cmd pointers.
2314          * Allocate the dynamic array first and then allocate individual
2315          * commands.
2316          */
2317         instance->cmd_list = kmem_zalloc(sz, KM_SLEEP);
2318         ASSERT(instance->cmd_list);
2319 
2320         for (i = 0; i < max_cmd+1; i++) {
2321                 instance->cmd_list[i] = kmem_zalloc(sizeof (struct mrsas_cmd),
2322                     KM_SLEEP);
2323                 ASSERT(instance->cmd_list[i]);

2324         }
2325 


2326         INIT_LIST_HEAD(&instance->cmd_pool_list);
2327         INIT_LIST_HEAD(&instance->cmd_pend_list);
2328         /* add all the commands to command pool (instance->cmd_pool) */
2329         reserve_cmd     =       APP_RESERVE_CMDS;
2330         INIT_LIST_HEAD(&instance->app_cmd_pool_list);
2331         for (i = 0; i < reserve_cmd-1; i++) {



2332                 cmd     = instance->cmd_list[i];
2333                 cmd->index   = i;
2334                 mlist_add_tail(&cmd->list, &instance->app_cmd_pool_list);
2335         }
2336         /*
2337          * reserve slot instance->cmd_list[APP_RESERVE_CMDS-1]
2338          * for abort_aen_cmd
2339          */
2340         for (i = reserve_cmd; i < max_cmd; i++) {
2341                 cmd                     = instance->cmd_list[i];
2342                 cmd->index   = i;
2343                 mlist_add_tail(&cmd->list, &instance->cmd_pool_list);
2344         }
2345 
2346         /* single slot for flush_cache won't be added in command pool */
2347         cmd             = instance->cmd_list[max_cmd];
2348         cmd->index   = i;
2349 
2350         /* create a frame pool and assign one frame to each cmd */
2351         if (create_mfi_frame_pool(instance)) {
2352                 con_log(CL_ANN, (CE_NOTE, "error creating frame DMA pool"));














2353                 return (DDI_FAILURE);













2354         }
2355 
2356         /* create a frame pool and assign one frame to each cmd */
2357         if (alloc_additional_dma_buffer(instance)) {
2358                 con_log(CL_ANN, (CE_NOTE, "error creating frame DMA pool"));

















2359                 return (DDI_FAILURE);
2360         }
2361 












2362         return (DDI_SUCCESS);








2363 }
2364 
2365 

2366 /*
2367  * get_ctrl_info
2368  */
2369 static int
2370 get_ctrl_info(struct mrsas_instance *instance,
2371     struct mrsas_ctrl_info *ctrl_info)
2372 {
2373         int     ret = 0;
2374 
2375         struct mrsas_cmd                *cmd;
2376         struct mrsas_dcmd_frame *dcmd;
2377         struct mrsas_ctrl_info  *ci;
2378 



2379         cmd = get_mfi_pkt(instance);

2380 
2381         if (!cmd) {
2382                 con_log(CL_ANN, (CE_WARN,
2383                     "Failed to get a cmd for ctrl info"));
2384                 DTRACE_PROBE2(info_mfi_err, uint16_t, instance->fw_outstanding,
2385                     uint16_t, instance->max_fw_cmds);
2386                 return (DDI_FAILURE);
2387         }
2388         cmd->retry_count_for_ocr = 0;
2389         /* Clear the frame buffer and assign back the context id */
2390         (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
2391         ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
2392             cmd->index);
2393 
2394         dcmd = &cmd->frame->dcmd;
2395 
2396         ci = (struct mrsas_ctrl_info *)instance->internal_buf;
2397 
2398         if (!ci) {
2399                 con_log(CL_ANN, (CE_WARN,
2400                     "Failed to alloc mem for ctrl info"));
2401                 return_mfi_pkt(instance, cmd);
2402                 return (DDI_FAILURE);
2403         }
2404 
2405         (void) memset(ci, 0, sizeof (struct mrsas_ctrl_info));
2406 
2407         /* for( i = 0; i < DCMD_MBOX_SZ; i++ ) dcmd->mbox.b[i] = 0; */
2408         (void) memset(dcmd->mbox.b, 0, DCMD_MBOX_SZ);
2409 
2410         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd, MFI_CMD_OP_DCMD);
2411         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd_status,
2412             MFI_CMD_STATUS_POLL_MODE);
2413         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->sge_count, 1);
2414         ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->flags,
2415             MFI_FRAME_DIR_READ);
2416         ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->timeout, 0);
2417         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->data_xfer_len,
2418             sizeof (struct mrsas_ctrl_info));
2419         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->opcode,
2420             MR_DCMD_CTRL_GET_INFO);
2421         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].phys_addr,
2422             instance->internal_buf_dmac_add);
2423         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].length,
2424             sizeof (struct mrsas_ctrl_info));
2425 
2426         cmd->frame_count = 1;
2427 




2428         if (!instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) {
2429         ret = 0;
2430 
2431         ctrl_info->max_request_size = ddi_get32(
2432             cmd->frame_dma_obj.acc_handle, &ci->max_request_size);
2433 
2434         ctrl_info->ld_present_count = ddi_get16(
2435             cmd->frame_dma_obj.acc_handle, &ci->ld_present_count);
2436 
2437         ctrl_info->properties.on_off_properties =
2438             ddi_get32(cmd->frame_dma_obj.acc_handle,
2439             &ci->properties.on_off_properties);
2440 
2441         ddi_rep_get8(cmd->frame_dma_obj.acc_handle,
2442             (uint8_t *)(ctrl_info->product_name),
2443             (uint8_t *)(ci->product_name), 80 * sizeof (char),
2444             DDI_DEV_AUTOINCR);
2445         /* should get more members of ci with ddi_get when needed */
2446         } else {
2447                 con_log(CL_ANN, (CE_WARN, "get_ctrl_info: Ctrl info failed"));
2448                 ret = -1;
2449         }
2450 
2451         if (mrsas_common_check(instance, cmd) != DDI_SUCCESS) {
2452                 ret = -1;
2453         }



2454         return_mfi_pkt(instance, cmd);

2455 
2456         return (ret);
2457 }
2458 
2459 /*
2460  * abort_aen_cmd
2461  */
2462 static int
2463 abort_aen_cmd(struct mrsas_instance *instance,
2464     struct mrsas_cmd *cmd_to_abort)
2465 {
2466         int     ret = 0;
2467 
2468         struct mrsas_cmd                *cmd;
2469         struct mrsas_abort_frame        *abort_fr;
2470 
2471         cmd = instance->cmd_list[APP_RESERVE_CMDS-1];
2472 






2473         if (!cmd) {
2474                 con_log(CL_ANN1, (CE_WARN,
2475                     "abort_aen_cmd():Failed to get a cmd for abort_aen_cmd"));
2476                 DTRACE_PROBE2(abort_mfi_err, uint16_t, instance->fw_outstanding,
2477                     uint16_t, instance->max_fw_cmds);
2478                 return (DDI_FAILURE);
2479         }
2480         cmd->retry_count_for_ocr = 0;
2481         /* Clear the frame buffer and assign back the context id */
2482         (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
2483         ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
2484             cmd->index);
2485 
2486         abort_fr = &cmd->frame->abort;
2487 
2488         /* prepare and issue the abort frame */
2489         ddi_put8(cmd->frame_dma_obj.acc_handle,
2490             &abort_fr->cmd, MFI_CMD_OP_ABORT);
2491         ddi_put8(cmd->frame_dma_obj.acc_handle, &abort_fr->cmd_status,
2492             MFI_CMD_STATUS_SYNC_MODE);
2493         ddi_put16(cmd->frame_dma_obj.acc_handle, &abort_fr->flags, 0);
2494         ddi_put32(cmd->frame_dma_obj.acc_handle, &abort_fr->abort_context,
2495             cmd_to_abort->index);
2496         ddi_put32(cmd->frame_dma_obj.acc_handle,
2497             &abort_fr->abort_mfi_phys_addr_lo, cmd_to_abort->frame_phys_addr);
2498         ddi_put32(cmd->frame_dma_obj.acc_handle,
2499             &abort_fr->abort_mfi_phys_addr_hi, 0);
2500 
2501         instance->aen_cmd->abort_aen = 1;
2502 
2503         cmd->sync_cmd = MRSAS_TRUE;
2504         cmd->frame_count = 1;
2505 




2506         if (instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) {
2507                 con_log(CL_ANN1, (CE_WARN,
2508                     "abort_aen_cmd: issue_cmd_in_poll_mode failed"));
2509                 ret = -1;
2510         } else {
2511                 ret = 0;
2512         }
2513 
2514         instance->aen_cmd->abort_aen = 1;
2515         instance->aen_cmd = 0;
2516 






2517         atomic_add_16(&instance->fw_outstanding, (-1));
2518 
2519         return (ret);
2520 }
2521 
2522 
2523 /*
2524  * init_mfi
2525  */
2526 static int
2527 init_mfi(struct mrsas_instance *instance)

2528 {
2529         struct mrsas_cmd                *cmd;
2530         struct mrsas_ctrl_info          ctrl_info;
2531         struct mrsas_init_frame         *init_frame;
2532         struct mrsas_init_queue_info    *initq_info;

2533 
2534         /* we expect the FW state to be READY */
2535         if (mfi_state_transition_to_ready(instance)) {
2536                 con_log(CL_ANN, (CE_WARN, "mr_sas: F/W is not ready"));
2537                 goto fail_ready_state;
2538         }
2539 
2540         /* get various operational parameters from status register */
2541         instance->max_num_sge =
2542             (instance->func_ptr->read_fw_status_reg(instance) &
2543             0xFF0000) >> 0x10;
2544         /*
2545          * Reduce the max supported cmds by 1. This is to ensure that the
2546          * reply_q_sz (1 more than the max cmd that driver may send)
2547          * does not exceed max cmds that the FW can support
2548          */
2549         instance->max_fw_cmds =
2550             instance->func_ptr->read_fw_status_reg(instance) & 0xFFFF;
2551         instance->max_fw_cmds = instance->max_fw_cmds - 1;
2552 
2553         instance->max_num_sge =
2554             (instance->max_num_sge > MRSAS_MAX_SGE_CNT) ?
2555             MRSAS_MAX_SGE_CNT : instance->max_num_sge;
2556 
2557         /* create a pool of commands */
2558         if (alloc_space_for_mfi(instance) != DDI_SUCCESS)
2559                 goto fail_alloc_fw_space;
2560 
2561         /*
2562          * Prepare a init frame. Note the init frame points to queue info
2563          * structure. Each frame has SGL allocated after first 64 bytes. For
2564          * this frame - since we don't need any SGL - we use SGL's space as
2565          * queue info structure
2566          */
2567         cmd = get_mfi_pkt(instance);
2568         cmd->retry_count_for_ocr = 0;
2569 

2570         /* Clear the frame buffer and assign back the context id */
2571         (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
2572         ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
2573             cmd->index);
2574 
2575         init_frame = (struct mrsas_init_frame *)cmd->frame;
2576         initq_info = (struct mrsas_init_queue_info *)
2577             ((unsigned long)init_frame + 64);
2578 
2579         (void) memset(init_frame, 0, MRMFI_FRAME_SIZE);
2580         (void) memset(initq_info, 0, sizeof (struct mrsas_init_queue_info));
2581 
2582         ddi_put32(cmd->frame_dma_obj.acc_handle, &initq_info->init_flags, 0);
2583 
2584         ddi_put32(cmd->frame_dma_obj.acc_handle,
2585             &initq_info->reply_queue_entries, instance->max_fw_cmds + 1);
2586 
2587         ddi_put32(cmd->frame_dma_obj.acc_handle,
2588             &initq_info->producer_index_phys_addr_hi, 0);
2589         ddi_put32(cmd->frame_dma_obj.acc_handle,


2596             &initq_info->consumer_index_phys_addr_lo,
2597             instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address + 4);
2598 
2599         ddi_put32(cmd->frame_dma_obj.acc_handle,
2600             &initq_info->reply_queue_start_phys_addr_hi, 0);
2601         ddi_put32(cmd->frame_dma_obj.acc_handle,
2602             &initq_info->reply_queue_start_phys_addr_lo,
2603             instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address + 8);
2604 
2605         ddi_put8(cmd->frame_dma_obj.acc_handle,
2606             &init_frame->cmd, MFI_CMD_OP_INIT);
2607         ddi_put8(cmd->frame_dma_obj.acc_handle, &init_frame->cmd_status,
2608             MFI_CMD_STATUS_POLL_MODE);
2609         ddi_put16(cmd->frame_dma_obj.acc_handle, &init_frame->flags, 0);
2610         ddi_put32(cmd->frame_dma_obj.acc_handle,
2611             &init_frame->queue_info_new_phys_addr_lo,
2612             cmd->frame_phys_addr + 64);
2613         ddi_put32(cmd->frame_dma_obj.acc_handle,
2614             &init_frame->queue_info_new_phys_addr_hi, 0);
2615 































2616         ddi_put32(cmd->frame_dma_obj.acc_handle, &init_frame->data_xfer_len,
2617             sizeof (struct mrsas_init_queue_info));
2618 
2619         cmd->frame_count = 1;
2620 
2621         /* issue the init frame in polled mode */





































2622         if (instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) {
2623                 con_log(CL_ANN, (CE_WARN, "failed to init firmware"));
2624                 return_mfi_pkt(instance, cmd);
2625                 goto fail_fw_init;
2626         }
2627 
2628         if (mrsas_common_check(instance, cmd) != DDI_SUCCESS) {
2629                 return_mfi_pkt(instance, cmd);
2630                 goto fail_fw_init;
2631         }
2632         return_mfi_pkt(instance, cmd);
2633 
2634         if (ctio_enable &&
2635             (instance->func_ptr->read_fw_status_reg(instance) & 0x04000000)) {
2636                 con_log(CL_ANN, (CE_NOTE, "mr_sas: IEEE SGL's supported"));
2637                 instance->flag_ieee = 1;
2638         } else {
2639                 instance->flag_ieee = 0;
2640         }
2641 
2642         instance->disable_online_ctrl_reset = 0;

























































2643         /* gather misc FW related information */


2644         if (!get_ctrl_info(instance, &ctrl_info)) {
2645                 instance->max_sectors_per_req = ctrl_info.max_request_size;
2646                 con_log(CL_ANN1, (CE_NOTE,
2647                     "product name %s ld present %d",
2648                     ctrl_info.product_name, ctrl_info.ld_present_count));
2649         } else {
2650                 instance->max_sectors_per_req = instance->max_num_sge *
2651                     PAGESIZE / 512;
2652         }
2653 
2654         if (ctrl_info.properties.on_off_properties & DISABLE_OCR_PROP_FLAG)
2655                 instance->disable_online_ctrl_reset = 1;
2656 
2657         return (DDI_SUCCESS);
2658 
2659 fail_fw_init:
2660 fail_alloc_fw_space:
2661 
2662         free_space_for_mfi(instance);
2663 
2664 fail_ready_state:
2665         ddi_regs_map_free(&instance->regmap_handle);
2666 
2667 fail_mfi_reg_setup:
2668         return (DDI_FAILURE);
2669 }
2670 
2671 
2672 
2673 
2674 
2675 
2676 static int
2677 mrsas_issue_init_mfi(struct mrsas_instance *instance)
2678 {
2679         struct mrsas_cmd                *cmd;
2680         struct mrsas_init_frame         *init_frame;
2681         struct mrsas_init_queue_info    *initq_info;
2682 
2683 /*
2684  * Prepare a init frame. Note the init frame points to queue info
2685  * structure. Each frame has SGL allocated after first 64 bytes. For
2686  * this frame - since we don't need any SGL - we use SGL's space as
2687  * queue info structure
2688  */
2689         con_log(CL_ANN1, (CE_NOTE,
2690             "mrsas_issue_init_mfi: entry\n"));
2691         cmd = get_mfi_app_pkt(instance);
2692 
2693         if (!cmd) {
2694                 con_log(CL_ANN1, (CE_NOTE,
2695                     "mrsas_issue_init_mfi: get_pkt failed\n"));
2696                 return (DDI_FAILURE);
2697         }
2698 
2699         /* Clear the frame buffer and assign back the context id */
2700         (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
2701         ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
2702             cmd->index);
2703 
2704         init_frame = (struct mrsas_init_frame *)cmd->frame;
2705         initq_info = (struct mrsas_init_queue_info *)
2706             ((unsigned long)init_frame + 64);
2707 
2708         (void) memset(init_frame, 0, MRMFI_FRAME_SIZE);
2709         (void) memset(initq_info, 0, sizeof (struct mrsas_init_queue_info));
2710 
2711         ddi_put32(cmd->frame_dma_obj.acc_handle, &initq_info->init_flags, 0);
2712 
2713         ddi_put32(cmd->frame_dma_obj.acc_handle,
2714             &initq_info->reply_queue_entries, instance->max_fw_cmds + 1);


2736         ddi_put16(cmd->frame_dma_obj.acc_handle, &init_frame->flags, 0);
2737         ddi_put32(cmd->frame_dma_obj.acc_handle,
2738             &init_frame->queue_info_new_phys_addr_lo,
2739             cmd->frame_phys_addr + 64);
2740         ddi_put32(cmd->frame_dma_obj.acc_handle,
2741             &init_frame->queue_info_new_phys_addr_hi, 0);
2742 
2743         ddi_put32(cmd->frame_dma_obj.acc_handle, &init_frame->data_xfer_len,
2744             sizeof (struct mrsas_init_queue_info));
2745 
2746         cmd->frame_count = 1;
2747 
2748         /* issue the init frame in polled mode */
2749         if (instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) {
2750                 con_log(CL_ANN1, (CE_WARN,
2751                     "mrsas_issue_init_mfi():failed to "
2752                     "init firmware"));
2753                 return_mfi_app_pkt(instance, cmd);
2754                 return (DDI_FAILURE);
2755         }






2756         return_mfi_app_pkt(instance, cmd);
2757         con_log(CL_ANN1, (CE_NOTE, "mrsas_issue_init_mfi: Done"));

2758         return (DDI_SUCCESS);
2759 }
2760 /*
2761  * mfi_state_transition_to_ready        : Move the FW to READY state
2762  *
2763  * @reg_set                     : MFI register set
2764  */
2765 static int
2766 mfi_state_transition_to_ready(struct mrsas_instance *instance)
2767 {
2768         int             i;
2769         uint8_t         max_wait;
2770         uint32_t        fw_ctrl;
2771         uint32_t        fw_state;
2772         uint32_t        cur_state;
2773         uint32_t        cur_abs_reg_val;
2774         uint32_t        prev_abs_reg_val;

2775 
2776         cur_abs_reg_val =
2777             instance->func_ptr->read_fw_status_reg(instance);
2778         fw_state =
2779             cur_abs_reg_val & MFI_STATE_MASK;
2780         con_log(CL_ANN1, (CE_NOTE,
2781             "mfi_state_transition_to_ready:FW state = 0x%x", fw_state));
2782 
2783         while (fw_state != MFI_STATE_READY) {
2784                 con_log(CL_ANN, (CE_NOTE,
2785                     "mfi_state_transition_to_ready:FW state%x", fw_state));
2786 
2787                 switch (fw_state) {
2788                 case MFI_STATE_FAULT:
2789                         con_log(CL_ANN1, (CE_NOTE,
2790                             "mr_sas: FW in FAULT state!!"));
2791 
2792                         return (ENODEV);
2793                 case MFI_STATE_WAIT_HANDSHAKE:
2794                         /* set the CLR bit in IMR0 */
2795                         con_log(CL_ANN1, (CE_NOTE,
2796                             "mr_sas: FW waiting for HANDSHAKE"));
2797                         /*
2798                          * PCI_Hot Plug: MFI F/W requires
2799                          * (MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG)
2800                          * to be set
2801                          */
2802                         /* WR_IB_MSG_0(MFI_INIT_CLEAR_HANDSHAKE, instance); */

2803                         WR_IB_DOORBELL(MFI_INIT_CLEAR_HANDSHAKE |
2804                             MFI_INIT_HOTPLUG, instance);
2805 
2806                         max_wait        = 2;



2807                         cur_state       = MFI_STATE_WAIT_HANDSHAKE;
2808                         break;
2809                 case MFI_STATE_BOOT_MESSAGE_PENDING:
2810                         /* set the CLR bit in IMR0 */
2811                         con_log(CL_ANN1, (CE_NOTE,
2812                             "mr_sas: FW state boot message pending"));
2813                         /*
2814                          * PCI_Hot Plug: MFI F/W requires
2815                          * (MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG)
2816                          * to be set
2817                          */

2818                         WR_IB_DOORBELL(MFI_INIT_HOTPLUG, instance);
2819 
2820                         max_wait        = 10;



2821                         cur_state       = MFI_STATE_BOOT_MESSAGE_PENDING;
2822                         break;
2823                 case MFI_STATE_OPERATIONAL:
2824                         /* bring it to READY state; assuming max wait 2 secs */
2825                         instance->func_ptr->disable_intr(instance);
2826                         con_log(CL_ANN1, (CE_NOTE,
2827                             "mr_sas: FW in OPERATIONAL state"));
2828                         /*
2829                          * PCI_Hot Plug: MFI F/W requires
2830                          * (MFI_INIT_READY | MFI_INIT_MFIMODE | MFI_INIT_ABORT)
2831                          * to be set
2832                          */
2833                         /* WR_IB_DOORBELL(MFI_INIT_READY, instance); */

2834                         WR_IB_DOORBELL(MFI_RESET_FLAGS, instance);



2835 
2836                         max_wait        = 10;












2837                         cur_state       = MFI_STATE_OPERATIONAL;
2838                         break;
2839                 case MFI_STATE_UNDEFINED:
2840                         /* this state should not last for more than 2 seconds */
2841                         con_log(CL_ANN1, (CE_NOTE, "FW state undefined"));
2842 
2843                         max_wait        = 2;
2844                         cur_state       = MFI_STATE_UNDEFINED;
2845                         break;
2846                 case MFI_STATE_BB_INIT:
2847                         max_wait        = 2;
2848                         cur_state       = MFI_STATE_BB_INIT;
2849                         break;
2850                 case MFI_STATE_FW_INIT:
2851                         max_wait        = 2;
2852                         cur_state       = MFI_STATE_FW_INIT;
2853                         break;




2854                 case MFI_STATE_DEVICE_SCAN:
2855                         max_wait        = 180;
2856                         cur_state       = MFI_STATE_DEVICE_SCAN;
2857                         prev_abs_reg_val = cur_abs_reg_val;
2858                         con_log(CL_NONE, (CE_NOTE,
2859                             "Device scan in progress ...\n"));
2860                         break;




2861                 default:
2862                         con_log(CL_ANN1, (CE_NOTE,
2863                             "mr_sas: Unknown state 0x%x", fw_state));
2864                         return (ENODEV);
2865                 }
2866 
2867                 /* the cur_state should not last for more than max_wait secs */
2868                 for (i = 0; i < (max_wait * MILLISEC); i++) {
2869                         /* fw_state = RD_OB_MSG_0(instance) & MFI_STATE_MASK; */
2870                         cur_abs_reg_val =
2871                             instance->func_ptr->read_fw_status_reg(instance);
2872                         fw_state = cur_abs_reg_val & MFI_STATE_MASK;
2873 
2874                         if (fw_state == cur_state) {
2875                                 delay(1 * drv_usectohz(MILLISEC));
2876                         } else {
2877                                 break;
2878                         }
2879                 }
2880                 if (fw_state == MFI_STATE_DEVICE_SCAN) {
2881                         if (prev_abs_reg_val != cur_abs_reg_val) {
2882                                 continue;
2883                         }
2884                 }
2885 
2886                 /* return error if fw_state hasn't changed after max_wait */
2887                 if (fw_state == cur_state) {
2888                         con_log(CL_ANN1, (CE_NOTE,
2889                             "FW state hasn't changed in %d secs", max_wait));
2890                         return (ENODEV);
2891                 }
2892         };
2893 

2894         fw_ctrl = RD_IB_DOORBELL(instance);
2895 
2896         con_log(CL_ANN1, (CE_NOTE,
2897             "mfi_state_transition_to_ready:FW ctrl = 0x%x", fw_ctrl));
2898 
2899         /*
2900          * Write 0xF to the doorbell register to do the following.
2901          * - Abort all outstanding commands (bit 0).
2902          * - Transition from OPERATIONAL to READY state (bit 1).
2903          * - Discard (possible) low MFA posted in 64-bit mode (bit-2).
2904          * - Set to release FW to continue running (i.e. BIOS handshake
2905          *   (bit 3).
2906          */
2907         WR_IB_DOORBELL(0xF, instance);

2908 
2909         if (mrsas_check_acc_handle(instance->regmap_handle) != DDI_SUCCESS) {
2910                 return (ENODEV);
2911         }

2912         return (DDI_SUCCESS);
2913 }
2914 
2915 /*
2916  * get_seq_num
2917  */
2918 static int
2919 get_seq_num(struct mrsas_instance *instance,
2920     struct mrsas_evt_log_info *eli)
2921 {
2922         int     ret = DDI_SUCCESS;
2923 
2924         dma_obj_t                       dcmd_dma_obj;
2925         struct mrsas_cmd                *cmd;
2926         struct mrsas_dcmd_frame         *dcmd;
2927         struct mrsas_evt_log_info *eli_tmp;



2928         cmd = get_mfi_pkt(instance);

2929 
2930         if (!cmd) {
2931                 cmn_err(CE_WARN, "mr_sas: failed to get a cmd");
2932                 DTRACE_PROBE2(seq_num_mfi_err, uint16_t,
2933                     instance->fw_outstanding, uint16_t, instance->max_fw_cmds);
2934                 return (ENOMEM);
2935         }
2936         cmd->retry_count_for_ocr = 0;
2937         /* Clear the frame buffer and assign back the context id */
2938         (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
2939         ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
2940             cmd->index);
2941 
2942         dcmd    = &cmd->frame->dcmd;
2943 
2944         /* allocate the data transfer buffer */
2945         dcmd_dma_obj.size = sizeof (struct mrsas_evt_log_info);
2946         dcmd_dma_obj.dma_attr = mrsas_generic_dma_attr;
2947         dcmd_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
2948         dcmd_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
2949         dcmd_dma_obj.dma_attr.dma_attr_sgllen = 1;
2950         dcmd_dma_obj.dma_attr.dma_attr_align = 1;
2951 
2952         if (mrsas_alloc_dma_obj(instance, &dcmd_dma_obj,
2953             (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
2954                 con_log(CL_ANN, (CE_WARN,
2955                     "get_seq_num: could not allocate data transfer buffer."));
2956                 return (DDI_FAILURE);
2957         }
2958 
2959         (void) memset(dcmd_dma_obj.buffer, 0,
2960             sizeof (struct mrsas_evt_log_info));
2961 
2962         (void) memset(dcmd->mbox.b, 0, DCMD_MBOX_SZ);
2963 
2964         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd, MFI_CMD_OP_DCMD);
2965         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd_status, 0);
2966         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->sge_count, 1);
2967         ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->flags,
2968             MFI_FRAME_DIR_READ);
2969         ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->timeout, 0);
2970         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->data_xfer_len,
2971             sizeof (struct mrsas_evt_log_info));
2972         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->opcode,
2973             MR_DCMD_CTRL_EVENT_GET_INFO);
2974         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].length,
2975             sizeof (struct mrsas_evt_log_info));
2976         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].phys_addr,
2977             dcmd_dma_obj.dma_cookie[0].dmac_address);
2978 
2979         cmd->sync_cmd = MRSAS_TRUE;
2980         cmd->frame_count = 1;
2981 




2982         if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) {
2983                 cmn_err(CE_WARN, "get_seq_num: "
2984                     "failed to issue MRSAS_DCMD_CTRL_EVENT_GET_INFO");
2985                 ret = DDI_FAILURE;
2986         } else {
2987                 eli_tmp = (struct mrsas_evt_log_info *)dcmd_dma_obj.buffer;
2988                 eli->newest_seq_num = ddi_get32(cmd->frame_dma_obj.acc_handle,
2989                     &eli_tmp->newest_seq_num);
2990                 ret = DDI_SUCCESS;
2991         }
2992 
2993         if (mrsas_free_dma_obj(instance, dcmd_dma_obj) != DDI_SUCCESS)
2994                 ret = DDI_FAILURE;
2995 
2996         if (mrsas_common_check(instance, cmd) != DDI_SUCCESS) {
2997                 ret = DDI_FAILURE;


2998         }
2999 
3000         return_mfi_pkt(instance, cmd);
3001 
3002         return (ret);
3003 }
3004 
3005 /*
3006  * start_mfi_aen
3007  */
3008 static int
3009 start_mfi_aen(struct mrsas_instance *instance)
3010 {
3011         int     ret = 0;
3012 
3013         struct mrsas_evt_log_info       eli;
3014         union mrsas_evt_class_locale    class_locale;
3015 
3016         /* get the latest sequence number from FW */
3017         (void) memset(&eli, 0, sizeof (struct mrsas_evt_log_info));
3018 
3019         if (get_seq_num(instance, &eli)) {
3020                 cmn_err(CE_WARN, "start_mfi_aen: failed to get seq num");
3021                 return (-1);
3022         }
3023 
3024         /* register AEN with FW for latest sequence number plus 1 */
3025         class_locale.members.reserved   = 0;
3026         class_locale.members.locale     = LE_16(MR_EVT_LOCALE_ALL);
3027         class_locale.members.class      = MR_EVT_CLASS_INFO;
3028         class_locale.word       = LE_32(class_locale.word);
3029         ret = register_mfi_aen(instance, eli.newest_seq_num + 1,
3030             class_locale.word);
3031 
3032         if (ret) {
3033                 cmn_err(CE_WARN, "start_mfi_aen: aen registration failed");
3034                 return (-1);
3035         }
3036 

3037         return (ret);
3038 }
3039 
3040 /*
3041  * flush_cache
3042  */
3043 static void
3044 flush_cache(struct mrsas_instance *instance)
3045 {
3046         struct mrsas_cmd                *cmd = NULL;
3047         struct mrsas_dcmd_frame         *dcmd;
3048         uint32_t        max_cmd = instance->max_fw_cmds;




3049 
3050         cmd = instance->cmd_list[max_cmd];
3051 
3052         if (!cmd) {
3053                 con_log(CL_ANN1, (CE_WARN,
3054                     "flush_cache():Failed to get a cmd for flush_cache"));
3055                 DTRACE_PROBE2(flush_cache_err, uint16_t,
3056                     instance->fw_outstanding, uint16_t, instance->max_fw_cmds);
3057                 return;
3058         }
3059         cmd->retry_count_for_ocr = 0;
3060         /* Clear the frame buffer and assign back the context id */
3061         (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
3062         ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
3063             cmd->index);
3064 
3065         dcmd = &cmd->frame->dcmd;
3066 
3067         (void) memset(dcmd->mbox.b, 0, DCMD_MBOX_SZ);
3068 
3069         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd, MFI_CMD_OP_DCMD);
3070         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd_status, 0x0);
3071         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->sge_count, 0);
3072         ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->flags,
3073             MFI_FRAME_DIR_NONE);
3074         ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->timeout, 0);
3075         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->data_xfer_len, 0);
3076         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->opcode,
3077             MR_DCMD_CTRL_CACHE_FLUSH);
3078         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->mbox.b[0],
3079             MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE);
3080 
3081         cmd->frame_count = 1;
3082 




3083         if (instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) {
3084                 con_log(CL_ANN1, (CE_WARN,
3085             "flush_cache: failed to issue MFI_DCMD_CTRL_CACHE_FLUSH"));
3086         }
3087         con_log(CL_ANN1, (CE_NOTE, "flush_cache done"));






3088 }
3089 
3090 /*
3091  * service_mfi_aen-     Completes an AEN command
3092  * @instance:                   Adapter soft state
3093  * @cmd:                        Command to be completed
3094  *
3095  */
3096 static void
3097 service_mfi_aen(struct mrsas_instance *instance, struct mrsas_cmd *cmd)
3098 {
3099         uint32_t        seq_num;
3100         struct mrsas_evt_detail *evt_detail =
3101             (struct mrsas_evt_detail *)instance->mfi_evt_detail_obj.buffer;
3102         int             rval = 0;
3103         int             tgt = 0;




3104         ddi_acc_handle_t                acc_handle;
3105 
3106         acc_handle = cmd->frame_dma_obj.acc_handle;
3107 

3108         cmd->cmd_status = ddi_get8(acc_handle, &cmd->frame->io.cmd_status);
3109 
3110         if (cmd->cmd_status == ENODATA) {
3111                 cmd->cmd_status = 0;
3112         }
3113 
3114         /*
3115          * log the MFI AEN event to the sysevent queue so that
3116          * application will get noticed
3117          */
3118         if (ddi_log_sysevent(instance->dip, DDI_VENDOR_LSI, "LSIMEGA", "SAS",
3119             NULL, NULL, DDI_NOSLEEP) != DDI_SUCCESS) {
3120                 int     instance_no = ddi_get_instance(instance->dip);
3121                 con_log(CL_ANN, (CE_WARN,
3122                     "mr_sas%d: Failed to log AEN event", instance_no));
3123         }
3124         /*
3125          * Check for any ld devices that has changed state. i.e. online
3126          * or offline.
3127          */
3128         con_log(CL_ANN1, (CE_NOTE,
3129             "AEN: code = %x class = %x locale = %x args = %x",
3130             ddi_get32(acc_handle, &evt_detail->code),
3131             evt_detail->cl.members.class,
3132             ddi_get16(acc_handle, &evt_detail->cl.members.locale),
3133             ddi_get8(acc_handle, &evt_detail->arg_type)));
3134 
3135         switch (ddi_get32(acc_handle, &evt_detail->code)) {
3136         case MR_EVT_CFG_CLEARED: {
3137                 for (tgt = 0; tgt < MRDRV_MAX_LD; tgt++) {
3138                         if (instance->mr_ld_list[tgt].dip != NULL) {




3139                                 rval = mrsas_service_evt(instance, tgt, 0,
3140                                     MRSAS_EVT_UNCONFIG_TGT, NULL);
3141                                 con_log(CL_ANN1, (CE_WARN,
3142                                     "mr_sas: CFG CLEARED AEN rval = %d "
3143                                     "tgt id = %d", rval, tgt));
3144                         }
3145                 }
3146                 break;
3147         }
3148 
3149         case MR_EVT_LD_DELETED: {




3150                 rval = mrsas_service_evt(instance,
3151                     ddi_get16(acc_handle, &evt_detail->args.ld.target_id), 0,
3152                     MRSAS_EVT_UNCONFIG_TGT, NULL);
3153                 con_log(CL_ANN1, (CE_WARN, "mr_sas: LD DELETED AEN rval = %d "
3154                     "tgt id = %d index = %d", rval,
3155                     ddi_get16(acc_handle, &evt_detail->args.ld.target_id),
3156                     ddi_get8(acc_handle, &evt_detail->args.ld.ld_index)));
3157                 break;
3158         } /* End of MR_EVT_LD_DELETED */
3159 
3160         case MR_EVT_LD_CREATED: {
3161                 rval = mrsas_service_evt(instance,
3162                     ddi_get16(acc_handle, &evt_detail->args.ld.target_id), 0,
3163                     MRSAS_EVT_CONFIG_TGT, NULL);
3164                 con_log(CL_ANN1, (CE_WARN, "mr_sas: LD CREATED AEN rval = %d "
3165                     "tgt id = %d index = %d", rval,
3166                     ddi_get16(acc_handle, &evt_detail->args.ld.target_id),
3167                     ddi_get8(acc_handle, &evt_detail->args.ld.ld_index)));
3168                 break;
3169         } /* End of MR_EVT_LD_CREATED */
















































































3170         } /* End of Main Switch */
3171 
3172         /* get copy of seq_num and class/locale for re-registration */
3173         seq_num = ddi_get32(acc_handle, &evt_detail->seq_num);
3174         seq_num++;
3175         (void) memset(instance->mfi_evt_detail_obj.buffer, 0,
3176             sizeof (struct mrsas_evt_detail));
3177 
3178         ddi_put8(acc_handle, &cmd->frame->dcmd.cmd_status, 0x0);
3179         ddi_put32(acc_handle, &cmd->frame->dcmd.mbox.w[0], seq_num);
3180 
3181         instance->aen_seq_num = seq_num;
3182 
3183         cmd->frame_count = 1;
3184 



3185         /* Issue the aen registration frame */
3186         instance->func_ptr->issue_cmd(cmd, instance);
3187 }
3188 
3189 /*
3190  * complete_cmd_in_sync_mode -  Completes an internal command
3191  * @instance:                   Adapter soft state
3192  * @cmd:                        Command to be completed
3193  *
3194  * The issue_cmd_in_sync_mode() function waits for a command to complete
3195  * after it issues a command. This function wakes up that waiting routine by
3196  * calling wake_up() on the wait queue.
3197  */
3198 static void
3199 complete_cmd_in_sync_mode(struct mrsas_instance *instance,
3200     struct mrsas_cmd *cmd)
3201 {
3202         cmd->cmd_status = ddi_get8(cmd->frame_dma_obj.acc_handle,
3203             &cmd->frame->io.cmd_status);
3204 
3205         cmd->sync_cmd = MRSAS_FALSE;
3206 
3207         if (cmd->cmd_status == ENODATA) {
3208                 cmd->cmd_status = 0;
3209         }
3210 
3211         con_log(CL_ANN1, (CE_NOTE, "complete_cmd_in_sync_mode called %p \n",
3212             (void *)cmd));
3213 




3214         cv_broadcast(&instance->int_cmd_cv);


3215 }
3216 
3217 /*
3218  * Call this function inside mrsas_softintr.
3219  * mrsas_initiate_ocr_if_fw_is_faulty  - Initiates OCR if FW status is faulty
3220  * @instance:                   Adapter soft state
3221  */
3222 
3223 static uint32_t
3224 mrsas_initiate_ocr_if_fw_is_faulty(struct mrsas_instance *instance)
3225 {
3226         uint32_t        cur_abs_reg_val;
3227         uint32_t        fw_state;
3228 
3229         cur_abs_reg_val =  instance->func_ptr->read_fw_status_reg(instance);
3230         fw_state = cur_abs_reg_val & MFI_STATE_MASK;
3231         if (fw_state == MFI_STATE_FAULT) {
3232 
3233                 if (instance->disable_online_ctrl_reset == 1) {
3234                 con_log(CL_ANN1, (CE_NOTE,
3235                     "mrsas_initiate_ocr_if_fw_is_faulty: "
3236                     "FW in Fault state, detected in ISR: "
3237                     "FW doesn't support ocr "));

3238                 return (ADAPTER_RESET_NOT_REQUIRED);
3239                 } else {
3240                 con_log(CL_ANN1, (CE_NOTE,
3241                     "mrsas_initiate_ocr_if_fw_is_faulty: "
3242                     "FW in Fault state, detected in ISR: FW supports ocr "));

3243                         return (ADAPTER_RESET_REQUIRED);
3244                 }
3245         }

3246         return (ADAPTER_RESET_NOT_REQUIRED);
3247 }
3248 
3249 /*
3250  * mrsas_softintr - The Software ISR
3251  * @param arg   : HBA soft state
3252  *
3253  * called from high-level interrupt if hi-level interrupt are not there,
3254  * otherwise triggered as a soft interrupt
3255  */
3256 static uint_t
3257 mrsas_softintr(struct mrsas_instance *instance)
3258 {
3259         struct scsi_pkt         *pkt;
3260         struct scsa_cmd         *acmd;
3261         struct mrsas_cmd        *cmd;
3262         struct mlist_head       *pos, *next;
3263         mlist_t                 process_list;
3264         struct mrsas_header     *hdr;
3265         struct scsi_arq_status  *arqstat;
3266 
3267         con_log(CL_ANN1, (CE_CONT, "mrsas_softintr called"));
3268 
3269         ASSERT(instance);
3270 
3271         mutex_enter(&instance->completed_pool_mtx);
3272 
3273         if (mlist_empty(&instance->completed_pool_list)) {
3274                 mutex_exit(&instance->completed_pool_mtx);
3275                 return (DDI_INTR_CLAIMED);
3276         }
3277 
3278         instance->softint_running = 1;
3279 
3280         INIT_LIST_HEAD(&process_list);
3281         mlist_splice(&instance->completed_pool_list, &process_list);
3282         INIT_LIST_HEAD(&instance->completed_pool_list);
3283 
3284         mutex_exit(&instance->completed_pool_mtx);
3285 
3286         /* perform all callbacks first, before releasing the SCBs */
3287         mlist_for_each_safe(pos, next, &process_list) {


3324 
3325                         /* regular commands */
3326                         acmd =  cmd->cmd;
3327                         pkt =   CMD2PKT(acmd);
3328 
3329                         if (acmd->cmd_flags & CFLAG_DMAVALID) {
3330                                 if (acmd->cmd_flags & CFLAG_CONSISTENT) {
3331                                         (void) ddi_dma_sync(acmd->cmd_dmahandle,
3332                                             acmd->cmd_dma_offset,
3333                                             acmd->cmd_dma_len,
3334                                             DDI_DMA_SYNC_FORCPU);
3335                                 }
3336                         }
3337 
3338                         pkt->pkt_reason              = CMD_CMPLT;
3339                         pkt->pkt_statistics  = 0;
3340                         pkt->pkt_state = STATE_GOT_BUS
3341                             | STATE_GOT_TARGET | STATE_SENT_CMD
3342                             | STATE_XFERRED_DATA | STATE_GOT_STATUS;
3343 
3344                         con_log(CL_ANN1, (CE_CONT,
3345                             "CDB[0] = %x completed for %s: size %lx context %x",
3346                             pkt->pkt_cdbp[0], ((acmd->islogical) ? "LD" : "PD"),
3347                             acmd->cmd_dmacount, hdr->context));
3348                         DTRACE_PROBE3(softintr_cdb, uint8_t, pkt->pkt_cdbp[0],
3349                             uint_t, acmd->cmd_cdblen, ulong_t,
3350                             acmd->cmd_dmacount);
3351 
3352                         if (pkt->pkt_cdbp[0] == SCMD_INQUIRY) {
3353                                 struct scsi_inquiry     *inq;
3354 
3355                                 if (acmd->cmd_dmacount != 0) {
3356                                         bp_mapin(acmd->cmd_buf);
3357                                         inq = (struct scsi_inquiry *)
3358                                             acmd->cmd_buf->b_un.b_addr;
3359 
3360                                         /* don't expose physical drives to OS */
3361                                         if (acmd->islogical &&
3362                                             (hdr->cmd_status == MFI_STAT_OK)) {
3363                                                 display_scsi_inquiry(
3364                                                     (caddr_t)inq);


3377                         }
3378 
3379                         DTRACE_PROBE2(softintr_done, uint8_t, hdr->cmd,
3380                             uint8_t, hdr->cmd_status);
3381 
3382                         switch (hdr->cmd_status) {
3383                         case MFI_STAT_OK:
3384                                 pkt->pkt_scbp[0] = STATUS_GOOD;
3385                                 break;
3386                         case MFI_STAT_LD_CC_IN_PROGRESS:
3387                         case MFI_STAT_LD_RECON_IN_PROGRESS:
3388                                 pkt->pkt_scbp[0] = STATUS_GOOD;
3389                                 break;
3390                         case MFI_STAT_LD_INIT_IN_PROGRESS:
3391                                 con_log(CL_ANN,
3392                                     (CE_WARN, "Initialization in Progress"));
3393                                 pkt->pkt_reason      = CMD_TRAN_ERR;
3394 
3395                                 break;
3396                         case MFI_STAT_SCSI_DONE_WITH_ERROR:
3397                                 con_log(CL_ANN1, (CE_CONT, "scsi_done error"));
3398 
3399                                 pkt->pkt_reason      = CMD_CMPLT;
3400                                 ((struct scsi_status *)
3401                                     pkt->pkt_scbp)->sts_chk = 1;
3402 
3403                                 if (pkt->pkt_cdbp[0] == SCMD_TEST_UNIT_READY) {
3404 
3405                                         con_log(CL_ANN,
3406                                             (CE_WARN, "TEST_UNIT_READY fail"));
3407 
3408                                 } else {
3409                                         pkt->pkt_state |= STATE_ARQ_DONE;
3410                                         arqstat = (void *)(pkt->pkt_scbp);
3411                                         arqstat->sts_rqpkt_reason = CMD_CMPLT;
3412                                         arqstat->sts_rqpkt_resid = 0;
3413                                         arqstat->sts_rqpkt_state |=
3414                                             STATE_GOT_BUS | STATE_GOT_TARGET
3415                                             | STATE_SENT_CMD
3416                                             | STATE_XFERRED_DATA;
3417                                         *(uint8_t *)&arqstat->sts_rqpkt_status =
3418                                             STATUS_GOOD;
3419                                         ddi_rep_get8(
3420                                             cmd->frame_dma_obj.acc_handle,
3421                                             (uint8_t *)
3422                                             &(arqstat->sts_sensedata),
3423                                             cmd->sense,
3424                                             acmd->cmd_scblen -
3425                                             offsetof(struct scsi_arq_status,
3426                                             sts_sensedata), DDI_DEV_AUTOINCR);
3427                         }
3428                                 break;
3429                         case MFI_STAT_LD_OFFLINE:
3430                         case MFI_STAT_DEVICE_NOT_FOUND:
3431                                 con_log(CL_ANN1, (CE_CONT,
3432                                 "mrsas_softintr:device not found error"));
3433                                 pkt->pkt_reason      = CMD_DEV_GONE;
3434                                 pkt->pkt_statistics  = STAT_DISCON;
3435                                 break;
3436                         case MFI_STAT_LD_LBA_OUT_OF_RANGE:
3437                                 pkt->pkt_state |= STATE_ARQ_DONE;
3438                                 pkt->pkt_reason      = CMD_CMPLT;
3439                                 ((struct scsi_status *)
3440                                     pkt->pkt_scbp)->sts_chk = 1;
3441 
3442                                 arqstat = (void *)(pkt->pkt_scbp);
3443                                 arqstat->sts_rqpkt_reason = CMD_CMPLT;
3444                                 arqstat->sts_rqpkt_resid = 0;
3445                                 arqstat->sts_rqpkt_state |= STATE_GOT_BUS
3446                                     | STATE_GOT_TARGET | STATE_SENT_CMD
3447                                     | STATE_XFERRED_DATA;
3448                                 *(uint8_t *)&arqstat->sts_rqpkt_status =
3449                                     STATUS_GOOD;
3450 
3451                                 arqstat->sts_sensedata.es_valid = 1;


3471                         }
3472 
3473                         atomic_add_16(&instance->fw_outstanding, (-1));
3474 
3475                         (void) mrsas_common_check(instance, cmd);
3476 
3477                         if (acmd->cmd_dmahandle) {
3478                                 if (mrsas_check_dma_handle(
3479                                     acmd->cmd_dmahandle) != DDI_SUCCESS) {
3480                                         ddi_fm_service_impact(instance->dip,
3481                                             DDI_SERVICE_UNAFFECTED);
3482                                         pkt->pkt_reason = CMD_TRAN_ERR;
3483                                         pkt->pkt_statistics = 0;
3484                                 }
3485                         }
3486 
3487                         /* Call the callback routine */
3488                         if (((pkt->pkt_flags & FLAG_NOINTR) == 0) &&
3489                             pkt->pkt_comp) {
3490 
3491                                 con_log(CL_ANN1, (CE_NOTE, "mrsas_softintr: "
3492                                     "posting to scsa cmd %p index %x pkt %p "
3493                                     "time %llx", (void *)cmd, cmd->index,
3494                                     (void *)pkt, gethrtime()));
3495                                 (*pkt->pkt_comp)(pkt);
3496 
3497                         }

3498                         return_mfi_pkt(instance, cmd);
3499                         break;

3500                 case MFI_CMD_OP_SMP:
3501                 case MFI_CMD_OP_STP:
3502                         complete_cmd_in_sync_mode(instance, cmd);
3503                         break;

3504                 case MFI_CMD_OP_DCMD:
3505                         /* see if got an event notification */
3506                         if (ddi_get32(cmd->frame_dma_obj.acc_handle,
3507                             &cmd->frame->dcmd.opcode) ==
3508                             MR_DCMD_CTRL_EVENT_WAIT) {
3509                                 if ((instance->aen_cmd == cmd) &&
3510                                     (instance->aen_cmd->abort_aen)) {
3511                                         con_log(CL_ANN, (CE_WARN,
3512                                             "mrsas_softintr: "
3513                                             "aborted_aen returned"));
3514                                 } else {
3515                                         atomic_add_16(&instance->fw_outstanding,
3516                                             (-1));
3517                                         service_mfi_aen(instance, cmd);
3518                                 }
3519                         } else {
3520                                 complete_cmd_in_sync_mode(instance, cmd);
3521                         }
3522 
3523                         break;

3524                 case MFI_CMD_OP_ABORT:
3525                         con_log(CL_ANN, (CE_WARN, "MFI_CMD_OP_ABORT complete"));
3526                         /*
3527                          * MFI_CMD_OP_ABORT successfully completed
3528                          * in the synchronous mode
3529                          */
3530                         complete_cmd_in_sync_mode(instance, cmd);
3531                         break;

3532                 default:
3533                         mrsas_fm_ereport(instance, DDI_FM_DEVICE_NO_RESPONSE);
3534                         ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST);
3535 
3536                         if (cmd->pkt != NULL) {
3537                                 pkt = cmd->pkt;
3538                                 if (((pkt->pkt_flags & FLAG_NOINTR) == 0) &&
3539                                     pkt->pkt_comp) {
3540 
3541                                         con_log(CL_ANN1, (CE_CONT, "posting to "
3542                                             "scsa cmd %p index %x pkt %p"
3543                                             "time %llx, default ", (void *)cmd,
3544                                             cmd->index, (void *)pkt,
3545                                             gethrtime()));
3546 
3547                                         (*pkt->pkt_comp)(pkt);
3548 
3549                                 }
3550                         }
3551                         con_log(CL_ANN, (CE_WARN, "Cmd type unknown !"));
3552                         break;
3553                 }
3554         }
3555 
3556         instance->softint_running = 0;
3557 
3558         return (DDI_INTR_CLAIMED);
3559 }
3560 
3561 /*
3562  * mrsas_alloc_dma_obj
3563  *
3564  * Allocate the memory and other resources for an dma object.
3565  */
3566 static int
3567 mrsas_alloc_dma_obj(struct mrsas_instance *instance, dma_obj_t *obj,
3568     uchar_t endian_flags)
3569 {
3570         int     i;
3571         size_t  alen = 0;
3572         uint_t  cookie_cnt;
3573         struct ddi_device_acc_attr tmp_endian_attr;
3574 
3575         tmp_endian_attr = endian_attr;
3576         tmp_endian_attr.devacc_attr_endian_flags = endian_flags;
3577         tmp_endian_attr.devacc_attr_access = DDI_DEFAULT_ACC;
3578 
3579         i = ddi_dma_alloc_handle(instance->dip, &obj->dma_attr,
3580             DDI_DMA_SLEEP, NULL, &obj->dma_handle);
3581         if (i != DDI_SUCCESS) {
3582 
3583                 switch (i) {
3584                         case DDI_DMA_BADATTR :
3585                                 con_log(CL_ANN, (CE_WARN,
3586                                 "Failed ddi_dma_alloc_handle- Bad attribute"));


3625 
3626         if (mrsas_check_dma_handle(obj->dma_handle) != DDI_SUCCESS) {
3627                 ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST);
3628                 return (-1);
3629         }
3630 
3631         if (mrsas_check_acc_handle(obj->acc_handle) != DDI_SUCCESS) {
3632                 ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST);
3633                 return (-1);
3634         }
3635 
3636         return (cookie_cnt);
3637 }
3638 
3639 /*
3640  * mrsas_free_dma_obj(struct mrsas_instance *, dma_obj_t)
3641  *
3642  * De-allocate the memory and other resources for an dma object, which must
3643  * have been alloated by a previous call to mrsas_alloc_dma_obj()
3644  */
3645 static int
3646 mrsas_free_dma_obj(struct mrsas_instance *instance, dma_obj_t obj)
3647 {
3648 








3649         if (mrsas_check_dma_handle(obj.dma_handle) != DDI_SUCCESS) {
3650                 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED);
3651                 return (DDI_FAILURE);
3652         }
3653 
3654         if (mrsas_check_acc_handle(obj.acc_handle) != DDI_SUCCESS) {
3655                 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED);
3656                 return (DDI_FAILURE);
3657         }
3658 
3659         (void) ddi_dma_unbind_handle(obj.dma_handle);
3660         ddi_dma_mem_free(&obj.acc_handle);
3661         ddi_dma_free_handle(&obj.dma_handle);
3662 
3663         return (DDI_SUCCESS);
3664 }
3665 
3666 /*
3667  * mrsas_dma_alloc(instance_t *, struct scsi_pkt *, struct buf *,
3668  * int, int (*)())
3669  *
3670  * Allocate dma resources for a new scsi command
3671  */
3672 static int
3673 mrsas_dma_alloc(struct mrsas_instance *instance, struct scsi_pkt *pkt,
3674     struct buf *bp, int flags, int (*callback)())
3675 {
3676         int     dma_flags;
3677         int     (*cb)(caddr_t);
3678         int     i;
3679 
3680         ddi_dma_attr_t  tmp_dma_attr = mrsas_generic_dma_attr;
3681         struct scsa_cmd *acmd = PKT2CMD(pkt);
3682 
3683         acmd->cmd_buf = bp;
3684 
3685         if (bp->b_flags & B_READ) {
3686                 acmd->cmd_flags &= ~CFLAG_DMASEND;
3687                 dma_flags = DDI_DMA_READ;
3688         } else {
3689                 acmd->cmd_flags |= CFLAG_DMASEND;
3690                 dma_flags = DDI_DMA_WRITE;
3691         }
3692 
3693         if (flags & PKT_CONSISTENT) {
3694                 acmd->cmd_flags |= CFLAG_CONSISTENT;
3695                 dma_flags |= DDI_DMA_CONSISTENT;
3696         }
3697 
3698         if (flags & PKT_DMA_PARTIAL) {
3699                 dma_flags |= DDI_DMA_PARTIAL;
3700         }
3701 
3702         dma_flags |= DDI_DMA_REDZONE;
3703 
3704         cb = (callback == NULL_FUNC) ? DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
3705 
3706         tmp_dma_attr.dma_attr_sgllen = instance->max_num_sge;
3707         tmp_dma_attr.dma_attr_addr_hi = 0xffffffffffffffffull;







3708 
3709         if ((i = ddi_dma_alloc_handle(instance->dip, &tmp_dma_attr,
3710             cb, 0, &acmd->cmd_dmahandle)) != DDI_SUCCESS) {
3711                 switch (i) {
3712                 case DDI_DMA_BADATTR:
3713                         bioerror(bp, EFAULT);
3714                         return (DDI_FAILURE);
3715 
3716                 case DDI_DMA_NORESOURCES:
3717                         bioerror(bp, 0);
3718                         return (DDI_FAILURE);
3719 
3720                 default:
3721                         con_log(CL_ANN, (CE_PANIC, "ddi_dma_alloc_handle: "
3722                             "impossible result (0x%x)", i));
3723                         bioerror(bp, EFAULT);
3724                         return (DDI_FAILURE);
3725                 }
3726         }
3727 


3799                 break;
3800         default:
3801                 con_log(CL_ANN, (CE_PANIC, "ddi_dma_buf_bind_handle: "
3802                     "impossible result (0x%x)", i));
3803                 break;
3804         }
3805 
3806 no_dma_cookies:
3807         ddi_dma_free_handle(&acmd->cmd_dmahandle);
3808         acmd->cmd_dmahandle = NULL;
3809         acmd->cmd_flags &= ~CFLAG_DMAVALID;
3810         return (DDI_FAILURE);
3811 }
3812 
3813 /*
3814  * mrsas_dma_move(struct mrsas_instance *, struct scsi_pkt *, struct buf *)
3815  *
3816  * move dma resources to next dma window
3817  *
3818  */
3819 static int
3820 mrsas_dma_move(struct mrsas_instance *instance, struct scsi_pkt *pkt,
3821     struct buf *bp)
3822 {
3823         int     i = 0;
3824 
3825         struct scsa_cmd *acmd = PKT2CMD(pkt);
3826 
3827         /*
3828          * If there are no more cookies remaining in this window,
3829          * must move to the next window first.
3830          */
3831         if (acmd->cmd_cookie == acmd->cmd_ncookies) {
3832                 if (acmd->cmd_curwin == acmd->cmd_nwin && acmd->cmd_nwin == 1) {
3833                         return (DDI_SUCCESS);
3834                 }
3835 
3836                 /* at last window, cannot move */
3837                 if (++acmd->cmd_curwin >= acmd->cmd_nwin) {
3838                         return (DDI_FAILURE);
3839                 }


3871         if (bp->b_bcount >= acmd->cmd_dmacount) {
3872                 pkt->pkt_resid = bp->b_bcount - acmd->cmd_dmacount;
3873         } else {
3874                 pkt->pkt_resid = 0;
3875         }
3876 
3877         return (DDI_SUCCESS);
3878 }
3879 
3880 /*
3881  * build_cmd
3882  */
3883 static struct mrsas_cmd *
3884 build_cmd(struct mrsas_instance *instance, struct scsi_address *ap,
3885     struct scsi_pkt *pkt, uchar_t *cmd_done)
3886 {
3887         uint16_t        flags = 0;
3888         uint32_t        i;
3889         uint32_t        context;
3890         uint32_t        sge_bytes;

3891         ddi_acc_handle_t acc_handle;
3892         struct mrsas_cmd                *cmd;
3893         struct mrsas_sge64              *mfi_sgl;
3894         struct mrsas_sge_ieee           *mfi_sgl_ieee;
3895         struct scsa_cmd                 *acmd = PKT2CMD(pkt);
3896         struct mrsas_pthru_frame        *pthru;
3897         struct mrsas_io_frame           *ldio;
3898 
3899         /* find out if this is logical or physical drive command.  */
3900         acmd->islogical = MRDRV_IS_LOGICAL(ap);
3901         acmd->device_id = MAP_DEVICE_ID(instance, ap);
3902         *cmd_done = 0;
3903 
3904         /* get the command packet */
3905         if (!(cmd = get_mfi_pkt(instance))) {
3906                 DTRACE_PROBE2(build_cmd_mfi_err, uint16_t,
3907                     instance->fw_outstanding, uint16_t, instance->max_fw_cmds);
3908                 return (NULL);
3909         }
3910 
3911         cmd->retry_count_for_ocr = 0;
3912 
3913         acc_handle = cmd->frame_dma_obj.acc_handle;
3914 
3915         /* Clear the frame buffer and assign back the context id */
3916         (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
3917         ddi_put32(acc_handle, &cmd->frame->hdr.context, cmd->index);
3918 
3919         cmd->pkt = pkt;
3920         cmd->cmd = acmd;
3921         DTRACE_PROBE3(build_cmds, uint8_t, pkt->pkt_cdbp[0],
3922             ulong_t, acmd->cmd_dmacount, ulong_t, acmd->cmd_dma_len);
3923 
3924         /* lets get the command directions */
3925         if (acmd->cmd_flags & CFLAG_DMASEND) {
3926                 flags = MFI_FRAME_DIR_WRITE;
3927 
3928                 if (acmd->cmd_flags & CFLAG_CONSISTENT) {
3929                         (void) ddi_dma_sync(acmd->cmd_dmahandle,
3930                             acmd->cmd_dma_offset, acmd->cmd_dma_len,
3931                             DDI_DMA_SYNC_FORDEV);
3932                 }


3945         if (instance->flag_ieee) {
3946                 flags |= MFI_FRAME_IEEE;
3947         }
3948         flags |= MFI_FRAME_SGL64;
3949 
3950         switch (pkt->pkt_cdbp[0]) {
3951 
3952         /*
3953          * case SCMD_SYNCHRONIZE_CACHE:
3954          *      flush_cache(instance);
3955          *      return_mfi_pkt(instance, cmd);
3956          *      *cmd_done = 1;
3957          *
3958          *      return (NULL);
3959          */
3960 
3961         case SCMD_READ:
3962         case SCMD_WRITE:
3963         case SCMD_READ_G1:
3964         case SCMD_WRITE_G1:




3965                 if (acmd->islogical) {
3966                         ldio = (struct mrsas_io_frame *)cmd->frame;
3967 
3968                         /*
3969                          * preare the Logical IO frame:
3970                          * 2nd bit is zero for all read cmds
3971                          */
3972                         ddi_put8(acc_handle, &ldio->cmd,
3973                             (pkt->pkt_cdbp[0] & 0x02) ? MFI_CMD_OP_LD_WRITE
3974                             : MFI_CMD_OP_LD_READ);
3975                         ddi_put8(acc_handle, &ldio->cmd_status, 0x0);
3976                         ddi_put8(acc_handle, &ldio->scsi_status, 0x0);
3977                         ddi_put8(acc_handle, &ldio->target_id, acmd->device_id);
3978                         ddi_put16(acc_handle, &ldio->timeout, 0);
3979                         ddi_put8(acc_handle, &ldio->reserved_0, 0);
3980                         ddi_put16(acc_handle, &ldio->pad_0, 0);
3981                         ddi_put16(acc_handle, &ldio->flags, flags);
3982 
3983                         /* Initialize sense Information */
3984                         bzero(cmd->sense, SENSE_LENGTH);
3985                         ddi_put8(acc_handle, &ldio->sense_len, SENSE_LENGTH);
3986                         ddi_put32(acc_handle, &ldio->sense_buf_phys_addr_hi, 0);
3987                         ddi_put32(acc_handle, &ldio->sense_buf_phys_addr_lo,
3988                             cmd->sense_phys_addr);
3989                         ddi_put32(acc_handle, &ldio->start_lba_hi, 0);
3990                         ddi_put8(acc_handle, &ldio->access_byte,
3991                             (acmd->cmd_cdblen != 6) ? pkt->pkt_cdbp[1] : 0);
3992                         ddi_put8(acc_handle, &ldio->sge_count,
3993                             acmd->cmd_cookiecnt);
3994                         if (instance->flag_ieee) {
3995                                 mfi_sgl_ieee =
3996                                     (struct mrsas_sge_ieee *)&ldio->sgl;
3997                         } else {
3998                                 mfi_sgl = (struct mrsas_sge64   *)&ldio->sgl;
3999                         }
4000 
4001                         context = ddi_get32(acc_handle, &ldio->context);
4002 
4003                         if (acmd->cmd_cdblen == CDB_GROUP0) {

4004                                 ddi_put32(acc_handle, &ldio->lba_count, (
4005                                     (uint16_t)(pkt->pkt_cdbp[4])));
4006 
4007                                 ddi_put32(acc_handle, &ldio->start_lba_lo, (
4008                                     ((uint32_t)(pkt->pkt_cdbp[3])) |
4009                                     ((uint32_t)(pkt->pkt_cdbp[2]) << 8) |
4010                                     ((uint32_t)((pkt->pkt_cdbp[1]) & 0x1F)
4011                                     << 16)));
4012                         } else if (acmd->cmd_cdblen == CDB_GROUP1) {

4013                                 ddi_put32(acc_handle, &ldio->lba_count, (
4014                                     ((uint16_t)(pkt->pkt_cdbp[8])) |
4015                                     ((uint16_t)(pkt->pkt_cdbp[7]) << 8)));
4016 
4017                                 ddi_put32(acc_handle, &ldio->start_lba_lo, (
4018                                     ((uint32_t)(pkt->pkt_cdbp[5])) |
4019                                     ((uint32_t)(pkt->pkt_cdbp[4]) << 8) |
4020                                     ((uint32_t)(pkt->pkt_cdbp[3]) << 16) |
4021                                     ((uint32_t)(pkt->pkt_cdbp[2]) << 24)));
4022                         } else if (acmd->cmd_cdblen == CDB_GROUP2) {

4023                                 ddi_put32(acc_handle, &ldio->lba_count, (
4024                                     ((uint16_t)(pkt->pkt_cdbp[9])) |
4025                                     ((uint16_t)(pkt->pkt_cdbp[8]) << 8) |
4026                                     ((uint16_t)(pkt->pkt_cdbp[7]) << 16) |
4027                                     ((uint16_t)(pkt->pkt_cdbp[6]) << 24)));
4028 
4029                                 ddi_put32(acc_handle, &ldio->start_lba_lo, (
4030                                     ((uint32_t)(pkt->pkt_cdbp[5])) |
4031                                     ((uint32_t)(pkt->pkt_cdbp[4]) << 8) |
4032                                     ((uint32_t)(pkt->pkt_cdbp[3]) << 16) |
4033                                     ((uint32_t)(pkt->pkt_cdbp[2]) << 24)));
4034                         } else if (acmd->cmd_cdblen == CDB_GROUP3) {

4035                                 ddi_put32(acc_handle, &ldio->lba_count, (
4036                                     ((uint16_t)(pkt->pkt_cdbp[13])) |
4037                                     ((uint16_t)(pkt->pkt_cdbp[12]) << 8) |
4038                                     ((uint16_t)(pkt->pkt_cdbp[11]) << 16) |
4039                                     ((uint16_t)(pkt->pkt_cdbp[10]) << 24)));
4040 
4041                                 ddi_put32(acc_handle, &ldio->start_lba_lo, (
4042                                     ((uint32_t)(pkt->pkt_cdbp[9])) |
4043                                     ((uint32_t)(pkt->pkt_cdbp[8]) << 8) |
4044                                     ((uint32_t)(pkt->pkt_cdbp[7]) << 16) |
4045                                     ((uint32_t)(pkt->pkt_cdbp[6]) << 24)));
4046 
4047                                 ddi_put32(acc_handle, &ldio->start_lba_lo, (
4048                                     ((uint32_t)(pkt->pkt_cdbp[5])) |
4049                                     ((uint32_t)(pkt->pkt_cdbp[4]) << 8) |
4050                                     ((uint32_t)(pkt->pkt_cdbp[3]) << 16) |
4051                                     ((uint32_t)(pkt->pkt_cdbp[2]) << 24)));
4052                         }
4053 
4054                         break;
4055                 }
4056                 /* fall through For all non-rd/wr cmds */
4057         default:
4058 
4059                 switch (pkt->pkt_cdbp[0]) {
4060                 case SCMD_MODE_SENSE:
4061                 case SCMD_MODE_SENSE_G1: {
4062                         union scsi_cdb  *cdbp;
4063                         uint16_t        page_code;
4064 
4065                         cdbp = (void *)pkt->pkt_cdbp;
4066                         page_code = (uint16_t)cdbp->cdb_un.sg.scsi[0];
4067                         switch (page_code) {


4073                                 return (NULL);
4074                         }
4075                         break;
4076                 }
4077                 default:
4078                         break;
4079                 }
4080 
4081                 pthru   = (struct mrsas_pthru_frame *)cmd->frame;
4082 
4083                 /* prepare the DCDB frame */
4084                 ddi_put8(acc_handle, &pthru->cmd, (acmd->islogical) ?
4085                     MFI_CMD_OP_LD_SCSI : MFI_CMD_OP_PD_SCSI);
4086                 ddi_put8(acc_handle, &pthru->cmd_status, 0x0);
4087                 ddi_put8(acc_handle, &pthru->scsi_status, 0x0);
4088                 ddi_put8(acc_handle, &pthru->target_id, acmd->device_id);
4089                 ddi_put8(acc_handle, &pthru->lun, 0);
4090                 ddi_put8(acc_handle, &pthru->cdb_len, acmd->cmd_cdblen);
4091                 ddi_put16(acc_handle, &pthru->timeout, 0);
4092                 ddi_put16(acc_handle, &pthru->flags, flags);




4093                 ddi_put32(acc_handle, &pthru->data_xfer_len,
4094                     acmd->cmd_dmacount);
4095                 ddi_put8(acc_handle, &pthru->sge_count, acmd->cmd_cookiecnt);
4096                 if (instance->flag_ieee) {
4097                         mfi_sgl_ieee = (struct mrsas_sge_ieee *)&pthru->sgl;
4098                 } else {
4099                         mfi_sgl = (struct mrsas_sge64 *)&pthru->sgl;
4100                 }
4101 
4102                 bzero(cmd->sense, SENSE_LENGTH);
4103                 ddi_put8(acc_handle, &pthru->sense_len, SENSE_LENGTH);
4104                 ddi_put32(acc_handle, &pthru->sense_buf_phys_addr_hi, 0);
4105                 ddi_put32(acc_handle, &pthru->sense_buf_phys_addr_lo,
4106                     cmd->sense_phys_addr);
4107 
4108                 context = ddi_get32(acc_handle, &pthru->context);
4109                 ddi_rep_put8(acc_handle, (uint8_t *)pkt->pkt_cdbp,
4110                     (uint8_t *)pthru->cdb, acmd->cmd_cdblen, DDI_DEV_AUTOINCR);
4111 
4112                 break;
4113         }
4114 #ifdef lint


4125                 sge_bytes = sizeof (struct mrsas_sge_ieee)*acmd->cmd_cookiecnt;
4126         } else {
4127                 for (i = 0; i < acmd->cmd_cookiecnt; i++, mfi_sgl++) {
4128                         ddi_put64(acc_handle, &mfi_sgl->phys_addr,
4129                             acmd->cmd_dmacookies[i].dmac_laddress);
4130                         ddi_put32(acc_handle, &mfi_sgl->length,
4131                             acmd->cmd_dmacookies[i].dmac_size);
4132                 }
4133                 sge_bytes = sizeof (struct mrsas_sge64)*acmd->cmd_cookiecnt;
4134         }
4135 
4136         cmd->frame_count = (sge_bytes / MRMFI_FRAME_SIZE) +
4137             ((sge_bytes % MRMFI_FRAME_SIZE) ? 1 : 0) + 1;
4138 
4139         if (cmd->frame_count >= 8) {
4140                 cmd->frame_count = 8;
4141         }
4142 
4143         return (cmd);
4144 }

4145 #ifndef __sparc








4146 static int
4147 wait_for_outstanding(struct mrsas_instance *instance)
4148 {
4149         int             i;
4150         uint32_t        wait_time = 90;
4151 
4152         for (i = 0; i < wait_time; i++) {
4153                 if (!instance->fw_outstanding) {
4154                         break;
4155                 }

4156                 drv_usecwait(MILLISEC); /* wait for 1000 usecs */;
4157         }
4158 
4159         if (instance->fw_outstanding) {
4160                 return (1);
4161         }
4162 
4163         return (0);
4164 }
4165 #endif  /* __sparc */

4166 /*
4167  * issue_mfi_pthru
4168  */
4169 static int
4170 issue_mfi_pthru(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl,
4171     struct mrsas_cmd *cmd, int mode)
4172 {
4173         void            *ubuf;
4174         uint32_t        kphys_addr = 0;
4175         uint32_t        xferlen = 0;

4176         uint_t          model;
4177         ddi_acc_handle_t        acc_handle = cmd->frame_dma_obj.acc_handle;
4178         dma_obj_t                       pthru_dma_obj;
4179         struct mrsas_pthru_frame        *kpthru;
4180         struct mrsas_pthru_frame        *pthru;
4181         int i;
4182         pthru = &cmd->frame->pthru;
4183         kpthru = (struct mrsas_pthru_frame *)&ioctl->frame[0];
4184 
4185         if (instance->adapterresetinprogress) {
4186                 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_pthru: Reset flag set, "
4187                 "returning mfi_pkt and setting TRAN_BUSY\n"));
4188                 return (DDI_FAILURE);
4189         }
4190         model = ddi_model_convert_from(mode & FMODELS);
4191         if (model == DDI_MODEL_ILP32) {
4192                 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_pthru: DDI_MODEL_LP32"));
4193 
4194                 xferlen = kpthru->sgl.sge32[0].length;
4195 
4196                 ubuf    = (void *)(ulong_t)kpthru->sgl.sge32[0].phys_addr;
4197         } else {
4198 #ifdef _ILP32
4199                 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_pthru: DDI_MODEL_LP32"));
4200                 xferlen = kpthru->sgl.sge32[0].length;
4201                 ubuf    = (void *)(ulong_t)kpthru->sgl.sge32[0].phys_addr;
4202 #else
4203                 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_pthru: DDI_MODEL_LP64"));
4204                 xferlen = kpthru->sgl.sge64[0].length;
4205                 ubuf    = (void *)(ulong_t)kpthru->sgl.sge64[0].phys_addr;
4206 #endif
4207         }
4208 
4209         if (xferlen) {
4210                 /* means IOCTL requires DMA */
4211                 /* allocate the data transfer buffer */
4212                 pthru_dma_obj.size = xferlen;



4213                 pthru_dma_obj.dma_attr = mrsas_generic_dma_attr;
4214                 pthru_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
4215                 pthru_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
4216                 pthru_dma_obj.dma_attr.dma_attr_sgllen = 1;
4217                 pthru_dma_obj.dma_attr.dma_attr_align = 1;
4218 
4219                 /* allocate kernel buffer for DMA */
4220                 if (mrsas_alloc_dma_obj(instance, &pthru_dma_obj,
4221                     (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
4222                         con_log(CL_ANN, (CE_WARN, "issue_mfi_pthru: "
4223                             "could not allocate data transfer buffer."));
4224                         return (DDI_FAILURE);
4225                 }
4226                 (void) memset(pthru_dma_obj.buffer, 0, xferlen);
4227 
4228                 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */
4229                 if (kpthru->flags & MFI_FRAME_DIR_WRITE) {
4230                         for (i = 0; i < xferlen; i++) {
4231                                 if (ddi_copyin((uint8_t *)ubuf+i,
4232                                     (uint8_t *)pthru_dma_obj.buffer+i,
4233                                     1, mode)) {
4234                                         con_log(CL_ANN, (CE_WARN,
4235                                             "issue_mfi_pthru : "
4236                                             "copy from user space failed"));
4237                                         return (DDI_FAILURE);
4238                                 }
4239                         }
4240                 }
4241 
4242                 kphys_addr = pthru_dma_obj.dma_cookie[0].dmac_address;
4243         }
4244 
4245         ddi_put8(acc_handle, &pthru->cmd, kpthru->cmd);
4246         ddi_put8(acc_handle, &pthru->sense_len, 0);
4247         ddi_put8(acc_handle, &pthru->cmd_status, 0);
4248         ddi_put8(acc_handle, &pthru->scsi_status, 0);
4249         ddi_put8(acc_handle, &pthru->target_id, kpthru->target_id);
4250         ddi_put8(acc_handle, &pthru->lun, kpthru->lun);
4251         ddi_put8(acc_handle, &pthru->cdb_len, kpthru->cdb_len);
4252         ddi_put8(acc_handle, &pthru->sge_count, kpthru->sge_count);
4253         ddi_put16(acc_handle, &pthru->timeout, kpthru->timeout);
4254         ddi_put32(acc_handle, &pthru->data_xfer_len, kpthru->data_xfer_len);
4255 
4256         ddi_put32(acc_handle, &pthru->sense_buf_phys_addr_hi, 0);
4257         /* pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr; */
4258         ddi_put32(acc_handle, &pthru->sense_buf_phys_addr_lo, 0);
4259 
4260         ddi_rep_put8(acc_handle, (uint8_t *)kpthru->cdb, (uint8_t *)pthru->cdb,
4261             pthru->cdb_len, DDI_DEV_AUTOINCR);
4262 
4263         ddi_put16(acc_handle, &pthru->flags, kpthru->flags & ~MFI_FRAME_SGL64);
4264         ddi_put32(acc_handle, &pthru->sgl.sge32[0].length, xferlen);
4265         ddi_put32(acc_handle, &pthru->sgl.sge32[0].phys_addr, kphys_addr);
4266 
4267         cmd->sync_cmd = MRSAS_TRUE;
4268         cmd->frame_count = 1;
4269 




4270         if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) {
4271                 con_log(CL_ANN, (CE_WARN,
4272                     "issue_mfi_pthru: fw_ioctl failed"));
4273         } else {
4274                 if (xferlen && kpthru->flags & MFI_FRAME_DIR_READ) {
4275                         for (i = 0; i < xferlen; i++) {
4276                                 if (ddi_copyout(
4277                                     (uint8_t *)pthru_dma_obj.buffer+i,
4278                                     (uint8_t *)ubuf+i, 1, mode)) {
4279                                         con_log(CL_ANN, (CE_WARN,
4280                                             "issue_mfi_pthru : "
4281                                             "copy to user space failed"));
4282                                         return (DDI_FAILURE);
4283                                 }
4284                         }
4285                 }
4286         }
4287 
4288         kpthru->cmd_status = ddi_get8(acc_handle, &pthru->cmd_status);
4289         kpthru->scsi_status = ddi_get8(acc_handle, &pthru->scsi_status);
4290 
4291         con_log(CL_ANN, (CE_NOTE, "issue_mfi_pthru: cmd_status %x, "
4292             "scsi_status %x", kpthru->cmd_status, kpthru->scsi_status));
4293         DTRACE_PROBE3(issue_pthru, uint8_t, kpthru->cmd, uint8_t,
4294             kpthru->cmd_status, uint8_t, kpthru->scsi_status);
4295 
























4296         if (xferlen) {
4297                 /* free kernel buffer */
4298                 if (mrsas_free_dma_obj(instance, pthru_dma_obj) != DDI_SUCCESS)
4299                         return (DDI_FAILURE);
4300         }
4301 
4302         return (DDI_SUCCESS);
4303 }
4304 
4305 /*
4306  * issue_mfi_dcmd
4307  */
4308 static int
4309 issue_mfi_dcmd(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl,
4310     struct mrsas_cmd *cmd, int mode)
4311 {
4312         void            *ubuf;
4313         uint32_t        kphys_addr = 0;
4314         uint32_t        xferlen = 0;

4315         uint32_t        model;
4316         dma_obj_t       dcmd_dma_obj;
4317         struct mrsas_dcmd_frame *kdcmd;
4318         struct mrsas_dcmd_frame *dcmd;
4319         ddi_acc_handle_t        acc_handle = cmd->frame_dma_obj.acc_handle;
4320         int i;
4321         dcmd = &cmd->frame->dcmd;
4322         kdcmd = (struct mrsas_dcmd_frame *)&ioctl->frame[0];

4323         if (instance->adapterresetinprogress) {
4324                 con_log(CL_ANN1, (CE_NOTE, "Reset flag set, "
4325                 "returning mfi_pkt and setting TRAN_BUSY\n"));
4326                 return (DDI_FAILURE);
4327         }
4328         model = ddi_model_convert_from(mode & FMODELS);
4329         if (model == DDI_MODEL_ILP32) {
4330                 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_dcmd: DDI_MODEL_ILP32"));
4331 
4332                 xferlen = kdcmd->sgl.sge32[0].length;
4333 
4334                 ubuf    = (void *)(ulong_t)kdcmd->sgl.sge32[0].phys_addr;
4335         } else {
4336 #ifdef _ILP32
4337                 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_dcmd: DDI_MODEL_ILP32"));
4338                 xferlen = kdcmd->sgl.sge32[0].length;
4339                 ubuf    = (void *)(ulong_t)kdcmd->sgl.sge32[0].phys_addr;
4340 #else
4341                 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_dcmd: DDI_MODEL_LP64"));
4342                 xferlen = kdcmd->sgl.sge64[0].length;
4343                 ubuf    = (void *)(ulong_t)kdcmd->sgl.sge64[0].phys_addr;
4344 #endif
4345         }
4346         if (xferlen) {
4347                 /* means IOCTL requires DMA */
4348                 /* allocate the data transfer buffer */
4349                 dcmd_dma_obj.size = xferlen;



4350                 dcmd_dma_obj.dma_attr = mrsas_generic_dma_attr;
4351                 dcmd_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
4352                 dcmd_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
4353                 dcmd_dma_obj.dma_attr.dma_attr_sgllen = 1;
4354                 dcmd_dma_obj.dma_attr.dma_attr_align = 1;
4355 
4356                 /* allocate kernel buffer for DMA */
4357                 if (mrsas_alloc_dma_obj(instance, &dcmd_dma_obj,
4358                     (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
4359                         con_log(CL_ANN, (CE_WARN, "issue_mfi_dcmd: "
4360                             "could not allocate data transfer buffer."));

4361                         return (DDI_FAILURE);
4362                 }
4363                 (void) memset(dcmd_dma_obj.buffer, 0, xferlen);
4364 
4365                 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */
4366                 if (kdcmd->flags & MFI_FRAME_DIR_WRITE) {
4367                         for (i = 0; i < xferlen; i++) {
4368                                 if (ddi_copyin((uint8_t *)ubuf + i,
4369                                     (uint8_t *)dcmd_dma_obj.buffer + i,
4370                                     1, mode)) {
4371                                         con_log(CL_ANN, (CE_WARN,
4372                                             "issue_mfi_dcmd : "
4373                                             "copy from user space failed"));
4374                                         return (DDI_FAILURE);
4375                                 }
4376                         }
4377                 }
4378 
4379                 kphys_addr = dcmd_dma_obj.dma_cookie[0].dmac_address;
4380         }
4381 
4382         ddi_put8(acc_handle, &dcmd->cmd, kdcmd->cmd);
4383         ddi_put8(acc_handle, &dcmd->cmd_status, 0);
4384         ddi_put8(acc_handle, &dcmd->sge_count, kdcmd->sge_count);
4385         ddi_put16(acc_handle, &dcmd->timeout, kdcmd->timeout);
4386         ddi_put32(acc_handle, &dcmd->data_xfer_len, kdcmd->data_xfer_len);
4387         ddi_put32(acc_handle, &dcmd->opcode, kdcmd->opcode);
4388 
4389         ddi_rep_put8(acc_handle, (uint8_t *)kdcmd->mbox.b,
4390             (uint8_t *)dcmd->mbox.b, DCMD_MBOX_SZ, DDI_DEV_AUTOINCR);
4391 
4392         ddi_put16(acc_handle, &dcmd->flags, kdcmd->flags & ~MFI_FRAME_SGL64);
4393         ddi_put32(acc_handle, &dcmd->sgl.sge32[0].length, xferlen);
4394         ddi_put32(acc_handle, &dcmd->sgl.sge32[0].phys_addr, kphys_addr);
4395 
4396         cmd->sync_cmd = MRSAS_TRUE;
4397         cmd->frame_count = 1;
4398 




4399         if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) {
4400                 con_log(CL_ANN, (CE_WARN, "issue_mfi_dcmd: fw_ioctl failed"));
4401         } else {
4402                 if (xferlen && (kdcmd->flags & MFI_FRAME_DIR_READ)) {
4403                         for (i = 0; i < xferlen; i++) {
4404                                 if (ddi_copyout(
4405                                     (uint8_t *)dcmd_dma_obj.buffer + i,
4406                                     (uint8_t *)ubuf + i,
4407                                     1, mode)) {
4408                                         con_log(CL_ANN, (CE_WARN,
4409                                             "issue_mfi_dcmd : "
4410                                             "copy to user space failed"));
4411                                         return (DDI_FAILURE);
4412                                 }
4413                         }
4414                 }
4415         }
4416 
4417         kdcmd->cmd_status = ddi_get8(acc_handle, &dcmd->cmd_status);


4418         DTRACE_PROBE3(issue_dcmd, uint32_t, kdcmd->opcode, uint8_t,
4419             kdcmd->cmd, uint8_t, kdcmd->cmd_status);
4420 
4421         if (xferlen) {
4422                 /* free kernel buffer */
4423                 if (mrsas_free_dma_obj(instance, dcmd_dma_obj) != DDI_SUCCESS)
4424                         return (DDI_FAILURE);
4425         }
4426 
4427         return (DDI_SUCCESS);
4428 }
4429 
4430 /*
4431  * issue_mfi_smp
4432  */
4433 static int
4434 issue_mfi_smp(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl,
4435     struct mrsas_cmd *cmd, int mode)
4436 {
4437         void            *request_ubuf;
4438         void            *response_ubuf;
4439         uint32_t        request_xferlen = 0;
4440         uint32_t        response_xferlen = 0;


4441         uint_t          model;
4442         dma_obj_t                       request_dma_obj;
4443         dma_obj_t                       response_dma_obj;
4444         ddi_acc_handle_t        acc_handle = cmd->frame_dma_obj.acc_handle;
4445         struct mrsas_smp_frame          *ksmp;
4446         struct mrsas_smp_frame          *smp;
4447         struct mrsas_sge32              *sge32;
4448 #ifndef _ILP32
4449         struct mrsas_sge64              *sge64;
4450 #endif
4451         int i;
4452         uint64_t                        tmp_sas_addr;
4453 
4454         smp = &cmd->frame->smp;
4455         ksmp = (struct mrsas_smp_frame *)&ioctl->frame[0];
4456 
4457         if (instance->adapterresetinprogress) {
4458                 con_log(CL_ANN1, (CE_NOTE, "Reset flag set, "
4459                 "returning mfi_pkt and setting TRAN_BUSY\n"));
4460                 return (DDI_FAILURE);
4461         }
4462         model = ddi_model_convert_from(mode & FMODELS);
4463         if (model == DDI_MODEL_ILP32) {
4464                 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: DDI_MODEL_ILP32"));
4465 
4466                 sge32                   = &ksmp->sgl[0].sge32[0];
4467                 response_xferlen        = sge32[0].length;
4468                 request_xferlen         = sge32[1].length;
4469                 con_log(CL_ANN, (CE_NOTE, "issue_mfi_smp: "
4470                     "response_xferlen = %x, request_xferlen = %x",
4471                     response_xferlen, request_xferlen));
4472 
4473                 response_ubuf   = (void *)(ulong_t)sge32[0].phys_addr;
4474                 request_ubuf    = (void *)(ulong_t)sge32[1].phys_addr;
4475                 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: "
4476                     "response_ubuf = %p, request_ubuf = %p",
4477                     response_ubuf, request_ubuf));
4478         } else {
4479 #ifdef _ILP32
4480                 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: DDI_MODEL_ILP32"));
4481 
4482                 sge32                   = &ksmp->sgl[0].sge32[0];
4483                 response_xferlen        = sge32[0].length;
4484                 request_xferlen         = sge32[1].length;
4485                 con_log(CL_ANN, (CE_NOTE, "issue_mfi_smp: "
4486                     "response_xferlen = %x, request_xferlen = %x",
4487                     response_xferlen, request_xferlen));
4488 
4489                 response_ubuf   = (void *)(ulong_t)sge32[0].phys_addr;
4490                 request_ubuf    = (void *)(ulong_t)sge32[1].phys_addr;
4491                 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: "
4492                     "response_ubuf = %p, request_ubuf = %p",
4493                     response_ubuf, request_ubuf));
4494 #else
4495                 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: DDI_MODEL_LP64"));
4496 
4497                 sge64                   = &ksmp->sgl[0].sge64[0];
4498                 response_xferlen        = sge64[0].length;
4499                 request_xferlen         = sge64[1].length;
4500 
4501                 response_ubuf   = (void *)(ulong_t)sge64[0].phys_addr;
4502                 request_ubuf    = (void *)(ulong_t)sge64[1].phys_addr;
4503 #endif
4504         }
4505         if (request_xferlen) {
4506                 /* means IOCTL requires DMA */
4507                 /* allocate the data transfer buffer */
4508                 request_dma_obj.size = request_xferlen;



4509                 request_dma_obj.dma_attr = mrsas_generic_dma_attr;
4510                 request_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
4511                 request_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
4512                 request_dma_obj.dma_attr.dma_attr_sgllen = 1;
4513                 request_dma_obj.dma_attr.dma_attr_align = 1;
4514 
4515                 /* allocate kernel buffer for DMA */
4516                 if (mrsas_alloc_dma_obj(instance, &request_dma_obj,
4517                     (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
4518                         con_log(CL_ANN, (CE_WARN, "issue_mfi_smp: "
4519                             "could not allocate data transfer buffer."));
4520                         return (DDI_FAILURE);
4521                 }
4522                 (void) memset(request_dma_obj.buffer, 0, request_xferlen);
4523 
4524                 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */
4525                 for (i = 0; i < request_xferlen; i++) {
4526                         if (ddi_copyin((uint8_t *)request_ubuf + i,
4527                             (uint8_t *)request_dma_obj.buffer + i,
4528                             1, mode)) {
4529                                 con_log(CL_ANN, (CE_WARN, "issue_mfi_smp: "
4530                                     "copy from user space failed"));
4531                                 return (DDI_FAILURE);
4532                         }
4533                 }
4534         }
4535 
4536         if (response_xferlen) {
4537                 /* means IOCTL requires DMA */
4538                 /* allocate the data transfer buffer */
4539                 response_dma_obj.size = response_xferlen;



4540                 response_dma_obj.dma_attr = mrsas_generic_dma_attr;
4541                 response_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
4542                 response_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
4543                 response_dma_obj.dma_attr.dma_attr_sgllen = 1;
4544                 response_dma_obj.dma_attr.dma_attr_align = 1;
4545 
4546                 /* allocate kernel buffer for DMA */
4547                 if (mrsas_alloc_dma_obj(instance, &response_dma_obj,
4548                     (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
4549                         con_log(CL_ANN, (CE_WARN, "issue_mfi_smp: "
4550                             "could not allocate data transfer buffer."));
4551                         return (DDI_FAILURE);
4552                 }
4553                 (void) memset(response_dma_obj.buffer, 0, response_xferlen);
4554 
4555                 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */
4556                 for (i = 0; i < response_xferlen; i++) {
4557                         if (ddi_copyin((uint8_t *)response_ubuf + i,
4558                             (uint8_t *)response_dma_obj.buffer + i,
4559                             1, mode)) {


4563                         }
4564                 }
4565         }
4566 
4567         ddi_put8(acc_handle, &smp->cmd, ksmp->cmd);
4568         ddi_put8(acc_handle, &smp->cmd_status, 0);
4569         ddi_put8(acc_handle, &smp->connection_status, 0);
4570         ddi_put8(acc_handle, &smp->sge_count, ksmp->sge_count);
4571         /* smp->context              = ksmp->context; */
4572         ddi_put16(acc_handle, &smp->timeout, ksmp->timeout);
4573         ddi_put32(acc_handle, &smp->data_xfer_len, ksmp->data_xfer_len);
4574 
4575         bcopy((void *)&ksmp->sas_addr, (void *)&tmp_sas_addr,
4576             sizeof (uint64_t));
4577         ddi_put64(acc_handle, &smp->sas_addr, tmp_sas_addr);
4578 
4579         ddi_put16(acc_handle, &smp->flags, ksmp->flags & ~MFI_FRAME_SGL64);
4580 
4581         model = ddi_model_convert_from(mode & FMODELS);
4582         if (model == DDI_MODEL_ILP32) {
4583                 con_log(CL_ANN1, (CE_NOTE,
4584                     "issue_mfi_smp: DDI_MODEL_ILP32"));
4585 
4586                 sge32 = &smp->sgl[0].sge32[0];
4587                 ddi_put32(acc_handle, &sge32[0].length, response_xferlen);
4588                 ddi_put32(acc_handle, &sge32[0].phys_addr,
4589                     response_dma_obj.dma_cookie[0].dmac_address);
4590                 ddi_put32(acc_handle, &sge32[1].length, request_xferlen);
4591                 ddi_put32(acc_handle, &sge32[1].phys_addr,
4592                     request_dma_obj.dma_cookie[0].dmac_address);
4593         } else {
4594 #ifdef _ILP32
4595                 con_log(CL_ANN1, (CE_NOTE,
4596                     "issue_mfi_smp: DDI_MODEL_ILP32"));
4597                 sge32 = &smp->sgl[0].sge32[0];
4598                 ddi_put32(acc_handle, &sge32[0].length, response_xferlen);
4599                 ddi_put32(acc_handle, &sge32[0].phys_addr,
4600                     response_dma_obj.dma_cookie[0].dmac_address);
4601                 ddi_put32(acc_handle, &sge32[1].length, request_xferlen);
4602                 ddi_put32(acc_handle, &sge32[1].phys_addr,
4603                     request_dma_obj.dma_cookie[0].dmac_address);
4604 #else
4605                 con_log(CL_ANN1, (CE_NOTE,
4606                     "issue_mfi_smp: DDI_MODEL_LP64"));
4607                 sge64 = &smp->sgl[0].sge64[0];
4608                 ddi_put32(acc_handle, &sge64[0].length, response_xferlen);
4609                 ddi_put64(acc_handle, &sge64[0].phys_addr,
4610                     response_dma_obj.dma_cookie[0].dmac_address);
4611                 ddi_put32(acc_handle, &sge64[1].length, request_xferlen);
4612                 ddi_put64(acc_handle, &sge64[1].phys_addr,
4613                     request_dma_obj.dma_cookie[0].dmac_address);
4614 #endif
4615         }
4616         con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp : "
4617             "smp->response_xferlen = %d, smp->request_xferlen = %d "
4618             "smp->data_xfer_len = %d", ddi_get32(acc_handle, &sge32[0].length),
4619             ddi_get32(acc_handle, &sge32[1].length),
4620             ddi_get32(acc_handle, &smp->data_xfer_len)));
4621 
4622         cmd->sync_cmd = MRSAS_TRUE;
4623         cmd->frame_count = 1;
4624 




4625         if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) {
4626                 con_log(CL_ANN, (CE_WARN,
4627                     "issue_mfi_smp: fw_ioctl failed"));
4628         } else {
4629                 con_log(CL_ANN1, (CE_NOTE,
4630                     "issue_mfi_smp: copy to user space"));
4631 
4632                 if (request_xferlen) {
4633                         for (i = 0; i < request_xferlen; i++) {
4634                                 if (ddi_copyout(
4635                                     (uint8_t *)request_dma_obj.buffer +
4636                                     i, (uint8_t *)request_ubuf + i,
4637                                     1, mode)) {
4638                                         con_log(CL_ANN, (CE_WARN,
4639                                             "issue_mfi_smp : copy to user space"
4640                                             " failed"));
4641                                         return (DDI_FAILURE);
4642                                 }
4643                         }
4644                 }
4645 
4646                 if (response_xferlen) {
4647                         for (i = 0; i < response_xferlen; i++) {
4648                                 if (ddi_copyout(
4649                                     (uint8_t *)response_dma_obj.buffer
4650                                     + i, (uint8_t *)response_ubuf
4651                                     + i, 1, mode)) {
4652                                         con_log(CL_ANN, (CE_WARN,
4653                                             "issue_mfi_smp : copy to "
4654                                             "user space failed"));
4655                                         return (DDI_FAILURE);
4656                                 }
4657                         }
4658                 }
4659         }
4660 
4661         ksmp->cmd_status = ddi_get8(acc_handle, &smp->cmd_status);
4662         con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: smp->cmd_status = %d",
4663             ddi_get8(acc_handle, &smp->cmd_status)));
4664         DTRACE_PROBE2(issue_smp, uint8_t, ksmp->cmd, uint8_t, ksmp->cmd_status);
4665 
4666         if (request_xferlen) {
4667                 /* free kernel buffer */
4668                 if (mrsas_free_dma_obj(instance, request_dma_obj) !=
4669                     DDI_SUCCESS)
4670                         return (DDI_FAILURE);
4671         }
4672 
4673         if (response_xferlen) {
4674                 /* free kernel buffer */
4675                 if (mrsas_free_dma_obj(instance, response_dma_obj) !=
4676                     DDI_SUCCESS)
4677                         return (DDI_FAILURE);
4678         }
4679 
4680         return (DDI_SUCCESS);
4681 }
4682 
4683 /*
4684  * issue_mfi_stp
4685  */
4686 static int
4687 issue_mfi_stp(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl,
4688     struct mrsas_cmd *cmd, int mode)
4689 {
4690         void            *fis_ubuf;
4691         void            *data_ubuf;
4692         uint32_t        fis_xferlen = 0;


4693         uint32_t        data_xferlen = 0;
4694         uint_t          model;
4695         dma_obj_t       fis_dma_obj;
4696         dma_obj_t       data_dma_obj;
4697         struct mrsas_stp_frame  *kstp;
4698         struct mrsas_stp_frame  *stp;
4699         ddi_acc_handle_t        acc_handle = cmd->frame_dma_obj.acc_handle;
4700         int i;
4701 
4702         stp = &cmd->frame->stp;
4703         kstp = (struct mrsas_stp_frame *)&ioctl->frame[0];
4704 
4705         if (instance->adapterresetinprogress) {
4706                 con_log(CL_ANN1, (CE_NOTE, "Reset flag set, "
4707                 "returning mfi_pkt and setting TRAN_BUSY\n"));
4708                 return (DDI_FAILURE);
4709         }
4710         model = ddi_model_convert_from(mode & FMODELS);
4711         if (model == DDI_MODEL_ILP32) {
4712                 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_stp: DDI_MODEL_ILP32"));
4713 
4714                 fis_xferlen     = kstp->sgl.sge32[0].length;
4715                 data_xferlen    = kstp->sgl.sge32[1].length;
4716 
4717                 fis_ubuf        = (void *)(ulong_t)kstp->sgl.sge32[0].phys_addr;
4718                 data_ubuf       = (void *)(ulong_t)kstp->sgl.sge32[1].phys_addr;
4719         }
4720         else
4721         {
4722 #ifdef _ILP32
4723                 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_stp: DDI_MODEL_ILP32"));
4724 
4725                 fis_xferlen     = kstp->sgl.sge32[0].length;
4726                 data_xferlen    = kstp->sgl.sge32[1].length;
4727 
4728                 fis_ubuf        = (void *)(ulong_t)kstp->sgl.sge32[0].phys_addr;
4729                 data_ubuf       = (void *)(ulong_t)kstp->sgl.sge32[1].phys_addr;
4730 #else
4731                 con_log(CL_ANN1, (CE_NOTE, "issue_mfi_stp: DDI_MODEL_LP64"));
4732 
4733                 fis_xferlen     = kstp->sgl.sge64[0].length;
4734                 data_xferlen    = kstp->sgl.sge64[1].length;
4735 
4736                 fis_ubuf        = (void *)(ulong_t)kstp->sgl.sge64[0].phys_addr;
4737                 data_ubuf       = (void *)(ulong_t)kstp->sgl.sge64[1].phys_addr;
4738 #endif
4739         }
4740 
4741 
4742         if (fis_xferlen) {
4743                 con_log(CL_ANN, (CE_NOTE, "issue_mfi_stp: "
4744                     "fis_ubuf = %p fis_xferlen = %x", fis_ubuf, fis_xferlen));
4745 
4746                 /* means IOCTL requires DMA */
4747                 /* allocate the data transfer buffer */
4748                 fis_dma_obj.size = fis_xferlen;



4749                 fis_dma_obj.dma_attr = mrsas_generic_dma_attr;
4750                 fis_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
4751                 fis_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
4752                 fis_dma_obj.dma_attr.dma_attr_sgllen = 1;
4753                 fis_dma_obj.dma_attr.dma_attr_align = 1;
4754 
4755                 /* allocate kernel buffer for DMA */
4756                 if (mrsas_alloc_dma_obj(instance, &fis_dma_obj,
4757                     (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
4758                         con_log(CL_ANN, (CE_WARN, "issue_mfi_stp : "
4759                             "could not allocate data transfer buffer."));
4760                         return (DDI_FAILURE);
4761                 }
4762                 (void) memset(fis_dma_obj.buffer, 0, fis_xferlen);
4763 
4764                 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */
4765                 for (i = 0; i < fis_xferlen; i++) {
4766                         if (ddi_copyin((uint8_t *)fis_ubuf + i,
4767                             (uint8_t *)fis_dma_obj.buffer + i, 1, mode)) {
4768                                 con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: "
4769                                     "copy from user space failed"));
4770                                 return (DDI_FAILURE);
4771                         }
4772                 }
4773         }
4774 
4775         if (data_xferlen) {
4776                 con_log(CL_ANN, (CE_NOTE, "issue_mfi_stp: data_ubuf = %p "
4777                     "data_xferlen = %x", data_ubuf, data_xferlen));
4778 
4779                 /* means IOCTL requires DMA */
4780                 /* allocate the data transfer buffer */
4781                 data_dma_obj.size = data_xferlen;



4782                 data_dma_obj.dma_attr = mrsas_generic_dma_attr;
4783                 data_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
4784                 data_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
4785                 data_dma_obj.dma_attr.dma_attr_sgllen = 1;
4786                 data_dma_obj.dma_attr.dma_attr_align = 1;
4787 
4788 /* allocate kernel buffer for DMA */
4789                 if (mrsas_alloc_dma_obj(instance, &data_dma_obj,
4790                     (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
4791                         con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: "
4792                             "could not allocate data transfer buffer."));
4793                         return (DDI_FAILURE);
4794                 }
4795                 (void) memset(data_dma_obj.buffer, 0, data_xferlen);
4796 
4797                 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */
4798                 for (i = 0; i < data_xferlen; i++) {
4799                         if (ddi_copyin((uint8_t *)data_ubuf + i,
4800                             (uint8_t *)data_dma_obj.buffer + i, 1, mode)) {
4801                                 con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: "
4802                                     "copy from user space failed"));
4803                                 return (DDI_FAILURE);
4804                         }
4805                 }
4806         }
4807 
4808         ddi_put8(acc_handle, &stp->cmd, kstp->cmd);


4812         ddi_put8(acc_handle, &stp->sge_count, kstp->sge_count);
4813 
4814         ddi_put16(acc_handle, &stp->timeout, kstp->timeout);
4815         ddi_put32(acc_handle, &stp->data_xfer_len, kstp->data_xfer_len);
4816 
4817         ddi_rep_put8(acc_handle, (uint8_t *)kstp->fis, (uint8_t *)stp->fis, 10,
4818             DDI_DEV_AUTOINCR);
4819 
4820         ddi_put16(acc_handle, &stp->flags, kstp->flags & ~MFI_FRAME_SGL64);
4821         ddi_put32(acc_handle, &stp->stp_flags, kstp->stp_flags);
4822         ddi_put32(acc_handle, &stp->sgl.sge32[0].length, fis_xferlen);
4823         ddi_put32(acc_handle, &stp->sgl.sge32[0].phys_addr,
4824             fis_dma_obj.dma_cookie[0].dmac_address);
4825         ddi_put32(acc_handle, &stp->sgl.sge32[1].length, data_xferlen);
4826         ddi_put32(acc_handle, &stp->sgl.sge32[1].phys_addr,
4827             data_dma_obj.dma_cookie[0].dmac_address);
4828 
4829         cmd->sync_cmd = MRSAS_TRUE;
4830         cmd->frame_count = 1;
4831 




4832         if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) {
4833                 con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: fw_ioctl failed"));
4834         } else {
4835 
4836                 if (fis_xferlen) {
4837                         for (i = 0; i < fis_xferlen; i++) {
4838                                 if (ddi_copyout(
4839                                     (uint8_t *)fis_dma_obj.buffer + i,
4840                                     (uint8_t *)fis_ubuf + i, 1, mode)) {
4841                                         con_log(CL_ANN, (CE_WARN,
4842                                             "issue_mfi_stp : copy to "
4843                                             "user space failed"));
4844                                         return (DDI_FAILURE);
4845                                 }
4846                         }
4847                 }
4848         }
4849         if (data_xferlen) {
4850                 for (i = 0; i < data_xferlen; i++) {
4851                         if (ddi_copyout(
4852                             (uint8_t *)data_dma_obj.buffer + i,
4853                             (uint8_t *)data_ubuf + i, 1, mode)) {
4854                                 con_log(CL_ANN, (CE_WARN,
4855                                     "issue_mfi_stp : copy to"
4856                                     " user space failed"));
4857                                 return (DDI_FAILURE);
4858                         }
4859                 }
4860         }
4861 
4862         kstp->cmd_status = ddi_get8(acc_handle, &stp->cmd_status);


4863         DTRACE_PROBE2(issue_stp, uint8_t, kstp->cmd, uint8_t, kstp->cmd_status);
4864 
4865         if (fis_xferlen) {
4866                 /* free kernel buffer */
4867                 if (mrsas_free_dma_obj(instance, fis_dma_obj) != DDI_SUCCESS)
4868                         return (DDI_FAILURE);
4869         }
4870 
4871         if (data_xferlen) {
4872                 /* free kernel buffer */
4873                 if (mrsas_free_dma_obj(instance, data_dma_obj) != DDI_SUCCESS)
4874                         return (DDI_FAILURE);
4875         }
4876 
4877         return (DDI_SUCCESS);
4878 }
4879 
4880 /*
4881  * fill_up_drv_ver
4882  */
4883 static void
4884 fill_up_drv_ver(struct mrsas_drv_ver *dv)
4885 {
4886         (void) memset(dv, 0, sizeof (struct mrsas_drv_ver));
4887 
4888         (void) memcpy(dv->signature, "$LSI LOGIC$", strlen("$LSI LOGIC$"));
4889         (void) memcpy(dv->os_name, "Solaris", strlen("Solaris"));
4890         (void) memcpy(dv->drv_name, "mr_sas", strlen("mr_sas"));
4891         (void) memcpy(dv->drv_ver, MRSAS_VERSION, strlen(MRSAS_VERSION));
4892         (void) memcpy(dv->drv_rel_date, MRSAS_RELDATE,
4893             strlen(MRSAS_RELDATE));

4894 }
4895 
4896 /*
4897  * handle_drv_ioctl
4898  */
4899 static int
4900 handle_drv_ioctl(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl,
4901     int mode)
4902 {
4903         int     i;
4904         int     rval = DDI_SUCCESS;
4905         int     *props = NULL;
4906         void    *ubuf;
4907 
4908         uint8_t         *pci_conf_buf;
4909         uint32_t        xferlen;
4910         uint32_t        num_props;
4911         uint_t          model;
4912         struct mrsas_dcmd_frame *kdcmd;
4913         struct mrsas_drv_ver    dv;
4914         struct mrsas_pci_information pi;
4915 
4916         kdcmd = (struct mrsas_dcmd_frame *)&ioctl->frame[0];
4917 
4918         model = ddi_model_convert_from(mode & FMODELS);
4919         if (model == DDI_MODEL_ILP32) {
4920                 con_log(CL_ANN1, (CE_NOTE,
4921                     "handle_drv_ioctl: DDI_MODEL_ILP32"));
4922 
4923                 xferlen = kdcmd->sgl.sge32[0].length;
4924 
4925                 ubuf = (void *)(ulong_t)kdcmd->sgl.sge32[0].phys_addr;
4926         } else {
4927 #ifdef _ILP32
4928                 con_log(CL_ANN1, (CE_NOTE,
4929                     "handle_drv_ioctl: DDI_MODEL_ILP32"));
4930                 xferlen = kdcmd->sgl.sge32[0].length;
4931                 ubuf = (void *)(ulong_t)kdcmd->sgl.sge32[0].phys_addr;
4932 #else
4933                 con_log(CL_ANN1, (CE_NOTE,
4934                     "handle_drv_ioctl: DDI_MODEL_LP64"));
4935                 xferlen = kdcmd->sgl.sge64[0].length;
4936                 ubuf = (void *)(ulong_t)kdcmd->sgl.sge64[0].phys_addr;
4937 #endif
4938         }
4939         con_log(CL_ANN1, (CE_NOTE, "handle_drv_ioctl: "
4940             "dataBuf=%p size=%d bytes", ubuf, xferlen));
4941 
4942         switch (kdcmd->opcode) {
4943         case MRSAS_DRIVER_IOCTL_DRIVER_VERSION:
4944                 con_log(CL_ANN1, (CE_NOTE, "handle_drv_ioctl: "
4945                     "MRSAS_DRIVER_IOCTL_DRIVER_VERSION"));
4946 
4947                 fill_up_drv_ver(&dv);
4948 
4949                 if (ddi_copyout(&dv, ubuf, xferlen, mode)) {
4950                         con_log(CL_ANN, (CE_WARN, "handle_drv_ioctl: "
4951                             "MRSAS_DRIVER_IOCTL_DRIVER_VERSION : "
4952                             "copy to user space failed"));
4953                         kdcmd->cmd_status = 1;
4954                         rval = 1;
4955                 } else {
4956                         kdcmd->cmd_status = 0;
4957                 }
4958                 break;
4959         case MRSAS_DRIVER_IOCTL_PCI_INFORMATION:
4960                 con_log(CL_ANN1, (CE_NOTE, "handle_drv_ioctl: "
4961                     "MRSAS_DRIVER_IOCTL_PCI_INFORMAITON"));
4962 
4963                 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, instance->dip,
4964                     0, "reg", &props, &num_props)) {


5000                 kdcmd->cmd_status = 1;
5001                 rval = DDI_FAILURE;
5002                 break;
5003         }
5004 
5005         return (rval);
5006 }
5007 
5008 /*
5009  * handle_mfi_ioctl
5010  */
5011 static int
5012 handle_mfi_ioctl(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl,
5013     int mode)
5014 {
5015         int     rval = DDI_SUCCESS;
5016 
5017         struct mrsas_header     *hdr;
5018         struct mrsas_cmd        *cmd;
5019 



5020         cmd = get_mfi_pkt(instance);
5021 
5022         if (!cmd) {
5023                 con_log(CL_ANN, (CE_WARN, "mr_sas: "
5024                     "failed to get a cmd packet"));
5025                 DTRACE_PROBE2(mfi_ioctl_err, uint16_t,
5026                     instance->fw_outstanding, uint16_t, instance->max_fw_cmds);
5027                 return (DDI_FAILURE);
5028         }
5029         cmd->retry_count_for_ocr = 0;
5030 
5031         /* Clear the frame buffer and assign back the context id */
5032         (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
5033         ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
5034             cmd->index);
5035 
5036         hdr = (struct mrsas_header *)&ioctl->frame[0];
5037 
5038         switch (ddi_get8(cmd->frame_dma_obj.acc_handle, &hdr->cmd)) {
5039         case MFI_CMD_OP_DCMD:
5040                 rval = issue_mfi_dcmd(instance, ioctl, cmd, mode);
5041                 break;
5042         case MFI_CMD_OP_SMP:
5043                 rval = issue_mfi_smp(instance, ioctl, cmd, mode);
5044                 break;
5045         case MFI_CMD_OP_STP:
5046                 rval = issue_mfi_stp(instance, ioctl, cmd, mode);
5047                 break;
5048         case MFI_CMD_OP_LD_SCSI:
5049         case MFI_CMD_OP_PD_SCSI:
5050                 rval = issue_mfi_pthru(instance, ioctl, cmd, mode);
5051                 break;
5052         default:
5053                 con_log(CL_ANN, (CE_WARN, "handle_mfi_ioctl: "
5054                     "invalid mfi ioctl hdr->cmd = %d", hdr->cmd));
5055                 rval = DDI_FAILURE;
5056                 break;
5057         }
5058 
5059         if (mrsas_common_check(instance, cmd) != DDI_SUCCESS)
5060                 rval = DDI_FAILURE;
5061 



5062         return_mfi_pkt(instance, cmd);

5063 
5064         return (rval);
5065 }
5066 
5067 /*
5068  * AEN
5069  */
5070 static int
5071 handle_mfi_aen(struct mrsas_instance *instance, struct mrsas_aen *aen)
5072 {
5073         int     rval = 0;
5074 
5075         rval = register_mfi_aen(instance, instance->aen_seq_num,
5076             aen->class_locale_word);
5077 
5078         aen->cmd_status = (uint8_t)rval;
5079 
5080         return (rval);
5081 }
5082 
5083 static int
5084 register_mfi_aen(struct mrsas_instance *instance, uint32_t seq_num,
5085     uint32_t class_locale_word)
5086 {
5087         int     ret_val;
5088 
5089         struct mrsas_cmd        *cmd, *aen_cmd;
5090         struct mrsas_dcmd_frame *dcmd;
5091         union mrsas_evt_class_locale    curr_aen;
5092         union mrsas_evt_class_locale    prev_aen;
5093 

5094         /*
5095          * If there an AEN pending already (aen_cmd), check if the
5096          * class_locale of that pending AEN is inclusive of the new
5097          * AEN request we currently have. If it is, then we don't have
5098          * to do anything. In other words, whichever events the current
5099          * AEN request is subscribing to, have already been subscribed
5100          * to.
5101          *
5102          * If the old_cmd is _not_ inclusive, then we have to abort
5103          * that command, form a class_locale that is superset of both
5104          * old and current and re-issue to the FW
5105          */
5106 
5107         curr_aen.word = LE_32(class_locale_word);
5108         curr_aen.members.locale = LE_16(curr_aen.members.locale);
5109         aen_cmd = instance->aen_cmd;
5110         if (aen_cmd) {
5111                 prev_aen.word = ddi_get32(aen_cmd->frame_dma_obj.acc_handle,
5112                     &aen_cmd->frame->dcmd.mbox.w[1]);
5113                 prev_aen.word = LE_32(prev_aen.word);


5134                 } else {
5135                         curr_aen.members.locale |= prev_aen.members.locale;
5136 
5137                         if (prev_aen.members.class < curr_aen.members.class)
5138                                 curr_aen.members.class = prev_aen.members.class;
5139 
5140                         ret_val = abort_aen_cmd(instance, aen_cmd);
5141 
5142                         if (ret_val) {
5143                                 con_log(CL_ANN, (CE_WARN, "register_mfi_aen: "
5144                                     "failed to abort prevous AEN command"));
5145 
5146                                 return (ret_val);
5147                         }
5148                 }
5149         } else {
5150                 curr_aen.word = LE_32(class_locale_word);
5151                 curr_aen.members.locale = LE_16(curr_aen.members.locale);
5152         }
5153 



5154         cmd = get_mfi_pkt(instance);

5155 
5156         if (!cmd) {
5157                 DTRACE_PROBE2(mfi_aen_err, uint16_t, instance->fw_outstanding,
5158                     uint16_t, instance->max_fw_cmds);
5159                 return (ENOMEM);
5160         }
5161         cmd->retry_count_for_ocr = 0;
5162         /* Clear the frame buffer and assign back the context id */
5163         (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
5164         ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
5165             cmd->index);
5166 
5167         dcmd = &cmd->frame->dcmd;
5168 
5169         /* for(i = 0; i < DCMD_MBOX_SZ; i++) dcmd->mbox.b[i] = 0; */
5170         (void) memset(dcmd->mbox.b, 0, DCMD_MBOX_SZ);
5171 
5172         (void) memset(instance->mfi_evt_detail_obj.buffer, 0,
5173             sizeof (struct mrsas_evt_detail));
5174 
5175         /* Prepare DCMD for aen registration */
5176         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd, MFI_CMD_OP_DCMD);
5177         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd_status, 0x0);
5178         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->sge_count, 1);
5179         ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->flags,
5180             MFI_FRAME_DIR_READ);
5181         ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->timeout, 0);


5190             curr_aen.word);
5191         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].phys_addr,
5192             instance->mfi_evt_detail_obj.dma_cookie[0].dmac_address);
5193         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].length,
5194             sizeof (struct mrsas_evt_detail));
5195 
5196         instance->aen_seq_num = seq_num;
5197 
5198 
5199         /*
5200          * Store reference to the cmd used to register for AEN. When an
5201          * application wants us to register for AEN, we have to abort this
5202          * cmd and re-register with a new EVENT LOCALE supplied by that app
5203          */
5204         instance->aen_cmd = cmd;
5205 
5206         cmd->frame_count = 1;
5207 
5208         /* Issue the aen registration frame */
5209         /* atomic_add_16 (&instance->fw_outstanding, 1); */



5210         instance->func_ptr->issue_cmd(cmd, instance);
5211 
5212         return (0);
5213 }
5214 
5215 static void
5216 display_scsi_inquiry(caddr_t scsi_inq)
5217 {
5218 #define MAX_SCSI_DEVICE_CODE    14
5219         int             i;
5220         char            inquiry_buf[256] = {0};
5221         int             len;
5222         const char      *const scsi_device_types[] = {
5223                 "Direct-Access    ",
5224                 "Sequential-Access",
5225                 "Printer          ",
5226                 "Processor        ",
5227                 "WORM             ",
5228                 "CD-ROM           ",
5229                 "Scanner          ",
5230                 "Optical Device   ",
5231                 "Medium Changer   ",
5232                 "Communications   ",
5233                 "Unknown          ",
5234                 "Unknown          ",
5235                 "Unknown          ",


5261         len += snprintf(inquiry_buf + len, 265 - len, "\n");
5262 
5263 
5264         i = scsi_inq[0] & 0x1f;
5265 
5266 
5267         len += snprintf(inquiry_buf + len, 265 - len, "  Type:   %s ",
5268             i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] :
5269             "Unknown          ");
5270 
5271 
5272         len += snprintf(inquiry_buf + len, 265 - len,
5273             "                 ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
5274 
5275         if ((scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1) {
5276                 len += snprintf(inquiry_buf + len, 265 - len, " CCS\n");
5277         } else {
5278                 len += snprintf(inquiry_buf + len, 265 - len, "\n");
5279         }
5280 
5281         con_log(CL_ANN1, (CE_CONT, inquiry_buf));
5282 }
5283 
5284 static void
5285 io_timeout_checker(void *arg)
5286 {
5287         struct scsi_pkt *pkt;
5288         struct mrsas_instance *instance = arg;
5289         struct mrsas_cmd        *cmd = NULL;
5290         struct mrsas_header     *hdr;
5291         int time = 0;
5292         int counter = 0;
5293         struct mlist_head       *pos, *next;
5294         mlist_t                 process_list;
5295 
5296         if (instance->adapterresetinprogress == 1) {
5297                 con_log(CL_ANN1, (CE_NOTE, "io_timeout_checker"
5298                     " reset in progress"));

5299                 instance->timeout_id = timeout(io_timeout_checker,
5300                     (void *) instance, drv_usectohz(MRSAS_1_SECOND));
5301                 return;
5302         }
5303 
5304         /* See if this check needs to be in the beginning or last in ISR */
5305         if (mrsas_initiate_ocr_if_fw_is_faulty(instance) ==  1) {
5306                 con_log(CL_ANN1, (CE_NOTE,
5307                     "Fw Fault state Handling in io_timeout_checker"));



5308                 if (instance->adapterresetinprogress == 0) {




5309                         (void) mrsas_reset_ppc(instance);

5310                 }
5311                 instance->timeout_id = timeout(io_timeout_checker,
5312                     (void *) instance, drv_usectohz(MRSAS_1_SECOND));
5313                 return;
5314         }
5315 
5316         INIT_LIST_HEAD(&process_list);
5317 
5318         mutex_enter(&instance->cmd_pend_mtx);
5319         mlist_for_each_safe(pos, next, &instance->cmd_pend_list) {
5320                 cmd = mlist_entry(pos, struct mrsas_cmd, list);
5321 
5322                 if (cmd == NULL) {
5323                         continue;
5324                 }
5325 
5326                 if (cmd->sync_cmd == MRSAS_TRUE) {
5327                         hdr = (struct mrsas_header *)&cmd->frame->hdr;
5328                         if (hdr == NULL) {
5329                                 continue;
5330                         }
5331                         time = --cmd->drv_pkt_time;
5332                 } else {
5333                         pkt = cmd->pkt;
5334                         if (pkt == NULL) {
5335                                 continue;
5336                         }
5337                         time = --cmd->drv_pkt_time;
5338                 }
5339                 if (time <= 0) {
5340                         con_log(CL_ANN1, (CE_NOTE, "%llx: "
5341                             "io_timeout_checker: TIMING OUT: pkt "
5342                             ": %p, cmd %p", gethrtime(), (void *)pkt,
5343                             (void *)cmd));


5344                         counter++;
5345                         break;
5346                 }
5347         }
5348         mutex_exit(&instance->cmd_pend_mtx);
5349 
5350         if (counter) {
5351                 con_log(CL_ANN1, (CE_NOTE,
5352                     "io_timeout_checker "
5353                     "cmd->retrycount_for_ocr %d, "
5354                     "cmd index %d , cmd address %p ",
5355                     cmd->retry_count_for_ocr+1, cmd->index, (void *)cmd));
5356 
5357                 if (instance->disable_online_ctrl_reset == 1) {
5358                         con_log(CL_ANN1, (CE_NOTE, "mrsas: "
5359                             "OCR is not supported by the Firmware "
5360                             "Failing all the queued packets \n"));
5361 



5362                         (void) mrsas_kill_adapter(instance);

5363                         return;
5364                 } else {
5365                         if (cmd->retry_count_for_ocr <=  IO_RETRY_COUNT) {
5366                                 if (instance->adapterresetinprogress == 0) {
5367                                 con_log(CL_ANN1, (CE_NOTE, "mrsas: "
5368                                     "OCR is supported by FW "
5369                                     "triggering  mrsas_reset_ppc"));
5370                                 (void) mrsas_reset_ppc(instance);


5371                                 }

5372                         } else {
5373                                 con_log(CL_ANN1, (CE_NOTE,
5374                                     "io_timeout_checker:"
5375                                     " cmdindex: %d,cmd address: %p "
5376                                     "timed out even after 3 resets: "
5377                                     "so kill adapter", cmd->index,
5378                                     (void *)cmd));





5379                                 (void) mrsas_kill_adapter(instance);
5380                                 return;
5381                         }
5382                 }
5383         }
5384 
5385 
5386         con_log(CL_ANN1, (CE_NOTE, "mrsas: "
5387             "schedule next timeout check: "
5388             "do timeout \n"));
5389         instance->timeout_id =
5390             timeout(io_timeout_checker, (void *)instance,
5391             drv_usectohz(MRSAS_1_SECOND));
5392 }
5393 static int

5394 read_fw_status_reg_ppc(struct mrsas_instance *instance)
5395 {
5396         return ((int)RD_OB_SCRATCH_PAD_0(instance));
5397 }
5398 
5399 static void
5400 issue_cmd_ppc(struct mrsas_cmd *cmd, struct mrsas_instance *instance)
5401 {
5402         struct scsi_pkt *pkt;
5403         atomic_add_16(&instance->fw_outstanding, 1);
5404 
5405         pkt = cmd->pkt;
5406         if (pkt) {
5407                 con_log(CL_ANN1, (CE_CONT, "%llx : issue_cmd_ppc:"
5408                     "ISSUED CMD TO FW : called : cmd:"
5409                     ": %p instance : %p pkt : %p pkt_time : %x\n",
5410                     gethrtime(), (void *)cmd, (void *)instance,
5411                     (void *)pkt, cmd->drv_pkt_time));
5412                 if (instance->adapterresetinprogress) {
5413                         cmd->drv_pkt_time = (uint16_t)debug_timeout_g;
5414                         con_log(CL_ANN1, (CE_NOTE, "Reset the scsi_pkt timer"));
5415                 } else {
5416                         push_pending_mfi_pkt(instance, cmd);
5417                 }
5418 
5419         } else {
5420                 con_log(CL_ANN1, (CE_CONT, "%llx : issue_cmd_ppc:"
5421                     "ISSUED CMD TO FW : called : cmd : %p, instance: %p"
5422                     "(NO PKT)\n", gethrtime(), (void *)cmd, (void *)instance));
5423         }


5424         /* Issue the command to the FW */
5425         WR_IB_QPORT((cmd->frame_phys_addr) |
5426             (((cmd->frame_count - 1) << 1) | 1), instance);


5427 }
5428 
5429 /*
5430  * issue_cmd_in_sync_mode
5431  */
5432 static int
5433 issue_cmd_in_sync_mode_ppc(struct mrsas_instance *instance,
5434 struct mrsas_cmd *cmd)
5435 {
5436         int     i;
5437         uint32_t        msecs = MFI_POLL_TIMEOUT_SECS * (10 * MILLISEC);
5438         struct mrsas_header *hdr = &cmd->frame->hdr;
5439 
5440         con_log(CL_ANN1, (CE_NOTE, "issue_cmd_in_sync_mode_ppc: called"));
5441 
5442         if (instance->adapterresetinprogress) {
5443                 cmd->drv_pkt_time = ddi_get16(
5444                     cmd->frame_dma_obj.acc_handle, &hdr->timeout);
5445                 if (cmd->drv_pkt_time < debug_timeout_g)
5446                         cmd->drv_pkt_time = (uint16_t)debug_timeout_g;

5447                 con_log(CL_ANN1, (CE_NOTE, "sync_mode_ppc: "
5448                     "issue and return in reset case\n"));
5449                 WR_IB_QPORT((cmd->frame_phys_addr) |
5450                     (((cmd->frame_count - 1) << 1) | 1), instance);

5451                 return (DDI_SUCCESS);
5452         } else {
5453                 con_log(CL_ANN1, (CE_NOTE, "sync_mode_ppc: pushing the pkt\n"));
5454                 push_pending_mfi_pkt(instance, cmd);
5455         }
5456 
5457         cmd->cmd_status      = ENODATA;
5458 


5459         WR_IB_QPORT((cmd->frame_phys_addr) |
5460             (((cmd->frame_count - 1) << 1) | 1), instance);

5461 
5462         mutex_enter(&instance->int_cmd_mtx);
5463 
5464         for (i = 0; i < msecs && (cmd->cmd_status == ENODATA); i++) {
5465                 cv_wait(&instance->int_cmd_cv, &instance->int_cmd_mtx);
5466         }
5467 
5468         mutex_exit(&instance->int_cmd_mtx);
5469 
5470         con_log(CL_ANN1, (CE_NOTE, "issue_cmd_in_sync_mode_ppc: done"));
5471 
5472         if (i < (msecs -1)) {
5473                 return (DDI_SUCCESS);
5474         } else {
5475                 return (DDI_FAILURE);
5476         }
5477 }
5478 
5479 /*
5480  * issue_cmd_in_poll_mode
5481  */
5482 static int
5483 issue_cmd_in_poll_mode_ppc(struct mrsas_instance *instance,
5484     struct mrsas_cmd *cmd)
5485 {
5486         int             i;
5487         uint16_t        flags;


5494         ddi_put8(cmd->frame_dma_obj.acc_handle, &frame_hdr->cmd_status,
5495             MFI_CMD_STATUS_POLL_MODE);
5496         flags = ddi_get16(cmd->frame_dma_obj.acc_handle, &frame_hdr->flags);
5497         flags   |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
5498 
5499         ddi_put16(cmd->frame_dma_obj.acc_handle, &frame_hdr->flags, flags);
5500 
5501         /* issue the frame using inbound queue port */
5502         WR_IB_QPORT((cmd->frame_phys_addr) |
5503             (((cmd->frame_count - 1) << 1) | 1), instance);
5504 
5505         /* wait for cmd_status to change from 0xFF */
5506         for (i = 0; i < msecs && (
5507             ddi_get8(cmd->frame_dma_obj.acc_handle, &frame_hdr->cmd_status)
5508             == MFI_CMD_STATUS_POLL_MODE); i++) {
5509                 drv_usecwait(MILLISEC); /* wait for 1000 usecs */
5510         }
5511 
5512         if (ddi_get8(cmd->frame_dma_obj.acc_handle, &frame_hdr->cmd_status)
5513             == MFI_CMD_STATUS_POLL_MODE) {
5514                 con_log(CL_ANN1, (CE_NOTE, "issue_cmd_in_poll_mode: "
5515                     "cmd polling timed out"));
5516                 return (DDI_FAILURE);
5517         }
5518 
5519         return (DDI_SUCCESS);
5520 }
5521 
5522 static void
5523 enable_intr_ppc(struct mrsas_instance *instance)
5524 {
5525         uint32_t        mask;
5526 
5527         con_log(CL_ANN1, (CE_NOTE, "enable_intr_ppc: called"));
5528 
5529         /* WR_OB_DOORBELL_CLEAR(0xFFFFFFFF, instance); */
5530         WR_OB_DOORBELL_CLEAR(OB_DOORBELL_CLEAR_MASK, instance);
5531 
5532         /* WR_OB_INTR_MASK(~0x80000000, instance); */
5533         WR_OB_INTR_MASK(~(MFI_REPLY_2108_MESSAGE_INTR_MASK), instance);
5534 


5613                 con_log(CL_ANN1, (CE_NOTE, "mrsas_kill_adapter: "
5614                     "Writing to doorbell with MFI_STOP_ADP "));
5615                 mutex_enter(&instance->ocr_flags_mtx);
5616                 instance->deadadapter = 1;
5617                 mutex_exit(&instance->ocr_flags_mtx);
5618                 instance->func_ptr->disable_intr(instance);
5619                 WR_IB_DOORBELL(MFI_STOP_ADP, instance);
5620                 (void) mrsas_complete_pending_cmds(instance);
5621                 return (DDI_SUCCESS);
5622 }
5623 
5624 
5625 static int
5626 mrsas_reset_ppc(struct mrsas_instance *instance)
5627 {
5628         uint32_t status;
5629         uint32_t retry = 0;
5630         uint32_t cur_abs_reg_val;
5631         uint32_t fw_state;
5632 


5633         if (instance->deadadapter == 1) {
5634                 con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
5635                     "no more resets as HBA has been marked dead "));
5636                 return (DDI_FAILURE);
5637         }
5638         mutex_enter(&instance->ocr_flags_mtx);
5639         instance->adapterresetinprogress = 1;
5640         mutex_exit(&instance->ocr_flags_mtx);
5641         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: adpterresetinprogress "
5642             "flag set, time %llx", gethrtime()));

5643         instance->func_ptr->disable_intr(instance);
5644 retry_reset:
5645         WR_IB_WRITE_SEQ(0, instance);
5646         WR_IB_WRITE_SEQ(4, instance);
5647         WR_IB_WRITE_SEQ(0xb, instance);
5648         WR_IB_WRITE_SEQ(2, instance);
5649         WR_IB_WRITE_SEQ(7, instance);
5650         WR_IB_WRITE_SEQ(0xd, instance);
5651         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: magic number written "
5652             "to write sequence register\n"));
5653         delay(100 * drv_usectohz(MILLISEC));
5654         status = RD_OB_DRWE(instance);
5655 
5656         while (!(status & DIAG_WRITE_ENABLE)) {
5657                 delay(100 * drv_usectohz(MILLISEC));
5658                 status = RD_OB_DRWE(instance);
5659                 if (retry++ == 100) {
5660                         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: DRWE bit "
5661                             "check retry count %d\n", retry));
5662                         return (DDI_FAILURE);
5663                 }
5664         }
5665         WR_IB_DRWE(status | DIAG_RESET_ADAPTER, instance);
5666         delay(100 * drv_usectohz(MILLISEC));
5667         status = RD_OB_DRWE(instance);
5668         while (status & DIAG_RESET_ADAPTER) {
5669                 delay(100 * drv_usectohz(MILLISEC));
5670                 status = RD_OB_DRWE(instance);
5671                 if (retry++ == 100) {



5672                         (void) mrsas_kill_adapter(instance);
5673                         return (DDI_FAILURE);
5674                 }
5675         }
5676         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: Adapter reset complete"));
5677         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
5678             "Calling mfi_state_transition_to_ready"));
5679 
5680         /* Mark HBA as bad, if FW is fault after 3 continuous resets */
5681         if (mfi_state_transition_to_ready(instance) ||
5682             debug_fw_faults_after_ocr_g == 1) {
5683                 cur_abs_reg_val =
5684                     instance->func_ptr->read_fw_status_reg(instance);
5685                 fw_state        = cur_abs_reg_val & MFI_STATE_MASK;
5686 
5687 #ifdef OCRDEBUG
5688                 con_log(CL_ANN1, (CE_NOTE,
5689                     "mrsas_reset_ppc :before fake: FW is not ready "
5690                     "FW state = 0x%x", fw_state));
5691                 if (debug_fw_faults_after_ocr_g == 1)
5692                         fw_state = MFI_STATE_FAULT;
5693 #endif
5694 
5695                 con_log(CL_ANN1, (CE_NOTE,  "mrsas_reset_ppc : FW is not ready "
5696                     "FW state = 0x%x", fw_state));
5697 
5698                 if (fw_state == MFI_STATE_FAULT) {
5699                         /* increment the count */
5700                         instance->fw_fault_count_after_ocr++;
5701                         if (instance->fw_fault_count_after_ocr
5702                             < MAX_FW_RESET_COUNT) {
5703                                 con_log(CL_ANN1, (CE_WARN, "mrsas_reset_ppc: "
5704                                     "FW is in fault after OCR count %d ",
5705                                     instance->fw_fault_count_after_ocr));

5706                                 goto retry_reset;
5707 
5708                         } else {
5709                                 con_log(CL_ANN1, (CE_WARN, "mrsas_reset_ppc: "
5710                                     "Max Reset Count exceeded "
5711                                     "Mark HBA as bad"));


5712                                 (void) mrsas_kill_adapter(instance);
5713                                 return (DDI_FAILURE);
5714                         }
5715                 }
5716         }
5717         /* reset the counter as FW is up after OCR */
5718         instance->fw_fault_count_after_ocr = 0;
5719 
5720 
5721         ddi_put32(instance->mfi_internal_dma_obj.acc_handle,
5722             instance->producer, 0);
5723 
5724         ddi_put32(instance->mfi_internal_dma_obj.acc_handle,
5725             instance->consumer, 0);
5726 
5727         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
5728             " after resetting produconsumer chck indexs:"
5729             "producer %x consumer %x", *instance->producer,
5730             *instance->consumer));
5731 
5732         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
5733             "Calling mrsas_issue_init_mfi"));
5734         (void) mrsas_issue_init_mfi(instance);
5735         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
5736             "mrsas_issue_init_mfi Done"));

5737         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
5738             "Calling mrsas_print_pending_cmd\n"));
5739         (void) mrsas_print_pending_cmds(instance);
5740         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
5741             "mrsas_print_pending_cmd done\n"));

5742         instance->func_ptr->enable_intr(instance);
5743         instance->fw_outstanding = 0;

5744         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
5745             "Calling mrsas_issue_pending_cmds"));
5746         (void) mrsas_issue_pending_cmds(instance);
5747         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
5748         "Complete"));

5749         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
5750             "Calling aen registration"));





5751         instance->func_ptr->issue_cmd(instance->aen_cmd, instance);
5752         con_log(CL_ANN1, (CE_NOTE, "Unsetting adpresetinprogress flag.\n"));

5753         mutex_enter(&instance->ocr_flags_mtx);
5754         instance->adapterresetinprogress = 0;
5755         mutex_exit(&instance->ocr_flags_mtx);
5756         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
5757             "adpterresetinprogress flag unset"));

5758         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc done\n"));
5759         return (DDI_SUCCESS);
5760 }
5761 static int
5762 mrsas_common_check(struct mrsas_instance *instance,
5763     struct  mrsas_cmd *cmd)



5764 {
5765         int ret = DDI_SUCCESS;
5766 
5767         if (mrsas_check_dma_handle(cmd->frame_dma_obj.dma_handle) !=

5768             DDI_SUCCESS) {
5769                 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED);
5770                 if (cmd->pkt != NULL) {
5771                         cmd->pkt->pkt_reason = CMD_TRAN_ERR;
5772                         cmd->pkt->pkt_statistics = 0;
5773                 }
5774                 ret = DDI_FAILURE;
5775         }
5776         if (mrsas_check_dma_handle(instance->mfi_internal_dma_obj.dma_handle)
5777             != DDI_SUCCESS) {
5778                 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED);
5779                 if (cmd->pkt != NULL) {
5780                         cmd->pkt->pkt_reason = CMD_TRAN_ERR;
5781                         cmd->pkt->pkt_statistics = 0;
5782                 }
5783                 ret = DDI_FAILURE;
5784         }
5785         if (mrsas_check_dma_handle(instance->mfi_evt_detail_obj.dma_handle) !=
5786             DDI_SUCCESS) {
5787                 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED);
5788                 if (cmd->pkt != NULL) {
5789                         cmd->pkt->pkt_reason = CMD_TRAN_ERR;
5790                         cmd->pkt->pkt_statistics = 0;
5791                 }
5792                 ret = DDI_FAILURE;
5793         }
5794         if (mrsas_check_acc_handle(instance->regmap_handle) != DDI_SUCCESS) {
5795                 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED);
5796 
5797                 ddi_fm_acc_err_clear(instance->regmap_handle, DDI_FME_VER0);
5798 
5799                 if (cmd->pkt != NULL) {
5800                         cmd->pkt->pkt_reason = CMD_TRAN_ERR;
5801                         cmd->pkt->pkt_statistics = 0;
5802                 }
5803                 ret = DDI_FAILURE;
5804         }
5805 
5806         return (ret);
5807 }
5808 
5809 /*ARGSUSED*/
5810 static int
5811 mrsas_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
5812 {
5813         /*
5814          * as the driver can always deal with an error in any dma or
5815          * access handle, we can just return the fme_status value.
5816          */
5817         pci_ereport_post(dip, err, NULL);
5818         return (err->fme_status);
5819 }


5923 {
5924         uint64_t ena;
5925         char buf[FM_MAX_CLASS];
5926 
5927         (void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
5928         ena = fm_ena_generate(0, FM_ENA_FMT1);
5929         if (DDI_FM_EREPORT_CAP(instance->fm_capabilities)) {
5930                 ddi_fm_ereport_post(instance->dip, buf, ena, DDI_NOSLEEP,
5931                     FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERSION, NULL);
5932         }
5933 }
5934 
5935 static int
5936 mrsas_add_intrs(struct mrsas_instance *instance, int intr_type)
5937 {
5938 
5939         dev_info_t *dip = instance->dip;
5940         int     avail, actual, count;
5941         int     i, flag, ret;
5942 
5943         con_log(CL_DLEVEL1, (CE_WARN, "mrsas_add_intrs: intr_type = %x",
5944             intr_type));
5945 
5946         /* Get number of interrupts */
5947         ret = ddi_intr_get_nintrs(dip, intr_type, &count);
5948         if ((ret != DDI_SUCCESS) || (count == 0)) {
5949                 con_log(CL_ANN, (CE_WARN, "ddi_intr_get_nintrs() failed:"
5950                     "ret %d count %d", ret, count));
5951 
5952                 return (DDI_FAILURE);
5953         }
5954 
5955         con_log(CL_DLEVEL1, (CE_WARN, "mrsas_add_intrs: count = %d ", count));
5956 
5957         /* Get number of available interrupts */
5958         ret = ddi_intr_get_navail(dip, intr_type, &avail);
5959         if ((ret != DDI_SUCCESS) || (avail == 0)) {
5960                 con_log(CL_ANN, (CE_WARN, "ddi_intr_get_navail() failed:"
5961                     "ret %d avail %d", ret, avail));
5962 
5963                 return (DDI_FAILURE);
5964         }
5965         con_log(CL_DLEVEL1, (CE_WARN, "mrsas_add_intrs: avail = %d ", avail));
5966 
5967         /* Only one interrupt routine. So limit the count to 1 */
5968         if (count > 1) {
5969                 count = 1;
5970         }
5971 
5972         /*
5973          * Allocate an array of interrupt handlers. Currently we support
5974          * only one interrupt. The framework can be extended later.
5975          */
5976         instance->intr_size = count * sizeof (ddi_intr_handle_t);
5977         instance->intr_htable = kmem_zalloc(instance->intr_size, KM_SLEEP);

5978         ASSERT(instance->intr_htable);
5979 
5980         flag = ((intr_type == DDI_INTR_TYPE_MSI) || (intr_type ==
5981             DDI_INTR_TYPE_MSIX)) ? DDI_INTR_ALLOC_STRICT:DDI_INTR_ALLOC_NORMAL;

5982 
5983         /* Allocate interrupt */
5984         ret = ddi_intr_alloc(dip, instance->intr_htable, intr_type, 0,
5985             count, &actual, flag);
5986 
5987         if ((ret != DDI_SUCCESS) || (actual == 0)) {
5988                 con_log(CL_ANN, (CE_WARN, "mrsas_add_intrs: "
5989                     "avail = %d", avail));
5990                 kmem_free(instance->intr_htable, instance->intr_size);
5991                 return (DDI_FAILURE);
5992         }

5993         if (actual < count) {
5994                 con_log(CL_ANN, (CE_WARN, "mrsas_add_intrs: "
5995                     "Requested = %d  Received = %d", count, actual));
5996         }
5997         instance->intr_cnt = actual;
5998 
5999         /*
6000          * Get the priority of the interrupt allocated.
6001          */
6002         if ((ret = ddi_intr_get_pri(instance->intr_htable[0],
6003             &instance->intr_pri)) != DDI_SUCCESS) {
6004                 con_log(CL_ANN, (CE_WARN, "mrsas_add_intrs: "
6005                     "get priority call failed"));
6006 
6007                 for (i = 0; i < actual; i++) {
6008                         (void) ddi_intr_free(instance->intr_htable[i]);
6009                 }
6010                 kmem_free(instance->intr_htable, instance->intr_size);
6011                 return (DDI_FAILURE);
6012         }
6013 
6014         /*
6015          * Test for high level mutex. we don't support them.
6016          */
6017         if (instance->intr_pri >= ddi_intr_get_hilevel_pri()) {
6018                 con_log(CL_ANN, (CE_WARN, "mrsas_add_intrs: "
6019                     "High level interrupts not supported."));
6020 
6021                 for (i = 0; i < actual; i++) {
6022                         (void) ddi_intr_free(instance->intr_htable[i]);
6023                 }
6024                 kmem_free(instance->intr_htable, instance->intr_size);
6025                 return (DDI_FAILURE);
6026         }
6027 
6028         con_log(CL_DLEVEL1, (CE_NOTE, "mrsas_add_intrs: intr_pri = 0x%x ",
6029             instance->intr_pri));
6030 
6031         /* Call ddi_intr_add_handler() */
6032         for (i = 0; i < actual; i++) {
6033                 ret = ddi_intr_add_handler(instance->intr_htable[i],
6034                     (ddi_intr_handler_t *)mrsas_isr, (caddr_t)instance,
6035                     (caddr_t)(uintptr_t)i);
6036 
6037                 if (ret != DDI_SUCCESS) {
6038                         con_log(CL_ANN, (CE_WARN, "mrsas_add_intrs:"
6039                             "failed %d", ret));
6040 
6041                         for (i = 0; i < actual; i++) {
6042                                 (void) ddi_intr_free(instance->intr_htable[i]);
6043                         }
6044                         kmem_free(instance->intr_htable, instance->intr_size);
6045                         return (DDI_FAILURE);
6046                 }
6047 
6048         }
6049 
6050         con_log(CL_DLEVEL1, (CE_WARN, " ddi_intr_add_handler done"));
6051 
6052         if ((ret = ddi_intr_get_cap(instance->intr_htable[0],
6053             &instance->intr_cap)) != DDI_SUCCESS) {
6054                 con_log(CL_ANN, (CE_WARN, "ddi_intr_get_cap() failed %d",
6055                     ret));
6056 
6057                 /* Free already allocated intr */
6058                 for (i = 0; i < actual; i++) {
6059                         (void) ddi_intr_remove_handler(
6060                             instance->intr_htable[i]);
6061                         (void) ddi_intr_free(instance->intr_htable[i]);
6062                 }
6063                 kmem_free(instance->intr_htable, instance->intr_size);
6064                 return (DDI_FAILURE);
6065         }
6066 
6067         if (instance->intr_cap &  DDI_INTR_FLAG_BLOCK) {
6068                 con_log(CL_ANN, (CE_WARN, "Calling ddi_intr_block _enable"));
6069 
6070                 (void) ddi_intr_block_enable(instance->intr_htable,
6071                     instance->intr_cnt);
6072         } else {
6073                 con_log(CL_ANN, (CE_NOTE, " calling ddi_intr_enable"));
6074 
6075                 for (i = 0; i < instance->intr_cnt; i++) {
6076                         (void) ddi_intr_enable(instance->intr_htable[i]);
6077                         con_log(CL_ANN, (CE_NOTE, "ddi intr enable returns "
6078                             "%d", i));
6079                 }
6080         }
6081 
6082         return (DDI_SUCCESS);
6083 

















6084 }
6085 
6086 
6087 static void
6088 mrsas_rem_intrs(struct mrsas_instance *instance)
6089 {
6090         int i;
6091 
6092         con_log(CL_ANN, (CE_NOTE, "mrsas_rem_intrs called"));
6093 
6094         /* Disable all interrupts first */
6095         if (instance->intr_cap & DDI_INTR_FLAG_BLOCK) {
6096                 (void) ddi_intr_block_disable(instance->intr_htable,
6097                     instance->intr_cnt);
6098         } else {
6099                 for (i = 0; i < instance->intr_cnt; i++) {
6100                         (void) ddi_intr_disable(instance->intr_htable[i]);
6101                 }
6102         }
6103 
6104         /* Remove all the handlers */
6105 
6106         for (i = 0; i < instance->intr_cnt; i++) {
6107                 (void) ddi_intr_remove_handler(instance->intr_htable[i]);
6108                 (void) ddi_intr_free(instance->intr_htable[i]);
6109         }
6110 
6111         kmem_free(instance->intr_htable, instance->intr_size);





6112 }
6113 
6114 static int
6115 mrsas_tran_bus_config(dev_info_t *parent, uint_t flags,
6116     ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
6117 {
6118         struct mrsas_instance *instance;
6119         int config;
6120         int rval;
6121 
6122         char *ptr = NULL;
6123         int tgt, lun;
6124 
6125         con_log(CL_ANN1, (CE_NOTE, "Bus config called for op = %x", op));
6126 
6127         if ((instance = ddi_get_soft_state(mrsas_state,
6128             ddi_get_instance(parent))) == NULL) {
6129                 return (NDI_FAILURE);
6130         }
6131 
6132         /* Hold nexus during bus_config */
6133         ndi_devi_enter(parent, &config);
6134         switch (op) {
6135         case BUS_CONFIG_ONE: {
6136 
6137                 /* parse wwid/target name out of name given */
6138                 if ((ptr = strchr((char *)arg, '@')) == NULL) {
6139                         rval = NDI_FAILURE;
6140                         break;
6141                 }
6142                 ptr++;
6143 
6144                 if (mrsas_parse_devname(arg, &tgt, &lun) != 0) {
6145                         rval = NDI_FAILURE;
6146                         break;
6147                 }
6148 
6149                 if (lun == 0) {
6150                         rval = mrsas_config_ld(instance, tgt, lun, childp);





6151                 } else {
6152                         rval = NDI_FAILURE;
6153                 }
6154 
6155                 break;
6156         }
6157         case BUS_CONFIG_DRIVER:
6158         case BUS_CONFIG_ALL: {
6159 
6160                 rval = mrsas_config_all_devices(instance);
6161 
6162                 rval = NDI_SUCCESS;
6163                 break;
6164         }
6165         }
6166 
6167         if (rval == NDI_SUCCESS) {
6168                 rval = ndi_busop_bus_config(parent, flags, op, arg, childp, 0);
6169 
6170         }
6171         ndi_devi_exit(parent, config);
6172 
6173         con_log(CL_ANN1, (CE_NOTE, "mrsas_tran_bus_config: rval = %x",
6174             rval));
6175         return (rval);
6176 }
6177 
6178 static int
6179 mrsas_config_all_devices(struct mrsas_instance *instance)
6180 {
6181         int rval, tgt;
6182 
6183         for (tgt = 0; tgt < MRDRV_MAX_LD; tgt++) {
6184                 (void) mrsas_config_ld(instance, tgt, 0, NULL);
6185 
6186         }
6187 









6188         rval = NDI_SUCCESS;
6189         return (rval);
6190 }
6191 
6192 static int
6193 mrsas_parse_devname(char *devnm, int *tgt, int *lun)
6194 {
6195         char devbuf[SCSI_MAXNAMELEN];
6196         char *addr;
6197         char *p,  *tp, *lp;
6198         long num;
6199 
6200         /* Parse dev name and address */
6201         (void) strcpy(devbuf, devnm);
6202         addr = "";
6203         for (p = devbuf; *p != '\0'; p++) {
6204                 if (*p == '@') {
6205                         addr = p + 1;
6206                         *p = '\0';
6207                 } else if (*p == ':') {


6224                 }
6225                         *tgt = (int)num;
6226         }
6227         if (lun && lp) {
6228                 if (ddi_strtol(lp, NULL, 0x10, &num)) {
6229                         return (DDI_FAILURE);
6230                 }
6231                         *lun = (int)num;
6232         }
6233         return (DDI_SUCCESS);  /* Success case */
6234 }
6235 
6236 static int
6237 mrsas_config_ld(struct mrsas_instance *instance, uint16_t tgt,
6238     uint8_t lun, dev_info_t **ldip)
6239 {
6240         struct scsi_device *sd;
6241         dev_info_t *child;
6242         int rval;
6243 
6244         con_log(CL_ANN1, (CE_NOTE, "mrsas_config_ld: t = %d l = %d",
6245             tgt, lun));
6246 
6247         if ((child = mrsas_find_child(instance, tgt, lun)) != NULL) {
6248                 if (ldip) {
6249                         *ldip = child;
6250                 }
6251                 con_log(CL_ANN1, (CE_NOTE,
6252                     "mrsas_config_ld: Child = %p found t = %d l = %d",
6253                     (void *)child, tgt, lun));





6254                 return (NDI_SUCCESS);
6255         }
6256 
6257         sd = kmem_zalloc(sizeof (struct scsi_device), KM_SLEEP);
6258         sd->sd_address.a_hba_tran = instance->tran;
6259         sd->sd_address.a_target = (uint16_t)tgt;
6260         sd->sd_address.a_lun = (uint8_t)lun;
6261 
6262         if (scsi_hba_probe(sd, NULL) == SCSIPROBE_EXISTS)
6263                 rval = mrsas_config_scsi_device(instance, sd, ldip);
6264         else
6265                 rval = NDI_FAILURE;
6266 
6267         /* sd_unprobe is blank now. Free buffer manually */
6268         if (sd->sd_inq) {
6269                 kmem_free(sd->sd_inq, SUN_INQSIZE);
6270                 sd->sd_inq = (struct scsi_inquiry *)NULL;
6271         }
6272 
6273         kmem_free(sd, sizeof (struct scsi_device));
6274         con_log(CL_ANN1, (CE_NOTE, "mrsas_config_ld: return rval = %d",
6275             rval));
6276         return (rval);
6277 }
6278 
6279 static int
6280 mrsas_config_scsi_device(struct mrsas_instance *instance,
6281     struct scsi_device *sd, dev_info_t **dipp)
6282 {
6283         char *nodename = NULL;
6284         char **compatible = NULL;
6285         int ncompatible = 0;
6286         char *childname;
6287         dev_info_t *ldip = NULL;
6288         int tgt = sd->sd_address.a_target;
6289         int lun = sd->sd_address.a_lun;
6290         int dtype = sd->sd_inq->inq_dtype & DTYPE_MASK;
6291         int rval;
6292 
6293         con_log(CL_ANN1, (CE_WARN, "mr_sas: scsi_device t%dL%d", tgt, lun));
6294         scsi_hba_nodename_compatible_get(sd->sd_inq, NULL, dtype,
6295             NULL, &nodename, &compatible, &ncompatible);
6296 
6297         if (nodename == NULL) {
6298                 con_log(CL_ANN1, (CE_WARN, "mr_sas: Found no compatible driver "
6299                     "for t%dL%d", tgt, lun));
6300                 rval = NDI_FAILURE;
6301                 goto finish;
6302         }
6303 
6304         childname = (dtype == DTYPE_DIRECT) ? "sd" : nodename;
6305         con_log(CL_ANN1, (CE_WARN,
6306             "mr_sas: Childname = %2s nodename = %s", childname, nodename));
6307 
6308         /* Create a dev node */
6309         rval = ndi_devi_alloc(instance->dip, childname, DEVI_SID_NODEID, &ldip);
6310         con_log(CL_ANN1, (CE_WARN,
6311             "mr_sas_config_scsi_device: ndi_devi_alloc rval = %x", rval));
6312         if (rval == NDI_SUCCESS) {
6313                 if (ndi_prop_update_int(DDI_DEV_T_NONE, ldip, "target", tgt) !=
6314                     DDI_PROP_SUCCESS) {
6315                         con_log(CL_ANN1, (CE_WARN, "mr_sas: unable to create "
6316                             "property for t%dl%d target", tgt, lun));
6317                         rval = NDI_FAILURE;
6318                         goto finish;
6319                 }
6320                 if (ndi_prop_update_int(DDI_DEV_T_NONE, ldip, "lun", lun) !=
6321                     DDI_PROP_SUCCESS) {
6322                         con_log(CL_ANN1, (CE_WARN, "mr_sas: unable to create "
6323                             "property for t%dl%d lun", tgt, lun));
6324                         rval = NDI_FAILURE;
6325                         goto finish;
6326                 }
6327 
6328                 if (ndi_prop_update_string_array(DDI_DEV_T_NONE, ldip,
6329                     "compatible", compatible, ncompatible) !=
6330                     DDI_PROP_SUCCESS) {
6331                         con_log(CL_ANN1, (CE_WARN, "mr_sas: unable to create "
6332                             "property for t%dl%d compatible", tgt, lun));
6333                         rval = NDI_FAILURE;
6334                         goto finish;
6335                 }
6336 
6337                 rval = ndi_devi_online(ldip, NDI_ONLINE_ATTACH);
6338                 if (rval != NDI_SUCCESS) {
6339                         con_log(CL_ANN1, (CE_WARN, "mr_sas: unable to online "
6340                             "t%dl%d", tgt, lun));
6341                         ndi_prop_remove_all(ldip);
6342                         (void) ndi_devi_free(ldip);
6343                 } else {
6344                         con_log(CL_ANN1, (CE_WARN, "mr_sas: online Done :"
6345                             "0 t%dl%d", tgt, lun));
6346                 }
6347 
6348         }
6349 finish:
6350         if (dipp) {
6351                 *dipp = ldip;
6352         }
6353 
6354         con_log(CL_DLEVEL1, (CE_WARN,
6355             "mr_sas: config_scsi_device rval = %d t%dL%d",
6356             rval, tgt, lun));
6357         scsi_hba_nodename_compatible_free(nodename, compatible);
6358         return (rval);
6359 }
6360 
6361 /*ARGSUSED*/
6362 static int
6363 mrsas_service_evt(struct mrsas_instance *instance, int tgt, int lun, int event,
6364     uint64_t wwn)
6365 {
6366         struct mrsas_eventinfo *mrevt = NULL;
6367 
6368         con_log(CL_ANN1, (CE_NOTE,
6369             "mrsas_service_evt called for t%dl%d event = %d",
6370             tgt, lun, event));
6371 
6372         if ((instance->taskq == NULL) || (mrevt =
6373             kmem_zalloc(sizeof (struct mrsas_eventinfo), KM_NOSLEEP)) == NULL) {
6374                 return (ENOMEM);
6375         }
6376 
6377         mrevt->instance = instance;
6378         mrevt->tgt = tgt;
6379         mrevt->lun = lun;
6380         mrevt->event = event;

6381 
6382         if ((ddi_taskq_dispatch(instance->taskq,
6383             (void (*)(void *))mrsas_issue_evt_taskq, mrevt, DDI_NOSLEEP)) !=
6384             DDI_SUCCESS) {
6385                 con_log(CL_ANN1, (CE_NOTE,
6386                     "mr_sas: Event task failed for t%dl%d event = %d",
6387                     tgt, lun, event));
6388                 kmem_free(mrevt, sizeof (struct mrsas_eventinfo));
6389                 return (DDI_FAILURE);
6390         }
6391         DTRACE_PROBE3(service_evt, int, tgt, int, lun, int, event);
6392         return (DDI_SUCCESS);
6393 }
6394 
6395 static void
6396 mrsas_issue_evt_taskq(struct mrsas_eventinfo *mrevt)
6397 {
6398         struct mrsas_instance *instance = mrevt->instance;
6399         dev_info_t *dip, *pdip;
6400         int circ1 = 0;
6401         char *devname;
6402 
6403         con_log(CL_ANN1, (CE_NOTE, "mrsas_issue_evt_taskq: called for"
6404             " tgt %d lun %d event %d",
6405             mrevt->tgt, mrevt->lun, mrevt->event));
6406 
6407         if (mrevt->tgt < MRDRV_MAX_LD && mrevt->lun == 0) {

6408                 dip = instance->mr_ld_list[mrevt->tgt].dip;


6409         } else {
6410                 return;



6411         }
6412 

6413         ndi_devi_enter(instance->dip, &circ1);
6414         switch (mrevt->event) {
6415         case MRSAS_EVT_CONFIG_TGT:
6416                 if (dip == NULL) {
6417 
6418                         if (mrevt->lun == 0) {
6419                                 (void) mrsas_config_ld(instance, mrevt->tgt,
6420                                     0, NULL);






6421                         }
6422                         con_log(CL_ANN1, (CE_NOTE,
6423                             "mr_sas: EVT_CONFIG_TGT called:"
6424                             " for tgt %d lun %d event %d",
6425                             mrevt->tgt, mrevt->lun, mrevt->event));
6426 
6427                 } else {
6428                         con_log(CL_ANN1, (CE_NOTE,
6429                             "mr_sas: EVT_CONFIG_TGT dip != NULL:"
6430                             " for tgt %d lun %d event %d",
6431                             mrevt->tgt, mrevt->lun, mrevt->event));
6432                 }
6433                 break;
6434         case MRSAS_EVT_UNCONFIG_TGT:
6435                 if (dip) {
6436                         if (i_ddi_devi_attached(dip)) {
6437 
6438                                 pdip = ddi_get_parent(dip);
6439 
6440                                 devname = kmem_zalloc(MAXNAMELEN + 1, KM_SLEEP);


6444                                     DV_CLEAN_FORCE);
6445                                 kmem_free(devname, MAXNAMELEN + 1);
6446                         }
6447                         (void) ndi_devi_offline(dip, NDI_DEVI_REMOVE);
6448                         con_log(CL_ANN1, (CE_NOTE,
6449                             "mr_sas: EVT_UNCONFIG_TGT called:"
6450                             " for tgt %d lun %d event %d",
6451                             mrevt->tgt, mrevt->lun, mrevt->event));
6452                 } else {
6453                         con_log(CL_ANN1, (CE_NOTE,
6454                             "mr_sas: EVT_UNCONFIG_TGT dip == NULL:"
6455                             " for tgt %d lun %d event %d",
6456                             mrevt->tgt, mrevt->lun, mrevt->event));
6457                 }
6458                 break;
6459         }
6460         kmem_free(mrevt, sizeof (struct mrsas_eventinfo));
6461         ndi_devi_exit(instance->dip, circ1);
6462 }
6463 
6464 static int

6465 mrsas_mode_sense_build(struct scsi_pkt *pkt)
6466 {
6467         union scsi_cdb          *cdbp;
6468         uint16_t                page_code;
6469         struct scsa_cmd         *acmd;
6470         struct buf              *bp;
6471         struct mode_header      *modehdrp;
6472 
6473         cdbp = (void *)pkt->pkt_cdbp;
6474         page_code = cdbp->cdb_un.sg.scsi[0];
6475         acmd = PKT2CMD(pkt);
6476         bp = acmd->cmd_buf;
6477         if ((!bp) && bp->b_un.b_addr && bp->b_bcount && acmd->cmd_dmacount) {
6478                 con_log(CL_ANN1, (CE_WARN, "Failing MODESENSE Command"));
6479                 /* ADD pkt statistics as Command failed. */
6480                 return (NULL);
6481         }
6482 
6483         bp_mapin(bp);
6484         bzero(bp->b_un.b_addr, bp->b_bcount);


   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 */


 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: "


 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, &reglength) != 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),


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;


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                 }


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 #if 0
2054 /*
2055  * tran_bus_reset - reset the SCSI bus
2056  * @dip:
2057  * @level:
2058  *
2059  * The tran_bus_reset() vector in the scsi_hba_tran structure should be
2060  * initialized during the HBA driver's attach(). The vector should point to
2061  * an HBA entry point that is to be called when a user initiates a bus reset.
2062  * Implementation is hardware specific. If the HBA driver cannot reset the
2063  * SCSI bus without affecting the targets, the driver should fail RESET_BUS
2064  * or not initialize this vector.
2065  */
2066 /*ARGSUSED*/
2067 static int
2068 mrsas_tran_bus_reset(dev_info_t *dip, int level)
2069 {
2070         int     instance_no = ddi_get_instance(dip);
2071 
2072         struct mrsas_instance   *instance = ddi_get_soft_state(mrsas_state,
2073             instance_no);
2074 
2075         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
2076 
2077         if (wait_for_outstanding(instance)) {
2078                 con_log(CL_ANN1,
2079                     (CE_CONT, "wait_for_outstanding: return FAIL.\n"));
2080                 return (DDI_FAILURE);
2081         } else {
2082                 return (DDI_SUCCESS);
2083         }
2084 }
2085 #endif
2086 
2087 /*
2088  * tran_getcap - get one of a set of SCSA-defined capabilities
2089  * @ap:
2090  * @cap:
2091  * @whom:
2092  *
2093  * The target driver can request the current setting of the capability for a
2094  * particular target by setting the whom parameter to nonzero. A whom value of
2095  * zero indicates a request for the current setting of the general capability
2096  * for the SCSI bus or for adapter hardware. The tran_getcap() should return -1
2097  * for undefined capabilities or the current value of the requested capability.
2098  */
2099 /*ARGSUSED*/
2100 static int
2101 mrsas_tran_getcap(struct scsi_address *ap, char *cap, int whom)
2102 {
2103         int     rval = 0;
2104 
2105         struct mrsas_instance   *instance = ADDR2MR(ap);
2106 
2107         con_log(CL_DLEVEL2, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
2108 
2109         /* we do allow inquiring about capabilities for other targets */
2110         if (cap == NULL) {
2111                 return (-1);
2112         }
2113 
2114         switch (scsi_hba_lookup_capstr(cap)) {
2115         case SCSI_CAP_DMA_MAX:
2116                 if (instance->tbolt) {
2117                         /* Limit to 256k max transfer */
2118                         rval = mrsas_tbolt_max_cap_maxxfer;
2119                 } else {
2120                         /* Limit to 16MB max transfer */
2121                         rval = mrsas_max_cap_maxxfer;
2122                 }
2123                 break;
2124         case SCSI_CAP_MSG_OUT:
2125                 rval = 1;
2126                 break;
2127         case SCSI_CAP_DISCONNECT:
2128                 rval = 0;
2129                 break;
2130         case SCSI_CAP_SYNCHRONOUS:
2131                 rval = 0;
2132                 break;
2133         case SCSI_CAP_WIDE_XFER:
2134                 rval = 1;
2135                 break;
2136         case SCSI_CAP_TAGGED_QING:
2137                 rval = 1;
2138                 break;
2139         case SCSI_CAP_UNTAGGED_QING:
2140                 rval = 1;
2141                 break;
2142         case SCSI_CAP_PARITY:


2151         case SCSI_CAP_LINKED_CMDS:
2152                 rval = 0;
2153                 break;
2154         case SCSI_CAP_RESET_NOTIFICATION:
2155                 rval = 1;
2156                 break;
2157         case SCSI_CAP_GEOMETRY:
2158                 rval = -1;
2159 
2160                 break;
2161         default:
2162                 con_log(CL_DLEVEL2, (CE_NOTE, "Default cap coming 0x%x",
2163                     scsi_hba_lookup_capstr(cap)));
2164                 rval = -1;
2165                 break;
2166         }
2167 
2168         return (rval);
2169 }
2170 
2171 /*
2172  * tran_setcap - set one of a set of SCSA-defined capabilities
2173  * @ap:
2174  * @cap:
2175  * @value:
2176  * @whom:
2177  *
2178  * The target driver might request that the new value be set for a particular
2179  * target by setting the whom parameter to nonzero. A whom value of zero
2180  * means that request is to set the new value for the SCSI bus or for adapter
2181  * hardware in general.
2182  * The tran_setcap() should return the following values as appropriate:
2183  * - -1 for undefined capabilities
2184  * - 0 if the HBA driver cannot set the capability to the requested value
2185  * - 1 if the HBA driver is able to set the capability to the requested value
2186  */
2187 /*ARGSUSED*/
2188 static int
2189 mrsas_tran_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2190 {
2191         int             rval = 1;
2192 
2193         con_log(CL_DLEVEL2, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
2194 
2195         /* We don't allow setting capabilities for other targets */
2196         if (cap == NULL || whom == 0) {
2197                 return (-1);
2198         }
2199 
2200         switch (scsi_hba_lookup_capstr(cap)) {
2201                 case SCSI_CAP_DMA_MAX:
2202                 case SCSI_CAP_MSG_OUT:
2203                 case SCSI_CAP_PARITY:
2204                 case SCSI_CAP_LINKED_CMDS:
2205                 case SCSI_CAP_RESET_NOTIFICATION:
2206                 case SCSI_CAP_DISCONNECT:
2207                 case SCSI_CAP_SYNCHRONOUS:
2208                 case SCSI_CAP_UNTAGGED_QING:
2209                 case SCSI_CAP_WIDE_XFER:
2210                 case SCSI_CAP_INITIATOR_ID:
2211                 case SCSI_CAP_ARQ:
2212                         /*
2213                          * None of these are settable via


2215                          */
2216                         break;
2217                 case SCSI_CAP_TAGGED_QING:
2218                         rval = 1;
2219                         break;
2220                 case SCSI_CAP_SECTOR_SIZE:
2221                         rval = 1;
2222                         break;
2223 
2224                 case SCSI_CAP_TOTAL_SECTORS:
2225                         rval = 1;
2226                         break;
2227                 default:
2228                         rval = -1;
2229                         break;
2230         }
2231 
2232         return (rval);
2233 }
2234 
2235 /*
2236  * tran_destroy_pkt - deallocate scsi_pkt structure
2237  * @ap:
2238  * @pkt:
2239  *
2240  * The tran_destroy_pkt() entry point is the HBA driver function that
2241  * deallocates scsi_pkt structures. The tran_destroy_pkt() entry point is
2242  * called when the target driver calls scsi_destroy_pkt(). The
2243  * tran_destroy_pkt() entry point must free any DMA resources that have been
2244  * allocated for the packet. An implicit DMA synchronization occurs if the
2245  * DMA resources are freed and any cached data remains after the completion
2246  * of the transfer.
2247  */
2248 static void
2249 mrsas_tran_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2250 {
2251         struct scsa_cmd *acmd = PKT2CMD(pkt);
2252 
2253         con_log(CL_DLEVEL2, (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         /* free the pkt */
2266         scsi_hba_pkt_free(ap, pkt);
2267 }
2268 
2269 /*
2270  * tran_dmafree - deallocates DMA resources
2271  * @ap:
2272  * @pkt:
2273  *
2274  * The tran_dmafree() entry point deallocates DMAQ resources that have been
2275  * allocated for a scsi_pkt structure. The tran_dmafree() entry point is
2276  * called when the target driver calls scsi_dmafree(). The tran_dmafree() must
2277  * free only DMA resources allocated for a scsi_pkt structure, not the
2278  * scsi_pkt itself. When DMA resources are freed, a DMA synchronization is
2279  * implicitly performed.
2280  */
2281 /*ARGSUSED*/
2282 static void
2283 mrsas_tran_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
2284 {
2285         register struct scsa_cmd *acmd = PKT2CMD(pkt);
2286 
2287         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
2288 
2289         if (acmd->cmd_flags & CFLAG_DMAVALID) {
2290                 acmd->cmd_flags &= ~CFLAG_DMAVALID;
2291 
2292                 (void) ddi_dma_unbind_handle(acmd->cmd_dmahandle);
2293 
2294                 ddi_dma_free_handle(&acmd->cmd_dmahandle);
2295 
2296                 acmd->cmd_dmahandle = NULL;
2297         }
2298 }
2299 
2300 /*
2301  * tran_sync_pkt - synchronize the DMA object allocated
2302  * @ap:
2303  * @pkt:
2304  *
2305  * The tran_sync_pkt() entry point synchronizes the DMA object allocated for
2306  * the scsi_pkt structure before or after a DMA transfer. The tran_sync_pkt()
2307  * entry point is called when the target driver calls scsi_sync_pkt(). If the
2308  * data transfer direction is a DMA read from device to memory, tran_sync_pkt()
2309  * must synchronize the CPU's view of the data. If the data transfer direction
2310  * is a DMA write from memory to device, tran_sync_pkt() must synchronize the
2311  * device's view of the data.
2312  */
2313 /*ARGSUSED*/
2314 static void
2315 mrsas_tran_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2316 {
2317         register struct scsa_cmd        *acmd = PKT2CMD(pkt);
2318 
2319         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
2320 
2321         if (acmd->cmd_flags & CFLAG_DMAVALID) {
2322                 (void) ddi_dma_sync(acmd->cmd_dmahandle, acmd->cmd_dma_offset,
2323                     acmd->cmd_dma_len, (acmd->cmd_flags & CFLAG_DMASEND) ?
2324                     DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU);
2325         }
2326 }
2327 
2328 /*ARGSUSED*/
2329 static int
2330 mrsas_tran_quiesce(dev_info_t *dip)
2331 {
2332         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
2333 
2334         return (1);
2335 }
2336 
2337 /*ARGSUSED*/
2338 static int
2339 mrsas_tran_unquiesce(dev_info_t *dip)
2340 {
2341         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
2342 
2343         return (1);
2344 }
2345 
2346 
2347 /*
2348  * mrsas_isr(caddr_t)
2349  *
2350  * The Interrupt Service Routine
2351  *
2352  * Collect status for all completed commands and do callback
2353  *
2354  */
2355 static uint_t
2356 mrsas_isr(struct mrsas_instance *instance)
2357 {
2358         int             need_softintr;
2359         uint32_t        producer;
2360         uint32_t        consumer;
2361         uint32_t        context;
2362         int             retval;
2363 
2364         struct mrsas_cmd        *cmd;
2365         struct mrsas_header     *hdr;
2366         struct scsi_pkt         *pkt;
2367 
2368         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
2369         ASSERT(instance);
2370         if (instance->tbolt) {
2371                 mutex_enter(&instance->chip_mtx);
2372                 if ((instance->intr_type == DDI_INTR_TYPE_FIXED) &&
2373                     !(instance->func_ptr->intr_ack(instance))) {
2374                         mutex_exit(&instance->chip_mtx);
2375                         return (DDI_INTR_UNCLAIMED);
2376                 }
2377                 retval = mr_sas_tbolt_process_outstanding_cmd(instance);
2378                 mutex_exit(&instance->chip_mtx);
2379                 return (retval);
2380         } else {
2381                 if ((instance->intr_type == DDI_INTR_TYPE_FIXED) &&
2382                     !instance->func_ptr->intr_ack(instance)) {
2383                         return (DDI_INTR_UNCLAIMED);
2384                 }
2385         }
2386 
2387         (void) ddi_dma_sync(instance->mfi_internal_dma_obj.dma_handle,
2388             0, 0, DDI_DMA_SYNC_FORCPU);
2389 
2390         if (mrsas_check_dma_handle(instance->mfi_internal_dma_obj.dma_handle)
2391             != DDI_SUCCESS) {
2392                 mrsas_fm_ereport(instance, DDI_FM_DEVICE_NO_RESPONSE);
2393                 ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST);
2394                 con_log(CL_ANN1, (CE_WARN,
2395                     "mr_sas_isr(): FMA check, returning DDI_INTR_UNCLAIMED"));
2396                 return (DDI_INTR_CLAIMED);
2397         }
2398         con_log(CL_ANN1, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
2399 
2400 #ifdef OCRDEBUG
2401         if (debug_consecutive_timeout_after_ocr_g == 1) {
2402                 con_log(CL_ANN1, (CE_NOTE,
2403                     "simulating consecutive timeout after ocr"));
2404                 return (DDI_INTR_CLAIMED);
2405         }
2406 #endif
2407 
2408         mutex_enter(&instance->completed_pool_mtx);
2409         mutex_enter(&instance->cmd_pend_mtx);
2410 
2411         producer = ddi_get32(instance->mfi_internal_dma_obj.acc_handle,
2412             instance->producer);
2413         consumer = ddi_get32(instance->mfi_internal_dma_obj.acc_handle,
2414             instance->consumer);
2415 
2416         con_log(CL_ANN, (CE_CONT, " producer %x consumer %x ",
2417             producer, consumer));
2418         if (producer == consumer) {
2419                 con_log(CL_ANN, (CE_WARN, "producer ==  consumer case"));
2420                 DTRACE_PROBE2(isr_pc_err, uint32_t, producer,
2421                     uint32_t, consumer);
2422                 mutex_exit(&instance->cmd_pend_mtx);
2423                 mutex_exit(&instance->completed_pool_mtx);
2424                 return (DDI_INTR_CLAIMED);
2425         }
2426 
2427         while (consumer != producer) {
2428                 context = ddi_get32(instance->mfi_internal_dma_obj.acc_handle,
2429                     &instance->reply_queue[consumer]);
2430                 cmd = instance->cmd_list[context];
2431 
2432                 if (cmd->sync_cmd == MRSAS_TRUE) {
2433                         hdr = (struct mrsas_header *)&cmd->frame->hdr;
2434                         if (hdr) {
2435                                 mlist_del_init(&cmd->list);
2436                         }
2437                 } else {
2438                         pkt = cmd->pkt;
2439                         if (pkt) {


2485  *                                                                            *
2486  * ************************************************************************** *
2487  */
2488 /*
2489  * get_mfi_pkt : Get a command from the free pool
2490  * After successful allocation, the caller of this routine
2491  * must clear the frame buffer (memset to zero) before
2492  * using the packet further.
2493  *
2494  * ***** Note *****
2495  * After clearing the frame buffer the context id of the
2496  * frame buffer SHOULD be restored back.
2497  */
2498 static struct mrsas_cmd *
2499 get_mfi_pkt(struct mrsas_instance *instance)
2500 {
2501         mlist_t                 *head = &instance->cmd_pool_list;
2502         struct mrsas_cmd        *cmd = NULL;
2503 
2504         mutex_enter(&instance->cmd_pool_mtx);

2505 
2506         if (!mlist_empty(head)) {
2507                 cmd = mlist_entry(head->next, struct mrsas_cmd, list);
2508                 mlist_del_init(head->next);
2509         }
2510         if (cmd != NULL) {
2511                 cmd->pkt = NULL;
2512                 cmd->retry_count_for_ocr = 0;
2513                 cmd->drv_pkt_time = 0;
2514 
2515         }
2516         mutex_exit(&instance->cmd_pool_mtx);
2517 
2518         return (cmd);
2519 }
2520 
2521 static struct mrsas_cmd *
2522 get_mfi_app_pkt(struct mrsas_instance *instance)
2523 {
2524         mlist_t                         *head = &instance->app_cmd_pool_list;
2525         struct mrsas_cmd        *cmd = NULL;
2526 
2527         mutex_enter(&instance->app_cmd_pool_mtx);

2528 
2529         if (!mlist_empty(head)) {
2530                 cmd = mlist_entry(head->next, struct mrsas_cmd, list);
2531                 mlist_del_init(head->next);
2532         }
2533         if (cmd != NULL) {
2534                 cmd->pkt = NULL;
2535                 cmd->retry_count_for_ocr = 0;
2536                 cmd->drv_pkt_time = 0;
2537         }
2538 
2539         mutex_exit(&instance->app_cmd_pool_mtx);
2540 
2541         return (cmd);
2542 }
2543 /*
2544  * return_mfi_pkt : Return a cmd to free command pool
2545  */
2546 static void
2547 return_mfi_pkt(struct mrsas_instance *instance, struct mrsas_cmd *cmd)
2548 {
2549         mutex_enter(&instance->cmd_pool_mtx);

2550         /* use mlist_add_tail for debug assistance */
2551         mlist_add_tail(&cmd->list, &instance->cmd_pool_list);
2552 
2553         mutex_exit(&instance->cmd_pool_mtx);
2554 }
2555 
2556 static void
2557 return_mfi_app_pkt(struct mrsas_instance *instance, struct mrsas_cmd *cmd)
2558 {
2559         mutex_enter(&instance->app_cmd_pool_mtx);

2560 
2561         mlist_add(&cmd->list, &instance->app_cmd_pool_list);
2562 
2563         mutex_exit(&instance->app_cmd_pool_mtx);
2564 }
2565 void
2566 push_pending_mfi_pkt(struct mrsas_instance *instance, struct mrsas_cmd *cmd)
2567 {
2568         struct scsi_pkt *pkt;
2569         struct mrsas_header     *hdr;
2570         con_log(CL_DLEVEL2, (CE_NOTE, "push_pending_pkt(): Called\n"));
2571         mutex_enter(&instance->cmd_pend_mtx);

2572         mlist_del_init(&cmd->list);
2573         mlist_add_tail(&cmd->list, &instance->cmd_pend_list);
2574         if (cmd->sync_cmd == MRSAS_TRUE) {
2575                 hdr = (struct mrsas_header *)&cmd->frame->hdr;
2576                 if (hdr) {
2577                         con_log(CL_ANN1, (CE_CONT,
2578                             "push_pending_mfi_pkt: "
2579                             "cmd %p index %x "
2580                             "time %llx",
2581                             (void *)cmd, cmd->index,
2582                             gethrtime()));
2583                         /* Wait for specified interval  */
2584                         cmd->drv_pkt_time = ddi_get16(
2585                             cmd->frame_dma_obj.acc_handle, &hdr->timeout);
2586                         if (cmd->drv_pkt_time < debug_timeout_g)
2587                                 cmd->drv_pkt_time = (uint16_t)debug_timeout_g;
2588                                 con_log(CL_ANN1, (CE_CONT,
2589                                     "push_pending_pkt(): "
2590                                     "Called IO Timeout Value %x\n",
2591                                     cmd->drv_pkt_time));


2595                             (void *) instance, drv_usectohz(MRSAS_1_SECOND));
2596                 }
2597         } else {
2598                 pkt = cmd->pkt;
2599                 if (pkt) {
2600                         con_log(CL_ANN1, (CE_CONT,
2601                             "push_pending_mfi_pkt: "
2602                             "cmd %p index %x pkt %p, "
2603                             "time %llx",
2604                             (void *)cmd, cmd->index, (void *)pkt,
2605                             gethrtime()));
2606                         cmd->drv_pkt_time = (uint16_t)debug_timeout_g;
2607                 }
2608                 if (pkt && instance->timeout_id == (timeout_id_t)-1) {
2609                         instance->timeout_id = timeout(io_timeout_checker,
2610                             (void *) instance, drv_usectohz(MRSAS_1_SECOND));
2611                 }
2612         }
2613 
2614         mutex_exit(&instance->cmd_pend_mtx);
2615 
2616 }
2617 
2618 int
2619 mrsas_print_pending_cmds(struct mrsas_instance *instance)
2620 {
2621         mlist_t *head = &instance->cmd_pend_list;
2622         mlist_t *tmp = head;
2623         struct mrsas_cmd *cmd = NULL;
2624         struct mrsas_header     *hdr;
2625         unsigned int            flag = 1;

2626         struct scsi_pkt *pkt;
2627         int saved_level;
2628         int cmd_count = 0;
2629 
2630         saved_level = debug_level_g;
2631         debug_level_g = CL_ANN1;
2632 
2633         cmn_err(CE_NOTE, "mrsas_print_pending_cmds(): Called\n");
2634 
2635         while (flag) {
2636                 mutex_enter(&instance->cmd_pend_mtx);
2637                 tmp     =       tmp->next;
2638                 if (tmp == head) {
2639                         mutex_exit(&instance->cmd_pend_mtx);
2640                         flag = 0;
2641                         con_log(CL_ANN1, (CE_CONT, "mrsas_print_pending_cmds():"
2642                             " NO MORE CMDS PENDING....\n"));
2643                         break;
2644                 } else {
2645                         cmd = mlist_entry(tmp, struct mrsas_cmd, list);
2646                         mutex_exit(&instance->cmd_pend_mtx);
2647                         if (cmd) {
2648                                 if (cmd->sync_cmd == MRSAS_TRUE) {
2649                                         hdr = (struct mrsas_header *)
2650                                             &cmd->frame->hdr;
2651                                         if (hdr) {
2652                                                 con_log(CL_ANN1, (CE_CONT,
2653                                                     "print: cmd %p index 0x%x "
2654                                                     "drv_pkt_time 0x%x (NO-PKT)"
2655                                                     " hdr %p\n", (void *)cmd,
2656                                                     cmd->index,
2657                                                     cmd->drv_pkt_time,
2658                                                     (void *)hdr));
2659                                         }
2660                                 } else {
2661                                         pkt = cmd->pkt;
2662                                         if (pkt) {
2663                                         con_log(CL_ANN1, (CE_CONT,
2664                                             "print: cmd %p index 0x%x "
2665                                             "drv_pkt_time 0x%x pkt %p \n",
2666                                             (void *)cmd, cmd->index,
2667                                             cmd->drv_pkt_time, (void *)pkt));
2668                                         }
2669                                 }
2670 
2671                                 if (++cmd_count == 1) {
2672                                         mrsas_print_cmd_details(instance, cmd,
2673                                             0xDD);
2674                                 } else {
2675                                         mrsas_print_cmd_details(instance, cmd,
2676                                             1);
2677                                 }
2678 
2679                         }
2680                 }
2681         }
2682         con_log(CL_ANN1, (CE_CONT, "mrsas_print_pending_cmds(): Done\n"));
2683 
2684 
2685         debug_level_g = saved_level;
2686 
2687         return (DDI_SUCCESS);
2688 }
2689 
2690 
2691 int
2692 mrsas_complete_pending_cmds(struct mrsas_instance *instance)
2693 {
2694 
2695         struct mrsas_cmd *cmd = NULL;
2696         struct scsi_pkt *pkt;
2697         struct mrsas_header *hdr;
2698 
2699         struct mlist_head               *pos, *next;
2700 
2701         con_log(CL_ANN1, (CE_NOTE,
2702             "mrsas_complete_pending_cmds(): Called"));
2703 
2704         mutex_enter(&instance->cmd_pend_mtx);
2705         mlist_for_each_safe(pos, next, &instance->cmd_pend_list) {
2706                 cmd = mlist_entry(pos, struct mrsas_cmd, list);
2707                 if (cmd) {
2708                         pkt = cmd->pkt;
2709                         if (pkt) { /* for IO */
2710                                 if (((pkt->pkt_flags & FLAG_NOINTR)
2711                                     == 0) && pkt->pkt_comp) {
2712                                         pkt->pkt_reason
2713                                             = CMD_DEV_GONE;
2714                                         pkt->pkt_statistics
2715                                             = STAT_DISCON;
2716                                         con_log(CL_ANN1, (CE_CONT,
2717                                             "fail and posting to scsa "
2718                                             "cmd %p index %x"
2719                                             " pkt %p "
2720                                             "time : %llx",
2721                                             (void *)cmd, cmd->index,
2722                                             (void *)pkt, gethrtime()));
2723                                         (*pkt->pkt_comp)(pkt);
2724                                 }
2725                         } else { /* for DCMDS */
2726                                 if (cmd->sync_cmd == MRSAS_TRUE) {
2727                                 hdr = (struct mrsas_header *)&cmd->frame->hdr;
2728                                 con_log(CL_ANN1, (CE_CONT,
2729                                     "posting invalid status to application "
2730                                     "cmd %p index %x"
2731                                     " hdr %p "
2732                                     "time : %llx",
2733                                     (void *)cmd, cmd->index,
2734                                     (void *)hdr, gethrtime()));
2735                                 hdr->cmd_status = MFI_STAT_INVALID_STATUS;
2736                                 complete_cmd_in_sync_mode(instance, cmd);
2737                                 }
2738                         }
2739                         mlist_del_init(&cmd->list);
2740                 } else {
2741                         con_log(CL_ANN1, (CE_CONT,
2742                             "mrsas_complete_pending_cmds:"
2743                             "NULL command\n"));
2744                 }
2745                 con_log(CL_ANN1, (CE_CONT,
2746                     "mrsas_complete_pending_cmds:"
2747                     "looping for more commands\n"));
2748         }
2749         mutex_exit(&instance->cmd_pend_mtx);
2750 
2751         con_log(CL_ANN1, (CE_CONT, "mrsas_complete_pending_cmds(): DONE\n"));
2752         return (DDI_SUCCESS);
2753 }
2754 
2755 void
2756 mrsas_print_cmd_details(struct mrsas_instance *instance, struct mrsas_cmd *cmd,
2757     int detail)
2758 {
2759         struct scsi_pkt *pkt = cmd->pkt;
2760         Mpi2RaidSCSIIORequest_t *scsi_io = cmd->scsi_io_request;
2761         int i;
2762         int saved_level;
2763         ddi_acc_handle_t acc_handle =
2764             instance->mpi2_frame_pool_dma_obj.acc_handle;
2765 
2766         if (detail == 0xDD) {
2767                 saved_level = debug_level_g;
2768                 debug_level_g = CL_ANN1;
2769         }
2770 
2771 
2772         if (instance->tbolt) {
2773                 con_log(CL_ANN1, (CE_CONT, "print_cmd_details: cmd %p "
2774                     "cmd->index 0x%x SMID 0x%x timer 0x%x sec\n",
2775                     (void *)cmd, cmd->index, cmd->SMID, cmd->drv_pkt_time));
2776         } else {
2777                 con_log(CL_ANN1, (CE_CONT, "print_cmd_details: cmd %p "
2778                     "cmd->index 0x%x timer 0x%x sec\n",
2779                     (void *)cmd, cmd->index, cmd->drv_pkt_time));
2780         }
2781 
2782         if (pkt) {
2783                 con_log(CL_ANN1, (CE_CONT, "scsi_pkt CDB[0]=0x%x",
2784                     pkt->pkt_cdbp[0]));
2785         } else {
2786                 con_log(CL_ANN1, (CE_CONT, "NO-PKT"));
2787         }
2788 
2789         if ((detail == 0xDD) && instance->tbolt) {
2790                 con_log(CL_ANN1, (CE_CONT, "RAID_SCSI_IO_REQUEST\n"));
2791                 con_log(CL_ANN1, (CE_CONT, "DevHandle=0x%X Function=0x%X "
2792                     "IoFlags=0x%X SGLFlags=0x%X DataLength=0x%X\n",
2793                     ddi_get16(acc_handle, &scsi_io->DevHandle),
2794                     ddi_get8(acc_handle, &scsi_io->Function),
2795                     ddi_get16(acc_handle, &scsi_io->IoFlags),
2796                     ddi_get16(acc_handle, &scsi_io->SGLFlags),
2797                     ddi_get32(acc_handle, &scsi_io->DataLength)));
2798 
2799                 for (i = 0; i < 32; i++) {
2800                         con_log(CL_ANN1, (CE_CONT, "CDB[%d]=0x%x ", i,
2801                             ddi_get8(acc_handle, &scsi_io->CDB.CDB32[i])));
2802                 }
2803 
2804                 con_log(CL_ANN1, (CE_CONT, "RAID-CONTEXT\n"));
2805                 con_log(CL_ANN1, (CE_CONT, "status=0x%X extStatus=0x%X "
2806                     "ldTargetId=0x%X timeoutValue=0x%X regLockFlags=0x%X "
2807                     "RAIDFlags=0x%X regLockRowLBA=0x%" PRIu64
2808                     " regLockLength=0x%X spanArm=0x%X\n",
2809                     ddi_get8(acc_handle, &scsi_io->RaidContext.status),
2810                     ddi_get8(acc_handle, &scsi_io->RaidContext.extStatus),
2811                     ddi_get16(acc_handle, &scsi_io->RaidContext.ldTargetId),
2812                     ddi_get16(acc_handle, &scsi_io->RaidContext.timeoutValue),
2813                     ddi_get8(acc_handle, &scsi_io->RaidContext.regLockFlags),
2814                     ddi_get8(acc_handle, &scsi_io->RaidContext.RAIDFlags),
2815                     ddi_get64(acc_handle, &scsi_io->RaidContext.regLockRowLBA),
2816                     ddi_get32(acc_handle, &scsi_io->RaidContext.regLockLength),
2817                     ddi_get8(acc_handle, &scsi_io->RaidContext.spanArm)));
2818         }
2819 
2820         if (detail == 0xDD) {
2821                 debug_level_g = saved_level;
2822         }
2823 }
2824 
2825 
2826 int
2827 mrsas_issue_pending_cmds(struct mrsas_instance *instance)
2828 {
2829         mlist_t *head   =       &instance->cmd_pend_list;
2830         mlist_t *tmp    =       head->next;
2831         struct mrsas_cmd *cmd = NULL;
2832         struct scsi_pkt *pkt;
2833 
2834         con_log(CL_ANN1, (CE_NOTE, "mrsas_issue_pending_cmds(): Called"));
2835         while (tmp != head) {
2836                 mutex_enter(&instance->cmd_pend_mtx);
2837                 cmd = mlist_entry(tmp, struct mrsas_cmd, list);
2838                 tmp = tmp->next;
2839                 mutex_exit(&instance->cmd_pend_mtx);
2840                 if (cmd) {
2841                         con_log(CL_ANN1, (CE_CONT,
2842                             "mrsas_issue_pending_cmds(): "
2843                             "Got a cmd: cmd %p index 0x%x drv_pkt_time 0x%x ",
2844                             (void *)cmd, cmd->index, cmd->drv_pkt_time));
2845 
2846                         /* Reset command timeout value */
2847                         if (cmd->drv_pkt_time < debug_timeout_g)
2848                                 cmd->drv_pkt_time = (uint16_t)debug_timeout_g;
2849 
2850                         cmd->retry_count_for_ocr++;
2851 
2852                         cmn_err(CE_CONT, "cmd retry count = %d\n",
2853                             cmd->retry_count_for_ocr);
2854 
2855                         if (cmd->retry_count_for_ocr > IO_RETRY_COUNT) {
2856                                 cmn_err(CE_WARN, "mrsas_issue_pending_cmds(): "
2857                                     "cmd->retry_count exceeded limit >%d\n",
2858                                     IO_RETRY_COUNT);
2859                                 mrsas_print_cmd_details(instance, cmd, 0xDD);
2860 
2861                                 cmn_err(CE_WARN,
2862                                     "mrsas_issue_pending_cmds():"
2863                                     "Calling KILL Adapter\n");
2864                                 if (instance->tbolt)
2865                                         mrsas_tbolt_kill_adapter(instance);
2866                                 else
2867                                         (void) mrsas_kill_adapter(instance);
2868                                 return (DDI_FAILURE);
2869                         }
2870 
2871                         pkt = cmd->pkt;
2872                         if (pkt) {
2873                                 con_log(CL_ANN1, (CE_CONT,
2874                                     "PENDING PKT-CMD ISSUE: cmd %p index %x "
2875                                     "pkt %p time %llx",
2876                                     (void *)cmd, cmd->index,
2877                                     (void *)pkt,
2878                                     gethrtime()));
2879 
2880                         } else {
2881                                 cmn_err(CE_CONT,
2882                                     "mrsas_issue_pending_cmds(): NO-PKT, "
2883                                     "cmd %p index 0x%x drv_pkt_time 0x%x ",
2884                                     (void *)cmd, cmd->index, cmd->drv_pkt_time);
2885                         }
2886 
2887 
2888                         if (cmd->sync_cmd == MRSAS_TRUE) {
2889                                 cmn_err(CE_CONT, "mrsas_issue_pending_cmds(): "
2890                                     "SYNC_CMD == TRUE \n");
2891                                 instance->func_ptr->issue_cmd_in_sync_mode(
2892                                     instance, cmd);
2893                         } else {
2894                                 instance->func_ptr->issue_cmd(cmd, instance);
2895                         }
2896                 } else {
2897                         con_log(CL_ANN1, (CE_CONT,
2898                             "mrsas_issue_pending_cmds: NULL command\n"));
2899                 }
2900                 con_log(CL_ANN1, (CE_CONT,
2901                     "mrsas_issue_pending_cmds:"
2902                     "looping for more commands"));
2903         }
2904         con_log(CL_ANN1, (CE_CONT, "mrsas_issue_pending_cmds(): DONE\n"));
2905         return (DDI_SUCCESS);
2906 }
2907 
2908 
2909 
2910 /*
2911  * destroy_mfi_frame_pool
2912  */
2913 void
2914 destroy_mfi_frame_pool(struct mrsas_instance *instance)
2915 {
2916         int             i;
2917         uint32_t        max_cmd = instance->max_fw_cmds;
2918 
2919         struct mrsas_cmd        *cmd;
2920 
2921         /* return all frames to pool */

2922 
2923         for (i = 0; i < max_cmd; i++) {
2924 
2925                 cmd = instance->cmd_list[i];
2926 
2927                 if (cmd->frame_dma_obj_status == DMA_OBJ_ALLOCATED)
2928                         (void) mrsas_free_dma_obj(instance, cmd->frame_dma_obj);
2929 
2930                 cmd->frame_dma_obj_status  = DMA_OBJ_FREED;
2931         }
2932 
2933 }
2934 
2935 /*
2936  * create_mfi_frame_pool
2937  */
2938 int
2939 create_mfi_frame_pool(struct mrsas_instance *instance)
2940 {
2941         int             i = 0;
2942         int             cookie_cnt;
2943         uint16_t        max_cmd;
2944         uint16_t        sge_sz;
2945         uint32_t        sgl_sz;
2946         uint32_t        tot_frame_size;
2947         struct mrsas_cmd        *cmd;
2948         int                     retval = DDI_SUCCESS;
2949 
2950         max_cmd = instance->max_fw_cmds;

2951         sge_sz  = sizeof (struct mrsas_sge_ieee);

2952         /* calculated the number of 64byte frames required for SGL */
2953         sgl_sz          = sge_sz * instance->max_num_sge;
2954         tot_frame_size  = sgl_sz + MRMFI_FRAME_SIZE + SENSE_LENGTH;
2955 
2956         con_log(CL_DLEVEL3, (CE_NOTE, "create_mfi_frame_pool: "
2957             "sgl_sz %x tot_frame_size %x", sgl_sz, tot_frame_size));
2958 
2959         while (i < max_cmd) {
2960                 cmd = instance->cmd_list[i];
2961 
2962                 cmd->frame_dma_obj.size      = tot_frame_size;
2963                 cmd->frame_dma_obj.dma_attr = mrsas_generic_dma_attr;
2964                 cmd->frame_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
2965                 cmd->frame_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
2966                 cmd->frame_dma_obj.dma_attr.dma_attr_sgllen = 1;
2967                 cmd->frame_dma_obj.dma_attr.dma_attr_align = 64;
2968 

2969                 cookie_cnt = mrsas_alloc_dma_obj(instance, &cmd->frame_dma_obj,
2970                     (uchar_t)DDI_STRUCTURE_LE_ACC);
2971 
2972                 if (cookie_cnt == -1 || cookie_cnt > 1) {
2973                         cmn_err(CE_WARN,
2974                             "create_mfi_frame_pool: could not alloc.");
2975                         retval = DDI_FAILURE;
2976                         goto mrsas_undo_frame_pool;
2977                 }
2978 
2979                 bzero(cmd->frame_dma_obj.buffer, tot_frame_size);
2980 
2981                 cmd->frame_dma_obj_status = DMA_OBJ_ALLOCATED;
2982                 cmd->frame = (union mrsas_frame *)cmd->frame_dma_obj.buffer;
2983                 cmd->frame_phys_addr =
2984                     cmd->frame_dma_obj.dma_cookie[0].dmac_address;
2985 
2986                 cmd->sense = (uint8_t *)(((unsigned long)
2987                     cmd->frame_dma_obj.buffer) +
2988                     tot_frame_size - SENSE_LENGTH);
2989                 cmd->sense_phys_addr =
2990                     cmd->frame_dma_obj.dma_cookie[0].dmac_address +
2991                     tot_frame_size - SENSE_LENGTH;
2992 
2993                 if (!cmd->frame || !cmd->sense) {
2994                         cmn_err(CE_WARN,
2995                             "mr_sas: pci_pool_alloc failed");
2996                         retval = ENOMEM;
2997                         goto mrsas_undo_frame_pool;
2998                 }
2999 
3000                 ddi_put32(cmd->frame_dma_obj.acc_handle,
3001                     &cmd->frame->io.context, cmd->index);
3002                 i++;
3003 
3004                 con_log(CL_DLEVEL3, (CE_NOTE, "[%x]-%x",
3005                     cmd->index, cmd->frame_phys_addr));
3006         }
3007 
3008         return (DDI_SUCCESS);
3009 
3010 mrsas_undo_frame_pool:
3011         if (i > 0)
3012                 destroy_mfi_frame_pool(instance);
3013 
3014         return (retval);
3015 }
3016 
3017 /*
3018  * free_additional_dma_buffer
3019  */
3020 static void
3021 free_additional_dma_buffer(struct mrsas_instance *instance)
3022 {
3023         if (instance->mfi_internal_dma_obj.status == DMA_OBJ_ALLOCATED) {
3024                 (void) mrsas_free_dma_obj(instance,
3025                     instance->mfi_internal_dma_obj);
3026                 instance->mfi_internal_dma_obj.status = DMA_OBJ_FREED;
3027         }
3028 
3029         if (instance->mfi_evt_detail_obj.status == DMA_OBJ_ALLOCATED) {
3030                 (void) mrsas_free_dma_obj(instance,
3031                     instance->mfi_evt_detail_obj);
3032                 instance->mfi_evt_detail_obj.status = DMA_OBJ_FREED;
3033         }
3034 }


3037  * alloc_additional_dma_buffer
3038  */
3039 static int
3040 alloc_additional_dma_buffer(struct mrsas_instance *instance)
3041 {
3042         uint32_t        reply_q_sz;
3043         uint32_t        internal_buf_size = PAGESIZE*2;
3044 
3045         /* max cmds plus 1 + producer & consumer */
3046         reply_q_sz = sizeof (uint32_t) * (instance->max_fw_cmds + 1 + 2);
3047 
3048         instance->mfi_internal_dma_obj.size = internal_buf_size;
3049         instance->mfi_internal_dma_obj.dma_attr      = mrsas_generic_dma_attr;
3050         instance->mfi_internal_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
3051         instance->mfi_internal_dma_obj.dma_attr.dma_attr_count_max =
3052             0xFFFFFFFFU;
3053         instance->mfi_internal_dma_obj.dma_attr.dma_attr_sgllen      = 1;
3054 
3055         if (mrsas_alloc_dma_obj(instance, &instance->mfi_internal_dma_obj,
3056             (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
3057                 cmn_err(CE_WARN,
3058                     "mr_sas: could not alloc reply queue");
3059                 return (DDI_FAILURE);
3060         }
3061 
3062         bzero(instance->mfi_internal_dma_obj.buffer, internal_buf_size);
3063 
3064         instance->mfi_internal_dma_obj.status |= DMA_OBJ_ALLOCATED;
3065 
3066         instance->producer = (uint32_t *)((unsigned long)
3067             instance->mfi_internal_dma_obj.buffer);
3068         instance->consumer = (uint32_t *)((unsigned long)
3069             instance->mfi_internal_dma_obj.buffer + 4);
3070         instance->reply_queue = (uint32_t *)((unsigned long)
3071             instance->mfi_internal_dma_obj.buffer + 8);
3072         instance->internal_buf = (caddr_t)(((unsigned long)
3073             instance->mfi_internal_dma_obj.buffer) + reply_q_sz + 8);
3074         instance->internal_buf_dmac_add =
3075             instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address +
3076             (reply_q_sz + 8);
3077         instance->internal_buf_size = internal_buf_size -
3078             (reply_q_sz + 8);
3079 
3080         /* allocate evt_detail */
3081         instance->mfi_evt_detail_obj.size = sizeof (struct mrsas_evt_detail);
3082         instance->mfi_evt_detail_obj.dma_attr = mrsas_generic_dma_attr;
3083         instance->mfi_evt_detail_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
3084         instance->mfi_evt_detail_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
3085         instance->mfi_evt_detail_obj.dma_attr.dma_attr_sgllen = 1;
3086         instance->mfi_evt_detail_obj.dma_attr.dma_attr_align = 1;
3087 
3088         if (mrsas_alloc_dma_obj(instance, &instance->mfi_evt_detail_obj,
3089             (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
3090                 cmn_err(CE_WARN, "alloc_additional_dma_buffer: "
3091                     "could not allocate data transfer buffer.");
3092                 goto mrsas_undo_internal_buff;
3093         }
3094 
3095         bzero(instance->mfi_evt_detail_obj.buffer,
3096             sizeof (struct mrsas_evt_detail));
3097 
3098         instance->mfi_evt_detail_obj.status |= DMA_OBJ_ALLOCATED;
3099 
3100         return (DDI_SUCCESS);
3101 
3102 mrsas_undo_internal_buff:
3103         if (instance->mfi_internal_dma_obj.status == DMA_OBJ_ALLOCATED) {
3104                 (void) mrsas_free_dma_obj(instance,
3105                     instance->mfi_internal_dma_obj);
3106                 instance->mfi_internal_dma_obj.status = DMA_OBJ_FREED;
3107         }
3108 
3109         return (DDI_FAILURE);
3110 }
3111 
3112 
3113 void
3114 mrsas_free_cmd_pool(struct mrsas_instance *instance)


3115 {
3116         int             i;
3117         uint32_t        max_cmd;
3118         size_t          sz;
3119 
3120         /* already freed */
3121         if (instance->cmd_list == NULL) {
3122                 return;
3123         }
3124 
3125         max_cmd = instance->max_fw_cmds;
3126 
3127         /* size of cmd_list array */
3128         sz = sizeof (struct mrsas_cmd *) * max_cmd;
3129 
3130         /* First free each cmd */
3131         for (i = 0; i < max_cmd; i++) {
3132                 if (instance->cmd_list[i] != NULL) {
3133                         kmem_free(instance->cmd_list[i],
3134                             sizeof (struct mrsas_cmd));
3135                 }
3136 
3137                 instance->cmd_list[i] = NULL;
3138         }
3139 
3140         /* Now, free cmd_list array */
3141         if (instance->cmd_list != NULL)
3142                 kmem_free(instance->cmd_list, sz);
3143 
3144         instance->cmd_list = NULL;
3145 
3146         INIT_LIST_HEAD(&instance->cmd_pool_list);

3147         INIT_LIST_HEAD(&instance->cmd_pend_list);
3148         if (instance->tbolt) {
3149                 INIT_LIST_HEAD(&instance->cmd_app_pool_list);
3150         } else {
3151                 INIT_LIST_HEAD(&instance->app_cmd_pool_list);
3152         }
3153 
3154 }
3155 
3156 
3157 /*
3158  * mrsas_alloc_cmd_pool
3159  */
3160 int
3161 mrsas_alloc_cmd_pool(struct mrsas_instance *instance)
3162 {
3163         int             i;
3164         int             count;
3165         uint32_t        max_cmd;
3166         uint32_t        reserve_cmd;
3167         size_t          sz;
3168 
3169         struct mrsas_cmd        *cmd;
3170 
3171         max_cmd = instance->max_fw_cmds;
3172         con_log(CL_ANN1, (CE_NOTE, "mrsas_alloc_cmd_pool: "
3173             "max_cmd %x", max_cmd));
3174 


3175 
3176         sz = sizeof (struct mrsas_cmd *) * max_cmd;
3177 
3178         /*
3179          * instance->cmd_list is an array of struct mrsas_cmd pointers.
3180          * Allocate the dynamic array first and then allocate individual
3181          * commands.
3182          */
3183         instance->cmd_list = kmem_zalloc(sz, KM_SLEEP);
3184         ASSERT(instance->cmd_list);
3185 
3186         /* create a frame pool and assign one frame to each cmd */
3187         for (count = 0; count < max_cmd; count++) {
3188                 instance->cmd_list[count] =
3189                     kmem_zalloc(sizeof (struct mrsas_cmd), KM_SLEEP);
3190                 ASSERT(instance->cmd_list[count]);
3191         }
3192 
3193         /* add all the commands to command pool */
3194 
3195         INIT_LIST_HEAD(&instance->cmd_pool_list);
3196         INIT_LIST_HEAD(&instance->cmd_pend_list);


3197         INIT_LIST_HEAD(&instance->app_cmd_pool_list);
3198 
3199         reserve_cmd = MRSAS_APP_RESERVED_CMDS;
3200 
3201         for (i = 0; i < reserve_cmd; i++) {
3202                 cmd = instance->cmd_list[i];
3203                 cmd->index = i;
3204                 mlist_add_tail(&cmd->list, &instance->app_cmd_pool_list);
3205         }
3206 
3207 


3208         for (i = reserve_cmd; i < max_cmd; i++) {
3209                 cmd = instance->cmd_list[i];
3210                 cmd->index = i;
3211                 mlist_add_tail(&cmd->list, &instance->cmd_pool_list);
3212         }
3213 
3214         return (DDI_SUCCESS);


3215 
3216 mrsas_undo_cmds:
3217         if (count > 0) {
3218                 /* free each cmd */
3219                 for (i = 0; i < count; i++) {
3220                         if (instance->cmd_list[i] != NULL) {
3221                                 kmem_free(instance->cmd_list[i],
3222                                     sizeof (struct mrsas_cmd));
3223                         }
3224                         instance->cmd_list[i] = NULL;
3225                 }
3226         }
3227 
3228 mrsas_undo_cmd_list:
3229         if (instance->cmd_list != NULL)
3230                 kmem_free(instance->cmd_list, sz);
3231         instance->cmd_list = NULL;
3232 
3233         return (DDI_FAILURE);
3234 }
3235 
3236 
3237 /*
3238  * free_space_for_mfi
3239  */
3240 static void
3241 free_space_for_mfi(struct mrsas_instance *instance)
3242 {
3243 
3244         /* already freed */
3245         if (instance->cmd_list == NULL) {
3246                 return;
3247         }
3248 
3249         /* Free additional dma buffer */
3250         free_additional_dma_buffer(instance);
3251 
3252         /* Free the MFI frame pool */
3253         destroy_mfi_frame_pool(instance);
3254 
3255         /* Free all the commands in the cmd_list */
3256         /* Free the cmd_list buffer itself */
3257         mrsas_free_cmd_pool(instance);
3258 }
3259 
3260 /*
3261  * alloc_space_for_mfi
3262  */
3263 static int
3264 alloc_space_for_mfi(struct mrsas_instance *instance)
3265 {
3266         /* Allocate command pool (memory for cmd_list & individual commands) */
3267         if (mrsas_alloc_cmd_pool(instance)) {
3268                 cmn_err(CE_WARN, "error creating cmd pool");
3269                 return (DDI_FAILURE);
3270         }
3271 
3272         /* Allocate MFI Frame pool */
3273         if (create_mfi_frame_pool(instance)) {
3274                 cmn_err(CE_WARN, "error creating frame DMA pool");
3275                 goto mfi_undo_cmd_pool;
3276         }
3277 
3278         /* Allocate additional DMA buffer */
3279         if (alloc_additional_dma_buffer(instance)) {
3280                 cmn_err(CE_WARN, "error creating frame DMA pool");
3281                 goto mfi_undo_frame_pool;
3282         }
3283 
3284         return (DDI_SUCCESS);
3285 
3286 mfi_undo_frame_pool:
3287         destroy_mfi_frame_pool(instance);
3288 
3289 mfi_undo_cmd_pool:
3290         mrsas_free_cmd_pool(instance);
3291 
3292         return (DDI_FAILURE);
3293 }
3294 
3295 
3296 
3297 /*
3298  * get_ctrl_info
3299  */
3300 static int
3301 get_ctrl_info(struct mrsas_instance *instance,
3302     struct mrsas_ctrl_info *ctrl_info)
3303 {
3304         int     ret = 0;
3305 
3306         struct mrsas_cmd                *cmd;
3307         struct mrsas_dcmd_frame *dcmd;
3308         struct mrsas_ctrl_info  *ci;
3309 
3310         if (instance->tbolt) {
3311                 cmd = get_raid_msg_mfi_pkt(instance);
3312         } else {
3313                 cmd = get_mfi_pkt(instance);
3314         }
3315 
3316         if (!cmd) {
3317                 con_log(CL_ANN, (CE_WARN,
3318                     "Failed to get a cmd for ctrl info"));
3319                 DTRACE_PROBE2(info_mfi_err, uint16_t, instance->fw_outstanding,
3320                     uint16_t, instance->max_fw_cmds);
3321                 return (DDI_FAILURE);
3322         }
3323 
3324         /* Clear the frame buffer and assign back the context id */
3325         (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
3326         ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
3327             cmd->index);
3328 
3329         dcmd = &cmd->frame->dcmd;
3330 
3331         ci = (struct mrsas_ctrl_info *)instance->internal_buf;
3332 
3333         if (!ci) {
3334                 cmn_err(CE_WARN,
3335                     "Failed to alloc mem for ctrl info");
3336                 return_mfi_pkt(instance, cmd);
3337                 return (DDI_FAILURE);
3338         }
3339 
3340         (void) memset(ci, 0, sizeof (struct mrsas_ctrl_info));
3341 
3342         /* for( i = 0; i < DCMD_MBOX_SZ; i++ ) dcmd->mbox.b[i] = 0; */
3343         (void) memset(dcmd->mbox.b, 0, DCMD_MBOX_SZ);
3344 
3345         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd, MFI_CMD_OP_DCMD);
3346         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd_status,
3347             MFI_CMD_STATUS_POLL_MODE);
3348         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->sge_count, 1);
3349         ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->flags,
3350             MFI_FRAME_DIR_READ);
3351         ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->timeout, 0);
3352         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->data_xfer_len,
3353             sizeof (struct mrsas_ctrl_info));
3354         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->opcode,
3355             MR_DCMD_CTRL_GET_INFO);
3356         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].phys_addr,
3357             instance->internal_buf_dmac_add);
3358         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].length,
3359             sizeof (struct mrsas_ctrl_info));
3360 
3361         cmd->frame_count = 1;
3362 
3363         if (instance->tbolt) {
3364                 mr_sas_tbolt_build_mfi_cmd(instance, cmd);
3365         }
3366 
3367         if (!instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) {
3368                 ret = 0;
3369 
3370                 ctrl_info->max_request_size = ddi_get32(
3371                     cmd->frame_dma_obj.acc_handle, &ci->max_request_size);
3372 
3373                 ctrl_info->ld_present_count = ddi_get16(
3374                     cmd->frame_dma_obj.acc_handle, &ci->ld_present_count);
3375 
3376                 ctrl_info->properties.on_off_properties = ddi_get32(
3377                     cmd->frame_dma_obj.acc_handle,
3378                     &ci->properties.on_off_properties);

3379                 ddi_rep_get8(cmd->frame_dma_obj.acc_handle,
3380                     (uint8_t *)(ctrl_info->product_name),
3381                     (uint8_t *)(ci->product_name), 80 * sizeof (char),
3382                     DDI_DEV_AUTOINCR);
3383                 /* should get more members of ci with ddi_get when needed */
3384         } else {
3385                 cmn_err(CE_WARN, "get_ctrl_info: Ctrl info failed");
3386                 ret = -1;
3387         }
3388 
3389         if (mrsas_common_check(instance, cmd) != DDI_SUCCESS) {
3390                 ret = -1;
3391         }
3392         if (instance->tbolt) {
3393                 return_raid_msg_mfi_pkt(instance, cmd);
3394         } else {
3395                 return_mfi_pkt(instance, cmd);
3396         }
3397 
3398         return (ret);
3399 }
3400 
3401 /*
3402  * abort_aen_cmd
3403  */
3404 static int
3405 abort_aen_cmd(struct mrsas_instance *instance,
3406     struct mrsas_cmd *cmd_to_abort)
3407 {
3408         int     ret = 0;
3409 
3410         struct mrsas_cmd                *cmd;
3411         struct mrsas_abort_frame        *abort_fr;
3412 
3413         con_log(CL_ANN1, (CE_NOTE, "chkpnt: abort_aen:%d", __LINE__));
3414 
3415         if (instance->tbolt) {
3416                 cmd = get_raid_msg_mfi_pkt(instance);
3417         } else {
3418                 cmd = get_mfi_pkt(instance);
3419         }
3420 
3421         if (!cmd) {
3422                 con_log(CL_ANN1, (CE_WARN,
3423                     "abort_aen_cmd():Failed to get a cmd for abort_aen_cmd"));
3424                 DTRACE_PROBE2(abort_mfi_err, uint16_t, instance->fw_outstanding,
3425                     uint16_t, instance->max_fw_cmds);
3426                 return (DDI_FAILURE);
3427         }
3428 
3429         /* Clear the frame buffer and assign back the context id */
3430         (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
3431         ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
3432             cmd->index);
3433 
3434         abort_fr = &cmd->frame->abort;
3435 
3436         /* prepare and issue the abort frame */
3437         ddi_put8(cmd->frame_dma_obj.acc_handle,
3438             &abort_fr->cmd, MFI_CMD_OP_ABORT);
3439         ddi_put8(cmd->frame_dma_obj.acc_handle, &abort_fr->cmd_status,
3440             MFI_CMD_STATUS_SYNC_MODE);
3441         ddi_put16(cmd->frame_dma_obj.acc_handle, &abort_fr->flags, 0);
3442         ddi_put32(cmd->frame_dma_obj.acc_handle, &abort_fr->abort_context,
3443             cmd_to_abort->index);
3444         ddi_put32(cmd->frame_dma_obj.acc_handle,
3445             &abort_fr->abort_mfi_phys_addr_lo, cmd_to_abort->frame_phys_addr);
3446         ddi_put32(cmd->frame_dma_obj.acc_handle,
3447             &abort_fr->abort_mfi_phys_addr_hi, 0);
3448 
3449         instance->aen_cmd->abort_aen = 1;
3450 

3451         cmd->frame_count = 1;
3452 
3453         if (instance->tbolt) {
3454                 mr_sas_tbolt_build_mfi_cmd(instance, cmd);
3455         }
3456 
3457         if (instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) {
3458                 con_log(CL_ANN1, (CE_WARN,
3459                     "abort_aen_cmd: issue_cmd_in_poll_mode failed"));
3460                 ret = -1;
3461         } else {
3462                 ret = 0;
3463         }
3464 
3465         instance->aen_cmd->abort_aen = 1;
3466         instance->aen_cmd = 0;
3467 
3468         if (instance->tbolt) {
3469                 return_raid_msg_mfi_pkt(instance, cmd);
3470         } else {
3471                 return_mfi_pkt(instance, cmd);
3472         }
3473 
3474         atomic_add_16(&instance->fw_outstanding, (-1));
3475 
3476         return (ret);
3477 }
3478 
3479 



3480 static int
3481 mrsas_build_init_cmd(struct mrsas_instance *instance,
3482     struct mrsas_cmd **cmd_ptr)
3483 {
3484         struct mrsas_cmd                *cmd;

3485         struct mrsas_init_frame         *init_frame;
3486         struct mrsas_init_queue_info    *initq_info;
3487         struct mrsas_drv_ver            drv_ver_info;
3488 





3489 




3490         /*

















3491          * Prepare a init frame. Note the init frame points to queue info
3492          * structure. Each frame has SGL allocated after first 64 bytes. For
3493          * this frame - since we don't need any SGL - we use SGL's space as
3494          * queue info structure
3495          */
3496         cmd = *cmd_ptr;

3497 
3498 
3499         /* Clear the frame buffer and assign back the context id */
3500         (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
3501         ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
3502             cmd->index);
3503 
3504         init_frame = (struct mrsas_init_frame *)cmd->frame;
3505         initq_info = (struct mrsas_init_queue_info *)
3506             ((unsigned long)init_frame + 64);
3507 
3508         (void) memset(init_frame, 0, MRMFI_FRAME_SIZE);
3509         (void) memset(initq_info, 0, sizeof (struct mrsas_init_queue_info));
3510 
3511         ddi_put32(cmd->frame_dma_obj.acc_handle, &initq_info->init_flags, 0);
3512 
3513         ddi_put32(cmd->frame_dma_obj.acc_handle,
3514             &initq_info->reply_queue_entries, instance->max_fw_cmds + 1);
3515 
3516         ddi_put32(cmd->frame_dma_obj.acc_handle,
3517             &initq_info->producer_index_phys_addr_hi, 0);
3518         ddi_put32(cmd->frame_dma_obj.acc_handle,


3525             &initq_info->consumer_index_phys_addr_lo,
3526             instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address + 4);
3527 
3528         ddi_put32(cmd->frame_dma_obj.acc_handle,
3529             &initq_info->reply_queue_start_phys_addr_hi, 0);
3530         ddi_put32(cmd->frame_dma_obj.acc_handle,
3531             &initq_info->reply_queue_start_phys_addr_lo,
3532             instance->mfi_internal_dma_obj.dma_cookie[0].dmac_address + 8);
3533 
3534         ddi_put8(cmd->frame_dma_obj.acc_handle,
3535             &init_frame->cmd, MFI_CMD_OP_INIT);
3536         ddi_put8(cmd->frame_dma_obj.acc_handle, &init_frame->cmd_status,
3537             MFI_CMD_STATUS_POLL_MODE);
3538         ddi_put16(cmd->frame_dma_obj.acc_handle, &init_frame->flags, 0);
3539         ddi_put32(cmd->frame_dma_obj.acc_handle,
3540             &init_frame->queue_info_new_phys_addr_lo,
3541             cmd->frame_phys_addr + 64);
3542         ddi_put32(cmd->frame_dma_obj.acc_handle,
3543             &init_frame->queue_info_new_phys_addr_hi, 0);
3544 
3545 
3546         /* fill driver version information */
3547         fill_up_drv_ver(&drv_ver_info);
3548 
3549         /* allocate the driver version data transfer buffer */
3550         instance->drv_ver_dma_obj.size = sizeof (drv_ver_info.drv_ver);
3551         instance->drv_ver_dma_obj.dma_attr = mrsas_generic_dma_attr;
3552         instance->drv_ver_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
3553         instance->drv_ver_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
3554         instance->drv_ver_dma_obj.dma_attr.dma_attr_sgllen = 1;
3555         instance->drv_ver_dma_obj.dma_attr.dma_attr_align = 1;
3556 
3557         if (mrsas_alloc_dma_obj(instance, &instance->drv_ver_dma_obj,
3558             (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
3559                 con_log(CL_ANN, (CE_WARN,
3560                     "init_mfi : Could not allocate driver version buffer."));
3561                 return (DDI_FAILURE);
3562         }
3563         /* copy driver version to dma buffer */
3564         (void) memset(instance->drv_ver_dma_obj.buffer, 0,
3565             sizeof (drv_ver_info.drv_ver));
3566         ddi_rep_put8(cmd->frame_dma_obj.acc_handle,
3567             (uint8_t *)drv_ver_info.drv_ver,
3568             (uint8_t *)instance->drv_ver_dma_obj.buffer,
3569             sizeof (drv_ver_info.drv_ver), DDI_DEV_AUTOINCR);
3570 
3571 
3572         /* copy driver version physical address to init frame */
3573         ddi_put64(cmd->frame_dma_obj.acc_handle, &init_frame->driverversion,
3574             instance->drv_ver_dma_obj.dma_cookie[0].dmac_address);
3575 
3576         ddi_put32(cmd->frame_dma_obj.acc_handle, &init_frame->data_xfer_len,
3577             sizeof (struct mrsas_init_queue_info));
3578 
3579         cmd->frame_count = 1;
3580 
3581         *cmd_ptr = cmd;
3582 
3583         return (DDI_SUCCESS);
3584 }
3585 
3586 
3587 /*
3588  * mrsas_init_adapter_ppc - Initialize MFI interface adapter.
3589  */
3590 int
3591 mrsas_init_adapter_ppc(struct mrsas_instance *instance)
3592 {
3593         struct mrsas_cmd                *cmd;
3594 
3595         /*
3596          * allocate memory for mfi adapter(cmd pool, individual commands, mfi
3597          * frames etc
3598          */
3599         if (alloc_space_for_mfi(instance) != DDI_SUCCESS) {
3600                 con_log(CL_ANN, (CE_NOTE,
3601                     "Error, failed to allocate memory for MFI adapter"));
3602                 return (DDI_FAILURE);
3603         }
3604 
3605         /* Build INIT command */
3606         cmd = get_mfi_pkt(instance);
3607 
3608         if (mrsas_build_init_cmd(instance, &cmd) != DDI_SUCCESS) {
3609                 con_log(CL_ANN,
3610                     (CE_NOTE, "Error, failed to build INIT command"));
3611 
3612                 goto fail_undo_alloc_mfi_space;
3613         }
3614 
3615         /*
3616          * Disable interrupt before sending init frame ( see linux driver code)
3617          * send INIT MFI frame in polled mode
3618          */
3619         if (instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) {
3620                 con_log(CL_ANN, (CE_WARN, "failed to init firmware"));

3621                 goto fail_fw_init;
3622         }
3623 
3624         if (mrsas_common_check(instance, cmd) != DDI_SUCCESS)

3625                 goto fail_fw_init;

3626         return_mfi_pkt(instance, cmd);
3627 
3628         if (ctio_enable &&
3629             (instance->func_ptr->read_fw_status_reg(instance) & 0x04000000)) {
3630                 con_log(CL_ANN, (CE_NOTE, "mr_sas: IEEE SGL's supported"));
3631                 instance->flag_ieee = 1;
3632         } else {
3633                 instance->flag_ieee = 0;
3634         }
3635 
3636         instance->unroll.alloc_space_mfi = 1;
3637         instance->unroll.verBuff = 1;
3638 
3639         return (DDI_SUCCESS);
3640 
3641 
3642 fail_fw_init:
3643         (void) mrsas_free_dma_obj(instance, instance->drv_ver_dma_obj);
3644 
3645 fail_undo_alloc_mfi_space:
3646         return_mfi_pkt(instance, cmd);
3647         free_space_for_mfi(instance);
3648 
3649         return (DDI_FAILURE);
3650 
3651 }
3652 
3653 /*
3654  * mrsas_init_adapter - Initialize adapter.
3655  */
3656 int
3657 mrsas_init_adapter(struct mrsas_instance *instance)
3658 {
3659         struct mrsas_ctrl_info          ctrl_info;
3660 
3661 
3662         /* we expect the FW state to be READY */
3663         if (mfi_state_transition_to_ready(instance)) {
3664                 con_log(CL_ANN, (CE_WARN, "mr_sas: F/W is not ready"));
3665                 return (DDI_FAILURE);
3666         }
3667 
3668         /* get various operational parameters from status register */
3669         instance->max_num_sge =
3670             (instance->func_ptr->read_fw_status_reg(instance) &
3671             0xFF0000) >> 0x10;
3672         instance->max_num_sge =
3673             (instance->max_num_sge > MRSAS_MAX_SGE_CNT) ?
3674             MRSAS_MAX_SGE_CNT : instance->max_num_sge;
3675 
3676         /*
3677          * Reduce the max supported cmds by 1. This is to ensure that the
3678          * reply_q_sz (1 more than the max cmd that driver may send)
3679          * does not exceed max cmds that the FW can support
3680          */
3681         instance->max_fw_cmds =
3682             instance->func_ptr->read_fw_status_reg(instance) & 0xFFFF;
3683         instance->max_fw_cmds = instance->max_fw_cmds - 1;
3684 
3685 
3686 
3687         /* Initialize adapter */
3688         if (instance->func_ptr->init_adapter(instance) != DDI_SUCCESS) {
3689                 con_log(CL_ANN,
3690                     (CE_WARN, "mr_sas: could not initialize adapter"));
3691                 return (DDI_FAILURE);
3692         }
3693 
3694         /* gather misc FW related information */
3695         instance->disable_online_ctrl_reset = 0;
3696 
3697         if (!get_ctrl_info(instance, &ctrl_info)) {
3698                 instance->max_sectors_per_req = ctrl_info.max_request_size;
3699                 con_log(CL_ANN1, (CE_NOTE,
3700                     "product name %s ld present %d",
3701                     ctrl_info.product_name, ctrl_info.ld_present_count));
3702         } else {
3703                 instance->max_sectors_per_req = instance->max_num_sge *
3704                     PAGESIZE / 512;
3705         }
3706 
3707         if (ctrl_info.properties.on_off_properties & DISABLE_OCR_PROP_FLAG)
3708                 instance->disable_online_ctrl_reset = 1;
3709 
3710         return (DDI_SUCCESS);
3711 










3712 }
3713 
3714 
3715 



3716 static int
3717 mrsas_issue_init_mfi(struct mrsas_instance *instance)
3718 {
3719         struct mrsas_cmd                *cmd;
3720         struct mrsas_init_frame         *init_frame;
3721         struct mrsas_init_queue_info    *initq_info;
3722 
3723 /*
3724  * Prepare a init frame. Note the init frame points to queue info
3725  * structure. Each frame has SGL allocated after first 64 bytes. For
3726  * this frame - since we don't need any SGL - we use SGL's space as
3727  * queue info structure
3728  */
3729         con_log(CL_ANN1, (CE_NOTE,
3730             "mrsas_issue_init_mfi: entry\n"));
3731         cmd = get_mfi_app_pkt(instance);
3732 
3733         if (!cmd) {
3734                 con_log(CL_ANN1, (CE_WARN,
3735                     "mrsas_issue_init_mfi: get_pkt failed\n"));
3736                 return (DDI_FAILURE);
3737         }
3738 
3739         /* Clear the frame buffer and assign back the context id */
3740         (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
3741         ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
3742             cmd->index);
3743 
3744         init_frame = (struct mrsas_init_frame *)cmd->frame;
3745         initq_info = (struct mrsas_init_queue_info *)
3746             ((unsigned long)init_frame + 64);
3747 
3748         (void) memset(init_frame, 0, MRMFI_FRAME_SIZE);
3749         (void) memset(initq_info, 0, sizeof (struct mrsas_init_queue_info));
3750 
3751         ddi_put32(cmd->frame_dma_obj.acc_handle, &initq_info->init_flags, 0);
3752 
3753         ddi_put32(cmd->frame_dma_obj.acc_handle,
3754             &initq_info->reply_queue_entries, instance->max_fw_cmds + 1);


3776         ddi_put16(cmd->frame_dma_obj.acc_handle, &init_frame->flags, 0);
3777         ddi_put32(cmd->frame_dma_obj.acc_handle,
3778             &init_frame->queue_info_new_phys_addr_lo,
3779             cmd->frame_phys_addr + 64);
3780         ddi_put32(cmd->frame_dma_obj.acc_handle,
3781             &init_frame->queue_info_new_phys_addr_hi, 0);
3782 
3783         ddi_put32(cmd->frame_dma_obj.acc_handle, &init_frame->data_xfer_len,
3784             sizeof (struct mrsas_init_queue_info));
3785 
3786         cmd->frame_count = 1;
3787 
3788         /* issue the init frame in polled mode */
3789         if (instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) {
3790                 con_log(CL_ANN1, (CE_WARN,
3791                     "mrsas_issue_init_mfi():failed to "
3792                     "init firmware"));
3793                 return_mfi_app_pkt(instance, cmd);
3794                 return (DDI_FAILURE);
3795         }
3796 
3797         if (mrsas_common_check(instance, cmd) != DDI_SUCCESS) {
3798                 return_mfi_pkt(instance, cmd);
3799                 return (DDI_FAILURE);
3800         }
3801 
3802         return_mfi_app_pkt(instance, cmd);
3803         con_log(CL_ANN1, (CE_CONT, "mrsas_issue_init_mfi: Done"));
3804 
3805         return (DDI_SUCCESS);
3806 }
3807 /*
3808  * mfi_state_transition_to_ready        : Move the FW to READY state
3809  *
3810  * @reg_set                     : MFI register set
3811  */
3812 int
3813 mfi_state_transition_to_ready(struct mrsas_instance *instance)
3814 {
3815         int             i;
3816         uint8_t         max_wait;
3817         uint32_t        fw_ctrl = 0;
3818         uint32_t        fw_state;
3819         uint32_t        cur_state;
3820         uint32_t        cur_abs_reg_val;
3821         uint32_t        prev_abs_reg_val;
3822         uint32_t        status;
3823 
3824         cur_abs_reg_val =
3825             instance->func_ptr->read_fw_status_reg(instance);
3826         fw_state =
3827             cur_abs_reg_val & MFI_STATE_MASK;
3828         con_log(CL_ANN1, (CE_CONT,
3829             "mfi_state_transition_to_ready:FW state = 0x%x", fw_state));
3830 
3831         while (fw_state != MFI_STATE_READY) {
3832                 con_log(CL_ANN, (CE_CONT,
3833                     "mfi_state_transition_to_ready:FW state%x", fw_state));
3834 
3835                 switch (fw_state) {
3836                 case MFI_STATE_FAULT:
3837                         con_log(CL_ANN, (CE_NOTE,
3838                             "mr_sas: FW in FAULT state!!"));
3839 
3840                         return (ENODEV);
3841                 case MFI_STATE_WAIT_HANDSHAKE:
3842                         /* set the CLR bit in IMR0 */
3843                         con_log(CL_ANN1, (CE_NOTE,
3844                             "mr_sas: FW waiting for HANDSHAKE"));
3845                         /*
3846                          * PCI_Hot Plug: MFI F/W requires
3847                          * (MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG)
3848                          * to be set
3849                          */
3850                         /* WR_IB_MSG_0(MFI_INIT_CLEAR_HANDSHAKE, instance); */
3851                         if (!instance->tbolt) {
3852                                 WR_IB_DOORBELL(MFI_INIT_CLEAR_HANDSHAKE |
3853                                     MFI_INIT_HOTPLUG, instance);
3854                         } else {
3855                                 WR_RESERVED0_REGISTER(MFI_INIT_CLEAR_HANDSHAKE |
3856                                     MFI_INIT_HOTPLUG, instance);
3857                         }
3858                         max_wait        = (instance->tbolt == 1) ? 180 : 2;
3859                         cur_state       = MFI_STATE_WAIT_HANDSHAKE;
3860                         break;
3861                 case MFI_STATE_BOOT_MESSAGE_PENDING:
3862                         /* set the CLR bit in IMR0 */
3863                         con_log(CL_ANN1, (CE_NOTE,
3864                             "mr_sas: FW state boot message pending"));
3865                         /*
3866                          * PCI_Hot Plug: MFI F/W requires
3867                          * (MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG)
3868                          * to be set
3869                          */
3870                         if (!instance->tbolt) {
3871                                 WR_IB_DOORBELL(MFI_INIT_HOTPLUG, instance);
3872                         } else {
3873                                 WR_RESERVED0_REGISTER(MFI_INIT_HOTPLUG,
3874                                     instance);
3875                         }
3876                         max_wait        = (instance->tbolt == 1) ? 180 : 10;
3877                         cur_state       = MFI_STATE_BOOT_MESSAGE_PENDING;
3878                         break;
3879                 case MFI_STATE_OPERATIONAL:
3880                         /* bring it to READY state; assuming max wait 2 secs */
3881                         instance->func_ptr->disable_intr(instance);
3882                         con_log(CL_ANN1, (CE_NOTE,
3883                             "mr_sas: FW in OPERATIONAL state"));
3884                         /*
3885                          * PCI_Hot Plug: MFI F/W requires
3886                          * (MFI_INIT_READY | MFI_INIT_MFIMODE | MFI_INIT_ABORT)
3887                          * to be set
3888                          */
3889                         /* WR_IB_DOORBELL(MFI_INIT_READY, instance); */
3890                         if (!instance->tbolt) {
3891                                 WR_IB_DOORBELL(MFI_RESET_FLAGS, instance);
3892                         } else {
3893                                 WR_RESERVED0_REGISTER(MFI_RESET_FLAGS,
3894                                     instance);
3895 
3896                                 for (i = 0; i < (10 * 1000); i++) {
3897                                         status =
3898                                             RD_RESERVED0_REGISTER(instance);
3899                                         if (status & 1) {
3900                                                 delay(1 *
3901                                                     drv_usectohz(MILLISEC));
3902                                         } else {
3903                                                 break;
3904                                         }
3905                                 }
3906 
3907                         }
3908                         max_wait        = (instance->tbolt == 1) ? 180 : 10;
3909                         cur_state       = MFI_STATE_OPERATIONAL;
3910                         break;
3911                 case MFI_STATE_UNDEFINED:
3912                         /* this state should not last for more than 2 seconds */
3913                         con_log(CL_ANN1, (CE_NOTE, "FW state undefined"));
3914 
3915                         max_wait        = (instance->tbolt == 1) ? 180 : 2;
3916                         cur_state       = MFI_STATE_UNDEFINED;
3917                         break;
3918                 case MFI_STATE_BB_INIT:
3919                         max_wait        = (instance->tbolt == 1) ? 180 : 2;
3920                         cur_state       = MFI_STATE_BB_INIT;
3921                         break;
3922                 case MFI_STATE_FW_INIT:
3923                         max_wait        = (instance->tbolt == 1) ? 180 : 2;
3924                         cur_state       = MFI_STATE_FW_INIT;
3925                         break;
3926                 case MFI_STATE_FW_INIT_2:
3927                         max_wait        = 180;
3928                         cur_state       = MFI_STATE_FW_INIT_2;
3929                         break;
3930                 case MFI_STATE_DEVICE_SCAN:
3931                         max_wait        = 180;
3932                         cur_state       = MFI_STATE_DEVICE_SCAN;
3933                         prev_abs_reg_val = cur_abs_reg_val;
3934                         con_log(CL_NONE, (CE_NOTE,
3935                             "Device scan in progress ...\n"));
3936                         break;
3937                 case MFI_STATE_FLUSH_CACHE:
3938                         max_wait        = 180;
3939                         cur_state       = MFI_STATE_FLUSH_CACHE;
3940                         break;
3941                 default:
3942                         con_log(CL_ANN1, (CE_NOTE,
3943                             "mr_sas: Unknown state 0x%x", fw_state));
3944                         return (ENODEV);
3945                 }
3946 
3947                 /* the cur_state should not last for more than max_wait secs */
3948                 for (i = 0; i < (max_wait * MILLISEC); i++) {
3949                         /* fw_state = RD_OB_MSG_0(instance) & MFI_STATE_MASK; */
3950                         cur_abs_reg_val =
3951                             instance->func_ptr->read_fw_status_reg(instance);
3952                         fw_state = cur_abs_reg_val & MFI_STATE_MASK;
3953 
3954                         if (fw_state == cur_state) {
3955                                 delay(1 * drv_usectohz(MILLISEC));
3956                         } else {
3957                                 break;
3958                         }
3959                 }
3960                 if (fw_state == MFI_STATE_DEVICE_SCAN) {
3961                         if (prev_abs_reg_val != cur_abs_reg_val) {
3962                                 continue;
3963                         }
3964                 }
3965 
3966                 /* return error if fw_state hasn't changed after max_wait */
3967                 if (fw_state == cur_state) {
3968                         con_log(CL_ANN1, (CE_WARN,
3969                             "FW state hasn't changed in %d secs", max_wait));
3970                         return (ENODEV);
3971                 }
3972         };
3973 
3974         if (!instance->tbolt) {
3975                 fw_ctrl = RD_IB_DOORBELL(instance);
3976                 con_log(CL_ANN1, (CE_CONT,

3977                     "mfi_state_transition_to_ready:FW ctrl = 0x%x", fw_ctrl));
3978 
3979                 /*
3980                  * Write 0xF to the doorbell register to do the following.
3981                  * - Abort all outstanding commands (bit 0).
3982                  * - Transition from OPERATIONAL to READY state (bit 1).
3983                  * - Discard (possible) low MFA posted in 64-bit mode (bit-2).
3984                  * - Set to release FW to continue running (i.e. BIOS handshake
3985                  *   (bit 3).
3986                  */
3987                 WR_IB_DOORBELL(0xF, instance);
3988         }
3989 
3990         if (mrsas_check_acc_handle(instance->regmap_handle) != DDI_SUCCESS) {
3991                 return (EIO);
3992         }
3993 
3994         return (DDI_SUCCESS);
3995 }
3996 
3997 /*
3998  * get_seq_num
3999  */
4000 static int
4001 get_seq_num(struct mrsas_instance *instance,
4002     struct mrsas_evt_log_info *eli)
4003 {
4004         int     ret = DDI_SUCCESS;
4005 
4006         dma_obj_t                       dcmd_dma_obj;
4007         struct mrsas_cmd                *cmd;
4008         struct mrsas_dcmd_frame         *dcmd;
4009         struct mrsas_evt_log_info *eli_tmp;
4010         if (instance->tbolt) {
4011                 cmd = get_raid_msg_mfi_pkt(instance);
4012         } else {
4013                 cmd = get_mfi_pkt(instance);
4014         }
4015 
4016         if (!cmd) {
4017                 cmn_err(CE_WARN, "mr_sas: failed to get a cmd");
4018                 DTRACE_PROBE2(seq_num_mfi_err, uint16_t,
4019                     instance->fw_outstanding, uint16_t, instance->max_fw_cmds);
4020                 return (ENOMEM);
4021         }
4022 
4023         /* Clear the frame buffer and assign back the context id */
4024         (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
4025         ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
4026             cmd->index);
4027 
4028         dcmd = &cmd->frame->dcmd;
4029 
4030         /* allocate the data transfer buffer */
4031         dcmd_dma_obj.size = sizeof (struct mrsas_evt_log_info);
4032         dcmd_dma_obj.dma_attr = mrsas_generic_dma_attr;
4033         dcmd_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
4034         dcmd_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
4035         dcmd_dma_obj.dma_attr.dma_attr_sgllen = 1;
4036         dcmd_dma_obj.dma_attr.dma_attr_align = 1;
4037 
4038         if (mrsas_alloc_dma_obj(instance, &dcmd_dma_obj,
4039             (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
4040                 cmn_err(CE_WARN,
4041                     "get_seq_num: could not allocate data transfer buffer.");
4042                 return (DDI_FAILURE);
4043         }
4044 
4045         (void) memset(dcmd_dma_obj.buffer, 0,
4046             sizeof (struct mrsas_evt_log_info));
4047 
4048         (void) memset(dcmd->mbox.b, 0, DCMD_MBOX_SZ);
4049 
4050         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd, MFI_CMD_OP_DCMD);
4051         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd_status, 0);
4052         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->sge_count, 1);
4053         ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->flags,
4054             MFI_FRAME_DIR_READ);
4055         ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->timeout, 0);
4056         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->data_xfer_len,
4057             sizeof (struct mrsas_evt_log_info));
4058         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->opcode,
4059             MR_DCMD_CTRL_EVENT_GET_INFO);
4060         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].length,
4061             sizeof (struct mrsas_evt_log_info));
4062         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].phys_addr,
4063             dcmd_dma_obj.dma_cookie[0].dmac_address);
4064 
4065         cmd->sync_cmd = MRSAS_TRUE;
4066         cmd->frame_count = 1;
4067 
4068         if (instance->tbolt) {
4069                 mr_sas_tbolt_build_mfi_cmd(instance, cmd);
4070         }
4071 
4072         if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) {
4073                 cmn_err(CE_WARN, "get_seq_num: "
4074                     "failed to issue MRSAS_DCMD_CTRL_EVENT_GET_INFO");
4075                 ret = DDI_FAILURE;
4076         } else {
4077                 eli_tmp = (struct mrsas_evt_log_info *)dcmd_dma_obj.buffer;
4078                 eli->newest_seq_num = ddi_get32(cmd->frame_dma_obj.acc_handle,
4079                     &eli_tmp->newest_seq_num);
4080                 ret = DDI_SUCCESS;
4081         }
4082 
4083         if (mrsas_free_dma_obj(instance, dcmd_dma_obj) != DDI_SUCCESS)
4084                 ret = DDI_FAILURE;
4085 
4086         if (instance->tbolt) {
4087                 return_raid_msg_mfi_pkt(instance, cmd);
4088         } else {
4089                 return_mfi_pkt(instance, cmd);
4090         }
4091 


4092         return (ret);
4093 }
4094 
4095 /*
4096  * start_mfi_aen
4097  */
4098 static int
4099 start_mfi_aen(struct mrsas_instance *instance)
4100 {
4101         int     ret = 0;
4102 
4103         struct mrsas_evt_log_info       eli;
4104         union mrsas_evt_class_locale    class_locale;
4105 
4106         /* get the latest sequence number from FW */
4107         (void) memset(&eli, 0, sizeof (struct mrsas_evt_log_info));
4108 
4109         if (get_seq_num(instance, &eli)) {
4110                 cmn_err(CE_WARN, "start_mfi_aen: failed to get seq num");
4111                 return (-1);
4112         }
4113 
4114         /* register AEN with FW for latest sequence number plus 1 */
4115         class_locale.members.reserved   = 0;
4116         class_locale.members.locale     = LE_16(MR_EVT_LOCALE_ALL);
4117         class_locale.members.class      = MR_EVT_CLASS_INFO;
4118         class_locale.word       = LE_32(class_locale.word);
4119         ret = register_mfi_aen(instance, eli.newest_seq_num + 1,
4120             class_locale.word);
4121 
4122         if (ret) {
4123                 cmn_err(CE_WARN, "start_mfi_aen: aen registration failed");
4124                 return (-1);
4125         }
4126 
4127 
4128         return (ret);
4129 }
4130 
4131 /*
4132  * flush_cache
4133  */
4134 static void
4135 flush_cache(struct mrsas_instance *instance)
4136 {
4137         struct mrsas_cmd                *cmd = NULL;
4138         struct mrsas_dcmd_frame         *dcmd;
4139         if (instance->tbolt) {
4140                 cmd = get_raid_msg_mfi_pkt(instance);
4141         } else {
4142                 cmd = get_mfi_pkt(instance);
4143         }
4144 


4145         if (!cmd) {
4146                 con_log(CL_ANN1, (CE_WARN,
4147                     "flush_cache():Failed to get a cmd for flush_cache"));
4148                 DTRACE_PROBE2(flush_cache_err, uint16_t,
4149                     instance->fw_outstanding, uint16_t, instance->max_fw_cmds);
4150                 return;
4151         }
4152 
4153         /* Clear the frame buffer and assign back the context id */
4154         (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
4155         ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
4156             cmd->index);
4157 
4158         dcmd = &cmd->frame->dcmd;
4159 
4160         (void) memset(dcmd->mbox.b, 0, DCMD_MBOX_SZ);
4161 
4162         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd, MFI_CMD_OP_DCMD);
4163         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd_status, 0x0);
4164         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->sge_count, 0);
4165         ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->flags,
4166             MFI_FRAME_DIR_NONE);
4167         ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->timeout, 0);
4168         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->data_xfer_len, 0);
4169         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->opcode,
4170             MR_DCMD_CTRL_CACHE_FLUSH);
4171         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->mbox.b[0],
4172             MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE);
4173 
4174         cmd->frame_count = 1;
4175 
4176         if (instance->tbolt) {
4177                 mr_sas_tbolt_build_mfi_cmd(instance, cmd);
4178         }
4179 
4180         if (instance->func_ptr->issue_cmd_in_poll_mode(instance, cmd)) {
4181                 con_log(CL_ANN1, (CE_WARN,
4182             "flush_cache: failed to issue MFI_DCMD_CTRL_CACHE_FLUSH"));
4183         }
4184         con_log(CL_ANN1, (CE_CONT, "flush_cache done"));
4185         if (instance->tbolt) {
4186                 return_raid_msg_mfi_pkt(instance, cmd);
4187         } else {
4188                 return_mfi_pkt(instance, cmd);
4189         }
4190 
4191 }
4192 
4193 /*
4194  * service_mfi_aen-     Completes an AEN command
4195  * @instance:                   Adapter soft state
4196  * @cmd:                        Command to be completed
4197  *
4198  */
4199 void
4200 service_mfi_aen(struct mrsas_instance *instance, struct mrsas_cmd *cmd)
4201 {
4202         uint32_t        seq_num;
4203         struct mrsas_evt_detail *evt_detail =
4204             (struct mrsas_evt_detail *)instance->mfi_evt_detail_obj.buffer;
4205         int             rval = 0;
4206         int             tgt = 0;
4207         uint8_t         dtype;
4208 #ifdef PDSUPPORT
4209         mrsas_pd_address_t      *pd_addr;
4210 #endif
4211         ddi_acc_handle_t                acc_handle;
4212 
4213         con_log(CL_ANN, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
4214 
4215         acc_handle = cmd->frame_dma_obj.acc_handle;
4216         cmd->cmd_status = ddi_get8(acc_handle, &cmd->frame->io.cmd_status);

4217         if (cmd->cmd_status == ENODATA) {
4218                 cmd->cmd_status = 0;
4219         }
4220 
4221         /*
4222          * log the MFI AEN event to the sysevent queue so that
4223          * application will get noticed
4224          */
4225         if (ddi_log_sysevent(instance->dip, DDI_VENDOR_LSI, "LSIMEGA", "SAS",
4226             NULL, NULL, DDI_NOSLEEP) != DDI_SUCCESS) {
4227                 int     instance_no = ddi_get_instance(instance->dip);
4228                 con_log(CL_ANN, (CE_WARN,
4229                     "mr_sas%d: Failed to log AEN event", instance_no));
4230         }
4231         /*
4232          * Check for any ld devices that has changed state. i.e. online
4233          * or offline.
4234          */
4235         con_log(CL_ANN1, (CE_CONT,
4236             "AEN: code = %x class = %x locale = %x args = %x",
4237             ddi_get32(acc_handle, &evt_detail->code),
4238             evt_detail->cl.members.class,
4239             ddi_get16(acc_handle, &evt_detail->cl.members.locale),
4240             ddi_get8(acc_handle, &evt_detail->arg_type)));
4241 
4242         switch (ddi_get32(acc_handle, &evt_detail->code)) {
4243         case MR_EVT_CFG_CLEARED: {
4244                 for (tgt = 0; tgt < MRDRV_MAX_LD; tgt++) {
4245                         if (instance->mr_ld_list[tgt].dip != NULL) {
4246                                 mutex_enter(&instance->config_dev_mtx);
4247                                 instance->mr_ld_list[tgt].flag =
4248                                     (uint8_t)~MRDRV_TGT_VALID;
4249                                 mutex_exit(&instance->config_dev_mtx);
4250                                 rval = mrsas_service_evt(instance, tgt, 0,
4251                                     MRSAS_EVT_UNCONFIG_TGT, NULL);
4252                                 con_log(CL_ANN1, (CE_WARN,
4253                                     "mr_sas: CFG CLEARED AEN rval = %d "
4254                                     "tgt id = %d", rval, tgt));
4255                         }
4256                 }
4257                 break;
4258         }
4259 
4260         case MR_EVT_LD_DELETED: {
4261                 tgt = ddi_get16(acc_handle, &evt_detail->args.ld.target_id);
4262                 mutex_enter(&instance->config_dev_mtx);
4263                 instance->mr_ld_list[tgt].flag = (uint8_t)~MRDRV_TGT_VALID;
4264                 mutex_exit(&instance->config_dev_mtx);
4265                 rval = mrsas_service_evt(instance,
4266                     ddi_get16(acc_handle, &evt_detail->args.ld.target_id), 0,
4267                     MRSAS_EVT_UNCONFIG_TGT, NULL);
4268                 con_log(CL_ANN1, (CE_WARN, "mr_sas: LD DELETED AEN rval = %d "
4269                     "tgt id = %d index = %d", rval,
4270                     ddi_get16(acc_handle, &evt_detail->args.ld.target_id),
4271                     ddi_get8(acc_handle, &evt_detail->args.ld.ld_index)));
4272                 break;
4273         } /* End of MR_EVT_LD_DELETED */
4274 
4275         case MR_EVT_LD_CREATED: {
4276                 rval = mrsas_service_evt(instance,
4277                     ddi_get16(acc_handle, &evt_detail->args.ld.target_id), 0,
4278                     MRSAS_EVT_CONFIG_TGT, NULL);
4279                 con_log(CL_ANN1, (CE_WARN, "mr_sas: LD CREATED AEN rval = %d "
4280                     "tgt id = %d index = %d", rval,
4281                     ddi_get16(acc_handle, &evt_detail->args.ld.target_id),
4282                     ddi_get8(acc_handle, &evt_detail->args.ld.ld_index)));
4283                 break;
4284         } /* End of MR_EVT_LD_CREATED */
4285 
4286 #ifdef PDSUPPORT
4287         case MR_EVT_PD_REMOVED_EXT: {
4288                 if (instance->tbolt) {
4289                         pd_addr = &evt_detail->args.pd_addr;
4290                         dtype = pd_addr->scsi_dev_type;
4291                         con_log(CL_DLEVEL1, (CE_NOTE,
4292                             " MR_EVT_PD_REMOVED_EXT: dtype = %x,"
4293                             " arg_type = %d ", dtype, evt_detail->arg_type));
4294                         tgt = ddi_get16(acc_handle,
4295                             &evt_detail->args.pd.device_id);
4296                         mutex_enter(&instance->config_dev_mtx);
4297                         instance->mr_tbolt_pd_list[tgt].flag =
4298                             (uint8_t)~MRDRV_TGT_VALID;
4299                         mutex_exit(&instance->config_dev_mtx);
4300                         rval = mrsas_service_evt(instance, ddi_get16(
4301                             acc_handle, &evt_detail->args.pd.device_id),
4302                             1, MRSAS_EVT_UNCONFIG_TGT, NULL);
4303                         con_log(CL_ANN1, (CE_WARN, "mr_sas: PD_REMOVED:"
4304                             "rval = %d tgt id = %d ", rval,
4305                             ddi_get16(acc_handle,
4306                             &evt_detail->args.pd.device_id)));
4307                 }
4308                 break;
4309         } /* End of MR_EVT_PD_REMOVED_EXT */
4310 
4311         case MR_EVT_PD_INSERTED_EXT: {
4312                 if (instance->tbolt) {
4313                         rval = mrsas_service_evt(instance,
4314                             ddi_get16(acc_handle,
4315                             &evt_detail->args.pd.device_id),
4316                             1, MRSAS_EVT_CONFIG_TGT, NULL);
4317                         con_log(CL_ANN1, (CE_WARN, "mr_sas: PD_INSERTEDi_EXT:"
4318                             "rval = %d tgt id = %d ", rval,
4319                             ddi_get16(acc_handle,
4320                             &evt_detail->args.pd.device_id)));
4321                 }
4322                 break;
4323         } /* End of MR_EVT_PD_INSERTED_EXT */
4324 
4325         case MR_EVT_PD_STATE_CHANGE: {
4326                 if (instance->tbolt) {
4327                         tgt = ddi_get16(acc_handle,
4328                             &evt_detail->args.pd.device_id);
4329                         if ((evt_detail->args.pd_state.prevState ==
4330                             PD_SYSTEM) &&
4331                             (evt_detail->args.pd_state.newState != PD_SYSTEM)) {
4332                                 mutex_enter(&instance->config_dev_mtx);
4333                                 instance->mr_tbolt_pd_list[tgt].flag =
4334                                     (uint8_t)~MRDRV_TGT_VALID;
4335                                 mutex_exit(&instance->config_dev_mtx);
4336                                 rval = mrsas_service_evt(instance,
4337                                     ddi_get16(acc_handle,
4338                                     &evt_detail->args.pd.device_id),
4339                                     1, MRSAS_EVT_UNCONFIG_TGT, NULL);
4340                                 con_log(CL_ANN1, (CE_WARN, "mr_sas: PD_REMOVED:"
4341                                     "rval = %d tgt id = %d ", rval,
4342                                     ddi_get16(acc_handle,
4343                                     &evt_detail->args.pd.device_id)));
4344                                 break;
4345                         }
4346                         if ((evt_detail->args.pd_state.prevState
4347                             == UNCONFIGURED_GOOD) &&
4348                             (evt_detail->args.pd_state.newState == PD_SYSTEM)) {
4349                                 rval = mrsas_service_evt(instance,
4350                                     ddi_get16(acc_handle,
4351                                     &evt_detail->args.pd.device_id),
4352                                     1, MRSAS_EVT_CONFIG_TGT, NULL);
4353                                 con_log(CL_ANN1, (CE_WARN,
4354                                     "mr_sas: PD_INSERTED: rval = %d "
4355                                     " tgt id = %d ", rval,
4356                                     ddi_get16(acc_handle,
4357                                     &evt_detail->args.pd.device_id)));
4358                                 break;
4359                         }
4360                 }
4361                 break;
4362         }
4363 #endif
4364 
4365         } /* End of Main Switch */
4366 
4367         /* get copy of seq_num and class/locale for re-registration */
4368         seq_num = ddi_get32(acc_handle, &evt_detail->seq_num);
4369         seq_num++;
4370         (void) memset(instance->mfi_evt_detail_obj.buffer, 0,
4371             sizeof (struct mrsas_evt_detail));
4372 
4373         ddi_put8(acc_handle, &cmd->frame->dcmd.cmd_status, 0x0);
4374         ddi_put32(acc_handle, &cmd->frame->dcmd.mbox.w[0], seq_num);
4375 
4376         instance->aen_seq_num = seq_num;
4377 
4378         cmd->frame_count = 1;
4379 
4380         cmd->retry_count_for_ocr = 0;
4381         cmd->drv_pkt_time = 0;
4382 
4383         /* Issue the aen registration frame */
4384         instance->func_ptr->issue_cmd(cmd, instance);
4385 }
4386 
4387 /*
4388  * complete_cmd_in_sync_mode -  Completes an internal command
4389  * @instance:                   Adapter soft state
4390  * @cmd:                        Command to be completed
4391  *
4392  * The issue_cmd_in_sync_mode() function waits for a command to complete
4393  * after it issues a command. This function wakes up that waiting routine by
4394  * calling wake_up() on the wait queue.
4395  */
4396 static void
4397 complete_cmd_in_sync_mode(struct mrsas_instance *instance,
4398     struct mrsas_cmd *cmd)
4399 {
4400         cmd->cmd_status = ddi_get8(cmd->frame_dma_obj.acc_handle,
4401             &cmd->frame->io.cmd_status);
4402 
4403         cmd->sync_cmd = MRSAS_FALSE;
4404 




4405         con_log(CL_ANN1, (CE_NOTE, "complete_cmd_in_sync_mode called %p \n",
4406             (void *)cmd));
4407 
4408         mutex_enter(&instance->int_cmd_mtx);
4409         if (cmd->cmd_status == ENODATA) {
4410                 cmd->cmd_status = 0;
4411         }
4412         cv_broadcast(&instance->int_cmd_cv);
4413         mutex_exit(&instance->int_cmd_mtx);
4414 
4415 }
4416 
4417 /*
4418  * Call this function inside mrsas_softintr.
4419  * mrsas_initiate_ocr_if_fw_is_faulty  - Initiates OCR if FW status is faulty
4420  * @instance:                   Adapter soft state
4421  */
4422 
4423 static uint32_t
4424 mrsas_initiate_ocr_if_fw_is_faulty(struct mrsas_instance *instance)
4425 {
4426         uint32_t        cur_abs_reg_val;
4427         uint32_t        fw_state;
4428 
4429         cur_abs_reg_val =  instance->func_ptr->read_fw_status_reg(instance);
4430         fw_state = cur_abs_reg_val & MFI_STATE_MASK;
4431         if (fw_state == MFI_STATE_FAULT) {

4432                 if (instance->disable_online_ctrl_reset == 1) {
4433                         cmn_err(CE_WARN,
4434                             "mrsas_initiate_ocr_if_fw_is_faulty: "
4435                             "FW in Fault state, detected in ISR: "
4436                             "FW doesn't support ocr ");
4437 
4438                         return (ADAPTER_RESET_NOT_REQUIRED);
4439                 } else {
4440                         con_log(CL_ANN, (CE_NOTE,
4441                             "mrsas_initiate_ocr_if_fw_is_faulty: FW in Fault "
4442                             "state, detected in ISR: FW supports ocr "));
4443 
4444                         return (ADAPTER_RESET_REQUIRED);
4445                 }
4446         }
4447 
4448         return (ADAPTER_RESET_NOT_REQUIRED);
4449 }
4450 
4451 /*
4452  * mrsas_softintr - The Software ISR
4453  * @param arg   : HBA soft state
4454  *
4455  * called from high-level interrupt if hi-level interrupt are not there,
4456  * otherwise triggered as a soft interrupt
4457  */
4458 static uint_t
4459 mrsas_softintr(struct mrsas_instance *instance)
4460 {
4461         struct scsi_pkt         *pkt;
4462         struct scsa_cmd         *acmd;
4463         struct mrsas_cmd        *cmd;
4464         struct mlist_head       *pos, *next;
4465         mlist_t                 process_list;
4466         struct mrsas_header     *hdr;
4467         struct scsi_arq_status  *arqstat;
4468 
4469         con_log(CL_ANN1, (CE_NOTE, "mrsas_softintr() called."));
4470 
4471         ASSERT(instance);
4472 
4473         mutex_enter(&instance->completed_pool_mtx);
4474 
4475         if (mlist_empty(&instance->completed_pool_list)) {
4476                 mutex_exit(&instance->completed_pool_mtx);
4477                 return (DDI_INTR_CLAIMED);
4478         }
4479 
4480         instance->softint_running = 1;
4481 
4482         INIT_LIST_HEAD(&process_list);
4483         mlist_splice(&instance->completed_pool_list, &process_list);
4484         INIT_LIST_HEAD(&instance->completed_pool_list);
4485 
4486         mutex_exit(&instance->completed_pool_mtx);
4487 
4488         /* perform all callbacks first, before releasing the SCBs */
4489         mlist_for_each_safe(pos, next, &process_list) {


4526 
4527                         /* regular commands */
4528                         acmd =  cmd->cmd;
4529                         pkt =   CMD2PKT(acmd);
4530 
4531                         if (acmd->cmd_flags & CFLAG_DMAVALID) {
4532                                 if (acmd->cmd_flags & CFLAG_CONSISTENT) {
4533                                         (void) ddi_dma_sync(acmd->cmd_dmahandle,
4534                                             acmd->cmd_dma_offset,
4535                                             acmd->cmd_dma_len,
4536                                             DDI_DMA_SYNC_FORCPU);
4537                                 }
4538                         }
4539 
4540                         pkt->pkt_reason              = CMD_CMPLT;
4541                         pkt->pkt_statistics  = 0;
4542                         pkt->pkt_state = STATE_GOT_BUS
4543                             | STATE_GOT_TARGET | STATE_SENT_CMD
4544                             | STATE_XFERRED_DATA | STATE_GOT_STATUS;
4545 
4546                         con_log(CL_ANN, (CE_CONT,
4547                             "CDB[0] = %x completed for %s: size %lx context %x",
4548                             pkt->pkt_cdbp[0], ((acmd->islogical) ? "LD" : "PD"),
4549                             acmd->cmd_dmacount, hdr->context));
4550                         DTRACE_PROBE3(softintr_cdb, uint8_t, pkt->pkt_cdbp[0],
4551                             uint_t, acmd->cmd_cdblen, ulong_t,
4552                             acmd->cmd_dmacount);
4553 
4554                         if (pkt->pkt_cdbp[0] == SCMD_INQUIRY) {
4555                                 struct scsi_inquiry     *inq;
4556 
4557                                 if (acmd->cmd_dmacount != 0) {
4558                                         bp_mapin(acmd->cmd_buf);
4559                                         inq = (struct scsi_inquiry *)
4560                                             acmd->cmd_buf->b_un.b_addr;
4561 
4562                                         /* don't expose physical drives to OS */
4563                                         if (acmd->islogical &&
4564                                             (hdr->cmd_status == MFI_STAT_OK)) {
4565                                                 display_scsi_inquiry(
4566                                                     (caddr_t)inq);


4579                         }
4580 
4581                         DTRACE_PROBE2(softintr_done, uint8_t, hdr->cmd,
4582                             uint8_t, hdr->cmd_status);
4583 
4584                         switch (hdr->cmd_status) {
4585                         case MFI_STAT_OK:
4586                                 pkt->pkt_scbp[0] = STATUS_GOOD;
4587                                 break;
4588                         case MFI_STAT_LD_CC_IN_PROGRESS:
4589                         case MFI_STAT_LD_RECON_IN_PROGRESS:
4590                                 pkt->pkt_scbp[0] = STATUS_GOOD;
4591                                 break;
4592                         case MFI_STAT_LD_INIT_IN_PROGRESS:
4593                                 con_log(CL_ANN,
4594                                     (CE_WARN, "Initialization in Progress"));
4595                                 pkt->pkt_reason      = CMD_TRAN_ERR;
4596 
4597                                 break;
4598                         case MFI_STAT_SCSI_DONE_WITH_ERROR:
4599                                 con_log(CL_ANN, (CE_CONT, "scsi_done error"));
4600 
4601                                 pkt->pkt_reason      = CMD_CMPLT;
4602                                 ((struct scsi_status *)
4603                                     pkt->pkt_scbp)->sts_chk = 1;
4604 
4605                                 if (pkt->pkt_cdbp[0] == SCMD_TEST_UNIT_READY) {

4606                                         con_log(CL_ANN,
4607                                             (CE_WARN, "TEST_UNIT_READY fail"));

4608                                 } else {
4609                                         pkt->pkt_state |= STATE_ARQ_DONE;
4610                                         arqstat = (void *)(pkt->pkt_scbp);
4611                                         arqstat->sts_rqpkt_reason = CMD_CMPLT;
4612                                         arqstat->sts_rqpkt_resid = 0;
4613                                         arqstat->sts_rqpkt_state |=
4614                                             STATE_GOT_BUS | STATE_GOT_TARGET
4615                                             | STATE_SENT_CMD
4616                                             | STATE_XFERRED_DATA;
4617                                         *(uint8_t *)&arqstat->sts_rqpkt_status =
4618                                             STATUS_GOOD;
4619                                         ddi_rep_get8(
4620                                             cmd->frame_dma_obj.acc_handle,
4621                                             (uint8_t *)
4622                                             &(arqstat->sts_sensedata),
4623                                             cmd->sense,
4624                                             sizeof (struct scsi_extended_sense),
4625                                             DDI_DEV_AUTOINCR);

4626                                 }
4627                                 break;
4628                         case MFI_STAT_LD_OFFLINE:
4629                         case MFI_STAT_DEVICE_NOT_FOUND:
4630                                 con_log(CL_ANN, (CE_CONT,
4631                                 "mrsas_softintr:device not found error"));
4632                                 pkt->pkt_reason      = CMD_DEV_GONE;
4633                                 pkt->pkt_statistics  = STAT_DISCON;
4634                                 break;
4635                         case MFI_STAT_LD_LBA_OUT_OF_RANGE:
4636                                 pkt->pkt_state |= STATE_ARQ_DONE;
4637                                 pkt->pkt_reason      = CMD_CMPLT;
4638                                 ((struct scsi_status *)
4639                                     pkt->pkt_scbp)->sts_chk = 1;
4640 
4641                                 arqstat = (void *)(pkt->pkt_scbp);
4642                                 arqstat->sts_rqpkt_reason = CMD_CMPLT;
4643                                 arqstat->sts_rqpkt_resid = 0;
4644                                 arqstat->sts_rqpkt_state |= STATE_GOT_BUS
4645                                     | STATE_GOT_TARGET | STATE_SENT_CMD
4646                                     | STATE_XFERRED_DATA;
4647                                 *(uint8_t *)&arqstat->sts_rqpkt_status =
4648                                     STATUS_GOOD;
4649 
4650                                 arqstat->sts_sensedata.es_valid = 1;


4670                         }
4671 
4672                         atomic_add_16(&instance->fw_outstanding, (-1));
4673 
4674                         (void) mrsas_common_check(instance, cmd);
4675 
4676                         if (acmd->cmd_dmahandle) {
4677                                 if (mrsas_check_dma_handle(
4678                                     acmd->cmd_dmahandle) != DDI_SUCCESS) {
4679                                         ddi_fm_service_impact(instance->dip,
4680                                             DDI_SERVICE_UNAFFECTED);
4681                                         pkt->pkt_reason = CMD_TRAN_ERR;
4682                                         pkt->pkt_statistics = 0;
4683                                 }
4684                         }
4685 
4686                         /* Call the callback routine */
4687                         if (((pkt->pkt_flags & FLAG_NOINTR) == 0) &&
4688                             pkt->pkt_comp) {
4689 
4690                                 con_log(CL_DLEVEL1, (CE_NOTE, "mrsas_softintr: "
4691                                     "posting to scsa cmd %p index %x pkt %p "
4692                                     "time %llx", (void *)cmd, cmd->index,
4693                                     (void *)pkt, gethrtime()));
4694                                 (*pkt->pkt_comp)(pkt);
4695 
4696                         }
4697 
4698                         return_mfi_pkt(instance, cmd);
4699                         break;
4700 
4701                 case MFI_CMD_OP_SMP:
4702                 case MFI_CMD_OP_STP:
4703                         complete_cmd_in_sync_mode(instance, cmd);
4704                         break;
4705 
4706                 case MFI_CMD_OP_DCMD:
4707                         /* see if got an event notification */
4708                         if (ddi_get32(cmd->frame_dma_obj.acc_handle,
4709                             &cmd->frame->dcmd.opcode) ==
4710                             MR_DCMD_CTRL_EVENT_WAIT) {
4711                                 if ((instance->aen_cmd == cmd) &&
4712                                     (instance->aen_cmd->abort_aen)) {
4713                                         con_log(CL_ANN, (CE_WARN,
4714                                             "mrsas_softintr: "
4715                                             "aborted_aen returned"));
4716                                 } else {
4717                                         atomic_add_16(&instance->fw_outstanding,
4718                                             (-1));
4719                                         service_mfi_aen(instance, cmd);
4720                                 }
4721                         } else {
4722                                 complete_cmd_in_sync_mode(instance, cmd);
4723                         }
4724 
4725                         break;
4726 
4727                 case MFI_CMD_OP_ABORT:
4728                         con_log(CL_ANN, (CE_NOTE, "MFI_CMD_OP_ABORT complete"));
4729                         /*
4730                          * MFI_CMD_OP_ABORT successfully completed
4731                          * in the synchronous mode
4732                          */
4733                         complete_cmd_in_sync_mode(instance, cmd);
4734                         break;
4735 
4736                 default:
4737                         mrsas_fm_ereport(instance, DDI_FM_DEVICE_NO_RESPONSE);
4738                         ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST);
4739 
4740                         if (cmd->pkt != NULL) {
4741                                 pkt = cmd->pkt;
4742                                 if (((pkt->pkt_flags & FLAG_NOINTR) == 0) &&
4743                                     pkt->pkt_comp) {
4744 
4745                                         con_log(CL_ANN1, (CE_CONT, "posting to "
4746                                             "scsa cmd %p index %x pkt %p"
4747                                             "time %llx, default ", (void *)cmd,
4748                                             cmd->index, (void *)pkt,
4749                                             gethrtime()));
4750 
4751                                         (*pkt->pkt_comp)(pkt);
4752 
4753                                 }
4754                         }
4755                         con_log(CL_ANN, (CE_WARN, "Cmd type unknown !"));
4756                         break;
4757                 }
4758         }
4759 
4760         instance->softint_running = 0;
4761 
4762         return (DDI_INTR_CLAIMED);
4763 }
4764 
4765 /*
4766  * mrsas_alloc_dma_obj
4767  *
4768  * Allocate the memory and other resources for an dma object.
4769  */
4770 int
4771 mrsas_alloc_dma_obj(struct mrsas_instance *instance, dma_obj_t *obj,
4772     uchar_t endian_flags)
4773 {
4774         int     i;
4775         size_t  alen = 0;
4776         uint_t  cookie_cnt;
4777         struct ddi_device_acc_attr tmp_endian_attr;
4778 
4779         tmp_endian_attr = endian_attr;
4780         tmp_endian_attr.devacc_attr_endian_flags = endian_flags;
4781         tmp_endian_attr.devacc_attr_access = DDI_DEFAULT_ACC;
4782 
4783         i = ddi_dma_alloc_handle(instance->dip, &obj->dma_attr,
4784             DDI_DMA_SLEEP, NULL, &obj->dma_handle);
4785         if (i != DDI_SUCCESS) {
4786 
4787                 switch (i) {
4788                         case DDI_DMA_BADATTR :
4789                                 con_log(CL_ANN, (CE_WARN,
4790                                 "Failed ddi_dma_alloc_handle- Bad attribute"));


4829 
4830         if (mrsas_check_dma_handle(obj->dma_handle) != DDI_SUCCESS) {
4831                 ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST);
4832                 return (-1);
4833         }
4834 
4835         if (mrsas_check_acc_handle(obj->acc_handle) != DDI_SUCCESS) {
4836                 ddi_fm_service_impact(instance->dip, DDI_SERVICE_LOST);
4837                 return (-1);
4838         }
4839 
4840         return (cookie_cnt);
4841 }
4842 
4843 /*
4844  * mrsas_free_dma_obj(struct mrsas_instance *, dma_obj_t)
4845  *
4846  * De-allocate the memory and other resources for an dma object, which must
4847  * have been alloated by a previous call to mrsas_alloc_dma_obj()
4848  */
4849 int
4850 mrsas_free_dma_obj(struct mrsas_instance *instance, dma_obj_t obj)
4851 {
4852 
4853         if ((obj.dma_handle == NULL) || (obj.acc_handle == NULL)) {
4854                 return (DDI_SUCCESS);
4855         }
4856 
4857         /*
4858          * NOTE: These check-handle functions fail if *_handle == NULL, but
4859          * this function succeeds because of the previous check.
4860          */
4861         if (mrsas_check_dma_handle(obj.dma_handle) != DDI_SUCCESS) {
4862                 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED);
4863                 return (DDI_FAILURE);
4864         }
4865 
4866         if (mrsas_check_acc_handle(obj.acc_handle) != DDI_SUCCESS) {
4867                 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED);
4868                 return (DDI_FAILURE);
4869         }
4870 
4871         (void) ddi_dma_unbind_handle(obj.dma_handle);
4872         ddi_dma_mem_free(&obj.acc_handle);
4873         ddi_dma_free_handle(&obj.dma_handle);
4874         obj.acc_handle = NULL;
4875         return (DDI_SUCCESS);
4876 }
4877 
4878 /*
4879  * mrsas_dma_alloc(instance_t *, struct scsi_pkt *, struct buf *,
4880  * int, int (*)())
4881  *
4882  * Allocate dma resources for a new scsi command
4883  */
4884 int
4885 mrsas_dma_alloc(struct mrsas_instance *instance, struct scsi_pkt *pkt,
4886     struct buf *bp, int flags, int (*callback)())
4887 {
4888         int     dma_flags;
4889         int     (*cb)(caddr_t);
4890         int     i;
4891 
4892         ddi_dma_attr_t  tmp_dma_attr = mrsas_generic_dma_attr;
4893         struct scsa_cmd *acmd = PKT2CMD(pkt);
4894 
4895         acmd->cmd_buf = bp;
4896 
4897         if (bp->b_flags & B_READ) {
4898                 acmd->cmd_flags &= ~CFLAG_DMASEND;
4899                 dma_flags = DDI_DMA_READ;
4900         } else {
4901                 acmd->cmd_flags |= CFLAG_DMASEND;
4902                 dma_flags = DDI_DMA_WRITE;
4903         }
4904 
4905         if (flags & PKT_CONSISTENT) {
4906                 acmd->cmd_flags |= CFLAG_CONSISTENT;
4907                 dma_flags |= DDI_DMA_CONSISTENT;
4908         }
4909 
4910         if (flags & PKT_DMA_PARTIAL) {
4911                 dma_flags |= DDI_DMA_PARTIAL;
4912         }
4913 
4914         dma_flags |= DDI_DMA_REDZONE;
4915 
4916         cb = (callback == NULL_FUNC) ? DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
4917 
4918         tmp_dma_attr.dma_attr_sgllen = instance->max_num_sge;
4919         tmp_dma_attr.dma_attr_addr_hi = 0xffffffffffffffffull;
4920         if (instance->tbolt) {
4921                 /* OCR-RESET FIX */
4922                 tmp_dma_attr.dma_attr_count_max =
4923                     (U64)mrsas_tbolt_max_cap_maxxfer;  /* limit to 256K */
4924                 tmp_dma_attr.dma_attr_maxxfer =
4925                     (U64)mrsas_tbolt_max_cap_maxxfer;  /* limit to 256K */
4926         }
4927 
4928         if ((i = ddi_dma_alloc_handle(instance->dip, &tmp_dma_attr,
4929             cb, 0, &acmd->cmd_dmahandle)) != DDI_SUCCESS) {
4930                 switch (i) {
4931                 case DDI_DMA_BADATTR:
4932                         bioerror(bp, EFAULT);
4933                         return (DDI_FAILURE);
4934 
4935                 case DDI_DMA_NORESOURCES:
4936                         bioerror(bp, 0);
4937                         return (DDI_FAILURE);
4938 
4939                 default:
4940                         con_log(CL_ANN, (CE_PANIC, "ddi_dma_alloc_handle: "
4941                             "impossible result (0x%x)", i));
4942                         bioerror(bp, EFAULT);
4943                         return (DDI_FAILURE);
4944                 }
4945         }
4946 


5018                 break;
5019         default:
5020                 con_log(CL_ANN, (CE_PANIC, "ddi_dma_buf_bind_handle: "
5021                     "impossible result (0x%x)", i));
5022                 break;
5023         }
5024 
5025 no_dma_cookies:
5026         ddi_dma_free_handle(&acmd->cmd_dmahandle);
5027         acmd->cmd_dmahandle = NULL;
5028         acmd->cmd_flags &= ~CFLAG_DMAVALID;
5029         return (DDI_FAILURE);
5030 }
5031 
5032 /*
5033  * mrsas_dma_move(struct mrsas_instance *, struct scsi_pkt *, struct buf *)
5034  *
5035  * move dma resources to next dma window
5036  *
5037  */
5038 int
5039 mrsas_dma_move(struct mrsas_instance *instance, struct scsi_pkt *pkt,
5040     struct buf *bp)
5041 {
5042         int     i = 0;
5043 
5044         struct scsa_cmd *acmd = PKT2CMD(pkt);
5045 
5046         /*
5047          * If there are no more cookies remaining in this window,
5048          * must move to the next window first.
5049          */
5050         if (acmd->cmd_cookie == acmd->cmd_ncookies) {
5051                 if (acmd->cmd_curwin == acmd->cmd_nwin && acmd->cmd_nwin == 1) {
5052                         return (DDI_SUCCESS);
5053                 }
5054 
5055                 /* at last window, cannot move */
5056                 if (++acmd->cmd_curwin >= acmd->cmd_nwin) {
5057                         return (DDI_FAILURE);
5058                 }


5090         if (bp->b_bcount >= acmd->cmd_dmacount) {
5091                 pkt->pkt_resid = bp->b_bcount - acmd->cmd_dmacount;
5092         } else {
5093                 pkt->pkt_resid = 0;
5094         }
5095 
5096         return (DDI_SUCCESS);
5097 }
5098 
5099 /*
5100  * build_cmd
5101  */
5102 static struct mrsas_cmd *
5103 build_cmd(struct mrsas_instance *instance, struct scsi_address *ap,
5104     struct scsi_pkt *pkt, uchar_t *cmd_done)
5105 {
5106         uint16_t        flags = 0;
5107         uint32_t        i;
5108         uint32_t        context;
5109         uint32_t        sge_bytes;
5110         uint32_t        tmp_data_xfer_len;
5111         ddi_acc_handle_t acc_handle;
5112         struct mrsas_cmd                *cmd;
5113         struct mrsas_sge64              *mfi_sgl;
5114         struct mrsas_sge_ieee           *mfi_sgl_ieee;
5115         struct scsa_cmd                 *acmd = PKT2CMD(pkt);
5116         struct mrsas_pthru_frame        *pthru;
5117         struct mrsas_io_frame           *ldio;
5118 
5119         /* find out if this is logical or physical drive command.  */
5120         acmd->islogical = MRDRV_IS_LOGICAL(ap);
5121         acmd->device_id = MAP_DEVICE_ID(instance, ap);
5122         *cmd_done = 0;
5123 
5124         /* get the command packet */
5125         if (!(cmd = get_mfi_pkt(instance))) {
5126                 DTRACE_PROBE2(build_cmd_mfi_err, uint16_t,
5127                     instance->fw_outstanding, uint16_t, instance->max_fw_cmds);
5128                 return (NULL);
5129         }
5130 


5131         acc_handle = cmd->frame_dma_obj.acc_handle;
5132 
5133         /* Clear the frame buffer and assign back the context id */
5134         (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
5135         ddi_put32(acc_handle, &cmd->frame->hdr.context, cmd->index);
5136 
5137         cmd->pkt = pkt;
5138         cmd->cmd = acmd;
5139         DTRACE_PROBE3(build_cmds, uint8_t, pkt->pkt_cdbp[0],
5140             ulong_t, acmd->cmd_dmacount, ulong_t, acmd->cmd_dma_len);
5141 
5142         /* lets get the command directions */
5143         if (acmd->cmd_flags & CFLAG_DMASEND) {
5144                 flags = MFI_FRAME_DIR_WRITE;
5145 
5146                 if (acmd->cmd_flags & CFLAG_CONSISTENT) {
5147                         (void) ddi_dma_sync(acmd->cmd_dmahandle,
5148                             acmd->cmd_dma_offset, acmd->cmd_dma_len,
5149                             DDI_DMA_SYNC_FORDEV);
5150                 }


5163         if (instance->flag_ieee) {
5164                 flags |= MFI_FRAME_IEEE;
5165         }
5166         flags |= MFI_FRAME_SGL64;
5167 
5168         switch (pkt->pkt_cdbp[0]) {
5169 
5170         /*
5171          * case SCMD_SYNCHRONIZE_CACHE:
5172          *      flush_cache(instance);
5173          *      return_mfi_pkt(instance, cmd);
5174          *      *cmd_done = 1;
5175          *
5176          *      return (NULL);
5177          */
5178 
5179         case SCMD_READ:
5180         case SCMD_WRITE:
5181         case SCMD_READ_G1:
5182         case SCMD_WRITE_G1:
5183         case SCMD_READ_G4:
5184         case SCMD_WRITE_G4:
5185         case SCMD_READ_G5:
5186         case SCMD_WRITE_G5:
5187                 if (acmd->islogical) {
5188                         ldio = (struct mrsas_io_frame *)cmd->frame;
5189 
5190                         /*
5191                          * preare the Logical IO frame:
5192                          * 2nd bit is zero for all read cmds
5193                          */
5194                         ddi_put8(acc_handle, &ldio->cmd,
5195                             (pkt->pkt_cdbp[0] & 0x02) ? MFI_CMD_OP_LD_WRITE
5196                             : MFI_CMD_OP_LD_READ);
5197                         ddi_put8(acc_handle, &ldio->cmd_status, 0x0);
5198                         ddi_put8(acc_handle, &ldio->scsi_status, 0x0);
5199                         ddi_put8(acc_handle, &ldio->target_id, acmd->device_id);
5200                         ddi_put16(acc_handle, &ldio->timeout, 0);
5201                         ddi_put8(acc_handle, &ldio->reserved_0, 0);
5202                         ddi_put16(acc_handle, &ldio->pad_0, 0);
5203                         ddi_put16(acc_handle, &ldio->flags, flags);
5204 
5205                         /* Initialize sense Information */
5206                         bzero(cmd->sense, SENSE_LENGTH);
5207                         ddi_put8(acc_handle, &ldio->sense_len, SENSE_LENGTH);
5208                         ddi_put32(acc_handle, &ldio->sense_buf_phys_addr_hi, 0);
5209                         ddi_put32(acc_handle, &ldio->sense_buf_phys_addr_lo,
5210                             cmd->sense_phys_addr);
5211                         ddi_put32(acc_handle, &ldio->start_lba_hi, 0);
5212                         ddi_put8(acc_handle, &ldio->access_byte,
5213                             (acmd->cmd_cdblen != 6) ? pkt->pkt_cdbp[1] : 0);
5214                         ddi_put8(acc_handle, &ldio->sge_count,
5215                             acmd->cmd_cookiecnt);
5216                         if (instance->flag_ieee) {
5217                                 mfi_sgl_ieee =
5218                                     (struct mrsas_sge_ieee *)&ldio->sgl;
5219                         } else {
5220                                 mfi_sgl = (struct mrsas_sge64   *)&ldio->sgl;
5221                         }
5222 
5223                         context = ddi_get32(acc_handle, &ldio->context);
5224 
5225                         if (acmd->cmd_cdblen == CDB_GROUP0) {
5226                                 /* 6-byte cdb */
5227                                 ddi_put32(acc_handle, &ldio->lba_count, (
5228                                     (uint16_t)(pkt->pkt_cdbp[4])));
5229 
5230                                 ddi_put32(acc_handle, &ldio->start_lba_lo, (
5231                                     ((uint32_t)(pkt->pkt_cdbp[3])) |
5232                                     ((uint32_t)(pkt->pkt_cdbp[2]) << 8) |
5233                                     ((uint32_t)((pkt->pkt_cdbp[1]) & 0x1F)
5234                                     << 16)));
5235                         } else if (acmd->cmd_cdblen == CDB_GROUP1) {
5236                                 /* 10-byte cdb */
5237                                 ddi_put32(acc_handle, &ldio->lba_count, (
5238                                     ((uint16_t)(pkt->pkt_cdbp[8])) |
5239                                     ((uint16_t)(pkt->pkt_cdbp[7]) << 8)));
5240 
5241                                 ddi_put32(acc_handle, &ldio->start_lba_lo, (
5242                                     ((uint32_t)(pkt->pkt_cdbp[5])) |
5243                                     ((uint32_t)(pkt->pkt_cdbp[4]) << 8) |
5244                                     ((uint32_t)(pkt->pkt_cdbp[3]) << 16) |
5245                                     ((uint32_t)(pkt->pkt_cdbp[2]) << 24)));
5246                         } else if (acmd->cmd_cdblen == CDB_GROUP5) {
5247                                 /* 12-byte cdb */
5248                                 ddi_put32(acc_handle, &ldio->lba_count, (
5249                                     ((uint32_t)(pkt->pkt_cdbp[9])) |
5250                                     ((uint32_t)(pkt->pkt_cdbp[8]) << 8) |
5251                                     ((uint32_t)(pkt->pkt_cdbp[7]) << 16) |
5252                                     ((uint32_t)(pkt->pkt_cdbp[6]) << 24)));
5253 
5254                                 ddi_put32(acc_handle, &ldio->start_lba_lo, (
5255                                     ((uint32_t)(pkt->pkt_cdbp[5])) |
5256                                     ((uint32_t)(pkt->pkt_cdbp[4]) << 8) |
5257                                     ((uint32_t)(pkt->pkt_cdbp[3]) << 16) |
5258                                     ((uint32_t)(pkt->pkt_cdbp[2]) << 24)));
5259                         } else if (acmd->cmd_cdblen == CDB_GROUP4) {
5260                                 /* 16-byte cdb */
5261                                 ddi_put32(acc_handle, &ldio->lba_count, (
5262                                     ((uint32_t)(pkt->pkt_cdbp[13])) |
5263                                     ((uint32_t)(pkt->pkt_cdbp[12]) << 8) |
5264                                     ((uint32_t)(pkt->pkt_cdbp[11]) << 16) |
5265                                     ((uint32_t)(pkt->pkt_cdbp[10]) << 24)));
5266 
5267                                 ddi_put32(acc_handle, &ldio->start_lba_lo, (
5268                                     ((uint32_t)(pkt->pkt_cdbp[9])) |
5269                                     ((uint32_t)(pkt->pkt_cdbp[8]) << 8) |
5270                                     ((uint32_t)(pkt->pkt_cdbp[7]) << 16) |
5271                                     ((uint32_t)(pkt->pkt_cdbp[6]) << 24)));
5272 
5273                                 ddi_put32(acc_handle, &ldio->start_lba_hi, (
5274                                     ((uint32_t)(pkt->pkt_cdbp[5])) |
5275                                     ((uint32_t)(pkt->pkt_cdbp[4]) << 8) |
5276                                     ((uint32_t)(pkt->pkt_cdbp[3]) << 16) |
5277                                     ((uint32_t)(pkt->pkt_cdbp[2]) << 24)));
5278                         }
5279 
5280                         break;
5281                 }
5282                 /* fall through For all non-rd/wr cmds */
5283         default:
5284 
5285                 switch (pkt->pkt_cdbp[0]) {
5286                 case SCMD_MODE_SENSE:
5287                 case SCMD_MODE_SENSE_G1: {
5288                         union scsi_cdb  *cdbp;
5289                         uint16_t        page_code;
5290 
5291                         cdbp = (void *)pkt->pkt_cdbp;
5292                         page_code = (uint16_t)cdbp->cdb_un.sg.scsi[0];
5293                         switch (page_code) {


5299                                 return (NULL);
5300                         }
5301                         break;
5302                 }
5303                 default:
5304                         break;
5305                 }
5306 
5307                 pthru   = (struct mrsas_pthru_frame *)cmd->frame;
5308 
5309                 /* prepare the DCDB frame */
5310                 ddi_put8(acc_handle, &pthru->cmd, (acmd->islogical) ?
5311                     MFI_CMD_OP_LD_SCSI : MFI_CMD_OP_PD_SCSI);
5312                 ddi_put8(acc_handle, &pthru->cmd_status, 0x0);
5313                 ddi_put8(acc_handle, &pthru->scsi_status, 0x0);
5314                 ddi_put8(acc_handle, &pthru->target_id, acmd->device_id);
5315                 ddi_put8(acc_handle, &pthru->lun, 0);
5316                 ddi_put8(acc_handle, &pthru->cdb_len, acmd->cmd_cdblen);
5317                 ddi_put16(acc_handle, &pthru->timeout, 0);
5318                 ddi_put16(acc_handle, &pthru->flags, flags);
5319                 tmp_data_xfer_len = 0;
5320                 for (i = 0; i < acmd->cmd_cookiecnt; i++) {
5321                         tmp_data_xfer_len += acmd->cmd_dmacookies[i].dmac_size;
5322                 }
5323                 ddi_put32(acc_handle, &pthru->data_xfer_len,
5324                     tmp_data_xfer_len);
5325                 ddi_put8(acc_handle, &pthru->sge_count, acmd->cmd_cookiecnt);
5326                 if (instance->flag_ieee) {
5327                         mfi_sgl_ieee = (struct mrsas_sge_ieee *)&pthru->sgl;
5328                 } else {
5329                         mfi_sgl = (struct mrsas_sge64 *)&pthru->sgl;
5330                 }
5331 
5332                 bzero(cmd->sense, SENSE_LENGTH);
5333                 ddi_put8(acc_handle, &pthru->sense_len, SENSE_LENGTH);
5334                 ddi_put32(acc_handle, &pthru->sense_buf_phys_addr_hi, 0);
5335                 ddi_put32(acc_handle, &pthru->sense_buf_phys_addr_lo,
5336                     cmd->sense_phys_addr);
5337 
5338                 context = ddi_get32(acc_handle, &pthru->context);
5339                 ddi_rep_put8(acc_handle, (uint8_t *)pkt->pkt_cdbp,
5340                     (uint8_t *)pthru->cdb, acmd->cmd_cdblen, DDI_DEV_AUTOINCR);
5341 
5342                 break;
5343         }
5344 #ifdef lint


5355                 sge_bytes = sizeof (struct mrsas_sge_ieee)*acmd->cmd_cookiecnt;
5356         } else {
5357                 for (i = 0; i < acmd->cmd_cookiecnt; i++, mfi_sgl++) {
5358                         ddi_put64(acc_handle, &mfi_sgl->phys_addr,
5359                             acmd->cmd_dmacookies[i].dmac_laddress);
5360                         ddi_put32(acc_handle, &mfi_sgl->length,
5361                             acmd->cmd_dmacookies[i].dmac_size);
5362                 }
5363                 sge_bytes = sizeof (struct mrsas_sge64)*acmd->cmd_cookiecnt;
5364         }
5365 
5366         cmd->frame_count = (sge_bytes / MRMFI_FRAME_SIZE) +
5367             ((sge_bytes % MRMFI_FRAME_SIZE) ? 1 : 0) + 1;
5368 
5369         if (cmd->frame_count >= 8) {
5370                 cmd->frame_count = 8;
5371         }
5372 
5373         return (cmd);
5374 }
5375 
5376 #ifndef __sparc
5377 /*
5378  * wait_for_outstanding -       Wait for all outstanding cmds
5379  * @instance:                           Adapter soft state
5380  *
5381  * This function waits for upto MRDRV_RESET_WAIT_TIME seconds for FW to
5382  * complete all its outstanding commands. Returns error if one or more IOs
5383  * are pending after this time period.
5384  */
5385 static int
5386 wait_for_outstanding(struct mrsas_instance *instance)
5387 {
5388         int             i;
5389         uint32_t        wait_time = 90;
5390 
5391         for (i = 0; i < wait_time; i++) {
5392                 if (!instance->fw_outstanding) {
5393                         break;
5394                 }
5395 
5396                 drv_usecwait(MILLISEC); /* wait for 1000 usecs */;
5397         }
5398 
5399         if (instance->fw_outstanding) {
5400                 return (1);
5401         }
5402 
5403         return (0);
5404 }
5405 #endif  /* __sparc */
5406 
5407 /*
5408  * issue_mfi_pthru
5409  */
5410 static int
5411 issue_mfi_pthru(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl,
5412     struct mrsas_cmd *cmd, int mode)
5413 {
5414         void            *ubuf;
5415         uint32_t        kphys_addr = 0;
5416         uint32_t        xferlen = 0;
5417         uint32_t        new_xfer_length = 0;
5418         uint_t          model;
5419         ddi_acc_handle_t        acc_handle = cmd->frame_dma_obj.acc_handle;
5420         dma_obj_t                       pthru_dma_obj;
5421         struct mrsas_pthru_frame        *kpthru;
5422         struct mrsas_pthru_frame        *pthru;
5423         int i;
5424         pthru = &cmd->frame->pthru;
5425         kpthru = (struct mrsas_pthru_frame *)&ioctl->frame[0];
5426 
5427         if (instance->adapterresetinprogress) {
5428                 con_log(CL_ANN1, (CE_WARN, "issue_mfi_pthru: Reset flag set, "
5429                 "returning mfi_pkt and setting TRAN_BUSY\n"));
5430                 return (DDI_FAILURE);
5431         }
5432         model = ddi_model_convert_from(mode & FMODELS);
5433         if (model == DDI_MODEL_ILP32) {
5434                 con_log(CL_ANN1, (CE_CONT, "issue_mfi_pthru: DDI_MODEL_LP32"));
5435 
5436                 xferlen = kpthru->sgl.sge32[0].length;
5437 
5438                 ubuf    = (void *)(ulong_t)kpthru->sgl.sge32[0].phys_addr;
5439         } else {
5440 #ifdef _ILP32
5441                 con_log(CL_ANN1, (CE_CONT, "issue_mfi_pthru: DDI_MODEL_LP32"));
5442                 xferlen = kpthru->sgl.sge32[0].length;
5443                 ubuf    = (void *)(ulong_t)kpthru->sgl.sge32[0].phys_addr;
5444 #else
5445                 con_log(CL_ANN1, (CE_CONT, "issue_mfi_pthru: DDI_MODEL_LP64"));
5446                 xferlen = kpthru->sgl.sge64[0].length;
5447                 ubuf    = (void *)(ulong_t)kpthru->sgl.sge64[0].phys_addr;
5448 #endif
5449         }
5450 
5451         if (xferlen) {
5452                 /* means IOCTL requires DMA */
5453                 /* allocate the data transfer buffer */
5454                 /* pthru_dma_obj.size = xferlen; */
5455                 MRSAS_GET_BOUNDARY_ALIGNED_LEN(xferlen, new_xfer_length,
5456                     PAGESIZE);
5457                 pthru_dma_obj.size = new_xfer_length;
5458                 pthru_dma_obj.dma_attr = mrsas_generic_dma_attr;
5459                 pthru_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
5460                 pthru_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
5461                 pthru_dma_obj.dma_attr.dma_attr_sgllen = 1;
5462                 pthru_dma_obj.dma_attr.dma_attr_align = 1;
5463 
5464                 /* allocate kernel buffer for DMA */
5465                 if (mrsas_alloc_dma_obj(instance, &pthru_dma_obj,
5466                     (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
5467                         con_log(CL_ANN, (CE_WARN, "issue_mfi_pthru: "
5468                             "could not allocate data transfer buffer."));
5469                         return (DDI_FAILURE);
5470                 }
5471                 (void) memset(pthru_dma_obj.buffer, 0, xferlen);
5472 
5473                 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */
5474                 if (kpthru->flags & MFI_FRAME_DIR_WRITE) {
5475                         for (i = 0; i < xferlen; i++) {
5476                                 if (ddi_copyin((uint8_t *)ubuf+i,
5477                                     (uint8_t *)pthru_dma_obj.buffer+i,
5478                                     1, mode)) {
5479                                         con_log(CL_ANN, (CE_WARN,
5480                                             "issue_mfi_pthru : "
5481                                             "copy from user space failed"));
5482                                         return (DDI_FAILURE);
5483                                 }
5484                         }
5485                 }
5486 
5487                 kphys_addr = pthru_dma_obj.dma_cookie[0].dmac_address;
5488         }
5489 
5490         ddi_put8(acc_handle, &pthru->cmd, kpthru->cmd);
5491         ddi_put8(acc_handle, &pthru->sense_len, SENSE_LENGTH);
5492         ddi_put8(acc_handle, &pthru->cmd_status, 0);
5493         ddi_put8(acc_handle, &pthru->scsi_status, 0);
5494         ddi_put8(acc_handle, &pthru->target_id, kpthru->target_id);
5495         ddi_put8(acc_handle, &pthru->lun, kpthru->lun);
5496         ddi_put8(acc_handle, &pthru->cdb_len, kpthru->cdb_len);
5497         ddi_put8(acc_handle, &pthru->sge_count, kpthru->sge_count);
5498         ddi_put16(acc_handle, &pthru->timeout, kpthru->timeout);
5499         ddi_put32(acc_handle, &pthru->data_xfer_len, kpthru->data_xfer_len);
5500 
5501         ddi_put32(acc_handle, &pthru->sense_buf_phys_addr_hi, 0);
5502         pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
5503         /* ddi_put32(acc_handle, &pthru->sense_buf_phys_addr_lo, 0); */
5504 
5505         ddi_rep_put8(acc_handle, (uint8_t *)kpthru->cdb, (uint8_t *)pthru->cdb,
5506             pthru->cdb_len, DDI_DEV_AUTOINCR);
5507 
5508         ddi_put16(acc_handle, &pthru->flags, kpthru->flags & ~MFI_FRAME_SGL64);
5509         ddi_put32(acc_handle, &pthru->sgl.sge32[0].length, xferlen);
5510         ddi_put32(acc_handle, &pthru->sgl.sge32[0].phys_addr, kphys_addr);
5511 
5512         cmd->sync_cmd = MRSAS_TRUE;
5513         cmd->frame_count = 1;
5514 
5515         if (instance->tbolt) {
5516                 mr_sas_tbolt_build_mfi_cmd(instance, cmd);
5517         }
5518 
5519         if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) {
5520                 con_log(CL_ANN, (CE_WARN,
5521                     "issue_mfi_pthru: fw_ioctl failed"));
5522         } else {
5523                 if (xferlen && kpthru->flags & MFI_FRAME_DIR_READ) {
5524                         for (i = 0; i < xferlen; i++) {
5525                                 if (ddi_copyout(
5526                                     (uint8_t *)pthru_dma_obj.buffer+i,
5527                                     (uint8_t *)ubuf+i, 1, mode)) {
5528                                         con_log(CL_ANN, (CE_WARN,
5529                                             "issue_mfi_pthru : "
5530                                             "copy to user space failed"));
5531                                         return (DDI_FAILURE);
5532                                 }
5533                         }
5534                 }
5535         }
5536 
5537         kpthru->cmd_status = ddi_get8(acc_handle, &pthru->cmd_status);
5538         kpthru->scsi_status = ddi_get8(acc_handle, &pthru->scsi_status);
5539 
5540         con_log(CL_ANN, (CE_CONT, "issue_mfi_pthru: cmd_status %x, "
5541             "scsi_status %x", kpthru->cmd_status, kpthru->scsi_status));
5542         DTRACE_PROBE3(issue_pthru, uint8_t, kpthru->cmd, uint8_t,
5543             kpthru->cmd_status, uint8_t, kpthru->scsi_status);
5544 
5545         if (kpthru->sense_len) {
5546                 uint_t sense_len = SENSE_LENGTH;
5547                 void *sense_ubuf =
5548                     (void *)(ulong_t)kpthru->sense_buf_phys_addr_lo;
5549                 if (kpthru->sense_len <= SENSE_LENGTH) {
5550                         sense_len = kpthru->sense_len;
5551                 }
5552 
5553                 for (i = 0; i < sense_len; i++) {
5554                         if (ddi_copyout(
5555                             (uint8_t *)cmd->sense+i,
5556                             (uint8_t *)sense_ubuf+i, 1, mode)) {
5557                                 con_log(CL_ANN, (CE_WARN,
5558                                     "issue_mfi_pthru : "
5559                                     "copy to user space failed"));
5560                         }
5561                         con_log(CL_DLEVEL1, (CE_WARN,
5562                             "Copying Sense info sense_buff[%d] = 0x%X",
5563                             i, *((uint8_t *)cmd->sense + i)));
5564                 }
5565         }
5566         (void) ddi_dma_sync(cmd->frame_dma_obj.dma_handle, 0, 0,
5567             DDI_DMA_SYNC_FORDEV);
5568 
5569         if (xferlen) {
5570                 /* free kernel buffer */
5571                 if (mrsas_free_dma_obj(instance, pthru_dma_obj) != DDI_SUCCESS)
5572                         return (DDI_FAILURE);
5573         }
5574 
5575         return (DDI_SUCCESS);
5576 }
5577 
5578 /*
5579  * issue_mfi_dcmd
5580  */
5581 static int
5582 issue_mfi_dcmd(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl,
5583     struct mrsas_cmd *cmd, int mode)
5584 {
5585         void            *ubuf;
5586         uint32_t        kphys_addr = 0;
5587         uint32_t        xferlen = 0;
5588         uint32_t        new_xfer_length = 0;
5589         uint32_t        model;
5590         dma_obj_t       dcmd_dma_obj;
5591         struct mrsas_dcmd_frame *kdcmd;
5592         struct mrsas_dcmd_frame *dcmd;
5593         ddi_acc_handle_t        acc_handle = cmd->frame_dma_obj.acc_handle;
5594         int i;
5595         dcmd = &cmd->frame->dcmd;
5596         kdcmd = (struct mrsas_dcmd_frame *)&ioctl->frame[0];
5597 
5598         if (instance->adapterresetinprogress) {
5599                 con_log(CL_ANN1, (CE_NOTE, "Reset flag set, "
5600                 "returning mfi_pkt and setting TRAN_BUSY"));
5601                 return (DDI_FAILURE);
5602         }
5603         model = ddi_model_convert_from(mode & FMODELS);
5604         if (model == DDI_MODEL_ILP32) {
5605                 con_log(CL_ANN1, (CE_CONT, "issue_mfi_dcmd: DDI_MODEL_ILP32"));
5606 
5607                 xferlen = kdcmd->sgl.sge32[0].length;
5608 
5609                 ubuf    = (void *)(ulong_t)kdcmd->sgl.sge32[0].phys_addr;
5610         } else {
5611 #ifdef _ILP32
5612                 con_log(CL_ANN1, (CE_CONT, "issue_mfi_dcmd: DDI_MODEL_ILP32"));
5613                 xferlen = kdcmd->sgl.sge32[0].length;
5614                 ubuf    = (void *)(ulong_t)kdcmd->sgl.sge32[0].phys_addr;
5615 #else
5616                 con_log(CL_ANN1, (CE_CONT, "issue_mfi_dcmd: DDI_MODEL_LP64"));
5617                 xferlen = kdcmd->sgl.sge64[0].length;
5618                 ubuf    = (void *)(ulong_t)kdcmd->sgl.sge64[0].phys_addr;
5619 #endif
5620         }
5621         if (xferlen) {
5622                 /* means IOCTL requires DMA */
5623                 /* allocate the data transfer buffer */
5624                 /* dcmd_dma_obj.size = xferlen; */
5625                 MRSAS_GET_BOUNDARY_ALIGNED_LEN(xferlen, new_xfer_length,
5626                     PAGESIZE);
5627                 dcmd_dma_obj.size = new_xfer_length;
5628                 dcmd_dma_obj.dma_attr = mrsas_generic_dma_attr;
5629                 dcmd_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
5630                 dcmd_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
5631                 dcmd_dma_obj.dma_attr.dma_attr_sgllen = 1;
5632                 dcmd_dma_obj.dma_attr.dma_attr_align = 1;
5633 
5634                 /* allocate kernel buffer for DMA */
5635                         if (mrsas_alloc_dma_obj(instance, &dcmd_dma_obj,
5636                             (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
5637                                 con_log(CL_ANN,
5638                                     (CE_WARN, "issue_mfi_dcmd: could not "
5639                                     "allocate data transfer buffer."));
5640                                 return (DDI_FAILURE);
5641                         }
5642                 (void) memset(dcmd_dma_obj.buffer, 0, xferlen);
5643 
5644                 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */
5645                 if (kdcmd->flags & MFI_FRAME_DIR_WRITE) {
5646                         for (i = 0; i < xferlen; i++) {
5647                                 if (ddi_copyin((uint8_t *)ubuf + i,
5648                                     (uint8_t *)dcmd_dma_obj.buffer + i,
5649                                     1, mode)) {
5650                                         con_log(CL_ANN, (CE_WARN,
5651                                             "issue_mfi_dcmd : "
5652                                             "copy from user space failed"));
5653                                         return (DDI_FAILURE);
5654                                 }
5655                         }
5656                 }
5657 
5658                 kphys_addr = dcmd_dma_obj.dma_cookie[0].dmac_address;
5659         }
5660 
5661         ddi_put8(acc_handle, &dcmd->cmd, kdcmd->cmd);
5662         ddi_put8(acc_handle, &dcmd->cmd_status, 0);
5663         ddi_put8(acc_handle, &dcmd->sge_count, kdcmd->sge_count);
5664         ddi_put16(acc_handle, &dcmd->timeout, kdcmd->timeout);
5665         ddi_put32(acc_handle, &dcmd->data_xfer_len, kdcmd->data_xfer_len);
5666         ddi_put32(acc_handle, &dcmd->opcode, kdcmd->opcode);
5667 
5668         ddi_rep_put8(acc_handle, (uint8_t *)kdcmd->mbox.b,
5669             (uint8_t *)dcmd->mbox.b, DCMD_MBOX_SZ, DDI_DEV_AUTOINCR);
5670 
5671         ddi_put16(acc_handle, &dcmd->flags, kdcmd->flags & ~MFI_FRAME_SGL64);
5672         ddi_put32(acc_handle, &dcmd->sgl.sge32[0].length, xferlen);
5673         ddi_put32(acc_handle, &dcmd->sgl.sge32[0].phys_addr, kphys_addr);
5674 
5675         cmd->sync_cmd = MRSAS_TRUE;
5676         cmd->frame_count = 1;
5677 
5678         if (instance->tbolt) {
5679                 mr_sas_tbolt_build_mfi_cmd(instance, cmd);
5680         }
5681 
5682         if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) {
5683                 con_log(CL_ANN, (CE_WARN, "issue_mfi_dcmd: fw_ioctl failed"));
5684         } else {
5685                 if (xferlen && (kdcmd->flags & MFI_FRAME_DIR_READ)) {
5686                         for (i = 0; i < xferlen; i++) {
5687                                 if (ddi_copyout(
5688                                     (uint8_t *)dcmd_dma_obj.buffer + i,
5689                                     (uint8_t *)ubuf + i,
5690                                     1, mode)) {
5691                                         con_log(CL_ANN, (CE_WARN,
5692                                             "issue_mfi_dcmd : "
5693                                             "copy to user space failed"));
5694                                         return (DDI_FAILURE);
5695                                 }
5696                         }
5697                 }
5698         }
5699 
5700         kdcmd->cmd_status = ddi_get8(acc_handle, &dcmd->cmd_status);
5701         con_log(CL_ANN,
5702             (CE_CONT, "issue_mfi_dcmd: cmd_status %x", kdcmd->cmd_status));
5703         DTRACE_PROBE3(issue_dcmd, uint32_t, kdcmd->opcode, uint8_t,
5704             kdcmd->cmd, uint8_t, kdcmd->cmd_status);
5705 
5706         if (xferlen) {
5707                 /* free kernel buffer */
5708                 if (mrsas_free_dma_obj(instance, dcmd_dma_obj) != DDI_SUCCESS)
5709                         return (DDI_FAILURE);
5710         }
5711 
5712         return (DDI_SUCCESS);
5713 }
5714 
5715 /*
5716  * issue_mfi_smp
5717  */
5718 static int
5719 issue_mfi_smp(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl,
5720     struct mrsas_cmd *cmd, int mode)
5721 {
5722         void            *request_ubuf;
5723         void            *response_ubuf;
5724         uint32_t        request_xferlen = 0;
5725         uint32_t        response_xferlen = 0;
5726         uint32_t        new_xfer_length1 = 0;
5727         uint32_t        new_xfer_length2 = 0;
5728         uint_t          model;
5729         dma_obj_t                       request_dma_obj;
5730         dma_obj_t                       response_dma_obj;
5731         ddi_acc_handle_t        acc_handle = cmd->frame_dma_obj.acc_handle;
5732         struct mrsas_smp_frame          *ksmp;
5733         struct mrsas_smp_frame          *smp;
5734         struct mrsas_sge32              *sge32;
5735 #ifndef _ILP32
5736         struct mrsas_sge64              *sge64;
5737 #endif
5738         int i;
5739         uint64_t                        tmp_sas_addr;
5740 
5741         smp = &cmd->frame->smp;
5742         ksmp = (struct mrsas_smp_frame *)&ioctl->frame[0];
5743 
5744         if (instance->adapterresetinprogress) {
5745                 con_log(CL_ANN1, (CE_WARN, "Reset flag set, "
5746                 "returning mfi_pkt and setting TRAN_BUSY\n"));
5747                 return (DDI_FAILURE);
5748         }
5749         model = ddi_model_convert_from(mode & FMODELS);
5750         if (model == DDI_MODEL_ILP32) {
5751                 con_log(CL_ANN1, (CE_CONT, "issue_mfi_smp: DDI_MODEL_ILP32"));
5752 
5753                 sge32                   = &ksmp->sgl[0].sge32[0];
5754                 response_xferlen        = sge32[0].length;
5755                 request_xferlen         = sge32[1].length;
5756                 con_log(CL_ANN, (CE_CONT, "issue_mfi_smp: "
5757                     "response_xferlen = %x, request_xferlen = %x",
5758                     response_xferlen, request_xferlen));
5759 
5760                 response_ubuf   = (void *)(ulong_t)sge32[0].phys_addr;
5761                 request_ubuf    = (void *)(ulong_t)sge32[1].phys_addr;
5762                 con_log(CL_ANN1, (CE_CONT, "issue_mfi_smp: "
5763                     "response_ubuf = %p, request_ubuf = %p",
5764                     response_ubuf, request_ubuf));
5765         } else {
5766 #ifdef _ILP32
5767                 con_log(CL_ANN1, (CE_CONT, "issue_mfi_smp: DDI_MODEL_ILP32"));
5768 
5769                 sge32                   = &ksmp->sgl[0].sge32[0];
5770                 response_xferlen        = sge32[0].length;
5771                 request_xferlen         = sge32[1].length;
5772                 con_log(CL_ANN, (CE_CONT, "issue_mfi_smp: "
5773                     "response_xferlen = %x, request_xferlen = %x",
5774                     response_xferlen, request_xferlen));
5775 
5776                 response_ubuf   = (void *)(ulong_t)sge32[0].phys_addr;
5777                 request_ubuf    = (void *)(ulong_t)sge32[1].phys_addr;
5778                 con_log(CL_ANN1, (CE_CONT, "issue_mfi_smp: "
5779                     "response_ubuf = %p, request_ubuf = %p",
5780                     response_ubuf, request_ubuf));
5781 #else
5782                 con_log(CL_ANN1, (CE_CONT, "issue_mfi_smp: DDI_MODEL_LP64"));
5783 
5784                 sge64                   = &ksmp->sgl[0].sge64[0];
5785                 response_xferlen        = sge64[0].length;
5786                 request_xferlen         = sge64[1].length;
5787 
5788                 response_ubuf   = (void *)(ulong_t)sge64[0].phys_addr;
5789                 request_ubuf    = (void *)(ulong_t)sge64[1].phys_addr;
5790 #endif
5791         }
5792         if (request_xferlen) {
5793                 /* means IOCTL requires DMA */
5794                 /* allocate the data transfer buffer */
5795                 /* request_dma_obj.size = request_xferlen; */
5796                 MRSAS_GET_BOUNDARY_ALIGNED_LEN(request_xferlen,
5797                     new_xfer_length1, PAGESIZE);
5798                 request_dma_obj.size = new_xfer_length1;
5799                 request_dma_obj.dma_attr = mrsas_generic_dma_attr;
5800                 request_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
5801                 request_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
5802                 request_dma_obj.dma_attr.dma_attr_sgllen = 1;
5803                 request_dma_obj.dma_attr.dma_attr_align = 1;
5804 
5805                 /* allocate kernel buffer for DMA */
5806                 if (mrsas_alloc_dma_obj(instance, &request_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(request_dma_obj.buffer, 0, request_xferlen);
5813 
5814                 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */
5815                 for (i = 0; i < request_xferlen; i++) {
5816                         if (ddi_copyin((uint8_t *)request_ubuf + i,
5817                             (uint8_t *)request_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         if (response_xferlen) {
5827                 /* means IOCTL requires DMA */
5828                 /* allocate the data transfer buffer */
5829                 /* response_dma_obj.size = response_xferlen; */
5830                 MRSAS_GET_BOUNDARY_ALIGNED_LEN(response_xferlen,
5831                     new_xfer_length2, PAGESIZE);
5832                 response_dma_obj.size = new_xfer_length2;
5833                 response_dma_obj.dma_attr = mrsas_generic_dma_attr;
5834                 response_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
5835                 response_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
5836                 response_dma_obj.dma_attr.dma_attr_sgllen = 1;
5837                 response_dma_obj.dma_attr.dma_attr_align = 1;
5838 
5839                 /* allocate kernel buffer for DMA */
5840                 if (mrsas_alloc_dma_obj(instance, &response_dma_obj,
5841                     (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
5842                         con_log(CL_ANN, (CE_WARN, "issue_mfi_smp: "
5843                             "could not allocate data transfer buffer."));
5844                         return (DDI_FAILURE);
5845                 }
5846                 (void) memset(response_dma_obj.buffer, 0, response_xferlen);
5847 
5848                 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */
5849                 for (i = 0; i < response_xferlen; i++) {
5850                         if (ddi_copyin((uint8_t *)response_ubuf + i,
5851                             (uint8_t *)response_dma_obj.buffer + i,
5852                             1, mode)) {


5856                         }
5857                 }
5858         }
5859 
5860         ddi_put8(acc_handle, &smp->cmd, ksmp->cmd);
5861         ddi_put8(acc_handle, &smp->cmd_status, 0);
5862         ddi_put8(acc_handle, &smp->connection_status, 0);
5863         ddi_put8(acc_handle, &smp->sge_count, ksmp->sge_count);
5864         /* smp->context              = ksmp->context; */
5865         ddi_put16(acc_handle, &smp->timeout, ksmp->timeout);
5866         ddi_put32(acc_handle, &smp->data_xfer_len, ksmp->data_xfer_len);
5867 
5868         bcopy((void *)&ksmp->sas_addr, (void *)&tmp_sas_addr,
5869             sizeof (uint64_t));
5870         ddi_put64(acc_handle, &smp->sas_addr, tmp_sas_addr);
5871 
5872         ddi_put16(acc_handle, &smp->flags, ksmp->flags & ~MFI_FRAME_SGL64);
5873 
5874         model = ddi_model_convert_from(mode & FMODELS);
5875         if (model == DDI_MODEL_ILP32) {
5876                 con_log(CL_ANN1, (CE_CONT,
5877                     "issue_mfi_smp: DDI_MODEL_ILP32"));
5878 
5879                 sge32 = &smp->sgl[0].sge32[0];
5880                 ddi_put32(acc_handle, &sge32[0].length, response_xferlen);
5881                 ddi_put32(acc_handle, &sge32[0].phys_addr,
5882                     response_dma_obj.dma_cookie[0].dmac_address);
5883                 ddi_put32(acc_handle, &sge32[1].length, request_xferlen);
5884                 ddi_put32(acc_handle, &sge32[1].phys_addr,
5885                     request_dma_obj.dma_cookie[0].dmac_address);
5886         } else {
5887 #ifdef _ILP32
5888                 con_log(CL_ANN1, (CE_CONT,
5889                     "issue_mfi_smp: DDI_MODEL_ILP32"));
5890                 sge32 = &smp->sgl[0].sge32[0];
5891                 ddi_put32(acc_handle, &sge32[0].length, response_xferlen);
5892                 ddi_put32(acc_handle, &sge32[0].phys_addr,
5893                     response_dma_obj.dma_cookie[0].dmac_address);
5894                 ddi_put32(acc_handle, &sge32[1].length, request_xferlen);
5895                 ddi_put32(acc_handle, &sge32[1].phys_addr,
5896                     request_dma_obj.dma_cookie[0].dmac_address);
5897 #else
5898                 con_log(CL_ANN1, (CE_CONT,
5899                     "issue_mfi_smp: DDI_MODEL_LP64"));
5900                 sge64 = &smp->sgl[0].sge64[0];
5901                 ddi_put32(acc_handle, &sge64[0].length, response_xferlen);
5902                 ddi_put64(acc_handle, &sge64[0].phys_addr,
5903                     response_dma_obj.dma_cookie[0].dmac_address);
5904                 ddi_put32(acc_handle, &sge64[1].length, request_xferlen);
5905                 ddi_put64(acc_handle, &sge64[1].phys_addr,
5906                     request_dma_obj.dma_cookie[0].dmac_address);
5907 #endif
5908         }
5909         con_log(CL_ANN1, (CE_CONT, "issue_mfi_smp : "
5910             "smp->response_xferlen = %d, smp->request_xferlen = %d "
5911             "smp->data_xfer_len = %d", ddi_get32(acc_handle, &sge32[0].length),
5912             ddi_get32(acc_handle, &sge32[1].length),
5913             ddi_get32(acc_handle, &smp->data_xfer_len)));
5914 
5915         cmd->sync_cmd = MRSAS_TRUE;
5916         cmd->frame_count = 1;
5917 
5918         if (instance->tbolt) {
5919                 mr_sas_tbolt_build_mfi_cmd(instance, cmd);
5920         }
5921 
5922         if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) {
5923                 con_log(CL_ANN, (CE_WARN,
5924                     "issue_mfi_smp: fw_ioctl failed"));
5925         } else {
5926                 con_log(CL_ANN1, (CE_CONT,
5927                     "issue_mfi_smp: copy to user space"));
5928 
5929                 if (request_xferlen) {
5930                         for (i = 0; i < request_xferlen; i++) {
5931                                 if (ddi_copyout(
5932                                     (uint8_t *)request_dma_obj.buffer +
5933                                     i, (uint8_t *)request_ubuf + i,
5934                                     1, mode)) {
5935                                         con_log(CL_ANN, (CE_WARN,
5936                                             "issue_mfi_smp : copy to user space"
5937                                             " failed"));
5938                                         return (DDI_FAILURE);
5939                                 }
5940                         }
5941                 }
5942 
5943                 if (response_xferlen) {
5944                         for (i = 0; i < response_xferlen; i++) {
5945                                 if (ddi_copyout(
5946                                     (uint8_t *)response_dma_obj.buffer
5947                                     + i, (uint8_t *)response_ubuf
5948                                     + i, 1, mode)) {
5949                                         con_log(CL_ANN, (CE_WARN,
5950                                             "issue_mfi_smp : copy to "
5951                                             "user space failed"));
5952                                         return (DDI_FAILURE);
5953                                 }
5954                         }
5955                 }
5956         }
5957 
5958         ksmp->cmd_status = ddi_get8(acc_handle, &smp->cmd_status);
5959         con_log(CL_ANN1, (CE_NOTE, "issue_mfi_smp: smp->cmd_status = %d",
5960             ksmp->cmd_status));
5961         DTRACE_PROBE2(issue_smp, uint8_t, ksmp->cmd, uint8_t, ksmp->cmd_status);
5962 
5963         if (request_xferlen) {
5964                 /* free kernel buffer */
5965                 if (mrsas_free_dma_obj(instance, request_dma_obj) !=
5966                     DDI_SUCCESS)
5967                         return (DDI_FAILURE);
5968         }
5969 
5970         if (response_xferlen) {
5971                 /* free kernel buffer */
5972                 if (mrsas_free_dma_obj(instance, response_dma_obj) !=
5973                     DDI_SUCCESS)
5974                         return (DDI_FAILURE);
5975         }
5976 
5977         return (DDI_SUCCESS);
5978 }
5979 
5980 /*
5981  * issue_mfi_stp
5982  */
5983 static int
5984 issue_mfi_stp(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl,
5985     struct mrsas_cmd *cmd, int mode)
5986 {
5987         void            *fis_ubuf;
5988         void            *data_ubuf;
5989         uint32_t        fis_xferlen = 0;
5990         uint32_t   new_xfer_length1 = 0;
5991         uint32_t   new_xfer_length2 = 0;
5992         uint32_t        data_xferlen = 0;
5993         uint_t          model;
5994         dma_obj_t       fis_dma_obj;
5995         dma_obj_t       data_dma_obj;
5996         struct mrsas_stp_frame  *kstp;
5997         struct mrsas_stp_frame  *stp;
5998         ddi_acc_handle_t        acc_handle = cmd->frame_dma_obj.acc_handle;
5999         int i;
6000 
6001         stp = &cmd->frame->stp;
6002         kstp = (struct mrsas_stp_frame *)&ioctl->frame[0];
6003 
6004         if (instance->adapterresetinprogress) {
6005                 con_log(CL_ANN1, (CE_WARN, "Reset flag set, "
6006                 "returning mfi_pkt and setting TRAN_BUSY\n"));
6007                 return (DDI_FAILURE);
6008         }
6009         model = ddi_model_convert_from(mode & FMODELS);
6010         if (model == DDI_MODEL_ILP32) {
6011                 con_log(CL_ANN1, (CE_CONT, "issue_mfi_stp: DDI_MODEL_ILP32"));
6012 
6013                 fis_xferlen     = kstp->sgl.sge32[0].length;
6014                 data_xferlen    = kstp->sgl.sge32[1].length;
6015 
6016                 fis_ubuf        = (void *)(ulong_t)kstp->sgl.sge32[0].phys_addr;
6017                 data_ubuf       = (void *)(ulong_t)kstp->sgl.sge32[1].phys_addr;
6018         } else {


6019 #ifdef _ILP32
6020                 con_log(CL_ANN1, (CE_CONT, "issue_mfi_stp: DDI_MODEL_ILP32"));
6021 
6022                 fis_xferlen     = kstp->sgl.sge32[0].length;
6023                 data_xferlen    = kstp->sgl.sge32[1].length;
6024 
6025                 fis_ubuf        = (void *)(ulong_t)kstp->sgl.sge32[0].phys_addr;
6026                 data_ubuf       = (void *)(ulong_t)kstp->sgl.sge32[1].phys_addr;
6027 #else
6028                 con_log(CL_ANN1, (CE_CONT, "issue_mfi_stp: DDI_MODEL_LP64"));
6029 
6030                 fis_xferlen     = kstp->sgl.sge64[0].length;
6031                 data_xferlen    = kstp->sgl.sge64[1].length;
6032 
6033                 fis_ubuf        = (void *)(ulong_t)kstp->sgl.sge64[0].phys_addr;
6034                 data_ubuf       = (void *)(ulong_t)kstp->sgl.sge64[1].phys_addr;
6035 #endif
6036         }
6037 
6038 
6039         if (fis_xferlen) {
6040                 con_log(CL_ANN, (CE_CONT, "issue_mfi_stp: "
6041                     "fis_ubuf = %p fis_xferlen = %x", fis_ubuf, fis_xferlen));
6042 
6043                 /* means IOCTL requires DMA */
6044                 /* allocate the data transfer buffer */
6045                 /* fis_dma_obj.size = fis_xferlen; */
6046                 MRSAS_GET_BOUNDARY_ALIGNED_LEN(fis_xferlen,
6047                     new_xfer_length1, PAGESIZE);
6048                 fis_dma_obj.size = new_xfer_length1;
6049                 fis_dma_obj.dma_attr = mrsas_generic_dma_attr;
6050                 fis_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
6051                 fis_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
6052                 fis_dma_obj.dma_attr.dma_attr_sgllen = 1;
6053                 fis_dma_obj.dma_attr.dma_attr_align = 1;
6054 
6055                 /* allocate kernel buffer for DMA */
6056                 if (mrsas_alloc_dma_obj(instance, &fis_dma_obj,
6057                     (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
6058                         con_log(CL_ANN, (CE_WARN, "issue_mfi_stp : "
6059                             "could not allocate data transfer buffer."));
6060                         return (DDI_FAILURE);
6061                 }
6062                 (void) memset(fis_dma_obj.buffer, 0, fis_xferlen);
6063 
6064                 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */
6065                 for (i = 0; i < fis_xferlen; i++) {
6066                         if (ddi_copyin((uint8_t *)fis_ubuf + i,
6067                             (uint8_t *)fis_dma_obj.buffer + i, 1, mode)) {
6068                                 con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: "
6069                                     "copy from user space failed"));
6070                                 return (DDI_FAILURE);
6071                         }
6072                 }
6073         }
6074 
6075         if (data_xferlen) {
6076                 con_log(CL_ANN, (CE_CONT, "issue_mfi_stp: data_ubuf = %p "
6077                     "data_xferlen = %x", data_ubuf, data_xferlen));
6078 
6079                 /* means IOCTL requires DMA */
6080                 /* allocate the data transfer buffer */
6081                 /* data_dma_obj.size = data_xferlen; */
6082                 MRSAS_GET_BOUNDARY_ALIGNED_LEN(data_xferlen, new_xfer_length2,
6083                     PAGESIZE);
6084                 data_dma_obj.size = new_xfer_length2;
6085                 data_dma_obj.dma_attr = mrsas_generic_dma_attr;
6086                 data_dma_obj.dma_attr.dma_attr_addr_hi = 0xFFFFFFFFU;
6087                 data_dma_obj.dma_attr.dma_attr_count_max = 0xFFFFFFFFU;
6088                 data_dma_obj.dma_attr.dma_attr_sgllen = 1;
6089                 data_dma_obj.dma_attr.dma_attr_align = 1;
6090 
6091                 /* allocate kernel buffer for DMA */
6092                 if (mrsas_alloc_dma_obj(instance, &data_dma_obj,
6093                     (uchar_t)DDI_STRUCTURE_LE_ACC) != 1) {
6094                         con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: "
6095                             "could not allocate data transfer buffer."));
6096                         return (DDI_FAILURE);
6097                 }
6098                 (void) memset(data_dma_obj.buffer, 0, data_xferlen);
6099 
6100                 /* If IOCTL requires DMA WRITE, do ddi_copyin IOCTL data copy */
6101                 for (i = 0; i < data_xferlen; i++) {
6102                         if (ddi_copyin((uint8_t *)data_ubuf + i,
6103                             (uint8_t *)data_dma_obj.buffer + i, 1, mode)) {
6104                                 con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: "
6105                                     "copy from user space failed"));
6106                                 return (DDI_FAILURE);
6107                         }
6108                 }
6109         }
6110 
6111         ddi_put8(acc_handle, &stp->cmd, kstp->cmd);


6115         ddi_put8(acc_handle, &stp->sge_count, kstp->sge_count);
6116 
6117         ddi_put16(acc_handle, &stp->timeout, kstp->timeout);
6118         ddi_put32(acc_handle, &stp->data_xfer_len, kstp->data_xfer_len);
6119 
6120         ddi_rep_put8(acc_handle, (uint8_t *)kstp->fis, (uint8_t *)stp->fis, 10,
6121             DDI_DEV_AUTOINCR);
6122 
6123         ddi_put16(acc_handle, &stp->flags, kstp->flags & ~MFI_FRAME_SGL64);
6124         ddi_put32(acc_handle, &stp->stp_flags, kstp->stp_flags);
6125         ddi_put32(acc_handle, &stp->sgl.sge32[0].length, fis_xferlen);
6126         ddi_put32(acc_handle, &stp->sgl.sge32[0].phys_addr,
6127             fis_dma_obj.dma_cookie[0].dmac_address);
6128         ddi_put32(acc_handle, &stp->sgl.sge32[1].length, data_xferlen);
6129         ddi_put32(acc_handle, &stp->sgl.sge32[1].phys_addr,
6130             data_dma_obj.dma_cookie[0].dmac_address);
6131 
6132         cmd->sync_cmd = MRSAS_TRUE;
6133         cmd->frame_count = 1;
6134 
6135         if (instance->tbolt) {
6136                 mr_sas_tbolt_build_mfi_cmd(instance, cmd);
6137         }
6138 
6139         if (instance->func_ptr->issue_cmd_in_sync_mode(instance, cmd)) {
6140                 con_log(CL_ANN, (CE_WARN, "issue_mfi_stp: fw_ioctl failed"));
6141         } else {
6142 
6143                 if (fis_xferlen) {
6144                         for (i = 0; i < fis_xferlen; i++) {
6145                                 if (ddi_copyout(
6146                                     (uint8_t *)fis_dma_obj.buffer + i,
6147                                     (uint8_t *)fis_ubuf + i, 1, mode)) {
6148                                         con_log(CL_ANN, (CE_WARN,
6149                                             "issue_mfi_stp : copy to "
6150                                             "user space failed"));
6151                                         return (DDI_FAILURE);
6152                                 }
6153                         }
6154                 }
6155         }
6156         if (data_xferlen) {
6157                 for (i = 0; i < data_xferlen; i++) {
6158                         if (ddi_copyout(
6159                             (uint8_t *)data_dma_obj.buffer + i,
6160                             (uint8_t *)data_ubuf + i, 1, mode)) {
6161                                 con_log(CL_ANN, (CE_WARN,
6162                                     "issue_mfi_stp : copy to"
6163                                     " user space failed"));
6164                                 return (DDI_FAILURE);
6165                         }
6166                 }
6167         }
6168 
6169         kstp->cmd_status = ddi_get8(acc_handle, &stp->cmd_status);
6170         con_log(CL_ANN1, (CE_NOTE, "issue_mfi_stp: stp->cmd_status = %d",
6171             kstp->cmd_status));
6172         DTRACE_PROBE2(issue_stp, uint8_t, kstp->cmd, uint8_t, kstp->cmd_status);
6173 
6174         if (fis_xferlen) {
6175                 /* free kernel buffer */
6176                 if (mrsas_free_dma_obj(instance, fis_dma_obj) != DDI_SUCCESS)
6177                         return (DDI_FAILURE);
6178         }
6179 
6180         if (data_xferlen) {
6181                 /* free kernel buffer */
6182                 if (mrsas_free_dma_obj(instance, data_dma_obj) != DDI_SUCCESS)
6183                         return (DDI_FAILURE);
6184         }
6185 
6186         return (DDI_SUCCESS);
6187 }
6188 
6189 /*
6190  * fill_up_drv_ver
6191  */
6192 void
6193 fill_up_drv_ver(struct mrsas_drv_ver *dv)
6194 {
6195         (void) memset(dv, 0, sizeof (struct mrsas_drv_ver));
6196 
6197         (void) memcpy(dv->signature, "$LSI LOGIC$", strlen("$LSI LOGIC$"));
6198         (void) memcpy(dv->os_name, "Solaris", strlen("Solaris"));
6199         (void) memcpy(dv->drv_name, "mr_sas", strlen("mr_sas"));
6200         (void) memcpy(dv->drv_ver, MRSAS_VERSION, strlen(MRSAS_VERSION));
6201         (void) memcpy(dv->drv_rel_date, MRSAS_RELDATE,
6202             strlen(MRSAS_RELDATE));
6203 
6204 }
6205 
6206 /*
6207  * handle_drv_ioctl
6208  */
6209 static int
6210 handle_drv_ioctl(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl,
6211     int mode)
6212 {
6213         int     i;
6214         int     rval = DDI_SUCCESS;
6215         int     *props = NULL;
6216         void    *ubuf;
6217 
6218         uint8_t         *pci_conf_buf;
6219         uint32_t        xferlen;
6220         uint32_t        num_props;
6221         uint_t          model;
6222         struct mrsas_dcmd_frame *kdcmd;
6223         struct mrsas_drv_ver    dv;
6224         struct mrsas_pci_information pi;
6225 
6226         kdcmd = (struct mrsas_dcmd_frame *)&ioctl->frame[0];
6227 
6228         model = ddi_model_convert_from(mode & FMODELS);
6229         if (model == DDI_MODEL_ILP32) {
6230                 con_log(CL_ANN1, (CE_CONT,
6231                     "handle_drv_ioctl: DDI_MODEL_ILP32"));
6232 
6233                 xferlen = kdcmd->sgl.sge32[0].length;
6234 
6235                 ubuf = (void *)(ulong_t)kdcmd->sgl.sge32[0].phys_addr;
6236         } else {
6237 #ifdef _ILP32
6238                 con_log(CL_ANN1, (CE_CONT,
6239                     "handle_drv_ioctl: DDI_MODEL_ILP32"));
6240                 xferlen = kdcmd->sgl.sge32[0].length;
6241                 ubuf = (void *)(ulong_t)kdcmd->sgl.sge32[0].phys_addr;
6242 #else
6243                 con_log(CL_ANN1, (CE_CONT,
6244                     "handle_drv_ioctl: DDI_MODEL_LP64"));
6245                 xferlen = kdcmd->sgl.sge64[0].length;
6246                 ubuf = (void *)(ulong_t)kdcmd->sgl.sge64[0].phys_addr;
6247 #endif
6248         }
6249         con_log(CL_ANN1, (CE_CONT, "handle_drv_ioctl: "
6250             "dataBuf=%p size=%d bytes", ubuf, xferlen));
6251 
6252         switch (kdcmd->opcode) {
6253         case MRSAS_DRIVER_IOCTL_DRIVER_VERSION:
6254                 con_log(CL_ANN1, (CE_CONT, "handle_drv_ioctl: "
6255                     "MRSAS_DRIVER_IOCTL_DRIVER_VERSION"));
6256 
6257                 fill_up_drv_ver(&dv);
6258 
6259                 if (ddi_copyout(&dv, ubuf, xferlen, mode)) {
6260                         con_log(CL_ANN, (CE_WARN, "handle_drv_ioctl: "
6261                             "MRSAS_DRIVER_IOCTL_DRIVER_VERSION : "
6262                             "copy to user space failed"));
6263                         kdcmd->cmd_status = 1;
6264                         rval = 1;
6265                 } else {
6266                         kdcmd->cmd_status = 0;
6267                 }
6268                 break;
6269         case MRSAS_DRIVER_IOCTL_PCI_INFORMATION:
6270                 con_log(CL_ANN1, (CE_NOTE, "handle_drv_ioctl: "
6271                     "MRSAS_DRIVER_IOCTL_PCI_INFORMAITON"));
6272 
6273                 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, instance->dip,
6274                     0, "reg", &props, &num_props)) {


6310                 kdcmd->cmd_status = 1;
6311                 rval = DDI_FAILURE;
6312                 break;
6313         }
6314 
6315         return (rval);
6316 }
6317 
6318 /*
6319  * handle_mfi_ioctl
6320  */
6321 static int
6322 handle_mfi_ioctl(struct mrsas_instance *instance, struct mrsas_ioctl *ioctl,
6323     int mode)
6324 {
6325         int     rval = DDI_SUCCESS;
6326 
6327         struct mrsas_header     *hdr;
6328         struct mrsas_cmd        *cmd;
6329 
6330         if (instance->tbolt) {
6331                 cmd = get_raid_msg_mfi_pkt(instance);
6332         } else {
6333                 cmd = get_mfi_pkt(instance);
6334         }
6335         if (!cmd) {
6336                 con_log(CL_ANN, (CE_WARN, "mr_sas: "
6337                     "failed to get a cmd packet"));
6338                 DTRACE_PROBE2(mfi_ioctl_err, uint16_t,
6339                     instance->fw_outstanding, uint16_t, instance->max_fw_cmds);
6340                 return (DDI_FAILURE);
6341         }

6342 
6343         /* Clear the frame buffer and assign back the context id */
6344         (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
6345         ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
6346             cmd->index);
6347 
6348         hdr = (struct mrsas_header *)&ioctl->frame[0];
6349 
6350         switch (ddi_get8(cmd->frame_dma_obj.acc_handle, &hdr->cmd)) {
6351         case MFI_CMD_OP_DCMD:
6352                 rval = issue_mfi_dcmd(instance, ioctl, cmd, mode);
6353                 break;
6354         case MFI_CMD_OP_SMP:
6355                 rval = issue_mfi_smp(instance, ioctl, cmd, mode);
6356                 break;
6357         case MFI_CMD_OP_STP:
6358                 rval = issue_mfi_stp(instance, ioctl, cmd, mode);
6359                 break;
6360         case MFI_CMD_OP_LD_SCSI:
6361         case MFI_CMD_OP_PD_SCSI:
6362                 rval = issue_mfi_pthru(instance, ioctl, cmd, mode);
6363                 break;
6364         default:
6365                 con_log(CL_ANN, (CE_WARN, "handle_mfi_ioctl: "
6366                     "invalid mfi ioctl hdr->cmd = %d", hdr->cmd));
6367                 rval = DDI_FAILURE;
6368                 break;
6369         }
6370 
6371         if (mrsas_common_check(instance, cmd) != DDI_SUCCESS)
6372                 rval = DDI_FAILURE;
6373 
6374         if (instance->tbolt) {
6375                 return_raid_msg_mfi_pkt(instance, cmd);
6376         } else {
6377                 return_mfi_pkt(instance, cmd);
6378         }
6379 
6380         return (rval);
6381 }
6382 
6383 /*
6384  * AEN
6385  */
6386 static int
6387 handle_mfi_aen(struct mrsas_instance *instance, struct mrsas_aen *aen)
6388 {
6389         int     rval = 0;
6390 
6391         rval = register_mfi_aen(instance, instance->aen_seq_num,
6392             aen->class_locale_word);
6393 
6394         aen->cmd_status = (uint8_t)rval;
6395 
6396         return (rval);
6397 }
6398 
6399 static int
6400 register_mfi_aen(struct mrsas_instance *instance, uint32_t seq_num,
6401     uint32_t class_locale_word)
6402 {
6403         int     ret_val;
6404 
6405         struct mrsas_cmd        *cmd, *aen_cmd;
6406         struct mrsas_dcmd_frame *dcmd;
6407         union mrsas_evt_class_locale    curr_aen;
6408         union mrsas_evt_class_locale    prev_aen;
6409 
6410         con_log(CL_ANN, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
6411         /*
6412          * If there an AEN pending already (aen_cmd), check if the
6413          * class_locale of that pending AEN is inclusive of the new
6414          * AEN request we currently have. If it is, then we don't have
6415          * to do anything. In other words, whichever events the current
6416          * AEN request is subscribing to, have already been subscribed
6417          * to.
6418          *
6419          * If the old_cmd is _not_ inclusive, then we have to abort
6420          * that command, form a class_locale that is superset of both
6421          * old and current and re-issue to the FW
6422          */
6423 
6424         curr_aen.word = LE_32(class_locale_word);
6425         curr_aen.members.locale = LE_16(curr_aen.members.locale);
6426         aen_cmd = instance->aen_cmd;
6427         if (aen_cmd) {
6428                 prev_aen.word = ddi_get32(aen_cmd->frame_dma_obj.acc_handle,
6429                     &aen_cmd->frame->dcmd.mbox.w[1]);
6430                 prev_aen.word = LE_32(prev_aen.word);


6451                 } else {
6452                         curr_aen.members.locale |= prev_aen.members.locale;
6453 
6454                         if (prev_aen.members.class < curr_aen.members.class)
6455                                 curr_aen.members.class = prev_aen.members.class;
6456 
6457                         ret_val = abort_aen_cmd(instance, aen_cmd);
6458 
6459                         if (ret_val) {
6460                                 con_log(CL_ANN, (CE_WARN, "register_mfi_aen: "
6461                                     "failed to abort prevous AEN command"));
6462 
6463                                 return (ret_val);
6464                         }
6465                 }
6466         } else {
6467                 curr_aen.word = LE_32(class_locale_word);
6468                 curr_aen.members.locale = LE_16(curr_aen.members.locale);
6469         }
6470 
6471         if (instance->tbolt) {
6472                 cmd = get_raid_msg_mfi_pkt(instance);
6473         } else {
6474                 cmd = get_mfi_pkt(instance);
6475         }
6476 
6477         if (!cmd) {
6478                 DTRACE_PROBE2(mfi_aen_err, uint16_t, instance->fw_outstanding,
6479                     uint16_t, instance->max_fw_cmds);
6480                 return (ENOMEM);
6481         }
6482 
6483         /* Clear the frame buffer and assign back the context id */
6484         (void) memset((char *)&cmd->frame[0], 0, sizeof (union mrsas_frame));
6485         ddi_put32(cmd->frame_dma_obj.acc_handle, &cmd->frame->hdr.context,
6486             cmd->index);
6487 
6488         dcmd = &cmd->frame->dcmd;
6489 
6490         /* for(i = 0; i < DCMD_MBOX_SZ; i++) dcmd->mbox.b[i] = 0; */
6491         (void) memset(dcmd->mbox.b, 0, DCMD_MBOX_SZ);
6492 
6493         (void) memset(instance->mfi_evt_detail_obj.buffer, 0,
6494             sizeof (struct mrsas_evt_detail));
6495 
6496         /* Prepare DCMD for aen registration */
6497         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd, MFI_CMD_OP_DCMD);
6498         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->cmd_status, 0x0);
6499         ddi_put8(cmd->frame_dma_obj.acc_handle, &dcmd->sge_count, 1);
6500         ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->flags,
6501             MFI_FRAME_DIR_READ);
6502         ddi_put16(cmd->frame_dma_obj.acc_handle, &dcmd->timeout, 0);


6511             curr_aen.word);
6512         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].phys_addr,
6513             instance->mfi_evt_detail_obj.dma_cookie[0].dmac_address);
6514         ddi_put32(cmd->frame_dma_obj.acc_handle, &dcmd->sgl.sge32[0].length,
6515             sizeof (struct mrsas_evt_detail));
6516 
6517         instance->aen_seq_num = seq_num;
6518 
6519 
6520         /*
6521          * Store reference to the cmd used to register for AEN. When an
6522          * application wants us to register for AEN, we have to abort this
6523          * cmd and re-register with a new EVENT LOCALE supplied by that app
6524          */
6525         instance->aen_cmd = cmd;
6526 
6527         cmd->frame_count = 1;
6528 
6529         /* Issue the aen registration frame */
6530         /* atomic_add_16 (&instance->fw_outstanding, 1); */
6531         if (instance->tbolt) {
6532                 mr_sas_tbolt_build_mfi_cmd(instance, cmd);
6533         }
6534         instance->func_ptr->issue_cmd(cmd, instance);
6535 
6536         return (0);
6537 }
6538 
6539 void
6540 display_scsi_inquiry(caddr_t scsi_inq)
6541 {
6542 #define MAX_SCSI_DEVICE_CODE    14
6543         int             i;
6544         char            inquiry_buf[256] = {0};
6545         int             len;
6546         const char      *const scsi_device_types[] = {
6547                 "Direct-Access    ",
6548                 "Sequential-Access",
6549                 "Printer          ",
6550                 "Processor        ",
6551                 "WORM             ",
6552                 "CD-ROM           ",
6553                 "Scanner          ",
6554                 "Optical Device   ",
6555                 "Medium Changer   ",
6556                 "Communications   ",
6557                 "Unknown          ",
6558                 "Unknown          ",
6559                 "Unknown          ",


6585         len += snprintf(inquiry_buf + len, 265 - len, "\n");
6586 
6587 
6588         i = scsi_inq[0] & 0x1f;
6589 
6590 
6591         len += snprintf(inquiry_buf + len, 265 - len, "  Type:   %s ",
6592             i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] :
6593             "Unknown          ");
6594 
6595 
6596         len += snprintf(inquiry_buf + len, 265 - len,
6597             "                 ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
6598 
6599         if ((scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1) {
6600                 len += snprintf(inquiry_buf + len, 265 - len, " CCS\n");
6601         } else {
6602                 len += snprintf(inquiry_buf + len, 265 - len, "\n");
6603         }
6604 
6605         con_log(CL_DLEVEL2, (CE_CONT, inquiry_buf));
6606 }
6607 
6608 static void
6609 io_timeout_checker(void *arg)
6610 {
6611         struct scsi_pkt *pkt;
6612         struct mrsas_instance *instance = arg;
6613         struct mrsas_cmd        *cmd = NULL;
6614         struct mrsas_header     *hdr;
6615         int time = 0;
6616         int counter = 0;
6617         struct mlist_head       *pos, *next;
6618         mlist_t                 process_list;
6619 
6620         if (instance->adapterresetinprogress == 1) {
6621                 con_log(CL_ANN, (CE_NOTE, "io_timeout_checker:"
6622                     " reset in progress"));
6623 
6624                 instance->timeout_id = timeout(io_timeout_checker,
6625                     (void *) instance, drv_usectohz(MRSAS_1_SECOND));
6626                 return;
6627         }
6628 
6629         /* See if this check needs to be in the beginning or last in ISR */
6630         if (mrsas_initiate_ocr_if_fw_is_faulty(instance) ==  1) {
6631                 cmn_err(CE_WARN, "io_timeout_checker: "
6632                     "FW Fault, calling reset adapter");
6633                 cmn_err(CE_CONT, "io_timeout_checker: "
6634                     "fw_outstanding 0x%X max_fw_cmds 0x%X",
6635                     instance->fw_outstanding, instance->max_fw_cmds);
6636                 if (instance->adapterresetinprogress == 0) {
6637                         instance->adapterresetinprogress = 1;
6638                         if (instance->tbolt)
6639                                 (void) mrsas_tbolt_reset_ppc(instance);
6640                         else
6641                                 (void) mrsas_reset_ppc(instance);
6642                         instance->adapterresetinprogress = 0;
6643                 }
6644                 instance->timeout_id = timeout(io_timeout_checker,
6645                     (void *) instance, drv_usectohz(MRSAS_1_SECOND));
6646                 return;
6647         }
6648 
6649         INIT_LIST_HEAD(&process_list);
6650 
6651         mutex_enter(&instance->cmd_pend_mtx);
6652         mlist_for_each_safe(pos, next, &instance->cmd_pend_list) {
6653                 cmd = mlist_entry(pos, struct mrsas_cmd, list);
6654 
6655                 if (cmd == NULL) {
6656                         continue;
6657                 }
6658 
6659                 if (cmd->sync_cmd == MRSAS_TRUE) {
6660                         hdr = (struct mrsas_header *)&cmd->frame->hdr;
6661                         if (hdr == NULL) {
6662                                 continue;
6663                         }
6664                         time = --cmd->drv_pkt_time;
6665                 } else {
6666                         pkt = cmd->pkt;
6667                         if (pkt == NULL) {
6668                                 continue;
6669                         }
6670                         time = --cmd->drv_pkt_time;
6671                 }
6672                 if (time <= 0) {
6673                         cmn_err(CE_WARN, "%llx: "
6674                             "io_timeout_checker: TIMING OUT: pkt: %p, "
6675                             "cmd %p fw_outstanding 0x%X max_fw_cmds 0x%X\n",
6676                             gethrtime(), (void *)pkt, (void *)cmd,
6677                             instance->fw_outstanding, instance->max_fw_cmds);
6678 
6679                         counter++;
6680                         break;
6681                 }
6682         }
6683         mutex_exit(&instance->cmd_pend_mtx);
6684 
6685         if (counter) {






6686                 if (instance->disable_online_ctrl_reset == 1) {
6687                         cmn_err(CE_WARN, "mr_sas %d: %s(): OCR is NOT "
6688                             "supported by Firmware, KILL adapter!!!",
6689                             instance->instance, __func__);
6690 
6691                         if (instance->tbolt)
6692                                 mrsas_tbolt_kill_adapter(instance);
6693                         else
6694                                 (void) mrsas_kill_adapter(instance);
6695 
6696                         return;
6697                 } else {
6698                         if (cmd->retry_count_for_ocr <=    IO_RETRY_COUNT) {
6699                                 if (instance->adapterresetinprogress == 0) {
6700                                         if (instance->tbolt) {
6701                                                 (void) mrsas_tbolt_reset_ppc(
6702                                                     instance);
6703                                         } else {
6704                                                 (void) mrsas_reset_ppc(
6705                                                     instance);
6706                                         }
6707                                 }
6708                         } else {
6709                                 cmn_err(CE_WARN,
6710                                     "io_timeout_checker: "
6711                                     "cmd %p cmd->index %d "
6712                                     "timed out even after 3 resets: "
6713                                     "so KILL adapter", (void *)cmd, cmd->index);
6714 
6715                                 mrsas_print_cmd_details(instance, cmd, 0xDD);
6716 
6717                                 if (instance->tbolt)
6718                                         mrsas_tbolt_kill_adapter(instance);
6719                                 else
6720                                         (void) mrsas_kill_adapter(instance);
6721                                 return;
6722                         }
6723                 }
6724         }
6725         con_log(CL_ANN, (CE_NOTE, "mrsas: "


6726             "schedule next timeout check: "
6727             "do timeout \n"));
6728         instance->timeout_id =
6729             timeout(io_timeout_checker, (void *)instance,
6730             drv_usectohz(MRSAS_1_SECOND));
6731 }
6732 
6733 static uint32_t
6734 read_fw_status_reg_ppc(struct mrsas_instance *instance)
6735 {
6736         return ((uint32_t)RD_OB_SCRATCH_PAD_0(instance));
6737 }
6738 
6739 static void
6740 issue_cmd_ppc(struct mrsas_cmd *cmd, struct mrsas_instance *instance)
6741 {
6742         struct scsi_pkt *pkt;
6743         atomic_add_16(&instance->fw_outstanding, 1);
6744 
6745         pkt = cmd->pkt;
6746         if (pkt) {
6747                 con_log(CL_DLEVEL1, (CE_NOTE, "%llx : issue_cmd_ppc:"
6748                     "ISSUED CMD TO FW : called : cmd:"
6749                     ": %p instance : %p pkt : %p pkt_time : %x\n",
6750                     gethrtime(), (void *)cmd, (void *)instance,
6751                     (void *)pkt, cmd->drv_pkt_time));
6752                 if (instance->adapterresetinprogress) {
6753                         cmd->drv_pkt_time = (uint16_t)debug_timeout_g;
6754                         con_log(CL_ANN1, (CE_NOTE, "Reset the scsi_pkt timer"));
6755                 } else {
6756                         push_pending_mfi_pkt(instance, cmd);
6757                 }
6758 
6759         } else {
6760                 con_log(CL_DLEVEL1, (CE_NOTE, "%llx : issue_cmd_ppc:"
6761                     "ISSUED CMD TO FW : called : cmd : %p, instance: %p"
6762                     "(NO PKT)\n", gethrtime(), (void *)cmd, (void *)instance));
6763         }
6764 
6765         mutex_enter(&instance->reg_write_mtx);
6766         /* Issue the command to the FW */
6767         WR_IB_QPORT((cmd->frame_phys_addr) |
6768             (((cmd->frame_count - 1) << 1) | 1), instance);
6769         mutex_exit(&instance->reg_write_mtx);
6770 
6771 }
6772 
6773 /*
6774  * issue_cmd_in_sync_mode
6775  */
6776 static int
6777 issue_cmd_in_sync_mode_ppc(struct mrsas_instance *instance,
6778 struct mrsas_cmd *cmd)
6779 {
6780         int     i;
6781         uint32_t        msecs = MFI_POLL_TIMEOUT_SECS * (10 * MILLISEC);
6782         struct mrsas_header *hdr = &cmd->frame->hdr;
6783 
6784         con_log(CL_ANN1, (CE_NOTE, "issue_cmd_in_sync_mode_ppc: called"));
6785 
6786         if (instance->adapterresetinprogress) {
6787                 cmd->drv_pkt_time = ddi_get16(
6788                     cmd->frame_dma_obj.acc_handle, &hdr->timeout);
6789                 if (cmd->drv_pkt_time < debug_timeout_g)
6790                         cmd->drv_pkt_time = (uint16_t)debug_timeout_g;
6791 
6792                 con_log(CL_ANN1, (CE_NOTE, "sync_mode_ppc: "
6793                     "issue and return in reset case\n"));
6794                 WR_IB_QPORT((cmd->frame_phys_addr) |
6795                     (((cmd->frame_count - 1) << 1) | 1), instance);
6796 
6797                 return (DDI_SUCCESS);
6798         } else {
6799                 con_log(CL_ANN1, (CE_NOTE, "sync_mode_ppc: pushing the pkt\n"));
6800                 push_pending_mfi_pkt(instance, cmd);
6801         }
6802 
6803         cmd->cmd_status      = ENODATA;
6804 
6805         mutex_enter(&instance->reg_write_mtx);
6806         /* Issue the command to the FW */
6807         WR_IB_QPORT((cmd->frame_phys_addr) |
6808             (((cmd->frame_count - 1) << 1) | 1), instance);
6809         mutex_exit(&instance->reg_write_mtx);
6810 
6811         mutex_enter(&instance->int_cmd_mtx);

6812         for (i = 0; i < msecs && (cmd->cmd_status == ENODATA); i++) {
6813                 cv_wait(&instance->int_cmd_cv, &instance->int_cmd_mtx);
6814         }

6815         mutex_exit(&instance->int_cmd_mtx);
6816 
6817         con_log(CL_ANN1, (CE_NOTE, "issue_cmd_in_sync_mode_ppc: done"));
6818 
6819         if (i < (msecs -1)) {
6820                 return (DDI_SUCCESS);
6821         } else {
6822                 return (DDI_FAILURE);
6823         }
6824 }
6825 
6826 /*
6827  * issue_cmd_in_poll_mode
6828  */
6829 static int
6830 issue_cmd_in_poll_mode_ppc(struct mrsas_instance *instance,
6831     struct mrsas_cmd *cmd)
6832 {
6833         int             i;
6834         uint16_t        flags;


6841         ddi_put8(cmd->frame_dma_obj.acc_handle, &frame_hdr->cmd_status,
6842             MFI_CMD_STATUS_POLL_MODE);
6843         flags = ddi_get16(cmd->frame_dma_obj.acc_handle, &frame_hdr->flags);
6844         flags   |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
6845 
6846         ddi_put16(cmd->frame_dma_obj.acc_handle, &frame_hdr->flags, flags);
6847 
6848         /* issue the frame using inbound queue port */
6849         WR_IB_QPORT((cmd->frame_phys_addr) |
6850             (((cmd->frame_count - 1) << 1) | 1), instance);
6851 
6852         /* wait for cmd_status to change from 0xFF */
6853         for (i = 0; i < msecs && (
6854             ddi_get8(cmd->frame_dma_obj.acc_handle, &frame_hdr->cmd_status)
6855             == MFI_CMD_STATUS_POLL_MODE); i++) {
6856                 drv_usecwait(MILLISEC); /* wait for 1000 usecs */
6857         }
6858 
6859         if (ddi_get8(cmd->frame_dma_obj.acc_handle, &frame_hdr->cmd_status)
6860             == MFI_CMD_STATUS_POLL_MODE) {
6861                 con_log(CL_ANN, (CE_NOTE, "issue_cmd_in_poll_mode: "
6862                     "cmd polling timed out"));
6863                 return (DDI_FAILURE);
6864         }
6865 
6866         return (DDI_SUCCESS);
6867 }
6868 
6869 static void
6870 enable_intr_ppc(struct mrsas_instance *instance)
6871 {
6872         uint32_t        mask;
6873 
6874         con_log(CL_ANN1, (CE_NOTE, "enable_intr_ppc: called"));
6875 
6876         /* WR_OB_DOORBELL_CLEAR(0xFFFFFFFF, instance); */
6877         WR_OB_DOORBELL_CLEAR(OB_DOORBELL_CLEAR_MASK, instance);
6878 
6879         /* WR_OB_INTR_MASK(~0x80000000, instance); */
6880         WR_OB_INTR_MASK(~(MFI_REPLY_2108_MESSAGE_INTR_MASK), instance);
6881 


6960         con_log(CL_ANN1, (CE_NOTE, "mrsas_kill_adapter: "
6961             "Writing to doorbell with MFI_STOP_ADP "));
6962         mutex_enter(&instance->ocr_flags_mtx);
6963         instance->deadadapter = 1;
6964         mutex_exit(&instance->ocr_flags_mtx);
6965         instance->func_ptr->disable_intr(instance);
6966         WR_IB_DOORBELL(MFI_STOP_ADP, instance);
6967         (void) mrsas_complete_pending_cmds(instance);
6968         return (DDI_SUCCESS);
6969 }
6970 
6971 
6972 static int
6973 mrsas_reset_ppc(struct mrsas_instance *instance)
6974 {
6975         uint32_t status;
6976         uint32_t retry = 0;
6977         uint32_t cur_abs_reg_val;
6978         uint32_t fw_state;
6979 
6980         con_log(CL_ANN, (CE_NOTE, "chkpnt:%s:%d", __func__, __LINE__));
6981 
6982         if (instance->deadadapter == 1) {
6983                 cmn_err(CE_WARN, "mrsas_reset_ppc: "
6984                     "no more resets as HBA has been marked dead ");
6985                 return (DDI_FAILURE);
6986         }
6987         mutex_enter(&instance->ocr_flags_mtx);
6988         instance->adapterresetinprogress = 1;
6989         mutex_exit(&instance->ocr_flags_mtx);
6990         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: adpterresetinprogress "
6991             "flag set, time %llx", gethrtime()));
6992 
6993         instance->func_ptr->disable_intr(instance);
6994 retry_reset:
6995         WR_IB_WRITE_SEQ(0, instance);
6996         WR_IB_WRITE_SEQ(4, instance);
6997         WR_IB_WRITE_SEQ(0xb, instance);
6998         WR_IB_WRITE_SEQ(2, instance);
6999         WR_IB_WRITE_SEQ(7, instance);
7000         WR_IB_WRITE_SEQ(0xd, instance);
7001         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: magic number written "
7002             "to write sequence register\n"));
7003         delay(100 * drv_usectohz(MILLISEC));
7004         status = RD_OB_DRWE(instance);
7005 
7006         while (!(status & DIAG_WRITE_ENABLE)) {
7007                 delay(100 * drv_usectohz(MILLISEC));
7008                 status = RD_OB_DRWE(instance);
7009                 if (retry++ == 100) {
7010                         cmn_err(CE_WARN, "mrsas_reset_ppc: DRWE bit "
7011                             "check retry count %d", retry);
7012                         return (DDI_FAILURE);
7013                 }
7014         }
7015         WR_IB_DRWE(status | DIAG_RESET_ADAPTER, instance);
7016         delay(100 * drv_usectohz(MILLISEC));
7017         status = RD_OB_DRWE(instance);
7018         while (status & DIAG_RESET_ADAPTER) {
7019                 delay(100 * drv_usectohz(MILLISEC));
7020                 status = RD_OB_DRWE(instance);
7021                 if (retry++ == 100) {
7022                         cmn_err(CE_WARN, "mrsas_reset_ppc: "
7023                             "RESET FAILED. KILL adapter called.");
7024 
7025                         (void) mrsas_kill_adapter(instance);
7026                         return (DDI_FAILURE);
7027                 }
7028         }
7029         con_log(CL_ANN, (CE_NOTE, "mrsas_reset_ppc: Adapter reset complete"));
7030         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
7031             "Calling mfi_state_transition_to_ready"));
7032 
7033         /* Mark HBA as bad, if FW is fault after 3 continuous resets */
7034         if (mfi_state_transition_to_ready(instance) ||
7035             debug_fw_faults_after_ocr_g == 1) {
7036                 cur_abs_reg_val =
7037                     instance->func_ptr->read_fw_status_reg(instance);
7038                 fw_state        = cur_abs_reg_val & MFI_STATE_MASK;
7039 
7040 #ifdef OCRDEBUG
7041                 con_log(CL_ANN1, (CE_NOTE,
7042                     "mrsas_reset_ppc :before fake: FW is not ready "
7043                     "FW state = 0x%x", fw_state));
7044                 if (debug_fw_faults_after_ocr_g == 1)
7045                         fw_state = MFI_STATE_FAULT;
7046 #endif
7047 
7048                 con_log(CL_ANN1, (CE_NOTE,  "mrsas_reset_ppc : FW is not ready "
7049                     "FW state = 0x%x", fw_state));
7050 
7051                 if (fw_state == MFI_STATE_FAULT) {
7052                         /* increment the count */
7053                         instance->fw_fault_count_after_ocr++;
7054                         if (instance->fw_fault_count_after_ocr
7055                             < MAX_FW_RESET_COUNT) {
7056                                 cmn_err(CE_WARN, "mrsas_reset_ppc: "
7057                                     "FW is in fault after OCR count %d "
7058                                     "Retry Reset",
7059                                     instance->fw_fault_count_after_ocr);
7060                                 goto retry_reset;
7061 
7062                         } else {
7063                                 cmn_err(CE_WARN, "mrsas_reset_ppc: "
7064                                     "Max Reset Count exceeded >%d"
7065                                     "Mark HBA as bad, KILL adapter",
7066                                     MAX_FW_RESET_COUNT);
7067 
7068                                 (void) mrsas_kill_adapter(instance);
7069                                 return (DDI_FAILURE);
7070                         }
7071                 }
7072         }
7073         /* reset the counter as FW is up after OCR */
7074         instance->fw_fault_count_after_ocr = 0;
7075 
7076 
7077         ddi_put32(instance->mfi_internal_dma_obj.acc_handle,
7078             instance->producer, 0);
7079 
7080         ddi_put32(instance->mfi_internal_dma_obj.acc_handle,
7081             instance->consumer, 0);
7082 
7083         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
7084             " after resetting produconsumer chck indexs:"
7085             "producer %x consumer %x", *instance->producer,
7086             *instance->consumer));
7087 
7088         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
7089             "Calling mrsas_issue_init_mfi"));
7090         (void) mrsas_issue_init_mfi(instance);
7091         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
7092             "mrsas_issue_init_mfi Done"));
7093 
7094         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
7095             "Calling mrsas_print_pending_cmd\n"));
7096         (void) mrsas_print_pending_cmds(instance);
7097         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
7098             "mrsas_print_pending_cmd done\n"));
7099 
7100         instance->func_ptr->enable_intr(instance);
7101         instance->fw_outstanding = 0;
7102 
7103         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
7104             "Calling mrsas_issue_pending_cmds"));
7105         (void) mrsas_issue_pending_cmds(instance);
7106         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
7107             "issue_pending_cmds done.\n"));
7108 
7109         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
7110             "Calling aen registration"));
7111 
7112 
7113         instance->aen_cmd->retry_count_for_ocr = 0;
7114         instance->aen_cmd->drv_pkt_time = 0;
7115 
7116         instance->func_ptr->issue_cmd(instance->aen_cmd, instance);
7117         con_log(CL_ANN1, (CE_NOTE, "Unsetting adpresetinprogress flag.\n"));
7118 
7119         mutex_enter(&instance->ocr_flags_mtx);
7120         instance->adapterresetinprogress = 0;
7121         mutex_exit(&instance->ocr_flags_mtx);
7122         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc: "
7123             "adpterresetinprogress flag unset"));
7124 
7125         con_log(CL_ANN1, (CE_NOTE, "mrsas_reset_ppc done\n"));
7126         return (DDI_SUCCESS);
7127 }
7128 
7129 /*
7130  * FMA functions.
7131  */
7132 int
7133 mrsas_common_check(struct mrsas_instance *instance, struct  mrsas_cmd *cmd)
7134 {
7135         int ret = DDI_SUCCESS;
7136 
7137         if (cmd != NULL &&
7138             mrsas_check_dma_handle(cmd->frame_dma_obj.dma_handle) !=
7139             DDI_SUCCESS) {
7140                 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED);
7141                 if (cmd->pkt != NULL) {
7142                         cmd->pkt->pkt_reason = CMD_TRAN_ERR;
7143                         cmd->pkt->pkt_statistics = 0;
7144                 }
7145                 ret = DDI_FAILURE;
7146         }
7147         if (mrsas_check_dma_handle(instance->mfi_internal_dma_obj.dma_handle)
7148             != DDI_SUCCESS) {
7149                 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED);
7150                 if (cmd != NULL && cmd->pkt != NULL) {
7151                         cmd->pkt->pkt_reason = CMD_TRAN_ERR;
7152                         cmd->pkt->pkt_statistics = 0;
7153                 }
7154                 ret = DDI_FAILURE;
7155         }
7156         if (mrsas_check_dma_handle(instance->mfi_evt_detail_obj.dma_handle) !=
7157             DDI_SUCCESS) {
7158                 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED);
7159                 if (cmd != NULL && cmd->pkt != NULL) {
7160                         cmd->pkt->pkt_reason = CMD_TRAN_ERR;
7161                         cmd->pkt->pkt_statistics = 0;
7162                 }
7163                 ret = DDI_FAILURE;
7164         }
7165         if (mrsas_check_acc_handle(instance->regmap_handle) != DDI_SUCCESS) {
7166                 ddi_fm_service_impact(instance->dip, DDI_SERVICE_UNAFFECTED);
7167 
7168                 ddi_fm_acc_err_clear(instance->regmap_handle, DDI_FME_VER0);
7169 
7170                 if (cmd != NULL && cmd->pkt != NULL) {
7171                         cmd->pkt->pkt_reason = CMD_TRAN_ERR;
7172                         cmd->pkt->pkt_statistics = 0;
7173                 }
7174                 ret = DDI_FAILURE;
7175         }
7176 
7177         return (ret);
7178 }
7179 
7180 /*ARGSUSED*/
7181 static int
7182 mrsas_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
7183 {
7184         /*
7185          * as the driver can always deal with an error in any dma or
7186          * access handle, we can just return the fme_status value.
7187          */
7188         pci_ereport_post(dip, err, NULL);
7189         return (err->fme_status);
7190 }


7294 {
7295         uint64_t ena;
7296         char buf[FM_MAX_CLASS];
7297 
7298         (void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
7299         ena = fm_ena_generate(0, FM_ENA_FMT1);
7300         if (DDI_FM_EREPORT_CAP(instance->fm_capabilities)) {
7301                 ddi_fm_ereport_post(instance->dip, buf, ena, DDI_NOSLEEP,
7302                     FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERSION, NULL);
7303         }
7304 }
7305 
7306 static int
7307 mrsas_add_intrs(struct mrsas_instance *instance, int intr_type)
7308 {
7309 
7310         dev_info_t *dip = instance->dip;
7311         int     avail, actual, count;
7312         int     i, flag, ret;
7313 
7314         con_log(CL_DLEVEL1, (CE_NOTE, "mrsas_add_intrs: intr_type = %x",
7315             intr_type));
7316 
7317         /* Get number of interrupts */
7318         ret = ddi_intr_get_nintrs(dip, intr_type, &count);
7319         if ((ret != DDI_SUCCESS) || (count == 0)) {
7320                 con_log(CL_ANN, (CE_WARN, "ddi_intr_get_nintrs() failed:"
7321                     "ret %d count %d", ret, count));
7322 
7323                 return (DDI_FAILURE);
7324         }
7325 
7326         con_log(CL_DLEVEL1, (CE_NOTE, "mrsas_add_intrs: count = %d ", count));
7327 
7328         /* Get number of available interrupts */
7329         ret = ddi_intr_get_navail(dip, intr_type, &avail);
7330         if ((ret != DDI_SUCCESS) || (avail == 0)) {
7331                 con_log(CL_ANN, (CE_WARN, "ddi_intr_get_navail() failed:"
7332                     "ret %d avail %d", ret, avail));
7333 
7334                 return (DDI_FAILURE);
7335         }
7336         con_log(CL_DLEVEL1, (CE_NOTE, "mrsas_add_intrs: avail = %d ", avail));
7337 
7338         /* Only one interrupt routine. So limit the count to 1 */
7339         if (count > 1) {
7340                 count = 1;
7341         }
7342 
7343         /*
7344          * Allocate an array of interrupt handlers. Currently we support
7345          * only one interrupt. The framework can be extended later.
7346          */
7347         instance->intr_htable_size = count * sizeof (ddi_intr_handle_t);
7348         instance->intr_htable = kmem_zalloc(instance->intr_htable_size,
7349             KM_SLEEP);
7350         ASSERT(instance->intr_htable);
7351 
7352         flag = ((intr_type == DDI_INTR_TYPE_MSI) ||
7353             (intr_type == DDI_INTR_TYPE_MSIX)) ?
7354             DDI_INTR_ALLOC_STRICT : DDI_INTR_ALLOC_NORMAL;
7355 
7356         /* Allocate interrupt */
7357         ret = ddi_intr_alloc(dip, instance->intr_htable, intr_type, 0,
7358             count, &actual, flag);
7359 
7360         if ((ret != DDI_SUCCESS) || (actual == 0)) {
7361                 con_log(CL_ANN, (CE_WARN, "mrsas_add_intrs: "
7362                     "avail = %d", avail));
7363                 goto mrsas_free_htable;

7364         }
7365 
7366         if (actual < count) {
7367                 con_log(CL_ANN, (CE_WARN, "mrsas_add_intrs: "
7368                     "Requested = %d  Received = %d", count, actual));
7369         }
7370         instance->intr_cnt = actual;
7371 
7372         /*
7373          * Get the priority of the interrupt allocated.
7374          */
7375         if ((ret = ddi_intr_get_pri(instance->intr_htable[0],
7376             &instance->intr_pri)) != DDI_SUCCESS) {
7377                 con_log(CL_ANN, (CE_WARN, "mrsas_add_intrs: "
7378                     "get priority call failed"));
7379                 goto mrsas_free_handles;


7380         }



7381 
7382         /*
7383          * Test for high level mutex. we don't support them.
7384          */
7385         if (instance->intr_pri >= ddi_intr_get_hilevel_pri()) {
7386                 con_log(CL_ANN, (CE_WARN, "mrsas_add_intrs: "
7387                     "High level interrupts not supported."));
7388                 goto mrsas_free_handles;


7389         }



7390 
7391         con_log(CL_DLEVEL1, (CE_NOTE, "mrsas_add_intrs: intr_pri = 0x%x ",
7392             instance->intr_pri));
7393 
7394         /* Call ddi_intr_add_handler() */
7395         for (i = 0; i < actual; i++) {
7396                 ret = ddi_intr_add_handler(instance->intr_htable[i],
7397                     (ddi_intr_handler_t *)mrsas_isr, (caddr_t)instance,
7398                     (caddr_t)(uintptr_t)i);
7399 
7400                 if (ret != DDI_SUCCESS) {
7401                         con_log(CL_ANN, (CE_WARN, "mrsas_add_intrs:"
7402                             "failed %d", ret));
7403                         goto mrsas_free_handles;


7404                 }



7405 
7406         }
7407 
7408         con_log(CL_DLEVEL1, (CE_NOTE, " ddi_intr_add_handler done"));
7409 
7410         if ((ret = ddi_intr_get_cap(instance->intr_htable[0],
7411             &instance->intr_cap)) != DDI_SUCCESS) {
7412                 con_log(CL_ANN, (CE_WARN, "ddi_intr_get_cap() failed %d",
7413                     ret));
7414                 goto mrsas_free_handlers;





7415         }



7416 
7417         if (instance->intr_cap &  DDI_INTR_FLAG_BLOCK) {
7418                 con_log(CL_ANN, (CE_WARN, "Calling ddi_intr_block _enable"));
7419 
7420                 (void) ddi_intr_block_enable(instance->intr_htable,
7421                     instance->intr_cnt);
7422         } else {
7423                 con_log(CL_ANN, (CE_NOTE, " calling ddi_intr_enable"));
7424 
7425                 for (i = 0; i < instance->intr_cnt; i++) {
7426                         (void) ddi_intr_enable(instance->intr_htable[i]);
7427                         con_log(CL_ANN, (CE_NOTE, "ddi intr enable returns "
7428                             "%d", i));
7429                 }
7430         }
7431 
7432         return (DDI_SUCCESS);
7433 
7434 mrsas_free_handlers:
7435         for (i = 0; i < actual; i++)
7436                 (void) ddi_intr_remove_handler(instance->intr_htable[i]);
7437 
7438 mrsas_free_handles:
7439         for (i = 0; i < actual; i++)
7440                 (void) ddi_intr_free(instance->intr_htable[i]);
7441 
7442 mrsas_free_htable:
7443         if (instance->intr_htable != NULL)
7444                 kmem_free(instance->intr_htable, instance->intr_htable_size);
7445 
7446         instance->intr_htable = NULL;
7447         instance->intr_htable_size = 0;
7448 
7449         return (DDI_FAILURE);
7450 
7451 }
7452 
7453 
7454 static void
7455 mrsas_rem_intrs(struct mrsas_instance *instance)
7456 {
7457         int i;
7458 
7459         con_log(CL_ANN, (CE_NOTE, "mrsas_rem_intrs called"));
7460 
7461         /* Disable all interrupts first */
7462         if (instance->intr_cap & DDI_INTR_FLAG_BLOCK) {
7463                 (void) ddi_intr_block_disable(instance->intr_htable,
7464                     instance->intr_cnt);
7465         } else {
7466                 for (i = 0; i < instance->intr_cnt; i++) {
7467                         (void) ddi_intr_disable(instance->intr_htable[i]);
7468                 }
7469         }
7470 
7471         /* Remove all the handlers */
7472 
7473         for (i = 0; i < instance->intr_cnt; i++) {
7474                 (void) ddi_intr_remove_handler(instance->intr_htable[i]);
7475                 (void) ddi_intr_free(instance->intr_htable[i]);
7476         }
7477 
7478         if (instance->intr_htable != NULL)
7479                 kmem_free(instance->intr_htable, instance->intr_htable_size);
7480 
7481         instance->intr_htable = NULL;
7482         instance->intr_htable_size = 0;
7483 
7484 }
7485 
7486 static int
7487 mrsas_tran_bus_config(dev_info_t *parent, uint_t flags,
7488     ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
7489 {
7490         struct mrsas_instance *instance;
7491         int config;
7492         int rval  = NDI_SUCCESS;
7493 
7494         char *ptr = NULL;
7495         int tgt, lun;
7496 
7497         con_log(CL_ANN1, (CE_NOTE, "Bus config called for op = %x", op));
7498 
7499         if ((instance = ddi_get_soft_state(mrsas_state,
7500             ddi_get_instance(parent))) == NULL) {
7501                 return (NDI_FAILURE);
7502         }
7503 
7504         /* Hold nexus during bus_config */
7505         ndi_devi_enter(parent, &config);
7506         switch (op) {
7507         case BUS_CONFIG_ONE: {
7508 
7509                 /* parse wwid/target name out of name given */
7510                 if ((ptr = strchr((char *)arg, '@')) == NULL) {
7511                         rval = NDI_FAILURE;
7512                         break;
7513                 }
7514                 ptr++;
7515 
7516                 if (mrsas_parse_devname(arg, &tgt, &lun) != 0) {
7517                         rval = NDI_FAILURE;
7518                         break;
7519                 }
7520 
7521                 if (lun == 0) {
7522                         rval = mrsas_config_ld(instance, tgt, lun, childp);
7523 #ifdef PDSUPPORT
7524                 } else if (instance->tbolt == 1 && lun != 0) {
7525                         rval = mrsas_tbolt_config_pd(instance,
7526                             tgt, lun, childp);
7527 #endif
7528                 } else {
7529                         rval = NDI_FAILURE;
7530                 }
7531 
7532                 break;
7533         }
7534         case BUS_CONFIG_DRIVER:
7535         case BUS_CONFIG_ALL: {
7536 
7537                 rval = mrsas_config_all_devices(instance);
7538 
7539                 rval = NDI_SUCCESS;
7540                 break;
7541         }
7542         }
7543 
7544         if (rval == NDI_SUCCESS) {
7545                 rval = ndi_busop_bus_config(parent, flags, op, arg, childp, 0);
7546 
7547         }
7548         ndi_devi_exit(parent, config);
7549 
7550         con_log(CL_ANN1, (CE_NOTE, "mrsas_tran_bus_config: rval = %x",
7551             rval));
7552         return (rval);
7553 }
7554 
7555 static int
7556 mrsas_config_all_devices(struct mrsas_instance *instance)
7557 {
7558         int rval, tgt;
7559 
7560         for (tgt = 0; tgt < MRDRV_MAX_LD; tgt++) {
7561                 (void) mrsas_config_ld(instance, tgt, 0, NULL);
7562 
7563         }
7564 
7565 #ifdef PDSUPPORT
7566         /* Config PD devices connected to the card */
7567         if (instance->tbolt) {
7568                 for (tgt = 0; tgt < instance->mr_tbolt_pd_max; tgt++) {
7569                         (void) mrsas_tbolt_config_pd(instance, tgt, 1, NULL);
7570                 }
7571         }
7572 #endif
7573 
7574         rval = NDI_SUCCESS;
7575         return (rval);
7576 }
7577 
7578 static int
7579 mrsas_parse_devname(char *devnm, int *tgt, int *lun)
7580 {
7581         char devbuf[SCSI_MAXNAMELEN];
7582         char *addr;
7583         char *p,  *tp, *lp;
7584         long num;
7585 
7586         /* Parse dev name and address */
7587         (void) strcpy(devbuf, devnm);
7588         addr = "";
7589         for (p = devbuf; *p != '\0'; p++) {
7590                 if (*p == '@') {
7591                         addr = p + 1;
7592                         *p = '\0';
7593                 } else if (*p == ':') {


7610                 }
7611                         *tgt = (int)num;
7612         }
7613         if (lun && lp) {
7614                 if (ddi_strtol(lp, NULL, 0x10, &num)) {
7615                         return (DDI_FAILURE);
7616                 }
7617                         *lun = (int)num;
7618         }
7619         return (DDI_SUCCESS);  /* Success case */
7620 }
7621 
7622 static int
7623 mrsas_config_ld(struct mrsas_instance *instance, uint16_t tgt,
7624     uint8_t lun, dev_info_t **ldip)
7625 {
7626         struct scsi_device *sd;
7627         dev_info_t *child;
7628         int rval;
7629 
7630         con_log(CL_DLEVEL1, (CE_NOTE, "mrsas_config_ld: t = %d l = %d",
7631             tgt, lun));
7632 
7633         if ((child = mrsas_find_child(instance, tgt, lun)) != NULL) {
7634                 if (ldip) {
7635                         *ldip = child;
7636                 }
7637                 if (instance->mr_ld_list[tgt].flag != MRDRV_TGT_VALID) {
7638                         rval = mrsas_service_evt(instance, tgt, 0,
7639                             MRSAS_EVT_UNCONFIG_TGT, NULL);
7640                         con_log(CL_ANN1, (CE_WARN,
7641                             "mr_sas: DELETING STALE ENTRY rval = %d "
7642                             "tgt id = %d ", rval, tgt));
7643                         return (NDI_FAILURE);
7644                 }
7645                 return (NDI_SUCCESS);
7646         }
7647 
7648         sd = kmem_zalloc(sizeof (struct scsi_device), KM_SLEEP);
7649         sd->sd_address.a_hba_tran = instance->tran;
7650         sd->sd_address.a_target = (uint16_t)tgt;
7651         sd->sd_address.a_lun = (uint8_t)lun;
7652 
7653         if (scsi_hba_probe(sd, NULL) == SCSIPROBE_EXISTS)
7654                 rval = mrsas_config_scsi_device(instance, sd, ldip);
7655         else
7656                 rval = NDI_FAILURE;
7657 
7658         /* sd_unprobe is blank now. Free buffer manually */
7659         if (sd->sd_inq) {
7660                 kmem_free(sd->sd_inq, SUN_INQSIZE);
7661                 sd->sd_inq = (struct scsi_inquiry *)NULL;
7662         }
7663 
7664         kmem_free(sd, sizeof (struct scsi_device));
7665         con_log(CL_DLEVEL1, (CE_NOTE, "mrsas_config_ld: return rval = %d",
7666             rval));
7667         return (rval);
7668 }
7669 
7670 int
7671 mrsas_config_scsi_device(struct mrsas_instance *instance,
7672     struct scsi_device *sd, dev_info_t **dipp)
7673 {
7674         char *nodename = NULL;
7675         char **compatible = NULL;
7676         int ncompatible = 0;
7677         char *childname;
7678         dev_info_t *ldip = NULL;
7679         int tgt = sd->sd_address.a_target;
7680         int lun = sd->sd_address.a_lun;
7681         int dtype = sd->sd_inq->inq_dtype & DTYPE_MASK;
7682         int rval;
7683 
7684         con_log(CL_DLEVEL1, (CE_NOTE, "mr_sas: scsi_device t%dL%d", tgt, lun));
7685         scsi_hba_nodename_compatible_get(sd->sd_inq, NULL, dtype,
7686             NULL, &nodename, &compatible, &ncompatible);
7687 
7688         if (nodename == NULL) {
7689                 con_log(CL_ANN1, (CE_WARN, "mr_sas: Found no compatible driver "
7690                     "for t%dL%d", tgt, lun));
7691                 rval = NDI_FAILURE;
7692                 goto finish;
7693         }
7694 
7695         childname = (dtype == DTYPE_DIRECT) ? "sd" : nodename;
7696         con_log(CL_DLEVEL1, (CE_NOTE,
7697             "mr_sas: Childname = %2s nodename = %s", childname, nodename));
7698 
7699         /* Create a dev node */
7700         rval = ndi_devi_alloc(instance->dip, childname, DEVI_SID_NODEID, &ldip);
7701         con_log(CL_DLEVEL1, (CE_NOTE,
7702             "mr_sas_config_scsi_device: ndi_devi_alloc rval = %x", rval));
7703         if (rval == NDI_SUCCESS) {
7704                 if (ndi_prop_update_int(DDI_DEV_T_NONE, ldip, "target", tgt) !=
7705                     DDI_PROP_SUCCESS) {
7706                         con_log(CL_ANN1, (CE_WARN, "mr_sas: unable to create "
7707                             "property for t%dl%d target", tgt, lun));
7708                         rval = NDI_FAILURE;
7709                         goto finish;
7710                 }
7711                 if (ndi_prop_update_int(DDI_DEV_T_NONE, ldip, "lun", lun) !=
7712                     DDI_PROP_SUCCESS) {
7713                         con_log(CL_ANN1, (CE_WARN, "mr_sas: unable to create "
7714                             "property for t%dl%d lun", tgt, lun));
7715                         rval = NDI_FAILURE;
7716                         goto finish;
7717                 }
7718 
7719                 if (ndi_prop_update_string_array(DDI_DEV_T_NONE, ldip,
7720                     "compatible", compatible, ncompatible) !=
7721                     DDI_PROP_SUCCESS) {
7722                         con_log(CL_ANN1, (CE_WARN, "mr_sas: unable to create "
7723                             "property for t%dl%d compatible", tgt, lun));
7724                         rval = NDI_FAILURE;
7725                         goto finish;
7726                 }
7727 
7728                 rval = ndi_devi_online(ldip, NDI_ONLINE_ATTACH);
7729                 if (rval != NDI_SUCCESS) {
7730                         con_log(CL_ANN1, (CE_WARN, "mr_sas: unable to online "
7731                             "t%dl%d", tgt, lun));
7732                         ndi_prop_remove_all(ldip);
7733                         (void) ndi_devi_free(ldip);
7734                 } else {
7735                         con_log(CL_ANN1, (CE_CONT, "mr_sas: online Done :"
7736                             "0 t%dl%d", tgt, lun));
7737                 }
7738 
7739         }
7740 finish:
7741         if (dipp) {
7742                 *dipp = ldip;
7743         }
7744 
7745         con_log(CL_DLEVEL1, (CE_NOTE,
7746             "mr_sas: config_scsi_device rval = %d t%dL%d",
7747             rval, tgt, lun));
7748         scsi_hba_nodename_compatible_free(nodename, compatible);
7749         return (rval);
7750 }
7751 
7752 /*ARGSUSED*/
7753 int
7754 mrsas_service_evt(struct mrsas_instance *instance, int tgt, int lun, int event,
7755     uint64_t wwn)
7756 {
7757         struct mrsas_eventinfo *mrevt = NULL;
7758 
7759         con_log(CL_ANN1, (CE_NOTE,
7760             "mrsas_service_evt called for t%dl%d event = %d",
7761             tgt, lun, event));
7762 
7763         if ((instance->taskq == NULL) || (mrevt =
7764             kmem_zalloc(sizeof (struct mrsas_eventinfo), KM_NOSLEEP)) == NULL) {
7765                 return (ENOMEM);
7766         }
7767 
7768         mrevt->instance = instance;
7769         mrevt->tgt = tgt;
7770         mrevt->lun = lun;
7771         mrevt->event = event;
7772         mrevt->wwn = wwn;
7773 
7774         if ((ddi_taskq_dispatch(instance->taskq,
7775             (void (*)(void *))mrsas_issue_evt_taskq, mrevt, DDI_NOSLEEP)) !=
7776             DDI_SUCCESS) {
7777                 con_log(CL_ANN1, (CE_NOTE,
7778                     "mr_sas: Event task failed for t%dl%d event = %d",
7779                     tgt, lun, event));
7780                 kmem_free(mrevt, sizeof (struct mrsas_eventinfo));
7781                 return (DDI_FAILURE);
7782         }
7783         DTRACE_PROBE3(service_evt, int, tgt, int, lun, int, event);
7784         return (DDI_SUCCESS);
7785 }
7786 
7787 static void
7788 mrsas_issue_evt_taskq(struct mrsas_eventinfo *mrevt)
7789 {
7790         struct mrsas_instance *instance = mrevt->instance;
7791         dev_info_t *dip, *pdip;
7792         int circ1 = 0;
7793         char *devname;
7794 
7795         con_log(CL_ANN1, (CE_NOTE, "mrsas_issue_evt_taskq: called for"
7796             " tgt %d lun %d event %d",
7797             mrevt->tgt, mrevt->lun, mrevt->event));
7798 
7799         if (mrevt->tgt < MRDRV_MAX_LD && mrevt->lun == 0) {
7800                 mutex_enter(&instance->config_dev_mtx);
7801                 dip = instance->mr_ld_list[mrevt->tgt].dip;
7802                 mutex_exit(&instance->config_dev_mtx);
7803 #ifdef PDSUPPORT
7804         } else {
7805                 mutex_enter(&instance->config_dev_mtx);
7806                 dip = instance->mr_tbolt_pd_list[mrevt->tgt].dip;
7807                 mutex_exit(&instance->config_dev_mtx);
7808 #endif
7809         }
7810 
7811 
7812         ndi_devi_enter(instance->dip, &circ1);
7813         switch (mrevt->event) {
7814         case MRSAS_EVT_CONFIG_TGT:
7815                 if (dip == NULL) {
7816 
7817                         if (mrevt->lun == 0) {
7818                                 (void) mrsas_config_ld(instance, mrevt->tgt,
7819                                     0, NULL);
7820 #ifdef PDSUPPORT
7821                         } else if (instance->tbolt) {
7822                                 (void) mrsas_tbolt_config_pd(instance,
7823                                     mrevt->tgt,
7824                                     1, NULL);
7825 #endif
7826                         }
7827                         con_log(CL_ANN1, (CE_NOTE,
7828                             "mr_sas: EVT_CONFIG_TGT called:"
7829                             " for tgt %d lun %d event %d",
7830                             mrevt->tgt, mrevt->lun, mrevt->event));
7831 
7832                 } else {
7833                         con_log(CL_ANN1, (CE_NOTE,
7834                             "mr_sas: EVT_CONFIG_TGT dip != NULL:"
7835                             " for tgt %d lun %d event %d",
7836                             mrevt->tgt, mrevt->lun, mrevt->event));
7837                 }
7838                 break;
7839         case MRSAS_EVT_UNCONFIG_TGT:
7840                 if (dip) {
7841                         if (i_ddi_devi_attached(dip)) {
7842 
7843                                 pdip = ddi_get_parent(dip);
7844 
7845                                 devname = kmem_zalloc(MAXNAMELEN + 1, KM_SLEEP);


7849                                     DV_CLEAN_FORCE);
7850                                 kmem_free(devname, MAXNAMELEN + 1);
7851                         }
7852                         (void) ndi_devi_offline(dip, NDI_DEVI_REMOVE);
7853                         con_log(CL_ANN1, (CE_NOTE,
7854                             "mr_sas: EVT_UNCONFIG_TGT called:"
7855                             " for tgt %d lun %d event %d",
7856                             mrevt->tgt, mrevt->lun, mrevt->event));
7857                 } else {
7858                         con_log(CL_ANN1, (CE_NOTE,
7859                             "mr_sas: EVT_UNCONFIG_TGT dip == NULL:"
7860                             " for tgt %d lun %d event %d",
7861                             mrevt->tgt, mrevt->lun, mrevt->event));
7862                 }
7863                 break;
7864         }
7865         kmem_free(mrevt, sizeof (struct mrsas_eventinfo));
7866         ndi_devi_exit(instance->dip, circ1);
7867 }
7868 
7869 
7870 int
7871 mrsas_mode_sense_build(struct scsi_pkt *pkt)
7872 {
7873         union scsi_cdb          *cdbp;
7874         uint16_t                page_code;
7875         struct scsa_cmd         *acmd;
7876         struct buf              *bp;
7877         struct mode_header      *modehdrp;
7878 
7879         cdbp = (void *)pkt->pkt_cdbp;
7880         page_code = cdbp->cdb_un.sg.scsi[0];
7881         acmd = PKT2CMD(pkt);
7882         bp = acmd->cmd_buf;
7883         if ((!bp) && bp->b_un.b_addr && bp->b_bcount && acmd->cmd_dmacount) {
7884                 con_log(CL_ANN1, (CE_WARN, "Failing MODESENSE Command"));
7885                 /* ADD pkt statistics as Command failed. */
7886                 return (NULL);
7887         }
7888 
7889         bp_mapin(bp);
7890         bzero(bp->b_un.b_addr, bp->b_bcount);