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 2010 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 28 /* 29 * SATA Framework 30 * Generic SATA Host Adapter Implementation 31 */ 32 33 #include <sys/conf.h> 34 #include <sys/file.h> 35 #include <sys/ddi.h> 36 #include <sys/sunddi.h> 37 #include <sys/modctl.h> 38 #include <sys/cmn_err.h> 39 #include <sys/errno.h> 40 #include <sys/thread.h> 41 #include <sys/kstat.h> 42 #include <sys/note.h> 43 #include <sys/sysevent.h> 44 #include <sys/sysevent/eventdefs.h> 45 #include <sys/sysevent/dr.h> 46 #include <sys/taskq.h> 47 #include <sys/disp.h> 48 49 #include <sys/sata/impl/sata.h> 50 #include <sys/sata/sata_hba.h> 51 #include <sys/sata/sata_defs.h> 52 #include <sys/sata/sata_cfgadm.h> 53 #include <sys/sata/sata_blacklist.h> 54 #include <sys/sata/sata_satl.h> 55 56 #include <sys/scsi/impl/spc3_types.h> 57 58 /* Debug flags - defined in sata.h */ 59 int sata_debug_flags = 0; 60 int sata_msg = 0; 61 62 /* 63 * Flags enabling selected SATA HBA framework functionality 64 */ 65 #define SATA_ENABLE_QUEUING 1 66 #define SATA_ENABLE_NCQ 2 67 #define SATA_ENABLE_PROCESS_EVENTS 4 68 #define SATA_ENABLE_PMULT_FBS 8 /* FIS-Based Switching */ 69 int sata_func_enable = 70 SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ; 71 72 /* 73 * Global variable setting default maximum queue depth (NCQ or TCQ) 74 * Note:minimum queue depth is 1 75 */ 76 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */ 77 78 /* 79 * Currently used default NCQ/TCQ queue depth. It is set-up during the driver 80 * initialization, using value from sata_max_queue_depth 81 * It is adjusted to minimum supported by the controller and by the device, 82 * if queueing is enabled. 83 */ 84 static int sata_current_max_qdepth; 85 86 /* 87 * Global variable determining the default behavior after device hotpluggin. 88 * If non-zero, the hotplugged device is onlined (if possible) without explicit 89 * IOCTL request (AP_CONFIGURE). 90 * If zero, hotplugged device is identified, but not onlined. 91 * Enabling (AP_CONNECT) device port with an attached device does not result 92 * in device onlining regardless of the flag setting 93 */ 94 int sata_auto_online = 0; 95 96 #ifdef SATA_DEBUG 97 98 #define SATA_LOG_D(args) sata_log args 99 uint64_t mbuf_count = 0; 100 uint64_t mbuffail_count = 0; 101 102 sata_atapi_cmd_t sata_atapi_trace[64]; 103 uint32_t sata_atapi_trace_index = 0; 104 int sata_atapi_trace_save = 1; 105 static void sata_save_atapi_trace(sata_pkt_txlate_t *, int); 106 #define SATAATAPITRACE(spx, count) if (sata_atapi_trace_save) \ 107 sata_save_atapi_trace(spx, count); 108 109 #else 110 #define SATA_LOG_D(args) sata_trace_log args 111 #define SATAATAPITRACE(spx, count) 112 #endif 113 114 #if 0 115 static void 116 sata_test_atapi_packet_command(sata_hba_inst_t *, int); 117 #endif 118 119 #ifdef SATA_INJECT_FAULTS 120 121 #define SATA_INJECT_PKT_FAULT 1 122 uint32_t sata_inject_fault = 0; 123 124 uint32_t sata_inject_fault_count = 0; 125 uint32_t sata_inject_fault_pause_count = 0; 126 uint32_t sata_fault_type = 0; 127 uint32_t sata_fault_cmd = 0; 128 dev_info_t *sata_fault_ctrl = NULL; 129 sata_device_t sata_fault_device; 130 131 static void sata_inject_pkt_fault(sata_pkt_t *, int *, int); 132 133 #endif 134 135 #define LEGACY_HWID_LEN 64 /* Model (40) + Serial (20) + pad */ 136 137 static char sata_rev_tag[] = {"1.46"}; 138 139 /* 140 * SATA cb_ops functions 141 */ 142 static int sata_hba_open(dev_t *, int, int, cred_t *); 143 static int sata_hba_close(dev_t, int, int, cred_t *); 144 static int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 145 146 /* 147 * SCSA required entry points 148 */ 149 static int sata_scsi_tgt_init(dev_info_t *, dev_info_t *, 150 scsi_hba_tran_t *, struct scsi_device *); 151 static int sata_scsi_tgt_probe(struct scsi_device *, 152 int (*callback)(void)); 153 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *, 154 scsi_hba_tran_t *, struct scsi_device *); 155 static int sata_scsi_start(struct scsi_address *, struct scsi_pkt *); 156 static int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *); 157 static int sata_scsi_reset(struct scsi_address *, int); 158 static int sata_scsi_getcap(struct scsi_address *, char *, int); 159 static int sata_scsi_setcap(struct scsi_address *, char *, int, int); 160 static struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *, 161 struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t), 162 caddr_t); 163 static void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *); 164 static void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *); 165 static void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *); 166 167 /* 168 * SATA HBA interface functions are defined in sata_hba.h header file 169 */ 170 171 /* Event processing functions */ 172 static void sata_event_daemon(void *); 173 static void sata_event_thread_control(int); 174 static void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst); 175 static void sata_process_pmult_events(sata_hba_inst_t *, uint8_t); 176 static void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *); 177 static void sata_process_pmdevice_reset(sata_hba_inst_t *, sata_address_t *); 178 static void sata_process_port_failed_event(sata_hba_inst_t *, 179 sata_address_t *); 180 static void sata_process_port_link_events(sata_hba_inst_t *, 181 sata_address_t *); 182 static void sata_process_pmport_link_events(sata_hba_inst_t *, 183 sata_address_t *); 184 static void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *); 185 static void sata_process_pmdevice_detached(sata_hba_inst_t *, 186 sata_address_t *); 187 static void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *); 188 static void sata_process_pmdevice_attached(sata_hba_inst_t *, 189 sata_address_t *); 190 static void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *); 191 static void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *); 192 static void sata_process_target_node_cleanup(sata_hba_inst_t *, 193 sata_address_t *); 194 static void sata_process_device_autoonline(sata_hba_inst_t *, 195 sata_address_t *saddr); 196 197 /* 198 * Local translation functions 199 */ 200 static int sata_txlt_inquiry(sata_pkt_txlate_t *); 201 static int sata_txlt_test_unit_ready(sata_pkt_txlate_t *); 202 static int sata_txlt_start_stop_unit(sata_pkt_txlate_t *); 203 static int sata_txlt_read_capacity(sata_pkt_txlate_t *); 204 static int sata_txlt_request_sense(sata_pkt_txlate_t *); 205 static int sata_txlt_read(sata_pkt_txlate_t *); 206 static int sata_txlt_write(sata_pkt_txlate_t *); 207 static int sata_txlt_log_sense(sata_pkt_txlate_t *); 208 static int sata_txlt_log_select(sata_pkt_txlate_t *); 209 static int sata_txlt_mode_sense(sata_pkt_txlate_t *); 210 static int sata_txlt_mode_select(sata_pkt_txlate_t *); 211 static int sata_txlt_ata_pass_thru(sata_pkt_txlate_t *); 212 static int sata_txlt_synchronize_cache(sata_pkt_txlate_t *); 213 static int sata_txlt_write_buffer(sata_pkt_txlate_t *); 214 static int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *); 215 216 static int sata_hba_start(sata_pkt_txlate_t *, int *); 217 static int sata_txlt_invalid_command(sata_pkt_txlate_t *); 218 static int sata_txlt_check_condition(sata_pkt_txlate_t *, uchar_t, uchar_t); 219 static int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *); 220 static int sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *); 221 static void sata_txlt_rw_completion(sata_pkt_t *); 222 static void sata_txlt_nodata_cmd_completion(sata_pkt_t *); 223 static void sata_txlt_apt_completion(sata_pkt_t *sata_pkt); 224 static void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *); 225 static int sata_emul_rw_completion(sata_pkt_txlate_t *); 226 static void sata_fill_ata_return_desc(sata_pkt_t *, uint8_t, uint8_t, 227 uint8_t); 228 static struct scsi_extended_sense *sata_immediate_error_response( 229 sata_pkt_txlate_t *, int); 230 static struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *); 231 232 static int sata_txlt_atapi(sata_pkt_txlate_t *); 233 static void sata_txlt_atapi_completion(sata_pkt_t *); 234 235 /* 236 * Local functions for ioctl 237 */ 238 static int32_t sata_get_port_num(sata_hba_inst_t *, struct devctl_iocdata *); 239 static void sata_cfgadm_state(sata_hba_inst_t *, int32_t, 240 devctl_ap_state_t *); 241 static dev_info_t *sata_get_target_dip(dev_info_t *, uint8_t, uint8_t); 242 static dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *); 243 static dev_info_t *sata_devt_to_devinfo(dev_t); 244 static int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *); 245 static int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *); 246 static int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *); 247 static int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *); 248 static int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *); 249 static int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *); 250 static int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *); 251 static int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *); 252 static int sata_ioctl_reset_all(sata_hba_inst_t *); 253 static int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *); 254 static int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *, 255 sata_ioctl_data_t *, int mode); 256 static int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *, 257 sata_ioctl_data_t *, int mode); 258 static int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *, 259 sata_ioctl_data_t *, int mode); 260 static int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *, 261 sata_ioctl_data_t *, int mode); 262 static int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *, 263 sata_device_t *, sata_ioctl_data_t *, int mode); 264 265 /* 266 * Local functions 267 */ 268 static void sata_remove_hba_instance(dev_info_t *); 269 static int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *); 270 static void sata_probe_ports(sata_hba_inst_t *); 271 static void sata_probe_pmports(sata_hba_inst_t *, uint8_t); 272 static int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int); 273 static int sata_reprobe_pmult(sata_hba_inst_t *, sata_device_t *, int); 274 static int sata_reprobe_pmport(sata_hba_inst_t *, sata_device_t *, int); 275 static int sata_alloc_pmult(sata_hba_inst_t *, sata_device_t *); 276 static void sata_free_pmult(sata_hba_inst_t *, sata_device_t *); 277 static int sata_add_device(dev_info_t *, sata_hba_inst_t *, sata_device_t *); 278 static int sata_offline_device(sata_hba_inst_t *, sata_device_t *, 279 sata_drive_info_t *); 280 static dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *, 281 sata_address_t *); 282 static void sata_remove_target_node(sata_hba_inst_t *, 283 sata_address_t *); 284 static int sata_validate_scsi_address(sata_hba_inst_t *, 285 struct scsi_address *, sata_device_t *); 286 static int sata_validate_sata_address(sata_hba_inst_t *, int, int, int); 287 static sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t)); 288 static void sata_pkt_free(sata_pkt_txlate_t *); 289 static int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t), 290 caddr_t, ddi_dma_attr_t *); 291 static void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *); 292 static int sata_probe_device(sata_hba_inst_t *, sata_device_t *); 293 static sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *, 294 sata_device_t *); 295 static int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *); 296 static void sata_reidentify_device(sata_pkt_txlate_t *); 297 static struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int); 298 static void sata_free_local_buffer(sata_pkt_txlate_t *); 299 static uint64_t sata_check_capacity(sata_drive_info_t *); 300 void sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *, 301 ddi_dma_attr_t *); 302 static int sata_fetch_device_identify_data(sata_hba_inst_t *, 303 sata_drive_info_t *); 304 static void sata_update_port_info(sata_hba_inst_t *, sata_device_t *); 305 static void sata_update_pmport_info(sata_hba_inst_t *, sata_device_t *); 306 static void sata_update_port_scr(sata_port_scr_t *, sata_device_t *); 307 static int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *); 308 static int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int); 309 static int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int); 310 static int sata_set_drive_features(sata_hba_inst_t *, 311 sata_drive_info_t *, int flag); 312 static void sata_init_write_cache_mode(sata_drive_info_t *sdinfo); 313 static int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *); 314 static void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *, 315 uint8_t *); 316 static int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *, 317 struct scsi_inquiry *); 318 static int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *); 319 static int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *); 320 static int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *); 321 static int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *); 322 static int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *); 323 static int sata_mode_select_page_8(sata_pkt_txlate_t *, 324 struct mode_cache_scsi3 *, int, int *, int *, int *); 325 static int sata_mode_select_page_1a(sata_pkt_txlate_t *, 326 struct mode_info_power_cond *, int, int *, int *, int *); 327 static int sata_mode_select_page_1c(sata_pkt_txlate_t *, 328 struct mode_info_excpt_page *, int, int *, int *, int *); 329 static int sata_mode_select_page_30(sata_pkt_txlate_t *, 330 struct mode_acoustic_management *, int, int *, int *, int *); 331 332 static int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *); 333 static int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *, 334 sata_hba_inst_t *); 335 static int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *, 336 sata_hba_inst_t *); 337 static int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *, 338 sata_hba_inst_t *); 339 static int sata_build_lsense_page_0e(sata_drive_info_t *, uint8_t *, 340 sata_pkt_txlate_t *); 341 342 static void sata_set_arq_data(sata_pkt_t *); 343 static void sata_build_read_verify_cmd(sata_cmd_t *, uint16_t, uint64_t); 344 static void sata_build_generic_cmd(sata_cmd_t *, uint8_t); 345 static uint8_t sata_get_standby_timer(uint8_t *timer); 346 347 static void sata_save_drive_settings(sata_drive_info_t *); 348 static void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *); 349 static void sata_show_pmult_info(sata_hba_inst_t *, sata_device_t *); 350 static void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...); 351 static void sata_trace_log(sata_hba_inst_t *, uint_t, const char *fmt, ...); 352 static int sata_fetch_smart_return_status(sata_hba_inst_t *, 353 sata_drive_info_t *); 354 static int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *, 355 struct smart_data *); 356 static int sata_smart_selftest_log(sata_hba_inst_t *, 357 sata_drive_info_t *, 358 struct smart_selftest_log *); 359 static int sata_ext_smart_selftest_read_log(sata_hba_inst_t *, 360 sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t); 361 static int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *, 362 uint8_t *, uint8_t, uint8_t); 363 static int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *, 364 struct read_log_ext_directory *); 365 static void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int); 366 static void sata_xlate_errors(sata_pkt_txlate_t *); 367 static void sata_decode_device_error(sata_pkt_txlate_t *, 368 struct scsi_extended_sense *); 369 static void sata_set_device_removed(dev_info_t *); 370 static boolean_t sata_check_device_removed(dev_info_t *); 371 static void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *); 372 static int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *, 373 sata_drive_info_t *); 374 static int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *, 375 sata_drive_info_t *); 376 static void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *); 377 static void sata_fixed_sense_data_preset(struct scsi_extended_sense *); 378 static void sata_target_devid_register(dev_info_t *, sata_drive_info_t *); 379 static int sata_check_modser(char *, int); 380 381 382 383 /* 384 * SATA Framework will ignore SATA HBA driver cb_ops structure and 385 * register following one with SCSA framework. 386 * Open & close are provided, so scsi framework will not use its own 387 */ 388 static struct cb_ops sata_cb_ops = { 389 sata_hba_open, /* open */ 390 sata_hba_close, /* close */ 391 nodev, /* strategy */ 392 nodev, /* print */ 393 nodev, /* dump */ 394 nodev, /* read */ 395 nodev, /* write */ 396 sata_hba_ioctl, /* ioctl */ 397 nodev, /* devmap */ 398 nodev, /* mmap */ 399 nodev, /* segmap */ 400 nochpoll, /* chpoll */ 401 ddi_prop_op, /* cb_prop_op */ 402 0, /* streamtab */ 403 D_NEW | D_MP, /* cb_flag */ 404 CB_REV, /* rev */ 405 nodev, /* aread */ 406 nodev /* awrite */ 407 }; 408 409 410 extern struct mod_ops mod_miscops; 411 extern uchar_t scsi_cdb_size[]; 412 413 static struct modlmisc modlmisc = { 414 &mod_miscops, /* Type of module */ 415 "SATA Module" /* module name */ 416 }; 417 418 419 static struct modlinkage modlinkage = { 420 MODREV_1, 421 (void *)&modlmisc, 422 NULL 423 }; 424 425 /* 426 * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero, 427 * i.e. when scsi_pkt has not timeout specified. 428 */ 429 static int sata_default_pkt_time = 60; /* 60 seconds */ 430 431 /* 432 * Intermediate buffer device access attributes - they are required, 433 * but not necessarily used. 434 */ 435 static ddi_device_acc_attr_t sata_acc_attr = { 436 DDI_DEVICE_ATTR_V0, 437 DDI_STRUCTURE_LE_ACC, 438 DDI_STRICTORDER_ACC 439 }; 440 441 442 /* 443 * Mutexes protecting structures in multithreaded operations. 444 * Because events are relatively rare, a single global mutex protecting 445 * data structures should be sufficient. To increase performance, add 446 * separate mutex per each sata port and use global mutex only to protect 447 * common data structures. 448 */ 449 static kmutex_t sata_mutex; /* protects sata_hba_list */ 450 static kmutex_t sata_log_mutex; /* protects log */ 451 452 static char sata_log_buf[256]; 453 454 /* 455 * sata trace debug 456 */ 457 static sata_trace_rbuf_t *sata_debug_rbuf; 458 static sata_trace_dmsg_t *sata_trace_dmsg_alloc(void); 459 static void sata_trace_dmsg_free(void); 460 static void sata_trace_rbuf_alloc(void); 461 static void sata_trace_rbuf_free(void); 462 463 int dmsg_ring_size = DMSG_RING_SIZE; 464 465 /* Default write cache setting for SATA hard disks */ 466 int sata_write_cache = 1; /* enabled */ 467 468 /* Default write cache setting for SATA ATAPI CD/DVD */ 469 int sata_atapicdvd_write_cache = 1; /* enabled */ 470 471 /* Default write cache setting for SATA ATAPI tape */ 472 int sata_atapitape_write_cache = 1; /* enabled */ 473 474 /* Default write cache setting for SATA ATAPI disk */ 475 int sata_atapidisk_write_cache = 1; /* enabled */ 476 477 /* 478 * Linked list of HBA instances 479 */ 480 static sata_hba_inst_t *sata_hba_list = NULL; 481 static sata_hba_inst_t *sata_hba_list_tail = NULL; 482 /* 483 * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran 484 * structure and in sata soft state. 485 */ 486 487 /* 488 * Event daemon related variables 489 */ 490 static kmutex_t sata_event_mutex; 491 static kcondvar_t sata_event_cv; 492 static kthread_t *sata_event_thread = NULL; 493 static int sata_event_thread_terminate = 0; 494 static int sata_event_pending = 0; 495 static int sata_event_thread_active = 0; 496 extern pri_t minclsyspri; 497 498 /* 499 * NCQ error recovery command 500 */ 501 static const sata_cmd_t sata_rle_cmd = { 502 SATA_CMD_REV, 503 NULL, 504 { 505 SATA_DIR_READ 506 }, 507 ATA_ADDR_LBA48, 508 0, 509 0, 510 0, 511 0, 512 0, 513 1, 514 READ_LOG_EXT_NCQ_ERROR_RECOVERY, 515 0, 516 0, 517 0, 518 SATAC_READ_LOG_EXT, 519 0, 520 0, 521 0, 522 }; 523 524 /* 525 * ATAPI error recovery CDB 526 */ 527 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = { 528 SCMD_REQUEST_SENSE, 529 0, /* Only fixed RQ format is supported */ 530 0, 531 0, 532 SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */ 533 0 534 }; 535 536 537 /* Warlock directives */ 538 539 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran)) 540 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device)) 541 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops)) 542 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense)) 543 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status)) 544 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr)) 545 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t)) 546 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state)) 547 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state)) 548 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list)) 549 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list)) 550 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next)) 551 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev)) 552 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \ 553 sata_hba_inst::satahba_scsi_tran)) 554 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran)) 555 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip)) 556 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached)) 557 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port)) 558 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex, 559 sata_hba_inst::satahba_event_flags)) 560 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 561 sata_cport_info::cport_devp)) 562 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp)) 563 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr)) 564 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 565 sata_cport_info::cport_dev_type)) 566 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type)) 567 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 568 sata_cport_info::cport_state)) 569 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state)) 570 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 571 sata_pmport_info::pmport_state)) 572 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state)) 573 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 574 sata_pmport_info::pmport_dev_type)) 575 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type)) 576 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 577 sata_pmport_info::pmport_sata_drive)) 578 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 579 sata_pmport_info::pmport_tgtnode_clean)) 580 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 581 sata_pmport_info::pmport_event_flags)) 582 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive)) 583 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port)) 584 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports)) 585 #ifdef SATA_DEBUG 586 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count)) 587 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count)) 588 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace)) 589 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index)) 590 #endif 591 592 /* End of warlock directives */ 593 594 /* ************** loadable module configuration functions ************** */ 595 596 int 597 _init() 598 { 599 int rval; 600 601 mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL); 602 mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL); 603 mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL); 604 cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL); 605 sata_trace_rbuf_alloc(); 606 if ((rval = mod_install(&modlinkage)) != 0) { 607 #ifdef SATA_DEBUG 608 cmn_err(CE_WARN, "sata: _init: mod_install failed\n"); 609 #endif 610 sata_trace_rbuf_free(); 611 mutex_destroy(&sata_log_mutex); 612 cv_destroy(&sata_event_cv); 613 mutex_destroy(&sata_event_mutex); 614 mutex_destroy(&sata_mutex); 615 } 616 return (rval); 617 } 618 619 int 620 _fini() 621 { 622 int rval; 623 624 if ((rval = mod_remove(&modlinkage)) != 0) 625 return (rval); 626 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 return (rval); 633 } 634 635 int 636 _info(struct modinfo *modinfop) 637 { 638 return (mod_info(&modlinkage, modinfop)); 639 } 640 641 642 643 /* ********************* SATA HBA entry points ********************* */ 644 645 646 /* 647 * Called by SATA HBA from _init(). 648 * Registers HBA driver instance/sata framework pair with scsi framework, by 649 * calling scsi_hba_init(). 650 * 651 * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used 652 * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver 653 * cb_ops pointer in SATA HBA driver dev_ops structure. 654 * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors. 655 * 656 * Return status of the scsi_hba_init() is returned to a calling SATA HBA 657 * driver. 658 */ 659 int 660 sata_hba_init(struct modlinkage *modlp) 661 { 662 int rval; 663 struct dev_ops *hba_ops; 664 665 SATADBG1(SATA_DBG_HBA_IF, NULL, 666 "sata_hba_init: name %s \n", 667 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo); 668 /* 669 * Fill-up cb_ops and dev_ops when necessary 670 */ 671 hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops; 672 /* 673 * Provide pointer to SATA dev_ops 674 */ 675 hba_ops->devo_cb_ops = &sata_cb_ops; 676 677 /* 678 * Register SATA HBA with SCSI framework 679 */ 680 if ((rval = scsi_hba_init(modlp)) != 0) { 681 SATADBG1(SATA_DBG_HBA_IF, NULL, 682 "sata_hba_init: scsi hba init failed\n", NULL); 683 return (rval); 684 } 685 686 return (0); 687 } 688 689 690 /* HBA attach stages */ 691 #define HBA_ATTACH_STAGE_SATA_HBA_INST 1 692 #define HBA_ATTACH_STAGE_SCSI_ATTACHED 2 693 #define HBA_ATTACH_STAGE_SETUP 4 694 #define HBA_ATTACH_STAGE_LINKED 8 695 696 697 /* 698 * 699 * Called from SATA HBA driver's attach routine to attach an instance of 700 * the HBA. 701 * 702 * For DDI_ATTACH command: 703 * sata_hba_inst structure is allocated here and initialized with pointers to 704 * SATA framework implementation of required scsi tran functions. 705 * The scsi_tran's tran_hba_private field is used by SATA Framework to point 706 * to the soft structure (sata_hba_inst) allocated by SATA framework for 707 * SATA HBA instance related data. 708 * The scsi_tran's tran_hba_private field is used by SATA framework to 709 * store a pointer to per-HBA-instance of sata_hba_inst structure. 710 * The sata_hba_inst structure is cross-linked to scsi tran structure. 711 * Among other info, a pointer to sata_hba_tran structure is stored in 712 * sata_hba_inst. The sata_hba_inst structures for different HBA instances are 713 * linked together into the list, pointed to by sata_hba_list. 714 * On the first HBA instance attach the sata event thread is initialized. 715 * Attachment points are created for all SATA ports of the HBA being attached. 716 * All HBA instance's SATA ports are probed and type of plugged devices is 717 * determined. For each device of a supported type, a target node is created. 718 * 719 * DDI_SUCCESS is returned when attachment process is successful, 720 * DDI_FAILURE is returned otherwise. 721 * 722 * For DDI_RESUME command: 723 * Not implemented at this time (postponed until phase 2 of the development). 724 */ 725 int 726 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran, 727 ddi_attach_cmd_t cmd) 728 { 729 sata_hba_inst_t *sata_hba_inst; 730 scsi_hba_tran_t *scsi_tran = NULL; 731 int hba_attach_state = 0; 732 char taskq_name[MAXPATHLEN]; 733 734 SATADBG3(SATA_DBG_HBA_IF, NULL, 735 "sata_hba_attach: node %s (%s%d)\n", 736 ddi_node_name(dip), ddi_driver_name(dip), 737 ddi_get_instance(dip)); 738 739 if (cmd == DDI_RESUME) { 740 /* 741 * Postponed until phase 2 of the development 742 */ 743 return (DDI_FAILURE); 744 } 745 746 if (cmd != DDI_ATTACH) { 747 return (DDI_FAILURE); 748 } 749 750 /* cmd == DDI_ATTACH */ 751 752 if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) { 753 SATA_LOG_D((NULL, CE_WARN, 754 "sata_hba_attach: invalid sata_hba_tran")); 755 return (DDI_FAILURE); 756 } 757 /* 758 * Allocate and initialize SCSI tran structure. 759 * SATA copy of tran_bus_config is provided to create port nodes. 760 */ 761 scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP); 762 if (scsi_tran == NULL) 763 return (DDI_FAILURE); 764 /* 765 * Allocate soft structure for SATA HBA instance. 766 * There is a separate softstate for each HBA instance. 767 */ 768 sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP); 769 ASSERT(sata_hba_inst != NULL); /* this should not fail */ 770 mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL); 771 hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST; 772 773 /* 774 * scsi_trans's tran_hba_private is used by SATA Framework to point to 775 * soft structure allocated by SATA framework for 776 * SATA HBA instance related data. 777 */ 778 scsi_tran->tran_hba_private = sata_hba_inst; 779 scsi_tran->tran_tgt_private = NULL; 780 781 scsi_tran->tran_tgt_init = sata_scsi_tgt_init; 782 scsi_tran->tran_tgt_probe = sata_scsi_tgt_probe; 783 scsi_tran->tran_tgt_free = sata_scsi_tgt_free; 784 785 scsi_tran->tran_start = sata_scsi_start; 786 scsi_tran->tran_reset = sata_scsi_reset; 787 scsi_tran->tran_abort = sata_scsi_abort; 788 scsi_tran->tran_getcap = sata_scsi_getcap; 789 scsi_tran->tran_setcap = sata_scsi_setcap; 790 scsi_tran->tran_init_pkt = sata_scsi_init_pkt; 791 scsi_tran->tran_destroy_pkt = sata_scsi_destroy_pkt; 792 793 scsi_tran->tran_dmafree = sata_scsi_dmafree; 794 scsi_tran->tran_sync_pkt = sata_scsi_sync_pkt; 795 796 scsi_tran->tran_reset_notify = NULL; 797 scsi_tran->tran_get_bus_addr = NULL; 798 scsi_tran->tran_quiesce = NULL; 799 scsi_tran->tran_unquiesce = NULL; 800 scsi_tran->tran_bus_reset = NULL; 801 802 if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr, 803 scsi_tran, 0) != DDI_SUCCESS) { 804 #ifdef SATA_DEBUG 805 cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed", 806 ddi_driver_name(dip), ddi_get_instance(dip)); 807 #endif 808 goto fail; 809 } 810 hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED; 811 812 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) { 813 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, 814 "sata", 1) != DDI_PROP_SUCCESS) { 815 SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: " 816 "failed to create hba sata prop")); 817 goto fail; 818 } 819 } 820 821 /* 822 * Save pointers in hba instance soft state. 823 */ 824 sata_hba_inst->satahba_scsi_tran = scsi_tran; 825 sata_hba_inst->satahba_tran = sata_tran; 826 sata_hba_inst->satahba_dip = dip; 827 828 /* 829 * Create a task queue to handle emulated commands completion 830 * Use node name, dash, instance number as the queue name. 831 */ 832 taskq_name[0] = '\0'; 833 (void) strlcat(taskq_name, DEVI(dip)->devi_node_name, 834 sizeof (taskq_name)); 835 (void) snprintf(taskq_name + strlen(taskq_name), 836 sizeof (taskq_name) - strlen(taskq_name), 837 "-%d", DEVI(dip)->devi_instance); 838 sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1, 839 minclsyspri, 1, sata_tran->sata_tran_hba_num_cports * 4, 840 TASKQ_DYNAMIC); 841 842 hba_attach_state |= HBA_ATTACH_STAGE_SETUP; 843 844 /* 845 * Create events thread if not created yet. 846 */ 847 sata_event_thread_control(1); 848 849 /* 850 * Link this hba instance into the list. 851 */ 852 mutex_enter(&sata_mutex); 853 854 if (sata_hba_list == NULL) { 855 /* 856 * The first instance of HBA is attached. 857 * Set current/active default maximum NCQ/TCQ queue depth for 858 * all SATA devices. It is done here and now, to eliminate the 859 * possibility of the dynamic, programatic modification of the 860 * queue depth via global (and public) sata_max_queue_depth 861 * variable (this would require special handling in HBA drivers) 862 */ 863 sata_current_max_qdepth = sata_max_queue_depth; 864 if (sata_current_max_qdepth > 32) 865 sata_current_max_qdepth = 32; 866 else if (sata_current_max_qdepth < 1) 867 sata_current_max_qdepth = 1; 868 } 869 870 sata_hba_inst->satahba_next = NULL; 871 sata_hba_inst->satahba_prev = sata_hba_list_tail; 872 if (sata_hba_list == NULL) { 873 sata_hba_list = sata_hba_inst; 874 } 875 if (sata_hba_list_tail != NULL) { 876 sata_hba_list_tail->satahba_next = sata_hba_inst; 877 } 878 sata_hba_list_tail = sata_hba_inst; 879 mutex_exit(&sata_mutex); 880 hba_attach_state |= HBA_ATTACH_STAGE_LINKED; 881 882 /* 883 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl 884 * SATA HBA driver should not use its own open/close entry points. 885 * 886 * Make sure that instance number doesn't overflow 887 * when forming minor numbers. 888 */ 889 ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT)); 890 if (ddi_create_minor_node(dip, "devctl", S_IFCHR, 891 INST2DEVCTL(ddi_get_instance(dip)), 892 DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) { 893 #ifdef SATA_DEBUG 894 cmn_err(CE_WARN, "sata_hba_attach: " 895 "cannot create devctl minor node"); 896 #endif 897 goto fail; 898 } 899 900 901 /* 902 * Set-up kstats here, if necessary. 903 * (postponed until future phase of the development). 904 */ 905 906 /* 907 * Indicate that HBA is attached. This will enable events processing 908 * for this HBA. 909 */ 910 sata_hba_inst->satahba_attached = 1; 911 /* 912 * Probe controller ports. This operation will describe a current 913 * controller/port/multipliers/device configuration and will create 914 * attachment points. 915 * We may end-up with just a controller with no devices attached. 916 * For the ports with a supported device attached, device target nodes 917 * are created and devices are initialized. 918 */ 919 sata_probe_ports(sata_hba_inst); 920 921 return (DDI_SUCCESS); 922 923 fail: 924 if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) { 925 (void) sata_remove_hba_instance(dip); 926 if (sata_hba_list == NULL) 927 sata_event_thread_control(0); 928 } 929 930 if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) { 931 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata"); 932 taskq_destroy(sata_hba_inst->satahba_taskq); 933 } 934 935 if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED) 936 (void) scsi_hba_detach(dip); 937 938 if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) { 939 mutex_destroy(&sata_hba_inst->satahba_mutex); 940 kmem_free((void *)sata_hba_inst, 941 sizeof (struct sata_hba_inst)); 942 scsi_hba_tran_free(scsi_tran); 943 } 944 945 sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed", 946 ddi_driver_name(dip), ddi_get_instance(dip)); 947 948 return (DDI_FAILURE); 949 } 950 951 952 /* 953 * Called by SATA HBA from to detach an instance of the driver. 954 * 955 * For DDI_DETACH command: 956 * Free local structures allocated for SATA HBA instance during 957 * sata_hba_attach processing. 958 * 959 * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise. 960 * 961 * For DDI_SUSPEND command: 962 * Not implemented at this time (postponed until phase 2 of the development) 963 * Returnd DDI_SUCCESS. 964 * 965 * When the last HBA instance is detached, the event daemon is terminated. 966 * 967 * NOTE: Port multiplier is supported. 968 */ 969 int 970 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 971 { 972 dev_info_t *tdip; 973 sata_hba_inst_t *sata_hba_inst; 974 scsi_hba_tran_t *scsi_hba_tran; 975 sata_cport_info_t *cportinfo; 976 sata_pmult_info_t *pminfo; 977 sata_drive_info_t *sdinfo; 978 sata_device_t sdevice; 979 int ncport, npmport; 980 981 SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n", 982 ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip)); 983 984 switch (cmd) { 985 case DDI_DETACH: 986 987 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 988 return (DDI_FAILURE); 989 990 sata_hba_inst = scsi_hba_tran->tran_hba_private; 991 if (sata_hba_inst == NULL) 992 return (DDI_FAILURE); 993 994 if (scsi_hba_detach(dip) == DDI_FAILURE) { 995 sata_hba_inst->satahba_attached = 1; 996 return (DDI_FAILURE); 997 } 998 999 /* 1000 * Free all target nodes - at this point 1001 * devices should be at least offlined 1002 * otherwise scsi_hba_detach() should not be called. 1003 */ 1004 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); 1005 ncport++) { 1006 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 1007 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 1008 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 1009 if (sdinfo != NULL) { 1010 tdip = sata_get_target_dip(dip, 1011 ncport, 0); 1012 if (tdip != NULL) { 1013 if (ndi_devi_offline(tdip, 1014 NDI_DEVI_REMOVE) != 1015 NDI_SUCCESS) { 1016 SATA_LOG_D(( 1017 sata_hba_inst, 1018 CE_WARN, 1019 "sata_hba_detach: " 1020 "Target node not " 1021 "removed !")); 1022 return (DDI_FAILURE); 1023 } 1024 } 1025 } 1026 } else { /* SATA_DTYPE_PMULT */ 1027 mutex_enter(&cportinfo->cport_mutex); 1028 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 1029 1030 if (pminfo == NULL) { 1031 SATA_LOG_D((sata_hba_inst, CE_WARN, 1032 "sata_hba_detach: Port multiplier " 1033 "not ready yet!")); 1034 mutex_exit(&cportinfo->cport_mutex); 1035 return (DDI_FAILURE); 1036 } 1037 1038 /* 1039 * Detach would fail if removal of any of the 1040 * target nodes is failed - albeit in that 1041 * case some of them may have been removed. 1042 */ 1043 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 1044 sata_hba_inst, ncport); npmport++) { 1045 tdip = sata_get_target_dip(dip, ncport, 1046 npmport); 1047 if (tdip != NULL) { 1048 if (ndi_devi_offline(tdip, 1049 NDI_DEVI_REMOVE) != 1050 NDI_SUCCESS) { 1051 SATA_LOG_D(( 1052 sata_hba_inst, 1053 CE_WARN, 1054 "sata_hba_detach: " 1055 "Target node not " 1056 "removed !")); 1057 mutex_exit(&cportinfo-> 1058 cport_mutex); 1059 return (DDI_FAILURE); 1060 } 1061 } 1062 } 1063 mutex_exit(&cportinfo->cport_mutex); 1064 } 1065 } 1066 /* 1067 * Disable sata event daemon processing for this HBA 1068 */ 1069 sata_hba_inst->satahba_attached = 0; 1070 1071 /* 1072 * Remove event daemon thread, if it is last HBA instance. 1073 */ 1074 1075 mutex_enter(&sata_mutex); 1076 if (sata_hba_list->satahba_next == NULL) { 1077 mutex_exit(&sata_mutex); 1078 sata_event_thread_control(0); 1079 mutex_enter(&sata_mutex); 1080 } 1081 mutex_exit(&sata_mutex); 1082 1083 /* Remove this HBA instance from the HBA list */ 1084 sata_remove_hba_instance(dip); 1085 1086 /* 1087 * At this point there should be no target nodes attached. 1088 * Detach and destroy device and port info structures. 1089 */ 1090 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); 1091 ncport++) { 1092 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 1093 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 1094 sdinfo = 1095 cportinfo->cport_devp.cport_sata_drive; 1096 if (sdinfo != NULL) { 1097 /* Release device structure */ 1098 kmem_free(sdinfo, 1099 sizeof (sata_drive_info_t)); 1100 } 1101 /* Release cport info */ 1102 mutex_destroy(&cportinfo->cport_mutex); 1103 kmem_free(cportinfo, 1104 sizeof (sata_cport_info_t)); 1105 } else { /* SATA_DTYPE_PMULT */ 1106 sdevice.satadev_addr.cport = (uint8_t)ncport; 1107 sdevice.satadev_addr.qual = SATA_ADDR_PMULT; 1108 sata_free_pmult(sata_hba_inst, &sdevice); 1109 } 1110 } 1111 1112 scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran); 1113 1114 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata"); 1115 1116 taskq_destroy(sata_hba_inst->satahba_taskq); 1117 1118 mutex_destroy(&sata_hba_inst->satahba_mutex); 1119 kmem_free((void *)sata_hba_inst, 1120 sizeof (struct sata_hba_inst)); 1121 1122 return (DDI_SUCCESS); 1123 1124 case DDI_SUSPEND: 1125 /* 1126 * Postponed until phase 2 1127 */ 1128 return (DDI_FAILURE); 1129 1130 default: 1131 return (DDI_FAILURE); 1132 } 1133 } 1134 1135 1136 /* 1137 * Called by an HBA drive from _fini() routine. 1138 * Unregisters SATA HBA instance/SATA framework pair from the scsi framework. 1139 */ 1140 void 1141 sata_hba_fini(struct modlinkage *modlp) 1142 { 1143 SATADBG1(SATA_DBG_HBA_IF, NULL, 1144 "sata_hba_fini: name %s\n", 1145 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo); 1146 1147 scsi_hba_fini(modlp); 1148 } 1149 1150 1151 /* 1152 * Default open and close routine for sata_hba framework. 1153 * 1154 */ 1155 /* 1156 * Open devctl node. 1157 * 1158 * Returns: 1159 * 0 if node was open successfully, error code otherwise. 1160 * 1161 * 1162 */ 1163 1164 static int 1165 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp) 1166 { 1167 #ifndef __lock_lint 1168 _NOTE(ARGUNUSED(credp)) 1169 #endif 1170 int rv = 0; 1171 dev_info_t *dip; 1172 scsi_hba_tran_t *scsi_hba_tran; 1173 sata_hba_inst_t *sata_hba_inst; 1174 1175 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL); 1176 1177 if (otyp != OTYP_CHR) 1178 return (EINVAL); 1179 1180 dip = sata_devt_to_devinfo(*devp); 1181 if (dip == NULL) 1182 return (ENXIO); 1183 1184 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1185 return (ENXIO); 1186 1187 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1188 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0) 1189 return (ENXIO); 1190 1191 mutex_enter(&sata_mutex); 1192 if (flags & FEXCL) { 1193 if (sata_hba_inst->satahba_open_flag != 0) { 1194 rv = EBUSY; 1195 } else { 1196 sata_hba_inst->satahba_open_flag = 1197 SATA_DEVCTL_EXOPENED; 1198 } 1199 } else { 1200 if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) { 1201 rv = EBUSY; 1202 } else { 1203 sata_hba_inst->satahba_open_flag = 1204 SATA_DEVCTL_SOPENED; 1205 } 1206 } 1207 mutex_exit(&sata_mutex); 1208 1209 return (rv); 1210 } 1211 1212 1213 /* 1214 * Close devctl node. 1215 * Returns: 1216 * 0 if node was closed successfully, error code otherwise. 1217 * 1218 */ 1219 1220 static int 1221 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp) 1222 { 1223 #ifndef __lock_lint 1224 _NOTE(ARGUNUSED(credp)) 1225 _NOTE(ARGUNUSED(flag)) 1226 #endif 1227 dev_info_t *dip; 1228 scsi_hba_tran_t *scsi_hba_tran; 1229 sata_hba_inst_t *sata_hba_inst; 1230 1231 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL); 1232 1233 if (otyp != OTYP_CHR) 1234 return (EINVAL); 1235 1236 dip = sata_devt_to_devinfo(dev); 1237 if (dip == NULL) 1238 return (ENXIO); 1239 1240 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1241 return (ENXIO); 1242 1243 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1244 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0) 1245 return (ENXIO); 1246 1247 mutex_enter(&sata_mutex); 1248 sata_hba_inst->satahba_open_flag = 0; 1249 mutex_exit(&sata_mutex); 1250 return (0); 1251 } 1252 1253 1254 1255 /* 1256 * Standard IOCTL commands for SATA hotplugging. 1257 * Implemented DEVCTL_AP commands: 1258 * DEVCTL_AP_CONNECT 1259 * DEVCTL_AP_DISCONNECT 1260 * DEVCTL_AP_CONFIGURE 1261 * DEVCTL_UNCONFIGURE 1262 * DEVCTL_AP_CONTROL 1263 * 1264 * Commands passed to default ndi ioctl handler: 1265 * DEVCTL_DEVICE_GETSTATE 1266 * DEVCTL_DEVICE_ONLINE 1267 * DEVCTL_DEVICE_OFFLINE 1268 * DEVCTL_DEVICE_REMOVE 1269 * DEVCTL_DEVICE_INSERT 1270 * DEVCTL_BUS_GETSTATE 1271 * 1272 * All other cmds are passed to HBA if it provide ioctl handler, or failed 1273 * if not. 1274 * 1275 * Returns: 1276 * 0 if successful, 1277 * error code if operation failed. 1278 * 1279 * Port Multiplier support is supported now. 1280 * 1281 * NOTE: qual should be SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT 1282 */ 1283 1284 static int 1285 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, 1286 int *rvalp) 1287 { 1288 #ifndef __lock_lint 1289 _NOTE(ARGUNUSED(credp)) 1290 _NOTE(ARGUNUSED(rvalp)) 1291 #endif 1292 int rv = 0; 1293 int32_t comp_port = -1; 1294 dev_info_t *dip; 1295 devctl_ap_state_t ap_state; 1296 struct devctl_iocdata *dcp = NULL; 1297 scsi_hba_tran_t *scsi_hba_tran; 1298 sata_hba_inst_t *sata_hba_inst; 1299 sata_device_t sata_device; 1300 sata_cport_info_t *cportinfo; 1301 int cport, pmport, qual; 1302 int rval = SATA_SUCCESS; 1303 1304 dip = sata_devt_to_devinfo(dev); 1305 if (dip == NULL) 1306 return (ENXIO); 1307 1308 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1309 return (ENXIO); 1310 1311 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1312 if (sata_hba_inst == NULL) 1313 return (ENXIO); 1314 1315 if (sata_hba_inst->satahba_tran == NULL) 1316 return (ENXIO); 1317 1318 switch (cmd) { 1319 1320 case DEVCTL_DEVICE_GETSTATE: 1321 case DEVCTL_DEVICE_ONLINE: 1322 case DEVCTL_DEVICE_OFFLINE: 1323 case DEVCTL_DEVICE_REMOVE: 1324 case DEVCTL_BUS_GETSTATE: 1325 /* 1326 * There may be more cases that we want to pass to default 1327 * handler rather than fail them. 1328 */ 1329 return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0)); 1330 } 1331 1332 /* read devctl ioctl data */ 1333 if (cmd != DEVCTL_AP_CONTROL) { 1334 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) 1335 return (EFAULT); 1336 1337 if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) == 1338 -1) { 1339 if (dcp) 1340 ndi_dc_freehdl(dcp); 1341 return (EINVAL); 1342 } 1343 1344 /* 1345 * According to SCSI_TO_SATA_ADDR_QUAL, qual should be either 1346 * SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT. 1347 */ 1348 cport = SCSI_TO_SATA_CPORT(comp_port); 1349 pmport = SCSI_TO_SATA_PMPORT(comp_port); 1350 qual = SCSI_TO_SATA_ADDR_QUAL(comp_port); 1351 1352 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, 1353 qual) != 0) { 1354 ndi_dc_freehdl(dcp); 1355 return (EINVAL); 1356 } 1357 1358 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 1359 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1360 cport_mutex); 1361 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) { 1362 /* 1363 * Cannot process ioctl request now. Come back later. 1364 */ 1365 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1366 cport_mutex); 1367 ndi_dc_freehdl(dcp); 1368 return (EBUSY); 1369 } 1370 /* Block event processing for this port */ 1371 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 1372 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1373 1374 sata_device.satadev_addr.cport = cport; 1375 sata_device.satadev_addr.pmport = pmport; 1376 sata_device.satadev_addr.qual = qual; 1377 sata_device.satadev_rev = SATA_DEVICE_REV; 1378 } 1379 1380 switch (cmd) { 1381 1382 case DEVCTL_AP_DISCONNECT: 1383 1384 /* 1385 * Normally, cfgadm sata plugin will try to offline 1386 * (unconfigure) device before this request. Nevertheless, 1387 * if a device is still configured, we need to 1388 * attempt to offline and unconfigure device first, and we will 1389 * deactivate the port regardless of the unconfigure 1390 * operation results. 1391 * 1392 */ 1393 rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device); 1394 1395 break; 1396 1397 case DEVCTL_AP_UNCONFIGURE: 1398 1399 /* 1400 * The unconfigure operation uses generic nexus operation to 1401 * offline a device. It leaves a target device node attached. 1402 * and obviously sata_drive_info attached as well, because 1403 * from the hardware point of view nothing has changed. 1404 */ 1405 rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device); 1406 break; 1407 1408 case DEVCTL_AP_CONNECT: 1409 { 1410 /* 1411 * The sata cfgadm pluging will invoke this operation only if 1412 * port was found in the disconnect state (failed state 1413 * is also treated as the disconnected state). 1414 * If port activation is successful and a device is found 1415 * attached to the port, the initialization sequence is 1416 * executed to probe the port and attach 1417 * a device structure to a port structure. The device is not 1418 * set in configured state (system-wise) by this operation. 1419 */ 1420 1421 rv = sata_ioctl_connect(sata_hba_inst, &sata_device); 1422 1423 break; 1424 } 1425 1426 case DEVCTL_AP_CONFIGURE: 1427 { 1428 /* 1429 * A port may be in an active or shutdown state. 1430 * If port is in a failed state, operation is aborted. 1431 * If a port is in a shutdown state, sata_tran_port_activate() 1432 * is invoked prior to any other operation. 1433 * 1434 * Onlining the device involves creating a new target node. 1435 * If there is an old target node present (belonging to 1436 * previously removed device), the operation is aborted - the 1437 * old node has to be released and removed before configure 1438 * operation is attempted. 1439 */ 1440 1441 rv = sata_ioctl_configure(sata_hba_inst, &sata_device); 1442 1443 break; 1444 } 1445 1446 case DEVCTL_AP_GETSTATE: 1447 1448 sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state); 1449 1450 ap_state.ap_last_change = (time_t)-1; 1451 ap_state.ap_error_code = 0; 1452 ap_state.ap_in_transition = 0; 1453 1454 /* Copy the return AP-state information to the user space */ 1455 if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) { 1456 rv = EFAULT; 1457 } 1458 break; 1459 1460 case DEVCTL_AP_CONTROL: 1461 { 1462 /* 1463 * Generic devctl for hardware specific functionality 1464 */ 1465 sata_ioctl_data_t ioc; 1466 1467 ASSERT(dcp == NULL); 1468 1469 /* Copy in user ioctl data first */ 1470 #ifdef _MULTI_DATAMODEL 1471 if (ddi_model_convert_from(mode & FMODELS) == 1472 DDI_MODEL_ILP32) { 1473 1474 sata_ioctl_data_32_t ioc32; 1475 1476 if (ddi_copyin((void *)arg, (void *)&ioc32, 1477 sizeof (ioc32), mode) != 0) { 1478 rv = EFAULT; 1479 break; 1480 } 1481 ioc.cmd = (uint_t)ioc32.cmd; 1482 ioc.port = (uint_t)ioc32.port; 1483 ioc.get_size = (uint_t)ioc32.get_size; 1484 ioc.buf = (caddr_t)(uintptr_t)ioc32.buf; 1485 ioc.bufsiz = (uint_t)ioc32.bufsiz; 1486 ioc.misc_arg = (uint_t)ioc32.misc_arg; 1487 } else 1488 #endif /* _MULTI_DATAMODEL */ 1489 if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc), 1490 mode) != 0) { 1491 return (EFAULT); 1492 } 1493 1494 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 1495 "sata_hba_ioctl: DEVCTL_AP_CONTROL " 1496 "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port); 1497 1498 /* 1499 * To avoid BE/LE and 32/64 issues, a get_size always returns 1500 * a 32-bit number. 1501 */ 1502 if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) { 1503 return (EINVAL); 1504 } 1505 /* validate address */ 1506 cport = SCSI_TO_SATA_CPORT(ioc.port); 1507 pmport = SCSI_TO_SATA_PMPORT(ioc.port); 1508 qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port); 1509 1510 SATADBG3(SATA_DBG_IOCTL_IF, sata_hba_inst, 1511 "sata_hba_ioctl: target port is %d:%d (%d)", 1512 cport, pmport, qual); 1513 1514 if (sata_validate_sata_address(sata_hba_inst, cport, 1515 pmport, qual) != 0) 1516 return (EINVAL); 1517 1518 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 1519 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1520 cport_mutex); 1521 /* Is the port locked by event processing daemon ? */ 1522 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) { 1523 /* 1524 * Cannot process ioctl request now. Come back later 1525 */ 1526 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1527 cport_mutex); 1528 return (EBUSY); 1529 } 1530 /* Block event processing for this port */ 1531 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 1532 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1533 1534 1535 sata_device.satadev_addr.cport = cport; 1536 sata_device.satadev_addr.pmport = pmport; 1537 sata_device.satadev_addr.qual = qual; 1538 sata_device.satadev_rev = SATA_DEVICE_REV; 1539 1540 switch (ioc.cmd) { 1541 1542 case SATA_CFGA_RESET_PORT: 1543 /* 1544 * There is no protection for configured device. 1545 */ 1546 rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device); 1547 break; 1548 1549 case SATA_CFGA_RESET_DEVICE: 1550 /* 1551 * There is no protection for configured device. 1552 */ 1553 rv = sata_ioctl_reset_device(sata_hba_inst, 1554 &sata_device); 1555 break; 1556 1557 case SATA_CFGA_RESET_ALL: 1558 /* 1559 * There is no protection for configured devices. 1560 */ 1561 rv = sata_ioctl_reset_all(sata_hba_inst); 1562 /* 1563 * We return here, because common return is for 1564 * a single port operation - we have already unlocked 1565 * all ports and no dc handle was allocated. 1566 */ 1567 return (rv); 1568 1569 case SATA_CFGA_PORT_DEACTIVATE: 1570 /* 1571 * Arbitrarily unconfigure attached device, if any. 1572 * Even if the unconfigure fails, proceed with the 1573 * port deactivation. 1574 */ 1575 rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device); 1576 1577 break; 1578 1579 case SATA_CFGA_PORT_ACTIVATE: 1580 1581 rv = sata_ioctl_activate(sata_hba_inst, &sata_device); 1582 break; 1583 1584 case SATA_CFGA_PORT_SELF_TEST: 1585 1586 rv = sata_ioctl_port_self_test(sata_hba_inst, 1587 &sata_device); 1588 break; 1589 1590 case SATA_CFGA_GET_DEVICE_PATH: 1591 1592 rv = sata_ioctl_get_device_path(sata_hba_inst, 1593 &sata_device, &ioc, mode); 1594 break; 1595 1596 case SATA_CFGA_GET_AP_TYPE: 1597 1598 rv = sata_ioctl_get_ap_type(sata_hba_inst, 1599 &sata_device, &ioc, mode); 1600 break; 1601 1602 case SATA_CFGA_GET_MODEL_INFO: 1603 1604 rv = sata_ioctl_get_model_info(sata_hba_inst, 1605 &sata_device, &ioc, mode); 1606 break; 1607 1608 case SATA_CFGA_GET_REVFIRMWARE_INFO: 1609 1610 rv = sata_ioctl_get_revfirmware_info(sata_hba_inst, 1611 &sata_device, &ioc, mode); 1612 break; 1613 1614 case SATA_CFGA_GET_SERIALNUMBER_INFO: 1615 1616 rv = sata_ioctl_get_serialnumber_info(sata_hba_inst, 1617 &sata_device, &ioc, mode); 1618 break; 1619 1620 default: 1621 rv = EINVAL; 1622 break; 1623 1624 } /* End of DEVCTL_AP_CONTROL cmd switch */ 1625 1626 break; 1627 } 1628 1629 default: 1630 { 1631 /* 1632 * If we got here, we got an IOCTL that SATA HBA Framework 1633 * does not recognize. Pass ioctl to HBA driver, in case 1634 * it could process it. 1635 */ 1636 sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran; 1637 dev_info_t *mydip = SATA_DIP(sata_hba_inst); 1638 1639 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 1640 "IOCTL 0x%2x not supported in SATA framework, " 1641 "passthrough to HBA", cmd); 1642 1643 if (sata_tran->sata_tran_ioctl == NULL) { 1644 rv = EINVAL; 1645 break; 1646 } 1647 rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg); 1648 if (rval != 0) { 1649 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 1650 "IOCTL 0x%2x failed in HBA", cmd); 1651 rv = rval; 1652 } 1653 break; 1654 } 1655 1656 } /* End of main IOCTL switch */ 1657 1658 if (dcp) { 1659 ndi_dc_freehdl(dcp); 1660 } 1661 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1662 cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY; 1663 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1664 1665 return (rv); 1666 } 1667 1668 1669 /* 1670 * Create error retrieval sata packet 1671 * 1672 * A sata packet is allocated and set-up to contain specified error retrieval 1673 * command and appropriate dma-able data buffer. 1674 * No association with any scsi packet is made and no callback routine is 1675 * specified. 1676 * 1677 * Returns a pointer to sata packet upon successfull packet creation. 1678 * Returns NULL, if packet cannot be created. 1679 */ 1680 sata_pkt_t * 1681 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device, 1682 int pkt_type) 1683 { 1684 sata_hba_inst_t *sata_hba_inst; 1685 sata_pkt_txlate_t *spx; 1686 sata_pkt_t *spkt; 1687 sata_drive_info_t *sdinfo; 1688 1689 mutex_enter(&sata_mutex); 1690 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 1691 sata_hba_inst = sata_hba_inst->satahba_next) { 1692 if (SATA_DIP(sata_hba_inst) == dip) 1693 break; 1694 } 1695 mutex_exit(&sata_mutex); 1696 ASSERT(sata_hba_inst != NULL); 1697 1698 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 1699 if (sdinfo == NULL) { 1700 sata_log(sata_hba_inst, CE_WARN, 1701 "sata: error recovery request for non-attached device at " 1702 "cport %d", sata_device->satadev_addr.cport); 1703 return (NULL); 1704 } 1705 1706 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 1707 spx->txlt_sata_hba_inst = sata_hba_inst; 1708 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 1709 spkt = sata_pkt_alloc(spx, NULL); 1710 if (spkt == NULL) { 1711 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1712 return (NULL); 1713 } 1714 /* address is needed now */ 1715 spkt->satapkt_device.satadev_addr = sata_device->satadev_addr; 1716 1717 switch (pkt_type) { 1718 case SATA_ERR_RETR_PKT_TYPE_NCQ: 1719 if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) 1720 return (spkt); 1721 break; 1722 1723 case SATA_ERR_RETR_PKT_TYPE_ATAPI: 1724 if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) 1725 return (spkt); 1726 break; 1727 1728 default: 1729 break; 1730 } 1731 1732 sata_pkt_free(spx); 1733 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1734 return (NULL); 1735 1736 } 1737 1738 1739 /* 1740 * Free error retrieval sata packet 1741 * 1742 * Free sata packet and any associated resources allocated previously by 1743 * sata_get_error_retrieval_pkt(). 1744 * 1745 * Void return. 1746 */ 1747 void 1748 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt) 1749 { 1750 sata_pkt_txlate_t *spx = 1751 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 1752 1753 ASSERT(sata_pkt != NULL); 1754 1755 sata_free_local_buffer(spx); 1756 sata_pkt_free(spx); 1757 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1758 1759 } 1760 1761 /* 1762 * Create READ PORT MULTIPLIER and WRITE PORT MULTIPLIER sata packet 1763 * 1764 * No association with any scsi packet is made and no callback routine is 1765 * specified. 1766 * 1767 * Returns a pointer to sata packet upon successfull packet creation. 1768 * Returns NULL, if packet cannot be created. 1769 * 1770 * NOTE: Input/Output value includes 64 bits accoring to SATA Spec 2.6, 1771 * only lower 32 bits are available currently. 1772 */ 1773 sata_pkt_t * 1774 sata_get_rdwr_pmult_pkt(dev_info_t *dip, sata_device_t *sd, 1775 uint8_t regn, uint32_t regv, uint32_t type) 1776 { 1777 sata_hba_inst_t *sata_hba_inst; 1778 sata_pkt_txlate_t *spx; 1779 sata_pkt_t *spkt; 1780 sata_cmd_t *scmd; 1781 1782 /* Only READ/WRITE commands are accepted. */ 1783 ASSERT(type == SATA_RDWR_PMULT_PKT_TYPE_READ || 1784 type == SATA_RDWR_PMULT_PKT_TYPE_WRITE); 1785 1786 mutex_enter(&sata_mutex); 1787 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 1788 sata_hba_inst = sata_hba_inst->satahba_next) { 1789 if (SATA_DIP(sata_hba_inst) == dip) 1790 break; 1791 } 1792 mutex_exit(&sata_mutex); 1793 ASSERT(sata_hba_inst != NULL); 1794 1795 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 1796 spx->txlt_sata_hba_inst = sata_hba_inst; 1797 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 1798 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 1799 if (spkt == NULL) { 1800 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1801 return (NULL); 1802 } 1803 1804 /* 1805 * NOTE: We need to send this command to the port multiplier, 1806 * that means send to SATA_PMULT_HOSTPORT(0xf) pmport 1807 * 1808 * sata_device contains the address of actual target device, and the 1809 * pmport number in the command comes from the sata_device structure. 1810 */ 1811 spkt->satapkt_device.satadev_addr = sd->satadev_addr; 1812 spkt->satapkt_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT; 1813 spkt->satapkt_device.satadev_addr.qual = SATA_ADDR_PMULT; 1814 1815 /* Fill sata_pkt */ 1816 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING; 1817 spkt->satapkt_comp = NULL; /* Synchronous mode, no callback */ 1818 spkt->satapkt_time = 10; /* Timeout 10s */ 1819 1820 /* Build READ PORT MULTIPLIER cmd in the sata_pkt */ 1821 scmd = &spkt->satapkt_cmd; 1822 scmd->satacmd_features_reg = regn & 0xff; 1823 scmd->satacmd_features_reg_ext = (regn >> 8) & 0xff; 1824 scmd->satacmd_device_reg = sd->satadev_addr.pmport; 1825 scmd->satacmd_addr_type = 0; /* N/A */ 1826 1827 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 1828 1829 if (type == SATA_RDWR_PMULT_PKT_TYPE_READ) { 1830 scmd->satacmd_cmd_reg = SATAC_READ_PORTMULT; 1831 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 1832 scmd->satacmd_flags.sata_special_regs = 1; 1833 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1; 1834 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1; 1835 scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1; 1836 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1; 1837 } else if (type == SATA_RDWR_PMULT_PKT_TYPE_WRITE) { 1838 scmd->satacmd_cmd_reg = SATAC_WRITE_PORTMULT; 1839 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 1840 scmd->satacmd_sec_count_lsb = regv & 0xff; 1841 scmd->satacmd_lba_low_lsb = regv >> 8 & 0xff; 1842 scmd->satacmd_lba_mid_lsb = regv >> 16 & 0xff; 1843 scmd->satacmd_lba_high_lsb = regv >> 24 & 0xff; 1844 } 1845 1846 return (spkt); 1847 } 1848 1849 /* 1850 * Free sata packet and any associated resources allocated previously by 1851 * sata_get_rdwr_pmult_pkt(). 1852 * 1853 * Void return. 1854 */ 1855 void 1856 sata_free_rdwr_pmult_pkt(sata_pkt_t *sata_pkt) 1857 { 1858 sata_pkt_txlate_t *spx = 1859 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 1860 1861 /* Free allocated resources */ 1862 sata_pkt_free(spx); 1863 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1864 } 1865 1866 /* 1867 * Register a port multiplier to framework. 1868 * 1) Store the GSCR values in the previous allocated pmult_info strctures. 1869 * 2) Search in the blacklist and update the number of the device ports of the 1870 * port multiplier. 1871 * 1872 * Void return. 1873 */ 1874 void 1875 sata_register_pmult(dev_info_t *dip, sata_device_t *sd, sata_pmult_gscr_t *sg) 1876 { 1877 sata_hba_inst_t *sata_hba_inst = NULL; 1878 sata_pmult_info_t *pmultinfo; 1879 sata_pmult_bl_t *blp; 1880 int cport = sd->satadev_addr.cport; 1881 1882 mutex_enter(&sata_mutex); 1883 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 1884 sata_hba_inst = sata_hba_inst->satahba_next) { 1885 if (SATA_DIP(sata_hba_inst) == dip) 1886 if (sata_hba_inst->satahba_attached == 1) 1887 break; 1888 } 1889 mutex_exit(&sata_mutex); 1890 /* HBA not attached? */ 1891 if (sata_hba_inst == NULL) 1892 return; 1893 1894 /* Number of pmports */ 1895 sd->satadev_add_info = sg->gscr2 & SATA_PMULT_PORTNUM_MASK; 1896 1897 /* Check the blacklist */ 1898 for (blp = sata_pmult_blacklist; blp->bl_gscr0; blp++) { 1899 if (sg->gscr0 != blp->bl_gscr0 && blp->bl_gscr0) 1900 continue; 1901 if (sg->gscr1 != blp->bl_gscr1 && blp->bl_gscr1) 1902 continue; 1903 if (sg->gscr2 != blp->bl_gscr2 && blp->bl_gscr2) 1904 continue; 1905 1906 cmn_err(CE_WARN, "!Port multiplier is on the blacklist."); 1907 sd->satadev_add_info = blp->bl_flags; 1908 break; 1909 } 1910 1911 /* Register the port multiplier GSCR */ 1912 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 1913 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 1914 if (pmultinfo != NULL) { 1915 pmultinfo->pmult_gscr = *sg; 1916 pmultinfo->pmult_num_dev_ports = 1917 sd->satadev_add_info & SATA_PMULT_PORTNUM_MASK; 1918 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 1919 "Port multiplier registered at port %d", cport); 1920 } 1921 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 1922 } 1923 1924 /* 1925 * sata_name_child is for composing the name of the node 1926 * the format of the name is "target,0". 1927 */ 1928 static int 1929 sata_name_child(dev_info_t *dip, char *name, int namelen) 1930 { 1931 int target; 1932 1933 target = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 1934 DDI_PROP_DONTPASS, "target", -1); 1935 if (target == -1) 1936 return (DDI_FAILURE); 1937 (void) snprintf(name, namelen, "%x,0", target); 1938 return (DDI_SUCCESS); 1939 } 1940 1941 1942 1943 /* ****************** SCSA required entry points *********************** */ 1944 1945 /* 1946 * Implementation of scsi tran_tgt_init. 1947 * sata_scsi_tgt_init() initializes scsi_device structure 1948 * 1949 * If successful, DDI_SUCCESS is returned. 1950 * DDI_FAILURE is returned if addressed device does not exist 1951 */ 1952 1953 static int 1954 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip, 1955 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 1956 { 1957 #ifndef __lock_lint 1958 _NOTE(ARGUNUSED(hba_dip)) 1959 _NOTE(ARGUNUSED(tgt_dip)) 1960 #endif 1961 sata_device_t sata_device; 1962 sata_drive_info_t *sdinfo; 1963 struct sata_id *sid; 1964 sata_hba_inst_t *sata_hba_inst; 1965 char model[SATA_ID_MODEL_LEN + 1]; 1966 char fw[SATA_ID_FW_LEN + 1]; 1967 char *vid, *pid; 1968 int i; 1969 1970 /* 1971 * Fail tran_tgt_init for .conf stub node 1972 */ 1973 if (ndi_dev_is_persistent_node(tgt_dip) == 0) { 1974 (void) ndi_merge_node(tgt_dip, sata_name_child); 1975 ddi_set_name_addr(tgt_dip, NULL); 1976 return (DDI_FAILURE); 1977 } 1978 1979 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 1980 1981 /* Validate scsi device address */ 1982 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 1983 &sata_device) != 0) 1984 return (DDI_FAILURE); 1985 1986 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 1987 sata_device.satadev_addr.cport))); 1988 1989 /* sata_device now contains a valid sata address */ 1990 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 1991 if (sdinfo == NULL) { 1992 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1993 sata_device.satadev_addr.cport))); 1994 return (DDI_FAILURE); 1995 } 1996 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1997 sata_device.satadev_addr.cport))); 1998 1999 /* 2000 * Check if we need to create a legacy devid (i.e cmdk style) for 2001 * the target disks. 2002 * 2003 * HBA devinfo node will have the property "use-cmdk-devid-format" 2004 * if we need to create cmdk-style devid for all the disk devices 2005 * attached to this controller. This property may have been set 2006 * from HBA driver's .conf file or by the HBA driver in its 2007 * attach(9F) function. 2008 */ 2009 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 2010 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS, 2011 "use-cmdk-devid-format", 0) == 1)) { 2012 /* register a legacy devid for this target node */ 2013 sata_target_devid_register(tgt_dip, sdinfo); 2014 } 2015 2016 2017 /* 2018 * 'Identify Device Data' does not always fit in standard SCSI 2019 * INQUIRY data, so establish INQUIRY_* properties with full-form 2020 * of information. 2021 */ 2022 sid = &sdinfo->satadrv_id; 2023 #ifdef _LITTLE_ENDIAN 2024 swab(sid->ai_model, model, SATA_ID_MODEL_LEN); 2025 swab(sid->ai_fw, fw, SATA_ID_FW_LEN); 2026 #else /* _LITTLE_ENDIAN */ 2027 bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN); 2028 bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN); 2029 #endif /* _LITTLE_ENDIAN */ 2030 model[SATA_ID_MODEL_LEN] = 0; 2031 fw[SATA_ID_FW_LEN] = 0; 2032 2033 /* split model into into vid/pid */ 2034 for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++) 2035 if ((*pid == ' ') || (*pid == '\t')) 2036 break; 2037 if (i < SATA_ID_MODEL_LEN) { 2038 vid = model; 2039 *pid++ = 0; /* terminate vid, establish pid */ 2040 } else { 2041 vid = NULL; /* vid will stay "ATA " */ 2042 pid = model; /* model is all pid */ 2043 } 2044 2045 if (vid) 2046 (void) scsi_device_prop_update_inqstring(sd, INQUIRY_VENDOR_ID, 2047 vid, strlen(vid)); 2048 if (pid) 2049 (void) scsi_device_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID, 2050 pid, strlen(pid)); 2051 (void) scsi_device_prop_update_inqstring(sd, INQUIRY_REVISION_ID, 2052 fw, strlen(fw)); 2053 2054 return (DDI_SUCCESS); 2055 } 2056 2057 /* 2058 * Implementation of scsi tran_tgt_probe. 2059 * Probe target, by calling default scsi routine scsi_hba_probe() 2060 */ 2061 static int 2062 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void)) 2063 { 2064 sata_hba_inst_t *sata_hba_inst = 2065 (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private); 2066 int rval; 2067 uint32_t pm_cap; 2068 2069 rval = scsi_hba_probe(sd, callback); 2070 pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE | 2071 SATA_CAP_LOG_SENSE; 2072 2073 if (rval == SCSIPROBE_EXISTS) { 2074 /* 2075 * Set property "pm-capable" on the target device node, so that 2076 * the target driver will not try to fetch scsi cycle counters 2077 * before enabling device power-management. 2078 */ 2079 if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev, 2080 "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) { 2081 sata_log(sata_hba_inst, CE_WARN, 2082 "SATA device at port %d: " 2083 "will not be power-managed ", 2084 SCSI_TO_SATA_CPORT(sd->sd_address.a_target)); 2085 SATA_LOG_D((sata_hba_inst, CE_WARN, 2086 "failure updating pm-capable property")); 2087 } 2088 } 2089 return (rval); 2090 } 2091 2092 /* 2093 * Implementation of scsi tran_tgt_free. 2094 * Release all resources allocated for scsi_device 2095 */ 2096 static void 2097 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip, 2098 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 2099 { 2100 #ifndef __lock_lint 2101 _NOTE(ARGUNUSED(hba_dip)) 2102 #endif 2103 sata_device_t sata_device; 2104 sata_drive_info_t *sdinfo; 2105 sata_hba_inst_t *sata_hba_inst; 2106 ddi_devid_t devid; 2107 2108 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 2109 2110 /* Validate scsi device address */ 2111 /* 2112 * Note: tgt_free relates to the SCSA view of a device. If called, there 2113 * was a device at this address, so even if the sata framework internal 2114 * resources were alredy released because a device was detached, 2115 * this function should be executed as long as its actions do 2116 * not require the internal sata view of a device and the address 2117 * refers to a valid sata address. 2118 * Validating the address here means that we do not trust SCSA... 2119 */ 2120 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 2121 &sata_device) == -1) 2122 return; 2123 2124 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2125 sata_device.satadev_addr.cport))); 2126 2127 /* sata_device now should contain a valid sata address */ 2128 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 2129 if (sdinfo == NULL) { 2130 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2131 sata_device.satadev_addr.cport))); 2132 return; 2133 } 2134 /* 2135 * We did not allocate any resources in sata_scsi_tgt_init() 2136 * other than few properties. 2137 * Free them. 2138 */ 2139 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2140 sata_device.satadev_addr.cport))); 2141 (void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable"); 2142 2143 /* 2144 * If devid was previously created but not freed up from 2145 * sd(7D) driver (i.e during detach(9F)) then do it here. 2146 */ 2147 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 2148 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS, 2149 "use-cmdk-devid-format", 0) == 1) && 2150 (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) { 2151 ddi_devid_unregister(tgt_dip); 2152 ddi_devid_free(devid); 2153 } 2154 } 2155 2156 /* 2157 * Implementation of scsi tran_init_pkt 2158 * Upon successful return, scsi pkt buffer has DMA resources allocated. 2159 * 2160 * It seems that we should always allocate pkt, even if the address is 2161 * for non-existing device - just use some default for dma_attr. 2162 * The reason is that there is no way to communicate this to a caller here. 2163 * Subsequent call to sata_scsi_start may fail appropriately. 2164 * Simply returning NULL does not seem to discourage a target driver... 2165 * 2166 * Returns a pointer to initialized scsi_pkt, or NULL otherwise. 2167 */ 2168 static struct scsi_pkt * 2169 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt, 2170 struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags, 2171 int (*callback)(caddr_t), caddr_t arg) 2172 { 2173 sata_hba_inst_t *sata_hba_inst = 2174 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2175 dev_info_t *dip = SATA_DIP(sata_hba_inst); 2176 sata_device_t sata_device; 2177 sata_drive_info_t *sdinfo; 2178 sata_pkt_txlate_t *spx; 2179 ddi_dma_attr_t cur_dma_attr; 2180 int rval; 2181 boolean_t new_pkt = TRUE; 2182 2183 ASSERT(ap->a_hba_tran->tran_hba_dip == dip); 2184 2185 /* 2186 * We need to translate the address, even if it could be 2187 * a bogus one, for a non-existing device 2188 */ 2189 sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 2190 sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target); 2191 sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 2192 sata_device.satadev_rev = SATA_DEVICE_REV; 2193 2194 if (pkt == NULL) { 2195 /* 2196 * Have to allocate a brand new scsi packet. 2197 * We need to operate with auto request sense enabled. 2198 */ 2199 pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen, 2200 MAX(statuslen, SATA_MAX_SENSE_LEN), 2201 tgtlen, sizeof (sata_pkt_txlate_t), callback, arg); 2202 2203 if (pkt == NULL) 2204 return (NULL); 2205 2206 /* Fill scsi packet structure */ 2207 pkt->pkt_comp = (void (*)())NULL; 2208 pkt->pkt_time = 0; 2209 pkt->pkt_resid = 0; 2210 pkt->pkt_statistics = 0; 2211 pkt->pkt_reason = 0; 2212 2213 /* 2214 * pkt_hba_private will point to sata pkt txlate structure 2215 */ 2216 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2217 bzero(spx, sizeof (sata_pkt_txlate_t)); 2218 2219 spx->txlt_scsi_pkt = pkt; 2220 spx->txlt_sata_hba_inst = sata_hba_inst; 2221 2222 /* Allocate sata_pkt */ 2223 spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback); 2224 if (spx->txlt_sata_pkt == NULL) { 2225 /* Could not allocate sata pkt */ 2226 scsi_hba_pkt_free(ap, pkt); 2227 return (NULL); 2228 } 2229 /* Set sata address */ 2230 spx->txlt_sata_pkt->satapkt_device.satadev_addr = 2231 sata_device.satadev_addr; 2232 spx->txlt_sata_pkt->satapkt_device.satadev_rev = 2233 sata_device.satadev_rev; 2234 2235 if ((bp == NULL) || (bp->b_bcount == 0)) 2236 return (pkt); 2237 2238 spx->txlt_total_residue = bp->b_bcount; 2239 } else { 2240 new_pkt = FALSE; 2241 /* 2242 * Packet was preallocated/initialized by previous call 2243 */ 2244 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2245 2246 if ((bp == NULL) || (bp->b_bcount == 0)) { 2247 return (pkt); 2248 } 2249 2250 /* Pkt is available already: spx->txlt_scsi_pkt == pkt; */ 2251 } 2252 2253 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 2254 2255 /* 2256 * We use an adjusted version of the dma_attr, to account 2257 * for device addressing limitations. 2258 * sata_adjust_dma_attr() will handle sdinfo == NULL which may 2259 * happen when a device is not yet configured. 2260 */ 2261 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2262 sata_device.satadev_addr.cport))); 2263 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 2264 &spx->txlt_sata_pkt->satapkt_device); 2265 /* NULL sdinfo may be passsed to sata_adjust_dma_attr() */ 2266 sata_adjust_dma_attr(sdinfo, 2267 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 2268 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2269 sata_device.satadev_addr.cport))); 2270 /* 2271 * Allocate necessary DMA resources for the packet's data buffer 2272 * NOTE: 2273 * In case of read/write commands, DMA resource allocation here is 2274 * based on the premise that the transfer length specified in 2275 * the read/write scsi cdb will match exactly DMA resources - 2276 * returning correct packet residue is crucial. 2277 */ 2278 if ((rval = sata_dma_buf_setup(spx, flags, callback, arg, 2279 &cur_dma_attr)) != DDI_SUCCESS) { 2280 /* 2281 * If a DMA allocation request fails with 2282 * DDI_DMA_NOMAPPING, indicate the error by calling 2283 * bioerror(9F) with bp and an error code of EFAULT. 2284 * If a DMA allocation request fails with 2285 * DDI_DMA_TOOBIG, indicate the error by calling 2286 * bioerror(9F) with bp and an error code of EINVAL. 2287 * For DDI_DMA_NORESOURCES, we may have some of them allocated. 2288 * Request may be repeated later - there is no real error. 2289 */ 2290 switch (rval) { 2291 case DDI_DMA_NORESOURCES: 2292 bioerror(bp, 0); 2293 break; 2294 case DDI_DMA_NOMAPPING: 2295 case DDI_DMA_BADATTR: 2296 bioerror(bp, EFAULT); 2297 break; 2298 case DDI_DMA_TOOBIG: 2299 default: 2300 bioerror(bp, EINVAL); 2301 break; 2302 } 2303 if (new_pkt == TRUE) { 2304 /* 2305 * Since this is a new packet, we can clean-up 2306 * everything 2307 */ 2308 sata_scsi_destroy_pkt(ap, pkt); 2309 } else { 2310 /* 2311 * This is a re-used packet. It will be target driver's 2312 * responsibility to eventually destroy it (which 2313 * will free allocated resources). 2314 * Here, we just "complete" the request, leaving 2315 * allocated resources intact, so the request may 2316 * be retried. 2317 */ 2318 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 2319 sata_pkt_free(spx); 2320 } 2321 return (NULL); 2322 } 2323 /* Set number of bytes that are not yet accounted for */ 2324 pkt->pkt_resid = spx->txlt_total_residue; 2325 ASSERT(pkt->pkt_resid >= 0); 2326 2327 return (pkt); 2328 } 2329 2330 /* 2331 * Implementation of scsi tran_start. 2332 * Translate scsi cmd into sata operation and return status. 2333 * ATAPI CDBs are passed to ATAPI devices - the device determines what commands 2334 * are supported. 2335 * For SATA hard disks, supported scsi commands: 2336 * SCMD_INQUIRY 2337 * SCMD_TEST_UNIT_READY 2338 * SCMD_START_STOP 2339 * SCMD_READ_CAPACITY 2340 * SCMD_REQUEST_SENSE 2341 * SCMD_LOG_SENSE_G1 2342 * SCMD_LOG_SELECT_G1 2343 * SCMD_MODE_SENSE (specific pages) 2344 * SCMD_MODE_SENSE_G1 (specific pages) 2345 * SCMD_MODE_SELECT (specific pages) 2346 * SCMD_MODE_SELECT_G1 (specific pages) 2347 * SCMD_SYNCHRONIZE_CACHE 2348 * SCMD_SYNCHRONIZE_CACHE_G1 2349 * SCMD_READ 2350 * SCMD_READ_G1 2351 * SCMD_READ_G4 2352 * SCMD_READ_G5 2353 * SCMD_WRITE 2354 * SCMD_WRITE_BUFFER 2355 * SCMD_WRITE_G1 2356 * SCMD_WRITE_G4 2357 * SCMD_WRITE_G5 2358 * SCMD_SEEK (noop) 2359 * SCMD_SDIAG 2360 * 2361 * All other commands are rejected as unsupported. 2362 * 2363 * Returns: 2364 * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver 2365 * for execution. TRAN_ACCEPT may be returned also if device was removed but 2366 * a callback could be scheduled. 2367 * TRAN_BADPKT if cmd was directed to invalid address. 2368 * TRAN_FATAL_ERROR is command was rejected due to hardware error, including 2369 * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device 2370 * was removed and there was no callback specified in scsi pkt. 2371 * TRAN_BUSY if command could not be executed becasue HBA driver or SATA 2372 * framework was busy performing some other operation(s). 2373 * 2374 */ 2375 static int 2376 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt) 2377 { 2378 sata_hba_inst_t *sata_hba_inst = 2379 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2380 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2381 sata_device_t *sdevice = &spx->txlt_sata_pkt->satapkt_device; 2382 sata_drive_info_t *sdinfo; 2383 struct buf *bp; 2384 uint8_t cport, pmport; 2385 boolean_t dev_gone = B_FALSE; 2386 int rval; 2387 2388 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2389 "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]); 2390 2391 ASSERT(spx != NULL && 2392 spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL); 2393 2394 cport = SCSI_TO_SATA_CPORT(ap->a_target); 2395 pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 2396 2397 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2398 2399 if (sdevice->satadev_addr.qual == SATA_ADDR_DCPORT) { 2400 sdinfo = sata_get_device_info(sata_hba_inst, sdevice); 2401 if (sdinfo == NULL || 2402 SATA_CPORT_INFO(sata_hba_inst, cport)-> 2403 cport_tgtnode_clean == B_FALSE || 2404 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 2405 dev_gone = B_TRUE; 2406 } 2407 } else if (sdevice->satadev_addr.qual == SATA_ADDR_DPMPORT) { 2408 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 2409 SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst, 2410 cport) == NULL) { 2411 dev_gone = B_TRUE; 2412 } else if (SATA_PMPORT_INFO(sata_hba_inst, cport, 2413 pmport) == NULL) { 2414 dev_gone = B_TRUE; 2415 } else { 2416 mutex_enter(&(SATA_PMPORT_MUTEX(sata_hba_inst, 2417 cport, pmport))); 2418 sdinfo = sata_get_device_info(sata_hba_inst, sdevice); 2419 if (sdinfo == NULL || 2420 SATA_PMPORT_INFO(sata_hba_inst, cport, pmport)-> 2421 pmport_tgtnode_clean == B_FALSE || 2422 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 2423 dev_gone = B_TRUE; 2424 } 2425 mutex_exit(&(SATA_PMPORT_MUTEX(sata_hba_inst, 2426 cport, pmport))); 2427 } 2428 } 2429 2430 if (dev_gone == B_TRUE) { 2431 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2432 pkt->pkt_reason = CMD_DEV_GONE; 2433 /* 2434 * The sd target driver is checking CMD_DEV_GONE pkt_reason 2435 * only in callback function (for normal requests) and 2436 * in the dump code path. 2437 * So, if the callback is available, we need to do 2438 * the callback rather than returning TRAN_FATAL_ERROR here. 2439 */ 2440 if (pkt->pkt_comp != NULL) { 2441 /* scsi callback required */ 2442 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 2443 (task_func_t *)pkt->pkt_comp, 2444 (void *)pkt, TQ_NOSLEEP) == NULL) 2445 /* Scheduling the callback failed */ 2446 return (TRAN_BUSY); 2447 return (TRAN_ACCEPT); 2448 } 2449 /* No callback available */ 2450 return (TRAN_FATAL_ERROR); 2451 } 2452 2453 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 2454 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2455 rval = sata_txlt_atapi(spx); 2456 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2457 "sata_scsi_start atapi: rval %d\n", rval); 2458 return (rval); 2459 } 2460 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2461 2462 /* 2463 * Checking for power state, if it was on 2464 * STOPPED state, then the drive is not capable 2465 * of processing media access command. And 2466 * TEST_UNIT_READY, REQUEST_SENSE has special handling 2467 * in the function for different power state. 2468 */ 2469 if (((sdinfo->satadrv_power_level == SATA_POWER_STANDBY) || 2470 (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)) && 2471 (SATA_IS_MEDIUM_ACCESS_CMD(pkt->pkt_cdbp[0]))) { 2472 return (sata_txlt_check_condition(spx, KEY_NOT_READY, 2473 SD_SCSI_ASC_LU_NOT_READY)); 2474 } 2475 2476 /* ATA Disk commands processing starts here */ 2477 2478 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 2479 2480 switch (pkt->pkt_cdbp[0]) { 2481 2482 case SCMD_INQUIRY: 2483 /* Mapped to identify device */ 2484 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2485 bp_mapin(bp); 2486 rval = sata_txlt_inquiry(spx); 2487 break; 2488 2489 case SCMD_TEST_UNIT_READY: 2490 /* 2491 * SAT "SATA to ATA Translation" doc specifies translation 2492 * to ATA CHECK POWER MODE. 2493 */ 2494 rval = sata_txlt_test_unit_ready(spx); 2495 break; 2496 2497 case SCMD_START_STOP: 2498 /* Mapping depends on the command */ 2499 rval = sata_txlt_start_stop_unit(spx); 2500 break; 2501 2502 case SCMD_READ_CAPACITY: 2503 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2504 bp_mapin(bp); 2505 rval = sata_txlt_read_capacity(spx); 2506 break; 2507 2508 case SCMD_REQUEST_SENSE: 2509 /* 2510 * Always No Sense, since we force ARQ 2511 */ 2512 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2513 bp_mapin(bp); 2514 rval = sata_txlt_request_sense(spx); 2515 break; 2516 2517 case SCMD_LOG_SENSE_G1: 2518 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2519 bp_mapin(bp); 2520 rval = sata_txlt_log_sense(spx); 2521 break; 2522 2523 case SCMD_LOG_SELECT_G1: 2524 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2525 bp_mapin(bp); 2526 rval = sata_txlt_log_select(spx); 2527 break; 2528 2529 case SCMD_MODE_SENSE: 2530 case SCMD_MODE_SENSE_G1: 2531 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2532 bp_mapin(bp); 2533 rval = sata_txlt_mode_sense(spx); 2534 break; 2535 2536 2537 case SCMD_MODE_SELECT: 2538 case SCMD_MODE_SELECT_G1: 2539 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2540 bp_mapin(bp); 2541 rval = sata_txlt_mode_select(spx); 2542 break; 2543 2544 case SCMD_SYNCHRONIZE_CACHE: 2545 case SCMD_SYNCHRONIZE_CACHE_G1: 2546 rval = sata_txlt_synchronize_cache(spx); 2547 break; 2548 2549 case SCMD_READ: 2550 case SCMD_READ_G1: 2551 case SCMD_READ_G4: 2552 case SCMD_READ_G5: 2553 rval = sata_txlt_read(spx); 2554 break; 2555 case SCMD_WRITE_BUFFER: 2556 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2557 bp_mapin(bp); 2558 rval = sata_txlt_write_buffer(spx); 2559 break; 2560 2561 case SCMD_WRITE: 2562 case SCMD_WRITE_G1: 2563 case SCMD_WRITE_G4: 2564 case SCMD_WRITE_G5: 2565 rval = sata_txlt_write(spx); 2566 break; 2567 2568 case SCMD_SEEK: 2569 rval = sata_txlt_nodata_cmd_immediate(spx); 2570 break; 2571 2572 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12: 2573 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16: 2574 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2575 bp_mapin(bp); 2576 rval = sata_txlt_ata_pass_thru(spx); 2577 break; 2578 2579 /* Other cases will be filed later */ 2580 /* postponed until phase 2 of the development */ 2581 default: 2582 rval = sata_txlt_invalid_command(spx); 2583 break; 2584 } 2585 2586 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2587 "sata_scsi_start: rval %d\n", rval); 2588 2589 return (rval); 2590 } 2591 2592 /* 2593 * Implementation of scsi tran_abort. 2594 * Abort specific pkt or all packets. 2595 * 2596 * Returns 1 if one or more packets were aborted, returns 0 otherwise 2597 * 2598 * May be called from an interrupt level. 2599 */ 2600 static int 2601 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt) 2602 { 2603 sata_hba_inst_t *sata_hba_inst = 2604 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2605 sata_device_t sata_device; 2606 sata_pkt_t *sata_pkt; 2607 2608 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2609 "sata_scsi_abort: %s at target: 0x%x\n", 2610 scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target); 2611 2612 /* Validate address */ 2613 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) 2614 /* Invalid address */ 2615 return (0); 2616 2617 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2618 sata_device.satadev_addr.cport))); 2619 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) { 2620 /* invalid address */ 2621 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2622 sata_device.satadev_addr.cport))); 2623 return (0); 2624 } 2625 if (scsi_pkt == NULL) { 2626 /* 2627 * Abort all packets. 2628 * Although we do not have specific packet, we still need 2629 * dummy packet structure to pass device address to HBA. 2630 * Allocate one, without sleeping. Fail if pkt cannot be 2631 * allocated. 2632 */ 2633 sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP); 2634 if (sata_pkt == NULL) { 2635 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2636 sata_device.satadev_addr.cport))); 2637 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: " 2638 "could not allocate sata_pkt")); 2639 return (0); 2640 } 2641 sata_pkt->satapkt_rev = SATA_PKT_REV; 2642 sata_pkt->satapkt_device = sata_device; 2643 sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 2644 } else { 2645 if (scsi_pkt->pkt_ha_private == NULL) { 2646 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2647 sata_device.satadev_addr.cport))); 2648 return (0); /* Bad scsi pkt */ 2649 } 2650 /* extract pointer to sata pkt */ 2651 sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)-> 2652 txlt_sata_pkt; 2653 } 2654 2655 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2656 sata_device.satadev_addr.cport))); 2657 /* Send abort request to HBA */ 2658 if ((*SATA_ABORT_FUNC(sata_hba_inst)) 2659 (SATA_DIP(sata_hba_inst), sata_pkt, 2660 scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) == 2661 SATA_SUCCESS) { 2662 if (scsi_pkt == NULL) 2663 kmem_free(sata_pkt, sizeof (sata_pkt_t)); 2664 /* Success */ 2665 return (1); 2666 } 2667 /* Else, something did not go right */ 2668 if (scsi_pkt == NULL) 2669 kmem_free(sata_pkt, sizeof (sata_pkt_t)); 2670 /* Failure */ 2671 return (0); 2672 } 2673 2674 2675 /* 2676 * Implementation of scsi tran_reset. 2677 * RESET_ALL request is translated into port reset. 2678 * RESET_TARGET requests is translated into a device reset, 2679 * RESET_LUN request is accepted only for LUN 0 and translated into 2680 * device reset. 2681 * The target reset should cause all HBA active and queued packets to 2682 * be terminated and returned with pkt reason SATA_PKT_RESET prior to 2683 * the return. HBA should report reset event for the device. 2684 * 2685 * Returns 1 upon success, 0 upon failure. 2686 */ 2687 static int 2688 sata_scsi_reset(struct scsi_address *ap, int level) 2689 { 2690 sata_hba_inst_t *sata_hba_inst = 2691 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2692 sata_device_t sata_device; 2693 int val; 2694 2695 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2696 "sata_scsi_reset: level %d target: 0x%x\n", 2697 level, ap->a_target); 2698 2699 /* Validate address */ 2700 val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device); 2701 if (val == -1) 2702 /* Invalid address */ 2703 return (0); 2704 2705 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2706 sata_device.satadev_addr.cport))); 2707 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) { 2708 /* invalid address */ 2709 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2710 sata_device.satadev_addr.cport))); 2711 return (0); 2712 } 2713 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2714 sata_device.satadev_addr.cport))); 2715 if (level == RESET_ALL) { 2716 /* port reset */ 2717 if (sata_device.satadev_addr.qual == SATA_ADDR_DCPORT) 2718 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 2719 else 2720 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT; 2721 2722 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 2723 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS) 2724 return (1); 2725 else 2726 return (0); 2727 2728 } else if (val == 0 && 2729 (level == RESET_TARGET || level == RESET_LUN)) { 2730 /* reset device (device attached) */ 2731 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 2732 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS) 2733 return (1); 2734 else 2735 return (0); 2736 } 2737 return (0); 2738 } 2739 2740 2741 /* 2742 * Implementation of scsi tran_getcap (get transport/device capabilities). 2743 * Supported capabilities for SATA hard disks: 2744 * auto-rqsense (always supported) 2745 * tagged-qing (supported if HBA supports it) 2746 * untagged-qing (could be supported if disk supports it, but because 2747 * caching behavior allowing untagged queuing actually 2748 * results in reduced performance. sd tries to throttle 2749 * back to only 3 outstanding commands, which may 2750 * work for real SCSI disks, but with read ahead 2751 * caching, having more than 1 outstanding command 2752 * results in cache thrashing.) 2753 * sector_size 2754 * dma_max 2755 * interconnect-type (INTERCONNECT_SATA) 2756 * 2757 * Supported capabilities for ATAPI CD/DVD devices: 2758 * auto-rqsense (always supported) 2759 * sector_size 2760 * dma_max 2761 * max-cdb-length 2762 * interconnect-type (INTERCONNECT_SATA) 2763 * 2764 * Supported capabilities for ATAPI TAPE devices: 2765 * auto-rqsense (always supported) 2766 * dma_max 2767 * max-cdb-length 2768 * 2769 * Supported capabilities for SATA ATAPI hard disks: 2770 * auto-rqsense (always supported) 2771 * interconnect-type (INTERCONNECT_SATA) 2772 * max-cdb-length 2773 * 2774 * Request for other capabilities is rejected as unsupported. 2775 * 2776 * Returns supported capability value, or -1 if capability is unsuppported or 2777 * the address is invalid - no device. 2778 */ 2779 2780 static int 2781 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom) 2782 { 2783 2784 sata_hba_inst_t *sata_hba_inst = 2785 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2786 sata_device_t sata_device; 2787 sata_drive_info_t *sdinfo; 2788 ddi_dma_attr_t adj_dma_attr; 2789 int rval; 2790 2791 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2792 "sata_scsi_getcap: target: 0x%x, cap: %s\n", 2793 ap->a_target, cap); 2794 2795 /* 2796 * We want to process the capabilities on per port granularity. 2797 * So, we are specifically restricting ourselves to whom != 0 2798 * to exclude the controller wide handling. 2799 */ 2800 if (cap == NULL || whom == 0) 2801 return (-1); 2802 2803 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) { 2804 /* Invalid address */ 2805 return (-1); 2806 } 2807 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2808 sata_device.satadev_addr.cport))); 2809 if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) == 2810 NULL) { 2811 /* invalid address */ 2812 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2813 sata_device.satadev_addr.cport))); 2814 return (-1); 2815 } 2816 2817 switch (scsi_hba_lookup_capstr(cap)) { 2818 case SCSI_CAP_ARQ: 2819 rval = 1; /* ARQ supported, turned on */ 2820 break; 2821 2822 case SCSI_CAP_SECTOR_SIZE: 2823 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) 2824 rval = SATA_DISK_SECTOR_SIZE; /* fixed size */ 2825 else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) 2826 rval = SATA_ATAPI_SECTOR_SIZE; 2827 else rval = -1; 2828 break; 2829 2830 /* 2831 * untagged queuing cause a performance inversion because of 2832 * the way sd operates. Because of this reason we do not 2833 * use it when available. 2834 */ 2835 case SCSI_CAP_UNTAGGED_QING: 2836 if (sdinfo->satadrv_features_enabled & 2837 SATA_DEV_F_E_UNTAGGED_QING) 2838 rval = 1; /* Untagged queuing available */ 2839 else 2840 rval = -1; /* Untagged queuing not available */ 2841 break; 2842 2843 case SCSI_CAP_TAGGED_QING: 2844 if ((sdinfo->satadrv_features_enabled & 2845 SATA_DEV_F_E_TAGGED_QING) && 2846 (sdinfo->satadrv_max_queue_depth > 1)) 2847 rval = 1; /* Tagged queuing available */ 2848 else 2849 rval = -1; /* Tagged queuing not available */ 2850 break; 2851 2852 case SCSI_CAP_DMA_MAX: 2853 sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst), 2854 &adj_dma_attr); 2855 rval = (int)adj_dma_attr.dma_attr_maxxfer; 2856 /* We rely on the fact that dma_attr_maxxfer < 0x80000000 */ 2857 break; 2858 2859 case SCSI_CAP_INTERCONNECT_TYPE: 2860 rval = INTERCONNECT_SATA; /* SATA interconnect type */ 2861 break; 2862 2863 case SCSI_CAP_CDB_LEN: 2864 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) 2865 rval = sdinfo->satadrv_atapi_cdb_len; 2866 else 2867 rval = -1; 2868 break; 2869 2870 default: 2871 rval = -1; 2872 break; 2873 } 2874 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2875 sata_device.satadev_addr.cport))); 2876 return (rval); 2877 } 2878 2879 /* 2880 * Implementation of scsi tran_setcap 2881 * 2882 * Only SCSI_CAP_UNTAGGED_QING and SCSI_CAP_TAGGED_QING are changeable. 2883 * 2884 */ 2885 static int 2886 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom) 2887 { 2888 sata_hba_inst_t *sata_hba_inst = 2889 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2890 sata_device_t sata_device; 2891 sata_drive_info_t *sdinfo; 2892 int rval; 2893 2894 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2895 "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap); 2896 2897 /* 2898 * We want to process the capabilities on per port granularity. 2899 * So, we are specifically restricting ourselves to whom != 0 2900 * to exclude the controller wide handling. 2901 */ 2902 if (cap == NULL || whom == 0) { 2903 return (-1); 2904 } 2905 2906 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) { 2907 /* Invalid address */ 2908 return (-1); 2909 } 2910 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2911 sata_device.satadev_addr.cport))); 2912 if ((sdinfo = sata_get_device_info(sata_hba_inst, 2913 &sata_device)) == NULL) { 2914 /* invalid address */ 2915 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2916 sata_device.satadev_addr.cport))); 2917 return (-1); 2918 } 2919 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2920 sata_device.satadev_addr.cport))); 2921 2922 switch (scsi_hba_lookup_capstr(cap)) { 2923 case SCSI_CAP_ARQ: 2924 case SCSI_CAP_SECTOR_SIZE: 2925 case SCSI_CAP_DMA_MAX: 2926 case SCSI_CAP_INTERCONNECT_TYPE: 2927 rval = 0; 2928 break; 2929 case SCSI_CAP_UNTAGGED_QING: 2930 if (SATA_QDEPTH(sata_hba_inst) > 1) { 2931 rval = 1; 2932 if (value == 1) { 2933 sdinfo->satadrv_features_enabled |= 2934 SATA_DEV_F_E_UNTAGGED_QING; 2935 } else if (value == 0) { 2936 sdinfo->satadrv_features_enabled &= 2937 ~SATA_DEV_F_E_UNTAGGED_QING; 2938 } else { 2939 rval = -1; 2940 } 2941 } else { 2942 rval = 0; 2943 } 2944 break; 2945 case SCSI_CAP_TAGGED_QING: 2946 /* This can TCQ or NCQ */ 2947 if (sata_func_enable & SATA_ENABLE_QUEUING && 2948 ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ && 2949 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) || 2950 (sata_func_enable & SATA_ENABLE_NCQ && 2951 sdinfo->satadrv_features_support & SATA_DEV_F_NCQ && 2952 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) && 2953 (sdinfo->satadrv_max_queue_depth > 1)) { 2954 rval = 1; 2955 if (value == 1) { 2956 sdinfo->satadrv_features_enabled |= 2957 SATA_DEV_F_E_TAGGED_QING; 2958 } else if (value == 0) { 2959 sdinfo->satadrv_features_enabled &= 2960 ~SATA_DEV_F_E_TAGGED_QING; 2961 } else { 2962 rval = -1; 2963 } 2964 } else { 2965 rval = 0; 2966 } 2967 break; 2968 default: 2969 rval = -1; 2970 break; 2971 } 2972 return (rval); 2973 } 2974 2975 /* 2976 * Implementations of scsi tran_destroy_pkt. 2977 * Free resources allocated by sata_scsi_init_pkt() 2978 */ 2979 static void 2980 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 2981 { 2982 sata_pkt_txlate_t *spx; 2983 2984 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2985 2986 sata_common_free_dma_rsrcs(spx); 2987 2988 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 2989 sata_pkt_free(spx); 2990 2991 scsi_hba_pkt_free(ap, pkt); 2992 } 2993 2994 /* 2995 * Implementation of scsi tran_dmafree. 2996 * Free DMA resources allocated by sata_scsi_init_pkt() 2997 */ 2998 2999 static void 3000 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt) 3001 { 3002 #ifndef __lock_lint 3003 _NOTE(ARGUNUSED(ap)) 3004 #endif 3005 sata_pkt_txlate_t *spx; 3006 3007 ASSERT(pkt != NULL); 3008 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 3009 3010 sata_common_free_dma_rsrcs(spx); 3011 } 3012 3013 /* 3014 * Implementation of scsi tran_sync_pkt. 3015 * 3016 * The assumption below is that pkt is unique - there is no need to check ap 3017 * 3018 * Synchronize DMA buffer and, if the intermediate buffer is used, copy data 3019 * into/from the real buffer. 3020 */ 3021 static void 3022 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 3023 { 3024 #ifndef __lock_lint 3025 _NOTE(ARGUNUSED(ap)) 3026 #endif 3027 int rval; 3028 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 3029 struct buf *bp; 3030 int direction; 3031 3032 ASSERT(spx != NULL); 3033 if (spx->txlt_buf_dma_handle != NULL) { 3034 direction = spx->txlt_sata_pkt-> 3035 satapkt_cmd.satacmd_flags.sata_data_direction; 3036 if (spx->txlt_sata_pkt != NULL && 3037 direction != SATA_DIR_NODATA_XFER) { 3038 if (spx->txlt_tmp_buf != NULL) { 3039 /* Intermediate DMA buffer used */ 3040 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3041 3042 if (direction & SATA_DIR_WRITE) { 3043 bcopy(bp->b_un.b_addr, 3044 spx->txlt_tmp_buf, bp->b_bcount); 3045 } 3046 } 3047 /* Sync the buffer for device or for CPU */ 3048 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 3049 (direction & SATA_DIR_WRITE) ? 3050 DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU); 3051 ASSERT(rval == DDI_SUCCESS); 3052 if (spx->txlt_tmp_buf != NULL && 3053 !(direction & SATA_DIR_WRITE)) { 3054 /* Intermediate DMA buffer used for read */ 3055 bcopy(spx->txlt_tmp_buf, 3056 bp->b_un.b_addr, bp->b_bcount); 3057 } 3058 3059 } 3060 } 3061 } 3062 3063 3064 3065 /* ******************* SATA - SCSI Translation functions **************** */ 3066 /* 3067 * SCSI to SATA pkt and command translation and SATA to SCSI status/error 3068 * translation. 3069 */ 3070 3071 /* 3072 * Checks if a device exists and can be access and translates common 3073 * scsi_pkt data to sata_pkt data. 3074 * 3075 * Flag argument indicates that a non-read/write ATA command may be sent 3076 * to HBA in arbitrary SYNC mode to execute this packet. 3077 * 3078 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and 3079 * sata_pkt was set-up. 3080 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not 3081 * exist and pkt_comp callback was scheduled. 3082 * Returns other TRAN_XXXXX values when error occured and command should be 3083 * rejected with the returned TRAN_XXXXX value. 3084 * 3085 * This function should be called with port mutex held. 3086 */ 3087 static int 3088 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason, int flag) 3089 { 3090 sata_drive_info_t *sdinfo; 3091 sata_device_t sata_device; 3092 const struct sata_cmd_flags sata_initial_cmd_flags = { 3093 SATA_DIR_NODATA_XFER, 3094 /* all other values to 0/FALSE */ 3095 }; 3096 /* 3097 * Pkt_reason has to be set if the pkt_comp callback is invoked, 3098 * and that implies TRAN_ACCEPT return value. Any other returned value 3099 * indicates that the scsi packet was not accepted (the reason will not 3100 * be checked by the scsi target driver). 3101 * To make debugging easier, we set pkt_reason to know value here. 3102 * It may be changed later when different completion reason is 3103 * determined. 3104 */ 3105 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 3106 *reason = CMD_TRAN_ERR; 3107 3108 /* Validate address */ 3109 switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst, 3110 &spx->txlt_scsi_pkt->pkt_address, &sata_device)) { 3111 3112 case -1: 3113 /* Invalid address or invalid device type */ 3114 return (TRAN_BADPKT); 3115 case 2: 3116 /* 3117 * Valid address but device type is unknown - Chack if it is 3118 * in the reset state and therefore in an indeterminate state. 3119 */ 3120 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3121 &spx->txlt_sata_pkt->satapkt_device); 3122 if (sdinfo != NULL && (sdinfo->satadrv_event_flags & 3123 (SATA_EVNT_DEVICE_RESET | 3124 SATA_EVNT_INPROC_DEVICE_RESET)) != 0) { 3125 if (!ddi_in_panic()) { 3126 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 3127 *reason = CMD_INCOMPLETE; 3128 SATADBG1(SATA_DBG_SCSI_IF, 3129 spx->txlt_sata_hba_inst, 3130 "sata_scsi_start: rejecting command " 3131 "because of device reset state\n", NULL); 3132 return (TRAN_BUSY); 3133 } 3134 } 3135 /* FALLTHROUGH */ 3136 case 1: 3137 /* valid address but no valid device - it has disappeared */ 3138 spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE; 3139 *reason = CMD_DEV_GONE; 3140 /* 3141 * The sd target driver is checking CMD_DEV_GONE pkt_reason 3142 * only in callback function (for normal requests) and 3143 * in the dump code path. 3144 * So, if the callback is available, we need to do 3145 * the callback rather than returning TRAN_FATAL_ERROR here. 3146 */ 3147 if (spx->txlt_scsi_pkt->pkt_comp != NULL) { 3148 /* scsi callback required */ 3149 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3150 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3151 (void *)spx->txlt_scsi_pkt, 3152 TQ_SLEEP) == NULL) 3153 /* Scheduling the callback failed */ 3154 return (TRAN_BUSY); 3155 3156 return (TRAN_ACCEPT); 3157 } 3158 return (TRAN_FATAL_ERROR); 3159 default: 3160 /* all OK; pkt reason will be overwritten later */ 3161 break; 3162 } 3163 /* 3164 * If pkt is to be executed in polling mode and a command will not be 3165 * emulated in SATA module (requires sending a non-read/write ATA 3166 * command to HBA driver in arbitrary SYNC mode) and we are in the 3167 * interrupt context and not in the panic dump, then reject the packet 3168 * to avoid a possible interrupt stack overrun or hang caused by 3169 * a potentially blocked interrupt. 3170 */ 3171 if (((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0 || flag != 0) && 3172 servicing_interrupt() && !ddi_in_panic()) { 3173 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 3174 "sata_scsi_start: rejecting synchronous command because " 3175 "of interrupt context\n", NULL); 3176 return (TRAN_BUSY); 3177 } 3178 3179 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3180 &spx->txlt_sata_pkt->satapkt_device); 3181 3182 /* 3183 * If device is in reset condition, reject the packet with 3184 * TRAN_BUSY, unless: 3185 * 1. system is panicking (dumping) 3186 * In such case only one thread is running and there is no way to 3187 * process reset. 3188 * 2. cfgadm operation is is progress (internal APCTL lock is set) 3189 * Some cfgadm operations involve drive commands, so reset condition 3190 * needs to be ignored for IOCTL operations. 3191 */ 3192 if ((sdinfo->satadrv_event_flags & 3193 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) { 3194 3195 if (!ddi_in_panic() && 3196 ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst, 3197 sata_device.satadev_addr.cport) & 3198 SATA_APCTL_LOCK_PORT_BUSY) == 0)) { 3199 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 3200 *reason = CMD_INCOMPLETE; 3201 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3202 "sata_scsi_start: rejecting command because " 3203 "of device reset state\n", NULL); 3204 return (TRAN_BUSY); 3205 } 3206 } 3207 3208 /* 3209 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by 3210 * sata_scsi_pkt_init() because pkt init had to work also with 3211 * non-existing devices. 3212 * Now we know that the packet was set-up for a real device, so its 3213 * type is known. 3214 */ 3215 spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type; 3216 3217 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags; 3218 if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst, 3219 sata_device.satadev_addr.cport)->cport_event_flags & 3220 SATA_APCTL_LOCK_PORT_BUSY) != 0) { 3221 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 3222 sata_ignore_dev_reset = B_TRUE; 3223 } 3224 /* 3225 * At this point the generic translation routine determined that the 3226 * scsi packet should be accepted. Packet completion reason may be 3227 * changed later when a different completion reason is determined. 3228 */ 3229 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 3230 *reason = CMD_CMPLT; 3231 3232 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) { 3233 /* Synchronous execution */ 3234 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH | 3235 SATA_OPMODE_POLLING; 3236 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 3237 sata_ignore_dev_reset = ddi_in_panic(); 3238 } else { 3239 /* Asynchronous execution */ 3240 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH | 3241 SATA_OPMODE_INTERRUPTS; 3242 } 3243 /* Convert queuing information */ 3244 if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG) 3245 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag = 3246 B_TRUE; 3247 else if (spx->txlt_scsi_pkt->pkt_flags & 3248 (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD)) 3249 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag = 3250 B_TRUE; 3251 3252 /* Always limit pkt time */ 3253 if (spx->txlt_scsi_pkt->pkt_time == 0) 3254 spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time; 3255 else 3256 /* Pass on scsi_pkt time */ 3257 spx->txlt_sata_pkt->satapkt_time = 3258 spx->txlt_scsi_pkt->pkt_time; 3259 3260 return (TRAN_ACCEPT); 3261 } 3262 3263 3264 /* 3265 * Translate ATA Identify Device data to SCSI Inquiry data. 3266 * This function may be called only for ATA devices. 3267 * This function should not be called for ATAPI devices - they 3268 * respond directly to SCSI Inquiry command. 3269 * 3270 * SATA Identify Device data has to be valid in sata_drive_info. 3271 * Buffer has to accomodate the inquiry length (36 bytes). 3272 * 3273 * This function should be called with a port mutex held. 3274 */ 3275 static void 3276 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst, 3277 sata_drive_info_t *sdinfo, uint8_t *buf) 3278 { 3279 3280 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 3281 struct sata_id *sid = &sdinfo->satadrv_id; 3282 3283 /* Start with a nice clean slate */ 3284 bzero((void *)inq, sizeof (struct scsi_inquiry)); 3285 3286 /* 3287 * Rely on the dev_type for setting paripheral qualifier. 3288 * Assume that DTYPE_RODIRECT applies to CD/DVD R/W devices. 3289 * It could be that DTYPE_OPTICAL could also qualify in the future. 3290 * ATAPI Inquiry may provide more data to the target driver. 3291 */ 3292 inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 3293 DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */ 3294 3295 /* CFA type device is not a removable media device */ 3296 inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) && 3297 (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0; 3298 inq->inq_qual = 0; /* Device type qualifier (obsolete in SCSI3? */ 3299 inq->inq_iso = 0; /* ISO version */ 3300 inq->inq_ecma = 0; /* ECMA version */ 3301 inq->inq_ansi = 3; /* ANSI version - SCSI 3 */ 3302 inq->inq_aenc = 0; /* Async event notification cap. */ 3303 inq->inq_trmiop = 0; /* Supports TERMINATE I/O PROC msg - NO */ 3304 inq->inq_normaca = 0; /* setting NACA bit supported - NO */ 3305 inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */ 3306 inq->inq_len = 31; /* Additional length */ 3307 inq->inq_dualp = 0; /* dual port device - NO */ 3308 inq->inq_reladdr = 0; /* Supports relative addressing - NO */ 3309 inq->inq_sync = 0; /* Supports synchronous data xfers - NO */ 3310 inq->inq_linked = 0; /* Supports linked commands - NO */ 3311 /* 3312 * Queuing support - controller has to 3313 * support some sort of command queuing. 3314 */ 3315 if (SATA_QDEPTH(sata_hba_inst) > 1) 3316 inq->inq_cmdque = 1; /* Supports command queueing - YES */ 3317 else 3318 inq->inq_cmdque = 0; /* Supports command queueing - NO */ 3319 inq->inq_sftre = 0; /* Supports Soft Reset option - NO ??? */ 3320 inq->inq_wbus32 = 0; /* Supports 32 bit wide data xfers - NO */ 3321 inq->inq_wbus16 = 0; /* Supports 16 bit wide data xfers - NO */ 3322 3323 #ifdef _LITTLE_ENDIAN 3324 /* Swap text fields to match SCSI format */ 3325 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 3326 swab(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 3327 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 3328 swab(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 3329 else 3330 swab(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 3331 #else /* _LITTLE_ENDIAN */ 3332 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 3333 bcopy(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 3334 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 3335 bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 3336 else 3337 bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 3338 #endif /* _LITTLE_ENDIAN */ 3339 } 3340 3341 3342 /* 3343 * Scsi response set up for invalid command (command not supported) 3344 * 3345 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3346 */ 3347 static int 3348 sata_txlt_invalid_command(sata_pkt_txlate_t *spx) 3349 { 3350 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3351 struct scsi_extended_sense *sense; 3352 3353 scsipkt->pkt_reason = CMD_CMPLT; 3354 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3355 STATE_SENT_CMD | STATE_GOT_STATUS; 3356 3357 *scsipkt->pkt_scbp = STATUS_CHECK; 3358 3359 sense = sata_arq_sense(spx); 3360 sense->es_key = KEY_ILLEGAL_REQUEST; 3361 sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE; 3362 3363 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3364 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3365 3366 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3367 scsipkt->pkt_comp != NULL) 3368 /* scsi callback required */ 3369 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3370 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3371 (void *)spx->txlt_scsi_pkt, 3372 TQ_SLEEP) == NULL) 3373 /* Scheduling the callback failed */ 3374 return (TRAN_BUSY); 3375 return (TRAN_ACCEPT); 3376 } 3377 3378 /* 3379 * Scsi response set up for check condition with special sense key 3380 * and additional sense code. 3381 * 3382 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3383 */ 3384 static int 3385 sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code) 3386 { 3387 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 3388 int cport = SATA_TXLT_CPORT(spx); 3389 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3390 struct scsi_extended_sense *sense; 3391 3392 mutex_enter(&SATA_CPORT_MUTEX(shi, cport)); 3393 scsipkt->pkt_reason = CMD_CMPLT; 3394 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3395 STATE_SENT_CMD | STATE_GOT_STATUS; 3396 3397 *scsipkt->pkt_scbp = STATUS_CHECK; 3398 3399 sense = sata_arq_sense(spx); 3400 sense->es_key = key; 3401 sense->es_add_code = code; 3402 3403 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3404 3405 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3406 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3407 3408 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 3409 /* scsi callback required */ 3410 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3411 (task_func_t *)scsi_hba_pkt_comp, 3412 (void *)spx->txlt_scsi_pkt, 3413 TQ_SLEEP) == NULL) 3414 /* Scheduling the callback failed */ 3415 return (TRAN_BUSY); 3416 return (TRAN_ACCEPT); 3417 } 3418 3419 /* 3420 * Scsi response setup for 3421 * emulated non-data command that requires no action/return data 3422 * 3423 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3424 */ 3425 static int 3426 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx) 3427 { 3428 int rval; 3429 int reason; 3430 3431 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3432 3433 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 3434 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3435 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3436 return (rval); 3437 } 3438 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3439 3440 spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3441 STATE_SENT_CMD | STATE_GOT_STATUS; 3442 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 3443 *(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD; 3444 3445 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3446 "Scsi_pkt completion reason %x\n", 3447 spx->txlt_scsi_pkt->pkt_reason); 3448 3449 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 && 3450 spx->txlt_scsi_pkt->pkt_comp != NULL) 3451 /* scsi callback required */ 3452 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3453 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3454 (void *)spx->txlt_scsi_pkt, 3455 TQ_SLEEP) == NULL) 3456 /* Scheduling the callback failed */ 3457 return (TRAN_BUSY); 3458 return (TRAN_ACCEPT); 3459 } 3460 3461 3462 /* 3463 * SATA translate command: Inquiry / Identify Device 3464 * Use cached Identify Device data for now, rather than issuing actual 3465 * Device Identify cmd request. If device is detached and re-attached, 3466 * asynchronous event processing should fetch and refresh Identify Device 3467 * data. 3468 * Two VPD pages are supported now: 3469 * Vital Product Data page 3470 * Unit Serial Number page 3471 * 3472 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3473 */ 3474 3475 #define EVPD 1 /* Extended Vital Product Data flag */ 3476 #define CMDDT 2 /* Command Support Data - Obsolete */ 3477 #define INQUIRY_SUP_VPD_PAGE 0 /* Supported VDP Pages Page COde */ 3478 #define INQUIRY_USN_PAGE 0x80 /* Unit Serial Number Page Code */ 3479 #define INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */ 3480 3481 static int 3482 sata_txlt_inquiry(sata_pkt_txlate_t *spx) 3483 { 3484 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3485 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3486 sata_drive_info_t *sdinfo; 3487 struct scsi_extended_sense *sense; 3488 int count; 3489 uint8_t *p; 3490 int i, j; 3491 uint8_t page_buf[0xff]; /* Max length */ 3492 int rval, reason; 3493 3494 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3495 3496 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 3497 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3498 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3499 return (rval); 3500 } 3501 3502 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3503 &spx->txlt_sata_pkt->satapkt_device); 3504 3505 ASSERT(sdinfo != NULL); 3506 3507 scsipkt->pkt_reason = CMD_CMPLT; 3508 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3509 STATE_SENT_CMD | STATE_GOT_STATUS; 3510 3511 /* Reject not supported request */ 3512 if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */ 3513 *scsipkt->pkt_scbp = STATUS_CHECK; 3514 sense = sata_arq_sense(spx); 3515 sense->es_key = KEY_ILLEGAL_REQUEST; 3516 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3517 goto done; 3518 } 3519 3520 /* Valid Inquiry request */ 3521 *scsipkt->pkt_scbp = STATUS_GOOD; 3522 3523 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 3524 3525 /* 3526 * Because it is fully emulated command storing data 3527 * programatically in the specified buffer, release 3528 * preallocated DMA resources before storing data in the buffer, 3529 * so no unwanted DMA sync would take place. 3530 */ 3531 sata_scsi_dmafree(NULL, scsipkt); 3532 3533 if (!(scsipkt->pkt_cdbp[1] & EVPD)) { 3534 /* Standard Inquiry Data request */ 3535 struct scsi_inquiry inq; 3536 unsigned int bufsize; 3537 3538 sata_identdev_to_inquiry(spx->txlt_sata_hba_inst, 3539 sdinfo, (uint8_t *)&inq); 3540 /* Copy no more than requested */ 3541 count = MIN(bp->b_bcount, 3542 sizeof (struct scsi_inquiry)); 3543 bufsize = scsipkt->pkt_cdbp[4]; 3544 bufsize |= scsipkt->pkt_cdbp[3] << 8; 3545 count = MIN(count, bufsize); 3546 bcopy(&inq, bp->b_un.b_addr, count); 3547 3548 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3549 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 3550 bufsize - count : 0; 3551 } else { 3552 /* 3553 * peripheral_qualifier = 0; 3554 * 3555 * We are dealing only with HD and will be 3556 * dealing with CD/DVD devices soon 3557 */ 3558 uint8_t peripheral_device_type = 3559 sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 3560 DTYPE_DIRECT : DTYPE_RODIRECT; 3561 3562 switch ((uint_t)scsipkt->pkt_cdbp[2]) { 3563 case INQUIRY_SUP_VPD_PAGE: 3564 /* 3565 * Request for suported Vital Product Data 3566 * pages - assuming only 2 page codes 3567 * supported. 3568 */ 3569 page_buf[0] = peripheral_device_type; 3570 page_buf[1] = INQUIRY_SUP_VPD_PAGE; 3571 page_buf[2] = 0; 3572 page_buf[3] = 2; /* page length */ 3573 page_buf[4] = INQUIRY_SUP_VPD_PAGE; 3574 page_buf[5] = INQUIRY_USN_PAGE; 3575 /* Copy no more than requested */ 3576 count = MIN(bp->b_bcount, 6); 3577 bcopy(page_buf, bp->b_un.b_addr, count); 3578 break; 3579 3580 case INQUIRY_USN_PAGE: 3581 /* 3582 * Request for Unit Serial Number page. 3583 * Set-up the page. 3584 */ 3585 page_buf[0] = peripheral_device_type; 3586 page_buf[1] = INQUIRY_USN_PAGE; 3587 page_buf[2] = 0; 3588 /* remaining page length */ 3589 page_buf[3] = SATA_ID_SERIAL_LEN; 3590 3591 /* 3592 * Copy serial number from Identify Device data 3593 * words into the inquiry page and swap bytes 3594 * when necessary. 3595 */ 3596 p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser); 3597 #ifdef _LITTLE_ENDIAN 3598 swab(p, &page_buf[4], SATA_ID_SERIAL_LEN); 3599 #else 3600 bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN); 3601 #endif 3602 /* 3603 * Least significant character of the serial 3604 * number shall appear as the last byte, 3605 * according to SBC-3 spec. 3606 * Count trailing spaces to determine the 3607 * necessary shift length. 3608 */ 3609 p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1]; 3610 for (j = 0; j < SATA_ID_SERIAL_LEN; j++) { 3611 if (*(p - j) != '\0' && 3612 *(p - j) != '\040') 3613 break; 3614 } 3615 3616 /* 3617 * Shift SN string right, so that the last 3618 * non-blank character would appear in last 3619 * byte of SN field in the page. 3620 * 'j' is the shift length. 3621 */ 3622 for (i = 0; 3623 i < (SATA_ID_SERIAL_LEN - j) && j != 0; 3624 i++, p--) 3625 *p = *(p - j); 3626 3627 /* 3628 * Add leading spaces - same number as the 3629 * shift size 3630 */ 3631 for (; j > 0; j--) 3632 page_buf[4 + j - 1] = '\040'; 3633 3634 count = MIN(bp->b_bcount, 3635 SATA_ID_SERIAL_LEN + 4); 3636 bcopy(page_buf, bp->b_un.b_addr, count); 3637 break; 3638 3639 case INQUIRY_DEV_IDENTIFICATION_PAGE: 3640 /* 3641 * We may want to implement this page, when 3642 * identifiers are common for SATA devices 3643 * But not now. 3644 */ 3645 /*FALLTHROUGH*/ 3646 3647 default: 3648 /* Request for unsupported VPD page */ 3649 *scsipkt->pkt_scbp = STATUS_CHECK; 3650 sense = sata_arq_sense(spx); 3651 sense->es_key = KEY_ILLEGAL_REQUEST; 3652 sense->es_add_code = 3653 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3654 goto done; 3655 } 3656 } 3657 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3658 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 3659 scsipkt->pkt_cdbp[4] - count : 0; 3660 } 3661 done: 3662 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3663 3664 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3665 "Scsi_pkt completion reason %x\n", 3666 scsipkt->pkt_reason); 3667 3668 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3669 scsipkt->pkt_comp != NULL) { 3670 /* scsi callback required */ 3671 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3672 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3673 TQ_SLEEP) == NULL) 3674 /* Scheduling the callback failed */ 3675 return (TRAN_BUSY); 3676 } 3677 return (TRAN_ACCEPT); 3678 } 3679 3680 /* 3681 * SATA translate command: Request Sense. 3682 * 3683 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3684 * At the moment this is an emulated command (ATA version for SATA hard disks). 3685 * May be translated into Check Power Mode command in the future. 3686 * 3687 * Note: There is a mismatch between already implemented Informational 3688 * Exception Mode Select page 0x1C and this function. 3689 * When MRIE bit is set in page 0x1C, Request Sense is supposed to return 3690 * NO SENSE and set additional sense code to the exception code - this is not 3691 * implemented here. 3692 */ 3693 static int 3694 sata_txlt_request_sense(sata_pkt_txlate_t *spx) 3695 { 3696 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3697 struct scsi_extended_sense sense; 3698 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3699 sata_drive_info_t *sdinfo; 3700 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 3701 int rval, reason, power_state = 0; 3702 3703 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3704 3705 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 3706 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3707 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3708 return (rval); 3709 } 3710 3711 scsipkt->pkt_reason = CMD_CMPLT; 3712 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3713 STATE_SENT_CMD | STATE_GOT_STATUS; 3714 *scsipkt->pkt_scbp = STATUS_GOOD; 3715 3716 /* 3717 * when CONTROL field's NACA bit == 1 3718 * return ILLEGAL_REQUEST 3719 */ 3720 if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) { 3721 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3722 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 3723 SD_SCSI_ASC_CMD_SEQUENCE_ERR)); 3724 } 3725 3726 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3727 &spx->txlt_sata_pkt->satapkt_device); 3728 ASSERT(sdinfo != NULL); 3729 3730 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH; 3731 3732 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE); 3733 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE; 3734 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 3735 if (sata_hba_start(spx, &rval) != 0) { 3736 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3737 return (rval); 3738 } else { 3739 if (scmd->satacmd_error_reg != 0) { 3740 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3741 return (sata_txlt_check_condition(spx, KEY_NO_SENSE, 3742 SD_SCSI_ASC_NO_ADD_SENSE)); 3743 } 3744 } 3745 3746 switch (scmd->satacmd_sec_count_lsb) { 3747 case SATA_PWRMODE_STANDBY: /* device in standby mode */ 3748 if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED) 3749 power_state = SATA_POWER_STOPPED; 3750 else { 3751 power_state = SATA_POWER_STANDBY; 3752 sdinfo->satadrv_power_level = SATA_POWER_STANDBY; 3753 } 3754 break; 3755 case SATA_PWRMODE_IDLE: /* device in idle mode */ 3756 power_state = SATA_POWER_IDLE; 3757 sdinfo->satadrv_power_level = SATA_POWER_IDLE; 3758 break; 3759 case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */ 3760 default: /* 0x40, 0x41 active mode */ 3761 if (sdinfo->satadrv_power_level == SATA_POWER_IDLE) 3762 power_state = SATA_POWER_IDLE; 3763 else { 3764 power_state = SATA_POWER_ACTIVE; 3765 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 3766 } 3767 break; 3768 } 3769 3770 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3771 3772 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 3773 /* 3774 * Because it is fully emulated command storing data 3775 * programatically in the specified buffer, release 3776 * preallocated DMA resources before storing data in the buffer, 3777 * so no unwanted DMA sync would take place. 3778 */ 3779 int count = MIN(bp->b_bcount, 3780 sizeof (struct scsi_extended_sense)); 3781 sata_scsi_dmafree(NULL, scsipkt); 3782 bzero(&sense, sizeof (struct scsi_extended_sense)); 3783 sense.es_valid = 0; /* Valid LBA */ 3784 sense.es_class = 7; /* Response code 0x70 - current err */ 3785 sense.es_key = KEY_NO_SENSE; 3786 sense.es_add_len = 6; /* Additional length */ 3787 /* Copy no more than requested */ 3788 bcopy(&sense, bp->b_un.b_addr, count); 3789 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3790 scsipkt->pkt_resid = 0; 3791 switch (power_state) { 3792 case SATA_POWER_IDLE: 3793 case SATA_POWER_STANDBY: 3794 sense.es_add_code = 3795 SD_SCSI_ASC_LOW_POWER_CONDITION_ON; 3796 break; 3797 case SATA_POWER_STOPPED: 3798 sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE; 3799 break; 3800 case SATA_POWER_ACTIVE: 3801 default: 3802 break; 3803 } 3804 } 3805 3806 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3807 "Scsi_pkt completion reason %x\n", 3808 scsipkt->pkt_reason); 3809 3810 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 3811 /* scsi callback required */ 3812 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3813 (task_func_t *)scsi_hba_pkt_comp, (void *) scsipkt, 3814 TQ_SLEEP) == NULL) 3815 /* Scheduling the callback failed */ 3816 return (TRAN_BUSY); 3817 return (TRAN_ACCEPT); 3818 } 3819 3820 /* 3821 * SATA translate command: Test Unit Ready 3822 * (ATA version for SATA hard disks). 3823 * It is translated into the Check Power Mode command. 3824 * 3825 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3826 */ 3827 static int 3828 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx) 3829 { 3830 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3831 struct scsi_extended_sense *sense; 3832 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 3833 sata_drive_info_t *sdinfo; 3834 int power_state; 3835 int rval, reason; 3836 3837 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3838 3839 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 3840 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3841 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3842 return (rval); 3843 } 3844 3845 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3846 &spx->txlt_sata_pkt->satapkt_device); 3847 ASSERT(sdinfo != NULL); 3848 3849 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH; 3850 3851 /* send CHECK POWER MODE command */ 3852 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE); 3853 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE; 3854 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 3855 if (sata_hba_start(spx, &rval) != 0) { 3856 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3857 return (rval); 3858 } else { 3859 if (scmd->satacmd_error_reg != 0) { 3860 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3861 return (sata_txlt_check_condition(spx, KEY_NOT_READY, 3862 SD_SCSI_ASC_LU_NOT_RESPONSE)); 3863 } 3864 } 3865 3866 power_state = scmd->satacmd_sec_count_lsb; 3867 3868 /* 3869 * return NOT READY when device in STOPPED mode 3870 */ 3871 if (power_state == SATA_PWRMODE_STANDBY && 3872 sdinfo->satadrv_power_level == SATA_POWER_STOPPED) { 3873 *scsipkt->pkt_scbp = STATUS_CHECK; 3874 sense = sata_arq_sense(spx); 3875 sense->es_key = KEY_NOT_READY; 3876 sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY; 3877 } else { 3878 /* 3879 * For other power mode, return GOOD status 3880 */ 3881 *scsipkt->pkt_scbp = STATUS_GOOD; 3882 } 3883 3884 scsipkt->pkt_reason = CMD_CMPLT; 3885 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3886 STATE_SENT_CMD | STATE_GOT_STATUS; 3887 3888 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3889 3890 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3891 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3892 3893 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 3894 /* scsi callback required */ 3895 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3896 (task_func_t *)scsi_hba_pkt_comp, (void *) scsipkt, 3897 TQ_SLEEP) == NULL) 3898 /* Scheduling the callback failed */ 3899 return (TRAN_BUSY); 3900 3901 return (TRAN_ACCEPT); 3902 } 3903 3904 /* 3905 * SATA translate command: Start Stop Unit 3906 * Translation depends on a command: 3907 * 3908 * Power condition bits will be supported 3909 * and the power level should be maintained by SATL, 3910 * When SATL received a command, it will check the 3911 * power level firstly, and return the status according 3912 * to SAT2 v2.6 and SAT-2 Standby Modifications 3913 * 3914 * SPC-4/SBC-3 SATL ATA power condition SATL SPC/SBC 3915 * ----------------------------------------------------------------------- 3916 * SSU_PC1 Active <==> ATA Active <==> SSU:start_bit =1 3917 * SSU_PC2 Idle <==> ATA Idle <==> N/A 3918 * SSU_PC3 Standby <==> ATA Standby <==> N/A 3919 * SSU_PC4 Stopped <==> ATA Standby <==> SSU:start_bit = 0 3920 * 3921 * Unload Media / NOT SUPPORTED YET 3922 * Load Media / NOT SUPPROTED YET 3923 * Immediate bit / NOT SUPPORTED YET (deferred error) 3924 * 3925 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 3926 * appropriate values in scsi_pkt fields. 3927 */ 3928 static int 3929 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx) 3930 { 3931 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3932 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 3933 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 3934 int cport = SATA_TXLT_CPORT(spx); 3935 int rval, reason; 3936 sata_drive_info_t *sdinfo; 3937 sata_id_t *sata_id; 3938 3939 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3940 "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1); 3941 3942 mutex_enter(&SATA_CPORT_MUTEX(shi, cport)); 3943 3944 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 3945 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3946 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3947 return (rval); 3948 } 3949 3950 if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) { 3951 /* IMMED bit - not supported */ 3952 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3953 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 3954 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 3955 } 3956 3957 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH; 3958 spx->txlt_sata_pkt->satapkt_comp = NULL; 3959 3960 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3961 &spx->txlt_sata_pkt->satapkt_device); 3962 ASSERT(sdinfo != NULL); 3963 sata_id = &sdinfo->satadrv_id; 3964 3965 switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) { 3966 case 0: 3967 if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) { 3968 /* Load/Unload Media - invalid request */ 3969 goto err_out; 3970 } 3971 if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) { 3972 /* Start Unit */ 3973 sata_build_read_verify_cmd(scmd, 1, 5); 3974 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 3975 /* Transfer command to HBA */ 3976 if (sata_hba_start(spx, &rval) != 0) { 3977 /* Pkt not accepted for execution */ 3978 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3979 return (rval); 3980 } else { 3981 if (scmd->satacmd_error_reg != 0) { 3982 goto err_out; 3983 } 3984 } 3985 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 3986 } else { 3987 /* Stop Unit */ 3988 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 3989 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 3990 if (sata_hba_start(spx, &rval) != 0) { 3991 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3992 return (rval); 3993 } else { 3994 if (scmd->satacmd_error_reg != 0) { 3995 goto err_out; 3996 } 3997 } 3998 /* ata standby immediate command */ 3999 sata_build_generic_cmd(scmd, SATAC_STANDBY_IM); 4000 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4001 if (sata_hba_start(spx, &rval) != 0) { 4002 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4003 return (rval); 4004 } else { 4005 if (scmd->satacmd_error_reg != 0) { 4006 goto err_out; 4007 } 4008 } 4009 sdinfo->satadrv_power_level = SATA_POWER_STOPPED; 4010 } 4011 break; 4012 case 0x1: 4013 sata_build_generic_cmd(scmd, SATAC_IDLE); 4014 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4015 if (sata_hba_start(spx, &rval) != 0) { 4016 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4017 return (rval); 4018 } else { 4019 if (scmd->satacmd_error_reg != 0) { 4020 goto err_out; 4021 } 4022 } 4023 sata_build_read_verify_cmd(scmd, 1, 5); 4024 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4025 /* Transfer command to HBA */ 4026 if (sata_hba_start(spx, &rval) != 0) { 4027 /* Pkt not accepted for execution */ 4028 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4029 return (rval); 4030 } else { 4031 if (scmd->satacmd_error_reg != 0) { 4032 goto err_out; 4033 } 4034 } 4035 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 4036 break; 4037 case 0x2: 4038 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 4039 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4040 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) { 4041 if (sata_hba_start(spx, &rval) != 0) { 4042 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4043 return (rval); 4044 } else { 4045 if (scmd->satacmd_error_reg != 0) { 4046 goto err_out; 4047 } 4048 } 4049 } 4050 sata_build_generic_cmd(scmd, SATAC_IDLE); 4051 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4052 if (sata_hba_start(spx, &rval) != 0) { 4053 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4054 return (rval); 4055 } else { 4056 if (scmd->satacmd_error_reg != 0) { 4057 goto err_out; 4058 } 4059 } 4060 if ((scsipkt->pkt_cdbp[3] & START_STOP_MODIFIER_MASK)) { 4061 /* 4062 * POWER CONDITION MODIFIER bit set 4063 * to 0x1 or larger it will be handled 4064 * on the same way as bit = 0x1 4065 */ 4066 if (!(sata_id->ai_cmdset84 & 4067 SATA_IDLE_UNLOAD_SUPPORTED)) { 4068 sdinfo->satadrv_power_level = SATA_POWER_IDLE; 4069 break; 4070 } 4071 sata_build_generic_cmd(scmd, SATAC_IDLE_IM); 4072 scmd->satacmd_features_reg = 0x44; 4073 scmd->satacmd_lba_low_lsb = 0x4c; 4074 scmd->satacmd_lba_mid_lsb = 0x4e; 4075 scmd->satacmd_lba_high_lsb = 0x55; 4076 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4077 if (sata_hba_start(spx, &rval) != 0) { 4078 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4079 return (rval); 4080 } else { 4081 if (scmd->satacmd_error_reg != 0) { 4082 goto err_out; 4083 } 4084 } 4085 } 4086 sdinfo->satadrv_power_level = SATA_POWER_IDLE; 4087 break; 4088 case 0x3: 4089 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 4090 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4091 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) { 4092 if (sata_hba_start(spx, &rval) != 0) { 4093 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4094 return (rval); 4095 } else { 4096 if (scmd->satacmd_error_reg != 0) { 4097 goto err_out; 4098 } 4099 } 4100 } 4101 sata_build_generic_cmd(scmd, SATAC_STANDBY); 4102 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4103 if (sata_hba_start(spx, &rval) != 0) { 4104 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4105 return (rval); 4106 } else { 4107 if (scmd->satacmd_error_reg != 0) { 4108 goto err_out; 4109 } 4110 } 4111 sdinfo->satadrv_power_level = SATA_POWER_STANDBY; 4112 break; 4113 case 0x7: 4114 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE); 4115 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE; 4116 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4117 if (sata_hba_start(spx, &rval) != 0) { 4118 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4119 return (rval); 4120 } else { 4121 if (scmd->satacmd_error_reg != 0) { 4122 goto err_out; 4123 } 4124 } 4125 switch (scmd->satacmd_sec_count_lsb) { 4126 case SATA_PWRMODE_STANDBY: 4127 sata_build_generic_cmd(scmd, SATAC_STANDBY); 4128 scmd->satacmd_sec_count_msb = sata_get_standby_timer( 4129 sdinfo->satadrv_standby_timer); 4130 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4131 if (sata_hba_start(spx, &rval) != 0) { 4132 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4133 return (rval); 4134 } else { 4135 if (scmd->satacmd_error_reg != 0) { 4136 goto err_out; 4137 } 4138 } 4139 break; 4140 case SATA_PWRMODE_IDLE: 4141 sata_build_generic_cmd(scmd, SATAC_IDLE); 4142 scmd->satacmd_sec_count_msb = sata_get_standby_timer( 4143 sdinfo->satadrv_standby_timer); 4144 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4145 if (sata_hba_start(spx, &rval) != 0) { 4146 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4147 return (rval); 4148 } else { 4149 if (scmd->satacmd_error_reg != 0) { 4150 goto err_out; 4151 } 4152 } 4153 break; 4154 case SATA_PWRMODE_ACTIVE_SPINDOWN: 4155 case SATA_PWRMODE_ACTIVE_SPINUP: 4156 case SATA_PWRMODE_ACTIVE: 4157 sata_build_generic_cmd(scmd, SATAC_IDLE); 4158 scmd->satacmd_sec_count_msb = sata_get_standby_timer( 4159 sdinfo->satadrv_standby_timer); 4160 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4161 if (sata_hba_start(spx, &rval) != 0) { 4162 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4163 return (rval); 4164 } else { 4165 if (scmd->satacmd_error_reg != 0) { 4166 goto err_out; 4167 } 4168 } 4169 sata_build_read_verify_cmd(scmd, 1, 5); 4170 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4171 if (sata_hba_start(spx, &rval) != 0) { 4172 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4173 return (rval); 4174 } else { 4175 if (scmd->satacmd_error_reg != 0) { 4176 goto err_out; 4177 } 4178 } 4179 break; 4180 default: 4181 goto err_out; 4182 } 4183 break; 4184 case 0xb: 4185 if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) == 4186 0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) { 4187 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4188 return (sata_txlt_check_condition(spx, 4189 KEY_ILLEGAL_REQUEST, 4190 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4191 } 4192 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 4193 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4194 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) { 4195 if (sata_hba_start(spx, &rval) != 0) { 4196 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4197 return (rval); 4198 } else { 4199 if (scmd->satacmd_error_reg != 0) { 4200 goto err_out; 4201 } 4202 } 4203 sata_build_generic_cmd(scmd, SATAC_STANDBY_IM); 4204 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4205 if (sata_hba_start(spx, &rval) != 0) { 4206 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4207 return (rval); 4208 } else { 4209 if (scmd->satacmd_error_reg != 0) { 4210 goto err_out; 4211 } 4212 } 4213 } 4214 bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4); 4215 break; 4216 default: 4217 err_out: 4218 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4219 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 4220 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4221 } 4222 4223 /* 4224 * Since it was a synchronous command, 4225 * a callback function will be called directly. 4226 */ 4227 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4228 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4229 "synchronous execution status %x\n", 4230 spx->txlt_sata_pkt->satapkt_reason); 4231 4232 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) { 4233 sata_set_arq_data(spx->txlt_sata_pkt); 4234 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4235 (task_func_t *)scsi_hba_pkt_comp, (void *) scsipkt, 4236 TQ_SLEEP) == 0) { 4237 return (TRAN_BUSY); 4238 } 4239 } 4240 else 4241 4242 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 4243 4244 return (TRAN_ACCEPT); 4245 4246 } 4247 4248 /* 4249 * SATA translate command: Read Capacity. 4250 * Emulated command for SATA disks. 4251 * Capacity is retrieved from cached Idenifty Device data. 4252 * Identify Device data shows effective disk capacity, not the native 4253 * capacity, which may be limitted by Set Max Address command. 4254 * This is ATA version for SATA hard disks. 4255 * 4256 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4257 */ 4258 static int 4259 sata_txlt_read_capacity(sata_pkt_txlate_t *spx) 4260 { 4261 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4262 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4263 sata_drive_info_t *sdinfo; 4264 uint64_t val; 4265 uchar_t *rbuf; 4266 int rval, reason; 4267 4268 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4269 "sata_txlt_read_capacity: ", NULL); 4270 4271 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4272 4273 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 4274 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4275 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4276 return (rval); 4277 } 4278 4279 scsipkt->pkt_reason = CMD_CMPLT; 4280 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4281 STATE_SENT_CMD | STATE_GOT_STATUS; 4282 *scsipkt->pkt_scbp = STATUS_GOOD; 4283 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4284 /* 4285 * Because it is fully emulated command storing data 4286 * programatically in the specified buffer, release 4287 * preallocated DMA resources before storing data in the buffer, 4288 * so no unwanted DMA sync would take place. 4289 */ 4290 sata_scsi_dmafree(NULL, scsipkt); 4291 4292 sdinfo = sata_get_device_info( 4293 spx->txlt_sata_hba_inst, 4294 &spx->txlt_sata_pkt->satapkt_device); 4295 /* Last logical block address */ 4296 val = sdinfo->satadrv_capacity - 1; 4297 rbuf = (uchar_t *)bp->b_un.b_addr; 4298 /* Need to swap endians to match scsi format */ 4299 rbuf[0] = (val >> 24) & 0xff; 4300 rbuf[1] = (val >> 16) & 0xff; 4301 rbuf[2] = (val >> 8) & 0xff; 4302 rbuf[3] = val & 0xff; 4303 /* block size - always 512 bytes, for now */ 4304 rbuf[4] = 0; 4305 rbuf[5] = 0; 4306 rbuf[6] = 0x02; 4307 rbuf[7] = 0; 4308 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4309 scsipkt->pkt_resid = 0; 4310 4311 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n", 4312 sdinfo->satadrv_capacity -1); 4313 } 4314 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4315 /* 4316 * If a callback was requested, do it now. 4317 */ 4318 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4319 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4320 4321 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4322 scsipkt->pkt_comp != NULL) 4323 /* scsi callback required */ 4324 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4325 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4326 TQ_SLEEP) == NULL) 4327 /* Scheduling the callback failed */ 4328 return (TRAN_BUSY); 4329 4330 return (TRAN_ACCEPT); 4331 } 4332 4333 /* 4334 * SATA translate command: Mode Sense. 4335 * Translated into appropriate SATA command or emulated. 4336 * Saved Values Page Control (03) are not supported. 4337 * 4338 * NOTE: only caching mode sense page is currently implemented. 4339 * 4340 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4341 */ 4342 4343 #define LLBAA 0x10 /* Long LBA Accepted */ 4344 4345 static int 4346 sata_txlt_mode_sense(sata_pkt_txlate_t *spx) 4347 { 4348 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4349 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4350 sata_drive_info_t *sdinfo; 4351 sata_id_t *sata_id; 4352 struct scsi_extended_sense *sense; 4353 int len, bdlen, count, alc_len; 4354 int pc; /* Page Control code */ 4355 uint8_t *buf; /* mode sense buffer */ 4356 int rval, reason; 4357 4358 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4359 "sata_txlt_mode_sense, pc %x page code 0x%02x\n", 4360 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 4361 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 4362 4363 buf = kmem_zalloc(1024, KM_SLEEP); 4364 4365 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4366 4367 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 4368 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4369 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4370 kmem_free(buf, 1024); 4371 return (rval); 4372 } 4373 4374 scsipkt->pkt_reason = CMD_CMPLT; 4375 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4376 STATE_SENT_CMD | STATE_GOT_STATUS; 4377 4378 pc = scsipkt->pkt_cdbp[2] >> 6; 4379 4380 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4381 /* 4382 * Because it is fully emulated command storing data 4383 * programatically in the specified buffer, release 4384 * preallocated DMA resources before storing data in the buffer, 4385 * so no unwanted DMA sync would take place. 4386 */ 4387 sata_scsi_dmafree(NULL, scsipkt); 4388 4389 len = 0; 4390 bdlen = 0; 4391 if (!(scsipkt->pkt_cdbp[1] & 8)) { 4392 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 && 4393 (scsipkt->pkt_cdbp[1] & LLBAA)) 4394 bdlen = 16; 4395 else 4396 bdlen = 8; 4397 } 4398 /* Build mode parameter header */ 4399 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 4400 /* 4-byte mode parameter header */ 4401 buf[len++] = 0; /* mode data length */ 4402 buf[len++] = 0; /* medium type */ 4403 buf[len++] = 0; /* dev-specific param */ 4404 buf[len++] = bdlen; /* Block Descriptor length */ 4405 } else { 4406 /* 8-byte mode parameter header */ 4407 buf[len++] = 0; /* mode data length */ 4408 buf[len++] = 0; 4409 buf[len++] = 0; /* medium type */ 4410 buf[len++] = 0; /* dev-specific param */ 4411 if (bdlen == 16) 4412 buf[len++] = 1; /* long lba descriptor */ 4413 else 4414 buf[len++] = 0; 4415 buf[len++] = 0; 4416 buf[len++] = 0; /* Block Descriptor length */ 4417 buf[len++] = bdlen; 4418 } 4419 4420 sdinfo = sata_get_device_info( 4421 spx->txlt_sata_hba_inst, 4422 &spx->txlt_sata_pkt->satapkt_device); 4423 4424 /* Build block descriptor only if not disabled (DBD) */ 4425 if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) { 4426 /* Block descriptor - direct-access device format */ 4427 if (bdlen == 8) { 4428 /* build regular block descriptor */ 4429 buf[len++] = 4430 (sdinfo->satadrv_capacity >> 24) & 0xff; 4431 buf[len++] = 4432 (sdinfo->satadrv_capacity >> 16) & 0xff; 4433 buf[len++] = 4434 (sdinfo->satadrv_capacity >> 8) & 0xff; 4435 buf[len++] = sdinfo->satadrv_capacity & 0xff; 4436 buf[len++] = 0; /* density code */ 4437 buf[len++] = 0; 4438 if (sdinfo->satadrv_type == 4439 SATA_DTYPE_ATADISK) 4440 buf[len++] = 2; 4441 else 4442 /* ATAPI */ 4443 buf[len++] = 8; 4444 buf[len++] = 0; 4445 } else if (bdlen == 16) { 4446 /* Long LBA Accepted */ 4447 /* build long lba block descriptor */ 4448 #ifndef __lock_lint 4449 buf[len++] = 4450 (sdinfo->satadrv_capacity >> 56) & 0xff; 4451 buf[len++] = 4452 (sdinfo->satadrv_capacity >> 48) & 0xff; 4453 buf[len++] = 4454 (sdinfo->satadrv_capacity >> 40) & 0xff; 4455 buf[len++] = 4456 (sdinfo->satadrv_capacity >> 32) & 0xff; 4457 #endif 4458 buf[len++] = 4459 (sdinfo->satadrv_capacity >> 24) & 0xff; 4460 buf[len++] = 4461 (sdinfo->satadrv_capacity >> 16) & 0xff; 4462 buf[len++] = 4463 (sdinfo->satadrv_capacity >> 8) & 0xff; 4464 buf[len++] = sdinfo->satadrv_capacity & 0xff; 4465 buf[len++] = 0; 4466 buf[len++] = 0; /* density code */ 4467 buf[len++] = 0; 4468 buf[len++] = 0; 4469 if (sdinfo->satadrv_type == 4470 SATA_DTYPE_ATADISK) 4471 buf[len++] = 2; 4472 else 4473 /* ATAPI */ 4474 buf[len++] = 8; 4475 buf[len++] = 0; 4476 } 4477 } 4478 4479 sata_id = &sdinfo->satadrv_id; 4480 4481 /* 4482 * Add requested pages. 4483 * Page 3 and 4 are obsolete and we are not supporting them. 4484 * We deal now with: 4485 * caching (read/write cache control). 4486 * We should eventually deal with following mode pages: 4487 * error recovery (0x01), 4488 * power condition (0x1a), 4489 * exception control page (enables SMART) (0x1c), 4490 * enclosure management (ses), 4491 * protocol-specific port mode (port control). 4492 */ 4493 switch (scsipkt->pkt_cdbp[2] & 0x3f) { 4494 case MODEPAGE_RW_ERRRECOV: 4495 /* DAD_MODE_ERR_RECOV */ 4496 /* R/W recovery */ 4497 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 4498 break; 4499 case MODEPAGE_CACHING: 4500 /* DAD_MODE_CACHE */ 4501 /* Reject not supported request for saved parameters */ 4502 if (pc == 3) { 4503 *scsipkt->pkt_scbp = STATUS_CHECK; 4504 sense = sata_arq_sense(spx); 4505 sense->es_key = KEY_ILLEGAL_REQUEST; 4506 sense->es_add_code = 4507 SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED; 4508 goto done; 4509 } 4510 4511 /* caching */ 4512 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 4513 break; 4514 case MODEPAGE_INFO_EXCPT: 4515 /* exception cntrl */ 4516 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 4517 len += sata_build_msense_page_1c(sdinfo, pc, 4518 buf+len); 4519 } 4520 else 4521 goto err; 4522 break; 4523 case MODEPAGE_POWER_COND: 4524 /* DAD_MODE_POWER_COND */ 4525 /* power condition */ 4526 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 4527 break; 4528 4529 case MODEPAGE_ACOUSTIC_MANAG: 4530 /* acoustic management */ 4531 len += sata_build_msense_page_30(sdinfo, pc, buf+len); 4532 break; 4533 case MODEPAGE_ALLPAGES: 4534 /* all pages */ 4535 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 4536 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 4537 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 4538 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 4539 len += sata_build_msense_page_1c(sdinfo, pc, 4540 buf+len); 4541 } 4542 len += sata_build_msense_page_30(sdinfo, pc, buf+len); 4543 break; 4544 default: 4545 err: 4546 /* Invalid request */ 4547 *scsipkt->pkt_scbp = STATUS_CHECK; 4548 sense = sata_arq_sense(spx); 4549 sense->es_key = KEY_ILLEGAL_REQUEST; 4550 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4551 goto done; 4552 } 4553 4554 /* fix total mode data length */ 4555 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 4556 /* 4-byte mode parameter header */ 4557 buf[0] = len - 1; /* mode data length */ 4558 } else { 4559 buf[0] = (len -2) >> 8; 4560 buf[1] = (len -2) & 0xff; 4561 } 4562 4563 4564 /* Check allocation length */ 4565 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 4566 alc_len = scsipkt->pkt_cdbp[4]; 4567 } else { 4568 alc_len = scsipkt->pkt_cdbp[7]; 4569 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 4570 } 4571 /* 4572 * We do not check for possible parameters truncation 4573 * (alc_len < len) assuming that the target driver works 4574 * correctly. Just avoiding overrun. 4575 * Copy no more than requested and possible, buffer-wise. 4576 */ 4577 count = MIN(alc_len, len); 4578 count = MIN(bp->b_bcount, count); 4579 bcopy(buf, bp->b_un.b_addr, count); 4580 4581 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4582 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 4583 } 4584 *scsipkt->pkt_scbp = STATUS_GOOD; 4585 done: 4586 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4587 (void) kmem_free(buf, 1024); 4588 4589 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4590 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4591 4592 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4593 scsipkt->pkt_comp != NULL) 4594 /* scsi callback required */ 4595 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4596 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4597 TQ_SLEEP) == NULL) 4598 /* Scheduling the callback failed */ 4599 return (TRAN_BUSY); 4600 4601 return (TRAN_ACCEPT); 4602 } 4603 4604 4605 /* 4606 * SATA translate command: Mode Select. 4607 * Translated into appropriate SATA command or emulated. 4608 * Saving parameters is not supported. 4609 * Changing device capacity is not supported (although theoretically 4610 * possible by executing SET FEATURES/SET MAX ADDRESS) 4611 * 4612 * Assumption is that the target driver is working correctly. 4613 * 4614 * More than one SATA command may be executed to perform operations specified 4615 * by mode select pages. The first error terminates further execution. 4616 * Operations performed successully are not backed-up in such case. 4617 * 4618 * NOTE: Implemented pages: 4619 * - caching page 4620 * - informational exception page 4621 * - acoustic management page 4622 * - power condition page 4623 * Caching setup is remembered so it could be re-stored in case of 4624 * an unexpected device reset. 4625 * 4626 * Returns TRAN_XXXX. 4627 * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields. 4628 */ 4629 4630 static int 4631 sata_txlt_mode_select(sata_pkt_txlate_t *spx) 4632 { 4633 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4634 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4635 struct scsi_extended_sense *sense; 4636 int len, pagelen, count, pllen; 4637 uint8_t *buf; /* mode select buffer */ 4638 int rval, stat, reason; 4639 uint_t nointr_flag; 4640 int dmod = 0; 4641 4642 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4643 "sata_txlt_mode_select, pc %x page code 0x%02x\n", 4644 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 4645 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 4646 4647 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4648 4649 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 4650 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4651 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4652 return (rval); 4653 } 4654 4655 rval = TRAN_ACCEPT; 4656 4657 scsipkt->pkt_reason = CMD_CMPLT; 4658 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4659 STATE_SENT_CMD | STATE_GOT_STATUS; 4660 nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR; 4661 4662 /* Reject not supported request */ 4663 if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */ 4664 *scsipkt->pkt_scbp = STATUS_CHECK; 4665 sense = sata_arq_sense(spx); 4666 sense->es_key = KEY_ILLEGAL_REQUEST; 4667 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4668 goto done; 4669 } 4670 4671 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 4672 pllen = scsipkt->pkt_cdbp[4]; 4673 } else { 4674 pllen = scsipkt->pkt_cdbp[7]; 4675 pllen = (pllen << 8) | scsipkt->pkt_cdbp[7]; 4676 } 4677 4678 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 4679 4680 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) { 4681 buf = (uint8_t *)bp->b_un.b_addr; 4682 count = MIN(bp->b_bcount, pllen); 4683 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4684 scsipkt->pkt_resid = 0; 4685 pllen = count; 4686 4687 /* 4688 * Check the header to skip the block descriptor(s) - we 4689 * do not support setting device capacity. 4690 * Existing macros do not recognize long LBA dscriptor, 4691 * hence manual calculation. 4692 */ 4693 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 4694 /* 6-bytes CMD, 4 bytes header */ 4695 if (count <= 4) 4696 goto done; /* header only */ 4697 len = buf[3] + 4; 4698 } else { 4699 /* 10-bytes CMD, 8 bytes header */ 4700 if (count <= 8) 4701 goto done; /* header only */ 4702 len = buf[6]; 4703 len = (len << 8) + buf[7] + 8; 4704 } 4705 if (len >= count) 4706 goto done; /* header + descriptor(s) only */ 4707 4708 pllen -= len; /* remaining data length */ 4709 4710 /* 4711 * We may be executing SATA command and want to execute it 4712 * in SYNCH mode, regardless of scsi_pkt setting. 4713 * Save scsi_pkt setting and indicate SYNCH mode 4714 */ 4715 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4716 scsipkt->pkt_comp != NULL) { 4717 scsipkt->pkt_flags |= FLAG_NOINTR; 4718 } 4719 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH; 4720 4721 /* 4722 * len is now the offset to a first mode select page 4723 * Process all pages 4724 */ 4725 while (pllen > 0) { 4726 switch ((int)buf[len]) { 4727 case MODEPAGE_CACHING: 4728 /* No support for SP (saving) */ 4729 if (scsipkt->pkt_cdbp[1] & 0x01) { 4730 *scsipkt->pkt_scbp = STATUS_CHECK; 4731 sense = sata_arq_sense(spx); 4732 sense->es_key = KEY_ILLEGAL_REQUEST; 4733 sense->es_add_code = 4734 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4735 goto done; 4736 } 4737 stat = sata_mode_select_page_8(spx, 4738 (struct mode_cache_scsi3 *)&buf[len], 4739 pllen, &pagelen, &rval, &dmod); 4740 /* 4741 * The pagelen value indicates the number of 4742 * parameter bytes already processed. 4743 * The rval is the return value from 4744 * sata_tran_start(). 4745 * The stat indicates the overall status of 4746 * the operation(s). 4747 */ 4748 if (stat != SATA_SUCCESS) 4749 /* 4750 * Page processing did not succeed - 4751 * all error info is already set-up, 4752 * just return 4753 */ 4754 pllen = 0; /* this breaks the loop */ 4755 else { 4756 len += pagelen; 4757 pllen -= pagelen; 4758 } 4759 break; 4760 4761 case MODEPAGE_INFO_EXCPT: 4762 stat = sata_mode_select_page_1c(spx, 4763 (struct mode_info_excpt_page *)&buf[len], 4764 pllen, &pagelen, &rval, &dmod); 4765 /* 4766 * The pagelen value indicates the number of 4767 * parameter bytes already processed. 4768 * The rval is the return value from 4769 * sata_tran_start(). 4770 * The stat indicates the overall status of 4771 * the operation(s). 4772 */ 4773 if (stat != SATA_SUCCESS) 4774 /* 4775 * Page processing did not succeed - 4776 * all error info is already set-up, 4777 * just return 4778 */ 4779 pllen = 0; /* this breaks the loop */ 4780 else { 4781 len += pagelen; 4782 pllen -= pagelen; 4783 } 4784 break; 4785 4786 case MODEPAGE_ACOUSTIC_MANAG: 4787 stat = sata_mode_select_page_30(spx, 4788 (struct mode_acoustic_management *) 4789 &buf[len], pllen, &pagelen, &rval, &dmod); 4790 /* 4791 * The pagelen value indicates the number of 4792 * parameter bytes already processed. 4793 * The rval is the return value from 4794 * sata_tran_start(). 4795 * The stat indicates the overall status of 4796 * the operation(s). 4797 */ 4798 if (stat != SATA_SUCCESS) 4799 /* 4800 * Page processing did not succeed - 4801 * all error info is already set-up, 4802 * just return 4803 */ 4804 pllen = 0; /* this breaks the loop */ 4805 else { 4806 len += pagelen; 4807 pllen -= pagelen; 4808 } 4809 4810 break; 4811 case MODEPAGE_POWER_COND: 4812 stat = sata_mode_select_page_1a(spx, 4813 (struct mode_info_power_cond *)&buf[len], 4814 pllen, &pagelen, &rval, &dmod); 4815 /* 4816 * The pagelen value indicates the number of 4817 * parameter bytes already processed. 4818 * The rval is the return value from 4819 * sata_tran_start(). 4820 * The stat indicates the overall status of 4821 * the operation(s). 4822 */ 4823 if (stat != SATA_SUCCESS) 4824 /* 4825 * Page processing did not succeed - 4826 * all error info is already set-up, 4827 * just return 4828 */ 4829 pllen = 0; /* this breaks the loop */ 4830 else { 4831 len += pagelen; 4832 pllen -= pagelen; 4833 } 4834 break; 4835 default: 4836 *scsipkt->pkt_scbp = STATUS_CHECK; 4837 sense = sata_arq_sense(spx); 4838 sense->es_key = KEY_ILLEGAL_REQUEST; 4839 sense->es_add_code = 4840 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 4841 goto done; 4842 } 4843 } 4844 } 4845 done: 4846 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4847 /* 4848 * If device parameters were modified, fetch and store the new 4849 * Identify Device data. Since port mutex could have been released 4850 * for accessing HBA driver, we need to re-check device existence. 4851 */ 4852 if (dmod != 0) { 4853 sata_drive_info_t new_sdinfo, *sdinfo; 4854 int rv = 0; 4855 4856 /* 4857 * Following statement has to be changed if this function is 4858 * used for devices other than SATA hard disks. 4859 */ 4860 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK; 4861 4862 new_sdinfo.satadrv_addr = 4863 spx->txlt_sata_pkt->satapkt_device.satadev_addr; 4864 rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst, 4865 &new_sdinfo); 4866 4867 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4868 /* 4869 * Since port mutex could have been released when 4870 * accessing HBA driver, we need to re-check that the 4871 * framework still holds the device info structure. 4872 */ 4873 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4874 &spx->txlt_sata_pkt->satapkt_device); 4875 if (sdinfo != NULL) { 4876 /* 4877 * Device still has info structure in the 4878 * sata framework. Copy newly fetched info 4879 */ 4880 if (rv == 0) { 4881 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 4882 sata_save_drive_settings(sdinfo); 4883 } else { 4884 /* 4885 * Could not fetch new data - invalidate 4886 * sata_drive_info. That makes device 4887 * unusable. 4888 */ 4889 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 4890 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 4891 } 4892 } 4893 if (rv != 0 || sdinfo == NULL) { 4894 /* 4895 * This changes the overall mode select completion 4896 * reason to a failed one !!!!! 4897 */ 4898 *scsipkt->pkt_scbp = STATUS_CHECK; 4899 sense = sata_arq_sense(spx); 4900 scsipkt->pkt_reason = CMD_INCOMPLETE; 4901 rval = TRAN_ACCEPT; 4902 } 4903 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4904 } 4905 /* Restore the scsi pkt flags */ 4906 scsipkt->pkt_flags &= ~FLAG_NOINTR; 4907 scsipkt->pkt_flags |= nointr_flag; 4908 4909 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4910 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4911 4912 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4913 scsipkt->pkt_comp != NULL) 4914 /* scsi callback required */ 4915 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4916 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4917 TQ_SLEEP) == NULL) 4918 /* Scheduling the callback failed */ 4919 return (TRAN_BUSY); 4920 4921 return (rval); 4922 } 4923 4924 /* 4925 * Translate command: ATA Pass Through 4926 * Incomplete implementation. Only supports No-Data, PIO Data-In, and 4927 * PIO Data-Out protocols. Also supports CK_COND bit. 4928 * 4929 * Mapping of the incoming CDB bytes to the outgoing satacmd bytes is 4930 * described in Table 111 of SAT-2 (Draft 9). 4931 */ 4932 static int 4933 sata_txlt_ata_pass_thru(sata_pkt_txlate_t *spx) 4934 { 4935 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4936 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4937 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4938 int extend; 4939 uint64_t lba; 4940 uint16_t feature, sec_count; 4941 int t_len, synch; 4942 int rval, reason; 4943 4944 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4945 4946 rval = sata_txlt_generic_pkt_info(spx, &reason, 1); 4947 if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4948 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4949 return (rval); 4950 } 4951 4952 /* T_DIR bit */ 4953 if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_T_DIR) 4954 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 4955 else 4956 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 4957 4958 /* MULTIPLE_COUNT field. If non-zero, invalid command (for now). */ 4959 if (((scsipkt->pkt_cdbp[1] >> 5) & 0x7) != 0) { 4960 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4961 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 4962 } 4963 4964 /* OFFLINE field. If non-zero, invalid command (for now). */ 4965 if (((scsipkt->pkt_cdbp[2] >> 6) & 0x3) != 0) { 4966 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4967 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 4968 } 4969 4970 /* PROTOCOL field */ 4971 switch ((scsipkt->pkt_cdbp[1] >> 1) & 0xf) { 4972 case SATL_APT_P_HW_RESET: 4973 case SATL_APT_P_SRST: 4974 case SATL_APT_P_DMA: 4975 case SATL_APT_P_DMA_QUEUED: 4976 case SATL_APT_P_DEV_DIAG: 4977 case SATL_APT_P_DEV_RESET: 4978 case SATL_APT_P_UDMA_IN: 4979 case SATL_APT_P_UDMA_OUT: 4980 case SATL_APT_P_FPDMA: 4981 case SATL_APT_P_RET_RESP: 4982 /* Not yet implemented */ 4983 default: 4984 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4985 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 4986 4987 case SATL_APT_P_NON_DATA: 4988 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 4989 break; 4990 4991 case SATL_APT_P_PIO_DATA_IN: 4992 /* If PROTOCOL disagrees with T_DIR, invalid command */ 4993 if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_WRITE) { 4994 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4995 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 4996 } 4997 4998 /* if there is a buffer, release its DMA resources */ 4999 if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) { 5000 sata_scsi_dmafree(NULL, scsipkt); 5001 } else { 5002 /* if there is no buffer, how do you PIO in? */ 5003 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5004 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5005 } 5006 5007 break; 5008 5009 case SATL_APT_P_PIO_DATA_OUT: 5010 /* If PROTOCOL disagrees with T_DIR, invalid command */ 5011 if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_READ) { 5012 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5013 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5014 } 5015 5016 /* if there is a buffer, release its DMA resources */ 5017 if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) { 5018 sata_scsi_dmafree(NULL, scsipkt); 5019 } else { 5020 /* if there is no buffer, how do you PIO out? */ 5021 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5022 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5023 } 5024 5025 break; 5026 } 5027 5028 /* Parse the ATA cmd fields, transfer some straight to the satacmd */ 5029 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 5030 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12: 5031 feature = scsipkt->pkt_cdbp[3]; 5032 5033 sec_count = scsipkt->pkt_cdbp[4]; 5034 5035 lba = scsipkt->pkt_cdbp[8] & 0xf; 5036 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 5037 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 5038 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5039 5040 scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] & 0xf0; 5041 scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[9]; 5042 5043 break; 5044 5045 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16: 5046 if (scsipkt->pkt_cdbp[1] & SATL_APT_BM_EXTEND) { 5047 extend = 1; 5048 5049 feature = scsipkt->pkt_cdbp[3]; 5050 feature = (feature << 8) | scsipkt->pkt_cdbp[4]; 5051 5052 sec_count = scsipkt->pkt_cdbp[5]; 5053 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[6]; 5054 5055 lba = scsipkt->pkt_cdbp[11]; 5056 lba = (lba << 8) | scsipkt->pkt_cdbp[12]; 5057 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 5058 lba = (lba << 8) | scsipkt->pkt_cdbp[10]; 5059 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 5060 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 5061 5062 scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13]; 5063 scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14]; 5064 } else { 5065 feature = scsipkt->pkt_cdbp[3]; 5066 5067 sec_count = scsipkt->pkt_cdbp[5]; 5068 5069 lba = scsipkt->pkt_cdbp[13] & 0xf; 5070 lba = (lba << 8) | scsipkt->pkt_cdbp[12]; 5071 lba = (lba << 8) | scsipkt->pkt_cdbp[10]; 5072 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 5073 5074 scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] & 5075 0xf0; 5076 scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14]; 5077 } 5078 5079 break; 5080 } 5081 5082 /* CK_COND bit */ 5083 if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) { 5084 if (extend) { 5085 scmd->satacmd_flags.sata_copy_out_sec_count_msb = 1; 5086 scmd->satacmd_flags.sata_copy_out_lba_low_msb = 1; 5087 scmd->satacmd_flags.sata_copy_out_lba_mid_msb = 1; 5088 scmd->satacmd_flags.sata_copy_out_lba_high_msb = 1; 5089 } 5090 5091 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1; 5092 scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1; 5093 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1; 5094 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1; 5095 scmd->satacmd_flags.sata_copy_out_device_reg = 1; 5096 scmd->satacmd_flags.sata_copy_out_error_reg = 1; 5097 } 5098 5099 /* Transfer remaining parsed ATA cmd values to the satacmd */ 5100 if (extend) { 5101 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 5102 5103 scmd->satacmd_features_reg_ext = (feature >> 8) & 0xff; 5104 scmd->satacmd_sec_count_msb = (sec_count >> 8) & 0xff; 5105 scmd->satacmd_lba_low_msb = (lba >> 8) & 0xff; 5106 scmd->satacmd_lba_mid_msb = (lba >> 8) & 0xff; 5107 scmd->satacmd_lba_high_msb = lba >> 40; 5108 } else { 5109 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 5110 5111 scmd->satacmd_features_reg_ext = 0; 5112 scmd->satacmd_sec_count_msb = 0; 5113 scmd->satacmd_lba_low_msb = 0; 5114 scmd->satacmd_lba_mid_msb = 0; 5115 scmd->satacmd_lba_high_msb = 0; 5116 } 5117 5118 scmd->satacmd_features_reg = feature & 0xff; 5119 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 5120 scmd->satacmd_lba_low_lsb = lba & 0xff; 5121 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 5122 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 5123 5124 /* Determine transfer length */ 5125 switch (scsipkt->pkt_cdbp[2] & 0x3) { /* T_LENGTH field */ 5126 case 1: 5127 t_len = feature; 5128 break; 5129 case 2: 5130 t_len = sec_count; 5131 break; 5132 default: 5133 t_len = 0; 5134 break; 5135 } 5136 5137 /* Adjust transfer length for the Byte Block bit */ 5138 if ((scsipkt->pkt_cdbp[2] >> 2) & 1) 5139 t_len *= SATA_DISK_SECTOR_SIZE; 5140 5141 /* Start processing command */ 5142 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5143 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_apt_completion; 5144 synch = FALSE; 5145 } else { 5146 synch = TRUE; 5147 } 5148 5149 if (sata_hba_start(spx, &rval) != 0) { 5150 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5151 return (rval); 5152 } 5153 5154 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5155 5156 if (synch) { 5157 sata_txlt_apt_completion(spx->txlt_sata_pkt); 5158 } 5159 5160 return (TRAN_ACCEPT); 5161 } 5162 5163 /* 5164 * Translate command: Log Sense 5165 */ 5166 static int 5167 sata_txlt_log_sense(sata_pkt_txlate_t *spx) 5168 { 5169 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5170 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5171 sata_drive_info_t *sdinfo; 5172 struct scsi_extended_sense *sense; 5173 int len, count, alc_len; 5174 int pc; /* Page Control code */ 5175 int page_code; /* Page code */ 5176 uint8_t *buf; /* log sense buffer */ 5177 int rval, reason; 5178 #define MAX_LOG_SENSE_PAGE_SIZE 512 5179 5180 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5181 "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n", 5182 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 5183 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 5184 5185 buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP); 5186 5187 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5188 5189 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 5190 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5191 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5192 kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 5193 return (rval); 5194 } 5195 5196 scsipkt->pkt_reason = CMD_CMPLT; 5197 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5198 STATE_SENT_CMD | STATE_GOT_STATUS; 5199 5200 pc = scsipkt->pkt_cdbp[2] >> 6; 5201 page_code = scsipkt->pkt_cdbp[2] & 0x3f; 5202 5203 /* Reject not supported request for all but cumulative values */ 5204 switch (pc) { 5205 case PC_CUMULATIVE_VALUES: 5206 break; 5207 default: 5208 *scsipkt->pkt_scbp = STATUS_CHECK; 5209 sense = sata_arq_sense(spx); 5210 sense->es_key = KEY_ILLEGAL_REQUEST; 5211 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5212 goto done; 5213 } 5214 5215 switch (page_code) { 5216 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 5217 case PAGE_CODE_SELF_TEST_RESULTS: 5218 case PAGE_CODE_INFORMATION_EXCEPTIONS: 5219 case PAGE_CODE_SMART_READ_DATA: 5220 case PAGE_CODE_START_STOP_CYCLE_COUNTER: 5221 break; 5222 default: 5223 *scsipkt->pkt_scbp = STATUS_CHECK; 5224 sense = sata_arq_sense(spx); 5225 sense->es_key = KEY_ILLEGAL_REQUEST; 5226 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5227 goto done; 5228 } 5229 5230 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 5231 /* 5232 * Because log sense uses local buffers for data retrieval from 5233 * the devices and sets the data programatically in the 5234 * original specified buffer, release preallocated DMA 5235 * resources before storing data in the original buffer, 5236 * so no unwanted DMA sync would take place. 5237 */ 5238 sata_id_t *sata_id; 5239 5240 sata_scsi_dmafree(NULL, scsipkt); 5241 5242 len = 0; 5243 5244 /* Build log parameter header */ 5245 buf[len++] = page_code; /* page code as in the CDB */ 5246 buf[len++] = 0; /* reserved */ 5247 buf[len++] = 0; /* Zero out page length for now (MSB) */ 5248 buf[len++] = 0; /* (LSB) */ 5249 5250 sdinfo = sata_get_device_info( 5251 spx->txlt_sata_hba_inst, 5252 &spx->txlt_sata_pkt->satapkt_device); 5253 5254 /* 5255 * Add requested pages. 5256 */ 5257 switch (page_code) { 5258 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 5259 len = sata_build_lsense_page_0(sdinfo, buf + len); 5260 break; 5261 case PAGE_CODE_SELF_TEST_RESULTS: 5262 sata_id = &sdinfo->satadrv_id; 5263 if ((! (sata_id->ai_cmdset84 & 5264 SATA_SMART_SELF_TEST_SUPPORTED)) || 5265 (! (sata_id->ai_features87 & 5266 SATA_SMART_SELF_TEST_SUPPORTED))) { 5267 *scsipkt->pkt_scbp = STATUS_CHECK; 5268 sense = sata_arq_sense(spx); 5269 sense->es_key = KEY_ILLEGAL_REQUEST; 5270 sense->es_add_code = 5271 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5272 5273 goto done; 5274 } 5275 len = sata_build_lsense_page_10(sdinfo, buf + len, 5276 spx->txlt_sata_hba_inst); 5277 break; 5278 case PAGE_CODE_INFORMATION_EXCEPTIONS: 5279 sata_id = &sdinfo->satadrv_id; 5280 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 5281 *scsipkt->pkt_scbp = STATUS_CHECK; 5282 sense = sata_arq_sense(spx); 5283 sense->es_key = KEY_ILLEGAL_REQUEST; 5284 sense->es_add_code = 5285 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5286 5287 goto done; 5288 } 5289 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 5290 *scsipkt->pkt_scbp = STATUS_CHECK; 5291 sense = sata_arq_sense(spx); 5292 sense->es_key = KEY_ABORTED_COMMAND; 5293 sense->es_add_code = 5294 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 5295 sense->es_qual_code = 5296 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 5297 5298 goto done; 5299 } 5300 5301 len = sata_build_lsense_page_2f(sdinfo, buf + len, 5302 spx->txlt_sata_hba_inst); 5303 break; 5304 case PAGE_CODE_SMART_READ_DATA: 5305 sata_id = &sdinfo->satadrv_id; 5306 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 5307 *scsipkt->pkt_scbp = STATUS_CHECK; 5308 sense = sata_arq_sense(spx); 5309 sense->es_key = KEY_ILLEGAL_REQUEST; 5310 sense->es_add_code = 5311 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5312 5313 goto done; 5314 } 5315 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 5316 *scsipkt->pkt_scbp = STATUS_CHECK; 5317 sense = sata_arq_sense(spx); 5318 sense->es_key = KEY_ABORTED_COMMAND; 5319 sense->es_add_code = 5320 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 5321 sense->es_qual_code = 5322 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 5323 5324 goto done; 5325 } 5326 5327 /* This page doesn't include a page header */ 5328 len = sata_build_lsense_page_30(sdinfo, buf, 5329 spx->txlt_sata_hba_inst); 5330 goto no_header; 5331 case PAGE_CODE_START_STOP_CYCLE_COUNTER: 5332 sata_id = &sdinfo->satadrv_id; 5333 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 5334 *scsipkt->pkt_scbp = STATUS_CHECK; 5335 sense = sata_arq_sense(spx); 5336 sense->es_key = KEY_ILLEGAL_REQUEST; 5337 sense->es_add_code = 5338 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5339 5340 goto done; 5341 } 5342 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 5343 *scsipkt->pkt_scbp = STATUS_CHECK; 5344 sense = sata_arq_sense(spx); 5345 sense->es_key = KEY_ABORTED_COMMAND; 5346 sense->es_add_code = 5347 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 5348 sense->es_qual_code = 5349 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 5350 5351 goto done; 5352 } 5353 len = sata_build_lsense_page_0e(sdinfo, buf, spx); 5354 goto no_header; 5355 default: 5356 /* Invalid request */ 5357 *scsipkt->pkt_scbp = STATUS_CHECK; 5358 sense = sata_arq_sense(spx); 5359 sense->es_key = KEY_ILLEGAL_REQUEST; 5360 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5361 goto done; 5362 } 5363 5364 /* set parameter log sense data length */ 5365 buf[2] = len >> 8; /* log sense length (MSB) */ 5366 buf[3] = len & 0xff; /* log sense length (LSB) */ 5367 5368 len += SCSI_LOG_PAGE_HDR_LEN; 5369 ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE); 5370 5371 no_header: 5372 /* Check allocation length */ 5373 alc_len = scsipkt->pkt_cdbp[7]; 5374 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 5375 5376 /* 5377 * We do not check for possible parameters truncation 5378 * (alc_len < len) assuming that the target driver works 5379 * correctly. Just avoiding overrun. 5380 * Copy no more than requested and possible, buffer-wise. 5381 */ 5382 count = MIN(alc_len, len); 5383 count = MIN(bp->b_bcount, count); 5384 bcopy(buf, bp->b_un.b_addr, count); 5385 5386 scsipkt->pkt_state |= STATE_XFERRED_DATA; 5387 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 5388 } 5389 *scsipkt->pkt_scbp = STATUS_GOOD; 5390 done: 5391 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5392 (void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 5393 5394 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5395 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5396 5397 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5398 scsipkt->pkt_comp != NULL) 5399 /* scsi callback required */ 5400 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5401 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 5402 TQ_SLEEP) == NULL) 5403 /* Scheduling the callback failed */ 5404 return (TRAN_BUSY); 5405 5406 return (TRAN_ACCEPT); 5407 } 5408 5409 /* 5410 * Translate command: Log Select 5411 * Not implemented at this time - returns invalid command response. 5412 */ 5413 static int 5414 sata_txlt_log_select(sata_pkt_txlate_t *spx) 5415 { 5416 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5417 "sata_txlt_log_select\n", NULL); 5418 5419 return (sata_txlt_invalid_command(spx)); 5420 } 5421 5422 5423 /* 5424 * Translate command: Read (various types). 5425 * Translated into appropriate type of ATA READ command 5426 * for SATA hard disks. 5427 * Both the device capabilities and requested operation mode are 5428 * considered. 5429 * 5430 * Following scsi cdb fields are ignored: 5431 * rdprotect, dpo, fua, fua_nv, group_number. 5432 * 5433 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 5434 * enable variable sata_func_enable), the capability of the controller and 5435 * capability of a device are checked and if both support queueing, read 5436 * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT 5437 * command rather than plain READ_XXX command. 5438 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 5439 * both the controller and device suport such functionality, the read 5440 * request will be translated to READ_FPDMA_QUEUED command. 5441 * In both cases the maximum queue depth is derived as minimum of: 5442 * HBA capability,device capability and sata_max_queue_depth variable setting. 5443 * The value passed to HBA driver is decremented by 1, because only 5 bits are 5444 * used to pass max queue depth value, and the maximum possible queue depth 5445 * is 32. 5446 * 5447 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 5448 * appropriate values in scsi_pkt fields. 5449 */ 5450 static int 5451 sata_txlt_read(sata_pkt_txlate_t *spx) 5452 { 5453 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5454 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5455 sata_drive_info_t *sdinfo; 5456 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 5457 int cport = SATA_TXLT_CPORT(spx); 5458 uint16_t sec_count; 5459 uint64_t lba; 5460 int rval, reason; 5461 int synch; 5462 5463 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5464 5465 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 5466 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5467 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5468 return (rval); 5469 } 5470 5471 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 5472 &spx->txlt_sata_pkt->satapkt_device); 5473 5474 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 5475 /* 5476 * Extract LBA and sector count from scsi CDB. 5477 */ 5478 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 5479 case SCMD_READ: 5480 /* 6-byte scsi read cmd : 0x08 */ 5481 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 5482 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 5483 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5484 sec_count = scsipkt->pkt_cdbp[4]; 5485 /* sec_count 0 will be interpreted as 256 by a device */ 5486 break; 5487 case SCMD_READ_G1: 5488 /* 10-bytes scsi read command : 0x28 */ 5489 lba = scsipkt->pkt_cdbp[2]; 5490 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5491 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5492 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5493 sec_count = scsipkt->pkt_cdbp[7]; 5494 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 5495 break; 5496 case SCMD_READ_G5: 5497 /* 12-bytes scsi read command : 0xA8 */ 5498 lba = scsipkt->pkt_cdbp[2]; 5499 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5500 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5501 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5502 sec_count = scsipkt->pkt_cdbp[6]; 5503 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 5504 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 5505 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 5506 break; 5507 case SCMD_READ_G4: 5508 /* 16-bytes scsi read command : 0x88 */ 5509 lba = scsipkt->pkt_cdbp[2]; 5510 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5511 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5512 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5513 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 5514 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 5515 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 5516 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 5517 sec_count = scsipkt->pkt_cdbp[10]; 5518 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 5519 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 5520 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 5521 break; 5522 default: 5523 /* Unsupported command */ 5524 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5525 return (sata_txlt_invalid_command(spx)); 5526 } 5527 5528 /* 5529 * Check if specified address exceeds device capacity 5530 */ 5531 if ((lba >= sdinfo->satadrv_capacity) || 5532 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 5533 /* LBA out of range */ 5534 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5535 return (sata_txlt_lba_out_of_range(spx)); 5536 } 5537 5538 /* 5539 * For zero-length transfer, emulate good completion of the command 5540 * (reasons for rejecting the command were already checked). 5541 * No DMA resources were allocated. 5542 */ 5543 if (spx->txlt_dma_cookie_list == NULL) { 5544 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5545 return (sata_emul_rw_completion(spx)); 5546 } 5547 5548 /* 5549 * Build cmd block depending on the device capability and 5550 * requested operation mode. 5551 * Do not bother with non-dma mode - we are working only with 5552 * devices supporting DMA. 5553 */ 5554 scmd->satacmd_addr_type = ATA_ADDR_LBA; 5555 scmd->satacmd_device_reg = SATA_ADH_LBA; 5556 scmd->satacmd_cmd_reg = SATAC_READ_DMA; 5557 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 5558 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 5559 scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT; 5560 scmd->satacmd_sec_count_msb = sec_count >> 8; 5561 #ifndef __lock_lint 5562 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 5563 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 5564 scmd->satacmd_lba_high_msb = lba >> 40; 5565 #endif 5566 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 5567 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 5568 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 5569 } 5570 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 5571 scmd->satacmd_lba_low_lsb = lba & 0xff; 5572 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 5573 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 5574 scmd->satacmd_features_reg = 0; 5575 scmd->satacmd_status_reg = 0; 5576 scmd->satacmd_error_reg = 0; 5577 5578 /* 5579 * Check if queueing commands should be used and switch 5580 * to appropriate command if possible 5581 */ 5582 if (sata_func_enable & SATA_ENABLE_QUEUING) { 5583 boolean_t using_queuing; 5584 5585 /* Queuing supported by controller and device? */ 5586 if ((sata_func_enable & SATA_ENABLE_NCQ) && 5587 (sdinfo->satadrv_features_support & 5588 SATA_DEV_F_NCQ) && 5589 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 5590 SATA_CTLF_NCQ)) { 5591 using_queuing = B_TRUE; 5592 5593 /* NCQ supported - use FPDMA READ */ 5594 scmd->satacmd_cmd_reg = 5595 SATAC_READ_FPDMA_QUEUED; 5596 scmd->satacmd_features_reg_ext = 5597 scmd->satacmd_sec_count_msb; 5598 scmd->satacmd_sec_count_msb = 0; 5599 } else if ((sdinfo->satadrv_features_support & 5600 SATA_DEV_F_TCQ) && 5601 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 5602 SATA_CTLF_QCMD)) { 5603 using_queuing = B_TRUE; 5604 5605 /* Legacy queueing */ 5606 if (sdinfo->satadrv_features_support & 5607 SATA_DEV_F_LBA48) { 5608 scmd->satacmd_cmd_reg = 5609 SATAC_READ_DMA_QUEUED_EXT; 5610 scmd->satacmd_features_reg_ext = 5611 scmd->satacmd_sec_count_msb; 5612 scmd->satacmd_sec_count_msb = 0; 5613 } else { 5614 scmd->satacmd_cmd_reg = 5615 SATAC_READ_DMA_QUEUED; 5616 } 5617 } else /* NCQ nor legacy queuing not supported */ 5618 using_queuing = B_FALSE; 5619 5620 /* 5621 * If queuing, the sector count goes in the features register 5622 * and the secount count will contain the tag. 5623 */ 5624 if (using_queuing) { 5625 scmd->satacmd_features_reg = 5626 scmd->satacmd_sec_count_lsb; 5627 scmd->satacmd_sec_count_lsb = 0; 5628 scmd->satacmd_flags.sata_queued = B_TRUE; 5629 5630 /* Set-up maximum queue depth */ 5631 scmd->satacmd_flags.sata_max_queue_depth = 5632 sdinfo->satadrv_max_queue_depth - 1; 5633 } else if (sdinfo->satadrv_features_enabled & 5634 SATA_DEV_F_E_UNTAGGED_QING) { 5635 /* 5636 * Although NCQ/TCQ is not enabled, untagged queuing 5637 * may be still used. 5638 * Set-up the maximum untagged queue depth. 5639 * Use controller's queue depth from sata_hba_tran. 5640 * SATA HBA drivers may ignore this value and rely on 5641 * the internal limits.For drivers that do not 5642 * ignore untaged queue depth, limit the value to 5643 * SATA_MAX_QUEUE_DEPTH (32), as this is the 5644 * largest value that can be passed via 5645 * satacmd_flags.sata_max_queue_depth. 5646 */ 5647 scmd->satacmd_flags.sata_max_queue_depth = 5648 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ? 5649 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1; 5650 5651 } else { 5652 scmd->satacmd_flags.sata_max_queue_depth = 0; 5653 } 5654 } else 5655 scmd->satacmd_flags.sata_max_queue_depth = 0; 5656 5657 SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst, 5658 "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n", 5659 scmd->satacmd_cmd_reg, lba, sec_count); 5660 5661 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5662 /* Need callback function */ 5663 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 5664 synch = FALSE; 5665 } else 5666 synch = TRUE; 5667 5668 /* Transfer command to HBA */ 5669 if (sata_hba_start(spx, &rval) != 0) { 5670 /* Pkt not accepted for execution */ 5671 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5672 return (rval); 5673 } 5674 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5675 /* 5676 * If execution is non-synchronous, 5677 * a callback function will handle potential errors, translate 5678 * the response and will do a callback to a target driver. 5679 * If it was synchronous, check execution status using the same 5680 * framework callback. 5681 */ 5682 if (synch) { 5683 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5684 "synchronous execution status %x\n", 5685 spx->txlt_sata_pkt->satapkt_reason); 5686 sata_txlt_rw_completion(spx->txlt_sata_pkt); 5687 } 5688 return (TRAN_ACCEPT); 5689 } 5690 5691 5692 /* 5693 * SATA translate command: Write (various types) 5694 * Translated into appropriate type of ATA WRITE command 5695 * for SATA hard disks. 5696 * Both the device capabilities and requested operation mode are 5697 * considered. 5698 * 5699 * Following scsi cdb fields are ignored: 5700 * rwprotect, dpo, fua, fua_nv, group_number. 5701 * 5702 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 5703 * enable variable sata_func_enable), the capability of the controller and 5704 * capability of a device are checked and if both support queueing, write 5705 * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT 5706 * command rather than plain WRITE_XXX command. 5707 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 5708 * both the controller and device suport such functionality, the write 5709 * request will be translated to WRITE_FPDMA_QUEUED command. 5710 * In both cases the maximum queue depth is derived as minimum of: 5711 * HBA capability,device capability and sata_max_queue_depth variable setting. 5712 * The value passed to HBA driver is decremented by 1, because only 5 bits are 5713 * used to pass max queue depth value, and the maximum possible queue depth 5714 * is 32. 5715 * 5716 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 5717 * appropriate values in scsi_pkt fields. 5718 */ 5719 static int 5720 sata_txlt_write(sata_pkt_txlate_t *spx) 5721 { 5722 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5723 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5724 sata_drive_info_t *sdinfo; 5725 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 5726 int cport = SATA_TXLT_CPORT(spx); 5727 uint16_t sec_count; 5728 uint64_t lba; 5729 int rval, reason; 5730 int synch; 5731 5732 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5733 5734 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 5735 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5736 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5737 return (rval); 5738 } 5739 5740 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 5741 &spx->txlt_sata_pkt->satapkt_device); 5742 5743 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 5744 /* 5745 * Extract LBA and sector count from scsi CDB 5746 */ 5747 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 5748 case SCMD_WRITE: 5749 /* 6-byte scsi read cmd : 0x0A */ 5750 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 5751 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 5752 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5753 sec_count = scsipkt->pkt_cdbp[4]; 5754 /* sec_count 0 will be interpreted as 256 by a device */ 5755 break; 5756 case SCMD_WRITE_G1: 5757 /* 10-bytes scsi write command : 0x2A */ 5758 lba = scsipkt->pkt_cdbp[2]; 5759 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5760 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5761 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5762 sec_count = scsipkt->pkt_cdbp[7]; 5763 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 5764 break; 5765 case SCMD_WRITE_G5: 5766 /* 12-bytes scsi read command : 0xAA */ 5767 lba = scsipkt->pkt_cdbp[2]; 5768 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5769 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5770 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5771 sec_count = scsipkt->pkt_cdbp[6]; 5772 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 5773 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 5774 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 5775 break; 5776 case SCMD_WRITE_G4: 5777 /* 16-bytes scsi write command : 0x8A */ 5778 lba = scsipkt->pkt_cdbp[2]; 5779 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5780 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5781 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5782 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 5783 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 5784 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 5785 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 5786 sec_count = scsipkt->pkt_cdbp[10]; 5787 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 5788 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 5789 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 5790 break; 5791 default: 5792 /* Unsupported command */ 5793 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5794 return (sata_txlt_invalid_command(spx)); 5795 } 5796 5797 /* 5798 * Check if specified address and length exceeds device capacity 5799 */ 5800 if ((lba >= sdinfo->satadrv_capacity) || 5801 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 5802 /* LBA out of range */ 5803 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5804 return (sata_txlt_lba_out_of_range(spx)); 5805 } 5806 5807 /* 5808 * For zero-length transfer, emulate good completion of the command 5809 * (reasons for rejecting the command were already checked). 5810 * No DMA resources were allocated. 5811 */ 5812 if (spx->txlt_dma_cookie_list == NULL) { 5813 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5814 return (sata_emul_rw_completion(spx)); 5815 } 5816 5817 /* 5818 * Build cmd block depending on the device capability and 5819 * requested operation mode. 5820 * Do not bother with non-dma mode- we are working only with 5821 * devices supporting DMA. 5822 */ 5823 scmd->satacmd_addr_type = ATA_ADDR_LBA; 5824 scmd->satacmd_device_reg = SATA_ADH_LBA; 5825 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA; 5826 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 5827 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 5828 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT; 5829 scmd->satacmd_sec_count_msb = sec_count >> 8; 5830 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 5831 #ifndef __lock_lint 5832 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 5833 scmd->satacmd_lba_high_msb = lba >> 40; 5834 #endif 5835 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 5836 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 5837 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 5838 } 5839 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 5840 scmd->satacmd_lba_low_lsb = lba & 0xff; 5841 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 5842 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 5843 scmd->satacmd_features_reg = 0; 5844 scmd->satacmd_status_reg = 0; 5845 scmd->satacmd_error_reg = 0; 5846 5847 /* 5848 * Check if queueing commands should be used and switch 5849 * to appropriate command if possible 5850 */ 5851 if (sata_func_enable & SATA_ENABLE_QUEUING) { 5852 boolean_t using_queuing; 5853 5854 /* Queuing supported by controller and device? */ 5855 if ((sata_func_enable & SATA_ENABLE_NCQ) && 5856 (sdinfo->satadrv_features_support & 5857 SATA_DEV_F_NCQ) && 5858 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 5859 SATA_CTLF_NCQ)) { 5860 using_queuing = B_TRUE; 5861 5862 /* NCQ supported - use FPDMA WRITE */ 5863 scmd->satacmd_cmd_reg = 5864 SATAC_WRITE_FPDMA_QUEUED; 5865 scmd->satacmd_features_reg_ext = 5866 scmd->satacmd_sec_count_msb; 5867 scmd->satacmd_sec_count_msb = 0; 5868 } else if ((sdinfo->satadrv_features_support & 5869 SATA_DEV_F_TCQ) && 5870 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 5871 SATA_CTLF_QCMD)) { 5872 using_queuing = B_TRUE; 5873 5874 /* Legacy queueing */ 5875 if (sdinfo->satadrv_features_support & 5876 SATA_DEV_F_LBA48) { 5877 scmd->satacmd_cmd_reg = 5878 SATAC_WRITE_DMA_QUEUED_EXT; 5879 scmd->satacmd_features_reg_ext = 5880 scmd->satacmd_sec_count_msb; 5881 scmd->satacmd_sec_count_msb = 0; 5882 } else { 5883 scmd->satacmd_cmd_reg = 5884 SATAC_WRITE_DMA_QUEUED; 5885 } 5886 } else /* NCQ nor legacy queuing not supported */ 5887 using_queuing = B_FALSE; 5888 5889 if (using_queuing) { 5890 scmd->satacmd_features_reg = 5891 scmd->satacmd_sec_count_lsb; 5892 scmd->satacmd_sec_count_lsb = 0; 5893 scmd->satacmd_flags.sata_queued = B_TRUE; 5894 /* Set-up maximum queue depth */ 5895 scmd->satacmd_flags.sata_max_queue_depth = 5896 sdinfo->satadrv_max_queue_depth - 1; 5897 } else if (sdinfo->satadrv_features_enabled & 5898 SATA_DEV_F_E_UNTAGGED_QING) { 5899 /* 5900 * Although NCQ/TCQ is not enabled, untagged queuing 5901 * may be still used. 5902 * Set-up the maximum untagged queue depth. 5903 * Use controller's queue depth from sata_hba_tran. 5904 * SATA HBA drivers may ignore this value and rely on 5905 * the internal limits. For drivera that do not 5906 * ignore untaged queue depth, limit the value to 5907 * SATA_MAX_QUEUE_DEPTH (32), as this is the 5908 * largest value that can be passed via 5909 * satacmd_flags.sata_max_queue_depth. 5910 */ 5911 scmd->satacmd_flags.sata_max_queue_depth = 5912 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ? 5913 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1; 5914 5915 } else { 5916 scmd->satacmd_flags.sata_max_queue_depth = 0; 5917 } 5918 } else 5919 scmd->satacmd_flags.sata_max_queue_depth = 0; 5920 5921 SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5922 "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n", 5923 scmd->satacmd_cmd_reg, lba, sec_count); 5924 5925 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5926 /* Need callback function */ 5927 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 5928 synch = FALSE; 5929 } else 5930 synch = TRUE; 5931 5932 /* Transfer command to HBA */ 5933 if (sata_hba_start(spx, &rval) != 0) { 5934 /* Pkt not accepted for execution */ 5935 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5936 return (rval); 5937 } 5938 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5939 5940 /* 5941 * If execution is non-synchronous, 5942 * a callback function will handle potential errors, translate 5943 * the response and will do a callback to a target driver. 5944 * If it was synchronous, check execution status using the same 5945 * framework callback. 5946 */ 5947 if (synch) { 5948 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5949 "synchronous execution status %x\n", 5950 spx->txlt_sata_pkt->satapkt_reason); 5951 sata_txlt_rw_completion(spx->txlt_sata_pkt); 5952 } 5953 return (TRAN_ACCEPT); 5954 } 5955 5956 5957 /* 5958 * Implements SCSI SBC WRITE BUFFER command download microcode option 5959 */ 5960 static int 5961 sata_txlt_write_buffer(sata_pkt_txlate_t *spx) 5962 { 5963 #define WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE 4 5964 #define WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE 5 5965 5966 sata_hba_inst_t *sata_hba_inst = SATA_TXLT_HBA_INST(spx); 5967 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5968 struct sata_pkt *sata_pkt = spx->txlt_sata_pkt; 5969 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5970 5971 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5972 struct scsi_extended_sense *sense; 5973 int rval, mode, sector_count, reason; 5974 int cport = SATA_TXLT_CPORT(spx); 5975 5976 mode = scsipkt->pkt_cdbp[1] & 0x1f; 5977 5978 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5979 "sata_txlt_write_buffer, mode 0x%x\n", mode); 5980 5981 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5982 5983 if ((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 5984 TRAN_ACCEPT) { 5985 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5986 return (rval); 5987 } 5988 5989 /* Use synchronous mode */ 5990 spx->txlt_sata_pkt->satapkt_op_mode 5991 |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 5992 5993 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 5994 5995 scsipkt->pkt_reason = CMD_CMPLT; 5996 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5997 STATE_SENT_CMD | STATE_GOT_STATUS; 5998 5999 /* 6000 * The SCSI to ATA translation specification only calls 6001 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE. 6002 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but 6003 * ATA 8 (draft) got rid of download microcode for temp 6004 * and it is even optional for ATA 7, so it may be aborted. 6005 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as 6006 * it is not specified and the buffer offset for SCSI is a 16-bit 6007 * value in bytes, but for ATA it is a 16-bit offset in 512 byte 6008 * sectors. Thus the offset really doesn't buy us anything. 6009 * If and when ATA 8 is stabilized and the SCSI to ATA specification 6010 * is revised, this can be revisisted. 6011 */ 6012 /* Reject not supported request */ 6013 switch (mode) { 6014 case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE: 6015 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP; 6016 break; 6017 case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE: 6018 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE; 6019 break; 6020 default: 6021 goto bad_param; 6022 } 6023 6024 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 6025 6026 scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE; 6027 if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0) 6028 goto bad_param; 6029 sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE; 6030 scmd->satacmd_sec_count_lsb = (uint8_t)sector_count; 6031 scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8; 6032 scmd->satacmd_lba_mid_lsb = 0; 6033 scmd->satacmd_lba_high_lsb = 0; 6034 scmd->satacmd_device_reg = 0; 6035 spx->txlt_sata_pkt->satapkt_comp = NULL; 6036 scmd->satacmd_addr_type = 0; 6037 6038 /* Transfer command to HBA */ 6039 if (sata_hba_start(spx, &rval) != 0) { 6040 /* Pkt not accepted for execution */ 6041 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 6042 return (rval); 6043 } 6044 6045 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 6046 6047 /* Then we need synchronous check the status of the disk */ 6048 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6049 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 6050 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 6051 scsipkt->pkt_reason = CMD_CMPLT; 6052 6053 /* Download commmand succeed, so probe and identify device */ 6054 sata_reidentify_device(spx); 6055 } else { 6056 /* Something went wrong, microcode download command failed */ 6057 scsipkt->pkt_reason = CMD_INCOMPLETE; 6058 *scsipkt->pkt_scbp = STATUS_CHECK; 6059 sense = sata_arq_sense(spx); 6060 switch (sata_pkt->satapkt_reason) { 6061 case SATA_PKT_PORT_ERROR: 6062 /* 6063 * We have no device data. Assume no data transfered. 6064 */ 6065 sense->es_key = KEY_HARDWARE_ERROR; 6066 break; 6067 6068 case SATA_PKT_DEV_ERROR: 6069 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 6070 SATA_STATUS_ERR) { 6071 /* 6072 * determine dev error reason from error 6073 * reg content 6074 */ 6075 sata_decode_device_error(spx, sense); 6076 break; 6077 } 6078 /* No extended sense key - no info available */ 6079 break; 6080 6081 case SATA_PKT_TIMEOUT: 6082 scsipkt->pkt_reason = CMD_TIMEOUT; 6083 scsipkt->pkt_statistics |= 6084 STAT_TIMEOUT | STAT_DEV_RESET; 6085 /* No extended sense key ? */ 6086 break; 6087 6088 case SATA_PKT_ABORTED: 6089 scsipkt->pkt_reason = CMD_ABORTED; 6090 scsipkt->pkt_statistics |= STAT_ABORTED; 6091 /* No extended sense key ? */ 6092 break; 6093 6094 case SATA_PKT_RESET: 6095 /* pkt aborted by an explicit reset from a host */ 6096 scsipkt->pkt_reason = CMD_RESET; 6097 scsipkt->pkt_statistics |= STAT_DEV_RESET; 6098 break; 6099 6100 default: 6101 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 6102 "sata_txlt_nodata_cmd_completion: " 6103 "invalid packet completion reason %d", 6104 sata_pkt->satapkt_reason)); 6105 scsipkt->pkt_reason = CMD_TRAN_ERR; 6106 break; 6107 } 6108 6109 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6110 "scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6111 6112 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 6113 /* scsi callback required */ 6114 scsi_hba_pkt_comp(scsipkt); 6115 } 6116 return (TRAN_ACCEPT); 6117 6118 bad_param: 6119 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 6120 *scsipkt->pkt_scbp = STATUS_CHECK; 6121 sense = sata_arq_sense(spx); 6122 sense->es_key = KEY_ILLEGAL_REQUEST; 6123 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 6124 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6125 scsipkt->pkt_comp != NULL) { 6126 /* scsi callback required */ 6127 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6128 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 6129 TQ_SLEEP) == 0) { 6130 /* Scheduling the callback failed */ 6131 rval = TRAN_BUSY; 6132 } 6133 } 6134 return (rval); 6135 } 6136 6137 /* 6138 * Re-identify device after doing a firmware download. 6139 */ 6140 static void 6141 sata_reidentify_device(sata_pkt_txlate_t *spx) 6142 { 6143 #define DOWNLOAD_WAIT_TIME_SECS 60 6144 #define DOWNLOAD_WAIT_INTERVAL_SECS 1 6145 int rval; 6146 int retry_cnt; 6147 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6148 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 6149 sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device; 6150 sata_drive_info_t *sdinfo; 6151 6152 /* 6153 * Before returning good status, probe device. 6154 * Device probing will get IDENTIFY DEVICE data, if possible. 6155 * The assumption is that the new microcode is applied by the 6156 * device. It is a caller responsibility to verify this. 6157 */ 6158 for (retry_cnt = 0; 6159 retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS; 6160 retry_cnt++) { 6161 rval = sata_probe_device(sata_hba_inst, &sata_device); 6162 6163 if (rval == SATA_SUCCESS) { /* Set default features */ 6164 sdinfo = sata_get_device_info(sata_hba_inst, 6165 &sata_device); 6166 if (sata_initialize_device(sata_hba_inst, sdinfo) != 6167 SATA_SUCCESS) { 6168 /* retry */ 6169 rval = sata_initialize_device(sata_hba_inst, 6170 sdinfo); 6171 if (rval == SATA_RETRY) 6172 sata_log(sata_hba_inst, CE_WARN, 6173 "SATA device at port %d pmport %d -" 6174 " default device features could not" 6175 " be set. Device may not operate " 6176 "as expected.", 6177 sata_device.satadev_addr.cport, 6178 sata_device.satadev_addr.pmport); 6179 } 6180 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 6181 scsi_hba_pkt_comp(scsipkt); 6182 return; 6183 } else if (rval == SATA_RETRY) { 6184 delay(drv_usectohz(1000000 * 6185 DOWNLOAD_WAIT_INTERVAL_SECS)); 6186 continue; 6187 } else /* failed - no reason to retry */ 6188 break; 6189 } 6190 6191 /* 6192 * Something went wrong, device probing failed. 6193 */ 6194 SATA_LOG_D((sata_hba_inst, CE_WARN, 6195 "Cannot probe device after downloading microcode\n")); 6196 6197 /* Reset device to force retrying the probe. */ 6198 (void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 6199 (SATA_DIP(sata_hba_inst), &sata_device); 6200 6201 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 6202 scsi_hba_pkt_comp(scsipkt); 6203 } 6204 6205 6206 /* 6207 * Translate command: Synchronize Cache. 6208 * Translates into Flush Cache command for SATA hard disks. 6209 * 6210 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 6211 * appropriate values in scsi_pkt fields. 6212 */ 6213 static int 6214 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx) 6215 { 6216 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6217 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 6218 int cport = SATA_TXLT_CPORT(spx); 6219 int rval, reason; 6220 int synch; 6221 6222 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 6223 6224 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 6225 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 6226 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 6227 return (rval); 6228 } 6229 6230 scmd->satacmd_addr_type = 0; 6231 scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE; 6232 scmd->satacmd_device_reg = 0; 6233 scmd->satacmd_sec_count_lsb = 0; 6234 scmd->satacmd_lba_low_lsb = 0; 6235 scmd->satacmd_lba_mid_lsb = 0; 6236 scmd->satacmd_lba_high_lsb = 0; 6237 scmd->satacmd_features_reg = 0; 6238 scmd->satacmd_status_reg = 0; 6239 scmd->satacmd_error_reg = 0; 6240 6241 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6242 "sata_txlt_synchronize_cache\n", NULL); 6243 6244 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 6245 /* Need to set-up a callback function */ 6246 spx->txlt_sata_pkt->satapkt_comp = 6247 sata_txlt_nodata_cmd_completion; 6248 synch = FALSE; 6249 } else 6250 synch = TRUE; 6251 6252 /* Transfer command to HBA */ 6253 if (sata_hba_start(spx, &rval) != 0) { 6254 /* Pkt not accepted for execution */ 6255 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 6256 return (rval); 6257 } 6258 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 6259 6260 /* 6261 * If execution non-synchronous, it had to be completed 6262 * a callback function will handle potential errors, translate 6263 * the response and will do a callback to a target driver. 6264 * If it was synchronous, check status, using the same 6265 * framework callback. 6266 */ 6267 if (synch) { 6268 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6269 "synchronous execution status %x\n", 6270 spx->txlt_sata_pkt->satapkt_reason); 6271 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 6272 } 6273 return (TRAN_ACCEPT); 6274 } 6275 6276 6277 /* 6278 * Send pkt to SATA HBA driver 6279 * 6280 * This function may be called only if the operation is requested by scsi_pkt, 6281 * i.e. scsi_pkt is not NULL. 6282 * 6283 * This function has to be called with cport mutex held. It does release 6284 * the mutex when it calls HBA driver sata_tran_start function and 6285 * re-acquires it afterwards. 6286 * 6287 * If return value is 0, pkt was accepted, -1 otherwise 6288 * rval is set to appropriate sata_scsi_start return value. 6289 * 6290 * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not 6291 * have called the sata_pkt callback function for this packet. 6292 * 6293 * The scsi callback has to be performed by the caller of this routine. 6294 */ 6295 static int 6296 sata_hba_start(sata_pkt_txlate_t *spx, int *rval) 6297 { 6298 int stat; 6299 uint8_t cport = SATA_TXLT_CPORT(spx); 6300 uint8_t pmport = SATA_TXLT_PMPORT(spx); 6301 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 6302 sata_drive_info_t *sdinfo; 6303 sata_pmult_info_t *pminfo; 6304 sata_pmport_info_t *pmportinfo = NULL; 6305 sata_device_t *sata_device = NULL; 6306 uint8_t cmd; 6307 struct sata_cmd_flags cmd_flags; 6308 6309 ASSERT(spx->txlt_sata_pkt != NULL); 6310 6311 ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6312 6313 sdinfo = sata_get_device_info(sata_hba_inst, 6314 &spx->txlt_sata_pkt->satapkt_device); 6315 ASSERT(sdinfo != NULL); 6316 6317 /* Clear device reset state? */ 6318 /* qual should be XXX_DPMPORT, but add XXX_PMPORT in case */ 6319 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT || 6320 sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) { 6321 6322 /* 6323 * Get the pmult_info of the its parent port multiplier, all 6324 * sub-devices share a common device reset flags on in 6325 * pmult_info. 6326 */ 6327 pminfo = SATA_PMULT_INFO(sata_hba_inst, cport); 6328 pmportinfo = pminfo->pmult_dev_port[pmport]; 6329 ASSERT(pminfo != NULL); 6330 if (pminfo->pmult_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) { 6331 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 6332 sata_clear_dev_reset = B_TRUE; 6333 pminfo->pmult_event_flags &= 6334 ~SATA_EVNT_CLEAR_DEVICE_RESET; 6335 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6336 "sata_hba_start: clearing device reset state" 6337 "on pmult.\n", NULL); 6338 } 6339 } else { 6340 if (sdinfo->satadrv_event_flags & 6341 SATA_EVNT_CLEAR_DEVICE_RESET) { 6342 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 6343 sata_clear_dev_reset = B_TRUE; 6344 sdinfo->satadrv_event_flags &= 6345 ~SATA_EVNT_CLEAR_DEVICE_RESET; 6346 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6347 "sata_hba_start: clearing device reset state\n", 6348 NULL); 6349 } 6350 } 6351 6352 cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg; 6353 cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags; 6354 sata_device = &spx->txlt_sata_pkt->satapkt_device; 6355 6356 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6357 6358 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6359 "Sata cmd 0x%2x\n", cmd); 6360 6361 stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 6362 spx->txlt_sata_pkt); 6363 6364 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6365 /* 6366 * If sata pkt was accepted and executed in asynchronous mode, i.e. 6367 * with the sata callback, the sata_pkt could be already destroyed 6368 * by the time we check ther return status from the hba_start() 6369 * function, because sata_scsi_destroy_pkt() could have been already 6370 * called (perhaps in the interrupt context). So, in such case, there 6371 * should be no references to it. In other cases, sata_pkt still 6372 * exists. 6373 */ 6374 if (stat == SATA_TRAN_ACCEPTED) { 6375 /* 6376 * pkt accepted for execution. 6377 * If it was executed synchronously, it is already completed 6378 * and pkt completion_reason indicates completion status. 6379 */ 6380 *rval = TRAN_ACCEPT; 6381 return (0); 6382 } 6383 6384 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 6385 switch (stat) { 6386 case SATA_TRAN_QUEUE_FULL: 6387 /* 6388 * Controller detected queue full condition. 6389 */ 6390 SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst, 6391 "sata_hba_start: queue full\n", NULL); 6392 6393 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 6394 *spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL; 6395 6396 *rval = TRAN_BUSY; 6397 break; 6398 6399 case SATA_TRAN_PORT_ERROR: 6400 /* 6401 * Communication/link with device or general port error 6402 * detected before pkt execution begun. 6403 */ 6404 if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 6405 SATA_ADDR_CPORT || 6406 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 6407 SATA_ADDR_DCPORT) 6408 sata_log(sata_hba_inst, CE_CONT, 6409 "SATA port %d error", 6410 sata_device->satadev_addr.cport); 6411 else 6412 sata_log(sata_hba_inst, CE_CONT, 6413 "SATA port %d:%d error\n", 6414 sata_device->satadev_addr.cport, 6415 sata_device->satadev_addr.pmport); 6416 6417 /* 6418 * Update the port/device structure. 6419 * sata_pkt should be still valid. Since port error is 6420 * returned, sata_device content should reflect port 6421 * state - it means, that sata address have been changed, 6422 * because original packet's sata address refered to a device 6423 * attached to some port. 6424 */ 6425 if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT || 6426 sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) { 6427 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6428 mutex_enter(&pmportinfo->pmport_mutex); 6429 sata_update_pmport_info(sata_hba_inst, sata_device); 6430 mutex_exit(&pmportinfo->pmport_mutex); 6431 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6432 } else { 6433 sata_update_port_info(sata_hba_inst, sata_device); 6434 } 6435 6436 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 6437 *rval = TRAN_FATAL_ERROR; 6438 break; 6439 6440 case SATA_TRAN_CMD_UNSUPPORTED: 6441 /* 6442 * Command rejected by HBA as unsupported. It was HBA driver 6443 * that rejected the command, command was not sent to 6444 * an attached device. 6445 */ 6446 if ((sdinfo != NULL) && 6447 (sdinfo->satadrv_state & SATA_DSTATE_RESET)) 6448 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6449 "sat_hba_start: cmd 0x%2x rejected " 6450 "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd); 6451 6452 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6453 (void) sata_txlt_invalid_command(spx); 6454 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6455 6456 *rval = TRAN_ACCEPT; 6457 break; 6458 6459 case SATA_TRAN_BUSY: 6460 /* 6461 * Command rejected by HBA because other operation prevents 6462 * accepting the packet, or device is in RESET condition. 6463 */ 6464 if (sdinfo != NULL) { 6465 sdinfo->satadrv_state = 6466 spx->txlt_sata_pkt->satapkt_device.satadev_state; 6467 6468 if (sdinfo->satadrv_state & SATA_DSTATE_RESET) { 6469 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6470 "sata_hba_start: cmd 0x%2x rejected " 6471 "because of device reset condition\n", 6472 cmd); 6473 } else { 6474 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6475 "sata_hba_start: cmd 0x%2x rejected " 6476 "with SATA_TRAN_BUSY status\n", 6477 cmd); 6478 } 6479 } 6480 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 6481 *rval = TRAN_BUSY; 6482 break; 6483 6484 default: 6485 /* Unrecognized HBA response */ 6486 SATA_LOG_D((sata_hba_inst, CE_WARN, 6487 "sata_hba_start: unrecognized HBA response " 6488 "to cmd : 0x%2x resp 0x%x", cmd, rval)); 6489 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 6490 *rval = TRAN_FATAL_ERROR; 6491 break; 6492 } 6493 6494 /* 6495 * If we got here, the packet was rejected. 6496 * Check if we need to remember reset state clearing request 6497 */ 6498 if (cmd_flags.sata_clear_dev_reset) { 6499 /* 6500 * Check if device is still configured - it may have 6501 * disapeared from the configuration 6502 */ 6503 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 6504 if (sdinfo != NULL) { 6505 /* 6506 * Restore the flag that requests clearing of 6507 * the device reset state, 6508 * so the next sata packet may carry it to HBA. 6509 */ 6510 if (sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT || 6511 sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT) { 6512 pminfo->pmult_event_flags |= 6513 SATA_EVNT_CLEAR_DEVICE_RESET; 6514 } else { 6515 sdinfo->satadrv_event_flags |= 6516 SATA_EVNT_CLEAR_DEVICE_RESET; 6517 } 6518 } 6519 } 6520 return (-1); 6521 } 6522 6523 /* 6524 * Scsi response setup for invalid LBA 6525 * 6526 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 6527 */ 6528 static int 6529 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx) 6530 { 6531 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6532 struct scsi_extended_sense *sense; 6533 6534 scsipkt->pkt_reason = CMD_CMPLT; 6535 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6536 STATE_SENT_CMD | STATE_GOT_STATUS; 6537 *scsipkt->pkt_scbp = STATUS_CHECK; 6538 6539 *scsipkt->pkt_scbp = STATUS_CHECK; 6540 sense = sata_arq_sense(spx); 6541 sense->es_key = KEY_ILLEGAL_REQUEST; 6542 sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE; 6543 6544 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6545 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6546 6547 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6548 scsipkt->pkt_comp != NULL) 6549 /* scsi callback required */ 6550 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6551 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 6552 TQ_SLEEP) == NULL) 6553 /* Scheduling the callback failed */ 6554 return (TRAN_BUSY); 6555 return (TRAN_ACCEPT); 6556 } 6557 6558 6559 /* 6560 * Analyze device status and error registers and translate them into 6561 * appropriate scsi sense codes. 6562 * NOTE: non-packet commands only for now 6563 */ 6564 static void 6565 sata_decode_device_error(sata_pkt_txlate_t *spx, 6566 struct scsi_extended_sense *sense) 6567 { 6568 uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg; 6569 6570 ASSERT(sense != NULL); 6571 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 6572 SATA_STATUS_ERR); 6573 6574 6575 if (err_reg & SATA_ERROR_ICRC) { 6576 sense->es_key = KEY_ABORTED_COMMAND; 6577 sense->es_add_code = 0x08; /* Communication failure */ 6578 return; 6579 } 6580 6581 if (err_reg & SATA_ERROR_UNC) { 6582 sense->es_key = KEY_MEDIUM_ERROR; 6583 /* Information bytes (LBA) need to be set by a caller */ 6584 return; 6585 } 6586 6587 /* ADD HERE: MC error bit handling for ATAPI CD/DVD */ 6588 if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) { 6589 sense->es_key = KEY_UNIT_ATTENTION; 6590 sense->es_add_code = 0x3a; /* No media present */ 6591 return; 6592 } 6593 6594 if (err_reg & SATA_ERROR_IDNF) { 6595 if (err_reg & SATA_ERROR_ABORT) { 6596 sense->es_key = KEY_ABORTED_COMMAND; 6597 } else { 6598 sense->es_key = KEY_ILLEGAL_REQUEST; 6599 sense->es_add_code = 0x21; /* LBA out of range */ 6600 } 6601 return; 6602 } 6603 6604 if (err_reg & SATA_ERROR_ABORT) { 6605 ASSERT(spx->txlt_sata_pkt != NULL); 6606 sense->es_key = KEY_ABORTED_COMMAND; 6607 return; 6608 } 6609 } 6610 6611 /* 6612 * Extract error LBA from sata_pkt.satapkt_cmd register fields 6613 */ 6614 static void 6615 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba) 6616 { 6617 sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd; 6618 6619 *lba = 0; 6620 if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) { 6621 *lba = sata_cmd->satacmd_lba_high_msb; 6622 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb; 6623 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb; 6624 } else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) { 6625 *lba = sata_cmd->satacmd_device_reg & 0xf; 6626 } 6627 *lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb; 6628 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb; 6629 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb; 6630 } 6631 6632 /* 6633 * This is fixed sense format - if LBA exceeds the info field size, 6634 * no valid info will be returned (valid bit in extended sense will 6635 * be set to 0). 6636 */ 6637 static struct scsi_extended_sense * 6638 sata_arq_sense(sata_pkt_txlate_t *spx) 6639 { 6640 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6641 struct scsi_arq_status *arqs; 6642 struct scsi_extended_sense *sense; 6643 6644 /* Fill ARQ sense data */ 6645 scsipkt->pkt_state |= STATE_ARQ_DONE; 6646 arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp; 6647 *(uchar_t *)&arqs->sts_status = STATUS_CHECK; 6648 *(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD; 6649 arqs->sts_rqpkt_reason = CMD_CMPLT; 6650 arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6651 STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS; 6652 arqs->sts_rqpkt_resid = 0; 6653 sense = &arqs->sts_sensedata; 6654 bzero(sense, sizeof (struct scsi_extended_sense)); 6655 sata_fixed_sense_data_preset(sense); 6656 return (sense); 6657 } 6658 6659 /* 6660 * ATA Pass Through support 6661 * Sets flags indicating that an invalid value was found in some 6662 * field in the command. It could be something illegal according to 6663 * the SAT-2 spec or it could be a feature that is not (yet?) 6664 * supported. 6665 */ 6666 static int 6667 sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *spx) 6668 { 6669 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6670 struct scsi_extended_sense *sense = sata_arq_sense(spx); 6671 6672 scsipkt->pkt_reason = CMD_CMPLT; 6673 *scsipkt->pkt_scbp = STATUS_CHECK; 6674 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6675 STATE_SENT_CMD | STATE_GOT_STATUS; 6676 6677 sense = sata_arq_sense(spx); 6678 sense->es_key = KEY_ILLEGAL_REQUEST; 6679 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 6680 6681 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6682 scsipkt->pkt_comp != NULL) 6683 /* scsi callback required */ 6684 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6685 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 6686 TQ_SLEEP) == NULL) 6687 /* Scheduling the callback failed */ 6688 return (TRAN_BUSY); 6689 6690 return (TRAN_ACCEPT); 6691 } 6692 6693 /* 6694 * Emulated SATA Read/Write command completion for zero-length requests. 6695 * This request always succedes, so in synchronous mode it always returns 6696 * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the 6697 * callback cannot be scheduled. 6698 */ 6699 static int 6700 sata_emul_rw_completion(sata_pkt_txlate_t *spx) 6701 { 6702 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6703 6704 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6705 STATE_SENT_CMD | STATE_GOT_STATUS; 6706 scsipkt->pkt_reason = CMD_CMPLT; 6707 *scsipkt->pkt_scbp = STATUS_GOOD; 6708 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 6709 /* scsi callback required - have to schedule it */ 6710 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6711 (task_func_t *)scsipkt->pkt_comp, 6712 (void *)scsipkt, TQ_SLEEP) == NULL) 6713 /* Scheduling the callback failed */ 6714 return (TRAN_BUSY); 6715 } 6716 return (TRAN_ACCEPT); 6717 } 6718 6719 6720 /* 6721 * Translate completion status of SATA read/write commands into scsi response. 6722 * pkt completion_reason is checked to determine the completion status. 6723 * Do scsi callback if necessary. 6724 * 6725 * Note: this function may be called also for synchronously executed 6726 * commands. 6727 * This function may be used only if scsi_pkt is non-NULL. 6728 */ 6729 static void 6730 sata_txlt_rw_completion(sata_pkt_t *sata_pkt) 6731 { 6732 sata_pkt_txlate_t *spx = 6733 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 6734 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 6735 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6736 struct scsi_extended_sense *sense; 6737 uint64_t lba; 6738 struct buf *bp; 6739 int rval; 6740 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 6741 /* Normal completion */ 6742 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6743 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 6744 scsipkt->pkt_reason = CMD_CMPLT; 6745 *scsipkt->pkt_scbp = STATUS_GOOD; 6746 if (spx->txlt_tmp_buf != NULL) { 6747 /* Temporary buffer was used */ 6748 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 6749 if (bp->b_flags & B_READ) { 6750 rval = ddi_dma_sync( 6751 spx->txlt_buf_dma_handle, 0, 0, 6752 DDI_DMA_SYNC_FORCPU); 6753 ASSERT(rval == DDI_SUCCESS); 6754 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 6755 bp->b_bcount); 6756 } 6757 } 6758 } else { 6759 /* 6760 * Something went wrong - analyze return 6761 */ 6762 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6763 STATE_SENT_CMD | STATE_GOT_STATUS; 6764 scsipkt->pkt_reason = CMD_INCOMPLETE; 6765 *scsipkt->pkt_scbp = STATUS_CHECK; 6766 sense = sata_arq_sense(spx); 6767 ASSERT(sense != NULL); 6768 6769 /* 6770 * SATA_PKT_DEV_ERROR is the only case where we may be able to 6771 * extract from device registers the failing LBA. 6772 */ 6773 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 6774 if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) && 6775 (scmd->satacmd_lba_mid_msb != 0 || 6776 scmd->satacmd_lba_high_msb != 0)) { 6777 /* 6778 * We have problem reporting this cmd LBA 6779 * in fixed sense data format, because of 6780 * the size of the scsi LBA fields. 6781 */ 6782 sense->es_valid = 0; 6783 } else { 6784 sata_extract_error_lba(spx, &lba); 6785 sense->es_info_1 = (lba & 0xFF000000) >> 24; 6786 sense->es_info_2 = (lba & 0xFF0000) >> 16; 6787 sense->es_info_3 = (lba & 0xFF00) >> 8; 6788 sense->es_info_4 = lba & 0xFF; 6789 } 6790 } else { 6791 /* Invalid extended sense info */ 6792 sense->es_valid = 0; 6793 } 6794 6795 switch (sata_pkt->satapkt_reason) { 6796 case SATA_PKT_PORT_ERROR: 6797 /* We may want to handle DEV GONE state as well */ 6798 /* 6799 * We have no device data. Assume no data transfered. 6800 */ 6801 sense->es_key = KEY_HARDWARE_ERROR; 6802 break; 6803 6804 case SATA_PKT_DEV_ERROR: 6805 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 6806 SATA_STATUS_ERR) { 6807 /* 6808 * determine dev error reason from error 6809 * reg content 6810 */ 6811 sata_decode_device_error(spx, sense); 6812 if (sense->es_key == KEY_MEDIUM_ERROR) { 6813 switch (scmd->satacmd_cmd_reg) { 6814 case SATAC_READ_DMA: 6815 case SATAC_READ_DMA_EXT: 6816 case SATAC_READ_DMA_QUEUED: 6817 case SATAC_READ_DMA_QUEUED_EXT: 6818 case SATAC_READ_FPDMA_QUEUED: 6819 /* Unrecovered read error */ 6820 sense->es_add_code = 6821 SD_SCSI_ASC_UNREC_READ_ERR; 6822 break; 6823 case SATAC_WRITE_DMA: 6824 case SATAC_WRITE_DMA_EXT: 6825 case SATAC_WRITE_DMA_QUEUED: 6826 case SATAC_WRITE_DMA_QUEUED_EXT: 6827 case SATAC_WRITE_FPDMA_QUEUED: 6828 /* Write error */ 6829 sense->es_add_code = 6830 SD_SCSI_ASC_WRITE_ERR; 6831 break; 6832 default: 6833 /* Internal error */ 6834 SATA_LOG_D(( 6835 spx->txlt_sata_hba_inst, 6836 CE_WARN, 6837 "sata_txlt_rw_completion :" 6838 "internal error - invalid " 6839 "command 0x%2x", 6840 scmd->satacmd_cmd_reg)); 6841 break; 6842 } 6843 } 6844 break; 6845 } 6846 /* No extended sense key - no info available */ 6847 scsipkt->pkt_reason = CMD_INCOMPLETE; 6848 break; 6849 6850 case SATA_PKT_TIMEOUT: 6851 scsipkt->pkt_reason = CMD_TIMEOUT; 6852 scsipkt->pkt_statistics |= 6853 STAT_TIMEOUT | STAT_DEV_RESET; 6854 sense->es_key = KEY_ABORTED_COMMAND; 6855 break; 6856 6857 case SATA_PKT_ABORTED: 6858 scsipkt->pkt_reason = CMD_ABORTED; 6859 scsipkt->pkt_statistics |= STAT_ABORTED; 6860 sense->es_key = KEY_ABORTED_COMMAND; 6861 break; 6862 6863 case SATA_PKT_RESET: 6864 scsipkt->pkt_reason = CMD_RESET; 6865 scsipkt->pkt_statistics |= STAT_DEV_RESET; 6866 sense->es_key = KEY_ABORTED_COMMAND; 6867 break; 6868 6869 default: 6870 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 6871 "sata_txlt_rw_completion: " 6872 "invalid packet completion reason")); 6873 scsipkt->pkt_reason = CMD_TRAN_ERR; 6874 break; 6875 } 6876 } 6877 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6878 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6879 6880 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 6881 /* scsi callback required */ 6882 scsi_hba_pkt_comp(scsipkt); 6883 } 6884 6885 6886 /* 6887 * Translate completion status of non-data commands (i.e. commands returning 6888 * no data). 6889 * pkt completion_reason is checked to determine the completion status. 6890 * Do scsi callback if necessary (FLAG_NOINTR == 0) 6891 * 6892 * Note: this function may be called also for synchronously executed 6893 * commands. 6894 * This function may be used only if scsi_pkt is non-NULL. 6895 */ 6896 6897 static void 6898 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt) 6899 { 6900 sata_pkt_txlate_t *spx = 6901 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 6902 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6903 6904 sata_set_arq_data(sata_pkt); 6905 6906 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 6907 /* scsi callback required */ 6908 scsi_hba_pkt_comp(scsipkt); 6909 } 6910 6911 /* 6912 * Completion handler for ATA Pass Through command 6913 */ 6914 static void 6915 sata_txlt_apt_completion(sata_pkt_t *sata_pkt) 6916 { 6917 sata_pkt_txlate_t *spx = 6918 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 6919 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 6920 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6921 struct buf *bp; 6922 uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0; 6923 6924 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 6925 /* Normal completion */ 6926 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6927 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 6928 scsipkt->pkt_reason = CMD_CMPLT; 6929 *scsipkt->pkt_scbp = STATUS_GOOD; 6930 6931 /* 6932 * If the command has CK_COND set 6933 */ 6934 if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) { 6935 *scsipkt->pkt_scbp = STATUS_CHECK; 6936 sata_fill_ata_return_desc(sata_pkt, 6937 KEY_RECOVERABLE_ERROR, 6938 SD_SCSI_ASC_ATP_INFO_AVAIL, 0); 6939 } 6940 6941 if (spx->txlt_tmp_buf != NULL) { 6942 /* Temporary buffer was used */ 6943 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 6944 if (bp->b_flags & B_READ) { 6945 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 6946 bp->b_bcount); 6947 } 6948 } 6949 } else { 6950 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6951 STATE_SENT_CMD | STATE_GOT_STATUS; 6952 scsipkt->pkt_reason = CMD_INCOMPLETE; 6953 *scsipkt->pkt_scbp = STATUS_CHECK; 6954 6955 /* 6956 * If DF or ERR was set, the HBA should have copied out the 6957 * status and error registers to the satacmd structure. 6958 */ 6959 if (scmd->satacmd_status_reg & SATA_STATUS_DF) { 6960 sense_key = KEY_HARDWARE_ERROR; 6961 addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE; 6962 addl_sense_qual = 0; 6963 } else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) { 6964 if (scmd->satacmd_error_reg & SATA_ERROR_NM) { 6965 sense_key = KEY_NOT_READY; 6966 addl_sense_code = 6967 SD_SCSI_ASC_MEDIUM_NOT_PRESENT; 6968 addl_sense_qual = 0; 6969 } else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) { 6970 sense_key = KEY_MEDIUM_ERROR; 6971 addl_sense_code = SD_SCSI_ASC_UNREC_READ_ERR; 6972 addl_sense_qual = 0; 6973 } else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) { 6974 sense_key = KEY_DATA_PROTECT; 6975 addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED; 6976 addl_sense_qual = 0; 6977 } else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) { 6978 sense_key = KEY_ILLEGAL_REQUEST; 6979 addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE; 6980 addl_sense_qual = 0; 6981 } else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) { 6982 sense_key = KEY_ABORTED_COMMAND; 6983 addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE; 6984 addl_sense_qual = 0; 6985 } else if (scmd->satacmd_error_reg & SATA_ERROR_MC) { 6986 sense_key = KEY_UNIT_ATTENTION; 6987 addl_sense_code = 6988 SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED; 6989 addl_sense_qual = 0; 6990 } else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) { 6991 sense_key = KEY_UNIT_ATTENTION; 6992 addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ; 6993 addl_sense_qual = 0; 6994 } else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) { 6995 sense_key = KEY_ABORTED_COMMAND; 6996 addl_sense_code = 6997 SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR; 6998 addl_sense_qual = 0; 6999 } 7000 } 7001 7002 sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code, 7003 addl_sense_qual); 7004 } 7005 7006 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 7007 /* scsi callback required */ 7008 scsi_hba_pkt_comp(scsipkt); 7009 } 7010 7011 /* 7012 * j 7013 */ 7014 static void 7015 sata_fill_ata_return_desc(sata_pkt_t *sata_pkt, uint8_t sense_key, 7016 uint8_t addl_sense_code, uint8_t addl_sense_qual) 7017 { 7018 sata_pkt_txlate_t *spx = 7019 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7020 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 7021 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7022 struct sata_apt_sense_data *apt_sd = 7023 (struct sata_apt_sense_data *)scsipkt->pkt_scbp; 7024 struct scsi_descr_sense_hdr *sds = &(apt_sd->apt_sd_hdr); 7025 struct scsi_ata_status_ret_sense_descr *ata_ret_desc = 7026 &(apt_sd->apt_sd_sense); 7027 int extend = 0; 7028 7029 if ((scsipkt->pkt_cdbp[0] == SPC3_CMD_ATA_COMMAND_PASS_THROUGH16) && 7030 (scsipkt->pkt_cdbp[2] & SATL_APT_BM_EXTEND)) 7031 extend = 1; 7032 7033 scsipkt->pkt_state |= STATE_ARQ_DONE; 7034 7035 /* update the residual count */ 7036 *(uchar_t *)&apt_sd->apt_status = STATUS_CHECK; 7037 *(uchar_t *)&apt_sd->apt_rqpkt_status = STATUS_GOOD; 7038 apt_sd->apt_rqpkt_reason = CMD_CMPLT; 7039 apt_sd->apt_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7040 STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS; 7041 apt_sd->apt_rqpkt_resid = scsipkt->pkt_scblen - 7042 sizeof (struct sata_apt_sense_data); 7043 7044 /* 7045 * Fill in the Descriptor sense header 7046 */ 7047 bzero(sds, sizeof (struct scsi_descr_sense_hdr)); 7048 sds->ds_code = CODE_FMT_DESCR_CURRENT; 7049 sds->ds_class = CLASS_EXTENDED_SENSE; 7050 sds->ds_key = sense_key & 0xf; 7051 sds->ds_add_code = addl_sense_code; 7052 sds->ds_qual_code = addl_sense_qual; 7053 sds->ds_addl_sense_length = 7054 sizeof (struct scsi_ata_status_ret_sense_descr); 7055 7056 /* 7057 * Fill in the ATA Return descriptor sense data 7058 */ 7059 bzero(ata_ret_desc, sizeof (struct scsi_ata_status_ret_sense_descr)); 7060 ata_ret_desc->ars_descr_type = DESCR_ATA_STATUS_RETURN; 7061 ata_ret_desc->ars_addl_length = 0xc; 7062 ata_ret_desc->ars_error = scmd->satacmd_error_reg; 7063 ata_ret_desc->ars_sec_count_lsb = scmd->satacmd_sec_count_lsb; 7064 ata_ret_desc->ars_lba_low_lsb = scmd->satacmd_lba_low_lsb; 7065 ata_ret_desc->ars_lba_mid_lsb = scmd->satacmd_lba_mid_lsb; 7066 ata_ret_desc->ars_lba_high_lsb = scmd->satacmd_lba_high_lsb; 7067 ata_ret_desc->ars_device = scmd->satacmd_device_reg; 7068 ata_ret_desc->ars_status = scmd->satacmd_status_reg; 7069 7070 if (extend == 1) { 7071 ata_ret_desc->ars_extend = 1; 7072 ata_ret_desc->ars_sec_count_msb = scmd->satacmd_sec_count_msb; 7073 ata_ret_desc->ars_lba_low_msb = scmd->satacmd_lba_low_msb; 7074 ata_ret_desc->ars_lba_mid_msb = scmd->satacmd_lba_mid_msb; 7075 ata_ret_desc->ars_lba_high_msb = scmd->satacmd_lba_high_msb; 7076 } else { 7077 ata_ret_desc->ars_extend = 0; 7078 ata_ret_desc->ars_sec_count_msb = 0; 7079 ata_ret_desc->ars_lba_low_msb = 0; 7080 ata_ret_desc->ars_lba_mid_msb = 0; 7081 ata_ret_desc->ars_lba_high_msb = 0; 7082 } 7083 } 7084 7085 static void 7086 sata_set_arq_data(sata_pkt_t *sata_pkt) 7087 { 7088 sata_pkt_txlate_t *spx = 7089 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7090 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7091 struct scsi_extended_sense *sense; 7092 7093 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7094 STATE_SENT_CMD | STATE_GOT_STATUS; 7095 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 7096 /* Normal completion */ 7097 scsipkt->pkt_reason = CMD_CMPLT; 7098 *scsipkt->pkt_scbp = STATUS_GOOD; 7099 } else { 7100 /* Something went wrong */ 7101 scsipkt->pkt_reason = CMD_INCOMPLETE; 7102 *scsipkt->pkt_scbp = STATUS_CHECK; 7103 sense = sata_arq_sense(spx); 7104 switch (sata_pkt->satapkt_reason) { 7105 case SATA_PKT_PORT_ERROR: 7106 /* 7107 * We have no device data. Assume no data transfered. 7108 */ 7109 sense->es_key = KEY_HARDWARE_ERROR; 7110 break; 7111 7112 case SATA_PKT_DEV_ERROR: 7113 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 7114 SATA_STATUS_ERR) { 7115 /* 7116 * determine dev error reason from error 7117 * reg content 7118 */ 7119 sata_decode_device_error(spx, sense); 7120 break; 7121 } 7122 /* No extended sense key - no info available */ 7123 break; 7124 7125 case SATA_PKT_TIMEOUT: 7126 scsipkt->pkt_reason = CMD_TIMEOUT; 7127 scsipkt->pkt_statistics |= 7128 STAT_TIMEOUT | STAT_DEV_RESET; 7129 /* No extended sense key ? */ 7130 break; 7131 7132 case SATA_PKT_ABORTED: 7133 scsipkt->pkt_reason = CMD_ABORTED; 7134 scsipkt->pkt_statistics |= STAT_ABORTED; 7135 /* No extended sense key ? */ 7136 break; 7137 7138 case SATA_PKT_RESET: 7139 /* pkt aborted by an explicit reset from a host */ 7140 scsipkt->pkt_reason = CMD_RESET; 7141 scsipkt->pkt_statistics |= STAT_DEV_RESET; 7142 break; 7143 7144 default: 7145 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 7146 "sata_txlt_nodata_cmd_completion: " 7147 "invalid packet completion reason %d", 7148 sata_pkt->satapkt_reason)); 7149 scsipkt->pkt_reason = CMD_TRAN_ERR; 7150 break; 7151 } 7152 7153 } 7154 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 7155 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 7156 } 7157 7158 7159 /* 7160 * Build Mode sense R/W recovery page 7161 * NOT IMPLEMENTED 7162 */ 7163 7164 static int 7165 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 7166 { 7167 #ifndef __lock_lint 7168 _NOTE(ARGUNUSED(sdinfo)) 7169 _NOTE(ARGUNUSED(pcntrl)) 7170 _NOTE(ARGUNUSED(buf)) 7171 #endif 7172 return (0); 7173 } 7174 7175 /* 7176 * Build Mode sense caching page - scsi-3 implementation. 7177 * Page length distinguishes previous format from scsi-3 format. 7178 * buf must have space for 0x12 bytes. 7179 * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable. 7180 * 7181 */ 7182 static int 7183 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 7184 { 7185 struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf; 7186 sata_id_t *sata_id = &sdinfo->satadrv_id; 7187 7188 /* 7189 * Most of the fields are set to 0, being not supported and/or disabled 7190 */ 7191 bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3); 7192 7193 /* Saved paramters not supported */ 7194 if (pcntrl == 3) 7195 return (0); 7196 if (pcntrl == 0 || pcntrl == 2) { 7197 /* 7198 * For now treat current and default parameters as same 7199 * That may have to change, if target driver will complain 7200 */ 7201 page->mode_page.code = MODEPAGE_CACHING; /* PS = 0 */ 7202 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 7203 7204 if (SATA_READ_AHEAD_SUPPORTED(*sata_id) && 7205 !SATA_READ_AHEAD_ENABLED(*sata_id)) { 7206 page->dra = 1; /* Read Ahead disabled */ 7207 page->rcd = 1; /* Read Cache disabled */ 7208 } 7209 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) && 7210 SATA_WRITE_CACHE_ENABLED(*sata_id)) 7211 page->wce = 1; /* Write Cache enabled */ 7212 } else { 7213 /* Changeable parameters */ 7214 page->mode_page.code = MODEPAGE_CACHING; 7215 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 7216 if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) { 7217 page->dra = 1; 7218 page->rcd = 1; 7219 } 7220 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) 7221 page->wce = 1; 7222 } 7223 return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 7224 sizeof (struct mode_page)); 7225 } 7226 7227 /* 7228 * Build Mode sense exception cntrl page 7229 */ 7230 static int 7231 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 7232 { 7233 struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf; 7234 sata_id_t *sata_id = &sdinfo->satadrv_id; 7235 7236 /* 7237 * Most of the fields are set to 0, being not supported and/or disabled 7238 */ 7239 bzero(buf, PAGELENGTH_INFO_EXCPT); 7240 7241 page->mode_page.code = MODEPAGE_INFO_EXCPT; 7242 page->mode_page.length = PAGELENGTH_INFO_EXCPT; 7243 7244 /* Indicate that this is page is saveable */ 7245 page->mode_page.ps = 1; 7246 7247 /* 7248 * We will return the same data for default, current and saved page. 7249 * The only changeable bit is dexcpt and that bit is required 7250 * by the ATA specification to be preserved across power cycles. 7251 */ 7252 if (pcntrl != 1) { 7253 page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED); 7254 page->mrie = MRIE_ONLY_ON_REQUEST; 7255 } 7256 else 7257 page->dexcpt = 1; /* Only changeable parameter */ 7258 7259 return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)); 7260 } 7261 7262 7263 static int 7264 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 7265 { 7266 struct mode_acoustic_management *page = 7267 (struct mode_acoustic_management *)buf; 7268 sata_id_t *sata_id = &sdinfo->satadrv_id; 7269 7270 /* 7271 * Most of the fields are set to 0, being not supported and/or disabled 7272 */ 7273 bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT); 7274 7275 switch (pcntrl) { 7276 case P_CNTRL_DEFAULT: 7277 /* default paramters not supported */ 7278 return (0); 7279 7280 case P_CNTRL_CURRENT: 7281 case P_CNTRL_SAVED: 7282 /* Saved and current are supported and are identical */ 7283 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG; 7284 page->mode_page.length = 7285 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT; 7286 page->mode_page.ps = 1; 7287 7288 /* Word 83 indicates if feature is supported */ 7289 /* If feature is not supported */ 7290 if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) { 7291 page->acoustic_manag_enable = 7292 ACOUSTIC_DISABLED; 7293 } else { 7294 page->acoustic_manag_enable = 7295 ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT) 7296 != 0); 7297 /* Word 94 inidicates the value */ 7298 #ifdef _LITTLE_ENDIAN 7299 page->acoustic_manag_level = 7300 (uchar_t)sata_id->ai_acoustic; 7301 page->vendor_recommended_value = 7302 sata_id->ai_acoustic >> 8; 7303 #else 7304 page->acoustic_manag_level = 7305 sata_id->ai_acoustic >> 8; 7306 page->vendor_recommended_value = 7307 (uchar_t)sata_id->ai_acoustic; 7308 #endif 7309 } 7310 break; 7311 7312 case P_CNTRL_CHANGEABLE: 7313 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG; 7314 page->mode_page.length = 7315 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT; 7316 page->mode_page.ps = 1; 7317 7318 /* Word 83 indicates if the feature is supported */ 7319 if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) { 7320 page->acoustic_manag_enable = 7321 ACOUSTIC_ENABLED; 7322 page->acoustic_manag_level = 0xff; 7323 } 7324 break; 7325 } 7326 return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 7327 sizeof (struct mode_page)); 7328 } 7329 7330 7331 /* 7332 * Build Mode sense power condition page. 7333 */ 7334 static int 7335 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 7336 { 7337 struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf; 7338 sata_id_t *sata_id = &sdinfo->satadrv_id; 7339 7340 /* 7341 * Most of the fields are set to 0, being not supported and/or disabled 7342 * power condition page length was 0x0a 7343 */ 7344 bzero(buf, sizeof (struct mode_info_power_cond)); 7345 7346 if (pcntrl == P_CNTRL_DEFAULT) { 7347 /* default paramters not supported */ 7348 return (0); 7349 } 7350 7351 page->mode_page.code = MODEPAGE_POWER_COND; 7352 page->mode_page.length = sizeof (struct mode_info_power_cond); 7353 7354 if (sata_id->ai_cap && SATA_STANDBYTIMER) { 7355 page->standby = 1; 7356 bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer, 7357 sizeof (uchar_t) * 4); 7358 } 7359 7360 return (sizeof (struct mode_info_power_cond)); 7361 } 7362 7363 /* 7364 * Process mode select caching page 8 (scsi3 format only). 7365 * Read Ahead (same as read cache) and Write Cache may be turned on and off 7366 * if these features are supported by the device. If these features are not 7367 * supported, the command will be terminated with STATUS_CHECK. 7368 * This function fails only if the SET FEATURE command sent to 7369 * the device fails. The page format is not varified, assuming that the 7370 * target driver operates correctly - if parameters length is too short, 7371 * we just drop the page. 7372 * Two command may be sent if both Read Cache/Read Ahead and Write Cache 7373 * setting have to be changed. 7374 * SET FEATURE command is executed synchronously, i.e. we wait here until 7375 * it is completed, regardless of the scsi pkt directives. 7376 * 7377 * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e. 7378 * changing DRA will change RCD. 7379 * 7380 * More than one SATA command may be executed to perform operations specified 7381 * by mode select pages. The first error terminates further execution. 7382 * Operations performed successully are not backed-up in such case. 7383 * 7384 * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 7385 * If operation resulted in changing device setup, dmod flag should be set to 7386 * one (1). If parameters were not changed, dmod flag should be set to 0. 7387 * Upon return, if operation required sending command to the device, the rval 7388 * should be set to the value returned by sata_hba_start. If operation 7389 * did not require device access, rval should be set to TRAN_ACCEPT. 7390 * The pagelen should be set to the length of the page. 7391 * 7392 * This function has to be called with a port mutex held. 7393 * 7394 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 7395 */ 7396 int 7397 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page, 7398 int parmlen, int *pagelen, int *rval, int *dmod) 7399 { 7400 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7401 sata_drive_info_t *sdinfo; 7402 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 7403 sata_id_t *sata_id; 7404 struct scsi_extended_sense *sense; 7405 int wce, dra; /* Current settings */ 7406 7407 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 7408 &spx->txlt_sata_pkt->satapkt_device); 7409 sata_id = &sdinfo->satadrv_id; 7410 *dmod = 0; 7411 7412 /* Verify parameters length. If too short, drop it */ 7413 if ((PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 7414 sizeof (struct mode_page)) > parmlen) { 7415 *scsipkt->pkt_scbp = STATUS_CHECK; 7416 sense = sata_arq_sense(spx); 7417 sense->es_key = KEY_ILLEGAL_REQUEST; 7418 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 7419 *pagelen = parmlen; 7420 *rval = TRAN_ACCEPT; 7421 return (SATA_FAILURE); 7422 } 7423 7424 *pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page); 7425 7426 /* Current setting of Read Ahead (and Read Cache) */ 7427 if (SATA_READ_AHEAD_ENABLED(*sata_id)) 7428 dra = 0; /* 0 == not disabled */ 7429 else 7430 dra = 1; 7431 /* Current setting of Write Cache */ 7432 if (SATA_WRITE_CACHE_ENABLED(*sata_id)) 7433 wce = 1; 7434 else 7435 wce = 0; 7436 7437 if (page->dra == dra && page->wce == wce && page->rcd == dra) { 7438 /* nothing to do */ 7439 *rval = TRAN_ACCEPT; 7440 return (SATA_SUCCESS); 7441 } 7442 7443 /* 7444 * Need to flip some setting 7445 * Set-up Internal SET FEATURES command(s) 7446 */ 7447 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 7448 scmd->satacmd_addr_type = 0; 7449 scmd->satacmd_device_reg = 0; 7450 scmd->satacmd_status_reg = 0; 7451 scmd->satacmd_error_reg = 0; 7452 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 7453 if (page->dra != dra || page->rcd != dra) { 7454 if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) { 7455 /* Need to flip read ahead setting */ 7456 if (dra == 0) 7457 /* Disable read ahead / read cache */ 7458 scmd->satacmd_features_reg = 7459 SATAC_SF_DISABLE_READ_AHEAD; 7460 else 7461 /* Enable read ahead / read cache */ 7462 scmd->satacmd_features_reg = 7463 SATAC_SF_ENABLE_READ_AHEAD; 7464 7465 /* Transfer command to HBA */ 7466 if (sata_hba_start(spx, rval) != 0) 7467 /* 7468 * Pkt not accepted for execution. 7469 */ 7470 return (SATA_FAILURE); 7471 7472 *dmod = 1; 7473 7474 /* Now process return */ 7475 if (spx->txlt_sata_pkt->satapkt_reason != 7476 SATA_PKT_COMPLETED) { 7477 goto failure; /* Terminate */ 7478 } 7479 } else { 7480 *scsipkt->pkt_scbp = STATUS_CHECK; 7481 sense = sata_arq_sense(spx); 7482 sense->es_key = KEY_ILLEGAL_REQUEST; 7483 sense->es_add_code = 7484 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 7485 *pagelen = parmlen; 7486 *rval = TRAN_ACCEPT; 7487 return (SATA_FAILURE); 7488 } 7489 } 7490 7491 /* Note that the packet is not removed, so it could be re-used */ 7492 if (page->wce != wce) { 7493 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) { 7494 /* Need to flip Write Cache setting */ 7495 if (page->wce == 1) 7496 /* Enable write cache */ 7497 scmd->satacmd_features_reg = 7498 SATAC_SF_ENABLE_WRITE_CACHE; 7499 else 7500 /* Disable write cache */ 7501 scmd->satacmd_features_reg = 7502 SATAC_SF_DISABLE_WRITE_CACHE; 7503 7504 /* Transfer command to HBA */ 7505 if (sata_hba_start(spx, rval) != 0) 7506 /* 7507 * Pkt not accepted for execution. 7508 */ 7509 return (SATA_FAILURE); 7510 7511 *dmod = 1; 7512 7513 /* Now process return */ 7514 if (spx->txlt_sata_pkt->satapkt_reason != 7515 SATA_PKT_COMPLETED) { 7516 goto failure; 7517 } 7518 } else { 7519 *scsipkt->pkt_scbp = STATUS_CHECK; 7520 sense = sata_arq_sense(spx); 7521 sense->es_key = KEY_ILLEGAL_REQUEST; 7522 sense->es_add_code = 7523 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 7524 *pagelen = parmlen; 7525 *rval = TRAN_ACCEPT; 7526 return (SATA_FAILURE); 7527 } 7528 } 7529 return (SATA_SUCCESS); 7530 7531 failure: 7532 sata_xlate_errors(spx); 7533 7534 return (SATA_FAILURE); 7535 } 7536 7537 /* 7538 * Process mode select informational exceptions control page 0x1c 7539 * 7540 * The only changeable bit is dexcpt (disable exceptions). 7541 * MRIE (method of reporting informational exceptions) must be 7542 * "only on request". 7543 * This page applies to informational exceptions that report 7544 * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh 7545 * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_). 7546 * Informational exception conditions occur as the result of background scan 7547 * errors, background self-test errors, or vendor specific events within a 7548 * logical unit. An informational exception condition may occur asynchronous 7549 * to any commands. 7550 * 7551 * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 7552 * If operation resulted in changing device setup, dmod flag should be set to 7553 * one (1). If parameters were not changed, dmod flag should be set to 0. 7554 * Upon return, if operation required sending command to the device, the rval 7555 * should be set to the value returned by sata_hba_start. If operation 7556 * did not require device access, rval should be set to TRAN_ACCEPT. 7557 * The pagelen should be set to the length of the page. 7558 * 7559 * This function has to be called with a port mutex held. 7560 * 7561 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 7562 * 7563 * Cannot be called in the interrupt context. 7564 */ 7565 static int 7566 sata_mode_select_page_1c( 7567 sata_pkt_txlate_t *spx, 7568 struct mode_info_excpt_page *page, 7569 int parmlen, 7570 int *pagelen, 7571 int *rval, 7572 int *dmod) 7573 { 7574 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7575 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 7576 sata_drive_info_t *sdinfo; 7577 sata_id_t *sata_id; 7578 struct scsi_extended_sense *sense; 7579 7580 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 7581 &spx->txlt_sata_pkt->satapkt_device); 7582 sata_id = &sdinfo->satadrv_id; 7583 7584 *dmod = 0; 7585 7586 /* Verify parameters length. If too short, drop it */ 7587 if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) > parmlen) || 7588 page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) { 7589 *scsipkt->pkt_scbp = STATUS_CHECK; 7590 sense = sata_arq_sense(spx); 7591 sense->es_key = KEY_ILLEGAL_REQUEST; 7592 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 7593 *pagelen = parmlen; 7594 *rval = TRAN_ACCEPT; 7595 return (SATA_FAILURE); 7596 } 7597 7598 *pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page); 7599 7600 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 7601 *scsipkt->pkt_scbp = STATUS_CHECK; 7602 sense = sata_arq_sense(spx); 7603 sense->es_key = KEY_ILLEGAL_REQUEST; 7604 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 7605 *pagelen = parmlen; 7606 *rval = TRAN_ACCEPT; 7607 return (SATA_FAILURE); 7608 } 7609 7610 /* If already in the state requested, we are done */ 7611 if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 7612 /* nothing to do */ 7613 *rval = TRAN_ACCEPT; 7614 return (SATA_SUCCESS); 7615 } 7616 7617 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 7618 7619 /* Build SMART_ENABLE or SMART_DISABLE command */ 7620 scmd->satacmd_addr_type = 0; /* N/A */ 7621 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 7622 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 7623 scmd->satacmd_features_reg = page->dexcpt ? 7624 SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS; 7625 scmd->satacmd_device_reg = 0; /* Always device 0 */ 7626 scmd->satacmd_cmd_reg = SATAC_SMART; 7627 7628 /* Transfer command to HBA */ 7629 if (sata_hba_start(spx, rval) != 0) 7630 /* 7631 * Pkt not accepted for execution. 7632 */ 7633 return (SATA_FAILURE); 7634 7635 *dmod = 1; /* At least may have been modified */ 7636 7637 /* Now process return */ 7638 if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) 7639 return (SATA_SUCCESS); 7640 7641 /* Packet did not complete successfully */ 7642 sata_xlate_errors(spx); 7643 7644 return (SATA_FAILURE); 7645 } 7646 7647 /* 7648 * Process mode select acoustic management control page 0x30 7649 * 7650 * 7651 * This function has to be called with a port mutex held. 7652 * 7653 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 7654 * 7655 * Cannot be called in the interrupt context. 7656 */ 7657 int 7658 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct 7659 mode_acoustic_management *page, int parmlen, int *pagelen, 7660 int *rval, int *dmod) 7661 { 7662 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7663 sata_drive_info_t *sdinfo; 7664 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 7665 sata_id_t *sata_id; 7666 struct scsi_extended_sense *sense; 7667 7668 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 7669 &spx->txlt_sata_pkt->satapkt_device); 7670 sata_id = &sdinfo->satadrv_id; 7671 *dmod = 0; 7672 7673 /* If parmlen is too short or the feature is not supported, drop it */ 7674 if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 7675 sizeof (struct mode_page)) > parmlen) || 7676 (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) { 7677 *scsipkt->pkt_scbp = STATUS_CHECK; 7678 sense = sata_arq_sense(spx); 7679 sense->es_key = KEY_ILLEGAL_REQUEST; 7680 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 7681 *pagelen = parmlen; 7682 *rval = TRAN_ACCEPT; 7683 return (SATA_FAILURE); 7684 } 7685 7686 *pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 7687 sizeof (struct mode_page); 7688 7689 /* 7690 * We can enable and disable acoustice management and 7691 * set the acoustic management level. 7692 */ 7693 7694 /* 7695 * Set-up Internal SET FEATURES command(s) 7696 */ 7697 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 7698 scmd->satacmd_addr_type = 0; 7699 scmd->satacmd_device_reg = 0; 7700 scmd->satacmd_status_reg = 0; 7701 scmd->satacmd_error_reg = 0; 7702 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 7703 if (page->acoustic_manag_enable) { 7704 scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC; 7705 scmd->satacmd_sec_count_lsb = page->acoustic_manag_level; 7706 } else { /* disabling acoustic management */ 7707 scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC; 7708 } 7709 7710 /* Transfer command to HBA */ 7711 if (sata_hba_start(spx, rval) != 0) 7712 /* 7713 * Pkt not accepted for execution. 7714 */ 7715 return (SATA_FAILURE); 7716 7717 /* Now process return */ 7718 if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) { 7719 sata_xlate_errors(spx); 7720 return (SATA_FAILURE); 7721 } 7722 7723 *dmod = 1; 7724 7725 return (SATA_SUCCESS); 7726 } 7727 7728 /* 7729 * Process mode select power condition page 0x1a 7730 * 7731 * This function has to be called with a port mutex held. 7732 * 7733 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 7734 * 7735 * Cannot be called in the interrupt context. 7736 */ 7737 int 7738 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct 7739 mode_info_power_cond *page, int parmlen, int *pagelen, 7740 int *rval, int *dmod) 7741 { 7742 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7743 sata_drive_info_t *sdinfo; 7744 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 7745 sata_id_t *sata_id; 7746 struct scsi_extended_sense *sense; 7747 uint8_t ata_count; 7748 int i, len; 7749 7750 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 7751 &spx->txlt_sata_pkt->satapkt_device); 7752 sata_id = &sdinfo->satadrv_id; 7753 *dmod = 0; 7754 7755 len = sizeof (struct mode_info_power_cond); 7756 len += sizeof (struct mode_page); 7757 7758 /* If parmlen is too short or the feature is not supported, drop it */ 7759 if ((len < parmlen) || (page->idle == 1) || 7760 (!(sata_id->ai_cap && SATA_STANDBYTIMER) && page->standby == 1)) { 7761 *scsipkt->pkt_scbp = STATUS_CHECK; 7762 sense = sata_arq_sense(spx); 7763 sense->es_key = KEY_ILLEGAL_REQUEST; 7764 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 7765 *pagelen = parmlen; 7766 *rval = TRAN_ACCEPT; 7767 return (SATA_FAILURE); 7768 } 7769 7770 *pagelen = len; 7771 7772 /* 7773 * Set-up Internal STANDBY command(s) 7774 */ 7775 if (page->standby == 0) 7776 goto out; 7777 7778 ata_count = sata_get_standby_timer(page->standby_cond_timer); 7779 7780 scmd->satacmd_addr_type = 0; 7781 scmd->satacmd_sec_count_lsb = ata_count; 7782 scmd->satacmd_lba_low_lsb = 0; 7783 scmd->satacmd_lba_mid_lsb = 0; 7784 scmd->satacmd_lba_high_lsb = 0; 7785 scmd->satacmd_features_reg = 0; 7786 scmd->satacmd_device_reg = 0; 7787 scmd->satacmd_status_reg = 0; 7788 scmd->satacmd_cmd_reg = SATAC_STANDBY; 7789 scmd->satacmd_flags.sata_special_regs = B_TRUE; 7790 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 7791 7792 /* Transfer command to HBA */ 7793 if (sata_hba_start(spx, rval) != 0) { 7794 return (SATA_FAILURE); 7795 } else { 7796 if ((scmd->satacmd_error_reg != 0) || 7797 (spx->txlt_sata_pkt->satapkt_reason != 7798 SATA_PKT_COMPLETED)) { 7799 sata_xlate_errors(spx); 7800 return (SATA_FAILURE); 7801 } 7802 } 7803 7804 for (i = 0; i < 4; i++) { 7805 sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i]; 7806 } 7807 out: 7808 *dmod = 1; 7809 return (SATA_SUCCESS); 7810 } 7811 7812 /* 7813 * sata_build_lsense_page0() is used to create the 7814 * SCSI LOG SENSE page 0 (supported log pages) 7815 * 7816 * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e 7817 * (supported log pages, self-test results, informational exceptions 7818 * Sun vendor specific ATA SMART data, and start stop cycle counter). 7819 * 7820 * Takes a sata_drive_info t * and the address of a buffer 7821 * in which to create the page information. 7822 * 7823 * Returns the number of bytes valid in the buffer. 7824 */ 7825 static int 7826 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf) 7827 { 7828 struct log_parameter *lpp = (struct log_parameter *)buf; 7829 uint8_t *page_ptr = (uint8_t *)lpp->param_values; 7830 int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */ 7831 sata_id_t *sata_id = &sdinfo->satadrv_id; 7832 7833 lpp->param_code[0] = 0; 7834 lpp->param_code[1] = 0; 7835 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 7836 *page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES; 7837 7838 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 7839 if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) { 7840 *page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS; 7841 ++num_pages_supported; 7842 } 7843 *page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS; 7844 ++num_pages_supported; 7845 *page_ptr++ = PAGE_CODE_SMART_READ_DATA; 7846 ++num_pages_supported; 7847 *page_ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER; 7848 ++num_pages_supported; 7849 } 7850 7851 lpp->param_len = num_pages_supported; 7852 7853 return ((&lpp->param_values[0] - (uint8_t *)lpp) + 7854 num_pages_supported); 7855 } 7856 7857 /* 7858 * sata_build_lsense_page_10() is used to create the 7859 * SCSI LOG SENSE page 0x10 (self-test results) 7860 * 7861 * Takes a sata_drive_info t * and the address of a buffer 7862 * in which to create the page information as well as a sata_hba_inst_t *. 7863 * 7864 * Returns the number of bytes valid in the buffer. 7865 * 7866 * Note: Self test and SMART data is accessible in device log pages. 7867 * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors 7868 * of data can be transferred by a single command), or by the General Purpose 7869 * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors 7870 * - approximately 33MB - can be transferred by a single command. 7871 * The SCT Command response (either error or command) is the same for both 7872 * the SMART and GPL methods of issuing commands. 7873 * This function uses READ LOG EXT command when drive supports LBA48, and 7874 * SMART READ command otherwise. 7875 * 7876 * Since above commands are executed in a synchronous mode, this function 7877 * should not be called in an interrupt context. 7878 */ 7879 static int 7880 sata_build_lsense_page_10( 7881 sata_drive_info_t *sdinfo, 7882 uint8_t *buf, 7883 sata_hba_inst_t *sata_hba_inst) 7884 { 7885 struct log_parameter *lpp = (struct log_parameter *)buf; 7886 int rval; 7887 7888 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 7889 struct smart_ext_selftest_log *ext_selftest_log; 7890 7891 ext_selftest_log = kmem_zalloc( 7892 sizeof (struct smart_ext_selftest_log), KM_SLEEP); 7893 7894 rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo, 7895 ext_selftest_log, 0); 7896 if (rval == 0) { 7897 int index, start_index; 7898 struct smart_ext_selftest_log_entry *entry; 7899 static const struct smart_ext_selftest_log_entry empty = 7900 {0}; 7901 uint16_t block_num; 7902 int count; 7903 boolean_t only_one_block = B_FALSE; 7904 7905 index = ext_selftest_log-> 7906 smart_ext_selftest_log_index[0]; 7907 index |= ext_selftest_log-> 7908 smart_ext_selftest_log_index[1] << 8; 7909 if (index == 0) 7910 goto out; 7911 7912 --index; /* Correct for 0 origin */ 7913 start_index = index; /* remember where we started */ 7914 block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 7915 if (block_num != 0) { 7916 rval = sata_ext_smart_selftest_read_log( 7917 sata_hba_inst, sdinfo, ext_selftest_log, 7918 block_num); 7919 if (rval != 0) 7920 goto out; 7921 } 7922 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 7923 entry = 7924 &ext_selftest_log-> 7925 smart_ext_selftest_log_entries[index]; 7926 7927 for (count = 1; 7928 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 7929 ++count) { 7930 uint8_t status; 7931 uint8_t code; 7932 uint8_t sense_key; 7933 uint8_t add_sense_code; 7934 uint8_t add_sense_code_qual; 7935 7936 /* If this is an unused entry, we are done */ 7937 if (bcmp(entry, &empty, sizeof (empty)) == 0) { 7938 /* Broken firmware on some disks */ 7939 if (index + 1 == 7940 ENTRIES_PER_EXT_SELFTEST_LOG_BLK) { 7941 --entry; 7942 --index; 7943 if (bcmp(entry, &empty, 7944 sizeof (empty)) == 0) 7945 goto out; 7946 } else 7947 goto out; 7948 } 7949 7950 if (only_one_block && 7951 start_index == index) 7952 goto out; 7953 7954 lpp->param_code[0] = 0; 7955 lpp->param_code[1] = count; 7956 lpp->param_ctrl_flags = 7957 LOG_CTRL_LP | LOG_CTRL_LBIN; 7958 lpp->param_len = 7959 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 7960 7961 status = entry->smart_ext_selftest_log_status; 7962 status >>= 4; 7963 switch (status) { 7964 case 0: 7965 default: 7966 sense_key = KEY_NO_SENSE; 7967 add_sense_code = 7968 SD_SCSI_ASC_NO_ADD_SENSE; 7969 add_sense_code_qual = 0; 7970 break; 7971 case 1: 7972 sense_key = KEY_ABORTED_COMMAND; 7973 add_sense_code = 7974 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7975 add_sense_code_qual = SCSI_COMPONENT_81; 7976 break; 7977 case 2: 7978 sense_key = KEY_ABORTED_COMMAND; 7979 add_sense_code = 7980 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7981 add_sense_code_qual = SCSI_COMPONENT_82; 7982 break; 7983 case 3: 7984 sense_key = KEY_ABORTED_COMMAND; 7985 add_sense_code = 7986 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7987 add_sense_code_qual = SCSI_COMPONENT_83; 7988 break; 7989 case 4: 7990 sense_key = KEY_HARDWARE_ERROR; 7991 add_sense_code = 7992 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7993 add_sense_code_qual = SCSI_COMPONENT_84; 7994 break; 7995 case 5: 7996 sense_key = KEY_HARDWARE_ERROR; 7997 add_sense_code = 7998 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7999 add_sense_code_qual = SCSI_COMPONENT_85; 8000 break; 8001 case 6: 8002 sense_key = KEY_HARDWARE_ERROR; 8003 add_sense_code = 8004 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8005 add_sense_code_qual = SCSI_COMPONENT_86; 8006 break; 8007 case 7: 8008 sense_key = KEY_MEDIUM_ERROR; 8009 add_sense_code = 8010 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8011 add_sense_code_qual = SCSI_COMPONENT_87; 8012 break; 8013 case 8: 8014 sense_key = KEY_HARDWARE_ERROR; 8015 add_sense_code = 8016 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8017 add_sense_code_qual = SCSI_COMPONENT_88; 8018 break; 8019 } 8020 code = 0; /* unspecified */ 8021 status |= (code << 4); 8022 lpp->param_values[0] = status; 8023 lpp->param_values[1] = 0; /* unspecified */ 8024 lpp->param_values[2] = entry-> 8025 smart_ext_selftest_log_timestamp[1]; 8026 lpp->param_values[3] = entry-> 8027 smart_ext_selftest_log_timestamp[0]; 8028 if (status != 0) { 8029 lpp->param_values[4] = 0; 8030 lpp->param_values[5] = 0; 8031 lpp->param_values[6] = entry-> 8032 smart_ext_selftest_log_failing_lba 8033 [5]; 8034 lpp->param_values[7] = entry-> 8035 smart_ext_selftest_log_failing_lba 8036 [4]; 8037 lpp->param_values[8] = entry-> 8038 smart_ext_selftest_log_failing_lba 8039 [3]; 8040 lpp->param_values[9] = entry-> 8041 smart_ext_selftest_log_failing_lba 8042 [2]; 8043 lpp->param_values[10] = entry-> 8044 smart_ext_selftest_log_failing_lba 8045 [1]; 8046 lpp->param_values[11] = entry-> 8047 smart_ext_selftest_log_failing_lba 8048 [0]; 8049 } else { /* No bad block address */ 8050 lpp->param_values[4] = 0xff; 8051 lpp->param_values[5] = 0xff; 8052 lpp->param_values[6] = 0xff; 8053 lpp->param_values[7] = 0xff; 8054 lpp->param_values[8] = 0xff; 8055 lpp->param_values[9] = 0xff; 8056 lpp->param_values[10] = 0xff; 8057 lpp->param_values[11] = 0xff; 8058 } 8059 8060 lpp->param_values[12] = sense_key; 8061 lpp->param_values[13] = add_sense_code; 8062 lpp->param_values[14] = add_sense_code_qual; 8063 lpp->param_values[15] = 0; /* undefined */ 8064 8065 lpp = (struct log_parameter *) 8066 (((uint8_t *)lpp) + 8067 SCSI_LOG_PARAM_HDR_LEN + 8068 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 8069 8070 --index; /* Back up to previous entry */ 8071 if (index < 0) { 8072 if (block_num > 0) { 8073 --block_num; 8074 } else { 8075 struct read_log_ext_directory 8076 logdir; 8077 8078 rval = 8079 sata_read_log_ext_directory( 8080 sata_hba_inst, sdinfo, 8081 &logdir); 8082 if (rval == -1) 8083 goto out; 8084 if ((logdir.read_log_ext_vers 8085 [0] == 0) && 8086 (logdir.read_log_ext_vers 8087 [1] == 0)) 8088 goto out; 8089 block_num = 8090 logdir.read_log_ext_nblks 8091 [EXT_SMART_SELFTEST_LOG_PAGE 8092 - 1][0]; 8093 block_num |= logdir. 8094 read_log_ext_nblks 8095 [EXT_SMART_SELFTEST_LOG_PAGE 8096 - 1][1] << 8; 8097 --block_num; 8098 only_one_block = 8099 (block_num == 0); 8100 } 8101 rval = sata_ext_smart_selftest_read_log( 8102 sata_hba_inst, sdinfo, 8103 ext_selftest_log, block_num); 8104 if (rval != 0) 8105 goto out; 8106 8107 index = 8108 ENTRIES_PER_EXT_SELFTEST_LOG_BLK - 8109 1; 8110 } 8111 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 8112 entry = &ext_selftest_log-> 8113 smart_ext_selftest_log_entries[index]; 8114 } 8115 } 8116 out: 8117 kmem_free(ext_selftest_log, 8118 sizeof (struct smart_ext_selftest_log)); 8119 } else { 8120 struct smart_selftest_log *selftest_log; 8121 8122 selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log), 8123 KM_SLEEP); 8124 8125 rval = sata_smart_selftest_log(sata_hba_inst, sdinfo, 8126 selftest_log); 8127 8128 if (rval == 0) { 8129 int index; 8130 int count; 8131 struct smart_selftest_log_entry *entry; 8132 static const struct smart_selftest_log_entry empty = 8133 { 0 }; 8134 8135 index = selftest_log->smart_selftest_log_index; 8136 if (index == 0) 8137 goto done; 8138 --index; /* Correct for 0 origin */ 8139 entry = &selftest_log-> 8140 smart_selftest_log_entries[index]; 8141 for (count = 1; 8142 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 8143 ++count) { 8144 uint8_t status; 8145 uint8_t code; 8146 uint8_t sense_key; 8147 uint8_t add_sense_code; 8148 uint8_t add_sense_code_qual; 8149 8150 if (bcmp(entry, &empty, sizeof (empty)) == 0) 8151 goto done; 8152 8153 lpp->param_code[0] = 0; 8154 lpp->param_code[1] = count; 8155 lpp->param_ctrl_flags = 8156 LOG_CTRL_LP | LOG_CTRL_LBIN; 8157 lpp->param_len = 8158 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 8159 8160 status = entry->smart_selftest_log_status; 8161 status >>= 4; 8162 switch (status) { 8163 case 0: 8164 default: 8165 sense_key = KEY_NO_SENSE; 8166 add_sense_code = 8167 SD_SCSI_ASC_NO_ADD_SENSE; 8168 break; 8169 case 1: 8170 sense_key = KEY_ABORTED_COMMAND; 8171 add_sense_code = 8172 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8173 add_sense_code_qual = SCSI_COMPONENT_81; 8174 break; 8175 case 2: 8176 sense_key = KEY_ABORTED_COMMAND; 8177 add_sense_code = 8178 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8179 add_sense_code_qual = SCSI_COMPONENT_82; 8180 break; 8181 case 3: 8182 sense_key = KEY_ABORTED_COMMAND; 8183 add_sense_code = 8184 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8185 add_sense_code_qual = SCSI_COMPONENT_83; 8186 break; 8187 case 4: 8188 sense_key = KEY_HARDWARE_ERROR; 8189 add_sense_code = 8190 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8191 add_sense_code_qual = SCSI_COMPONENT_84; 8192 break; 8193 case 5: 8194 sense_key = KEY_HARDWARE_ERROR; 8195 add_sense_code = 8196 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8197 add_sense_code_qual = SCSI_COMPONENT_85; 8198 break; 8199 case 6: 8200 sense_key = KEY_HARDWARE_ERROR; 8201 add_sense_code = 8202 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8203 add_sense_code_qual = SCSI_COMPONENT_86; 8204 break; 8205 case 7: 8206 sense_key = KEY_MEDIUM_ERROR; 8207 add_sense_code = 8208 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8209 add_sense_code_qual = SCSI_COMPONENT_87; 8210 break; 8211 case 8: 8212 sense_key = KEY_HARDWARE_ERROR; 8213 add_sense_code = 8214 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8215 add_sense_code_qual = SCSI_COMPONENT_88; 8216 break; 8217 } 8218 code = 0; /* unspecified */ 8219 status |= (code << 4); 8220 lpp->param_values[0] = status; 8221 lpp->param_values[1] = 0; /* unspecified */ 8222 lpp->param_values[2] = entry-> 8223 smart_selftest_log_timestamp[1]; 8224 lpp->param_values[3] = entry-> 8225 smart_selftest_log_timestamp[0]; 8226 if (status != 0) { 8227 lpp->param_values[4] = 0; 8228 lpp->param_values[5] = 0; 8229 lpp->param_values[6] = 0; 8230 lpp->param_values[7] = 0; 8231 lpp->param_values[8] = entry-> 8232 smart_selftest_log_failing_lba[3]; 8233 lpp->param_values[9] = entry-> 8234 smart_selftest_log_failing_lba[2]; 8235 lpp->param_values[10] = entry-> 8236 smart_selftest_log_failing_lba[1]; 8237 lpp->param_values[11] = entry-> 8238 smart_selftest_log_failing_lba[0]; 8239 } else { /* No block address */ 8240 lpp->param_values[4] = 0xff; 8241 lpp->param_values[5] = 0xff; 8242 lpp->param_values[6] = 0xff; 8243 lpp->param_values[7] = 0xff; 8244 lpp->param_values[8] = 0xff; 8245 lpp->param_values[9] = 0xff; 8246 lpp->param_values[10] = 0xff; 8247 lpp->param_values[11] = 0xff; 8248 } 8249 lpp->param_values[12] = sense_key; 8250 lpp->param_values[13] = add_sense_code; 8251 lpp->param_values[14] = add_sense_code_qual; 8252 lpp->param_values[15] = 0; /* undefined */ 8253 8254 lpp = (struct log_parameter *) 8255 (((uint8_t *)lpp) + 8256 SCSI_LOG_PARAM_HDR_LEN + 8257 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 8258 --index; /* back up to previous entry */ 8259 if (index < 0) { 8260 index = 8261 NUM_SMART_SELFTEST_LOG_ENTRIES - 1; 8262 } 8263 entry = &selftest_log-> 8264 smart_selftest_log_entries[index]; 8265 } 8266 } 8267 done: 8268 kmem_free(selftest_log, sizeof (struct smart_selftest_log)); 8269 } 8270 8271 return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) * 8272 SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS); 8273 } 8274 8275 /* 8276 * sata_build_lsense_page_2f() is used to create the 8277 * SCSI LOG SENSE page 0x2f (informational exceptions) 8278 * 8279 * Takes a sata_drive_info t * and the address of a buffer 8280 * in which to create the page information as well as a sata_hba_inst_t *. 8281 * 8282 * Returns the number of bytes valid in the buffer. 8283 * 8284 * Because it invokes function(s) that send synchronously executed command 8285 * to the HBA, it cannot be called in the interrupt context. 8286 */ 8287 static int 8288 sata_build_lsense_page_2f( 8289 sata_drive_info_t *sdinfo, 8290 uint8_t *buf, 8291 sata_hba_inst_t *sata_hba_inst) 8292 { 8293 struct log_parameter *lpp = (struct log_parameter *)buf; 8294 int rval; 8295 uint8_t *smart_data; 8296 uint8_t temp; 8297 sata_id_t *sata_id; 8298 #define SMART_NO_TEMP 0xff 8299 8300 lpp->param_code[0] = 0; 8301 lpp->param_code[1] = 0; 8302 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 8303 8304 /* Now get the SMART status w.r.t. threshold exceeded */ 8305 rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo); 8306 switch (rval) { 8307 case 1: 8308 lpp->param_values[0] = SCSI_PREDICTED_FAILURE; 8309 lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE; 8310 break; 8311 case 0: 8312 case -1: /* failed to get data */ 8313 lpp->param_values[0] = 0; /* No failure predicted */ 8314 lpp->param_values[1] = 0; 8315 break; 8316 #if defined(SATA_DEBUG) 8317 default: 8318 cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value"); 8319 /* NOTREACHED */ 8320 #endif 8321 } 8322 8323 sata_id = &sdinfo->satadrv_id; 8324 if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP)) 8325 temp = SMART_NO_TEMP; 8326 else { 8327 /* Now get the temperature */ 8328 smart_data = kmem_zalloc(512, KM_SLEEP); 8329 rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data, 8330 SCT_STATUS_LOG_PAGE, 1); 8331 if (rval == -1) 8332 temp = SMART_NO_TEMP; 8333 else { 8334 temp = smart_data[200]; 8335 if (temp & 0x80) { 8336 if (temp & 0x7f) 8337 temp = 0; 8338 else 8339 temp = SMART_NO_TEMP; 8340 } 8341 } 8342 kmem_free(smart_data, 512); 8343 } 8344 8345 lpp->param_values[2] = temp; /* most recent temperature */ 8346 lpp->param_values[3] = 0; /* required vendor specific byte */ 8347 8348 lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN; 8349 8350 8351 return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN); 8352 } 8353 8354 /* 8355 * sata_build_lsense_page_30() is used to create the 8356 * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data). 8357 * 8358 * Takes a sata_drive_info t * and the address of a buffer 8359 * in which to create the page information as well as a sata_hba_inst_t *. 8360 * 8361 * Returns the number of bytes valid in the buffer. 8362 */ 8363 static int 8364 sata_build_lsense_page_30( 8365 sata_drive_info_t *sdinfo, 8366 uint8_t *buf, 8367 sata_hba_inst_t *sata_hba_inst) 8368 { 8369 struct smart_data *smart_data = (struct smart_data *)buf; 8370 int rval; 8371 8372 /* Now do the SMART READ DATA */ 8373 rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data); 8374 if (rval == -1) 8375 return (0); 8376 8377 return (sizeof (struct smart_data)); 8378 } 8379 8380 /* 8381 * sata_build_lsense_page_0e() is used to create the 8382 * SCSI LOG SENSE page 0e (start-stop cycle counter page) 8383 * 8384 * Date of Manufacture (0x0001) 8385 * YEAR = "0000" 8386 * WEEK = "00" 8387 * Accounting Date (0x0002) 8388 * 6 ASCII space character(20h) 8389 * Specified cycle count over device lifetime 8390 * VALUE - THRESH - the delta between max and min; 8391 * Accumulated start-stop cycles 8392 * VALUE - WORST - the accumulated cycles; 8393 * 8394 * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute 8395 * 8396 * Takes a sata_drive_info t * and the address of a buffer 8397 * in which to create the page information as well as a sata_hba_inst_t *. 8398 * 8399 * Returns the number of bytes valid in the buffer. 8400 */ 8401 static int 8402 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf, 8403 sata_pkt_txlate_t *spx) 8404 { 8405 struct start_stop_cycle_counter_log *log_page; 8406 int i, rval, index; 8407 uint8_t smart_data[512], id, value, worst, thresh; 8408 uint32_t max_count, cycles; 8409 8410 /* Now do the SMART READ DATA */ 8411 rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo, 8412 (struct smart_data *)smart_data); 8413 if (rval == -1) 8414 return (0); 8415 for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) { 8416 index = (i * 12) + 2; 8417 id = smart_data[index]; 8418 if (id != SMART_START_STOP_COUNT_ID) 8419 continue; 8420 else { 8421 thresh = smart_data[index + 2]; 8422 value = smart_data[index + 3]; 8423 worst = smart_data[index + 4]; 8424 break; 8425 } 8426 } 8427 if (id != SMART_START_STOP_COUNT_ID) 8428 return (0); 8429 max_count = value - thresh; 8430 cycles = value - worst; 8431 8432 log_page = (struct start_stop_cycle_counter_log *)buf; 8433 bzero(log_page, sizeof (struct start_stop_cycle_counter_log)); 8434 log_page->code = 0x0e; 8435 log_page->page_len_low = 0x24; 8436 8437 log_page->manufactor_date_low = 0x1; 8438 log_page->param_1.fmt_link = 0x1; /* 01b */ 8439 log_page->param_len_1 = 0x06; 8440 for (i = 0; i < 4; i++) { 8441 log_page->year_manu[i] = 0x30; 8442 if (i < 2) 8443 log_page->week_manu[i] = 0x30; 8444 } 8445 8446 log_page->account_date_low = 0x02; 8447 log_page->param_2.fmt_link = 0x01; /* 01b */ 8448 log_page->param_len_2 = 0x06; 8449 for (i = 0; i < 4; i++) { 8450 log_page->year_account[i] = 0x20; 8451 if (i < 2) 8452 log_page->week_account[i] = 0x20; 8453 } 8454 8455 log_page->lifetime_code_low = 0x03; 8456 log_page->param_3.fmt_link = 0x03; /* 11b */ 8457 log_page->param_len_3 = 0x04; 8458 /* VALUE - THRESH - the delta between max and min */ 8459 log_page->cycle_code_low = 0x04; 8460 log_page->param_4.fmt_link = 0x03; /* 11b */ 8461 log_page->param_len_4 = 0x04; 8462 /* WORST - THRESH - the distance from 'now' to min */ 8463 8464 for (i = 0; i < 4; i++) { 8465 log_page->cycle_lifetime[i] = 8466 (max_count >> (8 * (3 - i))) & 0xff; 8467 log_page->cycle_accumulated[i] = 8468 (cycles >> (8 * (3 - i))) & 0xff; 8469 } 8470 8471 return (sizeof (struct start_stop_cycle_counter_log)); 8472 } 8473 8474 /* 8475 * This function was used for build a ATA read verify sector command 8476 */ 8477 static void 8478 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba) 8479 { 8480 scmd->satacmd_cmd_reg = SATAC_RDVER; 8481 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 8482 scmd->satacmd_flags.sata_special_regs = B_TRUE; 8483 8484 scmd->satacmd_sec_count_lsb = sec & 0xff; 8485 scmd->satacmd_lba_low_lsb = lba & 0xff; 8486 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 8487 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 8488 scmd->satacmd_device_reg = (SATA_ADH_LBA | (lba >> 24) & 0xf); 8489 scmd->satacmd_features_reg = 0; 8490 scmd->satacmd_status_reg = 0; 8491 scmd->satacmd_error_reg = 0; 8492 } 8493 8494 /* 8495 * This function was used for building an ATA 8496 * command, and only command register need to 8497 * be defined, other register will be zero or na. 8498 */ 8499 static void 8500 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd) 8501 { 8502 scmd->satacmd_addr_type = 0; 8503 scmd->satacmd_cmd_reg = cmd; 8504 scmd->satacmd_device_reg = 0; 8505 scmd->satacmd_sec_count_lsb = 0; 8506 scmd->satacmd_lba_low_lsb = 0; 8507 scmd->satacmd_lba_mid_lsb = 0; 8508 scmd->satacmd_lba_high_lsb = 0; 8509 scmd->satacmd_features_reg = 0; 8510 scmd->satacmd_status_reg = 0; 8511 scmd->satacmd_error_reg = 0; 8512 scmd->satacmd_flags.sata_special_regs = B_TRUE; 8513 } 8514 8515 /* 8516 * This function was used for changing the standby 8517 * timer format from SCSI to ATA. 8518 */ 8519 static uint8_t 8520 sata_get_standby_timer(uint8_t *timer) 8521 { 8522 uint32_t i = 0, count = 0; 8523 uint8_t ata_count; 8524 8525 for (i = 0; i < 4; i++) { 8526 count = count << 8 | timer[i]; 8527 } 8528 8529 if (count == 0) 8530 return (0); 8531 8532 if (count >= 1 && count <= 12000) 8533 ata_count = (count -1) / 50 + 1; 8534 else if (count > 12000 && count <= 12600) 8535 ata_count = 0xfc; 8536 else if (count > 12601 && count <= 12750) 8537 ata_count = 0xff; 8538 else if (count > 12750 && count <= 17999) 8539 ata_count = 0xf1; 8540 else if (count > 18000 && count <= 198000) 8541 ata_count = count / 18000 + 240; 8542 else 8543 ata_count = 0xfd; 8544 return (ata_count); 8545 } 8546 8547 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */ 8548 8549 /* 8550 * Start command for ATAPI device. 8551 * This function processes scsi_pkt requests. 8552 * Now CD/DVD, tape and ATAPI disk devices are supported. 8553 * Most commands are packet without any translation into Packet Command. 8554 * Some may be trapped and executed as SATA commands (not clear which one). 8555 * 8556 * Returns TRAN_ACCEPT if command is accepted for execution (or completed 8557 * execution). 8558 * Returns other TRAN_XXXX codes if command is not accepted or completed 8559 * (see return values for sata_hba_start()). 8560 * 8561 * Note: 8562 * Inquiry cdb format differs between transport version 2 and 3. 8563 * However, the transport version 3 devices that were checked did not adhere 8564 * to the specification (ignored MSB of the allocation length). Therefore, 8565 * the transport version is not checked, but Inquiry allocation length is 8566 * truncated to 255 bytes if the original allocation length set-up by the 8567 * target driver is greater than 255 bytes. 8568 */ 8569 static int 8570 sata_txlt_atapi(sata_pkt_txlate_t *spx) 8571 { 8572 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 8573 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 8574 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 8575 sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx); 8576 sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba, 8577 &spx->txlt_sata_pkt->satapkt_device); 8578 int cport = SATA_TXLT_CPORT(spx); 8579 int cdblen; 8580 int rval, reason; 8581 int synch; 8582 union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp; 8583 8584 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 8585 8586 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 8587 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 8588 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 8589 return (rval); 8590 } 8591 8592 /* 8593 * ATAPI device executes some ATA commands in addition to those 8594 * commands sent via PACKET command. These ATA commands may be 8595 * executed by the regular SATA translation functions. None needs 8596 * to be captured now. 8597 * 8598 * Commands sent via PACKET command include: 8599 * MMC command set for ATAPI CD/DVD device 8600 * SSC command set for ATAPI TAPE device 8601 * SBC command set for ATAPI disk device 8602 * 8603 */ 8604 8605 /* Check the size of cdb */ 8606 8607 switch (GETGROUP(cdbp)) { 8608 case CDB_GROUPID_3: /* Reserved, per SPC-4 */ 8609 /* 8610 * opcodes 0x7e and 0x7f identify variable-length CDBs and 8611 * therefore require special handling. Return failure, for now. 8612 */ 8613 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 8614 return (TRAN_BADPKT); 8615 8616 case CDB_GROUPID_6: /* Vendor-specific, per SPC-4 */ 8617 case CDB_GROUPID_7: /* Vendor-specific, per SPC-4 */ 8618 /* obtain length from the scsi_pkt */ 8619 cdblen = scsipkt->pkt_cdblen; 8620 break; 8621 8622 default: 8623 /* CDB's length is statically known, per SPC-4 */ 8624 cdblen = scsi_cdb_size[GETGROUP(cdbp)]; 8625 break; 8626 } 8627 8628 if (cdblen <= 0 || cdblen > sdinfo->satadrv_atapi_cdb_len) { 8629 sata_log(NULL, CE_WARN, 8630 "sata: invalid ATAPI cdb length %d", 8631 cdblen); 8632 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 8633 return (TRAN_BADPKT); 8634 } 8635 8636 SATAATAPITRACE(spx, cdblen); 8637 8638 /* 8639 * For non-read/write commands we need to 8640 * map buffer 8641 */ 8642 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 8643 case SCMD_READ: 8644 case SCMD_READ_G1: 8645 case SCMD_READ_G5: 8646 case SCMD_READ_G4: 8647 case SCMD_WRITE: 8648 case SCMD_WRITE_G1: 8649 case SCMD_WRITE_G5: 8650 case SCMD_WRITE_G4: 8651 break; 8652 default: 8653 if (bp != NULL) { 8654 if (bp->b_flags & (B_PHYS | B_PAGEIO)) 8655 bp_mapin(bp); 8656 } 8657 break; 8658 } 8659 /* 8660 * scmd->satacmd_flags.sata_data_direction default - 8661 * SATA_DIR_NODATA_XFER - is set by 8662 * sata_txlt_generic_pkt_info(). 8663 */ 8664 if (scmd->satacmd_bp) { 8665 if (scmd->satacmd_bp->b_flags & B_READ) { 8666 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 8667 } else { 8668 scmd->satacmd_flags.sata_data_direction = 8669 SATA_DIR_WRITE; 8670 } 8671 } 8672 8673 /* 8674 * Set up ATAPI packet command. 8675 */ 8676 8677 sata_atapi_packet_cmd_setup(scmd, sdinfo); 8678 8679 /* Copy cdb into sata_cmd */ 8680 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 8681 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 8682 bcopy(cdbp, scmd->satacmd_acdb, cdblen); 8683 8684 /* See note in the command header */ 8685 if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) { 8686 if (scmd->satacmd_acdb[3] != 0) 8687 scmd->satacmd_acdb[4] = 255; 8688 } 8689 8690 #ifdef SATA_DEBUG 8691 if (sata_debug_flags & SATA_DBG_ATAPI) { 8692 uint8_t *p = scmd->satacmd_acdb; 8693 char buf[3 * SATA_ATAPI_MAX_CDB_LEN]; 8694 8695 (void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN, 8696 "%02x %02x %02x %02x %02x %02x %02x %02x " 8697 "%2x %02x %02x %02x %02x %02x %02x %02x", 8698 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 8699 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 8700 buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0'; 8701 cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf); 8702 } 8703 #endif 8704 8705 /* 8706 * Preset request sense data to NO SENSE. 8707 * If there is no way to get error information via Request Sense, 8708 * the packet request sense data would not have to be modified by HBA, 8709 * but it could be returned as is. 8710 */ 8711 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN); 8712 sata_fixed_sense_data_preset( 8713 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 8714 8715 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 8716 /* Need callback function */ 8717 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion; 8718 synch = FALSE; 8719 } else 8720 synch = TRUE; 8721 8722 /* Transfer command to HBA */ 8723 if (sata_hba_start(spx, &rval) != 0) { 8724 /* Pkt not accepted for execution */ 8725 mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport)); 8726 return (rval); 8727 } 8728 mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport)); 8729 /* 8730 * If execution is non-synchronous, 8731 * a callback function will handle potential errors, translate 8732 * the response and will do a callback to a target driver. 8733 * If it was synchronous, use the same framework callback to check 8734 * an execution status. 8735 */ 8736 if (synch) { 8737 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 8738 "synchronous execution status %x\n", 8739 spx->txlt_sata_pkt->satapkt_reason); 8740 sata_txlt_atapi_completion(spx->txlt_sata_pkt); 8741 } 8742 return (TRAN_ACCEPT); 8743 } 8744 8745 8746 /* 8747 * ATAPI Packet command completion. 8748 * 8749 * Failure of the command passed via Packet command are considered device 8750 * error. SATA HBA driver would have to retrieve error data (via Request 8751 * Sense command delivered via error retrieval sata packet) and copy it 8752 * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data. 8753 */ 8754 static void 8755 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt) 8756 { 8757 sata_pkt_txlate_t *spx = 8758 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 8759 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 8760 struct scsi_extended_sense *sense; 8761 struct buf *bp; 8762 int rval; 8763 8764 #ifdef SATA_DEBUG 8765 uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense; 8766 #endif 8767 8768 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 8769 STATE_SENT_CMD | STATE_GOT_STATUS; 8770 8771 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 8772 /* Normal completion */ 8773 if (sata_pkt->satapkt_cmd.satacmd_bp != NULL) 8774 scsipkt->pkt_state |= STATE_XFERRED_DATA; 8775 scsipkt->pkt_reason = CMD_CMPLT; 8776 *scsipkt->pkt_scbp = STATUS_GOOD; 8777 if (spx->txlt_tmp_buf != NULL) { 8778 /* Temporary buffer was used */ 8779 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 8780 if (bp->b_flags & B_READ) { 8781 rval = ddi_dma_sync( 8782 spx->txlt_buf_dma_handle, 0, 0, 8783 DDI_DMA_SYNC_FORCPU); 8784 ASSERT(rval == DDI_SUCCESS); 8785 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 8786 bp->b_bcount); 8787 } 8788 } 8789 } else { 8790 /* 8791 * Something went wrong - analyze return 8792 */ 8793 *scsipkt->pkt_scbp = STATUS_CHECK; 8794 sense = sata_arq_sense(spx); 8795 8796 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 8797 /* 8798 * pkt_reason should be CMD_CMPLT for DEVICE ERROR. 8799 * Under this condition ERR bit is set for ATA command, 8800 * and CHK bit set for ATAPI command. 8801 * 8802 * Please check st_intr & sdintr about how pkt_reason 8803 * is used. 8804 */ 8805 scsipkt->pkt_reason = CMD_CMPLT; 8806 8807 /* 8808 * We may not have ARQ data if there was a double 8809 * error. But sense data in sata packet was pre-set 8810 * with NO SENSE so it is valid even if HBA could 8811 * not retrieve a real sense data. 8812 * Just copy this sense data into scsi pkt sense area. 8813 */ 8814 bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense, 8815 SATA_ATAPI_MIN_RQSENSE_LEN); 8816 #ifdef SATA_DEBUG 8817 if (sata_debug_flags & SATA_DBG_SCSI_IF) { 8818 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 8819 "sata_txlt_atapi_completion: %02x\n" 8820 "RQSENSE: %02x %02x %02x %02x %02x %02x " 8821 " %02x %02x %02x %02x %02x %02x " 8822 " %02x %02x %02x %02x %02x %02x\n", 8823 scsipkt->pkt_reason, 8824 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 8825 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 8826 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 8827 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 8828 rqsp[16], rqsp[17]); 8829 } 8830 #endif 8831 } else { 8832 switch (sata_pkt->satapkt_reason) { 8833 case SATA_PKT_PORT_ERROR: 8834 /* 8835 * We have no device data. 8836 */ 8837 scsipkt->pkt_reason = CMD_INCOMPLETE; 8838 scsipkt->pkt_state &= ~(STATE_GOT_BUS | 8839 STATE_GOT_TARGET | STATE_SENT_CMD | 8840 STATE_GOT_STATUS); 8841 sense->es_key = KEY_HARDWARE_ERROR; 8842 break; 8843 8844 case SATA_PKT_TIMEOUT: 8845 scsipkt->pkt_reason = CMD_TIMEOUT; 8846 scsipkt->pkt_statistics |= 8847 STAT_TIMEOUT | STAT_DEV_RESET; 8848 /* 8849 * Need to check if HARDWARE_ERROR/ 8850 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more 8851 * appropriate. 8852 */ 8853 break; 8854 8855 case SATA_PKT_ABORTED: 8856 scsipkt->pkt_reason = CMD_ABORTED; 8857 scsipkt->pkt_statistics |= STAT_ABORTED; 8858 /* Should we set key COMMAND_ABPRTED? */ 8859 break; 8860 8861 case SATA_PKT_RESET: 8862 scsipkt->pkt_reason = CMD_RESET; 8863 scsipkt->pkt_statistics |= STAT_DEV_RESET; 8864 /* 8865 * May be we should set Unit Attention / 8866 * Reset. Perhaps the same should be 8867 * returned for disks.... 8868 */ 8869 sense->es_key = KEY_UNIT_ATTENTION; 8870 sense->es_add_code = SD_SCSI_ASC_RESET; 8871 break; 8872 8873 default: 8874 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 8875 "sata_txlt_atapi_completion: " 8876 "invalid packet completion reason")); 8877 scsipkt->pkt_reason = CMD_TRAN_ERR; 8878 scsipkt->pkt_state &= ~(STATE_GOT_BUS | 8879 STATE_GOT_TARGET | STATE_SENT_CMD | 8880 STATE_GOT_STATUS); 8881 break; 8882 } 8883 } 8884 } 8885 8886 SATAATAPITRACE(spx, 0); 8887 8888 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 8889 scsipkt->pkt_comp != NULL) { 8890 /* scsi callback required */ 8891 (*scsipkt->pkt_comp)(scsipkt); 8892 } 8893 } 8894 8895 /* 8896 * Set up error retrieval sata command for ATAPI Packet Command error data 8897 * recovery. 8898 * 8899 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up, 8900 * returns SATA_FAILURE otherwise. 8901 */ 8902 8903 static int 8904 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo) 8905 { 8906 sata_pkt_t *spkt = spx->txlt_sata_pkt; 8907 sata_cmd_t *scmd; 8908 struct buf *bp; 8909 8910 /* 8911 * Allocate dma-able buffer error data. 8912 * Buffer allocation will take care of buffer alignment and other DMA 8913 * attributes. 8914 */ 8915 bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN); 8916 if (bp == NULL) { 8917 SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst, 8918 "sata_get_err_retrieval_pkt: " 8919 "cannot allocate buffer for error data", NULL); 8920 return (SATA_FAILURE); 8921 } 8922 bp_mapin(bp); /* make data buffer accessible */ 8923 8924 /* Operation modes are up to the caller */ 8925 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 8926 8927 /* Synchronous mode, no callback - may be changed by the caller */ 8928 spkt->satapkt_comp = NULL; 8929 spkt->satapkt_time = sata_default_pkt_time; 8930 8931 scmd = &spkt->satapkt_cmd; 8932 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 8933 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 8934 8935 sata_atapi_packet_cmd_setup(scmd, sdinfo); 8936 8937 /* 8938 * Set-up acdb. Request Sense CDB (packet command content) is 8939 * not in DMA-able buffer. Its handling is HBA-specific (how 8940 * it is transfered into packet FIS). 8941 */ 8942 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 8943 bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN); 8944 /* Following zeroing of pad bytes may not be necessary */ 8945 bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN], 8946 sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN); 8947 8948 /* 8949 * Set-up pointer to the buffer handle, so HBA can sync buffer 8950 * before accessing it. Handle is in usual place in translate struct. 8951 */ 8952 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle; 8953 8954 /* 8955 * Preset request sense data to NO SENSE. 8956 * Here it is redundant, only for a symetry with scsi-originated 8957 * packets. It should not be used for anything but debugging. 8958 */ 8959 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN); 8960 sata_fixed_sense_data_preset( 8961 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 8962 8963 ASSERT(scmd->satacmd_num_dma_cookies != 0); 8964 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 8965 8966 return (SATA_SUCCESS); 8967 } 8968 8969 /* 8970 * Set-up ATAPI packet command. 8971 * Data transfer direction has to be set-up in sata_cmd structure prior to 8972 * calling this function. 8973 * 8974 * Returns void 8975 */ 8976 8977 static void 8978 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo) 8979 { 8980 scmd->satacmd_addr_type = 0; /* N/A */ 8981 scmd->satacmd_sec_count_lsb = 0; /* no tag */ 8982 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 8983 scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ; 8984 scmd->satacmd_lba_high_lsb = 8985 (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8); 8986 scmd->satacmd_cmd_reg = SATAC_PACKET; /* Command */ 8987 8988 /* 8989 * We want all data to be transfered via DMA. 8990 * But specify it only if drive supports DMA and DMA mode is 8991 * selected - some drives are sensitive about it. 8992 * Hopefully it wil work for all drives.... 8993 */ 8994 if (sdinfo->satadrv_settings & SATA_DEV_DMA) 8995 scmd->satacmd_features_reg = SATA_ATAPI_F_DMA; 8996 8997 /* 8998 * Features register requires special care for devices that use 8999 * Serial ATA bridge - they need an explicit specification of 9000 * the data transfer direction for Packet DMA commands. 9001 * Setting this bit is harmless if DMA is not used. 9002 * 9003 * Many drives do not implement word 80, specifying what ATA/ATAPI 9004 * spec they follow. 9005 * We are arbitrarily following the latest SerialATA 2.6 spec, 9006 * which uses ATA/ATAPI 6 specification for Identify Data, unless 9007 * ATA/ATAPI-7 support is explicitly indicated. 9008 */ 9009 if (sdinfo->satadrv_id.ai_majorversion != 0 && 9010 sdinfo->satadrv_id.ai_majorversion != 0xffff && 9011 (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) { 9012 /* 9013 * Specification of major version is valid and version 7 9014 * is supported. It does automatically imply that all 9015 * spec features are supported. For now, we assume that 9016 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete. 9017 */ 9018 if ((sdinfo->satadrv_id.ai_dirdma & 9019 SATA_ATAPI_ID_DMADIR_REQ) != 0) { 9020 if (scmd->satacmd_flags.sata_data_direction == 9021 SATA_DIR_READ) 9022 scmd->satacmd_features_reg |= 9023 SATA_ATAPI_F_DATA_DIR_READ; 9024 } 9025 } 9026 } 9027 9028 9029 #ifdef SATA_DEBUG 9030 9031 /* Display 18 bytes of Inquiry data */ 9032 static void 9033 sata_show_inqry_data(uint8_t *buf) 9034 { 9035 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 9036 uint8_t *p; 9037 9038 cmn_err(CE_NOTE, "Inquiry data:"); 9039 cmn_err(CE_NOTE, "device type %x", inq->inq_dtype); 9040 cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb); 9041 cmn_err(CE_NOTE, "version %x", inq->inq_ansi); 9042 cmn_err(CE_NOTE, "ATAPI transport version %d", 9043 SATA_ATAPI_TRANS_VERSION(inq)); 9044 cmn_err(CE_NOTE, "response data format %d, aenc %d", 9045 inq->inq_rdf, inq->inq_aenc); 9046 cmn_err(CE_NOTE, " additional length %d", inq->inq_len); 9047 cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs); 9048 p = (uint8_t *)inq->inq_vid; 9049 cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x " 9050 "%02x %02x %02x %02x", 9051 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 9052 p = (uint8_t *)inq->inq_vid; 9053 cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c", 9054 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 9055 9056 p = (uint8_t *)inq->inq_pid; 9057 cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x " 9058 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", 9059 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 9060 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 9061 p = (uint8_t *)inq->inq_pid; 9062 cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c " 9063 "%c %c %c %c %c %c %c %c", 9064 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 9065 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 9066 9067 p = (uint8_t *)inq->inq_revision; 9068 cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x", 9069 p[0], p[1], p[2], p[3]); 9070 p = (uint8_t *)inq->inq_revision; 9071 cmn_err(CE_NOTE, "revision: %c %c %c %c", 9072 p[0], p[1], p[2], p[3]); 9073 9074 } 9075 9076 9077 static void 9078 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count) 9079 { 9080 struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt; 9081 9082 if (scsi_pkt == NULL) 9083 return; 9084 if (count != 0) { 9085 /* saving cdb */ 9086 bzero(sata_atapi_trace[sata_atapi_trace_index].acdb, 9087 SATA_ATAPI_MAX_CDB_LEN); 9088 bcopy(scsi_pkt->pkt_cdbp, 9089 sata_atapi_trace[sata_atapi_trace_index].acdb, count); 9090 } else { 9091 bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)-> 9092 sts_sensedata, 9093 sata_atapi_trace[sata_atapi_trace_index].arqs, 9094 SATA_ATAPI_MIN_RQSENSE_LEN); 9095 sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason = 9096 scsi_pkt->pkt_reason; 9097 sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason = 9098 spx->txlt_sata_pkt->satapkt_reason; 9099 9100 if (++sata_atapi_trace_index >= 64) 9101 sata_atapi_trace_index = 0; 9102 } 9103 } 9104 9105 #endif 9106 9107 /* 9108 * Fetch inquiry data from ATAPI device 9109 * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise. 9110 * 9111 * Note: 9112 * inqb pointer does not point to a DMA-able buffer. It is a local buffer 9113 * where the caller expects to see the inquiry data. 9114 * 9115 */ 9116 9117 static int 9118 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba, 9119 sata_address_t *saddr, struct scsi_inquiry *inq) 9120 { 9121 sata_pkt_txlate_t *spx; 9122 sata_pkt_t *spkt; 9123 struct buf *bp; 9124 sata_drive_info_t *sdinfo; 9125 sata_cmd_t *scmd; 9126 int rval; 9127 uint8_t *rqsp; 9128 #ifdef SATA_DEBUG 9129 char msg_buf[MAXPATHLEN]; 9130 #endif 9131 9132 ASSERT(sata_hba != NULL); 9133 9134 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 9135 spx->txlt_sata_hba_inst = sata_hba; 9136 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 9137 spkt = sata_pkt_alloc(spx, NULL); 9138 if (spkt == NULL) { 9139 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 9140 return (SATA_FAILURE); 9141 } 9142 /* address is needed now */ 9143 spkt->satapkt_device.satadev_addr = *saddr; 9144 9145 /* scsi_inquiry size buffer */ 9146 bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry)); 9147 if (bp == NULL) { 9148 sata_pkt_free(spx); 9149 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 9150 SATA_LOG_D((sata_hba, CE_WARN, 9151 "sata_get_atapi_inquiry_data: " 9152 "cannot allocate data buffer")); 9153 return (SATA_FAILURE); 9154 } 9155 bp_mapin(bp); /* make data buffer accessible */ 9156 9157 scmd = &spkt->satapkt_cmd; 9158 ASSERT(scmd->satacmd_num_dma_cookies != 0); 9159 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 9160 9161 /* Use synchronous mode */ 9162 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 9163 spkt->satapkt_comp = NULL; 9164 spkt->satapkt_time = sata_default_pkt_time; 9165 9166 /* Issue inquiry command - 6 bytes cdb, data transfer, read */ 9167 9168 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 9169 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 9170 9171 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 9172 sdinfo = sata_get_device_info(sata_hba, 9173 &spx->txlt_sata_pkt->satapkt_device); 9174 if (sdinfo == NULL) { 9175 /* we have to be carefull about the disapearing device */ 9176 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 9177 rval = SATA_FAILURE; 9178 goto cleanup; 9179 } 9180 sata_atapi_packet_cmd_setup(scmd, sdinfo); 9181 9182 /* 9183 * Set-up acdb. This works for atapi transport version 2 and later. 9184 */ 9185 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 9186 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 9187 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */ 9188 scmd->satacmd_acdb[1] = 0x00; 9189 scmd->satacmd_acdb[2] = 0x00; 9190 scmd->satacmd_acdb[3] = 0x00; 9191 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry); 9192 scmd->satacmd_acdb[5] = 0x00; 9193 9194 sata_fixed_sense_data_preset( 9195 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 9196 9197 /* Transfer command to HBA */ 9198 if (sata_hba_start(spx, &rval) != 0) { 9199 /* Pkt not accepted for execution */ 9200 SATADBG1(SATA_DBG_ATAPI, sata_hba, 9201 "sata_get_atapi_inquiry_data: " 9202 "Packet not accepted for execution - ret: %02x", rval); 9203 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 9204 rval = SATA_FAILURE; 9205 goto cleanup; 9206 } 9207 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 9208 9209 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) { 9210 SATADBG1(SATA_DBG_ATAPI, sata_hba, 9211 "sata_get_atapi_inquiry_data: " 9212 "Packet completed successfully - ret: %02x", rval); 9213 if (spx->txlt_buf_dma_handle != NULL) { 9214 /* 9215 * Sync buffer. Handle is in usual place in translate 9216 * struct. 9217 */ 9218 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 9219 DDI_DMA_SYNC_FORCPU); 9220 ASSERT(rval == DDI_SUCCESS); 9221 } 9222 /* 9223 * Normal completion - copy data into caller's buffer 9224 */ 9225 bcopy(bp->b_un.b_addr, (uint8_t *)inq, 9226 sizeof (struct scsi_inquiry)); 9227 #ifdef SATA_DEBUG 9228 if (sata_debug_flags & SATA_DBG_ATAPI) { 9229 sata_show_inqry_data((uint8_t *)inq); 9230 } 9231 #endif 9232 rval = SATA_SUCCESS; 9233 } else { 9234 /* 9235 * Something went wrong - analyze return - check rqsense data 9236 */ 9237 rval = SATA_FAILURE; 9238 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 9239 /* 9240 * ARQ data hopefull show something other than NO SENSE 9241 */ 9242 rqsp = scmd->satacmd_rqsense; 9243 #ifdef SATA_DEBUG 9244 if (sata_debug_flags & SATA_DBG_ATAPI) { 9245 msg_buf[0] = '\0'; 9246 (void) snprintf(msg_buf, MAXPATHLEN, 9247 "ATAPI packet completion reason: %02x\n" 9248 "RQSENSE: %02x %02x %02x %02x %02x %02x\n" 9249 " %02x %02x %02x %02x %02x %02x\n" 9250 " %02x %02x %02x %02x %02x %02x", 9251 spkt->satapkt_reason, 9252 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 9253 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 9254 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 9255 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 9256 rqsp[16], rqsp[17]); 9257 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 9258 "%s", msg_buf); 9259 } 9260 #endif 9261 } else { 9262 switch (spkt->satapkt_reason) { 9263 case SATA_PKT_PORT_ERROR: 9264 SATADBG1(SATA_DBG_ATAPI, sata_hba, 9265 "sata_get_atapi_inquiry_data: " 9266 "packet reason: port error", NULL); 9267 break; 9268 9269 case SATA_PKT_TIMEOUT: 9270 SATADBG1(SATA_DBG_ATAPI, sata_hba, 9271 "sata_get_atapi_inquiry_data: " 9272 "packet reason: timeout", NULL); 9273 break; 9274 9275 case SATA_PKT_ABORTED: 9276 SATADBG1(SATA_DBG_ATAPI, sata_hba, 9277 "sata_get_atapi_inquiry_data: " 9278 "packet reason: aborted", NULL); 9279 break; 9280 9281 case SATA_PKT_RESET: 9282 SATADBG1(SATA_DBG_ATAPI, sata_hba, 9283 "sata_get_atapi_inquiry_data: " 9284 "packet reason: reset\n", NULL); 9285 break; 9286 default: 9287 SATADBG1(SATA_DBG_ATAPI, sata_hba, 9288 "sata_get_atapi_inquiry_data: " 9289 "invalid packet reason: %02x\n", 9290 spkt->satapkt_reason); 9291 break; 9292 } 9293 } 9294 } 9295 cleanup: 9296 sata_free_local_buffer(spx); 9297 sata_pkt_free(spx); 9298 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 9299 return (rval); 9300 } 9301 9302 9303 9304 9305 9306 #if 0 9307 #ifdef SATA_DEBUG 9308 9309 /* 9310 * Test ATAPI packet command. 9311 * Single threaded test: send packet command in synch mode, process completion 9312 * 9313 */ 9314 static void 9315 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport) 9316 { 9317 sata_pkt_txlate_t *spx; 9318 sata_pkt_t *spkt; 9319 struct buf *bp; 9320 sata_device_t sata_device; 9321 sata_drive_info_t *sdinfo; 9322 sata_cmd_t *scmd; 9323 int rval; 9324 uint8_t *rqsp; 9325 9326 ASSERT(sata_hba_inst != NULL); 9327 sata_device.satadev_addr.cport = cport; 9328 sata_device.satadev_addr.pmport = 0; 9329 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 9330 sata_device.satadev_rev = SATA_DEVICE_REV; 9331 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 9332 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 9333 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 9334 if (sdinfo == NULL) { 9335 sata_log(sata_hba_inst, CE_WARN, 9336 "sata_test_atapi_packet_command: " 9337 "no device info for cport %d", 9338 sata_device.satadev_addr.cport); 9339 return; 9340 } 9341 9342 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 9343 spx->txlt_sata_hba_inst = sata_hba_inst; 9344 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 9345 spkt = sata_pkt_alloc(spx, NULL); 9346 if (spkt == NULL) { 9347 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 9348 return; 9349 } 9350 /* address is needed now */ 9351 spkt->satapkt_device.satadev_addr = sata_device.satadev_addr; 9352 9353 /* 1024k buffer */ 9354 bp = sata_alloc_local_buffer(spx, 1024); 9355 if (bp == NULL) { 9356 sata_pkt_free(spx); 9357 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 9358 sata_log(sata_hba_inst, CE_WARN, 9359 "sata_test_atapi_packet_command: " 9360 "cannot allocate data buffer"); 9361 return; 9362 } 9363 bp_mapin(bp); /* make data buffer accessible */ 9364 9365 scmd = &spkt->satapkt_cmd; 9366 ASSERT(scmd->satacmd_num_dma_cookies != 0); 9367 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 9368 9369 /* Use synchronous mode */ 9370 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 9371 9372 /* Synchronous mode, no callback - may be changed by the caller */ 9373 spkt->satapkt_comp = NULL; 9374 spkt->satapkt_time = sata_default_pkt_time; 9375 9376 /* Issue inquiry command - 6 bytes cdb, data transfer, read */ 9377 9378 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 9379 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 9380 9381 sata_atapi_packet_cmd_setup(scmd, sdinfo); 9382 9383 /* Set-up acdb. */ 9384 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 9385 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 9386 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */ 9387 scmd->satacmd_acdb[1] = 0x00; 9388 scmd->satacmd_acdb[2] = 0x00; 9389 scmd->satacmd_acdb[3] = 0x00; 9390 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry); 9391 scmd->satacmd_acdb[5] = 0x00; 9392 9393 sata_fixed_sense_data_preset( 9394 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 9395 9396 /* Transfer command to HBA */ 9397 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 9398 if (sata_hba_start(spx, &rval) != 0) { 9399 /* Pkt not accepted for execution */ 9400 sata_log(sata_hba_inst, CE_WARN, 9401 "sata_test_atapi_packet_command: " 9402 "Packet not accepted for execution - ret: %02x", rval); 9403 mutex_exit( 9404 &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 9405 goto cleanup; 9406 } 9407 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 9408 9409 if (spx->txlt_buf_dma_handle != NULL) { 9410 /* 9411 * Sync buffer. Handle is in usual place in translate struct. 9412 */ 9413 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 9414 DDI_DMA_SYNC_FORCPU); 9415 ASSERT(rval == DDI_SUCCESS); 9416 } 9417 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) { 9418 sata_log(sata_hba_inst, CE_WARN, 9419 "sata_test_atapi_packet_command: " 9420 "Packet completed successfully"); 9421 /* 9422 * Normal completion - show inquiry data 9423 */ 9424 sata_show_inqry_data((uint8_t *)bp->b_un.b_addr); 9425 } else { 9426 /* 9427 * Something went wrong - analyze return - check rqsense data 9428 */ 9429 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 9430 /* 9431 * ARQ data hopefull show something other than NO SENSE 9432 */ 9433 rqsp = scmd->satacmd_rqsense; 9434 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 9435 "ATAPI packet completion reason: %02x\n" 9436 "RQSENSE: %02x %02x %02x %02x %02x %02x " 9437 " %02x %02x %02x %02x %02x %02x " 9438 " %02x %02x %02x %02x %02x %02x\n", 9439 spkt->satapkt_reason, 9440 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 9441 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 9442 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 9443 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 9444 rqsp[16], rqsp[17]); 9445 } else { 9446 switch (spkt->satapkt_reason) { 9447 case SATA_PKT_PORT_ERROR: 9448 sata_log(sata_hba_inst, CE_WARN, 9449 "sata_test_atapi_packet_command: " 9450 "packet reason: port error\n"); 9451 break; 9452 9453 case SATA_PKT_TIMEOUT: 9454 sata_log(sata_hba_inst, CE_WARN, 9455 "sata_test_atapi_packet_command: " 9456 "packet reason: timeout\n"); 9457 break; 9458 9459 case SATA_PKT_ABORTED: 9460 sata_log(sata_hba_inst, CE_WARN, 9461 "sata_test_atapi_packet_command: " 9462 "packet reason: aborted\n"); 9463 break; 9464 9465 case SATA_PKT_RESET: 9466 sata_log(sata_hba_inst, CE_WARN, 9467 "sata_test_atapi_packet_command: " 9468 "packet reason: reset\n"); 9469 break; 9470 default: 9471 sata_log(sata_hba_inst, CE_WARN, 9472 "sata_test_atapi_packet_command: " 9473 "invalid packet reason: %02x\n", 9474 spkt->satapkt_reason); 9475 break; 9476 } 9477 } 9478 } 9479 cleanup: 9480 sata_free_local_buffer(spx); 9481 sata_pkt_free(spx); 9482 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 9483 } 9484 9485 #endif /* SATA_DEBUG */ 9486 #endif /* 1 */ 9487 9488 9489 /* ************************** LOCAL HELPER FUNCTIONS *********************** */ 9490 9491 /* 9492 * Validate sata_tran info 9493 * SATA_FAILURE returns if structure is inconsistent or structure revision 9494 * does not match one used by the framework. 9495 * 9496 * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains 9497 * required function pointers. 9498 * Returns SATA_FAILURE otherwise. 9499 */ 9500 static int 9501 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran) 9502 { 9503 /* 9504 * SATA_TRAN_HBA_REV is the current (highest) revision number 9505 * of the SATA interface. 9506 */ 9507 if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) { 9508 sata_log(NULL, CE_WARN, 9509 "sata: invalid sata_hba_tran version %d for driver %s", 9510 sata_tran->sata_tran_hba_rev, ddi_driver_name(dip)); 9511 return (SATA_FAILURE); 9512 } 9513 9514 if (dip != sata_tran->sata_tran_hba_dip) { 9515 SATA_LOG_D((NULL, CE_WARN, 9516 "sata: inconsistent sata_tran_hba_dip " 9517 "%p / %p", sata_tran->sata_tran_hba_dip, dip)); 9518 return (SATA_FAILURE); 9519 } 9520 9521 if (sata_tran->sata_tran_probe_port == NULL || 9522 sata_tran->sata_tran_start == NULL || 9523 sata_tran->sata_tran_abort == NULL || 9524 sata_tran->sata_tran_reset_dport == NULL || 9525 sata_tran->sata_tran_hotplug_ops == NULL || 9526 sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL || 9527 sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate == 9528 NULL) { 9529 SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing " 9530 "required functions")); 9531 } 9532 return (SATA_SUCCESS); 9533 } 9534 9535 /* 9536 * Remove HBA instance from sata_hba_list. 9537 */ 9538 static void 9539 sata_remove_hba_instance(dev_info_t *dip) 9540 { 9541 sata_hba_inst_t *sata_hba_inst; 9542 9543 mutex_enter(&sata_mutex); 9544 for (sata_hba_inst = sata_hba_list; 9545 sata_hba_inst != (struct sata_hba_inst *)NULL; 9546 sata_hba_inst = sata_hba_inst->satahba_next) { 9547 if (sata_hba_inst->satahba_dip == dip) 9548 break; 9549 } 9550 9551 if (sata_hba_inst == (struct sata_hba_inst *)NULL) { 9552 #ifdef SATA_DEBUG 9553 cmn_err(CE_WARN, "sata_remove_hba_instance: " 9554 "unknown HBA instance\n"); 9555 #endif 9556 ASSERT(FALSE); 9557 } 9558 if (sata_hba_inst == sata_hba_list) { 9559 sata_hba_list = sata_hba_inst->satahba_next; 9560 if (sata_hba_list) { 9561 sata_hba_list->satahba_prev = 9562 (struct sata_hba_inst *)NULL; 9563 } 9564 if (sata_hba_inst == sata_hba_list_tail) { 9565 sata_hba_list_tail = NULL; 9566 } 9567 } else if (sata_hba_inst == sata_hba_list_tail) { 9568 sata_hba_list_tail = sata_hba_inst->satahba_prev; 9569 if (sata_hba_list_tail) { 9570 sata_hba_list_tail->satahba_next = 9571 (struct sata_hba_inst *)NULL; 9572 } 9573 } else { 9574 sata_hba_inst->satahba_prev->satahba_next = 9575 sata_hba_inst->satahba_next; 9576 sata_hba_inst->satahba_next->satahba_prev = 9577 sata_hba_inst->satahba_prev; 9578 } 9579 mutex_exit(&sata_mutex); 9580 } 9581 9582 /* 9583 * Probe all SATA ports of the specified HBA instance. 9584 * The assumption is that there are no target and attachment point minor nodes 9585 * created by the boot subsystems, so we do not need to prune device tree. 9586 * 9587 * This function is called only from sata_hba_attach(). It does not have to 9588 * be protected by controller mutex, because the hba_attached flag is not set 9589 * yet and no one would be touching this HBA instance other than this thread. 9590 * Determines if port is active and what type of the device is attached 9591 * (if any). Allocates necessary structures for each port. 9592 * 9593 * An AP (Attachement Point) node is created for each SATA device port even 9594 * when there is no device attached. 9595 */ 9596 9597 static void 9598 sata_probe_ports(sata_hba_inst_t *sata_hba_inst) 9599 { 9600 dev_info_t *dip = SATA_DIP(sata_hba_inst); 9601 int ncport; 9602 sata_cport_info_t *cportinfo; 9603 sata_drive_info_t *drive; 9604 sata_device_t sata_device; 9605 int rval; 9606 dev_t minor_number; 9607 char name[16]; 9608 clock_t start_time, cur_time; 9609 9610 /* 9611 * Probe controller ports first, to find port status and 9612 * any port multiplier attached. 9613 */ 9614 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 9615 /* allocate cport structure */ 9616 cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP); 9617 ASSERT(cportinfo != NULL); 9618 mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL); 9619 9620 mutex_enter(&cportinfo->cport_mutex); 9621 9622 cportinfo->cport_addr.cport = ncport; 9623 cportinfo->cport_addr.pmport = 0; 9624 cportinfo->cport_addr.qual = SATA_ADDR_CPORT; 9625 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 9626 cportinfo->cport_state |= SATA_STATE_PROBING; 9627 SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo; 9628 9629 /* 9630 * Regardless if a port is usable or not, create 9631 * an attachment point 9632 */ 9633 mutex_exit(&cportinfo->cport_mutex); 9634 minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip), 9635 ncport, 0, SATA_ADDR_CPORT); 9636 (void) sprintf(name, "%d", ncport); 9637 if (ddi_create_minor_node(dip, name, S_IFCHR, 9638 minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) != 9639 DDI_SUCCESS) { 9640 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: " 9641 "cannot create SATA attachment point for port %d", 9642 ncport); 9643 } 9644 9645 /* Probe port */ 9646 start_time = ddi_get_lbolt(); 9647 reprobe_cport: 9648 sata_device.satadev_addr.cport = ncport; 9649 sata_device.satadev_addr.pmport = 0; 9650 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 9651 sata_device.satadev_rev = SATA_DEVICE_REV; 9652 9653 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 9654 (dip, &sata_device); 9655 9656 mutex_enter(&cportinfo->cport_mutex); 9657 cportinfo->cport_scr = sata_device.satadev_scr; 9658 if (rval != SATA_SUCCESS) { 9659 /* Something went wrong? Fail the port */ 9660 cportinfo->cport_state = SATA_PSTATE_FAILED; 9661 mutex_exit(&cportinfo->cport_mutex); 9662 continue; 9663 } 9664 cportinfo->cport_state &= ~SATA_STATE_PROBING; 9665 cportinfo->cport_state |= SATA_STATE_PROBED; 9666 cportinfo->cport_dev_type = sata_device.satadev_type; 9667 9668 cportinfo->cport_state |= SATA_STATE_READY; 9669 if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) { 9670 mutex_exit(&cportinfo->cport_mutex); 9671 continue; 9672 } 9673 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 9674 /* 9675 * There is some device attached. 9676 * Allocate device info structure 9677 */ 9678 if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) { 9679 mutex_exit(&cportinfo->cport_mutex); 9680 SATA_CPORTINFO_DRV_INFO(cportinfo) = 9681 kmem_zalloc(sizeof (sata_drive_info_t), 9682 KM_SLEEP); 9683 mutex_enter(&cportinfo->cport_mutex); 9684 } 9685 drive = SATA_CPORTINFO_DRV_INFO(cportinfo); 9686 drive->satadrv_addr = cportinfo->cport_addr; 9687 drive->satadrv_addr.qual = SATA_ADDR_DCPORT; 9688 drive->satadrv_type = cportinfo->cport_dev_type; 9689 drive->satadrv_state = SATA_STATE_UNKNOWN; 9690 9691 mutex_exit(&cportinfo->cport_mutex); 9692 if (sata_add_device(dip, sata_hba_inst, &sata_device) != 9693 SATA_SUCCESS) { 9694 /* 9695 * Plugged device was not correctly identified. 9696 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT 9697 */ 9698 cur_time = ddi_get_lbolt(); 9699 if ((cur_time - start_time) < 9700 drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) { 9701 /* sleep for a while */ 9702 delay(drv_usectohz( 9703 SATA_DEV_RETRY_DLY)); 9704 goto reprobe_cport; 9705 } 9706 } 9707 } else { /* SATA_DTYPE_PMULT */ 9708 mutex_exit(&cportinfo->cport_mutex); 9709 9710 /* Allocate sata_pmult_info and sata_pmport_info */ 9711 if (sata_alloc_pmult(sata_hba_inst, &sata_device) != 9712 SATA_SUCCESS) 9713 continue; 9714 9715 /* Log the information of the port multiplier */ 9716 sata_show_pmult_info(sata_hba_inst, &sata_device); 9717 9718 /* Probe its pmports */ 9719 sata_probe_pmports(sata_hba_inst, ncport); 9720 } 9721 } 9722 } 9723 9724 /* 9725 * Probe all device ports behind a port multiplier. 9726 * 9727 * PMult-related structure should be allocated before by sata_alloc_pmult(). 9728 * 9729 * NOTE1: Only called from sata_probe_ports() 9730 * NOTE2: No mutex should be hold. 9731 */ 9732 static void 9733 sata_probe_pmports(sata_hba_inst_t *sata_hba_inst, uint8_t ncport) 9734 { 9735 dev_info_t *dip = SATA_DIP(sata_hba_inst); 9736 sata_pmult_info_t *pmultinfo = NULL; 9737 sata_pmport_info_t *pmportinfo = NULL; 9738 sata_drive_info_t *drive = NULL; 9739 sata_device_t sata_device; 9740 9741 clock_t start_time, cur_time; 9742 int npmport; 9743 int rval; 9744 9745 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, ncport); 9746 9747 /* Probe Port Multiplier ports */ 9748 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; npmport++) { 9749 pmportinfo = pmultinfo->pmult_dev_port[npmport]; 9750 start_time = ddi_get_lbolt(); 9751 reprobe_pmport: 9752 sata_device.satadev_addr.cport = ncport; 9753 sata_device.satadev_addr.pmport = npmport; 9754 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT; 9755 sata_device.satadev_rev = SATA_DEVICE_REV; 9756 9757 /* Let HBA driver probe it. */ 9758 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 9759 (dip, &sata_device); 9760 mutex_enter(&pmportinfo->pmport_mutex); 9761 9762 pmportinfo->pmport_scr = sata_device.satadev_scr; 9763 9764 if (rval != SATA_SUCCESS) { 9765 pmportinfo->pmport_state = 9766 SATA_PSTATE_FAILED; 9767 mutex_exit(&pmportinfo->pmport_mutex); 9768 continue; 9769 } 9770 pmportinfo->pmport_state &= ~SATA_STATE_PROBING; 9771 pmportinfo->pmport_state |= SATA_STATE_PROBED; 9772 pmportinfo->pmport_dev_type = sata_device.satadev_type; 9773 9774 pmportinfo->pmport_state |= SATA_STATE_READY; 9775 if (pmportinfo->pmport_dev_type == 9776 SATA_DTYPE_NONE) { 9777 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 9778 "no device found at port %d:%d", ncport, npmport); 9779 mutex_exit(&pmportinfo->pmport_mutex); 9780 continue; 9781 } 9782 /* Port multipliers cannot be chained */ 9783 ASSERT(pmportinfo->pmport_dev_type != SATA_DTYPE_PMULT); 9784 /* 9785 * There is something attached to Port 9786 * Multiplier device port 9787 * Allocate device info structure 9788 */ 9789 if (pmportinfo->pmport_sata_drive == NULL) { 9790 mutex_exit(&pmportinfo->pmport_mutex); 9791 pmportinfo->pmport_sata_drive = 9792 kmem_zalloc(sizeof (sata_drive_info_t), KM_SLEEP); 9793 mutex_enter(&pmportinfo->pmport_mutex); 9794 } 9795 drive = pmportinfo->pmport_sata_drive; 9796 drive->satadrv_addr.cport = pmportinfo->pmport_addr.cport; 9797 drive->satadrv_addr.pmport = npmport; 9798 drive->satadrv_addr.qual = SATA_ADDR_DPMPORT; 9799 drive->satadrv_type = pmportinfo-> pmport_dev_type; 9800 drive->satadrv_state = SATA_STATE_UNKNOWN; 9801 9802 mutex_exit(&pmportinfo->pmport_mutex); 9803 rval = sata_add_device(dip, sata_hba_inst, &sata_device); 9804 9805 if (rval != SATA_SUCCESS) { 9806 /* 9807 * Plugged device was not correctly identified. 9808 * Retry, within the SATA_DEV_IDENTIFY_TIMEOUT 9809 */ 9810 cur_time = ddi_get_lbolt(); 9811 if ((cur_time - start_time) < drv_usectohz( 9812 SATA_DEV_IDENTIFY_TIMEOUT)) { 9813 /* sleep for a while */ 9814 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 9815 goto reprobe_pmport; 9816 } 9817 } 9818 } 9819 } 9820 9821 /* 9822 * Add SATA device for specified HBA instance & port (SCSI target 9823 * device nodes). 9824 * This function is called (indirectly) only from sata_hba_attach(). 9825 * A target node is created when there is a supported type device attached, 9826 * but may be removed if it cannot be put online. 9827 * 9828 * This function cannot be called from an interrupt context. 9829 * 9830 * Create target nodes for disk, CD/DVD, Tape and ATAPI disk devices 9831 * 9832 * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when 9833 * device identification failed - adding a device could be retried. 9834 * 9835 */ 9836 static int 9837 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, 9838 sata_device_t *sata_device) 9839 { 9840 sata_cport_info_t *cportinfo; 9841 sata_pmult_info_t *pminfo; 9842 sata_pmport_info_t *pmportinfo; 9843 dev_info_t *cdip; /* child dip */ 9844 sata_address_t *saddr = &sata_device->satadev_addr; 9845 uint8_t cport, pmport; 9846 int rval; 9847 9848 cport = saddr->cport; 9849 pmport = saddr->pmport; 9850 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 9851 ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE); 9852 9853 /* 9854 * Some device is attached to a controller port. 9855 * We rely on controllers distinquishing between no-device, 9856 * attached port multiplier and other kind of attached device. 9857 * We need to get Identify Device data and determine 9858 * positively the dev type before trying to attach 9859 * the target driver. 9860 */ 9861 sata_device->satadev_rev = SATA_DEVICE_REV; 9862 switch (saddr->qual) { 9863 case SATA_ADDR_CPORT: 9864 /* 9865 * Add a non-port-multiplier device at controller port. 9866 */ 9867 saddr->qual = SATA_ADDR_DCPORT; 9868 9869 rval = sata_probe_device(sata_hba_inst, sata_device); 9870 if (rval != SATA_SUCCESS || 9871 sata_device->satadev_type == SATA_DTYPE_UNKNOWN) 9872 return (SATA_FAILURE); 9873 9874 mutex_enter(&cportinfo->cport_mutex); 9875 sata_show_drive_info(sata_hba_inst, 9876 SATA_CPORTINFO_DRV_INFO(cportinfo)); 9877 9878 if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) { 9879 /* 9880 * Could not determine device type or 9881 * a device is not supported. 9882 * Degrade this device to unknown. 9883 */ 9884 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 9885 mutex_exit(&cportinfo->cport_mutex); 9886 return (SATA_SUCCESS); 9887 } 9888 cportinfo->cport_dev_type = sata_device->satadev_type; 9889 cportinfo->cport_tgtnode_clean = B_TRUE; 9890 mutex_exit(&cportinfo->cport_mutex); 9891 9892 /* 9893 * Initialize device to the desired state. Even if it 9894 * fails, the device will still attach but syslog 9895 * will show the warning. 9896 */ 9897 if (sata_initialize_device(sata_hba_inst, 9898 SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) { 9899 /* Retry */ 9900 rval = sata_initialize_device(sata_hba_inst, 9901 SATA_CPORTINFO_DRV_INFO(cportinfo)); 9902 9903 if (rval == SATA_RETRY) 9904 sata_log(sata_hba_inst, CE_WARN, 9905 "SATA device at port %d - " 9906 "default device features could not be set." 9907 " Device may not operate as expected.", 9908 cport); 9909 } 9910 9911 cdip = sata_create_target_node(pdip, sata_hba_inst, saddr); 9912 if (cdip == NULL) { 9913 /* 9914 * Attaching target node failed. 9915 * We retain sata_drive_info structure... 9916 */ 9917 return (SATA_SUCCESS); 9918 } 9919 9920 mutex_enter(&cportinfo->cport_mutex); 9921 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 9922 satadrv_state = SATA_STATE_READY; 9923 mutex_exit(&cportinfo->cport_mutex); 9924 9925 break; 9926 9927 case SATA_ADDR_PMPORT: 9928 saddr->qual = SATA_ADDR_DPMPORT; 9929 9930 mutex_enter(&cportinfo->cport_mutex); 9931 /* It must be a Port Multiplier at the controller port */ 9932 ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT); 9933 9934 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 9935 pmportinfo = pminfo->pmult_dev_port[saddr->pmport]; 9936 mutex_exit(&cportinfo->cport_mutex); 9937 9938 rval = sata_probe_device(sata_hba_inst, sata_device); 9939 if (rval != SATA_SUCCESS || 9940 sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 9941 return (SATA_FAILURE); 9942 } 9943 9944 mutex_enter(&pmportinfo->pmport_mutex); 9945 sata_show_drive_info(sata_hba_inst, 9946 SATA_PMPORTINFO_DRV_INFO(pmportinfo)); 9947 9948 if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) { 9949 /* 9950 * Could not determine device type. 9951 * Degrade this device to unknown. 9952 */ 9953 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN; 9954 mutex_exit(&pmportinfo->pmport_mutex); 9955 return (SATA_SUCCESS); 9956 } 9957 pmportinfo->pmport_dev_type = sata_device->satadev_type; 9958 pmportinfo->pmport_tgtnode_clean = B_TRUE; 9959 mutex_exit(&pmportinfo->pmport_mutex); 9960 9961 /* 9962 * Initialize device to the desired state. 9963 * Even if it fails, the device will still 9964 * attach but syslog will show the warning. 9965 */ 9966 if (sata_initialize_device(sata_hba_inst, 9967 pmportinfo->pmport_sata_drive) != SATA_SUCCESS) { 9968 /* Retry */ 9969 rval = sata_initialize_device(sata_hba_inst, 9970 pmportinfo->pmport_sata_drive); 9971 9972 if (rval == SATA_RETRY) 9973 sata_log(sata_hba_inst, CE_WARN, 9974 "SATA device at port %d:%d - " 9975 "default device features could not be set." 9976 " Device may not operate as expected.", 9977 cport, pmport); 9978 } 9979 9980 cdip = sata_create_target_node(pdip, sata_hba_inst, saddr); 9981 if (cdip == NULL) { 9982 /* 9983 * Attaching target node failed. 9984 * We retain sata_drive_info structure... 9985 */ 9986 return (SATA_SUCCESS); 9987 } 9988 mutex_enter(&pmportinfo->pmport_mutex); 9989 pmportinfo->pmport_sata_drive->satadrv_state |= 9990 SATA_STATE_READY; 9991 mutex_exit(&pmportinfo->pmport_mutex); 9992 9993 break; 9994 9995 default: 9996 return (SATA_FAILURE); 9997 } 9998 9999 return (SATA_SUCCESS); 10000 } 10001 10002 /* 10003 * Clean up target node at specific address. 10004 * 10005 * NOTE: No Mutex should be hold. 10006 */ 10007 static int 10008 sata_offline_device(sata_hba_inst_t *sata_hba_inst, 10009 sata_device_t *sata_device, sata_drive_info_t *sdinfo) 10010 { 10011 uint8_t cport, pmport, qual; 10012 dev_info_t *tdip; 10013 10014 cport = sata_device->satadev_addr.cport; 10015 pmport = sata_device->satadev_addr.pmport; 10016 qual = sata_device->satadev_addr.qual; 10017 10018 if (qual == SATA_ADDR_DCPORT) { 10019 SATA_LOG_D((sata_hba_inst, CE_WARN, 10020 "sata_hba_ioctl: disconnect device at port %d", cport)); 10021 } else { 10022 SATA_LOG_D((sata_hba_inst, CE_WARN, 10023 "sata_hba_ioctl: disconnect device at port %d:%d", 10024 cport, pmport)); 10025 } 10026 10027 /* We are addressing attached device, not a port */ 10028 sata_device->satadev_addr.qual = 10029 sdinfo->satadrv_addr.qual; 10030 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 10031 &sata_device->satadev_addr); 10032 if (tdip != NULL && ndi_devi_offline(tdip, 10033 NDI_DEVI_REMOVE) != NDI_SUCCESS) { 10034 /* 10035 * Problem : 10036 * The target node remained attached. 10037 * This happens when the device file was open 10038 * or a node was waiting for resources. 10039 * Cannot do anything about it. 10040 */ 10041 if (qual == SATA_ADDR_DCPORT) { 10042 SATA_LOG_D((sata_hba_inst, CE_WARN, 10043 "sata_hba_ioctl: disconnect: could " 10044 "not unconfigure device before " 10045 "disconnecting the SATA port %d", 10046 cport)); 10047 } else { 10048 SATA_LOG_D((sata_hba_inst, CE_WARN, 10049 "sata_hba_ioctl: disconnect: could " 10050 "not unconfigure device before " 10051 "disconnecting the SATA port %d:%d", 10052 cport, pmport)); 10053 } 10054 /* 10055 * Set DEVICE REMOVED state in the target 10056 * node. It will prevent access to the device 10057 * even when a new device is attached, until 10058 * the old target node is released, removed and 10059 * recreated for a new device. 10060 */ 10061 sata_set_device_removed(tdip); 10062 10063 /* 10064 * Instruct event daemon to try the target 10065 * node cleanup later. 10066 */ 10067 sata_set_target_node_cleanup( 10068 sata_hba_inst, &sata_device->satadev_addr); 10069 } 10070 10071 10072 return (SATA_SUCCESS); 10073 } 10074 10075 10076 /* 10077 * Create scsi target node for attached device, create node properties and 10078 * attach the node. 10079 * The node could be removed if the device onlining fails. 10080 * 10081 * A dev_info_t pointer is returned if operation is successful, NULL is 10082 * returned otherwise. 10083 */ 10084 10085 static dev_info_t * 10086 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst, 10087 sata_address_t *sata_addr) 10088 { 10089 dev_info_t *cdip = NULL; 10090 int rval; 10091 char *nname = NULL; 10092 char **compatible = NULL; 10093 int ncompatible; 10094 struct scsi_inquiry inq; 10095 sata_device_t sata_device; 10096 sata_drive_info_t *sdinfo; 10097 int target; 10098 int i; 10099 10100 sata_device.satadev_rev = SATA_DEVICE_REV; 10101 sata_device.satadev_addr = *sata_addr; 10102 10103 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport))); 10104 10105 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 10106 10107 target = SATA_TO_SCSI_TARGET(sata_addr->cport, 10108 sata_addr->pmport, sata_addr->qual); 10109 10110 if (sdinfo == NULL) { 10111 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 10112 sata_addr->cport))); 10113 SATA_LOG_D((sata_hba_inst, CE_WARN, 10114 "sata_create_target_node: no sdinfo for target %x", 10115 target)); 10116 return (NULL); 10117 } 10118 10119 /* 10120 * create or get scsi inquiry data, expected by 10121 * scsi_hba_nodename_compatible_get() 10122 * SATA hard disks get Identify Data translated into Inguiry Data. 10123 * ATAPI devices respond directly to Inquiry request. 10124 */ 10125 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 10126 sata_identdev_to_inquiry(sata_hba_inst, sdinfo, 10127 (uint8_t *)&inq); 10128 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 10129 sata_addr->cport))); 10130 } else { /* Assume supported ATAPI device */ 10131 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 10132 sata_addr->cport))); 10133 if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr, 10134 &inq) == SATA_FAILURE) 10135 return (NULL); 10136 /* 10137 * Save supported ATAPI transport version 10138 */ 10139 sdinfo->satadrv_atapi_trans_ver = 10140 SATA_ATAPI_TRANS_VERSION(&inq); 10141 } 10142 10143 /* determine the node name and compatible */ 10144 scsi_hba_nodename_compatible_get(&inq, NULL, 10145 inq.inq_dtype, NULL, &nname, &compatible, &ncompatible); 10146 10147 #ifdef SATA_DEBUG 10148 if (sata_debug_flags & SATA_DBG_NODES) { 10149 if (nname == NULL) { 10150 cmn_err(CE_NOTE, "sata_create_target_node: " 10151 "cannot determine nodename for target %d\n", 10152 target); 10153 } else { 10154 cmn_err(CE_WARN, "sata_create_target_node: " 10155 "target %d nodename: %s\n", target, nname); 10156 } 10157 if (compatible == NULL) { 10158 cmn_err(CE_WARN, 10159 "sata_create_target_node: no compatible name\n"); 10160 } else { 10161 for (i = 0; i < ncompatible; i++) { 10162 cmn_err(CE_WARN, "sata_create_target_node: " 10163 "compatible name: %s\n", compatible[i]); 10164 } 10165 } 10166 } 10167 #endif 10168 10169 /* if nodename can't be determined, log error and exit */ 10170 if (nname == NULL) { 10171 SATA_LOG_D((sata_hba_inst, CE_WARN, 10172 "sata_create_target_node: cannot determine nodename " 10173 "for target %d\n", target)); 10174 scsi_hba_nodename_compatible_free(nname, compatible); 10175 return (NULL); 10176 } 10177 /* 10178 * Create scsi target node 10179 */ 10180 ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip); 10181 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 10182 "device-type", "scsi"); 10183 10184 if (rval != DDI_PROP_SUCCESS) { 10185 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 10186 "updating device_type prop failed %d", rval)); 10187 goto fail; 10188 } 10189 10190 /* 10191 * Create target node properties: target & lun 10192 */ 10193 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target); 10194 if (rval != DDI_PROP_SUCCESS) { 10195 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 10196 "updating target prop failed %d", rval)); 10197 goto fail; 10198 } 10199 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0); 10200 if (rval != DDI_PROP_SUCCESS) { 10201 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 10202 "updating target prop failed %d", rval)); 10203 goto fail; 10204 } 10205 10206 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 10207 /* 10208 * Add "variant" property 10209 */ 10210 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 10211 "variant", "atapi"); 10212 if (rval != DDI_PROP_SUCCESS) { 10213 SATA_LOG_D((sata_hba_inst, CE_WARN, 10214 "sata_create_target_node: variant atapi " 10215 "property could not be created: %d", rval)); 10216 goto fail; 10217 } 10218 } 10219 /* decorate the node with compatible */ 10220 if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible", 10221 compatible, ncompatible) != DDI_PROP_SUCCESS) { 10222 SATA_LOG_D((sata_hba_inst, CE_WARN, 10223 "sata_create_target_node: FAIL compatible props cdip 0x%p", 10224 (void *)cdip)); 10225 goto fail; 10226 } 10227 10228 10229 /* 10230 * Now, try to attach the driver. If probing of the device fails, 10231 * the target node may be removed 10232 */ 10233 rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH); 10234 10235 scsi_hba_nodename_compatible_free(nname, compatible); 10236 10237 if (rval == NDI_SUCCESS) 10238 return (cdip); 10239 10240 /* target node was removed - are we sure? */ 10241 return (NULL); 10242 10243 fail: 10244 scsi_hba_nodename_compatible_free(nname, compatible); 10245 ddi_prop_remove_all(cdip); 10246 rval = ndi_devi_free(cdip); 10247 if (rval != NDI_SUCCESS) { 10248 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 10249 "node removal failed %d", rval)); 10250 } 10251 sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: " 10252 "cannot create target node for SATA device at port %d", 10253 sata_addr->cport); 10254 return (NULL); 10255 } 10256 10257 /* 10258 * Remove a target node. 10259 */ 10260 static void 10261 sata_remove_target_node(sata_hba_inst_t *sata_hba_inst, 10262 sata_address_t *sata_addr) 10263 { 10264 dev_info_t *tdip; 10265 uint8_t cport = sata_addr->cport; 10266 uint8_t pmport = sata_addr->pmport; 10267 uint8_t qual = sata_addr->qual; 10268 10269 /* Note the sata daemon uses the address of the port/pmport */ 10270 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT); 10271 10272 /* Remove target node */ 10273 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), cport, pmport); 10274 if (tdip != NULL) { 10275 /* 10276 * Target node exists. Unconfigure device 10277 * then remove the target node (one ndi 10278 * operation). 10279 */ 10280 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) { 10281 /* 10282 * PROBLEM - no device, but target node remained. This 10283 * happens when the file was open or node was waiting 10284 * for resources. 10285 */ 10286 SATA_LOG_D((sata_hba_inst, CE_WARN, 10287 "sata_remove_target_node: " 10288 "Failed to remove target node for " 10289 "detached SATA device.")); 10290 /* 10291 * Set target node state to DEVI_DEVICE_REMOVED. But 10292 * re-check first that the node still exists. 10293 */ 10294 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 10295 cport, pmport); 10296 if (tdip != NULL) { 10297 sata_set_device_removed(tdip); 10298 /* 10299 * Instruct event daemon to retry the cleanup 10300 * later. 10301 */ 10302 sata_set_target_node_cleanup(sata_hba_inst, 10303 sata_addr); 10304 } 10305 } 10306 10307 if (qual == SATA_ADDR_CPORT) 10308 sata_log(sata_hba_inst, CE_WARN, 10309 "SATA device detached at port %d", cport); 10310 else 10311 sata_log(sata_hba_inst, CE_WARN, 10312 "SATA device detached at port %d:%d", 10313 cport, pmport); 10314 } 10315 #ifdef SATA_DEBUG 10316 else { 10317 if (qual == SATA_ADDR_CPORT) 10318 sata_log(sata_hba_inst, CE_WARN, 10319 "target node not found at port %d", cport); 10320 else 10321 sata_log(sata_hba_inst, CE_WARN, 10322 "target node not found at port %d:%d", 10323 cport, pmport); 10324 } 10325 #endif 10326 } 10327 10328 10329 /* 10330 * Re-probe sata port, check for a device and attach info 10331 * structures when necessary. Identify Device data is fetched, if possible. 10332 * Assumption: sata address is already validated. 10333 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of 10334 * the presence of a device and its type. 10335 * 10336 * flag arg specifies that the function should try multiple times to identify 10337 * device type and to initialize it, or it should return immediately on failure. 10338 * SATA_DEV_IDENTIFY_RETRY - retry 10339 * SATA_DEV_IDENTIFY_NORETRY - no retry 10340 * 10341 * SATA_FAILURE is returned if one of the operations failed. 10342 * 10343 * This function cannot be called in interrupt context - it may sleep. 10344 * 10345 * Note: Port multiplier is supported. 10346 */ 10347 static int 10348 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 10349 int flag) 10350 { 10351 sata_cport_info_t *cportinfo; 10352 sata_pmult_info_t *pmultinfo; 10353 sata_drive_info_t *sdinfo, *osdinfo; 10354 boolean_t init_device = B_FALSE; 10355 int prev_device_type = SATA_DTYPE_NONE; 10356 int prev_device_settings = 0; 10357 int prev_device_state = 0; 10358 clock_t start_time; 10359 int retry = B_FALSE; 10360 uint8_t cport = sata_device->satadev_addr.cport; 10361 int rval_probe, rval_init; 10362 10363 /* 10364 * If target is pmport, sata_reprobe_pmport() will handle it. 10365 */ 10366 if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT || 10367 sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) 10368 return (sata_reprobe_pmport(sata_hba_inst, sata_device, flag)); 10369 10370 /* We only care about host sata cport for now */ 10371 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 10372 sata_device->satadev_addr.cport); 10373 10374 /* 10375 * If a port multiplier was previously attached (we have no idea it 10376 * still there or not), sata_reprobe_pmult() will handle it. 10377 */ 10378 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) 10379 return (sata_reprobe_pmult(sata_hba_inst, sata_device, flag)); 10380 10381 /* Store sata_drive_info when a non-pmult device was attached. */ 10382 osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 10383 if (osdinfo != NULL) { 10384 /* 10385 * We are re-probing port with a previously attached device. 10386 * Save previous device type and settings. 10387 */ 10388 prev_device_type = cportinfo->cport_dev_type; 10389 prev_device_settings = osdinfo->satadrv_settings; 10390 prev_device_state = osdinfo->satadrv_state; 10391 } 10392 if (flag == SATA_DEV_IDENTIFY_RETRY) { 10393 start_time = ddi_get_lbolt(); 10394 retry = B_TRUE; 10395 } 10396 retry_probe: 10397 10398 /* probe port */ 10399 mutex_enter(&cportinfo->cport_mutex); 10400 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 10401 cportinfo->cport_state |= SATA_STATE_PROBING; 10402 mutex_exit(&cportinfo->cport_mutex); 10403 10404 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 10405 (SATA_DIP(sata_hba_inst), sata_device); 10406 10407 mutex_enter(&cportinfo->cport_mutex); 10408 if (rval_probe != SATA_SUCCESS) { 10409 cportinfo->cport_state = SATA_PSTATE_FAILED; 10410 mutex_exit(&cportinfo->cport_mutex); 10411 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: " 10412 "SATA port %d probing failed", 10413 cportinfo->cport_addr.cport)); 10414 return (SATA_FAILURE); 10415 } 10416 10417 /* 10418 * update sata port state and set device type 10419 */ 10420 sata_update_port_info(sata_hba_inst, sata_device); 10421 cportinfo->cport_state &= ~SATA_STATE_PROBING; 10422 10423 /* 10424 * Sanity check - Port is active? Is the link active? 10425 * Is there any device attached? 10426 */ 10427 if ((cportinfo->cport_state & 10428 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 10429 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 10430 SATA_PORT_DEVLINK_UP) { 10431 /* 10432 * Port in non-usable state or no link active/no device. 10433 * Free info structure if necessary (direct attached drive 10434 * only, for now! 10435 */ 10436 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 10437 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 10438 /* Add here differentiation for device attached or not */ 10439 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 10440 mutex_exit(&cportinfo->cport_mutex); 10441 if (sdinfo != NULL) 10442 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10443 return (SATA_SUCCESS); 10444 } 10445 10446 cportinfo->cport_state |= SATA_STATE_READY; 10447 cportinfo->cport_state |= SATA_STATE_PROBED; 10448 10449 cportinfo->cport_dev_type = sata_device->satadev_type; 10450 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 10451 10452 /* 10453 * If we are re-probing the port, there may be 10454 * sata_drive_info structure attached 10455 */ 10456 if (sata_device->satadev_type == SATA_DTYPE_NONE) { 10457 10458 /* 10459 * There is no device, so remove device info structure, 10460 * if necessary. 10461 */ 10462 /* Device change: Drive -> None */ 10463 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 10464 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 10465 if (sdinfo != NULL) { 10466 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10467 sata_log(sata_hba_inst, CE_WARN, 10468 "SATA device detached " 10469 "from port %d", cportinfo->cport_addr.cport); 10470 } 10471 mutex_exit(&cportinfo->cport_mutex); 10472 return (SATA_SUCCESS); 10473 10474 } 10475 10476 if (sata_device->satadev_type != SATA_DTYPE_PMULT) { 10477 10478 /* Device (may) change: Drive -> Drive */ 10479 if (sdinfo == NULL) { 10480 /* 10481 * There is some device attached, but there is 10482 * no sata_drive_info structure - allocate one 10483 */ 10484 mutex_exit(&cportinfo->cport_mutex); 10485 sdinfo = kmem_zalloc( 10486 sizeof (sata_drive_info_t), KM_SLEEP); 10487 mutex_enter(&cportinfo->cport_mutex); 10488 /* 10489 * Recheck, that the port state did not change when we 10490 * released mutex. 10491 */ 10492 if (cportinfo->cport_state & SATA_STATE_READY) { 10493 SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo; 10494 sdinfo->satadrv_addr = cportinfo->cport_addr; 10495 sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT; 10496 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 10497 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 10498 } else { 10499 /* 10500 * Port is not in ready state, we 10501 * cannot attach a device. 10502 */ 10503 mutex_exit(&cportinfo->cport_mutex); 10504 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10505 return (SATA_SUCCESS); 10506 } 10507 /* 10508 * Since we are adding device, presumably new one, 10509 * indicate that it should be initalized, 10510 * as well as some internal framework states). 10511 */ 10512 init_device = B_TRUE; 10513 } 10514 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 10515 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual; 10516 } else { 10517 /* Device change: Drive -> PMult */ 10518 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 10519 if (sdinfo != NULL) { 10520 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10521 sata_log(sata_hba_inst, CE_WARN, 10522 "SATA device detached " 10523 "from port %d", cportinfo->cport_addr.cport); 10524 } 10525 10526 sata_log(sata_hba_inst, CE_WARN, 10527 "SATA port multiplier detected at port %d", 10528 cportinfo->cport_addr.cport); 10529 10530 mutex_exit(&cportinfo->cport_mutex); 10531 if (sata_alloc_pmult(sata_hba_inst, sata_device) != 10532 SATA_SUCCESS) 10533 return (SATA_FAILURE); 10534 sata_show_pmult_info(sata_hba_inst, sata_device); 10535 mutex_enter(&cportinfo->cport_mutex); 10536 10537 /* 10538 * Mark all the port multiplier port behind the port 10539 * multiplier behind with link events, so that the sata daemon 10540 * will update their status. 10541 */ 10542 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 10543 pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET; 10544 mutex_exit(&cportinfo->cport_mutex); 10545 return (SATA_SUCCESS); 10546 } 10547 mutex_exit(&cportinfo->cport_mutex); 10548 10549 /* 10550 * Figure out what kind of device we are really 10551 * dealing with. Failure of identifying device does not fail this 10552 * function. 10553 */ 10554 rval_probe = sata_probe_device(sata_hba_inst, sata_device); 10555 rval_init = SATA_FAILURE; 10556 mutex_enter(&cportinfo->cport_mutex); 10557 if (rval_probe == SATA_SUCCESS) { 10558 /* 10559 * If we are dealing with the same type of a device as before, 10560 * restore its settings flags. 10561 */ 10562 if (osdinfo != NULL && 10563 sata_device->satadev_type == prev_device_type) 10564 sdinfo->satadrv_settings = prev_device_settings; 10565 10566 mutex_exit(&cportinfo->cport_mutex); 10567 rval_init = SATA_SUCCESS; 10568 /* Set initial device features, if necessary */ 10569 if (init_device == B_TRUE) { 10570 rval_init = sata_initialize_device(sata_hba_inst, 10571 sdinfo); 10572 } 10573 if (rval_init == SATA_SUCCESS) 10574 return (rval_init); 10575 /* else we will retry if retry was asked for */ 10576 10577 } else { 10578 /* 10579 * If there was some device info before we probe the device, 10580 * restore previous device setting, so we can retry from scratch 10581 * later. Providing, of course, that device has not disapear 10582 * during probing process. 10583 */ 10584 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 10585 if (osdinfo != NULL) { 10586 cportinfo->cport_dev_type = prev_device_type; 10587 sdinfo->satadrv_type = prev_device_type; 10588 sdinfo->satadrv_state = prev_device_state; 10589 } 10590 } else { 10591 /* device is gone */ 10592 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10593 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 10594 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 10595 mutex_exit(&cportinfo->cport_mutex); 10596 return (SATA_SUCCESS); 10597 } 10598 mutex_exit(&cportinfo->cport_mutex); 10599 } 10600 10601 if (retry) { 10602 clock_t cur_time = ddi_get_lbolt(); 10603 /* 10604 * A device was not successfully identified or initialized. 10605 * Track retry time for device identification. 10606 */ 10607 if ((cur_time - start_time) < 10608 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 10609 /* sleep for a while */ 10610 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 10611 goto retry_probe; 10612 } 10613 /* else no more retries */ 10614 mutex_enter(&cportinfo->cport_mutex); 10615 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 10616 if (rval_init == SATA_RETRY) { 10617 /* 10618 * Setting drive features have failed, but 10619 * because the drive is still accessible, 10620 * keep it and emit a warning message. 10621 */ 10622 sata_log(sata_hba_inst, CE_WARN, 10623 "SATA device at port %d - desired " 10624 "drive features could not be set. " 10625 "Device may not operate as expected.", 10626 cportinfo->cport_addr.cport); 10627 } else { 10628 SATA_CPORTINFO_DRV_INFO(cportinfo)-> 10629 satadrv_state = SATA_DSTATE_FAILED; 10630 } 10631 } 10632 mutex_exit(&cportinfo->cport_mutex); 10633 } 10634 return (SATA_SUCCESS); 10635 } 10636 10637 /* 10638 * Reprobe a controller port that connected to a port multiplier. 10639 * 10640 * NOTE: No Mutex should be hold. 10641 */ 10642 static int 10643 sata_reprobe_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 10644 int flag) 10645 { 10646 _NOTE(ARGUNUSED(flag)) 10647 sata_cport_info_t *cportinfo; 10648 sata_pmult_info_t *pmultinfo; 10649 uint8_t cport = sata_device->satadev_addr.cport; 10650 int rval_probe; 10651 10652 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 10653 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 10654 10655 /* probe port */ 10656 mutex_enter(&cportinfo->cport_mutex); 10657 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 10658 cportinfo->cport_state |= SATA_STATE_PROBING; 10659 mutex_exit(&cportinfo->cport_mutex); 10660 10661 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 10662 (SATA_DIP(sata_hba_inst), sata_device); 10663 10664 mutex_enter(&cportinfo->cport_mutex); 10665 if (rval_probe != SATA_SUCCESS) { 10666 cportinfo->cport_state = SATA_PSTATE_FAILED; 10667 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmult: " 10668 "SATA port %d probing failed", cport)); 10669 sata_log(sata_hba_inst, CE_WARN, 10670 "SATA port multiplier detached at port %d", cport); 10671 mutex_exit(&cportinfo->cport_mutex); 10672 sata_free_pmult(sata_hba_inst, sata_device); 10673 return (SATA_FAILURE); 10674 } 10675 10676 /* 10677 * update sata port state and set device type 10678 */ 10679 sata_update_port_info(sata_hba_inst, sata_device); 10680 cportinfo->cport_state &= ~SATA_STATE_PROBING; 10681 cportinfo->cport_state |= SATA_STATE_PROBED; 10682 10683 /* 10684 * Sanity check - Port is active? Is the link active? 10685 * Is there any device attached? 10686 */ 10687 if ((cportinfo->cport_state & 10688 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 10689 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 10690 SATA_PORT_DEVLINK_UP || 10691 (sata_device->satadev_type == SATA_DTYPE_NONE)) { 10692 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 10693 mutex_exit(&cportinfo->cport_mutex); 10694 sata_free_pmult(sata_hba_inst, sata_device); 10695 sata_log(sata_hba_inst, CE_WARN, 10696 "SATA port multiplier detached at port %d", cport); 10697 return (SATA_SUCCESS); 10698 } 10699 10700 /* 10701 * Device changed: PMult -> Non-PMult 10702 * 10703 * This situation is uncommon, most possibly being caused by errors 10704 * after which the port multiplier is not correct initialized and 10705 * recognized. In that case the new device will be marked as unknown 10706 * and will not be automatically probed in this routine. Instead 10707 * system administrator could manually restart it via cfgadm(1M). 10708 */ 10709 if (sata_device->satadev_type != SATA_DTYPE_PMULT) { 10710 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 10711 mutex_exit(&cportinfo->cport_mutex); 10712 sata_free_pmult(sata_hba_inst, sata_device); 10713 sata_log(sata_hba_inst, CE_WARN, 10714 "SATA port multiplier detached at port %d", cport); 10715 return (SATA_FAILURE); 10716 } 10717 10718 /* 10719 * Now we know it is a port multiplier. However, if this is not the 10720 * previously attached port multiplier - they may have different 10721 * pmport numbers - we need to re-allocate data structures for every 10722 * pmport and drive. 10723 * 10724 * Port multipliers of the same model have identical values in these 10725 * registers, so it is still necessary to update the information of 10726 * all drives attached to the previous port multiplier afterwards. 10727 */ 10728 /* Device changed: PMult -> another PMult */ 10729 mutex_exit(&cportinfo->cport_mutex); 10730 sata_free_pmult(sata_hba_inst, sata_device); 10731 if (sata_alloc_pmult(sata_hba_inst, sata_device) != SATA_SUCCESS) 10732 return (SATA_FAILURE); 10733 mutex_enter(&cportinfo->cport_mutex); 10734 10735 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 10736 "SATA port multiplier [changed] at port %d", cport); 10737 sata_log(sata_hba_inst, CE_WARN, 10738 "SATA port multiplier detected at port %d", cport); 10739 10740 /* 10741 * Mark all the port multiplier port behind the port 10742 * multiplier behind with link events, so that the sata daemon 10743 * will update their status. 10744 */ 10745 pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET; 10746 mutex_exit(&cportinfo->cport_mutex); 10747 10748 return (SATA_SUCCESS); 10749 } 10750 10751 /* 10752 * Re-probe a port multiplier port, check for a device and attach info 10753 * structures when necessary. Identify Device data is fetched, if possible. 10754 * Assumption: sata address is already validated as port multiplier port. 10755 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of 10756 * the presence of a device and its type. 10757 * 10758 * flag arg specifies that the function should try multiple times to identify 10759 * device type and to initialize it, or it should return immediately on failure. 10760 * SATA_DEV_IDENTIFY_RETRY - retry 10761 * SATA_DEV_IDENTIFY_NORETRY - no retry 10762 * 10763 * SATA_FAILURE is returned if one of the operations failed. 10764 * 10765 * This function cannot be called in interrupt context - it may sleep. 10766 * 10767 * NOTE: Should be only called by sata_probe_port() in case target port is a 10768 * port multiplier port. 10769 * NOTE: No Mutex should be hold. 10770 */ 10771 static int 10772 sata_reprobe_pmport(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 10773 int flag) 10774 { 10775 sata_cport_info_t *cportinfo = NULL; 10776 sata_pmport_info_t *pmportinfo = NULL; 10777 sata_drive_info_t *sdinfo, *osdinfo; 10778 sata_device_t sdevice; 10779 boolean_t init_device = B_FALSE; 10780 int prev_device_type = SATA_DTYPE_NONE; 10781 int prev_device_settings = 0; 10782 int prev_device_state = 0; 10783 clock_t start_time; 10784 uint8_t cport = sata_device->satadev_addr.cport; 10785 uint8_t pmport = sata_device->satadev_addr.pmport; 10786 int rval; 10787 10788 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 10789 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 10790 osdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 10791 10792 if (osdinfo != NULL) { 10793 /* 10794 * We are re-probing port with a previously attached device. 10795 * Save previous device type and settings. 10796 */ 10797 prev_device_type = pmportinfo->pmport_dev_type; 10798 prev_device_settings = osdinfo->satadrv_settings; 10799 prev_device_state = osdinfo->satadrv_state; 10800 } 10801 10802 start_time = ddi_get_lbolt(); 10803 10804 /* check parent status */ 10805 mutex_enter(&cportinfo->cport_mutex); 10806 if ((cportinfo->cport_state & 10807 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 10808 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 10809 SATA_PORT_DEVLINK_UP) { 10810 mutex_exit(&cportinfo->cport_mutex); 10811 return (SATA_FAILURE); 10812 } 10813 mutex_exit(&cportinfo->cport_mutex); 10814 10815 retry_probe_pmport: 10816 10817 /* probe port */ 10818 mutex_enter(&pmportinfo->pmport_mutex); 10819 pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 10820 pmportinfo->pmport_state |= SATA_STATE_PROBING; 10821 mutex_exit(&pmportinfo->pmport_mutex); 10822 10823 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 10824 (SATA_DIP(sata_hba_inst), sata_device); 10825 10826 /* might need retry because we cannot touch registers. */ 10827 if (rval == SATA_FAILURE) { 10828 mutex_enter(&pmportinfo->pmport_mutex); 10829 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 10830 mutex_exit(&pmportinfo->pmport_mutex); 10831 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: " 10832 "SATA port %d:%d probing failed", 10833 cport, pmport)); 10834 return (SATA_FAILURE); 10835 } else if (rval == SATA_RETRY) { 10836 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: " 10837 "SATA port %d:%d probing failed, retrying...", 10838 cport, pmport)); 10839 clock_t cur_time = ddi_get_lbolt(); 10840 /* 10841 * A device was not successfully identified or initialized. 10842 * Track retry time for device identification. 10843 */ 10844 if ((cur_time - start_time) < 10845 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 10846 /* sleep for a while */ 10847 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 10848 goto retry_probe_pmport; 10849 } else { 10850 mutex_enter(&pmportinfo->pmport_mutex); 10851 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) 10852 SATA_PMPORTINFO_DRV_INFO(pmportinfo)-> 10853 satadrv_state = SATA_DSTATE_FAILED; 10854 mutex_exit(&pmportinfo->pmport_mutex); 10855 return (SATA_SUCCESS); 10856 } 10857 } 10858 10859 /* 10860 * Sanity check - Controller port is active? Is the link active? 10861 * Is it still a port multiplier? 10862 */ 10863 if ((cportinfo->cport_state & 10864 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 10865 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 10866 SATA_PORT_DEVLINK_UP || 10867 (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) { 10868 /* 10869 * Port in non-usable state or no link active/no 10870 * device. Free info structure. 10871 */ 10872 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 10873 10874 sdevice.satadev_addr.cport = cport; 10875 sdevice.satadev_addr.pmport = pmport; 10876 sdevice.satadev_addr.qual = SATA_ADDR_PMULT; 10877 mutex_exit(&cportinfo->cport_mutex); 10878 10879 sata_free_pmult(sata_hba_inst, &sdevice); 10880 return (SATA_FAILURE); 10881 } 10882 10883 /* SATA_SUCCESS NOW */ 10884 /* 10885 * update sata port state and set device type 10886 */ 10887 mutex_enter(&pmportinfo->pmport_mutex); 10888 sata_update_pmport_info(sata_hba_inst, sata_device); 10889 pmportinfo->pmport_state &= ~SATA_STATE_PROBING; 10890 10891 /* 10892 * Sanity check - Port is active? Is the link active? 10893 * Is there any device attached? 10894 */ 10895 if ((pmportinfo->pmport_state & 10896 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 10897 (pmportinfo->pmport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 10898 SATA_PORT_DEVLINK_UP) { 10899 /* 10900 * Port in non-usable state or no link active/no device. 10901 * Free info structure if necessary (direct attached drive 10902 * only, for now! 10903 */ 10904 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 10905 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 10906 /* Add here differentiation for device attached or not */ 10907 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 10908 mutex_exit(&pmportinfo->pmport_mutex); 10909 if (sdinfo != NULL) 10910 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10911 return (SATA_SUCCESS); 10912 } 10913 10914 pmportinfo->pmport_state |= SATA_STATE_READY; 10915 pmportinfo->pmport_dev_type = sata_device->satadev_type; 10916 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 10917 10918 /* 10919 * If we are re-probing the port, there may be 10920 * sata_drive_info structure attached 10921 * (or sata_pm_info, if PMult is supported). 10922 */ 10923 if (sata_device->satadev_type == SATA_DTYPE_NONE) { 10924 /* 10925 * There is no device, so remove device info structure, 10926 * if necessary. 10927 */ 10928 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 10929 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 10930 if (sdinfo != NULL) { 10931 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10932 sata_log(sata_hba_inst, CE_WARN, 10933 "SATA device detached from port %d:%d", 10934 cport, pmport); 10935 } 10936 mutex_exit(&pmportinfo->pmport_mutex); 10937 return (SATA_SUCCESS); 10938 } 10939 10940 /* this should not be a pmult */ 10941 ASSERT(sata_device->satadev_type != SATA_DTYPE_PMULT); 10942 if (sdinfo == NULL) { 10943 /* 10944 * There is some device attached, but there is 10945 * no sata_drive_info structure - allocate one 10946 */ 10947 mutex_exit(&pmportinfo->pmport_mutex); 10948 sdinfo = kmem_zalloc(sizeof (sata_drive_info_t), 10949 KM_SLEEP); 10950 mutex_enter(&pmportinfo->pmport_mutex); 10951 /* 10952 * Recheck, that the port state did not change when we 10953 * released mutex. 10954 */ 10955 if (pmportinfo->pmport_state & SATA_STATE_READY) { 10956 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = sdinfo; 10957 sdinfo->satadrv_addr = pmportinfo->pmport_addr; 10958 sdinfo->satadrv_addr.qual = SATA_ADDR_DPMPORT; 10959 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 10960 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 10961 } else { 10962 /* 10963 * Port is not in ready state, we 10964 * cannot attach a device. 10965 */ 10966 mutex_exit(&pmportinfo->pmport_mutex); 10967 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10968 return (SATA_SUCCESS); 10969 } 10970 /* 10971 * Since we are adding device, presumably new one, 10972 * indicate that it should be initalized, 10973 * as well as some internal framework states). 10974 */ 10975 init_device = B_TRUE; 10976 } 10977 10978 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN; 10979 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual; 10980 10981 mutex_exit(&pmportinfo->pmport_mutex); 10982 /* 10983 * Figure out what kind of device we are really 10984 * dealing with. 10985 */ 10986 rval = sata_probe_device(sata_hba_inst, sata_device); 10987 10988 mutex_enter(&pmportinfo->pmport_mutex); 10989 if (rval == SATA_SUCCESS) { 10990 /* 10991 * If we are dealing with the same type of a device as before, 10992 * restore its settings flags. 10993 */ 10994 if (osdinfo != NULL && 10995 sata_device->satadev_type == prev_device_type) 10996 sdinfo->satadrv_settings = prev_device_settings; 10997 10998 mutex_exit(&pmportinfo->pmport_mutex); 10999 /* Set initial device features, if necessary */ 11000 if (init_device == B_TRUE) { 11001 rval = sata_initialize_device(sata_hba_inst, sdinfo); 11002 } 11003 if (rval == SATA_SUCCESS) 11004 return (rval); 11005 } else { 11006 /* 11007 * If there was some device info before we probe the device, 11008 * restore previous device setting, so we can retry from scratch 11009 * later. Providing, of course, that device has not disappeared 11010 * during probing process. 11011 */ 11012 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 11013 if (osdinfo != NULL) { 11014 pmportinfo->pmport_dev_type = prev_device_type; 11015 sdinfo->satadrv_type = prev_device_type; 11016 sdinfo->satadrv_state = prev_device_state; 11017 } 11018 } else { 11019 /* device is gone */ 11020 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11021 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 11022 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 11023 mutex_exit(&pmportinfo->pmport_mutex); 11024 return (SATA_SUCCESS); 11025 } 11026 mutex_exit(&pmportinfo->pmport_mutex); 11027 } 11028 11029 if (flag == SATA_DEV_IDENTIFY_RETRY) { 11030 clock_t cur_time = ddi_get_lbolt(); 11031 /* 11032 * A device was not successfully identified or initialized. 11033 * Track retry time for device identification. 11034 */ 11035 if ((cur_time - start_time) < 11036 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 11037 /* sleep for a while */ 11038 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 11039 goto retry_probe_pmport; 11040 } else { 11041 mutex_enter(&pmportinfo->pmport_mutex); 11042 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) 11043 SATA_PMPORTINFO_DRV_INFO(pmportinfo)-> 11044 satadrv_state = SATA_DSTATE_FAILED; 11045 mutex_exit(&pmportinfo->pmport_mutex); 11046 } 11047 } 11048 return (SATA_SUCCESS); 11049 } 11050 11051 /* 11052 * Allocated related structure for a port multiplier and its device ports 11053 * 11054 * Port multiplier should be ready and probed, and related information like 11055 * the number of the device ports should be store in sata_device_t. 11056 * 11057 * NOTE: No Mutex should be hold. 11058 */ 11059 static int 11060 sata_alloc_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 11061 { 11062 dev_info_t *dip = SATA_DIP(sata_hba_inst); 11063 sata_cport_info_t *cportinfo = NULL; 11064 sata_pmult_info_t *pmultinfo = NULL; 11065 sata_pmport_info_t *pmportinfo = NULL; 11066 sata_device_t sd; 11067 dev_t minor_number; 11068 char name[16]; 11069 uint8_t cport = sata_device->satadev_addr.cport; 11070 int rval; 11071 int npmport; 11072 11073 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11074 11075 /* This function might be called while a port-mult is hot-plugged. */ 11076 mutex_enter(&cportinfo->cport_mutex); 11077 11078 /* dev_type's not updated when get called from sata_reprobe_port() */ 11079 if (SATA_CPORTINFO_PMULT_INFO(cportinfo) == NULL) { 11080 /* Create a pmult_info structure */ 11081 SATA_CPORTINFO_PMULT_INFO(cportinfo) = 11082 kmem_zalloc(sizeof (sata_pmult_info_t), KM_SLEEP); 11083 } 11084 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 11085 11086 pmultinfo->pmult_addr = sata_device->satadev_addr; 11087 pmultinfo->pmult_addr.qual = SATA_ADDR_PMULT; 11088 pmultinfo->pmult_state = SATA_STATE_PROBING; 11089 11090 /* 11091 * Probe the port multiplier with qualifier SATA_ADDR_PMULT_SPEC, 11092 * The HBA driver should initialize and register the port multiplier, 11093 * sata_register_pmult() will fill following fields, 11094 * + sata_pmult_info.pmult_gscr 11095 * + sata_pmult_info.pmult_num_dev_ports 11096 */ 11097 sd.satadev_addr = sata_device->satadev_addr; 11098 sd.satadev_addr.qual = SATA_ADDR_PMULT_SPEC; 11099 mutex_exit(&cportinfo->cport_mutex); 11100 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11101 (SATA_DIP(sata_hba_inst), &sd); 11102 mutex_enter(&cportinfo->cport_mutex); 11103 11104 if (rval != SATA_SUCCESS || 11105 (sd.satadev_type != SATA_DTYPE_PMULT) || 11106 !(sd.satadev_state & SATA_DSTATE_PMULT_INIT)) { 11107 SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL; 11108 kmem_free(pmultinfo, sizeof (sata_pmult_info_t)); 11109 cportinfo->cport_state = SATA_PSTATE_FAILED; 11110 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 11111 mutex_exit(&cportinfo->cport_mutex); 11112 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 11113 "sata_alloc_pmult: failed to initialize pmult " 11114 "at port %d.", cport) 11115 return (SATA_FAILURE); 11116 } 11117 11118 /* Initialize pmport_info structure */ 11119 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; 11120 npmport++) { 11121 11122 /* if everything is allocated, skip */ 11123 if (SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) != NULL) 11124 continue; 11125 11126 pmportinfo = kmem_zalloc(sizeof (sata_pmport_info_t), KM_SLEEP); 11127 mutex_init(&pmportinfo->pmport_mutex, NULL, MUTEX_DRIVER, NULL); 11128 mutex_exit(&cportinfo->cport_mutex); 11129 11130 mutex_enter(&pmportinfo->pmport_mutex); 11131 pmportinfo->pmport_addr.cport = cport; 11132 pmportinfo->pmport_addr.pmport = (uint8_t)npmport; 11133 pmportinfo->pmport_addr.qual = SATA_ADDR_PMPORT; 11134 pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 11135 mutex_exit(&pmportinfo->pmport_mutex); 11136 11137 mutex_enter(&cportinfo->cport_mutex); 11138 SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) = pmportinfo; 11139 11140 /* Create an attachment point */ 11141 minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip), 11142 cport, (uint8_t)npmport, SATA_ADDR_PMPORT); 11143 (void) sprintf(name, "%d.%d", cport, npmport); 11144 11145 if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number, 11146 DDI_NT_SATA_ATTACHMENT_POINT, 0) != DDI_SUCCESS) { 11147 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: " 11148 "cannot create SATA attachment point for " 11149 "port %d:%d", cport, npmport); 11150 } 11151 } 11152 11153 pmultinfo->pmult_state &= ~SATA_STATE_PROBING; 11154 pmultinfo->pmult_state |= (SATA_STATE_PROBED|SATA_STATE_READY); 11155 cportinfo->cport_dev_type = SATA_DTYPE_PMULT; 11156 11157 mutex_exit(&cportinfo->cport_mutex); 11158 return (SATA_SUCCESS); 11159 } 11160 11161 /* 11162 * Free data structures when a port multiplier is removed. 11163 * 11164 * NOTE: No Mutex should be hold. 11165 */ 11166 static void 11167 sata_free_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 11168 { 11169 sata_cport_info_t *cportinfo; 11170 sata_pmult_info_t *pmultinfo; 11171 sata_pmport_info_t *pmportinfo; 11172 sata_device_t pmport_device; 11173 sata_drive_info_t *sdinfo; 11174 dev_info_t *tdip; 11175 char name[16]; 11176 uint8_t cport = sata_device->satadev_addr.cport; 11177 int npmport; 11178 11179 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11180 11181 /* This function might be called while port-mult is hot plugged. */ 11182 mutex_enter(&cportinfo->cport_mutex); 11183 11184 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 11185 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 11186 ASSERT(pmultinfo != NULL); 11187 11188 /* Free pmport_info structure */ 11189 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; 11190 npmport++) { 11191 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport); 11192 if (pmportinfo == NULL) 11193 continue; 11194 mutex_exit(&cportinfo->cport_mutex); 11195 11196 mutex_enter(&pmportinfo->pmport_mutex); 11197 sdinfo = pmportinfo->pmport_sata_drive; 11198 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 11199 mutex_exit(&pmportinfo->pmport_mutex); 11200 11201 /* Remove attachment point. */ 11202 name[0] = '\0'; 11203 (void) sprintf(name, "%d.%d", cport, npmport); 11204 ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name); 11205 sata_log(sata_hba_inst, CE_NOTE, 11206 "Remove attachment point of port %d:%d", 11207 cport, npmport); 11208 11209 /* 11210 * Rumove target node 11211 */ 11212 bzero(&pmport_device, sizeof (sata_device_t)); 11213 pmport_device.satadev_rev = SATA_DEVICE_REV; 11214 pmport_device.satadev_addr.cport = cport; 11215 pmport_device.satadev_addr.pmport = (uint8_t)npmport; 11216 pmport_device.satadev_addr.qual = SATA_ADDR_DPMPORT; 11217 11218 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 11219 &(pmport_device.satadev_addr)); 11220 if (tdip != NULL && ndi_devi_offline(tdip, 11221 NDI_DEVI_REMOVE) != NDI_SUCCESS) { 11222 /* 11223 * Problem : 11224 * The target node remained attached. 11225 * This happens when the device file was open 11226 * or a node was waiting for resources. 11227 * Cannot do anything about it. 11228 */ 11229 SATA_LOG_D((sata_hba_inst, CE_WARN, 11230 "sata_free_pmult: could not unconfigure device " 11231 "before disconnecting the SATA port %d:%d", 11232 cport, npmport)); 11233 11234 /* 11235 * Set DEVICE REMOVED state in the target 11236 * node. It will prevent access to the device 11237 * even when a new device is attached, until 11238 * the old target node is released, removed and 11239 * recreated for a new device. 11240 */ 11241 sata_set_device_removed(tdip); 11242 11243 /* 11244 * Instruct event daemon to try the target 11245 * node cleanup later. 11246 */ 11247 sata_set_target_node_cleanup( 11248 sata_hba_inst, &(pmport_device.satadev_addr)); 11249 11250 } 11251 mutex_enter(&cportinfo->cport_mutex); 11252 11253 /* 11254 * Add here differentiation for device attached or not 11255 */ 11256 if (sdinfo != NULL) { 11257 sata_log(sata_hba_inst, CE_WARN, 11258 "SATA device detached from port %d:%d", 11259 cport, npmport); 11260 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11261 } 11262 11263 mutex_destroy(&pmportinfo->pmport_mutex); 11264 kmem_free(pmportinfo, sizeof (sata_pmport_info_t)); 11265 } 11266 11267 kmem_free(pmultinfo, sizeof (sata_pmult_info_t)); 11268 11269 cportinfo->cport_devp.cport_sata_pmult = NULL; 11270 11271 sata_log(sata_hba_inst, CE_WARN, 11272 "SATA port multiplier detached at port %d", cport); 11273 11274 mutex_exit(&cportinfo->cport_mutex); 11275 } 11276 11277 /* 11278 * Initialize device 11279 * Specified device is initialized to a default state. 11280 * 11281 * Returns SATA_SUCCESS if all device features are set successfully, 11282 * SATA_RETRY if device is accessible but device features were not set 11283 * successfully, and SATA_FAILURE otherwise. 11284 */ 11285 static int 11286 sata_initialize_device(sata_hba_inst_t *sata_hba_inst, 11287 sata_drive_info_t *sdinfo) 11288 { 11289 int rval; 11290 11291 sata_save_drive_settings(sdinfo); 11292 11293 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 11294 11295 sata_init_write_cache_mode(sdinfo); 11296 11297 rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0); 11298 11299 /* Determine current data transfer mode */ 11300 if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) { 11301 sdinfo->satadrv_settings &= ~SATA_DEV_DMA; 11302 } else if ((sdinfo->satadrv_id.ai_validinfo & 11303 SATA_VALIDINFO_88) != 0 && 11304 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) { 11305 sdinfo->satadrv_settings |= SATA_DEV_DMA; 11306 } else if ((sdinfo->satadrv_id.ai_dworddma & 11307 SATA_MDMA_SEL_MASK) != 0) { 11308 sdinfo->satadrv_settings |= SATA_DEV_DMA; 11309 } else 11310 /* DMA supported, not no DMA transfer mode is selected !? */ 11311 sdinfo->satadrv_settings &= ~SATA_DEV_DMA; 11312 11313 if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) && 11314 (sdinfo->satadrv_id.ai_features86 & 0x20)) 11315 sdinfo->satadrv_power_level = SATA_POWER_STANDBY; 11316 else 11317 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 11318 11319 return (rval); 11320 } 11321 11322 11323 /* 11324 * Initialize write cache mode. 11325 * 11326 * The default write cache setting for SATA HDD is provided by sata_write_cache 11327 * static variable. ATAPI CD/DVDs devices have write cache default is 11328 * determined by sata_atapicdvd_write_cache static variable. 11329 * ATAPI tape devices have write cache default is determined by 11330 * sata_atapitape_write_cache static variable. 11331 * ATAPI disk devices have write cache default is determined by 11332 * sata_atapidisk_write_cache static variable. 11333 * 1 - enable 11334 * 0 - disable 11335 * any other value - current drive setting 11336 * 11337 * Although there is not reason to disable write cache on CD/DVD devices, 11338 * tape devices and ATAPI disk devices, the default setting control is provided 11339 * for the maximun flexibility. 11340 * 11341 * In the future, it may be overridden by the 11342 * disk-write-cache-enable property setting, if it is defined. 11343 * Returns SATA_SUCCESS if all device features are set successfully, 11344 * SATA_FAILURE otherwise. 11345 */ 11346 static void 11347 sata_init_write_cache_mode(sata_drive_info_t *sdinfo) 11348 { 11349 switch (sdinfo->satadrv_type) { 11350 case SATA_DTYPE_ATADISK: 11351 if (sata_write_cache == 1) 11352 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 11353 else if (sata_write_cache == 0) 11354 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 11355 /* 11356 * When sata_write_cache value is not 0 or 1, 11357 * a current setting of the drive's write cache is used. 11358 */ 11359 break; 11360 case SATA_DTYPE_ATAPICD: 11361 if (sata_atapicdvd_write_cache == 1) 11362 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 11363 else if (sata_atapicdvd_write_cache == 0) 11364 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 11365 /* 11366 * When sata_atapicdvd_write_cache value is not 0 or 1, 11367 * a current setting of the drive's write cache is used. 11368 */ 11369 break; 11370 case SATA_DTYPE_ATAPITAPE: 11371 if (sata_atapitape_write_cache == 1) 11372 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 11373 else if (sata_atapitape_write_cache == 0) 11374 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 11375 /* 11376 * When sata_atapitape_write_cache value is not 0 or 1, 11377 * a current setting of the drive's write cache is used. 11378 */ 11379 break; 11380 case SATA_DTYPE_ATAPIDISK: 11381 if (sata_atapidisk_write_cache == 1) 11382 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 11383 else if (sata_atapidisk_write_cache == 0) 11384 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 11385 /* 11386 * When sata_atapidisk_write_cache value is not 0 or 1, 11387 * a current setting of the drive's write cache is used. 11388 */ 11389 break; 11390 } 11391 } 11392 11393 11394 /* 11395 * Validate sata address. 11396 * Specified cport, pmport and qualifier has to match 11397 * passed sata_scsi configuration info. 11398 * The presence of an attached device is not verified. 11399 * 11400 * Returns 0 when address is valid, -1 otherwise. 11401 */ 11402 static int 11403 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport, 11404 int pmport, int qual) 11405 { 11406 if (qual == SATA_ADDR_DCPORT && pmport != 0) 11407 goto invalid_address; 11408 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 11409 goto invalid_address; 11410 if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) && 11411 ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) || 11412 (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) || 11413 (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport)))) 11414 goto invalid_address; 11415 11416 return (0); 11417 11418 invalid_address: 11419 return (-1); 11420 11421 } 11422 11423 /* 11424 * Validate scsi address 11425 * SCSI target address is translated into SATA cport/pmport and compared 11426 * with a controller port/device configuration. LUN has to be 0. 11427 * Returns 0 if a scsi target refers to an attached device, 11428 * returns 1 if address is valid but no valid device is attached, 11429 * returns 2 if address is valid but device type is unknown (not valid device), 11430 * returns -1 if bad address or device is of an unsupported type. 11431 * Upon return sata_device argument is set. 11432 * 11433 * Port multiplier is supported now. 11434 */ 11435 static int 11436 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst, 11437 struct scsi_address *ap, sata_device_t *sata_device) 11438 { 11439 int cport, pmport, qual, rval; 11440 11441 rval = -1; /* Invalid address */ 11442 if (ap->a_lun != 0) 11443 goto out; 11444 11445 qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 11446 cport = SCSI_TO_SATA_CPORT(ap->a_target); 11447 pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 11448 11449 if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT) 11450 goto out; 11451 11452 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) == 11453 0) { 11454 11455 sata_cport_info_t *cportinfo; 11456 sata_pmult_info_t *pmultinfo; 11457 sata_drive_info_t *sdinfo = NULL; 11458 11459 sata_device->satadev_addr.qual = qual; 11460 sata_device->satadev_addr.cport = cport; 11461 sata_device->satadev_addr.pmport = pmport; 11462 sata_device->satadev_rev = SATA_DEVICE_REV_1; 11463 11464 rval = 1; /* Valid sata address */ 11465 11466 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11467 if (qual == SATA_ADDR_DCPORT) { 11468 if (cportinfo == NULL || 11469 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 11470 goto out; 11471 11472 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 11473 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN && 11474 sdinfo != NULL) { 11475 rval = 2; 11476 goto out; 11477 } 11478 11479 if ((cportinfo->cport_dev_type & 11480 SATA_VALID_DEV_TYPE) == 0) { 11481 rval = -1; 11482 goto out; 11483 } 11484 11485 } else if (qual == SATA_ADDR_DPMPORT) { 11486 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 11487 if (pmultinfo == NULL) { 11488 rval = -1; 11489 goto out; 11490 } 11491 if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) == 11492 NULL || 11493 SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 11494 pmport) == SATA_DTYPE_NONE) 11495 goto out; 11496 11497 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, 11498 pmport); 11499 if (SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 11500 pmport) == SATA_DTYPE_UNKNOWN && sdinfo != NULL) { 11501 rval = 2; 11502 goto out; 11503 } 11504 11505 if ((SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 11506 pmport) && SATA_VALID_DEV_TYPE) == 0) { 11507 rval = -1; 11508 goto out; 11509 } 11510 11511 } else { 11512 rval = -1; 11513 goto out; 11514 } 11515 if ((sdinfo == NULL) || 11516 (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0) 11517 goto out; 11518 11519 sata_device->satadev_type = sdinfo->satadrv_type; 11520 11521 return (0); 11522 } 11523 out: 11524 if (rval > 0) { 11525 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 11526 "sata_validate_scsi_address: no valid target %x lun %x", 11527 ap->a_target, ap->a_lun); 11528 } 11529 return (rval); 11530 } 11531 11532 /* 11533 * Find dip corresponding to passed device number 11534 * 11535 * Returns NULL if invalid device number is passed or device cannot be found, 11536 * Returns dip is device is found. 11537 */ 11538 static dev_info_t * 11539 sata_devt_to_devinfo(dev_t dev) 11540 { 11541 dev_info_t *dip; 11542 #ifndef __lock_lint 11543 struct devnames *dnp; 11544 major_t major = getmajor(dev); 11545 int instance = SATA_MINOR2INSTANCE(getminor(dev)); 11546 11547 if (major >= devcnt) 11548 return (NULL); 11549 11550 dnp = &devnamesp[major]; 11551 LOCK_DEV_OPS(&(dnp->dn_lock)); 11552 dip = dnp->dn_head; 11553 while (dip && (ddi_get_instance(dip) != instance)) { 11554 dip = ddi_get_next(dip); 11555 } 11556 UNLOCK_DEV_OPS(&(dnp->dn_lock)); 11557 #endif 11558 11559 return (dip); 11560 } 11561 11562 11563 /* 11564 * Probe device. 11565 * This function issues Identify Device command and initializes local 11566 * sata_drive_info structure if the device can be identified. 11567 * The device type is determined by examining Identify Device 11568 * command response. 11569 * If the sata_hba_inst has linked drive info structure for this 11570 * device address, the Identify Device data is stored into sata_drive_info 11571 * structure linked to the port info structure. 11572 * 11573 * sata_device has to refer to the valid sata port(s) for HBA described 11574 * by sata_hba_inst structure. 11575 * 11576 * Returns: 11577 * SATA_SUCCESS if device type was successfully probed and port-linked 11578 * drive info structure was updated; 11579 * SATA_FAILURE if there is no device, or device was not probed 11580 * successully; 11581 * SATA_RETRY if device probe can be retried later. 11582 * If a device cannot be identified, sata_device's dev_state and dev_type 11583 * fields are set to unknown. 11584 * There are no retries in this function. Any retries should be managed by 11585 * the caller. 11586 */ 11587 11588 11589 static int 11590 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 11591 { 11592 sata_pmport_info_t *pmportinfo; 11593 sata_drive_info_t *sdinfo; 11594 sata_drive_info_t new_sdinfo; /* local drive info struct */ 11595 int rval; 11596 11597 ASSERT((SATA_CPORT_STATE(sata_hba_inst, 11598 sata_device->satadev_addr.cport) & 11599 (SATA_STATE_PROBED | SATA_STATE_READY)) != 0); 11600 11601 sata_device->satadev_type = SATA_DTYPE_NONE; 11602 11603 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 11604 sata_device->satadev_addr.cport))); 11605 11606 if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) { 11607 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 11608 sata_device->satadev_addr.cport, 11609 sata_device->satadev_addr.pmport); 11610 ASSERT(pmportinfo != NULL); 11611 } 11612 11613 /* Get pointer to port-linked sata device info structure */ 11614 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 11615 if (sdinfo != NULL) { 11616 sdinfo->satadrv_state &= 11617 ~(SATA_STATE_PROBED | SATA_STATE_READY); 11618 sdinfo->satadrv_state |= SATA_STATE_PROBING; 11619 } else { 11620 /* No device to probe */ 11621 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 11622 sata_device->satadev_addr.cport))); 11623 sata_device->satadev_type = SATA_DTYPE_NONE; 11624 sata_device->satadev_state = SATA_STATE_UNKNOWN; 11625 return (SATA_FAILURE); 11626 } 11627 /* 11628 * Need to issue both types of identify device command and 11629 * determine device type by examining retreived data/status. 11630 * First, ATA Identify Device. 11631 */ 11632 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 11633 new_sdinfo.satadrv_addr = sata_device->satadev_addr; 11634 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 11635 sata_device->satadev_addr.cport))); 11636 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK; 11637 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 11638 if (rval == SATA_RETRY) { 11639 /* We may try to check for ATAPI device */ 11640 if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) { 11641 /* 11642 * HBA supports ATAPI - try to issue Identify Packet 11643 * Device command. 11644 */ 11645 new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI; 11646 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 11647 } 11648 } 11649 if (rval == SATA_SUCCESS) { 11650 /* 11651 * Got something responding positively to ATA Identify Device 11652 * or to Identify Packet Device cmd. 11653 * Save last used device type. 11654 */ 11655 sata_device->satadev_type = new_sdinfo.satadrv_type; 11656 11657 /* save device info, if possible */ 11658 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 11659 sata_device->satadev_addr.cport))); 11660 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 11661 if (sdinfo == NULL) { 11662 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 11663 sata_device->satadev_addr.cport))); 11664 return (SATA_FAILURE); 11665 } 11666 /* 11667 * Copy drive info into the port-linked drive info structure. 11668 */ 11669 *sdinfo = new_sdinfo; 11670 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 11671 sdinfo->satadrv_state |= SATA_STATE_PROBED; 11672 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 11673 SATA_CPORT_DEV_TYPE(sata_hba_inst, 11674 sata_device->satadev_addr.cport) = 11675 sdinfo->satadrv_type; 11676 else { /* SATA_ADDR_DPMPORT */ 11677 mutex_enter(&pmportinfo->pmport_mutex); 11678 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 11679 sata_device->satadev_addr.cport, 11680 sata_device->satadev_addr.pmport) = 11681 sdinfo->satadrv_type; 11682 mutex_exit(&pmportinfo->pmport_mutex); 11683 } 11684 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 11685 sata_device->satadev_addr.cport))); 11686 return (SATA_SUCCESS); 11687 } 11688 11689 /* 11690 * It may be SATA_RETRY or SATA_FAILURE return. 11691 * Looks like we cannot determine the device type at this time. 11692 */ 11693 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 11694 sata_device->satadev_addr.cport))); 11695 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 11696 if (sdinfo != NULL) { 11697 sata_device->satadev_type = SATA_DTYPE_UNKNOWN; 11698 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 11699 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 11700 sdinfo->satadrv_state |= SATA_STATE_PROBED; 11701 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 11702 SATA_CPORT_DEV_TYPE(sata_hba_inst, 11703 sata_device->satadev_addr.cport) = 11704 SATA_DTYPE_UNKNOWN; 11705 else { 11706 /* SATA_ADDR_DPMPORT */ 11707 mutex_enter(&pmportinfo->pmport_mutex); 11708 if ((SATA_PMULT_INFO(sata_hba_inst, 11709 sata_device->satadev_addr.cport) != NULL) && 11710 (SATA_PMPORT_INFO(sata_hba_inst, 11711 sata_device->satadev_addr.cport, 11712 sata_device->satadev_addr.pmport) != NULL)) 11713 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 11714 sata_device->satadev_addr.cport, 11715 sata_device->satadev_addr.pmport) = 11716 SATA_DTYPE_UNKNOWN; 11717 mutex_exit(&pmportinfo->pmport_mutex); 11718 } 11719 } 11720 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 11721 sata_device->satadev_addr.cport))); 11722 return (rval); 11723 } 11724 11725 11726 /* 11727 * Get pointer to sata_drive_info structure. 11728 * 11729 * The sata_device has to contain address (cport, pmport and qualifier) for 11730 * specified sata_scsi structure. 11731 * 11732 * Returns NULL if device address is not valid for this HBA configuration. 11733 * Otherwise, returns a pointer to sata_drive_info structure. 11734 * 11735 * This function should be called with a port mutex held. 11736 */ 11737 static sata_drive_info_t * 11738 sata_get_device_info(sata_hba_inst_t *sata_hba_inst, 11739 sata_device_t *sata_device) 11740 { 11741 uint8_t cport = sata_device->satadev_addr.cport; 11742 uint8_t pmport = sata_device->satadev_addr.pmport; 11743 uint8_t qual = sata_device->satadev_addr.qual; 11744 11745 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 11746 return (NULL); 11747 11748 if (!(SATA_CPORT_STATE(sata_hba_inst, cport) & 11749 (SATA_STATE_PROBED | SATA_STATE_READY))) 11750 /* Port not probed yet */ 11751 return (NULL); 11752 11753 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE) 11754 return (NULL); 11755 11756 if (qual == SATA_ADDR_DCPORT) { 11757 /* Request for a device on a controller port */ 11758 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == 11759 SATA_DTYPE_PMULT) 11760 /* Port multiplier attached */ 11761 return (NULL); 11762 return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport)); 11763 } 11764 if (qual == SATA_ADDR_DPMPORT) { 11765 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 11766 SATA_DTYPE_PMULT) 11767 return (NULL); 11768 11769 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) 11770 return (NULL); 11771 11772 if (!(SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) & 11773 (SATA_STATE_PROBED | SATA_STATE_READY))) 11774 /* Port multiplier port not probed yet */ 11775 return (NULL); 11776 11777 return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport)); 11778 } 11779 11780 /* we should not get here */ 11781 return (NULL); 11782 } 11783 11784 11785 /* 11786 * sata_identify_device. 11787 * Send Identify Device command to SATA HBA driver. 11788 * If command executes successfully, update sata_drive_info structure pointed 11789 * to by sdinfo argument, including Identify Device data. 11790 * If command fails, invalidate data in sata_drive_info. 11791 * 11792 * Cannot be called from interrupt level. 11793 * 11794 * Returns: 11795 * SATA_SUCCESS if the device was identified as a supported device, 11796 * SATA_RETRY if the device was not identified but could be retried, 11797 * SATA_FAILURE if the device was not identified and identify attempt 11798 * should not be retried. 11799 */ 11800 static int 11801 sata_identify_device(sata_hba_inst_t *sata_hba_inst, 11802 sata_drive_info_t *sdinfo) 11803 { 11804 uint16_t cfg_word; 11805 int rval; 11806 11807 /* fetch device identify data */ 11808 if ((rval = sata_fetch_device_identify_data(sata_hba_inst, 11809 sdinfo)) != SATA_SUCCESS) 11810 goto fail_unknown; 11811 11812 cfg_word = sdinfo->satadrv_id.ai_config; 11813 11814 /* Set the correct device type */ 11815 if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) { 11816 sdinfo->satadrv_type = SATA_DTYPE_ATADISK; 11817 } else if (cfg_word == SATA_CFA_TYPE) { 11818 /* It's a Compact Flash media via CF-to-SATA HDD adapter */ 11819 sdinfo->satadrv_type = SATA_DTYPE_ATADISK; 11820 } else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) { 11821 switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) { 11822 case SATA_ATAPI_CDROM_DEV: 11823 sdinfo->satadrv_type = SATA_DTYPE_ATAPICD; 11824 break; 11825 case SATA_ATAPI_SQACC_DEV: 11826 sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE; 11827 break; 11828 case SATA_ATAPI_DIRACC_DEV: 11829 sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK; 11830 break; 11831 default: 11832 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 11833 } 11834 } else { 11835 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 11836 } 11837 11838 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 11839 if (sdinfo->satadrv_capacity == 0) { 11840 /* Non-LBA disk. Too bad... */ 11841 sata_log(sata_hba_inst, CE_WARN, 11842 "SATA disk device at port %d does not support LBA", 11843 sdinfo->satadrv_addr.cport); 11844 rval = SATA_FAILURE; 11845 goto fail_unknown; 11846 } 11847 } 11848 #if 0 11849 /* Left for historical reason */ 11850 /* 11851 * Some initial version of SATA spec indicated that at least 11852 * UDMA mode 4 has to be supported. It is not metioned in 11853 * SerialATA 2.6, so this restriction is removed. 11854 */ 11855 /* Check for Ultra DMA modes 6 through 0 being supported */ 11856 for (i = 6; i >= 0; --i) { 11857 if (sdinfo->satadrv_id.ai_ultradma & (1 << i)) 11858 break; 11859 } 11860 11861 /* 11862 * At least UDMA 4 mode has to be supported. If mode 4 or 11863 * higher are not supported by the device, fail this 11864 * device. 11865 */ 11866 if (i < 4) { 11867 /* No required Ultra DMA mode supported */ 11868 sata_log(sata_hba_inst, CE_WARN, 11869 "SATA disk device at port %d does not support UDMA " 11870 "mode 4 or higher", sdinfo->satadrv_addr.cport); 11871 SATA_LOG_D((sata_hba_inst, CE_WARN, 11872 "mode 4 or higher required, %d supported", i)); 11873 rval = SATA_FAILURE; 11874 goto fail_unknown; 11875 } 11876 #endif 11877 11878 /* 11879 * For Disk devices, if it doesn't support UDMA mode, we would 11880 * like to return failure directly. 11881 */ 11882 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 11883 !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 && 11884 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) { 11885 sata_log(sata_hba_inst, CE_WARN, 11886 "SATA disk device at port %d does not support UDMA", 11887 sdinfo->satadrv_addr.cport); 11888 rval = SATA_FAILURE; 11889 goto fail_unknown; 11890 } 11891 11892 return (SATA_SUCCESS); 11893 11894 fail_unknown: 11895 /* Invalidate sata_drive_info ? */ 11896 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 11897 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 11898 return (rval); 11899 } 11900 11901 /* 11902 * Log/display device information 11903 */ 11904 static void 11905 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst, 11906 sata_drive_info_t *sdinfo) 11907 { 11908 int valid_version; 11909 char msg_buf[MAXPATHLEN]; 11910 int i; 11911 11912 /* Show HBA path */ 11913 (void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf); 11914 11915 cmn_err(CE_CONT, "?%s :\n", msg_buf); 11916 11917 switch (sdinfo->satadrv_type) { 11918 case SATA_DTYPE_ATADISK: 11919 (void) sprintf(msg_buf, "SATA disk device at"); 11920 break; 11921 11922 case SATA_DTYPE_ATAPICD: 11923 (void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at"); 11924 break; 11925 11926 case SATA_DTYPE_ATAPITAPE: 11927 (void) sprintf(msg_buf, "SATA tape (ATAPI) device at"); 11928 break; 11929 11930 case SATA_DTYPE_ATAPIDISK: 11931 (void) sprintf(msg_buf, "SATA disk (ATAPI) device at"); 11932 break; 11933 11934 case SATA_DTYPE_UNKNOWN: 11935 (void) sprintf(msg_buf, 11936 "Unsupported SATA device type (cfg 0x%x) at ", 11937 sdinfo->satadrv_id.ai_config); 11938 break; 11939 } 11940 11941 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT) 11942 cmn_err(CE_CONT, "?\t%s port %d\n", 11943 msg_buf, sdinfo->satadrv_addr.cport); 11944 else 11945 cmn_err(CE_CONT, "?\t%s port %d:%d\n", 11946 msg_buf, sdinfo->satadrv_addr.cport, 11947 sdinfo->satadrv_addr.pmport); 11948 11949 bcopy(&sdinfo->satadrv_id.ai_model, msg_buf, 11950 sizeof (sdinfo->satadrv_id.ai_model)); 11951 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model)); 11952 msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0'; 11953 cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf); 11954 11955 bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf, 11956 sizeof (sdinfo->satadrv_id.ai_fw)); 11957 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw)); 11958 msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0'; 11959 cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf); 11960 11961 bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf, 11962 sizeof (sdinfo->satadrv_id.ai_drvser)); 11963 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser)); 11964 msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0'; 11965 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 11966 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 11967 } else { 11968 /* 11969 * Some drives do not implement serial number and may 11970 * violate the spec by providing spaces rather than zeros 11971 * in serial number field. Scan the buffer to detect it. 11972 */ 11973 for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) { 11974 if (msg_buf[i] != '\0' && msg_buf[i] != ' ') 11975 break; 11976 } 11977 if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) { 11978 cmn_err(CE_CONT, "?\tserial number - none\n"); 11979 } else { 11980 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 11981 } 11982 } 11983 11984 #ifdef SATA_DEBUG 11985 if (sdinfo->satadrv_id.ai_majorversion != 0 && 11986 sdinfo->satadrv_id.ai_majorversion != 0xffff) { 11987 int i; 11988 for (i = 14; i >= 2; i--) { 11989 if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) { 11990 valid_version = i; 11991 break; 11992 } 11993 } 11994 cmn_err(CE_CONT, 11995 "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n", 11996 valid_version, 11997 sdinfo->satadrv_id.ai_majorversion, 11998 sdinfo->satadrv_id.ai_minorversion); 11999 } 12000 #endif 12001 /* Log some info */ 12002 cmn_err(CE_CONT, "?\tsupported features:\n"); 12003 msg_buf[0] = '\0'; 12004 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 12005 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) 12006 (void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN); 12007 else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) 12008 (void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN); 12009 } 12010 if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA) 12011 (void) strlcat(msg_buf, "DMA", MAXPATHLEN); 12012 if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) 12013 (void) strlcat(msg_buf, ", Native Command Queueing", 12014 MAXPATHLEN); 12015 if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ) 12016 (void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN); 12017 if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) && 12018 (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED)) 12019 (void) strlcat(msg_buf, ", SMART", MAXPATHLEN); 12020 if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) && 12021 (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED)) 12022 (void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN); 12023 cmn_err(CE_CONT, "?\t %s\n", msg_buf); 12024 if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2) 12025 cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n"); 12026 else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1) 12027 cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n"); 12028 if (sdinfo->satadrv_features_support & 12029 (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) { 12030 msg_buf[0] = '\0'; 12031 (void) snprintf(msg_buf, MAXPATHLEN, 12032 "Supported queue depth %d", 12033 sdinfo->satadrv_queue_depth); 12034 if (!(sata_func_enable & 12035 (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ))) 12036 (void) strlcat(msg_buf, 12037 " - queueing disabled globally", MAXPATHLEN); 12038 else if (sdinfo->satadrv_queue_depth > 12039 sdinfo->satadrv_max_queue_depth) { 12040 (void) snprintf(&msg_buf[strlen(msg_buf)], 12041 MAXPATHLEN - strlen(msg_buf), ", limited to %d", 12042 (int)sdinfo->satadrv_max_queue_depth); 12043 } 12044 cmn_err(CE_CONT, "?\t%s\n", msg_buf); 12045 } 12046 12047 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 12048 #ifdef __i386 12049 (void) sprintf(msg_buf, "\tcapacity = %llu sectors\n", 12050 sdinfo->satadrv_capacity); 12051 #else 12052 (void) sprintf(msg_buf, "\tcapacity = %lu sectors\n", 12053 sdinfo->satadrv_capacity); 12054 #endif 12055 cmn_err(CE_CONT, "?%s", msg_buf); 12056 } 12057 } 12058 12059 /* 12060 * Log/display port multiplier information 12061 * No Mutex should be hold. 12062 */ 12063 static void 12064 sata_show_pmult_info(sata_hba_inst_t *sata_hba_inst, 12065 sata_device_t *sata_device) 12066 { 12067 _NOTE(ARGUNUSED(sata_hba_inst)) 12068 12069 int cport = sata_device->satadev_addr.cport; 12070 sata_pmult_info_t *pmultinfo; 12071 char msg_buf[MAXPATHLEN]; 12072 uint32_t gscr0, gscr1, gscr2, gscr64; 12073 12074 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 12075 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 12076 if (pmultinfo == NULL) { 12077 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 12078 return; 12079 } 12080 12081 gscr0 = pmultinfo->pmult_gscr.gscr0; 12082 gscr1 = pmultinfo->pmult_gscr.gscr1; 12083 gscr2 = pmultinfo->pmult_gscr.gscr2; 12084 gscr64 = pmultinfo->pmult_gscr.gscr64; 12085 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 12086 12087 cmn_err(CE_CONT, "?Port Multiplier %d device-ports found at port %d", 12088 sata_device->satadev_add_info, sata_device->satadev_addr.cport); 12089 12090 (void) sprintf(msg_buf, "\tVendor_ID 0x%04x, Module_ID 0x%04x", 12091 gscr0 & 0xffff, (gscr0 >> 16) & 0xffff); 12092 cmn_err(CE_CONT, "?%s", msg_buf); 12093 12094 (void) strcpy(msg_buf, "\tSupport SATA PMP Spec "); 12095 if (gscr1 & (1 << 3)) 12096 (void) strlcat(msg_buf, "1.2", MAXPATHLEN); 12097 else if (gscr1 & (1 << 2)) 12098 (void) strlcat(msg_buf, "1.1", MAXPATHLEN); 12099 else if (gscr1 & (1 << 1)) 12100 (void) strlcat(msg_buf, "1.0", MAXPATHLEN); 12101 else 12102 (void) strlcat(msg_buf, "unknown", MAXPATHLEN); 12103 cmn_err(CE_CONT, "?%s", msg_buf); 12104 12105 (void) strcpy(msg_buf, "\tSupport "); 12106 if (gscr64 & (1 << 3)) 12107 (void) strlcat(msg_buf, "Asy-Notif, ", 12108 MAXPATHLEN); 12109 if (gscr64 & (1 << 2)) 12110 (void) strlcat(msg_buf, "Dyn-SSC, ", MAXPATHLEN); 12111 if (gscr64 & (1 << 1)) 12112 (void) strlcat(msg_buf, "Iss-PMREQ, ", MAXPATHLEN); 12113 if (gscr64 & (1 << 0)) 12114 (void) strlcat(msg_buf, "BIST", MAXPATHLEN); 12115 if ((gscr64 & 0xf) == 0) 12116 (void) strlcat(msg_buf, "nothing", MAXPATHLEN); 12117 cmn_err(CE_CONT, "?%s", msg_buf); 12118 12119 (void) sprintf(msg_buf, "\tNumber of exposed device fan-out ports: %d", 12120 gscr2 & SATA_PMULT_PORTNUM_MASK); 12121 cmn_err(CE_CONT, "?%s", msg_buf); 12122 } 12123 12124 /* 12125 * sata_save_drive_settings extracts current setting of the device and stores 12126 * it for future reference, in case the device setup would need to be restored 12127 * after the device reset. 12128 * 12129 * For all devices read ahead and write cache settings are saved, if the 12130 * device supports these features at all. 12131 * For ATAPI devices the Removable Media Status Notification setting is saved. 12132 */ 12133 static void 12134 sata_save_drive_settings(sata_drive_info_t *sdinfo) 12135 { 12136 if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) || 12137 SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) { 12138 12139 /* Current setting of Read Ahead (and Read Cache) */ 12140 if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id)) 12141 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 12142 else 12143 sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD; 12144 12145 /* Current setting of Write Cache */ 12146 if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id)) 12147 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 12148 else 12149 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 12150 } 12151 12152 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) { 12153 if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id)) 12154 sdinfo->satadrv_settings |= SATA_DEV_RMSN; 12155 else 12156 sdinfo->satadrv_settings &= ~SATA_DEV_RMSN; 12157 } 12158 } 12159 12160 12161 /* 12162 * sata_check_capacity function determines a disk capacity 12163 * and addressing mode (LBA28/LBA48) by examining a disk identify device data. 12164 * 12165 * NOTE: CHS mode is not supported! If a device does not support LBA, 12166 * this function is not called. 12167 * 12168 * Returns device capacity in number of blocks, i.e. largest addressable LBA+1 12169 */ 12170 static uint64_t 12171 sata_check_capacity(sata_drive_info_t *sdinfo) 12172 { 12173 uint64_t capacity = 0; 12174 int i; 12175 12176 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK || 12177 !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT) 12178 /* Capacity valid only for LBA-addressable disk devices */ 12179 return (0); 12180 12181 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) && 12182 (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) && 12183 (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) { 12184 /* LBA48 mode supported and enabled */ 12185 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 | 12186 SATA_DEV_F_LBA28; 12187 for (i = 3; i >= 0; --i) { 12188 capacity <<= 16; 12189 capacity += sdinfo->satadrv_id.ai_addrsecxt[i]; 12190 } 12191 } else { 12192 capacity = sdinfo->satadrv_id.ai_addrsec[1]; 12193 capacity <<= 16; 12194 capacity += sdinfo->satadrv_id.ai_addrsec[0]; 12195 if (capacity >= 0x1000000) 12196 /* LBA28 mode */ 12197 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28; 12198 } 12199 return (capacity); 12200 } 12201 12202 12203 /* 12204 * Allocate consistent buffer for DMA transfer 12205 * 12206 * Cannot be called from interrupt level or with mutex held - it may sleep. 12207 * 12208 * Returns pointer to allocated buffer structure, or NULL if allocation failed. 12209 */ 12210 static struct buf * 12211 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len) 12212 { 12213 struct scsi_address ap; 12214 struct buf *bp; 12215 ddi_dma_attr_t cur_dma_attr; 12216 12217 ASSERT(spx->txlt_sata_pkt != NULL); 12218 ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran; 12219 ap.a_target = SATA_TO_SCSI_TARGET( 12220 spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport, 12221 spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport, 12222 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual); 12223 ap.a_lun = 0; 12224 12225 bp = scsi_alloc_consistent_buf(&ap, NULL, len, 12226 B_READ, SLEEP_FUNC, NULL); 12227 12228 if (bp != NULL) { 12229 /* Allocate DMA resources for this buffer */ 12230 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 12231 /* 12232 * We use a local version of the dma_attr, to account 12233 * for a device addressing limitations. 12234 * sata_adjust_dma_attr() will handle sdinfo == NULL which 12235 * will cause dma attributes to be adjusted to a lowest 12236 * acceptable level. 12237 */ 12238 sata_adjust_dma_attr(NULL, 12239 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 12240 12241 if (sata_dma_buf_setup(spx, PKT_CONSISTENT, 12242 SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) { 12243 scsi_free_consistent_buf(bp); 12244 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 12245 bp = NULL; 12246 } 12247 } 12248 return (bp); 12249 } 12250 12251 /* 12252 * Release local buffer (consistent buffer for DMA transfer) allocated 12253 * via sata_alloc_local_buffer(). 12254 */ 12255 static void 12256 sata_free_local_buffer(sata_pkt_txlate_t *spx) 12257 { 12258 ASSERT(spx->txlt_sata_pkt != NULL); 12259 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL); 12260 12261 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0; 12262 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL; 12263 12264 sata_common_free_dma_rsrcs(spx); 12265 12266 /* Free buffer */ 12267 scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp); 12268 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 12269 } 12270 12271 /* 12272 * Allocate sata_pkt 12273 * Pkt structure version and embedded strcutures version are initialized. 12274 * sata_pkt and sata_pkt_txlate structures are cross-linked. 12275 * 12276 * Since this may be called in interrupt context by sata_scsi_init_pkt, 12277 * callback argument determines if it can sleep or not. 12278 * Hence, it should not be called from interrupt context. 12279 * 12280 * If successful, non-NULL pointer to a sata pkt is returned. 12281 * Upon failure, NULL pointer is returned. 12282 */ 12283 static sata_pkt_t * 12284 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t)) 12285 { 12286 sata_pkt_t *spkt; 12287 int kmsflag; 12288 12289 kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP; 12290 spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag); 12291 if (spkt == NULL) { 12292 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 12293 "sata_pkt_alloc: failed")); 12294 return (NULL); 12295 } 12296 spkt->satapkt_rev = SATA_PKT_REV; 12297 spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV; 12298 spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 12299 spkt->satapkt_framework_private = spx; 12300 spx->txlt_sata_pkt = spkt; 12301 return (spkt); 12302 } 12303 12304 /* 12305 * Free sata pkt allocated via sata_pkt_alloc() 12306 */ 12307 static void 12308 sata_pkt_free(sata_pkt_txlate_t *spx) 12309 { 12310 ASSERT(spx->txlt_sata_pkt != NULL); 12311 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL); 12312 kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t)); 12313 spx->txlt_sata_pkt = NULL; 12314 } 12315 12316 12317 /* 12318 * Adjust DMA attributes. 12319 * SCSI cmds block count is up to 24 bits, SATA cmd block count vary 12320 * from 8 bits to 16 bits, depending on a command being used. 12321 * Limiting max block count arbitrarily to 256 for all read/write 12322 * commands may affects performance, so check both the device and 12323 * controller capability before adjusting dma attributes. 12324 */ 12325 void 12326 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr, 12327 ddi_dma_attr_t *adj_dma_attr) 12328 { 12329 uint32_t count_max; 12330 12331 /* Copy original attributes */ 12332 *adj_dma_attr = *dma_attr; 12333 /* 12334 * Things to consider: device addressing capability, 12335 * "excessive" controller DMA capabilities. 12336 * If a device is being probed/initialized, there are 12337 * no device info - use default limits then. 12338 */ 12339 if (sdinfo == NULL) { 12340 count_max = dma_attr->dma_attr_granular * 0x100; 12341 if (dma_attr->dma_attr_count_max > count_max) 12342 adj_dma_attr->dma_attr_count_max = count_max; 12343 if (dma_attr->dma_attr_maxxfer > count_max) 12344 adj_dma_attr->dma_attr_maxxfer = count_max; 12345 return; 12346 } 12347 12348 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 12349 if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) { 12350 /* 12351 * 16-bit sector count may be used - we rely on 12352 * the assumption that only read and write cmds 12353 * will request more than 256 sectors worth of data 12354 */ 12355 count_max = adj_dma_attr->dma_attr_granular * 0x10000; 12356 } else { 12357 /* 12358 * 8-bit sector count will be used - default limits 12359 * for dma attributes 12360 */ 12361 count_max = adj_dma_attr->dma_attr_granular * 0x100; 12362 } 12363 /* 12364 * Adjust controler dma attributes, if necessary 12365 */ 12366 if (dma_attr->dma_attr_count_max > count_max) 12367 adj_dma_attr->dma_attr_count_max = count_max; 12368 if (dma_attr->dma_attr_maxxfer > count_max) 12369 adj_dma_attr->dma_attr_maxxfer = count_max; 12370 } 12371 } 12372 12373 12374 /* 12375 * Allocate DMA resources for the buffer 12376 * This function handles initial DMA resource allocation as well as 12377 * DMA window shift and may be called repeatedly for the same DMA window 12378 * until all DMA cookies in the DMA window are processed. 12379 * To guarantee that there is always a coherent set of cookies to process 12380 * by SATA HBA driver (observing alignment, device granularity, etc.), 12381 * the number of slots for DMA cookies is equal to lesser of a number of 12382 * cookies in a DMA window and a max number of scatter/gather entries. 12383 * 12384 * Returns DDI_SUCCESS upon successful operation. 12385 * Return failure code of a failing command or DDI_FAILURE when 12386 * internal cleanup failed. 12387 */ 12388 static int 12389 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags, 12390 int (*callback)(caddr_t), caddr_t arg, 12391 ddi_dma_attr_t *cur_dma_attr) 12392 { 12393 int rval; 12394 off_t offset; 12395 size_t size; 12396 int max_sg_len, req_len, i; 12397 uint_t dma_flags; 12398 struct buf *bp; 12399 uint64_t cur_txfer_len; 12400 12401 12402 ASSERT(spx->txlt_sata_pkt != NULL); 12403 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 12404 ASSERT(bp != NULL); 12405 12406 12407 if (spx->txlt_buf_dma_handle == NULL) { 12408 /* 12409 * No DMA resources allocated so far - this is a first call 12410 * for this sata pkt. 12411 */ 12412 rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst), 12413 cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle); 12414 12415 if (rval != DDI_SUCCESS) { 12416 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 12417 "sata_dma_buf_setup: no buf DMA resources %x", 12418 rval)); 12419 return (rval); 12420 } 12421 12422 if (bp->b_flags & B_READ) 12423 dma_flags = DDI_DMA_READ; 12424 else 12425 dma_flags = DDI_DMA_WRITE; 12426 12427 if (flags & PKT_CONSISTENT) 12428 dma_flags |= DDI_DMA_CONSISTENT; 12429 12430 if (flags & PKT_DMA_PARTIAL) 12431 dma_flags |= DDI_DMA_PARTIAL; 12432 12433 /* 12434 * Check buffer alignment and size against dma attributes 12435 * Consider dma_attr_align only. There may be requests 12436 * with the size lower than device granularity, but they 12437 * will not read/write from/to the device, so no adjustment 12438 * is necessary. The dma_attr_minxfer theoretically should 12439 * be considered, but no HBA driver is checking it. 12440 */ 12441 if (IS_P2ALIGNED(bp->b_un.b_addr, 12442 cur_dma_attr->dma_attr_align)) { 12443 rval = ddi_dma_buf_bind_handle( 12444 spx->txlt_buf_dma_handle, 12445 bp, dma_flags, callback, arg, 12446 &spx->txlt_dma_cookie, 12447 &spx->txlt_curwin_num_dma_cookies); 12448 } else { /* Buffer is not aligned */ 12449 12450 int (*ddicallback)(caddr_t); 12451 size_t bufsz; 12452 12453 /* Check id sleeping is allowed */ 12454 ddicallback = (callback == NULL_FUNC) ? 12455 DDI_DMA_DONTWAIT : DDI_DMA_SLEEP; 12456 12457 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 12458 "mis-aligned buffer: addr=0x%p, cnt=%lu", 12459 (void *)bp->b_un.b_addr, bp->b_bcount); 12460 12461 if (bp->b_flags & (B_PAGEIO|B_PHYS)) 12462 /* 12463 * CPU will need to access data in the buffer 12464 * (for copying) so map it. 12465 */ 12466 bp_mapin(bp); 12467 12468 ASSERT(spx->txlt_tmp_buf == NULL); 12469 12470 /* Buffer may be padded by ddi_dma_mem_alloc()! */ 12471 rval = ddi_dma_mem_alloc( 12472 spx->txlt_buf_dma_handle, 12473 bp->b_bcount, 12474 &sata_acc_attr, 12475 DDI_DMA_STREAMING, 12476 ddicallback, NULL, 12477 &spx->txlt_tmp_buf, 12478 &bufsz, 12479 &spx->txlt_tmp_buf_handle); 12480 12481 if (rval != DDI_SUCCESS) { 12482 /* DMA mapping failed */ 12483 (void) ddi_dma_free_handle( 12484 &spx->txlt_buf_dma_handle); 12485 spx->txlt_buf_dma_handle = NULL; 12486 #ifdef SATA_DEBUG 12487 mbuffail_count++; 12488 #endif 12489 SATADBG1(SATA_DBG_DMA_SETUP, 12490 spx->txlt_sata_hba_inst, 12491 "sata_dma_buf_setup: " 12492 "buf dma mem alloc failed %x\n", rval); 12493 return (rval); 12494 } 12495 ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf, 12496 cur_dma_attr->dma_attr_align)); 12497 12498 #ifdef SATA_DEBUG 12499 mbuf_count++; 12500 12501 if (bp->b_bcount != bufsz) 12502 /* 12503 * This will require special handling, because 12504 * DMA cookies will be based on the temporary 12505 * buffer size, not the original buffer 12506 * b_bcount, so the residue may have to 12507 * be counted differently. 12508 */ 12509 SATADBG2(SATA_DBG_DMA_SETUP, 12510 spx->txlt_sata_hba_inst, 12511 "sata_dma_buf_setup: bp size %x != " 12512 "bufsz %x\n", bp->b_bcount, bufsz); 12513 #endif 12514 if (dma_flags & DDI_DMA_WRITE) { 12515 /* 12516 * Write operation - copy data into 12517 * an aligned temporary buffer. Buffer will be 12518 * synced for device by ddi_dma_addr_bind_handle 12519 */ 12520 bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf, 12521 bp->b_bcount); 12522 } 12523 12524 rval = ddi_dma_addr_bind_handle( 12525 spx->txlt_buf_dma_handle, 12526 NULL, 12527 spx->txlt_tmp_buf, 12528 bufsz, dma_flags, ddicallback, 0, 12529 &spx->txlt_dma_cookie, 12530 &spx->txlt_curwin_num_dma_cookies); 12531 } 12532 12533 switch (rval) { 12534 case DDI_DMA_PARTIAL_MAP: 12535 SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 12536 "sata_dma_buf_setup: DMA Partial Map\n", NULL); 12537 /* 12538 * Partial DMA mapping. 12539 * Retrieve number of DMA windows for this request. 12540 */ 12541 if (ddi_dma_numwin(spx->txlt_buf_dma_handle, 12542 &spx->txlt_num_dma_win) != DDI_SUCCESS) { 12543 if (spx->txlt_tmp_buf != NULL) { 12544 ddi_dma_mem_free( 12545 &spx->txlt_tmp_buf_handle); 12546 spx->txlt_tmp_buf = NULL; 12547 } 12548 (void) ddi_dma_unbind_handle( 12549 spx->txlt_buf_dma_handle); 12550 (void) ddi_dma_free_handle( 12551 &spx->txlt_buf_dma_handle); 12552 spx->txlt_buf_dma_handle = NULL; 12553 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 12554 "sata_dma_buf_setup: numwin failed\n")); 12555 return (DDI_FAILURE); 12556 } 12557 SATADBG2(SATA_DBG_DMA_SETUP, 12558 spx->txlt_sata_hba_inst, 12559 "sata_dma_buf_setup: windows: %d, cookies: %d\n", 12560 spx->txlt_num_dma_win, 12561 spx->txlt_curwin_num_dma_cookies); 12562 spx->txlt_cur_dma_win = 0; 12563 break; 12564 12565 case DDI_DMA_MAPPED: 12566 /* DMA fully mapped */ 12567 spx->txlt_num_dma_win = 1; 12568 spx->txlt_cur_dma_win = 0; 12569 SATADBG1(SATA_DBG_DMA_SETUP, 12570 spx->txlt_sata_hba_inst, 12571 "sata_dma_buf_setup: windows: 1 " 12572 "cookies: %d\n", spx->txlt_curwin_num_dma_cookies); 12573 break; 12574 12575 default: 12576 /* DMA mapping failed */ 12577 if (spx->txlt_tmp_buf != NULL) { 12578 ddi_dma_mem_free( 12579 &spx->txlt_tmp_buf_handle); 12580 spx->txlt_tmp_buf = NULL; 12581 } 12582 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 12583 spx->txlt_buf_dma_handle = NULL; 12584 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 12585 "sata_dma_buf_setup: buf dma handle binding " 12586 "failed %x\n", rval)); 12587 return (rval); 12588 } 12589 spx->txlt_curwin_processed_dma_cookies = 0; 12590 spx->txlt_dma_cookie_list = NULL; 12591 } else { 12592 /* 12593 * DMA setup is reused. Check if we need to process more 12594 * cookies in current window, or to get next window, if any. 12595 */ 12596 12597 ASSERT(spx->txlt_curwin_processed_dma_cookies <= 12598 spx->txlt_curwin_num_dma_cookies); 12599 12600 if (spx->txlt_curwin_processed_dma_cookies == 12601 spx->txlt_curwin_num_dma_cookies) { 12602 /* 12603 * All cookies from current DMA window were processed. 12604 * Get next DMA window. 12605 */ 12606 spx->txlt_cur_dma_win++; 12607 if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) { 12608 (void) ddi_dma_getwin(spx->txlt_buf_dma_handle, 12609 spx->txlt_cur_dma_win, &offset, &size, 12610 &spx->txlt_dma_cookie, 12611 &spx->txlt_curwin_num_dma_cookies); 12612 spx->txlt_curwin_processed_dma_cookies = 0; 12613 } else { 12614 /* No more windows! End of request! */ 12615 /* What to do? - panic for now */ 12616 ASSERT(spx->txlt_cur_dma_win >= 12617 spx->txlt_num_dma_win); 12618 12619 spx->txlt_curwin_num_dma_cookies = 0; 12620 spx->txlt_curwin_processed_dma_cookies = 0; 12621 spx->txlt_sata_pkt-> 12622 satapkt_cmd.satacmd_num_dma_cookies = 0; 12623 return (DDI_SUCCESS); 12624 } 12625 } 12626 } 12627 /* There better be at least one DMA cookie outstanding */ 12628 ASSERT((spx->txlt_curwin_num_dma_cookies - 12629 spx->txlt_curwin_processed_dma_cookies) > 0); 12630 12631 if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) { 12632 /* The default cookie slot was used in previous run */ 12633 ASSERT(spx->txlt_curwin_processed_dma_cookies == 0); 12634 spx->txlt_dma_cookie_list = NULL; 12635 spx->txlt_dma_cookie_list_len = 0; 12636 } 12637 if (spx->txlt_curwin_processed_dma_cookies == 0) { 12638 /* 12639 * Processing a new DMA window - set-up dma cookies list. 12640 * We may reuse previously allocated cookie array if it is 12641 * possible. 12642 */ 12643 if (spx->txlt_dma_cookie_list != NULL && 12644 spx->txlt_dma_cookie_list_len < 12645 spx->txlt_curwin_num_dma_cookies) { 12646 /* 12647 * New DMA window contains more cookies than 12648 * the previous one. We need larger cookie list - free 12649 * the old one. 12650 */ 12651 (void) kmem_free(spx->txlt_dma_cookie_list, 12652 spx->txlt_dma_cookie_list_len * 12653 sizeof (ddi_dma_cookie_t)); 12654 spx->txlt_dma_cookie_list = NULL; 12655 spx->txlt_dma_cookie_list_len = 0; 12656 } 12657 if (spx->txlt_dma_cookie_list == NULL) { 12658 /* 12659 * Calculate lesser of number of cookies in this 12660 * DMA window and number of s/g entries. 12661 */ 12662 max_sg_len = cur_dma_attr->dma_attr_sgllen; 12663 req_len = MIN(max_sg_len, 12664 spx->txlt_curwin_num_dma_cookies); 12665 12666 /* Allocate new dma cookie array if necessary */ 12667 if (req_len == 1) { 12668 /* Only one cookie - no need for a list */ 12669 spx->txlt_dma_cookie_list = 12670 &spx->txlt_dma_cookie; 12671 spx->txlt_dma_cookie_list_len = 1; 12672 } else { 12673 /* 12674 * More than one cookie - try to allocate space. 12675 */ 12676 spx->txlt_dma_cookie_list = kmem_zalloc( 12677 sizeof (ddi_dma_cookie_t) * req_len, 12678 callback == NULL_FUNC ? KM_NOSLEEP : 12679 KM_SLEEP); 12680 if (spx->txlt_dma_cookie_list == NULL) { 12681 SATADBG1(SATA_DBG_DMA_SETUP, 12682 spx->txlt_sata_hba_inst, 12683 "sata_dma_buf_setup: cookie list " 12684 "allocation failed\n", NULL); 12685 /* 12686 * We could not allocate space for 12687 * neccessary number of dma cookies in 12688 * this window, so we fail this request. 12689 * Next invocation would try again to 12690 * allocate space for cookie list. 12691 * Note:Packet residue was not modified. 12692 */ 12693 return (DDI_DMA_NORESOURCES); 12694 } else { 12695 spx->txlt_dma_cookie_list_len = req_len; 12696 } 12697 } 12698 } 12699 /* 12700 * Fetch DMA cookies into cookie list in sata_pkt_txlate. 12701 * First cookie was already fetched. 12702 */ 12703 *(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie; 12704 cur_txfer_len = 12705 (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size; 12706 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1; 12707 spx->txlt_curwin_processed_dma_cookies++; 12708 for (i = 1; (i < spx->txlt_dma_cookie_list_len) && 12709 (i < spx->txlt_curwin_num_dma_cookies); i++) { 12710 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 12711 &spx->txlt_dma_cookie_list[i]); 12712 cur_txfer_len += 12713 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 12714 spx->txlt_curwin_processed_dma_cookies++; 12715 spx->txlt_sata_pkt-> 12716 satapkt_cmd.satacmd_num_dma_cookies += 1; 12717 } 12718 } else { 12719 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 12720 "sata_dma_buf_setup: sliding within DMA window, " 12721 "cur cookie %d, total cookies %d\n", 12722 spx->txlt_curwin_processed_dma_cookies, 12723 spx->txlt_curwin_num_dma_cookies); 12724 12725 /* 12726 * Not all cookies from the current dma window were used because 12727 * of s/g limitation. 12728 * There is no need to re-size the list - it was set at 12729 * optimal size, or only default entry is used (s/g = 1). 12730 */ 12731 if (spx->txlt_dma_cookie_list == NULL) { 12732 spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie; 12733 spx->txlt_dma_cookie_list_len = 1; 12734 } 12735 /* 12736 * Since we are processing remaining cookies in a DMA window, 12737 * there may be less of them than the number of entries in the 12738 * current dma cookie list. 12739 */ 12740 req_len = MIN(spx->txlt_dma_cookie_list_len, 12741 (spx->txlt_curwin_num_dma_cookies - 12742 spx->txlt_curwin_processed_dma_cookies)); 12743 12744 /* Fetch the next batch of cookies */ 12745 for (i = 0, cur_txfer_len = 0; i < req_len; i++) { 12746 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 12747 &spx->txlt_dma_cookie_list[i]); 12748 cur_txfer_len += 12749 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 12750 spx->txlt_sata_pkt-> 12751 satapkt_cmd.satacmd_num_dma_cookies++; 12752 spx->txlt_curwin_processed_dma_cookies++; 12753 } 12754 } 12755 12756 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0); 12757 12758 /* Point sata_cmd to the cookie list */ 12759 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = 12760 &spx->txlt_dma_cookie_list[0]; 12761 12762 /* Remember number of DMA cookies passed in sata packet */ 12763 spx->txlt_num_dma_cookies = 12764 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies; 12765 12766 ASSERT(cur_txfer_len != 0); 12767 if (cur_txfer_len <= bp->b_bcount) 12768 spx->txlt_total_residue -= cur_txfer_len; 12769 else { 12770 /* 12771 * Temporary DMA buffer has been padded by 12772 * ddi_dma_mem_alloc()! 12773 * This requires special handling, because DMA cookies are 12774 * based on the temporary buffer size, not the b_bcount, 12775 * and we have extra bytes to transfer - but the packet 12776 * residue has to stay correct because we will copy only 12777 * the requested number of bytes. 12778 */ 12779 spx->txlt_total_residue -= bp->b_bcount; 12780 } 12781 12782 return (DDI_SUCCESS); 12783 } 12784 12785 /* 12786 * Common routine for releasing DMA resources 12787 */ 12788 static void 12789 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx) 12790 { 12791 if (spx->txlt_buf_dma_handle != NULL) { 12792 if (spx->txlt_tmp_buf != NULL) { 12793 /* 12794 * Intermediate DMA buffer was allocated. 12795 * Free allocated buffer and associated access handle. 12796 */ 12797 ddi_dma_mem_free(&spx->txlt_tmp_buf_handle); 12798 spx->txlt_tmp_buf = NULL; 12799 } 12800 /* 12801 * Free DMA resources - cookies and handles 12802 */ 12803 /* ASSERT(spx->txlt_dma_cookie_list != NULL); */ 12804 if (spx->txlt_dma_cookie_list != NULL) { 12805 if (spx->txlt_dma_cookie_list != 12806 &spx->txlt_dma_cookie) { 12807 (void) kmem_free(spx->txlt_dma_cookie_list, 12808 spx->txlt_dma_cookie_list_len * 12809 sizeof (ddi_dma_cookie_t)); 12810 spx->txlt_dma_cookie_list = NULL; 12811 } 12812 } 12813 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle); 12814 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 12815 spx->txlt_buf_dma_handle = NULL; 12816 } 12817 } 12818 12819 /* 12820 * Free DMA resources 12821 * Used by the HBA driver to release DMA resources that it does not use. 12822 * 12823 * Returns Void 12824 */ 12825 void 12826 sata_free_dma_resources(sata_pkt_t *sata_pkt) 12827 { 12828 sata_pkt_txlate_t *spx; 12829 12830 if (sata_pkt == NULL) 12831 return; 12832 12833 spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 12834 12835 sata_common_free_dma_rsrcs(spx); 12836 } 12837 12838 /* 12839 * Fetch Device Identify data. 12840 * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type) 12841 * command to a device and get the device identify data. 12842 * The device_info structure has to be set to device type (for selecting proper 12843 * device identify command). 12844 * 12845 * Returns: 12846 * SATA_SUCCESS if cmd succeeded 12847 * SATA_RETRY if cmd was rejected and could be retried, 12848 * SATA_FAILURE if cmd failed and should not be retried (port error) 12849 * 12850 * Cannot be called in an interrupt context. 12851 */ 12852 12853 static int 12854 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst, 12855 sata_drive_info_t *sdinfo) 12856 { 12857 struct buf *bp; 12858 sata_pkt_t *spkt; 12859 sata_cmd_t *scmd; 12860 sata_pkt_txlate_t *spx; 12861 int rval; 12862 12863 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12864 spx->txlt_sata_hba_inst = sata_hba_inst; 12865 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12866 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12867 if (spkt == NULL) { 12868 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12869 return (SATA_RETRY); /* may retry later */ 12870 } 12871 /* address is needed now */ 12872 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12873 12874 /* 12875 * Allocate buffer for Identify Data return data 12876 */ 12877 bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t)); 12878 if (bp == NULL) { 12879 sata_pkt_free(spx); 12880 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12881 SATA_LOG_D((sata_hba_inst, CE_WARN, 12882 "sata_fetch_device_identify_data: " 12883 "cannot allocate buffer for ID")); 12884 return (SATA_RETRY); /* may retry later */ 12885 } 12886 12887 /* Fill sata_pkt */ 12888 sdinfo->satadrv_state = SATA_STATE_PROBING; 12889 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12890 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12891 /* Synchronous mode, no callback */ 12892 spkt->satapkt_comp = NULL; 12893 /* Timeout 30s */ 12894 spkt->satapkt_time = sata_default_pkt_time; 12895 12896 scmd = &spkt->satapkt_cmd; 12897 scmd->satacmd_bp = bp; 12898 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 12899 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 12900 12901 /* Build Identify Device cmd in the sata_pkt */ 12902 scmd->satacmd_addr_type = 0; /* N/A */ 12903 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 12904 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 12905 scmd->satacmd_lba_mid_lsb = 0; /* N/A */ 12906 scmd->satacmd_lba_high_lsb = 0; /* N/A */ 12907 scmd->satacmd_features_reg = 0; /* N/A */ 12908 scmd->satacmd_device_reg = 0; /* Always device 0 */ 12909 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 12910 /* Identify Packet Device cmd */ 12911 scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE; 12912 } else { 12913 /* Identify Device cmd - mandatory for all other devices */ 12914 scmd->satacmd_cmd_reg = SATAC_ID_DEVICE; 12915 } 12916 12917 /* Send pkt to SATA HBA driver */ 12918 rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt); 12919 12920 #ifdef SATA_INJECT_FAULTS 12921 sata_inject_pkt_fault(spkt, &rval, sata_fault_type); 12922 #endif 12923 12924 if (rval == SATA_TRAN_ACCEPTED && 12925 spkt->satapkt_reason == SATA_PKT_COMPLETED) { 12926 if (spx->txlt_buf_dma_handle != NULL) { 12927 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 12928 DDI_DMA_SYNC_FORKERNEL); 12929 ASSERT(rval == DDI_SUCCESS); 12930 } 12931 if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config & 12932 SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) { 12933 SATA_LOG_D((sata_hba_inst, CE_WARN, 12934 "SATA disk device at port %d - " 12935 "partial Identify Data", 12936 sdinfo->satadrv_addr.cport)); 12937 rval = SATA_RETRY; /* may retry later */ 12938 goto fail; 12939 } 12940 /* Update sata_drive_info */ 12941 bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id, 12942 sizeof (sata_id_t)); 12943 12944 sdinfo->satadrv_features_support = 0; 12945 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 12946 /* 12947 * Retrieve capacity (disks only) and addressing mode 12948 */ 12949 sdinfo->satadrv_capacity = sata_check_capacity(sdinfo); 12950 } else { 12951 /* 12952 * For ATAPI devices one would have to issue 12953 * Get Capacity cmd for media capacity. Not here. 12954 */ 12955 sdinfo->satadrv_capacity = 0; 12956 /* 12957 * Check what cdb length is supported 12958 */ 12959 if ((sdinfo->satadrv_id.ai_config & 12960 SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B) 12961 sdinfo->satadrv_atapi_cdb_len = 16; 12962 else 12963 sdinfo->satadrv_atapi_cdb_len = 12; 12964 } 12965 /* Setup supported features flags */ 12966 if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) 12967 sdinfo->satadrv_features_support |= SATA_DEV_F_DMA; 12968 12969 /* Check for SATA GEN and NCQ support */ 12970 if (sdinfo->satadrv_id.ai_satacap != 0 && 12971 sdinfo->satadrv_id.ai_satacap != 0xffff) { 12972 /* SATA compliance */ 12973 if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ) 12974 sdinfo->satadrv_features_support |= 12975 SATA_DEV_F_NCQ; 12976 if (sdinfo->satadrv_id.ai_satacap & 12977 (SATA_1_SPEED | SATA_2_SPEED)) { 12978 if (sdinfo->satadrv_id.ai_satacap & 12979 SATA_2_SPEED) 12980 sdinfo->satadrv_features_support |= 12981 SATA_DEV_F_SATA2; 12982 if (sdinfo->satadrv_id.ai_satacap & 12983 SATA_1_SPEED) 12984 sdinfo->satadrv_features_support |= 12985 SATA_DEV_F_SATA1; 12986 } else { 12987 sdinfo->satadrv_features_support |= 12988 SATA_DEV_F_SATA1; 12989 } 12990 } 12991 if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) && 12992 (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD)) 12993 sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ; 12994 12995 sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth; 12996 if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) || 12997 (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) { 12998 ++sdinfo->satadrv_queue_depth; 12999 /* Adjust according to controller capabilities */ 13000 sdinfo->satadrv_max_queue_depth = MIN( 13001 sdinfo->satadrv_queue_depth, 13002 SATA_QDEPTH(sata_hba_inst)); 13003 /* Adjust according to global queue depth limit */ 13004 sdinfo->satadrv_max_queue_depth = MIN( 13005 sdinfo->satadrv_max_queue_depth, 13006 sata_current_max_qdepth); 13007 if (sdinfo->satadrv_max_queue_depth == 0) 13008 sdinfo->satadrv_max_queue_depth = 1; 13009 } else 13010 sdinfo->satadrv_max_queue_depth = 1; 13011 13012 rval = SATA_SUCCESS; 13013 } else { 13014 /* 13015 * Woops, no Identify Data. 13016 */ 13017 if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) { 13018 rval = SATA_RETRY; /* may retry later */ 13019 } else if (rval == SATA_TRAN_ACCEPTED) { 13020 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR || 13021 spkt->satapkt_reason == SATA_PKT_ABORTED || 13022 spkt->satapkt_reason == SATA_PKT_TIMEOUT || 13023 spkt->satapkt_reason == SATA_PKT_RESET) 13024 rval = SATA_RETRY; /* may retry later */ 13025 else 13026 rval = SATA_FAILURE; 13027 } else { 13028 rval = SATA_FAILURE; 13029 } 13030 } 13031 fail: 13032 /* Free allocated resources */ 13033 sata_free_local_buffer(spx); 13034 sata_pkt_free(spx); 13035 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13036 13037 return (rval); 13038 } 13039 13040 13041 /* 13042 * Some devices may not come-up with default DMA mode (UDMA or MWDMA). 13043 * UDMA mode is checked first, followed by MWDMA mode. 13044 * set correctly, so this function is setting it to the highest supported level. 13045 * Older SATA spec required that the device supports at least DMA 4 mode and 13046 * UDMA mode is selected. It is not mentioned in SerialATA 2.6, so this 13047 * restriction has been removed. 13048 * 13049 * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported. 13050 * Returns SATA_FAILURE if proper DMA mode could not be selected. 13051 * 13052 * NOTE: This function should be called only if DMA mode is supported. 13053 */ 13054 static int 13055 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo) 13056 { 13057 sata_pkt_t *spkt; 13058 sata_cmd_t *scmd; 13059 sata_pkt_txlate_t *spx; 13060 int i, mode; 13061 uint8_t subcmd; 13062 int rval = SATA_SUCCESS; 13063 13064 ASSERT(sdinfo != NULL); 13065 ASSERT(sata_hba_inst != NULL); 13066 13067 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 && 13068 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) { 13069 /* Find highest Ultra DMA mode supported */ 13070 for (mode = 6; mode >= 0; --mode) { 13071 if (sdinfo->satadrv_id.ai_ultradma & (1 << mode)) 13072 break; 13073 } 13074 #if 0 13075 /* Left for historical reasons */ 13076 /* 13077 * Some initial version of SATA spec indicated that at least 13078 * UDMA mode 4 has to be supported. It is not mentioned in 13079 * SerialATA 2.6, so this restriction is removed. 13080 */ 13081 if (mode < 4) 13082 return (SATA_FAILURE); 13083 #endif 13084 13085 /* 13086 * For disk, we're still going to set DMA mode whatever is 13087 * selected by default 13088 * 13089 * We saw an old maxtor sata drive will select Ultra DMA and 13090 * Multi-Word DMA simultaneouly by default, which is going 13091 * to cause DMA command timed out, so we need to select DMA 13092 * mode even when it's already done by default 13093 */ 13094 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) { 13095 13096 /* Find UDMA mode currently selected */ 13097 for (i = 6; i >= 0; --i) { 13098 if (sdinfo->satadrv_id.ai_ultradma & 13099 (1 << (i + 8))) 13100 break; 13101 } 13102 if (i >= mode) 13103 /* Nothing to do */ 13104 return (SATA_SUCCESS); 13105 } 13106 13107 subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA; 13108 13109 } else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) { 13110 /* Find highest MultiWord DMA mode supported */ 13111 for (mode = 2; mode >= 0; --mode) { 13112 if (sdinfo->satadrv_id.ai_dworddma & (1 << mode)) 13113 break; 13114 } 13115 13116 /* 13117 * For disk, We're still going to set DMA mode whatever is 13118 * selected by default 13119 * 13120 * We saw an old maxtor sata drive will select Ultra DMA and 13121 * Multi-Word DMA simultaneouly by default, which is going 13122 * to cause DMA command timed out, so we need to select DMA 13123 * mode even when it's already done by default 13124 */ 13125 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) { 13126 13127 /* Find highest MultiWord DMA mode selected */ 13128 for (i = 2; i >= 0; --i) { 13129 if (sdinfo->satadrv_id.ai_dworddma & 13130 (1 << (i + 8))) 13131 break; 13132 } 13133 if (i >= mode) 13134 /* Nothing to do */ 13135 return (SATA_SUCCESS); 13136 } 13137 13138 subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA; 13139 } else 13140 return (SATA_SUCCESS); 13141 13142 /* 13143 * Set DMA mode via SET FEATURES COMMAND. 13144 * Prepare packet for SET FEATURES COMMAND. 13145 */ 13146 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13147 spx->txlt_sata_hba_inst = sata_hba_inst; 13148 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13149 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13150 if (spkt == NULL) { 13151 SATA_LOG_D((sata_hba_inst, CE_WARN, 13152 "sata_set_dma_mode: could not set DMA mode %d", mode)); 13153 rval = SATA_FAILURE; 13154 goto done; 13155 } 13156 /* Fill sata_pkt */ 13157 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13158 /* Timeout 30s */ 13159 spkt->satapkt_time = sata_default_pkt_time; 13160 /* Synchronous mode, no callback, interrupts */ 13161 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13162 spkt->satapkt_comp = NULL; 13163 scmd = &spkt->satapkt_cmd; 13164 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 13165 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 13166 scmd->satacmd_addr_type = 0; 13167 scmd->satacmd_device_reg = 0; 13168 scmd->satacmd_status_reg = 0; 13169 scmd->satacmd_error_reg = 0; 13170 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 13171 scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE; 13172 scmd->satacmd_sec_count_lsb = subcmd | mode; 13173 13174 /* Transfer command to HBA */ 13175 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 13176 spkt) != SATA_TRAN_ACCEPTED || 13177 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 13178 /* Pkt execution failed */ 13179 rval = SATA_FAILURE; 13180 } 13181 done: 13182 13183 /* Free allocated resources */ 13184 if (spkt != NULL) 13185 sata_pkt_free(spx); 13186 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13187 13188 return (rval); 13189 } 13190 13191 13192 /* 13193 * Set device caching mode. 13194 * One of the following operations should be specified: 13195 * SATAC_SF_ENABLE_READ_AHEAD 13196 * SATAC_SF_DISABLE_READ_AHEAD 13197 * SATAC_SF_ENABLE_WRITE_CACHE 13198 * SATAC_SF_DISABLE_WRITE_CACHE 13199 * 13200 * If operation fails, system log messgage is emitted. 13201 * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if 13202 * command was sent but did not succeed, and SATA_FAILURE otherwise. 13203 */ 13204 13205 static int 13206 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 13207 int cache_op) 13208 { 13209 sata_pkt_t *spkt; 13210 sata_cmd_t *scmd; 13211 sata_pkt_txlate_t *spx; 13212 int rval = SATA_SUCCESS; 13213 int hba_rval; 13214 char *infop; 13215 13216 ASSERT(sdinfo != NULL); 13217 ASSERT(sata_hba_inst != NULL); 13218 ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD || 13219 cache_op == SATAC_SF_DISABLE_READ_AHEAD || 13220 cache_op == SATAC_SF_ENABLE_WRITE_CACHE || 13221 cache_op == SATAC_SF_DISABLE_WRITE_CACHE); 13222 13223 13224 /* Prepare packet for SET FEATURES COMMAND */ 13225 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13226 spx->txlt_sata_hba_inst = sata_hba_inst; 13227 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13228 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13229 if (spkt == NULL) { 13230 rval = SATA_FAILURE; 13231 goto failure; 13232 } 13233 /* Fill sata_pkt */ 13234 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13235 /* Timeout 30s */ 13236 spkt->satapkt_time = sata_default_pkt_time; 13237 /* Synchronous mode, no callback, interrupts */ 13238 spkt->satapkt_op_mode = 13239 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13240 spkt->satapkt_comp = NULL; 13241 scmd = &spkt->satapkt_cmd; 13242 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 13243 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 13244 scmd->satacmd_addr_type = 0; 13245 scmd->satacmd_device_reg = 0; 13246 scmd->satacmd_status_reg = 0; 13247 scmd->satacmd_error_reg = 0; 13248 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 13249 scmd->satacmd_features_reg = cache_op; 13250 13251 /* Transfer command to HBA */ 13252 hba_rval = (*SATA_START_FUNC(sata_hba_inst))( 13253 SATA_DIP(sata_hba_inst), spkt); 13254 13255 #ifdef SATA_INJECT_FAULTS 13256 sata_inject_pkt_fault(spkt, &rval, sata_fault_type); 13257 #endif 13258 13259 if ((hba_rval != SATA_TRAN_ACCEPTED) || 13260 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 13261 /* Pkt execution failed */ 13262 switch (cache_op) { 13263 case SATAC_SF_ENABLE_READ_AHEAD: 13264 infop = "enabling read ahead failed"; 13265 break; 13266 case SATAC_SF_DISABLE_READ_AHEAD: 13267 infop = "disabling read ahead failed"; 13268 break; 13269 case SATAC_SF_ENABLE_WRITE_CACHE: 13270 infop = "enabling write cache failed"; 13271 break; 13272 case SATAC_SF_DISABLE_WRITE_CACHE: 13273 infop = "disabling write cache failed"; 13274 break; 13275 } 13276 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 13277 rval = SATA_RETRY; 13278 } 13279 failure: 13280 /* Free allocated resources */ 13281 if (spkt != NULL) 13282 sata_pkt_free(spx); 13283 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13284 return (rval); 13285 } 13286 13287 /* 13288 * Set Removable Media Status Notification (enable/disable) 13289 * state == 0 , disable 13290 * state != 0 , enable 13291 * 13292 * If operation fails, system log messgage is emitted. 13293 * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise. 13294 */ 13295 13296 static int 13297 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 13298 int state) 13299 { 13300 sata_pkt_t *spkt; 13301 sata_cmd_t *scmd; 13302 sata_pkt_txlate_t *spx; 13303 int rval = SATA_SUCCESS; 13304 char *infop; 13305 13306 ASSERT(sdinfo != NULL); 13307 ASSERT(sata_hba_inst != NULL); 13308 13309 /* Prepare packet for SET FEATURES COMMAND */ 13310 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13311 spx->txlt_sata_hba_inst = sata_hba_inst; 13312 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13313 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13314 if (spkt == NULL) { 13315 rval = SATA_FAILURE; 13316 goto failure; 13317 } 13318 /* Fill sata_pkt */ 13319 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13320 /* Timeout 30s */ 13321 spkt->satapkt_time = sata_default_pkt_time; 13322 /* Synchronous mode, no callback, interrupts */ 13323 spkt->satapkt_op_mode = 13324 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13325 spkt->satapkt_comp = NULL; 13326 scmd = &spkt->satapkt_cmd; 13327 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 13328 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 13329 scmd->satacmd_addr_type = 0; 13330 scmd->satacmd_device_reg = 0; 13331 scmd->satacmd_status_reg = 0; 13332 scmd->satacmd_error_reg = 0; 13333 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 13334 if (state == 0) 13335 scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN; 13336 else 13337 scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN; 13338 13339 /* Transfer command to HBA */ 13340 if (((*SATA_START_FUNC(sata_hba_inst))( 13341 SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) || 13342 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 13343 /* Pkt execution failed */ 13344 if (state == 0) 13345 infop = "disabling Removable Media Status " 13346 "Notification failed"; 13347 else 13348 infop = "enabling Removable Media Status " 13349 "Notification failed"; 13350 13351 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 13352 rval = SATA_FAILURE; 13353 } 13354 failure: 13355 /* Free allocated resources */ 13356 if (spkt != NULL) 13357 sata_pkt_free(spx); 13358 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13359 return (rval); 13360 } 13361 13362 13363 /* 13364 * Update state and copy port ss* values from passed sata_device structure. 13365 * sata_address is validated - if not valid, nothing is changed in sata_scsi 13366 * configuration struct. 13367 * 13368 * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function 13369 * regardless of the state in device argument. 13370 * 13371 * Port mutex should be held while calling this function. 13372 */ 13373 static void 13374 sata_update_port_info(sata_hba_inst_t *sata_hba_inst, 13375 sata_device_t *sata_device) 13376 { 13377 sata_cport_info_t *cportinfo; 13378 13379 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT || 13380 sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) { 13381 if (SATA_NUM_CPORTS(sata_hba_inst) <= 13382 sata_device->satadev_addr.cport) 13383 return; 13384 13385 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 13386 sata_device->satadev_addr.cport); 13387 13388 ASSERT(mutex_owned(&cportinfo->cport_mutex)); 13389 cportinfo->cport_scr = sata_device->satadev_scr; 13390 13391 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 13392 cportinfo->cport_state &= ~(SATA_PSTATE_PWRON | 13393 SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED); 13394 cportinfo->cport_state |= 13395 sata_device->satadev_state & SATA_PSTATE_VALID; 13396 } 13397 } 13398 13399 void 13400 sata_update_pmport_info(sata_hba_inst_t *sata_hba_inst, 13401 sata_device_t *sata_device) 13402 { 13403 sata_pmport_info_t *pmportinfo; 13404 13405 if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT && 13406 sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) || 13407 SATA_NUM_PMPORTS(sata_hba_inst, 13408 sata_device->satadev_addr.cport) < 13409 sata_device->satadev_addr.pmport) { 13410 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 13411 "sata_update_port_info: error address %p.", 13412 &sata_device->satadev_addr); 13413 return; 13414 } 13415 13416 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 13417 sata_device->satadev_addr.cport, 13418 sata_device->satadev_addr.pmport); 13419 13420 ASSERT(mutex_owned(&pmportinfo->pmport_mutex)); 13421 pmportinfo->pmport_scr = sata_device->satadev_scr; 13422 13423 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 13424 pmportinfo->pmport_state &= 13425 ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED); 13426 pmportinfo->pmport_state |= 13427 sata_device->satadev_state & SATA_PSTATE_VALID; 13428 } 13429 13430 /* 13431 * Extract SATA port specification from an IOCTL argument. 13432 * 13433 * This function return the port the user land send us as is, unless it 13434 * cannot retrieve port spec, then -1 is returned. 13435 * 13436 * Support port multiplier. 13437 */ 13438 static int32_t 13439 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp) 13440 { 13441 int32_t port; 13442 13443 /* Extract port number from nvpair in dca structure */ 13444 if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) { 13445 SATA_LOG_D((sata_hba_inst, CE_NOTE, 13446 "sata_get_port_num: invalid port spec 0x%x in ioctl", 13447 port)); 13448 port = -1; 13449 } 13450 13451 return (port); 13452 } 13453 13454 /* 13455 * Get dev_info_t pointer to the device node pointed to by port argument. 13456 * NOTE: target argument is a value used in ioctls to identify 13457 * the AP - it is not a sata_address. 13458 * It is a combination of cport, pmport and address qualifier, encodded same 13459 * way as a scsi target number. 13460 * At this moment it carries only cport number. 13461 * 13462 * PMult hotplug is supported now. 13463 * 13464 * Returns dev_info_t pointer if target device was found, NULL otherwise. 13465 */ 13466 13467 static dev_info_t * 13468 sata_get_target_dip(dev_info_t *dip, uint8_t cport, uint8_t pmport) 13469 { 13470 dev_info_t *cdip = NULL; 13471 int target, tgt; 13472 int circ; 13473 uint8_t qual; 13474 13475 sata_hba_inst_t *sata_hba_inst; 13476 scsi_hba_tran_t *scsi_hba_tran; 13477 13478 /* Get target id */ 13479 scsi_hba_tran = ddi_get_driver_private(dip); 13480 if (scsi_hba_tran == NULL) 13481 return (NULL); 13482 13483 sata_hba_inst = scsi_hba_tran->tran_hba_private; 13484 13485 if (sata_hba_inst == NULL) 13486 return (NULL); 13487 13488 /* Identify a port-mult by cport_info.cport_dev_type */ 13489 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) 13490 qual = SATA_ADDR_DPMPORT; 13491 else 13492 qual = SATA_ADDR_DCPORT; 13493 13494 target = SATA_TO_SCSI_TARGET(cport, pmport, qual); 13495 13496 /* Retrieve target dip */ 13497 ndi_devi_enter(dip, &circ); 13498 for (cdip = ddi_get_child(dip); cdip != NULL; ) { 13499 dev_info_t *next = ddi_get_next_sibling(cdip); 13500 13501 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip, 13502 DDI_PROP_DONTPASS, "target", -1); 13503 if (tgt == -1) { 13504 /* 13505 * This is actually an error condition, but not 13506 * a fatal one. Just continue the search. 13507 */ 13508 cdip = next; 13509 continue; 13510 } 13511 13512 if (tgt == target) 13513 break; 13514 13515 cdip = next; 13516 } 13517 ndi_devi_exit(dip, circ); 13518 13519 return (cdip); 13520 } 13521 13522 /* 13523 * Get dev_info_t pointer to the device node pointed to by port argument. 13524 * NOTE: target argument is a value used in ioctls to identify 13525 * the AP - it is not a sata_address. 13526 * It is a combination of cport, pmport and address qualifier, encoded same 13527 * way as a scsi target number. 13528 * 13529 * Returns dev_info_t pointer if target device was found, NULL otherwise. 13530 */ 13531 13532 static dev_info_t * 13533 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr) 13534 { 13535 dev_info_t *cdip = NULL; 13536 int target, tgt; 13537 int circ; 13538 13539 target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual); 13540 13541 ndi_devi_enter(dip, &circ); 13542 for (cdip = ddi_get_child(dip); cdip != NULL; ) { 13543 dev_info_t *next = ddi_get_next_sibling(cdip); 13544 13545 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip, 13546 DDI_PROP_DONTPASS, "target", -1); 13547 if (tgt == -1) { 13548 /* 13549 * This is actually an error condition, but not 13550 * a fatal one. Just continue the search. 13551 */ 13552 cdip = next; 13553 continue; 13554 } 13555 13556 if (tgt == target) 13557 break; 13558 13559 cdip = next; 13560 } 13561 ndi_devi_exit(dip, circ); 13562 13563 return (cdip); 13564 } 13565 13566 /* 13567 * Process sata port disconnect request. 13568 * Normally, cfgadm sata plugin will try to offline (unconfigure) the device 13569 * before this request. Nevertheless, if a device is still configured, 13570 * we need to attempt to offline and unconfigure device. 13571 * Regardless of the unconfigure operation results the port is marked as 13572 * deactivated and no access to the attached device is possible. 13573 * If the target node remains because unconfigure operation failed, its state 13574 * will be set to DEVICE_REMOVED, preventing it to be used again when a device 13575 * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure 13576 * the device and remove old target node. 13577 * 13578 * This function invokes sata_hba_inst->satahba_tran-> 13579 * sata_tran_hotplug_ops->sata_tran_port_deactivate(). 13580 * If successful, the device structure (if any) attached to the specified port 13581 * is removed and state of the port marked appropriately. 13582 * Failure of the port_deactivate may keep port in the physically active state, 13583 * or may fail the port. 13584 * 13585 * NOTE: Port multiplier is supported. 13586 */ 13587 13588 static int 13589 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst, 13590 sata_device_t *sata_device) 13591 { 13592 sata_drive_info_t *sdinfo = NULL, *subsdinfo = NULL; 13593 sata_cport_info_t *cportinfo = NULL; 13594 sata_pmport_info_t *pmportinfo = NULL; 13595 sata_pmult_info_t *pmultinfo = NULL; 13596 sata_device_t subsdevice; 13597 int cport, pmport, qual; 13598 int rval = SATA_SUCCESS; 13599 int npmport = 0; 13600 int rv = 0; 13601 13602 cport = sata_device->satadev_addr.cport; 13603 pmport = sata_device->satadev_addr.pmport; 13604 qual = sata_device->satadev_addr.qual; 13605 13606 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 13607 if (qual == SATA_ADDR_DCPORT) 13608 qual = SATA_ADDR_CPORT; 13609 else 13610 qual = SATA_ADDR_PMPORT; 13611 13612 /* 13613 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran-> 13614 * sata_tran_hotplug_ops->sata_tran_port_deactivate(). 13615 * Do the sanity check. 13616 */ 13617 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) { 13618 /* No physical port deactivation supported. */ 13619 return (EINVAL); 13620 } 13621 13622 /* Check the current state of the port */ 13623 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 13624 (SATA_DIP(sata_hba_inst), sata_device); 13625 13626 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 13627 13628 /* 13629 * Processing port mulitiplier 13630 */ 13631 if (qual == SATA_ADDR_CPORT && 13632 SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) { 13633 mutex_enter(&cportinfo->cport_mutex); 13634 13635 /* Check controller port status */ 13636 sata_update_port_info(sata_hba_inst, sata_device); 13637 if (rval != SATA_SUCCESS || 13638 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 13639 /* 13640 * Device port status is unknown or it is in failed 13641 * state 13642 */ 13643 SATA_CPORT_STATE(sata_hba_inst, cport) = 13644 SATA_PSTATE_FAILED; 13645 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 13646 "sata_hba_ioctl: connect: failed to deactivate " 13647 "SATA port %d", cport); 13648 mutex_exit(&cportinfo->cport_mutex); 13649 return (EIO); 13650 } 13651 13652 /* Disconnect all sub-devices. */ 13653 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 13654 if (pmultinfo != NULL) { 13655 13656 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 13657 sata_hba_inst, cport); npmport ++) { 13658 subsdinfo = SATA_PMPORT_DRV_INFO( 13659 sata_hba_inst, cport, npmport); 13660 if (subsdinfo == NULL) 13661 continue; 13662 13663 subsdevice.satadev_addr = subsdinfo-> 13664 satadrv_addr; 13665 13666 mutex_exit(&cportinfo->cport_mutex); 13667 if (sata_ioctl_disconnect(sata_hba_inst, 13668 &subsdevice) == SATA_SUCCESS) { 13669 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 13670 "[Remove] device at port %d:%d " 13671 "successfully.", cport, npmport); 13672 } 13673 mutex_enter(&cportinfo->cport_mutex); 13674 } 13675 } 13676 13677 /* Disconnect the port multiplier */ 13678 cportinfo->cport_state &= ~SATA_STATE_READY; 13679 mutex_exit(&cportinfo->cport_mutex); 13680 13681 sata_device->satadev_addr.qual = qual; 13682 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 13683 (SATA_DIP(sata_hba_inst), sata_device); 13684 13685 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 13686 SE_NO_HINT); 13687 13688 mutex_enter(&cportinfo->cport_mutex); 13689 sata_update_port_info(sata_hba_inst, sata_device); 13690 if (rval != SATA_SUCCESS && 13691 sata_device->satadev_state & SATA_PSTATE_FAILED) { 13692 cportinfo->cport_state = SATA_PSTATE_FAILED; 13693 rv = EIO; 13694 } else { 13695 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 13696 } 13697 mutex_exit(&cportinfo->cport_mutex); 13698 13699 return (rv); 13700 } 13701 13702 /* 13703 * Process non-port-multiplier device - it could be a drive connected 13704 * to a port multiplier port or a controller port. 13705 */ 13706 if (qual == SATA_ADDR_PMPORT) { 13707 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 13708 mutex_enter(&pmportinfo->pmport_mutex); 13709 sata_update_pmport_info(sata_hba_inst, sata_device); 13710 if (rval != SATA_SUCCESS || 13711 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 13712 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 13713 SATA_PSTATE_FAILED; 13714 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 13715 "sata_hba_ioctl: connect: failed to deactivate " 13716 "SATA port %d:%d", cport, pmport); 13717 mutex_exit(&pmportinfo->pmport_mutex); 13718 return (EIO); 13719 } 13720 13721 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 13722 sdinfo = pmportinfo->pmport_sata_drive; 13723 ASSERT(sdinfo != NULL); 13724 } 13725 13726 /* 13727 * Set port's dev_state to not ready - this will disable 13728 * an access to a potentially attached device. 13729 */ 13730 pmportinfo->pmport_state &= ~SATA_STATE_READY; 13731 13732 /* Remove and release sata_drive info structure. */ 13733 if (sdinfo != NULL) { 13734 if ((sdinfo->satadrv_type & 13735 SATA_VALID_DEV_TYPE) != 0) { 13736 /* 13737 * If a target node exists, try to offline 13738 * a device and remove target node. 13739 */ 13740 mutex_exit(&pmportinfo->pmport_mutex); 13741 (void) sata_offline_device(sata_hba_inst, 13742 sata_device, sdinfo); 13743 mutex_enter(&pmportinfo->pmport_mutex); 13744 } 13745 13746 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 13747 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 13748 (void) kmem_free((void *)sdinfo, 13749 sizeof (sata_drive_info_t)); 13750 } 13751 mutex_exit(&pmportinfo->pmport_mutex); 13752 13753 } else if (qual == SATA_ADDR_CPORT) { 13754 mutex_enter(&cportinfo->cport_mutex); 13755 sata_update_port_info(sata_hba_inst, sata_device); 13756 if (rval != SATA_SUCCESS || 13757 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 13758 /* 13759 * Device port status is unknown or it is in failed 13760 * state 13761 */ 13762 SATA_CPORT_STATE(sata_hba_inst, cport) = 13763 SATA_PSTATE_FAILED; 13764 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 13765 "sata_hba_ioctl: connect: failed to deactivate " 13766 "SATA port %d", cport); 13767 mutex_exit(&cportinfo->cport_mutex); 13768 return (EIO); 13769 } 13770 13771 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 13772 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 13773 ASSERT(pmultinfo != NULL); 13774 } else if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 13775 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 13776 ASSERT(sdinfo != NULL); 13777 } 13778 cportinfo->cport_state &= ~SATA_STATE_READY; 13779 13780 if (sdinfo != NULL) { 13781 if ((sdinfo->satadrv_type & 13782 SATA_VALID_DEV_TYPE) != 0) { 13783 /* 13784 * If a target node exists, try to offline 13785 * a device and remove target node. 13786 */ 13787 mutex_exit(&cportinfo->cport_mutex); 13788 (void) sata_offline_device(sata_hba_inst, 13789 sata_device, sdinfo); 13790 mutex_enter(&cportinfo->cport_mutex); 13791 } 13792 13793 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 13794 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 13795 (void) kmem_free((void *)sdinfo, 13796 sizeof (sata_drive_info_t)); 13797 } 13798 mutex_exit(&cportinfo->cport_mutex); 13799 } 13800 13801 /* Just ask HBA driver to deactivate port */ 13802 sata_device->satadev_addr.qual = qual; 13803 13804 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 13805 (SATA_DIP(sata_hba_inst), sata_device); 13806 13807 /* 13808 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 13809 * without the hint (to force listener to investivate the state). 13810 */ 13811 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 13812 SE_NO_HINT); 13813 13814 if (qual == SATA_ADDR_PMPORT) { 13815 mutex_enter(&pmportinfo->pmport_mutex); 13816 sata_update_pmport_info(sata_hba_inst, sata_device); 13817 13818 if (rval != SATA_SUCCESS && 13819 sata_device->satadev_state & SATA_PSTATE_FAILED) { 13820 /* 13821 * Port deactivation failure - do not change port 13822 * state unless the state returned by HBA indicates a 13823 * port failure. 13824 * 13825 * NOTE: device structures were released, so devices 13826 * now are invisible! Port reset is needed to 13827 * re-enumerate devices. 13828 */ 13829 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 13830 rv = EIO; 13831 } else { 13832 /* 13833 * Deactivation succeded. From now on the sata framework 13834 * will not care what is happening to the device, until 13835 * the port is activated again. 13836 */ 13837 pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN; 13838 } 13839 mutex_exit(&pmportinfo->pmport_mutex); 13840 } else if (qual == SATA_ADDR_CPORT) { 13841 mutex_enter(&cportinfo->cport_mutex); 13842 sata_update_port_info(sata_hba_inst, sata_device); 13843 13844 if (rval != SATA_SUCCESS && 13845 sata_device->satadev_state & SATA_PSTATE_FAILED) { 13846 cportinfo->cport_state = SATA_PSTATE_FAILED; 13847 rv = EIO; 13848 } else { 13849 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 13850 } 13851 mutex_exit(&cportinfo->cport_mutex); 13852 } 13853 13854 return (rv); 13855 } 13856 13857 13858 13859 /* 13860 * Process sata port connect request 13861 * The sata cfgadm pluging will invoke this operation only if port was found 13862 * in the disconnect state (failed state is also treated as the disconnected 13863 * state). 13864 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->satahba_tran-> 13865 * sata_tran_hotplug_ops->sata_tran_port_activate(). 13866 * If successful and a device is found attached to the port, 13867 * the initialization sequence is executed to attach a device structure to 13868 * a port structure. The state of the port and a device would be set 13869 * appropriately. 13870 * The device is not set in configured state (system-wise) by this operation. 13871 * 13872 * Note, that activating the port may generate link events, 13873 * so it is important that following processing and the 13874 * event processing does not interfere with each other! 13875 * 13876 * This operation may remove port failed state and will 13877 * try to make port active and in good standing. 13878 * 13879 * NOTE: Port multiplier is supported. 13880 */ 13881 13882 static int 13883 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst, 13884 sata_device_t *sata_device) 13885 { 13886 sata_pmport_info_t *pmportinfo = NULL; 13887 uint8_t cport, pmport, qual; 13888 int rv = 0; 13889 13890 cport = sata_device->satadev_addr.cport; 13891 pmport = sata_device->satadev_addr.pmport; 13892 qual = sata_device->satadev_addr.qual; 13893 13894 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 13895 if (qual == SATA_ADDR_DCPORT) 13896 qual = SATA_ADDR_CPORT; 13897 else 13898 qual = SATA_ADDR_PMPORT; 13899 13900 if (qual == SATA_ADDR_PMPORT) 13901 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 13902 13903 /* 13904 * DEVCTL_AP_CONNECT would invoke sata_hba_inst-> 13905 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate(). 13906 * Perform sanity check now. 13907 */ 13908 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) { 13909 /* No physical port activation supported. */ 13910 return (EINVAL); 13911 } 13912 13913 /* Just ask HBA driver to activate port */ 13914 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 13915 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 13916 /* 13917 * Port activation failure. 13918 */ 13919 if (qual == SATA_ADDR_CPORT) { 13920 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 13921 cport)->cport_mutex); 13922 sata_update_port_info(sata_hba_inst, sata_device); 13923 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 13924 SATA_CPORT_STATE(sata_hba_inst, cport) = 13925 SATA_PSTATE_FAILED; 13926 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 13927 "sata_hba_ioctl: connect: failed to " 13928 "activate SATA port %d", cport); 13929 } 13930 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 13931 cport)->cport_mutex); 13932 } else { /* port multiplier device port */ 13933 mutex_enter(&pmportinfo->pmport_mutex); 13934 sata_update_pmport_info(sata_hba_inst, sata_device); 13935 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 13936 SATA_PMPORT_STATE(sata_hba_inst, cport, 13937 pmport) = SATA_PSTATE_FAILED; 13938 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 13939 "sata_hba_ioctl: connect: failed to " 13940 "activate SATA port %d:%d", cport, pmport); 13941 } 13942 mutex_exit(&pmportinfo->pmport_mutex); 13943 } 13944 return (EIO); 13945 } 13946 13947 /* Virgin port state - will be updated by the port re-probe. */ 13948 if (qual == SATA_ADDR_CPORT) { 13949 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 13950 cport)->cport_mutex); 13951 SATA_CPORT_STATE(sata_hba_inst, cport) = 0; 13952 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 13953 cport)->cport_mutex); 13954 } else { /* port multiplier device port */ 13955 mutex_enter(&pmportinfo->pmport_mutex); 13956 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0; 13957 mutex_exit(&pmportinfo->pmport_mutex); 13958 } 13959 13960 /* 13961 * Probe the port to find its state and attached device. 13962 */ 13963 if (sata_reprobe_port(sata_hba_inst, sata_device, 13964 SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE) 13965 rv = EIO; 13966 13967 /* 13968 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 13969 * without the hint 13970 */ 13971 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 13972 SE_NO_HINT); 13973 13974 /* 13975 * If there is a device attached to the port, emit 13976 * a message. 13977 */ 13978 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 13979 13980 if (qual == SATA_ADDR_CPORT) { 13981 if (sata_device->satadev_type == SATA_DTYPE_PMULT) { 13982 sata_log(sata_hba_inst, CE_WARN, 13983 "SATA port multiplier detected " 13984 "at port %d", cport); 13985 } else { 13986 sata_log(sata_hba_inst, CE_WARN, 13987 "SATA device detected at port %d", cport); 13988 if (sata_device->satadev_type == 13989 SATA_DTYPE_UNKNOWN) { 13990 /* 13991 * A device was not successfully identified 13992 */ 13993 sata_log(sata_hba_inst, CE_WARN, 13994 "Could not identify SATA " 13995 "device at port %d", cport); 13996 } 13997 } 13998 } else { /* port multiplier device port */ 13999 sata_log(sata_hba_inst, CE_WARN, 14000 "SATA device detected at port %d:%d", 14001 cport, pmport); 14002 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 14003 /* 14004 * A device was not successfully identified 14005 */ 14006 sata_log(sata_hba_inst, CE_WARN, 14007 "Could not identify SATA " 14008 "device at port %d:%d", cport, pmport); 14009 } 14010 } 14011 } 14012 14013 return (rv); 14014 } 14015 14016 14017 /* 14018 * Process sata device unconfigure request. 14019 * The unconfigure operation uses generic nexus operation to 14020 * offline a device. It leaves a target device node attached. 14021 * and obviously sata_drive_info attached as well, because 14022 * from the hardware point of view nothing has changed. 14023 */ 14024 static int 14025 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst, 14026 sata_device_t *sata_device) 14027 { 14028 int rv = 0; 14029 dev_info_t *tdip; 14030 14031 /* We are addressing attached device, not a port */ 14032 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 14033 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 14034 else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) 14035 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 14036 14037 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 14038 &sata_device->satadev_addr)) != NULL) { 14039 14040 if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) { 14041 SATA_LOG_D((sata_hba_inst, CE_WARN, 14042 "sata_hba_ioctl: unconfigure: " 14043 "failed to unconfigure device at SATA port %d:%d", 14044 sata_device->satadev_addr.cport, 14045 sata_device->satadev_addr.pmport)); 14046 rv = EIO; 14047 } 14048 /* 14049 * The target node devi_state should be marked with 14050 * DEVI_DEVICE_OFFLINE by ndi_devi_offline(). 14051 * This would be the indication for cfgadm that 14052 * the AP node occupant state is 'unconfigured'. 14053 */ 14054 14055 } else { 14056 /* 14057 * This would indicate a failure on the part of cfgadm 14058 * to detect correct state of the node prior to this 14059 * call - one cannot unconfigure non-existing device. 14060 */ 14061 SATA_LOG_D((sata_hba_inst, CE_WARN, 14062 "sata_hba_ioctl: unconfigure: " 14063 "attempt to unconfigure non-existing device " 14064 "at SATA port %d:%d", 14065 sata_device->satadev_addr.cport, 14066 sata_device->satadev_addr.pmport)); 14067 rv = ENXIO; 14068 } 14069 return (rv); 14070 } 14071 14072 /* 14073 * Process sata device configure request 14074 * If port is in a failed state, operation is aborted - one has to use 14075 * an explicit connect or port activate request to try to get a port into 14076 * non-failed mode. Port reset wil also work in such situation. 14077 * If the port is in disconnected (shutdown) state, the connect operation is 14078 * attempted prior to any other action. 14079 * When port is in the active state, there is a device attached and the target 14080 * node exists, a device was most likely offlined. 14081 * If target node does not exist, a new target node is created. In both cases 14082 * an attempt is made to online (configure) the device. 14083 * 14084 * NOTE: Port multiplier is supported. 14085 */ 14086 static int 14087 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst, 14088 sata_device_t *sata_device) 14089 { 14090 int cport, pmport, qual; 14091 int rval; 14092 boolean_t target = TRUE; 14093 sata_cport_info_t *cportinfo; 14094 sata_pmport_info_t *pmportinfo = NULL; 14095 dev_info_t *tdip; 14096 sata_drive_info_t *sdinfo; 14097 14098 cport = sata_device->satadev_addr.cport; 14099 pmport = sata_device->satadev_addr.pmport; 14100 qual = sata_device->satadev_addr.qual; 14101 14102 /* Get current port state */ 14103 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 14104 (SATA_DIP(sata_hba_inst), sata_device); 14105 14106 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 14107 if (qual == SATA_ADDR_DPMPORT) { 14108 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 14109 mutex_enter(&pmportinfo->pmport_mutex); 14110 sata_update_pmport_info(sata_hba_inst, sata_device); 14111 if (rval != SATA_SUCCESS || 14112 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 14113 /* 14114 * Obviously, device on a failed port is not visible 14115 */ 14116 mutex_exit(&pmportinfo->pmport_mutex); 14117 return (ENXIO); 14118 } 14119 mutex_exit(&pmportinfo->pmport_mutex); 14120 } else { 14121 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14122 cport)->cport_mutex); 14123 sata_update_port_info(sata_hba_inst, sata_device); 14124 if (rval != SATA_SUCCESS || 14125 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 14126 /* 14127 * Obviously, device on a failed port is not visible 14128 */ 14129 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14130 cport)->cport_mutex); 14131 return (ENXIO); 14132 } 14133 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14134 cport)->cport_mutex); 14135 } 14136 14137 if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) { 14138 /* need to activate port */ 14139 target = FALSE; 14140 14141 /* Sanity check */ 14142 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) 14143 return (ENXIO); 14144 14145 /* Just let HBA driver to activate port */ 14146 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 14147 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 14148 /* 14149 * Port activation failure - do not change port state 14150 * unless the state returned by HBA indicates a port 14151 * failure. 14152 */ 14153 if (qual == SATA_ADDR_DPMPORT) { 14154 mutex_enter(&pmportinfo->pmport_mutex); 14155 sata_update_pmport_info(sata_hba_inst, 14156 sata_device); 14157 if (sata_device->satadev_state & 14158 SATA_PSTATE_FAILED) 14159 pmportinfo->pmport_state = 14160 SATA_PSTATE_FAILED; 14161 mutex_exit(&pmportinfo->pmport_mutex); 14162 } else { 14163 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14164 cport)->cport_mutex); 14165 sata_update_port_info(sata_hba_inst, 14166 sata_device); 14167 if (sata_device->satadev_state & 14168 SATA_PSTATE_FAILED) 14169 cportinfo->cport_state = 14170 SATA_PSTATE_FAILED; 14171 mutex_exit(&SATA_CPORT_INFO( 14172 sata_hba_inst, cport)->cport_mutex); 14173 } 14174 } 14175 SATA_LOG_D((sata_hba_inst, CE_WARN, 14176 "sata_hba_ioctl: configure: " 14177 "failed to activate SATA port %d:%d", 14178 cport, pmport)); 14179 return (EIO); 14180 } 14181 /* 14182 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 14183 * without the hint. 14184 */ 14185 sata_gen_sysevent(sata_hba_inst, 14186 &sata_device->satadev_addr, SE_NO_HINT); 14187 14188 /* Virgin port state */ 14189 if (qual == SATA_ADDR_DPMPORT) { 14190 mutex_enter(&pmportinfo->pmport_mutex); 14191 pmportinfo->pmport_state = 0; 14192 mutex_exit(&pmportinfo->pmport_mutex); 14193 } else { 14194 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14195 cport)-> cport_mutex); 14196 cportinfo->cport_state = 0; 14197 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14198 cport)->cport_mutex); 14199 } 14200 /* 14201 * Always reprobe port, to get current device info. 14202 */ 14203 if (sata_reprobe_port(sata_hba_inst, sata_device, 14204 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 14205 return (EIO); 14206 14207 if (sata_device->satadev_type != SATA_DTYPE_NONE && target == FALSE) { 14208 if (qual == SATA_ADDR_DPMPORT) { 14209 /* 14210 * That's the transition from "inactive" port 14211 * to active one with device attached. 14212 */ 14213 sata_log(sata_hba_inst, CE_WARN, 14214 "SATA device detected at port %d:%d", 14215 cport, pmport); 14216 } else { 14217 /* 14218 * When PM is attached to the cport and cport is 14219 * activated, every PM device port needs to be reprobed. 14220 * We need to emit message for all devices detected 14221 * at port multiplier's device ports. 14222 * Add such code here. 14223 * For now, just inform about device attached to 14224 * cport. 14225 */ 14226 sata_log(sata_hba_inst, CE_WARN, 14227 "SATA device detected at port %d", cport); 14228 } 14229 } 14230 14231 /* 14232 * This is where real configuration operation starts. 14233 * 14234 * When PM is attached to the cport and cport is activated, 14235 * devices attached PM device ports may have to be configured 14236 * explicitly. This may change when port multiplier is supported. 14237 * For now, configure only disks and other valid target devices. 14238 */ 14239 if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) { 14240 if (qual == SATA_ADDR_DCPORT) { 14241 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 14242 /* 14243 * A device was not successfully identified 14244 */ 14245 sata_log(sata_hba_inst, CE_WARN, 14246 "Could not identify SATA " 14247 "device at port %d", cport); 14248 } 14249 } else { /* port multiplier device port */ 14250 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 14251 /* 14252 * A device was not successfully identified 14253 */ 14254 sata_log(sata_hba_inst, CE_WARN, 14255 "Could not identify SATA " 14256 "device at port %d:%d", cport, pmport); 14257 } 14258 } 14259 return (ENXIO); /* No device to configure */ 14260 } 14261 14262 /* 14263 * Here we may have a device in reset condition, 14264 * but because we are just configuring it, there is 14265 * no need to process the reset other than just 14266 * to clear device reset condition in the HBA driver. 14267 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will 14268 * cause a first command sent the HBA driver with the request 14269 * to clear device reset condition. 14270 */ 14271 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14272 if (qual == SATA_ADDR_DPMPORT) 14273 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 14274 else 14275 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 14276 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 14277 if (sdinfo == NULL) { 14278 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14279 return (ENXIO); 14280 } 14281 if (sdinfo->satadrv_event_flags & 14282 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 14283 sdinfo->satadrv_event_flags = 0; 14284 } 14285 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 14286 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14287 14288 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 14289 &sata_device->satadev_addr)) != NULL) { 14290 /* 14291 * Target node exists. Verify, that it belongs 14292 * to existing, attached device and not to 14293 * a removed device. 14294 */ 14295 if (sata_check_device_removed(tdip) == B_TRUE) { 14296 if (qual == SATA_ADDR_DPMPORT) 14297 sata_log(sata_hba_inst, CE_WARN, 14298 "SATA device at port %d cannot be " 14299 "configured. " 14300 "Application(s) accessing " 14301 "previously attached device " 14302 "have to release it before newly " 14303 "inserted device can be made accessible.", 14304 cport); 14305 else 14306 sata_log(sata_hba_inst, CE_WARN, 14307 "SATA device at port %d:%d cannot be" 14308 "configured. " 14309 "Application(s) accessing " 14310 "previously attached device " 14311 "have to release it before newly " 14312 "inserted device can be made accessible.", 14313 cport, pmport); 14314 return (EIO); 14315 } 14316 /* 14317 * Device was not removed and re-inserted. 14318 * Try to online it. 14319 */ 14320 if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) { 14321 SATA_LOG_D((sata_hba_inst, CE_WARN, 14322 "sata_hba_ioctl: configure: " 14323 "onlining device at SATA port " 14324 "%d:%d failed", cport, pmport)); 14325 return (EIO); 14326 } 14327 14328 if (qual == SATA_ADDR_DPMPORT) { 14329 mutex_enter(&pmportinfo->pmport_mutex); 14330 pmportinfo->pmport_tgtnode_clean = B_TRUE; 14331 mutex_exit(&pmportinfo->pmport_mutex); 14332 } else { 14333 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14334 cport)->cport_mutex); 14335 cportinfo-> cport_tgtnode_clean = B_TRUE; 14336 mutex_exit(&SATA_CPORT_INFO( 14337 sata_hba_inst, cport)->cport_mutex); 14338 } 14339 } else { 14340 /* 14341 * No target node - need to create a new target node. 14342 */ 14343 if (qual == SATA_ADDR_DPMPORT) { 14344 mutex_enter(&pmportinfo->pmport_mutex); 14345 pmportinfo->pmport_tgtnode_clean = B_TRUE; 14346 mutex_exit(&pmportinfo->pmport_mutex); 14347 } else { 14348 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 14349 cport_mutex); 14350 cportinfo-> cport_tgtnode_clean = B_TRUE; 14351 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 14352 cport_mutex); 14353 } 14354 14355 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst), 14356 sata_hba_inst, &sata_device->satadev_addr); 14357 if (tdip == NULL) { 14358 /* Configure operation failed */ 14359 SATA_LOG_D((sata_hba_inst, CE_WARN, 14360 "sata_hba_ioctl: configure: " 14361 "configuring SATA device at port %d:%d " 14362 "failed", cport, pmport)); 14363 return (EIO); 14364 } 14365 } 14366 return (0); 14367 } 14368 14369 14370 /* 14371 * Process ioctl deactivate port request. 14372 * Arbitrarily unconfigure attached device, if any. 14373 * Even if the unconfigure fails, proceed with the 14374 * port deactivation. 14375 * 14376 * NOTE: Port Multiplier is supported now. 14377 */ 14378 14379 static int 14380 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst, 14381 sata_device_t *sata_device) 14382 { 14383 int cport, pmport, qual; 14384 int rval, rv = 0; 14385 int npmport; 14386 sata_cport_info_t *cportinfo; 14387 sata_pmport_info_t *pmportinfo; 14388 sata_pmult_info_t *pmultinfo; 14389 dev_info_t *tdip; 14390 sata_drive_info_t *sdinfo = NULL; 14391 sata_device_t subsdevice; 14392 14393 /* Sanity check */ 14394 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) 14395 return (ENOTSUP); 14396 14397 cport = sata_device->satadev_addr.cport; 14398 pmport = sata_device->satadev_addr.pmport; 14399 qual = sata_device->satadev_addr.qual; 14400 14401 /* SCSI_TO_SATA_ADDR_QUAL() translate ap_id into a device qualifier */ 14402 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 14403 if (qual == SATA_ADDR_DCPORT) 14404 qual = SATA_ADDR_CPORT; 14405 else 14406 qual = SATA_ADDR_PMPORT; 14407 14408 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 14409 if (qual == SATA_ADDR_PMPORT) 14410 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 14411 14412 /* 14413 * Processing port multiplier 14414 */ 14415 if (qual == SATA_ADDR_CPORT && 14416 SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) { 14417 mutex_enter(&cportinfo->cport_mutex); 14418 14419 /* Deactivate all sub-deices */ 14420 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 14421 if (pmultinfo != NULL) { 14422 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 14423 sata_hba_inst, cport); npmport++) { 14424 14425 subsdevice.satadev_addr.cport = cport; 14426 subsdevice.satadev_addr.pmport = 14427 (uint8_t)npmport; 14428 subsdevice.satadev_addr.qual = 14429 SATA_ADDR_DPMPORT; 14430 14431 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 14432 "sata_hba_ioctl: deactivate: trying to " 14433 "deactivate SATA port %d:%d", 14434 cport, npmport); 14435 14436 mutex_exit(&cportinfo->cport_mutex); 14437 if (sata_ioctl_deactivate(sata_hba_inst, 14438 &subsdevice) == SATA_SUCCESS) { 14439 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 14440 "[Deactivate] device at port %d:%d " 14441 "successfully.", cport, npmport); 14442 } 14443 mutex_enter(&cportinfo->cport_mutex); 14444 } 14445 } 14446 14447 /* Deactivate the port multiplier now. */ 14448 cportinfo->cport_state &= ~SATA_STATE_READY; 14449 mutex_exit(&cportinfo->cport_mutex); 14450 14451 sata_device->satadev_addr.qual = qual; 14452 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 14453 (SATA_DIP(sata_hba_inst), sata_device); 14454 14455 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 14456 SE_NO_HINT); 14457 14458 mutex_enter(&cportinfo->cport_mutex); 14459 sata_update_port_info(sata_hba_inst, sata_device); 14460 if (rval != SATA_SUCCESS) { 14461 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 14462 cportinfo->cport_state = SATA_PSTATE_FAILED; 14463 } 14464 rv = EIO; 14465 } else { 14466 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 14467 } 14468 mutex_exit(&cportinfo->cport_mutex); 14469 14470 return (rv); 14471 } 14472 14473 /* 14474 * Process non-port-multiplier device - it could be a drive connected 14475 * to a port multiplier port or a controller port. 14476 */ 14477 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14478 if (qual == SATA_ADDR_CPORT) { 14479 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 14480 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 14481 /* deal only with valid devices */ 14482 if ((cportinfo->cport_dev_type & 14483 SATA_VALID_DEV_TYPE) != 0) 14484 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 14485 } 14486 cportinfo->cport_state &= ~SATA_STATE_READY; 14487 } else { 14488 /* Port multiplier device port */ 14489 mutex_enter(&pmportinfo->pmport_mutex); 14490 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 14491 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE && 14492 (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0) 14493 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 14494 pmportinfo->pmport_state &= ~SATA_STATE_READY; 14495 mutex_exit(&pmportinfo->pmport_mutex); 14496 } 14497 14498 if (sdinfo != NULL) { 14499 /* 14500 * If a target node exists, try to offline a device and 14501 * to remove a target node. 14502 */ 14503 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 14504 cport_mutex); 14505 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 14506 &sata_device->satadev_addr); 14507 if (tdip != NULL) { 14508 /* target node exist */ 14509 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 14510 "sata_hba_ioctl: port deactivate: " 14511 "target node exists.", NULL); 14512 14513 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != 14514 NDI_SUCCESS) { 14515 SATA_LOG_D((sata_hba_inst, CE_WARN, 14516 "sata_hba_ioctl: port deactivate: " 14517 "failed to unconfigure device at port " 14518 "%d:%d before deactivating the port", 14519 cport, pmport)); 14520 /* 14521 * Set DEVICE REMOVED state in the target 14522 * node. It will prevent an access to 14523 * the device even when a new device is 14524 * attached, until the old target node is 14525 * released, removed and recreated for a new 14526 * device. 14527 */ 14528 sata_set_device_removed(tdip); 14529 14530 /* 14531 * Instruct the event daemon to try the 14532 * target node cleanup later. 14533 */ 14534 sata_set_target_node_cleanup(sata_hba_inst, 14535 &sata_device->satadev_addr); 14536 } 14537 } 14538 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 14539 cport_mutex); 14540 /* 14541 * In any case, remove and release sata_drive_info 14542 * structure. 14543 */ 14544 if (qual == SATA_ADDR_CPORT) { 14545 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 14546 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 14547 } else { /* port multiplier device port */ 14548 mutex_enter(&pmportinfo->pmport_mutex); 14549 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 14550 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 14551 mutex_exit(&pmportinfo->pmport_mutex); 14552 } 14553 (void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t)); 14554 } 14555 14556 if (qual == SATA_ADDR_CPORT) { 14557 cportinfo->cport_state &= ~(SATA_STATE_PROBED | 14558 SATA_STATE_PROBING); 14559 } else if (qual == SATA_ADDR_PMPORT) { 14560 mutex_enter(&pmportinfo->pmport_mutex); 14561 pmportinfo->pmport_state &= ~(SATA_STATE_PROBED | 14562 SATA_STATE_PROBING); 14563 mutex_exit(&pmportinfo->pmport_mutex); 14564 } 14565 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14566 14567 /* Just let HBA driver to deactivate port */ 14568 sata_device->satadev_addr.qual = qual; 14569 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 14570 (SATA_DIP(sata_hba_inst), sata_device); 14571 14572 /* 14573 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 14574 * without the hint 14575 */ 14576 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 14577 SE_NO_HINT); 14578 14579 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14580 sata_update_port_info(sata_hba_inst, sata_device); 14581 if (qual == SATA_ADDR_CPORT) { 14582 if (rval != SATA_SUCCESS) { 14583 /* 14584 * Port deactivation failure - do not change port state 14585 * unless the state returned by HBA indicates a port 14586 * failure. 14587 */ 14588 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 14589 SATA_CPORT_STATE(sata_hba_inst, cport) = 14590 SATA_PSTATE_FAILED; 14591 } 14592 SATA_LOG_D((sata_hba_inst, CE_WARN, 14593 "sata_hba_ioctl: port deactivate: " 14594 "cannot deactivate SATA port %d", cport)); 14595 rv = EIO; 14596 } else { 14597 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 14598 } 14599 } else { 14600 mutex_enter(&pmportinfo->pmport_mutex); 14601 if (rval != SATA_SUCCESS) { 14602 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 14603 SATA_PMPORT_STATE(sata_hba_inst, cport, 14604 pmport) = SATA_PSTATE_FAILED; 14605 } 14606 SATA_LOG_D((sata_hba_inst, CE_WARN, 14607 "sata_hba_ioctl: port deactivate: " 14608 "cannot deactivate SATA port %d:%d", 14609 cport, pmport)); 14610 rv = EIO; 14611 } else { 14612 pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN; 14613 } 14614 mutex_exit(&pmportinfo->pmport_mutex); 14615 } 14616 14617 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14618 14619 return (rv); 14620 } 14621 14622 /* 14623 * Process ioctl port activate request. 14624 * 14625 * NOTE: Port multiplier is supported now. 14626 */ 14627 static int 14628 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst, 14629 sata_device_t *sata_device) 14630 { 14631 int cport, pmport, qual; 14632 sata_cport_info_t *cportinfo; 14633 sata_pmport_info_t *pmportinfo = NULL; 14634 boolean_t dev_existed = TRUE; 14635 14636 /* Sanity check */ 14637 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) 14638 return (ENOTSUP); 14639 14640 cport = sata_device->satadev_addr.cport; 14641 pmport = sata_device->satadev_addr.pmport; 14642 qual = sata_device->satadev_addr.qual; 14643 14644 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 14645 14646 /* 14647 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL()) 14648 * is a device. But what we are dealing with is port/pmport. 14649 */ 14650 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 14651 if (qual == SATA_ADDR_DCPORT) 14652 sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT; 14653 else 14654 sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT; 14655 14656 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14657 if (qual == SATA_ADDR_PMPORT) { 14658 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 14659 if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN || 14660 pmportinfo->pmport_dev_type == SATA_DTYPE_NONE) 14661 dev_existed = FALSE; 14662 } else { /* cport */ 14663 if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN || 14664 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 14665 dev_existed = FALSE; 14666 } 14667 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14668 14669 /* Just let HBA driver to activate port, if necessary */ 14670 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 14671 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 14672 /* 14673 * Port activation failure - do not change port state unless 14674 * the state returned by HBA indicates a port failure. 14675 */ 14676 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14677 cport)->cport_mutex); 14678 sata_update_port_info(sata_hba_inst, sata_device); 14679 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 14680 if (qual == SATA_ADDR_PMPORT) { 14681 mutex_enter(&pmportinfo->pmport_mutex); 14682 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 14683 mutex_exit(&pmportinfo->pmport_mutex); 14684 } else 14685 cportinfo->cport_state = SATA_PSTATE_FAILED; 14686 14687 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14688 cport)->cport_mutex); 14689 SATA_LOG_D((sata_hba_inst, CE_WARN, 14690 "sata_hba_ioctl: port activate: cannot activate " 14691 "SATA port %d:%d", cport, pmport)); 14692 return (EIO); 14693 } 14694 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14695 } 14696 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14697 if (qual == SATA_ADDR_PMPORT) { 14698 mutex_enter(&pmportinfo->pmport_mutex); 14699 pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN; 14700 mutex_exit(&pmportinfo->pmport_mutex); 14701 } else 14702 cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN; 14703 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14704 14705 /* 14706 * Re-probe port to find its current state and possibly attached device. 14707 * Port re-probing may change the cportinfo device type if device is 14708 * found attached. 14709 * If port probing failed, the device type would be set to 14710 * SATA_DTYPE_NONE. 14711 */ 14712 (void) sata_reprobe_port(sata_hba_inst, sata_device, 14713 SATA_DEV_IDENTIFY_RETRY); 14714 14715 /* 14716 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 14717 * without the hint. 14718 */ 14719 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 14720 SE_NO_HINT); 14721 14722 if (dev_existed == FALSE) { 14723 if (qual == SATA_ADDR_PMPORT && 14724 pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 14725 /* 14726 * That's the transition from the "inactive" port state 14727 * or the active port without a device attached to the 14728 * active port state with a device attached. 14729 */ 14730 sata_log(sata_hba_inst, CE_WARN, 14731 "SATA device detected at port %d:%d", 14732 cport, pmport); 14733 } else if (qual == SATA_ADDR_CPORT && 14734 cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 14735 /* 14736 * That's the transition from the "inactive" port state 14737 * or the active port without a device attached to the 14738 * active port state with a device attached. 14739 */ 14740 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 14741 sata_log(sata_hba_inst, CE_WARN, 14742 "SATA device detected at port %d", cport); 14743 } else { 14744 sata_log(sata_hba_inst, CE_WARN, 14745 "SATA port multiplier detected at port %d", 14746 cport); 14747 } 14748 } 14749 } 14750 return (0); 14751 } 14752 14753 14754 14755 /* 14756 * Process ioctl reset port request. 14757 * 14758 * NOTE: Port-Multiplier is supported. 14759 */ 14760 static int 14761 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst, 14762 sata_device_t *sata_device) 14763 { 14764 int cport, pmport, qual; 14765 int rv = 0; 14766 14767 cport = sata_device->satadev_addr.cport; 14768 pmport = sata_device->satadev_addr.pmport; 14769 qual = sata_device->satadev_addr.qual; 14770 14771 /* 14772 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL()) 14773 * is a device. But what we are dealing with is port/pmport. 14774 */ 14775 if (qual == SATA_ADDR_DCPORT) 14776 sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT; 14777 else 14778 sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT; 14779 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT); 14780 14781 /* Sanity check */ 14782 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 14783 SATA_LOG_D((sata_hba_inst, CE_WARN, 14784 "sata_hba_ioctl: sata_hba_tran missing required " 14785 "function sata_tran_reset_dport")); 14786 return (ENOTSUP); 14787 } 14788 14789 /* Ask HBA to reset port */ 14790 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 14791 sata_device) != SATA_SUCCESS) { 14792 SATA_LOG_D((sata_hba_inst, CE_WARN, 14793 "sata_hba_ioctl: reset port: failed %d:%d", 14794 cport, pmport)); 14795 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 14796 cport_mutex); 14797 sata_update_port_info(sata_hba_inst, sata_device); 14798 if (qual == SATA_ADDR_CPORT) 14799 SATA_CPORT_STATE(sata_hba_inst, cport) = 14800 SATA_PSTATE_FAILED; 14801 else { 14802 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, 14803 pmport)); 14804 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 14805 SATA_PSTATE_FAILED; 14806 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, 14807 pmport)); 14808 } 14809 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 14810 cport_mutex); 14811 rv = EIO; 14812 } 14813 /* 14814 * Beacuse the port was reset, it should be probed and 14815 * attached device reinitialized. At this point the 14816 * port state is unknown - it's state is HBA-specific. 14817 * Re-probe port to get its state. 14818 */ 14819 if (sata_reprobe_port(sata_hba_inst, sata_device, 14820 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) { 14821 rv = EIO; 14822 } 14823 return (rv); 14824 } 14825 14826 /* 14827 * Process ioctl reset device request. 14828 * 14829 * NOTE: Port multiplier is supported. 14830 */ 14831 static int 14832 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst, 14833 sata_device_t *sata_device) 14834 { 14835 sata_drive_info_t *sdinfo = NULL; 14836 sata_pmult_info_t *pmultinfo = NULL; 14837 int cport, pmport; 14838 int rv = 0; 14839 14840 /* Sanity check */ 14841 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 14842 SATA_LOG_D((sata_hba_inst, CE_WARN, 14843 "sata_hba_ioctl: sata_hba_tran missing required " 14844 "function sata_tran_reset_dport")); 14845 return (ENOTSUP); 14846 } 14847 14848 cport = sata_device->satadev_addr.cport; 14849 pmport = sata_device->satadev_addr.pmport; 14850 14851 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14852 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) { 14853 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == 14854 SATA_DTYPE_PMULT) 14855 pmultinfo = SATA_CPORT_INFO(sata_hba_inst, cport)-> 14856 cport_devp.cport_sata_pmult; 14857 else 14858 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 14859 sata_device->satadev_addr.cport); 14860 } else { /* port multiplier */ 14861 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 14862 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 14863 sata_device->satadev_addr.cport, 14864 sata_device->satadev_addr.pmport); 14865 } 14866 if (sdinfo == NULL && pmultinfo == NULL) { 14867 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14868 return (EINVAL); 14869 } 14870 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14871 14872 /* Ask HBA to reset device */ 14873 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 14874 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 14875 SATA_LOG_D((sata_hba_inst, CE_WARN, 14876 "sata_hba_ioctl: reset device: failed at port %d:%d", 14877 cport, pmport)); 14878 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 14879 cport_mutex); 14880 sata_update_port_info(sata_hba_inst, sata_device); 14881 /* 14882 * Device info structure remains attached. Another device reset 14883 * or port disconnect/connect and re-probing is 14884 * needed to change it's state 14885 */ 14886 if (sdinfo != NULL) { 14887 sdinfo->satadrv_state &= ~SATA_STATE_READY; 14888 sdinfo->satadrv_state |= SATA_DSTATE_FAILED; 14889 } else if (pmultinfo != NULL) { 14890 pmultinfo->pmult_state &= ~SATA_STATE_READY; 14891 pmultinfo->pmult_state |= SATA_DSTATE_FAILED; 14892 } 14893 14894 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14895 rv = EIO; 14896 } 14897 /* 14898 * If attached device was a port multiplier, some extra processing 14899 * may be needed to bring it back. SATA specification requies a 14900 * mandatory software reset on host port to reliably enumerate a port 14901 * multiplier, the HBA driver should handle that after reset 14902 * operation. 14903 */ 14904 return (rv); 14905 } 14906 14907 14908 /* 14909 * Process ioctl reset all request. 14910 */ 14911 static int 14912 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst) 14913 { 14914 sata_device_t sata_device; 14915 int rv = 0; 14916 int tcport; 14917 int tpmport = 0; 14918 14919 sata_device.satadev_rev = SATA_DEVICE_REV; 14920 14921 /* 14922 * There is no protection here for configured devices. 14923 */ 14924 /* Sanity check */ 14925 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 14926 SATA_LOG_D((sata_hba_inst, CE_WARN, 14927 "sata_hba_ioctl: sata_hba_tran missing required " 14928 "function sata_tran_reset_dport")); 14929 return (ENOTSUP); 14930 } 14931 14932 /* 14933 * Need to lock all ports, not just one. 14934 * If any port is locked by event processing, fail the whole operation. 14935 * One port is already locked, but for simplicity lock it again. 14936 */ 14937 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) { 14938 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14939 cport_mutex); 14940 if (((SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14941 cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) { 14942 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14943 cport_mutex); 14944 rv = EBUSY; 14945 break; 14946 } else { 14947 /* 14948 * It is enough to lock cport in command-based 14949 * switching mode. 14950 */ 14951 SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14952 cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 14953 } 14954 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14955 cport_mutex); 14956 } 14957 14958 if (rv == 0) { 14959 /* 14960 * All cports were successfully locked. 14961 * Reset main SATA controller. 14962 * Set the device address to port 0, to have a valid device 14963 * address. 14964 */ 14965 sata_device.satadev_addr.qual = SATA_ADDR_CNTRL; 14966 sata_device.satadev_addr.cport = 0; 14967 sata_device.satadev_addr.pmport = 0; 14968 14969 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 14970 (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) { 14971 SATA_LOG_D((sata_hba_inst, CE_WARN, 14972 "sata_hba_ioctl: reset controller failed")); 14973 return (EIO); 14974 } 14975 /* 14976 * Because ports were reset, port states are unknown. 14977 * They should be re-probed to get their state and 14978 * attached devices should be reinitialized. 14979 */ 14980 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); 14981 tcport++) { 14982 sata_device.satadev_addr.cport = tcport; 14983 sata_device.satadev_addr.pmport = tpmport; 14984 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 14985 14986 /* 14987 * The sata_reprobe_port() will mark a 14988 * SATA_EVNT_DEVICE_RESET event on the port 14989 * multiplier, all its sub-ports will be probed by 14990 * sata daemon afterwards. 14991 */ 14992 if (sata_reprobe_port(sata_hba_inst, &sata_device, 14993 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 14994 rv = EIO; 14995 } 14996 } 14997 /* 14998 * Unlock all ports 14999 */ 15000 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) { 15001 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15002 cport_mutex); 15003 SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15004 cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY; 15005 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15006 cport_mutex); 15007 } 15008 15009 /* 15010 * This operation returns EFAULT if either reset 15011 * controller failed or a re-probing of any port failed. 15012 */ 15013 return (rv); 15014 } 15015 15016 15017 /* 15018 * Process ioctl port self test request. 15019 * 15020 * NOTE: Port multiplier code is not completed nor tested. 15021 */ 15022 static int 15023 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst, 15024 sata_device_t *sata_device) 15025 { 15026 int cport, pmport, qual; 15027 int rv = 0; 15028 15029 /* Sanity check */ 15030 if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL) 15031 return (ENOTSUP); 15032 15033 cport = sata_device->satadev_addr.cport; 15034 pmport = sata_device->satadev_addr.pmport; 15035 qual = sata_device->satadev_addr.qual; 15036 15037 /* 15038 * There is no protection here for a configured 15039 * device attached to this port. 15040 */ 15041 15042 if ((*SATA_SELFTEST_FUNC(sata_hba_inst)) 15043 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 15044 SATA_LOG_D((sata_hba_inst, CE_WARN, 15045 "sata_hba_ioctl: port selftest: " 15046 "failed port %d:%d", cport, pmport)); 15047 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15048 cport_mutex); 15049 sata_update_port_info(sata_hba_inst, sata_device); 15050 if (qual == SATA_ADDR_CPORT) 15051 SATA_CPORT_STATE(sata_hba_inst, cport) = 15052 SATA_PSTATE_FAILED; 15053 else { /* port multiplier device port */ 15054 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, 15055 cport, pmport)); 15056 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 15057 SATA_PSTATE_FAILED; 15058 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, 15059 cport, pmport)); 15060 } 15061 15062 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15063 cport_mutex); 15064 return (EIO); 15065 } 15066 /* 15067 * Beacuse the port was reset in the course of testing, it should be 15068 * re-probed and attached device state should be restored. At this 15069 * point the port state is unknown - it's state is HBA-specific. 15070 * Force port re-probing to get it into a known state. 15071 */ 15072 if (sata_reprobe_port(sata_hba_inst, sata_device, 15073 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 15074 rv = EIO; 15075 return (rv); 15076 } 15077 15078 15079 /* 15080 * sata_cfgadm_state: 15081 * Use the sata port state and state of the target node to figure out 15082 * the cfgadm_state. 15083 * 15084 * The port argument is a value with encoded cport, 15085 * pmport and address qualifier, in the same manner as a scsi target number. 15086 * SCSI_TO_SATA_CPORT macro extracts cport number, 15087 * SCSI_TO_SATA_PMPORT extracts pmport number and 15088 * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag. 15089 * 15090 * Port multiplier is supported. 15091 */ 15092 15093 static void 15094 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port, 15095 devctl_ap_state_t *ap_state) 15096 { 15097 uint8_t cport, pmport, qual; 15098 uint32_t port_state, pmult_state; 15099 uint32_t dev_type; 15100 sata_drive_info_t *sdinfo; 15101 15102 cport = SCSI_TO_SATA_CPORT(port); 15103 pmport = SCSI_TO_SATA_PMPORT(port); 15104 qual = SCSI_TO_SATA_ADDR_QUAL(port); 15105 15106 /* Check cport state */ 15107 port_state = SATA_CPORT_STATE(sata_hba_inst, cport); 15108 if (port_state & SATA_PSTATE_SHUTDOWN || 15109 port_state & SATA_PSTATE_FAILED) { 15110 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 15111 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 15112 if (port_state & SATA_PSTATE_FAILED) 15113 ap_state->ap_condition = AP_COND_FAILED; 15114 else 15115 ap_state->ap_condition = AP_COND_UNKNOWN; 15116 15117 return; 15118 } 15119 15120 /* cport state is okay. Now check pmport state */ 15121 if (qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) { 15122 /* Sanity check */ 15123 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 15124 SATA_DTYPE_PMULT || SATA_PMPORT_INFO(sata_hba_inst, 15125 cport, pmport) == NULL) 15126 return; 15127 port_state = SATA_PMPORT_STATE(sata_hba_inst, cport, pmport); 15128 if (port_state & SATA_PSTATE_SHUTDOWN || 15129 port_state & SATA_PSTATE_FAILED) { 15130 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 15131 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 15132 if (port_state & SATA_PSTATE_FAILED) 15133 ap_state->ap_condition = AP_COND_FAILED; 15134 else 15135 ap_state->ap_condition = AP_COND_UNKNOWN; 15136 15137 return; 15138 } 15139 } 15140 15141 /* Port is enabled and ready */ 15142 if (qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_CPORT) 15143 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, cport); 15144 else 15145 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport); 15146 15147 switch (dev_type) { 15148 case SATA_DTYPE_NONE: 15149 { 15150 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 15151 ap_state->ap_condition = AP_COND_OK; 15152 /* No device attached */ 15153 ap_state->ap_rstate = AP_RSTATE_EMPTY; 15154 break; 15155 } 15156 case SATA_DTYPE_PMULT: 15157 { 15158 /* Need to check port multiplier state */ 15159 ASSERT(qual == SATA_ADDR_DCPORT); 15160 pmult_state = SATA_PMULT_INFO(sata_hba_inst, cport)-> 15161 pmult_state; 15162 if (pmult_state & (SATA_PSTATE_SHUTDOWN|SATA_PSTATE_FAILED)) { 15163 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 15164 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 15165 if (pmult_state & SATA_PSTATE_FAILED) 15166 ap_state->ap_condition = AP_COND_FAILED; 15167 else 15168 ap_state->ap_condition = AP_COND_UNKNOWN; 15169 15170 return; 15171 } 15172 15173 /* Port multiplier is not configurable */ 15174 ap_state->ap_ostate = AP_OSTATE_CONFIGURED; 15175 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 15176 ap_state->ap_condition = AP_COND_OK; 15177 break; 15178 } 15179 15180 case SATA_DTYPE_ATADISK: 15181 case SATA_DTYPE_ATAPICD: 15182 case SATA_DTYPE_ATAPITAPE: 15183 case SATA_DTYPE_ATAPIDISK: 15184 { 15185 dev_info_t *tdip = NULL; 15186 dev_info_t *dip = NULL; 15187 int circ; 15188 15189 dip = SATA_DIP(sata_hba_inst); 15190 tdip = sata_get_target_dip(dip, cport, pmport); 15191 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 15192 if (tdip != NULL) { 15193 ndi_devi_enter(dip, &circ); 15194 mutex_enter(&(DEVI(tdip)->devi_lock)); 15195 if (DEVI_IS_DEVICE_REMOVED(tdip)) { 15196 /* 15197 * There could be the case where previously 15198 * configured and opened device was removed 15199 * and unknown device was plugged. 15200 * In such case we want to show a device, and 15201 * its configured or unconfigured state but 15202 * indicate unusable condition untill the 15203 * old target node is released and removed. 15204 */ 15205 ap_state->ap_condition = AP_COND_UNUSABLE; 15206 } else { 15207 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, 15208 cport)); 15209 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 15210 cport); 15211 if (sdinfo != NULL) { 15212 if ((sdinfo->satadrv_state & 15213 SATA_DSTATE_FAILED) != 0) 15214 ap_state->ap_condition = 15215 AP_COND_FAILED; 15216 else 15217 ap_state->ap_condition = 15218 AP_COND_OK; 15219 } else { 15220 ap_state->ap_condition = 15221 AP_COND_UNKNOWN; 15222 } 15223 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, 15224 cport)); 15225 } 15226 if ((DEVI_IS_DEVICE_OFFLINE(tdip)) || 15227 (DEVI_IS_DEVICE_DOWN(tdip))) { 15228 ap_state->ap_ostate = 15229 AP_OSTATE_UNCONFIGURED; 15230 } else { 15231 ap_state->ap_ostate = 15232 AP_OSTATE_CONFIGURED; 15233 } 15234 mutex_exit(&(DEVI(tdip)->devi_lock)); 15235 ndi_devi_exit(dip, circ); 15236 } else { 15237 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 15238 ap_state->ap_condition = AP_COND_UNKNOWN; 15239 } 15240 break; 15241 } 15242 default: 15243 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 15244 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 15245 ap_state->ap_condition = AP_COND_UNKNOWN; 15246 /* 15247 * This is actually internal error condition (non fatal), 15248 * because we have already checked all defined device types. 15249 */ 15250 SATA_LOG_D((sata_hba_inst, CE_WARN, 15251 "sata_cfgadm_state: Internal error: " 15252 "unknown device type")); 15253 break; 15254 } 15255 } 15256 15257 15258 /* 15259 * Process ioctl get device path request. 15260 * 15261 * NOTE: Port multiplier has no target dip. Devices connected to port 15262 * multiplier have target node attached to the HBA node. The only difference 15263 * between them and the directly-attached device node is a target address. 15264 */ 15265 static int 15266 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst, 15267 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 15268 { 15269 char path[MAXPATHLEN]; 15270 uint32_t size; 15271 dev_info_t *tdip; 15272 15273 (void) strcpy(path, "/devices"); 15274 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 15275 &sata_device->satadev_addr)) == NULL) { 15276 /* 15277 * No such device. If this is a request for a size, do not 15278 * return EINVAL for non-existing target, because cfgadm 15279 * will then indicate a meaningless ioctl failure. 15280 * If this is a request for a path, indicate invalid 15281 * argument. 15282 */ 15283 if (ioc->get_size == 0) 15284 return (EINVAL); 15285 } else { 15286 (void) ddi_pathname(tdip, path + strlen(path)); 15287 } 15288 size = strlen(path) + 1; 15289 15290 if (ioc->get_size != 0) { 15291 if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz, 15292 mode) != 0) 15293 return (EFAULT); 15294 } else { 15295 if (ioc->bufsiz != size) 15296 return (EINVAL); 15297 15298 else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz, 15299 mode) != 0) 15300 return (EFAULT); 15301 } 15302 return (0); 15303 } 15304 15305 /* 15306 * Process ioctl get attachment point type request. 15307 * 15308 * NOTE: Port multiplier is supported. 15309 */ 15310 static int 15311 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst, 15312 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 15313 { 15314 uint32_t type_len; 15315 const char *ap_type; 15316 int dev_type; 15317 15318 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 15319 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, 15320 sata_device->satadev_addr.cport); 15321 else /* pmport */ 15322 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, 15323 sata_device->satadev_addr.cport, 15324 sata_device->satadev_addr.pmport); 15325 15326 switch (dev_type) { 15327 case SATA_DTYPE_NONE: 15328 ap_type = "port"; 15329 break; 15330 15331 case SATA_DTYPE_ATADISK: 15332 case SATA_DTYPE_ATAPIDISK: 15333 ap_type = "disk"; 15334 break; 15335 15336 case SATA_DTYPE_ATAPICD: 15337 ap_type = "cd/dvd"; 15338 break; 15339 15340 case SATA_DTYPE_ATAPITAPE: 15341 ap_type = "tape"; 15342 break; 15343 15344 case SATA_DTYPE_PMULT: 15345 ap_type = "sata-pmult"; 15346 break; 15347 15348 case SATA_DTYPE_UNKNOWN: 15349 ap_type = "unknown"; 15350 break; 15351 15352 default: 15353 ap_type = "unsupported"; 15354 break; 15355 15356 } /* end of dev_type switch */ 15357 15358 type_len = strlen(ap_type) + 1; 15359 15360 if (ioc->get_size) { 15361 if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz, 15362 mode) != 0) 15363 return (EFAULT); 15364 } else { 15365 if (ioc->bufsiz != type_len) 15366 return (EINVAL); 15367 15368 if (ddi_copyout((void *)ap_type, ioc->buf, 15369 ioc->bufsiz, mode) != 0) 15370 return (EFAULT); 15371 } 15372 return (0); 15373 15374 } 15375 15376 /* 15377 * Process ioctl get device model info request. 15378 * This operation should return to cfgadm the device model 15379 * information string 15380 * 15381 * NOTE: Port multiplier is supported. 15382 */ 15383 static int 15384 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst, 15385 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 15386 { 15387 sata_drive_info_t *sdinfo; 15388 uint32_t info_len; 15389 char ap_info[SATA_ID_MODEL_LEN + 1]; 15390 15391 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15392 sata_device->satadev_addr.cport)->cport_mutex); 15393 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 15394 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 15395 sata_device->satadev_addr.cport); 15396 else /* port multiplier */ 15397 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 15398 sata_device->satadev_addr.cport, 15399 sata_device->satadev_addr.pmport); 15400 if (sdinfo == NULL) { 15401 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15402 sata_device->satadev_addr.cport)->cport_mutex); 15403 return (EINVAL); 15404 } 15405 15406 #ifdef _LITTLE_ENDIAN 15407 swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN); 15408 #else /* _LITTLE_ENDIAN */ 15409 bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN); 15410 #endif /* _LITTLE_ENDIAN */ 15411 15412 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15413 sata_device->satadev_addr.cport)->cport_mutex); 15414 15415 ap_info[SATA_ID_MODEL_LEN] = '\0'; 15416 15417 info_len = strlen(ap_info) + 1; 15418 15419 if (ioc->get_size) { 15420 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 15421 mode) != 0) 15422 return (EFAULT); 15423 } else { 15424 if (ioc->bufsiz < info_len) 15425 return (EINVAL); 15426 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 15427 mode) != 0) 15428 return (EFAULT); 15429 } 15430 return (0); 15431 } 15432 15433 15434 /* 15435 * Process ioctl get device firmware revision info request. 15436 * This operation should return to cfgadm the device firmware revision 15437 * information string 15438 * 15439 * Port multiplier is supported. 15440 */ 15441 static int 15442 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst, 15443 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 15444 { 15445 sata_drive_info_t *sdinfo; 15446 uint32_t info_len; 15447 char ap_info[SATA_ID_FW_LEN + 1]; 15448 15449 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15450 sata_device->satadev_addr.cport)->cport_mutex); 15451 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 15452 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 15453 sata_device->satadev_addr.cport); 15454 else /* port multiplier */ 15455 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 15456 sata_device->satadev_addr.cport, 15457 sata_device->satadev_addr.pmport); 15458 if (sdinfo == NULL) { 15459 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15460 sata_device->satadev_addr.cport)->cport_mutex); 15461 return (EINVAL); 15462 } 15463 15464 #ifdef _LITTLE_ENDIAN 15465 swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN); 15466 #else /* _LITTLE_ENDIAN */ 15467 bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN); 15468 #endif /* _LITTLE_ENDIAN */ 15469 15470 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15471 sata_device->satadev_addr.cport)->cport_mutex); 15472 15473 ap_info[SATA_ID_FW_LEN] = '\0'; 15474 15475 info_len = strlen(ap_info) + 1; 15476 15477 if (ioc->get_size) { 15478 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 15479 mode) != 0) 15480 return (EFAULT); 15481 } else { 15482 if (ioc->bufsiz < info_len) 15483 return (EINVAL); 15484 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 15485 mode) != 0) 15486 return (EFAULT); 15487 } 15488 return (0); 15489 } 15490 15491 15492 /* 15493 * Process ioctl get device serial number info request. 15494 * This operation should return to cfgadm the device serial number string. 15495 * 15496 * NOTE: Port multiplier is supported. 15497 */ 15498 static int 15499 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst, 15500 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 15501 { 15502 sata_drive_info_t *sdinfo; 15503 uint32_t info_len; 15504 char ap_info[SATA_ID_SERIAL_LEN + 1]; 15505 15506 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15507 sata_device->satadev_addr.cport)->cport_mutex); 15508 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 15509 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 15510 sata_device->satadev_addr.cport); 15511 else /* port multiplier */ 15512 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 15513 sata_device->satadev_addr.cport, 15514 sata_device->satadev_addr.pmport); 15515 if (sdinfo == NULL) { 15516 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15517 sata_device->satadev_addr.cport)->cport_mutex); 15518 return (EINVAL); 15519 } 15520 15521 #ifdef _LITTLE_ENDIAN 15522 swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN); 15523 #else /* _LITTLE_ENDIAN */ 15524 bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN); 15525 #endif /* _LITTLE_ENDIAN */ 15526 15527 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15528 sata_device->satadev_addr.cport)->cport_mutex); 15529 15530 ap_info[SATA_ID_SERIAL_LEN] = '\0'; 15531 15532 info_len = strlen(ap_info) + 1; 15533 15534 if (ioc->get_size) { 15535 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 15536 mode) != 0) 15537 return (EFAULT); 15538 } else { 15539 if (ioc->bufsiz < info_len) 15540 return (EINVAL); 15541 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 15542 mode) != 0) 15543 return (EFAULT); 15544 } 15545 return (0); 15546 } 15547 15548 15549 /* 15550 * Preset scsi extended sense data (to NO SENSE) 15551 * First 18 bytes of the sense data are preset to current valid sense 15552 * with a key NO SENSE data. 15553 * 15554 * Returns void 15555 */ 15556 static void 15557 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense) 15558 { 15559 sense->es_valid = 1; /* Valid sense */ 15560 sense->es_class = CLASS_EXTENDED_SENSE; /* 0x70 - current err */ 15561 sense->es_key = KEY_NO_SENSE; 15562 sense->es_info_1 = 0; 15563 sense->es_info_2 = 0; 15564 sense->es_info_3 = 0; 15565 sense->es_info_4 = 0; 15566 sense->es_add_len = 10; /* Additional length - replace with a def */ 15567 sense->es_cmd_info[0] = 0; 15568 sense->es_cmd_info[1] = 0; 15569 sense->es_cmd_info[2] = 0; 15570 sense->es_cmd_info[3] = 0; 15571 sense->es_add_code = 0; 15572 sense->es_qual_code = 0; 15573 } 15574 15575 /* 15576 * Register a legacy cmdk-style devid for the target (disk) device. 15577 * 15578 * Note: This function is called only when the HBA devinfo node has the 15579 * property "use-cmdk-devid-format" set. This property indicates that 15580 * devid compatible with old cmdk (target) driver is to be generated 15581 * for any target device attached to this controller. This will take 15582 * precedence over the devid generated by sd (target) driver. 15583 * This function is derived from cmdk_devid_setup() function in cmdk.c. 15584 */ 15585 static void 15586 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo) 15587 { 15588 char *hwid; 15589 int modlen; 15590 int serlen; 15591 int rval; 15592 ddi_devid_t devid; 15593 15594 /* 15595 * device ID is a concatanation of model number, "=", serial number. 15596 */ 15597 hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP); 15598 bcopy(&sdinfo->satadrv_id.ai_model, hwid, 15599 sizeof (sdinfo->satadrv_id.ai_model)); 15600 swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model)); 15601 modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model)); 15602 if (modlen == 0) 15603 goto err; 15604 hwid[modlen++] = '='; 15605 bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen], 15606 sizeof (sdinfo->satadrv_id.ai_drvser)); 15607 swab(&hwid[modlen], &hwid[modlen], 15608 sizeof (sdinfo->satadrv_id.ai_drvser)); 15609 serlen = sata_check_modser(&hwid[modlen], 15610 sizeof (sdinfo->satadrv_id.ai_drvser)); 15611 if (serlen == 0) 15612 goto err; 15613 hwid[modlen + serlen] = 0; /* terminate the hwid string */ 15614 15615 /* initialize/register devid */ 15616 if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL, 15617 (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) { 15618 rval = ddi_devid_register(dip, devid); 15619 /* 15620 * Free up the allocated devid buffer. 15621 * NOTE: This doesn't mean unregistering devid. 15622 */ 15623 ddi_devid_free(devid); 15624 } 15625 15626 if (rval != DDI_SUCCESS) 15627 cmn_err(CE_WARN, "sata: failed to create devid for the disk" 15628 " on port %d", sdinfo->satadrv_addr.cport); 15629 err: 15630 kmem_free(hwid, LEGACY_HWID_LEN); 15631 } 15632 15633 /* 15634 * valid model/serial string must contain a non-zero non-space characters. 15635 * trim trailing spaces/NULLs. 15636 */ 15637 static int 15638 sata_check_modser(char *buf, int buf_len) 15639 { 15640 boolean_t ret; 15641 char *s; 15642 int i; 15643 int tb; 15644 char ch; 15645 15646 ret = B_FALSE; 15647 s = buf; 15648 for (i = 0; i < buf_len; i++) { 15649 ch = *s++; 15650 if (ch != ' ' && ch != '\0') 15651 tb = i + 1; 15652 if (ch != ' ' && ch != '\0' && ch != '0') 15653 ret = B_TRUE; 15654 } 15655 15656 if (ret == B_FALSE) 15657 return (0); /* invalid string */ 15658 15659 return (tb); /* return length */ 15660 } 15661 15662 /* 15663 * sata_set_drive_features function compares current device features setting 15664 * with the saved device features settings and, if there is a difference, 15665 * it restores device features setting to the previously saved state. 15666 * It also arbitrarily tries to select the highest supported DMA mode. 15667 * Device Identify or Identify Packet Device data has to be current. 15668 * At the moment read ahead and write cache are considered for all devices. 15669 * For atapi devices, Removable Media Status Notification is set in addition 15670 * to common features. 15671 * 15672 * This function cannot be called in the interrupt context (it may sleep). 15673 * 15674 * The input argument sdinfo should point to the drive info structure 15675 * to be updated after features are set. Note, that only 15676 * device (packet) identify data is updated, not the flags indicating the 15677 * supported features. 15678 * 15679 * Returns SATA_SUCCESS if successful or there was nothing to do. 15680 * Device Identify data in the drive info structure pointed to by the sdinfo 15681 * arguments is updated even when no features were set or changed. 15682 * 15683 * Returns SATA_FAILURE if device features could not be set or DMA mode 15684 * for a disk cannot be set and device identify data cannot be fetched. 15685 * 15686 * Returns SATA_RETRY if device features could not be set (other than disk 15687 * DMA mode) but the device identify data was fetched successfully. 15688 * 15689 * Note: This function may fail the port, making it inaccessible. 15690 * In such case the explicit port disconnect/connect or physical device 15691 * detach/attach is required to re-evaluate port state again. 15692 */ 15693 15694 static int 15695 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst, 15696 sata_drive_info_t *sdinfo, int restore) 15697 { 15698 int rval = SATA_SUCCESS; 15699 int rval_set; 15700 sata_drive_info_t new_sdinfo; 15701 char *finfo = "sata_set_drive_features: cannot"; 15702 char *finfox; 15703 int cache_op; 15704 15705 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 15706 new_sdinfo.satadrv_addr = sdinfo->satadrv_addr; 15707 new_sdinfo.satadrv_type = sdinfo->satadrv_type; 15708 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 15709 /* 15710 * Cannot get device identification - caller may retry later 15711 */ 15712 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15713 "%s fetch device identify data\n", finfo); 15714 return (SATA_FAILURE); 15715 } 15716 finfox = (restore != 0) ? " restore device features" : 15717 " initialize device features\n"; 15718 15719 switch (sdinfo->satadrv_type) { 15720 case SATA_DTYPE_ATADISK: 15721 /* Arbitrarily set UDMA mode */ 15722 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) != 15723 SATA_SUCCESS) { 15724 SATA_LOG_D((sata_hba_inst, CE_WARN, 15725 "%s set UDMA mode\n", finfo)); 15726 return (SATA_FAILURE); 15727 } 15728 break; 15729 case SATA_DTYPE_ATAPICD: 15730 case SATA_DTYPE_ATAPITAPE: 15731 case SATA_DTYPE_ATAPIDISK: 15732 /* Set Removable Media Status Notification, if necessary */ 15733 if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) && 15734 restore != 0) { 15735 if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) && 15736 (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))|| 15737 ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) && 15738 SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) { 15739 /* Current setting does not match saved one */ 15740 if (sata_set_rmsn(sata_hba_inst, sdinfo, 15741 sdinfo->satadrv_settings & 15742 SATA_DEV_RMSN) != SATA_SUCCESS) 15743 rval = SATA_FAILURE; 15744 } 15745 } 15746 /* 15747 * We have to set Multiword DMA or UDMA, if it is supported, as 15748 * we want to use DMA transfer mode whenever possible. 15749 * Some devices require explicit setting of the DMA mode. 15750 */ 15751 if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) { 15752 /* Set highest supported DMA mode */ 15753 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) != 15754 SATA_SUCCESS) { 15755 SATA_LOG_D((sata_hba_inst, CE_WARN, 15756 "%s set UDMA mode\n", finfo)); 15757 rval = SATA_FAILURE; 15758 } 15759 } 15760 break; 15761 } 15762 15763 if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) && 15764 !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) { 15765 /* 15766 * neither READ AHEAD nor WRITE CACHE is supported 15767 * - do nothing 15768 */ 15769 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15770 "settable features not supported\n", NULL); 15771 goto update_sdinfo; 15772 } 15773 15774 if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) && 15775 (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) && 15776 (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) && 15777 (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) { 15778 /* 15779 * both READ AHEAD and WRITE CACHE are enabled 15780 * - Nothing to do 15781 */ 15782 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15783 "no device features to set\n", NULL); 15784 goto update_sdinfo; 15785 } 15786 15787 cache_op = 0; 15788 15789 if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) { 15790 if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) && 15791 !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) { 15792 /* Enable read ahead / read cache */ 15793 cache_op = SATAC_SF_ENABLE_READ_AHEAD; 15794 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15795 "enabling read cache\n", NULL); 15796 } else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) && 15797 SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) { 15798 /* Disable read ahead / read cache */ 15799 cache_op = SATAC_SF_DISABLE_READ_AHEAD; 15800 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15801 "disabling read cache\n", NULL); 15802 } 15803 15804 if (cache_op != 0) { 15805 /* Try to set read cache mode */ 15806 rval_set = sata_set_cache_mode(sata_hba_inst, 15807 &new_sdinfo, cache_op); 15808 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS) 15809 rval = rval_set; 15810 } 15811 } 15812 15813 cache_op = 0; 15814 15815 if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) { 15816 if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) && 15817 !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) { 15818 /* Enable write cache */ 15819 cache_op = SATAC_SF_ENABLE_WRITE_CACHE; 15820 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15821 "enabling write cache\n", NULL); 15822 } else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) && 15823 SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) { 15824 /* Disable write cache */ 15825 cache_op = SATAC_SF_DISABLE_WRITE_CACHE; 15826 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15827 "disabling write cache\n", NULL); 15828 } 15829 15830 if (cache_op != 0) { 15831 /* Try to set write cache mode */ 15832 rval_set = sata_set_cache_mode(sata_hba_inst, 15833 &new_sdinfo, cache_op); 15834 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS) 15835 rval = rval_set; 15836 } 15837 } 15838 if (rval != SATA_SUCCESS) 15839 SATA_LOG_D((sata_hba_inst, CE_WARN, 15840 "%s %s", finfo, finfox)); 15841 15842 update_sdinfo: 15843 /* 15844 * We need to fetch Device Identify data again 15845 */ 15846 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 15847 /* 15848 * Cannot get device identification - retry later 15849 */ 15850 SATA_LOG_D((sata_hba_inst, CE_WARN, 15851 "%s re-fetch device identify data\n", finfo)); 15852 rval = SATA_FAILURE; 15853 } 15854 /* Copy device sata info. */ 15855 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 15856 15857 return (rval); 15858 } 15859 15860 15861 /* 15862 * 15863 * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if 15864 * unable to determine. 15865 * 15866 * Cannot be called in an interrupt context. 15867 * 15868 * Called by sata_build_lsense_page_2f() 15869 */ 15870 15871 static int 15872 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst, 15873 sata_drive_info_t *sdinfo) 15874 { 15875 sata_pkt_t *spkt; 15876 sata_cmd_t *scmd; 15877 sata_pkt_txlate_t *spx; 15878 int rval; 15879 15880 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 15881 spx->txlt_sata_hba_inst = sata_hba_inst; 15882 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 15883 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 15884 if (spkt == NULL) { 15885 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15886 return (-1); 15887 } 15888 /* address is needed now */ 15889 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15890 15891 15892 /* Fill sata_pkt */ 15893 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15894 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 15895 /* Synchronous mode, no callback */ 15896 spkt->satapkt_comp = NULL; 15897 /* Timeout 30s */ 15898 spkt->satapkt_time = sata_default_pkt_time; 15899 15900 scmd = &spkt->satapkt_cmd; 15901 scmd->satacmd_flags.sata_special_regs = B_TRUE; 15902 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 15903 15904 /* Set up which registers need to be returned */ 15905 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE; 15906 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE; 15907 15908 /* Build SMART_RETURN_STATUS cmd in the sata_pkt */ 15909 scmd->satacmd_addr_type = 0; /* N/A */ 15910 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 15911 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 15912 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 15913 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 15914 scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS; 15915 scmd->satacmd_device_reg = 0; /* Always device 0 */ 15916 scmd->satacmd_cmd_reg = SATAC_SMART; 15917 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 15918 sdinfo->satadrv_addr.cport))); 15919 15920 15921 /* Send pkt to SATA HBA driver */ 15922 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 15923 SATA_TRAN_ACCEPTED || 15924 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 15925 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15926 sdinfo->satadrv_addr.cport))); 15927 /* 15928 * Whoops, no SMART RETURN STATUS 15929 */ 15930 rval = -1; 15931 } else { 15932 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15933 sdinfo->satadrv_addr.cport))); 15934 if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) { 15935 rval = -1; 15936 goto fail; 15937 } 15938 if (scmd->satacmd_status_reg & SATA_STATUS_ERR) { 15939 rval = -1; 15940 goto fail; 15941 } 15942 if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) && 15943 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2)) 15944 rval = 0; 15945 else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) && 15946 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4)) 15947 rval = 1; 15948 else { 15949 rval = -1; 15950 goto fail; 15951 } 15952 } 15953 fail: 15954 /* Free allocated resources */ 15955 sata_pkt_free(spx); 15956 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15957 15958 return (rval); 15959 } 15960 15961 /* 15962 * 15963 * Returns 0 if succeeded, -1 otherwise 15964 * 15965 * Cannot be called in an interrupt context. 15966 * 15967 */ 15968 static int 15969 sata_fetch_smart_data( 15970 sata_hba_inst_t *sata_hba_inst, 15971 sata_drive_info_t *sdinfo, 15972 struct smart_data *smart_data) 15973 { 15974 sata_pkt_t *spkt; 15975 sata_cmd_t *scmd; 15976 sata_pkt_txlate_t *spx; 15977 int rval; 15978 15979 #if ! defined(lint) 15980 ASSERT(sizeof (struct smart_data) == 512); 15981 #endif 15982 15983 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 15984 spx->txlt_sata_hba_inst = sata_hba_inst; 15985 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 15986 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 15987 if (spkt == NULL) { 15988 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15989 return (-1); 15990 } 15991 /* address is needed now */ 15992 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15993 15994 15995 /* Fill sata_pkt */ 15996 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15997 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 15998 /* Synchronous mode, no callback */ 15999 spkt->satapkt_comp = NULL; 16000 /* Timeout 30s */ 16001 spkt->satapkt_time = sata_default_pkt_time; 16002 16003 scmd = &spkt->satapkt_cmd; 16004 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 16005 16006 /* 16007 * Allocate buffer for SMART data 16008 */ 16009 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 16010 sizeof (struct smart_data)); 16011 if (scmd->satacmd_bp == NULL) { 16012 sata_pkt_free(spx); 16013 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16014 SATA_LOG_D((sata_hba_inst, CE_WARN, 16015 "sata_fetch_smart_data: " 16016 "cannot allocate buffer")); 16017 return (-1); 16018 } 16019 16020 16021 /* Build SMART_READ_DATA cmd in the sata_pkt */ 16022 scmd->satacmd_addr_type = 0; /* N/A */ 16023 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 16024 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 16025 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 16026 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 16027 scmd->satacmd_features_reg = SATA_SMART_READ_DATA; 16028 scmd->satacmd_device_reg = 0; /* Always device 0 */ 16029 scmd->satacmd_cmd_reg = SATAC_SMART; 16030 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 16031 sdinfo->satadrv_addr.cport))); 16032 16033 /* Send pkt to SATA HBA driver */ 16034 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 16035 SATA_TRAN_ACCEPTED || 16036 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 16037 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16038 sdinfo->satadrv_addr.cport))); 16039 /* 16040 * Whoops, no SMART DATA available 16041 */ 16042 rval = -1; 16043 goto fail; 16044 } else { 16045 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16046 sdinfo->satadrv_addr.cport))); 16047 if (spx->txlt_buf_dma_handle != NULL) { 16048 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 16049 DDI_DMA_SYNC_FORKERNEL); 16050 ASSERT(rval == DDI_SUCCESS); 16051 } 16052 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data, 16053 sizeof (struct smart_data)); 16054 } 16055 16056 fail: 16057 /* Free allocated resources */ 16058 sata_free_local_buffer(spx); 16059 sata_pkt_free(spx); 16060 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16061 16062 return (rval); 16063 } 16064 16065 /* 16066 * Used by LOG SENSE page 0x10 16067 * Reads (in synchronous mode) the self test log data using Read Log Ext cmd. 16068 * Note: cannot be called in the interrupt context. 16069 * 16070 * return 0 for success, -1 otherwise 16071 * 16072 */ 16073 static int 16074 sata_ext_smart_selftest_read_log( 16075 sata_hba_inst_t *sata_hba_inst, 16076 sata_drive_info_t *sdinfo, 16077 struct smart_ext_selftest_log *ext_selftest_log, 16078 uint16_t block_num) 16079 { 16080 sata_pkt_txlate_t *spx; 16081 sata_pkt_t *spkt; 16082 sata_cmd_t *scmd; 16083 int rval; 16084 16085 #if ! defined(lint) 16086 ASSERT(sizeof (struct smart_ext_selftest_log) == 512); 16087 #endif 16088 16089 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 16090 spx->txlt_sata_hba_inst = sata_hba_inst; 16091 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 16092 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 16093 if (spkt == NULL) { 16094 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16095 return (-1); 16096 } 16097 /* address is needed now */ 16098 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16099 16100 16101 /* Fill sata_pkt */ 16102 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16103 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 16104 /* Synchronous mode, no callback */ 16105 spkt->satapkt_comp = NULL; 16106 /* Timeout 30s */ 16107 spkt->satapkt_time = sata_default_pkt_time; 16108 16109 scmd = &spkt->satapkt_cmd; 16110 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 16111 16112 /* 16113 * Allocate buffer for SMART extended self-test log 16114 */ 16115 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 16116 sizeof (struct smart_ext_selftest_log)); 16117 if (scmd->satacmd_bp == NULL) { 16118 sata_pkt_free(spx); 16119 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16120 SATA_LOG_D((sata_hba_inst, CE_WARN, 16121 "sata_ext_smart_selftest_log: " 16122 "cannot allocate buffer")); 16123 return (-1); 16124 } 16125 16126 /* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */ 16127 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 16128 scmd->satacmd_sec_count_lsb = 1; /* One sector of selftest log */ 16129 scmd->satacmd_sec_count_msb = 0; /* One sector of selftest log */ 16130 scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE; 16131 scmd->satacmd_lba_low_msb = 0; 16132 scmd->satacmd_lba_mid_lsb = block_num & 0xff; 16133 scmd->satacmd_lba_mid_msb = block_num >> 8; 16134 scmd->satacmd_device_reg = 0; /* Always device 0 */ 16135 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 16136 16137 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 16138 sdinfo->satadrv_addr.cport))); 16139 16140 /* Send pkt to SATA HBA driver */ 16141 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 16142 SATA_TRAN_ACCEPTED || 16143 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 16144 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16145 sdinfo->satadrv_addr.cport))); 16146 16147 /* 16148 * Whoops, no SMART selftest log info available 16149 */ 16150 rval = -1; 16151 goto fail; 16152 } else { 16153 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16154 sdinfo->satadrv_addr.cport))); 16155 16156 if (spx->txlt_buf_dma_handle != NULL) { 16157 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 16158 DDI_DMA_SYNC_FORKERNEL); 16159 ASSERT(rval == DDI_SUCCESS); 16160 } 16161 bcopy(scmd->satacmd_bp->b_un.b_addr, 16162 (uint8_t *)ext_selftest_log, 16163 sizeof (struct smart_ext_selftest_log)); 16164 rval = 0; 16165 } 16166 16167 fail: 16168 /* Free allocated resources */ 16169 sata_free_local_buffer(spx); 16170 sata_pkt_free(spx); 16171 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16172 16173 return (rval); 16174 } 16175 16176 /* 16177 * Returns 0 for success, -1 otherwise 16178 * 16179 * SMART self-test log data is returned in buffer pointed to by selftest_log 16180 */ 16181 static int 16182 sata_smart_selftest_log( 16183 sata_hba_inst_t *sata_hba_inst, 16184 sata_drive_info_t *sdinfo, 16185 struct smart_selftest_log *selftest_log) 16186 { 16187 sata_pkt_t *spkt; 16188 sata_cmd_t *scmd; 16189 sata_pkt_txlate_t *spx; 16190 int rval; 16191 16192 #if ! defined(lint) 16193 ASSERT(sizeof (struct smart_selftest_log) == 512); 16194 #endif 16195 16196 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 16197 spx->txlt_sata_hba_inst = sata_hba_inst; 16198 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 16199 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 16200 if (spkt == NULL) { 16201 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16202 return (-1); 16203 } 16204 /* address is needed now */ 16205 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16206 16207 16208 /* Fill sata_pkt */ 16209 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16210 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 16211 /* Synchronous mode, no callback */ 16212 spkt->satapkt_comp = NULL; 16213 /* Timeout 30s */ 16214 spkt->satapkt_time = sata_default_pkt_time; 16215 16216 scmd = &spkt->satapkt_cmd; 16217 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 16218 16219 /* 16220 * Allocate buffer for SMART SELFTEST LOG 16221 */ 16222 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 16223 sizeof (struct smart_selftest_log)); 16224 if (scmd->satacmd_bp == NULL) { 16225 sata_pkt_free(spx); 16226 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16227 SATA_LOG_D((sata_hba_inst, CE_WARN, 16228 "sata_smart_selftest_log: " 16229 "cannot allocate buffer")); 16230 return (-1); 16231 } 16232 16233 /* Build SMART_READ_LOG cmd in the sata_pkt */ 16234 scmd->satacmd_addr_type = 0; /* N/A */ 16235 scmd->satacmd_sec_count_lsb = 1; /* One sector of SMART log */ 16236 scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE; 16237 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 16238 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 16239 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 16240 scmd->satacmd_device_reg = 0; /* Always device 0 */ 16241 scmd->satacmd_cmd_reg = SATAC_SMART; 16242 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 16243 sdinfo->satadrv_addr.cport))); 16244 16245 /* Send pkt to SATA HBA driver */ 16246 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 16247 SATA_TRAN_ACCEPTED || 16248 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 16249 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16250 sdinfo->satadrv_addr.cport))); 16251 /* 16252 * Whoops, no SMART DATA available 16253 */ 16254 rval = -1; 16255 goto fail; 16256 } else { 16257 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16258 sdinfo->satadrv_addr.cport))); 16259 if (spx->txlt_buf_dma_handle != NULL) { 16260 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 16261 DDI_DMA_SYNC_FORKERNEL); 16262 ASSERT(rval == DDI_SUCCESS); 16263 } 16264 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log, 16265 sizeof (struct smart_selftest_log)); 16266 rval = 0; 16267 } 16268 16269 fail: 16270 /* Free allocated resources */ 16271 sata_free_local_buffer(spx); 16272 sata_pkt_free(spx); 16273 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16274 16275 return (rval); 16276 } 16277 16278 16279 /* 16280 * Returns 0 for success, -1 otherwise 16281 * 16282 * SMART READ LOG data is returned in buffer pointed to by smart_log 16283 */ 16284 static int 16285 sata_smart_read_log( 16286 sata_hba_inst_t *sata_hba_inst, 16287 sata_drive_info_t *sdinfo, 16288 uint8_t *smart_log, /* where the data should be returned */ 16289 uint8_t which_log, /* which log should be returned */ 16290 uint8_t log_size) /* # of 512 bytes in log */ 16291 { 16292 sata_pkt_t *spkt; 16293 sata_cmd_t *scmd; 16294 sata_pkt_txlate_t *spx; 16295 int rval; 16296 16297 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 16298 spx->txlt_sata_hba_inst = sata_hba_inst; 16299 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 16300 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 16301 if (spkt == NULL) { 16302 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16303 return (-1); 16304 } 16305 /* address is needed now */ 16306 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16307 16308 16309 /* Fill sata_pkt */ 16310 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16311 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 16312 /* Synchronous mode, no callback */ 16313 spkt->satapkt_comp = NULL; 16314 /* Timeout 30s */ 16315 spkt->satapkt_time = sata_default_pkt_time; 16316 16317 scmd = &spkt->satapkt_cmd; 16318 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 16319 16320 /* 16321 * Allocate buffer for SMART READ LOG 16322 */ 16323 scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512); 16324 if (scmd->satacmd_bp == NULL) { 16325 sata_pkt_free(spx); 16326 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16327 SATA_LOG_D((sata_hba_inst, CE_WARN, 16328 "sata_smart_read_log: " "cannot allocate buffer")); 16329 return (-1); 16330 } 16331 16332 /* Build SMART_READ_LOG cmd in the sata_pkt */ 16333 scmd->satacmd_addr_type = 0; /* N/A */ 16334 scmd->satacmd_sec_count_lsb = log_size; /* what the caller asked for */ 16335 scmd->satacmd_lba_low_lsb = which_log; /* which log page */ 16336 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 16337 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 16338 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 16339 scmd->satacmd_device_reg = 0; /* Always device 0 */ 16340 scmd->satacmd_cmd_reg = SATAC_SMART; 16341 16342 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 16343 sdinfo->satadrv_addr.cport))); 16344 16345 /* Send pkt to SATA HBA driver */ 16346 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 16347 SATA_TRAN_ACCEPTED || 16348 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 16349 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16350 sdinfo->satadrv_addr.cport))); 16351 16352 /* 16353 * Whoops, no SMART DATA available 16354 */ 16355 rval = -1; 16356 goto fail; 16357 } else { 16358 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16359 sdinfo->satadrv_addr.cport))); 16360 16361 if (spx->txlt_buf_dma_handle != NULL) { 16362 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 16363 DDI_DMA_SYNC_FORKERNEL); 16364 ASSERT(rval == DDI_SUCCESS); 16365 } 16366 bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512); 16367 rval = 0; 16368 } 16369 16370 fail: 16371 /* Free allocated resources */ 16372 sata_free_local_buffer(spx); 16373 sata_pkt_free(spx); 16374 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16375 16376 return (rval); 16377 } 16378 16379 /* 16380 * Used by LOG SENSE page 0x10 16381 * 16382 * return 0 for success, -1 otherwise 16383 * 16384 */ 16385 static int 16386 sata_read_log_ext_directory( 16387 sata_hba_inst_t *sata_hba_inst, 16388 sata_drive_info_t *sdinfo, 16389 struct read_log_ext_directory *logdir) 16390 { 16391 sata_pkt_txlate_t *spx; 16392 sata_pkt_t *spkt; 16393 sata_cmd_t *scmd; 16394 int rval; 16395 16396 #if ! defined(lint) 16397 ASSERT(sizeof (struct read_log_ext_directory) == 512); 16398 #endif 16399 16400 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 16401 spx->txlt_sata_hba_inst = sata_hba_inst; 16402 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 16403 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 16404 if (spkt == NULL) { 16405 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16406 return (-1); 16407 } 16408 16409 /* Fill sata_pkt */ 16410 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16411 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 16412 /* Synchronous mode, no callback */ 16413 spkt->satapkt_comp = NULL; 16414 /* Timeout 30s */ 16415 spkt->satapkt_time = sata_default_pkt_time; 16416 16417 scmd = &spkt->satapkt_cmd; 16418 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 16419 16420 /* 16421 * Allocate buffer for SMART READ LOG EXTENDED command 16422 */ 16423 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 16424 sizeof (struct read_log_ext_directory)); 16425 if (scmd->satacmd_bp == NULL) { 16426 sata_pkt_free(spx); 16427 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16428 SATA_LOG_D((sata_hba_inst, CE_WARN, 16429 "sata_read_log_ext_directory: " 16430 "cannot allocate buffer")); 16431 return (-1); 16432 } 16433 16434 /* Build READ LOG EXT w/ log directory cmd in the sata_pkt */ 16435 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 16436 scmd->satacmd_sec_count_lsb = 1; /* One sector of directory */ 16437 scmd->satacmd_sec_count_msb = 0; /* One sector of directory */ 16438 scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY; 16439 scmd->satacmd_lba_low_msb = 0; 16440 scmd->satacmd_lba_mid_lsb = 0; 16441 scmd->satacmd_lba_mid_msb = 0; 16442 scmd->satacmd_device_reg = 0; /* Always device 0 */ 16443 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 16444 16445 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 16446 sdinfo->satadrv_addr.cport))); 16447 16448 /* Send pkt to SATA HBA driver */ 16449 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 16450 SATA_TRAN_ACCEPTED || 16451 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 16452 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16453 sdinfo->satadrv_addr.cport))); 16454 /* 16455 * Whoops, no SMART selftest log info available 16456 */ 16457 rval = -1; 16458 goto fail; 16459 } else { 16460 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16461 sdinfo->satadrv_addr.cport))); 16462 if (spx->txlt_buf_dma_handle != NULL) { 16463 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 16464 DDI_DMA_SYNC_FORKERNEL); 16465 ASSERT(rval == DDI_SUCCESS); 16466 } 16467 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir, 16468 sizeof (struct read_log_ext_directory)); 16469 rval = 0; 16470 } 16471 16472 fail: 16473 /* Free allocated resources */ 16474 sata_free_local_buffer(spx); 16475 sata_pkt_free(spx); 16476 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16477 16478 return (rval); 16479 } 16480 16481 /* 16482 * Set up error retrieval sata command for NCQ command error data 16483 * recovery. 16484 * 16485 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up, 16486 * returns SATA_FAILURE otherwise. 16487 */ 16488 static int 16489 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo) 16490 { 16491 #ifndef __lock_lint 16492 _NOTE(ARGUNUSED(sdinfo)) 16493 #endif 16494 16495 sata_pkt_t *spkt = spx->txlt_sata_pkt; 16496 sata_cmd_t *scmd; 16497 struct buf *bp; 16498 16499 /* Operation modes are up to the caller */ 16500 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 16501 16502 /* Synchronous mode, no callback - may be changed by the caller */ 16503 spkt->satapkt_comp = NULL; 16504 spkt->satapkt_time = sata_default_pkt_time; 16505 16506 scmd = &spkt->satapkt_cmd; 16507 bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t)); 16508 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 16509 16510 /* 16511 * Allocate dma_able buffer error data. 16512 * Buffer allocation will take care of buffer alignment and other DMA 16513 * attributes. 16514 */ 16515 bp = sata_alloc_local_buffer(spx, 16516 sizeof (struct sata_ncq_error_recovery_page)); 16517 if (bp == NULL) 16518 return (SATA_FAILURE); 16519 16520 bp_mapin(bp); /* make data buffer accessible */ 16521 scmd->satacmd_bp = bp; 16522 16523 /* 16524 * Set-up pointer to the buffer handle, so HBA can sync buffer 16525 * before accessing it. Handle is in usual place in translate struct. 16526 */ 16527 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle; 16528 16529 ASSERT(scmd->satacmd_num_dma_cookies != 0); 16530 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 16531 16532 return (SATA_SUCCESS); 16533 } 16534 16535 /* 16536 * sata_xlate_errors() is used to translate (S)ATA error 16537 * information to SCSI information returned in the SCSI 16538 * packet. 16539 */ 16540 static void 16541 sata_xlate_errors(sata_pkt_txlate_t *spx) 16542 { 16543 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 16544 struct scsi_extended_sense *sense; 16545 16546 scsipkt->pkt_reason = CMD_INCOMPLETE; 16547 *scsipkt->pkt_scbp = STATUS_CHECK; 16548 sense = sata_arq_sense(spx); 16549 16550 switch (spx->txlt_sata_pkt->satapkt_reason) { 16551 case SATA_PKT_PORT_ERROR: 16552 /* 16553 * We have no device data. Assume no data transfered. 16554 */ 16555 sense->es_key = KEY_HARDWARE_ERROR; 16556 break; 16557 16558 case SATA_PKT_DEV_ERROR: 16559 if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 16560 SATA_STATUS_ERR) { 16561 /* 16562 * determine dev error reason from error 16563 * reg content 16564 */ 16565 sata_decode_device_error(spx, sense); 16566 break; 16567 } 16568 /* No extended sense key - no info available */ 16569 break; 16570 16571 case SATA_PKT_TIMEOUT: 16572 scsipkt->pkt_reason = CMD_TIMEOUT; 16573 scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET; 16574 /* No extended sense key */ 16575 break; 16576 16577 case SATA_PKT_ABORTED: 16578 scsipkt->pkt_reason = CMD_ABORTED; 16579 scsipkt->pkt_statistics |= STAT_ABORTED; 16580 /* No extended sense key */ 16581 break; 16582 16583 case SATA_PKT_RESET: 16584 /* 16585 * pkt aborted either by an explicit reset request from 16586 * a host, or due to error recovery 16587 */ 16588 scsipkt->pkt_reason = CMD_RESET; 16589 scsipkt->pkt_statistics |= STAT_DEV_RESET; 16590 break; 16591 16592 default: 16593 scsipkt->pkt_reason = CMD_TRAN_ERR; 16594 break; 16595 } 16596 } 16597 16598 16599 16600 16601 /* 16602 * Log sata message 16603 * dev pathname msg line preceeds the logged message. 16604 */ 16605 16606 static void 16607 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...) 16608 { 16609 char pathname[128]; 16610 dev_info_t *dip = NULL; 16611 va_list ap; 16612 16613 mutex_enter(&sata_log_mutex); 16614 16615 va_start(ap, fmt); 16616 (void) vsprintf(sata_log_buf, fmt, ap); 16617 va_end(ap); 16618 16619 if (sata_hba_inst != NULL) { 16620 dip = SATA_DIP(sata_hba_inst); 16621 (void) ddi_pathname(dip, pathname); 16622 } else { 16623 pathname[0] = 0; 16624 } 16625 if (level == CE_CONT) { 16626 if (sata_debug_flags == 0) 16627 cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf); 16628 else 16629 cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf); 16630 } else { 16631 if (level != CE_NOTE) { 16632 cmn_err(level, "%s:\n %s", pathname, sata_log_buf); 16633 } else if (sata_msg) { 16634 cmn_err(level, "%s:\n %s", pathname, 16635 sata_log_buf); 16636 } 16637 } 16638 16639 /* sata trace debug */ 16640 sata_trace_debug(dip, sata_log_buf); 16641 16642 mutex_exit(&sata_log_mutex); 16643 } 16644 16645 16646 /* ******** Asynchronous HBA events handling & hotplugging support ******** */ 16647 16648 /* 16649 * Start or terminate the thread, depending on flag arg and current state 16650 */ 16651 static void 16652 sata_event_thread_control(int startstop) 16653 { 16654 static int sata_event_thread_terminating = 0; 16655 static int sata_event_thread_starting = 0; 16656 int i; 16657 16658 mutex_enter(&sata_event_mutex); 16659 16660 if (startstop == 0 && (sata_event_thread_starting == 1 || 16661 sata_event_thread_terminating == 1)) { 16662 mutex_exit(&sata_event_mutex); 16663 return; 16664 } 16665 if (startstop == 1 && sata_event_thread_starting == 1) { 16666 mutex_exit(&sata_event_mutex); 16667 return; 16668 } 16669 if (startstop == 1 && sata_event_thread_terminating == 1) { 16670 sata_event_thread_starting = 1; 16671 /* wait til terminate operation completes */ 16672 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 16673 while (sata_event_thread_terminating == 1) { 16674 if (i-- <= 0) { 16675 sata_event_thread_starting = 0; 16676 mutex_exit(&sata_event_mutex); 16677 #ifdef SATA_DEBUG 16678 cmn_err(CE_WARN, "sata_event_thread_control: " 16679 "timeout waiting for thread to terminate"); 16680 #endif 16681 return; 16682 } 16683 mutex_exit(&sata_event_mutex); 16684 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 16685 mutex_enter(&sata_event_mutex); 16686 } 16687 } 16688 if (startstop == 1) { 16689 if (sata_event_thread == NULL) { 16690 sata_event_thread = thread_create(NULL, 0, 16691 (void (*)())sata_event_daemon, 16692 &sata_hba_list, 0, &p0, TS_RUN, minclsyspri); 16693 } 16694 sata_event_thread_starting = 0; 16695 mutex_exit(&sata_event_mutex); 16696 return; 16697 } 16698 16699 /* 16700 * If we got here, thread may need to be terminated 16701 */ 16702 if (sata_event_thread != NULL) { 16703 int i; 16704 /* Signal event thread to go away */ 16705 sata_event_thread_terminating = 1; 16706 sata_event_thread_terminate = 1; 16707 cv_signal(&sata_event_cv); 16708 /* 16709 * Wait til daemon terminates. 16710 */ 16711 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 16712 while (sata_event_thread_terminate == 1) { 16713 mutex_exit(&sata_event_mutex); 16714 if (i-- <= 0) { 16715 /* Daemon did not go away !!! */ 16716 #ifdef SATA_DEBUG 16717 cmn_err(CE_WARN, "sata_event_thread_control: " 16718 "cannot terminate event daemon thread"); 16719 #endif 16720 mutex_enter(&sata_event_mutex); 16721 break; 16722 } 16723 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 16724 mutex_enter(&sata_event_mutex); 16725 } 16726 sata_event_thread_terminating = 0; 16727 } 16728 ASSERT(sata_event_thread_terminating == 0); 16729 ASSERT(sata_event_thread_starting == 0); 16730 mutex_exit(&sata_event_mutex); 16731 } 16732 16733 16734 /* 16735 * SATA HBA event notification function. 16736 * Events reported by SATA HBA drivers per HBA instance relate to a change in 16737 * a port and/or device state or a controller itself. 16738 * Events for different addresses/addr types cannot be combined. 16739 * A warning message is generated for each event type. 16740 * Events are not processed by this function, so only the 16741 * event flag(s)is set for an affected entity and the event thread is 16742 * waken up. Event daemon thread processes all events. 16743 * 16744 * NOTE: Since more than one event may be reported at the same time, one 16745 * cannot determine a sequence of events when opposite event are reported, eg. 16746 * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing 16747 * is taking precedence over reported events, i.e. may cause ignoring some 16748 * events. 16749 */ 16750 #define SATA_EVENT_MAX_MSG_LENGTH 79 16751 16752 void 16753 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event) 16754 { 16755 sata_hba_inst_t *sata_hba_inst = NULL; 16756 sata_address_t *saddr; 16757 sata_pmult_info_t *pmultinfo; 16758 sata_drive_info_t *sdinfo; 16759 sata_port_stats_t *pstats; 16760 sata_cport_info_t *cportinfo; 16761 sata_pmport_info_t *pmportinfo; 16762 int cport, pmport; 16763 char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1]; 16764 char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1]; 16765 char *lcp; 16766 static char *err_msg_evnt_1 = 16767 "sata_hba_event_notify: invalid port event 0x%x "; 16768 static char *err_msg_evnt_2 = 16769 "sata_hba_event_notify: invalid device event 0x%x "; 16770 int linkevent; 16771 16772 /* 16773 * There is a possibility that an event will be generated on HBA 16774 * that has not completed attachment or is detaching. We still want 16775 * to process events until HBA is detached. 16776 */ 16777 mutex_enter(&sata_mutex); 16778 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 16779 sata_hba_inst = sata_hba_inst->satahba_next) { 16780 if (SATA_DIP(sata_hba_inst) == dip) 16781 if (sata_hba_inst->satahba_attached == 1) 16782 break; 16783 } 16784 mutex_exit(&sata_mutex); 16785 if (sata_hba_inst == NULL) 16786 /* HBA not attached */ 16787 return; 16788 16789 ASSERT(sata_device != NULL); 16790 16791 /* 16792 * Validate address before - do not proceed with invalid address. 16793 */ 16794 saddr = &sata_device->satadev_addr; 16795 if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst)) 16796 return; 16797 16798 cport = saddr->cport; 16799 pmport = saddr->pmport; 16800 16801 buf1[0] = buf2[0] = '\0'; 16802 16803 /* 16804 * If event relates to port or device, check port state. 16805 * Port has to be initialized, or we cannot accept an event. 16806 */ 16807 if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT | 16808 SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT | SATA_ADDR_PMULT)) != 0) { 16809 mutex_enter(&sata_hba_inst->satahba_mutex); 16810 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 16811 mutex_exit(&sata_hba_inst->satahba_mutex); 16812 if (cportinfo == NULL || cportinfo->cport_state == 0) 16813 return; 16814 } 16815 16816 if ((saddr->qual & (SATA_ADDR_PMULT | SATA_ADDR_PMPORT | 16817 SATA_ADDR_DPMPORT)) != 0) { 16818 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 16819 SATA_LOG_D((sata_hba_inst, CE_WARN, 16820 "sata_hba_event_notify: Non-pmult device (0x%x)" 16821 "is attached to port %d, ignore pmult/pmport " 16822 "event 0x%x", cportinfo->cport_dev_type, 16823 cport, event)); 16824 return; 16825 } 16826 16827 mutex_enter(&cportinfo->cport_mutex); 16828 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 16829 mutex_exit(&cportinfo->cport_mutex); 16830 16831 /* 16832 * The daemon might be processing attachment of port 16833 * multiplier, in that case we should ignore events on its 16834 * sub-devices. 16835 * 16836 * NOTE: Only pmult_state is checked in sata_hba_event_notify. 16837 * The pmport_state is checked by sata daemon. 16838 */ 16839 if (pmultinfo == NULL || 16840 pmultinfo->pmult_state == SATA_STATE_UNKNOWN) { 16841 SATA_LOG_D((sata_hba_inst, CE_WARN, 16842 "sata_hba_event_notify: pmult is not" 16843 "available at port %d:%d, ignore event 0x%x", 16844 cport, pmport, event)); 16845 return; 16846 } 16847 } 16848 16849 if ((saddr->qual & 16850 (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) != 0) { 16851 16852 mutex_enter(&cportinfo->cport_mutex); 16853 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) { 16854 SATA_LOG_D((sata_hba_inst, CE_WARN, 16855 "sata_hba_event_notify: invalid/" 16856 "un-implemented port %d:%d (%d ports), " 16857 "ignore event 0x%x", cport, pmport, 16858 SATA_NUM_PMPORTS(sata_hba_inst, cport), event)); 16859 mutex_exit(&cportinfo->cport_mutex); 16860 return; 16861 } 16862 mutex_exit(&cportinfo->cport_mutex); 16863 16864 mutex_enter(&sata_hba_inst->satahba_mutex); 16865 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 16866 cport, pmport); 16867 mutex_exit(&sata_hba_inst->satahba_mutex); 16868 16869 /* pmport is implemented/valid? */ 16870 if (pmportinfo == NULL) { 16871 SATA_LOG_D((sata_hba_inst, CE_WARN, 16872 "sata_hba_event_notify: invalid/" 16873 "un-implemented port %d:%d, ignore " 16874 "event 0x%x", cport, pmport, event)); 16875 return; 16876 } 16877 } 16878 16879 /* 16880 * Events refer to devices, ports and controllers - each has 16881 * unique address. Events for different addresses cannot be combined. 16882 */ 16883 if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) { 16884 16885 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16886 16887 /* qualify this event(s) */ 16888 if ((event & SATA_EVNT_PORT_EVENTS) == 0) { 16889 /* Invalid event for the device port */ 16890 (void) sprintf(buf2, err_msg_evnt_1, 16891 event & SATA_EVNT_PORT_EVENTS); 16892 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16893 goto event_info; 16894 } 16895 if (saddr->qual == SATA_ADDR_CPORT) { 16896 /* Controller's device port event */ 16897 16898 (SATA_CPORT_INFO(sata_hba_inst, cport))-> 16899 cport_event_flags |= 16900 event & SATA_EVNT_PORT_EVENTS; 16901 pstats = 16902 &(SATA_CPORT_INFO(sata_hba_inst, cport))-> 16903 cport_stats; 16904 } else { 16905 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16906 mutex_enter(&pmportinfo->pmport_mutex); 16907 /* Port multiplier's device port event */ 16908 (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 16909 pmport_event_flags |= 16910 event & SATA_EVNT_PORT_EVENTS; 16911 pstats = 16912 &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 16913 pmport_stats; 16914 mutex_exit(&pmportinfo->pmport_mutex); 16915 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16916 } 16917 16918 /* 16919 * Add to statistics and log the message. We have to do it 16920 * here rather than in the event daemon, because there may be 16921 * multiple events occuring before they are processed. 16922 */ 16923 linkevent = event & 16924 (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED); 16925 if (linkevent) { 16926 if (linkevent == (SATA_EVNT_LINK_LOST | 16927 SATA_EVNT_LINK_ESTABLISHED)) { 16928 /* This is likely event combination */ 16929 (void) strlcat(buf1, "link lost/established, ", 16930 SATA_EVENT_MAX_MSG_LENGTH); 16931 16932 if (pstats->link_lost < 0xffffffffffffffffULL) 16933 pstats->link_lost++; 16934 if (pstats->link_established < 16935 0xffffffffffffffffULL) 16936 pstats->link_established++; 16937 linkevent = 0; 16938 } else if (linkevent & SATA_EVNT_LINK_LOST) { 16939 (void) strlcat(buf1, "link lost, ", 16940 SATA_EVENT_MAX_MSG_LENGTH); 16941 16942 if (pstats->link_lost < 0xffffffffffffffffULL) 16943 pstats->link_lost++; 16944 } else { 16945 (void) strlcat(buf1, "link established, ", 16946 SATA_EVENT_MAX_MSG_LENGTH); 16947 if (pstats->link_established < 16948 0xffffffffffffffffULL) 16949 pstats->link_established++; 16950 } 16951 } 16952 if (event & SATA_EVNT_DEVICE_ATTACHED) { 16953 (void) strlcat(buf1, "device attached, ", 16954 SATA_EVENT_MAX_MSG_LENGTH); 16955 if (pstats->device_attached < 0xffffffffffffffffULL) 16956 pstats->device_attached++; 16957 } 16958 if (event & SATA_EVNT_DEVICE_DETACHED) { 16959 (void) strlcat(buf1, "device detached, ", 16960 SATA_EVENT_MAX_MSG_LENGTH); 16961 if (pstats->device_detached < 0xffffffffffffffffULL) 16962 pstats->device_detached++; 16963 } 16964 if (event & SATA_EVNT_PWR_LEVEL_CHANGED) { 16965 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 16966 "port %d power level changed", cport); 16967 if (pstats->port_pwr_changed < 0xffffffffffffffffULL) 16968 pstats->port_pwr_changed++; 16969 } 16970 16971 if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) { 16972 /* There should be no other events for this address */ 16973 (void) sprintf(buf2, err_msg_evnt_1, 16974 event & ~SATA_EVNT_PORT_EVENTS); 16975 } 16976 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16977 16978 } else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) { 16979 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16980 16981 /* qualify this event */ 16982 if ((event & SATA_EVNT_DEVICE_RESET) == 0) { 16983 /* Invalid event for a device */ 16984 (void) sprintf(buf2, err_msg_evnt_2, 16985 event & SATA_EVNT_DEVICE_RESET); 16986 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16987 goto event_info; 16988 } 16989 /* drive event */ 16990 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 16991 if (sdinfo != NULL) { 16992 if (event & SATA_EVNT_DEVICE_RESET) { 16993 (void) strlcat(buf1, "device reset, ", 16994 SATA_EVENT_MAX_MSG_LENGTH); 16995 if (sdinfo->satadrv_stats.drive_reset < 16996 0xffffffffffffffffULL) 16997 sdinfo->satadrv_stats.drive_reset++; 16998 sdinfo->satadrv_event_flags |= 16999 SATA_EVNT_DEVICE_RESET; 17000 } 17001 } 17002 if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) { 17003 /* Invalid event for a device */ 17004 (void) sprintf(buf2, err_msg_evnt_2, 17005 event & ~SATA_EVNT_DRIVE_EVENTS); 17006 } 17007 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17008 } else if (saddr->qual == SATA_ADDR_PMULT) { 17009 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17010 17011 /* qualify this event */ 17012 if ((event & (SATA_EVNT_DEVICE_RESET | 17013 SATA_EVNT_PMULT_LINK_CHANGED)) == 0) { 17014 /* Invalid event for a port multiplier */ 17015 (void) sprintf(buf2, err_msg_evnt_2, 17016 event & SATA_EVNT_DEVICE_RESET); 17017 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17018 goto event_info; 17019 } 17020 17021 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 17022 17023 if (event & SATA_EVNT_DEVICE_RESET) { 17024 17025 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 17026 "[Reset] port-mult on cport %d", cport); 17027 pmultinfo->pmult_event_flags |= 17028 SATA_EVNT_DEVICE_RESET; 17029 (void) strlcat(buf1, "pmult reset, ", 17030 SATA_EVENT_MAX_MSG_LENGTH); 17031 } 17032 17033 if (event & SATA_EVNT_PMULT_LINK_CHANGED) { 17034 17035 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 17036 "pmult link changed on cport %d", cport); 17037 pmultinfo->pmult_event_flags |= 17038 SATA_EVNT_PMULT_LINK_CHANGED; 17039 (void) strlcat(buf1, "pmult link changed, ", 17040 SATA_EVENT_MAX_MSG_LENGTH); 17041 } 17042 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17043 17044 } else { 17045 if (saddr->qual != SATA_ADDR_NULL) { 17046 /* Wrong address qualifier */ 17047 SATA_LOG_D((sata_hba_inst, CE_WARN, 17048 "sata_hba_event_notify: invalid address 0x%x", 17049 *(uint32_t *)saddr)); 17050 return; 17051 } 17052 if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 || 17053 (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) { 17054 /* Invalid event for the controller */ 17055 SATA_LOG_D((sata_hba_inst, CE_WARN, 17056 "sata_hba_event_notify: invalid event 0x%x for " 17057 "controller", 17058 event & SATA_EVNT_CONTROLLER_EVENTS)); 17059 return; 17060 } 17061 buf1[0] = '\0'; 17062 /* This may be a frequent and not interesting event */ 17063 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 17064 "controller power level changed\n", NULL); 17065 17066 mutex_enter(&sata_hba_inst->satahba_mutex); 17067 if (sata_hba_inst->satahba_stats.ctrl_pwr_change < 17068 0xffffffffffffffffULL) 17069 sata_hba_inst->satahba_stats.ctrl_pwr_change++; 17070 17071 sata_hba_inst->satahba_event_flags |= 17072 SATA_EVNT_PWR_LEVEL_CHANGED; 17073 mutex_exit(&sata_hba_inst->satahba_mutex); 17074 } 17075 /* 17076 * If we got here, there is something to do with this HBA 17077 * instance. 17078 */ 17079 mutex_enter(&sata_hba_inst->satahba_mutex); 17080 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 17081 mutex_exit(&sata_hba_inst->satahba_mutex); 17082 mutex_enter(&sata_mutex); 17083 sata_event_pending |= SATA_EVNT_MAIN; /* global event indicator */ 17084 mutex_exit(&sata_mutex); 17085 17086 /* Tickle event thread */ 17087 mutex_enter(&sata_event_mutex); 17088 if (sata_event_thread_active == 0) 17089 cv_signal(&sata_event_cv); 17090 mutex_exit(&sata_event_mutex); 17091 17092 event_info: 17093 if (buf1[0] != '\0') { 17094 lcp = strrchr(buf1, ','); 17095 if (lcp != NULL) 17096 *lcp = '\0'; 17097 } 17098 if (saddr->qual == SATA_ADDR_CPORT || 17099 saddr->qual == SATA_ADDR_DCPORT) { 17100 if (buf1[0] != '\0') { 17101 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 17102 cport, buf1); 17103 } 17104 if (buf2[0] != '\0') { 17105 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 17106 cport, buf2); 17107 } 17108 } else if (saddr->qual == SATA_ADDR_PMPORT || 17109 saddr->qual == SATA_ADDR_DPMPORT) { 17110 if (buf1[0] != '\0') { 17111 sata_log(sata_hba_inst, CE_NOTE, 17112 "port %d pmport %d: %s\n", cport, pmport, buf1); 17113 } 17114 if (buf2[0] != '\0') { 17115 sata_log(sata_hba_inst, CE_NOTE, 17116 "port %d pmport %d: %s\n", cport, pmport, buf2); 17117 } 17118 } 17119 } 17120 17121 17122 /* 17123 * Event processing thread. 17124 * Arg is a pointer to the sata_hba_list pointer. 17125 * It is not really needed, because sata_hba_list is global and static 17126 */ 17127 static void 17128 sata_event_daemon(void *arg) 17129 { 17130 #ifndef __lock_lint 17131 _NOTE(ARGUNUSED(arg)) 17132 #endif 17133 sata_hba_inst_t *sata_hba_inst; 17134 clock_t delta; 17135 17136 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 17137 "SATA event daemon started\n", NULL); 17138 loop: 17139 /* 17140 * Process events here. Walk through all registered HBAs 17141 */ 17142 mutex_enter(&sata_mutex); 17143 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 17144 sata_hba_inst = sata_hba_inst->satahba_next) { 17145 ASSERT(sata_hba_inst != NULL); 17146 mutex_enter(&sata_hba_inst->satahba_mutex); 17147 if (sata_hba_inst->satahba_attached == 0 || 17148 (sata_hba_inst->satahba_event_flags & 17149 SATA_EVNT_SKIP) != 0) { 17150 mutex_exit(&sata_hba_inst->satahba_mutex); 17151 continue; 17152 } 17153 if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) { 17154 sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP; 17155 mutex_exit(&sata_hba_inst->satahba_mutex); 17156 mutex_exit(&sata_mutex); 17157 /* Got the controller with pending event */ 17158 sata_process_controller_events(sata_hba_inst); 17159 /* 17160 * Since global mutex was released, there is a 17161 * possibility that HBA list has changed, so start 17162 * over from the top. Just processed controller 17163 * will be passed-over because of the SKIP flag. 17164 */ 17165 goto loop; 17166 } 17167 mutex_exit(&sata_hba_inst->satahba_mutex); 17168 } 17169 /* Clear SKIP flag in all controllers */ 17170 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 17171 sata_hba_inst = sata_hba_inst->satahba_next) { 17172 mutex_enter(&sata_hba_inst->satahba_mutex); 17173 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP; 17174 mutex_exit(&sata_hba_inst->satahba_mutex); 17175 } 17176 mutex_exit(&sata_mutex); 17177 17178 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 17179 "SATA EVENT DAEMON suspending itself", NULL); 17180 17181 #ifdef SATA_DEBUG 17182 if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) { 17183 sata_log(sata_hba_inst, CE_WARN, 17184 "SATA EVENTS PROCESSING DISABLED\n"); 17185 thread_exit(); /* Daemon will not run again */ 17186 } 17187 #endif 17188 mutex_enter(&sata_event_mutex); 17189 sata_event_thread_active = 0; 17190 mutex_exit(&sata_event_mutex); 17191 /* 17192 * Go to sleep/suspend itself and wake up either because new event or 17193 * wait timeout. Exit if there is a termination request (driver 17194 * unload). 17195 */ 17196 delta = drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME); 17197 do { 17198 mutex_enter(&sata_event_mutex); 17199 (void) cv_reltimedwait(&sata_event_cv, &sata_event_mutex, 17200 delta, TR_CLOCK_TICK); 17201 17202 if (sata_event_thread_active != 0) { 17203 mutex_exit(&sata_event_mutex); 17204 continue; 17205 } 17206 17207 /* Check if it is time to go away */ 17208 if (sata_event_thread_terminate == 1) { 17209 /* 17210 * It is up to the thread setting above flag to make 17211 * sure that this thread is not killed prematurely. 17212 */ 17213 sata_event_thread_terminate = 0; 17214 sata_event_thread = NULL; 17215 mutex_exit(&sata_event_mutex); 17216 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 17217 "SATA_EVENT_DAEMON_TERMINATING", NULL); 17218 thread_exit(); { _NOTE(NOT_REACHED) } 17219 } 17220 mutex_exit(&sata_event_mutex); 17221 } while (!(sata_event_pending & SATA_EVNT_MAIN)); 17222 17223 mutex_enter(&sata_event_mutex); 17224 sata_event_thread_active = 1; 17225 mutex_exit(&sata_event_mutex); 17226 17227 mutex_enter(&sata_mutex); 17228 sata_event_pending &= ~SATA_EVNT_MAIN; 17229 mutex_exit(&sata_mutex); 17230 17231 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 17232 "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL); 17233 17234 goto loop; 17235 } 17236 17237 /* 17238 * Specific HBA instance event processing. 17239 * 17240 * NOTE: At the moment, device event processing is limited to hard disks 17241 * only. 17242 * Port multiplier is supported now. 17243 */ 17244 static void 17245 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst) 17246 { 17247 int ncport; 17248 uint32_t event_flags; 17249 sata_address_t *saddr; 17250 sata_cport_info_t *cportinfo; 17251 sata_pmult_info_t *pmultinfo; 17252 17253 SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst, 17254 "Processing controller %d event(s)", 17255 ddi_get_instance(SATA_DIP(sata_hba_inst))); 17256 17257 mutex_enter(&sata_hba_inst->satahba_mutex); 17258 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN; 17259 event_flags = sata_hba_inst->satahba_event_flags; 17260 mutex_exit(&sata_hba_inst->satahba_mutex); 17261 /* 17262 * Process controller power change first 17263 * HERE 17264 */ 17265 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) 17266 sata_process_cntrl_pwr_level_change(sata_hba_inst); 17267 17268 /* 17269 * Search through ports/devices to identify affected port/device. 17270 * We may have to process events for more than one port/device. 17271 */ 17272 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 17273 /* 17274 * Not all ports may be processed in attach by the time we 17275 * get an event. Check if port info is initialized. 17276 */ 17277 mutex_enter(&sata_hba_inst->satahba_mutex); 17278 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 17279 mutex_exit(&sata_hba_inst->satahba_mutex); 17280 if (cportinfo == NULL || cportinfo->cport_state == NULL) 17281 continue; 17282 17283 /* We have initialized controller port info */ 17284 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 17285 event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 17286 cport_event_flags; 17287 /* Check if port was locked by IOCTL processing */ 17288 if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) { 17289 /* 17290 * We ignore port events because port is busy 17291 * with AP control processing. Set again 17292 * controller and main event flag, so that 17293 * events may be processed by the next daemon 17294 * run. 17295 */ 17296 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 17297 mutex_enter(&sata_hba_inst->satahba_mutex); 17298 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 17299 mutex_exit(&sata_hba_inst->satahba_mutex); 17300 mutex_enter(&sata_mutex); 17301 sata_event_pending |= SATA_EVNT_MAIN; 17302 mutex_exit(&sata_mutex); 17303 SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst, 17304 "Event processing postponed until " 17305 "AP control processing completes", 17306 NULL); 17307 /* Check other ports */ 17308 continue; 17309 } else { 17310 /* 17311 * Set BSY flag so that AP control would not 17312 * interfere with events processing for 17313 * this port. 17314 */ 17315 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 17316 cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY; 17317 } 17318 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 17319 17320 saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr; 17321 17322 if ((event_flags & 17323 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) { 17324 /* 17325 * Got port event. 17326 * We need some hierarchy of event processing as they 17327 * are affecting each other: 17328 * 1. port failed 17329 * 2. device detached/attached 17330 * 3. link events - link events may trigger device 17331 * detached or device attached events in some 17332 * circumstances. 17333 * 4. port power level changed 17334 */ 17335 if (event_flags & SATA_EVNT_PORT_FAILED) { 17336 sata_process_port_failed_event(sata_hba_inst, 17337 saddr); 17338 } 17339 if (event_flags & SATA_EVNT_DEVICE_DETACHED) { 17340 sata_process_device_detached(sata_hba_inst, 17341 saddr); 17342 } 17343 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) { 17344 sata_process_device_attached(sata_hba_inst, 17345 saddr); 17346 } 17347 if (event_flags & 17348 (SATA_EVNT_LINK_ESTABLISHED | 17349 SATA_EVNT_LINK_LOST)) { 17350 sata_process_port_link_events(sata_hba_inst, 17351 saddr); 17352 } 17353 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) { 17354 sata_process_port_pwr_change(sata_hba_inst, 17355 saddr); 17356 } 17357 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) { 17358 sata_process_target_node_cleanup( 17359 sata_hba_inst, saddr); 17360 } 17361 if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) { 17362 sata_process_device_autoonline( 17363 sata_hba_inst, saddr); 17364 } 17365 } 17366 17367 17368 /* 17369 * Scan port multiplier and all its sub-ports event flags. 17370 * The events are marked by 17371 * (1) sata_pmult_info.pmult_event_flags 17372 * (2) sata_pmport_info.pmport_event_flags 17373 */ 17374 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 17375 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 17376 /* 17377 * There should be another extra check: this 17378 * port multiplier still exists? 17379 */ 17380 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, 17381 ncport); 17382 17383 if (pmultinfo != NULL) { 17384 mutex_exit(&(SATA_CPORT_MUTEX( 17385 sata_hba_inst, ncport))); 17386 sata_process_pmult_events( 17387 sata_hba_inst, ncport); 17388 mutex_enter(&(SATA_CPORT_MUTEX( 17389 sata_hba_inst, ncport))); 17390 } else { 17391 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 17392 "Port-multiplier is gone. " 17393 "Ignore all sub-device events " 17394 "at port %d.", ncport); 17395 } 17396 } 17397 17398 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) != 17399 SATA_DTYPE_NONE) && 17400 (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) { 17401 if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)-> 17402 satadrv_event_flags & 17403 (SATA_EVNT_DEVICE_RESET | 17404 SATA_EVNT_INPROC_DEVICE_RESET)) { 17405 /* Have device event */ 17406 sata_process_device_reset(sata_hba_inst, 17407 saddr); 17408 } 17409 } 17410 /* Release PORT_BUSY flag */ 17411 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 17412 cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY; 17413 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 17414 17415 } /* End of loop through the controller SATA ports */ 17416 } 17417 17418 /* 17419 * Specific port multiplier instance event processing. At the moment, device 17420 * event processing is limited to link/attach event only. 17421 * 17422 * NOTE: power management event is not supported yet. 17423 */ 17424 static void 17425 sata_process_pmult_events(sata_hba_inst_t *sata_hba_inst, uint8_t cport) 17426 { 17427 sata_cport_info_t *cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 17428 sata_pmult_info_t *pmultinfo; 17429 sata_pmport_info_t *pmportinfo; 17430 sata_address_t *saddr; 17431 sata_device_t sata_device; 17432 uint32_t event_flags; 17433 int npmport; 17434 int rval; 17435 17436 SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst, 17437 "Processing pmult event(s) on cport %d of controller %d", 17438 cport, ddi_get_instance(SATA_DIP(sata_hba_inst))); 17439 17440 /* First process events on port multiplier */ 17441 mutex_enter(&cportinfo->cport_mutex); 17442 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 17443 event_flags = pmultinfo->pmult_event_flags; 17444 17445 /* 17446 * Reset event (of port multiplier) has higher priority because the 17447 * port multiplier itself might be failed or removed after reset. 17448 */ 17449 if (event_flags & SATA_EVNT_DEVICE_RESET) { 17450 /* 17451 * The status of the sub-links are uncertain, 17452 * so mark all sub-ports as RESET 17453 */ 17454 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 17455 sata_hba_inst, cport); npmport ++) { 17456 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 17457 cport, npmport); 17458 if (pmportinfo == NULL) { 17459 /* That's weird. */ 17460 SATA_LOG_D((sata_hba_inst, CE_WARN, 17461 "sata_hba_event_notify: " 17462 "invalid/un-implemented " 17463 "port %d:%d (%d ports), ", 17464 cport, npmport, SATA_NUM_PMPORTS( 17465 sata_hba_inst, cport))); 17466 continue; 17467 } 17468 17469 mutex_enter(&pmportinfo->pmport_mutex); 17470 17471 /* Mark all pmport to unknow state. */ 17472 pmportinfo->pmport_state = SATA_STATE_UNKNOWN; 17473 /* Mark all pmports with link events. */ 17474 pmportinfo->pmport_event_flags = 17475 (SATA_EVNT_LINK_ESTABLISHED|SATA_EVNT_LINK_LOST); 17476 mutex_exit(&pmportinfo->pmport_mutex); 17477 } 17478 17479 } else if (event_flags & SATA_EVNT_PMULT_LINK_CHANGED) { 17480 /* 17481 * We need probe the port multiplier to know what has 17482 * happened. 17483 */ 17484 bzero(&sata_device, sizeof (sata_device_t)); 17485 sata_device.satadev_rev = SATA_DEVICE_REV; 17486 sata_device.satadev_addr.cport = cport; 17487 sata_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT; 17488 sata_device.satadev_addr.qual = SATA_ADDR_PMULT; 17489 17490 mutex_exit(&cportinfo->cport_mutex); 17491 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 17492 (SATA_DIP(sata_hba_inst), &sata_device); 17493 mutex_enter(&cportinfo->cport_mutex); 17494 if (rval != SATA_SUCCESS) { 17495 /* Something went wrong? Fail the port */ 17496 cportinfo->cport_state = SATA_PSTATE_FAILED; 17497 mutex_exit(&cportinfo->cport_mutex); 17498 SATA_LOG_D((sata_hba_inst, CE_WARN, 17499 "SATA port %d probing failed", cport)); 17500 17501 /* PMult structure must be released. */ 17502 sata_free_pmult(sata_hba_inst, &sata_device); 17503 return; 17504 } 17505 17506 sata_update_port_info(sata_hba_inst, &sata_device); 17507 17508 /* 17509 * Sanity check - Port is active? Is the link active? 17510 * The device is still a port multiplier? 17511 */ 17512 if ((cportinfo->cport_state & 17513 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 17514 ((cportinfo->cport_scr.sstatus & 17515 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) || 17516 (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) { 17517 mutex_exit(&cportinfo->cport_mutex); 17518 17519 /* PMult structure must be released. */ 17520 sata_free_pmult(sata_hba_inst, &sata_device); 17521 return; 17522 } 17523 17524 /* Probed succeed, set port ready. */ 17525 cportinfo->cport_state |= 17526 SATA_STATE_PROBED | SATA_STATE_READY; 17527 } 17528 17529 /* Release port multiplier event flags. */ 17530 pmultinfo->pmult_event_flags &= 17531 ~(SATA_EVNT_DEVICE_RESET|SATA_EVNT_PMULT_LINK_CHANGED); 17532 mutex_exit(&cportinfo->cport_mutex); 17533 17534 /* 17535 * Check all sub-links. 17536 */ 17537 for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, cport); 17538 npmport ++) { 17539 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport); 17540 mutex_enter(&pmportinfo->pmport_mutex); 17541 event_flags = pmportinfo->pmport_event_flags; 17542 mutex_exit(&pmportinfo->pmport_mutex); 17543 saddr = &pmportinfo->pmport_addr; 17544 17545 if ((event_flags & 17546 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) { 17547 /* 17548 * Got port multiplier port event. 17549 * We need some hierarchy of event processing as they 17550 * are affecting each other: 17551 * 1. device detached/attached 17552 * 2. link events - link events may trigger device 17553 * detached or device attached events in some 17554 * circumstances. 17555 */ 17556 if (event_flags & SATA_EVNT_DEVICE_DETACHED) { 17557 sata_process_pmdevice_detached(sata_hba_inst, 17558 saddr); 17559 } 17560 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) { 17561 sata_process_pmdevice_attached(sata_hba_inst, 17562 saddr); 17563 } 17564 if (event_flags & SATA_EVNT_LINK_ESTABLISHED || 17565 event_flags & SATA_EVNT_LINK_LOST) { 17566 sata_process_pmport_link_events(sata_hba_inst, 17567 saddr); 17568 } 17569 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) { 17570 sata_process_target_node_cleanup( 17571 sata_hba_inst, saddr); 17572 } 17573 } 17574 17575 /* Checking drive event(s). */ 17576 mutex_enter(&pmportinfo->pmport_mutex); 17577 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE && 17578 pmportinfo->pmport_sata_drive != NULL) { 17579 event_flags = pmportinfo->pmport_sata_drive-> 17580 satadrv_event_flags; 17581 if (event_flags & (SATA_EVNT_DEVICE_RESET | 17582 SATA_EVNT_INPROC_DEVICE_RESET)) { 17583 17584 /* Have device event */ 17585 sata_process_pmdevice_reset(sata_hba_inst, 17586 saddr); 17587 } 17588 } 17589 mutex_exit(&pmportinfo->pmport_mutex); 17590 17591 /* Release PORT_BUSY flag */ 17592 mutex_enter(&cportinfo->cport_mutex); 17593 cportinfo->cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY; 17594 mutex_exit(&cportinfo->cport_mutex); 17595 } 17596 17597 SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst, 17598 "[DONE] pmult event(s) on cport %d of controller %d", 17599 cport, ddi_get_instance(SATA_DIP(sata_hba_inst))); 17600 } 17601 17602 /* 17603 * Process HBA power level change reported by HBA driver. 17604 * Not implemented at this time - event is ignored. 17605 */ 17606 static void 17607 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst) 17608 { 17609 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17610 "Processing controller power level change", NULL); 17611 17612 /* Ignoring it for now */ 17613 mutex_enter(&sata_hba_inst->satahba_mutex); 17614 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 17615 mutex_exit(&sata_hba_inst->satahba_mutex); 17616 } 17617 17618 /* 17619 * Process port power level change reported by HBA driver. 17620 * Not implemented at this time - event is ignored. 17621 */ 17622 static void 17623 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst, 17624 sata_address_t *saddr) 17625 { 17626 sata_cport_info_t *cportinfo; 17627 17628 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17629 "Processing port power level change", NULL); 17630 17631 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 17632 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17633 /* Reset event flag */ 17634 cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 17635 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17636 } 17637 17638 /* 17639 * Process port failure reported by HBA driver. 17640 * cports support only - no pmports. 17641 */ 17642 static void 17643 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst, 17644 sata_address_t *saddr) 17645 { 17646 sata_cport_info_t *cportinfo; 17647 17648 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 17649 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17650 /* Reset event flag first */ 17651 cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED; 17652 /* If the port is in SHUTDOWN or FAILED state, ignore this event. */ 17653 if ((cportinfo->cport_state & 17654 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) { 17655 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 17656 cport_mutex); 17657 return; 17658 } 17659 /* Fail the port */ 17660 cportinfo->cport_state = SATA_PSTATE_FAILED; 17661 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17662 sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport); 17663 } 17664 17665 /* 17666 * Device Reset Event processing. 17667 * The seqeunce is managed by 3 stage flags: 17668 * - reset event reported, 17669 * - reset event being processed, 17670 * - request to clear device reset state. 17671 * 17672 * NOTE: This function has to be entered with cport mutex held. It exits with 17673 * mutex held as well, but can release mutex during the processing. 17674 */ 17675 static void 17676 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst, 17677 sata_address_t *saddr) 17678 { 17679 sata_drive_info_t old_sdinfo; /* local copy of the drive info */ 17680 sata_drive_info_t *sdinfo; 17681 sata_cport_info_t *cportinfo; 17682 sata_device_t sata_device; 17683 int rval_probe, rval_set; 17684 17685 /* We only care about host sata cport for now */ 17686 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 17687 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 17688 /* 17689 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED 17690 * state, ignore reset event. 17691 */ 17692 if (((cportinfo->cport_state & 17693 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) || 17694 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 17695 sdinfo->satadrv_event_flags &= 17696 ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET); 17697 return; 17698 } 17699 17700 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) == 17701 SATA_DTYPE_PMULT)) { 17702 /* 17703 * Should not happened: this is already handled in 17704 * sata_hba_event_notify() 17705 */ 17706 mutex_exit(&cportinfo->cport_mutex); 17707 goto done; 17708 } 17709 17710 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) & 17711 SATA_VALID_DEV_TYPE) == 0) { 17712 /* 17713 * This should not happen - coding error. 17714 * But we can recover, so do not panic, just clean up 17715 * and if in debug mode, log the message. 17716 */ 17717 #ifdef SATA_DEBUG 17718 sata_log(sata_hba_inst, CE_WARN, 17719 "sata_process_device_reset: " 17720 "Invalid device type with sdinfo!", NULL); 17721 #endif 17722 sdinfo->satadrv_event_flags = 0; 17723 return; 17724 } 17725 17726 #ifdef SATA_DEBUG 17727 if ((sdinfo->satadrv_event_flags & 17728 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) { 17729 /* Nothing to do */ 17730 /* Something is weird - why we are processing dev reset? */ 17731 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17732 "No device reset event!!!!", NULL); 17733 17734 return; 17735 } 17736 if ((sdinfo->satadrv_event_flags & 17737 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 17738 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 17739 /* Something is weird - new device reset event */ 17740 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17741 "Overlapping device reset events!", NULL); 17742 } 17743 #endif 17744 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17745 "Processing port %d device reset", saddr->cport); 17746 17747 /* Clear event flag */ 17748 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET; 17749 17750 /* It seems that we always need to check the port state first */ 17751 sata_device.satadev_rev = SATA_DEVICE_REV; 17752 sata_device.satadev_addr = *saddr; 17753 /* 17754 * We have to exit mutex, because the HBA probe port function may 17755 * block on its own mutex. 17756 */ 17757 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17758 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 17759 (SATA_DIP(sata_hba_inst), &sata_device); 17760 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17761 sata_update_port_info(sata_hba_inst, &sata_device); 17762 if (rval_probe != SATA_SUCCESS) { 17763 /* Something went wrong? Fail the port */ 17764 cportinfo->cport_state = SATA_PSTATE_FAILED; 17765 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 17766 if (sdinfo != NULL) 17767 sdinfo->satadrv_event_flags = 0; 17768 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 17769 cport_mutex); 17770 SATA_LOG_D((sata_hba_inst, CE_WARN, 17771 "SATA port %d probing failed", 17772 saddr->cport)); 17773 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 17774 saddr->cport)->cport_mutex); 17775 return; 17776 } 17777 if ((sata_device.satadev_scr.sstatus & 17778 SATA_PORT_DEVLINK_UP_MASK) != 17779 SATA_PORT_DEVLINK_UP || 17780 sata_device.satadev_type == SATA_DTYPE_NONE) { 17781 /* 17782 * No device to process, anymore. Some other event processing 17783 * would or have already performed port info cleanup. 17784 * To be safe (HBA may need it), request clearing device 17785 * reset condition. 17786 */ 17787 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 17788 if (sdinfo != NULL) { 17789 sdinfo->satadrv_event_flags &= 17790 ~SATA_EVNT_INPROC_DEVICE_RESET; 17791 sdinfo->satadrv_event_flags |= 17792 SATA_EVNT_CLEAR_DEVICE_RESET; 17793 } 17794 return; 17795 } 17796 17797 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 17798 if (sdinfo == NULL) { 17799 return; 17800 } 17801 if ((sdinfo->satadrv_event_flags & 17802 SATA_EVNT_INPROC_DEVICE_RESET) == 0) { 17803 /* 17804 * Start tracking time for device feature restoration and 17805 * identification. Save current time (lbolt value). 17806 */ 17807 sdinfo->satadrv_reset_time = ddi_get_lbolt(); 17808 } 17809 /* Mark device reset processing as active */ 17810 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET; 17811 17812 old_sdinfo = *sdinfo; /* local copy of the drive info */ 17813 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17814 17815 rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1); 17816 17817 if (rval_set != SATA_SUCCESS) { 17818 /* 17819 * Restoring drive setting failed. 17820 * Probe the port first, to check if the port state has changed 17821 */ 17822 sata_device.satadev_rev = SATA_DEVICE_REV; 17823 sata_device.satadev_addr = *saddr; 17824 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 17825 /* probe port */ 17826 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 17827 (SATA_DIP(sata_hba_inst), &sata_device); 17828 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 17829 cport_mutex); 17830 if (rval_probe == SATA_SUCCESS && 17831 (sata_device.satadev_state & 17832 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 && 17833 (sata_device.satadev_scr.sstatus & 17834 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP && 17835 sata_device.satadev_type != SATA_DTYPE_NONE) { 17836 /* 17837 * We may retry this a bit later - in-process reset 17838 * condition should be already set. 17839 * Track retry time for device identification. 17840 */ 17841 if ((cportinfo->cport_dev_type & 17842 SATA_VALID_DEV_TYPE) != 0 && 17843 SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL && 17844 sdinfo->satadrv_reset_time != 0) { 17845 clock_t cur_time = ddi_get_lbolt(); 17846 /* 17847 * If the retry time limit was not 17848 * exceeded, retry. 17849 */ 17850 if ((cur_time - sdinfo->satadrv_reset_time) < 17851 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 17852 mutex_enter( 17853 &sata_hba_inst->satahba_mutex); 17854 sata_hba_inst->satahba_event_flags |= 17855 SATA_EVNT_MAIN; 17856 mutex_exit( 17857 &sata_hba_inst->satahba_mutex); 17858 mutex_enter(&sata_mutex); 17859 sata_event_pending |= SATA_EVNT_MAIN; 17860 mutex_exit(&sata_mutex); 17861 return; 17862 } 17863 if (rval_set == SATA_RETRY) { 17864 /* 17865 * Setting drive features failed, but 17866 * the drive is still accessible, 17867 * so emit a warning message before 17868 * return. 17869 */ 17870 mutex_exit(&SATA_CPORT_INFO( 17871 sata_hba_inst, 17872 saddr->cport)->cport_mutex); 17873 goto done; 17874 } 17875 } 17876 /* Fail the drive */ 17877 sdinfo->satadrv_state = SATA_DSTATE_FAILED; 17878 17879 sata_log(sata_hba_inst, CE_WARN, 17880 "SATA device at port %d - device failed", 17881 saddr->cport); 17882 } 17883 /* 17884 * No point of retrying - device failed or some other event 17885 * processing or already did or will do port info cleanup. 17886 * To be safe (HBA may need it), 17887 * request clearing device reset condition. 17888 */ 17889 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 17890 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET; 17891 sdinfo->satadrv_reset_time = 0; 17892 return; 17893 } 17894 done: 17895 /* 17896 * If setting of drive features failed, but the drive is still 17897 * accessible, emit a warning message. 17898 */ 17899 if (rval_set == SATA_RETRY) { 17900 sata_log(sata_hba_inst, CE_WARN, 17901 "SATA device at port %d - desired setting could not be " 17902 "restored after reset. Device may not operate as expected.", 17903 saddr->cport); 17904 } 17905 /* 17906 * Raise the flag indicating that the next sata command could 17907 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device 17908 * reset is reported. 17909 */ 17910 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17911 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 17912 sdinfo->satadrv_reset_time = 0; 17913 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) { 17914 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 17915 sdinfo->satadrv_event_flags &= 17916 ~SATA_EVNT_INPROC_DEVICE_RESET; 17917 sdinfo->satadrv_event_flags |= 17918 SATA_EVNT_CLEAR_DEVICE_RESET; 17919 } 17920 } 17921 } 17922 17923 17924 /* 17925 * Port Multiplier Port Device Reset Event processing. 17926 * 17927 * NOTE: This function has to be entered with pmport mutex held. It exits with 17928 * mutex held as well, but can release mutex during the processing. 17929 */ 17930 static void 17931 sata_process_pmdevice_reset(sata_hba_inst_t *sata_hba_inst, 17932 sata_address_t *saddr) 17933 { 17934 sata_drive_info_t old_sdinfo; /* local copy of the drive info */ 17935 sata_drive_info_t *sdinfo = NULL; 17936 sata_cport_info_t *cportinfo = NULL; 17937 sata_pmport_info_t *pmportinfo = NULL; 17938 sata_pmult_info_t *pminfo = NULL; 17939 sata_device_t sata_device; 17940 uint8_t cport = saddr->cport; 17941 uint8_t pmport = saddr->pmport; 17942 int rval; 17943 17944 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17945 "Processing drive reset at port %d:%d", cport, pmport); 17946 17947 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 17948 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 17949 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport); 17950 17951 /* 17952 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED 17953 * state, ignore reset event. 17954 */ 17955 if (((cportinfo->cport_state & 17956 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) || 17957 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 17958 sdinfo->satadrv_event_flags &= 17959 ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET); 17960 return; 17961 } 17962 17963 if ((pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) == 0) { 17964 /* 17965 * This should not happen - coding error. 17966 * But we can recover, so do not panic, just clean up 17967 * and if in debug mode, log the message. 17968 */ 17969 #ifdef SATA_DEBUG 17970 sata_log(sata_hba_inst, CE_WARN, 17971 "sata_process_pmdevice_reset: " 17972 "Invalid device type with sdinfo!", NULL); 17973 #endif 17974 sdinfo->satadrv_event_flags = 0; 17975 return; 17976 } 17977 17978 #ifdef SATA_DEBUG 17979 if ((sdinfo->satadrv_event_flags & 17980 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) { 17981 /* Nothing to do */ 17982 /* Something is weird - why we are processing dev reset? */ 17983 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17984 "No device reset event!!!!", NULL); 17985 17986 return; 17987 } 17988 if ((sdinfo->satadrv_event_flags & 17989 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 17990 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 17991 /* Something is weird - new device reset event */ 17992 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17993 "Overlapping device reset events!", NULL); 17994 } 17995 #endif 17996 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17997 "Processing port %d:%d device reset", cport, pmport); 17998 17999 /* Clear event flag */ 18000 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET; 18001 18002 /* It seems that we always need to check the port state first */ 18003 sata_device.satadev_rev = SATA_DEVICE_REV; 18004 sata_device.satadev_addr = *saddr; 18005 /* 18006 * We have to exit mutex, because the HBA probe port function may 18007 * block on its own mutex. 18008 */ 18009 mutex_exit(&pmportinfo->pmport_mutex); 18010 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18011 (SATA_DIP(sata_hba_inst), &sata_device); 18012 mutex_enter(&pmportinfo->pmport_mutex); 18013 18014 sata_update_pmport_info(sata_hba_inst, &sata_device); 18015 if (rval != SATA_SUCCESS) { 18016 /* Something went wrong? Fail the port */ 18017 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 18018 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport, 18019 saddr->pmport); 18020 if (sdinfo != NULL) 18021 sdinfo->satadrv_event_flags = 0; 18022 mutex_exit(&pmportinfo->pmport_mutex); 18023 SATA_LOG_D((sata_hba_inst, CE_WARN, 18024 "SATA port %d:%d probing failed", 18025 saddr->cport, saddr->pmport)); 18026 mutex_enter(&pmportinfo->pmport_mutex); 18027 return; 18028 } 18029 if ((sata_device.satadev_scr.sstatus & 18030 SATA_PORT_DEVLINK_UP_MASK) != 18031 SATA_PORT_DEVLINK_UP || 18032 sata_device.satadev_type == SATA_DTYPE_NONE) { 18033 /* 18034 * No device to process, anymore. Some other event processing 18035 * would or have already performed port info cleanup. 18036 * To be safe (HBA may need it), request clearing device 18037 * reset condition. 18038 */ 18039 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport, 18040 saddr->pmport); 18041 if (sdinfo != NULL) { 18042 sdinfo->satadrv_event_flags &= 18043 ~SATA_EVNT_INPROC_DEVICE_RESET; 18044 /* must clear flags on cport */ 18045 pminfo = SATA_PMULT_INFO(sata_hba_inst, 18046 saddr->cport); 18047 pminfo->pmult_event_flags |= 18048 SATA_EVNT_CLEAR_DEVICE_RESET; 18049 } 18050 return; 18051 } 18052 18053 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport, 18054 saddr->pmport); 18055 if (sdinfo == NULL) { 18056 return; 18057 } 18058 if ((sdinfo->satadrv_event_flags & 18059 SATA_EVNT_INPROC_DEVICE_RESET) == 0) { 18060 /* 18061 * Start tracking time for device feature restoration and 18062 * identification. Save current time (lbolt value). 18063 */ 18064 sdinfo->satadrv_reset_time = ddi_get_lbolt(); 18065 } 18066 /* Mark device reset processing as active */ 18067 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET; 18068 18069 old_sdinfo = *sdinfo; /* local copy of the drive info */ 18070 mutex_exit(&pmportinfo->pmport_mutex); 18071 18072 if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) == 18073 SATA_FAILURE) { 18074 /* 18075 * Restoring drive setting failed. 18076 * Probe the port first, to check if the port state has changed 18077 */ 18078 sata_device.satadev_rev = SATA_DEVICE_REV; 18079 sata_device.satadev_addr = *saddr; 18080 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT; 18081 18082 /* probe port */ 18083 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18084 (SATA_DIP(sata_hba_inst), &sata_device); 18085 mutex_enter(&pmportinfo->pmport_mutex); 18086 if (rval == SATA_SUCCESS && 18087 (sata_device.satadev_state & 18088 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 && 18089 (sata_device.satadev_scr.sstatus & 18090 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP && 18091 sata_device.satadev_type != SATA_DTYPE_NONE) { 18092 /* 18093 * We may retry this a bit later - in-process reset 18094 * condition should be already set. 18095 * Track retry time for device identification. 18096 */ 18097 if ((pmportinfo->pmport_dev_type & 18098 SATA_VALID_DEV_TYPE) != 0 && 18099 SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL && 18100 sdinfo->satadrv_reset_time != 0) { 18101 clock_t cur_time = ddi_get_lbolt(); 18102 /* 18103 * If the retry time limit was not 18104 * exceeded, retry. 18105 */ 18106 if ((cur_time - sdinfo->satadrv_reset_time) < 18107 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 18108 mutex_enter( 18109 &sata_hba_inst->satahba_mutex); 18110 sata_hba_inst->satahba_event_flags |= 18111 SATA_EVNT_MAIN; 18112 mutex_exit( 18113 &sata_hba_inst->satahba_mutex); 18114 mutex_enter(&sata_mutex); 18115 sata_event_pending |= SATA_EVNT_MAIN; 18116 mutex_exit(&sata_mutex); 18117 return; 18118 } 18119 } 18120 /* Fail the drive */ 18121 sdinfo->satadrv_state = SATA_DSTATE_FAILED; 18122 18123 sata_log(sata_hba_inst, CE_WARN, 18124 "SATA device at port %d:%d - device failed", 18125 saddr->cport, saddr->pmport); 18126 } else { 18127 /* 18128 * No point of retrying - some other event processing 18129 * would or already did port info cleanup. 18130 * To be safe (HBA may need it), 18131 * request clearing device reset condition. 18132 */ 18133 sdinfo->satadrv_event_flags |= 18134 SATA_EVNT_CLEAR_DEVICE_RESET; 18135 } 18136 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET; 18137 sdinfo->satadrv_reset_time = 0; 18138 return; 18139 } 18140 /* 18141 * Raise the flag indicating that the next sata command could 18142 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device 18143 * reset is reported. 18144 */ 18145 mutex_enter(&pmportinfo->pmport_mutex); 18146 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 18147 sdinfo->satadrv_reset_time = 0; 18148 if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) { 18149 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 18150 sdinfo->satadrv_event_flags &= 18151 ~SATA_EVNT_INPROC_DEVICE_RESET; 18152 /* must clear flags on cport */ 18153 pminfo = SATA_PMULT_INFO(sata_hba_inst, 18154 saddr->cport); 18155 pminfo->pmult_event_flags |= 18156 SATA_EVNT_CLEAR_DEVICE_RESET; 18157 } 18158 } 18159 } 18160 18161 /* 18162 * Port Link Events processing. 18163 * Every link established event may involve device reset (due to 18164 * COMRESET signal, equivalent of the hard reset) so arbitrarily 18165 * set device reset event for an attached device (if any). 18166 * If the port is in SHUTDOWN or FAILED state, ignore link events. 18167 * 18168 * The link established event processing varies, depending on the state 18169 * of the target node, HBA hotplugging capabilities, state of the port. 18170 * If the link is not active, the link established event is ignored. 18171 * If HBA cannot detect device attachment and there is no target node, 18172 * the link established event triggers device attach event processing. 18173 * Else, link established event triggers device reset event processing. 18174 * 18175 * The link lost event processing varies, depending on a HBA hotplugging 18176 * capability and the state of the port (link active or not active). 18177 * If the link is active, the lost link event is ignored. 18178 * If HBA cannot detect device removal, the lost link event triggers 18179 * device detached event processing after link lost timeout. 18180 * Else, the event is ignored. 18181 * 18182 * NOTE: Port multiplier ports events are handled by 18183 * sata_process_pmport_link_events(); 18184 */ 18185 static void 18186 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst, 18187 sata_address_t *saddr) 18188 { 18189 sata_device_t sata_device; 18190 sata_cport_info_t *cportinfo; 18191 sata_drive_info_t *sdinfo; 18192 uint32_t event_flags; 18193 int rval; 18194 18195 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18196 "Processing port %d link event(s)", saddr->cport); 18197 18198 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 18199 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18200 event_flags = cportinfo->cport_event_flags; 18201 18202 /* Reset event flags first */ 18203 cportinfo->cport_event_flags &= 18204 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST); 18205 18206 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */ 18207 if ((cportinfo->cport_state & 18208 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 18209 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18210 cport_mutex); 18211 return; 18212 } 18213 18214 /* 18215 * For the sanity sake get current port state. 18216 * Set device address only. Other sata_device fields should be 18217 * set by HBA driver. 18218 */ 18219 sata_device.satadev_rev = SATA_DEVICE_REV; 18220 sata_device.satadev_addr = *saddr; 18221 /* 18222 * We have to exit mutex, because the HBA probe port function may 18223 * block on its own mutex. 18224 */ 18225 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18226 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18227 (SATA_DIP(sata_hba_inst), &sata_device); 18228 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18229 sata_update_port_info(sata_hba_inst, &sata_device); 18230 if (rval != SATA_SUCCESS) { 18231 /* Something went wrong? Fail the port */ 18232 cportinfo->cport_state = SATA_PSTATE_FAILED; 18233 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18234 cport_mutex); 18235 SATA_LOG_D((sata_hba_inst, CE_WARN, 18236 "SATA port %d probing failed", 18237 saddr->cport)); 18238 /* 18239 * We may want to release device info structure, but 18240 * it is not necessary. 18241 */ 18242 return; 18243 } else { 18244 /* port probed successfully */ 18245 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 18246 } 18247 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) { 18248 18249 if ((sata_device.satadev_scr.sstatus & 18250 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) { 18251 /* Ignore event */ 18252 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18253 "Ignoring port %d link established event - " 18254 "link down", 18255 saddr->cport); 18256 goto linklost; 18257 } 18258 18259 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18260 "Processing port %d link established event", 18261 saddr->cport); 18262 18263 /* 18264 * For the sanity sake check if a device is attached - check 18265 * return state of a port probing. 18266 */ 18267 if (sata_device.satadev_type != SATA_DTYPE_NONE) { 18268 /* 18269 * HBA port probe indicated that there is a device 18270 * attached. Check if the framework had device info 18271 * structure attached for this device. 18272 */ 18273 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 18274 ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) != 18275 NULL); 18276 18277 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 18278 if ((sdinfo->satadrv_type & 18279 SATA_VALID_DEV_TYPE) != 0) { 18280 /* 18281 * Dev info structure is present. 18282 * If dev_type is set to known type in 18283 * the framework's drive info struct 18284 * then the device existed before and 18285 * the link was probably lost 18286 * momentarily - in such case 18287 * we may want to check device 18288 * identity. 18289 * Identity check is not supported now. 18290 * 18291 * Link established event 18292 * triggers device reset event. 18293 */ 18294 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 18295 satadrv_event_flags |= 18296 SATA_EVNT_DEVICE_RESET; 18297 } 18298 } else if (cportinfo->cport_dev_type == 18299 SATA_DTYPE_NONE) { 18300 /* 18301 * We got new device attached! If HBA does not 18302 * generate device attached events, trigger it 18303 * here. 18304 */ 18305 if (!(SATA_FEATURES(sata_hba_inst) & 18306 SATA_CTLF_HOTPLUG)) { 18307 cportinfo->cport_event_flags |= 18308 SATA_EVNT_DEVICE_ATTACHED; 18309 } 18310 } 18311 /* Reset link lost timeout */ 18312 cportinfo->cport_link_lost_time = 0; 18313 } 18314 } 18315 linklost: 18316 if (event_flags & SATA_EVNT_LINK_LOST) { 18317 if ((sata_device.satadev_scr.sstatus & 18318 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) { 18319 /* Ignore event */ 18320 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18321 "Ignoring port %d link lost event - link is up", 18322 saddr->cport); 18323 goto done; 18324 } 18325 #ifdef SATA_DEBUG 18326 if (cportinfo->cport_link_lost_time == 0) { 18327 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18328 "Processing port %d link lost event", 18329 saddr->cport); 18330 } 18331 #endif 18332 /* 18333 * When HBA cannot generate device attached/detached events, 18334 * we need to track link lost time and eventually generate 18335 * device detach event. 18336 */ 18337 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) { 18338 /* We are tracking link lost time */ 18339 if (cportinfo->cport_link_lost_time == 0) { 18340 /* save current time (lbolt value) */ 18341 cportinfo->cport_link_lost_time = 18342 ddi_get_lbolt(); 18343 /* just keep link lost event */ 18344 cportinfo->cport_event_flags |= 18345 SATA_EVNT_LINK_LOST; 18346 } else { 18347 clock_t cur_time = ddi_get_lbolt(); 18348 if ((cur_time - 18349 cportinfo->cport_link_lost_time) >= 18350 drv_usectohz( 18351 SATA_EVNT_LINK_LOST_TIMEOUT)) { 18352 /* trigger device detach event */ 18353 cportinfo->cport_event_flags |= 18354 SATA_EVNT_DEVICE_DETACHED; 18355 cportinfo->cport_link_lost_time = 0; 18356 SATADBG1(SATA_DBG_EVENTS, 18357 sata_hba_inst, 18358 "Triggering port %d " 18359 "device detached event", 18360 saddr->cport); 18361 } else { 18362 /* keep link lost event */ 18363 cportinfo->cport_event_flags |= 18364 SATA_EVNT_LINK_LOST; 18365 } 18366 } 18367 } 18368 /* 18369 * We could change port state to disable/delay access to 18370 * the attached device until the link is recovered. 18371 */ 18372 } 18373 done: 18374 event_flags = cportinfo->cport_event_flags; 18375 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18376 if (event_flags != 0) { 18377 mutex_enter(&sata_hba_inst->satahba_mutex); 18378 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 18379 mutex_exit(&sata_hba_inst->satahba_mutex); 18380 mutex_enter(&sata_mutex); 18381 sata_event_pending |= SATA_EVNT_MAIN; 18382 mutex_exit(&sata_mutex); 18383 } 18384 } 18385 18386 /* 18387 * Port Multiplier Port Link Events processing. 18388 */ 18389 static void 18390 sata_process_pmport_link_events(sata_hba_inst_t *sata_hba_inst, 18391 sata_address_t *saddr) 18392 { 18393 sata_device_t sata_device; 18394 sata_pmport_info_t *pmportinfo = NULL; 18395 sata_drive_info_t *sdinfo = NULL; 18396 uint32_t event_flags; 18397 uint8_t cport = saddr->cport; 18398 uint8_t pmport = saddr->pmport; 18399 int rval; 18400 18401 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18402 "Processing port %d:%d link event(s)", 18403 cport, pmport); 18404 18405 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 18406 mutex_enter(&pmportinfo->pmport_mutex); 18407 event_flags = pmportinfo->pmport_event_flags; 18408 18409 /* Reset event flags first */ 18410 pmportinfo->pmport_event_flags &= 18411 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST); 18412 18413 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */ 18414 if ((pmportinfo->pmport_state & 18415 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 18416 mutex_exit(&pmportinfo->pmport_mutex); 18417 return; 18418 } 18419 18420 /* 18421 * For the sanity sake get current port state. 18422 * Set device address only. Other sata_device fields should be 18423 * set by HBA driver. 18424 */ 18425 sata_device.satadev_rev = SATA_DEVICE_REV; 18426 sata_device.satadev_addr = *saddr; 18427 /* 18428 * We have to exit mutex, because the HBA probe port function may 18429 * block on its own mutex. 18430 */ 18431 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 18432 saddr->pmport)); 18433 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18434 (SATA_DIP(sata_hba_inst), &sata_device); 18435 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 18436 saddr->pmport)); 18437 sata_update_pmport_info(sata_hba_inst, &sata_device); 18438 if (rval != SATA_SUCCESS) { 18439 /* Something went wrong? Fail the port */ 18440 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 18441 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 18442 saddr->pmport)); 18443 SATA_LOG_D((sata_hba_inst, CE_WARN, 18444 "SATA port %d:%d probing failed", 18445 saddr->cport, saddr->pmport)); 18446 /* 18447 * We may want to release device info structure, but 18448 * it is not necessary. 18449 */ 18450 return; 18451 } else { 18452 /* port probed successfully */ 18453 pmportinfo->pmport_state |= 18454 SATA_STATE_PROBED | SATA_STATE_READY; 18455 } 18456 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, 18457 saddr->cport, saddr->pmport)); 18458 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, 18459 saddr->cport, saddr->pmport)); 18460 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) { 18461 18462 if ((sata_device.satadev_scr.sstatus & 18463 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) { 18464 /* Ignore event */ 18465 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18466 "Ignoring port %d:%d link established event - " 18467 "link down", 18468 saddr->cport, saddr->pmport); 18469 goto linklost; 18470 } 18471 18472 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18473 "Processing port %d:%d link established event", 18474 cport, pmport); 18475 18476 /* 18477 * For the sanity sake check if a device is attached - check 18478 * return state of a port probing. 18479 */ 18480 if (sata_device.satadev_type != SATA_DTYPE_NONE && 18481 sata_device.satadev_type != SATA_DTYPE_PMULT) { 18482 /* 18483 * HBA port probe indicated that there is a device 18484 * attached. Check if the framework had device info 18485 * structure attached for this device. 18486 */ 18487 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 18488 ASSERT(SATA_PMPORTINFO_DRV_INFO(pmportinfo) != 18489 NULL); 18490 18491 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 18492 if ((sdinfo->satadrv_type & 18493 SATA_VALID_DEV_TYPE) != 0) { 18494 /* 18495 * Dev info structure is present. 18496 * If dev_type is set to known type in 18497 * the framework's drive info struct 18498 * then the device existed before and 18499 * the link was probably lost 18500 * momentarily - in such case 18501 * we may want to check device 18502 * identity. 18503 * Identity check is not supported now. 18504 * 18505 * Link established event 18506 * triggers device reset event. 18507 */ 18508 (SATA_PMPORTINFO_DRV_INFO(pmportinfo))-> 18509 satadrv_event_flags |= 18510 SATA_EVNT_DEVICE_RESET; 18511 } 18512 } else if (pmportinfo->pmport_dev_type == 18513 SATA_DTYPE_NONE) { 18514 /* 18515 * We got new device attached! If HBA does not 18516 * generate device attached events, trigger it 18517 * here. 18518 */ 18519 if (!(SATA_FEATURES(sata_hba_inst) & 18520 SATA_CTLF_HOTPLUG)) { 18521 pmportinfo->pmport_event_flags |= 18522 SATA_EVNT_DEVICE_ATTACHED; 18523 } 18524 } 18525 /* Reset link lost timeout */ 18526 pmportinfo->pmport_link_lost_time = 0; 18527 } 18528 } 18529 linklost: 18530 if (event_flags & SATA_EVNT_LINK_LOST) { 18531 #ifdef SATA_DEBUG 18532 if (pmportinfo->pmport_link_lost_time == 0) { 18533 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18534 "Processing port %d:%d link lost event", 18535 saddr->cport, saddr->pmport); 18536 } 18537 #endif 18538 if ((sata_device.satadev_scr.sstatus & 18539 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) { 18540 /* Ignore event */ 18541 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18542 "Ignoring port %d:%d link lost event - link is up", 18543 saddr->cport, saddr->pmport); 18544 goto done; 18545 } 18546 /* 18547 * When HBA cannot generate device attached/detached events, 18548 * we need to track link lost time and eventually generate 18549 * device detach event. 18550 */ 18551 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) { 18552 /* We are tracking link lost time */ 18553 if (pmportinfo->pmport_link_lost_time == 0) { 18554 /* save current time (lbolt value) */ 18555 pmportinfo->pmport_link_lost_time = 18556 ddi_get_lbolt(); 18557 /* just keep link lost event */ 18558 pmportinfo->pmport_event_flags |= 18559 SATA_EVNT_LINK_LOST; 18560 } else { 18561 clock_t cur_time = ddi_get_lbolt(); 18562 if ((cur_time - 18563 pmportinfo->pmport_link_lost_time) >= 18564 drv_usectohz( 18565 SATA_EVNT_LINK_LOST_TIMEOUT)) { 18566 /* trigger device detach event */ 18567 pmportinfo->pmport_event_flags |= 18568 SATA_EVNT_DEVICE_DETACHED; 18569 pmportinfo->pmport_link_lost_time = 0; 18570 SATADBG2(SATA_DBG_EVENTS, 18571 sata_hba_inst, 18572 "Triggering port %d:%d " 18573 "device detached event", 18574 saddr->cport, saddr->pmport); 18575 } else { 18576 /* keep link lost event */ 18577 pmportinfo->pmport_event_flags |= 18578 SATA_EVNT_LINK_LOST; 18579 } 18580 } 18581 } 18582 /* 18583 * We could change port state to disable/delay access to 18584 * the attached device until the link is recovered. 18585 */ 18586 } 18587 done: 18588 event_flags = pmportinfo->pmport_event_flags; 18589 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 18590 saddr->pmport)); 18591 if (event_flags != 0) { 18592 mutex_enter(&sata_hba_inst->satahba_mutex); 18593 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 18594 mutex_exit(&sata_hba_inst->satahba_mutex); 18595 mutex_enter(&sata_mutex); 18596 sata_event_pending |= SATA_EVNT_MAIN; 18597 mutex_exit(&sata_mutex); 18598 } 18599 } 18600 18601 /* 18602 * Device Detached Event processing. 18603 * Port is probed to find if a device is really gone. If so, 18604 * the device info structure is detached from the SATA port info structure 18605 * and released. 18606 * Port status is updated. 18607 * 18608 * NOTE: Port multiplier ports events are handled by 18609 * sata_process_pmdevice_detached() 18610 */ 18611 static void 18612 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst, 18613 sata_address_t *saddr) 18614 { 18615 sata_cport_info_t *cportinfo; 18616 sata_pmport_info_t *pmportinfo; 18617 sata_drive_info_t *sdevinfo; 18618 sata_device_t sata_device; 18619 sata_address_t pmport_addr; 18620 char name[16]; 18621 uint8_t cport = saddr->cport; 18622 int npmport; 18623 int rval; 18624 18625 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18626 "Processing port %d device detached", saddr->cport); 18627 18628 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 18629 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18630 /* Clear event flag */ 18631 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED; 18632 18633 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */ 18634 if ((cportinfo->cport_state & 18635 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 18636 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18637 cport_mutex); 18638 return; 18639 } 18640 /* For sanity, re-probe the port */ 18641 sata_device.satadev_rev = SATA_DEVICE_REV; 18642 sata_device.satadev_addr = *saddr; 18643 18644 /* 18645 * We have to exit mutex, because the HBA probe port function may 18646 * block on its own mutex. 18647 */ 18648 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18649 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18650 (SATA_DIP(sata_hba_inst), &sata_device); 18651 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18652 sata_update_port_info(sata_hba_inst, &sata_device); 18653 if (rval != SATA_SUCCESS) { 18654 /* Something went wrong? Fail the port */ 18655 cportinfo->cport_state = SATA_PSTATE_FAILED; 18656 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18657 cport_mutex); 18658 SATA_LOG_D((sata_hba_inst, CE_WARN, 18659 "SATA port %d probing failed", 18660 saddr->cport)); 18661 /* 18662 * We may want to release device info structure, but 18663 * it is not necessary. 18664 */ 18665 return; 18666 } else { 18667 /* port probed successfully */ 18668 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 18669 } 18670 /* 18671 * Check if a device is still attached. For sanity, check also 18672 * link status - if no link, there is no device. 18673 */ 18674 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) == 18675 SATA_PORT_DEVLINK_UP && sata_device.satadev_type != 18676 SATA_DTYPE_NONE) { 18677 /* 18678 * Device is still attached - ignore detach event. 18679 */ 18680 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18681 cport_mutex); 18682 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18683 "Ignoring detach - device still attached to port %d", 18684 sata_device.satadev_addr.cport); 18685 return; 18686 } 18687 /* 18688 * We need to detach and release device info structure here 18689 */ 18690 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 18691 /* 18692 * A port-multiplier is removed. 18693 * 18694 * Calling sata_process_pmdevice_detached() does not work 18695 * here. The port multiplier is gone, so we cannot probe 18696 * sub-port any more and all pmult-related data structure must 18697 * be de-allocated immediately. Following structure of every 18698 * implemented sub-port behind the pmult are required to 18699 * released. 18700 * 18701 * - attachment point 18702 * - target node 18703 * - sata_drive_info 18704 * - sata_pmport_info 18705 */ 18706 for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, 18707 cport); npmport ++) { 18708 SATADBG2(SATA_DBG_PMULT|SATA_DBG_EVENTS_PROC, 18709 sata_hba_inst, 18710 "Detaching target node at port %d:%d", 18711 cport, npmport); 18712 18713 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 18714 18715 /* Remove attachment point. */ 18716 name[0] = '\0'; 18717 (void) sprintf(name, "%d.%d", cport, npmport); 18718 ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name); 18719 sata_log(sata_hba_inst, CE_NOTE, 18720 "Remove attachment point of port %d:%d", 18721 cport, npmport); 18722 18723 /* Remove target node */ 18724 pmport_addr.cport = cport; 18725 pmport_addr.pmport = (uint8_t)npmport; 18726 pmport_addr.qual = SATA_ADDR_PMPORT; 18727 sata_remove_target_node(sata_hba_inst, &pmport_addr); 18728 18729 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 18730 18731 /* Release sata_pmport_info & sata_drive_info. */ 18732 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 18733 cport, npmport); 18734 ASSERT(pmportinfo != NULL); 18735 18736 sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 18737 if (sdevinfo != NULL) { 18738 (void) kmem_free((void *) sdevinfo, 18739 sizeof (sata_drive_info_t)); 18740 } 18741 18742 /* Release sata_pmport_info at last */ 18743 (void) kmem_free((void *) pmportinfo, 18744 sizeof (sata_pmport_info_t)); 18745 } 18746 18747 /* Finally, release sata_pmult_info */ 18748 (void) kmem_free((void *) 18749 SATA_CPORTINFO_PMULT_INFO(cportinfo), 18750 sizeof (sata_pmult_info_t)); 18751 SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL; 18752 18753 sata_log(sata_hba_inst, CE_WARN, 18754 "SATA port-multiplier detached at port %d", cport); 18755 18756 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 18757 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 18758 saddr->cport)->cport_mutex); 18759 } else { 18760 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 18761 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 18762 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 18763 (void) kmem_free((void *)sdevinfo, 18764 sizeof (sata_drive_info_t)); 18765 } 18766 sata_log(sata_hba_inst, CE_WARN, 18767 "SATA device detached at port %d", cport); 18768 18769 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 18770 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 18771 saddr->cport)->cport_mutex); 18772 18773 /* 18774 * Try to offline a device and remove target node 18775 * if it still exists 18776 */ 18777 sata_remove_target_node(sata_hba_inst, saddr); 18778 } 18779 18780 18781 /* 18782 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 18783 * with the hint: SE_HINT_REMOVE 18784 */ 18785 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE); 18786 } 18787 18788 /* 18789 * Port Multiplier Port Device Deattached Event processing. 18790 * 18791 * NOTE: No Mutex should be hold. 18792 */ 18793 static void 18794 sata_process_pmdevice_detached(sata_hba_inst_t *sata_hba_inst, 18795 sata_address_t *saddr) 18796 { 18797 sata_pmport_info_t *pmportinfo; 18798 sata_drive_info_t *sdevinfo; 18799 sata_device_t sata_device; 18800 int rval; 18801 uint8_t cport, pmport; 18802 18803 cport = saddr->cport; 18804 pmport = saddr->pmport; 18805 18806 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18807 "Processing port %d:%d device detached", 18808 cport, pmport); 18809 18810 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 18811 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18812 18813 /* Clear event flag */ 18814 pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED; 18815 18816 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */ 18817 if ((pmportinfo->pmport_state & 18818 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 18819 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18820 return; 18821 } 18822 /* For sanity, re-probe the port */ 18823 sata_device.satadev_rev = SATA_DEVICE_REV; 18824 sata_device.satadev_addr = *saddr; 18825 18826 /* 18827 * We have to exit mutex, because the HBA probe port function may 18828 * block on its own mutex. 18829 */ 18830 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18831 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18832 (SATA_DIP(sata_hba_inst), &sata_device); 18833 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18834 sata_update_pmport_info(sata_hba_inst, &sata_device); 18835 if (rval != SATA_SUCCESS) { 18836 /* Something went wrong? Fail the port */ 18837 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 18838 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18839 SATA_LOG_D((sata_hba_inst, CE_WARN, 18840 "SATA port %d:%d probing failed", 18841 saddr->pmport)); 18842 /* 18843 * We may want to release device info structure, but 18844 * it is not necessary. 18845 */ 18846 return; 18847 } else { 18848 /* port probed successfully */ 18849 pmportinfo->pmport_state |= 18850 SATA_STATE_PROBED | SATA_STATE_READY; 18851 } 18852 /* 18853 * Check if a device is still attached. For sanity, check also 18854 * link status - if no link, there is no device. 18855 */ 18856 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) == 18857 SATA_PORT_DEVLINK_UP && sata_device.satadev_type != 18858 SATA_DTYPE_NONE) { 18859 /* 18860 * Device is still attached - ignore detach event. 18861 */ 18862 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18863 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18864 "Ignoring detach - device still attached to port %d", 18865 sata_device.satadev_addr.pmport); 18866 return; 18867 } 18868 /* 18869 * We need to detach and release device info structure here 18870 */ 18871 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 18872 sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 18873 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 18874 (void) kmem_free((void *)sdevinfo, 18875 sizeof (sata_drive_info_t)); 18876 } 18877 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 18878 /* 18879 * Device cannot be reached anymore, even if the target node may be 18880 * still present. 18881 */ 18882 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18883 18884 /* 18885 * Try to offline a device and remove target node if it still exists 18886 */ 18887 sata_remove_target_node(sata_hba_inst, saddr); 18888 18889 /* 18890 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 18891 * with the hint: SE_HINT_REMOVE 18892 */ 18893 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE); 18894 } 18895 18896 18897 /* 18898 * Device Attached Event processing. 18899 * Port state is checked to verify that a device is really attached. If so, 18900 * the device info structure is created and attached to the SATA port info 18901 * structure. 18902 * 18903 * If attached device cannot be identified or set-up, the retry for the 18904 * attach processing is set-up. Subsequent daemon run would try again to 18905 * identify the device, until the time limit is reached 18906 * (SATA_DEV_IDENTIFY_TIMEOUT). 18907 * 18908 * This function cannot be called in interrupt context (it may sleep). 18909 * 18910 * NOTE: Port multiplier ports events are handled by 18911 * sata_process_pmdevice_attached() 18912 */ 18913 static void 18914 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst, 18915 sata_address_t *saddr) 18916 { 18917 sata_cport_info_t *cportinfo = NULL; 18918 sata_drive_info_t *sdevinfo = NULL; 18919 sata_pmult_info_t *pmultinfo = NULL; 18920 sata_pmport_info_t *pmportinfo = NULL; 18921 sata_device_t sata_device; 18922 dev_info_t *tdip; 18923 uint32_t event_flags = 0, pmult_event_flags = 0; 18924 int rval; 18925 int npmport; 18926 18927 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18928 "Processing port %d device attached", saddr->cport); 18929 18930 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 18931 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18932 18933 /* Clear attach event flag first */ 18934 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED; 18935 18936 /* If the port is in SHUTDOWN or FAILED state, ignore event. */ 18937 if ((cportinfo->cport_state & 18938 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 18939 cportinfo->cport_dev_attach_time = 0; 18940 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18941 cport_mutex); 18942 return; 18943 } 18944 18945 /* 18946 * If the sata_drive_info structure is found attached to the port info, 18947 * despite the fact the device was removed and now it is re-attached, 18948 * the old drive info structure was not removed. 18949 * Arbitrarily release device info structure. 18950 */ 18951 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 18952 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 18953 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 18954 (void) kmem_free((void *)sdevinfo, 18955 sizeof (sata_drive_info_t)); 18956 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18957 "Arbitrarily detaching old device info.", NULL); 18958 } 18959 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 18960 18961 /* For sanity, re-probe the port */ 18962 sata_device.satadev_rev = SATA_DEVICE_REV; 18963 sata_device.satadev_addr = *saddr; 18964 18965 /* 18966 * We have to exit mutex, because the HBA probe port function may 18967 * block on its own mutex. 18968 */ 18969 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18970 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18971 (SATA_DIP(sata_hba_inst), &sata_device); 18972 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18973 sata_update_port_info(sata_hba_inst, &sata_device); 18974 if (rval != SATA_SUCCESS) { 18975 /* Something went wrong? Fail the port */ 18976 cportinfo->cport_state = SATA_PSTATE_FAILED; 18977 cportinfo->cport_dev_attach_time = 0; 18978 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18979 cport_mutex); 18980 SATA_LOG_D((sata_hba_inst, CE_WARN, 18981 "SATA port %d probing failed", 18982 saddr->cport)); 18983 return; 18984 } else { 18985 /* port probed successfully */ 18986 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 18987 } 18988 /* 18989 * Check if a device is still attached. For sanity, check also 18990 * link status - if no link, there is no device. 18991 */ 18992 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 18993 SATA_PORT_DEVLINK_UP || sata_device.satadev_type == 18994 SATA_DTYPE_NONE) { 18995 /* 18996 * No device - ignore attach event. 18997 */ 18998 cportinfo->cport_dev_attach_time = 0; 18999 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19000 cport_mutex); 19001 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19002 "Ignoring attach - no device connected to port %d", 19003 sata_device.satadev_addr.cport); 19004 return; 19005 } 19006 19007 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19008 /* 19009 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 19010 * with the hint: SE_HINT_INSERT 19011 */ 19012 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT); 19013 19014 /* 19015 * Port reprobing will take care of the creation of the device 19016 * info structure and determination of the device type. 19017 */ 19018 sata_device.satadev_addr = *saddr; 19019 (void) sata_reprobe_port(sata_hba_inst, &sata_device, 19020 SATA_DEV_IDENTIFY_NORETRY); 19021 19022 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19023 cport_mutex); 19024 if ((cportinfo->cport_state & SATA_STATE_READY) && 19025 (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) { 19026 /* Some device is attached to the port */ 19027 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) { 19028 /* 19029 * A device was not successfully attached. 19030 * Track retry time for device identification. 19031 */ 19032 if (cportinfo->cport_dev_attach_time != 0) { 19033 clock_t cur_time = ddi_get_lbolt(); 19034 /* 19035 * If the retry time limit was not exceeded, 19036 * reinstate attach event. 19037 */ 19038 if ((cur_time - 19039 cportinfo->cport_dev_attach_time) < 19040 drv_usectohz( 19041 SATA_DEV_IDENTIFY_TIMEOUT)) { 19042 /* OK, restore attach event */ 19043 cportinfo->cport_event_flags |= 19044 SATA_EVNT_DEVICE_ATTACHED; 19045 } else { 19046 /* Timeout - cannot identify device */ 19047 cportinfo->cport_dev_attach_time = 0; 19048 sata_log(sata_hba_inst, 19049 CE_WARN, 19050 "Could not identify SATA device " 19051 "at port %d", 19052 saddr->cport); 19053 } 19054 } else { 19055 /* 19056 * Start tracking time for device 19057 * identification. 19058 * Save current time (lbolt value). 19059 */ 19060 cportinfo->cport_dev_attach_time = 19061 ddi_get_lbolt(); 19062 /* Restore attach event */ 19063 cportinfo->cport_event_flags |= 19064 SATA_EVNT_DEVICE_ATTACHED; 19065 } 19066 } else if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 19067 cportinfo->cport_dev_attach_time = 0; 19068 sata_log(sata_hba_inst, CE_NOTE, 19069 "SATA port-multiplier detected at port %d", 19070 saddr->cport); 19071 19072 if (SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL) { 19073 /* Log the info of new port multiplier */ 19074 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19075 saddr->cport)->cport_mutex); 19076 sata_show_pmult_info(sata_hba_inst, 19077 &sata_device); 19078 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 19079 saddr->cport)->cport_mutex); 19080 } 19081 19082 ASSERT(SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL); 19083 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 19084 for (npmport = 0; npmport < 19085 pmultinfo->pmult_num_dev_ports; npmport++) { 19086 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 19087 saddr->cport, npmport); 19088 ASSERT(pmportinfo != NULL); 19089 19090 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19091 saddr->cport)->cport_mutex); 19092 mutex_enter(&pmportinfo->pmport_mutex); 19093 /* Marked all pmports with link events. */ 19094 pmportinfo->pmport_event_flags = 19095 SATA_EVNT_LINK_ESTABLISHED; 19096 pmult_event_flags |= 19097 pmportinfo->pmport_event_flags; 19098 mutex_exit(&pmportinfo->pmport_mutex); 19099 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 19100 saddr->cport)->cport_mutex); 19101 } 19102 /* Auto-online is not available for PMult now. */ 19103 19104 } else { 19105 /* 19106 * If device was successfully attached, the subsequent 19107 * action depends on a state of the 19108 * sata_auto_online variable. If it is set to zero. 19109 * an explicit 'configure' command will be needed to 19110 * configure it. If its value is non-zero, we will 19111 * attempt to online (configure) the device. 19112 * First, log the message indicating that a device 19113 * was attached. 19114 */ 19115 cportinfo->cport_dev_attach_time = 0; 19116 sata_log(sata_hba_inst, CE_WARN, 19117 "SATA device detected at port %d", saddr->cport); 19118 19119 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 19120 sata_drive_info_t new_sdinfo; 19121 19122 /* Log device info data */ 19123 new_sdinfo = *(SATA_CPORTINFO_DRV_INFO( 19124 cportinfo)); 19125 sata_show_drive_info(sata_hba_inst, 19126 &new_sdinfo); 19127 } 19128 19129 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19130 saddr->cport)->cport_mutex); 19131 19132 /* 19133 * Make sure that there is no target node for that 19134 * device. If so, release it. It should not happen, 19135 * unless we had problem removing the node when 19136 * device was detached. 19137 */ 19138 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 19139 saddr->cport, saddr->pmport); 19140 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 19141 saddr->cport)->cport_mutex); 19142 if (tdip != NULL) { 19143 19144 #ifdef SATA_DEBUG 19145 if ((cportinfo->cport_event_flags & 19146 SATA_EVNT_TARGET_NODE_CLEANUP) == 0) 19147 sata_log(sata_hba_inst, CE_WARN, 19148 "sata_process_device_attached: " 19149 "old device target node exists!"); 19150 #endif 19151 /* 19152 * target node exists - try to unconfigure 19153 * device and remove the node. 19154 */ 19155 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19156 saddr->cport)->cport_mutex); 19157 rval = ndi_devi_offline(tdip, 19158 NDI_DEVI_REMOVE); 19159 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 19160 saddr->cport)->cport_mutex); 19161 19162 if (rval == NDI_SUCCESS) { 19163 cportinfo->cport_event_flags &= 19164 ~SATA_EVNT_TARGET_NODE_CLEANUP; 19165 cportinfo->cport_tgtnode_clean = B_TRUE; 19166 } else { 19167 /* 19168 * PROBLEM - the target node remained 19169 * and it belongs to a previously 19170 * attached device. 19171 * This happens when the file was open 19172 * or the node was waiting for 19173 * resources at the time the 19174 * associated device was removed. 19175 * Instruct event daemon to retry the 19176 * cleanup later. 19177 */ 19178 sata_log(sata_hba_inst, 19179 CE_WARN, 19180 "Application(s) accessing " 19181 "previously attached SATA " 19182 "device have to release " 19183 "it before newly inserted " 19184 "device can be made accessible.", 19185 saddr->cport); 19186 cportinfo->cport_event_flags |= 19187 SATA_EVNT_TARGET_NODE_CLEANUP; 19188 cportinfo->cport_tgtnode_clean = 19189 B_FALSE; 19190 } 19191 } 19192 if (sata_auto_online != 0) { 19193 cportinfo->cport_event_flags |= 19194 SATA_EVNT_AUTOONLINE_DEVICE; 19195 } 19196 19197 } 19198 } else { 19199 cportinfo->cport_dev_attach_time = 0; 19200 } 19201 19202 event_flags = cportinfo->cport_event_flags; 19203 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19204 if (event_flags != 0 || pmult_event_flags != 0) { 19205 mutex_enter(&sata_hba_inst->satahba_mutex); 19206 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 19207 mutex_exit(&sata_hba_inst->satahba_mutex); 19208 mutex_enter(&sata_mutex); 19209 sata_event_pending |= SATA_EVNT_MAIN; 19210 mutex_exit(&sata_mutex); 19211 } 19212 } 19213 19214 /* 19215 * Port Multiplier Port Device Attached Event processing. 19216 * 19217 * NOTE: No Mutex should be hold. 19218 */ 19219 static void 19220 sata_process_pmdevice_attached(sata_hba_inst_t *sata_hba_inst, 19221 sata_address_t *saddr) 19222 { 19223 sata_pmport_info_t *pmportinfo; 19224 sata_drive_info_t *sdinfo; 19225 sata_device_t sata_device; 19226 dev_info_t *tdip; 19227 uint32_t event_flags; 19228 uint8_t cport = saddr->cport; 19229 uint8_t pmport = saddr->pmport; 19230 int rval; 19231 19232 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19233 "Processing port %d:%d device attached", cport, pmport); 19234 19235 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 19236 19237 mutex_enter(&pmportinfo->pmport_mutex); 19238 19239 /* Clear attach event flag first */ 19240 pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED; 19241 19242 /* If the port is in SHUTDOWN or FAILED state, ignore event. */ 19243 if ((pmportinfo->pmport_state & 19244 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 19245 pmportinfo->pmport_dev_attach_time = 0; 19246 mutex_exit(&pmportinfo->pmport_mutex); 19247 return; 19248 } 19249 19250 /* 19251 * If the sata_drive_info structure is found attached to the port info, 19252 * despite the fact the device was removed and now it is re-attached, 19253 * the old drive info structure was not removed. 19254 * Arbitrarily release device info structure. 19255 */ 19256 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 19257 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 19258 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 19259 (void) kmem_free((void *)sdinfo, 19260 sizeof (sata_drive_info_t)); 19261 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19262 "Arbitrarily detaching old device info.", NULL); 19263 } 19264 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 19265 19266 /* For sanity, re-probe the port */ 19267 sata_device.satadev_rev = SATA_DEVICE_REV; 19268 sata_device.satadev_addr = *saddr; 19269 19270 /* 19271 * We have to exit mutex, because the HBA probe port function may 19272 * block on its own mutex. 19273 */ 19274 mutex_exit(&pmportinfo->pmport_mutex); 19275 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 19276 (SATA_DIP(sata_hba_inst), &sata_device); 19277 mutex_enter(&pmportinfo->pmport_mutex); 19278 19279 sata_update_pmport_info(sata_hba_inst, &sata_device); 19280 if (rval != SATA_SUCCESS) { 19281 /* Something went wrong? Fail the port */ 19282 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 19283 pmportinfo->pmport_dev_attach_time = 0; 19284 mutex_exit(&pmportinfo->pmport_mutex); 19285 SATA_LOG_D((sata_hba_inst, CE_WARN, 19286 "SATA port %d:%d probing failed", cport, pmport)); 19287 return; 19288 } else { 19289 /* pmport probed successfully */ 19290 pmportinfo->pmport_state |= 19291 SATA_STATE_PROBED | SATA_STATE_READY; 19292 } 19293 /* 19294 * Check if a device is still attached. For sanity, check also 19295 * link status - if no link, there is no device. 19296 */ 19297 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 19298 SATA_PORT_DEVLINK_UP || sata_device.satadev_type == 19299 SATA_DTYPE_NONE) { 19300 /* 19301 * No device - ignore attach event. 19302 */ 19303 pmportinfo->pmport_dev_attach_time = 0; 19304 mutex_exit(&pmportinfo->pmport_mutex); 19305 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19306 "Ignoring attach - no device connected to port %d:%d", 19307 cport, pmport); 19308 return; 19309 } 19310 19311 mutex_exit(&pmportinfo->pmport_mutex); 19312 /* 19313 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 19314 * with the hint: SE_HINT_INSERT 19315 */ 19316 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT); 19317 19318 /* 19319 * Port reprobing will take care of the creation of the device 19320 * info structure and determination of the device type. 19321 */ 19322 sata_device.satadev_addr = *saddr; 19323 (void) sata_reprobe_port(sata_hba_inst, &sata_device, 19324 SATA_DEV_IDENTIFY_NORETRY); 19325 19326 mutex_enter(&pmportinfo->pmport_mutex); 19327 if ((pmportinfo->pmport_state & SATA_STATE_READY) && 19328 (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE)) { 19329 /* Some device is attached to the port */ 19330 if (pmportinfo->pmport_dev_type == SATA_DTYPE_UNKNOWN) { 19331 /* 19332 * A device was not successfully attached. 19333 * Track retry time for device identification. 19334 */ 19335 if (pmportinfo->pmport_dev_attach_time != 0) { 19336 clock_t cur_time = ddi_get_lbolt(); 19337 /* 19338 * If the retry time limit was not exceeded, 19339 * reinstate attach event. 19340 */ 19341 if ((cur_time - 19342 pmportinfo->pmport_dev_attach_time) < 19343 drv_usectohz( 19344 SATA_DEV_IDENTIFY_TIMEOUT)) { 19345 /* OK, restore attach event */ 19346 pmportinfo->pmport_event_flags |= 19347 SATA_EVNT_DEVICE_ATTACHED; 19348 } else { 19349 /* Timeout - cannot identify device */ 19350 pmportinfo->pmport_dev_attach_time = 0; 19351 sata_log(sata_hba_inst, CE_WARN, 19352 "Could not identify SATA device " 19353 "at port %d:%d", 19354 cport, pmport); 19355 } 19356 } else { 19357 /* 19358 * Start tracking time for device 19359 * identification. 19360 * Save current time (lbolt value). 19361 */ 19362 pmportinfo->pmport_dev_attach_time = 19363 ddi_get_lbolt(); 19364 /* Restore attach event */ 19365 pmportinfo->pmport_event_flags |= 19366 SATA_EVNT_DEVICE_ATTACHED; 19367 } 19368 } else { 19369 /* 19370 * If device was successfully attached, the subsequent 19371 * action depends on a state of the 19372 * sata_auto_online variable. If it is set to zero. 19373 * an explicit 'configure' command will be needed to 19374 * configure it. If its value is non-zero, we will 19375 * attempt to online (configure) the device. 19376 * First, log the message indicating that a device 19377 * was attached. 19378 */ 19379 pmportinfo->pmport_dev_attach_time = 0; 19380 sata_log(sata_hba_inst, CE_WARN, 19381 "SATA device detected at port %d:%d", 19382 cport, pmport); 19383 19384 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 19385 sata_drive_info_t new_sdinfo; 19386 19387 /* Log device info data */ 19388 new_sdinfo = *(SATA_PMPORTINFO_DRV_INFO( 19389 pmportinfo)); 19390 sata_show_drive_info(sata_hba_inst, 19391 &new_sdinfo); 19392 } 19393 19394 mutex_exit(&pmportinfo->pmport_mutex); 19395 19396 /* 19397 * Make sure that there is no target node for that 19398 * device. If so, release it. It should not happen, 19399 * unless we had problem removing the node when 19400 * device was detached. 19401 */ 19402 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 19403 saddr->cport, saddr->pmport); 19404 mutex_enter(&pmportinfo->pmport_mutex); 19405 if (tdip != NULL) { 19406 19407 #ifdef SATA_DEBUG 19408 if ((pmportinfo->pmport_event_flags & 19409 SATA_EVNT_TARGET_NODE_CLEANUP) == 0) 19410 sata_log(sata_hba_inst, CE_WARN, 19411 "sata_process_device_attached: " 19412 "old device target node exists!"); 19413 #endif 19414 /* 19415 * target node exists - try to unconfigure 19416 * device and remove the node. 19417 */ 19418 mutex_exit(&pmportinfo->pmport_mutex); 19419 rval = ndi_devi_offline(tdip, 19420 NDI_DEVI_REMOVE); 19421 mutex_enter(&pmportinfo->pmport_mutex); 19422 19423 if (rval == NDI_SUCCESS) { 19424 pmportinfo->pmport_event_flags &= 19425 ~SATA_EVNT_TARGET_NODE_CLEANUP; 19426 pmportinfo->pmport_tgtnode_clean = 19427 B_TRUE; 19428 } else { 19429 /* 19430 * PROBLEM - the target node remained 19431 * and it belongs to a previously 19432 * attached device. 19433 * This happens when the file was open 19434 * or the node was waiting for 19435 * resources at the time the 19436 * associated device was removed. 19437 * Instruct event daemon to retry the 19438 * cleanup later. 19439 */ 19440 sata_log(sata_hba_inst, 19441 CE_WARN, 19442 "Application(s) accessing " 19443 "previously attached SATA " 19444 "device have to release " 19445 "it before newly inserted " 19446 "device can be made accessible." 19447 "at port %d:%d", 19448 cport, pmport); 19449 pmportinfo->pmport_event_flags |= 19450 SATA_EVNT_TARGET_NODE_CLEANUP; 19451 pmportinfo->pmport_tgtnode_clean = 19452 B_FALSE; 19453 } 19454 } 19455 if (sata_auto_online != 0) { 19456 pmportinfo->pmport_event_flags |= 19457 SATA_EVNT_AUTOONLINE_DEVICE; 19458 } 19459 19460 } 19461 } else { 19462 pmportinfo->pmport_dev_attach_time = 0; 19463 } 19464 19465 event_flags = pmportinfo->pmport_event_flags; 19466 mutex_exit(&pmportinfo->pmport_mutex); 19467 if (event_flags != 0) { 19468 mutex_enter(&sata_hba_inst->satahba_mutex); 19469 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 19470 mutex_exit(&sata_hba_inst->satahba_mutex); 19471 mutex_enter(&sata_mutex); 19472 sata_event_pending |= SATA_EVNT_MAIN; 19473 mutex_exit(&sata_mutex); 19474 } 19475 19476 /* clear the reset_in_progress events */ 19477 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 19478 if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) { 19479 /* must clear flags on cport */ 19480 sata_pmult_info_t *pminfo = 19481 SATA_PMULT_INFO(sata_hba_inst, 19482 saddr->cport); 19483 pminfo->pmult_event_flags |= 19484 SATA_EVNT_CLEAR_DEVICE_RESET; 19485 } 19486 } 19487 } 19488 19489 /* 19490 * Device Target Node Cleanup Event processing. 19491 * If the target node associated with a sata port device is in 19492 * DEVI_DEVICE_REMOVED state, an attempt is made to remove it. 19493 * If the target node cannot be removed, the event flag is left intact, 19494 * so that event daemon may re-run this function later. 19495 * 19496 * This function cannot be called in interrupt context (it may sleep). 19497 * 19498 * NOTE: Processes cport events only, not port multiplier ports. 19499 */ 19500 static void 19501 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, 19502 sata_address_t *saddr) 19503 { 19504 sata_cport_info_t *cportinfo; 19505 dev_info_t *tdip; 19506 19507 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19508 "Processing port %d device target node cleanup", saddr->cport); 19509 19510 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 19511 19512 /* 19513 * Check if there is target node for that device and it is in the 19514 * DEVI_DEVICE_REMOVED state. If so, release it. 19515 */ 19516 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport, 19517 saddr->pmport); 19518 if (tdip != NULL) { 19519 /* 19520 * target node exists - check if it is target node of 19521 * a removed device. 19522 */ 19523 if (sata_check_device_removed(tdip) == B_TRUE) { 19524 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19525 "sata_process_target_node_cleanup: " 19526 "old device target node exists!", NULL); 19527 /* 19528 * Unconfigure and remove the target node 19529 */ 19530 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) == 19531 NDI_SUCCESS) { 19532 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 19533 saddr->cport)->cport_mutex); 19534 cportinfo->cport_event_flags &= 19535 ~SATA_EVNT_TARGET_NODE_CLEANUP; 19536 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19537 saddr->cport)->cport_mutex); 19538 return; 19539 } 19540 /* 19541 * Event daemon will retry the cleanup later. 19542 */ 19543 mutex_enter(&sata_hba_inst->satahba_mutex); 19544 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 19545 mutex_exit(&sata_hba_inst->satahba_mutex); 19546 mutex_enter(&sata_mutex); 19547 sata_event_pending |= SATA_EVNT_MAIN; 19548 mutex_exit(&sata_mutex); 19549 } 19550 } else { 19551 if (saddr->qual == SATA_ADDR_CPORT || 19552 saddr->qual == SATA_ADDR_DCPORT) { 19553 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 19554 saddr->cport)->cport_mutex); 19555 cportinfo->cport_event_flags &= 19556 ~SATA_EVNT_TARGET_NODE_CLEANUP; 19557 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19558 saddr->cport)->cport_mutex); 19559 } else { 19560 /* sanity check */ 19561 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) != 19562 SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst, 19563 saddr->cport) == NULL) 19564 return; 19565 if (SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, 19566 saddr->pmport) == NULL) 19567 return; 19568 19569 mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst, 19570 saddr->cport, saddr->pmport)->pmport_mutex); 19571 SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, 19572 saddr->pmport)->pmport_event_flags &= 19573 ~SATA_EVNT_TARGET_NODE_CLEANUP; 19574 mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst, 19575 saddr->cport, saddr->pmport)->pmport_mutex); 19576 } 19577 } 19578 } 19579 19580 /* 19581 * Device AutoOnline Event processing. 19582 * If attached device is to be onlined, an attempt is made to online this 19583 * device, but only if there is no lingering (old) target node present. 19584 * If the device cannot be onlined, the event flag is left intact, 19585 * so that event daemon may re-run this function later. 19586 * 19587 * This function cannot be called in interrupt context (it may sleep). 19588 * 19589 * NOTE: Processes cport events only, not port multiplier ports. 19590 */ 19591 static void 19592 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst, 19593 sata_address_t *saddr) 19594 { 19595 sata_cport_info_t *cportinfo; 19596 sata_drive_info_t *sdinfo; 19597 sata_device_t sata_device; 19598 dev_info_t *tdip; 19599 19600 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19601 "Processing port %d attached device auto-onlining", saddr->cport); 19602 19603 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 19604 19605 /* 19606 * Check if device is present and recognized. If not, reset event. 19607 */ 19608 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19609 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) { 19610 /* Nothing to online */ 19611 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE; 19612 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19613 saddr->cport)->cport_mutex); 19614 return; 19615 } 19616 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19617 19618 /* 19619 * Check if there is target node for this device and if it is in the 19620 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep 19621 * the event for later processing. 19622 */ 19623 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport, 19624 saddr->pmport); 19625 if (tdip != NULL) { 19626 /* 19627 * target node exists - check if it is target node of 19628 * a removed device. 19629 */ 19630 if (sata_check_device_removed(tdip) == B_TRUE) { 19631 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19632 "sata_process_device_autoonline: " 19633 "old device target node exists!", NULL); 19634 /* 19635 * Event daemon will retry device onlining later. 19636 */ 19637 mutex_enter(&sata_hba_inst->satahba_mutex); 19638 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 19639 mutex_exit(&sata_hba_inst->satahba_mutex); 19640 mutex_enter(&sata_mutex); 19641 sata_event_pending |= SATA_EVNT_MAIN; 19642 mutex_exit(&sata_mutex); 19643 return; 19644 } 19645 /* 19646 * If the target node is not in the 'removed" state, assume 19647 * that it belongs to this device. There is nothing more to do, 19648 * but reset the event. 19649 */ 19650 } else { 19651 19652 /* 19653 * Try to online the device 19654 * If there is any reset-related event, remove it. We are 19655 * configuring the device and no state restoring is needed. 19656 */ 19657 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 19658 saddr->cport)->cport_mutex); 19659 sata_device.satadev_addr = *saddr; 19660 if (saddr->qual == SATA_ADDR_CPORT) 19661 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 19662 else 19663 sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT; 19664 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 19665 if (sdinfo != NULL) { 19666 if (sdinfo->satadrv_event_flags & 19667 (SATA_EVNT_DEVICE_RESET | 19668 SATA_EVNT_INPROC_DEVICE_RESET)) 19669 sdinfo->satadrv_event_flags = 0; 19670 sdinfo->satadrv_event_flags |= 19671 SATA_EVNT_CLEAR_DEVICE_RESET; 19672 19673 /* Need to create a new target node. */ 19674 cportinfo->cport_tgtnode_clean = B_TRUE; 19675 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19676 saddr->cport)->cport_mutex); 19677 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst), 19678 sata_hba_inst, &sata_device.satadev_addr); 19679 if (tdip == NULL) { 19680 /* 19681 * Configure (onlining) failed. 19682 * We will NOT retry 19683 */ 19684 SATA_LOG_D((sata_hba_inst, CE_WARN, 19685 "sata_process_device_autoonline: " 19686 "configuring SATA device at port %d failed", 19687 saddr->cport)); 19688 } 19689 } else { 19690 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19691 saddr->cport)->cport_mutex); 19692 } 19693 19694 } 19695 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19696 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE; 19697 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19698 saddr->cport)->cport_mutex); 19699 } 19700 19701 19702 static void 19703 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr, 19704 int hint) 19705 { 19706 char ap[MAXPATHLEN]; 19707 nvlist_t *ev_attr_list = NULL; 19708 int err; 19709 19710 /* Allocate and build sysevent attribute list */ 19711 err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP); 19712 if (err != 0) { 19713 SATA_LOG_D((sata_hba_inst, CE_WARN, 19714 "sata_gen_sysevent: " 19715 "cannot allocate memory for sysevent attributes\n")); 19716 return; 19717 } 19718 /* Add hint attribute */ 19719 err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint)); 19720 if (err != 0) { 19721 SATA_LOG_D((sata_hba_inst, CE_WARN, 19722 "sata_gen_sysevent: " 19723 "failed to add DR_HINT attr for sysevent")); 19724 nvlist_free(ev_attr_list); 19725 return; 19726 } 19727 /* 19728 * Add AP attribute. 19729 * Get controller pathname and convert it into AP pathname by adding 19730 * a target number. 19731 */ 19732 (void) snprintf(ap, MAXPATHLEN, "/devices"); 19733 (void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap)); 19734 (void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d", 19735 SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual)); 19736 19737 err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap); 19738 if (err != 0) { 19739 SATA_LOG_D((sata_hba_inst, CE_WARN, 19740 "sata_gen_sysevent: " 19741 "failed to add DR_AP_ID attr for sysevent")); 19742 nvlist_free(ev_attr_list); 19743 return; 19744 } 19745 19746 /* Generate/log sysevent */ 19747 err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR, 19748 ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP); 19749 if (err != DDI_SUCCESS) { 19750 SATA_LOG_D((sata_hba_inst, CE_WARN, 19751 "sata_gen_sysevent: " 19752 "cannot log sysevent, err code %x\n", err)); 19753 } 19754 19755 nvlist_free(ev_attr_list); 19756 } 19757 19758 19759 19760 19761 /* 19762 * Set DEVI_DEVICE_REMOVED state in the SATA device target node. 19763 */ 19764 static void 19765 sata_set_device_removed(dev_info_t *tdip) 19766 { 19767 int circ; 19768 19769 ASSERT(tdip != NULL); 19770 19771 ndi_devi_enter(tdip, &circ); 19772 mutex_enter(&DEVI(tdip)->devi_lock); 19773 DEVI_SET_DEVICE_REMOVED(tdip); 19774 mutex_exit(&DEVI(tdip)->devi_lock); 19775 ndi_devi_exit(tdip, circ); 19776 } 19777 19778 19779 /* 19780 * Set internal event instructing event daemon to try 19781 * to perform the target node cleanup. 19782 */ 19783 static void 19784 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, 19785 sata_address_t *saddr) 19786 { 19787 if (saddr->qual == SATA_ADDR_CPORT || 19788 saddr->qual == SATA_ADDR_DCPORT) { 19789 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 19790 saddr->cport)->cport_mutex); 19791 SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |= 19792 SATA_EVNT_TARGET_NODE_CLEANUP; 19793 SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19794 cport_tgtnode_clean = B_FALSE; 19795 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19796 saddr->cport)->cport_mutex); 19797 } else { 19798 mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst, 19799 saddr->cport, saddr->pmport)->pmport_mutex); 19800 SATA_PMPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport, 19801 saddr->pmport) |= SATA_EVNT_TARGET_NODE_CLEANUP; 19802 SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, saddr->pmport)-> 19803 pmport_tgtnode_clean = B_FALSE; 19804 mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst, 19805 saddr->cport, saddr->pmport)->pmport_mutex); 19806 } 19807 mutex_enter(&sata_hba_inst->satahba_mutex); 19808 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 19809 mutex_exit(&sata_hba_inst->satahba_mutex); 19810 mutex_enter(&sata_mutex); 19811 sata_event_pending |= SATA_EVNT_MAIN; 19812 mutex_exit(&sata_mutex); 19813 } 19814 19815 19816 /* 19817 * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state, 19818 * i.e. check if the target node state indicates that it belongs to a removed 19819 * device. 19820 * 19821 * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state, 19822 * B_FALSE otherwise. 19823 */ 19824 static boolean_t 19825 sata_check_device_removed(dev_info_t *tdip) 19826 { 19827 ASSERT(tdip != NULL); 19828 19829 if (DEVI_IS_DEVICE_REMOVED(tdip)) 19830 return (B_TRUE); 19831 else 19832 return (B_FALSE); 19833 } 19834 19835 /* ************************ FAULT INJECTTION **************************** */ 19836 19837 #ifdef SATA_INJECT_FAULTS 19838 19839 static uint32_t sata_fault_count = 0; 19840 static uint32_t sata_fault_suspend_count = 0; 19841 19842 /* 19843 * Inject sata pkt fault 19844 * It modifies returned values of the sata packet. 19845 * It returns immediately if: 19846 * pkt fault injection is not enabled (via sata_inject_fault, 19847 * sata_inject_fault_count), or invalid fault is specified (sata_fault_type), 19848 * or pkt does not contain command to be faulted (set in sata_fault_cmd), or 19849 * pkt is not directed to specified fault controller/device 19850 * (sata_fault_ctrl_dev and sata_fault_device). 19851 * If fault controller is not specified, fault injection applies to all 19852 * controllers and devices. 19853 * 19854 * First argument is the pointer to the executed sata packet. 19855 * Second argument is a pointer to a value returned by the HBA tran_start 19856 * function. 19857 * Third argument specifies injected error. Injected sata packet faults 19858 * are the satapkt_reason values. 19859 * SATA_PKT_BUSY -1 Not completed, busy 19860 * SATA_PKT_DEV_ERROR 1 Device reported error 19861 * SATA_PKT_QUEUE_FULL 2 Not accepted, queue full 19862 * SATA_PKT_PORT_ERROR 3 Not completed, port error 19863 * SATA_PKT_CMD_UNSUPPORTED 4 Cmd unsupported 19864 * SATA_PKT_ABORTED 5 Aborted by request 19865 * SATA_PKT_TIMEOUT 6 Operation timeut 19866 * SATA_PKT_RESET 7 Aborted by reset request 19867 * 19868 * Additional global variables affecting the execution: 19869 * 19870 * sata_inject_fault_count variable specifies number of times in row the 19871 * error is injected. Value of -1 specifies permanent fault, ie. every time 19872 * the fault injection point is reached, the fault is injected and a pause 19873 * between fault injection specified by sata_inject_fault_pause_count is 19874 * ignored). Fault injection routine decrements sata_inject_fault_count 19875 * (if greater than zero) until it reaches 0. No fault is injected when 19876 * sata_inject_fault_count is 0 (zero). 19877 * 19878 * sata_inject_fault_pause_count variable specifies number of times a fault 19879 * injection is bypassed (pause between fault injections). 19880 * If set to 0, a fault is injected only a number of times specified by 19881 * sata_inject_fault_count. 19882 * 19883 * The fault counts are static, so for periodic errors they have to be manually 19884 * reset to start repetition sequence from scratch. 19885 * If the original value returned by the HBA tran_start function is not 19886 * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error 19887 * is injected (to avoid masking real problems); 19888 * 19889 * NOTE: In its current incarnation, this function should be invoked only for 19890 * commands executed in SYNCHRONOUS mode. 19891 */ 19892 19893 19894 static void 19895 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault) 19896 { 19897 19898 if (sata_inject_fault != SATA_INJECT_PKT_FAULT) 19899 return; 19900 19901 if (sata_inject_fault_count == 0) 19902 return; 19903 19904 if (fault == 0) 19905 return; 19906 19907 if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg) 19908 return; 19909 19910 if (sata_fault_ctrl != NULL) { 19911 sata_pkt_txlate_t *spx = 19912 (sata_pkt_txlate_t *)spkt->satapkt_framework_private; 19913 19914 if (sata_fault_ctrl != NULL && sata_fault_ctrl != 19915 spx->txlt_sata_hba_inst->satahba_dip) 19916 return; 19917 19918 if (sata_fault_device.satadev_addr.cport != 19919 spkt->satapkt_device.satadev_addr.cport || 19920 sata_fault_device.satadev_addr.pmport != 19921 spkt->satapkt_device.satadev_addr.pmport || 19922 sata_fault_device.satadev_addr.qual != 19923 spkt->satapkt_device.satadev_addr.qual) 19924 return; 19925 } 19926 19927 /* Modify pkt return parameters */ 19928 if (*rval != SATA_TRAN_ACCEPTED || 19929 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 19930 sata_fault_count = 0; 19931 sata_fault_suspend_count = 0; 19932 return; 19933 } 19934 if (sata_fault_count == 0 && sata_fault_suspend_count != 0) { 19935 /* Pause in the injection */ 19936 sata_fault_suspend_count -= 1; 19937 return; 19938 } 19939 19940 if (sata_fault_count == 0 && sata_fault_suspend_count == 0) { 19941 /* 19942 * Init inject fault cycle. If fault count is set to -1, 19943 * it is a permanent fault. 19944 */ 19945 if (sata_inject_fault_count != -1) { 19946 sata_fault_count = sata_inject_fault_count; 19947 sata_fault_suspend_count = 19948 sata_inject_fault_pause_count; 19949 if (sata_fault_suspend_count == 0) 19950 sata_inject_fault_count = 0; 19951 } 19952 } 19953 19954 if (sata_fault_count != 0) 19955 sata_fault_count -= 1; 19956 19957 switch (fault) { 19958 case SATA_PKT_BUSY: 19959 *rval = SATA_TRAN_BUSY; 19960 spkt->satapkt_reason = SATA_PKT_BUSY; 19961 break; 19962 19963 case SATA_PKT_QUEUE_FULL: 19964 *rval = SATA_TRAN_QUEUE_FULL; 19965 spkt->satapkt_reason = SATA_PKT_QUEUE_FULL; 19966 break; 19967 19968 case SATA_PKT_CMD_UNSUPPORTED: 19969 *rval = SATA_TRAN_CMD_UNSUPPORTED; 19970 spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED; 19971 break; 19972 19973 case SATA_PKT_PORT_ERROR: 19974 /* This is "rejected" command */ 19975 *rval = SATA_TRAN_PORT_ERROR; 19976 spkt->satapkt_reason = SATA_PKT_PORT_ERROR; 19977 /* Additional error setup could be done here - port state */ 19978 break; 19979 19980 case SATA_PKT_DEV_ERROR: 19981 spkt->satapkt_reason = SATA_PKT_DEV_ERROR; 19982 /* 19983 * Additional error setup could be done here 19984 */ 19985 break; 19986 19987 case SATA_PKT_ABORTED: 19988 spkt->satapkt_reason = SATA_PKT_ABORTED; 19989 break; 19990 19991 case SATA_PKT_TIMEOUT: 19992 spkt->satapkt_reason = SATA_PKT_TIMEOUT; 19993 /* Additional error setup could be done here */ 19994 break; 19995 19996 case SATA_PKT_RESET: 19997 spkt->satapkt_reason = SATA_PKT_RESET; 19998 /* 19999 * Additional error setup could be done here - device reset 20000 */ 20001 break; 20002 20003 default: 20004 break; 20005 } 20006 } 20007 20008 #endif 20009 20010 /* 20011 * SATA Trace Ring Buffer 20012 * ---------------------- 20013 * 20014 * Overview 20015 * 20016 * The SATA trace ring buffer is a ring buffer created and managed by 20017 * the SATA framework module that can be used by any module or driver 20018 * within the SATA framework to store debug messages. 20019 * 20020 * Ring Buffer Interfaces: 20021 * 20022 * sata_vtrace_debug() <-- Adds debug message to ring buffer 20023 * sata_trace_debug() <-- Wraps varargs into sata_vtrace_debug() 20024 * 20025 * Note that the sata_trace_debug() interface was created to give 20026 * consumers the flexibilty of sending debug messages to ring buffer 20027 * as variable arguments. Consumers can send type va_list debug 20028 * messages directly to sata_vtrace_debug(). The sata_trace_debug() 20029 * and sata_vtrace_debug() relationship is similar to that of 20030 * cmn_err(9F) and vcmn_err(9F). 20031 * 20032 * Below is a diagram of the SATA trace ring buffer interfaces and 20033 * sample consumers: 20034 * 20035 * +---------------------------------+ 20036 * | o o SATA Framework Module | 20037 * | o SATA o +------------------+ +------------------+ 20038 * |o Trace o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1| 20039 * |o R-Buf o |sata_trace_debug |<--+ +------------------+ 20040 * | o o +------------------+ | +------------------+ 20041 * | o o ^ | +--|SATA HBA Driver #2| 20042 * | | | +------------------+ 20043 * | +------------------+ | 20044 * | |SATA Debug Message| | 20045 * | +------------------+ | 20046 * +---------------------------------+ 20047 * 20048 * Supporting Routines: 20049 * 20050 * sata_trace_rbuf_alloc() <-- Initializes ring buffer 20051 * sata_trace_rbuf_free() <-- Destroys ring buffer 20052 * sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer 20053 * sata_trace_dmsg_free() <-- Destroys content of ring buffer 20054 * 20055 * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE. 20056 * The ring buffer size can be adjusted by setting dmsg_ring_size in 20057 * /etc/system to desired size in unit of bytes. 20058 * 20059 * The individual debug message size in the ring buffer is restricted 20060 * to DMSG_BUF_SIZE. 20061 */ 20062 void 20063 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap) 20064 { 20065 sata_trace_dmsg_t *dmsg; 20066 20067 if (sata_debug_rbuf == NULL) { 20068 return; 20069 } 20070 20071 /* 20072 * If max size of ring buffer is smaller than size 20073 * required for one debug message then just return 20074 * since we have no room for the debug message. 20075 */ 20076 if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) { 20077 return; 20078 } 20079 20080 mutex_enter(&sata_debug_rbuf->lock); 20081 20082 /* alloc or reuse on ring buffer */ 20083 dmsg = sata_trace_dmsg_alloc(); 20084 20085 if (dmsg == NULL) { 20086 /* resource allocation failed */ 20087 mutex_exit(&sata_debug_rbuf->lock); 20088 return; 20089 } 20090 20091 dmsg->dip = dip; 20092 gethrestime(&dmsg->timestamp); 20093 20094 (void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap); 20095 20096 mutex_exit(&sata_debug_rbuf->lock); 20097 } 20098 20099 void 20100 sata_trace_debug(dev_info_t *dip, const char *fmt, ...) 20101 { 20102 va_list ap; 20103 20104 va_start(ap, fmt); 20105 sata_vtrace_debug(dip, fmt, ap); 20106 va_end(ap); 20107 } 20108 20109 /* 20110 * This routine is used to manage debug messages 20111 * on ring buffer. 20112 */ 20113 static sata_trace_dmsg_t * 20114 sata_trace_dmsg_alloc(void) 20115 { 20116 sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp; 20117 20118 if (sata_debug_rbuf->looped == TRUE) { 20119 sata_debug_rbuf->dmsgp = dmsg->next; 20120 return (sata_debug_rbuf->dmsgp); 20121 } 20122 20123 /* 20124 * If we're looping for the first time, 20125 * connect the ring. 20126 */ 20127 if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) > 20128 sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) { 20129 dmsg->next = sata_debug_rbuf->dmsgh; 20130 sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh; 20131 sata_debug_rbuf->looped = TRUE; 20132 return (sata_debug_rbuf->dmsgp); 20133 } 20134 20135 /* If we've gotten this far then memory allocation is needed */ 20136 dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP); 20137 if (dmsg_alloc == NULL) { 20138 sata_debug_rbuf->allocfailed++; 20139 return (dmsg_alloc); 20140 } else { 20141 sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t); 20142 } 20143 20144 if (sata_debug_rbuf->dmsgp != NULL) { 20145 dmsg->next = dmsg_alloc; 20146 sata_debug_rbuf->dmsgp = dmsg->next; 20147 return (sata_debug_rbuf->dmsgp); 20148 } else { 20149 /* 20150 * We should only be here if we're initializing 20151 * the ring buffer. 20152 */ 20153 if (sata_debug_rbuf->dmsgh == NULL) { 20154 sata_debug_rbuf->dmsgh = dmsg_alloc; 20155 } else { 20156 /* Something is wrong */ 20157 kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t)); 20158 return (NULL); 20159 } 20160 20161 sata_debug_rbuf->dmsgp = dmsg_alloc; 20162 return (sata_debug_rbuf->dmsgp); 20163 } 20164 } 20165 20166 20167 /* 20168 * Free all messages on debug ring buffer. 20169 */ 20170 static void 20171 sata_trace_dmsg_free(void) 20172 { 20173 sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh; 20174 20175 while (dmsg != NULL) { 20176 dmsg_next = dmsg->next; 20177 kmem_free(dmsg, sizeof (sata_trace_dmsg_t)); 20178 20179 /* 20180 * If we've looped around the ring than we're done. 20181 */ 20182 if (dmsg_next == sata_debug_rbuf->dmsgh) { 20183 break; 20184 } else { 20185 dmsg = dmsg_next; 20186 } 20187 } 20188 } 20189 20190 20191 /* 20192 * This function can block 20193 */ 20194 static void 20195 sata_trace_rbuf_alloc(void) 20196 { 20197 sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP); 20198 20199 mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL); 20200 20201 if (dmsg_ring_size > 0) { 20202 sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size; 20203 } 20204 } 20205 20206 20207 static void 20208 sata_trace_rbuf_free(void) 20209 { 20210 sata_trace_dmsg_free(); 20211 mutex_destroy(&sata_debug_rbuf->lock); 20212 kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t)); 20213 } 20214 20215 /* 20216 * If SATA_DEBUG is not defined then this routine is called instead 20217 * of sata_log() via the SATA_LOG_D macro. 20218 */ 20219 static void 20220 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level, 20221 const char *fmt, ...) 20222 { 20223 #ifndef __lock_lint 20224 _NOTE(ARGUNUSED(level)) 20225 #endif 20226 20227 dev_info_t *dip = NULL; 20228 va_list ap; 20229 20230 if (sata_hba_inst != NULL) { 20231 dip = SATA_DIP(sata_hba_inst); 20232 } 20233 20234 va_start(ap, fmt); 20235 sata_vtrace_debug(dip, fmt, ap); 20236 va_end(ap); 20237 } 20238