1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
  24  */
  25 /*
  26  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
  27  */
  28 
  29 /*
  30  * SATA Framework
  31  * Generic SATA Host Adapter Implementation
  32  */
  33 
  34 #include <sys/conf.h>
  35 #include <sys/file.h>
  36 #include <sys/ddi.h>
  37 #include <sys/sunddi.h>
  38 #include <sys/modctl.h>
  39 #include <sys/cmn_err.h>
  40 #include <sys/errno.h>
  41 #include <sys/thread.h>
  42 #include <sys/kstat.h>
  43 #include <sys/note.h>
  44 #include <sys/sysevent.h>
  45 #include <sys/sysevent/eventdefs.h>
  46 #include <sys/sysevent/dr.h>
  47 #include <sys/taskq.h>
  48 #include <sys/disp.h>
  49 #include <sys/sdt.h>
  50 
  51 #include <sys/sata/impl/sata.h>
  52 #include <sys/sata/sata_hba.h>
  53 #include <sys/sata/sata_defs.h>
  54 #include <sys/sata/sata_cfgadm.h>
  55 #include <sys/sata/sata_blacklist.h>
  56 #include <sys/sata/sata_satl.h>
  57 
  58 #include <sys/scsi/impl/spc3_types.h>
  59 
  60 /*
  61  * FMA header files
  62  */
  63 #include <sys/ddifm.h>
  64 #include <sys/fm/protocol.h>
  65 #include <sys/fm/util.h>
  66 #include <sys/fm/io/ddi.h>
  67 
  68 /* Debug flags - defined in sata.h */
  69 int     sata_debug_flags = 0;
  70 int     sata_msg = 0;
  71 
  72 /*
  73  * Flags enabling selected SATA HBA framework functionality
  74  */
  75 #define SATA_ENABLE_QUEUING             1
  76 #define SATA_ENABLE_NCQ                 2
  77 #define SATA_ENABLE_PROCESS_EVENTS      4
  78 #define SATA_ENABLE_PMULT_FBS           8 /* FIS-Based Switching */
  79 int sata_func_enable =
  80         SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ;
  81 
  82 /*
  83  * Global variable setting default maximum queue depth (NCQ or TCQ)
  84  * Note:minimum queue depth is 1
  85  */
  86 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */
  87 
  88 /*
  89  * Currently used default NCQ/TCQ queue depth. It is set-up during the driver
  90  * initialization, using value from sata_max_queue_depth
  91  * It is adjusted to minimum supported by the controller and by the device,
  92  * if queueing is enabled.
  93  */
  94 static  int sata_current_max_qdepth;
  95 
  96 /*
  97  * Global variable determining the default behavior after device hotpluggin.
  98  * If non-zero, the hotplugged device is onlined (if possible) without explicit
  99  * IOCTL request (AP_CONFIGURE).
 100  * If zero, hotplugged device is identified, but not onlined.
 101  * Enabling (AP_CONNECT) device port with an attached device does not result
 102  * in device onlining regardless of the flag setting
 103  */
 104 int sata_auto_online = 0;
 105 
 106 #ifdef SATA_DEBUG
 107 
 108 #define SATA_LOG_D(args)        sata_log args
 109 uint64_t mbuf_count = 0;
 110 uint64_t mbuffail_count = 0;
 111 
 112 sata_atapi_cmd_t sata_atapi_trace[64];
 113 uint32_t sata_atapi_trace_index = 0;
 114 int sata_atapi_trace_save = 1;
 115 static  void sata_save_atapi_trace(sata_pkt_txlate_t *, int);
 116 #define SATAATAPITRACE(spx, count)      if (sata_atapi_trace_save) \
 117     sata_save_atapi_trace(spx, count);
 118 
 119 #else
 120 #define SATA_LOG_D(args)        sata_trace_log args
 121 #define SATAATAPITRACE(spx, count)
 122 #endif
 123 
 124 #if 0
 125 static void
 126 sata_test_atapi_packet_command(sata_hba_inst_t *, int);
 127 #endif
 128 
 129 #ifdef SATA_INJECT_FAULTS
 130 
 131 #define         SATA_INJECT_PKT_FAULT   1
 132 uint32_t        sata_inject_fault = 0;
 133 
 134 uint32_t        sata_inject_fault_count = 0;
 135 uint32_t        sata_inject_fault_pause_count = 0;
 136 uint32_t        sata_fault_type = 0;
 137 uint32_t        sata_fault_cmd = 0;
 138 dev_info_t      *sata_fault_ctrl = NULL;
 139 sata_device_t   sata_fault_device;
 140 
 141 static  void sata_inject_pkt_fault(sata_pkt_t *, int *, int);
 142 
 143 #endif
 144 
 145 #define LEGACY_HWID_LEN 64      /* Model (40) + Serial (20) + pad */
 146 
 147 static char sata_rev_tag[] = {"1.46"};
 148 
 149 /*
 150  * SATA cb_ops functions
 151  */
 152 static  int sata_hba_open(dev_t *, int, int, cred_t *);
 153 static  int sata_hba_close(dev_t, int, int, cred_t *);
 154 static  int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
 155 
 156 /*
 157  * SCSA required entry points
 158  */
 159 static  int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
 160     scsi_hba_tran_t *, struct scsi_device *);
 161 static  int sata_scsi_tgt_probe(struct scsi_device *,
 162     int (*callback)(void));
 163 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
 164     scsi_hba_tran_t *, struct scsi_device *);
 165 static  int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
 166 static  int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
 167 static  int sata_scsi_reset(struct scsi_address *, int);
 168 static  int sata_scsi_getcap(struct scsi_address *, char *, int);
 169 static  int sata_scsi_setcap(struct scsi_address *, char *, int, int);
 170 static  struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
 171     struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
 172     caddr_t);
 173 static  void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
 174 static  void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
 175 static  void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
 176 
 177 /*
 178  * SATA HBA interface functions are defined in sata_hba.h header file
 179  */
 180 
 181 /* Event processing functions */
 182 static  void sata_event_daemon(void *);
 183 static  void sata_event_thread_control(int);
 184 static  void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
 185 static  void sata_process_pmult_events(sata_hba_inst_t *, uint8_t);
 186 static  void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
 187 static  void sata_process_pmdevice_reset(sata_hba_inst_t *, sata_address_t *);
 188 static  void sata_process_port_failed_event(sata_hba_inst_t *,
 189     sata_address_t *);
 190 static  void sata_process_port_link_events(sata_hba_inst_t *,
 191     sata_address_t *);
 192 static  void sata_process_pmport_link_events(sata_hba_inst_t *,
 193     sata_address_t *);
 194 static  void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
 195 static  void sata_process_pmdevice_detached(sata_hba_inst_t *,
 196     sata_address_t *);
 197 static  void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
 198 static  void sata_process_pmdevice_attached(sata_hba_inst_t *,
 199     sata_address_t *);
 200 static  void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
 201 static  void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
 202 static  void sata_process_target_node_cleanup(sata_hba_inst_t *,
 203     sata_address_t *);
 204 static  void sata_process_device_autoonline(sata_hba_inst_t *,
 205     sata_address_t *saddr);
 206 
 207 /*
 208  * Local translation functions
 209  */
 210 static  int sata_txlt_inquiry(sata_pkt_txlate_t *);
 211 static  int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
 212 static  int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
 213 static  int sata_txlt_read_capacity(sata_pkt_txlate_t *);
 214 static  int sata_txlt_read_capacity16(sata_pkt_txlate_t *);
 215 static  int sata_txlt_unmap(sata_pkt_txlate_t *);
 216 static  int sata_txlt_request_sense(sata_pkt_txlate_t *);
 217 static  int sata_txlt_read(sata_pkt_txlate_t *);
 218 static  int sata_txlt_write(sata_pkt_txlate_t *);
 219 static  int sata_txlt_log_sense(sata_pkt_txlate_t *);
 220 static  int sata_txlt_log_select(sata_pkt_txlate_t *);
 221 static  int sata_txlt_mode_sense(sata_pkt_txlate_t *);
 222 static  int sata_txlt_mode_select(sata_pkt_txlate_t *);
 223 static  int sata_txlt_ata_pass_thru(sata_pkt_txlate_t *);
 224 static  int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
 225 static  int sata_txlt_write_buffer(sata_pkt_txlate_t *);
 226 static  int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
 227 
 228 static  int sata_hba_start(sata_pkt_txlate_t *, int *);
 229 static  int sata_txlt_invalid_command(sata_pkt_txlate_t *);
 230 static  int sata_txlt_check_condition(sata_pkt_txlate_t *, uchar_t, uchar_t);
 231 static  int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
 232 static  int sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *);
 233 static  int sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *);
 234 static  void sata_txlt_rw_completion(sata_pkt_t *);
 235 static  void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
 236 static  void sata_txlt_apt_completion(sata_pkt_t *sata_pkt);
 237 static  void sata_txlt_unmap_completion(sata_pkt_t *sata_pkt);
 238 static  void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
 239 static  int sata_emul_rw_completion(sata_pkt_txlate_t *);
 240 static  void sata_fill_ata_return_desc(sata_pkt_t *, uint8_t, uint8_t,
 241     uint8_t);
 242 static  struct scsi_extended_sense *sata_immediate_error_response(
 243     sata_pkt_txlate_t *, int);
 244 static  struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
 245 
 246 static  int sata_txlt_atapi(sata_pkt_txlate_t *);
 247 static  void sata_txlt_atapi_completion(sata_pkt_t *);
 248 
 249 /*
 250  * Local functions for ioctl
 251  */
 252 static  int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
 253 static  void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
 254     devctl_ap_state_t *);
 255 static  dev_info_t *sata_get_target_dip(dev_info_t *, uint8_t, uint8_t);
 256 static  dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
 257 static  dev_info_t *sata_devt_to_devinfo(dev_t);
 258 static  int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
 259 static  int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
 260 static  int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
 261 static  int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
 262 static  int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
 263 static  int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
 264 static  int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
 265 static  int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
 266 static  int sata_ioctl_reset_all(sata_hba_inst_t *);
 267 static  int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
 268 static  int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
 269     sata_ioctl_data_t *, int mode);
 270 static  int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
 271     sata_ioctl_data_t *, int mode);
 272 static  int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
 273     sata_ioctl_data_t *, int mode);
 274 static  int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
 275     sata_ioctl_data_t *, int mode);
 276 static  int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
 277     sata_device_t *, sata_ioctl_data_t *, int mode);
 278 
 279 /*
 280  * Local functions
 281  */
 282 static  void sata_remove_hba_instance(dev_info_t *);
 283 static  int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
 284 static  void sata_probe_ports(sata_hba_inst_t *);
 285 static  void sata_probe_pmports(sata_hba_inst_t *, uint8_t);
 286 static  int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
 287 static  int sata_reprobe_pmult(sata_hba_inst_t *, sata_device_t *, int);
 288 static  int sata_reprobe_pmport(sata_hba_inst_t *, sata_device_t *, int);
 289 static  int sata_alloc_pmult(sata_hba_inst_t *, sata_device_t *);
 290 static  void sata_free_pmult(sata_hba_inst_t *, sata_device_t *);
 291 static  int sata_add_device(dev_info_t *, sata_hba_inst_t *, sata_device_t *);
 292 static  int sata_offline_device(sata_hba_inst_t *, sata_device_t *,
 293     sata_drive_info_t *);
 294 static  dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
 295     sata_address_t *);
 296 static  void sata_remove_target_node(sata_hba_inst_t *,
 297     sata_address_t *);
 298 static  int sata_validate_scsi_address(sata_hba_inst_t *,
 299     struct scsi_address *, sata_device_t *);
 300 static  int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
 301 static  sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
 302 static  void sata_pkt_free(sata_pkt_txlate_t *);
 303 static  int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
 304     caddr_t, ddi_dma_attr_t *);
 305 static  void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *);
 306 static  int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
 307 static  sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
 308     sata_device_t *);
 309 static  int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
 310 static  void sata_reidentify_device(sata_pkt_txlate_t *);
 311 static  struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
 312 static  void sata_free_local_buffer(sata_pkt_txlate_t *);
 313 static  uint64_t sata_check_capacity(sata_drive_info_t *);
 314 void    sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
 315     ddi_dma_attr_t *);
 316 static  int sata_fetch_device_identify_data(sata_hba_inst_t *,
 317     sata_drive_info_t *);
 318 static  void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
 319 static  void sata_update_pmport_info(sata_hba_inst_t *, sata_device_t *);
 320 static  void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
 321 static  int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
 322 static  int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
 323 static  int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
 324 static  int sata_set_drive_features(sata_hba_inst_t *,
 325     sata_drive_info_t *, int flag);
 326 static  void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
 327 static  int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
 328 static  void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
 329     uint8_t *);
 330 static  int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
 331     struct scsi_inquiry *);
 332 static  int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
 333 static  int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
 334 static  int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
 335 static  int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
 336 static  int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
 337 static  int sata_mode_select_page_8(sata_pkt_txlate_t *,
 338     struct mode_cache_scsi3 *, int, int *, int *, int *);
 339 static  int sata_mode_select_page_1a(sata_pkt_txlate_t *,
 340     struct mode_info_power_cond *, int, int *, int *, int *);
 341 static  int sata_mode_select_page_1c(sata_pkt_txlate_t *,
 342     struct mode_info_excpt_page *, int, int *, int *, int *);
 343 static  int sata_mode_select_page_30(sata_pkt_txlate_t *,
 344     struct mode_acoustic_management *, int, int *, int *, int *);
 345 
 346 static  int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
 347 static  int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
 348     sata_hba_inst_t *);
 349 static  int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
 350     sata_hba_inst_t *);
 351 static  int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
 352     sata_hba_inst_t *);
 353 static  int sata_build_lsense_page_0e(sata_drive_info_t *, uint8_t *,
 354     sata_pkt_txlate_t *);
 355 
 356 static  void sata_set_arq_data(sata_pkt_t *);
 357 static  void sata_build_read_verify_cmd(sata_cmd_t *, uint16_t, uint64_t);
 358 static  void sata_build_generic_cmd(sata_cmd_t *, uint8_t);
 359 static  uint8_t sata_get_standby_timer(uint8_t *timer);
 360 
 361 static  void sata_save_drive_settings(sata_drive_info_t *);
 362 static  void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
 363 static  void sata_show_pmult_info(sata_hba_inst_t *, sata_device_t *);
 364 static  void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
 365 static  void sata_trace_log(sata_hba_inst_t *, uint_t, const char *fmt, ...);
 366 static  int sata_fetch_smart_return_status(sata_hba_inst_t *,
 367     sata_drive_info_t *);
 368 static  int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
 369     struct smart_data *);
 370 static  int sata_smart_selftest_log(sata_hba_inst_t *,
 371     sata_drive_info_t *,
 372     struct smart_selftest_log *);
 373 static  int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
 374     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
 375 static  int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
 376     uint8_t *, uint8_t, uint8_t);
 377 static  int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
 378     struct read_log_ext_directory *);
 379 static  void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
 380 static  void sata_xlate_errors(sata_pkt_txlate_t *);
 381 static  void sata_decode_device_error(sata_pkt_txlate_t *,
 382     struct scsi_extended_sense *);
 383 static  void sata_set_device_removed(dev_info_t *);
 384 static  boolean_t sata_check_device_removed(dev_info_t *);
 385 static  void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
 386 static  int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
 387     sata_drive_info_t *);
 388 static  int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
 389     sata_drive_info_t *);
 390 static  void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
 391 static  void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
 392 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
 393 static  int sata_check_modser(char *, int);
 394 
 395 /*
 396  * FMA
 397  */
 398 static boolean_t sata_check_for_dma_error(dev_info_t *, sata_pkt_txlate_t *);
 399 
 400 
 401 /*
 402  * SATA Framework will ignore SATA HBA driver cb_ops structure and
 403  * register following one with SCSA framework.
 404  * Open & close are provided, so scsi framework will not use its own
 405  */
 406 static struct cb_ops sata_cb_ops = {
 407         sata_hba_open,                  /* open */
 408         sata_hba_close,                 /* close */
 409         nodev,                          /* strategy */
 410         nodev,                          /* print */
 411         nodev,                          /* dump */
 412         nodev,                          /* read */
 413         nodev,                          /* write */
 414         sata_hba_ioctl,                 /* ioctl */
 415         nodev,                          /* devmap */
 416         nodev,                          /* mmap */
 417         nodev,                          /* segmap */
 418         nochpoll,                       /* chpoll */
 419         ddi_prop_op,                    /* cb_prop_op */
 420         0,                              /* streamtab */
 421         D_NEW | D_MP,                   /* cb_flag */
 422         CB_REV,                         /* rev */
 423         nodev,                          /* aread */
 424         nodev                           /* awrite */
 425 };
 426 
 427 
 428 extern struct mod_ops mod_miscops;
 429 extern uchar_t  scsi_cdb_size[];
 430 
 431 static struct modlmisc modlmisc = {
 432         &mod_miscops,                       /* Type of module */
 433         "SATA Module"                   /* module name */
 434 };
 435 
 436 
 437 static struct modlinkage modlinkage = {
 438         MODREV_1,
 439         { (void *)&modlmisc, NULL }
 440 };
 441 
 442 /*
 443  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
 444  * i.e. when scsi_pkt has not timeout specified.
 445  */
 446 static int sata_default_pkt_time = 60;  /* 60 seconds */
 447 
 448 /*
 449  * Intermediate buffer device access attributes - they are required,
 450  * but not necessarily used.
 451  */
 452 static ddi_device_acc_attr_t sata_acc_attr = {
 453         DDI_DEVICE_ATTR_V0,
 454         DDI_STRUCTURE_LE_ACC,
 455         DDI_STRICTORDER_ACC
 456 };
 457 
 458 
 459 /*
 460  * Mutexes protecting structures in multithreaded operations.
 461  * Because events are relatively rare, a single global mutex protecting
 462  * data structures should be sufficient. To increase performance, add
 463  * separate mutex per each sata port and use global mutex only to protect
 464  * common data structures.
 465  */
 466 static  kmutex_t sata_mutex;            /* protects sata_hba_list */
 467 static  kmutex_t sata_log_mutex;        /* protects log */
 468 
 469 static  char sata_log_buf[256];
 470 
 471 /*
 472  * sata trace debug
 473  */
 474 static  sata_trace_rbuf_t *sata_debug_rbuf;
 475 static  sata_trace_dmsg_t *sata_trace_dmsg_alloc(void);
 476 static  void sata_trace_dmsg_free(void);
 477 static  void sata_trace_rbuf_alloc(void);
 478 static  void sata_trace_rbuf_free(void);
 479 
 480 int     dmsg_ring_size = DMSG_RING_SIZE;
 481 
 482 /* Default write cache setting for SATA hard disks */
 483 int     sata_write_cache = 1;           /* enabled */
 484 
 485 /* Default write cache setting for SATA ATAPI CD/DVD */
 486 int     sata_atapicdvd_write_cache = 1; /* enabled */
 487 
 488 /* Default write cache setting for SATA ATAPI tape */
 489 int     sata_atapitape_write_cache = 1; /* enabled */
 490 
 491 /* Default write cache setting for SATA ATAPI disk */
 492 int     sata_atapidisk_write_cache = 1; /* enabled */
 493 
 494 /*
 495  * Linked list of HBA instances
 496  */
 497 static  sata_hba_inst_t *sata_hba_list = NULL;
 498 static  sata_hba_inst_t *sata_hba_list_tail = NULL;
 499 /*
 500  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
 501  * structure and in sata soft state.
 502  */
 503 
 504 /*
 505  * Event daemon related variables
 506  */
 507 static  kmutex_t sata_event_mutex;
 508 static  kcondvar_t sata_event_cv;
 509 static  kthread_t *sata_event_thread = NULL;
 510 static  int sata_event_thread_terminate = 0;
 511 static  int sata_event_pending = 0;
 512 static  int sata_event_thread_active = 0;
 513 extern  pri_t minclsyspri;
 514 
 515 /*
 516  * NCQ error recovery command
 517  */
 518 static const sata_cmd_t sata_rle_cmd = {
 519         SATA_CMD_REV,
 520         NULL,
 521         {
 522                 SATA_DIR_READ
 523         },
 524         ATA_ADDR_LBA48,
 525         0,
 526         0,
 527         0,
 528         0,
 529         0,
 530         1,
 531         READ_LOG_EXT_NCQ_ERROR_RECOVERY,
 532         0,
 533         0,
 534         0,
 535         SATAC_READ_LOG_EXT,
 536         0,
 537         0,
 538         0,
 539 };
 540 
 541 /*
 542  * ATAPI error recovery CDB
 543  */
 544 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
 545         SCMD_REQUEST_SENSE,
 546         0,                      /* Only fixed RQ format is supported */
 547         0,
 548         0,
 549         SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
 550         0
 551 };
 552 
 553 
 554 /* Warlock directives */
 555 
 556 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
 557 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
 558 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
 559 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
 560 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
 561 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
 562 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
 563 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
 564 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
 565 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
 566 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
 567 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
 568 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
 569 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
 570     sata_hba_inst::satahba_scsi_tran))
 571 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
 572 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
 573 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
 574 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
 575 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
 576     sata_hba_inst::satahba_event_flags))
 577 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
 578     sata_cport_info::cport_devp))
 579 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
 580 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
 581 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
 582     sata_cport_info::cport_dev_type))
 583 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
 584 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
 585     sata_cport_info::cport_state))
 586 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
 587 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
 588     sata_pmport_info::pmport_state))
 589 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
 590 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
 591     sata_pmport_info::pmport_dev_type))
 592 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
 593 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
 594     sata_pmport_info::pmport_sata_drive))
 595 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
 596     sata_pmport_info::pmport_tgtnode_clean))
 597 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
 598     sata_pmport_info::pmport_event_flags))
 599 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
 600 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
 601 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
 602 #ifdef SATA_DEBUG
 603 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
 604 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
 605 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
 606 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
 607 #endif
 608 
 609 /* End of warlock directives */
 610 
 611 /* ************** loadable module configuration functions ************** */
 612 
 613 int
 614 _init()
 615 {
 616         int rval;
 617 
 618         mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
 619         mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
 620         mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
 621         cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
 622         sata_trace_rbuf_alloc();
 623         if ((rval = mod_install(&modlinkage)) != 0) {
 624 #ifdef SATA_DEBUG
 625                 cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
 626 #endif
 627                 sata_trace_rbuf_free();
 628                 mutex_destroy(&sata_log_mutex);
 629                 cv_destroy(&sata_event_cv);
 630                 mutex_destroy(&sata_event_mutex);
 631                 mutex_destroy(&sata_mutex);
 632         }
 633         return (rval);
 634 }
 635 
 636 int
 637 _fini()
 638 {
 639         int rval;
 640 
 641         if ((rval = mod_remove(&modlinkage)) != 0)
 642                 return (rval);
 643 
 644         sata_trace_rbuf_free();
 645         mutex_destroy(&sata_log_mutex);
 646         cv_destroy(&sata_event_cv);
 647         mutex_destroy(&sata_event_mutex);
 648         mutex_destroy(&sata_mutex);
 649         return (rval);
 650 }
 651 
 652 int
 653 _info(struct modinfo *modinfop)
 654 {
 655         return (mod_info(&modlinkage, modinfop));
 656 }
 657 
 658 
 659 
 660 /* ********************* SATA HBA entry points ********************* */
 661 
 662 
 663 /*
 664  * Called by SATA HBA from _init().
 665  * Registers HBA driver instance/sata framework pair with scsi framework, by
 666  * calling scsi_hba_init().
 667  *
 668  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
 669  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
 670  * cb_ops pointer in SATA HBA driver dev_ops structure.
 671  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
 672  *
 673  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
 674  * driver.
 675  */
 676 int
 677 sata_hba_init(struct modlinkage *modlp)
 678 {
 679         int rval;
 680         struct dev_ops *hba_ops;
 681 
 682         SATADBG1(SATA_DBG_HBA_IF, NULL,
 683             "sata_hba_init: name %s \n",
 684             ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
 685         /*
 686          * Fill-up cb_ops and dev_ops when necessary
 687          */
 688         hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
 689         /*
 690          * Provide pointer to SATA dev_ops
 691          */
 692         hba_ops->devo_cb_ops = &sata_cb_ops;
 693 
 694         /*
 695          * Register SATA HBA with SCSI framework
 696          */
 697         if ((rval = scsi_hba_init(modlp)) != 0) {
 698                 SATADBG1(SATA_DBG_HBA_IF, NULL,
 699                     "sata_hba_init: scsi hba init failed\n", NULL);
 700                 return (rval);
 701         }
 702 
 703         return (0);
 704 }
 705 
 706 
 707 /* HBA attach stages */
 708 #define HBA_ATTACH_STAGE_SATA_HBA_INST  1
 709 #define HBA_ATTACH_STAGE_SCSI_ATTACHED  2
 710 #define HBA_ATTACH_STAGE_SETUP          4
 711 #define HBA_ATTACH_STAGE_LINKED         8
 712 
 713 
 714 /*
 715  *
 716  * Called from SATA HBA driver's attach routine to attach an instance of
 717  * the HBA.
 718  *
 719  * For DDI_ATTACH command:
 720  * sata_hba_inst structure is allocated here and initialized with pointers to
 721  * SATA framework implementation of required scsi tran functions.
 722  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
 723  * to the soft structure (sata_hba_inst) allocated by SATA framework for
 724  * SATA HBA instance related data.
 725  * The scsi_tran's tran_hba_private field is used by SATA framework to
 726  * store a pointer to per-HBA-instance of sata_hba_inst structure.
 727  * The sata_hba_inst structure is cross-linked to scsi tran structure.
 728  * Among other info, a pointer to sata_hba_tran structure is stored in
 729  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
 730  * linked together into the list, pointed to by sata_hba_list.
 731  * On the first HBA instance attach the sata event thread is initialized.
 732  * Attachment points are created for all SATA ports of the HBA being attached.
 733  * All HBA instance's SATA ports are probed and type of plugged devices is
 734  * determined. For each device of a supported type, a target node is created.
 735  *
 736  * DDI_SUCCESS is returned when attachment process is successful,
 737  * DDI_FAILURE is returned otherwise.
 738  *
 739  * For DDI_RESUME command:
 740  * Not implemented at this time (postponed until phase 2 of the development).
 741  */
 742 int
 743 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
 744     ddi_attach_cmd_t cmd)
 745 {
 746         sata_hba_inst_t *sata_hba_inst;
 747         scsi_hba_tran_t *scsi_tran = NULL;
 748         int hba_attach_state = 0;
 749         char taskq_name[MAXPATHLEN];
 750 
 751         SATADBG3(SATA_DBG_HBA_IF, NULL,
 752             "sata_hba_attach: node %s (%s%d)\n",
 753             ddi_node_name(dip), ddi_driver_name(dip),
 754             ddi_get_instance(dip));
 755 
 756         if (cmd == DDI_RESUME) {
 757                 /*
 758                  * Postponed until phase 2 of the development
 759                  */
 760                 return (DDI_FAILURE);
 761         }
 762 
 763         if (cmd != DDI_ATTACH) {
 764                 return (DDI_FAILURE);
 765         }
 766 
 767         /* cmd == DDI_ATTACH */
 768 
 769         if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
 770                 SATA_LOG_D((NULL, CE_WARN,
 771                     "sata_hba_attach: invalid sata_hba_tran"));
 772                 return (DDI_FAILURE);
 773         }
 774         /*
 775          * Allocate and initialize SCSI tran structure.
 776          * SATA copy of tran_bus_config is provided to create port nodes.
 777          */
 778         scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
 779         if (scsi_tran == NULL)
 780                 return (DDI_FAILURE);
 781         /*
 782          * Allocate soft structure for SATA HBA instance.
 783          * There is a separate softstate for each HBA instance.
 784          */
 785         sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
 786         ASSERT(sata_hba_inst != NULL); /* this should not fail */
 787         mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
 788         hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
 789 
 790         /*
 791          * scsi_trans's tran_hba_private is used by SATA Framework to point to
 792          * soft structure allocated by SATA framework for
 793          * SATA HBA instance related data.
 794          */
 795         scsi_tran->tran_hba_private  = sata_hba_inst;
 796         scsi_tran->tran_tgt_private  = NULL;
 797 
 798         scsi_tran->tran_tgt_init     = sata_scsi_tgt_init;
 799         scsi_tran->tran_tgt_probe    = sata_scsi_tgt_probe;
 800         scsi_tran->tran_tgt_free     = sata_scsi_tgt_free;
 801 
 802         scsi_tran->tran_start                = sata_scsi_start;
 803         scsi_tran->tran_reset                = sata_scsi_reset;
 804         scsi_tran->tran_abort                = sata_scsi_abort;
 805         scsi_tran->tran_getcap               = sata_scsi_getcap;
 806         scsi_tran->tran_setcap               = sata_scsi_setcap;
 807         scsi_tran->tran_init_pkt     = sata_scsi_init_pkt;
 808         scsi_tran->tran_destroy_pkt  = sata_scsi_destroy_pkt;
 809 
 810         scsi_tran->tran_dmafree              = sata_scsi_dmafree;
 811         scsi_tran->tran_sync_pkt     = sata_scsi_sync_pkt;
 812 
 813         scsi_tran->tran_reset_notify = NULL;
 814         scsi_tran->tran_get_bus_addr = NULL;
 815         scsi_tran->tran_quiesce              = NULL;
 816         scsi_tran->tran_unquiesce    = NULL;
 817         scsi_tran->tran_bus_reset    = NULL;
 818 
 819         if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
 820             scsi_tran, 0) != DDI_SUCCESS) {
 821 #ifdef SATA_DEBUG
 822                 cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
 823                     ddi_driver_name(dip), ddi_get_instance(dip));
 824 #endif
 825                 goto fail;
 826         }
 827         hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
 828 
 829         if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
 830                 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
 831                     "sata", 1) != DDI_PROP_SUCCESS) {
 832                         SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
 833                             "failed to create hba sata prop"));
 834                         goto fail;
 835                 }
 836         }
 837 
 838         /*
 839          * Save pointers in hba instance soft state.
 840          */
 841         sata_hba_inst->satahba_scsi_tran = scsi_tran;
 842         sata_hba_inst->satahba_tran = sata_tran;
 843         sata_hba_inst->satahba_dip = dip;
 844 
 845         /*
 846          * Create a task queue to handle emulated commands completion
 847          * Use node name, dash, instance number as the queue name.
 848          */
 849         taskq_name[0] = '\0';
 850         (void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
 851             sizeof (taskq_name));
 852         (void) snprintf(taskq_name + strlen(taskq_name),
 853             sizeof (taskq_name) - strlen(taskq_name),
 854             "-%d", DEVI(dip)->devi_instance);
 855         sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
 856             minclsyspri, 1, sata_tran->sata_tran_hba_num_cports * 4,
 857             TASKQ_DYNAMIC);
 858 
 859         hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
 860 
 861         /*
 862          * Create events thread if not created yet.
 863          */
 864         sata_event_thread_control(1);
 865 
 866         /*
 867          * Link this hba instance into the list.
 868          */
 869         mutex_enter(&sata_mutex);
 870 
 871         if (sata_hba_list == NULL) {
 872                 /*
 873                  * The first instance of HBA is attached.
 874                  * Set current/active default maximum NCQ/TCQ queue depth for
 875                  * all SATA devices. It is done here and now, to eliminate the
 876                  * possibility of the dynamic, programatic modification of the
 877                  * queue depth via global (and public) sata_max_queue_depth
 878                  * variable (this would require special handling in HBA drivers)
 879                  */
 880                 sata_current_max_qdepth = sata_max_queue_depth;
 881                 if (sata_current_max_qdepth > 32)
 882                         sata_current_max_qdepth = 32;
 883                 else if (sata_current_max_qdepth < 1)
 884                         sata_current_max_qdepth = 1;
 885         }
 886 
 887         sata_hba_inst->satahba_next = NULL;
 888         sata_hba_inst->satahba_prev = sata_hba_list_tail;
 889         if (sata_hba_list == NULL) {
 890                 sata_hba_list = sata_hba_inst;
 891         }
 892         if (sata_hba_list_tail != NULL) {
 893                 sata_hba_list_tail->satahba_next = sata_hba_inst;
 894         }
 895         sata_hba_list_tail = sata_hba_inst;
 896         mutex_exit(&sata_mutex);
 897         hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
 898 
 899         /*
 900          * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
 901          * SATA HBA driver should not use its own open/close entry points.
 902          *
 903          * Make sure that instance number doesn't overflow
 904          * when forming minor numbers.
 905          */
 906         ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
 907         if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
 908             INST2DEVCTL(ddi_get_instance(dip)),
 909             DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
 910 #ifdef SATA_DEBUG
 911                 cmn_err(CE_WARN, "sata_hba_attach: "
 912                     "cannot create devctl minor node");
 913 #endif
 914                 goto fail;
 915         }
 916 
 917 
 918         /*
 919          * Set-up kstats here, if necessary.
 920          * (postponed until future phase of the development).
 921          */
 922 
 923         /*
 924          * Indicate that HBA is attached. This will enable events processing
 925          * for this HBA.
 926          */
 927         sata_hba_inst->satahba_attached = 1;
 928         /*
 929          * Probe controller ports. This operation will describe a current
 930          * controller/port/multipliers/device configuration and will create
 931          * attachment points.
 932          * We may end-up with just a controller with no devices attached.
 933          * For the ports with a supported device attached, device target nodes
 934          * are created and devices are initialized.
 935          */
 936         sata_probe_ports(sata_hba_inst);
 937 
 938         return (DDI_SUCCESS);
 939 
 940 fail:
 941         if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
 942                 (void) sata_remove_hba_instance(dip);
 943                 if (sata_hba_list == NULL)
 944                         sata_event_thread_control(0);
 945         }
 946 
 947         if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
 948                 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
 949                 taskq_destroy(sata_hba_inst->satahba_taskq);
 950         }
 951 
 952         if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
 953                 (void) scsi_hba_detach(dip);
 954 
 955         if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
 956                 mutex_destroy(&sata_hba_inst->satahba_mutex);
 957                 kmem_free((void *)sata_hba_inst,
 958                     sizeof (struct sata_hba_inst));
 959                 scsi_hba_tran_free(scsi_tran);
 960         }
 961 
 962         sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
 963             ddi_driver_name(dip), ddi_get_instance(dip));
 964 
 965         return (DDI_FAILURE);
 966 }
 967 
 968 
 969 /*
 970  * Called by SATA HBA from to detach an instance of the driver.
 971  *
 972  * For DDI_DETACH command:
 973  * Free local structures allocated for SATA HBA instance during
 974  * sata_hba_attach processing.
 975  *
 976  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
 977  *
 978  * For DDI_SUSPEND command:
 979  * Not implemented at this time (postponed until phase 2 of the development)
 980  * Returnd DDI_SUCCESS.
 981  *
 982  * When the last HBA instance is detached, the event daemon is terminated.
 983  *
 984  * NOTE: Port multiplier is supported.
 985  */
 986 int
 987 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
 988 {
 989         dev_info_t      *tdip;
 990         sata_hba_inst_t *sata_hba_inst;
 991         scsi_hba_tran_t *scsi_hba_tran;
 992         sata_cport_info_t *cportinfo;
 993         sata_pmult_info_t *pminfo;
 994         sata_drive_info_t *sdinfo;
 995         sata_device_t   sdevice;
 996         int ncport, npmport;
 997 
 998         SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
 999             ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
1000 
1001         switch (cmd) {
1002         case DDI_DETACH:
1003 
1004                 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1005                         return (DDI_FAILURE);
1006 
1007                 sata_hba_inst = scsi_hba_tran->tran_hba_private;
1008                 if (sata_hba_inst == NULL)
1009                         return (DDI_FAILURE);
1010 
1011                 if (scsi_hba_detach(dip) == DDI_FAILURE) {
1012                         sata_hba_inst->satahba_attached = 1;
1013                         return (DDI_FAILURE);
1014                 }
1015 
1016                 /*
1017                  * Free all target nodes - at this point
1018                  * devices should be at least offlined
1019                  * otherwise scsi_hba_detach() should not be called.
1020                  */
1021                 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1022                     ncport++) {
1023                         cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1024                         if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1025                                 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
1026                                 if (sdinfo != NULL) {
1027                                         tdip = sata_get_target_dip(dip,
1028                                             ncport, 0);
1029                                         if (tdip != NULL) {
1030                                                 if (ndi_devi_offline(tdip,
1031                                                     NDI_DEVI_REMOVE) !=
1032                                                     NDI_SUCCESS) {
1033                                                         SATA_LOG_D((
1034                                                             sata_hba_inst,
1035                                                             CE_WARN,
1036                                                             "sata_hba_detach: "
1037                                                             "Target node not "
1038                                                             "removed !"));
1039                                                         return (DDI_FAILURE);
1040                                                 }
1041                                         }
1042                                 }
1043                         } else { /* SATA_DTYPE_PMULT */
1044                                 mutex_enter(&cportinfo->cport_mutex);
1045                                 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
1046 
1047                                 if (pminfo == NULL) {
1048                                         SATA_LOG_D((sata_hba_inst, CE_WARN,
1049                                             "sata_hba_detach: Port multiplier "
1050                                             "not ready yet!"));
1051                                         mutex_exit(&cportinfo->cport_mutex);
1052                                         return (DDI_FAILURE);
1053                                 }
1054 
1055                                 /*
1056                                  * Detach would fail if removal of any of the
1057                                  * target nodes is failed - albeit in that
1058                                  * case some of them may have been removed.
1059                                  */
1060                                 for (npmport = 0; npmport < SATA_NUM_PMPORTS(
1061                                     sata_hba_inst, ncport); npmport++) {
1062                                         tdip = sata_get_target_dip(dip, ncport,
1063                                             npmport);
1064                                         if (tdip != NULL) {
1065                                                 if (ndi_devi_offline(tdip,
1066                                                     NDI_DEVI_REMOVE) !=
1067                                                     NDI_SUCCESS) {
1068                                                         SATA_LOG_D((
1069                                                             sata_hba_inst,
1070                                                             CE_WARN,
1071                                                             "sata_hba_detach: "
1072                                                             "Target node not "
1073                                                             "removed !"));
1074                                                         mutex_exit(&cportinfo->
1075                                                             cport_mutex);
1076                                                         return (DDI_FAILURE);
1077                                                 }
1078                                         }
1079                                 }
1080                                 mutex_exit(&cportinfo->cport_mutex);
1081                         }
1082                 }
1083                 /*
1084                  * Disable sata event daemon processing for this HBA
1085                  */
1086                 sata_hba_inst->satahba_attached = 0;
1087 
1088                 /*
1089                  * Remove event daemon thread, if it is last HBA instance.
1090                  */
1091 
1092                 mutex_enter(&sata_mutex);
1093                 if (sata_hba_list->satahba_next == NULL) {
1094                         mutex_exit(&sata_mutex);
1095                         sata_event_thread_control(0);
1096                         mutex_enter(&sata_mutex);
1097                 }
1098                 mutex_exit(&sata_mutex);
1099 
1100                 /* Remove this HBA instance from the HBA list */
1101                 sata_remove_hba_instance(dip);
1102 
1103                 /*
1104                  * At this point there should be no target nodes attached.
1105                  * Detach and destroy device and port info structures.
1106                  */
1107                 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1108                     ncport++) {
1109                         cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1110                         if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1111                                 sdinfo =
1112                                     cportinfo->cport_devp.cport_sata_drive;
1113                                 if (sdinfo != NULL) {
1114                                         /* Release device structure */
1115                                         kmem_free(sdinfo,
1116                                             sizeof (sata_drive_info_t));
1117                                 }
1118                                 /* Release cport info */
1119                                 mutex_destroy(&cportinfo->cport_mutex);
1120                                 kmem_free(cportinfo,
1121                                     sizeof (sata_cport_info_t));
1122                         } else { /* SATA_DTYPE_PMULT */
1123                                 sdevice.satadev_addr.cport = (uint8_t)ncport;
1124                                 sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
1125                                 sata_free_pmult(sata_hba_inst, &sdevice);
1126                         }
1127                 }
1128 
1129                 scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
1130 
1131                 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
1132 
1133                 taskq_destroy(sata_hba_inst->satahba_taskq);
1134 
1135                 mutex_destroy(&sata_hba_inst->satahba_mutex);
1136                 kmem_free((void *)sata_hba_inst,
1137                     sizeof (struct sata_hba_inst));
1138 
1139                 return (DDI_SUCCESS);
1140 
1141         case DDI_SUSPEND:
1142                 /*
1143                  * Postponed until phase 2
1144                  */
1145                 return (DDI_FAILURE);
1146 
1147         default:
1148                 return (DDI_FAILURE);
1149         }
1150 }
1151 
1152 
1153 /*
1154  * Called by an HBA drive from _fini() routine.
1155  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1156  */
1157 void
1158 sata_hba_fini(struct modlinkage *modlp)
1159 {
1160         SATADBG1(SATA_DBG_HBA_IF, NULL,
1161             "sata_hba_fini: name %s\n",
1162             ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1163 
1164         scsi_hba_fini(modlp);
1165 }
1166 
1167 
1168 /*
1169  * Default open and close routine for sata_hba framework.
1170  *
1171  */
1172 /*
1173  * Open devctl node.
1174  *
1175  * Returns:
1176  * 0 if node was open successfully, error code otherwise.
1177  *
1178  *
1179  */
1180 
1181 static int
1182 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1183 {
1184 #ifndef __lock_lint
1185         _NOTE(ARGUNUSED(credp))
1186 #endif
1187         int rv = 0;
1188         dev_info_t *dip;
1189         scsi_hba_tran_t *scsi_hba_tran;
1190         sata_hba_inst_t *sata_hba_inst;
1191 
1192         SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1193 
1194         if (otyp != OTYP_CHR)
1195                 return (EINVAL);
1196 
1197         dip = sata_devt_to_devinfo(*devp);
1198         if (dip == NULL)
1199                 return (ENXIO);
1200 
1201         if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1202                 return (ENXIO);
1203 
1204         sata_hba_inst = scsi_hba_tran->tran_hba_private;
1205         if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1206                 return (ENXIO);
1207 
1208         mutex_enter(&sata_mutex);
1209         if (flags & FEXCL) {
1210                 if (sata_hba_inst->satahba_open_flag != 0) {
1211                         rv = EBUSY;
1212                 } else {
1213                         sata_hba_inst->satahba_open_flag =
1214                             SATA_DEVCTL_EXOPENED;
1215                 }
1216         } else {
1217                 if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1218                         rv = EBUSY;
1219                 } else {
1220                         sata_hba_inst->satahba_open_flag =
1221                             SATA_DEVCTL_SOPENED;
1222                 }
1223         }
1224         mutex_exit(&sata_mutex);
1225 
1226         return (rv);
1227 }
1228 
1229 
1230 /*
1231  * Close devctl node.
1232  * Returns:
1233  * 0 if node was closed successfully, error code otherwise.
1234  *
1235  */
1236 
1237 static int
1238 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1239 {
1240 #ifndef __lock_lint
1241         _NOTE(ARGUNUSED(credp))
1242         _NOTE(ARGUNUSED(flag))
1243 #endif
1244         dev_info_t *dip;
1245         scsi_hba_tran_t *scsi_hba_tran;
1246         sata_hba_inst_t *sata_hba_inst;
1247 
1248         SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1249 
1250         if (otyp != OTYP_CHR)
1251                 return (EINVAL);
1252 
1253         dip = sata_devt_to_devinfo(dev);
1254         if (dip == NULL)
1255                 return (ENXIO);
1256 
1257         if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1258                 return (ENXIO);
1259 
1260         sata_hba_inst = scsi_hba_tran->tran_hba_private;
1261         if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1262                 return (ENXIO);
1263 
1264         mutex_enter(&sata_mutex);
1265         sata_hba_inst->satahba_open_flag = 0;
1266         mutex_exit(&sata_mutex);
1267         return (0);
1268 }
1269 
1270 
1271 
1272 /*
1273  * Standard IOCTL commands for SATA hotplugging.
1274  * Implemented DEVCTL_AP commands:
1275  * DEVCTL_AP_CONNECT
1276  * DEVCTL_AP_DISCONNECT
1277  * DEVCTL_AP_CONFIGURE
1278  * DEVCTL_UNCONFIGURE
1279  * DEVCTL_AP_CONTROL
1280  *
1281  * Commands passed to default ndi ioctl handler:
1282  * DEVCTL_DEVICE_GETSTATE
1283  * DEVCTL_DEVICE_ONLINE
1284  * DEVCTL_DEVICE_OFFLINE
1285  * DEVCTL_DEVICE_REMOVE
1286  * DEVCTL_DEVICE_INSERT
1287  * DEVCTL_BUS_GETSTATE
1288  *
1289  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1290  * if not.
1291  *
1292  * Returns:
1293  * 0 if successful,
1294  * error code if operation failed.
1295  *
1296  * Port Multiplier support is supported now.
1297  *
1298  * NOTE: qual should be SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT
1299  */
1300 
1301 static int
1302 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1303     int *rvalp)
1304 {
1305 #ifndef __lock_lint
1306         _NOTE(ARGUNUSED(credp))
1307         _NOTE(ARGUNUSED(rvalp))
1308 #endif
1309         int rv = 0;
1310         int32_t comp_port = -1;
1311         dev_info_t *dip;
1312         devctl_ap_state_t ap_state;
1313         struct devctl_iocdata *dcp = NULL;
1314         scsi_hba_tran_t *scsi_hba_tran;
1315         sata_hba_inst_t *sata_hba_inst;
1316         sata_device_t sata_device;
1317         sata_cport_info_t *cportinfo;
1318         int cport, pmport, qual;
1319         int rval = SATA_SUCCESS;
1320 
1321         dip = sata_devt_to_devinfo(dev);
1322         if (dip == NULL)
1323                 return (ENXIO);
1324 
1325         if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1326                 return (ENXIO);
1327 
1328         sata_hba_inst = scsi_hba_tran->tran_hba_private;
1329         if (sata_hba_inst == NULL)
1330                 return (ENXIO);
1331 
1332         if (sata_hba_inst->satahba_tran == NULL)
1333                 return (ENXIO);
1334 
1335         switch (cmd) {
1336 
1337         case DEVCTL_DEVICE_GETSTATE:
1338         case DEVCTL_DEVICE_ONLINE:
1339         case DEVCTL_DEVICE_OFFLINE:
1340         case DEVCTL_DEVICE_REMOVE:
1341         case DEVCTL_BUS_GETSTATE:
1342                 /*
1343                  * There may be more cases that we want to pass to default
1344                  * handler rather than fail them.
1345                  */
1346                 return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1347         }
1348 
1349         /* read devctl ioctl data */
1350         if (cmd != DEVCTL_AP_CONTROL) {
1351                 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1352                         return (EFAULT);
1353 
1354                 if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1355                     -1) {
1356                         if (dcp)
1357                                 ndi_dc_freehdl(dcp);
1358                         return (EINVAL);
1359                 }
1360 
1361                 /*
1362                  * According to SCSI_TO_SATA_ADDR_QUAL, qual should be either
1363                  * SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT.
1364                  */
1365                 cport = SCSI_TO_SATA_CPORT(comp_port);
1366                 pmport = SCSI_TO_SATA_PMPORT(comp_port);
1367                 qual = SCSI_TO_SATA_ADDR_QUAL(comp_port);
1368 
1369                 if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1370                     qual) != 0) {
1371                         ndi_dc_freehdl(dcp);
1372                         return (EINVAL);
1373                 }
1374 
1375                 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1376                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1377                     cport_mutex);
1378                 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1379                         /*
1380                          * Cannot process ioctl request now. Come back later.
1381                          */
1382                         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1383                             cport_mutex);
1384                         ndi_dc_freehdl(dcp);
1385                         return (EBUSY);
1386                 }
1387                 /* Block event processing for this port */
1388                 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1389                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1390 
1391                 sata_device.satadev_addr.cport = cport;
1392                 sata_device.satadev_addr.pmport = pmport;
1393                 sata_device.satadev_addr.qual = qual;
1394                 sata_device.satadev_rev = SATA_DEVICE_REV;
1395         }
1396 
1397         switch (cmd) {
1398 
1399         case DEVCTL_AP_DISCONNECT:
1400 
1401                 /*
1402                  * Normally, cfgadm sata plugin will try to offline
1403                  * (unconfigure) device before this request. Nevertheless,
1404                  * if a device is still configured, we need to
1405                  * attempt to offline and unconfigure device first, and we will
1406                  * deactivate the port regardless of the unconfigure
1407                  * operation results.
1408                  *
1409                  */
1410                 rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1411 
1412                 break;
1413 
1414         case DEVCTL_AP_UNCONFIGURE:
1415 
1416                 /*
1417                  * The unconfigure operation uses generic nexus operation to
1418                  * offline a device. It leaves a target device node attached.
1419                  * and obviously sata_drive_info attached as well, because
1420                  * from the hardware point of view nothing has changed.
1421                  */
1422                 rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1423                 break;
1424 
1425         case DEVCTL_AP_CONNECT:
1426         {
1427                 /*
1428                  * The sata cfgadm pluging will invoke this operation only if
1429                  * port was found in the disconnect state (failed state
1430                  * is also treated as the disconnected state).
1431                  * If port activation is successful and a device is found
1432                  * attached to the port, the initialization sequence is
1433                  * executed to probe the port and attach
1434                  * a device structure to a port structure. The device is not
1435                  * set in configured state (system-wise) by this operation.
1436                  */
1437 
1438                 rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1439 
1440                 break;
1441         }
1442 
1443         case DEVCTL_AP_CONFIGURE:
1444         {
1445                 /*
1446                  * A port may be in an active or shutdown state.
1447                  * If port is in a failed state, operation is aborted.
1448                  * If a port is in a shutdown state, sata_tran_port_activate()
1449                  * is invoked prior to any other operation.
1450                  *
1451                  * Onlining the device involves creating a new target node.
1452                  * If there is an old target node present (belonging to
1453                  * previously removed device), the operation is aborted - the
1454                  * old node has to be released and removed before configure
1455                  * operation is attempted.
1456                  */
1457 
1458                 rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1459 
1460                 break;
1461         }
1462 
1463         case DEVCTL_AP_GETSTATE:
1464 
1465                 sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1466 
1467                 ap_state.ap_last_change = (time_t)-1;
1468                 ap_state.ap_error_code = 0;
1469                 ap_state.ap_in_transition = 0;
1470 
1471                 /* Copy the return AP-state information to the user space */
1472                 if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1473                         rv = EFAULT;
1474                 }
1475                 break;
1476 
1477         case DEVCTL_AP_CONTROL:
1478         {
1479                 /*
1480                  * Generic devctl for hardware specific functionality
1481                  */
1482                 sata_ioctl_data_t       ioc;
1483 
1484                 ASSERT(dcp == NULL);
1485 
1486                 /* Copy in user ioctl data first */
1487 #ifdef _MULTI_DATAMODEL
1488                 if (ddi_model_convert_from(mode & FMODELS) ==
1489                     DDI_MODEL_ILP32) {
1490 
1491                         sata_ioctl_data_32_t    ioc32;
1492 
1493                         if (ddi_copyin((void *)arg, (void *)&ioc32,
1494                             sizeof (ioc32), mode) != 0) {
1495                                 rv = EFAULT;
1496                                 break;
1497                         }
1498                         ioc.cmd         = (uint_t)ioc32.cmd;
1499                         ioc.port        = (uint_t)ioc32.port;
1500                         ioc.get_size    = (uint_t)ioc32.get_size;
1501                         ioc.buf         = (caddr_t)(uintptr_t)ioc32.buf;
1502                         ioc.bufsiz      = (uint_t)ioc32.bufsiz;
1503                         ioc.misc_arg    = (uint_t)ioc32.misc_arg;
1504                 } else
1505 #endif /* _MULTI_DATAMODEL */
1506                 if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1507                     mode) != 0) {
1508                         return (EFAULT);
1509                 }
1510 
1511                 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1512                     "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1513                     "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1514 
1515                 /*
1516                  * To avoid BE/LE and 32/64 issues, a get_size always returns
1517                  * a 32-bit number.
1518                  */
1519                 if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1520                         return (EINVAL);
1521                 }
1522                 /* validate address */
1523                 cport = SCSI_TO_SATA_CPORT(ioc.port);
1524                 pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1525                 qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1526 
1527                 SATADBG3(SATA_DBG_IOCTL_IF, sata_hba_inst,
1528                     "sata_hba_ioctl: target port is %d:%d (%d)",
1529                     cport, pmport, qual);
1530 
1531                 if (sata_validate_sata_address(sata_hba_inst, cport,
1532                     pmport, qual) != 0)
1533                         return (EINVAL);
1534 
1535                 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1536                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1537                     cport_mutex);
1538                 /* Is the port locked by event processing daemon ? */
1539                 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1540                         /*
1541                          * Cannot process ioctl request now. Come back later
1542                          */
1543                         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1544                             cport_mutex);
1545                         return (EBUSY);
1546                 }
1547                 /* Block event processing for this port */
1548                 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1549                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1550 
1551 
1552                 sata_device.satadev_addr.cport = cport;
1553                 sata_device.satadev_addr.pmport = pmport;
1554                 sata_device.satadev_addr.qual = qual;
1555                 sata_device.satadev_rev = SATA_DEVICE_REV;
1556 
1557                 switch (ioc.cmd) {
1558 
1559                 case SATA_CFGA_RESET_PORT:
1560                         /*
1561                          * There is no protection for configured device.
1562                          */
1563                         rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1564                         break;
1565 
1566                 case SATA_CFGA_RESET_DEVICE:
1567                         /*
1568                          * There is no protection for configured device.
1569                          */
1570                         rv = sata_ioctl_reset_device(sata_hba_inst,
1571                             &sata_device);
1572                         break;
1573 
1574                 case SATA_CFGA_RESET_ALL:
1575                         /*
1576                          * There is no protection for configured devices.
1577                          */
1578                         rv = sata_ioctl_reset_all(sata_hba_inst);
1579                         /*
1580                          * We return here, because common return is for
1581                          * a single port operation - we have already unlocked
1582                          * all ports and no dc handle was allocated.
1583                          */
1584                         return (rv);
1585 
1586                 case SATA_CFGA_PORT_DEACTIVATE:
1587                         /*
1588                          * Arbitrarily unconfigure attached device, if any.
1589                          * Even if the unconfigure fails, proceed with the
1590                          * port deactivation.
1591                          */
1592                         rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1593 
1594                         break;
1595 
1596                 case SATA_CFGA_PORT_ACTIVATE:
1597 
1598                         rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1599                         break;
1600 
1601                 case SATA_CFGA_PORT_SELF_TEST:
1602 
1603                         rv = sata_ioctl_port_self_test(sata_hba_inst,
1604                             &sata_device);
1605                         break;
1606 
1607                 case SATA_CFGA_GET_DEVICE_PATH:
1608 
1609                         rv = sata_ioctl_get_device_path(sata_hba_inst,
1610                             &sata_device, &ioc, mode);
1611                         break;
1612 
1613                 case SATA_CFGA_GET_AP_TYPE:
1614 
1615                         rv = sata_ioctl_get_ap_type(sata_hba_inst,
1616                             &sata_device, &ioc, mode);
1617                         break;
1618 
1619                 case SATA_CFGA_GET_MODEL_INFO:
1620 
1621                         rv = sata_ioctl_get_model_info(sata_hba_inst,
1622                             &sata_device, &ioc, mode);
1623                         break;
1624 
1625                 case SATA_CFGA_GET_REVFIRMWARE_INFO:
1626 
1627                         rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1628                             &sata_device, &ioc, mode);
1629                         break;
1630 
1631                 case SATA_CFGA_GET_SERIALNUMBER_INFO:
1632 
1633                         rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1634                             &sata_device, &ioc, mode);
1635                         break;
1636 
1637                 default:
1638                         rv = EINVAL;
1639                         break;
1640 
1641                 } /* End of DEVCTL_AP_CONTROL cmd switch */
1642 
1643                 break;
1644         }
1645 
1646         default:
1647         {
1648                 /*
1649                  * If we got here, we got an IOCTL that SATA HBA Framework
1650                  * does not recognize. Pass ioctl to HBA driver, in case
1651                  * it could process it.
1652                  */
1653                 sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1654                 dev_info_t      *mydip = SATA_DIP(sata_hba_inst);
1655 
1656                 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1657                     "IOCTL 0x%2x not supported in SATA framework, "
1658                     "passthrough to HBA", cmd);
1659 
1660                 if (sata_tran->sata_tran_ioctl == NULL) {
1661                         rv = EINVAL;
1662                         break;
1663                 }
1664                 rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1665                 if (rval != 0) {
1666                         SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1667                             "IOCTL 0x%2x failed in HBA", cmd);
1668                         rv = rval;
1669                 }
1670                 break;
1671         }
1672 
1673         } /* End of main IOCTL switch */
1674 
1675         if (dcp) {
1676                 ndi_dc_freehdl(dcp);
1677         }
1678         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1679         cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1680         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1681 
1682         return (rv);
1683 }
1684 
1685 
1686 /*
1687  * Create error retrieval sata packet
1688  *
1689  * A sata packet is allocated and set-up to contain specified error retrieval
1690  * command and appropriate dma-able data buffer.
1691  * No association with any scsi packet is made and no callback routine is
1692  * specified.
1693  *
1694  * Returns a pointer to sata packet upon successful packet creation.
1695  * Returns NULL, if packet cannot be created.
1696  */
1697 sata_pkt_t *
1698 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1699     int pkt_type)
1700 {
1701         sata_hba_inst_t *sata_hba_inst;
1702         sata_pkt_txlate_t *spx;
1703         sata_pkt_t *spkt;
1704         sata_drive_info_t *sdinfo;
1705 
1706         mutex_enter(&sata_mutex);
1707         for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1708             sata_hba_inst = sata_hba_inst->satahba_next) {
1709                 if (SATA_DIP(sata_hba_inst) == dip)
1710                         break;
1711         }
1712         mutex_exit(&sata_mutex);
1713         ASSERT(sata_hba_inst != NULL);
1714 
1715         sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1716         if (sdinfo == NULL) {
1717                 sata_log(sata_hba_inst, CE_WARN,
1718                     "sata: error recovery request for non-attached device at "
1719                     "cport %d", sata_device->satadev_addr.cport);
1720                 return (NULL);
1721         }
1722 
1723         spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1724         spx->txlt_sata_hba_inst = sata_hba_inst;
1725         spx->txlt_scsi_pkt = NULL;           /* No scsi pkt involved */
1726         spkt = sata_pkt_alloc(spx, NULL);
1727         if (spkt == NULL) {
1728                 kmem_free(spx, sizeof (sata_pkt_txlate_t));
1729                 return (NULL);
1730         }
1731         /* address is needed now */
1732         spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1733 
1734         switch (pkt_type) {
1735         case SATA_ERR_RETR_PKT_TYPE_NCQ:
1736                 if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1737                         if (sata_check_for_dma_error(dip, spx)) {
1738                                 ddi_fm_service_impact(dip,
1739                                     DDI_SERVICE_UNAFFECTED);
1740                                 break;
1741                         }
1742                         return (spkt);
1743                 }
1744                 break;
1745 
1746         case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1747                 if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1748                         if (sata_check_for_dma_error(dip, spx)) {
1749                                 ddi_fm_service_impact(dip,
1750                                     DDI_SERVICE_UNAFFECTED);
1751                                 break;
1752                         }
1753                         return (spkt);
1754                 }
1755                 break;
1756 
1757         default:
1758                 break;
1759         }
1760 
1761         sata_pkt_free(spx);
1762         kmem_free(spx, sizeof (sata_pkt_txlate_t));
1763         return (NULL);
1764 
1765 }
1766 
1767 
1768 /*
1769  * Free error retrieval sata packet
1770  *
1771  * Free sata packet and any associated resources allocated previously by
1772  * sata_get_error_retrieval_pkt().
1773  *
1774  * Void return.
1775  */
1776 void
1777 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1778 {
1779         sata_pkt_txlate_t *spx =
1780             (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1781 
1782         ASSERT(sata_pkt != NULL);
1783 
1784         sata_free_local_buffer(spx);
1785         sata_pkt_free(spx);
1786         kmem_free(spx, sizeof (sata_pkt_txlate_t));
1787 
1788 }
1789 
1790 /*
1791  * Create READ PORT MULTIPLIER and WRITE PORT MULTIPLIER sata packet
1792  *
1793  * No association with any scsi packet is made and no callback routine is
1794  * specified.
1795  *
1796  * Returns a pointer to sata packet upon successful packet creation.
1797  * Returns NULL, if packet cannot be created.
1798  *
1799  * NOTE: Input/Output value includes 64 bits accoring to SATA Spec 2.6,
1800  * only lower 32 bits are available currently.
1801  */
1802 sata_pkt_t *
1803 sata_get_rdwr_pmult_pkt(dev_info_t *dip, sata_device_t *sd,
1804     uint8_t regn, uint32_t regv, uint32_t type)
1805 {
1806         sata_hba_inst_t *sata_hba_inst;
1807         sata_pkt_txlate_t *spx;
1808         sata_pkt_t *spkt;
1809         sata_cmd_t *scmd;
1810 
1811         /* Only READ/WRITE commands are accepted. */
1812         ASSERT(type == SATA_RDWR_PMULT_PKT_TYPE_READ ||
1813             type == SATA_RDWR_PMULT_PKT_TYPE_WRITE);
1814 
1815         mutex_enter(&sata_mutex);
1816         for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1817             sata_hba_inst = sata_hba_inst->satahba_next) {
1818                 if (SATA_DIP(sata_hba_inst) == dip)
1819                         break;
1820         }
1821         mutex_exit(&sata_mutex);
1822         ASSERT(sata_hba_inst != NULL);
1823 
1824         spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1825         spx->txlt_sata_hba_inst = sata_hba_inst;
1826         spx->txlt_scsi_pkt = NULL;   /* No scsi pkt involved */
1827         spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
1828         if (spkt == NULL) {
1829                 kmem_free(spx, sizeof (sata_pkt_txlate_t));
1830                 return (NULL);
1831         }
1832 
1833         /*
1834          * NOTE: We need to send this command to the port multiplier,
1835          * that means send to SATA_PMULT_HOSTPORT(0xf) pmport
1836          *
1837          * sata_device contains the address of actual target device, and the
1838          * pmport number in the command comes from the sata_device structure.
1839          */
1840         spkt->satapkt_device.satadev_addr = sd->satadev_addr;
1841         spkt->satapkt_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
1842         spkt->satapkt_device.satadev_addr.qual = SATA_ADDR_PMULT;
1843 
1844         /* Fill sata_pkt */
1845         spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING;
1846         spkt->satapkt_comp = NULL; /* Synchronous mode, no callback */
1847         spkt->satapkt_time = 10; /* Timeout 10s */
1848 
1849         /* Build READ PORT MULTIPLIER cmd in the sata_pkt */
1850         scmd = &spkt->satapkt_cmd;
1851         scmd->satacmd_features_reg = regn & 0xff;
1852         scmd->satacmd_features_reg_ext = (regn >> 8) & 0xff;
1853         scmd->satacmd_device_reg = sd->satadev_addr.pmport;
1854         scmd->satacmd_addr_type = 0;         /* N/A */
1855 
1856         scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
1857 
1858         if (type == SATA_RDWR_PMULT_PKT_TYPE_READ) {
1859                 scmd->satacmd_cmd_reg = SATAC_READ_PORTMULT;
1860                 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
1861                 scmd->satacmd_flags.sata_special_regs = 1;
1862                 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
1863                 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
1864                 scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
1865                 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
1866         } else if (type == SATA_RDWR_PMULT_PKT_TYPE_WRITE) {
1867                 scmd->satacmd_cmd_reg = SATAC_WRITE_PORTMULT;
1868                 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
1869                 scmd->satacmd_sec_count_lsb = regv & 0xff;
1870                 scmd->satacmd_lba_low_lsb = regv >> 8 & 0xff;
1871                 scmd->satacmd_lba_mid_lsb = regv >> 16 & 0xff;
1872                 scmd->satacmd_lba_high_lsb = regv >> 24 & 0xff;
1873         }
1874 
1875         return (spkt);
1876 }
1877 
1878 /*
1879  * Free sata packet and any associated resources allocated previously by
1880  * sata_get_rdwr_pmult_pkt().
1881  *
1882  * Void return.
1883  */
1884 void
1885 sata_free_rdwr_pmult_pkt(sata_pkt_t *sata_pkt)
1886 {
1887         sata_pkt_txlate_t *spx =
1888             (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1889 
1890         /* Free allocated resources */
1891         sata_pkt_free(spx);
1892         kmem_free(spx, sizeof (sata_pkt_txlate_t));
1893 }
1894 
1895 /*
1896  * Register a port multiplier to framework.
1897  * 1) Store the GSCR values in the previous allocated pmult_info strctures.
1898  * 2) Search in the blacklist and update the number of the device ports of the
1899  * port multiplier.
1900  *
1901  * Void return.
1902  */
1903 void
1904 sata_register_pmult(dev_info_t *dip, sata_device_t *sd, sata_pmult_gscr_t *sg)
1905 {
1906         sata_hba_inst_t *sata_hba_inst = NULL;
1907         sata_pmult_info_t *pmultinfo;
1908         sata_pmult_bl_t *blp;
1909         int cport = sd->satadev_addr.cport;
1910 
1911         mutex_enter(&sata_mutex);
1912         for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1913             sata_hba_inst = sata_hba_inst->satahba_next) {
1914                 if (SATA_DIP(sata_hba_inst) == dip)
1915                         if (sata_hba_inst->satahba_attached == 1)
1916                                 break;
1917         }
1918         mutex_exit(&sata_mutex);
1919         /* HBA not attached? */
1920         if (sata_hba_inst == NULL)
1921                 return;
1922 
1923         /* Number of pmports */
1924         sd->satadev_add_info = sg->gscr2 & SATA_PMULT_PORTNUM_MASK;
1925 
1926         /* Check the blacklist */
1927         for (blp = sata_pmult_blacklist; blp->bl_gscr0; blp++) {
1928                 if (sg->gscr0 != blp->bl_gscr0 && blp->bl_gscr0)
1929                         continue;
1930                 if (sg->gscr1 != blp->bl_gscr1 && blp->bl_gscr1)
1931                         continue;
1932                 if (sg->gscr2 != blp->bl_gscr2 && blp->bl_gscr2)
1933                         continue;
1934 
1935                 cmn_err(CE_WARN, "!Port multiplier is on the blacklist.");
1936                 sd->satadev_add_info = blp->bl_flags;
1937                 break;
1938         }
1939 
1940         /* Register the port multiplier GSCR */
1941         mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1942         pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
1943         if (pmultinfo != NULL) {
1944                 pmultinfo->pmult_gscr = *sg;
1945                 pmultinfo->pmult_num_dev_ports =
1946                     sd->satadev_add_info & SATA_PMULT_PORTNUM_MASK;
1947                 SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
1948                     "Port multiplier registered at port %d", cport);
1949         }
1950         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1951 }
1952 
1953 /*
1954  * sata_split_model splits the model ID into vendor and product IDs.
1955  * It assumes that a vendor ID cannot be longer than 8 characters, and
1956  * that vendor and product ID are separated by a whitespace.
1957  */
1958 void
1959 sata_split_model(char *model, char **vendor, char **product)
1960 {
1961         int i, modlen;
1962         char *vid, *pid;
1963 
1964         /*
1965          * remove whitespace at the end of model
1966          */
1967         for (i = SATA_ID_MODEL_LEN; i > 0; i--)
1968                 if (model[i] == ' ' || model[i] == '\t' || model[i] == '\0')
1969                         model[i] = '\0';
1970                 else
1971                         break;
1972 
1973         /*
1974          * try to split model into into vid/pid
1975          */
1976         modlen = strlen(model);
1977         for (i = 0, pid = model; i < modlen; i++, pid++)
1978                 if ((*pid == ' ') || (*pid == '\t'))
1979                         break;
1980 
1981         /*
1982          * only use vid if it is less than 8 chars (as in SCSI)
1983          */
1984         if (i < modlen && i <= 8) {
1985                 vid = model;
1986                 /*
1987                  * terminate vid, establish pid
1988                  */
1989                 *pid++ = '\0';
1990         } else {
1991                 /*
1992                  * vid will stay "ATA     "
1993                  */
1994                 vid = NULL;
1995                 /*
1996                  * model is all pid
1997                  */
1998                 pid = model;
1999         }
2000 
2001         *vendor = vid;
2002         *product = pid;
2003 }
2004 
2005 /*
2006  * sata_name_child is for composing the name of the node
2007  * the format of the name is "target,0".
2008  */
2009 static int
2010 sata_name_child(dev_info_t *dip, char *name, int namelen)
2011 {
2012         int target;
2013 
2014         target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
2015             DDI_PROP_DONTPASS, "target", -1);
2016         if (target == -1)
2017                 return (DDI_FAILURE);
2018         (void) snprintf(name, namelen, "%x,0", target);
2019         return (DDI_SUCCESS);
2020 }
2021 
2022 
2023 
2024 /* ****************** SCSA required entry points *********************** */
2025 
2026 /*
2027  * Implementation of scsi tran_tgt_init.
2028  * sata_scsi_tgt_init() initializes scsi_device structure
2029  *
2030  * If successful, DDI_SUCCESS is returned.
2031  * DDI_FAILURE is returned if addressed device does not exist
2032  */
2033 
2034 static int
2035 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2036     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2037 {
2038 #ifndef __lock_lint
2039         _NOTE(ARGUNUSED(hba_dip))
2040         _NOTE(ARGUNUSED(tgt_dip))
2041 #endif
2042         sata_device_t           sata_device;
2043         sata_drive_info_t       *sdinfo;
2044         struct sata_id          *sid;
2045         sata_hba_inst_t         *sata_hba_inst;
2046         char                    model[SATA_ID_MODEL_LEN + 1];
2047         char                    fw[SATA_ID_FW_LEN + 1];
2048         char                    *vid, *pid;
2049 
2050         /*
2051          * Fail tran_tgt_init for .conf stub node
2052          */
2053         if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
2054                 (void) ndi_merge_node(tgt_dip, sata_name_child);
2055                 ddi_set_name_addr(tgt_dip, NULL);
2056                 return (DDI_FAILURE);
2057         }
2058 
2059         sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2060 
2061         /* Validate scsi device address */
2062         if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2063             &sata_device) != 0)
2064                 return (DDI_FAILURE);
2065 
2066         mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2067             sata_device.satadev_addr.cport)));
2068 
2069         /* sata_device now contains a valid sata address */
2070         sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2071         if (sdinfo == NULL) {
2072                 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2073                     sata_device.satadev_addr.cport)));
2074                 return (DDI_FAILURE);
2075         }
2076         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2077             sata_device.satadev_addr.cport)));
2078 
2079         /*
2080          * Check if we need to create a legacy devid (i.e cmdk style) for
2081          * the target disks.
2082          *
2083          * HBA devinfo node will have the property "use-cmdk-devid-format"
2084          * if we need to create cmdk-style devid for all the disk devices
2085          * attached to this controller. This property may have been set
2086          * from HBA driver's .conf file or by the HBA driver in its
2087          * attach(9F) function.
2088          */
2089         if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2090             (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2091             "use-cmdk-devid-format", 0) == 1)) {
2092                 /* register a legacy devid for this target node */
2093                 sata_target_devid_register(tgt_dip, sdinfo);
2094         }
2095 
2096 
2097         /*
2098          * 'Identify Device Data' does not always fit in standard SCSI
2099          * INQUIRY data, so establish INQUIRY_* properties with full-form
2100          * of information.
2101          */
2102         sid = &sdinfo->satadrv_id;
2103 #ifdef  _LITTLE_ENDIAN
2104         swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
2105         swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
2106 #else   /* _LITTLE_ENDIAN */
2107         bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
2108         bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
2109 #endif  /* _LITTLE_ENDIAN */
2110         model[SATA_ID_MODEL_LEN] = 0;
2111         fw[SATA_ID_FW_LEN] = 0;
2112 
2113         sata_split_model(model, &vid, &pid);
2114 
2115         if (vid)
2116                 (void) scsi_device_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
2117                     vid, strlen(vid));
2118         if (pid)
2119                 (void) scsi_device_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
2120                     pid, strlen(pid));
2121         (void) scsi_device_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
2122             fw, strlen(fw));
2123 
2124         return (DDI_SUCCESS);
2125 }
2126 
2127 /*
2128  * Implementation of scsi tran_tgt_probe.
2129  * Probe target, by calling default scsi routine scsi_hba_probe()
2130  */
2131 static int
2132 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
2133 {
2134         sata_hba_inst_t *sata_hba_inst =
2135             (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
2136         int rval;
2137         uint32_t pm_cap;
2138 
2139         rval = scsi_hba_probe(sd, callback);
2140         pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE |
2141             SATA_CAP_LOG_SENSE;
2142 
2143         if (rval == SCSIPROBE_EXISTS) {
2144                 /*
2145                  * Set property "pm-capable" on the target device node, so that
2146                  * the target driver will not try to fetch scsi cycle counters
2147                  * before enabling device power-management.
2148                  */
2149                 if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
2150                     "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) {
2151                         sata_log(sata_hba_inst, CE_WARN,
2152                             "SATA device at port %d: "
2153                             "will not be power-managed ",
2154                             SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
2155                         SATA_LOG_D((sata_hba_inst, CE_WARN,
2156                             "failure updating pm-capable property"));
2157                 }
2158         }
2159         return (rval);
2160 }
2161 
2162 /*
2163  * Implementation of scsi tran_tgt_free.
2164  * Release all resources allocated for scsi_device
2165  */
2166 static void
2167 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2168     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2169 {
2170 #ifndef __lock_lint
2171         _NOTE(ARGUNUSED(hba_dip))
2172 #endif
2173         sata_device_t           sata_device;
2174         sata_drive_info_t       *sdinfo;
2175         sata_hba_inst_t         *sata_hba_inst;
2176         ddi_devid_t             devid;
2177 
2178         sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2179 
2180         /* Validate scsi device address */
2181         /*
2182          * Note: tgt_free relates to the SCSA view of a device. If called, there
2183          * was a device at this address, so even if the sata framework internal
2184          * resources were alredy released because a device was detached,
2185          * this function should be executed as long as its actions do
2186          * not require the internal sata view of a device and the address
2187          * refers to a valid sata address.
2188          * Validating the address here means that we do not trust SCSA...
2189          */
2190         if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2191             &sata_device) == -1)
2192                 return;
2193 
2194         mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2195             sata_device.satadev_addr.cport)));
2196 
2197         /* sata_device now should contain a valid sata address */
2198         sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2199         if (sdinfo == NULL) {
2200                 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2201                     sata_device.satadev_addr.cport)));
2202                 return;
2203         }
2204         /*
2205          * We did not allocate any resources in sata_scsi_tgt_init()
2206          * other than few properties.
2207          * Free them.
2208          */
2209         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2210             sata_device.satadev_addr.cport)));
2211         (void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
2212 
2213         /*
2214          * If devid was previously created but not freed up from
2215          * sd(7D) driver (i.e during detach(9F)) then do it here.
2216          */
2217         if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2218             (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2219             "use-cmdk-devid-format", 0) == 1) &&
2220             (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
2221                 ddi_devid_unregister(tgt_dip);
2222                 ddi_devid_free(devid);
2223         }
2224 }
2225 
2226 /*
2227  * Implementation of scsi tran_init_pkt
2228  * Upon successful return, scsi pkt buffer has DMA resources allocated.
2229  *
2230  * It seems that we should always allocate pkt, even if the address is
2231  * for non-existing device - just use some default for dma_attr.
2232  * The reason is that there is no way to communicate this to a caller here.
2233  * Subsequent call to sata_scsi_start may fail appropriately.
2234  * Simply returning NULL does not seem to discourage a target driver...
2235  *
2236  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
2237  */
2238 static struct scsi_pkt *
2239 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
2240     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
2241     int (*callback)(caddr_t), caddr_t arg)
2242 {
2243         sata_hba_inst_t *sata_hba_inst =
2244             (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2245         dev_info_t *dip = SATA_DIP(sata_hba_inst);
2246         sata_device_t sata_device;
2247         sata_drive_info_t *sdinfo;
2248         sata_pkt_txlate_t *spx;
2249         ddi_dma_attr_t cur_dma_attr;
2250         int rval;
2251         boolean_t new_pkt = B_TRUE;
2252 
2253         ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
2254 
2255         /*
2256          * We need to translate the address, even if it could be
2257          * a bogus one, for a non-existing device
2258          */
2259         sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
2260         sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
2261         sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2262         sata_device.satadev_rev = SATA_DEVICE_REV;
2263 
2264         if (pkt == NULL) {
2265                 /*
2266                  * Have to allocate a brand new scsi packet.
2267                  * We need to operate with auto request sense enabled.
2268                  */
2269                 pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
2270                     MAX(statuslen, SATA_MAX_SENSE_LEN),
2271                     tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
2272 
2273                 if (pkt == NULL)
2274                         return (NULL);
2275 
2276                 /* Fill scsi packet structure */
2277                 pkt->pkt_comp                = (void (*)())NULL;
2278                 pkt->pkt_time                = 0;
2279                 pkt->pkt_resid               = 0;
2280                 pkt->pkt_statistics  = 0;
2281                 pkt->pkt_reason              = 0;
2282 
2283                 /*
2284                  * pkt_hba_private will point to sata pkt txlate structure
2285                  */
2286                 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2287                 bzero(spx, sizeof (sata_pkt_txlate_t));
2288 
2289                 spx->txlt_scsi_pkt = pkt;
2290                 spx->txlt_sata_hba_inst = sata_hba_inst;
2291 
2292                 /* Allocate sata_pkt */
2293                 spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
2294                 if (spx->txlt_sata_pkt == NULL) {
2295                         /* Could not allocate sata pkt */
2296                         scsi_hba_pkt_free(ap, pkt);
2297                         return (NULL);
2298                 }
2299                 /* Set sata address */
2300                 spx->txlt_sata_pkt->satapkt_device.satadev_addr =
2301                     sata_device.satadev_addr;
2302                 spx->txlt_sata_pkt->satapkt_device.satadev_rev =
2303                     sata_device.satadev_rev;
2304 
2305                 if ((bp == NULL) || (bp->b_bcount == 0))
2306                         return (pkt);
2307 
2308                 spx->txlt_total_residue = bp->b_bcount;
2309         } else {
2310                 new_pkt = B_FALSE;
2311                 /*
2312                  * Packet was preallocated/initialized by previous call
2313                  */
2314                 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2315 
2316                 if ((bp == NULL) || (bp->b_bcount == 0)) {
2317                         return (pkt);
2318                 }
2319 
2320                 /* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
2321         }
2322 
2323         spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
2324 
2325         /*
2326          * We use an adjusted version of the dma_attr, to account
2327          * for device addressing limitations.
2328          * sata_adjust_dma_attr() will handle sdinfo == NULL which may
2329          * happen when a device is not yet configured.
2330          */
2331         mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2332             sata_device.satadev_addr.cport)));
2333         sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2334             &spx->txlt_sata_pkt->satapkt_device);
2335         /* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
2336         sata_adjust_dma_attr(sdinfo,
2337             SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
2338         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2339             sata_device.satadev_addr.cport)));
2340         /*
2341          * Allocate necessary DMA resources for the packet's data buffer
2342          * NOTE:
2343          * In case of read/write commands, DMA resource allocation here is
2344          * based on the premise that the transfer length specified in
2345          * the read/write scsi cdb will match exactly DMA resources -
2346          * returning correct packet residue is crucial.
2347          */
2348         if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
2349             &cur_dma_attr)) != DDI_SUCCESS) {
2350                 /*
2351                  * If a DMA allocation request fails with
2352                  * DDI_DMA_NOMAPPING, indicate the error by calling
2353                  * bioerror(9F) with bp and an error code of EFAULT.
2354                  * If a DMA allocation request fails with
2355                  * DDI_DMA_TOOBIG, indicate the error by calling
2356                  * bioerror(9F) with bp and an error code of EINVAL.
2357                  * For DDI_DMA_NORESOURCES, we may have some of them allocated.
2358                  * Request may be repeated later - there is no real error.
2359                  */
2360                 switch (rval) {
2361                 case DDI_DMA_NORESOURCES:
2362                         bioerror(bp, 0);
2363                         break;
2364                 case DDI_DMA_NOMAPPING:
2365                 case DDI_DMA_BADATTR:
2366                         bioerror(bp, EFAULT);
2367                         break;
2368                 case DDI_DMA_TOOBIG:
2369                 default:
2370                         bioerror(bp, EINVAL);
2371                         break;
2372                 }
2373                 goto fail;
2374         }
2375 
2376         if (sata_check_for_dma_error(dip, spx)) {
2377                 ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
2378                 bioerror(bp, EFAULT);
2379                 goto fail;
2380         }
2381 
2382 success:
2383         /* Set number of bytes that are not yet accounted for */
2384         pkt->pkt_resid = spx->txlt_total_residue;
2385         ASSERT(pkt->pkt_resid >= 0);
2386 
2387         return (pkt);
2388 
2389 fail:
2390         if (new_pkt == B_TRUE) {
2391                 /*
2392                  * Since this is a new packet, we can clean-up
2393                  * everything
2394                  */
2395                 sata_scsi_destroy_pkt(ap, pkt);
2396         } else {
2397                 /*
2398                  * This is a re-used packet. It will be target driver's
2399                  * responsibility to eventually destroy it (which
2400                  * will free allocated resources).
2401                  * Here, we just "complete" the request, leaving
2402                  * allocated resources intact, so the request may
2403                  * be retried.
2404                  */
2405                 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2406                 sata_pkt_free(spx);
2407         }
2408         return (NULL);
2409 }
2410 
2411 /*
2412  * Implementation of scsi tran_start.
2413  * Translate scsi cmd into sata operation and return status.
2414  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
2415  * are supported.
2416  * For SATA hard disks, supported scsi commands:
2417  * SCMD_INQUIRY
2418  * SCMD_TEST_UNIT_READY
2419  * SCMD_START_STOP
2420  * SCMD_READ_CAPACITY
2421  * SCMD_SVC_ACTION_IN_G4 (READ CAPACITY (16))
2422  * SCMD_REQUEST_SENSE
2423  * SCMD_LOG_SENSE_G1
2424  * SCMD_LOG_SELECT_G1
2425  * SCMD_MODE_SENSE      (specific pages)
2426  * SCMD_MODE_SENSE_G1   (specific pages)
2427  * SCMD_MODE_SELECT     (specific pages)
2428  * SCMD_MODE_SELECT_G1  (specific pages)
2429  * SCMD_SYNCHRONIZE_CACHE
2430  * SCMD_SYNCHRONIZE_CACHE_G1
2431  * SCMD_READ
2432  * SCMD_READ_G1
2433  * SCMD_READ_G4
2434  * SCMD_READ_G5
2435  * SCMD_WRITE
2436  * SCMD_WRITE_BUFFER
2437  * SCMD_WRITE_G1
2438  * SCMD_WRITE_G4
2439  * SCMD_WRITE_G5
2440  * SCMD_SEEK            (noop)
2441  * SCMD_SDIAG
2442  *
2443  * All other commands are rejected as unsupported.
2444  *
2445  * Returns:
2446  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2447  * for execution. TRAN_ACCEPT may be returned also if device was removed but
2448  * a callback could be scheduled.
2449  * TRAN_BADPKT if cmd was directed to invalid address.
2450  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2451  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2452  * was removed and there was no callback specified in scsi pkt.
2453  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2454  * framework was busy performing some other operation(s).
2455  *
2456  */
2457 static int
2458 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2459 {
2460         sata_hba_inst_t *sata_hba_inst =
2461             (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2462         sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2463         sata_device_t *sdevice = &spx->txlt_sata_pkt->satapkt_device;
2464         sata_drive_info_t *sdinfo;
2465         struct buf *bp;
2466         uint8_t cport, pmport;
2467         boolean_t dev_gone = B_FALSE;
2468         int rval;
2469 
2470         SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2471             "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2472 
2473         ASSERT(spx != NULL &&
2474             spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2475 
2476         cport = SCSI_TO_SATA_CPORT(ap->a_target);
2477         pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2478 
2479         mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2480 
2481         if (sdevice->satadev_addr.qual == SATA_ADDR_DCPORT) {
2482                 sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2483                 if (sdinfo == NULL ||
2484                     SATA_CPORT_INFO(sata_hba_inst, cport)->
2485                     cport_tgtnode_clean == B_FALSE ||
2486                     (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2487                         dev_gone = B_TRUE;
2488                 }
2489         } else if (sdevice->satadev_addr.qual == SATA_ADDR_DPMPORT) {
2490                 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
2491                     SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
2492                     cport) == NULL) {
2493                         dev_gone = B_TRUE;
2494                 } else if (SATA_PMPORT_INFO(sata_hba_inst, cport,
2495                     pmport) == NULL) {
2496                         dev_gone = B_TRUE;
2497                 } else {
2498                         mutex_enter(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2499                             cport, pmport)));
2500                         sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2501                         if (sdinfo == NULL ||
2502                             SATA_PMPORT_INFO(sata_hba_inst, cport, pmport)->
2503                             pmport_tgtnode_clean == B_FALSE ||
2504                             (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2505                                 dev_gone = B_TRUE;
2506                         }
2507                         mutex_exit(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2508                             cport, pmport)));
2509                 }
2510         }
2511 
2512         if (dev_gone == B_TRUE) {
2513                 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2514                 pkt->pkt_reason = CMD_DEV_GONE;
2515                 /*
2516                  * The sd target driver is checking CMD_DEV_GONE pkt_reason
2517                  * only in callback function (for normal requests) and
2518                  * in the dump code path.
2519                  * So, if the callback is available, we need to do
2520                  * the callback rather than returning TRAN_FATAL_ERROR here.
2521                  */
2522                 if (pkt->pkt_comp != NULL) {
2523                         /* scsi callback required */
2524                         if (servicing_interrupt()) {
2525                                 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2526                                     (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2527                                     (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
2528                                     NULL) {
2529                                         return (TRAN_BUSY);
2530                                 }
2531                         } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2532                             (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2533                             (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
2534                                 /* Scheduling the callback failed */
2535                                 return (TRAN_BUSY);
2536                         }
2537                         return (TRAN_ACCEPT);
2538                 }
2539                 /* No callback available */
2540                 return (TRAN_FATAL_ERROR);
2541         }
2542 
2543         if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
2544                 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2545                 rval = sata_txlt_atapi(spx);
2546                 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2547                     "sata_scsi_start atapi: rval %d\n", rval);
2548                 return (rval);
2549         }
2550         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2551 
2552         /*
2553          * Checking for power state, if it was on
2554          * STOPPED state, then the drive is not capable
2555          * of processing media access command.  And
2556          * TEST_UNIT_READY, REQUEST_SENSE has special handling
2557          * in the function for different power state.
2558          */
2559         if (((sdinfo->satadrv_power_level == SATA_POWER_STANDBY) ||
2560             (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)) &&
2561             (SATA_IS_MEDIUM_ACCESS_CMD(pkt->pkt_cdbp[0]))) {
2562                 return (sata_txlt_check_condition(spx, KEY_NOT_READY,
2563                     SD_SCSI_ASC_LU_NOT_READY));
2564         }
2565 
2566         /* ATA Disk commands processing starts here */
2567 
2568         bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2569 
2570         switch (pkt->pkt_cdbp[0]) {
2571 
2572         case SCMD_INQUIRY:
2573                 /* Mapped to identify device */
2574                 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2575                         bp_mapin(bp);
2576                 rval = sata_txlt_inquiry(spx);
2577                 break;
2578 
2579         case SCMD_TEST_UNIT_READY:
2580                 /*
2581                  * SAT "SATA to ATA Translation" doc specifies translation
2582                  * to ATA CHECK POWER MODE.
2583                  */
2584                 rval = sata_txlt_test_unit_ready(spx);
2585                 break;
2586 
2587         case SCMD_START_STOP:
2588                 /* Mapping depends on the command */
2589                 rval = sata_txlt_start_stop_unit(spx);
2590                 break;
2591 
2592         case SCMD_READ_CAPACITY:
2593                 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2594                         bp_mapin(bp);
2595                 rval = sata_txlt_read_capacity(spx);
2596                 break;
2597 
2598         case SCMD_SVC_ACTION_IN_G4:             /* READ CAPACITY (16) */
2599                 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2600                         bp_mapin(bp);
2601                 rval = sata_txlt_read_capacity16(spx);
2602                 break;
2603 
2604         case SCMD_REQUEST_SENSE:
2605                 /*
2606                  * Always No Sense, since we force ARQ
2607                  */
2608                 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2609                         bp_mapin(bp);
2610                 rval = sata_txlt_request_sense(spx);
2611                 break;
2612 
2613         case SCMD_LOG_SENSE_G1:
2614                 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2615                         bp_mapin(bp);
2616                 rval = sata_txlt_log_sense(spx);
2617                 break;
2618 
2619         case SCMD_LOG_SELECT_G1:
2620                 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2621                         bp_mapin(bp);
2622                 rval = sata_txlt_log_select(spx);
2623                 break;
2624 
2625         case SCMD_MODE_SENSE:
2626         case SCMD_MODE_SENSE_G1:
2627                 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2628                         bp_mapin(bp);
2629                 rval = sata_txlt_mode_sense(spx);
2630                 break;
2631 
2632 
2633         case SCMD_MODE_SELECT:
2634         case SCMD_MODE_SELECT_G1:
2635                 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2636                         bp_mapin(bp);
2637                 rval = sata_txlt_mode_select(spx);
2638                 break;
2639 
2640         case SCMD_SYNCHRONIZE_CACHE:
2641         case SCMD_SYNCHRONIZE_CACHE_G1:
2642                 rval = sata_txlt_synchronize_cache(spx);
2643                 break;
2644 
2645         case SCMD_READ:
2646         case SCMD_READ_G1:
2647         case SCMD_READ_G4:
2648         case SCMD_READ_G5:
2649                 rval = sata_txlt_read(spx);
2650                 break;
2651         case SCMD_WRITE_BUFFER:
2652                 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2653                         bp_mapin(bp);
2654                 rval = sata_txlt_write_buffer(spx);
2655                 break;
2656 
2657         case SCMD_WRITE:
2658         case SCMD_WRITE_G1:
2659         case SCMD_WRITE_G4:
2660         case SCMD_WRITE_G5:
2661                 rval = sata_txlt_write(spx);
2662                 break;
2663 
2664         case SCMD_SEEK:
2665                 rval = sata_txlt_nodata_cmd_immediate(spx);
2666                 break;
2667 
2668         case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
2669         case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
2670                 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2671                         bp_mapin(bp);
2672                 rval = sata_txlt_ata_pass_thru(spx);
2673                 break;
2674 
2675                 /* Other cases will be filed later */
2676                 /* postponed until phase 2 of the development */
2677         case SPC3_CMD_UNMAP:
2678                 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2679                         bp_mapin(bp);
2680                 rval = sata_txlt_unmap(spx);
2681                 break;
2682         default:
2683                 rval = sata_txlt_invalid_command(spx);
2684                 break;
2685         }
2686 
2687         SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2688             "sata_scsi_start: rval %d\n", rval);
2689 
2690         return (rval);
2691 }
2692 
2693 /*
2694  * Implementation of scsi tran_abort.
2695  * Abort specific pkt or all packets.
2696  *
2697  * Returns 1 if one or more packets were aborted, returns 0 otherwise
2698  *
2699  * May be called from an interrupt level.
2700  */
2701 static int
2702 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2703 {
2704         sata_hba_inst_t *sata_hba_inst =
2705             (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2706         sata_device_t   sata_device;
2707         sata_pkt_t      *sata_pkt;
2708 
2709         SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2710             "sata_scsi_abort: %s at target: 0x%x\n",
2711             scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2712 
2713         /* Validate address */
2714         if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2715                 /* Invalid address */
2716                 return (0);
2717 
2718         mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2719             sata_device.satadev_addr.cport)));
2720         if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2721                 /* invalid address */
2722                 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2723                     sata_device.satadev_addr.cport)));
2724                 return (0);
2725         }
2726         if (scsi_pkt == NULL) {
2727                 /*
2728                  * Abort all packets.
2729                  * Although we do not have specific packet, we still need
2730                  * dummy packet structure to pass device address to HBA.
2731                  * Allocate one, without sleeping. Fail if pkt cannot be
2732                  * allocated.
2733                  */
2734                 sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2735                 if (sata_pkt == NULL) {
2736                         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2737                             sata_device.satadev_addr.cport)));
2738                         SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2739                             "could not allocate sata_pkt"));
2740                         return (0);
2741                 }
2742                 sata_pkt->satapkt_rev = SATA_PKT_REV;
2743                 sata_pkt->satapkt_device = sata_device;
2744                 sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2745         } else {
2746                 if (scsi_pkt->pkt_ha_private == NULL) {
2747                         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2748                             sata_device.satadev_addr.cport)));
2749                         return (0); /* Bad scsi pkt */
2750                 }
2751                 /* extract pointer to sata pkt */
2752                 sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2753                     txlt_sata_pkt;
2754         }
2755 
2756         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2757             sata_device.satadev_addr.cport)));
2758         /* Send abort request to HBA */
2759         if ((*SATA_ABORT_FUNC(sata_hba_inst))
2760             (SATA_DIP(sata_hba_inst), sata_pkt,
2761             scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2762             SATA_SUCCESS) {
2763                 if (scsi_pkt == NULL)
2764                         kmem_free(sata_pkt, sizeof (sata_pkt_t));
2765                 /* Success */
2766                 return (1);
2767         }
2768         /* Else, something did not go right */
2769         if (scsi_pkt == NULL)
2770                 kmem_free(sata_pkt, sizeof (sata_pkt_t));
2771         /* Failure */
2772         return (0);
2773 }
2774 
2775 
2776 /*
2777  * Implementation of scsi tran_reset.
2778  * RESET_ALL request is translated into port reset.
2779  * RESET_TARGET requests is translated into a device reset,
2780  * RESET_LUN request is accepted only for LUN 0 and translated into
2781  * device reset.
2782  * The target reset should cause all HBA active and queued packets to
2783  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2784  * the return. HBA should report reset event for the device.
2785  *
2786  * Returns 1 upon success, 0 upon failure.
2787  */
2788 static int
2789 sata_scsi_reset(struct scsi_address *ap, int level)
2790 {
2791         sata_hba_inst_t *sata_hba_inst =
2792             (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2793         sata_device_t   sata_device;
2794         int             val;
2795 
2796         SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2797             "sata_scsi_reset: level %d target: 0x%x\n",
2798             level, ap->a_target);
2799 
2800         /* Validate address */
2801         val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2802         if (val == -1)
2803                 /* Invalid address */
2804                 return (0);
2805 
2806         mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2807             sata_device.satadev_addr.cport)));
2808         if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2809                 /* invalid address */
2810                 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2811                     sata_device.satadev_addr.cport)));
2812                 return (0);
2813         }
2814         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2815             sata_device.satadev_addr.cport)));
2816         if (level == RESET_ALL) {
2817                 /* port reset */
2818                 if (sata_device.satadev_addr.qual == SATA_ADDR_DCPORT)
2819                         sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2820                 else
2821                         sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
2822 
2823                 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2824                     (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2825                         return (1);
2826                 else
2827                         return (0);
2828 
2829         } else if (val == 0 &&
2830             (level == RESET_TARGET || level == RESET_LUN)) {
2831                 /* reset device (device attached) */
2832                 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2833                     (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2834                         return (1);
2835                 else
2836                         return (0);
2837         }
2838         return (0);
2839 }
2840 
2841 
2842 /*
2843  * Implementation of scsi tran_getcap (get transport/device capabilities).
2844  * Supported capabilities for SATA hard disks:
2845  * auto-rqsense         (always supported)
2846  * tagged-qing          (supported if HBA supports it)
2847  * untagged-qing        (could be supported if disk supports it, but because
2848  *                       caching behavior allowing untagged queuing actually
2849  *                       results in reduced performance.  sd tries to throttle
2850  *                       back to only 3 outstanding commands, which may
2851  *                       work for real SCSI disks, but with read ahead
2852  *                       caching, having more than 1 outstanding command
2853  *                       results in cache thrashing.)
2854  * sector_size
2855  * dma_max
2856  * interconnect-type    (INTERCONNECT_SATA)
2857  *
2858  * Supported capabilities for ATAPI CD/DVD devices:
2859  * auto-rqsense         (always supported)
2860  * sector_size
2861  * dma_max
2862  * max-cdb-length
2863  * interconnect-type    (INTERCONNECT_SATA)
2864  *
2865  * Supported capabilities for ATAPI TAPE devices:
2866  * auto-rqsense         (always supported)
2867  * dma_max
2868  * max-cdb-length
2869  *
2870  * Supported capabilities for SATA ATAPI hard disks:
2871  * auto-rqsense         (always supported)
2872  * interconnect-type    (INTERCONNECT_SATA)
2873  * max-cdb-length
2874  *
2875  * Request for other capabilities is rejected as unsupported.
2876  *
2877  * Returns supported capability value, or -1 if capability is unsuppported or
2878  * the address is invalid - no device.
2879  */
2880 
2881 static int
2882 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2883 {
2884 
2885         sata_hba_inst_t         *sata_hba_inst =
2886             (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2887         sata_device_t           sata_device;
2888         sata_drive_info_t       *sdinfo;
2889         ddi_dma_attr_t          adj_dma_attr;
2890         int                     rval;
2891 
2892         SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2893             "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2894             ap->a_target, cap);
2895 
2896         /*
2897          * We want to process the capabilities on per port granularity.
2898          * So, we are specifically restricting ourselves to whom != 0
2899          * to exclude the controller wide handling.
2900          */
2901         if (cap == NULL || whom == 0)
2902                 return (-1);
2903 
2904         if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2905                 /* Invalid address */
2906                 return (-1);
2907         }
2908         mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2909             sata_device.satadev_addr.cport)));
2910         if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2911             NULL) {
2912                 /* invalid address */
2913                 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2914                     sata_device.satadev_addr.cport)));
2915                 return (-1);
2916         }
2917 
2918         switch (scsi_hba_lookup_capstr(cap)) {
2919         case SCSI_CAP_ARQ:
2920                 rval = 1;               /* ARQ supported, turned on */
2921                 break;
2922 
2923         case SCSI_CAP_SECTOR_SIZE:
2924                 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2925                         rval = SATA_DISK_SECTOR_SIZE;   /* fixed size */
2926                 else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2927                         rval = SATA_ATAPI_SECTOR_SIZE;
2928                 else rval = -1;
2929                 break;
2930 
2931         /*
2932          * untagged queuing cause a performance inversion because of
2933          * the way sd operates.  Because of this reason we do not
2934          * use it when available.
2935          */
2936         case SCSI_CAP_UNTAGGED_QING:
2937                 if (sdinfo->satadrv_features_enabled &
2938                     SATA_DEV_F_E_UNTAGGED_QING)
2939                         rval = 1;       /* Untagged queuing available */
2940                 else
2941                         rval = -1;      /* Untagged queuing not available */
2942                 break;
2943 
2944         case SCSI_CAP_TAGGED_QING:
2945                 if ((sdinfo->satadrv_features_enabled &
2946                     SATA_DEV_F_E_TAGGED_QING) &&
2947                     (sdinfo->satadrv_max_queue_depth > 1))
2948                         rval = 1;       /* Tagged queuing available */
2949                 else
2950                         rval = -1;      /* Tagged queuing not available */
2951                 break;
2952 
2953         case SCSI_CAP_DMA_MAX:
2954                 sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2955                     &adj_dma_attr);
2956                 rval = (int)adj_dma_attr.dma_attr_maxxfer;
2957                 /* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2958                 break;
2959 
2960         case SCSI_CAP_INTERCONNECT_TYPE:
2961                 rval = INTERCONNECT_SATA;       /* SATA interconnect type */
2962                 break;
2963 
2964         case SCSI_CAP_CDB_LEN:
2965                 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI)
2966                         rval = sdinfo->satadrv_atapi_cdb_len;
2967                 else
2968                         rval = -1;
2969                 break;
2970 
2971         default:
2972                 rval = -1;
2973                 break;
2974         }
2975         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2976             sata_device.satadev_addr.cport)));
2977         return (rval);
2978 }
2979 
2980 /*
2981  * Implementation of scsi tran_setcap
2982  *
2983  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2984  *
2985  */
2986 static int
2987 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2988 {
2989         sata_hba_inst_t *sata_hba_inst =
2990             (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2991         sata_device_t   sata_device;
2992         sata_drive_info_t       *sdinfo;
2993         int             rval;
2994 
2995         SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2996             "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
2997 
2998         /*
2999          * We want to process the capabilities on per port granularity.
3000          * So, we are specifically restricting ourselves to whom != 0
3001          * to exclude the controller wide handling.
3002          */
3003         if (cap == NULL || whom == 0) {
3004                 return (-1);
3005         }
3006 
3007         if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
3008                 /* Invalid address */
3009                 return (-1);
3010         }
3011         mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
3012             sata_device.satadev_addr.cport)));
3013         if ((sdinfo = sata_get_device_info(sata_hba_inst,
3014             &sata_device)) == NULL) {
3015                 /* invalid address */
3016                 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3017                     sata_device.satadev_addr.cport)));
3018                 return (-1);
3019         }
3020         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3021             sata_device.satadev_addr.cport)));
3022 
3023         switch (scsi_hba_lookup_capstr(cap)) {
3024         case SCSI_CAP_ARQ:
3025         case SCSI_CAP_SECTOR_SIZE:
3026         case SCSI_CAP_DMA_MAX:
3027         case SCSI_CAP_INTERCONNECT_TYPE:
3028                 rval = 0;
3029                 break;
3030         case SCSI_CAP_UNTAGGED_QING:
3031                 if (SATA_QDEPTH(sata_hba_inst) > 1) {
3032                         rval = 1;
3033                         if (value == 1) {
3034                                 sdinfo->satadrv_features_enabled |=
3035                                     SATA_DEV_F_E_UNTAGGED_QING;
3036                         } else if (value == 0) {
3037                                 sdinfo->satadrv_features_enabled &=
3038                                     ~SATA_DEV_F_E_UNTAGGED_QING;
3039                         } else {
3040                                 rval = -1;
3041                         }
3042                 } else {
3043                         rval = 0;
3044                 }
3045                 break;
3046         case SCSI_CAP_TAGGED_QING:
3047                 /* This can TCQ or NCQ */
3048                 if (sata_func_enable & SATA_ENABLE_QUEUING &&
3049                     ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
3050                     SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
3051                     (sata_func_enable & SATA_ENABLE_NCQ &&
3052                     sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
3053                     SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
3054                     (sdinfo->satadrv_max_queue_depth > 1)) {
3055                         rval = 1;
3056                         if (value == 1) {
3057                                 sdinfo->satadrv_features_enabled |=
3058                                     SATA_DEV_F_E_TAGGED_QING;
3059                         } else if (value == 0) {
3060                                 sdinfo->satadrv_features_enabled &=
3061                                     ~SATA_DEV_F_E_TAGGED_QING;
3062                         } else {
3063                                 rval = -1;
3064                         }
3065                 } else {
3066                         rval = 0;
3067                 }
3068                 break;
3069         default:
3070                 rval = -1;
3071                 break;
3072         }
3073         return (rval);
3074 }
3075 
3076 /*
3077  * Implementations of scsi tran_destroy_pkt.
3078  * Free resources allocated by sata_scsi_init_pkt()
3079  */
3080 static void
3081 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3082 {
3083         sata_pkt_txlate_t *spx;
3084 
3085         spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3086 
3087         sata_common_free_dma_rsrcs(spx);
3088 
3089         spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
3090         sata_pkt_free(spx);
3091 
3092         scsi_hba_pkt_free(ap, pkt);
3093 }
3094 
3095 /*
3096  * Implementation of scsi tran_dmafree.
3097  * Free DMA resources allocated by sata_scsi_init_pkt()
3098  */
3099 
3100 static void
3101 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
3102 {
3103 #ifndef __lock_lint
3104         _NOTE(ARGUNUSED(ap))
3105 #endif
3106         sata_pkt_txlate_t *spx;
3107 
3108         ASSERT(pkt != NULL);
3109         spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3110 
3111         sata_common_free_dma_rsrcs(spx);
3112 }
3113 
3114 /*
3115  * Implementation of scsi tran_sync_pkt.
3116  *
3117  * The assumption below is that pkt is unique - there is no need to check ap
3118  *
3119  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
3120  * into/from the real buffer.
3121  */
3122 static void
3123 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3124 {
3125 #ifndef __lock_lint
3126         _NOTE(ARGUNUSED(ap))
3127 #endif
3128         int rval;
3129         sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3130         struct buf *bp;
3131         int direction;
3132 
3133         ASSERT(spx != NULL);
3134         if (spx->txlt_buf_dma_handle != NULL) {
3135                 direction = spx->txlt_sata_pkt->
3136                     satapkt_cmd.satacmd_flags.sata_data_direction;
3137                 if (spx->txlt_sata_pkt != NULL &&
3138                     direction != SATA_DIR_NODATA_XFER) {
3139                         if (spx->txlt_tmp_buf != NULL) {
3140                                 /* Intermediate DMA buffer used */
3141                                 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3142 
3143                                 if (direction & SATA_DIR_WRITE) {
3144                                         bcopy(bp->b_un.b_addr,
3145                                             spx->txlt_tmp_buf, bp->b_bcount);
3146                                 }
3147                         }
3148                         /* Sync the buffer for device or for CPU */
3149                         rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
3150                             (direction & SATA_DIR_WRITE) ?
3151                             DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
3152                         ASSERT(rval == DDI_SUCCESS);
3153                         if (spx->txlt_tmp_buf != NULL &&
3154                             !(direction & SATA_DIR_WRITE)) {
3155                                 /* Intermediate DMA buffer used for read */
3156                                 bcopy(spx->txlt_tmp_buf,
3157                                     bp->b_un.b_addr, bp->b_bcount);
3158                         }
3159 
3160                 }
3161         }
3162 }
3163 
3164 
3165 
3166 /* *******************  SATA - SCSI Translation functions **************** */
3167 /*
3168  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
3169  * translation.
3170  */
3171 
3172 /*
3173  * Checks if a device exists and can be access and translates common
3174  * scsi_pkt data to sata_pkt data.
3175  *
3176  * Flag argument indicates that a non-read/write ATA command may be sent
3177  * to HBA in arbitrary SYNC mode to execute this packet.
3178  *
3179  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
3180  * sata_pkt was set-up.
3181  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
3182  * exist and pkt_comp callback was scheduled.
3183  * Returns other TRAN_XXXXX values when error occured and command should be
3184  * rejected with the returned TRAN_XXXXX value.
3185  *
3186  * This function should be called with port mutex held.
3187  */
3188 static int
3189 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason, int flag)
3190 {
3191         sata_drive_info_t *sdinfo;
3192         sata_device_t sata_device;
3193         const struct sata_cmd_flags sata_initial_cmd_flags = {
3194                 SATA_DIR_NODATA_XFER,
3195                 /* all other values to 0/FALSE */
3196         };
3197         /*
3198          * Pkt_reason has to be set if the pkt_comp callback is invoked,
3199          * and that implies TRAN_ACCEPT return value. Any other returned value
3200          * indicates that the scsi packet was not accepted (the reason will not
3201          * be checked by the scsi target driver).
3202          * To make debugging easier, we set pkt_reason to know value here.
3203          * It may be changed later when different completion reason is
3204          * determined.
3205          */
3206         spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
3207         *reason = CMD_TRAN_ERR;
3208 
3209         /* Validate address */
3210         switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
3211             &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
3212 
3213         case -1:
3214                 /* Invalid address or invalid device type */
3215                 return (TRAN_BADPKT);
3216         case 2:
3217                 /*
3218                  * Valid address but device type is unknown - Chack if it is
3219                  * in the reset state and therefore in an indeterminate state.
3220                  */
3221                 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3222                     &spx->txlt_sata_pkt->satapkt_device);
3223                 if (sdinfo != NULL && (sdinfo->satadrv_event_flags &
3224                     (SATA_EVNT_DEVICE_RESET |
3225                     SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3226                         if (!ddi_in_panic()) {
3227                                 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3228                                 *reason = CMD_INCOMPLETE;
3229                                 SATADBG1(SATA_DBG_SCSI_IF,
3230                                     spx->txlt_sata_hba_inst,
3231                                     "sata_scsi_start: rejecting command "
3232                                     "because of device reset state\n", NULL);
3233                                 return (TRAN_BUSY);
3234                         }
3235                 }
3236                 /* FALLTHROUGH */
3237         case 1:
3238                 /* valid address but no valid device - it has disappeared */
3239                 spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
3240                 *reason = CMD_DEV_GONE;
3241                 /*
3242                  * The sd target driver is checking CMD_DEV_GONE pkt_reason
3243                  * only in callback function (for normal requests) and
3244                  * in the dump code path.
3245                  * So, if the callback is available, we need to do
3246                  * the callback rather than returning TRAN_FATAL_ERROR here.
3247                  */
3248                 if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
3249                         /* scsi callback required */
3250                         if (servicing_interrupt()) {
3251                                 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3252                                     (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3253                                     (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3254                                     NULL) {
3255                                         return (TRAN_BUSY);
3256                                 }
3257                         } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3258                             (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3259                             (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3260                                 /* Scheduling the callback failed */
3261                                 return (TRAN_BUSY);
3262                         }
3263 
3264                         return (TRAN_ACCEPT);
3265                 }
3266                 return (TRAN_FATAL_ERROR);
3267         default:
3268                 /* all OK; pkt reason will be overwritten later */
3269                 break;
3270         }
3271         /*
3272          * If pkt is to be executed in polling mode and a command will not be
3273          * emulated in SATA module (requires sending a non-read/write ATA
3274          * command to HBA driver in arbitrary SYNC mode) and we are in the
3275          * interrupt context and not in the panic dump, then reject the packet
3276          * to avoid a possible interrupt stack overrun or hang caused by
3277          * a potentially blocked interrupt.
3278          */
3279         if (((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0 || flag != 0) &&
3280             servicing_interrupt() && !ddi_in_panic()) {
3281                 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
3282                     "sata_scsi_start: rejecting synchronous command because "
3283                     "of interrupt context\n", NULL);
3284                 return (TRAN_BUSY);
3285         }
3286 
3287         sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3288             &spx->txlt_sata_pkt->satapkt_device);
3289 
3290         /*
3291          * If device is in reset condition, reject the packet with
3292          * TRAN_BUSY, unless:
3293          * 1. system is panicking (dumping)
3294          * In such case only one thread is running and there is no way to
3295          * process reset.
3296          * 2. cfgadm operation is is progress (internal APCTL lock is set)
3297          * Some cfgadm operations involve drive commands, so reset condition
3298          * needs to be ignored for IOCTL operations.
3299          */
3300         if ((sdinfo->satadrv_event_flags &
3301             (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3302 
3303                 if (!ddi_in_panic() &&
3304                     ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
3305                     sata_device.satadev_addr.cport) &
3306                     SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
3307                         spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3308                         *reason = CMD_INCOMPLETE;
3309                         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3310                             "sata_scsi_start: rejecting command because "
3311                             "of device reset state\n", NULL);
3312                         return (TRAN_BUSY);
3313                 }
3314         }
3315 
3316         /*
3317          * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
3318          * sata_scsi_pkt_init() because pkt init had to work also with
3319          * non-existing devices.
3320          * Now we know that the packet was set-up for a real device, so its
3321          * type is known.
3322          */
3323         spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
3324 
3325         spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
3326         if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
3327             sata_device.satadev_addr.cport)->cport_event_flags &
3328             SATA_APCTL_LOCK_PORT_BUSY) != 0) {
3329                 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3330                     sata_ignore_dev_reset = B_TRUE;
3331         }
3332         /*
3333          * At this point the generic translation routine determined that the
3334          * scsi packet should be accepted. Packet completion reason may be
3335          * changed later when a different completion reason is determined.
3336          */
3337         spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3338         *reason = CMD_CMPLT;
3339 
3340         if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
3341                 /* Synchronous execution */
3342                 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
3343                     SATA_OPMODE_POLLING;
3344                 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3345                     sata_ignore_dev_reset = ddi_in_panic();
3346         } else {
3347                 /* Asynchronous execution */
3348                 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
3349                     SATA_OPMODE_INTERRUPTS;
3350         }
3351         /* Convert queuing information */
3352         if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
3353                 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
3354                     B_TRUE;
3355         else if (spx->txlt_scsi_pkt->pkt_flags &
3356             (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
3357                 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
3358                     B_TRUE;
3359 
3360         /* Always limit pkt time */
3361         if (spx->txlt_scsi_pkt->pkt_time == 0)
3362                 spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
3363         else
3364                 /* Pass on scsi_pkt time */
3365                 spx->txlt_sata_pkt->satapkt_time =
3366                     spx->txlt_scsi_pkt->pkt_time;
3367 
3368         return (TRAN_ACCEPT);
3369 }
3370 
3371 
3372 /*
3373  * Translate ATA Identify Device data to SCSI Inquiry data.
3374  * This function may be called only for ATA devices.
3375  * This function should not be called for ATAPI devices - they
3376  * respond directly to SCSI Inquiry command.
3377  *
3378  * SATA Identify Device data has to be valid in sata_drive_info.
3379  * Buffer has to accomodate the inquiry length (36 bytes).
3380  *
3381  * This function should be called with a port mutex held.
3382  */
3383 static  void
3384 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
3385     sata_drive_info_t *sdinfo, uint8_t *buf)
3386 {
3387 
3388         struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
3389         struct sata_id *sid = &sdinfo->satadrv_id;
3390 
3391         /* Start with a nice clean slate */
3392         bzero((void *)inq, sizeof (struct scsi_inquiry));
3393 
3394         /*
3395          * Rely on the dev_type for setting paripheral qualifier.
3396          * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
3397          * It could be that DTYPE_OPTICAL could also qualify in the future.
3398          * ATAPI Inquiry may provide more data to the target driver.
3399          */
3400         inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3401             DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
3402 
3403         /* CFA type device is not a removable media device */
3404         inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) &&
3405             (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0;
3406         inq->inq_qual = 0;   /* Device type qualifier (obsolete in SCSI3? */
3407         inq->inq_iso = 0;    /* ISO version */
3408         inq->inq_ecma = 0;   /* ECMA version */
3409         inq->inq_ansi = 3;   /* ANSI version - SCSI 3 */
3410         inq->inq_aenc = 0;   /* Async event notification cap. */
3411         inq->inq_trmiop = 0; /* Supports TERMINATE I/O PROC msg - NO */
3412         inq->inq_normaca = 0;        /* setting NACA bit supported - NO */
3413         inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
3414         inq->inq_len = 31;   /* Additional length */
3415         inq->inq_dualp = 0;  /* dual port device - NO */
3416         inq->inq_reladdr = 0;        /* Supports relative addressing - NO */
3417         inq->inq_sync = 0;   /* Supports synchronous data xfers - NO */
3418         inq->inq_linked = 0; /* Supports linked commands - NO */
3419                                 /*
3420                                  * Queuing support - controller has to
3421                                  * support some sort of command queuing.
3422                                  */
3423         if (SATA_QDEPTH(sata_hba_inst) > 1)
3424                 inq->inq_cmdque = 1; /* Supports command queueing - YES */
3425         else
3426                 inq->inq_cmdque = 0; /* Supports command queueing - NO */
3427         inq->inq_sftre = 0;  /* Supports Soft Reset option - NO ??? */
3428         inq->inq_wbus32 = 0; /* Supports 32 bit wide data xfers - NO */
3429         inq->inq_wbus16 = 0; /* Supports 16 bit wide data xfers - NO */
3430 
3431 #ifdef  _LITTLE_ENDIAN
3432         /* Swap text fields to match SCSI format */
3433         bcopy("ATA     ", inq->inq_vid, 8);          /* Vendor ID */
3434         swab(sid->ai_model, inq->inq_pid, 16);            /* Product ID */
3435         if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3436                 swab(sid->ai_fw, inq->inq_revision, 4);   /* Revision level */
3437         else
3438                 swab(&sid->ai_fw[4], inq->inq_revision, 4);   /* Rev. level */
3439 #else   /* _LITTLE_ENDIAN */
3440         bcopy("ATA     ", inq->inq_vid, 8);          /* Vendor ID */
3441         bcopy(sid->ai_model, inq->inq_pid, 16);           /* Product ID */
3442         if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3443                 bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
3444         else
3445                 bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
3446 #endif  /* _LITTLE_ENDIAN */
3447 }
3448 
3449 
3450 /*
3451  * Scsi response set up for invalid command (command not supported)
3452  *
3453  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3454  */
3455 static int
3456 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
3457 {
3458         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3459         struct scsi_extended_sense *sense;
3460 
3461         scsipkt->pkt_reason = CMD_CMPLT;
3462         scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3463             STATE_SENT_CMD | STATE_GOT_STATUS;
3464 
3465         *scsipkt->pkt_scbp = STATUS_CHECK;
3466 
3467         sense = sata_arq_sense(spx);
3468         sense->es_key = KEY_ILLEGAL_REQUEST;
3469         sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
3470 
3471         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3472             "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3473 
3474         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3475             scsipkt->pkt_comp != NULL) {
3476                 /* scsi callback required */
3477                 if (servicing_interrupt()) {
3478                         if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3479                             (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3480                             (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3481                                 return (TRAN_BUSY);
3482                         }
3483                 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3484                     (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3485                     (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3486                         /* Scheduling the callback failed */
3487                         return (TRAN_BUSY);
3488                 }
3489         }
3490         return (TRAN_ACCEPT);
3491 }
3492 
3493 /*
3494  * Scsi response set up for check condition with special sense key
3495  * and additional sense code.
3496  *
3497  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3498  */
3499 static int
3500 sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code)
3501 {
3502         sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3503         int cport = SATA_TXLT_CPORT(spx);
3504         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3505         struct scsi_extended_sense *sense;
3506 
3507         mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3508         scsipkt->pkt_reason = CMD_CMPLT;
3509         scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3510             STATE_SENT_CMD | STATE_GOT_STATUS;
3511 
3512         *scsipkt->pkt_scbp = STATUS_CHECK;
3513 
3514         sense = sata_arq_sense(spx);
3515         sense->es_key = key;
3516         sense->es_add_code = code;
3517 
3518         mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3519 
3520         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3521             "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3522 
3523         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3524             scsipkt->pkt_comp != NULL) {
3525                 /* scsi callback required */
3526                 if (servicing_interrupt()) {
3527                         if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3528                             (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3529                             (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3530                                 return (TRAN_BUSY);
3531                         }
3532                 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3533                     (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3534                     (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3535                         /* Scheduling the callback failed */
3536                         return (TRAN_BUSY);
3537                 }
3538         }
3539         return (TRAN_ACCEPT);
3540 }
3541 
3542 /*
3543  * Scsi response setup for
3544  * emulated non-data command that requires no action/return data
3545  *
3546  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3547  */
3548 static  int
3549 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
3550 {
3551         int rval;
3552         int reason;
3553         kmutex_t *cport_mutex =  &(SATA_TXLT_CPORT_MUTEX(spx));
3554 
3555         mutex_enter(cport_mutex);
3556 
3557         if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3558             TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3559                 mutex_exit(cport_mutex);
3560                 return (rval);
3561         }
3562         mutex_exit(cport_mutex);
3563 
3564         spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3565             STATE_SENT_CMD | STATE_GOT_STATUS;
3566         spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3567         *(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
3568 
3569         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3570             "Scsi_pkt completion reason %x\n",
3571             spx->txlt_scsi_pkt->pkt_reason);
3572 
3573         if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3574             spx->txlt_scsi_pkt->pkt_comp != NULL) {
3575                 /* scsi callback required */
3576                 if (servicing_interrupt()) {
3577                         if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3578                             (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3579                             (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3580                                 return (TRAN_BUSY);
3581                         }
3582                 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3583                     (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3584                     (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3585                         /* Scheduling the callback failed */
3586                         return (TRAN_BUSY);
3587                 }
3588         }
3589         return (TRAN_ACCEPT);
3590 }
3591 
3592 
3593 /*
3594  * SATA translate command: Inquiry / Identify Device
3595  * Use cached Identify Device data for now, rather than issuing actual
3596  * Device Identify cmd request. If device is detached and re-attached,
3597  * asynchronous event processing should fetch and refresh Identify Device
3598  * data.
3599  * VPD pages supported now:
3600  * Vital Product Data page
3601  * Unit Serial Number page
3602  * Block Device Characteristics Page
3603  * ATA Information Page
3604  *
3605  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3606  */
3607 
3608 #define EVPD                    1       /* Extended Vital Product Data flag */
3609 #define CMDDT                   2       /* Command Support Data - Obsolete */
3610 #define INQUIRY_SUP_VPD_PAGE    0       /* Supported VPD Pages Page Code */
3611 #define INQUIRY_USN_PAGE        0x80    /* Unit Serial Number Page Code */
3612 #define INQUIRY_BDC_PAGE        0xB1    /* Block Device Characteristics Page */
3613                                         /* Code */
3614 #define INQUIRY_ATA_INFO_PAGE   0x89    /* ATA Information Page Code */
3615 #define INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3616 
3617 static int
3618 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3619 {
3620         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3621         struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3622         sata_drive_info_t *sdinfo;
3623         struct scsi_extended_sense *sense;
3624         int count;
3625         uint8_t *p;
3626         int i, j;
3627         uint8_t page_buf[1024]; /* Max length */
3628         int rval, reason;
3629         ushort_t rate;
3630         kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
3631 
3632         mutex_enter(cport_mutex);
3633 
3634         if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3635             TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3636                 mutex_exit(cport_mutex);
3637                 return (rval);
3638         }
3639 
3640         sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3641             &spx->txlt_sata_pkt->satapkt_device);
3642 
3643         ASSERT(sdinfo != NULL);
3644 
3645         scsipkt->pkt_reason = CMD_CMPLT;
3646         scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3647             STATE_SENT_CMD | STATE_GOT_STATUS;
3648 
3649         /* Reject not supported request */
3650         if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3651                 *scsipkt->pkt_scbp = STATUS_CHECK;
3652                 sense = sata_arq_sense(spx);
3653                 sense->es_key = KEY_ILLEGAL_REQUEST;
3654                 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3655                 goto done;
3656         }
3657 
3658         /* Valid Inquiry request */
3659         *scsipkt->pkt_scbp = STATUS_GOOD;
3660 
3661         if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3662 
3663                 /*
3664                  * Because it is fully emulated command storing data
3665                  * programatically in the specified buffer, release
3666                  * preallocated DMA resources before storing data in the buffer,
3667                  * so no unwanted DMA sync would take place.
3668                  */
3669                 sata_scsi_dmafree(NULL, scsipkt);
3670 
3671                 if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3672                         /* Standard Inquiry Data request */
3673                         struct scsi_inquiry inq;
3674                         unsigned int bufsize;
3675 
3676                         sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3677                             sdinfo, (uint8_t *)&inq);
3678                         /* Copy no more than requested */
3679                         count = MIN(bp->b_bcount,
3680                             sizeof (struct scsi_inquiry));
3681                         bufsize = scsipkt->pkt_cdbp[4];
3682                         bufsize |= scsipkt->pkt_cdbp[3] << 8;
3683                         count = MIN(count, bufsize);
3684                         bcopy(&inq, bp->b_un.b_addr, count);
3685 
3686                         scsipkt->pkt_state |= STATE_XFERRED_DATA;
3687                         scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3688                             bufsize - count : 0;
3689                 } else {
3690                         /*
3691                          * peripheral_qualifier = 0;
3692                          *
3693                          * We are dealing only with HD and will be
3694                          * dealing with CD/DVD devices soon
3695                          */
3696                         uint8_t peripheral_device_type =
3697                             sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3698                             DTYPE_DIRECT : DTYPE_RODIRECT;
3699 
3700                         bzero(page_buf, sizeof (page_buf));
3701 
3702                         switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3703                         case INQUIRY_SUP_VPD_PAGE:
3704                                 /*
3705                                  * Request for supported Vital Product Data
3706                                  * pages.
3707                                  */
3708                                 page_buf[0] = peripheral_device_type;
3709                                 page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3710                                 page_buf[2] = 0;
3711                                 page_buf[3] = 4; /* page length */
3712                                 page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3713                                 page_buf[5] = INQUIRY_USN_PAGE;
3714                                 page_buf[6] = INQUIRY_BDC_PAGE;
3715                                 page_buf[7] = INQUIRY_ATA_INFO_PAGE;
3716                                 /* Copy no more than requested */
3717                                 count = MIN(bp->b_bcount, 8);
3718                                 bcopy(page_buf, bp->b_un.b_addr, count);
3719                                 break;
3720 
3721                         case INQUIRY_USN_PAGE:
3722                                 /*
3723                                  * Request for Unit Serial Number page.
3724                                  * Set-up the page.
3725                                  */
3726                                 page_buf[0] = peripheral_device_type;
3727                                 page_buf[1] = INQUIRY_USN_PAGE;
3728                                 page_buf[2] = 0;
3729                                 /* remaining page length */
3730                                 page_buf[3] = SATA_ID_SERIAL_LEN;
3731 
3732                                 /*
3733                                  * Copy serial number from Identify Device data
3734                                  * words into the inquiry page and swap bytes
3735                                  * when necessary.
3736                                  */
3737                                 p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3738 #ifdef  _LITTLE_ENDIAN
3739                                 swab(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3740 #else
3741                                 bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3742 #endif
3743                                 /*
3744                                  * Least significant character of the serial
3745                                  * number shall appear as the last byte,
3746                                  * according to SBC-3 spec.
3747                                  * Count trailing spaces to determine the
3748                                  * necessary shift length.
3749                                  */
3750                                 p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1];
3751                                 for (j = 0; j < SATA_ID_SERIAL_LEN; j++) {
3752                                         if (*(p - j) != '\0' &&
3753                                             *(p - j) != '\040')
3754                                                 break;
3755                                 }
3756 
3757                                 /*
3758                                  * Shift SN string right, so that the last
3759                                  * non-blank character would appear in last
3760                                  * byte of SN field in the page.
3761                                  * 'j' is the shift length.
3762                                  */
3763                                 for (i = 0;
3764                                     i < (SATA_ID_SERIAL_LEN - j) && j != 0;
3765                                     i++, p--)
3766                                         *p = *(p - j);
3767 
3768                                 /*
3769                                  * Add leading spaces - same number as the
3770                                  * shift size
3771                                  */
3772                                 for (; j > 0; j--)
3773                                         page_buf[4 + j - 1] = '\040';
3774 
3775                                 count = MIN(bp->b_bcount,
3776                                     SATA_ID_SERIAL_LEN + 4);
3777                                 bcopy(page_buf, bp->b_un.b_addr, count);
3778                                 break;
3779 
3780                         case INQUIRY_BDC_PAGE:
3781                                 /*
3782                                  * Request for Block Device Characteristics
3783                                  * page.  Set-up the page.
3784                                  */
3785                                 page_buf[0] = peripheral_device_type;
3786                                 page_buf[1] = INQUIRY_BDC_PAGE;
3787                                 page_buf[2] = 0;
3788                                 /* remaining page length */
3789                                 page_buf[3] = SATA_ID_BDC_LEN;
3790 
3791                                 rate = sdinfo->satadrv_id.ai_medrotrate;
3792                                 page_buf[4] = (rate >> 8) & 0xff;
3793                                 page_buf[5] = rate & 0xff;
3794                                 page_buf[6] = 0;
3795                                 page_buf[7] = sdinfo->satadrv_id.
3796                                     ai_nomformfactor & 0xf;
3797 
3798                                 count = MIN(bp->b_bcount,
3799                                     SATA_ID_BDC_LEN + 4);
3800                                 bcopy(page_buf, bp->b_un.b_addr, count);
3801                                 break;
3802 
3803                         case INQUIRY_ATA_INFO_PAGE:
3804                                 /*
3805                                  * Request for ATA Information page.
3806                                  */
3807                                 page_buf[0] = peripheral_device_type;
3808                                 page_buf[1] = INQUIRY_ATA_INFO_PAGE;
3809                                 page_buf[2] = (SATA_ID_ATA_INFO_LEN >> 8) &
3810                                     0xff;
3811                                 page_buf[3] = SATA_ID_ATA_INFO_LEN & 0xff;
3812                                 /* page_buf[4-7] reserved */
3813 #ifdef  _LITTLE_ENDIAN
3814                                 bcopy("ATA     ", &page_buf[8], 8);
3815                                 swab(sdinfo->satadrv_id.ai_model,
3816                                     &page_buf[16], 16);
3817                                 if (strncmp(&sdinfo->satadrv_id.ai_fw[4],
3818                                     "    ", 4) == 0) {
3819                                         swab(sdinfo->satadrv_id.ai_fw,
3820                                             &page_buf[32], 4);
3821                                 } else {
3822                                         swab(&sdinfo->satadrv_id.ai_fw[4],
3823                                             &page_buf[32], 4);
3824                                 }
3825 #else   /* _LITTLE_ENDIAN */
3826                                 bcopy("ATA     ", &page_buf[8], 8);
3827                                 bcopy(sdinfo->satadrv_id.ai_model,
3828                                     &page_buf[16], 16);
3829                                 if (strncmp(&sdinfo->satadrv_id.ai_fw[4],
3830                                     "    ", 4) == 0) {
3831                                         bcopy(sdinfo->satadrv_id.ai_fw,
3832                                             &page_buf[32], 4);
3833                                 } else {
3834                                         bcopy(&sdinfo->satadrv_id.ai_fw[4],
3835                                             &page_buf[32], 4);
3836                                 }
3837 #endif  /* _LITTLE_ENDIAN */
3838                                 /*
3839                                  * page_buf[36-55] which defines the device
3840                                  * signature is not defined at this
3841                                  * time.
3842                                  */
3843 
3844                                 /* Set the command code */
3845                                 if (sdinfo->satadrv_type ==
3846                                     SATA_DTYPE_ATADISK) {
3847                                         page_buf[56] = SATAC_ID_DEVICE;
3848                                 } else if (sdinfo->satadrv_type ==
3849                                     SATA_DTYPE_ATAPI) {
3850                                         page_buf[56] = SATAC_ID_PACKET_DEVICE;
3851                                 }
3852                                 /*
3853                                  * If the command code, page_buf[56], is not
3854                                  * zero and if one of the identify commands
3855                                  * succeeds, return the identify data.
3856                                  */
3857                                 if ((page_buf[56] != 0) &&
3858                                     (sata_fetch_device_identify_data(
3859                                     spx->txlt_sata_hba_inst, sdinfo) ==
3860                                     SATA_SUCCESS)) {
3861                                         bcopy(&sdinfo->satadrv_id,
3862                                             &page_buf[60], sizeof (sata_id_t));
3863                                 }
3864 
3865                                 /* Need to copy out the page_buf to bp */
3866                                 count = MIN(bp->b_bcount,
3867                                     SATA_ID_ATA_INFO_LEN + 4);
3868                                 bcopy(page_buf, bp->b_un.b_addr, count);
3869                                 break;
3870 
3871                         case INQUIRY_DEV_IDENTIFICATION_PAGE:
3872                                 /*
3873                                  * We may want to implement this page, when
3874                                  * identifiers are common for SATA devices
3875                                  * But not now.
3876                                  */
3877                                 /*FALLTHROUGH*/
3878 
3879                         default:
3880                                 /* Request for unsupported VPD page */
3881                                 *scsipkt->pkt_scbp = STATUS_CHECK;
3882                                 sense = sata_arq_sense(spx);
3883                                 sense->es_key = KEY_ILLEGAL_REQUEST;
3884                                 sense->es_add_code =
3885                                     SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3886                                 goto done;
3887                         }
3888                 }
3889                 scsipkt->pkt_state |= STATE_XFERRED_DATA;
3890                 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3891                     scsipkt->pkt_cdbp[4] - count : 0;
3892         }
3893 done:
3894         mutex_exit(cport_mutex);
3895 
3896         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3897             "Scsi_pkt completion reason %x\n",
3898             scsipkt->pkt_reason);
3899 
3900         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3901             scsipkt->pkt_comp != NULL) {
3902                 /* scsi callback required */
3903                 if (servicing_interrupt()) {
3904                         if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3905                             (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3906                             (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3907                                 return (TRAN_BUSY);
3908                         }
3909                 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3910                     (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3911                     (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3912                         /* Scheduling the callback failed */
3913                         return (TRAN_BUSY);
3914                 }
3915         }
3916         return (TRAN_ACCEPT);
3917 }
3918 
3919 /*
3920  * SATA translate command: Request Sense.
3921  *
3922  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3923  * At the moment this is an emulated command (ATA version for SATA hard disks).
3924  * May be translated into Check Power Mode command in the future.
3925  *
3926  * Note: There is a mismatch between already implemented Informational
3927  * Exception Mode Select page 0x1C and this function.
3928  * When MRIE bit is set in page 0x1C, Request Sense is supposed to return
3929  * NO SENSE and set additional sense code to the exception code - this is not
3930  * implemented here.
3931  */
3932 static int
3933 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3934 {
3935         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3936         struct scsi_extended_sense sense;
3937         struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3938         sata_drive_info_t *sdinfo;
3939         sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3940         int rval, reason, power_state = 0;
3941         kmutex_t *cport_mutex;
3942 
3943         cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
3944         mutex_enter(cport_mutex);
3945 
3946         if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
3947             TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3948                 mutex_exit(cport_mutex);
3949                 return (rval);
3950         }
3951 
3952         scsipkt->pkt_reason = CMD_CMPLT;
3953         scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3954             STATE_SENT_CMD | STATE_GOT_STATUS;
3955         *scsipkt->pkt_scbp = STATUS_GOOD;
3956 
3957         /*
3958          * when CONTROL field's NACA bit == 1
3959          * return ILLEGAL_REQUEST
3960          */
3961         if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) {
3962                 mutex_exit(cport_mutex);
3963                 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
3964                     SD_SCSI_ASC_CMD_SEQUENCE_ERR));
3965         }
3966 
3967         sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3968             &spx->txlt_sata_pkt->satapkt_device);
3969         ASSERT(sdinfo != NULL);
3970 
3971         spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
3972 
3973         sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
3974         scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
3975         scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3976         if (sata_hba_start(spx, &rval) != 0) {
3977                 mutex_exit(cport_mutex);
3978                 return (rval);
3979         }
3980         if (scmd->satacmd_error_reg != 0) {
3981                 mutex_exit(cport_mutex);
3982                 return (sata_txlt_check_condition(spx, KEY_NO_SENSE,
3983                     SD_SCSI_ASC_NO_ADD_SENSE));
3984         }
3985 
3986         switch (scmd->satacmd_sec_count_lsb) {
3987         case SATA_PWRMODE_STANDBY: /* device in standby mode */
3988                 if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)
3989                         power_state = SATA_POWER_STOPPED;
3990                 else {
3991                         power_state = SATA_POWER_STANDBY;
3992                         sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
3993                 }
3994                 break;
3995         case SATA_PWRMODE_IDLE: /* device in idle mode */
3996                 power_state = SATA_POWER_IDLE;
3997                 sdinfo->satadrv_power_level = SATA_POWER_IDLE;
3998                 break;
3999         case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */
4000         default:                  /* 0x40, 0x41 active mode */
4001                 if (sdinfo->satadrv_power_level == SATA_POWER_IDLE)
4002                         power_state = SATA_POWER_IDLE;
4003                 else {
4004                         power_state = SATA_POWER_ACTIVE;
4005                         sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4006                 }
4007                 break;
4008         }
4009 
4010         mutex_exit(cport_mutex);
4011 
4012         if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4013                 /*
4014                  * Because it is fully emulated command storing data
4015                  * programatically in the specified buffer, release
4016                  * preallocated DMA resources before storing data in the buffer,
4017                  * so no unwanted DMA sync would take place.
4018                  */
4019                 int count = MIN(bp->b_bcount,
4020                     sizeof (struct scsi_extended_sense));
4021                 sata_scsi_dmafree(NULL, scsipkt);
4022                 bzero(&sense, sizeof (struct scsi_extended_sense));
4023                 sense.es_valid = 0;     /* Valid LBA */
4024                 sense.es_class = 7;     /* Response code 0x70 - current err */
4025                 sense.es_key = KEY_NO_SENSE;
4026                 sense.es_add_len = 6;   /* Additional length */
4027                 /* Copy no more than requested */
4028                 bcopy(&sense, bp->b_un.b_addr, count);
4029                 scsipkt->pkt_state |= STATE_XFERRED_DATA;
4030                 scsipkt->pkt_resid = 0;
4031                 switch (power_state) {
4032                 case SATA_POWER_IDLE:
4033                 case SATA_POWER_STANDBY:
4034                         sense.es_add_code =
4035                             SD_SCSI_ASC_LOW_POWER_CONDITION_ON;
4036                         break;
4037                 case SATA_POWER_STOPPED:
4038                         sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE;
4039                         break;
4040                 case SATA_POWER_ACTIVE:
4041                 default:
4042                         break;
4043                 }
4044         }
4045 
4046         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4047             "Scsi_pkt completion reason %x\n",
4048             scsipkt->pkt_reason);
4049 
4050         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4051             scsipkt->pkt_comp != NULL) {
4052                 /* scsi callback required */
4053                 if (servicing_interrupt()) {
4054                         if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4055                             (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4056                             (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4057                                 return (TRAN_BUSY);
4058                         }
4059                 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4060                     (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4061                     (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4062                         /* Scheduling the callback failed */
4063                         return (TRAN_BUSY);
4064                 }
4065         }
4066         return (TRAN_ACCEPT);
4067 }
4068 
4069 /*
4070  * SATA translate command: Test Unit Ready
4071  * (ATA version for SATA hard disks).
4072  * It is translated into the Check Power Mode command.
4073  *
4074  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4075  */
4076 static int
4077 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
4078 {
4079         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4080         struct scsi_extended_sense *sense;
4081         sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4082         sata_drive_info_t *sdinfo;
4083         int power_state;
4084         int rval, reason;
4085         kmutex_t *cport_mutex =  &(SATA_TXLT_CPORT_MUTEX(spx));
4086 
4087         mutex_enter(cport_mutex);
4088 
4089         if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4090             TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4091                 mutex_exit(cport_mutex);
4092                 return (rval);
4093         }
4094 
4095         sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4096             &spx->txlt_sata_pkt->satapkt_device);
4097         ASSERT(sdinfo != NULL);
4098 
4099         spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4100 
4101         /* send CHECK POWER MODE command */
4102         sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4103         scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4104         scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4105         if (sata_hba_start(spx, &rval) != 0) {
4106                 mutex_exit(cport_mutex);
4107                 return (rval);
4108         }
4109 
4110         if (scmd->satacmd_error_reg != 0) {
4111                 mutex_exit(cport_mutex);
4112                 return (sata_txlt_check_condition(spx, KEY_NOT_READY,
4113                     SD_SCSI_ASC_LU_NOT_RESPONSE));
4114         }
4115 
4116         power_state = scmd->satacmd_sec_count_lsb;
4117 
4118         /*
4119          * return NOT READY when device in STOPPED mode
4120          */
4121         if (power_state == SATA_PWRMODE_STANDBY &&
4122             sdinfo->satadrv_power_level == SATA_POWER_STOPPED) {
4123                 *scsipkt->pkt_scbp = STATUS_CHECK;
4124                 sense = sata_arq_sense(spx);
4125                 sense->es_key = KEY_NOT_READY;
4126                 sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
4127         } else {
4128                 /*
4129                  * For other power mode, return GOOD status
4130                  */
4131                 *scsipkt->pkt_scbp = STATUS_GOOD;
4132         }
4133 
4134         scsipkt->pkt_reason = CMD_CMPLT;
4135         scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4136             STATE_SENT_CMD | STATE_GOT_STATUS;
4137 
4138         mutex_exit(cport_mutex);
4139 
4140         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4141             "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4142 
4143         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4144             scsipkt->pkt_comp != NULL) {
4145                 /* scsi callback required */
4146                 if (servicing_interrupt()) {
4147                         if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4148                             (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4149                             (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4150                                 return (TRAN_BUSY);
4151                         }
4152                 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4153                     (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4154                     (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4155                         /* Scheduling the callback failed */
4156                         return (TRAN_BUSY);
4157                 }
4158         }
4159 
4160         return (TRAN_ACCEPT);
4161 }
4162 
4163 /*
4164  * SATA translate command: Start Stop Unit
4165  * Translation depends on a command:
4166  *
4167  * Power condition bits will be supported
4168  * and the power level should be maintained by SATL,
4169  * When SATL received a command, it will check the
4170  * power level firstly, and return the status according
4171  * to SAT2 v2.6 and SAT-2 Standby Modifications
4172  *
4173  * SPC-4/SBC-3      SATL    ATA power condition  SATL      SPC/SBC
4174  * -----------------------------------------------------------------------
4175  * SSU_PC1 Active   <==>     ATA  Active         <==>     SSU:start_bit =1
4176  * SSU_PC2 Idle     <==>     ATA  Idle           <==>     N/A
4177  * SSU_PC3 Standby  <==>     ATA  Standby        <==>     N/A
4178  * SSU_PC4 Stopped  <==>     ATA  Standby        <==>     SSU:start_bit = 0
4179  *
4180  *      Unload Media / NOT SUPPORTED YET
4181  *      Load Media / NOT SUPPROTED YET
4182  *      Immediate bit / NOT SUPPORTED YET (deferred error)
4183  *
4184  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4185  * appropriate values in scsi_pkt fields.
4186  */
4187 static int
4188 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
4189 {
4190         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4191         sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4192         int rval, reason;
4193         sata_drive_info_t *sdinfo;
4194         sata_id_t *sata_id;
4195         kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4196 
4197         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4198             "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
4199 
4200         mutex_enter(cport_mutex);
4201 
4202         if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4203             TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4204                 mutex_exit(cport_mutex);
4205                 return (rval);
4206         }
4207 
4208         if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) {
4209                 /* IMMED bit - not supported */
4210                 mutex_exit(cport_mutex);
4211                 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4212                     SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4213         }
4214 
4215         spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4216         spx->txlt_sata_pkt->satapkt_comp = NULL;
4217 
4218         sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4219             &spx->txlt_sata_pkt->satapkt_device);
4220         ASSERT(sdinfo != NULL);
4221         sata_id = &sdinfo->satadrv_id;
4222 
4223         switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) {
4224         case 0:
4225                 if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) {
4226                         /* Load/Unload Media - invalid request */
4227                         goto err_out;
4228                 }
4229                 if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) {
4230                         /* Start Unit */
4231                         sata_build_read_verify_cmd(scmd, 1, 5);
4232                         scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4233                         /* Transfer command to HBA */
4234                         if (sata_hba_start(spx, &rval) != 0) {
4235                                 /* Pkt not accepted for execution */
4236                                 mutex_exit(cport_mutex);
4237                                 return (rval);
4238                         }
4239                         if (scmd->satacmd_error_reg != 0) {
4240                                 goto err_out;
4241                         }
4242                         sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4243                 } else {
4244                         /* Stop Unit */
4245                         sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4246                         scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4247                         if (sata_hba_start(spx, &rval) != 0) {
4248                                 mutex_exit(cport_mutex);
4249                                 return (rval);
4250                         } else {
4251                                 if (scmd->satacmd_error_reg != 0) {
4252                                         goto err_out;
4253                                 }
4254                         }
4255                         /* ata standby immediate command */
4256                         sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4257                         scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4258                         if (sata_hba_start(spx, &rval) != 0) {
4259                                 mutex_exit(cport_mutex);
4260                                 return (rval);
4261                         }
4262                         if (scmd->satacmd_error_reg != 0) {
4263                                 goto err_out;
4264                         }
4265                         sdinfo->satadrv_power_level = SATA_POWER_STOPPED;
4266                 }
4267                 break;
4268         case 0x1:
4269                 sata_build_generic_cmd(scmd, SATAC_IDLE);
4270                 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4271                 if (sata_hba_start(spx, &rval) != 0) {
4272                         mutex_exit(cport_mutex);
4273                         return (rval);
4274                 }
4275                 if (scmd->satacmd_error_reg != 0) {
4276                         goto err_out;
4277                 }
4278                 sata_build_read_verify_cmd(scmd, 1, 5);
4279                 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4280                 /* Transfer command to HBA */
4281                 if (sata_hba_start(spx, &rval) != 0) {
4282                         /* Pkt not accepted for execution */
4283                         mutex_exit(cport_mutex);
4284                         return (rval);
4285                 } else {
4286                         if (scmd->satacmd_error_reg != 0) {
4287                                 goto err_out;
4288                         }
4289                 }
4290                 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4291                 break;
4292         case 0x2:
4293                 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4294                 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4295                 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4296                         if (sata_hba_start(spx, &rval) != 0) {
4297                                 mutex_exit(cport_mutex);
4298                                 return (rval);
4299                         }
4300                         if (scmd->satacmd_error_reg != 0) {
4301                                 goto err_out;
4302                         }
4303                 }
4304                 sata_build_generic_cmd(scmd, SATAC_IDLE);
4305                 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4306                 if (sata_hba_start(spx, &rval) != 0) {
4307                         mutex_exit(cport_mutex);
4308                         return (rval);
4309                 }
4310                 if (scmd->satacmd_error_reg != 0) {
4311                         goto err_out;
4312                 }
4313                 if ((scsipkt->pkt_cdbp[3] & START_STOP_MODIFIER_MASK)) {
4314                         /*
4315                          *  POWER CONDITION MODIFIER bit set
4316                          *  to 0x1 or larger it will be handled
4317                          *  on the same way as bit = 0x1
4318                          */
4319                         if (!(sata_id->ai_cmdset84 &
4320                             SATA_IDLE_UNLOAD_SUPPORTED)) {
4321                                 sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4322                                 break;
4323                         }
4324                         sata_build_generic_cmd(scmd, SATAC_IDLE_IM);
4325                         scmd->satacmd_features_reg = 0x44;
4326                         scmd->satacmd_lba_low_lsb = 0x4c;
4327                         scmd->satacmd_lba_mid_lsb = 0x4e;
4328                         scmd->satacmd_lba_high_lsb = 0x55;
4329                         scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4330                         if (sata_hba_start(spx, &rval) != 0) {
4331                                 mutex_exit(cport_mutex);
4332                                 return (rval);
4333                         }
4334                         if (scmd->satacmd_error_reg != 0) {
4335                                 goto err_out;
4336                         }
4337                 }
4338                 sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4339                 break;
4340         case 0x3:
4341                 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4342                 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4343                 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4344                         if (sata_hba_start(spx, &rval) != 0) {
4345                                 mutex_exit(cport_mutex);
4346                                 return (rval);
4347                         }
4348                         if (scmd->satacmd_error_reg != 0) {
4349                                 goto err_out;
4350                         }
4351                 }
4352                 sata_build_generic_cmd(scmd, SATAC_STANDBY);
4353                 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4354                 if (sata_hba_start(spx, &rval) != 0) {
4355                         mutex_exit(cport_mutex);
4356                         return (rval);
4357                 }
4358                 if (scmd->satacmd_error_reg != 0) {
4359                         goto err_out;
4360                 }
4361                 sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
4362                 break;
4363         case 0x7:
4364                 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4365                 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4366                 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4367                 if (sata_hba_start(spx, &rval) != 0) {
4368                         mutex_exit(cport_mutex);
4369                         return (rval);
4370                 }
4371                 if (scmd->satacmd_error_reg != 0) {
4372                         goto err_out;
4373                 }
4374                 switch (scmd->satacmd_sec_count_lsb) {
4375                 case SATA_PWRMODE_STANDBY:
4376                         sata_build_generic_cmd(scmd, SATAC_STANDBY);
4377                         scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4378                             sdinfo->satadrv_standby_timer);
4379                         scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4380                         if (sata_hba_start(spx, &rval) != 0) {
4381                                 mutex_exit(cport_mutex);
4382                                 return (rval);
4383                         } else {
4384                                 if (scmd->satacmd_error_reg != 0) {
4385                                         goto err_out;
4386                                 }
4387                         }
4388                         break;
4389                 case SATA_PWRMODE_IDLE:
4390                         sata_build_generic_cmd(scmd, SATAC_IDLE);
4391                         scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4392                             sdinfo->satadrv_standby_timer);
4393                         scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4394                         if (sata_hba_start(spx, &rval) != 0) {
4395                                 mutex_exit(cport_mutex);
4396                                 return (rval);
4397                         } else {
4398                                 if (scmd->satacmd_error_reg != 0) {
4399                                         goto err_out;
4400                                 }
4401                         }
4402                         break;
4403                 case SATA_PWRMODE_ACTIVE_SPINDOWN:
4404                 case SATA_PWRMODE_ACTIVE_SPINUP:
4405                 case SATA_PWRMODE_ACTIVE:
4406                         sata_build_generic_cmd(scmd, SATAC_IDLE);
4407                         scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4408                             sdinfo->satadrv_standby_timer);
4409                         scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4410                         if (sata_hba_start(spx, &rval) != 0) {
4411                                 mutex_exit(cport_mutex);
4412                                 return (rval);
4413                         }
4414                         if (scmd->satacmd_error_reg != 0) {
4415                                 goto err_out;
4416                         }
4417                         sata_build_read_verify_cmd(scmd, 1, 5);
4418                         scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4419                         if (sata_hba_start(spx, &rval) != 0) {
4420                                 mutex_exit(cport_mutex);
4421                                 return (rval);
4422                         }
4423                         if (scmd->satacmd_error_reg != 0) {
4424                                 goto err_out;
4425                         }
4426                         break;
4427                 default:
4428                         goto err_out;
4429                 }
4430                 break;
4431         case 0xb:
4432                 if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) ==
4433                     0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) {
4434                         mutex_exit(cport_mutex);
4435                         return (sata_txlt_check_condition(spx,
4436                             KEY_ILLEGAL_REQUEST,
4437                             SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4438                 }
4439                 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4440                 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4441                 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4442                         if (sata_hba_start(spx, &rval) != 0) {
4443                                 mutex_exit(cport_mutex);
4444                                 return (rval);
4445                         }
4446                         if (scmd->satacmd_error_reg != 0) {
4447                                 goto err_out;
4448                         }
4449                         sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4450                         scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4451                         if (sata_hba_start(spx, &rval) != 0) {
4452                                 mutex_exit(cport_mutex);
4453                                 return (rval);
4454                         }
4455                         if (scmd->satacmd_error_reg != 0) {
4456                                 goto err_out;
4457                         }
4458                 }
4459                 bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4);
4460                 break;
4461         default:
4462 err_out:
4463                 mutex_exit(cport_mutex);
4464                 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4465                     SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4466         }
4467 
4468         /*
4469          * Since it was a synchronous command,
4470          * a callback function will be called directly.
4471          */
4472         mutex_exit(cport_mutex);
4473         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4474             "synchronous execution status %x\n",
4475             spx->txlt_sata_pkt->satapkt_reason);
4476 
4477         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4478             scsipkt->pkt_comp != NULL) {
4479                 sata_set_arq_data(spx->txlt_sata_pkt);
4480                 if (servicing_interrupt()) {
4481                         if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4482                             (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4483                             (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4484                                 return (TRAN_BUSY);
4485                         }
4486                 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4487                     (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4488                     (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4489                         /* Scheduling the callback failed */
4490                         return (TRAN_BUSY);
4491                 }
4492         }
4493         else
4494 
4495                 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
4496 
4497         return (TRAN_ACCEPT);
4498 
4499 }
4500 
4501 /*
4502  * SATA translate command:  Read Capacity.
4503  * Emulated command for SATA disks.
4504  * Capacity is retrieved from cached Idenifty Device data.
4505  * Identify Device data shows effective disk capacity, not the native
4506  * capacity, which may be limitted by Set Max Address command.
4507  * This is ATA version for SATA hard disks.
4508  *
4509  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4510  */
4511 static int
4512 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
4513 {
4514         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4515         struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4516         sata_drive_info_t *sdinfo;
4517         uint64_t val;
4518         uchar_t *rbuf;
4519         int rval, reason;
4520         kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4521 
4522         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4523             "sata_txlt_read_capacity: ", NULL);
4524 
4525         mutex_enter(cport_mutex);
4526 
4527         if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4528             TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4529                 mutex_exit(cport_mutex);
4530                 return (rval);
4531         }
4532 
4533         scsipkt->pkt_reason = CMD_CMPLT;
4534         scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4535             STATE_SENT_CMD | STATE_GOT_STATUS;
4536         *scsipkt->pkt_scbp = STATUS_GOOD;
4537         if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4538                 /*
4539                  * Because it is fully emulated command storing data
4540                  * programatically in the specified buffer, release
4541                  * preallocated DMA resources before storing data in the buffer,
4542                  * so no unwanted DMA sync would take place.
4543                  */
4544                 sata_scsi_dmafree(NULL, scsipkt);
4545 
4546                 sdinfo = sata_get_device_info(
4547                     spx->txlt_sata_hba_inst,
4548                     &spx->txlt_sata_pkt->satapkt_device);
4549 
4550                 /*
4551                  * As per SBC-3, the "returned LBA" is either the highest
4552                  * addressable LBA or 0xffffffff, whichever is smaller.
4553                  */
4554                 val = MIN(sdinfo->satadrv_capacity - 1, UINT32_MAX);
4555 
4556                 rbuf = (uchar_t *)bp->b_un.b_addr;
4557                 /* Need to swap endians to match scsi format */
4558                 rbuf[0] = (val >> 24) & 0xff;
4559                 rbuf[1] = (val >> 16) & 0xff;
4560                 rbuf[2] = (val >> 8) & 0xff;
4561                 rbuf[3] = val & 0xff;
4562                 /* block size - always 512 bytes, for now */
4563                 rbuf[4] = 0;
4564                 rbuf[5] = 0;
4565                 rbuf[6] = 0x02;
4566                 rbuf[7] = 0;
4567                 scsipkt->pkt_state |= STATE_XFERRED_DATA;
4568                 scsipkt->pkt_resid = 0;
4569 
4570                 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
4571                     sdinfo->satadrv_capacity -1);
4572         }
4573         mutex_exit(cport_mutex);
4574         /*
4575          * If a callback was requested, do it now.
4576          */
4577         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4578             "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4579 
4580         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4581             scsipkt->pkt_comp != NULL) {
4582                 /* scsi callback required */
4583                 if (servicing_interrupt()) {
4584                         if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4585                             (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4586                             (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4587                                 return (TRAN_BUSY);
4588                         }
4589                 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4590                     (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4591                     (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4592                         /* Scheduling the callback failed */
4593                         return (TRAN_BUSY);
4594                 }
4595         }
4596 
4597         return (TRAN_ACCEPT);
4598 }
4599 
4600 /*
4601  * SATA translate command:  Read Capacity (16).
4602  * Emulated command for SATA disks.
4603  * Info is retrieved from cached Identify Device data.
4604  * Implemented to SBC-3 (draft 21) and SAT-2 (final) specifications.
4605  *
4606  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4607  */
4608 static int
4609 sata_txlt_read_capacity16(sata_pkt_txlate_t *spx)
4610 {
4611         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4612         struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4613         sata_drive_info_t *sdinfo;
4614         uint64_t val;
4615         uint16_t l2p_exp;
4616         uchar_t *rbuf;
4617         int rval, reason;
4618 #define TPE     0x80
4619 #define TPRZ    0x40
4620         kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4621 
4622         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4623             "sata_txlt_read_capacity: ", NULL);
4624 
4625         mutex_enter(cport_mutex);
4626 
4627         if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4628             TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4629                 mutex_exit(cport_mutex);
4630                 return (rval);
4631         }
4632 
4633         scsipkt->pkt_reason = CMD_CMPLT;
4634         scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4635             STATE_SENT_CMD | STATE_GOT_STATUS;
4636         if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4637                 /*
4638                  * Because it is fully emulated command storing data
4639                  * programatically in the specified buffer, release
4640                  * preallocated DMA resources before storing data in the buffer,
4641                  * so no unwanted DMA sync would take place.
4642                  */
4643                 sata_scsi_dmafree(NULL, scsipkt);
4644 
4645                 /* Check SERVICE ACTION field */
4646                 if ((scsipkt->pkt_cdbp[1] & 0x1f) !=
4647                     SSVC_ACTION_READ_CAPACITY_G4) {
4648                         mutex_exit(cport_mutex);
4649                         return (sata_txlt_check_condition(spx,
4650                             KEY_ILLEGAL_REQUEST,
4651                             SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4652                 }
4653 
4654                 /* Check LBA field */
4655                 if ((scsipkt->pkt_cdbp[2] != 0) ||
4656                     (scsipkt->pkt_cdbp[3] != 0) ||
4657                     (scsipkt->pkt_cdbp[4] != 0) ||
4658                     (scsipkt->pkt_cdbp[5] != 0) ||
4659                     (scsipkt->pkt_cdbp[6] != 0) ||
4660                     (scsipkt->pkt_cdbp[7] != 0) ||
4661                     (scsipkt->pkt_cdbp[8] != 0) ||
4662                     (scsipkt->pkt_cdbp[9] != 0)) {
4663                         mutex_exit(cport_mutex);
4664                         return (sata_txlt_check_condition(spx,
4665                             KEY_ILLEGAL_REQUEST,
4666                             SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4667                 }
4668 
4669                 /* Check PMI bit */
4670                 if (scsipkt->pkt_cdbp[14] & 0x1) {
4671                         mutex_exit(cport_mutex);
4672                         return (sata_txlt_check_condition(spx,
4673                             KEY_ILLEGAL_REQUEST,
4674                             SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4675                 }
4676 
4677                 *scsipkt->pkt_scbp = STATUS_GOOD;
4678 
4679                 sdinfo = sata_get_device_info(
4680                     spx->txlt_sata_hba_inst,
4681                     &spx->txlt_sata_pkt->satapkt_device);
4682 
4683                 /* last logical block address */
4684                 val = MIN(sdinfo->satadrv_capacity - 1,
4685                     SCSI_READ_CAPACITY16_MAX_LBA);
4686 
4687                 /* logical to physical block size exponent */
4688                 l2p_exp = 0;
4689                 if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) {
4690                         /* physical/logical sector size word is valid */
4691 
4692                         if (sdinfo->satadrv_id.ai_phys_sect_sz &
4693                             SATA_L2PS_HAS_MULT) {
4694                                 /* multiple logical sectors per phys sectors */
4695                                 l2p_exp =
4696                                     sdinfo->satadrv_id.ai_phys_sect_sz &
4697                                     SATA_L2PS_EXP_MASK;
4698                         }
4699                 }
4700 
4701                 rbuf = (uchar_t *)bp->b_un.b_addr;
4702                 bzero(rbuf, bp->b_bcount);
4703 
4704                 /* returned logical block address */
4705                 rbuf[0] = (val >> 56) & 0xff;
4706                 rbuf[1] = (val >> 48) & 0xff;
4707                 rbuf[2] = (val >> 40) & 0xff;
4708                 rbuf[3] = (val >> 32) & 0xff;
4709                 rbuf[4] = (val >> 24) & 0xff;
4710                 rbuf[5] = (val >> 16) & 0xff;
4711                 rbuf[6] = (val >> 8) & 0xff;
4712                 rbuf[7] = val & 0xff;
4713 
4714                 /* logical block length in bytes = 512 (for now) */
4715                 /* rbuf[8] = 0; */
4716                 /* rbuf[9] = 0; */
4717                 rbuf[10] = 0x02;
4718                 /* rbuf[11] = 0; */
4719 
4720                 /* p_type, prot_en, unspecified by SAT-2 */
4721                 /* rbuf[12] = 0; */
4722 
4723                 /* p_i_exponent, undefined by SAT-2 */
4724                 /* logical blocks per physical block exponent */
4725                 rbuf[13] = l2p_exp;
4726 
4727                 /* lowest aligned logical block address = 0 (for now) */
4728                 /* tpe and tprz as defined in T10/10-079 r0 */
4729                 if (sdinfo->satadrv_id.ai_addsupported &
4730                     SATA_DETERMINISTIC_READ) {
4731                         if (sdinfo->satadrv_id.ai_addsupported &
4732                             SATA_READ_ZERO) {
4733                                 rbuf[14] |= TPRZ;
4734                         } else {
4735                                 rbuf[14] |= TPE;
4736                         }
4737                 }
4738                 /* rbuf[15] = 0; */
4739 
4740                 scsipkt->pkt_state |= STATE_XFERRED_DATA;
4741                 scsipkt->pkt_resid = 0;
4742 
4743                 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%llu\n",
4744                     sdinfo->satadrv_capacity -1);
4745         }
4746 
4747         mutex_exit(cport_mutex);
4748 
4749         /*
4750          * If a callback was requested, do it now.
4751          */
4752         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4753             "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4754 
4755         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4756             scsipkt->pkt_comp != NULL) {
4757                 /* scsi callback required */
4758                 if (servicing_interrupt()) {
4759                         if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4760                             (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4761                             (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4762                                 return (TRAN_BUSY);
4763                         }
4764                 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4765                     (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4766                     (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4767                         /* Scheduling the callback failed */
4768                         return (TRAN_BUSY);
4769                 }
4770         }
4771 
4772         return (TRAN_ACCEPT);
4773 }
4774 
4775 /*
4776  * Translate command: UNMAP
4777  *
4778  * The function cannot be called in interrupt context since it may sleep.
4779  */
4780 static int
4781 sata_txlt_unmap(sata_pkt_txlate_t *spx)
4782 {
4783         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4784         sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4785         struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4786         uint16_t count = 0;
4787         int synch;
4788         int rval, reason;
4789         int i, x;
4790         int bdlen = 0;
4791         int ranges = 0;
4792         int paramlen = 8;
4793         uint8_t *data, *tmpbd;
4794         sata_drive_info_t *sdinfo;
4795         kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4796 #define TRIM    0x1
4797 
4798         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4799             "sata_txlt_unmap: ", NULL);
4800 
4801         mutex_enter(cport_mutex);
4802 
4803         sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4804             &spx->txlt_sata_pkt->satapkt_device);
4805         if (sdinfo != NULL) {
4806                 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4807                     "DSM support 0x%x, max number of 512 byte blocks of LBA "
4808                     "range entries 0x%x\n", sdinfo->satadrv_id.ai_dsm,
4809                     sdinfo->satadrv_id.ai_maxcount);
4810         }
4811 
4812         rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
4813         if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4814                 mutex_exit(cport_mutex);
4815                 return (rval);
4816         }
4817 
4818         /*
4819          * Need to modify bp to have TRIM data instead of UNMAP data.
4820          * Start by getting the block descriptor data length by subtracting
4821          * the 8 byte parameter list header from the parameter list length.
4822          * The block descriptor size has to be a multiple of 16 bytes.
4823          */
4824         bdlen = scsipkt->pkt_cdbp[7];
4825         bdlen = (bdlen << 8) + scsipkt->pkt_cdbp[8] - paramlen;
4826         if ((bdlen < 0) || ((bdlen % 16) != 0) ||
4827             (bdlen > (bp->b_bcount - paramlen))) {
4828                 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4829                     "sata_txlt_unmap: invalid block descriptor length", NULL);
4830                 mutex_exit(cport_mutex);
4831                 return ((sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4832                     SD_SCSI_ASC_INVALID_FIELD_IN_CDB)));
4833         }
4834         /*
4835          * If there are no parameter data or block descriptors, it is not
4836          * considered an error so just complete the command without sending
4837          * TRIM.
4838          */
4839         if ((bdlen == 0) || (bp == NULL) || (bp->b_un.b_addr == NULL) ||
4840             (bp->b_bcount == 0)) {
4841                 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4842                     "sata_txlt_unmap: no parameter data or block descriptors",
4843                     NULL);
4844                 mutex_exit(cport_mutex);
4845                 return (sata_txlt_unmap_nodata_cmd(spx));
4846         }
4847         tmpbd = (uint8_t *)bp->b_un.b_addr + paramlen;
4848         data = kmem_zalloc(bdlen, KM_SLEEP);
4849 
4850         /*
4851          * Loop through all the UNMAP block descriptors and convert the data
4852          * into TRIM format.
4853          */
4854         for (i = 0, x = 0; i < bdlen; i += 16, x += 8) {
4855                 /* get range length */
4856                 data[x] = tmpbd[i+7];
4857                 data[x+1] = tmpbd[i+6];
4858                 /* get LBA */
4859                 data[x+2] = tmpbd[i+5];
4860                 data[x+3] = tmpbd[i+4];
4861                 data[x+4] = tmpbd[i+3];
4862                 data[x+5] = tmpbd[i+2];
4863                 data[x+6] = tmpbd[i+11];
4864                 data[x+7] = tmpbd[i+10];
4865 
4866                 ranges++;
4867         }
4868 
4869         /*
4870          * The TRIM command expects the data buffer to be a multiple of
4871          * 512-byte blocks of range entries.  This means that the UNMAP buffer
4872          * may be too small.  Free the original DMA resources and create a
4873          * local buffer.
4874          */
4875         sata_common_free_dma_rsrcs(spx);
4876 
4877         /*
4878          * Get count of 512-byte blocks of range entries.  The length
4879          * of a range entry is 8 bytes which means one count has 64 range
4880          * entries.
4881          */
4882         count = (ranges + 63)/64;
4883 
4884         /* Allocate a buffer that is a multiple of 512 bytes. */
4885         mutex_exit(cport_mutex);
4886         bp = sata_alloc_local_buffer(spx, count * 512);
4887         if (bp == NULL) {
4888                 SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
4889                     "sata_txlt_unmap: "
4890                     "cannot allocate buffer for TRIM command", NULL);
4891                 kmem_free(data, bdlen);
4892                 return (TRAN_BUSY);
4893         }
4894         bp_mapin(bp); /* make data buffer accessible */
4895         mutex_enter(cport_mutex);
4896 
4897         bzero(bp->b_un.b_addr, bp->b_bcount);
4898         bcopy(data, bp->b_un.b_addr, x);
4899         kmem_free(data, bdlen);
4900         rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
4901             DDI_DMA_SYNC_FORDEV);
4902         ASSERT(rval == DDI_SUCCESS);
4903 
4904         scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4905         scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4906         scmd->satacmd_cmd_reg = SATAC_DSM;
4907         scmd->satacmd_sec_count_msb = (count >> 8) & 0xff;
4908         scmd->satacmd_sec_count_lsb = count & 0xff;
4909         scmd->satacmd_features_reg = TRIM;
4910         scmd->satacmd_device_reg = SATA_ADH_LBA;
4911         scmd->satacmd_status_reg = 0;
4912         scmd->satacmd_error_reg = 0;
4913 
4914         /* Start processing command */
4915         if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4916                 spx->txlt_sata_pkt->satapkt_comp =
4917                     sata_txlt_unmap_completion;
4918                 synch = FALSE;
4919         } else {
4920                 synch = TRUE;
4921         }
4922 
4923         if (sata_hba_start(spx, &rval) != 0) {
4924                 mutex_exit(cport_mutex);
4925                 return (rval);
4926         }
4927 
4928         mutex_exit(cport_mutex);
4929 
4930         if (synch) {
4931                 sata_txlt_unmap_completion(spx->txlt_sata_pkt);
4932         }
4933 
4934         return (TRAN_ACCEPT);
4935 }
4936 
4937 /*
4938  * SATA translate command: Mode Sense.
4939  * Translated into appropriate SATA command or emulated.
4940  * Saved Values Page Control (03) are not supported.
4941  *
4942  * NOTE: only caching mode sense page is currently implemented.
4943  *
4944  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4945  */
4946 
4947 #define LLBAA   0x10    /* Long LBA Accepted */
4948 
4949 static int
4950 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
4951 {
4952         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4953         struct buf      *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4954         sata_drive_info_t *sdinfo;
4955         sata_id_t *sata_id;
4956         struct scsi_extended_sense *sense;
4957         int             len, bdlen, count, alc_len;
4958         int             pc;     /* Page Control code */
4959         uint8_t         *buf;   /* mode sense buffer */
4960         int             rval, reason;
4961         kmutex_t        *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4962 
4963         SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4964             "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
4965             spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4966             spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4967 
4968         if (servicing_interrupt()) {
4969                 buf = kmem_zalloc(1024, KM_NOSLEEP);
4970                 if (buf == NULL) {
4971                         return (TRAN_BUSY);
4972                 }
4973         } else {
4974                 buf = kmem_zalloc(1024, KM_SLEEP);
4975         }
4976 
4977         mutex_enter(cport_mutex);
4978 
4979         if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4980             TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4981                 mutex_exit(cport_mutex);
4982                 kmem_free(buf, 1024);
4983                 return (rval);
4984         }
4985 
4986         scsipkt->pkt_reason = CMD_CMPLT;
4987         scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4988             STATE_SENT_CMD | STATE_GOT_STATUS;
4989 
4990         pc = scsipkt->pkt_cdbp[2] >> 6;
4991 
4992         if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4993                 /*
4994                  * Because it is fully emulated command storing data
4995                  * programatically in the specified buffer, release
4996                  * preallocated DMA resources before storing data in the buffer,
4997                  * so no unwanted DMA sync would take place.
4998                  */
4999                 sata_scsi_dmafree(NULL, scsipkt);
5000 
5001                 len = 0;
5002                 bdlen = 0;
5003                 if (!(scsipkt->pkt_cdbp[1] & 8)) {
5004                         if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
5005                             (scsipkt->pkt_cdbp[1] & LLBAA))
5006                                 bdlen = 16;
5007                         else
5008                                 bdlen = 8;
5009                 }
5010                 /* Build mode parameter header */
5011                 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5012                         /* 4-byte mode parameter header */
5013                         buf[len++] = 0;         /* mode data length */
5014                         buf[len++] = 0;         /* medium type */
5015                         buf[len++] = 0;         /* dev-specific param */
5016                         buf[len++] = bdlen;     /* Block Descriptor length */
5017                 } else {
5018                         /* 8-byte mode parameter header */
5019                         buf[len++] = 0;         /* mode data length */
5020                         buf[len++] = 0;
5021                         buf[len++] = 0;         /* medium type */
5022                         buf[len++] = 0;         /* dev-specific param */
5023                         if (bdlen == 16)
5024                                 buf[len++] = 1; /* long lba descriptor */
5025                         else
5026                                 buf[len++] = 0;
5027                         buf[len++] = 0;
5028                         buf[len++] = 0;         /* Block Descriptor length */
5029                         buf[len++] = bdlen;
5030                 }
5031 
5032                 sdinfo = sata_get_device_info(
5033                     spx->txlt_sata_hba_inst,
5034                     &spx->txlt_sata_pkt->satapkt_device);
5035 
5036                 /* Build block descriptor only if not disabled (DBD) */
5037                 if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
5038                         /* Block descriptor - direct-access device format */
5039                         if (bdlen == 8) {
5040                                 /* build regular block descriptor */
5041                                 buf[len++] =
5042                                     (sdinfo->satadrv_capacity >> 24) & 0xff;
5043                                 buf[len++] =
5044                                     (sdinfo->satadrv_capacity >> 16) & 0xff;
5045                                 buf[len++] =
5046                                     (sdinfo->satadrv_capacity >> 8) & 0xff;
5047                                 buf[len++] = sdinfo->satadrv_capacity & 0xff;
5048                                 buf[len++] = 0; /* density code */
5049                                 buf[len++] = 0;
5050                                 if (sdinfo->satadrv_type ==
5051                                     SATA_DTYPE_ATADISK)
5052                                         buf[len++] = 2;
5053                                 else
5054                                         /* ATAPI */
5055                                         buf[len++] = 8;
5056                                 buf[len++] = 0;
5057                         } else if (bdlen == 16) {
5058                                 /* Long LBA Accepted */
5059                                 /* build long lba block descriptor */
5060 #ifndef __lock_lint
5061                                 buf[len++] =
5062                                     (sdinfo->satadrv_capacity >> 56) & 0xff;
5063                                 buf[len++] =
5064                                     (sdinfo->satadrv_capacity >> 48) & 0xff;
5065                                 buf[len++] =
5066                                     (sdinfo->satadrv_capacity >> 40) & 0xff;
5067                                 buf[len++] =
5068                                     (sdinfo->satadrv_capacity >> 32) & 0xff;
5069 #endif
5070                                 buf[len++] =
5071                                     (sdinfo->satadrv_capacity >> 24) & 0xff;
5072                                 buf[len++] =
5073                                     (sdinfo->satadrv_capacity >> 16) & 0xff;
5074                                 buf[len++] =
5075                                     (sdinfo->satadrv_capacity >> 8) & 0xff;
5076                                 buf[len++] = sdinfo->satadrv_capacity & 0xff;
5077                                 buf[len++] = 0;
5078                                 buf[len++] = 0; /* density code */
5079                                 buf[len++] = 0;
5080                                 buf[len++] = 0;
5081                                 if (sdinfo->satadrv_type ==
5082                                     SATA_DTYPE_ATADISK)
5083                                         buf[len++] = 2;
5084                                 else
5085                                         /* ATAPI */
5086                                         buf[len++] = 8;
5087                                 buf[len++] = 0;
5088                         }
5089                 }
5090 
5091                 sata_id = &sdinfo->satadrv_id;
5092 
5093                 /*
5094                  * Add requested pages.
5095                  * Page 3 and 4 are obsolete and we are not supporting them.
5096                  * We deal now with:
5097                  * caching (read/write cache control).
5098                  * We should eventually deal with following mode pages:
5099                  * error recovery  (0x01),
5100                  * power condition (0x1a),
5101                  * exception control page (enables SMART) (0x1c),
5102                  * enclosure management (ses),
5103                  * protocol-specific port mode (port control).
5104                  */
5105                 switch (scsipkt->pkt_cdbp[2] & 0x3f) {
5106                 case MODEPAGE_RW_ERRRECOV:
5107                         /* DAD_MODE_ERR_RECOV */
5108                         /* R/W recovery */
5109                         len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5110                         break;
5111                 case MODEPAGE_CACHING:
5112                         /* DAD_MODE_CACHE */
5113                         /* Reject not supported request for saved parameters */
5114                         if (pc == 3) {
5115                                 *scsipkt->pkt_scbp = STATUS_CHECK;
5116                                 sense = sata_arq_sense(spx);
5117                                 sense->es_key = KEY_ILLEGAL_REQUEST;
5118                                 sense->es_add_code =
5119                                     SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
5120                                 goto done;
5121                         }
5122 
5123                         /* caching */
5124                         len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5125                         break;
5126                 case MODEPAGE_INFO_EXCPT:
5127                         /* exception cntrl */
5128                         if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5129                                 len += sata_build_msense_page_1c(sdinfo, pc,
5130                                     buf+len);
5131                         }
5132                         else
5133                                 goto err;
5134                         break;
5135                 case MODEPAGE_POWER_COND:
5136                         /* DAD_MODE_POWER_COND */
5137                         /* power condition */
5138                         len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5139                         break;
5140 
5141                 case MODEPAGE_ACOUSTIC_MANAG:
5142                         /* acoustic management */
5143                         len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5144                         break;
5145                 case MODEPAGE_ALLPAGES:
5146                         /* all pages */
5147                         len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5148                         len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5149                         len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5150                         if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5151                                 len += sata_build_msense_page_1c(sdinfo, pc,
5152                                     buf+len);
5153                         }
5154                         len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5155                         break;
5156                 default:
5157                 err:
5158                         /* Invalid request */
5159                         *scsipkt->pkt_scbp = STATUS_CHECK;
5160                         sense = sata_arq_sense(spx);
5161                         sense->es_key = KEY_ILLEGAL_REQUEST;
5162                         sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5163                         goto done;
5164                 }
5165 
5166                 /* fix total mode data length */
5167                 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5168                         /* 4-byte mode parameter header */
5169                         buf[0] = len - 1;       /* mode data length */
5170                 } else {
5171                         buf[0] = (len -2) >> 8;
5172                         buf[1] = (len -2) & 0xff;
5173                 }
5174 
5175 
5176                 /* Check allocation length */
5177                 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5178                         alc_len = scsipkt->pkt_cdbp[4];
5179                 } else {
5180                         alc_len = scsipkt->pkt_cdbp[7];
5181                         alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
5182                 }
5183                 /*
5184                  * We do not check for possible parameters truncation
5185                  * (alc_len < len) assuming that the target driver works
5186                  * correctly. Just avoiding overrun.
5187                  * Copy no more than requested and possible, buffer-wise.
5188                  */
5189                 count = MIN(alc_len, len);
5190                 count = MIN(bp->b_bcount, count);
5191                 bcopy(buf, bp->b_un.b_addr, count);
5192 
5193                 scsipkt->pkt_state |= STATE_XFERRED_DATA;
5194                 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
5195         }
5196         *scsipkt->pkt_scbp = STATUS_GOOD;
5197 done:
5198         mutex_exit(cport_mutex);
5199         (void) kmem_free(buf, 1024);
5200 
5201         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5202             "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5203 
5204         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5205             scsipkt->pkt_comp != NULL) {
5206                 /* scsi callback required */
5207                 if (servicing_interrupt()) {
5208                         if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5209                             (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5210                             (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
5211                                 return (TRAN_BUSY);
5212                         }
5213                 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5214                     (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5215                     (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
5216                         /* Scheduling the callback failed */
5217                         return (TRAN_BUSY);
5218                 }
5219         }
5220 
5221         return (TRAN_ACCEPT);
5222 }
5223 
5224 
5225 /*
5226  * SATA translate command: Mode Select.
5227  * Translated into appropriate SATA command or emulated.
5228  * Saving parameters is not supported.
5229  * Changing device capacity is not supported (although theoretically
5230  * possible by executing SET FEATURES/SET MAX ADDRESS)
5231  *
5232  * Assumption is that the target driver is working correctly.
5233  *
5234  * More than one SATA command may be executed to perform operations specified
5235  * by mode select pages. The first error terminates further execution.
5236  * Operations performed successully are not backed-up in such case.
5237  *
5238  * NOTE: Implemented pages:
5239  * - caching page
5240  * - informational exception page
5241  * - acoustic management page
5242  * - power condition page
5243  * Caching setup is remembered so it could be re-stored in case of
5244  * an unexpected device reset.
5245  *
5246  * Returns TRAN_XXXX.
5247  * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields.
5248  */
5249 
5250 static int
5251 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
5252 {
5253         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5254         struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5255         struct scsi_extended_sense *sense;
5256         int len, pagelen, count, pllen;
5257         uint8_t *buf;   /* mode select buffer */
5258         int rval, stat, reason;
5259         uint_t nointr_flag;
5260         int dmod = 0;
5261         kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5262 
5263         SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5264             "sata_txlt_mode_select, pc %x page code 0x%02x\n",
5265             spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5266             spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5267 
5268         mutex_enter(cport_mutex);
5269 
5270         if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5271             TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5272                 mutex_exit(cport_mutex);
5273                 return (rval);
5274         }
5275 
5276         rval = TRAN_ACCEPT;
5277 
5278         scsipkt->pkt_reason = CMD_CMPLT;
5279         scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5280             STATE_SENT_CMD | STATE_GOT_STATUS;
5281         nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
5282 
5283         /* Reject not supported request */
5284         if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
5285                 *scsipkt->pkt_scbp = STATUS_CHECK;
5286                 sense = sata_arq_sense(spx);
5287                 sense->es_key = KEY_ILLEGAL_REQUEST;
5288                 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5289                 goto done;
5290         }
5291 
5292         if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5293                 pllen = scsipkt->pkt_cdbp[4];
5294         } else {
5295                 pllen = scsipkt->pkt_cdbp[7];
5296                 pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
5297         }
5298 
5299         *scsipkt->pkt_scbp = STATUS_GOOD;    /* Presumed outcome */
5300 
5301         if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
5302                 buf = (uint8_t *)bp->b_un.b_addr;
5303                 count = MIN(bp->b_bcount, pllen);
5304                 scsipkt->pkt_state |= STATE_XFERRED_DATA;
5305                 scsipkt->pkt_resid = 0;
5306                 pllen = count;
5307 
5308                 /*
5309                  * Check the header to skip the block descriptor(s) - we
5310                  * do not support setting device capacity.
5311                  * Existing macros do not recognize long LBA dscriptor,
5312                  * hence manual calculation.
5313                  */
5314                 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5315                         /* 6-bytes CMD, 4 bytes header */
5316                         if (count <= 4)
5317                                 goto done;              /* header only */
5318                         len = buf[3] + 4;
5319                 } else {
5320                         /* 10-bytes CMD, 8 bytes header */
5321                         if (count <= 8)
5322                                 goto done;              /* header only */
5323                         len = buf[6];
5324                         len = (len << 8) + buf[7] + 8;
5325                 }
5326                 if (len >= count)
5327                         goto done;      /* header + descriptor(s) only */
5328 
5329                 pllen -= len;           /* remaining data length */
5330 
5331                 /*
5332                  * We may be executing SATA command and want to execute it
5333                  * in SYNCH mode, regardless of scsi_pkt setting.
5334                  * Save scsi_pkt setting and indicate SYNCH mode
5335                  */
5336                 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5337                     scsipkt->pkt_comp != NULL) {
5338                         scsipkt->pkt_flags |= FLAG_NOINTR;
5339                 }
5340                 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
5341 
5342                 /*
5343                  * len is now the offset to a first mode select page
5344                  * Process all pages
5345                  */
5346                 while (pllen > 0) {
5347                         switch ((int)buf[len]) {
5348                         case MODEPAGE_CACHING:
5349                                 /* No support for SP (saving) */
5350                                 if (scsipkt->pkt_cdbp[1] & 0x01) {
5351                                         *scsipkt->pkt_scbp = STATUS_CHECK;
5352                                         sense = sata_arq_sense(spx);
5353                                         sense->es_key = KEY_ILLEGAL_REQUEST;
5354                                         sense->es_add_code =
5355                                             SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5356                                         goto done;
5357                                 }
5358                                 stat = sata_mode_select_page_8(spx,
5359                                     (struct mode_cache_scsi3 *)&buf[len],
5360                                     pllen, &pagelen, &rval, &dmod);
5361                                 /*
5362                                  * The pagelen value indicates the number of
5363                                  * parameter bytes already processed.
5364                                  * The rval is the return value from
5365                                  * sata_tran_start().
5366                                  * The stat indicates the overall status of
5367                                  * the operation(s).
5368                                  */
5369                                 if (stat != SATA_SUCCESS)
5370                                         /*
5371                                          * Page processing did not succeed -
5372                                          * all error info is already set-up,
5373                                          * just return
5374                                          */
5375                                         pllen = 0; /* this breaks the loop */
5376                                 else {
5377                                         len += pagelen;
5378                                         pllen -= pagelen;
5379                                 }
5380                                 break;
5381 
5382                         case MODEPAGE_INFO_EXCPT:
5383                                 stat = sata_mode_select_page_1c(spx,
5384                                     (struct mode_info_excpt_page *)&buf[len],
5385                                     pllen, &pagelen, &rval, &dmod);
5386                                 /*
5387                                  * The pagelen value indicates the number of
5388                                  * parameter bytes already processed.
5389                                  * The rval is the return value from
5390                                  * sata_tran_start().
5391                                  * The stat indicates the overall status of
5392                                  * the operation(s).
5393                                  */
5394                                 if (stat != SATA_SUCCESS)
5395                                         /*
5396                                          * Page processing did not succeed -
5397                                          * all error info is already set-up,
5398                                          * just return
5399                                          */
5400                                         pllen = 0; /* this breaks the loop */
5401                                 else {
5402                                         len += pagelen;
5403                                         pllen -= pagelen;
5404                                 }
5405                                 break;
5406 
5407                         case MODEPAGE_ACOUSTIC_MANAG:
5408                                 stat = sata_mode_select_page_30(spx,
5409                                     (struct mode_acoustic_management *)
5410                                     &buf[len], pllen, &pagelen, &rval, &dmod);
5411                                 /*
5412                                  * The pagelen value indicates the number of
5413                                  * parameter bytes already processed.
5414                                  * The rval is the return value from
5415                                  * sata_tran_start().
5416                                  * The stat indicates the overall status of
5417                                  * the operation(s).
5418                                  */
5419                                 if (stat != SATA_SUCCESS)
5420                                         /*
5421                                          * Page processing did not succeed -
5422                                          * all error info is already set-up,
5423                                          * just return
5424                                          */
5425                                         pllen = 0; /* this breaks the loop */
5426                                 else {
5427                                         len += pagelen;
5428                                         pllen -= pagelen;
5429                                 }
5430 
5431                                 break;
5432                         case MODEPAGE_POWER_COND:
5433                                 stat = sata_mode_select_page_1a(spx,
5434                                     (struct mode_info_power_cond *)&buf[len],
5435                                     pllen, &pagelen, &rval, &dmod);
5436                                 /*
5437                                  * The pagelen value indicates the number of
5438                                  * parameter bytes already processed.
5439                                  * The rval is the return value from
5440                                  * sata_tran_start().
5441                                  * The stat indicates the overall status of
5442                                  * the operation(s).
5443                                  */
5444                                 if (stat != SATA_SUCCESS)
5445                                         /*
5446                                          * Page processing did not succeed -
5447                                          * all error info is already set-up,
5448                                          * just return
5449                                          */
5450                                         pllen = 0; /* this breaks the loop */
5451                                 else {
5452                                         len += pagelen;
5453                                         pllen -= pagelen;
5454                                 }
5455                                 break;
5456                         default:
5457                                 *scsipkt->pkt_scbp = STATUS_CHECK;
5458                                 sense = sata_arq_sense(spx);
5459                                 sense->es_key = KEY_ILLEGAL_REQUEST;
5460                                 sense->es_add_code =
5461                                     SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
5462                                 goto done;
5463                         }
5464                 }
5465         }
5466 done:
5467         mutex_exit(cport_mutex);
5468         /*
5469          * If device parameters were modified, fetch and store the new
5470          * Identify Device data. Since port mutex could have been released
5471          * for accessing HBA driver, we need to re-check device existence.
5472          */
5473         if (dmod != 0) {
5474                 sata_drive_info_t new_sdinfo, *sdinfo;
5475                 int rv = 0;
5476 
5477                 /*
5478                  * Following statement has to be changed if this function is
5479                  * used for devices other than SATA hard disks.
5480                  */
5481                 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
5482 
5483                 new_sdinfo.satadrv_addr =
5484                     spx->txlt_sata_pkt->satapkt_device.satadev_addr;
5485                 rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
5486                     &new_sdinfo);
5487 
5488                 mutex_enter(cport_mutex);
5489                 /*
5490                  * Since port mutex could have been released when
5491                  * accessing HBA driver, we need to re-check that the
5492                  * framework still holds the device info structure.
5493                  */
5494                 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5495                     &spx->txlt_sata_pkt->satapkt_device);
5496                 if (sdinfo != NULL) {
5497                         /*
5498                          * Device still has info structure in the
5499                          * sata framework. Copy newly fetched info
5500                          */
5501                         if (rv == 0) {
5502                                 sdinfo->satadrv_id = new_sdinfo.satadrv_id;
5503                                 sata_save_drive_settings(sdinfo);
5504                         } else {
5505                                 /*
5506                                  * Could not fetch new data - invalidate
5507                                  * sata_drive_info. That makes device
5508                                  * unusable.
5509                                  */
5510                                 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
5511                                 sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
5512                         }
5513                 }
5514                 if (rv != 0 || sdinfo == NULL) {
5515                         /*
5516                          * This changes the overall mode select completion
5517                          * reason to a failed one !!!!!
5518                          */
5519                         *scsipkt->pkt_scbp = STATUS_CHECK;
5520                         sense = sata_arq_sense(spx);
5521                         scsipkt->pkt_reason = CMD_INCOMPLETE;
5522                         rval = TRAN_ACCEPT;
5523                 }
5524                 mutex_exit(cport_mutex);
5525         }
5526         /* Restore the scsi pkt flags */
5527         scsipkt->pkt_flags &= ~FLAG_NOINTR;
5528         scsipkt->pkt_flags |= nointr_flag;
5529 
5530         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5531             "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5532 
5533         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5534             scsipkt->pkt_comp != NULL) {
5535                 /* scsi callback required */
5536                 if (servicing_interrupt()) {
5537                         if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5538                             (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5539                             (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
5540                                 return (TRAN_BUSY);
5541                         }
5542                 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5543                     (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5544                     (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
5545                         /* Scheduling the callback failed */
5546                         return (TRAN_BUSY);
5547                 }
5548         }
5549 
5550         return (rval);
5551 }
5552 
5553 /*
5554  * Translate command: ATA Pass Through
5555  * Incomplete implementation.  Only supports No-Data, PIO Data-In, and
5556  * PIO Data-Out protocols.  Also supports CK_COND bit.
5557  *
5558  * Mapping of the incoming CDB bytes to the outgoing satacmd bytes is
5559  * described in Table 111 of SAT-2 (Draft 9).
5560  */
5561 static  int
5562 sata_txlt_ata_pass_thru(sata_pkt_txlate_t *spx)
5563 {
5564         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5565         sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5566         struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5567         int extend;
5568         uint64_t lba;
5569         uint16_t feature, sec_count;
5570         int t_len, synch;
5571         int rval, reason;
5572         kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5573 
5574         mutex_enter(cport_mutex);
5575 
5576         rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
5577         if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5578                 mutex_exit(cport_mutex);
5579                 return (rval);
5580         }
5581 
5582         /* T_DIR bit */
5583         if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_T_DIR)
5584                 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
5585         else
5586                 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5587 
5588         /* MULTIPLE_COUNT field.  If non-zero, invalid command (for now). */
5589         if (((scsipkt->pkt_cdbp[1] >> 5) & 0x7) != 0) {
5590                 mutex_exit(cport_mutex);
5591                 return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5592         }
5593 
5594         /* OFFLINE field. If non-zero, invalid command (for now). */
5595         if (((scsipkt->pkt_cdbp[2] >> 6) & 0x3) != 0) {
5596                 mutex_exit(cport_mutex);
5597                 return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5598         }
5599 
5600         /* PROTOCOL field */
5601         switch ((scsipkt->pkt_cdbp[1] >> 1) & 0xf) {
5602         case SATL_APT_P_HW_RESET:
5603         case SATL_APT_P_SRST:
5604         case SATL_APT_P_DMA:
5605         case SATL_APT_P_DMA_QUEUED:
5606         case SATL_APT_P_DEV_DIAG:
5607         case SATL_APT_P_DEV_RESET:
5608         case SATL_APT_P_UDMA_IN:
5609         case SATL_APT_P_UDMA_OUT:
5610         case SATL_APT_P_FPDMA:
5611         case SATL_APT_P_RET_RESP:
5612                 /* Not yet implemented */
5613         default:
5614                 mutex_exit(cport_mutex);
5615                 return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5616 
5617         case SATL_APT_P_NON_DATA:
5618                 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
5619                 break;
5620 
5621         case SATL_APT_P_PIO_DATA_IN:
5622                 /* If PROTOCOL disagrees with T_DIR, invalid command */
5623                 if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_WRITE) {
5624                         mutex_exit(cport_mutex);
5625                         return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5626                 }
5627 
5628                 /* if there is a buffer, release its DMA resources */
5629                 if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5630                         sata_scsi_dmafree(NULL, scsipkt);
5631                 } else {
5632                         /* if there is no buffer, how do you PIO in? */
5633                         mutex_exit(cport_mutex);
5634                         return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5635                 }
5636 
5637                 break;
5638 
5639         case SATL_APT_P_PIO_DATA_OUT:
5640                 /* If PROTOCOL disagrees with T_DIR, invalid command */
5641                 if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_READ) {
5642                         mutex_exit(cport_mutex);
5643                         return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5644                 }
5645 
5646                 /* if there is a buffer, release its DMA resources */
5647                 if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5648                         sata_scsi_dmafree(NULL, scsipkt);
5649                 } else {
5650                         /* if there is no buffer, how do you PIO out? */
5651                         mutex_exit(cport_mutex);
5652                         return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5653                 }
5654 
5655                 break;
5656         }
5657 
5658         /* Parse the ATA cmd fields, transfer some straight to the satacmd */
5659         switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5660         case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
5661                 feature = scsipkt->pkt_cdbp[3];
5662 
5663                 sec_count = scsipkt->pkt_cdbp[4];
5664 
5665                 lba = scsipkt->pkt_cdbp[8] & 0xf;
5666                 lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5667                 lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5668                 lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5669 
5670                 scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] & 0xf0;
5671                 scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[9];
5672 
5673                 break;
5674 
5675         case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
5676                 if (scsipkt->pkt_cdbp[1] & SATL_APT_BM_EXTEND) {
5677                         extend = 1;
5678 
5679                         feature = scsipkt->pkt_cdbp[3];
5680                         feature = (feature << 8) | scsipkt->pkt_cdbp[4];
5681 
5682                         sec_count = scsipkt->pkt_cdbp[5];
5683                         sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[6];
5684 
5685                         lba = scsipkt->pkt_cdbp[11];
5686                         lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5687                         lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5688                         lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5689                         lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5690                         lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5691 
5692                         scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13];
5693                         scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5694                 } else {
5695                         feature = scsipkt->pkt_cdbp[3];
5696 
5697                         sec_count = scsipkt->pkt_cdbp[5];
5698 
5699                         lba = scsipkt->pkt_cdbp[13] & 0xf;
5700                         lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5701                         lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5702                         lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5703 
5704                         scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] &
5705                             0xf0;
5706                         scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5707                 }
5708 
5709                 break;
5710         }
5711 
5712         /* CK_COND bit */
5713         if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
5714                 if (extend) {
5715                         scmd->satacmd_flags.sata_copy_out_sec_count_msb = 1;
5716                         scmd->satacmd_flags.sata_copy_out_lba_low_msb = 1;
5717                         scmd->satacmd_flags.sata_copy_out_lba_mid_msb = 1;
5718                         scmd->satacmd_flags.sata_copy_out_lba_high_msb = 1;
5719                 }
5720 
5721                 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
5722                 scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
5723                 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
5724                 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
5725                 scmd->satacmd_flags.sata_copy_out_device_reg = 1;
5726                 scmd->satacmd_flags.sata_copy_out_error_reg = 1;
5727         }
5728 
5729         /* Transfer remaining parsed ATA cmd values to the satacmd */
5730         if (extend) {
5731                 scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5732 
5733                 scmd->satacmd_features_reg_ext = (feature >> 8) & 0xff;
5734                 scmd->satacmd_sec_count_msb = (sec_count >> 8) & 0xff;
5735                 scmd->satacmd_lba_low_msb = (lba >> 8) & 0xff;
5736                 scmd->satacmd_lba_mid_msb = (lba >> 8) & 0xff;
5737                 scmd->satacmd_lba_high_msb = lba >> 40;
5738         } else {
5739                 scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5740 
5741                 scmd->satacmd_features_reg_ext = 0;
5742                 scmd->satacmd_sec_count_msb = 0;
5743                 scmd->satacmd_lba_low_msb = 0;
5744                 scmd->satacmd_lba_mid_msb = 0;
5745                 scmd->satacmd_lba_high_msb = 0;
5746         }
5747 
5748         scmd->satacmd_features_reg = feature & 0xff;
5749         scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5750         scmd->satacmd_lba_low_lsb = lba & 0xff;
5751         scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5752         scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5753 
5754         /* Determine transfer length */
5755         switch (scsipkt->pkt_cdbp[2] & 0x3) {            /* T_LENGTH field */
5756         case 1:
5757                 t_len = feature;
5758                 break;
5759         case 2:
5760                 t_len = sec_count;
5761                 break;
5762         default:
5763                 t_len = 0;
5764                 break;
5765         }
5766 
5767         /* Adjust transfer length for the Byte Block bit */
5768         if ((scsipkt->pkt_cdbp[2] >> 2) & 1)
5769                 t_len *= SATA_DISK_SECTOR_SIZE;
5770 
5771         /* Start processing command */
5772         if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5773                 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_apt_completion;
5774                 synch = FALSE;
5775         } else {
5776                 synch = TRUE;
5777         }
5778 
5779         if (sata_hba_start(spx, &rval) != 0) {
5780                 mutex_exit(cport_mutex);
5781                 return (rval);
5782         }
5783 
5784         mutex_exit(cport_mutex);
5785 
5786         if (synch) {
5787                 sata_txlt_apt_completion(spx->txlt_sata_pkt);
5788         }
5789 
5790         return (TRAN_ACCEPT);
5791 }
5792 
5793 /*
5794  * Translate command: Log Sense
5795  */
5796 static  int
5797 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
5798 {
5799         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5800         struct buf      *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5801         sata_drive_info_t *sdinfo;
5802         struct scsi_extended_sense *sense;
5803         int             len, count, alc_len;
5804         int             pc;     /* Page Control code */
5805         int             page_code;      /* Page code */
5806         uint8_t         *buf;   /* log sense buffer */
5807         int             rval, reason;
5808 #define MAX_LOG_SENSE_PAGE_SIZE 512
5809         kmutex_t        *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5810 
5811         SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5812             "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
5813             spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5814             spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5815 
5816         if (servicing_interrupt()) {
5817                 buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_NOSLEEP);
5818                 if (buf == NULL) {
5819                         return (TRAN_BUSY);
5820                 }
5821         } else {
5822                 buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
5823         }
5824 
5825         mutex_enter(cport_mutex);
5826 
5827         if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5828             TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5829                 mutex_exit(cport_mutex);
5830                 kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5831                 return (rval);
5832         }
5833 
5834         scsipkt->pkt_reason = CMD_CMPLT;
5835         scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5836             STATE_SENT_CMD | STATE_GOT_STATUS;
5837 
5838         pc = scsipkt->pkt_cdbp[2] >> 6;
5839         page_code = scsipkt->pkt_cdbp[2] & 0x3f;
5840 
5841         /* Reject not supported request for all but cumulative values */
5842         switch (pc) {
5843         case PC_CUMULATIVE_VALUES:
5844                 break;
5845         default:
5846                 *scsipkt->pkt_scbp = STATUS_CHECK;
5847                 sense = sata_arq_sense(spx);
5848                 sense->es_key = KEY_ILLEGAL_REQUEST;
5849                 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5850                 goto done;
5851         }
5852 
5853         switch (page_code) {
5854         case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5855         case PAGE_CODE_SELF_TEST_RESULTS:
5856         case PAGE_CODE_INFORMATION_EXCEPTIONS:
5857         case PAGE_CODE_SMART_READ_DATA:
5858         case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5859                 break;
5860         default:
5861                 *scsipkt->pkt_scbp = STATUS_CHECK;
5862                 sense = sata_arq_sense(spx);
5863                 sense->es_key = KEY_ILLEGAL_REQUEST;
5864                 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5865                 goto done;
5866         }
5867 
5868         if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
5869                 /*
5870                  * Because log sense uses local buffers for data retrieval from
5871                  * the devices and sets the data programatically in the
5872                  * original specified buffer, release preallocated DMA
5873                  * resources before storing data in the original buffer,
5874                  * so no unwanted DMA sync would take place.
5875                  */
5876                 sata_id_t *sata_id;
5877 
5878                 sata_scsi_dmafree(NULL, scsipkt);
5879 
5880                 len = 0;
5881 
5882                 /* Build log parameter header */
5883                 buf[len++] = page_code; /* page code as in the CDB */
5884                 buf[len++] = 0;         /* reserved */
5885                 buf[len++] = 0;         /* Zero out page length for now (MSB) */
5886                 buf[len++] = 0;         /* (LSB) */
5887 
5888                 sdinfo = sata_get_device_info(
5889                     spx->txlt_sata_hba_inst,
5890                     &spx->txlt_sata_pkt->satapkt_device);
5891 
5892                 /*
5893                  * Add requested pages.
5894                  */
5895                 switch (page_code) {
5896                 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5897                         len = sata_build_lsense_page_0(sdinfo, buf + len);
5898                         break;
5899                 case PAGE_CODE_SELF_TEST_RESULTS:
5900                         sata_id = &sdinfo->satadrv_id;
5901                         if ((! (sata_id->ai_cmdset84 &
5902                             SATA_SMART_SELF_TEST_SUPPORTED)) ||
5903                             (! (sata_id->ai_features87 &
5904                             SATA_SMART_SELF_TEST_SUPPORTED))) {
5905                                 *scsipkt->pkt_scbp = STATUS_CHECK;
5906                                 sense = sata_arq_sense(spx);
5907                                 sense->es_key = KEY_ILLEGAL_REQUEST;
5908                                 sense->es_add_code =
5909                                     SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5910 
5911                                 goto done;
5912                         }
5913                         len = sata_build_lsense_page_10(sdinfo, buf + len,
5914                             spx->txlt_sata_hba_inst);
5915                         break;
5916                 case PAGE_CODE_INFORMATION_EXCEPTIONS:
5917                         sata_id = &sdinfo->satadrv_id;
5918                         if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5919                                 *scsipkt->pkt_scbp = STATUS_CHECK;
5920                                 sense = sata_arq_sense(spx);
5921                                 sense->es_key = KEY_ILLEGAL_REQUEST;
5922                                 sense->es_add_code =
5923                                     SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5924 
5925                                 goto done;
5926                         }
5927                         if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5928                                 *scsipkt->pkt_scbp = STATUS_CHECK;
5929                                 sense = sata_arq_sense(spx);
5930                                 sense->es_key = KEY_ABORTED_COMMAND;
5931                                 sense->es_add_code =
5932                                     SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5933                                 sense->es_qual_code =
5934                                     SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5935 
5936                                 goto done;
5937                         }
5938 
5939                         len = sata_build_lsense_page_2f(sdinfo, buf + len,
5940                             spx->txlt_sata_hba_inst);
5941                         break;
5942                 case PAGE_CODE_SMART_READ_DATA:
5943                         sata_id = &sdinfo->satadrv_id;
5944                         if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5945                                 *scsipkt->pkt_scbp = STATUS_CHECK;
5946                                 sense = sata_arq_sense(spx);
5947                                 sense->es_key = KEY_ILLEGAL_REQUEST;
5948                                 sense->es_add_code =
5949                                     SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5950 
5951                                 goto done;
5952                         }
5953                         if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5954                                 *scsipkt->pkt_scbp = STATUS_CHECK;
5955                                 sense = sata_arq_sense(spx);
5956                                 sense->es_key = KEY_ABORTED_COMMAND;
5957                                 sense->es_add_code =
5958                                     SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5959                                 sense->es_qual_code =
5960                                     SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5961 
5962                                 goto done;
5963                         }
5964 
5965                         /* This page doesn't include a page header */
5966                         len = sata_build_lsense_page_30(sdinfo, buf,
5967                             spx->txlt_sata_hba_inst);
5968                         goto no_header;
5969                 case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5970                         sata_id = &sdinfo->satadrv_id;
5971                         if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5972                                 *scsipkt->pkt_scbp = STATUS_CHECK;
5973                                 sense = sata_arq_sense(spx);
5974                                 sense->es_key = KEY_ILLEGAL_REQUEST;
5975                                 sense->es_add_code =
5976                                     SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5977 
5978                                 goto done;
5979                         }
5980                         if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5981                                 *scsipkt->pkt_scbp = STATUS_CHECK;
5982                                 sense = sata_arq_sense(spx);
5983                                 sense->es_key = KEY_ABORTED_COMMAND;
5984                                 sense->es_add_code =
5985                                     SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5986                                 sense->es_qual_code =
5987                                     SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5988 
5989                                 goto done;
5990                         }
5991                         len = sata_build_lsense_page_0e(sdinfo, buf, spx);
5992                         goto no_header;
5993                 default:
5994                         /* Invalid request */
5995                         *scsipkt->pkt_scbp = STATUS_CHECK;
5996                         sense = sata_arq_sense(spx);
5997                         sense->es_key = KEY_ILLEGAL_REQUEST;
5998                         sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5999                         goto done;
6000                 }
6001 
6002                 /* set parameter log sense data length */
6003                 buf[2] = len >> 8;        /* log sense length (MSB) */
6004                 buf[3] = len & 0xff;        /* log sense length (LSB) */
6005 
6006                 len += SCSI_LOG_PAGE_HDR_LEN;
6007                 ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
6008 
6009 no_header:
6010                 /* Check allocation length */
6011                 alc_len = scsipkt->pkt_cdbp[7];
6012                 alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
6013 
6014                 /*
6015                  * We do not check for possible parameters truncation
6016                  * (alc_len < len) assuming that the target driver works
6017                  * correctly. Just avoiding overrun.
6018                  * Copy no more than requested and possible, buffer-wise.
6019                  */
6020                 count = MIN(alc_len, len);
6021                 count = MIN(bp->b_bcount, count);
6022                 bcopy(buf, bp->b_un.b_addr, count);
6023 
6024                 scsipkt->pkt_state |= STATE_XFERRED_DATA;
6025                 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
6026         }
6027         *scsipkt->pkt_scbp = STATUS_GOOD;
6028 done:
6029         mutex_exit(cport_mutex);
6030         (void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
6031 
6032         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6033             "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6034 
6035         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6036             scsipkt->pkt_comp != NULL) {
6037                 /* scsi callback required */
6038                 if (servicing_interrupt()) {
6039                         if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6040                             (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6041                             (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
6042                                 return (TRAN_BUSY);
6043                         }
6044                 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6045                     (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6046                     (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
6047                         /* Scheduling the callback failed */
6048                         return (TRAN_BUSY);
6049                 }
6050         }
6051 
6052         return (TRAN_ACCEPT);
6053 }
6054 
6055 /*
6056  * Translate command: Log Select
6057  * Not implemented at this time - returns invalid command response.
6058  */
6059 static  int
6060 sata_txlt_log_select(sata_pkt_txlate_t *spx)
6061 {
6062         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6063             "sata_txlt_log_select\n", NULL);
6064 
6065         return (sata_txlt_invalid_command(spx));
6066 }
6067 
6068 
6069 /*
6070  * Translate command: Read (various types).
6071  * Translated into appropriate type of ATA READ command
6072  * for SATA hard disks.
6073  * Both the device capabilities and requested operation mode are
6074  * considered.
6075  *
6076  * Following scsi cdb fields are ignored:
6077  * rdprotect, dpo, fua, fua_nv, group_number.
6078  *
6079  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6080  * enable variable sata_func_enable), the capability of the controller and
6081  * capability of a device are checked and if both support queueing, read
6082  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
6083  * command rather than plain READ_XXX command.
6084  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6085  * both the controller and device suport such functionality, the read
6086  * request will be translated to READ_FPDMA_QUEUED command.
6087  * In both cases the maximum queue depth is derived as minimum of:
6088  * HBA capability,device capability and sata_max_queue_depth variable setting.
6089  * The value passed to HBA driver is decremented by 1, because only 5 bits are
6090  * used to pass max queue depth value, and the maximum possible queue depth
6091  * is 32.
6092  *
6093  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6094  * appropriate values in scsi_pkt fields.
6095  */
6096 static int
6097 sata_txlt_read(sata_pkt_txlate_t *spx)
6098 {
6099         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6100         sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6101         sata_drive_info_t *sdinfo;
6102         sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6103         kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6104         uint16_t sec_count;
6105         uint64_t lba;
6106         int rval, reason;
6107         int synch;
6108 
6109         mutex_enter(cport_mutex);
6110 
6111         if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6112             TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6113                 mutex_exit(cport_mutex);
6114                 return (rval);
6115         }
6116 
6117         sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6118             &spx->txlt_sata_pkt->satapkt_device);
6119 
6120         scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
6121         /*
6122          * Extract LBA and sector count from scsi CDB.
6123          */
6124         switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6125         case SCMD_READ:
6126                 /* 6-byte scsi read cmd : 0x08 */
6127                 lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6128                 lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6129                 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6130                 sec_count = scsipkt->pkt_cdbp[4];
6131                 /* sec_count 0 will be interpreted as 256 by a device */
6132                 break;
6133         case SCMD_READ_G1:
6134                 /* 10-bytes scsi read command : 0x28 */
6135                 lba = scsipkt->pkt_cdbp[2];
6136                 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6137                 lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6138                 lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6139                 sec_count = scsipkt->pkt_cdbp[7];
6140                 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6141                 break;
6142         case SCMD_READ_G5:
6143                 /* 12-bytes scsi read command : 0xA8 */
6144                 lba = scsipkt->pkt_cdbp[2];
6145                 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6146                 lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6147                 lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6148                 sec_count = scsipkt->pkt_cdbp[6];
6149                 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6150                 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6151                 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6152                 break;
6153         case SCMD_READ_G4:
6154                 /* 16-bytes scsi read command : 0x88 */
6155                 lba = scsipkt->pkt_cdbp[2];
6156                 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6157                 lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6158                 lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6159                 lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6160                 lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6161                 lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6162                 lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6163                 sec_count = scsipkt->pkt_cdbp[10];
6164                 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6165                 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6166                 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6167                 break;
6168         default:
6169                 /* Unsupported command */
6170                 mutex_exit(cport_mutex);
6171                 return (sata_txlt_invalid_command(spx));
6172         }
6173 
6174         /*
6175          * Check if specified address exceeds device capacity
6176          */
6177         if ((lba >= sdinfo->satadrv_capacity) ||
6178             ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6179                 /* LBA out of range */
6180                 mutex_exit(cport_mutex);
6181                 return (sata_txlt_lba_out_of_range(spx));
6182         }
6183 
6184         /*
6185          * For zero-length transfer, emulate good completion of the command
6186          * (reasons for rejecting the command were already checked).
6187          * No DMA resources were allocated.
6188          */
6189         if (spx->txlt_dma_cookie_list == NULL) {
6190                 mutex_exit(cport_mutex);
6191                 return (sata_emul_rw_completion(spx));
6192         }
6193 
6194         /*
6195          * Build cmd block depending on the device capability and
6196          * requested operation mode.
6197          * Do not bother with non-dma mode - we are working only with
6198          * devices supporting DMA.
6199          */
6200         scmd->satacmd_addr_type = ATA_ADDR_LBA;
6201         scmd->satacmd_device_reg = SATA_ADH_LBA;
6202         scmd->satacmd_cmd_reg = SATAC_READ_DMA;
6203         if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6204                 scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6205                 scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
6206                 scmd->satacmd_sec_count_msb = sec_count >> 8;
6207 #ifndef __lock_lint
6208                 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6209                 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6210                 scmd->satacmd_lba_high_msb = lba >> 40;
6211 #endif
6212         } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6213                 scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6214                 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6215         }
6216         scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6217         scmd->satacmd_lba_low_lsb = lba & 0xff;
6218         scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6219         scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6220         scmd->satacmd_features_reg = 0;
6221         scmd->satacmd_status_reg = 0;
6222         scmd->satacmd_error_reg = 0;
6223 
6224         /*
6225          * Check if queueing commands should be used and switch
6226          * to appropriate command if possible
6227          */
6228         if (sata_func_enable & SATA_ENABLE_QUEUING) {
6229                 boolean_t using_queuing;
6230 
6231                 /* Queuing supported by controller and device? */
6232                 if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6233                     (sdinfo->satadrv_features_support &
6234                     SATA_DEV_F_NCQ) &&
6235                     (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6236                     SATA_CTLF_NCQ)) {
6237                         using_queuing = B_TRUE;
6238 
6239                         /* NCQ supported - use FPDMA READ */
6240                         scmd->satacmd_cmd_reg =
6241                             SATAC_READ_FPDMA_QUEUED;
6242                         scmd->satacmd_features_reg_ext =
6243                             scmd->satacmd_sec_count_msb;
6244                         scmd->satacmd_sec_count_msb = 0;
6245                 } else if ((sdinfo->satadrv_features_support &
6246                     SATA_DEV_F_TCQ) &&
6247                     (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6248                     SATA_CTLF_QCMD)) {
6249                         using_queuing = B_TRUE;
6250 
6251                         /* Legacy queueing */
6252                         if (sdinfo->satadrv_features_support &
6253                             SATA_DEV_F_LBA48) {
6254                                 scmd->satacmd_cmd_reg =
6255                                     SATAC_READ_DMA_QUEUED_EXT;
6256                                 scmd->satacmd_features_reg_ext =
6257                                     scmd->satacmd_sec_count_msb;
6258                                 scmd->satacmd_sec_count_msb = 0;
6259                         } else {
6260                                 scmd->satacmd_cmd_reg =
6261                                     SATAC_READ_DMA_QUEUED;
6262                         }
6263                 } else  /* NCQ nor legacy queuing not supported */
6264                         using_queuing = B_FALSE;
6265 
6266                 /*
6267                  * If queuing, the sector count goes in the features register
6268                  * and the secount count will contain the tag.
6269                  */
6270                 if (using_queuing) {
6271                         scmd->satacmd_features_reg =
6272                             scmd->satacmd_sec_count_lsb;
6273                         scmd->satacmd_sec_count_lsb = 0;
6274                         scmd->satacmd_flags.sata_queued = B_TRUE;
6275 
6276                         /* Set-up maximum queue depth */
6277                         scmd->satacmd_flags.sata_max_queue_depth =
6278                             sdinfo->satadrv_max_queue_depth - 1;
6279                 } else if (sdinfo->satadrv_features_enabled &
6280                     SATA_DEV_F_E_UNTAGGED_QING) {
6281                         /*
6282                          * Although NCQ/TCQ is not enabled, untagged queuing
6283                          * may be still used.
6284                          * Set-up the maximum untagged queue depth.
6285                          * Use controller's queue depth from sata_hba_tran.
6286                          * SATA HBA drivers may ignore this value and rely on
6287                          * the internal limits.For drivers that do not
6288                          * ignore untaged queue depth, limit the value to
6289                          * SATA_MAX_QUEUE_DEPTH (32), as this is the
6290                          * largest value that can be passed via
6291                          * satacmd_flags.sata_max_queue_depth.
6292                          */
6293                         scmd->satacmd_flags.sata_max_queue_depth =
6294                             SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6295                             SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6296 
6297                 } else {
6298                         scmd->satacmd_flags.sata_max_queue_depth = 0;
6299                 }
6300         } else
6301                 scmd->satacmd_flags.sata_max_queue_depth = 0;
6302 
6303         SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
6304             "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
6305             scmd->satacmd_cmd_reg, lba, sec_count);
6306 
6307         if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6308                 /* Need callback function */
6309                 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6310                 synch = FALSE;
6311         } else
6312                 synch = TRUE;
6313 
6314         /* Transfer command to HBA */
6315         if (sata_hba_start(spx, &rval) != 0) {
6316                 /* Pkt not accepted for execution */
6317                 mutex_exit(cport_mutex);
6318                 return (rval);
6319         }
6320         mutex_exit(cport_mutex);
6321         /*
6322          * If execution is non-synchronous,
6323          * a callback function will handle potential errors, translate
6324          * the response and will do a callback to a target driver.
6325          * If it was synchronous, check execution status using the same
6326          * framework callback.
6327          */
6328         if (synch) {
6329                 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6330                     "synchronous execution status %x\n",
6331                     spx->txlt_sata_pkt->satapkt_reason);
6332                 sata_txlt_rw_completion(spx->txlt_sata_pkt);
6333         }
6334         return (TRAN_ACCEPT);
6335 }
6336 
6337 
6338 /*
6339  * SATA translate command: Write (various types)
6340  * Translated into appropriate type of ATA WRITE command
6341  * for SATA hard disks.
6342  * Both the device capabilities and requested operation mode are
6343  * considered.
6344  *
6345  * Following scsi cdb fields are ignored:
6346  * rwprotect, dpo, fua, fua_nv, group_number.
6347  *
6348  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6349  * enable variable sata_func_enable), the capability of the controller and
6350  * capability of a device are checked and if both support queueing, write
6351  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
6352  * command rather than plain WRITE_XXX command.
6353  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6354  * both the controller and device suport such functionality, the write
6355  * request will be translated to WRITE_FPDMA_QUEUED command.
6356  * In both cases the maximum queue depth is derived as minimum of:
6357  * HBA capability,device capability and sata_max_queue_depth variable setting.
6358  * The value passed to HBA driver is decremented by 1, because only 5 bits are
6359  * used to pass max queue depth value, and the maximum possible queue depth
6360  * is 32.
6361  *
6362  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6363  * appropriate values in scsi_pkt fields.
6364  */
6365 static int
6366 sata_txlt_write(sata_pkt_txlate_t *spx)
6367 {
6368         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6369         sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6370         sata_drive_info_t *sdinfo;
6371         sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6372         uint16_t sec_count;
6373         uint64_t lba;
6374         int rval, reason;
6375         int synch;
6376         kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6377 
6378         mutex_enter(cport_mutex);
6379 
6380         if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6381             TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6382                 mutex_exit(cport_mutex);
6383                 return (rval);
6384         }
6385 
6386         sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6387             &spx->txlt_sata_pkt->satapkt_device);
6388 
6389         scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6390         /*
6391          * Extract LBA and sector count from scsi CDB
6392          */
6393         switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6394         case SCMD_WRITE:
6395                 /* 6-byte scsi read cmd : 0x0A */
6396                 lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6397                 lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6398                 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6399                 sec_count = scsipkt->pkt_cdbp[4];
6400                 /* sec_count 0 will be interpreted as 256 by a device */
6401                 break;
6402         case SCMD_WRITE_G1:
6403                 /* 10-bytes scsi write command : 0x2A */
6404                 lba = scsipkt->pkt_cdbp[2];
6405                 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6406                 lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6407                 lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6408                 sec_count = scsipkt->pkt_cdbp[7];
6409                 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6410                 break;
6411         case SCMD_WRITE_G5:
6412                 /* 12-bytes scsi read command : 0xAA */
6413                 lba = scsipkt->pkt_cdbp[2];
6414                 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6415                 lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6416                 lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6417                 sec_count = scsipkt->pkt_cdbp[6];
6418                 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6419                 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6420                 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6421                 break;
6422         case SCMD_WRITE_G4:
6423                 /* 16-bytes scsi write command : 0x8A */
6424                 lba = scsipkt->pkt_cdbp[2];
6425                 lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6426                 lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6427                 lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6428                 lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6429                 lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6430                 lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6431                 lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6432                 sec_count = scsipkt->pkt_cdbp[10];
6433                 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6434                 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6435                 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6436                 break;
6437         default:
6438                 /* Unsupported command */
6439                 mutex_exit(cport_mutex);
6440                 return (sata_txlt_invalid_command(spx));
6441         }
6442 
6443         /*
6444          * Check if specified address and length exceeds device capacity
6445          */
6446         if ((lba >= sdinfo->satadrv_capacity) ||
6447             ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6448                 /* LBA out of range */
6449                 mutex_exit(cport_mutex);
6450                 return (sata_txlt_lba_out_of_range(spx));
6451         }
6452 
6453         /*
6454          * For zero-length transfer, emulate good completion of the command
6455          * (reasons for rejecting the command were already checked).
6456          * No DMA resources were allocated.
6457          */
6458         if (spx->txlt_dma_cookie_list == NULL) {
6459                 mutex_exit(cport_mutex);
6460                 return (sata_emul_rw_completion(spx));
6461         }
6462 
6463         /*
6464          * Build cmd block depending on the device capability and
6465          * requested operation mode.
6466          * Do not bother with non-dma mode- we are working only with
6467          * devices supporting DMA.
6468          */
6469         scmd->satacmd_addr_type = ATA_ADDR_LBA;
6470         scmd->satacmd_device_reg = SATA_ADH_LBA;
6471         scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
6472         if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6473                 scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6474                 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
6475                 scmd->satacmd_sec_count_msb = sec_count >> 8;
6476                 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6477 #ifndef __lock_lint
6478                 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6479                 scmd->satacmd_lba_high_msb = lba >> 40;
6480 #endif
6481         } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6482                 scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6483                 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6484         }
6485         scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6486         scmd->satacmd_lba_low_lsb = lba & 0xff;
6487         scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6488         scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6489         scmd->satacmd_features_reg = 0;
6490         scmd->satacmd_status_reg = 0;
6491         scmd->satacmd_error_reg = 0;
6492 
6493         /*
6494          * Check if queueing commands should be used and switch
6495          * to appropriate command if possible
6496          */
6497         if (sata_func_enable & SATA_ENABLE_QUEUING) {
6498                 boolean_t using_queuing;
6499 
6500                 /* Queuing supported by controller and device? */
6501                 if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6502                     (sdinfo->satadrv_features_support &
6503                     SATA_DEV_F_NCQ) &&
6504                     (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6505                     SATA_CTLF_NCQ)) {
6506                         using_queuing = B_TRUE;
6507 
6508                         /* NCQ supported - use FPDMA WRITE */
6509                         scmd->satacmd_cmd_reg =
6510                             SATAC_WRITE_FPDMA_QUEUED;
6511                         scmd->satacmd_features_reg_ext =
6512                             scmd->satacmd_sec_count_msb;
6513                         scmd->satacmd_sec_count_msb = 0;
6514                 } else if ((sdinfo->satadrv_features_support &
6515                     SATA_DEV_F_TCQ) &&
6516                     (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6517                     SATA_CTLF_QCMD)) {
6518                         using_queuing = B_TRUE;
6519 
6520                         /* Legacy queueing */
6521                         if (sdinfo->satadrv_features_support &
6522                             SATA_DEV_F_LBA48) {
6523                                 scmd->satacmd_cmd_reg =
6524                                     SATAC_WRITE_DMA_QUEUED_EXT;
6525                                 scmd->satacmd_features_reg_ext =
6526                                     scmd->satacmd_sec_count_msb;
6527                                 scmd->satacmd_sec_count_msb = 0;
6528                         } else {
6529                                 scmd->satacmd_cmd_reg =
6530                                     SATAC_WRITE_DMA_QUEUED;
6531                         }
6532                 } else  /*  NCQ nor legacy queuing not supported */
6533                         using_queuing = B_FALSE;
6534 
6535                 if (using_queuing) {
6536                         scmd->satacmd_features_reg =
6537                             scmd->satacmd_sec_count_lsb;
6538                         scmd->satacmd_sec_count_lsb = 0;
6539                         scmd->satacmd_flags.sata_queued = B_TRUE;
6540                         /* Set-up maximum queue depth */
6541                         scmd->satacmd_flags.sata_max_queue_depth =
6542                             sdinfo->satadrv_max_queue_depth - 1;
6543                 } else if (sdinfo->satadrv_features_enabled &
6544                     SATA_DEV_F_E_UNTAGGED_QING) {
6545                         /*
6546                          * Although NCQ/TCQ is not enabled, untagged queuing
6547                          * may be still used.
6548                          * Set-up the maximum untagged queue depth.
6549                          * Use controller's queue depth from sata_hba_tran.
6550                          * SATA HBA drivers may ignore this value and rely on
6551                          * the internal limits. For drivera that do not
6552                          * ignore untaged queue depth, limit the value to
6553                          * SATA_MAX_QUEUE_DEPTH (32), as this is the
6554                          * largest value that can be passed via
6555                          * satacmd_flags.sata_max_queue_depth.
6556                          */
6557                         scmd->satacmd_flags.sata_max_queue_depth =
6558                             SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6559                             SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6560 
6561                 } else {
6562                         scmd->satacmd_flags.sata_max_queue_depth = 0;
6563                 }
6564         } else
6565                 scmd->satacmd_flags.sata_max_queue_depth = 0;
6566 
6567         SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6568             "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
6569             scmd->satacmd_cmd_reg, lba, sec_count);
6570 
6571         if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6572                 /* Need callback function */
6573                 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6574                 synch = FALSE;
6575         } else
6576                 synch = TRUE;
6577 
6578         /* Transfer command to HBA */
6579         if (sata_hba_start(spx, &rval) != 0) {
6580                 /* Pkt not accepted for execution */
6581                 mutex_exit(cport_mutex);
6582                 return (rval);
6583         }
6584         mutex_exit(cport_mutex);
6585 
6586         /*
6587          * If execution is non-synchronous,
6588          * a callback function will handle potential errors, translate
6589          * the response and will do a callback to a target driver.
6590          * If it was synchronous, check execution status using the same
6591          * framework callback.
6592          */
6593         if (synch) {
6594                 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6595                     "synchronous execution status %x\n",
6596                     spx->txlt_sata_pkt->satapkt_reason);
6597                 sata_txlt_rw_completion(spx->txlt_sata_pkt);
6598         }
6599         return (TRAN_ACCEPT);
6600 }
6601 
6602 
6603 /*
6604  * Implements SCSI SBC WRITE BUFFER command download microcode option
6605  */
6606 static int
6607 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
6608 {
6609 #define WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE                   4
6610 #define WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE                     5
6611 
6612         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6613         struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
6614         sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6615 
6616         struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6617         struct scsi_extended_sense *sense;
6618         int rval, mode, sector_count, reason;
6619         kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6620 
6621         mode = scsipkt->pkt_cdbp[1] & 0x1f;
6622 
6623         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6624             "sata_txlt_write_buffer, mode 0x%x\n", mode);
6625 
6626         mutex_enter(cport_mutex);
6627 
6628         if ((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6629             TRAN_ACCEPT) {
6630                 mutex_exit(cport_mutex);
6631                 return (rval);
6632         }
6633 
6634         /* Use synchronous mode */
6635         spx->txlt_sata_pkt->satapkt_op_mode
6636             |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
6637 
6638         scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6639 
6640         scsipkt->pkt_reason = CMD_CMPLT;
6641         scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6642             STATE_SENT_CMD | STATE_GOT_STATUS;
6643 
6644         /*
6645          * The SCSI to ATA translation specification only calls
6646          * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
6647          * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
6648          * ATA 8 (draft) got rid of download microcode for temp
6649          * and it is even optional for ATA 7, so it may be aborted.
6650          * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
6651          * it is not specified and the buffer offset for SCSI is a 16-bit
6652          * value in bytes, but for ATA it is a 16-bit offset in 512 byte
6653          * sectors.  Thus the offset really doesn't buy us anything.
6654          * If and when ATA 8 is stabilized and the SCSI to ATA specification
6655          * is revised, this can be revisisted.
6656          */
6657         /* Reject not supported request */
6658         switch (mode) {
6659         case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
6660                 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
6661                 break;
6662         case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
6663                 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
6664                 break;
6665         default:
6666                 goto bad_param;
6667         }
6668 
6669         *scsipkt->pkt_scbp = STATUS_GOOD;    /* Presumed outcome */
6670 
6671         scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
6672         if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
6673                 goto bad_param;
6674         sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
6675         scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
6676         scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
6677         scmd->satacmd_lba_mid_lsb = 0;
6678         scmd->satacmd_lba_high_lsb = 0;
6679         scmd->satacmd_device_reg = 0;
6680         spx->txlt_sata_pkt->satapkt_comp = NULL;
6681         scmd->satacmd_addr_type = 0;
6682 
6683         /* Transfer command to HBA */
6684         if (sata_hba_start(spx, &rval) != 0) {
6685                 /* Pkt not accepted for execution */
6686                 mutex_exit(cport_mutex);
6687                 return (rval);
6688         }
6689 
6690         mutex_exit(cport_mutex);
6691 
6692         /* Then we need synchronous check the status of the disk */
6693         scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6694             STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
6695         if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6696                 scsipkt->pkt_reason = CMD_CMPLT;
6697 
6698                 /* Download commmand succeed, so probe and identify device */
6699                 sata_reidentify_device(spx);
6700         } else {
6701                 /* Something went wrong, microcode download command failed */
6702                 scsipkt->pkt_reason = CMD_INCOMPLETE;
6703                 *scsipkt->pkt_scbp = STATUS_CHECK;
6704                 sense = sata_arq_sense(spx);
6705                 switch (sata_pkt->satapkt_reason) {
6706                 case SATA_PKT_PORT_ERROR:
6707                         /*
6708                          * We have no device data. Assume no data transfered.
6709                          */
6710                         sense->es_key = KEY_HARDWARE_ERROR;
6711                         break;
6712 
6713                 case SATA_PKT_DEV_ERROR:
6714                         if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6715                             SATA_STATUS_ERR) {
6716                                 /*
6717                                  * determine dev error reason from error
6718                                  * reg content
6719                                  */
6720                                 sata_decode_device_error(spx, sense);
6721                                 break;
6722                         }
6723                         /* No extended sense key - no info available */
6724                         break;
6725 
6726                 case SATA_PKT_TIMEOUT:
6727                         scsipkt->pkt_reason = CMD_TIMEOUT;
6728                         scsipkt->pkt_statistics |=
6729                             STAT_TIMEOUT | STAT_DEV_RESET;
6730                         /* No extended sense key ? */
6731                         break;
6732 
6733                 case SATA_PKT_ABORTED:
6734                         scsipkt->pkt_reason = CMD_ABORTED;
6735                         scsipkt->pkt_statistics |= STAT_ABORTED;
6736                         /* No extended sense key ? */
6737                         break;
6738 
6739                 case SATA_PKT_RESET:
6740                         /* pkt aborted by an explicit reset from a host */
6741                         scsipkt->pkt_reason = CMD_RESET;
6742                         scsipkt->pkt_statistics |= STAT_DEV_RESET;
6743                         break;
6744 
6745                 default:
6746                         SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6747                             "sata_txlt_nodata_cmd_completion: "
6748                             "invalid packet completion reason %d",
6749                             sata_pkt->satapkt_reason));
6750                         scsipkt->pkt_reason = CMD_TRAN_ERR;
6751                         break;
6752                 }
6753 
6754                 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6755                     "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6756 
6757                 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6758                         /* scsi callback required */
6759                         scsi_hba_pkt_comp(scsipkt);
6760         }
6761         return (TRAN_ACCEPT);
6762 
6763 bad_param:
6764         mutex_exit(cport_mutex);
6765         *scsipkt->pkt_scbp = STATUS_CHECK;
6766         sense = sata_arq_sense(spx);
6767         sense->es_key = KEY_ILLEGAL_REQUEST;
6768         sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6769         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6770             scsipkt->pkt_comp != NULL) {
6771                 /* scsi callback required */
6772                 if (servicing_interrupt()) {
6773                         if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6774                             (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6775                             (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
6776                                 return (TRAN_BUSY);
6777                         }
6778                 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6779                     (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6780                     (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
6781                         /* Scheduling the callback failed */
6782                         return (TRAN_BUSY);
6783                 }
6784         }
6785         return (rval);
6786 }
6787 
6788 /*
6789  * Re-identify device after doing a firmware download.
6790  */
6791 static void
6792 sata_reidentify_device(sata_pkt_txlate_t *spx)
6793 {
6794 #define DOWNLOAD_WAIT_TIME_SECS 60
6795 #define DOWNLOAD_WAIT_INTERVAL_SECS     1
6796         int rval;
6797         int retry_cnt;
6798         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6799         sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6800         sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
6801         sata_drive_info_t *sdinfo;
6802 
6803         /*
6804          * Before returning good status, probe device.
6805          * Device probing will get IDENTIFY DEVICE data, if possible.
6806          * The assumption is that the new microcode is applied by the
6807          * device. It is a caller responsibility to verify this.
6808          */
6809         for (retry_cnt = 0;
6810             retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
6811             retry_cnt++) {
6812                 rval = sata_probe_device(sata_hba_inst, &sata_device);
6813 
6814                 if (rval == SATA_SUCCESS) { /* Set default features */
6815                         sdinfo = sata_get_device_info(sata_hba_inst,
6816                             &sata_device);
6817                         if (sata_initialize_device(sata_hba_inst, sdinfo) !=
6818                             SATA_SUCCESS) {
6819                                 /* retry */
6820                                 rval = sata_initialize_device(sata_hba_inst,
6821                                     sdinfo);
6822                                 if (rval == SATA_RETRY)
6823                                         sata_log(sata_hba_inst, CE_WARN,
6824                                             "SATA device at port %d pmport %d -"
6825                                             " default device features could not"
6826                                             " be set. Device may not operate "
6827                                             "as expected.",
6828                                             sata_device.satadev_addr.cport,
6829                                             sata_device.satadev_addr.pmport);
6830                         }
6831                         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6832                                 scsi_hba_pkt_comp(scsipkt);
6833                         return;
6834                 } else if (rval == SATA_RETRY) {
6835                         delay(drv_usectohz(1000000 *
6836                             DOWNLOAD_WAIT_INTERVAL_SECS));
6837                         continue;
6838                 } else  /* failed - no reason to retry */
6839                         break;
6840         }
6841 
6842         /*
6843          * Something went wrong, device probing failed.
6844          */
6845         SATA_LOG_D((sata_hba_inst, CE_WARN,
6846             "Cannot probe device after downloading microcode\n"));
6847 
6848         /* Reset device to force retrying the probe. */
6849         (void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
6850             (SATA_DIP(sata_hba_inst), &sata_device);
6851 
6852         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6853                 scsi_hba_pkt_comp(scsipkt);
6854 }
6855 
6856 
6857 /*
6858  * Translate command: Synchronize Cache.
6859  * Translates into Flush Cache command for SATA hard disks.
6860  *
6861  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6862  * appropriate values in scsi_pkt fields.
6863  */
6864 static  int
6865 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
6866 {
6867         sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6868         kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6869         int rval, reason;
6870         int synch;
6871 
6872         mutex_enter(cport_mutex);
6873 
6874         if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6875             TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6876                 mutex_exit(cport_mutex);
6877                 return (rval);
6878         }
6879 
6880         scmd->satacmd_addr_type = 0;
6881         scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
6882         scmd->satacmd_device_reg = 0;
6883         scmd->satacmd_sec_count_lsb = 0;
6884         scmd->satacmd_lba_low_lsb = 0;
6885         scmd->satacmd_lba_mid_lsb = 0;
6886         scmd->satacmd_lba_high_lsb = 0;
6887         scmd->satacmd_features_reg = 0;
6888         scmd->satacmd_status_reg = 0;
6889         scmd->satacmd_error_reg = 0;
6890 
6891         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6892             "sata_txlt_synchronize_cache\n", NULL);
6893 
6894         if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6895                 /* Need to set-up a callback function */
6896                 spx->txlt_sata_pkt->satapkt_comp =
6897                     sata_txlt_nodata_cmd_completion;
6898                 synch = FALSE;
6899         } else
6900                 synch = TRUE;
6901 
6902         /* Transfer command to HBA */
6903         if (sata_hba_start(spx, &rval) != 0) {
6904                 /* Pkt not accepted for execution */
6905                 mutex_exit(cport_mutex);
6906                 return (rval);
6907         }
6908         mutex_exit(cport_mutex);
6909 
6910         /*
6911          * If execution non-synchronous, it had to be completed
6912          * a callback function will handle potential errors, translate
6913          * the response and will do a callback to a target driver.
6914          * If it was synchronous, check status, using the same
6915          * framework callback.
6916          */
6917         if (synch) {
6918                 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6919                     "synchronous execution status %x\n",
6920                     spx->txlt_sata_pkt->satapkt_reason);
6921                 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
6922         }
6923         return (TRAN_ACCEPT);
6924 }
6925 
6926 
6927 /*
6928  * Send pkt to SATA HBA driver
6929  *
6930  * This function may be called only if the operation is requested by scsi_pkt,
6931  * i.e. scsi_pkt is not NULL.
6932  *
6933  * This function has to be called with cport mutex held. It does release
6934  * the mutex when it calls HBA driver sata_tran_start function and
6935  * re-acquires it afterwards.
6936  *
6937  * If return value is 0, pkt was accepted, -1 otherwise
6938  * rval is set to appropriate sata_scsi_start return value.
6939  *
6940  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
6941  * have called the sata_pkt callback function for this packet.
6942  *
6943  * The scsi callback has to be performed by the caller of this routine.
6944  */
6945 static int
6946 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
6947 {
6948         int stat;
6949         uint8_t cport = SATA_TXLT_CPORT(spx);
6950         uint8_t pmport = SATA_TXLT_PMPORT(spx);
6951         sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6952         sata_drive_info_t *sdinfo;
6953         sata_pmult_info_t *pminfo;
6954         sata_pmport_info_t *pmportinfo = NULL;
6955         sata_device_t *sata_device = NULL;
6956         uint8_t cmd;
6957         struct sata_cmd_flags cmd_flags;
6958 
6959         ASSERT(spx->txlt_sata_pkt != NULL);
6960 
6961         ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6962 
6963         sdinfo = sata_get_device_info(sata_hba_inst,
6964             &spx->txlt_sata_pkt->satapkt_device);
6965         ASSERT(sdinfo != NULL);
6966 
6967         /* Clear device reset state? */
6968         /* qual should be XXX_DPMPORT, but add XXX_PMPORT in case */
6969         if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT ||
6970             sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) {
6971 
6972                 /*
6973                  * Get the pmult_info of the its parent port multiplier, all
6974                  * sub-devices share a common device reset flags on in
6975                  * pmult_info.
6976                  */
6977                 pminfo = SATA_PMULT_INFO(sata_hba_inst, cport);
6978                 pmportinfo = pminfo->pmult_dev_port[pmport];
6979                 ASSERT(pminfo != NULL);
6980                 if (pminfo->pmult_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
6981                         spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
6982                             sata_clear_dev_reset = B_TRUE;
6983                         pminfo->pmult_event_flags &=
6984                             ~SATA_EVNT_CLEAR_DEVICE_RESET;
6985                         SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6986                             "sata_hba_start: clearing device reset state"
6987                             "on pmult.\n", NULL);
6988                 }
6989         } else {
6990                 if (sdinfo->satadrv_event_flags &
6991                     SATA_EVNT_CLEAR_DEVICE_RESET) {
6992                         spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
6993                             sata_clear_dev_reset = B_TRUE;
6994                         sdinfo->satadrv_event_flags &=
6995                             ~SATA_EVNT_CLEAR_DEVICE_RESET;
6996                         SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6997                             "sata_hba_start: clearing device reset state\n",
6998                             NULL);
6999                 }
7000         }
7001 
7002         cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
7003         cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
7004         sata_device = &spx->txlt_sata_pkt->satapkt_device;
7005 
7006         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7007 
7008         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7009             "Sata cmd 0x%2x\n", cmd);
7010 
7011         stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
7012             spx->txlt_sata_pkt);
7013         mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7014         /*
7015          * If sata pkt was accepted and executed in asynchronous mode, i.e.
7016          * with the sata callback, the sata_pkt could be already destroyed
7017          * by the time we check ther return status from the hba_start()
7018          * function, because sata_scsi_destroy_pkt() could have been already
7019          * called (perhaps in the interrupt context). So, in such case, there
7020          * should be no references to it. In other cases, sata_pkt still
7021          * exists.
7022          */
7023         if (stat == SATA_TRAN_ACCEPTED) {
7024                 /*
7025                  * pkt accepted for execution.
7026                  * If it was executed synchronously, it is already completed
7027                  * and pkt completion_reason indicates completion status.
7028                  */
7029                 *rval = TRAN_ACCEPT;
7030                 return (0);
7031         }
7032 
7033         sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
7034         switch (stat) {
7035         case SATA_TRAN_QUEUE_FULL:
7036                 /*
7037                  * Controller detected queue full condition.
7038                  */
7039                 SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
7040                     "sata_hba_start: queue full\n", NULL);
7041 
7042                 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7043                 *spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
7044 
7045                 *rval = TRAN_BUSY;
7046                 break;
7047 
7048         case SATA_TRAN_PORT_ERROR:
7049                 /*
7050                  * Communication/link with device or general port error
7051                  * detected before pkt execution begun.
7052                  */
7053                 if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
7054                     SATA_ADDR_CPORT ||
7055                     spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
7056                     SATA_ADDR_DCPORT)
7057                         sata_log(sata_hba_inst, CE_CONT,
7058                             "SATA port %d error",
7059                             sata_device->satadev_addr.cport);
7060                 else
7061                         sata_log(sata_hba_inst, CE_CONT,
7062                             "SATA port %d:%d error\n",
7063                             sata_device->satadev_addr.cport,
7064                             sata_device->satadev_addr.pmport);
7065 
7066                 /*
7067                  * Update the port/device structure.
7068                  * sata_pkt should be still valid. Since port error is
7069                  * returned, sata_device content should reflect port
7070                  * state - it means, that sata address have been changed,
7071                  * because original packet's sata address refered to a device
7072                  * attached to some port.
7073                  */
7074                 if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT ||
7075                     sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) {
7076                         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7077                         mutex_enter(&pmportinfo->pmport_mutex);
7078                         sata_update_pmport_info(sata_hba_inst, sata_device);
7079                         mutex_exit(&pmportinfo->pmport_mutex);
7080                         mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7081                 } else {
7082                         sata_update_port_info(sata_hba_inst, sata_device);
7083                 }
7084 
7085                 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7086                 *rval = TRAN_FATAL_ERROR;
7087                 break;
7088 
7089         case SATA_TRAN_CMD_UNSUPPORTED:
7090                 /*
7091                  * Command rejected by HBA as unsupported. It was HBA driver
7092                  * that rejected the command, command was not sent to
7093                  * an attached device.
7094                  */
7095                 if ((sdinfo != NULL) &&
7096                     (sdinfo->satadrv_state & SATA_DSTATE_RESET))
7097                         SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7098                             "sat_hba_start: cmd 0x%2x rejected "
7099                             "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
7100 
7101                 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7102                 (void) sata_txlt_invalid_command(spx);
7103                 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7104 
7105                 *rval = TRAN_ACCEPT;
7106                 break;
7107 
7108         case SATA_TRAN_BUSY:
7109                 /*
7110                  * Command rejected by HBA because other operation prevents
7111                  * accepting the packet, or device is in RESET condition.
7112                  */
7113                 if (sdinfo != NULL) {
7114                         sdinfo->satadrv_state =
7115                             spx->txlt_sata_pkt->satapkt_device.satadev_state;
7116 
7117                         if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
7118                                 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7119                                     "sata_hba_start: cmd 0x%2x rejected "
7120                                     "because of device reset condition\n",
7121                                     cmd);
7122                         } else {
7123                                 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7124                                     "sata_hba_start: cmd 0x%2x rejected "
7125                                     "with SATA_TRAN_BUSY status\n",
7126                                     cmd);
7127                         }
7128                 }
7129                 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7130                 *rval = TRAN_BUSY;
7131                 break;
7132 
7133         default:
7134                 /* Unrecognized HBA response */
7135                 SATA_LOG_D((sata_hba_inst, CE_WARN,
7136                     "sata_hba_start: unrecognized HBA response "
7137                     "to cmd : 0x%2x resp 0x%x", cmd, rval));
7138                 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7139                 *rval = TRAN_FATAL_ERROR;
7140                 break;
7141         }
7142 
7143         /*
7144          * If we got here, the packet was rejected.
7145          * Check if we need to remember reset state clearing request
7146          */
7147         if (cmd_flags.sata_clear_dev_reset) {
7148                 /*
7149                  * Check if device is still configured - it may have
7150                  * disapeared from the configuration
7151                  */
7152                 sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
7153                 if (sdinfo != NULL) {
7154                         /*
7155                          * Restore the flag that requests clearing of
7156                          * the device reset state,
7157                          * so the next sata packet may carry it to HBA.
7158                          */
7159                         if (sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT ||
7160                             sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT) {
7161                                 pminfo->pmult_event_flags |=
7162                                     SATA_EVNT_CLEAR_DEVICE_RESET;
7163                         } else {
7164                                 sdinfo->satadrv_event_flags |=
7165                                     SATA_EVNT_CLEAR_DEVICE_RESET;
7166                         }
7167                 }
7168         }
7169         return (-1);
7170 }
7171 
7172 /*
7173  * Scsi response setup for invalid LBA
7174  *
7175  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
7176  */
7177 static int
7178 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
7179 {
7180         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7181         struct scsi_extended_sense *sense;
7182 
7183         scsipkt->pkt_reason = CMD_CMPLT;
7184         scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7185             STATE_SENT_CMD | STATE_GOT_STATUS;
7186         *scsipkt->pkt_scbp = STATUS_CHECK;
7187 
7188         *scsipkt->pkt_scbp = STATUS_CHECK;
7189         sense = sata_arq_sense(spx);
7190         sense->es_key = KEY_ILLEGAL_REQUEST;
7191         sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7192 
7193         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7194             "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7195 
7196         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7197             scsipkt->pkt_comp != NULL) {
7198                 /* scsi callback required */
7199                 if (servicing_interrupt()) {
7200                         if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7201                             (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7202                             (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7203                                 return (TRAN_BUSY);
7204                         }
7205                 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7206                     (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7207                     (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7208                         /* Scheduling the callback failed */
7209                         return (TRAN_BUSY);
7210                 }
7211         }
7212         return (TRAN_ACCEPT);
7213 }
7214 
7215 
7216 /*
7217  * Analyze device status and error registers and translate them into
7218  * appropriate scsi sense codes.
7219  * NOTE: non-packet commands only for now
7220  */
7221 static void
7222 sata_decode_device_error(sata_pkt_txlate_t *spx,
7223     struct scsi_extended_sense *sense)
7224 {
7225         uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
7226 
7227         ASSERT(sense != NULL);
7228         ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
7229             SATA_STATUS_ERR);
7230 
7231 
7232         if (err_reg & SATA_ERROR_ICRC) {
7233                 sense->es_key = KEY_ABORTED_COMMAND;
7234                 sense->es_add_code = 0x08; /* Communication failure */
7235                 return;
7236         }
7237 
7238         if (err_reg & SATA_ERROR_UNC) {
7239                 sense->es_key = KEY_MEDIUM_ERROR;
7240                 /* Information bytes (LBA) need to be set by a caller */
7241                 return;
7242         }
7243 
7244         /* ADD HERE: MC error bit handling for ATAPI CD/DVD */
7245         if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
7246                 sense->es_key = KEY_UNIT_ATTENTION;
7247                 sense->es_add_code = 0x3a; /* No media present */
7248                 return;
7249         }
7250 
7251         if (err_reg & SATA_ERROR_IDNF) {
7252                 if (err_reg & SATA_ERROR_ABORT) {
7253                         sense->es_key = KEY_ABORTED_COMMAND;
7254                 } else {
7255                         sense->es_key = KEY_ILLEGAL_REQUEST;
7256                         sense->es_add_code = 0x21; /* LBA out of range */
7257                 }
7258                 return;
7259         }
7260 
7261         if (err_reg & SATA_ERROR_ABORT) {
7262                 ASSERT(spx->txlt_sata_pkt != NULL);
7263                 sense->es_key = KEY_ABORTED_COMMAND;
7264                 return;
7265         }
7266 }
7267 
7268 /*
7269  * Extract error LBA from sata_pkt.satapkt_cmd register fields
7270  */
7271 static void
7272 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
7273 {
7274         sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
7275 
7276         *lba = 0;
7277         if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
7278                 *lba = sata_cmd->satacmd_lba_high_msb;
7279                 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
7280                 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
7281         } else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
7282                 *lba = sata_cmd->satacmd_device_reg & 0xf;
7283         }
7284         *lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
7285         *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
7286         *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
7287 }
7288 
7289 /*
7290  * This is fixed sense format - if LBA exceeds the info field size,
7291  * no valid info will be returned (valid bit in extended sense will
7292  * be set to 0).
7293  */
7294 static struct scsi_extended_sense *
7295 sata_arq_sense(sata_pkt_txlate_t *spx)
7296 {
7297         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7298         struct scsi_arq_status *arqs;
7299         struct scsi_extended_sense *sense;
7300 
7301         /* Fill ARQ sense data */
7302         scsipkt->pkt_state |= STATE_ARQ_DONE;
7303         arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
7304         *(uchar_t *)&arqs->sts_status = STATUS_CHECK;
7305         *(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
7306         arqs->sts_rqpkt_reason = CMD_CMPLT;
7307         arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7308             STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7309         arqs->sts_rqpkt_resid = 0;
7310         sense = &arqs->sts_sensedata;
7311         bzero(sense, sizeof (struct scsi_extended_sense));
7312         sata_fixed_sense_data_preset(sense);
7313         return (sense);
7314 }
7315 
7316 /*
7317  * ATA Pass Through support
7318  * Sets flags indicating that an invalid value was found in some
7319  * field in the command.  It could be something illegal according to
7320  * the SAT-2 spec or it could be a feature that is not (yet?)
7321  * supported.
7322  */
7323 static int
7324 sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *spx)
7325 {
7326         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7327         struct scsi_extended_sense *sense = sata_arq_sense(spx);
7328 
7329         scsipkt->pkt_reason = CMD_CMPLT;
7330         *scsipkt->pkt_scbp = STATUS_CHECK;
7331         scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7332             STATE_SENT_CMD | STATE_GOT_STATUS;
7333 
7334         sense = sata_arq_sense(spx);
7335         sense->es_key = KEY_ILLEGAL_REQUEST;
7336         sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
7337 
7338         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7339             scsipkt->pkt_comp != NULL) {
7340                 /* scsi callback required */
7341                 if (servicing_interrupt()) {
7342                         if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7343                             (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7344                             (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7345                                 return (TRAN_BUSY);
7346                         }
7347                 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7348                     (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7349                     (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7350                         /* Scheduling the callback failed */
7351                         return (TRAN_BUSY);
7352                 }
7353         }
7354 
7355         return (TRAN_ACCEPT);
7356 }
7357 
7358 /*
7359  * The UNMAP command considers it not to be an error if the parameter length
7360  * or block descriptor length is 0.  For this case, there is nothing for TRIM
7361  * to do so just complete the command.
7362  */
7363 static int
7364 sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *spx)
7365 {
7366         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7367 
7368         scsipkt->pkt_reason = CMD_CMPLT;
7369         *scsipkt->pkt_scbp = STATUS_GOOD;
7370         scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7371             STATE_SENT_CMD | STATE_GOT_STATUS;
7372 
7373         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7374             scsipkt->pkt_comp != NULL) {
7375                 /* scsi callback required */
7376                 if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7377                     (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7378                     (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7379                         /* Scheduling the callback failed */
7380                         return (TRAN_BUSY);
7381                 }
7382         }
7383 
7384         return (TRAN_ACCEPT);
7385 }
7386 
7387 /*
7388  * Emulated SATA Read/Write command completion for zero-length requests.
7389  * This request always succedes, so in synchronous mode it always returns
7390  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
7391  * callback cannot be scheduled.
7392  */
7393 static int
7394 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
7395 {
7396         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7397 
7398         scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7399             STATE_SENT_CMD | STATE_GOT_STATUS;
7400         scsipkt->pkt_reason = CMD_CMPLT;
7401         *scsipkt->pkt_scbp = STATUS_GOOD;
7402         if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7403                 /* scsi callback required - have to schedule it */
7404                 if (servicing_interrupt()) {
7405                         if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7406                             (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7407                             (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7408                                 return (TRAN_BUSY);
7409                         }
7410                 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7411                     (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7412                     (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7413                         /* Scheduling the callback failed */
7414                         return (TRAN_BUSY);
7415                 }
7416         }
7417         return (TRAN_ACCEPT);
7418 }
7419 
7420 
7421 /*
7422  * Translate completion status of SATA read/write commands into scsi response.
7423  * pkt completion_reason is checked to determine the completion status.
7424  * Do scsi callback if necessary.
7425  *
7426  * Note: this function may be called also for synchronously executed
7427  * commands.
7428  * This function may be used only if scsi_pkt is non-NULL.
7429  */
7430 static void
7431 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
7432 {
7433         sata_pkt_txlate_t *spx =
7434             (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7435         sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7436         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7437         struct scsi_extended_sense *sense;
7438         uint64_t lba;
7439         struct buf *bp;
7440         int rval;
7441         if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7442                 /* Normal completion */
7443                 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7444                     STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7445                 scsipkt->pkt_reason = CMD_CMPLT;
7446                 *scsipkt->pkt_scbp = STATUS_GOOD;
7447                 if (spx->txlt_tmp_buf != NULL) {
7448                         /* Temporary buffer was used */
7449                         bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7450                         if (bp->b_flags & B_READ) {
7451                                 rval = ddi_dma_sync(
7452                                     spx->txlt_buf_dma_handle, 0, 0,
7453                                     DDI_DMA_SYNC_FORCPU);
7454                                 ASSERT(rval == DDI_SUCCESS);
7455                                 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7456                                     bp->b_bcount);
7457                         }
7458                 }
7459         } else {
7460                 /*
7461                  * Something went wrong - analyze return
7462                  */
7463                 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7464                     STATE_SENT_CMD | STATE_GOT_STATUS;
7465                 scsipkt->pkt_reason = CMD_INCOMPLETE;
7466                 *scsipkt->pkt_scbp = STATUS_CHECK;
7467                 sense = sata_arq_sense(spx);
7468                 ASSERT(sense != NULL);
7469 
7470                 /*
7471                  * SATA_PKT_DEV_ERROR is the only case where we may be able to
7472                  * extract from device registers the failing LBA.
7473                  */
7474                 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7475                         if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
7476                             (scmd->satacmd_lba_mid_msb != 0 ||
7477                             scmd->satacmd_lba_high_msb != 0)) {
7478                                 /*
7479                                  * We have problem reporting this cmd LBA
7480                                  * in fixed sense data format, because of
7481                                  * the size of the scsi LBA fields.
7482                                  */
7483                                 sense->es_valid = 0;
7484                         } else {
7485                                 sata_extract_error_lba(spx, &lba);
7486                                 sense->es_info_1 = (lba & 0xFF000000) >> 24;
7487                                 sense->es_info_2 = (lba & 0xFF0000) >> 16;
7488                                 sense->es_info_3 = (lba & 0xFF00) >> 8;
7489                                 sense->es_info_4 = lba & 0xFF;
7490                         }
7491                 } else {
7492                         /* Invalid extended sense info */
7493                         sense->es_valid = 0;
7494                 }
7495 
7496                 switch (sata_pkt->satapkt_reason) {
7497                 case SATA_PKT_PORT_ERROR:
7498                         /* We may want to handle DEV GONE state as well */
7499                         /*
7500                          * We have no device data. Assume no data transfered.
7501                          */
7502                         sense->es_key = KEY_HARDWARE_ERROR;
7503                         break;
7504 
7505                 case SATA_PKT_DEV_ERROR:
7506                         if (sata_pkt->satapkt_cmd.satacmd_status_reg &
7507                             SATA_STATUS_ERR) {
7508                                 /*
7509                                  * determine dev error reason from error
7510                                  * reg content
7511                                  */
7512                                 sata_decode_device_error(spx, sense);
7513                                 if (sense->es_key == KEY_MEDIUM_ERROR) {
7514                                         switch (scmd->satacmd_cmd_reg) {
7515                                         case SATAC_READ_DMA:
7516                                         case SATAC_READ_DMA_EXT:
7517                                         case SATAC_READ_DMA_QUEUED:
7518                                         case SATAC_READ_DMA_QUEUED_EXT:
7519                                         case SATAC_READ_FPDMA_QUEUED:
7520                                                 /* Unrecovered read error */
7521                                                 sense->es_add_code =
7522                                                     SD_SCSI_ASC_UNREC_READ_ERR;
7523                                                 break;
7524                                         case SATAC_WRITE_DMA:
7525                                         case SATAC_WRITE_DMA_EXT:
7526                                         case SATAC_WRITE_DMA_QUEUED:
7527                                         case SATAC_WRITE_DMA_QUEUED_EXT:
7528                                         case SATAC_WRITE_FPDMA_QUEUED:
7529                                                 /* Write error */
7530                                                 sense->es_add_code =
7531                                                     SD_SCSI_ASC_WRITE_ERR;
7532                                                 break;
7533                                         default:
7534                                                 /* Internal error */
7535                                                 SATA_LOG_D((
7536                                                     spx->txlt_sata_hba_inst,
7537                                                     CE_WARN,
7538                                                     "sata_txlt_rw_completion :"
7539                                                     "internal error - invalid "
7540                                                     "command 0x%2x",
7541                                                     scmd->satacmd_cmd_reg));
7542                                                 break;
7543                                         }
7544                                 }
7545                                 break;
7546                         }
7547                         /* No extended sense key - no info available */
7548                         scsipkt->pkt_reason = CMD_INCOMPLETE;
7549                         break;
7550 
7551                 case SATA_PKT_TIMEOUT:
7552                         scsipkt->pkt_reason = CMD_TIMEOUT;
7553                         scsipkt->pkt_statistics |=
7554                             STAT_TIMEOUT | STAT_DEV_RESET;
7555                         sense->es_key = KEY_ABORTED_COMMAND;
7556                         break;
7557 
7558                 case SATA_PKT_ABORTED:
7559                         scsipkt->pkt_reason = CMD_ABORTED;
7560                         scsipkt->pkt_statistics |= STAT_ABORTED;
7561                         sense->es_key = KEY_ABORTED_COMMAND;
7562                         break;
7563 
7564                 case SATA_PKT_RESET:
7565                         scsipkt->pkt_reason = CMD_RESET;
7566                         scsipkt->pkt_statistics |= STAT_DEV_RESET;
7567                         sense->es_key = KEY_ABORTED_COMMAND;
7568                         break;
7569 
7570                 default:
7571                         SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7572                             "sata_txlt_rw_completion: "
7573                             "invalid packet completion reason"));
7574                         scsipkt->pkt_reason = CMD_TRAN_ERR;
7575                         break;
7576                 }
7577         }
7578         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7579             "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7580 
7581         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7582                 /* scsi callback required */
7583                 scsi_hba_pkt_comp(scsipkt);
7584 }
7585 
7586 
7587 /*
7588  * Translate completion status of non-data commands (i.e. commands returning
7589  * no data).
7590  * pkt completion_reason is checked to determine the completion status.
7591  * Do scsi callback if necessary (FLAG_NOINTR == 0)
7592  *
7593  * Note: this function may be called also for synchronously executed
7594  * commands.
7595  * This function may be used only if scsi_pkt is non-NULL.
7596  */
7597 
7598 static  void
7599 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
7600 {
7601         sata_pkt_txlate_t *spx =
7602             (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7603         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7604 
7605         sata_set_arq_data(sata_pkt);
7606 
7607         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7608                 /* scsi callback required */
7609                 scsi_hba_pkt_comp(scsipkt);
7610 }
7611 
7612 /*
7613  * Completion handler for ATA Pass Through command
7614  */
7615 static void
7616 sata_txlt_apt_completion(sata_pkt_t *sata_pkt)
7617 {
7618         sata_pkt_txlate_t *spx =
7619             (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7620         sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7621         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7622         struct buf *bp;
7623         uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7624 
7625         if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7626                 /* Normal completion */
7627                 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7628                     STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7629                 scsipkt->pkt_reason = CMD_CMPLT;
7630                 *scsipkt->pkt_scbp = STATUS_GOOD;
7631 
7632                 /*
7633                  * If the command has CK_COND set
7634                  */
7635                 if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
7636                         *scsipkt->pkt_scbp = STATUS_CHECK;
7637                         sata_fill_ata_return_desc(sata_pkt,
7638                             KEY_RECOVERABLE_ERROR,
7639                             SD_SCSI_ASC_APT_INFO_AVAIL, 0x1d);
7640                 }
7641 
7642                 if (spx->txlt_tmp_buf != NULL) {
7643                         /* Temporary buffer was used */
7644                         bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7645                         if (bp->b_flags & B_READ) {
7646                                 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7647                                     bp->b_bcount);
7648                         }
7649                 }
7650         } else {
7651                 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7652                     STATE_SENT_CMD | STATE_GOT_STATUS;
7653                 scsipkt->pkt_reason = CMD_INCOMPLETE;
7654                 *scsipkt->pkt_scbp = STATUS_CHECK;
7655 
7656                 /*
7657                  * If DF or ERR was set, the HBA should have copied out the
7658                  * status and error registers to the satacmd structure.
7659                  */
7660                 if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7661                         sense_key = KEY_HARDWARE_ERROR;
7662                         addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7663                         addl_sense_qual = 0;
7664                 } else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7665                         if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7666                                 sense_key = KEY_NOT_READY;
7667                                 addl_sense_code =
7668                                     SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7669                                 addl_sense_qual = 0;
7670                         } else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7671                                 sense_key = KEY_MEDIUM_ERROR;
7672                                 addl_sense_code = SD_SCSI_ASC_UNREC_READ_ERR;
7673                                 addl_sense_qual = 0;
7674                         } else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7675                                 sense_key = KEY_DATA_PROTECT;
7676                                 addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7677                                 addl_sense_qual = 0;
7678                         } else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7679                                 sense_key = KEY_ILLEGAL_REQUEST;
7680                                 addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7681                                 addl_sense_qual = 0;
7682                         } else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7683                                 sense_key = KEY_ABORTED_COMMAND;
7684                                 addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7685                                 addl_sense_qual = 0;
7686                         } else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7687                                 sense_key = KEY_UNIT_ATTENTION;
7688                                 addl_sense_code =
7689                                     SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7690                                 addl_sense_qual = 0;
7691                         } else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7692                                 sense_key = KEY_UNIT_ATTENTION;
7693                                 addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7694                                 addl_sense_qual = 0;
7695                         } else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7696                                 sense_key = KEY_ABORTED_COMMAND;
7697                                 addl_sense_code =
7698                                     SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7699                                 addl_sense_qual = 0;
7700                         }
7701                 }
7702 
7703                 sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7704                     addl_sense_qual);
7705         }
7706 
7707         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7708                 /* scsi callback required */
7709                 scsi_hba_pkt_comp(scsipkt);
7710 }
7711 
7712 /*
7713  * Completion handler for unmap translation command
7714  */
7715 static void
7716 sata_txlt_unmap_completion(sata_pkt_t *sata_pkt)
7717 {
7718         sata_pkt_txlate_t *spx =
7719             (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7720         sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7721         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7722         struct buf *bp;
7723         uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7724 
7725         if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7726                 /* Normal completion */
7727                 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7728                     STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7729                 scsipkt->pkt_reason = CMD_CMPLT;
7730                 *scsipkt->pkt_scbp = STATUS_GOOD;
7731 
7732                 if (spx->txlt_tmp_buf != NULL) {
7733                         /* Temporary buffer was used */
7734                         bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7735                         if (bp->b_flags & B_READ) {
7736                                 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7737                                     bp->b_bcount);
7738                         }
7739                 }
7740         } else {
7741                 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7742                     STATE_SENT_CMD | STATE_GOT_STATUS;
7743                 scsipkt->pkt_reason = CMD_INCOMPLETE;
7744                 *scsipkt->pkt_scbp = STATUS_CHECK;
7745 
7746                 /*
7747                  * If DF or ERR was set, the HBA should have copied out the
7748                  * status and error registers to the satacmd structure.
7749                  */
7750                 if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7751                         sense_key = KEY_HARDWARE_ERROR;
7752                         addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7753                         addl_sense_qual = 0;
7754                 } else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7755                         if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7756                                 sense_key = KEY_NOT_READY;
7757                                 addl_sense_code =
7758                                     SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7759                                 addl_sense_qual = 0;
7760                         } else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7761                                 sense_key = KEY_MEDIUM_ERROR;
7762                                 addl_sense_code = SD_SCSI_ASC_WRITE_ERR;
7763                                 addl_sense_qual = 0;
7764                         } else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7765                                 sense_key = KEY_DATA_PROTECT;
7766                                 addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7767                                 addl_sense_qual = 0;
7768                         } else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7769                                 sense_key = KEY_ILLEGAL_REQUEST;
7770                                 addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7771                                 addl_sense_qual = 0;
7772                         } else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7773                                 sense_key = KEY_ABORTED_COMMAND;
7774                                 addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7775                                 addl_sense_qual = 0;
7776                         } else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7777                                 sense_key = KEY_UNIT_ATTENTION;
7778                                 addl_sense_code =
7779                                     SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7780                                 addl_sense_qual = 0;
7781                         } else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7782                                 sense_key = KEY_UNIT_ATTENTION;
7783                                 addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7784                                 addl_sense_qual = 0;
7785                         } else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7786                                 sense_key = KEY_ABORTED_COMMAND;
7787                                 addl_sense_code =
7788                                     SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7789                                 addl_sense_qual = 0;
7790                         }
7791                 }
7792 
7793                 sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7794                     addl_sense_qual);
7795         }
7796 
7797         sata_free_local_buffer(spx);
7798 
7799         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7800                 /* scsi callback required */
7801                 scsi_hba_pkt_comp(scsipkt);
7802 }
7803 
7804 /*
7805  *
7806  */
7807 static void
7808 sata_fill_ata_return_desc(sata_pkt_t *sata_pkt, uint8_t sense_key,
7809     uint8_t addl_sense_code, uint8_t addl_sense_qual)
7810 {
7811         sata_pkt_txlate_t *spx =
7812             (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7813         sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7814         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7815         struct sata_apt_sense_data *apt_sd =
7816             (struct sata_apt_sense_data *)scsipkt->pkt_scbp;
7817         struct scsi_descr_sense_hdr *sds = &(apt_sd->apt_sd_hdr);
7818         struct scsi_ata_status_ret_sense_descr *ata_ret_desc =
7819             &(apt_sd->apt_sd_sense);
7820         int extend = 0;
7821 
7822         if ((scsipkt->pkt_cdbp[0] == SPC3_CMD_ATA_COMMAND_PASS_THROUGH16) &&
7823             (scsipkt->pkt_cdbp[2] & SATL_APT_BM_EXTEND))
7824                 extend = 1;
7825 
7826         scsipkt->pkt_state |= STATE_ARQ_DONE;
7827 
7828         /* update the residual count */
7829         *(uchar_t *)&apt_sd->apt_status = STATUS_CHECK;
7830         *(uchar_t *)&apt_sd->apt_rqpkt_status = STATUS_GOOD;
7831         apt_sd->apt_rqpkt_reason = CMD_CMPLT;
7832         apt_sd->apt_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7833             STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7834         apt_sd->apt_rqpkt_resid = scsipkt->pkt_scblen -
7835             sizeof (struct sata_apt_sense_data);
7836 
7837         /*
7838          * Fill in the Descriptor sense header
7839          */
7840         bzero(sds, sizeof (struct scsi_descr_sense_hdr));
7841         sds->ds_code = CODE_FMT_DESCR_CURRENT;
7842         sds->ds_class = CLASS_EXTENDED_SENSE;
7843         sds->ds_key = sense_key & 0xf;
7844         sds->ds_add_code = addl_sense_code;
7845         sds->ds_qual_code = addl_sense_qual;
7846         sds->ds_addl_sense_length =
7847             sizeof (struct scsi_ata_status_ret_sense_descr);
7848 
7849         /*
7850          * Fill in the ATA Return descriptor sense data
7851          */
7852         bzero(ata_ret_desc, sizeof (struct scsi_ata_status_ret_sense_descr));
7853         ata_ret_desc->ars_descr_type = DESCR_ATA_STATUS_RETURN;
7854         ata_ret_desc->ars_addl_length = 0xc;
7855         ata_ret_desc->ars_error = scmd->satacmd_error_reg;
7856         ata_ret_desc->ars_sec_count_lsb = scmd->satacmd_sec_count_lsb;
7857         ata_ret_desc->ars_lba_low_lsb = scmd->satacmd_lba_low_lsb;
7858         ata_ret_desc->ars_lba_mid_lsb = scmd->satacmd_lba_mid_lsb;
7859         ata_ret_desc->ars_lba_high_lsb = scmd->satacmd_lba_high_lsb;
7860         ata_ret_desc->ars_device = scmd->satacmd_device_reg;
7861         ata_ret_desc->ars_status = scmd->satacmd_status_reg;
7862 
7863         if (extend == 1) {
7864                 ata_ret_desc->ars_extend = 1;
7865                 ata_ret_desc->ars_sec_count_msb = scmd->satacmd_sec_count_msb;
7866                 ata_ret_desc->ars_lba_low_msb = scmd->satacmd_lba_low_msb;
7867                 ata_ret_desc->ars_lba_mid_msb = scmd->satacmd_lba_mid_msb;
7868                 ata_ret_desc->ars_lba_high_msb = scmd->satacmd_lba_high_msb;
7869         } else {
7870                 ata_ret_desc->ars_extend = 0;
7871                 ata_ret_desc->ars_sec_count_msb = 0;
7872                 ata_ret_desc->ars_lba_low_msb = 0;
7873                 ata_ret_desc->ars_lba_mid_msb = 0;
7874                 ata_ret_desc->ars_lba_high_msb = 0;
7875         }
7876 }
7877 
7878 static  void
7879 sata_set_arq_data(sata_pkt_t *sata_pkt)
7880 {
7881         sata_pkt_txlate_t *spx =
7882             (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7883         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7884         struct scsi_extended_sense *sense;
7885 
7886         scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7887             STATE_SENT_CMD | STATE_GOT_STATUS;
7888         if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7889                 /* Normal completion */
7890                 scsipkt->pkt_reason = CMD_CMPLT;
7891                 *scsipkt->pkt_scbp = STATUS_GOOD;
7892         } else {
7893                 /* Something went wrong */
7894                 scsipkt->pkt_reason = CMD_INCOMPLETE;
7895                 *scsipkt->pkt_scbp = STATUS_CHECK;
7896                 sense = sata_arq_sense(spx);
7897                 switch (sata_pkt->satapkt_reason) {
7898                 case SATA_PKT_PORT_ERROR:
7899                         /*
7900                          * We have no device data. Assume no data transfered.
7901                          */
7902                         sense->es_key = KEY_HARDWARE_ERROR;
7903                         break;
7904 
7905                 case SATA_PKT_DEV_ERROR:
7906                         if (sata_pkt->satapkt_cmd.satacmd_status_reg &
7907                             SATA_STATUS_ERR) {
7908                                 /*
7909                                  * determine dev error reason from error
7910                                  * reg content
7911                                  */
7912                                 sata_decode_device_error(spx, sense);
7913                                 break;
7914                         }
7915                         /* No extended sense key - no info available */
7916                         break;
7917 
7918                 case SATA_PKT_TIMEOUT:
7919                         scsipkt->pkt_reason = CMD_TIMEOUT;
7920                         scsipkt->pkt_statistics |=
7921                             STAT_TIMEOUT | STAT_DEV_RESET;
7922                         /* No extended sense key ? */
7923                         break;
7924 
7925                 case SATA_PKT_ABORTED:
7926                         scsipkt->pkt_reason = CMD_ABORTED;
7927                         scsipkt->pkt_statistics |= STAT_ABORTED;
7928                         /* No extended sense key ? */
7929                         break;
7930 
7931                 case SATA_PKT_RESET:
7932                         /* pkt aborted by an explicit reset from a host */
7933                         scsipkt->pkt_reason = CMD_RESET;
7934                         scsipkt->pkt_statistics |= STAT_DEV_RESET;
7935                         break;
7936 
7937                 default:
7938                         SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7939                             "sata_txlt_nodata_cmd_completion: "
7940                             "invalid packet completion reason %d",
7941                             sata_pkt->satapkt_reason));
7942                         scsipkt->pkt_reason = CMD_TRAN_ERR;
7943                         break;
7944                 }
7945 
7946         }
7947         SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7948             "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7949 }
7950 
7951 
7952 /*
7953  * Build Mode sense R/W recovery page
7954  * NOT IMPLEMENTED
7955  */
7956 
7957 static int
7958 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7959 {
7960 #ifndef __lock_lint
7961         _NOTE(ARGUNUSED(sdinfo))
7962         _NOTE(ARGUNUSED(pcntrl))
7963         _NOTE(ARGUNUSED(buf))
7964 #endif
7965         return (0);
7966 }
7967 
7968 /*
7969  * Build Mode sense caching page  -  scsi-3 implementation.
7970  * Page length distinguishes previous format from scsi-3 format.
7971  * buf must have space for 0x12 bytes.
7972  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
7973  *
7974  */
7975 static int
7976 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7977 {
7978         struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
7979         sata_id_t *sata_id = &sdinfo->satadrv_id;
7980 
7981         /*
7982          * Most of the fields are set to 0, being not supported and/or disabled
7983          */
7984         bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
7985 
7986         /* Saved paramters not supported */
7987         if (pcntrl == 3)
7988                 return (0);
7989         if (pcntrl == 0 || pcntrl == 2) {
7990                 /*
7991                  * For now treat current and default parameters as same
7992                  * That may have to change, if target driver will complain
7993                  */
7994                 page->mode_page.code = MODEPAGE_CACHING;     /* PS = 0 */
7995                 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
7996 
7997                 if (SATA_READ_AHEAD_SUPPORTED(*sata_id) &&
7998                     !SATA_READ_AHEAD_ENABLED(*sata_id)) {
7999                         page->dra = 1;               /* Read Ahead disabled */
8000                         page->rcd = 1;               /* Read Cache disabled */
8001                 }
8002                 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) &&
8003                     SATA_WRITE_CACHE_ENABLED(*sata_id))
8004                         page->wce = 1;               /* Write Cache enabled */
8005         } else {
8006                 /* Changeable parameters */
8007                 page->mode_page.code = MODEPAGE_CACHING;
8008                 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
8009                 if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
8010                         page->dra = 1;
8011                         page->rcd = 1;
8012                 }
8013                 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id))
8014                         page->wce = 1;
8015         }
8016         return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
8017             sizeof (struct mode_page));
8018 }
8019 
8020 /*
8021  * Build Mode sense exception cntrl page
8022  */
8023 static int
8024 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8025 {
8026         struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
8027         sata_id_t *sata_id = &sdinfo->satadrv_id;
8028 
8029         /*
8030          * Most of the fields are set to 0, being not supported and/or disabled
8031          */
8032         bzero(buf, PAGELENGTH_INFO_EXCPT);
8033 
8034         page->mode_page.code = MODEPAGE_INFO_EXCPT;
8035         page->mode_page.length = PAGELENGTH_INFO_EXCPT;
8036 
8037         /* Indicate that this is page is saveable */
8038         page->mode_page.ps = 1;
8039 
8040         /*
8041          * We will return the same data for default, current and saved page.
8042          * The only changeable bit is dexcpt and that bit is required
8043          * by the ATA specification to be preserved across power cycles.
8044          */
8045         if (pcntrl != 1) {
8046                 page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
8047                 page->mrie = MRIE_ONLY_ON_REQUEST;
8048         }
8049         else
8050                 page->dexcpt = 1;    /* Only changeable parameter */
8051 
8052         return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page));
8053 }
8054 
8055 
8056 static int
8057 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8058 {
8059         struct mode_acoustic_management *page =
8060             (struct mode_acoustic_management *)buf;
8061         sata_id_t *sata_id = &sdinfo->satadrv_id;
8062 
8063         /*
8064          * Most of the fields are set to 0, being not supported and/or disabled
8065          */
8066         bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
8067 
8068         switch (pcntrl) {
8069         case P_CNTRL_DEFAULT:
8070                 /*  default paramters not supported */
8071                 return (0);
8072 
8073         case P_CNTRL_CURRENT:
8074         case P_CNTRL_SAVED:
8075                 /* Saved and current are supported and are identical */
8076                 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
8077                 page->mode_page.length =
8078                     PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
8079                 page->mode_page.ps = 1;
8080 
8081                 /* Word 83 indicates if feature is supported */
8082                 /* If feature is not supported */
8083                 if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
8084                         page->acoustic_manag_enable =
8085                             ACOUSTIC_DISABLED;
8086                 } else {
8087                         page->acoustic_manag_enable =
8088                             ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
8089                             != 0);
8090                         /* Word 94 inidicates the value */
8091 #ifdef  _LITTLE_ENDIAN
8092                         page->acoustic_manag_level =
8093                             (uchar_t)sata_id->ai_acoustic;
8094                         page->vendor_recommended_value =
8095                             sata_id->ai_acoustic >> 8;
8096 #else
8097                         page->acoustic_manag_level =
8098                             sata_id->ai_acoustic >> 8;
8099                         page->vendor_recommended_value =
8100                             (uchar_t)sata_id->ai_acoustic;
8101 #endif
8102                 }
8103                 break;
8104 
8105         case P_CNTRL_CHANGEABLE:
8106                 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
8107                 page->mode_page.length =
8108                     PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
8109                 page->mode_page.ps = 1;
8110 
8111                 /* Word 83 indicates if the feature is supported */
8112                 if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
8113                         page->acoustic_manag_enable =
8114                             ACOUSTIC_ENABLED;
8115                         page->acoustic_manag_level = 0xff;
8116                 }
8117                 break;
8118         }
8119         return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8120             sizeof (struct mode_page));
8121 }
8122 
8123 
8124 /*
8125  * Build Mode sense power condition page.
8126  */
8127 static int
8128 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8129 {
8130         struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf;
8131         sata_id_t *sata_id = &sdinfo->satadrv_id;
8132 
8133         /*
8134          * Most of the fields are set to 0, being not supported and/or disabled
8135          * power condition page length was 0x0a
8136          */
8137         bzero(buf, sizeof (struct mode_info_power_cond));
8138 
8139         if (pcntrl == P_CNTRL_DEFAULT) {
8140                 /*  default paramters not supported */
8141                 return (0);
8142         }
8143 
8144         page->mode_page.code = MODEPAGE_POWER_COND;
8145         page->mode_page.length = sizeof (struct mode_info_power_cond);
8146 
8147         if (sata_id->ai_cap & SATA_STANDBYTIMER) {
8148                 page->standby = 1;
8149                 bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer,
8150                     sizeof (uchar_t) * 4);
8151         }
8152 
8153         return (sizeof (struct mode_info_power_cond));
8154 }
8155 
8156 /*
8157  * Process mode select caching page 8 (scsi3 format only).
8158  * Read Ahead (same as read cache) and Write Cache may be turned on and off
8159  * if these features are supported by the device. If these features are not
8160  * supported, the command will be terminated with STATUS_CHECK.
8161  * This function fails only if the SET FEATURE command sent to
8162  * the device fails. The page format is not verified, assuming that the
8163  * target driver operates correctly - if parameters length is too short,
8164  * we just drop the page.
8165  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
8166  * setting have to be changed.
8167  * SET FEATURE command is executed synchronously, i.e. we wait here until
8168  * it is completed, regardless of the scsi pkt directives.
8169  *
8170  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
8171  * changing DRA will change RCD.
8172  *
8173  * More than one SATA command may be executed to perform operations specified
8174  * by mode select pages. The first error terminates further execution.
8175  * Operations performed successully are not backed-up in such case.
8176  *
8177  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8178  * If operation resulted in changing device setup, dmod flag should be set to
8179  * one (1). If parameters were not changed, dmod flag should be set to 0.
8180  * Upon return, if operation required sending command to the device, the rval
8181  * should be set to the value returned by sata_hba_start. If operation
8182  * did not require device access, rval should be set to TRAN_ACCEPT.
8183  * The pagelen should be set to the length of the page.
8184  *
8185  * This function has to be called with a port mutex held.
8186  *
8187  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8188  */
8189 int
8190 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
8191     int parmlen, int *pagelen, int *rval, int *dmod)
8192 {
8193         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8194         sata_drive_info_t *sdinfo;
8195         sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8196         sata_id_t *sata_id;
8197         struct scsi_extended_sense *sense;
8198         int wce, dra;   /* Current settings */
8199 
8200         sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8201             &spx->txlt_sata_pkt->satapkt_device);
8202         sata_id = &sdinfo->satadrv_id;
8203         *dmod = 0;
8204 
8205         /* Verify parameters length. If too short, drop it */
8206         if ((PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
8207             sizeof (struct mode_page)) > parmlen) {
8208                 *scsipkt->pkt_scbp = STATUS_CHECK;
8209                 sense = sata_arq_sense(spx);
8210                 sense->es_key = KEY_ILLEGAL_REQUEST;
8211                 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8212                 *pagelen = parmlen;
8213                 *rval = TRAN_ACCEPT;
8214                 return (SATA_FAILURE);
8215         }
8216 
8217         *pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
8218 
8219         /* Current setting of Read Ahead (and Read Cache) */
8220         if (SATA_READ_AHEAD_ENABLED(*sata_id))
8221                 dra = 0;        /* 0 == not disabled */
8222         else
8223                 dra = 1;
8224         /* Current setting of Write Cache */
8225         if (SATA_WRITE_CACHE_ENABLED(*sata_id))
8226                 wce = 1;
8227         else
8228                 wce = 0;
8229 
8230         if (page->dra == dra && page->wce == wce && page->rcd == dra) {
8231                 /* nothing to do */
8232                 *rval = TRAN_ACCEPT;
8233                 return (SATA_SUCCESS);
8234         }
8235 
8236         /*
8237          * Need to flip some setting
8238          * Set-up Internal SET FEATURES command(s)
8239          */
8240         scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8241         scmd->satacmd_addr_type = 0;
8242         scmd->satacmd_device_reg = 0;
8243         scmd->satacmd_status_reg = 0;
8244         scmd->satacmd_error_reg = 0;
8245         scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8246         if (page->dra != dra || page->rcd != dra) {
8247                 if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
8248                         /* Need to flip read ahead setting */
8249                         if (dra == 0)
8250                                 /* Disable read ahead / read cache */
8251                                 scmd->satacmd_features_reg =
8252                                     SATAC_SF_DISABLE_READ_AHEAD;
8253                         else
8254                                 /* Enable read ahead  / read cache */
8255                                 scmd->satacmd_features_reg =
8256                                     SATAC_SF_ENABLE_READ_AHEAD;
8257 
8258                         /* Transfer command to HBA */
8259                         if (sata_hba_start(spx, rval) != 0)
8260                                 /*
8261                                  * Pkt not accepted for execution.
8262                                  */
8263                                 return (SATA_FAILURE);
8264 
8265                         *dmod = 1;
8266 
8267                         /* Now process return */
8268                         if (spx->txlt_sata_pkt->satapkt_reason !=
8269                             SATA_PKT_COMPLETED) {
8270                                 goto failure;   /* Terminate */
8271                         }
8272                 } else {
8273                         *scsipkt->pkt_scbp = STATUS_CHECK;
8274                         sense = sata_arq_sense(spx);
8275                         sense->es_key = KEY_ILLEGAL_REQUEST;
8276                         sense->es_add_code =
8277                             SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8278                         *pagelen = parmlen;
8279                         *rval = TRAN_ACCEPT;
8280                         return (SATA_FAILURE);
8281                 }
8282         }
8283 
8284         /* Note that the packet is not removed, so it could be re-used */
8285         if (page->wce != wce) {
8286                 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) {
8287                         /* Need to flip Write Cache setting */
8288                         if (page->wce == 1)
8289                                 /* Enable write cache */
8290                                 scmd->satacmd_features_reg =
8291                                     SATAC_SF_ENABLE_WRITE_CACHE;
8292                         else
8293                                 /* Disable write cache */
8294                                 scmd->satacmd_features_reg =
8295                                     SATAC_SF_DISABLE_WRITE_CACHE;
8296 
8297                         /* Transfer command to HBA */
8298                         if (sata_hba_start(spx, rval) != 0)
8299                                 /*
8300                                  * Pkt not accepted for execution.
8301                                  */
8302                                 return (SATA_FAILURE);
8303 
8304                         *dmod = 1;
8305 
8306                         /* Now process return */
8307                         if (spx->txlt_sata_pkt->satapkt_reason !=
8308                             SATA_PKT_COMPLETED) {
8309                                 goto failure;
8310                         }
8311                 } else {
8312                         *scsipkt->pkt_scbp = STATUS_CHECK;
8313                         sense = sata_arq_sense(spx);
8314                         sense->es_key = KEY_ILLEGAL_REQUEST;
8315                         sense->es_add_code =
8316                             SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8317                         *pagelen = parmlen;
8318                         *rval = TRAN_ACCEPT;
8319                         return (SATA_FAILURE);
8320                 }
8321         }
8322         return (SATA_SUCCESS);
8323 
8324 failure:
8325         sata_xlate_errors(spx);
8326 
8327         return (SATA_FAILURE);
8328 }
8329 
8330 /*
8331  * Process mode select informational exceptions control page 0x1c
8332  *
8333  * The only changeable bit is dexcpt (disable exceptions).
8334  * MRIE (method of reporting informational exceptions) must be
8335  * "only on request".
8336  * This page applies to informational exceptions that report
8337  * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh
8338  * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_).
8339  * Informational exception conditions occur as the result of background scan
8340  * errors, background self-test errors, or vendor specific events within a
8341  * logical unit. An informational exception condition may occur asynchronous
8342  * to any commands.
8343  *
8344  * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8345  * If operation resulted in changing device setup, dmod flag should be set to
8346  * one (1). If parameters were not changed, dmod flag should be set to 0.
8347  * Upon return, if operation required sending command to the device, the rval
8348  * should be set to the value returned by sata_hba_start. If operation
8349  * did not require device access, rval should be set to TRAN_ACCEPT.
8350  * The pagelen should be set to the length of the page.
8351  *
8352  * This function has to be called with a port mutex held.
8353  *
8354  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8355  *
8356  * Cannot be called in the interrupt context.
8357  */
8358 static  int
8359 sata_mode_select_page_1c(
8360         sata_pkt_txlate_t *spx,
8361         struct mode_info_excpt_page *page,
8362         int parmlen,
8363         int *pagelen,
8364         int *rval,
8365         int *dmod)
8366 {
8367         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8368         sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8369         sata_drive_info_t *sdinfo;
8370         sata_id_t *sata_id;
8371         struct scsi_extended_sense *sense;
8372 
8373         sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8374             &spx->txlt_sata_pkt->satapkt_device);
8375         sata_id = &sdinfo->satadrv_id;
8376 
8377         *dmod = 0;
8378 
8379         /* Verify parameters length. If too short, drop it */
8380         if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) > parmlen) ||
8381             page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
8382                 *scsipkt->pkt_scbp = STATUS_CHECK;
8383                 sense = sata_arq_sense(spx);
8384                 sense->es_key = KEY_ILLEGAL_REQUEST;
8385                 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8386                 *pagelen = parmlen;
8387                 *rval = TRAN_ACCEPT;
8388                 return (SATA_FAILURE);
8389         }
8390 
8391         *pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
8392 
8393         if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
8394                 *scsipkt->pkt_scbp = STATUS_CHECK;
8395                 sense = sata_arq_sense(spx);
8396                 sense->es_key = KEY_ILLEGAL_REQUEST;
8397                 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
8398                 *pagelen = parmlen;
8399                 *rval = TRAN_ACCEPT;
8400                 return (SATA_FAILURE);
8401         }
8402 
8403         /* If already in the state requested, we are done */
8404         if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
8405                 /* nothing to do */
8406                 *rval = TRAN_ACCEPT;
8407                 return (SATA_SUCCESS);
8408         }
8409 
8410         scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8411 
8412         /* Build SMART_ENABLE or SMART_DISABLE command */
8413         scmd->satacmd_addr_type = 0;         /* N/A */
8414         scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
8415         scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
8416         scmd->satacmd_features_reg = page->dexcpt ?
8417             SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
8418         scmd->satacmd_device_reg = 0;                /* Always device 0 */
8419         scmd->satacmd_cmd_reg = SATAC_SMART;
8420 
8421         /* Transfer command to HBA */
8422         if (sata_hba_start(spx, rval) != 0)
8423                 /*
8424                  * Pkt not accepted for execution.
8425                  */
8426                 return (SATA_FAILURE);
8427 
8428         *dmod = 1;      /* At least may have been modified */
8429 
8430         /* Now process return */
8431         if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
8432                 return (SATA_SUCCESS);
8433 
8434         /* Packet did not complete successfully */
8435         sata_xlate_errors(spx);
8436 
8437         return (SATA_FAILURE);
8438 }
8439 
8440 /*
8441  * Process mode select acoustic management control page 0x30
8442  *
8443  *
8444  * This function has to be called with a port mutex held.
8445  *
8446  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8447  *
8448  * Cannot be called in the interrupt context.
8449  */
8450 int
8451 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
8452     mode_acoustic_management *page, int parmlen, int *pagelen,
8453     int *rval, int *dmod)
8454 {
8455         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8456         sata_drive_info_t *sdinfo;
8457         sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8458         sata_id_t *sata_id;
8459         struct scsi_extended_sense *sense;
8460 
8461         sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8462             &spx->txlt_sata_pkt->satapkt_device);
8463         sata_id = &sdinfo->satadrv_id;
8464         *dmod = 0;
8465 
8466         /* If parmlen is too short or the feature is not supported, drop it */
8467         if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8468             sizeof (struct mode_page)) > parmlen) ||
8469             (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
8470                 *scsipkt->pkt_scbp = STATUS_CHECK;
8471                 sense = sata_arq_sense(spx);
8472                 sense->es_key = KEY_ILLEGAL_REQUEST;
8473                 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8474                 *pagelen = parmlen;
8475                 *rval = TRAN_ACCEPT;
8476                 return (SATA_FAILURE);
8477         }
8478 
8479         *pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8480             sizeof (struct mode_page);
8481 
8482         /*
8483          * We can enable and disable acoustice management and
8484          * set the acoustic management level.
8485          */
8486 
8487         /*
8488          * Set-up Internal SET FEATURES command(s)
8489          */
8490         scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8491         scmd->satacmd_addr_type = 0;
8492         scmd->satacmd_device_reg = 0;
8493         scmd->satacmd_status_reg = 0;
8494         scmd->satacmd_error_reg = 0;
8495         scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8496         if (page->acoustic_manag_enable) {
8497                 scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
8498                 scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
8499         } else {        /* disabling acoustic management */
8500                 scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
8501         }
8502 
8503         /* Transfer command to HBA */
8504         if (sata_hba_start(spx, rval) != 0)
8505                 /*
8506                  * Pkt not accepted for execution.
8507                  */
8508                 return (SATA_FAILURE);
8509 
8510         /* Now process return */
8511         if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
8512                 sata_xlate_errors(spx);
8513                 return (SATA_FAILURE);
8514         }
8515 
8516         *dmod = 1;
8517 
8518         return (SATA_SUCCESS);
8519 }
8520 
8521 /*
8522  * Process mode select power condition page 0x1a
8523  *
8524  * This function has to be called with a port mutex held.
8525  *
8526  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8527  *
8528  * Cannot be called in the interrupt context.
8529  */
8530 int
8531 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct
8532     mode_info_power_cond *page, int parmlen, int *pagelen,
8533     int *rval, int *dmod)
8534 {
8535         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8536         sata_drive_info_t *sdinfo;
8537         sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8538         sata_id_t *sata_id;
8539         struct scsi_extended_sense *sense;
8540         uint8_t ata_count;
8541         int i, len;
8542 
8543         sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8544             &spx->txlt_sata_pkt->satapkt_device);
8545         sata_id = &sdinfo->satadrv_id;
8546         *dmod = 0;
8547 
8548         len = sizeof (struct mode_info_power_cond);
8549         len += sizeof (struct mode_page);
8550 
8551         /* If parmlen is too short or the feature is not supported, drop it */
8552         if ((len < parmlen) || (page->idle == 1) ||
8553             (!(sata_id->ai_cap & SATA_STANDBYTIMER) && page->standby == 1)) {
8554                 *scsipkt->pkt_scbp = STATUS_CHECK;
8555                 sense = sata_arq_sense(spx);
8556                 sense->es_key = KEY_ILLEGAL_REQUEST;
8557                 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8558                 *pagelen = parmlen;
8559                 *rval = TRAN_ACCEPT;
8560                 return (SATA_FAILURE);
8561         }
8562 
8563         *pagelen = len;
8564 
8565         /*
8566          * Set-up Internal STANDBY command(s)
8567          */
8568         if (page->standby == 0)
8569                 goto out;
8570 
8571         ata_count = sata_get_standby_timer(page->standby_cond_timer);
8572 
8573         scmd->satacmd_addr_type = 0;
8574         scmd->satacmd_sec_count_lsb = ata_count;
8575         scmd->satacmd_lba_low_lsb = 0;
8576         scmd->satacmd_lba_mid_lsb = 0;
8577         scmd->satacmd_lba_high_lsb = 0;
8578         scmd->satacmd_features_reg = 0;
8579         scmd->satacmd_device_reg = 0;
8580         scmd->satacmd_status_reg = 0;
8581         scmd->satacmd_cmd_reg = SATAC_STANDBY;
8582         scmd->satacmd_flags.sata_special_regs = B_TRUE;
8583         scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
8584 
8585         /* Transfer command to HBA */
8586         if (sata_hba_start(spx, rval) != 0) {
8587                 return (SATA_FAILURE);
8588         } else {
8589                 if ((scmd->satacmd_error_reg != 0) ||
8590                     (spx->txlt_sata_pkt->satapkt_reason !=
8591                     SATA_PKT_COMPLETED)) {
8592                         sata_xlate_errors(spx);
8593                         return (SATA_FAILURE);
8594                 }
8595         }
8596 
8597         for (i = 0; i < 4; i++) {
8598                 sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i];
8599         }
8600 out:
8601         *dmod = 1;
8602         return (SATA_SUCCESS);
8603 }
8604 
8605 /*
8606  * sata_build_lsense_page0() is used to create the
8607  * SCSI LOG SENSE page 0 (supported log pages)
8608  *
8609  * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e
8610  * (supported log pages, self-test results, informational exceptions
8611  * Sun vendor specific ATA SMART data, and start stop cycle counter).
8612  *
8613  * Takes a sata_drive_info t * and the address of a buffer
8614  * in which to create the page information.
8615  *
8616  * Returns the number of bytes valid in the buffer.
8617  */
8618 static  int
8619 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
8620 {
8621         struct log_parameter *lpp = (struct log_parameter *)buf;
8622         uint8_t *page_ptr = (uint8_t *)lpp->param_values;
8623         int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
8624         sata_id_t *sata_id = &sdinfo->satadrv_id;
8625 
8626         lpp->param_code[0] = 0;
8627         lpp->param_code[1] = 0;
8628         lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
8629         *page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
8630 
8631         if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
8632                 if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
8633                         *page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
8634                         ++num_pages_supported;
8635                 }
8636                 *page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
8637                 ++num_pages_supported;
8638                 *page_ptr++ = PAGE_CODE_SMART_READ_DATA;
8639                 ++num_pages_supported;
8640                 *page_ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER;
8641                 ++num_pages_supported;
8642         }
8643 
8644         lpp->param_len = num_pages_supported;
8645 
8646         return ((&lpp->param_values[0] - (uint8_t *)lpp) +
8647             num_pages_supported);
8648 }
8649 
8650 /*
8651  * sata_build_lsense_page_10() is used to create the
8652  * SCSI LOG SENSE page 0x10 (self-test results)
8653  *
8654  * Takes a sata_drive_info t * and the address of a buffer
8655  * in which to create the page information as well as a sata_hba_inst_t *.
8656  *
8657  * Returns the number of bytes valid in the buffer.
8658  *
8659  * Note: Self test and SMART data is accessible in device log pages.
8660  * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors
8661  * of data can be transferred by a single command), or by the General Purpose
8662  * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors
8663  * - approximately 33MB - can be transferred by a single command.
8664  * The SCT Command response (either error or command) is the same for both
8665  * the SMART and GPL methods of issuing commands.
8666  * This function uses READ LOG EXT command when drive supports LBA48, and
8667  * SMART READ command otherwise.
8668  *
8669  * Since above commands are executed in a synchronous mode, this function
8670  * should not be called in an interrupt context.
8671  */
8672 static  int
8673 sata_build_lsense_page_10(
8674         sata_drive_info_t *sdinfo,
8675         uint8_t *buf,
8676         sata_hba_inst_t *sata_hba_inst)
8677 {
8678         struct log_parameter *lpp = (struct log_parameter *)buf;
8679         int rval;
8680 
8681         if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
8682                 struct smart_ext_selftest_log *ext_selftest_log;
8683 
8684                 ext_selftest_log = kmem_zalloc(
8685                     sizeof (struct smart_ext_selftest_log), KM_SLEEP);
8686 
8687                 rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
8688                     ext_selftest_log, 0);
8689                 if (rval == 0) {
8690                         int index, start_index;
8691                         struct smart_ext_selftest_log_entry *entry;
8692                         static const struct smart_ext_selftest_log_entry empty =
8693                             {0};
8694                         uint16_t block_num;
8695                         int count;
8696                         boolean_t only_one_block = B_FALSE;
8697 
8698                         index = ext_selftest_log->
8699                             smart_ext_selftest_log_index[0];
8700                         index |= ext_selftest_log->
8701                             smart_ext_selftest_log_index[1] << 8;
8702                         if (index == 0)
8703                                 goto out;
8704 
8705                         --index;        /* Correct for 0 origin */
8706                         start_index = index;    /* remember where we started */
8707                         block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8708                         if (block_num != 0) {
8709                                 rval = sata_ext_smart_selftest_read_log(
8710                                     sata_hba_inst, sdinfo, ext_selftest_log,
8711                                     block_num);
8712                                 if (rval != 0)
8713                                         goto out;
8714                         }
8715                         index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8716                         entry =
8717                             &ext_selftest_log->
8718                             smart_ext_selftest_log_entries[index];
8719 
8720                         for (count = 1;
8721                             count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
8722                             ++count) {
8723                                 uint8_t status;
8724                                 uint8_t code;
8725                                 uint8_t sense_key;
8726                                 uint8_t add_sense_code;
8727                                 uint8_t add_sense_code_qual;
8728 
8729                                 /* If this is an unused entry, we are done */
8730                                 if (bcmp(entry, &empty, sizeof (empty)) == 0) {
8731                                         /* Broken firmware on some disks */
8732                                         if (index + 1 ==
8733                                             ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
8734                                                 --entry;
8735                                                 --index;
8736                                                 if (bcmp(entry, &empty,
8737                                                     sizeof (empty)) == 0)
8738                                                         goto out;
8739                                         } else
8740                                                 goto out;
8741                                 }
8742 
8743                                 if (only_one_block &&
8744                                     start_index == index)
8745                                         goto out;
8746 
8747                                 lpp->param_code[0] = 0;
8748                                 lpp->param_code[1] = count;
8749                                 lpp->param_ctrl_flags =
8750                                     LOG_CTRL_LP | LOG_CTRL_LBIN;
8751                                 lpp->param_len =
8752                                     SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
8753 
8754                                 status = entry->smart_ext_selftest_log_status;
8755                                 status >>= 4;
8756                                 switch (status) {
8757                                 case 0:
8758                                 default:
8759                                         sense_key = KEY_NO_SENSE;
8760                                         add_sense_code =
8761                                             SD_SCSI_ASC_NO_ADD_SENSE;
8762                                         add_sense_code_qual = 0;
8763                                         break;
8764                                 case 1:
8765                                         sense_key = KEY_ABORTED_COMMAND;
8766                                         add_sense_code =
8767                                             DIAGNOSTIC_FAILURE_ON_COMPONENT;
8768                                         add_sense_code_qual = SCSI_COMPONENT_81;
8769                                         break;
8770                                 case 2:
8771                                         sense_key = KEY_ABORTED_COMMAND;
8772                                         add_sense_code =
8773                                             DIAGNOSTIC_FAILURE_ON_COMPONENT;
8774                                         add_sense_code_qual = SCSI_COMPONENT_82;
8775                                         break;
8776                                 case 3:
8777                                         sense_key = KEY_ABORTED_COMMAND;
8778                                         add_sense_code =
8779                                             DIAGNOSTIC_FAILURE_ON_COMPONENT;
8780                                         add_sense_code_qual = SCSI_COMPONENT_83;
8781                                         break;
8782                                 case 4:
8783                                         sense_key = KEY_HARDWARE_ERROR;
8784                                         add_sense_code =
8785                                             DIAGNOSTIC_FAILURE_ON_COMPONENT;
8786                                         add_sense_code_qual = SCSI_COMPONENT_84;
8787                                         break;
8788                                 case 5:
8789                                         sense_key = KEY_HARDWARE_ERROR;
8790                                         add_sense_code =
8791                                             DIAGNOSTIC_FAILURE_ON_COMPONENT;
8792                                         add_sense_code_qual = SCSI_COMPONENT_85;
8793                                         break;
8794                                 case 6:
8795                                         sense_key = KEY_HARDWARE_ERROR;
8796                                         add_sense_code =
8797                                             DIAGNOSTIC_FAILURE_ON_COMPONENT;
8798                                         add_sense_code_qual = SCSI_COMPONENT_86;
8799                                         break;
8800                                 case 7:
8801                                         sense_key = KEY_MEDIUM_ERROR;
8802                                         add_sense_code =
8803                                             DIAGNOSTIC_FAILURE_ON_COMPONENT;
8804                                         add_sense_code_qual = SCSI_COMPONENT_87;
8805                                         break;
8806                                 case 8:
8807                                         sense_key = KEY_HARDWARE_ERROR;
8808                                         add_sense_code =
8809                                             DIAGNOSTIC_FAILURE_ON_COMPONENT;
8810                                         add_sense_code_qual = SCSI_COMPONENT_88;
8811                                         break;
8812                                 }
8813                                 code = 0;       /* unspecified */
8814                                 status |= (code << 4);
8815                                 lpp->param_values[0] = status;
8816                                 lpp->param_values[1] = 0; /* unspecified */
8817                                 lpp->param_values[2] = entry->
8818                                     smart_ext_selftest_log_timestamp[1];
8819                                 lpp->param_values[3] = entry->
8820                                     smart_ext_selftest_log_timestamp[0];
8821                                 if (status != 0) {
8822                                         lpp->param_values[4] = 0;
8823                                         lpp->param_values[5] = 0;
8824                                         lpp->param_values[6] = entry->
8825                                             smart_ext_selftest_log_failing_lba
8826                                             [5];
8827                                         lpp->param_values[7] = entry->
8828                                             smart_ext_selftest_log_failing_lba
8829                                             [4];
8830                                         lpp->param_values[8] = entry->
8831                                             smart_ext_selftest_log_failing_lba
8832                                             [3];
8833                                         lpp->param_values[9] = entry->
8834                                             smart_ext_selftest_log_failing_lba
8835                                             [2];
8836                                         lpp->param_values[10] = entry->
8837                                             smart_ext_selftest_log_failing_lba
8838                                             [1];
8839                                         lpp->param_values[11] = entry->
8840                                             smart_ext_selftest_log_failing_lba
8841                                             [0];
8842                                 } else {        /* No bad block address */
8843                                         lpp->param_values[4] = 0xff;
8844                                         lpp->param_values[5] = 0xff;
8845                                         lpp->param_values[6] = 0xff;
8846                                         lpp->param_values[7] = 0xff;
8847                                         lpp->param_values[8] = 0xff;
8848                                         lpp->param_values[9] = 0xff;
8849                                         lpp->param_values[10] = 0xff;
8850                                         lpp->param_values[11] = 0xff;
8851                                 }
8852 
8853                                 lpp->param_values[12] = sense_key;
8854                                 lpp->param_values[13] = add_sense_code;
8855                                 lpp->param_values[14] = add_sense_code_qual;
8856                                 lpp->param_values[15] = 0; /* undefined */
8857 
8858                                 lpp = (struct log_parameter *)
8859                                     (((uint8_t *)lpp) +
8860                                     SCSI_LOG_PARAM_HDR_LEN +
8861                                     SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
8862 
8863                                 --index;        /* Back up to previous entry */
8864                                 if (index < 0) {
8865                                         if (block_num > 0) {
8866                                                 --block_num;
8867                                         } else {
8868                                                 struct read_log_ext_directory
8869                                                     logdir;
8870 
8871                                                 rval =
8872                                                     sata_read_log_ext_directory(
8873                                                     sata_hba_inst, sdinfo,
8874                                                     &logdir);
8875                                                 if (rval == -1)
8876                                                         goto out;
8877                                                 if ((logdir.read_log_ext_vers
8878                                                     [0] == 0) &&
8879                                                     (logdir.read_log_ext_vers
8880                                                     [1] == 0))
8881                                                         goto out;
8882                                                 block_num =
8883                                                     logdir.read_log_ext_nblks
8884                                                     [EXT_SMART_SELFTEST_LOG_PAGE
8885                                                     - 1][0];
8886                                                 block_num |= logdir.
8887                                                     read_log_ext_nblks
8888                                                     [EXT_SMART_SELFTEST_LOG_PAGE
8889                                                     - 1][1] << 8;
8890                                                 --block_num;
8891                                                 only_one_block =
8892                                                     (block_num == 0);
8893                                         }
8894                                         rval = sata_ext_smart_selftest_read_log(
8895                                             sata_hba_inst, sdinfo,
8896                                             ext_selftest_log, block_num);
8897                                         if (rval != 0)
8898                                                 goto out;
8899 
8900                                         index =
8901                                             ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
8902                                             1;
8903                                 }
8904                                 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8905                                 entry = &ext_selftest_log->
8906                                     smart_ext_selftest_log_entries[index];
8907                         }
8908                 }
8909 out:
8910                 kmem_free(ext_selftest_log,
8911                     sizeof (struct smart_ext_selftest_log));
8912         } else {
8913                 struct smart_selftest_log *selftest_log;
8914 
8915                 selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
8916                     KM_SLEEP);
8917 
8918                 rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
8919                     selftest_log);
8920 
8921                 if (rval == 0) {
8922                         int index;
8923                         int count;
8924                         struct smart_selftest_log_entry *entry;
8925                         static const struct smart_selftest_log_entry empty =
8926                             { 0 };
8927 
8928                         index = selftest_log->smart_selftest_log_index;
8929                         if (index == 0)
8930                                 goto done;
8931                         --index;        /* Correct for 0 origin */
8932                         entry = &selftest_log->
8933                             smart_selftest_log_entries[index];
8934                         for (count = 1;
8935                             count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
8936                             ++count) {
8937                                 uint8_t status;
8938                                 uint8_t code;
8939                                 uint8_t sense_key;
8940                                 uint8_t add_sense_code;
8941                                 uint8_t add_sense_code_qual;
8942 
8943                                 if (bcmp(entry, &empty, sizeof (empty)) == 0)
8944                                         goto done;
8945 
8946                                 lpp->param_code[0] = 0;
8947                                 lpp->param_code[1] = count;
8948                                 lpp->param_ctrl_flags =
8949                                     LOG_CTRL_LP | LOG_CTRL_LBIN;
8950                                 lpp->param_len =
8951                                     SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
8952 
8953                                 status = entry->smart_selftest_log_status;
8954                                 status >>= 4;
8955                                 switch (status) {
8956                                 case 0:
8957                                 default:
8958                                         sense_key = KEY_NO_SENSE;
8959                                         add_sense_code =
8960                                             SD_SCSI_ASC_NO_ADD_SENSE;
8961                                         break;
8962                                 case 1:
8963                                         sense_key = KEY_ABORTED_COMMAND;
8964                                         add_sense_code =
8965                                             DIAGNOSTIC_FAILURE_ON_COMPONENT;
8966                                         add_sense_code_qual = SCSI_COMPONENT_81;
8967                                         break;
8968                                 case 2:
8969                                         sense_key = KEY_ABORTED_COMMAND;
8970                                         add_sense_code =
8971                                             DIAGNOSTIC_FAILURE_ON_COMPONENT;
8972                                         add_sense_code_qual = SCSI_COMPONENT_82;
8973                                         break;
8974                                 case 3:
8975                                         sense_key = KEY_ABORTED_COMMAND;
8976                                         add_sense_code =
8977                                             DIAGNOSTIC_FAILURE_ON_COMPONENT;
8978                                         add_sense_code_qual = SCSI_COMPONENT_83;
8979                                         break;
8980                                 case 4:
8981                                         sense_key = KEY_HARDWARE_ERROR;
8982                                         add_sense_code =
8983                                             DIAGNOSTIC_FAILURE_ON_COMPONENT;
8984                                         add_sense_code_qual = SCSI_COMPONENT_84;
8985                                         break;
8986                                 case 5:
8987                                         sense_key = KEY_HARDWARE_ERROR;
8988                                         add_sense_code =
8989                                             DIAGNOSTIC_FAILURE_ON_COMPONENT;
8990                                         add_sense_code_qual = SCSI_COMPONENT_85;
8991                                         break;
8992                                 case 6:
8993                                         sense_key = KEY_HARDWARE_ERROR;
8994                                         add_sense_code =
8995                                             DIAGNOSTIC_FAILURE_ON_COMPONENT;
8996                                         add_sense_code_qual = SCSI_COMPONENT_86;
8997                                         break;
8998                                 case 7:
8999                                         sense_key = KEY_MEDIUM_ERROR;
9000                                         add_sense_code =
9001                                             DIAGNOSTIC_FAILURE_ON_COMPONENT;
9002                                         add_sense_code_qual = SCSI_COMPONENT_87;
9003                                         break;
9004                                 case 8:
9005                                         sense_key = KEY_HARDWARE_ERROR;
9006                                         add_sense_code =
9007                                             DIAGNOSTIC_FAILURE_ON_COMPONENT;
9008                                         add_sense_code_qual = SCSI_COMPONENT_88;
9009                                         break;
9010                                 }
9011                                 code = 0;       /* unspecified */
9012                                 status |= (code << 4);
9013                                 lpp->param_values[0] = status;
9014                                 lpp->param_values[1] = 0; /* unspecified */
9015                                 lpp->param_values[2] = entry->
9016                                     smart_selftest_log_timestamp[1];
9017                                 lpp->param_values[3] = entry->
9018                                     smart_selftest_log_timestamp[0];
9019                                 if (status != 0) {
9020                                         lpp->param_values[4] = 0;
9021                                         lpp->param_values[5] = 0;
9022                                         lpp->param_values[6] = 0;
9023                                         lpp->param_values[7] = 0;
9024                                         lpp->param_values[8] = entry->
9025                                             smart_selftest_log_failing_lba[3];
9026                                         lpp->param_values[9] = entry->
9027                                             smart_selftest_log_failing_lba[2];
9028                                         lpp->param_values[10] = entry->
9029                                             smart_selftest_log_failing_lba[1];
9030                                         lpp->param_values[11] = entry->
9031                                             smart_selftest_log_failing_lba[0];
9032                                 } else {        /* No block address */
9033                                         lpp->param_values[4] = 0xff;
9034                                         lpp->param_values[5] = 0xff;
9035                                         lpp->param_values[6] = 0xff;
9036                                         lpp->param_values[7] = 0xff;
9037                                         lpp->param_values[8] = 0xff;
9038                                         lpp->param_values[9] = 0xff;
9039                                         lpp->param_values[10] = 0xff;
9040                                         lpp->param_values[11] = 0xff;
9041                                 }
9042                                 lpp->param_values[12] = sense_key;
9043                                 lpp->param_values[13] = add_sense_code;
9044                                 lpp->param_values[14] = add_sense_code_qual;
9045                                 lpp->param_values[15] = 0; /* undefined */
9046 
9047                                 lpp = (struct log_parameter *)
9048                                     (((uint8_t *)lpp) +
9049                                     SCSI_LOG_PARAM_HDR_LEN +
9050                                     SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
9051                                 --index;        /* back up to previous entry */
9052                                 if (index < 0) {
9053                                         index =
9054                                             NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
9055                                 }
9056                                 entry = &selftest_log->
9057                                     smart_selftest_log_entries[index];
9058                         }
9059                 }
9060 done:
9061                 kmem_free(selftest_log, sizeof (struct smart_selftest_log));
9062         }
9063 
9064         return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
9065             SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
9066 }
9067 
9068 /*
9069  * sata_build_lsense_page_2f() is used to create the
9070  * SCSI LOG SENSE page 0x2f (informational exceptions)
9071  *
9072  * Takes a sata_drive_info t * and the address of a buffer
9073  * in which to create the page information as well as a sata_hba_inst_t *.
9074  *
9075  * Returns the number of bytes valid in the buffer.
9076  *
9077  * Because it invokes function(s) that send synchronously executed command
9078  * to the HBA, it cannot be called in the interrupt context.
9079  */
9080 static  int
9081 sata_build_lsense_page_2f(
9082         sata_drive_info_t *sdinfo,
9083         uint8_t *buf,
9084         sata_hba_inst_t *sata_hba_inst)
9085 {
9086         struct log_parameter *lpp = (struct log_parameter *)buf;
9087         int rval;
9088         uint8_t *smart_data;
9089         uint8_t temp;
9090         sata_id_t *sata_id;
9091 #define SMART_NO_TEMP   0xff
9092 
9093         lpp->param_code[0] = 0;
9094         lpp->param_code[1] = 0;
9095         lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
9096 
9097         /* Now get the SMART status w.r.t. threshold exceeded */
9098         rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
9099         switch (rval) {
9100         case 1:
9101                 lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
9102                 lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
9103                 break;
9104         case 0:
9105         case -1:        /* failed to get data */
9106                 lpp->param_values[0] = 0;    /* No failure predicted */
9107                 lpp->param_values[1] = 0;
9108                 break;
9109 #if defined(SATA_DEBUG)
9110         default:
9111                 cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
9112                 /* NOTREACHED */
9113 #endif
9114         }
9115 
9116         sata_id = &sdinfo->satadrv_id;
9117         if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
9118                 temp = SMART_NO_TEMP;
9119         else {
9120                 /* Now get the temperature */
9121                 smart_data = kmem_zalloc(512, KM_SLEEP);
9122                 rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
9123                     SCT_STATUS_LOG_PAGE, 1);
9124                 if (rval == -1)
9125                         temp = SMART_NO_TEMP;
9126                 else {
9127                         temp = smart_data[200];
9128                         if (temp & 0x80) {
9129                                 if (temp & 0x7f)
9130                                         temp = 0;
9131                                 else
9132                                         temp = SMART_NO_TEMP;
9133                         }
9134                 }
9135                 kmem_free(smart_data, 512);
9136         }
9137 
9138         lpp->param_values[2] = temp; /* most recent temperature */
9139         lpp->param_values[3] = 0;    /* required vendor specific byte */
9140 
9141         lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
9142 
9143 
9144         return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
9145 }
9146 
9147 /*
9148  * sata_build_lsense_page_30() is used to create the
9149  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
9150  *
9151  * Takes a sata_drive_info t * and the address of a buffer
9152  * in which to create the page information as well as a sata_hba_inst_t *.
9153  *
9154  * Returns the number of bytes valid in the buffer.
9155  */
9156 static int
9157 sata_build_lsense_page_30(
9158         sata_drive_info_t *sdinfo,
9159         uint8_t *buf,
9160         sata_hba_inst_t *sata_hba_inst)
9161 {
9162         struct smart_data *smart_data = (struct smart_data *)buf;
9163         int rval;
9164 
9165         /* Now do the SMART READ DATA */
9166         rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
9167         if (rval == -1)
9168                 return (0);
9169 
9170         return (sizeof (struct smart_data));
9171 }
9172 
9173 /*
9174  * sata_build_lsense_page_0e() is used to create the
9175  * SCSI LOG SENSE page 0e (start-stop cycle counter page)
9176  *
9177  * Date of Manufacture (0x0001)
9178  *      YEAR = "0000"
9179  *      WEEK = "00"
9180  * Accounting Date (0x0002)
9181  *      6 ASCII space character(20h)
9182  * Specified cycle count over device lifetime
9183  *      VALUE - THRESH - the delta between max and min;
9184  * Accumulated start-stop cycles
9185  *      VALUE - WORST - the accumulated cycles;
9186  *
9187  * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute
9188  *
9189  * Takes a sata_drive_info t * and the address of a buffer
9190  * in which to create the page information as well as a sata_hba_inst_t *.
9191  *
9192  * Returns the number of bytes valid in the buffer.
9193  */
9194 static  int
9195 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf,
9196         sata_pkt_txlate_t *spx)
9197 {
9198         struct start_stop_cycle_counter_log *log_page;
9199         int i, rval, index;
9200         uint8_t smart_data[512], id, value, worst, thresh;
9201         uint32_t max_count, cycles;
9202 
9203         /* Now do the SMART READ DATA */
9204         rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo,
9205             (struct smart_data *)smart_data);
9206         if (rval == -1)
9207                 return (0);
9208         for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) {
9209                 index = (i * 12) + 2;
9210                 id = smart_data[index];
9211                 if (id != SMART_START_STOP_COUNT_ID)
9212                         continue;
9213                 else {
9214                         thresh = smart_data[index + 2];
9215                         value = smart_data[index + 3];
9216                         worst = smart_data[index + 4];
9217                         break;
9218                 }
9219         }
9220         if (id != SMART_START_STOP_COUNT_ID)
9221                 return (0);
9222         max_count = value - thresh;
9223         cycles = value - worst;
9224 
9225         log_page = (struct start_stop_cycle_counter_log *)buf;
9226         bzero(log_page, sizeof (struct start_stop_cycle_counter_log));
9227         log_page->code = 0x0e;
9228         log_page->page_len_low = 0x24;
9229 
9230         log_page->manufactor_date_low = 0x1;
9231         log_page->param_1.fmt_link = 0x1; /* 01b */
9232         log_page->param_len_1 = 0x06;
9233         for (i = 0; i < 4; i++) {
9234                 log_page->year_manu[i] = 0x30;
9235                 if (i < 2)
9236                         log_page->week_manu[i] = 0x30;
9237         }
9238 
9239         log_page->account_date_low = 0x02;
9240         log_page->param_2.fmt_link = 0x01; /* 01b */
9241         log_page->param_len_2 = 0x06;
9242         for (i = 0; i < 4; i++) {
9243                 log_page->year_account[i] = 0x20;
9244                 if (i < 2)
9245                         log_page->week_account[i] = 0x20;
9246         }
9247 
9248         log_page->lifetime_code_low = 0x03;
9249         log_page->param_3.fmt_link = 0x03; /* 11b */
9250         log_page->param_len_3 = 0x04;
9251         /* VALUE - THRESH - the delta between max and min */
9252         log_page->cycle_code_low = 0x04;
9253         log_page->param_4.fmt_link = 0x03; /* 11b */
9254         log_page->param_len_4 = 0x04;
9255         /* WORST - THRESH - the distance from 'now' to min */
9256 
9257         for (i = 0; i < 4; i++) {
9258                 log_page->cycle_lifetime[i] =
9259                     (max_count >> (8 * (3 - i))) & 0xff;
9260                 log_page->cycle_accumulated[i] =
9261                     (cycles >> (8 * (3 - i))) & 0xff;
9262         }
9263 
9264         return (sizeof (struct start_stop_cycle_counter_log));
9265 }
9266 
9267 /*
9268  * This function was used for build a ATA read verify sector command
9269  */
9270 static void
9271 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba)
9272 {
9273         scmd->satacmd_cmd_reg = SATAC_RDVER;
9274         scmd->satacmd_addr_type = ATA_ADDR_LBA28;
9275         scmd->satacmd_flags.sata_special_regs = B_TRUE;
9276 
9277         scmd->satacmd_sec_count_lsb = sec & 0xff;
9278         scmd->satacmd_lba_low_lsb = lba & 0xff;
9279         scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
9280         scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
9281         scmd->satacmd_device_reg = (SATA_ADH_LBA | (lba >> 24) & 0xf);
9282         scmd->satacmd_features_reg = 0;
9283         scmd->satacmd_status_reg = 0;
9284         scmd->satacmd_error_reg = 0;
9285 }
9286 
9287 /*
9288  * This function was used for building an ATA
9289  * command, and only command register need to
9290  * be defined, other register will be zero or na.
9291  */
9292 static void
9293 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd)
9294 {
9295         scmd->satacmd_addr_type = 0;
9296         scmd->satacmd_cmd_reg = cmd;
9297         scmd->satacmd_device_reg = 0;
9298         scmd->satacmd_sec_count_lsb = 0;
9299         scmd->satacmd_lba_low_lsb = 0;
9300         scmd->satacmd_lba_mid_lsb = 0;
9301         scmd->satacmd_lba_high_lsb = 0;
9302         scmd->satacmd_features_reg = 0;
9303         scmd->satacmd_status_reg = 0;
9304         scmd->satacmd_error_reg = 0;
9305         scmd->satacmd_flags.sata_special_regs = B_TRUE;
9306 }
9307 
9308 /*
9309  * This function was used for changing the standby
9310  * timer format from SCSI to ATA.
9311  */
9312 static uint8_t
9313 sata_get_standby_timer(uint8_t *timer)
9314 {
9315         uint32_t i = 0, count = 0;
9316         uint8_t ata_count;
9317 
9318         for (i = 0; i < 4; i++) {
9319                 count = count << 8 | timer[i];
9320         }
9321 
9322         if (count == 0)
9323                 return (0);
9324 
9325         if (count >= 1 && count <= 12000)
9326                 ata_count = (count -1) / 50 + 1;
9327         else if (count > 12000 && count <= 12600)
9328                 ata_count = 0xfc;
9329         else if (count > 12601 && count <= 12750)
9330                 ata_count = 0xff;
9331         else if (count > 12750 && count <= 17999)
9332                 ata_count = 0xf1;
9333         else if (count > 18000 && count <= 198000)
9334                 ata_count = count / 18000 + 240;
9335         else
9336                 ata_count = 0xfd;
9337         return (ata_count);
9338 }
9339 
9340 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
9341 
9342 /*
9343  * Start command for ATAPI device.
9344  * This function processes scsi_pkt requests.
9345  * Now CD/DVD, tape and ATAPI disk devices are supported.
9346  * Most commands are packet without any translation into Packet Command.
9347  * Some may be trapped and executed as SATA commands (not clear which one).
9348  *
9349  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
9350  * execution).
9351  * Returns other TRAN_XXXX codes if command is not accepted or completed
9352  * (see return values for sata_hba_start()).
9353  *
9354  * Note:
9355  * Inquiry cdb format differs between transport version 2 and 3.
9356  * However, the transport version 3 devices that were checked did not adhere
9357  * to the specification (ignored MSB of the allocation length). Therefore,
9358  * the transport version is not checked, but Inquiry allocation length is
9359  * truncated to 255 bytes if the original allocation length set-up by the
9360  * target driver is greater than 255 bytes.
9361  */
9362 static int
9363 sata_txlt_atapi(sata_pkt_txlate_t *spx)
9364 {
9365         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9366         sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
9367         struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9368         sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
9369         sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
9370             &spx->txlt_sata_pkt->satapkt_device);
9371         kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
9372         int cdblen;
9373         int rval, reason;
9374         int synch;
9375         union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
9376 
9377         mutex_enter(cport_mutex);
9378 
9379         if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
9380             TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
9381                 mutex_exit(cport_mutex);
9382                 return (rval);
9383         }
9384 
9385         /*
9386          * ATAPI device executes some ATA commands in addition to those
9387          * commands sent via PACKET command. These ATA commands may be
9388          * executed by the regular SATA translation functions. None needs
9389          * to be captured now.
9390          *
9391          * Commands sent via PACKET command include:
9392          *      MMC command set for ATAPI CD/DVD device
9393          *      SSC command set for ATAPI TAPE device
9394          *      SBC command set for ATAPI disk device
9395          *
9396          */
9397 
9398         /* Check the size of cdb */
9399 
9400         switch (GETGROUP(cdbp)) {
9401         case CDB_GROUPID_3:   /* Reserved, per SPC-4 */
9402                 /*
9403                  * opcodes 0x7e and 0x7f identify variable-length CDBs and
9404                  * therefore require special handling.  Return failure, for now.
9405                  */
9406                 mutex_exit(cport_mutex);
9407                 return (TRAN_BADPKT);
9408 
9409         case CDB_GROUPID_6:   /* Vendor-specific, per SPC-4 */
9410         case CDB_GROUPID_7:   /* Vendor-specific, per SPC-4 */
9411                 /* obtain length from the scsi_pkt */
9412                 cdblen = scsipkt->pkt_cdblen;
9413                 break;
9414 
9415         default:
9416                 /* CDB's length is statically known, per SPC-4 */
9417                 cdblen = scsi_cdb_size[GETGROUP(cdbp)];
9418                 break;
9419         }
9420 
9421         if (cdblen <= 0 || cdblen > sdinfo->satadrv_atapi_cdb_len) {
9422                 sata_log(NULL, CE_WARN,
9423                     "sata: invalid ATAPI cdb length %d",
9424                     cdblen);
9425                 mutex_exit(cport_mutex);
9426                 return (TRAN_BADPKT);
9427         }
9428 
9429         SATAATAPITRACE(spx, cdblen);
9430 
9431         /*
9432          * For non-read/write commands we need to
9433          * map buffer
9434          */
9435         switch ((uint_t)scsipkt->pkt_cdbp[0]) {
9436         case SCMD_READ:
9437         case SCMD_READ_G1:
9438         case SCMD_READ_G5:
9439         case SCMD_READ_G4:
9440         case SCMD_WRITE:
9441         case SCMD_WRITE_G1:
9442         case SCMD_WRITE_G5:
9443         case SCMD_WRITE_G4:
9444                 break;
9445         default:
9446                 if (bp != NULL) {
9447                         if (bp->b_flags & (B_PHYS | B_PAGEIO))
9448                                 bp_mapin(bp);
9449                 }
9450                 break;
9451         }
9452         /*
9453          * scmd->satacmd_flags.sata_data_direction default -
9454          * SATA_DIR_NODATA_XFER - is set by
9455          * sata_txlt_generic_pkt_info().
9456          */
9457         if (scmd->satacmd_bp) {
9458                 if (scmd->satacmd_bp->b_flags & B_READ) {
9459                         scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9460                 } else {
9461                         scmd->satacmd_flags.sata_data_direction =
9462                             SATA_DIR_WRITE;
9463                 }
9464         }
9465 
9466         /*
9467          * Set up ATAPI packet command.
9468          */
9469 
9470         sata_atapi_packet_cmd_setup(scmd, sdinfo);
9471 
9472         /* Copy cdb into sata_cmd */
9473         scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9474         bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
9475         bcopy(cdbp, scmd->satacmd_acdb, cdblen);
9476 
9477         /* See note in the command header */
9478         if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
9479                 if (scmd->satacmd_acdb[3] != 0)
9480                         scmd->satacmd_acdb[4] = 255;
9481         }
9482 
9483 #ifdef SATA_DEBUG
9484         if (sata_debug_flags & SATA_DBG_ATAPI) {
9485                 uint8_t *p = scmd->satacmd_acdb;
9486                 char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
9487 
9488                 (void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
9489                     "%02x %02x %02x %02x %02x %02x %02x %02x "
9490                     "%2x %02x %02x %02x %02x %02x %02x %02x",
9491                     p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9492                     p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9493                 buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
9494                 cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
9495         }
9496 #endif
9497 
9498         /*
9499          * Preset request sense data to NO SENSE.
9500          * If there is no way to get error information via Request Sense,
9501          * the packet request sense data would not have to be modified by HBA,
9502          * but it could be returned as is.
9503          */
9504         bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
9505         sata_fixed_sense_data_preset(
9506             (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9507 
9508         if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
9509                 /* Need callback function */
9510                 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
9511                 synch = FALSE;
9512         } else
9513                 synch = TRUE;
9514 
9515         /* Transfer command to HBA */
9516         if (sata_hba_start(spx, &rval) != 0) {
9517                 /* Pkt not accepted for execution */
9518                 mutex_exit(cport_mutex);
9519                 return (rval);
9520         }
9521         mutex_exit(cport_mutex);
9522         /*
9523          * If execution is non-synchronous,
9524          * a callback function will handle potential errors, translate
9525          * the response and will do a callback to a target driver.
9526          * If it was synchronous, use the same framework callback to check
9527          * an execution status.
9528          */
9529         if (synch) {
9530                 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
9531                     "synchronous execution status %x\n",
9532                     spx->txlt_sata_pkt->satapkt_reason);
9533                 sata_txlt_atapi_completion(spx->txlt_sata_pkt);
9534         }
9535         return (TRAN_ACCEPT);
9536 }
9537 
9538 
9539 /*
9540  * ATAPI Packet command completion.
9541  *
9542  * Failure of the command passed via Packet command are considered device
9543  * error. SATA HBA driver would have to retrieve error data (via Request
9544  * Sense command delivered via error retrieval sata packet) and copy it
9545  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
9546  */
9547 static void
9548 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
9549 {
9550         sata_pkt_txlate_t *spx =
9551             (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
9552         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9553         struct scsi_extended_sense *sense;
9554         struct buf *bp;
9555         int rval;
9556 
9557 #ifdef SATA_DEBUG
9558         uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
9559 #endif
9560 
9561         scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
9562             STATE_SENT_CMD | STATE_GOT_STATUS;
9563 
9564         if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
9565                 /* Normal completion */
9566                 if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
9567                         scsipkt->pkt_state |= STATE_XFERRED_DATA;
9568                 scsipkt->pkt_reason = CMD_CMPLT;
9569                 *scsipkt->pkt_scbp = STATUS_GOOD;
9570                 if (spx->txlt_tmp_buf != NULL) {
9571                         /* Temporary buffer was used */
9572                         bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9573                         if (bp->b_flags & B_READ) {
9574                                 rval = ddi_dma_sync(
9575                                     spx->txlt_buf_dma_handle, 0, 0,
9576                                     DDI_DMA_SYNC_FORCPU);
9577                                 ASSERT(rval == DDI_SUCCESS);
9578                                 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
9579                                     bp->b_bcount);
9580                         }
9581                 }
9582         } else {
9583                 /*
9584                  * Something went wrong - analyze return
9585                  */
9586                 *scsipkt->pkt_scbp = STATUS_CHECK;
9587                 sense = sata_arq_sense(spx);
9588 
9589                 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
9590                         /*
9591                          * pkt_reason should be CMD_CMPLT for DEVICE ERROR.
9592                          * Under this condition ERR bit is set for ATA command,
9593                          * and CHK bit set for ATAPI command.
9594                          *
9595                          * Please check st_intr & sdintr about how pkt_reason
9596                          * is used.
9597                          */
9598                         scsipkt->pkt_reason = CMD_CMPLT;
9599 
9600                         /*
9601                          * We may not have ARQ data if there was a double
9602                          * error. But sense data in sata packet was pre-set
9603                          * with NO SENSE so it is valid even if HBA could
9604                          * not retrieve a real sense data.
9605                          * Just copy this sense data into scsi pkt sense area.
9606                          */
9607                         bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
9608                             SATA_ATAPI_MIN_RQSENSE_LEN);
9609 #ifdef SATA_DEBUG
9610                         if (sata_debug_flags & SATA_DBG_SCSI_IF) {
9611                                 sata_log(spx->txlt_sata_hba_inst, CE_WARN,
9612                                     "sata_txlt_atapi_completion: %02x\n"
9613                                     "RQSENSE:  %02x %02x %02x %02x %02x %02x "
9614                                     "          %02x %02x %02x %02x %02x %02x "
9615                                     "          %02x %02x %02x %02x %02x %02x\n",
9616                                     scsipkt->pkt_reason,
9617                                     rqsp[0], rqsp[1], rqsp[2], rqsp[3],
9618                                     rqsp[4], rqsp[5], rqsp[6], rqsp[7],
9619                                     rqsp[8], rqsp[9], rqsp[10], rqsp[11],
9620                                     rqsp[12], rqsp[13], rqsp[14], rqsp[15],
9621                                     rqsp[16], rqsp[17]);
9622                         }
9623 #endif
9624                 } else {
9625                         switch (sata_pkt->satapkt_reason) {
9626                         case SATA_PKT_PORT_ERROR:
9627                                 /*
9628                                  * We have no device data.
9629                                  */
9630                                 scsipkt->pkt_reason = CMD_INCOMPLETE;
9631                                 scsipkt->pkt_state &= ~(STATE_GOT_BUS |
9632                                     STATE_GOT_TARGET | STATE_SENT_CMD |
9633                                     STATE_GOT_STATUS);
9634                                 sense->es_key = KEY_HARDWARE_ERROR;
9635                                 break;
9636 
9637                         case SATA_PKT_TIMEOUT:
9638                                 scsipkt->pkt_reason = CMD_TIMEOUT;
9639                                 scsipkt->pkt_statistics |=
9640                                     STAT_TIMEOUT | STAT_DEV_RESET;
9641                                 /*
9642                                  * Need to check if HARDWARE_ERROR/
9643                                  * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
9644                                  * appropriate.
9645                                  */
9646                                 break;
9647 
9648                         case SATA_PKT_ABORTED:
9649                                 scsipkt->pkt_reason = CMD_ABORTED;
9650                                 scsipkt->pkt_statistics |= STAT_ABORTED;
9651                                 /* Should we set key COMMAND_ABPRTED? */
9652                                 break;
9653 
9654                         case SATA_PKT_RESET:
9655                                 scsipkt->pkt_reason = CMD_RESET;
9656                                 scsipkt->pkt_statistics |= STAT_DEV_RESET;
9657                                 /*
9658                                  * May be we should set Unit Attention /
9659                                  * Reset. Perhaps the same should be
9660                                  * returned for disks....
9661                                  */
9662                                 sense->es_key = KEY_UNIT_ATTENTION;
9663                                 sense->es_add_code = SD_SCSI_ASC_RESET;
9664                                 break;
9665 
9666                         default:
9667                                 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9668                                     "sata_txlt_atapi_completion: "
9669                                     "invalid packet completion reason"));
9670                                 scsipkt->pkt_reason = CMD_TRAN_ERR;
9671                                 scsipkt->pkt_state &= ~(STATE_GOT_BUS |
9672                                     STATE_GOT_TARGET | STATE_SENT_CMD |
9673                                     STATE_GOT_STATUS);
9674                                 break;
9675                         }
9676                 }
9677         }
9678 
9679         SATAATAPITRACE(spx, 0);
9680 
9681         if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
9682             scsipkt->pkt_comp != NULL) {
9683                 /* scsi callback required */
9684                 (*scsipkt->pkt_comp)(scsipkt);
9685         }
9686 }
9687 
9688 /*
9689  * Set up error retrieval sata command for ATAPI Packet Command error data
9690  * recovery.
9691  *
9692  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
9693  * returns SATA_FAILURE otherwise.
9694  */
9695 
9696 static int
9697 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
9698 {
9699         sata_pkt_t *spkt = spx->txlt_sata_pkt;
9700         sata_cmd_t *scmd;
9701         struct buf *bp;
9702 
9703         /*
9704          * Allocate dma-able buffer error data.
9705          * Buffer allocation will take care of buffer alignment and other DMA
9706          * attributes.
9707          */
9708         bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
9709         if (bp == NULL) {
9710                 SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
9711                     "sata_get_err_retrieval_pkt: "
9712                     "cannot allocate buffer for error data", NULL);
9713                 return (SATA_FAILURE);
9714         }
9715         bp_mapin(bp); /* make data buffer accessible */
9716 
9717         /* Operation modes are up to the caller */
9718         spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
9719 
9720         /* Synchronous mode, no callback - may be changed by the caller */
9721         spkt->satapkt_comp = NULL;
9722         spkt->satapkt_time = sata_default_pkt_time;
9723 
9724         scmd = &spkt->satapkt_cmd;
9725         scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9726         scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
9727 
9728         sata_atapi_packet_cmd_setup(scmd, sdinfo);
9729 
9730         /*
9731          * Set-up acdb. Request Sense CDB (packet command content) is
9732          * not in DMA-able buffer. Its handling is HBA-specific (how
9733          * it is transfered into packet FIS).
9734          */
9735         scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9736         bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
9737         /* Following zeroing of pad bytes may not be necessary */
9738         bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
9739             sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
9740 
9741         /*
9742          * Set-up pointer to the buffer handle, so HBA can sync buffer
9743          * before accessing it. Handle is in usual place in translate struct.
9744          */
9745         scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
9746 
9747         /*
9748          * Preset request sense data to NO SENSE.
9749          * Here it is redundant, only for a symetry with scsi-originated
9750          * packets. It should not be used for anything but debugging.
9751          */
9752         bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
9753         sata_fixed_sense_data_preset(
9754             (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9755 
9756         ASSERT(scmd->satacmd_num_dma_cookies != 0);
9757         ASSERT(scmd->satacmd_dma_cookie_list != NULL);
9758 
9759         return (SATA_SUCCESS);
9760 }
9761 
9762 /*
9763  * Set-up ATAPI packet command.
9764  * Data transfer direction has to be set-up in sata_cmd structure prior to
9765  * calling this function.
9766  *
9767  * Returns void
9768  */
9769 
9770 static void
9771 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
9772 {
9773         scmd->satacmd_addr_type = 0;         /* N/A */
9774         scmd->satacmd_sec_count_lsb = 0;     /* no tag */
9775         scmd->satacmd_lba_low_lsb = 0;               /* N/A */
9776         scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
9777         scmd->satacmd_lba_high_lsb =
9778             (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
9779         scmd->satacmd_cmd_reg = SATAC_PACKET;        /* Command */
9780 
9781         /*
9782          * We want all data to be transfered via DMA.
9783          * But specify it only if drive supports DMA and DMA mode is
9784          * selected - some drives are sensitive about it.
9785          * Hopefully it wil work for all drives....
9786          */
9787         if (sdinfo->satadrv_settings & SATA_DEV_DMA)
9788                 scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
9789 
9790         /*
9791          * Features register requires special care for devices that use
9792          * Serial ATA bridge - they need an explicit specification of
9793          * the data transfer direction for Packet DMA commands.
9794          * Setting this bit is harmless if DMA is not used.
9795          *
9796          * Many drives do not implement word 80, specifying what ATA/ATAPI
9797          * spec they follow.
9798          * We are arbitrarily following the latest SerialATA 2.6 spec,
9799          * which uses ATA/ATAPI 6 specification for Identify Data, unless
9800          * ATA/ATAPI-7 support is explicitly indicated.
9801          */
9802         if (sdinfo->satadrv_id.ai_majorversion != 0 &&
9803             sdinfo->satadrv_id.ai_majorversion != 0xffff &&
9804             (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
9805                 /*
9806                  * Specification of major version is valid and version 7
9807                  * is supported. It does automatically imply that all
9808                  * spec features are supported. For now, we assume that
9809                  * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
9810                  */
9811                 if ((sdinfo->satadrv_id.ai_dirdma &
9812                     SATA_ATAPI_ID_DMADIR_REQ) != 0) {
9813                         if (scmd->satacmd_flags.sata_data_direction ==
9814                             SATA_DIR_READ)
9815                         scmd->satacmd_features_reg |=
9816                             SATA_ATAPI_F_DATA_DIR_READ;
9817                 }
9818         }
9819 }
9820 
9821 
9822 #ifdef SATA_DEBUG
9823 
9824 /* Display 18 bytes of Inquiry data */
9825 static void
9826 sata_show_inqry_data(uint8_t *buf)
9827 {
9828         struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
9829         uint8_t *p;
9830 
9831         cmn_err(CE_NOTE, "Inquiry data:");
9832         cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
9833         cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
9834         cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
9835         cmn_err(CE_NOTE, "ATAPI transport version %d",
9836             SATA_ATAPI_TRANS_VERSION(inq));
9837         cmn_err(CE_NOTE, "response data format %d, aenc %d",
9838             inq->inq_rdf, inq->inq_aenc);
9839         cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
9840         cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
9841         p = (uint8_t *)inq->inq_vid;
9842         cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
9843             "%02x %02x %02x %02x",
9844             p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
9845         p = (uint8_t *)inq->inq_vid;
9846         cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
9847             p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
9848 
9849         p = (uint8_t *)inq->inq_pid;
9850         cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
9851             "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
9852             p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9853             p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9854         p = (uint8_t *)inq->inq_pid;
9855         cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
9856             "%c %c %c %c %c %c %c %c",
9857             p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9858             p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9859 
9860         p = (uint8_t *)inq->inq_revision;
9861         cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
9862             p[0], p[1], p[2], p[3]);
9863         p = (uint8_t *)inq->inq_revision;
9864         cmn_err(CE_NOTE, "revision: %c %c %c %c",
9865             p[0], p[1], p[2], p[3]);
9866 
9867 }
9868 
9869 
9870 static void
9871 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
9872 {
9873         struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
9874 
9875         if (scsi_pkt == NULL)
9876                 return;
9877         if (count != 0) {
9878                 /* saving cdb */
9879                 bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
9880                     SATA_ATAPI_MAX_CDB_LEN);
9881                 bcopy(scsi_pkt->pkt_cdbp,
9882                     sata_atapi_trace[sata_atapi_trace_index].acdb, count);
9883         } else {
9884                 bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
9885                     sts_sensedata,
9886                     sata_atapi_trace[sata_atapi_trace_index].arqs,
9887                     SATA_ATAPI_MIN_RQSENSE_LEN);
9888                 sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
9889                     scsi_pkt->pkt_reason;
9890                 sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
9891                     spx->txlt_sata_pkt->satapkt_reason;
9892 
9893                 if (++sata_atapi_trace_index >= 64)
9894                         sata_atapi_trace_index = 0;
9895         }
9896 }
9897 
9898 #endif
9899 
9900 /*
9901  * Fetch inquiry data from ATAPI device
9902  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
9903  *
9904  * Note:
9905  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
9906  * where the caller expects to see the inquiry data.
9907  *
9908  */
9909 
9910 static int
9911 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
9912     sata_address_t *saddr, struct scsi_inquiry *inq)
9913 {
9914         sata_pkt_txlate_t *spx;
9915         sata_pkt_t *spkt;
9916         struct buf *bp;
9917         sata_drive_info_t *sdinfo;
9918         sata_cmd_t *scmd;
9919         int rval;
9920         uint8_t *rqsp;
9921         dev_info_t *dip = SATA_DIP(sata_hba);
9922 #ifdef SATA_DEBUG
9923         char msg_buf[MAXPATHLEN];
9924 #endif
9925         kmutex_t *cport_mutex;
9926 
9927         ASSERT(sata_hba != NULL);
9928 
9929         spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
9930         spx->txlt_sata_hba_inst = sata_hba;
9931         spx->txlt_scsi_pkt = NULL;           /* No scsi pkt involved */
9932         spkt = sata_pkt_alloc(spx, NULL);
9933         if (spkt == NULL) {
9934                 kmem_free(spx, sizeof (sata_pkt_txlate_t));
9935                 return (SATA_FAILURE);
9936         }
9937         /* address is needed now */
9938         spkt->satapkt_device.satadev_addr = *saddr;
9939 
9940         /* scsi_inquiry size buffer */
9941         bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
9942         if (bp == NULL) {
9943                 sata_pkt_free(spx);
9944                 kmem_free(spx, sizeof (sata_pkt_txlate_t));
9945                 SATA_LOG_D((sata_hba, CE_WARN,
9946                     "sata_get_atapi_inquiry_data: "
9947                     "cannot allocate data buffer"));
9948                 return (SATA_FAILURE);
9949         }
9950         bp_mapin(bp); /* make data buffer accessible */
9951 
9952         scmd = &spkt->satapkt_cmd;
9953         ASSERT(scmd->satacmd_num_dma_cookies != 0);
9954         ASSERT(scmd->satacmd_dma_cookie_list != NULL);
9955 
9956         /* Use synchronous mode */
9957         spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
9958         spkt->satapkt_comp = NULL;
9959         spkt->satapkt_time = sata_default_pkt_time;
9960 
9961         /* Issue inquiry command - 6 bytes cdb, data transfer, read */
9962 
9963         scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9964         scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
9965 
9966         cport_mutex = &(SATA_CPORT_MUTEX(sata_hba, saddr->cport));
9967         mutex_enter(cport_mutex);
9968         sdinfo = sata_get_device_info(sata_hba,
9969             &spx->txlt_sata_pkt->satapkt_device);
9970         if (sdinfo == NULL) {
9971                 /* we have to be carefull about the disapearing device */
9972                 mutex_exit(cport_mutex);
9973                 rval = SATA_FAILURE;
9974                 goto cleanup;
9975         }
9976         sata_atapi_packet_cmd_setup(scmd, sdinfo);
9977 
9978         /*
9979          * Set-up acdb. This works for atapi transport version 2 and later.
9980          */
9981         scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9982         bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
9983         scmd->satacmd_acdb[0] = 0x12;        /* Inquiry */
9984         scmd->satacmd_acdb[1] = 0x00;
9985         scmd->satacmd_acdb[2] = 0x00;
9986         scmd->satacmd_acdb[3] = 0x00;
9987         scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
9988         scmd->satacmd_acdb[5] = 0x00;
9989 
9990         sata_fixed_sense_data_preset(
9991             (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9992 
9993         /* Transfer command to HBA */
9994         if (sata_hba_start(spx, &rval) != 0) {
9995                 /* Pkt not accepted for execution */
9996                 SATADBG1(SATA_DBG_ATAPI, sata_hba,
9997                     "sata_get_atapi_inquiry_data: "
9998                     "Packet not accepted for execution - ret: %02x", rval);
9999                 mutex_exit(cport_mutex);
10000                 rval = SATA_FAILURE;
10001                 goto cleanup;
10002         }
10003         mutex_exit(cport_mutex);
10004 
10005         if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10006                 SATADBG1(SATA_DBG_ATAPI, sata_hba,
10007                     "sata_get_atapi_inquiry_data: "
10008                     "Packet completed successfully - ret: %02x", rval);
10009                 if (spx->txlt_buf_dma_handle != NULL) {
10010                         /*
10011                          * Sync buffer. Handle is in usual place in translate
10012                          * struct.
10013                          */
10014                         rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10015                             DDI_DMA_SYNC_FORCPU);
10016                         ASSERT(rval == DDI_SUCCESS);
10017                 }
10018 
10019                 if (sata_check_for_dma_error(dip, spx)) {
10020                         ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
10021                         rval = SATA_FAILURE;
10022                 } else {
10023                         /*
10024                          * Normal completion - copy data into caller's buffer
10025                          */
10026                         bcopy(bp->b_un.b_addr, (uint8_t *)inq,
10027                             sizeof (struct scsi_inquiry));
10028 #ifdef SATA_DEBUG
10029                         if (sata_debug_flags & SATA_DBG_ATAPI) {
10030                                 sata_show_inqry_data((uint8_t *)inq);
10031                         }
10032 #endif
10033                         rval = SATA_SUCCESS;
10034                 }
10035         } else {
10036                 /*
10037                  * Something went wrong - analyze return - check rqsense data
10038                  */
10039                 rval = SATA_FAILURE;
10040                 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10041                         /*
10042                          * ARQ data hopefull show something other than NO SENSE
10043                          */
10044                         rqsp = scmd->satacmd_rqsense;
10045 #ifdef SATA_DEBUG
10046                         if (sata_debug_flags & SATA_DBG_ATAPI) {
10047                                 msg_buf[0] = '\0';
10048                                 (void) snprintf(msg_buf, MAXPATHLEN,
10049                                     "ATAPI packet completion reason: %02x\n"
10050                                     "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
10051                                     "          %02x %02x %02x %02x %02x %02x\n"
10052                                     "          %02x %02x %02x %02x %02x %02x",
10053                                     spkt->satapkt_reason,
10054                                     rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10055                                     rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10056                                     rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10057                                     rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10058                                     rqsp[16], rqsp[17]);
10059                                 sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10060                                     "%s", msg_buf);
10061                         }
10062 #endif
10063                 } else {
10064                         switch (spkt->satapkt_reason) {
10065                         case SATA_PKT_PORT_ERROR:
10066                                 SATADBG1(SATA_DBG_ATAPI, sata_hba,
10067                                     "sata_get_atapi_inquiry_data: "
10068                                     "packet reason: port error", NULL);
10069                                 break;
10070 
10071                         case SATA_PKT_TIMEOUT:
10072                                 SATADBG1(SATA_DBG_ATAPI, sata_hba,
10073                                     "sata_get_atapi_inquiry_data: "
10074                                     "packet reason: timeout", NULL);
10075                                 break;
10076 
10077                         case SATA_PKT_ABORTED:
10078                                 SATADBG1(SATA_DBG_ATAPI, sata_hba,
10079                                     "sata_get_atapi_inquiry_data: "
10080                                     "packet reason: aborted", NULL);
10081                                 break;
10082 
10083                         case SATA_PKT_RESET:
10084                                 SATADBG1(SATA_DBG_ATAPI, sata_hba,
10085                                     "sata_get_atapi_inquiry_data: "
10086                                     "packet reason: reset\n", NULL);
10087                                 break;
10088                         default:
10089                                 SATADBG1(SATA_DBG_ATAPI, sata_hba,
10090                                     "sata_get_atapi_inquiry_data: "
10091                                     "invalid packet reason: %02x\n",
10092                                     spkt->satapkt_reason);
10093                                 break;
10094                         }
10095                 }
10096         }
10097 cleanup:
10098         sata_free_local_buffer(spx);
10099         sata_pkt_free(spx);
10100         kmem_free(spx, sizeof (sata_pkt_txlate_t));
10101         return (rval);
10102 }
10103 
10104 
10105 
10106 
10107 
10108 #if 0
10109 #ifdef SATA_DEBUG
10110 
10111 /*
10112  * Test ATAPI packet command.
10113  * Single threaded test: send packet command in synch mode, process completion
10114  *
10115  */
10116 static void
10117 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
10118 {
10119         sata_pkt_txlate_t *spx;
10120         sata_pkt_t *spkt;
10121         struct buf *bp;
10122         sata_device_t sata_device;
10123         sata_drive_info_t *sdinfo;
10124         sata_cmd_t *scmd;
10125         int rval;
10126         uint8_t *rqsp;
10127 
10128         ASSERT(sata_hba_inst != NULL);
10129         sata_device.satadev_addr.cport = cport;
10130         sata_device.satadev_addr.pmport = 0;
10131         sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
10132         sata_device.satadev_rev = SATA_DEVICE_REV;
10133         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10134         sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
10135         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10136         if (sdinfo == NULL) {
10137                 sata_log(sata_hba_inst, CE_WARN,
10138                     "sata_test_atapi_packet_command: "
10139                     "no device info for cport %d",
10140                     sata_device.satadev_addr.cport);
10141                 return;
10142         }
10143 
10144         spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10145         spx->txlt_sata_hba_inst = sata_hba_inst;
10146         spx->txlt_scsi_pkt = NULL;           /* No scsi pkt involved */
10147         spkt = sata_pkt_alloc(spx, NULL);
10148         if (spkt == NULL) {
10149                 kmem_free(spx, sizeof (sata_pkt_txlate_t));
10150                 return;
10151         }
10152         /* address is needed now */
10153         spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
10154 
10155         /* 1024k buffer */
10156         bp = sata_alloc_local_buffer(spx, 1024);
10157         if (bp == NULL) {
10158                 sata_pkt_free(spx);
10159                 kmem_free(spx, sizeof (sata_pkt_txlate_t));
10160                 sata_log(sata_hba_inst, CE_WARN,
10161                     "sata_test_atapi_packet_command: "
10162                     "cannot allocate data buffer");
10163                 return;
10164         }
10165         bp_mapin(bp); /* make data buffer accessible */
10166 
10167         scmd = &spkt->satapkt_cmd;
10168         ASSERT(scmd->satacmd_num_dma_cookies != 0);
10169         ASSERT(scmd->satacmd_dma_cookie_list != NULL);
10170 
10171         /* Use synchronous mode */
10172         spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10173 
10174         /* Synchronous mode, no callback - may be changed by the caller */
10175         spkt->satapkt_comp = NULL;
10176         spkt->satapkt_time = sata_default_pkt_time;
10177 
10178         /* Issue inquiry command - 6 bytes cdb, data transfer, read */
10179 
10180         scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10181         scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10182 
10183         sata_atapi_packet_cmd_setup(scmd, sdinfo);
10184 
10185         /* Set-up acdb. */
10186         scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
10187         bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
10188         scmd->satacmd_acdb[0] = 0x12;        /* Inquiry */
10189         scmd->satacmd_acdb[1] = 0x00;
10190         scmd->satacmd_acdb[2] = 0x00;
10191         scmd->satacmd_acdb[3] = 0x00;
10192         scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
10193         scmd->satacmd_acdb[5] = 0x00;
10194 
10195         sata_fixed_sense_data_preset(
10196             (struct scsi_extended_sense *)scmd->satacmd_rqsense);
10197 
10198         /* Transfer command to HBA */
10199         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10200         if (sata_hba_start(spx, &rval) != 0) {
10201                 /* Pkt not accepted for execution */
10202                 sata_log(sata_hba_inst, CE_WARN,
10203                     "sata_test_atapi_packet_command: "
10204                     "Packet not accepted for execution - ret: %02x", rval);
10205                 mutex_exit(
10206                     &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10207                 goto cleanup;
10208         }
10209         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10210 
10211         if (spx->txlt_buf_dma_handle != NULL) {
10212                 /*
10213                  * Sync buffer. Handle is in usual place in translate struct.
10214                  */
10215                 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10216                     DDI_DMA_SYNC_FORCPU);
10217                 ASSERT(rval == DDI_SUCCESS);
10218         }
10219         if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10220                 sata_log(sata_hba_inst, CE_WARN,
10221                     "sata_test_atapi_packet_command: "
10222                     "Packet completed successfully");
10223                 /*
10224                  * Normal completion - show inquiry data
10225                  */
10226                 sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
10227         } else {
10228                 /*
10229                  * Something went wrong - analyze return - check rqsense data
10230                  */
10231                 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10232                         /*
10233                          * ARQ data hopefull show something other than NO SENSE
10234                          */
10235                         rqsp = scmd->satacmd_rqsense;
10236                         sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10237                             "ATAPI packet completion reason: %02x\n"
10238                             "RQSENSE:  %02x %02x %02x %02x %02x %02x "
10239                             "          %02x %02x %02x %02x %02x %02x "
10240                             "          %02x %02x %02x %02x %02x %02x\n",
10241                             spkt->satapkt_reason,
10242                             rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10243                             rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10244                             rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10245                             rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10246                             rqsp[16], rqsp[17]);
10247                 } else {
10248                         switch (spkt->satapkt_reason) {
10249                         case SATA_PKT_PORT_ERROR:
10250                                 sata_log(sata_hba_inst, CE_WARN,
10251                                     "sata_test_atapi_packet_command: "
10252                                     "packet reason: port error\n");
10253                                 break;
10254 
10255                         case SATA_PKT_TIMEOUT:
10256                                 sata_log(sata_hba_inst, CE_WARN,
10257                                     "sata_test_atapi_packet_command: "
10258                                     "packet reason: timeout\n");
10259                                 break;
10260 
10261                         case SATA_PKT_ABORTED:
10262                                 sata_log(sata_hba_inst, CE_WARN,
10263                                     "sata_test_atapi_packet_command: "
10264                                     "packet reason: aborted\n");
10265                                 break;
10266 
10267                         case SATA_PKT_RESET:
10268                                 sata_log(sata_hba_inst, CE_WARN,
10269                                     "sata_test_atapi_packet_command: "
10270                                     "packet reason: reset\n");
10271                                 break;
10272                         default:
10273                                 sata_log(sata_hba_inst, CE_WARN,
10274                                     "sata_test_atapi_packet_command: "
10275                                     "invalid packet reason: %02x\n",
10276                                     spkt->satapkt_reason);
10277                                 break;
10278                         }
10279                 }
10280         }
10281 cleanup:
10282         sata_free_local_buffer(spx);
10283         sata_pkt_free(spx);
10284         kmem_free(spx, sizeof (sata_pkt_txlate_t));
10285 }
10286 
10287 #endif /* SATA_DEBUG */
10288 #endif /* 1 */
10289 
10290 
10291 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
10292 
10293 /*
10294  * Validate sata_tran info
10295  * SATA_FAILURE returns if structure is inconsistent or structure revision
10296  * does not match one used by the framework.
10297  *
10298  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
10299  * required function pointers.
10300  * Returns SATA_FAILURE otherwise.
10301  */
10302 static int
10303 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
10304 {
10305         /*
10306          * SATA_TRAN_HBA_REV is the current (highest) revision number
10307          * of the SATA interface.
10308          */
10309         if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
10310                 sata_log(NULL, CE_WARN,
10311                     "sata: invalid sata_hba_tran version %d for driver %s",
10312                     sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
10313                 return (SATA_FAILURE);
10314         }
10315 
10316         if (dip != sata_tran->sata_tran_hba_dip) {
10317                 SATA_LOG_D((NULL, CE_WARN,
10318                     "sata: inconsistent sata_tran_hba_dip "
10319                     "%p / %p", sata_tran->sata_tran_hba_dip, dip));
10320                 return (SATA_FAILURE);
10321         }
10322 
10323         if (sata_tran->sata_tran_probe_port == NULL ||
10324             sata_tran->sata_tran_start == NULL ||
10325             sata_tran->sata_tran_abort == NULL ||
10326             sata_tran->sata_tran_reset_dport == NULL ||
10327             sata_tran->sata_tran_hotplug_ops == NULL ||
10328             sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
10329             sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
10330             NULL) {
10331                 SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
10332                     "required functions"));
10333         }
10334         return (SATA_SUCCESS);
10335 }
10336 
10337 /*
10338  * Remove HBA instance from sata_hba_list.
10339  */
10340 static void
10341 sata_remove_hba_instance(dev_info_t *dip)
10342 {
10343         sata_hba_inst_t *sata_hba_inst;
10344 
10345         mutex_enter(&sata_mutex);
10346         for (sata_hba_inst = sata_hba_list;
10347             sata_hba_inst != (struct sata_hba_inst *)NULL;
10348             sata_hba_inst = sata_hba_inst->satahba_next) {
10349                 if (sata_hba_inst->satahba_dip == dip)
10350                         break;
10351         }
10352 
10353         if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
10354 #ifdef SATA_DEBUG
10355                 cmn_err(CE_WARN, "sata_remove_hba_instance: "
10356                     "unknown HBA instance\n");
10357 #endif
10358                 ASSERT(FALSE);
10359         }
10360         if (sata_hba_inst == sata_hba_list) {
10361                 sata_hba_list = sata_hba_inst->satahba_next;
10362                 if (sata_hba_list) {
10363                         sata_hba_list->satahba_prev =
10364                             (struct sata_hba_inst *)NULL;
10365                 }
10366                 if (sata_hba_inst == sata_hba_list_tail) {
10367                         sata_hba_list_tail = NULL;
10368                 }
10369         } else if (sata_hba_inst == sata_hba_list_tail) {
10370                 sata_hba_list_tail = sata_hba_inst->satahba_prev;
10371                 if (sata_hba_list_tail) {
10372                         sata_hba_list_tail->satahba_next =
10373                             (struct sata_hba_inst *)NULL;
10374                 }
10375         } else {
10376                 sata_hba_inst->satahba_prev->satahba_next =
10377                     sata_hba_inst->satahba_next;
10378                 sata_hba_inst->satahba_next->satahba_prev =
10379                     sata_hba_inst->satahba_prev;
10380         }
10381         mutex_exit(&sata_mutex);
10382 }
10383 
10384 /*
10385  * Probe all SATA ports of the specified HBA instance.
10386  * The assumption is that there are no target and attachment point minor nodes
10387  * created by the boot subsystems, so we do not need to prune device tree.
10388  *
10389  * This function is called only from sata_hba_attach(). It does not have to
10390  * be protected by controller mutex, because the hba_attached flag is not set
10391  * yet and no one would be touching this HBA instance other than this thread.
10392  * Determines if port is active and what type of the device is attached
10393  * (if any). Allocates necessary structures for each port.
10394  *
10395  * An AP (Attachement Point) node is created for each SATA device port even
10396  * when there is no device attached.
10397  */
10398 
10399 static  void
10400 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
10401 {
10402         dev_info_t              *dip = SATA_DIP(sata_hba_inst);
10403         int                     ncport;
10404         sata_cport_info_t       *cportinfo;
10405         sata_drive_info_t       *drive;
10406         sata_device_t           sata_device;
10407         int                     rval;
10408         dev_t                   minor_number;
10409         char                    name[16];
10410         clock_t                 start_time, cur_time;
10411 
10412         /*
10413          * Probe controller ports first, to find port status and
10414          * any port multiplier attached.
10415          */
10416         for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
10417                 /* allocate cport structure */
10418                 cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
10419                 ASSERT(cportinfo != NULL);
10420                 mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
10421 
10422                 mutex_enter(&cportinfo->cport_mutex);
10423 
10424                 cportinfo->cport_addr.cport = ncport;
10425                 cportinfo->cport_addr.pmport = 0;
10426                 cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
10427                 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
10428                 cportinfo->cport_state |= SATA_STATE_PROBING;
10429                 SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
10430 
10431                 /*
10432                  * Regardless if a port is usable or not, create
10433                  * an attachment point
10434                  */
10435                 mutex_exit(&cportinfo->cport_mutex);
10436                 minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
10437                     ncport, 0, SATA_ADDR_CPORT);
10438                 (void) sprintf(name, "%d", ncport);
10439                 if (ddi_create_minor_node(dip, name, S_IFCHR,
10440                     minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
10441                     DDI_SUCCESS) {
10442                         sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
10443                             "cannot create SATA attachment point for port %d",
10444                             ncport);
10445                 }
10446 
10447                 /* Probe port */
10448                 start_time = ddi_get_lbolt();
10449         reprobe_cport:
10450                 sata_device.satadev_addr.cport = ncport;
10451                 sata_device.satadev_addr.pmport = 0;
10452                 sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
10453                 sata_device.satadev_rev = SATA_DEVICE_REV;
10454 
10455                 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10456                     (dip, &sata_device);
10457 
10458                 mutex_enter(&cportinfo->cport_mutex);
10459                 cportinfo->cport_scr = sata_device.satadev_scr;
10460                 if (rval != SATA_SUCCESS) {
10461                         /* Something went wrong? Fail the port */
10462                         cportinfo->cport_state = SATA_PSTATE_FAILED;
10463                         mutex_exit(&cportinfo->cport_mutex);
10464                         continue;
10465                 }
10466                 cportinfo->cport_state &= ~SATA_STATE_PROBING;
10467                 cportinfo->cport_state |= SATA_STATE_PROBED;
10468                 cportinfo->cport_dev_type = sata_device.satadev_type;
10469 
10470                 cportinfo->cport_state |= SATA_STATE_READY;
10471                 if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
10472                         mutex_exit(&cportinfo->cport_mutex);
10473                         continue;
10474                 }
10475                 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
10476                         /*
10477                          * There is some device attached.
10478                          * Allocate device info structure
10479                          */
10480                         if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
10481                                 mutex_exit(&cportinfo->cport_mutex);
10482                                 SATA_CPORTINFO_DRV_INFO(cportinfo) =
10483                                     kmem_zalloc(sizeof (sata_drive_info_t),
10484                                     KM_SLEEP);
10485                                 mutex_enter(&cportinfo->cport_mutex);
10486                         }
10487                         drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
10488                         drive->satadrv_addr = cportinfo->cport_addr;
10489                         drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
10490                         drive->satadrv_type = cportinfo->cport_dev_type;
10491                         drive->satadrv_state = SATA_STATE_UNKNOWN;
10492 
10493                         mutex_exit(&cportinfo->cport_mutex);
10494                         if (sata_add_device(dip, sata_hba_inst, &sata_device) !=
10495                             SATA_SUCCESS) {
10496                                 /*
10497                                  * Plugged device was not correctly identified.
10498                                  * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
10499                                  */
10500                                 cur_time = ddi_get_lbolt();
10501                                 if ((cur_time - start_time) <
10502                                     drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
10503                                         /* sleep for a while */
10504                                         delay(drv_usectohz(
10505                                             SATA_DEV_RETRY_DLY));
10506                                         goto reprobe_cport;
10507                                 }
10508                         }
10509                 } else { /* SATA_DTYPE_PMULT */
10510                         mutex_exit(&cportinfo->cport_mutex);
10511 
10512                         /* Allocate sata_pmult_info and sata_pmport_info */
10513                         if (sata_alloc_pmult(sata_hba_inst, &sata_device) !=
10514                             SATA_SUCCESS)
10515                                 continue;
10516 
10517                         /* Log the information of the port multiplier */
10518                         sata_show_pmult_info(sata_hba_inst, &sata_device);
10519 
10520                         /* Probe its pmports */
10521                         sata_probe_pmports(sata_hba_inst, ncport);
10522                 }
10523         }
10524 }
10525 
10526 /*
10527  * Probe all device ports behind a port multiplier.
10528  *
10529  * PMult-related structure should be allocated before by sata_alloc_pmult().
10530  *
10531  * NOTE1: Only called from sata_probe_ports()
10532  * NOTE2: No mutex should be hold.
10533  */
10534 static void
10535 sata_probe_pmports(sata_hba_inst_t *sata_hba_inst, uint8_t ncport)
10536 {
10537         dev_info_t              *dip = SATA_DIP(sata_hba_inst);
10538         sata_pmult_info_t       *pmultinfo = NULL;
10539         sata_pmport_info_t      *pmportinfo = NULL;
10540         sata_drive_info_t       *drive = NULL;
10541         sata_device_t           sata_device;
10542 
10543         clock_t                 start_time, cur_time;
10544         int                     npmport;
10545         int                     rval;
10546 
10547         pmultinfo = SATA_PMULT_INFO(sata_hba_inst, ncport);
10548 
10549         /* Probe Port Multiplier ports */
10550         for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; npmport++) {
10551                 pmportinfo = pmultinfo->pmult_dev_port[npmport];
10552                 start_time = ddi_get_lbolt();
10553 reprobe_pmport:
10554                 sata_device.satadev_addr.cport = ncport;
10555                 sata_device.satadev_addr.pmport = npmport;
10556                 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
10557                 sata_device.satadev_rev = SATA_DEVICE_REV;
10558 
10559                 /* Let HBA driver probe it. */
10560                 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10561                     (dip, &sata_device);
10562                 mutex_enter(&pmportinfo->pmport_mutex);
10563 
10564                 pmportinfo->pmport_scr = sata_device.satadev_scr;
10565 
10566                 if (rval != SATA_SUCCESS) {
10567                         pmportinfo->pmport_state =
10568                             SATA_PSTATE_FAILED;
10569                         mutex_exit(&pmportinfo->pmport_mutex);
10570                         continue;
10571                 }
10572                 pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
10573                 pmportinfo->pmport_state |= SATA_STATE_PROBED;
10574                 pmportinfo->pmport_dev_type = sata_device.satadev_type;
10575 
10576                 pmportinfo->pmport_state |= SATA_STATE_READY;
10577                 if (pmportinfo->pmport_dev_type ==
10578                     SATA_DTYPE_NONE) {
10579                         SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
10580                             "no device found at port %d:%d", ncport, npmport);
10581                         mutex_exit(&pmportinfo->pmport_mutex);
10582                         continue;
10583                 }
10584                 /* Port multipliers cannot be chained */
10585                 ASSERT(pmportinfo->pmport_dev_type != SATA_DTYPE_PMULT);
10586                 /*
10587                  * There is something attached to Port
10588                  * Multiplier device port
10589                  * Allocate device info structure
10590                  */
10591                 if (pmportinfo->pmport_sata_drive == NULL) {
10592                         mutex_exit(&pmportinfo->pmport_mutex);
10593                         pmportinfo->pmport_sata_drive =
10594                             kmem_zalloc(sizeof (sata_drive_info_t), KM_SLEEP);
10595                         mutex_enter(&pmportinfo->pmport_mutex);
10596                 }
10597                 drive = pmportinfo->pmport_sata_drive;
10598                 drive->satadrv_addr.cport = pmportinfo->pmport_addr.cport;
10599                 drive->satadrv_addr.pmport = npmport;
10600                 drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
10601                 drive->satadrv_type = pmportinfo-> pmport_dev_type;
10602                 drive->satadrv_state = SATA_STATE_UNKNOWN;
10603 
10604                 mutex_exit(&pmportinfo->pmport_mutex);
10605                 rval = sata_add_device(dip, sata_hba_inst, &sata_device);
10606 
10607                 if (rval != SATA_SUCCESS) {
10608                         /*
10609                          * Plugged device was not correctly identified.
10610                          * Retry, within the SATA_DEV_IDENTIFY_TIMEOUT
10611                          */
10612                         cur_time = ddi_get_lbolt();
10613                         if ((cur_time - start_time) < drv_usectohz(
10614                             SATA_DEV_IDENTIFY_TIMEOUT)) {
10615                                 /* sleep for a while */
10616                                 delay(drv_usectohz(SATA_DEV_RETRY_DLY));
10617                                 goto reprobe_pmport;
10618                         }
10619                 }
10620         }
10621 }
10622 
10623 /*
10624  * Add SATA device for specified HBA instance & port (SCSI target
10625  * device nodes).
10626  * This function is called (indirectly) only from sata_hba_attach().
10627  * A target node is created when there is a supported type device attached,
10628  * but may be removed if it cannot be put online.
10629  *
10630  * This function cannot be called from an interrupt context.
10631  *
10632  * Create target nodes for disk, CD/DVD, Tape and ATAPI disk devices
10633  *
10634  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
10635  * device identification failed - adding a device could be retried.
10636  *
10637  */
10638 static  int
10639 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst,
10640     sata_device_t *sata_device)
10641 {
10642         sata_cport_info_t       *cportinfo;
10643         sata_pmult_info_t       *pminfo;
10644         sata_pmport_info_t      *pmportinfo;
10645         dev_info_t              *cdip;          /* child dip */
10646         sata_address_t          *saddr = &sata_device->satadev_addr;
10647         uint8_t                 cport, pmport;
10648         int                     rval;
10649 
10650         cport = saddr->cport;
10651         pmport = saddr->pmport;
10652         cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10653         ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
10654 
10655         /*
10656          * Some device is attached to a controller port.
10657          * We rely on controllers distinquishing between no-device,
10658          * attached port multiplier and other kind of attached device.
10659          * We need to get Identify Device data and determine
10660          * positively the dev type before trying to attach
10661          * the target driver.
10662          */
10663         sata_device->satadev_rev = SATA_DEVICE_REV;
10664         switch (saddr->qual) {
10665         case SATA_ADDR_CPORT:
10666                 /*
10667                  * Add a non-port-multiplier device at controller port.
10668                  */
10669                 saddr->qual = SATA_ADDR_DCPORT;
10670 
10671                 rval = sata_probe_device(sata_hba_inst, sata_device);
10672                 if (rval != SATA_SUCCESS ||
10673                     sata_device->satadev_type == SATA_DTYPE_UNKNOWN)
10674                         return (SATA_FAILURE);
10675 
10676                 mutex_enter(&cportinfo->cport_mutex);
10677                 sata_show_drive_info(sata_hba_inst,
10678                     SATA_CPORTINFO_DRV_INFO(cportinfo));
10679 
10680                 if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
10681                         /*
10682                          * Could not determine device type or
10683                          * a device is not supported.
10684                          * Degrade this device to unknown.
10685                          */
10686                         cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
10687                         mutex_exit(&cportinfo->cport_mutex);
10688                         return (SATA_SUCCESS);
10689                 }
10690                 cportinfo->cport_dev_type = sata_device->satadev_type;
10691                 cportinfo->cport_tgtnode_clean = B_TRUE;
10692                 mutex_exit(&cportinfo->cport_mutex);
10693 
10694                 /*
10695                  * Initialize device to the desired state. Even if it
10696                  * fails, the device will still attach but syslog
10697                  * will show the warning.
10698                  */
10699                 if (sata_initialize_device(sata_hba_inst,
10700                     SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) {
10701                         /* Retry */
10702                         rval = sata_initialize_device(sata_hba_inst,
10703                             SATA_CPORTINFO_DRV_INFO(cportinfo));
10704 
10705                         if (rval == SATA_RETRY)
10706                                 sata_log(sata_hba_inst, CE_WARN,
10707                                     "SATA device at port %d - "
10708                                     "default device features could not be set."
10709                                     " Device may not operate as expected.",
10710                                     cport);
10711                 }
10712 
10713                 cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
10714                 if (cdip == NULL) {
10715                         /*
10716                          * Attaching target node failed.
10717                          * We retain sata_drive_info structure...
10718                          */
10719                         return (SATA_SUCCESS);
10720                 }
10721 
10722                 mutex_enter(&cportinfo->cport_mutex);
10723                 (SATA_CPORTINFO_DRV_INFO(cportinfo))->
10724                     satadrv_state = SATA_STATE_READY;
10725                 mutex_exit(&cportinfo->cport_mutex);
10726 
10727                 break;
10728 
10729         case SATA_ADDR_PMPORT:
10730                 saddr->qual = SATA_ADDR_DPMPORT;
10731 
10732                 mutex_enter(&cportinfo->cport_mutex);
10733                 /* It must be a Port Multiplier at the controller port */
10734                 ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
10735 
10736                 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
10737                 pmportinfo = pminfo->pmult_dev_port[saddr->pmport];
10738                 mutex_exit(&cportinfo->cport_mutex);
10739 
10740                 rval = sata_probe_device(sata_hba_inst, sata_device);
10741                 if (rval != SATA_SUCCESS ||
10742                     sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
10743                         return (SATA_FAILURE);
10744                 }
10745 
10746                 mutex_enter(&pmportinfo->pmport_mutex);
10747                 sata_show_drive_info(sata_hba_inst,
10748                     SATA_PMPORTINFO_DRV_INFO(pmportinfo));
10749 
10750                 if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
10751                         /*
10752                          * Could not determine device type.
10753                          * Degrade this device to unknown.
10754                          */
10755                         pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
10756                         mutex_exit(&pmportinfo->pmport_mutex);
10757                         return (SATA_SUCCESS);
10758                 }
10759                 pmportinfo->pmport_dev_type = sata_device->satadev_type;
10760                 pmportinfo->pmport_tgtnode_clean = B_TRUE;
10761                 mutex_exit(&pmportinfo->pmport_mutex);
10762 
10763                 /*
10764                  * Initialize device to the desired state.
10765                  * Even if it fails, the device will still
10766                  * attach but syslog will show the warning.
10767                  */
10768                 if (sata_initialize_device(sata_hba_inst,
10769                     pmportinfo->pmport_sata_drive) != SATA_SUCCESS) {
10770                         /* Retry */
10771                         rval = sata_initialize_device(sata_hba_inst,
10772                             pmportinfo->pmport_sata_drive);
10773 
10774                         if (rval == SATA_RETRY)
10775                                 sata_log(sata_hba_inst, CE_WARN,
10776                                     "SATA device at port %d:%d - "
10777                                     "default device features could not be set."
10778                                     " Device may not operate as expected.",
10779                                     cport, pmport);
10780                 }
10781 
10782                 cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
10783                 if (cdip == NULL) {
10784                         /*
10785                          * Attaching target node failed.
10786                          * We retain sata_drive_info structure...
10787                          */
10788                         return (SATA_SUCCESS);
10789                 }
10790                 mutex_enter(&pmportinfo->pmport_mutex);
10791                 pmportinfo->pmport_sata_drive->satadrv_state |=
10792                     SATA_STATE_READY;
10793                 mutex_exit(&pmportinfo->pmport_mutex);
10794 
10795                 break;
10796 
10797         default:
10798                 return (SATA_FAILURE);
10799         }
10800 
10801         return (SATA_SUCCESS);
10802 }
10803 
10804 /*
10805  * Clean up target node at specific address.
10806  *
10807  * NOTE: No Mutex should be hold.
10808  */
10809 static int
10810 sata_offline_device(sata_hba_inst_t *sata_hba_inst,
10811     sata_device_t *sata_device, sata_drive_info_t *sdinfo)
10812 {
10813         uint8_t cport, pmport, qual;
10814         dev_info_t *tdip;
10815 
10816         cport = sata_device->satadev_addr.cport;
10817         pmport = sata_device->satadev_addr.pmport;
10818         qual = sata_device->satadev_addr.qual;
10819 
10820         if (qual == SATA_ADDR_DCPORT) {
10821                 SATA_LOG_D((sata_hba_inst, CE_WARN,
10822                     "sata_hba_ioctl: disconnect device at port %d", cport));
10823         } else {
10824                 SATA_LOG_D((sata_hba_inst, CE_WARN,
10825                     "sata_hba_ioctl: disconnect device at port %d:%d",
10826                     cport, pmport));
10827         }
10828 
10829         /* We are addressing attached device, not a port */
10830         sata_device->satadev_addr.qual =
10831             sdinfo->satadrv_addr.qual;
10832         tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
10833             &sata_device->satadev_addr);
10834         if (tdip != NULL && ndi_devi_offline(tdip,
10835             NDI_DEVI_REMOVE) != NDI_SUCCESS) {
10836                 /*
10837                  * Problem :
10838                  * The target node remained attached.
10839                  * This happens when the device file was open
10840                  * or a node was waiting for resources.
10841                  * Cannot do anything about it.
10842                  */
10843                 if (qual == SATA_ADDR_DCPORT) {
10844                         SATA_LOG_D((sata_hba_inst, CE_WARN,
10845                             "sata_hba_ioctl: disconnect: could "
10846                             "not unconfigure device before "
10847                             "disconnecting the SATA port %d",
10848                             cport));
10849                 } else {
10850                         SATA_LOG_D((sata_hba_inst, CE_WARN,
10851                             "sata_hba_ioctl: disconnect: could "
10852                             "not unconfigure device before "
10853                             "disconnecting the SATA port %d:%d",
10854                             cport, pmport));
10855                 }
10856                 /*
10857                  * Set DEVICE REMOVED state in the target
10858                  * node. It will prevent access to the device
10859                  * even when a new device is attached, until
10860                  * the old target node is released, removed and
10861                  * recreated for a new  device.
10862                  */
10863                 sata_set_device_removed(tdip);
10864 
10865                 /*
10866                  * Instruct event daemon to try the target
10867                  * node cleanup later.
10868                  */
10869                 sata_set_target_node_cleanup(
10870                     sata_hba_inst, &sata_device->satadev_addr);
10871         }
10872 
10873 
10874         return (SATA_SUCCESS);
10875 }
10876 
10877 
10878 /*
10879  * Create scsi target node for attached device, create node properties and
10880  * attach the node.
10881  * The node could be removed if the device onlining fails.
10882  *
10883  * A dev_info_t pointer is returned if operation is successful, NULL is
10884  * returned otherwise.
10885  */
10886 
10887 static dev_info_t *
10888 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
10889                         sata_address_t *sata_addr)
10890 {
10891         dev_info_t *cdip = NULL;
10892         int rval;
10893         char *nname = NULL;
10894         char **compatible = NULL;
10895         int ncompatible;
10896         struct scsi_inquiry inq;
10897         sata_device_t sata_device;
10898         sata_drive_info_t *sdinfo;
10899         int target;
10900         int i;
10901 
10902         sata_device.satadev_rev = SATA_DEVICE_REV;
10903         sata_device.satadev_addr = *sata_addr;
10904 
10905         mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
10906 
10907         sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
10908 
10909         target = SATA_TO_SCSI_TARGET(sata_addr->cport,
10910             sata_addr->pmport, sata_addr->qual);
10911 
10912         if (sdinfo == NULL) {
10913                 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10914                     sata_addr->cport)));
10915                 SATA_LOG_D((sata_hba_inst, CE_WARN,
10916                     "sata_create_target_node: no sdinfo for target %x",
10917                     target));
10918                 return (NULL);
10919         }
10920 
10921         /*
10922          * create or get scsi inquiry data, expected by
10923          * scsi_hba_nodename_compatible_get()
10924          * SATA hard disks get Identify Data translated into Inguiry Data.
10925          * ATAPI devices respond directly to Inquiry request.
10926          */
10927         if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10928                 sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
10929                     (uint8_t *)&inq);
10930                 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10931                     sata_addr->cport)));
10932         } else { /* Assume supported ATAPI device */
10933                 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10934                     sata_addr->cport)));
10935                 if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
10936                     &inq) == SATA_FAILURE)
10937                         return (NULL);
10938                 /*
10939                  * Save supported ATAPI transport version
10940                  */
10941                 sdinfo->satadrv_atapi_trans_ver =
10942                     SATA_ATAPI_TRANS_VERSION(&inq);
10943         }
10944 
10945         /* determine the node name and compatible */
10946         scsi_hba_nodename_compatible_get(&inq, NULL,
10947             inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
10948 
10949 #ifdef SATA_DEBUG
10950         if (sata_debug_flags & SATA_DBG_NODES) {
10951                 if (nname == NULL) {
10952                         cmn_err(CE_NOTE, "sata_create_target_node: "
10953                             "cannot determine nodename for target %d\n",
10954                             target);
10955                 } else {
10956                         cmn_err(CE_WARN, "sata_create_target_node: "
10957                             "target %d nodename: %s\n", target, nname);
10958                 }
10959                 if (compatible == NULL) {
10960                         cmn_err(CE_WARN,
10961                             "sata_create_target_node: no compatible name\n");
10962                 } else {
10963                         for (i = 0; i < ncompatible; i++) {
10964                                 cmn_err(CE_WARN, "sata_create_target_node: "
10965                                     "compatible name: %s\n", compatible[i]);
10966                         }
10967                 }
10968         }
10969 #endif
10970 
10971         /* if nodename can't be determined, log error and exit */
10972         if (nname == NULL) {
10973                 SATA_LOG_D((sata_hba_inst, CE_WARN,
10974                     "sata_create_target_node: cannot determine nodename "
10975                     "for target %d\n", target));
10976                 scsi_hba_nodename_compatible_free(nname, compatible);
10977                 return (NULL);
10978         }
10979         /*
10980          * Create scsi target node
10981          */
10982         ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
10983         rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
10984             "device-type", "scsi");
10985 
10986         if (rval != DDI_PROP_SUCCESS) {
10987                 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
10988                     "updating device_type prop failed %d", rval));
10989                 goto fail;
10990         }
10991 
10992         /*
10993          * Create target node properties: target & lun
10994          */
10995         rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
10996         if (rval != DDI_PROP_SUCCESS) {
10997                 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
10998                     "updating target prop failed %d", rval));
10999                 goto fail;
11000         }
11001         rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
11002         if (rval != DDI_PROP_SUCCESS) {
11003                 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11004                     "updating target prop failed %d", rval));
11005                 goto fail;
11006         }
11007 
11008         if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
11009                 /*
11010                  * Add "variant" property
11011                  */
11012                 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
11013                     "variant", "atapi");
11014                 if (rval != DDI_PROP_SUCCESS) {
11015                         SATA_LOG_D((sata_hba_inst, CE_WARN,
11016                             "sata_create_target_node: variant atapi "
11017                             "property could not be created: %d", rval));
11018                         goto fail;
11019                 }
11020         }
11021         /* decorate the node with compatible */
11022         if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
11023             compatible, ncompatible) != DDI_PROP_SUCCESS) {
11024                 SATA_LOG_D((sata_hba_inst, CE_WARN,
11025                     "sata_create_target_node: FAIL compatible props cdip 0x%p",
11026                     (void *)cdip));
11027                 goto fail;
11028         }
11029 
11030         if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11031                 /*
11032                  * Add "sata-phy" property
11033                  */
11034                 if (ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "sata-phy",
11035                     (int)sata_addr->cport) != DDI_PROP_SUCCESS) {
11036                         SATA_LOG_D((sata_hba_inst, CE_WARN,
11037                             "sata_create_target_node: failed to create "
11038                             "\"sata-phy\" property: port %d",
11039                             sata_addr->cport));
11040                 }
11041         }
11042 
11043 
11044         /*
11045          * Now, try to attach the driver. If probing of the device fails,
11046          * the target node may be removed
11047          */
11048         rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
11049 
11050         scsi_hba_nodename_compatible_free(nname, compatible);
11051 
11052         if (rval == NDI_SUCCESS)
11053                 return (cdip);
11054 
11055         /* target node was removed - are we sure? */
11056         return (NULL);
11057 
11058 fail:
11059         scsi_hba_nodename_compatible_free(nname, compatible);
11060         ddi_prop_remove_all(cdip);
11061         rval = ndi_devi_free(cdip);
11062         if (rval != NDI_SUCCESS) {
11063                 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11064                     "node removal failed %d", rval));
11065         }
11066         sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
11067             "cannot create target node for SATA device at port %d",
11068             sata_addr->cport);
11069         return (NULL);
11070 }
11071 
11072 /*
11073  * Remove a target node.
11074  */
11075 static void
11076 sata_remove_target_node(sata_hba_inst_t *sata_hba_inst,
11077                         sata_address_t *sata_addr)
11078 {
11079         dev_info_t *tdip;
11080         uint8_t cport = sata_addr->cport;
11081         uint8_t pmport = sata_addr->pmport;
11082         uint8_t qual = sata_addr->qual;
11083 
11084         /* Note the sata daemon uses the address of the port/pmport */
11085         ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11086 
11087         /* Remove target node */
11088         tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), cport, pmport);
11089         if (tdip != NULL) {
11090                 /*
11091                  * Target node exists.  Unconfigure device
11092                  * then remove the target node (one ndi
11093                  * operation).
11094                  */
11095                 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
11096                         /*
11097                          * PROBLEM - no device, but target node remained. This
11098                          * happens when the file was open or node was waiting
11099                          * for resources.
11100                          */
11101                         SATA_LOG_D((sata_hba_inst, CE_WARN,
11102                             "sata_remove_target_node: "
11103                             "Failed to remove target node for "
11104                             "detached SATA device."));
11105                         /*
11106                          * Set target node state to DEVI_DEVICE_REMOVED. But
11107                          * re-check first that the node still exists.
11108                          */
11109                         tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
11110                             cport, pmport);
11111                         if (tdip != NULL) {
11112                                 sata_set_device_removed(tdip);
11113                                 /*
11114                                  * Instruct event daemon to retry the cleanup
11115                                  * later.
11116                                  */
11117                                 sata_set_target_node_cleanup(sata_hba_inst,
11118                                     sata_addr);
11119                         }
11120                 }
11121 
11122                 if (qual == SATA_ADDR_CPORT)
11123                         sata_log(sata_hba_inst, CE_WARN,
11124                             "SATA device detached at port %d", cport);
11125                 else
11126                         sata_log(sata_hba_inst, CE_WARN,
11127                             "SATA device detached at port %d:%d",
11128                             cport, pmport);
11129         }
11130 #ifdef SATA_DEBUG
11131         else {
11132                 if (qual == SATA_ADDR_CPORT)
11133                         sata_log(sata_hba_inst, CE_WARN,
11134                             "target node not found at port %d", cport);
11135                 else
11136                         sata_log(sata_hba_inst, CE_WARN,
11137                             "target node not found at port %d:%d",
11138                             cport, pmport);
11139         }
11140 #endif
11141 }
11142 
11143 
11144 /*
11145  * Re-probe sata port, check for a device and attach info
11146  * structures when necessary. Identify Device data is fetched, if possible.
11147  * Assumption: sata address is already validated.
11148  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11149  * the presence of a device and its type.
11150  *
11151  * flag arg specifies that the function should try multiple times to identify
11152  * device type and to initialize it, or it should return immediately on failure.
11153  * SATA_DEV_IDENTIFY_RETRY - retry
11154  * SATA_DEV_IDENTIFY_NORETRY - no retry
11155  *
11156  * SATA_FAILURE is returned if one of the operations failed.
11157  *
11158  * This function cannot be called in interrupt context - it may sleep.
11159  *
11160  * Note: Port multiplier is supported.
11161  */
11162 static int
11163 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11164     int flag)
11165 {
11166         sata_cport_info_t *cportinfo;
11167         sata_pmult_info_t *pmultinfo;
11168         sata_drive_info_t *sdinfo, *osdinfo;
11169         boolean_t init_device = B_FALSE;
11170         int prev_device_type = SATA_DTYPE_NONE;
11171         int prev_device_settings = 0;
11172         int prev_device_state = 0;
11173         clock_t start_time;
11174         int retry = B_FALSE;
11175         uint8_t cport = sata_device->satadev_addr.cport;
11176         int rval_probe, rval_init;
11177 
11178         /*
11179          * If target is pmport, sata_reprobe_pmport() will handle it.
11180          */
11181         if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT ||
11182             sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT)
11183                 return (sata_reprobe_pmport(sata_hba_inst, sata_device, flag));
11184 
11185         /* We only care about host sata cport for now */
11186         cportinfo = SATA_CPORT_INFO(sata_hba_inst,
11187             sata_device->satadev_addr.cport);
11188 
11189         /*
11190          * If a port multiplier was previously attached (we have no idea it
11191          * still there or not), sata_reprobe_pmult() will handle it.
11192          */
11193         if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT)
11194                 return (sata_reprobe_pmult(sata_hba_inst, sata_device, flag));
11195 
11196         /* Store sata_drive_info when a non-pmult device was attached. */
11197         osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11198         if (osdinfo != NULL) {
11199                 /*
11200                  * We are re-probing port with a previously attached device.
11201                  * Save previous device type and settings.
11202                  */
11203                 prev_device_type = cportinfo->cport_dev_type;
11204                 prev_device_settings = osdinfo->satadrv_settings;
11205                 prev_device_state = osdinfo->satadrv_state;
11206         }
11207         if (flag == SATA_DEV_IDENTIFY_RETRY) {
11208                 start_time = ddi_get_lbolt();
11209                 retry = B_TRUE;
11210         }
11211 retry_probe:
11212 
11213         /* probe port */
11214         mutex_enter(&cportinfo->cport_mutex);
11215         cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11216         cportinfo->cport_state |= SATA_STATE_PROBING;
11217         mutex_exit(&cportinfo->cport_mutex);
11218 
11219         rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11220             (SATA_DIP(sata_hba_inst), sata_device);
11221 
11222         mutex_enter(&cportinfo->cport_mutex);
11223         if (rval_probe != SATA_SUCCESS) {
11224                 cportinfo->cport_state = SATA_PSTATE_FAILED;
11225                 mutex_exit(&cportinfo->cport_mutex);
11226                 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
11227                     "SATA port %d probing failed",
11228                     cportinfo->cport_addr.cport));
11229                 return (SATA_FAILURE);
11230         }
11231 
11232         /*
11233          * update sata port state and set device type
11234          */
11235         sata_update_port_info(sata_hba_inst, sata_device);
11236         cportinfo->cport_state &= ~SATA_STATE_PROBING;
11237 
11238         /*
11239          * Sanity check - Port is active? Is the link active?
11240          * Is there any device attached?
11241          */
11242         if ((cportinfo->cport_state &
11243             (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11244             (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11245             SATA_PORT_DEVLINK_UP) {
11246                 /*
11247                  * Port in non-usable state or no link active/no device.
11248                  * Free info structure if necessary (direct attached drive
11249                  * only, for now!
11250                  */
11251                 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11252                 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11253                 /* Add here differentiation for device attached or not */
11254                 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11255                 mutex_exit(&cportinfo->cport_mutex);
11256                 if (sdinfo != NULL)
11257                         kmem_free(sdinfo, sizeof (sata_drive_info_t));
11258                 return (SATA_SUCCESS);
11259         }
11260 
11261         cportinfo->cport_state |= SATA_STATE_READY;
11262         cportinfo->cport_state |= SATA_STATE_PROBED;
11263 
11264         cportinfo->cport_dev_type = sata_device->satadev_type;
11265         sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11266 
11267         /*
11268          * If we are re-probing the port, there may be
11269          * sata_drive_info structure attached
11270          */
11271         if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11272 
11273                 /*
11274                  * There is no device, so remove device info structure,
11275                  * if necessary.
11276                  */
11277                 /* Device change: Drive -> None */
11278                 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11279                 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11280                 if (sdinfo != NULL) {
11281                         kmem_free(sdinfo, sizeof (sata_drive_info_t));
11282                         sata_log(sata_hba_inst, CE_WARN,
11283                             "SATA device detached "
11284                             "from port %d", cportinfo->cport_addr.cport);
11285                 }
11286                 mutex_exit(&cportinfo->cport_mutex);
11287                 return (SATA_SUCCESS);
11288 
11289         }
11290 
11291         if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11292 
11293                 /* Device (may) change: Drive -> Drive */
11294                 if (sdinfo == NULL) {
11295                         /*
11296                          * There is some device attached, but there is
11297                          * no sata_drive_info structure - allocate one
11298                          */
11299                         mutex_exit(&cportinfo->cport_mutex);
11300                         sdinfo = kmem_zalloc(
11301                             sizeof (sata_drive_info_t), KM_SLEEP);
11302                         mutex_enter(&cportinfo->cport_mutex);
11303                         /*
11304                          * Recheck, that the port state did not change when we
11305                          * released mutex.
11306                          */
11307                         if (cportinfo->cport_state & SATA_STATE_READY) {
11308                                 SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
11309                                 sdinfo->satadrv_addr = cportinfo->cport_addr;
11310                                 sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
11311                                 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11312                                 sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11313                         } else {
11314                                 /*
11315                                  * Port is not in ready state, we
11316                                  * cannot attach a device.
11317                                  */
11318                                 mutex_exit(&cportinfo->cport_mutex);
11319                                 kmem_free(sdinfo, sizeof (sata_drive_info_t));
11320                                 return (SATA_SUCCESS);
11321                         }
11322                         /*
11323                          * Since we are adding device, presumably new one,
11324                          * indicate that it  should be initalized,
11325                          * as well as some internal framework states).
11326                          */
11327                         init_device = B_TRUE;
11328                 }
11329                 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11330                 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11331         } else {
11332                 /* Device change: Drive -> PMult */
11333                 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11334                 if (sdinfo != NULL) {
11335                         kmem_free(sdinfo, sizeof (sata_drive_info_t));
11336                         sata_log(sata_hba_inst, CE_WARN,
11337                             "SATA device detached "
11338                             "from port %d", cportinfo->cport_addr.cport);
11339                 }
11340 
11341                 sata_log(sata_hba_inst, CE_WARN,
11342                     "SATA port multiplier detected at port %d",
11343                     cportinfo->cport_addr.cport);
11344 
11345                 mutex_exit(&cportinfo->cport_mutex);
11346                 if (sata_alloc_pmult(sata_hba_inst, sata_device) !=
11347                     SATA_SUCCESS)
11348                         return (SATA_FAILURE);
11349                 sata_show_pmult_info(sata_hba_inst, sata_device);
11350                 mutex_enter(&cportinfo->cport_mutex);
11351 
11352                 /*
11353                  * Mark all the port multiplier port behind the port
11354                  * multiplier behind with link events, so that the sata daemon
11355                  * will update their status.
11356                  */
11357                 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11358                 pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11359                 mutex_exit(&cportinfo->cport_mutex);
11360                 return (SATA_SUCCESS);
11361         }
11362         mutex_exit(&cportinfo->cport_mutex);
11363 
11364         /*
11365          * Figure out what kind of device we are really
11366          * dealing with. Failure of identifying device does not fail this
11367          * function.
11368          */
11369         rval_probe = sata_probe_device(sata_hba_inst, sata_device);
11370         rval_init = SATA_FAILURE;
11371         mutex_enter(&cportinfo->cport_mutex);
11372         if (rval_probe == SATA_SUCCESS) {
11373                 /*
11374                  * If we are dealing with the same type of a device as before,
11375                  * restore its settings flags.
11376                  */
11377                 if (osdinfo != NULL &&
11378                     sata_device->satadev_type == prev_device_type)
11379                         sdinfo->satadrv_settings = prev_device_settings;
11380 
11381                 mutex_exit(&cportinfo->cport_mutex);
11382                 rval_init = SATA_SUCCESS;
11383                 /* Set initial device features, if necessary */
11384                 if (init_device == B_TRUE) {
11385                         rval_init = sata_initialize_device(sata_hba_inst,
11386                             sdinfo);
11387                 }
11388                 if (rval_init == SATA_SUCCESS)
11389                         return (rval_init);
11390                 /* else we will retry if retry was asked for */
11391 
11392         } else {
11393                 /*
11394                  * If there was some device info before we probe the device,
11395                  * restore previous device setting, so we can retry from scratch
11396                  * later. Providing, of course, that device has not disapear
11397                  * during probing process.
11398                  */
11399                 if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11400                         if (osdinfo != NULL) {
11401                                 cportinfo->cport_dev_type = prev_device_type;
11402                                 sdinfo->satadrv_type = prev_device_type;
11403                                 sdinfo->satadrv_state = prev_device_state;
11404                         }
11405                 } else {
11406                         /* device is gone */
11407                         kmem_free(sdinfo, sizeof (sata_drive_info_t));
11408                         cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11409                         SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11410                         mutex_exit(&cportinfo->cport_mutex);
11411                         return (SATA_SUCCESS);
11412                 }
11413                 mutex_exit(&cportinfo->cport_mutex);
11414         }
11415 
11416         if (retry) {
11417                 clock_t cur_time = ddi_get_lbolt();
11418                 /*
11419                  * A device was not successfully identified or initialized.
11420                  * Track retry time for device identification.
11421                  */
11422                 if ((cur_time - start_time) <
11423                     drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11424                         /* sleep for a while */
11425                         delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11426                         goto retry_probe;
11427                 }
11428                 /* else no more retries */
11429                 mutex_enter(&cportinfo->cport_mutex);
11430                 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
11431                         if (rval_init == SATA_RETRY) {
11432                                 /*
11433                                  * Setting drive features have failed, but
11434                                  * because the drive is still accessible,
11435                                  * keep it and emit a warning message.
11436                                  */
11437                                 sata_log(sata_hba_inst, CE_WARN,
11438                                     "SATA device at port %d - desired "
11439                                     "drive features could not be set. "
11440                                     "Device may not operate as expected.",
11441                                     cportinfo->cport_addr.cport);
11442                         } else {
11443                                 SATA_CPORTINFO_DRV_INFO(cportinfo)->
11444                                     satadrv_state = SATA_DSTATE_FAILED;
11445                         }
11446                 }
11447                 mutex_exit(&cportinfo->cport_mutex);
11448         }
11449         return (SATA_SUCCESS);
11450 }
11451 
11452 /*
11453  * Reprobe a controller port that connected to a port multiplier.
11454  *
11455  * NOTE: No Mutex should be hold.
11456  */
11457 static int
11458 sata_reprobe_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11459     int flag)
11460 {
11461         _NOTE(ARGUNUSED(flag))
11462         sata_cport_info_t *cportinfo;
11463         sata_pmult_info_t *pmultinfo;
11464         uint8_t cport = sata_device->satadev_addr.cport;
11465         int rval_probe;
11466 
11467         cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11468         pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11469 
11470         /* probe port */
11471         mutex_enter(&cportinfo->cport_mutex);
11472         cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11473         cportinfo->cport_state |= SATA_STATE_PROBING;
11474         mutex_exit(&cportinfo->cport_mutex);
11475 
11476         rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11477             (SATA_DIP(sata_hba_inst), sata_device);
11478 
11479         mutex_enter(&cportinfo->cport_mutex);
11480         if (rval_probe != SATA_SUCCESS) {
11481                 cportinfo->cport_state = SATA_PSTATE_FAILED;
11482                 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmult: "
11483                     "SATA port %d probing failed", cport));
11484                 sata_log(sata_hba_inst, CE_WARN,
11485                     "SATA port multiplier detached at port %d", cport);
11486                 mutex_exit(&cportinfo->cport_mutex);
11487                 sata_free_pmult(sata_hba_inst, sata_device);
11488                 return (SATA_FAILURE);
11489         }
11490 
11491         /*
11492          * update sata port state and set device type
11493          */
11494         sata_update_port_info(sata_hba_inst, sata_device);
11495         cportinfo->cport_state &= ~SATA_STATE_PROBING;
11496         cportinfo->cport_state |= SATA_STATE_PROBED;
11497 
11498         /*
11499          * Sanity check - Port is active? Is the link active?
11500          * Is there any device attached?
11501          */
11502         if ((cportinfo->cport_state &
11503             (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11504             (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11505             SATA_PORT_DEVLINK_UP ||
11506             (sata_device->satadev_type == SATA_DTYPE_NONE)) {
11507                 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11508                 mutex_exit(&cportinfo->cport_mutex);
11509                 sata_free_pmult(sata_hba_inst, sata_device);
11510                 sata_log(sata_hba_inst, CE_WARN,
11511                     "SATA port multiplier detached at port %d", cport);
11512                 return (SATA_SUCCESS);
11513         }
11514 
11515         /*
11516          * Device changed: PMult -> Non-PMult
11517          *
11518          * This situation is uncommon, most possibly being caused by errors
11519          * after which the port multiplier is not correct initialized and
11520          * recognized. In that case the new device will be marked as unknown
11521          * and will not be automatically probed in this routine. Instead
11522          * system administrator could manually restart it via cfgadm(1M).
11523          */
11524         if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11525                 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11526                 mutex_exit(&cportinfo->cport_mutex);
11527                 sata_free_pmult(sata_hba_inst, sata_device);
11528                 sata_log(sata_hba_inst, CE_WARN,
11529                     "SATA port multiplier detached at port %d", cport);
11530                 return (SATA_FAILURE);
11531         }
11532 
11533         /*
11534          * Now we know it is a port multiplier. However, if this is not the
11535          * previously attached port multiplier - they may have different
11536          * pmport numbers - we need to re-allocate data structures for every
11537          * pmport and drive.
11538          *
11539          * Port multipliers of the same model have identical values in these
11540          * registers, so it is still necessary to update the information of
11541          * all drives attached to the previous port multiplier afterwards.
11542          */
11543         /* Device changed: PMult -> another PMult */
11544         mutex_exit(&cportinfo->cport_mutex);
11545         sata_free_pmult(sata_hba_inst, sata_device);
11546         if (sata_alloc_pmult(sata_hba_inst, sata_device) != SATA_SUCCESS)
11547                 return (SATA_FAILURE);
11548         mutex_enter(&cportinfo->cport_mutex);
11549 
11550         SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
11551             "SATA port multiplier [changed] at port %d", cport);
11552         sata_log(sata_hba_inst, CE_WARN,
11553             "SATA port multiplier detected at port %d", cport);
11554 
11555         /*
11556          * Mark all the port multiplier port behind the port
11557          * multiplier behind with link events, so that the sata daemon
11558          * will update their status.
11559          */
11560         pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11561         mutex_exit(&cportinfo->cport_mutex);
11562 
11563         return (SATA_SUCCESS);
11564 }
11565 
11566 /*
11567  * Re-probe a port multiplier port, check for a device and attach info
11568  * structures when necessary. Identify Device data is fetched, if possible.
11569  * Assumption: sata address is already validated as port multiplier port.
11570  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11571  * the presence of a device and its type.
11572  *
11573  * flag arg specifies that the function should try multiple times to identify
11574  * device type and to initialize it, or it should return immediately on failure.
11575  * SATA_DEV_IDENTIFY_RETRY - retry
11576  * SATA_DEV_IDENTIFY_NORETRY - no retry
11577  *
11578  * SATA_FAILURE is returned if one of the operations failed.
11579  *
11580  * This function cannot be called in interrupt context - it may sleep.
11581  *
11582  * NOTE: Should be only called by sata_probe_port() in case target port is a
11583  *       port multiplier port.
11584  * NOTE: No Mutex should be hold.
11585  */
11586 static int
11587 sata_reprobe_pmport(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11588     int flag)
11589 {
11590         sata_cport_info_t *cportinfo = NULL;
11591         sata_pmport_info_t *pmportinfo = NULL;
11592         sata_drive_info_t *sdinfo, *osdinfo;
11593         sata_device_t sdevice;
11594         boolean_t init_device = B_FALSE;
11595         int prev_device_type = SATA_DTYPE_NONE;
11596         int prev_device_settings = 0;
11597         int prev_device_state = 0;
11598         clock_t start_time;
11599         uint8_t cport = sata_device->satadev_addr.cport;
11600         uint8_t pmport = sata_device->satadev_addr.pmport;
11601         int rval;
11602 
11603         cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11604         pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11605         osdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11606 
11607         if (osdinfo != NULL) {
11608                 /*
11609                  * We are re-probing port with a previously attached device.
11610                  * Save previous device type and settings.
11611                  */
11612                 prev_device_type = pmportinfo->pmport_dev_type;
11613                 prev_device_settings = osdinfo->satadrv_settings;
11614                 prev_device_state = osdinfo->satadrv_state;
11615         }
11616 
11617         start_time = ddi_get_lbolt();
11618 
11619         /* check parent status */
11620         mutex_enter(&cportinfo->cport_mutex);
11621         if ((cportinfo->cport_state &
11622             (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11623             (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11624             SATA_PORT_DEVLINK_UP) {
11625                 mutex_exit(&cportinfo->cport_mutex);
11626                 return (SATA_FAILURE);
11627         }
11628         mutex_exit(&cportinfo->cport_mutex);
11629 
11630 retry_probe_pmport:
11631 
11632         /* probe port */
11633         mutex_enter(&pmportinfo->pmport_mutex);
11634         pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11635         pmportinfo->pmport_state |= SATA_STATE_PROBING;
11636         mutex_exit(&pmportinfo->pmport_mutex);
11637 
11638         rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11639             (SATA_DIP(sata_hba_inst), sata_device);
11640 
11641         /* might need retry because we cannot touch registers. */
11642         if (rval == SATA_FAILURE) {
11643                 mutex_enter(&pmportinfo->pmport_mutex);
11644                 pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11645                 mutex_exit(&pmportinfo->pmport_mutex);
11646                 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
11647                     "SATA port %d:%d probing failed",
11648                     cport, pmport));
11649                 return (SATA_FAILURE);
11650         } else if (rval == SATA_RETRY) {
11651                 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
11652                     "SATA port %d:%d probing failed, retrying...",
11653                     cport, pmport));
11654                 clock_t cur_time = ddi_get_lbolt();
11655                 /*
11656                  * A device was not successfully identified or initialized.
11657                  * Track retry time for device identification.
11658                  */
11659                 if ((cur_time - start_time) <
11660                     drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11661                         /* sleep for a while */
11662                         delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11663                         goto retry_probe_pmport;
11664                 } else {
11665                         mutex_enter(&pmportinfo->pmport_mutex);
11666                         if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
11667                                 SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
11668                                     satadrv_state = SATA_DSTATE_FAILED;
11669                         mutex_exit(&pmportinfo->pmport_mutex);
11670                         return (SATA_SUCCESS);
11671                 }
11672         }
11673 
11674         /*
11675          * Sanity check - Controller port is active? Is the link active?
11676          * Is it still a port multiplier?
11677          */
11678         if ((cportinfo->cport_state &
11679             (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11680             (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11681             SATA_PORT_DEVLINK_UP ||
11682             (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
11683                 /*
11684                  * Port in non-usable state or no link active/no
11685                  * device. Free info structure.
11686                  */
11687                 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11688 
11689                 sdevice.satadev_addr.cport = cport;
11690                 sdevice.satadev_addr.pmport = pmport;
11691                 sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
11692                 mutex_exit(&cportinfo->cport_mutex);
11693 
11694                 sata_free_pmult(sata_hba_inst, &sdevice);
11695                 return (SATA_FAILURE);
11696         }
11697 
11698         /* SATA_SUCCESS NOW */
11699         /*
11700          * update sata port state and set device type
11701          */
11702         mutex_enter(&pmportinfo->pmport_mutex);
11703         sata_update_pmport_info(sata_hba_inst, sata_device);
11704         pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
11705 
11706         /*
11707          * Sanity check - Port is active? Is the link active?
11708          * Is there any device attached?
11709          */
11710         if ((pmportinfo->pmport_state &
11711             (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11712             (pmportinfo->pmport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11713             SATA_PORT_DEVLINK_UP) {
11714                 /*
11715                  * Port in non-usable state or no link active/no device.
11716                  * Free info structure if necessary (direct attached drive
11717                  * only, for now!
11718                  */
11719                 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11720                 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11721                 /* Add here differentiation for device attached or not */
11722                 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11723                 mutex_exit(&pmportinfo->pmport_mutex);
11724                 if (sdinfo != NULL)
11725                         kmem_free(sdinfo, sizeof (sata_drive_info_t));
11726                 return (SATA_SUCCESS);
11727         }
11728 
11729         pmportinfo->pmport_state |= SATA_STATE_READY;
11730         pmportinfo->pmport_dev_type = sata_device->satadev_type;
11731         sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11732 
11733         /*
11734          * If we are re-probing the port, there may be
11735          * sata_drive_info structure attached
11736          * (or sata_pm_info, if PMult is supported).
11737          */
11738         if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11739                 /*
11740                  * There is no device, so remove device info structure,
11741                  * if necessary.
11742                  */
11743                 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11744                 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11745                 if (sdinfo != NULL) {
11746                         kmem_free(sdinfo, sizeof (sata_drive_info_t));
11747                         sata_log(sata_hba_inst, CE_WARN,
11748                             "SATA device detached from port %d:%d",
11749                             cport, pmport);
11750                 }
11751                 mutex_exit(&pmportinfo->pmport_mutex);
11752                 return (SATA_SUCCESS);
11753         }
11754 
11755         /* this should not be a pmult */
11756         ASSERT(sata_device->satadev_type != SATA_DTYPE_PMULT);
11757         if (sdinfo == NULL) {
11758                 /*
11759                  * There is some device attached, but there is
11760                  * no sata_drive_info structure - allocate one
11761                  */
11762                 mutex_exit(&pmportinfo->pmport_mutex);
11763                 sdinfo = kmem_zalloc(sizeof (sata_drive_info_t),
11764                     KM_SLEEP);
11765                 mutex_enter(&pmportinfo->pmport_mutex);
11766                 /*
11767                  * Recheck, that the port state did not change when we
11768                  * released mutex.
11769                  */
11770                 if (pmportinfo->pmport_state & SATA_STATE_READY) {
11771                         SATA_PMPORTINFO_DRV_INFO(pmportinfo) = sdinfo;
11772                         sdinfo->satadrv_addr = pmportinfo->pmport_addr;
11773                         sdinfo->satadrv_addr.qual = SATA_ADDR_DPMPORT;
11774                         sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11775                         sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11776                 } else {
11777                         /*
11778                          * Port is not in ready state, we
11779                          * cannot attach a device.
11780                          */
11781                         mutex_exit(&pmportinfo->pmport_mutex);
11782                         kmem_free(sdinfo, sizeof (sata_drive_info_t));
11783                         return (SATA_SUCCESS);
11784                 }
11785                 /*
11786                  * Since we are adding device, presumably new one,
11787                  * indicate that it  should be initalized,
11788                  * as well as some internal framework states).
11789                  */
11790                 init_device = B_TRUE;
11791         }
11792 
11793         pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
11794         sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11795 
11796         mutex_exit(&pmportinfo->pmport_mutex);
11797         /*
11798          * Figure out what kind of device we are really
11799          * dealing with.
11800          */
11801         rval = sata_probe_device(sata_hba_inst, sata_device);
11802 
11803         mutex_enter(&pmportinfo->pmport_mutex);
11804         if (rval == SATA_SUCCESS) {
11805                 /*
11806                  * If we are dealing with the same type of a device as before,
11807                  * restore its settings flags.
11808                  */
11809                 if (osdinfo != NULL &&
11810                     sata_device->satadev_type == prev_device_type)
11811                         sdinfo->satadrv_settings = prev_device_settings;
11812 
11813                 mutex_exit(&pmportinfo->pmport_mutex);
11814                 /* Set initial device features, if necessary */
11815                 if (init_device == B_TRUE) {
11816                         rval = sata_initialize_device(sata_hba_inst, sdinfo);
11817                 }
11818                 if (rval == SATA_SUCCESS)
11819                         return (rval);
11820         } else {
11821                 /*
11822                  * If there was some device info before we probe the device,
11823                  * restore previous device setting, so we can retry from scratch
11824                  * later. Providing, of course, that device has not disappeared
11825                  * during probing process.
11826                  */
11827                 if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11828                         if (osdinfo != NULL) {
11829                                 pmportinfo->pmport_dev_type = prev_device_type;
11830                                 sdinfo->satadrv_type = prev_device_type;
11831                                 sdinfo->satadrv_state = prev_device_state;
11832                         }
11833                 } else {
11834                         /* device is gone */
11835                         kmem_free(sdinfo, sizeof (sata_drive_info_t));
11836                         pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11837                         SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11838                         mutex_exit(&pmportinfo->pmport_mutex);
11839                         return (SATA_SUCCESS);
11840                 }
11841                 mutex_exit(&pmportinfo->pmport_mutex);
11842         }
11843 
11844         if (flag == SATA_DEV_IDENTIFY_RETRY) {
11845                 clock_t cur_time = ddi_get_lbolt();
11846                 /*
11847                  * A device was not successfully identified or initialized.
11848                  * Track retry time for device identification.
11849                  */
11850                 if ((cur_time - start_time) <
11851                     drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11852                         /* sleep for a while */
11853                         delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11854                         goto retry_probe_pmport;
11855                 } else {
11856                         mutex_enter(&pmportinfo->pmport_mutex);
11857                         if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
11858                                 SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
11859                                     satadrv_state = SATA_DSTATE_FAILED;
11860                         mutex_exit(&pmportinfo->pmport_mutex);
11861                 }
11862         }
11863         return (SATA_SUCCESS);
11864 }
11865 
11866 /*
11867  * Allocated related structure for a port multiplier and its device ports
11868  *
11869  * Port multiplier should be ready and probed, and related information like
11870  * the number of the device ports should be store in sata_device_t.
11871  *
11872  * NOTE: No Mutex should be hold.
11873  */
11874 static int
11875 sata_alloc_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
11876 {
11877         dev_info_t *dip = SATA_DIP(sata_hba_inst);
11878         sata_cport_info_t *cportinfo = NULL;
11879         sata_pmult_info_t *pmultinfo = NULL;
11880         sata_pmport_info_t *pmportinfo = NULL;
11881         sata_device_t sd;
11882         dev_t minor_number;
11883         char name[16];
11884         uint8_t cport = sata_device->satadev_addr.cport;
11885         int rval;
11886         int npmport;
11887 
11888         cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11889 
11890         /* This function might be called while a port-mult is hot-plugged. */
11891         mutex_enter(&cportinfo->cport_mutex);
11892 
11893         /* dev_type's not updated when get called from sata_reprobe_port() */
11894         if (SATA_CPORTINFO_PMULT_INFO(cportinfo) == NULL) {
11895                 /* Create a pmult_info structure */
11896                 SATA_CPORTINFO_PMULT_INFO(cportinfo) =
11897                     kmem_zalloc(sizeof (sata_pmult_info_t), KM_SLEEP);
11898         }
11899         pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
11900 
11901         pmultinfo->pmult_addr = sata_device->satadev_addr;
11902         pmultinfo->pmult_addr.qual = SATA_ADDR_PMULT;
11903         pmultinfo->pmult_state = SATA_STATE_PROBING;
11904 
11905         /*
11906          * Probe the port multiplier with qualifier SATA_ADDR_PMULT_SPEC,
11907          * The HBA driver should initialize and register the port multiplier,
11908          * sata_register_pmult() will fill following fields,
11909          *   + sata_pmult_info.pmult_gscr
11910          *   + sata_pmult_info.pmult_num_dev_ports
11911          */
11912         sd.satadev_addr = sata_device->satadev_addr;
11913         sd.satadev_addr.qual = SATA_ADDR_PMULT_SPEC;
11914         mutex_exit(&cportinfo->cport_mutex);
11915         rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11916             (SATA_DIP(sata_hba_inst), &sd);
11917         mutex_enter(&cportinfo->cport_mutex);
11918 
11919         if (rval != SATA_SUCCESS ||
11920             (sd.satadev_type != SATA_DTYPE_PMULT) ||
11921             !(sd.satadev_state & SATA_DSTATE_PMULT_INIT)) {
11922                 SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
11923                 kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
11924                 cportinfo->cport_state = SATA_PSTATE_FAILED;
11925                 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11926                 mutex_exit(&cportinfo->cport_mutex);
11927                 SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
11928                     "sata_alloc_pmult: failed to initialize pmult "
11929                     "at port %d.", cport)
11930                 return (SATA_FAILURE);
11931         }
11932 
11933         /* Initialize pmport_info structure */
11934         for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
11935             npmport++) {
11936 
11937                 /* if everything is allocated, skip */
11938                 if (SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) != NULL)
11939                         continue;
11940 
11941                 pmportinfo = kmem_zalloc(sizeof (sata_pmport_info_t), KM_SLEEP);
11942                 mutex_init(&pmportinfo->pmport_mutex, NULL, MUTEX_DRIVER, NULL);
11943                 mutex_exit(&cportinfo->cport_mutex);
11944 
11945                 mutex_enter(&pmportinfo->pmport_mutex);
11946                 pmportinfo->pmport_addr.cport = cport;
11947                 pmportinfo->pmport_addr.pmport = (uint8_t)npmport;
11948                 pmportinfo->pmport_addr.qual = SATA_ADDR_PMPORT;
11949                 pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11950                 mutex_exit(&pmportinfo->pmport_mutex);
11951 
11952                 mutex_enter(&cportinfo->cport_mutex);
11953                 SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) = pmportinfo;
11954 
11955                 /* Create an attachment point */
11956                 minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
11957                     cport, (uint8_t)npmport, SATA_ADDR_PMPORT);
11958                 (void) sprintf(name, "%d.%d", cport, npmport);
11959 
11960                 if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number,
11961                     DDI_NT_SATA_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
11962                         sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
11963                             "cannot create SATA attachment point for "
11964                             "port %d:%d", cport, npmport);
11965                 }
11966         }
11967 
11968         pmultinfo->pmult_state &= ~SATA_STATE_PROBING;
11969         pmultinfo->pmult_state |= (SATA_STATE_PROBED|SATA_STATE_READY);
11970         cportinfo->cport_dev_type = SATA_DTYPE_PMULT;
11971 
11972         mutex_exit(&cportinfo->cport_mutex);
11973         return (SATA_SUCCESS);
11974 }
11975 
11976 /*
11977  * Free data structures when a port multiplier is removed.
11978  *
11979  * NOTE: No Mutex should be hold.
11980  */
11981 static void
11982 sata_free_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
11983 {
11984         sata_cport_info_t *cportinfo;
11985         sata_pmult_info_t *pmultinfo;
11986         sata_pmport_info_t *pmportinfo;
11987         sata_device_t pmport_device;
11988         sata_drive_info_t *sdinfo;
11989         dev_info_t *tdip;
11990         char name[16];
11991         uint8_t cport = sata_device->satadev_addr.cport;
11992         int npmport;
11993 
11994         cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11995 
11996         /* This function might be called while port-mult is hot plugged. */
11997         mutex_enter(&cportinfo->cport_mutex);
11998 
11999         cportinfo->cport_dev_type = SATA_DTYPE_NONE;
12000         pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
12001         ASSERT(pmultinfo != NULL);
12002 
12003         /* Free pmport_info structure */
12004         for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
12005             npmport++) {
12006                 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
12007                 if (pmportinfo == NULL)
12008                         continue;
12009                 mutex_exit(&cportinfo->cport_mutex);
12010 
12011                 mutex_enter(&pmportinfo->pmport_mutex);
12012                 sdinfo = pmportinfo->pmport_sata_drive;
12013                 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
12014                 mutex_exit(&pmportinfo->pmport_mutex);
12015 
12016                 /* Remove attachment point. */
12017                 name[0] = '\0';
12018                 (void) sprintf(name, "%d.%d", cport, npmport);
12019                 ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
12020                 sata_log(sata_hba_inst, CE_NOTE,
12021                     "Remove attachment point of port %d:%d",
12022                     cport, npmport);
12023 
12024                 /*
12025                  * Rumove target node
12026                  */
12027                 bzero(&pmport_device, sizeof (sata_device_t));
12028                 pmport_device.satadev_rev = SATA_DEVICE_REV;
12029                 pmport_device.satadev_addr.cport = cport;
12030                 pmport_device.satadev_addr.pmport = (uint8_t)npmport;
12031                 pmport_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
12032 
12033                 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12034                     &(pmport_device.satadev_addr));
12035                 if (tdip != NULL && ndi_devi_offline(tdip,
12036                     NDI_DEVI_REMOVE) != NDI_SUCCESS) {
12037                         /*
12038                          * Problem :
12039                          * The target node remained attached.
12040                          * This happens when the device file was open
12041                          * or a node was waiting for resources.
12042                          * Cannot do anything about it.
12043                          */
12044                         SATA_LOG_D((sata_hba_inst, CE_WARN,
12045                             "sata_free_pmult: could not unconfigure device "
12046                             "before disconnecting the SATA port %d:%d",
12047                             cport, npmport));
12048 
12049                         /*
12050                          * Set DEVICE REMOVED state in the target
12051                          * node. It will prevent access to the device
12052                          * even when a new device is attached, until
12053                          * the old target node is released, removed and
12054                          * recreated for a new  device.
12055                          */
12056                         sata_set_device_removed(tdip);
12057 
12058                         /*
12059                          * Instruct event daemon to try the target
12060                          * node cleanup later.
12061                          */
12062                         sata_set_target_node_cleanup(
12063                             sata_hba_inst, &(pmport_device.satadev_addr));
12064 
12065                 }
12066                 mutex_enter(&cportinfo->cport_mutex);
12067 
12068                 /*
12069                  * Add here differentiation for device attached or not
12070                  */
12071                 if (sdinfo != NULL)  {
12072                         sata_log(sata_hba_inst, CE_WARN,
12073                             "SATA device detached from port %d:%d",
12074                             cport, npmport);
12075                         kmem_free(sdinfo, sizeof (sata_drive_info_t));
12076                 }
12077 
12078                 mutex_destroy(&pmportinfo->pmport_mutex);
12079                 kmem_free(pmportinfo, sizeof (sata_pmport_info_t));
12080         }
12081 
12082         kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
12083 
12084         cportinfo->cport_devp.cport_sata_pmult = NULL;
12085 
12086         sata_log(sata_hba_inst, CE_WARN,
12087             "SATA port multiplier detached at port %d", cport);
12088 
12089         mutex_exit(&cportinfo->cport_mutex);
12090 }
12091 
12092 /*
12093  * Initialize device
12094  * Specified device is initialized to a default state.
12095  *
12096  * Returns SATA_SUCCESS if all device features are set successfully,
12097  * SATA_RETRY if device is accessible but device features were not set
12098  * successfully, and SATA_FAILURE otherwise.
12099  */
12100 static int
12101 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
12102     sata_drive_info_t *sdinfo)
12103 {
12104         int rval;
12105 
12106         sata_save_drive_settings(sdinfo);
12107 
12108         sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
12109 
12110         sata_init_write_cache_mode(sdinfo);
12111 
12112         rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
12113 
12114         /* Determine current data transfer mode */
12115         if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
12116                 sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12117         } else if ((sdinfo->satadrv_id.ai_validinfo &
12118             SATA_VALIDINFO_88) != 0 &&
12119             (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
12120                 sdinfo->satadrv_settings |= SATA_DEV_DMA;
12121         } else if ((sdinfo->satadrv_id.ai_dworddma &
12122             SATA_MDMA_SEL_MASK) != 0) {
12123                 sdinfo->satadrv_settings |= SATA_DEV_DMA;
12124         } else
12125                 /* DMA supported, not no DMA transfer mode is selected !? */
12126                 sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12127 
12128         if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) &&
12129             (sdinfo->satadrv_id.ai_features86 & 0x20))
12130                 sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
12131         else
12132                 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
12133 
12134         return (rval);
12135 }
12136 
12137 
12138 /*
12139  * Initialize write cache mode.
12140  *
12141  * The default write cache setting for SATA HDD is provided by sata_write_cache
12142  * static variable. ATAPI CD/DVDs devices have write cache default is
12143  * determined by sata_atapicdvd_write_cache static variable.
12144  * ATAPI tape devices have write cache default is determined by
12145  * sata_atapitape_write_cache static variable.
12146  * ATAPI disk devices have write cache default is determined by
12147  * sata_atapidisk_write_cache static variable.
12148  * 1 - enable
12149  * 0 - disable
12150  * any other value - current drive setting
12151  *
12152  * Although there is not reason to disable write cache on CD/DVD devices,
12153  * tape devices and ATAPI disk devices, the default setting control is provided
12154  * for the maximun flexibility.
12155  *
12156  * In the future, it may be overridden by the
12157  * disk-write-cache-enable property setting, if it is defined.
12158  * Returns SATA_SUCCESS if all device features are set successfully,
12159  * SATA_FAILURE otherwise.
12160  */
12161 static void
12162 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
12163 {
12164         switch (sdinfo->satadrv_type) {
12165         case SATA_DTYPE_ATADISK:
12166                 if (sata_write_cache == 1)
12167                         sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12168                 else if (sata_write_cache == 0)
12169                         sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12170                 /*
12171                  * When sata_write_cache value is not 0 or 1,
12172                  * a current setting of the drive's write cache is used.
12173                  */
12174                 break;
12175         case SATA_DTYPE_ATAPICD:
12176                 if (sata_atapicdvd_write_cache == 1)
12177                         sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12178                 else if (sata_atapicdvd_write_cache == 0)
12179                         sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12180                 /*
12181                  * When sata_atapicdvd_write_cache value is not 0 or 1,
12182                  * a current setting of the drive's write cache is used.
12183                  */
12184                 break;
12185         case SATA_DTYPE_ATAPITAPE:
12186                 if (sata_atapitape_write_cache == 1)
12187                         sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12188                 else if (sata_atapitape_write_cache == 0)
12189                         sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12190                 /*
12191                  * When sata_atapitape_write_cache value is not 0 or 1,
12192                  * a current setting of the drive's write cache is used.
12193                  */
12194                 break;
12195         case SATA_DTYPE_ATAPIDISK:
12196                 if (sata_atapidisk_write_cache == 1)
12197                         sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12198                 else if (sata_atapidisk_write_cache == 0)
12199                         sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12200                 /*
12201                  * When sata_atapidisk_write_cache value is not 0 or 1,
12202                  * a current setting of the drive's write cache is used.
12203                  */
12204                 break;
12205         }
12206 }
12207 
12208 
12209 /*
12210  * Validate sata address.
12211  * Specified cport, pmport and qualifier has to match
12212  * passed sata_scsi configuration info.
12213  * The presence of an attached device is not verified.
12214  *
12215  * Returns 0 when address is valid, -1 otherwise.
12216  */
12217 static int
12218 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
12219         int pmport, int qual)
12220 {
12221         if (qual == SATA_ADDR_DCPORT && pmport != 0)
12222                 goto invalid_address;
12223         if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12224                 goto invalid_address;
12225         if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
12226             ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
12227             (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
12228             (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
12229                 goto invalid_address;
12230 
12231         return (0);
12232 
12233 invalid_address:
12234         return (-1);
12235 
12236 }
12237 
12238 /*
12239  * Validate scsi address
12240  * SCSI target address is translated into SATA cport/pmport and compared
12241  * with a controller port/device configuration. LUN has to be 0.
12242  * Returns 0 if a scsi target refers to an attached device,
12243  * returns 1 if address is valid but no valid device is attached,
12244  * returns 2 if address is valid but device type is unknown (not valid device),
12245  * returns -1 if bad address or device is of an unsupported type.
12246  * Upon return sata_device argument is set.
12247  *
12248  * Port multiplier is supported now.
12249  */
12250 static int
12251 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
12252         struct scsi_address *ap, sata_device_t *sata_device)
12253 {
12254         int cport, pmport, qual, rval;
12255 
12256         rval = -1;      /* Invalid address */
12257         if (ap->a_lun != 0)
12258                 goto out;
12259 
12260         qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
12261         cport = SCSI_TO_SATA_CPORT(ap->a_target);
12262         pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
12263 
12264         if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
12265                 goto out;
12266 
12267         if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
12268             0) {
12269 
12270                 sata_cport_info_t *cportinfo;
12271                 sata_pmult_info_t *pmultinfo;
12272                 sata_drive_info_t *sdinfo = NULL;
12273 
12274                 sata_device->satadev_addr.qual = qual;
12275                 sata_device->satadev_addr.cport = cport;
12276                 sata_device->satadev_addr.pmport = pmport;
12277                 sata_device->satadev_rev = SATA_DEVICE_REV_1;
12278 
12279                 rval = 1;       /* Valid sata address */
12280 
12281                 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12282                 if (qual == SATA_ADDR_DCPORT) {
12283                         if (cportinfo == NULL ||
12284                             cportinfo->cport_dev_type == SATA_DTYPE_NONE)
12285                                 goto out;
12286 
12287                         sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
12288                         if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN &&
12289                             sdinfo != NULL) {
12290                                 rval = 2;
12291                                 goto out;
12292                         }
12293 
12294                         if ((cportinfo->cport_dev_type &
12295                             SATA_VALID_DEV_TYPE) == 0) {
12296                                 rval = -1;
12297                                 goto out;
12298                         }
12299 
12300                 } else if (qual == SATA_ADDR_DPMPORT) {
12301                         pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
12302                         if (pmultinfo == NULL) {
12303                                 rval = -1;
12304                                 goto out;
12305                         }
12306                         if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
12307                             NULL ||
12308                             SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12309                             pmport) == SATA_DTYPE_NONE)
12310                                 goto out;
12311 
12312                         sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
12313                             pmport);
12314                         if (SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12315                             pmport) == SATA_DTYPE_UNKNOWN && sdinfo != NULL) {
12316                                 rval = 2;
12317                                 goto out;
12318                         }
12319 
12320                         if ((SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12321                             pmport) & SATA_VALID_DEV_TYPE) == 0) {
12322                                 rval = -1;
12323                                 goto out;
12324                         }
12325 
12326                 } else {
12327                         rval = -1;
12328                         goto out;
12329                 }
12330                 if ((sdinfo == NULL) ||
12331                     (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
12332                         goto out;
12333 
12334                 sata_device->satadev_type = sdinfo->satadrv_type;
12335 
12336                 return (0);
12337         }
12338 out:
12339         if (rval > 0) {
12340                 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
12341                     "sata_validate_scsi_address: no valid target %x lun %x",
12342                     ap->a_target, ap->a_lun);
12343         }
12344         return (rval);
12345 }
12346 
12347 /*
12348  * Find dip corresponding to passed device number
12349  *
12350  * Returns NULL if invalid device number is passed or device cannot be found,
12351  * Returns dip is device is found.
12352  */
12353 static dev_info_t *
12354 sata_devt_to_devinfo(dev_t dev)
12355 {
12356         dev_info_t *dip;
12357 #ifndef __lock_lint
12358         struct devnames *dnp;
12359         major_t major = getmajor(dev);
12360         int instance = SATA_MINOR2INSTANCE(getminor(dev));
12361 
12362         if (major >= devcnt)
12363                 return (NULL);
12364 
12365         dnp = &devnamesp[major];
12366         LOCK_DEV_OPS(&(dnp->dn_lock));
12367         dip = dnp->dn_head;
12368         while (dip && (ddi_get_instance(dip) != instance)) {
12369                 dip = ddi_get_next(dip);
12370         }
12371         UNLOCK_DEV_OPS(&(dnp->dn_lock));
12372 #endif
12373 
12374         return (dip);
12375 }
12376 
12377 
12378 /*
12379  * Probe device.
12380  * This function issues Identify Device command and initializes local
12381  * sata_drive_info structure if the device can be identified.
12382  * The device type is determined by examining Identify Device
12383  * command response.
12384  * If the sata_hba_inst has linked drive info structure for this
12385  * device address, the Identify Device data is stored into sata_drive_info
12386  * structure linked to the port info structure.
12387  *
12388  * sata_device has to refer to the valid sata port(s) for HBA described
12389  * by sata_hba_inst structure.
12390  *
12391  * Returns:
12392  *      SATA_SUCCESS if device type was successfully probed and port-linked
12393  *              drive info structure was updated;
12394  *      SATA_FAILURE if there is no device, or device was not probed
12395  *              successully;
12396  *      SATA_RETRY if device probe can be retried later.
12397  * If a device cannot be identified, sata_device's dev_state and dev_type
12398  * fields are set to unknown.
12399  * There are no retries in this function. Any retries should be managed by
12400  * the caller.
12401  */
12402 
12403 
12404 static int
12405 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
12406 {
12407         sata_pmport_info_t *pmportinfo;
12408         sata_drive_info_t *sdinfo;
12409         sata_drive_info_t new_sdinfo;   /* local drive info struct */
12410         int rval;
12411 
12412         ASSERT((SATA_CPORT_STATE(sata_hba_inst,
12413             sata_device->satadev_addr.cport) &
12414             (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
12415 
12416         sata_device->satadev_type = SATA_DTYPE_NONE;
12417 
12418         mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12419             sata_device->satadev_addr.cport)));
12420 
12421         if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) {
12422                 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
12423                     sata_device->satadev_addr.cport,
12424                     sata_device->satadev_addr.pmport);
12425                 ASSERT(pmportinfo != NULL);
12426         }
12427 
12428         /* Get pointer to port-linked sata device info structure */
12429         sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12430         if (sdinfo != NULL) {
12431                 sdinfo->satadrv_state &=
12432                     ~(SATA_STATE_PROBED | SATA_STATE_READY);
12433                 sdinfo->satadrv_state |= SATA_STATE_PROBING;
12434         } else {
12435                 /* No device to probe */
12436                 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12437                     sata_device->satadev_addr.cport)));
12438                 sata_device->satadev_type = SATA_DTYPE_NONE;
12439                 sata_device->satadev_state = SATA_STATE_UNKNOWN;
12440                 return (SATA_FAILURE);
12441         }
12442         /*
12443          * Need to issue both types of identify device command and
12444          * determine device type by examining retreived data/status.
12445          * First, ATA Identify Device.
12446          */
12447         bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12448         new_sdinfo.satadrv_addr = sata_device->satadev_addr;
12449         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12450             sata_device->satadev_addr.cport)));
12451         new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
12452         rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12453         if (rval == SATA_RETRY) {
12454                 /* We may try to check for ATAPI device */
12455                 if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
12456                         /*
12457                          * HBA supports ATAPI - try to issue Identify Packet
12458                          * Device command.
12459                          */
12460                         new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI;
12461                         rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12462                 }
12463         }
12464         if (rval == SATA_SUCCESS) {
12465                 /*
12466                  * Got something responding positively to ATA Identify Device
12467                  * or to Identify Packet Device cmd.
12468                  * Save last used device type.
12469                  */
12470                 sata_device->satadev_type = new_sdinfo.satadrv_type;
12471 
12472                 /* save device info, if possible */
12473                 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12474                     sata_device->satadev_addr.cport)));
12475                 sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12476                 if (sdinfo == NULL) {
12477                         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12478                             sata_device->satadev_addr.cport)));
12479                         return (SATA_FAILURE);
12480                 }
12481                 /*
12482                  * Copy drive info into the port-linked drive info structure.
12483                  */
12484                 *sdinfo = new_sdinfo;
12485                 sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
12486                 sdinfo->satadrv_state |= SATA_STATE_PROBED;
12487                 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
12488                         SATA_CPORT_DEV_TYPE(sata_hba_inst,
12489                             sata_device->satadev_addr.cport) =
12490                             sdinfo->satadrv_type;
12491                 else { /* SATA_ADDR_DPMPORT */
12492                         mutex_enter(&pmportinfo->pmport_mutex);
12493                         SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12494                             sata_device->satadev_addr.cport,
12495                             sata_device->satadev_addr.pmport) =
12496                             sdinfo->satadrv_type;
12497                         mutex_exit(&pmportinfo->pmport_mutex);
12498                 }
12499                 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12500                     sata_device->satadev_addr.cport)));
12501                 return (SATA_SUCCESS);
12502         }
12503 
12504         /*
12505          * It may be SATA_RETRY or SATA_FAILURE return.
12506          * Looks like we cannot determine the device type at this time.
12507          */
12508         mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12509             sata_device->satadev_addr.cport)));
12510         sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12511         if (sdinfo != NULL) {
12512                 sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
12513                 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12514                 sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
12515                 sdinfo->satadrv_state |= SATA_STATE_PROBED;
12516                 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
12517                         SATA_CPORT_DEV_TYPE(sata_hba_inst,
12518                             sata_device->satadev_addr.cport) =
12519                             SATA_DTYPE_UNKNOWN;
12520                 else {
12521                         /* SATA_ADDR_DPMPORT */
12522                         mutex_enter(&pmportinfo->pmport_mutex);
12523                         if ((SATA_PMULT_INFO(sata_hba_inst,
12524                             sata_device->satadev_addr.cport) != NULL) &&
12525                             (SATA_PMPORT_INFO(sata_hba_inst,
12526                             sata_device->satadev_addr.cport,
12527                             sata_device->satadev_addr.pmport) != NULL))
12528                                 SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12529                                     sata_device->satadev_addr.cport,
12530                                     sata_device->satadev_addr.pmport) =
12531                                     SATA_DTYPE_UNKNOWN;
12532                         mutex_exit(&pmportinfo->pmport_mutex);
12533                 }
12534         }
12535         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12536             sata_device->satadev_addr.cport)));
12537         return (rval);
12538 }
12539 
12540 
12541 /*
12542  * Get pointer to sata_drive_info structure.
12543  *
12544  * The sata_device has to contain address (cport, pmport and qualifier) for
12545  * specified sata_scsi structure.
12546  *
12547  * Returns NULL if device address is not valid for this HBA configuration.
12548  * Otherwise, returns a pointer to sata_drive_info structure.
12549  *
12550  * This function should be called with a port mutex held.
12551  */
12552 static sata_drive_info_t *
12553 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
12554     sata_device_t *sata_device)
12555 {
12556         uint8_t cport = sata_device->satadev_addr.cport;
12557         uint8_t pmport = sata_device->satadev_addr.pmport;
12558         uint8_t qual = sata_device->satadev_addr.qual;
12559 
12560         if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12561                 return (NULL);
12562 
12563         if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
12564             (SATA_STATE_PROBED | SATA_STATE_READY)))
12565                 /* Port not probed yet */
12566                 return (NULL);
12567 
12568         if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
12569                 return (NULL);
12570 
12571         if (qual == SATA_ADDR_DCPORT) {
12572                 /* Request for a device on a controller port */
12573                 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
12574                     SATA_DTYPE_PMULT)
12575                         /* Port multiplier attached */
12576                         return (NULL);
12577                 return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
12578         }
12579         if (qual == SATA_ADDR_DPMPORT) {
12580                 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
12581                     SATA_DTYPE_PMULT)
12582                         return (NULL);
12583 
12584                 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
12585                         return (NULL);
12586 
12587                 if (!(SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) &
12588                     (SATA_STATE_PROBED | SATA_STATE_READY)))
12589                         /* Port multiplier port not probed yet */
12590                         return (NULL);
12591 
12592                 return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
12593         }
12594 
12595         /* we should not get here */
12596         return (NULL);
12597 }
12598 
12599 
12600 /*
12601  * sata_identify_device.
12602  * Send Identify Device command to SATA HBA driver.
12603  * If command executes successfully, update sata_drive_info structure pointed
12604  * to by sdinfo argument, including Identify Device data.
12605  * If command fails, invalidate data in sata_drive_info.
12606  *
12607  * Cannot be called from interrupt level.
12608  *
12609  * Returns:
12610  * SATA_SUCCESS if the device was identified as a supported device,
12611  * SATA_RETRY if the device was not identified but could be retried,
12612  * SATA_FAILURE if the device was not identified and identify attempt
12613  *      should not be retried.
12614  */
12615 static int
12616 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
12617     sata_drive_info_t *sdinfo)
12618 {
12619         uint16_t cfg_word;
12620         int rval;
12621 
12622         /* fetch device identify data */
12623         if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
12624             sdinfo)) != SATA_SUCCESS)
12625                 goto fail_unknown;
12626 
12627         cfg_word = sdinfo->satadrv_id.ai_config;
12628 
12629         /* Set the correct device type */
12630         if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) {
12631                 sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
12632         } else if (cfg_word == SATA_CFA_TYPE) {
12633                 /* It's a Compact Flash media via CF-to-SATA HDD adapter */
12634                 sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
12635         } else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) {
12636                 switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) {
12637                 case SATA_ATAPI_CDROM_DEV:
12638                         sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
12639                         break;
12640                 case SATA_ATAPI_SQACC_DEV:
12641                         sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE;
12642                         break;
12643                 case SATA_ATAPI_DIRACC_DEV:
12644                         sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK;
12645                         break;
12646                 case SATA_ATAPI_PROC_DEV:
12647                         sdinfo->satadrv_type = SATA_DTYPE_ATAPIPROC;
12648                         break;
12649                 default:
12650                         sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12651                 }
12652         } else {
12653                         sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12654         }
12655 
12656         if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12657                 if (sdinfo->satadrv_capacity == 0) {
12658                         /* Non-LBA disk. Too bad... */
12659                         sata_log(sata_hba_inst, CE_WARN,
12660                             "SATA disk device at port %d does not support LBA",
12661                             sdinfo->satadrv_addr.cport);
12662                         rval = SATA_FAILURE;
12663                         goto fail_unknown;
12664                 }
12665         }
12666 #if 0
12667         /* Left for historical reason */
12668         /*
12669          * Some initial version of SATA spec indicated that at least
12670          * UDMA mode 4 has to be supported. It is not metioned in
12671          * SerialATA 2.6, so this restriction is removed.
12672          */
12673         /* Check for Ultra DMA modes 6 through 0 being supported */
12674         for (i = 6; i >= 0; --i) {
12675                 if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
12676                         break;
12677         }
12678 
12679         /*
12680          * At least UDMA 4 mode has to be supported. If mode 4 or
12681          * higher are not supported by the device, fail this
12682          * device.
12683          */
12684         if (i < 4) {
12685                 /* No required Ultra DMA mode supported */
12686                 sata_log(sata_hba_inst, CE_WARN,
12687                     "SATA disk device at port %d does not support UDMA "
12688                     "mode 4 or higher", sdinfo->satadrv_addr.cport);
12689                 SATA_LOG_D((sata_hba_inst, CE_WARN,
12690                     "mode 4 or higher required, %d supported", i));
12691                 rval = SATA_FAILURE;
12692                 goto fail_unknown;
12693         }
12694 #endif
12695 
12696         /*
12697          * For Disk devices, if it doesn't support UDMA mode, we would
12698          * like to return failure directly.
12699          */
12700         if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
12701             !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
12702             (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) {
12703                 sata_log(sata_hba_inst, CE_WARN,
12704                     "SATA disk device at port %d does not support UDMA",
12705                     sdinfo->satadrv_addr.cport);
12706                 rval = SATA_FAILURE;
12707                 goto fail_unknown;
12708         }
12709 
12710         return (SATA_SUCCESS);
12711 
12712 fail_unknown:
12713         /* Invalidate sata_drive_info ? */
12714         sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12715         sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
12716         return (rval);
12717 }
12718 
12719 /*
12720  * Log/display device information
12721  */
12722 static void
12723 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
12724     sata_drive_info_t *sdinfo)
12725 {
12726         int valid_version;
12727         char msg_buf[MAXPATHLEN];
12728         int i;
12729 
12730         /* Show HBA path */
12731         (void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
12732 
12733         cmn_err(CE_CONT, "?%s :\n", msg_buf);
12734 
12735         switch (sdinfo->satadrv_type) {
12736         case SATA_DTYPE_ATADISK:
12737                 (void) sprintf(msg_buf, "SATA disk device at");
12738                 break;
12739 
12740         case SATA_DTYPE_ATAPICD:
12741                 (void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at");
12742                 break;
12743 
12744         case SATA_DTYPE_ATAPITAPE:
12745                 (void) sprintf(msg_buf, "SATA tape (ATAPI) device at");
12746                 break;
12747 
12748         case SATA_DTYPE_ATAPIDISK:
12749                 (void) sprintf(msg_buf, "SATA disk (ATAPI) device at");
12750                 break;
12751 
12752         case SATA_DTYPE_ATAPIPROC:
12753                 (void) sprintf(msg_buf, "SATA processor (ATAPI) device at");
12754                 break;
12755 
12756         case SATA_DTYPE_UNKNOWN:
12757                 (void) sprintf(msg_buf,
12758                     "Unsupported SATA device type (cfg 0x%x) at ",
12759                     sdinfo->satadrv_id.ai_config);
12760                 break;
12761         }
12762 
12763         if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
12764                 cmn_err(CE_CONT, "?\t%s port %d\n",
12765                     msg_buf, sdinfo->satadrv_addr.cport);
12766         else
12767                 cmn_err(CE_CONT, "?\t%s port %d:%d\n",
12768                     msg_buf, sdinfo->satadrv_addr.cport,
12769                     sdinfo->satadrv_addr.pmport);
12770 
12771         bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
12772             sizeof (sdinfo->satadrv_id.ai_model));
12773         swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
12774         msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
12775         cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
12776 
12777         bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
12778             sizeof (sdinfo->satadrv_id.ai_fw));
12779         swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
12780         msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
12781         cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
12782 
12783         bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
12784             sizeof (sdinfo->satadrv_id.ai_drvser));
12785         swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
12786         msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
12787         if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12788                 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
12789         } else {
12790                 /*
12791                  * Some drives do not implement serial number and may
12792                  * violate the spec by providing spaces rather than zeros
12793                  * in serial number field. Scan the buffer to detect it.
12794                  */
12795                 for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
12796                         if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
12797                                 break;
12798                 }
12799                 if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
12800                         cmn_err(CE_CONT, "?\tserial number - none\n");
12801                 } else {
12802                         cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
12803                 }
12804         }
12805 
12806 #ifdef SATA_DEBUG
12807         if (sdinfo->satadrv_id.ai_majorversion != 0 &&
12808             sdinfo->satadrv_id.ai_majorversion != 0xffff) {
12809                 int i;
12810                 for (i = 14; i >= 2; i--) {
12811                         if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
12812                                 valid_version = i;
12813                                 break;
12814                         }
12815                 }
12816                 cmn_err(CE_CONT,
12817                     "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
12818                     valid_version,
12819                     sdinfo->satadrv_id.ai_majorversion,
12820                     sdinfo->satadrv_id.ai_minorversion);
12821         }
12822 #endif
12823         /* Log some info */
12824         cmn_err(CE_CONT, "?\tsupported features:\n");
12825         msg_buf[0] = '\0';
12826         if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12827                 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
12828                         (void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
12829                 else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
12830                         (void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
12831         }
12832         if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
12833                 (void) strlcat(msg_buf, "DMA", MAXPATHLEN);
12834         if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
12835                 (void) strlcat(msg_buf, ", Native Command Queueing",
12836                     MAXPATHLEN);
12837         if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
12838                 (void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
12839         if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
12840             (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
12841                 (void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
12842         if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
12843             (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
12844                 (void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
12845         cmn_err(CE_CONT, "?\t %s\n", msg_buf);
12846         if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA3)
12847                 cmn_err(CE_CONT, "?\tSATA Gen3 signaling speed (6.0Gbps)\n");
12848         else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
12849                 cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
12850         else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
12851                 cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
12852         if (sdinfo->satadrv_features_support &
12853             (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
12854                 msg_buf[0] = '\0';
12855                 (void) snprintf(msg_buf, MAXPATHLEN,
12856                     "Supported queue depth %d",
12857                     sdinfo->satadrv_queue_depth);
12858                 if (!(sata_func_enable &
12859                     (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
12860                         (void) strlcat(msg_buf,
12861                             " - queueing disabled globally", MAXPATHLEN);
12862                 else if (sdinfo->satadrv_queue_depth >
12863                     sdinfo->satadrv_max_queue_depth) {
12864                         (void) snprintf(&msg_buf[strlen(msg_buf)],
12865                             MAXPATHLEN - strlen(msg_buf), ", limited to %d",
12866                             (int)sdinfo->satadrv_max_queue_depth);
12867                 }
12868                 cmn_err(CE_CONT, "?\t%s\n", msg_buf);
12869         }
12870 
12871         if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12872 #ifdef __i386
12873                 (void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
12874                     sdinfo->satadrv_capacity);
12875 #else
12876                 (void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
12877                     sdinfo->satadrv_capacity);
12878 #endif
12879                 cmn_err(CE_CONT, "?%s", msg_buf);
12880         }
12881 }
12882 
12883 /*
12884  * Log/display port multiplier information
12885  * No Mutex should be hold.
12886  */
12887 static void
12888 sata_show_pmult_info(sata_hba_inst_t *sata_hba_inst,
12889     sata_device_t *sata_device)
12890 {
12891         _NOTE(ARGUNUSED(sata_hba_inst))
12892 
12893         int cport = sata_device->satadev_addr.cport;
12894         sata_pmult_info_t *pmultinfo;
12895         char msg_buf[MAXPATHLEN];
12896         uint32_t gscr0, gscr1, gscr2, gscr64;
12897 
12898         mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12899         pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
12900         if (pmultinfo == NULL) {
12901                 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12902                 return;
12903         }
12904 
12905         gscr0 = pmultinfo->pmult_gscr.gscr0;
12906         gscr1 = pmultinfo->pmult_gscr.gscr1;
12907         gscr2 = pmultinfo->pmult_gscr.gscr2;
12908         gscr64 = pmultinfo->pmult_gscr.gscr64;
12909         mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12910 
12911         cmn_err(CE_CONT, "?Port Multiplier %d device-ports found at port %d",
12912             sata_device->satadev_add_info, sata_device->satadev_addr.cport);
12913 
12914         (void) sprintf(msg_buf, "\tVendor_ID 0x%04x, Module_ID 0x%04x",
12915             gscr0 & 0xffff, (gscr0 >> 16) & 0xffff);
12916         cmn_err(CE_CONT, "?%s", msg_buf);
12917 
12918         (void) strcpy(msg_buf, "\tSupport SATA PMP Spec ");
12919         if (gscr1 & (1 << 3))
12920                 (void) strlcat(msg_buf, "1.2", MAXPATHLEN);
12921         else if (gscr1 & (1 << 2))
12922                 (void) strlcat(msg_buf, "1.1", MAXPATHLEN);
12923         else if (gscr1 & (1 << 1))
12924                 (void) strlcat(msg_buf, "1.0", MAXPATHLEN);
12925         else
12926                 (void) strlcat(msg_buf, "unknown", MAXPATHLEN);
12927         cmn_err(CE_CONT, "?%s", msg_buf);
12928 
12929         (void) strcpy(msg_buf, "\tSupport ");
12930         if (gscr64 & (1 << 3))
12931                 (void) strlcat(msg_buf, "Asy-Notif, ",
12932                     MAXPATHLEN);
12933         if (gscr64 & (1 << 2))
12934                 (void) strlcat(msg_buf, "Dyn-SSC, ", MAXPATHLEN);
12935         if (gscr64 & (1 << 1))
12936                 (void) strlcat(msg_buf, "Iss-PMREQ, ", MAXPATHLEN);
12937         if (gscr64 & (1 << 0))
12938                 (void) strlcat(msg_buf, "BIST", MAXPATHLEN);
12939         if ((gscr64 & 0xf) == 0)
12940                 (void) strlcat(msg_buf, "nothing", MAXPATHLEN);
12941         cmn_err(CE_CONT, "?%s", msg_buf);
12942 
12943         (void) sprintf(msg_buf, "\tNumber of exposed device fan-out ports: %d",
12944             gscr2 & SATA_PMULT_PORTNUM_MASK);
12945         cmn_err(CE_CONT, "?%s", msg_buf);
12946 }
12947 
12948 /*
12949  * sata_save_drive_settings extracts current setting of the device and stores
12950  * it for future reference, in case the device setup would need to be restored
12951  * after the device reset.
12952  *
12953  * For all devices read ahead and write cache settings are saved, if the
12954  * device supports these features at all.
12955  * For ATAPI devices the Removable Media Status Notification setting is saved.
12956  */
12957 static void
12958 sata_save_drive_settings(sata_drive_info_t *sdinfo)
12959 {
12960         if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) ||
12961             SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) {
12962 
12963                 /* Current setting of Read Ahead (and Read Cache) */
12964                 if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id))
12965                         sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
12966                 else
12967                         sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
12968 
12969                 /* Current setting of Write Cache */
12970                 if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id))
12971                         sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12972                 else
12973                         sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12974         }
12975 
12976         if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
12977                 if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id))
12978                         sdinfo->satadrv_settings |= SATA_DEV_RMSN;
12979                 else
12980                         sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
12981         }
12982 }
12983 
12984 
12985 /*
12986  * sata_check_capacity function determines a disk capacity
12987  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
12988  *
12989  * NOTE: CHS mode is not supported! If a device does not support LBA,
12990  * this function is not called.
12991  *
12992  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
12993  */
12994 static uint64_t
12995 sata_check_capacity(sata_drive_info_t *sdinfo)
12996 {
12997         uint64_t capacity = 0;
12998         int i;
12999 
13000         if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
13001             !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
13002                 /* Capacity valid only for LBA-addressable disk devices */
13003                 return (0);
13004 
13005         if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
13006             (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
13007             (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
13008                 /* LBA48 mode supported and enabled */
13009                 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
13010                     SATA_DEV_F_LBA28;
13011                 for (i = 3;  i >= 0;  --i) {
13012                         capacity <<= 16;
13013                         capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
13014                 }
13015         } else {
13016                 capacity = sdinfo->satadrv_id.ai_addrsec[1];
13017                 capacity <<= 16;
13018                 capacity += sdinfo->satadrv_id.ai_addrsec[0];
13019                 if (capacity >= 0x1000000)
13020                         /* LBA28 mode */
13021                         sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
13022         }
13023         return (capacity);
13024 }
13025 
13026 
13027 /*
13028  * Allocate consistent buffer for DMA transfer
13029  *
13030  * Cannot be called from interrupt level or with mutex held - it may sleep.
13031  *
13032  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
13033  */
13034 static struct buf *
13035 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
13036 {
13037         struct scsi_address ap;
13038         struct buf *bp;
13039         ddi_dma_attr_t  cur_dma_attr;
13040 
13041         ASSERT(spx->txlt_sata_pkt != NULL);
13042         ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
13043         ap.a_target = SATA_TO_SCSI_TARGET(
13044             spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
13045             spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
13046             spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
13047         ap.a_lun = 0;
13048 
13049         bp = scsi_alloc_consistent_buf(&ap, NULL, len,
13050             B_READ, SLEEP_FUNC, NULL);
13051 
13052         if (bp != NULL) {
13053                 /* Allocate DMA resources for this buffer */
13054                 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
13055                 /*
13056                  * We use a local version of the dma_attr, to account
13057                  * for a device addressing limitations.
13058                  * sata_adjust_dma_attr() will handle sdinfo == NULL which
13059                  * will cause dma attributes to be adjusted to a lowest
13060                  * acceptable level.
13061                  */
13062                 sata_adjust_dma_attr(NULL,
13063                     SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
13064 
13065                 if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
13066                     SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
13067                         scsi_free_consistent_buf(bp);
13068                         spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
13069                         bp = NULL;
13070                 }
13071         }
13072         return (bp);
13073 }
13074 
13075 /*
13076  * Release local buffer (consistent buffer for DMA transfer) allocated
13077  * via sata_alloc_local_buffer().
13078  */
13079 static void
13080 sata_free_local_buffer(sata_pkt_txlate_t *spx)
13081 {
13082         ASSERT(spx->txlt_sata_pkt != NULL);
13083         ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
13084 
13085         spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
13086         spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
13087 
13088         sata_common_free_dma_rsrcs(spx);
13089 
13090         /* Free buffer */
13091         scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
13092         spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
13093 }
13094 
13095 /*
13096  * Allocate sata_pkt
13097  * Pkt structure version and embedded strcutures version are initialized.
13098  * sata_pkt and sata_pkt_txlate structures are cross-linked.
13099  *
13100  * Since this may be called in interrupt context by sata_scsi_init_pkt,
13101  * callback argument determines if it can sleep or not.
13102  * Hence, it should not be called from interrupt context.
13103  *
13104  * If successful, non-NULL pointer to a sata pkt is returned.
13105  * Upon failure, NULL pointer is returned.
13106  */
13107 static sata_pkt_t *
13108 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
13109 {
13110         sata_pkt_t *spkt;
13111         int kmsflag;
13112 
13113         kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
13114         spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
13115         if (spkt == NULL) {
13116                 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13117                     "sata_pkt_alloc: failed"));
13118                 return (NULL);
13119         }
13120         spkt->satapkt_rev = SATA_PKT_REV;
13121         spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
13122         spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
13123         spkt->satapkt_framework_private = spx;
13124         spx->txlt_sata_pkt = spkt;
13125         return (spkt);
13126 }
13127 
13128 /*
13129  * Free sata pkt allocated via sata_pkt_alloc()
13130  */
13131 static void
13132 sata_pkt_free(sata_pkt_txlate_t *spx)
13133 {
13134         ASSERT(spx->txlt_sata_pkt != NULL);
13135         ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
13136         kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
13137         spx->txlt_sata_pkt = NULL;
13138 }
13139 
13140 
13141 /*
13142  * Adjust DMA attributes.
13143  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
13144  * from 8 bits to 16 bits, depending on a command being used.
13145  * Limiting max block count arbitrarily to 256 for all read/write
13146  * commands may affects performance, so check both the device and
13147  * controller capability before adjusting dma attributes.
13148  */
13149 void
13150 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
13151     ddi_dma_attr_t *adj_dma_attr)
13152 {
13153         uint32_t count_max;
13154 
13155         /* Copy original attributes */
13156         *adj_dma_attr = *dma_attr;
13157         /*
13158          * Things to consider: device addressing capability,
13159          * "excessive" controller DMA capabilities.
13160          * If a device is being probed/initialized, there are
13161          * no device info - use default limits then.
13162          */
13163         if (sdinfo == NULL) {
13164                 count_max = dma_attr->dma_attr_granular * 0x100;
13165                 if (dma_attr->dma_attr_count_max > count_max)
13166                         adj_dma_attr->dma_attr_count_max = count_max;
13167                 if (dma_attr->dma_attr_maxxfer > count_max)
13168                         adj_dma_attr->dma_attr_maxxfer = count_max;
13169                 return;
13170         }
13171 
13172         if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13173                 if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
13174                         /*
13175                          * 16-bit sector count may be used - we rely on
13176                          * the assumption that only read and write cmds
13177                          * will request more than 256 sectors worth of data
13178                          */
13179                         count_max = adj_dma_attr->dma_attr_granular * 0x10000;
13180                 } else {
13181                         /*
13182                          * 8-bit sector count will be used - default limits
13183                          * for dma attributes
13184                          */
13185                         count_max = adj_dma_attr->dma_attr_granular * 0x100;
13186                 }
13187                 /*
13188                  * Adjust controler dma attributes, if necessary
13189                  */
13190                 if (dma_attr->dma_attr_count_max > count_max)
13191                         adj_dma_attr->dma_attr_count_max = count_max;
13192                 if (dma_attr->dma_attr_maxxfer > count_max)
13193                         adj_dma_attr->dma_attr_maxxfer = count_max;
13194         }
13195 }
13196 
13197 
13198 /*
13199  * Allocate DMA resources for the buffer
13200  * This function handles initial DMA resource allocation as well as
13201  * DMA window shift and may be called repeatedly for the same DMA window
13202  * until all DMA cookies in the DMA window are processed.
13203  * To guarantee that there is always a coherent set of cookies to process
13204  * by SATA HBA driver (observing alignment, device granularity, etc.),
13205  * the number of slots for DMA cookies is equal to lesser of  a number of
13206  * cookies in a DMA window and a max number of scatter/gather entries.
13207  *
13208  * Returns DDI_SUCCESS upon successful operation.
13209  * Return failure code of a failing command or DDI_FAILURE when
13210  * internal cleanup failed.
13211  */
13212 static int
13213 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
13214     int (*callback)(caddr_t), caddr_t arg,
13215     ddi_dma_attr_t *cur_dma_attr)
13216 {
13217         int     rval;
13218         off_t   offset;
13219         size_t  size;
13220         int     max_sg_len, req_len, i;
13221         uint_t  dma_flags;
13222         struct buf      *bp;
13223         uint64_t        cur_txfer_len;
13224 
13225 
13226         ASSERT(spx->txlt_sata_pkt != NULL);
13227         bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
13228         ASSERT(bp != NULL);
13229 
13230 
13231         if (spx->txlt_buf_dma_handle == NULL) {
13232                 /*
13233                  * No DMA resources allocated so far - this is a first call
13234                  * for this sata pkt.
13235                  */
13236                 rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
13237                     cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
13238 
13239                 if (rval != DDI_SUCCESS) {
13240                         SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13241                             "sata_dma_buf_setup: no buf DMA resources %x",
13242                             rval));
13243                         return (rval);
13244                 }
13245 
13246                 if (bp->b_flags & B_READ)
13247                         dma_flags = DDI_DMA_READ;
13248                 else
13249                         dma_flags = DDI_DMA_WRITE;
13250 
13251                 if (flags & PKT_CONSISTENT)
13252                         dma_flags |= DDI_DMA_CONSISTENT;
13253 
13254                 if (flags & PKT_DMA_PARTIAL)
13255                         dma_flags |= DDI_DMA_PARTIAL;
13256 
13257                 /*
13258                  * Check buffer alignment and size against dma attributes
13259                  * Consider dma_attr_align only. There may be requests
13260                  * with the size lower than device granularity, but they
13261                  * will not read/write from/to the device, so no adjustment
13262                  * is necessary. The dma_attr_minxfer theoretically should
13263                  * be considered, but no HBA driver is checking it.
13264                  */
13265                 if (IS_P2ALIGNED(bp->b_un.b_addr,
13266                     cur_dma_attr->dma_attr_align)) {
13267                         rval = ddi_dma_buf_bind_handle(
13268                             spx->txlt_buf_dma_handle,
13269                             bp, dma_flags, callback, arg,
13270                             &spx->txlt_dma_cookie,
13271                             &spx->txlt_curwin_num_dma_cookies);
13272                 } else { /* Buffer is not aligned */
13273 
13274                         int     (*ddicallback)(caddr_t);
13275                         size_t  bufsz;
13276 
13277                         /* Check id sleeping is allowed */
13278                         ddicallback = (callback == NULL_FUNC) ?
13279                             DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
13280 
13281                         SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13282                             "mis-aligned buffer: addr=0x%p, cnt=%lu",
13283                             (void *)bp->b_un.b_addr, bp->b_bcount);
13284 
13285                         if (bp->b_flags & (B_PAGEIO|B_PHYS))
13286                                 /*
13287                                  * CPU will need to access data in the buffer
13288                                  * (for copying) so map it.
13289                                  */
13290                                 bp_mapin(bp);
13291 
13292                         ASSERT(spx->txlt_tmp_buf == NULL);
13293 
13294                         /* Buffer may be padded by ddi_dma_mem_alloc()! */
13295                         rval = ddi_dma_mem_alloc(
13296                             spx->txlt_buf_dma_handle,
13297                             bp->b_bcount,
13298                             &sata_acc_attr,
13299                             DDI_DMA_STREAMING,
13300                             ddicallback, NULL,
13301                             &spx->txlt_tmp_buf,
13302                             &bufsz,
13303                             &spx->txlt_tmp_buf_handle);
13304 
13305                         if (rval != DDI_SUCCESS) {
13306                                 /* DMA mapping failed */
13307                                 (void) ddi_dma_free_handle(
13308                                     &spx->txlt_buf_dma_handle);
13309                                 spx->txlt_buf_dma_handle = NULL;
13310 #ifdef SATA_DEBUG
13311                                 mbuffail_count++;
13312 #endif
13313                                 SATADBG1(SATA_DBG_DMA_SETUP,
13314                                     spx->txlt_sata_hba_inst,
13315                                     "sata_dma_buf_setup: "
13316                                     "buf dma mem alloc failed %x\n", rval);
13317                                 return (rval);
13318                         }
13319                         ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
13320                             cur_dma_attr->dma_attr_align));
13321 
13322 #ifdef SATA_DEBUG
13323                         mbuf_count++;
13324 
13325                         if (bp->b_bcount != bufsz)
13326                                 /*
13327                                  * This will require special handling, because
13328                                  * DMA cookies will be based on the temporary
13329                                  * buffer size, not the original buffer
13330                                  * b_bcount, so the residue may have to
13331                                  * be counted differently.
13332                                  */
13333                                 SATADBG2(SATA_DBG_DMA_SETUP,
13334                                     spx->txlt_sata_hba_inst,
13335                                     "sata_dma_buf_setup: bp size %x != "
13336                                     "bufsz %x\n", bp->b_bcount, bufsz);
13337 #endif
13338                         if (dma_flags & DDI_DMA_WRITE) {
13339                                 /*
13340                                  * Write operation - copy data into
13341                                  * an aligned temporary buffer. Buffer will be
13342                                  * synced for device by ddi_dma_addr_bind_handle
13343                                  */
13344                                 bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
13345                                     bp->b_bcount);
13346                         }
13347 
13348                         rval = ddi_dma_addr_bind_handle(
13349                             spx->txlt_buf_dma_handle,
13350                             NULL,
13351                             spx->txlt_tmp_buf,
13352                             bufsz, dma_flags, ddicallback, 0,
13353                             &spx->txlt_dma_cookie,
13354                             &spx->txlt_curwin_num_dma_cookies);
13355                 }
13356 
13357                 switch (rval) {
13358                 case DDI_DMA_PARTIAL_MAP:
13359                         SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13360                             "sata_dma_buf_setup: DMA Partial Map\n", NULL);
13361                         /*
13362                          * Partial DMA mapping.
13363                          * Retrieve number of DMA windows for this request.
13364                          */
13365                         if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
13366                             &spx->txlt_num_dma_win) != DDI_SUCCESS) {
13367                                 if (spx->txlt_tmp_buf != NULL) {
13368                                         ddi_dma_mem_free(
13369                                             &spx->txlt_tmp_buf_handle);
13370                                         spx->txlt_tmp_buf = NULL;
13371                                 }
13372                                 (void) ddi_dma_unbind_handle(
13373                                     spx->txlt_buf_dma_handle);
13374                                 (void) ddi_dma_free_handle(
13375                                     &spx->txlt_buf_dma_handle);
13376                                 spx->txlt_buf_dma_handle = NULL;
13377                                 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13378                                     "sata_dma_buf_setup: numwin failed\n"));
13379                                 return (DDI_FAILURE);
13380                         }
13381                         SATADBG2(SATA_DBG_DMA_SETUP,
13382                             spx->txlt_sata_hba_inst,
13383                             "sata_dma_buf_setup: windows: %d, cookies: %d\n",
13384                             spx->txlt_num_dma_win,
13385                             spx->txlt_curwin_num_dma_cookies);
13386                         spx->txlt_cur_dma_win = 0;
13387                         break;
13388 
13389                 case DDI_DMA_MAPPED:
13390                         /* DMA fully mapped */
13391                         spx->txlt_num_dma_win = 1;
13392                         spx->txlt_cur_dma_win = 0;
13393                         SATADBG1(SATA_DBG_DMA_SETUP,
13394                             spx->txlt_sata_hba_inst,
13395                             "sata_dma_buf_setup: windows: 1 "
13396                             "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
13397                         break;
13398 
13399                 default:
13400                         /* DMA mapping failed */
13401                         if (spx->txlt_tmp_buf != NULL) {
13402                                 ddi_dma_mem_free(
13403                                     &spx->txlt_tmp_buf_handle);
13404                                 spx->txlt_tmp_buf = NULL;
13405                         }
13406                         (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13407                         spx->txlt_buf_dma_handle = NULL;
13408                         SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13409                             "sata_dma_buf_setup: buf dma handle binding "
13410                             "failed %x\n", rval));
13411                         return (rval);
13412                 }
13413                 spx->txlt_curwin_processed_dma_cookies = 0;
13414                 spx->txlt_dma_cookie_list = NULL;
13415         } else {
13416                 /*
13417                  * DMA setup is reused. Check if we need to process more
13418                  * cookies in current window, or to get next window, if any.
13419                  */
13420 
13421                 ASSERT(spx->txlt_curwin_processed_dma_cookies <=
13422                     spx->txlt_curwin_num_dma_cookies);
13423 
13424                 if (spx->txlt_curwin_processed_dma_cookies ==
13425                     spx->txlt_curwin_num_dma_cookies) {
13426                         /*
13427                          * All cookies from current DMA window were processed.
13428                          * Get next DMA window.
13429                          */
13430                         spx->txlt_cur_dma_win++;
13431                         if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
13432                                 (void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
13433                                     spx->txlt_cur_dma_win, &offset, &size,
13434                                     &spx->txlt_dma_cookie,
13435                                     &spx->txlt_curwin_num_dma_cookies);
13436                                 spx->txlt_curwin_processed_dma_cookies = 0;
13437                         } else {
13438                                 /* No more windows! End of request! */
13439                                 /* What to do? - panic for now */
13440                                 ASSERT(spx->txlt_cur_dma_win >=
13441                                     spx->txlt_num_dma_win);
13442 
13443                                 spx->txlt_curwin_num_dma_cookies = 0;
13444                                 spx->txlt_curwin_processed_dma_cookies = 0;
13445                                 spx->txlt_sata_pkt->
13446                                     satapkt_cmd.satacmd_num_dma_cookies = 0;
13447                                 return (DDI_SUCCESS);
13448                         }
13449                 }
13450         }
13451         /* There better be at least one DMA cookie outstanding */
13452         ASSERT((spx->txlt_curwin_num_dma_cookies -
13453             spx->txlt_curwin_processed_dma_cookies) > 0);
13454 
13455         if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
13456                 /* The default cookie slot was used in previous run */
13457                 ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
13458                 spx->txlt_dma_cookie_list = NULL;
13459                 spx->txlt_dma_cookie_list_len = 0;
13460         }
13461         if (spx->txlt_curwin_processed_dma_cookies == 0) {
13462                 /*
13463                  * Processing a new DMA window - set-up dma cookies list.
13464                  * We may reuse previously allocated cookie array if it is
13465                  * possible.
13466                  */
13467                 if (spx->txlt_dma_cookie_list != NULL &&
13468                     spx->txlt_dma_cookie_list_len <
13469                     spx->txlt_curwin_num_dma_cookies) {
13470                         /*
13471                          * New DMA window contains more cookies than
13472                          * the previous one. We need larger cookie list - free
13473                          * the old one.
13474                          */
13475                         (void) kmem_free(spx->txlt_dma_cookie_list,
13476                             spx->txlt_dma_cookie_list_len *
13477                             sizeof (ddi_dma_cookie_t));
13478                         spx->txlt_dma_cookie_list = NULL;
13479                         spx->txlt_dma_cookie_list_len = 0;
13480                 }
13481                 if (spx->txlt_dma_cookie_list == NULL) {
13482                         /*
13483                          * Calculate lesser of number of cookies in this
13484                          * DMA window and number of s/g entries.
13485                          */
13486                         max_sg_len = cur_dma_attr->dma_attr_sgllen;
13487                         req_len = MIN(max_sg_len,
13488                             spx->txlt_curwin_num_dma_cookies);
13489 
13490                         /* Allocate new dma cookie array if necessary */
13491                         if (req_len == 1) {
13492                                 /* Only one cookie - no need for a list */
13493                                 spx->txlt_dma_cookie_list =
13494                                     &spx->txlt_dma_cookie;
13495                                 spx->txlt_dma_cookie_list_len = 1;
13496                         } else {
13497                                 /*
13498                                  * More than one cookie - try to allocate space.
13499                                  */
13500                                 spx->txlt_dma_cookie_list = kmem_zalloc(
13501                                     sizeof (ddi_dma_cookie_t) * req_len,
13502                                     callback == NULL_FUNC ? KM_NOSLEEP :
13503                                     KM_SLEEP);
13504                                 if (spx->txlt_dma_cookie_list == NULL) {
13505                                         SATADBG1(SATA_DBG_DMA_SETUP,
13506                                             spx->txlt_sata_hba_inst,
13507                                             "sata_dma_buf_setup: cookie list "
13508                                             "allocation failed\n", NULL);
13509                                         /*
13510                                          * We could not allocate space for
13511                                          * neccessary number of dma cookies in
13512                                          * this window, so we fail this request.
13513                                          * Next invocation would try again to
13514                                          * allocate space for cookie list.
13515                                          * Note:Packet residue was not modified.
13516                                          */
13517                                         return (DDI_DMA_NORESOURCES);
13518                                 } else {
13519                                         spx->txlt_dma_cookie_list_len = req_len;
13520                                 }
13521                         }
13522                 }
13523                 /*
13524                  * Fetch DMA cookies into cookie list in sata_pkt_txlate.
13525                  * First cookie was already fetched.
13526                  */
13527                 *(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
13528                 cur_txfer_len =
13529                     (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
13530                 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
13531                 spx->txlt_curwin_processed_dma_cookies++;
13532                 for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
13533                     (i < spx->txlt_curwin_num_dma_cookies); i++) {
13534                         ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
13535                             &spx->txlt_dma_cookie_list[i]);
13536                         cur_txfer_len +=
13537                             (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
13538                         spx->txlt_curwin_processed_dma_cookies++;
13539                         spx->txlt_sata_pkt->
13540                             satapkt_cmd.satacmd_num_dma_cookies += 1;
13541                 }
13542         } else {
13543                 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13544                     "sata_dma_buf_setup: sliding within DMA window, "
13545                     "cur cookie %d, total cookies %d\n",
13546                     spx->txlt_curwin_processed_dma_cookies,
13547                     spx->txlt_curwin_num_dma_cookies);
13548 
13549                 /*
13550                  * Not all cookies from the current dma window were used because
13551                  * of s/g limitation.
13552                  * There is no need to re-size the list - it was set at
13553                  * optimal size, or only default entry is used (s/g = 1).
13554                  */
13555                 if (spx->txlt_dma_cookie_list == NULL) {
13556                         spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
13557                         spx->txlt_dma_cookie_list_len = 1;
13558                 }
13559                 /*
13560                  * Since we are processing remaining cookies in a DMA window,
13561                  * there may be less of them than the number of entries in the
13562                  * current dma cookie list.
13563                  */
13564                 req_len = MIN(spx->txlt_dma_cookie_list_len,
13565                     (spx->txlt_curwin_num_dma_cookies -
13566                     spx->txlt_curwin_processed_dma_cookies));
13567 
13568                 /* Fetch the next batch of cookies */
13569                 for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
13570                         ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
13571                             &spx->txlt_dma_cookie_list[i]);
13572                         cur_txfer_len +=
13573                             (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
13574                         spx->txlt_sata_pkt->
13575                             satapkt_cmd.satacmd_num_dma_cookies++;
13576                         spx->txlt_curwin_processed_dma_cookies++;
13577                 }
13578         }
13579 
13580         ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
13581 
13582         /* Point sata_cmd to the cookie list */
13583         spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
13584             &spx->txlt_dma_cookie_list[0];
13585 
13586         /* Remember number of DMA cookies passed in sata packet */
13587         spx->txlt_num_dma_cookies =
13588             spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
13589 
13590         ASSERT(cur_txfer_len != 0);
13591         if (cur_txfer_len <= bp->b_bcount)
13592                 spx->txlt_total_residue -= cur_txfer_len;
13593         else {
13594                 /*
13595                  * Temporary DMA buffer has been padded by
13596                  * ddi_dma_mem_alloc()!
13597                  * This requires special handling, because DMA cookies are
13598                  * based on the temporary buffer size, not the b_bcount,
13599                  * and we have extra bytes to transfer - but the packet
13600                  * residue has to stay correct because we will copy only
13601                  * the requested number of bytes.
13602                  */
13603                 spx->txlt_total_residue -= bp->b_bcount;
13604         }
13605 
13606         return (DDI_SUCCESS);
13607 }
13608 
13609 /*
13610  * Common routine for releasing DMA resources
13611  */
13612 static void
13613 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx)
13614 {
13615         if (spx->txlt_buf_dma_handle != NULL) {
13616                 if (spx->txlt_tmp_buf != NULL)  {
13617                         /*
13618                          * Intermediate DMA buffer was allocated.
13619                          * Free allocated buffer and associated access handle.
13620                          */
13621                         ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
13622                         spx->txlt_tmp_buf = NULL;
13623                 }
13624                 /*
13625                  * Free DMA resources - cookies and handles
13626                  */
13627                 /* ASSERT(spx->txlt_dma_cookie_list != NULL); */
13628                 if (spx->txlt_dma_cookie_list != NULL) {
13629                         if (spx->txlt_dma_cookie_list !=
13630                             &spx->txlt_dma_cookie) {
13631                                 (void) kmem_free(spx->txlt_dma_cookie_list,
13632                                     spx->txlt_dma_cookie_list_len *
13633                                     sizeof (ddi_dma_cookie_t));
13634                                 spx->txlt_dma_cookie_list = NULL;
13635                         }
13636                 }
13637                 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
13638                 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13639                 spx->txlt_buf_dma_handle = NULL;
13640         }
13641 }
13642 
13643 /*
13644  * Free DMA resources
13645  * Used by the HBA driver to release DMA resources that it does not use.
13646  *
13647  * Returns Void
13648  */
13649 void
13650 sata_free_dma_resources(sata_pkt_t *sata_pkt)
13651 {
13652         sata_pkt_txlate_t *spx;
13653 
13654         if (sata_pkt == NULL)
13655                 return;
13656 
13657         spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
13658 
13659         sata_common_free_dma_rsrcs(spx);
13660 }
13661 
13662 /*
13663  * Fetch Device Identify data.
13664  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
13665  * command to a device and get the device identify data.
13666  * The device_info structure has to be set to device type (for selecting proper
13667  * device identify command).
13668  *
13669  * Returns:
13670  * SATA_SUCCESS if cmd succeeded
13671  * SATA_RETRY if cmd was rejected and could be retried,
13672  * SATA_FAILURE if cmd failed and should not be retried (port error)
13673  *
13674  * Cannot be called in an interrupt context.
13675  */
13676 
13677 static int
13678 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
13679     sata_drive_info_t *sdinfo)
13680 {
13681         struct buf *bp;
13682         sata_pkt_t *spkt;
13683         sata_cmd_t *scmd;
13684         sata_pkt_txlate_t *spx;
13685         int rval;
13686         dev_info_t *dip = SATA_DIP(sata_hba_inst);
13687 
13688         spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13689         spx->txlt_sata_hba_inst = sata_hba_inst;
13690         spx->txlt_scsi_pkt = NULL;           /* No scsi pkt involved */
13691         spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13692         if (spkt == NULL) {
13693                 kmem_free(spx, sizeof (sata_pkt_txlate_t));
13694                 return (SATA_RETRY); /* may retry later */
13695         }
13696         /* address is needed now */
13697         spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13698 
13699         /*
13700          * Allocate buffer for Identify Data return data
13701          */
13702         bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
13703         if (bp == NULL) {
13704                 sata_pkt_free(spx);
13705                 kmem_free(spx, sizeof (sata_pkt_txlate_t));
13706                 SATA_LOG_D((sata_hba_inst, CE_WARN,
13707                     "sata_fetch_device_identify_data: "
13708                     "cannot allocate buffer for ID"));
13709                 return (SATA_RETRY); /* may retry later */
13710         }
13711 
13712         /* Fill sata_pkt */
13713         sdinfo->satadrv_state = SATA_STATE_PROBING;
13714         spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13715         spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13716         /* Synchronous mode, no callback */
13717         spkt->satapkt_comp = NULL;
13718         /* Timeout 30s */
13719         spkt->satapkt_time = sata_default_pkt_time;
13720 
13721         scmd = &spkt->satapkt_cmd;
13722         scmd->satacmd_bp = bp;
13723         scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13724         scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13725 
13726         /* Build Identify Device cmd in the sata_pkt */
13727         scmd->satacmd_addr_type = 0;         /* N/A */
13728         scmd->satacmd_sec_count_lsb = 0;     /* N/A */
13729         scmd->satacmd_lba_low_lsb = 0;               /* N/A */
13730         scmd->satacmd_lba_mid_lsb = 0;               /* N/A */
13731         scmd->satacmd_lba_high_lsb = 0;              /* N/A */
13732         scmd->satacmd_features_reg = 0;              /* N/A */
13733         scmd->satacmd_device_reg = 0;                /* Always device 0 */
13734         if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
13735                 /* Identify Packet Device cmd */
13736                 scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
13737         } else {
13738                 /* Identify Device cmd - mandatory for all other devices */
13739                 scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
13740         }
13741 
13742         /* Send pkt to SATA HBA driver */
13743         rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
13744 
13745 #ifdef SATA_INJECT_FAULTS
13746         sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
13747 #endif
13748 
13749         if (rval == SATA_TRAN_ACCEPTED &&
13750             spkt->satapkt_reason == SATA_PKT_COMPLETED) {
13751                 if (spx->txlt_buf_dma_handle != NULL) {
13752                         rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13753                             DDI_DMA_SYNC_FORKERNEL);
13754                         ASSERT(rval == DDI_SUCCESS);
13755                         if (sata_check_for_dma_error(dip, spx)) {
13756                                 ddi_fm_service_impact(dip,
13757                                     DDI_SERVICE_UNAFFECTED);
13758                                 rval = SATA_RETRY;
13759                                 goto fail;
13760                         }
13761 
13762                 }
13763                 if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
13764                     SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
13765                         SATA_LOG_D((sata_hba_inst, CE_WARN,
13766                             "SATA disk device at port %d - "
13767                             "partial Identify Data",
13768                             sdinfo->satadrv_addr.cport));
13769                         rval = SATA_RETRY; /* may retry later */
13770                         goto fail;
13771                 }
13772                 /* Update sata_drive_info */
13773                 bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
13774                     sizeof (sata_id_t));
13775 
13776                 sdinfo->satadrv_features_support = 0;
13777                 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13778                         /*
13779                          * Retrieve capacity (disks only) and addressing mode
13780                          */
13781                         sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
13782                 } else {
13783                         /*
13784                          * For ATAPI devices one would have to issue
13785                          * Get Capacity cmd for media capacity. Not here.
13786                          */
13787                         sdinfo->satadrv_capacity = 0;
13788                         /*
13789                          * Check what cdb length is supported
13790                          */
13791                         if ((sdinfo->satadrv_id.ai_config &
13792                             SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
13793                                 sdinfo->satadrv_atapi_cdb_len = 16;
13794                         else
13795                                 sdinfo->satadrv_atapi_cdb_len = 12;
13796                 }
13797                 /* Setup supported features flags */
13798                 if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
13799                         sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
13800 
13801                 /* Check for SATA GEN and NCQ support */
13802                 if (sdinfo->satadrv_id.ai_satacap != 0 &&
13803                     sdinfo->satadrv_id.ai_satacap != 0xffff) {
13804                         /* SATA compliance */
13805                         if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
13806                                 sdinfo->satadrv_features_support |=
13807                                     SATA_DEV_F_NCQ;
13808                         if (sdinfo->satadrv_id.ai_satacap &
13809                             (SATA_1_SPEED | SATA_2_SPEED | SATA_3_SPEED)) {
13810                                 if (sdinfo->satadrv_id.ai_satacap &
13811                                     SATA_3_SPEED)
13812                                         sdinfo->satadrv_features_support |=
13813                                             SATA_DEV_F_SATA3;
13814                                 if (sdinfo->satadrv_id.ai_satacap &
13815                                     SATA_2_SPEED)
13816                                         sdinfo->satadrv_features_support |=
13817                                             SATA_DEV_F_SATA2;
13818                                 if (sdinfo->satadrv_id.ai_satacap &
13819                                     SATA_1_SPEED)
13820                                         sdinfo->satadrv_features_support |=
13821                                             SATA_DEV_F_SATA1;
13822                         } else {
13823                                 sdinfo->satadrv_features_support |=
13824                                     SATA_DEV_F_SATA1;
13825                         }
13826                 }
13827                 if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
13828                     (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
13829                         sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
13830 
13831                 sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
13832                 if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
13833                     (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
13834                         ++sdinfo->satadrv_queue_depth;
13835                         /* Adjust according to controller capabilities */
13836                         sdinfo->satadrv_max_queue_depth = MIN(
13837                             sdinfo->satadrv_queue_depth,
13838                             SATA_QDEPTH(sata_hba_inst));
13839                         /* Adjust according to global queue depth limit */
13840                         sdinfo->satadrv_max_queue_depth = MIN(
13841                             sdinfo->satadrv_max_queue_depth,
13842                             sata_current_max_qdepth);
13843                         if (sdinfo->satadrv_max_queue_depth == 0)
13844                                 sdinfo->satadrv_max_queue_depth = 1;
13845                 } else
13846                         sdinfo->satadrv_max_queue_depth = 1;
13847 
13848                 rval = SATA_SUCCESS;
13849         } else {
13850                 /*
13851                  * Woops, no Identify Data.
13852                  */
13853                 if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
13854                         rval = SATA_RETRY; /* may retry later */
13855                 } else if (rval == SATA_TRAN_ACCEPTED) {
13856                         if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
13857                             spkt->satapkt_reason == SATA_PKT_ABORTED ||
13858                             spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
13859                             spkt->satapkt_reason == SATA_PKT_RESET)
13860                                 rval = SATA_RETRY; /* may retry later */
13861                         else
13862                                 rval = SATA_FAILURE;
13863                 } else {
13864                         rval = SATA_FAILURE;
13865                 }
13866         }
13867 fail:
13868         /* Free allocated resources */
13869         sata_free_local_buffer(spx);
13870         sata_pkt_free(spx);
13871         kmem_free(spx, sizeof (sata_pkt_txlate_t));
13872 
13873         return (rval);
13874 }
13875 
13876 
13877 /*
13878  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
13879  * UDMA mode is checked first, followed by MWDMA mode.
13880  * set correctly, so this function is setting it to the highest supported level.
13881  * Older SATA spec required that the device supports at least DMA 4 mode and
13882  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
13883  * restriction has been removed.
13884  *
13885  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
13886  * Returns SATA_FAILURE if proper DMA mode could not be selected.
13887  *
13888  * NOTE: This function should be called only if DMA mode is supported.
13889  */
13890 static int
13891 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
13892 {
13893         sata_pkt_t *spkt;
13894         sata_cmd_t *scmd;
13895         sata_pkt_txlate_t *spx;
13896         int i, mode;
13897         uint8_t subcmd;
13898         int rval = SATA_SUCCESS;
13899 
13900         ASSERT(sdinfo != NULL);
13901         ASSERT(sata_hba_inst != NULL);
13902 
13903         if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
13904             (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
13905                 /* Find highest Ultra DMA mode supported */
13906                 for (mode = 6; mode >= 0; --mode) {
13907                         if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
13908                                 break;
13909                 }
13910 #if 0
13911                 /* Left for historical reasons */
13912                 /*
13913                  * Some initial version of SATA spec indicated that at least
13914                  * UDMA mode 4 has to be supported. It is not mentioned in
13915                  * SerialATA 2.6, so this restriction is removed.
13916                  */
13917                 if (mode < 4)
13918                         return (SATA_FAILURE);
13919 #endif
13920 
13921                 /*
13922                  * For disk, we're still going to set DMA mode whatever is
13923                  * selected by default
13924                  *
13925                  * We saw an old maxtor sata drive will select Ultra DMA and
13926                  * Multi-Word DMA simultaneouly by default, which is going
13927                  * to cause DMA command timed out, so we need to select DMA
13928                  * mode even when it's already done by default
13929                  */
13930                 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
13931 
13932                         /* Find UDMA mode currently selected */
13933                         for (i = 6; i >= 0; --i) {
13934                                 if (sdinfo->satadrv_id.ai_ultradma &
13935                                     (1 << (i + 8)))
13936                                         break;
13937                         }
13938                         if (i >= mode)
13939                                 /* Nothing to do */
13940                                 return (SATA_SUCCESS);
13941                 }
13942 
13943                 subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
13944 
13945         } else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
13946                 /* Find highest MultiWord DMA mode supported */
13947                 for (mode = 2; mode >= 0; --mode) {
13948                         if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
13949                                 break;
13950                 }
13951 
13952                 /*
13953                  * For disk, We're still going to set DMA mode whatever is
13954                  * selected by default
13955                  *
13956                  * We saw an old maxtor sata drive will select Ultra DMA and
13957                  * Multi-Word DMA simultaneouly by default, which is going
13958                  * to cause DMA command timed out, so we need to select DMA
13959                  * mode even when it's already done by default
13960                  */
13961                 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
13962 
13963                         /* Find highest MultiWord DMA mode selected */
13964                         for (i = 2; i >= 0; --i) {
13965                                 if (sdinfo->satadrv_id.ai_dworddma &
13966                                     (1 << (i + 8)))
13967                                         break;
13968                         }
13969                         if (i >= mode)
13970                                 /* Nothing to do */
13971                                 return (SATA_SUCCESS);
13972                 }
13973 
13974                 subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
13975         } else
13976                 return (SATA_SUCCESS);
13977 
13978         /*
13979          * Set DMA mode via SET FEATURES COMMAND.
13980          * Prepare packet for SET FEATURES COMMAND.
13981          */
13982         spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13983         spx->txlt_sata_hba_inst = sata_hba_inst;
13984         spx->txlt_scsi_pkt = NULL;   /* No scsi pkt involved */
13985         spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13986         if (spkt == NULL) {
13987                 SATA_LOG_D((sata_hba_inst, CE_WARN,
13988                     "sata_set_dma_mode: could not set DMA mode %d", mode));
13989                 rval = SATA_FAILURE;
13990                 goto done;
13991         }
13992         /* Fill sata_pkt */
13993         spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13994         /* Timeout 30s */
13995         spkt->satapkt_time = sata_default_pkt_time;
13996         /* Synchronous mode, no callback, interrupts */
13997         spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13998         spkt->satapkt_comp = NULL;
13999         scmd = &spkt->satapkt_cmd;
14000         scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14001         scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14002         scmd->satacmd_addr_type = 0;
14003         scmd->satacmd_device_reg = 0;
14004         scmd->satacmd_status_reg = 0;
14005         scmd->satacmd_error_reg = 0;
14006         scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14007         scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
14008         scmd->satacmd_sec_count_lsb = subcmd | mode;
14009 
14010         /* Transfer command to HBA */
14011         if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
14012             spkt) != SATA_TRAN_ACCEPTED ||
14013             spkt->satapkt_reason != SATA_PKT_COMPLETED) {
14014                 /* Pkt execution failed */
14015                 rval = SATA_FAILURE;
14016         }
14017 done:
14018 
14019         /* Free allocated resources */
14020         if (spkt != NULL)
14021                 sata_pkt_free(spx);
14022         (void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14023 
14024         return (rval);
14025 }
14026 
14027 
14028 /*
14029  * Set device caching mode.
14030  * One of the following operations should be specified:
14031  * SATAC_SF_ENABLE_READ_AHEAD
14032  * SATAC_SF_DISABLE_READ_AHEAD
14033  * SATAC_SF_ENABLE_WRITE_CACHE
14034  * SATAC_SF_DISABLE_WRITE_CACHE
14035  *
14036  * If operation fails, system log messgage is emitted.
14037  * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if
14038  * command was sent but did not succeed, and SATA_FAILURE otherwise.
14039  */
14040 
14041 static int
14042 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
14043     int cache_op)
14044 {
14045         sata_pkt_t *spkt;
14046         sata_cmd_t *scmd;
14047         sata_pkt_txlate_t *spx;
14048         int rval = SATA_SUCCESS;
14049         int hba_rval;
14050         char *infop;
14051 
14052         ASSERT(sdinfo != NULL);
14053         ASSERT(sata_hba_inst != NULL);
14054         ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
14055             cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
14056             cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
14057             cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
14058 
14059 
14060         /* Prepare packet for SET FEATURES COMMAND */
14061         spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14062         spx->txlt_sata_hba_inst = sata_hba_inst;
14063         spx->txlt_scsi_pkt = NULL;   /* No scsi pkt involved */
14064         spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14065         if (spkt == NULL) {
14066                 rval = SATA_FAILURE;
14067                 goto failure;
14068         }
14069         /* Fill sata_pkt */
14070         spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14071         /* Timeout 30s */
14072         spkt->satapkt_time = sata_default_pkt_time;
14073         /* Synchronous mode, no callback, interrupts */
14074         spkt->satapkt_op_mode =
14075             SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14076         spkt->satapkt_comp = NULL;
14077         scmd = &spkt->satapkt_cmd;
14078         scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14079         scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14080         scmd->satacmd_addr_type = 0;
14081         scmd->satacmd_device_reg = 0;
14082         scmd->satacmd_status_reg = 0;
14083         scmd->satacmd_error_reg = 0;
14084         scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14085         scmd->satacmd_features_reg = cache_op;
14086 
14087         /* Transfer command to HBA */
14088         hba_rval = (*SATA_START_FUNC(sata_hba_inst))(
14089             SATA_DIP(sata_hba_inst), spkt);
14090 
14091 #ifdef SATA_INJECT_FAULTS
14092         sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
14093 #endif
14094 
14095         if ((hba_rval != SATA_TRAN_ACCEPTED) ||
14096             (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14097                 /* Pkt execution failed */
14098                 switch (cache_op) {
14099                 case SATAC_SF_ENABLE_READ_AHEAD:
14100                         infop = "enabling read ahead failed";
14101                         break;
14102                 case SATAC_SF_DISABLE_READ_AHEAD:
14103                         infop = "disabling read ahead failed";
14104                         break;
14105                 case SATAC_SF_ENABLE_WRITE_CACHE:
14106                         infop = "enabling write cache failed";
14107                         break;
14108                 case SATAC_SF_DISABLE_WRITE_CACHE:
14109                         infop = "disabling write cache failed";
14110                         break;
14111                 }
14112                 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14113                 rval = SATA_RETRY;
14114         }
14115 failure:
14116         /* Free allocated resources */
14117         if (spkt != NULL)
14118                 sata_pkt_free(spx);
14119         (void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14120         return (rval);
14121 }
14122 
14123 /*
14124  * Set Removable Media Status Notification (enable/disable)
14125  * state == 0 , disable
14126  * state != 0 , enable
14127  *
14128  * If operation fails, system log messgage is emitted.
14129  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
14130  */
14131 
14132 static int
14133 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
14134     int state)
14135 {
14136         sata_pkt_t *spkt;
14137         sata_cmd_t *scmd;
14138         sata_pkt_txlate_t *spx;
14139         int rval = SATA_SUCCESS;
14140         char *infop;
14141 
14142         ASSERT(sdinfo != NULL);
14143         ASSERT(sata_hba_inst != NULL);
14144 
14145         /* Prepare packet for SET FEATURES COMMAND */
14146         spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14147         spx->txlt_sata_hba_inst = sata_hba_inst;
14148         spx->txlt_scsi_pkt = NULL;   /* No scsi pkt involved */
14149         spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14150         if (spkt == NULL) {
14151                 rval = SATA_FAILURE;
14152                 goto failure;
14153         }
14154         /* Fill sata_pkt */
14155         spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14156         /* Timeout 30s */
14157         spkt->satapkt_time = sata_default_pkt_time;
14158         /* Synchronous mode, no callback, interrupts */
14159         spkt->satapkt_op_mode =
14160             SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14161         spkt->satapkt_comp = NULL;
14162         scmd = &spkt->satapkt_cmd;
14163         scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14164         scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14165         scmd->satacmd_addr_type = 0;
14166         scmd->satacmd_device_reg = 0;
14167         scmd->satacmd_status_reg = 0;
14168         scmd->satacmd_error_reg = 0;
14169         scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14170         if (state == 0)
14171                 scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
14172         else
14173                 scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
14174 
14175         /* Transfer command to HBA */
14176         if (((*SATA_START_FUNC(sata_hba_inst))(
14177             SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
14178             (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14179                 /* Pkt execution failed */
14180                 if (state == 0)
14181                         infop = "disabling Removable Media Status "
14182                             "Notification failed";
14183                 else
14184                         infop = "enabling Removable Media Status "
14185                             "Notification failed";
14186 
14187                 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14188                 rval = SATA_FAILURE;
14189         }
14190 failure:
14191         /* Free allocated resources */
14192         if (spkt != NULL)
14193                 sata_pkt_free(spx);
14194         (void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14195         return (rval);
14196 }
14197 
14198 
14199 /*
14200  * Update state and copy port ss* values from passed sata_device structure.
14201  * sata_address is validated - if not valid, nothing is changed in sata_scsi
14202  * configuration struct.
14203  *
14204  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
14205  * regardless of the state in device argument.
14206  *
14207  * Port mutex should be held while calling this function.
14208  */
14209 static void
14210 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
14211     sata_device_t *sata_device)
14212 {
14213         sata_cport_info_t *cportinfo;
14214 
14215         if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
14216             sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
14217                 if (SATA_NUM_CPORTS(sata_hba_inst) <=
14218                     sata_device->satadev_addr.cport)
14219                         return;
14220 
14221                 cportinfo = SATA_CPORT_INFO(sata_hba_inst,
14222                     sata_device->satadev_addr.cport);
14223 
14224                 ASSERT(mutex_owned(&cportinfo->cport_mutex));
14225                 cportinfo->cport_scr = sata_device->satadev_scr;
14226 
14227                 /* Preserve SATA_PSTATE_SHUTDOWN flag */
14228                 cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
14229                     SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14230                 cportinfo->cport_state |=
14231                     sata_device->satadev_state & SATA_PSTATE_VALID;
14232         }
14233 }
14234 
14235 void
14236 sata_update_pmport_info(sata_hba_inst_t *sata_hba_inst,
14237     sata_device_t *sata_device)
14238 {
14239         sata_pmport_info_t *pmportinfo;
14240 
14241         if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT &&
14242             sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
14243             SATA_NUM_PMPORTS(sata_hba_inst,
14244             sata_device->satadev_addr.cport) <
14245             sata_device->satadev_addr.pmport) {
14246                 SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
14247                     "sata_update_port_info: error address %p.",
14248                     &sata_device->satadev_addr);
14249                 return;
14250         }
14251 
14252         pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
14253             sata_device->satadev_addr.cport,
14254             sata_device->satadev_addr.pmport);
14255 
14256         ASSERT(mutex_owned(&pmportinfo->pmport_mutex));
14257         pmportinfo->pmport_scr = sata_device->satadev_scr;
14258 
14259         /* Preserve SATA_PSTATE_SHUTDOWN flag */
14260         pmportinfo->pmport_state &=
14261             ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14262         pmportinfo->pmport_state |=
14263             sata_device->satadev_state & SATA_PSTATE_VALID;
14264 }
14265 
14266 /*
14267  * Extract SATA port specification from an IOCTL argument.
14268  *
14269  * This function return the port the user land send us as is, unless it
14270  * cannot retrieve port spec, then -1 is returned.
14271  *
14272  * Support port multiplier.
14273  */
14274 static int32_t
14275 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
14276 {
14277         int32_t port;
14278 
14279         /* Extract port number from nvpair in dca structure  */
14280         if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
14281                 SATA_LOG_D((sata_hba_inst, CE_NOTE,
14282                     "sata_get_port_num: invalid port spec 0x%x in ioctl",
14283                     port));
14284                 port = -1;
14285         }
14286 
14287         return (port);
14288 }
14289 
14290 /*
14291  * Get dev_info_t pointer to the device node pointed to by port argument.
14292  * NOTE: target argument is a value used in ioctls to identify
14293  * the AP - it is not a sata_address.
14294  * It is a combination of cport, pmport and address qualifier, encodded same
14295  * way as a scsi target number.
14296  * At this moment it carries only cport number.
14297  *
14298  * PMult hotplug is supported now.
14299  *
14300  * Returns dev_info_t pointer if target device was found, NULL otherwise.
14301  */
14302 
14303 static dev_info_t *
14304 sata_get_target_dip(dev_info_t *dip, uint8_t cport, uint8_t pmport)
14305 {
14306         dev_info_t      *cdip = NULL;
14307         int             target, tgt;
14308         int             circ;
14309         uint8_t         qual;
14310 
14311         sata_hba_inst_t *sata_hba_inst;
14312         scsi_hba_tran_t *scsi_hba_tran;
14313 
14314         /* Get target id */
14315         scsi_hba_tran = ddi_get_driver_private(dip);
14316         if (scsi_hba_tran == NULL)
14317                 return (NULL);
14318 
14319         sata_hba_inst = scsi_hba_tran->tran_hba_private;
14320 
14321         if (sata_hba_inst == NULL)
14322                 return (NULL);
14323 
14324         /* Identify a port-mult by cport_info.cport_dev_type */
14325         if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT)
14326                 qual = SATA_ADDR_DPMPORT;
14327         else
14328                 qual = SATA_ADDR_DCPORT;
14329 
14330         target = SATA_TO_SCSI_TARGET(cport, pmport, qual);
14331 
14332         /* Retrieve target dip */
14333         ndi_devi_enter(dip, &circ);
14334         for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14335                 dev_info_t *next = ddi_get_next_sibling(cdip);
14336 
14337                 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14338                     DDI_PROP_DONTPASS, "target", -1);
14339                 if (tgt == -1) {
14340                         /*
14341                          * This is actually an error condition, but not
14342                          * a fatal one. Just continue the search.
14343                          */
14344                         cdip = next;
14345                         continue;
14346                 }
14347 
14348                 if (tgt == target)
14349                         break;
14350 
14351                 cdip = next;
14352         }
14353         ndi_devi_exit(dip, circ);
14354 
14355         return (cdip);
14356 }
14357 
14358 /*
14359  * Get dev_info_t pointer to the device node pointed to by port argument.
14360  * NOTE: target argument is a value used in ioctls to identify
14361  * the AP - it is not a sata_address.
14362  * It is a combination of cport, pmport and address qualifier, encoded same
14363  * way as a scsi target number.
14364  *
14365  * Returns dev_info_t pointer if target device was found, NULL otherwise.
14366  */
14367 
14368 static dev_info_t *
14369 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
14370 {
14371         dev_info_t      *cdip = NULL;
14372         int             target, tgt;
14373         int             circ;
14374 
14375         target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
14376 
14377         ndi_devi_enter(dip, &circ);
14378         for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14379                 dev_info_t *next = ddi_get_next_sibling(cdip);
14380 
14381                 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14382                     DDI_PROP_DONTPASS, "target", -1);
14383                 if (tgt == -1) {
14384                         /*
14385                          * This is actually an error condition, but not
14386                          * a fatal one. Just continue the search.
14387                          */
14388                         cdip = next;
14389                         continue;
14390                 }
14391 
14392                 if (tgt == target)
14393                         break;
14394 
14395                 cdip = next;
14396         }
14397         ndi_devi_exit(dip, circ);
14398 
14399         return (cdip);
14400 }
14401 
14402 /*
14403  * Process sata port disconnect request.
14404  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
14405  * before this request. Nevertheless, if a device is still configured,
14406  * we need to attempt to offline and unconfigure device.
14407  * Regardless of the unconfigure operation results the port is marked as
14408  * deactivated and no access to the attached device is possible.
14409  * If the target node remains because unconfigure operation failed, its state
14410  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
14411  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
14412  * the device and remove old target node.
14413  *
14414  * This function invokes sata_hba_inst->satahba_tran->
14415  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14416  * If successful, the device structure (if any) attached to the specified port
14417  * is removed and state of the port marked appropriately.
14418  * Failure of the port_deactivate may keep port in the physically active state,
14419  * or may fail the port.
14420  *
14421  * NOTE: Port multiplier is supported.
14422  */
14423 
14424 static int
14425 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
14426     sata_device_t *sata_device)
14427 {
14428         sata_drive_info_t *sdinfo = NULL, *subsdinfo = NULL;
14429         sata_cport_info_t *cportinfo = NULL;
14430         sata_pmport_info_t *pmportinfo = NULL;
14431         sata_pmult_info_t *pmultinfo = NULL;
14432         sata_device_t subsdevice;
14433         int cport, pmport, qual;
14434         int rval = SATA_SUCCESS;
14435         int npmport = 0;
14436         int rv = 0;
14437 
14438         cport = sata_device->satadev_addr.cport;
14439         pmport = sata_device->satadev_addr.pmport;
14440         qual = sata_device->satadev_addr.qual;
14441 
14442         ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14443         if (qual == SATA_ADDR_DCPORT)
14444                 qual = SATA_ADDR_CPORT;
14445         else
14446                 qual = SATA_ADDR_PMPORT;
14447 
14448         /*
14449          * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
14450          * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14451          * Do the sanity check.
14452          */
14453         if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
14454                 /* No physical port deactivation supported. */
14455                 return (EINVAL);
14456         }
14457 
14458         /* Check the current state of the port */
14459         rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14460             (SATA_DIP(sata_hba_inst), sata_device);
14461 
14462         cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14463 
14464         /*
14465          * Processing port mulitiplier
14466          */
14467         if (qual == SATA_ADDR_CPORT &&
14468             SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
14469                 mutex_enter(&cportinfo->cport_mutex);
14470 
14471                 /* Check controller port status */
14472                 sata_update_port_info(sata_hba_inst, sata_device);
14473                 if (rval != SATA_SUCCESS ||
14474                     (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14475                         /*
14476                          * Device port status is unknown or it is in failed
14477                          * state
14478                          */
14479                         SATA_CPORT_STATE(sata_hba_inst, cport) =
14480                             SATA_PSTATE_FAILED;
14481                         SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14482                             "sata_hba_ioctl: connect: failed to deactivate "
14483                             "SATA port %d", cport);
14484                         mutex_exit(&cportinfo->cport_mutex);
14485                         return (EIO);
14486                 }
14487 
14488                 /* Disconnect all sub-devices. */
14489                 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
14490                 if (pmultinfo != NULL) {
14491 
14492                         for (npmport = 0; npmport < SATA_NUM_PMPORTS(
14493                             sata_hba_inst, cport); npmport ++) {
14494                                 subsdinfo = SATA_PMPORT_DRV_INFO(
14495                                     sata_hba_inst, cport, npmport);
14496                                 if (subsdinfo == NULL)
14497                                         continue;
14498 
14499                                 subsdevice.satadev_addr = subsdinfo->
14500                                     satadrv_addr;
14501 
14502                                 mutex_exit(&cportinfo->cport_mutex);
14503                                 if (sata_ioctl_disconnect(sata_hba_inst,
14504                                     &subsdevice) == SATA_SUCCESS) {
14505                                         SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
14506                                         "[Remove] device at port %d:%d "
14507                                         "successfully.", cport, npmport);
14508                                 }
14509                                 mutex_enter(&cportinfo->cport_mutex);
14510                         }
14511                 }
14512 
14513                 /* Disconnect the port multiplier */
14514                 cportinfo->cport_state &= ~SATA_STATE_READY;
14515                 mutex_exit(&cportinfo->cport_mutex);
14516 
14517                 sata_device->satadev_addr.qual = qual;
14518                 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14519                     (SATA_DIP(sata_hba_inst), sata_device);
14520 
14521                 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14522                     SE_NO_HINT);
14523 
14524                 mutex_enter(&cportinfo->cport_mutex);
14525                 sata_update_port_info(sata_hba_inst, sata_device);
14526                 if (rval != SATA_SUCCESS &&
14527                     sata_device->satadev_state & SATA_PSTATE_FAILED) {
14528                         cportinfo->cport_state = SATA_PSTATE_FAILED;
14529                         rv = EIO;
14530                 } else {
14531                         cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14532                 }
14533                 mutex_exit(&cportinfo->cport_mutex);
14534 
14535                 return (rv);
14536         }
14537 
14538         /*
14539          * Process non-port-multiplier device - it could be a drive connected
14540          * to a port multiplier port or a controller port.
14541          */
14542         if (qual == SATA_ADDR_PMPORT) {
14543                 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14544                 mutex_enter(&pmportinfo->pmport_mutex);
14545                 sata_update_pmport_info(sata_hba_inst, sata_device);
14546                 if (rval != SATA_SUCCESS ||
14547                     (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14548                         SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
14549                             SATA_PSTATE_FAILED;
14550                         SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
14551                             "sata_hba_ioctl: connect: failed to deactivate "
14552                             "SATA port %d:%d", cport, pmport);
14553                         mutex_exit(&pmportinfo->pmport_mutex);
14554                         return (EIO);
14555                 }
14556 
14557                 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
14558                         sdinfo = pmportinfo->pmport_sata_drive;
14559                         ASSERT(sdinfo != NULL);
14560                 }
14561 
14562                 /*
14563                  * Set port's dev_state to not ready - this will disable
14564                  * an access to a potentially attached device.
14565                  */
14566                 pmportinfo->pmport_state &= ~SATA_STATE_READY;
14567 
14568                 /* Remove and release sata_drive info structure. */
14569                 if (sdinfo != NULL) {
14570                         if ((sdinfo->satadrv_type &
14571                             SATA_VALID_DEV_TYPE) != 0) {
14572                                 /*
14573                                  * If a target node exists, try to offline
14574                                  * a device and remove target node.
14575                                  */
14576                                 mutex_exit(&pmportinfo->pmport_mutex);
14577                                 (void) sata_offline_device(sata_hba_inst,
14578                                     sata_device, sdinfo);
14579                                 mutex_enter(&pmportinfo->pmport_mutex);
14580                         }
14581 
14582                         SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
14583                         pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
14584                         (void) kmem_free((void *)sdinfo,
14585                             sizeof (sata_drive_info_t));
14586                 }
14587                 mutex_exit(&pmportinfo->pmport_mutex);
14588 
14589         } else if (qual == SATA_ADDR_CPORT) {
14590                 mutex_enter(&cportinfo->cport_mutex);
14591                 sata_update_port_info(sata_hba_inst, sata_device);
14592                 if (rval != SATA_SUCCESS ||
14593                     (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14594                         /*
14595                          * Device port status is unknown or it is in failed
14596                          * state
14597                          */
14598                         SATA_CPORT_STATE(sata_hba_inst, cport) =
14599                             SATA_PSTATE_FAILED;
14600                         SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14601                             "sata_hba_ioctl: connect: failed to deactivate "
14602                             "SATA port %d", cport);
14603                         mutex_exit(&cportinfo->cport_mutex);
14604                         return (EIO);
14605                 }
14606 
14607                 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
14608                         pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
14609                         ASSERT(pmultinfo != NULL);
14610                 } else if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14611                         sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14612                         ASSERT(sdinfo != NULL);
14613                 }
14614                 cportinfo->cport_state &= ~SATA_STATE_READY;
14615 
14616                 if (sdinfo != NULL) {
14617                         if ((sdinfo->satadrv_type &
14618                             SATA_VALID_DEV_TYPE) != 0) {
14619                                 /*
14620                                  * If a target node exists, try to offline
14621                                  * a device and remove target node.
14622                                  */
14623                                 mutex_exit(&cportinfo->cport_mutex);
14624                                 (void) sata_offline_device(sata_hba_inst,
14625                                     sata_device, sdinfo);
14626                                 mutex_enter(&cportinfo->cport_mutex);
14627                         }
14628 
14629                         SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14630                         cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14631                         (void) kmem_free((void *)sdinfo,
14632                             sizeof (sata_drive_info_t));
14633                 }
14634                 mutex_exit(&cportinfo->cport_mutex);
14635         }
14636 
14637         /* Just ask HBA driver to deactivate port */
14638         sata_device->satadev_addr.qual = qual;
14639 
14640         rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14641             (SATA_DIP(sata_hba_inst), sata_device);
14642 
14643         /*
14644          * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14645          * without the hint (to force listener to investivate the state).
14646          */
14647         sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14648             SE_NO_HINT);
14649 
14650         if (qual == SATA_ADDR_PMPORT) {
14651                 mutex_enter(&pmportinfo->pmport_mutex);
14652                 sata_update_pmport_info(sata_hba_inst, sata_device);
14653 
14654                 if (rval != SATA_SUCCESS &&
14655                     sata_device->satadev_state & SATA_PSTATE_FAILED) {
14656                         /*
14657                          * Port deactivation failure - do not change port
14658                          * state unless the state returned by HBA indicates a
14659                          * port failure.
14660                          *
14661                          * NOTE: device structures were released, so devices
14662                          * now are invisible! Port reset is needed to
14663                          * re-enumerate devices.
14664                          */
14665                         pmportinfo->pmport_state = SATA_PSTATE_FAILED;
14666                         rv = EIO;
14667                 } else {
14668                         /*
14669                          * Deactivation succeded. From now on the sata framework
14670                          * will not care what is happening to the device, until
14671                          * the port is activated again.
14672                          */
14673                         pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
14674                 }
14675                 mutex_exit(&pmportinfo->pmport_mutex);
14676         } else if (qual == SATA_ADDR_CPORT) {
14677                 mutex_enter(&cportinfo->cport_mutex);
14678                 sata_update_port_info(sata_hba_inst, sata_device);
14679 
14680                 if (rval != SATA_SUCCESS &&
14681                     sata_device->satadev_state & SATA_PSTATE_FAILED) {
14682                         cportinfo->cport_state = SATA_PSTATE_FAILED;
14683                         rv = EIO;
14684                 } else {
14685                         cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14686                 }
14687                 mutex_exit(&cportinfo->cport_mutex);
14688         }
14689 
14690         return (rv);
14691 }
14692 
14693 
14694 
14695 /*
14696  * Process sata port connect request
14697  * The sata cfgadm pluging will invoke this operation only if port was found
14698  * in the disconnect state (failed state is also treated as the disconnected
14699  * state).
14700  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
14701  * sata_tran_hotplug_ops->sata_tran_port_activate().
14702  * If successful and a device is found attached to the port,
14703  * the initialization sequence is executed to attach a device structure to
14704  * a port structure. The state of the port and a device would be set
14705  * appropriately.
14706  * The device is not set in configured state (system-wise) by this operation.
14707  *
14708  * Note, that activating the port may generate link events,
14709  * so it is important that following processing and the
14710  * event processing does not interfere with each other!
14711  *
14712  * This operation may remove port failed state and will
14713  * try to make port active and in good standing.
14714  *
14715  * NOTE: Port multiplier is supported.
14716  */
14717 
14718 static int
14719 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
14720     sata_device_t *sata_device)
14721 {
14722         sata_pmport_info_t      *pmportinfo = NULL;
14723         uint8_t cport, pmport, qual;
14724         int rv = 0;
14725 
14726         cport = sata_device->satadev_addr.cport;
14727         pmport = sata_device->satadev_addr.pmport;
14728         qual = sata_device->satadev_addr.qual;
14729 
14730         ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14731         if (qual == SATA_ADDR_DCPORT)
14732                 qual = SATA_ADDR_CPORT;
14733         else
14734                 qual = SATA_ADDR_PMPORT;
14735 
14736         if (qual == SATA_ADDR_PMPORT)
14737                 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14738 
14739         /*
14740          * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
14741          * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
14742          * Perform sanity check now.
14743          */
14744         if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
14745                 /* No physical port activation supported. */
14746                 return (EINVAL);
14747         }
14748 
14749         /* Just ask HBA driver to activate port */
14750         if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
14751             (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14752                 /*
14753                  * Port activation failure.
14754                  */
14755                 if (qual == SATA_ADDR_CPORT) {
14756                         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14757                             cport)->cport_mutex);
14758                         sata_update_port_info(sata_hba_inst, sata_device);
14759                         if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14760                                 SATA_CPORT_STATE(sata_hba_inst, cport) =
14761                                     SATA_PSTATE_FAILED;
14762                                 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14763                                     "sata_hba_ioctl: connect: failed to "
14764                                     "activate SATA port %d", cport);
14765                         }
14766                         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14767                             cport)->cport_mutex);
14768                 } else { /* port multiplier device port */
14769                         mutex_enter(&pmportinfo->pmport_mutex);
14770                         sata_update_pmport_info(sata_hba_inst, sata_device);
14771                         if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14772                                 SATA_PMPORT_STATE(sata_hba_inst, cport,
14773                                     pmport) = SATA_PSTATE_FAILED;
14774                                 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
14775                                     "sata_hba_ioctl: connect: failed to "
14776                                     "activate SATA port %d:%d", cport, pmport);
14777                         }
14778                         mutex_exit(&pmportinfo->pmport_mutex);
14779                 }
14780                 return (EIO);
14781         }
14782 
14783         /* Virgin port state - will be updated by the port re-probe. */
14784         if (qual == SATA_ADDR_CPORT) {
14785                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14786                     cport)->cport_mutex);
14787                 SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
14788                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14789                     cport)->cport_mutex);
14790         } else { /* port multiplier device port */
14791                 mutex_enter(&pmportinfo->pmport_mutex);
14792                 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
14793                 mutex_exit(&pmportinfo->pmport_mutex);
14794         }
14795 
14796         /*
14797          * Probe the port to find its state and attached device.
14798          */
14799         if (sata_reprobe_port(sata_hba_inst, sata_device,
14800             SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
14801                 rv = EIO;
14802 
14803         /*
14804          * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14805          * without the hint
14806          */
14807         sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14808             SE_NO_HINT);
14809 
14810         /*
14811          * If there is a device attached to the port, emit
14812          * a message.
14813          */
14814         if (sata_device->satadev_type != SATA_DTYPE_NONE) {
14815 
14816                 if (qual == SATA_ADDR_CPORT) {
14817                         if (sata_device->satadev_type == SATA_DTYPE_PMULT) {
14818                                 sata_log(sata_hba_inst, CE_WARN,
14819                                     "SATA port multiplier detected "
14820                                     "at port %d", cport);
14821                         } else {
14822                                 sata_log(sata_hba_inst, CE_WARN,
14823                                     "SATA device detected at port %d", cport);
14824                                 if (sata_device->satadev_type ==
14825                                     SATA_DTYPE_UNKNOWN) {
14826                                 /*
14827                                  * A device was not successfully identified
14828                                  */
14829                                 sata_log(sata_hba_inst, CE_WARN,
14830                                     "Could not identify SATA "
14831                                     "device at port %d", cport);
14832                                 }
14833                         }
14834                 } else { /* port multiplier device port */
14835                         sata_log(sata_hba_inst, CE_WARN,
14836                             "SATA device detected at port %d:%d",
14837                             cport, pmport);
14838                         if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
14839                                 /*
14840                                  * A device was not successfully identified
14841                                  */
14842                                 sata_log(sata_hba_inst, CE_WARN,
14843                                     "Could not identify SATA "
14844                                     "device at port %d:%d", cport, pmport);
14845                         }
14846                 }
14847         }
14848 
14849         return (rv);
14850 }
14851 
14852 
14853 /*
14854  * Process sata device unconfigure request.
14855  * The unconfigure operation uses generic nexus operation to
14856  * offline a device. It leaves a target device node attached.
14857  * and obviously sata_drive_info attached as well, because
14858  * from the hardware point of view nothing has changed.
14859  */
14860 static int
14861 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
14862     sata_device_t *sata_device)
14863 {
14864         int rv = 0;
14865         dev_info_t *tdip;
14866 
14867         /* We are addressing attached device, not a port */
14868         if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
14869                 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
14870         else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
14871                 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
14872 
14873         if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
14874             &sata_device->satadev_addr)) != NULL) {
14875 
14876                 if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
14877                         SATA_LOG_D((sata_hba_inst, CE_WARN,
14878                             "sata_hba_ioctl: unconfigure: "
14879                             "failed to unconfigure device at SATA port %d:%d",
14880                             sata_device->satadev_addr.cport,
14881                             sata_device->satadev_addr.pmport));
14882                         rv = EIO;
14883                 }
14884                 /*
14885                  * The target node devi_state should be marked with
14886                  * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
14887                  * This would be the indication for cfgadm that
14888                  * the AP node occupant state is 'unconfigured'.
14889                  */
14890 
14891         } else {
14892                 /*
14893                  * This would indicate a failure on the part of cfgadm
14894                  * to detect correct state of the node prior to this
14895                  * call - one cannot unconfigure non-existing device.
14896                  */
14897                 SATA_LOG_D((sata_hba_inst, CE_WARN,
14898                     "sata_hba_ioctl: unconfigure: "
14899                     "attempt to unconfigure non-existing device "
14900                     "at SATA port %d:%d",
14901                     sata_device->satadev_addr.cport,
14902                     sata_device->satadev_addr.pmport));
14903                 rv = ENXIO;
14904         }
14905         return (rv);
14906 }
14907 
14908 /*
14909  * Process sata device configure request
14910  * If port is in a failed state, operation is aborted - one has to use
14911  * an explicit connect or port activate request to try to get a port into
14912  * non-failed mode. Port reset wil also work in such situation.
14913  * If the port is in disconnected (shutdown) state, the connect operation is
14914  * attempted prior to any other action.
14915  * When port is in the active state, there is a device attached and the target
14916  * node exists, a device was most likely offlined.
14917  * If target node does not exist, a new target node is created. In both cases
14918  * an attempt is made to online (configure) the device.
14919  *
14920  * NOTE: Port multiplier is supported.
14921  */
14922 static int
14923 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
14924     sata_device_t *sata_device)
14925 {
14926         int cport, pmport, qual;
14927         int rval;
14928         boolean_t target = B_TRUE;
14929         sata_cport_info_t *cportinfo;
14930         sata_pmport_info_t *pmportinfo = NULL;
14931         dev_info_t *tdip;
14932         sata_drive_info_t *sdinfo;
14933 
14934         cport = sata_device->satadev_addr.cport;
14935         pmport = sata_device->satadev_addr.pmport;
14936         qual = sata_device->satadev_addr.qual;
14937 
14938         /* Get current port state */
14939         rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14940             (SATA_DIP(sata_hba_inst), sata_device);
14941 
14942         cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14943         if (qual == SATA_ADDR_DPMPORT) {
14944                 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14945                 mutex_enter(&pmportinfo->pmport_mutex);
14946                 sata_update_pmport_info(sata_hba_inst, sata_device);
14947                 if (rval != SATA_SUCCESS ||
14948                     (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14949                         /*
14950                          * Obviously, device on a failed port is not visible
14951                          */
14952                         mutex_exit(&pmportinfo->pmport_mutex);
14953                         return (ENXIO);
14954                 }
14955                 mutex_exit(&pmportinfo->pmport_mutex);
14956         } else {
14957                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14958                     cport)->cport_mutex);
14959                 sata_update_port_info(sata_hba_inst, sata_device);
14960                 if (rval != SATA_SUCCESS ||
14961                     (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14962                         /*
14963                          * Obviously, device on a failed port is not visible
14964                          */
14965                         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14966                             cport)->cport_mutex);
14967                         return (ENXIO);
14968                 }
14969                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14970                     cport)->cport_mutex);
14971         }
14972 
14973         if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
14974                 /* need to activate port */
14975                 target = B_FALSE;
14976 
14977                 /* Sanity check */
14978                 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
14979                         return (ENXIO);
14980 
14981                 /* Just let HBA driver to activate port */
14982                 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
14983                     (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14984                         /*
14985                          * Port activation failure - do not change port state
14986                          * unless the state returned by HBA indicates a port
14987                          * failure.
14988                          */
14989                         if (qual == SATA_ADDR_DPMPORT) {
14990                                 mutex_enter(&pmportinfo->pmport_mutex);
14991                                 sata_update_pmport_info(sata_hba_inst,
14992                                     sata_device);
14993                                 if (sata_device->satadev_state &
14994                                     SATA_PSTATE_FAILED)
14995                                         pmportinfo->pmport_state =
14996                                             SATA_PSTATE_FAILED;
14997                                 mutex_exit(&pmportinfo->pmport_mutex);
14998                         } else {
14999                                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15000                                     cport)->cport_mutex);
15001                                 sata_update_port_info(sata_hba_inst,
15002                                     sata_device);
15003                                 if (sata_device->satadev_state &
15004                                     SATA_PSTATE_FAILED)
15005                                         cportinfo->cport_state =
15006                                             SATA_PSTATE_FAILED;
15007                                 mutex_exit(&SATA_CPORT_INFO(
15008                                     sata_hba_inst, cport)->cport_mutex);
15009                         }
15010                 }
15011                 SATA_LOG_D((sata_hba_inst, CE_WARN,
15012                     "sata_hba_ioctl: configure: "
15013                     "failed to activate SATA port %d:%d",
15014                     cport, pmport));
15015                 return (EIO);
15016         }
15017         /*
15018          * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15019          * without the hint.
15020          */
15021         sata_gen_sysevent(sata_hba_inst,
15022             &sata_device->satadev_addr, SE_NO_HINT);
15023 
15024         /* Virgin port state */
15025         if (qual == SATA_ADDR_DPMPORT) {
15026                 mutex_enter(&pmportinfo->pmport_mutex);
15027                 pmportinfo->pmport_state = 0;
15028                 mutex_exit(&pmportinfo->pmport_mutex);
15029         } else {
15030                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15031                     cport)-> cport_mutex);
15032                 cportinfo->cport_state = 0;
15033                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15034                     cport)->cport_mutex);
15035         }
15036         /*
15037          * Always reprobe port, to get current device info.
15038          */
15039         if (sata_reprobe_port(sata_hba_inst, sata_device,
15040             SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
15041                 return (EIO);
15042 
15043         if (sata_device->satadev_type != SATA_DTYPE_NONE && target == B_FALSE) {
15044                 if (qual == SATA_ADDR_DPMPORT) {
15045                         /*
15046                          * That's the transition from "inactive" port
15047                          * to active one with device attached.
15048                          */
15049                         sata_log(sata_hba_inst, CE_WARN,
15050                             "SATA device detected at port %d:%d",
15051                             cport, pmport);
15052                 } else {
15053                         /*
15054                          * When PM is attached to the cport and cport is
15055                          * activated, every PM device port needs to be reprobed.
15056                          * We need to emit message for all devices detected
15057                          * at port multiplier's device ports.
15058                          * Add such code here.
15059                          * For now, just inform about device attached to
15060                          * cport.
15061                          */
15062                         sata_log(sata_hba_inst, CE_WARN,
15063                             "SATA device detected at port %d", cport);
15064                 }
15065         }
15066 
15067         /*
15068          * This is where real configuration operation starts.
15069          *
15070          * When PM is attached to the cport and cport is activated,
15071          * devices attached PM device ports may have to be configured
15072          * explicitly. This may change when port multiplier is supported.
15073          * For now, configure only disks and other valid target devices.
15074          */
15075         if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
15076                 if (qual == SATA_ADDR_DCPORT) {
15077                         if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15078                                 /*
15079                                  * A device was not successfully identified
15080                                  */
15081                                 sata_log(sata_hba_inst, CE_WARN,
15082                                     "Could not identify SATA "
15083                                     "device at port %d", cport);
15084                         }
15085                 } else { /* port multiplier device port */
15086                         if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15087                                 /*
15088                                  * A device was not successfully identified
15089                                  */
15090                                 sata_log(sata_hba_inst, CE_WARN,
15091                                     "Could not identify SATA "
15092                                     "device at port %d:%d", cport, pmport);
15093                         }
15094                 }
15095                 return (ENXIO);         /* No device to configure */
15096         }
15097 
15098         /*
15099          * Here we may have a device in reset condition,
15100          * but because we are just configuring it, there is
15101          * no need to process the reset other than just
15102          * to clear device reset condition in the HBA driver.
15103          * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
15104          * cause a first command sent the HBA driver with the request
15105          * to clear device reset condition.
15106          */
15107         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15108         if (qual == SATA_ADDR_DPMPORT)
15109                 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15110         else
15111                 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15112         sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
15113         if (sdinfo == NULL) {
15114                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15115                 return (ENXIO);
15116         }
15117         if (sdinfo->satadrv_event_flags &
15118             (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
15119                 sdinfo->satadrv_event_flags = 0;
15120         }
15121         sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
15122         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15123 
15124         if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15125             &sata_device->satadev_addr)) != NULL) {
15126                 /*
15127                  * Target node exists. Verify, that it belongs
15128                  * to existing, attached device and not to
15129                  * a removed device.
15130                  */
15131                 if (sata_check_device_removed(tdip) == B_TRUE) {
15132                         if (qual == SATA_ADDR_DPMPORT)
15133                                 sata_log(sata_hba_inst, CE_WARN,
15134                                     "SATA device at port %d cannot be "
15135                                     "configured. "
15136                                     "Application(s) accessing "
15137                                     "previously attached device "
15138                                     "have to release it before newly "
15139                                     "inserted device can be made accessible.",
15140                                     cport);
15141                         else
15142                                 sata_log(sata_hba_inst, CE_WARN,
15143                                     "SATA device at port %d:%d cannot be"
15144                                     "configured. "
15145                                     "Application(s) accessing "
15146                                     "previously attached device "
15147                                     "have to release it before newly "
15148                                     "inserted device can be made accessible.",
15149                                     cport, pmport);
15150                         return (EIO);
15151                 }
15152                 /*
15153                  * Device was not removed and re-inserted.
15154                  * Try to online it.
15155                  */
15156                 if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
15157                         SATA_LOG_D((sata_hba_inst, CE_WARN,
15158                             "sata_hba_ioctl: configure: "
15159                             "onlining device at SATA port "
15160                             "%d:%d failed", cport, pmport));
15161                         return (EIO);
15162                 }
15163 
15164                 if (qual == SATA_ADDR_DPMPORT) {
15165                         mutex_enter(&pmportinfo->pmport_mutex);
15166                         pmportinfo->pmport_tgtnode_clean = B_TRUE;
15167                         mutex_exit(&pmportinfo->pmport_mutex);
15168                 } else {
15169                         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15170                             cport)->cport_mutex);
15171                         cportinfo-> cport_tgtnode_clean = B_TRUE;
15172                         mutex_exit(&SATA_CPORT_INFO(
15173                             sata_hba_inst, cport)->cport_mutex);
15174                 }
15175         } else {
15176                 /*
15177                  * No target node - need to create a new target node.
15178                  */
15179                 if (qual == SATA_ADDR_DPMPORT) {
15180                         mutex_enter(&pmportinfo->pmport_mutex);
15181                         pmportinfo->pmport_tgtnode_clean = B_TRUE;
15182                         mutex_exit(&pmportinfo->pmport_mutex);
15183                 } else {
15184                         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15185                             cport_mutex);
15186                         cportinfo-> cport_tgtnode_clean = B_TRUE;
15187                         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15188                             cport_mutex);
15189                 }
15190 
15191                 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15192                     sata_hba_inst, &sata_device->satadev_addr);
15193                 if (tdip == NULL) {
15194                         /* Configure operation failed */
15195                         SATA_LOG_D((sata_hba_inst, CE_WARN,
15196                             "sata_hba_ioctl: configure: "
15197                             "configuring SATA device at port %d:%d "
15198                             "failed", cport, pmport));
15199                         return (EIO);
15200                 }
15201         }
15202         return (0);
15203 }
15204 
15205 
15206 /*
15207  * Process ioctl deactivate port request.
15208  * Arbitrarily unconfigure attached device, if any.
15209  * Even if the unconfigure fails, proceed with the
15210  * port deactivation.
15211  *
15212  * NOTE: Port Multiplier is supported now.
15213  */
15214 
15215 static int
15216 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
15217     sata_device_t *sata_device)
15218 {
15219         int cport, pmport, qual;
15220         int rval, rv = 0;
15221         int npmport;
15222         sata_cport_info_t *cportinfo;
15223         sata_pmport_info_t *pmportinfo;
15224         sata_pmult_info_t *pmultinfo;
15225         dev_info_t *tdip;
15226         sata_drive_info_t *sdinfo = NULL;
15227         sata_device_t subsdevice;
15228 
15229         /* Sanity check */
15230         if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
15231                 return (ENOTSUP);
15232 
15233         cport = sata_device->satadev_addr.cport;
15234         pmport = sata_device->satadev_addr.pmport;
15235         qual = sata_device->satadev_addr.qual;
15236 
15237         /* SCSI_TO_SATA_ADDR_QUAL() translate ap_id into a device qualifier */
15238         ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15239         if (qual == SATA_ADDR_DCPORT)
15240                 qual = SATA_ADDR_CPORT;
15241         else
15242                 qual = SATA_ADDR_PMPORT;
15243 
15244         cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15245         if (qual == SATA_ADDR_PMPORT)
15246                 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15247 
15248         /*
15249          * Processing port multiplier
15250          */
15251         if (qual == SATA_ADDR_CPORT &&
15252             SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
15253                 mutex_enter(&cportinfo->cport_mutex);
15254 
15255                 /* Deactivate all sub-deices */
15256                 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
15257                 if (pmultinfo != NULL) {
15258                         for (npmport = 0; npmport < SATA_NUM_PMPORTS(
15259                             sata_hba_inst, cport); npmport++) {
15260 
15261                                 subsdevice.satadev_addr.cport = cport;
15262                                 subsdevice.satadev_addr.pmport =
15263                                     (uint8_t)npmport;
15264                                 subsdevice.satadev_addr.qual =
15265                                     SATA_ADDR_DPMPORT;
15266 
15267                                 SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15268                                     "sata_hba_ioctl: deactivate: trying to "
15269                                     "deactivate SATA port %d:%d",
15270                                     cport, npmport);
15271 
15272                                 mutex_exit(&cportinfo->cport_mutex);
15273                                 if (sata_ioctl_deactivate(sata_hba_inst,
15274                                     &subsdevice) == SATA_SUCCESS) {
15275                                         SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15276                                             "[Deactivate] device at port %d:%d "
15277                                             "successfully.", cport, npmport);
15278                                 }
15279                                 mutex_enter(&cportinfo->cport_mutex);
15280                         }
15281                 }
15282 
15283                 /* Deactivate the port multiplier now. */
15284                 cportinfo->cport_state &= ~SATA_STATE_READY;
15285                 mutex_exit(&cportinfo->cport_mutex);
15286 
15287                 sata_device->satadev_addr.qual = qual;
15288                 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15289                     (SATA_DIP(sata_hba_inst), sata_device);
15290 
15291                 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15292                     SE_NO_HINT);
15293 
15294                 mutex_enter(&cportinfo->cport_mutex);
15295                 sata_update_port_info(sata_hba_inst, sata_device);
15296                 if (rval != SATA_SUCCESS) {
15297                         if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15298                                 cportinfo->cport_state = SATA_PSTATE_FAILED;
15299                         }
15300                         rv = EIO;
15301                 } else {
15302                         cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15303                 }
15304                 mutex_exit(&cportinfo->cport_mutex);
15305 
15306                 return (rv);
15307         }
15308 
15309         /*
15310          * Process non-port-multiplier device - it could be a drive connected
15311          * to a port multiplier port or a controller port.
15312          */
15313         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15314         if (qual == SATA_ADDR_CPORT) {
15315                 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15316                 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15317                         /* deal only with valid devices */
15318                         if ((cportinfo->cport_dev_type &
15319                             SATA_VALID_DEV_TYPE) != 0)
15320                                 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15321                 }
15322                 cportinfo->cport_state &= ~SATA_STATE_READY;
15323         } else {
15324                 /* Port multiplier device port */
15325                 mutex_enter(&pmportinfo->pmport_mutex);
15326                 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15327                 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
15328                     (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
15329                         sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
15330                 pmportinfo->pmport_state &= ~SATA_STATE_READY;
15331                 mutex_exit(&pmportinfo->pmport_mutex);
15332         }
15333 
15334         if (sdinfo != NULL) {
15335                 /*
15336                  * If a target node exists, try to offline a device and
15337                  * to remove a target node.
15338                  */
15339                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15340                     cport_mutex);
15341                 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15342                     &sata_device->satadev_addr);
15343                 if (tdip != NULL) {
15344                         /* target node exist */
15345                         SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
15346                             "sata_hba_ioctl: port deactivate: "
15347                             "target node exists.", NULL);
15348 
15349                         if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
15350                             NDI_SUCCESS) {
15351                                 SATA_LOG_D((sata_hba_inst, CE_WARN,
15352                                     "sata_hba_ioctl: port deactivate: "
15353                                     "failed to unconfigure device at port "
15354                                     "%d:%d before deactivating the port",
15355                                     cport, pmport));
15356                                 /*
15357                                  * Set DEVICE REMOVED state in the target
15358                                  * node. It will prevent an access to
15359                                  * the device even when a new device is
15360                                  * attached, until the old target node is
15361                                  * released, removed and recreated for a new
15362                                  * device.
15363                                  */
15364                                 sata_set_device_removed(tdip);
15365 
15366                                 /*
15367                                  * Instruct the event daemon to try the
15368                                  * target node cleanup later.
15369                                  */
15370                                 sata_set_target_node_cleanup(sata_hba_inst,
15371                                     &sata_device->satadev_addr);
15372                         }
15373                 }
15374                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15375                     cport_mutex);
15376                 /*
15377                  * In any case, remove and release sata_drive_info
15378                  * structure.
15379                  */
15380                 if (qual == SATA_ADDR_CPORT) {
15381                         SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
15382                         cportinfo->cport_dev_type = SATA_DTYPE_NONE;
15383                 } else { /* port multiplier device port */
15384                         mutex_enter(&pmportinfo->pmport_mutex);
15385                         SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
15386                         pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
15387                         mutex_exit(&pmportinfo->pmport_mutex);
15388                 }
15389                 (void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
15390         }
15391 
15392         if (qual == SATA_ADDR_CPORT) {
15393                 cportinfo->cport_state &= ~(SATA_STATE_PROBED |
15394                     SATA_STATE_PROBING);
15395         } else if (qual == SATA_ADDR_PMPORT) {
15396                 mutex_enter(&pmportinfo->pmport_mutex);
15397                 pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
15398                     SATA_STATE_PROBING);
15399                 mutex_exit(&pmportinfo->pmport_mutex);
15400         }
15401         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15402 
15403         /* Just let HBA driver to deactivate port */
15404         sata_device->satadev_addr.qual = qual;
15405         rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15406             (SATA_DIP(sata_hba_inst), sata_device);
15407 
15408         /*
15409          * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15410          * without the hint
15411          */
15412         sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15413             SE_NO_HINT);
15414 
15415         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15416         sata_update_port_info(sata_hba_inst, sata_device);
15417         if (qual == SATA_ADDR_CPORT) {
15418                 if (rval != SATA_SUCCESS) {
15419                         /*
15420                          * Port deactivation failure - do not change port state
15421                          * unless the state returned by HBA indicates a port
15422                          * failure.
15423                          */
15424                         if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15425                                 SATA_CPORT_STATE(sata_hba_inst, cport) =
15426                                     SATA_PSTATE_FAILED;
15427                         }
15428                         SATA_LOG_D((sata_hba_inst, CE_WARN,
15429                             "sata_hba_ioctl: port deactivate: "
15430                             "cannot deactivate SATA port %d", cport));
15431                         rv = EIO;
15432                 } else {
15433                         cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15434                 }
15435         } else {
15436                 mutex_enter(&pmportinfo->pmport_mutex);
15437                 if (rval != SATA_SUCCESS) {
15438                         if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15439                                 SATA_PMPORT_STATE(sata_hba_inst, cport,
15440                                     pmport) = SATA_PSTATE_FAILED;
15441                         }
15442                         SATA_LOG_D((sata_hba_inst, CE_WARN,
15443                             "sata_hba_ioctl: port deactivate: "
15444                             "cannot deactivate SATA port %d:%d",
15445                             cport, pmport));
15446                         rv = EIO;
15447                 } else {
15448                         pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
15449                 }
15450                 mutex_exit(&pmportinfo->pmport_mutex);
15451         }
15452 
15453         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15454 
15455         return (rv);
15456 }
15457 
15458 /*
15459  * Process ioctl port activate request.
15460  *
15461  * NOTE: Port multiplier is supported now.
15462  */
15463 static int
15464 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
15465     sata_device_t *sata_device)
15466 {
15467         int cport, pmport, qual;
15468         sata_cport_info_t *cportinfo;
15469         sata_pmport_info_t *pmportinfo = NULL;
15470         boolean_t dev_existed = B_TRUE;
15471 
15472         /* Sanity check */
15473         if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
15474                 return (ENOTSUP);
15475 
15476         cport = sata_device->satadev_addr.cport;
15477         pmport = sata_device->satadev_addr.pmport;
15478         qual = sata_device->satadev_addr.qual;
15479 
15480         cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15481 
15482         /*
15483          * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
15484          * is a device. But what we are dealing with is port/pmport.
15485          */
15486         ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15487         if (qual == SATA_ADDR_DCPORT)
15488                 sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
15489         else
15490                 sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
15491 
15492         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15493         if (qual == SATA_ADDR_PMPORT) {
15494                 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15495                 if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
15496                     pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
15497                         dev_existed = B_FALSE;
15498         } else { /* cport */
15499                 if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
15500                     cportinfo->cport_dev_type == SATA_DTYPE_NONE)
15501                         dev_existed = B_FALSE;
15502         }
15503         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15504 
15505         /* Just let HBA driver to activate port, if necessary */
15506         if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
15507             (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15508                 /*
15509                  * Port activation failure - do not change port state unless
15510                  * the state returned by HBA indicates a port failure.
15511                  */
15512                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15513                     cport)->cport_mutex);
15514                 sata_update_port_info(sata_hba_inst, sata_device);
15515                 if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15516                         if (qual == SATA_ADDR_PMPORT) {
15517                                 mutex_enter(&pmportinfo->pmport_mutex);
15518                                 pmportinfo->pmport_state = SATA_PSTATE_FAILED;
15519                                 mutex_exit(&pmportinfo->pmport_mutex);
15520                         } else
15521                                 cportinfo->cport_state = SATA_PSTATE_FAILED;
15522 
15523                         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15524                             cport)->cport_mutex);
15525                         SATA_LOG_D((sata_hba_inst, CE_WARN,
15526                             "sata_hba_ioctl: port activate: cannot activate "
15527                             "SATA port %d:%d", cport, pmport));
15528                         return (EIO);
15529                 }
15530                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15531         }
15532         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15533         if (qual == SATA_ADDR_PMPORT) {
15534                 mutex_enter(&pmportinfo->pmport_mutex);
15535                 pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
15536                 mutex_exit(&pmportinfo->pmport_mutex);
15537         } else
15538                 cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
15539         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15540 
15541         /*
15542          * Re-probe port to find its current state and possibly attached device.
15543          * Port re-probing may change the cportinfo device type if device is
15544          * found attached.
15545          * If port probing failed, the device type would be set to
15546          * SATA_DTYPE_NONE.
15547          */
15548         (void) sata_reprobe_port(sata_hba_inst, sata_device,
15549             SATA_DEV_IDENTIFY_RETRY);
15550 
15551         /*
15552          * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15553          * without the hint.
15554          */
15555         sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15556             SE_NO_HINT);
15557 
15558         if (dev_existed == B_FALSE) {
15559                 if (qual == SATA_ADDR_PMPORT &&
15560                     pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
15561                         /*
15562                          * That's the transition from the "inactive" port state
15563                          * or the active port without a device attached to the
15564                          * active port state with a device attached.
15565                          */
15566                         sata_log(sata_hba_inst, CE_WARN,
15567                             "SATA device detected at port %d:%d",
15568                             cport, pmport);
15569                 } else if (qual == SATA_ADDR_CPORT &&
15570                     cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15571                         /*
15572                          * That's the transition from the "inactive" port state
15573                          * or the active port without a device attached to the
15574                          * active port state with a device attached.
15575                          */
15576                         if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
15577                                 sata_log(sata_hba_inst, CE_WARN,
15578                                     "SATA device detected at port %d", cport);
15579                         } else {
15580                                 sata_log(sata_hba_inst, CE_WARN,
15581                                     "SATA port multiplier detected at port %d",
15582                                     cport);
15583                         }
15584                 }
15585         }
15586         return (0);
15587 }
15588 
15589 
15590 
15591 /*
15592  * Process ioctl reset port request.
15593  *
15594  * NOTE: Port-Multiplier is supported.
15595  */
15596 static int
15597 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
15598     sata_device_t *sata_device)
15599 {
15600         int cport, pmport, qual;
15601         int rv = 0;
15602 
15603         cport = sata_device->satadev_addr.cport;
15604         pmport = sata_device->satadev_addr.pmport;
15605         qual = sata_device->satadev_addr.qual;
15606 
15607         /*
15608          * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
15609          * is a device. But what we are dealing with is port/pmport.
15610          */
15611         if (qual == SATA_ADDR_DCPORT)
15612                 sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
15613         else
15614                 sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
15615         ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
15616 
15617         /* Sanity check */
15618         if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15619                 SATA_LOG_D((sata_hba_inst, CE_WARN,
15620                     "sata_hba_ioctl: sata_hba_tran missing required "
15621                     "function sata_tran_reset_dport"));
15622                 return (ENOTSUP);
15623         }
15624 
15625         /* Ask HBA to reset port */
15626         if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
15627             sata_device) != SATA_SUCCESS) {
15628                 SATA_LOG_D((sata_hba_inst, CE_WARN,
15629                     "sata_hba_ioctl: reset port: failed %d:%d",
15630                     cport, pmport));
15631                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15632                     cport_mutex);
15633                 sata_update_port_info(sata_hba_inst, sata_device);
15634                 if (qual == SATA_ADDR_CPORT)
15635                         SATA_CPORT_STATE(sata_hba_inst, cport) =
15636                             SATA_PSTATE_FAILED;
15637                 else {
15638                         mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
15639                             pmport));
15640                         SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15641                             SATA_PSTATE_FAILED;
15642                         mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
15643                             pmport));
15644                 }
15645                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15646                     cport_mutex);
15647                 rv = EIO;
15648         }
15649 
15650         return (rv);
15651 }
15652 
15653 /*
15654  * Process ioctl reset device request.
15655  *
15656  * NOTE: Port multiplier is supported.
15657  */
15658 static int
15659 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
15660     sata_device_t *sata_device)
15661 {
15662         sata_drive_info_t *sdinfo = NULL;
15663         sata_pmult_info_t *pmultinfo = NULL;
15664         int cport, pmport;
15665         int rv = 0;
15666 
15667         /* Sanity check */
15668         if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15669                 SATA_LOG_D((sata_hba_inst, CE_WARN,
15670                     "sata_hba_ioctl: sata_hba_tran missing required "
15671                     "function sata_tran_reset_dport"));
15672                 return (ENOTSUP);
15673         }
15674 
15675         cport = sata_device->satadev_addr.cport;
15676         pmport = sata_device->satadev_addr.pmport;
15677 
15678         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15679         if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
15680                 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
15681                     SATA_DTYPE_PMULT)
15682                         pmultinfo = SATA_CPORT_INFO(sata_hba_inst, cport)->
15683                             cport_devp.cport_sata_pmult;
15684                 else
15685                         sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
15686                             sata_device->satadev_addr.cport);
15687         } else { /* port multiplier */
15688                 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15689                 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
15690                     sata_device->satadev_addr.cport,
15691                     sata_device->satadev_addr.pmport);
15692         }
15693         if (sdinfo == NULL && pmultinfo == NULL) {
15694                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15695                 return (EINVAL);
15696         }
15697         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15698 
15699         /* Ask HBA to reset device */
15700         if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
15701             (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15702                 SATA_LOG_D((sata_hba_inst, CE_WARN,
15703                     "sata_hba_ioctl: reset device: failed at port %d:%d",
15704                     cport, pmport));
15705                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15706                     cport_mutex);
15707                 sata_update_port_info(sata_hba_inst, sata_device);
15708                 /*
15709                  * Device info structure remains attached. Another device reset
15710                  * or port disconnect/connect and re-probing is
15711                  * needed to change it's state
15712                  */
15713                 if (sdinfo != NULL) {
15714                         sdinfo->satadrv_state &= ~SATA_STATE_READY;
15715                         sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
15716                 } else if (pmultinfo != NULL) {
15717                         pmultinfo->pmult_state &= ~SATA_STATE_READY;
15718                         pmultinfo->pmult_state |= SATA_DSTATE_FAILED;
15719                 }
15720 
15721                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15722                 rv = EIO;
15723         }
15724         /*
15725          * If attached device was a port multiplier, some extra processing
15726          * may be needed to bring it back. SATA specification requies a
15727          * mandatory software reset on host port to reliably enumerate a port
15728          * multiplier, the HBA driver should handle that after reset
15729          * operation.
15730          */
15731         return (rv);
15732 }
15733 
15734 
15735 /*
15736  * Process ioctl reset all request.
15737  */
15738 static int
15739 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
15740 {
15741         sata_device_t sata_device;
15742         int rv = 0;
15743         int tcport;
15744 
15745         sata_device.satadev_rev = SATA_DEVICE_REV;
15746 
15747         /*
15748          * There is no protection here for configured devices.
15749          */
15750         /* Sanity check */
15751         if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15752                 SATA_LOG_D((sata_hba_inst, CE_WARN,
15753                     "sata_hba_ioctl: sata_hba_tran missing required "
15754                     "function sata_tran_reset_dport"));
15755                 return (ENOTSUP);
15756         }
15757 
15758         /*
15759          * Need to lock all ports, not just one.
15760          * If any port is locked by event processing, fail the whole operation.
15761          * One port is already locked, but for simplicity lock it again.
15762          */
15763         for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
15764                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15765                     cport_mutex);
15766                 if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
15767                     cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
15768                         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15769                             cport_mutex);
15770                         rv = EBUSY;
15771                         break;
15772                 } else {
15773                         /*
15774                          * It is enough to lock cport in command-based
15775                          * switching mode.
15776                          */
15777                         SATA_CPORT_INFO(sata_hba_inst, tcport)->
15778                             cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
15779                 }
15780                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15781                     cport_mutex);
15782         }
15783 
15784         if (rv == 0) {
15785                 /*
15786                  * All cports were successfully locked.
15787                  * Reset main SATA controller.
15788                  * Set the device address to port 0, to have a valid device
15789                  * address.
15790                  */
15791                 sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
15792                 sata_device.satadev_addr.cport = 0;
15793                 sata_device.satadev_addr.pmport = 0;
15794 
15795                 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
15796                     (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
15797                         SATA_LOG_D((sata_hba_inst, CE_WARN,
15798                             "sata_hba_ioctl: reset controller failed"));
15799                         return (EIO);
15800                 }
15801         }
15802         /*
15803          * Unlock all ports
15804          */
15805         for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
15806                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15807                     cport_mutex);
15808                 SATA_CPORT_INFO(sata_hba_inst, tcport)->
15809                     cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
15810                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15811                     cport_mutex);
15812         }
15813 
15814         /*
15815          * This operation returns EFAULT if either reset
15816          * controller failed or a re-probing of any port failed.
15817          */
15818         return (rv);
15819 }
15820 
15821 
15822 /*
15823  * Process ioctl port self test request.
15824  *
15825  * NOTE: Port multiplier code is not completed nor tested.
15826  */
15827 static int
15828 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
15829     sata_device_t *sata_device)
15830 {
15831         int cport, pmport, qual;
15832         int rv = 0;
15833 
15834         /* Sanity check */
15835         if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
15836                 return (ENOTSUP);
15837 
15838         cport = sata_device->satadev_addr.cport;
15839         pmport = sata_device->satadev_addr.pmport;
15840         qual = sata_device->satadev_addr.qual;
15841 
15842         /*
15843          * There is no protection here for a configured
15844          * device attached to this port.
15845          */
15846 
15847         if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
15848             (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15849                 SATA_LOG_D((sata_hba_inst, CE_WARN,
15850                     "sata_hba_ioctl: port selftest: "
15851                     "failed port %d:%d", cport, pmport));
15852                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15853                     cport_mutex);
15854                 sata_update_port_info(sata_hba_inst, sata_device);
15855                 if (qual == SATA_ADDR_CPORT)
15856                         SATA_CPORT_STATE(sata_hba_inst, cport) =
15857                             SATA_PSTATE_FAILED;
15858                 else { /* port multiplier device port */
15859                         mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
15860                             cport, pmport));
15861                         SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15862                             SATA_PSTATE_FAILED;
15863                         mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
15864                             cport, pmport));
15865                 }
15866 
15867                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15868                     cport_mutex);
15869                 return (EIO);
15870         }
15871         /*
15872          * Beacuse the port was reset in the course of testing, it should be
15873          * re-probed and attached device state should be restored. At this
15874          * point the port state is unknown - it's state is HBA-specific.
15875          * Force port re-probing to get it into a known state.
15876          */
15877         if (sata_reprobe_port(sata_hba_inst, sata_device,
15878             SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
15879                 rv = EIO;
15880         return (rv);
15881 }
15882 
15883 
15884 /*
15885  * sata_cfgadm_state:
15886  * Use the sata port state and state of the target node to figure out
15887  * the cfgadm_state.
15888  *
15889  * The port argument is a value with encoded cport,
15890  * pmport and address qualifier, in the same manner as a scsi target number.
15891  * SCSI_TO_SATA_CPORT macro extracts cport number,
15892  * SCSI_TO_SATA_PMPORT extracts pmport number and
15893  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
15894  *
15895  * Port multiplier is supported.
15896  */
15897 
15898 static void
15899 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
15900     devctl_ap_state_t *ap_state)
15901 {
15902         uint8_t         cport, pmport, qual;
15903         uint32_t        port_state, pmult_state;
15904         uint32_t        dev_type;
15905         sata_drive_info_t *sdinfo;
15906 
15907         cport = SCSI_TO_SATA_CPORT(port);
15908         pmport = SCSI_TO_SATA_PMPORT(port);
15909         qual = SCSI_TO_SATA_ADDR_QUAL(port);
15910 
15911         /* Check cport state */
15912         port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
15913         if (port_state & SATA_PSTATE_SHUTDOWN ||
15914             port_state & SATA_PSTATE_FAILED) {
15915                 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15916                 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15917                 if (port_state & SATA_PSTATE_FAILED)
15918                         ap_state->ap_condition = AP_COND_FAILED;
15919                 else
15920                         ap_state->ap_condition = AP_COND_UNKNOWN;
15921 
15922                 return;
15923         }
15924 
15925         /* cport state is okay. Now check pmport state */
15926         if (qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) {
15927                 /* Sanity check */
15928                 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
15929                     SATA_DTYPE_PMULT || SATA_PMPORT_INFO(sata_hba_inst,
15930                     cport, pmport) == NULL)
15931                         return;
15932                 port_state = SATA_PMPORT_STATE(sata_hba_inst, cport, pmport);
15933                 if (port_state & SATA_PSTATE_SHUTDOWN ||
15934                     port_state & SATA_PSTATE_FAILED) {
15935                         ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15936                         ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15937                         if (port_state & SATA_PSTATE_FAILED)
15938                                 ap_state->ap_condition = AP_COND_FAILED;
15939                         else
15940                                 ap_state->ap_condition = AP_COND_UNKNOWN;
15941 
15942                         return;
15943                 }
15944         }
15945 
15946         /* Port is enabled and ready */
15947         if (qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_CPORT)
15948                 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, cport);
15949         else
15950                 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport);
15951 
15952         switch (dev_type) {
15953         case SATA_DTYPE_NONE:
15954         {
15955                 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15956                 ap_state->ap_condition = AP_COND_OK;
15957                 /* No device attached */
15958                 ap_state->ap_rstate = AP_RSTATE_EMPTY;
15959                 break;
15960         }
15961         case SATA_DTYPE_PMULT:
15962         {
15963                 /* Need to check port multiplier state */
15964                 ASSERT(qual == SATA_ADDR_DCPORT);
15965                 pmult_state = SATA_PMULT_INFO(sata_hba_inst, cport)->
15966                     pmult_state;
15967                 if (pmult_state & (SATA_PSTATE_SHUTDOWN|SATA_PSTATE_FAILED)) {
15968                         ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15969                         ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15970                         if (pmult_state & SATA_PSTATE_FAILED)
15971                                 ap_state->ap_condition = AP_COND_FAILED;
15972                         else
15973                                 ap_state->ap_condition = AP_COND_UNKNOWN;
15974 
15975                         return;
15976                 }
15977 
15978                 /* Port multiplier is not configurable */
15979                 ap_state->ap_ostate = AP_OSTATE_CONFIGURED;
15980                 ap_state->ap_rstate = AP_RSTATE_CONNECTED;
15981                 ap_state->ap_condition = AP_COND_OK;
15982                 break;
15983         }
15984 
15985         case SATA_DTYPE_ATADISK:
15986         case SATA_DTYPE_ATAPICD:
15987         case SATA_DTYPE_ATAPITAPE:
15988         case SATA_DTYPE_ATAPIDISK:
15989         {
15990                 dev_info_t *tdip = NULL;
15991                 dev_info_t *dip = NULL;
15992                 int circ;
15993 
15994                 dip = SATA_DIP(sata_hba_inst);
15995                 tdip = sata_get_target_dip(dip, cport, pmport);
15996                 ap_state->ap_rstate = AP_RSTATE_CONNECTED;
15997                 if (tdip != NULL) {
15998                         ndi_devi_enter(dip, &circ);
15999                         mutex_enter(&(DEVI(tdip)->devi_lock));
16000                         if (DEVI_IS_DEVICE_REMOVED(tdip)) {
16001                                 /*
16002                                  * There could be the case where previously
16003                                  * configured and opened device was removed
16004                                  * and unknown device was plugged.
16005                                  * In such case we want to show a device, and
16006                                  * its configured or unconfigured state but
16007                                  * indicate unusable condition untill the
16008                                  * old target node is released and removed.
16009                                  */
16010                                 ap_state->ap_condition = AP_COND_UNUSABLE;
16011                         } else {
16012                                 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
16013                                     cport));
16014                                 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16015                                     cport);
16016                                 if (sdinfo != NULL) {
16017                                         if ((sdinfo->satadrv_state &
16018                                             SATA_DSTATE_FAILED) != 0)
16019                                                 ap_state->ap_condition =
16020                                                     AP_COND_FAILED;
16021                                         else
16022                                                 ap_state->ap_condition =
16023                                                     AP_COND_OK;
16024                                 } else {
16025                                         ap_state->ap_condition =
16026                                             AP_COND_UNKNOWN;
16027                                 }
16028                                 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
16029                                     cport));
16030                         }
16031                         if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
16032                             (DEVI_IS_DEVICE_DOWN(tdip))) {
16033                                 ap_state->ap_ostate =
16034                                     AP_OSTATE_UNCONFIGURED;
16035                         } else {
16036                                 ap_state->ap_ostate =
16037                                     AP_OSTATE_CONFIGURED;
16038                         }
16039                         mutex_exit(&(DEVI(tdip)->devi_lock));
16040                         ndi_devi_exit(dip, circ);
16041                 } else {
16042                         ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16043                         ap_state->ap_condition = AP_COND_UNKNOWN;
16044                 }
16045                 break;
16046         }
16047         case SATA_DTYPE_ATAPIPROC:
16048                 ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16049                 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16050                 ap_state->ap_condition = AP_COND_OK;
16051                 break;
16052         default:
16053                 ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16054                 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16055                 ap_state->ap_condition = AP_COND_UNKNOWN;
16056                 /*
16057                  * This is actually internal error condition (non fatal),
16058                  * because we have already checked all defined device types.
16059                  */
16060                 SATA_LOG_D((sata_hba_inst, CE_WARN,
16061                     "sata_cfgadm_state: Internal error: "
16062                     "unknown device type"));
16063                 break;
16064         }
16065 }
16066 
16067 
16068 /*
16069  * Process ioctl get device path request.
16070  *
16071  * NOTE: Port multiplier has no target dip. Devices connected to port
16072  * multiplier have target node attached to the HBA node. The only difference
16073  * between them and the directly-attached device node is a target address.
16074  */
16075 static int
16076 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
16077     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16078 {
16079         char path[MAXPATHLEN];
16080         uint32_t size;
16081         dev_info_t *tdip;
16082 
16083         (void) strcpy(path, "/devices");
16084         if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
16085             &sata_device->satadev_addr)) == NULL) {
16086                 /*
16087                  * No such device. If this is a request for a size, do not
16088                  * return EINVAL for non-existing target, because cfgadm
16089                  * will then indicate a meaningless ioctl failure.
16090                  * If this is a request for a path, indicate invalid
16091                  * argument.
16092                  */
16093                 if (ioc->get_size == 0)
16094                         return (EINVAL);
16095         } else {
16096                 (void) ddi_pathname(tdip, path + strlen(path));
16097         }
16098         size = strlen(path) + 1;
16099 
16100         if (ioc->get_size != 0) {
16101                 if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
16102                     mode) != 0)
16103                         return (EFAULT);
16104         } else {
16105                 if (ioc->bufsiz != size)
16106                         return (EINVAL);
16107 
16108                 else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
16109                     mode) != 0)
16110                         return (EFAULT);
16111         }
16112         return (0);
16113 }
16114 
16115 /*
16116  * Process ioctl get attachment point type request.
16117  *
16118  * NOTE: Port multiplier is supported.
16119  */
16120 static  int
16121 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
16122     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16123 {
16124         uint32_t        type_len;
16125         const char      *ap_type;
16126         int             dev_type;
16127 
16128         if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16129                 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
16130                     sata_device->satadev_addr.cport);
16131         else /* pmport */
16132                 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
16133                     sata_device->satadev_addr.cport,
16134                     sata_device->satadev_addr.pmport);
16135 
16136         switch (dev_type) {
16137         case SATA_DTYPE_NONE:
16138                 ap_type = "port";
16139                 break;
16140 
16141         case SATA_DTYPE_ATADISK:
16142         case SATA_DTYPE_ATAPIDISK:
16143                 ap_type = "disk";
16144                 break;
16145 
16146         case SATA_DTYPE_ATAPICD:
16147                 ap_type = "cd/dvd";
16148                 break;
16149 
16150         case SATA_DTYPE_ATAPITAPE:
16151                 ap_type = "tape";
16152                 break;
16153 
16154         case SATA_DTYPE_ATAPIPROC:
16155                 ap_type = "processor";
16156                 break;
16157 
16158         case SATA_DTYPE_PMULT:
16159                 ap_type = "sata-pmult";
16160                 break;
16161 
16162         case SATA_DTYPE_UNKNOWN:
16163                 ap_type = "unknown";
16164                 break;
16165 
16166         default:
16167                 ap_type = "unsupported";
16168                 break;
16169 
16170         } /* end of dev_type switch */
16171 
16172         type_len = strlen(ap_type) + 1;
16173 
16174         if (ioc->get_size) {
16175                 if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
16176                     mode) != 0)
16177                         return (EFAULT);
16178         } else {
16179                 if (ioc->bufsiz != type_len)
16180                         return (EINVAL);
16181 
16182                 if (ddi_copyout((void *)ap_type, ioc->buf,
16183                     ioc->bufsiz, mode) != 0)
16184                         return (EFAULT);
16185         }
16186         return (0);
16187 
16188 }
16189 
16190 /*
16191  * Process ioctl get device model info request.
16192  * This operation should return to cfgadm the device model
16193  * information string
16194  *
16195  * NOTE: Port multiplier is supported.
16196  */
16197 static  int
16198 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
16199     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16200 {
16201         sata_drive_info_t *sdinfo;
16202         uint32_t info_len;
16203         char ap_info[SATA_ID_MODEL_LEN + 1];
16204 
16205         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16206             sata_device->satadev_addr.cport)->cport_mutex);
16207         if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16208                 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16209                     sata_device->satadev_addr.cport);
16210         else /* port multiplier */
16211                 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16212                     sata_device->satadev_addr.cport,
16213                     sata_device->satadev_addr.pmport);
16214         if (sdinfo == NULL) {
16215                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16216                     sata_device->satadev_addr.cport)->cport_mutex);
16217                 return (EINVAL);
16218         }
16219 
16220 #ifdef  _LITTLE_ENDIAN
16221         swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16222 #else   /* _LITTLE_ENDIAN */
16223         bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16224 #endif  /* _LITTLE_ENDIAN */
16225 
16226         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16227             sata_device->satadev_addr.cport)->cport_mutex);
16228 
16229         ap_info[SATA_ID_MODEL_LEN] = '\0';
16230 
16231         info_len = strlen(ap_info) + 1;
16232 
16233         if (ioc->get_size) {
16234                 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16235                     mode) != 0)
16236                         return (EFAULT);
16237         } else {
16238                 if (ioc->bufsiz < info_len)
16239                         return (EINVAL);
16240                 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16241                     mode) != 0)
16242                         return (EFAULT);
16243         }
16244         return (0);
16245 }
16246 
16247 
16248 /*
16249  * Process ioctl get device firmware revision info request.
16250  * This operation should return to cfgadm the device firmware revision
16251  * information string
16252  *
16253  * Port multiplier is supported.
16254  */
16255 static  int
16256 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
16257     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16258 {
16259         sata_drive_info_t *sdinfo;
16260         uint32_t info_len;
16261         char ap_info[SATA_ID_FW_LEN + 1];
16262 
16263         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16264             sata_device->satadev_addr.cport)->cport_mutex);
16265         if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16266                 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16267                     sata_device->satadev_addr.cport);
16268         else /* port multiplier */
16269                 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16270                     sata_device->satadev_addr.cport,
16271                     sata_device->satadev_addr.pmport);
16272         if (sdinfo == NULL) {
16273                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16274                     sata_device->satadev_addr.cport)->cport_mutex);
16275                 return (EINVAL);
16276         }
16277 
16278 #ifdef  _LITTLE_ENDIAN
16279         swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16280 #else   /* _LITTLE_ENDIAN */
16281         bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16282 #endif  /* _LITTLE_ENDIAN */
16283 
16284         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16285             sata_device->satadev_addr.cport)->cport_mutex);
16286 
16287         ap_info[SATA_ID_FW_LEN] = '\0';
16288 
16289         info_len = strlen(ap_info) + 1;
16290 
16291         if (ioc->get_size) {
16292                 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16293                     mode) != 0)
16294                         return (EFAULT);
16295         } else {
16296                 if (ioc->bufsiz < info_len)
16297                         return (EINVAL);
16298                 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16299                     mode) != 0)
16300                         return (EFAULT);
16301         }
16302         return (0);
16303 }
16304 
16305 
16306 /*
16307  * Process ioctl get device serial number info request.
16308  * This operation should return to cfgadm the device serial number string.
16309  *
16310  * NOTE: Port multiplier is supported.
16311  */
16312 static  int
16313 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
16314     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16315 {
16316         sata_drive_info_t *sdinfo;
16317         uint32_t info_len;
16318         char ap_info[SATA_ID_SERIAL_LEN + 1];
16319 
16320         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16321             sata_device->satadev_addr.cport)->cport_mutex);
16322         if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16323                 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16324                     sata_device->satadev_addr.cport);
16325         else /* port multiplier */
16326                 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16327                     sata_device->satadev_addr.cport,
16328                     sata_device->satadev_addr.pmport);
16329         if (sdinfo == NULL) {
16330                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16331                     sata_device->satadev_addr.cport)->cport_mutex);
16332                 return (EINVAL);
16333         }
16334 
16335 #ifdef  _LITTLE_ENDIAN
16336         swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16337 #else   /* _LITTLE_ENDIAN */
16338         bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16339 #endif  /* _LITTLE_ENDIAN */
16340 
16341         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16342             sata_device->satadev_addr.cport)->cport_mutex);
16343 
16344         ap_info[SATA_ID_SERIAL_LEN] = '\0';
16345 
16346         info_len = strlen(ap_info) + 1;
16347 
16348         if (ioc->get_size) {
16349                 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16350                     mode) != 0)
16351                         return (EFAULT);
16352         } else {
16353                 if (ioc->bufsiz < info_len)
16354                         return (EINVAL);
16355                 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16356                     mode) != 0)
16357                         return (EFAULT);
16358         }
16359         return (0);
16360 }
16361 
16362 
16363 /*
16364  * Preset scsi extended sense data (to NO SENSE)
16365  * First 18 bytes of the sense data are preset to current valid sense
16366  * with a key NO SENSE data.
16367  *
16368  * Returns void
16369  */
16370 static void
16371 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
16372 {
16373         sense->es_valid = 1;         /* Valid sense */
16374         sense->es_class = CLASS_EXTENDED_SENSE;      /* 0x70 - current err */
16375         sense->es_key = KEY_NO_SENSE;
16376         sense->es_info_1 = 0;
16377         sense->es_info_2 = 0;
16378         sense->es_info_3 = 0;
16379         sense->es_info_4 = 0;
16380         sense->es_add_len = 10;      /* Additional length - replace with a def */
16381         sense->es_cmd_info[0] = 0;
16382         sense->es_cmd_info[1] = 0;
16383         sense->es_cmd_info[2] = 0;
16384         sense->es_cmd_info[3] = 0;
16385         sense->es_add_code = 0;
16386         sense->es_qual_code = 0;
16387 }
16388 
16389 /*
16390  * Register a legacy cmdk-style devid for the target (disk) device.
16391  *
16392  * Note: This function is called only when the HBA devinfo node has the
16393  * property "use-cmdk-devid-format" set. This property indicates that
16394  * devid compatible with old cmdk (target) driver is to be generated
16395  * for any target device attached to this controller. This will take
16396  * precedence over the devid generated by sd (target) driver.
16397  * This function is derived from cmdk_devid_setup() function in cmdk.c.
16398  */
16399 static void
16400 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
16401 {
16402         char    *hwid;
16403         int     modlen;
16404         int     serlen;
16405         int     rval;
16406         ddi_devid_t     devid;
16407 
16408         /*
16409          * device ID is a concatanation of model number, "=", serial number.
16410          */
16411         hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
16412         bcopy(&sdinfo->satadrv_id.ai_model, hwid,
16413             sizeof (sdinfo->satadrv_id.ai_model));
16414         swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
16415         modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
16416         if (modlen == 0)
16417                 goto err;
16418         hwid[modlen++] = '=';
16419         bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
16420             sizeof (sdinfo->satadrv_id.ai_drvser));
16421         swab(&hwid[modlen], &hwid[modlen],
16422             sizeof (sdinfo->satadrv_id.ai_drvser));
16423         serlen = sata_check_modser(&hwid[modlen],
16424             sizeof (sdinfo->satadrv_id.ai_drvser));
16425         if (serlen == 0)
16426                 goto err;
16427         hwid[modlen + serlen] = 0; /* terminate the hwid string */
16428 
16429         /* initialize/register devid */
16430         if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
16431             (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) {
16432                 rval = ddi_devid_register(dip, devid);
16433                 /*
16434                  * Free up the allocated devid buffer.
16435                  * NOTE: This doesn't mean unregistering devid.
16436                  */
16437                 ddi_devid_free(devid);
16438         }
16439 
16440         if (rval != DDI_SUCCESS)
16441                 cmn_err(CE_WARN, "sata: failed to create devid for the disk"
16442                     " on port %d", sdinfo->satadrv_addr.cport);
16443 err:
16444         kmem_free(hwid, LEGACY_HWID_LEN);
16445 }
16446 
16447 /*
16448  * valid model/serial string must contain a non-zero non-space characters.
16449  * trim trailing spaces/NULLs.
16450  */
16451 static int
16452 sata_check_modser(char *buf, int buf_len)
16453 {
16454         boolean_t ret;
16455         char *s;
16456         int i;
16457         int tb;
16458         char ch;
16459 
16460         ret = B_FALSE;
16461         s = buf;
16462         for (i = 0; i < buf_len; i++) {
16463                 ch = *s++;
16464                 if (ch != ' ' && ch != '\0')
16465                         tb = i + 1;
16466                 if (ch != ' ' && ch != '\0' && ch != '0')
16467                         ret = B_TRUE;
16468         }
16469 
16470         if (ret == B_FALSE)
16471                 return (0); /* invalid string */
16472 
16473         return (tb); /* return length */
16474 }
16475 
16476 /*
16477  * sata_set_drive_features function compares current device features setting
16478  * with the saved device features settings and, if there is a difference,
16479  * it restores device features setting to the previously saved state.
16480  * It also arbitrarily tries to select the highest supported DMA mode.
16481  * Device Identify or Identify Packet Device data has to be current.
16482  * At the moment read ahead and write cache are considered for all devices.
16483  * For atapi devices, Removable Media Status Notification is set in addition
16484  * to common features.
16485  *
16486  * This function cannot be called in the interrupt context (it may sleep).
16487  *
16488  * The input argument sdinfo should point to the drive info structure
16489  * to be updated after features are set. Note, that only
16490  * device (packet) identify data is updated, not the flags indicating the
16491  * supported features.
16492  *
16493  * Returns SATA_SUCCESS if successful or there was nothing to do.
16494  * Device Identify data in the drive info structure pointed to by the sdinfo
16495  * arguments is updated even when no features were set or changed.
16496  *
16497  * Returns SATA_FAILURE if device features could not be set or DMA mode
16498  * for a disk cannot be set and device identify data cannot be fetched.
16499  *
16500  * Returns SATA_RETRY if device features could not be set (other than disk
16501  * DMA mode) but the device identify data was fetched successfully.
16502  *
16503  * Note: This function may fail the port, making it inaccessible.
16504  * In such case the explicit port disconnect/connect or physical device
16505  * detach/attach is required to re-evaluate port state again.
16506  */
16507 
16508 static int
16509 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
16510     sata_drive_info_t *sdinfo, int restore)
16511 {
16512         int rval = SATA_SUCCESS;
16513         int rval_set;
16514         sata_drive_info_t new_sdinfo;
16515         char *finfo = "sata_set_drive_features: cannot";
16516         char *finfox;
16517         int cache_op;
16518 
16519         bzero(&new_sdinfo, sizeof (sata_drive_info_t));
16520         new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
16521         new_sdinfo.satadrv_type = sdinfo->satadrv_type;
16522         if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
16523                 /*
16524                  * Cannot get device identification - caller may retry later
16525                  */
16526                 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16527                     "%s fetch device identify data\n", finfo);
16528                 return (SATA_FAILURE);
16529         }
16530         finfox = (restore != 0) ? " restore device features" :
16531             " initialize device features\n";
16532 
16533         switch (sdinfo->satadrv_type) {
16534         case SATA_DTYPE_ATADISK:
16535                 /* Arbitrarily set UDMA mode */
16536                 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
16537                     SATA_SUCCESS) {
16538                         SATA_LOG_D((sata_hba_inst, CE_WARN,
16539                             "%s set UDMA mode\n", finfo));
16540                         return (SATA_FAILURE);
16541                 }
16542                 break;
16543         case SATA_DTYPE_ATAPICD:
16544         case SATA_DTYPE_ATAPITAPE:
16545         case SATA_DTYPE_ATAPIDISK:
16546                 /*  Set Removable Media Status Notification, if necessary */
16547                 if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) &&
16548                     restore != 0) {
16549                         if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
16550                             (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))||
16551                             ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
16552                             SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) {
16553                                 /* Current setting does not match saved one */
16554                                 if (sata_set_rmsn(sata_hba_inst, sdinfo,
16555                                     sdinfo->satadrv_settings &
16556                                     SATA_DEV_RMSN) != SATA_SUCCESS)
16557                                         rval = SATA_FAILURE;
16558                         }
16559                 }
16560                 /*
16561                  * We have to set Multiword DMA or UDMA, if it is supported, as
16562                  * we want to use DMA transfer mode whenever possible.
16563                  * Some devices require explicit setting of the DMA mode.
16564                  */
16565                 if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
16566                         /* Set highest supported DMA mode */
16567                         if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
16568                             SATA_SUCCESS) {
16569                                 SATA_LOG_D((sata_hba_inst, CE_WARN,
16570                                     "%s set UDMA mode\n", finfo));
16571                                 rval = SATA_FAILURE;
16572                         }
16573                 }
16574                 break;
16575         }
16576 
16577         if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) &&
16578             !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
16579                 /*
16580                  * neither READ AHEAD nor WRITE CACHE is supported
16581                  * - do nothing
16582                  */
16583                 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16584                     "settable features not supported\n", NULL);
16585                 goto update_sdinfo;
16586         }
16587 
16588         if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) &&
16589             (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
16590             (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) &&
16591             (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
16592                 /*
16593                  * both READ AHEAD and WRITE CACHE are enabled
16594                  * - Nothing to do
16595                  */
16596                 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16597                     "no device features to set\n", NULL);
16598                 goto update_sdinfo;
16599         }
16600 
16601         cache_op = 0;
16602 
16603         if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) {
16604                 if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
16605                     !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
16606                         /* Enable read ahead / read cache */
16607                         cache_op = SATAC_SF_ENABLE_READ_AHEAD;
16608                         SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16609                             "enabling read cache\n", NULL);
16610                 } else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
16611                     SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
16612                         /* Disable read ahead  / read cache */
16613                         cache_op = SATAC_SF_DISABLE_READ_AHEAD;
16614                         SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16615                             "disabling read cache\n", NULL);
16616                 }
16617 
16618                 if (cache_op != 0) {
16619                         /* Try to set read cache mode */
16620                         rval_set = sata_set_cache_mode(sata_hba_inst,
16621                             &new_sdinfo, cache_op);
16622                         if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
16623                                 rval = rval_set;
16624                 }
16625         }
16626 
16627         cache_op = 0;
16628 
16629         if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
16630                 if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
16631                     !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
16632                         /* Enable write cache */
16633                         cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
16634                         SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16635                             "enabling write cache\n", NULL);
16636                 } else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
16637                     SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
16638                         /* Disable write cache */
16639                         cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
16640                         SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16641                             "disabling write cache\n", NULL);
16642                 }
16643 
16644                 if (cache_op != 0) {
16645                         /* Try to set write cache mode */
16646                         rval_set = sata_set_cache_mode(sata_hba_inst,
16647                             &new_sdinfo, cache_op);
16648                         if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
16649                                 rval = rval_set;
16650                 }
16651         }
16652         if (rval != SATA_SUCCESS)
16653                 SATA_LOG_D((sata_hba_inst, CE_WARN,
16654                     "%s %s", finfo, finfox));
16655 
16656 update_sdinfo:
16657         /*
16658          * We need to fetch Device Identify data again
16659          */
16660         if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
16661                 /*
16662                  * Cannot get device identification - retry later
16663                  */
16664                 SATA_LOG_D((sata_hba_inst, CE_WARN,
16665                     "%s re-fetch device identify data\n", finfo));
16666                 rval = SATA_FAILURE;
16667         }
16668         /* Copy device sata info. */
16669         sdinfo->satadrv_id = new_sdinfo.satadrv_id;
16670 
16671         return (rval);
16672 }
16673 
16674 
16675 /*
16676  *
16677  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
16678  * unable to determine.
16679  *
16680  * Cannot be called in an interrupt context.
16681  *
16682  * Called by sata_build_lsense_page_2f()
16683  */
16684 
16685 static int
16686 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
16687     sata_drive_info_t *sdinfo)
16688 {
16689         sata_pkt_t *spkt;
16690         sata_cmd_t *scmd;
16691         sata_pkt_txlate_t *spx;
16692         int rval;
16693 
16694         spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16695         spx->txlt_sata_hba_inst = sata_hba_inst;
16696         spx->txlt_scsi_pkt = NULL;           /* No scsi pkt involved */
16697         spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16698         if (spkt == NULL) {
16699                 kmem_free(spx, sizeof (sata_pkt_txlate_t));
16700                 return (-1);
16701         }
16702         /* address is needed now */
16703         spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16704 
16705 
16706         /* Fill sata_pkt */
16707         spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16708         spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16709         /* Synchronous mode, no callback */
16710         spkt->satapkt_comp = NULL;
16711         /* Timeout 30s */
16712         spkt->satapkt_time = sata_default_pkt_time;
16713 
16714         scmd = &spkt->satapkt_cmd;
16715         scmd->satacmd_flags.sata_special_regs = B_TRUE;
16716         scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
16717 
16718         /* Set up which registers need to be returned */
16719         scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
16720         scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
16721 
16722         /* Build SMART_RETURN_STATUS cmd in the sata_pkt */
16723         scmd->satacmd_addr_type = 0;         /* N/A */
16724         scmd->satacmd_sec_count_lsb = 0;     /* N/A */
16725         scmd->satacmd_lba_low_lsb = 0;               /* N/A */
16726         scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16727         scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16728         scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
16729         scmd->satacmd_device_reg = 0;                /* Always device 0 */
16730         scmd->satacmd_cmd_reg = SATAC_SMART;
16731         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16732             sdinfo->satadrv_addr.cport)));
16733 
16734 
16735         /* Send pkt to SATA HBA driver */
16736         if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16737             SATA_TRAN_ACCEPTED ||
16738             spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16739                 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16740                     sdinfo->satadrv_addr.cport)));
16741                 /*
16742                  * Whoops, no SMART RETURN STATUS
16743                  */
16744                 rval = -1;
16745         } else {
16746                 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16747                     sdinfo->satadrv_addr.cport)));
16748                 if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
16749                         rval = -1;
16750                         goto fail;
16751                 }
16752                 if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
16753                         rval = -1;
16754                         goto fail;
16755                 }
16756                 if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
16757                     (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
16758                         rval = 0;
16759                 else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
16760                     (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
16761                         rval = 1;
16762                 else {
16763                         rval = -1;
16764                         goto fail;
16765                 }
16766         }
16767 fail:
16768         /* Free allocated resources */
16769         sata_pkt_free(spx);
16770         kmem_free(spx, sizeof (sata_pkt_txlate_t));
16771 
16772         return (rval);
16773 }
16774 
16775 /*
16776  *
16777  * Returns 0 if succeeded, -1 otherwise
16778  *
16779  * Cannot be called in an interrupt context.
16780  *
16781  */
16782 static int
16783 sata_fetch_smart_data(
16784         sata_hba_inst_t *sata_hba_inst,
16785         sata_drive_info_t *sdinfo,
16786         struct smart_data *smart_data)
16787 {
16788         sata_pkt_t *spkt;
16789         sata_cmd_t *scmd;
16790         sata_pkt_txlate_t *spx;
16791         int rval;
16792         dev_info_t *dip = SATA_DIP(sata_hba_inst);
16793 
16794 #if ! defined(lint)
16795         ASSERT(sizeof (struct smart_data) == 512);
16796 #endif
16797 
16798         spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16799         spx->txlt_sata_hba_inst = sata_hba_inst;
16800         spx->txlt_scsi_pkt = NULL;           /* No scsi pkt involved */
16801         spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16802         if (spkt == NULL) {
16803                 kmem_free(spx, sizeof (sata_pkt_txlate_t));
16804                 return (-1);
16805         }
16806         /* address is needed now */
16807         spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16808 
16809 
16810         /* Fill sata_pkt */
16811         spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16812         spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16813         /* Synchronous mode, no callback */
16814         spkt->satapkt_comp = NULL;
16815         /* Timeout 30s */
16816         spkt->satapkt_time = sata_default_pkt_time;
16817 
16818         scmd = &spkt->satapkt_cmd;
16819         scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16820 
16821         /*
16822          * Allocate buffer for SMART data
16823          */
16824         scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16825             sizeof (struct smart_data));
16826         if (scmd->satacmd_bp == NULL) {
16827                 sata_pkt_free(spx);
16828                 kmem_free(spx, sizeof (sata_pkt_txlate_t));
16829                 SATA_LOG_D((sata_hba_inst, CE_WARN,
16830                     "sata_fetch_smart_data: "
16831                     "cannot allocate buffer"));
16832                 return (-1);
16833         }
16834 
16835 
16836         /* Build SMART_READ_DATA cmd in the sata_pkt */
16837         scmd->satacmd_addr_type = 0;         /* N/A */
16838         scmd->satacmd_sec_count_lsb = 0;     /* N/A */
16839         scmd->satacmd_lba_low_lsb = 0;               /* N/A */
16840         scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16841         scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16842         scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
16843         scmd->satacmd_device_reg = 0;                /* Always device 0 */
16844         scmd->satacmd_cmd_reg = SATAC_SMART;
16845         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16846             sdinfo->satadrv_addr.cport)));
16847 
16848         /* Send pkt to SATA HBA driver */
16849         if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16850             SATA_TRAN_ACCEPTED ||
16851             spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16852                 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16853                     sdinfo->satadrv_addr.cport)));
16854                 /*
16855                  * Whoops, no SMART DATA available
16856                  */
16857                 rval = -1;
16858                 goto fail;
16859         } else {
16860                 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16861                     sdinfo->satadrv_addr.cport)));
16862                 if (spx->txlt_buf_dma_handle != NULL) {
16863                         rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
16864                             DDI_DMA_SYNC_FORKERNEL);
16865                         ASSERT(rval == DDI_SUCCESS);
16866                         if (sata_check_for_dma_error(dip, spx)) {
16867                                 ddi_fm_service_impact(dip,
16868                                     DDI_SERVICE_UNAFFECTED);
16869                                 rval = -1;
16870                                 goto fail;
16871                         }
16872                 }
16873                 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
16874                     sizeof (struct smart_data));
16875         }
16876 
16877 fail:
16878         /* Free allocated resources */
16879         sata_free_local_buffer(spx);
16880         sata_pkt_free(spx);
16881         kmem_free(spx, sizeof (sata_pkt_txlate_t));
16882 
16883         return (rval);
16884 }
16885 
16886 /*
16887  * Used by LOG SENSE page 0x10
16888  * Reads (in synchronous mode) the self test log data using Read Log Ext cmd.
16889  * Note: cannot be called in the interrupt context.
16890  *
16891  * return 0 for success, -1 otherwise
16892  *
16893  */
16894 static int
16895 sata_ext_smart_selftest_read_log(
16896         sata_hba_inst_t *sata_hba_inst,
16897         sata_drive_info_t *sdinfo,
16898         struct smart_ext_selftest_log *ext_selftest_log,
16899         uint16_t block_num)
16900 {
16901         sata_pkt_txlate_t *spx;
16902         sata_pkt_t *spkt;
16903         sata_cmd_t *scmd;
16904         int rval;
16905         dev_info_t *dip = SATA_DIP(sata_hba_inst);
16906 
16907 #if ! defined(lint)
16908         ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
16909 #endif
16910 
16911         spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16912         spx->txlt_sata_hba_inst = sata_hba_inst;
16913         spx->txlt_scsi_pkt = NULL;           /* No scsi pkt involved */
16914         spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16915         if (spkt == NULL) {
16916                 kmem_free(spx, sizeof (sata_pkt_txlate_t));
16917                 return (-1);
16918         }
16919         /* address is needed now */
16920         spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16921 
16922 
16923         /* Fill sata_pkt */
16924         spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16925         spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16926         /* Synchronous mode, no callback */
16927         spkt->satapkt_comp = NULL;
16928         /* Timeout 30s */
16929         spkt->satapkt_time = sata_default_pkt_time;
16930 
16931         scmd = &spkt->satapkt_cmd;
16932         scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16933 
16934         /*
16935          * Allocate buffer for SMART extended self-test log
16936          */
16937         scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16938             sizeof (struct smart_ext_selftest_log));
16939         if (scmd->satacmd_bp == NULL) {
16940                 sata_pkt_free(spx);
16941                 kmem_free(spx, sizeof (sata_pkt_txlate_t));
16942                 SATA_LOG_D((sata_hba_inst, CE_WARN,
16943                     "sata_ext_smart_selftest_log: "
16944                     "cannot allocate buffer"));
16945                 return (-1);
16946         }
16947 
16948         /* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
16949         scmd->satacmd_addr_type = ATA_ADDR_LBA48;
16950         scmd->satacmd_sec_count_lsb = 1;     /* One sector of selftest log */
16951         scmd->satacmd_sec_count_msb = 0;     /* One sector of selftest log */
16952         scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
16953         scmd->satacmd_lba_low_msb = 0;
16954         scmd->satacmd_lba_mid_lsb = block_num & 0xff;
16955         scmd->satacmd_lba_mid_msb = block_num >> 8;
16956         scmd->satacmd_device_reg = 0;                /* Always device 0 */
16957         scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
16958 
16959         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16960             sdinfo->satadrv_addr.cport)));
16961 
16962         /* Send pkt to SATA HBA driver */
16963         if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16964             SATA_TRAN_ACCEPTED ||
16965             spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16966                 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16967                     sdinfo->satadrv_addr.cport)));
16968 
16969                 /*
16970                  * Whoops, no SMART selftest log info available
16971                  */
16972                 rval = -1;
16973                 goto fail;
16974         } else {
16975                 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16976                     sdinfo->satadrv_addr.cport)));
16977 
16978                 if (spx->txlt_buf_dma_handle != NULL) {
16979                         rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
16980                             DDI_DMA_SYNC_FORKERNEL);
16981                         ASSERT(rval == DDI_SUCCESS);
16982                         if (sata_check_for_dma_error(dip, spx)) {
16983                                 ddi_fm_service_impact(dip,
16984                                     DDI_SERVICE_UNAFFECTED);
16985                                 rval = -1;
16986                                 goto fail;
16987                         }
16988                 }
16989                 bcopy(scmd->satacmd_bp->b_un.b_addr,
16990                     (uint8_t *)ext_selftest_log,
16991                     sizeof (struct smart_ext_selftest_log));
16992                 rval = 0;
16993         }
16994 
16995 fail:
16996         /* Free allocated resources */
16997         sata_free_local_buffer(spx);
16998         sata_pkt_free(spx);
16999         kmem_free(spx, sizeof (sata_pkt_txlate_t));
17000 
17001         return (rval);
17002 }
17003 
17004 /*
17005  * Returns 0 for success, -1 otherwise
17006  *
17007  * SMART self-test log data is returned in buffer pointed to by selftest_log
17008  */
17009 static int
17010 sata_smart_selftest_log(
17011         sata_hba_inst_t *sata_hba_inst,
17012         sata_drive_info_t *sdinfo,
17013         struct smart_selftest_log *selftest_log)
17014 {
17015         sata_pkt_t *spkt;
17016         sata_cmd_t *scmd;
17017         sata_pkt_txlate_t *spx;
17018         int rval;
17019         dev_info_t *dip = SATA_DIP(sata_hba_inst);
17020 
17021 #if ! defined(lint)
17022         ASSERT(sizeof (struct smart_selftest_log) == 512);
17023 #endif
17024 
17025         spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17026         spx->txlt_sata_hba_inst = sata_hba_inst;
17027         spx->txlt_scsi_pkt = NULL;           /* No scsi pkt involved */
17028         spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17029         if (spkt == NULL) {
17030                 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17031                 return (-1);
17032         }
17033         /* address is needed now */
17034         spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17035 
17036 
17037         /* Fill sata_pkt */
17038         spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17039         spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17040         /* Synchronous mode, no callback */
17041         spkt->satapkt_comp = NULL;
17042         /* Timeout 30s */
17043         spkt->satapkt_time = sata_default_pkt_time;
17044 
17045         scmd = &spkt->satapkt_cmd;
17046         scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17047 
17048         /*
17049          * Allocate buffer for SMART SELFTEST LOG
17050          */
17051         scmd->satacmd_bp = sata_alloc_local_buffer(spx,
17052             sizeof (struct smart_selftest_log));
17053         if (scmd->satacmd_bp == NULL) {
17054                 sata_pkt_free(spx);
17055                 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17056                 SATA_LOG_D((sata_hba_inst, CE_WARN,
17057                     "sata_smart_selftest_log: "
17058                     "cannot allocate buffer"));
17059                 return (-1);
17060         }
17061 
17062         /* Build SMART_READ_LOG cmd in the sata_pkt */
17063         scmd->satacmd_addr_type = 0;         /* N/A */
17064         scmd->satacmd_sec_count_lsb = 1;     /* One sector of SMART log */
17065         scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
17066         scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17067         scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17068         scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17069         scmd->satacmd_device_reg = 0;                /* Always device 0 */
17070         scmd->satacmd_cmd_reg = SATAC_SMART;
17071         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17072             sdinfo->satadrv_addr.cport)));
17073 
17074         /* Send pkt to SATA HBA driver */
17075         if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17076             SATA_TRAN_ACCEPTED ||
17077             spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17078                 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17079                     sdinfo->satadrv_addr.cport)));
17080                 /*
17081                  * Whoops, no SMART DATA available
17082                  */
17083                 rval = -1;
17084                 goto fail;
17085         } else {
17086                 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17087                     sdinfo->satadrv_addr.cport)));
17088                 if (spx->txlt_buf_dma_handle != NULL) {
17089                         rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17090                             DDI_DMA_SYNC_FORKERNEL);
17091                         ASSERT(rval == DDI_SUCCESS);
17092                         if (sata_check_for_dma_error(dip, spx)) {
17093                                 ddi_fm_service_impact(dip,
17094                                     DDI_SERVICE_UNAFFECTED);
17095                                 rval = -1;
17096                                 goto fail;
17097                         }
17098                 }
17099                 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
17100                     sizeof (struct smart_selftest_log));
17101                 rval = 0;
17102         }
17103 
17104 fail:
17105         /* Free allocated resources */
17106         sata_free_local_buffer(spx);
17107         sata_pkt_free(spx);
17108         kmem_free(spx, sizeof (sata_pkt_txlate_t));
17109 
17110         return (rval);
17111 }
17112 
17113 
17114 /*
17115  * Returns 0 for success, -1 otherwise
17116  *
17117  * SMART READ LOG data is returned in buffer pointed to by smart_log
17118  */
17119 static int
17120 sata_smart_read_log(
17121         sata_hba_inst_t *sata_hba_inst,
17122         sata_drive_info_t *sdinfo,
17123         uint8_t *smart_log,             /* where the data should be returned */
17124         uint8_t which_log,              /* which log should be returned */
17125         uint8_t log_size)               /* # of 512 bytes in log */
17126 {
17127         sata_pkt_t *spkt;
17128         sata_cmd_t *scmd;
17129         sata_pkt_txlate_t *spx;
17130         int rval;
17131         dev_info_t *dip = SATA_DIP(sata_hba_inst);
17132 
17133         spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17134         spx->txlt_sata_hba_inst = sata_hba_inst;
17135         spx->txlt_scsi_pkt = NULL;           /* No scsi pkt involved */
17136         spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17137         if (spkt == NULL) {
17138                 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17139                 return (-1);
17140         }
17141         /* address is needed now */
17142         spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17143 
17144 
17145         /* Fill sata_pkt */
17146         spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17147         spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17148         /* Synchronous mode, no callback */
17149         spkt->satapkt_comp = NULL;
17150         /* Timeout 30s */
17151         spkt->satapkt_time = sata_default_pkt_time;
17152 
17153         scmd = &spkt->satapkt_cmd;
17154         scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17155 
17156         /*
17157          * Allocate buffer for SMART READ LOG
17158          */
17159         scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
17160         if (scmd->satacmd_bp == NULL) {
17161                 sata_pkt_free(spx);
17162                 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17163                 SATA_LOG_D((sata_hba_inst, CE_WARN,
17164                     "sata_smart_read_log: " "cannot allocate buffer"));
17165                 return (-1);
17166         }
17167 
17168         /* Build SMART_READ_LOG cmd in the sata_pkt */
17169         scmd->satacmd_addr_type = 0;         /* N/A */
17170         scmd->satacmd_sec_count_lsb = log_size;      /* what the caller asked for */
17171         scmd->satacmd_lba_low_lsb = which_log;       /* which log page */
17172         scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17173         scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17174         scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17175         scmd->satacmd_device_reg = 0;                /* Always device 0 */
17176         scmd->satacmd_cmd_reg = SATAC_SMART;
17177 
17178         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17179             sdinfo->satadrv_addr.cport)));
17180 
17181         /* Send pkt to SATA HBA driver */
17182         if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17183             SATA_TRAN_ACCEPTED ||
17184             spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17185                 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17186                     sdinfo->satadrv_addr.cport)));
17187 
17188                 /*
17189                  * Whoops, no SMART DATA available
17190                  */
17191                 rval = -1;
17192                 goto fail;
17193         } else {
17194                 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17195                     sdinfo->satadrv_addr.cport)));
17196 
17197                 if (spx->txlt_buf_dma_handle != NULL) {
17198                         rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17199                             DDI_DMA_SYNC_FORKERNEL);
17200                         ASSERT(rval == DDI_SUCCESS);
17201                         if (sata_check_for_dma_error(dip, spx)) {
17202                                 ddi_fm_service_impact(dip,
17203                                     DDI_SERVICE_UNAFFECTED);
17204                                 rval = -1;
17205                                 goto fail;
17206                         }
17207                 }
17208                 bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
17209                 rval = 0;
17210         }
17211 
17212 fail:
17213         /* Free allocated resources */
17214         sata_free_local_buffer(spx);
17215         sata_pkt_free(spx);
17216         kmem_free(spx, sizeof (sata_pkt_txlate_t));
17217 
17218         return (rval);
17219 }
17220 
17221 /*
17222  * Used by LOG SENSE page 0x10
17223  *
17224  * return 0 for success, -1 otherwise
17225  *
17226  */
17227 static int
17228 sata_read_log_ext_directory(
17229         sata_hba_inst_t *sata_hba_inst,
17230         sata_drive_info_t *sdinfo,
17231         struct read_log_ext_directory *logdir)
17232 {
17233         sata_pkt_txlate_t *spx;
17234         sata_pkt_t *spkt;
17235         sata_cmd_t *scmd;
17236         int rval;
17237         dev_info_t *dip = SATA_DIP(sata_hba_inst);
17238 
17239 #if ! defined(lint)
17240         ASSERT(sizeof (struct read_log_ext_directory) == 512);
17241 #endif
17242 
17243         spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17244         spx->txlt_sata_hba_inst = sata_hba_inst;
17245         spx->txlt_scsi_pkt = NULL;           /* No scsi pkt involved */
17246         spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17247         if (spkt == NULL) {
17248                 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17249                 return (-1);
17250         }
17251 
17252         /* Fill sata_pkt */
17253         spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17254         spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17255         /* Synchronous mode, no callback */
17256         spkt->satapkt_comp = NULL;
17257         /* Timeout 30s */
17258         spkt->satapkt_time = sata_default_pkt_time;
17259 
17260         scmd = &spkt->satapkt_cmd;
17261         scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17262 
17263         /*
17264          * Allocate buffer for SMART READ LOG EXTENDED command
17265          */
17266         scmd->satacmd_bp = sata_alloc_local_buffer(spx,
17267             sizeof (struct read_log_ext_directory));
17268         if (scmd->satacmd_bp == NULL) {
17269                 sata_pkt_free(spx);
17270                 kmem_free(spx, sizeof (sata_pkt_txlate_t));
17271                 SATA_LOG_D((sata_hba_inst, CE_WARN,
17272                     "sata_read_log_ext_directory: "
17273                     "cannot allocate buffer"));
17274                 return (-1);
17275         }
17276 
17277         /* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
17278         scmd->satacmd_addr_type = ATA_ADDR_LBA48;
17279         scmd->satacmd_sec_count_lsb = 1;     /* One sector of directory */
17280         scmd->satacmd_sec_count_msb = 0;     /* One sector of directory */
17281         scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
17282         scmd->satacmd_lba_low_msb = 0;
17283         scmd->satacmd_lba_mid_lsb = 0;
17284         scmd->satacmd_lba_mid_msb = 0;
17285         scmd->satacmd_device_reg = 0;                /* Always device 0 */
17286         scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
17287 
17288         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17289             sdinfo->satadrv_addr.cport)));
17290 
17291         /* Send pkt to SATA HBA driver */
17292         if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17293             SATA_TRAN_ACCEPTED ||
17294             spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17295                 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17296                     sdinfo->satadrv_addr.cport)));
17297                 /*
17298                  * Whoops, no SMART selftest log info available
17299                  */
17300                 rval = -1;
17301                 goto fail;
17302         } else {
17303                 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17304                     sdinfo->satadrv_addr.cport)));
17305                 if (spx->txlt_buf_dma_handle != NULL) {
17306                         rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17307                             DDI_DMA_SYNC_FORKERNEL);
17308                         ASSERT(rval == DDI_SUCCESS);
17309                         if (sata_check_for_dma_error(dip, spx)) {
17310                                 ddi_fm_service_impact(dip,
17311                                     DDI_SERVICE_UNAFFECTED);
17312                                 rval = -1;
17313                                 goto fail;
17314                         }
17315                 }
17316                 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
17317                     sizeof (struct read_log_ext_directory));
17318                 rval = 0;
17319         }
17320 
17321 fail:
17322         /* Free allocated resources */
17323         sata_free_local_buffer(spx);
17324         sata_pkt_free(spx);
17325         kmem_free(spx, sizeof (sata_pkt_txlate_t));
17326 
17327         return (rval);
17328 }
17329 
17330 /*
17331  * Set up error retrieval sata command for NCQ command error data
17332  * recovery.
17333  *
17334  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
17335  * returns SATA_FAILURE otherwise.
17336  */
17337 static int
17338 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
17339 {
17340 #ifndef __lock_lint
17341         _NOTE(ARGUNUSED(sdinfo))
17342 #endif
17343 
17344         sata_pkt_t *spkt = spx->txlt_sata_pkt;
17345         sata_cmd_t *scmd;
17346         struct buf *bp;
17347 
17348         /* Operation modes are up to the caller */
17349         spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17350 
17351         /* Synchronous mode, no callback - may be changed by the caller */
17352         spkt->satapkt_comp = NULL;
17353         spkt->satapkt_time = sata_default_pkt_time;
17354 
17355         scmd = &spkt->satapkt_cmd;
17356         bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
17357         scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
17358 
17359         /*
17360          * Allocate dma_able buffer error data.
17361          * Buffer allocation will take care of buffer alignment and other DMA
17362          * attributes.
17363          */
17364         bp = sata_alloc_local_buffer(spx,
17365             sizeof (struct sata_ncq_error_recovery_page));
17366         if (bp == NULL)
17367                 return (SATA_FAILURE);
17368 
17369         bp_mapin(bp); /* make data buffer accessible */
17370         scmd->satacmd_bp = bp;
17371 
17372         /*
17373          * Set-up pointer to the buffer handle, so HBA can sync buffer
17374          * before accessing it. Handle is in usual place in translate struct.
17375          */
17376         scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
17377 
17378         ASSERT(scmd->satacmd_num_dma_cookies != 0);
17379         ASSERT(scmd->satacmd_dma_cookie_list != NULL);
17380 
17381         return (SATA_SUCCESS);
17382 }
17383 
17384 /*
17385  * sata_xlate_errors() is used to translate (S)ATA error
17386  * information to SCSI information returned in the SCSI
17387  * packet.
17388  */
17389 static void
17390 sata_xlate_errors(sata_pkt_txlate_t *spx)
17391 {
17392         struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
17393         struct scsi_extended_sense *sense;
17394 
17395         scsipkt->pkt_reason = CMD_INCOMPLETE;
17396         *scsipkt->pkt_scbp = STATUS_CHECK;
17397         sense = sata_arq_sense(spx);
17398 
17399         switch (spx->txlt_sata_pkt->satapkt_reason) {
17400         case SATA_PKT_PORT_ERROR:
17401                 /*
17402                  * We have no device data. Assume no data transfered.
17403                  */
17404                 sense->es_key = KEY_HARDWARE_ERROR;
17405                 break;
17406 
17407         case SATA_PKT_DEV_ERROR:
17408                 if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
17409                     SATA_STATUS_ERR) {
17410                         /*
17411                          * determine dev error reason from error
17412                          * reg content
17413                          */
17414                         sata_decode_device_error(spx, sense);
17415                         break;
17416                 }
17417                 /* No extended sense key - no info available */
17418                 break;
17419 
17420         case SATA_PKT_TIMEOUT:
17421                 scsipkt->pkt_reason = CMD_TIMEOUT;
17422                 scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
17423                 /* No extended sense key */
17424                 break;
17425 
17426         case SATA_PKT_ABORTED:
17427                 scsipkt->pkt_reason = CMD_ABORTED;
17428                 scsipkt->pkt_statistics |= STAT_ABORTED;
17429                 /* No extended sense key */
17430                 break;
17431 
17432         case SATA_PKT_RESET:
17433                 /*
17434                  * pkt aborted either by an explicit reset request from
17435                  * a host, or due to error recovery
17436                  */
17437                 scsipkt->pkt_reason = CMD_RESET;
17438                 scsipkt->pkt_statistics |= STAT_DEV_RESET;
17439                 break;
17440 
17441         default:
17442                 scsipkt->pkt_reason = CMD_TRAN_ERR;
17443                 break;
17444         }
17445 }
17446 
17447 
17448 
17449 
17450 /*
17451  * Log sata message
17452  * dev pathname msg line preceeds the logged message.
17453  */
17454 
17455 static  void
17456 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
17457 {
17458         char pathname[128];
17459         dev_info_t *dip = NULL;
17460         va_list ap;
17461 
17462         mutex_enter(&sata_log_mutex);
17463 
17464         va_start(ap, fmt);
17465         (void) vsprintf(sata_log_buf, fmt, ap);
17466         va_end(ap);
17467 
17468         if (sata_hba_inst != NULL) {
17469                 dip = SATA_DIP(sata_hba_inst);
17470                 (void) ddi_pathname(dip, pathname);
17471         } else {
17472                 pathname[0] = 0;
17473         }
17474         if (level == CE_CONT) {
17475                 if (sata_debug_flags == 0)
17476                         cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
17477                 else
17478                         cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
17479         } else {
17480                 if (level != CE_NOTE) {
17481                         cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
17482                 } else if (sata_msg) {
17483                         cmn_err(level, "%s:\n %s", pathname,
17484                             sata_log_buf);
17485                 }
17486         }
17487 
17488         /* sata trace debug */
17489         sata_trace_debug(dip, sata_log_buf);
17490 
17491         mutex_exit(&sata_log_mutex);
17492 }
17493 
17494 
17495 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
17496 
17497 /*
17498  * Start or terminate the thread, depending on flag arg and current state
17499  */
17500 static void
17501 sata_event_thread_control(int startstop)
17502 {
17503         static  int sata_event_thread_terminating = 0;
17504         static  int sata_event_thread_starting = 0;
17505         int i;
17506 
17507         mutex_enter(&sata_event_mutex);
17508 
17509         if (startstop == 0 && (sata_event_thread_starting == 1 ||
17510             sata_event_thread_terminating == 1)) {
17511                 mutex_exit(&sata_event_mutex);
17512                 return;
17513         }
17514         if (startstop == 1 && sata_event_thread_starting == 1) {
17515                 mutex_exit(&sata_event_mutex);
17516                 return;
17517         }
17518         if (startstop == 1 && sata_event_thread_terminating == 1) {
17519                 sata_event_thread_starting = 1;
17520                 /* wait til terminate operation completes */
17521                 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17522                 while (sata_event_thread_terminating == 1) {
17523                         if (i-- <= 0) {
17524                                 sata_event_thread_starting = 0;
17525                                 mutex_exit(&sata_event_mutex);
17526 #ifdef SATA_DEBUG
17527                                 cmn_err(CE_WARN, "sata_event_thread_control: "
17528                                     "timeout waiting for thread to terminate");
17529 #endif
17530                                 return;
17531                         }
17532                         mutex_exit(&sata_event_mutex);
17533                         delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17534                         mutex_enter(&sata_event_mutex);
17535                 }
17536         }
17537         if (startstop == 1) {
17538                 if (sata_event_thread == NULL) {
17539                         sata_event_thread = thread_create(NULL, 0,
17540                             (void (*)())sata_event_daemon,
17541                             &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
17542                 }
17543                 sata_event_thread_starting = 0;
17544                 mutex_exit(&sata_event_mutex);
17545                 return;
17546         }
17547 
17548         /*
17549          * If we got here, thread may need to be terminated
17550          */
17551         if (sata_event_thread != NULL) {
17552                 int i;
17553                 /* Signal event thread to go away */
17554                 sata_event_thread_terminating = 1;
17555                 sata_event_thread_terminate = 1;
17556                 cv_signal(&sata_event_cv);
17557                 /*
17558                  * Wait til daemon terminates.
17559                  */
17560                 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17561                 while (sata_event_thread_terminate == 1) {
17562                         mutex_exit(&sata_event_mutex);
17563                         if (i-- <= 0) {
17564                                 /* Daemon did not go away !!! */
17565 #ifdef SATA_DEBUG
17566                                 cmn_err(CE_WARN, "sata_event_thread_control: "
17567                                     "cannot terminate event daemon thread");
17568 #endif
17569                                 mutex_enter(&sata_event_mutex);
17570                                 break;
17571                         }
17572                         delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17573                         mutex_enter(&sata_event_mutex);
17574                 }
17575                 sata_event_thread_terminating = 0;
17576         }
17577         ASSERT(sata_event_thread_terminating == 0);
17578         ASSERT(sata_event_thread_starting == 0);
17579         mutex_exit(&sata_event_mutex);
17580 }
17581 
17582 
17583 /*
17584  * SATA HBA event notification function.
17585  * Events reported by SATA HBA drivers per HBA instance relate to a change in
17586  * a port and/or device state or a controller itself.
17587  * Events for different addresses/addr types cannot be combined.
17588  * A warning message is generated for each event type.
17589  * Events are not processed by this function, so only the
17590  * event flag(s)is set for an affected entity and the event thread is
17591  * waken up. Event daemon thread processes all events.
17592  *
17593  * NOTE: Since more than one event may be reported at the same time, one
17594  * cannot determine a sequence of events when opposite event are reported, eg.
17595  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
17596  * is taking precedence over reported events, i.e. may cause ignoring some
17597  * events.
17598  */
17599 #define SATA_EVENT_MAX_MSG_LENGTH       79
17600 
17601 void
17602 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
17603 {
17604         sata_hba_inst_t *sata_hba_inst = NULL;
17605         sata_address_t *saddr;
17606         sata_pmult_info_t *pmultinfo;
17607         sata_drive_info_t *sdinfo;
17608         sata_port_stats_t *pstats;
17609         sata_cport_info_t *cportinfo;
17610         sata_pmport_info_t *pmportinfo;
17611         int cport, pmport;
17612         char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
17613         char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
17614         char *lcp;
17615         static char *err_msg_evnt_1 =
17616             "sata_hba_event_notify: invalid port event 0x%x ";
17617         static char *err_msg_evnt_2 =
17618             "sata_hba_event_notify: invalid device event 0x%x ";
17619         int linkevent;
17620 
17621         /*
17622          * There is a possibility that an event will be generated on HBA
17623          * that has not completed attachment or is detaching. We still want
17624          * to process events until HBA is detached.
17625          */
17626         mutex_enter(&sata_mutex);
17627         for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17628             sata_hba_inst = sata_hba_inst->satahba_next) {
17629                 if (SATA_DIP(sata_hba_inst) == dip)
17630                         if (sata_hba_inst->satahba_attached == 1)
17631                                 break;
17632         }
17633         mutex_exit(&sata_mutex);
17634         if (sata_hba_inst == NULL)
17635                 /* HBA not attached */
17636                 return;
17637 
17638         ASSERT(sata_device != NULL);
17639 
17640         /*
17641          * Validate address before - do not proceed with invalid address.
17642          */
17643         saddr = &sata_device->satadev_addr;
17644         if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
17645                 return;
17646 
17647         cport = saddr->cport;
17648         pmport = saddr->pmport;
17649 
17650         buf1[0] = buf2[0] = '\0';
17651 
17652         /*
17653          * If event relates to port or device, check port state.
17654          * Port has to be initialized, or we cannot accept an event.
17655          */
17656         if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT |
17657             SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT | SATA_ADDR_PMULT)) != 0) {
17658                 mutex_enter(&sata_hba_inst->satahba_mutex);
17659                 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
17660                 mutex_exit(&sata_hba_inst->satahba_mutex);
17661                 if (cportinfo == NULL || cportinfo->cport_state == 0)
17662                         return;
17663         }
17664 
17665         if ((saddr->qual & (SATA_ADDR_PMULT | SATA_ADDR_PMPORT |
17666             SATA_ADDR_DPMPORT)) != 0) {
17667                 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
17668                         SATA_LOG_D((sata_hba_inst, CE_WARN,
17669                             "sata_hba_event_notify: Non-pmult device (0x%x)"
17670                             "is attached to port %d, ignore pmult/pmport "
17671                             "event 0x%x", cportinfo->cport_dev_type,
17672                             cport, event));
17673                         return;
17674                 }
17675 
17676                 mutex_enter(&cportinfo->cport_mutex);
17677                 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
17678                 mutex_exit(&cportinfo->cport_mutex);
17679 
17680                 /*
17681                  * The daemon might be processing attachment of port
17682                  * multiplier, in that case we should ignore events on its
17683                  * sub-devices.
17684                  *
17685                  * NOTE: Only pmult_state is checked in sata_hba_event_notify.
17686                  * The pmport_state is checked by sata daemon.
17687                  */
17688                 if (pmultinfo == NULL ||
17689                     pmultinfo->pmult_state == SATA_STATE_UNKNOWN) {
17690                         SATA_LOG_D((sata_hba_inst, CE_WARN,
17691                             "sata_hba_event_notify: pmult is not"
17692                             "available at port %d:%d, ignore event 0x%x",
17693                             cport, pmport, event));
17694                         return;
17695                 }
17696         }
17697 
17698         if ((saddr->qual &
17699             (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) != 0) {
17700 
17701                 mutex_enter(&cportinfo->cport_mutex);
17702                 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) {
17703                         SATA_LOG_D((sata_hba_inst, CE_WARN,
17704                             "sata_hba_event_notify: invalid/"
17705                             "un-implemented port %d:%d (%d ports), "
17706                             "ignore event 0x%x", cport, pmport,
17707                             SATA_NUM_PMPORTS(sata_hba_inst, cport), event));
17708                         mutex_exit(&cportinfo->cport_mutex);
17709                         return;
17710                 }
17711                 mutex_exit(&cportinfo->cport_mutex);
17712 
17713                 mutex_enter(&sata_hba_inst->satahba_mutex);
17714                 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
17715                     cport, pmport);
17716                 mutex_exit(&sata_hba_inst->satahba_mutex);
17717 
17718                 /* pmport is implemented/valid? */
17719                 if (pmportinfo == NULL) {
17720                         SATA_LOG_D((sata_hba_inst, CE_WARN,
17721                             "sata_hba_event_notify: invalid/"
17722                             "un-implemented port %d:%d, ignore "
17723                             "event 0x%x", cport, pmport, event));
17724                         return;
17725                 }
17726         }
17727 
17728         /*
17729          * Events refer to devices, ports and controllers - each has
17730          * unique address. Events for different addresses cannot be combined.
17731          */
17732         if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
17733 
17734                 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17735 
17736                 /* qualify this event(s) */
17737                 if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
17738                         /* Invalid event for the device port */
17739                         (void) sprintf(buf2, err_msg_evnt_1,
17740                             event & SATA_EVNT_PORT_EVENTS);
17741                         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17742                         goto event_info;
17743                 }
17744                 if (saddr->qual == SATA_ADDR_CPORT) {
17745                         /* Controller's device port event */
17746 
17747                         (SATA_CPORT_INFO(sata_hba_inst, cport))->
17748                             cport_event_flags |=
17749                             event & SATA_EVNT_PORT_EVENTS;
17750                         pstats =
17751                             &(SATA_CPORT_INFO(sata_hba_inst, cport))->
17752                             cport_stats;
17753                 } else {
17754                         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17755                         mutex_enter(&pmportinfo->pmport_mutex);
17756                         /* Port multiplier's device port event */
17757                         (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
17758                             pmport_event_flags |=
17759                             event & SATA_EVNT_PORT_EVENTS;
17760                         pstats =
17761                             &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
17762                             pmport_stats;
17763                         mutex_exit(&pmportinfo->pmport_mutex);
17764                         mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17765                 }
17766 
17767                 /*
17768                  * Add to statistics and log the message. We have to do it
17769                  * here rather than in the event daemon, because there may be
17770                  * multiple events occuring before they are processed.
17771                  */
17772                 linkevent = event &
17773                     (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
17774                 if (linkevent) {
17775                         if (linkevent == (SATA_EVNT_LINK_LOST |
17776                             SATA_EVNT_LINK_ESTABLISHED)) {
17777                                 /* This is likely event combination */
17778                                 (void) strlcat(buf1, "link lost/established, ",
17779                                     SATA_EVENT_MAX_MSG_LENGTH);
17780 
17781                                 if (pstats->link_lost < 0xffffffffffffffffULL)
17782                                         pstats->link_lost++;
17783                                 if (pstats->link_established <
17784                                     0xffffffffffffffffULL)
17785                                         pstats->link_established++;
17786                                 linkevent = 0;
17787                         } else if (linkevent & SATA_EVNT_LINK_LOST) {
17788                                 (void) strlcat(buf1, "link lost, ",
17789                                     SATA_EVENT_MAX_MSG_LENGTH);
17790 
17791                                 if (pstats->link_lost < 0xffffffffffffffffULL)
17792                                         pstats->link_lost++;
17793                         } else {
17794                                 (void) strlcat(buf1, "link established, ",
17795                                     SATA_EVENT_MAX_MSG_LENGTH);
17796                                 if (pstats->link_established <
17797                                     0xffffffffffffffffULL)
17798                                         pstats->link_established++;
17799                         }
17800                 }
17801                 if (event & SATA_EVNT_DEVICE_ATTACHED) {
17802                         (void) strlcat(buf1, "device attached, ",
17803                             SATA_EVENT_MAX_MSG_LENGTH);
17804                         if (pstats->device_attached < 0xffffffffffffffffULL)
17805                                 pstats->device_attached++;
17806                 }
17807                 if (event & SATA_EVNT_DEVICE_DETACHED) {
17808                         (void) strlcat(buf1, "device detached, ",
17809                             SATA_EVENT_MAX_MSG_LENGTH);
17810                         if (pstats->device_detached < 0xffffffffffffffffULL)
17811                                 pstats->device_detached++;
17812                 }
17813                 if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
17814                         SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
17815                             "port %d power level changed", cport);
17816                         if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
17817                                 pstats->port_pwr_changed++;
17818                 }
17819 
17820                 if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
17821                         /* There should be no other events for this address */
17822                         (void) sprintf(buf2, err_msg_evnt_1,
17823                             event & ~SATA_EVNT_PORT_EVENTS);
17824                 }
17825                 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17826 
17827         } else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
17828                 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17829 
17830                 /* qualify this event */
17831                 if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
17832                         /* Invalid event for a device */
17833                         (void) sprintf(buf2, err_msg_evnt_2,
17834                             event & SATA_EVNT_DEVICE_RESET);
17835                         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17836                         goto event_info;
17837                 }
17838                 /* drive event */
17839                 sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
17840                 if (sdinfo != NULL) {
17841                         if (event & SATA_EVNT_DEVICE_RESET) {
17842                                 (void) strlcat(buf1, "device reset, ",
17843                                     SATA_EVENT_MAX_MSG_LENGTH);
17844                                 if (sdinfo->satadrv_stats.drive_reset <
17845                                     0xffffffffffffffffULL)
17846                                         sdinfo->satadrv_stats.drive_reset++;
17847                                 sdinfo->satadrv_event_flags |=
17848                                     SATA_EVNT_DEVICE_RESET;
17849                         }
17850                 }
17851                 if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
17852                         /* Invalid event for a device */
17853                         (void) sprintf(buf2, err_msg_evnt_2,
17854                             event & ~SATA_EVNT_DRIVE_EVENTS);
17855                 }
17856                 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17857         } else if (saddr->qual == SATA_ADDR_PMULT) {
17858                 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17859 
17860                 /* qualify this event */
17861                 if ((event & (SATA_EVNT_DEVICE_RESET |
17862                     SATA_EVNT_PMULT_LINK_CHANGED)) == 0) {
17863                         /* Invalid event for a port multiplier */
17864                         (void) sprintf(buf2, err_msg_evnt_2,
17865                             event & SATA_EVNT_DEVICE_RESET);
17866                         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17867                         goto event_info;
17868                 }
17869 
17870                 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
17871 
17872                 if (event & SATA_EVNT_DEVICE_RESET) {
17873 
17874                         SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
17875                             "[Reset] port-mult on cport %d", cport);
17876                         pmultinfo->pmult_event_flags |=
17877                             SATA_EVNT_DEVICE_RESET;
17878                         (void) strlcat(buf1, "pmult reset, ",
17879                             SATA_EVENT_MAX_MSG_LENGTH);
17880                 }
17881 
17882                 if (event & SATA_EVNT_PMULT_LINK_CHANGED) {
17883 
17884                         SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
17885                             "pmult link changed on cport %d", cport);
17886                         pmultinfo->pmult_event_flags |=
17887                             SATA_EVNT_PMULT_LINK_CHANGED;
17888                         (void) strlcat(buf1, "pmult link changed, ",
17889                             SATA_EVENT_MAX_MSG_LENGTH);
17890                 }
17891                 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17892 
17893         } else {
17894                 if (saddr->qual != SATA_ADDR_NULL) {
17895                         /* Wrong address qualifier */
17896                         SATA_LOG_D((sata_hba_inst, CE_WARN,
17897                             "sata_hba_event_notify: invalid address 0x%x",
17898                             *(uint32_t *)saddr));
17899                         return;
17900                 }
17901                 if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
17902                     (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
17903                         /* Invalid event for the controller */
17904                         SATA_LOG_D((sata_hba_inst, CE_WARN,
17905                             "sata_hba_event_notify: invalid event 0x%x for "
17906                             "controller",
17907                             event & SATA_EVNT_CONTROLLER_EVENTS));
17908                         return;
17909                 }
17910                 buf1[0] = '\0';
17911                 /* This may be a frequent and not interesting event */
17912                 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
17913                     "controller power level changed\n", NULL);
17914 
17915                 mutex_enter(&sata_hba_inst->satahba_mutex);
17916                 if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
17917                     0xffffffffffffffffULL)
17918                         sata_hba_inst->satahba_stats.ctrl_pwr_change++;
17919 
17920                 sata_hba_inst->satahba_event_flags |=
17921                     SATA_EVNT_PWR_LEVEL_CHANGED;
17922                 mutex_exit(&sata_hba_inst->satahba_mutex);
17923         }
17924         /*
17925          * If we got here, there is something to do with this HBA
17926          * instance.
17927          */
17928         mutex_enter(&sata_hba_inst->satahba_mutex);
17929         sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
17930         mutex_exit(&sata_hba_inst->satahba_mutex);
17931         mutex_enter(&sata_mutex);
17932         sata_event_pending |= SATA_EVNT_MAIN;   /* global event indicator */
17933         mutex_exit(&sata_mutex);
17934 
17935         /* Tickle event thread */
17936         mutex_enter(&sata_event_mutex);
17937         if (sata_event_thread_active == 0)
17938                 cv_signal(&sata_event_cv);
17939         mutex_exit(&sata_event_mutex);
17940 
17941 event_info:
17942         if (buf1[0] != '\0') {
17943                 lcp = strrchr(buf1, ',');
17944                 if (lcp != NULL)
17945                         *lcp = '\0';
17946         }
17947         if (saddr->qual == SATA_ADDR_CPORT ||
17948             saddr->qual == SATA_ADDR_DCPORT) {
17949                 if (buf1[0] != '\0') {
17950                         sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
17951                             cport, buf1);
17952                 }
17953                 if (buf2[0] != '\0') {
17954                         sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
17955                             cport, buf2);
17956                 }
17957         } else if (saddr->qual == SATA_ADDR_PMPORT ||
17958             saddr->qual == SATA_ADDR_DPMPORT) {
17959                 if (buf1[0] != '\0') {
17960                         sata_log(sata_hba_inst, CE_NOTE,
17961                             "port %d pmport %d: %s\n", cport, pmport, buf1);
17962                 }
17963                 if (buf2[0] != '\0') {
17964                         sata_log(sata_hba_inst, CE_NOTE,
17965                             "port %d pmport %d: %s\n", cport, pmport, buf2);
17966                 }
17967         }
17968 }
17969 
17970 
17971 /*
17972  * Event processing thread.
17973  * Arg is a pointer to the sata_hba_list pointer.
17974  * It is not really needed, because sata_hba_list is global and static
17975  */
17976 static void
17977 sata_event_daemon(void *arg)
17978 {
17979 #ifndef __lock_lint
17980         _NOTE(ARGUNUSED(arg))
17981 #endif
17982         sata_hba_inst_t *sata_hba_inst;
17983         clock_t delta;
17984 
17985         SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
17986             "SATA event daemon started\n", NULL);
17987 loop:
17988         /*
17989          * Process events here. Walk through all registered HBAs
17990          */
17991         mutex_enter(&sata_mutex);
17992         for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17993             sata_hba_inst = sata_hba_inst->satahba_next) {
17994                 ASSERT(sata_hba_inst != NULL);
17995                 mutex_enter(&sata_hba_inst->satahba_mutex);
17996                 if (sata_hba_inst->satahba_attached == 0 ||
17997                     (sata_hba_inst->satahba_event_flags &
17998                     SATA_EVNT_SKIP) != 0) {
17999                         mutex_exit(&sata_hba_inst->satahba_mutex);
18000                         continue;
18001                 }
18002                 if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
18003                         sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
18004                         mutex_exit(&sata_hba_inst->satahba_mutex);
18005                         mutex_exit(&sata_mutex);
18006                         /* Got the controller with pending event */
18007                         sata_process_controller_events(sata_hba_inst);
18008                         /*
18009                          * Since global mutex was released, there is a
18010                          * possibility that HBA list has changed, so start
18011                          * over from the top. Just processed controller
18012                          * will be passed-over because of the SKIP flag.
18013                          */
18014                         goto loop;
18015                 }
18016                 mutex_exit(&sata_hba_inst->satahba_mutex);
18017         }
18018         /* Clear SKIP flag in all controllers */
18019         for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
18020             sata_hba_inst = sata_hba_inst->satahba_next) {
18021                 mutex_enter(&sata_hba_inst->satahba_mutex);
18022                 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
18023                 mutex_exit(&sata_hba_inst->satahba_mutex);
18024         }
18025         mutex_exit(&sata_mutex);
18026 
18027         SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18028             "SATA EVENT DAEMON suspending itself", NULL);
18029 
18030 #ifdef SATA_DEBUG
18031         if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
18032                 sata_log(sata_hba_inst, CE_WARN,
18033                     "SATA EVENTS PROCESSING DISABLED\n");
18034                 thread_exit(); /* Daemon will not run again */
18035         }
18036 #endif
18037         mutex_enter(&sata_event_mutex);
18038         sata_event_thread_active = 0;
18039         mutex_exit(&sata_event_mutex);
18040         /*
18041          * Go to sleep/suspend itself and wake up either because new event or
18042          * wait timeout. Exit if there is a termination request (driver
18043          * unload).
18044          */
18045         delta = drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
18046         do {
18047                 mutex_enter(&sata_event_mutex);
18048                 (void) cv_reltimedwait(&sata_event_cv, &sata_event_mutex,
18049                     delta, TR_CLOCK_TICK);
18050 
18051                 if (sata_event_thread_active != 0) {
18052                         mutex_exit(&sata_event_mutex);
18053                         continue;
18054                 }
18055 
18056                 /* Check if it is time to go away */
18057                 if (sata_event_thread_terminate == 1) {
18058                         /*
18059                          * It is up to the thread setting above flag to make
18060                          * sure that this thread is not killed prematurely.
18061                          */
18062                         sata_event_thread_terminate = 0;
18063                         sata_event_thread = NULL;
18064                         mutex_exit(&sata_event_mutex);
18065                         SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18066                             "SATA_EVENT_DAEMON_TERMINATING", NULL);
18067                         thread_exit();  { _NOTE(NOT_REACHED) }
18068                 }
18069                 mutex_exit(&sata_event_mutex);
18070         } while (!(sata_event_pending & SATA_EVNT_MAIN));
18071 
18072         mutex_enter(&sata_event_mutex);
18073         sata_event_thread_active = 1;
18074         mutex_exit(&sata_event_mutex);
18075 
18076         mutex_enter(&sata_mutex);
18077         sata_event_pending &= ~SATA_EVNT_MAIN;
18078         mutex_exit(&sata_mutex);
18079 
18080         SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18081             "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
18082 
18083         goto loop;
18084 }
18085 
18086 /*
18087  * Specific HBA instance event processing.
18088  *
18089  * NOTE: At the moment, device event processing is limited to hard disks
18090  * only.
18091  * Port multiplier is supported now.
18092  */
18093 static void
18094 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
18095 {
18096         int ncport;
18097         uint32_t event_flags;
18098         sata_address_t *saddr;
18099         sata_cport_info_t *cportinfo;
18100         sata_pmult_info_t *pmultinfo;
18101 
18102         SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
18103             "Processing controller %d event(s)",
18104             ddi_get_instance(SATA_DIP(sata_hba_inst)));
18105 
18106         mutex_enter(&sata_hba_inst->satahba_mutex);
18107         sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
18108         event_flags = sata_hba_inst->satahba_event_flags;
18109         mutex_exit(&sata_hba_inst->satahba_mutex);
18110         /*
18111          * Process controller power change first
18112          * HERE
18113          */
18114         if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
18115                 sata_process_cntrl_pwr_level_change(sata_hba_inst);
18116 
18117         /*
18118          * Search through ports/devices to identify affected port/device.
18119          * We may have to process events for more than one port/device.
18120          */
18121         for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
18122                 /*
18123                  * Not all ports may be processed in attach by the time we
18124                  * get an event. Check if port info is initialized.
18125                  */
18126                 mutex_enter(&sata_hba_inst->satahba_mutex);
18127                 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
18128                 mutex_exit(&sata_hba_inst->satahba_mutex);
18129                 if (cportinfo == NULL || cportinfo->cport_state == NULL)
18130                         continue;
18131 
18132                 /* We have initialized controller port info */
18133                 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18134                 event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
18135                     cport_event_flags;
18136                 /* Check if port was locked by IOCTL processing */
18137                 if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
18138                         /*
18139                          * We ignore port events because port is busy
18140                          * with AP control processing. Set again
18141                          * controller and main event flag, so that
18142                          * events may be processed by the next daemon
18143                          * run.
18144                          */
18145                         mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18146                         mutex_enter(&sata_hba_inst->satahba_mutex);
18147                         sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
18148                         mutex_exit(&sata_hba_inst->satahba_mutex);
18149                         mutex_enter(&sata_mutex);
18150                         sata_event_pending |= SATA_EVNT_MAIN;
18151                         mutex_exit(&sata_mutex);
18152                         SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
18153                             "Event processing postponed until "
18154                             "AP control processing completes",
18155                             NULL);
18156                         /* Check other ports */
18157                         continue;
18158                 } else {
18159                         /*
18160                          * Set BSY flag so that AP control would not
18161                          * interfere with events processing for
18162                          * this port.
18163                          */
18164                         (SATA_CPORT_INFO(sata_hba_inst, ncport))->
18165                             cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
18166                 }
18167                 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18168 
18169                 saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
18170 
18171                 if ((event_flags &
18172                     (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18173                         /*
18174                          * Got port event.
18175                          * We need some hierarchy of event processing as they
18176                          * are affecting each other:
18177                          * 1. port failed
18178                          * 2. device detached/attached
18179                          * 3. link events - link events may trigger device
18180                          *    detached or device attached events in some
18181                          *    circumstances.
18182                          * 4. port power level changed
18183                          */
18184                         if (event_flags & SATA_EVNT_PORT_FAILED) {
18185                                 sata_process_port_failed_event(sata_hba_inst,
18186                                     saddr);
18187                         }
18188                         if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18189                                 sata_process_device_detached(sata_hba_inst,
18190                                     saddr);
18191                         }
18192                         if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18193                                 sata_process_device_attached(sata_hba_inst,
18194                                     saddr);
18195                         }
18196                         if (event_flags &
18197                             (SATA_EVNT_LINK_ESTABLISHED |
18198                             SATA_EVNT_LINK_LOST)) {
18199                                 sata_process_port_link_events(sata_hba_inst,
18200                                     saddr);
18201                         }
18202                         if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
18203                                 sata_process_port_pwr_change(sata_hba_inst,
18204                                     saddr);
18205                         }
18206                         if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18207                                 sata_process_target_node_cleanup(
18208                                     sata_hba_inst, saddr);
18209                         }
18210                         if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
18211                                 sata_process_device_autoonline(
18212                                     sata_hba_inst, saddr);
18213                         }
18214                 }
18215 
18216 
18217                 /*
18218                  * Scan port multiplier and all its sub-ports event flags.
18219                  * The events are marked by
18220                  * (1) sata_pmult_info.pmult_event_flags
18221                  * (2) sata_pmport_info.pmport_event_flags
18222                  */
18223                 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18224                 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
18225                         /*
18226                          * There should be another extra check: this
18227                          * port multiplier still exists?
18228                          */
18229                         pmultinfo = SATA_PMULT_INFO(sata_hba_inst,
18230                             ncport);
18231 
18232                         if (pmultinfo != NULL) {
18233                                 mutex_exit(&(SATA_CPORT_MUTEX(
18234                                     sata_hba_inst, ncport)));
18235                                 sata_process_pmult_events(
18236                                     sata_hba_inst, ncport);
18237                                 mutex_enter(&(SATA_CPORT_MUTEX(
18238                                     sata_hba_inst, ncport)));
18239                         } else {
18240                                 SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
18241                                     "Port-multiplier is gone. "
18242                                     "Ignore all sub-device events "
18243                                     "at port %d.", ncport);
18244                         }
18245                 }
18246 
18247                 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
18248                     SATA_DTYPE_NONE) &&
18249                     (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
18250                         if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
18251                             satadrv_event_flags &
18252                             (SATA_EVNT_DEVICE_RESET |
18253                             SATA_EVNT_INPROC_DEVICE_RESET)) {
18254                                 /* Have device event */
18255                                 sata_process_device_reset(sata_hba_inst,
18256                                     saddr);
18257                         }
18258                 }
18259                 /* Release PORT_BUSY flag */
18260                 (SATA_CPORT_INFO(sata_hba_inst, ncport))->
18261                     cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18262                 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18263 
18264         } /* End of loop through the controller SATA ports */
18265 }
18266 
18267 /*
18268  * Specific port multiplier instance event processing. At the moment, device
18269  * event processing is limited to link/attach event only.
18270  *
18271  * NOTE: power management event is not supported yet.
18272  */
18273 static void
18274 sata_process_pmult_events(sata_hba_inst_t *sata_hba_inst, uint8_t cport)
18275 {
18276         sata_cport_info_t *cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18277         sata_pmult_info_t *pmultinfo;
18278         sata_pmport_info_t *pmportinfo;
18279         sata_address_t *saddr;
18280         sata_device_t sata_device;
18281         uint32_t event_flags;
18282         int npmport;
18283         int rval;
18284 
18285         SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18286             "Processing pmult event(s) on cport %d of controller %d",
18287             cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18288 
18289         /* First process events on port multiplier */
18290         mutex_enter(&cportinfo->cport_mutex);
18291         pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
18292         event_flags = pmultinfo->pmult_event_flags;
18293 
18294         /*
18295          * Reset event (of port multiplier) has higher priority because the
18296          * port multiplier itself might be failed or removed after reset.
18297          */
18298         if (event_flags & SATA_EVNT_DEVICE_RESET) {
18299                 /*
18300                  * The status of the sub-links are uncertain,
18301                  * so mark all sub-ports as RESET
18302                  */
18303                 for (npmport = 0; npmport < SATA_NUM_PMPORTS(
18304                     sata_hba_inst, cport); npmport ++) {
18305                         pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
18306                             cport, npmport);
18307                         if (pmportinfo == NULL) {
18308                                 /* That's weird. */
18309                                 SATA_LOG_D((sata_hba_inst, CE_WARN,
18310                                     "sata_hba_event_notify: "
18311                                     "invalid/un-implemented "
18312                                     "port %d:%d (%d ports), ",
18313                                     cport, npmport, SATA_NUM_PMPORTS(
18314                                     sata_hba_inst, cport)));
18315                                 continue;
18316                         }
18317 
18318                         mutex_enter(&pmportinfo->pmport_mutex);
18319 
18320                         /* Mark all pmport to unknow state. */
18321                         pmportinfo->pmport_state = SATA_STATE_UNKNOWN;
18322                         /* Mark all pmports with link events. */
18323                         pmportinfo->pmport_event_flags =
18324                             (SATA_EVNT_LINK_ESTABLISHED|SATA_EVNT_LINK_LOST);
18325                         mutex_exit(&pmportinfo->pmport_mutex);
18326                 }
18327 
18328         } else if (event_flags & SATA_EVNT_PMULT_LINK_CHANGED) {
18329                 /*
18330                  * We need probe the port multiplier to know what has
18331                  * happened.
18332                  */
18333                 bzero(&sata_device, sizeof (sata_device_t));
18334                 sata_device.satadev_rev = SATA_DEVICE_REV;
18335                 sata_device.satadev_addr.cport = cport;
18336                 sata_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
18337                 sata_device.satadev_addr.qual = SATA_ADDR_PMULT;
18338 
18339                 mutex_exit(&cportinfo->cport_mutex);
18340                 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18341                     (SATA_DIP(sata_hba_inst), &sata_device);
18342                 mutex_enter(&cportinfo->cport_mutex);
18343                 if (rval != SATA_SUCCESS) {
18344                         /* Something went wrong? Fail the port */
18345                         cportinfo->cport_state = SATA_PSTATE_FAILED;
18346                         mutex_exit(&cportinfo->cport_mutex);
18347                         SATA_LOG_D((sata_hba_inst, CE_WARN,
18348                             "SATA port %d probing failed", cport));
18349 
18350                         /* PMult structure must be released.  */
18351                         sata_free_pmult(sata_hba_inst, &sata_device);
18352                         return;
18353                 }
18354 
18355                 sata_update_port_info(sata_hba_inst, &sata_device);
18356 
18357                 /*
18358                  * Sanity check - Port is active? Is the link active?
18359                  * The device is still a port multiplier?
18360                  */
18361                 if ((cportinfo->cport_state &
18362                     (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
18363                     ((cportinfo->cport_scr.sstatus &
18364                     SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) ||
18365                     (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
18366                         mutex_exit(&cportinfo->cport_mutex);
18367 
18368                         /* PMult structure must be released.  */
18369                         sata_free_pmult(sata_hba_inst, &sata_device);
18370                         return;
18371                 }
18372 
18373                 /* Probed succeed, set port ready. */
18374                 cportinfo->cport_state |=
18375                     SATA_STATE_PROBED | SATA_STATE_READY;
18376         }
18377 
18378         /* Release port multiplier event flags. */
18379         pmultinfo->pmult_event_flags &=
18380             ~(SATA_EVNT_DEVICE_RESET|SATA_EVNT_PMULT_LINK_CHANGED);
18381         mutex_exit(&cportinfo->cport_mutex);
18382 
18383         /*
18384          * Check all sub-links.
18385          */
18386         for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, cport);
18387             npmport ++) {
18388                 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
18389                 mutex_enter(&pmportinfo->pmport_mutex);
18390                 event_flags = pmportinfo->pmport_event_flags;
18391                 mutex_exit(&pmportinfo->pmport_mutex);
18392                 saddr = &pmportinfo->pmport_addr;
18393 
18394                 if ((event_flags &
18395                     (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18396                         /*
18397                          * Got port multiplier port event.
18398                          * We need some hierarchy of event processing as they
18399                          * are affecting each other:
18400                          * 1. device detached/attached
18401                          * 2. link events - link events may trigger device
18402                          *    detached or device attached events in some
18403                          *    circumstances.
18404                          */
18405                         if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18406                                 sata_process_pmdevice_detached(sata_hba_inst,
18407                                     saddr);
18408                         }
18409                         if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18410                                 sata_process_pmdevice_attached(sata_hba_inst,
18411                                     saddr);
18412                         }
18413                         if (event_flags & SATA_EVNT_LINK_ESTABLISHED ||
18414                             event_flags & SATA_EVNT_LINK_LOST) {
18415                                 sata_process_pmport_link_events(sata_hba_inst,
18416                                     saddr);
18417                         }
18418                         if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18419                                 sata_process_target_node_cleanup(
18420                                     sata_hba_inst, saddr);
18421                         }
18422                 }
18423 
18424                 /* Checking drive event(s). */
18425                 mutex_enter(&pmportinfo->pmport_mutex);
18426                 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
18427                     pmportinfo->pmport_sata_drive != NULL) {
18428                         event_flags = pmportinfo->pmport_sata_drive->
18429                             satadrv_event_flags;
18430                         if (event_flags & (SATA_EVNT_DEVICE_RESET |
18431                             SATA_EVNT_INPROC_DEVICE_RESET)) {
18432 
18433                                 /* Have device event */
18434                                 sata_process_pmdevice_reset(sata_hba_inst,
18435                                     saddr);
18436                         }
18437                 }
18438                 mutex_exit(&pmportinfo->pmport_mutex);
18439 
18440                 /* Release PORT_BUSY flag */
18441                 mutex_enter(&cportinfo->cport_mutex);
18442                 cportinfo->cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18443                 mutex_exit(&cportinfo->cport_mutex);
18444         }
18445 
18446         SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18447             "[DONE] pmult event(s) on cport %d of controller %d",
18448             cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18449 }
18450 
18451 /*
18452  * Process HBA power level change reported by HBA driver.
18453  * Not implemented at this time - event is ignored.
18454  */
18455 static void
18456 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
18457 {
18458         SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18459             "Processing controller power level change", NULL);
18460 
18461         /* Ignoring it for now */
18462         mutex_enter(&sata_hba_inst->satahba_mutex);
18463         sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18464         mutex_exit(&sata_hba_inst->satahba_mutex);
18465 }
18466 
18467 /*
18468  * Process port power level change reported by HBA driver.
18469  * Not implemented at this time - event is ignored.
18470  */
18471 static void
18472 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
18473     sata_address_t *saddr)
18474 {
18475         sata_cport_info_t *cportinfo;
18476 
18477         SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18478             "Processing port power level change", NULL);
18479 
18480         cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18481         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18482         /* Reset event flag */
18483         cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18484         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18485 }
18486 
18487 /*
18488  * Process port failure reported by HBA driver.
18489  * cports support only - no pmports.
18490  */
18491 static void
18492 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
18493     sata_address_t *saddr)
18494 {
18495         sata_cport_info_t *cportinfo;
18496 
18497         cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18498         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18499         /* Reset event flag first */
18500         cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
18501         /* If the port is in SHUTDOWN or FAILED state, ignore this event. */
18502         if ((cportinfo->cport_state &
18503             (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
18504                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18505                     cport_mutex);
18506                 return;
18507         }
18508         /* Fail the port */
18509         cportinfo->cport_state = SATA_PSTATE_FAILED;
18510         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18511         sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
18512 }
18513 
18514 /*
18515  * Device Reset Event processing.
18516  * The sequence is managed by 3 stage flags:
18517  * - reset event reported,
18518  * - reset event being processed,
18519  * - request to clear device reset state.
18520  *
18521  * NOTE: This function has to be entered with cport mutex held. It exits with
18522  * mutex held as well, but can release mutex during the processing.
18523  */
18524 static void
18525 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
18526     sata_address_t *saddr)
18527 {
18528         sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18529         sata_drive_info_t *sdinfo;
18530         sata_cport_info_t *cportinfo;
18531         sata_device_t sata_device;
18532         int rval_probe, rval_set;
18533 
18534         /* We only care about host sata cport for now */
18535         cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18536         sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18537         /*
18538          * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18539          * state, ignore reset event.
18540          */
18541         if (((cportinfo->cport_state &
18542             (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18543             (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18544                 sdinfo->satadrv_event_flags &=
18545                     ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18546                 return;
18547         }
18548 
18549         if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) ==
18550             SATA_DTYPE_PMULT)) {
18551                 /*
18552                  * Should not happened: this is already handled in
18553                  * sata_hba_event_notify()
18554                  */
18555                 mutex_exit(&cportinfo->cport_mutex);
18556                 goto done;
18557         }
18558 
18559         if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
18560             SATA_VALID_DEV_TYPE) == 0) {
18561                 /*
18562                  * This should not happen - coding error.
18563                  * But we can recover, so do not panic, just clean up
18564                  * and if in debug mode, log the message.
18565                  */
18566 #ifdef SATA_DEBUG
18567                 sata_log(sata_hba_inst, CE_WARN,
18568                     "sata_process_device_reset: "
18569                     "Invalid device type with sdinfo!", NULL);
18570 #endif
18571                 sdinfo->satadrv_event_flags = 0;
18572                 return;
18573         }
18574 
18575 #ifdef SATA_DEBUG
18576         if ((sdinfo->satadrv_event_flags &
18577             (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18578                 /* Nothing to do */
18579                 /* Something is weird - why we are processing dev reset? */
18580                 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18581                     "No device reset event!!!!", NULL);
18582 
18583                 return;
18584         }
18585         if ((sdinfo->satadrv_event_flags &
18586             (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
18587             (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
18588                 /* Something is weird - new device reset event */
18589                 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18590                     "Overlapping device reset events!", NULL);
18591         }
18592 #endif
18593         SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18594             "Processing port %d device reset", saddr->cport);
18595 
18596         /* Clear event flag */
18597         sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
18598 
18599         /* It seems that we always need to check the port state first */
18600         sata_device.satadev_rev = SATA_DEVICE_REV;
18601         sata_device.satadev_addr = *saddr;
18602         /*
18603          * We have to exit mutex, because the HBA probe port function may
18604          * block on its own mutex.
18605          */
18606         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18607         rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18608             (SATA_DIP(sata_hba_inst), &sata_device);
18609         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18610         sata_update_port_info(sata_hba_inst, &sata_device);
18611         if (rval_probe != SATA_SUCCESS) {
18612                 /* Something went wrong? Fail the port */
18613                 cportinfo->cport_state = SATA_PSTATE_FAILED;
18614                 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18615                 if (sdinfo != NULL)
18616                         sdinfo->satadrv_event_flags = 0;
18617                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18618                     cport_mutex);
18619                 SATA_LOG_D((sata_hba_inst, CE_WARN,
18620                     "SATA port %d probing failed",
18621                     saddr->cport));
18622                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
18623                     saddr->cport)->cport_mutex);
18624                 return;
18625         }
18626         if ((sata_device.satadev_scr.sstatus  &
18627             SATA_PORT_DEVLINK_UP_MASK) !=
18628             SATA_PORT_DEVLINK_UP ||
18629             sata_device.satadev_type == SATA_DTYPE_NONE) {
18630                 /*
18631                  * No device to process, anymore. Some other event processing
18632                  * would or have already performed port info cleanup.
18633                  * To be safe (HBA may need it), request clearing device
18634                  * reset condition.
18635                  */
18636                 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18637                 if (sdinfo != NULL) {
18638                         sdinfo->satadrv_event_flags &=
18639                             ~SATA_EVNT_INPROC_DEVICE_RESET;
18640                         sdinfo->satadrv_event_flags |=
18641                             SATA_EVNT_CLEAR_DEVICE_RESET;
18642                 }
18643                 return;
18644         }
18645 
18646         sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18647         if (sdinfo == NULL) {
18648                 return;
18649         }
18650         if ((sdinfo->satadrv_event_flags &
18651             SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
18652                 /*
18653                  * Start tracking time for device feature restoration and
18654                  * identification. Save current time (lbolt value).
18655                  */
18656                 sdinfo->satadrv_reset_time = ddi_get_lbolt();
18657         }
18658         /* Mark device reset processing as active */
18659         sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
18660 
18661         old_sdinfo = *sdinfo;   /* local copy of the drive info */
18662         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18663 
18664         rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1);
18665 
18666         if (rval_set  != SATA_SUCCESS) {
18667                 /*
18668                  * Restoring drive setting failed.
18669                  * Probe the port first, to check if the port state has changed
18670                  */
18671                 sata_device.satadev_rev = SATA_DEVICE_REV;
18672                 sata_device.satadev_addr = *saddr;
18673                 sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
18674                 /* probe port */
18675                 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18676                     (SATA_DIP(sata_hba_inst), &sata_device);
18677                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18678                     cport_mutex);
18679                 if (rval_probe == SATA_SUCCESS &&
18680                     (sata_device.satadev_state &
18681                     (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
18682                     (sata_device.satadev_scr.sstatus  &
18683                     SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
18684                     sata_device.satadev_type != SATA_DTYPE_NONE) {
18685                         /*
18686                          * We may retry this a bit later - in-process reset
18687                          * condition should be already set.
18688                          * Track retry time for device identification.
18689                          */
18690                         if ((cportinfo->cport_dev_type &
18691                             SATA_VALID_DEV_TYPE) != 0 &&
18692                             SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
18693                             sdinfo->satadrv_reset_time != 0) {
18694                                 clock_t cur_time = ddi_get_lbolt();
18695                                 /*
18696                                  * If the retry time limit was not
18697                                  * exceeded, retry.
18698                                  */
18699                                 if ((cur_time - sdinfo->satadrv_reset_time) <
18700                                     drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
18701                                         mutex_enter(
18702                                             &sata_hba_inst->satahba_mutex);
18703                                         sata_hba_inst->satahba_event_flags |=
18704                                             SATA_EVNT_MAIN;
18705                                         mutex_exit(
18706                                             &sata_hba_inst->satahba_mutex);
18707                                         mutex_enter(&sata_mutex);
18708                                         sata_event_pending |= SATA_EVNT_MAIN;
18709                                         mutex_exit(&sata_mutex);
18710                                         return;
18711                                 }
18712                                 if (rval_set == SATA_RETRY) {
18713                                         /*
18714                                          * Setting drive features failed, but
18715                                          * the drive is still accessible,
18716                                          * so emit a warning message before
18717                                          * return.
18718                                          */
18719                                         mutex_exit(&SATA_CPORT_INFO(
18720                                             sata_hba_inst,
18721                                             saddr->cport)->cport_mutex);
18722                                         goto done;
18723                                 }
18724                         }
18725                         /* Fail the drive */
18726                         sdinfo->satadrv_state = SATA_DSTATE_FAILED;
18727 
18728                         sata_log(sata_hba_inst, CE_WARN,
18729                             "SATA device at port %d - device failed",
18730                             saddr->cport);
18731 
18732                         DTRACE_PROBE(port_failed_f);
18733                 }
18734                 /*
18735                  * No point of retrying - device failed or some other event
18736                  * processing or already did or will do port info cleanup.
18737                  * To be safe (HBA may need it),
18738                  * request clearing device reset condition.
18739                  */
18740                 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
18741                 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
18742                 sdinfo->satadrv_reset_time = 0;
18743                 return;
18744         }
18745 done:
18746         /*
18747          * If setting of drive features failed, but the drive is still
18748          * accessible, emit a warning message.
18749          */
18750         if (rval_set == SATA_RETRY) {
18751                 sata_log(sata_hba_inst, CE_WARN,
18752                     "SATA device at port %d - desired setting could not be "
18753                     "restored after reset. Device may not operate as expected.",
18754                     saddr->cport);
18755         }
18756         /*
18757          * Raise the flag indicating that the next sata command could
18758          * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
18759          * reset is reported.
18760          */
18761         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18762         if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
18763                 sdinfo->satadrv_reset_time = 0;
18764                 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
18765                         sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
18766                         sdinfo->satadrv_event_flags &=
18767                             ~SATA_EVNT_INPROC_DEVICE_RESET;
18768                         sdinfo->satadrv_event_flags |=
18769                             SATA_EVNT_CLEAR_DEVICE_RESET;
18770                 }
18771         }
18772 }
18773 
18774 
18775 /*
18776  * Port Multiplier Port Device Reset Event processing.
18777  *
18778  * NOTE: This function has to be entered with pmport mutex held. It exits with
18779  * mutex held as well, but can release mutex during the processing.
18780  */
18781 static void
18782 sata_process_pmdevice_reset(sata_hba_inst_t *sata_hba_inst,
18783     sata_address_t *saddr)
18784 {
18785         sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18786         sata_drive_info_t *sdinfo = NULL;
18787         sata_cport_info_t *cportinfo = NULL;
18788         sata_pmport_info_t *pmportinfo = NULL;
18789         sata_pmult_info_t *pminfo = NULL;
18790         sata_device_t sata_device;
18791         uint8_t cport = saddr->cport;
18792         uint8_t pmport = saddr->pmport;
18793         int rval;
18794 
18795         SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18796             "Processing drive reset at port %d:%d", cport, pmport);
18797 
18798         cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18799         pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
18800         sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport);
18801 
18802         /*
18803          * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18804          * state, ignore reset event.
18805          */
18806         if (((cportinfo->cport_state &
18807             (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18808             (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18809                 sdinfo->satadrv_event_flags &=
18810                     ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18811                 return;
18812         }
18813 
18814         if ((pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
18815                 /*
18816                  * This should not happen - coding error.
18817                  * But we can recover, so do not panic, just clean up
18818                  * and if in debug mode, log the message.
18819                  */
18820 #ifdef SATA_DEBUG
18821                 sata_log(sata_hba_inst, CE_WARN,
18822                     "sata_process_pmdevice_reset: "
18823                     "Invalid device type with sdinfo!", NULL);
18824 #endif
18825                 sdinfo->satadrv_event_flags = 0;
18826                 return;
18827         }
18828 
18829 #ifdef SATA_DEBUG
18830         if ((sdinfo->satadrv_event_flags &
18831             (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18832                 /* Nothing to do */
18833                 /* Something is weird - why we are processing dev reset? */
18834                 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18835                     "No device reset event!!!!", NULL);
18836 
18837                 return;
18838         }
18839         if ((sdinfo->satadrv_event_flags &
18840             (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
18841             (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
18842                 /* Something is weird - new device reset event */
18843                 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18844                     "Overlapping device reset events!", NULL);
18845         }
18846 #endif
18847         SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18848             "Processing port %d:%d device reset", cport, pmport);
18849 
18850         /* Clear event flag */
18851         sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
18852 
18853         /* It seems that we always need to check the port state first */
18854         sata_device.satadev_rev = SATA_DEVICE_REV;
18855         sata_device.satadev_addr = *saddr;
18856         /*
18857          * We have to exit mutex, because the HBA probe port function may
18858          * block on its own mutex.
18859          */
18860         mutex_exit(&pmportinfo->pmport_mutex);
18861         rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18862             (SATA_DIP(sata_hba_inst), &sata_device);
18863         mutex_enter(&pmportinfo->pmport_mutex);
18864 
18865         sata_update_pmport_info(sata_hba_inst, &sata_device);
18866         if (rval != SATA_SUCCESS) {
18867                 /* Something went wrong? Fail the port */
18868                 pmportinfo->pmport_state = SATA_PSTATE_FAILED;
18869                 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18870                     saddr->pmport);
18871                 if (sdinfo != NULL)
18872                         sdinfo->satadrv_event_flags = 0;
18873                 mutex_exit(&pmportinfo->pmport_mutex);
18874                 SATA_LOG_D((sata_hba_inst, CE_WARN,
18875                     "SATA port %d:%d probing failed",
18876                     saddr->cport, saddr->pmport));
18877                 mutex_enter(&pmportinfo->pmport_mutex);
18878                 return;
18879         }
18880         if ((sata_device.satadev_scr.sstatus  &
18881             SATA_PORT_DEVLINK_UP_MASK) !=
18882             SATA_PORT_DEVLINK_UP ||
18883             sata_device.satadev_type == SATA_DTYPE_NONE) {
18884                 /*
18885                  * No device to process, anymore. Some other event processing
18886                  * would or have already performed port info cleanup.
18887                  * To be safe (HBA may need it), request clearing device
18888                  * reset condition.
18889                  */
18890                 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18891                     saddr->pmport);
18892                 if (sdinfo != NULL) {
18893                         sdinfo->satadrv_event_flags &=
18894                             ~SATA_EVNT_INPROC_DEVICE_RESET;
18895                         /* must clear flags on cport */
18896                         pminfo = SATA_PMULT_INFO(sata_hba_inst,
18897                             saddr->cport);
18898                         pminfo->pmult_event_flags |=
18899                             SATA_EVNT_CLEAR_DEVICE_RESET;
18900                 }
18901                 return;
18902         }
18903 
18904         sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18905             saddr->pmport);
18906         if (sdinfo == NULL) {
18907                 return;
18908         }
18909         if ((sdinfo->satadrv_event_flags &
18910             SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
18911                 /*
18912                  * Start tracking time for device feature restoration and
18913                  * identification. Save current time (lbolt value).
18914                  */
18915                 sdinfo->satadrv_reset_time = ddi_get_lbolt();
18916         }
18917         /* Mark device reset processing as active */
18918         sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
18919 
18920         old_sdinfo = *sdinfo;   /* local copy of the drive info */
18921         mutex_exit(&pmportinfo->pmport_mutex);
18922 
18923         if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
18924             SATA_FAILURE) {
18925                 /*
18926                  * Restoring drive setting failed.
18927                  * Probe the port first, to check if the port state has changed
18928                  */
18929                 sata_device.satadev_rev = SATA_DEVICE_REV;
18930                 sata_device.satadev_addr = *saddr;
18931                 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
18932 
18933                 /* probe port */
18934                 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18935                     (SATA_DIP(sata_hba_inst), &sata_device);
18936                 mutex_enter(&pmportinfo->pmport_mutex);
18937                 if (rval == SATA_SUCCESS &&
18938                     (sata_device.satadev_state &
18939                     (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
18940                     (sata_device.satadev_scr.sstatus  &
18941                     SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
18942                     sata_device.satadev_type != SATA_DTYPE_NONE) {
18943                         /*
18944                          * We may retry this a bit later - in-process reset
18945                          * condition should be already set.
18946                          * Track retry time for device identification.
18947                          */
18948                         if ((pmportinfo->pmport_dev_type &
18949                             SATA_VALID_DEV_TYPE) != 0 &&
18950                             SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL &&
18951                             sdinfo->satadrv_reset_time != 0) {
18952                                 clock_t cur_time = ddi_get_lbolt();
18953                                 /*
18954                                  * If the retry time limit was not
18955                                  * exceeded, retry.
18956                                  */
18957                                 if ((cur_time - sdinfo->satadrv_reset_time) <
18958                                     drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
18959                                         mutex_enter(
18960                                             &sata_hba_inst->satahba_mutex);
18961                                         sata_hba_inst->satahba_event_flags |=
18962                                             SATA_EVNT_MAIN;
18963                                         mutex_exit(
18964                                             &sata_hba_inst->satahba_mutex);
18965                                         mutex_enter(&sata_mutex);
18966                                         sata_event_pending |= SATA_EVNT_MAIN;
18967                                         mutex_exit(&sata_mutex);
18968                                         return;
18969                                 }
18970                         }
18971                         /* Fail the drive */
18972                         sdinfo->satadrv_state = SATA_DSTATE_FAILED;
18973 
18974                         sata_log(sata_hba_inst, CE_WARN,
18975                             "SATA device at port %d:%d - device failed",
18976                             saddr->cport, saddr->pmport);
18977                 } else {
18978                         /*
18979                          * No point of retrying - some other event processing
18980                          * would or already did port info cleanup.
18981                          * To be safe (HBA may need it),
18982                          * request clearing device reset condition.
18983                          */
18984                         sdinfo->satadrv_event_flags |=
18985                             SATA_EVNT_CLEAR_DEVICE_RESET;
18986                 }
18987                 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
18988                 sdinfo->satadrv_reset_time = 0;
18989                 return;
18990         }
18991         /*
18992          * Raise the flag indicating that the next sata command could
18993          * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
18994          * reset is reported.
18995          */
18996         mutex_enter(&pmportinfo->pmport_mutex);
18997         if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
18998                 sdinfo->satadrv_reset_time = 0;
18999                 if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
19000                         sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19001                         sdinfo->satadrv_event_flags &=
19002                             ~SATA_EVNT_INPROC_DEVICE_RESET;
19003                         /* must clear flags on cport */
19004                         pminfo = SATA_PMULT_INFO(sata_hba_inst,
19005                             saddr->cport);
19006                         pminfo->pmult_event_flags |=
19007                             SATA_EVNT_CLEAR_DEVICE_RESET;
19008                 }
19009         }
19010 }
19011 
19012 /*
19013  * Port Link Events processing.
19014  * Every link established event may involve device reset (due to
19015  * COMRESET signal, equivalent of the hard reset) so arbitrarily
19016  * set device reset event for an attached device (if any).
19017  * If the port is in SHUTDOWN or FAILED state, ignore link events.
19018  *
19019  * The link established event processing varies, depending on the state
19020  * of the target node, HBA hotplugging capabilities, state of the port.
19021  * If the link is not active, the link established event is ignored.
19022  * If HBA cannot detect device attachment and there is no target node,
19023  * the link established event triggers device attach event processing.
19024  * Else, link established event triggers device reset event processing.
19025  *
19026  * The link lost event processing varies, depending on a HBA hotplugging
19027  * capability and the state of the port (link active or not active).
19028  * If the link is active, the lost link event is ignored.
19029  * If HBA cannot detect device removal, the lost link event triggers
19030  * device detached event processing after link lost timeout.
19031  * Else, the event is ignored.
19032  *
19033  * NOTE: Port multiplier ports events are handled by
19034  * sata_process_pmport_link_events();
19035  */
19036 static void
19037 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
19038     sata_address_t *saddr)
19039 {
19040         sata_device_t sata_device;
19041         sata_cport_info_t *cportinfo;
19042         sata_drive_info_t *sdinfo;
19043         uint32_t event_flags;
19044         int rval;
19045 
19046         SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19047             "Processing port %d link event(s)", saddr->cport);
19048 
19049         cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19050         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19051         event_flags = cportinfo->cport_event_flags;
19052 
19053         /* Reset event flags first */
19054         cportinfo->cport_event_flags &=
19055             ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
19056 
19057         /* If the port is in SHUTDOWN or FAILED state, ignore link events. */
19058         if ((cportinfo->cport_state &
19059             (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19060                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19061                     cport_mutex);
19062                 return;
19063         }
19064 
19065         /*
19066          * For the sanity sake get current port state.
19067          * Set device address only. Other sata_device fields should be
19068          * set by HBA driver.
19069          */
19070         sata_device.satadev_rev = SATA_DEVICE_REV;
19071         sata_device.satadev_addr = *saddr;
19072         /*
19073          * We have to exit mutex, because the HBA probe port function may
19074          * block on its own mutex.
19075          */
19076         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19077         rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19078             (SATA_DIP(sata_hba_inst), &sata_device);
19079         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19080         sata_update_port_info(sata_hba_inst, &sata_device);
19081         if (rval != SATA_SUCCESS) {
19082                 /* Something went wrong? Fail the port */
19083                 cportinfo->cport_state = SATA_PSTATE_FAILED;
19084                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19085                     cport_mutex);
19086                 SATA_LOG_D((sata_hba_inst, CE_WARN,
19087                     "SATA port %d probing failed",
19088                     saddr->cport));
19089                 /*
19090                  * We may want to release device info structure, but
19091                  * it is not necessary.
19092                  */
19093                 return;
19094         } else {
19095                 /* port probed successfully */
19096                 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19097         }
19098         if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19099 
19100                 if ((sata_device.satadev_scr.sstatus &
19101                     SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19102                         /* Ignore event */
19103                         SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19104                             "Ignoring port %d link established event - "
19105                             "link down",
19106                             saddr->cport);
19107                         goto linklost;
19108                 }
19109 
19110                 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19111                     "Processing port %d link established event",
19112                     saddr->cport);
19113 
19114                 /*
19115                  * For the sanity sake check if a device is attached - check
19116                  * return state of a port probing.
19117                  */
19118                 if (sata_device.satadev_type != SATA_DTYPE_NONE) {
19119                         /*
19120                          * HBA port probe indicated that there is a device
19121                          * attached. Check if the framework had device info
19122                          * structure attached for this device.
19123                          */
19124                         if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
19125                                 ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
19126                                     NULL);
19127 
19128                                 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19129                                 if ((sdinfo->satadrv_type &
19130                                     SATA_VALID_DEV_TYPE) != 0) {
19131                                         /*
19132                                          * Dev info structure is present.
19133                                          * If dev_type is set to known type in
19134                                          * the framework's drive info struct
19135                                          * then the device existed before and
19136                                          * the link was probably lost
19137                                          * momentarily - in such case
19138                                          * we may want to check device
19139                                          * identity.
19140                                          * Identity check is not supported now.
19141                                          *
19142                                          * Link established event
19143                                          * triggers device reset event.
19144                                          */
19145                                         (SATA_CPORTINFO_DRV_INFO(cportinfo))->
19146                                             satadrv_event_flags |=
19147                                             SATA_EVNT_DEVICE_RESET;
19148                                 }
19149                         } else if (cportinfo->cport_dev_type ==
19150                             SATA_DTYPE_NONE) {
19151                                 /*
19152                                  * We got new device attached! If HBA does not
19153                                  * generate device attached events, trigger it
19154                                  * here.
19155                                  */
19156                                 if (!(SATA_FEATURES(sata_hba_inst) &
19157                                     SATA_CTLF_HOTPLUG)) {
19158                                         cportinfo->cport_event_flags |=
19159                                             SATA_EVNT_DEVICE_ATTACHED;
19160                                 }
19161                         }
19162                         /* Reset link lost timeout */
19163                         cportinfo->cport_link_lost_time = 0;
19164                 }
19165         }
19166 linklost:
19167         if (event_flags & SATA_EVNT_LINK_LOST) {
19168                 if ((sata_device.satadev_scr.sstatus &
19169                     SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19170                         /* Ignore event */
19171                         SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19172                             "Ignoring port %d link lost event - link is up",
19173                             saddr->cport);
19174                         goto done;
19175                 }
19176 #ifdef SATA_DEBUG
19177                 if (cportinfo->cport_link_lost_time == 0) {
19178                         SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19179                             "Processing port %d link lost event",
19180                             saddr->cport);
19181                 }
19182 #endif
19183                 /*
19184                  * When HBA cannot generate device attached/detached events,
19185                  * we need to track link lost time and eventually generate
19186                  * device detach event.
19187                  */
19188                 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19189                         /* We are tracking link lost time */
19190                         if (cportinfo->cport_link_lost_time == 0) {
19191                                 /* save current time (lbolt value) */
19192                                 cportinfo->cport_link_lost_time =
19193                                     ddi_get_lbolt();
19194                                 /* just keep link lost event */
19195                                 cportinfo->cport_event_flags |=
19196                                     SATA_EVNT_LINK_LOST;
19197                         } else {
19198                                 clock_t cur_time = ddi_get_lbolt();
19199                                 if ((cur_time -
19200                                     cportinfo->cport_link_lost_time) >=
19201                                     drv_usectohz(
19202                                     SATA_EVNT_LINK_LOST_TIMEOUT)) {
19203                                         /* trigger device detach event */
19204                                         cportinfo->cport_event_flags |=
19205                                             SATA_EVNT_DEVICE_DETACHED;
19206                                         cportinfo->cport_link_lost_time = 0;
19207                                         SATADBG1(SATA_DBG_EVENTS,
19208                                             sata_hba_inst,
19209                                             "Triggering port %d "
19210                                             "device detached event",
19211                                             saddr->cport);
19212                                 } else {
19213                                         /* keep link lost event */
19214                                         cportinfo->cport_event_flags |=
19215                                             SATA_EVNT_LINK_LOST;
19216                                 }
19217                         }
19218                 }
19219                 /*
19220                  * We could change port state to disable/delay access to
19221                  * the attached device until the link is recovered.
19222                  */
19223         }
19224 done:
19225         event_flags = cportinfo->cport_event_flags;
19226         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19227         if (event_flags != 0) {
19228                 mutex_enter(&sata_hba_inst->satahba_mutex);
19229                 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19230                 mutex_exit(&sata_hba_inst->satahba_mutex);
19231                 mutex_enter(&sata_mutex);
19232                 sata_event_pending |= SATA_EVNT_MAIN;
19233                 mutex_exit(&sata_mutex);
19234         }
19235 }
19236 
19237 /*
19238  * Port Multiplier Port Link Events processing.
19239  */
19240 static void
19241 sata_process_pmport_link_events(sata_hba_inst_t *sata_hba_inst,
19242     sata_address_t *saddr)
19243 {
19244         sata_device_t sata_device;
19245         sata_pmport_info_t *pmportinfo = NULL;
19246         sata_drive_info_t *sdinfo = NULL;
19247         uint32_t event_flags;
19248         uint8_t cport = saddr->cport;
19249         uint8_t pmport = saddr->pmport;
19250         int rval;
19251 
19252         SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19253             "Processing port %d:%d link event(s)",
19254             cport, pmport);
19255 
19256         pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19257         mutex_enter(&pmportinfo->pmport_mutex);
19258         event_flags = pmportinfo->pmport_event_flags;
19259 
19260         /* Reset event flags first */
19261         pmportinfo->pmport_event_flags &=
19262             ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
19263 
19264         /* If the port is in SHUTDOWN or FAILED state, ignore link events. */
19265         if ((pmportinfo->pmport_state &
19266             (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19267                 mutex_exit(&pmportinfo->pmport_mutex);
19268                 return;
19269         }
19270 
19271         /*
19272          * For the sanity sake get current port state.
19273          * Set device address only. Other sata_device fields should be
19274          * set by HBA driver.
19275          */
19276         sata_device.satadev_rev = SATA_DEVICE_REV;
19277         sata_device.satadev_addr = *saddr;
19278         /*
19279          * We have to exit mutex, because the HBA probe port function may
19280          * block on its own mutex.
19281          */
19282         mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19283             saddr->pmport));
19284         rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19285             (SATA_DIP(sata_hba_inst), &sata_device);
19286         mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19287             saddr->pmport));
19288         sata_update_pmport_info(sata_hba_inst, &sata_device);
19289         if (rval != SATA_SUCCESS) {
19290                 /* Something went wrong? Fail the port */
19291                 pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19292                 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19293                     saddr->pmport));
19294                 SATA_LOG_D((sata_hba_inst, CE_WARN,
19295                     "SATA port %d:%d probing failed",
19296                     saddr->cport, saddr->pmport));
19297                 /*
19298                  * We may want to release device info structure, but
19299                  * it is not necessary.
19300                  */
19301                 return;
19302         } else {
19303                 /* port probed successfully */
19304                 pmportinfo->pmport_state |=
19305                     SATA_STATE_PROBED | SATA_STATE_READY;
19306         }
19307         mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
19308             saddr->cport, saddr->pmport));
19309         mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
19310             saddr->cport, saddr->pmport));
19311         if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19312 
19313                 if ((sata_device.satadev_scr.sstatus &
19314                     SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19315                         /* Ignore event */
19316                         SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19317                             "Ignoring port %d:%d link established event - "
19318                             "link down",
19319                             saddr->cport, saddr->pmport);
19320                         goto linklost;
19321                 }
19322 
19323                 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19324                     "Processing port %d:%d link established event",
19325                     cport, pmport);
19326 
19327                 /*
19328                  * For the sanity sake check if a device is attached - check
19329                  * return state of a port probing.
19330                  */
19331                 if (sata_device.satadev_type != SATA_DTYPE_NONE &&
19332                     sata_device.satadev_type != SATA_DTYPE_PMULT) {
19333                         /*
19334                          * HBA port probe indicated that there is a device
19335                          * attached. Check if the framework had device info
19336                          * structure attached for this device.
19337                          */
19338                         if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
19339                                 ASSERT(SATA_PMPORTINFO_DRV_INFO(pmportinfo) !=
19340                                     NULL);
19341 
19342                                 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19343                                 if ((sdinfo->satadrv_type &
19344                                     SATA_VALID_DEV_TYPE) != 0) {
19345                                         /*
19346                                          * Dev info structure is present.
19347                                          * If dev_type is set to known type in
19348                                          * the framework's drive info struct
19349                                          * then the device existed before and
19350                                          * the link was probably lost
19351                                          * momentarily - in such case
19352                                          * we may want to check device
19353                                          * identity.
19354                                          * Identity check is not supported now.
19355                                          *
19356                                          * Link established event
19357                                          * triggers device reset event.
19358                                          */
19359                                         (SATA_PMPORTINFO_DRV_INFO(pmportinfo))->
19360                                             satadrv_event_flags |=
19361                                             SATA_EVNT_DEVICE_RESET;
19362                                 }
19363                         } else if (pmportinfo->pmport_dev_type ==
19364                             SATA_DTYPE_NONE) {
19365                                 /*
19366                                  * We got new device attached! If HBA does not
19367                                  * generate device attached events, trigger it
19368                                  * here.
19369                                  */
19370                                 if (!(SATA_FEATURES(sata_hba_inst) &
19371                                     SATA_CTLF_HOTPLUG)) {
19372                                         pmportinfo->pmport_event_flags |=
19373                                             SATA_EVNT_DEVICE_ATTACHED;
19374                                 }
19375                         }
19376                         /* Reset link lost timeout */
19377                         pmportinfo->pmport_link_lost_time = 0;
19378                 }
19379         }
19380 linklost:
19381         if (event_flags & SATA_EVNT_LINK_LOST) {
19382 #ifdef SATA_DEBUG
19383                 if (pmportinfo->pmport_link_lost_time == 0) {
19384                         SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19385                             "Processing port %d:%d link lost event",
19386                             saddr->cport, saddr->pmport);
19387                 }
19388 #endif
19389                 if ((sata_device.satadev_scr.sstatus &
19390                     SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19391                         /* Ignore event */
19392                         SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19393                             "Ignoring port %d:%d link lost event - link is up",
19394                             saddr->cport, saddr->pmport);
19395                         goto done;
19396                 }
19397                 /*
19398                  * When HBA cannot generate device attached/detached events,
19399                  * we need to track link lost time and eventually generate
19400                  * device detach event.
19401                  */
19402                 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19403                         /* We are tracking link lost time */
19404                         if (pmportinfo->pmport_link_lost_time == 0) {
19405                                 /* save current time (lbolt value) */
19406                                 pmportinfo->pmport_link_lost_time =
19407                                     ddi_get_lbolt();
19408                                 /* just keep link lost event */
19409                                 pmportinfo->pmport_event_flags |=
19410                                     SATA_EVNT_LINK_LOST;
19411                         } else {
19412                                 clock_t cur_time = ddi_get_lbolt();
19413                                 if ((cur_time -
19414                                     pmportinfo->pmport_link_lost_time) >=
19415                                     drv_usectohz(
19416                                     SATA_EVNT_LINK_LOST_TIMEOUT)) {
19417                                         /* trigger device detach event */
19418                                         pmportinfo->pmport_event_flags |=
19419                                             SATA_EVNT_DEVICE_DETACHED;
19420                                         pmportinfo->pmport_link_lost_time = 0;
19421                                         SATADBG2(SATA_DBG_EVENTS,
19422                                             sata_hba_inst,
19423                                             "Triggering port %d:%d "
19424                                             "device detached event",
19425                                             saddr->cport, saddr->pmport);
19426                                 } else {
19427                                         /* keep link lost event */
19428                                         pmportinfo->pmport_event_flags |=
19429                                             SATA_EVNT_LINK_LOST;
19430                                 }
19431                         }
19432                 }
19433                 /*
19434                  * We could change port state to disable/delay access to
19435                  * the attached device until the link is recovered.
19436                  */
19437         }
19438 done:
19439         event_flags = pmportinfo->pmport_event_flags;
19440         mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19441             saddr->pmport));
19442         if (event_flags != 0) {
19443                 mutex_enter(&sata_hba_inst->satahba_mutex);
19444                 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19445                 mutex_exit(&sata_hba_inst->satahba_mutex);
19446                 mutex_enter(&sata_mutex);
19447                 sata_event_pending |= SATA_EVNT_MAIN;
19448                 mutex_exit(&sata_mutex);
19449         }
19450 }
19451 
19452 /*
19453  * Device Detached Event processing.
19454  * Port is probed to find if a device is really gone. If so,
19455  * the device info structure is detached from the SATA port info structure
19456  * and released.
19457  * Port status is updated.
19458  *
19459  * NOTE: Port multiplier ports events are handled by
19460  * sata_process_pmdevice_detached()
19461  */
19462 static void
19463 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
19464     sata_address_t *saddr)
19465 {
19466         sata_cport_info_t *cportinfo;
19467         sata_pmport_info_t *pmportinfo;
19468         sata_drive_info_t *sdevinfo;
19469         sata_device_t sata_device;
19470         sata_address_t pmport_addr;
19471         char name[16];
19472         uint8_t cport = saddr->cport;
19473         int npmport;
19474         int rval;
19475 
19476         SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19477             "Processing port %d device detached", saddr->cport);
19478 
19479         cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19480         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19481         /* Clear event flag */
19482         cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19483 
19484         /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19485         if ((cportinfo->cport_state &
19486             (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19487                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19488                     cport_mutex);
19489                 return;
19490         }
19491         /* For sanity, re-probe the port */
19492         sata_device.satadev_rev = SATA_DEVICE_REV;
19493         sata_device.satadev_addr = *saddr;
19494 
19495         /*
19496          * We have to exit mutex, because the HBA probe port function may
19497          * block on its own mutex.
19498          */
19499         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19500         rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19501             (SATA_DIP(sata_hba_inst), &sata_device);
19502         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19503         sata_update_port_info(sata_hba_inst, &sata_device);
19504         if (rval != SATA_SUCCESS) {
19505                 /* Something went wrong? Fail the port */
19506                 cportinfo->cport_state = SATA_PSTATE_FAILED;
19507                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19508                     cport_mutex);
19509                 SATA_LOG_D((sata_hba_inst, CE_WARN,
19510                     "SATA port %d probing failed",
19511                     saddr->cport));
19512                 /*
19513                  * We may want to release device info structure, but
19514                  * it is not necessary.
19515                  */
19516                 return;
19517         } else {
19518                 /* port probed successfully */
19519                 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19520         }
19521         /*
19522          * Check if a device is still attached. For sanity, check also
19523          * link status - if no link, there is no device.
19524          */
19525         if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19526             SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19527             SATA_DTYPE_NONE) {
19528                 /*
19529                  * Device is still attached - ignore detach event.
19530                  */
19531                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19532                     cport_mutex);
19533                 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19534                     "Ignoring detach - device still attached to port %d",
19535                     sata_device.satadev_addr.cport);
19536                 return;
19537         }
19538         /*
19539          * We need to detach and release device info structure here
19540          */
19541         if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
19542                 /*
19543                  * A port-multiplier is removed.
19544                  *
19545                  * Calling sata_process_pmdevice_detached() does not work
19546                  * here. The port multiplier is gone, so we cannot probe
19547                  * sub-port any more and all pmult-related data structure must
19548                  * be de-allocated immediately. Following structure of every
19549                  * implemented sub-port behind the pmult are required to
19550                  * released.
19551                  *
19552                  *   - attachment point
19553                  *   - target node
19554                  *   - sata_drive_info
19555                  *   - sata_pmport_info
19556                  */
19557                 for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst,
19558                     cport); npmport ++) {
19559                         SATADBG2(SATA_DBG_PMULT|SATA_DBG_EVENTS_PROC,
19560                             sata_hba_inst,
19561                             "Detaching target node at port %d:%d",
19562                             cport, npmport);
19563 
19564                         mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19565 
19566                         /* Remove attachment point. */
19567                         name[0] = '\0';
19568                         (void) sprintf(name, "%d.%d", cport, npmport);
19569                         ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
19570                         sata_log(sata_hba_inst, CE_NOTE,
19571                             "Remove attachment point of port %d:%d",
19572                             cport, npmport);
19573 
19574                         /* Remove target node */
19575                         pmport_addr.cport = cport;
19576                         pmport_addr.pmport = (uint8_t)npmport;
19577                         pmport_addr.qual = SATA_ADDR_PMPORT;
19578                         sata_remove_target_node(sata_hba_inst, &pmport_addr);
19579 
19580                         mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19581 
19582                         /* Release sata_pmport_info & sata_drive_info. */
19583                         pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
19584                             cport, npmport);
19585                         ASSERT(pmportinfo != NULL);
19586 
19587                         sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19588                         if (sdevinfo != NULL) {
19589                                 (void) kmem_free((void *) sdevinfo,
19590                                     sizeof (sata_drive_info_t));
19591                         }
19592 
19593                         /* Release sata_pmport_info at last */
19594                         (void) kmem_free((void *) pmportinfo,
19595                             sizeof (sata_pmport_info_t));
19596                 }
19597 
19598                 /* Finally, release sata_pmult_info */
19599                 (void) kmem_free((void *)
19600                     SATA_CPORTINFO_PMULT_INFO(cportinfo),
19601                     sizeof (sata_pmult_info_t));
19602                 SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
19603 
19604                 sata_log(sata_hba_inst, CE_WARN,
19605                     "SATA port-multiplier detached at port %d", cport);
19606 
19607                 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19608                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19609                     saddr->cport)->cport_mutex);
19610         } else {
19611                 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19612                         sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19613                         SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
19614                         (void) kmem_free((void *)sdevinfo,
19615                             sizeof (sata_drive_info_t));
19616                 }
19617                 sata_log(sata_hba_inst, CE_WARN,
19618                     "SATA device detached at port %d", cport);
19619 
19620                 cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19621                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19622                     saddr->cport)->cport_mutex);
19623 
19624                 /*
19625                  * Try to offline a device and remove target node
19626                  * if it still exists
19627                  */
19628                 sata_remove_target_node(sata_hba_inst, saddr);
19629         }
19630 
19631 
19632         /*
19633          * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19634          * with the hint: SE_HINT_REMOVE
19635          */
19636         sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
19637 }
19638 
19639 /*
19640  * Port Multiplier Port Device Deattached Event processing.
19641  *
19642  * NOTE: No Mutex should be hold.
19643  */
19644 static void
19645 sata_process_pmdevice_detached(sata_hba_inst_t *sata_hba_inst,
19646     sata_address_t *saddr)
19647 {
19648         sata_pmport_info_t *pmportinfo;
19649         sata_drive_info_t *sdevinfo;
19650         sata_device_t sata_device;
19651         int rval;
19652         uint8_t cport, pmport;
19653 
19654         cport = saddr->cport;
19655         pmport = saddr->pmport;
19656 
19657         SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19658             "Processing port %d:%d device detached",
19659             cport, pmport);
19660 
19661         pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19662         mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19663 
19664         /* Clear event flag */
19665         pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19666 
19667         /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19668         if ((pmportinfo->pmport_state &
19669             (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19670                 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19671                 return;
19672         }
19673         /* For sanity, re-probe the port */
19674         sata_device.satadev_rev = SATA_DEVICE_REV;
19675         sata_device.satadev_addr = *saddr;
19676 
19677         /*
19678          * We have to exit mutex, because the HBA probe port function may
19679          * block on its own mutex.
19680          */
19681         mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19682         rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19683             (SATA_DIP(sata_hba_inst), &sata_device);
19684         mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19685         sata_update_pmport_info(sata_hba_inst, &sata_device);
19686         if (rval != SATA_SUCCESS) {
19687                 /* Something went wrong? Fail the port */
19688                 pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19689                 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19690                 SATA_LOG_D((sata_hba_inst, CE_WARN,
19691                     "SATA port %d:%d probing failed",
19692                     saddr->pmport));
19693                 /*
19694                  * We may want to release device info structure, but
19695                  * it is not necessary.
19696                  */
19697                 return;
19698         } else {
19699                 /* port probed successfully */
19700                 pmportinfo->pmport_state |=
19701                     SATA_STATE_PROBED | SATA_STATE_READY;
19702         }
19703         /*
19704          * Check if a device is still attached. For sanity, check also
19705          * link status - if no link, there is no device.
19706          */
19707         if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19708             SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19709             SATA_DTYPE_NONE) {
19710                 /*
19711                  * Device is still attached - ignore detach event.
19712                  */
19713                 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19714                 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19715                     "Ignoring detach - device still attached to port %d",
19716                     sata_device.satadev_addr.pmport);
19717                 return;
19718         }
19719         /*
19720          * We need to detach and release device info structure here
19721          */
19722         if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
19723                 sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19724                 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
19725                 (void) kmem_free((void *)sdevinfo,
19726                     sizeof (sata_drive_info_t));
19727         }
19728         pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
19729         /*
19730          * Device cannot be reached anymore, even if the target node may be
19731          * still present.
19732          */
19733         mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19734 
19735         /*
19736          * Try to offline a device and remove target node if it still exists
19737          */
19738         sata_remove_target_node(sata_hba_inst, saddr);
19739 
19740         /*
19741          * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19742          * with the hint: SE_HINT_REMOVE
19743          */
19744         sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
19745 }
19746 
19747 
19748 /*
19749  * Device Attached Event processing.
19750  * Port state is checked to verify that a device is really attached. If so,
19751  * the device info structure is created and attached to the SATA port info
19752  * structure.
19753  *
19754  * If attached device cannot be identified or set-up, the retry for the
19755  * attach processing is set-up. Subsequent daemon run would try again to
19756  * identify the device, until the time limit is reached
19757  * (SATA_DEV_IDENTIFY_TIMEOUT).
19758  *
19759  * This function cannot be called in interrupt context (it may sleep).
19760  *
19761  * NOTE: Port multiplier ports events are handled by
19762  * sata_process_pmdevice_attached()
19763  */
19764 static void
19765 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
19766     sata_address_t *saddr)
19767 {
19768         sata_cport_info_t *cportinfo = NULL;
19769         sata_drive_info_t *sdevinfo = NULL;
19770         sata_pmult_info_t *pmultinfo = NULL;
19771         sata_pmport_info_t *pmportinfo = NULL;
19772         sata_device_t sata_device;
19773         dev_info_t *tdip;
19774         uint32_t event_flags = 0, pmult_event_flags = 0;
19775         int rval;
19776         int npmport;
19777 
19778         SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19779             "Processing port %d device attached", saddr->cport);
19780 
19781         cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19782         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19783 
19784         /* Clear attach event flag first */
19785         cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
19786 
19787         /* If the port is in SHUTDOWN or FAILED state, ignore event. */
19788         if ((cportinfo->cport_state &
19789             (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19790                 cportinfo->cport_dev_attach_time = 0;
19791                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19792                     cport_mutex);
19793                 return;
19794         }
19795 
19796         /*
19797          * If the sata_drive_info structure is found attached to the port info,
19798          * despite the fact the device was removed and now it is re-attached,
19799          * the old drive info structure was not removed.
19800          * Arbitrarily release device info structure.
19801          */
19802         if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19803                 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19804                 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
19805                 (void) kmem_free((void *)sdevinfo,
19806                     sizeof (sata_drive_info_t));
19807                 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19808                     "Arbitrarily detaching old device info.", NULL);
19809         }
19810         cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19811 
19812         /* For sanity, re-probe the port */
19813         sata_device.satadev_rev = SATA_DEVICE_REV;
19814         sata_device.satadev_addr = *saddr;
19815 
19816         /*
19817          * We have to exit mutex, because the HBA probe port function may
19818          * block on its own mutex.
19819          */
19820         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19821         rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19822             (SATA_DIP(sata_hba_inst), &sata_device);
19823         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19824         sata_update_port_info(sata_hba_inst, &sata_device);
19825         if (rval != SATA_SUCCESS) {
19826                 /* Something went wrong? Fail the port */
19827                 cportinfo->cport_state = SATA_PSTATE_FAILED;
19828                 cportinfo->cport_dev_attach_time = 0;
19829                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19830                     cport_mutex);
19831                 SATA_LOG_D((sata_hba_inst, CE_WARN,
19832                     "SATA port %d probing failed",
19833                     saddr->cport));
19834                 return;
19835         } else {
19836                 /* port probed successfully */
19837                 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19838         }
19839         /*
19840          * Check if a device is still attached. For sanity, check also
19841          * link status - if no link, there is no device.
19842          */
19843         if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
19844             SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
19845             SATA_DTYPE_NONE) {
19846                 /*
19847                  * No device - ignore attach event.
19848                  */
19849                 cportinfo->cport_dev_attach_time = 0;
19850                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19851                     cport_mutex);
19852                 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19853                     "Ignoring attach - no device connected to port %d",
19854                     sata_device.satadev_addr.cport);
19855                 return;
19856         }
19857 
19858         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19859         /*
19860          * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19861          * with the hint: SE_HINT_INSERT
19862          */
19863         sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
19864 
19865         /*
19866          * Port reprobing will take care of the creation of the device
19867          * info structure and determination of the device type.
19868          */
19869         sata_device.satadev_addr = *saddr;
19870         (void) sata_reprobe_port(sata_hba_inst, &sata_device,
19871             SATA_DEV_IDENTIFY_NORETRY);
19872 
19873         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19874             cport_mutex);
19875         if ((cportinfo->cport_state & SATA_STATE_READY) &&
19876             (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
19877                 /* Some device is attached to the port */
19878                 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
19879                         /*
19880                          * A device was not successfully attached.
19881                          * Track retry time for device identification.
19882                          */
19883                         if (cportinfo->cport_dev_attach_time != 0) {
19884                                 clock_t cur_time = ddi_get_lbolt();
19885                                 /*
19886                                  * If the retry time limit was not exceeded,
19887                                  * reinstate attach event.
19888                                  */
19889                                 if ((cur_time -
19890                                     cportinfo->cport_dev_attach_time) <
19891                                     drv_usectohz(
19892                                     SATA_DEV_IDENTIFY_TIMEOUT)) {
19893                                         /* OK, restore attach event */
19894                                         cportinfo->cport_event_flags |=
19895                                             SATA_EVNT_DEVICE_ATTACHED;
19896                                 } else {
19897                                         /* Timeout - cannot identify device */
19898                                         cportinfo->cport_dev_attach_time = 0;
19899                                         sata_log(sata_hba_inst,
19900                                             CE_WARN,
19901                                             "Could not identify SATA device "
19902                                             "at port %d",
19903                                             saddr->cport);
19904                                 }
19905                         } else {
19906                                 /*
19907                                  * Start tracking time for device
19908                                  * identification.
19909                                  * Save current time (lbolt value).
19910                                  */
19911                                 cportinfo->cport_dev_attach_time =
19912                                     ddi_get_lbolt();
19913                                 /* Restore attach event */
19914                                 cportinfo->cport_event_flags |=
19915                                     SATA_EVNT_DEVICE_ATTACHED;
19916                         }
19917                 } else if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
19918                         cportinfo->cport_dev_attach_time = 0;
19919                         sata_log(sata_hba_inst, CE_NOTE,
19920                             "SATA port-multiplier detected at port %d",
19921                             saddr->cport);
19922 
19923                         if (SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL) {
19924                                 /* Log the info of new port multiplier */
19925                                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19926                                     saddr->cport)->cport_mutex);
19927                                 sata_show_pmult_info(sata_hba_inst,
19928                                     &sata_device);
19929                                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19930                                     saddr->cport)->cport_mutex);
19931                         }
19932 
19933                         ASSERT(SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL);
19934                         pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
19935                         for (npmport = 0; npmport <
19936                             pmultinfo->pmult_num_dev_ports; npmport++) {
19937                                 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
19938                                     saddr->cport, npmport);
19939                                 ASSERT(pmportinfo != NULL);
19940 
19941                                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19942                                     saddr->cport)->cport_mutex);
19943                                 mutex_enter(&pmportinfo->pmport_mutex);
19944                                 /* Marked all pmports with link events. */
19945                                 pmportinfo->pmport_event_flags =
19946                                     SATA_EVNT_LINK_ESTABLISHED;
19947                                 pmult_event_flags |=
19948                                     pmportinfo->pmport_event_flags;
19949                                 mutex_exit(&pmportinfo->pmport_mutex);
19950                                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19951                                     saddr->cport)->cport_mutex);
19952                         }
19953                         /* Auto-online is not available for PMult now. */
19954 
19955                 } else {
19956                         /*
19957                          * If device was successfully attached, the subsequent
19958                          * action depends on a state of the
19959                          * sata_auto_online variable. If it is set to zero.
19960                          * an explicit 'configure' command will be needed to
19961                          * configure it. If its value is non-zero, we will
19962                          * attempt to online (configure) the device.
19963                          * First, log the message indicating that a device
19964                          * was attached.
19965                          */
19966                         cportinfo->cport_dev_attach_time = 0;
19967                         sata_log(sata_hba_inst, CE_WARN,
19968                             "SATA device detected at port %d", saddr->cport);
19969 
19970                         if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19971                                 sata_drive_info_t new_sdinfo;
19972 
19973                                 /* Log device info data */
19974                                 new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
19975                                     cportinfo));
19976                                 sata_show_drive_info(sata_hba_inst,
19977                                     &new_sdinfo);
19978                         }
19979 
19980                         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19981                             saddr->cport)->cport_mutex);
19982 
19983                         /*
19984                          * Make sure that there is no target node for that
19985                          * device. If so, release it. It should not happen,
19986                          * unless we had problem removing the node when
19987                          * device was detached.
19988                          */
19989                         tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
19990                             saddr->cport, saddr->pmport);
19991                         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19992                             saddr->cport)->cport_mutex);
19993                         if (tdip != NULL) {
19994 
19995 #ifdef SATA_DEBUG
19996                                 if ((cportinfo->cport_event_flags &
19997                                     SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
19998                                         sata_log(sata_hba_inst, CE_WARN,
19999                                             "sata_process_device_attached: "
20000                                             "old device target node exists!");
20001 #endif
20002                                 /*
20003                                  * target node exists - try to unconfigure
20004                                  * device and remove the node.
20005                                  */
20006                                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20007                                     saddr->cport)->cport_mutex);
20008                                 rval = ndi_devi_offline(tdip,
20009                                     NDI_DEVI_REMOVE);
20010                                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20011                                     saddr->cport)->cport_mutex);
20012 
20013                                 if (rval == NDI_SUCCESS) {
20014                                         cportinfo->cport_event_flags &=
20015                                             ~SATA_EVNT_TARGET_NODE_CLEANUP;
20016                                         cportinfo->cport_tgtnode_clean = B_TRUE;
20017                                 } else {
20018                                         /*
20019                                          * PROBLEM - the target node remained
20020                                          * and it belongs to a previously
20021                                          * attached device.
20022                                          * This happens when the file was open
20023                                          * or the node was waiting for
20024                                          * resources at the time the
20025                                          * associated device was removed.
20026                                          * Instruct event daemon to retry the
20027                                          * cleanup later.
20028                                          */
20029                                         sata_log(sata_hba_inst,
20030                                             CE_WARN,
20031                                             "Application(s) accessing "
20032                                             "previously attached SATA "
20033                                             "device have to release "
20034                                             "it before newly inserted "
20035                                             "device can be made accessible.",
20036                                             saddr->cport);
20037                                         cportinfo->cport_event_flags |=
20038                                             SATA_EVNT_TARGET_NODE_CLEANUP;
20039                                         cportinfo->cport_tgtnode_clean =
20040                                             B_FALSE;
20041                                 }
20042                         }
20043                         if (sata_auto_online != 0) {
20044                                 cportinfo->cport_event_flags |=
20045                                     SATA_EVNT_AUTOONLINE_DEVICE;
20046                         }
20047 
20048                 }
20049         } else {
20050                 cportinfo->cport_dev_attach_time = 0;
20051         }
20052 
20053         event_flags = cportinfo->cport_event_flags;
20054         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20055         if (event_flags != 0 || pmult_event_flags != 0) {
20056                 mutex_enter(&sata_hba_inst->satahba_mutex);
20057                 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20058                 mutex_exit(&sata_hba_inst->satahba_mutex);
20059                 mutex_enter(&sata_mutex);
20060                 sata_event_pending |= SATA_EVNT_MAIN;
20061                 mutex_exit(&sata_mutex);
20062         }
20063 }
20064 
20065 /*
20066  * Port Multiplier Port Device Attached Event processing.
20067  *
20068  * NOTE: No Mutex should be hold.
20069  */
20070 static void
20071 sata_process_pmdevice_attached(sata_hba_inst_t *sata_hba_inst,
20072     sata_address_t *saddr)
20073 {
20074         sata_pmport_info_t *pmportinfo;
20075         sata_drive_info_t *sdinfo;
20076         sata_device_t sata_device;
20077         dev_info_t *tdip;
20078         uint32_t event_flags;
20079         uint8_t cport = saddr->cport;
20080         uint8_t pmport = saddr->pmport;
20081         int rval;
20082 
20083         SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20084             "Processing port %d:%d device attached", cport, pmport);
20085 
20086         pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
20087 
20088         mutex_enter(&pmportinfo->pmport_mutex);
20089 
20090         /* Clear attach event flag first */
20091         pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
20092 
20093         /* If the port is in SHUTDOWN or FAILED state, ignore event. */
20094         if ((pmportinfo->pmport_state &
20095             (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
20096                 pmportinfo->pmport_dev_attach_time = 0;
20097                 mutex_exit(&pmportinfo->pmport_mutex);
20098                 return;
20099         }
20100 
20101         /*
20102          * If the sata_drive_info structure is found attached to the port info,
20103          * despite the fact the device was removed and now it is re-attached,
20104          * the old drive info structure was not removed.
20105          * Arbitrarily release device info structure.
20106          */
20107         if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20108                 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
20109                 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
20110                 (void) kmem_free((void *)sdinfo,
20111                     sizeof (sata_drive_info_t));
20112                 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20113                     "Arbitrarily detaching old device info.", NULL);
20114         }
20115         pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
20116 
20117         /* For sanity, re-probe the port */
20118         sata_device.satadev_rev = SATA_DEVICE_REV;
20119         sata_device.satadev_addr = *saddr;
20120 
20121         /*
20122          * We have to exit mutex, because the HBA probe port function may
20123          * block on its own mutex.
20124          */
20125         mutex_exit(&pmportinfo->pmport_mutex);
20126         rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
20127             (SATA_DIP(sata_hba_inst), &sata_device);
20128         mutex_enter(&pmportinfo->pmport_mutex);
20129 
20130         sata_update_pmport_info(sata_hba_inst, &sata_device);
20131         if (rval != SATA_SUCCESS) {
20132                 /* Something went wrong? Fail the port */
20133                 pmportinfo->pmport_state = SATA_PSTATE_FAILED;
20134                 pmportinfo->pmport_dev_attach_time = 0;
20135                 mutex_exit(&pmportinfo->pmport_mutex);
20136                 SATA_LOG_D((sata_hba_inst, CE_WARN,
20137                     "SATA port %d:%d probing failed", cport, pmport));
20138                 return;
20139         } else {
20140                 /* pmport probed successfully */
20141                 pmportinfo->pmport_state |=
20142                     SATA_STATE_PROBED | SATA_STATE_READY;
20143         }
20144         /*
20145          * Check if a device is still attached. For sanity, check also
20146          * link status - if no link, there is no device.
20147          */
20148         if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
20149             SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
20150             SATA_DTYPE_NONE) {
20151                 /*
20152                  * No device - ignore attach event.
20153                  */
20154                 pmportinfo->pmport_dev_attach_time = 0;
20155                 mutex_exit(&pmportinfo->pmport_mutex);
20156                 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20157                     "Ignoring attach - no device connected to port %d:%d",
20158                     cport, pmport);
20159                 return;
20160         }
20161 
20162         mutex_exit(&pmportinfo->pmport_mutex);
20163         /*
20164          * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
20165          * with the hint: SE_HINT_INSERT
20166          */
20167         sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
20168 
20169         /*
20170          * Port reprobing will take care of the creation of the device
20171          * info structure and determination of the device type.
20172          */
20173         sata_device.satadev_addr = *saddr;
20174         (void) sata_reprobe_port(sata_hba_inst, &sata_device,
20175             SATA_DEV_IDENTIFY_NORETRY);
20176 
20177         mutex_enter(&pmportinfo->pmport_mutex);
20178         if ((pmportinfo->pmport_state & SATA_STATE_READY) &&
20179             (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE)) {
20180                 /* Some device is attached to the port */
20181                 if (pmportinfo->pmport_dev_type == SATA_DTYPE_UNKNOWN) {
20182                         /*
20183                          * A device was not successfully attached.
20184                          * Track retry time for device identification.
20185                          */
20186                         if (pmportinfo->pmport_dev_attach_time != 0) {
20187                                 clock_t cur_time = ddi_get_lbolt();
20188                                 /*
20189                                  * If the retry time limit was not exceeded,
20190                                  * reinstate attach event.
20191                                  */
20192                                 if ((cur_time -
20193                                     pmportinfo->pmport_dev_attach_time) <
20194                                     drv_usectohz(
20195                                     SATA_DEV_IDENTIFY_TIMEOUT)) {
20196                                         /* OK, restore attach event */
20197                                         pmportinfo->pmport_event_flags |=
20198                                             SATA_EVNT_DEVICE_ATTACHED;
20199                                 } else {
20200                                         /* Timeout - cannot identify device */
20201                                         pmportinfo->pmport_dev_attach_time = 0;
20202                                         sata_log(sata_hba_inst, CE_WARN,
20203                                             "Could not identify SATA device "
20204                                             "at port %d:%d",
20205                                             cport, pmport);
20206                                 }
20207                         } else {
20208                                 /*
20209                                  * Start tracking time for device
20210                                  * identification.
20211                                  * Save current time (lbolt value).
20212                                  */
20213                                 pmportinfo->pmport_dev_attach_time =
20214                                     ddi_get_lbolt();
20215                                 /* Restore attach event */
20216                                 pmportinfo->pmport_event_flags |=
20217                                     SATA_EVNT_DEVICE_ATTACHED;
20218                         }
20219                 } else {
20220                         /*
20221                          * If device was successfully attached, the subsequent
20222                          * action depends on a state of the
20223                          * sata_auto_online variable. If it is set to zero.
20224                          * an explicit 'configure' command will be needed to
20225                          * configure it. If its value is non-zero, we will
20226                          * attempt to online (configure) the device.
20227                          * First, log the message indicating that a device
20228                          * was attached.
20229                          */
20230                         pmportinfo->pmport_dev_attach_time = 0;
20231                         sata_log(sata_hba_inst, CE_WARN,
20232                             "SATA device detected at port %d:%d",
20233                             cport, pmport);
20234 
20235                         if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20236                                 sata_drive_info_t new_sdinfo;
20237 
20238                                 /* Log device info data */
20239                                 new_sdinfo = *(SATA_PMPORTINFO_DRV_INFO(
20240                                     pmportinfo));
20241                                 sata_show_drive_info(sata_hba_inst,
20242                                     &new_sdinfo);
20243                         }
20244 
20245                         mutex_exit(&pmportinfo->pmport_mutex);
20246 
20247                         /*
20248                          * Make sure that there is no target node for that
20249                          * device. If so, release it. It should not happen,
20250                          * unless we had problem removing the node when
20251                          * device was detached.
20252                          */
20253                         tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
20254                             saddr->cport, saddr->pmport);
20255                         mutex_enter(&pmportinfo->pmport_mutex);
20256                         if (tdip != NULL) {
20257 
20258 #ifdef SATA_DEBUG
20259                                 if ((pmportinfo->pmport_event_flags &
20260                                     SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
20261                                         sata_log(sata_hba_inst, CE_WARN,
20262                                             "sata_process_device_attached: "
20263                                             "old device target node exists!");
20264 #endif
20265                                 /*
20266                                  * target node exists - try to unconfigure
20267                                  * device and remove the node.
20268                                  */
20269                                 mutex_exit(&pmportinfo->pmport_mutex);
20270                                 rval = ndi_devi_offline(tdip,
20271                                     NDI_DEVI_REMOVE);
20272                                 mutex_enter(&pmportinfo->pmport_mutex);
20273 
20274                                 if (rval == NDI_SUCCESS) {
20275                                         pmportinfo->pmport_event_flags &=
20276                                             ~SATA_EVNT_TARGET_NODE_CLEANUP;
20277                                         pmportinfo->pmport_tgtnode_clean =
20278                                             B_TRUE;
20279                                 } else {
20280                                         /*
20281                                          * PROBLEM - the target node remained
20282                                          * and it belongs to a previously
20283                                          * attached device.
20284                                          * This happens when the file was open
20285                                          * or the node was waiting for
20286                                          * resources at the time the
20287                                          * associated device was removed.
20288                                          * Instruct event daemon to retry the
20289                                          * cleanup later.
20290                                          */
20291                                         sata_log(sata_hba_inst,
20292                                             CE_WARN,
20293                                             "Application(s) accessing "
20294                                             "previously attached SATA "
20295                                             "device have to release "
20296                                             "it before newly inserted "
20297                                             "device can be made accessible."
20298                                             "at port %d:%d",
20299                                             cport, pmport);
20300                                         pmportinfo->pmport_event_flags |=
20301                                             SATA_EVNT_TARGET_NODE_CLEANUP;
20302                                         pmportinfo->pmport_tgtnode_clean =
20303                                             B_FALSE;
20304                                 }
20305                         }
20306                         if (sata_auto_online != 0) {
20307                                 pmportinfo->pmport_event_flags |=
20308                                     SATA_EVNT_AUTOONLINE_DEVICE;
20309                         }
20310 
20311                 }
20312         } else {
20313                 pmportinfo->pmport_dev_attach_time = 0;
20314         }
20315 
20316         event_flags = pmportinfo->pmport_event_flags;
20317         mutex_exit(&pmportinfo->pmport_mutex);
20318         if (event_flags != 0) {
20319                 mutex_enter(&sata_hba_inst->satahba_mutex);
20320                 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20321                 mutex_exit(&sata_hba_inst->satahba_mutex);
20322                 mutex_enter(&sata_mutex);
20323                 sata_event_pending |= SATA_EVNT_MAIN;
20324                 mutex_exit(&sata_mutex);
20325         }
20326 
20327         /* clear the reset_in_progress events */
20328         if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20329                 if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
20330                         /* must clear flags on cport */
20331                         sata_pmult_info_t *pminfo =
20332                             SATA_PMULT_INFO(sata_hba_inst,
20333                             saddr->cport);
20334                         pminfo->pmult_event_flags |=
20335                             SATA_EVNT_CLEAR_DEVICE_RESET;
20336                 }
20337         }
20338 }
20339 
20340 /*
20341  * Device Target Node Cleanup Event processing.
20342  * If the target node associated with a sata port device is in
20343  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
20344  * If the target node cannot be removed, the event flag is left intact,
20345  * so that event daemon may re-run this function later.
20346  *
20347  * This function cannot be called in interrupt context (it may sleep).
20348  *
20349  * NOTE: Processes cport events only, not port multiplier ports.
20350  */
20351 static void
20352 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20353     sata_address_t *saddr)
20354 {
20355         sata_cport_info_t *cportinfo;
20356         dev_info_t *tdip;
20357 
20358         SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20359             "Processing port %d device target node cleanup", saddr->cport);
20360 
20361         cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20362 
20363         /*
20364          * Check if there is target node for that device and it is in the
20365          * DEVI_DEVICE_REMOVED state. If so, release it.
20366          */
20367         tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20368             saddr->pmport);
20369         if (tdip != NULL) {
20370                 /*
20371                  * target node exists - check if it is target node of
20372                  * a removed device.
20373                  */
20374                 if (sata_check_device_removed(tdip) == B_TRUE) {
20375                         SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20376                             "sata_process_target_node_cleanup: "
20377                             "old device target node exists!", NULL);
20378                         /*
20379                          * Unconfigure and remove the target node
20380                          */
20381                         if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
20382                             NDI_SUCCESS) {
20383                                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20384                                     saddr->cport)->cport_mutex);
20385                                 cportinfo->cport_event_flags &=
20386                                     ~SATA_EVNT_TARGET_NODE_CLEANUP;
20387                                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20388                                     saddr->cport)->cport_mutex);
20389                                 return;
20390                         }
20391                         /*
20392                          * Event daemon will retry the cleanup later.
20393                          */
20394                         mutex_enter(&sata_hba_inst->satahba_mutex);
20395                         sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20396                         mutex_exit(&sata_hba_inst->satahba_mutex);
20397                         mutex_enter(&sata_mutex);
20398                         sata_event_pending |= SATA_EVNT_MAIN;
20399                         mutex_exit(&sata_mutex);
20400                 }
20401         } else {
20402                 if (saddr->qual == SATA_ADDR_CPORT ||
20403                     saddr->qual == SATA_ADDR_DCPORT) {
20404                         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20405                             saddr->cport)->cport_mutex);
20406                         cportinfo->cport_event_flags &=
20407                             ~SATA_EVNT_TARGET_NODE_CLEANUP;
20408                         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20409                             saddr->cport)->cport_mutex);
20410                 } else {
20411                         /* sanity check */
20412                         if (SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) !=
20413                             SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
20414                             saddr->cport) == NULL)
20415                                 return;
20416                         if (SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20417                             saddr->pmport) == NULL)
20418                                 return;
20419 
20420                         mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20421                             saddr->cport, saddr->pmport)->pmport_mutex);
20422                         SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20423                             saddr->pmport)->pmport_event_flags &=
20424                             ~SATA_EVNT_TARGET_NODE_CLEANUP;
20425                         mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20426                             saddr->cport, saddr->pmport)->pmport_mutex);
20427                 }
20428         }
20429 }
20430 
20431 /*
20432  * Device AutoOnline Event processing.
20433  * If attached device is to be onlined, an attempt is made to online this
20434  * device, but only if there is no lingering (old) target node present.
20435  * If the device cannot be onlined, the event flag is left intact,
20436  * so that event daemon may re-run this function later.
20437  *
20438  * This function cannot be called in interrupt context (it may sleep).
20439  *
20440  * NOTE: Processes cport events only, not port multiplier ports.
20441  */
20442 static void
20443 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
20444     sata_address_t *saddr)
20445 {
20446         sata_cport_info_t *cportinfo;
20447         sata_drive_info_t *sdinfo;
20448         sata_device_t sata_device;
20449         dev_info_t *tdip;
20450 
20451         SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20452             "Processing port %d attached device auto-onlining", saddr->cport);
20453 
20454         cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20455 
20456         /*
20457          * Check if device is present and recognized. If not, reset event.
20458          */
20459         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20460         if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
20461                 /* Nothing to online */
20462                 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20463                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20464                     saddr->cport)->cport_mutex);
20465                 return;
20466         }
20467         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20468 
20469         /*
20470          * Check if there is target node for this device and if it is in the
20471          * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
20472          * the event for later processing.
20473          */
20474         tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20475             saddr->pmport);
20476         if (tdip != NULL) {
20477                 /*
20478                  * target node exists - check if it is target node of
20479                  * a removed device.
20480                  */
20481                 if (sata_check_device_removed(tdip) == B_TRUE) {
20482                         SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20483                             "sata_process_device_autoonline: "
20484                             "old device target node exists!", NULL);
20485                         /*
20486                          * Event daemon will retry device onlining later.
20487                          */
20488                         mutex_enter(&sata_hba_inst->satahba_mutex);
20489                         sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20490                         mutex_exit(&sata_hba_inst->satahba_mutex);
20491                         mutex_enter(&sata_mutex);
20492                         sata_event_pending |= SATA_EVNT_MAIN;
20493                         mutex_exit(&sata_mutex);
20494                         return;
20495                 }
20496                 /*
20497                  * If the target node is not in the 'removed" state, assume
20498                  * that it belongs to this device. There is nothing more to do,
20499                  * but reset the event.
20500                  */
20501         } else {
20502 
20503                 /*
20504                  * Try to online the device
20505                  * If there is any reset-related event, remove it. We are
20506                  * configuring the device and no state restoring is needed.
20507                  */
20508                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20509                     saddr->cport)->cport_mutex);
20510                 sata_device.satadev_addr = *saddr;
20511                 if (saddr->qual == SATA_ADDR_CPORT)
20512                         sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
20513                 else
20514                         sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
20515                 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
20516                 if (sdinfo != NULL) {
20517                         if (sdinfo->satadrv_event_flags &
20518                             (SATA_EVNT_DEVICE_RESET |
20519                             SATA_EVNT_INPROC_DEVICE_RESET))
20520                                 sdinfo->satadrv_event_flags = 0;
20521                         sdinfo->satadrv_event_flags |=
20522                             SATA_EVNT_CLEAR_DEVICE_RESET;
20523 
20524                         /* Need to create a new target node. */
20525                         cportinfo->cport_tgtnode_clean = B_TRUE;
20526                         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20527                             saddr->cport)->cport_mutex);
20528                         tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
20529                             sata_hba_inst, &sata_device.satadev_addr);
20530                         if (tdip == NULL) {
20531                                 /*
20532                                  * Configure (onlining) failed.
20533                                  * We will NOT retry
20534                                  */
20535                                 SATA_LOG_D((sata_hba_inst, CE_WARN,
20536                                     "sata_process_device_autoonline: "
20537                                     "configuring SATA device at port %d failed",
20538                                     saddr->cport));
20539                         }
20540                 } else {
20541                         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20542                             saddr->cport)->cport_mutex);
20543                 }
20544 
20545         }
20546         mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20547         cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20548         mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20549             saddr->cport)->cport_mutex);
20550 }
20551 
20552 
20553 static void
20554 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
20555     int hint)
20556 {
20557         char ap[MAXPATHLEN];
20558         nvlist_t *ev_attr_list = NULL;
20559         int err;
20560 
20561         /* Allocate and build sysevent attribute list */
20562         err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
20563         if (err != 0) {
20564                 SATA_LOG_D((sata_hba_inst, CE_WARN,
20565                     "sata_gen_sysevent: "
20566                     "cannot allocate memory for sysevent attributes\n"));
20567                 return;
20568         }
20569         /* Add hint attribute */
20570         err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
20571         if (err != 0) {
20572                 SATA_LOG_D((sata_hba_inst, CE_WARN,
20573                     "sata_gen_sysevent: "
20574                     "failed to add DR_HINT attr for sysevent"));
20575                 nvlist_free(ev_attr_list);
20576                 return;
20577         }
20578         /*
20579          * Add AP attribute.
20580          * Get controller pathname and convert it into AP pathname by adding
20581          * a target number.
20582          */
20583         (void) snprintf(ap, MAXPATHLEN, "/devices");
20584         (void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
20585         (void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
20586             SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
20587 
20588         err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
20589         if (err != 0) {
20590                 SATA_LOG_D((sata_hba_inst, CE_WARN,
20591                     "sata_gen_sysevent: "
20592                     "failed to add DR_AP_ID attr for sysevent"));
20593                 nvlist_free(ev_attr_list);
20594                 return;
20595         }
20596 
20597         /* Generate/log sysevent */
20598         err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
20599             ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
20600         if (err != DDI_SUCCESS) {
20601                 SATA_LOG_D((sata_hba_inst, CE_WARN,
20602                     "sata_gen_sysevent: "
20603                     "cannot log sysevent, err code %x\n", err));
20604         }
20605 
20606         nvlist_free(ev_attr_list);
20607 }
20608 
20609 
20610 
20611 
20612 /*
20613  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
20614  */
20615 static void
20616 sata_set_device_removed(dev_info_t *tdip)
20617 {
20618         int circ;
20619 
20620         ASSERT(tdip != NULL);
20621 
20622         ndi_devi_enter(tdip, &circ);
20623         mutex_enter(&DEVI(tdip)->devi_lock);
20624         DEVI_SET_DEVICE_REMOVED(tdip);
20625         mutex_exit(&DEVI(tdip)->devi_lock);
20626         ndi_devi_exit(tdip, circ);
20627 }
20628 
20629 
20630 /*
20631  * Set internal event instructing event daemon to try
20632  * to perform the target node cleanup.
20633  */
20634 static void
20635 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20636     sata_address_t *saddr)
20637 {
20638         if (saddr->qual == SATA_ADDR_CPORT ||
20639             saddr->qual == SATA_ADDR_DCPORT) {
20640                 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20641                     saddr->cport)->cport_mutex);
20642                 SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
20643                     SATA_EVNT_TARGET_NODE_CLEANUP;
20644                 SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
20645                     cport_tgtnode_clean = B_FALSE;
20646                 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20647                     saddr->cport)->cport_mutex);
20648         } else {
20649                 mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20650                     saddr->cport, saddr->pmport)->pmport_mutex);
20651                 SATA_PMPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport,
20652                     saddr->pmport) |= SATA_EVNT_TARGET_NODE_CLEANUP;
20653                 SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, saddr->pmport)->
20654                     pmport_tgtnode_clean = B_FALSE;
20655                 mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20656                     saddr->cport, saddr->pmport)->pmport_mutex);
20657         }
20658         mutex_enter(&sata_hba_inst->satahba_mutex);
20659         sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20660         mutex_exit(&sata_hba_inst->satahba_mutex);
20661         mutex_enter(&sata_mutex);
20662         sata_event_pending |= SATA_EVNT_MAIN;
20663         mutex_exit(&sata_mutex);
20664 }
20665 
20666 
20667 /*
20668  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
20669  * i.e. check if the target node state indicates that it belongs to a removed
20670  * device.
20671  *
20672  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
20673  * B_FALSE otherwise.
20674  */
20675 static boolean_t
20676 sata_check_device_removed(dev_info_t *tdip)
20677 {
20678         ASSERT(tdip != NULL);
20679 
20680         if (DEVI_IS_DEVICE_REMOVED(tdip))
20681                 return (B_TRUE);
20682         else
20683                 return (B_FALSE);
20684 }
20685 
20686 
20687 /*
20688  * Check for DMA error. Return B_TRUE if error, B_FALSE otherwise.
20689  */
20690 static boolean_t
20691 sata_check_for_dma_error(dev_info_t *dip, sata_pkt_txlate_t *spx)
20692 {
20693         int fm_capability = ddi_fm_capable(dip);
20694         ddi_fm_error_t de;
20695 
20696         if (fm_capability & DDI_FM_DMACHK_CAPABLE) {
20697                 if (spx->txlt_buf_dma_handle != NULL) {
20698                         ddi_fm_dma_err_get(spx->txlt_buf_dma_handle, &de,
20699                             DDI_FME_VERSION);
20700                         if (de.fme_status != DDI_SUCCESS)
20701                                 return (B_TRUE);
20702                 }
20703         }
20704         return (B_FALSE);
20705 }
20706 
20707 
20708 /* ************************ FAULT INJECTTION **************************** */
20709 
20710 #ifdef SATA_INJECT_FAULTS
20711 
20712 static  uint32_t sata_fault_count = 0;
20713 static  uint32_t sata_fault_suspend_count = 0;
20714 
20715 /*
20716  * Inject sata pkt fault
20717  * It modifies returned values of the sata packet.
20718  * It returns immediately if:
20719  * pkt fault injection is not enabled (via sata_inject_fault,
20720  * sata_inject_fault_count), or invalid fault is specified (sata_fault_type),
20721  * or pkt does not contain command to be faulted (set in sata_fault_cmd), or
20722  * pkt is not directed to specified fault controller/device
20723  * (sata_fault_ctrl_dev and sata_fault_device).
20724  * If fault controller is not specified, fault injection applies to all
20725  * controllers and devices.
20726  *
20727  * First argument is the pointer to the executed sata packet.
20728  * Second argument is a pointer to a value returned by the HBA tran_start
20729  * function.
20730  * Third argument specifies injected error. Injected sata packet faults
20731  * are the satapkt_reason values.
20732  * SATA_PKT_BUSY                -1      Not completed, busy
20733  * SATA_PKT_DEV_ERROR           1       Device reported error
20734  * SATA_PKT_QUEUE_FULL          2       Not accepted, queue full
20735  * SATA_PKT_PORT_ERROR          3       Not completed, port error
20736  * SATA_PKT_CMD_UNSUPPORTED     4       Cmd unsupported
20737  * SATA_PKT_ABORTED             5       Aborted by request
20738  * SATA_PKT_TIMEOUT             6       Operation timeut
20739  * SATA_PKT_RESET               7       Aborted by reset request
20740  *
20741  * Additional global variables affecting the execution:
20742  *
20743  * sata_inject_fault_count variable specifies number of times in row the
20744  * error is injected. Value of -1 specifies permanent fault, ie. every time
20745  * the fault injection point is reached, the fault is injected and a pause
20746  * between fault injection specified by sata_inject_fault_pause_count is
20747  * ignored). Fault injection routine decrements sata_inject_fault_count
20748  * (if greater than zero) until it reaches 0. No fault is injected when
20749  * sata_inject_fault_count is 0 (zero).
20750  *
20751  * sata_inject_fault_pause_count variable specifies number of times a fault
20752  * injection is bypassed (pause between fault injections).
20753  * If set to 0, a fault is injected only a number of times specified by
20754  * sata_inject_fault_count.
20755  *
20756  * The fault counts are static, so for periodic errors they have to be manually
20757  * reset to start repetition sequence from scratch.
20758  * If the original value returned by the HBA tran_start function is not
20759  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
20760  * is injected (to avoid masking real problems);
20761  *
20762  * NOTE: In its current incarnation, this function should be invoked only for
20763  * commands executed in SYNCHRONOUS mode.
20764  */
20765 
20766 
20767 static void
20768 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault)
20769 {
20770 
20771         if (sata_inject_fault != SATA_INJECT_PKT_FAULT)
20772                 return;
20773 
20774         if (sata_inject_fault_count == 0)
20775                 return;
20776 
20777         if (fault == 0)
20778                 return;
20779 
20780         if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg)
20781                 return;
20782 
20783         if (sata_fault_ctrl != NULL) {
20784                 sata_pkt_txlate_t *spx =
20785                     (sata_pkt_txlate_t *)spkt->satapkt_framework_private;
20786 
20787                 if (sata_fault_ctrl != NULL && sata_fault_ctrl !=
20788                     spx->txlt_sata_hba_inst->satahba_dip)
20789                         return;
20790 
20791                 if (sata_fault_device.satadev_addr.cport !=
20792                     spkt->satapkt_device.satadev_addr.cport ||
20793                     sata_fault_device.satadev_addr.pmport !=
20794                     spkt->satapkt_device.satadev_addr.pmport ||
20795                     sata_fault_device.satadev_addr.qual !=
20796                     spkt->satapkt_device.satadev_addr.qual)
20797                         return;
20798         }
20799 
20800         /* Modify pkt return parameters */
20801         if (*rval != SATA_TRAN_ACCEPTED ||
20802             spkt->satapkt_reason != SATA_PKT_COMPLETED) {
20803                 sata_fault_count = 0;
20804                 sata_fault_suspend_count = 0;
20805                 return;
20806         }
20807         if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
20808                 /* Pause in the injection */
20809                 sata_fault_suspend_count -= 1;
20810                 return;
20811         }
20812 
20813         if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
20814                 /*
20815                  * Init inject fault cycle. If fault count is set to -1,
20816                  * it is a permanent fault.
20817                  */
20818                 if (sata_inject_fault_count != -1) {
20819                         sata_fault_count = sata_inject_fault_count;
20820                         sata_fault_suspend_count =
20821                             sata_inject_fault_pause_count;
20822                         if (sata_fault_suspend_count == 0)
20823                                 sata_inject_fault_count = 0;
20824                 }
20825         }
20826 
20827         if (sata_fault_count != 0)
20828                 sata_fault_count -= 1;
20829 
20830         switch (fault) {
20831         case SATA_PKT_BUSY:
20832                 *rval = SATA_TRAN_BUSY;
20833                 spkt->satapkt_reason = SATA_PKT_BUSY;
20834                 break;
20835 
20836         case SATA_PKT_QUEUE_FULL:
20837                 *rval = SATA_TRAN_QUEUE_FULL;
20838                 spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
20839                 break;
20840 
20841         case SATA_PKT_CMD_UNSUPPORTED:
20842                 *rval = SATA_TRAN_CMD_UNSUPPORTED;
20843                 spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
20844                 break;
20845 
20846         case SATA_PKT_PORT_ERROR:
20847                 /* This is "rejected" command */
20848                 *rval = SATA_TRAN_PORT_ERROR;
20849                 spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
20850                 /* Additional error setup could be done here - port state */
20851                 break;
20852 
20853         case SATA_PKT_DEV_ERROR:
20854                 spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
20855                 /*
20856                  * Additional error setup could be done here
20857                  */
20858                 break;
20859 
20860         case SATA_PKT_ABORTED:
20861                 spkt->satapkt_reason = SATA_PKT_ABORTED;
20862                 break;
20863 
20864         case SATA_PKT_TIMEOUT:
20865                 spkt->satapkt_reason = SATA_PKT_TIMEOUT;
20866                 /* Additional error setup could be done here */
20867                 break;
20868 
20869         case SATA_PKT_RESET:
20870                 spkt->satapkt_reason = SATA_PKT_RESET;
20871                 /*
20872                  * Additional error setup could be done here - device reset
20873                  */
20874                 break;
20875 
20876         default:
20877                 break;
20878         }
20879 }
20880 
20881 #endif
20882 
20883 /*
20884  * SATA Trace Ring Buffer
20885  * ----------------------
20886  *
20887  * Overview
20888  *
20889  * The SATA trace ring buffer is a ring buffer created and managed by
20890  * the SATA framework module that can be used by any module or driver
20891  * within the SATA framework to store debug messages.
20892  *
20893  * Ring Buffer Interfaces:
20894  *
20895  *      sata_vtrace_debug()     <-- Adds debug message to ring buffer
20896  *      sata_trace_debug()      <-- Wraps varargs into sata_vtrace_debug()
20897  *
20898  *      Note that the sata_trace_debug() interface was created to give
20899  *      consumers the flexibilty of sending debug messages to ring buffer
20900  *      as variable arguments.  Consumers can send type va_list debug
20901  *      messages directly to sata_vtrace_debug(). The sata_trace_debug()
20902  *      and sata_vtrace_debug() relationship is similar to that of
20903  *      cmn_err(9F) and vcmn_err(9F).
20904  *
20905  * Below is a diagram of the SATA trace ring buffer interfaces and
20906  * sample consumers:
20907  *
20908  * +---------------------------------+
20909  * |    o  o  SATA Framework Module  |
20910  * | o  SATA  o     +------------------+      +------------------+
20911  * |o   Trace  o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1|
20912  * |o   R-Buf  o    |sata_trace_debug  |<--+  +------------------+
20913  * | o        o     +------------------+   |  +------------------+
20914  * |    o  o                ^        |     +--|SATA HBA Driver #2|
20915  * |                        |        |        +------------------+
20916  * |           +------------------+  |
20917  * |           |SATA Debug Message|  |
20918  * |           +------------------+  |
20919  * +---------------------------------+
20920  *
20921  * Supporting Routines:
20922  *
20923  *      sata_trace_rbuf_alloc() <-- Initializes ring buffer
20924  *      sata_trace_rbuf_free()  <-- Destroys ring buffer
20925  *      sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer
20926  *      sata_trace_dmsg_free()  <-- Destroys content of ring buffer
20927  *
20928  * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE.
20929  * The ring buffer size can be adjusted by setting dmsg_ring_size in
20930  * /etc/system to desired size in unit of bytes.
20931  *
20932  * The individual debug message size in the ring buffer is restricted
20933  * to DMSG_BUF_SIZE.
20934  */
20935 void
20936 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap)
20937 {
20938         sata_trace_dmsg_t *dmsg;
20939 
20940         if (sata_debug_rbuf == NULL) {
20941                 return;
20942         }
20943 
20944         /*
20945          * If max size of ring buffer is smaller than size
20946          * required for one debug message then just return
20947          * since we have no room for the debug message.
20948          */
20949         if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) {
20950                 return;
20951         }
20952 
20953         mutex_enter(&sata_debug_rbuf->lock);
20954 
20955         /* alloc or reuse on ring buffer */
20956         dmsg = sata_trace_dmsg_alloc();
20957 
20958         if (dmsg == NULL) {
20959                 /* resource allocation failed */
20960                 mutex_exit(&sata_debug_rbuf->lock);
20961                 return;
20962         }
20963 
20964         dmsg->dip = dip;
20965         gethrestime(&dmsg->timestamp);
20966 
20967         (void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap);
20968 
20969         mutex_exit(&sata_debug_rbuf->lock);
20970 }
20971 
20972 void
20973 sata_trace_debug(dev_info_t *dip, const char *fmt, ...)
20974 {
20975         va_list ap;
20976 
20977         va_start(ap, fmt);
20978         sata_vtrace_debug(dip, fmt, ap);
20979         va_end(ap);
20980 }
20981 
20982 /*
20983  * This routine is used to manage debug messages
20984  * on ring buffer.
20985  */
20986 static sata_trace_dmsg_t *
20987 sata_trace_dmsg_alloc(void)
20988 {
20989         sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp;
20990 
20991         if (sata_debug_rbuf->looped == TRUE) {
20992                 sata_debug_rbuf->dmsgp = dmsg->next;
20993                 return (sata_debug_rbuf->dmsgp);
20994         }
20995 
20996         /*
20997          * If we're looping for the first time,
20998          * connect the ring.
20999          */
21000         if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) >
21001             sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) {
21002                 dmsg->next = sata_debug_rbuf->dmsgh;
21003                 sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh;
21004                 sata_debug_rbuf->looped = TRUE;
21005                 return (sata_debug_rbuf->dmsgp);
21006         }
21007 
21008         /* If we've gotten this far then memory allocation is needed */
21009         dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP);
21010         if (dmsg_alloc == NULL) {
21011                 sata_debug_rbuf->allocfailed++;
21012                 return (dmsg_alloc);
21013         } else {
21014                 sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t);
21015         }
21016 
21017         if (sata_debug_rbuf->dmsgp != NULL) {
21018                 dmsg->next = dmsg_alloc;
21019                 sata_debug_rbuf->dmsgp = dmsg->next;
21020                 return (sata_debug_rbuf->dmsgp);
21021         } else {
21022                 /*
21023                  * We should only be here if we're initializing
21024                  * the ring buffer.
21025                  */
21026                 if (sata_debug_rbuf->dmsgh == NULL) {
21027                         sata_debug_rbuf->dmsgh = dmsg_alloc;
21028                 } else {
21029                         /* Something is wrong */
21030                         kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t));
21031                         return (NULL);
21032                 }
21033 
21034                 sata_debug_rbuf->dmsgp = dmsg_alloc;
21035                 return (sata_debug_rbuf->dmsgp);
21036         }
21037 }
21038 
21039 
21040 /*
21041  * Free all messages on debug ring buffer.
21042  */
21043 static void
21044 sata_trace_dmsg_free(void)
21045 {
21046         sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh;
21047 
21048         while (dmsg != NULL) {
21049                 dmsg_next = dmsg->next;
21050                 kmem_free(dmsg, sizeof (sata_trace_dmsg_t));
21051 
21052                 /*
21053                  * If we've looped around the ring than we're done.
21054                  */
21055                 if (dmsg_next == sata_debug_rbuf->dmsgh) {
21056                         break;
21057                 } else {
21058                         dmsg = dmsg_next;
21059                 }
21060         }
21061 }
21062 
21063 
21064 /*
21065  * This function can block
21066  */
21067 static void
21068 sata_trace_rbuf_alloc(void)
21069 {
21070         sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP);
21071 
21072         mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL);
21073 
21074         if (dmsg_ring_size > 0) {
21075                 sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size;
21076         }
21077 }
21078 
21079 
21080 static void
21081 sata_trace_rbuf_free(void)
21082 {
21083         sata_trace_dmsg_free();
21084         mutex_destroy(&sata_debug_rbuf->lock);
21085         kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t));
21086 }
21087 
21088 /*
21089  * If SATA_DEBUG is not defined then this routine is called instead
21090  * of sata_log() via the SATA_LOG_D macro.
21091  */
21092 static void
21093 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level,
21094     const char *fmt, ...)
21095 {
21096 #ifndef __lock_lint
21097         _NOTE(ARGUNUSED(level))
21098 #endif
21099 
21100         dev_info_t *dip = NULL;
21101         va_list ap;
21102 
21103         if (sata_hba_inst != NULL) {
21104                 dip = SATA_DIP(sata_hba_inst);
21105         }
21106 
21107         va_start(ap, fmt);
21108         sata_vtrace_debug(dip, fmt, ap);
21109         va_end(ap);
21110 }