Print this page
    
2777 mpt_sas needs to try MUR reset at attach() time.
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/uts/common/io/scsi/adapters/mpt_sas/mptsas.c
          +++ new/usr/src/uts/common/io/scsi/adapters/mpt_sas/mptsas.c
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License (the "License").
   6    6   * You may not use this file except in compliance with the License.
   7    7   *
   8    8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9    9   * or http://www.opensolaris.org/os/licensing.
  10   10   * See the License for the specific language governing permissions
  11   11   * and limitations under the License.
  12   12   *
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  
    | 
      ↓ open down ↓ | 
    13 lines elided | 
    
      ↑ open up ↑ | 
  
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
       24 + * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
  24   25   */
  25   26  
  26   27  /*
  27   28   * Copyright (c) 2000 to 2010, LSI Corporation.
  28   29   * All rights reserved.
  29   30   *
  30   31   * Redistribution and use in source and binary forms of all code within
  31   32   * this file that is exclusively owned by LSI, with or without
  32   33   * modification, is permitted provided that, in addition to the CDDL 1.0
  33   34   * License requirements, the following conditions are met:
  34   35   *
  35   36   *    Neither the name of the author nor the names of its contributors may be
  36   37   *    used to endorse or promote products derived from this software without
  37   38   *    specific prior written permission.
  38   39   *
  39   40   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  40   41   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  41   42   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  42   43   * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  43   44   * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  44   45   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  45   46   * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  46   47   * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  47   48   * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  48   49   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  49   50   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  50   51   * DAMAGE.
  51   52   */
  52   53  
  53   54  /*
  54   55   * mptsas - This is a driver based on LSI Logic's MPT2.0 interface.
  55   56   *
  56   57   */
  57   58  
  58   59  #if defined(lint) || defined(DEBUG)
  59   60  #define MPTSAS_DEBUG
  60   61  #endif
  61   62  
  62   63  /*
  63   64   * standard header files.
  64   65   */
  65   66  #include <sys/note.h>
  66   67  #include <sys/scsi/scsi.h>
  67   68  #include <sys/pci.h>
  68   69  #include <sys/file.h>
  69   70  #include <sys/cpuvar.h>
  70   71  #include <sys/policy.h>
  71   72  #include <sys/sysevent.h>
  72   73  #include <sys/sysevent/eventdefs.h>
  73   74  #include <sys/sysevent/dr.h>
  74   75  #include <sys/sata/sata_defs.h>
  75   76  #include <sys/scsi/generic/sas.h>
  76   77  #include <sys/scsi/impl/scsi_sas.h>
  77   78  
  78   79  #pragma pack(1)
  79   80  #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_type.h>
  80   81  #include <sys/scsi/adapters/mpt_sas/mpi/mpi2.h>
  81   82  #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_cnfg.h>
  82   83  #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_init.h>
  83   84  #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_ioc.h>
  84   85  #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_sas.h>
  85   86  #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_tool.h>
  86   87  #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_raid.h>
  87   88  #pragma pack()
  88   89  
  89   90  /*
  90   91   * private header files.
  91   92   *
  92   93   */
  93   94  #include <sys/scsi/impl/scsi_reset_notify.h>
  94   95  #include <sys/scsi/adapters/mpt_sas/mptsas_var.h>
  95   96  #include <sys/scsi/adapters/mpt_sas/mptsas_ioctl.h>
  96   97  #include <sys/scsi/adapters/mpt_sas/mptsas_smhba.h>
  97   98  
  98   99  #include <sys/raidioctl.h>
  99  100  
 100  101  #include <sys/fs/dv_node.h>     /* devfs_clean */
 101  102  
 102  103  /*
 103  104   * FMA header files
 104  105   */
 105  106  #include <sys/ddifm.h>
 106  107  #include <sys/fm/protocol.h>
 107  108  #include <sys/fm/util.h>
 108  109  #include <sys/fm/io/ddi.h>
 109  110  
 110  111  /*
 111  112   * For anyone who would modify the code in mptsas_driver, it must be awared
 112  113   * that from snv_145 where CR6910752(mpt_sas driver performance can be
 113  114   * improved) is integrated, the per_instance mutex m_mutex is not hold
 114  115   * in the key IO code path, including mptsas_scsi_start(), mptsas_intr()
 115  116   * and all of the recursive functions called in them, so don't
 116  117   * make it for granted that all operations are sync/exclude correctly. Before
 117  118   * doing any modification in key code path, and even other code path such as
 118  119   * DR, watchsubr, ioctl, passthrough etc, make sure the elements modified have
 119  120   * no releationship to elements shown in the fastpath
 120  121   * (function mptsas_handle_io_fastpath()) in ISR and its recursive functions.
 121  122   * otherwise, you have to use the new introduced mutex to protect them.
 122  123   * As to how to do correctly, refer to the comments in mptsas_intr().
 123  124   */
 124  125  
 125  126  /*
 126  127   * autoconfiguration data and routines.
 127  128   */
 128  129  static int mptsas_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
 129  130  static int mptsas_detach(dev_info_t *devi, ddi_detach_cmd_t cmd);
 130  131  static int mptsas_power(dev_info_t *dip, int component, int level);
 131  132  
 132  133  /*
 133  134   * cb_ops function
 134  135   */
 135  136  static int mptsas_ioctl(dev_t dev, int cmd, intptr_t data, int mode,
 136  137          cred_t *credp, int *rval);
 137  138  #ifdef __sparc
 138  139  static int mptsas_reset(dev_info_t *devi, ddi_reset_cmd_t cmd);
 139  140  #else  /* __sparc */
 140  141  static int mptsas_quiesce(dev_info_t *devi);
 141  142  #endif  /* __sparc */
 142  143  
 143  144  /*
 144  145   * Resource initilaization for hardware
 145  146   */
 146  147  static void mptsas_setup_cmd_reg(mptsas_t *mpt);
 147  148  static void mptsas_disable_bus_master(mptsas_t *mpt);
 148  149  static void mptsas_hba_fini(mptsas_t *mpt);
 149  150  static void mptsas_cfg_fini(mptsas_t *mptsas_blkp);
 150  151  static int mptsas_hba_setup(mptsas_t *mpt);
 151  152  static void mptsas_hba_teardown(mptsas_t *mpt);
 152  153  static int mptsas_config_space_init(mptsas_t *mpt);
 153  154  static void mptsas_config_space_fini(mptsas_t *mpt);
 154  155  static void mptsas_iport_register(mptsas_t *mpt);
 155  156  static int mptsas_smp_setup(mptsas_t *mpt);
 156  157  static void mptsas_smp_teardown(mptsas_t *mpt);
 157  158  static int mptsas_cache_create(mptsas_t *mpt);
 158  159  static void mptsas_cache_destroy(mptsas_t *mpt);
 159  160  static int mptsas_alloc_request_frames(mptsas_t *mpt);
 160  161  static int mptsas_alloc_reply_frames(mptsas_t *mpt);
 161  162  static int mptsas_alloc_free_queue(mptsas_t *mpt);
 162  163  static int mptsas_alloc_post_queue(mptsas_t *mpt);
 163  164  static void mptsas_alloc_reply_args(mptsas_t *mpt);
 164  165  static int mptsas_alloc_extra_sgl_frame(mptsas_t *mpt, mptsas_cmd_t *cmd);
 165  166  static void mptsas_free_extra_sgl_frame(mptsas_t *mpt, mptsas_cmd_t *cmd);
 166  167  static int mptsas_init_chip(mptsas_t *mpt, int first_time);
 167  168  
 168  169  /*
 169  170   * SCSA function prototypes
 170  171   */
 171  172  static int mptsas_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt);
 172  173  static int mptsas_scsi_reset(struct scsi_address *ap, int level);
 173  174  static int mptsas_scsi_abort(struct scsi_address *ap, struct scsi_pkt *pkt);
 174  175  static int mptsas_scsi_getcap(struct scsi_address *ap, char *cap, int tgtonly);
 175  176  static int mptsas_scsi_setcap(struct scsi_address *ap, char *cap, int value,
 176  177      int tgtonly);
 177  178  static void mptsas_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt);
 178  179  static struct scsi_pkt *mptsas_scsi_init_pkt(struct scsi_address *ap,
 179  180      struct scsi_pkt *pkt, struct buf *bp, int cmdlen, int statuslen,
 180  181          int tgtlen, int flags, int (*callback)(), caddr_t arg);
 181  182  static void mptsas_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt);
 182  183  static void mptsas_scsi_destroy_pkt(struct scsi_address *ap,
 183  184      struct scsi_pkt *pkt);
 184  185  static int mptsas_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
 185  186      scsi_hba_tran_t *hba_tran, struct scsi_device *sd);
 186  187  static void mptsas_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
 187  188      scsi_hba_tran_t *hba_tran, struct scsi_device *sd);
 188  189  static int mptsas_scsi_reset_notify(struct scsi_address *ap, int flag,
 189  190      void (*callback)(caddr_t), caddr_t arg);
 190  191  static int mptsas_get_name(struct scsi_device *sd, char *name, int len);
 191  192  static int mptsas_get_bus_addr(struct scsi_device *sd, char *name, int len);
 192  193  static int mptsas_scsi_quiesce(dev_info_t *dip);
 193  194  static int mptsas_scsi_unquiesce(dev_info_t *dip);
 194  195  static int mptsas_bus_config(dev_info_t *pdip, uint_t flags,
 195  196      ddi_bus_config_op_t op, void *arg, dev_info_t **childp);
 196  197  
 197  198  /*
 198  199   * SMP functions
 199  200   */
 200  201  static int mptsas_smp_start(struct smp_pkt *smp_pkt);
 201  202  
 202  203  /*
 203  204   * internal function prototypes.
 204  205   */
 205  206  static void mptsas_list_add(mptsas_t *mpt);
 206  207  static void mptsas_list_del(mptsas_t *mpt);
 207  208  
 208  209  static int mptsas_quiesce_bus(mptsas_t *mpt);
 209  210  static int mptsas_unquiesce_bus(mptsas_t *mpt);
 210  211  
 211  212  static int mptsas_alloc_handshake_msg(mptsas_t *mpt, size_t alloc_size);
 212  213  static void mptsas_free_handshake_msg(mptsas_t *mpt);
 213  214  
 214  215  static void mptsas_ncmds_checkdrain(void *arg);
 215  216  
 216  217  static int mptsas_prepare_pkt(mptsas_cmd_t *cmd);
 217  218  static int mptsas_accept_pkt(mptsas_t *mpt, mptsas_cmd_t *sp);
 218  219  
 219  220  static int mptsas_do_detach(dev_info_t *dev);
 220  221  static int mptsas_do_scsi_reset(mptsas_t *mpt, uint16_t devhdl);
 221  222  static int mptsas_do_scsi_abort(mptsas_t *mpt, int target, int lun,
 222  223      struct scsi_pkt *pkt);
 223  224  static int mptsas_scsi_capchk(char *cap, int tgtonly, int *cidxp);
 224  225  
 225  226  static void mptsas_handle_qfull(mptsas_t *mpt, mptsas_cmd_t *cmd);
 226  227  static void mptsas_handle_event(void *args);
 227  228  static int mptsas_handle_event_sync(void *args);
 228  229  static void mptsas_handle_dr(void *args);
 229  230  static void mptsas_handle_topo_change(mptsas_topo_change_list_t *topo_node,
 230  231      dev_info_t *pdip);
 231  232  
 232  233  static void mptsas_restart_cmd(void *);
 233  234  
 234  235  static void mptsas_flush_hba(mptsas_t *mpt);
 235  236  static void mptsas_flush_target(mptsas_t *mpt, ushort_t target, int lun,
 236  237          uint8_t tasktype);
 237  238  static void mptsas_set_pkt_reason(mptsas_t *mpt, mptsas_cmd_t *cmd,
 238  239      uchar_t reason, uint_t stat);
 239  240  
 240  241  static uint_t mptsas_intr(caddr_t arg1, caddr_t arg2);
 241  242  static void mptsas_process_intr(mptsas_t *mpt,
 242  243      pMpi2ReplyDescriptorsUnion_t reply_desc_union);
 243  244  static int mptsas_handle_io_fastpath(mptsas_t *mpt, uint16_t SMID);
 244  245  static void mptsas_handle_scsi_io_success(mptsas_t *mpt,
 245  246      pMpi2ReplyDescriptorsUnion_t reply_desc);
 246  247  static void mptsas_handle_address_reply(mptsas_t *mpt,
 247  248      pMpi2ReplyDescriptorsUnion_t reply_desc);
 248  249  static int mptsas_wait_intr(mptsas_t *mpt, int polltime);
 249  250  static void mptsas_sge_setup(mptsas_t *mpt, mptsas_cmd_t *cmd,
 250  251      uint32_t *control, pMpi2SCSIIORequest_t frame, ddi_acc_handle_t acc_hdl);
 251  252  
 252  253  static void mptsas_watch(void *arg);
 253  254  static void mptsas_watchsubr(mptsas_t *mpt);
 254  255  static void mptsas_cmd_timeout(mptsas_t *mpt, uint16_t devhdl);
 255  256  
 256  257  static void mptsas_start_passthru(mptsas_t *mpt, mptsas_cmd_t *cmd);
 257  258  static int mptsas_do_passthru(mptsas_t *mpt, uint8_t *request, uint8_t *reply,
 258  259      uint8_t *data, uint32_t request_size, uint32_t reply_size,
 259  260      uint32_t data_size, uint32_t direction, uint8_t *dataout,
 260  261      uint32_t dataout_size, short timeout, int mode);
 261  262  static int mptsas_free_devhdl(mptsas_t *mpt, uint16_t devhdl);
 262  263  
 263  264  static uint8_t mptsas_get_fw_diag_buffer_number(mptsas_t *mpt,
 264  265      uint32_t unique_id);
 265  266  static void mptsas_start_diag(mptsas_t *mpt, mptsas_cmd_t *cmd);
 266  267  static int mptsas_post_fw_diag_buffer(mptsas_t *mpt,
 267  268      mptsas_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code);
 268  269  static int mptsas_release_fw_diag_buffer(mptsas_t *mpt,
 269  270      mptsas_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code,
 270  271      uint32_t diag_type);
 271  272  static int mptsas_diag_register(mptsas_t *mpt,
 272  273      mptsas_fw_diag_register_t *diag_register, uint32_t *return_code);
 273  274  static int mptsas_diag_unregister(mptsas_t *mpt,
 274  275      mptsas_fw_diag_unregister_t *diag_unregister, uint32_t *return_code);
 275  276  static int mptsas_diag_query(mptsas_t *mpt, mptsas_fw_diag_query_t *diag_query,
 276  277      uint32_t *return_code);
 277  278  static int mptsas_diag_read_buffer(mptsas_t *mpt,
 278  279      mptsas_diag_read_buffer_t *diag_read_buffer, uint8_t *ioctl_buf,
 279  280      uint32_t *return_code, int ioctl_mode);
 280  281  static int mptsas_diag_release(mptsas_t *mpt,
 281  282      mptsas_fw_diag_release_t *diag_release, uint32_t *return_code);
 282  283  static int mptsas_do_diag_action(mptsas_t *mpt, uint32_t action,
 283  284      uint8_t *diag_action, uint32_t length, uint32_t *return_code,
 284  285      int ioctl_mode);
 285  286  static int mptsas_diag_action(mptsas_t *mpt, mptsas_diag_action_t *data,
 286  287      int mode);
 287  288  
 288  289  static int mptsas_pkt_alloc_extern(mptsas_t *mpt, mptsas_cmd_t *cmd,
 289  290      int cmdlen, int tgtlen, int statuslen, int kf);
 290  291  static void mptsas_pkt_destroy_extern(mptsas_t *mpt, mptsas_cmd_t *cmd);
 291  292  
 292  293  static int mptsas_kmem_cache_constructor(void *buf, void *cdrarg, int kmflags);
 293  294  static void mptsas_kmem_cache_destructor(void *buf, void *cdrarg);
 294  295  
 295  296  static int mptsas_cache_frames_constructor(void *buf, void *cdrarg,
 296  297      int kmflags);
 297  298  static void mptsas_cache_frames_destructor(void *buf, void *cdrarg);
 298  299  
 299  300  static void mptsas_check_scsi_io_error(mptsas_t *mpt, pMpi2SCSIIOReply_t reply,
 300  301      mptsas_cmd_t *cmd);
 301  302  static void mptsas_check_task_mgt(mptsas_t *mpt,
 302  303      pMpi2SCSIManagementReply_t reply, mptsas_cmd_t *cmd);
 303  304  static int mptsas_send_scsi_cmd(mptsas_t *mpt, struct scsi_address *ap,
 304  305      mptsas_target_t *ptgt, uchar_t *cdb, int cdblen, struct buf *data_bp,
 305  306      int *resid);
 306  307  
 307  308  static int mptsas_alloc_active_slots(mptsas_t *mpt, int flag);
 308  309  static void mptsas_free_active_slots(mptsas_t *mpt);
 309  310  static int mptsas_start_cmd(mptsas_t *mpt, mptsas_cmd_t *cmd);
 310  311  static int mptsas_start_cmd0(mptsas_t *mpt, mptsas_cmd_t *cmd);
 311  312  
 312  313  static void mptsas_restart_hba(mptsas_t *mpt);
 313  314  
 314  315  static void mptsas_deliver_doneq_thread(mptsas_t *mpt);
 315  316  static void mptsas_doneq_add(mptsas_t *mpt, mptsas_cmd_t *cmd);
 316  317  static inline void mptsas_doneq_add0(mptsas_t *mpt, mptsas_cmd_t *cmd);
 317  318  static void mptsas_doneq_mv(mptsas_t *mpt, uint64_t t);
 318  319  
 319  320  static mptsas_cmd_t *mptsas_doneq_thread_rm(mptsas_t *mpt, uint64_t t);
 320  321  static void mptsas_doneq_empty(mptsas_t *mpt);
 321  322  static void mptsas_doneq_thread(mptsas_doneq_thread_arg_t *arg);
 322  323  
 323  324  static mptsas_cmd_t *mptsas_waitq_rm(mptsas_t *mpt);
 324  325  static void mptsas_waitq_delete(mptsas_t *mpt, mptsas_cmd_t *cmd);
 325  326  
 326  327  static void mptsas_start_watch_reset_delay();
 327  328  static void mptsas_setup_bus_reset_delay(mptsas_t *mpt);
 328  329  static void mptsas_watch_reset_delay(void *arg);
 329  330  static int mptsas_watch_reset_delay_subr(mptsas_t *mpt);
 330  331  
 331  332  static int mptsas_outstanding_cmds_n(mptsas_t *mpt);
 332  333  /*
 333  334   * helper functions
 334  335   */
 335  336  static void mptsas_dump_cmd(mptsas_t *mpt, mptsas_cmd_t *cmd);
 336  337  
 337  338  static dev_info_t *mptsas_find_child(dev_info_t *pdip, char *name);
 338  339  static dev_info_t *mptsas_find_child_phy(dev_info_t *pdip, uint8_t phy);
 339  340  static dev_info_t *mptsas_find_child_addr(dev_info_t *pdip, uint64_t sasaddr,
 340  341      int lun);
 341  342  static mdi_pathinfo_t *mptsas_find_path_addr(dev_info_t *pdip, uint64_t sasaddr,
 342  343      int lun);
 343  344  static mdi_pathinfo_t *mptsas_find_path_phy(dev_info_t *pdip, uint8_t phy);
 344  345  static dev_info_t *mptsas_find_smp_child(dev_info_t *pdip, char *str_wwn);
 345  346  
 346  347  static int mptsas_parse_address(char *name, uint64_t *wwid, uint8_t *phy,
 347  348      int *lun);
 348  349  static int mptsas_parse_smp_name(char *name, uint64_t *wwn);
 349  350  
 350  351  static mptsas_target_t *mptsas_phy_to_tgt(mptsas_t *mpt, int phymask,
 351  352      uint8_t phy);
 352  353  static mptsas_target_t *mptsas_wwid_to_ptgt(mptsas_t *mpt, int phymask,
 353  354      uint64_t wwid);
 354  355  static mptsas_smp_t *mptsas_wwid_to_psmp(mptsas_t *mpt, int phymask,
 355  356      uint64_t wwid);
 356  357  
 357  358  static int mptsas_inquiry(mptsas_t *mpt, mptsas_target_t *ptgt, int lun,
 358  359      uchar_t page, unsigned char *buf, int len, int *rlen, uchar_t evpd);
 359  360  
 360  361  static int mptsas_get_target_device_info(mptsas_t *mpt, uint32_t page_address,
 361  362      uint16_t *handle, mptsas_target_t **pptgt);
 362  363  static void mptsas_update_phymask(mptsas_t *mpt);
 363  364  static inline void mptsas_remove_cmd0(mptsas_t *mpt, mptsas_cmd_t *cmd);
 364  365  
 365  366  static int mptsas_send_sep(mptsas_t *mpt, mptsas_target_t *ptgt,
 366  367      uint32_t *status, uint8_t cmd);
 367  368  static dev_info_t *mptsas_get_dip_from_dev(dev_t dev,
 368  369      mptsas_phymask_t *phymask);
 369  370  static mptsas_target_t *mptsas_addr_to_ptgt(mptsas_t *mpt, char *addr,
 370  371      mptsas_phymask_t phymask);
 371  372  static int mptsas_set_led_status(mptsas_t *mpt, mptsas_target_t *ptgt,
 372  373      uint32_t slotstatus);
 373  374  
 374  375  
 375  376  /*
 376  377   * Enumeration / DR functions
 377  378   */
 378  379  static void mptsas_config_all(dev_info_t *pdip);
 379  380  static int mptsas_config_one_addr(dev_info_t *pdip, uint64_t sasaddr, int lun,
 380  381      dev_info_t **lundip);
 381  382  static int mptsas_config_one_phy(dev_info_t *pdip, uint8_t phy, int lun,
 382  383      dev_info_t **lundip);
 383  384  
 384  385  static int mptsas_config_target(dev_info_t *pdip, mptsas_target_t *ptgt);
 385  386  static int mptsas_offline_target(dev_info_t *pdip, char *name);
 386  387  
 387  388  static int mptsas_config_raid(dev_info_t *pdip, uint16_t target,
 388  389      dev_info_t **dip);
 389  390  
 390  391  static int mptsas_config_luns(dev_info_t *pdip, mptsas_target_t *ptgt);
 391  392  static int mptsas_probe_lun(dev_info_t *pdip, int lun,
 392  393      dev_info_t **dip, mptsas_target_t *ptgt);
 393  394  
 394  395  static int mptsas_create_lun(dev_info_t *pdip, struct scsi_inquiry *sd_inq,
 395  396      dev_info_t **dip, mptsas_target_t *ptgt, int lun);
 396  397  
 397  398  static int mptsas_create_phys_lun(dev_info_t *pdip, struct scsi_inquiry *sd,
 398  399      char *guid, dev_info_t **dip, mptsas_target_t *ptgt, int lun);
 399  400  static int mptsas_create_virt_lun(dev_info_t *pdip, struct scsi_inquiry *sd,
 400  401      char *guid, dev_info_t **dip, mdi_pathinfo_t **pip, mptsas_target_t *ptgt,
 401  402      int lun);
 402  403  
 403  404  static void mptsas_offline_missed_luns(dev_info_t *pdip,
 404  405      uint16_t *repluns, int lun_cnt, mptsas_target_t *ptgt);
 405  406  static int mptsas_offline_lun(dev_info_t *pdip, dev_info_t *rdip,
 406  407      mdi_pathinfo_t *rpip, uint_t flags);
 407  408  
 408  409  static int mptsas_config_smp(dev_info_t *pdip, uint64_t sas_wwn,
 409  410      dev_info_t **smp_dip);
 410  411  static int mptsas_offline_smp(dev_info_t *pdip, mptsas_smp_t *smp_node,
 411  412      uint_t flags);
 412  413  
 413  414  static int mptsas_event_query(mptsas_t *mpt, mptsas_event_query_t *data,
 414  415      int mode, int *rval);
 415  416  static int mptsas_event_enable(mptsas_t *mpt, mptsas_event_enable_t *data,
 416  417      int mode, int *rval);
 417  418  static int mptsas_event_report(mptsas_t *mpt, mptsas_event_report_t *data,
 418  419      int mode, int *rval);
 419  420  static void mptsas_record_event(void *args);
 420  421  static int mptsas_reg_access(mptsas_t *mpt, mptsas_reg_access_t *data,
 421  422      int mode);
 422  423  
 423  424  static void mptsas_hash_init(mptsas_hash_table_t *hashtab);
 424  425  static void mptsas_hash_uninit(mptsas_hash_table_t *hashtab, size_t datalen);
 425  426  static void mptsas_hash_add(mptsas_hash_table_t *hashtab, void *data);
 426  427  static void * mptsas_hash_rem(mptsas_hash_table_t *hashtab, uint64_t key1,
 427  428      mptsas_phymask_t key2);
 428  429  static void * mptsas_hash_search(mptsas_hash_table_t *hashtab, uint64_t key1,
 429  430      mptsas_phymask_t key2);
 430  431  static void * mptsas_hash_traverse(mptsas_hash_table_t *hashtab, int pos);
 431  432  
 432  433  mptsas_target_t *mptsas_tgt_alloc(mptsas_hash_table_t *, uint16_t, uint64_t,
 433  434      uint32_t, mptsas_phymask_t, uint8_t, mptsas_t *);
 434  435  static mptsas_smp_t *mptsas_smp_alloc(mptsas_hash_table_t *hashtab,
 435  436      mptsas_smp_t *data);
 436  437  static void mptsas_smp_free(mptsas_hash_table_t *hashtab, uint64_t wwid,
 437  438      mptsas_phymask_t phymask);
 438  439  static void mptsas_tgt_free(mptsas_hash_table_t *, uint64_t, mptsas_phymask_t);
 439  440  static void * mptsas_search_by_devhdl(mptsas_hash_table_t *, uint16_t);
 440  441  static int mptsas_online_smp(dev_info_t *pdip, mptsas_smp_t *smp_node,
 441  442      dev_info_t **smp_dip);
 442  443  
 443  444  /*
 444  445   * Power management functions
 445  446   */
 446  447  static int mptsas_get_pci_cap(mptsas_t *mpt);
 447  448  static int mptsas_init_pm(mptsas_t *mpt);
 448  449  
 449  450  /*
 450  451   * MPT MSI tunable:
 451  452   *
 452  453   * By default MSI is enabled on all supported platforms.
 453  454   */
 454  455  boolean_t mptsas_enable_msi = B_TRUE;
 455  456  boolean_t mptsas_physical_bind_failed_page_83 = B_FALSE;
 456  457  
 457  458  static int mptsas_register_intrs(mptsas_t *);
 458  459  static void mptsas_unregister_intrs(mptsas_t *);
 459  460  static int mptsas_add_intrs(mptsas_t *, int);
 460  461  static void mptsas_rem_intrs(mptsas_t *);
 461  462  
 462  463  /*
 463  464   * FMA Prototypes
 464  465   */
 465  466  static void mptsas_fm_init(mptsas_t *mpt);
 466  467  static void mptsas_fm_fini(mptsas_t *mpt);
 467  468  static int mptsas_fm_error_cb(dev_info_t *, ddi_fm_error_t *, const void *);
 468  469  
 469  470  extern pri_t minclsyspri, maxclsyspri;
 470  471  
 471  472  /*
 472  473   * This device is created by the SCSI pseudo nexus driver (SCSI vHCI).  It is
 473  474   * under this device that the paths to a physical device are created when
 474  475   * MPxIO is used.
 475  476   */
 476  477  extern dev_info_t       *scsi_vhci_dip;
 477  478  
 478  479  /*
 479  480   * Tunable timeout value for Inquiry VPD page 0x83
 480  481   * By default the value is 30 seconds.
 481  482   */
 482  483  int mptsas_inq83_retry_timeout = 30;
 483  484  
 484  485  /*
 485  486   * This is used to allocate memory for message frame storage, not for
 486  487   * data I/O DMA. All message frames must be stored in the first 4G of
 487  488   * physical memory.
 488  489   */
 489  490  ddi_dma_attr_t mptsas_dma_attrs = {
 490  491          DMA_ATTR_V0,    /* attribute layout version             */
 491  492          0x0ull,         /* address low - should be 0 (longlong) */
 492  493          0xffffffffull,  /* address high - 32-bit max range      */
 493  494          0x00ffffffull,  /* count max - max DMA object size      */
 494  495          4,              /* allocation alignment requirements    */
 495  496          0x78,           /* burstsizes - binary encoded values   */
 496  497          1,              /* minxfer - gran. of DMA engine        */
 497  498          0x00ffffffull,  /* maxxfer - gran. of DMA engine        */
 498  499          0xffffffffull,  /* max segment size (DMA boundary)      */
 499  500          MPTSAS_MAX_DMA_SEGS, /* scatter/gather list length      */
 500  501          512,            /* granularity - device transfer size   */
 501  502          0               /* flags, set to 0                      */
 502  503  };
 503  504  
 504  505  /*
 505  506   * This is used for data I/O DMA memory allocation. (full 64-bit DMA
 506  507   * physical addresses are supported.)
 507  508   */
 508  509  ddi_dma_attr_t mptsas_dma_attrs64 = {
 509  510          DMA_ATTR_V0,    /* attribute layout version             */
 510  511          0x0ull,         /* address low - should be 0 (longlong) */
 511  512          0xffffffffffffffffull,  /* address high - 64-bit max    */
 512  513          0x00ffffffull,  /* count max - max DMA object size      */
 513  514          4,              /* allocation alignment requirements    */
 514  515          0x78,           /* burstsizes - binary encoded values   */
 515  516          1,              /* minxfer - gran. of DMA engine        */
 516  517          0x00ffffffull,  /* maxxfer - gran. of DMA engine        */
 517  518          0xffffffffull,  /* max segment size (DMA boundary)      */
 518  519          MPTSAS_MAX_DMA_SEGS, /* scatter/gather list length      */
 519  520          512,            /* granularity - device transfer size   */
 520  521          DDI_DMA_RELAXED_ORDERING        /* flags, enable relaxed ordering */
 521  522  };
 522  523  
 523  524  ddi_device_acc_attr_t mptsas_dev_attr = {
 524  525          DDI_DEVICE_ATTR_V1,
 525  526          DDI_STRUCTURE_LE_ACC,
 526  527          DDI_STRICTORDER_ACC,
 527  528          DDI_DEFAULT_ACC
 528  529  };
 529  530  
 530  531  static struct cb_ops mptsas_cb_ops = {
 531  532          scsi_hba_open,          /* open */
 532  533          scsi_hba_close,         /* close */
 533  534          nodev,                  /* strategy */
 534  535          nodev,                  /* print */
 535  536          nodev,                  /* dump */
 536  537          nodev,                  /* read */
 537  538          nodev,                  /* write */
 538  539          mptsas_ioctl,           /* ioctl */
 539  540          nodev,                  /* devmap */
 540  541          nodev,                  /* mmap */
 541  542          nodev,                  /* segmap */
 542  543          nochpoll,               /* chpoll */
 543  544          ddi_prop_op,            /* cb_prop_op */
 544  545          NULL,                   /* streamtab */
 545  546          D_MP,                   /* cb_flag */
 546  547          CB_REV,                 /* rev */
 547  548          nodev,                  /* aread */
 548  549          nodev                   /* awrite */
 549  550  };
 550  551  
 551  552  static struct dev_ops mptsas_ops = {
 552  553          DEVO_REV,               /* devo_rev, */
 553  554          0,                      /* refcnt  */
 554  555          ddi_no_info,            /* info */
 555  556          nulldev,                /* identify */
 556  557          nulldev,                /* probe */
 557  558          mptsas_attach,          /* attach */
 558  559          mptsas_detach,          /* detach */
 559  560  #ifdef  __sparc
 560  561          mptsas_reset,
 561  562  #else
 562  563          nodev,                  /* reset */
 563  564  #endif  /* __sparc */
 564  565          &mptsas_cb_ops,         /* driver operations */
 565  566          NULL,                   /* bus operations */
 566  567          mptsas_power,           /* power management */
 567  568  #ifdef  __sparc
 568  569          ddi_quiesce_not_needed
 569  570  #else
 570  571          mptsas_quiesce          /* quiesce */
 571  572  #endif  /* __sparc */
 572  573  };
 573  574  
 574  575  
 575  576  #define MPTSAS_MOD_STRING "MPTSAS HBA Driver 00.00.00.24"
 576  577  
 577  578  static struct modldrv modldrv = {
 578  579          &mod_driverops, /* Type of module. This one is a driver */
 579  580          MPTSAS_MOD_STRING, /* Name of the module. */
 580  581          &mptsas_ops,    /* driver ops */
 581  582  };
 582  583  
 583  584  static struct modlinkage modlinkage = {
 584  585          MODREV_1, &modldrv, NULL
 585  586  };
 586  587  #define TARGET_PROP     "target"
 587  588  #define LUN_PROP        "lun"
 588  589  #define LUN64_PROP      "lun64"
 589  590  #define SAS_PROP        "sas-mpt"
 590  591  #define MDI_GUID        "wwn"
 591  592  #define NDI_GUID        "guid"
 592  593  #define MPTSAS_DEV_GONE "mptsas_dev_gone"
 593  594  
 594  595  /*
 595  596   * Local static data
 596  597   */
 597  598  #if defined(MPTSAS_DEBUG)
 598  599  uint32_t mptsas_debug_flags = 0;
 599  600  #endif  /* defined(MPTSAS_DEBUG) */
 600  601  uint32_t mptsas_debug_resets = 0;
 601  602  
 602  603  static kmutex_t         mptsas_global_mutex;
 603  604  static void             *mptsas_state;          /* soft state ptr */
 604  605  static krwlock_t        mptsas_global_rwlock;
 605  606  
 606  607  static kmutex_t         mptsas_log_mutex;
 607  608  static char             mptsas_log_buf[256];
 608  609  _NOTE(MUTEX_PROTECTS_DATA(mptsas_log_mutex, mptsas_log_buf))
 609  610  
 610  611  static mptsas_t *mptsas_head, *mptsas_tail;
 611  612  static clock_t mptsas_scsi_watchdog_tick;
 612  613  static clock_t mptsas_tick;
 613  614  static timeout_id_t mptsas_reset_watch;
 614  615  static timeout_id_t mptsas_timeout_id;
 615  616  static int mptsas_timeouts_enabled = 0;
 616  617  /*
 617  618   * warlock directives
 618  619   */
 619  620  _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", scsi_pkt \
 620  621          mptsas_cmd NcrTableIndirect buf scsi_cdb scsi_status))
 621  622  _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", smp_pkt))
 622  623  _NOTE(SCHEME_PROTECTS_DATA("stable data", scsi_device scsi_address))
 623  624  _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mptsas_tgt_private))
 624  625  _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran::tran_tgt_private))
 625  626  
 626  627  /*
 627  628   * SM - HBA statics
 628  629   */
 629  630  char    *mptsas_driver_rev = MPTSAS_MOD_STRING;
 630  631  
 631  632  #ifdef MPTSAS_DEBUG
 632  633  void debug_enter(char *);
 633  634  #endif
 634  635  
 635  636  /*
 636  637   * Notes:
 637  638   *      - scsi_hba_init(9F) initializes SCSI HBA modules
 638  639   *      - must call scsi_hba_fini(9F) if modload() fails
 639  640   */
 640  641  int
 641  642  _init(void)
 642  643  {
 643  644          int status;
 644  645          /* CONSTCOND */
 645  646          ASSERT(NO_COMPETING_THREADS);
 646  647  
 647  648          NDBG0(("_init"));
 648  649  
 649  650          status = ddi_soft_state_init(&mptsas_state, MPTSAS_SIZE,
 650  651              MPTSAS_INITIAL_SOFT_SPACE);
 651  652          if (status != 0) {
 652  653                  return (status);
 653  654          }
 654  655  
 655  656          if ((status = scsi_hba_init(&modlinkage)) != 0) {
 656  657                  ddi_soft_state_fini(&mptsas_state);
 657  658                  return (status);
 658  659          }
 659  660  
 660  661          mutex_init(&mptsas_global_mutex, NULL, MUTEX_DRIVER, NULL);
 661  662          rw_init(&mptsas_global_rwlock, NULL, RW_DRIVER, NULL);
 662  663          mutex_init(&mptsas_log_mutex, NULL, MUTEX_DRIVER, NULL);
 663  664  
 664  665          if ((status = mod_install(&modlinkage)) != 0) {
 665  666                  mutex_destroy(&mptsas_log_mutex);
 666  667                  rw_destroy(&mptsas_global_rwlock);
 667  668                  mutex_destroy(&mptsas_global_mutex);
 668  669                  ddi_soft_state_fini(&mptsas_state);
 669  670                  scsi_hba_fini(&modlinkage);
 670  671          }
 671  672  
 672  673          return (status);
 673  674  }
 674  675  
 675  676  /*
 676  677   * Notes:
 677  678   *      - scsi_hba_fini(9F) uninitializes SCSI HBA modules
 678  679   */
 679  680  int
 680  681  _fini(void)
 681  682  {
 682  683          int     status;
 683  684          /* CONSTCOND */
 684  685          ASSERT(NO_COMPETING_THREADS);
 685  686  
 686  687          NDBG0(("_fini"));
 687  688  
 688  689          if ((status = mod_remove(&modlinkage)) == 0) {
 689  690                  ddi_soft_state_fini(&mptsas_state);
 690  691                  scsi_hba_fini(&modlinkage);
 691  692                  mutex_destroy(&mptsas_global_mutex);
 692  693                  rw_destroy(&mptsas_global_rwlock);
 693  694                  mutex_destroy(&mptsas_log_mutex);
 694  695          }
 695  696          return (status);
 696  697  }
 697  698  
 698  699  /*
 699  700   * The loadable-module _info(9E) entry point
 700  701   */
 701  702  int
 702  703  _info(struct modinfo *modinfop)
 703  704  {
 704  705          /* CONSTCOND */
 705  706          ASSERT(NO_COMPETING_THREADS);
 706  707          NDBG0(("mptsas _info"));
 707  708  
 708  709          return (mod_info(&modlinkage, modinfop));
 709  710  }
 710  711  
 711  712  
 712  713  static int
 713  714  mptsas_iport_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
 714  715  {
 715  716          dev_info_t              *pdip;
 716  717          mptsas_t                *mpt;
 717  718          scsi_hba_tran_t         *hba_tran;
 718  719          char                    *iport = NULL;
 719  720          char                    phymask[MPTSAS_MAX_PHYS];
 720  721          mptsas_phymask_t        phy_mask = 0;
 721  722          int                     dynamic_port = 0;
 722  723          uint32_t                page_address;
 723  724          char                    initiator_wwnstr[MPTSAS_WWN_STRLEN];
 724  725          int                     rval = DDI_FAILURE;
 725  726          int                     i = 0;
 726  727          uint8_t                 numphys = 0;
 727  728          uint8_t                 phy_id;
 728  729          uint8_t                 phy_port = 0;
 729  730          uint16_t                attached_devhdl = 0;
 730  731          uint32_t                dev_info;
 731  732          uint64_t                attached_sas_wwn;
 732  733          uint16_t                dev_hdl;
 733  734          uint16_t                pdev_hdl;
 734  735          uint16_t                bay_num, enclosure;
 735  736          char                    attached_wwnstr[MPTSAS_WWN_STRLEN];
 736  737  
 737  738          /* CONSTCOND */
 738  739          ASSERT(NO_COMPETING_THREADS);
 739  740  
 740  741          switch (cmd) {
 741  742          case DDI_ATTACH:
 742  743                  break;
 743  744  
 744  745          case DDI_RESUME:
 745  746                  /*
 746  747                   * If this a scsi-iport node, nothing to do here.
 747  748                   */
 748  749                  return (DDI_SUCCESS);
 749  750  
 750  751          default:
 751  752                  return (DDI_FAILURE);
 752  753          }
 753  754  
 754  755          pdip = ddi_get_parent(dip);
 755  756  
 756  757          if ((hba_tran = ndi_flavorv_get(pdip, SCSA_FLAVOR_SCSI_DEVICE)) ==
 757  758              NULL) {
 758  759                  cmn_err(CE_WARN, "Failed attach iport because fail to "
 759  760                      "get tran vector for the HBA node");
 760  761                  return (DDI_FAILURE);
 761  762          }
 762  763  
 763  764          mpt = TRAN2MPT(hba_tran);
 764  765          ASSERT(mpt != NULL);
 765  766          if (mpt == NULL)
 766  767                  return (DDI_FAILURE);
 767  768  
 768  769          if ((hba_tran = ndi_flavorv_get(dip, SCSA_FLAVOR_SCSI_DEVICE)) ==
 769  770              NULL) {
 770  771                  mptsas_log(mpt, CE_WARN, "Failed attach iport because fail to "
 771  772                      "get tran vector for the iport node");
 772  773                  return (DDI_FAILURE);
 773  774          }
 774  775  
 775  776          /*
 776  777           * Overwrite parent's tran_hba_private to iport's tran vector
 777  778           */
 778  779          hba_tran->tran_hba_private = mpt;
 779  780  
 780  781          ddi_report_dev(dip);
 781  782  
 782  783          /*
 783  784           * Get SAS address for initiator port according dev_handle
 784  785           */
 785  786          iport = ddi_get_name_addr(dip);
 786  787          if (iport && strncmp(iport, "v0", 2) == 0) {
 787  788                  if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
 788  789                      MPTSAS_VIRTUAL_PORT, 1) !=
 789  790                      DDI_PROP_SUCCESS) {
 790  791                          (void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
 791  792                              MPTSAS_VIRTUAL_PORT);
 792  793                          mptsas_log(mpt, CE_WARN, "mptsas virtual port "
 793  794                              "prop update failed");
 794  795                          return (DDI_FAILURE);
 795  796                  }
 796  797                  return (DDI_SUCCESS);
 797  798          }
 798  799  
 799  800          mutex_enter(&mpt->m_mutex);
 800  801          for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
 801  802                  bzero(phymask, sizeof (phymask));
 802  803                  (void) sprintf(phymask,
 803  804                      "%x", mpt->m_phy_info[i].phy_mask);
 804  805                  if (strcmp(phymask, iport) == 0) {
 805  806                          break;
 806  807                  }
 807  808          }
 808  809  
 809  810          if (i == MPTSAS_MAX_PHYS) {
 810  811                  mptsas_log(mpt, CE_WARN, "Failed attach port %s because port"
 811  812                      "seems not exist", iport);
 812  813                  mutex_exit(&mpt->m_mutex);
 813  814                  return (DDI_FAILURE);
 814  815          }
 815  816  
 816  817          phy_mask = mpt->m_phy_info[i].phy_mask;
 817  818  
 818  819          if (mpt->m_phy_info[i].port_flags & AUTO_PORT_CONFIGURATION)
 819  820                  dynamic_port = 1;
 820  821          else
 821  822                  dynamic_port = 0;
 822  823  
 823  824          /*
 824  825           * Update PHY info for smhba
 825  826           */
 826  827          if (mptsas_smhba_phy_init(mpt)) {
 827  828                  mutex_exit(&mpt->m_mutex);
 828  829                  mptsas_log(mpt, CE_WARN, "mptsas phy update "
 829  830                      "failed");
 830  831                  return (DDI_FAILURE);
 831  832          }
 832  833  
 833  834          mutex_exit(&mpt->m_mutex);
 834  835  
 835  836          numphys = 0;
 836  837          for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
 837  838                  if ((phy_mask >> i) & 0x01) {
 838  839                          numphys++;
 839  840                  }
 840  841          }
 841  842  
 842  843          bzero(initiator_wwnstr, sizeof (initiator_wwnstr));
 843  844          (void) sprintf(initiator_wwnstr, "w%016"PRIx64,
 844  845              mpt->un.m_base_wwid);
 845  846  
 846  847          if (ddi_prop_update_string(DDI_DEV_T_NONE, dip,
 847  848              SCSI_ADDR_PROP_INITIATOR_PORT, initiator_wwnstr) !=
 848  849              DDI_PROP_SUCCESS) {
 849  850                  (void) ddi_prop_remove(DDI_DEV_T_NONE,
 850  851                      dip, SCSI_ADDR_PROP_INITIATOR_PORT);
 851  852                  mptsas_log(mpt, CE_WARN, "mptsas Initiator port "
 852  853                      "prop update failed");
 853  854                  return (DDI_FAILURE);
 854  855          }
 855  856          if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
 856  857              MPTSAS_NUM_PHYS, numphys) !=
 857  858              DDI_PROP_SUCCESS) {
 858  859                  (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, MPTSAS_NUM_PHYS);
 859  860                  return (DDI_FAILURE);
 860  861          }
 861  862  
 862  863          if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
 863  864              "phymask", phy_mask) !=
 864  865              DDI_PROP_SUCCESS) {
 865  866                  (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, "phymask");
 866  867                  mptsas_log(mpt, CE_WARN, "mptsas phy mask "
 867  868                      "prop update failed");
 868  869                  return (DDI_FAILURE);
 869  870          }
 870  871  
 871  872          if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
 872  873              "dynamic-port", dynamic_port) !=
 873  874              DDI_PROP_SUCCESS) {
 874  875                  (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, "dynamic-port");
 875  876                  mptsas_log(mpt, CE_WARN, "mptsas dynamic port "
 876  877                      "prop update failed");
 877  878                  return (DDI_FAILURE);
 878  879          }
 879  880          if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
 880  881              MPTSAS_VIRTUAL_PORT, 0) !=
 881  882              DDI_PROP_SUCCESS) {
 882  883                  (void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
 883  884                      MPTSAS_VIRTUAL_PORT);
 884  885                  mptsas_log(mpt, CE_WARN, "mptsas virtual port "
 885  886                      "prop update failed");
 886  887                  return (DDI_FAILURE);
 887  888          }
 888  889          mptsas_smhba_set_phy_props(mpt,
 889  890              iport, dip, numphys, &attached_devhdl);
 890  891  
 891  892          mutex_enter(&mpt->m_mutex);
 892  893          page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
 893  894              MPI2_SAS_DEVICE_PGAD_FORM_MASK) | (uint32_t)attached_devhdl;
 894  895          rval = mptsas_get_sas_device_page0(mpt, page_address, &dev_hdl,
 895  896              &attached_sas_wwn, &dev_info, &phy_port, &phy_id,
 896  897              &pdev_hdl, &bay_num, &enclosure);
 897  898          if (rval != DDI_SUCCESS) {
 898  899                  mptsas_log(mpt, CE_WARN,
 899  900                      "Failed to get device page0 for handle:%d",
 900  901                      attached_devhdl);
 901  902                  mutex_exit(&mpt->m_mutex);
 902  903                  return (DDI_FAILURE);
 903  904          }
 904  905  
 905  906          for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
 906  907                  bzero(phymask, sizeof (phymask));
 907  908                  (void) sprintf(phymask, "%x", mpt->m_phy_info[i].phy_mask);
 908  909                  if (strcmp(phymask, iport) == 0) {
 909  910                          (void) sprintf(&mpt->m_phy_info[i].smhba_info.path[0],
 910  911                              "%x",
 911  912                              mpt->m_phy_info[i].phy_mask);
 912  913                  }
 913  914          }
 914  915          mutex_exit(&mpt->m_mutex);
 915  916  
 916  917          bzero(attached_wwnstr, sizeof (attached_wwnstr));
 917  918          (void) sprintf(attached_wwnstr, "w%016"PRIx64,
 918  919              attached_sas_wwn);
 919  920          if (ddi_prop_update_string(DDI_DEV_T_NONE, dip,
 920  921              SCSI_ADDR_PROP_ATTACHED_PORT, attached_wwnstr) !=
 921  922              DDI_PROP_SUCCESS) {
 922  923                  (void) ddi_prop_remove(DDI_DEV_T_NONE,
 923  924                      dip, SCSI_ADDR_PROP_ATTACHED_PORT);
 924  925                  return (DDI_FAILURE);
 925  926          }
 926  927  
 927  928          /* Create kstats for each phy on this iport */
 928  929  
 929  930          mptsas_create_phy_stats(mpt, iport, dip);
 930  931  
 931  932          /*
 932  933           * register sas hba iport with mdi (MPxIO/vhci)
 933  934           */
 934  935          if (mdi_phci_register(MDI_HCI_CLASS_SCSI,
 935  936              dip, 0) == MDI_SUCCESS) {
 936  937                  mpt->m_mpxio_enable = TRUE;
 937  938          }
 938  939          return (DDI_SUCCESS);
 939  940  }
 940  941  
 941  942  /*
 942  943   * Notes:
 943  944   *      Set up all device state and allocate data structures,
 944  945   *      mutexes, condition variables, etc. for device operation.
 945  946   *      Add interrupts needed.
 946  947   *      Return DDI_SUCCESS if device is ready, else return DDI_FAILURE.
 947  948   */
 948  949  static int
 949  950  mptsas_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
 950  951  {
 951  952          mptsas_t                *mpt = NULL;
 952  953          int                     instance, i, j;
 953  954          int                     doneq_thread_num;
 954  955          char                    intr_added = 0;
 955  956          char                    map_setup = 0;
 956  957          char                    config_setup = 0;
 957  958          char                    hba_attach_setup = 0;
 958  959          char                    smp_attach_setup = 0;
 959  960          char                    mutex_init_done = 0;
 960  961          char                    event_taskq_create = 0;
 961  962          char                    dr_taskq_create = 0;
 962  963          char                    doneq_thread_create = 0;
 963  964          scsi_hba_tran_t         *hba_tran;
 964  965          uint_t                  mem_bar = MEM_SPACE;
 965  966          int                     rval = DDI_FAILURE;
 966  967  
 967  968          /* CONSTCOND */
 968  969          ASSERT(NO_COMPETING_THREADS);
 969  970  
 970  971          if (scsi_hba_iport_unit_address(dip)) {
 971  972                  return (mptsas_iport_attach(dip, cmd));
 972  973          }
 973  974  
 974  975          switch (cmd) {
 975  976          case DDI_ATTACH:
 976  977                  break;
 977  978  
 978  979          case DDI_RESUME:
 979  980                  if ((hba_tran = ddi_get_driver_private(dip)) == NULL)
 980  981                          return (DDI_FAILURE);
 981  982  
 982  983                  mpt = TRAN2MPT(hba_tran);
 983  984  
 984  985                  if (!mpt) {
 985  986                          return (DDI_FAILURE);
 986  987                  }
 987  988  
 988  989                  /*
 989  990                   * Reset hardware and softc to "no outstanding commands"
 990  991                   * Note that a check condition can result on first command
 991  992                   * to a target.
 992  993                   */
 993  994                  mutex_enter(&mpt->m_mutex);
 994  995  
 995  996                  /*
 996  997                   * raise power.
 997  998                   */
 998  999                  if (mpt->m_options & MPTSAS_OPT_PM) {
 999 1000                          mutex_exit(&mpt->m_mutex);
1000 1001                          (void) pm_busy_component(dip, 0);
1001 1002                          rval = pm_power_has_changed(dip, 0, PM_LEVEL_D0);
1002 1003                          if (rval == DDI_SUCCESS) {
1003 1004                                  mutex_enter(&mpt->m_mutex);
1004 1005                          } else {
1005 1006                                  /*
1006 1007                                   * The pm_raise_power() call above failed,
1007 1008                                   * and that can only occur if we were unable
1008 1009                                   * to reset the hardware.  This is probably
1009 1010                                   * due to unhealty hardware, and because
1010 1011                                   * important filesystems(such as the root
1011 1012                                   * filesystem) could be on the attached disks,
1012 1013                                   * it would not be a good idea to continue,
1013 1014                                   * as we won't be entirely certain we are
1014 1015                                   * writing correct data.  So we panic() here
1015 1016                                   * to not only prevent possible data corruption,
1016 1017                                   * but to give developers or end users a hope
1017 1018                                   * of identifying and correcting any problems.
1018 1019                                   */
1019 1020                                  fm_panic("mptsas could not reset hardware "
1020 1021                                      "during resume");
1021 1022                          }
1022 1023                  }
1023 1024  
1024 1025                  mpt->m_suspended = 0;
1025 1026  
1026 1027                  /*
1027 1028                   * Reinitialize ioc
1028 1029                   */
1029 1030                  mpt->m_softstate |= MPTSAS_SS_MSG_UNIT_RESET;
1030 1031                  if (mptsas_init_chip(mpt, FALSE) == DDI_FAILURE) {
1031 1032                          mutex_exit(&mpt->m_mutex);
1032 1033                          if (mpt->m_options & MPTSAS_OPT_PM) {
1033 1034                                  (void) pm_idle_component(dip, 0);
1034 1035                          }
1035 1036                          fm_panic("mptsas init chip fail during resume");
1036 1037                  }
1037 1038                  /*
1038 1039                   * mptsas_update_driver_data needs interrupts so enable them
1039 1040                   * first.
1040 1041                   */
1041 1042                  MPTSAS_ENABLE_INTR(mpt);
1042 1043                  mptsas_update_driver_data(mpt);
1043 1044  
1044 1045                  /* start requests, if possible */
1045 1046                  mptsas_restart_hba(mpt);
1046 1047  
1047 1048                  mutex_exit(&mpt->m_mutex);
1048 1049  
1049 1050                  /*
1050 1051                   * Restart watch thread
1051 1052                   */
1052 1053                  mutex_enter(&mptsas_global_mutex);
1053 1054                  if (mptsas_timeout_id == 0) {
1054 1055                          mptsas_timeout_id = timeout(mptsas_watch, NULL,
1055 1056                              mptsas_tick);
1056 1057                          mptsas_timeouts_enabled = 1;
1057 1058                  }
1058 1059                  mutex_exit(&mptsas_global_mutex);
1059 1060  
1060 1061                  /* report idle status to pm framework */
1061 1062                  if (mpt->m_options & MPTSAS_OPT_PM) {
1062 1063                          (void) pm_idle_component(dip, 0);
1063 1064                  }
1064 1065  
1065 1066                  return (DDI_SUCCESS);
1066 1067  
1067 1068          default:
1068 1069                  return (DDI_FAILURE);
1069 1070  
1070 1071          }
1071 1072  
1072 1073          instance = ddi_get_instance(dip);
1073 1074  
1074 1075          /*
1075 1076           * Allocate softc information.
1076 1077           */
1077 1078          if (ddi_soft_state_zalloc(mptsas_state, instance) != DDI_SUCCESS) {
1078 1079                  mptsas_log(NULL, CE_WARN,
1079 1080                      "mptsas%d: cannot allocate soft state", instance);
1080 1081                  goto fail;
1081 1082          }
1082 1083  
1083 1084          mpt = ddi_get_soft_state(mptsas_state, instance);
1084 1085  
1085 1086          if (mpt == NULL) {
1086 1087                  mptsas_log(NULL, CE_WARN,
1087 1088                      "mptsas%d: cannot get soft state", instance);
1088 1089                  goto fail;
1089 1090          }
1090 1091  
1091 1092          /* Indicate that we are 'sizeof (scsi_*(9S))' clean. */
1092 1093          scsi_size_clean(dip);
1093 1094  
1094 1095          mpt->m_dip = dip;
1095 1096          mpt->m_instance = instance;
1096 1097  
1097 1098          /* Make a per-instance copy of the structures */
1098 1099          mpt->m_io_dma_attr = mptsas_dma_attrs64;
1099 1100          mpt->m_msg_dma_attr = mptsas_dma_attrs;
1100 1101          mpt->m_reg_acc_attr = mptsas_dev_attr;
1101 1102          mpt->m_dev_acc_attr = mptsas_dev_attr;
1102 1103  
1103 1104          /*
1104 1105           * Initialize FMA
1105 1106           */
1106 1107          mpt->m_fm_capabilities = ddi_getprop(DDI_DEV_T_ANY, mpt->m_dip,
1107 1108              DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS, "fm-capable",
1108 1109              DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
1109 1110              DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
1110 1111  
1111 1112          mptsas_fm_init(mpt);
1112 1113  
1113 1114          if (mptsas_alloc_handshake_msg(mpt,
1114 1115              sizeof (Mpi2SCSITaskManagementRequest_t)) == DDI_FAILURE) {
1115 1116                  mptsas_log(mpt, CE_WARN, "cannot initialize handshake msg.");
1116 1117                  goto fail;
1117 1118          }
1118 1119  
1119 1120          /*
1120 1121           * Setup configuration space
1121 1122           */
1122 1123          if (mptsas_config_space_init(mpt) == FALSE) {
1123 1124                  mptsas_log(mpt, CE_WARN, "mptsas_config_space_init failed");
1124 1125                  goto fail;
1125 1126          }
1126 1127          config_setup++;
1127 1128  
1128 1129          if (ddi_regs_map_setup(dip, mem_bar, (caddr_t *)&mpt->m_reg,
1129 1130              0, 0, &mpt->m_reg_acc_attr, &mpt->m_datap) != DDI_SUCCESS) {
1130 1131                  mptsas_log(mpt, CE_WARN, "map setup failed");
1131 1132                  goto fail;
1132 1133          }
1133 1134          map_setup++;
1134 1135  
1135 1136          /*
1136 1137           * A taskq is created for dealing with the event handler
1137 1138           */
1138 1139          if ((mpt->m_event_taskq = ddi_taskq_create(dip, "mptsas_event_taskq",
1139 1140              1, TASKQ_DEFAULTPRI, 0)) == NULL) {
1140 1141                  mptsas_log(mpt, CE_NOTE, "ddi_taskq_create failed");
1141 1142                  goto fail;
1142 1143          }
1143 1144          event_taskq_create++;
1144 1145  
1145 1146          /*
1146 1147           * A taskq is created for dealing with dr events
1147 1148           */
1148 1149          if ((mpt->m_dr_taskq = ddi_taskq_create(dip,
1149 1150              "mptsas_dr_taskq",
1150 1151              1, TASKQ_DEFAULTPRI, 0)) == NULL) {
1151 1152                  mptsas_log(mpt, CE_NOTE, "ddi_taskq_create for discovery "
1152 1153                      "failed");
1153 1154                  goto fail;
1154 1155          }
1155 1156          dr_taskq_create++;
1156 1157  
1157 1158          mpt->m_doneq_thread_threshold = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1158 1159              0, "mptsas_doneq_thread_threshold_prop", 10);
1159 1160          mpt->m_doneq_length_threshold = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1160 1161              0, "mptsas_doneq_length_threshold_prop", 8);
1161 1162          mpt->m_doneq_thread_n = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1162 1163              0, "mptsas_doneq_thread_n_prop", 8);
1163 1164  
1164 1165          if (mpt->m_doneq_thread_n) {
1165 1166                  cv_init(&mpt->m_doneq_thread_cv, NULL, CV_DRIVER, NULL);
1166 1167                  mutex_init(&mpt->m_doneq_mutex, NULL, MUTEX_DRIVER, NULL);
1167 1168  
1168 1169                  mutex_enter(&mpt->m_doneq_mutex);
1169 1170                  mpt->m_doneq_thread_id =
1170 1171                      kmem_zalloc(sizeof (mptsas_doneq_thread_list_t)
1171 1172                      * mpt->m_doneq_thread_n, KM_SLEEP);
1172 1173  
1173 1174                  for (j = 0; j < mpt->m_doneq_thread_n; j++) {
1174 1175                          cv_init(&mpt->m_doneq_thread_id[j].cv, NULL,
1175 1176                              CV_DRIVER, NULL);
1176 1177                          mutex_init(&mpt->m_doneq_thread_id[j].mutex, NULL,
1177 1178                              MUTEX_DRIVER, NULL);
1178 1179                          mutex_enter(&mpt->m_doneq_thread_id[j].mutex);
1179 1180                          mpt->m_doneq_thread_id[j].flag |=
1180 1181                              MPTSAS_DONEQ_THREAD_ACTIVE;
1181 1182                          mpt->m_doneq_thread_id[j].arg.mpt = mpt;
1182 1183                          mpt->m_doneq_thread_id[j].arg.t = j;
1183 1184                          mpt->m_doneq_thread_id[j].threadp =
1184 1185                              thread_create(NULL, 0, mptsas_doneq_thread,
1185 1186                              &mpt->m_doneq_thread_id[j].arg,
1186 1187                              0, &p0, TS_RUN, minclsyspri);
1187 1188                          mpt->m_doneq_thread_id[j].donetail =
1188 1189                              &mpt->m_doneq_thread_id[j].doneq;
1189 1190                          mutex_exit(&mpt->m_doneq_thread_id[j].mutex);
1190 1191                  }
1191 1192                  mutex_exit(&mpt->m_doneq_mutex);
1192 1193                  doneq_thread_create++;
1193 1194          }
1194 1195  
1195 1196          /* Initialize mutex used in interrupt handler */
1196 1197          mutex_init(&mpt->m_mutex, NULL, MUTEX_DRIVER,
1197 1198              DDI_INTR_PRI(mpt->m_intr_pri));
1198 1199          mutex_init(&mpt->m_passthru_mutex, NULL, MUTEX_DRIVER, NULL);
1199 1200          mutex_init(&mpt->m_intr_mutex, NULL, MUTEX_DRIVER,
1200 1201              DDI_INTR_PRI(mpt->m_intr_pri));
1201 1202          for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
1202 1203                  mutex_init(&mpt->m_phy_info[i].smhba_info.phy_mutex,
1203 1204                      NULL, MUTEX_DRIVER,
1204 1205                      DDI_INTR_PRI(mpt->m_intr_pri));
1205 1206          }
1206 1207  
1207 1208          cv_init(&mpt->m_cv, NULL, CV_DRIVER, NULL);
1208 1209          cv_init(&mpt->m_passthru_cv, NULL, CV_DRIVER, NULL);
1209 1210          cv_init(&mpt->m_fw_cv, NULL, CV_DRIVER, NULL);
1210 1211          cv_init(&mpt->m_config_cv, NULL, CV_DRIVER, NULL);
1211 1212          cv_init(&mpt->m_fw_diag_cv, NULL, CV_DRIVER, NULL);
1212 1213          mutex_init_done++;
1213 1214  
1214 1215          /*
1215 1216           * Disable hardware interrupt since we're not ready to
1216 1217           * handle it yet.
1217 1218           */
1218 1219          MPTSAS_DISABLE_INTR(mpt);
1219 1220          if (mptsas_register_intrs(mpt) == FALSE)
1220 1221                  goto fail;
1221 1222          intr_added++;
1222 1223  
1223 1224          mutex_enter(&mpt->m_mutex);
1224 1225          /*
1225 1226           * Initialize power management component
1226 1227           */
1227 1228          if (mpt->m_options & MPTSAS_OPT_PM) {
1228 1229                  if (mptsas_init_pm(mpt)) {
1229 1230                          mutex_exit(&mpt->m_mutex);
1230 1231                          mptsas_log(mpt, CE_WARN, "mptsas pm initialization "
1231 1232                              "failed");
1232 1233                          goto fail;
1233 1234                  }
1234 1235          }
1235 1236  
1236 1237          /*
1237 1238           * Initialize chip using Message Unit Reset, if allowed
1238 1239           */
1239 1240          mpt->m_softstate |= MPTSAS_SS_MSG_UNIT_RESET;
1240 1241          if (mptsas_init_chip(mpt, TRUE) == DDI_FAILURE) {
1241 1242                  mutex_exit(&mpt->m_mutex);
1242 1243                  mptsas_log(mpt, CE_WARN, "mptsas chip initialization failed");
1243 1244                  goto fail;
1244 1245          }
1245 1246  
1246 1247          /*
1247 1248           * Fill in the phy_info structure and get the base WWID
1248 1249           */
1249 1250          if (mptsas_get_manufacture_page5(mpt) == DDI_FAILURE) {
1250 1251                  mptsas_log(mpt, CE_WARN,
1251 1252                      "mptsas_get_manufacture_page5 failed!");
1252 1253                  goto fail;
1253 1254          }
1254 1255  
1255 1256          if (mptsas_get_sas_io_unit_page_hndshk(mpt)) {
1256 1257                  mptsas_log(mpt, CE_WARN,
1257 1258                      "mptsas_get_sas_io_unit_page_hndshk failed!");
1258 1259                  goto fail;
1259 1260          }
1260 1261  
1261 1262          if (mptsas_get_manufacture_page0(mpt) == DDI_FAILURE) {
1262 1263                  mptsas_log(mpt, CE_WARN,
1263 1264                      "mptsas_get_manufacture_page0 failed!");
1264 1265                  goto fail;
1265 1266          }
1266 1267  
1267 1268          mutex_exit(&mpt->m_mutex);
1268 1269  
1269 1270          /*
1270 1271           * Register the iport for multiple port HBA
1271 1272           */
1272 1273          mptsas_iport_register(mpt);
1273 1274  
1274 1275          /*
1275 1276           * initialize SCSI HBA transport structure
1276 1277           */
1277 1278          if (mptsas_hba_setup(mpt) == FALSE)
1278 1279                  goto fail;
1279 1280          hba_attach_setup++;
1280 1281  
1281 1282          if (mptsas_smp_setup(mpt) == FALSE)
1282 1283                  goto fail;
1283 1284          smp_attach_setup++;
1284 1285  
1285 1286          if (mptsas_cache_create(mpt) == FALSE)
1286 1287                  goto fail;
1287 1288  
1288 1289          mpt->m_scsi_reset_delay = ddi_prop_get_int(DDI_DEV_T_ANY,
1289 1290              dip, 0, "scsi-reset-delay", SCSI_DEFAULT_RESET_DELAY);
1290 1291          if (mpt->m_scsi_reset_delay == 0) {
1291 1292                  mptsas_log(mpt, CE_NOTE,
1292 1293                      "scsi_reset_delay of 0 is not recommended,"
1293 1294                      " resetting to SCSI_DEFAULT_RESET_DELAY\n");
1294 1295                  mpt->m_scsi_reset_delay = SCSI_DEFAULT_RESET_DELAY;
1295 1296          }
1296 1297  
1297 1298          /*
1298 1299           * Initialize the wait and done FIFO queue
1299 1300           */
1300 1301          mpt->m_donetail = &mpt->m_doneq;
1301 1302          mpt->m_waitqtail = &mpt->m_waitq;
1302 1303  
1303 1304          /*
1304 1305           * ioc cmd queue initialize
1305 1306           */
1306 1307          mpt->m_ioc_event_cmdtail = &mpt->m_ioc_event_cmdq;
1307 1308          mpt->m_dev_handle = 0xFFFF;
1308 1309  
1309 1310          MPTSAS_ENABLE_INTR(mpt);
1310 1311  
1311 1312          /*
1312 1313           * enable event notification
1313 1314           */
1314 1315          mutex_enter(&mpt->m_mutex);
1315 1316          if (mptsas_ioc_enable_event_notification(mpt)) {
1316 1317                  mutex_exit(&mpt->m_mutex);
1317 1318                  goto fail;
1318 1319          }
1319 1320          mutex_exit(&mpt->m_mutex);
1320 1321  
1321 1322          /*
1322 1323           * Initialize PHY info for smhba
1323 1324           */
1324 1325          if (mptsas_smhba_setup(mpt)) {
1325 1326                  mptsas_log(mpt, CE_WARN, "mptsas phy initialization "
1326 1327                      "failed");
1327 1328                  goto fail;
1328 1329          }
1329 1330  
1330 1331          /* Check all dma handles allocated in attach */
1331 1332          if ((mptsas_check_dma_handle(mpt->m_dma_req_frame_hdl)
1332 1333              != DDI_SUCCESS) ||
1333 1334              (mptsas_check_dma_handle(mpt->m_dma_reply_frame_hdl)
1334 1335              != DDI_SUCCESS) ||
1335 1336              (mptsas_check_dma_handle(mpt->m_dma_free_queue_hdl)
1336 1337              != DDI_SUCCESS) ||
1337 1338              (mptsas_check_dma_handle(mpt->m_dma_post_queue_hdl)
1338 1339              != DDI_SUCCESS) ||
1339 1340              (mptsas_check_dma_handle(mpt->m_hshk_dma_hdl)
1340 1341              != DDI_SUCCESS)) {
1341 1342                  goto fail;
1342 1343          }
1343 1344  
1344 1345          /* Check all acc handles allocated in attach */
1345 1346          if ((mptsas_check_acc_handle(mpt->m_datap) != DDI_SUCCESS) ||
1346 1347              (mptsas_check_acc_handle(mpt->m_acc_req_frame_hdl)
1347 1348              != DDI_SUCCESS) ||
1348 1349              (mptsas_check_acc_handle(mpt->m_acc_reply_frame_hdl)
1349 1350              != DDI_SUCCESS) ||
1350 1351              (mptsas_check_acc_handle(mpt->m_acc_free_queue_hdl)
1351 1352              != DDI_SUCCESS) ||
1352 1353              (mptsas_check_acc_handle(mpt->m_acc_post_queue_hdl)
1353 1354              != DDI_SUCCESS) ||
1354 1355              (mptsas_check_acc_handle(mpt->m_hshk_acc_hdl)
1355 1356              != DDI_SUCCESS) ||
1356 1357              (mptsas_check_acc_handle(mpt->m_config_handle)
1357 1358              != DDI_SUCCESS)) {
1358 1359                  goto fail;
1359 1360          }
1360 1361  
1361 1362          /*
1362 1363           * After this point, we are not going to fail the attach.
1363 1364           */
1364 1365          /*
1365 1366           * used for mptsas_watch
1366 1367           */
1367 1368          mptsas_list_add(mpt);
1368 1369  
1369 1370          mutex_enter(&mptsas_global_mutex);
1370 1371          if (mptsas_timeouts_enabled == 0) {
1371 1372                  mptsas_scsi_watchdog_tick = ddi_prop_get_int(DDI_DEV_T_ANY,
1372 1373                      dip, 0, "scsi-watchdog-tick", DEFAULT_WD_TICK);
1373 1374  
1374 1375                  mptsas_tick = mptsas_scsi_watchdog_tick *
1375 1376                      drv_usectohz((clock_t)1000000);
1376 1377  
1377 1378                  mptsas_timeout_id = timeout(mptsas_watch, NULL, mptsas_tick);
1378 1379                  mptsas_timeouts_enabled = 1;
1379 1380          }
1380 1381          mutex_exit(&mptsas_global_mutex);
1381 1382  
1382 1383          /* Print message of HBA present */
1383 1384          ddi_report_dev(dip);
1384 1385  
1385 1386          /* report idle status to pm framework */
1386 1387          if (mpt->m_options & MPTSAS_OPT_PM) {
1387 1388                  (void) pm_idle_component(dip, 0);
1388 1389          }
1389 1390  
1390 1391          return (DDI_SUCCESS);
1391 1392  
1392 1393  fail:
1393 1394          mptsas_log(mpt, CE_WARN, "attach failed");
1394 1395          mptsas_fm_ereport(mpt, DDI_FM_DEVICE_NO_RESPONSE);
1395 1396          ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_LOST);
1396 1397          if (mpt) {
1397 1398                  mutex_enter(&mptsas_global_mutex);
1398 1399  
1399 1400                  if (mptsas_timeout_id && (mptsas_head == NULL)) {
1400 1401                          timeout_id_t tid = mptsas_timeout_id;
1401 1402                          mptsas_timeouts_enabled = 0;
1402 1403                          mptsas_timeout_id = 0;
1403 1404                          mutex_exit(&mptsas_global_mutex);
1404 1405                          (void) untimeout(tid);
1405 1406                          mutex_enter(&mptsas_global_mutex);
1406 1407                  }
1407 1408                  mutex_exit(&mptsas_global_mutex);
1408 1409                  /* deallocate in reverse order */
1409 1410                  mptsas_cache_destroy(mpt);
1410 1411  
1411 1412                  if (smp_attach_setup) {
1412 1413                          mptsas_smp_teardown(mpt);
1413 1414                  }
1414 1415                  if (hba_attach_setup) {
1415 1416                          mptsas_hba_teardown(mpt);
1416 1417                  }
1417 1418  
1418 1419                  if (mpt->m_active) {
1419 1420                          mptsas_hash_uninit(&mpt->m_active->m_smptbl,
1420 1421                              sizeof (mptsas_smp_t));
1421 1422                          mptsas_hash_uninit(&mpt->m_active->m_tgttbl,
1422 1423                              sizeof (mptsas_target_t));
1423 1424                          mptsas_free_active_slots(mpt);
1424 1425                  }
1425 1426                  if (intr_added) {
1426 1427                          mptsas_unregister_intrs(mpt);
1427 1428                  }
1428 1429  
1429 1430                  if (doneq_thread_create) {
1430 1431                          mutex_enter(&mpt->m_doneq_mutex);
1431 1432                          doneq_thread_num = mpt->m_doneq_thread_n;
1432 1433                          for (j = 0; j < mpt->m_doneq_thread_n; j++) {
1433 1434                                  mutex_enter(&mpt->m_doneq_thread_id[j].mutex);
1434 1435                                  mpt->m_doneq_thread_id[j].flag &=
1435 1436                                      (~MPTSAS_DONEQ_THREAD_ACTIVE);
1436 1437                                  cv_signal(&mpt->m_doneq_thread_id[j].cv);
1437 1438                                  mutex_exit(&mpt->m_doneq_thread_id[j].mutex);
1438 1439                          }
1439 1440                          while (mpt->m_doneq_thread_n) {
1440 1441                                  cv_wait(&mpt->m_doneq_thread_cv,
1441 1442                                      &mpt->m_doneq_mutex);
1442 1443                          }
1443 1444                          for (j = 0; j < doneq_thread_num; j++) {
1444 1445                                  cv_destroy(&mpt->m_doneq_thread_id[j].cv);
1445 1446                                  mutex_destroy(&mpt->m_doneq_thread_id[j].mutex);
1446 1447                          }
1447 1448                          kmem_free(mpt->m_doneq_thread_id,
1448 1449                              sizeof (mptsas_doneq_thread_list_t)
1449 1450                              * doneq_thread_num);
1450 1451                          mutex_exit(&mpt->m_doneq_mutex);
1451 1452                          cv_destroy(&mpt->m_doneq_thread_cv);
1452 1453                          mutex_destroy(&mpt->m_doneq_mutex);
1453 1454                  }
1454 1455                  if (event_taskq_create) {
1455 1456                          ddi_taskq_destroy(mpt->m_event_taskq);
1456 1457                  }
1457 1458                  if (dr_taskq_create) {
1458 1459                          ddi_taskq_destroy(mpt->m_dr_taskq);
1459 1460                  }
1460 1461                  if (mutex_init_done) {
1461 1462                          mutex_destroy(&mpt->m_intr_mutex);
1462 1463                          mutex_destroy(&mpt->m_passthru_mutex);
1463 1464                          mutex_destroy(&mpt->m_mutex);
1464 1465                          for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
1465 1466                                  mutex_destroy(
1466 1467                                      &mpt->m_phy_info[i].smhba_info.phy_mutex);
1467 1468                          }
1468 1469                          cv_destroy(&mpt->m_cv);
1469 1470                          cv_destroy(&mpt->m_passthru_cv);
1470 1471                          cv_destroy(&mpt->m_fw_cv);
1471 1472                          cv_destroy(&mpt->m_config_cv);
1472 1473                          cv_destroy(&mpt->m_fw_diag_cv);
1473 1474                  }
1474 1475  
1475 1476                  if (map_setup) {
1476 1477                          mptsas_cfg_fini(mpt);
1477 1478                  }
1478 1479                  if (config_setup) {
1479 1480                          mptsas_config_space_fini(mpt);
1480 1481                  }
1481 1482                  mptsas_free_handshake_msg(mpt);
1482 1483                  mptsas_hba_fini(mpt);
1483 1484  
1484 1485                  mptsas_fm_fini(mpt);
1485 1486                  ddi_soft_state_free(mptsas_state, instance);
1486 1487                  ddi_prop_remove_all(dip);
1487 1488          }
1488 1489          return (DDI_FAILURE);
1489 1490  }
1490 1491  
1491 1492  static int
1492 1493  mptsas_suspend(dev_info_t *devi)
1493 1494  {
1494 1495          mptsas_t        *mpt, *g;
1495 1496          scsi_hba_tran_t *tran;
1496 1497  
1497 1498          if (scsi_hba_iport_unit_address(devi)) {
1498 1499                  return (DDI_SUCCESS);
1499 1500          }
1500 1501  
1501 1502          if ((tran = ddi_get_driver_private(devi)) == NULL)
1502 1503                  return (DDI_SUCCESS);
1503 1504  
1504 1505          mpt = TRAN2MPT(tran);
1505 1506          if (!mpt) {
1506 1507                  return (DDI_SUCCESS);
1507 1508          }
1508 1509  
1509 1510          mutex_enter(&mpt->m_mutex);
1510 1511  
1511 1512          if (mpt->m_suspended++) {
1512 1513                  mutex_exit(&mpt->m_mutex);
1513 1514                  return (DDI_SUCCESS);
1514 1515          }
1515 1516  
1516 1517          /*
1517 1518           * Cancel timeout threads for this mpt
1518 1519           */
1519 1520          if (mpt->m_quiesce_timeid) {
1520 1521                  timeout_id_t tid = mpt->m_quiesce_timeid;
1521 1522                  mpt->m_quiesce_timeid = 0;
1522 1523                  mutex_exit(&mpt->m_mutex);
1523 1524                  (void) untimeout(tid);
1524 1525                  mutex_enter(&mpt->m_mutex);
1525 1526          }
1526 1527  
1527 1528          if (mpt->m_restart_cmd_timeid) {
1528 1529                  timeout_id_t tid = mpt->m_restart_cmd_timeid;
1529 1530                  mpt->m_restart_cmd_timeid = 0;
1530 1531                  mutex_exit(&mpt->m_mutex);
1531 1532                  (void) untimeout(tid);
1532 1533                  mutex_enter(&mpt->m_mutex);
1533 1534          }
1534 1535  
1535 1536          mutex_exit(&mpt->m_mutex);
1536 1537  
1537 1538          (void) pm_idle_component(mpt->m_dip, 0);
1538 1539  
1539 1540          /*
1540 1541           * Cancel watch threads if all mpts suspended
1541 1542           */
1542 1543          rw_enter(&mptsas_global_rwlock, RW_WRITER);
1543 1544          for (g = mptsas_head; g != NULL; g = g->m_next) {
1544 1545                  if (!g->m_suspended)
1545 1546                          break;
1546 1547          }
1547 1548          rw_exit(&mptsas_global_rwlock);
1548 1549  
1549 1550          mutex_enter(&mptsas_global_mutex);
1550 1551          if (g == NULL) {
1551 1552                  timeout_id_t tid;
1552 1553  
1553 1554                  mptsas_timeouts_enabled = 0;
1554 1555                  if (mptsas_timeout_id) {
1555 1556                          tid = mptsas_timeout_id;
1556 1557                          mptsas_timeout_id = 0;
1557 1558                          mutex_exit(&mptsas_global_mutex);
1558 1559                          (void) untimeout(tid);
1559 1560                          mutex_enter(&mptsas_global_mutex);
1560 1561                  }
1561 1562                  if (mptsas_reset_watch) {
1562 1563                          tid = mptsas_reset_watch;
1563 1564                          mptsas_reset_watch = 0;
1564 1565                          mutex_exit(&mptsas_global_mutex);
1565 1566                          (void) untimeout(tid);
1566 1567                          mutex_enter(&mptsas_global_mutex);
1567 1568                  }
1568 1569          }
1569 1570          mutex_exit(&mptsas_global_mutex);
1570 1571  
1571 1572          mutex_enter(&mpt->m_mutex);
1572 1573  
1573 1574          /*
1574 1575           * If this mpt is not in full power(PM_LEVEL_D0), just return.
1575 1576           */
1576 1577          if ((mpt->m_options & MPTSAS_OPT_PM) &&
1577 1578              (mpt->m_power_level != PM_LEVEL_D0)) {
1578 1579                  mutex_exit(&mpt->m_mutex);
1579 1580                  return (DDI_SUCCESS);
1580 1581          }
1581 1582  
1582 1583          /* Disable HBA interrupts in hardware */
1583 1584          MPTSAS_DISABLE_INTR(mpt);
1584 1585          /*
1585 1586           * Send RAID action system shutdown to sync IR
1586 1587           */
1587 1588          mptsas_raid_action_system_shutdown(mpt);
1588 1589  
1589 1590          mutex_exit(&mpt->m_mutex);
1590 1591  
1591 1592          /* drain the taskq */
1592 1593          ddi_taskq_wait(mpt->m_event_taskq);
1593 1594          ddi_taskq_wait(mpt->m_dr_taskq);
1594 1595  
1595 1596          return (DDI_SUCCESS);
1596 1597  }
1597 1598  
1598 1599  #ifdef  __sparc
1599 1600  /*ARGSUSED*/
1600 1601  static int
1601 1602  mptsas_reset(dev_info_t *devi, ddi_reset_cmd_t cmd)
1602 1603  {
1603 1604          mptsas_t        *mpt;
1604 1605          scsi_hba_tran_t *tran;
1605 1606  
1606 1607          /*
1607 1608           * If this call is for iport, just return.
1608 1609           */
1609 1610          if (scsi_hba_iport_unit_address(devi))
1610 1611                  return (DDI_SUCCESS);
1611 1612  
1612 1613          if ((tran = ddi_get_driver_private(devi)) == NULL)
1613 1614                  return (DDI_SUCCESS);
1614 1615  
1615 1616          if ((mpt = TRAN2MPT(tran)) == NULL)
1616 1617                  return (DDI_SUCCESS);
1617 1618  
1618 1619          /*
1619 1620           * Send RAID action system shutdown to sync IR.  Disable HBA
1620 1621           * interrupts in hardware first.
1621 1622           */
1622 1623          MPTSAS_DISABLE_INTR(mpt);
1623 1624          mptsas_raid_action_system_shutdown(mpt);
1624 1625  
1625 1626          return (DDI_SUCCESS);
1626 1627  }
1627 1628  #else /* __sparc */
1628 1629  /*
1629 1630   * quiesce(9E) entry point.
1630 1631   *
1631 1632   * This function is called when the system is single-threaded at high
1632 1633   * PIL with preemption disabled. Therefore, this function must not be
1633 1634   * blocked.
1634 1635   *
1635 1636   * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
1636 1637   * DDI_FAILURE indicates an error condition and should almost never happen.
1637 1638   */
1638 1639  static int
1639 1640  mptsas_quiesce(dev_info_t *devi)
1640 1641  {
1641 1642          mptsas_t        *mpt;
1642 1643          scsi_hba_tran_t *tran;
1643 1644  
1644 1645          /*
1645 1646           * If this call is for iport, just return.
1646 1647           */
1647 1648          if (scsi_hba_iport_unit_address(devi))
1648 1649                  return (DDI_SUCCESS);
1649 1650  
1650 1651          if ((tran = ddi_get_driver_private(devi)) == NULL)
1651 1652                  return (DDI_SUCCESS);
1652 1653  
1653 1654          if ((mpt = TRAN2MPT(tran)) == NULL)
1654 1655                  return (DDI_SUCCESS);
1655 1656  
1656 1657          /* Disable HBA interrupts in hardware */
1657 1658          MPTSAS_DISABLE_INTR(mpt);
1658 1659          /* Send RAID action system shutdonw to sync IR */
1659 1660          mptsas_raid_action_system_shutdown(mpt);
1660 1661  
1661 1662          return (DDI_SUCCESS);
1662 1663  }
1663 1664  #endif  /* __sparc */
1664 1665  
1665 1666  /*
1666 1667   * detach(9E).  Remove all device allocations and system resources;
1667 1668   * disable device interrupts.
1668 1669   * Return DDI_SUCCESS if done; DDI_FAILURE if there's a problem.
1669 1670   */
1670 1671  static int
1671 1672  mptsas_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
1672 1673  {
1673 1674          /* CONSTCOND */
1674 1675          ASSERT(NO_COMPETING_THREADS);
1675 1676          NDBG0(("mptsas_detach: dip=0x%p cmd=0x%p", (void *)devi, (void *)cmd));
1676 1677  
1677 1678          switch (cmd) {
1678 1679          case DDI_DETACH:
1679 1680                  return (mptsas_do_detach(devi));
1680 1681  
1681 1682          case DDI_SUSPEND:
1682 1683                  return (mptsas_suspend(devi));
1683 1684  
1684 1685          default:
1685 1686                  return (DDI_FAILURE);
1686 1687          }
1687 1688          /* NOTREACHED */
1688 1689  }
1689 1690  
1690 1691  static int
1691 1692  mptsas_do_detach(dev_info_t *dip)
1692 1693  {
1693 1694          mptsas_t        *mpt;
1694 1695          scsi_hba_tran_t *tran;
1695 1696          int             circ = 0;
1696 1697          int             circ1 = 0;
1697 1698          mdi_pathinfo_t  *pip = NULL;
1698 1699          int             i;
1699 1700          int             doneq_thread_num = 0;
1700 1701  
1701 1702          NDBG0(("mptsas_do_detach: dip=0x%p", (void *)dip));
1702 1703  
1703 1704          if ((tran = ndi_flavorv_get(dip, SCSA_FLAVOR_SCSI_DEVICE)) == NULL)
1704 1705                  return (DDI_FAILURE);
1705 1706  
1706 1707          mpt = TRAN2MPT(tran);
1707 1708          if (!mpt) {
1708 1709                  return (DDI_FAILURE);
1709 1710          }
1710 1711          /*
1711 1712           * Still have pathinfo child, should not detach mpt driver
1712 1713           */
1713 1714          if (scsi_hba_iport_unit_address(dip)) {
1714 1715                  if (mpt->m_mpxio_enable) {
1715 1716                          /*
1716 1717                           * MPxIO enabled for the iport
1717 1718                           */
1718 1719                          ndi_devi_enter(scsi_vhci_dip, &circ1);
1719 1720                          ndi_devi_enter(dip, &circ);
1720 1721                          while (pip = mdi_get_next_client_path(dip, NULL)) {
1721 1722                                  if (mdi_pi_free(pip, 0) == MDI_SUCCESS) {
1722 1723                                          continue;
1723 1724                                  }
1724 1725                                  ndi_devi_exit(dip, circ);
1725 1726                                  ndi_devi_exit(scsi_vhci_dip, circ1);
1726 1727                                  NDBG12(("detach failed because of "
1727 1728                                      "outstanding path info"));
1728 1729                                  return (DDI_FAILURE);
1729 1730                          }
1730 1731                          ndi_devi_exit(dip, circ);
1731 1732                          ndi_devi_exit(scsi_vhci_dip, circ1);
1732 1733                          (void) mdi_phci_unregister(dip, 0);
1733 1734                  }
1734 1735  
1735 1736                  ddi_prop_remove_all(dip);
1736 1737  
1737 1738                  return (DDI_SUCCESS);
1738 1739          }
1739 1740  
1740 1741          /* Make sure power level is D0 before accessing registers */
1741 1742          if (mpt->m_options & MPTSAS_OPT_PM) {
1742 1743                  (void) pm_busy_component(dip, 0);
1743 1744                  if (mpt->m_power_level != PM_LEVEL_D0) {
1744 1745                          if (pm_raise_power(dip, 0, PM_LEVEL_D0) !=
1745 1746                              DDI_SUCCESS) {
1746 1747                                  mptsas_log(mpt, CE_WARN,
1747 1748                                      "mptsas%d: Raise power request failed.",
1748 1749                                      mpt->m_instance);
1749 1750                                  (void) pm_idle_component(dip, 0);
1750 1751                                  return (DDI_FAILURE);
1751 1752                          }
1752 1753                  }
1753 1754          }
  
    | 
      ↓ open down ↓ | 
    1720 lines elided | 
    
      ↑ open up ↑ | 
  
1754 1755  
1755 1756          /*
1756 1757           * Send RAID action system shutdown to sync IR.  After action, send a
1757 1758           * Message Unit Reset. Since after that DMA resource will be freed,
1758 1759           * set ioc to READY state will avoid HBA initiated DMA operation.
1759 1760           */
1760 1761          mutex_enter(&mpt->m_mutex);
1761 1762          MPTSAS_DISABLE_INTR(mpt);
1762 1763          mptsas_raid_action_system_shutdown(mpt);
1763 1764          mpt->m_softstate |= MPTSAS_SS_MSG_UNIT_RESET;
1764      -        (void) mptsas_ioc_reset(mpt);
     1765 +        (void) mptsas_ioc_reset(mpt, FALSE);
1765 1766          mutex_exit(&mpt->m_mutex);
1766 1767          mptsas_rem_intrs(mpt);
1767 1768          ddi_taskq_destroy(mpt->m_event_taskq);
1768 1769          ddi_taskq_destroy(mpt->m_dr_taskq);
1769 1770  
1770 1771          if (mpt->m_doneq_thread_n) {
1771 1772                  mutex_enter(&mpt->m_doneq_mutex);
1772 1773                  doneq_thread_num = mpt->m_doneq_thread_n;
1773 1774                  for (i = 0; i < mpt->m_doneq_thread_n; i++) {
1774 1775                          mutex_enter(&mpt->m_doneq_thread_id[i].mutex);
1775 1776                          mpt->m_doneq_thread_id[i].flag &=
1776 1777                              (~MPTSAS_DONEQ_THREAD_ACTIVE);
1777 1778                          cv_signal(&mpt->m_doneq_thread_id[i].cv);
1778 1779                          mutex_exit(&mpt->m_doneq_thread_id[i].mutex);
1779 1780                  }
1780 1781                  while (mpt->m_doneq_thread_n) {
1781 1782                          cv_wait(&mpt->m_doneq_thread_cv,
1782 1783                              &mpt->m_doneq_mutex);
1783 1784                  }
1784 1785                  for (i = 0;  i < doneq_thread_num; i++) {
1785 1786                          cv_destroy(&mpt->m_doneq_thread_id[i].cv);
1786 1787                          mutex_destroy(&mpt->m_doneq_thread_id[i].mutex);
1787 1788                  }
1788 1789                  kmem_free(mpt->m_doneq_thread_id,
1789 1790                      sizeof (mptsas_doneq_thread_list_t)
1790 1791                      * doneq_thread_num);
1791 1792                  mutex_exit(&mpt->m_doneq_mutex);
1792 1793                  cv_destroy(&mpt->m_doneq_thread_cv);
1793 1794                  mutex_destroy(&mpt->m_doneq_mutex);
1794 1795          }
1795 1796  
1796 1797          scsi_hba_reset_notify_tear_down(mpt->m_reset_notify_listf);
1797 1798  
1798 1799          mptsas_list_del(mpt);
1799 1800  
1800 1801          /*
1801 1802           * Cancel timeout threads for this mpt
1802 1803           */
1803 1804          mutex_enter(&mpt->m_mutex);
1804 1805          if (mpt->m_quiesce_timeid) {
1805 1806                  timeout_id_t tid = mpt->m_quiesce_timeid;
1806 1807                  mpt->m_quiesce_timeid = 0;
1807 1808                  mutex_exit(&mpt->m_mutex);
1808 1809                  (void) untimeout(tid);
1809 1810                  mutex_enter(&mpt->m_mutex);
1810 1811          }
1811 1812  
1812 1813          if (mpt->m_restart_cmd_timeid) {
1813 1814                  timeout_id_t tid = mpt->m_restart_cmd_timeid;
1814 1815                  mpt->m_restart_cmd_timeid = 0;
1815 1816                  mutex_exit(&mpt->m_mutex);
1816 1817                  (void) untimeout(tid);
1817 1818                  mutex_enter(&mpt->m_mutex);
1818 1819          }
1819 1820  
1820 1821          mutex_exit(&mpt->m_mutex);
1821 1822  
1822 1823          /*
1823 1824           * last mpt? ... if active, CANCEL watch threads.
1824 1825           */
1825 1826          mutex_enter(&mptsas_global_mutex);
1826 1827          if (mptsas_head == NULL) {
1827 1828                  timeout_id_t tid;
1828 1829                  /*
1829 1830                   * Clear mptsas_timeouts_enable so that the watch thread
1830 1831                   * gets restarted on DDI_ATTACH
1831 1832                   */
1832 1833                  mptsas_timeouts_enabled = 0;
1833 1834                  if (mptsas_timeout_id) {
1834 1835                          tid = mptsas_timeout_id;
1835 1836                          mptsas_timeout_id = 0;
1836 1837                          mutex_exit(&mptsas_global_mutex);
1837 1838                          (void) untimeout(tid);
1838 1839                          mutex_enter(&mptsas_global_mutex);
1839 1840                  }
1840 1841                  if (mptsas_reset_watch) {
1841 1842                          tid = mptsas_reset_watch;
1842 1843                          mptsas_reset_watch = 0;
1843 1844                          mutex_exit(&mptsas_global_mutex);
1844 1845                          (void) untimeout(tid);
1845 1846                          mutex_enter(&mptsas_global_mutex);
1846 1847                  }
1847 1848          }
1848 1849          mutex_exit(&mptsas_global_mutex);
1849 1850  
1850 1851          /*
1851 1852           * Delete Phy stats
1852 1853           */
1853 1854          mptsas_destroy_phy_stats(mpt);
1854 1855  
1855 1856          /*
1856 1857           * Delete nt_active.
1857 1858           */
1858 1859          mutex_enter(&mpt->m_mutex);
1859 1860          mptsas_hash_uninit(&mpt->m_active->m_tgttbl, sizeof (mptsas_target_t));
1860 1861          mptsas_hash_uninit(&mpt->m_active->m_smptbl, sizeof (mptsas_smp_t));
1861 1862          mptsas_free_active_slots(mpt);
1862 1863          mutex_exit(&mpt->m_mutex);
1863 1864  
1864 1865          /* deallocate everything that was allocated in mptsas_attach */
1865 1866          mptsas_cache_destroy(mpt);
1866 1867  
1867 1868          mptsas_hba_fini(mpt);
1868 1869          mptsas_cfg_fini(mpt);
1869 1870  
1870 1871          /* Lower the power informing PM Framework */
1871 1872          if (mpt->m_options & MPTSAS_OPT_PM) {
1872 1873                  if (pm_lower_power(dip, 0, PM_LEVEL_D3) != DDI_SUCCESS)
1873 1874                          mptsas_log(mpt, CE_WARN,
1874 1875                              "!mptsas%d: Lower power request failed "
1875 1876                              "during detach, ignoring.",
1876 1877                              mpt->m_instance);
1877 1878          }
1878 1879  
1879 1880          mutex_destroy(&mpt->m_intr_mutex);
1880 1881          mutex_destroy(&mpt->m_passthru_mutex);
1881 1882          mutex_destroy(&mpt->m_mutex);
1882 1883          for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
1883 1884                  mutex_destroy(&mpt->m_phy_info[i].smhba_info.phy_mutex);
1884 1885          }
1885 1886          cv_destroy(&mpt->m_cv);
1886 1887          cv_destroy(&mpt->m_passthru_cv);
1887 1888          cv_destroy(&mpt->m_fw_cv);
1888 1889          cv_destroy(&mpt->m_config_cv);
1889 1890          cv_destroy(&mpt->m_fw_diag_cv);
1890 1891  
1891 1892  
1892 1893          mptsas_smp_teardown(mpt);
1893 1894          mptsas_hba_teardown(mpt);
1894 1895  
1895 1896          mptsas_config_space_fini(mpt);
1896 1897  
1897 1898          mptsas_free_handshake_msg(mpt);
1898 1899  
1899 1900          mptsas_fm_fini(mpt);
1900 1901          ddi_soft_state_free(mptsas_state, ddi_get_instance(dip));
1901 1902          ddi_prop_remove_all(dip);
1902 1903  
1903 1904          return (DDI_SUCCESS);
1904 1905  }
1905 1906  
1906 1907  static void
1907 1908  mptsas_list_add(mptsas_t *mpt)
1908 1909  {
1909 1910          rw_enter(&mptsas_global_rwlock, RW_WRITER);
1910 1911  
1911 1912          if (mptsas_head == NULL) {
1912 1913                  mptsas_head = mpt;
1913 1914          } else {
1914 1915                  mptsas_tail->m_next = mpt;
1915 1916          }
1916 1917          mptsas_tail = mpt;
1917 1918          rw_exit(&mptsas_global_rwlock);
1918 1919  }
1919 1920  
1920 1921  static void
1921 1922  mptsas_list_del(mptsas_t *mpt)
1922 1923  {
1923 1924          mptsas_t *m;
1924 1925          /*
1925 1926           * Remove device instance from the global linked list
1926 1927           */
1927 1928          rw_enter(&mptsas_global_rwlock, RW_WRITER);
1928 1929          if (mptsas_head == mpt) {
1929 1930                  m = mptsas_head = mpt->m_next;
1930 1931          } else {
1931 1932                  for (m = mptsas_head; m != NULL; m = m->m_next) {
1932 1933                          if (m->m_next == mpt) {
1933 1934                                  m->m_next = mpt->m_next;
1934 1935                                  break;
1935 1936                          }
1936 1937                  }
1937 1938                  if (m == NULL) {
1938 1939                          mptsas_log(mpt, CE_PANIC, "Not in softc list!");
1939 1940                  }
1940 1941          }
1941 1942  
1942 1943          if (mptsas_tail == mpt) {
1943 1944                  mptsas_tail = m;
1944 1945          }
1945 1946          rw_exit(&mptsas_global_rwlock);
1946 1947  }
1947 1948  
1948 1949  static int
1949 1950  mptsas_alloc_handshake_msg(mptsas_t *mpt, size_t alloc_size)
1950 1951  {
1951 1952          ddi_dma_attr_t  task_dma_attrs;
1952 1953  
1953 1954          task_dma_attrs = mpt->m_msg_dma_attr;
1954 1955          task_dma_attrs.dma_attr_sgllen = 1;
1955 1956          task_dma_attrs.dma_attr_granular = (uint32_t)(alloc_size);
1956 1957  
1957 1958          /* allocate Task Management ddi_dma resources */
1958 1959          if (mptsas_dma_addr_create(mpt, task_dma_attrs,
1959 1960              &mpt->m_hshk_dma_hdl, &mpt->m_hshk_acc_hdl, &mpt->m_hshk_memp,
1960 1961              alloc_size, NULL) == FALSE) {
1961 1962                  return (DDI_FAILURE);
1962 1963          }
1963 1964          mpt->m_hshk_dma_size = alloc_size;
1964 1965  
1965 1966          return (DDI_SUCCESS);
1966 1967  }
1967 1968  
1968 1969  static void
1969 1970  mptsas_free_handshake_msg(mptsas_t *mpt)
1970 1971  {
1971 1972          mptsas_dma_addr_destroy(&mpt->m_hshk_dma_hdl, &mpt->m_hshk_acc_hdl);
1972 1973          mpt->m_hshk_dma_size = 0;
1973 1974  }
1974 1975  
1975 1976  static int
1976 1977  mptsas_hba_setup(mptsas_t *mpt)
1977 1978  {
1978 1979          scsi_hba_tran_t         *hba_tran;
1979 1980          int                     tran_flags;
1980 1981  
1981 1982          /* Allocate a transport structure */
1982 1983          hba_tran = mpt->m_tran = scsi_hba_tran_alloc(mpt->m_dip,
1983 1984              SCSI_HBA_CANSLEEP);
1984 1985          ASSERT(mpt->m_tran != NULL);
1985 1986  
1986 1987          hba_tran->tran_hba_private      = mpt;
1987 1988          hba_tran->tran_tgt_private      = NULL;
1988 1989  
1989 1990          hba_tran->tran_tgt_init         = mptsas_scsi_tgt_init;
1990 1991          hba_tran->tran_tgt_free         = mptsas_scsi_tgt_free;
1991 1992  
1992 1993          hba_tran->tran_start            = mptsas_scsi_start;
1993 1994          hba_tran->tran_reset            = mptsas_scsi_reset;
1994 1995          hba_tran->tran_abort            = mptsas_scsi_abort;
1995 1996          hba_tran->tran_getcap           = mptsas_scsi_getcap;
1996 1997          hba_tran->tran_setcap           = mptsas_scsi_setcap;
1997 1998          hba_tran->tran_init_pkt         = mptsas_scsi_init_pkt;
1998 1999          hba_tran->tran_destroy_pkt      = mptsas_scsi_destroy_pkt;
1999 2000  
2000 2001          hba_tran->tran_dmafree          = mptsas_scsi_dmafree;
2001 2002          hba_tran->tran_sync_pkt         = mptsas_scsi_sync_pkt;
2002 2003          hba_tran->tran_reset_notify     = mptsas_scsi_reset_notify;
2003 2004  
2004 2005          hba_tran->tran_get_bus_addr     = mptsas_get_bus_addr;
2005 2006          hba_tran->tran_get_name         = mptsas_get_name;
2006 2007  
2007 2008          hba_tran->tran_quiesce          = mptsas_scsi_quiesce;
2008 2009          hba_tran->tran_unquiesce        = mptsas_scsi_unquiesce;
2009 2010          hba_tran->tran_bus_reset        = NULL;
2010 2011  
2011 2012          hba_tran->tran_add_eventcall    = NULL;
2012 2013          hba_tran->tran_get_eventcookie  = NULL;
2013 2014          hba_tran->tran_post_event       = NULL;
2014 2015          hba_tran->tran_remove_eventcall = NULL;
2015 2016  
2016 2017          hba_tran->tran_bus_config       = mptsas_bus_config;
2017 2018  
2018 2019          hba_tran->tran_interconnect_type = INTERCONNECT_SAS;
2019 2020  
2020 2021          /*
2021 2022           * All children of the HBA are iports. We need tran was cloned.
2022 2023           * So we pass the flags to SCSA. SCSI_HBA_TRAN_CLONE will be
2023 2024           * inherited to iport's tran vector.
2024 2025           */
2025 2026          tran_flags = (SCSI_HBA_HBA | SCSI_HBA_TRAN_CLONE);
2026 2027  
2027 2028          if (scsi_hba_attach_setup(mpt->m_dip, &mpt->m_msg_dma_attr,
2028 2029              hba_tran, tran_flags) != DDI_SUCCESS) {
2029 2030                  mptsas_log(mpt, CE_WARN, "hba attach setup failed");
2030 2031                  scsi_hba_tran_free(hba_tran);
2031 2032                  mpt->m_tran = NULL;
2032 2033                  return (FALSE);
2033 2034          }
2034 2035          return (TRUE);
2035 2036  }
2036 2037  
2037 2038  static void
2038 2039  mptsas_hba_teardown(mptsas_t *mpt)
2039 2040  {
2040 2041          (void) scsi_hba_detach(mpt->m_dip);
2041 2042          if (mpt->m_tran != NULL) {
2042 2043                  scsi_hba_tran_free(mpt->m_tran);
2043 2044                  mpt->m_tran = NULL;
2044 2045          }
2045 2046  }
2046 2047  
2047 2048  static void
2048 2049  mptsas_iport_register(mptsas_t *mpt)
2049 2050  {
2050 2051          int i, j;
2051 2052          mptsas_phymask_t        mask = 0x0;
2052 2053          /*
2053 2054           * initial value of mask is 0
2054 2055           */
2055 2056          mutex_enter(&mpt->m_mutex);
2056 2057          for (i = 0; i < mpt->m_num_phys; i++) {
2057 2058                  mptsas_phymask_t phy_mask = 0x0;
2058 2059                  char phy_mask_name[MPTSAS_MAX_PHYS];
2059 2060                  uint8_t current_port;
2060 2061  
2061 2062                  if (mpt->m_phy_info[i].attached_devhdl == 0)
2062 2063                          continue;
2063 2064  
2064 2065                  bzero(phy_mask_name, sizeof (phy_mask_name));
2065 2066  
2066 2067                  current_port = mpt->m_phy_info[i].port_num;
2067 2068  
2068 2069                  if ((mask & (1 << i)) != 0)
2069 2070                          continue;
2070 2071  
2071 2072                  for (j = 0; j < mpt->m_num_phys; j++) {
2072 2073                          if (mpt->m_phy_info[j].attached_devhdl &&
2073 2074                              (mpt->m_phy_info[j].port_num == current_port)) {
2074 2075                                  phy_mask |= (1 << j);
2075 2076                          }
2076 2077                  }
2077 2078                  mask = mask | phy_mask;
2078 2079  
2079 2080                  for (j = 0; j < mpt->m_num_phys; j++) {
2080 2081                          if ((phy_mask >> j) & 0x01) {
2081 2082                                  mpt->m_phy_info[j].phy_mask = phy_mask;
2082 2083                          }
2083 2084                  }
2084 2085  
2085 2086                  (void) sprintf(phy_mask_name, "%x", phy_mask);
2086 2087  
2087 2088                  mutex_exit(&mpt->m_mutex);
2088 2089                  /*
2089 2090                   * register a iport
2090 2091                   */
2091 2092                  (void) scsi_hba_iport_register(mpt->m_dip, phy_mask_name);
2092 2093                  mutex_enter(&mpt->m_mutex);
2093 2094          }
2094 2095          mutex_exit(&mpt->m_mutex);
2095 2096          /*
2096 2097           * register a virtual port for RAID volume always
2097 2098           */
2098 2099          (void) scsi_hba_iport_register(mpt->m_dip, "v0");
2099 2100  
2100 2101  }
2101 2102  
2102 2103  static int
2103 2104  mptsas_smp_setup(mptsas_t *mpt)
2104 2105  {
2105 2106          mpt->m_smptran = smp_hba_tran_alloc(mpt->m_dip);
2106 2107          ASSERT(mpt->m_smptran != NULL);
2107 2108          mpt->m_smptran->smp_tran_hba_private = mpt;
2108 2109          mpt->m_smptran->smp_tran_start = mptsas_smp_start;
2109 2110          if (smp_hba_attach_setup(mpt->m_dip, mpt->m_smptran) != DDI_SUCCESS) {
2110 2111                  mptsas_log(mpt, CE_WARN, "smp attach setup failed");
2111 2112                  smp_hba_tran_free(mpt->m_smptran);
2112 2113                  mpt->m_smptran = NULL;
2113 2114                  return (FALSE);
2114 2115          }
2115 2116          /*
2116 2117           * Initialize smp hash table
2117 2118           */
2118 2119          mptsas_hash_init(&mpt->m_active->m_smptbl);
2119 2120          mpt->m_smp_devhdl = 0xFFFF;
2120 2121  
2121 2122          return (TRUE);
2122 2123  }
2123 2124  
2124 2125  static void
2125 2126  mptsas_smp_teardown(mptsas_t *mpt)
2126 2127  {
2127 2128          (void) smp_hba_detach(mpt->m_dip);
2128 2129          if (mpt->m_smptran != NULL) {
2129 2130                  smp_hba_tran_free(mpt->m_smptran);
2130 2131                  mpt->m_smptran = NULL;
2131 2132          }
2132 2133          mpt->m_smp_devhdl = 0;
2133 2134  }
2134 2135  
2135 2136  static int
2136 2137  mptsas_cache_create(mptsas_t *mpt)
2137 2138  {
2138 2139          int instance = mpt->m_instance;
2139 2140          char buf[64];
2140 2141  
2141 2142          /*
2142 2143           * create kmem cache for packets
2143 2144           */
2144 2145          (void) sprintf(buf, "mptsas%d_cache", instance);
2145 2146          mpt->m_kmem_cache = kmem_cache_create(buf,
2146 2147              sizeof (struct mptsas_cmd) + scsi_pkt_size(), 8,
2147 2148              mptsas_kmem_cache_constructor, mptsas_kmem_cache_destructor,
2148 2149              NULL, (void *)mpt, NULL, 0);
2149 2150  
2150 2151          if (mpt->m_kmem_cache == NULL) {
2151 2152                  mptsas_log(mpt, CE_WARN, "creating kmem cache failed");
2152 2153                  return (FALSE);
2153 2154          }
2154 2155  
2155 2156          /*
2156 2157           * create kmem cache for extra SGL frames if SGL cannot
2157 2158           * be accomodated into main request frame.
2158 2159           */
2159 2160          (void) sprintf(buf, "mptsas%d_cache_frames", instance);
2160 2161          mpt->m_cache_frames = kmem_cache_create(buf,
2161 2162              sizeof (mptsas_cache_frames_t), 8,
2162 2163              mptsas_cache_frames_constructor, mptsas_cache_frames_destructor,
2163 2164              NULL, (void *)mpt, NULL, 0);
2164 2165  
2165 2166          if (mpt->m_cache_frames == NULL) {
2166 2167                  mptsas_log(mpt, CE_WARN, "creating cache for frames failed");
2167 2168                  return (FALSE);
2168 2169          }
2169 2170  
2170 2171          return (TRUE);
2171 2172  }
2172 2173  
2173 2174  static void
2174 2175  mptsas_cache_destroy(mptsas_t *mpt)
2175 2176  {
2176 2177          /* deallocate in reverse order */
2177 2178          if (mpt->m_cache_frames) {
2178 2179                  kmem_cache_destroy(mpt->m_cache_frames);
2179 2180                  mpt->m_cache_frames = NULL;
2180 2181          }
2181 2182          if (mpt->m_kmem_cache) {
2182 2183                  kmem_cache_destroy(mpt->m_kmem_cache);
2183 2184                  mpt->m_kmem_cache = NULL;
2184 2185          }
2185 2186  }
2186 2187  
2187 2188  static int
2188 2189  mptsas_power(dev_info_t *dip, int component, int level)
2189 2190  {
2190 2191  #ifndef __lock_lint
2191 2192          _NOTE(ARGUNUSED(component))
2192 2193  #endif
2193 2194          mptsas_t        *mpt;
2194 2195          int             rval = DDI_SUCCESS;
2195 2196          int             polls = 0;
2196 2197          uint32_t        ioc_status;
2197 2198  
2198 2199          if (scsi_hba_iport_unit_address(dip) != 0)
2199 2200                  return (DDI_SUCCESS);
2200 2201  
2201 2202          mpt = ddi_get_soft_state(mptsas_state, ddi_get_instance(dip));
2202 2203          if (mpt == NULL) {
2203 2204                  return (DDI_FAILURE);
2204 2205          }
2205 2206  
2206 2207          mutex_enter(&mpt->m_mutex);
2207 2208  
2208 2209          /*
2209 2210           * If the device is busy, don't lower its power level
2210 2211           */
2211 2212          if (mpt->m_busy && (mpt->m_power_level > level)) {
2212 2213                  mutex_exit(&mpt->m_mutex);
2213 2214                  return (DDI_FAILURE);
2214 2215          }
2215 2216          switch (level) {
2216 2217          case PM_LEVEL_D0:
2217 2218                  NDBG11(("mptsas%d: turning power ON.", mpt->m_instance));
2218 2219                  MPTSAS_POWER_ON(mpt);
2219 2220                  /*
2220 2221                   * Wait up to 30 seconds for IOC to come out of reset.
2221 2222                   */
2222 2223                  while (((ioc_status = ddi_get32(mpt->m_datap,
2223 2224                      &mpt->m_reg->Doorbell)) &
2224 2225                      MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_RESET) {
2225 2226                          if (polls++ > 3000) {
2226 2227                                  break;
2227 2228                          }
2228 2229                          delay(drv_usectohz(10000));
2229 2230                  }
2230 2231                  /*
2231 2232                   * If IOC is not in operational state, try to hard reset it.
2232 2233                   */
2233 2234                  if ((ioc_status & MPI2_IOC_STATE_MASK) !=
2234 2235                      MPI2_IOC_STATE_OPERATIONAL) {
2235 2236                          mpt->m_softstate &= ~MPTSAS_SS_MSG_UNIT_RESET;
2236 2237                          if (mptsas_restart_ioc(mpt) == DDI_FAILURE) {
2237 2238                                  mptsas_log(mpt, CE_WARN,
2238 2239                                      "mptsas_power: hard reset failed");
2239 2240                                  mutex_exit(&mpt->m_mutex);
2240 2241                                  return (DDI_FAILURE);
2241 2242                          }
2242 2243                  }
2243 2244                  mutex_enter(&mpt->m_intr_mutex);
2244 2245                  mpt->m_power_level = PM_LEVEL_D0;
2245 2246                  mutex_exit(&mpt->m_intr_mutex);
2246 2247                  break;
2247 2248          case PM_LEVEL_D3:
2248 2249                  NDBG11(("mptsas%d: turning power OFF.", mpt->m_instance));
2249 2250                  MPTSAS_POWER_OFF(mpt);
2250 2251                  break;
2251 2252          default:
2252 2253                  mptsas_log(mpt, CE_WARN, "mptsas%d: unknown power level <%x>.",
2253 2254                      mpt->m_instance, level);
2254 2255                  rval = DDI_FAILURE;
2255 2256                  break;
2256 2257          }
2257 2258          mutex_exit(&mpt->m_mutex);
2258 2259          return (rval);
2259 2260  }
2260 2261  
2261 2262  /*
2262 2263   * Initialize configuration space and figure out which
2263 2264   * chip and revison of the chip the mpt driver is using.
2264 2265   */
2265 2266  static int
2266 2267  mptsas_config_space_init(mptsas_t *mpt)
2267 2268  {
2268 2269          NDBG0(("mptsas_config_space_init"));
2269 2270  
2270 2271          if (mpt->m_config_handle != NULL)
2271 2272                  return (TRUE);
2272 2273  
2273 2274          if (pci_config_setup(mpt->m_dip,
2274 2275              &mpt->m_config_handle) != DDI_SUCCESS) {
2275 2276                  mptsas_log(mpt, CE_WARN, "cannot map configuration space.");
2276 2277                  return (FALSE);
2277 2278          }
2278 2279  
2279 2280          /*
2280 2281           * This is a workaround for a XMITS ASIC bug which does not
2281 2282           * drive the CBE upper bits.
2282 2283           */
2283 2284          if (pci_config_get16(mpt->m_config_handle, PCI_CONF_STAT) &
2284 2285              PCI_STAT_PERROR) {
2285 2286                  pci_config_put16(mpt->m_config_handle, PCI_CONF_STAT,
2286 2287                      PCI_STAT_PERROR);
2287 2288          }
2288 2289  
2289 2290          mptsas_setup_cmd_reg(mpt);
2290 2291  
2291 2292          /*
2292 2293           * Get the chip device id:
2293 2294           */
2294 2295          mpt->m_devid = pci_config_get16(mpt->m_config_handle, PCI_CONF_DEVID);
2295 2296  
2296 2297          /*
2297 2298           * Save the revision.
2298 2299           */
2299 2300          mpt->m_revid = pci_config_get8(mpt->m_config_handle, PCI_CONF_REVID);
2300 2301  
2301 2302          /*
2302 2303           * Save the SubSystem Vendor and Device IDs
2303 2304           */
2304 2305          mpt->m_svid = pci_config_get16(mpt->m_config_handle, PCI_CONF_SUBVENID);
2305 2306          mpt->m_ssid = pci_config_get16(mpt->m_config_handle, PCI_CONF_SUBSYSID);
2306 2307  
2307 2308          /*
2308 2309           * Set the latency timer to 0x40 as specified by the upa -> pci
2309 2310           * bridge chip design team.  This may be done by the sparc pci
2310 2311           * bus nexus driver, but the driver should make sure the latency
2311 2312           * timer is correct for performance reasons.
2312 2313           */
2313 2314          pci_config_put8(mpt->m_config_handle, PCI_CONF_LATENCY_TIMER,
2314 2315              MPTSAS_LATENCY_TIMER);
2315 2316  
2316 2317          (void) mptsas_get_pci_cap(mpt);
2317 2318          return (TRUE);
2318 2319  }
2319 2320  
2320 2321  static void
2321 2322  mptsas_config_space_fini(mptsas_t *mpt)
2322 2323  {
2323 2324          if (mpt->m_config_handle != NULL) {
2324 2325                  mptsas_disable_bus_master(mpt);
2325 2326                  pci_config_teardown(&mpt->m_config_handle);
2326 2327                  mpt->m_config_handle = NULL;
2327 2328          }
2328 2329  }
2329 2330  
2330 2331  static void
2331 2332  mptsas_setup_cmd_reg(mptsas_t *mpt)
2332 2333  {
2333 2334          ushort_t        cmdreg;
2334 2335  
2335 2336          /*
2336 2337           * Set the command register to the needed values.
2337 2338           */
2338 2339          cmdreg = pci_config_get16(mpt->m_config_handle, PCI_CONF_COMM);
2339 2340          cmdreg |= (PCI_COMM_ME | PCI_COMM_SERR_ENABLE |
2340 2341              PCI_COMM_PARITY_DETECT | PCI_COMM_MAE);
2341 2342          cmdreg &= ~PCI_COMM_IO;
2342 2343          pci_config_put16(mpt->m_config_handle, PCI_CONF_COMM, cmdreg);
2343 2344  }
2344 2345  
2345 2346  static void
2346 2347  mptsas_disable_bus_master(mptsas_t *mpt)
2347 2348  {
2348 2349          ushort_t        cmdreg;
2349 2350  
2350 2351          /*
2351 2352           * Clear the master enable bit in the PCI command register.
2352 2353           * This prevents any bus mastering activity like DMA.
2353 2354           */
2354 2355          cmdreg = pci_config_get16(mpt->m_config_handle, PCI_CONF_COMM);
2355 2356          cmdreg &= ~PCI_COMM_ME;
2356 2357          pci_config_put16(mpt->m_config_handle, PCI_CONF_COMM, cmdreg);
2357 2358  }
2358 2359  
2359 2360  int
2360 2361  mptsas_dma_alloc(mptsas_t *mpt, mptsas_dma_alloc_state_t *dma_statep)
2361 2362  {
2362 2363          ddi_dma_attr_t  attrs;
2363 2364  
2364 2365          attrs = mpt->m_io_dma_attr;
2365 2366          attrs.dma_attr_sgllen = 1;
2366 2367  
2367 2368          ASSERT(dma_statep != NULL);
2368 2369  
2369 2370          if (mptsas_dma_addr_create(mpt, attrs, &dma_statep->handle,
2370 2371              &dma_statep->accessp, &dma_statep->memp, dma_statep->size,
2371 2372              &dma_statep->cookie) == FALSE) {
2372 2373                  return (DDI_FAILURE);
2373 2374          }
2374 2375  
2375 2376          return (DDI_SUCCESS);
2376 2377  }
2377 2378  
2378 2379  void
2379 2380  mptsas_dma_free(mptsas_dma_alloc_state_t *dma_statep)
2380 2381  {
2381 2382          ASSERT(dma_statep != NULL);
2382 2383          mptsas_dma_addr_destroy(&dma_statep->handle, &dma_statep->accessp);
2383 2384          dma_statep->size = 0;
2384 2385  }
2385 2386  
2386 2387  int
2387 2388  mptsas_do_dma(mptsas_t *mpt, uint32_t size, int var, int (*callback)())
2388 2389  {
2389 2390          ddi_dma_attr_t          attrs;
2390 2391          ddi_dma_handle_t        dma_handle;
2391 2392          caddr_t                 memp;
2392 2393          ddi_acc_handle_t        accessp;
2393 2394          int                     rval;
2394 2395  
2395 2396          ASSERT(mutex_owned(&mpt->m_mutex));
2396 2397  
2397 2398          attrs = mpt->m_msg_dma_attr;
2398 2399          attrs.dma_attr_sgllen = 1;
2399 2400          attrs.dma_attr_granular = size;
2400 2401  
2401 2402          if (mptsas_dma_addr_create(mpt, attrs, &dma_handle,
2402 2403              &accessp, &memp, size, NULL) == FALSE) {
2403 2404                  return (DDI_FAILURE);
2404 2405          }
2405 2406  
2406 2407          rval = (*callback) (mpt, memp, var, accessp);
2407 2408  
2408 2409          if ((mptsas_check_dma_handle(dma_handle) != DDI_SUCCESS) ||
2409 2410              (mptsas_check_acc_handle(accessp) != DDI_SUCCESS)) {
2410 2411                  ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
2411 2412                  rval = DDI_FAILURE;
2412 2413          }
2413 2414  
2414 2415          mptsas_dma_addr_destroy(&dma_handle, &accessp);
2415 2416          return (rval);
2416 2417  
2417 2418  }
2418 2419  
2419 2420  static int
2420 2421  mptsas_alloc_request_frames(mptsas_t *mpt)
2421 2422  {
2422 2423          ddi_dma_attr_t          frame_dma_attrs;
2423 2424          caddr_t                 memp;
2424 2425          ddi_dma_cookie_t        cookie;
2425 2426          size_t                  mem_size;
2426 2427  
2427 2428          /*
2428 2429           * re-alloc when it has already alloced
2429 2430           */
2430 2431          mptsas_dma_addr_destroy(&mpt->m_dma_req_frame_hdl,
2431 2432              &mpt->m_acc_req_frame_hdl);
2432 2433  
2433 2434          /*
2434 2435           * The size of the request frame pool is:
2435 2436           *   Number of Request Frames * Request Frame Size
2436 2437           */
2437 2438          mem_size = mpt->m_max_requests * mpt->m_req_frame_size;
2438 2439  
2439 2440          /*
2440 2441           * set the DMA attributes.  System Request Message Frames must be
2441 2442           * aligned on a 16-byte boundry.
2442 2443           */
2443 2444          frame_dma_attrs = mpt->m_msg_dma_attr;
2444 2445          frame_dma_attrs.dma_attr_align = 16;
2445 2446          frame_dma_attrs.dma_attr_sgllen = 1;
2446 2447  
2447 2448          /*
2448 2449           * allocate the request frame pool.
2449 2450           */
2450 2451          if (mptsas_dma_addr_create(mpt, frame_dma_attrs,
2451 2452              &mpt->m_dma_req_frame_hdl, &mpt->m_acc_req_frame_hdl, &memp,
2452 2453              mem_size, &cookie) == FALSE) {
2453 2454                  return (DDI_FAILURE);
2454 2455          }
2455 2456  
2456 2457          /*
2457 2458           * Store the request frame memory address.  This chip uses this
2458 2459           * address to dma to and from the driver's frame.  The second
2459 2460           * address is the address mpt uses to fill in the frame.
2460 2461           */
2461 2462          mpt->m_req_frame_dma_addr = cookie.dmac_laddress;
2462 2463          mpt->m_req_frame = memp;
2463 2464  
2464 2465          /*
2465 2466           * Clear the request frame pool.
2466 2467           */
2467 2468          bzero(mpt->m_req_frame, mem_size);
2468 2469  
2469 2470          return (DDI_SUCCESS);
2470 2471  }
2471 2472  
2472 2473  static int
2473 2474  mptsas_alloc_reply_frames(mptsas_t *mpt)
2474 2475  {
2475 2476          ddi_dma_attr_t          frame_dma_attrs;
2476 2477          caddr_t                 memp;
2477 2478          ddi_dma_cookie_t        cookie;
2478 2479          size_t                  mem_size;
2479 2480  
2480 2481          /*
2481 2482           * re-alloc when it has already alloced
2482 2483           */
2483 2484          mptsas_dma_addr_destroy(&mpt->m_dma_reply_frame_hdl,
2484 2485              &mpt->m_acc_reply_frame_hdl);
2485 2486  
2486 2487          /*
2487 2488           * The size of the reply frame pool is:
2488 2489           *   Number of Reply Frames * Reply Frame Size
2489 2490           */
2490 2491          mem_size = mpt->m_max_replies * mpt->m_reply_frame_size;
2491 2492  
2492 2493          /*
2493 2494           * set the DMA attributes.   System Reply Message Frames must be
2494 2495           * aligned on a 4-byte boundry.  This is the default.
2495 2496           */
2496 2497          frame_dma_attrs = mpt->m_msg_dma_attr;
2497 2498          frame_dma_attrs.dma_attr_sgllen = 1;
2498 2499  
2499 2500          /*
2500 2501           * allocate the reply frame pool
2501 2502           */
2502 2503          if (mptsas_dma_addr_create(mpt, frame_dma_attrs,
2503 2504              &mpt->m_dma_reply_frame_hdl, &mpt->m_acc_reply_frame_hdl, &memp,
2504 2505              mem_size, &cookie) == FALSE) {
2505 2506                  return (DDI_FAILURE);
2506 2507          }
2507 2508  
2508 2509          /*
2509 2510           * Store the reply frame memory address.  This chip uses this
2510 2511           * address to dma to and from the driver's frame.  The second
2511 2512           * address is the address mpt uses to process the frame.
2512 2513           */
2513 2514          mpt->m_reply_frame_dma_addr = cookie.dmac_laddress;
2514 2515          mpt->m_reply_frame = memp;
2515 2516  
2516 2517          /*
2517 2518           * Clear the reply frame pool.
2518 2519           */
2519 2520          bzero(mpt->m_reply_frame, mem_size);
2520 2521  
2521 2522          return (DDI_SUCCESS);
2522 2523  }
2523 2524  
2524 2525  static int
2525 2526  mptsas_alloc_free_queue(mptsas_t *mpt)
2526 2527  {
2527 2528          ddi_dma_attr_t          frame_dma_attrs;
2528 2529          caddr_t                 memp;
2529 2530          ddi_dma_cookie_t        cookie;
2530 2531          size_t                  mem_size;
2531 2532  
2532 2533          /*
2533 2534           * re-alloc when it has already alloced
2534 2535           */
2535 2536          mptsas_dma_addr_destroy(&mpt->m_dma_free_queue_hdl,
2536 2537              &mpt->m_acc_free_queue_hdl);
2537 2538  
2538 2539          /*
2539 2540           * The reply free queue size is:
2540 2541           *   Reply Free Queue Depth * 4
2541 2542           * The "4" is the size of one 32 bit address (low part of 64-bit
2542 2543           *   address)
2543 2544           */
2544 2545          mem_size = mpt->m_free_queue_depth * 4;
2545 2546  
2546 2547          /*
2547 2548           * set the DMA attributes  The Reply Free Queue must be aligned on a
2548 2549           * 16-byte boundry.
2549 2550           */
2550 2551          frame_dma_attrs = mpt->m_msg_dma_attr;
2551 2552          frame_dma_attrs.dma_attr_align = 16;
2552 2553          frame_dma_attrs.dma_attr_sgllen = 1;
2553 2554  
2554 2555          /*
2555 2556           * allocate the reply free queue
2556 2557           */
2557 2558          if (mptsas_dma_addr_create(mpt, frame_dma_attrs,
2558 2559              &mpt->m_dma_free_queue_hdl, &mpt->m_acc_free_queue_hdl, &memp,
2559 2560              mem_size, &cookie) == FALSE) {
2560 2561                  return (DDI_FAILURE);
2561 2562          }
2562 2563  
2563 2564          /*
2564 2565           * Store the reply free queue memory address.  This chip uses this
2565 2566           * address to read from the reply free queue.  The second address
2566 2567           * is the address mpt uses to manage the queue.
2567 2568           */
2568 2569          mpt->m_free_queue_dma_addr = cookie.dmac_laddress;
2569 2570          mpt->m_free_queue = memp;
2570 2571  
2571 2572          /*
2572 2573           * Clear the reply free queue memory.
2573 2574           */
2574 2575          bzero(mpt->m_free_queue, mem_size);
2575 2576  
2576 2577          return (DDI_SUCCESS);
2577 2578  }
2578 2579  
2579 2580  static int
2580 2581  mptsas_alloc_post_queue(mptsas_t *mpt)
2581 2582  {
2582 2583          ddi_dma_attr_t          frame_dma_attrs;
2583 2584          caddr_t                 memp;
2584 2585          ddi_dma_cookie_t        cookie;
2585 2586          size_t                  mem_size;
2586 2587  
2587 2588          /*
2588 2589           * re-alloc when it has already alloced
2589 2590           */
2590 2591          mptsas_dma_addr_destroy(&mpt->m_dma_post_queue_hdl,
2591 2592              &mpt->m_acc_post_queue_hdl);
2592 2593  
2593 2594          /*
2594 2595           * The reply descriptor post queue size is:
2595 2596           *   Reply Descriptor Post Queue Depth * 8
2596 2597           * The "8" is the size of each descriptor (8 bytes or 64 bits).
2597 2598           */
2598 2599          mem_size = mpt->m_post_queue_depth * 8;
2599 2600  
2600 2601          /*
2601 2602           * set the DMA attributes.  The Reply Descriptor Post Queue must be
2602 2603           * aligned on a 16-byte boundry.
2603 2604           */
2604 2605          frame_dma_attrs = mpt->m_msg_dma_attr;
2605 2606          frame_dma_attrs.dma_attr_align = 16;
2606 2607          frame_dma_attrs.dma_attr_sgllen = 1;
2607 2608  
2608 2609          /*
2609 2610           * allocate the reply post queue
2610 2611           */
2611 2612          if (mptsas_dma_addr_create(mpt, frame_dma_attrs,
2612 2613              &mpt->m_dma_post_queue_hdl, &mpt->m_acc_post_queue_hdl, &memp,
2613 2614              mem_size, &cookie) == FALSE) {
2614 2615                  return (DDI_FAILURE);
2615 2616          }
2616 2617  
2617 2618          /*
2618 2619           * Store the reply descriptor post queue memory address.  This chip
2619 2620           * uses this address to write to the reply descriptor post queue.  The
2620 2621           * second address is the address mpt uses to manage the queue.
2621 2622           */
2622 2623          mpt->m_post_queue_dma_addr = cookie.dmac_laddress;
2623 2624          mpt->m_post_queue = memp;
2624 2625  
2625 2626          /*
2626 2627           * Clear the reply post queue memory.
2627 2628           */
2628 2629          bzero(mpt->m_post_queue, mem_size);
2629 2630  
2630 2631          return (DDI_SUCCESS);
2631 2632  }
2632 2633  
2633 2634  static void
2634 2635  mptsas_alloc_reply_args(mptsas_t *mpt)
2635 2636  {
2636 2637          if (mpt->m_replyh_args != NULL) {
2637 2638                  kmem_free(mpt->m_replyh_args, sizeof (m_replyh_arg_t)
2638 2639                      * mpt->m_max_replies);
2639 2640                  mpt->m_replyh_args = NULL;
2640 2641          }
2641 2642          mpt->m_replyh_args = kmem_zalloc(sizeof (m_replyh_arg_t) *
2642 2643              mpt->m_max_replies, KM_SLEEP);
2643 2644  }
2644 2645  
2645 2646  static int
2646 2647  mptsas_alloc_extra_sgl_frame(mptsas_t *mpt, mptsas_cmd_t *cmd)
2647 2648  {
2648 2649          mptsas_cache_frames_t   *frames = NULL;
2649 2650          if (cmd->cmd_extra_frames == NULL) {
2650 2651                  frames = kmem_cache_alloc(mpt->m_cache_frames, KM_NOSLEEP);
2651 2652                  if (frames == NULL) {
2652 2653                          return (DDI_FAILURE);
2653 2654                  }
2654 2655                  cmd->cmd_extra_frames = frames;
2655 2656          }
2656 2657          return (DDI_SUCCESS);
2657 2658  }
2658 2659  
2659 2660  static void
2660 2661  mptsas_free_extra_sgl_frame(mptsas_t *mpt, mptsas_cmd_t *cmd)
2661 2662  {
2662 2663          if (cmd->cmd_extra_frames) {
2663 2664                  kmem_cache_free(mpt->m_cache_frames,
2664 2665                      (void *)cmd->cmd_extra_frames);
2665 2666                  cmd->cmd_extra_frames = NULL;
2666 2667          }
2667 2668  }
2668 2669  
2669 2670  static void
2670 2671  mptsas_cfg_fini(mptsas_t *mpt)
2671 2672  {
2672 2673          NDBG0(("mptsas_cfg_fini"));
2673 2674          ddi_regs_map_free(&mpt->m_datap);
2674 2675  }
2675 2676  
2676 2677  static void
2677 2678  mptsas_hba_fini(mptsas_t *mpt)
2678 2679  {
2679 2680          NDBG0(("mptsas_hba_fini"));
2680 2681  
2681 2682          /*
2682 2683           * Free up any allocated memory
2683 2684           */
2684 2685          mptsas_dma_addr_destroy(&mpt->m_dma_req_frame_hdl,
2685 2686              &mpt->m_acc_req_frame_hdl);
2686 2687  
2687 2688          mptsas_dma_addr_destroy(&mpt->m_dma_reply_frame_hdl,
2688 2689              &mpt->m_acc_reply_frame_hdl);
2689 2690  
2690 2691          mptsas_dma_addr_destroy(&mpt->m_dma_free_queue_hdl,
2691 2692              &mpt->m_acc_free_queue_hdl);
2692 2693  
2693 2694          mptsas_dma_addr_destroy(&mpt->m_dma_post_queue_hdl,
2694 2695              &mpt->m_acc_post_queue_hdl);
2695 2696  
2696 2697          if (mpt->m_replyh_args != NULL) {
2697 2698                  kmem_free(mpt->m_replyh_args, sizeof (m_replyh_arg_t)
2698 2699                      * mpt->m_max_replies);
2699 2700          }
2700 2701  }
2701 2702  
2702 2703  static int
2703 2704  mptsas_name_child(dev_info_t *lun_dip, char *name, int len)
2704 2705  {
2705 2706          int             lun = 0;
2706 2707          char            *sas_wwn = NULL;
2707 2708          int             phynum = -1;
2708 2709          int             reallen = 0;
2709 2710  
2710 2711          /* Get the target num */
2711 2712          lun = ddi_prop_get_int(DDI_DEV_T_ANY, lun_dip, DDI_PROP_DONTPASS,
2712 2713              LUN_PROP, 0);
2713 2714  
2714 2715          if ((phynum = ddi_prop_get_int(DDI_DEV_T_ANY, lun_dip,
2715 2716              DDI_PROP_DONTPASS, "sata-phy", -1)) != -1) {
2716 2717                  /*
2717 2718                   * Stick in the address of form "pPHY,LUN"
2718 2719                   */
2719 2720                  reallen = snprintf(name, len, "p%x,%x", phynum, lun);
2720 2721          } else if (ddi_prop_lookup_string(DDI_DEV_T_ANY, lun_dip,
2721 2722              DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET_PORT, &sas_wwn)
2722 2723              == DDI_PROP_SUCCESS) {
2723 2724                  /*
2724 2725                   * Stick in the address of the form "wWWN,LUN"
2725 2726                   */
2726 2727                  reallen = snprintf(name, len, "%s,%x", sas_wwn, lun);
2727 2728                  ddi_prop_free(sas_wwn);
2728 2729          } else {
2729 2730                  return (DDI_FAILURE);
2730 2731          }
2731 2732  
2732 2733          ASSERT(reallen < len);
2733 2734          if (reallen >= len) {
2734 2735                  mptsas_log(0, CE_WARN, "!mptsas_get_name: name parameter "
2735 2736                      "length too small, it needs to be %d bytes", reallen + 1);
2736 2737          }
2737 2738          return (DDI_SUCCESS);
2738 2739  }
2739 2740  
2740 2741  /*
2741 2742   * tran_tgt_init(9E) - target device instance initialization
2742 2743   */
2743 2744  static int
2744 2745  mptsas_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2745 2746      scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2746 2747  {
2747 2748  #ifndef __lock_lint
2748 2749          _NOTE(ARGUNUSED(hba_tran))
2749 2750  #endif
2750 2751  
2751 2752          /*
2752 2753           * At this point, the scsi_device structure already exists
2753 2754           * and has been initialized.
2754 2755           *
2755 2756           * Use this function to allocate target-private data structures,
2756 2757           * if needed by this HBA.  Add revised flow-control and queue
2757 2758           * properties for child here, if desired and if you can tell they
2758 2759           * support tagged queueing by now.
2759 2760           */
2760 2761          mptsas_t                *mpt;
2761 2762          int                     lun = sd->sd_address.a_lun;
2762 2763          mdi_pathinfo_t          *pip = NULL;
2763 2764          mptsas_tgt_private_t    *tgt_private = NULL;
2764 2765          mptsas_target_t         *ptgt = NULL;
2765 2766          char                    *psas_wwn = NULL;
2766 2767          int                     phymask = 0;
2767 2768          uint64_t                sas_wwn = 0;
2768 2769          mpt = SDEV2MPT(sd);
2769 2770  
2770 2771          ASSERT(scsi_hba_iport_unit_address(hba_dip) != 0);
2771 2772  
2772 2773          NDBG0(("mptsas_scsi_tgt_init: hbadip=0x%p tgtdip=0x%p lun=%d",
2773 2774              (void *)hba_dip, (void *)tgt_dip, lun));
2774 2775  
2775 2776          if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
2776 2777                  (void) ndi_merge_node(tgt_dip, mptsas_name_child);
2777 2778                  ddi_set_name_addr(tgt_dip, NULL);
2778 2779                  return (DDI_FAILURE);
2779 2780          }
2780 2781          /*
2781 2782           * phymask is 0 means the virtual port for RAID
2782 2783           */
2783 2784          phymask = ddi_prop_get_int(DDI_DEV_T_ANY, hba_dip, 0,
2784 2785              "phymask", 0);
2785 2786          if (mdi_component_is_client(tgt_dip, NULL) == MDI_SUCCESS) {
2786 2787                  if ((pip = (void *)(sd->sd_private)) == NULL) {
2787 2788                          /*
2788 2789                           * Very bad news if this occurs. Somehow scsi_vhci has
2789 2790                           * lost the pathinfo node for this target.
2790 2791                           */
2791 2792                          return (DDI_NOT_WELL_FORMED);
2792 2793                  }
2793 2794  
2794 2795                  if (mdi_prop_lookup_int(pip, LUN_PROP, &lun) !=
2795 2796                      DDI_PROP_SUCCESS) {
2796 2797                          mptsas_log(mpt, CE_WARN, "Get lun property failed\n");
2797 2798                          return (DDI_FAILURE);
2798 2799                  }
2799 2800  
2800 2801                  if (mdi_prop_lookup_string(pip, SCSI_ADDR_PROP_TARGET_PORT,
2801 2802                      &psas_wwn) == MDI_SUCCESS) {
2802 2803                          if (scsi_wwnstr_to_wwn(psas_wwn, &sas_wwn)) {
2803 2804                                  sas_wwn = 0;
2804 2805                          }
2805 2806                          (void) mdi_prop_free(psas_wwn);
2806 2807                  }
2807 2808          } else {
2808 2809                  lun = ddi_prop_get_int(DDI_DEV_T_ANY, tgt_dip,
2809 2810                      DDI_PROP_DONTPASS, LUN_PROP, 0);
2810 2811                  if (ddi_prop_lookup_string(DDI_DEV_T_ANY, tgt_dip,
2811 2812                      DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET_PORT, &psas_wwn) ==
2812 2813                      DDI_PROP_SUCCESS) {
2813 2814                          if (scsi_wwnstr_to_wwn(psas_wwn, &sas_wwn)) {
2814 2815                                  sas_wwn = 0;
2815 2816                          }
2816 2817                          ddi_prop_free(psas_wwn);
2817 2818                  } else {
2818 2819                          sas_wwn = 0;
2819 2820                  }
2820 2821          }
2821 2822          ASSERT((sas_wwn != 0) || (phymask != 0));
2822 2823          mutex_enter(&mpt->m_mutex);
2823 2824          ptgt = mptsas_hash_search(&mpt->m_active->m_tgttbl, sas_wwn, phymask);
2824 2825          mutex_exit(&mpt->m_mutex);
2825 2826          if (ptgt == NULL) {
2826 2827                  mptsas_log(mpt, CE_WARN, "!tgt_init: target doesn't exist or "
2827 2828                      "gone already! phymask:%x, saswwn %"PRIx64, phymask,
2828 2829                      sas_wwn);
2829 2830                  return (DDI_FAILURE);
2830 2831          }
2831 2832          if (hba_tran->tran_tgt_private == NULL) {
2832 2833                  tgt_private = kmem_zalloc(sizeof (mptsas_tgt_private_t),
2833 2834                      KM_SLEEP);
2834 2835                  tgt_private->t_lun = lun;
2835 2836                  tgt_private->t_private = ptgt;
2836 2837                  hba_tran->tran_tgt_private = tgt_private;
2837 2838          }
2838 2839  
2839 2840          if (mdi_component_is_client(tgt_dip, NULL) == MDI_SUCCESS) {
2840 2841                  return (DDI_SUCCESS);
2841 2842          }
2842 2843          mutex_enter(&mpt->m_mutex);
2843 2844  
2844 2845          if (ptgt->m_deviceinfo &
2845 2846              (MPI2_SAS_DEVICE_INFO_SATA_DEVICE |
2846 2847              MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE)) {
2847 2848                  uchar_t *inq89 = NULL;
2848 2849                  int inq89_len = 0x238;
2849 2850                  int reallen = 0;
2850 2851                  int rval = 0;
2851 2852                  struct sata_id *sid = NULL;
2852 2853                  char model[SATA_ID_MODEL_LEN + 1];
2853 2854                  char fw[SATA_ID_FW_LEN + 1];
2854 2855                  char *vid, *pid;
2855 2856                  int i;
2856 2857  
2857 2858                  mutex_exit(&mpt->m_mutex);
2858 2859                  /*
2859 2860                   * According SCSI/ATA Translation -2 (SAT-2) revision 01a
2860 2861                   * chapter 12.4.2 VPD page 89h includes 512 bytes ATA IDENTIFY
2861 2862                   * DEVICE data or ATA IDENTIFY PACKET DEVICE data.
2862 2863                   */
2863 2864                  inq89 = kmem_zalloc(inq89_len, KM_SLEEP);
2864 2865                  rval = mptsas_inquiry(mpt, ptgt, 0, 0x89,
2865 2866                      inq89, inq89_len, &reallen, 1);
2866 2867  
2867 2868                  if (rval != 0) {
2868 2869                          if (inq89 != NULL) {
2869 2870                                  kmem_free(inq89, inq89_len);
2870 2871                          }
2871 2872  
2872 2873                          mptsas_log(mpt, CE_WARN, "!mptsas request inquiry page "
2873 2874                              "0x89 for SATA target:%x failed!", ptgt->m_devhdl);
2874 2875                          return (DDI_SUCCESS);
2875 2876                  }
2876 2877                  sid = (void *)(&inq89[60]);
2877 2878  
2878 2879                  swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
2879 2880                  swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
2880 2881  
2881 2882                  model[SATA_ID_MODEL_LEN] = 0;
2882 2883                  fw[SATA_ID_FW_LEN] = 0;
2883 2884  
2884 2885                  /*
2885 2886                   * split model into into vid/pid
2886 2887                   */
2887 2888                  for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++)
2888 2889                          if ((*pid == ' ') || (*pid == '\t'))
2889 2890                                  break;
2890 2891                  if (i < SATA_ID_MODEL_LEN) {
2891 2892                          vid = model;
2892 2893                          /*
2893 2894                           * terminate vid, establish pid
2894 2895                           */
2895 2896                          *pid++ = 0;
2896 2897                  } else {
2897 2898                          /*
2898 2899                           * vid will stay "ATA     ", the rule is same
2899 2900                           * as sata framework implementation.
2900 2901                           */
2901 2902                          vid = NULL;
2902 2903                          /*
2903 2904                           * model is all pid
2904 2905                           */
2905 2906                          pid = model;
2906 2907                  }
2907 2908  
2908 2909                  /*
2909 2910                   * override SCSA "inquiry-*" properties
2910 2911                   */
2911 2912                  if (vid)
2912 2913                          (void) scsi_device_prop_update_inqstring(sd,
2913 2914                              INQUIRY_VENDOR_ID, vid, strlen(vid));
2914 2915                  if (pid)
2915 2916                          (void) scsi_device_prop_update_inqstring(sd,
2916 2917                              INQUIRY_PRODUCT_ID, pid, strlen(pid));
2917 2918                  (void) scsi_device_prop_update_inqstring(sd,
2918 2919                      INQUIRY_REVISION_ID, fw, strlen(fw));
2919 2920  
2920 2921                  if (inq89 != NULL) {
2921 2922                          kmem_free(inq89, inq89_len);
2922 2923                  }
2923 2924          } else {
2924 2925                  mutex_exit(&mpt->m_mutex);
2925 2926          }
2926 2927  
2927 2928          return (DDI_SUCCESS);
2928 2929  }
2929 2930  /*
2930 2931   * tran_tgt_free(9E) - target device instance deallocation
2931 2932   */
2932 2933  static void
2933 2934  mptsas_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2934 2935      scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2935 2936  {
2936 2937  #ifndef __lock_lint
2937 2938          _NOTE(ARGUNUSED(hba_dip, tgt_dip, hba_tran, sd))
2938 2939  #endif
2939 2940  
2940 2941          mptsas_tgt_private_t    *tgt_private = hba_tran->tran_tgt_private;
2941 2942  
2942 2943          if (tgt_private != NULL) {
2943 2944                  kmem_free(tgt_private, sizeof (mptsas_tgt_private_t));
2944 2945                  hba_tran->tran_tgt_private = NULL;
2945 2946          }
2946 2947  }
2947 2948  
2948 2949  /*
2949 2950   * scsi_pkt handling
2950 2951   *
2951 2952   * Visible to the external world via the transport structure.
2952 2953   */
2953 2954  
2954 2955  /*
2955 2956   * Notes:
2956 2957   *      - transport the command to the addressed SCSI target/lun device
2957 2958   *      - normal operation is to schedule the command to be transported,
2958 2959   *        and return TRAN_ACCEPT if this is successful.
2959 2960   *      - if NO_INTR, tran_start must poll device for command completion
2960 2961   */
2961 2962  static int
2962 2963  mptsas_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2963 2964  {
2964 2965  #ifndef __lock_lint
2965 2966          _NOTE(ARGUNUSED(ap))
2966 2967  #endif
2967 2968          mptsas_t        *mpt = PKT2MPT(pkt);
2968 2969          mptsas_cmd_t    *cmd = PKT2CMD(pkt);
2969 2970          int             rval;
2970 2971          mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
2971 2972  
2972 2973          NDBG1(("mptsas_scsi_start: pkt=0x%p", (void *)pkt));
2973 2974          ASSERT(ptgt);
2974 2975          if (ptgt == NULL)
2975 2976                  return (TRAN_FATAL_ERROR);
2976 2977  
2977 2978          /*
2978 2979           * prepare the pkt before taking mutex.
2979 2980           */
2980 2981          rval = mptsas_prepare_pkt(cmd);
2981 2982          if (rval != TRAN_ACCEPT) {
2982 2983                  return (rval);
2983 2984          }
2984 2985  
2985 2986          /*
2986 2987           * Send the command to target/lun, however your HBA requires it.
2987 2988           * If busy, return TRAN_BUSY; if there's some other formatting error
2988 2989           * in the packet, return TRAN_BADPKT; otherwise, fall through to the
2989 2990           * return of TRAN_ACCEPT.
2990 2991           *
2991 2992           * Remember that access to shared resources, including the mptsas_t
2992 2993           * data structure and the HBA hardware registers, must be protected
2993 2994           * with mutexes, here and everywhere.
2994 2995           *
2995 2996           * Also remember that at interrupt time, you'll get an argument
2996 2997           * to the interrupt handler which is a pointer to your mptsas_t
2997 2998           * structure; you'll have to remember which commands are outstanding
2998 2999           * and which scsi_pkt is the currently-running command so the
2999 3000           * interrupt handler can refer to the pkt to set completion
3000 3001           * status, call the target driver back through pkt_comp, etc.
3001 3002           */
3002 3003  
3003 3004          mutex_enter(&ptgt->m_tgt_intr_mutex);
3004 3005          if (ptgt->m_dr_flag == MPTSAS_DR_INTRANSITION) {
3005 3006                  if (cmd->cmd_pkt_flags & FLAG_NOQUEUE) {
3006 3007                          /*
3007 3008                           * commands should be allowed to retry by
3008 3009                           * returning TRAN_BUSY to stall the I/O's
3009 3010                           * which come from scsi_vhci since the device/
3010 3011                           * path is in unstable state now.
3011 3012                           */
3012 3013                          mutex_exit(&ptgt->m_tgt_intr_mutex);
3013 3014                          return (TRAN_BUSY);
3014 3015                  } else {
3015 3016                          /*
3016 3017                           * The device is offline, just fail the
3017 3018                           * command by returning TRAN_FATAL_ERROR.
3018 3019                           */
3019 3020                          mutex_exit(&ptgt->m_tgt_intr_mutex);
3020 3021                          return (TRAN_FATAL_ERROR);
3021 3022                  }
3022 3023          }
3023 3024          mutex_exit(&ptgt->m_tgt_intr_mutex);
3024 3025          rval = mptsas_accept_pkt(mpt, cmd);
3025 3026  
3026 3027          return (rval);
3027 3028  }
3028 3029  
3029 3030  static int
3030 3031  mptsas_accept_pkt(mptsas_t *mpt, mptsas_cmd_t *cmd)
3031 3032  {
3032 3033          int             rval = TRAN_ACCEPT;
3033 3034          mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
3034 3035  
3035 3036          NDBG1(("mptsas_accept_pkt: cmd=0x%p", (void *)cmd));
3036 3037  
3037 3038          if ((cmd->cmd_flags & CFLAG_PREPARED) == 0) {
3038 3039                  rval = mptsas_prepare_pkt(cmd);
3039 3040                  if (rval != TRAN_ACCEPT) {
3040 3041                          cmd->cmd_flags &= ~CFLAG_TRANFLAG;
3041 3042                          return (rval);
3042 3043                  }
3043 3044          }
3044 3045  
3045 3046          /*
3046 3047           * reset the throttle if we were draining
3047 3048           */
3048 3049          mutex_enter(&ptgt->m_tgt_intr_mutex);
3049 3050          if ((ptgt->m_t_ncmds == 0) &&
3050 3051              (ptgt->m_t_throttle == DRAIN_THROTTLE)) {
3051 3052                  NDBG23(("reset throttle"));
3052 3053                  ASSERT(ptgt->m_reset_delay == 0);
3053 3054                  mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
3054 3055          }
3055 3056  
3056 3057          /*
3057 3058           * If device handle has already been invalidated, just
3058 3059           * fail the command. In theory, command from scsi_vhci
3059 3060           * client is impossible send down command with invalid
3060 3061           * devhdl since devhdl is set after path offline, target
3061 3062           * driver is not suppose to select a offlined path.
3062 3063           */
3063 3064          if (ptgt->m_devhdl == MPTSAS_INVALID_DEVHDL) {
3064 3065                  NDBG20(("rejecting command, it might because invalid devhdl "
3065 3066                      "request."));
3066 3067                  mutex_exit(&ptgt->m_tgt_intr_mutex);
3067 3068                  mutex_enter(&mpt->m_mutex);
3068 3069                  /*
3069 3070                   * If HBA is being reset, the DevHandles are being
3070 3071                   * re-initialized, which means that they could be invalid
3071 3072                   * even if the target is still attached. Check if being reset
3072 3073                   * and if DevHandle is being re-initialized. If this is the
3073 3074                   * case, return BUSY so the I/O can be retried later.
3074 3075                   */
3075 3076                  if (mpt->m_in_reset) {
3076 3077                          mptsas_set_pkt_reason(mpt, cmd, CMD_RESET,
3077 3078                              STAT_BUS_RESET);
3078 3079                          if (cmd->cmd_flags & CFLAG_TXQ) {
3079 3080                                  mptsas_doneq_add(mpt, cmd);
3080 3081                                  mptsas_doneq_empty(mpt);
3081 3082                                  mutex_exit(&mpt->m_mutex);
3082 3083                                  return (rval);
3083 3084                          } else {
3084 3085                                  mutex_exit(&mpt->m_mutex);
3085 3086                                  return (TRAN_BUSY);
3086 3087                          }
3087 3088                  }
3088 3089                  mptsas_set_pkt_reason(mpt, cmd, CMD_DEV_GONE, STAT_TERMINATED);
3089 3090                  if (cmd->cmd_flags & CFLAG_TXQ) {
3090 3091                          mptsas_doneq_add(mpt, cmd);
3091 3092                          mptsas_doneq_empty(mpt);
3092 3093                          mutex_exit(&mpt->m_mutex);
3093 3094                          return (rval);
3094 3095                  } else {
3095 3096                          mutex_exit(&mpt->m_mutex);
3096 3097                          return (TRAN_FATAL_ERROR);
3097 3098                  }
3098 3099          }
3099 3100          mutex_exit(&ptgt->m_tgt_intr_mutex);
3100 3101          /*
3101 3102           * The first case is the normal case.  mpt gets a command from the
3102 3103           * target driver and starts it.
3103 3104           * Since SMID 0 is reserved and the TM slot is reserved, the actual max
3104 3105           * commands is m_max_requests - 2.
3105 3106           */
3106 3107          mutex_enter(&ptgt->m_tgt_intr_mutex);
3107 3108          if ((ptgt->m_t_throttle > HOLD_THROTTLE) &&
3108 3109              (ptgt->m_t_ncmds < ptgt->m_t_throttle) &&
3109 3110              (ptgt->m_reset_delay == 0) &&
3110 3111              (ptgt->m_t_nwait == 0) &&
3111 3112              ((cmd->cmd_pkt_flags & FLAG_NOINTR) == 0)) {
3112 3113                  mutex_exit(&ptgt->m_tgt_intr_mutex);
3113 3114                  if (mptsas_save_cmd(mpt, cmd) == TRUE) {
3114 3115                          (void) mptsas_start_cmd0(mpt, cmd);
3115 3116                  } else {
3116 3117                          mutex_enter(&mpt->m_mutex);
3117 3118                          mptsas_waitq_add(mpt, cmd);
3118 3119                          mutex_exit(&mpt->m_mutex);
3119 3120                  }
3120 3121          } else {
3121 3122                  /*
3122 3123                   * Add this pkt to the work queue
3123 3124                   */
3124 3125                  mutex_exit(&ptgt->m_tgt_intr_mutex);
3125 3126                  mutex_enter(&mpt->m_mutex);
3126 3127                  mptsas_waitq_add(mpt, cmd);
3127 3128  
3128 3129                  if (cmd->cmd_pkt_flags & FLAG_NOINTR) {
3129 3130                          (void) mptsas_poll(mpt, cmd, MPTSAS_POLL_TIME);
3130 3131  
3131 3132                          /*
3132 3133                           * Only flush the doneq if this is not a TM
3133 3134                           * cmd.  For TM cmds the flushing of the
3134 3135                           * doneq will be done in those routines.
3135 3136                           */
3136 3137                          if ((cmd->cmd_flags & CFLAG_TM_CMD) == 0) {
3137 3138                                  mptsas_doneq_empty(mpt);
3138 3139                          }
3139 3140                  }
3140 3141                  mutex_exit(&mpt->m_mutex);
3141 3142          }
3142 3143          return (rval);
3143 3144  }
3144 3145  
3145 3146  int
3146 3147  mptsas_save_cmd(mptsas_t *mpt, mptsas_cmd_t *cmd)
3147 3148  {
3148 3149          mptsas_slots_t  *slots;
3149 3150          int             slot;
3150 3151          mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
3151 3152          mptsas_slot_free_e_t    *pe;
3152 3153          int             qn, qn_first;
3153 3154  
3154 3155          slots = mpt->m_active;
3155 3156  
3156 3157          /*
3157 3158           * Account for reserved TM request slot and reserved SMID of 0.
3158 3159           */
3159 3160          ASSERT(slots->m_n_slots == (mpt->m_max_requests - 2));
3160 3161  
3161 3162          qn = qn_first = CPU->cpu_seqid & (mpt->m_slot_freeq_pair_n - 1);
3162 3163  
3163 3164  qpair_retry:
3164 3165          ASSERT(qn < mpt->m_slot_freeq_pair_n);
3165 3166          mutex_enter(&mpt->m_slot_freeq_pairp[qn].m_slot_allocq.s.m_fq_mutex);
3166 3167          pe = list_head(&mpt->m_slot_freeq_pairp[qn].m_slot_allocq.
3167 3168              s.m_fq_list);
3168 3169          if (!pe) { /* switch the allocq and releq */
3169 3170                  mutex_enter(&mpt->m_slot_freeq_pairp[qn].m_slot_releq.
3170 3171                      s.m_fq_mutex);
3171 3172                  if (mpt->m_slot_freeq_pairp[qn].m_slot_releq.s.m_fq_n) {
3172 3173                          mpt->m_slot_freeq_pairp[qn].
3173 3174                              m_slot_allocq.s.m_fq_n =
3174 3175                              mpt->m_slot_freeq_pairp[qn].
3175 3176                              m_slot_releq.s.m_fq_n;
3176 3177                          mpt->m_slot_freeq_pairp[qn].
3177 3178                              m_slot_allocq.s.m_fq_list.list_head.list_next =
3178 3179                              mpt->m_slot_freeq_pairp[qn].
3179 3180                              m_slot_releq.s.m_fq_list.list_head.list_next;
3180 3181                          mpt->m_slot_freeq_pairp[qn].
3181 3182                              m_slot_allocq.s.m_fq_list.list_head.list_prev =
3182 3183                              mpt->m_slot_freeq_pairp[qn].
3183 3184                              m_slot_releq.s.m_fq_list.list_head.list_prev;
3184 3185                          mpt->m_slot_freeq_pairp[qn].
3185 3186                              m_slot_releq.s.m_fq_list.list_head.list_prev->
3186 3187                              list_next =
3187 3188                              &mpt->m_slot_freeq_pairp[qn].
3188 3189                              m_slot_allocq.s.m_fq_list.list_head;
3189 3190                          mpt->m_slot_freeq_pairp[qn].
3190 3191                              m_slot_releq.s.m_fq_list.list_head.list_next->
3191 3192                              list_prev =
3192 3193                              &mpt->m_slot_freeq_pairp[qn].
3193 3194                              m_slot_allocq.s.m_fq_list.list_head;
3194 3195  
3195 3196                          mpt->m_slot_freeq_pairp[qn].
3196 3197                              m_slot_releq.s.m_fq_list.list_head.list_next =
3197 3198                              mpt->m_slot_freeq_pairp[qn].
3198 3199                              m_slot_releq.s.m_fq_list.list_head.list_prev =
3199 3200                              &mpt->m_slot_freeq_pairp[qn].
3200 3201                              m_slot_releq.s.m_fq_list.list_head;
3201 3202                          mpt->m_slot_freeq_pairp[qn].
3202 3203                              m_slot_releq.s.m_fq_n = 0;
3203 3204                  } else {
3204 3205                          mutex_exit(&mpt->m_slot_freeq_pairp[qn].
3205 3206                              m_slot_releq.s.m_fq_mutex);
3206 3207                          mutex_exit(&mpt->m_slot_freeq_pairp[qn].
3207 3208                              m_slot_allocq.s.m_fq_mutex);
3208 3209                          qn = (qn + 1) & (mpt->m_slot_freeq_pair_n - 1);
3209 3210                          if (qn == qn_first)
3210 3211                                  return (FALSE);
3211 3212                          else
3212 3213                                  goto qpair_retry;
3213 3214                  }
3214 3215                  mutex_exit(&mpt->m_slot_freeq_pairp[qn].
3215 3216                      m_slot_releq.s.m_fq_mutex);
3216 3217                  pe = list_head(&mpt->m_slot_freeq_pairp[qn].
3217 3218                      m_slot_allocq.s.m_fq_list);
3218 3219                  ASSERT(pe);
3219 3220          }
3220 3221          list_remove(&mpt->m_slot_freeq_pairp[qn].
3221 3222              m_slot_allocq.s.m_fq_list, pe);
3222 3223          slot = pe->slot;
3223 3224          /*
3224 3225           * Make sure SMID is not using reserved value of 0
3225 3226           * and the TM request slot.
3226 3227           */
3227 3228          ASSERT((slot > 0) && (slot <= slots->m_n_slots) &&
3228 3229              mpt->m_slot_freeq_pairp[qn].m_slot_allocq.s.m_fq_n > 0);
3229 3230          cmd->cmd_slot = slot;
3230 3231          mpt->m_slot_freeq_pairp[qn].m_slot_allocq.s.m_fq_n--;
3231 3232          ASSERT(mpt->m_slot_freeq_pairp[qn].m_slot_allocq.s.m_fq_n >= 0);
3232 3233  
3233 3234          mutex_exit(&mpt->m_slot_freeq_pairp[qn].m_slot_allocq.s.m_fq_mutex);
3234 3235          /*
3235 3236           * only increment per target ncmds if this is not a
3236 3237           * command that has no target associated with it (i.e. a
3237 3238           * event acknoledgment)
3238 3239           */
3239 3240          if ((cmd->cmd_flags & CFLAG_CMDIOC) == 0) {
3240 3241                  mutex_enter(&ptgt->m_tgt_intr_mutex);
3241 3242                  ptgt->m_t_ncmds++;
3242 3243                  mutex_exit(&ptgt->m_tgt_intr_mutex);
3243 3244          }
3244 3245          cmd->cmd_active_timeout = cmd->cmd_pkt->pkt_time;
3245 3246  
3246 3247          /*
3247 3248           * If initial timout is less than or equal to one tick, bump
3248 3249           * the timeout by a tick so that command doesn't timeout before
3249 3250           * its allotted time.
3250 3251           */
3251 3252          if (cmd->cmd_active_timeout <= mptsas_scsi_watchdog_tick) {
3252 3253                  cmd->cmd_active_timeout += mptsas_scsi_watchdog_tick;
3253 3254          }
3254 3255          return (TRUE);
3255 3256  }
3256 3257  
3257 3258  /*
3258 3259   * prepare the pkt:
3259 3260   * the pkt may have been resubmitted or just reused so
3260 3261   * initialize some fields and do some checks.
3261 3262   */
3262 3263  static int
3263 3264  mptsas_prepare_pkt(mptsas_cmd_t *cmd)
3264 3265  {
3265 3266          struct scsi_pkt *pkt = CMD2PKT(cmd);
3266 3267  
3267 3268          NDBG1(("mptsas_prepare_pkt: cmd=0x%p", (void *)cmd));
3268 3269  
3269 3270          /*
3270 3271           * Reinitialize some fields that need it; the packet may
3271 3272           * have been resubmitted
3272 3273           */
3273 3274          pkt->pkt_reason = CMD_CMPLT;
3274 3275          pkt->pkt_state = 0;
3275 3276          pkt->pkt_statistics = 0;
3276 3277          pkt->pkt_resid = 0;
3277 3278          cmd->cmd_age = 0;
3278 3279          cmd->cmd_pkt_flags = pkt->pkt_flags;
3279 3280  
3280 3281          /*
3281 3282           * zero status byte.
3282 3283           */
3283 3284          *(pkt->pkt_scbp) = 0;
3284 3285  
3285 3286          if (cmd->cmd_flags & CFLAG_DMAVALID) {
3286 3287                  pkt->pkt_resid = cmd->cmd_dmacount;
3287 3288  
3288 3289                  /*
3289 3290                   * consistent packets need to be sync'ed first
3290 3291                   * (only for data going out)
3291 3292                   */
3292 3293                  if ((cmd->cmd_flags & CFLAG_CMDIOPB) &&
3293 3294                      (cmd->cmd_flags & CFLAG_DMASEND)) {
3294 3295                          (void) ddi_dma_sync(cmd->cmd_dmahandle, 0, 0,
3295 3296                              DDI_DMA_SYNC_FORDEV);
3296 3297                  }
3297 3298          }
3298 3299  
3299 3300          cmd->cmd_flags =
3300 3301              (cmd->cmd_flags & ~(CFLAG_TRANFLAG)) |
3301 3302              CFLAG_PREPARED | CFLAG_IN_TRANSPORT;
3302 3303  
3303 3304          return (TRAN_ACCEPT);
3304 3305  }
3305 3306  
3306 3307  /*
3307 3308   * tran_init_pkt(9E) - allocate scsi_pkt(9S) for command
3308 3309   *
3309 3310   * One of three possibilities:
3310 3311   *      - allocate scsi_pkt
3311 3312   *      - allocate scsi_pkt and DMA resources
3312 3313   *      - allocate DMA resources to an already-allocated pkt
3313 3314   */
3314 3315  static struct scsi_pkt *
3315 3316  mptsas_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
3316 3317      struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
3317 3318      int (*callback)(), caddr_t arg)
3318 3319  {
3319 3320          mptsas_cmd_t            *cmd, *new_cmd;
3320 3321          mptsas_t                *mpt = ADDR2MPT(ap);
3321 3322          int                     failure = 1;
3322 3323  #ifndef __sparc
3323 3324          uint_t                  oldcookiec;
3324 3325  #endif  /* __sparc */
3325 3326          mptsas_target_t         *ptgt = NULL;
3326 3327          int                     rval;
3327 3328          mptsas_tgt_private_t    *tgt_private;
3328 3329          int                     kf;
3329 3330  
3330 3331          kf = (callback == SLEEP_FUNC)? KM_SLEEP: KM_NOSLEEP;
3331 3332  
3332 3333          tgt_private = (mptsas_tgt_private_t *)ap->a_hba_tran->
3333 3334              tran_tgt_private;
3334 3335          ASSERT(tgt_private != NULL);
3335 3336          if (tgt_private == NULL) {
3336 3337                  return (NULL);
3337 3338          }
3338 3339          ptgt = tgt_private->t_private;
3339 3340          ASSERT(ptgt != NULL);
3340 3341          if (ptgt == NULL)
3341 3342                  return (NULL);
3342 3343          ap->a_target = ptgt->m_devhdl;
3343 3344          ap->a_lun = tgt_private->t_lun;
3344 3345  
3345 3346          ASSERT(callback == NULL_FUNC || callback == SLEEP_FUNC);
3346 3347  #ifdef MPTSAS_TEST_EXTRN_ALLOC
3347 3348          statuslen *= 100; tgtlen *= 4;
3348 3349  #endif
3349 3350          NDBG3(("mptsas_scsi_init_pkt:\n"
3350 3351              "\ttgt=%d in=0x%p bp=0x%p clen=%d slen=%d tlen=%d flags=%x",
3351 3352              ap->a_target, (void *)pkt, (void *)bp,
3352 3353              cmdlen, statuslen, tgtlen, flags));
3353 3354  
3354 3355          /*
3355 3356           * Allocate the new packet.
3356 3357           */
3357 3358          if (pkt == NULL) {
3358 3359                  ddi_dma_handle_t        save_dma_handle;
3359 3360                  ddi_dma_handle_t        save_arq_dma_handle;
3360 3361                  struct buf              *save_arq_bp;
3361 3362                  ddi_dma_cookie_t        save_arqcookie;
3362 3363  #ifdef  __sparc
3363 3364                  mptti_t                 *save_sg;
3364 3365  #endif  /* __sparc */
3365 3366  
3366 3367                  cmd = kmem_cache_alloc(mpt->m_kmem_cache, kf);
3367 3368  
3368 3369                  if (cmd) {
3369 3370                          save_dma_handle = cmd->cmd_dmahandle;
3370 3371                          save_arq_dma_handle = cmd->cmd_arqhandle;
3371 3372                          save_arq_bp = cmd->cmd_arq_buf;
3372 3373                          save_arqcookie = cmd->cmd_arqcookie;
3373 3374  #ifdef  __sparc
3374 3375                          save_sg = cmd->cmd_sg;
3375 3376  #endif  /* __sparc */
3376 3377                          bzero(cmd, sizeof (*cmd) + scsi_pkt_size());
3377 3378                          cmd->cmd_dmahandle = save_dma_handle;
3378 3379                          cmd->cmd_arqhandle = save_arq_dma_handle;
3379 3380                          cmd->cmd_arq_buf = save_arq_bp;
3380 3381                          cmd->cmd_arqcookie = save_arqcookie;
3381 3382  #ifdef  __sparc
3382 3383                          cmd->cmd_sg = save_sg;
3383 3384  #endif  /* __sparc */
3384 3385                          pkt = (void *)((uchar_t *)cmd +
3385 3386                              sizeof (struct mptsas_cmd));
3386 3387                          pkt->pkt_ha_private = (opaque_t)cmd;
3387 3388                          pkt->pkt_address = *ap;
3388 3389                          pkt->pkt_private = (opaque_t)cmd->cmd_pkt_private;
3389 3390                          pkt->pkt_scbp = (opaque_t)&cmd->cmd_scb;
3390 3391                          pkt->pkt_cdbp = (opaque_t)&cmd->cmd_cdb;
3391 3392                          cmd->cmd_pkt = (struct scsi_pkt *)pkt;
3392 3393                          cmd->cmd_cdblen = (uchar_t)cmdlen;
3393 3394                          cmd->cmd_scblen = statuslen;
3394 3395                          cmd->cmd_rqslen = SENSE_LENGTH;
3395 3396                          cmd->cmd_tgt_addr = ptgt;
3396 3397                          failure = 0;
3397 3398                  }
3398 3399  
3399 3400                  if (failure || (cmdlen > sizeof (cmd->cmd_cdb)) ||
3400 3401                      (tgtlen > PKT_PRIV_LEN) ||
3401 3402                      (statuslen > EXTCMDS_STATUS_SIZE)) {
3402 3403                          if (failure == 0) {
3403 3404                                  /*
3404 3405                                   * if extern alloc fails, all will be
3405 3406                                   * deallocated, including cmd
3406 3407                                   */
3407 3408                                  failure = mptsas_pkt_alloc_extern(mpt, cmd,
3408 3409                                      cmdlen, tgtlen, statuslen, kf);
3409 3410                          }
3410 3411                          if (failure) {
3411 3412                                  /*
3412 3413                                   * if extern allocation fails, it will
3413 3414                                   * deallocate the new pkt as well
3414 3415                                   */
3415 3416                                  return (NULL);
3416 3417                          }
3417 3418                  }
3418 3419                  new_cmd = cmd;
3419 3420  
3420 3421          } else {
3421 3422                  cmd = PKT2CMD(pkt);
3422 3423                  new_cmd = NULL;
3423 3424          }
3424 3425  
3425 3426  
3426 3427  #ifndef __sparc
3427 3428          /* grab cmd->cmd_cookiec here as oldcookiec */
3428 3429  
3429 3430          oldcookiec = cmd->cmd_cookiec;
3430 3431  #endif  /* __sparc */
3431 3432  
3432 3433          /*
3433 3434           * If the dma was broken up into PARTIAL transfers cmd_nwin will be
3434 3435           * greater than 0 and we'll need to grab the next dma window
3435 3436           */
3436 3437          /*
3437 3438           * SLM-not doing extra command frame right now; may add later
3438 3439           */
3439 3440  
3440 3441          if (cmd->cmd_nwin > 0) {
3441 3442  
3442 3443                  /*
3443 3444                   * Make sure we havn't gone past the the total number
3444 3445                   * of windows
3445 3446                   */
3446 3447                  if (++cmd->cmd_winindex >= cmd->cmd_nwin) {
3447 3448                          return (NULL);
3448 3449                  }
3449 3450                  if (ddi_dma_getwin(cmd->cmd_dmahandle, cmd->cmd_winindex,
3450 3451                      &cmd->cmd_dma_offset, &cmd->cmd_dma_len,
3451 3452                      &cmd->cmd_cookie, &cmd->cmd_cookiec) == DDI_FAILURE) {
3452 3453                          return (NULL);
3453 3454                  }
3454 3455                  goto get_dma_cookies;
3455 3456          }
3456 3457  
3457 3458  
3458 3459          if (flags & PKT_XARQ) {
3459 3460                  cmd->cmd_flags |= CFLAG_XARQ;
3460 3461          }
3461 3462  
3462 3463          /*
3463 3464           * DMA resource allocation.  This version assumes your
3464 3465           * HBA has some sort of bus-mastering or onboard DMA capability, with a
3465 3466           * scatter-gather list of length MPTSAS_MAX_DMA_SEGS, as given in the
3466 3467           * ddi_dma_attr_t structure and passed to scsi_impl_dmaget.
3467 3468           */
3468 3469          if (bp && (bp->b_bcount != 0) &&
3469 3470              (cmd->cmd_flags & CFLAG_DMAVALID) == 0) {
3470 3471  
3471 3472                  int     cnt, dma_flags;
3472 3473                  mptti_t *dmap;          /* ptr to the S/G list */
3473 3474  
3474 3475                  /*
3475 3476                   * Set up DMA memory and position to the next DMA segment.
3476 3477                   */
3477 3478                  ASSERT(cmd->cmd_dmahandle != NULL);
3478 3479  
3479 3480                  if (bp->b_flags & B_READ) {
3480 3481                          dma_flags = DDI_DMA_READ;
3481 3482                          cmd->cmd_flags &= ~CFLAG_DMASEND;
3482 3483                  } else {
3483 3484                          dma_flags = DDI_DMA_WRITE;
3484 3485                          cmd->cmd_flags |= CFLAG_DMASEND;
3485 3486                  }
3486 3487                  if (flags & PKT_CONSISTENT) {
3487 3488                          cmd->cmd_flags |= CFLAG_CMDIOPB;
3488 3489                          dma_flags |= DDI_DMA_CONSISTENT;
3489 3490                  }
3490 3491  
3491 3492                  if (flags & PKT_DMA_PARTIAL) {
3492 3493                          dma_flags |= DDI_DMA_PARTIAL;
3493 3494                  }
3494 3495  
3495 3496                  /*
3496 3497                   * workaround for byte hole issue on psycho and
3497 3498                   * schizo pre 2.1
3498 3499                   */
3499 3500                  if ((bp->b_flags & B_READ) && ((bp->b_flags &
3500 3501                      (B_PAGEIO|B_REMAPPED)) != B_PAGEIO) &&
3501 3502                      ((uintptr_t)bp->b_un.b_addr & 0x7)) {
3502 3503                          dma_flags |= DDI_DMA_CONSISTENT;
3503 3504                  }
3504 3505  
3505 3506                  rval = ddi_dma_buf_bind_handle(cmd->cmd_dmahandle, bp,
3506 3507                      dma_flags, callback, arg,
3507 3508                      &cmd->cmd_cookie, &cmd->cmd_cookiec);
3508 3509                  if (rval == DDI_DMA_PARTIAL_MAP) {
3509 3510                          (void) ddi_dma_numwin(cmd->cmd_dmahandle,
3510 3511                              &cmd->cmd_nwin);
3511 3512                          cmd->cmd_winindex = 0;
3512 3513                          (void) ddi_dma_getwin(cmd->cmd_dmahandle,
3513 3514                              cmd->cmd_winindex, &cmd->cmd_dma_offset,
3514 3515                              &cmd->cmd_dma_len, &cmd->cmd_cookie,
3515 3516                              &cmd->cmd_cookiec);
3516 3517                  } else if (rval && (rval != DDI_DMA_MAPPED)) {
3517 3518                          switch (rval) {
3518 3519                          case DDI_DMA_NORESOURCES:
3519 3520                                  bioerror(bp, 0);
3520 3521                                  break;
3521 3522                          case DDI_DMA_BADATTR:
3522 3523                          case DDI_DMA_NOMAPPING:
3523 3524                                  bioerror(bp, EFAULT);
3524 3525                                  break;
3525 3526                          case DDI_DMA_TOOBIG:
3526 3527                          default:
3527 3528                                  bioerror(bp, EINVAL);
3528 3529                                  break;
3529 3530                          }
3530 3531                          cmd->cmd_flags &= ~CFLAG_DMAVALID;
3531 3532                          if (new_cmd) {
3532 3533                                  mptsas_scsi_destroy_pkt(ap, pkt);
3533 3534                          }
3534 3535                          return ((struct scsi_pkt *)NULL);
3535 3536                  }
3536 3537  
3537 3538  get_dma_cookies:
3538 3539                  cmd->cmd_flags |= CFLAG_DMAVALID;
3539 3540                  ASSERT(cmd->cmd_cookiec > 0);
3540 3541  
3541 3542                  if (cmd->cmd_cookiec > MPTSAS_MAX_CMD_SEGS) {
3542 3543                          mptsas_log(mpt, CE_NOTE, "large cookiec received %d\n",
3543 3544                              cmd->cmd_cookiec);
3544 3545                          bioerror(bp, EINVAL);
3545 3546                          if (new_cmd) {
3546 3547                                  mptsas_scsi_destroy_pkt(ap, pkt);
3547 3548                          }
3548 3549                          return ((struct scsi_pkt *)NULL);
3549 3550                  }
3550 3551  
3551 3552                  /*
3552 3553                   * Allocate extra SGL buffer if needed.
3553 3554                   */
3554 3555                  if ((cmd->cmd_cookiec > MPTSAS_MAX_FRAME_SGES64(mpt)) &&
3555 3556                      (cmd->cmd_extra_frames == NULL)) {
3556 3557                          if (mptsas_alloc_extra_sgl_frame(mpt, cmd) ==
3557 3558                              DDI_FAILURE) {
3558 3559                                  mptsas_log(mpt, CE_WARN, "MPT SGL mem alloc "
3559 3560                                      "failed");
3560 3561                                  bioerror(bp, ENOMEM);
3561 3562                                  if (new_cmd) {
3562 3563                                          mptsas_scsi_destroy_pkt(ap, pkt);
3563 3564                                  }
3564 3565                                  return ((struct scsi_pkt *)NULL);
3565 3566                          }
3566 3567                  }
3567 3568  
3568 3569                  /*
3569 3570                   * Always use scatter-gather transfer
3570 3571                   * Use the loop below to store physical addresses of
3571 3572                   * DMA segments, from the DMA cookies, into your HBA's
3572 3573                   * scatter-gather list.
3573 3574                   * We need to ensure we have enough kmem alloc'd
3574 3575                   * for the sg entries since we are no longer using an
3575 3576                   * array inside mptsas_cmd_t.
3576 3577                   *
3577 3578                   * We check cmd->cmd_cookiec against oldcookiec so
3578 3579                   * the scatter-gather list is correctly allocated
3579 3580                   */
3580 3581  #ifndef __sparc
3581 3582                  if (oldcookiec != cmd->cmd_cookiec) {
3582 3583                          if (cmd->cmd_sg != (mptti_t *)NULL) {
3583 3584                                  kmem_free(cmd->cmd_sg, sizeof (mptti_t) *
3584 3585                                      oldcookiec);
3585 3586                                  cmd->cmd_sg = NULL;
3586 3587                          }
3587 3588                  }
3588 3589  
3589 3590                  if (cmd->cmd_sg == (mptti_t *)NULL) {
3590 3591                          cmd->cmd_sg = kmem_alloc((size_t)(sizeof (mptti_t)*
3591 3592                              cmd->cmd_cookiec), kf);
3592 3593  
3593 3594                          if (cmd->cmd_sg == (mptti_t *)NULL) {
3594 3595                                  mptsas_log(mpt, CE_WARN,
3595 3596                                      "unable to kmem_alloc enough memory "
3596 3597                                      "for scatter/gather list");
3597 3598                  /*
3598 3599                   * if we have an ENOMEM condition we need to behave
3599 3600                   * the same way as the rest of this routine
3600 3601                   */
3601 3602  
3602 3603                                  bioerror(bp, ENOMEM);
3603 3604                                  if (new_cmd) {
3604 3605                                          mptsas_scsi_destroy_pkt(ap, pkt);
3605 3606                                  }
3606 3607                                  return ((struct scsi_pkt *)NULL);
3607 3608                          }
3608 3609                  }
3609 3610  #endif  /* __sparc */
3610 3611                  dmap = cmd->cmd_sg;
3611 3612  
3612 3613                  ASSERT(cmd->cmd_cookie.dmac_size != 0);
3613 3614  
3614 3615                  /*
3615 3616                   * store the first segment into the S/G list
3616 3617                   */
3617 3618                  dmap->count = cmd->cmd_cookie.dmac_size;
3618 3619                  dmap->addr.address64.Low = (uint32_t)
3619 3620                      (cmd->cmd_cookie.dmac_laddress & 0xffffffffull);
3620 3621                  dmap->addr.address64.High = (uint32_t)
3621 3622                      (cmd->cmd_cookie.dmac_laddress >> 32);
3622 3623  
3623 3624                  /*
3624 3625                   * dmacount counts the size of the dma for this window
3625 3626                   * (if partial dma is being used).  totaldmacount
3626 3627                   * keeps track of the total amount of dma we have
3627 3628                   * transferred for all the windows (needed to calculate
3628 3629                   * the resid value below).
3629 3630                   */
3630 3631                  cmd->cmd_dmacount = cmd->cmd_cookie.dmac_size;
3631 3632                  cmd->cmd_totaldmacount += cmd->cmd_cookie.dmac_size;
3632 3633  
3633 3634                  /*
3634 3635                   * We already stored the first DMA scatter gather segment,
3635 3636                   * start at 1 if we need to store more.
3636 3637                   */
3637 3638                  for (cnt = 1; cnt < cmd->cmd_cookiec; cnt++) {
3638 3639                          /*
3639 3640                           * Get next DMA cookie
3640 3641                           */
3641 3642                          ddi_dma_nextcookie(cmd->cmd_dmahandle,
3642 3643                              &cmd->cmd_cookie);
3643 3644                          dmap++;
3644 3645  
3645 3646                          cmd->cmd_dmacount += cmd->cmd_cookie.dmac_size;
3646 3647                          cmd->cmd_totaldmacount += cmd->cmd_cookie.dmac_size;
3647 3648  
3648 3649                          /*
3649 3650                           * store the segment parms into the S/G list
3650 3651                           */
3651 3652                          dmap->count = cmd->cmd_cookie.dmac_size;
3652 3653                          dmap->addr.address64.Low = (uint32_t)
3653 3654                              (cmd->cmd_cookie.dmac_laddress & 0xffffffffull);
3654 3655                          dmap->addr.address64.High = (uint32_t)
3655 3656                              (cmd->cmd_cookie.dmac_laddress >> 32);
3656 3657                  }
3657 3658  
3658 3659                  /*
3659 3660                   * If this was partially allocated we set the resid
3660 3661                   * the amount of data NOT transferred in this window
3661 3662                   * If there is only one window, the resid will be 0
3662 3663                   */
3663 3664                  pkt->pkt_resid = (bp->b_bcount - cmd->cmd_totaldmacount);
3664 3665                  NDBG16(("mptsas_dmaget: cmd_dmacount=%d.", cmd->cmd_dmacount));
3665 3666          }
3666 3667          return (pkt);
3667 3668  }
3668 3669  
3669 3670  /*
3670 3671   * tran_destroy_pkt(9E) - scsi_pkt(9s) deallocation
3671 3672   *
3672 3673   * Notes:
3673 3674   *      - also frees DMA resources if allocated
3674 3675   *      - implicit DMA synchonization
3675 3676   */
3676 3677  static void
3677 3678  mptsas_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3678 3679  {
3679 3680          mptsas_cmd_t    *cmd = PKT2CMD(pkt);
3680 3681          mptsas_t        *mpt = ADDR2MPT(ap);
3681 3682  
3682 3683          NDBG3(("mptsas_scsi_destroy_pkt: target=%d pkt=0x%p",
3683 3684              ap->a_target, (void *)pkt));
3684 3685  
3685 3686          if (cmd->cmd_flags & CFLAG_DMAVALID) {
3686 3687                  (void) ddi_dma_unbind_handle(cmd->cmd_dmahandle);
3687 3688                  cmd->cmd_flags &= ~CFLAG_DMAVALID;
3688 3689          }
3689 3690  #ifndef __sparc
3690 3691          if (cmd->cmd_sg) {
3691 3692                  kmem_free(cmd->cmd_sg, sizeof (mptti_t) * cmd->cmd_cookiec);
3692 3693                  cmd->cmd_sg = NULL;
3693 3694          }
3694 3695  #endif  /* __sparc */
3695 3696          mptsas_free_extra_sgl_frame(mpt, cmd);
3696 3697  
3697 3698          if ((cmd->cmd_flags &
3698 3699              (CFLAG_FREE | CFLAG_CDBEXTERN | CFLAG_PRIVEXTERN |
3699 3700              CFLAG_SCBEXTERN)) == 0) {
3700 3701                  cmd->cmd_flags = CFLAG_FREE;
3701 3702                  kmem_cache_free(mpt->m_kmem_cache, (void *)cmd);
3702 3703          } else {
3703 3704                  mptsas_pkt_destroy_extern(mpt, cmd);
3704 3705          }
3705 3706  }
3706 3707  
3707 3708  /*
3708 3709   * kmem cache constructor and destructor:
3709 3710   * When constructing, we bzero the cmd and allocate the dma handle
3710 3711   * When destructing, just free the dma handle
3711 3712   */
3712 3713  static int
3713 3714  mptsas_kmem_cache_constructor(void *buf, void *cdrarg, int kmflags)
3714 3715  {
3715 3716          mptsas_cmd_t            *cmd = buf;
3716 3717          mptsas_t                *mpt  = cdrarg;
3717 3718          struct scsi_address     ap;
3718 3719          uint_t                  cookiec;
3719 3720          ddi_dma_attr_t          arq_dma_attr;
3720 3721          int                     (*callback)(caddr_t);
3721 3722  
3722 3723          callback = (kmflags == KM_SLEEP)? DDI_DMA_SLEEP: DDI_DMA_DONTWAIT;
3723 3724  
3724 3725          NDBG4(("mptsas_kmem_cache_constructor"));
3725 3726  
3726 3727          ap.a_hba_tran = mpt->m_tran;
3727 3728          ap.a_target = 0;
3728 3729          ap.a_lun = 0;
3729 3730  
3730 3731          /*
3731 3732           * allocate a dma handle
3732 3733           */
3733 3734          if ((ddi_dma_alloc_handle(mpt->m_dip, &mpt->m_io_dma_attr, callback,
3734 3735              NULL, &cmd->cmd_dmahandle)) != DDI_SUCCESS) {
3735 3736                  cmd->cmd_dmahandle = NULL;
3736 3737                  return (-1);
3737 3738          }
3738 3739  
3739 3740          cmd->cmd_arq_buf = scsi_alloc_consistent_buf(&ap, (struct buf *)NULL,
3740 3741              SENSE_LENGTH, B_READ, callback, NULL);
3741 3742          if (cmd->cmd_arq_buf == NULL) {
3742 3743                  ddi_dma_free_handle(&cmd->cmd_dmahandle);
3743 3744                  cmd->cmd_dmahandle = NULL;
3744 3745                  return (-1);
3745 3746          }
3746 3747  
3747 3748          /*
3748 3749           * allocate a arq handle
3749 3750           */
3750 3751          arq_dma_attr = mpt->m_msg_dma_attr;
3751 3752          arq_dma_attr.dma_attr_sgllen = 1;
3752 3753          if ((ddi_dma_alloc_handle(mpt->m_dip, &arq_dma_attr, callback,
3753 3754              NULL, &cmd->cmd_arqhandle)) != DDI_SUCCESS) {
3754 3755                  ddi_dma_free_handle(&cmd->cmd_dmahandle);
3755 3756                  scsi_free_consistent_buf(cmd->cmd_arq_buf);
3756 3757                  cmd->cmd_dmahandle = NULL;
3757 3758                  cmd->cmd_arqhandle = NULL;
3758 3759                  return (-1);
3759 3760          }
3760 3761  
3761 3762          if (ddi_dma_buf_bind_handle(cmd->cmd_arqhandle,
3762 3763              cmd->cmd_arq_buf, (DDI_DMA_READ | DDI_DMA_CONSISTENT),
3763 3764              callback, NULL, &cmd->cmd_arqcookie, &cookiec) != DDI_SUCCESS) {
3764 3765                  ddi_dma_free_handle(&cmd->cmd_dmahandle);
3765 3766                  ddi_dma_free_handle(&cmd->cmd_arqhandle);
3766 3767                  scsi_free_consistent_buf(cmd->cmd_arq_buf);
3767 3768                  cmd->cmd_dmahandle = NULL;
3768 3769                  cmd->cmd_arqhandle = NULL;
3769 3770                  cmd->cmd_arq_buf = NULL;
3770 3771                  return (-1);
3771 3772          }
3772 3773          /*
3773 3774           * In sparc, the sgl length in most of the cases would be 1, so we
3774 3775           * pre-allocate it in cache. On x86, the max number would be 256,
3775 3776           * pre-allocate a maximum would waste a lot of memory especially
3776 3777           * when many cmds are put onto waitq.
3777 3778           */
3778 3779  #ifdef  __sparc
3779 3780          cmd->cmd_sg = kmem_alloc((size_t)(sizeof (mptti_t)*
3780 3781              MPTSAS_MAX_CMD_SEGS), KM_SLEEP);
3781 3782  #endif  /* __sparc */
3782 3783  
3783 3784          return (0);
3784 3785  }
3785 3786  
3786 3787  static void
3787 3788  mptsas_kmem_cache_destructor(void *buf, void *cdrarg)
3788 3789  {
3789 3790  #ifndef __lock_lint
3790 3791          _NOTE(ARGUNUSED(cdrarg))
3791 3792  #endif
3792 3793          mptsas_cmd_t    *cmd = buf;
3793 3794  
3794 3795          NDBG4(("mptsas_kmem_cache_destructor"));
3795 3796  
3796 3797          if (cmd->cmd_arqhandle) {
3797 3798                  (void) ddi_dma_unbind_handle(cmd->cmd_arqhandle);
3798 3799                  ddi_dma_free_handle(&cmd->cmd_arqhandle);
3799 3800                  cmd->cmd_arqhandle = NULL;
3800 3801          }
3801 3802          if (cmd->cmd_arq_buf) {
3802 3803                  scsi_free_consistent_buf(cmd->cmd_arq_buf);
3803 3804                  cmd->cmd_arq_buf = NULL;
3804 3805          }
3805 3806          if (cmd->cmd_dmahandle) {
3806 3807                  ddi_dma_free_handle(&cmd->cmd_dmahandle);
3807 3808                  cmd->cmd_dmahandle = NULL;
3808 3809          }
3809 3810  #ifdef  __sparc
3810 3811          if (cmd->cmd_sg) {
3811 3812                  kmem_free(cmd->cmd_sg, sizeof (mptti_t)* MPTSAS_MAX_CMD_SEGS);
3812 3813                  cmd->cmd_sg = NULL;
3813 3814          }
3814 3815  #endif  /* __sparc */
3815 3816  }
3816 3817  
3817 3818  static int
3818 3819  mptsas_cache_frames_constructor(void *buf, void *cdrarg, int kmflags)
3819 3820  {
3820 3821          mptsas_cache_frames_t   *p = buf;
3821 3822          mptsas_t                *mpt = cdrarg;
3822 3823          ddi_dma_attr_t          frame_dma_attr;
3823 3824          size_t                  mem_size, alloc_len;
3824 3825          ddi_dma_cookie_t        cookie;
3825 3826          uint_t                  ncookie;
3826 3827          int (*callback)(caddr_t) = (kmflags == KM_SLEEP)
3827 3828              ? DDI_DMA_SLEEP: DDI_DMA_DONTWAIT;
3828 3829  
3829 3830          frame_dma_attr = mpt->m_msg_dma_attr;
3830 3831          frame_dma_attr.dma_attr_align = 0x10;
3831 3832          frame_dma_attr.dma_attr_sgllen = 1;
3832 3833  
3833 3834          if (ddi_dma_alloc_handle(mpt->m_dip, &frame_dma_attr, callback, NULL,
3834 3835              &p->m_dma_hdl) != DDI_SUCCESS) {
3835 3836                  mptsas_log(mpt, CE_WARN, "Unable to allocate dma handle for"
3836 3837                      " extra SGL.");
3837 3838                  return (DDI_FAILURE);
3838 3839          }
3839 3840  
3840 3841          mem_size = (mpt->m_max_request_frames - 1) * mpt->m_req_frame_size;
3841 3842  
3842 3843          if (ddi_dma_mem_alloc(p->m_dma_hdl, mem_size, &mpt->m_dev_acc_attr,
3843 3844              DDI_DMA_CONSISTENT, callback, NULL, (caddr_t *)&p->m_frames_addr,
3844 3845              &alloc_len, &p->m_acc_hdl) != DDI_SUCCESS) {
3845 3846                  ddi_dma_free_handle(&p->m_dma_hdl);
3846 3847                  p->m_dma_hdl = NULL;
3847 3848                  mptsas_log(mpt, CE_WARN, "Unable to allocate dma memory for"
3848 3849                      " extra SGL.");
3849 3850                  return (DDI_FAILURE);
3850 3851          }
3851 3852  
3852 3853          if (ddi_dma_addr_bind_handle(p->m_dma_hdl, NULL, p->m_frames_addr,
3853 3854              alloc_len, DDI_DMA_RDWR | DDI_DMA_CONSISTENT, callback, NULL,
3854 3855              &cookie, &ncookie) != DDI_DMA_MAPPED) {
3855 3856                  (void) ddi_dma_mem_free(&p->m_acc_hdl);
3856 3857                  ddi_dma_free_handle(&p->m_dma_hdl);
3857 3858                  p->m_dma_hdl = NULL;
3858 3859                  mptsas_log(mpt, CE_WARN, "Unable to bind DMA resources for"
3859 3860                      " extra SGL");
3860 3861                  return (DDI_FAILURE);
3861 3862          }
3862 3863  
3863 3864          /*
3864 3865           * Store the SGL memory address.  This chip uses this
3865 3866           * address to dma to and from the driver.  The second
3866 3867           * address is the address mpt uses to fill in the SGL.
3867 3868           */
3868 3869          p->m_phys_addr = cookie.dmac_address;
3869 3870  
3870 3871          return (DDI_SUCCESS);
3871 3872  }
3872 3873  
3873 3874  static void
3874 3875  mptsas_cache_frames_destructor(void *buf, void *cdrarg)
3875 3876  {
3876 3877  #ifndef __lock_lint
3877 3878          _NOTE(ARGUNUSED(cdrarg))
3878 3879  #endif
3879 3880          mptsas_cache_frames_t   *p = buf;
3880 3881          if (p->m_dma_hdl != NULL) {
3881 3882                  (void) ddi_dma_unbind_handle(p->m_dma_hdl);
3882 3883                  (void) ddi_dma_mem_free(&p->m_acc_hdl);
3883 3884                  ddi_dma_free_handle(&p->m_dma_hdl);
3884 3885                  p->m_phys_addr = NULL;
3885 3886                  p->m_frames_addr = NULL;
3886 3887                  p->m_dma_hdl = NULL;
3887 3888                  p->m_acc_hdl = NULL;
3888 3889          }
3889 3890  
3890 3891  }
3891 3892  
3892 3893  /*
3893 3894   * allocate and deallocate external pkt space (ie. not part of mptsas_cmd)
3894 3895   * for non-standard length cdb, pkt_private, status areas
3895 3896   * if allocation fails, then deallocate all external space and the pkt
3896 3897   */
3897 3898  /* ARGSUSED */
3898 3899  static int
3899 3900  mptsas_pkt_alloc_extern(mptsas_t *mpt, mptsas_cmd_t *cmd,
3900 3901      int cmdlen, int tgtlen, int statuslen, int kf)
3901 3902  {
3902 3903          caddr_t                 cdbp, scbp, tgt;
3903 3904          int                     (*callback)(caddr_t) = (kf == KM_SLEEP) ?
3904 3905              DDI_DMA_SLEEP : DDI_DMA_DONTWAIT;
3905 3906          struct scsi_address     ap;
3906 3907          size_t                  senselength;
3907 3908          ddi_dma_attr_t          ext_arq_dma_attr;
3908 3909          uint_t                  cookiec;
3909 3910  
3910 3911          NDBG3(("mptsas_pkt_alloc_extern: "
3911 3912              "cmd=0x%p cmdlen=%d tgtlen=%d statuslen=%d kf=%x",
3912 3913              (void *)cmd, cmdlen, tgtlen, statuslen, kf));
3913 3914  
3914 3915          tgt = cdbp = scbp = NULL;
3915 3916          cmd->cmd_scblen         = statuslen;
3916 3917          cmd->cmd_privlen        = (uchar_t)tgtlen;
3917 3918  
3918 3919          if (cmdlen > sizeof (cmd->cmd_cdb)) {
3919 3920                  if ((cdbp = kmem_zalloc((size_t)cmdlen, kf)) == NULL) {
3920 3921                          goto fail;
3921 3922                  }
3922 3923                  cmd->cmd_pkt->pkt_cdbp = (opaque_t)cdbp;
3923 3924                  cmd->cmd_flags |= CFLAG_CDBEXTERN;
3924 3925          }
3925 3926          if (tgtlen > PKT_PRIV_LEN) {
3926 3927                  if ((tgt = kmem_zalloc((size_t)tgtlen, kf)) == NULL) {
3927 3928                          goto fail;
3928 3929                  }
3929 3930                  cmd->cmd_flags |= CFLAG_PRIVEXTERN;
3930 3931                  cmd->cmd_pkt->pkt_private = tgt;
3931 3932          }
3932 3933          if (statuslen > EXTCMDS_STATUS_SIZE) {
3933 3934                  if ((scbp = kmem_zalloc((size_t)statuslen, kf)) == NULL) {
3934 3935                          goto fail;
3935 3936                  }
3936 3937                  cmd->cmd_flags |= CFLAG_SCBEXTERN;
3937 3938                  cmd->cmd_pkt->pkt_scbp = (opaque_t)scbp;
3938 3939  
3939 3940                  /* allocate sense data buf for DMA */
3940 3941  
3941 3942                  senselength = statuslen - MPTSAS_GET_ITEM_OFF(
3942 3943                      struct scsi_arq_status, sts_sensedata);
3943 3944                  cmd->cmd_rqslen = (uchar_t)senselength;
3944 3945  
3945 3946                  ap.a_hba_tran = mpt->m_tran;
3946 3947                  ap.a_target = 0;
3947 3948                  ap.a_lun = 0;
3948 3949  
3949 3950                  cmd->cmd_ext_arq_buf = scsi_alloc_consistent_buf(&ap,
3950 3951                      (struct buf *)NULL, senselength, B_READ,
3951 3952                      callback, NULL);
3952 3953  
3953 3954                  if (cmd->cmd_ext_arq_buf == NULL) {
3954 3955                          goto fail;
3955 3956                  }
3956 3957                  /*
3957 3958                   * allocate a extern arq handle and bind the buf
3958 3959                   */
3959 3960                  ext_arq_dma_attr = mpt->m_msg_dma_attr;
3960 3961                  ext_arq_dma_attr.dma_attr_sgllen = 1;
3961 3962                  if ((ddi_dma_alloc_handle(mpt->m_dip,
3962 3963                      &ext_arq_dma_attr, callback,
3963 3964                      NULL, &cmd->cmd_ext_arqhandle)) != DDI_SUCCESS) {
3964 3965                          goto fail;
3965 3966                  }
3966 3967  
3967 3968                  if (ddi_dma_buf_bind_handle(cmd->cmd_ext_arqhandle,
3968 3969                      cmd->cmd_ext_arq_buf, (DDI_DMA_READ | DDI_DMA_CONSISTENT),
3969 3970                      callback, NULL, &cmd->cmd_ext_arqcookie,
3970 3971                      &cookiec)
3971 3972                      != DDI_SUCCESS) {
3972 3973                          goto fail;
3973 3974                  }
3974 3975                  cmd->cmd_flags |= CFLAG_EXTARQBUFVALID;
3975 3976          }
3976 3977          return (0);
3977 3978  fail:
3978 3979          mptsas_pkt_destroy_extern(mpt, cmd);
3979 3980          return (1);
3980 3981  }
3981 3982  
3982 3983  /*
3983 3984   * deallocate external pkt space and deallocate the pkt
3984 3985   */
3985 3986  static void
3986 3987  mptsas_pkt_destroy_extern(mptsas_t *mpt, mptsas_cmd_t *cmd)
3987 3988  {
3988 3989          NDBG3(("mptsas_pkt_destroy_extern: cmd=0x%p", (void *)cmd));
3989 3990  
3990 3991          if (cmd->cmd_flags & CFLAG_FREE) {
3991 3992                  mptsas_log(mpt, CE_PANIC,
3992 3993                      "mptsas_pkt_destroy_extern: freeing free packet");
3993 3994                  _NOTE(NOT_REACHED)
3994 3995                  /* NOTREACHED */
3995 3996          }
3996 3997          if (cmd->cmd_flags & CFLAG_CDBEXTERN) {
3997 3998                  kmem_free(cmd->cmd_pkt->pkt_cdbp, (size_t)cmd->cmd_cdblen);
3998 3999          }
3999 4000          if (cmd->cmd_flags & CFLAG_SCBEXTERN) {
4000 4001                  kmem_free(cmd->cmd_pkt->pkt_scbp, (size_t)cmd->cmd_scblen);
4001 4002                  if (cmd->cmd_flags & CFLAG_EXTARQBUFVALID) {
4002 4003                          (void) ddi_dma_unbind_handle(cmd->cmd_ext_arqhandle);
4003 4004                  }
4004 4005                  if (cmd->cmd_ext_arqhandle) {
4005 4006                          ddi_dma_free_handle(&cmd->cmd_ext_arqhandle);
4006 4007                          cmd->cmd_ext_arqhandle = NULL;
4007 4008                  }
4008 4009                  if (cmd->cmd_ext_arq_buf)
4009 4010                          scsi_free_consistent_buf(cmd->cmd_ext_arq_buf);
4010 4011          }
4011 4012          if (cmd->cmd_flags & CFLAG_PRIVEXTERN) {
4012 4013                  kmem_free(cmd->cmd_pkt->pkt_private, (size_t)cmd->cmd_privlen);
4013 4014          }
4014 4015          cmd->cmd_flags = CFLAG_FREE;
4015 4016          kmem_cache_free(mpt->m_kmem_cache, (void *)cmd);
4016 4017  }
4017 4018  
4018 4019  /*
4019 4020   * tran_sync_pkt(9E) - explicit DMA synchronization
4020 4021   */
4021 4022  /*ARGSUSED*/
4022 4023  static void
4023 4024  mptsas_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
4024 4025  {
4025 4026          mptsas_cmd_t    *cmd = PKT2CMD(pkt);
4026 4027  
4027 4028          NDBG3(("mptsas_scsi_sync_pkt: target=%d, pkt=0x%p",
4028 4029              ap->a_target, (void *)pkt));
4029 4030  
4030 4031          if (cmd->cmd_dmahandle) {
4031 4032                  (void) ddi_dma_sync(cmd->cmd_dmahandle, 0, 0,
4032 4033                      (cmd->cmd_flags & CFLAG_DMASEND) ?
4033 4034                      DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU);
4034 4035          }
4035 4036  }
4036 4037  
4037 4038  /*
4038 4039   * tran_dmafree(9E) - deallocate DMA resources allocated for command
4039 4040   */
4040 4041  /*ARGSUSED*/
4041 4042  static void
4042 4043  mptsas_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
4043 4044  {
4044 4045          mptsas_cmd_t    *cmd = PKT2CMD(pkt);
4045 4046          mptsas_t        *mpt = ADDR2MPT(ap);
4046 4047  
4047 4048          NDBG3(("mptsas_scsi_dmafree: target=%d pkt=0x%p",
4048 4049              ap->a_target, (void *)pkt));
4049 4050  
4050 4051          if (cmd->cmd_flags & CFLAG_DMAVALID) {
4051 4052                  (void) ddi_dma_unbind_handle(cmd->cmd_dmahandle);
4052 4053                  cmd->cmd_flags &= ~CFLAG_DMAVALID;
4053 4054          }
4054 4055  
4055 4056          if (cmd->cmd_flags & CFLAG_EXTARQBUFVALID) {
4056 4057                  (void) ddi_dma_unbind_handle(cmd->cmd_ext_arqhandle);
4057 4058                  cmd->cmd_flags &= ~CFLAG_EXTARQBUFVALID;
4058 4059          }
4059 4060  
4060 4061          mptsas_free_extra_sgl_frame(mpt, cmd);
4061 4062  }
4062 4063  
4063 4064  static void
4064 4065  mptsas_pkt_comp(struct scsi_pkt *pkt, mptsas_cmd_t *cmd)
4065 4066  {
4066 4067          if ((cmd->cmd_flags & CFLAG_CMDIOPB) &&
4067 4068              (!(cmd->cmd_flags & CFLAG_DMASEND))) {
4068 4069                  (void) ddi_dma_sync(cmd->cmd_dmahandle, 0, 0,
4069 4070                      DDI_DMA_SYNC_FORCPU);
4070 4071          }
4071 4072          (*pkt->pkt_comp)(pkt);
4072 4073  }
4073 4074  
4074 4075  static void
4075 4076  mptsas_sge_setup(mptsas_t *mpt, mptsas_cmd_t *cmd, uint32_t *control,
4076 4077          pMpi2SCSIIORequest_t frame, ddi_acc_handle_t acc_hdl)
4077 4078  {
4078 4079          uint_t                  cookiec;
4079 4080          mptti_t                 *dmap;
4080 4081          uint32_t                flags;
4081 4082          pMpi2SGESimple64_t      sge;
4082 4083          pMpi2SGEChain64_t       sgechain;
4083 4084          ASSERT(cmd->cmd_flags & CFLAG_DMAVALID);
4084 4085  
4085 4086          /*
4086 4087           * Save the number of entries in the DMA
4087 4088           * Scatter/Gather list
4088 4089           */
4089 4090          cookiec = cmd->cmd_cookiec;
4090 4091  
4091 4092          NDBG1(("mptsas_sge_setup: cookiec=%d", cookiec));
4092 4093  
4093 4094          /*
4094 4095           * Set read/write bit in control.
4095 4096           */
4096 4097          if (cmd->cmd_flags & CFLAG_DMASEND) {
4097 4098                  *control |= MPI2_SCSIIO_CONTROL_WRITE;
4098 4099          } else {
4099 4100                  *control |= MPI2_SCSIIO_CONTROL_READ;
4100 4101          }
4101 4102  
4102 4103          ddi_put32(acc_hdl, &frame->DataLength, cmd->cmd_dmacount);
4103 4104  
4104 4105          /*
4105 4106           * We have 2 cases here.  First where we can fit all the
4106 4107           * SG elements into the main frame, and the case
4107 4108           * where we can't.
4108 4109           * If we have more cookies than we can attach to a frame
4109 4110           * we will need to use a chain element to point
4110 4111           * a location of memory where the rest of the S/G
4111 4112           * elements reside.
4112 4113           */
4113 4114          if (cookiec <= MPTSAS_MAX_FRAME_SGES64(mpt)) {
4114 4115                  dmap = cmd->cmd_sg;
4115 4116                  sge = (pMpi2SGESimple64_t)(&frame->SGL);
4116 4117                  while (cookiec--) {
4117 4118                          ddi_put32(acc_hdl,
4118 4119                              &sge->Address.Low, dmap->addr.address64.Low);
4119 4120                          ddi_put32(acc_hdl,
4120 4121                              &sge->Address.High, dmap->addr.address64.High);
4121 4122                          ddi_put32(acc_hdl, &sge->FlagsLength,
4122 4123                              dmap->count);
4123 4124                          flags = ddi_get32(acc_hdl, &sge->FlagsLength);
4124 4125                          flags |= ((uint32_t)
4125 4126                              (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
4126 4127                              MPI2_SGE_FLAGS_SYSTEM_ADDRESS |
4127 4128                              MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
4128 4129                              MPI2_SGE_FLAGS_SHIFT);
4129 4130  
4130 4131                          /*
4131 4132                           * If this is the last cookie, we set the flags
4132 4133                           * to indicate so
4133 4134                           */
4134 4135                          if (cookiec == 0) {
4135 4136                                  flags |=
4136 4137                                      ((uint32_t)(MPI2_SGE_FLAGS_LAST_ELEMENT
4137 4138                                      | MPI2_SGE_FLAGS_END_OF_BUFFER
4138 4139                                      | MPI2_SGE_FLAGS_END_OF_LIST) <<
4139 4140                                      MPI2_SGE_FLAGS_SHIFT);
4140 4141                          }
4141 4142                          if (cmd->cmd_flags & CFLAG_DMASEND) {
4142 4143                                  flags |= (MPI2_SGE_FLAGS_HOST_TO_IOC <<
4143 4144                                      MPI2_SGE_FLAGS_SHIFT);
4144 4145                          } else {
4145 4146                                  flags |= (MPI2_SGE_FLAGS_IOC_TO_HOST <<
4146 4147                                      MPI2_SGE_FLAGS_SHIFT);
4147 4148                          }
4148 4149                          ddi_put32(acc_hdl, &sge->FlagsLength, flags);
4149 4150                          dmap++;
4150 4151                          sge++;
4151 4152                  }
4152 4153          } else {
4153 4154                  /*
4154 4155                   * Hereby we start to deal with multiple frames.
4155 4156                   * The process is as follows:
4156 4157                   * 1. Determine how many frames are needed for SGL element
4157 4158                   *    storage; Note that all frames are stored in contiguous
4158 4159                   *    memory space and in 64-bit DMA mode each element is
4159 4160                   *    3 double-words (12 bytes) long.
4160 4161                   * 2. Fill up the main frame. We need to do this separately
4161 4162                   *    since it contains the SCSI IO request header and needs
4162 4163                   *    dedicated processing. Note that the last 4 double-words
4163 4164                   *    of the SCSI IO header is for SGL element storage
4164 4165                   *    (MPI2_SGE_IO_UNION).
4165 4166                   * 3. Fill the chain element in the main frame, so the DMA
4166 4167                   *    engine can use the following frames.
4167 4168                   * 4. Enter a loop to fill the remaining frames. Note that the
4168 4169                   *    last frame contains no chain element.  The remaining
4169 4170                   *    frames go into the mpt SGL buffer allocated on the fly,
4170 4171                   *    not immediately following the main message frame, as in
4171 4172                   *    Gen1.
4172 4173                   * Some restrictions:
4173 4174                   * 1. For 64-bit DMA, the simple element and chain element
4174 4175                   *    are both of 3 double-words (12 bytes) in size, even
4175 4176                   *    though all frames are stored in the first 4G of mem
4176 4177                   *    range and the higher 32-bits of the address are always 0.
4177 4178                   * 2. On some controllers (like the 1064/1068), a frame can
4178 4179                   *    hold SGL elements with the last 1 or 2 double-words
4179 4180                   *    (4 or 8 bytes) un-used. On these controllers, we should
4180 4181                   *    recognize that there's not enough room for another SGL
4181 4182                   *    element and move the sge pointer to the next frame.
4182 4183                   */
4183 4184                  int             i, j, k, l, frames, sgemax;
4184 4185                  int             temp;
4185 4186                  uint8_t         chainflags;
4186 4187                  uint16_t        chainlength;
4187 4188                  mptsas_cache_frames_t *p;
4188 4189  
4189 4190                  /*
4190 4191                   * Sgemax is the number of SGE's that will fit
4191 4192                   * each extra frame and frames is total
4192 4193                   * number of frames we'll need.  1 sge entry per
4193 4194                   * frame is reseverd for the chain element thus the -1 below.
4194 4195                   */
4195 4196                  sgemax = ((mpt->m_req_frame_size / sizeof (MPI2_SGE_SIMPLE64))
4196 4197                      - 1);
4197 4198                  temp = (cookiec - (MPTSAS_MAX_FRAME_SGES64(mpt) - 1)) / sgemax;
4198 4199  
4199 4200                  /*
4200 4201                   * A little check to see if we need to round up the number
4201 4202                   * of frames we need
4202 4203                   */
4203 4204                  if ((cookiec - (MPTSAS_MAX_FRAME_SGES64(mpt) - 1)) - (temp *
4204 4205                      sgemax) > 1) {
4205 4206                          frames = (temp + 1);
4206 4207                  } else {
4207 4208                          frames = temp;
4208 4209                  }
4209 4210                  dmap = cmd->cmd_sg;
4210 4211                  sge = (pMpi2SGESimple64_t)(&frame->SGL);
4211 4212  
4212 4213                  /*
4213 4214                   * First fill in the main frame
4214 4215                   */
4215 4216                  for (j = 1; j < MPTSAS_MAX_FRAME_SGES64(mpt); j++) {
4216 4217                          ddi_put32(acc_hdl, &sge->Address.Low,
4217 4218                              dmap->addr.address64.Low);
4218 4219                          ddi_put32(acc_hdl, &sge->Address.High,
4219 4220                              dmap->addr.address64.High);
4220 4221                          ddi_put32(acc_hdl, &sge->FlagsLength, dmap->count);
4221 4222                          flags = ddi_get32(acc_hdl, &sge->FlagsLength);
4222 4223                          flags |= ((uint32_t)(MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
4223 4224                              MPI2_SGE_FLAGS_SYSTEM_ADDRESS |
4224 4225                              MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
4225 4226                              MPI2_SGE_FLAGS_SHIFT);
4226 4227  
4227 4228                          /*
4228 4229                           * If this is the last SGE of this frame
4229 4230                           * we set the end of list flag
4230 4231                           */
4231 4232                          if (j == (MPTSAS_MAX_FRAME_SGES64(mpt) - 1)) {
4232 4233                                  flags |= ((uint32_t)
4233 4234                                      (MPI2_SGE_FLAGS_LAST_ELEMENT) <<
4234 4235                                      MPI2_SGE_FLAGS_SHIFT);
4235 4236                          }
4236 4237                          if (cmd->cmd_flags & CFLAG_DMASEND) {
4237 4238                                  flags |=
4238 4239                                      (MPI2_SGE_FLAGS_HOST_TO_IOC <<
4239 4240                                      MPI2_SGE_FLAGS_SHIFT);
4240 4241                          } else {
4241 4242                                  flags |=
4242 4243                                      (MPI2_SGE_FLAGS_IOC_TO_HOST <<
4243 4244                                      MPI2_SGE_FLAGS_SHIFT);
4244 4245                          }
4245 4246                          ddi_put32(acc_hdl, &sge->FlagsLength, flags);
4246 4247                          dmap++;
4247 4248                          sge++;
4248 4249                  }
4249 4250  
4250 4251                  /*
4251 4252                   * Fill in the chain element in the main frame.
4252 4253                   * About calculation on ChainOffset:
4253 4254                   * 1. Struct msg_scsi_io_request has 4 double-words (16 bytes)
4254 4255                   *    in the end reserved for SGL element storage
4255 4256                   *    (MPI2_SGE_IO_UNION); we should count it in our
4256 4257                   *    calculation.  See its definition in the header file.
4257 4258                   * 2. Constant j is the counter of the current SGL element
4258 4259                   *    that will be processed, and (j - 1) is the number of
4259 4260                   *    SGL elements that have been processed (stored in the
4260 4261                   *    main frame).
4261 4262                   * 3. ChainOffset value should be in units of double-words (4
4262 4263                   *    bytes) so the last value should be divided by 4.
4263 4264                   */
4264 4265                  ddi_put8(acc_hdl, &frame->ChainOffset,
4265 4266                      (sizeof (MPI2_SCSI_IO_REQUEST) -
4266 4267                      sizeof (MPI2_SGE_IO_UNION) +
4267 4268                      (j - 1) * sizeof (MPI2_SGE_SIMPLE64)) >> 2);
4268 4269                  sgechain = (pMpi2SGEChain64_t)sge;
4269 4270                  chainflags = (MPI2_SGE_FLAGS_CHAIN_ELEMENT |
4270 4271                      MPI2_SGE_FLAGS_SYSTEM_ADDRESS |
4271 4272                      MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
4272 4273                  ddi_put8(acc_hdl, &sgechain->Flags, chainflags);
4273 4274  
4274 4275                  /*
4275 4276                   * The size of the next frame is the accurate size of space
4276 4277                   * (in bytes) used to store the SGL elements. j is the counter
4277 4278                   * of SGL elements. (j - 1) is the number of SGL elements that
4278 4279                   * have been processed (stored in frames).
4279 4280                   */
4280 4281                  if (frames >= 2) {
4281 4282                          chainlength = mpt->m_req_frame_size /
4282 4283                              sizeof (MPI2_SGE_SIMPLE64) *
4283 4284                              sizeof (MPI2_SGE_SIMPLE64);
4284 4285                  } else {
4285 4286                          chainlength = ((cookiec - (j - 1)) *
4286 4287                              sizeof (MPI2_SGE_SIMPLE64));
4287 4288                  }
4288 4289  
4289 4290                  p = cmd->cmd_extra_frames;
4290 4291  
4291 4292                  ddi_put16(acc_hdl, &sgechain->Length, chainlength);
4292 4293                  ddi_put32(acc_hdl, &sgechain->Address.Low,
4293 4294                      p->m_phys_addr);
4294 4295                  /* SGL is allocated in the first 4G mem range */
4295 4296                  ddi_put32(acc_hdl, &sgechain->Address.High, 0);
4296 4297  
4297 4298                  /*
4298 4299                   * If there are more than 2 frames left we have to
4299 4300                   * fill in the next chain offset to the location of
4300 4301                   * the chain element in the next frame.
4301 4302                   * sgemax is the number of simple elements in an extra
4302 4303                   * frame. Note that the value NextChainOffset should be
4303 4304                   * in double-words (4 bytes).
4304 4305                   */
4305 4306                  if (frames >= 2) {
4306 4307                          ddi_put8(acc_hdl, &sgechain->NextChainOffset,
4307 4308                              (sgemax * sizeof (MPI2_SGE_SIMPLE64)) >> 2);
4308 4309                  } else {
4309 4310                          ddi_put8(acc_hdl, &sgechain->NextChainOffset, 0);
4310 4311                  }
4311 4312  
4312 4313                  /*
4313 4314                   * Jump to next frame;
4314 4315                   * Starting here, chain buffers go into the per command SGL.
4315 4316                   * This buffer is allocated when chain buffers are needed.
4316 4317                   */
4317 4318                  sge = (pMpi2SGESimple64_t)p->m_frames_addr;
4318 4319                  i = cookiec;
4319 4320  
4320 4321                  /*
4321 4322                   * Start filling in frames with SGE's.  If we
4322 4323                   * reach the end of frame and still have SGE's
4323 4324                   * to fill we need to add a chain element and
4324 4325                   * use another frame.  j will be our counter
4325 4326                   * for what cookie we are at and i will be
4326 4327                   * the total cookiec. k is the current frame
4327 4328                   */
4328 4329                  for (k = 1; k <= frames; k++) {
4329 4330                          for (l = 1; (l <= (sgemax + 1)) && (j <= i); j++, l++) {
4330 4331  
4331 4332                                  /*
4332 4333                                   * If we have reached the end of frame
4333 4334                                   * and we have more SGE's to fill in
4334 4335                                   * we have to fill the final entry
4335 4336                                   * with a chain element and then
4336 4337                                   * continue to the next frame
4337 4338                                   */
4338 4339                                  if ((l == (sgemax + 1)) && (k != frames)) {
4339 4340                                          sgechain = (pMpi2SGEChain64_t)sge;
4340 4341                                          j--;
4341 4342                                          chainflags = (
4342 4343                                              MPI2_SGE_FLAGS_CHAIN_ELEMENT |
4343 4344                                              MPI2_SGE_FLAGS_SYSTEM_ADDRESS |
4344 4345                                              MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
4345 4346                                          ddi_put8(p->m_acc_hdl,
4346 4347                                              &sgechain->Flags, chainflags);
4347 4348                                          /*
4348 4349                                           * k is the frame counter and (k + 1)
4349 4350                                           * is the number of the next frame.
4350 4351                                           * Note that frames are in contiguous
4351 4352                                           * memory space.
4352 4353                                           */
4353 4354                                          ddi_put32(p->m_acc_hdl,
4354 4355                                              &sgechain->Address.Low,
4355 4356                                              (p->m_phys_addr +
4356 4357                                              (mpt->m_req_frame_size * k)));
4357 4358                                          ddi_put32(p->m_acc_hdl,
4358 4359                                              &sgechain->Address.High, 0);
4359 4360  
4360 4361                                          /*
4361 4362                                           * If there are more than 2 frames left
4362 4363                                           * we have to next chain offset to
4363 4364                                           * the location of the chain element
4364 4365                                           * in the next frame and fill in the
4365 4366                                           * length of the next chain
4366 4367                                           */
4367 4368                                          if ((frames - k) >= 2) {
4368 4369                                                  ddi_put8(p->m_acc_hdl,
4369 4370                                                      &sgechain->NextChainOffset,
4370 4371                                                      (sgemax *
4371 4372                                                      sizeof (MPI2_SGE_SIMPLE64))
4372 4373                                                      >> 2);
4373 4374                                                  ddi_put16(p->m_acc_hdl,
4374 4375                                                      &sgechain->Length,
4375 4376                                                      mpt->m_req_frame_size /
4376 4377                                                      sizeof (MPI2_SGE_SIMPLE64) *
4377 4378                                                      sizeof (MPI2_SGE_SIMPLE64));
4378 4379                                          } else {
4379 4380                                                  /*
4380 4381                                                   * This is the last frame. Set
4381 4382                                                   * the NextChainOffset to 0 and
4382 4383                                                   * Length is the total size of
4383 4384                                                   * all remaining simple elements
4384 4385                                                   */
4385 4386                                                  ddi_put8(p->m_acc_hdl,
4386 4387                                                      &sgechain->NextChainOffset,
4387 4388                                                      0);
4388 4389                                                  ddi_put16(p->m_acc_hdl,
4389 4390                                                      &sgechain->Length,
4390 4391                                                      (cookiec - j) *
4391 4392                                                      sizeof (MPI2_SGE_SIMPLE64));
4392 4393                                          }
4393 4394  
4394 4395                                          /* Jump to the next frame */
4395 4396                                          sge = (pMpi2SGESimple64_t)
4396 4397                                              ((char *)p->m_frames_addr +
4397 4398                                              (int)mpt->m_req_frame_size * k);
4398 4399  
4399 4400                                          continue;
4400 4401                                  }
4401 4402  
4402 4403                                  ddi_put32(p->m_acc_hdl,
4403 4404                                      &sge->Address.Low,
4404 4405                                      dmap->addr.address64.Low);
4405 4406                                  ddi_put32(p->m_acc_hdl,
4406 4407                                      &sge->Address.High,
4407 4408                                      dmap->addr.address64.High);
4408 4409                                  ddi_put32(p->m_acc_hdl,
4409 4410                                      &sge->FlagsLength, dmap->count);
4410 4411                                  flags = ddi_get32(p->m_acc_hdl,
4411 4412                                      &sge->FlagsLength);
4412 4413                                  flags |= ((uint32_t)(
4413 4414                                      MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
4414 4415                                      MPI2_SGE_FLAGS_SYSTEM_ADDRESS |
4415 4416                                      MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
4416 4417                                      MPI2_SGE_FLAGS_SHIFT);
4417 4418  
4418 4419                                  /*
4419 4420                                   * If we are at the end of the frame and
4420 4421                                   * there is another frame to fill in
4421 4422                                   * we set the last simple element as last
4422 4423                                   * element
4423 4424                                   */
4424 4425                                  if ((l == sgemax) && (k != frames)) {
4425 4426                                          flags |= ((uint32_t)
4426 4427                                              (MPI2_SGE_FLAGS_LAST_ELEMENT) <<
4427 4428                                              MPI2_SGE_FLAGS_SHIFT);
4428 4429                                  }
4429 4430  
4430 4431                                  /*
4431 4432                                   * If this is the final cookie we
4432 4433                                   * indicate it by setting the flags
4433 4434                                   */
4434 4435                                  if (j == i) {
4435 4436                                          flags |= ((uint32_t)
4436 4437                                              (MPI2_SGE_FLAGS_LAST_ELEMENT |
4437 4438                                              MPI2_SGE_FLAGS_END_OF_BUFFER |
4438 4439                                              MPI2_SGE_FLAGS_END_OF_LIST) <<
4439 4440                                              MPI2_SGE_FLAGS_SHIFT);
4440 4441                                  }
4441 4442                                  if (cmd->cmd_flags & CFLAG_DMASEND) {
4442 4443                                          flags |=
4443 4444                                              (MPI2_SGE_FLAGS_HOST_TO_IOC <<
4444 4445                                              MPI2_SGE_FLAGS_SHIFT);
4445 4446                                  } else {
4446 4447                                          flags |=
4447 4448                                              (MPI2_SGE_FLAGS_IOC_TO_HOST <<
4448 4449                                              MPI2_SGE_FLAGS_SHIFT);
4449 4450                                  }
4450 4451                                  ddi_put32(p->m_acc_hdl,
4451 4452                                      &sge->FlagsLength, flags);
4452 4453                                  dmap++;
4453 4454                                  sge++;
4454 4455                          }
4455 4456                  }
4456 4457  
4457 4458                  /*
4458 4459                   * Sync DMA with the chain buffers that were just created
4459 4460                   */
4460 4461                  (void) ddi_dma_sync(p->m_dma_hdl, 0, 0, DDI_DMA_SYNC_FORDEV);
4461 4462          }
4462 4463  }
4463 4464  
4464 4465  /*
4465 4466   * Interrupt handling
4466 4467   * Utility routine.  Poll for status of a command sent to HBA
4467 4468   * without interrupts (a FLAG_NOINTR command).
4468 4469   */
4469 4470  int
4470 4471  mptsas_poll(mptsas_t *mpt, mptsas_cmd_t *poll_cmd, int polltime)
4471 4472  {
4472 4473          int     rval = TRUE;
4473 4474  
4474 4475          NDBG5(("mptsas_poll: cmd=0x%p", (void *)poll_cmd));
4475 4476  
4476 4477          /*
4477 4478           * In order to avoid using m_mutex in ISR(a new separate mutex
4478 4479           * m_intr_mutex is introduced) and keep the same lock logic,
4479 4480           * the m_intr_mutex should be used to protect the getting and
4480 4481           * setting of the ReplyDescriptorIndex.
4481 4482           *
4482 4483           * Since the m_intr_mutex would be released during processing the poll
4483 4484           * cmd, so we should set the poll flag earlier here to make sure the
4484 4485           * polled cmd be handled in this thread/context. A side effect is other
4485 4486           * cmds during the period between the flag set and reset are also
4486 4487           * handled in this thread and not the ISR. Since the poll cmd is not
4487 4488           * so common, so the performance degradation in this case is not a big
4488 4489           * issue.
4489 4490           */
4490 4491          mutex_enter(&mpt->m_intr_mutex);
4491 4492          mpt->m_polled_intr = 1;
4492 4493          mutex_exit(&mpt->m_intr_mutex);
4493 4494  
4494 4495          if ((poll_cmd->cmd_flags & CFLAG_TM_CMD) == 0) {
4495 4496                  mptsas_restart_hba(mpt);
4496 4497          }
4497 4498  
4498 4499          /*
4499 4500           * Wait, using drv_usecwait(), long enough for the command to
4500 4501           * reasonably return from the target if the target isn't
4501 4502           * "dead".  A polled command may well be sent from scsi_poll, and
4502 4503           * there are retries built in to scsi_poll if the transport
4503 4504           * accepted the packet (TRAN_ACCEPT).  scsi_poll waits 1 second
4504 4505           * and retries the transport up to scsi_poll_busycnt times
4505 4506           * (currently 60) if
4506 4507           * 1. pkt_reason is CMD_INCOMPLETE and pkt_state is 0, or
4507 4508           * 2. pkt_reason is CMD_CMPLT and *pkt_scbp has STATUS_BUSY
4508 4509           *
4509 4510           * limit the waiting to avoid a hang in the event that the
4510 4511           * cmd never gets started but we are still receiving interrupts
4511 4512           */
4512 4513          while (!(poll_cmd->cmd_flags & CFLAG_FINISHED)) {
4513 4514                  if (mptsas_wait_intr(mpt, polltime) == FALSE) {
4514 4515                          NDBG5(("mptsas_poll: command incomplete"));
4515 4516                          rval = FALSE;
4516 4517                          break;
4517 4518                  }
4518 4519          }
4519 4520  
4520 4521          mutex_enter(&mpt->m_intr_mutex);
4521 4522          mpt->m_polled_intr = 0;
4522 4523          mutex_exit(&mpt->m_intr_mutex);
4523 4524  
4524 4525          if (rval == FALSE) {
4525 4526  
4526 4527                  /*
4527 4528                   * this isn't supposed to happen, the hba must be wedged
4528 4529                   * Mark this cmd as a timeout.
4529 4530                   */
4530 4531                  mptsas_set_pkt_reason(mpt, poll_cmd, CMD_TIMEOUT,
4531 4532                      (STAT_TIMEOUT|STAT_ABORTED));
4532 4533  
4533 4534                  if (poll_cmd->cmd_queued == FALSE) {
4534 4535  
4535 4536                          NDBG5(("mptsas_poll: not on waitq"));
4536 4537  
4537 4538                          poll_cmd->cmd_pkt->pkt_state |=
4538 4539                              (STATE_GOT_BUS|STATE_GOT_TARGET|STATE_SENT_CMD);
4539 4540                  } else {
4540 4541  
4541 4542                          /* find and remove it from the waitq */
4542 4543                          NDBG5(("mptsas_poll: delete from waitq"));
4543 4544                          mptsas_waitq_delete(mpt, poll_cmd);
4544 4545                  }
4545 4546  
4546 4547          }
4547 4548          mptsas_fma_check(mpt, poll_cmd);
4548 4549          NDBG5(("mptsas_poll: done"));
4549 4550          return (rval);
4550 4551  }
4551 4552  
4552 4553  /*
4553 4554   * Used for polling cmds and TM function
4554 4555   */
4555 4556  static int
4556 4557  mptsas_wait_intr(mptsas_t *mpt, int polltime)
4557 4558  {
4558 4559          int                             cnt;
4559 4560          pMpi2ReplyDescriptorsUnion_t    reply_desc_union;
4560 4561          Mpi2ReplyDescriptorsUnion_t     reply_desc_union_v;
4561 4562          uint32_t                        int_mask;
4562 4563          uint8_t reply_type;
4563 4564  
4564 4565          NDBG5(("mptsas_wait_intr"));
4565 4566  
4566 4567  
4567 4568          /*
4568 4569           * Get the current interrupt mask and disable interrupts.  When
4569 4570           * re-enabling ints, set mask to saved value.
4570 4571           */
4571 4572          int_mask = ddi_get32(mpt->m_datap, &mpt->m_reg->HostInterruptMask);
4572 4573          MPTSAS_DISABLE_INTR(mpt);
4573 4574  
4574 4575          /*
4575 4576           * Keep polling for at least (polltime * 1000) seconds
4576 4577           */
4577 4578          for (cnt = 0; cnt < polltime; cnt++) {
4578 4579                  mutex_enter(&mpt->m_intr_mutex);
4579 4580                  (void) ddi_dma_sync(mpt->m_dma_post_queue_hdl, 0, 0,
4580 4581                      DDI_DMA_SYNC_FORCPU);
4581 4582  
4582 4583                  reply_desc_union = (pMpi2ReplyDescriptorsUnion_t)
4583 4584                      MPTSAS_GET_NEXT_REPLY(mpt, mpt->m_post_index);
4584 4585  
4585 4586                  if (ddi_get32(mpt->m_acc_post_queue_hdl,
4586 4587                      &reply_desc_union->Words.Low) == 0xFFFFFFFF ||
4587 4588                      ddi_get32(mpt->m_acc_post_queue_hdl,
4588 4589                      &reply_desc_union->Words.High) == 0xFFFFFFFF) {
4589 4590                          mutex_exit(&mpt->m_intr_mutex);
4590 4591                          drv_usecwait(1000);
4591 4592                          continue;
4592 4593                  }
4593 4594  
4594 4595                  reply_type = ddi_get8(mpt->m_acc_post_queue_hdl,
4595 4596                      &reply_desc_union->Default.ReplyFlags);
4596 4597                  reply_type &= MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
4597 4598                  reply_desc_union_v.Default.ReplyFlags = reply_type;
4598 4599                  if (reply_type == MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
4599 4600                          reply_desc_union_v.SCSIIOSuccess.SMID =
4600 4601                              ddi_get16(mpt->m_acc_post_queue_hdl,
4601 4602                              &reply_desc_union->SCSIIOSuccess.SMID);
4602 4603                  } else if (reply_type ==
4603 4604                      MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
4604 4605                          reply_desc_union_v.AddressReply.ReplyFrameAddress =
4605 4606                              ddi_get32(mpt->m_acc_post_queue_hdl,
4606 4607                              &reply_desc_union->AddressReply.ReplyFrameAddress);
4607 4608                          reply_desc_union_v.AddressReply.SMID =
4608 4609                              ddi_get16(mpt->m_acc_post_queue_hdl,
4609 4610                              &reply_desc_union->AddressReply.SMID);
4610 4611                  }
4611 4612                  /*
4612 4613                   * Clear the reply descriptor for re-use and increment
4613 4614                   * index.
4614 4615                   */
4615 4616                  ddi_put64(mpt->m_acc_post_queue_hdl,
4616 4617                      &((uint64_t *)(void *)mpt->m_post_queue)[mpt->m_post_index],
4617 4618                      0xFFFFFFFFFFFFFFFF);
4618 4619                  (void) ddi_dma_sync(mpt->m_dma_post_queue_hdl, 0, 0,
4619 4620                      DDI_DMA_SYNC_FORDEV);
4620 4621  
4621 4622                  if (++mpt->m_post_index == mpt->m_post_queue_depth) {
4622 4623                          mpt->m_post_index = 0;
4623 4624                  }
4624 4625  
4625 4626                  /*
4626 4627                   * Update the global reply index
4627 4628                   */
4628 4629                  ddi_put32(mpt->m_datap,
4629 4630                      &mpt->m_reg->ReplyPostHostIndex, mpt->m_post_index);
4630 4631                  mutex_exit(&mpt->m_intr_mutex);
4631 4632  
4632 4633                  /*
4633 4634                   * The reply is valid, process it according to its
4634 4635                   * type.
4635 4636                   */
4636 4637                  mptsas_process_intr(mpt, &reply_desc_union_v);
4637 4638  
4638 4639  
4639 4640                  /*
4640 4641                   * Re-enable interrupts and quit.
4641 4642                   */
4642 4643                  ddi_put32(mpt->m_datap, &mpt->m_reg->HostInterruptMask,
4643 4644                      int_mask);
4644 4645                  return (TRUE);
4645 4646  
4646 4647          }
4647 4648  
4648 4649          /*
4649 4650           * Clear polling flag, re-enable interrupts and quit.
4650 4651           */
4651 4652          ddi_put32(mpt->m_datap, &mpt->m_reg->HostInterruptMask, int_mask);
4652 4653          return (FALSE);
4653 4654  }
4654 4655  
4655 4656  /*
4656 4657   * For fastpath, the m_intr_mutex should be held from the begining to the end,
4657 4658   * so we only treat those cmds that need not release m_intr_mutex(even just for
4658 4659   * a moment) as candidate for fast processing. otherwise, we don't handle them
4659 4660   * and just return, then in ISR, those cmds would be handled later with m_mutex
4660 4661   * held and m_intr_mutex not held.
4661 4662   */
4662 4663  static int
4663 4664  mptsas_handle_io_fastpath(mptsas_t *mpt,
4664 4665      uint16_t SMID)
4665 4666  {
4666 4667          mptsas_slots_t                          *slots = mpt->m_active;
4667 4668          mptsas_cmd_t                            *cmd = NULL;
4668 4669          struct scsi_pkt                         *pkt;
4669 4670  
4670 4671          /*
4671 4672           * This is a success reply so just complete the IO.  First, do a sanity
4672 4673           * check on the SMID.  The final slot is used for TM requests, which
4673 4674           * would not come into this reply handler.
4674 4675           */
4675 4676          if ((SMID == 0) || (SMID > slots->m_n_slots)) {
4676 4677                  mptsas_log(mpt, CE_WARN, "?Received invalid SMID of %d\n",
4677 4678                      SMID);
4678 4679                  ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
4679 4680                  return (TRUE);
4680 4681          }
4681 4682  
4682 4683          cmd = slots->m_slot[SMID];
4683 4684  
4684 4685          /*
4685 4686           * print warning and return if the slot is empty
4686 4687           */
4687 4688          if (cmd == NULL) {
4688 4689                  mptsas_log(mpt, CE_WARN, "?NULL command for successful SCSI IO "
4689 4690                      "in slot %d", SMID);
4690 4691                  return (TRUE);
4691 4692          }
4692 4693  
4693 4694          pkt = CMD2PKT(cmd);
4694 4695          pkt->pkt_state |= (STATE_GOT_BUS | STATE_GOT_TARGET | STATE_SENT_CMD |
4695 4696              STATE_GOT_STATUS);
4696 4697          if (cmd->cmd_flags & CFLAG_DMAVALID) {
4697 4698                  pkt->pkt_state |= STATE_XFERRED_DATA;
4698 4699          }
4699 4700          pkt->pkt_resid = 0;
4700 4701  
4701 4702          /*
4702 4703           * If the cmd is a IOC, or a passthrough, then we don't process it in
4703 4704           * fastpath, and later it would be handled by mptsas_process_intr()
4704 4705           * with m_mutex protected.
4705 4706           */
4706 4707          if (cmd->cmd_flags & (CFLAG_PASSTHRU | CFLAG_CMDIOC)) {
4707 4708                  return (FALSE);
4708 4709          } else {
4709 4710                  mptsas_remove_cmd0(mpt, cmd);
4710 4711          }
4711 4712  
4712 4713          if (cmd->cmd_flags & CFLAG_RETRY) {
4713 4714                  /*
4714 4715                   * The target returned QFULL or busy, do not add tihs
4715 4716                   * pkt to the doneq since the hba will retry
4716 4717                   * this cmd.
4717 4718                   *
4718 4719                   * The pkt has already been resubmitted in
4719 4720                   * mptsas_handle_qfull() or in mptsas_check_scsi_io_error().
4720 4721                   * Remove this cmd_flag here.
4721 4722                   */
4722 4723                  cmd->cmd_flags &= ~CFLAG_RETRY;
4723 4724          } else {
4724 4725                  mptsas_doneq_add0(mpt, cmd);
4725 4726          }
4726 4727  
4727 4728          /*
4728 4729           * In fastpath, the cmd should only be a context reply, so just check
4729 4730           * the post queue of the reply descriptor and the dmahandle of the cmd
4730 4731           * is enough. No sense data in this case and no need to check the dma
4731 4732           * handle where sense data dma info is saved, the dma handle of the
4732 4733           * reply frame, and the dma handle of the reply free queue.
4733 4734           * For the dma handle of the request queue. Check fma here since we
4734 4735           * are sure the request must have already been sent/DMAed correctly.
4735 4736           * otherwise checking in mptsas_scsi_start() is not correct since
4736 4737           * at that time the dma may not start.
4737 4738           */
4738 4739          if ((mptsas_check_dma_handle(mpt->m_dma_req_frame_hdl) !=
4739 4740              DDI_SUCCESS) ||
4740 4741              (mptsas_check_dma_handle(mpt->m_dma_post_queue_hdl) !=
4741 4742              DDI_SUCCESS)) {
4742 4743                  ddi_fm_service_impact(mpt->m_dip,
4743 4744                      DDI_SERVICE_UNAFFECTED);
4744 4745                  pkt->pkt_reason = CMD_TRAN_ERR;
4745 4746                  pkt->pkt_statistics = 0;
4746 4747          }
4747 4748          if (cmd->cmd_dmahandle &&
4748 4749              (mptsas_check_dma_handle(cmd->cmd_dmahandle) != DDI_SUCCESS)) {
4749 4750                  ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
4750 4751                  pkt->pkt_reason = CMD_TRAN_ERR;
4751 4752                  pkt->pkt_statistics = 0;
4752 4753          }
4753 4754          if ((cmd->cmd_extra_frames &&
4754 4755              ((mptsas_check_dma_handle(cmd->cmd_extra_frames->m_dma_hdl) !=
4755 4756              DDI_SUCCESS) ||
4756 4757              (mptsas_check_acc_handle(cmd->cmd_extra_frames->m_acc_hdl) !=
4757 4758              DDI_SUCCESS)))) {
4758 4759                  ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
4759 4760                  pkt->pkt_reason = CMD_TRAN_ERR;
4760 4761                  pkt->pkt_statistics = 0;
4761 4762          }
4762 4763  
4763 4764          return (TRUE);
4764 4765  }
4765 4766  
4766 4767  static void
4767 4768  mptsas_handle_scsi_io_success(mptsas_t *mpt,
4768 4769      pMpi2ReplyDescriptorsUnion_t reply_desc)
4769 4770  {
4770 4771          pMpi2SCSIIOSuccessReplyDescriptor_t     scsi_io_success;
4771 4772          uint16_t                                SMID;
4772 4773          mptsas_slots_t                          *slots = mpt->m_active;
4773 4774          mptsas_cmd_t                            *cmd = NULL;
4774 4775          struct scsi_pkt                         *pkt;
4775 4776  
4776 4777          scsi_io_success = (pMpi2SCSIIOSuccessReplyDescriptor_t)reply_desc;
4777 4778          SMID = scsi_io_success->SMID;
4778 4779  
4779 4780          /*
4780 4781           * This is a success reply so just complete the IO.  First, do a sanity
4781 4782           * check on the SMID.  The final slot is used for TM requests, which
4782 4783           * would not come into this reply handler.
4783 4784           */
4784 4785          if ((SMID == 0) || (SMID > slots->m_n_slots)) {
4785 4786                  mptsas_log(mpt, CE_WARN, "?Received invalid SMID of %d\n",
4786 4787                      SMID);
4787 4788                  ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
4788 4789                  return;
4789 4790          }
4790 4791  
4791 4792          cmd = slots->m_slot[SMID];
4792 4793  
4793 4794          /*
4794 4795           * print warning and return if the slot is empty
4795 4796           */
4796 4797          if (cmd == NULL) {
4797 4798                  mptsas_log(mpt, CE_WARN, "?NULL command for successful SCSI IO "
4798 4799                      "in slot %d", SMID);
4799 4800                  return;
4800 4801          }
4801 4802  
4802 4803          pkt = CMD2PKT(cmd);
4803 4804          pkt->pkt_state |= (STATE_GOT_BUS | STATE_GOT_TARGET | STATE_SENT_CMD |
4804 4805              STATE_GOT_STATUS);
4805 4806          if (cmd->cmd_flags & CFLAG_DMAVALID) {
4806 4807                  pkt->pkt_state |= STATE_XFERRED_DATA;
4807 4808          }
4808 4809          pkt->pkt_resid = 0;
4809 4810  
4810 4811          if (cmd->cmd_flags & CFLAG_PASSTHRU) {
4811 4812                  cmd->cmd_flags |= CFLAG_FINISHED;
4812 4813                  cv_broadcast(&mpt->m_passthru_cv);
4813 4814                  return;
4814 4815          } else {
4815 4816                  mptsas_remove_cmd(mpt, cmd);
4816 4817          }
4817 4818  
4818 4819          if (cmd->cmd_flags & CFLAG_RETRY) {
4819 4820                  /*
4820 4821                   * The target returned QFULL or busy, do not add tihs
4821 4822                   * pkt to the doneq since the hba will retry
4822 4823                   * this cmd.
4823 4824                   *
4824 4825                   * The pkt has already been resubmitted in
4825 4826                   * mptsas_handle_qfull() or in mptsas_check_scsi_io_error().
4826 4827                   * Remove this cmd_flag here.
4827 4828                   */
4828 4829                  cmd->cmd_flags &= ~CFLAG_RETRY;
4829 4830          } else {
4830 4831                  mptsas_doneq_add(mpt, cmd);
4831 4832          }
4832 4833  }
4833 4834  
4834 4835  static void
4835 4836  mptsas_handle_address_reply(mptsas_t *mpt,
4836 4837      pMpi2ReplyDescriptorsUnion_t reply_desc)
4837 4838  {
4838 4839          pMpi2AddressReplyDescriptor_t   address_reply;
4839 4840          pMPI2DefaultReply_t             reply;
4840 4841          mptsas_fw_diagnostic_buffer_t   *pBuffer;
4841 4842          uint32_t                        reply_addr;
4842 4843          uint16_t                        SMID, iocstatus;
4843 4844          mptsas_slots_t                  *slots = mpt->m_active;
4844 4845          mptsas_cmd_t                    *cmd = NULL;
4845 4846          uint8_t                         function, buffer_type;
4846 4847          m_replyh_arg_t                  *args;
4847 4848          int                             reply_frame_no;
4848 4849  
4849 4850          ASSERT(mutex_owned(&mpt->m_mutex));
4850 4851  
4851 4852          address_reply = (pMpi2AddressReplyDescriptor_t)reply_desc;
4852 4853  
4853 4854          reply_addr = address_reply->ReplyFrameAddress;
4854 4855          SMID = address_reply->SMID;
4855 4856          /*
4856 4857           * If reply frame is not in the proper range we should ignore this
4857 4858           * message and exit the interrupt handler.
4858 4859           */
4859 4860          if ((reply_addr < mpt->m_reply_frame_dma_addr) ||
4860 4861              (reply_addr >= (mpt->m_reply_frame_dma_addr +
4861 4862              (mpt->m_reply_frame_size * mpt->m_max_replies))) ||
4862 4863              ((reply_addr - mpt->m_reply_frame_dma_addr) %
4863 4864              mpt->m_reply_frame_size != 0)) {
4864 4865                  mptsas_log(mpt, CE_WARN, "?Received invalid reply frame "
4865 4866                      "address 0x%x\n", reply_addr);
4866 4867                  ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
4867 4868                  return;
4868 4869          }
4869 4870  
4870 4871          (void) ddi_dma_sync(mpt->m_dma_reply_frame_hdl, 0, 0,
4871 4872              DDI_DMA_SYNC_FORCPU);
4872 4873          reply = (pMPI2DefaultReply_t)(mpt->m_reply_frame + (reply_addr -
4873 4874              mpt->m_reply_frame_dma_addr));
4874 4875          function = ddi_get8(mpt->m_acc_reply_frame_hdl, &reply->Function);
4875 4876  
4876 4877          /*
4877 4878           * don't get slot information and command for events since these values
4878 4879           * don't exist
4879 4880           */
4880 4881          if ((function != MPI2_FUNCTION_EVENT_NOTIFICATION) &&
4881 4882              (function != MPI2_FUNCTION_DIAG_BUFFER_POST)) {
4882 4883                  /*
4883 4884                   * This could be a TM reply, which use the last allocated SMID,
4884 4885                   * so allow for that.
4885 4886                   */
4886 4887                  if ((SMID == 0) || (SMID > (slots->m_n_slots + 1))) {
4887 4888                          mptsas_log(mpt, CE_WARN, "?Received invalid SMID of "
4888 4889                              "%d\n", SMID);
4889 4890                          ddi_fm_service_impact(mpt->m_dip,
4890 4891                              DDI_SERVICE_UNAFFECTED);
4891 4892                          return;
4892 4893                  }
4893 4894  
4894 4895                  cmd = slots->m_slot[SMID];
4895 4896  
4896 4897                  /*
4897 4898                   * print warning and return if the slot is empty
4898 4899                   */
4899 4900                  if (cmd == NULL) {
4900 4901                          mptsas_log(mpt, CE_WARN, "?NULL command for address "
4901 4902                              "reply in slot %d", SMID);
4902 4903                          return;
4903 4904                  }
4904 4905                  if ((cmd->cmd_flags & CFLAG_PASSTHRU) ||
4905 4906                      (cmd->cmd_flags & CFLAG_CONFIG) ||
4906 4907                      (cmd->cmd_flags & CFLAG_FW_DIAG)) {
4907 4908                          cmd->cmd_rfm = reply_addr;
4908 4909                          cmd->cmd_flags |= CFLAG_FINISHED;
4909 4910                          cv_broadcast(&mpt->m_passthru_cv);
4910 4911                          cv_broadcast(&mpt->m_config_cv);
4911 4912                          cv_broadcast(&mpt->m_fw_diag_cv);
4912 4913                          return;
4913 4914                  } else if (!(cmd->cmd_flags & CFLAG_FW_CMD)) {
4914 4915                          mptsas_remove_cmd(mpt, cmd);
4915 4916                  }
4916 4917                  NDBG31(("\t\tmptsas_process_intr: slot=%d", SMID));
4917 4918          }
4918 4919          /*
4919 4920           * Depending on the function, we need to handle
4920 4921           * the reply frame (and cmd) differently.
4921 4922           */
4922 4923          switch (function) {
4923 4924          case MPI2_FUNCTION_SCSI_IO_REQUEST:
4924 4925                  mptsas_check_scsi_io_error(mpt, (pMpi2SCSIIOReply_t)reply, cmd);
4925 4926                  break;
4926 4927          case MPI2_FUNCTION_SCSI_TASK_MGMT:
4927 4928                  cmd->cmd_rfm = reply_addr;
4928 4929                  mptsas_check_task_mgt(mpt, (pMpi2SCSIManagementReply_t)reply,
4929 4930                      cmd);
4930 4931                  break;
4931 4932          case MPI2_FUNCTION_FW_DOWNLOAD:
4932 4933                  cmd->cmd_flags |= CFLAG_FINISHED;
4933 4934                  cv_signal(&mpt->m_fw_cv);
4934 4935                  break;
4935 4936          case MPI2_FUNCTION_EVENT_NOTIFICATION:
4936 4937                  reply_frame_no = (reply_addr - mpt->m_reply_frame_dma_addr) /
4937 4938                      mpt->m_reply_frame_size;
4938 4939                  args = &mpt->m_replyh_args[reply_frame_no];
4939 4940                  args->mpt = (void *)mpt;
4940 4941                  args->rfm = reply_addr;
4941 4942  
4942 4943                  /*
4943 4944                   * Record the event if its type is enabled in
4944 4945                   * this mpt instance by ioctl.
4945 4946                   */
4946 4947                  mptsas_record_event(args);
4947 4948  
4948 4949                  /*
4949 4950                   * Handle time critical events
4950 4951                   * NOT_RESPONDING/ADDED only now
4951 4952                   */
4952 4953                  if (mptsas_handle_event_sync(args) == DDI_SUCCESS) {
4953 4954                          /*
4954 4955                           * Would not return main process,
4955 4956                           * just let taskq resolve ack action
4956 4957                           * and ack would be sent in taskq thread
4957 4958                           */
4958 4959                          NDBG20(("send mptsas_handle_event_sync success"));
4959 4960                  }
4960 4961                  if ((ddi_taskq_dispatch(mpt->m_event_taskq, mptsas_handle_event,
4961 4962                      (void *)args, DDI_NOSLEEP)) != DDI_SUCCESS) {
4962 4963                          mptsas_log(mpt, CE_WARN, "No memory available"
4963 4964                          "for dispatch taskq");
4964 4965                          /*
4965 4966                           * Return the reply frame to the free queue.
4966 4967                           */
4967 4968                          ddi_put32(mpt->m_acc_free_queue_hdl,
4968 4969                              &((uint32_t *)(void *)
4969 4970                              mpt->m_free_queue)[mpt->m_free_index], reply_addr);
4970 4971                          (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
4971 4972                              DDI_DMA_SYNC_FORDEV);
4972 4973                          if (++mpt->m_free_index == mpt->m_free_queue_depth) {
4973 4974                                  mpt->m_free_index = 0;
4974 4975                          }
4975 4976  
4976 4977                          ddi_put32(mpt->m_datap,
4977 4978                              &mpt->m_reg->ReplyFreeHostIndex, mpt->m_free_index);
4978 4979                  }
4979 4980                  return;
4980 4981          case MPI2_FUNCTION_DIAG_BUFFER_POST:
4981 4982                  /*
4982 4983                   * If SMID is 0, this implies that the reply is due to a
4983 4984                   * release function with a status that the buffer has been
4984 4985                   * released.  Set the buffer flags accordingly.
4985 4986                   */
4986 4987                  if (SMID == 0) {
4987 4988                          iocstatus = ddi_get16(mpt->m_acc_reply_frame_hdl,
4988 4989                              &reply->IOCStatus);
4989 4990                          buffer_type = ddi_get8(mpt->m_acc_reply_frame_hdl,
4990 4991                              &(((pMpi2DiagBufferPostReply_t)reply)->BufferType));
4991 4992                          if (iocstatus == MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED) {
4992 4993                                  pBuffer =
4993 4994                                      &mpt->m_fw_diag_buffer_list[buffer_type];
4994 4995                                  pBuffer->valid_data = TRUE;
4995 4996                                  pBuffer->owned_by_firmware = FALSE;
4996 4997                                  pBuffer->immediate = FALSE;
4997 4998                          }
4998 4999                  } else {
4999 5000                          /*
5000 5001                           * Normal handling of diag post reply with SMID.
5001 5002                           */
5002 5003                          cmd = slots->m_slot[SMID];
5003 5004  
5004 5005                          /*
5005 5006                           * print warning and return if the slot is empty
5006 5007                           */
5007 5008                          if (cmd == NULL) {
5008 5009                                  mptsas_log(mpt, CE_WARN, "?NULL command for "
5009 5010                                      "address reply in slot %d", SMID);
5010 5011                                  return;
5011 5012                          }
5012 5013                          cmd->cmd_rfm = reply_addr;
5013 5014                          cmd->cmd_flags |= CFLAG_FINISHED;
5014 5015                          cv_broadcast(&mpt->m_fw_diag_cv);
5015 5016                  }
5016 5017                  return;
5017 5018          default:
5018 5019                  mptsas_log(mpt, CE_WARN, "Unknown function 0x%x ", function);
5019 5020                  break;
5020 5021          }
5021 5022  
5022 5023          /*
5023 5024           * Return the reply frame to the free queue.
5024 5025           */
5025 5026          ddi_put32(mpt->m_acc_free_queue_hdl,
5026 5027              &((uint32_t *)(void *)mpt->m_free_queue)[mpt->m_free_index],
5027 5028              reply_addr);
5028 5029          (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
5029 5030              DDI_DMA_SYNC_FORDEV);
5030 5031          if (++mpt->m_free_index == mpt->m_free_queue_depth) {
5031 5032                  mpt->m_free_index = 0;
5032 5033          }
5033 5034          ddi_put32(mpt->m_datap, &mpt->m_reg->ReplyFreeHostIndex,
5034 5035              mpt->m_free_index);
5035 5036  
5036 5037          if (cmd->cmd_flags & CFLAG_FW_CMD)
5037 5038                  return;
5038 5039  
5039 5040          if (cmd->cmd_flags & CFLAG_RETRY) {
5040 5041                  /*
5041 5042                   * The target returned QFULL or busy, do not add tihs
5042 5043                   * pkt to the doneq since the hba will retry
5043 5044                   * this cmd.
5044 5045                   *
5045 5046                   * The pkt has already been resubmitted in
5046 5047                   * mptsas_handle_qfull() or in mptsas_check_scsi_io_error().
5047 5048                   * Remove this cmd_flag here.
5048 5049                   */
5049 5050                  cmd->cmd_flags &= ~CFLAG_RETRY;
5050 5051          } else {
5051 5052                  mptsas_doneq_add(mpt, cmd);
5052 5053          }
5053 5054  }
5054 5055  
5055 5056  static void
5056 5057  mptsas_check_scsi_io_error(mptsas_t *mpt, pMpi2SCSIIOReply_t reply,
5057 5058      mptsas_cmd_t *cmd)
5058 5059  {
5059 5060          uint8_t                 scsi_status, scsi_state;
5060 5061          uint16_t                ioc_status;
5061 5062          uint32_t                xferred, sensecount, responsedata, loginfo = 0;
5062 5063          struct scsi_pkt         *pkt;
5063 5064          struct scsi_arq_status  *arqstat;
5064 5065          struct buf              *bp;
5065 5066          mptsas_target_t         *ptgt = cmd->cmd_tgt_addr;
5066 5067          uint8_t                 *sensedata = NULL;
5067 5068  
5068 5069          if ((cmd->cmd_flags & (CFLAG_SCBEXTERN | CFLAG_EXTARQBUFVALID)) ==
5069 5070              (CFLAG_SCBEXTERN | CFLAG_EXTARQBUFVALID)) {
5070 5071                  bp = cmd->cmd_ext_arq_buf;
5071 5072          } else {
5072 5073                  bp = cmd->cmd_arq_buf;
5073 5074          }
5074 5075  
5075 5076          scsi_status = ddi_get8(mpt->m_acc_reply_frame_hdl, &reply->SCSIStatus);
5076 5077          ioc_status = ddi_get16(mpt->m_acc_reply_frame_hdl, &reply->IOCStatus);
5077 5078          scsi_state = ddi_get8(mpt->m_acc_reply_frame_hdl, &reply->SCSIState);
5078 5079          xferred = ddi_get32(mpt->m_acc_reply_frame_hdl, &reply->TransferCount);
5079 5080          sensecount = ddi_get32(mpt->m_acc_reply_frame_hdl, &reply->SenseCount);
5080 5081          responsedata = ddi_get32(mpt->m_acc_reply_frame_hdl,
5081 5082              &reply->ResponseInfo);
5082 5083  
5083 5084          if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
5084 5085                  loginfo = ddi_get32(mpt->m_acc_reply_frame_hdl,
5085 5086                      &reply->IOCLogInfo);
5086 5087                  mptsas_log(mpt, CE_NOTE,
5087 5088                      "?Log info 0x%x received for target %d.\n"
5088 5089                      "\tscsi_status=0x%x, ioc_status=0x%x, scsi_state=0x%x",
5089 5090                      loginfo, Tgt(cmd), scsi_status, ioc_status,
5090 5091                      scsi_state);
5091 5092          }
5092 5093  
5093 5094          NDBG31(("\t\tscsi_status=0x%x, ioc_status=0x%x, scsi_state=0x%x",
5094 5095              scsi_status, ioc_status, scsi_state));
5095 5096  
5096 5097          pkt = CMD2PKT(cmd);
5097 5098          *(pkt->pkt_scbp) = scsi_status;
5098 5099  
5099 5100          if (loginfo == 0x31170000) {
5100 5101                  /*
5101 5102                   * if loginfo PL_LOGINFO_CODE_IO_DEVICE_MISSING_DELAY_RETRY
5102 5103                   * 0x31170000 comes, that means the device missing delay
5103 5104                   * is in progressing, the command need retry later.
5104 5105                   */
5105 5106                  *(pkt->pkt_scbp) = STATUS_BUSY;
5106 5107                  return;
5107 5108          }
5108 5109  
5109 5110          if ((scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS) &&
5110 5111              ((ioc_status & MPI2_IOCSTATUS_MASK) ==
5111 5112              MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE)) {
5112 5113                  pkt->pkt_reason = CMD_INCOMPLETE;
5113 5114                  pkt->pkt_state |= STATE_GOT_BUS;
5114 5115                  mutex_enter(&ptgt->m_tgt_intr_mutex);
5115 5116                  if (ptgt->m_reset_delay == 0) {
5116 5117                          mptsas_set_throttle(mpt, ptgt,
5117 5118                              DRAIN_THROTTLE);
5118 5119                  }
5119 5120                  mutex_exit(&ptgt->m_tgt_intr_mutex);
5120 5121                  return;
5121 5122          }
5122 5123  
5123 5124          if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
5124 5125                  responsedata &= 0x000000FF;
5125 5126                  if (responsedata & MPTSAS_SCSI_RESPONSE_CODE_TLR_OFF) {
5126 5127                          mptsas_log(mpt, CE_NOTE, "Do not support the TLR\n");
5127 5128                          pkt->pkt_reason = CMD_TLR_OFF;
5128 5129                          return;
5129 5130                  }
5130 5131          }
5131 5132  
5132 5133  
5133 5134          switch (scsi_status) {
5134 5135          case MPI2_SCSI_STATUS_CHECK_CONDITION:
5135 5136                  pkt->pkt_resid = (cmd->cmd_dmacount - xferred);
5136 5137                  arqstat = (void*)(pkt->pkt_scbp);
5137 5138                  arqstat->sts_rqpkt_status = *((struct scsi_status *)
5138 5139                      (pkt->pkt_scbp));
5139 5140                  pkt->pkt_state |= (STATE_GOT_BUS | STATE_GOT_TARGET |
5140 5141                      STATE_SENT_CMD | STATE_GOT_STATUS | STATE_ARQ_DONE);
5141 5142                  if (cmd->cmd_flags & CFLAG_XARQ) {
5142 5143                          pkt->pkt_state |= STATE_XARQ_DONE;
5143 5144                  }
5144 5145                  if (pkt->pkt_resid != cmd->cmd_dmacount) {
5145 5146                          pkt->pkt_state |= STATE_XFERRED_DATA;
5146 5147                  }
5147 5148                  arqstat->sts_rqpkt_reason = pkt->pkt_reason;
5148 5149                  arqstat->sts_rqpkt_state  = pkt->pkt_state;
5149 5150                  arqstat->sts_rqpkt_state |= STATE_XFERRED_DATA;
5150 5151                  arqstat->sts_rqpkt_statistics = pkt->pkt_statistics;
5151 5152                  sensedata = (uint8_t *)&arqstat->sts_sensedata;
5152 5153  
5153 5154                  bcopy((uchar_t *)bp->b_un.b_addr, sensedata,
5154 5155                      ((cmd->cmd_rqslen >= sensecount) ? sensecount :
5155 5156                      cmd->cmd_rqslen));
5156 5157                  arqstat->sts_rqpkt_resid = (cmd->cmd_rqslen - sensecount);
5157 5158                  cmd->cmd_flags |= CFLAG_CMDARQ;
5158 5159                  /*
5159 5160                   * Set proper status for pkt if autosense was valid
5160 5161                   */
5161 5162                  if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
5162 5163                          struct scsi_status zero_status = { 0 };
5163 5164                          arqstat->sts_rqpkt_status = zero_status;
5164 5165                  }
5165 5166  
5166 5167                  /*
5167 5168                   * ASC=0x47 is parity error
5168 5169                   * ASC=0x48 is initiator detected error received
5169 5170                   */
5170 5171                  if ((scsi_sense_key(sensedata) == KEY_ABORTED_COMMAND) &&
5171 5172                      ((scsi_sense_asc(sensedata) == 0x47) ||
5172 5173                      (scsi_sense_asc(sensedata) == 0x48))) {
5173 5174                          mptsas_log(mpt, CE_NOTE, "Aborted_command!");
5174 5175                  }
5175 5176  
5176 5177                  /*
5177 5178                   * ASC/ASCQ=0x3F/0x0E means report_luns data changed
5178 5179                   * ASC/ASCQ=0x25/0x00 means invalid lun
5179 5180                   */
5180 5181                  if (((scsi_sense_key(sensedata) == KEY_UNIT_ATTENTION) &&
5181 5182                      (scsi_sense_asc(sensedata) == 0x3F) &&
5182 5183                      (scsi_sense_ascq(sensedata) == 0x0E)) ||
5183 5184                      ((scsi_sense_key(sensedata) == KEY_ILLEGAL_REQUEST) &&
5184 5185                      (scsi_sense_asc(sensedata) == 0x25) &&
5185 5186                      (scsi_sense_ascq(sensedata) == 0x00))) {
5186 5187                          mptsas_topo_change_list_t *topo_node = NULL;
5187 5188  
5188 5189                          topo_node = kmem_zalloc(
5189 5190                              sizeof (mptsas_topo_change_list_t),
5190 5191                              KM_NOSLEEP);
5191 5192                          if (topo_node == NULL) {
5192 5193                                  mptsas_log(mpt, CE_NOTE, "No memory"
5193 5194                                      "resource for handle SAS dynamic"
5194 5195                                      "reconfigure.\n");
5195 5196                                  break;
5196 5197                          }
5197 5198                          topo_node->mpt = mpt;
5198 5199                          topo_node->event = MPTSAS_DR_EVENT_RECONFIG_TARGET;
5199 5200                          topo_node->un.phymask = ptgt->m_phymask;
5200 5201                          topo_node->devhdl = ptgt->m_devhdl;
5201 5202                          topo_node->object = (void *)ptgt;
5202 5203                          topo_node->flags = MPTSAS_TOPO_FLAG_LUN_ASSOCIATED;
5203 5204  
5204 5205                          if ((ddi_taskq_dispatch(mpt->m_dr_taskq,
5205 5206                              mptsas_handle_dr,
5206 5207                              (void *)topo_node,
5207 5208                              DDI_NOSLEEP)) != DDI_SUCCESS) {
5208 5209                                  mptsas_log(mpt, CE_NOTE, "mptsas start taskq"
5209 5210                                      "for handle SAS dynamic reconfigure"
5210 5211                                      "failed. \n");
5211 5212                          }
5212 5213                  }
5213 5214                  break;
5214 5215          case MPI2_SCSI_STATUS_GOOD:
5215 5216                  switch (ioc_status & MPI2_IOCSTATUS_MASK) {
5216 5217                  case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
5217 5218                          pkt->pkt_reason = CMD_DEV_GONE;
5218 5219                          pkt->pkt_state |= STATE_GOT_BUS;
5219 5220                          mutex_enter(&ptgt->m_tgt_intr_mutex);
5220 5221                          if (ptgt->m_reset_delay == 0) {
5221 5222                                  mptsas_set_throttle(mpt, ptgt, DRAIN_THROTTLE);
5222 5223                          }
5223 5224                          mutex_exit(&ptgt->m_tgt_intr_mutex);
5224 5225                          NDBG31(("lost disk for target%d, command:%x",
5225 5226                              Tgt(cmd), pkt->pkt_cdbp[0]));
5226 5227                          break;
5227 5228                  case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
5228 5229                          NDBG31(("data overrun: xferred=%d", xferred));
5229 5230                          NDBG31(("dmacount=%d", cmd->cmd_dmacount));
5230 5231                          pkt->pkt_reason = CMD_DATA_OVR;
5231 5232                          pkt->pkt_state |= (STATE_GOT_BUS | STATE_GOT_TARGET
5232 5233                              | STATE_SENT_CMD | STATE_GOT_STATUS
5233 5234                              | STATE_XFERRED_DATA);
5234 5235                          pkt->pkt_resid = 0;
5235 5236                          break;
5236 5237                  case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
5237 5238                  case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
5238 5239                          NDBG31(("data underrun: xferred=%d", xferred));
5239 5240                          NDBG31(("dmacount=%d", cmd->cmd_dmacount));
5240 5241                          pkt->pkt_state |= (STATE_GOT_BUS | STATE_GOT_TARGET
5241 5242                              | STATE_SENT_CMD | STATE_GOT_STATUS);
5242 5243                          pkt->pkt_resid = (cmd->cmd_dmacount - xferred);
5243 5244                          if (pkt->pkt_resid != cmd->cmd_dmacount) {
5244 5245                                  pkt->pkt_state |= STATE_XFERRED_DATA;
5245 5246                          }
5246 5247                          break;
5247 5248                  case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
5248 5249                          mptsas_set_pkt_reason(mpt,
5249 5250                              cmd, CMD_RESET, STAT_BUS_RESET);
5250 5251                          break;
5251 5252                  case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
5252 5253                  case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
5253 5254                          mptsas_set_pkt_reason(mpt,
5254 5255                              cmd, CMD_RESET, STAT_DEV_RESET);
5255 5256                          break;
5256 5257                  case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
5257 5258                  case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
5258 5259                          pkt->pkt_state |= (STATE_GOT_BUS | STATE_GOT_TARGET);
5259 5260                          mptsas_set_pkt_reason(mpt,
5260 5261                              cmd, CMD_TERMINATED, STAT_TERMINATED);
5261 5262                          break;
5262 5263                  case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
5263 5264                  case MPI2_IOCSTATUS_BUSY:
5264 5265                          /*
5265 5266                           * set throttles to drain
5266 5267                           */
5267 5268                          ptgt = (mptsas_target_t *)mptsas_hash_traverse(
5268 5269                              &mpt->m_active->m_tgttbl, MPTSAS_HASH_FIRST);
5269 5270                          while (ptgt != NULL) {
5270 5271                                  mutex_enter(&ptgt->m_tgt_intr_mutex);
5271 5272                                  mptsas_set_throttle(mpt, ptgt, DRAIN_THROTTLE);
5272 5273                                  mutex_exit(&ptgt->m_tgt_intr_mutex);
5273 5274  
5274 5275                                  ptgt = (mptsas_target_t *)mptsas_hash_traverse(
5275 5276                                      &mpt->m_active->m_tgttbl, MPTSAS_HASH_NEXT);
5276 5277                          }
5277 5278  
5278 5279                          /*
5279 5280                           * retry command
5280 5281                           */
5281 5282                          cmd->cmd_flags |= CFLAG_RETRY;
5282 5283                          cmd->cmd_pkt_flags |= FLAG_HEAD;
5283 5284  
5284 5285                          mutex_exit(&mpt->m_mutex);
5285 5286                          (void) mptsas_accept_pkt(mpt, cmd);
5286 5287                          mutex_enter(&mpt->m_mutex);
5287 5288                          break;
5288 5289                  default:
5289 5290                          mptsas_log(mpt, CE_WARN,
5290 5291                              "unknown ioc_status = %x\n", ioc_status);
5291 5292                          mptsas_log(mpt, CE_CONT, "scsi_state = %x, transfer "
5292 5293                              "count = %x, scsi_status = %x", scsi_state,
5293 5294                              xferred, scsi_status);
5294 5295                          break;
5295 5296                  }
5296 5297                  break;
5297 5298          case MPI2_SCSI_STATUS_TASK_SET_FULL:
5298 5299                  mptsas_handle_qfull(mpt, cmd);
5299 5300                  break;
5300 5301          case MPI2_SCSI_STATUS_BUSY:
5301 5302                  NDBG31(("scsi_status busy received"));
5302 5303                  break;
5303 5304          case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
5304 5305                  NDBG31(("scsi_status reservation conflict received"));
5305 5306                  break;
5306 5307          default:
5307 5308                  mptsas_log(mpt, CE_WARN, "scsi_status=%x, ioc_status=%x\n",
5308 5309                      scsi_status, ioc_status);
5309 5310                  mptsas_log(mpt, CE_WARN,
5310 5311                      "mptsas_process_intr: invalid scsi status\n");
5311 5312                  break;
5312 5313          }
5313 5314  }
5314 5315  
5315 5316  static void
5316 5317  mptsas_check_task_mgt(mptsas_t *mpt, pMpi2SCSIManagementReply_t reply,
5317 5318          mptsas_cmd_t *cmd)
5318 5319  {
5319 5320          uint8_t         task_type;
5320 5321          uint16_t        ioc_status;
5321 5322          uint32_t        log_info;
5322 5323          uint16_t        dev_handle;
5323 5324          struct scsi_pkt *pkt = CMD2PKT(cmd);
5324 5325  
5325 5326          task_type = ddi_get8(mpt->m_acc_reply_frame_hdl, &reply->TaskType);
5326 5327          ioc_status = ddi_get16(mpt->m_acc_reply_frame_hdl, &reply->IOCStatus);
5327 5328          log_info = ddi_get32(mpt->m_acc_reply_frame_hdl, &reply->IOCLogInfo);
5328 5329          dev_handle = ddi_get16(mpt->m_acc_reply_frame_hdl, &reply->DevHandle);
5329 5330  
5330 5331          if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5331 5332                  mptsas_log(mpt, CE_WARN, "mptsas_check_task_mgt: Task 0x%x "
5332 5333                      "failed. IOCStatus=0x%x IOCLogInfo=0x%x target=%d\n",
5333 5334                      task_type, ioc_status, log_info, dev_handle);
5334 5335                  pkt->pkt_reason = CMD_INCOMPLETE;
5335 5336                  return;
5336 5337          }
5337 5338  
5338 5339          switch (task_type) {
5339 5340          case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
5340 5341          case MPI2_SCSITASKMGMT_TASKTYPE_CLEAR_TASK_SET:
5341 5342          case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
5342 5343          case MPI2_SCSITASKMGMT_TASKTYPE_CLR_ACA:
5343 5344          case MPI2_SCSITASKMGMT_TASKTYPE_QRY_TASK_SET:
5344 5345          case MPI2_SCSITASKMGMT_TASKTYPE_QRY_UNIT_ATTENTION:
5345 5346                  break;
5346 5347          case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
5347 5348          case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
5348 5349          case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
5349 5350                  /*
5350 5351                   * Check for invalid DevHandle of 0 in case application
5351 5352                   * sends bad command.  DevHandle of 0 could cause problems.
5352 5353                   */
5353 5354                  if (dev_handle == 0) {
5354 5355                          mptsas_log(mpt, CE_WARN, "!Can't flush target with"
5355 5356                              " DevHandle of 0.");
5356 5357                  } else {
5357 5358                          mptsas_flush_target(mpt, dev_handle, Lun(cmd),
5358 5359                              task_type);
5359 5360                  }
5360 5361                  break;
5361 5362          default:
5362 5363                  mptsas_log(mpt, CE_WARN, "Unknown task management type %d.",
5363 5364                      task_type);
5364 5365                  mptsas_log(mpt, CE_WARN, "ioc status = %x", ioc_status);
5365 5366                  break;
5366 5367          }
5367 5368  }
5368 5369  
5369 5370  static void
5370 5371  mptsas_doneq_thread(mptsas_doneq_thread_arg_t *arg)
5371 5372  {
5372 5373          mptsas_t                        *mpt = arg->mpt;
5373 5374          uint64_t                        t = arg->t;
5374 5375          mptsas_cmd_t                    *cmd;
5375 5376          struct scsi_pkt                 *pkt;
5376 5377          mptsas_doneq_thread_list_t      *item = &mpt->m_doneq_thread_id[t];
5377 5378  
5378 5379          mutex_enter(&item->mutex);
5379 5380          while (item->flag & MPTSAS_DONEQ_THREAD_ACTIVE) {
5380 5381                  if (!item->doneq) {
5381 5382                          cv_wait(&item->cv, &item->mutex);
5382 5383                  }
5383 5384                  pkt = NULL;
5384 5385                  if ((cmd = mptsas_doneq_thread_rm(mpt, t)) != NULL) {
5385 5386                          cmd->cmd_flags |= CFLAG_COMPLETED;
5386 5387                          pkt = CMD2PKT(cmd);
5387 5388                  }
5388 5389                  mutex_exit(&item->mutex);
5389 5390                  if (pkt) {
5390 5391                          mptsas_pkt_comp(pkt, cmd);
5391 5392                  }
5392 5393                  mutex_enter(&item->mutex);
5393 5394          }
5394 5395          mutex_exit(&item->mutex);
5395 5396          mutex_enter(&mpt->m_doneq_mutex);
5396 5397          mpt->m_doneq_thread_n--;
5397 5398          cv_broadcast(&mpt->m_doneq_thread_cv);
5398 5399          mutex_exit(&mpt->m_doneq_mutex);
5399 5400  }
5400 5401  
5401 5402  /*
5402 5403   * mpt interrupt handler.
5403 5404   */
5404 5405  static uint_t
5405 5406  mptsas_intr(caddr_t arg1, caddr_t arg2)
5406 5407  {
5407 5408          mptsas_t                        *mpt = (void *)arg1;
5408 5409          pMpi2ReplyDescriptorsUnion_t    reply_desc_union;
5409 5410          uchar_t                         did_reply = FALSE;
5410 5411          int                             i = 0, j;
5411 5412          uint8_t                         reply_type;
5412 5413          uint16_t                        SMID;
5413 5414  
5414 5415          NDBG1(("mptsas_intr: arg1 0x%p arg2 0x%p", (void *)arg1, (void *)arg2));
5415 5416  
5416 5417          /*
5417 5418           * 1.
5418 5419           * To avoid using m_mutex in the ISR(ISR referes not only mptsas_intr,
5419 5420           * but all of the recursive called functions in it. the same below),
5420 5421           * separate mutexs are introduced to protect the elements shown in ISR.
5421 5422           * 3 type of mutex are involved here:
5422 5423           *      a)per instance mutex m_intr_mutex.
5423 5424           *      b)per target mutex m_tgt_intr_mutex.
5424 5425           *      c)mutex that protect the free slot.
5425 5426           *
5426 5427           * a)per instance mutex m_intr_mutex:
5427 5428           * used to protect m_options, m_power, m_waitq, etc that would be
5428 5429           * checked/modified in ISR; protect the getting and setting the reply
5429 5430           * descriptor index; protect the m_slots[];
5430 5431           *
5431 5432           * b)per target mutex m_tgt_intr_mutex:
5432 5433           * used to protect per target element which has relationship to ISR.
5433 5434           * contention for the new per target mutex is just as high as it in
5434 5435           * sd(7d) driver.
5435 5436           *
5436 5437           * c)mutexs that protect the free slots:
5437 5438           * those mutexs are introduced to minimize the mutex contentions
5438 5439           * between the IO request threads where free slots are allocated
5439 5440           * for sending cmds and ISR where slots holding outstanding cmds
5440 5441           * are returned to the free pool.
5441 5442           * the idea is like this:
5442 5443           * 1) Partition all of the free slot into NCPU groups. For example,
5443 5444           * In system where we have 15 slots, and 4 CPU, then slot s1,s5,s9,s13
5444 5445           * are marked belonging to CPU1, s2,s6,s10,s14 to CPU2, s3,s7,s11,s15
5445 5446           * to CPU3, and s4,s8,s12 to CPU4.
5446 5447           * 2) In each of the group, an alloc/release queue pair is created,
5447 5448           * and both the allocq and the releaseq have a dedicated mutex.
5448 5449           * 3) When init, all of the slots in a CPU group are inserted into the
5449 5450           * allocq of its CPU's pair.
5450 5451           * 4) When doing IO,
5451 5452           * mptsas_scsi_start()
5452 5453           * {
5453 5454           *      cpuid = the cpu NO of the cpu where this thread is running on
5454 5455           * retry:
5455 5456           *      mutex_enter(&allocq[cpuid]);
5456 5457           *      if (get free slot = success) {
5457 5458           *              remove the slot from the allocq
5458 5459           *              mutex_exit(&allocq[cpuid]);
5459 5460           *              return(success);
5460 5461           *      } else { // exchange allocq and releaseq and try again
5461 5462           *              mutex_enter(&releq[cpuid]);
5462 5463           *              exchange the allocq and releaseq of this pair;
5463 5464           *              mutex_exit(&releq[cpuid]);
5464 5465           *              if (try to get free slot again = success) {
5465 5466           *                      remove the slot from the allocq
5466 5467           *                      mutex_exit(&allocq[cpuid]);
5467 5468           *                      return(success);
5468 5469           *              } else {
5469 5470           *                      MOD(cpuid)++;
5470 5471           *                      goto retry;
5471 5472           *                      if (all CPU groups tried)
5472 5473           *                              mutex_exit(&allocq[cpuid]);
5473 5474           *                              return(failure);
5474 5475           *              }
5475 5476           *      }
5476 5477           * }
5477 5478           * ISR()
5478 5479           * {
5479 5480           *              cpuid = the CPU group id where the slot sending the
5480 5481           *              cmd belongs;
5481 5482           *              mutex_enter(&releq[cpuid]);
5482 5483           *              remove the slot from the releaseq
5483 5484           *              mutex_exit(&releq[cpuid]);
5484 5485           * }
5485 5486           * This way, only when the queue pair doing exchange have mutex
5486 5487           * contentions.
5487 5488           *
5488 5489           * For mutex m_intr_mutex and m_tgt_intr_mutex, there are 2 scenarios:
5489 5490           *
5490 5491           * a)If the elements are only checked but not modified in the ISR, then
5491 5492           * only the places where those elements are modifed(outside of ISR)
5492 5493           * need to be protected by the new introduced mutex.
5493 5494           * For example, data A is only read/checked in ISR, then we need do
5494 5495           * like this:
5495 5496           * In ISR:
5496 5497           * {
5497 5498           *      mutex_enter(&new_mutex);
5498 5499           *      read(A);
5499 5500           *      mutex_exit(&new_mutex);
5500 5501           *      //the new_mutex here is either the m_tgt_intr_mutex or
5501 5502           *      //the m_intr_mutex.
5502 5503           * }
5503 5504           * In non-ISR
5504 5505           * {
5505 5506           *      mutex_enter(&m_mutex); //the stock driver already did this
5506 5507           *      mutex_enter(&new_mutex);
5507 5508           *      write(A);
5508 5509           *      mutex_exit(&new_mutex);
5509 5510           *      mutex_exit(&m_mutex); //the stock driver already did this
5510 5511           *
5511 5512           *      read(A);
5512 5513           *      // read(A) in non-ISR is not required to be protected by new
5513 5514           *      // mutex since 'A' has already been protected by m_mutex
5514 5515           *      // outside of the ISR
5515 5516           * }
5516 5517           *
5517 5518           * Those fields in mptsas_target_t/ptgt which are only read in ISR
5518 5519           * fall into this catergory. So they, together with the fields which
5519 5520           * are never read in ISR, are not necessary to be protected by
5520 5521           * m_tgt_intr_mutex, don't bother.
5521 5522           * checking of m_waitq also falls into this catergory. so all of the
5522 5523           * place outside of ISR where the m_waitq is modified, such as in
5523 5524           * mptsas_waitq_add(), mptsas_waitq_delete(), mptsas_waitq_rm(),
5524 5525           * m_intr_mutex should be used.
5525 5526           *
5526 5527           * b)If the elements are modified in the ISR, then each place where
5527 5528           * those elements are referred(outside of ISR) need to be protected
5528 5529           * by the new introduced mutex. Of course, if those elements only
5529 5530           * appear in the non-key code path, that is, they don't affect
5530 5531           * performance, then the m_mutex can still be used as before.
5531 5532           * For example, data B is modified in key code path in ISR, and data C
5532 5533           * is modified in non-key code path in ISR, then we can do like this:
5533 5534           * In ISR:
5534 5535           * {
5535 5536           *      mutex_enter(&new_mutex);
5536 5537           *      wirte(B);
5537 5538           *      mutex_exit(&new_mutex);
5538 5539           *      if (seldom happen) {
5539 5540           *              mutex_enter(&m_mutex);
5540 5541           *              write(C);
5541 5542           *              mutex_exit(&m_mutex);
5542 5543           *      }
5543 5544           *      //the new_mutex here is either the m_tgt_intr_mutex or
5544 5545           *      //the m_intr_mutex.
5545 5546           * }
5546 5547           * In non-ISR
5547 5548           * {
5548 5549           *      mutex_enter(&new_mutex);
5549 5550           *      write(B);
5550 5551           *      mutex_exit(&new_mutex);
5551 5552           *
5552 5553           *      mutex_enter(&new_mutex);
5553 5554           *      read(B);
5554 5555           *      mutex_exit(&new_mutex);
5555 5556           *      // both write(B) and read(B) in non-ISR is required to be
5556 5557           *      // protected by new mutex outside of the ISR
5557 5558           *
5558 5559           *      mutex_enter(&m_mutex); //the stock driver already did this
5559 5560           *      read(C);
5560 5561           *      write(C);
5561 5562           *      mutex_exit(&m_mutex); //the stock driver already did this
5562 5563           *      // both write(C) and read(C) in non-ISR have been already
5563 5564           *      // been protected by m_mutex outside of the ISR
5564 5565           * }
5565 5566           *
5566 5567           * For example, ptgt->m_t_ncmds fall into 'B' of this catergory, and
5567 5568           * elements shown in address reply, restart_hba, passthrough, IOC
5568 5569           * fall into 'C' of  this catergory.
5569 5570           *
5570 5571           * In any case where mutexs are nested, make sure in the following
5571 5572           * order:
5572 5573           *      m_mutex -> m_intr_mutex -> m_tgt_intr_mutex
5573 5574           *      m_intr_mutex -> m_tgt_intr_mutex
5574 5575           *      m_mutex -> m_intr_mutex
5575 5576           *      m_mutex -> m_tgt_intr_mutex
5576 5577           *
5577 5578           * 2.
5578 5579           * Make sure at any time, getting the ReplyDescriptor by m_post_index
5579 5580           * and setting m_post_index to the ReplyDescriptorIndex register are
5580 5581           * atomic. Since m_mutex is not used for this purpose in ISR, the new
5581 5582           * mutex m_intr_mutex must play this role. So mptsas_poll(), where this
5582 5583           * kind of getting/setting is also performed, must use m_intr_mutex.
5583 5584           * Note, since context reply in ISR/process_intr is the only code path
5584 5585           * which affect performance, a fast path is introduced to only handle
5585 5586           * the read/write IO having context reply. For other IOs such as
5586 5587           * passthrough and IOC with context reply and all address reply, we
5587 5588           * use the as-is process_intr() to handle them. In order to keep the
5588 5589           * same semantics in process_intr(), make sure any new mutex is not held
5589 5590           * before enterring it.
5590 5591           */
5591 5592  
5592 5593          mutex_enter(&mpt->m_intr_mutex);
5593 5594  
5594 5595          /*
5595 5596           * If interrupts are shared by two channels then check whether this
5596 5597           * interrupt is genuinely for this channel by making sure first the
5597 5598           * chip is in high power state.
5598 5599           */
5599 5600          if ((mpt->m_options & MPTSAS_OPT_PM) &&
5600 5601              (mpt->m_power_level != PM_LEVEL_D0)) {
5601 5602                  mutex_exit(&mpt->m_intr_mutex);
5602 5603                  return (DDI_INTR_UNCLAIMED);
5603 5604          }
5604 5605  
5605 5606          /*
5606 5607           * If polling, interrupt was triggered by some shared interrupt because
5607 5608           * IOC interrupts are disabled during polling, so polling routine will
5608 5609           * handle any replies.  Considering this, if polling is happening,
5609 5610           * return with interrupt unclaimed.
5610 5611           */
5611 5612          if (mpt->m_polled_intr) {
5612 5613                  mutex_exit(&mpt->m_intr_mutex);
5613 5614                  mptsas_log(mpt, CE_WARN, "mpt_sas: Unclaimed interrupt");
5614 5615                  return (DDI_INTR_UNCLAIMED);
5615 5616          }
5616 5617  
5617 5618          /*
5618 5619           * Read the istat register.
5619 5620           */
5620 5621          if ((INTPENDING(mpt)) != 0) {
5621 5622                  /*
5622 5623                   * read fifo until empty.
5623 5624                   */
5624 5625  #ifndef __lock_lint
5625 5626                  _NOTE(CONSTCOND)
5626 5627  #endif
5627 5628                  while (TRUE) {
5628 5629                          (void) ddi_dma_sync(mpt->m_dma_post_queue_hdl, 0, 0,
5629 5630                              DDI_DMA_SYNC_FORCPU);
5630 5631                          reply_desc_union = (pMpi2ReplyDescriptorsUnion_t)
5631 5632                              MPTSAS_GET_NEXT_REPLY(mpt, mpt->m_post_index);
5632 5633  
5633 5634                          if (ddi_get32(mpt->m_acc_post_queue_hdl,
5634 5635                              &reply_desc_union->Words.Low) == 0xFFFFFFFF ||
5635 5636                              ddi_get32(mpt->m_acc_post_queue_hdl,
5636 5637                              &reply_desc_union->Words.High) == 0xFFFFFFFF) {
5637 5638                                  break;
5638 5639                          }
5639 5640  
5640 5641                          /*
5641 5642                           * The reply is valid, process it according to its
5642 5643                           * type.  Also, set a flag for updating the reply index
5643 5644                           * after they've all been processed.
5644 5645                           */
5645 5646                          did_reply = TRUE;
5646 5647  
5647 5648                          reply_type = ddi_get8(mpt->m_acc_post_queue_hdl,
5648 5649                              &reply_desc_union->Default.ReplyFlags);
5649 5650                          reply_type &= MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
5650 5651                          mpt->m_reply[i].Default.ReplyFlags = reply_type;
5651 5652                          if (reply_type ==
5652 5653                              MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
5653 5654                                  SMID = ddi_get16(mpt->m_acc_post_queue_hdl,
5654 5655                                      &reply_desc_union->SCSIIOSuccess.SMID);
5655 5656                                  if (mptsas_handle_io_fastpath(mpt, SMID) !=
5656 5657                                      TRUE) {
5657 5658                                          mpt->m_reply[i].SCSIIOSuccess.SMID =
5658 5659                                              SMID;
5659 5660                                          i++;
5660 5661                                  }
5661 5662                          } else if (reply_type ==
5662 5663                              MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
5663 5664                                  mpt->m_reply[i].AddressReply.ReplyFrameAddress =
5664 5665                                      ddi_get32(mpt->m_acc_post_queue_hdl,
5665 5666                                      &reply_desc_union->AddressReply.
5666 5667                                      ReplyFrameAddress);
5667 5668                                  mpt->m_reply[i].AddressReply.SMID =
5668 5669                                      ddi_get16(mpt->m_acc_post_queue_hdl,
5669 5670                                      &reply_desc_union->AddressReply.SMID);
5670 5671                                  i++;
5671 5672                          }
5672 5673                          /*
5673 5674                           * Clear the reply descriptor for re-use and increment
5674 5675                           * index.
5675 5676                           */
5676 5677                          ddi_put64(mpt->m_acc_post_queue_hdl,
5677 5678                              &((uint64_t *)(void *)mpt->m_post_queue)
5678 5679                              [mpt->m_post_index], 0xFFFFFFFFFFFFFFFF);
5679 5680                          (void) ddi_dma_sync(mpt->m_dma_post_queue_hdl, 0, 0,
5680 5681                              DDI_DMA_SYNC_FORDEV);
5681 5682  
5682 5683                          /*
5683 5684                           * Increment post index and roll over if needed.
5684 5685                           */
5685 5686                          if (++mpt->m_post_index == mpt->m_post_queue_depth) {
5686 5687                                  mpt->m_post_index = 0;
5687 5688                          }
5688 5689                          if (i >= MPI_ADDRESS_COALSCE_MAX)
5689 5690                                  break;
5690 5691                  }
5691 5692  
5692 5693                  /*
5693 5694                   * Update the global reply index if at least one reply was
5694 5695                   * processed.
5695 5696                   */
5696 5697                  if (did_reply) {
5697 5698                          ddi_put32(mpt->m_datap,
5698 5699                              &mpt->m_reg->ReplyPostHostIndex, mpt->m_post_index);
5699 5700  
5700 5701                          /*
5701 5702                           * For fma, only check the PIO is required and enough
5702 5703                           * here. Those cases where fastpath is not hit, the
5703 5704                           * mptsas_fma_check() check all of the types of
5704 5705                           * fma. That is not necessary and sometimes not
5705 5706                           * correct. fma check should only be done after
5706 5707                           * the PIO and/or dma is performed.
5707 5708                           */
5708 5709                          if ((mptsas_check_acc_handle(mpt->m_datap) !=
5709 5710                              DDI_SUCCESS)) {
5710 5711                                  ddi_fm_service_impact(mpt->m_dip,
5711 5712                                      DDI_SERVICE_UNAFFECTED);
5712 5713                          }
5713 5714  
5714 5715                  }
5715 5716          } else {
5716 5717                  mutex_exit(&mpt->m_intr_mutex);
5717 5718                  return (DDI_INTR_UNCLAIMED);
5718 5719          }
5719 5720          NDBG1(("mptsas_intr complete"));
5720 5721          mutex_exit(&mpt->m_intr_mutex);
5721 5722  
5722 5723          /*
5723 5724           * Since most of the cmds(read and write IO with success return.)
5724 5725           * have already been processed in fast path in which the m_mutex
5725 5726           * is not held, handling here the address reply and other context reply
5726 5727           * such as passthrough and IOC cmd with m_mutex held should be a big
5727 5728           * issue for performance.
5728 5729           * If holding m_mutex to process these cmds was still an obvious issue,
5729 5730           * we can process them in a taskq.
5730 5731           */
5731 5732          for (j = 0; j < i; j++) {
5732 5733                  mutex_enter(&mpt->m_mutex);
5733 5734                  mptsas_process_intr(mpt, &mpt->m_reply[j]);
5734 5735                  mutex_exit(&mpt->m_mutex);
5735 5736          }
5736 5737  
5737 5738          /*
5738 5739           * If no helper threads are created, process the doneq in ISR. If
5739 5740           * helpers are created, use the doneq length as a metric to measure the
5740 5741           * load on the interrupt CPU. If it is long enough, which indicates the
5741 5742           * load is heavy, then we deliver the IO completions to the helpers.
5742 5743           * This measurement has some limitations, although it is simple and
5743 5744           * straightforward and works well for most of the cases at present.
5744 5745           */
5745 5746          if (!mpt->m_doneq_thread_n) {
5746 5747                  mptsas_doneq_empty(mpt);
5747 5748          } else {
5748 5749                  int helper = 1;
5749 5750                  mutex_enter(&mpt->m_intr_mutex);
5750 5751                  if (mpt->m_doneq_len <= mpt->m_doneq_length_threshold)
5751 5752                          helper = 0;
5752 5753                  mutex_exit(&mpt->m_intr_mutex);
5753 5754                  if (helper) {
5754 5755                          mptsas_deliver_doneq_thread(mpt);
5755 5756                  } else {
5756 5757                          mptsas_doneq_empty(mpt);
5757 5758                  }
5758 5759          }
5759 5760  
5760 5761          /*
5761 5762           * If there are queued cmd, start them now.
5762 5763           */
5763 5764          mutex_enter(&mpt->m_intr_mutex);
5764 5765          if (mpt->m_waitq != NULL) {
5765 5766                  mutex_exit(&mpt->m_intr_mutex);
5766 5767                  mutex_enter(&mpt->m_mutex);
5767 5768                  mptsas_restart_hba(mpt);
5768 5769                  mutex_exit(&mpt->m_mutex);
5769 5770                  return (DDI_INTR_CLAIMED);
5770 5771          }
5771 5772          mutex_exit(&mpt->m_intr_mutex);
5772 5773          return (DDI_INTR_CLAIMED);
5773 5774  }
5774 5775  
5775 5776  /*
5776 5777   * In ISR, the successfully completed read and write IO are processed in a
5777 5778   * fast path. This function is only used to handle non-fastpath IO, including
5778 5779   * all of the address reply, and the context reply for IOC cmd, passthrough,
5779 5780   * etc.
5780 5781   * This function is also used to process polled cmd.
5781 5782   */
5782 5783  static void
5783 5784  mptsas_process_intr(mptsas_t *mpt,
5784 5785      pMpi2ReplyDescriptorsUnion_t reply_desc_union)
5785 5786  {
5786 5787          uint8_t reply_type;
5787 5788  
5788 5789          /*
5789 5790           * The reply is valid, process it according to its
5790 5791           * type.  Also, set a flag for updated the reply index
5791 5792           * after they've all been processed.
5792 5793           */
5793 5794          reply_type = reply_desc_union->Default.ReplyFlags;
5794 5795          if (reply_type == MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
5795 5796                  mptsas_handle_scsi_io_success(mpt, reply_desc_union);
5796 5797          } else if (reply_type == MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
5797 5798                  mptsas_handle_address_reply(mpt, reply_desc_union);
5798 5799          } else {
5799 5800                  mptsas_log(mpt, CE_WARN, "?Bad reply type %x", reply_type);
5800 5801                  ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
5801 5802          }
5802 5803  }
5803 5804  
5804 5805  /*
5805 5806   * handle qfull condition
5806 5807   */
5807 5808  static void
5808 5809  mptsas_handle_qfull(mptsas_t *mpt, mptsas_cmd_t *cmd)
5809 5810  {
5810 5811          mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
5811 5812  
5812 5813          if ((++cmd->cmd_qfull_retries > ptgt->m_qfull_retries) ||
5813 5814              (ptgt->m_qfull_retries == 0)) {
5814 5815                  /*
5815 5816                   * We have exhausted the retries on QFULL, or,
5816 5817                   * the target driver has indicated that it
5817 5818                   * wants to handle QFULL itself by setting
5818 5819                   * qfull-retries capability to 0. In either case
5819 5820                   * we want the target driver's QFULL handling
5820 5821                   * to kick in. We do this by having pkt_reason
5821 5822                   * as CMD_CMPLT and pkt_scbp as STATUS_QFULL.
5822 5823                   */
5823 5824                  mutex_enter(&ptgt->m_tgt_intr_mutex);
5824 5825                  mptsas_set_throttle(mpt, ptgt, DRAIN_THROTTLE);
5825 5826                  mutex_exit(&ptgt->m_tgt_intr_mutex);
5826 5827          } else {
5827 5828                  mutex_enter(&ptgt->m_tgt_intr_mutex);
5828 5829                  if (ptgt->m_reset_delay == 0) {
5829 5830                          ptgt->m_t_throttle =
5830 5831                              max((ptgt->m_t_ncmds - 2), 0);
5831 5832                  }
5832 5833                  mutex_exit(&ptgt->m_tgt_intr_mutex);
5833 5834  
5834 5835                  cmd->cmd_pkt_flags |= FLAG_HEAD;
5835 5836                  cmd->cmd_flags &= ~(CFLAG_TRANFLAG);
5836 5837                  cmd->cmd_flags |= CFLAG_RETRY;
5837 5838  
5838 5839                  mutex_exit(&mpt->m_mutex);
5839 5840                  (void) mptsas_accept_pkt(mpt, cmd);
5840 5841                  mutex_enter(&mpt->m_mutex);
5841 5842  
5842 5843                  /*
5843 5844                   * when target gives queue full status with no commands
5844 5845                   * outstanding (m_t_ncmds == 0), throttle is set to 0
5845 5846                   * (HOLD_THROTTLE), and the queue full handling start
5846 5847                   * (see psarc/1994/313); if there are commands outstanding,
5847 5848                   * throttle is set to (m_t_ncmds - 2)
5848 5849                   */
5849 5850                  mutex_enter(&ptgt->m_tgt_intr_mutex);
5850 5851                  if (ptgt->m_t_throttle == HOLD_THROTTLE) {
5851 5852                          /*
5852 5853                           * By setting throttle to QFULL_THROTTLE, we
5853 5854                           * avoid submitting new commands and in
5854 5855                           * mptsas_restart_cmd find out slots which need
5855 5856                           * their throttles to be cleared.
5856 5857                           */
5857 5858                          mptsas_set_throttle(mpt, ptgt, QFULL_THROTTLE);
5858 5859                          if (mpt->m_restart_cmd_timeid == 0) {
5859 5860                                  mpt->m_restart_cmd_timeid =
5860 5861                                      timeout(mptsas_restart_cmd, mpt,
5861 5862                                      ptgt->m_qfull_retry_interval);
5862 5863                          }
5863 5864                  }
5864 5865                  mutex_exit(&ptgt->m_tgt_intr_mutex);
5865 5866          }
5866 5867  }
5867 5868  
5868 5869  mptsas_phymask_t
5869 5870  mptsas_physport_to_phymask(mptsas_t *mpt, uint8_t physport)
5870 5871  {
5871 5872          mptsas_phymask_t        phy_mask = 0;
5872 5873          uint8_t                 i = 0;
5873 5874  
5874 5875          NDBG20(("mptsas%d physport_to_phymask enter", mpt->m_instance));
5875 5876  
5876 5877          ASSERT(mutex_owned(&mpt->m_mutex));
5877 5878  
5878 5879          /*
5879 5880           * If physport is 0xFF, this is a RAID volume.  Use phymask of 0.
5880 5881           */
5881 5882          if (physport == 0xFF) {
5882 5883                  return (0);
5883 5884          }
5884 5885  
5885 5886          for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
5886 5887                  if (mpt->m_phy_info[i].attached_devhdl &&
5887 5888                      (mpt->m_phy_info[i].phy_mask != 0) &&
5888 5889                      (mpt->m_phy_info[i].port_num == physport)) {
5889 5890                          phy_mask = mpt->m_phy_info[i].phy_mask;
5890 5891                          break;
5891 5892                  }
5892 5893          }
5893 5894          NDBG20(("mptsas%d physport_to_phymask:physport :%x phymask :%x, ",
5894 5895              mpt->m_instance, physport, phy_mask));
5895 5896          return (phy_mask);
5896 5897  }
5897 5898  
5898 5899  /*
5899 5900   * mpt free device handle after device gone, by use of passthrough
5900 5901   */
5901 5902  static int
5902 5903  mptsas_free_devhdl(mptsas_t *mpt, uint16_t devhdl)
5903 5904  {
5904 5905          Mpi2SasIoUnitControlRequest_t   req;
5905 5906          Mpi2SasIoUnitControlReply_t     rep;
5906 5907          int                             ret;
5907 5908  
5908 5909          ASSERT(mutex_owned(&mpt->m_mutex));
5909 5910  
5910 5911          /*
5911 5912           * Need to compose a SAS IO Unit Control request message
5912 5913           * and call mptsas_do_passthru() function
5913 5914           */
5914 5915          bzero(&req, sizeof (req));
5915 5916          bzero(&rep, sizeof (rep));
5916 5917  
5917 5918          req.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
5918 5919          req.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
5919 5920          req.DevHandle = LE_16(devhdl);
5920 5921  
5921 5922          ret = mptsas_do_passthru(mpt, (uint8_t *)&req, (uint8_t *)&rep, NULL,
5922 5923              sizeof (req), sizeof (rep), NULL, 0, NULL, 0, 60, FKIOCTL);
5923 5924          if (ret != 0) {
5924 5925                  cmn_err(CE_WARN, "mptsas_free_devhdl: passthru SAS IO Unit "
5925 5926                      "Control error %d", ret);
5926 5927                  return (DDI_FAILURE);
5927 5928          }
5928 5929  
5929 5930          /* do passthrough success, check the ioc status */
5930 5931          if (LE_16(rep.IOCStatus) != MPI2_IOCSTATUS_SUCCESS) {
5931 5932                  cmn_err(CE_WARN, "mptsas_free_devhdl: passthru SAS IO Unit "
5932 5933                      "Control IOCStatus %d", LE_16(rep.IOCStatus));
5933 5934                  return (DDI_FAILURE);
5934 5935          }
5935 5936  
5936 5937          return (DDI_SUCCESS);
5937 5938  }
5938 5939  
5939 5940  static void
5940 5941  mptsas_update_phymask(mptsas_t *mpt)
5941 5942  {
5942 5943          mptsas_phymask_t mask = 0, phy_mask;
5943 5944          char            *phy_mask_name;
5944 5945          uint8_t         current_port;
5945 5946          int             i, j;
5946 5947  
5947 5948          NDBG20(("mptsas%d update phymask ", mpt->m_instance));
5948 5949  
5949 5950          ASSERT(mutex_owned(&mpt->m_mutex));
5950 5951  
5951 5952          (void) mptsas_get_sas_io_unit_page(mpt);
5952 5953  
5953 5954          phy_mask_name = kmem_zalloc(MPTSAS_MAX_PHYS, KM_SLEEP);
5954 5955  
5955 5956          for (i = 0; i < mpt->m_num_phys; i++) {
5956 5957                  phy_mask = 0x00;
5957 5958  
5958 5959                  if (mpt->m_phy_info[i].attached_devhdl == 0)
5959 5960                          continue;
5960 5961  
5961 5962                  bzero(phy_mask_name, sizeof (phy_mask_name));
5962 5963  
5963 5964                  current_port = mpt->m_phy_info[i].port_num;
5964 5965  
5965 5966                  if ((mask & (1 << i)) != 0)
5966 5967                          continue;
5967 5968  
5968 5969                  for (j = 0; j < mpt->m_num_phys; j++) {
5969 5970                          if (mpt->m_phy_info[j].attached_devhdl &&
5970 5971                              (mpt->m_phy_info[j].port_num == current_port)) {
5971 5972                                  phy_mask |= (1 << j);
5972 5973                          }
5973 5974                  }
5974 5975                  mask = mask | phy_mask;
5975 5976  
5976 5977                  for (j = 0; j < mpt->m_num_phys; j++) {
5977 5978                          if ((phy_mask >> j) & 0x01) {
5978 5979                                  mpt->m_phy_info[j].phy_mask = phy_mask;
5979 5980                          }
5980 5981                  }
5981 5982  
5982 5983                  (void) sprintf(phy_mask_name, "%x", phy_mask);
5983 5984  
5984 5985                  mutex_exit(&mpt->m_mutex);
5985 5986                  /*
5986 5987                   * register a iport, if the port has already been existed
5987 5988                   * SCSA will do nothing and just return.
5988 5989                   */
5989 5990                  (void) scsi_hba_iport_register(mpt->m_dip, phy_mask_name);
5990 5991                  mutex_enter(&mpt->m_mutex);
5991 5992          }
5992 5993          kmem_free(phy_mask_name, MPTSAS_MAX_PHYS);
5993 5994          NDBG20(("mptsas%d update phymask return", mpt->m_instance));
5994 5995  }
5995 5996  
5996 5997  /*
5997 5998   * mptsas_handle_dr is a task handler for DR, the DR action includes:
5998 5999   * 1. Directly attched Device Added/Removed.
5999 6000   * 2. Expander Device Added/Removed.
6000 6001   * 3. Indirectly Attached Device Added/Expander.
6001 6002   * 4. LUNs of a existing device status change.
6002 6003   * 5. RAID volume created/deleted.
6003 6004   * 6. Member of RAID volume is released because of RAID deletion.
6004 6005   * 7. Physical disks are removed because of RAID creation.
6005 6006   */
6006 6007  static void
6007 6008  mptsas_handle_dr(void *args) {
6008 6009          mptsas_topo_change_list_t       *topo_node = NULL;
6009 6010          mptsas_topo_change_list_t       *save_node = NULL;
6010 6011          mptsas_t                        *mpt;
6011 6012          dev_info_t                      *parent = NULL;
6012 6013          mptsas_phymask_t                phymask = 0;
6013 6014          char                            *phy_mask_name;
6014 6015          uint8_t                         flags = 0, physport = 0xff;
6015 6016          uint8_t                         port_update = 0;
6016 6017          uint_t                          event;
6017 6018  
6018 6019          topo_node = (mptsas_topo_change_list_t *)args;
6019 6020  
6020 6021          mpt = topo_node->mpt;
6021 6022          event = topo_node->event;
6022 6023          flags = topo_node->flags;
6023 6024  
6024 6025          phy_mask_name = kmem_zalloc(MPTSAS_MAX_PHYS, KM_SLEEP);
6025 6026  
6026 6027          NDBG20(("mptsas%d handle_dr enter", mpt->m_instance));
6027 6028  
6028 6029          switch (event) {
6029 6030          case MPTSAS_DR_EVENT_RECONFIG_TARGET:
6030 6031                  if ((flags == MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE) ||
6031 6032                      (flags == MPTSAS_TOPO_FLAG_EXPANDER_ATTACHED_DEVICE) ||
6032 6033                      (flags == MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED)) {
6033 6034                          /*
6034 6035                           * Direct attached or expander attached device added
6035 6036                           * into system or a Phys Disk that is being unhidden.
6036 6037                           */
6037 6038                          port_update = 1;
6038 6039                  }
6039 6040                  break;
6040 6041          case MPTSAS_DR_EVENT_RECONFIG_SMP:
6041 6042                  /*
6042 6043                   * New expander added into system, it must be the head
6043 6044                   * of topo_change_list_t
6044 6045                   */
6045 6046                  port_update = 1;
6046 6047                  break;
6047 6048          default:
6048 6049                  port_update = 0;
6049 6050                  break;
6050 6051          }
6051 6052          /*
6052 6053           * All cases port_update == 1 may cause initiator port form change
6053 6054           */
6054 6055          mutex_enter(&mpt->m_mutex);
6055 6056          if (mpt->m_port_chng && port_update) {
6056 6057                  /*
6057 6058                   * mpt->m_port_chng flag indicates some PHYs of initiator
6058 6059                   * port have changed to online. So when expander added or
6059 6060                   * directly attached device online event come, we force to
6060 6061                   * update port information by issueing SAS IO Unit Page and
6061 6062                   * update PHYMASKs.
6062 6063                   */
6063 6064                  (void) mptsas_update_phymask(mpt);
6064 6065                  mpt->m_port_chng = 0;
6065 6066  
6066 6067          }
6067 6068          mutex_exit(&mpt->m_mutex);
6068 6069          while (topo_node) {
6069 6070                  phymask = 0;
6070 6071                  if (parent == NULL) {
6071 6072                          physport = topo_node->un.physport;
6072 6073                          event = topo_node->event;
6073 6074                          flags = topo_node->flags;
6074 6075                          if (event & (MPTSAS_DR_EVENT_OFFLINE_TARGET |
6075 6076                              MPTSAS_DR_EVENT_OFFLINE_SMP)) {
6076 6077                                  /*
6077 6078                                   * For all offline events, phymask is known
6078 6079                                   */
6079 6080                                  phymask = topo_node->un.phymask;
6080 6081                                  goto find_parent;
6081 6082                          }
6082 6083                          if (event & MPTSAS_TOPO_FLAG_REMOVE_HANDLE) {
6083 6084                                  goto handle_topo_change;
6084 6085                          }
6085 6086                          if (flags & MPTSAS_TOPO_FLAG_LUN_ASSOCIATED) {
6086 6087                                  phymask = topo_node->un.phymask;
6087 6088                                  goto find_parent;
6088 6089                          }
6089 6090  
6090 6091                          if ((flags ==
6091 6092                              MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED) &&
6092 6093                              (event == MPTSAS_DR_EVENT_RECONFIG_TARGET)) {
6093 6094                                  /*
6094 6095                                   * There is no any field in IR_CONFIG_CHANGE
6095 6096                                   * event indicate physport/phynum, let's get
6096 6097                                   * parent after SAS Device Page0 request.
6097 6098                                   */
6098 6099                                  goto handle_topo_change;
6099 6100                          }
6100 6101  
6101 6102                          mutex_enter(&mpt->m_mutex);
6102 6103                          if (flags == MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE) {
6103 6104                                  /*
6104 6105                                   * If the direct attached device added or a
6105 6106                                   * phys disk is being unhidden, argument
6106 6107                                   * physport actually is PHY#, so we have to get
6107 6108                                   * phymask according PHY#.
6108 6109                                   */
6109 6110                                  physport = mpt->m_phy_info[physport].port_num;
6110 6111                          }
6111 6112  
6112 6113                          /*
6113 6114                           * Translate physport to phymask so that we can search
6114 6115                           * parent dip.
6115 6116                           */
6116 6117                          phymask = mptsas_physport_to_phymask(mpt,
6117 6118                              physport);
6118 6119                          mutex_exit(&mpt->m_mutex);
6119 6120  
6120 6121  find_parent:
6121 6122                          bzero(phy_mask_name, MPTSAS_MAX_PHYS);
6122 6123                          /*
6123 6124                           * For RAID topology change node, write the iport name
6124 6125                           * as v0.
6125 6126                           */
6126 6127                          if (flags & MPTSAS_TOPO_FLAG_RAID_ASSOCIATED) {
6127 6128                                  (void) sprintf(phy_mask_name, "v0");
6128 6129                          } else {
6129 6130                                  /*
6130 6131                                   * phymask can bo 0 if the drive has been
6131 6132                                   * pulled by the time an add event is
6132 6133                                   * processed.  If phymask is 0, just skip this
6133 6134                                   * event and continue.
6134 6135                                   */
6135 6136                                  if (phymask == 0) {
6136 6137                                          mutex_enter(&mpt->m_mutex);
6137 6138                                          save_node = topo_node;
6138 6139                                          topo_node = topo_node->next;
6139 6140                                          ASSERT(save_node);
6140 6141                                          kmem_free(save_node,
6141 6142                                              sizeof (mptsas_topo_change_list_t));
6142 6143                                          mutex_exit(&mpt->m_mutex);
6143 6144  
6144 6145                                          parent = NULL;
6145 6146                                          continue;
6146 6147                                  }
6147 6148                                  (void) sprintf(phy_mask_name, "%x", phymask);
6148 6149                          }
6149 6150                          parent = scsi_hba_iport_find(mpt->m_dip,
6150 6151                              phy_mask_name);
6151 6152                          if (parent == NULL) {
6152 6153                                  mptsas_log(mpt, CE_WARN, "Failed to find an "
6153 6154                                      "iport, should not happen!");
6154 6155                                  goto out;
6155 6156                          }
6156 6157  
6157 6158                  }
6158 6159                  ASSERT(parent);
6159 6160  handle_topo_change:
6160 6161  
6161 6162                  mutex_enter(&mpt->m_mutex);
6162 6163  
6163 6164                  mptsas_handle_topo_change(topo_node, parent);
6164 6165                  save_node = topo_node;
6165 6166                  topo_node = topo_node->next;
6166 6167                  ASSERT(save_node);
6167 6168                  kmem_free(save_node, sizeof (mptsas_topo_change_list_t));
6168 6169                  mutex_exit(&mpt->m_mutex);
6169 6170  
6170 6171                  if ((flags == MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE) ||
6171 6172                      (flags == MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED) ||
6172 6173                      (flags == MPTSAS_TOPO_FLAG_RAID_ASSOCIATED)) {
6173 6174                          /*
6174 6175                           * If direct attached device associated, make sure
6175 6176                           * reset the parent before start the next one. But
6176 6177                           * all devices associated with expander shares the
6177 6178                           * parent.  Also, reset parent if this is for RAID.
6178 6179                           */
6179 6180                          parent = NULL;
6180 6181                  }
6181 6182          }
6182 6183  out:
6183 6184          kmem_free(phy_mask_name, MPTSAS_MAX_PHYS);
6184 6185  }
6185 6186  
6186 6187  static void
6187 6188  mptsas_handle_topo_change(mptsas_topo_change_list_t *topo_node,
6188 6189      dev_info_t *parent)
6189 6190  {
6190 6191          mptsas_target_t *ptgt = NULL;
6191 6192          mptsas_smp_t    *psmp = NULL;
6192 6193          mptsas_t        *mpt = (void *)topo_node->mpt;
6193 6194          uint16_t        devhdl;
6194 6195          uint16_t        attached_devhdl;
6195 6196          uint64_t        sas_wwn = 0;
6196 6197          int             rval = 0;
6197 6198          uint32_t        page_address;
6198 6199          uint8_t         phy, flags;
6199 6200          char            *addr = NULL;
6200 6201          dev_info_t      *lundip;
6201 6202          int             circ = 0, circ1 = 0;
6202 6203          char            attached_wwnstr[MPTSAS_WWN_STRLEN];
6203 6204  
6204 6205          NDBG20(("mptsas%d handle_topo_change enter", mpt->m_instance));
6205 6206  
6206 6207          ASSERT(mutex_owned(&mpt->m_mutex));
6207 6208  
6208 6209          switch (topo_node->event) {
6209 6210          case MPTSAS_DR_EVENT_RECONFIG_TARGET:
6210 6211          {
6211 6212                  char *phy_mask_name;
6212 6213                  mptsas_phymask_t phymask = 0;
6213 6214  
6214 6215                  if (topo_node->flags == MPTSAS_TOPO_FLAG_RAID_ASSOCIATED) {
6215 6216                          /*
6216 6217                           * Get latest RAID info.
6217 6218                           */
6218 6219                          (void) mptsas_get_raid_info(mpt);
6219 6220                          ptgt = mptsas_search_by_devhdl(
6220 6221                              &mpt->m_active->m_tgttbl, topo_node->devhdl);
6221 6222                          if (ptgt == NULL)
6222 6223                                  break;
6223 6224                  } else {
6224 6225                          ptgt = (void *)topo_node->object;
6225 6226                  }
6226 6227  
6227 6228                  if (ptgt == NULL) {
6228 6229                          /*
6229 6230                           * If a Phys Disk was deleted, RAID info needs to be
6230 6231                           * updated to reflect the new topology.
6231 6232                           */
6232 6233                          (void) mptsas_get_raid_info(mpt);
6233 6234  
6234 6235                          /*
6235 6236                           * Get sas device page 0 by DevHandle to make sure if
6236 6237                           * SSP/SATA end device exist.
6237 6238                           */
6238 6239                          page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
6239 6240                              MPI2_SAS_DEVICE_PGAD_FORM_MASK) |
6240 6241                              topo_node->devhdl;
6241 6242  
6242 6243                          rval = mptsas_get_target_device_info(mpt, page_address,
6243 6244                              &devhdl, &ptgt);
6244 6245                          if (rval == DEV_INFO_WRONG_DEVICE_TYPE) {
6245 6246                                  mptsas_log(mpt, CE_NOTE,
6246 6247                                      "mptsas_handle_topo_change: target %d is "
6247 6248                                      "not a SAS/SATA device. \n",
6248 6249                                      topo_node->devhdl);
6249 6250                          } else if (rval == DEV_INFO_FAIL_ALLOC) {
6250 6251                                  mptsas_log(mpt, CE_NOTE,
6251 6252                                      "mptsas_handle_topo_change: could not "
6252 6253                                      "allocate memory. \n");
6253 6254                          }
6254 6255                          /*
6255 6256                           * If rval is DEV_INFO_PHYS_DISK than there is nothing
6256 6257                           * else to do, just leave.
6257 6258                           */
6258 6259                          if (rval != DEV_INFO_SUCCESS) {
6259 6260                                  return;
6260 6261                          }
6261 6262                  }
6262 6263  
6263 6264                  ASSERT(ptgt->m_devhdl == topo_node->devhdl);
6264 6265  
6265 6266                  mutex_exit(&mpt->m_mutex);
6266 6267                  flags = topo_node->flags;
6267 6268  
6268 6269                  if (flags == MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED) {
6269 6270                          phymask = ptgt->m_phymask;
6270 6271                          phy_mask_name = kmem_zalloc(MPTSAS_MAX_PHYS, KM_SLEEP);
6271 6272                          (void) sprintf(phy_mask_name, "%x", phymask);
6272 6273                          parent = scsi_hba_iport_find(mpt->m_dip,
6273 6274                              phy_mask_name);
6274 6275                          kmem_free(phy_mask_name, MPTSAS_MAX_PHYS);
6275 6276                          if (parent == NULL) {
6276 6277                                  mptsas_log(mpt, CE_WARN, "Failed to find a "
6277 6278                                      "iport for PD, should not happen!");
6278 6279                                  mutex_enter(&mpt->m_mutex);
6279 6280                                  break;
6280 6281                          }
6281 6282                  }
6282 6283  
6283 6284                  if (flags == MPTSAS_TOPO_FLAG_RAID_ASSOCIATED) {
6284 6285                          ndi_devi_enter(parent, &circ1);
6285 6286                          (void) mptsas_config_raid(parent, topo_node->devhdl,
6286 6287                              &lundip);
6287 6288                          ndi_devi_exit(parent, circ1);
6288 6289                  } else {
6289 6290                          /*
6290 6291                           * hold nexus for bus configure
6291 6292                           */
6292 6293                          ndi_devi_enter(scsi_vhci_dip, &circ);
6293 6294                          ndi_devi_enter(parent, &circ1);
6294 6295                          rval = mptsas_config_target(parent, ptgt);
6295 6296                          /*
6296 6297                           * release nexus for bus configure
6297 6298                           */
6298 6299                          ndi_devi_exit(parent, circ1);
6299 6300                          ndi_devi_exit(scsi_vhci_dip, circ);
6300 6301  
6301 6302                          /*
6302 6303                           * Add parent's props for SMHBA support
6303 6304                           */
6304 6305                          if (flags == MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE) {
6305 6306                                  bzero(attached_wwnstr,
6306 6307                                      sizeof (attached_wwnstr));
6307 6308                                  (void) sprintf(attached_wwnstr, "w%016"PRIx64,
6308 6309                                      ptgt->m_sas_wwn);
6309 6310                                  if (ddi_prop_update_string(DDI_DEV_T_NONE,
6310 6311                                      parent,
6311 6312                                      SCSI_ADDR_PROP_ATTACHED_PORT,
6312 6313                                      attached_wwnstr)
6313 6314                                      != DDI_PROP_SUCCESS) {
6314 6315                                          (void) ddi_prop_remove(DDI_DEV_T_NONE,
6315 6316                                              parent,
6316 6317                                              SCSI_ADDR_PROP_ATTACHED_PORT);
6317 6318                                          mptsas_log(mpt, CE_WARN, "Failed to"
6318 6319                                              "attached-port props");
6319 6320                                          return;
6320 6321                                  }
6321 6322                                  if (ddi_prop_update_int(DDI_DEV_T_NONE, parent,
6322 6323                                      MPTSAS_NUM_PHYS, 1) !=
6323 6324                                      DDI_PROP_SUCCESS) {
6324 6325                                          (void) ddi_prop_remove(DDI_DEV_T_NONE,
6325 6326                                              parent, MPTSAS_NUM_PHYS);
6326 6327                                          mptsas_log(mpt, CE_WARN, "Failed to"
6327 6328                                              " create num-phys props");
6328 6329                                          return;
6329 6330                                  }
6330 6331  
6331 6332                                  /*
6332 6333                                   * Update PHY info for smhba
6333 6334                                   */
6334 6335                                  mutex_enter(&mpt->m_mutex);
6335 6336                                  if (mptsas_smhba_phy_init(mpt)) {
6336 6337                                          mutex_exit(&mpt->m_mutex);
6337 6338                                          mptsas_log(mpt, CE_WARN, "mptsas phy"
6338 6339                                              " update failed");
6339 6340                                          return;
6340 6341                                  }
6341 6342                                  mutex_exit(&mpt->m_mutex);
6342 6343                                  mptsas_smhba_set_phy_props(mpt,
6343 6344                                      ddi_get_name_addr(parent), parent,
6344 6345                                      1, &attached_devhdl);
6345 6346                                  if (ddi_prop_update_int(DDI_DEV_T_NONE, parent,
6346 6347                                      MPTSAS_VIRTUAL_PORT, 0) !=
6347 6348                                      DDI_PROP_SUCCESS) {
6348 6349                                          (void) ddi_prop_remove(DDI_DEV_T_NONE,
6349 6350                                              parent, MPTSAS_VIRTUAL_PORT);
6350 6351                                          mptsas_log(mpt, CE_WARN,
6351 6352                                              "mptsas virtual-port"
6352 6353                                              "port prop update failed");
6353 6354                                          return;
6354 6355                                  }
6355 6356                          }
6356 6357                  }
6357 6358                  mutex_enter(&mpt->m_mutex);
6358 6359  
6359 6360                  NDBG20(("mptsas%d handle_topo_change to online devhdl:%x, "
6360 6361                      "phymask:%x.", mpt->m_instance, ptgt->m_devhdl,
6361 6362                      ptgt->m_phymask));
6362 6363                  break;
6363 6364          }
6364 6365          case MPTSAS_DR_EVENT_OFFLINE_TARGET:
6365 6366          {
6366 6367                  mptsas_hash_table_t *tgttbl = &mpt->m_active->m_tgttbl;
6367 6368                  devhdl = topo_node->devhdl;
6368 6369                  ptgt = mptsas_search_by_devhdl(tgttbl, devhdl);
6369 6370                  if (ptgt == NULL)
6370 6371                          break;
6371 6372  
6372 6373                  sas_wwn = ptgt->m_sas_wwn;
6373 6374                  phy = ptgt->m_phynum;
6374 6375  
6375 6376                  addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
6376 6377  
6377 6378                  if (sas_wwn) {
6378 6379                          (void) sprintf(addr, "w%016"PRIx64, sas_wwn);
6379 6380                  } else {
6380 6381                          (void) sprintf(addr, "p%x", phy);
6381 6382                  }
6382 6383                  ASSERT(ptgt->m_devhdl == devhdl);
6383 6384  
6384 6385                  if ((topo_node->flags == MPTSAS_TOPO_FLAG_RAID_ASSOCIATED) ||
6385 6386                      (topo_node->flags ==
6386 6387                      MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED)) {
6387 6388                          /*
6388 6389                           * Get latest RAID info if RAID volume status changes
6389 6390                           * or Phys Disk status changes
6390 6391                           */
6391 6392                          (void) mptsas_get_raid_info(mpt);
6392 6393                  }
6393 6394                  /*
6394 6395                   * Abort all outstanding command on the device
6395 6396                   */
6396 6397                  rval = mptsas_do_scsi_reset(mpt, devhdl);
6397 6398                  if (rval) {
6398 6399                          NDBG20(("mptsas%d handle_topo_change to reset target "
6399 6400                              "before offline devhdl:%x, phymask:%x, rval:%x",
6400 6401                              mpt->m_instance, ptgt->m_devhdl, ptgt->m_phymask,
6401 6402                              rval));
6402 6403                  }
6403 6404  
6404 6405                  mutex_exit(&mpt->m_mutex);
6405 6406  
6406 6407                  ndi_devi_enter(scsi_vhci_dip, &circ);
6407 6408                  ndi_devi_enter(parent, &circ1);
6408 6409                  rval = mptsas_offline_target(parent, addr);
6409 6410                  ndi_devi_exit(parent, circ1);
6410 6411                  ndi_devi_exit(scsi_vhci_dip, circ);
6411 6412                  NDBG20(("mptsas%d handle_topo_change to offline devhdl:%x, "
6412 6413                      "phymask:%x, rval:%x", mpt->m_instance,
6413 6414                      ptgt->m_devhdl, ptgt->m_phymask, rval));
6414 6415  
6415 6416                  kmem_free(addr, SCSI_MAXNAMELEN);
6416 6417  
6417 6418                  /*
6418 6419                   * Clear parent's props for SMHBA support
6419 6420                   */
6420 6421                  flags = topo_node->flags;
6421 6422                  if (flags == MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE) {
6422 6423                          bzero(attached_wwnstr, sizeof (attached_wwnstr));
6423 6424                          if (ddi_prop_update_string(DDI_DEV_T_NONE, parent,
6424 6425                              SCSI_ADDR_PROP_ATTACHED_PORT, attached_wwnstr) !=
6425 6426                              DDI_PROP_SUCCESS) {
6426 6427                                  (void) ddi_prop_remove(DDI_DEV_T_NONE, parent,
6427 6428                                      SCSI_ADDR_PROP_ATTACHED_PORT);
6428 6429                                  mptsas_log(mpt, CE_WARN, "mptsas attached port "
6429 6430                                      "prop update failed");
6430 6431                                  break;
6431 6432                          }
6432 6433                          if (ddi_prop_update_int(DDI_DEV_T_NONE, parent,
6433 6434                              MPTSAS_NUM_PHYS, 0) !=
6434 6435                              DDI_PROP_SUCCESS) {
6435 6436                                  (void) ddi_prop_remove(DDI_DEV_T_NONE, parent,
6436 6437                                      MPTSAS_NUM_PHYS);
6437 6438                                  mptsas_log(mpt, CE_WARN, "mptsas num phys "
6438 6439                                      "prop update failed");
6439 6440                                  break;
6440 6441                          }
6441 6442                          if (ddi_prop_update_int(DDI_DEV_T_NONE, parent,
6442 6443                              MPTSAS_VIRTUAL_PORT, 1) !=
6443 6444                              DDI_PROP_SUCCESS) {
6444 6445                                  (void) ddi_prop_remove(DDI_DEV_T_NONE, parent,
6445 6446                                      MPTSAS_VIRTUAL_PORT);
6446 6447                                  mptsas_log(mpt, CE_WARN, "mptsas virtual port "
6447 6448                                      "prop update failed");
6448 6449                                  break;
6449 6450                          }
6450 6451                  }
6451 6452  
6452 6453                  mutex_enter(&mpt->m_mutex);
6453 6454                  if (mptsas_set_led_status(mpt, ptgt, 0) != DDI_SUCCESS) {
6454 6455                          NDBG14(("mptsas: clear LED for tgt %x failed",
6455 6456                              ptgt->m_slot_num));
6456 6457                  }
6457 6458                  if (rval == DDI_SUCCESS) {
6458 6459                          mptsas_tgt_free(&mpt->m_active->m_tgttbl,
6459 6460                              ptgt->m_sas_wwn, ptgt->m_phymask);
6460 6461                          ptgt = NULL;
6461 6462                  } else {
6462 6463                          /*
6463 6464                           * clean DR_INTRANSITION flag to allow I/O down to
6464 6465                           * PHCI driver since failover finished.
6465 6466                           * Invalidate the devhdl
6466 6467                           */
6467 6468                          mutex_enter(&ptgt->m_tgt_intr_mutex);
6468 6469                          ptgt->m_devhdl = MPTSAS_INVALID_DEVHDL;
6469 6470                          ptgt->m_tgt_unconfigured = 0;
6470 6471                          ptgt->m_dr_flag = MPTSAS_DR_INACTIVE;
6471 6472                          mutex_exit(&ptgt->m_tgt_intr_mutex);
6472 6473                  }
6473 6474  
6474 6475                  /*
6475 6476                   * Send SAS IO Unit Control to free the dev handle
6476 6477                   */
6477 6478                  if ((flags == MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE) ||
6478 6479                      (flags == MPTSAS_TOPO_FLAG_EXPANDER_ATTACHED_DEVICE)) {
6479 6480                          rval = mptsas_free_devhdl(mpt, devhdl);
6480 6481  
6481 6482                          NDBG20(("mptsas%d handle_topo_change to remove "
6482 6483                              "devhdl:%x, rval:%x", mpt->m_instance, devhdl,
6483 6484                              rval));
6484 6485                  }
6485 6486  
6486 6487                  break;
6487 6488          }
6488 6489          case MPTSAS_TOPO_FLAG_REMOVE_HANDLE:
6489 6490          {
6490 6491                  devhdl = topo_node->devhdl;
6491 6492                  /*
6492 6493                   * If this is the remove handle event, do a reset first.
6493 6494                   */
6494 6495                  if (topo_node->event == MPTSAS_TOPO_FLAG_REMOVE_HANDLE) {
6495 6496                          rval = mptsas_do_scsi_reset(mpt, devhdl);
6496 6497                          if (rval) {
6497 6498                                  NDBG20(("mpt%d reset target before remove "
6498 6499                                      "devhdl:%x, rval:%x", mpt->m_instance,
6499 6500                                      devhdl, rval));
6500 6501                          }
6501 6502                  }
6502 6503  
6503 6504                  /*
6504 6505                   * Send SAS IO Unit Control to free the dev handle
6505 6506                   */
6506 6507                  rval = mptsas_free_devhdl(mpt, devhdl);
6507 6508                  NDBG20(("mptsas%d handle_topo_change to remove "
6508 6509                      "devhdl:%x, rval:%x", mpt->m_instance, devhdl,
6509 6510                      rval));
6510 6511                  break;
6511 6512          }
6512 6513          case MPTSAS_DR_EVENT_RECONFIG_SMP:
6513 6514          {
6514 6515                  mptsas_smp_t smp;
6515 6516                  dev_info_t *smpdip;
6516 6517                  mptsas_hash_table_t *smptbl = &mpt->m_active->m_smptbl;
6517 6518  
6518 6519                  devhdl = topo_node->devhdl;
6519 6520  
6520 6521                  page_address = (MPI2_SAS_EXPAND_PGAD_FORM_HNDL &
6521 6522                      MPI2_SAS_EXPAND_PGAD_FORM_MASK) | (uint32_t)devhdl;
6522 6523                  rval = mptsas_get_sas_expander_page0(mpt, page_address, &smp);
6523 6524                  if (rval != DDI_SUCCESS) {
6524 6525                          mptsas_log(mpt, CE_WARN, "failed to online smp, "
6525 6526                              "handle %x", devhdl);
6526 6527                          return;
6527 6528                  }
6528 6529  
6529 6530                  psmp = mptsas_smp_alloc(smptbl, &smp);
6530 6531                  if (psmp == NULL) {
6531 6532                          return;
6532 6533                  }
6533 6534  
6534 6535                  mutex_exit(&mpt->m_mutex);
6535 6536                  ndi_devi_enter(parent, &circ1);
6536 6537                  (void) mptsas_online_smp(parent, psmp, &smpdip);
6537 6538                  ndi_devi_exit(parent, circ1);
6538 6539  
6539 6540                  mutex_enter(&mpt->m_mutex);
6540 6541                  break;
6541 6542          }
6542 6543          case MPTSAS_DR_EVENT_OFFLINE_SMP:
6543 6544          {
6544 6545                  mptsas_hash_table_t *smptbl = &mpt->m_active->m_smptbl;
6545 6546                  devhdl = topo_node->devhdl;
6546 6547                  uint32_t dev_info;
6547 6548  
6548 6549                  psmp = mptsas_search_by_devhdl(smptbl, devhdl);
6549 6550                  if (psmp == NULL)
6550 6551                          break;
6551 6552                  /*
6552 6553                   * The mptsas_smp_t data is released only if the dip is offlined
6553 6554                   * successfully.
6554 6555                   */
6555 6556                  mutex_exit(&mpt->m_mutex);
6556 6557  
6557 6558                  ndi_devi_enter(parent, &circ1);
6558 6559                  rval = mptsas_offline_smp(parent, psmp, NDI_DEVI_REMOVE);
6559 6560                  ndi_devi_exit(parent, circ1);
6560 6561  
6561 6562                  dev_info = psmp->m_deviceinfo;
6562 6563                  if ((dev_info & DEVINFO_DIRECT_ATTACHED) ==
6563 6564                      DEVINFO_DIRECT_ATTACHED) {
6564 6565                          if (ddi_prop_update_int(DDI_DEV_T_NONE, parent,
6565 6566                              MPTSAS_VIRTUAL_PORT, 1) !=
6566 6567                              DDI_PROP_SUCCESS) {
6567 6568                                  (void) ddi_prop_remove(DDI_DEV_T_NONE, parent,
6568 6569                                      MPTSAS_VIRTUAL_PORT);
6569 6570                                  mptsas_log(mpt, CE_WARN, "mptsas virtual port "
6570 6571                                      "prop update failed");
6571 6572                                  return;
6572 6573                          }
6573 6574                          /*
6574 6575                           * Check whether the smp connected to the iport,
6575 6576                           */
6576 6577                          if (ddi_prop_update_int(DDI_DEV_T_NONE, parent,
6577 6578                              MPTSAS_NUM_PHYS, 0) !=
6578 6579                              DDI_PROP_SUCCESS) {
6579 6580                                  (void) ddi_prop_remove(DDI_DEV_T_NONE, parent,
6580 6581                                      MPTSAS_NUM_PHYS);
6581 6582                                  mptsas_log(mpt, CE_WARN, "mptsas num phys"
6582 6583                                      "prop update failed");
6583 6584                                  return;
6584 6585                          }
6585 6586                          /*
6586 6587                           * Clear parent's attached-port props
6587 6588                           */
6588 6589                          bzero(attached_wwnstr, sizeof (attached_wwnstr));
6589 6590                          if (ddi_prop_update_string(DDI_DEV_T_NONE, parent,
6590 6591                              SCSI_ADDR_PROP_ATTACHED_PORT, attached_wwnstr) !=
6591 6592                              DDI_PROP_SUCCESS) {
6592 6593                                  (void) ddi_prop_remove(DDI_DEV_T_NONE, parent,
6593 6594                                      SCSI_ADDR_PROP_ATTACHED_PORT);
6594 6595                                  mptsas_log(mpt, CE_WARN, "mptsas attached port "
6595 6596                                      "prop update failed");
6596 6597                                  return;
6597 6598                          }
6598 6599                  }
6599 6600  
6600 6601                  mutex_enter(&mpt->m_mutex);
6601 6602                  NDBG20(("mptsas%d handle_topo_change to remove devhdl:%x, "
6602 6603                      "rval:%x", mpt->m_instance, psmp->m_devhdl, rval));
6603 6604                  if (rval == DDI_SUCCESS) {
6604 6605                          mptsas_smp_free(smptbl, psmp->m_sasaddr,
6605 6606                              psmp->m_phymask);
6606 6607                  } else {
6607 6608                          psmp->m_devhdl = MPTSAS_INVALID_DEVHDL;
6608 6609                  }
6609 6610  
6610 6611                  bzero(attached_wwnstr, sizeof (attached_wwnstr));
6611 6612  
6612 6613                  break;
6613 6614          }
6614 6615          default:
6615 6616                  return;
6616 6617          }
6617 6618  }
6618 6619  
6619 6620  /*
6620 6621   * Record the event if its type is enabled in mpt instance by ioctl.
6621 6622   */
6622 6623  static void
6623 6624  mptsas_record_event(void *args)
6624 6625  {
6625 6626          m_replyh_arg_t                  *replyh_arg;
6626 6627          pMpi2EventNotificationReply_t   eventreply;
6627 6628          uint32_t                        event, rfm;
6628 6629          mptsas_t                        *mpt;
6629 6630          int                             i, j;
6630 6631          uint16_t                        event_data_len;
6631 6632          boolean_t                       sendAEN = FALSE;
6632 6633  
6633 6634          replyh_arg = (m_replyh_arg_t *)args;
6634 6635          rfm = replyh_arg->rfm;
6635 6636          mpt = replyh_arg->mpt;
6636 6637  
6637 6638          eventreply = (pMpi2EventNotificationReply_t)
6638 6639              (mpt->m_reply_frame + (rfm - mpt->m_reply_frame_dma_addr));
6639 6640          event = ddi_get16(mpt->m_acc_reply_frame_hdl, &eventreply->Event);
6640 6641  
6641 6642  
6642 6643          /*
6643 6644           * Generate a system event to let anyone who cares know that a
6644 6645           * LOG_ENTRY_ADDED event has occurred.  This is sent no matter what the
6645 6646           * event mask is set to.
6646 6647           */
6647 6648          if (event == MPI2_EVENT_LOG_ENTRY_ADDED) {
6648 6649                  sendAEN = TRUE;
6649 6650          }
6650 6651  
6651 6652          /*
6652 6653           * Record the event only if it is not masked.  Determine which dword
6653 6654           * and bit of event mask to test.
6654 6655           */
6655 6656          i = (uint8_t)(event / 32);
6656 6657          j = (uint8_t)(event % 32);
6657 6658          if ((i < 4) && ((1 << j) & mpt->m_event_mask[i])) {
6658 6659                  i = mpt->m_event_index;
6659 6660                  mpt->m_events[i].Type = event;
6660 6661                  mpt->m_events[i].Number = ++mpt->m_event_number;
6661 6662                  bzero(mpt->m_events[i].Data, MPTSAS_MAX_EVENT_DATA_LENGTH * 4);
6662 6663                  event_data_len = ddi_get16(mpt->m_acc_reply_frame_hdl,
6663 6664                      &eventreply->EventDataLength);
6664 6665  
6665 6666                  if (event_data_len > 0) {
6666 6667                          /*
6667 6668                           * Limit data to size in m_event entry
6668 6669                           */
6669 6670                          if (event_data_len > MPTSAS_MAX_EVENT_DATA_LENGTH) {
6670 6671                                  event_data_len = MPTSAS_MAX_EVENT_DATA_LENGTH;
6671 6672                          }
6672 6673                          for (j = 0; j < event_data_len; j++) {
6673 6674                                  mpt->m_events[i].Data[j] =
6674 6675                                      ddi_get32(mpt->m_acc_reply_frame_hdl,
6675 6676                                      &(eventreply->EventData[j]));
6676 6677                          }
6677 6678  
6678 6679                          /*
6679 6680                           * check for index wrap-around
6680 6681                           */
6681 6682                          if (++i == MPTSAS_EVENT_QUEUE_SIZE) {
6682 6683                                  i = 0;
6683 6684                          }
6684 6685                          mpt->m_event_index = (uint8_t)i;
6685 6686  
6686 6687                          /*
6687 6688                           * Set flag to send the event.
6688 6689                           */
6689 6690                          sendAEN = TRUE;
6690 6691                  }
6691 6692          }
6692 6693  
6693 6694          /*
6694 6695           * Generate a system event if flag is set to let anyone who cares know
6695 6696           * that an event has occurred.
6696 6697           */
6697 6698          if (sendAEN) {
6698 6699                  (void) ddi_log_sysevent(mpt->m_dip, DDI_VENDOR_LSI, "MPT_SAS",
6699 6700                      "SAS", NULL, NULL, DDI_NOSLEEP);
6700 6701          }
6701 6702  }
6702 6703  
6703 6704  #define SMP_RESET_IN_PROGRESS MPI2_EVENT_SAS_TOPO_LR_SMP_RESET_IN_PROGRESS
6704 6705  /*
6705 6706   * handle sync events from ioc in interrupt
6706 6707   * return value:
6707 6708   * DDI_SUCCESS: The event is handled by this func
6708 6709   * DDI_FAILURE: Event is not handled
6709 6710   */
6710 6711  static int
6711 6712  mptsas_handle_event_sync(void *args)
6712 6713  {
6713 6714          m_replyh_arg_t                  *replyh_arg;
6714 6715          pMpi2EventNotificationReply_t   eventreply;
6715 6716          uint32_t                        event, rfm;
6716 6717          mptsas_t                        *mpt;
6717 6718          uint_t                          iocstatus;
6718 6719  
6719 6720          replyh_arg = (m_replyh_arg_t *)args;
6720 6721          rfm = replyh_arg->rfm;
6721 6722          mpt = replyh_arg->mpt;
6722 6723  
6723 6724          ASSERT(mutex_owned(&mpt->m_mutex));
6724 6725  
6725 6726          eventreply = (pMpi2EventNotificationReply_t)
6726 6727              (mpt->m_reply_frame + (rfm - mpt->m_reply_frame_dma_addr));
6727 6728          event = ddi_get16(mpt->m_acc_reply_frame_hdl, &eventreply->Event);
6728 6729  
6729 6730          if (iocstatus = ddi_get16(mpt->m_acc_reply_frame_hdl,
6730 6731              &eventreply->IOCStatus)) {
6731 6732                  if (iocstatus == MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
6732 6733                          mptsas_log(mpt, CE_WARN,
6733 6734                              "!mptsas_handle_event_sync: IOCStatus=0x%x, "
6734 6735                              "IOCLogInfo=0x%x", iocstatus,
6735 6736                              ddi_get32(mpt->m_acc_reply_frame_hdl,
6736 6737                              &eventreply->IOCLogInfo));
6737 6738                  } else {
6738 6739                          mptsas_log(mpt, CE_WARN,
6739 6740                              "mptsas_handle_event_sync: IOCStatus=0x%x, "
6740 6741                              "IOCLogInfo=0x%x", iocstatus,
6741 6742                              ddi_get32(mpt->m_acc_reply_frame_hdl,
6742 6743                              &eventreply->IOCLogInfo));
6743 6744                  }
6744 6745          }
6745 6746  
6746 6747          /*
6747 6748           * figure out what kind of event we got and handle accordingly
6748 6749           */
6749 6750          switch (event) {
6750 6751          case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
6751 6752          {
6752 6753                  pMpi2EventDataSasTopologyChangeList_t   sas_topo_change_list;
6753 6754                  uint8_t                         num_entries, expstatus, phy;
6754 6755                  uint8_t                         phystatus, physport, state, i;
6755 6756                  uint8_t                         start_phy_num, link_rate;
6756 6757                  uint16_t                        dev_handle, reason_code;
6757 6758                  uint16_t                        enc_handle, expd_handle;
6758 6759                  char                            string[80], curr[80], prev[80];
6759 6760                  mptsas_topo_change_list_t       *topo_head = NULL;
6760 6761                  mptsas_topo_change_list_t       *topo_tail = NULL;
6761 6762                  mptsas_topo_change_list_t       *topo_node = NULL;
6762 6763                  mptsas_target_t                 *ptgt;
6763 6764                  mptsas_smp_t                    *psmp;
6764 6765                  mptsas_hash_table_t             *tgttbl, *smptbl;
6765 6766                  uint8_t                         flags = 0, exp_flag;
6766 6767                  smhba_info_t                    *pSmhba = NULL;
6767 6768  
6768 6769                  NDBG20(("mptsas_handle_event_sync: SAS topology change"));
6769 6770  
6770 6771                  tgttbl = &mpt->m_active->m_tgttbl;
6771 6772                  smptbl = &mpt->m_active->m_smptbl;
6772 6773  
6773 6774                  sas_topo_change_list = (pMpi2EventDataSasTopologyChangeList_t)
6774 6775                      eventreply->EventData;
6775 6776  
6776 6777                  enc_handle = ddi_get16(mpt->m_acc_reply_frame_hdl,
6777 6778                      &sas_topo_change_list->EnclosureHandle);
6778 6779                  expd_handle = ddi_get16(mpt->m_acc_reply_frame_hdl,
6779 6780                      &sas_topo_change_list->ExpanderDevHandle);
6780 6781                  num_entries = ddi_get8(mpt->m_acc_reply_frame_hdl,
6781 6782                      &sas_topo_change_list->NumEntries);
6782 6783                  start_phy_num = ddi_get8(mpt->m_acc_reply_frame_hdl,
6783 6784                      &sas_topo_change_list->StartPhyNum);
6784 6785                  expstatus = ddi_get8(mpt->m_acc_reply_frame_hdl,
6785 6786                      &sas_topo_change_list->ExpStatus);
6786 6787                  physport = ddi_get8(mpt->m_acc_reply_frame_hdl,
6787 6788                      &sas_topo_change_list->PhysicalPort);
6788 6789  
6789 6790                  string[0] = 0;
6790 6791                  if (expd_handle) {
6791 6792                          flags = MPTSAS_TOPO_FLAG_EXPANDER_ASSOCIATED;
6792 6793                          switch (expstatus) {
6793 6794                          case MPI2_EVENT_SAS_TOPO_ES_ADDED:
6794 6795                                  (void) sprintf(string, " added");
6795 6796                                  /*
6796 6797                                   * New expander device added
6797 6798                                   */
6798 6799                                  mpt->m_port_chng = 1;
6799 6800                                  topo_node = kmem_zalloc(
6800 6801                                      sizeof (mptsas_topo_change_list_t),
6801 6802                                      KM_SLEEP);
6802 6803                                  topo_node->mpt = mpt;
6803 6804                                  topo_node->event = MPTSAS_DR_EVENT_RECONFIG_SMP;
6804 6805                                  topo_node->un.physport = physport;
6805 6806                                  topo_node->devhdl = expd_handle;
6806 6807                                  topo_node->flags = flags;
6807 6808                                  topo_node->object = NULL;
6808 6809                                  if (topo_head == NULL) {
6809 6810                                          topo_head = topo_tail = topo_node;
6810 6811                                  } else {
6811 6812                                          topo_tail->next = topo_node;
6812 6813                                          topo_tail = topo_node;
6813 6814                                  }
6814 6815                                  break;
6815 6816                          case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
6816 6817                                  (void) sprintf(string, " not responding, "
6817 6818                                      "removed");
6818 6819                                  psmp = mptsas_search_by_devhdl(smptbl,
6819 6820                                      expd_handle);
6820 6821                                  if (psmp == NULL)
6821 6822                                          break;
6822 6823  
6823 6824                                  topo_node = kmem_zalloc(
6824 6825                                      sizeof (mptsas_topo_change_list_t),
6825 6826                                      KM_SLEEP);
6826 6827                                  topo_node->mpt = mpt;
6827 6828                                  topo_node->un.phymask = psmp->m_phymask;
6828 6829                                  topo_node->event = MPTSAS_DR_EVENT_OFFLINE_SMP;
6829 6830                                  topo_node->devhdl = expd_handle;
6830 6831                                  topo_node->flags = flags;
6831 6832                                  topo_node->object = NULL;
6832 6833                                  if (topo_head == NULL) {
6833 6834                                          topo_head = topo_tail = topo_node;
6834 6835                                  } else {
6835 6836                                          topo_tail->next = topo_node;
6836 6837                                          topo_tail = topo_node;
6837 6838                                  }
6838 6839                                  break;
6839 6840                          case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
6840 6841                                  break;
6841 6842                          case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
6842 6843                                  (void) sprintf(string, " not responding, "
6843 6844                                      "delaying removal");
6844 6845                                  break;
6845 6846                          default:
6846 6847                                  break;
6847 6848                          }
6848 6849                  } else {
6849 6850                          flags = MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE;
6850 6851                  }
6851 6852  
6852 6853                  NDBG20(("SAS TOPOLOGY CHANGE for enclosure %x expander %x%s\n",
6853 6854                      enc_handle, expd_handle, string));
6854 6855                  for (i = 0; i < num_entries; i++) {
6855 6856                          phy = i + start_phy_num;
6856 6857                          phystatus = ddi_get8(mpt->m_acc_reply_frame_hdl,
6857 6858                              &sas_topo_change_list->PHY[i].PhyStatus);
6858 6859                          dev_handle = ddi_get16(mpt->m_acc_reply_frame_hdl,
6859 6860                              &sas_topo_change_list->PHY[i].AttachedDevHandle);
6860 6861                          reason_code = phystatus & MPI2_EVENT_SAS_TOPO_RC_MASK;
6861 6862                          /*
6862 6863                           * Filter out processing of Phy Vacant Status unless
6863 6864                           * the reason code is "Not Responding".  Process all
6864 6865                           * other combinations of Phy Status and Reason Codes.
6865 6866                           */
6866 6867                          if ((phystatus &
6867 6868                              MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) &&
6868 6869                              (reason_code !=
6869 6870                              MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)) {
6870 6871                                  continue;
6871 6872                          }
6872 6873                          curr[0] = 0;
6873 6874                          prev[0] = 0;
6874 6875                          string[0] = 0;
6875 6876                          switch (reason_code) {
6876 6877                          case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
6877 6878                          {
6878 6879                                  NDBG20(("mptsas%d phy %d physical_port %d "
6879 6880                                      "dev_handle %d added", mpt->m_instance, phy,
6880 6881                                      physport, dev_handle));
6881 6882                                  link_rate = ddi_get8(mpt->m_acc_reply_frame_hdl,
6882 6883                                      &sas_topo_change_list->PHY[i].LinkRate);
6883 6884                                  state = (link_rate &
6884 6885                                      MPI2_EVENT_SAS_TOPO_LR_CURRENT_MASK) >>
6885 6886                                      MPI2_EVENT_SAS_TOPO_LR_CURRENT_SHIFT;
6886 6887                                  switch (state) {
6887 6888                                  case MPI2_EVENT_SAS_TOPO_LR_PHY_DISABLED:
6888 6889                                          (void) sprintf(curr, "is disabled");
6889 6890                                          break;
6890 6891                                  case MPI2_EVENT_SAS_TOPO_LR_NEGOTIATION_FAILED:
6891 6892                                          (void) sprintf(curr, "is offline, "
6892 6893                                              "failed speed negotiation");
6893 6894                                          break;
6894 6895                                  case MPI2_EVENT_SAS_TOPO_LR_SATA_OOB_COMPLETE:
6895 6896                                          (void) sprintf(curr, "SATA OOB "
6896 6897                                              "complete");
6897 6898                                          break;
6898 6899                                  case SMP_RESET_IN_PROGRESS:
6899 6900                                          (void) sprintf(curr, "SMP reset in "
6900 6901                                              "progress");
6901 6902                                          break;
6902 6903                                  case MPI2_EVENT_SAS_TOPO_LR_RATE_1_5:
6903 6904                                          (void) sprintf(curr, "is online at "
6904 6905                                              "1.5 Gbps");
6905 6906                                          break;
6906 6907                                  case MPI2_EVENT_SAS_TOPO_LR_RATE_3_0:
6907 6908                                          (void) sprintf(curr, "is online at 3.0 "
6908 6909                                              "Gbps");
6909 6910                                          break;
6910 6911                                  case MPI2_EVENT_SAS_TOPO_LR_RATE_6_0:
6911 6912                                          (void) sprintf(curr, "is online at 6.0 "
6912 6913                                              "Gbps");
6913 6914                                          break;
6914 6915                                  default:
6915 6916                                          (void) sprintf(curr, "state is "
6916 6917                                              "unknown");
6917 6918                                          break;
6918 6919                                  }
6919 6920                                  /*
6920 6921                                   * New target device added into the system.
6921 6922                                   * Set association flag according to if an
6922 6923                                   * expander is used or not.
6923 6924                                   */
6924 6925                                  exp_flag =
6925 6926                                      MPTSAS_TOPO_FLAG_EXPANDER_ATTACHED_DEVICE;
6926 6927                                  if (flags ==
6927 6928                                      MPTSAS_TOPO_FLAG_EXPANDER_ASSOCIATED) {
6928 6929                                          flags = exp_flag;
6929 6930                                  }
6930 6931                                  topo_node = kmem_zalloc(
6931 6932                                      sizeof (mptsas_topo_change_list_t),
6932 6933                                      KM_SLEEP);
6933 6934                                  topo_node->mpt = mpt;
6934 6935                                  topo_node->event =
6935 6936                                      MPTSAS_DR_EVENT_RECONFIG_TARGET;
6936 6937                                  if (expd_handle == 0) {
6937 6938                                          /*
6938 6939                                           * Per MPI 2, if expander dev handle
6939 6940                                           * is 0, it's a directly attached
6940 6941                                           * device. So driver use PHY to decide
6941 6942                                           * which iport is associated
6942 6943                                           */
6943 6944                                          physport = phy;
6944 6945                                          mpt->m_port_chng = 1;
6945 6946                                  }
6946 6947                                  topo_node->un.physport = physport;
6947 6948                                  topo_node->devhdl = dev_handle;
6948 6949                                  topo_node->flags = flags;
6949 6950                                  topo_node->object = NULL;
6950 6951                                  if (topo_head == NULL) {
6951 6952                                          topo_head = topo_tail = topo_node;
6952 6953                                  } else {
6953 6954                                          topo_tail->next = topo_node;
6954 6955                                          topo_tail = topo_node;
6955 6956                                  }
6956 6957                                  break;
6957 6958                          }
6958 6959                          case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
6959 6960                          {
6960 6961                                  NDBG20(("mptsas%d phy %d physical_port %d "
6961 6962                                      "dev_handle %d removed", mpt->m_instance,
6962 6963                                      phy, physport, dev_handle));
6963 6964                                  /*
6964 6965                                   * Set association flag according to if an
6965 6966                                   * expander is used or not.
6966 6967                                   */
6967 6968                                  exp_flag =
6968 6969                                      MPTSAS_TOPO_FLAG_EXPANDER_ATTACHED_DEVICE;
6969 6970                                  if (flags ==
6970 6971                                      MPTSAS_TOPO_FLAG_EXPANDER_ASSOCIATED) {
6971 6972                                          flags = exp_flag;
6972 6973                                  }
6973 6974                                  /*
6974 6975                                   * Target device is removed from the system
6975 6976                                   * Before the device is really offline from
6976 6977                                   * from system.
6977 6978                                   */
6978 6979                                  ptgt = mptsas_search_by_devhdl(tgttbl,
6979 6980                                      dev_handle);
6980 6981                                  /*
6981 6982                                   * If ptgt is NULL here, it means that the
6982 6983                                   * DevHandle is not in the hash table.  This is
6983 6984                                   * reasonable sometimes.  For example, if a
6984 6985                                   * disk was pulled, then added, then pulled
6985 6986                                   * again, the disk will not have been put into
6986 6987                                   * the hash table because the add event will
6987 6988                                   * have an invalid phymask.  BUT, this does not
6988 6989                                   * mean that the DevHandle is invalid.  The
6989 6990                                   * controller will still have a valid DevHandle
6990 6991                                   * that must be removed.  To do this, use the
6991 6992                                   * MPTSAS_TOPO_FLAG_REMOVE_HANDLE event.
6992 6993                                   */
6993 6994                                  if (ptgt == NULL) {
6994 6995                                          topo_node = kmem_zalloc(
6995 6996                                              sizeof (mptsas_topo_change_list_t),
6996 6997                                              KM_SLEEP);
6997 6998                                          topo_node->mpt = mpt;
6998 6999                                          topo_node->un.phymask = 0;
6999 7000                                          topo_node->event =
7000 7001                                              MPTSAS_TOPO_FLAG_REMOVE_HANDLE;
7001 7002                                          topo_node->devhdl = dev_handle;
7002 7003                                          topo_node->flags = flags;
7003 7004                                          topo_node->object = NULL;
7004 7005                                          if (topo_head == NULL) {
7005 7006                                                  topo_head = topo_tail =
7006 7007                                                      topo_node;
7007 7008                                          } else {
7008 7009                                                  topo_tail->next = topo_node;
7009 7010                                                  topo_tail = topo_node;
7010 7011                                          }
7011 7012                                          break;
7012 7013                                  }
7013 7014  
7014 7015                                  /*
7015 7016                                   * Update DR flag immediately avoid I/O failure
7016 7017                                   * before failover finish. Pay attention to the
7017 7018                                   * mutex protect, we need grab the per target
7018 7019                                   * mutex during set m_dr_flag because the
7019 7020                                   * m_mutex would not be held all the time in
7020 7021                                   * mptsas_scsi_start().
7021 7022                                   */
7022 7023                                  mutex_enter(&ptgt->m_tgt_intr_mutex);
7023 7024                                  ptgt->m_dr_flag = MPTSAS_DR_INTRANSITION;
7024 7025                                  mutex_exit(&ptgt->m_tgt_intr_mutex);
7025 7026  
7026 7027                                  topo_node = kmem_zalloc(
7027 7028                                      sizeof (mptsas_topo_change_list_t),
7028 7029                                      KM_SLEEP);
7029 7030                                  topo_node->mpt = mpt;
7030 7031                                  topo_node->un.phymask = ptgt->m_phymask;
7031 7032                                  topo_node->event =
7032 7033                                      MPTSAS_DR_EVENT_OFFLINE_TARGET;
7033 7034                                  topo_node->devhdl = dev_handle;
7034 7035                                  topo_node->flags = flags;
7035 7036                                  topo_node->object = NULL;
7036 7037                                  if (topo_head == NULL) {
7037 7038                                          topo_head = topo_tail = topo_node;
7038 7039                                  } else {
7039 7040                                          topo_tail->next = topo_node;
7040 7041                                          topo_tail = topo_node;
7041 7042                                  }
7042 7043                                  break;
7043 7044                          }
7044 7045                          case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
7045 7046                                  link_rate = ddi_get8(mpt->m_acc_reply_frame_hdl,
7046 7047                                      &sas_topo_change_list->PHY[i].LinkRate);
7047 7048                                  state = (link_rate &
7048 7049                                      MPI2_EVENT_SAS_TOPO_LR_CURRENT_MASK) >>
7049 7050                                      MPI2_EVENT_SAS_TOPO_LR_CURRENT_SHIFT;
7050 7051                                  pSmhba = &mpt->m_phy_info[i].smhba_info;
7051 7052                                  pSmhba->negotiated_link_rate = state;
7052 7053                                  switch (state) {
7053 7054                                  case MPI2_EVENT_SAS_TOPO_LR_PHY_DISABLED:
7054 7055                                          (void) sprintf(curr, "is disabled");
7055 7056                                          mptsas_smhba_log_sysevent(mpt,
7056 7057                                              ESC_SAS_PHY_EVENT,
7057 7058                                              SAS_PHY_REMOVE,
7058 7059                                              &mpt->m_phy_info[i].smhba_info);
7059 7060                                          mpt->m_phy_info[i].smhba_info.
7060 7061                                              negotiated_link_rate
7061 7062                                              = 0x1;
7062 7063                                          break;
7063 7064                                  case MPI2_EVENT_SAS_TOPO_LR_NEGOTIATION_FAILED:
7064 7065                                          (void) sprintf(curr, "is offline, "
7065 7066                                              "failed speed negotiation");
7066 7067                                          mptsas_smhba_log_sysevent(mpt,
7067 7068                                              ESC_SAS_PHY_EVENT,
7068 7069                                              SAS_PHY_OFFLINE,
7069 7070                                              &mpt->m_phy_info[i].smhba_info);
7070 7071                                          break;
7071 7072                                  case MPI2_EVENT_SAS_TOPO_LR_SATA_OOB_COMPLETE:
7072 7073                                          (void) sprintf(curr, "SATA OOB "
7073 7074                                              "complete");
7074 7075                                          break;
7075 7076                                  case SMP_RESET_IN_PROGRESS:
7076 7077                                          (void) sprintf(curr, "SMP reset in "
7077 7078                                              "progress");
7078 7079                                          break;
7079 7080                                  case MPI2_EVENT_SAS_TOPO_LR_RATE_1_5:
7080 7081                                          (void) sprintf(curr, "is online at "
7081 7082                                              "1.5 Gbps");
7082 7083                                          if ((expd_handle == 0) &&
7083 7084                                              (enc_handle == 1)) {
7084 7085                                                  mpt->m_port_chng = 1;
7085 7086                                          }
7086 7087                                          mptsas_smhba_log_sysevent(mpt,
7087 7088                                              ESC_SAS_PHY_EVENT,
7088 7089                                              SAS_PHY_ONLINE,
7089 7090                                              &mpt->m_phy_info[i].smhba_info);
7090 7091                                          break;
7091 7092                                  case MPI2_EVENT_SAS_TOPO_LR_RATE_3_0:
7092 7093                                          (void) sprintf(curr, "is online at 3.0 "
7093 7094                                              "Gbps");
7094 7095                                          if ((expd_handle == 0) &&
7095 7096                                              (enc_handle == 1)) {
7096 7097                                                  mpt->m_port_chng = 1;
7097 7098                                          }
7098 7099                                          mptsas_smhba_log_sysevent(mpt,
7099 7100                                              ESC_SAS_PHY_EVENT,
7100 7101                                              SAS_PHY_ONLINE,
7101 7102                                              &mpt->m_phy_info[i].smhba_info);
7102 7103                                          break;
7103 7104                                  case MPI2_EVENT_SAS_TOPO_LR_RATE_6_0:
7104 7105                                          (void) sprintf(curr, "is online at "
7105 7106                                              "6.0 Gbps");
7106 7107                                          if ((expd_handle == 0) &&
7107 7108                                              (enc_handle == 1)) {
7108 7109                                                  mpt->m_port_chng = 1;
7109 7110                                          }
7110 7111                                          mptsas_smhba_log_sysevent(mpt,
7111 7112                                              ESC_SAS_PHY_EVENT,
7112 7113                                              SAS_PHY_ONLINE,
7113 7114                                              &mpt->m_phy_info[i].smhba_info);
7114 7115                                          break;
7115 7116                                  default:
7116 7117                                          (void) sprintf(curr, "state is "
7117 7118                                              "unknown");
7118 7119                                          break;
7119 7120                                  }
7120 7121  
7121 7122                                  state = (link_rate &
7122 7123                                      MPI2_EVENT_SAS_TOPO_LR_PREV_MASK) >>
7123 7124                                      MPI2_EVENT_SAS_TOPO_LR_PREV_SHIFT;
7124 7125                                  switch (state) {
7125 7126                                  case MPI2_EVENT_SAS_TOPO_LR_PHY_DISABLED:
7126 7127                                          (void) sprintf(prev, ", was disabled");
7127 7128                                          break;
7128 7129                                  case MPI2_EVENT_SAS_TOPO_LR_NEGOTIATION_FAILED:
7129 7130                                          (void) sprintf(prev, ", was offline, "
7130 7131                                              "failed speed negotiation");
7131 7132                                          break;
7132 7133                                  case MPI2_EVENT_SAS_TOPO_LR_SATA_OOB_COMPLETE:
7133 7134                                          (void) sprintf(prev, ", was SATA OOB "
7134 7135                                              "complete");
7135 7136                                          break;
7136 7137                                  case SMP_RESET_IN_PROGRESS:
7137 7138                                          (void) sprintf(prev, ", was SMP reset "
7138 7139                                              "in progress");
7139 7140                                          break;
7140 7141                                  case MPI2_EVENT_SAS_TOPO_LR_RATE_1_5:
7141 7142                                          (void) sprintf(prev, ", was online at "
7142 7143                                              "1.5 Gbps");
7143 7144                                          break;
7144 7145                                  case MPI2_EVENT_SAS_TOPO_LR_RATE_3_0:
7145 7146                                          (void) sprintf(prev, ", was online at "
7146 7147                                              "3.0 Gbps");
7147 7148                                          break;
7148 7149                                  case MPI2_EVENT_SAS_TOPO_LR_RATE_6_0:
7149 7150                                          (void) sprintf(prev, ", was online at "
7150 7151                                              "6.0 Gbps");
7151 7152                                          break;
7152 7153                                  default:
7153 7154                                  break;
7154 7155                                  }
7155 7156                                  (void) sprintf(&string[strlen(string)], "link "
7156 7157                                      "changed, ");
7157 7158                                  break;
7158 7159                          case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
7159 7160                                  continue;
7160 7161                          case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
7161 7162                                  (void) sprintf(&string[strlen(string)],
7162 7163                                      "target not responding, delaying "
7163 7164                                      "removal");
7164 7165                                  break;
7165 7166                          }
7166 7167                          NDBG20(("mptsas%d phy %d DevHandle %x, %s%s%s\n",
7167 7168                              mpt->m_instance, phy, dev_handle, string, curr,
7168 7169                              prev));
7169 7170                  }
7170 7171                  if (topo_head != NULL) {
7171 7172                          /*
7172 7173                           * Launch DR taskq to handle topology change
7173 7174                           */
7174 7175                          if ((ddi_taskq_dispatch(mpt->m_dr_taskq,
7175 7176                              mptsas_handle_dr, (void *)topo_head,
7176 7177                              DDI_NOSLEEP)) != DDI_SUCCESS) {
7177 7178                                  mptsas_log(mpt, CE_NOTE, "mptsas start taskq "
7178 7179                                      "for handle SAS DR event failed. \n");
7179 7180                          }
7180 7181                  }
7181 7182                  break;
7182 7183          }
7183 7184          case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7184 7185          {
7185 7186                  Mpi2EventDataIrConfigChangeList_t       *irChangeList;
7186 7187                  mptsas_topo_change_list_t               *topo_head = NULL;
7187 7188                  mptsas_topo_change_list_t               *topo_tail = NULL;
7188 7189                  mptsas_topo_change_list_t               *topo_node = NULL;
7189 7190                  mptsas_target_t                         *ptgt;
7190 7191                  mptsas_hash_table_t                     *tgttbl;
7191 7192                  uint8_t                                 num_entries, i, reason;
7192 7193                  uint16_t                                volhandle, diskhandle;
7193 7194  
7194 7195                  irChangeList = (pMpi2EventDataIrConfigChangeList_t)
7195 7196                      eventreply->EventData;
7196 7197                  num_entries = ddi_get8(mpt->m_acc_reply_frame_hdl,
7197 7198                      &irChangeList->NumElements);
7198 7199  
7199 7200                  tgttbl = &mpt->m_active->m_tgttbl;
7200 7201  
7201 7202                  NDBG20(("mptsas%d IR_CONFIGURATION_CHANGE_LIST event received",
7202 7203                      mpt->m_instance));
7203 7204  
7204 7205                  for (i = 0; i < num_entries; i++) {
7205 7206                          reason = ddi_get8(mpt->m_acc_reply_frame_hdl,
7206 7207                              &irChangeList->ConfigElement[i].ReasonCode);
7207 7208                          volhandle = ddi_get16(mpt->m_acc_reply_frame_hdl,
7208 7209                              &irChangeList->ConfigElement[i].VolDevHandle);
7209 7210                          diskhandle = ddi_get16(mpt->m_acc_reply_frame_hdl,
7210 7211                              &irChangeList->ConfigElement[i].PhysDiskDevHandle);
7211 7212  
7212 7213                          switch (reason) {
7213 7214                          case MPI2_EVENT_IR_CHANGE_RC_ADDED:
7214 7215                          case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
7215 7216                          {
7216 7217                                  NDBG20(("mptsas %d volume added\n",
7217 7218                                      mpt->m_instance));
7218 7219  
7219 7220                                  topo_node = kmem_zalloc(
7220 7221                                      sizeof (mptsas_topo_change_list_t),
7221 7222                                      KM_SLEEP);
7222 7223  
7223 7224                                  topo_node->mpt = mpt;
7224 7225                                  topo_node->event =
7225 7226                                      MPTSAS_DR_EVENT_RECONFIG_TARGET;
7226 7227                                  topo_node->un.physport = 0xff;
7227 7228                                  topo_node->devhdl = volhandle;
7228 7229                                  topo_node->flags =
7229 7230                                      MPTSAS_TOPO_FLAG_RAID_ASSOCIATED;
7230 7231                                  topo_node->object = NULL;
7231 7232                                  if (topo_head == NULL) {
7232 7233                                          topo_head = topo_tail = topo_node;
7233 7234                                  } else {
7234 7235                                          topo_tail->next = topo_node;
7235 7236                                          topo_tail = topo_node;
7236 7237                                  }
7237 7238                                  break;
7238 7239                          }
7239 7240                          case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
7240 7241                          case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
7241 7242                          {
7242 7243                                  NDBG20(("mptsas %d volume deleted\n",
7243 7244                                      mpt->m_instance));
7244 7245                                  ptgt = mptsas_search_by_devhdl(tgttbl,
7245 7246                                      volhandle);
7246 7247                                  if (ptgt == NULL)
7247 7248                                          break;
7248 7249  
7249 7250                                  /*
7250 7251                                   * Clear any flags related to volume
7251 7252                                   */
7252 7253                                  (void) mptsas_delete_volume(mpt, volhandle);
7253 7254  
7254 7255                                  /*
7255 7256                                   * Update DR flag immediately avoid I/O failure
7256 7257                                   */
7257 7258                                  mutex_enter(&ptgt->m_tgt_intr_mutex);
7258 7259                                  ptgt->m_dr_flag = MPTSAS_DR_INTRANSITION;
7259 7260                                  mutex_exit(&ptgt->m_tgt_intr_mutex);
7260 7261  
7261 7262                                  topo_node = kmem_zalloc(
7262 7263                                      sizeof (mptsas_topo_change_list_t),
7263 7264                                      KM_SLEEP);
7264 7265                                  topo_node->mpt = mpt;
7265 7266                                  topo_node->un.phymask = ptgt->m_phymask;
7266 7267                                  topo_node->event =
7267 7268                                      MPTSAS_DR_EVENT_OFFLINE_TARGET;
7268 7269                                  topo_node->devhdl = volhandle;
7269 7270                                  topo_node->flags =
7270 7271                                      MPTSAS_TOPO_FLAG_RAID_ASSOCIATED;
7271 7272                                  topo_node->object = (void *)ptgt;
7272 7273                                  if (topo_head == NULL) {
7273 7274                                          topo_head = topo_tail = topo_node;
7274 7275                                  } else {
7275 7276                                          topo_tail->next = topo_node;
7276 7277                                          topo_tail = topo_node;
7277 7278                                  }
7278 7279                                  break;
7279 7280                          }
7280 7281                          case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
7281 7282                          case MPI2_EVENT_IR_CHANGE_RC_HIDE:
7282 7283                          {
7283 7284                                  ptgt = mptsas_search_by_devhdl(tgttbl,
7284 7285                                      diskhandle);
7285 7286                                  if (ptgt == NULL)
7286 7287                                          break;
7287 7288  
7288 7289                                  /*
7289 7290                                   * Update DR flag immediately avoid I/O failure
7290 7291                                   */
7291 7292                                  mutex_enter(&ptgt->m_tgt_intr_mutex);
7292 7293                                  ptgt->m_dr_flag = MPTSAS_DR_INTRANSITION;
7293 7294                                  mutex_exit(&ptgt->m_tgt_intr_mutex);
7294 7295  
7295 7296                                  topo_node = kmem_zalloc(
7296 7297                                      sizeof (mptsas_topo_change_list_t),
7297 7298                                      KM_SLEEP);
7298 7299                                  topo_node->mpt = mpt;
7299 7300                                  topo_node->un.phymask = ptgt->m_phymask;
7300 7301                                  topo_node->event =
7301 7302                                      MPTSAS_DR_EVENT_OFFLINE_TARGET;
7302 7303                                  topo_node->devhdl = diskhandle;
7303 7304                                  topo_node->flags =
7304 7305                                      MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED;
7305 7306                                  topo_node->object = (void *)ptgt;
7306 7307                                  if (topo_head == NULL) {
7307 7308                                          topo_head = topo_tail = topo_node;
7308 7309                                  } else {
7309 7310                                          topo_tail->next = topo_node;
7310 7311                                          topo_tail = topo_node;
7311 7312                                  }
7312 7313                                  break;
7313 7314                          }
7314 7315                          case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
7315 7316                          case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
7316 7317                          {
7317 7318                                  /*
7318 7319                                   * The physical drive is released by a IR
7319 7320                                   * volume. But we cannot get the the physport
7320 7321                                   * or phynum from the event data, so we only
7321 7322                                   * can get the physport/phynum after SAS
7322 7323                                   * Device Page0 request for the devhdl.
7323 7324                                   */
7324 7325                                  topo_node = kmem_zalloc(
7325 7326                                      sizeof (mptsas_topo_change_list_t),
7326 7327                                      KM_SLEEP);
7327 7328                                  topo_node->mpt = mpt;
7328 7329                                  topo_node->un.phymask = 0;
7329 7330                                  topo_node->event =
7330 7331                                      MPTSAS_DR_EVENT_RECONFIG_TARGET;
7331 7332                                  topo_node->devhdl = diskhandle;
7332 7333                                  topo_node->flags =
7333 7334                                      MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED;
7334 7335                                  topo_node->object = NULL;
7335 7336                                  mpt->m_port_chng = 1;
7336 7337                                  if (topo_head == NULL) {
7337 7338                                          topo_head = topo_tail = topo_node;
7338 7339                                  } else {
7339 7340                                          topo_tail->next = topo_node;
7340 7341                                          topo_tail = topo_node;
7341 7342                                  }
7342 7343                                  break;
7343 7344                          }
7344 7345                          default:
7345 7346                                  break;
7346 7347                          }
7347 7348                  }
7348 7349  
7349 7350                  if (topo_head != NULL) {
7350 7351                          /*
7351 7352                           * Launch DR taskq to handle topology change
7352 7353                           */
7353 7354                          if ((ddi_taskq_dispatch(mpt->m_dr_taskq,
7354 7355                              mptsas_handle_dr, (void *)topo_head,
7355 7356                              DDI_NOSLEEP)) != DDI_SUCCESS) {
7356 7357                                  mptsas_log(mpt, CE_NOTE, "mptsas start taskq "
7357 7358                                      "for handle SAS DR event failed. \n");
7358 7359                          }
7359 7360                  }
7360 7361                  break;
7361 7362          }
7362 7363          default:
7363 7364                  return (DDI_FAILURE);
7364 7365          }
7365 7366  
7366 7367          return (DDI_SUCCESS);
7367 7368  }
7368 7369  
7369 7370  /*
7370 7371   * handle events from ioc
7371 7372   */
7372 7373  static void
7373 7374  mptsas_handle_event(void *args)
7374 7375  {
7375 7376          m_replyh_arg_t                  *replyh_arg;
7376 7377          pMpi2EventNotificationReply_t   eventreply;
7377 7378          uint32_t                        event, iocloginfo, rfm;
7378 7379          uint32_t                        status;
7379 7380          uint8_t                         port;
7380 7381          mptsas_t                        *mpt;
7381 7382          uint_t                          iocstatus;
7382 7383  
7383 7384          replyh_arg = (m_replyh_arg_t *)args;
7384 7385          rfm = replyh_arg->rfm;
7385 7386          mpt = replyh_arg->mpt;
7386 7387  
7387 7388          mutex_enter(&mpt->m_mutex);
7388 7389  
7389 7390          eventreply = (pMpi2EventNotificationReply_t)
7390 7391              (mpt->m_reply_frame + (rfm - mpt->m_reply_frame_dma_addr));
7391 7392          event = ddi_get16(mpt->m_acc_reply_frame_hdl, &eventreply->Event);
7392 7393  
7393 7394          if (iocstatus = ddi_get16(mpt->m_acc_reply_frame_hdl,
7394 7395              &eventreply->IOCStatus)) {
7395 7396                  if (iocstatus == MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
7396 7397                          mptsas_log(mpt, CE_WARN,
7397 7398                              "!mptsas_handle_event: IOCStatus=0x%x, "
7398 7399                              "IOCLogInfo=0x%x", iocstatus,
7399 7400                              ddi_get32(mpt->m_acc_reply_frame_hdl,
7400 7401                              &eventreply->IOCLogInfo));
7401 7402                  } else {
7402 7403                          mptsas_log(mpt, CE_WARN,
7403 7404                              "mptsas_handle_event: IOCStatus=0x%x, "
7404 7405                              "IOCLogInfo=0x%x", iocstatus,
7405 7406                              ddi_get32(mpt->m_acc_reply_frame_hdl,
7406 7407                              &eventreply->IOCLogInfo));
7407 7408                  }
7408 7409          }
7409 7410  
7410 7411          /*
7411 7412           * figure out what kind of event we got and handle accordingly
7412 7413           */
7413 7414          switch (event) {
7414 7415          case MPI2_EVENT_LOG_ENTRY_ADDED:
7415 7416                  break;
7416 7417          case MPI2_EVENT_LOG_DATA:
7417 7418                  iocloginfo = ddi_get32(mpt->m_acc_reply_frame_hdl,
7418 7419                      &eventreply->IOCLogInfo);
7419 7420                  NDBG20(("mptsas %d log info %x received.\n", mpt->m_instance,
7420 7421                      iocloginfo));
7421 7422                  break;
7422 7423          case MPI2_EVENT_STATE_CHANGE:
7423 7424                  NDBG20(("mptsas%d state change.", mpt->m_instance));
7424 7425                  break;
7425 7426          case MPI2_EVENT_HARD_RESET_RECEIVED:
7426 7427                  NDBG20(("mptsas%d event change.", mpt->m_instance));
7427 7428                  break;
7428 7429          case MPI2_EVENT_SAS_DISCOVERY:
7429 7430          {
7430 7431                  MPI2_EVENT_DATA_SAS_DISCOVERY   *sasdiscovery;
7431 7432                  char                            string[80];
7432 7433                  uint8_t                         rc;
7433 7434  
7434 7435                  sasdiscovery =
7435 7436                      (pMpi2EventDataSasDiscovery_t)eventreply->EventData;
7436 7437  
7437 7438                  rc = ddi_get8(mpt->m_acc_reply_frame_hdl,
7438 7439                      &sasdiscovery->ReasonCode);
7439 7440                  port = ddi_get8(mpt->m_acc_reply_frame_hdl,
7440 7441                      &sasdiscovery->PhysicalPort);
7441 7442                  status = ddi_get32(mpt->m_acc_reply_frame_hdl,
7442 7443                      &sasdiscovery->DiscoveryStatus);
7443 7444  
7444 7445                  string[0] = 0;
7445 7446                  switch (rc) {
7446 7447                  case MPI2_EVENT_SAS_DISC_RC_STARTED:
7447 7448                          (void) sprintf(string, "STARTING");
7448 7449                          break;
7449 7450                  case MPI2_EVENT_SAS_DISC_RC_COMPLETED:
7450 7451                          (void) sprintf(string, "COMPLETED");
7451 7452                          break;
7452 7453                  default:
7453 7454                          (void) sprintf(string, "UNKNOWN");
7454 7455                          break;
7455 7456                  }
7456 7457  
7457 7458                  NDBG20(("SAS DISCOVERY is %s for port %d, status %x", string,
7458 7459                      port, status));
7459 7460  
7460 7461                  break;
7461 7462          }
7462 7463          case MPI2_EVENT_EVENT_CHANGE:
7463 7464                  NDBG20(("mptsas%d event change.", mpt->m_instance));
7464 7465                  break;
7465 7466          case MPI2_EVENT_TASK_SET_FULL:
7466 7467          {
7467 7468                  pMpi2EventDataTaskSetFull_t     taskfull;
7468 7469  
7469 7470                  taskfull = (pMpi2EventDataTaskSetFull_t)eventreply->EventData;
7470 7471  
7471 7472                  NDBG20(("TASK_SET_FULL received for mptsas%d, depth %d\n",
7472 7473                      mpt->m_instance,  ddi_get16(mpt->m_acc_reply_frame_hdl,
7473 7474                      &taskfull->CurrentDepth)));
7474 7475                  break;
7475 7476          }
7476 7477          case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7477 7478          {
7478 7479                  /*
7479 7480                   * SAS TOPOLOGY CHANGE LIST Event has already been handled
7480 7481                   * in mptsas_handle_event_sync() of interrupt context
7481 7482                   */
7482 7483                  break;
7483 7484          }
7484 7485          case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7485 7486          {
7486 7487                  pMpi2EventDataSasEnclDevStatusChange_t  encstatus;
7487 7488                  uint8_t                                 rc;
7488 7489                  char                                    string[80];
7489 7490  
7490 7491                  encstatus = (pMpi2EventDataSasEnclDevStatusChange_t)
7491 7492                      eventreply->EventData;
7492 7493  
7493 7494                  rc = ddi_get8(mpt->m_acc_reply_frame_hdl,
7494 7495                      &encstatus->ReasonCode);
7495 7496                  switch (rc) {
7496 7497                  case MPI2_EVENT_SAS_ENCL_RC_ADDED:
7497 7498                          (void) sprintf(string, "added");
7498 7499                          break;
7499 7500                  case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
7500 7501                          (void) sprintf(string, ", not responding");
7501 7502                          break;
7502 7503                  default:
7503 7504                  break;
7504 7505                  }
7505 7506                  NDBG20(("mptsas%d ENCLOSURE STATUS CHANGE for enclosure %x%s\n",
7506 7507                      mpt->m_instance, ddi_get16(mpt->m_acc_reply_frame_hdl,
7507 7508                      &encstatus->EnclosureHandle), string));
7508 7509                  break;
7509 7510          }
7510 7511  
7511 7512          /*
7512 7513           * MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE is handled by
7513 7514           * mptsas_handle_event_sync,in here just send ack message.
7514 7515           */
7515 7516          case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7516 7517          {
7517 7518                  pMpi2EventDataSasDeviceStatusChange_t   statuschange;
7518 7519                  uint8_t                                 rc;
7519 7520                  uint16_t                                devhdl;
7520 7521                  uint64_t                                wwn = 0;
7521 7522                  uint32_t                                wwn_lo, wwn_hi;
7522 7523  
7523 7524                  statuschange = (pMpi2EventDataSasDeviceStatusChange_t)
7524 7525                      eventreply->EventData;
7525 7526                  rc = ddi_get8(mpt->m_acc_reply_frame_hdl,
7526 7527                      &statuschange->ReasonCode);
7527 7528                  wwn_lo = ddi_get32(mpt->m_acc_reply_frame_hdl,
7528 7529                      (uint32_t *)(void *)&statuschange->SASAddress);
7529 7530                  wwn_hi = ddi_get32(mpt->m_acc_reply_frame_hdl,
7530 7531                      (uint32_t *)(void *)&statuschange->SASAddress + 1);
7531 7532                  wwn = ((uint64_t)wwn_hi << 32) | wwn_lo;
7532 7533                  devhdl =  ddi_get16(mpt->m_acc_reply_frame_hdl,
7533 7534                      &statuschange->DevHandle);
7534 7535  
7535 7536                  NDBG13(("MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE wwn is %"PRIx64,
7536 7537                      wwn));
7537 7538  
7538 7539                  switch (rc) {
7539 7540                  case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
7540 7541                          NDBG20(("SMART data received, ASC/ASCQ = %02x/%02x",
7541 7542                              ddi_get8(mpt->m_acc_reply_frame_hdl,
7542 7543                              &statuschange->ASC),
7543 7544                              ddi_get8(mpt->m_acc_reply_frame_hdl,
7544 7545                              &statuschange->ASCQ)));
7545 7546                          break;
7546 7547  
7547 7548                  case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
7548 7549                          NDBG20(("Device not supported"));
7549 7550                          break;
7550 7551  
7551 7552                  case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
7552 7553                          NDBG20(("IOC internally generated the Target Reset "
7553 7554                              "for devhdl:%x", devhdl));
7554 7555                          break;
7555 7556  
7556 7557                  case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
7557 7558                          NDBG20(("IOC's internally generated Target Reset "
7558 7559                              "completed for devhdl:%x", devhdl));
7559 7560                          break;
7560 7561  
7561 7562                  case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
7562 7563                          NDBG20(("IOC internally generated Abort Task"));
7563 7564                          break;
7564 7565  
7565 7566                  case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
7566 7567                          NDBG20(("IOC's internally generated Abort Task "
7567 7568                              "completed"));
7568 7569                          break;
7569 7570  
7570 7571                  case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
7571 7572                          NDBG20(("IOC internally generated Abort Task Set"));
7572 7573                          break;
7573 7574  
7574 7575                  case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
7575 7576                          NDBG20(("IOC internally generated Clear Task Set"));
7576 7577                          break;
7577 7578  
7578 7579                  case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
7579 7580                          NDBG20(("IOC internally generated Query Task"));
7580 7581                          break;
7581 7582  
7582 7583                  case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
7583 7584                          NDBG20(("Device sent an Asynchronous Notification"));
7584 7585                          break;
7585 7586  
7586 7587                  default:
7587 7588                          break;
7588 7589                  }
7589 7590                  break;
7590 7591          }
7591 7592          case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7592 7593          {
7593 7594                  /*
7594 7595                   * IR TOPOLOGY CHANGE LIST Event has already been handled
7595 7596                   * in mpt_handle_event_sync() of interrupt context
7596 7597                   */
7597 7598                  break;
7598 7599          }
7599 7600          case MPI2_EVENT_IR_OPERATION_STATUS:
7600 7601          {
7601 7602                  Mpi2EventDataIrOperationStatus_t        *irOpStatus;
7602 7603                  char                                    reason_str[80];
7603 7604                  uint8_t                                 rc, percent;
7604 7605                  uint16_t                                handle;
7605 7606  
7606 7607                  irOpStatus = (pMpi2EventDataIrOperationStatus_t)
7607 7608                      eventreply->EventData;
7608 7609                  rc = ddi_get8(mpt->m_acc_reply_frame_hdl,
7609 7610                      &irOpStatus->RAIDOperation);
7610 7611                  percent = ddi_get8(mpt->m_acc_reply_frame_hdl,
7611 7612                      &irOpStatus->PercentComplete);
7612 7613                  handle = ddi_get16(mpt->m_acc_reply_frame_hdl,
7613 7614                      &irOpStatus->VolDevHandle);
7614 7615  
7615 7616                  switch (rc) {
7616 7617                          case MPI2_EVENT_IR_RAIDOP_RESYNC:
7617 7618                                  (void) sprintf(reason_str, "resync");
7618 7619                                  break;
7619 7620                          case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
7620 7621                                  (void) sprintf(reason_str, "online capacity "
7621 7622                                      "expansion");
7622 7623                                  break;
7623 7624                          case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
7624 7625                                  (void) sprintf(reason_str, "consistency check");
7625 7626                                  break;
7626 7627                          default:
7627 7628                                  (void) sprintf(reason_str, "unknown reason %x",
7628 7629                                      rc);
7629 7630                  }
7630 7631  
7631 7632                  NDBG20(("mptsas%d raid operational status: (%s)"
7632 7633                      "\thandle(0x%04x), percent complete(%d)\n",
7633 7634                      mpt->m_instance, reason_str, handle, percent));
7634 7635                  break;
7635 7636          }
7636 7637          case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7637 7638          {
7638 7639                  pMpi2EventDataSasBroadcastPrimitive_t   sas_broadcast;
7639 7640                  uint8_t                                 phy_num;
7640 7641                  uint8_t                                 primitive;
7641 7642  
7642 7643                  sas_broadcast = (pMpi2EventDataSasBroadcastPrimitive_t)
7643 7644                      eventreply->EventData;
7644 7645  
7645 7646                  phy_num = ddi_get8(mpt->m_acc_reply_frame_hdl,
7646 7647                      &sas_broadcast->PhyNum);
7647 7648                  primitive = ddi_get8(mpt->m_acc_reply_frame_hdl,
7648 7649                      &sas_broadcast->Primitive);
7649 7650  
7650 7651                  switch (primitive) {
7651 7652                  case MPI2_EVENT_PRIMITIVE_CHANGE:
7652 7653                          mptsas_smhba_log_sysevent(mpt,
7653 7654                              ESC_SAS_HBA_PORT_BROADCAST,
7654 7655                              SAS_PORT_BROADCAST_CHANGE,
7655 7656                              &mpt->m_phy_info[phy_num].smhba_info);
7656 7657                          break;
7657 7658                  case MPI2_EVENT_PRIMITIVE_SES:
7658 7659                          mptsas_smhba_log_sysevent(mpt,
7659 7660                              ESC_SAS_HBA_PORT_BROADCAST,
7660 7661                              SAS_PORT_BROADCAST_SES,
7661 7662                              &mpt->m_phy_info[phy_num].smhba_info);
7662 7663                          break;
7663 7664                  case MPI2_EVENT_PRIMITIVE_EXPANDER:
7664 7665                          mptsas_smhba_log_sysevent(mpt,
7665 7666                              ESC_SAS_HBA_PORT_BROADCAST,
7666 7667                              SAS_PORT_BROADCAST_D01_4,
7667 7668                              &mpt->m_phy_info[phy_num].smhba_info);
7668 7669                          break;
7669 7670                  case MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT:
7670 7671                          mptsas_smhba_log_sysevent(mpt,
7671 7672                              ESC_SAS_HBA_PORT_BROADCAST,
7672 7673                              SAS_PORT_BROADCAST_D04_7,
7673 7674                              &mpt->m_phy_info[phy_num].smhba_info);
7674 7675                          break;
7675 7676                  case MPI2_EVENT_PRIMITIVE_RESERVED3:
7676 7677                          mptsas_smhba_log_sysevent(mpt,
7677 7678                              ESC_SAS_HBA_PORT_BROADCAST,
7678 7679                              SAS_PORT_BROADCAST_D16_7,
7679 7680                              &mpt->m_phy_info[phy_num].smhba_info);
7680 7681                          break;
7681 7682                  case MPI2_EVENT_PRIMITIVE_RESERVED4:
7682 7683                          mptsas_smhba_log_sysevent(mpt,
7683 7684                              ESC_SAS_HBA_PORT_BROADCAST,
7684 7685                              SAS_PORT_BROADCAST_D29_7,
7685 7686                              &mpt->m_phy_info[phy_num].smhba_info);
7686 7687                          break;
7687 7688                  case MPI2_EVENT_PRIMITIVE_CHANGE0_RESERVED:
7688 7689                          mptsas_smhba_log_sysevent(mpt,
7689 7690                              ESC_SAS_HBA_PORT_BROADCAST,
7690 7691                              SAS_PORT_BROADCAST_D24_0,
7691 7692                              &mpt->m_phy_info[phy_num].smhba_info);
7692 7693                          break;
7693 7694                  case MPI2_EVENT_PRIMITIVE_CHANGE1_RESERVED:
7694 7695                          mptsas_smhba_log_sysevent(mpt,
7695 7696                              ESC_SAS_HBA_PORT_BROADCAST,
7696 7697                              SAS_PORT_BROADCAST_D27_4,
7697 7698                              &mpt->m_phy_info[phy_num].smhba_info);
7698 7699                          break;
7699 7700                  default:
7700 7701                          NDBG20(("mptsas%d: unknown BROADCAST PRIMITIVE"
7701 7702                              " %x received",
7702 7703                              mpt->m_instance, primitive));
7703 7704                          break;
7704 7705                  }
7705 7706                  NDBG20(("mptsas%d sas broadcast primitive: "
7706 7707                      "\tprimitive(0x%04x), phy(%d) complete\n",
7707 7708                      mpt->m_instance, primitive, phy_num));
7708 7709                  break;
7709 7710          }
7710 7711          case MPI2_EVENT_IR_VOLUME:
7711 7712          {
7712 7713                  Mpi2EventDataIrVolume_t         *irVolume;
7713 7714                  uint16_t                        devhandle;
7714 7715                  uint32_t                        state;
7715 7716                  int                             config, vol;
7716 7717                  mptsas_slots_t                  *slots = mpt->m_active;
7717 7718                  uint8_t                         found = FALSE;
7718 7719  
7719 7720                  irVolume = (pMpi2EventDataIrVolume_t)eventreply->EventData;
7720 7721                  state = ddi_get32(mpt->m_acc_reply_frame_hdl,
7721 7722                      &irVolume->NewValue);
7722 7723                  devhandle = ddi_get16(mpt->m_acc_reply_frame_hdl,
7723 7724                      &irVolume->VolDevHandle);
7724 7725  
7725 7726                  NDBG20(("EVENT_IR_VOLUME event is received"));
7726 7727  
7727 7728                  /*
7728 7729                   * Get latest RAID info and then find the DevHandle for this
7729 7730                   * event in the configuration.  If the DevHandle is not found
7730 7731                   * just exit the event.
7731 7732                   */
7732 7733                  (void) mptsas_get_raid_info(mpt);
7733 7734                  for (config = 0; (config < slots->m_num_raid_configs) &&
7734 7735                      (!found); config++) {
7735 7736                          for (vol = 0; vol < MPTSAS_MAX_RAIDVOLS; vol++) {
7736 7737                                  if (slots->m_raidconfig[config].m_raidvol[vol].
7737 7738                                      m_raidhandle == devhandle) {
7738 7739                                          found = TRUE;
7739 7740                                          break;
7740 7741                                  }
7741 7742                          }
7742 7743                  }
7743 7744                  if (!found) {
7744 7745                          break;
7745 7746                  }
7746 7747  
7747 7748                  switch (irVolume->ReasonCode) {
7748 7749                  case MPI2_EVENT_IR_VOLUME_RC_SETTINGS_CHANGED:
7749 7750                  {
7750 7751                          uint32_t i;
7751 7752                          slots->m_raidconfig[config].m_raidvol[vol].m_settings =
7752 7753                              state;
7753 7754  
7754 7755                          i = state & MPI2_RAIDVOL0_SETTING_MASK_WRITE_CACHING;
7755 7756                          mptsas_log(mpt, CE_NOTE, " Volume %d settings changed"
7756 7757                              ", auto-config of hot-swap drives is %s"
7757 7758                              ", write caching is %s"
7758 7759                              ", hot-spare pool mask is %02x\n",
7759 7760                              vol, state &
7760 7761                              MPI2_RAIDVOL0_SETTING_AUTO_CONFIG_HSWAP_DISABLE
7761 7762                              ? "disabled" : "enabled",
7762 7763                              i == MPI2_RAIDVOL0_SETTING_UNCHANGED
7763 7764                              ? "controlled by member disks" :
7764 7765                              i == MPI2_RAIDVOL0_SETTING_DISABLE_WRITE_CACHING
7765 7766                              ? "disabled" :
7766 7767                              i == MPI2_RAIDVOL0_SETTING_ENABLE_WRITE_CACHING
7767 7768                              ? "enabled" :
7768 7769                              "incorrectly set",
7769 7770                              (state >> 16) & 0xff);
7770 7771                                  break;
7771 7772                  }
7772 7773                  case MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED:
7773 7774                  {
7774 7775                          slots->m_raidconfig[config].m_raidvol[vol].m_state =
7775 7776                              (uint8_t)state;
7776 7777  
7777 7778                          mptsas_log(mpt, CE_NOTE,
7778 7779                              "Volume %d is now %s\n", vol,
7779 7780                              state == MPI2_RAID_VOL_STATE_OPTIMAL
7780 7781                              ? "optimal" :
7781 7782                              state == MPI2_RAID_VOL_STATE_DEGRADED
7782 7783                              ? "degraded" :
7783 7784                              state == MPI2_RAID_VOL_STATE_ONLINE
7784 7785                              ? "online" :
7785 7786                              state == MPI2_RAID_VOL_STATE_INITIALIZING
7786 7787                              ? "initializing" :
7787 7788                              state == MPI2_RAID_VOL_STATE_FAILED
7788 7789                              ? "failed" :
7789 7790                              state == MPI2_RAID_VOL_STATE_MISSING
7790 7791                              ? "missing" :
7791 7792                              "state unknown");
7792 7793                          break;
7793 7794                  }
7794 7795                  case MPI2_EVENT_IR_VOLUME_RC_STATUS_FLAGS_CHANGED:
7795 7796                  {
7796 7797                          slots->m_raidconfig[config].m_raidvol[vol].
7797 7798                              m_statusflags = state;
7798 7799  
7799 7800                          mptsas_log(mpt, CE_NOTE,
7800 7801                              " Volume %d is now %s%s%s%s%s%s%s%s%s\n",
7801 7802                              vol,
7802 7803                              state & MPI2_RAIDVOL0_STATUS_FLAG_ENABLED
7803 7804                              ? ", enabled" : ", disabled",
7804 7805                              state & MPI2_RAIDVOL0_STATUS_FLAG_QUIESCED
7805 7806                              ? ", quiesced" : "",
7806 7807                              state & MPI2_RAIDVOL0_STATUS_FLAG_VOLUME_INACTIVE
7807 7808                              ? ", inactive" : ", active",
7808 7809                              state &
7809 7810                              MPI2_RAIDVOL0_STATUS_FLAG_BAD_BLOCK_TABLE_FULL
7810 7811                              ? ", bad block table is full" : "",
7811 7812                              state &
7812 7813                              MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS
7813 7814                              ? ", resync in progress" : "",
7814 7815                              state & MPI2_RAIDVOL0_STATUS_FLAG_BACKGROUND_INIT
7815 7816                              ? ", background initialization in progress" : "",
7816 7817                              state &
7817 7818                              MPI2_RAIDVOL0_STATUS_FLAG_CAPACITY_EXPANSION
7818 7819                              ? ", capacity expansion in progress" : "",
7819 7820                              state &
7820 7821                              MPI2_RAIDVOL0_STATUS_FLAG_CONSISTENCY_CHECK
7821 7822                              ? ", consistency check in progress" : "",
7822 7823                              state & MPI2_RAIDVOL0_STATUS_FLAG_DATA_SCRUB
7823 7824                              ? ", data scrub in progress" : "");
7824 7825                          break;
7825 7826                  }
7826 7827                  default:
7827 7828                          break;
7828 7829                  }
7829 7830                  break;
7830 7831          }
7831 7832          case MPI2_EVENT_IR_PHYSICAL_DISK:
7832 7833          {
7833 7834                  Mpi2EventDataIrPhysicalDisk_t   *irPhysDisk;
7834 7835                  uint16_t                        devhandle, enchandle, slot;
7835 7836                  uint32_t                        status, state;
7836 7837                  uint8_t                         physdisknum, reason;
7837 7838  
7838 7839                  irPhysDisk = (Mpi2EventDataIrPhysicalDisk_t *)
7839 7840                      eventreply->EventData;
7840 7841                  physdisknum = ddi_get8(mpt->m_acc_reply_frame_hdl,
7841 7842                      &irPhysDisk->PhysDiskNum);
7842 7843                  devhandle = ddi_get16(mpt->m_acc_reply_frame_hdl,
7843 7844                      &irPhysDisk->PhysDiskDevHandle);
7844 7845                  enchandle = ddi_get16(mpt->m_acc_reply_frame_hdl,
7845 7846                      &irPhysDisk->EnclosureHandle);
7846 7847                  slot = ddi_get16(mpt->m_acc_reply_frame_hdl,
7847 7848                      &irPhysDisk->Slot);
7848 7849                  state = ddi_get32(mpt->m_acc_reply_frame_hdl,
7849 7850                      &irPhysDisk->NewValue);
7850 7851                  reason = ddi_get8(mpt->m_acc_reply_frame_hdl,
7851 7852                      &irPhysDisk->ReasonCode);
7852 7853  
7853 7854                  NDBG20(("EVENT_IR_PHYSICAL_DISK event is received"));
7854 7855  
7855 7856                  switch (reason) {
7856 7857                  case MPI2_EVENT_IR_PHYSDISK_RC_SETTINGS_CHANGED:
7857 7858                          mptsas_log(mpt, CE_NOTE,
7858 7859                              " PhysDiskNum %d with DevHandle 0x%x in slot %d "
7859 7860                              "for enclosure with handle 0x%x is now in hot "
7860 7861                              "spare pool %d",
7861 7862                              physdisknum, devhandle, slot, enchandle,
7862 7863                              (state >> 16) & 0xff);
7863 7864                          break;
7864 7865  
7865 7866                  case MPI2_EVENT_IR_PHYSDISK_RC_STATUS_FLAGS_CHANGED:
7866 7867                          status = state;
7867 7868                          mptsas_log(mpt, CE_NOTE,
7868 7869                              " PhysDiskNum %d with DevHandle 0x%x in slot %d "
7869 7870                              "for enclosure with handle 0x%x is now "
7870 7871                              "%s%s%s%s%s\n", physdisknum, devhandle, slot,
7871 7872                              enchandle,
7872 7873                              status & MPI2_PHYSDISK0_STATUS_FLAG_INACTIVE_VOLUME
7873 7874                              ? ", inactive" : ", active",
7874 7875                              status & MPI2_PHYSDISK0_STATUS_FLAG_OUT_OF_SYNC
7875 7876                              ? ", out of sync" : "",
7876 7877                              status & MPI2_PHYSDISK0_STATUS_FLAG_QUIESCED
7877 7878                              ? ", quiesced" : "",
7878 7879                              status &
7879 7880                              MPI2_PHYSDISK0_STATUS_FLAG_WRITE_CACHE_ENABLED
7880 7881                              ? ", write cache enabled" : "",
7881 7882                              status & MPI2_PHYSDISK0_STATUS_FLAG_OCE_TARGET
7882 7883                              ? ", capacity expansion target" : "");
7883 7884                          break;
7884 7885  
7885 7886                  case MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED:
7886 7887                          mptsas_log(mpt, CE_NOTE,
7887 7888                              " PhysDiskNum %d with DevHandle 0x%x in slot %d "
7888 7889                              "for enclosure with handle 0x%x is now %s\n",
7889 7890                              physdisknum, devhandle, slot, enchandle,
7890 7891                              state == MPI2_RAID_PD_STATE_OPTIMAL
7891 7892                              ? "optimal" :
7892 7893                              state == MPI2_RAID_PD_STATE_REBUILDING
7893 7894                              ? "rebuilding" :
7894 7895                              state == MPI2_RAID_PD_STATE_DEGRADED
7895 7896                              ? "degraded" :
7896 7897                              state == MPI2_RAID_PD_STATE_HOT_SPARE
7897 7898                              ? "a hot spare" :
7898 7899                              state == MPI2_RAID_PD_STATE_ONLINE
7899 7900                              ? "online" :
7900 7901                              state == MPI2_RAID_PD_STATE_OFFLINE
7901 7902                              ? "offline" :
7902 7903                              state == MPI2_RAID_PD_STATE_NOT_COMPATIBLE
7903 7904                              ? "not compatible" :
7904 7905                              state == MPI2_RAID_PD_STATE_NOT_CONFIGURED
7905 7906                              ? "not configured" :
7906 7907                              "state unknown");
7907 7908                          break;
7908 7909                  }
7909 7910                  break;
7910 7911          }
7911 7912          default:
7912 7913                  NDBG20(("mptsas%d: unknown event %x received",
7913 7914                      mpt->m_instance, event));
7914 7915                  break;
7915 7916          }
7916 7917  
7917 7918          /*
7918 7919           * Return the reply frame to the free queue.
7919 7920           */
7920 7921          ddi_put32(mpt->m_acc_free_queue_hdl,
7921 7922              &((uint32_t *)(void *)mpt->m_free_queue)[mpt->m_free_index], rfm);
7922 7923          (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
7923 7924              DDI_DMA_SYNC_FORDEV);
7924 7925          if (++mpt->m_free_index == mpt->m_free_queue_depth) {
7925 7926                  mpt->m_free_index = 0;
7926 7927          }
7927 7928          ddi_put32(mpt->m_datap, &mpt->m_reg->ReplyFreeHostIndex,
7928 7929              mpt->m_free_index);
7929 7930          mutex_exit(&mpt->m_mutex);
7930 7931  }
7931 7932  
7932 7933  /*
7933 7934   * invoked from timeout() to restart qfull cmds with throttle == 0
7934 7935   */
7935 7936  static void
7936 7937  mptsas_restart_cmd(void *arg)
7937 7938  {
7938 7939          mptsas_t        *mpt = arg;
7939 7940          mptsas_target_t *ptgt = NULL;
7940 7941  
7941 7942          mutex_enter(&mpt->m_mutex);
7942 7943  
7943 7944          mpt->m_restart_cmd_timeid = 0;
7944 7945  
7945 7946          ptgt = (mptsas_target_t *)mptsas_hash_traverse(&mpt->m_active->m_tgttbl,
7946 7947              MPTSAS_HASH_FIRST);
7947 7948          while (ptgt != NULL) {
7948 7949                  mutex_enter(&ptgt->m_tgt_intr_mutex);
7949 7950                  if (ptgt->m_reset_delay == 0) {
7950 7951                          if (ptgt->m_t_throttle == QFULL_THROTTLE) {
7951 7952                                  mptsas_set_throttle(mpt, ptgt,
7952 7953                                      MAX_THROTTLE);
7953 7954                          }
7954 7955                  }
7955 7956                  mutex_exit(&ptgt->m_tgt_intr_mutex);
7956 7957  
7957 7958                  ptgt = (mptsas_target_t *)mptsas_hash_traverse(
7958 7959                      &mpt->m_active->m_tgttbl, MPTSAS_HASH_NEXT);
7959 7960          }
7960 7961          mptsas_restart_hba(mpt);
7961 7962          mutex_exit(&mpt->m_mutex);
7962 7963  }
7963 7964  
7964 7965  /*
7965 7966   * mptsas_remove_cmd0 is similar to mptsas_remove_cmd except that it is called
7966 7967   * where m_intr_mutex has already been held.
7967 7968   */
7968 7969  void
7969 7970  mptsas_remove_cmd(mptsas_t *mpt, mptsas_cmd_t *cmd)
7970 7971  {
7971 7972          ASSERT(mutex_owned(&mpt->m_mutex));
7972 7973  
7973 7974          /*
7974 7975           * With new fine-grained lock mechanism, the outstanding cmd is only
7975 7976           * linked to m_active before the dma is triggerred(MPTSAS_START_CMD)
7976 7977           * to send it. that is, mptsas_save_cmd() doesn't link the outstanding
7977 7978           * cmd now. So when mptsas_remove_cmd is called, a mptsas_save_cmd must
7978 7979           * have been called, but the cmd may have not been linked.
7979 7980           * For mptsas_remove_cmd0, the cmd must have been linked.
7980 7981           * In order to keep the same semantic, we link the cmd to the
7981 7982           * outstanding cmd list.
7982 7983           */
7983 7984          mpt->m_active->m_slot[cmd->cmd_slot] = cmd;
7984 7985  
7985 7986          mutex_enter(&mpt->m_intr_mutex);
7986 7987          mptsas_remove_cmd0(mpt, cmd);
7987 7988          mutex_exit(&mpt->m_intr_mutex);
7988 7989  }
7989 7990  
7990 7991  static inline void
7991 7992  mptsas_remove_cmd0(mptsas_t *mpt, mptsas_cmd_t *cmd)
7992 7993  {
7993 7994          int             slot;
7994 7995          mptsas_slots_t  *slots = mpt->m_active;
7995 7996          int             t;
7996 7997          mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
7997 7998          mptsas_slot_free_e_t    *pe;
7998 7999  
7999 8000          ASSERT(cmd != NULL);
8000 8001          ASSERT(cmd->cmd_queued == FALSE);
8001 8002  
8002 8003          /*
8003 8004           * Task Management cmds are removed in their own routines.  Also,
8004 8005           * we don't want to modify timeout based on TM cmds.
8005 8006           */
8006 8007          if (cmd->cmd_flags & CFLAG_TM_CMD) {
8007 8008                  return;
8008 8009          }
8009 8010  
8010 8011          t = Tgt(cmd);
8011 8012          slot = cmd->cmd_slot;
8012 8013          pe = mpt->m_slot_free_ae + slot - 1;
8013 8014          ASSERT(cmd == slots->m_slot[slot]);
8014 8015          ASSERT((slot > 0) && slot < (mpt->m_max_requests - 1));
8015 8016  
8016 8017          /*
8017 8018           * remove the cmd.
8018 8019           */
8019 8020          mutex_enter(&mpt->m_slot_freeq_pairp[pe->cpuid].
8020 8021              m_slot_releq.s.m_fq_mutex);
8021 8022          NDBG31(("mptsas_remove_cmd0: removing cmd=0x%p", (void *)cmd));
8022 8023          slots->m_slot[slot] = NULL;
8023 8024          ASSERT(pe->slot == slot);
8024 8025          list_insert_tail(&mpt->m_slot_freeq_pairp[pe->cpuid].
8025 8026              m_slot_releq.s.m_fq_list, pe);
8026 8027          mpt->m_slot_freeq_pairp[pe->cpuid].m_slot_releq.s.m_fq_n++;
8027 8028          ASSERT(mpt->m_slot_freeq_pairp[pe->cpuid].
8028 8029              m_slot_releq.s.m_fq_n <= mpt->m_max_requests - 2);
8029 8030          mutex_exit(&mpt->m_slot_freeq_pairp[pe->cpuid].
8030 8031              m_slot_releq.s.m_fq_mutex);
8031 8032  
8032 8033          /*
8033 8034           * only decrement per target ncmds if command
8034 8035           * has a target associated with it.
8035 8036           */
8036 8037          if ((cmd->cmd_flags & CFLAG_CMDIOC) == 0) {
8037 8038                  mutex_enter(&ptgt->m_tgt_intr_mutex);
8038 8039                  ptgt->m_t_ncmds--;
8039 8040                  /*
8040 8041                   * reset throttle if we just ran an untagged command
8041 8042                   * to a tagged target
8042 8043                   */
8043 8044                  if ((ptgt->m_t_ncmds == 0) &&
8044 8045                      ((cmd->cmd_pkt_flags & FLAG_TAGMASK) == 0)) {
8045 8046                          mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
8046 8047                  }
8047 8048                  mutex_exit(&ptgt->m_tgt_intr_mutex);
8048 8049          }
8049 8050  
8050 8051          /*
8051 8052           * This is all we need to do for ioc commands.
8052 8053           * The ioc cmds would never be handled in fastpath in ISR, so we make
8053 8054           * sure the mptsas_return_to_pool() would always be called with
8054 8055           * m_mutex protected.
8055 8056           */
8056 8057          if (cmd->cmd_flags & CFLAG_CMDIOC) {
8057 8058                  ASSERT(mutex_owned(&mpt->m_mutex));
8058 8059                  mptsas_return_to_pool(mpt, cmd);
8059 8060                  return;
8060 8061          }
8061 8062  
8062 8063          /*
8063 8064           * Figure out what to set tag Q timeout for...
8064 8065           *
8065 8066           * Optimize: If we have duplicate's of same timeout
8066 8067           * we're using, then we'll use it again until we run
8067 8068           * out of duplicates.  This should be the normal case
8068 8069           * for block and raw I/O.
8069 8070           * If no duplicates, we have to scan through tag que and
8070 8071           * find the longest timeout value and use it.  This is
8071 8072           * going to take a while...
8072 8073           * Add 1 to m_n_slots to account for TM request.
8073 8074           */
8074 8075          mutex_enter(&ptgt->m_tgt_intr_mutex);
8075 8076          if (cmd->cmd_pkt->pkt_time == ptgt->m_timebase) {
8076 8077                  if (--(ptgt->m_dups) == 0) {
8077 8078                          if (ptgt->m_t_ncmds) {
8078 8079                                  mptsas_cmd_t *ssp;
8079 8080                                  uint_t n = 0;
8080 8081                                  ushort_t nslots = (slots->m_n_slots + 1);
8081 8082                                  ushort_t i;
8082 8083                                  /*
8083 8084                                   * This crude check assumes we don't do
8084 8085                                   * this too often which seems reasonable
8085 8086                                   * for block and raw I/O.
8086 8087                                   */
8087 8088                                  for (i = 0; i < nslots; i++) {
8088 8089                                          ssp = slots->m_slot[i];
8089 8090                                          if (ssp && (Tgt(ssp) == t) &&
8090 8091                                              (ssp->cmd_pkt->pkt_time > n)) {
8091 8092                                                  n = ssp->cmd_pkt->pkt_time;
8092 8093                                                  ptgt->m_dups = 1;
8093 8094                                          } else if (ssp && (Tgt(ssp) == t) &&
8094 8095                                              (ssp->cmd_pkt->pkt_time == n)) {
8095 8096                                                  ptgt->m_dups++;
8096 8097                                          }
8097 8098                                  }
8098 8099                                  ptgt->m_timebase = n;
8099 8100                          } else {
8100 8101                                  ptgt->m_dups = 0;
8101 8102                                  ptgt->m_timebase = 0;
8102 8103                          }
8103 8104                  }
8104 8105          }
8105 8106          ptgt->m_timeout = ptgt->m_timebase;
8106 8107  
8107 8108          ASSERT(cmd != slots->m_slot[cmd->cmd_slot]);
8108 8109          mutex_exit(&ptgt->m_tgt_intr_mutex);
8109 8110  }
8110 8111  
8111 8112  /*
8112 8113   * start a fresh request from the top of the device queue.
8113 8114   */
8114 8115  static void
8115 8116  mptsas_restart_hba(mptsas_t *mpt)
8116 8117  {
8117 8118          mptsas_cmd_t    *cmd, *next_cmd;
8118 8119          mptsas_target_t *ptgt = NULL;
8119 8120  
8120 8121          NDBG1(("mptsas_restart_hba: mpt=0x%p", (void *)mpt));
8121 8122  
8122 8123          ASSERT(mutex_owned(&mpt->m_mutex));
8123 8124  
8124 8125          /*
8125 8126           * If there is a reset delay, don't start any cmds.  Otherwise, start
8126 8127           * as many cmds as possible.
8127 8128           * Since SMID 0 is reserved and the TM slot is reserved, the actual max
8128 8129           * commands is m_max_requests - 2.
8129 8130           */
8130 8131          cmd = mpt->m_waitq;
8131 8132  
8132 8133          while (cmd != NULL) {
8133 8134                  next_cmd = cmd->cmd_linkp;
8134 8135                  if (cmd->cmd_flags & CFLAG_PASSTHRU) {
8135 8136                          if (mptsas_save_cmd(mpt, cmd) == TRUE) {
8136 8137                                  /*
8137 8138                                   * passthru command get slot need
8138 8139                                   * set CFLAG_PREPARED.
8139 8140                                   */
8140 8141                                  cmd->cmd_flags |= CFLAG_PREPARED;
8141 8142                                  mptsas_waitq_delete(mpt, cmd);
8142 8143                                  mptsas_start_passthru(mpt, cmd);
8143 8144                          }
8144 8145                          cmd = next_cmd;
8145 8146                          continue;
8146 8147                  }
8147 8148                  if (cmd->cmd_flags & CFLAG_CONFIG) {
8148 8149                          if (mptsas_save_cmd(mpt, cmd) == TRUE) {
8149 8150                                  /*
8150 8151                                   * Send the config page request and delete it
8151 8152                                   * from the waitq.
8152 8153                                   */
8153 8154                                  cmd->cmd_flags |= CFLAG_PREPARED;
8154 8155                                  mptsas_waitq_delete(mpt, cmd);
8155 8156                                  mptsas_start_config_page_access(mpt, cmd);
8156 8157                          }
8157 8158                          cmd = next_cmd;
8158 8159                          continue;
8159 8160                  }
8160 8161                  if (cmd->cmd_flags & CFLAG_FW_DIAG) {
8161 8162                          if (mptsas_save_cmd(mpt, cmd) == TRUE) {
8162 8163                                  /*
8163 8164                                   * Send the FW Diag request and delete if from
8164 8165                                   * the waitq.
8165 8166                                   */
8166 8167                                  cmd->cmd_flags |= CFLAG_PREPARED;
8167 8168                                  mptsas_waitq_delete(mpt, cmd);
8168 8169                                  mptsas_start_diag(mpt, cmd);
8169 8170                          }
8170 8171                          cmd = next_cmd;
8171 8172                          continue;
8172 8173                  }
8173 8174  
8174 8175                  ptgt = cmd->cmd_tgt_addr;
8175 8176                  if (ptgt) {
8176 8177                          mutex_enter(&mpt->m_intr_mutex);
8177 8178                          mutex_enter(&ptgt->m_tgt_intr_mutex);
8178 8179                          if ((ptgt->m_t_throttle == DRAIN_THROTTLE) &&
8179 8180                              (ptgt->m_t_ncmds == 0)) {
8180 8181                                  mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
8181 8182                          }
8182 8183                          if ((ptgt->m_reset_delay == 0) &&
8183 8184                              (ptgt->m_t_ncmds < ptgt->m_t_throttle)) {
8184 8185                                  mutex_exit(&ptgt->m_tgt_intr_mutex);
8185 8186                                  mutex_exit(&mpt->m_intr_mutex);
8186 8187                                  if (mptsas_save_cmd(mpt, cmd) == TRUE) {
8187 8188                                          mptsas_waitq_delete(mpt, cmd);
8188 8189                                          (void) mptsas_start_cmd(mpt, cmd);
8189 8190                                  }
8190 8191                                  goto out;
8191 8192                          }
8192 8193                          mutex_exit(&ptgt->m_tgt_intr_mutex);
8193 8194                          mutex_exit(&mpt->m_intr_mutex);
8194 8195                  }
8195 8196  out:
8196 8197                  cmd = next_cmd;
8197 8198          }
8198 8199  }
8199 8200  
8200 8201  /*
8201 8202   * mpt tag type lookup
8202 8203   */
8203 8204  static char mptsas_tag_lookup[] =
8204 8205          {0, MSG_HEAD_QTAG, MSG_ORDERED_QTAG, 0, MSG_SIMPLE_QTAG};
8205 8206  
8206 8207  /*
8207 8208   * mptsas_start_cmd0 is similar to mptsas_start_cmd, except that, it is called
8208 8209   * without ANY mutex protected, while, mptsas_start_cmd is called with m_mutex
8209 8210   * protected.
8210 8211   *
8211 8212   * the relevant field in ptgt should be protected by m_tgt_intr_mutex in both
8212 8213   * functions.
8213 8214   *
8214 8215   * before the cmds are linked on the slot for monitor as outstanding cmds, they
8215 8216   * are accessed as slab objects, so slab framework ensures the exclusive access,
8216 8217   * and no other mutex is requireed. Linking for monitor and the trigger of dma
8217 8218   * must be done exclusively.
8218 8219   */
8219 8220  static int
8220 8221  mptsas_start_cmd0(mptsas_t *mpt, mptsas_cmd_t *cmd)
8221 8222  {
8222 8223          struct scsi_pkt         *pkt = CMD2PKT(cmd);
8223 8224          uint32_t                control = 0;
8224 8225          int                     n;
8225 8226          caddr_t                 mem;
8226 8227          pMpi2SCSIIORequest_t    io_request;
8227 8228          ddi_dma_handle_t        dma_hdl = mpt->m_dma_req_frame_hdl;
8228 8229          ddi_acc_handle_t        acc_hdl = mpt->m_acc_req_frame_hdl;
8229 8230          mptsas_target_t         *ptgt = cmd->cmd_tgt_addr;
8230 8231          uint16_t                SMID, io_flags = 0;
8231 8232          uint32_t                request_desc_low, request_desc_high;
8232 8233  
8233 8234          NDBG1(("mptsas_start_cmd0: cmd=0x%p", (void *)cmd));
8234 8235  
8235 8236          /*
8236 8237           * Set SMID and increment index.  Rollover to 1 instead of 0 if index
8237 8238           * is at the max.  0 is an invalid SMID, so we call the first index 1.
8238 8239           */
8239 8240          SMID = cmd->cmd_slot;
8240 8241  
8241 8242          /*
8242 8243           * It is possible for back to back device reset to
8243 8244           * happen before the reset delay has expired.  That's
8244 8245           * ok, just let the device reset go out on the bus.
8245 8246           */
8246 8247          if ((cmd->cmd_pkt_flags & FLAG_NOINTR) == 0) {
8247 8248                  ASSERT(ptgt->m_reset_delay == 0);
8248 8249          }
8249 8250  
8250 8251          /*
8251 8252           * if a non-tagged cmd is submitted to an active tagged target
8252 8253           * then drain before submitting this cmd; SCSI-2 allows RQSENSE
8253 8254           * to be untagged
8254 8255           */
8255 8256          mutex_enter(&ptgt->m_tgt_intr_mutex);
8256 8257          if (((cmd->cmd_pkt_flags & FLAG_TAGMASK) == 0) &&
8257 8258              (ptgt->m_t_ncmds > 1) &&
8258 8259              ((cmd->cmd_flags & CFLAG_TM_CMD) == 0) &&
8259 8260              (*(cmd->cmd_pkt->pkt_cdbp) != SCMD_REQUEST_SENSE)) {
8260 8261                  if ((cmd->cmd_pkt_flags & FLAG_NOINTR) == 0) {
8261 8262                          NDBG23(("target=%d, untagged cmd, start draining\n",
8262 8263                              ptgt->m_devhdl));
8263 8264  
8264 8265                          if (ptgt->m_reset_delay == 0) {
8265 8266                                  mptsas_set_throttle(mpt, ptgt, DRAIN_THROTTLE);
8266 8267                          }
8267 8268                          mutex_exit(&ptgt->m_tgt_intr_mutex);
8268 8269  
8269 8270                          mutex_enter(&mpt->m_mutex);
8270 8271                          mptsas_remove_cmd(mpt, cmd);
8271 8272                          cmd->cmd_pkt_flags |= FLAG_HEAD;
8272 8273                          mptsas_waitq_add(mpt, cmd);
8273 8274                          mutex_exit(&mpt->m_mutex);
8274 8275                          return (DDI_FAILURE);
8275 8276                  }
8276 8277                  mutex_exit(&ptgt->m_tgt_intr_mutex);
8277 8278                  return (DDI_FAILURE);
8278 8279          }
8279 8280          mutex_exit(&ptgt->m_tgt_intr_mutex);
8280 8281  
8281 8282          /*
8282 8283           * Set correct tag bits.
8283 8284           */
8284 8285          if (cmd->cmd_pkt_flags & FLAG_TAGMASK) {
8285 8286                  switch (mptsas_tag_lookup[((cmd->cmd_pkt_flags &
8286 8287                      FLAG_TAGMASK) >> 12)]) {
8287 8288                  case MSG_SIMPLE_QTAG:
8288 8289                          control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
8289 8290                          break;
8290 8291                  case MSG_HEAD_QTAG:
8291 8292                          control |= MPI2_SCSIIO_CONTROL_HEADOFQ;
8292 8293                          break;
8293 8294                  case MSG_ORDERED_QTAG:
8294 8295                          control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
8295 8296                          break;
8296 8297                  default:
8297 8298                          mptsas_log(mpt, CE_WARN, "mpt: Invalid tag type\n");
8298 8299                          break;
8299 8300                  }
8300 8301          } else {
8301 8302                  if (*(cmd->cmd_pkt->pkt_cdbp) != SCMD_REQUEST_SENSE) {
8302 8303                                  ptgt->m_t_throttle = 1;
8303 8304                  }
8304 8305                  control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
8305 8306          }
8306 8307  
8307 8308          if (cmd->cmd_pkt_flags & FLAG_TLR) {
8308 8309                  control |= MPI2_SCSIIO_CONTROL_TLR_ON;
8309 8310          }
8310 8311  
8311 8312          mem = mpt->m_req_frame + (mpt->m_req_frame_size * SMID);
8312 8313          io_request = (pMpi2SCSIIORequest_t)mem;
8313 8314  
8314 8315          bzero(io_request, sizeof (Mpi2SCSIIORequest_t));
8315 8316          ddi_put8(acc_hdl, &io_request->SGLOffset0, offsetof
8316 8317              (MPI2_SCSI_IO_REQUEST, SGL) / 4);
8317 8318          mptsas_init_std_hdr(acc_hdl, io_request, ptgt->m_devhdl, Lun(cmd), 0,
8318 8319              MPI2_FUNCTION_SCSI_IO_REQUEST);
8319 8320  
8320 8321          (void) ddi_rep_put8(acc_hdl, (uint8_t *)pkt->pkt_cdbp,
8321 8322              io_request->CDB.CDB32, cmd->cmd_cdblen, DDI_DEV_AUTOINCR);
8322 8323  
8323 8324          io_flags = cmd->cmd_cdblen;
8324 8325          ddi_put16(acc_hdl, &io_request->IoFlags, io_flags);
8325 8326          /*
8326 8327           * setup the Scatter/Gather DMA list for this request
8327 8328           */
8328 8329          if (cmd->cmd_cookiec > 0) {
8329 8330                  mptsas_sge_setup(mpt, cmd, &control, io_request, acc_hdl);
8330 8331          } else {
8331 8332                  ddi_put32(acc_hdl, &io_request->SGL.MpiSimple.FlagsLength,
8332 8333                      ((uint32_t)MPI2_SGE_FLAGS_LAST_ELEMENT |
8333 8334                      MPI2_SGE_FLAGS_END_OF_BUFFER |
8334 8335                      MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
8335 8336                      MPI2_SGE_FLAGS_END_OF_LIST) << MPI2_SGE_FLAGS_SHIFT);
8336 8337          }
8337 8338  
8338 8339          /*
8339 8340           * save ARQ information
8340 8341           */
8341 8342          ddi_put8(acc_hdl, &io_request->SenseBufferLength, cmd->cmd_rqslen);
8342 8343          if ((cmd->cmd_flags & (CFLAG_SCBEXTERN | CFLAG_EXTARQBUFVALID)) ==
8343 8344              (CFLAG_SCBEXTERN | CFLAG_EXTARQBUFVALID)) {
8344 8345                  ddi_put32(acc_hdl, &io_request->SenseBufferLowAddress,
8345 8346                      cmd->cmd_ext_arqcookie.dmac_address);
8346 8347          } else {
8347 8348                  ddi_put32(acc_hdl, &io_request->SenseBufferLowAddress,
8348 8349                      cmd->cmd_arqcookie.dmac_address);
8349 8350          }
8350 8351  
8351 8352          ddi_put32(acc_hdl, &io_request->Control, control);
8352 8353  
8353 8354          NDBG31(("starting message=0x%p, with cmd=0x%p",
8354 8355              (void *)(uintptr_t)mpt->m_req_frame_dma_addr, (void *)cmd));
8355 8356  
8356 8357          (void) ddi_dma_sync(dma_hdl, 0, 0, DDI_DMA_SYNC_FORDEV);
8357 8358  
8358 8359          /*
8359 8360           * Build request descriptor and write it to the request desc post reg.
8360 8361           */
8361 8362          request_desc_low = (SMID << 16) + MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
8362 8363          request_desc_high = ptgt->m_devhdl << 16;
8363 8364  
8364 8365          mutex_enter(&mpt->m_mutex);
8365 8366          mpt->m_active->m_slot[cmd->cmd_slot] = cmd;
8366 8367          MPTSAS_START_CMD(mpt, request_desc_low, request_desc_high);
8367 8368          mutex_exit(&mpt->m_mutex);
8368 8369  
8369 8370          /*
8370 8371           * Start timeout.
8371 8372           */
8372 8373          mutex_enter(&ptgt->m_tgt_intr_mutex);
8373 8374  #ifdef MPTSAS_TEST
8374 8375          /*
8375 8376           * Temporarily set timebase = 0;  needed for
8376 8377           * timeout torture test.
8377 8378           */
8378 8379          if (mptsas_test_timeouts) {
8379 8380                  ptgt->m_timebase = 0;
8380 8381          }
8381 8382  #endif
8382 8383          n = pkt->pkt_time - ptgt->m_timebase;
8383 8384  
8384 8385          if (n == 0) {
8385 8386                  (ptgt->m_dups)++;
8386 8387                  ptgt->m_timeout = ptgt->m_timebase;
8387 8388          } else if (n > 0) {
8388 8389                  ptgt->m_timeout =
8389 8390                      ptgt->m_timebase = pkt->pkt_time;
8390 8391                  ptgt->m_dups = 1;
8391 8392          } else if (n < 0) {
8392 8393                  ptgt->m_timeout = ptgt->m_timebase;
8393 8394          }
8394 8395  #ifdef MPTSAS_TEST
8395 8396          /*
8396 8397           * Set back to a number higher than
8397 8398           * mptsas_scsi_watchdog_tick
8398 8399           * so timeouts will happen in mptsas_watchsubr
8399 8400           */
8400 8401          if (mptsas_test_timeouts) {
8401 8402                  ptgt->m_timebase = 60;
8402 8403          }
8403 8404  #endif
8404 8405          mutex_exit(&ptgt->m_tgt_intr_mutex);
8405 8406  
8406 8407          if ((mptsas_check_dma_handle(dma_hdl) != DDI_SUCCESS) ||
8407 8408              (mptsas_check_acc_handle(acc_hdl) != DDI_SUCCESS)) {
8408 8409                  ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
8409 8410                  return (DDI_FAILURE);
8410 8411          }
8411 8412          return (DDI_SUCCESS);
8412 8413  }
8413 8414  
8414 8415  static int
8415 8416  mptsas_start_cmd(mptsas_t *mpt, mptsas_cmd_t *cmd)
8416 8417  {
8417 8418          struct scsi_pkt         *pkt = CMD2PKT(cmd);
8418 8419          uint32_t                control = 0;
8419 8420          int                     n;
8420 8421          caddr_t                 mem;
8421 8422          pMpi2SCSIIORequest_t    io_request;
8422 8423          ddi_dma_handle_t        dma_hdl = mpt->m_dma_req_frame_hdl;
8423 8424          ddi_acc_handle_t        acc_hdl = mpt->m_acc_req_frame_hdl;
8424 8425          mptsas_target_t         *ptgt = cmd->cmd_tgt_addr;
8425 8426          uint16_t                SMID, io_flags = 0;
8426 8427          uint32_t                request_desc_low, request_desc_high;
8427 8428  
8428 8429          NDBG1(("mptsas_start_cmd: cmd=0x%p", (void *)cmd));
8429 8430  
8430 8431          /*
8431 8432           * Set SMID and increment index.  Rollover to 1 instead of 0 if index
8432 8433           * is at the max.  0 is an invalid SMID, so we call the first index 1.
8433 8434           */
8434 8435          SMID = cmd->cmd_slot;
8435 8436  
8436 8437          /*
8437 8438           * It is possible for back to back device reset to
8438 8439           * happen before the reset delay has expired.  That's
8439 8440           * ok, just let the device reset go out on the bus.
8440 8441           */
8441 8442          if ((cmd->cmd_pkt_flags & FLAG_NOINTR) == 0) {
8442 8443                  ASSERT(ptgt->m_reset_delay == 0);
8443 8444          }
8444 8445  
8445 8446          /*
8446 8447           * if a non-tagged cmd is submitted to an active tagged target
8447 8448           * then drain before submitting this cmd; SCSI-2 allows RQSENSE
8448 8449           * to be untagged
8449 8450           */
8450 8451          mutex_enter(&ptgt->m_tgt_intr_mutex);
8451 8452          if (((cmd->cmd_pkt_flags & FLAG_TAGMASK) == 0) &&
8452 8453              (ptgt->m_t_ncmds > 1) &&
8453 8454              ((cmd->cmd_flags & CFLAG_TM_CMD) == 0) &&
8454 8455              (*(cmd->cmd_pkt->pkt_cdbp) != SCMD_REQUEST_SENSE)) {
8455 8456                  if ((cmd->cmd_pkt_flags & FLAG_NOINTR) == 0) {
8456 8457                          NDBG23(("target=%d, untagged cmd, start draining\n",
8457 8458                              ptgt->m_devhdl));
8458 8459  
8459 8460                          if (ptgt->m_reset_delay == 0) {
8460 8461                                  mptsas_set_throttle(mpt, ptgt, DRAIN_THROTTLE);
8461 8462                          }
8462 8463                          mutex_exit(&ptgt->m_tgt_intr_mutex);
8463 8464  
8464 8465                          mptsas_remove_cmd(mpt, cmd);
8465 8466                          cmd->cmd_pkt_flags |= FLAG_HEAD;
8466 8467                          mptsas_waitq_add(mpt, cmd);
8467 8468                          return (DDI_FAILURE);
8468 8469                  }
8469 8470                  mutex_exit(&ptgt->m_tgt_intr_mutex);
8470 8471                  return (DDI_FAILURE);
8471 8472          }
8472 8473          mutex_exit(&ptgt->m_tgt_intr_mutex);
8473 8474  
8474 8475          /*
8475 8476           * Set correct tag bits.
8476 8477           */
8477 8478          if (cmd->cmd_pkt_flags & FLAG_TAGMASK) {
8478 8479                  switch (mptsas_tag_lookup[((cmd->cmd_pkt_flags &
8479 8480                      FLAG_TAGMASK) >> 12)]) {
8480 8481                  case MSG_SIMPLE_QTAG:
8481 8482                          control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
8482 8483                          break;
8483 8484                  case MSG_HEAD_QTAG:
8484 8485                          control |= MPI2_SCSIIO_CONTROL_HEADOFQ;
8485 8486                          break;
8486 8487                  case MSG_ORDERED_QTAG:
8487 8488                          control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
8488 8489                          break;
8489 8490                  default:
8490 8491                          mptsas_log(mpt, CE_WARN, "mpt: Invalid tag type\n");
8491 8492                          break;
8492 8493                  }
8493 8494          } else {
8494 8495                  if (*(cmd->cmd_pkt->pkt_cdbp) != SCMD_REQUEST_SENSE) {
8495 8496                                  ptgt->m_t_throttle = 1;
8496 8497                  }
8497 8498                  control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
8498 8499          }
8499 8500  
8500 8501          if (cmd->cmd_pkt_flags & FLAG_TLR) {
8501 8502                  control |= MPI2_SCSIIO_CONTROL_TLR_ON;
8502 8503          }
8503 8504  
8504 8505          mem = mpt->m_req_frame + (mpt->m_req_frame_size * SMID);
8505 8506          io_request = (pMpi2SCSIIORequest_t)mem;
8506 8507  
8507 8508          bzero(io_request, sizeof (Mpi2SCSIIORequest_t));
8508 8509          ddi_put8(acc_hdl, &io_request->SGLOffset0, offsetof
8509 8510              (MPI2_SCSI_IO_REQUEST, SGL) / 4);
8510 8511          mptsas_init_std_hdr(acc_hdl, io_request, ptgt->m_devhdl, Lun(cmd), 0,
8511 8512              MPI2_FUNCTION_SCSI_IO_REQUEST);
8512 8513  
8513 8514          (void) ddi_rep_put8(acc_hdl, (uint8_t *)pkt->pkt_cdbp,
8514 8515              io_request->CDB.CDB32, cmd->cmd_cdblen, DDI_DEV_AUTOINCR);
8515 8516  
8516 8517          io_flags = cmd->cmd_cdblen;
8517 8518          ddi_put16(acc_hdl, &io_request->IoFlags, io_flags);
8518 8519          /*
8519 8520           * setup the Scatter/Gather DMA list for this request
8520 8521           */
8521 8522          if (cmd->cmd_cookiec > 0) {
8522 8523                  mptsas_sge_setup(mpt, cmd, &control, io_request, acc_hdl);
8523 8524          } else {
8524 8525                  ddi_put32(acc_hdl, &io_request->SGL.MpiSimple.FlagsLength,
8525 8526                      ((uint32_t)MPI2_SGE_FLAGS_LAST_ELEMENT |
8526 8527                      MPI2_SGE_FLAGS_END_OF_BUFFER |
8527 8528                      MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
8528 8529                      MPI2_SGE_FLAGS_END_OF_LIST) << MPI2_SGE_FLAGS_SHIFT);
8529 8530          }
8530 8531  
8531 8532          /*
8532 8533           * save ARQ information
8533 8534           */
8534 8535          ddi_put8(acc_hdl, &io_request->SenseBufferLength, cmd->cmd_rqslen);
8535 8536          if ((cmd->cmd_flags & (CFLAG_SCBEXTERN | CFLAG_EXTARQBUFVALID)) ==
8536 8537              (CFLAG_SCBEXTERN | CFLAG_EXTARQBUFVALID)) {
8537 8538                  ddi_put32(acc_hdl, &io_request->SenseBufferLowAddress,
8538 8539                      cmd->cmd_ext_arqcookie.dmac_address);
8539 8540          } else {
8540 8541                  ddi_put32(acc_hdl, &io_request->SenseBufferLowAddress,
8541 8542                      cmd->cmd_arqcookie.dmac_address);
8542 8543          }
8543 8544  
8544 8545          ddi_put32(acc_hdl, &io_request->Control, control);
8545 8546  
8546 8547          NDBG31(("starting message=0x%p, with cmd=0x%p",
8547 8548              (void *)(uintptr_t)mpt->m_req_frame_dma_addr, (void *)cmd));
8548 8549  
8549 8550          (void) ddi_dma_sync(dma_hdl, 0, 0, DDI_DMA_SYNC_FORDEV);
8550 8551  
8551 8552          /*
8552 8553           * Build request descriptor and write it to the request desc post reg.
8553 8554           */
8554 8555          request_desc_low = (SMID << 16) + MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
8555 8556          request_desc_high = ptgt->m_devhdl << 16;
8556 8557  
8557 8558          mpt->m_active->m_slot[cmd->cmd_slot] = cmd;
8558 8559          MPTSAS_START_CMD(mpt, request_desc_low, request_desc_high);
8559 8560  
8560 8561          /*
8561 8562           * Start timeout.
8562 8563           */
8563 8564          mutex_enter(&ptgt->m_tgt_intr_mutex);
8564 8565  #ifdef MPTSAS_TEST
8565 8566          /*
8566 8567           * Temporarily set timebase = 0;  needed for
8567 8568           * timeout torture test.
8568 8569           */
8569 8570          if (mptsas_test_timeouts) {
8570 8571                  ptgt->m_timebase = 0;
8571 8572          }
8572 8573  #endif
8573 8574          n = pkt->pkt_time - ptgt->m_timebase;
8574 8575  
8575 8576          if (n == 0) {
8576 8577                  (ptgt->m_dups)++;
8577 8578                  ptgt->m_timeout = ptgt->m_timebase;
8578 8579          } else if (n > 0) {
8579 8580                  ptgt->m_timeout =
8580 8581                      ptgt->m_timebase = pkt->pkt_time;
8581 8582                  ptgt->m_dups = 1;
8582 8583          } else if (n < 0) {
8583 8584                  ptgt->m_timeout = ptgt->m_timebase;
8584 8585          }
8585 8586  #ifdef MPTSAS_TEST
8586 8587          /*
8587 8588           * Set back to a number higher than
8588 8589           * mptsas_scsi_watchdog_tick
8589 8590           * so timeouts will happen in mptsas_watchsubr
8590 8591           */
8591 8592          if (mptsas_test_timeouts) {
8592 8593                  ptgt->m_timebase = 60;
8593 8594          }
8594 8595  #endif
8595 8596          mutex_exit(&ptgt->m_tgt_intr_mutex);
8596 8597  
8597 8598          if ((mptsas_check_dma_handle(dma_hdl) != DDI_SUCCESS) ||
8598 8599              (mptsas_check_acc_handle(acc_hdl) != DDI_SUCCESS)) {
8599 8600                  ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
8600 8601                  return (DDI_FAILURE);
8601 8602          }
8602 8603          return (DDI_SUCCESS);
8603 8604  }
8604 8605  
8605 8606  /*
8606 8607   * Select a helper thread to handle current doneq
8607 8608   */
8608 8609  static void
8609 8610  mptsas_deliver_doneq_thread(mptsas_t *mpt)
8610 8611  {
8611 8612          uint64_t                        t, i;
8612 8613          uint32_t                        min = 0xffffffff;
8613 8614          mptsas_doneq_thread_list_t      *item;
8614 8615  
8615 8616          for (i = 0; i < mpt->m_doneq_thread_n; i++) {
8616 8617                  item = &mpt->m_doneq_thread_id[i];
8617 8618                  /*
8618 8619                   * If the completed command on help thread[i] less than
8619 8620                   * doneq_thread_threshold, then pick the thread[i]. Otherwise
8620 8621                   * pick a thread which has least completed command.
8621 8622                   */
8622 8623  
8623 8624                  mutex_enter(&item->mutex);
8624 8625                  if (item->len < mpt->m_doneq_thread_threshold) {
8625 8626                          t = i;
8626 8627                          mutex_exit(&item->mutex);
8627 8628                          break;
8628 8629                  }
8629 8630                  if (item->len < min) {
8630 8631                          min = item->len;
8631 8632                          t = i;
8632 8633                  }
8633 8634                  mutex_exit(&item->mutex);
8634 8635          }
8635 8636          mutex_enter(&mpt->m_doneq_thread_id[t].mutex);
8636 8637          mptsas_doneq_mv(mpt, t);
8637 8638          cv_signal(&mpt->m_doneq_thread_id[t].cv);
8638 8639          mutex_exit(&mpt->m_doneq_thread_id[t].mutex);
8639 8640  }
8640 8641  
8641 8642  /*
8642 8643   * move the current global doneq to the doneq of thread[t]
8643 8644   */
8644 8645  static void
8645 8646  mptsas_doneq_mv(mptsas_t *mpt, uint64_t t)
8646 8647  {
8647 8648          mptsas_cmd_t                    *cmd;
8648 8649          mptsas_doneq_thread_list_t      *item = &mpt->m_doneq_thread_id[t];
8649 8650  
8650 8651          ASSERT(mutex_owned(&item->mutex));
8651 8652          mutex_enter(&mpt->m_intr_mutex);
8652 8653          while ((cmd = mpt->m_doneq) != NULL) {
8653 8654                  if ((mpt->m_doneq = cmd->cmd_linkp) == NULL) {
8654 8655                          mpt->m_donetail = &mpt->m_doneq;
8655 8656                  }
8656 8657                  cmd->cmd_linkp = NULL;
8657 8658                  *item->donetail = cmd;
8658 8659                  item->donetail = &cmd->cmd_linkp;
8659 8660                  mpt->m_doneq_len--;
8660 8661                  item->len++;
8661 8662          }
8662 8663          mutex_exit(&mpt->m_intr_mutex);
8663 8664  }
8664 8665  
8665 8666  void
8666 8667  mptsas_fma_check(mptsas_t *mpt, mptsas_cmd_t *cmd)
8667 8668  {
8668 8669          struct scsi_pkt *pkt = CMD2PKT(cmd);
8669 8670  
8670 8671          /* Check all acc and dma handles */
8671 8672          if ((mptsas_check_acc_handle(mpt->m_datap) !=
8672 8673              DDI_SUCCESS) ||
8673 8674              (mptsas_check_acc_handle(mpt->m_acc_req_frame_hdl) !=
8674 8675              DDI_SUCCESS) ||
8675 8676              (mptsas_check_acc_handle(mpt->m_acc_reply_frame_hdl) !=
8676 8677              DDI_SUCCESS) ||
8677 8678              (mptsas_check_acc_handle(mpt->m_acc_free_queue_hdl) !=
8678 8679              DDI_SUCCESS) ||
8679 8680              (mptsas_check_acc_handle(mpt->m_acc_post_queue_hdl) !=
8680 8681              DDI_SUCCESS) ||
8681 8682              (mptsas_check_acc_handle(mpt->m_hshk_acc_hdl) !=
8682 8683              DDI_SUCCESS) ||
8683 8684              (mptsas_check_acc_handle(mpt->m_config_handle) !=
8684 8685              DDI_SUCCESS)) {
8685 8686                  ddi_fm_service_impact(mpt->m_dip,
8686 8687                      DDI_SERVICE_UNAFFECTED);
8687 8688                  ddi_fm_acc_err_clear(mpt->m_config_handle,
8688 8689                      DDI_FME_VER0);
8689 8690                  pkt->pkt_reason = CMD_TRAN_ERR;
8690 8691                  pkt->pkt_statistics = 0;
8691 8692          }
8692 8693          if ((mptsas_check_dma_handle(mpt->m_dma_req_frame_hdl) !=
8693 8694              DDI_SUCCESS) ||
8694 8695              (mptsas_check_dma_handle(mpt->m_dma_reply_frame_hdl) !=
8695 8696              DDI_SUCCESS) ||
8696 8697              (mptsas_check_dma_handle(mpt->m_dma_free_queue_hdl) !=
8697 8698              DDI_SUCCESS) ||
8698 8699              (mptsas_check_dma_handle(mpt->m_dma_post_queue_hdl) !=
8699 8700              DDI_SUCCESS) ||
8700 8701              (mptsas_check_dma_handle(mpt->m_hshk_dma_hdl) !=
8701 8702              DDI_SUCCESS)) {
8702 8703                  ddi_fm_service_impact(mpt->m_dip,
8703 8704                      DDI_SERVICE_UNAFFECTED);
8704 8705                  pkt->pkt_reason = CMD_TRAN_ERR;
8705 8706                  pkt->pkt_statistics = 0;
8706 8707          }
8707 8708          if (cmd->cmd_dmahandle &&
8708 8709              (mptsas_check_dma_handle(cmd->cmd_dmahandle) != DDI_SUCCESS)) {
8709 8710                  ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
8710 8711                  pkt->pkt_reason = CMD_TRAN_ERR;
8711 8712                  pkt->pkt_statistics = 0;
8712 8713          }
8713 8714          if ((cmd->cmd_extra_frames &&
8714 8715              ((mptsas_check_dma_handle(cmd->cmd_extra_frames->m_dma_hdl) !=
8715 8716              DDI_SUCCESS) ||
8716 8717              (mptsas_check_acc_handle(cmd->cmd_extra_frames->m_acc_hdl) !=
8717 8718              DDI_SUCCESS)))) {
8718 8719                  ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
8719 8720                  pkt->pkt_reason = CMD_TRAN_ERR;
8720 8721                  pkt->pkt_statistics = 0;
8721 8722          }
8722 8723          if (cmd->cmd_arqhandle &&
8723 8724              (mptsas_check_dma_handle(cmd->cmd_arqhandle) != DDI_SUCCESS)) {
8724 8725                  ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
8725 8726                  pkt->pkt_reason = CMD_TRAN_ERR;
8726 8727                  pkt->pkt_statistics = 0;
8727 8728          }
8728 8729          if (cmd->cmd_ext_arqhandle &&
8729 8730              (mptsas_check_dma_handle(cmd->cmd_ext_arqhandle) != DDI_SUCCESS)) {
8730 8731                  ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
8731 8732                  pkt->pkt_reason = CMD_TRAN_ERR;
8732 8733                  pkt->pkt_statistics = 0;
8733 8734          }
8734 8735  }
8735 8736  
8736 8737  /*
8737 8738   * mptsas_doneq_add0 is similar to mptsas_doneq_add except that it is called
8738 8739   * where m_intr_mutex has already been held.
8739 8740   */
8740 8741  static inline void
8741 8742  mptsas_doneq_add0(mptsas_t *mpt, mptsas_cmd_t *cmd)
8742 8743  {
8743 8744          struct scsi_pkt *pkt = CMD2PKT(cmd);
8744 8745  
8745 8746          NDBG31(("mptsas_doneq_add0: cmd=0x%p", (void *)cmd));
8746 8747  
8747 8748          ASSERT((cmd->cmd_flags & CFLAG_COMPLETED) == 0);
8748 8749          cmd->cmd_linkp = NULL;
8749 8750          cmd->cmd_flags |= CFLAG_FINISHED;
8750 8751          cmd->cmd_flags &= ~CFLAG_IN_TRANSPORT;
8751 8752  
8752 8753          /*
8753 8754           * only add scsi pkts that have completion routines to
8754 8755           * the doneq.  no intr cmds do not have callbacks.
8755 8756           */
8756 8757          if (pkt && (pkt->pkt_comp)) {
8757 8758                  *mpt->m_donetail = cmd;
8758 8759                  mpt->m_donetail = &cmd->cmd_linkp;
8759 8760                  mpt->m_doneq_len++;
8760 8761          }
8761 8762  }
8762 8763  
8763 8764  /*
8764 8765   * These routines manipulate the queue of commands that
8765 8766   * are waiting for their completion routines to be called.
8766 8767   * The queue is usually in FIFO order but on an MP system
8767 8768   * it's possible for the completion routines to get out
8768 8769   * of order. If that's a problem you need to add a global
8769 8770   * mutex around the code that calls the completion routine
8770 8771   * in the interrupt handler.
8771 8772   */
8772 8773  static void
8773 8774  mptsas_doneq_add(mptsas_t *mpt, mptsas_cmd_t *cmd)
8774 8775  {
8775 8776          ASSERT(mutex_owned(&mpt->m_mutex));
8776 8777  
8777 8778          mptsas_fma_check(mpt, cmd);
8778 8779  
8779 8780          mutex_enter(&mpt->m_intr_mutex);
8780 8781          mptsas_doneq_add0(mpt, cmd);
8781 8782          mutex_exit(&mpt->m_intr_mutex);
8782 8783  }
8783 8784  
8784 8785  static mptsas_cmd_t *
8785 8786  mptsas_doneq_thread_rm(mptsas_t *mpt, uint64_t t)
8786 8787  {
8787 8788          mptsas_cmd_t                    *cmd;
8788 8789          mptsas_doneq_thread_list_t      *item = &mpt->m_doneq_thread_id[t];
8789 8790  
8790 8791          /* pop one off the done queue */
8791 8792          if ((cmd = item->doneq) != NULL) {
8792 8793                  /* if the queue is now empty fix the tail pointer */
8793 8794                  NDBG31(("mptsas_doneq_thread_rm: cmd=0x%p", (void *)cmd));
8794 8795                  if ((item->doneq = cmd->cmd_linkp) == NULL) {
8795 8796                          item->donetail = &item->doneq;
8796 8797                  }
8797 8798                  cmd->cmd_linkp = NULL;
8798 8799                  item->len--;
8799 8800          }
8800 8801          return (cmd);
8801 8802  }
8802 8803  
8803 8804  static void
8804 8805  mptsas_doneq_empty(mptsas_t *mpt)
8805 8806  {
8806 8807          mutex_enter(&mpt->m_intr_mutex);
8807 8808          if (mpt->m_doneq && !mpt->m_in_callback) {
8808 8809                  mptsas_cmd_t    *cmd, *next;
8809 8810                  struct scsi_pkt *pkt;
8810 8811  
8811 8812                  mpt->m_in_callback = 1;
8812 8813                  cmd = mpt->m_doneq;
8813 8814                  mpt->m_doneq = NULL;
8814 8815                  mpt->m_donetail = &mpt->m_doneq;
8815 8816                  mpt->m_doneq_len = 0;
8816 8817  
8817 8818                  mutex_exit(&mpt->m_intr_mutex);
8818 8819  
8819 8820                  /*
8820 8821                   * ONLY in ISR, is it called without m_mutex held, otherwise,
8821 8822                   * it is always called with m_mutex held.
8822 8823                   */
8823 8824                  if ((curthread->t_flag & T_INTR_THREAD) == 0)
8824 8825                          mutex_exit(&mpt->m_mutex);
8825 8826                  /*
8826 8827                   * run the completion routines of all the
8827 8828                   * completed commands
8828 8829                   */
8829 8830                  while (cmd != NULL) {
8830 8831                          next = cmd->cmd_linkp;
8831 8832                          cmd->cmd_linkp = NULL;
8832 8833                          /* run this command's completion routine */
8833 8834                          cmd->cmd_flags |= CFLAG_COMPLETED;
8834 8835                          pkt = CMD2PKT(cmd);
8835 8836                          mptsas_pkt_comp(pkt, cmd);
8836 8837                          cmd = next;
8837 8838                  }
8838 8839                  if ((curthread->t_flag & T_INTR_THREAD) == 0)
8839 8840                          mutex_enter(&mpt->m_mutex);
8840 8841                  mpt->m_in_callback = 0;
8841 8842                  return;
8842 8843          }
8843 8844          mutex_exit(&mpt->m_intr_mutex);
8844 8845  }
8845 8846  
8846 8847  /*
8847 8848   * These routines manipulate the target's queue of pending requests
8848 8849   */
8849 8850  void
8850 8851  mptsas_waitq_add(mptsas_t *mpt, mptsas_cmd_t *cmd)
8851 8852  {
8852 8853          NDBG7(("mptsas_waitq_add: cmd=0x%p", (void *)cmd));
8853 8854          mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
8854 8855          cmd->cmd_queued = TRUE;
8855 8856          if (ptgt)
8856 8857                  ptgt->m_t_nwait++;
8857 8858          if (cmd->cmd_pkt_flags & FLAG_HEAD) {
8858 8859                  mutex_enter(&mpt->m_intr_mutex);
8859 8860                  if ((cmd->cmd_linkp = mpt->m_waitq) == NULL) {
8860 8861                          mpt->m_waitqtail = &cmd->cmd_linkp;
8861 8862                  }
8862 8863                  mpt->m_waitq = cmd;
8863 8864                  mutex_exit(&mpt->m_intr_mutex);
8864 8865          } else {
8865 8866                  cmd->cmd_linkp = NULL;
8866 8867                  *(mpt->m_waitqtail) = cmd;
8867 8868                  mpt->m_waitqtail = &cmd->cmd_linkp;
8868 8869          }
8869 8870  }
8870 8871  
8871 8872  static mptsas_cmd_t *
8872 8873  mptsas_waitq_rm(mptsas_t *mpt)
8873 8874  {
8874 8875          mptsas_cmd_t    *cmd;
8875 8876          mptsas_target_t *ptgt;
8876 8877          NDBG7(("mptsas_waitq_rm"));
8877 8878  
8878 8879          mutex_enter(&mpt->m_intr_mutex);
8879 8880          MPTSAS_WAITQ_RM(mpt, cmd);
8880 8881          mutex_exit(&mpt->m_intr_mutex);
8881 8882  
8882 8883          NDBG7(("mptsas_waitq_rm: cmd=0x%p", (void *)cmd));
8883 8884          if (cmd) {
8884 8885                  ptgt = cmd->cmd_tgt_addr;
8885 8886                  if (ptgt) {
8886 8887                          ptgt->m_t_nwait--;
8887 8888                          ASSERT(ptgt->m_t_nwait >= 0);
8888 8889                  }
8889 8890          }
8890 8891          return (cmd);
8891 8892  }
8892 8893  
8893 8894  /*
8894 8895   * remove specified cmd from the middle of the wait queue.
8895 8896   */
8896 8897  static void
8897 8898  mptsas_waitq_delete(mptsas_t *mpt, mptsas_cmd_t *cmd)
8898 8899  {
8899 8900          mptsas_cmd_t    *prevp = mpt->m_waitq;
8900 8901          mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
8901 8902  
8902 8903          NDBG7(("mptsas_waitq_delete: mpt=0x%p cmd=0x%p",
8903 8904              (void *)mpt, (void *)cmd));
8904 8905          if (ptgt) {
8905 8906                  ptgt->m_t_nwait--;
8906 8907                  ASSERT(ptgt->m_t_nwait >= 0);
8907 8908          }
8908 8909  
8909 8910          if (prevp == cmd) {
8910 8911                  mutex_enter(&mpt->m_intr_mutex);
8911 8912                  if ((mpt->m_waitq = cmd->cmd_linkp) == NULL)
8912 8913                          mpt->m_waitqtail = &mpt->m_waitq;
8913 8914                  mutex_exit(&mpt->m_intr_mutex);
8914 8915  
8915 8916                  cmd->cmd_linkp = NULL;
8916 8917                  cmd->cmd_queued = FALSE;
8917 8918                  NDBG7(("mptsas_waitq_delete: mpt=0x%p cmd=0x%p",
8918 8919                      (void *)mpt, (void *)cmd));
8919 8920                  return;
8920 8921          }
8921 8922  
8922 8923          while (prevp != NULL) {
8923 8924                  if (prevp->cmd_linkp == cmd) {
8924 8925                          if ((prevp->cmd_linkp = cmd->cmd_linkp) == NULL)
8925 8926                                  mpt->m_waitqtail = &prevp->cmd_linkp;
8926 8927  
8927 8928                          cmd->cmd_linkp = NULL;
8928 8929                          cmd->cmd_queued = FALSE;
8929 8930                          NDBG7(("mptsas_waitq_delete: mpt=0x%p cmd=0x%p",
8930 8931                              (void *)mpt, (void *)cmd));
8931 8932                          return;
8932 8933                  }
8933 8934                  prevp = prevp->cmd_linkp;
8934 8935          }
8935 8936          cmn_err(CE_PANIC, "mpt: mptsas_waitq_delete: queue botch");
8936 8937  }
8937 8938  
8938 8939  /*
8939 8940   * device and bus reset handling
8940 8941   *
8941 8942   * Notes:
8942 8943   *      - RESET_ALL:    reset the controller
8943 8944   *      - RESET_TARGET: reset the target specified in scsi_address
8944 8945   */
8945 8946  static int
8946 8947  mptsas_scsi_reset(struct scsi_address *ap, int level)
8947 8948  {
8948 8949          mptsas_t                *mpt = ADDR2MPT(ap);
8949 8950          int                     rval;
8950 8951          mptsas_tgt_private_t    *tgt_private;
8951 8952          mptsas_target_t         *ptgt = NULL;
8952 8953  
8953 8954          tgt_private = (mptsas_tgt_private_t *)ap->a_hba_tran->tran_tgt_private;
8954 8955          ptgt = tgt_private->t_private;
8955 8956          if (ptgt == NULL) {
8956 8957                  return (FALSE);
8957 8958          }
8958 8959          NDBG22(("mptsas_scsi_reset: target=%d level=%d", ptgt->m_devhdl,
8959 8960              level));
8960 8961  
8961 8962          mutex_enter(&mpt->m_mutex);
8962 8963          /*
8963 8964           * if we are not in panic set up a reset delay for this target
8964 8965           */
8965 8966          if (!ddi_in_panic()) {
8966 8967                  mptsas_setup_bus_reset_delay(mpt);
8967 8968          } else {
8968 8969                  drv_usecwait(mpt->m_scsi_reset_delay * 1000);
8969 8970          }
8970 8971          rval = mptsas_do_scsi_reset(mpt, ptgt->m_devhdl);
8971 8972          mutex_exit(&mpt->m_mutex);
8972 8973  
8973 8974          /*
8974 8975           * The transport layer expect to only see TRUE and
8975 8976           * FALSE. Therefore, we will adjust the return value
8976 8977           * if mptsas_do_scsi_reset returns FAILED.
8977 8978           */
8978 8979          if (rval == FAILED)
8979 8980                  rval = FALSE;
8980 8981          return (rval);
8981 8982  }
8982 8983  
8983 8984  static int
8984 8985  mptsas_do_scsi_reset(mptsas_t *mpt, uint16_t devhdl)
8985 8986  {
8986 8987          int             rval = FALSE;
8987 8988          uint8_t         config, disk;
8988 8989          mptsas_slots_t  *slots = mpt->m_active;
8989 8990  
8990 8991          ASSERT(mutex_owned(&mpt->m_mutex));
8991 8992  
8992 8993          if (mptsas_debug_resets) {
8993 8994                  mptsas_log(mpt, CE_WARN, "mptsas_do_scsi_reset: target=%d",
8994 8995                      devhdl);
8995 8996          }
8996 8997  
8997 8998          /*
8998 8999           * Issue a Target Reset message to the target specified but not to a
8999 9000           * disk making up a raid volume.  Just look through the RAID config
9000 9001           * Phys Disk list of DevHandles.  If the target's DevHandle is in this
9001 9002           * list, then don't reset this target.
9002 9003           */
9003 9004          for (config = 0; config < slots->m_num_raid_configs; config++) {
9004 9005                  for (disk = 0; disk < MPTSAS_MAX_DISKS_IN_CONFIG; disk++) {
9005 9006                          if (devhdl == slots->m_raidconfig[config].
9006 9007                              m_physdisk_devhdl[disk]) {
9007 9008                                  return (TRUE);
9008 9009                          }
9009 9010                  }
9010 9011          }
9011 9012  
9012 9013          rval = mptsas_ioc_task_management(mpt,
9013 9014              MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, devhdl, 0, NULL, 0, 0);
9014 9015  
9015 9016          mptsas_doneq_empty(mpt);
9016 9017          return (rval);
9017 9018  }
9018 9019  
9019 9020  static int
9020 9021  mptsas_scsi_reset_notify(struct scsi_address *ap, int flag,
9021 9022          void (*callback)(caddr_t), caddr_t arg)
9022 9023  {
9023 9024          mptsas_t        *mpt = ADDR2MPT(ap);
9024 9025  
9025 9026          NDBG22(("mptsas_scsi_reset_notify: tgt=%d", ap->a_target));
9026 9027  
9027 9028          return (scsi_hba_reset_notify_setup(ap, flag, callback, arg,
9028 9029              &mpt->m_mutex, &mpt->m_reset_notify_listf));
9029 9030  }
9030 9031  
9031 9032  static int
9032 9033  mptsas_get_name(struct scsi_device *sd, char *name, int len)
9033 9034  {
9034 9035          dev_info_t      *lun_dip = NULL;
9035 9036  
9036 9037          ASSERT(sd != NULL);
9037 9038          ASSERT(name != NULL);
9038 9039          lun_dip = sd->sd_dev;
9039 9040          ASSERT(lun_dip != NULL);
9040 9041  
9041 9042          if (mptsas_name_child(lun_dip, name, len) == DDI_SUCCESS) {
9042 9043                  return (1);
9043 9044          } else {
9044 9045                  return (0);
9045 9046          }
9046 9047  }
9047 9048  
9048 9049  static int
9049 9050  mptsas_get_bus_addr(struct scsi_device *sd, char *name, int len)
9050 9051  {
9051 9052          return (mptsas_get_name(sd, name, len));
9052 9053  }
9053 9054  
9054 9055  void
9055 9056  mptsas_set_throttle(mptsas_t *mpt, mptsas_target_t *ptgt, int what)
9056 9057  {
9057 9058  
9058 9059          NDBG25(("mptsas_set_throttle: throttle=%x", what));
9059 9060  
9060 9061          /*
9061 9062           * if the bus is draining/quiesced, no changes to the throttles
9062 9063           * are allowed. Not allowing change of throttles during draining
9063 9064           * limits error recovery but will reduce draining time
9064 9065           *
9065 9066           * all throttles should have been set to HOLD_THROTTLE
9066 9067           */
9067 9068          if (mpt->m_softstate & (MPTSAS_SS_QUIESCED | MPTSAS_SS_DRAINING)) {
9068 9069                  return;
9069 9070          }
9070 9071  
9071 9072          if (what == HOLD_THROTTLE) {
9072 9073                  ptgt->m_t_throttle = HOLD_THROTTLE;
9073 9074          } else if (ptgt->m_reset_delay == 0) {
9074 9075                  ptgt->m_t_throttle = what;
9075 9076          }
9076 9077  }
9077 9078  
9078 9079  /*
9079 9080   * Clean up from a device reset.
9080 9081   * For the case of target reset, this function clears the waitq of all
9081 9082   * commands for a particular target.   For the case of abort task set, this
9082 9083   * function clears the waitq of all commonds for a particular target/lun.
9083 9084   */
9084 9085  static void
9085 9086  mptsas_flush_target(mptsas_t *mpt, ushort_t target, int lun, uint8_t tasktype)
9086 9087  {
9087 9088          mptsas_slots_t  *slots = mpt->m_active;
9088 9089          mptsas_cmd_t    *cmd, *next_cmd;
9089 9090          int             slot;
9090 9091          uchar_t         reason;
9091 9092          uint_t          stat;
9092 9093  
9093 9094          NDBG25(("mptsas_flush_target: target=%d lun=%d", target, lun));
9094 9095  
9095 9096          /*
9096 9097           * Make sure the I/O Controller has flushed all cmds
9097 9098           * that are associated with this target for a target reset
9098 9099           * and target/lun for abort task set.
9099 9100           * Account for TM requests, which use the last SMID.
9100 9101           */
9101 9102          mutex_enter(&mpt->m_intr_mutex);
9102 9103          for (slot = 0; slot <= mpt->m_active->m_n_slots; slot++) {
9103 9104                  if ((cmd = slots->m_slot[slot]) == NULL) {
9104 9105                          continue;
9105 9106                  }
9106 9107                  reason = CMD_RESET;
9107 9108                  stat = STAT_DEV_RESET;
9108 9109                  switch (tasktype) {
9109 9110                  case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
9110 9111                          if (Tgt(cmd) == target) {
9111 9112                                  NDBG25(("mptsas_flush_target discovered non-"
9112 9113                                      "NULL cmd in slot %d, tasktype 0x%x", slot,
9113 9114                                      tasktype));
9114 9115                                  mptsas_dump_cmd(mpt, cmd);
9115 9116                                  mptsas_remove_cmd0(mpt, cmd);
9116 9117                                  mptsas_set_pkt_reason(mpt, cmd, reason, stat);
9117 9118                                  mptsas_doneq_add0(mpt, cmd);
9118 9119                          }
9119 9120                          break;
9120 9121                  case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
9121 9122                          reason = CMD_ABORTED;
9122 9123                          stat = STAT_ABORTED;
9123 9124                          /*FALLTHROUGH*/
9124 9125                  case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
9125 9126                          if ((Tgt(cmd) == target) && (Lun(cmd) == lun)) {
9126 9127  
9127 9128                                  NDBG25(("mptsas_flush_target discovered non-"
9128 9129                                      "NULL cmd in slot %d, tasktype 0x%x", slot,
9129 9130                                      tasktype));
9130 9131                                  mptsas_dump_cmd(mpt, cmd);
9131 9132                                  mptsas_remove_cmd0(mpt, cmd);
9132 9133                                  mptsas_set_pkt_reason(mpt, cmd, reason,
9133 9134                                      stat);
9134 9135                                  mptsas_doneq_add0(mpt, cmd);
9135 9136                          }
9136 9137                          break;
9137 9138                  default:
9138 9139                          break;
9139 9140                  }
9140 9141          }
9141 9142          mutex_exit(&mpt->m_intr_mutex);
9142 9143  
9143 9144          /*
9144 9145           * Flush the waitq of this target's cmds
9145 9146           */
9146 9147          cmd = mpt->m_waitq;
9147 9148  
9148 9149          reason = CMD_RESET;
9149 9150          stat = STAT_DEV_RESET;
9150 9151  
9151 9152          switch (tasktype) {
9152 9153          case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
9153 9154                  while (cmd != NULL) {
9154 9155                          next_cmd = cmd->cmd_linkp;
9155 9156                          if (Tgt(cmd) == target) {
9156 9157                                  mptsas_waitq_delete(mpt, cmd);
9157 9158                                  mptsas_set_pkt_reason(mpt, cmd,
9158 9159                                      reason, stat);
9159 9160                                  mptsas_doneq_add(mpt, cmd);
9160 9161                          }
9161 9162                          cmd = next_cmd;
9162 9163                  }
9163 9164                  break;
9164 9165          case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
9165 9166                  reason = CMD_ABORTED;
9166 9167                  stat =  STAT_ABORTED;
9167 9168                  /*FALLTHROUGH*/
9168 9169          case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
9169 9170                  while (cmd != NULL) {
9170 9171                          next_cmd = cmd->cmd_linkp;
9171 9172                          if ((Tgt(cmd) == target) && (Lun(cmd) == lun)) {
9172 9173                                  mptsas_waitq_delete(mpt, cmd);
9173 9174                                  mptsas_set_pkt_reason(mpt, cmd,
9174 9175                                      reason, stat);
9175 9176                                  mptsas_doneq_add(mpt, cmd);
9176 9177                          }
9177 9178                          cmd = next_cmd;
9178 9179                  }
9179 9180                  break;
9180 9181          default:
9181 9182                  mptsas_log(mpt, CE_WARN, "Unknown task management type %d.",
9182 9183                      tasktype);
9183 9184                  break;
9184 9185          }
9185 9186  }
9186 9187  
9187 9188  /*
9188 9189   * Clean up hba state, abort all outstanding command and commands in waitq
9189 9190   * reset timeout of all targets.
9190 9191   */
9191 9192  static void
9192 9193  mptsas_flush_hba(mptsas_t *mpt)
9193 9194  {
9194 9195          mptsas_slots_t  *slots = mpt->m_active;
9195 9196          mptsas_cmd_t    *cmd;
9196 9197          int             slot;
9197 9198  
9198 9199          NDBG25(("mptsas_flush_hba"));
9199 9200  
9200 9201          /*
9201 9202           * The I/O Controller should have already sent back
9202 9203           * all commands via the scsi I/O reply frame.  Make
9203 9204           * sure all commands have been flushed.
9204 9205           * Account for TM request, which use the last SMID.
9205 9206           */
9206 9207          mutex_enter(&mpt->m_intr_mutex);
9207 9208          for (slot = 0; slot <= mpt->m_active->m_n_slots; slot++) {
9208 9209                  if ((cmd = slots->m_slot[slot]) == NULL) {
9209 9210                          continue;
9210 9211                  }
9211 9212  
9212 9213                  if (cmd->cmd_flags & CFLAG_CMDIOC) {
9213 9214                          /*
9214 9215                           * Need to make sure to tell everyone that might be
9215 9216                           * waiting on this command that it's going to fail.  If
9216 9217                           * we get here, this command will never timeout because
9217 9218                           * the active command table is going to be re-allocated,
9218 9219                           * so there will be nothing to check against a time out.
9219 9220                           * Instead, mark the command as failed due to reset.
9220 9221                           */
9221 9222                          mptsas_set_pkt_reason(mpt, cmd, CMD_RESET,
9222 9223                              STAT_BUS_RESET);
9223 9224                          if ((cmd->cmd_flags & CFLAG_PASSTHRU) ||
9224 9225                              (cmd->cmd_flags & CFLAG_CONFIG) ||
9225 9226                              (cmd->cmd_flags & CFLAG_FW_DIAG)) {
9226 9227                                  cmd->cmd_flags |= CFLAG_FINISHED;
9227 9228                                  cv_broadcast(&mpt->m_passthru_cv);
9228 9229                                  cv_broadcast(&mpt->m_config_cv);
9229 9230                                  cv_broadcast(&mpt->m_fw_diag_cv);
9230 9231                          }
9231 9232                          continue;
9232 9233                  }
9233 9234  
9234 9235                  NDBG25(("mptsas_flush_hba discovered non-NULL cmd in slot %d",
9235 9236                      slot));
9236 9237                  mptsas_dump_cmd(mpt, cmd);
9237 9238  
9238 9239                  mptsas_remove_cmd0(mpt, cmd);
9239 9240                  mptsas_set_pkt_reason(mpt, cmd, CMD_RESET, STAT_BUS_RESET);
9240 9241                  mptsas_doneq_add0(mpt, cmd);
9241 9242          }
9242 9243          mutex_exit(&mpt->m_intr_mutex);
9243 9244  
9244 9245          /*
9245 9246           * Flush the waitq.
9246 9247           */
9247 9248          while ((cmd = mptsas_waitq_rm(mpt)) != NULL) {
9248 9249                  mptsas_set_pkt_reason(mpt, cmd, CMD_RESET, STAT_BUS_RESET);
9249 9250                  if ((cmd->cmd_flags & CFLAG_PASSTHRU) ||
9250 9251                      (cmd->cmd_flags & CFLAG_CONFIG) ||
9251 9252                      (cmd->cmd_flags & CFLAG_FW_DIAG)) {
9252 9253                          cmd->cmd_flags |= CFLAG_FINISHED;
9253 9254                          cv_broadcast(&mpt->m_passthru_cv);
9254 9255                          cv_broadcast(&mpt->m_config_cv);
9255 9256                          cv_broadcast(&mpt->m_fw_diag_cv);
9256 9257                  } else {
9257 9258                          mptsas_doneq_add(mpt, cmd);
9258 9259                  }
9259 9260          }
9260 9261  }
9261 9262  
9262 9263  /*
9263 9264   * set pkt_reason and OR in pkt_statistics flag
9264 9265   */
9265 9266  static void
9266 9267  mptsas_set_pkt_reason(mptsas_t *mpt, mptsas_cmd_t *cmd, uchar_t reason,
9267 9268      uint_t stat)
9268 9269  {
9269 9270  #ifndef __lock_lint
9270 9271          _NOTE(ARGUNUSED(mpt))
9271 9272  #endif
9272 9273  
9273 9274          NDBG25(("mptsas_set_pkt_reason: cmd=0x%p reason=%x stat=%x",
9274 9275              (void *)cmd, reason, stat));
9275 9276  
9276 9277          if (cmd) {
9277 9278                  if (cmd->cmd_pkt->pkt_reason == CMD_CMPLT) {
9278 9279                          cmd->cmd_pkt->pkt_reason = reason;
9279 9280                  }
9280 9281                  cmd->cmd_pkt->pkt_statistics |= stat;
9281 9282          }
9282 9283  }
9283 9284  
9284 9285  static void
9285 9286  mptsas_start_watch_reset_delay()
9286 9287  {
9287 9288          NDBG22(("mptsas_start_watch_reset_delay"));
9288 9289  
9289 9290          mutex_enter(&mptsas_global_mutex);
9290 9291          if (mptsas_reset_watch == NULL && mptsas_timeouts_enabled) {
9291 9292                  mptsas_reset_watch = timeout(mptsas_watch_reset_delay, NULL,
9292 9293                      drv_usectohz((clock_t)
9293 9294                      MPTSAS_WATCH_RESET_DELAY_TICK * 1000));
9294 9295                  ASSERT(mptsas_reset_watch != NULL);
9295 9296          }
9296 9297          mutex_exit(&mptsas_global_mutex);
9297 9298  }
9298 9299  
9299 9300  static void
9300 9301  mptsas_setup_bus_reset_delay(mptsas_t *mpt)
9301 9302  {
9302 9303          mptsas_target_t *ptgt = NULL;
9303 9304  
9304 9305          NDBG22(("mptsas_setup_bus_reset_delay"));
9305 9306          ptgt = (mptsas_target_t *)mptsas_hash_traverse(&mpt->m_active->m_tgttbl,
9306 9307              MPTSAS_HASH_FIRST);
9307 9308          while (ptgt != NULL) {
9308 9309                  mutex_enter(&ptgt->m_tgt_intr_mutex);
9309 9310                  mptsas_set_throttle(mpt, ptgt, HOLD_THROTTLE);
9310 9311                  ptgt->m_reset_delay = mpt->m_scsi_reset_delay;
9311 9312                  mutex_exit(&ptgt->m_tgt_intr_mutex);
9312 9313  
9313 9314                  ptgt = (mptsas_target_t *)mptsas_hash_traverse(
9314 9315                      &mpt->m_active->m_tgttbl, MPTSAS_HASH_NEXT);
9315 9316          }
9316 9317  
9317 9318          mptsas_start_watch_reset_delay();
9318 9319  }
9319 9320  
9320 9321  /*
9321 9322   * mptsas_watch_reset_delay(_subr) is invoked by timeout() and checks every
9322 9323   * mpt instance for active reset delays
9323 9324   */
9324 9325  static void
9325 9326  mptsas_watch_reset_delay(void *arg)
9326 9327  {
9327 9328  #ifndef __lock_lint
9328 9329          _NOTE(ARGUNUSED(arg))
9329 9330  #endif
9330 9331  
9331 9332          mptsas_t        *mpt;
9332 9333          int             not_done = 0;
9333 9334  
9334 9335          NDBG22(("mptsas_watch_reset_delay"));
9335 9336  
9336 9337          mutex_enter(&mptsas_global_mutex);
9337 9338          mptsas_reset_watch = 0;
9338 9339          mutex_exit(&mptsas_global_mutex);
9339 9340          rw_enter(&mptsas_global_rwlock, RW_READER);
9340 9341          for (mpt = mptsas_head; mpt != NULL; mpt = mpt->m_next) {
9341 9342                  if (mpt->m_tran == 0) {
9342 9343                          continue;
9343 9344                  }
9344 9345                  mutex_enter(&mpt->m_mutex);
9345 9346                  not_done += mptsas_watch_reset_delay_subr(mpt);
9346 9347                  mutex_exit(&mpt->m_mutex);
9347 9348          }
9348 9349          rw_exit(&mptsas_global_rwlock);
9349 9350  
9350 9351          if (not_done) {
9351 9352                  mptsas_start_watch_reset_delay();
9352 9353          }
9353 9354  }
9354 9355  
9355 9356  static int
9356 9357  mptsas_watch_reset_delay_subr(mptsas_t *mpt)
9357 9358  {
9358 9359          int             done = 0;
9359 9360          int             restart = 0;
9360 9361          mptsas_target_t *ptgt = NULL;
9361 9362  
9362 9363          NDBG22(("mptsas_watch_reset_delay_subr: mpt=0x%p", (void *)mpt));
9363 9364  
9364 9365          ASSERT(mutex_owned(&mpt->m_mutex));
9365 9366  
9366 9367          ptgt = (mptsas_target_t *)mptsas_hash_traverse(&mpt->m_active->m_tgttbl,
9367 9368              MPTSAS_HASH_FIRST);
9368 9369          while (ptgt != NULL) {
9369 9370                  mutex_enter(&ptgt->m_tgt_intr_mutex);
9370 9371                  if (ptgt->m_reset_delay != 0) {
9371 9372                          ptgt->m_reset_delay -=
9372 9373                              MPTSAS_WATCH_RESET_DELAY_TICK;
9373 9374                          if (ptgt->m_reset_delay <= 0) {
9374 9375                                  ptgt->m_reset_delay = 0;
9375 9376                                  mptsas_set_throttle(mpt, ptgt,
9376 9377                                      MAX_THROTTLE);
9377 9378                                  restart++;
9378 9379                          } else {
9379 9380                                  done = -1;
9380 9381                          }
9381 9382                  }
9382 9383                  mutex_exit(&ptgt->m_tgt_intr_mutex);
9383 9384  
9384 9385                  ptgt = (mptsas_target_t *)mptsas_hash_traverse(
9385 9386                      &mpt->m_active->m_tgttbl, MPTSAS_HASH_NEXT);
9386 9387          }
9387 9388  
9388 9389          if (restart > 0) {
9389 9390                  mptsas_restart_hba(mpt);
9390 9391          }
9391 9392          return (done);
9392 9393  }
9393 9394  
9394 9395  #ifdef MPTSAS_TEST
9395 9396  static void
9396 9397  mptsas_test_reset(mptsas_t *mpt, int target)
9397 9398  {
9398 9399          mptsas_target_t    *ptgt = NULL;
9399 9400  
9400 9401          if (mptsas_rtest == target) {
9401 9402                  if (mptsas_do_scsi_reset(mpt, target) == TRUE) {
9402 9403                          mptsas_rtest = -1;
9403 9404                  }
9404 9405                  if (mptsas_rtest == -1) {
9405 9406                          NDBG22(("mptsas_test_reset success"));
9406 9407                  }
9407 9408          }
9408 9409  }
9409 9410  #endif
9410 9411  
9411 9412  /*
9412 9413   * abort handling:
9413 9414   *
9414 9415   * Notes:
9415 9416   *      - if pkt is not NULL, abort just that command
9416 9417   *      - if pkt is NULL, abort all outstanding commands for target
9417 9418   */
9418 9419  static int
9419 9420  mptsas_scsi_abort(struct scsi_address *ap, struct scsi_pkt *pkt)
9420 9421  {
9421 9422          mptsas_t                *mpt = ADDR2MPT(ap);
9422 9423          int                     rval;
9423 9424          mptsas_tgt_private_t    *tgt_private;
9424 9425          int                     target, lun;
9425 9426  
9426 9427          tgt_private = (mptsas_tgt_private_t *)ap->a_hba_tran->
9427 9428              tran_tgt_private;
9428 9429          ASSERT(tgt_private != NULL);
9429 9430          target = tgt_private->t_private->m_devhdl;
9430 9431          lun = tgt_private->t_lun;
9431 9432  
9432 9433          NDBG23(("mptsas_scsi_abort: target=%d.%d", target, lun));
9433 9434  
9434 9435          mutex_enter(&mpt->m_mutex);
9435 9436          rval = mptsas_do_scsi_abort(mpt, target, lun, pkt);
9436 9437          mutex_exit(&mpt->m_mutex);
9437 9438          return (rval);
9438 9439  }
9439 9440  
9440 9441  static int
9441 9442  mptsas_do_scsi_abort(mptsas_t *mpt, int target, int lun, struct scsi_pkt *pkt)
9442 9443  {
9443 9444          mptsas_cmd_t    *sp = NULL;
9444 9445          mptsas_slots_t  *slots = mpt->m_active;
9445 9446          int             rval = FALSE;
9446 9447  
9447 9448          ASSERT(mutex_owned(&mpt->m_mutex));
9448 9449  
9449 9450          /*
9450 9451           * Abort the command pkt on the target/lun in ap.  If pkt is
9451 9452           * NULL, abort all outstanding commands on that target/lun.
9452 9453           * If you can abort them, return 1, else return 0.
9453 9454           * Each packet that's aborted should be sent back to the target
9454 9455           * driver through the callback routine, with pkt_reason set to
9455 9456           * CMD_ABORTED.
9456 9457           *
9457 9458           * abort cmd pkt on HBA hardware; clean out of outstanding
9458 9459           * command lists, etc.
9459 9460           */
9460 9461          if (pkt != NULL) {
9461 9462                  /* abort the specified packet */
9462 9463                  sp = PKT2CMD(pkt);
9463 9464  
9464 9465                  if (sp->cmd_queued) {
9465 9466                          NDBG23(("mptsas_do_scsi_abort: queued sp=0x%p aborted",
9466 9467                              (void *)sp));
9467 9468                          mptsas_waitq_delete(mpt, sp);
9468 9469                          mptsas_set_pkt_reason(mpt, sp, CMD_ABORTED,
9469 9470                              STAT_ABORTED);
9470 9471                          mptsas_doneq_add(mpt, sp);
9471 9472                          rval = TRUE;
9472 9473                          goto done;
9473 9474                  }
9474 9475  
9475 9476                  /*
9476 9477                   * Have mpt firmware abort this command
9477 9478                   */
9478 9479                  mutex_enter(&mpt->m_intr_mutex);
9479 9480                  if (slots->m_slot[sp->cmd_slot] != NULL) {
9480 9481                          mutex_exit(&mpt->m_intr_mutex);
9481 9482                          rval = mptsas_ioc_task_management(mpt,
9482 9483                              MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, target,
9483 9484                              lun, NULL, 0, 0);
9484 9485  
9485 9486                          /*
9486 9487                           * The transport layer expects only TRUE and FALSE.
9487 9488                           * Therefore, if mptsas_ioc_task_management returns
9488 9489                           * FAILED we will return FALSE.
9489 9490                           */
9490 9491                          if (rval == FAILED)
9491 9492                                  rval = FALSE;
9492 9493                          goto done;
9493 9494                  }
9494 9495                  mutex_exit(&mpt->m_intr_mutex);
9495 9496          }
9496 9497  
9497 9498          /*
9498 9499           * If pkt is NULL then abort task set
9499 9500           */
9500 9501          rval = mptsas_ioc_task_management(mpt,
9501 9502              MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, target, lun, NULL, 0, 0);
9502 9503  
9503 9504          /*
9504 9505           * The transport layer expects only TRUE and FALSE.
9505 9506           * Therefore, if mptsas_ioc_task_management returns
9506 9507           * FAILED we will return FALSE.
9507 9508           */
9508 9509          if (rval == FAILED)
9509 9510                  rval = FALSE;
9510 9511  
9511 9512  #ifdef MPTSAS_TEST
9512 9513          if (rval && mptsas_test_stop) {
9513 9514                  debug_enter("mptsas_do_scsi_abort");
9514 9515          }
9515 9516  #endif
9516 9517  
9517 9518  done:
9518 9519          mptsas_doneq_empty(mpt);
9519 9520          return (rval);
9520 9521  }
9521 9522  
9522 9523  /*
9523 9524   * capability handling:
9524 9525   * (*tran_getcap).  Get the capability named, and return its value.
9525 9526   */
9526 9527  static int
9527 9528  mptsas_scsi_getcap(struct scsi_address *ap, char *cap, int tgtonly)
9528 9529  {
9529 9530          mptsas_t        *mpt = ADDR2MPT(ap);
9530 9531          int             ckey;
9531 9532          int             rval = FALSE;
9532 9533  
9533 9534          NDBG24(("mptsas_scsi_getcap: target=%d, cap=%s tgtonly=%x",
9534 9535              ap->a_target, cap, tgtonly));
9535 9536  
9536 9537          mutex_enter(&mpt->m_mutex);
9537 9538  
9538 9539          if ((mptsas_scsi_capchk(cap, tgtonly, &ckey)) != TRUE) {
9539 9540                  mutex_exit(&mpt->m_mutex);
9540 9541                  return (UNDEFINED);
9541 9542          }
9542 9543  
9543 9544          switch (ckey) {
9544 9545          case SCSI_CAP_DMA_MAX:
9545 9546                  rval = (int)mpt->m_msg_dma_attr.dma_attr_maxxfer;
9546 9547                  break;
9547 9548          case SCSI_CAP_ARQ:
9548 9549                  rval = TRUE;
9549 9550                  break;
9550 9551          case SCSI_CAP_MSG_OUT:
9551 9552          case SCSI_CAP_PARITY:
9552 9553          case SCSI_CAP_UNTAGGED_QING:
9553 9554                  rval = TRUE;
9554 9555                  break;
9555 9556          case SCSI_CAP_TAGGED_QING:
9556 9557                  rval = TRUE;
9557 9558                  break;
9558 9559          case SCSI_CAP_RESET_NOTIFICATION:
9559 9560                  rval = TRUE;
9560 9561                  break;
9561 9562          case SCSI_CAP_LINKED_CMDS:
9562 9563                  rval = FALSE;
9563 9564                  break;
9564 9565          case SCSI_CAP_QFULL_RETRIES:
9565 9566                  rval = ((mptsas_tgt_private_t *)(ap->a_hba_tran->
9566 9567                      tran_tgt_private))->t_private->m_qfull_retries;
9567 9568                  break;
9568 9569          case SCSI_CAP_QFULL_RETRY_INTERVAL:
9569 9570                  rval = drv_hztousec(((mptsas_tgt_private_t *)
9570 9571                      (ap->a_hba_tran->tran_tgt_private))->
9571 9572                      t_private->m_qfull_retry_interval) / 1000;
9572 9573                  break;
9573 9574          case SCSI_CAP_CDB_LEN:
9574 9575                  rval = CDB_GROUP4;
9575 9576                  break;
9576 9577          case SCSI_CAP_INTERCONNECT_TYPE:
9577 9578                  rval = INTERCONNECT_SAS;
9578 9579                  break;
9579 9580          case SCSI_CAP_TRAN_LAYER_RETRIES:
9580 9581                  if (mpt->m_ioc_capabilities &
9581 9582                      MPI2_IOCFACTS_CAPABILITY_TLR)
9582 9583                          rval = TRUE;
9583 9584                  else
9584 9585                          rval = FALSE;
9585 9586                  break;
9586 9587          default:
9587 9588                  rval = UNDEFINED;
9588 9589                  break;
9589 9590          }
9590 9591  
9591 9592          NDBG24(("mptsas_scsi_getcap: %s, rval=%x", cap, rval));
9592 9593  
9593 9594          mutex_exit(&mpt->m_mutex);
9594 9595          return (rval);
9595 9596  }
9596 9597  
9597 9598  /*
9598 9599   * (*tran_setcap).  Set the capability named to the value given.
9599 9600   */
9600 9601  static int
9601 9602  mptsas_scsi_setcap(struct scsi_address *ap, char *cap, int value, int tgtonly)
9602 9603  {
9603 9604          mptsas_t        *mpt = ADDR2MPT(ap);
9604 9605          int             ckey;
9605 9606          int             rval = FALSE;
9606 9607          mptsas_target_t *ptgt;
9607 9608  
9608 9609          NDBG24(("mptsas_scsi_setcap: target=%d, cap=%s value=%x tgtonly=%x",
9609 9610              ap->a_target, cap, value, tgtonly));
9610 9611  
9611 9612          if (!tgtonly) {
9612 9613                  return (rval);
9613 9614          }
9614 9615  
9615 9616          mutex_enter(&mpt->m_mutex);
9616 9617  
9617 9618          if ((mptsas_scsi_capchk(cap, tgtonly, &ckey)) != TRUE) {
9618 9619                  mutex_exit(&mpt->m_mutex);
9619 9620                  return (UNDEFINED);
9620 9621          }
9621 9622  
9622 9623          switch (ckey) {
9623 9624          case SCSI_CAP_DMA_MAX:
9624 9625          case SCSI_CAP_MSG_OUT:
9625 9626          case SCSI_CAP_PARITY:
9626 9627          case SCSI_CAP_INITIATOR_ID:
9627 9628          case SCSI_CAP_LINKED_CMDS:
9628 9629          case SCSI_CAP_UNTAGGED_QING:
9629 9630          case SCSI_CAP_RESET_NOTIFICATION:
9630 9631                  /*
9631 9632                   * None of these are settable via
9632 9633                   * the capability interface.
9633 9634                   */
9634 9635                  break;
9635 9636          case SCSI_CAP_ARQ:
9636 9637                  /*
9637 9638                   * We cannot turn off arq so return false if asked to
9638 9639                   */
9639 9640                  if (value) {
9640 9641                          rval = TRUE;
9641 9642                  } else {
9642 9643                          rval = FALSE;
9643 9644                  }
9644 9645                  break;
9645 9646          case SCSI_CAP_TAGGED_QING:
9646 9647                  ptgt = ((mptsas_tgt_private_t *)
9647 9648                      (ap->a_hba_tran->tran_tgt_private))->t_private;
9648 9649                  mutex_enter(&ptgt->m_tgt_intr_mutex);
9649 9650                  mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
9650 9651                  mutex_exit(&ptgt->m_tgt_intr_mutex);
9651 9652                  rval = TRUE;
9652 9653                  break;
9653 9654          case SCSI_CAP_QFULL_RETRIES:
9654 9655                  ((mptsas_tgt_private_t *)(ap->a_hba_tran->tran_tgt_private))->
9655 9656                      t_private->m_qfull_retries = (uchar_t)value;
9656 9657                  rval = TRUE;
9657 9658                  break;
9658 9659          case SCSI_CAP_QFULL_RETRY_INTERVAL:
9659 9660                  ((mptsas_tgt_private_t *)(ap->a_hba_tran->tran_tgt_private))->
9660 9661                      t_private->m_qfull_retry_interval =
9661 9662                      drv_usectohz(value * 1000);
9662 9663                  rval = TRUE;
9663 9664                  break;
9664 9665          default:
9665 9666                  rval = UNDEFINED;
9666 9667                  break;
9667 9668          }
9668 9669          mutex_exit(&mpt->m_mutex);
9669 9670          return (rval);
9670 9671  }
9671 9672  
9672 9673  /*
9673 9674   * Utility routine for mptsas_ifsetcap/ifgetcap
9674 9675   */
9675 9676  /*ARGSUSED*/
9676 9677  static int
9677 9678  mptsas_scsi_capchk(char *cap, int tgtonly, int *cidxp)
9678 9679  {
9679 9680          NDBG24(("mptsas_scsi_capchk: cap=%s", cap));
9680 9681  
9681 9682          if (!cap)
9682 9683                  return (FALSE);
9683 9684  
9684 9685          *cidxp = scsi_hba_lookup_capstr(cap);
9685 9686          return (TRUE);
9686 9687  }
9687 9688  
9688 9689  static int
9689 9690  mptsas_alloc_active_slots(mptsas_t *mpt, int flag)
9690 9691  {
9691 9692          mptsas_slots_t  *old_active = mpt->m_active;
9692 9693          mptsas_slots_t  *new_active;
9693 9694          size_t          size;
9694 9695          int             rval = -1, nslot, i;
9695 9696          mptsas_slot_free_e_t    *pe;
9696 9697  
9697 9698          if (mptsas_outstanding_cmds_n(mpt)) {
9698 9699                  NDBG9(("cannot change size of active slots array"));
9699 9700                  return (rval);
9700 9701          }
9701 9702  
9702 9703          size = MPTSAS_SLOTS_SIZE(mpt);
9703 9704          new_active = kmem_zalloc(size, flag);
9704 9705          if (new_active == NULL) {
9705 9706                  NDBG1(("new active alloc failed"));
9706 9707                  return (rval);
9707 9708          }
9708 9709          /*
9709 9710           * Since SMID 0 is reserved and the TM slot is reserved, the
9710 9711           * number of slots that can be used at any one time is
9711 9712           * m_max_requests - 2.
9712 9713           */
9713 9714          new_active->m_n_slots = nslot = (mpt->m_max_requests - 2);
9714 9715          new_active->m_size = size;
9715 9716          new_active->m_tags = 1;
9716 9717  
9717 9718          if (old_active) {
9718 9719                  new_active->m_tgttbl = old_active->m_tgttbl;
9719 9720                  new_active->m_smptbl = old_active->m_smptbl;
9720 9721                  new_active->m_num_raid_configs =
9721 9722                      old_active->m_num_raid_configs;
9722 9723                  for (i = 0; i < new_active->m_num_raid_configs; i++) {
9723 9724                          new_active->m_raidconfig[i] =
9724 9725                              old_active->m_raidconfig[i];
9725 9726                  }
9726 9727                  mptsas_free_active_slots(mpt);
9727 9728          }
9728 9729  
9729 9730          if (max_ncpus & (max_ncpus - 1)) {
9730 9731                  mpt->m_slot_freeq_pair_n = (1 << highbit(max_ncpus));
9731 9732          } else {
9732 9733                  mpt->m_slot_freeq_pair_n = max_ncpus;
9733 9734          }
9734 9735          mpt->m_slot_freeq_pairp = kmem_zalloc(
9735 9736              mpt->m_slot_freeq_pair_n *
9736 9737              sizeof (mptsas_slot_freeq_pair_t), KM_SLEEP);
9737 9738          for (i = 0; i < mpt->m_slot_freeq_pair_n; i++) {
9738 9739                  list_create(&mpt->m_slot_freeq_pairp[i].
9739 9740                      m_slot_allocq.s.m_fq_list,
9740 9741                      sizeof (mptsas_slot_free_e_t),
9741 9742                      offsetof(mptsas_slot_free_e_t, node));
9742 9743                  list_create(&mpt->m_slot_freeq_pairp[i].
9743 9744                      m_slot_releq.s.m_fq_list,
9744 9745                      sizeof (mptsas_slot_free_e_t),
9745 9746                      offsetof(mptsas_slot_free_e_t, node));
9746 9747                  mpt->m_slot_freeq_pairp[i].m_slot_allocq.s.m_fq_n = 0;
9747 9748                  mpt->m_slot_freeq_pairp[i].m_slot_releq.s.m_fq_n = 0;
9748 9749                  mutex_init(&mpt->m_slot_freeq_pairp[i].
9749 9750                      m_slot_allocq.s.m_fq_mutex, NULL, MUTEX_DRIVER,
9750 9751                      DDI_INTR_PRI(mpt->m_intr_pri));
9751 9752                  mutex_init(&mpt->m_slot_freeq_pairp[i].
9752 9753                      m_slot_releq.s.m_fq_mutex, NULL, MUTEX_DRIVER,
9753 9754                      DDI_INTR_PRI(mpt->m_intr_pri));
9754 9755          }
9755 9756          pe = mpt->m_slot_free_ae = kmem_zalloc(nslot *
9756 9757              sizeof (mptsas_slot_free_e_t), KM_SLEEP);
9757 9758          /*
9758 9759           * An array of Mpi2ReplyDescriptorsUnion_t is defined here.
9759 9760           * We are trying to eliminate the m_mutex in the context
9760 9761           * reply code path in the ISR. Since the read of the
9761 9762           * ReplyDescriptor and update/write of the ReplyIndex must
9762 9763           * be atomic (since the poll thread may also update them at
9763 9764           * the same time) so we first read out of the ReplyDescriptor
9764 9765           * into this array and update the ReplyIndex register with a
9765 9766           * separate mutex m_intr_mutex protected, and then release the
9766 9767           * mutex and process all of them. the length of the array is
9767 9768           * defined as max as 128(128*64=8k), which is
9768 9769           * assumed as the maxmium depth of the interrupt coalese.
9769 9770           */
9770 9771          mpt->m_reply = kmem_zalloc(MPI_ADDRESS_COALSCE_MAX *
9771 9772              sizeof (Mpi2ReplyDescriptorsUnion_t), KM_SLEEP);
9772 9773          for (i = 0; i < nslot; i++, pe++) {
9773 9774                  pe->slot = i + 1; /* SMID 0 is reserved */
9774 9775                  pe->cpuid = i % mpt->m_slot_freeq_pair_n;
9775 9776                  list_insert_tail(&mpt->m_slot_freeq_pairp
9776 9777                      [i % mpt->m_slot_freeq_pair_n]
9777 9778                      .m_slot_allocq.s.m_fq_list, pe);
9778 9779                  mpt->m_slot_freeq_pairp[i % mpt->m_slot_freeq_pair_n]
9779 9780                      .m_slot_allocq.s.m_fq_n++;
9780 9781                  mpt->m_slot_freeq_pairp[i % mpt->m_slot_freeq_pair_n]
9781 9782                      .m_slot_allocq.s.m_fq_n_init++;
9782 9783          }
9783 9784  
9784 9785          mpt->m_active = new_active;
9785 9786          rval = 0;
9786 9787  
9787 9788          return (rval);
9788 9789  }
9789 9790  
9790 9791  static void
9791 9792  mptsas_free_active_slots(mptsas_t *mpt)
9792 9793  {
9793 9794          mptsas_slots_t  *active = mpt->m_active;
9794 9795          size_t          size;
9795 9796          mptsas_slot_free_e_t    *pe;
9796 9797          int     i;
9797 9798  
9798 9799          if (active == NULL)
9799 9800                  return;
9800 9801  
9801 9802          if (mpt->m_slot_freeq_pairp) {
9802 9803                  for (i = 0; i < mpt->m_slot_freeq_pair_n; i++) {
9803 9804                          while ((pe = list_head(&mpt->m_slot_freeq_pairp
9804 9805                              [i].m_slot_allocq.s.m_fq_list)) != NULL) {
9805 9806                                  list_remove(&mpt->m_slot_freeq_pairp[i]
9806 9807                                      .m_slot_allocq.s.m_fq_list, pe);
9807 9808                          }
9808 9809                          list_destroy(&mpt->m_slot_freeq_pairp
9809 9810                              [i].m_slot_allocq.s.m_fq_list);
9810 9811                          while ((pe = list_head(&mpt->m_slot_freeq_pairp
9811 9812                              [i].m_slot_releq.s.m_fq_list)) != NULL) {
9812 9813                                  list_remove(&mpt->m_slot_freeq_pairp[i]
9813 9814                                      .m_slot_releq.s.m_fq_list, pe);
9814 9815                          }
9815 9816                          list_destroy(&mpt->m_slot_freeq_pairp
9816 9817                              [i].m_slot_releq.s.m_fq_list);
9817 9818                          mutex_destroy(&mpt->m_slot_freeq_pairp
9818 9819                              [i].m_slot_allocq.s.m_fq_mutex);
9819 9820                          mutex_destroy(&mpt->m_slot_freeq_pairp
9820 9821                              [i].m_slot_releq.s.m_fq_mutex);
9821 9822                  }
9822 9823                  kmem_free(mpt->m_slot_freeq_pairp, mpt->m_slot_freeq_pair_n *
9823 9824                      sizeof (mptsas_slot_freeq_pair_t));
9824 9825          }
9825 9826          if (mpt->m_slot_free_ae)
9826 9827                  kmem_free(mpt->m_slot_free_ae, mpt->m_active->m_n_slots *
9827 9828                      sizeof (mptsas_slot_free_e_t));
9828 9829  
9829 9830          if (mpt->m_reply)
9830 9831                  kmem_free(mpt->m_reply, MPI_ADDRESS_COALSCE_MAX *
9831 9832                      sizeof (Mpi2ReplyDescriptorsUnion_t));
9832 9833  
9833 9834          size = active->m_size;
9834 9835          kmem_free(active, size);
9835 9836          mpt->m_active = NULL;
9836 9837  }
9837 9838  
9838 9839  /*
9839 9840   * Error logging, printing, and debug print routines.
9840 9841   */
9841 9842  static char *mptsas_label = "mpt_sas";
9842 9843  
9843 9844  /*PRINTFLIKE3*/
9844 9845  void
9845 9846  mptsas_log(mptsas_t *mpt, int level, char *fmt, ...)
9846 9847  {
9847 9848          dev_info_t      *dev;
9848 9849          va_list         ap;
9849 9850  
9850 9851          if (mpt) {
9851 9852                  dev = mpt->m_dip;
9852 9853          } else {
9853 9854                  dev = 0;
9854 9855          }
9855 9856  
9856 9857          mutex_enter(&mptsas_log_mutex);
9857 9858  
9858 9859          va_start(ap, fmt);
9859 9860          (void) vsprintf(mptsas_log_buf, fmt, ap);
9860 9861          va_end(ap);
9861 9862  
9862 9863          if (level == CE_CONT) {
9863 9864                  scsi_log(dev, mptsas_label, level, "%s\n", mptsas_log_buf);
9864 9865          } else {
9865 9866                  scsi_log(dev, mptsas_label, level, "%s", mptsas_log_buf);
9866 9867          }
9867 9868  
9868 9869          mutex_exit(&mptsas_log_mutex);
9869 9870  }
9870 9871  
9871 9872  #ifdef MPTSAS_DEBUG
9872 9873  /*PRINTFLIKE1*/
9873 9874  void
9874 9875  mptsas_printf(char *fmt, ...)
9875 9876  {
9876 9877          dev_info_t      *dev = 0;
9877 9878          va_list         ap;
9878 9879  
9879 9880          mutex_enter(&mptsas_log_mutex);
9880 9881  
9881 9882          va_start(ap, fmt);
9882 9883          (void) vsprintf(mptsas_log_buf, fmt, ap);
9883 9884          va_end(ap);
9884 9885  
9885 9886  #ifdef PROM_PRINTF
9886 9887          prom_printf("%s:\t%s\n", mptsas_label, mptsas_log_buf);
9887 9888  #else
9888 9889          scsi_log(dev, mptsas_label, SCSI_DEBUG, "%s\n", mptsas_log_buf);
9889 9890  #endif
9890 9891          mutex_exit(&mptsas_log_mutex);
9891 9892  }
9892 9893  #endif
9893 9894  
9894 9895  /*
9895 9896   * timeout handling
9896 9897   */
9897 9898  static void
9898 9899  mptsas_watch(void *arg)
9899 9900  {
9900 9901  #ifndef __lock_lint
9901 9902          _NOTE(ARGUNUSED(arg))
9902 9903  #endif
9903 9904  
9904 9905          mptsas_t        *mpt;
9905 9906          uint32_t        doorbell;
9906 9907  
9907 9908          NDBG30(("mptsas_watch"));
9908 9909  
9909 9910          rw_enter(&mptsas_global_rwlock, RW_READER);
9910 9911          for (mpt = mptsas_head; mpt != (mptsas_t *)NULL; mpt = mpt->m_next) {
9911 9912  
9912 9913                  mutex_enter(&mpt->m_mutex);
9913 9914  
9914 9915                  /* Skip device if not powered on */
9915 9916                  if (mpt->m_options & MPTSAS_OPT_PM) {
9916 9917                          if (mpt->m_power_level == PM_LEVEL_D0) {
9917 9918                                  (void) pm_busy_component(mpt->m_dip, 0);
9918 9919                                  mpt->m_busy = 1;
9919 9920                          } else {
9920 9921                                  mutex_exit(&mpt->m_mutex);
9921 9922                                  continue;
9922 9923                          }
9923 9924                  }
9924 9925  
9925 9926                  /*
9926 9927                   * Check if controller is in a FAULT state. If so, reset it.
9927 9928                   */
9928 9929                  doorbell = ddi_get32(mpt->m_datap, &mpt->m_reg->Doorbell);
9929 9930                  if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
9930 9931                          doorbell &= MPI2_DOORBELL_DATA_MASK;
9931 9932                          mptsas_log(mpt, CE_WARN, "MPT Firmware Fault, "
9932 9933                              "code: %04x", doorbell);
9933 9934                          mpt->m_softstate &= ~MPTSAS_SS_MSG_UNIT_RESET;
9934 9935                          if ((mptsas_restart_ioc(mpt)) == DDI_FAILURE) {
9935 9936                                  mptsas_log(mpt, CE_WARN, "Reset failed"
9936 9937                                      "after fault was detected");
9937 9938                          }
9938 9939                  }
9939 9940  
9940 9941                  /*
9941 9942                   * For now, always call mptsas_watchsubr.
9942 9943                   */
9943 9944                  mptsas_watchsubr(mpt);
9944 9945  
9945 9946                  if (mpt->m_options & MPTSAS_OPT_PM) {
9946 9947                          mpt->m_busy = 0;
9947 9948                          (void) pm_idle_component(mpt->m_dip, 0);
9948 9949                  }
9949 9950  
9950 9951                  mutex_exit(&mpt->m_mutex);
9951 9952          }
9952 9953          rw_exit(&mptsas_global_rwlock);
9953 9954  
9954 9955          mutex_enter(&mptsas_global_mutex);
9955 9956          if (mptsas_timeouts_enabled)
9956 9957                  mptsas_timeout_id = timeout(mptsas_watch, NULL, mptsas_tick);
9957 9958          mutex_exit(&mptsas_global_mutex);
9958 9959  }
9959 9960  
9960 9961  static void
9961 9962  mptsas_watchsubr(mptsas_t *mpt)
9962 9963  {
9963 9964          int             i;
9964 9965          mptsas_cmd_t    *cmd;
9965 9966          mptsas_target_t *ptgt = NULL;
9966 9967  
9967 9968          NDBG30(("mptsas_watchsubr: mpt=0x%p", (void *)mpt));
9968 9969  
9969 9970  #ifdef MPTSAS_TEST
9970 9971          if (mptsas_enable_untagged) {
9971 9972                  mptsas_test_untagged++;
9972 9973          }
9973 9974  #endif
9974 9975  
9975 9976          /*
9976 9977           * Check for commands stuck in active slot
9977 9978           * Account for TM requests, which use the last SMID.
9978 9979           */
9979 9980          mutex_enter(&mpt->m_intr_mutex);
9980 9981          for (i = 0; i <= mpt->m_active->m_n_slots; i++) {
9981 9982                  if ((cmd = mpt->m_active->m_slot[i]) != NULL) {
9982 9983                          if ((cmd->cmd_flags & CFLAG_CMDIOC) == 0) {
9983 9984                                  cmd->cmd_active_timeout -=
9984 9985                                      mptsas_scsi_watchdog_tick;
9985 9986                                  if (cmd->cmd_active_timeout <= 0) {
9986 9987                                          /*
9987 9988                                           * There seems to be a command stuck
9988 9989                                           * in the active slot.  Drain throttle.
9989 9990                                           */
9990 9991                                          ptgt = cmd->cmd_tgt_addr;
9991 9992                                          mutex_enter(&ptgt->m_tgt_intr_mutex);
9992 9993                                          mptsas_set_throttle(mpt, ptgt,
9993 9994                                              DRAIN_THROTTLE);
9994 9995                                          mutex_exit(&ptgt->m_tgt_intr_mutex);
9995 9996                                  }
9996 9997                          }
9997 9998                          if ((cmd->cmd_flags & CFLAG_PASSTHRU) ||
9998 9999                              (cmd->cmd_flags & CFLAG_CONFIG) ||
9999 10000                              (cmd->cmd_flags & CFLAG_FW_DIAG)) {
10000 10001                                  cmd->cmd_active_timeout -=
10001 10002                                      mptsas_scsi_watchdog_tick;
10002 10003                                  if (cmd->cmd_active_timeout <= 0) {
10003 10004                                          /*
10004 10005                                           * passthrough command timeout
10005 10006                                           */
10006 10007                                          cmd->cmd_flags |= (CFLAG_FINISHED |
10007 10008                                              CFLAG_TIMEOUT);
10008 10009                                          cv_broadcast(&mpt->m_passthru_cv);
10009 10010                                          cv_broadcast(&mpt->m_config_cv);
10010 10011                                          cv_broadcast(&mpt->m_fw_diag_cv);
10011 10012                                  }
10012 10013                          }
10013 10014                  }
10014 10015          }
10015 10016          mutex_exit(&mpt->m_intr_mutex);
10016 10017  
10017 10018          ptgt = (mptsas_target_t *)mptsas_hash_traverse(&mpt->m_active->m_tgttbl,
10018 10019              MPTSAS_HASH_FIRST);
10019 10020          while (ptgt != NULL) {
10020 10021                  /*
10021 10022                   * In order to avoid using m_mutex in the key code path in ISR,
10022 10023                   * separate mutexs are introduced to protect those elements
10023 10024                   * shown in ISR.
10024 10025                   */
10025 10026                  mutex_enter(&ptgt->m_tgt_intr_mutex);
10026 10027  
10027 10028                  /*
10028 10029                   * If we were draining due to a qfull condition,
10029 10030                   * go back to full throttle.
10030 10031                   */
10031 10032                  if ((ptgt->m_t_throttle < MAX_THROTTLE) &&
10032 10033                      (ptgt->m_t_throttle > HOLD_THROTTLE) &&
10033 10034                      (ptgt->m_t_ncmds < ptgt->m_t_throttle)) {
10034 10035                          mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
10035 10036                          mptsas_restart_hba(mpt);
10036 10037                  }
10037 10038  
10038 10039                  if ((ptgt->m_t_ncmds > 0) &&
10039 10040                      (ptgt->m_timebase)) {
10040 10041  
10041 10042                          if (ptgt->m_timebase <=
10042 10043                              mptsas_scsi_watchdog_tick) {
10043 10044                                  ptgt->m_timebase +=
10044 10045                                      mptsas_scsi_watchdog_tick;
10045 10046                                  mutex_exit(&ptgt->m_tgt_intr_mutex);
10046 10047                                  ptgt = (mptsas_target_t *)mptsas_hash_traverse(
10047 10048                                      &mpt->m_active->m_tgttbl, MPTSAS_HASH_NEXT);
10048 10049                                  continue;
10049 10050                          }
10050 10051  
10051 10052                          ptgt->m_timeout -= mptsas_scsi_watchdog_tick;
10052 10053  
10053 10054                          if (ptgt->m_timeout < 0) {
10054 10055                                  mutex_exit(&ptgt->m_tgt_intr_mutex);
10055 10056                                  mptsas_cmd_timeout(mpt, ptgt->m_devhdl);
10056 10057                                  ptgt = (mptsas_target_t *)mptsas_hash_traverse(
10057 10058                                      &mpt->m_active->m_tgttbl, MPTSAS_HASH_NEXT);
10058 10059                                  continue;
10059 10060                          }
10060 10061  
10061 10062                          if ((ptgt->m_timeout) <=
10062 10063                              mptsas_scsi_watchdog_tick) {
10063 10064                                  NDBG23(("pending timeout"));
10064 10065                                  mptsas_set_throttle(mpt, ptgt,
10065 10066                                      DRAIN_THROTTLE);
10066 10067                          }
10067 10068                  }
10068 10069                  mutex_exit(&ptgt->m_tgt_intr_mutex);
10069 10070                  ptgt = (mptsas_target_t *)mptsas_hash_traverse(
10070 10071                      &mpt->m_active->m_tgttbl, MPTSAS_HASH_NEXT);
10071 10072          }
10072 10073  }
10073 10074  
10074 10075  /*
10075 10076   * timeout recovery
10076 10077   */
10077 10078  static void
10078 10079  mptsas_cmd_timeout(mptsas_t *mpt, uint16_t devhdl)
10079 10080  {
10080 10081  
10081 10082          NDBG29(("mptsas_cmd_timeout: target=%d", devhdl));
10082 10083          mptsas_log(mpt, CE_WARN, "Disconnected command timeout for "
10083 10084              "Target %d", devhdl);
10084 10085  
10085 10086          /*
10086 10087           * If the current target is not the target passed in,
10087 10088           * try to reset that target.
10088 10089           */
10089 10090          NDBG29(("mptsas_cmd_timeout: device reset"));
10090 10091          if (mptsas_do_scsi_reset(mpt, devhdl) != TRUE) {
10091 10092                  mptsas_log(mpt, CE_WARN, "Target %d reset for command timeout "
10092 10093                      "recovery failed!", devhdl);
10093 10094          }
10094 10095  }
10095 10096  
10096 10097  /*
10097 10098   * Device / Hotplug control
10098 10099   */
10099 10100  static int
10100 10101  mptsas_scsi_quiesce(dev_info_t *dip)
10101 10102  {
10102 10103          mptsas_t        *mpt;
10103 10104          scsi_hba_tran_t *tran;
10104 10105  
10105 10106          tran = ddi_get_driver_private(dip);
10106 10107          if (tran == NULL || (mpt = TRAN2MPT(tran)) == NULL)
10107 10108                  return (-1);
10108 10109  
10109 10110          return (mptsas_quiesce_bus(mpt));
10110 10111  }
10111 10112  
10112 10113  static int
10113 10114  mptsas_scsi_unquiesce(dev_info_t *dip)
10114 10115  {
10115 10116          mptsas_t                *mpt;
10116 10117          scsi_hba_tran_t *tran;
10117 10118  
10118 10119          tran = ddi_get_driver_private(dip);
10119 10120          if (tran == NULL || (mpt = TRAN2MPT(tran)) == NULL)
10120 10121                  return (-1);
10121 10122  
10122 10123          return (mptsas_unquiesce_bus(mpt));
10123 10124  }
10124 10125  
10125 10126  static int
10126 10127  mptsas_quiesce_bus(mptsas_t *mpt)
10127 10128  {
10128 10129          mptsas_target_t *ptgt = NULL;
10129 10130  
10130 10131          NDBG28(("mptsas_quiesce_bus"));
10131 10132          mutex_enter(&mpt->m_mutex);
10132 10133  
10133 10134          /* Set all the throttles to zero */
10134 10135          ptgt = (mptsas_target_t *)mptsas_hash_traverse(&mpt->m_active->m_tgttbl,
10135 10136              MPTSAS_HASH_FIRST);
10136 10137          while (ptgt != NULL) {
10137 10138                  mutex_enter(&ptgt->m_tgt_intr_mutex);
10138 10139                  mptsas_set_throttle(mpt, ptgt, HOLD_THROTTLE);
10139 10140                  mutex_exit(&ptgt->m_tgt_intr_mutex);
10140 10141  
10141 10142                  ptgt = (mptsas_target_t *)mptsas_hash_traverse(
10142 10143                      &mpt->m_active->m_tgttbl, MPTSAS_HASH_NEXT);
10143 10144          }
10144 10145  
10145 10146          /* If there are any outstanding commands in the queue */
10146 10147          mutex_enter(&mpt->m_intr_mutex);
10147 10148          if (mptsas_outstanding_cmds_n(mpt)) {
10148 10149                  mutex_exit(&mpt->m_intr_mutex);
10149 10150                  mpt->m_softstate |= MPTSAS_SS_DRAINING;
10150 10151                  mpt->m_quiesce_timeid = timeout(mptsas_ncmds_checkdrain,
10151 10152                      mpt, (MPTSAS_QUIESCE_TIMEOUT * drv_usectohz(1000000)));
10152 10153                  if (cv_wait_sig(&mpt->m_cv, &mpt->m_mutex) == 0) {
10153 10154                          /*
10154 10155                           * Quiesce has been interrupted
10155 10156                           */
10156 10157                          mpt->m_softstate &= ~MPTSAS_SS_DRAINING;
10157 10158                          ptgt = (mptsas_target_t *)mptsas_hash_traverse(
10158 10159                              &mpt->m_active->m_tgttbl, MPTSAS_HASH_FIRST);
10159 10160                          while (ptgt != NULL) {
10160 10161                                  mutex_enter(&ptgt->m_tgt_intr_mutex);
10161 10162                                  mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
10162 10163                                  mutex_exit(&ptgt->m_tgt_intr_mutex);
10163 10164  
10164 10165                                  ptgt = (mptsas_target_t *)mptsas_hash_traverse(
10165 10166                                      &mpt->m_active->m_tgttbl, MPTSAS_HASH_NEXT);
10166 10167                          }
10167 10168                          mptsas_restart_hba(mpt);
10168 10169                          if (mpt->m_quiesce_timeid != 0) {
10169 10170                                  timeout_id_t tid = mpt->m_quiesce_timeid;
10170 10171                                  mpt->m_quiesce_timeid = 0;
10171 10172                                  mutex_exit(&mpt->m_mutex);
10172 10173                                  (void) untimeout(tid);
10173 10174                                  return (-1);
10174 10175                          }
10175 10176                          mutex_exit(&mpt->m_mutex);
10176 10177                          return (-1);
10177 10178                  } else {
10178 10179                          /* Bus has been quiesced */
10179 10180                          ASSERT(mpt->m_quiesce_timeid == 0);
10180 10181                          mpt->m_softstate &= ~MPTSAS_SS_DRAINING;
10181 10182                          mpt->m_softstate |= MPTSAS_SS_QUIESCED;
10182 10183                          mutex_exit(&mpt->m_mutex);
10183 10184                          return (0);
10184 10185                  }
10185 10186          }
10186 10187          mutex_exit(&mpt->m_intr_mutex);
10187 10188          /* Bus was not busy - QUIESCED */
10188 10189          mutex_exit(&mpt->m_mutex);
10189 10190  
10190 10191          return (0);
10191 10192  }
10192 10193  
10193 10194  static int
10194 10195  mptsas_unquiesce_bus(mptsas_t *mpt)
10195 10196  {
10196 10197          mptsas_target_t *ptgt = NULL;
10197 10198  
10198 10199          NDBG28(("mptsas_unquiesce_bus"));
10199 10200          mutex_enter(&mpt->m_mutex);
10200 10201          mpt->m_softstate &= ~MPTSAS_SS_QUIESCED;
10201 10202          ptgt = (mptsas_target_t *)mptsas_hash_traverse(&mpt->m_active->m_tgttbl,
10202 10203              MPTSAS_HASH_FIRST);
10203 10204          while (ptgt != NULL) {
10204 10205                  mutex_enter(&ptgt->m_tgt_intr_mutex);
10205 10206                  mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
10206 10207                  mutex_exit(&ptgt->m_tgt_intr_mutex);
10207 10208  
10208 10209                  ptgt = (mptsas_target_t *)mptsas_hash_traverse(
10209 10210                      &mpt->m_active->m_tgttbl, MPTSAS_HASH_NEXT);
10210 10211          }
10211 10212          mptsas_restart_hba(mpt);
10212 10213          mutex_exit(&mpt->m_mutex);
10213 10214          return (0);
10214 10215  }
10215 10216  
10216 10217  static void
10217 10218  mptsas_ncmds_checkdrain(void *arg)
10218 10219  {
10219 10220          mptsas_t        *mpt = arg;
10220 10221          mptsas_target_t *ptgt = NULL;
10221 10222  
10222 10223          mutex_enter(&mpt->m_mutex);
10223 10224          if (mpt->m_softstate & MPTSAS_SS_DRAINING) {
10224 10225                  mpt->m_quiesce_timeid = 0;
10225 10226                  mutex_enter(&mpt->m_intr_mutex);
10226 10227                  if (mptsas_outstanding_cmds_n(mpt)) {
10227 10228                          mutex_exit(&mpt->m_intr_mutex);
10228 10229                          /*
10229 10230                           * The throttle may have been reset because
10230 10231                           * of a SCSI bus reset
10231 10232                           */
10232 10233                          ptgt = (mptsas_target_t *)mptsas_hash_traverse(
10233 10234                              &mpt->m_active->m_tgttbl, MPTSAS_HASH_FIRST);
10234 10235                          while (ptgt != NULL) {
10235 10236                                  mutex_enter(&ptgt->m_tgt_intr_mutex);
10236 10237                                  mptsas_set_throttle(mpt, ptgt, HOLD_THROTTLE);
10237 10238                                  mutex_exit(&ptgt->m_tgt_intr_mutex);
10238 10239  
10239 10240                                  ptgt = (mptsas_target_t *)mptsas_hash_traverse(
10240 10241                                      &mpt->m_active->m_tgttbl, MPTSAS_HASH_NEXT);
10241 10242                          }
10242 10243  
10243 10244                          mpt->m_quiesce_timeid = timeout(mptsas_ncmds_checkdrain,
10244 10245                              mpt, (MPTSAS_QUIESCE_TIMEOUT *
10245 10246                              drv_usectohz(1000000)));
10246 10247                  } else {
10247 10248                          mutex_exit(&mpt->m_intr_mutex);
10248 10249                          /* Command queue has been drained */
10249 10250                          cv_signal(&mpt->m_cv);
10250 10251                  }
10251 10252          }
10252 10253          mutex_exit(&mpt->m_mutex);
10253 10254  }
10254 10255  
10255 10256  /*ARGSUSED*/
10256 10257  static void
10257 10258  mptsas_dump_cmd(mptsas_t *mpt, mptsas_cmd_t *cmd)
10258 10259  {
10259 10260          int     i;
10260 10261          uint8_t *cp = (uchar_t *)cmd->cmd_pkt->pkt_cdbp;
10261 10262          char    buf[128];
10262 10263  
10263 10264          buf[0] = '\0';
10264 10265          NDBG25(("?Cmd (0x%p) dump for Target %d Lun %d:\n", (void *)cmd,
10265 10266              Tgt(cmd), Lun(cmd)));
10266 10267          (void) sprintf(&buf[0], "\tcdb=[");
10267 10268          for (i = 0; i < (int)cmd->cmd_cdblen; i++) {
10268 10269                  (void) sprintf(&buf[strlen(buf)], " 0x%x", *cp++);
10269 10270          }
10270 10271          (void) sprintf(&buf[strlen(buf)], " ]");
10271 10272          NDBG25(("?%s\n", buf));
10272 10273          NDBG25(("?pkt_flags=0x%x pkt_statistics=0x%x pkt_state=0x%x\n",
10273 10274              cmd->cmd_pkt->pkt_flags, cmd->cmd_pkt->pkt_statistics,
10274 10275              cmd->cmd_pkt->pkt_state));
10275 10276          NDBG25(("?pkt_scbp=0x%x cmd_flags=0x%x\n", cmd->cmd_pkt->pkt_scbp ?
10276 10277              *(cmd->cmd_pkt->pkt_scbp) : 0, cmd->cmd_flags));
10277 10278  }
10278 10279  
10279 10280  static void
10280 10281  mptsas_start_passthru(mptsas_t *mpt, mptsas_cmd_t *cmd)
10281 10282  {
10282 10283          caddr_t                 memp;
10283 10284          pMPI2RequestHeader_t    request_hdrp;
10284 10285          struct scsi_pkt         *pkt = cmd->cmd_pkt;
10285 10286          mptsas_pt_request_t     *pt = pkt->pkt_ha_private;
10286 10287          uint32_t                request_size, data_size, dataout_size;
10287 10288          uint32_t                direction;
10288 10289          ddi_dma_cookie_t        data_cookie;
10289 10290          ddi_dma_cookie_t        dataout_cookie;
10290 10291          uint32_t                request_desc_low, request_desc_high = 0;
10291 10292          uint32_t                i, sense_bufp;
10292 10293          uint8_t                 desc_type;
10293 10294          uint8_t                 *request, function;
10294 10295          ddi_dma_handle_t        dma_hdl = mpt->m_dma_req_frame_hdl;
10295 10296          ddi_acc_handle_t        acc_hdl = mpt->m_acc_req_frame_hdl;
10296 10297  
10297 10298          desc_type = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
10298 10299  
10299 10300          request = pt->request;
10300 10301          direction = pt->direction;
10301 10302          request_size = pt->request_size;
10302 10303          data_size = pt->data_size;
10303 10304          dataout_size = pt->dataout_size;
10304 10305          data_cookie = pt->data_cookie;
10305 10306          dataout_cookie = pt->dataout_cookie;
10306 10307  
10307 10308          /*
10308 10309           * Store the passthrough message in memory location
10309 10310           * corresponding to our slot number
10310 10311           */
10311 10312          memp = mpt->m_req_frame + (mpt->m_req_frame_size * cmd->cmd_slot);
10312 10313          request_hdrp = (pMPI2RequestHeader_t)memp;
10313 10314          bzero(memp, mpt->m_req_frame_size);
10314 10315  
10315 10316          for (i = 0; i < request_size; i++) {
10316 10317                  bcopy(request + i, memp + i, 1);
10317 10318          }
10318 10319  
10319 10320          if (data_size || dataout_size) {
10320 10321                  pMpi2SGESimple64_t      sgep;
10321 10322                  uint32_t                sge_flags;
10322 10323  
10323 10324                  sgep = (pMpi2SGESimple64_t)((uint8_t *)request_hdrp +
10324 10325                      request_size);
10325 10326                  if (dataout_size) {
10326 10327  
10327 10328                          sge_flags = dataout_size |
10328 10329                              ((uint32_t)(MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
10329 10330                              MPI2_SGE_FLAGS_END_OF_BUFFER |
10330 10331                              MPI2_SGE_FLAGS_HOST_TO_IOC |
10331 10332                              MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
10332 10333                              MPI2_SGE_FLAGS_SHIFT);
10333 10334                          ddi_put32(acc_hdl, &sgep->FlagsLength, sge_flags);
10334 10335                          ddi_put32(acc_hdl, &sgep->Address.Low,
10335 10336                              (uint32_t)(dataout_cookie.dmac_laddress &
10336 10337                              0xffffffffull));
10337 10338                          ddi_put32(acc_hdl, &sgep->Address.High,
10338 10339                              (uint32_t)(dataout_cookie.dmac_laddress
10339 10340                              >> 32));
10340 10341                          sgep++;
10341 10342                  }
10342 10343                  sge_flags = data_size;
10343 10344                  sge_flags |= ((uint32_t)(MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
10344 10345                      MPI2_SGE_FLAGS_LAST_ELEMENT |
10345 10346                      MPI2_SGE_FLAGS_END_OF_BUFFER |
10346 10347                      MPI2_SGE_FLAGS_END_OF_LIST |
10347 10348                      MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
10348 10349                      MPI2_SGE_FLAGS_SHIFT);
10349 10350                  if (direction == MPTSAS_PASS_THRU_DIRECTION_WRITE) {
10350 10351                          sge_flags |= ((uint32_t)(MPI2_SGE_FLAGS_HOST_TO_IOC) <<
10351 10352                              MPI2_SGE_FLAGS_SHIFT);
10352 10353                  } else {
10353 10354                          sge_flags |= ((uint32_t)(MPI2_SGE_FLAGS_IOC_TO_HOST) <<
10354 10355                              MPI2_SGE_FLAGS_SHIFT);
10355 10356                  }
10356 10357                  ddi_put32(acc_hdl, &sgep->FlagsLength,
10357 10358                      sge_flags);
10358 10359                  ddi_put32(acc_hdl, &sgep->Address.Low,
10359 10360                      (uint32_t)(data_cookie.dmac_laddress &
10360 10361                      0xffffffffull));
10361 10362                  ddi_put32(acc_hdl, &sgep->Address.High,
10362 10363                      (uint32_t)(data_cookie.dmac_laddress >> 32));
10363 10364          }
10364 10365  
10365 10366          function = request_hdrp->Function;
10366 10367          if ((function == MPI2_FUNCTION_SCSI_IO_REQUEST) ||
10367 10368              (function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
10368 10369                  pMpi2SCSIIORequest_t    scsi_io_req;
10369 10370  
10370 10371                  scsi_io_req = (pMpi2SCSIIORequest_t)request_hdrp;
10371 10372                  /*
10372 10373                   * Put SGE for data and data_out buffer at the end of
10373 10374                   * scsi_io_request message header.(64 bytes in total)
10374 10375                   * Following above SGEs, the residual space will be
10375 10376                   * used by sense data.
10376 10377                   */
10377 10378                  ddi_put8(acc_hdl,
10378 10379                      &scsi_io_req->SenseBufferLength,
10379 10380                      (uint8_t)(request_size - 64));
10380 10381  
10381 10382                  sense_bufp = mpt->m_req_frame_dma_addr +
10382 10383                      (mpt->m_req_frame_size * cmd->cmd_slot);
10383 10384                  sense_bufp += 64;
10384 10385                  ddi_put32(acc_hdl,
10385 10386                      &scsi_io_req->SenseBufferLowAddress, sense_bufp);
10386 10387  
10387 10388                  /*
10388 10389                   * Set SGLOffset0 value
10389 10390                   */
10390 10391                  ddi_put8(acc_hdl, &scsi_io_req->SGLOffset0,
10391 10392                      offsetof(MPI2_SCSI_IO_REQUEST, SGL) / 4);
10392 10393  
10393 10394                  /*
10394 10395                   * Setup descriptor info.  RAID passthrough must use the
10395 10396                   * default request descriptor which is already set, so if this
10396 10397                   * is a SCSI IO request, change the descriptor to SCSI IO.
10397 10398                   */
10398 10399                  if (function == MPI2_FUNCTION_SCSI_IO_REQUEST) {
10399 10400                          desc_type = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
10400 10401                          request_desc_high = (ddi_get16(acc_hdl,
10401 10402                              &scsi_io_req->DevHandle) << 16);
10402 10403                  }
10403 10404          }
10404 10405  
10405 10406          /*
10406 10407           * We must wait till the message has been completed before
10407 10408           * beginning the next message so we wait for this one to
10408 10409           * finish.
10409 10410           */
10410 10411          (void) ddi_dma_sync(dma_hdl, 0, 0, DDI_DMA_SYNC_FORDEV);
10411 10412          request_desc_low = (cmd->cmd_slot << 16) + desc_type;
10412 10413          cmd->cmd_rfm = NULL;
10413 10414          mpt->m_active->m_slot[cmd->cmd_slot] = cmd;
10414 10415          MPTSAS_START_CMD(mpt, request_desc_low, request_desc_high);
10415 10416          if ((mptsas_check_dma_handle(dma_hdl) != DDI_SUCCESS) ||
10416 10417              (mptsas_check_acc_handle(acc_hdl) != DDI_SUCCESS)) {
10417 10418                  ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
10418 10419          }
10419 10420  }
10420 10421  
10421 10422  
10422 10423  
10423 10424  static int
10424 10425  mptsas_do_passthru(mptsas_t *mpt, uint8_t *request, uint8_t *reply,
10425 10426      uint8_t *data, uint32_t request_size, uint32_t reply_size,
10426 10427      uint32_t data_size, uint32_t direction, uint8_t *dataout,
10427 10428      uint32_t dataout_size, short timeout, int mode)
10428 10429  {
10429 10430          mptsas_pt_request_t             pt;
10430 10431          mptsas_dma_alloc_state_t        data_dma_state;
10431 10432          mptsas_dma_alloc_state_t        dataout_dma_state;
10432 10433          caddr_t                         memp;
10433 10434          mptsas_cmd_t                    *cmd = NULL;
10434 10435          struct scsi_pkt                 *pkt;
10435 10436          uint32_t                        reply_len = 0, sense_len = 0;
10436 10437          pMPI2RequestHeader_t            request_hdrp;
10437 10438          pMPI2RequestHeader_t            request_msg;
10438 10439          pMPI2DefaultReply_t             reply_msg;
10439 10440          Mpi2SCSIIOReply_t               rep_msg;
10440 10441          int                             i, status = 0, pt_flags = 0, rv = 0;
10441 10442          int                             rvalue;
10442 10443          uint8_t                         function;
10443 10444  
10444 10445          ASSERT(mutex_owned(&mpt->m_mutex));
10445 10446  
10446 10447          reply_msg = (pMPI2DefaultReply_t)(&rep_msg);
10447 10448          bzero(reply_msg, sizeof (MPI2_DEFAULT_REPLY));
10448 10449          request_msg = kmem_zalloc(request_size, KM_SLEEP);
10449 10450  
10450 10451          mutex_exit(&mpt->m_mutex);
10451 10452          /*
10452 10453           * copy in the request buffer since it could be used by
10453 10454           * another thread when the pt request into waitq
10454 10455           */
10455 10456          if (ddi_copyin(request, request_msg, request_size, mode)) {
10456 10457                  mutex_enter(&mpt->m_mutex);
10457 10458                  status = EFAULT;
10458 10459                  mptsas_log(mpt, CE_WARN, "failed to copy request data");
10459 10460                  goto out;
10460 10461          }
10461 10462          mutex_enter(&mpt->m_mutex);
10462 10463  
10463 10464          function = request_msg->Function;
10464 10465          if (function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
10465 10466                  pMpi2SCSITaskManagementRequest_t        task;
10466 10467                  task = (pMpi2SCSITaskManagementRequest_t)request_msg;
10467 10468                  mptsas_setup_bus_reset_delay(mpt);
10468 10469                  rv = mptsas_ioc_task_management(mpt, task->TaskType,
10469 10470                      task->DevHandle, (int)task->LUN[1], reply, reply_size,
10470 10471                      mode);
10471 10472  
10472 10473                  if (rv != TRUE) {
10473 10474                          status = EIO;
10474 10475                          mptsas_log(mpt, CE_WARN, "task management failed");
10475 10476                  }
10476 10477                  goto out;
10477 10478          }
10478 10479  
10479 10480          if (data_size != 0) {
10480 10481                  data_dma_state.size = data_size;
10481 10482                  if (mptsas_dma_alloc(mpt, &data_dma_state) != DDI_SUCCESS) {
10482 10483                          status = ENOMEM;
10483 10484                          mptsas_log(mpt, CE_WARN, "failed to alloc DMA "
10484 10485                              "resource");
10485 10486                          goto out;
10486 10487                  }
10487 10488                  pt_flags |= MPTSAS_DATA_ALLOCATED;
10488 10489                  if (direction == MPTSAS_PASS_THRU_DIRECTION_WRITE) {
10489 10490                          mutex_exit(&mpt->m_mutex);
10490 10491                          for (i = 0; i < data_size; i++) {
10491 10492                                  if (ddi_copyin(data + i, (uint8_t *)
10492 10493                                      data_dma_state.memp + i, 1, mode)) {
10493 10494                                          mutex_enter(&mpt->m_mutex);
10494 10495                                          status = EFAULT;
10495 10496                                          mptsas_log(mpt, CE_WARN, "failed to "
10496 10497                                              "copy read data");
10497 10498                                          goto out;
10498 10499                                  }
10499 10500                          }
10500 10501                          mutex_enter(&mpt->m_mutex);
10501 10502                  }
10502 10503          }
10503 10504  
10504 10505          if (dataout_size != 0) {
10505 10506                  dataout_dma_state.size = dataout_size;
10506 10507                  if (mptsas_dma_alloc(mpt, &dataout_dma_state) != DDI_SUCCESS) {
10507 10508                          status = ENOMEM;
10508 10509                          mptsas_log(mpt, CE_WARN, "failed to alloc DMA "
10509 10510                              "resource");
10510 10511                          goto out;
10511 10512                  }
10512 10513                  pt_flags |= MPTSAS_DATAOUT_ALLOCATED;
10513 10514                  mutex_exit(&mpt->m_mutex);
10514 10515                  for (i = 0; i < dataout_size; i++) {
10515 10516                          if (ddi_copyin(dataout + i, (uint8_t *)
10516 10517                              dataout_dma_state.memp + i, 1, mode)) {
10517 10518                                  mutex_enter(&mpt->m_mutex);
10518 10519                                  mptsas_log(mpt, CE_WARN, "failed to copy out"
10519 10520                                      " data");
10520 10521                                  status = EFAULT;
10521 10522                                  goto out;
10522 10523                          }
10523 10524                  }
10524 10525                  mutex_enter(&mpt->m_mutex);
10525 10526          }
10526 10527  
10527 10528          if ((rvalue = (mptsas_request_from_pool(mpt, &cmd, &pkt))) == -1) {
10528 10529                  status = EAGAIN;
10529 10530                  mptsas_log(mpt, CE_NOTE, "event ack command pool is full");
10530 10531                  goto out;
10531 10532          }
10532 10533          pt_flags |= MPTSAS_REQUEST_POOL_CMD;
10533 10534  
10534 10535          bzero((caddr_t)cmd, sizeof (*cmd));
10535 10536          bzero((caddr_t)pkt, scsi_pkt_size());
10536 10537          bzero((caddr_t)&pt, sizeof (pt));
10537 10538  
10538 10539          cmd->ioc_cmd_slot = (uint32_t)(rvalue);
10539 10540  
10540 10541          pt.request = (uint8_t *)request_msg;
10541 10542          pt.direction = direction;
10542 10543          pt.request_size = request_size;
10543 10544          pt.data_size = data_size;
10544 10545          pt.dataout_size = dataout_size;
10545 10546          pt.data_cookie = data_dma_state.cookie;
10546 10547          pt.dataout_cookie = dataout_dma_state.cookie;
10547 10548  
10548 10549          /*
10549 10550           * Form a blank cmd/pkt to store the acknowledgement message
10550 10551           */
10551 10552          pkt->pkt_cdbp           = (opaque_t)&cmd->cmd_cdb[0];
10552 10553          pkt->pkt_scbp           = (opaque_t)&cmd->cmd_scb;
10553 10554          pkt->pkt_ha_private     = (opaque_t)&pt;
10554 10555          pkt->pkt_flags          = FLAG_HEAD;
10555 10556          pkt->pkt_time           = timeout;
10556 10557          cmd->cmd_pkt            = pkt;
10557 10558          cmd->cmd_flags          = CFLAG_CMDIOC | CFLAG_PASSTHRU;
10558 10559  
10559 10560          /*
10560 10561           * Save the command in a slot
10561 10562           */
10562 10563          if (mptsas_save_cmd(mpt, cmd) == TRUE) {
10563 10564                  /*
10564 10565                   * Once passthru command get slot, set cmd_flags
10565 10566                   * CFLAG_PREPARED.
10566 10567                   */
10567 10568                  cmd->cmd_flags |= CFLAG_PREPARED;
10568 10569                  mptsas_start_passthru(mpt, cmd);
10569 10570          } else {
10570 10571                  mptsas_waitq_add(mpt, cmd);
10571 10572          }
10572 10573  
10573 10574          while ((cmd->cmd_flags & CFLAG_FINISHED) == 0) {
10574 10575                  cv_wait(&mpt->m_passthru_cv, &mpt->m_mutex);
10575 10576          }
10576 10577  
10577 10578          if (cmd->cmd_flags & CFLAG_PREPARED) {
10578 10579                  memp = mpt->m_req_frame + (mpt->m_req_frame_size *
10579 10580                      cmd->cmd_slot);
10580 10581                  request_hdrp = (pMPI2RequestHeader_t)memp;
10581 10582          }
10582 10583  
10583 10584          if (cmd->cmd_flags & CFLAG_TIMEOUT) {
10584 10585                  status = ETIMEDOUT;
10585 10586                  mptsas_log(mpt, CE_WARN, "passthrough command timeout");
10586 10587                  pt_flags |= MPTSAS_CMD_TIMEOUT;
10587 10588                  goto out;
10588 10589          }
10589 10590  
10590 10591          if (cmd->cmd_rfm) {
10591 10592                  /*
10592 10593                   * cmd_rfm is zero means the command reply is a CONTEXT
10593 10594                   * reply and no PCI Write to post the free reply SMFA
10594 10595                   * because no reply message frame is used.
10595 10596                   * cmd_rfm is non-zero means the reply is a ADDRESS
10596 10597                   * reply and reply message frame is used.
10597 10598                   */
10598 10599                  pt_flags |= MPTSAS_ADDRESS_REPLY;
10599 10600                  (void) ddi_dma_sync(mpt->m_dma_reply_frame_hdl, 0, 0,
10600 10601                      DDI_DMA_SYNC_FORCPU);
10601 10602                  reply_msg = (pMPI2DefaultReply_t)
10602 10603                      (mpt->m_reply_frame + (cmd->cmd_rfm -
10603 10604                      mpt->m_reply_frame_dma_addr));
10604 10605          }
10605 10606  
10606 10607          mptsas_fma_check(mpt, cmd);
10607 10608          if (pkt->pkt_reason == CMD_TRAN_ERR) {
10608 10609                  status = EAGAIN;
10609 10610                  mptsas_log(mpt, CE_WARN, "passthru fma error");
10610 10611                  goto out;
10611 10612          }
10612 10613          if (pkt->pkt_reason == CMD_RESET) {
10613 10614                  status = EAGAIN;
10614 10615                  mptsas_log(mpt, CE_WARN, "ioc reset abort passthru");
10615 10616                  goto out;
10616 10617          }
10617 10618  
10618 10619          if (pkt->pkt_reason == CMD_INCOMPLETE) {
10619 10620                  status = EIO;
10620 10621                  mptsas_log(mpt, CE_WARN, "passthrough command incomplete");
10621 10622                  goto out;
10622 10623          }
10623 10624  
10624 10625          mutex_exit(&mpt->m_mutex);
10625 10626          if (cmd->cmd_flags & CFLAG_PREPARED) {
10626 10627                  function = request_hdrp->Function;
10627 10628                  if ((function == MPI2_FUNCTION_SCSI_IO_REQUEST) ||
10628 10629                      (function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
10629 10630                          reply_len = sizeof (MPI2_SCSI_IO_REPLY);
10630 10631                          sense_len = reply_size - reply_len;
10631 10632                  } else {
10632 10633                          reply_len = reply_size;
10633 10634                          sense_len = 0;
10634 10635                  }
10635 10636  
10636 10637                  for (i = 0; i < reply_len; i++) {
10637 10638                          if (ddi_copyout((uint8_t *)reply_msg + i, reply + i, 1,
10638 10639                              mode)) {
10639 10640                                  mutex_enter(&mpt->m_mutex);
10640 10641                                  status = EFAULT;
10641 10642                                  mptsas_log(mpt, CE_WARN, "failed to copy out "
10642 10643                                      "reply data");
10643 10644                                  goto out;
10644 10645                          }
10645 10646                  }
10646 10647                  for (i = 0; i < sense_len; i++) {
10647 10648                          if (ddi_copyout((uint8_t *)request_hdrp + 64 + i,
10648 10649                              reply + reply_len + i, 1, mode)) {
10649 10650                                  mutex_enter(&mpt->m_mutex);
10650 10651                                  status = EFAULT;
10651 10652                                  mptsas_log(mpt, CE_WARN, "failed to copy out "
10652 10653                                      "sense data");
10653 10654                                  goto out;
10654 10655                          }
10655 10656                  }
10656 10657          }
10657 10658  
10658 10659          if (data_size) {
10659 10660                  if (direction != MPTSAS_PASS_THRU_DIRECTION_WRITE) {
10660 10661                          (void) ddi_dma_sync(data_dma_state.handle, 0, 0,
10661 10662                              DDI_DMA_SYNC_FORCPU);
10662 10663                          for (i = 0; i < data_size; i++) {
10663 10664                                  if (ddi_copyout((uint8_t *)(
10664 10665                                      data_dma_state.memp + i), data + i,  1,
10665 10666                                      mode)) {
10666 10667                                          mutex_enter(&mpt->m_mutex);
10667 10668                                          status = EFAULT;
10668 10669                                          mptsas_log(mpt, CE_WARN, "failed to "
10669 10670                                              "copy out the reply data");
10670 10671                                          goto out;
10671 10672                                  }
10672 10673                          }
10673 10674                  }
10674 10675          }
10675 10676          mutex_enter(&mpt->m_mutex);
10676 10677  out:
10677 10678          /*
10678 10679           * Put the reply frame back on the free queue, increment the free
10679 10680           * index, and write the new index to the free index register.  But only
10680 10681           * if this reply is an ADDRESS reply.
10681 10682           */
10682 10683          if (pt_flags & MPTSAS_ADDRESS_REPLY) {
10683 10684                  ddi_put32(mpt->m_acc_free_queue_hdl,
10684 10685                      &((uint32_t *)(void *)mpt->m_free_queue)[mpt->m_free_index],
10685 10686                      cmd->cmd_rfm);
10686 10687                  (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
10687 10688                      DDI_DMA_SYNC_FORDEV);
10688 10689                  if (++mpt->m_free_index == mpt->m_free_queue_depth) {
10689 10690                          mpt->m_free_index = 0;
10690 10691                  }
10691 10692                  ddi_put32(mpt->m_datap, &mpt->m_reg->ReplyFreeHostIndex,
10692 10693                      mpt->m_free_index);
10693 10694          }
10694 10695          if (cmd && (cmd->cmd_flags & CFLAG_PREPARED)) {
10695 10696                  mptsas_remove_cmd(mpt, cmd);
10696 10697                  pt_flags &= (~MPTSAS_REQUEST_POOL_CMD);
10697 10698          }
10698 10699          if (pt_flags & MPTSAS_REQUEST_POOL_CMD)
10699 10700                  mptsas_return_to_pool(mpt, cmd);
10700 10701          if (pt_flags & MPTSAS_DATA_ALLOCATED) {
10701 10702                  if (mptsas_check_dma_handle(data_dma_state.handle) !=
10702 10703                      DDI_SUCCESS) {
10703 10704                          ddi_fm_service_impact(mpt->m_dip,
10704 10705                              DDI_SERVICE_UNAFFECTED);
10705 10706                          status = EFAULT;
10706 10707                  }
10707 10708                  mptsas_dma_free(&data_dma_state);
10708 10709          }
10709 10710          if (pt_flags & MPTSAS_DATAOUT_ALLOCATED) {
10710 10711                  if (mptsas_check_dma_handle(dataout_dma_state.handle) !=
10711 10712                      DDI_SUCCESS) {
10712 10713                          ddi_fm_service_impact(mpt->m_dip,
10713 10714                              DDI_SERVICE_UNAFFECTED);
10714 10715                          status = EFAULT;
10715 10716                  }
10716 10717                  mptsas_dma_free(&dataout_dma_state);
10717 10718          }
10718 10719          if (pt_flags & MPTSAS_CMD_TIMEOUT) {
10719 10720                  if ((mptsas_restart_ioc(mpt)) == DDI_FAILURE) {
10720 10721                          mptsas_log(mpt, CE_WARN, "mptsas_restart_ioc failed");
10721 10722                  }
10722 10723          }
10723 10724          if (request_msg)
10724 10725                  kmem_free(request_msg, request_size);
10725 10726  
10726 10727          return (status);
10727 10728  }
10728 10729  
10729 10730  static int
10730 10731  mptsas_pass_thru(mptsas_t *mpt, mptsas_pass_thru_t *data, int mode)
10731 10732  {
10732 10733          /*
10733 10734           * If timeout is 0, set timeout to default of 60 seconds.
10734 10735           */
10735 10736          if (data->Timeout == 0) {
10736 10737                  data->Timeout = MPTSAS_PASS_THRU_TIME_DEFAULT;
10737 10738          }
10738 10739  
10739 10740          if (((data->DataSize == 0) &&
10740 10741              (data->DataDirection == MPTSAS_PASS_THRU_DIRECTION_NONE)) ||
10741 10742              ((data->DataSize != 0) &&
10742 10743              ((data->DataDirection == MPTSAS_PASS_THRU_DIRECTION_READ) ||
10743 10744              (data->DataDirection == MPTSAS_PASS_THRU_DIRECTION_WRITE) ||
10744 10745              ((data->DataDirection == MPTSAS_PASS_THRU_DIRECTION_BOTH) &&
10745 10746              (data->DataOutSize != 0))))) {
10746 10747                  if (data->DataDirection == MPTSAS_PASS_THRU_DIRECTION_BOTH) {
10747 10748                          data->DataDirection = MPTSAS_PASS_THRU_DIRECTION_READ;
10748 10749                  } else {
10749 10750                          data->DataOutSize = 0;
10750 10751                  }
10751 10752                  /*
10752 10753                   * Send passthru request messages
10753 10754                   */
10754 10755                  return (mptsas_do_passthru(mpt,
10755 10756                      (uint8_t *)((uintptr_t)data->PtrRequest),
10756 10757                      (uint8_t *)((uintptr_t)data->PtrReply),
10757 10758                      (uint8_t *)((uintptr_t)data->PtrData),
10758 10759                      data->RequestSize, data->ReplySize,
10759 10760                      data->DataSize, data->DataDirection,
10760 10761                      (uint8_t *)((uintptr_t)data->PtrDataOut),
10761 10762                      data->DataOutSize, data->Timeout, mode));
10762 10763          } else {
10763 10764                  return (EINVAL);
10764 10765          }
10765 10766  }
10766 10767  
10767 10768  static uint8_t
10768 10769  mptsas_get_fw_diag_buffer_number(mptsas_t *mpt, uint32_t unique_id)
10769 10770  {
10770 10771          uint8_t index;
10771 10772  
10772 10773          for (index = 0; index < MPI2_DIAG_BUF_TYPE_COUNT; index++) {
10773 10774                  if (mpt->m_fw_diag_buffer_list[index].unique_id == unique_id) {
10774 10775                          return (index);
10775 10776                  }
10776 10777          }
10777 10778  
10778 10779          return (MPTSAS_FW_DIAGNOSTIC_UID_NOT_FOUND);
10779 10780  }
10780 10781  
10781 10782  static void
10782 10783  mptsas_start_diag(mptsas_t *mpt, mptsas_cmd_t *cmd)
10783 10784  {
10784 10785          pMpi2DiagBufferPostRequest_t    pDiag_post_msg;
10785 10786          pMpi2DiagReleaseRequest_t       pDiag_release_msg;
10786 10787          struct scsi_pkt                 *pkt = cmd->cmd_pkt;
10787 10788          mptsas_diag_request_t           *diag = pkt->pkt_ha_private;
10788 10789          uint32_t                        request_desc_low, i;
10789 10790  
10790 10791          ASSERT(mutex_owned(&mpt->m_mutex));
10791 10792  
10792 10793          /*
10793 10794           * Form the diag message depending on the post or release function.
10794 10795           */
10795 10796          if (diag->function == MPI2_FUNCTION_DIAG_BUFFER_POST) {
10796 10797                  pDiag_post_msg = (pMpi2DiagBufferPostRequest_t)
10797 10798                      (mpt->m_req_frame + (mpt->m_req_frame_size *
10798 10799                      cmd->cmd_slot));
10799 10800                  bzero(pDiag_post_msg, mpt->m_req_frame_size);
10800 10801                  ddi_put8(mpt->m_acc_req_frame_hdl, &pDiag_post_msg->Function,
10801 10802                      diag->function);
10802 10803                  ddi_put8(mpt->m_acc_req_frame_hdl, &pDiag_post_msg->BufferType,
10803 10804                      diag->pBuffer->buffer_type);
10804 10805                  ddi_put8(mpt->m_acc_req_frame_hdl,
10805 10806                      &pDiag_post_msg->ExtendedType,
10806 10807                      diag->pBuffer->extended_type);
10807 10808                  ddi_put32(mpt->m_acc_req_frame_hdl,
10808 10809                      &pDiag_post_msg->BufferLength,
10809 10810                      diag->pBuffer->buffer_data.size);
10810 10811                  for (i = 0; i < (sizeof (pDiag_post_msg->ProductSpecific) / 4);
10811 10812                      i++) {
10812 10813                          ddi_put32(mpt->m_acc_req_frame_hdl,
10813 10814                              &pDiag_post_msg->ProductSpecific[i],
10814 10815                              diag->pBuffer->product_specific[i]);
10815 10816                  }
10816 10817                  ddi_put32(mpt->m_acc_req_frame_hdl,
10817 10818                      &pDiag_post_msg->BufferAddress.Low,
10818 10819                      (uint32_t)(diag->pBuffer->buffer_data.cookie.dmac_laddress
10819 10820                      & 0xffffffffull));
10820 10821                  ddi_put32(mpt->m_acc_req_frame_hdl,
10821 10822                      &pDiag_post_msg->BufferAddress.High,
10822 10823                      (uint32_t)(diag->pBuffer->buffer_data.cookie.dmac_laddress
10823 10824                      >> 32));
10824 10825          } else {
10825 10826                  pDiag_release_msg = (pMpi2DiagReleaseRequest_t)
10826 10827                      (mpt->m_req_frame + (mpt->m_req_frame_size *
10827 10828                      cmd->cmd_slot));
10828 10829                  bzero(pDiag_release_msg, mpt->m_req_frame_size);
10829 10830                  ddi_put8(mpt->m_acc_req_frame_hdl,
10830 10831                      &pDiag_release_msg->Function, diag->function);
10831 10832                  ddi_put8(mpt->m_acc_req_frame_hdl,
10832 10833                      &pDiag_release_msg->BufferType,
10833 10834                      diag->pBuffer->buffer_type);
10834 10835          }
10835 10836  
10836 10837          /*
10837 10838           * Send the message
10838 10839           */
10839 10840          (void) ddi_dma_sync(mpt->m_dma_req_frame_hdl, 0, 0,
10840 10841              DDI_DMA_SYNC_FORDEV);
10841 10842          request_desc_low = (cmd->cmd_slot << 16) +
10842 10843              MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
10843 10844          cmd->cmd_rfm = NULL;
10844 10845          mpt->m_active->m_slot[cmd->cmd_slot] = cmd;
10845 10846          MPTSAS_START_CMD(mpt, request_desc_low, 0);
10846 10847          if ((mptsas_check_dma_handle(mpt->m_dma_req_frame_hdl) !=
10847 10848              DDI_SUCCESS) ||
10848 10849              (mptsas_check_acc_handle(mpt->m_acc_req_frame_hdl) !=
10849 10850              DDI_SUCCESS)) {
10850 10851                  ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
10851 10852          }
10852 10853  }
10853 10854  
10854 10855  static int
10855 10856  mptsas_post_fw_diag_buffer(mptsas_t *mpt,
10856 10857      mptsas_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code)
10857 10858  {
10858 10859          mptsas_diag_request_t           diag;
10859 10860          int                             status, slot_num, post_flags = 0;
10860 10861          mptsas_cmd_t                    *cmd = NULL;
10861 10862          struct scsi_pkt                 *pkt;
10862 10863          pMpi2DiagBufferPostReply_t      reply;
10863 10864          uint16_t                        iocstatus;
10864 10865          uint32_t                        iocloginfo, transfer_length;
10865 10866  
10866 10867          /*
10867 10868           * If buffer is not enabled, just leave.
10868 10869           */
10869 10870          *return_code = MPTSAS_FW_DIAG_ERROR_POST_FAILED;
10870 10871          if (!pBuffer->enabled) {
10871 10872                  status = DDI_FAILURE;
10872 10873                  goto out;
10873 10874          }
10874 10875  
10875 10876          /*
10876 10877           * Clear some flags initially.
10877 10878           */
10878 10879          pBuffer->force_release = FALSE;
10879 10880          pBuffer->valid_data = FALSE;
10880 10881          pBuffer->owned_by_firmware = FALSE;
10881 10882  
10882 10883          /*
10883 10884           * Get a cmd buffer from the cmd buffer pool
10884 10885           */
10885 10886          if ((slot_num = (mptsas_request_from_pool(mpt, &cmd, &pkt))) == -1) {
10886 10887                  status = DDI_FAILURE;
10887 10888                  mptsas_log(mpt, CE_NOTE, "command pool is full: Post FW Diag");
10888 10889                  goto out;
10889 10890          }
10890 10891          post_flags |= MPTSAS_REQUEST_POOL_CMD;
10891 10892  
10892 10893          bzero((caddr_t)cmd, sizeof (*cmd));
10893 10894          bzero((caddr_t)pkt, scsi_pkt_size());
10894 10895  
10895 10896          cmd->ioc_cmd_slot = (uint32_t)(slot_num);
10896 10897  
10897 10898          diag.pBuffer = pBuffer;
10898 10899          diag.function = MPI2_FUNCTION_DIAG_BUFFER_POST;
10899 10900  
10900 10901          /*
10901 10902           * Form a blank cmd/pkt to store the acknowledgement message
10902 10903           */
10903 10904          pkt->pkt_ha_private     = (opaque_t)&diag;
10904 10905          pkt->pkt_flags          = FLAG_HEAD;
10905 10906          pkt->pkt_time           = 60;
10906 10907          cmd->cmd_pkt            = pkt;
10907 10908          cmd->cmd_flags          = CFLAG_CMDIOC | CFLAG_FW_DIAG;
10908 10909  
10909 10910          /*
10910 10911           * Save the command in a slot
10911 10912           */
10912 10913          if (mptsas_save_cmd(mpt, cmd) == TRUE) {
10913 10914                  /*
10914 10915                   * Once passthru command get slot, set cmd_flags
10915 10916                   * CFLAG_PREPARED.
10916 10917                   */
10917 10918                  cmd->cmd_flags |= CFLAG_PREPARED;
10918 10919                  mptsas_start_diag(mpt, cmd);
10919 10920          } else {
10920 10921                  mptsas_waitq_add(mpt, cmd);
10921 10922          }
10922 10923  
10923 10924          while ((cmd->cmd_flags & CFLAG_FINISHED) == 0) {
10924 10925                  cv_wait(&mpt->m_fw_diag_cv, &mpt->m_mutex);
10925 10926          }
10926 10927  
10927 10928          if (cmd->cmd_flags & CFLAG_TIMEOUT) {
10928 10929                  status = DDI_FAILURE;
10929 10930                  mptsas_log(mpt, CE_WARN, "Post FW Diag command timeout");
10930 10931                  goto out;
10931 10932          }
10932 10933  
10933 10934          /*
10934 10935           * cmd_rfm points to the reply message if a reply was given.  Check the
10935 10936           * IOCStatus to make sure everything went OK with the FW diag request
10936 10937           * and set buffer flags.
10937 10938           */
10938 10939          if (cmd->cmd_rfm) {
10939 10940                  post_flags |= MPTSAS_ADDRESS_REPLY;
10940 10941                  (void) ddi_dma_sync(mpt->m_dma_reply_frame_hdl, 0, 0,
10941 10942                      DDI_DMA_SYNC_FORCPU);
10942 10943                  reply = (pMpi2DiagBufferPostReply_t)(mpt->m_reply_frame +
10943 10944                      (cmd->cmd_rfm - mpt->m_reply_frame_dma_addr));
10944 10945  
10945 10946                  /*
10946 10947                   * Get the reply message data
10947 10948                   */
10948 10949                  iocstatus = ddi_get16(mpt->m_acc_reply_frame_hdl,
10949 10950                      &reply->IOCStatus);
10950 10951                  iocloginfo = ddi_get32(mpt->m_acc_reply_frame_hdl,
10951 10952                      &reply->IOCLogInfo);
10952 10953                  transfer_length = ddi_get32(mpt->m_acc_reply_frame_hdl,
10953 10954                      &reply->TransferLength);
10954 10955  
10955 10956                  /*
10956 10957                   * If post failed quit.
10957 10958                   */
10958 10959                  if (iocstatus != MPI2_IOCSTATUS_SUCCESS) {
10959 10960                          status = DDI_FAILURE;
10960 10961                          NDBG13(("post FW Diag Buffer failed: IOCStatus=0x%x, "
10961 10962                              "IOCLogInfo=0x%x, TransferLength=0x%x", iocstatus,
10962 10963                              iocloginfo, transfer_length));
10963 10964                          goto out;
10964 10965                  }
10965 10966  
10966 10967                  /*
10967 10968                   * Post was successful.
10968 10969                   */
10969 10970                  pBuffer->valid_data = TRUE;
10970 10971                  pBuffer->owned_by_firmware = TRUE;
10971 10972                  *return_code = MPTSAS_FW_DIAG_ERROR_SUCCESS;
10972 10973                  status = DDI_SUCCESS;
10973 10974          }
10974 10975  
10975 10976  out:
10976 10977          /*
10977 10978           * Put the reply frame back on the free queue, increment the free
10978 10979           * index, and write the new index to the free index register.  But only
10979 10980           * if this reply is an ADDRESS reply.
10980 10981           */
10981 10982          if (post_flags & MPTSAS_ADDRESS_REPLY) {
10982 10983                  ddi_put32(mpt->m_acc_free_queue_hdl,
10983 10984                      &((uint32_t *)(void *)mpt->m_free_queue)[mpt->m_free_index],
10984 10985                      cmd->cmd_rfm);
10985 10986                  (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
10986 10987                      DDI_DMA_SYNC_FORDEV);
10987 10988                  if (++mpt->m_free_index == mpt->m_free_queue_depth) {
10988 10989                          mpt->m_free_index = 0;
10989 10990                  }
10990 10991                  ddi_put32(mpt->m_datap, &mpt->m_reg->ReplyFreeHostIndex,
10991 10992                      mpt->m_free_index);
10992 10993          }
10993 10994          if (cmd && (cmd->cmd_flags & CFLAG_PREPARED)) {
10994 10995                  mptsas_remove_cmd(mpt, cmd);
10995 10996                  post_flags &= (~MPTSAS_REQUEST_POOL_CMD);
10996 10997          }
10997 10998          if (post_flags & MPTSAS_REQUEST_POOL_CMD) {
10998 10999                  mptsas_return_to_pool(mpt, cmd);
10999 11000          }
11000 11001  
11001 11002          return (status);
11002 11003  }
11003 11004  
11004 11005  static int
11005 11006  mptsas_release_fw_diag_buffer(mptsas_t *mpt,
11006 11007      mptsas_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code,
11007 11008      uint32_t diag_type)
11008 11009  {
11009 11010          mptsas_diag_request_t   diag;
11010 11011          int                     status, slot_num, rel_flags = 0;
11011 11012          mptsas_cmd_t            *cmd = NULL;
11012 11013          struct scsi_pkt         *pkt;
11013 11014          pMpi2DiagReleaseReply_t reply;
11014 11015          uint16_t                iocstatus;
11015 11016          uint32_t                iocloginfo;
11016 11017  
11017 11018          /*
11018 11019           * If buffer is not enabled, just leave.
11019 11020           */
11020 11021          *return_code = MPTSAS_FW_DIAG_ERROR_RELEASE_FAILED;
11021 11022          if (!pBuffer->enabled) {
11022 11023                  mptsas_log(mpt, CE_NOTE, "This buffer type is not supported "
11023 11024                      "by the IOC");
11024 11025                  status = DDI_FAILURE;
11025 11026                  goto out;
11026 11027          }
11027 11028  
11028 11029          /*
11029 11030           * Clear some flags initially.
11030 11031           */
11031 11032          pBuffer->force_release = FALSE;
11032 11033          pBuffer->valid_data = FALSE;
11033 11034          pBuffer->owned_by_firmware = FALSE;
11034 11035  
11035 11036          /*
11036 11037           * Get a cmd buffer from the cmd buffer pool
11037 11038           */
11038 11039          if ((slot_num = (mptsas_request_from_pool(mpt, &cmd, &pkt))) == -1) {
11039 11040                  status = DDI_FAILURE;
11040 11041                  mptsas_log(mpt, CE_NOTE, "command pool is full: Release FW "
11041 11042                      "Diag");
11042 11043                  goto out;
11043 11044          }
11044 11045          rel_flags |= MPTSAS_REQUEST_POOL_CMD;
11045 11046  
11046 11047          bzero((caddr_t)cmd, sizeof (*cmd));
11047 11048          bzero((caddr_t)pkt, scsi_pkt_size());
11048 11049  
11049 11050          cmd->ioc_cmd_slot = (uint32_t)(slot_num);
11050 11051  
11051 11052          diag.pBuffer = pBuffer;
11052 11053          diag.function = MPI2_FUNCTION_DIAG_RELEASE;
11053 11054  
11054 11055          /*
11055 11056           * Form a blank cmd/pkt to store the acknowledgement message
11056 11057           */
11057 11058          pkt->pkt_ha_private     = (opaque_t)&diag;
11058 11059          pkt->pkt_flags          = FLAG_HEAD;
11059 11060          pkt->pkt_time           = 60;
11060 11061          cmd->cmd_pkt            = pkt;
11061 11062          cmd->cmd_flags          = CFLAG_CMDIOC | CFLAG_FW_DIAG;
11062 11063  
11063 11064          /*
11064 11065           * Save the command in a slot
11065 11066           */
11066 11067          if (mptsas_save_cmd(mpt, cmd) == TRUE) {
11067 11068                  /*
11068 11069                   * Once passthru command get slot, set cmd_flags
11069 11070                   * CFLAG_PREPARED.
11070 11071                   */
11071 11072                  cmd->cmd_flags |= CFLAG_PREPARED;
11072 11073                  mptsas_start_diag(mpt, cmd);
11073 11074          } else {
11074 11075                  mptsas_waitq_add(mpt, cmd);
11075 11076          }
11076 11077  
11077 11078          while ((cmd->cmd_flags & CFLAG_FINISHED) == 0) {
11078 11079                  cv_wait(&mpt->m_fw_diag_cv, &mpt->m_mutex);
11079 11080          }
11080 11081  
11081 11082          if (cmd->cmd_flags & CFLAG_TIMEOUT) {
11082 11083                  status = DDI_FAILURE;
11083 11084                  mptsas_log(mpt, CE_WARN, "Release FW Diag command timeout");
11084 11085                  goto out;
11085 11086          }
11086 11087  
11087 11088          /*
11088 11089           * cmd_rfm points to the reply message if a reply was given.  Check the
11089 11090           * IOCStatus to make sure everything went OK with the FW diag request
11090 11091           * and set buffer flags.
11091 11092           */
11092 11093          if (cmd->cmd_rfm) {
11093 11094                  rel_flags |= MPTSAS_ADDRESS_REPLY;
11094 11095                  (void) ddi_dma_sync(mpt->m_dma_reply_frame_hdl, 0, 0,
11095 11096                      DDI_DMA_SYNC_FORCPU);
11096 11097                  reply = (pMpi2DiagReleaseReply_t)(mpt->m_reply_frame +
11097 11098                      (cmd->cmd_rfm - mpt->m_reply_frame_dma_addr));
11098 11099  
11099 11100                  /*
11100 11101                   * Get the reply message data
11101 11102                   */
11102 11103                  iocstatus = ddi_get16(mpt->m_acc_reply_frame_hdl,
11103 11104                      &reply->IOCStatus);
11104 11105                  iocloginfo = ddi_get32(mpt->m_acc_reply_frame_hdl,
11105 11106                      &reply->IOCLogInfo);
11106 11107  
11107 11108                  /*
11108 11109                   * If release failed quit.
11109 11110                   */
11110 11111                  if ((iocstatus != MPI2_IOCSTATUS_SUCCESS) ||
11111 11112                      pBuffer->owned_by_firmware) {
11112 11113                          status = DDI_FAILURE;
11113 11114                          NDBG13(("release FW Diag Buffer failed: "
11114 11115                              "IOCStatus=0x%x, IOCLogInfo=0x%x", iocstatus,
11115 11116                              iocloginfo));
11116 11117                          goto out;
11117 11118                  }
11118 11119  
11119 11120                  /*
11120 11121                   * Release was successful.
11121 11122                   */
11122 11123                  *return_code = MPTSAS_FW_DIAG_ERROR_SUCCESS;
11123 11124                  status = DDI_SUCCESS;
11124 11125  
11125 11126                  /*
11126 11127                   * If this was for an UNREGISTER diag type command, clear the
11127 11128                   * unique ID.
11128 11129                   */
11129 11130                  if (diag_type == MPTSAS_FW_DIAG_TYPE_UNREGISTER) {
11130 11131                          pBuffer->unique_id = MPTSAS_FW_DIAG_INVALID_UID;
11131 11132                  }
11132 11133          }
11133 11134  
11134 11135  out:
11135 11136          /*
11136 11137           * Put the reply frame back on the free queue, increment the free
11137 11138           * index, and write the new index to the free index register.  But only
11138 11139           * if this reply is an ADDRESS reply.
11139 11140           */
11140 11141          if (rel_flags & MPTSAS_ADDRESS_REPLY) {
11141 11142                  ddi_put32(mpt->m_acc_free_queue_hdl,
11142 11143                      &((uint32_t *)(void *)mpt->m_free_queue)[mpt->m_free_index],
11143 11144                      cmd->cmd_rfm);
11144 11145                  (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
11145 11146                      DDI_DMA_SYNC_FORDEV);
11146 11147                  if (++mpt->m_free_index == mpt->m_free_queue_depth) {
11147 11148                          mpt->m_free_index = 0;
11148 11149                  }
11149 11150                  ddi_put32(mpt->m_datap, &mpt->m_reg->ReplyFreeHostIndex,
11150 11151                      mpt->m_free_index);
11151 11152          }
11152 11153          if (cmd && (cmd->cmd_flags & CFLAG_PREPARED)) {
11153 11154                  mptsas_remove_cmd(mpt, cmd);
11154 11155                  rel_flags &= (~MPTSAS_REQUEST_POOL_CMD);
11155 11156          }
11156 11157          if (rel_flags & MPTSAS_REQUEST_POOL_CMD) {
11157 11158                  mptsas_return_to_pool(mpt, cmd);
11158 11159          }
11159 11160  
11160 11161          return (status);
11161 11162  }
11162 11163  
11163 11164  static int
11164 11165  mptsas_diag_register(mptsas_t *mpt, mptsas_fw_diag_register_t *diag_register,
11165 11166      uint32_t *return_code)
11166 11167  {
11167 11168          mptsas_fw_diagnostic_buffer_t   *pBuffer;
11168 11169          uint8_t                         extended_type, buffer_type, i;
11169 11170          uint32_t                        buffer_size;
11170 11171          uint32_t                        unique_id;
11171 11172          int                             status;
11172 11173  
11173 11174          ASSERT(mutex_owned(&mpt->m_mutex));
11174 11175  
11175 11176          extended_type = diag_register->ExtendedType;
11176 11177          buffer_type = diag_register->BufferType;
11177 11178          buffer_size = diag_register->RequestedBufferSize;
11178 11179          unique_id = diag_register->UniqueId;
11179 11180  
11180 11181          /*
11181 11182           * Check for valid buffer type
11182 11183           */
11183 11184          if (buffer_type >= MPI2_DIAG_BUF_TYPE_COUNT) {
11184 11185                  *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11185 11186                  return (DDI_FAILURE);
11186 11187          }
11187 11188  
11188 11189          /*
11189 11190           * Get the current buffer and look up the unique ID.  The unique ID
11190 11191           * should not be found.  If it is, the ID is already in use.
11191 11192           */
11192 11193          i = mptsas_get_fw_diag_buffer_number(mpt, unique_id);
11193 11194          pBuffer = &mpt->m_fw_diag_buffer_list[buffer_type];
11194 11195          if (i != MPTSAS_FW_DIAGNOSTIC_UID_NOT_FOUND) {
11195 11196                  *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11196 11197                  return (DDI_FAILURE);
11197 11198          }
11198 11199  
11199 11200          /*
11200 11201           * The buffer's unique ID should not be registered yet, and the given
11201 11202           * unique ID cannot be 0.
11202 11203           */
11203 11204          if ((pBuffer->unique_id != MPTSAS_FW_DIAG_INVALID_UID) ||
11204 11205              (unique_id == MPTSAS_FW_DIAG_INVALID_UID)) {
11205 11206                  *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11206 11207                  return (DDI_FAILURE);
11207 11208          }
11208 11209  
11209 11210          /*
11210 11211           * If this buffer is already posted as immediate, just change owner.
11211 11212           */
11212 11213          if (pBuffer->immediate && pBuffer->owned_by_firmware &&
11213 11214              (pBuffer->unique_id == MPTSAS_FW_DIAG_INVALID_UID)) {
11214 11215                  pBuffer->immediate = FALSE;
11215 11216                  pBuffer->unique_id = unique_id;
11216 11217                  return (DDI_SUCCESS);
11217 11218          }
11218 11219  
11219 11220          /*
11220 11221           * Post a new buffer after checking if it's enabled.  The DMA buffer
11221 11222           * that is allocated will be contiguous (sgl_len = 1).
11222 11223           */
11223 11224          if (!pBuffer->enabled) {
11224 11225                  *return_code = MPTSAS_FW_DIAG_ERROR_NO_BUFFER;
11225 11226                  return (DDI_FAILURE);
11226 11227          }
11227 11228          bzero(&pBuffer->buffer_data, sizeof (mptsas_dma_alloc_state_t));
11228 11229          pBuffer->buffer_data.size = buffer_size;
11229 11230          if (mptsas_dma_alloc(mpt, &pBuffer->buffer_data) != DDI_SUCCESS) {
11230 11231                  mptsas_log(mpt, CE_WARN, "failed to alloc DMA resource for "
11231 11232                      "diag buffer: size = %d bytes", buffer_size);
11232 11233                  *return_code = MPTSAS_FW_DIAG_ERROR_NO_BUFFER;
11233 11234                  return (DDI_FAILURE);
11234 11235          }
11235 11236  
11236 11237          /*
11237 11238           * Copy the given info to the diag buffer and post the buffer.
11238 11239           */
11239 11240          pBuffer->buffer_type = buffer_type;
11240 11241          pBuffer->immediate = FALSE;
11241 11242          if (buffer_type == MPI2_DIAG_BUF_TYPE_TRACE) {
11242 11243                  for (i = 0; i < (sizeof (pBuffer->product_specific) / 4);
11243 11244                      i++) {
11244 11245                          pBuffer->product_specific[i] =
11245 11246                              diag_register->ProductSpecific[i];
11246 11247                  }
11247 11248          }
11248 11249          pBuffer->extended_type = extended_type;
11249 11250          pBuffer->unique_id = unique_id;
11250 11251          status = mptsas_post_fw_diag_buffer(mpt, pBuffer, return_code);
11251 11252  
11252 11253          if (mptsas_check_dma_handle(pBuffer->buffer_data.handle) !=
11253 11254              DDI_SUCCESS) {
11254 11255                  mptsas_log(mpt, CE_WARN, "Check of DMA handle failed in "
11255 11256                      "mptsas_diag_register.");
11256 11257                  ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
11257 11258                          status = DDI_FAILURE;
11258 11259          }
11259 11260  
11260 11261          /*
11261 11262           * In case there was a failure, free the DMA buffer.
11262 11263           */
11263 11264          if (status == DDI_FAILURE) {
11264 11265                  mptsas_dma_free(&pBuffer->buffer_data);
11265 11266          }
11266 11267  
11267 11268          return (status);
11268 11269  }
11269 11270  
11270 11271  static int
11271 11272  mptsas_diag_unregister(mptsas_t *mpt,
11272 11273      mptsas_fw_diag_unregister_t *diag_unregister, uint32_t *return_code)
11273 11274  {
11274 11275          mptsas_fw_diagnostic_buffer_t   *pBuffer;
11275 11276          uint8_t                         i;
11276 11277          uint32_t                        unique_id;
11277 11278          int                             status;
11278 11279  
11279 11280          ASSERT(mutex_owned(&mpt->m_mutex));
11280 11281  
11281 11282          unique_id = diag_unregister->UniqueId;
11282 11283  
11283 11284          /*
11284 11285           * Get the current buffer and look up the unique ID.  The unique ID
11285 11286           * should be there.
11286 11287           */
11287 11288          i = mptsas_get_fw_diag_buffer_number(mpt, unique_id);
11288 11289          if (i == MPTSAS_FW_DIAGNOSTIC_UID_NOT_FOUND) {
11289 11290                  *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11290 11291                  return (DDI_FAILURE);
11291 11292          }
11292 11293  
11293 11294          pBuffer = &mpt->m_fw_diag_buffer_list[i];
11294 11295  
11295 11296          /*
11296 11297           * Try to release the buffer from FW before freeing it.  If release
11297 11298           * fails, don't free the DMA buffer in case FW tries to access it
11298 11299           * later.  If buffer is not owned by firmware, can't release it.
11299 11300           */
11300 11301          if (!pBuffer->owned_by_firmware) {
11301 11302                  status = DDI_SUCCESS;
11302 11303          } else {
11303 11304                  status = mptsas_release_fw_diag_buffer(mpt, pBuffer,
11304 11305                      return_code, MPTSAS_FW_DIAG_TYPE_UNREGISTER);
11305 11306          }
11306 11307  
11307 11308          /*
11308 11309           * At this point, return the current status no matter what happens with
11309 11310           * the DMA buffer.
11310 11311           */
11311 11312          pBuffer->unique_id = MPTSAS_FW_DIAG_INVALID_UID;
11312 11313          if (status == DDI_SUCCESS) {
11313 11314                  if (mptsas_check_dma_handle(pBuffer->buffer_data.handle) !=
11314 11315                      DDI_SUCCESS) {
11315 11316                          mptsas_log(mpt, CE_WARN, "Check of DMA handle failed "
11316 11317                              "in mptsas_diag_unregister.");
11317 11318                          ddi_fm_service_impact(mpt->m_dip,
11318 11319                              DDI_SERVICE_UNAFFECTED);
11319 11320                  }
11320 11321                  mptsas_dma_free(&pBuffer->buffer_data);
11321 11322          }
11322 11323  
11323 11324          return (status);
11324 11325  }
11325 11326  
11326 11327  static int
11327 11328  mptsas_diag_query(mptsas_t *mpt, mptsas_fw_diag_query_t *diag_query,
11328 11329      uint32_t *return_code)
11329 11330  {
11330 11331          mptsas_fw_diagnostic_buffer_t   *pBuffer;
11331 11332          uint8_t                         i;
11332 11333          uint32_t                        unique_id;
11333 11334  
11334 11335          ASSERT(mutex_owned(&mpt->m_mutex));
11335 11336  
11336 11337          unique_id = diag_query->UniqueId;
11337 11338  
11338 11339          /*
11339 11340           * If ID is valid, query on ID.
11340 11341           * If ID is invalid, query on buffer type.
11341 11342           */
11342 11343          if (unique_id == MPTSAS_FW_DIAG_INVALID_UID) {
11343 11344                  i = diag_query->BufferType;
11344 11345                  if (i >= MPI2_DIAG_BUF_TYPE_COUNT) {
11345 11346                          *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11346 11347                          return (DDI_FAILURE);
11347 11348                  }
11348 11349          } else {
11349 11350                  i = mptsas_get_fw_diag_buffer_number(mpt, unique_id);
11350 11351                  if (i == MPTSAS_FW_DIAGNOSTIC_UID_NOT_FOUND) {
11351 11352                          *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11352 11353                          return (DDI_FAILURE);
11353 11354                  }
11354 11355          }
11355 11356  
11356 11357          /*
11357 11358           * Fill query structure with the diag buffer info.
11358 11359           */
11359 11360          pBuffer = &mpt->m_fw_diag_buffer_list[i];
11360 11361          diag_query->BufferType = pBuffer->buffer_type;
11361 11362          diag_query->ExtendedType = pBuffer->extended_type;
11362 11363          if (diag_query->BufferType == MPI2_DIAG_BUF_TYPE_TRACE) {
11363 11364                  for (i = 0; i < (sizeof (diag_query->ProductSpecific) / 4);
11364 11365                      i++) {
11365 11366                          diag_query->ProductSpecific[i] =
11366 11367                              pBuffer->product_specific[i];
11367 11368                  }
11368 11369          }
11369 11370          diag_query->TotalBufferSize = pBuffer->buffer_data.size;
11370 11371          diag_query->DriverAddedBufferSize = 0;
11371 11372          diag_query->UniqueId = pBuffer->unique_id;
11372 11373          diag_query->ApplicationFlags = 0;
11373 11374          diag_query->DiagnosticFlags = 0;
11374 11375  
11375 11376          /*
11376 11377           * Set/Clear application flags
11377 11378           */
11378 11379          if (pBuffer->immediate) {
11379 11380                  diag_query->ApplicationFlags &= ~MPTSAS_FW_DIAG_FLAG_APP_OWNED;
11380 11381          } else {
11381 11382                  diag_query->ApplicationFlags |= MPTSAS_FW_DIAG_FLAG_APP_OWNED;
11382 11383          }
11383 11384          if (pBuffer->valid_data || pBuffer->owned_by_firmware) {
11384 11385                  diag_query->ApplicationFlags |=
11385 11386                      MPTSAS_FW_DIAG_FLAG_BUFFER_VALID;
11386 11387          } else {
11387 11388                  diag_query->ApplicationFlags &=
11388 11389                      ~MPTSAS_FW_DIAG_FLAG_BUFFER_VALID;
11389 11390          }
11390 11391          if (pBuffer->owned_by_firmware) {
11391 11392                  diag_query->ApplicationFlags |=
11392 11393                      MPTSAS_FW_DIAG_FLAG_FW_BUFFER_ACCESS;
11393 11394          } else {
11394 11395                  diag_query->ApplicationFlags &=
11395 11396                      ~MPTSAS_FW_DIAG_FLAG_FW_BUFFER_ACCESS;
11396 11397          }
11397 11398  
11398 11399          return (DDI_SUCCESS);
11399 11400  }
11400 11401  
11401 11402  static int
11402 11403  mptsas_diag_read_buffer(mptsas_t *mpt,
11403 11404      mptsas_diag_read_buffer_t *diag_read_buffer, uint8_t *ioctl_buf,
11404 11405      uint32_t *return_code, int ioctl_mode)
11405 11406  {
11406 11407          mptsas_fw_diagnostic_buffer_t   *pBuffer;
11407 11408          uint8_t                         i, *pData;
11408 11409          uint32_t                        unique_id, byte;
11409 11410          int                             status;
11410 11411  
11411 11412          ASSERT(mutex_owned(&mpt->m_mutex));
11412 11413  
11413 11414          unique_id = diag_read_buffer->UniqueId;
11414 11415  
11415 11416          /*
11416 11417           * Get the current buffer and look up the unique ID.  The unique ID
11417 11418           * should be there.
11418 11419           */
11419 11420          i = mptsas_get_fw_diag_buffer_number(mpt, unique_id);
11420 11421          if (i == MPTSAS_FW_DIAGNOSTIC_UID_NOT_FOUND) {
11421 11422                  *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11422 11423                  return (DDI_FAILURE);
11423 11424          }
11424 11425  
11425 11426          pBuffer = &mpt->m_fw_diag_buffer_list[i];
11426 11427  
11427 11428          /*
11428 11429           * Make sure requested read is within limits
11429 11430           */
11430 11431          if (diag_read_buffer->StartingOffset + diag_read_buffer->BytesToRead >
11431 11432              pBuffer->buffer_data.size) {
11432 11433                  *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11433 11434                  return (DDI_FAILURE);
11434 11435          }
11435 11436  
11436 11437          /*
11437 11438           * Copy the requested data from DMA to the diag_read_buffer.  The DMA
11438 11439           * buffer that was allocated is one contiguous buffer.
11439 11440           */
11440 11441          pData = (uint8_t *)(pBuffer->buffer_data.memp +
11441 11442              diag_read_buffer->StartingOffset);
11442 11443          (void) ddi_dma_sync(pBuffer->buffer_data.handle, 0, 0,
11443 11444              DDI_DMA_SYNC_FORCPU);
11444 11445          for (byte = 0; byte < diag_read_buffer->BytesToRead; byte++) {
11445 11446                  if (ddi_copyout(pData + byte, ioctl_buf + byte, 1, ioctl_mode)
11446 11447                      != 0) {
11447 11448                          return (DDI_FAILURE);
11448 11449                  }
11449 11450          }
11450 11451          diag_read_buffer->Status = 0;
11451 11452  
11452 11453          /*
11453 11454           * Set or clear the Force Release flag.
11454 11455           */
11455 11456          if (pBuffer->force_release) {
11456 11457                  diag_read_buffer->Flags |= MPTSAS_FW_DIAG_FLAG_FORCE_RELEASE;
11457 11458          } else {
11458 11459                  diag_read_buffer->Flags &= ~MPTSAS_FW_DIAG_FLAG_FORCE_RELEASE;
11459 11460          }
11460 11461  
11461 11462          /*
11462 11463           * If buffer is to be reregistered, make sure it's not already owned by
11463 11464           * firmware first.
11464 11465           */
11465 11466          status = DDI_SUCCESS;
11466 11467          if (!pBuffer->owned_by_firmware) {
11467 11468                  if (diag_read_buffer->Flags & MPTSAS_FW_DIAG_FLAG_REREGISTER) {
11468 11469                          status = mptsas_post_fw_diag_buffer(mpt, pBuffer,
11469 11470                              return_code);
11470 11471                  }
11471 11472          }
11472 11473  
11473 11474          return (status);
11474 11475  }
11475 11476  
11476 11477  static int
11477 11478  mptsas_diag_release(mptsas_t *mpt, mptsas_fw_diag_release_t *diag_release,
11478 11479      uint32_t *return_code)
11479 11480  {
11480 11481          mptsas_fw_diagnostic_buffer_t   *pBuffer;
11481 11482          uint8_t                         i;
11482 11483          uint32_t                        unique_id;
11483 11484          int                             status;
11484 11485  
11485 11486          ASSERT(mutex_owned(&mpt->m_mutex));
11486 11487  
11487 11488          unique_id = diag_release->UniqueId;
11488 11489  
11489 11490          /*
11490 11491           * Get the current buffer and look up the unique ID.  The unique ID
11491 11492           * should be there.
11492 11493           */
11493 11494          i = mptsas_get_fw_diag_buffer_number(mpt, unique_id);
11494 11495          if (i == MPTSAS_FW_DIAGNOSTIC_UID_NOT_FOUND) {
11495 11496                  *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11496 11497                  return (DDI_FAILURE);
11497 11498          }
11498 11499  
11499 11500          pBuffer = &mpt->m_fw_diag_buffer_list[i];
11500 11501  
11501 11502          /*
11502 11503           * If buffer is not owned by firmware, it's already been released.
11503 11504           */
11504 11505          if (!pBuffer->owned_by_firmware) {
11505 11506                  *return_code = MPTSAS_FW_DIAG_ERROR_ALREADY_RELEASED;
11506 11507                  return (DDI_FAILURE);
11507 11508          }
11508 11509  
11509 11510          /*
11510 11511           * Release the buffer.
11511 11512           */
11512 11513          status = mptsas_release_fw_diag_buffer(mpt, pBuffer, return_code,
11513 11514              MPTSAS_FW_DIAG_TYPE_RELEASE);
11514 11515          return (status);
11515 11516  }
11516 11517  
11517 11518  static int
11518 11519  mptsas_do_diag_action(mptsas_t *mpt, uint32_t action, uint8_t *diag_action,
11519 11520      uint32_t length, uint32_t *return_code, int ioctl_mode)
11520 11521  {
11521 11522          mptsas_fw_diag_register_t       diag_register;
11522 11523          mptsas_fw_diag_unregister_t     diag_unregister;
11523 11524          mptsas_fw_diag_query_t          diag_query;
11524 11525          mptsas_diag_read_buffer_t       diag_read_buffer;
11525 11526          mptsas_fw_diag_release_t        diag_release;
11526 11527          int                             status = DDI_SUCCESS;
11527 11528          uint32_t                        original_return_code, read_buf_len;
11528 11529  
11529 11530          ASSERT(mutex_owned(&mpt->m_mutex));
11530 11531  
11531 11532          original_return_code = *return_code;
11532 11533          *return_code = MPTSAS_FW_DIAG_ERROR_SUCCESS;
11533 11534  
11534 11535          switch (action) {
11535 11536                  case MPTSAS_FW_DIAG_TYPE_REGISTER:
11536 11537                          if (!length) {
11537 11538                                  *return_code =
11538 11539                                      MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11539 11540                                  status = DDI_FAILURE;
11540 11541                                  break;
11541 11542                          }
11542 11543                          if (ddi_copyin(diag_action, &diag_register,
11543 11544                              sizeof (diag_register), ioctl_mode) != 0) {
11544 11545                                  return (DDI_FAILURE);
11545 11546                          }
11546 11547                          status = mptsas_diag_register(mpt, &diag_register,
11547 11548                              return_code);
11548 11549                          break;
11549 11550  
11550 11551                  case MPTSAS_FW_DIAG_TYPE_UNREGISTER:
11551 11552                          if (length < sizeof (diag_unregister)) {
11552 11553                                  *return_code =
11553 11554                                      MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11554 11555                                  status = DDI_FAILURE;
11555 11556                                  break;
11556 11557                          }
11557 11558                          if (ddi_copyin(diag_action, &diag_unregister,
11558 11559                              sizeof (diag_unregister), ioctl_mode) != 0) {
11559 11560                                  return (DDI_FAILURE);
11560 11561                          }
11561 11562                          status = mptsas_diag_unregister(mpt, &diag_unregister,
11562 11563                              return_code);
11563 11564                          break;
11564 11565  
11565 11566                  case MPTSAS_FW_DIAG_TYPE_QUERY:
11566 11567                          if (length < sizeof (diag_query)) {
11567 11568                                  *return_code =
11568 11569                                      MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11569 11570                                  status = DDI_FAILURE;
11570 11571                                  break;
11571 11572                          }
11572 11573                          if (ddi_copyin(diag_action, &diag_query,
11573 11574                              sizeof (diag_query), ioctl_mode) != 0) {
11574 11575                                  return (DDI_FAILURE);
11575 11576                          }
11576 11577                          status = mptsas_diag_query(mpt, &diag_query,
11577 11578                              return_code);
11578 11579                          if (status == DDI_SUCCESS) {
11579 11580                                  if (ddi_copyout(&diag_query, diag_action,
11580 11581                                      sizeof (diag_query), ioctl_mode) != 0) {
11581 11582                                          return (DDI_FAILURE);
11582 11583                                  }
11583 11584                          }
11584 11585                          break;
11585 11586  
11586 11587                  case MPTSAS_FW_DIAG_TYPE_READ_BUFFER:
11587 11588                          if (ddi_copyin(diag_action, &diag_read_buffer,
11588 11589                              sizeof (diag_read_buffer) - 4, ioctl_mode) != 0) {
11589 11590                                  return (DDI_FAILURE);
11590 11591                          }
11591 11592                          read_buf_len = sizeof (diag_read_buffer) -
11592 11593                              sizeof (diag_read_buffer.DataBuffer) +
11593 11594                              diag_read_buffer.BytesToRead;
11594 11595                          if (length < read_buf_len) {
11595 11596                                  *return_code =
11596 11597                                      MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11597 11598                                  status = DDI_FAILURE;
11598 11599                                  break;
11599 11600                          }
11600 11601                          status = mptsas_diag_read_buffer(mpt,
11601 11602                              &diag_read_buffer, diag_action +
11602 11603                              sizeof (diag_read_buffer) - 4, return_code,
11603 11604                              ioctl_mode);
11604 11605                          if (status == DDI_SUCCESS) {
11605 11606                                  if (ddi_copyout(&diag_read_buffer, diag_action,
11606 11607                                      sizeof (diag_read_buffer) - 4, ioctl_mode)
11607 11608                                      != 0) {
11608 11609                                          return (DDI_FAILURE);
11609 11610                                  }
11610 11611                          }
11611 11612                          break;
11612 11613  
11613 11614                  case MPTSAS_FW_DIAG_TYPE_RELEASE:
11614 11615                          if (length < sizeof (diag_release)) {
11615 11616                                  *return_code =
11616 11617                                      MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11617 11618                                  status = DDI_FAILURE;
11618 11619                                  break;
11619 11620                          }
11620 11621                          if (ddi_copyin(diag_action, &diag_release,
11621 11622                              sizeof (diag_release), ioctl_mode) != 0) {
11622 11623                                  return (DDI_FAILURE);
11623 11624                          }
11624 11625                          status = mptsas_diag_release(mpt, &diag_release,
11625 11626                              return_code);
11626 11627                          break;
11627 11628  
11628 11629                  default:
11629 11630                          *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11630 11631                          status = DDI_FAILURE;
11631 11632                          break;
11632 11633          }
11633 11634  
11634 11635          if ((status == DDI_FAILURE) &&
11635 11636              (original_return_code == MPTSAS_FW_DIAG_NEW) &&
11636 11637              (*return_code != MPTSAS_FW_DIAG_ERROR_SUCCESS)) {
11637 11638                  status = DDI_SUCCESS;
11638 11639          }
11639 11640  
11640 11641          return (status);
11641 11642  }
11642 11643  
11643 11644  static int
11644 11645  mptsas_diag_action(mptsas_t *mpt, mptsas_diag_action_t *user_data, int mode)
11645 11646  {
11646 11647          int                     status;
11647 11648          mptsas_diag_action_t    driver_data;
11648 11649  
11649 11650          ASSERT(mutex_owned(&mpt->m_mutex));
11650 11651  
11651 11652          /*
11652 11653           * Copy the user data to a driver data buffer.
11653 11654           */
11654 11655          if (ddi_copyin(user_data, &driver_data, sizeof (mptsas_diag_action_t),
11655 11656              mode) == 0) {
11656 11657                  /*
11657 11658                   * Send diag action request if Action is valid
11658 11659                   */
11659 11660                  if (driver_data.Action == MPTSAS_FW_DIAG_TYPE_REGISTER ||
11660 11661                      driver_data.Action == MPTSAS_FW_DIAG_TYPE_UNREGISTER ||
11661 11662                      driver_data.Action == MPTSAS_FW_DIAG_TYPE_QUERY ||
11662 11663                      driver_data.Action == MPTSAS_FW_DIAG_TYPE_READ_BUFFER ||
11663 11664                      driver_data.Action == MPTSAS_FW_DIAG_TYPE_RELEASE) {
11664 11665                          status = mptsas_do_diag_action(mpt, driver_data.Action,
11665 11666                              (void *)(uintptr_t)driver_data.PtrDiagAction,
11666 11667                              driver_data.Length, &driver_data.ReturnCode,
11667 11668                              mode);
11668 11669                          if (status == DDI_SUCCESS) {
11669 11670                                  if (ddi_copyout(&driver_data.ReturnCode,
11670 11671                                      &user_data->ReturnCode,
11671 11672                                      sizeof (user_data->ReturnCode), mode)
11672 11673                                      != 0) {
11673 11674                                          status = EFAULT;
11674 11675                                  } else {
11675 11676                                          status = 0;
11676 11677                                  }
11677 11678                          } else {
11678 11679                                  status = EIO;
11679 11680                          }
11680 11681                  } else {
11681 11682                          status = EINVAL;
11682 11683                  }
11683 11684          } else {
11684 11685                  status = EFAULT;
11685 11686          }
11686 11687  
11687 11688          return (status);
11688 11689  }
11689 11690  
11690 11691  /*
11691 11692   * This routine handles the "event query" ioctl.
11692 11693   */
11693 11694  static int
11694 11695  mptsas_event_query(mptsas_t *mpt, mptsas_event_query_t *data, int mode,
11695 11696      int *rval)
11696 11697  {
11697 11698          int                     status;
11698 11699          mptsas_event_query_t    driverdata;
11699 11700          uint8_t                 i;
11700 11701  
11701 11702          driverdata.Entries = MPTSAS_EVENT_QUEUE_SIZE;
11702 11703  
11703 11704          mutex_enter(&mpt->m_mutex);
11704 11705          for (i = 0; i < 4; i++) {
11705 11706                  driverdata.Types[i] = mpt->m_event_mask[i];
11706 11707          }
11707 11708          mutex_exit(&mpt->m_mutex);
11708 11709  
11709 11710          if (ddi_copyout(&driverdata, data, sizeof (driverdata), mode) != 0) {
11710 11711                  status = EFAULT;
11711 11712          } else {
11712 11713                  *rval = MPTIOCTL_STATUS_GOOD;
11713 11714                  status = 0;
11714 11715          }
11715 11716  
11716 11717          return (status);
11717 11718  }
11718 11719  
11719 11720  /*
11720 11721   * This routine handles the "event enable" ioctl.
11721 11722   */
11722 11723  static int
11723 11724  mptsas_event_enable(mptsas_t *mpt, mptsas_event_enable_t *data, int mode,
11724 11725      int *rval)
11725 11726  {
11726 11727          int                     status;
11727 11728          mptsas_event_enable_t   driverdata;
11728 11729          uint8_t                 i;
11729 11730  
11730 11731          if (ddi_copyin(data, &driverdata, sizeof (driverdata), mode) == 0) {
11731 11732                  mutex_enter(&mpt->m_mutex);
11732 11733                  for (i = 0; i < 4; i++) {
11733 11734                          mpt->m_event_mask[i] = driverdata.Types[i];
11734 11735                  }
11735 11736                  mutex_exit(&mpt->m_mutex);
11736 11737  
11737 11738                  *rval = MPTIOCTL_STATUS_GOOD;
11738 11739                  status = 0;
11739 11740          } else {
11740 11741                  status = EFAULT;
11741 11742          }
11742 11743          return (status);
11743 11744  }
11744 11745  
11745 11746  /*
11746 11747   * This routine handles the "event report" ioctl.
11747 11748   */
11748 11749  static int
11749 11750  mptsas_event_report(mptsas_t *mpt, mptsas_event_report_t *data, int mode,
11750 11751      int *rval)
11751 11752  {
11752 11753          int                     status;
11753 11754          mptsas_event_report_t   driverdata;
11754 11755  
11755 11756          mutex_enter(&mpt->m_mutex);
11756 11757  
11757 11758          if (ddi_copyin(&data->Size, &driverdata.Size, sizeof (driverdata.Size),
11758 11759              mode) == 0) {
11759 11760                  if (driverdata.Size >= sizeof (mpt->m_events)) {
11760 11761                          if (ddi_copyout(mpt->m_events, data->Events,
11761 11762                              sizeof (mpt->m_events), mode) != 0) {
11762 11763                                  status = EFAULT;
11763 11764                          } else {
11764 11765                                  if (driverdata.Size > sizeof (mpt->m_events)) {
11765 11766                                          driverdata.Size =
11766 11767                                              sizeof (mpt->m_events);
11767 11768                                          if (ddi_copyout(&driverdata.Size,
11768 11769                                              &data->Size,
11769 11770                                              sizeof (driverdata.Size),
11770 11771                                              mode) != 0) {
11771 11772                                                  status = EFAULT;
11772 11773                                          } else {
11773 11774                                                  *rval = MPTIOCTL_STATUS_GOOD;
11774 11775                                                  status = 0;
11775 11776                                          }
11776 11777                                  } else {
11777 11778                                          *rval = MPTIOCTL_STATUS_GOOD;
11778 11779                                          status = 0;
11779 11780                                  }
11780 11781                          }
11781 11782                  } else {
11782 11783                          *rval = MPTIOCTL_STATUS_LEN_TOO_SHORT;
11783 11784                          status = 0;
11784 11785                  }
11785 11786          } else {
11786 11787                  status = EFAULT;
11787 11788          }
11788 11789  
11789 11790          mutex_exit(&mpt->m_mutex);
11790 11791          return (status);
11791 11792  }
11792 11793  
11793 11794  static void
11794 11795  mptsas_lookup_pci_data(mptsas_t *mpt, mptsas_adapter_data_t *adapter_data)
11795 11796  {
11796 11797          int     *reg_data;
11797 11798          uint_t  reglen;
11798 11799  
11799 11800          /*
11800 11801           * Lookup the 'reg' property and extract the other data
11801 11802           */
11802 11803          if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, mpt->m_dip,
11803 11804              DDI_PROP_DONTPASS, "reg", ®_data, ®len) ==
11804 11805              DDI_PROP_SUCCESS) {
11805 11806                  /*
11806 11807                   * Extract the PCI data from the 'reg' property first DWORD.
11807 11808                   * The entry looks like the following:
11808 11809                   * First DWORD:
11809 11810                   * Bits 0 - 7 8-bit Register number
11810 11811                   * Bits 8 - 10 3-bit Function number
11811 11812                   * Bits 11 - 15 5-bit Device number
11812 11813                   * Bits 16 - 23 8-bit Bus number
11813 11814                   * Bits 24 - 25 2-bit Address Space type identifier
11814 11815                   *
11815 11816                   */
11816 11817                  adapter_data->PciInformation.u.bits.BusNumber =
11817 11818                      (reg_data[0] & 0x00FF0000) >> 16;
11818 11819                  adapter_data->PciInformation.u.bits.DeviceNumber =
11819 11820                      (reg_data[0] & 0x0000F800) >> 11;
11820 11821                  adapter_data->PciInformation.u.bits.FunctionNumber =
11821 11822                      (reg_data[0] & 0x00000700) >> 8;
11822 11823                  ddi_prop_free((void *)reg_data);
11823 11824          } else {
11824 11825                  /*
11825 11826                   * If we can't determine the PCI data then we fill in FF's for
11826 11827                   * the data to indicate this.
11827 11828                   */
11828 11829                  adapter_data->PCIDeviceHwId = 0xFFFFFFFF;
11829 11830                  adapter_data->MpiPortNumber = 0xFFFFFFFF;
11830 11831                  adapter_data->PciInformation.u.AsDWORD = 0xFFFFFFFF;
11831 11832          }
11832 11833  
11833 11834          /*
11834 11835           * Saved in the mpt->m_fwversion
11835 11836           */
11836 11837          adapter_data->MpiFirmwareVersion = mpt->m_fwversion;
11837 11838  }
11838 11839  
11839 11840  static void
11840 11841  mptsas_read_adapter_data(mptsas_t *mpt, mptsas_adapter_data_t *adapter_data)
11841 11842  {
11842 11843          char    *driver_verstr = MPTSAS_MOD_STRING;
11843 11844  
11844 11845          mptsas_lookup_pci_data(mpt, adapter_data);
11845 11846          adapter_data->AdapterType = MPTIOCTL_ADAPTER_TYPE_SAS2;
11846 11847          adapter_data->PCIDeviceHwId = (uint32_t)mpt->m_devid;
11847 11848          adapter_data->PCIDeviceHwRev = (uint32_t)mpt->m_revid;
11848 11849          adapter_data->SubSystemId = (uint32_t)mpt->m_ssid;
11849 11850          adapter_data->SubsystemVendorId = (uint32_t)mpt->m_svid;
11850 11851          (void) strcpy((char *)&adapter_data->DriverVersion[0], driver_verstr);
11851 11852          adapter_data->BiosVersion = 0;
11852 11853          (void) mptsas_get_bios_page3(mpt, &adapter_data->BiosVersion);
11853 11854  }
11854 11855  
11855 11856  static void
11856 11857  mptsas_read_pci_info(mptsas_t *mpt, mptsas_pci_info_t *pci_info)
11857 11858  {
11858 11859          int     *reg_data, i;
11859 11860          uint_t  reglen;
11860 11861  
11861 11862          /*
11862 11863           * Lookup the 'reg' property and extract the other data
11863 11864           */
11864 11865          if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, mpt->m_dip,
11865 11866              DDI_PROP_DONTPASS, "reg", ®_data, ®len) ==
11866 11867              DDI_PROP_SUCCESS) {
11867 11868                  /*
11868 11869                   * Extract the PCI data from the 'reg' property first DWORD.
11869 11870                   * The entry looks like the following:
11870 11871                   * First DWORD:
11871 11872                   * Bits 8 - 10 3-bit Function number
11872 11873                   * Bits 11 - 15 5-bit Device number
11873 11874                   * Bits 16 - 23 8-bit Bus number
11874 11875                   */
11875 11876                  pci_info->BusNumber = (reg_data[0] & 0x00FF0000) >> 16;
11876 11877                  pci_info->DeviceNumber = (reg_data[0] & 0x0000F800) >> 11;
11877 11878                  pci_info->FunctionNumber = (reg_data[0] & 0x00000700) >> 8;
11878 11879                  ddi_prop_free((void *)reg_data);
11879 11880          } else {
11880 11881                  /*
11881 11882                   * If we can't determine the PCI info then we fill in FF's for
11882 11883                   * the data to indicate this.
11883 11884                   */
11884 11885                  pci_info->BusNumber = 0xFFFFFFFF;
11885 11886                  pci_info->DeviceNumber = 0xFF;
11886 11887                  pci_info->FunctionNumber = 0xFF;
11887 11888          }
11888 11889  
11889 11890          /*
11890 11891           * Now get the interrupt vector and the pci header.  The vector can
11891 11892           * only be 0 right now.  The header is the first 256 bytes of config
11892 11893           * space.
11893 11894           */
11894 11895          pci_info->InterruptVector = 0;
11895 11896          for (i = 0; i < sizeof (pci_info->PciHeader); i++) {
11896 11897                  pci_info->PciHeader[i] = pci_config_get8(mpt->m_config_handle,
11897 11898                      i);
11898 11899          }
11899 11900  }
11900 11901  
11901 11902  static int
11902 11903  mptsas_reg_access(mptsas_t *mpt, mptsas_reg_access_t *data, int mode)
11903 11904  {
11904 11905          int                     status = 0;
11905 11906          mptsas_reg_access_t     driverdata;
11906 11907  
11907 11908          mutex_enter(&mpt->m_mutex);
11908 11909          if (ddi_copyin(data, &driverdata, sizeof (driverdata), mode) == 0) {
11909 11910                  switch (driverdata.Command) {
11910 11911                          /*
11911 11912                           * IO access is not supported.
11912 11913                           */
11913 11914                          case REG_IO_READ:
11914 11915                          case REG_IO_WRITE:
11915 11916                                  mptsas_log(mpt, CE_WARN, "IO access is not "
11916 11917                                      "supported.  Use memory access.");
11917 11918                                  status = EINVAL;
11918 11919                                  break;
11919 11920  
11920 11921                          case REG_MEM_READ:
11921 11922                                  driverdata.RegData = ddi_get32(mpt->m_datap,
11922 11923                                      (uint32_t *)(void *)mpt->m_reg +
11923 11924                                      driverdata.RegOffset);
11924 11925                                  if (ddi_copyout(&driverdata.RegData,
11925 11926                                      &data->RegData,
11926 11927                                      sizeof (driverdata.RegData), mode) != 0) {
11927 11928                                          mptsas_log(mpt, CE_WARN, "Register "
11928 11929                                              "Read Failed");
11929 11930                                          status = EFAULT;
11930 11931                                  }
11931 11932                                  break;
11932 11933  
11933 11934                          case REG_MEM_WRITE:
11934 11935                                  ddi_put32(mpt->m_datap,
11935 11936                                      (uint32_t *)(void *)mpt->m_reg +
11936 11937                                      driverdata.RegOffset,
11937 11938                                      driverdata.RegData);
11938 11939                                  break;
11939 11940  
11940 11941                          default:
11941 11942                                  status = EINVAL;
11942 11943                                  break;
11943 11944                  }
11944 11945          } else {
11945 11946                  status = EFAULT;
11946 11947          }
11947 11948  
11948 11949          mutex_exit(&mpt->m_mutex);
11949 11950          return (status);
11950 11951  }
11951 11952  
11952 11953  static int
11953 11954  mptsas_ioctl(dev_t dev, int cmd, intptr_t data, int mode, cred_t *credp,
11954 11955      int *rval)
11955 11956  {
11956 11957          int                     status = 0;
11957 11958          mptsas_t                *mpt;
11958 11959          mptsas_update_flash_t   flashdata;
11959 11960          mptsas_pass_thru_t      passthru_data;
11960 11961          mptsas_adapter_data_t   adapter_data;
11961 11962          mptsas_pci_info_t       pci_info;
11962 11963          int                     copylen;
11963 11964  
11964 11965          int                     iport_flag = 0;
11965 11966          dev_info_t              *dip = NULL;
11966 11967          mptsas_phymask_t        phymask = 0;
11967 11968          struct devctl_iocdata   *dcp = NULL;
11968 11969          uint32_t                slotstatus = 0;
11969 11970          char                    *addr = NULL;
11970 11971          mptsas_target_t         *ptgt = NULL;
11971 11972  
11972 11973          *rval = MPTIOCTL_STATUS_GOOD;
11973 11974          if (secpolicy_sys_config(credp, B_FALSE) != 0) {
11974 11975                  return (EPERM);
11975 11976          }
11976 11977  
11977 11978          mpt = ddi_get_soft_state(mptsas_state, MINOR2INST(getminor(dev)));
11978 11979          if (mpt == NULL) {
11979 11980                  /*
11980 11981                   * Called from iport node, get the states
11981 11982                   */
11982 11983                  iport_flag = 1;
11983 11984                  dip = mptsas_get_dip_from_dev(dev, &phymask);
11984 11985                  if (dip == NULL) {
11985 11986                          return (ENXIO);
11986 11987                  }
11987 11988                  mpt = DIP2MPT(dip);
11988 11989          }
11989 11990          /* Make sure power level is D0 before accessing registers */
11990 11991          mutex_enter(&mpt->m_mutex);
11991 11992          if (mpt->m_options & MPTSAS_OPT_PM) {
11992 11993                  (void) pm_busy_component(mpt->m_dip, 0);
11993 11994                  if (mpt->m_power_level != PM_LEVEL_D0) {
11994 11995                          mutex_exit(&mpt->m_mutex);
11995 11996                          if (pm_raise_power(mpt->m_dip, 0, PM_LEVEL_D0) !=
11996 11997                              DDI_SUCCESS) {
11997 11998                                  mptsas_log(mpt, CE_WARN,
11998 11999                                      "mptsas%d: mptsas_ioctl: Raise power "
11999 12000                                      "request failed.", mpt->m_instance);
12000 12001                                  (void) pm_idle_component(mpt->m_dip, 0);
12001 12002                                  return (ENXIO);
12002 12003                          }
12003 12004                  } else {
12004 12005                          mutex_exit(&mpt->m_mutex);
12005 12006                  }
12006 12007          } else {
12007 12008                  mutex_exit(&mpt->m_mutex);
12008 12009          }
12009 12010  
12010 12011          if (iport_flag) {
12011 12012                  status = scsi_hba_ioctl(dev, cmd, data, mode, credp, rval);
12012 12013                  if (status != 0) {
12013 12014                          goto out;
12014 12015                  }
12015 12016                  /*
12016 12017                   * The following code control the OK2RM LED, it doesn't affect
12017 12018                   * the ioctl return status.
12018 12019                   */
12019 12020                  if ((cmd == DEVCTL_DEVICE_ONLINE) ||
12020 12021                      (cmd == DEVCTL_DEVICE_OFFLINE)) {
12021 12022                          if (ndi_dc_allochdl((void *)data, &dcp) !=
12022 12023                              NDI_SUCCESS) {
12023 12024                                  goto out;
12024 12025                          }
12025 12026                          addr = ndi_dc_getaddr(dcp);
12026 12027                          ptgt = mptsas_addr_to_ptgt(mpt, addr, phymask);
12027 12028                          if (ptgt == NULL) {
12028 12029                                  NDBG14(("mptsas_ioctl led control: tgt %s not "
12029 12030                                      "found", addr));
12030 12031                                  ndi_dc_freehdl(dcp);
12031 12032                                  goto out;
12032 12033                          }
12033 12034                          mutex_enter(&mpt->m_mutex);
12034 12035                          if (cmd == DEVCTL_DEVICE_ONLINE) {
12035 12036                                  ptgt->m_tgt_unconfigured = 0;
12036 12037                          } else if (cmd == DEVCTL_DEVICE_OFFLINE) {
12037 12038                                  ptgt->m_tgt_unconfigured = 1;
12038 12039                          }
12039 12040                          slotstatus = 0;
12040 12041  #ifdef MPTSAS_GET_LED
12041 12042                          /*
12042 12043                           * The get led status can't get a valid/reasonable
12043 12044                           * state, so ignore the get led status, and write the
12044 12045                           * required value directly
12045 12046                           */
12046 12047                          if (mptsas_get_led_status(mpt, ptgt, &slotstatus) !=
12047 12048                              DDI_SUCCESS) {
12048 12049                                  NDBG14(("mptsas_ioctl: get LED for tgt %s "
12049 12050                                      "failed %x", addr, slotstatus));
12050 12051                                  slotstatus = 0;
12051 12052                          }
12052 12053                          NDBG14(("mptsas_ioctl: LED status %x for %s",
12053 12054                              slotstatus, addr));
12054 12055  #endif
12055 12056                          if (cmd == DEVCTL_DEVICE_OFFLINE) {
12056 12057                                  slotstatus |=
12057 12058                                      MPI2_SEP_REQ_SLOTSTATUS_REQUEST_REMOVE;
12058 12059                          } else {
12059 12060                                  slotstatus &=
12060 12061                                      ~MPI2_SEP_REQ_SLOTSTATUS_REQUEST_REMOVE;
12061 12062                          }
12062 12063                          if (mptsas_set_led_status(mpt, ptgt, slotstatus) !=
12063 12064                              DDI_SUCCESS) {
12064 12065                                  NDBG14(("mptsas_ioctl: set LED for tgt %s "
12065 12066                                      "failed %x", addr, slotstatus));
12066 12067                          }
12067 12068                          mutex_exit(&mpt->m_mutex);
12068 12069                          ndi_dc_freehdl(dcp);
12069 12070                  }
12070 12071                  goto out;
12071 12072          }
12072 12073          switch (cmd) {
12073 12074                  case MPTIOCTL_UPDATE_FLASH:
12074 12075                          if (ddi_copyin((void *)data, &flashdata,
12075 12076                                  sizeof (struct mptsas_update_flash), mode)) {
12076 12077                                  status = EFAULT;
12077 12078                                  break;
12078 12079                          }
12079 12080  
12080 12081                          mutex_enter(&mpt->m_mutex);
12081 12082                          if (mptsas_update_flash(mpt,
12082 12083                              (caddr_t)(long)flashdata.PtrBuffer,
12083 12084                              flashdata.ImageSize, flashdata.ImageType, mode)) {
12084 12085                                  status = EFAULT;
12085 12086                          }
12086 12087  
12087 12088                          /*
12088 12089                           * Reset the chip to start using the new
12089 12090                           * firmware.  Reset if failed also.
12090 12091                           */
12091 12092                          mpt->m_softstate &= ~MPTSAS_SS_MSG_UNIT_RESET;
12092 12093                          if (mptsas_restart_ioc(mpt) == DDI_FAILURE) {
12093 12094                                  status = EFAULT;
12094 12095                          }
12095 12096                          mutex_exit(&mpt->m_mutex);
12096 12097                          break;
12097 12098                  case MPTIOCTL_PASS_THRU:
12098 12099                          /*
12099 12100                           * The user has requested to pass through a command to
12100 12101                           * be executed by the MPT firmware.  Call our routine
12101 12102                           * which does this.  Only allow one passthru IOCTL at
12102 12103                           * one time. Other threads will block on
12103 12104                           * m_passthru_mutex, which is of adaptive variant.
12104 12105                           */
12105 12106                          if (ddi_copyin((void *)data, &passthru_data,
12106 12107                              sizeof (mptsas_pass_thru_t), mode)) {
12107 12108                                  status = EFAULT;
12108 12109                                  break;
12109 12110                          }
12110 12111                          mutex_enter(&mpt->m_passthru_mutex);
12111 12112                          mutex_enter(&mpt->m_mutex);
12112 12113                          status = mptsas_pass_thru(mpt, &passthru_data, mode);
12113 12114                          mutex_exit(&mpt->m_mutex);
12114 12115                          mutex_exit(&mpt->m_passthru_mutex);
12115 12116  
12116 12117                          break;
12117 12118                  case MPTIOCTL_GET_ADAPTER_DATA:
12118 12119                          /*
12119 12120                           * The user has requested to read adapter data.  Call
12120 12121                           * our routine which does this.
12121 12122                           */
12122 12123                          bzero(&adapter_data, sizeof (mptsas_adapter_data_t));
12123 12124                          if (ddi_copyin((void *)data, (void *)&adapter_data,
12124 12125                              sizeof (mptsas_adapter_data_t), mode)) {
12125 12126                                  status = EFAULT;
12126 12127                                  break;
12127 12128                          }
12128 12129                          if (adapter_data.StructureLength >=
12129 12130                              sizeof (mptsas_adapter_data_t)) {
12130 12131                                  adapter_data.StructureLength = (uint32_t)
12131 12132                                      sizeof (mptsas_adapter_data_t);
12132 12133                                  copylen = sizeof (mptsas_adapter_data_t);
12133 12134                                  mutex_enter(&mpt->m_mutex);
12134 12135                                  mptsas_read_adapter_data(mpt, &adapter_data);
12135 12136                                  mutex_exit(&mpt->m_mutex);
12136 12137                          } else {
12137 12138                                  adapter_data.StructureLength = (uint32_t)
12138 12139                                      sizeof (mptsas_adapter_data_t);
12139 12140                                  copylen = sizeof (adapter_data.StructureLength);
12140 12141                                  *rval = MPTIOCTL_STATUS_LEN_TOO_SHORT;
12141 12142                          }
12142 12143                          if (ddi_copyout((void *)(&adapter_data), (void *)data,
12143 12144                              copylen, mode) != 0) {
12144 12145                                  status = EFAULT;
12145 12146                          }
12146 12147                          break;
12147 12148                  case MPTIOCTL_GET_PCI_INFO:
12148 12149                          /*
12149 12150                           * The user has requested to read pci info.  Call
12150 12151                           * our routine which does this.
12151 12152                           */
12152 12153                          bzero(&pci_info, sizeof (mptsas_pci_info_t));
12153 12154                          mutex_enter(&mpt->m_mutex);
12154 12155                          mptsas_read_pci_info(mpt, &pci_info);
12155 12156                          mutex_exit(&mpt->m_mutex);
12156 12157                          if (ddi_copyout((void *)(&pci_info), (void *)data,
12157 12158                              sizeof (mptsas_pci_info_t), mode) != 0) {
12158 12159                                  status = EFAULT;
12159 12160                          }
12160 12161                          break;
12161 12162                  case MPTIOCTL_RESET_ADAPTER:
12162 12163                          mutex_enter(&mpt->m_mutex);
12163 12164                          mpt->m_softstate &= ~MPTSAS_SS_MSG_UNIT_RESET;
12164 12165                          if ((mptsas_restart_ioc(mpt)) == DDI_FAILURE) {
12165 12166                                  mptsas_log(mpt, CE_WARN, "reset adapter IOCTL "
12166 12167                                      "failed");
12167 12168                                  status = EFAULT;
12168 12169                          }
12169 12170                          mutex_exit(&mpt->m_mutex);
12170 12171                          break;
12171 12172                  case MPTIOCTL_DIAG_ACTION:
12172 12173                          /*
12173 12174                           * The user has done a diag buffer action.  Call our
12174 12175                           * routine which does this.  Only allow one diag action
12175 12176                           * at one time.
12176 12177                           */
12177 12178                          mutex_enter(&mpt->m_mutex);
12178 12179                          if (mpt->m_diag_action_in_progress) {
12179 12180                                  mutex_exit(&mpt->m_mutex);
12180 12181                                  return (EBUSY);
12181 12182                          }
12182 12183                          mpt->m_diag_action_in_progress = 1;
12183 12184                          status = mptsas_diag_action(mpt,
12184 12185                              (mptsas_diag_action_t *)data, mode);
12185 12186                          mpt->m_diag_action_in_progress = 0;
12186 12187                          mutex_exit(&mpt->m_mutex);
12187 12188                          break;
12188 12189                  case MPTIOCTL_EVENT_QUERY:
12189 12190                          /*
12190 12191                           * The user has done an event query. Call our routine
12191 12192                           * which does this.
12192 12193                           */
12193 12194                          status = mptsas_event_query(mpt,
12194 12195                              (mptsas_event_query_t *)data, mode, rval);
12195 12196                          break;
12196 12197                  case MPTIOCTL_EVENT_ENABLE:
12197 12198                          /*
12198 12199                           * The user has done an event enable. Call our routine
12199 12200                           * which does this.
12200 12201                           */
12201 12202                          status = mptsas_event_enable(mpt,
12202 12203                              (mptsas_event_enable_t *)data, mode, rval);
12203 12204                          break;
12204 12205                  case MPTIOCTL_EVENT_REPORT:
12205 12206                          /*
12206 12207                           * The user has done an event report. Call our routine
12207 12208                           * which does this.
12208 12209                           */
12209 12210                          status = mptsas_event_report(mpt,
12210 12211                              (mptsas_event_report_t *)data, mode, rval);
12211 12212                          break;
12212 12213                  case MPTIOCTL_REG_ACCESS:
12213 12214                          /*
12214 12215                           * The user has requested register access.  Call our
12215 12216                           * routine which does this.
12216 12217                           */
12217 12218                          status = mptsas_reg_access(mpt,
12218 12219                              (mptsas_reg_access_t *)data, mode);
12219 12220                          break;
12220 12221                  default:
12221 12222                          status = scsi_hba_ioctl(dev, cmd, data, mode, credp,
12222 12223                              rval);
12223 12224                          break;
12224 12225          }
12225 12226  
12226 12227  out:
12227 12228          if (mpt->m_options & MPTSAS_OPT_PM)
12228 12229                  (void) pm_idle_component(mpt->m_dip, 0);
12229 12230          return (status);
12230 12231  }
12231 12232  
12232 12233  int
12233 12234  mptsas_restart_ioc(mptsas_t *mpt)
12234 12235  {
12235 12236          int             rval = DDI_SUCCESS;
12236 12237          mptsas_target_t *ptgt = NULL;
12237 12238  
12238 12239          ASSERT(mutex_owned(&mpt->m_mutex));
12239 12240  
12240 12241          /*
12241 12242           * Set a flag telling I/O path that we're processing a reset.  This is
12242 12243           * needed because after the reset is complete, the hash table still
12243 12244           * needs to be rebuilt.  If I/Os are started before the hash table is
12244 12245           * rebuilt, I/O errors will occur.  This flag allows I/Os to be marked
12245 12246           * so that they can be retried.
12246 12247           */
12247 12248          mpt->m_in_reset = TRUE;
12248 12249  
12249 12250          /*
12250 12251           * Set all throttles to HOLD
12251 12252           */
12252 12253          ptgt = (mptsas_target_t *)mptsas_hash_traverse(&mpt->m_active->m_tgttbl,
12253 12254              MPTSAS_HASH_FIRST);
12254 12255          while (ptgt != NULL) {
12255 12256                  mutex_enter(&ptgt->m_tgt_intr_mutex);
12256 12257                  mptsas_set_throttle(mpt, ptgt, HOLD_THROTTLE);
12257 12258                  mutex_exit(&ptgt->m_tgt_intr_mutex);
12258 12259  
12259 12260                  ptgt = (mptsas_target_t *)mptsas_hash_traverse(
12260 12261                      &mpt->m_active->m_tgttbl, MPTSAS_HASH_NEXT);
12261 12262          }
12262 12263  
12263 12264          /*
12264 12265           * Disable interrupts
12265 12266           */
12266 12267          MPTSAS_DISABLE_INTR(mpt);
12267 12268  
12268 12269          /*
12269 12270           * Abort all commands: outstanding commands, commands in waitq
12270 12271           */
12271 12272          mptsas_flush_hba(mpt);
12272 12273  
12273 12274          /*
12274 12275           * Reinitialize the chip.
12275 12276           */
12276 12277          if (mptsas_init_chip(mpt, FALSE) == DDI_FAILURE) {
12277 12278                  rval = DDI_FAILURE;
12278 12279          }
12279 12280  
12280 12281          /*
12281 12282           * Enable interrupts again
12282 12283           */
12283 12284          MPTSAS_ENABLE_INTR(mpt);
12284 12285  
12285 12286          /*
12286 12287           * If mptsas_init_chip was successful, update the driver data.
12287 12288           */
12288 12289          if (rval == DDI_SUCCESS) {
12289 12290                  mptsas_update_driver_data(mpt);
12290 12291          }
12291 12292  
12292 12293          /*
12293 12294           * Reset the throttles
12294 12295           */
12295 12296          ptgt = (mptsas_target_t *)mptsas_hash_traverse(&mpt->m_active->m_tgttbl,
12296 12297              MPTSAS_HASH_FIRST);
12297 12298          while (ptgt != NULL) {
12298 12299                  mutex_enter(&ptgt->m_tgt_intr_mutex);
12299 12300                  mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
12300 12301                  mutex_exit(&ptgt->m_tgt_intr_mutex);
12301 12302  
12302 12303                  ptgt = (mptsas_target_t *)mptsas_hash_traverse(
12303 12304                      &mpt->m_active->m_tgttbl, MPTSAS_HASH_NEXT);
12304 12305          }
12305 12306  
12306 12307          mptsas_doneq_empty(mpt);
12307 12308          mptsas_restart_hba(mpt);
12308 12309  
12309 12310          if (rval != DDI_SUCCESS) {
12310 12311                  mptsas_fm_ereport(mpt, DDI_FM_DEVICE_NO_RESPONSE);
12311 12312                  ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_LOST);
12312 12313          }
12313 12314  
12314 12315          /*
12315 12316           * Clear the reset flag so that I/Os can continue.
12316 12317           */
12317 12318          mpt->m_in_reset = FALSE;
12318 12319  
12319 12320          return (rval);
12320 12321  }
12321 12322  
12322 12323  static int
12323 12324  mptsas_init_chip(mptsas_t *mpt, int first_time)
12324 12325  {
12325 12326          ddi_dma_cookie_t        cookie;
12326 12327          uint32_t                i;
12327 12328          int                     rval;
12328 12329  
12329 12330          /*
12330 12331           * Check to see if the firmware image is valid
  
    | 
      ↓ open down ↓ | 
    10556 lines elided | 
    
      ↑ open up ↑ | 
  
12331 12332           */
12332 12333          if (ddi_get32(mpt->m_datap, &mpt->m_reg->HostDiagnostic) &
12333 12334              MPI2_DIAG_FLASH_BAD_SIG) {
12334 12335                  mptsas_log(mpt, CE_WARN, "mptsas bad flash signature!");
12335 12336                  goto fail;
12336 12337          }
12337 12338  
12338 12339          /*
12339 12340           * Reset the chip
12340 12341           */
12341      -        rval = mptsas_ioc_reset(mpt);
     12342 +        rval = mptsas_ioc_reset(mpt, first_time);
12342 12343          if (rval == MPTSAS_RESET_FAIL) {
12343 12344                  mptsas_log(mpt, CE_WARN, "hard reset failed!");
12344 12345                  goto fail;
12345 12346          }
12346 12347  
12347 12348          if ((rval == MPTSAS_SUCCESS_MUR) && (!first_time)) {
12348 12349                  goto mur;
12349 12350          }
12350 12351          /*
12351 12352           * Setup configuration space
12352 12353           */
12353 12354          if (mptsas_config_space_init(mpt) == FALSE) {
12354 12355                  mptsas_log(mpt, CE_WARN, "mptsas_config_space_init "
12355 12356                      "failed!");
12356 12357                  goto fail;
12357 12358          }
12358 12359  
12359 12360          /*
12360 12361           * IOC facts can change after a diag reset so all buffers that are
12361 12362           * based on these numbers must be de-allocated and re-allocated.  Get
12362 12363           * new IOC facts each time chip is initialized.
12363 12364           */
12364 12365          if (mptsas_ioc_get_facts(mpt) == DDI_FAILURE) {
12365 12366                  mptsas_log(mpt, CE_WARN, "mptsas_ioc_get_facts failed");
12366 12367                  goto fail;
12367 12368          }
12368 12369  
12369 12370          if (mptsas_alloc_active_slots(mpt, KM_SLEEP)) {
12370 12371                  goto fail;
12371 12372          }
12372 12373          /*
12373 12374           * Allocate request message frames, reply free queue, reply descriptor
12374 12375           * post queue, and reply message frames using latest IOC facts.
12375 12376           */
12376 12377          if (mptsas_alloc_request_frames(mpt) == DDI_FAILURE) {
12377 12378                  mptsas_log(mpt, CE_WARN, "mptsas_alloc_request_frames failed");
12378 12379                  goto fail;
12379 12380          }
12380 12381          if (mptsas_alloc_free_queue(mpt) == DDI_FAILURE) {
12381 12382                  mptsas_log(mpt, CE_WARN, "mptsas_alloc_free_queue failed!");
12382 12383                  goto fail;
12383 12384          }
12384 12385          if (mptsas_alloc_post_queue(mpt) == DDI_FAILURE) {
12385 12386                  mptsas_log(mpt, CE_WARN, "mptsas_alloc_post_queue failed!");
12386 12387                  goto fail;
12387 12388          }
12388 12389          if (mptsas_alloc_reply_frames(mpt) == DDI_FAILURE) {
12389 12390                  mptsas_log(mpt, CE_WARN, "mptsas_alloc_reply_frames failed!");
12390 12391                  goto fail;
12391 12392          }
12392 12393  
12393 12394  mur:
12394 12395          /*
12395 12396           * Re-Initialize ioc to operational state
12396 12397           */
12397 12398          if (mptsas_ioc_init(mpt) == DDI_FAILURE) {
12398 12399                  mptsas_log(mpt, CE_WARN, "mptsas_ioc_init failed");
12399 12400                  goto fail;
12400 12401          }
12401 12402  
12402 12403          mptsas_alloc_reply_args(mpt);
12403 12404  
12404 12405          /*
12405 12406           * Initialize reply post index.  Reply free index is initialized after
12406 12407           * the next loop.
12407 12408           */
12408 12409          mpt->m_post_index = 0;
12409 12410  
12410 12411          /*
12411 12412           * Initialize the Reply Free Queue with the physical addresses of our
12412 12413           * reply frames.
12413 12414           */
12414 12415          cookie.dmac_address = mpt->m_reply_frame_dma_addr;
12415 12416          for (i = 0; i < mpt->m_max_replies; i++) {
12416 12417                  ddi_put32(mpt->m_acc_free_queue_hdl,
12417 12418                      &((uint32_t *)(void *)mpt->m_free_queue)[i],
12418 12419                      cookie.dmac_address);
12419 12420                  cookie.dmac_address += mpt->m_reply_frame_size;
12420 12421          }
12421 12422          (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
12422 12423              DDI_DMA_SYNC_FORDEV);
12423 12424  
12424 12425          /*
12425 12426           * Initialize the reply free index to one past the last frame on the
12426 12427           * queue.  This will signify that the queue is empty to start with.
12427 12428           */
12428 12429          mpt->m_free_index = i;
12429 12430          ddi_put32(mpt->m_datap, &mpt->m_reg->ReplyFreeHostIndex, i);
12430 12431  
12431 12432          /*
12432 12433           * Initialize the reply post queue to 0xFFFFFFFF,0xFFFFFFFF's.
12433 12434           */
12434 12435          for (i = 0; i < mpt->m_post_queue_depth; i++) {
12435 12436                  ddi_put64(mpt->m_acc_post_queue_hdl,
12436 12437                      &((uint64_t *)(void *)mpt->m_post_queue)[i],
12437 12438                      0xFFFFFFFFFFFFFFFF);
12438 12439          }
12439 12440          (void) ddi_dma_sync(mpt->m_dma_post_queue_hdl, 0, 0,
12440 12441              DDI_DMA_SYNC_FORDEV);
12441 12442  
12442 12443          /*
12443 12444           * Enable ports
12444 12445           */
12445 12446          if (mptsas_ioc_enable_port(mpt) == DDI_FAILURE) {
12446 12447                  mptsas_log(mpt, CE_WARN, "mptsas_ioc_enable_port failed");
12447 12448                  goto fail;
12448 12449          }
12449 12450  
12450 12451          /*
12451 12452           * enable events
12452 12453           */
12453 12454          if (mptsas_ioc_enable_event_notification(mpt)) {
12454 12455                  goto fail;
12455 12456          }
12456 12457  
12457 12458          /*
12458 12459           * We need checks in attach and these.
12459 12460           * chip_init is called in mult. places
12460 12461           */
12461 12462  
12462 12463          if ((mptsas_check_dma_handle(mpt->m_dma_req_frame_hdl) !=
12463 12464              DDI_SUCCESS) ||
12464 12465              (mptsas_check_dma_handle(mpt->m_dma_reply_frame_hdl) !=
12465 12466              DDI_SUCCESS) ||
12466 12467              (mptsas_check_dma_handle(mpt->m_dma_free_queue_hdl) !=
12467 12468              DDI_SUCCESS) ||
12468 12469              (mptsas_check_dma_handle(mpt->m_dma_post_queue_hdl) !=
12469 12470              DDI_SUCCESS) ||
12470 12471              (mptsas_check_dma_handle(mpt->m_hshk_dma_hdl) !=
12471 12472              DDI_SUCCESS)) {
12472 12473                  ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
12473 12474                  goto fail;
12474 12475          }
12475 12476  
12476 12477          /* Check all acc handles */
12477 12478          if ((mptsas_check_acc_handle(mpt->m_datap) != DDI_SUCCESS) ||
12478 12479              (mptsas_check_acc_handle(mpt->m_acc_req_frame_hdl) !=
12479 12480              DDI_SUCCESS) ||
12480 12481              (mptsas_check_acc_handle(mpt->m_acc_reply_frame_hdl) !=
12481 12482              DDI_SUCCESS) ||
12482 12483              (mptsas_check_acc_handle(mpt->m_acc_free_queue_hdl) !=
12483 12484              DDI_SUCCESS) ||
12484 12485              (mptsas_check_acc_handle(mpt->m_acc_post_queue_hdl) !=
12485 12486              DDI_SUCCESS) ||
12486 12487              (mptsas_check_acc_handle(mpt->m_hshk_acc_hdl) !=
12487 12488              DDI_SUCCESS) ||
12488 12489              (mptsas_check_acc_handle(mpt->m_config_handle) !=
12489 12490              DDI_SUCCESS)) {
12490 12491                  ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
12491 12492                  goto fail;
12492 12493          }
12493 12494  
12494 12495          return (DDI_SUCCESS);
12495 12496  
12496 12497  fail:
12497 12498          return (DDI_FAILURE);
12498 12499  }
12499 12500  
12500 12501  static int
12501 12502  mptsas_get_pci_cap(mptsas_t *mpt)
12502 12503  {
12503 12504          ushort_t caps_ptr, cap, cap_count;
12504 12505  
12505 12506          if (mpt->m_config_handle == NULL)
12506 12507                  return (FALSE);
12507 12508          /*
12508 12509           * Check if capabilities list is supported and if so,
12509 12510           * get initial capabilities pointer and clear bits 0,1.
12510 12511           */
12511 12512          if (pci_config_get16(mpt->m_config_handle, PCI_CONF_STAT)
12512 12513              & PCI_STAT_CAP) {
12513 12514                  caps_ptr = P2ALIGN(pci_config_get8(mpt->m_config_handle,
12514 12515                      PCI_CONF_CAP_PTR), 4);
12515 12516          } else {
12516 12517                  caps_ptr = PCI_CAP_NEXT_PTR_NULL;
12517 12518          }
12518 12519  
12519 12520          /*
12520 12521           * Walk capabilities if supported.
12521 12522           */
12522 12523          for (cap_count = 0; caps_ptr != PCI_CAP_NEXT_PTR_NULL; ) {
12523 12524  
12524 12525                  /*
12525 12526                   * Check that we haven't exceeded the maximum number of
12526 12527                   * capabilities and that the pointer is in a valid range.
12527 12528                   */
12528 12529                  if (++cap_count > 48) {
12529 12530                          mptsas_log(mpt, CE_WARN,
12530 12531                              "too many device capabilities.\n");
12531 12532                          break;
12532 12533                  }
12533 12534                  if (caps_ptr < 64) {
12534 12535                          mptsas_log(mpt, CE_WARN,
12535 12536                              "capabilities pointer 0x%x out of range.\n",
12536 12537                              caps_ptr);
12537 12538                          break;
12538 12539                  }
12539 12540  
12540 12541                  /*
12541 12542                   * Get next capability and check that it is valid.
12542 12543                   * For now, we only support power management.
12543 12544                   */
12544 12545                  cap = pci_config_get8(mpt->m_config_handle, caps_ptr);
12545 12546                  switch (cap) {
12546 12547                          case PCI_CAP_ID_PM:
12547 12548                                  mptsas_log(mpt, CE_NOTE,
12548 12549                                      "?mptsas%d supports power management.\n",
12549 12550                                      mpt->m_instance);
12550 12551                                  mpt->m_options |= MPTSAS_OPT_PM;
12551 12552  
12552 12553                                  /* Save PMCSR offset */
12553 12554                                  mpt->m_pmcsr_offset = caps_ptr + PCI_PMCSR;
12554 12555                                  break;
12555 12556                          /*
12556 12557                           * The following capabilities are valid.  Any others
12557 12558                           * will cause a message to be logged.
12558 12559                           */
12559 12560                          case PCI_CAP_ID_VPD:
12560 12561                          case PCI_CAP_ID_MSI:
12561 12562                          case PCI_CAP_ID_PCIX:
12562 12563                          case PCI_CAP_ID_PCI_E:
12563 12564                          case PCI_CAP_ID_MSI_X:
12564 12565                                  break;
12565 12566                          default:
12566 12567                                  mptsas_log(mpt, CE_NOTE,
12567 12568                                      "?mptsas%d unrecognized capability "
12568 12569                                      "0x%x.\n", mpt->m_instance, cap);
12569 12570                                  break;
12570 12571                  }
12571 12572  
12572 12573                  /*
12573 12574                   * Get next capabilities pointer and clear bits 0,1.
12574 12575                   */
12575 12576                  caps_ptr = P2ALIGN(pci_config_get8(mpt->m_config_handle,
12576 12577                      (caps_ptr + PCI_CAP_NEXT_PTR)), 4);
12577 12578          }
12578 12579          return (TRUE);
12579 12580  }
12580 12581  
12581 12582  static int
12582 12583  mptsas_init_pm(mptsas_t *mpt)
12583 12584  {
12584 12585          char            pmc_name[16];
12585 12586          char            *pmc[] = {
12586 12587                                  NULL,
12587 12588                                  "0=Off (PCI D3 State)",
12588 12589                                  "3=On (PCI D0 State)",
12589 12590                                  NULL
12590 12591                          };
12591 12592          uint16_t        pmcsr_stat;
12592 12593  
12593 12594          if (mptsas_get_pci_cap(mpt) == FALSE) {
12594 12595                  return (DDI_FAILURE);
12595 12596          }
12596 12597          /*
12597 12598           * If PCI's capability does not support PM, then don't need
12598 12599           * to registe the pm-components
12599 12600           */
12600 12601          if (!(mpt->m_options & MPTSAS_OPT_PM))
12601 12602                  return (DDI_SUCCESS);
12602 12603          /*
12603 12604           * If power management is supported by this chip, create
12604 12605           * pm-components property for the power management framework
12605 12606           */
12606 12607          (void) sprintf(pmc_name, "NAME=mptsas%d", mpt->m_instance);
12607 12608          pmc[0] = pmc_name;
12608 12609          if (ddi_prop_update_string_array(DDI_DEV_T_NONE, mpt->m_dip,
12609 12610              "pm-components", pmc, 3) != DDI_PROP_SUCCESS) {
12610 12611                  mutex_enter(&mpt->m_intr_mutex);
12611 12612                  mpt->m_options &= ~MPTSAS_OPT_PM;
12612 12613                  mutex_exit(&mpt->m_intr_mutex);
12613 12614                  mptsas_log(mpt, CE_WARN,
12614 12615                      "mptsas%d: pm-component property creation failed.",
12615 12616                      mpt->m_instance);
12616 12617                  return (DDI_FAILURE);
12617 12618          }
12618 12619  
12619 12620          /*
12620 12621           * Power on device.
12621 12622           */
12622 12623          (void) pm_busy_component(mpt->m_dip, 0);
12623 12624          pmcsr_stat = pci_config_get16(mpt->m_config_handle,
12624 12625              mpt->m_pmcsr_offset);
12625 12626          if ((pmcsr_stat & PCI_PMCSR_STATE_MASK) != PCI_PMCSR_D0) {
12626 12627                  mptsas_log(mpt, CE_WARN, "mptsas%d: Power up the device",
12627 12628                      mpt->m_instance);
12628 12629                  pci_config_put16(mpt->m_config_handle, mpt->m_pmcsr_offset,
12629 12630                      PCI_PMCSR_D0);
12630 12631          }
12631 12632          if (pm_power_has_changed(mpt->m_dip, 0, PM_LEVEL_D0) != DDI_SUCCESS) {
12632 12633                  mptsas_log(mpt, CE_WARN, "pm_power_has_changed failed");
12633 12634                  return (DDI_FAILURE);
12634 12635          }
12635 12636          mutex_enter(&mpt->m_intr_mutex);
12636 12637          mpt->m_power_level = PM_LEVEL_D0;
12637 12638          mutex_exit(&mpt->m_intr_mutex);
12638 12639          /*
12639 12640           * Set pm idle delay.
12640 12641           */
12641 12642          mpt->m_pm_idle_delay = ddi_prop_get_int(DDI_DEV_T_ANY,
12642 12643              mpt->m_dip, 0, "mptsas-pm-idle-delay", MPTSAS_PM_IDLE_TIMEOUT);
12643 12644  
12644 12645          return (DDI_SUCCESS);
12645 12646  }
12646 12647  
12647 12648  static int
12648 12649  mptsas_register_intrs(mptsas_t *mpt)
12649 12650  {
12650 12651          dev_info_t *dip;
12651 12652          int intr_types;
12652 12653  
12653 12654          dip = mpt->m_dip;
12654 12655  
12655 12656          /* Get supported interrupt types */
12656 12657          if (ddi_intr_get_supported_types(dip, &intr_types) != DDI_SUCCESS) {
12657 12658                  mptsas_log(mpt, CE_WARN, "ddi_intr_get_supported_types "
12658 12659                      "failed\n");
12659 12660                  return (FALSE);
12660 12661          }
12661 12662  
12662 12663          NDBG6(("ddi_intr_get_supported_types() returned: 0x%x", intr_types));
12663 12664  
12664 12665          /*
12665 12666           * Try MSI, but fall back to FIXED
12666 12667           */
12667 12668          if (mptsas_enable_msi && (intr_types & DDI_INTR_TYPE_MSI)) {
12668 12669                  if (mptsas_add_intrs(mpt, DDI_INTR_TYPE_MSI) == DDI_SUCCESS) {
12669 12670                          NDBG0(("Using MSI interrupt type"));
12670 12671                          mpt->m_intr_type = DDI_INTR_TYPE_MSI;
12671 12672                          return (TRUE);
12672 12673                  }
12673 12674          }
12674 12675          if (intr_types & DDI_INTR_TYPE_FIXED) {
12675 12676                  if (mptsas_add_intrs(mpt, DDI_INTR_TYPE_FIXED) == DDI_SUCCESS) {
12676 12677                          NDBG0(("Using FIXED interrupt type"));
12677 12678                          mpt->m_intr_type = DDI_INTR_TYPE_FIXED;
12678 12679                          return (TRUE);
12679 12680                  } else {
12680 12681                          NDBG0(("FIXED interrupt registration failed"));
12681 12682                          return (FALSE);
12682 12683                  }
12683 12684          }
12684 12685  
12685 12686          return (FALSE);
12686 12687  }
12687 12688  
12688 12689  static void
12689 12690  mptsas_unregister_intrs(mptsas_t *mpt)
12690 12691  {
12691 12692          mptsas_rem_intrs(mpt);
12692 12693  }
12693 12694  
12694 12695  /*
12695 12696   * mptsas_add_intrs:
12696 12697   *
12697 12698   * Register FIXED or MSI interrupts.
12698 12699   */
12699 12700  static int
12700 12701  mptsas_add_intrs(mptsas_t *mpt, int intr_type)
12701 12702  {
12702 12703          dev_info_t      *dip = mpt->m_dip;
12703 12704          int             avail, actual, count = 0;
12704 12705          int             i, flag, ret;
12705 12706  
12706 12707          NDBG6(("mptsas_add_intrs:interrupt type 0x%x", intr_type));
12707 12708  
12708 12709          /* Get number of interrupts */
12709 12710          ret = ddi_intr_get_nintrs(dip, intr_type, &count);
12710 12711          if ((ret != DDI_SUCCESS) || (count <= 0)) {
12711 12712                  mptsas_log(mpt, CE_WARN, "ddi_intr_get_nintrs() failed, "
12712 12713                      "ret %d count %d\n", ret, count);
12713 12714  
12714 12715                  return (DDI_FAILURE);
12715 12716          }
12716 12717  
12717 12718          /* Get number of available interrupts */
12718 12719          ret = ddi_intr_get_navail(dip, intr_type, &avail);
12719 12720          if ((ret != DDI_SUCCESS) || (avail == 0)) {
12720 12721                  mptsas_log(mpt, CE_WARN, "ddi_intr_get_navail() failed, "
12721 12722                      "ret %d avail %d\n", ret, avail);
12722 12723  
12723 12724                  return (DDI_FAILURE);
12724 12725          }
12725 12726  
12726 12727          if (avail < count) {
12727 12728                  mptsas_log(mpt, CE_NOTE, "ddi_intr_get_nvail returned %d, "
12728 12729                      "navail() returned %d", count, avail);
12729 12730          }
12730 12731  
12731 12732          /* Mpt only have one interrupt routine */
12732 12733          if ((intr_type == DDI_INTR_TYPE_MSI) && (count > 1)) {
12733 12734                  count = 1;
12734 12735          }
12735 12736  
12736 12737          /* Allocate an array of interrupt handles */
12737 12738          mpt->m_intr_size = count * sizeof (ddi_intr_handle_t);
12738 12739          mpt->m_htable = kmem_alloc(mpt->m_intr_size, KM_SLEEP);
12739 12740  
12740 12741          flag = DDI_INTR_ALLOC_NORMAL;
12741 12742  
12742 12743          /* call ddi_intr_alloc() */
12743 12744          ret = ddi_intr_alloc(dip, mpt->m_htable, intr_type, 0,
12744 12745              count, &actual, flag);
12745 12746  
12746 12747          if ((ret != DDI_SUCCESS) || (actual == 0)) {
12747 12748                  mptsas_log(mpt, CE_WARN, "ddi_intr_alloc() failed, ret %d\n",
12748 12749                      ret);
12749 12750                  kmem_free(mpt->m_htable, mpt->m_intr_size);
12750 12751                  return (DDI_FAILURE);
12751 12752          }
12752 12753  
12753 12754          /* use interrupt count returned or abort? */
12754 12755          if (actual < count) {
12755 12756                  mptsas_log(mpt, CE_NOTE, "Requested: %d, Received: %d\n",
12756 12757                      count, actual);
12757 12758          }
12758 12759  
12759 12760          mpt->m_intr_cnt = actual;
12760 12761  
12761 12762          /*
12762 12763           * Get priority for first msi, assume remaining are all the same
12763 12764           */
12764 12765          if ((ret = ddi_intr_get_pri(mpt->m_htable[0],
12765 12766              &mpt->m_intr_pri)) != DDI_SUCCESS) {
12766 12767                  mptsas_log(mpt, CE_WARN, "ddi_intr_get_pri() failed %d\n", ret);
12767 12768  
12768 12769                  /* Free already allocated intr */
12769 12770                  for (i = 0; i < actual; i++) {
12770 12771                          (void) ddi_intr_free(mpt->m_htable[i]);
12771 12772                  }
12772 12773  
12773 12774                  kmem_free(mpt->m_htable, mpt->m_intr_size);
12774 12775                  return (DDI_FAILURE);
12775 12776          }
12776 12777  
12777 12778          /* Test for high level mutex */
12778 12779          if (mpt->m_intr_pri >= ddi_intr_get_hilevel_pri()) {
12779 12780                  mptsas_log(mpt, CE_WARN, "mptsas_add_intrs: "
12780 12781                      "Hi level interrupt not supported\n");
12781 12782  
12782 12783                  /* Free already allocated intr */
12783 12784                  for (i = 0; i < actual; i++) {
12784 12785                          (void) ddi_intr_free(mpt->m_htable[i]);
12785 12786                  }
12786 12787  
12787 12788                  kmem_free(mpt->m_htable, mpt->m_intr_size);
12788 12789                  return (DDI_FAILURE);
12789 12790          }
12790 12791  
12791 12792          /* Call ddi_intr_add_handler() */
12792 12793          for (i = 0; i < actual; i++) {
12793 12794                  if ((ret = ddi_intr_add_handler(mpt->m_htable[i], mptsas_intr,
12794 12795                      (caddr_t)mpt, (caddr_t)(uintptr_t)i)) != DDI_SUCCESS) {
12795 12796                          mptsas_log(mpt, CE_WARN, "ddi_intr_add_handler() "
12796 12797                              "failed %d\n", ret);
12797 12798  
12798 12799                          /* Free already allocated intr */
12799 12800                          for (i = 0; i < actual; i++) {
12800 12801                                  (void) ddi_intr_free(mpt->m_htable[i]);
12801 12802                          }
12802 12803  
12803 12804                          kmem_free(mpt->m_htable, mpt->m_intr_size);
12804 12805                          return (DDI_FAILURE);
12805 12806                  }
12806 12807          }
12807 12808  
12808 12809          if ((ret = ddi_intr_get_cap(mpt->m_htable[0], &mpt->m_intr_cap))
12809 12810              != DDI_SUCCESS) {
12810 12811                  mptsas_log(mpt, CE_WARN, "ddi_intr_get_cap() failed %d\n", ret);
12811 12812  
12812 12813                  /* Free already allocated intr */
12813 12814                  for (i = 0; i < actual; i++) {
12814 12815                          (void) ddi_intr_free(mpt->m_htable[i]);
12815 12816                  }
12816 12817  
12817 12818                  kmem_free(mpt->m_htable, mpt->m_intr_size);
12818 12819                  return (DDI_FAILURE);
12819 12820          }
12820 12821  
12821 12822          /*
12822 12823           * Enable interrupts
12823 12824           */
12824 12825          if (mpt->m_intr_cap & DDI_INTR_FLAG_BLOCK) {
12825 12826                  /* Call ddi_intr_block_enable() for MSI interrupts */
12826 12827                  (void) ddi_intr_block_enable(mpt->m_htable, mpt->m_intr_cnt);
12827 12828          } else {
12828 12829                  /* Call ddi_intr_enable for MSI or FIXED interrupts */
12829 12830                  for (i = 0; i < mpt->m_intr_cnt; i++) {
12830 12831                          (void) ddi_intr_enable(mpt->m_htable[i]);
12831 12832                  }
12832 12833          }
12833 12834          return (DDI_SUCCESS);
12834 12835  }
12835 12836  
12836 12837  /*
12837 12838   * mptsas_rem_intrs:
12838 12839   *
12839 12840   * Unregister FIXED or MSI interrupts
12840 12841   */
12841 12842  static void
12842 12843  mptsas_rem_intrs(mptsas_t *mpt)
12843 12844  {
12844 12845          int     i;
12845 12846  
12846 12847          NDBG6(("mptsas_rem_intrs"));
12847 12848  
12848 12849          /* Disable all interrupts */
12849 12850          if (mpt->m_intr_cap & DDI_INTR_FLAG_BLOCK) {
12850 12851                  /* Call ddi_intr_block_disable() */
12851 12852                  (void) ddi_intr_block_disable(mpt->m_htable, mpt->m_intr_cnt);
12852 12853          } else {
12853 12854                  for (i = 0; i < mpt->m_intr_cnt; i++) {
12854 12855                          (void) ddi_intr_disable(mpt->m_htable[i]);
12855 12856                  }
12856 12857          }
12857 12858  
12858 12859          /* Call ddi_intr_remove_handler() */
12859 12860          for (i = 0; i < mpt->m_intr_cnt; i++) {
12860 12861                  (void) ddi_intr_remove_handler(mpt->m_htable[i]);
12861 12862                  (void) ddi_intr_free(mpt->m_htable[i]);
12862 12863          }
12863 12864  
12864 12865          kmem_free(mpt->m_htable, mpt->m_intr_size);
12865 12866  }
12866 12867  
12867 12868  /*
12868 12869   * The IO fault service error handling callback function
12869 12870   */
12870 12871  /*ARGSUSED*/
12871 12872  static int
12872 12873  mptsas_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
12873 12874  {
12874 12875          /*
12875 12876           * as the driver can always deal with an error in any dma or
12876 12877           * access handle, we can just return the fme_status value.
12877 12878           */
12878 12879          pci_ereport_post(dip, err, NULL);
12879 12880          return (err->fme_status);
12880 12881  }
12881 12882  
12882 12883  /*
12883 12884   * mptsas_fm_init - initialize fma capabilities and register with IO
12884 12885   *               fault services.
12885 12886   */
12886 12887  static void
12887 12888  mptsas_fm_init(mptsas_t *mpt)
12888 12889  {
12889 12890          /*
12890 12891           * Need to change iblock to priority for new MSI intr
12891 12892           */
12892 12893          ddi_iblock_cookie_t     fm_ibc;
12893 12894  
12894 12895          /* Only register with IO Fault Services if we have some capability */
12895 12896          if (mpt->m_fm_capabilities) {
12896 12897                  /* Adjust access and dma attributes for FMA */
12897 12898                  mpt->m_reg_acc_attr.devacc_attr_access = DDI_FLAGERR_ACC;
12898 12899                  mpt->m_msg_dma_attr.dma_attr_flags |= DDI_DMA_FLAGERR;
12899 12900                  mpt->m_io_dma_attr.dma_attr_flags |= DDI_DMA_FLAGERR;
12900 12901  
12901 12902                  /*
12902 12903                   * Register capabilities with IO Fault Services.
12903 12904                   * mpt->m_fm_capabilities will be updated to indicate
12904 12905                   * capabilities actually supported (not requested.)
12905 12906                   */
12906 12907                  ddi_fm_init(mpt->m_dip, &mpt->m_fm_capabilities, &fm_ibc);
12907 12908  
12908 12909                  /*
12909 12910                   * Initialize pci ereport capabilities if ereport
12910 12911                   * capable (should always be.)
12911 12912                   */
12912 12913                  if (DDI_FM_EREPORT_CAP(mpt->m_fm_capabilities) ||
12913 12914                      DDI_FM_ERRCB_CAP(mpt->m_fm_capabilities)) {
12914 12915                          pci_ereport_setup(mpt->m_dip);
12915 12916                  }
12916 12917  
12917 12918                  /*
12918 12919                   * Register error callback if error callback capable.
12919 12920                   */
12920 12921                  if (DDI_FM_ERRCB_CAP(mpt->m_fm_capabilities)) {
12921 12922                          ddi_fm_handler_register(mpt->m_dip,
12922 12923                              mptsas_fm_error_cb, (void *) mpt);
12923 12924                  }
12924 12925          }
12925 12926  }
12926 12927  
12927 12928  /*
12928 12929   * mptsas_fm_fini - Releases fma capabilities and un-registers with IO
12929 12930   *               fault services.
12930 12931   *
12931 12932   */
12932 12933  static void
12933 12934  mptsas_fm_fini(mptsas_t *mpt)
12934 12935  {
12935 12936          /* Only unregister FMA capabilities if registered */
12936 12937          if (mpt->m_fm_capabilities) {
12937 12938  
12938 12939                  /*
12939 12940                   * Un-register error callback if error callback capable.
12940 12941                   */
12941 12942  
12942 12943                  if (DDI_FM_ERRCB_CAP(mpt->m_fm_capabilities)) {
12943 12944                          ddi_fm_handler_unregister(mpt->m_dip);
12944 12945                  }
12945 12946  
12946 12947                  /*
12947 12948                   * Release any resources allocated by pci_ereport_setup()
12948 12949                   */
12949 12950  
12950 12951                  if (DDI_FM_EREPORT_CAP(mpt->m_fm_capabilities) ||
12951 12952                      DDI_FM_ERRCB_CAP(mpt->m_fm_capabilities)) {
12952 12953                          pci_ereport_teardown(mpt->m_dip);
12953 12954                  }
12954 12955  
12955 12956                  /* Unregister from IO Fault Services */
12956 12957                  ddi_fm_fini(mpt->m_dip);
12957 12958  
12958 12959                  /* Adjust access and dma attributes for FMA */
12959 12960                  mpt->m_reg_acc_attr.devacc_attr_access = DDI_DEFAULT_ACC;
12960 12961                  mpt->m_msg_dma_attr.dma_attr_flags &= ~DDI_DMA_FLAGERR;
12961 12962                  mpt->m_io_dma_attr.dma_attr_flags &= ~DDI_DMA_FLAGERR;
12962 12963  
12963 12964          }
12964 12965  }
12965 12966  
12966 12967  int
12967 12968  mptsas_check_acc_handle(ddi_acc_handle_t handle)
12968 12969  {
12969 12970          ddi_fm_error_t  de;
12970 12971  
12971 12972          if (handle == NULL)
12972 12973                  return (DDI_FAILURE);
12973 12974          ddi_fm_acc_err_get(handle, &de, DDI_FME_VER0);
12974 12975          return (de.fme_status);
12975 12976  }
12976 12977  
12977 12978  int
12978 12979  mptsas_check_dma_handle(ddi_dma_handle_t handle)
12979 12980  {
12980 12981          ddi_fm_error_t  de;
12981 12982  
12982 12983          if (handle == NULL)
12983 12984                  return (DDI_FAILURE);
12984 12985          ddi_fm_dma_err_get(handle, &de, DDI_FME_VER0);
12985 12986          return (de.fme_status);
12986 12987  }
12987 12988  
12988 12989  void
12989 12990  mptsas_fm_ereport(mptsas_t *mpt, char *detail)
12990 12991  {
12991 12992          uint64_t        ena;
12992 12993          char            buf[FM_MAX_CLASS];
12993 12994  
12994 12995          (void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
12995 12996          ena = fm_ena_generate(0, FM_ENA_FMT1);
12996 12997          if (DDI_FM_EREPORT_CAP(mpt->m_fm_capabilities)) {
12997 12998                  ddi_fm_ereport_post(mpt->m_dip, buf, ena, DDI_NOSLEEP,
12998 12999                      FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, NULL);
12999 13000          }
13000 13001  }
13001 13002  
13002 13003  static int
13003 13004  mptsas_get_target_device_info(mptsas_t *mpt, uint32_t page_address,
13004 13005      uint16_t *dev_handle, mptsas_target_t **pptgt)
13005 13006  {
13006 13007          int             rval;
13007 13008          uint32_t        dev_info;
13008 13009          uint64_t        sas_wwn;
13009 13010          mptsas_phymask_t phymask;
13010 13011          uint8_t         physport, phynum, config, disk;
13011 13012          mptsas_slots_t  *slots = mpt->m_active;
13012 13013          uint64_t        devicename;
13013 13014          uint16_t        pdev_hdl;
13014 13015          mptsas_target_t *tmp_tgt = NULL;
13015 13016          uint16_t        bay_num, enclosure;
13016 13017  
13017 13018          ASSERT(*pptgt == NULL);
13018 13019  
13019 13020          rval = mptsas_get_sas_device_page0(mpt, page_address, dev_handle,
13020 13021              &sas_wwn, &dev_info, &physport, &phynum, &pdev_hdl,
13021 13022              &bay_num, &enclosure);
13022 13023          if (rval != DDI_SUCCESS) {
13023 13024                  rval = DEV_INFO_FAIL_PAGE0;
13024 13025                  return (rval);
13025 13026          }
13026 13027  
13027 13028          if ((dev_info & (MPI2_SAS_DEVICE_INFO_SSP_TARGET |
13028 13029              MPI2_SAS_DEVICE_INFO_SATA_DEVICE |
13029 13030              MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE)) == NULL) {
13030 13031                  rval = DEV_INFO_WRONG_DEVICE_TYPE;
13031 13032                  return (rval);
13032 13033          }
13033 13034  
13034 13035          /*
13035 13036           * Check if the dev handle is for a Phys Disk. If so, set return value
13036 13037           * and exit.  Don't add Phys Disks to hash.
13037 13038           */
13038 13039          for (config = 0; config < slots->m_num_raid_configs; config++) {
13039 13040                  for (disk = 0; disk < MPTSAS_MAX_DISKS_IN_CONFIG; disk++) {
13040 13041                          if (*dev_handle == slots->m_raidconfig[config].
13041 13042                              m_physdisk_devhdl[disk]) {
13042 13043                                  rval = DEV_INFO_PHYS_DISK;
13043 13044                                  return (rval);
13044 13045                          }
13045 13046                  }
13046 13047          }
13047 13048  
13048 13049          /*
13049 13050           * Get SATA Device Name from SAS device page0 for
13050 13051           * sata device, if device name doesn't exist, set m_sas_wwn to
13051 13052           * 0 for direct attached SATA. For the device behind the expander
13052 13053           * we still can use STP address assigned by expander.
13053 13054           */
13054 13055          if (dev_info & (MPI2_SAS_DEVICE_INFO_SATA_DEVICE |
13055 13056              MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE)) {
13056 13057                  mutex_exit(&mpt->m_mutex);
13057 13058                  /* alloc a tmp_tgt to send the cmd */
13058 13059                  tmp_tgt = kmem_zalloc(sizeof (struct mptsas_target),
13059 13060                      KM_SLEEP);
13060 13061                  tmp_tgt->m_devhdl = *dev_handle;
13061 13062                  tmp_tgt->m_deviceinfo = dev_info;
13062 13063                  tmp_tgt->m_qfull_retries = QFULL_RETRIES;
13063 13064                  tmp_tgt->m_qfull_retry_interval =
13064 13065                      drv_usectohz(QFULL_RETRY_INTERVAL * 1000);
13065 13066                  tmp_tgt->m_t_throttle = MAX_THROTTLE;
13066 13067                  devicename = mptsas_get_sata_guid(mpt, tmp_tgt, 0);
13067 13068                  kmem_free(tmp_tgt, sizeof (struct mptsas_target));
13068 13069                  mutex_enter(&mpt->m_mutex);
13069 13070                  if (devicename != 0 && (((devicename >> 56) & 0xf0) == 0x50)) {
13070 13071                          sas_wwn = devicename;
13071 13072                  } else if (dev_info & MPI2_SAS_DEVICE_INFO_DIRECT_ATTACH) {
13072 13073                          sas_wwn = 0;
13073 13074                  }
13074 13075          }
13075 13076  
13076 13077          phymask = mptsas_physport_to_phymask(mpt, physport);
13077 13078          *pptgt = mptsas_tgt_alloc(&slots->m_tgttbl, *dev_handle, sas_wwn,
13078 13079              dev_info, phymask, phynum, mpt);
13079 13080          if (*pptgt == NULL) {
13080 13081                  mptsas_log(mpt, CE_WARN, "Failed to allocated target"
13081 13082                      "structure!");
13082 13083                  rval = DEV_INFO_FAIL_ALLOC;
13083 13084                  return (rval);
13084 13085          }
13085 13086          (*pptgt)->m_enclosure = enclosure;
13086 13087          (*pptgt)->m_slot_num = bay_num;
13087 13088          return (DEV_INFO_SUCCESS);
13088 13089  }
13089 13090  
13090 13091  uint64_t
13091 13092  mptsas_get_sata_guid(mptsas_t *mpt, mptsas_target_t *ptgt, int lun)
13092 13093  {
13093 13094          uint64_t        sata_guid = 0, *pwwn = NULL;
13094 13095          int             target = ptgt->m_devhdl;
13095 13096          uchar_t         *inq83 = NULL;
13096 13097          int             inq83_len = 0xFF;
13097 13098          uchar_t         *dblk = NULL;
13098 13099          int             inq83_retry = 3;
13099 13100          int             rval = DDI_FAILURE;
13100 13101  
13101 13102          inq83   = kmem_zalloc(inq83_len, KM_SLEEP);
13102 13103  
13103 13104  inq83_retry:
13104 13105          rval = mptsas_inquiry(mpt, ptgt, lun, 0x83, inq83,
13105 13106              inq83_len, NULL, 1);
13106 13107          if (rval != DDI_SUCCESS) {
13107 13108                  mptsas_log(mpt, CE_WARN, "!mptsas request inquiry page "
13108 13109                      "0x83 for target:%x, lun:%x failed!", target, lun);
13109 13110                  goto out;
13110 13111          }
13111 13112          /* According to SAT2, the first descriptor is logic unit name */
13112 13113          dblk = &inq83[4];
13113 13114          if ((dblk[1] & 0x30) != 0) {
13114 13115                  mptsas_log(mpt, CE_WARN, "!Descriptor is not lun associated.");
13115 13116                  goto out;
13116 13117          }
13117 13118          pwwn = (uint64_t *)(void *)(&dblk[4]);
13118 13119          if ((dblk[4] & 0xf0) == 0x50) {
13119 13120                  sata_guid = BE_64(*pwwn);
13120 13121                  goto out;
13121 13122          } else if (dblk[4] == 'A') {
13122 13123                  NDBG20(("SATA drive has no NAA format GUID."));
13123 13124                  goto out;
13124 13125          } else {
13125 13126                  /* The data is not ready, wait and retry */
13126 13127                  inq83_retry--;
13127 13128                  if (inq83_retry <= 0) {
13128 13129                          goto out;
13129 13130                  }
13130 13131                  NDBG20(("The GUID is not ready, retry..."));
13131 13132                  delay(1 * drv_usectohz(1000000));
13132 13133                  goto inq83_retry;
13133 13134          }
13134 13135  out:
13135 13136          kmem_free(inq83, inq83_len);
13136 13137          return (sata_guid);
13137 13138  }
13138 13139  
13139 13140  static int
13140 13141  mptsas_inquiry(mptsas_t *mpt, mptsas_target_t *ptgt, int lun, uchar_t page,
13141 13142      unsigned char *buf, int len, int *reallen, uchar_t evpd)
13142 13143  {
13143 13144          uchar_t                 cdb[CDB_GROUP0];
13144 13145          struct scsi_address     ap;
13145 13146          struct buf              *data_bp = NULL;
13146 13147          int                     resid = 0;
13147 13148          int                     ret = DDI_FAILURE;
13148 13149  
13149 13150          ASSERT(len <= 0xffff);
13150 13151  
13151 13152          ap.a_target = MPTSAS_INVALID_DEVHDL;
13152 13153          ap.a_lun = (uchar_t)(lun);
13153 13154          ap.a_hba_tran = mpt->m_tran;
13154 13155  
13155 13156          data_bp = scsi_alloc_consistent_buf(&ap,
13156 13157              (struct buf *)NULL, len, B_READ, NULL_FUNC, NULL);
13157 13158          if (data_bp == NULL) {
13158 13159                  return (ret);
13159 13160          }
13160 13161          bzero(cdb, CDB_GROUP0);
13161 13162          cdb[0] = SCMD_INQUIRY;
13162 13163          cdb[1] = evpd;
13163 13164          cdb[2] = page;
13164 13165          cdb[3] = (len & 0xff00) >> 8;
13165 13166          cdb[4] = (len & 0x00ff);
13166 13167          cdb[5] = 0;
13167 13168  
13168 13169          ret = mptsas_send_scsi_cmd(mpt, &ap, ptgt, &cdb[0], CDB_GROUP0, data_bp,
13169 13170              &resid);
13170 13171          if (ret == DDI_SUCCESS) {
13171 13172                  if (reallen) {
13172 13173                          *reallen = len - resid;
13173 13174                  }
13174 13175                  bcopy((caddr_t)data_bp->b_un.b_addr, buf, len);
13175 13176          }
13176 13177          if (data_bp) {
13177 13178                  scsi_free_consistent_buf(data_bp);
13178 13179          }
13179 13180          return (ret);
13180 13181  }
13181 13182  
13182 13183  static int
13183 13184  mptsas_send_scsi_cmd(mptsas_t *mpt, struct scsi_address *ap,
13184 13185      mptsas_target_t *ptgt, uchar_t *cdb, int cdblen, struct buf *data_bp,
13185 13186      int *resid)
13186 13187  {
13187 13188          struct scsi_pkt         *pktp = NULL;
13188 13189          scsi_hba_tran_t         *tran_clone = NULL;
13189 13190          mptsas_tgt_private_t    *tgt_private = NULL;
13190 13191          int                     ret = DDI_FAILURE;
13191 13192  
13192 13193          /*
13193 13194           * scsi_hba_tran_t->tran_tgt_private is used to pass the address
13194 13195           * information to scsi_init_pkt, allocate a scsi_hba_tran structure
13195 13196           * to simulate the cmds from sd
13196 13197           */
13197 13198          tran_clone = kmem_alloc(
13198 13199              sizeof (scsi_hba_tran_t), KM_SLEEP);
13199 13200          if (tran_clone == NULL) {
13200 13201                  goto out;
13201 13202          }
13202 13203          bcopy((caddr_t)mpt->m_tran,
13203 13204              (caddr_t)tran_clone, sizeof (scsi_hba_tran_t));
13204 13205          tgt_private = kmem_alloc(
13205 13206              sizeof (mptsas_tgt_private_t), KM_SLEEP);
13206 13207          if (tgt_private == NULL) {
13207 13208                  goto out;
13208 13209          }
13209 13210          tgt_private->t_lun = ap->a_lun;
13210 13211          tgt_private->t_private = ptgt;
13211 13212          tran_clone->tran_tgt_private = tgt_private;
13212 13213          ap->a_hba_tran = tran_clone;
13213 13214  
13214 13215          pktp = scsi_init_pkt(ap, (struct scsi_pkt *)NULL,
13215 13216              data_bp, cdblen, sizeof (struct scsi_arq_status),
13216 13217              0, PKT_CONSISTENT, NULL, NULL);
13217 13218          if (pktp == NULL) {
13218 13219                  goto out;
13219 13220          }
13220 13221          bcopy(cdb, pktp->pkt_cdbp, cdblen);
13221 13222          pktp->pkt_flags = FLAG_NOPARITY;
13222 13223          if (scsi_poll(pktp) < 0) {
13223 13224                  goto out;
13224 13225          }
13225 13226          if (((struct scsi_status *)pktp->pkt_scbp)->sts_chk) {
13226 13227                  goto out;
13227 13228          }
13228 13229          if (resid != NULL) {
13229 13230                  *resid = pktp->pkt_resid;
13230 13231          }
13231 13232  
13232 13233          ret = DDI_SUCCESS;
13233 13234  out:
13234 13235          if (pktp) {
13235 13236                  scsi_destroy_pkt(pktp);
13236 13237          }
13237 13238          if (tran_clone) {
13238 13239                  kmem_free(tran_clone, sizeof (scsi_hba_tran_t));
13239 13240          }
13240 13241          if (tgt_private) {
13241 13242                  kmem_free(tgt_private, sizeof (mptsas_tgt_private_t));
13242 13243          }
13243 13244          return (ret);
13244 13245  }
13245 13246  static int
13246 13247  mptsas_parse_address(char *name, uint64_t *wwid, uint8_t *phy, int *lun)
13247 13248  {
13248 13249          char    *cp = NULL;
13249 13250          char    *ptr = NULL;
13250 13251          size_t  s = 0;
13251 13252          char    *wwid_str = NULL;
13252 13253          char    *lun_str = NULL;
13253 13254          long    lunnum;
13254 13255          long    phyid = -1;
13255 13256          int     rc = DDI_FAILURE;
13256 13257  
13257 13258          ptr = name;
13258 13259          ASSERT(ptr[0] == 'w' || ptr[0] == 'p');
13259 13260          ptr++;
13260 13261          if ((cp = strchr(ptr, ',')) == NULL) {
13261 13262                  return (DDI_FAILURE);
13262 13263          }
13263 13264  
13264 13265          wwid_str = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
13265 13266          s = (uintptr_t)cp - (uintptr_t)ptr;
13266 13267  
13267 13268          bcopy(ptr, wwid_str, s);
13268 13269          wwid_str[s] = '\0';
13269 13270  
13270 13271          ptr = ++cp;
13271 13272  
13272 13273          if ((cp = strchr(ptr, '\0')) == NULL) {
13273 13274                  goto out;
13274 13275          }
13275 13276          lun_str =  kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
13276 13277          s = (uintptr_t)cp - (uintptr_t)ptr;
13277 13278  
13278 13279          bcopy(ptr, lun_str, s);
13279 13280          lun_str[s] = '\0';
13280 13281  
13281 13282          if (name[0] == 'p') {
13282 13283                  rc = ddi_strtol(wwid_str, NULL, 0x10, &phyid);
13283 13284          } else {
13284 13285                  rc = scsi_wwnstr_to_wwn(wwid_str, wwid);
13285 13286          }
13286 13287          if (rc != DDI_SUCCESS)
13287 13288                  goto out;
13288 13289  
13289 13290          if (phyid != -1) {
13290 13291                  ASSERT(phyid < MPTSAS_MAX_PHYS);
13291 13292                  *phy = (uint8_t)phyid;
13292 13293          }
13293 13294          rc = ddi_strtol(lun_str, NULL, 0x10, &lunnum);
13294 13295          if (rc != 0)
13295 13296                  goto out;
13296 13297  
13297 13298          *lun = (int)lunnum;
13298 13299          rc = DDI_SUCCESS;
13299 13300  out:
13300 13301          if (wwid_str)
13301 13302                  kmem_free(wwid_str, SCSI_MAXNAMELEN);
13302 13303          if (lun_str)
13303 13304                  kmem_free(lun_str, SCSI_MAXNAMELEN);
13304 13305  
13305 13306          return (rc);
13306 13307  }
13307 13308  
13308 13309  /*
13309 13310   * mptsas_parse_smp_name() is to parse sas wwn string
13310 13311   * which format is "wWWN"
13311 13312   */
13312 13313  static int
13313 13314  mptsas_parse_smp_name(char *name, uint64_t *wwn)
13314 13315  {
13315 13316          char    *ptr = name;
13316 13317  
13317 13318          if (*ptr != 'w') {
13318 13319                  return (DDI_FAILURE);
13319 13320          }
13320 13321  
13321 13322          ptr++;
13322 13323          if (scsi_wwnstr_to_wwn(ptr, wwn)) {
13323 13324                  return (DDI_FAILURE);
13324 13325          }
13325 13326          return (DDI_SUCCESS);
13326 13327  }
13327 13328  
13328 13329  static int
13329 13330  mptsas_bus_config(dev_info_t *pdip, uint_t flag,
13330 13331      ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
13331 13332  {
13332 13333          int             ret = NDI_FAILURE;
13333 13334          int             circ = 0;
13334 13335          int             circ1 = 0;
13335 13336          mptsas_t        *mpt;
13336 13337          char            *ptr = NULL;
13337 13338          char            *devnm = NULL;
13338 13339          uint64_t        wwid = 0;
13339 13340          uint8_t         phy = 0xFF;
13340 13341          int             lun = 0;
13341 13342          uint_t          mflags = flag;
13342 13343          int             bconfig = TRUE;
13343 13344  
13344 13345          if (scsi_hba_iport_unit_address(pdip) == 0) {
13345 13346                  return (DDI_FAILURE);
13346 13347          }
13347 13348  
13348 13349          mpt = DIP2MPT(pdip);
13349 13350          if (!mpt) {
13350 13351                  return (DDI_FAILURE);
13351 13352          }
13352 13353          /*
13353 13354           * Hold the nexus across the bus_config
13354 13355           */
13355 13356          ndi_devi_enter(scsi_vhci_dip, &circ);
13356 13357          ndi_devi_enter(pdip, &circ1);
13357 13358          switch (op) {
13358 13359          case BUS_CONFIG_ONE:
13359 13360                  /* parse wwid/target name out of name given */
13360 13361                  if ((ptr = strchr((char *)arg, '@')) == NULL) {
13361 13362                          ret = NDI_FAILURE;
13362 13363                          break;
13363 13364                  }
13364 13365                  ptr++;
13365 13366                  if (strncmp((char *)arg, "smp", 3) == 0) {
13366 13367                          /*
13367 13368                           * This is a SMP target device
13368 13369                           */
13369 13370                          ret = mptsas_parse_smp_name(ptr, &wwid);
13370 13371                          if (ret != DDI_SUCCESS) {
13371 13372                                  ret = NDI_FAILURE;
13372 13373                                  break;
13373 13374                          }
13374 13375                          ret = mptsas_config_smp(pdip, wwid, childp);
13375 13376                  } else if ((ptr[0] == 'w') || (ptr[0] == 'p')) {
13376 13377                          /*
13377 13378                           * OBP could pass down a non-canonical form
13378 13379                           * bootpath without LUN part when LUN is 0.
13379 13380                           * So driver need adjust the string.
13380 13381                           */
13381 13382                          if (strchr(ptr, ',') == NULL) {
13382 13383                                  devnm = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
13383 13384                                  (void) sprintf(devnm, "%s,0", (char *)arg);
13384 13385                                  ptr = strchr(devnm, '@');
13385 13386                                  ptr++;
13386 13387                          }
13387 13388  
13388 13389                          /*
13389 13390                           * The device path is wWWID format and the device
13390 13391                           * is not SMP target device.
13391 13392                           */
13392 13393                          ret = mptsas_parse_address(ptr, &wwid, &phy, &lun);
13393 13394                          if (ret != DDI_SUCCESS) {
13394 13395                                  ret = NDI_FAILURE;
13395 13396                                  break;
13396 13397                          }
13397 13398                          *childp = NULL;
13398 13399                          if (ptr[0] == 'w') {
13399 13400                                  ret = mptsas_config_one_addr(pdip, wwid,
13400 13401                                      lun, childp);
13401 13402                          } else if (ptr[0] == 'p') {
13402 13403                                  ret = mptsas_config_one_phy(pdip, phy, lun,
13403 13404                                      childp);
13404 13405                          }
13405 13406  
13406 13407                          /*
13407 13408                           * If this is CD/DVD device in OBP path, the
13408 13409                           * ndi_busop_bus_config can be skipped as config one
13409 13410                           * operation is done above.
13410 13411                           */
13411 13412                          if ((ret == NDI_SUCCESS) && (*childp != NULL) &&
13412 13413                              (strcmp(ddi_node_name(*childp), "cdrom") == 0) &&
13413 13414                              (strncmp((char *)arg, "disk", 4) == 0)) {
13414 13415                                  bconfig = FALSE;
13415 13416                                  ndi_hold_devi(*childp);
13416 13417                          }
13417 13418                  } else {
13418 13419                          ret = NDI_FAILURE;
13419 13420                          break;
13420 13421                  }
13421 13422  
13422 13423                  /*
13423 13424                   * DDI group instructed us to use this flag.
13424 13425                   */
13425 13426                  mflags |= NDI_MDI_FALLBACK;
13426 13427                  break;
13427 13428          case BUS_CONFIG_DRIVER:
13428 13429          case BUS_CONFIG_ALL:
13429 13430                  mptsas_config_all(pdip);
13430 13431                  ret = NDI_SUCCESS;
13431 13432                  break;
13432 13433          }
13433 13434  
13434 13435          if ((ret == NDI_SUCCESS) && bconfig) {
13435 13436                  ret = ndi_busop_bus_config(pdip, mflags, op,
13436 13437                      (devnm == NULL) ? arg : devnm, childp, 0);
13437 13438          }
13438 13439  
13439 13440          ndi_devi_exit(pdip, circ1);
13440 13441          ndi_devi_exit(scsi_vhci_dip, circ);
13441 13442          if (devnm != NULL)
13442 13443                  kmem_free(devnm, SCSI_MAXNAMELEN);
13443 13444          return (ret);
13444 13445  }
13445 13446  
13446 13447  static int
13447 13448  mptsas_probe_lun(dev_info_t *pdip, int lun, dev_info_t **dip,
13448 13449      mptsas_target_t *ptgt)
13449 13450  {
13450 13451          int                     rval = DDI_FAILURE;
13451 13452          struct scsi_inquiry     *sd_inq = NULL;
13452 13453          mptsas_t                *mpt = DIP2MPT(pdip);
13453 13454  
13454 13455          sd_inq = (struct scsi_inquiry *)kmem_alloc(SUN_INQSIZE, KM_SLEEP);
13455 13456  
13456 13457          rval = mptsas_inquiry(mpt, ptgt, lun, 0, (uchar_t *)sd_inq,
13457 13458              SUN_INQSIZE, 0, (uchar_t)0);
13458 13459  
13459 13460          if ((rval == DDI_SUCCESS) && MPTSAS_VALID_LUN(sd_inq)) {
13460 13461                  rval = mptsas_create_lun(pdip, sd_inq, dip, ptgt, lun);
13461 13462          } else {
13462 13463                  rval = DDI_FAILURE;
13463 13464          }
13464 13465  
13465 13466          kmem_free(sd_inq, SUN_INQSIZE);
13466 13467          return (rval);
13467 13468  }
13468 13469  
13469 13470  static int
13470 13471  mptsas_config_one_addr(dev_info_t *pdip, uint64_t sasaddr, int lun,
13471 13472      dev_info_t **lundip)
13472 13473  {
13473 13474          int             rval;
13474 13475          mptsas_t                *mpt = DIP2MPT(pdip);
13475 13476          int             phymask;
13476 13477          mptsas_target_t *ptgt = NULL;
13477 13478  
13478 13479          /*
13479 13480           * Get the physical port associated to the iport
13480 13481           */
13481 13482          phymask = ddi_prop_get_int(DDI_DEV_T_ANY, pdip, 0,
13482 13483              "phymask", 0);
13483 13484  
13484 13485          ptgt = mptsas_wwid_to_ptgt(mpt, phymask, sasaddr);
13485 13486          if (ptgt == NULL) {
13486 13487                  /*
13487 13488                   * didn't match any device by searching
13488 13489                   */
13489 13490                  return (DDI_FAILURE);
13490 13491          }
13491 13492          /*
13492 13493           * If the LUN already exists and the status is online,
13493 13494           * we just return the pointer to dev_info_t directly.
13494 13495           * For the mdi_pathinfo node, we'll handle it in
13495 13496           * mptsas_create_virt_lun()
13496 13497           * TODO should be also in mptsas_handle_dr
13497 13498           */
13498 13499  
13499 13500          *lundip = mptsas_find_child_addr(pdip, sasaddr, lun);
13500 13501          if (*lundip != NULL) {
13501 13502                  /*
13502 13503                   * TODO Another senario is, we hotplug the same disk
13503 13504                   * on the same slot, the devhdl changed, is this
13504 13505                   * possible?
13505 13506                   * tgt_private->t_private != ptgt
13506 13507                   */
13507 13508                  if (sasaddr != ptgt->m_sas_wwn) {
13508 13509                          /*
13509 13510                           * The device has changed although the devhdl is the
13510 13511                           * same (Enclosure mapping mode, change drive on the
13511 13512                           * same slot)
13512 13513                           */
13513 13514                          return (DDI_FAILURE);
13514 13515                  }
13515 13516                  return (DDI_SUCCESS);
13516 13517          }
13517 13518  
13518 13519          if (phymask == 0) {
13519 13520                  /*
13520 13521                   * Configure IR volume
13521 13522                   */
13522 13523                  rval =  mptsas_config_raid(pdip, ptgt->m_devhdl, lundip);
13523 13524                  return (rval);
13524 13525          }
13525 13526          rval = mptsas_probe_lun(pdip, lun, lundip, ptgt);
13526 13527  
13527 13528          return (rval);
13528 13529  }
13529 13530  
13530 13531  static int
13531 13532  mptsas_config_one_phy(dev_info_t *pdip, uint8_t phy, int lun,
13532 13533      dev_info_t **lundip)
13533 13534  {
13534 13535          int             rval;
13535 13536          mptsas_t        *mpt = DIP2MPT(pdip);
13536 13537          int             phymask;
13537 13538          mptsas_target_t *ptgt = NULL;
13538 13539  
13539 13540          /*
13540 13541           * Get the physical port associated to the iport
13541 13542           */
13542 13543          phymask = ddi_prop_get_int(DDI_DEV_T_ANY, pdip, 0,
13543 13544              "phymask", 0);
13544 13545  
13545 13546          ptgt = mptsas_phy_to_tgt(mpt, phymask, phy);
13546 13547          if (ptgt == NULL) {
13547 13548                  /*
13548 13549                   * didn't match any device by searching
13549 13550                   */
13550 13551                  return (DDI_FAILURE);
13551 13552          }
13552 13553  
13553 13554          /*
13554 13555           * If the LUN already exists and the status is online,
13555 13556           * we just return the pointer to dev_info_t directly.
13556 13557           * For the mdi_pathinfo node, we'll handle it in
13557 13558           * mptsas_create_virt_lun().
13558 13559           */
13559 13560  
13560 13561          *lundip = mptsas_find_child_phy(pdip, phy);
13561 13562          if (*lundip != NULL) {
13562 13563                  return (DDI_SUCCESS);
13563 13564          }
13564 13565  
13565 13566          rval = mptsas_probe_lun(pdip, lun, lundip, ptgt);
13566 13567  
13567 13568          return (rval);
13568 13569  }
13569 13570  
13570 13571  static int
13571 13572  mptsas_retrieve_lundata(int lun_cnt, uint8_t *buf, uint16_t *lun_num,
13572 13573      uint8_t *lun_addr_type)
13573 13574  {
13574 13575          uint32_t        lun_idx = 0;
13575 13576  
13576 13577          ASSERT(lun_num != NULL);
13577 13578          ASSERT(lun_addr_type != NULL);
13578 13579  
13579 13580          lun_idx = (lun_cnt + 1) * MPTSAS_SCSI_REPORTLUNS_ADDRESS_SIZE;
13580 13581          /* determine report luns addressing type */
13581 13582          switch (buf[lun_idx] & MPTSAS_SCSI_REPORTLUNS_ADDRESS_MASK) {
13582 13583                  /*
13583 13584                   * Vendors in the field have been found to be concatenating
13584 13585                   * bus/target/lun to equal the complete lun value instead
13585 13586                   * of switching to flat space addressing
13586 13587                   */
13587 13588                  /* 00b - peripheral device addressing method */
13588 13589          case MPTSAS_SCSI_REPORTLUNS_ADDRESS_PERIPHERAL:
13589 13590                  /* FALLTHRU */
13590 13591                  /* 10b - logical unit addressing method */
13591 13592          case MPTSAS_SCSI_REPORTLUNS_ADDRESS_LOGICAL_UNIT:
13592 13593                  /* FALLTHRU */
13593 13594                  /* 01b - flat space addressing method */
13594 13595          case MPTSAS_SCSI_REPORTLUNS_ADDRESS_FLAT_SPACE:
13595 13596                  /* byte0 bit0-5=msb lun byte1 bit0-7=lsb lun */
13596 13597                  *lun_addr_type = (buf[lun_idx] &
13597 13598                      MPTSAS_SCSI_REPORTLUNS_ADDRESS_MASK) >> 6;
13598 13599                  *lun_num = (buf[lun_idx] & 0x3F) << 8;
13599 13600                  *lun_num |= buf[lun_idx + 1];
13600 13601                  return (DDI_SUCCESS);
13601 13602          default:
13602 13603                  return (DDI_FAILURE);
13603 13604          }
13604 13605  }
13605 13606  
13606 13607  static int
13607 13608  mptsas_config_luns(dev_info_t *pdip, mptsas_target_t *ptgt)
13608 13609  {
13609 13610          struct buf              *repluns_bp = NULL;
13610 13611          struct scsi_address     ap;
13611 13612          uchar_t                 cdb[CDB_GROUP5];
13612 13613          int                     ret = DDI_FAILURE;
13613 13614          int                     retry = 0;
13614 13615          int                     lun_list_len = 0;
13615 13616          uint16_t                lun_num = 0;
13616 13617          uint8_t                 lun_addr_type = 0;
13617 13618          uint32_t                lun_cnt = 0;
13618 13619          uint32_t                lun_total = 0;
13619 13620          dev_info_t              *cdip = NULL;
13620 13621          uint16_t                *saved_repluns = NULL;
13621 13622          char                    *buffer = NULL;
13622 13623          int                     buf_len = 128;
13623 13624          mptsas_t                *mpt = DIP2MPT(pdip);
13624 13625          uint64_t                sas_wwn = 0;
13625 13626          uint8_t                 phy = 0xFF;
13626 13627          uint32_t                dev_info = 0;
13627 13628  
13628 13629          mutex_enter(&mpt->m_mutex);
13629 13630          sas_wwn = ptgt->m_sas_wwn;
13630 13631          phy = ptgt->m_phynum;
13631 13632          dev_info = ptgt->m_deviceinfo;
13632 13633          mutex_exit(&mpt->m_mutex);
13633 13634  
13634 13635          if (sas_wwn == 0) {
13635 13636                  /*
13636 13637                   * It's a SATA without Device Name
13637 13638                   * So don't try multi-LUNs
13638 13639                   */
13639 13640                  if (mptsas_find_child_phy(pdip, phy)) {
13640 13641                          return (DDI_SUCCESS);
13641 13642                  } else {
13642 13643                          /*
13643 13644                           * need configure and create node
13644 13645                           */
13645 13646                          return (DDI_FAILURE);
13646 13647                  }
13647 13648          }
13648 13649  
13649 13650          /*
13650 13651           * WWN (SAS address or Device Name exist)
13651 13652           */
13652 13653          if (dev_info & (MPI2_SAS_DEVICE_INFO_SATA_DEVICE |
13653 13654              MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE)) {
13654 13655                  /*
13655 13656                   * SATA device with Device Name
13656 13657                   * So don't try multi-LUNs
13657 13658                   */
13658 13659                  if (mptsas_find_child_addr(pdip, sas_wwn, 0)) {
13659 13660                          return (DDI_SUCCESS);
13660 13661                  } else {
13661 13662                          return (DDI_FAILURE);
13662 13663                  }
13663 13664          }
13664 13665  
13665 13666          do {
13666 13667                  ap.a_target = MPTSAS_INVALID_DEVHDL;
13667 13668                  ap.a_lun = 0;
13668 13669                  ap.a_hba_tran = mpt->m_tran;
13669 13670                  repluns_bp = scsi_alloc_consistent_buf(&ap,
13670 13671                      (struct buf *)NULL, buf_len, B_READ, NULL_FUNC, NULL);
13671 13672                  if (repluns_bp == NULL) {
13672 13673                          retry++;
13673 13674                          continue;
13674 13675                  }
13675 13676                  bzero(cdb, CDB_GROUP5);
13676 13677                  cdb[0] = SCMD_REPORT_LUNS;
13677 13678                  cdb[6] = (buf_len & 0xff000000) >> 24;
13678 13679                  cdb[7] = (buf_len & 0x00ff0000) >> 16;
13679 13680                  cdb[8] = (buf_len & 0x0000ff00) >> 8;
13680 13681                  cdb[9] = (buf_len & 0x000000ff);
13681 13682  
13682 13683                  ret = mptsas_send_scsi_cmd(mpt, &ap, ptgt, &cdb[0], CDB_GROUP5,
13683 13684                      repluns_bp, NULL);
13684 13685                  if (ret != DDI_SUCCESS) {
13685 13686                          scsi_free_consistent_buf(repluns_bp);
13686 13687                          retry++;
13687 13688                          continue;
13688 13689                  }
13689 13690                  lun_list_len = BE_32(*(int *)((void *)(
13690 13691                      repluns_bp->b_un.b_addr)));
13691 13692                  if (buf_len >= lun_list_len + 8) {
13692 13693                          ret = DDI_SUCCESS;
13693 13694                          break;
13694 13695                  }
13695 13696                  scsi_free_consistent_buf(repluns_bp);
13696 13697                  buf_len = lun_list_len + 8;
13697 13698  
13698 13699          } while (retry < 3);
13699 13700  
13700 13701          if (ret != DDI_SUCCESS)
13701 13702                  return (ret);
13702 13703          buffer = (char *)repluns_bp->b_un.b_addr;
13703 13704          /*
13704 13705           * find out the number of luns returned by the SCSI ReportLun call
13705 13706           * and allocate buffer space
13706 13707           */
13707 13708          lun_total = lun_list_len / MPTSAS_SCSI_REPORTLUNS_ADDRESS_SIZE;
13708 13709          saved_repluns = kmem_zalloc(sizeof (uint16_t) * lun_total, KM_SLEEP);
13709 13710          if (saved_repluns == NULL) {
13710 13711                  scsi_free_consistent_buf(repluns_bp);
13711 13712                  return (DDI_FAILURE);
13712 13713          }
13713 13714          for (lun_cnt = 0; lun_cnt < lun_total; lun_cnt++) {
13714 13715                  if (mptsas_retrieve_lundata(lun_cnt, (uint8_t *)(buffer),
13715 13716                      &lun_num, &lun_addr_type) != DDI_SUCCESS) {
13716 13717                          continue;
13717 13718                  }
13718 13719                  saved_repluns[lun_cnt] = lun_num;
13719 13720                  if (cdip = mptsas_find_child_addr(pdip, sas_wwn, lun_num))
13720 13721                          ret = DDI_SUCCESS;
13721 13722                  else
13722 13723                          ret = mptsas_probe_lun(pdip, lun_num, &cdip,
13723 13724                              ptgt);
13724 13725                  if ((ret == DDI_SUCCESS) && (cdip != NULL)) {
13725 13726                          (void) ndi_prop_remove(DDI_DEV_T_NONE, cdip,
13726 13727                              MPTSAS_DEV_GONE);
13727 13728                  }
13728 13729          }
13729 13730          mptsas_offline_missed_luns(pdip, saved_repluns, lun_total, ptgt);
13730 13731          kmem_free(saved_repluns, sizeof (uint16_t) * lun_total);
13731 13732          scsi_free_consistent_buf(repluns_bp);
13732 13733          return (DDI_SUCCESS);
13733 13734  }
13734 13735  
13735 13736  static int
13736 13737  mptsas_config_raid(dev_info_t *pdip, uint16_t target, dev_info_t **dip)
13737 13738  {
13738 13739          int                     rval = DDI_FAILURE;
13739 13740          struct scsi_inquiry     *sd_inq = NULL;
13740 13741          mptsas_t                *mpt = DIP2MPT(pdip);
13741 13742          mptsas_target_t         *ptgt = NULL;
13742 13743  
13743 13744          mutex_enter(&mpt->m_mutex);
13744 13745          ptgt = mptsas_search_by_devhdl(&mpt->m_active->m_tgttbl, target);
13745 13746          mutex_exit(&mpt->m_mutex);
13746 13747          if (ptgt == NULL) {
13747 13748                  mptsas_log(mpt, CE_WARN, "Volume with VolDevHandle of 0x%x "
13748 13749                      "not found.", target);
13749 13750                  return (rval);
13750 13751          }
13751 13752  
13752 13753          sd_inq = (struct scsi_inquiry *)kmem_alloc(SUN_INQSIZE, KM_SLEEP);
13753 13754          rval = mptsas_inquiry(mpt, ptgt, 0, 0, (uchar_t *)sd_inq,
13754 13755              SUN_INQSIZE, 0, (uchar_t)0);
13755 13756  
13756 13757          if ((rval == DDI_SUCCESS) && MPTSAS_VALID_LUN(sd_inq)) {
13757 13758                  rval = mptsas_create_phys_lun(pdip, sd_inq, NULL, dip, ptgt,
13758 13759                      0);
13759 13760          } else {
13760 13761                  rval = DDI_FAILURE;
13761 13762          }
13762 13763  
13763 13764          kmem_free(sd_inq, SUN_INQSIZE);
13764 13765          return (rval);
13765 13766  }
13766 13767  
13767 13768  /*
13768 13769   * configure all RAID volumes for virtual iport
13769 13770   */
13770 13771  static void
13771 13772  mptsas_config_all_viport(dev_info_t *pdip)
13772 13773  {
13773 13774          mptsas_t        *mpt = DIP2MPT(pdip);
13774 13775          int             config, vol;
13775 13776          int             target;
13776 13777          dev_info_t      *lundip = NULL;
13777 13778          mptsas_slots_t  *slots = mpt->m_active;
13778 13779  
13779 13780          /*
13780 13781           * Get latest RAID info and search for any Volume DevHandles.  If any
13781 13782           * are found, configure the volume.
13782 13783           */
13783 13784          mutex_enter(&mpt->m_mutex);
13784 13785          for (config = 0; config < slots->m_num_raid_configs; config++) {
13785 13786                  for (vol = 0; vol < MPTSAS_MAX_RAIDVOLS; vol++) {
13786 13787                          if (slots->m_raidconfig[config].m_raidvol[vol].m_israid
13787 13788                              == 1) {
13788 13789                                  target = slots->m_raidconfig[config].
13789 13790                                      m_raidvol[vol].m_raidhandle;
13790 13791                                  mutex_exit(&mpt->m_mutex);
13791 13792                                  (void) mptsas_config_raid(pdip, target,
13792 13793                                      &lundip);
13793 13794                                  mutex_enter(&mpt->m_mutex);
13794 13795                          }
13795 13796                  }
13796 13797          }
13797 13798          mutex_exit(&mpt->m_mutex);
13798 13799  }
13799 13800  
13800 13801  static void
13801 13802  mptsas_offline_missed_luns(dev_info_t *pdip, uint16_t *repluns,
13802 13803      int lun_cnt, mptsas_target_t *ptgt)
13803 13804  {
13804 13805          dev_info_t      *child = NULL, *savechild = NULL;
13805 13806          mdi_pathinfo_t  *pip = NULL, *savepip = NULL;
13806 13807          uint64_t        sas_wwn, wwid;
13807 13808          uint8_t         phy;
13808 13809          int             lun;
13809 13810          int             i;
13810 13811          int             find;
13811 13812          char            *addr;
13812 13813          char            *nodename;
13813 13814          mptsas_t        *mpt = DIP2MPT(pdip);
13814 13815  
13815 13816          mutex_enter(&mpt->m_mutex);
13816 13817          wwid = ptgt->m_sas_wwn;
13817 13818          mutex_exit(&mpt->m_mutex);
13818 13819  
13819 13820          child = ddi_get_child(pdip);
13820 13821          while (child) {
13821 13822                  find = 0;
13822 13823                  savechild = child;
13823 13824                  child = ddi_get_next_sibling(child);
13824 13825  
13825 13826                  nodename = ddi_node_name(savechild);
13826 13827                  if (strcmp(nodename, "smp") == 0) {
13827 13828                          continue;
13828 13829                  }
13829 13830  
13830 13831                  addr = ddi_get_name_addr(savechild);
13831 13832                  if (addr == NULL) {
13832 13833                          continue;
13833 13834                  }
13834 13835  
13835 13836                  if (mptsas_parse_address(addr, &sas_wwn, &phy, &lun) !=
13836 13837                      DDI_SUCCESS) {
13837 13838                          continue;
13838 13839                  }
13839 13840  
13840 13841                  if (wwid == sas_wwn) {
13841 13842                          for (i = 0; i < lun_cnt; i++) {
13842 13843                                  if (repluns[i] == lun) {
13843 13844                                          find = 1;
13844 13845                                          break;
13845 13846                                  }
13846 13847                          }
13847 13848                  } else {
13848 13849                          continue;
13849 13850                  }
13850 13851                  if (find == 0) {
13851 13852                          /*
13852 13853                           * The lun has not been there already
13853 13854                           */
13854 13855                          (void) mptsas_offline_lun(pdip, savechild, NULL,
13855 13856                              NDI_DEVI_REMOVE);
13856 13857                  }
13857 13858          }
13858 13859  
13859 13860          pip = mdi_get_next_client_path(pdip, NULL);
13860 13861          while (pip) {
13861 13862                  find = 0;
13862 13863                  savepip = pip;
13863 13864                  addr = MDI_PI(pip)->pi_addr;
13864 13865  
13865 13866                  pip = mdi_get_next_client_path(pdip, pip);
13866 13867  
13867 13868                  if (addr == NULL) {
13868 13869                          continue;
13869 13870                  }
13870 13871  
13871 13872                  if (mptsas_parse_address(addr, &sas_wwn, &phy,
13872 13873                      &lun) != DDI_SUCCESS) {
13873 13874                          continue;
13874 13875                  }
13875 13876  
13876 13877                  if (sas_wwn == wwid) {
13877 13878                          for (i = 0; i < lun_cnt; i++) {
13878 13879                                  if (repluns[i] == lun) {
13879 13880                                          find = 1;
13880 13881                                          break;
13881 13882                                  }
13882 13883                          }
13883 13884                  } else {
13884 13885                          continue;
13885 13886                  }
13886 13887  
13887 13888                  if (find == 0) {
13888 13889                          /*
13889 13890                           * The lun has not been there already
13890 13891                           */
13891 13892                          (void) mptsas_offline_lun(pdip, NULL, savepip,
13892 13893                              NDI_DEVI_REMOVE);
13893 13894                  }
13894 13895          }
13895 13896  }
13896 13897  
13897 13898  void
13898 13899  mptsas_update_hashtab(struct mptsas *mpt)
13899 13900  {
13900 13901          uint32_t        page_address;
13901 13902          int             rval = 0;
13902 13903          uint16_t        dev_handle;
13903 13904          mptsas_target_t *ptgt = NULL;
13904 13905          mptsas_smp_t    smp_node;
13905 13906  
13906 13907          /*
13907 13908           * Get latest RAID info.
13908 13909           */
13909 13910          (void) mptsas_get_raid_info(mpt);
13910 13911  
13911 13912          dev_handle = mpt->m_smp_devhdl;
13912 13913          for (; mpt->m_done_traverse_smp == 0; ) {
13913 13914                  page_address = (MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL &
13914 13915                      MPI2_SAS_EXPAND_PGAD_FORM_MASK) | (uint32_t)dev_handle;
13915 13916                  if (mptsas_get_sas_expander_page0(mpt, page_address, &smp_node)
13916 13917                      != DDI_SUCCESS) {
13917 13918                          break;
13918 13919                  }
13919 13920                  mpt->m_smp_devhdl = dev_handle = smp_node.m_devhdl;
13920 13921                  (void) mptsas_smp_alloc(&mpt->m_active->m_smptbl, &smp_node);
13921 13922          }
13922 13923  
13923 13924          /*
13924 13925           * Config target devices
13925 13926           */
13926 13927          dev_handle = mpt->m_dev_handle;
13927 13928  
13928 13929          /*
13929 13930           * Do loop to get sas device page 0 by GetNextHandle till the
13930 13931           * the last handle. If the sas device is a SATA/SSP target,
13931 13932           * we try to config it.
13932 13933           */
13933 13934          for (; mpt->m_done_traverse_dev == 0; ) {
13934 13935                  ptgt = NULL;
13935 13936                  page_address =
13936 13937                      (MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE &
13937 13938                      MPI2_SAS_DEVICE_PGAD_FORM_MASK) |
13938 13939                      (uint32_t)dev_handle;
13939 13940                  rval = mptsas_get_target_device_info(mpt, page_address,
13940 13941                      &dev_handle, &ptgt);
13941 13942                  if ((rval == DEV_INFO_FAIL_PAGE0) ||
13942 13943                      (rval == DEV_INFO_FAIL_ALLOC)) {
13943 13944                          break;
13944 13945                  }
13945 13946  
13946 13947                  mpt->m_dev_handle = dev_handle;
13947 13948          }
13948 13949  
13949 13950  }
13950 13951  
13951 13952  void
13952 13953  mptsas_invalid_hashtab(mptsas_hash_table_t *hashtab)
13953 13954  {
13954 13955          mptsas_hash_data_t *data;
13955 13956          data = mptsas_hash_traverse(hashtab, MPTSAS_HASH_FIRST);
13956 13957          while (data != NULL) {
13957 13958                  data->devhdl = MPTSAS_INVALID_DEVHDL;
13958 13959                  data->device_info = 0;
13959 13960                  /*
13960 13961                   * For tgttbl, clear dr_flag.
13961 13962                   */
13962 13963                  data->dr_flag = MPTSAS_DR_INACTIVE;
13963 13964                  data = mptsas_hash_traverse(hashtab, MPTSAS_HASH_NEXT);
13964 13965          }
13965 13966  }
13966 13967  
13967 13968  void
13968 13969  mptsas_update_driver_data(struct mptsas *mpt)
13969 13970  {
13970 13971          /*
13971 13972           * TODO after hard reset, update the driver data structures
13972 13973           * 1. update port/phymask mapping table mpt->m_phy_info
13973 13974           * 2. invalid all the entries in hash table
13974 13975           *    m_devhdl = 0xffff and m_deviceinfo = 0
13975 13976           * 3. call sas_device_page/expander_page to update hash table
13976 13977           */
13977 13978          mptsas_update_phymask(mpt);
13978 13979          /*
13979 13980           * Invalid the existing entries
13980 13981           */
13981 13982          mptsas_invalid_hashtab(&mpt->m_active->m_tgttbl);
13982 13983          mptsas_invalid_hashtab(&mpt->m_active->m_smptbl);
13983 13984          mpt->m_done_traverse_dev = 0;
13984 13985          mpt->m_done_traverse_smp = 0;
13985 13986          mpt->m_dev_handle = mpt->m_smp_devhdl = MPTSAS_INVALID_DEVHDL;
13986 13987          mptsas_update_hashtab(mpt);
13987 13988  }
13988 13989  
13989 13990  static void
13990 13991  mptsas_config_all(dev_info_t *pdip)
13991 13992  {
13992 13993          dev_info_t      *smpdip = NULL;
13993 13994          mptsas_t        *mpt = DIP2MPT(pdip);
13994 13995          int             phymask = 0;
13995 13996          mptsas_phymask_t phy_mask;
13996 13997          mptsas_target_t *ptgt = NULL;
13997 13998          mptsas_smp_t    *psmp;
13998 13999  
13999 14000          /*
14000 14001           * Get the phymask associated to the iport
14001 14002           */
14002 14003          phymask = ddi_prop_get_int(DDI_DEV_T_ANY, pdip, 0,
14003 14004              "phymask", 0);
14004 14005  
14005 14006          /*
14006 14007           * Enumerate RAID volumes here (phymask == 0).
14007 14008           */
14008 14009          if (phymask == 0) {
14009 14010                  mptsas_config_all_viport(pdip);
14010 14011                  return;
14011 14012          }
14012 14013  
14013 14014          mutex_enter(&mpt->m_mutex);
14014 14015  
14015 14016          if (!mpt->m_done_traverse_dev || !mpt->m_done_traverse_smp) {
14016 14017                  mptsas_update_hashtab(mpt);
14017 14018          }
14018 14019  
14019 14020          psmp = (mptsas_smp_t *)mptsas_hash_traverse(&mpt->m_active->m_smptbl,
14020 14021              MPTSAS_HASH_FIRST);
14021 14022          while (psmp != NULL) {
14022 14023                  phy_mask = psmp->m_phymask;
14023 14024                  if (phy_mask == phymask) {
14024 14025                          smpdip = NULL;
14025 14026                          mutex_exit(&mpt->m_mutex);
14026 14027                          (void) mptsas_online_smp(pdip, psmp, &smpdip);
14027 14028                          mutex_enter(&mpt->m_mutex);
14028 14029                  }
14029 14030                  psmp = (mptsas_smp_t *)mptsas_hash_traverse(
14030 14031                      &mpt->m_active->m_smptbl, MPTSAS_HASH_NEXT);
14031 14032          }
14032 14033  
14033 14034          ptgt = (mptsas_target_t *)mptsas_hash_traverse(&mpt->m_active->m_tgttbl,
14034 14035              MPTSAS_HASH_FIRST);
14035 14036          while (ptgt != NULL) {
14036 14037                  phy_mask = ptgt->m_phymask;
14037 14038                  if (phy_mask == phymask) {
14038 14039                          mutex_exit(&mpt->m_mutex);
14039 14040                          (void) mptsas_config_target(pdip, ptgt);
14040 14041                          mutex_enter(&mpt->m_mutex);
14041 14042                  }
14042 14043  
14043 14044                  ptgt = (mptsas_target_t *)mptsas_hash_traverse(
14044 14045                      &mpt->m_active->m_tgttbl, MPTSAS_HASH_NEXT);
14045 14046          }
14046 14047          mutex_exit(&mpt->m_mutex);
14047 14048  }
14048 14049  
14049 14050  static int
14050 14051  mptsas_config_target(dev_info_t *pdip, mptsas_target_t *ptgt)
14051 14052  {
14052 14053          int             rval = DDI_FAILURE;
14053 14054          dev_info_t      *tdip;
14054 14055  
14055 14056          rval = mptsas_config_luns(pdip, ptgt);
14056 14057          if (rval != DDI_SUCCESS) {
14057 14058                  /*
14058 14059                   * The return value means the SCMD_REPORT_LUNS
14059 14060                   * did not execute successfully. The target maybe
14060 14061                   * doesn't support such command.
14061 14062                   */
14062 14063                  rval = mptsas_probe_lun(pdip, 0, &tdip, ptgt);
14063 14064          }
14064 14065          return (rval);
14065 14066  }
14066 14067  
14067 14068  /*
14068 14069   * Return fail if not all the childs/paths are freed.
14069 14070   * if there is any path under the HBA, the return value will be always fail
14070 14071   * because we didn't call mdi_pi_free for path
14071 14072   */
14072 14073  static int
14073 14074  mptsas_offline_target(dev_info_t *pdip, char *name)
14074 14075  {
14075 14076          dev_info_t              *child = NULL, *prechild = NULL;
14076 14077          mdi_pathinfo_t          *pip = NULL, *savepip = NULL;
14077 14078          int                     tmp_rval, rval = DDI_SUCCESS;
14078 14079          char                    *addr, *cp;
14079 14080          size_t                  s;
14080 14081          mptsas_t                *mpt = DIP2MPT(pdip);
14081 14082  
14082 14083          child = ddi_get_child(pdip);
14083 14084          while (child) {
14084 14085                  addr = ddi_get_name_addr(child);
14085 14086                  prechild = child;
14086 14087                  child = ddi_get_next_sibling(child);
14087 14088  
14088 14089                  if (addr == NULL) {
14089 14090                          continue;
14090 14091                  }
14091 14092                  if ((cp = strchr(addr, ',')) == NULL) {
14092 14093                          continue;
14093 14094                  }
14094 14095  
14095 14096                  s = (uintptr_t)cp - (uintptr_t)addr;
14096 14097  
14097 14098                  if (strncmp(addr, name, s) != 0) {
14098 14099                          continue;
14099 14100                  }
14100 14101  
14101 14102                  tmp_rval = mptsas_offline_lun(pdip, prechild, NULL,
14102 14103                      NDI_DEVI_REMOVE);
14103 14104                  if (tmp_rval != DDI_SUCCESS) {
14104 14105                          rval = DDI_FAILURE;
14105 14106                          if (ndi_prop_create_boolean(DDI_DEV_T_NONE,
14106 14107                              prechild, MPTSAS_DEV_GONE) !=
14107 14108                              DDI_PROP_SUCCESS) {
14108 14109                                  mptsas_log(mpt, CE_WARN, "mptsas driver "
14109 14110                                      "unable to create property for "
14110 14111                                      "SAS %s (MPTSAS_DEV_GONE)", addr);
14111 14112                          }
14112 14113                  }
14113 14114          }
14114 14115  
14115 14116          pip = mdi_get_next_client_path(pdip, NULL);
14116 14117          while (pip) {
14117 14118                  addr = MDI_PI(pip)->pi_addr;
14118 14119                  savepip = pip;
14119 14120                  pip = mdi_get_next_client_path(pdip, pip);
14120 14121                  if (addr == NULL) {
14121 14122                          continue;
14122 14123                  }
14123 14124  
14124 14125                  if ((cp = strchr(addr, ',')) == NULL) {
14125 14126                          continue;
14126 14127                  }
14127 14128  
14128 14129                  s = (uintptr_t)cp - (uintptr_t)addr;
14129 14130  
14130 14131                  if (strncmp(addr, name, s) != 0) {
14131 14132                          continue;
14132 14133                  }
14133 14134  
14134 14135                  (void) mptsas_offline_lun(pdip, NULL, savepip,
14135 14136                      NDI_DEVI_REMOVE);
14136 14137                  /*
14137 14138                   * driver will not invoke mdi_pi_free, so path will not
14138 14139                   * be freed forever, return DDI_FAILURE.
14139 14140                   */
14140 14141                  rval = DDI_FAILURE;
14141 14142          }
14142 14143          return (rval);
14143 14144  }
14144 14145  
14145 14146  static int
14146 14147  mptsas_offline_lun(dev_info_t *pdip, dev_info_t *rdip,
14147 14148      mdi_pathinfo_t *rpip, uint_t flags)
14148 14149  {
14149 14150          int             rval = DDI_FAILURE;
14150 14151          char            *devname;
14151 14152          dev_info_t      *cdip, *parent;
14152 14153  
14153 14154          if (rpip != NULL) {
14154 14155                  parent = scsi_vhci_dip;
14155 14156                  cdip = mdi_pi_get_client(rpip);
14156 14157          } else if (rdip != NULL) {
14157 14158                  parent = pdip;
14158 14159                  cdip = rdip;
14159 14160          } else {
14160 14161                  return (DDI_FAILURE);
14161 14162          }
14162 14163  
14163 14164          /*
14164 14165           * Make sure node is attached otherwise
14165 14166           * it won't have related cache nodes to
14166 14167           * clean up.  i_ddi_devi_attached is
14167 14168           * similiar to i_ddi_node_state(cdip) >=
14168 14169           * DS_ATTACHED.
14169 14170           */
14170 14171          if (i_ddi_devi_attached(cdip)) {
14171 14172  
14172 14173                  /* Get full devname */
14173 14174                  devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
14174 14175                  (void) ddi_deviname(cdip, devname);
14175 14176                  /* Clean cache */
14176 14177                  (void) devfs_clean(parent, devname + 1,
14177 14178                      DV_CLEAN_FORCE);
14178 14179                  kmem_free(devname, MAXNAMELEN + 1);
14179 14180          }
14180 14181          if (rpip != NULL) {
14181 14182                  if (MDI_PI_IS_OFFLINE(rpip)) {
14182 14183                          rval = DDI_SUCCESS;
14183 14184                  } else {
14184 14185                          rval = mdi_pi_offline(rpip, 0);
14185 14186                  }
14186 14187          } else {
14187 14188                  rval = ndi_devi_offline(cdip, flags);
14188 14189          }
14189 14190  
14190 14191          return (rval);
14191 14192  }
14192 14193  
14193 14194  static dev_info_t *
14194 14195  mptsas_find_smp_child(dev_info_t *parent, char *str_wwn)
14195 14196  {
14196 14197          dev_info_t      *child = NULL;
14197 14198          char            *smp_wwn = NULL;
14198 14199  
14199 14200          child = ddi_get_child(parent);
14200 14201          while (child) {
14201 14202                  if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
14202 14203                      DDI_PROP_DONTPASS, SMP_WWN, &smp_wwn)
14203 14204                      != DDI_SUCCESS) {
14204 14205                          child = ddi_get_next_sibling(child);
14205 14206                          continue;
14206 14207                  }
14207 14208  
14208 14209                  if (strcmp(smp_wwn, str_wwn) == 0) {
14209 14210                          ddi_prop_free(smp_wwn);
14210 14211                          break;
14211 14212                  }
14212 14213                  child = ddi_get_next_sibling(child);
14213 14214                  ddi_prop_free(smp_wwn);
14214 14215          }
14215 14216          return (child);
14216 14217  }
14217 14218  
14218 14219  static int
14219 14220  mptsas_offline_smp(dev_info_t *pdip, mptsas_smp_t *smp_node, uint_t flags)
14220 14221  {
14221 14222          int             rval = DDI_FAILURE;
14222 14223          char            *devname;
14223 14224          char            wwn_str[MPTSAS_WWN_STRLEN];
14224 14225          dev_info_t      *cdip;
14225 14226  
14226 14227          (void) sprintf(wwn_str, "%"PRIx64, smp_node->m_sasaddr);
14227 14228  
14228 14229          cdip = mptsas_find_smp_child(pdip, wwn_str);
14229 14230  
14230 14231          if (cdip == NULL)
14231 14232                  return (DDI_SUCCESS);
14232 14233  
14233 14234          /*
14234 14235           * Make sure node is attached otherwise
14235 14236           * it won't have related cache nodes to
14236 14237           * clean up.  i_ddi_devi_attached is
14237 14238           * similiar to i_ddi_node_state(cdip) >=
14238 14239           * DS_ATTACHED.
14239 14240           */
14240 14241          if (i_ddi_devi_attached(cdip)) {
14241 14242  
14242 14243                  /* Get full devname */
14243 14244                  devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
14244 14245                  (void) ddi_deviname(cdip, devname);
14245 14246                  /* Clean cache */
14246 14247                  (void) devfs_clean(pdip, devname + 1,
14247 14248                      DV_CLEAN_FORCE);
14248 14249                  kmem_free(devname, MAXNAMELEN + 1);
14249 14250          }
14250 14251  
14251 14252          rval = ndi_devi_offline(cdip, flags);
14252 14253  
14253 14254          return (rval);
14254 14255  }
14255 14256  
14256 14257  static dev_info_t *
14257 14258  mptsas_find_child(dev_info_t *pdip, char *name)
14258 14259  {
14259 14260          dev_info_t      *child = NULL;
14260 14261          char            *rname = NULL;
14261 14262          int             rval = DDI_FAILURE;
14262 14263  
14263 14264          rname = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14264 14265  
14265 14266          child = ddi_get_child(pdip);
14266 14267          while (child) {
14267 14268                  rval = mptsas_name_child(child, rname, SCSI_MAXNAMELEN);
14268 14269                  if (rval != DDI_SUCCESS) {
14269 14270                          child = ddi_get_next_sibling(child);
14270 14271                          bzero(rname, SCSI_MAXNAMELEN);
14271 14272                          continue;
14272 14273                  }
14273 14274  
14274 14275                  if (strcmp(rname, name) == 0) {
14275 14276                          break;
14276 14277                  }
14277 14278                  child = ddi_get_next_sibling(child);
14278 14279                  bzero(rname, SCSI_MAXNAMELEN);
14279 14280          }
14280 14281  
14281 14282          kmem_free(rname, SCSI_MAXNAMELEN);
14282 14283  
14283 14284          return (child);
14284 14285  }
14285 14286  
14286 14287  
14287 14288  static dev_info_t *
14288 14289  mptsas_find_child_addr(dev_info_t *pdip, uint64_t sasaddr, int lun)
14289 14290  {
14290 14291          dev_info_t      *child = NULL;
14291 14292          char            *name = NULL;
14292 14293          char            *addr = NULL;
14293 14294  
14294 14295          name = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14295 14296          addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14296 14297          (void) sprintf(name, "%016"PRIx64, sasaddr);
14297 14298          (void) sprintf(addr, "w%s,%x", name, lun);
14298 14299          child = mptsas_find_child(pdip, addr);
14299 14300          kmem_free(name, SCSI_MAXNAMELEN);
14300 14301          kmem_free(addr, SCSI_MAXNAMELEN);
14301 14302          return (child);
14302 14303  }
14303 14304  
14304 14305  static dev_info_t *
14305 14306  mptsas_find_child_phy(dev_info_t *pdip, uint8_t phy)
14306 14307  {
14307 14308          dev_info_t      *child;
14308 14309          char            *addr;
14309 14310  
14310 14311          addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14311 14312          (void) sprintf(addr, "p%x,0", phy);
14312 14313          child = mptsas_find_child(pdip, addr);
14313 14314          kmem_free(addr, SCSI_MAXNAMELEN);
14314 14315          return (child);
14315 14316  }
14316 14317  
14317 14318  static mdi_pathinfo_t *
14318 14319  mptsas_find_path_phy(dev_info_t *pdip, uint8_t phy)
14319 14320  {
14320 14321          mdi_pathinfo_t  *path;
14321 14322          char            *addr = NULL;
14322 14323  
14323 14324          addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14324 14325          (void) sprintf(addr, "p%x,0", phy);
14325 14326          path = mdi_pi_find(pdip, NULL, addr);
14326 14327          kmem_free(addr, SCSI_MAXNAMELEN);
14327 14328          return (path);
14328 14329  }
14329 14330  
14330 14331  static mdi_pathinfo_t *
14331 14332  mptsas_find_path_addr(dev_info_t *parent, uint64_t sasaddr, int lun)
14332 14333  {
14333 14334          mdi_pathinfo_t  *path;
14334 14335          char            *name = NULL;
14335 14336          char            *addr = NULL;
14336 14337  
14337 14338          name = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14338 14339          addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14339 14340          (void) sprintf(name, "%016"PRIx64, sasaddr);
14340 14341          (void) sprintf(addr, "w%s,%x", name, lun);
14341 14342          path = mdi_pi_find(parent, NULL, addr);
14342 14343          kmem_free(name, SCSI_MAXNAMELEN);
14343 14344          kmem_free(addr, SCSI_MAXNAMELEN);
14344 14345  
14345 14346          return (path);
14346 14347  }
14347 14348  
14348 14349  static int
14349 14350  mptsas_create_lun(dev_info_t *pdip, struct scsi_inquiry *sd_inq,
14350 14351      dev_info_t **lun_dip, mptsas_target_t *ptgt, int lun)
14351 14352  {
14352 14353          int                     i = 0;
14353 14354          uchar_t                 *inq83 = NULL;
14354 14355          int                     inq83_len1 = 0xFF;
14355 14356          int                     inq83_len = 0;
14356 14357          int                     rval = DDI_FAILURE;
14357 14358          ddi_devid_t             devid;
14358 14359          char                    *guid = NULL;
14359 14360          int                     target = ptgt->m_devhdl;
14360 14361          mdi_pathinfo_t          *pip = NULL;
14361 14362          mptsas_t                *mpt = DIP2MPT(pdip);
14362 14363  
14363 14364          /*
14364 14365           * For DVD/CD ROM and tape devices and optical
14365 14366           * devices, we won't try to enumerate them under
14366 14367           * scsi_vhci, so no need to try page83
14367 14368           */
14368 14369          if (sd_inq && (sd_inq->inq_dtype == DTYPE_RODIRECT ||
14369 14370              sd_inq->inq_dtype == DTYPE_OPTICAL ||
14370 14371              sd_inq->inq_dtype == DTYPE_ESI))
14371 14372                  goto create_lun;
14372 14373  
14373 14374          /*
14374 14375           * The LCA returns good SCSI status, but corrupt page 83 data the first
14375 14376           * time it is queried. The solution is to keep trying to request page83
14376 14377           * and verify the GUID is not (DDI_NOT_WELL_FORMED) in
14377 14378           * mptsas_inq83_retry_timeout seconds. If the timeout expires, driver
14378 14379           * give up to get VPD page at this stage and fail the enumeration.
14379 14380           */
14380 14381  
14381 14382          inq83   = kmem_zalloc(inq83_len1, KM_SLEEP);
14382 14383  
14383 14384          for (i = 0; i < mptsas_inq83_retry_timeout; i++) {
14384 14385                  rval = mptsas_inquiry(mpt, ptgt, lun, 0x83, inq83,
14385 14386                      inq83_len1, &inq83_len, 1);
14386 14387                  if (rval != 0) {
14387 14388                          mptsas_log(mpt, CE_WARN, "!mptsas request inquiry page "
14388 14389                              "0x83 for target:%x, lun:%x failed!", target, lun);
14389 14390                          if (mptsas_physical_bind_failed_page_83 != B_FALSE)
14390 14391                                  goto create_lun;
14391 14392                          goto out;
14392 14393                  }
14393 14394                  /*
14394 14395                   * create DEVID from inquiry data
14395 14396                   */
14396 14397                  if ((rval = ddi_devid_scsi_encode(
14397 14398                      DEVID_SCSI_ENCODE_VERSION_LATEST, NULL, (uchar_t *)sd_inq,
14398 14399                      sizeof (struct scsi_inquiry), NULL, 0, inq83,
14399 14400                      (size_t)inq83_len, &devid)) == DDI_SUCCESS) {
14400 14401                          /*
14401 14402                           * extract GUID from DEVID
14402 14403                           */
14403 14404                          guid = ddi_devid_to_guid(devid);
14404 14405  
14405 14406                          /*
14406 14407                           * Do not enable MPXIO if the strlen(guid) is greater
14407 14408                           * than MPTSAS_MAX_GUID_LEN, this constrain would be
14408 14409                           * handled by framework later.
14409 14410                           */
14410 14411                          if (guid && (strlen(guid) > MPTSAS_MAX_GUID_LEN)) {
14411 14412                                  ddi_devid_free_guid(guid);
14412 14413                                  guid = NULL;
14413 14414                                  if (mpt->m_mpxio_enable == TRUE) {
14414 14415                                          mptsas_log(mpt, CE_NOTE, "!Target:%x, "
14415 14416                                              "lun:%x doesn't have a valid GUID, "
14416 14417                                              "multipathing for this drive is "
14417 14418                                              "not enabled", target, lun);
14418 14419                                  }
14419 14420                          }
14420 14421  
14421 14422                          /*
14422 14423                           * devid no longer needed
14423 14424                           */
14424 14425                          ddi_devid_free(devid);
14425 14426                          break;
14426 14427                  } else if (rval == DDI_NOT_WELL_FORMED) {
14427 14428                          /*
14428 14429                           * return value of ddi_devid_scsi_encode equal to
14429 14430                           * DDI_NOT_WELL_FORMED means DEVID_RETRY, it worth
14430 14431                           * to retry inquiry page 0x83 and get GUID.
14431 14432                           */
14432 14433                          NDBG20(("Not well formed devid, retry..."));
14433 14434                          delay(1 * drv_usectohz(1000000));
14434 14435                          continue;
14435 14436                  } else {
14436 14437                          mptsas_log(mpt, CE_WARN, "!Encode devid failed for "
14437 14438                              "path target:%x, lun:%x", target, lun);
14438 14439                          rval = DDI_FAILURE;
14439 14440                          goto create_lun;
14440 14441                  }
14441 14442          }
14442 14443  
14443 14444          if (i == mptsas_inq83_retry_timeout) {
14444 14445                  mptsas_log(mpt, CE_WARN, "!Repeated page83 requests timeout "
14445 14446                      "for path target:%x, lun:%x", target, lun);
14446 14447          }
14447 14448  
14448 14449          rval = DDI_FAILURE;
14449 14450  
14450 14451  create_lun:
14451 14452          if ((guid != NULL) && (mpt->m_mpxio_enable == TRUE)) {
14452 14453                  rval = mptsas_create_virt_lun(pdip, sd_inq, guid, lun_dip, &pip,
14453 14454                      ptgt, lun);
14454 14455          }
14455 14456          if (rval != DDI_SUCCESS) {
14456 14457                  rval = mptsas_create_phys_lun(pdip, sd_inq, guid, lun_dip,
14457 14458                      ptgt, lun);
14458 14459  
14459 14460          }
14460 14461  out:
14461 14462          if (guid != NULL) {
14462 14463                  /*
14463 14464                   * guid no longer needed
14464 14465                   */
14465 14466                  ddi_devid_free_guid(guid);
14466 14467          }
14467 14468          if (inq83 != NULL)
14468 14469                  kmem_free(inq83, inq83_len1);
14469 14470          return (rval);
14470 14471  }
14471 14472  
14472 14473  static int
14473 14474  mptsas_create_virt_lun(dev_info_t *pdip, struct scsi_inquiry *inq, char *guid,
14474 14475      dev_info_t **lun_dip, mdi_pathinfo_t **pip, mptsas_target_t *ptgt, int lun)
14475 14476  {
14476 14477          int                     target;
14477 14478          char                    *nodename = NULL;
14478 14479          char                    **compatible = NULL;
14479 14480          int                     ncompatible     = 0;
14480 14481          int                     mdi_rtn = MDI_FAILURE;
14481 14482          int                     rval = DDI_FAILURE;
14482 14483          char                    *old_guid = NULL;
14483 14484          mptsas_t                *mpt = DIP2MPT(pdip);
14484 14485          char                    *lun_addr = NULL;
14485 14486          char                    *wwn_str = NULL;
14486 14487          char                    *attached_wwn_str = NULL;
14487 14488          char                    *component = NULL;
14488 14489          uint8_t                 phy = 0xFF;
14489 14490          uint64_t                sas_wwn;
14490 14491          int64_t                 lun64 = 0;
14491 14492          uint32_t                devinfo;
14492 14493          uint16_t                dev_hdl;
14493 14494          uint16_t                pdev_hdl;
14494 14495          uint64_t                dev_sas_wwn;
14495 14496          uint64_t                pdev_sas_wwn;
14496 14497          uint32_t                pdev_info;
14497 14498          uint8_t                 physport;
14498 14499          uint8_t                 phy_id;
14499 14500          uint32_t                page_address;
14500 14501          uint16_t                bay_num, enclosure;
14501 14502          char                    pdev_wwn_str[MPTSAS_WWN_STRLEN];
14502 14503          uint32_t                dev_info;
14503 14504  
14504 14505          mutex_enter(&mpt->m_mutex);
14505 14506          target = ptgt->m_devhdl;
14506 14507          sas_wwn = ptgt->m_sas_wwn;
14507 14508          devinfo = ptgt->m_deviceinfo;
14508 14509          phy = ptgt->m_phynum;
14509 14510          mutex_exit(&mpt->m_mutex);
14510 14511  
14511 14512          if (sas_wwn) {
14512 14513                  *pip = mptsas_find_path_addr(pdip, sas_wwn, lun);
14513 14514          } else {
14514 14515                  *pip = mptsas_find_path_phy(pdip, phy);
14515 14516          }
14516 14517  
14517 14518          if (*pip != NULL) {
14518 14519                  *lun_dip = MDI_PI(*pip)->pi_client->ct_dip;
14519 14520                  ASSERT(*lun_dip != NULL);
14520 14521                  if (ddi_prop_lookup_string(DDI_DEV_T_ANY, *lun_dip,
14521 14522                      (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
14522 14523                      MDI_CLIENT_GUID_PROP, &old_guid) == DDI_SUCCESS) {
14523 14524                          if (strncmp(guid, old_guid, strlen(guid)) == 0) {
14524 14525                                  /*
14525 14526                                   * Same path back online again.
14526 14527                                   */
14527 14528                                  (void) ddi_prop_free(old_guid);
14528 14529                                  if ((!MDI_PI_IS_ONLINE(*pip)) &&
14529 14530                                      (!MDI_PI_IS_STANDBY(*pip)) &&
14530 14531                                      (ptgt->m_tgt_unconfigured == 0)) {
14531 14532                                          rval = mdi_pi_online(*pip, 0);
14532 14533                                          mutex_enter(&mpt->m_mutex);
14533 14534                                          (void) mptsas_set_led_status(mpt, ptgt,
14534 14535                                              0);
14535 14536                                          mutex_exit(&mpt->m_mutex);
14536 14537                                  } else {
14537 14538                                          rval = DDI_SUCCESS;
14538 14539                                  }
14539 14540                                  if (rval != DDI_SUCCESS) {
14540 14541                                          mptsas_log(mpt, CE_WARN, "path:target: "
14541 14542                                              "%x, lun:%x online failed!", target,
14542 14543                                              lun);
14543 14544                                          *pip = NULL;
14544 14545                                          *lun_dip = NULL;
14545 14546                                  }
14546 14547                                  return (rval);
14547 14548                          } else {
14548 14549                                  /*
14549 14550                                   * The GUID of the LUN has changed which maybe
14550 14551                                   * because customer mapped another volume to the
14551 14552                                   * same LUN.
14552 14553                                   */
14553 14554                                  mptsas_log(mpt, CE_WARN, "The GUID of the "
14554 14555                                      "target:%x, lun:%x was changed, maybe "
14555 14556                                      "because someone mapped another volume "
14556 14557                                      "to the same LUN", target, lun);
14557 14558                                  (void) ddi_prop_free(old_guid);
14558 14559                                  if (!MDI_PI_IS_OFFLINE(*pip)) {
14559 14560                                          rval = mdi_pi_offline(*pip, 0);
14560 14561                                          if (rval != MDI_SUCCESS) {
14561 14562                                                  mptsas_log(mpt, CE_WARN, "path:"
14562 14563                                                      "target:%x, lun:%x offline "
14563 14564                                                      "failed!", target, lun);
14564 14565                                                  *pip = NULL;
14565 14566                                                  *lun_dip = NULL;
14566 14567                                                  return (DDI_FAILURE);
14567 14568                                          }
14568 14569                                  }
14569 14570                                  if (mdi_pi_free(*pip, 0) != MDI_SUCCESS) {
14570 14571                                          mptsas_log(mpt, CE_WARN, "path:target:"
14571 14572                                              "%x, lun:%x free failed!", target,
14572 14573                                              lun);
14573 14574                                          *pip = NULL;
14574 14575                                          *lun_dip = NULL;
14575 14576                                          return (DDI_FAILURE);
14576 14577                                  }
14577 14578                          }
14578 14579                  } else {
14579 14580                          mptsas_log(mpt, CE_WARN, "Can't get client-guid "
14580 14581                              "property for path:target:%x, lun:%x", target, lun);
14581 14582                          *pip = NULL;
14582 14583                          *lun_dip = NULL;
14583 14584                          return (DDI_FAILURE);
14584 14585                  }
14585 14586          }
14586 14587          scsi_hba_nodename_compatible_get(inq, NULL,
14587 14588              inq->inq_dtype, NULL, &nodename, &compatible, &ncompatible);
14588 14589  
14589 14590          /*
14590 14591           * if nodename can't be determined then print a message and skip it
14591 14592           */
14592 14593          if (nodename == NULL) {
14593 14594                  mptsas_log(mpt, CE_WARN, "mptsas driver found no compatible "
14594 14595                      "driver for target%d lun %d dtype:0x%02x", target, lun,
14595 14596                      inq->inq_dtype);
14596 14597                  return (DDI_FAILURE);
14597 14598          }
14598 14599  
14599 14600          wwn_str = kmem_zalloc(MPTSAS_WWN_STRLEN, KM_SLEEP);
14600 14601          /* The property is needed by MPAPI */
14601 14602          (void) sprintf(wwn_str, "%016"PRIx64, sas_wwn);
14602 14603  
14603 14604          lun_addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14604 14605          if (guid) {
14605 14606                  (void) sprintf(lun_addr, "w%s,%x", wwn_str, lun);
14606 14607                  (void) sprintf(wwn_str, "w%016"PRIx64, sas_wwn);
14607 14608          } else {
14608 14609                  (void) sprintf(lun_addr, "p%x,%x", phy, lun);
14609 14610                  (void) sprintf(wwn_str, "p%x", phy);
14610 14611          }
14611 14612  
14612 14613          mdi_rtn = mdi_pi_alloc_compatible(pdip, nodename,
14613 14614              guid, lun_addr, compatible, ncompatible,
14614 14615              0, pip);
14615 14616          if (mdi_rtn == MDI_SUCCESS) {
14616 14617  
14617 14618                  if (mdi_prop_update_string(*pip, MDI_GUID,
14618 14619                      guid) != DDI_SUCCESS) {
14619 14620                          mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
14620 14621                              "create prop for target %d lun %d (MDI_GUID)",
14621 14622                              target, lun);
14622 14623                          mdi_rtn = MDI_FAILURE;
14623 14624                          goto virt_create_done;
14624 14625                  }
14625 14626  
14626 14627                  if (mdi_prop_update_int(*pip, LUN_PROP,
14627 14628                      lun) != DDI_SUCCESS) {
14628 14629                          mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
14629 14630                              "create prop for target %d lun %d (LUN_PROP)",
14630 14631                              target, lun);
14631 14632                          mdi_rtn = MDI_FAILURE;
14632 14633                          goto virt_create_done;
14633 14634                  }
14634 14635                  lun64 = (int64_t)lun;
14635 14636                  if (mdi_prop_update_int64(*pip, LUN64_PROP,
14636 14637                      lun64) != DDI_SUCCESS) {
14637 14638                          mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
14638 14639                              "create prop for target %d (LUN64_PROP)",
14639 14640                              target);
14640 14641                          mdi_rtn = MDI_FAILURE;
14641 14642                          goto virt_create_done;
14642 14643                  }
14643 14644                  if (mdi_prop_update_string_array(*pip, "compatible",
14644 14645                      compatible, ncompatible) !=
14645 14646                      DDI_PROP_SUCCESS) {
14646 14647                          mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
14647 14648                              "create prop for target %d lun %d (COMPATIBLE)",
14648 14649                              target, lun);
14649 14650                          mdi_rtn = MDI_FAILURE;
14650 14651                          goto virt_create_done;
14651 14652                  }
14652 14653                  if (sas_wwn && (mdi_prop_update_string(*pip,
14653 14654                      SCSI_ADDR_PROP_TARGET_PORT, wwn_str) != DDI_PROP_SUCCESS)) {
14654 14655                          mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
14655 14656                              "create prop for target %d lun %d "
14656 14657                              "(target-port)", target, lun);
14657 14658                          mdi_rtn = MDI_FAILURE;
14658 14659                          goto virt_create_done;
14659 14660                  } else if ((sas_wwn == 0) && (mdi_prop_update_int(*pip,
14660 14661                      "sata-phy", phy) != DDI_PROP_SUCCESS)) {
14661 14662                          /*
14662 14663                           * Direct attached SATA device without DeviceName
14663 14664                           */
14664 14665                          mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
14665 14666                              "create prop for SAS target %d lun %d "
14666 14667                              "(sata-phy)", target, lun);
14667 14668                          mdi_rtn = MDI_FAILURE;
14668 14669                          goto virt_create_done;
14669 14670                  }
14670 14671                  mutex_enter(&mpt->m_mutex);
14671 14672  
14672 14673                  page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
14673 14674                      MPI2_SAS_DEVICE_PGAD_FORM_MASK) |
14674 14675                      (uint32_t)ptgt->m_devhdl;
14675 14676                  rval = mptsas_get_sas_device_page0(mpt, page_address,
14676 14677                      &dev_hdl, &dev_sas_wwn, &dev_info, &physport,
14677 14678                      &phy_id, &pdev_hdl, &bay_num, &enclosure);
14678 14679                  if (rval != DDI_SUCCESS) {
14679 14680                          mutex_exit(&mpt->m_mutex);
14680 14681                          mptsas_log(mpt, CE_WARN, "mptsas unable to get "
14681 14682                              "parent device for handle %d", page_address);
14682 14683                          mdi_rtn = MDI_FAILURE;
14683 14684                          goto virt_create_done;
14684 14685                  }
14685 14686  
14686 14687                  page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
14687 14688                      MPI2_SAS_DEVICE_PGAD_FORM_MASK) | (uint32_t)pdev_hdl;
14688 14689                  rval = mptsas_get_sas_device_page0(mpt, page_address,
14689 14690                      &dev_hdl, &pdev_sas_wwn, &pdev_info, &physport,
14690 14691                      &phy_id, &pdev_hdl, &bay_num, &enclosure);
14691 14692                  if (rval != DDI_SUCCESS) {
14692 14693                          mutex_exit(&mpt->m_mutex);
14693 14694                          mptsas_log(mpt, CE_WARN, "mptsas unable to get"
14694 14695                              "device info for handle %d", page_address);
14695 14696                          mdi_rtn = MDI_FAILURE;
14696 14697                          goto virt_create_done;
14697 14698                  }
14698 14699  
14699 14700                  mutex_exit(&mpt->m_mutex);
14700 14701  
14701 14702                  /*
14702 14703                   * If this device direct attached to the controller
14703 14704                   * set the attached-port to the base wwid
14704 14705                   */
14705 14706                  if ((ptgt->m_deviceinfo & DEVINFO_DIRECT_ATTACHED)
14706 14707                      != DEVINFO_DIRECT_ATTACHED) {
14707 14708                          (void) sprintf(pdev_wwn_str, "w%016"PRIx64,
14708 14709                              pdev_sas_wwn);
14709 14710                  } else {
14710 14711                          /*
14711 14712                           * Update the iport's attached-port to guid
14712 14713                           */
14713 14714                          if (sas_wwn == 0) {
14714 14715                                  (void) sprintf(wwn_str, "p%x", phy);
14715 14716                          } else {
14716 14717                                  (void) sprintf(wwn_str, "w%016"PRIx64, sas_wwn);
14717 14718                          }
14718 14719                          if (ddi_prop_update_string(DDI_DEV_T_NONE,
14719 14720                              pdip, SCSI_ADDR_PROP_ATTACHED_PORT, wwn_str) !=
14720 14721                              DDI_PROP_SUCCESS) {
14721 14722                                  mptsas_log(mpt, CE_WARN,
14722 14723                                      "mptsas unable to create "
14723 14724                                      "property for iport target-port"
14724 14725                                      " %s (sas_wwn)",
14725 14726                                      wwn_str);
14726 14727                                  mdi_rtn = MDI_FAILURE;
14727 14728                                  goto virt_create_done;
14728 14729                          }
14729 14730  
14730 14731                          (void) sprintf(pdev_wwn_str, "w%016"PRIx64,
14731 14732                              mpt->un.m_base_wwid);
14732 14733                  }
14733 14734  
14734 14735                  if (mdi_prop_update_string(*pip,
14735 14736                      SCSI_ADDR_PROP_ATTACHED_PORT, pdev_wwn_str) !=
14736 14737                      DDI_PROP_SUCCESS) {
14737 14738                          mptsas_log(mpt, CE_WARN, "mptsas unable to create "
14738 14739                              "property for iport attached-port %s (sas_wwn)",
14739 14740                              attached_wwn_str);
14740 14741                          mdi_rtn = MDI_FAILURE;
14741 14742                          goto virt_create_done;
14742 14743                  }
14743 14744  
14744 14745  
14745 14746                  if (inq->inq_dtype == 0) {
14746 14747                          component = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
14747 14748                          /*
14748 14749                           * set obp path for pathinfo
14749 14750                           */
14750 14751                          (void) snprintf(component, MAXPATHLEN,
14751 14752                              "disk@%s", lun_addr);
14752 14753  
14753 14754                          if (mdi_pi_pathname_obp_set(*pip, component) !=
14754 14755                              DDI_SUCCESS) {
14755 14756                                  mptsas_log(mpt, CE_WARN, "mpt_sas driver "
14756 14757                                      "unable to set obp-path for object %s",
14757 14758                                      component);
14758 14759                                  mdi_rtn = MDI_FAILURE;
14759 14760                                  goto virt_create_done;
14760 14761                          }
14761 14762                  }
14762 14763  
14763 14764                  *lun_dip = MDI_PI(*pip)->pi_client->ct_dip;
14764 14765                  if (devinfo & (MPI2_SAS_DEVICE_INFO_SATA_DEVICE |
14765 14766                      MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE)) {
14766 14767                          if ((ndi_prop_update_int(DDI_DEV_T_NONE, *lun_dip,
14767 14768                              "pm-capable", 1)) !=
14768 14769                              DDI_PROP_SUCCESS) {
14769 14770                                  mptsas_log(mpt, CE_WARN, "mptsas driver"
14770 14771                                      "failed to create pm-capable "
14771 14772                                      "property, target %d", target);
14772 14773                                  mdi_rtn = MDI_FAILURE;
14773 14774                                  goto virt_create_done;
14774 14775                          }
14775 14776                  }
14776 14777                  /*
14777 14778                   * Create the phy-num property
14778 14779                   */
14779 14780                  if (mdi_prop_update_int(*pip, "phy-num",
14780 14781                      ptgt->m_phynum) != DDI_SUCCESS) {
14781 14782                          mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
14782 14783                              "create phy-num property for target %d lun %d",
14783 14784                              target, lun);
14784 14785                          mdi_rtn = MDI_FAILURE;
14785 14786                          goto virt_create_done;
14786 14787                  }
14787 14788                  NDBG20(("new path:%s onlining,", MDI_PI(*pip)->pi_addr));
14788 14789                  mdi_rtn = mdi_pi_online(*pip, 0);
14789 14790                  if (mdi_rtn == MDI_SUCCESS) {
14790 14791                          mutex_enter(&mpt->m_mutex);
14791 14792                          if (mptsas_set_led_status(mpt, ptgt, 0) !=
14792 14793                              DDI_SUCCESS) {
14793 14794                                  NDBG14(("mptsas: clear LED for slot %x "
14794 14795                                      "failed", ptgt->m_slot_num));
14795 14796                          }
14796 14797                          mutex_exit(&mpt->m_mutex);
14797 14798                  }
14798 14799                  if (mdi_rtn == MDI_NOT_SUPPORTED) {
14799 14800                          mdi_rtn = MDI_FAILURE;
14800 14801                  }
14801 14802  virt_create_done:
14802 14803                  if (*pip && mdi_rtn != MDI_SUCCESS) {
14803 14804                          (void) mdi_pi_free(*pip, 0);
14804 14805                          *pip = NULL;
14805 14806                          *lun_dip = NULL;
14806 14807                  }
14807 14808          }
14808 14809  
14809 14810          scsi_hba_nodename_compatible_free(nodename, compatible);
14810 14811          if (lun_addr != NULL) {
14811 14812                  kmem_free(lun_addr, SCSI_MAXNAMELEN);
14812 14813          }
14813 14814          if (wwn_str != NULL) {
14814 14815                  kmem_free(wwn_str, MPTSAS_WWN_STRLEN);
14815 14816          }
14816 14817          if (component != NULL) {
14817 14818                  kmem_free(component, MAXPATHLEN);
14818 14819          }
14819 14820  
14820 14821          return ((mdi_rtn == MDI_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
14821 14822  }
14822 14823  
14823 14824  static int
14824 14825  mptsas_create_phys_lun(dev_info_t *pdip, struct scsi_inquiry *inq,
14825 14826      char *guid, dev_info_t **lun_dip, mptsas_target_t *ptgt, int lun)
14826 14827  {
14827 14828          int                     target;
14828 14829          int                     rval;
14829 14830          int                     ndi_rtn = NDI_FAILURE;
14830 14831          uint64_t                be_sas_wwn;
14831 14832          char                    *nodename = NULL;
14832 14833          char                    **compatible = NULL;
14833 14834          int                     ncompatible = 0;
14834 14835          int                     instance = 0;
14835 14836          mptsas_t                *mpt = DIP2MPT(pdip);
14836 14837          char                    *wwn_str = NULL;
14837 14838          char                    *component = NULL;
14838 14839          char                    *attached_wwn_str = NULL;
14839 14840          uint8_t                 phy = 0xFF;
14840 14841          uint64_t                sas_wwn;
14841 14842          uint32_t                devinfo;
14842 14843          uint16_t                dev_hdl;
14843 14844          uint16_t                pdev_hdl;
14844 14845          uint64_t                pdev_sas_wwn;
14845 14846          uint64_t                dev_sas_wwn;
14846 14847          uint32_t                pdev_info;
14847 14848          uint8_t                 physport;
14848 14849          uint8_t                 phy_id;
14849 14850          uint32_t                page_address;
14850 14851          uint16_t                bay_num, enclosure;
14851 14852          char                    pdev_wwn_str[MPTSAS_WWN_STRLEN];
14852 14853          uint32_t                dev_info;
14853 14854          int64_t                 lun64 = 0;
14854 14855  
14855 14856          mutex_enter(&mpt->m_mutex);
14856 14857          target = ptgt->m_devhdl;
14857 14858          sas_wwn = ptgt->m_sas_wwn;
14858 14859          devinfo = ptgt->m_deviceinfo;
14859 14860          phy = ptgt->m_phynum;
14860 14861          mutex_exit(&mpt->m_mutex);
14861 14862  
14862 14863          /*
14863 14864           * generate compatible property with binding-set "mpt"
14864 14865           */
14865 14866          scsi_hba_nodename_compatible_get(inq, NULL, inq->inq_dtype, NULL,
14866 14867              &nodename, &compatible, &ncompatible);
14867 14868  
14868 14869          /*
14869 14870           * if nodename can't be determined then print a message and skip it
14870 14871           */
14871 14872          if (nodename == NULL) {
14872 14873                  mptsas_log(mpt, CE_WARN, "mptsas found no compatible driver "
14873 14874                      "for target %d lun %d", target, lun);
14874 14875                  return (DDI_FAILURE);
14875 14876          }
14876 14877  
14877 14878          ndi_rtn = ndi_devi_alloc(pdip, nodename,
14878 14879              DEVI_SID_NODEID, lun_dip);
14879 14880  
14880 14881          /*
14881 14882           * if lun alloc success, set props
14882 14883           */
14883 14884          if (ndi_rtn == NDI_SUCCESS) {
14884 14885  
14885 14886                  if (ndi_prop_update_int(DDI_DEV_T_NONE,
14886 14887                      *lun_dip, LUN_PROP, lun) !=
14887 14888                      DDI_PROP_SUCCESS) {
14888 14889                          mptsas_log(mpt, CE_WARN, "mptsas unable to create "
14889 14890                              "property for target %d lun %d (LUN_PROP)",
14890 14891                              target, lun);
14891 14892                          ndi_rtn = NDI_FAILURE;
14892 14893                          goto phys_create_done;
14893 14894                  }
14894 14895  
14895 14896                  lun64 = (int64_t)lun;
14896 14897                  if (ndi_prop_update_int64(DDI_DEV_T_NONE,
14897 14898                      *lun_dip, LUN64_PROP, lun64) !=
14898 14899                      DDI_PROP_SUCCESS) {
14899 14900                          mptsas_log(mpt, CE_WARN, "mptsas unable to create "
14900 14901                              "property for target %d lun64 %d (LUN64_PROP)",
14901 14902                              target, lun);
14902 14903                          ndi_rtn = NDI_FAILURE;
14903 14904                          goto phys_create_done;
14904 14905                  }
14905 14906                  if (ndi_prop_update_string_array(DDI_DEV_T_NONE,
14906 14907                      *lun_dip, "compatible", compatible, ncompatible)
14907 14908                      != DDI_PROP_SUCCESS) {
14908 14909                          mptsas_log(mpt, CE_WARN, "mptsas unable to create "
14909 14910                              "property for target %d lun %d (COMPATIBLE)",
14910 14911                              target, lun);
14911 14912                          ndi_rtn = NDI_FAILURE;
14912 14913                          goto phys_create_done;
14913 14914                  }
14914 14915  
14915 14916                  /*
14916 14917                   * We need the SAS WWN for non-multipath devices, so
14917 14918                   * we'll use the same property as that multipathing
14918 14919                   * devices need to present for MPAPI. If we don't have
14919 14920                   * a WWN (e.g. parallel SCSI), don't create the prop.
14920 14921                   */
14921 14922                  wwn_str = kmem_zalloc(MPTSAS_WWN_STRLEN, KM_SLEEP);
14922 14923                  (void) sprintf(wwn_str, "w%016"PRIx64, sas_wwn);
14923 14924                  if (sas_wwn && ndi_prop_update_string(DDI_DEV_T_NONE,
14924 14925                      *lun_dip, SCSI_ADDR_PROP_TARGET_PORT, wwn_str)
14925 14926                      != DDI_PROP_SUCCESS) {
14926 14927                          mptsas_log(mpt, CE_WARN, "mptsas unable to "
14927 14928                              "create property for SAS target %d lun %d "
14928 14929                              "(target-port)", target, lun);
14929 14930                          ndi_rtn = NDI_FAILURE;
14930 14931                          goto phys_create_done;
14931 14932                  }
14932 14933  
14933 14934                  be_sas_wwn = BE_64(sas_wwn);
14934 14935                  if (sas_wwn && ndi_prop_update_byte_array(
14935 14936                      DDI_DEV_T_NONE, *lun_dip, "port-wwn",
14936 14937                      (uchar_t *)&be_sas_wwn, 8) != DDI_PROP_SUCCESS) {
14937 14938                          mptsas_log(mpt, CE_WARN, "mptsas unable to "
14938 14939                              "create property for SAS target %d lun %d "
14939 14940                              "(port-wwn)", target, lun);
14940 14941                          ndi_rtn = NDI_FAILURE;
14941 14942                          goto phys_create_done;
14942 14943                  } else if ((sas_wwn == 0) && (ndi_prop_update_int(
14943 14944                      DDI_DEV_T_NONE, *lun_dip, "sata-phy", phy) !=
14944 14945                      DDI_PROP_SUCCESS)) {
14945 14946                          /*
14946 14947                           * Direct attached SATA device without DeviceName
14947 14948                           */
14948 14949                          mptsas_log(mpt, CE_WARN, "mptsas unable to "
14949 14950                              "create property for SAS target %d lun %d "
14950 14951                              "(sata-phy)", target, lun);
14951 14952                          ndi_rtn = NDI_FAILURE;
14952 14953                          goto phys_create_done;
14953 14954                  }
14954 14955  
14955 14956                  if (ndi_prop_create_boolean(DDI_DEV_T_NONE,
14956 14957                      *lun_dip, SAS_PROP) != DDI_PROP_SUCCESS) {
14957 14958                          mptsas_log(mpt, CE_WARN, "mptsas unable to"
14958 14959                              "create property for SAS target %d lun %d"
14959 14960                              " (SAS_PROP)", target, lun);
14960 14961                          ndi_rtn = NDI_FAILURE;
14961 14962                          goto phys_create_done;
14962 14963                  }
14963 14964                  if (guid && (ndi_prop_update_string(DDI_DEV_T_NONE,
14964 14965                      *lun_dip, NDI_GUID, guid) != DDI_SUCCESS)) {
14965 14966                          mptsas_log(mpt, CE_WARN, "mptsas unable "
14966 14967                              "to create guid property for target %d "
14967 14968                              "lun %d", target, lun);
14968 14969                          ndi_rtn = NDI_FAILURE;
14969 14970                          goto phys_create_done;
14970 14971                  }
14971 14972  
14972 14973                  /*
14973 14974                   * The following code is to set properties for SM-HBA support,
14974 14975                   * it doesn't apply to RAID volumes
14975 14976                   */
14976 14977                  if (ptgt->m_phymask == 0)
14977 14978                          goto phys_raid_lun;
14978 14979  
14979 14980                  mutex_enter(&mpt->m_mutex);
14980 14981  
14981 14982                  page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
14982 14983                      MPI2_SAS_DEVICE_PGAD_FORM_MASK) |
14983 14984                      (uint32_t)ptgt->m_devhdl;
14984 14985                  rval = mptsas_get_sas_device_page0(mpt, page_address,
14985 14986                      &dev_hdl, &dev_sas_wwn, &dev_info,
14986 14987                      &physport, &phy_id, &pdev_hdl,
14987 14988                      &bay_num, &enclosure);
14988 14989                  if (rval != DDI_SUCCESS) {
14989 14990                          mutex_exit(&mpt->m_mutex);
14990 14991                          mptsas_log(mpt, CE_WARN, "mptsas unable to get"
14991 14992                              "parent device for handle %d.", page_address);
14992 14993                          ndi_rtn = NDI_FAILURE;
14993 14994                          goto phys_create_done;
14994 14995                  }
14995 14996  
14996 14997                  page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
14997 14998                      MPI2_SAS_DEVICE_PGAD_FORM_MASK) | (uint32_t)pdev_hdl;
14998 14999                  rval = mptsas_get_sas_device_page0(mpt, page_address,
14999 15000                      &dev_hdl, &pdev_sas_wwn, &pdev_info,
15000 15001                      &physport, &phy_id, &pdev_hdl, &bay_num, &enclosure);
15001 15002                  if (rval != DDI_SUCCESS) {
15002 15003                          mutex_exit(&mpt->m_mutex);
15003 15004                          mptsas_log(mpt, CE_WARN, "mptsas unable to create "
15004 15005                              "device for handle %d.", page_address);
15005 15006                          ndi_rtn = NDI_FAILURE;
15006 15007                          goto phys_create_done;
15007 15008                  }
15008 15009  
15009 15010                  mutex_exit(&mpt->m_mutex);
15010 15011  
15011 15012                  /*
15012 15013                   * If this device direct attached to the controller
15013 15014                   * set the attached-port to the base wwid
15014 15015                   */
15015 15016                  if ((ptgt->m_deviceinfo & DEVINFO_DIRECT_ATTACHED)
15016 15017                      != DEVINFO_DIRECT_ATTACHED) {
15017 15018                          (void) sprintf(pdev_wwn_str, "w%016"PRIx64,
15018 15019                              pdev_sas_wwn);
15019 15020                  } else {
15020 15021                          /*
15021 15022                           * Update the iport's attached-port to guid
15022 15023                           */
15023 15024                          if (sas_wwn == 0) {
15024 15025                                  (void) sprintf(wwn_str, "p%x", phy);
15025 15026                          } else {
15026 15027                                  (void) sprintf(wwn_str, "w%016"PRIx64, sas_wwn);
15027 15028                          }
15028 15029                          if (ddi_prop_update_string(DDI_DEV_T_NONE,
15029 15030                              pdip, SCSI_ADDR_PROP_ATTACHED_PORT, wwn_str) !=
15030 15031                              DDI_PROP_SUCCESS) {
15031 15032                                  mptsas_log(mpt, CE_WARN,
15032 15033                                      "mptsas unable to create "
15033 15034                                      "property for iport target-port"
15034 15035                                      " %s (sas_wwn)",
15035 15036                                      wwn_str);
15036 15037                                  ndi_rtn = NDI_FAILURE;
15037 15038                                  goto phys_create_done;
15038 15039                          }
15039 15040  
15040 15041                          (void) sprintf(pdev_wwn_str, "w%016"PRIx64,
15041 15042                              mpt->un.m_base_wwid);
15042 15043                  }
15043 15044  
15044 15045                  if (ndi_prop_update_string(DDI_DEV_T_NONE,
15045 15046                      *lun_dip, SCSI_ADDR_PROP_ATTACHED_PORT, pdev_wwn_str) !=
15046 15047                      DDI_PROP_SUCCESS) {
15047 15048                          mptsas_log(mpt, CE_WARN,
15048 15049                              "mptsas unable to create "
15049 15050                              "property for iport attached-port %s (sas_wwn)",
15050 15051                              attached_wwn_str);
15051 15052                          ndi_rtn = NDI_FAILURE;
15052 15053                          goto phys_create_done;
15053 15054                  }
15054 15055  
15055 15056                  if (IS_SATA_DEVICE(dev_info)) {
15056 15057                          if (ndi_prop_update_string(DDI_DEV_T_NONE,
15057 15058                              *lun_dip, MPTSAS_VARIANT, "sata") !=
15058 15059                              DDI_PROP_SUCCESS) {
15059 15060                                  mptsas_log(mpt, CE_WARN,
15060 15061                                      "mptsas unable to create "
15061 15062                                      "property for device variant ");
15062 15063                                  ndi_rtn = NDI_FAILURE;
15063 15064                                  goto phys_create_done;
15064 15065                          }
15065 15066                  }
15066 15067  
15067 15068                  if (IS_ATAPI_DEVICE(dev_info)) {
15068 15069                          if (ndi_prop_update_string(DDI_DEV_T_NONE,
15069 15070                              *lun_dip, MPTSAS_VARIANT, "atapi") !=
15070 15071                              DDI_PROP_SUCCESS) {
15071 15072                                  mptsas_log(mpt, CE_WARN,
15072 15073                                      "mptsas unable to create "
15073 15074                                      "property for device variant ");
15074 15075                                  ndi_rtn = NDI_FAILURE;
15075 15076                                  goto phys_create_done;
15076 15077                          }
15077 15078                  }
15078 15079  
15079 15080  phys_raid_lun:
15080 15081                  /*
15081 15082                   * if this is a SAS controller, and the target is a SATA
15082 15083                   * drive, set the 'pm-capable' property for sd and if on
15083 15084                   * an OPL platform, also check if this is an ATAPI
15084 15085                   * device.
15085 15086                   */
15086 15087                  instance = ddi_get_instance(mpt->m_dip);
15087 15088                  if (devinfo & (MPI2_SAS_DEVICE_INFO_SATA_DEVICE |
15088 15089                      MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE)) {
15089 15090                          NDBG2(("mptsas%d: creating pm-capable property, "
15090 15091                              "target %d", instance, target));
15091 15092  
15092 15093                          if ((ndi_prop_update_int(DDI_DEV_T_NONE,
15093 15094                              *lun_dip, "pm-capable", 1)) !=
15094 15095                              DDI_PROP_SUCCESS) {
15095 15096                                  mptsas_log(mpt, CE_WARN, "mptsas "
15096 15097                                      "failed to create pm-capable "
15097 15098                                      "property, target %d", target);
15098 15099                                  ndi_rtn = NDI_FAILURE;
15099 15100                                  goto phys_create_done;
15100 15101                          }
15101 15102  
15102 15103                  }
15103 15104  
15104 15105                  if ((inq->inq_dtype == 0) || (inq->inq_dtype == 5)) {
15105 15106                          /*
15106 15107                           * add 'obp-path' properties for devinfo
15107 15108                           */
15108 15109                          bzero(wwn_str, sizeof (wwn_str));
15109 15110                          (void) sprintf(wwn_str, "%016"PRIx64, sas_wwn);
15110 15111                          component = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
15111 15112                          if (guid) {
15112 15113                                  (void) snprintf(component, MAXPATHLEN,
15113 15114                                      "disk@w%s,%x", wwn_str, lun);
15114 15115                          } else {
15115 15116                                  (void) snprintf(component, MAXPATHLEN,
15116 15117                                      "disk@p%x,%x", phy, lun);
15117 15118                          }
15118 15119                          if (ddi_pathname_obp_set(*lun_dip, component)
15119 15120                              != DDI_SUCCESS) {
15120 15121                                  mptsas_log(mpt, CE_WARN, "mpt_sas driver "
15121 15122                                      "unable to set obp-path for SAS "
15122 15123                                      "object %s", component);
15123 15124                                  ndi_rtn = NDI_FAILURE;
15124 15125                                  goto phys_create_done;
15125 15126                          }
15126 15127                  }
15127 15128                  /*
15128 15129                   * Create the phy-num property for non-raid disk
15129 15130                   */
15130 15131                  if (ptgt->m_phymask != 0) {
15131 15132                          if (ndi_prop_update_int(DDI_DEV_T_NONE,
15132 15133                              *lun_dip, "phy-num", ptgt->m_phynum) !=
15133 15134                              DDI_PROP_SUCCESS) {
15134 15135                                  mptsas_log(mpt, CE_WARN, "mptsas driver "
15135 15136                                      "failed to create phy-num property for "
15136 15137                                      "target %d", target);
15137 15138                                  ndi_rtn = NDI_FAILURE;
15138 15139                                  goto phys_create_done;
15139 15140                          }
15140 15141                  }
15141 15142  phys_create_done:
15142 15143                  /*
15143 15144                   * If props were setup ok, online the lun
15144 15145                   */
15145 15146                  if (ndi_rtn == NDI_SUCCESS) {
15146 15147                          /*
15147 15148                           * Try to online the new node
15148 15149                           */
15149 15150                          ndi_rtn = ndi_devi_online(*lun_dip, NDI_ONLINE_ATTACH);
15150 15151                  }
15151 15152                  if (ndi_rtn == NDI_SUCCESS) {
15152 15153                          mutex_enter(&mpt->m_mutex);
15153 15154                          if (mptsas_set_led_status(mpt, ptgt, 0) !=
15154 15155                              DDI_SUCCESS) {
15155 15156                                  NDBG14(("mptsas: clear LED for tgt %x "
15156 15157                                      "failed", ptgt->m_slot_num));
15157 15158                          }
15158 15159                          mutex_exit(&mpt->m_mutex);
15159 15160                  }
15160 15161  
15161 15162                  /*
15162 15163                   * If success set rtn flag, else unwire alloc'd lun
15163 15164                   */
15164 15165                  if (ndi_rtn != NDI_SUCCESS) {
15165 15166                          NDBG12(("mptsas driver unable to online "
15166 15167                              "target %d lun %d", target, lun));
15167 15168                          ndi_prop_remove_all(*lun_dip);
15168 15169                          (void) ndi_devi_free(*lun_dip);
15169 15170                          *lun_dip = NULL;
15170 15171                  }
15171 15172          }
15172 15173  
15173 15174          scsi_hba_nodename_compatible_free(nodename, compatible);
15174 15175  
15175 15176          if (wwn_str != NULL) {
15176 15177                  kmem_free(wwn_str, MPTSAS_WWN_STRLEN);
15177 15178          }
15178 15179          if (component != NULL) {
15179 15180                  kmem_free(component, MAXPATHLEN);
15180 15181          }
15181 15182  
15182 15183  
15183 15184          return ((ndi_rtn == NDI_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
15184 15185  }
15185 15186  
15186 15187  static int
15187 15188  mptsas_probe_smp(dev_info_t *pdip, uint64_t wwn)
15188 15189  {
15189 15190          mptsas_t        *mpt = DIP2MPT(pdip);
15190 15191          struct smp_device smp_sd;
15191 15192  
15192 15193          /* XXX An HBA driver should not be allocating an smp_device. */
15193 15194          bzero(&smp_sd, sizeof (struct smp_device));
15194 15195          smp_sd.smp_sd_address.smp_a_hba_tran = mpt->m_smptran;
15195 15196          bcopy(&wwn, smp_sd.smp_sd_address.smp_a_wwn, SAS_WWN_BYTE_SIZE);
15196 15197  
15197 15198          if (smp_probe(&smp_sd) != DDI_PROBE_SUCCESS)
15198 15199                  return (NDI_FAILURE);
15199 15200          return (NDI_SUCCESS);
15200 15201  }
15201 15202  
15202 15203  static int
15203 15204  mptsas_config_smp(dev_info_t *pdip, uint64_t sas_wwn, dev_info_t **smp_dip)
15204 15205  {
15205 15206          mptsas_t        *mpt = DIP2MPT(pdip);
15206 15207          mptsas_smp_t    *psmp = NULL;
15207 15208          int             rval;
15208 15209          int             phymask;
15209 15210  
15210 15211          /*
15211 15212           * Get the physical port associated to the iport
15212 15213           * PHYMASK TODO
15213 15214           */
15214 15215          phymask = ddi_prop_get_int(DDI_DEV_T_ANY, pdip, 0,
15215 15216              "phymask", 0);
15216 15217          /*
15217 15218           * Find the smp node in hash table with specified sas address and
15218 15219           * physical port
15219 15220           */
15220 15221          psmp = mptsas_wwid_to_psmp(mpt, phymask, sas_wwn);
15221 15222          if (psmp == NULL) {
15222 15223                  return (DDI_FAILURE);
15223 15224          }
15224 15225  
15225 15226          rval = mptsas_online_smp(pdip, psmp, smp_dip);
15226 15227  
15227 15228          return (rval);
15228 15229  }
15229 15230  
15230 15231  static int
15231 15232  mptsas_online_smp(dev_info_t *pdip, mptsas_smp_t *smp_node,
15232 15233      dev_info_t **smp_dip)
15233 15234  {
15234 15235          char            wwn_str[MPTSAS_WWN_STRLEN];
15235 15236          char            attached_wwn_str[MPTSAS_WWN_STRLEN];
15236 15237          int             ndi_rtn = NDI_FAILURE;
15237 15238          int             rval = 0;
15238 15239          mptsas_smp_t    dev_info;
15239 15240          uint32_t        page_address;
15240 15241          mptsas_t        *mpt = DIP2MPT(pdip);
15241 15242          uint16_t        dev_hdl;
15242 15243          uint64_t        sas_wwn;
15243 15244          uint64_t        smp_sas_wwn;
15244 15245          uint8_t         physport;
15245 15246          uint8_t         phy_id;
15246 15247          uint16_t        pdev_hdl;
15247 15248          uint8_t         numphys = 0;
15248 15249          uint16_t        i = 0;
15249 15250          char            phymask[MPTSAS_MAX_PHYS];
15250 15251          char            *iport = NULL;
15251 15252          mptsas_phymask_t        phy_mask = 0;
15252 15253          uint16_t        attached_devhdl;
15253 15254          uint16_t        bay_num, enclosure;
15254 15255  
15255 15256          (void) sprintf(wwn_str, "%"PRIx64, smp_node->m_sasaddr);
15256 15257  
15257 15258          /*
15258 15259           * Probe smp device, prevent the node of removed device from being
15259 15260           * configured succesfully
15260 15261           */
15261 15262          if (mptsas_probe_smp(pdip, smp_node->m_sasaddr) != NDI_SUCCESS) {
15262 15263                  return (DDI_FAILURE);
15263 15264          }
15264 15265  
15265 15266          if ((*smp_dip = mptsas_find_smp_child(pdip, wwn_str)) != NULL) {
15266 15267                  return (DDI_SUCCESS);
15267 15268          }
15268 15269  
15269 15270          ndi_rtn = ndi_devi_alloc(pdip, "smp", DEVI_SID_NODEID, smp_dip);
15270 15271  
15271 15272          /*
15272 15273           * if lun alloc success, set props
15273 15274           */
15274 15275          if (ndi_rtn == NDI_SUCCESS) {
15275 15276                  /*
15276 15277                   * Set the flavor of the child to be SMP flavored
15277 15278                   */
15278 15279                  ndi_flavor_set(*smp_dip, SCSA_FLAVOR_SMP);
15279 15280  
15280 15281                  if (ndi_prop_update_string(DDI_DEV_T_NONE,
15281 15282                      *smp_dip, SMP_WWN, wwn_str) !=
15282 15283                      DDI_PROP_SUCCESS) {
15283 15284                          mptsas_log(mpt, CE_WARN, "mptsas unable to create "
15284 15285                              "property for smp device %s (sas_wwn)",
15285 15286                              wwn_str);
15286 15287                          ndi_rtn = NDI_FAILURE;
15287 15288                          goto smp_create_done;
15288 15289                  }
15289 15290                  (void) sprintf(wwn_str, "w%"PRIx64, smp_node->m_sasaddr);
15290 15291                  if (ndi_prop_update_string(DDI_DEV_T_NONE,
15291 15292                      *smp_dip, SCSI_ADDR_PROP_TARGET_PORT, wwn_str) !=
15292 15293                      DDI_PROP_SUCCESS) {
15293 15294                          mptsas_log(mpt, CE_WARN, "mptsas unable to create "
15294 15295                              "property for iport target-port %s (sas_wwn)",
15295 15296                              wwn_str);
15296 15297                          ndi_rtn = NDI_FAILURE;
15297 15298                          goto smp_create_done;
15298 15299                  }
15299 15300  
15300 15301                  mutex_enter(&mpt->m_mutex);
15301 15302  
15302 15303                  page_address = (MPI2_SAS_EXPAND_PGAD_FORM_HNDL &
15303 15304                      MPI2_SAS_EXPAND_PGAD_FORM_MASK) | smp_node->m_devhdl;
15304 15305                  rval = mptsas_get_sas_expander_page0(mpt, page_address,
15305 15306                      &dev_info);
15306 15307                  if (rval != DDI_SUCCESS) {
15307 15308                          mutex_exit(&mpt->m_mutex);
15308 15309                          mptsas_log(mpt, CE_WARN,
15309 15310                              "mptsas unable to get expander "
15310 15311                              "parent device info for %x", page_address);
15311 15312                          ndi_rtn = NDI_FAILURE;
15312 15313                          goto smp_create_done;
15313 15314                  }
15314 15315  
15315 15316                  smp_node->m_pdevhdl = dev_info.m_pdevhdl;
15316 15317                  page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
15317 15318                      MPI2_SAS_DEVICE_PGAD_FORM_MASK) |
15318 15319                      (uint32_t)dev_info.m_pdevhdl;
15319 15320                  rval = mptsas_get_sas_device_page0(mpt, page_address,
15320 15321                      &dev_hdl, &sas_wwn, &smp_node->m_pdevinfo,
15321 15322                      &physport, &phy_id, &pdev_hdl, &bay_num, &enclosure);
15322 15323                  if (rval != DDI_SUCCESS) {
15323 15324                          mutex_exit(&mpt->m_mutex);
15324 15325                          mptsas_log(mpt, CE_WARN, "mptsas unable to get "
15325 15326                              "device info for %x", page_address);
15326 15327                          ndi_rtn = NDI_FAILURE;
15327 15328                          goto smp_create_done;
15328 15329                  }
15329 15330  
15330 15331                  page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
15331 15332                      MPI2_SAS_DEVICE_PGAD_FORM_MASK) |
15332 15333                      (uint32_t)dev_info.m_devhdl;
15333 15334                  rval = mptsas_get_sas_device_page0(mpt, page_address,
15334 15335                      &dev_hdl, &smp_sas_wwn, &smp_node->m_deviceinfo,
15335 15336                      &physport, &phy_id, &pdev_hdl, &bay_num, &enclosure);
15336 15337                  if (rval != DDI_SUCCESS) {
15337 15338                          mutex_exit(&mpt->m_mutex);
15338 15339                          mptsas_log(mpt, CE_WARN, "mptsas unable to get "
15339 15340                              "device info for %x", page_address);
15340 15341                          ndi_rtn = NDI_FAILURE;
15341 15342                          goto smp_create_done;
15342 15343                  }
15343 15344                  mutex_exit(&mpt->m_mutex);
15344 15345  
15345 15346                  /*
15346 15347                   * If this smp direct attached to the controller
15347 15348                   * set the attached-port to the base wwid
15348 15349                   */
15349 15350                  if ((smp_node->m_deviceinfo & DEVINFO_DIRECT_ATTACHED)
15350 15351                      != DEVINFO_DIRECT_ATTACHED) {
15351 15352                          (void) sprintf(attached_wwn_str, "w%016"PRIx64,
15352 15353                              sas_wwn);
15353 15354                  } else {
15354 15355                          (void) sprintf(attached_wwn_str, "w%016"PRIx64,
15355 15356                              mpt->un.m_base_wwid);
15356 15357                  }
15357 15358  
15358 15359                  if (ndi_prop_update_string(DDI_DEV_T_NONE,
15359 15360                      *smp_dip, SCSI_ADDR_PROP_ATTACHED_PORT, attached_wwn_str) !=
15360 15361                      DDI_PROP_SUCCESS) {
15361 15362                          mptsas_log(mpt, CE_WARN, "mptsas unable to create "
15362 15363                              "property for smp attached-port %s (sas_wwn)",
15363 15364                              attached_wwn_str);
15364 15365                          ndi_rtn = NDI_FAILURE;
15365 15366                          goto smp_create_done;
15366 15367                  }
15367 15368  
15368 15369                  if (ndi_prop_create_boolean(DDI_DEV_T_NONE,
15369 15370                      *smp_dip, SMP_PROP) != DDI_PROP_SUCCESS) {
15370 15371                          mptsas_log(mpt, CE_WARN, "mptsas unable to "
15371 15372                              "create property for SMP %s (SMP_PROP) ",
15372 15373                              wwn_str);
15373 15374                          ndi_rtn = NDI_FAILURE;
15374 15375                          goto smp_create_done;
15375 15376                  }
15376 15377  
15377 15378                  /*
15378 15379                   * check the smp to see whether it direct
15379 15380                   * attached to the controller
15380 15381                   */
15381 15382                  if ((smp_node->m_deviceinfo & DEVINFO_DIRECT_ATTACHED)
15382 15383                      != DEVINFO_DIRECT_ATTACHED) {
15383 15384                          goto smp_create_done;
15384 15385                  }
15385 15386                  numphys = ddi_prop_get_int(DDI_DEV_T_ANY, pdip,
15386 15387                      DDI_PROP_DONTPASS, MPTSAS_NUM_PHYS, -1);
15387 15388                  if (numphys > 0) {
15388 15389                          goto smp_create_done;
15389 15390                  }
15390 15391                  /*
15391 15392                   * this iport is an old iport, we need to
15392 15393                   * reconfig the props for it.
15393 15394                   */
15394 15395                  if (ddi_prop_update_int(DDI_DEV_T_NONE, pdip,
15395 15396                      MPTSAS_VIRTUAL_PORT, 0) !=
15396 15397                      DDI_PROP_SUCCESS) {
15397 15398                          (void) ddi_prop_remove(DDI_DEV_T_NONE, pdip,
15398 15399                              MPTSAS_VIRTUAL_PORT);
15399 15400                          mptsas_log(mpt, CE_WARN, "mptsas virtual port "
15400 15401                              "prop update failed");
15401 15402                          goto smp_create_done;
15402 15403                  }
15403 15404  
15404 15405                  mutex_enter(&mpt->m_mutex);
15405 15406                  numphys = 0;
15406 15407                  iport = ddi_get_name_addr(pdip);
15407 15408                  for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
15408 15409                          bzero(phymask, sizeof (phymask));
15409 15410                          (void) sprintf(phymask,
15410 15411                              "%x", mpt->m_phy_info[i].phy_mask);
15411 15412                          if (strcmp(phymask, iport) == 0) {
15412 15413                                  phy_mask = mpt->m_phy_info[i].phy_mask;
15413 15414                                  break;
15414 15415                          }
15415 15416                  }
15416 15417  
15417 15418                  for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
15418 15419                          if ((phy_mask >> i) & 0x01) {
15419 15420                                  numphys++;
15420 15421                          }
15421 15422                  }
15422 15423                  /*
15423 15424                   * Update PHY info for smhba
15424 15425                   */
15425 15426                  if (mptsas_smhba_phy_init(mpt)) {
15426 15427                          mutex_exit(&mpt->m_mutex);
15427 15428                          mptsas_log(mpt, CE_WARN, "mptsas phy update "
15428 15429                              "failed");
15429 15430                          goto smp_create_done;
15430 15431                  }
15431 15432                  mutex_exit(&mpt->m_mutex);
15432 15433  
15433 15434                  mptsas_smhba_set_phy_props(mpt, iport, pdip,
15434 15435                      numphys, &attached_devhdl);
15435 15436  
15436 15437                  if (ddi_prop_update_int(DDI_DEV_T_NONE, pdip,
15437 15438                      MPTSAS_NUM_PHYS, numphys) !=
15438 15439                      DDI_PROP_SUCCESS) {
15439 15440                          (void) ddi_prop_remove(DDI_DEV_T_NONE, pdip,
15440 15441                              MPTSAS_NUM_PHYS);
15441 15442                          mptsas_log(mpt, CE_WARN, "mptsas update "
15442 15443                              "num phys props failed");
15443 15444                          goto smp_create_done;
15444 15445                  }
15445 15446                  /*
15446 15447                   * Add parent's props for SMHBA support
15447 15448                   */
15448 15449                  if (ddi_prop_update_string(DDI_DEV_T_NONE, pdip,
15449 15450                      SCSI_ADDR_PROP_ATTACHED_PORT, wwn_str) !=
15450 15451                      DDI_PROP_SUCCESS) {
15451 15452                          (void) ddi_prop_remove(DDI_DEV_T_NONE, pdip,
15452 15453                              SCSI_ADDR_PROP_ATTACHED_PORT);
15453 15454                          mptsas_log(mpt, CE_WARN, "mptsas update iport"
15454 15455                              "attached-port failed");
15455 15456                          goto smp_create_done;
15456 15457                  }
15457 15458  
15458 15459  smp_create_done:
15459 15460                  /*
15460 15461                   * If props were setup ok, online the lun
15461 15462                   */
15462 15463                  if (ndi_rtn == NDI_SUCCESS) {
15463 15464                          /*
15464 15465                           * Try to online the new node
15465 15466                           */
15466 15467                          ndi_rtn = ndi_devi_online(*smp_dip, NDI_ONLINE_ATTACH);
15467 15468                  }
15468 15469  
15469 15470                  /*
15470 15471                   * If success set rtn flag, else unwire alloc'd lun
15471 15472                   */
15472 15473                  if (ndi_rtn != NDI_SUCCESS) {
15473 15474                          NDBG12(("mptsas unable to online "
15474 15475                              "SMP target %s", wwn_str));
15475 15476                          ndi_prop_remove_all(*smp_dip);
15476 15477                          (void) ndi_devi_free(*smp_dip);
15477 15478                  }
15478 15479          }
15479 15480  
15480 15481          return ((ndi_rtn == NDI_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
15481 15482  }
15482 15483  
15483 15484  /* smp transport routine */
15484 15485  static int mptsas_smp_start(struct smp_pkt *smp_pkt)
15485 15486  {
15486 15487          uint64_t                        wwn;
15487 15488          Mpi2SmpPassthroughRequest_t     req;
15488 15489          Mpi2SmpPassthroughReply_t       rep;
15489 15490          uint32_t                        direction = 0;
15490 15491          mptsas_t                        *mpt;
15491 15492          int                             ret;
15492 15493          uint64_t                        tmp64;
15493 15494  
15494 15495          mpt = (mptsas_t *)smp_pkt->smp_pkt_address->
15495 15496              smp_a_hba_tran->smp_tran_hba_private;
15496 15497  
15497 15498          bcopy(smp_pkt->smp_pkt_address->smp_a_wwn, &wwn, SAS_WWN_BYTE_SIZE);
15498 15499          /*
15499 15500           * Need to compose a SMP request message
15500 15501           * and call mptsas_do_passthru() function
15501 15502           */
15502 15503          bzero(&req, sizeof (req));
15503 15504          bzero(&rep, sizeof (rep));
15504 15505          req.PassthroughFlags = 0;
15505 15506          req.PhysicalPort = 0xff;
15506 15507          req.ChainOffset = 0;
15507 15508          req.Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
15508 15509  
15509 15510          if ((smp_pkt->smp_pkt_reqsize & 0xffff0000ul) != 0) {
15510 15511                  smp_pkt->smp_pkt_reason = ERANGE;
15511 15512                  return (DDI_FAILURE);
15512 15513          }
15513 15514          req.RequestDataLength = LE_16((uint16_t)(smp_pkt->smp_pkt_reqsize - 4));
15514 15515  
15515 15516          req.MsgFlags = 0;
15516 15517          tmp64 = LE_64(wwn);
15517 15518          bcopy(&tmp64, &req.SASAddress, SAS_WWN_BYTE_SIZE);
15518 15519          if (smp_pkt->smp_pkt_rspsize > 0) {
15519 15520                  direction |= MPTSAS_PASS_THRU_DIRECTION_READ;
15520 15521          }
15521 15522          if (smp_pkt->smp_pkt_reqsize > 0) {
15522 15523                  direction |= MPTSAS_PASS_THRU_DIRECTION_WRITE;
15523 15524          }
15524 15525  
15525 15526          mutex_enter(&mpt->m_mutex);
15526 15527          ret = mptsas_do_passthru(mpt, (uint8_t *)&req, (uint8_t *)&rep,
15527 15528              (uint8_t *)smp_pkt->smp_pkt_rsp,
15528 15529              offsetof(Mpi2SmpPassthroughRequest_t, SGL), sizeof (rep),
15529 15530              smp_pkt->smp_pkt_rspsize - 4, direction,
15530 15531              (uint8_t *)smp_pkt->smp_pkt_req, smp_pkt->smp_pkt_reqsize - 4,
15531 15532              smp_pkt->smp_pkt_timeout, FKIOCTL);
15532 15533          mutex_exit(&mpt->m_mutex);
15533 15534          if (ret != 0) {
15534 15535                  cmn_err(CE_WARN, "smp_start do passthru error %d", ret);
15535 15536                  smp_pkt->smp_pkt_reason = (uchar_t)(ret);
15536 15537                  return (DDI_FAILURE);
15537 15538          }
15538 15539          /* do passthrough success, check the smp status */
15539 15540          if (LE_16(rep.IOCStatus) != MPI2_IOCSTATUS_SUCCESS) {
15540 15541                  switch (LE_16(rep.IOCStatus)) {
15541 15542                  case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
15542 15543                          smp_pkt->smp_pkt_reason = ENODEV;
15543 15544                          break;
15544 15545                  case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
15545 15546                          smp_pkt->smp_pkt_reason = EOVERFLOW;
15546 15547                          break;
15547 15548                  case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
15548 15549                          smp_pkt->smp_pkt_reason = EIO;
15549 15550                          break;
15550 15551                  default:
15551 15552                          mptsas_log(mpt, CE_NOTE, "smp_start: get unknown ioc"
15552 15553                              "status:%x", LE_16(rep.IOCStatus));
15553 15554                          smp_pkt->smp_pkt_reason = EIO;
15554 15555                          break;
15555 15556                  }
15556 15557                  return (DDI_FAILURE);
15557 15558          }
15558 15559          if (rep.SASStatus != MPI2_SASSTATUS_SUCCESS) {
15559 15560                  mptsas_log(mpt, CE_NOTE, "smp_start: get error SAS status:%x",
15560 15561                      rep.SASStatus);
15561 15562                  smp_pkt->smp_pkt_reason = EIO;
15562 15563                  return (DDI_FAILURE);
15563 15564          }
15564 15565  
15565 15566          return (DDI_SUCCESS);
15566 15567  }
15567 15568  
15568 15569  /*
15569 15570   * If we didn't get a match, we need to get sas page0 for each device, and
15570 15571   * untill we get a match. If failed, return NULL
15571 15572   */
15572 15573  static mptsas_target_t *
15573 15574  mptsas_phy_to_tgt(mptsas_t *mpt, int phymask, uint8_t phy)
15574 15575  {
15575 15576          int             i, j = 0;
15576 15577          int             rval = 0;
15577 15578          uint16_t        cur_handle;
15578 15579          uint32_t        page_address;
15579 15580          mptsas_target_t *ptgt = NULL;
15580 15581  
15581 15582          /*
15582 15583           * PHY named device must be direct attached and attaches to
15583 15584           * narrow port, if the iport is not parent of the device which
15584 15585           * we are looking for.
15585 15586           */
15586 15587          for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
15587 15588                  if ((1 << i) & phymask)
15588 15589                          j++;
15589 15590          }
15590 15591  
15591 15592          if (j > 1)
15592 15593                  return (NULL);
15593 15594  
15594 15595          /*
15595 15596           * Must be a narrow port and single device attached to the narrow port
15596 15597           * So the physical port num of device  which is equal to the iport's
15597 15598           * port num is the device what we are looking for.
15598 15599           */
15599 15600  
15600 15601          if (mpt->m_phy_info[phy].phy_mask != phymask)
15601 15602                  return (NULL);
15602 15603  
15603 15604          mutex_enter(&mpt->m_mutex);
15604 15605  
15605 15606          ptgt = (mptsas_target_t *)mptsas_hash_traverse(&mpt->m_active->m_tgttbl,
15606 15607              MPTSAS_HASH_FIRST);
15607 15608          while (ptgt != NULL) {
15608 15609                          if ((ptgt->m_sas_wwn == 0) && (ptgt->m_phynum == phy)) {
15609 15610                          mutex_exit(&mpt->m_mutex);
15610 15611                          return (ptgt);
15611 15612                  }
15612 15613  
15613 15614                  ptgt = (mptsas_target_t *)mptsas_hash_traverse(
15614 15615                      &mpt->m_active->m_tgttbl, MPTSAS_HASH_NEXT);
15615 15616          }
15616 15617  
15617 15618          if (mpt->m_done_traverse_dev) {
15618 15619                  mutex_exit(&mpt->m_mutex);
15619 15620                  return (NULL);
15620 15621          }
15621 15622  
15622 15623          /* If didn't get a match, come here */
15623 15624          cur_handle = mpt->m_dev_handle;
15624 15625          for (; ; ) {
15625 15626                  ptgt = NULL;
15626 15627                  page_address = (MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE &
15627 15628                      MPI2_SAS_DEVICE_PGAD_FORM_MASK) | (uint32_t)cur_handle;
15628 15629                  rval = mptsas_get_target_device_info(mpt, page_address,
15629 15630                      &cur_handle, &ptgt);
15630 15631                  if ((rval == DEV_INFO_FAIL_PAGE0) ||
15631 15632                      (rval == DEV_INFO_FAIL_ALLOC)) {
15632 15633                          break;
15633 15634                  }
15634 15635                  if ((rval == DEV_INFO_WRONG_DEVICE_TYPE) ||
15635 15636                      (rval == DEV_INFO_PHYS_DISK)) {
15636 15637                          continue;
15637 15638                  }
15638 15639                  mpt->m_dev_handle = cur_handle;
15639 15640  
15640 15641                  if ((ptgt->m_sas_wwn == 0) && (ptgt->m_phynum == phy)) {
15641 15642                          break;
15642 15643                  }
15643 15644          }
15644 15645  
15645 15646          mutex_exit(&mpt->m_mutex);
15646 15647          return (ptgt);
15647 15648  }
15648 15649  
15649 15650  /*
15650 15651   * The ptgt->m_sas_wwn contains the wwid for each disk.
15651 15652   * For Raid volumes, we need to check m_raidvol[x].m_raidwwid
15652 15653   * If we didn't get a match, we need to get sas page0 for each device, and
15653 15654   * untill we get a match
15654 15655   * If failed, return NULL
15655 15656   */
15656 15657  static mptsas_target_t *
15657 15658  mptsas_wwid_to_ptgt(mptsas_t *mpt, int phymask, uint64_t wwid)
15658 15659  {
15659 15660          int             rval = 0;
15660 15661          uint16_t        cur_handle;
15661 15662          uint32_t        page_address;
15662 15663          mptsas_target_t *tmp_tgt = NULL;
15663 15664  
15664 15665          mutex_enter(&mpt->m_mutex);
15665 15666          tmp_tgt = (struct mptsas_target *)mptsas_hash_search(
15666 15667              &mpt->m_active->m_tgttbl, wwid, phymask);
15667 15668          if (tmp_tgt != NULL) {
15668 15669                  mutex_exit(&mpt->m_mutex);
15669 15670                  return (tmp_tgt);
15670 15671          }
15671 15672  
15672 15673          if (phymask == 0) {
15673 15674                  /*
15674 15675                   * It's IR volume
15675 15676                   */
15676 15677                  rval = mptsas_get_raid_info(mpt);
15677 15678                  if (rval) {
15678 15679                          tmp_tgt = (struct mptsas_target *)mptsas_hash_search(
15679 15680                              &mpt->m_active->m_tgttbl, wwid, phymask);
15680 15681                  }
15681 15682                  mutex_exit(&mpt->m_mutex);
15682 15683                  return (tmp_tgt);
15683 15684          }
15684 15685  
15685 15686          if (mpt->m_done_traverse_dev) {
15686 15687                  mutex_exit(&mpt->m_mutex);
15687 15688                  return (NULL);
15688 15689          }
15689 15690  
15690 15691          /* If didn't get a match, come here */
15691 15692          cur_handle = mpt->m_dev_handle;
15692 15693          for (; ; ) {
15693 15694                  tmp_tgt = NULL;
15694 15695                  page_address = (MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE &
15695 15696                      MPI2_SAS_DEVICE_PGAD_FORM_MASK) | cur_handle;
15696 15697                  rval = mptsas_get_target_device_info(mpt, page_address,
15697 15698                      &cur_handle, &tmp_tgt);
15698 15699                  if ((rval == DEV_INFO_FAIL_PAGE0) ||
15699 15700                      (rval == DEV_INFO_FAIL_ALLOC)) {
15700 15701                          tmp_tgt = NULL;
15701 15702                          break;
15702 15703                  }
15703 15704                  if ((rval == DEV_INFO_WRONG_DEVICE_TYPE) ||
15704 15705                      (rval == DEV_INFO_PHYS_DISK)) {
15705 15706                          continue;
15706 15707                  }
15707 15708                  mpt->m_dev_handle = cur_handle;
15708 15709                  if ((tmp_tgt->m_sas_wwn) && (tmp_tgt->m_sas_wwn == wwid) &&
15709 15710                      (tmp_tgt->m_phymask == phymask)) {
15710 15711                          break;
15711 15712                  }
15712 15713          }
15713 15714  
15714 15715          mutex_exit(&mpt->m_mutex);
15715 15716          return (tmp_tgt);
15716 15717  }
15717 15718  
15718 15719  static mptsas_smp_t *
15719 15720  mptsas_wwid_to_psmp(mptsas_t *mpt, int phymask, uint64_t wwid)
15720 15721  {
15721 15722          int             rval = 0;
15722 15723          uint16_t        cur_handle;
15723 15724          uint32_t        page_address;
15724 15725          mptsas_smp_t    smp_node, *psmp = NULL;
15725 15726  
15726 15727          mutex_enter(&mpt->m_mutex);
15727 15728          psmp = (struct mptsas_smp *)mptsas_hash_search(&mpt->m_active->m_smptbl,
15728 15729              wwid, phymask);
15729 15730          if (psmp != NULL) {
15730 15731                  mutex_exit(&mpt->m_mutex);
15731 15732                  return (psmp);
15732 15733          }
15733 15734  
15734 15735          if (mpt->m_done_traverse_smp) {
15735 15736                  mutex_exit(&mpt->m_mutex);
15736 15737                  return (NULL);
15737 15738          }
15738 15739  
15739 15740          /* If didn't get a match, come here */
15740 15741          cur_handle = mpt->m_smp_devhdl;
15741 15742          for (; ; ) {
15742 15743                  psmp = NULL;
15743 15744                  page_address = (MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL &
15744 15745                      MPI2_SAS_EXPAND_PGAD_FORM_MASK) | (uint32_t)cur_handle;
15745 15746                  rval = mptsas_get_sas_expander_page0(mpt, page_address,
15746 15747                      &smp_node);
15747 15748                  if (rval != DDI_SUCCESS) {
15748 15749                          break;
15749 15750                  }
15750 15751                  mpt->m_smp_devhdl = cur_handle = smp_node.m_devhdl;
15751 15752                  psmp = mptsas_smp_alloc(&mpt->m_active->m_smptbl, &smp_node);
15752 15753                  ASSERT(psmp);
15753 15754                  if ((psmp->m_sasaddr) && (psmp->m_sasaddr == wwid) &&
15754 15755                      (psmp->m_phymask == phymask)) {
15755 15756                          break;
15756 15757                  }
15757 15758          }
15758 15759  
15759 15760          mutex_exit(&mpt->m_mutex);
15760 15761          return (psmp);
15761 15762  }
15762 15763  
15763 15764  /* helper functions using hash */
15764 15765  
15765 15766  /*
15766 15767   * Can't have duplicate entries for same devhdl,
15767 15768   * if there are invalid entries, the devhdl should be set to 0xffff
15768 15769   */
15769 15770  static void *
15770 15771  mptsas_search_by_devhdl(mptsas_hash_table_t *hashtab, uint16_t devhdl)
15771 15772  {
15772 15773          mptsas_hash_data_t *data;
15773 15774  
15774 15775          data = mptsas_hash_traverse(hashtab, MPTSAS_HASH_FIRST);
15775 15776          while (data != NULL) {
15776 15777                  if (data->devhdl == devhdl) {
15777 15778                          break;
15778 15779                  }
15779 15780                  data = mptsas_hash_traverse(hashtab, MPTSAS_HASH_NEXT);
15780 15781          }
15781 15782          return (data);
15782 15783  }
15783 15784  
15784 15785  mptsas_target_t *
15785 15786  mptsas_tgt_alloc(mptsas_hash_table_t *hashtab, uint16_t devhdl, uint64_t wwid,
15786 15787      uint32_t devinfo, mptsas_phymask_t phymask, uint8_t phynum, mptsas_t *mpt)
15787 15788  {
15788 15789          mptsas_target_t *tmp_tgt = NULL;
15789 15790  
15790 15791          tmp_tgt = mptsas_hash_search(hashtab, wwid, phymask);
15791 15792          if (tmp_tgt != NULL) {
15792 15793                  NDBG20(("Hash item already exist"));
15793 15794                  tmp_tgt->m_deviceinfo = devinfo;
15794 15795                  tmp_tgt->m_devhdl = devhdl;
15795 15796                  return (tmp_tgt);
15796 15797          }
15797 15798          tmp_tgt = kmem_zalloc(sizeof (struct mptsas_target), KM_SLEEP);
15798 15799          if (tmp_tgt == NULL) {
15799 15800                  cmn_err(CE_WARN, "Fatal, allocated tgt failed");
15800 15801                  return (NULL);
15801 15802          }
15802 15803          tmp_tgt->m_devhdl = devhdl;
15803 15804          tmp_tgt->m_sas_wwn = wwid;
15804 15805          tmp_tgt->m_deviceinfo = devinfo;
15805 15806          tmp_tgt->m_phymask = phymask;
15806 15807          tmp_tgt->m_phynum = phynum;
15807 15808          /* Initialized the tgt structure */
15808 15809          tmp_tgt->m_qfull_retries = QFULL_RETRIES;
15809 15810          tmp_tgt->m_qfull_retry_interval =
15810 15811              drv_usectohz(QFULL_RETRY_INTERVAL * 1000);
15811 15812          tmp_tgt->m_t_throttle = MAX_THROTTLE;
15812 15813          mutex_init(&tmp_tgt->m_tgt_intr_mutex, NULL, MUTEX_DRIVER,
15813 15814              DDI_INTR_PRI(mpt->m_intr_pri));
15814 15815  
15815 15816          mptsas_hash_add(hashtab, tmp_tgt);
15816 15817  
15817 15818          return (tmp_tgt);
15818 15819  }
15819 15820  
15820 15821  static void
15821 15822  mptsas_tgt_free(mptsas_hash_table_t *hashtab, uint64_t wwid,
15822 15823      mptsas_phymask_t phymask)
15823 15824  {
15824 15825          mptsas_target_t *tmp_tgt;
15825 15826          tmp_tgt = mptsas_hash_rem(hashtab, wwid, phymask);
15826 15827          if (tmp_tgt == NULL) {
15827 15828                  cmn_err(CE_WARN, "Tgt not found, nothing to free");
15828 15829          } else {
15829 15830                  mutex_destroy(&tmp_tgt->m_tgt_intr_mutex);
15830 15831                  kmem_free(tmp_tgt, sizeof (struct mptsas_target));
15831 15832          }
15832 15833  }
15833 15834  
15834 15835  /*
15835 15836   * Return the entry in the hash table
15836 15837   */
15837 15838  static mptsas_smp_t *
15838 15839  mptsas_smp_alloc(mptsas_hash_table_t *hashtab, mptsas_smp_t *data)
15839 15840  {
15840 15841          uint64_t key1 = data->m_sasaddr;
15841 15842          mptsas_phymask_t key2 = data->m_phymask;
15842 15843          mptsas_smp_t *ret_data;
15843 15844  
15844 15845          ret_data = mptsas_hash_search(hashtab, key1, key2);
15845 15846          if (ret_data != NULL) {
15846 15847                  bcopy(data, ret_data, sizeof (mptsas_smp_t));
15847 15848                  return (ret_data);
15848 15849          }
15849 15850  
15850 15851          ret_data = kmem_alloc(sizeof (mptsas_smp_t), KM_SLEEP);
15851 15852          bcopy(data, ret_data, sizeof (mptsas_smp_t));
15852 15853          mptsas_hash_add(hashtab, ret_data);
15853 15854          return (ret_data);
15854 15855  }
15855 15856  
15856 15857  static void
15857 15858  mptsas_smp_free(mptsas_hash_table_t *hashtab, uint64_t wwid,
15858 15859      mptsas_phymask_t phymask)
15859 15860  {
15860 15861          mptsas_smp_t *tmp_smp;
15861 15862          tmp_smp = mptsas_hash_rem(hashtab, wwid, phymask);
15862 15863          if (tmp_smp == NULL) {
15863 15864                  cmn_err(CE_WARN, "Smp element not found, nothing to free");
15864 15865          } else {
15865 15866                  kmem_free(tmp_smp, sizeof (struct mptsas_smp));
15866 15867          }
15867 15868  }
15868 15869  
15869 15870  /*
15870 15871   * Hash operation functions
15871 15872   * key1 is the sas_wwn, key2 is the phymask
15872 15873   */
15873 15874  static void
15874 15875  mptsas_hash_init(mptsas_hash_table_t *hashtab)
15875 15876  {
15876 15877          if (hashtab == NULL) {
15877 15878                  return;
15878 15879          }
15879 15880          bzero(hashtab->head, sizeof (mptsas_hash_node_t) *
15880 15881              MPTSAS_HASH_ARRAY_SIZE);
15881 15882          hashtab->cur = NULL;
15882 15883          hashtab->line = 0;
15883 15884  }
15884 15885  
15885 15886  static void
15886 15887  mptsas_hash_uninit(mptsas_hash_table_t *hashtab, size_t datalen)
15887 15888  {
15888 15889          uint16_t line = 0;
15889 15890          mptsas_hash_node_t *cur = NULL, *last = NULL;
15890 15891  
15891 15892          if (hashtab == NULL) {
15892 15893                  return;
15893 15894          }
15894 15895          for (line = 0; line < MPTSAS_HASH_ARRAY_SIZE; line++) {
15895 15896                  cur = hashtab->head[line];
15896 15897                  while (cur != NULL) {
15897 15898                          last = cur;
15898 15899                          cur = cur->next;
15899 15900                          kmem_free(last->data, datalen);
15900 15901                          kmem_free(last, sizeof (mptsas_hash_node_t));
15901 15902                  }
15902 15903          }
15903 15904  }
15904 15905  
15905 15906  /*
15906 15907   * You must guarantee the element doesn't exist in the hash table
15907 15908   * before you call mptsas_hash_add()
15908 15909   */
15909 15910  static void
15910 15911  mptsas_hash_add(mptsas_hash_table_t *hashtab, void *data)
15911 15912  {
15912 15913          uint64_t key1 = ((mptsas_hash_data_t *)data)->key1;
15913 15914          mptsas_phymask_t key2 = ((mptsas_hash_data_t *)data)->key2;
15914 15915          mptsas_hash_node_t **head = NULL;
15915 15916          mptsas_hash_node_t *node = NULL;
15916 15917  
15917 15918          if (hashtab == NULL) {
15918 15919                  return;
15919 15920          }
15920 15921          ASSERT(mptsas_hash_search(hashtab, key1, key2) == NULL);
15921 15922          node = kmem_zalloc(sizeof (mptsas_hash_node_t), KM_NOSLEEP);
15922 15923          node->data = data;
15923 15924  
15924 15925          head = &(hashtab->head[key1 % MPTSAS_HASH_ARRAY_SIZE]);
15925 15926          if (*head == NULL) {
15926 15927                  *head = node;
15927 15928          } else {
15928 15929                  node->next = *head;
15929 15930                  *head = node;
15930 15931          }
15931 15932  }
15932 15933  
15933 15934  static void *
15934 15935  mptsas_hash_rem(mptsas_hash_table_t *hashtab, uint64_t key1,
15935 15936      mptsas_phymask_t key2)
15936 15937  {
15937 15938          mptsas_hash_node_t **head = NULL;
15938 15939          mptsas_hash_node_t *last = NULL, *cur = NULL;
15939 15940          mptsas_hash_data_t *data;
15940 15941          if (hashtab == NULL) {
15941 15942                  return (NULL);
15942 15943          }
15943 15944          head = &(hashtab->head[key1 % MPTSAS_HASH_ARRAY_SIZE]);
15944 15945          cur = *head;
15945 15946          while (cur != NULL) {
15946 15947                  data = cur->data;
15947 15948                  if ((data->key1 == key1) && (data->key2 == key2)) {
15948 15949                          if (last == NULL) {
15949 15950                                  (*head) = cur->next;
15950 15951                          } else {
15951 15952                                  last->next = cur->next;
15952 15953                          }
15953 15954                          kmem_free(cur, sizeof (mptsas_hash_node_t));
15954 15955                          return (data);
15955 15956                  } else {
15956 15957                          last = cur;
15957 15958                          cur = cur->next;
15958 15959                  }
15959 15960          }
15960 15961          return (NULL);
15961 15962  }
15962 15963  
15963 15964  static void *
15964 15965  mptsas_hash_search(mptsas_hash_table_t *hashtab, uint64_t key1,
15965 15966      mptsas_phymask_t key2)
15966 15967  {
15967 15968          mptsas_hash_node_t *cur = NULL;
15968 15969          mptsas_hash_data_t *data;
15969 15970          if (hashtab == NULL) {
15970 15971                  return (NULL);
15971 15972          }
15972 15973          cur = hashtab->head[key1 % MPTSAS_HASH_ARRAY_SIZE];
15973 15974          while (cur != NULL) {
15974 15975                  data = cur->data;
15975 15976                  if ((data->key1 == key1) && (data->key2 == key2)) {
15976 15977                          return (data);
15977 15978                  } else {
15978 15979                          cur = cur->next;
15979 15980                  }
15980 15981          }
15981 15982          return (NULL);
15982 15983  }
15983 15984  
15984 15985  static void *
15985 15986  mptsas_hash_traverse(mptsas_hash_table_t *hashtab, int pos)
15986 15987  {
15987 15988          mptsas_hash_node_t *this = NULL;
15988 15989  
15989 15990          if (hashtab == NULL) {
15990 15991                  return (NULL);
15991 15992          }
15992 15993  
15993 15994          if (pos == MPTSAS_HASH_FIRST) {
15994 15995                  hashtab->line = 0;
15995 15996                  hashtab->cur = NULL;
15996 15997                  this = hashtab->head[0];
15997 15998          } else {
15998 15999                  if (hashtab->cur == NULL) {
15999 16000                          return (NULL);
16000 16001                  } else {
16001 16002                          this = hashtab->cur->next;
16002 16003                  }
16003 16004          }
16004 16005  
16005 16006          while (this == NULL) {
16006 16007                  hashtab->line++;
16007 16008                  if (hashtab->line >= MPTSAS_HASH_ARRAY_SIZE) {
16008 16009                          /* the traverse reaches the end */
16009 16010                          hashtab->cur = NULL;
16010 16011                          return (NULL);
16011 16012                  } else {
16012 16013                          this = hashtab->head[hashtab->line];
16013 16014                  }
16014 16015          }
16015 16016          hashtab->cur = this;
16016 16017          return (this->data);
16017 16018  }
16018 16019  
16019 16020  /*
16020 16021   * Functions for SGPIO LED support
16021 16022   */
16022 16023  static dev_info_t *
16023 16024  mptsas_get_dip_from_dev(dev_t dev, mptsas_phymask_t *phymask)
16024 16025  {
16025 16026          dev_info_t      *dip;
16026 16027          int             prop;
16027 16028          dip = e_ddi_hold_devi_by_dev(dev, 0);
16028 16029          if (dip == NULL)
16029 16030                  return (dip);
16030 16031          prop = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
16031 16032              "phymask", 0);
16032 16033          *phymask = (mptsas_phymask_t)prop;
16033 16034          ddi_release_devi(dip);
16034 16035          return (dip);
16035 16036  }
16036 16037  static mptsas_target_t *
16037 16038  mptsas_addr_to_ptgt(mptsas_t *mpt, char *addr, mptsas_phymask_t phymask)
16038 16039  {
16039 16040          uint8_t                 phynum;
16040 16041          uint64_t                wwn;
16041 16042          int                     lun;
16042 16043          mptsas_target_t         *ptgt = NULL;
16043 16044  
16044 16045          if (mptsas_parse_address(addr, &wwn, &phynum, &lun) != DDI_SUCCESS) {
16045 16046                  return (NULL);
16046 16047          }
16047 16048          if (addr[0] == 'w') {
16048 16049                  ptgt = mptsas_wwid_to_ptgt(mpt, (int)phymask, wwn);
16049 16050          } else {
16050 16051                  ptgt = mptsas_phy_to_tgt(mpt, (int)phymask, phynum);
16051 16052          }
16052 16053          return (ptgt);
16053 16054  }
16054 16055  
16055 16056  #ifdef MPTSAS_GET_LED
16056 16057  static int
16057 16058  mptsas_get_led_status(mptsas_t *mpt, mptsas_target_t *ptgt,
16058 16059      uint32_t *slotstatus)
16059 16060  {
16060 16061          return (mptsas_send_sep(mpt, ptgt, slotstatus,
16061 16062              MPI2_SEP_REQ_ACTION_READ_STATUS));
16062 16063  }
16063 16064  #endif
16064 16065  static int
16065 16066  mptsas_set_led_status(mptsas_t *mpt, mptsas_target_t *ptgt, uint32_t slotstatus)
16066 16067  {
16067 16068          NDBG14(("mptsas_ioctl: set LED status %x for slot %x",
16068 16069              slotstatus, ptgt->m_slot_num));
16069 16070          return (mptsas_send_sep(mpt, ptgt, &slotstatus,
16070 16071              MPI2_SEP_REQ_ACTION_WRITE_STATUS));
16071 16072  }
16072 16073  /*
16073 16074   *  send sep request, use enclosure/slot addressing
16074 16075   */
16075 16076  static int mptsas_send_sep(mptsas_t *mpt, mptsas_target_t *ptgt,
16076 16077      uint32_t *status, uint8_t act)
16077 16078  {
16078 16079          Mpi2SepRequest_t        req;
16079 16080          Mpi2SepReply_t          rep;
16080 16081          int                     ret;
16081 16082  
16082 16083          ASSERT(mutex_owned(&mpt->m_mutex));
16083 16084  
16084 16085          bzero(&req, sizeof (req));
16085 16086          bzero(&rep, sizeof (rep));
16086 16087  
16087 16088          /* Do nothing for RAID volumes */
16088 16089          if (ptgt->m_phymask == 0) {
16089 16090                  NDBG14(("mptsas_send_sep: Skip RAID volumes"));
16090 16091                  return (DDI_FAILURE);
16091 16092          }
16092 16093  
16093 16094          req.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
16094 16095          req.Action = act;
16095 16096          req.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
16096 16097          req.EnclosureHandle = LE_16(ptgt->m_enclosure);
16097 16098          req.Slot = LE_16(ptgt->m_slot_num);
16098 16099          if (act == MPI2_SEP_REQ_ACTION_WRITE_STATUS) {
16099 16100                  req.SlotStatus = LE_32(*status);
16100 16101          }
16101 16102          ret = mptsas_do_passthru(mpt, (uint8_t *)&req, (uint8_t *)&rep, NULL,
16102 16103              sizeof (req), sizeof (rep), NULL, 0, NULL, 0, 60, FKIOCTL);
16103 16104          if (ret != 0) {
16104 16105                  mptsas_log(mpt, CE_NOTE, "mptsas_send_sep: passthru SEP "
16105 16106                      "Processor Request message error %d", ret);
16106 16107                  return (DDI_FAILURE);
16107 16108          }
16108 16109          /* do passthrough success, check the ioc status */
16109 16110          if (LE_16(rep.IOCStatus) != MPI2_IOCSTATUS_SUCCESS) {
16110 16111                  if ((LE_16(rep.IOCStatus) & MPI2_IOCSTATUS_MASK) ==
16111 16112                      MPI2_IOCSTATUS_INVALID_FIELD) {
16112 16113                          mptsas_log(mpt, CE_NOTE, "send sep act %x: Not "
16113 16114                              "supported action, loginfo %x", act,
16114 16115                              LE_32(rep.IOCLogInfo));
16115 16116                          return (DDI_FAILURE);
16116 16117                  }
16117 16118                  mptsas_log(mpt, CE_NOTE, "send_sep act %x: ioc "
16118 16119                      "status:%x", act, LE_16(rep.IOCStatus));
16119 16120                  return (DDI_FAILURE);
16120 16121          }
16121 16122          if (act != MPI2_SEP_REQ_ACTION_WRITE_STATUS) {
16122 16123                  *status = LE_32(rep.SlotStatus);
16123 16124          }
16124 16125  
16125 16126          return (DDI_SUCCESS);
16126 16127  }
16127 16128  
16128 16129  int
16129 16130  mptsas_dma_addr_create(mptsas_t *mpt, ddi_dma_attr_t dma_attr,
16130 16131      ddi_dma_handle_t *dma_hdp, ddi_acc_handle_t *acc_hdp, caddr_t *dma_memp,
16131 16132      uint32_t alloc_size, ddi_dma_cookie_t *cookiep)
16132 16133  {
16133 16134          ddi_dma_cookie_t        new_cookie;
16134 16135          size_t                  alloc_len;
16135 16136          uint_t                  ncookie;
16136 16137  
16137 16138          if (cookiep == NULL)
16138 16139                  cookiep = &new_cookie;
16139 16140  
16140 16141          if (ddi_dma_alloc_handle(mpt->m_dip, &dma_attr, DDI_DMA_SLEEP,
16141 16142              NULL, dma_hdp) != DDI_SUCCESS) {
16142 16143                  dma_hdp = NULL;
16143 16144                  return (FALSE);
16144 16145          }
16145 16146  
16146 16147          if (ddi_dma_mem_alloc(*dma_hdp, alloc_size, &mpt->m_dev_acc_attr,
16147 16148              DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL, dma_memp, &alloc_len,
16148 16149              acc_hdp) != DDI_SUCCESS) {
16149 16150                  ddi_dma_free_handle(dma_hdp);
16150 16151                  dma_hdp = NULL;
16151 16152                  return (FALSE);
16152 16153          }
16153 16154  
16154 16155          if (ddi_dma_addr_bind_handle(*dma_hdp, NULL, *dma_memp, alloc_len,
16155 16156              (DDI_DMA_RDWR | DDI_DMA_CONSISTENT), DDI_DMA_SLEEP, NULL,
16156 16157              cookiep, &ncookie) != DDI_DMA_MAPPED) {
16157 16158                  (void) ddi_dma_mem_free(acc_hdp);
16158 16159                  ddi_dma_free_handle(dma_hdp);
16159 16160                  dma_hdp = NULL;
16160 16161                  return (FALSE);
16161 16162          }
16162 16163  
16163 16164          return (TRUE);
16164 16165  }
16165 16166  
16166 16167  void
16167 16168  mptsas_dma_addr_destroy(ddi_dma_handle_t *dma_hdp, ddi_acc_handle_t *acc_hdp)
16168 16169  {
16169 16170          if (*dma_hdp == NULL)
16170 16171                  return;
16171 16172  
16172 16173          (void) ddi_dma_unbind_handle(*dma_hdp);
16173 16174          (void) ddi_dma_mem_free(acc_hdp);
16174 16175          ddi_dma_free_handle(dma_hdp);
16175 16176          dma_hdp = NULL;
16176 16177  }
16177 16178  
16178 16179  static int
16179 16180  mptsas_outstanding_cmds_n(mptsas_t *mpt)
16180 16181  {
16181 16182          int n = 0, i;
16182 16183          for (i = 0; i < mpt->m_slot_freeq_pair_n; i++) {
16183 16184                  mutex_enter(&mpt->m_slot_freeq_pairp[i].
16184 16185                      m_slot_allocq.s.m_fq_mutex);
16185 16186                  mutex_enter(&mpt->m_slot_freeq_pairp[i].
16186 16187                      m_slot_releq.s.m_fq_mutex);
16187 16188                  n += (mpt->m_slot_freeq_pairp[i].m_slot_allocq.s.m_fq_n_init -
16188 16189                      mpt->m_slot_freeq_pairp[i].m_slot_allocq.s.m_fq_n -
16189 16190                      mpt->m_slot_freeq_pairp[i].m_slot_releq.s.m_fq_n);
16190 16191                  mutex_exit(&mpt->m_slot_freeq_pairp[i].
16191 16192                      m_slot_releq.s.m_fq_mutex);
16192 16193                  mutex_exit(&mpt->m_slot_freeq_pairp[i].
16193 16194                      m_slot_allocq.s.m_fq_mutex);
16194 16195          }
16195 16196          if (mpt->m_max_requests - 2 < n)
16196 16197                  panic("mptsas: free slot allocq and releq crazy");
16197 16198          return (n);
16198 16199  }
  
    | 
      ↓ open down ↓ | 
    3847 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX