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 2009 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 55 /* Debug flags - defined in sata.h */ 56 int sata_debug_flags = 0; 57 int sata_msg = 0; 58 59 /* 60 * Flags enabling selected SATA HBA framework functionality 61 */ 62 #define SATA_ENABLE_QUEUING 1 63 #define SATA_ENABLE_NCQ 2 64 #define SATA_ENABLE_PROCESS_EVENTS 4 65 #define SATA_ENABLE_PMULT_FBS 8 /* FIS-Based Switching */ 66 int sata_func_enable = 67 SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ; 68 69 /* 70 * Global variable setting default maximum queue depth (NCQ or TCQ) 71 * Note:minimum queue depth is 1 72 */ 73 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */ 74 75 /* 76 * Currently used default NCQ/TCQ queue depth. It is set-up during the driver 77 * initialization, using value from sata_max_queue_depth 78 * It is adjusted to minimum supported by the controller and by the device, 79 * if queueing is enabled. 80 */ 81 static int sata_current_max_qdepth; 82 83 /* 84 * Global variable determining the default behavior after device hotpluggin. 85 * If non-zero, the hotplugged device is onlined (if possible) without explicit 86 * IOCTL request (AP_CONFIGURE). 87 * If zero, hotplugged device is identified, but not onlined. 88 * Enabling (AP_CONNECT) device port with an attached device does not result 89 * in device onlining regardless of the flag setting 90 */ 91 int sata_auto_online = 0; 92 93 #ifdef SATA_DEBUG 94 95 #define SATA_LOG_D(args) sata_log args 96 uint64_t mbuf_count = 0; 97 uint64_t mbuffail_count = 0; 98 99 sata_atapi_cmd_t sata_atapi_trace[64]; 100 uint32_t sata_atapi_trace_index = 0; 101 int sata_atapi_trace_save = 1; 102 static void sata_save_atapi_trace(sata_pkt_txlate_t *, int); 103 #define SATAATAPITRACE(spx, count) if (sata_atapi_trace_save) \ 104 sata_save_atapi_trace(spx, count); 105 106 #else 107 #define SATA_LOG_D(args) sata_trace_log args 108 #define SATAATAPITRACE(spx, count) 109 #endif 110 111 #if 0 112 static void 113 sata_test_atapi_packet_command(sata_hba_inst_t *, int); 114 #endif 115 116 #ifdef SATA_INJECT_FAULTS 117 118 #define SATA_INJECT_PKT_FAULT 1 119 uint32_t sata_inject_fault = 0; 120 121 uint32_t sata_inject_fault_count = 0; 122 uint32_t sata_inject_fault_pause_count = 0; 123 uint32_t sata_fault_type = 0; 124 uint32_t sata_fault_cmd = 0; 125 dev_info_t *sata_fault_ctrl = NULL; 126 sata_device_t sata_fault_device; 127 128 static void sata_inject_pkt_fault(sata_pkt_t *, int *, int); 129 130 #endif 131 132 #define LEGACY_HWID_LEN 64 /* Model (40) + Serial (20) + pad */ 133 134 static char sata_rev_tag[] = {"1.45"}; 135 136 /* 137 * SATA cb_ops functions 138 */ 139 static int sata_hba_open(dev_t *, int, int, cred_t *); 140 static int sata_hba_close(dev_t, int, int, cred_t *); 141 static int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 142 143 /* 144 * SCSA required entry points 145 */ 146 static int sata_scsi_tgt_init(dev_info_t *, dev_info_t *, 147 scsi_hba_tran_t *, struct scsi_device *); 148 static int sata_scsi_tgt_probe(struct scsi_device *, 149 int (*callback)(void)); 150 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *, 151 scsi_hba_tran_t *, struct scsi_device *); 152 static int sata_scsi_start(struct scsi_address *, struct scsi_pkt *); 153 static int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *); 154 static int sata_scsi_reset(struct scsi_address *, int); 155 static int sata_scsi_getcap(struct scsi_address *, char *, int); 156 static int sata_scsi_setcap(struct scsi_address *, char *, int, int); 157 static struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *, 158 struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t), 159 caddr_t); 160 static void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *); 161 static void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *); 162 static void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *); 163 164 /* 165 * SATA HBA interface functions are defined in sata_hba.h header file 166 */ 167 168 /* Event processing functions */ 169 static void sata_event_daemon(void *); 170 static void sata_event_thread_control(int); 171 static void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst); 172 static void sata_process_pmult_events(sata_hba_inst_t *, uint8_t); 173 static void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *); 174 static void sata_process_pmdevice_reset(sata_hba_inst_t *, sata_address_t *); 175 static void sata_process_port_failed_event(sata_hba_inst_t *, 176 sata_address_t *); 177 static void sata_process_port_link_events(sata_hba_inst_t *, 178 sata_address_t *); 179 static void sata_process_pmport_link_events(sata_hba_inst_t *, 180 sata_address_t *); 181 static void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *); 182 static void sata_process_pmdevice_detached(sata_hba_inst_t *, 183 sata_address_t *); 184 static void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *); 185 static void sata_process_pmdevice_attached(sata_hba_inst_t *, 186 sata_address_t *); 187 static void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *); 188 static void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *); 189 static void sata_process_target_node_cleanup(sata_hba_inst_t *, 190 sata_address_t *); 191 static void sata_process_device_autoonline(sata_hba_inst_t *, 192 sata_address_t *saddr); 193 194 /* 195 * Local translation functions 196 */ 197 static int sata_txlt_inquiry(sata_pkt_txlate_t *); 198 static int sata_txlt_test_unit_ready(sata_pkt_txlate_t *); 199 static int sata_txlt_start_stop_unit(sata_pkt_txlate_t *); 200 static int sata_txlt_read_capacity(sata_pkt_txlate_t *); 201 static int sata_txlt_request_sense(sata_pkt_txlate_t *); 202 static int sata_txlt_read(sata_pkt_txlate_t *); 203 static int sata_txlt_write(sata_pkt_txlate_t *); 204 static int sata_txlt_log_sense(sata_pkt_txlate_t *); 205 static int sata_txlt_log_select(sata_pkt_txlate_t *); 206 static int sata_txlt_mode_sense(sata_pkt_txlate_t *); 207 static int sata_txlt_mode_select(sata_pkt_txlate_t *); 208 static int sata_txlt_synchronize_cache(sata_pkt_txlate_t *); 209 static int sata_txlt_write_buffer(sata_pkt_txlate_t *); 210 static int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *); 211 212 static int sata_hba_start(sata_pkt_txlate_t *, int *); 213 static int sata_txlt_invalid_command(sata_pkt_txlate_t *); 214 static int sata_txlt_check_condition(sata_pkt_txlate_t *, uchar_t, uchar_t); 215 static int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *); 216 static void sata_txlt_rw_completion(sata_pkt_t *); 217 static void sata_txlt_nodata_cmd_completion(sata_pkt_t *); 218 static void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *); 219 static int sata_emul_rw_completion(sata_pkt_txlate_t *); 220 static struct scsi_extended_sense *sata_immediate_error_response( 221 sata_pkt_txlate_t *, int); 222 static struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *); 223 224 static int sata_txlt_atapi(sata_pkt_txlate_t *); 225 static void sata_txlt_atapi_completion(sata_pkt_t *); 226 227 /* 228 * Local functions for ioctl 229 */ 230 static int32_t sata_get_port_num(sata_hba_inst_t *, struct devctl_iocdata *); 231 static void sata_cfgadm_state(sata_hba_inst_t *, int32_t, 232 devctl_ap_state_t *); 233 static dev_info_t *sata_get_target_dip(dev_info_t *, uint8_t, uint8_t); 234 static dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *); 235 static dev_info_t *sata_devt_to_devinfo(dev_t); 236 static int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *); 237 static int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *); 238 static int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *); 239 static int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *); 240 static int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *); 241 static int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *); 242 static int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *); 243 static int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *); 244 static int sata_ioctl_reset_all(sata_hba_inst_t *); 245 static int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *); 246 static int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *, 247 sata_ioctl_data_t *, int mode); 248 static int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *, 249 sata_ioctl_data_t *, int mode); 250 static int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *, 251 sata_ioctl_data_t *, int mode); 252 static int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *, 253 sata_ioctl_data_t *, int mode); 254 static int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *, 255 sata_device_t *, sata_ioctl_data_t *, int mode); 256 257 /* 258 * Local functions 259 */ 260 static void sata_remove_hba_instance(dev_info_t *); 261 static int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *); 262 static void sata_probe_ports(sata_hba_inst_t *); 263 static void sata_probe_pmports(sata_hba_inst_t *, uint8_t); 264 static int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int); 265 static int sata_reprobe_pmult(sata_hba_inst_t *, sata_device_t *, int); 266 static int sata_reprobe_pmport(sata_hba_inst_t *, sata_device_t *, int); 267 static void sata_alloc_pmult(sata_hba_inst_t *, sata_device_t *); 268 static void sata_free_pmult(sata_hba_inst_t *, sata_device_t *); 269 static int sata_add_device(dev_info_t *, sata_hba_inst_t *, sata_device_t *); 270 static int sata_offline_device(sata_hba_inst_t *, sata_device_t *, 271 sata_drive_info_t *); 272 static dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *, 273 sata_address_t *); 274 static void sata_remove_target_node(sata_hba_inst_t *, 275 sata_address_t *); 276 static int sata_validate_scsi_address(sata_hba_inst_t *, 277 struct scsi_address *, sata_device_t *); 278 static int sata_validate_sata_address(sata_hba_inst_t *, int, int, int); 279 static sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t)); 280 static void sata_pkt_free(sata_pkt_txlate_t *); 281 static int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t), 282 caddr_t, ddi_dma_attr_t *); 283 static void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *); 284 static int sata_probe_device(sata_hba_inst_t *, sata_device_t *); 285 static sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *, 286 sata_device_t *); 287 static int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *); 288 static void sata_reidentify_device(sata_pkt_txlate_t *); 289 static struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int); 290 static void sata_free_local_buffer(sata_pkt_txlate_t *); 291 static uint64_t sata_check_capacity(sata_drive_info_t *); 292 void sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *, 293 ddi_dma_attr_t *); 294 static int sata_fetch_device_identify_data(sata_hba_inst_t *, 295 sata_drive_info_t *); 296 static void sata_update_port_info(sata_hba_inst_t *, sata_device_t *); 297 static void sata_update_pmport_info(sata_hba_inst_t *, sata_device_t *); 298 static void sata_update_port_scr(sata_port_scr_t *, sata_device_t *); 299 static int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *); 300 static int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int); 301 static int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int); 302 static int sata_set_drive_features(sata_hba_inst_t *, 303 sata_drive_info_t *, int flag); 304 static void sata_init_write_cache_mode(sata_drive_info_t *sdinfo); 305 static int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *); 306 static void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *, 307 uint8_t *); 308 static int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *, 309 struct scsi_inquiry *); 310 static int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *); 311 static int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *); 312 static int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *); 313 static int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *); 314 static int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *); 315 static int sata_mode_select_page_8(sata_pkt_txlate_t *, 316 struct mode_cache_scsi3 *, int, int *, int *, int *); 317 static int sata_mode_select_page_1a(sata_pkt_txlate_t *, 318 struct mode_info_power_cond *, int, int *, int *, int *); 319 static int sata_mode_select_page_1c(sata_pkt_txlate_t *, 320 struct mode_info_excpt_page *, int, int *, int *, int *); 321 static int sata_mode_select_page_30(sata_pkt_txlate_t *, 322 struct mode_acoustic_management *, int, int *, int *, int *); 323 324 static int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *); 325 static int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *, 326 sata_hba_inst_t *); 327 static int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *, 328 sata_hba_inst_t *); 329 static int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *, 330 sata_hba_inst_t *); 331 static int sata_build_lsense_page_0e(sata_drive_info_t *, uint8_t *, 332 sata_pkt_txlate_t *); 333 334 static void sata_set_arq_data(sata_pkt_t *); 335 static void sata_build_read_verify_cmd(sata_cmd_t *, uint16_t, uint64_t); 336 static void sata_build_generic_cmd(sata_cmd_t *, uint8_t); 337 static uint8_t sata_get_standby_timer(uint8_t *timer); 338 339 static void sata_save_drive_settings(sata_drive_info_t *); 340 static void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *); 341 static void sata_show_pmult_info(sata_hba_inst_t *, sata_device_t *); 342 static void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...); 343 static void sata_trace_log(sata_hba_inst_t *, uint_t, const char *fmt, ...); 344 static int sata_fetch_smart_return_status(sata_hba_inst_t *, 345 sata_drive_info_t *); 346 static int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *, 347 struct smart_data *); 348 static int sata_smart_selftest_log(sata_hba_inst_t *, 349 sata_drive_info_t *, 350 struct smart_selftest_log *); 351 static int sata_ext_smart_selftest_read_log(sata_hba_inst_t *, 352 sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t); 353 static int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *, 354 uint8_t *, uint8_t, uint8_t); 355 static int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *, 356 struct read_log_ext_directory *); 357 static void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int); 358 static void sata_xlate_errors(sata_pkt_txlate_t *); 359 static void sata_decode_device_error(sata_pkt_txlate_t *, 360 struct scsi_extended_sense *); 361 static void sata_set_device_removed(dev_info_t *); 362 static boolean_t sata_check_device_removed(dev_info_t *); 363 static void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *); 364 static int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *, 365 sata_drive_info_t *); 366 static int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *, 367 sata_drive_info_t *); 368 static void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *); 369 static void sata_fixed_sense_data_preset(struct scsi_extended_sense *); 370 static void sata_target_devid_register(dev_info_t *, sata_drive_info_t *); 371 static int sata_check_modser(char *, int); 372 373 374 375 /* 376 * SATA Framework will ignore SATA HBA driver cb_ops structure and 377 * register following one with SCSA framework. 378 * Open & close are provided, so scsi framework will not use its own 379 */ 380 static struct cb_ops sata_cb_ops = { 381 sata_hba_open, /* open */ 382 sata_hba_close, /* close */ 383 nodev, /* strategy */ 384 nodev, /* print */ 385 nodev, /* dump */ 386 nodev, /* read */ 387 nodev, /* write */ 388 sata_hba_ioctl, /* ioctl */ 389 nodev, /* devmap */ 390 nodev, /* mmap */ 391 nodev, /* segmap */ 392 nochpoll, /* chpoll */ 393 ddi_prop_op, /* cb_prop_op */ 394 0, /* streamtab */ 395 D_NEW | D_MP, /* cb_flag */ 396 CB_REV, /* rev */ 397 nodev, /* aread */ 398 nodev /* awrite */ 399 }; 400 401 402 extern struct mod_ops mod_miscops; 403 extern uchar_t scsi_cdb_size[]; 404 405 static struct modlmisc modlmisc = { 406 &mod_miscops, /* Type of module */ 407 "SATA Module" /* module name */ 408 }; 409 410 411 static struct modlinkage modlinkage = { 412 MODREV_1, 413 (void *)&modlmisc, 414 NULL 415 }; 416 417 /* 418 * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero, 419 * i.e. when scsi_pkt has not timeout specified. 420 */ 421 static int sata_default_pkt_time = 60; /* 60 seconds */ 422 423 /* 424 * Intermediate buffer device access attributes - they are required, 425 * but not necessarily used. 426 */ 427 static ddi_device_acc_attr_t sata_acc_attr = { 428 DDI_DEVICE_ATTR_V0, 429 DDI_STRUCTURE_LE_ACC, 430 DDI_STRICTORDER_ACC 431 }; 432 433 434 /* 435 * Mutexes protecting structures in multithreaded operations. 436 * Because events are relatively rare, a single global mutex protecting 437 * data structures should be sufficient. To increase performance, add 438 * separate mutex per each sata port and use global mutex only to protect 439 * common data structures. 440 */ 441 static kmutex_t sata_mutex; /* protects sata_hba_list */ 442 static kmutex_t sata_log_mutex; /* protects log */ 443 444 static char sata_log_buf[256]; 445 446 /* 447 * sata trace debug 448 */ 449 static sata_trace_rbuf_t *sata_debug_rbuf; 450 static sata_trace_dmsg_t *sata_trace_dmsg_alloc(void); 451 static void sata_trace_dmsg_free(void); 452 static void sata_trace_rbuf_alloc(void); 453 static void sata_trace_rbuf_free(void); 454 455 int dmsg_ring_size = DMSG_RING_SIZE; 456 457 /* Default write cache setting for SATA hard disks */ 458 int sata_write_cache = 1; /* enabled */ 459 460 /* Default write cache setting for SATA ATAPI CD/DVD */ 461 int sata_atapicdvd_write_cache = 1; /* enabled */ 462 463 /* Default write cache setting for SATA ATAPI tape */ 464 int sata_atapitape_write_cache = 1; /* enabled */ 465 466 /* Default write cache setting for SATA ATAPI disk */ 467 int sata_atapidisk_write_cache = 1; /* enabled */ 468 469 /* 470 * Linked list of HBA instances 471 */ 472 static sata_hba_inst_t *sata_hba_list = NULL; 473 static sata_hba_inst_t *sata_hba_list_tail = NULL; 474 /* 475 * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran 476 * structure and in sata soft state. 477 */ 478 479 /* 480 * Event daemon related variables 481 */ 482 static kmutex_t sata_event_mutex; 483 static kcondvar_t sata_event_cv; 484 static kthread_t *sata_event_thread = NULL; 485 static int sata_event_thread_terminate = 0; 486 static int sata_event_pending = 0; 487 static int sata_event_thread_active = 0; 488 extern pri_t minclsyspri; 489 490 /* 491 * NCQ error recovery command 492 */ 493 static const sata_cmd_t sata_rle_cmd = { 494 SATA_CMD_REV, 495 NULL, 496 { 497 SATA_DIR_READ 498 }, 499 ATA_ADDR_LBA48, 500 0, 501 0, 502 0, 503 0, 504 0, 505 1, 506 READ_LOG_EXT_NCQ_ERROR_RECOVERY, 507 0, 508 0, 509 0, 510 SATAC_READ_LOG_EXT, 511 0, 512 0, 513 0, 514 }; 515 516 /* 517 * ATAPI error recovery CDB 518 */ 519 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = { 520 SCMD_REQUEST_SENSE, 521 0, /* Only fixed RQ format is supported */ 522 0, 523 0, 524 SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */ 525 0 526 }; 527 528 529 /* Warlock directives */ 530 531 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran)) 532 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device)) 533 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops)) 534 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense)) 535 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status)) 536 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr)) 537 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t)) 538 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state)) 539 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state)) 540 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list)) 541 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list)) 542 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next)) 543 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev)) 544 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \ 545 sata_hba_inst::satahba_scsi_tran)) 546 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran)) 547 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip)) 548 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached)) 549 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port)) 550 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex, 551 sata_hba_inst::satahba_event_flags)) 552 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 553 sata_cport_info::cport_devp)) 554 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp)) 555 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr)) 556 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 557 sata_cport_info::cport_dev_type)) 558 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type)) 559 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 560 sata_cport_info::cport_state)) 561 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state)) 562 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 563 sata_pmport_info::pmport_state)) 564 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state)) 565 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 566 sata_pmport_info::pmport_dev_type)) 567 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type)) 568 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 569 sata_pmport_info::pmport_sata_drive)) 570 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 571 sata_pmport_info::pmport_tgtnode_clean)) 572 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 573 sata_pmport_info::pmport_event_flags)) 574 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive)) 575 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port)) 576 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports)) 577 #ifdef SATA_DEBUG 578 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count)) 579 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count)) 580 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace)) 581 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index)) 582 #endif 583 584 /* End of warlock directives */ 585 586 /* ************** loadable module configuration functions ************** */ 587 588 int 589 _init() 590 { 591 int rval; 592 593 mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL); 594 mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL); 595 mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL); 596 cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL); 597 sata_trace_rbuf_alloc(); 598 if ((rval = mod_install(&modlinkage)) != 0) { 599 #ifdef SATA_DEBUG 600 cmn_err(CE_WARN, "sata: _init: mod_install failed\n"); 601 #endif 602 sata_trace_rbuf_free(); 603 mutex_destroy(&sata_log_mutex); 604 cv_destroy(&sata_event_cv); 605 mutex_destroy(&sata_event_mutex); 606 mutex_destroy(&sata_mutex); 607 } 608 return (rval); 609 } 610 611 int 612 _fini() 613 { 614 int rval; 615 616 if ((rval = mod_remove(&modlinkage)) != 0) 617 return (rval); 618 619 sata_trace_rbuf_free(); 620 mutex_destroy(&sata_log_mutex); 621 cv_destroy(&sata_event_cv); 622 mutex_destroy(&sata_event_mutex); 623 mutex_destroy(&sata_mutex); 624 return (rval); 625 } 626 627 int 628 _info(struct modinfo *modinfop) 629 { 630 return (mod_info(&modlinkage, modinfop)); 631 } 632 633 634 635 /* ********************* SATA HBA entry points ********************* */ 636 637 638 /* 639 * Called by SATA HBA from _init(). 640 * Registers HBA driver instance/sata framework pair with scsi framework, by 641 * calling scsi_hba_init(). 642 * 643 * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used 644 * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver 645 * cb_ops pointer in SATA HBA driver dev_ops structure. 646 * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors. 647 * 648 * Return status of the scsi_hba_init() is returned to a calling SATA HBA 649 * driver. 650 */ 651 int 652 sata_hba_init(struct modlinkage *modlp) 653 { 654 int rval; 655 struct dev_ops *hba_ops; 656 657 SATADBG1(SATA_DBG_HBA_IF, NULL, 658 "sata_hba_init: name %s \n", 659 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo); 660 /* 661 * Fill-up cb_ops and dev_ops when necessary 662 */ 663 hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops; 664 /* 665 * Provide pointer to SATA dev_ops 666 */ 667 hba_ops->devo_cb_ops = &sata_cb_ops; 668 669 /* 670 * Register SATA HBA with SCSI framework 671 */ 672 if ((rval = scsi_hba_init(modlp)) != 0) { 673 SATADBG1(SATA_DBG_HBA_IF, NULL, 674 "sata_hba_init: scsi hba init failed\n", NULL); 675 return (rval); 676 } 677 678 return (0); 679 } 680 681 682 /* HBA attach stages */ 683 #define HBA_ATTACH_STAGE_SATA_HBA_INST 1 684 #define HBA_ATTACH_STAGE_SCSI_ATTACHED 2 685 #define HBA_ATTACH_STAGE_SETUP 4 686 #define HBA_ATTACH_STAGE_LINKED 8 687 688 689 /* 690 * 691 * Called from SATA HBA driver's attach routine to attach an instance of 692 * the HBA. 693 * 694 * For DDI_ATTACH command: 695 * sata_hba_inst structure is allocated here and initialized with pointers to 696 * SATA framework implementation of required scsi tran functions. 697 * The scsi_tran's tran_hba_private field is used by SATA Framework to point 698 * to the soft structure (sata_hba_inst) allocated by SATA framework for 699 * SATA HBA instance related data. 700 * The scsi_tran's tran_hba_private field is used by SATA framework to 701 * store a pointer to per-HBA-instance of sata_hba_inst structure. 702 * The sata_hba_inst structure is cross-linked to scsi tran structure. 703 * Among other info, a pointer to sata_hba_tran structure is stored in 704 * sata_hba_inst. The sata_hba_inst structures for different HBA instances are 705 * linked together into the list, pointed to by sata_hba_list. 706 * On the first HBA instance attach the sata event thread is initialized. 707 * Attachment points are created for all SATA ports of the HBA being attached. 708 * All HBA instance's SATA ports are probed and type of plugged devices is 709 * determined. For each device of a supported type, a target node is created. 710 * 711 * DDI_SUCCESS is returned when attachment process is successful, 712 * DDI_FAILURE is returned otherwise. 713 * 714 * For DDI_RESUME command: 715 * Not implemented at this time (postponed until phase 2 of the development). 716 */ 717 int 718 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran, 719 ddi_attach_cmd_t cmd) 720 { 721 sata_hba_inst_t *sata_hba_inst; 722 scsi_hba_tran_t *scsi_tran = NULL; 723 int hba_attach_state = 0; 724 char taskq_name[MAXPATHLEN]; 725 726 SATADBG3(SATA_DBG_HBA_IF, NULL, 727 "sata_hba_attach: node %s (%s%d)\n", 728 ddi_node_name(dip), ddi_driver_name(dip), 729 ddi_get_instance(dip)); 730 731 if (cmd == DDI_RESUME) { 732 /* 733 * Postponed until phase 2 of the development 734 */ 735 return (DDI_FAILURE); 736 } 737 738 if (cmd != DDI_ATTACH) { 739 return (DDI_FAILURE); 740 } 741 742 /* cmd == DDI_ATTACH */ 743 744 if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) { 745 SATA_LOG_D((NULL, CE_WARN, 746 "sata_hba_attach: invalid sata_hba_tran")); 747 return (DDI_FAILURE); 748 } 749 /* 750 * Allocate and initialize SCSI tran structure. 751 * SATA copy of tran_bus_config is provided to create port nodes. 752 */ 753 scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP); 754 if (scsi_tran == NULL) 755 return (DDI_FAILURE); 756 /* 757 * Allocate soft structure for SATA HBA instance. 758 * There is a separate softstate for each HBA instance. 759 */ 760 sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP); 761 ASSERT(sata_hba_inst != NULL); /* this should not fail */ 762 mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL); 763 hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST; 764 765 /* 766 * scsi_trans's tran_hba_private is used by SATA Framework to point to 767 * soft structure allocated by SATA framework for 768 * SATA HBA instance related data. 769 */ 770 scsi_tran->tran_hba_private = sata_hba_inst; 771 scsi_tran->tran_tgt_private = NULL; 772 773 scsi_tran->tran_tgt_init = sata_scsi_tgt_init; 774 scsi_tran->tran_tgt_probe = sata_scsi_tgt_probe; 775 scsi_tran->tran_tgt_free = sata_scsi_tgt_free; 776 777 scsi_tran->tran_start = sata_scsi_start; 778 scsi_tran->tran_reset = sata_scsi_reset; 779 scsi_tran->tran_abort = sata_scsi_abort; 780 scsi_tran->tran_getcap = sata_scsi_getcap; 781 scsi_tran->tran_setcap = sata_scsi_setcap; 782 scsi_tran->tran_init_pkt = sata_scsi_init_pkt; 783 scsi_tran->tran_destroy_pkt = sata_scsi_destroy_pkt; 784 785 scsi_tran->tran_dmafree = sata_scsi_dmafree; 786 scsi_tran->tran_sync_pkt = sata_scsi_sync_pkt; 787 788 scsi_tran->tran_reset_notify = NULL; 789 scsi_tran->tran_get_bus_addr = NULL; 790 scsi_tran->tran_quiesce = NULL; 791 scsi_tran->tran_unquiesce = NULL; 792 scsi_tran->tran_bus_reset = NULL; 793 794 if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr, 795 scsi_tran, 0) != DDI_SUCCESS) { 796 #ifdef SATA_DEBUG 797 cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed", 798 ddi_driver_name(dip), ddi_get_instance(dip)); 799 #endif 800 goto fail; 801 } 802 hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED; 803 804 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) { 805 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, 806 "sata", 1) != DDI_PROP_SUCCESS) { 807 SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: " 808 "failed to create hba sata prop")); 809 goto fail; 810 } 811 } 812 813 /* 814 * Save pointers in hba instance soft state. 815 */ 816 sata_hba_inst->satahba_scsi_tran = scsi_tran; 817 sata_hba_inst->satahba_tran = sata_tran; 818 sata_hba_inst->satahba_dip = dip; 819 820 /* 821 * Create a task queue to handle emulated commands completion 822 * Use node name, dash, instance number as the queue name. 823 */ 824 taskq_name[0] = '\0'; 825 (void) strlcat(taskq_name, DEVI(dip)->devi_node_name, 826 sizeof (taskq_name)); 827 (void) snprintf(taskq_name + strlen(taskq_name), 828 sizeof (taskq_name) - strlen(taskq_name), 829 "-%d", DEVI(dip)->devi_instance); 830 sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1, 831 minclsyspri, 1, sata_tran->sata_tran_hba_num_cports * 4, 832 TASKQ_DYNAMIC); 833 834 hba_attach_state |= HBA_ATTACH_STAGE_SETUP; 835 836 /* 837 * Create events thread if not created yet. 838 */ 839 sata_event_thread_control(1); 840 841 /* 842 * Link this hba instance into the list. 843 */ 844 mutex_enter(&sata_mutex); 845 846 if (sata_hba_list == NULL) { 847 /* 848 * The first instance of HBA is attached. 849 * Set current/active default maximum NCQ/TCQ queue depth for 850 * all SATA devices. It is done here and now, to eliminate the 851 * possibility of the dynamic, programatic modification of the 852 * queue depth via global (and public) sata_max_queue_depth 853 * variable (this would require special handling in HBA drivers) 854 */ 855 sata_current_max_qdepth = sata_max_queue_depth; 856 if (sata_current_max_qdepth > 32) 857 sata_current_max_qdepth = 32; 858 else if (sata_current_max_qdepth < 1) 859 sata_current_max_qdepth = 1; 860 } 861 862 sata_hba_inst->satahba_next = NULL; 863 sata_hba_inst->satahba_prev = sata_hba_list_tail; 864 if (sata_hba_list == NULL) { 865 sata_hba_list = sata_hba_inst; 866 } 867 if (sata_hba_list_tail != NULL) { 868 sata_hba_list_tail->satahba_next = sata_hba_inst; 869 } 870 sata_hba_list_tail = sata_hba_inst; 871 mutex_exit(&sata_mutex); 872 hba_attach_state |= HBA_ATTACH_STAGE_LINKED; 873 874 /* 875 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl 876 * SATA HBA driver should not use its own open/close entry points. 877 * 878 * Make sure that instance number doesn't overflow 879 * when forming minor numbers. 880 */ 881 ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT)); 882 if (ddi_create_minor_node(dip, "devctl", S_IFCHR, 883 INST2DEVCTL(ddi_get_instance(dip)), 884 DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) { 885 #ifdef SATA_DEBUG 886 cmn_err(CE_WARN, "sata_hba_attach: " 887 "cannot create devctl minor node"); 888 #endif 889 goto fail; 890 } 891 892 893 /* 894 * Set-up kstats here, if necessary. 895 * (postponed until future phase of the development). 896 */ 897 898 /* 899 * Indicate that HBA is attached. This will enable events processing 900 * for this HBA. 901 */ 902 sata_hba_inst->satahba_attached = 1; 903 /* 904 * Probe controller ports. This operation will describe a current 905 * controller/port/multipliers/device configuration and will create 906 * attachment points. 907 * We may end-up with just a controller with no devices attached. 908 * For the ports with a supported device attached, device target nodes 909 * are created and devices are initialized. 910 */ 911 sata_probe_ports(sata_hba_inst); 912 913 return (DDI_SUCCESS); 914 915 fail: 916 if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) { 917 (void) sata_remove_hba_instance(dip); 918 if (sata_hba_list == NULL) 919 sata_event_thread_control(0); 920 } 921 922 if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) { 923 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata"); 924 taskq_destroy(sata_hba_inst->satahba_taskq); 925 } 926 927 if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED) 928 (void) scsi_hba_detach(dip); 929 930 if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) { 931 mutex_destroy(&sata_hba_inst->satahba_mutex); 932 kmem_free((void *)sata_hba_inst, 933 sizeof (struct sata_hba_inst)); 934 scsi_hba_tran_free(scsi_tran); 935 } 936 937 sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed", 938 ddi_driver_name(dip), ddi_get_instance(dip)); 939 940 return (DDI_FAILURE); 941 } 942 943 944 /* 945 * Called by SATA HBA from to detach an instance of the driver. 946 * 947 * For DDI_DETACH command: 948 * Free local structures allocated for SATA HBA instance during 949 * sata_hba_attach processing. 950 * 951 * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise. 952 * 953 * For DDI_SUSPEND command: 954 * Not implemented at this time (postponed until phase 2 of the development) 955 * Returnd DDI_SUCCESS. 956 * 957 * When the last HBA instance is detached, the event daemon is terminated. 958 * 959 * NOTE: Port multiplier is supported. 960 */ 961 int 962 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 963 { 964 dev_info_t *tdip; 965 sata_hba_inst_t *sata_hba_inst; 966 scsi_hba_tran_t *scsi_hba_tran; 967 sata_cport_info_t *cportinfo; 968 sata_pmult_info_t *pminfo; 969 sata_drive_info_t *sdinfo; 970 sata_device_t sdevice; 971 int ncport, npmport; 972 973 SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n", 974 ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip)); 975 976 switch (cmd) { 977 case DDI_DETACH: 978 979 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 980 return (DDI_FAILURE); 981 982 sata_hba_inst = scsi_hba_tran->tran_hba_private; 983 if (sata_hba_inst == NULL) 984 return (DDI_FAILURE); 985 986 if (scsi_hba_detach(dip) == DDI_FAILURE) { 987 sata_hba_inst->satahba_attached = 1; 988 return (DDI_FAILURE); 989 } 990 991 /* 992 * Free all target nodes - at this point 993 * devices should be at least offlined 994 * otherwise scsi_hba_detach() should not be called. 995 */ 996 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); 997 ncport++) { 998 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 999 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 1000 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 1001 if (sdinfo != NULL) { 1002 tdip = sata_get_target_dip(dip, 1003 ncport, 0); 1004 if (tdip != NULL) { 1005 if (ndi_devi_offline(tdip, 1006 NDI_DEVI_REMOVE) != 1007 NDI_SUCCESS) { 1008 SATA_LOG_D(( 1009 sata_hba_inst, 1010 CE_WARN, 1011 "sata_hba_detach: " 1012 "Target node not " 1013 "removed !")); 1014 return (DDI_FAILURE); 1015 } 1016 } 1017 } 1018 } else { /* SATA_DTYPE_PMULT */ 1019 mutex_enter(&cportinfo->cport_mutex); 1020 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 1021 1022 if (pminfo == NULL) { 1023 SATA_LOG_D((sata_hba_inst, CE_WARN, 1024 "sata_hba_detach: Port multiplier " 1025 "not ready yet!")); 1026 mutex_exit(&cportinfo->cport_mutex); 1027 return (DDI_FAILURE); 1028 } 1029 1030 /* 1031 * Detach would fail if removal of any of the 1032 * target nodes is failed - albeit in that 1033 * case some of them may have been removed. 1034 */ 1035 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 1036 sata_hba_inst, ncport); npmport++) { 1037 tdip = sata_get_target_dip(dip, ncport, 1038 npmport); 1039 if (tdip != NULL) { 1040 if (ndi_devi_offline(tdip, 1041 NDI_DEVI_REMOVE) != 1042 NDI_SUCCESS) { 1043 SATA_LOG_D(( 1044 sata_hba_inst, 1045 CE_WARN, 1046 "sata_hba_detach: " 1047 "Target node not " 1048 "removed !")); 1049 mutex_exit(&cportinfo-> 1050 cport_mutex); 1051 return (DDI_FAILURE); 1052 } 1053 } 1054 } 1055 mutex_exit(&cportinfo->cport_mutex); 1056 } 1057 } 1058 /* 1059 * Disable sata event daemon processing for this HBA 1060 */ 1061 sata_hba_inst->satahba_attached = 0; 1062 1063 /* 1064 * Remove event daemon thread, if it is last HBA instance. 1065 */ 1066 1067 mutex_enter(&sata_mutex); 1068 if (sata_hba_list->satahba_next == NULL) { 1069 mutex_exit(&sata_mutex); 1070 sata_event_thread_control(0); 1071 mutex_enter(&sata_mutex); 1072 } 1073 mutex_exit(&sata_mutex); 1074 1075 /* Remove this HBA instance from the HBA list */ 1076 sata_remove_hba_instance(dip); 1077 1078 /* 1079 * At this point there should be no target nodes attached. 1080 * Detach and destroy device and port info structures. 1081 */ 1082 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); 1083 ncport++) { 1084 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 1085 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 1086 sdinfo = 1087 cportinfo->cport_devp.cport_sata_drive; 1088 if (sdinfo != NULL) { 1089 /* Release device structure */ 1090 kmem_free(sdinfo, 1091 sizeof (sata_drive_info_t)); 1092 } 1093 /* Release cport info */ 1094 mutex_destroy(&cportinfo->cport_mutex); 1095 kmem_free(cportinfo, 1096 sizeof (sata_cport_info_t)); 1097 } else { /* SATA_DTYPE_PMULT */ 1098 sdevice.satadev_addr.cport = (uint8_t)ncport; 1099 sdevice.satadev_addr.qual = SATA_ADDR_PMULT; 1100 sata_free_pmult(sata_hba_inst, &sdevice); 1101 } 1102 } 1103 1104 scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran); 1105 1106 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata"); 1107 1108 taskq_destroy(sata_hba_inst->satahba_taskq); 1109 1110 mutex_destroy(&sata_hba_inst->satahba_mutex); 1111 kmem_free((void *)sata_hba_inst, 1112 sizeof (struct sata_hba_inst)); 1113 1114 return (DDI_SUCCESS); 1115 1116 case DDI_SUSPEND: 1117 /* 1118 * Postponed until phase 2 1119 */ 1120 return (DDI_FAILURE); 1121 1122 default: 1123 return (DDI_FAILURE); 1124 } 1125 } 1126 1127 1128 /* 1129 * Called by an HBA drive from _fini() routine. 1130 * Unregisters SATA HBA instance/SATA framework pair from the scsi framework. 1131 */ 1132 void 1133 sata_hba_fini(struct modlinkage *modlp) 1134 { 1135 SATADBG1(SATA_DBG_HBA_IF, NULL, 1136 "sata_hba_fini: name %s\n", 1137 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo); 1138 1139 scsi_hba_fini(modlp); 1140 } 1141 1142 1143 /* 1144 * Default open and close routine for sata_hba framework. 1145 * 1146 */ 1147 /* 1148 * Open devctl node. 1149 * 1150 * Returns: 1151 * 0 if node was open successfully, error code otherwise. 1152 * 1153 * 1154 */ 1155 1156 static int 1157 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp) 1158 { 1159 #ifndef __lock_lint 1160 _NOTE(ARGUNUSED(credp)) 1161 #endif 1162 int rv = 0; 1163 dev_info_t *dip; 1164 scsi_hba_tran_t *scsi_hba_tran; 1165 sata_hba_inst_t *sata_hba_inst; 1166 1167 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL); 1168 1169 if (otyp != OTYP_CHR) 1170 return (EINVAL); 1171 1172 dip = sata_devt_to_devinfo(*devp); 1173 if (dip == NULL) 1174 return (ENXIO); 1175 1176 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1177 return (ENXIO); 1178 1179 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1180 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0) 1181 return (ENXIO); 1182 1183 mutex_enter(&sata_mutex); 1184 if (flags & FEXCL) { 1185 if (sata_hba_inst->satahba_open_flag != 0) { 1186 rv = EBUSY; 1187 } else { 1188 sata_hba_inst->satahba_open_flag = 1189 SATA_DEVCTL_EXOPENED; 1190 } 1191 } else { 1192 if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) { 1193 rv = EBUSY; 1194 } else { 1195 sata_hba_inst->satahba_open_flag = 1196 SATA_DEVCTL_SOPENED; 1197 } 1198 } 1199 mutex_exit(&sata_mutex); 1200 1201 return (rv); 1202 } 1203 1204 1205 /* 1206 * Close devctl node. 1207 * Returns: 1208 * 0 if node was closed successfully, error code otherwise. 1209 * 1210 */ 1211 1212 static int 1213 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp) 1214 { 1215 #ifndef __lock_lint 1216 _NOTE(ARGUNUSED(credp)) 1217 _NOTE(ARGUNUSED(flag)) 1218 #endif 1219 dev_info_t *dip; 1220 scsi_hba_tran_t *scsi_hba_tran; 1221 sata_hba_inst_t *sata_hba_inst; 1222 1223 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL); 1224 1225 if (otyp != OTYP_CHR) 1226 return (EINVAL); 1227 1228 dip = sata_devt_to_devinfo(dev); 1229 if (dip == NULL) 1230 return (ENXIO); 1231 1232 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1233 return (ENXIO); 1234 1235 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1236 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0) 1237 return (ENXIO); 1238 1239 mutex_enter(&sata_mutex); 1240 sata_hba_inst->satahba_open_flag = 0; 1241 mutex_exit(&sata_mutex); 1242 return (0); 1243 } 1244 1245 1246 1247 /* 1248 * Standard IOCTL commands for SATA hotplugging. 1249 * Implemented DEVCTL_AP commands: 1250 * DEVCTL_AP_CONNECT 1251 * DEVCTL_AP_DISCONNECT 1252 * DEVCTL_AP_CONFIGURE 1253 * DEVCTL_UNCONFIGURE 1254 * DEVCTL_AP_CONTROL 1255 * 1256 * Commands passed to default ndi ioctl handler: 1257 * DEVCTL_DEVICE_GETSTATE 1258 * DEVCTL_DEVICE_ONLINE 1259 * DEVCTL_DEVICE_OFFLINE 1260 * DEVCTL_DEVICE_REMOVE 1261 * DEVCTL_DEVICE_INSERT 1262 * DEVCTL_BUS_GETSTATE 1263 * 1264 * All other cmds are passed to HBA if it provide ioctl handler, or failed 1265 * if not. 1266 * 1267 * Returns: 1268 * 0 if successful, 1269 * error code if operation failed. 1270 * 1271 * Port Multiplier support is supported now. 1272 * 1273 * NOTE: qual should be SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT 1274 */ 1275 1276 static int 1277 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, 1278 int *rvalp) 1279 { 1280 #ifndef __lock_lint 1281 _NOTE(ARGUNUSED(credp)) 1282 _NOTE(ARGUNUSED(rvalp)) 1283 #endif 1284 int rv = 0; 1285 int32_t comp_port = -1; 1286 dev_info_t *dip; 1287 devctl_ap_state_t ap_state; 1288 struct devctl_iocdata *dcp = NULL; 1289 scsi_hba_tran_t *scsi_hba_tran; 1290 sata_hba_inst_t *sata_hba_inst; 1291 sata_device_t sata_device; 1292 sata_cport_info_t *cportinfo; 1293 int cport, pmport, qual; 1294 int rval = SATA_SUCCESS; 1295 1296 dip = sata_devt_to_devinfo(dev); 1297 if (dip == NULL) 1298 return (ENXIO); 1299 1300 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1301 return (ENXIO); 1302 1303 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1304 if (sata_hba_inst == NULL) 1305 return (ENXIO); 1306 1307 if (sata_hba_inst->satahba_tran == NULL) 1308 return (ENXIO); 1309 1310 switch (cmd) { 1311 1312 case DEVCTL_DEVICE_GETSTATE: 1313 case DEVCTL_DEVICE_ONLINE: 1314 case DEVCTL_DEVICE_OFFLINE: 1315 case DEVCTL_DEVICE_REMOVE: 1316 case DEVCTL_BUS_GETSTATE: 1317 /* 1318 * There may be more cases that we want to pass to default 1319 * handler rather than fail them. 1320 */ 1321 return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0)); 1322 } 1323 1324 /* read devctl ioctl data */ 1325 if (cmd != DEVCTL_AP_CONTROL) { 1326 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) 1327 return (EFAULT); 1328 1329 if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) == 1330 -1) { 1331 if (dcp) 1332 ndi_dc_freehdl(dcp); 1333 return (EINVAL); 1334 } 1335 1336 /* 1337 * According to SCSI_TO_SATA_ADDR_QUAL, qual should be either 1338 * SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT. 1339 */ 1340 cport = SCSI_TO_SATA_CPORT(comp_port); 1341 pmport = SCSI_TO_SATA_PMPORT(comp_port); 1342 qual = SCSI_TO_SATA_ADDR_QUAL(comp_port); 1343 1344 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, 1345 qual) != 0) { 1346 ndi_dc_freehdl(dcp); 1347 return (EINVAL); 1348 } 1349 1350 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 1351 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1352 cport_mutex); 1353 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) { 1354 /* 1355 * Cannot process ioctl request now. Come back later. 1356 */ 1357 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1358 cport_mutex); 1359 ndi_dc_freehdl(dcp); 1360 return (EBUSY); 1361 } 1362 /* Block event processing for this port */ 1363 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 1364 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1365 1366 sata_device.satadev_addr.cport = cport; 1367 sata_device.satadev_addr.pmport = pmport; 1368 sata_device.satadev_addr.qual = qual; 1369 sata_device.satadev_rev = SATA_DEVICE_REV; 1370 } 1371 1372 switch (cmd) { 1373 1374 case DEVCTL_AP_DISCONNECT: 1375 1376 /* 1377 * Normally, cfgadm sata plugin will try to offline 1378 * (unconfigure) device before this request. Nevertheless, 1379 * if a device is still configured, we need to 1380 * attempt to offline and unconfigure device first, and we will 1381 * deactivate the port regardless of the unconfigure 1382 * operation results. 1383 * 1384 */ 1385 rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device); 1386 1387 break; 1388 1389 case DEVCTL_AP_UNCONFIGURE: 1390 1391 /* 1392 * The unconfigure operation uses generic nexus operation to 1393 * offline a device. It leaves a target device node attached. 1394 * and obviously sata_drive_info attached as well, because 1395 * from the hardware point of view nothing has changed. 1396 */ 1397 rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device); 1398 break; 1399 1400 case DEVCTL_AP_CONNECT: 1401 { 1402 /* 1403 * The sata cfgadm pluging will invoke this operation only if 1404 * port was found in the disconnect state (failed state 1405 * is also treated as the disconnected state). 1406 * If port activation is successful and a device is found 1407 * attached to the port, the initialization sequence is 1408 * executed to probe the port and attach 1409 * a device structure to a port structure. The device is not 1410 * set in configured state (system-wise) by this operation. 1411 */ 1412 1413 rv = sata_ioctl_connect(sata_hba_inst, &sata_device); 1414 1415 break; 1416 } 1417 1418 case DEVCTL_AP_CONFIGURE: 1419 { 1420 /* 1421 * A port may be in an active or shutdown state. 1422 * If port is in a failed state, operation is aborted. 1423 * If a port is in a shutdown state, sata_tran_port_activate() 1424 * is invoked prior to any other operation. 1425 * 1426 * Onlining the device involves creating a new target node. 1427 * If there is an old target node present (belonging to 1428 * previously removed device), the operation is aborted - the 1429 * old node has to be released and removed before configure 1430 * operation is attempted. 1431 */ 1432 1433 rv = sata_ioctl_configure(sata_hba_inst, &sata_device); 1434 1435 break; 1436 } 1437 1438 case DEVCTL_AP_GETSTATE: 1439 1440 sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state); 1441 1442 ap_state.ap_last_change = (time_t)-1; 1443 ap_state.ap_error_code = 0; 1444 ap_state.ap_in_transition = 0; 1445 1446 /* Copy the return AP-state information to the user space */ 1447 if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) { 1448 rv = EFAULT; 1449 } 1450 break; 1451 1452 case DEVCTL_AP_CONTROL: 1453 { 1454 /* 1455 * Generic devctl for hardware specific functionality 1456 */ 1457 sata_ioctl_data_t ioc; 1458 1459 ASSERT(dcp == NULL); 1460 1461 /* Copy in user ioctl data first */ 1462 #ifdef _MULTI_DATAMODEL 1463 if (ddi_model_convert_from(mode & FMODELS) == 1464 DDI_MODEL_ILP32) { 1465 1466 sata_ioctl_data_32_t ioc32; 1467 1468 if (ddi_copyin((void *)arg, (void *)&ioc32, 1469 sizeof (ioc32), mode) != 0) { 1470 rv = EFAULT; 1471 break; 1472 } 1473 ioc.cmd = (uint_t)ioc32.cmd; 1474 ioc.port = (uint_t)ioc32.port; 1475 ioc.get_size = (uint_t)ioc32.get_size; 1476 ioc.buf = (caddr_t)(uintptr_t)ioc32.buf; 1477 ioc.bufsiz = (uint_t)ioc32.bufsiz; 1478 ioc.misc_arg = (uint_t)ioc32.misc_arg; 1479 } else 1480 #endif /* _MULTI_DATAMODEL */ 1481 if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc), 1482 mode) != 0) { 1483 return (EFAULT); 1484 } 1485 1486 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 1487 "sata_hba_ioctl: DEVCTL_AP_CONTROL " 1488 "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port); 1489 1490 /* 1491 * To avoid BE/LE and 32/64 issues, a get_size always returns 1492 * a 32-bit number. 1493 */ 1494 if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) { 1495 return (EINVAL); 1496 } 1497 /* validate address */ 1498 cport = SCSI_TO_SATA_CPORT(ioc.port); 1499 pmport = SCSI_TO_SATA_PMPORT(ioc.port); 1500 qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port); 1501 1502 SATADBG3(SATA_DBG_IOCTL_IF, sata_hba_inst, 1503 "sata_hba_ioctl: target port is %d:%d (%d)", 1504 cport, pmport, qual); 1505 1506 if (sata_validate_sata_address(sata_hba_inst, cport, 1507 pmport, qual) != 0) 1508 return (EINVAL); 1509 1510 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 1511 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1512 cport_mutex); 1513 /* Is the port locked by event processing daemon ? */ 1514 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) { 1515 /* 1516 * Cannot process ioctl request now. Come back later 1517 */ 1518 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1519 cport_mutex); 1520 return (EBUSY); 1521 } 1522 /* Block event processing for this port */ 1523 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 1524 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1525 1526 1527 sata_device.satadev_addr.cport = cport; 1528 sata_device.satadev_addr.pmport = pmport; 1529 sata_device.satadev_addr.qual = qual; 1530 sata_device.satadev_rev = SATA_DEVICE_REV; 1531 1532 switch (ioc.cmd) { 1533 1534 case SATA_CFGA_RESET_PORT: 1535 /* 1536 * There is no protection for configured device. 1537 */ 1538 rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device); 1539 break; 1540 1541 case SATA_CFGA_RESET_DEVICE: 1542 /* 1543 * There is no protection for configured device. 1544 */ 1545 rv = sata_ioctl_reset_device(sata_hba_inst, 1546 &sata_device); 1547 break; 1548 1549 case SATA_CFGA_RESET_ALL: 1550 /* 1551 * There is no protection for configured devices. 1552 */ 1553 rv = sata_ioctl_reset_all(sata_hba_inst); 1554 /* 1555 * We return here, because common return is for 1556 * a single port operation - we have already unlocked 1557 * all ports and no dc handle was allocated. 1558 */ 1559 return (rv); 1560 1561 case SATA_CFGA_PORT_DEACTIVATE: 1562 /* 1563 * Arbitrarily unconfigure attached device, if any. 1564 * Even if the unconfigure fails, proceed with the 1565 * port deactivation. 1566 */ 1567 rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device); 1568 1569 break; 1570 1571 case SATA_CFGA_PORT_ACTIVATE: 1572 1573 rv = sata_ioctl_activate(sata_hba_inst, &sata_device); 1574 break; 1575 1576 case SATA_CFGA_PORT_SELF_TEST: 1577 1578 rv = sata_ioctl_port_self_test(sata_hba_inst, 1579 &sata_device); 1580 break; 1581 1582 case SATA_CFGA_GET_DEVICE_PATH: 1583 1584 rv = sata_ioctl_get_device_path(sata_hba_inst, 1585 &sata_device, &ioc, mode); 1586 break; 1587 1588 case SATA_CFGA_GET_AP_TYPE: 1589 1590 rv = sata_ioctl_get_ap_type(sata_hba_inst, 1591 &sata_device, &ioc, mode); 1592 break; 1593 1594 case SATA_CFGA_GET_MODEL_INFO: 1595 1596 rv = sata_ioctl_get_model_info(sata_hba_inst, 1597 &sata_device, &ioc, mode); 1598 break; 1599 1600 case SATA_CFGA_GET_REVFIRMWARE_INFO: 1601 1602 rv = sata_ioctl_get_revfirmware_info(sata_hba_inst, 1603 &sata_device, &ioc, mode); 1604 break; 1605 1606 case SATA_CFGA_GET_SERIALNUMBER_INFO: 1607 1608 rv = sata_ioctl_get_serialnumber_info(sata_hba_inst, 1609 &sata_device, &ioc, mode); 1610 break; 1611 1612 default: 1613 rv = EINVAL; 1614 break; 1615 1616 } /* End of DEVCTL_AP_CONTROL cmd switch */ 1617 1618 break; 1619 } 1620 1621 default: 1622 { 1623 /* 1624 * If we got here, we got an IOCTL that SATA HBA Framework 1625 * does not recognize. Pass ioctl to HBA driver, in case 1626 * it could process it. 1627 */ 1628 sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran; 1629 dev_info_t *mydip = SATA_DIP(sata_hba_inst); 1630 1631 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 1632 "IOCTL 0x%2x not supported in SATA framework, " 1633 "passthrough to HBA", cmd); 1634 1635 if (sata_tran->sata_tran_ioctl == NULL) { 1636 rv = EINVAL; 1637 break; 1638 } 1639 rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg); 1640 if (rval != 0) { 1641 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 1642 "IOCTL 0x%2x failed in HBA", cmd); 1643 rv = rval; 1644 } 1645 break; 1646 } 1647 1648 } /* End of main IOCTL switch */ 1649 1650 if (dcp) { 1651 ndi_dc_freehdl(dcp); 1652 } 1653 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1654 cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY; 1655 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1656 1657 return (rv); 1658 } 1659 1660 1661 /* 1662 * Create error retrieval sata packet 1663 * 1664 * A sata packet is allocated and set-up to contain specified error retrieval 1665 * command and appropriate dma-able data buffer. 1666 * No association with any scsi packet is made and no callback routine is 1667 * specified. 1668 * 1669 * Returns a pointer to sata packet upon successfull packet creation. 1670 * Returns NULL, if packet cannot be created. 1671 */ 1672 sata_pkt_t * 1673 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device, 1674 int pkt_type) 1675 { 1676 sata_hba_inst_t *sata_hba_inst; 1677 sata_pkt_txlate_t *spx; 1678 sata_pkt_t *spkt; 1679 sata_drive_info_t *sdinfo; 1680 1681 mutex_enter(&sata_mutex); 1682 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 1683 sata_hba_inst = sata_hba_inst->satahba_next) { 1684 if (SATA_DIP(sata_hba_inst) == dip) 1685 break; 1686 } 1687 mutex_exit(&sata_mutex); 1688 ASSERT(sata_hba_inst != NULL); 1689 1690 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 1691 if (sdinfo == NULL) { 1692 sata_log(sata_hba_inst, CE_WARN, 1693 "sata: error recovery request for non-attached device at " 1694 "cport %d", sata_device->satadev_addr.cport); 1695 return (NULL); 1696 } 1697 1698 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 1699 spx->txlt_sata_hba_inst = sata_hba_inst; 1700 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 1701 spkt = sata_pkt_alloc(spx, NULL); 1702 if (spkt == NULL) { 1703 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1704 return (NULL); 1705 } 1706 /* address is needed now */ 1707 spkt->satapkt_device.satadev_addr = sata_device->satadev_addr; 1708 1709 switch (pkt_type) { 1710 case SATA_ERR_RETR_PKT_TYPE_NCQ: 1711 if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) 1712 return (spkt); 1713 break; 1714 1715 case SATA_ERR_RETR_PKT_TYPE_ATAPI: 1716 if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) 1717 return (spkt); 1718 break; 1719 1720 default: 1721 break; 1722 } 1723 1724 sata_pkt_free(spx); 1725 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1726 return (NULL); 1727 1728 } 1729 1730 1731 /* 1732 * Free error retrieval sata packet 1733 * 1734 * Free sata packet and any associated resources allocated previously by 1735 * sata_get_error_retrieval_pkt(). 1736 * 1737 * Void return. 1738 */ 1739 void 1740 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt) 1741 { 1742 sata_pkt_txlate_t *spx = 1743 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 1744 1745 ASSERT(sata_pkt != NULL); 1746 1747 sata_free_local_buffer(spx); 1748 sata_pkt_free(spx); 1749 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1750 1751 } 1752 1753 /* 1754 * Create READ PORT MULTIPLIER and WRITE PORT MULTIPLIER sata packet 1755 * 1756 * No association with any scsi packet is made and no callback routine is 1757 * specified. 1758 * 1759 * Returns a pointer to sata packet upon successfull packet creation. 1760 * Returns NULL, if packet cannot be created. 1761 * 1762 * NOTE: Input/Output value includes 64 bits accoring to SATA Spec 2.6, 1763 * only lower 32 bits are available currently. 1764 */ 1765 sata_pkt_t * 1766 sata_get_rdwr_pmult_pkt(dev_info_t *dip, sata_device_t *sd, 1767 uint8_t regn, uint32_t regv, uint32_t type) 1768 { 1769 sata_hba_inst_t *sata_hba_inst; 1770 sata_pkt_txlate_t *spx; 1771 sata_pkt_t *spkt; 1772 sata_cmd_t *scmd; 1773 1774 /* Only READ/WRITE commands are accepted. */ 1775 ASSERT(type == SATA_RDWR_PMULT_PKT_TYPE_READ || 1776 type == SATA_RDWR_PMULT_PKT_TYPE_WRITE); 1777 1778 mutex_enter(&sata_mutex); 1779 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 1780 sata_hba_inst = sata_hba_inst->satahba_next) { 1781 if (SATA_DIP(sata_hba_inst) == dip) 1782 break; 1783 } 1784 mutex_exit(&sata_mutex); 1785 ASSERT(sata_hba_inst != NULL); 1786 1787 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 1788 spx->txlt_sata_hba_inst = sata_hba_inst; 1789 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 1790 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 1791 if (spkt == NULL) { 1792 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1793 return (NULL); 1794 } 1795 1796 /* 1797 * NOTE: We need to send this command to the port multiplier, 1798 * that means send to SATA_PMULT_HOSTPORT(0xf) pmport 1799 * 1800 * sata_device contains the address of actual target device, and the 1801 * pmport number in the command comes from the sata_device structure. 1802 */ 1803 spkt->satapkt_device.satadev_addr = sd->satadev_addr; 1804 spkt->satapkt_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT; 1805 spkt->satapkt_device.satadev_addr.qual = SATA_ADDR_PMULT; 1806 1807 /* Fill sata_pkt */ 1808 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING; 1809 spkt->satapkt_comp = NULL; /* Synchronous mode, no callback */ 1810 spkt->satapkt_time = 10; /* Timeout 10s */ 1811 1812 /* Build READ PORT MULTIPLIER cmd in the sata_pkt */ 1813 scmd = &spkt->satapkt_cmd; 1814 scmd->satacmd_features_reg = regn & 0xff; 1815 scmd->satacmd_features_reg_ext = (regn >> 8) & 0xff; 1816 scmd->satacmd_device_reg = sd->satadev_addr.pmport; 1817 scmd->satacmd_addr_type = 0; /* N/A */ 1818 1819 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 1820 1821 if (type == SATA_RDWR_PMULT_PKT_TYPE_READ) { 1822 scmd->satacmd_cmd_reg = SATAC_READ_PORTMULT; 1823 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 1824 scmd->satacmd_flags.sata_special_regs = 1; 1825 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1; 1826 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1; 1827 scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1; 1828 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1; 1829 } else if (type == SATA_RDWR_PMULT_PKT_TYPE_WRITE) { 1830 scmd->satacmd_cmd_reg = SATAC_WRITE_PORTMULT; 1831 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 1832 scmd->satacmd_sec_count_lsb = regv & 0xff; 1833 scmd->satacmd_lba_low_lsb = regv >> 8 & 0xff; 1834 scmd->satacmd_lba_mid_lsb = regv >> 16 & 0xff; 1835 scmd->satacmd_lba_high_lsb = regv >> 24 & 0xff; 1836 } 1837 1838 return (spkt); 1839 } 1840 1841 /* 1842 * Free sata packet and any associated resources allocated previously by 1843 * sata_get_rdwr_pmult_pkt(). 1844 * 1845 * Void return. 1846 */ 1847 void 1848 sata_free_rdwr_pmult_pkt(sata_pkt_t *sata_pkt) 1849 { 1850 sata_pkt_txlate_t *spx = 1851 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 1852 1853 /* Free allocated resources */ 1854 sata_pkt_free(spx); 1855 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1856 } 1857 1858 /* 1859 * Search a port multiplier in the blacklist and update the flags if a match 1860 * is found. 1861 * 1862 * Returns: 1863 * SATA_SUCCESS if any matched entry is found. 1864 * SATA_FAILURE if no matched entry is found. 1865 */ 1866 int 1867 sata_check_pmult_blacklist(sata_device_t *sd) 1868 { 1869 sata_pmult_bl_t *blp; 1870 for (blp = sata_pmult_blacklist; blp->bl_gscr0; blp++) { 1871 if (sd->satadev_gscr.gscr0 != blp->bl_gscr0 && blp->bl_gscr0) 1872 continue; 1873 if (sd->satadev_gscr.gscr1 != blp->bl_gscr1 && blp->bl_gscr1) 1874 continue; 1875 if (sd->satadev_gscr.gscr2 != blp->bl_gscr2 && blp->bl_gscr2) 1876 continue; 1877 1878 cmn_err(CE_WARN, "!Port multiplier is on the blacklist."); 1879 sd->satadev_add_info = blp->bl_flags; 1880 return (SATA_SUCCESS); 1881 } 1882 return (SATA_FAILURE); 1883 } 1884 1885 /* 1886 * sata_name_child is for composing the name of the node 1887 * the format of the name is "target,0". 1888 */ 1889 static int 1890 sata_name_child(dev_info_t *dip, char *name, int namelen) 1891 { 1892 int target; 1893 1894 target = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 1895 DDI_PROP_DONTPASS, "target", -1); 1896 if (target == -1) 1897 return (DDI_FAILURE); 1898 (void) snprintf(name, namelen, "%x,0", target); 1899 return (DDI_SUCCESS); 1900 } 1901 1902 1903 1904 /* ****************** SCSA required entry points *********************** */ 1905 1906 /* 1907 * Implementation of scsi tran_tgt_init. 1908 * sata_scsi_tgt_init() initializes scsi_device structure 1909 * 1910 * If successful, DDI_SUCCESS is returned. 1911 * DDI_FAILURE is returned if addressed device does not exist 1912 */ 1913 1914 static int 1915 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip, 1916 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 1917 { 1918 #ifndef __lock_lint 1919 _NOTE(ARGUNUSED(hba_dip)) 1920 _NOTE(ARGUNUSED(tgt_dip)) 1921 #endif 1922 sata_device_t sata_device; 1923 sata_drive_info_t *sdinfo; 1924 struct sata_id *sid; 1925 sata_hba_inst_t *sata_hba_inst; 1926 char model[SATA_ID_MODEL_LEN + 1]; 1927 char fw[SATA_ID_FW_LEN + 1]; 1928 char *vid, *pid; 1929 int i; 1930 1931 /* 1932 * Fail tran_tgt_init for .conf stub node 1933 */ 1934 if (ndi_dev_is_persistent_node(tgt_dip) == 0) { 1935 (void) ndi_merge_node(tgt_dip, sata_name_child); 1936 ddi_set_name_addr(tgt_dip, NULL); 1937 return (DDI_FAILURE); 1938 } 1939 1940 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 1941 1942 /* Validate scsi device address */ 1943 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 1944 &sata_device) != 0) 1945 return (DDI_FAILURE); 1946 1947 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 1948 sata_device.satadev_addr.cport))); 1949 1950 /* sata_device now contains a valid sata address */ 1951 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 1952 if (sdinfo == NULL) { 1953 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1954 sata_device.satadev_addr.cport))); 1955 return (DDI_FAILURE); 1956 } 1957 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1958 sata_device.satadev_addr.cport))); 1959 1960 /* 1961 * Check if we need to create a legacy devid (i.e cmdk style) for 1962 * the target disks. 1963 * 1964 * HBA devinfo node will have the property "use-cmdk-devid-format" 1965 * if we need to create cmdk-style devid for all the disk devices 1966 * attached to this controller. This property may have been set 1967 * from HBA driver's .conf file or by the HBA driver in its 1968 * attach(9F) function. 1969 */ 1970 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 1971 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS, 1972 "use-cmdk-devid-format", 0) == 1)) { 1973 /* register a legacy devid for this target node */ 1974 sata_target_devid_register(tgt_dip, sdinfo); 1975 } 1976 1977 1978 /* 1979 * 'Identify Device Data' does not always fit in standard SCSI 1980 * INQUIRY data, so establish INQUIRY_* properties with full-form 1981 * of information. 1982 */ 1983 sid = &sdinfo->satadrv_id; 1984 #ifdef _LITTLE_ENDIAN 1985 swab(sid->ai_model, model, SATA_ID_MODEL_LEN); 1986 swab(sid->ai_fw, fw, SATA_ID_FW_LEN); 1987 #else /* _LITTLE_ENDIAN */ 1988 bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN); 1989 bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN); 1990 #endif /* _LITTLE_ENDIAN */ 1991 model[SATA_ID_MODEL_LEN] = 0; 1992 fw[SATA_ID_FW_LEN] = 0; 1993 1994 /* split model into into vid/pid */ 1995 for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++) 1996 if ((*pid == ' ') || (*pid == '\t')) 1997 break; 1998 if (i < SATA_ID_MODEL_LEN) { 1999 vid = model; 2000 *pid++ = 0; /* terminate vid, establish pid */ 2001 } else { 2002 vid = NULL; /* vid will stay "ATA " */ 2003 pid = model; /* model is all pid */ 2004 } 2005 2006 if (vid) 2007 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_VENDOR_ID, 2008 vid, strlen(vid)); 2009 if (pid) 2010 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID, 2011 pid, strlen(pid)); 2012 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_REVISION_ID, 2013 fw, strlen(fw)); 2014 2015 return (DDI_SUCCESS); 2016 } 2017 2018 /* 2019 * Implementation of scsi tran_tgt_probe. 2020 * Probe target, by calling default scsi routine scsi_hba_probe() 2021 */ 2022 static int 2023 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void)) 2024 { 2025 sata_hba_inst_t *sata_hba_inst = 2026 (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private); 2027 int rval; 2028 uint32_t pm_cap; 2029 2030 rval = scsi_hba_probe(sd, callback); 2031 pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE | 2032 SATA_CAP_LOG_SENSE; 2033 2034 if (rval == SCSIPROBE_EXISTS) { 2035 /* 2036 * Set property "pm-capable" on the target device node, so that 2037 * the target driver will not try to fetch scsi cycle counters 2038 * before enabling device power-management. 2039 */ 2040 if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev, 2041 "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) { 2042 sata_log(sata_hba_inst, CE_WARN, 2043 "SATA device at port %d: " 2044 "will not be power-managed ", 2045 SCSI_TO_SATA_CPORT(sd->sd_address.a_target)); 2046 SATA_LOG_D((sata_hba_inst, CE_WARN, 2047 "failure updating pm-capable property")); 2048 } 2049 } 2050 return (rval); 2051 } 2052 2053 /* 2054 * Implementation of scsi tran_tgt_free. 2055 * Release all resources allocated for scsi_device 2056 */ 2057 static void 2058 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip, 2059 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 2060 { 2061 #ifndef __lock_lint 2062 _NOTE(ARGUNUSED(hba_dip)) 2063 #endif 2064 sata_device_t sata_device; 2065 sata_drive_info_t *sdinfo; 2066 sata_hba_inst_t *sata_hba_inst; 2067 ddi_devid_t devid; 2068 2069 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 2070 2071 /* Validate scsi device address */ 2072 /* 2073 * Note: tgt_free relates to the SCSA view of a device. If called, there 2074 * was a device at this address, so even if the sata framework internal 2075 * resources were alredy released because a device was detached, 2076 * this function should be executed as long as its actions do 2077 * not require the internal sata view of a device and the address 2078 * refers to a valid sata address. 2079 * Validating the address here means that we do not trust SCSA... 2080 */ 2081 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 2082 &sata_device) == -1) 2083 return; 2084 2085 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2086 sata_device.satadev_addr.cport))); 2087 2088 /* sata_device now should contain a valid sata address */ 2089 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 2090 if (sdinfo == NULL) { 2091 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2092 sata_device.satadev_addr.cport))); 2093 return; 2094 } 2095 /* 2096 * We did not allocate any resources in sata_scsi_tgt_init() 2097 * other than few properties. 2098 * Free them. 2099 */ 2100 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2101 sata_device.satadev_addr.cport))); 2102 (void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable"); 2103 2104 /* 2105 * If devid was previously created but not freed up from 2106 * sd(7D) driver (i.e during detach(9F)) then do it here. 2107 */ 2108 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 2109 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS, 2110 "use-cmdk-devid-format", 0) == 1) && 2111 (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) { 2112 ddi_devid_unregister(tgt_dip); 2113 ddi_devid_free(devid); 2114 } 2115 } 2116 2117 /* 2118 * Implementation of scsi tran_init_pkt 2119 * Upon successful return, scsi pkt buffer has DMA resources allocated. 2120 * 2121 * It seems that we should always allocate pkt, even if the address is 2122 * for non-existing device - just use some default for dma_attr. 2123 * The reason is that there is no way to communicate this to a caller here. 2124 * Subsequent call to sata_scsi_start may fail appropriately. 2125 * Simply returning NULL does not seem to discourage a target driver... 2126 * 2127 * Returns a pointer to initialized scsi_pkt, or NULL otherwise. 2128 */ 2129 static struct scsi_pkt * 2130 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt, 2131 struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags, 2132 int (*callback)(caddr_t), caddr_t arg) 2133 { 2134 sata_hba_inst_t *sata_hba_inst = 2135 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2136 dev_info_t *dip = SATA_DIP(sata_hba_inst); 2137 sata_device_t sata_device; 2138 sata_drive_info_t *sdinfo; 2139 sata_pkt_txlate_t *spx; 2140 ddi_dma_attr_t cur_dma_attr; 2141 int rval; 2142 boolean_t new_pkt = TRUE; 2143 2144 ASSERT(ap->a_hba_tran->tran_hba_dip == dip); 2145 2146 /* 2147 * We need to translate the address, even if it could be 2148 * a bogus one, for a non-existing device 2149 */ 2150 sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 2151 sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target); 2152 sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 2153 sata_device.satadev_rev = SATA_DEVICE_REV; 2154 2155 if (pkt == NULL) { 2156 /* 2157 * Have to allocate a brand new scsi packet. 2158 * We need to operate with auto request sense enabled. 2159 */ 2160 pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen, 2161 MAX(statuslen, sizeof (struct scsi_arq_status)), 2162 tgtlen, sizeof (sata_pkt_txlate_t), callback, arg); 2163 2164 if (pkt == NULL) 2165 return (NULL); 2166 2167 /* Fill scsi packet structure */ 2168 pkt->pkt_comp = (void (*)())NULL; 2169 pkt->pkt_time = 0; 2170 pkt->pkt_resid = 0; 2171 pkt->pkt_statistics = 0; 2172 pkt->pkt_reason = 0; 2173 2174 /* 2175 * pkt_hba_private will point to sata pkt txlate structure 2176 */ 2177 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2178 bzero(spx, sizeof (sata_pkt_txlate_t)); 2179 2180 spx->txlt_scsi_pkt = pkt; 2181 spx->txlt_sata_hba_inst = sata_hba_inst; 2182 2183 /* Allocate sata_pkt */ 2184 spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback); 2185 if (spx->txlt_sata_pkt == NULL) { 2186 /* Could not allocate sata pkt */ 2187 scsi_hba_pkt_free(ap, pkt); 2188 return (NULL); 2189 } 2190 /* Set sata address */ 2191 spx->txlt_sata_pkt->satapkt_device.satadev_addr = 2192 sata_device.satadev_addr; 2193 spx->txlt_sata_pkt->satapkt_device.satadev_rev = 2194 sata_device.satadev_rev; 2195 2196 if ((bp == NULL) || (bp->b_bcount == 0)) 2197 return (pkt); 2198 2199 spx->txlt_total_residue = bp->b_bcount; 2200 } else { 2201 new_pkt = FALSE; 2202 /* 2203 * Packet was preallocated/initialized by previous call 2204 */ 2205 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2206 2207 if ((bp == NULL) || (bp->b_bcount == 0)) { 2208 return (pkt); 2209 } 2210 2211 /* Pkt is available already: spx->txlt_scsi_pkt == pkt; */ 2212 } 2213 2214 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 2215 2216 /* 2217 * We use an adjusted version of the dma_attr, to account 2218 * for device addressing limitations. 2219 * sata_adjust_dma_attr() will handle sdinfo == NULL which may 2220 * happen when a device is not yet configured. 2221 */ 2222 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2223 sata_device.satadev_addr.cport))); 2224 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 2225 &spx->txlt_sata_pkt->satapkt_device); 2226 /* NULL sdinfo may be passsed to sata_adjust_dma_attr() */ 2227 sata_adjust_dma_attr(sdinfo, 2228 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 2229 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2230 sata_device.satadev_addr.cport))); 2231 /* 2232 * Allocate necessary DMA resources for the packet's data buffer 2233 * NOTE: 2234 * In case of read/write commands, DMA resource allocation here is 2235 * based on the premise that the transfer length specified in 2236 * the read/write scsi cdb will match exactly DMA resources - 2237 * returning correct packet residue is crucial. 2238 */ 2239 if ((rval = sata_dma_buf_setup(spx, flags, callback, arg, 2240 &cur_dma_attr)) != DDI_SUCCESS) { 2241 /* 2242 * If a DMA allocation request fails with 2243 * DDI_DMA_NOMAPPING, indicate the error by calling 2244 * bioerror(9F) with bp and an error code of EFAULT. 2245 * If a DMA allocation request fails with 2246 * DDI_DMA_TOOBIG, indicate the error by calling 2247 * bioerror(9F) with bp and an error code of EINVAL. 2248 * For DDI_DMA_NORESOURCES, we may have some of them allocated. 2249 * Request may be repeated later - there is no real error. 2250 */ 2251 switch (rval) { 2252 case DDI_DMA_NORESOURCES: 2253 bioerror(bp, 0); 2254 break; 2255 case DDI_DMA_NOMAPPING: 2256 case DDI_DMA_BADATTR: 2257 bioerror(bp, EFAULT); 2258 break; 2259 case DDI_DMA_TOOBIG: 2260 default: 2261 bioerror(bp, EINVAL); 2262 break; 2263 } 2264 if (new_pkt == TRUE) { 2265 /* 2266 * Since this is a new packet, we can clean-up 2267 * everything 2268 */ 2269 sata_scsi_destroy_pkt(ap, pkt); 2270 } else { 2271 /* 2272 * This is a re-used packet. It will be target driver's 2273 * responsibility to eventually destroy it (which 2274 * will free allocated resources). 2275 * Here, we just "complete" the request, leaving 2276 * allocated resources intact, so the request may 2277 * be retried. 2278 */ 2279 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 2280 sata_pkt_free(spx); 2281 } 2282 return (NULL); 2283 } 2284 /* Set number of bytes that are not yet accounted for */ 2285 pkt->pkt_resid = spx->txlt_total_residue; 2286 ASSERT(pkt->pkt_resid >= 0); 2287 2288 return (pkt); 2289 } 2290 2291 /* 2292 * Implementation of scsi tran_start. 2293 * Translate scsi cmd into sata operation and return status. 2294 * ATAPI CDBs are passed to ATAPI devices - the device determines what commands 2295 * are supported. 2296 * For SATA hard disks, supported scsi commands: 2297 * SCMD_INQUIRY 2298 * SCMD_TEST_UNIT_READY 2299 * SCMD_START_STOP 2300 * SCMD_READ_CAPACITY 2301 * SCMD_REQUEST_SENSE 2302 * SCMD_LOG_SENSE_G1 2303 * SCMD_LOG_SELECT_G1 2304 * SCMD_MODE_SENSE (specific pages) 2305 * SCMD_MODE_SENSE_G1 (specific pages) 2306 * SCMD_MODE_SELECT (specific pages) 2307 * SCMD_MODE_SELECT_G1 (specific pages) 2308 * SCMD_SYNCHRONIZE_CACHE 2309 * SCMD_SYNCHRONIZE_CACHE_G1 2310 * SCMD_READ 2311 * SCMD_READ_G1 2312 * SCMD_READ_G4 2313 * SCMD_READ_G5 2314 * SCMD_WRITE 2315 * SCMD_WRITE_BUFFER 2316 * SCMD_WRITE_G1 2317 * SCMD_WRITE_G4 2318 * SCMD_WRITE_G5 2319 * SCMD_SEEK (noop) 2320 * SCMD_SDIAG 2321 * 2322 * All other commands are rejected as unsupported. 2323 * 2324 * Returns: 2325 * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver 2326 * for execution. TRAN_ACCEPT may be returned also if device was removed but 2327 * a callback could be scheduled. 2328 * TRAN_BADPKT if cmd was directed to invalid address. 2329 * TRAN_FATAL_ERROR is command was rejected due to hardware error, including 2330 * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device 2331 * was removed and there was no callback specified in scsi pkt. 2332 * TRAN_BUSY if command could not be executed becasue HBA driver or SATA 2333 * framework was busy performing some other operation(s). 2334 * 2335 */ 2336 static int 2337 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt) 2338 { 2339 sata_hba_inst_t *sata_hba_inst = 2340 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2341 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2342 sata_device_t *sdevice = &spx->txlt_sata_pkt->satapkt_device; 2343 sata_drive_info_t *sdinfo; 2344 struct buf *bp; 2345 uint8_t cport, pmport; 2346 boolean_t dev_gone = B_FALSE; 2347 int rval; 2348 2349 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2350 "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]); 2351 2352 ASSERT(spx != NULL && 2353 spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL); 2354 2355 cport = SCSI_TO_SATA_CPORT(ap->a_target); 2356 pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 2357 2358 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2359 2360 if (sdevice->satadev_addr.qual == SATA_ADDR_DCPORT) { 2361 sdinfo = sata_get_device_info(sata_hba_inst, sdevice); 2362 if (sdinfo == NULL || 2363 SATA_CPORT_INFO(sata_hba_inst, cport)-> 2364 cport_tgtnode_clean == B_FALSE || 2365 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 2366 dev_gone = B_TRUE; 2367 } 2368 } else if (sdevice->satadev_addr.qual == SATA_ADDR_DPMPORT) { 2369 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 2370 SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst, 2371 cport) == NULL) { 2372 dev_gone = B_TRUE; 2373 } else if (SATA_PMPORT_INFO(sata_hba_inst, cport, 2374 pmport) == NULL) { 2375 dev_gone = B_TRUE; 2376 } else { 2377 mutex_enter(&(SATA_PMPORT_MUTEX(sata_hba_inst, 2378 cport, pmport))); 2379 sdinfo = sata_get_device_info(sata_hba_inst, sdevice); 2380 if (sdinfo == NULL || 2381 SATA_PMPORT_INFO(sata_hba_inst, cport, pmport)-> 2382 pmport_tgtnode_clean == B_FALSE || 2383 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 2384 dev_gone = B_TRUE; 2385 } 2386 mutex_exit(&(SATA_PMPORT_MUTEX(sata_hba_inst, 2387 cport, pmport))); 2388 } 2389 } 2390 2391 if (dev_gone == B_TRUE) { 2392 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2393 pkt->pkt_reason = CMD_DEV_GONE; 2394 /* 2395 * The sd target driver is checking CMD_DEV_GONE pkt_reason 2396 * only in callback function (for normal requests) and 2397 * in the dump code path. 2398 * So, if the callback is available, we need to do 2399 * the callback rather than returning TRAN_FATAL_ERROR here. 2400 */ 2401 if (pkt->pkt_comp != NULL) { 2402 /* scsi callback required */ 2403 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 2404 (task_func_t *)pkt->pkt_comp, 2405 (void *)pkt, TQ_SLEEP) == NULL) 2406 /* Scheduling the callback failed */ 2407 return (TRAN_BUSY); 2408 return (TRAN_ACCEPT); 2409 } 2410 /* No callback available */ 2411 return (TRAN_FATAL_ERROR); 2412 } 2413 2414 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 2415 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2416 rval = sata_txlt_atapi(spx); 2417 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2418 "sata_scsi_start atapi: rval %d\n", rval); 2419 return (rval); 2420 } 2421 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2422 2423 /* 2424 * Checking for power state, if it was on 2425 * STOPPED state, then the drive is not capable 2426 * of processing media access command. And 2427 * TEST_UNIT_READY, REQUEST_SENSE has special handling 2428 * in the function for different power state. 2429 */ 2430 if (((sdinfo->satadrv_power_level == SATA_POWER_STANDBY) || 2431 (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)) && 2432 (SATA_IS_MEDIUM_ACCESS_CMD(pkt->pkt_cdbp[0]))) { 2433 return (sata_txlt_check_condition(spx, KEY_NOT_READY, 2434 SD_SCSI_ASC_LU_NOT_READY)); 2435 } 2436 2437 /* ATA Disk commands processing starts here */ 2438 2439 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 2440 2441 switch (pkt->pkt_cdbp[0]) { 2442 2443 case SCMD_INQUIRY: 2444 /* Mapped to identify device */ 2445 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2446 bp_mapin(bp); 2447 rval = sata_txlt_inquiry(spx); 2448 break; 2449 2450 case SCMD_TEST_UNIT_READY: 2451 /* 2452 * SAT "SATA to ATA Translation" doc specifies translation 2453 * to ATA CHECK POWER MODE. 2454 */ 2455 rval = sata_txlt_test_unit_ready(spx); 2456 break; 2457 2458 case SCMD_START_STOP: 2459 /* Mapping depends on the command */ 2460 rval = sata_txlt_start_stop_unit(spx); 2461 break; 2462 2463 case SCMD_READ_CAPACITY: 2464 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2465 bp_mapin(bp); 2466 rval = sata_txlt_read_capacity(spx); 2467 break; 2468 2469 case SCMD_REQUEST_SENSE: 2470 /* 2471 * Always No Sense, since we force ARQ 2472 */ 2473 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2474 bp_mapin(bp); 2475 rval = sata_txlt_request_sense(spx); 2476 break; 2477 2478 case SCMD_LOG_SENSE_G1: 2479 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2480 bp_mapin(bp); 2481 rval = sata_txlt_log_sense(spx); 2482 break; 2483 2484 case SCMD_LOG_SELECT_G1: 2485 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2486 bp_mapin(bp); 2487 rval = sata_txlt_log_select(spx); 2488 break; 2489 2490 case SCMD_MODE_SENSE: 2491 case SCMD_MODE_SENSE_G1: 2492 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2493 bp_mapin(bp); 2494 rval = sata_txlt_mode_sense(spx); 2495 break; 2496 2497 2498 case SCMD_MODE_SELECT: 2499 case SCMD_MODE_SELECT_G1: 2500 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2501 bp_mapin(bp); 2502 rval = sata_txlt_mode_select(spx); 2503 break; 2504 2505 case SCMD_SYNCHRONIZE_CACHE: 2506 case SCMD_SYNCHRONIZE_CACHE_G1: 2507 rval = sata_txlt_synchronize_cache(spx); 2508 break; 2509 2510 case SCMD_READ: 2511 case SCMD_READ_G1: 2512 case SCMD_READ_G4: 2513 case SCMD_READ_G5: 2514 rval = sata_txlt_read(spx); 2515 break; 2516 case SCMD_WRITE_BUFFER: 2517 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2518 bp_mapin(bp); 2519 rval = sata_txlt_write_buffer(spx); 2520 break; 2521 2522 case SCMD_WRITE: 2523 case SCMD_WRITE_G1: 2524 case SCMD_WRITE_G4: 2525 case SCMD_WRITE_G5: 2526 rval = sata_txlt_write(spx); 2527 break; 2528 2529 case SCMD_SEEK: 2530 rval = sata_txlt_nodata_cmd_immediate(spx); 2531 break; 2532 2533 /* Other cases will be filed later */ 2534 /* postponed until phase 2 of the development */ 2535 default: 2536 rval = sata_txlt_invalid_command(spx); 2537 break; 2538 } 2539 2540 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2541 "sata_scsi_start: rval %d\n", rval); 2542 2543 return (rval); 2544 } 2545 2546 /* 2547 * Implementation of scsi tran_abort. 2548 * Abort specific pkt or all packets. 2549 * 2550 * Returns 1 if one or more packets were aborted, returns 0 otherwise 2551 * 2552 * May be called from an interrupt level. 2553 */ 2554 static int 2555 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt) 2556 { 2557 sata_hba_inst_t *sata_hba_inst = 2558 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2559 sata_device_t sata_device; 2560 sata_pkt_t *sata_pkt; 2561 2562 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2563 "sata_scsi_abort: %s at target: 0x%x\n", 2564 scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target); 2565 2566 /* Validate address */ 2567 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) 2568 /* Invalid address */ 2569 return (0); 2570 2571 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2572 sata_device.satadev_addr.cport))); 2573 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) { 2574 /* invalid address */ 2575 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2576 sata_device.satadev_addr.cport))); 2577 return (0); 2578 } 2579 if (scsi_pkt == NULL) { 2580 /* 2581 * Abort all packets. 2582 * Although we do not have specific packet, we still need 2583 * dummy packet structure to pass device address to HBA. 2584 * Allocate one, without sleeping. Fail if pkt cannot be 2585 * allocated. 2586 */ 2587 sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP); 2588 if (sata_pkt == NULL) { 2589 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2590 sata_device.satadev_addr.cport))); 2591 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: " 2592 "could not allocate sata_pkt")); 2593 return (0); 2594 } 2595 sata_pkt->satapkt_rev = SATA_PKT_REV; 2596 sata_pkt->satapkt_device = sata_device; 2597 sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 2598 } else { 2599 if (scsi_pkt->pkt_ha_private == NULL) { 2600 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2601 sata_device.satadev_addr.cport))); 2602 return (0); /* Bad scsi pkt */ 2603 } 2604 /* extract pointer to sata pkt */ 2605 sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)-> 2606 txlt_sata_pkt; 2607 } 2608 2609 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2610 sata_device.satadev_addr.cport))); 2611 /* Send abort request to HBA */ 2612 if ((*SATA_ABORT_FUNC(sata_hba_inst)) 2613 (SATA_DIP(sata_hba_inst), sata_pkt, 2614 scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) == 2615 SATA_SUCCESS) { 2616 if (scsi_pkt == NULL) 2617 kmem_free(sata_pkt, sizeof (sata_pkt_t)); 2618 /* Success */ 2619 return (1); 2620 } 2621 /* Else, something did not go right */ 2622 if (scsi_pkt == NULL) 2623 kmem_free(sata_pkt, sizeof (sata_pkt_t)); 2624 /* Failure */ 2625 return (0); 2626 } 2627 2628 2629 /* 2630 * Implementation of scsi tran_reset. 2631 * RESET_ALL request is translated into port reset. 2632 * RESET_TARGET requests is translated into a device reset, 2633 * RESET_LUN request is accepted only for LUN 0 and translated into 2634 * device reset. 2635 * The target reset should cause all HBA active and queued packets to 2636 * be terminated and returned with pkt reason SATA_PKT_RESET prior to 2637 * the return. HBA should report reset event for the device. 2638 * 2639 * Returns 1 upon success, 0 upon failure. 2640 */ 2641 static int 2642 sata_scsi_reset(struct scsi_address *ap, int level) 2643 { 2644 sata_hba_inst_t *sata_hba_inst = 2645 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2646 sata_device_t sata_device; 2647 int val; 2648 2649 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2650 "sata_scsi_reset: level %d target: 0x%x\n", 2651 level, ap->a_target); 2652 2653 /* Validate address */ 2654 val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device); 2655 if (val == -1) 2656 /* Invalid address */ 2657 return (0); 2658 2659 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2660 sata_device.satadev_addr.cport))); 2661 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) { 2662 /* invalid address */ 2663 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2664 sata_device.satadev_addr.cport))); 2665 return (0); 2666 } 2667 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2668 sata_device.satadev_addr.cport))); 2669 if (level == RESET_ALL) { 2670 /* port reset */ 2671 if (sata_device.satadev_addr.qual == SATA_ADDR_DCPORT) 2672 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 2673 else 2674 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT; 2675 2676 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 2677 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS) 2678 return (1); 2679 else 2680 return (0); 2681 2682 } else if (val == 0 && 2683 (level == RESET_TARGET || level == RESET_LUN)) { 2684 /* reset device (device attached) */ 2685 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 2686 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS) 2687 return (1); 2688 else 2689 return (0); 2690 } 2691 return (0); 2692 } 2693 2694 2695 /* 2696 * Implementation of scsi tran_getcap (get transport/device capabilities). 2697 * Supported capabilities for SATA hard disks: 2698 * auto-rqsense (always supported) 2699 * tagged-qing (supported if HBA supports it) 2700 * untagged-qing (could be supported if disk supports it, but because 2701 * caching behavior allowing untagged queuing actually 2702 * results in reduced performance. sd tries to throttle 2703 * back to only 3 outstanding commands, which may 2704 * work for real SCSI disks, but with read ahead 2705 * caching, having more than 1 outstanding command 2706 * results in cache thrashing.) 2707 * sector_size 2708 * dma_max 2709 * interconnect-type (INTERCONNECT_SATA) 2710 * 2711 * Supported capabilities for ATAPI CD/DVD devices: 2712 * auto-rqsense (always supported) 2713 * sector_size 2714 * dma_max 2715 * max-cdb-length 2716 * interconnect-type (INTERCONNECT_SATA) 2717 * 2718 * Supported capabilities for ATAPI TAPE devices: 2719 * auto-rqsense (always supported) 2720 * dma_max 2721 * max-cdb-length 2722 * 2723 * Supported capabilities for SATA ATAPI hard disks: 2724 * auto-rqsense (always supported) 2725 * interconnect-type (INTERCONNECT_SATA) 2726 * max-cdb-length 2727 * 2728 * Request for other capabilities is rejected as unsupported. 2729 * 2730 * Returns supported capability value, or -1 if capability is unsuppported or 2731 * the address is invalid - no device. 2732 */ 2733 2734 static int 2735 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom) 2736 { 2737 2738 sata_hba_inst_t *sata_hba_inst = 2739 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2740 sata_device_t sata_device; 2741 sata_drive_info_t *sdinfo; 2742 ddi_dma_attr_t adj_dma_attr; 2743 int rval; 2744 2745 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2746 "sata_scsi_getcap: target: 0x%x, cap: %s\n", 2747 ap->a_target, cap); 2748 2749 /* 2750 * We want to process the capabilities on per port granularity. 2751 * So, we are specifically restricting ourselves to whom != 0 2752 * to exclude the controller wide handling. 2753 */ 2754 if (cap == NULL || whom == 0) 2755 return (-1); 2756 2757 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) { 2758 /* Invalid address */ 2759 return (-1); 2760 } 2761 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2762 sata_device.satadev_addr.cport))); 2763 if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) == 2764 NULL) { 2765 /* invalid address */ 2766 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2767 sata_device.satadev_addr.cport))); 2768 return (-1); 2769 } 2770 2771 switch (scsi_hba_lookup_capstr(cap)) { 2772 case SCSI_CAP_ARQ: 2773 rval = 1; /* ARQ supported, turned on */ 2774 break; 2775 2776 case SCSI_CAP_SECTOR_SIZE: 2777 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) 2778 rval = SATA_DISK_SECTOR_SIZE; /* fixed size */ 2779 else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) 2780 rval = SATA_ATAPI_SECTOR_SIZE; 2781 else rval = -1; 2782 break; 2783 2784 /* 2785 * untagged queuing cause a performance inversion because of 2786 * the way sd operates. Because of this reason we do not 2787 * use it when available. 2788 */ 2789 case SCSI_CAP_UNTAGGED_QING: 2790 if (sdinfo->satadrv_features_enabled & 2791 SATA_DEV_F_E_UNTAGGED_QING) 2792 rval = 1; /* Untagged queuing available */ 2793 else 2794 rval = -1; /* Untagged queuing not available */ 2795 break; 2796 2797 case SCSI_CAP_TAGGED_QING: 2798 if ((sdinfo->satadrv_features_enabled & 2799 SATA_DEV_F_E_TAGGED_QING) && 2800 (sdinfo->satadrv_max_queue_depth > 1)) 2801 rval = 1; /* Tagged queuing available */ 2802 else 2803 rval = -1; /* Tagged queuing not available */ 2804 break; 2805 2806 case SCSI_CAP_DMA_MAX: 2807 sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst), 2808 &adj_dma_attr); 2809 rval = (int)adj_dma_attr.dma_attr_maxxfer; 2810 /* We rely on the fact that dma_attr_maxxfer < 0x80000000 */ 2811 break; 2812 2813 case SCSI_CAP_INTERCONNECT_TYPE: 2814 rval = INTERCONNECT_SATA; /* SATA interconnect type */ 2815 break; 2816 2817 case SCSI_CAP_CDB_LEN: 2818 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) 2819 rval = sdinfo->satadrv_atapi_cdb_len; 2820 else 2821 rval = -1; 2822 break; 2823 2824 default: 2825 rval = -1; 2826 break; 2827 } 2828 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2829 sata_device.satadev_addr.cport))); 2830 return (rval); 2831 } 2832 2833 /* 2834 * Implementation of scsi tran_setcap 2835 * 2836 * Only SCSI_CAP_UNTAGGED_QING and SCSI_CAP_TAGGED_QING are changeable. 2837 * 2838 */ 2839 static int 2840 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom) 2841 { 2842 sata_hba_inst_t *sata_hba_inst = 2843 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2844 sata_device_t sata_device; 2845 sata_drive_info_t *sdinfo; 2846 int rval; 2847 2848 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2849 "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap); 2850 2851 /* 2852 * We want to process the capabilities on per port granularity. 2853 * So, we are specifically restricting ourselves to whom != 0 2854 * to exclude the controller wide handling. 2855 */ 2856 if (cap == NULL || whom == 0) { 2857 return (-1); 2858 } 2859 2860 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) { 2861 /* Invalid address */ 2862 return (-1); 2863 } 2864 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2865 sata_device.satadev_addr.cport))); 2866 if ((sdinfo = sata_get_device_info(sata_hba_inst, 2867 &sata_device)) == NULL) { 2868 /* invalid address */ 2869 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2870 sata_device.satadev_addr.cport))); 2871 return (-1); 2872 } 2873 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2874 sata_device.satadev_addr.cport))); 2875 2876 switch (scsi_hba_lookup_capstr(cap)) { 2877 case SCSI_CAP_ARQ: 2878 case SCSI_CAP_SECTOR_SIZE: 2879 case SCSI_CAP_DMA_MAX: 2880 case SCSI_CAP_INTERCONNECT_TYPE: 2881 rval = 0; 2882 break; 2883 case SCSI_CAP_UNTAGGED_QING: 2884 if (SATA_QDEPTH(sata_hba_inst) > 1) { 2885 rval = 1; 2886 if (value == 1) { 2887 sdinfo->satadrv_features_enabled |= 2888 SATA_DEV_F_E_UNTAGGED_QING; 2889 } else if (value == 0) { 2890 sdinfo->satadrv_features_enabled &= 2891 ~SATA_DEV_F_E_UNTAGGED_QING; 2892 } else { 2893 rval = -1; 2894 } 2895 } else { 2896 rval = 0; 2897 } 2898 break; 2899 case SCSI_CAP_TAGGED_QING: 2900 /* This can TCQ or NCQ */ 2901 if (sata_func_enable & SATA_ENABLE_QUEUING && 2902 ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ && 2903 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) || 2904 (sata_func_enable & SATA_ENABLE_NCQ && 2905 sdinfo->satadrv_features_support & SATA_DEV_F_NCQ && 2906 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) && 2907 (sdinfo->satadrv_max_queue_depth > 1)) { 2908 rval = 1; 2909 if (value == 1) { 2910 sdinfo->satadrv_features_enabled |= 2911 SATA_DEV_F_E_TAGGED_QING; 2912 } else if (value == 0) { 2913 sdinfo->satadrv_features_enabled &= 2914 ~SATA_DEV_F_E_TAGGED_QING; 2915 } else { 2916 rval = -1; 2917 } 2918 } else { 2919 rval = 0; 2920 } 2921 break; 2922 default: 2923 rval = -1; 2924 break; 2925 } 2926 return (rval); 2927 } 2928 2929 /* 2930 * Implementations of scsi tran_destroy_pkt. 2931 * Free resources allocated by sata_scsi_init_pkt() 2932 */ 2933 static void 2934 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 2935 { 2936 sata_pkt_txlate_t *spx; 2937 2938 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2939 2940 sata_common_free_dma_rsrcs(spx); 2941 2942 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 2943 sata_pkt_free(spx); 2944 2945 scsi_hba_pkt_free(ap, pkt); 2946 } 2947 2948 /* 2949 * Implementation of scsi tran_dmafree. 2950 * Free DMA resources allocated by sata_scsi_init_pkt() 2951 */ 2952 2953 static void 2954 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt) 2955 { 2956 #ifndef __lock_lint 2957 _NOTE(ARGUNUSED(ap)) 2958 #endif 2959 sata_pkt_txlate_t *spx; 2960 2961 ASSERT(pkt != NULL); 2962 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2963 2964 sata_common_free_dma_rsrcs(spx); 2965 } 2966 2967 /* 2968 * Implementation of scsi tran_sync_pkt. 2969 * 2970 * The assumption below is that pkt is unique - there is no need to check ap 2971 * 2972 * Synchronize DMA buffer and, if the intermediate buffer is used, copy data 2973 * into/from the real buffer. 2974 */ 2975 static void 2976 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 2977 { 2978 #ifndef __lock_lint 2979 _NOTE(ARGUNUSED(ap)) 2980 #endif 2981 int rval; 2982 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2983 struct buf *bp; 2984 int direction; 2985 2986 ASSERT(spx != NULL); 2987 if (spx->txlt_buf_dma_handle != NULL) { 2988 direction = spx->txlt_sata_pkt-> 2989 satapkt_cmd.satacmd_flags.sata_data_direction; 2990 if (spx->txlt_sata_pkt != NULL && 2991 direction != SATA_DIR_NODATA_XFER) { 2992 if (spx->txlt_tmp_buf != NULL) { 2993 /* Intermediate DMA buffer used */ 2994 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 2995 2996 if (direction & SATA_DIR_WRITE) { 2997 bcopy(bp->b_un.b_addr, 2998 spx->txlt_tmp_buf, bp->b_bcount); 2999 } 3000 } 3001 /* Sync the buffer for device or for CPU */ 3002 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 3003 (direction & SATA_DIR_WRITE) ? 3004 DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU); 3005 ASSERT(rval == DDI_SUCCESS); 3006 if (spx->txlt_tmp_buf != NULL && 3007 !(direction & SATA_DIR_WRITE)) { 3008 /* Intermediate DMA buffer used for read */ 3009 bcopy(spx->txlt_tmp_buf, 3010 bp->b_un.b_addr, bp->b_bcount); 3011 } 3012 3013 } 3014 } 3015 } 3016 3017 3018 3019 /* ******************* SATA - SCSI Translation functions **************** */ 3020 /* 3021 * SCSI to SATA pkt and command translation and SATA to SCSI status/error 3022 * translation. 3023 */ 3024 3025 /* 3026 * Checks if a device exists and can be access and translates common 3027 * scsi_pkt data to sata_pkt data. 3028 * 3029 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and 3030 * sata_pkt was set-up. 3031 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not 3032 * exist and pkt_comp callback was scheduled. 3033 * Returns other TRAN_XXXXX values when error occured and command should be 3034 * rejected with the returned TRAN_XXXXX value. 3035 * 3036 * This function should be called with port mutex held. 3037 */ 3038 static int 3039 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason) 3040 { 3041 sata_drive_info_t *sdinfo; 3042 sata_device_t sata_device; 3043 const struct sata_cmd_flags sata_initial_cmd_flags = { 3044 SATA_DIR_NODATA_XFER, 3045 /* all other values to 0/FALSE */ 3046 }; 3047 /* 3048 * Pkt_reason has to be set if the pkt_comp callback is invoked, 3049 * and that implies TRAN_ACCEPT return value. Any other returned value 3050 * indicates that the scsi packet was not accepted (the reason will not 3051 * be checked by the scsi target driver). 3052 * To make debugging easier, we set pkt_reason to know value here. 3053 * It may be changed later when different completion reason is 3054 * determined. 3055 */ 3056 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 3057 *reason = CMD_TRAN_ERR; 3058 3059 /* Validate address */ 3060 switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst, 3061 &spx->txlt_scsi_pkt->pkt_address, &sata_device)) { 3062 3063 case -1: 3064 /* Invalid address or invalid device type */ 3065 return (TRAN_BADPKT); 3066 case 2: 3067 /* 3068 * Valid address but device type is unknown - Chack if it is 3069 * in the reset state and therefore in an indeterminate state. 3070 */ 3071 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3072 &spx->txlt_sata_pkt->satapkt_device); 3073 if (sdinfo != NULL && (sdinfo->satadrv_event_flags & 3074 (SATA_EVNT_DEVICE_RESET | 3075 SATA_EVNT_INPROC_DEVICE_RESET)) != 0) { 3076 if (!ddi_in_panic()) { 3077 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 3078 *reason = CMD_INCOMPLETE; 3079 SATADBG1(SATA_DBG_SCSI_IF, 3080 spx->txlt_sata_hba_inst, 3081 "sata_scsi_start: rejecting command " 3082 "because of device reset state\n", NULL); 3083 return (TRAN_BUSY); 3084 } 3085 } 3086 /* FALLTHROUGH */ 3087 case 1: 3088 /* valid address but no valid device - it has disappeared */ 3089 spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE; 3090 *reason = CMD_DEV_GONE; 3091 /* 3092 * The sd target driver is checking CMD_DEV_GONE pkt_reason 3093 * only in callback function (for normal requests) and 3094 * in the dump code path. 3095 * So, if the callback is available, we need to do 3096 * the callback rather than returning TRAN_FATAL_ERROR here. 3097 */ 3098 if (spx->txlt_scsi_pkt->pkt_comp != NULL) { 3099 /* scsi callback required */ 3100 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3101 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3102 (void *)spx->txlt_scsi_pkt, 3103 TQ_SLEEP) == NULL) 3104 /* Scheduling the callback failed */ 3105 return (TRAN_BUSY); 3106 3107 return (TRAN_ACCEPT); 3108 } 3109 return (TRAN_FATAL_ERROR); 3110 default: 3111 /* all OK; pkt reason will be overwritten later */ 3112 break; 3113 } 3114 /* 3115 * If in an interrupt context, reject packet if it is to be 3116 * executed in polling mode 3117 */ 3118 if (servicing_interrupt() && 3119 (spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) { 3120 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 3121 "sata_scsi_start: rejecting synchronous command because " 3122 "of interrupt context\n", NULL); 3123 return (TRAN_BUSY); 3124 } 3125 3126 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3127 &spx->txlt_sata_pkt->satapkt_device); 3128 3129 /* 3130 * If device is in reset condition, reject the packet with 3131 * TRAN_BUSY, unless: 3132 * 1. system is panicking (dumping) 3133 * In such case only one thread is running and there is no way to 3134 * process reset. 3135 * 2. cfgadm operation is is progress (internal APCTL lock is set) 3136 * Some cfgadm operations involve drive commands, so reset condition 3137 * needs to be ignored for IOCTL operations. 3138 */ 3139 if ((sdinfo->satadrv_event_flags & 3140 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) { 3141 3142 if (!ddi_in_panic() && 3143 ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst, 3144 sata_device.satadev_addr.cport) & 3145 SATA_APCTL_LOCK_PORT_BUSY) == 0)) { 3146 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 3147 *reason = CMD_INCOMPLETE; 3148 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3149 "sata_scsi_start: rejecting command because " 3150 "of device reset state\n", NULL); 3151 return (TRAN_BUSY); 3152 } 3153 } 3154 3155 /* 3156 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by 3157 * sata_scsi_pkt_init() because pkt init had to work also with 3158 * non-existing devices. 3159 * Now we know that the packet was set-up for a real device, so its 3160 * type is known. 3161 */ 3162 spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type; 3163 3164 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags; 3165 if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst, 3166 sata_device.satadev_addr.cport)->cport_event_flags & 3167 SATA_APCTL_LOCK_PORT_BUSY) != 0) { 3168 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 3169 sata_ignore_dev_reset = B_TRUE; 3170 } 3171 /* 3172 * At this point the generic translation routine determined that the 3173 * scsi packet should be accepted. Packet completion reason may be 3174 * changed later when a different completion reason is determined. 3175 */ 3176 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 3177 *reason = CMD_CMPLT; 3178 3179 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) { 3180 /* Synchronous execution */ 3181 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH | 3182 SATA_OPMODE_POLLING; 3183 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 3184 sata_ignore_dev_reset = ddi_in_panic(); 3185 } else { 3186 /* Asynchronous execution */ 3187 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH | 3188 SATA_OPMODE_INTERRUPTS; 3189 } 3190 /* Convert queuing information */ 3191 if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG) 3192 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag = 3193 B_TRUE; 3194 else if (spx->txlt_scsi_pkt->pkt_flags & 3195 (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD)) 3196 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag = 3197 B_TRUE; 3198 3199 /* Always limit pkt time */ 3200 if (spx->txlt_scsi_pkt->pkt_time == 0) 3201 spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time; 3202 else 3203 /* Pass on scsi_pkt time */ 3204 spx->txlt_sata_pkt->satapkt_time = 3205 spx->txlt_scsi_pkt->pkt_time; 3206 3207 return (TRAN_ACCEPT); 3208 } 3209 3210 3211 /* 3212 * Translate ATA Identify Device data to SCSI Inquiry data. 3213 * This function may be called only for ATA devices. 3214 * This function should not be called for ATAPI devices - they 3215 * respond directly to SCSI Inquiry command. 3216 * 3217 * SATA Identify Device data has to be valid in sata_drive_info. 3218 * Buffer has to accomodate the inquiry length (36 bytes). 3219 * 3220 * This function should be called with a port mutex held. 3221 */ 3222 static void 3223 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst, 3224 sata_drive_info_t *sdinfo, uint8_t *buf) 3225 { 3226 3227 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 3228 struct sata_id *sid = &sdinfo->satadrv_id; 3229 3230 /* Start with a nice clean slate */ 3231 bzero((void *)inq, sizeof (struct scsi_inquiry)); 3232 3233 /* 3234 * Rely on the dev_type for setting paripheral qualifier. 3235 * Assume that DTYPE_RODIRECT applies to CD/DVD R/W devices. 3236 * It could be that DTYPE_OPTICAL could also qualify in the future. 3237 * ATAPI Inquiry may provide more data to the target driver. 3238 */ 3239 inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 3240 DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */ 3241 3242 /* CFA type device is not a removable media device */ 3243 inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) && 3244 (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0; 3245 inq->inq_qual = 0; /* Device type qualifier (obsolete in SCSI3? */ 3246 inq->inq_iso = 0; /* ISO version */ 3247 inq->inq_ecma = 0; /* ECMA version */ 3248 inq->inq_ansi = 3; /* ANSI version - SCSI 3 */ 3249 inq->inq_aenc = 0; /* Async event notification cap. */ 3250 inq->inq_trmiop = 0; /* Supports TERMINATE I/O PROC msg - NO */ 3251 inq->inq_normaca = 0; /* setting NACA bit supported - NO */ 3252 inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */ 3253 inq->inq_len = 31; /* Additional length */ 3254 inq->inq_dualp = 0; /* dual port device - NO */ 3255 inq->inq_reladdr = 0; /* Supports relative addressing - NO */ 3256 inq->inq_sync = 0; /* Supports synchronous data xfers - NO */ 3257 inq->inq_linked = 0; /* Supports linked commands - NO */ 3258 /* 3259 * Queuing support - controller has to 3260 * support some sort of command queuing. 3261 */ 3262 if (SATA_QDEPTH(sata_hba_inst) > 1) 3263 inq->inq_cmdque = 1; /* Supports command queueing - YES */ 3264 else 3265 inq->inq_cmdque = 0; /* Supports command queueing - NO */ 3266 inq->inq_sftre = 0; /* Supports Soft Reset option - NO ??? */ 3267 inq->inq_wbus32 = 0; /* Supports 32 bit wide data xfers - NO */ 3268 inq->inq_wbus16 = 0; /* Supports 16 bit wide data xfers - NO */ 3269 3270 #ifdef _LITTLE_ENDIAN 3271 /* Swap text fields to match SCSI format */ 3272 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 3273 swab(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 3274 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 3275 swab(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 3276 else 3277 swab(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 3278 #else /* _LITTLE_ENDIAN */ 3279 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 3280 bcopy(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 3281 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 3282 bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 3283 else 3284 bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 3285 #endif /* _LITTLE_ENDIAN */ 3286 } 3287 3288 3289 /* 3290 * Scsi response set up for invalid command (command not supported) 3291 * 3292 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3293 */ 3294 static int 3295 sata_txlt_invalid_command(sata_pkt_txlate_t *spx) 3296 { 3297 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3298 struct scsi_extended_sense *sense; 3299 3300 scsipkt->pkt_reason = CMD_CMPLT; 3301 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3302 STATE_SENT_CMD | STATE_GOT_STATUS; 3303 3304 *scsipkt->pkt_scbp = STATUS_CHECK; 3305 3306 sense = sata_arq_sense(spx); 3307 sense->es_key = KEY_ILLEGAL_REQUEST; 3308 sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE; 3309 3310 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3311 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3312 3313 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3314 scsipkt->pkt_comp != NULL) 3315 /* scsi callback required */ 3316 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3317 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3318 (void *)spx->txlt_scsi_pkt, 3319 TQ_SLEEP) == NULL) 3320 /* Scheduling the callback failed */ 3321 return (TRAN_BUSY); 3322 return (TRAN_ACCEPT); 3323 } 3324 3325 /* 3326 * Scsi response set up for check condition with special sense key 3327 * and additional sense code. 3328 * 3329 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3330 */ 3331 static int 3332 sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code) 3333 { 3334 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 3335 int cport = SATA_TXLT_CPORT(spx); 3336 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3337 struct scsi_extended_sense *sense; 3338 3339 mutex_enter(&SATA_CPORT_MUTEX(shi, cport)); 3340 scsipkt->pkt_reason = CMD_CMPLT; 3341 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3342 STATE_SENT_CMD | STATE_GOT_STATUS; 3343 3344 *scsipkt->pkt_scbp = STATUS_CHECK; 3345 3346 sense = sata_arq_sense(spx); 3347 sense->es_key = key; 3348 sense->es_add_code = code; 3349 3350 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3351 3352 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3353 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3354 3355 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 3356 /* scsi callback required */ 3357 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3358 (task_func_t *)scsi_hba_pkt_comp, 3359 (void *)spx->txlt_scsi_pkt, 3360 TQ_SLEEP) == NULL) 3361 /* Scheduling the callback failed */ 3362 return (TRAN_BUSY); 3363 return (TRAN_ACCEPT); 3364 } 3365 3366 /* 3367 * Scsi response setup for 3368 * emulated non-data command that requires no action/return data 3369 * 3370 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3371 */ 3372 static int 3373 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx) 3374 { 3375 int rval; 3376 int reason; 3377 3378 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3379 3380 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3381 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3382 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3383 return (rval); 3384 } 3385 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3386 3387 spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3388 STATE_SENT_CMD | STATE_GOT_STATUS; 3389 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 3390 *(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD; 3391 3392 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3393 "Scsi_pkt completion reason %x\n", 3394 spx->txlt_scsi_pkt->pkt_reason); 3395 3396 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 && 3397 spx->txlt_scsi_pkt->pkt_comp != NULL) 3398 /* scsi callback required */ 3399 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3400 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3401 (void *)spx->txlt_scsi_pkt, 3402 TQ_SLEEP) == NULL) 3403 /* Scheduling the callback failed */ 3404 return (TRAN_BUSY); 3405 return (TRAN_ACCEPT); 3406 } 3407 3408 3409 /* 3410 * SATA translate command: Inquiry / Identify Device 3411 * Use cached Identify Device data for now, rather than issuing actual 3412 * Device Identify cmd request. If device is detached and re-attached, 3413 * asynchronous event processing should fetch and refresh Identify Device 3414 * data. 3415 * Two VPD pages are supported now: 3416 * Vital Product Data page 3417 * Unit Serial Number page 3418 * 3419 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3420 */ 3421 3422 #define EVPD 1 /* Extended Vital Product Data flag */ 3423 #define CMDDT 2 /* Command Support Data - Obsolete */ 3424 #define INQUIRY_SUP_VPD_PAGE 0 /* Supported VDP Pages Page COde */ 3425 #define INQUIRY_USN_PAGE 0x80 /* Unit Serial Number Page Code */ 3426 #define INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */ 3427 3428 static int 3429 sata_txlt_inquiry(sata_pkt_txlate_t *spx) 3430 { 3431 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3432 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3433 sata_drive_info_t *sdinfo; 3434 struct scsi_extended_sense *sense; 3435 int count; 3436 uint8_t *p; 3437 int i, j; 3438 uint8_t page_buf[0xff]; /* Max length */ 3439 int rval, reason; 3440 3441 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3442 3443 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3444 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3445 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3446 return (rval); 3447 } 3448 3449 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3450 &spx->txlt_sata_pkt->satapkt_device); 3451 3452 ASSERT(sdinfo != NULL); 3453 3454 scsipkt->pkt_reason = CMD_CMPLT; 3455 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3456 STATE_SENT_CMD | STATE_GOT_STATUS; 3457 3458 /* Reject not supported request */ 3459 if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */ 3460 *scsipkt->pkt_scbp = STATUS_CHECK; 3461 sense = sata_arq_sense(spx); 3462 sense->es_key = KEY_ILLEGAL_REQUEST; 3463 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3464 goto done; 3465 } 3466 3467 /* Valid Inquiry request */ 3468 *scsipkt->pkt_scbp = STATUS_GOOD; 3469 3470 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 3471 3472 /* 3473 * Because it is fully emulated command storing data 3474 * programatically in the specified buffer, release 3475 * preallocated DMA resources before storing data in the buffer, 3476 * so no unwanted DMA sync would take place. 3477 */ 3478 sata_scsi_dmafree(NULL, scsipkt); 3479 3480 if (!(scsipkt->pkt_cdbp[1] & EVPD)) { 3481 /* Standard Inquiry Data request */ 3482 struct scsi_inquiry inq; 3483 unsigned int bufsize; 3484 3485 sata_identdev_to_inquiry(spx->txlt_sata_hba_inst, 3486 sdinfo, (uint8_t *)&inq); 3487 /* Copy no more than requested */ 3488 count = MIN(bp->b_bcount, 3489 sizeof (struct scsi_inquiry)); 3490 bufsize = scsipkt->pkt_cdbp[4]; 3491 bufsize |= scsipkt->pkt_cdbp[3] << 8; 3492 count = MIN(count, bufsize); 3493 bcopy(&inq, bp->b_un.b_addr, count); 3494 3495 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3496 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 3497 bufsize - count : 0; 3498 } else { 3499 /* 3500 * peripheral_qualifier = 0; 3501 * 3502 * We are dealing only with HD and will be 3503 * dealing with CD/DVD devices soon 3504 */ 3505 uint8_t peripheral_device_type = 3506 sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 3507 DTYPE_DIRECT : DTYPE_RODIRECT; 3508 3509 switch ((uint_t)scsipkt->pkt_cdbp[2]) { 3510 case INQUIRY_SUP_VPD_PAGE: 3511 /* 3512 * Request for suported Vital Product Data 3513 * pages - assuming only 2 page codes 3514 * supported. 3515 */ 3516 page_buf[0] = peripheral_device_type; 3517 page_buf[1] = INQUIRY_SUP_VPD_PAGE; 3518 page_buf[2] = 0; 3519 page_buf[3] = 2; /* page length */ 3520 page_buf[4] = INQUIRY_SUP_VPD_PAGE; 3521 page_buf[5] = INQUIRY_USN_PAGE; 3522 /* Copy no more than requested */ 3523 count = MIN(bp->b_bcount, 6); 3524 bcopy(page_buf, bp->b_un.b_addr, count); 3525 break; 3526 3527 case INQUIRY_USN_PAGE: 3528 /* 3529 * Request for Unit Serial Number page. 3530 * Set-up the page. 3531 */ 3532 page_buf[0] = peripheral_device_type; 3533 page_buf[1] = INQUIRY_USN_PAGE; 3534 page_buf[2] = 0; 3535 /* remaining page length */ 3536 page_buf[3] = SATA_ID_SERIAL_LEN; 3537 3538 /* 3539 * Copy serial number from Identify Device data 3540 * words into the inquiry page and swap bytes 3541 * when necessary. 3542 */ 3543 p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser); 3544 #ifdef _LITTLE_ENDIAN 3545 swab(p, &page_buf[4], SATA_ID_SERIAL_LEN); 3546 #else 3547 bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN); 3548 #endif 3549 /* 3550 * Least significant character of the serial 3551 * number shall appear as the last byte, 3552 * according to SBC-3 spec. 3553 * Count trailing spaces to determine the 3554 * necessary shift length. 3555 */ 3556 p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1]; 3557 for (j = 0; j < SATA_ID_SERIAL_LEN; j++) { 3558 if (*(p - j) != '\0' && 3559 *(p - j) != '\040') 3560 break; 3561 } 3562 3563 /* 3564 * Shift SN string right, so that the last 3565 * non-blank character would appear in last 3566 * byte of SN field in the page. 3567 * 'j' is the shift length. 3568 */ 3569 for (i = 0; 3570 i < (SATA_ID_SERIAL_LEN - j) && j != 0; 3571 i++, p--) 3572 *p = *(p - j); 3573 3574 /* 3575 * Add leading spaces - same number as the 3576 * shift size 3577 */ 3578 for (; j > 0; j--) 3579 page_buf[4 + j - 1] = '\040'; 3580 3581 count = MIN(bp->b_bcount, 3582 SATA_ID_SERIAL_LEN + 4); 3583 bcopy(page_buf, bp->b_un.b_addr, count); 3584 break; 3585 3586 case INQUIRY_DEV_IDENTIFICATION_PAGE: 3587 /* 3588 * We may want to implement this page, when 3589 * identifiers are common for SATA devices 3590 * But not now. 3591 */ 3592 /*FALLTHROUGH*/ 3593 3594 default: 3595 /* Request for unsupported VPD page */ 3596 *scsipkt->pkt_scbp = STATUS_CHECK; 3597 sense = sata_arq_sense(spx); 3598 sense->es_key = KEY_ILLEGAL_REQUEST; 3599 sense->es_add_code = 3600 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3601 goto done; 3602 } 3603 } 3604 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3605 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 3606 scsipkt->pkt_cdbp[4] - count : 0; 3607 } 3608 done: 3609 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3610 3611 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3612 "Scsi_pkt completion reason %x\n", 3613 scsipkt->pkt_reason); 3614 3615 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3616 scsipkt->pkt_comp != NULL) { 3617 /* scsi callback required */ 3618 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3619 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3620 TQ_SLEEP) == NULL) 3621 /* Scheduling the callback failed */ 3622 return (TRAN_BUSY); 3623 } 3624 return (TRAN_ACCEPT); 3625 } 3626 3627 /* 3628 * SATA translate command: Request Sense. 3629 * 3630 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3631 * At the moment this is an emulated command (ATA version for SATA hard disks). 3632 * May be translated into Check Power Mode command in the future. 3633 * 3634 * Note: There is a mismatch between already implemented Informational 3635 * Exception Mode Select page 0x1C and this function. 3636 * When MRIE bit is set in page 0x1C, Request Sense is supposed to return 3637 * NO SENSE and set additional sense code to the exception code - this is not 3638 * implemented here. 3639 */ 3640 static int 3641 sata_txlt_request_sense(sata_pkt_txlate_t *spx) 3642 { 3643 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3644 struct scsi_extended_sense sense; 3645 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3646 sata_drive_info_t *sdinfo; 3647 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 3648 int rval, reason, power_state = 0; 3649 3650 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3651 3652 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3653 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3654 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3655 return (rval); 3656 } 3657 3658 scsipkt->pkt_reason = CMD_CMPLT; 3659 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3660 STATE_SENT_CMD | STATE_GOT_STATUS; 3661 *scsipkt->pkt_scbp = STATUS_GOOD; 3662 3663 /* 3664 * when CONTROL field's NACA bit == 1 3665 * return ILLEGAL_REQUEST 3666 */ 3667 if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) { 3668 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3669 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 3670 SD_SCSI_ASC_CMD_SEQUENCE_ERR)); 3671 } 3672 3673 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3674 &spx->txlt_sata_pkt->satapkt_device); 3675 ASSERT(sdinfo != NULL); 3676 3677 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH; 3678 3679 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE); 3680 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE; 3681 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 3682 if (sata_hba_start(spx, &rval) != 0) { 3683 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3684 return (rval); 3685 } else { 3686 if (scmd->satacmd_error_reg != 0) { 3687 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3688 return (sata_txlt_check_condition(spx, KEY_NO_SENSE, 3689 SD_SCSI_ASC_NO_ADD_SENSE)); 3690 } 3691 } 3692 3693 switch (scmd->satacmd_sec_count_lsb) { 3694 case SATA_PWRMODE_STANDBY: /* device in standby mode */ 3695 if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED) 3696 power_state = SATA_POWER_STOPPED; 3697 else { 3698 power_state = SATA_POWER_STANDBY; 3699 sdinfo->satadrv_power_level = SATA_POWER_STANDBY; 3700 } 3701 break; 3702 case SATA_PWRMODE_IDLE: /* device in idle mode */ 3703 power_state = SATA_POWER_IDLE; 3704 sdinfo->satadrv_power_level = SATA_POWER_IDLE; 3705 break; 3706 case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */ 3707 default: /* 0x40, 0x41 active mode */ 3708 if (sdinfo->satadrv_power_level == SATA_POWER_IDLE) 3709 power_state = SATA_POWER_IDLE; 3710 else { 3711 power_state = SATA_POWER_ACTIVE; 3712 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 3713 } 3714 break; 3715 } 3716 3717 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3718 3719 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 3720 /* 3721 * Because it is fully emulated command storing data 3722 * programatically in the specified buffer, release 3723 * preallocated DMA resources before storing data in the buffer, 3724 * so no unwanted DMA sync would take place. 3725 */ 3726 int count = MIN(bp->b_bcount, 3727 sizeof (struct scsi_extended_sense)); 3728 sata_scsi_dmafree(NULL, scsipkt); 3729 bzero(&sense, sizeof (struct scsi_extended_sense)); 3730 sense.es_valid = 0; /* Valid LBA */ 3731 sense.es_class = 7; /* Response code 0x70 - current err */ 3732 sense.es_key = KEY_NO_SENSE; 3733 sense.es_add_len = 6; /* Additional length */ 3734 /* Copy no more than requested */ 3735 bcopy(&sense, bp->b_un.b_addr, count); 3736 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3737 scsipkt->pkt_resid = 0; 3738 switch (power_state) { 3739 case SATA_POWER_IDLE: 3740 case SATA_POWER_STANDBY: 3741 sense.es_add_code = 3742 SD_SCSI_ASC_LOW_POWER_CONDITION_ON; 3743 break; 3744 case SATA_POWER_STOPPED: 3745 sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE; 3746 break; 3747 case SATA_POWER_ACTIVE: 3748 default: 3749 break; 3750 } 3751 } 3752 3753 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3754 "Scsi_pkt completion reason %x\n", 3755 scsipkt->pkt_reason); 3756 3757 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 3758 /* scsi callback required */ 3759 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3760 (task_func_t *)scsi_hba_pkt_comp, (void *) scsipkt, 3761 TQ_SLEEP) == NULL) 3762 /* Scheduling the callback failed */ 3763 return (TRAN_BUSY); 3764 return (TRAN_ACCEPT); 3765 } 3766 3767 /* 3768 * SATA translate command: Test Unit Ready 3769 * (ATA version for SATA hard disks). 3770 * It is translated into the Check Power Mode command. 3771 * 3772 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3773 */ 3774 static int 3775 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx) 3776 { 3777 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3778 struct scsi_extended_sense *sense; 3779 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 3780 sata_drive_info_t *sdinfo; 3781 int power_state; 3782 int rval, reason; 3783 3784 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3785 3786 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3787 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3788 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3789 return (rval); 3790 } 3791 3792 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3793 &spx->txlt_sata_pkt->satapkt_device); 3794 ASSERT(sdinfo != NULL); 3795 3796 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH; 3797 3798 /* send CHECK POWER MODE command */ 3799 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE); 3800 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE; 3801 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 3802 if (sata_hba_start(spx, &rval) != 0) { 3803 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3804 return (rval); 3805 } else { 3806 if (scmd->satacmd_error_reg != 0) { 3807 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3808 return (sata_txlt_check_condition(spx, KEY_NOT_READY, 3809 SD_SCSI_ASC_LU_NOT_RESPONSE)); 3810 } 3811 } 3812 3813 power_state = scmd->satacmd_sec_count_lsb; 3814 3815 /* 3816 * return NOT READY when device in STOPPED mode 3817 */ 3818 if (power_state == SATA_PWRMODE_STANDBY && 3819 sdinfo->satadrv_power_level == SATA_POWER_STOPPED) { 3820 *scsipkt->pkt_scbp = STATUS_CHECK; 3821 sense = sata_arq_sense(spx); 3822 sense->es_key = KEY_NOT_READY; 3823 sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY; 3824 } else { 3825 /* 3826 * For other power mode, return GOOD status 3827 */ 3828 *scsipkt->pkt_scbp = STATUS_GOOD; 3829 } 3830 3831 scsipkt->pkt_reason = CMD_CMPLT; 3832 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3833 STATE_SENT_CMD | STATE_GOT_STATUS; 3834 3835 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3836 3837 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3838 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3839 3840 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 3841 /* scsi callback required */ 3842 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3843 (task_func_t *)scsi_hba_pkt_comp, (void *) scsipkt, 3844 TQ_SLEEP) == NULL) 3845 /* Scheduling the callback failed */ 3846 return (TRAN_BUSY); 3847 3848 return (TRAN_ACCEPT); 3849 } 3850 3851 /* 3852 * SATA translate command: Start Stop Unit 3853 * Translation depends on a command: 3854 * 3855 * Power condition bits will be supported 3856 * and the power level should be maintained by SATL, 3857 * When SATL received a command, it will check the 3858 * power level firstly, and return the status according 3859 * to SAT2 v2.6 and SAT-2 Standby Modifications 3860 * 3861 * SPC-4/SBC-3 SATL ATA power condition SATL SPC/SBC 3862 * ----------------------------------------------------------------------- 3863 * SSU_PC1 Active <==> ATA Active <==> SSU:start_bit =1 3864 * SSU_PC2 Idle <==> ATA Idle <==> N/A 3865 * SSU_PC3 Standby <==> ATA Standby <==> N/A 3866 * SSU_PC4 Stopped <==> ATA Standby <==> SSU:start_bit = 0 3867 * 3868 * Unload Media / NOT SUPPORTED YET 3869 * Load Media / NOT SUPPROTED YET 3870 * Immediate bit / NOT SUPPORTED YET (deferred error) 3871 * 3872 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 3873 * appropriate values in scsi_pkt fields. 3874 */ 3875 static int 3876 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx) 3877 { 3878 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3879 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 3880 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 3881 int cport = SATA_TXLT_CPORT(spx); 3882 int rval, reason; 3883 sata_drive_info_t *sdinfo; 3884 sata_id_t *sata_id; 3885 3886 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3887 "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1); 3888 3889 mutex_enter(&SATA_CPORT_MUTEX(shi, cport)); 3890 3891 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3892 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3893 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3894 return (rval); 3895 } 3896 3897 if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) { 3898 /* IMMED bit - not supported */ 3899 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3900 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 3901 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 3902 } 3903 3904 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH; 3905 spx->txlt_sata_pkt->satapkt_comp = NULL; 3906 3907 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3908 &spx->txlt_sata_pkt->satapkt_device); 3909 ASSERT(sdinfo != NULL); 3910 sata_id = &sdinfo->satadrv_id; 3911 3912 switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) { 3913 case 0: 3914 if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) { 3915 /* Load/Unload Media - invalid request */ 3916 goto err_out; 3917 } 3918 if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) { 3919 /* Start Unit */ 3920 sata_build_read_verify_cmd(scmd, 1, 5); 3921 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 3922 /* Transfer command to HBA */ 3923 if (sata_hba_start(spx, &rval) != 0) { 3924 /* Pkt not accepted for execution */ 3925 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3926 return (rval); 3927 } else { 3928 if (scmd->satacmd_error_reg != 0) { 3929 goto err_out; 3930 } 3931 } 3932 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 3933 } else { 3934 /* Stop Unit */ 3935 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 3936 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 3937 if (sata_hba_start(spx, &rval) != 0) { 3938 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3939 return (rval); 3940 } else { 3941 if (scmd->satacmd_error_reg != 0) { 3942 goto err_out; 3943 } 3944 } 3945 /* ata standby immediate command */ 3946 sata_build_generic_cmd(scmd, SATAC_STANDBY_IM); 3947 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 3948 if (sata_hba_start(spx, &rval) != 0) { 3949 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3950 return (rval); 3951 } else { 3952 if (scmd->satacmd_error_reg != 0) { 3953 goto err_out; 3954 } 3955 } 3956 sdinfo->satadrv_power_level = SATA_POWER_STOPPED; 3957 } 3958 break; 3959 case 0x1: 3960 sata_build_generic_cmd(scmd, SATAC_IDLE); 3961 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 3962 if (sata_hba_start(spx, &rval) != 0) { 3963 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3964 return (rval); 3965 } else { 3966 if (scmd->satacmd_error_reg != 0) { 3967 goto err_out; 3968 } 3969 } 3970 sata_build_read_verify_cmd(scmd, 1, 5); 3971 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 3972 /* Transfer command to HBA */ 3973 if (sata_hba_start(spx, &rval) != 0) { 3974 /* Pkt not accepted for execution */ 3975 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3976 return (rval); 3977 } else { 3978 if (scmd->satacmd_error_reg != 0) { 3979 goto err_out; 3980 } 3981 } 3982 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 3983 break; 3984 case 0x2: 3985 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 3986 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 3987 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) { 3988 if (sata_hba_start(spx, &rval) != 0) { 3989 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3990 return (rval); 3991 } else { 3992 if (scmd->satacmd_error_reg != 0) { 3993 goto err_out; 3994 } 3995 } 3996 } 3997 sata_build_generic_cmd(scmd, SATAC_IDLE); 3998 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 3999 if (sata_hba_start(spx, &rval) != 0) { 4000 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4001 return (rval); 4002 } else { 4003 if (scmd->satacmd_error_reg != 0) { 4004 goto err_out; 4005 } 4006 } 4007 if ((scsipkt->pkt_cdbp[3] & START_STOP_MODIFIER_MASK)) { 4008 /* 4009 * POWER CONDITION MODIFIER bit set 4010 * to 0x1 or larger it will be handled 4011 * on the same way as bit = 0x1 4012 */ 4013 if (!(sata_id->ai_cmdset84 & 4014 SATA_IDLE_UNLOAD_SUPPORTED)) { 4015 sdinfo->satadrv_power_level = SATA_POWER_IDLE; 4016 break; 4017 } 4018 sata_build_generic_cmd(scmd, SATAC_IDLE_IM); 4019 scmd->satacmd_features_reg = 0x44; 4020 scmd->satacmd_lba_low_lsb = 0x4c; 4021 scmd->satacmd_lba_mid_lsb = 0x4e; 4022 scmd->satacmd_lba_high_lsb = 0x55; 4023 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4024 if (sata_hba_start(spx, &rval) != 0) { 4025 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4026 return (rval); 4027 } else { 4028 if (scmd->satacmd_error_reg != 0) { 4029 goto err_out; 4030 } 4031 } 4032 } 4033 sdinfo->satadrv_power_level = SATA_POWER_IDLE; 4034 break; 4035 case 0x3: 4036 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 4037 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4038 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) { 4039 if (sata_hba_start(spx, &rval) != 0) { 4040 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4041 return (rval); 4042 } else { 4043 if (scmd->satacmd_error_reg != 0) { 4044 goto err_out; 4045 } 4046 } 4047 } 4048 sata_build_generic_cmd(scmd, SATAC_STANDBY); 4049 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4050 if (sata_hba_start(spx, &rval) != 0) { 4051 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4052 return (rval); 4053 } else { 4054 if (scmd->satacmd_error_reg != 0) { 4055 goto err_out; 4056 } 4057 } 4058 sdinfo->satadrv_power_level = SATA_POWER_STANDBY; 4059 break; 4060 case 0x7: 4061 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE); 4062 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE; 4063 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4064 if (sata_hba_start(spx, &rval) != 0) { 4065 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4066 return (rval); 4067 } else { 4068 if (scmd->satacmd_error_reg != 0) { 4069 goto err_out; 4070 } 4071 } 4072 switch (scmd->satacmd_sec_count_lsb) { 4073 case SATA_PWRMODE_STANDBY: 4074 sata_build_generic_cmd(scmd, SATAC_STANDBY); 4075 scmd->satacmd_sec_count_msb = sata_get_standby_timer( 4076 sdinfo->satadrv_standby_timer); 4077 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4078 if (sata_hba_start(spx, &rval) != 0) { 4079 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4080 return (rval); 4081 } else { 4082 if (scmd->satacmd_error_reg != 0) { 4083 goto err_out; 4084 } 4085 } 4086 break; 4087 case SATA_PWRMODE_IDLE: 4088 sata_build_generic_cmd(scmd, SATAC_IDLE); 4089 scmd->satacmd_sec_count_msb = sata_get_standby_timer( 4090 sdinfo->satadrv_standby_timer); 4091 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 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 break; 4101 case SATA_PWRMODE_ACTIVE_SPINDOWN: 4102 case SATA_PWRMODE_ACTIVE_SPINUP: 4103 case SATA_PWRMODE_ACTIVE: 4104 sata_build_generic_cmd(scmd, SATAC_IDLE); 4105 scmd->satacmd_sec_count_msb = sata_get_standby_timer( 4106 sdinfo->satadrv_standby_timer); 4107 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4108 if (sata_hba_start(spx, &rval) != 0) { 4109 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4110 return (rval); 4111 } else { 4112 if (scmd->satacmd_error_reg != 0) { 4113 goto err_out; 4114 } 4115 } 4116 sata_build_read_verify_cmd(scmd, 1, 5); 4117 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4118 if (sata_hba_start(spx, &rval) != 0) { 4119 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4120 return (rval); 4121 } else { 4122 if (scmd->satacmd_error_reg != 0) { 4123 goto err_out; 4124 } 4125 } 4126 break; 4127 default: 4128 goto err_out; 4129 } 4130 break; 4131 case 0xb: 4132 if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) == 4133 0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) { 4134 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4135 return (sata_txlt_check_condition(spx, 4136 KEY_ILLEGAL_REQUEST, 4137 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4138 } 4139 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 4140 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4141 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) { 4142 if (sata_hba_start(spx, &rval) != 0) { 4143 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4144 return (rval); 4145 } else { 4146 if (scmd->satacmd_error_reg != 0) { 4147 goto err_out; 4148 } 4149 } 4150 sata_build_generic_cmd(scmd, SATAC_STANDBY_IM); 4151 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4152 if (sata_hba_start(spx, &rval) != 0) { 4153 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4154 return (rval); 4155 } else { 4156 if (scmd->satacmd_error_reg != 0) { 4157 goto err_out; 4158 } 4159 } 4160 } 4161 bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4); 4162 break; 4163 default: 4164 err_out: 4165 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4166 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 4167 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4168 } 4169 4170 /* 4171 * Since it was a synchronous command, 4172 * a callback function will be called directly. 4173 */ 4174 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4175 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4176 "synchronous execution status %x\n", 4177 spx->txlt_sata_pkt->satapkt_reason); 4178 4179 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) { 4180 sata_set_arq_data(spx->txlt_sata_pkt); 4181 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4182 (task_func_t *)scsi_hba_pkt_comp, (void *) scsipkt, 4183 TQ_SLEEP) == 0) { 4184 return (TRAN_BUSY); 4185 } 4186 } 4187 else 4188 4189 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 4190 4191 return (TRAN_ACCEPT); 4192 4193 } 4194 4195 /* 4196 * SATA translate command: Read Capacity. 4197 * Emulated command for SATA disks. 4198 * Capacity is retrieved from cached Idenifty Device data. 4199 * Identify Device data shows effective disk capacity, not the native 4200 * capacity, which may be limitted by Set Max Address command. 4201 * This is ATA version for SATA hard disks. 4202 * 4203 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4204 */ 4205 static int 4206 sata_txlt_read_capacity(sata_pkt_txlate_t *spx) 4207 { 4208 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4209 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4210 sata_drive_info_t *sdinfo; 4211 uint64_t val; 4212 uchar_t *rbuf; 4213 int rval, reason; 4214 4215 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4216 "sata_txlt_read_capacity: ", NULL); 4217 4218 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4219 4220 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 4221 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4222 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4223 return (rval); 4224 } 4225 4226 scsipkt->pkt_reason = CMD_CMPLT; 4227 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4228 STATE_SENT_CMD | STATE_GOT_STATUS; 4229 *scsipkt->pkt_scbp = STATUS_GOOD; 4230 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4231 /* 4232 * Because it is fully emulated command storing data 4233 * programatically in the specified buffer, release 4234 * preallocated DMA resources before storing data in the buffer, 4235 * so no unwanted DMA sync would take place. 4236 */ 4237 sata_scsi_dmafree(NULL, scsipkt); 4238 4239 sdinfo = sata_get_device_info( 4240 spx->txlt_sata_hba_inst, 4241 &spx->txlt_sata_pkt->satapkt_device); 4242 /* Last logical block address */ 4243 val = sdinfo->satadrv_capacity - 1; 4244 rbuf = (uchar_t *)bp->b_un.b_addr; 4245 /* Need to swap endians to match scsi format */ 4246 rbuf[0] = (val >> 24) & 0xff; 4247 rbuf[1] = (val >> 16) & 0xff; 4248 rbuf[2] = (val >> 8) & 0xff; 4249 rbuf[3] = val & 0xff; 4250 /* block size - always 512 bytes, for now */ 4251 rbuf[4] = 0; 4252 rbuf[5] = 0; 4253 rbuf[6] = 0x02; 4254 rbuf[7] = 0; 4255 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4256 scsipkt->pkt_resid = 0; 4257 4258 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n", 4259 sdinfo->satadrv_capacity -1); 4260 } 4261 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4262 /* 4263 * If a callback was requested, do it now. 4264 */ 4265 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4266 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4267 4268 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4269 scsipkt->pkt_comp != NULL) 4270 /* scsi callback required */ 4271 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4272 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4273 TQ_SLEEP) == NULL) 4274 /* Scheduling the callback failed */ 4275 return (TRAN_BUSY); 4276 4277 return (TRAN_ACCEPT); 4278 } 4279 4280 /* 4281 * SATA translate command: Mode Sense. 4282 * Translated into appropriate SATA command or emulated. 4283 * Saved Values Page Control (03) are not supported. 4284 * 4285 * NOTE: only caching mode sense page is currently implemented. 4286 * 4287 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4288 */ 4289 4290 #define LLBAA 0x10 /* Long LBA Accepted */ 4291 4292 static int 4293 sata_txlt_mode_sense(sata_pkt_txlate_t *spx) 4294 { 4295 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4296 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4297 sata_drive_info_t *sdinfo; 4298 sata_id_t *sata_id; 4299 struct scsi_extended_sense *sense; 4300 int len, bdlen, count, alc_len; 4301 int pc; /* Page Control code */ 4302 uint8_t *buf; /* mode sense buffer */ 4303 int rval, reason; 4304 4305 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4306 "sata_txlt_mode_sense, pc %x page code 0x%02x\n", 4307 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 4308 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 4309 4310 buf = kmem_zalloc(1024, KM_SLEEP); 4311 4312 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4313 4314 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 4315 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4316 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4317 kmem_free(buf, 1024); 4318 return (rval); 4319 } 4320 4321 scsipkt->pkt_reason = CMD_CMPLT; 4322 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4323 STATE_SENT_CMD | STATE_GOT_STATUS; 4324 4325 pc = scsipkt->pkt_cdbp[2] >> 6; 4326 4327 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4328 /* 4329 * Because it is fully emulated command storing data 4330 * programatically in the specified buffer, release 4331 * preallocated DMA resources before storing data in the buffer, 4332 * so no unwanted DMA sync would take place. 4333 */ 4334 sata_scsi_dmafree(NULL, scsipkt); 4335 4336 len = 0; 4337 bdlen = 0; 4338 if (!(scsipkt->pkt_cdbp[1] & 8)) { 4339 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 && 4340 (scsipkt->pkt_cdbp[1] & LLBAA)) 4341 bdlen = 16; 4342 else 4343 bdlen = 8; 4344 } 4345 /* Build mode parameter header */ 4346 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 4347 /* 4-byte mode parameter header */ 4348 buf[len++] = 0; /* mode data length */ 4349 buf[len++] = 0; /* medium type */ 4350 buf[len++] = 0; /* dev-specific param */ 4351 buf[len++] = bdlen; /* Block Descriptor length */ 4352 } else { 4353 /* 8-byte mode parameter header */ 4354 buf[len++] = 0; /* mode data length */ 4355 buf[len++] = 0; 4356 buf[len++] = 0; /* medium type */ 4357 buf[len++] = 0; /* dev-specific param */ 4358 if (bdlen == 16) 4359 buf[len++] = 1; /* long lba descriptor */ 4360 else 4361 buf[len++] = 0; 4362 buf[len++] = 0; 4363 buf[len++] = 0; /* Block Descriptor length */ 4364 buf[len++] = bdlen; 4365 } 4366 4367 sdinfo = sata_get_device_info( 4368 spx->txlt_sata_hba_inst, 4369 &spx->txlt_sata_pkt->satapkt_device); 4370 4371 /* Build block descriptor only if not disabled (DBD) */ 4372 if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) { 4373 /* Block descriptor - direct-access device format */ 4374 if (bdlen == 8) { 4375 /* build regular block descriptor */ 4376 buf[len++] = 4377 (sdinfo->satadrv_capacity >> 24) & 0xff; 4378 buf[len++] = 4379 (sdinfo->satadrv_capacity >> 16) & 0xff; 4380 buf[len++] = 4381 (sdinfo->satadrv_capacity >> 8) & 0xff; 4382 buf[len++] = sdinfo->satadrv_capacity & 0xff; 4383 buf[len++] = 0; /* density code */ 4384 buf[len++] = 0; 4385 if (sdinfo->satadrv_type == 4386 SATA_DTYPE_ATADISK) 4387 buf[len++] = 2; 4388 else 4389 /* ATAPI */ 4390 buf[len++] = 8; 4391 buf[len++] = 0; 4392 } else if (bdlen == 16) { 4393 /* Long LBA Accepted */ 4394 /* build long lba block descriptor */ 4395 #ifndef __lock_lint 4396 buf[len++] = 4397 (sdinfo->satadrv_capacity >> 56) & 0xff; 4398 buf[len++] = 4399 (sdinfo->satadrv_capacity >> 48) & 0xff; 4400 buf[len++] = 4401 (sdinfo->satadrv_capacity >> 40) & 0xff; 4402 buf[len++] = 4403 (sdinfo->satadrv_capacity >> 32) & 0xff; 4404 #endif 4405 buf[len++] = 4406 (sdinfo->satadrv_capacity >> 24) & 0xff; 4407 buf[len++] = 4408 (sdinfo->satadrv_capacity >> 16) & 0xff; 4409 buf[len++] = 4410 (sdinfo->satadrv_capacity >> 8) & 0xff; 4411 buf[len++] = sdinfo->satadrv_capacity & 0xff; 4412 buf[len++] = 0; 4413 buf[len++] = 0; /* density code */ 4414 buf[len++] = 0; 4415 buf[len++] = 0; 4416 if (sdinfo->satadrv_type == 4417 SATA_DTYPE_ATADISK) 4418 buf[len++] = 2; 4419 else 4420 /* ATAPI */ 4421 buf[len++] = 8; 4422 buf[len++] = 0; 4423 } 4424 } 4425 4426 sata_id = &sdinfo->satadrv_id; 4427 4428 /* 4429 * Add requested pages. 4430 * Page 3 and 4 are obsolete and we are not supporting them. 4431 * We deal now with: 4432 * caching (read/write cache control). 4433 * We should eventually deal with following mode pages: 4434 * error recovery (0x01), 4435 * power condition (0x1a), 4436 * exception control page (enables SMART) (0x1c), 4437 * enclosure management (ses), 4438 * protocol-specific port mode (port control). 4439 */ 4440 switch (scsipkt->pkt_cdbp[2] & 0x3f) { 4441 case MODEPAGE_RW_ERRRECOV: 4442 /* DAD_MODE_ERR_RECOV */ 4443 /* R/W recovery */ 4444 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 4445 break; 4446 case MODEPAGE_CACHING: 4447 /* DAD_MODE_CACHE */ 4448 /* Reject not supported request for saved parameters */ 4449 if (pc == 3) { 4450 *scsipkt->pkt_scbp = STATUS_CHECK; 4451 sense = sata_arq_sense(spx); 4452 sense->es_key = KEY_ILLEGAL_REQUEST; 4453 sense->es_add_code = 4454 SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED; 4455 goto done; 4456 } 4457 4458 /* caching */ 4459 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 4460 break; 4461 case MODEPAGE_INFO_EXCPT: 4462 /* exception cntrl */ 4463 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 4464 len += sata_build_msense_page_1c(sdinfo, pc, 4465 buf+len); 4466 } 4467 else 4468 goto err; 4469 break; 4470 case MODEPAGE_POWER_COND: 4471 /* DAD_MODE_POWER_COND */ 4472 /* power condition */ 4473 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 4474 break; 4475 4476 case MODEPAGE_ACOUSTIC_MANAG: 4477 /* acoustic management */ 4478 len += sata_build_msense_page_30(sdinfo, pc, buf+len); 4479 break; 4480 case MODEPAGE_ALLPAGES: 4481 /* all pages */ 4482 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 4483 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 4484 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 4485 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 4486 len += sata_build_msense_page_1c(sdinfo, pc, 4487 buf+len); 4488 } 4489 len += sata_build_msense_page_30(sdinfo, pc, buf+len); 4490 break; 4491 default: 4492 err: 4493 /* Invalid request */ 4494 *scsipkt->pkt_scbp = STATUS_CHECK; 4495 sense = sata_arq_sense(spx); 4496 sense->es_key = KEY_ILLEGAL_REQUEST; 4497 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4498 goto done; 4499 } 4500 4501 /* fix total mode data length */ 4502 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 4503 /* 4-byte mode parameter header */ 4504 buf[0] = len - 1; /* mode data length */ 4505 } else { 4506 buf[0] = (len -2) >> 8; 4507 buf[1] = (len -2) & 0xff; 4508 } 4509 4510 4511 /* Check allocation length */ 4512 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 4513 alc_len = scsipkt->pkt_cdbp[4]; 4514 } else { 4515 alc_len = scsipkt->pkt_cdbp[7]; 4516 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 4517 } 4518 /* 4519 * We do not check for possible parameters truncation 4520 * (alc_len < len) assuming that the target driver works 4521 * correctly. Just avoiding overrun. 4522 * Copy no more than requested and possible, buffer-wise. 4523 */ 4524 count = MIN(alc_len, len); 4525 count = MIN(bp->b_bcount, count); 4526 bcopy(buf, bp->b_un.b_addr, count); 4527 4528 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4529 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 4530 } 4531 *scsipkt->pkt_scbp = STATUS_GOOD; 4532 done: 4533 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4534 (void) kmem_free(buf, 1024); 4535 4536 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4537 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4538 4539 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4540 scsipkt->pkt_comp != NULL) 4541 /* scsi callback required */ 4542 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4543 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4544 TQ_SLEEP) == NULL) 4545 /* Scheduling the callback failed */ 4546 return (TRAN_BUSY); 4547 4548 return (TRAN_ACCEPT); 4549 } 4550 4551 4552 /* 4553 * SATA translate command: Mode Select. 4554 * Translated into appropriate SATA command or emulated. 4555 * Saving parameters is not supported. 4556 * Changing device capacity is not supported (although theoretically 4557 * possible by executing SET FEATURES/SET MAX ADDRESS) 4558 * 4559 * Assumption is that the target driver is working correctly. 4560 * 4561 * More than one SATA command may be executed to perform operations specified 4562 * by mode select pages. The first error terminates further execution. 4563 * Operations performed successully are not backed-up in such case. 4564 * 4565 * NOTE: Implemented pages: 4566 * - caching page 4567 * - informational exception page 4568 * - acoustic management page 4569 * - power condition page 4570 * Caching setup is remembered so it could be re-stored in case of 4571 * an unexpected device reset. 4572 * 4573 * Returns TRAN_XXXX. 4574 * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields. 4575 */ 4576 4577 static int 4578 sata_txlt_mode_select(sata_pkt_txlate_t *spx) 4579 { 4580 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4581 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4582 struct scsi_extended_sense *sense; 4583 int len, pagelen, count, pllen; 4584 uint8_t *buf; /* mode select buffer */ 4585 int rval, stat, reason; 4586 uint_t nointr_flag; 4587 int dmod = 0; 4588 4589 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4590 "sata_txlt_mode_select, pc %x page code 0x%02x\n", 4591 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 4592 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 4593 4594 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4595 4596 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 4597 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4598 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4599 return (rval); 4600 } 4601 /* 4602 * If in interrupt context, reject this packet because it may result 4603 * in issuing a synchronous command to HBA. 4604 */ 4605 if (servicing_interrupt()) { 4606 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 4607 "sata_txlt_mode_select: rejecting command because " 4608 "of interrupt context\n", NULL); 4609 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4610 return (TRAN_BUSY); 4611 } 4612 4613 rval = TRAN_ACCEPT; 4614 4615 scsipkt->pkt_reason = CMD_CMPLT; 4616 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4617 STATE_SENT_CMD | STATE_GOT_STATUS; 4618 nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR; 4619 4620 /* Reject not supported request */ 4621 if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */ 4622 *scsipkt->pkt_scbp = STATUS_CHECK; 4623 sense = sata_arq_sense(spx); 4624 sense->es_key = KEY_ILLEGAL_REQUEST; 4625 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4626 goto done; 4627 } 4628 4629 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 4630 pllen = scsipkt->pkt_cdbp[4]; 4631 } else { 4632 pllen = scsipkt->pkt_cdbp[7]; 4633 pllen = (pllen << 8) | scsipkt->pkt_cdbp[7]; 4634 } 4635 4636 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 4637 4638 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) { 4639 buf = (uint8_t *)bp->b_un.b_addr; 4640 count = MIN(bp->b_bcount, pllen); 4641 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4642 scsipkt->pkt_resid = 0; 4643 pllen = count; 4644 4645 /* 4646 * Check the header to skip the block descriptor(s) - we 4647 * do not support setting device capacity. 4648 * Existing macros do not recognize long LBA dscriptor, 4649 * hence manual calculation. 4650 */ 4651 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 4652 /* 6-bytes CMD, 4 bytes header */ 4653 if (count <= 4) 4654 goto done; /* header only */ 4655 len = buf[3] + 4; 4656 } else { 4657 /* 10-bytes CMD, 8 bytes header */ 4658 if (count <= 8) 4659 goto done; /* header only */ 4660 len = buf[6]; 4661 len = (len << 8) + buf[7] + 8; 4662 } 4663 if (len >= count) 4664 goto done; /* header + descriptor(s) only */ 4665 4666 pllen -= len; /* remaining data length */ 4667 4668 /* 4669 * We may be executing SATA command and want to execute it 4670 * in SYNCH mode, regardless of scsi_pkt setting. 4671 * Save scsi_pkt setting and indicate SYNCH mode 4672 */ 4673 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4674 scsipkt->pkt_comp != NULL) { 4675 scsipkt->pkt_flags |= FLAG_NOINTR; 4676 } 4677 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH; 4678 4679 /* 4680 * len is now the offset to a first mode select page 4681 * Process all pages 4682 */ 4683 while (pllen > 0) { 4684 switch ((int)buf[len]) { 4685 case MODEPAGE_CACHING: 4686 /* No support for SP (saving) */ 4687 if (scsipkt->pkt_cdbp[1] & 0x01) { 4688 *scsipkt->pkt_scbp = STATUS_CHECK; 4689 sense = sata_arq_sense(spx); 4690 sense->es_key = KEY_ILLEGAL_REQUEST; 4691 sense->es_add_code = 4692 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4693 goto done; 4694 } 4695 stat = sata_mode_select_page_8(spx, 4696 (struct mode_cache_scsi3 *)&buf[len], 4697 pllen, &pagelen, &rval, &dmod); 4698 /* 4699 * The pagelen value indicates the number of 4700 * parameter bytes already processed. 4701 * The rval is the return value from 4702 * sata_tran_start(). 4703 * The stat indicates the overall status of 4704 * the operation(s). 4705 */ 4706 if (stat != SATA_SUCCESS) 4707 /* 4708 * Page processing did not succeed - 4709 * all error info is already set-up, 4710 * just return 4711 */ 4712 pllen = 0; /* this breaks the loop */ 4713 else { 4714 len += pagelen; 4715 pllen -= pagelen; 4716 } 4717 break; 4718 4719 case MODEPAGE_INFO_EXCPT: 4720 stat = sata_mode_select_page_1c(spx, 4721 (struct mode_info_excpt_page *)&buf[len], 4722 pllen, &pagelen, &rval, &dmod); 4723 /* 4724 * The pagelen value indicates the number of 4725 * parameter bytes already processed. 4726 * The rval is the return value from 4727 * sata_tran_start(). 4728 * The stat indicates the overall status of 4729 * the operation(s). 4730 */ 4731 if (stat != SATA_SUCCESS) 4732 /* 4733 * Page processing did not succeed - 4734 * all error info is already set-up, 4735 * just return 4736 */ 4737 pllen = 0; /* this breaks the loop */ 4738 else { 4739 len += pagelen; 4740 pllen -= pagelen; 4741 } 4742 break; 4743 4744 case MODEPAGE_ACOUSTIC_MANAG: 4745 stat = sata_mode_select_page_30(spx, 4746 (struct mode_acoustic_management *) 4747 &buf[len], pllen, &pagelen, &rval, &dmod); 4748 /* 4749 * The pagelen value indicates the number of 4750 * parameter bytes already processed. 4751 * The rval is the return value from 4752 * sata_tran_start(). 4753 * The stat indicates the overall status of 4754 * the operation(s). 4755 */ 4756 if (stat != SATA_SUCCESS) 4757 /* 4758 * Page processing did not succeed - 4759 * all error info is already set-up, 4760 * just return 4761 */ 4762 pllen = 0; /* this breaks the loop */ 4763 else { 4764 len += pagelen; 4765 pllen -= pagelen; 4766 } 4767 4768 break; 4769 case MODEPAGE_POWER_COND: 4770 stat = sata_mode_select_page_1a(spx, 4771 (struct mode_info_power_cond *)&buf[len], 4772 pllen, &pagelen, &rval, &dmod); 4773 /* 4774 * The pagelen value indicates the number of 4775 * parameter bytes already processed. 4776 * The rval is the return value from 4777 * sata_tran_start(). 4778 * The stat indicates the overall status of 4779 * the operation(s). 4780 */ 4781 if (stat != SATA_SUCCESS) 4782 /* 4783 * Page processing did not succeed - 4784 * all error info is already set-up, 4785 * just return 4786 */ 4787 pllen = 0; /* this breaks the loop */ 4788 else { 4789 len += pagelen; 4790 pllen -= pagelen; 4791 } 4792 break; 4793 default: 4794 *scsipkt->pkt_scbp = STATUS_CHECK; 4795 sense = sata_arq_sense(spx); 4796 sense->es_key = KEY_ILLEGAL_REQUEST; 4797 sense->es_add_code = 4798 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 4799 goto done; 4800 } 4801 } 4802 } 4803 done: 4804 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4805 /* 4806 * If device parameters were modified, fetch and store the new 4807 * Identify Device data. Since port mutex could have been released 4808 * for accessing HBA driver, we need to re-check device existence. 4809 */ 4810 if (dmod != 0) { 4811 sata_drive_info_t new_sdinfo, *sdinfo; 4812 int rv = 0; 4813 4814 /* 4815 * Following statement has to be changed if this function is 4816 * used for devices other than SATA hard disks. 4817 */ 4818 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK; 4819 4820 new_sdinfo.satadrv_addr = 4821 spx->txlt_sata_pkt->satapkt_device.satadev_addr; 4822 rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst, 4823 &new_sdinfo); 4824 4825 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4826 /* 4827 * Since port mutex could have been released when 4828 * accessing HBA driver, we need to re-check that the 4829 * framework still holds the device info structure. 4830 */ 4831 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4832 &spx->txlt_sata_pkt->satapkt_device); 4833 if (sdinfo != NULL) { 4834 /* 4835 * Device still has info structure in the 4836 * sata framework. Copy newly fetched info 4837 */ 4838 if (rv == 0) { 4839 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 4840 sata_save_drive_settings(sdinfo); 4841 } else { 4842 /* 4843 * Could not fetch new data - invalidate 4844 * sata_drive_info. That makes device 4845 * unusable. 4846 */ 4847 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 4848 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 4849 } 4850 } 4851 if (rv != 0 || sdinfo == NULL) { 4852 /* 4853 * This changes the overall mode select completion 4854 * reason to a failed one !!!!! 4855 */ 4856 *scsipkt->pkt_scbp = STATUS_CHECK; 4857 sense = sata_arq_sense(spx); 4858 scsipkt->pkt_reason = CMD_INCOMPLETE; 4859 rval = TRAN_ACCEPT; 4860 } 4861 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4862 } 4863 /* Restore the scsi pkt flags */ 4864 scsipkt->pkt_flags &= ~FLAG_NOINTR; 4865 scsipkt->pkt_flags |= nointr_flag; 4866 4867 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4868 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4869 4870 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4871 scsipkt->pkt_comp != NULL) 4872 /* scsi callback required */ 4873 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4874 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4875 TQ_SLEEP) == NULL) 4876 /* Scheduling the callback failed */ 4877 return (TRAN_BUSY); 4878 4879 return (rval); 4880 } 4881 4882 4883 4884 /* 4885 * Translate command: Log Sense 4886 */ 4887 static int 4888 sata_txlt_log_sense(sata_pkt_txlate_t *spx) 4889 { 4890 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4891 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4892 sata_drive_info_t *sdinfo; 4893 struct scsi_extended_sense *sense; 4894 int len, count, alc_len; 4895 int pc; /* Page Control code */ 4896 int page_code; /* Page code */ 4897 uint8_t *buf; /* log sense buffer */ 4898 int rval, reason; 4899 #define MAX_LOG_SENSE_PAGE_SIZE 512 4900 4901 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4902 "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n", 4903 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 4904 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 4905 4906 buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP); 4907 4908 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4909 4910 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 4911 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4912 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4913 kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 4914 return (rval); 4915 } 4916 /* 4917 * If in interrupt context, reject this packet because it may result 4918 * in issuing a synchronous command to HBA. 4919 */ 4920 if (servicing_interrupt()) { 4921 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 4922 "sata_log_sense: rejecting command because " 4923 "of interrupt context\n", NULL); 4924 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4925 kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 4926 return (TRAN_BUSY); 4927 } 4928 4929 scsipkt->pkt_reason = CMD_CMPLT; 4930 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4931 STATE_SENT_CMD | STATE_GOT_STATUS; 4932 4933 pc = scsipkt->pkt_cdbp[2] >> 6; 4934 page_code = scsipkt->pkt_cdbp[2] & 0x3f; 4935 4936 /* Reject not supported request for all but cumulative values */ 4937 switch (pc) { 4938 case PC_CUMULATIVE_VALUES: 4939 break; 4940 default: 4941 *scsipkt->pkt_scbp = STATUS_CHECK; 4942 sense = sata_arq_sense(spx); 4943 sense->es_key = KEY_ILLEGAL_REQUEST; 4944 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4945 goto done; 4946 } 4947 4948 switch (page_code) { 4949 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 4950 case PAGE_CODE_SELF_TEST_RESULTS: 4951 case PAGE_CODE_INFORMATION_EXCEPTIONS: 4952 case PAGE_CODE_SMART_READ_DATA: 4953 case PAGE_CODE_START_STOP_CYCLE_COUNTER: 4954 break; 4955 default: 4956 *scsipkt->pkt_scbp = STATUS_CHECK; 4957 sense = sata_arq_sense(spx); 4958 sense->es_key = KEY_ILLEGAL_REQUEST; 4959 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4960 goto done; 4961 } 4962 4963 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4964 /* 4965 * Because log sense uses local buffers for data retrieval from 4966 * the devices and sets the data programatically in the 4967 * original specified buffer, release preallocated DMA 4968 * resources before storing data in the original buffer, 4969 * so no unwanted DMA sync would take place. 4970 */ 4971 sata_id_t *sata_id; 4972 4973 sata_scsi_dmafree(NULL, scsipkt); 4974 4975 len = 0; 4976 4977 /* Build log parameter header */ 4978 buf[len++] = page_code; /* page code as in the CDB */ 4979 buf[len++] = 0; /* reserved */ 4980 buf[len++] = 0; /* Zero out page length for now (MSB) */ 4981 buf[len++] = 0; /* (LSB) */ 4982 4983 sdinfo = sata_get_device_info( 4984 spx->txlt_sata_hba_inst, 4985 &spx->txlt_sata_pkt->satapkt_device); 4986 4987 /* 4988 * Add requested pages. 4989 */ 4990 switch (page_code) { 4991 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 4992 len = sata_build_lsense_page_0(sdinfo, buf + len); 4993 break; 4994 case PAGE_CODE_SELF_TEST_RESULTS: 4995 sata_id = &sdinfo->satadrv_id; 4996 if ((! (sata_id->ai_cmdset84 & 4997 SATA_SMART_SELF_TEST_SUPPORTED)) || 4998 (! (sata_id->ai_features87 & 4999 SATA_SMART_SELF_TEST_SUPPORTED))) { 5000 *scsipkt->pkt_scbp = STATUS_CHECK; 5001 sense = sata_arq_sense(spx); 5002 sense->es_key = KEY_ILLEGAL_REQUEST; 5003 sense->es_add_code = 5004 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5005 5006 goto done; 5007 } 5008 len = sata_build_lsense_page_10(sdinfo, buf + len, 5009 spx->txlt_sata_hba_inst); 5010 break; 5011 case PAGE_CODE_INFORMATION_EXCEPTIONS: 5012 sata_id = &sdinfo->satadrv_id; 5013 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 5014 *scsipkt->pkt_scbp = STATUS_CHECK; 5015 sense = sata_arq_sense(spx); 5016 sense->es_key = KEY_ILLEGAL_REQUEST; 5017 sense->es_add_code = 5018 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5019 5020 goto done; 5021 } 5022 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 5023 *scsipkt->pkt_scbp = STATUS_CHECK; 5024 sense = sata_arq_sense(spx); 5025 sense->es_key = KEY_ABORTED_COMMAND; 5026 sense->es_add_code = 5027 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 5028 sense->es_qual_code = 5029 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 5030 5031 goto done; 5032 } 5033 5034 len = sata_build_lsense_page_2f(sdinfo, buf + len, 5035 spx->txlt_sata_hba_inst); 5036 break; 5037 case PAGE_CODE_SMART_READ_DATA: 5038 sata_id = &sdinfo->satadrv_id; 5039 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 5040 *scsipkt->pkt_scbp = STATUS_CHECK; 5041 sense = sata_arq_sense(spx); 5042 sense->es_key = KEY_ILLEGAL_REQUEST; 5043 sense->es_add_code = 5044 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5045 5046 goto done; 5047 } 5048 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 5049 *scsipkt->pkt_scbp = STATUS_CHECK; 5050 sense = sata_arq_sense(spx); 5051 sense->es_key = KEY_ABORTED_COMMAND; 5052 sense->es_add_code = 5053 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 5054 sense->es_qual_code = 5055 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 5056 5057 goto done; 5058 } 5059 5060 /* This page doesn't include a page header */ 5061 len = sata_build_lsense_page_30(sdinfo, buf, 5062 spx->txlt_sata_hba_inst); 5063 goto no_header; 5064 case PAGE_CODE_START_STOP_CYCLE_COUNTER: 5065 sata_id = &sdinfo->satadrv_id; 5066 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 5067 *scsipkt->pkt_scbp = STATUS_CHECK; 5068 sense = sata_arq_sense(spx); 5069 sense->es_key = KEY_ILLEGAL_REQUEST; 5070 sense->es_add_code = 5071 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5072 5073 goto done; 5074 } 5075 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 5076 *scsipkt->pkt_scbp = STATUS_CHECK; 5077 sense = sata_arq_sense(spx); 5078 sense->es_key = KEY_ABORTED_COMMAND; 5079 sense->es_add_code = 5080 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 5081 sense->es_qual_code = 5082 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 5083 5084 goto done; 5085 } 5086 len = sata_build_lsense_page_0e(sdinfo, buf, spx); 5087 goto no_header; 5088 default: 5089 /* Invalid request */ 5090 *scsipkt->pkt_scbp = STATUS_CHECK; 5091 sense = sata_arq_sense(spx); 5092 sense->es_key = KEY_ILLEGAL_REQUEST; 5093 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5094 goto done; 5095 } 5096 5097 /* set parameter log sense data length */ 5098 buf[2] = len >> 8; /* log sense length (MSB) */ 5099 buf[3] = len & 0xff; /* log sense length (LSB) */ 5100 5101 len += SCSI_LOG_PAGE_HDR_LEN; 5102 ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE); 5103 5104 no_header: 5105 /* Check allocation length */ 5106 alc_len = scsipkt->pkt_cdbp[7]; 5107 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 5108 5109 /* 5110 * We do not check for possible parameters truncation 5111 * (alc_len < len) assuming that the target driver works 5112 * correctly. Just avoiding overrun. 5113 * Copy no more than requested and possible, buffer-wise. 5114 */ 5115 count = MIN(alc_len, len); 5116 count = MIN(bp->b_bcount, count); 5117 bcopy(buf, bp->b_un.b_addr, count); 5118 5119 scsipkt->pkt_state |= STATE_XFERRED_DATA; 5120 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 5121 } 5122 *scsipkt->pkt_scbp = STATUS_GOOD; 5123 done: 5124 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5125 (void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 5126 5127 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5128 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5129 5130 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5131 scsipkt->pkt_comp != NULL) 5132 /* scsi callback required */ 5133 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5134 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 5135 TQ_SLEEP) == NULL) 5136 /* Scheduling the callback failed */ 5137 return (TRAN_BUSY); 5138 5139 return (TRAN_ACCEPT); 5140 } 5141 5142 /* 5143 * Translate command: Log Select 5144 * Not implemented at this time - returns invalid command response. 5145 */ 5146 static int 5147 sata_txlt_log_select(sata_pkt_txlate_t *spx) 5148 { 5149 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5150 "sata_txlt_log_select\n", NULL); 5151 5152 return (sata_txlt_invalid_command(spx)); 5153 } 5154 5155 5156 /* 5157 * Translate command: Read (various types). 5158 * Translated into appropriate type of ATA READ command 5159 * for SATA hard disks. 5160 * Both the device capabilities and requested operation mode are 5161 * considered. 5162 * 5163 * Following scsi cdb fields are ignored: 5164 * rdprotect, dpo, fua, fua_nv, group_number. 5165 * 5166 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 5167 * enable variable sata_func_enable), the capability of the controller and 5168 * capability of a device are checked and if both support queueing, read 5169 * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT 5170 * command rather than plain READ_XXX command. 5171 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 5172 * both the controller and device suport such functionality, the read 5173 * request will be translated to READ_FPDMA_QUEUED command. 5174 * In both cases the maximum queue depth is derived as minimum of: 5175 * HBA capability,device capability and sata_max_queue_depth variable setting. 5176 * The value passed to HBA driver is decremented by 1, because only 5 bits are 5177 * used to pass max queue depth value, and the maximum possible queue depth 5178 * is 32. 5179 * 5180 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 5181 * appropriate values in scsi_pkt fields. 5182 */ 5183 static int 5184 sata_txlt_read(sata_pkt_txlate_t *spx) 5185 { 5186 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5187 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5188 sata_drive_info_t *sdinfo; 5189 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 5190 int cport = SATA_TXLT_CPORT(spx); 5191 uint16_t sec_count; 5192 uint64_t lba; 5193 int rval, reason; 5194 int synch; 5195 5196 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5197 5198 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 5199 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5200 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5201 return (rval); 5202 } 5203 5204 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 5205 &spx->txlt_sata_pkt->satapkt_device); 5206 5207 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 5208 /* 5209 * Extract LBA and sector count from scsi CDB. 5210 */ 5211 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 5212 case SCMD_READ: 5213 /* 6-byte scsi read cmd : 0x08 */ 5214 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 5215 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 5216 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5217 sec_count = scsipkt->pkt_cdbp[4]; 5218 /* sec_count 0 will be interpreted as 256 by a device */ 5219 break; 5220 case SCMD_READ_G1: 5221 /* 10-bytes scsi read command : 0x28 */ 5222 lba = scsipkt->pkt_cdbp[2]; 5223 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5224 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5225 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5226 sec_count = scsipkt->pkt_cdbp[7]; 5227 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 5228 break; 5229 case SCMD_READ_G5: 5230 /* 12-bytes scsi read command : 0xA8 */ 5231 lba = scsipkt->pkt_cdbp[2]; 5232 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5233 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5234 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5235 sec_count = scsipkt->pkt_cdbp[6]; 5236 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 5237 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 5238 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 5239 break; 5240 case SCMD_READ_G4: 5241 /* 16-bytes scsi read command : 0x88 */ 5242 lba = scsipkt->pkt_cdbp[2]; 5243 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5244 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5245 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5246 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 5247 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 5248 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 5249 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 5250 sec_count = scsipkt->pkt_cdbp[10]; 5251 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 5252 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 5253 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 5254 break; 5255 default: 5256 /* Unsupported command */ 5257 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5258 return (sata_txlt_invalid_command(spx)); 5259 } 5260 5261 /* 5262 * Check if specified address exceeds device capacity 5263 */ 5264 if ((lba >= sdinfo->satadrv_capacity) || 5265 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 5266 /* LBA out of range */ 5267 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5268 return (sata_txlt_lba_out_of_range(spx)); 5269 } 5270 5271 /* 5272 * For zero-length transfer, emulate good completion of the command 5273 * (reasons for rejecting the command were already checked). 5274 * No DMA resources were allocated. 5275 */ 5276 if (spx->txlt_dma_cookie_list == NULL) { 5277 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5278 return (sata_emul_rw_completion(spx)); 5279 } 5280 5281 /* 5282 * Build cmd block depending on the device capability and 5283 * requested operation mode. 5284 * Do not bother with non-dma mode - we are working only with 5285 * devices supporting DMA. 5286 */ 5287 scmd->satacmd_addr_type = ATA_ADDR_LBA; 5288 scmd->satacmd_device_reg = SATA_ADH_LBA; 5289 scmd->satacmd_cmd_reg = SATAC_READ_DMA; 5290 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 5291 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 5292 scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT; 5293 scmd->satacmd_sec_count_msb = sec_count >> 8; 5294 #ifndef __lock_lint 5295 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 5296 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 5297 scmd->satacmd_lba_high_msb = lba >> 40; 5298 #endif 5299 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 5300 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 5301 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 5302 } 5303 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 5304 scmd->satacmd_lba_low_lsb = lba & 0xff; 5305 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 5306 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 5307 scmd->satacmd_features_reg = 0; 5308 scmd->satacmd_status_reg = 0; 5309 scmd->satacmd_error_reg = 0; 5310 5311 /* 5312 * Check if queueing commands should be used and switch 5313 * to appropriate command if possible 5314 */ 5315 if (sata_func_enable & SATA_ENABLE_QUEUING) { 5316 boolean_t using_queuing; 5317 5318 /* Queuing supported by controller and device? */ 5319 if ((sata_func_enable & SATA_ENABLE_NCQ) && 5320 (sdinfo->satadrv_features_support & 5321 SATA_DEV_F_NCQ) && 5322 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 5323 SATA_CTLF_NCQ)) { 5324 using_queuing = B_TRUE; 5325 5326 /* NCQ supported - use FPDMA READ */ 5327 scmd->satacmd_cmd_reg = 5328 SATAC_READ_FPDMA_QUEUED; 5329 scmd->satacmd_features_reg_ext = 5330 scmd->satacmd_sec_count_msb; 5331 scmd->satacmd_sec_count_msb = 0; 5332 } else if ((sdinfo->satadrv_features_support & 5333 SATA_DEV_F_TCQ) && 5334 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 5335 SATA_CTLF_QCMD)) { 5336 using_queuing = B_TRUE; 5337 5338 /* Legacy queueing */ 5339 if (sdinfo->satadrv_features_support & 5340 SATA_DEV_F_LBA48) { 5341 scmd->satacmd_cmd_reg = 5342 SATAC_READ_DMA_QUEUED_EXT; 5343 scmd->satacmd_features_reg_ext = 5344 scmd->satacmd_sec_count_msb; 5345 scmd->satacmd_sec_count_msb = 0; 5346 } else { 5347 scmd->satacmd_cmd_reg = 5348 SATAC_READ_DMA_QUEUED; 5349 } 5350 } else /* NCQ nor legacy queuing not supported */ 5351 using_queuing = B_FALSE; 5352 5353 /* 5354 * If queuing, the sector count goes in the features register 5355 * and the secount count will contain the tag. 5356 */ 5357 if (using_queuing) { 5358 scmd->satacmd_features_reg = 5359 scmd->satacmd_sec_count_lsb; 5360 scmd->satacmd_sec_count_lsb = 0; 5361 scmd->satacmd_flags.sata_queued = B_TRUE; 5362 5363 /* Set-up maximum queue depth */ 5364 scmd->satacmd_flags.sata_max_queue_depth = 5365 sdinfo->satadrv_max_queue_depth - 1; 5366 } else if (sdinfo->satadrv_features_enabled & 5367 SATA_DEV_F_E_UNTAGGED_QING) { 5368 /* 5369 * Although NCQ/TCQ is not enabled, untagged queuing 5370 * may be still used. 5371 * Set-up the maximum untagged queue depth. 5372 * Use controller's queue depth from sata_hba_tran. 5373 * SATA HBA drivers may ignore this value and rely on 5374 * the internal limits.For drivers that do not 5375 * ignore untaged queue depth, limit the value to 5376 * SATA_MAX_QUEUE_DEPTH (32), as this is the 5377 * largest value that can be passed via 5378 * satacmd_flags.sata_max_queue_depth. 5379 */ 5380 scmd->satacmd_flags.sata_max_queue_depth = 5381 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ? 5382 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1; 5383 5384 } else { 5385 scmd->satacmd_flags.sata_max_queue_depth = 0; 5386 } 5387 } else 5388 scmd->satacmd_flags.sata_max_queue_depth = 0; 5389 5390 SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst, 5391 "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n", 5392 scmd->satacmd_cmd_reg, lba, sec_count); 5393 5394 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5395 /* Need callback function */ 5396 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 5397 synch = FALSE; 5398 } else 5399 synch = TRUE; 5400 5401 /* Transfer command to HBA */ 5402 if (sata_hba_start(spx, &rval) != 0) { 5403 /* Pkt not accepted for execution */ 5404 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5405 return (rval); 5406 } 5407 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5408 /* 5409 * If execution is non-synchronous, 5410 * a callback function will handle potential errors, translate 5411 * the response and will do a callback to a target driver. 5412 * If it was synchronous, check execution status using the same 5413 * framework callback. 5414 */ 5415 if (synch) { 5416 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5417 "synchronous execution status %x\n", 5418 spx->txlt_sata_pkt->satapkt_reason); 5419 sata_txlt_rw_completion(spx->txlt_sata_pkt); 5420 } 5421 return (TRAN_ACCEPT); 5422 } 5423 5424 5425 /* 5426 * SATA translate command: Write (various types) 5427 * Translated into appropriate type of ATA WRITE command 5428 * for SATA hard disks. 5429 * Both the device capabilities and requested operation mode are 5430 * considered. 5431 * 5432 * Following scsi cdb fields are ignored: 5433 * rwprotect, dpo, fua, fua_nv, group_number. 5434 * 5435 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 5436 * enable variable sata_func_enable), the capability of the controller and 5437 * capability of a device are checked and if both support queueing, write 5438 * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT 5439 * command rather than plain WRITE_XXX command. 5440 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 5441 * both the controller and device suport such functionality, the write 5442 * request will be translated to WRITE_FPDMA_QUEUED command. 5443 * In both cases the maximum queue depth is derived as minimum of: 5444 * HBA capability,device capability and sata_max_queue_depth variable setting. 5445 * The value passed to HBA driver is decremented by 1, because only 5 bits are 5446 * used to pass max queue depth value, and the maximum possible queue depth 5447 * is 32. 5448 * 5449 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 5450 * appropriate values in scsi_pkt fields. 5451 */ 5452 static int 5453 sata_txlt_write(sata_pkt_txlate_t *spx) 5454 { 5455 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5456 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5457 sata_drive_info_t *sdinfo; 5458 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 5459 int cport = SATA_TXLT_CPORT(spx); 5460 uint16_t sec_count; 5461 uint64_t lba; 5462 int rval, reason; 5463 int synch; 5464 5465 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5466 5467 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 5468 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5469 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5470 return (rval); 5471 } 5472 5473 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 5474 &spx->txlt_sata_pkt->satapkt_device); 5475 5476 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 5477 /* 5478 * Extract LBA and sector count from scsi CDB 5479 */ 5480 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 5481 case SCMD_WRITE: 5482 /* 6-byte scsi read cmd : 0x0A */ 5483 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 5484 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 5485 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5486 sec_count = scsipkt->pkt_cdbp[4]; 5487 /* sec_count 0 will be interpreted as 256 by a device */ 5488 break; 5489 case SCMD_WRITE_G1: 5490 /* 10-bytes scsi write command : 0x2A */ 5491 lba = scsipkt->pkt_cdbp[2]; 5492 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5493 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5494 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5495 sec_count = scsipkt->pkt_cdbp[7]; 5496 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 5497 break; 5498 case SCMD_WRITE_G5: 5499 /* 12-bytes scsi read command : 0xAA */ 5500 lba = scsipkt->pkt_cdbp[2]; 5501 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5502 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5503 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5504 sec_count = scsipkt->pkt_cdbp[6]; 5505 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 5506 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 5507 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 5508 break; 5509 case SCMD_WRITE_G4: 5510 /* 16-bytes scsi write command : 0x8A */ 5511 lba = scsipkt->pkt_cdbp[2]; 5512 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5513 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5514 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5515 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 5516 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 5517 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 5518 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 5519 sec_count = scsipkt->pkt_cdbp[10]; 5520 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 5521 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 5522 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 5523 break; 5524 default: 5525 /* Unsupported command */ 5526 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5527 return (sata_txlt_invalid_command(spx)); 5528 } 5529 5530 /* 5531 * Check if specified address and length exceeds device capacity 5532 */ 5533 if ((lba >= sdinfo->satadrv_capacity) || 5534 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 5535 /* LBA out of range */ 5536 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5537 return (sata_txlt_lba_out_of_range(spx)); 5538 } 5539 5540 /* 5541 * For zero-length transfer, emulate good completion of the command 5542 * (reasons for rejecting the command were already checked). 5543 * No DMA resources were allocated. 5544 */ 5545 if (spx->txlt_dma_cookie_list == NULL) { 5546 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5547 return (sata_emul_rw_completion(spx)); 5548 } 5549 5550 /* 5551 * Build cmd block depending on the device capability and 5552 * requested operation mode. 5553 * Do not bother with non-dma mode- we are working only with 5554 * devices supporting DMA. 5555 */ 5556 scmd->satacmd_addr_type = ATA_ADDR_LBA; 5557 scmd->satacmd_device_reg = SATA_ADH_LBA; 5558 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA; 5559 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 5560 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 5561 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT; 5562 scmd->satacmd_sec_count_msb = sec_count >> 8; 5563 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 5564 #ifndef __lock_lint 5565 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 5566 scmd->satacmd_lba_high_msb = lba >> 40; 5567 #endif 5568 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 5569 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 5570 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 5571 } 5572 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 5573 scmd->satacmd_lba_low_lsb = lba & 0xff; 5574 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 5575 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 5576 scmd->satacmd_features_reg = 0; 5577 scmd->satacmd_status_reg = 0; 5578 scmd->satacmd_error_reg = 0; 5579 5580 /* 5581 * Check if queueing commands should be used and switch 5582 * to appropriate command if possible 5583 */ 5584 if (sata_func_enable & SATA_ENABLE_QUEUING) { 5585 boolean_t using_queuing; 5586 5587 /* Queuing supported by controller and device? */ 5588 if ((sata_func_enable & SATA_ENABLE_NCQ) && 5589 (sdinfo->satadrv_features_support & 5590 SATA_DEV_F_NCQ) && 5591 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 5592 SATA_CTLF_NCQ)) { 5593 using_queuing = B_TRUE; 5594 5595 /* NCQ supported - use FPDMA WRITE */ 5596 scmd->satacmd_cmd_reg = 5597 SATAC_WRITE_FPDMA_QUEUED; 5598 scmd->satacmd_features_reg_ext = 5599 scmd->satacmd_sec_count_msb; 5600 scmd->satacmd_sec_count_msb = 0; 5601 } else if ((sdinfo->satadrv_features_support & 5602 SATA_DEV_F_TCQ) && 5603 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 5604 SATA_CTLF_QCMD)) { 5605 using_queuing = B_TRUE; 5606 5607 /* Legacy queueing */ 5608 if (sdinfo->satadrv_features_support & 5609 SATA_DEV_F_LBA48) { 5610 scmd->satacmd_cmd_reg = 5611 SATAC_WRITE_DMA_QUEUED_EXT; 5612 scmd->satacmd_features_reg_ext = 5613 scmd->satacmd_sec_count_msb; 5614 scmd->satacmd_sec_count_msb = 0; 5615 } else { 5616 scmd->satacmd_cmd_reg = 5617 SATAC_WRITE_DMA_QUEUED; 5618 } 5619 } else /* NCQ nor legacy queuing not supported */ 5620 using_queuing = B_FALSE; 5621 5622 if (using_queuing) { 5623 scmd->satacmd_features_reg = 5624 scmd->satacmd_sec_count_lsb; 5625 scmd->satacmd_sec_count_lsb = 0; 5626 scmd->satacmd_flags.sata_queued = B_TRUE; 5627 /* Set-up maximum queue depth */ 5628 scmd->satacmd_flags.sata_max_queue_depth = 5629 sdinfo->satadrv_max_queue_depth - 1; 5630 } else if (sdinfo->satadrv_features_enabled & 5631 SATA_DEV_F_E_UNTAGGED_QING) { 5632 /* 5633 * Although NCQ/TCQ is not enabled, untagged queuing 5634 * may be still used. 5635 * Set-up the maximum untagged queue depth. 5636 * Use controller's queue depth from sata_hba_tran. 5637 * SATA HBA drivers may ignore this value and rely on 5638 * the internal limits. For drivera that do not 5639 * ignore untaged queue depth, limit the value to 5640 * SATA_MAX_QUEUE_DEPTH (32), as this is the 5641 * largest value that can be passed via 5642 * satacmd_flags.sata_max_queue_depth. 5643 */ 5644 scmd->satacmd_flags.sata_max_queue_depth = 5645 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ? 5646 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1; 5647 5648 } else { 5649 scmd->satacmd_flags.sata_max_queue_depth = 0; 5650 } 5651 } else 5652 scmd->satacmd_flags.sata_max_queue_depth = 0; 5653 5654 SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5655 "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n", 5656 scmd->satacmd_cmd_reg, lba, sec_count); 5657 5658 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5659 /* Need callback function */ 5660 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 5661 synch = FALSE; 5662 } else 5663 synch = TRUE; 5664 5665 /* Transfer command to HBA */ 5666 if (sata_hba_start(spx, &rval) != 0) { 5667 /* Pkt not accepted for execution */ 5668 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5669 return (rval); 5670 } 5671 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5672 5673 /* 5674 * If execution is non-synchronous, 5675 * a callback function will handle potential errors, translate 5676 * the response and will do a callback to a target driver. 5677 * If it was synchronous, check execution status using the same 5678 * framework callback. 5679 */ 5680 if (synch) { 5681 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5682 "synchronous execution status %x\n", 5683 spx->txlt_sata_pkt->satapkt_reason); 5684 sata_txlt_rw_completion(spx->txlt_sata_pkt); 5685 } 5686 return (TRAN_ACCEPT); 5687 } 5688 5689 5690 /* 5691 * Implements SCSI SBC WRITE BUFFER command download microcode option 5692 */ 5693 static int 5694 sata_txlt_write_buffer(sata_pkt_txlate_t *spx) 5695 { 5696 #define WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE 4 5697 #define WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE 5 5698 5699 sata_hba_inst_t *sata_hba_inst = SATA_TXLT_HBA_INST(spx); 5700 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5701 struct sata_pkt *sata_pkt = spx->txlt_sata_pkt; 5702 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5703 5704 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5705 struct scsi_extended_sense *sense; 5706 int rval, mode, sector_count, reason; 5707 int cport = SATA_TXLT_CPORT(spx); 5708 5709 mode = scsipkt->pkt_cdbp[1] & 0x1f; 5710 5711 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5712 "sata_txlt_write_buffer, mode 0x%x\n", mode); 5713 5714 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5715 5716 if ((rval = sata_txlt_generic_pkt_info(spx, &reason)) != TRAN_ACCEPT) { 5717 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5718 return (rval); 5719 } 5720 /* 5721 * If in interrupt context, reject this packet because it would issue 5722 * a synchronous command to HBA. 5723 */ 5724 if (servicing_interrupt()) { 5725 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 5726 "sata_txlt_write_buffer: rejecting command because " 5727 "of interrupt context\n", NULL); 5728 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5729 return (TRAN_BUSY); 5730 } 5731 5732 /* Use synchronous mode */ 5733 spx->txlt_sata_pkt->satapkt_op_mode 5734 |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 5735 5736 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 5737 5738 scsipkt->pkt_reason = CMD_CMPLT; 5739 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5740 STATE_SENT_CMD | STATE_GOT_STATUS; 5741 5742 /* 5743 * The SCSI to ATA translation specification only calls 5744 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE. 5745 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but 5746 * ATA 8 (draft) got rid of download microcode for temp 5747 * and it is even optional for ATA 7, so it may be aborted. 5748 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as 5749 * it is not specified and the buffer offset for SCSI is a 16-bit 5750 * value in bytes, but for ATA it is a 16-bit offset in 512 byte 5751 * sectors. Thus the offset really doesn't buy us anything. 5752 * If and when ATA 8 is stabilized and the SCSI to ATA specification 5753 * is revised, this can be revisisted. 5754 */ 5755 /* Reject not supported request */ 5756 switch (mode) { 5757 case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE: 5758 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP; 5759 break; 5760 case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE: 5761 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE; 5762 break; 5763 default: 5764 goto bad_param; 5765 } 5766 5767 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 5768 5769 scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE; 5770 if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0) 5771 goto bad_param; 5772 sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE; 5773 scmd->satacmd_sec_count_lsb = (uint8_t)sector_count; 5774 scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8; 5775 scmd->satacmd_lba_mid_lsb = 0; 5776 scmd->satacmd_lba_high_lsb = 0; 5777 scmd->satacmd_device_reg = 0; 5778 spx->txlt_sata_pkt->satapkt_comp = NULL; 5779 scmd->satacmd_addr_type = 0; 5780 5781 /* Transfer command to HBA */ 5782 if (sata_hba_start(spx, &rval) != 0) { 5783 /* Pkt not accepted for execution */ 5784 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 5785 return (rval); 5786 } 5787 5788 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 5789 5790 /* Then we need synchronous check the status of the disk */ 5791 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5792 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 5793 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 5794 scsipkt->pkt_reason = CMD_CMPLT; 5795 5796 /* Download commmand succeed, so probe and identify device */ 5797 sata_reidentify_device(spx); 5798 } else { 5799 /* Something went wrong, microcode download command failed */ 5800 scsipkt->pkt_reason = CMD_INCOMPLETE; 5801 *scsipkt->pkt_scbp = STATUS_CHECK; 5802 sense = sata_arq_sense(spx); 5803 switch (sata_pkt->satapkt_reason) { 5804 case SATA_PKT_PORT_ERROR: 5805 /* 5806 * We have no device data. Assume no data transfered. 5807 */ 5808 sense->es_key = KEY_HARDWARE_ERROR; 5809 break; 5810 5811 case SATA_PKT_DEV_ERROR: 5812 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 5813 SATA_STATUS_ERR) { 5814 /* 5815 * determine dev error reason from error 5816 * reg content 5817 */ 5818 sata_decode_device_error(spx, sense); 5819 break; 5820 } 5821 /* No extended sense key - no info available */ 5822 break; 5823 5824 case SATA_PKT_TIMEOUT: 5825 scsipkt->pkt_reason = CMD_TIMEOUT; 5826 scsipkt->pkt_statistics |= 5827 STAT_TIMEOUT | STAT_DEV_RESET; 5828 /* No extended sense key ? */ 5829 break; 5830 5831 case SATA_PKT_ABORTED: 5832 scsipkt->pkt_reason = CMD_ABORTED; 5833 scsipkt->pkt_statistics |= STAT_ABORTED; 5834 /* No extended sense key ? */ 5835 break; 5836 5837 case SATA_PKT_RESET: 5838 /* pkt aborted by an explicit reset from a host */ 5839 scsipkt->pkt_reason = CMD_RESET; 5840 scsipkt->pkt_statistics |= STAT_DEV_RESET; 5841 break; 5842 5843 default: 5844 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 5845 "sata_txlt_nodata_cmd_completion: " 5846 "invalid packet completion reason %d", 5847 sata_pkt->satapkt_reason)); 5848 scsipkt->pkt_reason = CMD_TRAN_ERR; 5849 break; 5850 } 5851 5852 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5853 "scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5854 5855 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 5856 /* scsi callback required */ 5857 scsi_hba_pkt_comp(scsipkt); 5858 } 5859 return (TRAN_ACCEPT); 5860 5861 bad_param: 5862 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5863 *scsipkt->pkt_scbp = STATUS_CHECK; 5864 sense = sata_arq_sense(spx); 5865 sense->es_key = KEY_ILLEGAL_REQUEST; 5866 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5867 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5868 scsipkt->pkt_comp != NULL) { 5869 /* scsi callback required */ 5870 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5871 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 5872 TQ_SLEEP) == 0) { 5873 /* Scheduling the callback failed */ 5874 rval = TRAN_BUSY; 5875 } 5876 } 5877 return (rval); 5878 } 5879 5880 /* 5881 * Re-identify device after doing a firmware download. 5882 */ 5883 static void 5884 sata_reidentify_device(sata_pkt_txlate_t *spx) 5885 { 5886 #define DOWNLOAD_WAIT_TIME_SECS 60 5887 #define DOWNLOAD_WAIT_INTERVAL_SECS 1 5888 int rval; 5889 int retry_cnt; 5890 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5891 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 5892 sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device; 5893 sata_drive_info_t *sdinfo; 5894 5895 /* 5896 * Before returning good status, probe device. 5897 * Device probing will get IDENTIFY DEVICE data, if possible. 5898 * The assumption is that the new microcode is applied by the 5899 * device. It is a caller responsibility to verify this. 5900 */ 5901 for (retry_cnt = 0; 5902 retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS; 5903 retry_cnt++) { 5904 rval = sata_probe_device(sata_hba_inst, &sata_device); 5905 5906 if (rval == SATA_SUCCESS) { /* Set default features */ 5907 sdinfo = sata_get_device_info(sata_hba_inst, 5908 &sata_device); 5909 if (sata_initialize_device(sata_hba_inst, sdinfo) != 5910 SATA_SUCCESS) { 5911 /* retry */ 5912 rval = sata_initialize_device(sata_hba_inst, 5913 sdinfo); 5914 if (rval == SATA_RETRY) 5915 sata_log(sata_hba_inst, CE_WARN, 5916 "SATA device at port %d pmport %d -" 5917 " default device features could not" 5918 " be set. Device may not operate " 5919 "as expected.", 5920 sata_device.satadev_addr.cport, 5921 sata_device.satadev_addr.pmport); 5922 } 5923 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 5924 scsi_hba_pkt_comp(scsipkt); 5925 return; 5926 } else if (rval == SATA_RETRY) { 5927 delay(drv_usectohz(1000000 * 5928 DOWNLOAD_WAIT_INTERVAL_SECS)); 5929 continue; 5930 } else /* failed - no reason to retry */ 5931 break; 5932 } 5933 5934 /* 5935 * Something went wrong, device probing failed. 5936 */ 5937 SATA_LOG_D((sata_hba_inst, CE_WARN, 5938 "Cannot probe device after downloading microcode\n")); 5939 5940 /* Reset device to force retrying the probe. */ 5941 (void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 5942 (SATA_DIP(sata_hba_inst), &sata_device); 5943 5944 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 5945 scsi_hba_pkt_comp(scsipkt); 5946 } 5947 5948 5949 /* 5950 * Translate command: Synchronize Cache. 5951 * Translates into Flush Cache command for SATA hard disks. 5952 * 5953 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 5954 * appropriate values in scsi_pkt fields. 5955 */ 5956 static int 5957 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx) 5958 { 5959 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5960 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 5961 int cport = SATA_TXLT_CPORT(spx); 5962 int rval, reason; 5963 int synch; 5964 5965 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5966 5967 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 5968 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5969 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5970 return (rval); 5971 } 5972 5973 scmd->satacmd_addr_type = 0; 5974 scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE; 5975 scmd->satacmd_device_reg = 0; 5976 scmd->satacmd_sec_count_lsb = 0; 5977 scmd->satacmd_lba_low_lsb = 0; 5978 scmd->satacmd_lba_mid_lsb = 0; 5979 scmd->satacmd_lba_high_lsb = 0; 5980 scmd->satacmd_features_reg = 0; 5981 scmd->satacmd_status_reg = 0; 5982 scmd->satacmd_error_reg = 0; 5983 5984 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5985 "sata_txlt_synchronize_cache\n", NULL); 5986 5987 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5988 /* Need to set-up a callback function */ 5989 spx->txlt_sata_pkt->satapkt_comp = 5990 sata_txlt_nodata_cmd_completion; 5991 synch = FALSE; 5992 } else 5993 synch = TRUE; 5994 5995 /* Transfer command to HBA */ 5996 if (sata_hba_start(spx, &rval) != 0) { 5997 /* Pkt not accepted for execution */ 5998 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5999 return (rval); 6000 } 6001 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 6002 6003 /* 6004 * If execution non-synchronous, it had to be completed 6005 * a callback function will handle potential errors, translate 6006 * the response and will do a callback to a target driver. 6007 * If it was synchronous, check status, using the same 6008 * framework callback. 6009 */ 6010 if (synch) { 6011 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6012 "synchronous execution status %x\n", 6013 spx->txlt_sata_pkt->satapkt_reason); 6014 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 6015 } 6016 return (TRAN_ACCEPT); 6017 } 6018 6019 6020 /* 6021 * Send pkt to SATA HBA driver 6022 * 6023 * This function may be called only if the operation is requested by scsi_pkt, 6024 * i.e. scsi_pkt is not NULL. 6025 * 6026 * This function has to be called with cport mutex held. It does release 6027 * the mutex when it calls HBA driver sata_tran_start function and 6028 * re-acquires it afterwards. 6029 * 6030 * If return value is 0, pkt was accepted, -1 otherwise 6031 * rval is set to appropriate sata_scsi_start return value. 6032 * 6033 * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not 6034 * have called the sata_pkt callback function for this packet. 6035 * 6036 * The scsi callback has to be performed by the caller of this routine. 6037 */ 6038 static int 6039 sata_hba_start(sata_pkt_txlate_t *spx, int *rval) 6040 { 6041 int stat; 6042 uint8_t cport = SATA_TXLT_CPORT(spx); 6043 uint8_t pmport = SATA_TXLT_PMPORT(spx); 6044 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 6045 sata_drive_info_t *sdinfo; 6046 sata_pmult_info_t *pminfo; 6047 sata_pmport_info_t *pmportinfo = NULL; 6048 sata_device_t *sata_device = NULL; 6049 uint8_t cmd; 6050 struct sata_cmd_flags cmd_flags; 6051 6052 ASSERT(spx->txlt_sata_pkt != NULL); 6053 6054 ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6055 6056 sdinfo = sata_get_device_info(sata_hba_inst, 6057 &spx->txlt_sata_pkt->satapkt_device); 6058 ASSERT(sdinfo != NULL); 6059 6060 /* Clear device reset state? */ 6061 /* qual should be XXX_DPMPORT, but add XXX_PMPORT in case */ 6062 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT || 6063 sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) { 6064 6065 /* 6066 * Get the pmult_info of the its parent port multiplier, all 6067 * sub-devices share a common device reset flags on in 6068 * pmult_info. 6069 */ 6070 pminfo = SATA_PMULT_INFO(sata_hba_inst, cport); 6071 pmportinfo = pminfo->pmult_dev_port[pmport]; 6072 ASSERT(pminfo != NULL); 6073 if (pminfo->pmult_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) { 6074 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 6075 sata_clear_dev_reset = B_TRUE; 6076 pminfo->pmult_event_flags &= 6077 ~SATA_EVNT_CLEAR_DEVICE_RESET; 6078 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6079 "sata_hba_start: clearing device reset state" 6080 "on pmult.\n", NULL); 6081 } 6082 } else { 6083 if (sdinfo->satadrv_event_flags & 6084 SATA_EVNT_CLEAR_DEVICE_RESET) { 6085 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 6086 sata_clear_dev_reset = B_TRUE; 6087 sdinfo->satadrv_event_flags &= 6088 ~SATA_EVNT_CLEAR_DEVICE_RESET; 6089 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6090 "sata_hba_start: clearing device reset state\n", 6091 NULL); 6092 } 6093 } 6094 6095 cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg; 6096 cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags; 6097 sata_device = &spx->txlt_sata_pkt->satapkt_device; 6098 6099 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6100 6101 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6102 "Sata cmd 0x%2x\n", cmd); 6103 6104 stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 6105 spx->txlt_sata_pkt); 6106 6107 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6108 /* 6109 * If sata pkt was accepted and executed in asynchronous mode, i.e. 6110 * with the sata callback, the sata_pkt could be already destroyed 6111 * by the time we check ther return status from the hba_start() 6112 * function, because sata_scsi_destroy_pkt() could have been already 6113 * called (perhaps in the interrupt context). So, in such case, there 6114 * should be no references to it. In other cases, sata_pkt still 6115 * exists. 6116 */ 6117 if (stat == SATA_TRAN_ACCEPTED) { 6118 /* 6119 * pkt accepted for execution. 6120 * If it was executed synchronously, it is already completed 6121 * and pkt completion_reason indicates completion status. 6122 */ 6123 *rval = TRAN_ACCEPT; 6124 return (0); 6125 } 6126 6127 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 6128 switch (stat) { 6129 case SATA_TRAN_QUEUE_FULL: 6130 /* 6131 * Controller detected queue full condition. 6132 */ 6133 SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst, 6134 "sata_hba_start: queue full\n", NULL); 6135 6136 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 6137 *spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL; 6138 6139 *rval = TRAN_BUSY; 6140 break; 6141 6142 case SATA_TRAN_PORT_ERROR: 6143 /* 6144 * Communication/link with device or general port error 6145 * detected before pkt execution begun. 6146 */ 6147 if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 6148 SATA_ADDR_CPORT || 6149 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 6150 SATA_ADDR_DCPORT) 6151 sata_log(sata_hba_inst, CE_CONT, 6152 "SATA port %d error", 6153 sata_device->satadev_addr.cport); 6154 else 6155 sata_log(sata_hba_inst, CE_CONT, 6156 "SATA port %d:%d error\n", 6157 sata_device->satadev_addr.cport, 6158 sata_device->satadev_addr.pmport); 6159 6160 /* 6161 * Update the port/device structure. 6162 * sata_pkt should be still valid. Since port error is 6163 * returned, sata_device content should reflect port 6164 * state - it means, that sata address have been changed, 6165 * because original packet's sata address refered to a device 6166 * attached to some port. 6167 */ 6168 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT || 6169 sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) { 6170 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6171 mutex_enter(&pmportinfo->pmport_mutex); 6172 sata_update_pmport_info(sata_hba_inst, sata_device); 6173 mutex_exit(&pmportinfo->pmport_mutex); 6174 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6175 } else { 6176 sata_update_port_info(sata_hba_inst, sata_device); 6177 } 6178 6179 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 6180 *rval = TRAN_FATAL_ERROR; 6181 break; 6182 6183 case SATA_TRAN_CMD_UNSUPPORTED: 6184 /* 6185 * Command rejected by HBA as unsupported. It was HBA driver 6186 * that rejected the command, command was not sent to 6187 * an attached device. 6188 */ 6189 if ((sdinfo != NULL) && 6190 (sdinfo->satadrv_state & SATA_DSTATE_RESET)) 6191 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6192 "sat_hba_start: cmd 0x%2x rejected " 6193 "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd); 6194 6195 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6196 (void) sata_txlt_invalid_command(spx); 6197 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6198 6199 *rval = TRAN_ACCEPT; 6200 break; 6201 6202 case SATA_TRAN_BUSY: 6203 /* 6204 * Command rejected by HBA because other operation prevents 6205 * accepting the packet, or device is in RESET condition. 6206 */ 6207 if (sdinfo != NULL) { 6208 sdinfo->satadrv_state = 6209 spx->txlt_sata_pkt->satapkt_device.satadev_state; 6210 6211 if (sdinfo->satadrv_state & SATA_DSTATE_RESET) { 6212 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6213 "sata_hba_start: cmd 0x%2x rejected " 6214 "because of device reset condition\n", 6215 cmd); 6216 } else { 6217 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6218 "sata_hba_start: cmd 0x%2x rejected " 6219 "with SATA_TRAN_BUSY status\n", 6220 cmd); 6221 } 6222 } 6223 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 6224 *rval = TRAN_BUSY; 6225 break; 6226 6227 default: 6228 /* Unrecognized HBA response */ 6229 SATA_LOG_D((sata_hba_inst, CE_WARN, 6230 "sata_hba_start: unrecognized HBA response " 6231 "to cmd : 0x%2x resp 0x%x", cmd, rval)); 6232 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 6233 *rval = TRAN_FATAL_ERROR; 6234 break; 6235 } 6236 6237 /* 6238 * If we got here, the packet was rejected. 6239 * Check if we need to remember reset state clearing request 6240 */ 6241 if (cmd_flags.sata_clear_dev_reset) { 6242 /* 6243 * Check if device is still configured - it may have 6244 * disapeared from the configuration 6245 */ 6246 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 6247 if (sdinfo != NULL) { 6248 /* 6249 * Restore the flag that requests clearing of 6250 * the device reset state, 6251 * so the next sata packet may carry it to HBA. 6252 */ 6253 if (sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT || 6254 sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT) { 6255 pminfo->pmult_event_flags |= 6256 SATA_EVNT_CLEAR_DEVICE_RESET; 6257 } else { 6258 sdinfo->satadrv_event_flags |= 6259 SATA_EVNT_CLEAR_DEVICE_RESET; 6260 } 6261 } 6262 } 6263 return (-1); 6264 } 6265 6266 /* 6267 * Scsi response setup for invalid LBA 6268 * 6269 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 6270 */ 6271 static int 6272 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx) 6273 { 6274 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6275 struct scsi_extended_sense *sense; 6276 6277 scsipkt->pkt_reason = CMD_CMPLT; 6278 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6279 STATE_SENT_CMD | STATE_GOT_STATUS; 6280 *scsipkt->pkt_scbp = STATUS_CHECK; 6281 6282 *scsipkt->pkt_scbp = STATUS_CHECK; 6283 sense = sata_arq_sense(spx); 6284 sense->es_key = KEY_ILLEGAL_REQUEST; 6285 sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE; 6286 6287 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6288 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6289 6290 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6291 scsipkt->pkt_comp != NULL) 6292 /* scsi callback required */ 6293 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6294 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 6295 TQ_SLEEP) == NULL) 6296 /* Scheduling the callback failed */ 6297 return (TRAN_BUSY); 6298 return (TRAN_ACCEPT); 6299 } 6300 6301 6302 /* 6303 * Analyze device status and error registers and translate them into 6304 * appropriate scsi sense codes. 6305 * NOTE: non-packet commands only for now 6306 */ 6307 static void 6308 sata_decode_device_error(sata_pkt_txlate_t *spx, 6309 struct scsi_extended_sense *sense) 6310 { 6311 uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg; 6312 6313 ASSERT(sense != NULL); 6314 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 6315 SATA_STATUS_ERR); 6316 6317 6318 if (err_reg & SATA_ERROR_ICRC) { 6319 sense->es_key = KEY_ABORTED_COMMAND; 6320 sense->es_add_code = 0x08; /* Communication failure */ 6321 return; 6322 } 6323 6324 if (err_reg & SATA_ERROR_UNC) { 6325 sense->es_key = KEY_MEDIUM_ERROR; 6326 /* Information bytes (LBA) need to be set by a caller */ 6327 return; 6328 } 6329 6330 /* ADD HERE: MC error bit handling for ATAPI CD/DVD */ 6331 if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) { 6332 sense->es_key = KEY_UNIT_ATTENTION; 6333 sense->es_add_code = 0x3a; /* No media present */ 6334 return; 6335 } 6336 6337 if (err_reg & SATA_ERROR_IDNF) { 6338 if (err_reg & SATA_ERROR_ABORT) { 6339 sense->es_key = KEY_ABORTED_COMMAND; 6340 } else { 6341 sense->es_key = KEY_ILLEGAL_REQUEST; 6342 sense->es_add_code = 0x21; /* LBA out of range */ 6343 } 6344 return; 6345 } 6346 6347 if (err_reg & SATA_ERROR_ABORT) { 6348 ASSERT(spx->txlt_sata_pkt != NULL); 6349 sense->es_key = KEY_ABORTED_COMMAND; 6350 return; 6351 } 6352 } 6353 6354 /* 6355 * Extract error LBA from sata_pkt.satapkt_cmd register fields 6356 */ 6357 static void 6358 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba) 6359 { 6360 sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd; 6361 6362 *lba = 0; 6363 if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) { 6364 *lba = sata_cmd->satacmd_lba_high_msb; 6365 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb; 6366 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb; 6367 } else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) { 6368 *lba = sata_cmd->satacmd_device_reg & 0xf; 6369 } 6370 *lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb; 6371 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb; 6372 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb; 6373 } 6374 6375 /* 6376 * This is fixed sense format - if LBA exceeds the info field size, 6377 * no valid info will be returned (valid bit in extended sense will 6378 * be set to 0). 6379 */ 6380 static struct scsi_extended_sense * 6381 sata_arq_sense(sata_pkt_txlate_t *spx) 6382 { 6383 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6384 struct scsi_arq_status *arqs; 6385 struct scsi_extended_sense *sense; 6386 6387 /* Fill ARQ sense data */ 6388 scsipkt->pkt_state |= STATE_ARQ_DONE; 6389 arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp; 6390 *(uchar_t *)&arqs->sts_status = STATUS_CHECK; 6391 *(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD; 6392 arqs->sts_rqpkt_reason = CMD_CMPLT; 6393 arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6394 STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS; 6395 arqs->sts_rqpkt_resid = 0; 6396 sense = &arqs->sts_sensedata; 6397 bzero(sense, sizeof (struct scsi_extended_sense)); 6398 sata_fixed_sense_data_preset(sense); 6399 return (sense); 6400 } 6401 6402 6403 /* 6404 * Emulated SATA Read/Write command completion for zero-length requests. 6405 * This request always succedes, so in synchronous mode it always returns 6406 * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the 6407 * callback cannot be scheduled. 6408 */ 6409 static int 6410 sata_emul_rw_completion(sata_pkt_txlate_t *spx) 6411 { 6412 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6413 6414 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6415 STATE_SENT_CMD | STATE_GOT_STATUS; 6416 scsipkt->pkt_reason = CMD_CMPLT; 6417 *scsipkt->pkt_scbp = STATUS_GOOD; 6418 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 6419 /* scsi callback required - have to schedule it */ 6420 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6421 (task_func_t *)scsipkt->pkt_comp, 6422 (void *)scsipkt, TQ_SLEEP) == NULL) 6423 /* Scheduling the callback failed */ 6424 return (TRAN_BUSY); 6425 } 6426 return (TRAN_ACCEPT); 6427 } 6428 6429 6430 /* 6431 * Translate completion status of SATA read/write commands into scsi response. 6432 * pkt completion_reason is checked to determine the completion status. 6433 * Do scsi callback if necessary. 6434 * 6435 * Note: this function may be called also for synchronously executed 6436 * commands. 6437 * This function may be used only if scsi_pkt is non-NULL. 6438 */ 6439 static void 6440 sata_txlt_rw_completion(sata_pkt_t *sata_pkt) 6441 { 6442 sata_pkt_txlate_t *spx = 6443 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 6444 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 6445 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6446 struct scsi_extended_sense *sense; 6447 uint64_t lba; 6448 struct buf *bp; 6449 int rval; 6450 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 6451 /* Normal completion */ 6452 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6453 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 6454 scsipkt->pkt_reason = CMD_CMPLT; 6455 *scsipkt->pkt_scbp = STATUS_GOOD; 6456 if (spx->txlt_tmp_buf != NULL) { 6457 /* Temporary buffer was used */ 6458 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 6459 if (bp->b_flags & B_READ) { 6460 rval = ddi_dma_sync( 6461 spx->txlt_buf_dma_handle, 0, 0, 6462 DDI_DMA_SYNC_FORCPU); 6463 ASSERT(rval == DDI_SUCCESS); 6464 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 6465 bp->b_bcount); 6466 } 6467 } 6468 } else { 6469 /* 6470 * Something went wrong - analyze return 6471 */ 6472 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6473 STATE_SENT_CMD | STATE_GOT_STATUS; 6474 scsipkt->pkt_reason = CMD_INCOMPLETE; 6475 *scsipkt->pkt_scbp = STATUS_CHECK; 6476 sense = sata_arq_sense(spx); 6477 ASSERT(sense != NULL); 6478 6479 /* 6480 * SATA_PKT_DEV_ERROR is the only case where we may be able to 6481 * extract from device registers the failing LBA. 6482 */ 6483 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 6484 if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) && 6485 (scmd->satacmd_lba_mid_msb != 0 || 6486 scmd->satacmd_lba_high_msb != 0)) { 6487 /* 6488 * We have problem reporting this cmd LBA 6489 * in fixed sense data format, because of 6490 * the size of the scsi LBA fields. 6491 */ 6492 sense->es_valid = 0; 6493 } else { 6494 sata_extract_error_lba(spx, &lba); 6495 sense->es_info_1 = (lba & 0xFF000000) >> 24; 6496 sense->es_info_2 = (lba & 0xFF0000) >> 16; 6497 sense->es_info_3 = (lba & 0xFF00) >> 8; 6498 sense->es_info_4 = lba & 0xFF; 6499 } 6500 } else { 6501 /* Invalid extended sense info */ 6502 sense->es_valid = 0; 6503 } 6504 6505 switch (sata_pkt->satapkt_reason) { 6506 case SATA_PKT_PORT_ERROR: 6507 /* We may want to handle DEV GONE state as well */ 6508 /* 6509 * We have no device data. Assume no data transfered. 6510 */ 6511 sense->es_key = KEY_HARDWARE_ERROR; 6512 break; 6513 6514 case SATA_PKT_DEV_ERROR: 6515 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 6516 SATA_STATUS_ERR) { 6517 /* 6518 * determine dev error reason from error 6519 * reg content 6520 */ 6521 sata_decode_device_error(spx, sense); 6522 if (sense->es_key == KEY_MEDIUM_ERROR) { 6523 switch (scmd->satacmd_cmd_reg) { 6524 case SATAC_READ_DMA: 6525 case SATAC_READ_DMA_EXT: 6526 case SATAC_READ_DMA_QUEUED: 6527 case SATAC_READ_DMA_QUEUED_EXT: 6528 case SATAC_READ_FPDMA_QUEUED: 6529 /* Unrecovered read error */ 6530 sense->es_add_code = 6531 SD_SCSI_ASC_UNREC_READ_ERR; 6532 break; 6533 case SATAC_WRITE_DMA: 6534 case SATAC_WRITE_DMA_EXT: 6535 case SATAC_WRITE_DMA_QUEUED: 6536 case SATAC_WRITE_DMA_QUEUED_EXT: 6537 case SATAC_WRITE_FPDMA_QUEUED: 6538 /* Write error */ 6539 sense->es_add_code = 6540 SD_SCSI_ASC_WRITE_ERR; 6541 break; 6542 default: 6543 /* Internal error */ 6544 SATA_LOG_D(( 6545 spx->txlt_sata_hba_inst, 6546 CE_WARN, 6547 "sata_txlt_rw_completion :" 6548 "internal error - invalid " 6549 "command 0x%2x", 6550 scmd->satacmd_cmd_reg)); 6551 break; 6552 } 6553 } 6554 break; 6555 } 6556 /* No extended sense key - no info available */ 6557 scsipkt->pkt_reason = CMD_INCOMPLETE; 6558 break; 6559 6560 case SATA_PKT_TIMEOUT: 6561 scsipkt->pkt_reason = CMD_TIMEOUT; 6562 scsipkt->pkt_statistics |= 6563 STAT_TIMEOUT | STAT_DEV_RESET; 6564 sense->es_key = KEY_ABORTED_COMMAND; 6565 break; 6566 6567 case SATA_PKT_ABORTED: 6568 scsipkt->pkt_reason = CMD_ABORTED; 6569 scsipkt->pkt_statistics |= STAT_ABORTED; 6570 sense->es_key = KEY_ABORTED_COMMAND; 6571 break; 6572 6573 case SATA_PKT_RESET: 6574 scsipkt->pkt_reason = CMD_RESET; 6575 scsipkt->pkt_statistics |= STAT_DEV_RESET; 6576 sense->es_key = KEY_ABORTED_COMMAND; 6577 break; 6578 6579 default: 6580 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 6581 "sata_txlt_rw_completion: " 6582 "invalid packet completion reason")); 6583 scsipkt->pkt_reason = CMD_TRAN_ERR; 6584 break; 6585 } 6586 } 6587 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6588 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6589 6590 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 6591 /* scsi callback required */ 6592 scsi_hba_pkt_comp(scsipkt); 6593 } 6594 6595 6596 /* 6597 * Translate completion status of non-data commands (i.e. commands returning 6598 * no data). 6599 * pkt completion_reason is checked to determine the completion status. 6600 * Do scsi callback if necessary (FLAG_NOINTR == 0) 6601 * 6602 * Note: this function may be called also for synchronously executed 6603 * commands. 6604 * This function may be used only if scsi_pkt is non-NULL. 6605 */ 6606 6607 static void 6608 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt) 6609 { 6610 sata_pkt_txlate_t *spx = 6611 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 6612 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6613 6614 sata_set_arq_data(sata_pkt); 6615 6616 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 6617 /* scsi callback required */ 6618 scsi_hba_pkt_comp(scsipkt); 6619 } 6620 6621 static void 6622 sata_set_arq_data(sata_pkt_t *sata_pkt) 6623 { 6624 sata_pkt_txlate_t *spx = 6625 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 6626 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6627 struct scsi_extended_sense *sense; 6628 6629 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6630 STATE_SENT_CMD | STATE_GOT_STATUS; 6631 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 6632 /* Normal completion */ 6633 scsipkt->pkt_reason = CMD_CMPLT; 6634 *scsipkt->pkt_scbp = STATUS_GOOD; 6635 } else { 6636 /* Something went wrong */ 6637 scsipkt->pkt_reason = CMD_INCOMPLETE; 6638 *scsipkt->pkt_scbp = STATUS_CHECK; 6639 sense = sata_arq_sense(spx); 6640 switch (sata_pkt->satapkt_reason) { 6641 case SATA_PKT_PORT_ERROR: 6642 /* 6643 * We have no device data. Assume no data transfered. 6644 */ 6645 sense->es_key = KEY_HARDWARE_ERROR; 6646 break; 6647 6648 case SATA_PKT_DEV_ERROR: 6649 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 6650 SATA_STATUS_ERR) { 6651 /* 6652 * determine dev error reason from error 6653 * reg content 6654 */ 6655 sata_decode_device_error(spx, sense); 6656 break; 6657 } 6658 /* No extended sense key - no info available */ 6659 break; 6660 6661 case SATA_PKT_TIMEOUT: 6662 scsipkt->pkt_reason = CMD_TIMEOUT; 6663 scsipkt->pkt_statistics |= 6664 STAT_TIMEOUT | STAT_DEV_RESET; 6665 /* No extended sense key ? */ 6666 break; 6667 6668 case SATA_PKT_ABORTED: 6669 scsipkt->pkt_reason = CMD_ABORTED; 6670 scsipkt->pkt_statistics |= STAT_ABORTED; 6671 /* No extended sense key ? */ 6672 break; 6673 6674 case SATA_PKT_RESET: 6675 /* pkt aborted by an explicit reset from a host */ 6676 scsipkt->pkt_reason = CMD_RESET; 6677 scsipkt->pkt_statistics |= STAT_DEV_RESET; 6678 break; 6679 6680 default: 6681 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 6682 "sata_txlt_nodata_cmd_completion: " 6683 "invalid packet completion reason %d", 6684 sata_pkt->satapkt_reason)); 6685 scsipkt->pkt_reason = CMD_TRAN_ERR; 6686 break; 6687 } 6688 6689 } 6690 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6691 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6692 } 6693 6694 6695 /* 6696 * Build Mode sense R/W recovery page 6697 * NOT IMPLEMENTED 6698 */ 6699 6700 static int 6701 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6702 { 6703 #ifndef __lock_lint 6704 _NOTE(ARGUNUSED(sdinfo)) 6705 _NOTE(ARGUNUSED(pcntrl)) 6706 _NOTE(ARGUNUSED(buf)) 6707 #endif 6708 return (0); 6709 } 6710 6711 /* 6712 * Build Mode sense caching page - scsi-3 implementation. 6713 * Page length distinguishes previous format from scsi-3 format. 6714 * buf must have space for 0x12 bytes. 6715 * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable. 6716 * 6717 */ 6718 static int 6719 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6720 { 6721 struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf; 6722 sata_id_t *sata_id = &sdinfo->satadrv_id; 6723 6724 /* 6725 * Most of the fields are set to 0, being not supported and/or disabled 6726 */ 6727 bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3); 6728 6729 /* Saved paramters not supported */ 6730 if (pcntrl == 3) 6731 return (0); 6732 if (pcntrl == 0 || pcntrl == 2) { 6733 /* 6734 * For now treat current and default parameters as same 6735 * That may have to change, if target driver will complain 6736 */ 6737 page->mode_page.code = MODEPAGE_CACHING; /* PS = 0 */ 6738 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 6739 6740 if (SATA_READ_AHEAD_SUPPORTED(*sata_id) && 6741 !SATA_READ_AHEAD_ENABLED(*sata_id)) { 6742 page->dra = 1; /* Read Ahead disabled */ 6743 page->rcd = 1; /* Read Cache disabled */ 6744 } 6745 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) && 6746 SATA_WRITE_CACHE_ENABLED(*sata_id)) 6747 page->wce = 1; /* Write Cache enabled */ 6748 } else { 6749 /* Changeable parameters */ 6750 page->mode_page.code = MODEPAGE_CACHING; 6751 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 6752 if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) { 6753 page->dra = 1; 6754 page->rcd = 1; 6755 } 6756 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) 6757 page->wce = 1; 6758 } 6759 return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 6760 sizeof (struct mode_page)); 6761 } 6762 6763 /* 6764 * Build Mode sense exception cntrl page 6765 */ 6766 static int 6767 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6768 { 6769 struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf; 6770 sata_id_t *sata_id = &sdinfo->satadrv_id; 6771 6772 /* 6773 * Most of the fields are set to 0, being not supported and/or disabled 6774 */ 6775 bzero(buf, PAGELENGTH_INFO_EXCPT); 6776 6777 page->mode_page.code = MODEPAGE_INFO_EXCPT; 6778 page->mode_page.length = PAGELENGTH_INFO_EXCPT; 6779 6780 /* Indicate that this is page is saveable */ 6781 page->mode_page.ps = 1; 6782 6783 /* 6784 * We will return the same data for default, current and saved page. 6785 * The only changeable bit is dexcpt and that bit is required 6786 * by the ATA specification to be preserved across power cycles. 6787 */ 6788 if (pcntrl != 1) { 6789 page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED); 6790 page->mrie = MRIE_ONLY_ON_REQUEST; 6791 } 6792 else 6793 page->dexcpt = 1; /* Only changeable parameter */ 6794 6795 return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)); 6796 } 6797 6798 6799 static int 6800 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6801 { 6802 struct mode_acoustic_management *page = 6803 (struct mode_acoustic_management *)buf; 6804 sata_id_t *sata_id = &sdinfo->satadrv_id; 6805 6806 /* 6807 * Most of the fields are set to 0, being not supported and/or disabled 6808 */ 6809 bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT); 6810 6811 switch (pcntrl) { 6812 case P_CNTRL_DEFAULT: 6813 /* default paramters not supported */ 6814 return (0); 6815 6816 case P_CNTRL_CURRENT: 6817 case P_CNTRL_SAVED: 6818 /* Saved and current are supported and are identical */ 6819 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG; 6820 page->mode_page.length = 6821 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT; 6822 page->mode_page.ps = 1; 6823 6824 /* Word 83 indicates if feature is supported */ 6825 /* If feature is not supported */ 6826 if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) { 6827 page->acoustic_manag_enable = 6828 ACOUSTIC_DISABLED; 6829 } else { 6830 page->acoustic_manag_enable = 6831 ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT) 6832 != 0); 6833 /* Word 94 inidicates the value */ 6834 #ifdef _LITTLE_ENDIAN 6835 page->acoustic_manag_level = 6836 (uchar_t)sata_id->ai_acoustic; 6837 page->vendor_recommended_value = 6838 sata_id->ai_acoustic >> 8; 6839 #else 6840 page->acoustic_manag_level = 6841 sata_id->ai_acoustic >> 8; 6842 page->vendor_recommended_value = 6843 (uchar_t)sata_id->ai_acoustic; 6844 #endif 6845 } 6846 break; 6847 6848 case P_CNTRL_CHANGEABLE: 6849 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG; 6850 page->mode_page.length = 6851 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT; 6852 page->mode_page.ps = 1; 6853 6854 /* Word 83 indicates if the feature is supported */ 6855 if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) { 6856 page->acoustic_manag_enable = 6857 ACOUSTIC_ENABLED; 6858 page->acoustic_manag_level = 0xff; 6859 } 6860 break; 6861 } 6862 return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 6863 sizeof (struct mode_page)); 6864 } 6865 6866 6867 /* 6868 * Build Mode sense power condition page. 6869 */ 6870 static int 6871 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6872 { 6873 struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf; 6874 sata_id_t *sata_id = &sdinfo->satadrv_id; 6875 6876 /* 6877 * Most of the fields are set to 0, being not supported and/or disabled 6878 * power condition page length was 0x0a 6879 */ 6880 bzero(buf, sizeof (struct mode_info_power_cond)); 6881 6882 if (pcntrl == P_CNTRL_DEFAULT) { 6883 /* default paramters not supported */ 6884 return (0); 6885 } 6886 6887 page->mode_page.code = MODEPAGE_POWER_COND; 6888 page->mode_page.length = sizeof (struct mode_info_power_cond); 6889 6890 if (sata_id->ai_cap && SATA_STANDBYTIMER) { 6891 page->standby = 1; 6892 bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer, 6893 sizeof (uchar_t) * 4); 6894 } 6895 6896 return (sizeof (struct mode_info_power_cond)); 6897 } 6898 6899 /* 6900 * Process mode select caching page 8 (scsi3 format only). 6901 * Read Ahead (same as read cache) and Write Cache may be turned on and off 6902 * if these features are supported by the device. If these features are not 6903 * supported, the command will be terminated with STATUS_CHECK. 6904 * This function fails only if the SET FEATURE command sent to 6905 * the device fails. The page format is not varified, assuming that the 6906 * target driver operates correctly - if parameters length is too short, 6907 * we just drop the page. 6908 * Two command may be sent if both Read Cache/Read Ahead and Write Cache 6909 * setting have to be changed. 6910 * SET FEATURE command is executed synchronously, i.e. we wait here until 6911 * it is completed, regardless of the scsi pkt directives. 6912 * 6913 * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e. 6914 * changing DRA will change RCD. 6915 * 6916 * More than one SATA command may be executed to perform operations specified 6917 * by mode select pages. The first error terminates further execution. 6918 * Operations performed successully are not backed-up in such case. 6919 * 6920 * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 6921 * If operation resulted in changing device setup, dmod flag should be set to 6922 * one (1). If parameters were not changed, dmod flag should be set to 0. 6923 * Upon return, if operation required sending command to the device, the rval 6924 * should be set to the value returned by sata_hba_start. If operation 6925 * did not require device access, rval should be set to TRAN_ACCEPT. 6926 * The pagelen should be set to the length of the page. 6927 * 6928 * This function has to be called with a port mutex held. 6929 * 6930 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 6931 */ 6932 int 6933 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page, 6934 int parmlen, int *pagelen, int *rval, int *dmod) 6935 { 6936 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6937 sata_drive_info_t *sdinfo; 6938 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6939 sata_id_t *sata_id; 6940 struct scsi_extended_sense *sense; 6941 int wce, dra; /* Current settings */ 6942 6943 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 6944 &spx->txlt_sata_pkt->satapkt_device); 6945 sata_id = &sdinfo->satadrv_id; 6946 *dmod = 0; 6947 6948 /* Verify parameters length. If too short, drop it */ 6949 if ((PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 6950 sizeof (struct mode_page)) > parmlen) { 6951 *scsipkt->pkt_scbp = STATUS_CHECK; 6952 sense = sata_arq_sense(spx); 6953 sense->es_key = KEY_ILLEGAL_REQUEST; 6954 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 6955 *pagelen = parmlen; 6956 *rval = TRAN_ACCEPT; 6957 return (SATA_FAILURE); 6958 } 6959 6960 *pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page); 6961 6962 /* Current setting of Read Ahead (and Read Cache) */ 6963 if (SATA_READ_AHEAD_ENABLED(*sata_id)) 6964 dra = 0; /* 0 == not disabled */ 6965 else 6966 dra = 1; 6967 /* Current setting of Write Cache */ 6968 if (SATA_WRITE_CACHE_ENABLED(*sata_id)) 6969 wce = 1; 6970 else 6971 wce = 0; 6972 6973 if (page->dra == dra && page->wce == wce && page->rcd == dra) { 6974 /* nothing to do */ 6975 *rval = TRAN_ACCEPT; 6976 return (SATA_SUCCESS); 6977 } 6978 6979 /* 6980 * Need to flip some setting 6981 * Set-up Internal SET FEATURES command(s) 6982 */ 6983 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 6984 scmd->satacmd_addr_type = 0; 6985 scmd->satacmd_device_reg = 0; 6986 scmd->satacmd_status_reg = 0; 6987 scmd->satacmd_error_reg = 0; 6988 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 6989 if (page->dra != dra || page->rcd != dra) { 6990 if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) { 6991 /* Need to flip read ahead setting */ 6992 if (dra == 0) 6993 /* Disable read ahead / read cache */ 6994 scmd->satacmd_features_reg = 6995 SATAC_SF_DISABLE_READ_AHEAD; 6996 else 6997 /* Enable read ahead / read cache */ 6998 scmd->satacmd_features_reg = 6999 SATAC_SF_ENABLE_READ_AHEAD; 7000 7001 /* Transfer command to HBA */ 7002 if (sata_hba_start(spx, rval) != 0) 7003 /* 7004 * Pkt not accepted for execution. 7005 */ 7006 return (SATA_FAILURE); 7007 7008 *dmod = 1; 7009 7010 /* Now process return */ 7011 if (spx->txlt_sata_pkt->satapkt_reason != 7012 SATA_PKT_COMPLETED) { 7013 goto failure; /* Terminate */ 7014 } 7015 } else { 7016 *scsipkt->pkt_scbp = STATUS_CHECK; 7017 sense = sata_arq_sense(spx); 7018 sense->es_key = KEY_ILLEGAL_REQUEST; 7019 sense->es_add_code = 7020 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 7021 *pagelen = parmlen; 7022 *rval = TRAN_ACCEPT; 7023 return (SATA_FAILURE); 7024 } 7025 } 7026 7027 /* Note that the packet is not removed, so it could be re-used */ 7028 if (page->wce != wce) { 7029 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) { 7030 /* Need to flip Write Cache setting */ 7031 if (page->wce == 1) 7032 /* Enable write cache */ 7033 scmd->satacmd_features_reg = 7034 SATAC_SF_ENABLE_WRITE_CACHE; 7035 else 7036 /* Disable write cache */ 7037 scmd->satacmd_features_reg = 7038 SATAC_SF_DISABLE_WRITE_CACHE; 7039 7040 /* Transfer command to HBA */ 7041 if (sata_hba_start(spx, rval) != 0) 7042 /* 7043 * Pkt not accepted for execution. 7044 */ 7045 return (SATA_FAILURE); 7046 7047 *dmod = 1; 7048 7049 /* Now process return */ 7050 if (spx->txlt_sata_pkt->satapkt_reason != 7051 SATA_PKT_COMPLETED) { 7052 goto failure; 7053 } 7054 } else { 7055 *scsipkt->pkt_scbp = STATUS_CHECK; 7056 sense = sata_arq_sense(spx); 7057 sense->es_key = KEY_ILLEGAL_REQUEST; 7058 sense->es_add_code = 7059 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 7060 *pagelen = parmlen; 7061 *rval = TRAN_ACCEPT; 7062 return (SATA_FAILURE); 7063 } 7064 } 7065 return (SATA_SUCCESS); 7066 7067 failure: 7068 sata_xlate_errors(spx); 7069 7070 return (SATA_FAILURE); 7071 } 7072 7073 /* 7074 * Process mode select informational exceptions control page 0x1c 7075 * 7076 * The only changeable bit is dexcpt (disable exceptions). 7077 * MRIE (method of reporting informational exceptions) must be 7078 * "only on request". 7079 * This page applies to informational exceptions that report 7080 * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh 7081 * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_). 7082 * Informational exception conditions occur as the result of background scan 7083 * errors, background self-test errors, or vendor specific events within a 7084 * logical unit. An informational exception condition may occur asynchronous 7085 * to any commands. 7086 * 7087 * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 7088 * If operation resulted in changing device setup, dmod flag should be set to 7089 * one (1). If parameters were not changed, dmod flag should be set to 0. 7090 * Upon return, if operation required sending command to the device, the rval 7091 * should be set to the value returned by sata_hba_start. If operation 7092 * did not require device access, rval should be set to TRAN_ACCEPT. 7093 * The pagelen should be set to the length of the page. 7094 * 7095 * This function has to be called with a port mutex held. 7096 * 7097 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 7098 * 7099 * Cannot be called in the interrupt context. 7100 */ 7101 static int 7102 sata_mode_select_page_1c( 7103 sata_pkt_txlate_t *spx, 7104 struct mode_info_excpt_page *page, 7105 int parmlen, 7106 int *pagelen, 7107 int *rval, 7108 int *dmod) 7109 { 7110 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7111 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 7112 sata_drive_info_t *sdinfo; 7113 sata_id_t *sata_id; 7114 struct scsi_extended_sense *sense; 7115 7116 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 7117 &spx->txlt_sata_pkt->satapkt_device); 7118 sata_id = &sdinfo->satadrv_id; 7119 7120 *dmod = 0; 7121 7122 /* Verify parameters length. If too short, drop it */ 7123 if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) > parmlen) || 7124 page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) { 7125 *scsipkt->pkt_scbp = STATUS_CHECK; 7126 sense = sata_arq_sense(spx); 7127 sense->es_key = KEY_ILLEGAL_REQUEST; 7128 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 7129 *pagelen = parmlen; 7130 *rval = TRAN_ACCEPT; 7131 return (SATA_FAILURE); 7132 } 7133 7134 *pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page); 7135 7136 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 7137 *scsipkt->pkt_scbp = STATUS_CHECK; 7138 sense = sata_arq_sense(spx); 7139 sense->es_key = KEY_ILLEGAL_REQUEST; 7140 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 7141 *pagelen = parmlen; 7142 *rval = TRAN_ACCEPT; 7143 return (SATA_FAILURE); 7144 } 7145 7146 /* If already in the state requested, we are done */ 7147 if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 7148 /* nothing to do */ 7149 *rval = TRAN_ACCEPT; 7150 return (SATA_SUCCESS); 7151 } 7152 7153 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 7154 7155 /* Build SMART_ENABLE or SMART_DISABLE command */ 7156 scmd->satacmd_addr_type = 0; /* N/A */ 7157 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 7158 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 7159 scmd->satacmd_features_reg = page->dexcpt ? 7160 SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS; 7161 scmd->satacmd_device_reg = 0; /* Always device 0 */ 7162 scmd->satacmd_cmd_reg = SATAC_SMART; 7163 7164 /* Transfer command to HBA */ 7165 if (sata_hba_start(spx, rval) != 0) 7166 /* 7167 * Pkt not accepted for execution. 7168 */ 7169 return (SATA_FAILURE); 7170 7171 *dmod = 1; /* At least may have been modified */ 7172 7173 /* Now process return */ 7174 if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) 7175 return (SATA_SUCCESS); 7176 7177 /* Packet did not complete successfully */ 7178 sata_xlate_errors(spx); 7179 7180 return (SATA_FAILURE); 7181 } 7182 7183 /* 7184 * Process mode select acoustic management control page 0x30 7185 * 7186 * 7187 * This function has to be called with a port mutex held. 7188 * 7189 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 7190 * 7191 * Cannot be called in the interrupt context. 7192 */ 7193 int 7194 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct 7195 mode_acoustic_management *page, int parmlen, int *pagelen, 7196 int *rval, int *dmod) 7197 { 7198 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7199 sata_drive_info_t *sdinfo; 7200 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 7201 sata_id_t *sata_id; 7202 struct scsi_extended_sense *sense; 7203 7204 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 7205 &spx->txlt_sata_pkt->satapkt_device); 7206 sata_id = &sdinfo->satadrv_id; 7207 *dmod = 0; 7208 7209 /* If parmlen is too short or the feature is not supported, drop it */ 7210 if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 7211 sizeof (struct mode_page)) > parmlen) || 7212 (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) { 7213 *scsipkt->pkt_scbp = STATUS_CHECK; 7214 sense = sata_arq_sense(spx); 7215 sense->es_key = KEY_ILLEGAL_REQUEST; 7216 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 7217 *pagelen = parmlen; 7218 *rval = TRAN_ACCEPT; 7219 return (SATA_FAILURE); 7220 } 7221 7222 *pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 7223 sizeof (struct mode_page); 7224 7225 /* 7226 * We can enable and disable acoustice management and 7227 * set the acoustic management level. 7228 */ 7229 7230 /* 7231 * Set-up Internal SET FEATURES command(s) 7232 */ 7233 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 7234 scmd->satacmd_addr_type = 0; 7235 scmd->satacmd_device_reg = 0; 7236 scmd->satacmd_status_reg = 0; 7237 scmd->satacmd_error_reg = 0; 7238 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 7239 if (page->acoustic_manag_enable) { 7240 scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC; 7241 scmd->satacmd_sec_count_lsb = page->acoustic_manag_level; 7242 } else { /* disabling acoustic management */ 7243 scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC; 7244 } 7245 7246 /* Transfer command to HBA */ 7247 if (sata_hba_start(spx, rval) != 0) 7248 /* 7249 * Pkt not accepted for execution. 7250 */ 7251 return (SATA_FAILURE); 7252 7253 /* Now process return */ 7254 if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) { 7255 sata_xlate_errors(spx); 7256 return (SATA_FAILURE); 7257 } 7258 7259 *dmod = 1; 7260 7261 return (SATA_SUCCESS); 7262 } 7263 7264 /* 7265 * Process mode select power condition page 0x1a 7266 * 7267 * This function has to be called with a port mutex held. 7268 * 7269 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 7270 * 7271 * Cannot be called in the interrupt context. 7272 */ 7273 int 7274 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct 7275 mode_info_power_cond *page, int parmlen, int *pagelen, 7276 int *rval, int *dmod) 7277 { 7278 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7279 sata_drive_info_t *sdinfo; 7280 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 7281 sata_id_t *sata_id; 7282 struct scsi_extended_sense *sense; 7283 uint8_t ata_count; 7284 int i, len; 7285 7286 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 7287 &spx->txlt_sata_pkt->satapkt_device); 7288 sata_id = &sdinfo->satadrv_id; 7289 *dmod = 0; 7290 7291 len = sizeof (struct mode_info_power_cond); 7292 len += sizeof (struct mode_page); 7293 7294 /* If parmlen is too short or the feature is not supported, drop it */ 7295 if ((len < parmlen) || (page->idle == 1) || 7296 (!(sata_id->ai_cap && SATA_STANDBYTIMER) && page->standby == 1)) { 7297 *scsipkt->pkt_scbp = STATUS_CHECK; 7298 sense = sata_arq_sense(spx); 7299 sense->es_key = KEY_ILLEGAL_REQUEST; 7300 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 7301 *pagelen = parmlen; 7302 *rval = TRAN_ACCEPT; 7303 return (SATA_FAILURE); 7304 } 7305 7306 *pagelen = len; 7307 7308 /* 7309 * Set-up Internal STANDBY command(s) 7310 */ 7311 if (page->standby == 0) 7312 goto out; 7313 7314 ata_count = sata_get_standby_timer(page->standby_cond_timer); 7315 7316 scmd->satacmd_addr_type = 0; 7317 scmd->satacmd_sec_count_lsb = ata_count; 7318 scmd->satacmd_lba_low_lsb = 0; 7319 scmd->satacmd_lba_mid_lsb = 0; 7320 scmd->satacmd_lba_high_lsb = 0; 7321 scmd->satacmd_features_reg = 0; 7322 scmd->satacmd_device_reg = 0; 7323 scmd->satacmd_status_reg = 0; 7324 scmd->satacmd_cmd_reg = SATAC_STANDBY; 7325 scmd->satacmd_flags.sata_special_regs = B_TRUE; 7326 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 7327 7328 /* Transfer command to HBA */ 7329 if (sata_hba_start(spx, rval) != 0) { 7330 return (SATA_FAILURE); 7331 } else { 7332 if ((scmd->satacmd_error_reg != 0) || 7333 (spx->txlt_sata_pkt->satapkt_reason != 7334 SATA_PKT_COMPLETED)) { 7335 sata_xlate_errors(spx); 7336 return (SATA_FAILURE); 7337 } 7338 } 7339 7340 for (i = 0; i < 4; i++) { 7341 sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i]; 7342 } 7343 out: 7344 *dmod = 1; 7345 return (SATA_SUCCESS); 7346 } 7347 7348 /* 7349 * sata_build_lsense_page0() is used to create the 7350 * SCSI LOG SENSE page 0 (supported log pages) 7351 * 7352 * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e 7353 * (supported log pages, self-test results, informational exceptions 7354 * Sun vendor specific ATA SMART data, and start stop cycle counter). 7355 * 7356 * Takes a sata_drive_info t * and the address of a buffer 7357 * in which to create the page information. 7358 * 7359 * Returns the number of bytes valid in the buffer. 7360 */ 7361 static int 7362 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf) 7363 { 7364 struct log_parameter *lpp = (struct log_parameter *)buf; 7365 uint8_t *page_ptr = (uint8_t *)lpp->param_values; 7366 int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */ 7367 sata_id_t *sata_id = &sdinfo->satadrv_id; 7368 7369 lpp->param_code[0] = 0; 7370 lpp->param_code[1] = 0; 7371 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 7372 *page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES; 7373 7374 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 7375 if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) { 7376 *page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS; 7377 ++num_pages_supported; 7378 } 7379 *page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS; 7380 ++num_pages_supported; 7381 *page_ptr++ = PAGE_CODE_SMART_READ_DATA; 7382 ++num_pages_supported; 7383 *page_ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER; 7384 ++num_pages_supported; 7385 } 7386 7387 lpp->param_len = num_pages_supported; 7388 7389 return ((&lpp->param_values[0] - (uint8_t *)lpp) + 7390 num_pages_supported); 7391 } 7392 7393 /* 7394 * sata_build_lsense_page_10() is used to create the 7395 * SCSI LOG SENSE page 0x10 (self-test results) 7396 * 7397 * Takes a sata_drive_info t * and the address of a buffer 7398 * in which to create the page information as well as a sata_hba_inst_t *. 7399 * 7400 * Returns the number of bytes valid in the buffer. 7401 * 7402 * Note: Self test and SMART data is accessible in device log pages. 7403 * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors 7404 * of data can be transferred by a single command), or by the General Purpose 7405 * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors 7406 * - approximately 33MB - can be transferred by a single command. 7407 * The SCT Command response (either error or command) is the same for both 7408 * the SMART and GPL methods of issuing commands. 7409 * This function uses READ LOG EXT command when drive supports LBA48, and 7410 * SMART READ command otherwise. 7411 * 7412 * Since above commands are executed in a synchronous mode, this function 7413 * should not be called in an interrupt context. 7414 */ 7415 static int 7416 sata_build_lsense_page_10( 7417 sata_drive_info_t *sdinfo, 7418 uint8_t *buf, 7419 sata_hba_inst_t *sata_hba_inst) 7420 { 7421 struct log_parameter *lpp = (struct log_parameter *)buf; 7422 int rval; 7423 7424 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 7425 struct smart_ext_selftest_log *ext_selftest_log; 7426 7427 ext_selftest_log = kmem_zalloc( 7428 sizeof (struct smart_ext_selftest_log), KM_SLEEP); 7429 7430 rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo, 7431 ext_selftest_log, 0); 7432 if (rval == 0) { 7433 int index, start_index; 7434 struct smart_ext_selftest_log_entry *entry; 7435 static const struct smart_ext_selftest_log_entry empty = 7436 {0}; 7437 uint16_t block_num; 7438 int count; 7439 boolean_t only_one_block = B_FALSE; 7440 7441 index = ext_selftest_log-> 7442 smart_ext_selftest_log_index[0]; 7443 index |= ext_selftest_log-> 7444 smart_ext_selftest_log_index[1] << 8; 7445 if (index == 0) 7446 goto out; 7447 7448 --index; /* Correct for 0 origin */ 7449 start_index = index; /* remember where we started */ 7450 block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 7451 if (block_num != 0) { 7452 rval = sata_ext_smart_selftest_read_log( 7453 sata_hba_inst, sdinfo, ext_selftest_log, 7454 block_num); 7455 if (rval != 0) 7456 goto out; 7457 } 7458 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 7459 entry = 7460 &ext_selftest_log-> 7461 smart_ext_selftest_log_entries[index]; 7462 7463 for (count = 1; 7464 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 7465 ++count) { 7466 uint8_t status; 7467 uint8_t code; 7468 uint8_t sense_key; 7469 uint8_t add_sense_code; 7470 uint8_t add_sense_code_qual; 7471 7472 /* If this is an unused entry, we are done */ 7473 if (bcmp(entry, &empty, sizeof (empty)) == 0) { 7474 /* Broken firmware on some disks */ 7475 if (index + 1 == 7476 ENTRIES_PER_EXT_SELFTEST_LOG_BLK) { 7477 --entry; 7478 --index; 7479 if (bcmp(entry, &empty, 7480 sizeof (empty)) == 0) 7481 goto out; 7482 } else 7483 goto out; 7484 } 7485 7486 if (only_one_block && 7487 start_index == index) 7488 goto out; 7489 7490 lpp->param_code[0] = 0; 7491 lpp->param_code[1] = count; 7492 lpp->param_ctrl_flags = 7493 LOG_CTRL_LP | LOG_CTRL_LBIN; 7494 lpp->param_len = 7495 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 7496 7497 status = entry->smart_ext_selftest_log_status; 7498 status >>= 4; 7499 switch (status) { 7500 case 0: 7501 default: 7502 sense_key = KEY_NO_SENSE; 7503 add_sense_code = 7504 SD_SCSI_ASC_NO_ADD_SENSE; 7505 add_sense_code_qual = 0; 7506 break; 7507 case 1: 7508 sense_key = KEY_ABORTED_COMMAND; 7509 add_sense_code = 7510 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7511 add_sense_code_qual = SCSI_COMPONENT_81; 7512 break; 7513 case 2: 7514 sense_key = KEY_ABORTED_COMMAND; 7515 add_sense_code = 7516 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7517 add_sense_code_qual = SCSI_COMPONENT_82; 7518 break; 7519 case 3: 7520 sense_key = KEY_ABORTED_COMMAND; 7521 add_sense_code = 7522 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7523 add_sense_code_qual = SCSI_COMPONENT_83; 7524 break; 7525 case 4: 7526 sense_key = KEY_HARDWARE_ERROR; 7527 add_sense_code = 7528 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7529 add_sense_code_qual = SCSI_COMPONENT_84; 7530 break; 7531 case 5: 7532 sense_key = KEY_HARDWARE_ERROR; 7533 add_sense_code = 7534 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7535 add_sense_code_qual = SCSI_COMPONENT_85; 7536 break; 7537 case 6: 7538 sense_key = KEY_HARDWARE_ERROR; 7539 add_sense_code = 7540 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7541 add_sense_code_qual = SCSI_COMPONENT_86; 7542 break; 7543 case 7: 7544 sense_key = KEY_MEDIUM_ERROR; 7545 add_sense_code = 7546 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7547 add_sense_code_qual = SCSI_COMPONENT_87; 7548 break; 7549 case 8: 7550 sense_key = KEY_HARDWARE_ERROR; 7551 add_sense_code = 7552 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7553 add_sense_code_qual = SCSI_COMPONENT_88; 7554 break; 7555 } 7556 code = 0; /* unspecified */ 7557 status |= (code << 4); 7558 lpp->param_values[0] = status; 7559 lpp->param_values[1] = 0; /* unspecified */ 7560 lpp->param_values[2] = entry-> 7561 smart_ext_selftest_log_timestamp[1]; 7562 lpp->param_values[3] = entry-> 7563 smart_ext_selftest_log_timestamp[0]; 7564 if (status != 0) { 7565 lpp->param_values[4] = 0; 7566 lpp->param_values[5] = 0; 7567 lpp->param_values[6] = entry-> 7568 smart_ext_selftest_log_failing_lba 7569 [5]; 7570 lpp->param_values[7] = entry-> 7571 smart_ext_selftest_log_failing_lba 7572 [4]; 7573 lpp->param_values[8] = entry-> 7574 smart_ext_selftest_log_failing_lba 7575 [3]; 7576 lpp->param_values[9] = entry-> 7577 smart_ext_selftest_log_failing_lba 7578 [2]; 7579 lpp->param_values[10] = entry-> 7580 smart_ext_selftest_log_failing_lba 7581 [1]; 7582 lpp->param_values[11] = entry-> 7583 smart_ext_selftest_log_failing_lba 7584 [0]; 7585 } else { /* No bad block address */ 7586 lpp->param_values[4] = 0xff; 7587 lpp->param_values[5] = 0xff; 7588 lpp->param_values[6] = 0xff; 7589 lpp->param_values[7] = 0xff; 7590 lpp->param_values[8] = 0xff; 7591 lpp->param_values[9] = 0xff; 7592 lpp->param_values[10] = 0xff; 7593 lpp->param_values[11] = 0xff; 7594 } 7595 7596 lpp->param_values[12] = sense_key; 7597 lpp->param_values[13] = add_sense_code; 7598 lpp->param_values[14] = add_sense_code_qual; 7599 lpp->param_values[15] = 0; /* undefined */ 7600 7601 lpp = (struct log_parameter *) 7602 (((uint8_t *)lpp) + 7603 SCSI_LOG_PARAM_HDR_LEN + 7604 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 7605 7606 --index; /* Back up to previous entry */ 7607 if (index < 0) { 7608 if (block_num > 0) { 7609 --block_num; 7610 } else { 7611 struct read_log_ext_directory 7612 logdir; 7613 7614 rval = 7615 sata_read_log_ext_directory( 7616 sata_hba_inst, sdinfo, 7617 &logdir); 7618 if (rval == -1) 7619 goto out; 7620 if ((logdir.read_log_ext_vers 7621 [0] == 0) && 7622 (logdir.read_log_ext_vers 7623 [1] == 0)) 7624 goto out; 7625 block_num = 7626 logdir.read_log_ext_nblks 7627 [EXT_SMART_SELFTEST_LOG_PAGE 7628 - 1][0]; 7629 block_num |= logdir. 7630 read_log_ext_nblks 7631 [EXT_SMART_SELFTEST_LOG_PAGE 7632 - 1][1] << 8; 7633 --block_num; 7634 only_one_block = 7635 (block_num == 0); 7636 } 7637 rval = sata_ext_smart_selftest_read_log( 7638 sata_hba_inst, sdinfo, 7639 ext_selftest_log, block_num); 7640 if (rval != 0) 7641 goto out; 7642 7643 index = 7644 ENTRIES_PER_EXT_SELFTEST_LOG_BLK - 7645 1; 7646 } 7647 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 7648 entry = &ext_selftest_log-> 7649 smart_ext_selftest_log_entries[index]; 7650 } 7651 } 7652 out: 7653 kmem_free(ext_selftest_log, 7654 sizeof (struct smart_ext_selftest_log)); 7655 } else { 7656 struct smart_selftest_log *selftest_log; 7657 7658 selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log), 7659 KM_SLEEP); 7660 7661 rval = sata_smart_selftest_log(sata_hba_inst, sdinfo, 7662 selftest_log); 7663 7664 if (rval == 0) { 7665 int index; 7666 int count; 7667 struct smart_selftest_log_entry *entry; 7668 static const struct smart_selftest_log_entry empty = 7669 { 0 }; 7670 7671 index = selftest_log->smart_selftest_log_index; 7672 if (index == 0) 7673 goto done; 7674 --index; /* Correct for 0 origin */ 7675 entry = &selftest_log-> 7676 smart_selftest_log_entries[index]; 7677 for (count = 1; 7678 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 7679 ++count) { 7680 uint8_t status; 7681 uint8_t code; 7682 uint8_t sense_key; 7683 uint8_t add_sense_code; 7684 uint8_t add_sense_code_qual; 7685 7686 if (bcmp(entry, &empty, sizeof (empty)) == 0) 7687 goto done; 7688 7689 lpp->param_code[0] = 0; 7690 lpp->param_code[1] = count; 7691 lpp->param_ctrl_flags = 7692 LOG_CTRL_LP | LOG_CTRL_LBIN; 7693 lpp->param_len = 7694 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 7695 7696 status = entry->smart_selftest_log_status; 7697 status >>= 4; 7698 switch (status) { 7699 case 0: 7700 default: 7701 sense_key = KEY_NO_SENSE; 7702 add_sense_code = 7703 SD_SCSI_ASC_NO_ADD_SENSE; 7704 break; 7705 case 1: 7706 sense_key = KEY_ABORTED_COMMAND; 7707 add_sense_code = 7708 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7709 add_sense_code_qual = SCSI_COMPONENT_81; 7710 break; 7711 case 2: 7712 sense_key = KEY_ABORTED_COMMAND; 7713 add_sense_code = 7714 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7715 add_sense_code_qual = SCSI_COMPONENT_82; 7716 break; 7717 case 3: 7718 sense_key = KEY_ABORTED_COMMAND; 7719 add_sense_code = 7720 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7721 add_sense_code_qual = SCSI_COMPONENT_83; 7722 break; 7723 case 4: 7724 sense_key = KEY_HARDWARE_ERROR; 7725 add_sense_code = 7726 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7727 add_sense_code_qual = SCSI_COMPONENT_84; 7728 break; 7729 case 5: 7730 sense_key = KEY_HARDWARE_ERROR; 7731 add_sense_code = 7732 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7733 add_sense_code_qual = SCSI_COMPONENT_85; 7734 break; 7735 case 6: 7736 sense_key = KEY_HARDWARE_ERROR; 7737 add_sense_code = 7738 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7739 add_sense_code_qual = SCSI_COMPONENT_86; 7740 break; 7741 case 7: 7742 sense_key = KEY_MEDIUM_ERROR; 7743 add_sense_code = 7744 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7745 add_sense_code_qual = SCSI_COMPONENT_87; 7746 break; 7747 case 8: 7748 sense_key = KEY_HARDWARE_ERROR; 7749 add_sense_code = 7750 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7751 add_sense_code_qual = SCSI_COMPONENT_88; 7752 break; 7753 } 7754 code = 0; /* unspecified */ 7755 status |= (code << 4); 7756 lpp->param_values[0] = status; 7757 lpp->param_values[1] = 0; /* unspecified */ 7758 lpp->param_values[2] = entry-> 7759 smart_selftest_log_timestamp[1]; 7760 lpp->param_values[3] = entry-> 7761 smart_selftest_log_timestamp[0]; 7762 if (status != 0) { 7763 lpp->param_values[4] = 0; 7764 lpp->param_values[5] = 0; 7765 lpp->param_values[6] = 0; 7766 lpp->param_values[7] = 0; 7767 lpp->param_values[8] = entry-> 7768 smart_selftest_log_failing_lba[3]; 7769 lpp->param_values[9] = entry-> 7770 smart_selftest_log_failing_lba[2]; 7771 lpp->param_values[10] = entry-> 7772 smart_selftest_log_failing_lba[1]; 7773 lpp->param_values[11] = entry-> 7774 smart_selftest_log_failing_lba[0]; 7775 } else { /* No block address */ 7776 lpp->param_values[4] = 0xff; 7777 lpp->param_values[5] = 0xff; 7778 lpp->param_values[6] = 0xff; 7779 lpp->param_values[7] = 0xff; 7780 lpp->param_values[8] = 0xff; 7781 lpp->param_values[9] = 0xff; 7782 lpp->param_values[10] = 0xff; 7783 lpp->param_values[11] = 0xff; 7784 } 7785 lpp->param_values[12] = sense_key; 7786 lpp->param_values[13] = add_sense_code; 7787 lpp->param_values[14] = add_sense_code_qual; 7788 lpp->param_values[15] = 0; /* undefined */ 7789 7790 lpp = (struct log_parameter *) 7791 (((uint8_t *)lpp) + 7792 SCSI_LOG_PARAM_HDR_LEN + 7793 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 7794 --index; /* back up to previous entry */ 7795 if (index < 0) { 7796 index = 7797 NUM_SMART_SELFTEST_LOG_ENTRIES - 1; 7798 } 7799 entry = &selftest_log-> 7800 smart_selftest_log_entries[index]; 7801 } 7802 } 7803 done: 7804 kmem_free(selftest_log, sizeof (struct smart_selftest_log)); 7805 } 7806 7807 return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) * 7808 SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS); 7809 } 7810 7811 /* 7812 * sata_build_lsense_page_2f() is used to create the 7813 * SCSI LOG SENSE page 0x2f (informational exceptions) 7814 * 7815 * Takes a sata_drive_info t * and the address of a buffer 7816 * in which to create the page information as well as a sata_hba_inst_t *. 7817 * 7818 * Returns the number of bytes valid in the buffer. 7819 * 7820 * Because it invokes function(s) that send synchronously executed command 7821 * to the HBA, it cannot be called in the interrupt context. 7822 */ 7823 static int 7824 sata_build_lsense_page_2f( 7825 sata_drive_info_t *sdinfo, 7826 uint8_t *buf, 7827 sata_hba_inst_t *sata_hba_inst) 7828 { 7829 struct log_parameter *lpp = (struct log_parameter *)buf; 7830 int rval; 7831 uint8_t *smart_data; 7832 uint8_t temp; 7833 sata_id_t *sata_id; 7834 #define SMART_NO_TEMP 0xff 7835 7836 lpp->param_code[0] = 0; 7837 lpp->param_code[1] = 0; 7838 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 7839 7840 /* Now get the SMART status w.r.t. threshold exceeded */ 7841 rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo); 7842 switch (rval) { 7843 case 1: 7844 lpp->param_values[0] = SCSI_PREDICTED_FAILURE; 7845 lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE; 7846 break; 7847 case 0: 7848 case -1: /* failed to get data */ 7849 lpp->param_values[0] = 0; /* No failure predicted */ 7850 lpp->param_values[1] = 0; 7851 break; 7852 #if defined(SATA_DEBUG) 7853 default: 7854 cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value"); 7855 /* NOTREACHED */ 7856 #endif 7857 } 7858 7859 sata_id = &sdinfo->satadrv_id; 7860 if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP)) 7861 temp = SMART_NO_TEMP; 7862 else { 7863 /* Now get the temperature */ 7864 smart_data = kmem_zalloc(512, KM_SLEEP); 7865 rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data, 7866 SCT_STATUS_LOG_PAGE, 1); 7867 if (rval == -1) 7868 temp = SMART_NO_TEMP; 7869 else { 7870 temp = smart_data[200]; 7871 if (temp & 0x80) { 7872 if (temp & 0x7f) 7873 temp = 0; 7874 else 7875 temp = SMART_NO_TEMP; 7876 } 7877 } 7878 kmem_free(smart_data, 512); 7879 } 7880 7881 lpp->param_values[2] = temp; /* most recent temperature */ 7882 lpp->param_values[3] = 0; /* required vendor specific byte */ 7883 7884 lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN; 7885 7886 7887 return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN); 7888 } 7889 7890 /* 7891 * sata_build_lsense_page_30() is used to create the 7892 * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data). 7893 * 7894 * Takes a sata_drive_info t * and the address of a buffer 7895 * in which to create the page information as well as a sata_hba_inst_t *. 7896 * 7897 * Returns the number of bytes valid in the buffer. 7898 */ 7899 static int 7900 sata_build_lsense_page_30( 7901 sata_drive_info_t *sdinfo, 7902 uint8_t *buf, 7903 sata_hba_inst_t *sata_hba_inst) 7904 { 7905 struct smart_data *smart_data = (struct smart_data *)buf; 7906 int rval; 7907 7908 /* Now do the SMART READ DATA */ 7909 rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data); 7910 if (rval == -1) 7911 return (0); 7912 7913 return (sizeof (struct smart_data)); 7914 } 7915 7916 /* 7917 * sata_build_lsense_page_0e() is used to create the 7918 * SCSI LOG SENSE page 0e (start-stop cycle counter page) 7919 * 7920 * Date of Manufacture (0x0001) 7921 * YEAR = "0000" 7922 * WEEK = "00" 7923 * Accounting Date (0x0002) 7924 * 6 ASCII space character(20h) 7925 * Specified cycle count over device lifetime 7926 * VALUE - THRESH - the delta between max and min; 7927 * Accumulated start-stop cycles 7928 * VALUE - WORST - the accumulated cycles; 7929 * 7930 * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute 7931 * 7932 * Takes a sata_drive_info t * and the address of a buffer 7933 * in which to create the page information as well as a sata_hba_inst_t *. 7934 * 7935 * Returns the number of bytes valid in the buffer. 7936 */ 7937 static int 7938 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf, 7939 sata_pkt_txlate_t *spx) 7940 { 7941 struct start_stop_cycle_counter_log *log_page; 7942 int i, rval, index; 7943 uint8_t smart_data[512], id, value, worst, thresh; 7944 uint32_t max_count, cycles; 7945 7946 /* Now do the SMART READ DATA */ 7947 rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo, 7948 (struct smart_data *)smart_data); 7949 if (rval == -1) 7950 return (0); 7951 for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) { 7952 index = (i * 12) + 2; 7953 id = smart_data[index]; 7954 if (id != SMART_START_STOP_COUNT_ID) 7955 continue; 7956 else { 7957 thresh = smart_data[index + 2]; 7958 value = smart_data[index + 3]; 7959 worst = smart_data[index + 4]; 7960 break; 7961 } 7962 } 7963 if (id != SMART_START_STOP_COUNT_ID) 7964 return (0); 7965 max_count = value - thresh; 7966 cycles = value - worst; 7967 7968 log_page = (struct start_stop_cycle_counter_log *)buf; 7969 bzero(log_page, sizeof (struct start_stop_cycle_counter_log)); 7970 log_page->code = 0x0e; 7971 log_page->page_len_low = 0x24; 7972 7973 log_page->manufactor_date_low = 0x1; 7974 log_page->param_1.fmt_link = 0x1; /* 01b */ 7975 log_page->param_len_1 = 0x06; 7976 for (i = 0; i < 4; i++) { 7977 log_page->year_manu[i] = 0x30; 7978 if (i < 2) 7979 log_page->week_manu[i] = 0x30; 7980 } 7981 7982 log_page->account_date_low = 0x02; 7983 log_page->param_2.fmt_link = 0x01; /* 01b */ 7984 log_page->param_len_2 = 0x06; 7985 for (i = 0; i < 4; i++) { 7986 log_page->year_account[i] = 0x20; 7987 if (i < 2) 7988 log_page->week_account[i] = 0x20; 7989 } 7990 7991 log_page->lifetime_code_low = 0x03; 7992 log_page->param_3.fmt_link = 0x03; /* 11b */ 7993 log_page->param_len_3 = 0x04; 7994 /* VALUE - THRESH - the delta between max and min */ 7995 log_page->cycle_code_low = 0x04; 7996 log_page->param_4.fmt_link = 0x03; /* 11b */ 7997 log_page->param_len_4 = 0x04; 7998 /* WORST - THRESH - the distance from 'now' to min */ 7999 8000 for (i = 0; i < 4; i++) { 8001 log_page->cycle_lifetime[i] = 8002 (max_count >> (8 * (3 - i))) & 0xff; 8003 log_page->cycle_accumulated[i] = 8004 (cycles >> (8 * (3 - i))) & 0xff; 8005 } 8006 8007 return (sizeof (struct start_stop_cycle_counter_log)); 8008 } 8009 8010 /* 8011 * This function was used for build a ATA read verify sector command 8012 */ 8013 static void 8014 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba) 8015 { 8016 scmd->satacmd_cmd_reg = SATAC_RDVER; 8017 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 8018 scmd->satacmd_flags.sata_special_regs = B_TRUE; 8019 8020 scmd->satacmd_sec_count_lsb = sec & 0xff; 8021 scmd->satacmd_lba_low_lsb = lba & 0xff; 8022 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 8023 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 8024 scmd->satacmd_device_reg = (SATA_ADH_LBA | (lba >> 24) & 0xf); 8025 scmd->satacmd_features_reg = 0; 8026 scmd->satacmd_status_reg = 0; 8027 scmd->satacmd_error_reg = 0; 8028 } 8029 8030 /* 8031 * This function was used for building an ATA 8032 * command, and only command register need to 8033 * be defined, other register will be zero or na. 8034 */ 8035 static void 8036 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd) 8037 { 8038 scmd->satacmd_addr_type = 0; 8039 scmd->satacmd_cmd_reg = cmd; 8040 scmd->satacmd_device_reg = 0; 8041 scmd->satacmd_sec_count_lsb = 0; 8042 scmd->satacmd_lba_low_lsb = 0; 8043 scmd->satacmd_lba_mid_lsb = 0; 8044 scmd->satacmd_lba_high_lsb = 0; 8045 scmd->satacmd_features_reg = 0; 8046 scmd->satacmd_status_reg = 0; 8047 scmd->satacmd_error_reg = 0; 8048 scmd->satacmd_flags.sata_special_regs = B_TRUE; 8049 } 8050 8051 /* 8052 * This function was used for changing the standby 8053 * timer format from SCSI to ATA. 8054 */ 8055 static uint8_t 8056 sata_get_standby_timer(uint8_t *timer) 8057 { 8058 uint32_t i = 0, count = 0; 8059 uint8_t ata_count; 8060 8061 for (i = 0; i < 4; i++) { 8062 count = count << 8 | timer[i]; 8063 } 8064 8065 if (count == 0) 8066 return (0); 8067 8068 if (count >= 1 && count <= 12000) 8069 ata_count = (count -1) / 50 + 1; 8070 else if (count > 12000 && count <= 12600) 8071 ata_count = 0xfc; 8072 else if (count > 12601 && count <= 12750) 8073 ata_count = 0xff; 8074 else if (count > 12750 && count <= 17999) 8075 ata_count = 0xf1; 8076 else if (count > 18000 && count <= 198000) 8077 ata_count = count / 18000 + 240; 8078 else 8079 ata_count = 0xfd; 8080 return (ata_count); 8081 } 8082 8083 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */ 8084 8085 /* 8086 * Start command for ATAPI device. 8087 * This function processes scsi_pkt requests. 8088 * Now CD/DVD, tape and ATAPI disk devices are supported. 8089 * Most commands are packet without any translation into Packet Command. 8090 * Some may be trapped and executed as SATA commands (not clear which one). 8091 * 8092 * Returns TRAN_ACCEPT if command is accepted for execution (or completed 8093 * execution). 8094 * Returns other TRAN_XXXX codes if command is not accepted or completed 8095 * (see return values for sata_hba_start()). 8096 * 8097 * Note: 8098 * Inquiry cdb format differs between transport version 2 and 3. 8099 * However, the transport version 3 devices that were checked did not adhere 8100 * to the specification (ignored MSB of the allocation length). Therefore, 8101 * the transport version is not checked, but Inquiry allocation length is 8102 * truncated to 255 bytes if the original allocation length set-up by the 8103 * target driver is greater than 255 bytes. 8104 */ 8105 static int 8106 sata_txlt_atapi(sata_pkt_txlate_t *spx) 8107 { 8108 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 8109 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 8110 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 8111 sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx); 8112 sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba, 8113 &spx->txlt_sata_pkt->satapkt_device); 8114 int cport = SATA_TXLT_CPORT(spx); 8115 int cdblen; 8116 int rval, reason; 8117 int synch; 8118 union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp; 8119 8120 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 8121 8122 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 8123 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 8124 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 8125 return (rval); 8126 } 8127 8128 /* 8129 * ATAPI device executes some ATA commands in addition to those 8130 * commands sent via PACKET command. These ATA commands may be 8131 * executed by the regular SATA translation functions. None needs 8132 * to be captured now. 8133 * 8134 * Commands sent via PACKET command include: 8135 * MMC command set for ATAPI CD/DVD device 8136 * SSC command set for ATAPI TAPE device 8137 * SBC command set for ATAPI disk device 8138 * 8139 */ 8140 8141 /* Check the size of cdb */ 8142 cdblen = scsi_cdb_size[GETGROUP(cdbp)]; 8143 if (cdblen > sdinfo->satadrv_atapi_cdb_len) { 8144 sata_log(NULL, CE_WARN, 8145 "sata: invalid ATAPI cdb length %d", 8146 scsipkt->pkt_cdblen); 8147 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 8148 return (TRAN_BADPKT); 8149 } 8150 8151 SATAATAPITRACE(spx, cdblen); 8152 8153 /* 8154 * For non-read/write commands we need to 8155 * map buffer 8156 */ 8157 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 8158 case SCMD_READ: 8159 case SCMD_READ_G1: 8160 case SCMD_READ_G5: 8161 case SCMD_READ_G4: 8162 case SCMD_WRITE: 8163 case SCMD_WRITE_G1: 8164 case SCMD_WRITE_G5: 8165 case SCMD_WRITE_G4: 8166 break; 8167 default: 8168 if (bp != NULL) { 8169 if (bp->b_flags & (B_PHYS | B_PAGEIO)) 8170 bp_mapin(bp); 8171 } 8172 break; 8173 } 8174 /* 8175 * scmd->satacmd_flags.sata_data_direction default - 8176 * SATA_DIR_NODATA_XFER - is set by 8177 * sata_txlt_generic_pkt_info(). 8178 */ 8179 if (scmd->satacmd_bp) { 8180 if (scmd->satacmd_bp->b_flags & B_READ) { 8181 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 8182 } else { 8183 scmd->satacmd_flags.sata_data_direction = 8184 SATA_DIR_WRITE; 8185 } 8186 } 8187 8188 /* 8189 * Set up ATAPI packet command. 8190 */ 8191 8192 sata_atapi_packet_cmd_setup(scmd, sdinfo); 8193 8194 /* Copy cdb into sata_cmd */ 8195 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 8196 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 8197 bcopy(cdbp, scmd->satacmd_acdb, cdblen); 8198 8199 /* See note in the command header */ 8200 if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) { 8201 if (scmd->satacmd_acdb[3] != 0) 8202 scmd->satacmd_acdb[4] = 255; 8203 } 8204 8205 #ifdef SATA_DEBUG 8206 if (sata_debug_flags & SATA_DBG_ATAPI) { 8207 uint8_t *p = scmd->satacmd_acdb; 8208 char buf[3 * SATA_ATAPI_MAX_CDB_LEN]; 8209 8210 (void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN, 8211 "%02x %02x %02x %02x %02x %02x %02x %02x " 8212 "%2x %02x %02x %02x %02x %02x %02x %02x", 8213 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 8214 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 8215 buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0'; 8216 cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf); 8217 } 8218 #endif 8219 8220 /* 8221 * Preset request sense data to NO SENSE. 8222 * If there is no way to get error information via Request Sense, 8223 * the packet request sense data would not have to be modified by HBA, 8224 * but it could be returned as is. 8225 */ 8226 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN); 8227 sata_fixed_sense_data_preset( 8228 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 8229 8230 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 8231 /* Need callback function */ 8232 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion; 8233 synch = FALSE; 8234 } else 8235 synch = TRUE; 8236 8237 /* Transfer command to HBA */ 8238 if (sata_hba_start(spx, &rval) != 0) { 8239 /* Pkt not accepted for execution */ 8240 mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport)); 8241 return (rval); 8242 } 8243 mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport)); 8244 /* 8245 * If execution is non-synchronous, 8246 * a callback function will handle potential errors, translate 8247 * the response and will do a callback to a target driver. 8248 * If it was synchronous, use the same framework callback to check 8249 * an execution status. 8250 */ 8251 if (synch) { 8252 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 8253 "synchronous execution status %x\n", 8254 spx->txlt_sata_pkt->satapkt_reason); 8255 sata_txlt_atapi_completion(spx->txlt_sata_pkt); 8256 } 8257 return (TRAN_ACCEPT); 8258 } 8259 8260 8261 /* 8262 * ATAPI Packet command completion. 8263 * 8264 * Failure of the command passed via Packet command are considered device 8265 * error. SATA HBA driver would have to retrieve error data (via Request 8266 * Sense command delivered via error retrieval sata packet) and copy it 8267 * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data. 8268 */ 8269 static void 8270 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt) 8271 { 8272 sata_pkt_txlate_t *spx = 8273 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 8274 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 8275 struct scsi_extended_sense *sense; 8276 struct buf *bp; 8277 int rval; 8278 8279 #ifdef SATA_DEBUG 8280 uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense; 8281 #endif 8282 8283 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 8284 STATE_SENT_CMD | STATE_GOT_STATUS; 8285 8286 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 8287 /* Normal completion */ 8288 if (sata_pkt->satapkt_cmd.satacmd_bp != NULL) 8289 scsipkt->pkt_state |= STATE_XFERRED_DATA; 8290 scsipkt->pkt_reason = CMD_CMPLT; 8291 *scsipkt->pkt_scbp = STATUS_GOOD; 8292 if (spx->txlt_tmp_buf != NULL) { 8293 /* Temporary buffer was used */ 8294 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 8295 if (bp->b_flags & B_READ) { 8296 rval = ddi_dma_sync( 8297 spx->txlt_buf_dma_handle, 0, 0, 8298 DDI_DMA_SYNC_FORCPU); 8299 ASSERT(rval == DDI_SUCCESS); 8300 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 8301 bp->b_bcount); 8302 } 8303 } 8304 } else { 8305 /* 8306 * Something went wrong - analyze return 8307 */ 8308 *scsipkt->pkt_scbp = STATUS_CHECK; 8309 sense = sata_arq_sense(spx); 8310 8311 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 8312 /* 8313 * pkt_reason should be CMD_CMPLT for DEVICE ERROR. 8314 * Under this condition ERR bit is set for ATA command, 8315 * and CHK bit set for ATAPI command. 8316 * 8317 * Please check st_intr & sdintr about how pkt_reason 8318 * is used. 8319 */ 8320 scsipkt->pkt_reason = CMD_CMPLT; 8321 8322 /* 8323 * We may not have ARQ data if there was a double 8324 * error. But sense data in sata packet was pre-set 8325 * with NO SENSE so it is valid even if HBA could 8326 * not retrieve a real sense data. 8327 * Just copy this sense data into scsi pkt sense area. 8328 */ 8329 bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense, 8330 SATA_ATAPI_MIN_RQSENSE_LEN); 8331 #ifdef SATA_DEBUG 8332 if (sata_debug_flags & SATA_DBG_SCSI_IF) { 8333 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 8334 "sata_txlt_atapi_completion: %02x\n" 8335 "RQSENSE: %02x %02x %02x %02x %02x %02x " 8336 " %02x %02x %02x %02x %02x %02x " 8337 " %02x %02x %02x %02x %02x %02x\n", 8338 scsipkt->pkt_reason, 8339 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 8340 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 8341 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 8342 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 8343 rqsp[16], rqsp[17]); 8344 } 8345 #endif 8346 } else { 8347 switch (sata_pkt->satapkt_reason) { 8348 case SATA_PKT_PORT_ERROR: 8349 /* 8350 * We have no device data. 8351 */ 8352 scsipkt->pkt_reason = CMD_INCOMPLETE; 8353 scsipkt->pkt_state &= ~(STATE_GOT_BUS | 8354 STATE_GOT_TARGET | STATE_SENT_CMD | 8355 STATE_GOT_STATUS); 8356 sense->es_key = KEY_HARDWARE_ERROR; 8357 break; 8358 8359 case SATA_PKT_TIMEOUT: 8360 scsipkt->pkt_reason = CMD_TIMEOUT; 8361 scsipkt->pkt_statistics |= 8362 STAT_TIMEOUT | STAT_DEV_RESET; 8363 /* 8364 * Need to check if HARDWARE_ERROR/ 8365 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more 8366 * appropriate. 8367 */ 8368 break; 8369 8370 case SATA_PKT_ABORTED: 8371 scsipkt->pkt_reason = CMD_ABORTED; 8372 scsipkt->pkt_statistics |= STAT_ABORTED; 8373 /* Should we set key COMMAND_ABPRTED? */ 8374 break; 8375 8376 case SATA_PKT_RESET: 8377 scsipkt->pkt_reason = CMD_RESET; 8378 scsipkt->pkt_statistics |= STAT_DEV_RESET; 8379 /* 8380 * May be we should set Unit Attention / 8381 * Reset. Perhaps the same should be 8382 * returned for disks.... 8383 */ 8384 sense->es_key = KEY_UNIT_ATTENTION; 8385 sense->es_add_code = SD_SCSI_ASC_RESET; 8386 break; 8387 8388 default: 8389 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 8390 "sata_txlt_atapi_completion: " 8391 "invalid packet completion reason")); 8392 scsipkt->pkt_reason = CMD_TRAN_ERR; 8393 scsipkt->pkt_state &= ~(STATE_GOT_BUS | 8394 STATE_GOT_TARGET | STATE_SENT_CMD | 8395 STATE_GOT_STATUS); 8396 break; 8397 } 8398 } 8399 } 8400 8401 SATAATAPITRACE(spx, 0); 8402 8403 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 8404 scsipkt->pkt_comp != NULL) { 8405 /* scsi callback required */ 8406 (*scsipkt->pkt_comp)(scsipkt); 8407 } 8408 } 8409 8410 /* 8411 * Set up error retrieval sata command for ATAPI Packet Command error data 8412 * recovery. 8413 * 8414 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up, 8415 * returns SATA_FAILURE otherwise. 8416 */ 8417 8418 static int 8419 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo) 8420 { 8421 sata_pkt_t *spkt = spx->txlt_sata_pkt; 8422 sata_cmd_t *scmd; 8423 struct buf *bp; 8424 8425 /* 8426 * Allocate dma-able buffer error data. 8427 * Buffer allocation will take care of buffer alignment and other DMA 8428 * attributes. 8429 */ 8430 bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN); 8431 if (bp == NULL) { 8432 SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst, 8433 "sata_get_err_retrieval_pkt: " 8434 "cannot allocate buffer for error data", NULL); 8435 return (SATA_FAILURE); 8436 } 8437 bp_mapin(bp); /* make data buffer accessible */ 8438 8439 /* Operation modes are up to the caller */ 8440 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 8441 8442 /* Synchronous mode, no callback - may be changed by the caller */ 8443 spkt->satapkt_comp = NULL; 8444 spkt->satapkt_time = sata_default_pkt_time; 8445 8446 scmd = &spkt->satapkt_cmd; 8447 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 8448 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 8449 8450 sata_atapi_packet_cmd_setup(scmd, sdinfo); 8451 8452 /* 8453 * Set-up acdb. Request Sense CDB (packet command content) is 8454 * not in DMA-able buffer. Its handling is HBA-specific (how 8455 * it is transfered into packet FIS). 8456 */ 8457 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 8458 bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN); 8459 /* Following zeroing of pad bytes may not be necessary */ 8460 bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN], 8461 sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN); 8462 8463 /* 8464 * Set-up pointer to the buffer handle, so HBA can sync buffer 8465 * before accessing it. Handle is in usual place in translate struct. 8466 */ 8467 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle; 8468 8469 /* 8470 * Preset request sense data to NO SENSE. 8471 * Here it is redundant, only for a symetry with scsi-originated 8472 * packets. It should not be used for anything but debugging. 8473 */ 8474 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN); 8475 sata_fixed_sense_data_preset( 8476 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 8477 8478 ASSERT(scmd->satacmd_num_dma_cookies != 0); 8479 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 8480 8481 return (SATA_SUCCESS); 8482 } 8483 8484 /* 8485 * Set-up ATAPI packet command. 8486 * Data transfer direction has to be set-up in sata_cmd structure prior to 8487 * calling this function. 8488 * 8489 * Returns void 8490 */ 8491 8492 static void 8493 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo) 8494 { 8495 scmd->satacmd_addr_type = 0; /* N/A */ 8496 scmd->satacmd_sec_count_lsb = 0; /* no tag */ 8497 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 8498 scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ; 8499 scmd->satacmd_lba_high_lsb = 8500 (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8); 8501 scmd->satacmd_cmd_reg = SATAC_PACKET; /* Command */ 8502 8503 /* 8504 * We want all data to be transfered via DMA. 8505 * But specify it only if drive supports DMA and DMA mode is 8506 * selected - some drives are sensitive about it. 8507 * Hopefully it wil work for all drives.... 8508 */ 8509 if (sdinfo->satadrv_settings & SATA_DEV_DMA) 8510 scmd->satacmd_features_reg = SATA_ATAPI_F_DMA; 8511 8512 /* 8513 * Features register requires special care for devices that use 8514 * Serial ATA bridge - they need an explicit specification of 8515 * the data transfer direction for Packet DMA commands. 8516 * Setting this bit is harmless if DMA is not used. 8517 * 8518 * Many drives do not implement word 80, specifying what ATA/ATAPI 8519 * spec they follow. 8520 * We are arbitrarily following the latest SerialATA 2.6 spec, 8521 * which uses ATA/ATAPI 6 specification for Identify Data, unless 8522 * ATA/ATAPI-7 support is explicitly indicated. 8523 */ 8524 if (sdinfo->satadrv_id.ai_majorversion != 0 && 8525 sdinfo->satadrv_id.ai_majorversion != 0xffff && 8526 (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) { 8527 /* 8528 * Specification of major version is valid and version 7 8529 * is supported. It does automatically imply that all 8530 * spec features are supported. For now, we assume that 8531 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete. 8532 */ 8533 if ((sdinfo->satadrv_id.ai_dirdma & 8534 SATA_ATAPI_ID_DMADIR_REQ) != 0) { 8535 if (scmd->satacmd_flags.sata_data_direction == 8536 SATA_DIR_READ) 8537 scmd->satacmd_features_reg |= 8538 SATA_ATAPI_F_DATA_DIR_READ; 8539 } 8540 } 8541 } 8542 8543 8544 #ifdef SATA_DEBUG 8545 8546 /* Display 18 bytes of Inquiry data */ 8547 static void 8548 sata_show_inqry_data(uint8_t *buf) 8549 { 8550 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 8551 uint8_t *p; 8552 8553 cmn_err(CE_NOTE, "Inquiry data:"); 8554 cmn_err(CE_NOTE, "device type %x", inq->inq_dtype); 8555 cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb); 8556 cmn_err(CE_NOTE, "version %x", inq->inq_ansi); 8557 cmn_err(CE_NOTE, "ATAPI transport version %d", 8558 SATA_ATAPI_TRANS_VERSION(inq)); 8559 cmn_err(CE_NOTE, "response data format %d, aenc %d", 8560 inq->inq_rdf, inq->inq_aenc); 8561 cmn_err(CE_NOTE, " additional length %d", inq->inq_len); 8562 cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs); 8563 p = (uint8_t *)inq->inq_vid; 8564 cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x " 8565 "%02x %02x %02x %02x", 8566 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 8567 p = (uint8_t *)inq->inq_vid; 8568 cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c", 8569 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 8570 8571 p = (uint8_t *)inq->inq_pid; 8572 cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x " 8573 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", 8574 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 8575 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 8576 p = (uint8_t *)inq->inq_pid; 8577 cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c " 8578 "%c %c %c %c %c %c %c %c", 8579 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 8580 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 8581 8582 p = (uint8_t *)inq->inq_revision; 8583 cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x", 8584 p[0], p[1], p[2], p[3]); 8585 p = (uint8_t *)inq->inq_revision; 8586 cmn_err(CE_NOTE, "revision: %c %c %c %c", 8587 p[0], p[1], p[2], p[3]); 8588 8589 } 8590 8591 8592 static void 8593 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count) 8594 { 8595 struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt; 8596 8597 if (scsi_pkt == NULL) 8598 return; 8599 if (count != 0) { 8600 /* saving cdb */ 8601 bzero(sata_atapi_trace[sata_atapi_trace_index].acdb, 8602 SATA_ATAPI_MAX_CDB_LEN); 8603 bcopy(scsi_pkt->pkt_cdbp, 8604 sata_atapi_trace[sata_atapi_trace_index].acdb, count); 8605 } else { 8606 bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)-> 8607 sts_sensedata, 8608 sata_atapi_trace[sata_atapi_trace_index].arqs, 8609 SATA_ATAPI_MIN_RQSENSE_LEN); 8610 sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason = 8611 scsi_pkt->pkt_reason; 8612 sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason = 8613 spx->txlt_sata_pkt->satapkt_reason; 8614 8615 if (++sata_atapi_trace_index >= 64) 8616 sata_atapi_trace_index = 0; 8617 } 8618 } 8619 8620 #endif 8621 8622 /* 8623 * Fetch inquiry data from ATAPI device 8624 * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise. 8625 * 8626 * Note: 8627 * inqb pointer does not point to a DMA-able buffer. It is a local buffer 8628 * where the caller expects to see the inquiry data. 8629 * 8630 */ 8631 8632 static int 8633 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba, 8634 sata_address_t *saddr, struct scsi_inquiry *inq) 8635 { 8636 sata_pkt_txlate_t *spx; 8637 sata_pkt_t *spkt; 8638 struct buf *bp; 8639 sata_drive_info_t *sdinfo; 8640 sata_cmd_t *scmd; 8641 int rval; 8642 uint8_t *rqsp; 8643 #ifdef SATA_DEBUG 8644 char msg_buf[MAXPATHLEN]; 8645 #endif 8646 8647 ASSERT(sata_hba != NULL); 8648 8649 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 8650 spx->txlt_sata_hba_inst = sata_hba; 8651 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 8652 spkt = sata_pkt_alloc(spx, NULL); 8653 if (spkt == NULL) { 8654 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 8655 return (SATA_FAILURE); 8656 } 8657 /* address is needed now */ 8658 spkt->satapkt_device.satadev_addr = *saddr; 8659 8660 /* scsi_inquiry size buffer */ 8661 bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry)); 8662 if (bp == NULL) { 8663 sata_pkt_free(spx); 8664 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 8665 SATA_LOG_D((sata_hba, CE_WARN, 8666 "sata_get_atapi_inquiry_data: " 8667 "cannot allocate data buffer")); 8668 return (SATA_FAILURE); 8669 } 8670 bp_mapin(bp); /* make data buffer accessible */ 8671 8672 scmd = &spkt->satapkt_cmd; 8673 ASSERT(scmd->satacmd_num_dma_cookies != 0); 8674 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 8675 8676 /* Use synchronous mode */ 8677 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 8678 spkt->satapkt_comp = NULL; 8679 spkt->satapkt_time = sata_default_pkt_time; 8680 8681 /* Issue inquiry command - 6 bytes cdb, data transfer, read */ 8682 8683 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 8684 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 8685 8686 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 8687 sdinfo = sata_get_device_info(sata_hba, 8688 &spx->txlt_sata_pkt->satapkt_device); 8689 if (sdinfo == NULL) { 8690 /* we have to be carefull about the disapearing device */ 8691 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 8692 rval = SATA_FAILURE; 8693 goto cleanup; 8694 } 8695 sata_atapi_packet_cmd_setup(scmd, sdinfo); 8696 8697 /* 8698 * Set-up acdb. This works for atapi transport version 2 and later. 8699 */ 8700 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 8701 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 8702 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */ 8703 scmd->satacmd_acdb[1] = 0x00; 8704 scmd->satacmd_acdb[2] = 0x00; 8705 scmd->satacmd_acdb[3] = 0x00; 8706 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry); 8707 scmd->satacmd_acdb[5] = 0x00; 8708 8709 sata_fixed_sense_data_preset( 8710 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 8711 8712 /* Transfer command to HBA */ 8713 if (sata_hba_start(spx, &rval) != 0) { 8714 /* Pkt not accepted for execution */ 8715 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8716 "sata_get_atapi_inquiry_data: " 8717 "Packet not accepted for execution - ret: %02x", rval); 8718 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 8719 rval = SATA_FAILURE; 8720 goto cleanup; 8721 } 8722 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 8723 8724 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) { 8725 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8726 "sata_get_atapi_inquiry_data: " 8727 "Packet completed successfully - ret: %02x", rval); 8728 if (spx->txlt_buf_dma_handle != NULL) { 8729 /* 8730 * Sync buffer. Handle is in usual place in translate 8731 * struct. 8732 */ 8733 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 8734 DDI_DMA_SYNC_FORCPU); 8735 ASSERT(rval == DDI_SUCCESS); 8736 } 8737 /* 8738 * Normal completion - copy data into caller's buffer 8739 */ 8740 bcopy(bp->b_un.b_addr, (uint8_t *)inq, 8741 sizeof (struct scsi_inquiry)); 8742 #ifdef SATA_DEBUG 8743 if (sata_debug_flags & SATA_DBG_ATAPI) { 8744 sata_show_inqry_data((uint8_t *)inq); 8745 } 8746 #endif 8747 rval = SATA_SUCCESS; 8748 } else { 8749 /* 8750 * Something went wrong - analyze return - check rqsense data 8751 */ 8752 rval = SATA_FAILURE; 8753 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 8754 /* 8755 * ARQ data hopefull show something other than NO SENSE 8756 */ 8757 rqsp = scmd->satacmd_rqsense; 8758 #ifdef SATA_DEBUG 8759 if (sata_debug_flags & SATA_DBG_ATAPI) { 8760 msg_buf[0] = '\0'; 8761 (void) snprintf(msg_buf, MAXPATHLEN, 8762 "ATAPI packet completion reason: %02x\n" 8763 "RQSENSE: %02x %02x %02x %02x %02x %02x\n" 8764 " %02x %02x %02x %02x %02x %02x\n" 8765 " %02x %02x %02x %02x %02x %02x", 8766 spkt->satapkt_reason, 8767 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 8768 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 8769 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 8770 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 8771 rqsp[16], rqsp[17]); 8772 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 8773 "%s", msg_buf); 8774 } 8775 #endif 8776 } else { 8777 switch (spkt->satapkt_reason) { 8778 case SATA_PKT_PORT_ERROR: 8779 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8780 "sata_get_atapi_inquiry_data: " 8781 "packet reason: port error", NULL); 8782 break; 8783 8784 case SATA_PKT_TIMEOUT: 8785 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8786 "sata_get_atapi_inquiry_data: " 8787 "packet reason: timeout", NULL); 8788 break; 8789 8790 case SATA_PKT_ABORTED: 8791 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8792 "sata_get_atapi_inquiry_data: " 8793 "packet reason: aborted", NULL); 8794 break; 8795 8796 case SATA_PKT_RESET: 8797 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8798 "sata_get_atapi_inquiry_data: " 8799 "packet reason: reset\n", NULL); 8800 break; 8801 default: 8802 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8803 "sata_get_atapi_inquiry_data: " 8804 "invalid packet reason: %02x\n", 8805 spkt->satapkt_reason); 8806 break; 8807 } 8808 } 8809 } 8810 cleanup: 8811 sata_free_local_buffer(spx); 8812 sata_pkt_free(spx); 8813 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 8814 return (rval); 8815 } 8816 8817 8818 8819 8820 8821 #if 0 8822 #ifdef SATA_DEBUG 8823 8824 /* 8825 * Test ATAPI packet command. 8826 * Single threaded test: send packet command in synch mode, process completion 8827 * 8828 */ 8829 static void 8830 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport) 8831 { 8832 sata_pkt_txlate_t *spx; 8833 sata_pkt_t *spkt; 8834 struct buf *bp; 8835 sata_device_t sata_device; 8836 sata_drive_info_t *sdinfo; 8837 sata_cmd_t *scmd; 8838 int rval; 8839 uint8_t *rqsp; 8840 8841 ASSERT(sata_hba_inst != NULL); 8842 sata_device.satadev_addr.cport = cport; 8843 sata_device.satadev_addr.pmport = 0; 8844 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 8845 sata_device.satadev_rev = SATA_DEVICE_REV; 8846 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 8847 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 8848 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 8849 if (sdinfo == NULL) { 8850 sata_log(sata_hba_inst, CE_WARN, 8851 "sata_test_atapi_packet_command: " 8852 "no device info for cport %d", 8853 sata_device.satadev_addr.cport); 8854 return; 8855 } 8856 8857 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 8858 spx->txlt_sata_hba_inst = sata_hba_inst; 8859 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 8860 spkt = sata_pkt_alloc(spx, NULL); 8861 if (spkt == NULL) { 8862 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 8863 return; 8864 } 8865 /* address is needed now */ 8866 spkt->satapkt_device.satadev_addr = sata_device.satadev_addr; 8867 8868 /* 1024k buffer */ 8869 bp = sata_alloc_local_buffer(spx, 1024); 8870 if (bp == NULL) { 8871 sata_pkt_free(spx); 8872 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 8873 sata_log(sata_hba_inst, CE_WARN, 8874 "sata_test_atapi_packet_command: " 8875 "cannot allocate data buffer"); 8876 return; 8877 } 8878 bp_mapin(bp); /* make data buffer accessible */ 8879 8880 scmd = &spkt->satapkt_cmd; 8881 ASSERT(scmd->satacmd_num_dma_cookies != 0); 8882 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 8883 8884 /* Use synchronous mode */ 8885 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 8886 8887 /* Synchronous mode, no callback - may be changed by the caller */ 8888 spkt->satapkt_comp = NULL; 8889 spkt->satapkt_time = sata_default_pkt_time; 8890 8891 /* Issue inquiry command - 6 bytes cdb, data transfer, read */ 8892 8893 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 8894 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 8895 8896 sata_atapi_packet_cmd_setup(scmd, sdinfo); 8897 8898 /* Set-up acdb. */ 8899 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 8900 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 8901 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */ 8902 scmd->satacmd_acdb[1] = 0x00; 8903 scmd->satacmd_acdb[2] = 0x00; 8904 scmd->satacmd_acdb[3] = 0x00; 8905 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry); 8906 scmd->satacmd_acdb[5] = 0x00; 8907 8908 sata_fixed_sense_data_preset( 8909 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 8910 8911 /* Transfer command to HBA */ 8912 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 8913 if (sata_hba_start(spx, &rval) != 0) { 8914 /* Pkt not accepted for execution */ 8915 sata_log(sata_hba_inst, CE_WARN, 8916 "sata_test_atapi_packet_command: " 8917 "Packet not accepted for execution - ret: %02x", rval); 8918 mutex_exit( 8919 &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 8920 goto cleanup; 8921 } 8922 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 8923 8924 if (spx->txlt_buf_dma_handle != NULL) { 8925 /* 8926 * Sync buffer. Handle is in usual place in translate struct. 8927 */ 8928 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 8929 DDI_DMA_SYNC_FORCPU); 8930 ASSERT(rval == DDI_SUCCESS); 8931 } 8932 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) { 8933 sata_log(sata_hba_inst, CE_WARN, 8934 "sata_test_atapi_packet_command: " 8935 "Packet completed successfully"); 8936 /* 8937 * Normal completion - show inquiry data 8938 */ 8939 sata_show_inqry_data((uint8_t *)bp->b_un.b_addr); 8940 } else { 8941 /* 8942 * Something went wrong - analyze return - check rqsense data 8943 */ 8944 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 8945 /* 8946 * ARQ data hopefull show something other than NO SENSE 8947 */ 8948 rqsp = scmd->satacmd_rqsense; 8949 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 8950 "ATAPI packet completion reason: %02x\n" 8951 "RQSENSE: %02x %02x %02x %02x %02x %02x " 8952 " %02x %02x %02x %02x %02x %02x " 8953 " %02x %02x %02x %02x %02x %02x\n", 8954 spkt->satapkt_reason, 8955 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 8956 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 8957 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 8958 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 8959 rqsp[16], rqsp[17]); 8960 } else { 8961 switch (spkt->satapkt_reason) { 8962 case SATA_PKT_PORT_ERROR: 8963 sata_log(sata_hba_inst, CE_WARN, 8964 "sata_test_atapi_packet_command: " 8965 "packet reason: port error\n"); 8966 break; 8967 8968 case SATA_PKT_TIMEOUT: 8969 sata_log(sata_hba_inst, CE_WARN, 8970 "sata_test_atapi_packet_command: " 8971 "packet reason: timeout\n"); 8972 break; 8973 8974 case SATA_PKT_ABORTED: 8975 sata_log(sata_hba_inst, CE_WARN, 8976 "sata_test_atapi_packet_command: " 8977 "packet reason: aborted\n"); 8978 break; 8979 8980 case SATA_PKT_RESET: 8981 sata_log(sata_hba_inst, CE_WARN, 8982 "sata_test_atapi_packet_command: " 8983 "packet reason: reset\n"); 8984 break; 8985 default: 8986 sata_log(sata_hba_inst, CE_WARN, 8987 "sata_test_atapi_packet_command: " 8988 "invalid packet reason: %02x\n", 8989 spkt->satapkt_reason); 8990 break; 8991 } 8992 } 8993 } 8994 cleanup: 8995 sata_free_local_buffer(spx); 8996 sata_pkt_free(spx); 8997 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 8998 } 8999 9000 #endif /* SATA_DEBUG */ 9001 #endif /* 1 */ 9002 9003 9004 /* ************************** LOCAL HELPER FUNCTIONS *********************** */ 9005 9006 /* 9007 * Validate sata_tran info 9008 * SATA_FAILURE returns if structure is inconsistent or structure revision 9009 * does not match one used by the framework. 9010 * 9011 * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains 9012 * required function pointers. 9013 * Returns SATA_FAILURE otherwise. 9014 */ 9015 static int 9016 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran) 9017 { 9018 /* 9019 * SATA_TRAN_HBA_REV is the current (highest) revision number 9020 * of the SATA interface. 9021 */ 9022 if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) { 9023 sata_log(NULL, CE_WARN, 9024 "sata: invalid sata_hba_tran version %d for driver %s", 9025 sata_tran->sata_tran_hba_rev, ddi_driver_name(dip)); 9026 return (SATA_FAILURE); 9027 } 9028 9029 if (dip != sata_tran->sata_tran_hba_dip) { 9030 SATA_LOG_D((NULL, CE_WARN, 9031 "sata: inconsistent sata_tran_hba_dip " 9032 "%p / %p", sata_tran->sata_tran_hba_dip, dip)); 9033 return (SATA_FAILURE); 9034 } 9035 9036 if (sata_tran->sata_tran_probe_port == NULL || 9037 sata_tran->sata_tran_start == NULL || 9038 sata_tran->sata_tran_abort == NULL || 9039 sata_tran->sata_tran_reset_dport == NULL || 9040 sata_tran->sata_tran_hotplug_ops == NULL || 9041 sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL || 9042 sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate == 9043 NULL) { 9044 SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing " 9045 "required functions")); 9046 } 9047 return (SATA_SUCCESS); 9048 } 9049 9050 /* 9051 * Remove HBA instance from sata_hba_list. 9052 */ 9053 static void 9054 sata_remove_hba_instance(dev_info_t *dip) 9055 { 9056 sata_hba_inst_t *sata_hba_inst; 9057 9058 mutex_enter(&sata_mutex); 9059 for (sata_hba_inst = sata_hba_list; 9060 sata_hba_inst != (struct sata_hba_inst *)NULL; 9061 sata_hba_inst = sata_hba_inst->satahba_next) { 9062 if (sata_hba_inst->satahba_dip == dip) 9063 break; 9064 } 9065 9066 if (sata_hba_inst == (struct sata_hba_inst *)NULL) { 9067 #ifdef SATA_DEBUG 9068 cmn_err(CE_WARN, "sata_remove_hba_instance: " 9069 "unknown HBA instance\n"); 9070 #endif 9071 ASSERT(FALSE); 9072 } 9073 if (sata_hba_inst == sata_hba_list) { 9074 sata_hba_list = sata_hba_inst->satahba_next; 9075 if (sata_hba_list) { 9076 sata_hba_list->satahba_prev = 9077 (struct sata_hba_inst *)NULL; 9078 } 9079 if (sata_hba_inst == sata_hba_list_tail) { 9080 sata_hba_list_tail = NULL; 9081 } 9082 } else if (sata_hba_inst == sata_hba_list_tail) { 9083 sata_hba_list_tail = sata_hba_inst->satahba_prev; 9084 if (sata_hba_list_tail) { 9085 sata_hba_list_tail->satahba_next = 9086 (struct sata_hba_inst *)NULL; 9087 } 9088 } else { 9089 sata_hba_inst->satahba_prev->satahba_next = 9090 sata_hba_inst->satahba_next; 9091 sata_hba_inst->satahba_next->satahba_prev = 9092 sata_hba_inst->satahba_prev; 9093 } 9094 mutex_exit(&sata_mutex); 9095 } 9096 9097 /* 9098 * Probe all SATA ports of the specified HBA instance. 9099 * The assumption is that there are no target and attachment point minor nodes 9100 * created by the boot subsystems, so we do not need to prune device tree. 9101 * 9102 * This function is called only from sata_hba_attach(). It does not have to 9103 * be protected by controller mutex, because the hba_attached flag is not set 9104 * yet and no one would be touching this HBA instance other than this thread. 9105 * Determines if port is active and what type of the device is attached 9106 * (if any). Allocates necessary structures for each port. 9107 * 9108 * An AP (Attachement Point) node is created for each SATA device port even 9109 * when there is no device attached. 9110 */ 9111 9112 static void 9113 sata_probe_ports(sata_hba_inst_t *sata_hba_inst) 9114 { 9115 dev_info_t *dip = SATA_DIP(sata_hba_inst); 9116 int ncport; 9117 sata_cport_info_t *cportinfo; 9118 sata_drive_info_t *drive; 9119 sata_device_t sata_device; 9120 int rval; 9121 dev_t minor_number; 9122 char name[16]; 9123 clock_t start_time, cur_time; 9124 9125 /* 9126 * Probe controller ports first, to find port status and 9127 * any port multiplier attached. 9128 */ 9129 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 9130 /* allocate cport structure */ 9131 cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP); 9132 ASSERT(cportinfo != NULL); 9133 mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL); 9134 9135 mutex_enter(&cportinfo->cport_mutex); 9136 9137 cportinfo->cport_addr.cport = ncport; 9138 cportinfo->cport_addr.pmport = 0; 9139 cportinfo->cport_addr.qual = SATA_ADDR_CPORT; 9140 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 9141 cportinfo->cport_state |= SATA_STATE_PROBING; 9142 SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo; 9143 9144 /* 9145 * Regardless if a port is usable or not, create 9146 * an attachment point 9147 */ 9148 mutex_exit(&cportinfo->cport_mutex); 9149 minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip), 9150 ncport, 0, SATA_ADDR_CPORT); 9151 (void) sprintf(name, "%d", ncport); 9152 if (ddi_create_minor_node(dip, name, S_IFCHR, 9153 minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) != 9154 DDI_SUCCESS) { 9155 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: " 9156 "cannot create SATA attachment point for port %d", 9157 ncport); 9158 } 9159 9160 /* Probe port */ 9161 start_time = ddi_get_lbolt(); 9162 reprobe_cport: 9163 sata_device.satadev_addr.cport = ncport; 9164 sata_device.satadev_addr.pmport = 0; 9165 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 9166 sata_device.satadev_rev = SATA_DEVICE_REV; 9167 9168 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 9169 (dip, &sata_device); 9170 9171 mutex_enter(&cportinfo->cport_mutex); 9172 cportinfo->cport_scr = sata_device.satadev_scr; 9173 if (rval != SATA_SUCCESS) { 9174 /* Something went wrong? Fail the port */ 9175 cportinfo->cport_state = SATA_PSTATE_FAILED; 9176 mutex_exit(&cportinfo->cport_mutex); 9177 continue; 9178 } 9179 cportinfo->cport_state &= ~SATA_STATE_PROBING; 9180 cportinfo->cport_state |= SATA_STATE_PROBED; 9181 cportinfo->cport_dev_type = sata_device.satadev_type; 9182 9183 cportinfo->cport_state |= SATA_STATE_READY; 9184 if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) { 9185 mutex_exit(&cportinfo->cport_mutex); 9186 continue; 9187 } 9188 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 9189 /* 9190 * There is some device attached. 9191 * Allocate device info structure 9192 */ 9193 if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) { 9194 mutex_exit(&cportinfo->cport_mutex); 9195 SATA_CPORTINFO_DRV_INFO(cportinfo) = 9196 kmem_zalloc(sizeof (sata_drive_info_t), 9197 KM_SLEEP); 9198 mutex_enter(&cportinfo->cport_mutex); 9199 } 9200 drive = SATA_CPORTINFO_DRV_INFO(cportinfo); 9201 drive->satadrv_addr = cportinfo->cport_addr; 9202 drive->satadrv_addr.qual = SATA_ADDR_DCPORT; 9203 drive->satadrv_type = cportinfo->cport_dev_type; 9204 drive->satadrv_state = SATA_STATE_UNKNOWN; 9205 9206 mutex_exit(&cportinfo->cport_mutex); 9207 if (sata_add_device(dip, sata_hba_inst, &sata_device) != 9208 SATA_SUCCESS) { 9209 /* 9210 * Plugged device was not correctly identified. 9211 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT 9212 */ 9213 cur_time = ddi_get_lbolt(); 9214 if ((cur_time - start_time) < 9215 drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) { 9216 /* sleep for a while */ 9217 delay(drv_usectohz( 9218 SATA_DEV_RETRY_DLY)); 9219 goto reprobe_cport; 9220 } 9221 } 9222 } else { /* SATA_DTYPE_PMULT */ 9223 mutex_exit(&cportinfo->cport_mutex); 9224 9225 /* Allocate sata_pmult_info and sata_pmport_info */ 9226 sata_alloc_pmult(sata_hba_inst, &sata_device); 9227 9228 /* Log the information of the port multiplier */ 9229 sata_show_pmult_info(sata_hba_inst, &sata_device); 9230 9231 /* Probe its pmports */ 9232 sata_probe_pmports(sata_hba_inst, ncport); 9233 } 9234 } 9235 } 9236 9237 /* 9238 * Probe all device ports behind a port multiplier. 9239 * 9240 * PMult-related structure should be allocated before by sata_alloc_pmult(). 9241 * 9242 * NOTE1: Only called from sata_probe_ports() 9243 * NOTE2: No mutex should be hold. 9244 */ 9245 static void 9246 sata_probe_pmports(sata_hba_inst_t *sata_hba_inst, uint8_t ncport) 9247 { 9248 dev_info_t *dip = SATA_DIP(sata_hba_inst); 9249 sata_pmult_info_t *pmultinfo = NULL; 9250 sata_pmport_info_t *pmportinfo = NULL; 9251 sata_drive_info_t *drive = NULL; 9252 sata_device_t sata_device; 9253 9254 clock_t start_time, cur_time; 9255 int npmport; 9256 int rval; 9257 9258 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, ncport); 9259 9260 /* Probe Port Multiplier ports */ 9261 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; npmport++) { 9262 pmportinfo = pmultinfo->pmult_dev_port[npmport]; 9263 start_time = ddi_get_lbolt(); 9264 reprobe_pmport: 9265 sata_device.satadev_addr.cport = ncport; 9266 sata_device.satadev_addr.pmport = npmport; 9267 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT; 9268 sata_device.satadev_rev = SATA_DEVICE_REV; 9269 9270 /* Let HBA driver probe it. */ 9271 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 9272 (dip, &sata_device); 9273 mutex_enter(&pmportinfo->pmport_mutex); 9274 9275 pmportinfo->pmport_scr = sata_device.satadev_scr; 9276 9277 if (rval != SATA_SUCCESS) { 9278 pmportinfo->pmport_state = 9279 SATA_PSTATE_FAILED; 9280 mutex_exit(&pmportinfo->pmport_mutex); 9281 continue; 9282 } 9283 pmportinfo->pmport_state &= ~SATA_STATE_PROBING; 9284 pmportinfo->pmport_state |= SATA_STATE_PROBED; 9285 pmportinfo->pmport_dev_type = sata_device.satadev_type; 9286 9287 pmportinfo->pmport_state |= SATA_STATE_READY; 9288 if (pmportinfo->pmport_dev_type == 9289 SATA_DTYPE_NONE) { 9290 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 9291 "no device found at port %d:%d", ncport, npmport); 9292 mutex_exit(&pmportinfo->pmport_mutex); 9293 continue; 9294 } 9295 /* Port multipliers cannot be chained */ 9296 ASSERT(pmportinfo->pmport_dev_type != SATA_DTYPE_PMULT); 9297 /* 9298 * There is something attached to Port 9299 * Multiplier device port 9300 * Allocate device info structure 9301 */ 9302 if (pmportinfo->pmport_sata_drive == NULL) { 9303 mutex_exit(&pmportinfo->pmport_mutex); 9304 pmportinfo->pmport_sata_drive = 9305 kmem_zalloc(sizeof (sata_drive_info_t), KM_SLEEP); 9306 mutex_enter(&pmportinfo->pmport_mutex); 9307 } 9308 drive = pmportinfo->pmport_sata_drive; 9309 drive->satadrv_addr.cport = pmportinfo->pmport_addr.cport; 9310 drive->satadrv_addr.pmport = npmport; 9311 drive->satadrv_addr.qual = SATA_ADDR_DPMPORT; 9312 drive->satadrv_type = pmportinfo-> pmport_dev_type; 9313 drive->satadrv_state = SATA_STATE_UNKNOWN; 9314 9315 mutex_exit(&pmportinfo->pmport_mutex); 9316 rval = sata_add_device(dip, sata_hba_inst, &sata_device); 9317 9318 if (rval != SATA_SUCCESS) { 9319 /* 9320 * Plugged device was not correctly identified. 9321 * Retry, within the SATA_DEV_IDENTIFY_TIMEOUT 9322 */ 9323 cur_time = ddi_get_lbolt(); 9324 if ((cur_time - start_time) < drv_usectohz( 9325 SATA_DEV_IDENTIFY_TIMEOUT)) { 9326 /* sleep for a while */ 9327 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 9328 goto reprobe_pmport; 9329 } 9330 } 9331 } 9332 } 9333 9334 /* 9335 * Add SATA device for specified HBA instance & port (SCSI target 9336 * device nodes). 9337 * This function is called (indirectly) only from sata_hba_attach(). 9338 * A target node is created when there is a supported type device attached, 9339 * but may be removed if it cannot be put online. 9340 * 9341 * This function cannot be called from an interrupt context. 9342 * 9343 * Create target nodes for disk, CD/DVD, Tape and ATAPI disk devices 9344 * 9345 * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when 9346 * device identification failed - adding a device could be retried. 9347 * 9348 */ 9349 static int 9350 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, 9351 sata_device_t *sata_device) 9352 { 9353 sata_cport_info_t *cportinfo; 9354 sata_pmult_info_t *pminfo; 9355 sata_pmport_info_t *pmportinfo; 9356 dev_info_t *cdip; /* child dip */ 9357 sata_address_t *saddr = &sata_device->satadev_addr; 9358 uint8_t cport, pmport; 9359 int rval; 9360 9361 cport = saddr->cport; 9362 pmport = saddr->pmport; 9363 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 9364 ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE); 9365 9366 /* 9367 * Some device is attached to a controller port. 9368 * We rely on controllers distinquishing between no-device, 9369 * attached port multiplier and other kind of attached device. 9370 * We need to get Identify Device data and determine 9371 * positively the dev type before trying to attach 9372 * the target driver. 9373 */ 9374 sata_device->satadev_rev = SATA_DEVICE_REV; 9375 switch (saddr->qual) { 9376 case SATA_ADDR_CPORT: 9377 /* 9378 * Add a non-port-multiplier device at controller port. 9379 */ 9380 saddr->qual = SATA_ADDR_DCPORT; 9381 9382 rval = sata_probe_device(sata_hba_inst, sata_device); 9383 if (rval != SATA_SUCCESS || 9384 sata_device->satadev_type == SATA_DTYPE_UNKNOWN) 9385 return (SATA_FAILURE); 9386 9387 mutex_enter(&cportinfo->cport_mutex); 9388 sata_show_drive_info(sata_hba_inst, 9389 SATA_CPORTINFO_DRV_INFO(cportinfo)); 9390 9391 if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) { 9392 /* 9393 * Could not determine device type or 9394 * a device is not supported. 9395 * Degrade this device to unknown. 9396 */ 9397 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 9398 mutex_exit(&cportinfo->cport_mutex); 9399 return (SATA_SUCCESS); 9400 } 9401 cportinfo->cport_dev_type = sata_device->satadev_type; 9402 cportinfo->cport_tgtnode_clean = B_TRUE; 9403 mutex_exit(&cportinfo->cport_mutex); 9404 9405 /* 9406 * Initialize device to the desired state. Even if it 9407 * fails, the device will still attach but syslog 9408 * will show the warning. 9409 */ 9410 if (sata_initialize_device(sata_hba_inst, 9411 SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) { 9412 /* Retry */ 9413 rval = sata_initialize_device(sata_hba_inst, 9414 SATA_CPORTINFO_DRV_INFO(cportinfo)); 9415 9416 if (rval == SATA_RETRY) 9417 sata_log(sata_hba_inst, CE_WARN, 9418 "SATA device at port %d - " 9419 "default device features could not be set." 9420 " Device may not operate as expected.", 9421 cport); 9422 } 9423 9424 cdip = sata_create_target_node(pdip, sata_hba_inst, saddr); 9425 if (cdip == NULL) { 9426 /* 9427 * Attaching target node failed. 9428 * We retain sata_drive_info structure... 9429 */ 9430 return (SATA_SUCCESS); 9431 } 9432 9433 mutex_enter(&cportinfo->cport_mutex); 9434 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 9435 satadrv_state = SATA_STATE_READY; 9436 mutex_exit(&cportinfo->cport_mutex); 9437 9438 break; 9439 9440 case SATA_ADDR_PMPORT: 9441 saddr->qual = SATA_ADDR_DPMPORT; 9442 9443 mutex_enter(&cportinfo->cport_mutex); 9444 /* It must be a Port Multiplier at the controller port */ 9445 ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT); 9446 9447 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 9448 pmportinfo = pminfo->pmult_dev_port[saddr->pmport]; 9449 mutex_exit(&cportinfo->cport_mutex); 9450 9451 rval = sata_probe_device(sata_hba_inst, sata_device); 9452 if (rval != SATA_SUCCESS || 9453 sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 9454 return (SATA_FAILURE); 9455 } 9456 9457 mutex_enter(&pmportinfo->pmport_mutex); 9458 sata_show_drive_info(sata_hba_inst, 9459 SATA_PMPORTINFO_DRV_INFO(pmportinfo)); 9460 9461 if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) { 9462 /* 9463 * Could not determine device type. 9464 * Degrade this device to unknown. 9465 */ 9466 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN; 9467 mutex_exit(&pmportinfo->pmport_mutex); 9468 return (SATA_SUCCESS); 9469 } 9470 pmportinfo->pmport_dev_type = sata_device->satadev_type; 9471 pmportinfo->pmport_tgtnode_clean = B_TRUE; 9472 mutex_exit(&pmportinfo->pmport_mutex); 9473 9474 /* 9475 * Initialize device to the desired state. 9476 * Even if it fails, the device will still 9477 * attach but syslog will show the warning. 9478 */ 9479 if (sata_initialize_device(sata_hba_inst, 9480 pmportinfo->pmport_sata_drive) != SATA_SUCCESS) { 9481 /* Retry */ 9482 rval = sata_initialize_device(sata_hba_inst, 9483 pmportinfo->pmport_sata_drive); 9484 9485 if (rval == SATA_RETRY) 9486 sata_log(sata_hba_inst, CE_WARN, 9487 "SATA device at port %d:%d - " 9488 "default device features could not be set." 9489 " Device may not operate as expected.", 9490 cport, pmport); 9491 } 9492 9493 cdip = sata_create_target_node(pdip, sata_hba_inst, saddr); 9494 if (cdip == NULL) { 9495 /* 9496 * Attaching target node failed. 9497 * We retain sata_drive_info structure... 9498 */ 9499 return (SATA_SUCCESS); 9500 } 9501 mutex_enter(&pmportinfo->pmport_mutex); 9502 pmportinfo->pmport_sata_drive->satadrv_state |= 9503 SATA_STATE_READY; 9504 mutex_exit(&pmportinfo->pmport_mutex); 9505 9506 break; 9507 9508 default: 9509 return (SATA_FAILURE); 9510 } 9511 9512 return (SATA_SUCCESS); 9513 } 9514 9515 /* 9516 * Clean up target node at specific address. 9517 * 9518 * NOTE: No Mutex should be hold. 9519 */ 9520 static int 9521 sata_offline_device(sata_hba_inst_t *sata_hba_inst, 9522 sata_device_t *sata_device, sata_drive_info_t *sdinfo) 9523 { 9524 uint8_t cport, pmport, qual; 9525 dev_info_t *tdip; 9526 9527 cport = sata_device->satadev_addr.cport; 9528 pmport = sata_device->satadev_addr.pmport; 9529 qual = sata_device->satadev_addr.qual; 9530 9531 if (qual == SATA_ADDR_DCPORT) { 9532 SATA_LOG_D((sata_hba_inst, CE_WARN, 9533 "sata_hba_ioctl: disconnect device at port %d", cport)); 9534 } else { 9535 SATA_LOG_D((sata_hba_inst, CE_WARN, 9536 "sata_hba_ioctl: disconnect device at port %d:%d", 9537 cport, pmport)); 9538 } 9539 9540 /* We are addressing attached device, not a port */ 9541 sata_device->satadev_addr.qual = 9542 sdinfo->satadrv_addr.qual; 9543 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 9544 &sata_device->satadev_addr); 9545 if (tdip != NULL && ndi_devi_offline(tdip, 9546 NDI_DEVI_REMOVE) != NDI_SUCCESS) { 9547 /* 9548 * Problem : 9549 * The target node remained attached. 9550 * This happens when the device file was open 9551 * or a node was waiting for resources. 9552 * Cannot do anything about it. 9553 */ 9554 if (qual == SATA_ADDR_DCPORT) { 9555 SATA_LOG_D((sata_hba_inst, CE_WARN, 9556 "sata_hba_ioctl: disconnect: could " 9557 "not unconfigure device before " 9558 "disconnecting the SATA port %d", 9559 cport)); 9560 } else { 9561 SATA_LOG_D((sata_hba_inst, CE_WARN, 9562 "sata_hba_ioctl: disconnect: could " 9563 "not unconfigure device before " 9564 "disconnecting the SATA port %d:%d", 9565 cport, pmport)); 9566 } 9567 /* 9568 * Set DEVICE REMOVED state in the target 9569 * node. It will prevent access to the device 9570 * even when a new device is attached, until 9571 * the old target node is released, removed and 9572 * recreated for a new device. 9573 */ 9574 sata_set_device_removed(tdip); 9575 9576 /* 9577 * Instruct event daemon to try the target 9578 * node cleanup later. 9579 */ 9580 sata_set_target_node_cleanup( 9581 sata_hba_inst, &sata_device->satadev_addr); 9582 } 9583 9584 9585 return (SATA_SUCCESS); 9586 } 9587 9588 9589 /* 9590 * Create scsi target node for attached device, create node properties and 9591 * attach the node. 9592 * The node could be removed if the device onlining fails. 9593 * 9594 * A dev_info_t pointer is returned if operation is successful, NULL is 9595 * returned otherwise. 9596 */ 9597 9598 static dev_info_t * 9599 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst, 9600 sata_address_t *sata_addr) 9601 { 9602 dev_info_t *cdip = NULL; 9603 int rval; 9604 char *nname = NULL; 9605 char **compatible = NULL; 9606 int ncompatible; 9607 struct scsi_inquiry inq; 9608 sata_device_t sata_device; 9609 sata_drive_info_t *sdinfo; 9610 int target; 9611 int i; 9612 9613 sata_device.satadev_rev = SATA_DEVICE_REV; 9614 sata_device.satadev_addr = *sata_addr; 9615 9616 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport))); 9617 9618 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 9619 9620 target = SATA_TO_SCSI_TARGET(sata_addr->cport, 9621 sata_addr->pmport, sata_addr->qual); 9622 9623 if (sdinfo == NULL) { 9624 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9625 sata_addr->cport))); 9626 SATA_LOG_D((sata_hba_inst, CE_WARN, 9627 "sata_create_target_node: no sdinfo for target %x", 9628 target)); 9629 return (NULL); 9630 } 9631 9632 /* 9633 * create or get scsi inquiry data, expected by 9634 * scsi_hba_nodename_compatible_get() 9635 * SATA hard disks get Identify Data translated into Inguiry Data. 9636 * ATAPI devices respond directly to Inquiry request. 9637 */ 9638 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 9639 sata_identdev_to_inquiry(sata_hba_inst, sdinfo, 9640 (uint8_t *)&inq); 9641 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9642 sata_addr->cport))); 9643 } else { /* Assume supported ATAPI device */ 9644 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9645 sata_addr->cport))); 9646 if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr, 9647 &inq) == SATA_FAILURE) 9648 return (NULL); 9649 /* 9650 * Save supported ATAPI transport version 9651 */ 9652 sdinfo->satadrv_atapi_trans_ver = 9653 SATA_ATAPI_TRANS_VERSION(&inq); 9654 } 9655 9656 /* determine the node name and compatible */ 9657 scsi_hba_nodename_compatible_get(&inq, NULL, 9658 inq.inq_dtype, NULL, &nname, &compatible, &ncompatible); 9659 9660 #ifdef SATA_DEBUG 9661 if (sata_debug_flags & SATA_DBG_NODES) { 9662 if (nname == NULL) { 9663 cmn_err(CE_NOTE, "sata_create_target_node: " 9664 "cannot determine nodename for target %d\n", 9665 target); 9666 } else { 9667 cmn_err(CE_WARN, "sata_create_target_node: " 9668 "target %d nodename: %s\n", target, nname); 9669 } 9670 if (compatible == NULL) { 9671 cmn_err(CE_WARN, 9672 "sata_create_target_node: no compatible name\n"); 9673 } else { 9674 for (i = 0; i < ncompatible; i++) { 9675 cmn_err(CE_WARN, "sata_create_target_node: " 9676 "compatible name: %s\n", compatible[i]); 9677 } 9678 } 9679 } 9680 #endif 9681 9682 /* if nodename can't be determined, log error and exit */ 9683 if (nname == NULL) { 9684 SATA_LOG_D((sata_hba_inst, CE_WARN, 9685 "sata_create_target_node: cannot determine nodename " 9686 "for target %d\n", target)); 9687 scsi_hba_nodename_compatible_free(nname, compatible); 9688 return (NULL); 9689 } 9690 /* 9691 * Create scsi target node 9692 */ 9693 ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip); 9694 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 9695 "device-type", "scsi"); 9696 9697 if (rval != DDI_PROP_SUCCESS) { 9698 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 9699 "updating device_type prop failed %d", rval)); 9700 goto fail; 9701 } 9702 9703 /* 9704 * Create target node properties: target & lun 9705 */ 9706 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target); 9707 if (rval != DDI_PROP_SUCCESS) { 9708 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 9709 "updating target prop failed %d", rval)); 9710 goto fail; 9711 } 9712 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0); 9713 if (rval != DDI_PROP_SUCCESS) { 9714 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 9715 "updating target prop failed %d", rval)); 9716 goto fail; 9717 } 9718 9719 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 9720 /* 9721 * Add "variant" property 9722 */ 9723 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 9724 "variant", "atapi"); 9725 if (rval != DDI_PROP_SUCCESS) { 9726 SATA_LOG_D((sata_hba_inst, CE_WARN, 9727 "sata_create_target_node: variant atapi " 9728 "property could not be created: %d", rval)); 9729 goto fail; 9730 } 9731 } 9732 /* decorate the node with compatible */ 9733 if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible", 9734 compatible, ncompatible) != DDI_PROP_SUCCESS) { 9735 SATA_LOG_D((sata_hba_inst, CE_WARN, 9736 "sata_create_target_node: FAIL compatible props cdip 0x%p", 9737 (void *)cdip)); 9738 goto fail; 9739 } 9740 9741 9742 /* 9743 * Now, try to attach the driver. If probing of the device fails, 9744 * the target node may be removed 9745 */ 9746 rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH); 9747 9748 scsi_hba_nodename_compatible_free(nname, compatible); 9749 9750 if (rval == NDI_SUCCESS) 9751 return (cdip); 9752 9753 /* target node was removed - are we sure? */ 9754 return (NULL); 9755 9756 fail: 9757 scsi_hba_nodename_compatible_free(nname, compatible); 9758 ddi_prop_remove_all(cdip); 9759 rval = ndi_devi_free(cdip); 9760 if (rval != NDI_SUCCESS) { 9761 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 9762 "node removal failed %d", rval)); 9763 } 9764 sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: " 9765 "cannot create target node for SATA device at port %d", 9766 sata_addr->cport); 9767 return (NULL); 9768 } 9769 9770 /* 9771 * Remove a target node. 9772 */ 9773 static void 9774 sata_remove_target_node(sata_hba_inst_t *sata_hba_inst, 9775 sata_address_t *sata_addr) 9776 { 9777 dev_info_t *tdip; 9778 uint8_t cport = sata_addr->cport; 9779 uint8_t pmport = sata_addr->pmport; 9780 uint8_t qual = sata_addr->qual; 9781 9782 /* Note the sata daemon uses the address of the port/pmport */ 9783 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT); 9784 9785 /* Remove target node */ 9786 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), cport, pmport); 9787 if (tdip != NULL) { 9788 /* 9789 * Target node exists. Unconfigure device 9790 * then remove the target node (one ndi 9791 * operation). 9792 */ 9793 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) { 9794 /* 9795 * PROBLEM - no device, but target node remained. This 9796 * happens when the file was open or node was waiting 9797 * for resources. 9798 */ 9799 SATA_LOG_D((sata_hba_inst, CE_WARN, 9800 "sata_remove_target_node: " 9801 "Failed to remove target node for " 9802 "detached SATA device.")); 9803 /* 9804 * Set target node state to DEVI_DEVICE_REMOVED. But 9805 * re-check first that the node still exists. 9806 */ 9807 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 9808 cport, pmport); 9809 if (tdip != NULL) { 9810 sata_set_device_removed(tdip); 9811 /* 9812 * Instruct event daemon to retry the cleanup 9813 * later. 9814 */ 9815 sata_set_target_node_cleanup(sata_hba_inst, 9816 sata_addr); 9817 } 9818 } 9819 9820 if (qual == SATA_ADDR_CPORT) 9821 sata_log(sata_hba_inst, CE_WARN, 9822 "SATA device detached at port %d", cport); 9823 else 9824 sata_log(sata_hba_inst, CE_WARN, 9825 "SATA device detached at port %d:%d", 9826 cport, pmport); 9827 } 9828 #ifdef SATA_DEBUG 9829 else { 9830 if (qual == SATA_ADDR_CPORT) 9831 sata_log(sata_hba_inst, CE_WARN, 9832 "target node not found at port %d", cport); 9833 else 9834 sata_log(sata_hba_inst, CE_WARN, 9835 "target node not found at port %d:%d", 9836 cport, pmport); 9837 } 9838 #endif 9839 } 9840 9841 9842 /* 9843 * Re-probe sata port, check for a device and attach info 9844 * structures when necessary. Identify Device data is fetched, if possible. 9845 * Assumption: sata address is already validated. 9846 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of 9847 * the presence of a device and its type. 9848 * 9849 * flag arg specifies that the function should try multiple times to identify 9850 * device type and to initialize it, or it should return immediately on failure. 9851 * SATA_DEV_IDENTIFY_RETRY - retry 9852 * SATA_DEV_IDENTIFY_NORETRY - no retry 9853 * 9854 * SATA_FAILURE is returned if one of the operations failed. 9855 * 9856 * This function cannot be called in interrupt context - it may sleep. 9857 * 9858 * Note: Port multiplier is supported. 9859 */ 9860 static int 9861 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 9862 int flag) 9863 { 9864 sata_cport_info_t *cportinfo; 9865 sata_pmult_info_t *pmultinfo; 9866 sata_drive_info_t *sdinfo, *osdinfo; 9867 boolean_t init_device = B_FALSE; 9868 int prev_device_type = SATA_DTYPE_NONE; 9869 int prev_device_settings = 0; 9870 int prev_device_state = 0; 9871 clock_t start_time; 9872 int retry = B_FALSE; 9873 uint8_t cport = sata_device->satadev_addr.cport; 9874 int rval_probe, rval_init; 9875 9876 /* 9877 * If target is pmport, sata_reprobe_pmport() will handle it. 9878 */ 9879 if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT || 9880 sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) 9881 return (sata_reprobe_pmport(sata_hba_inst, sata_device, flag)); 9882 9883 /* We only care about host sata cport for now */ 9884 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 9885 sata_device->satadev_addr.cport); 9886 9887 /* 9888 * If a port multiplier was previously attached (we have no idea it 9889 * still there or not), sata_reprobe_pmult() will handle it. 9890 */ 9891 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) 9892 return (sata_reprobe_pmult(sata_hba_inst, sata_device, flag)); 9893 9894 /* Store sata_drive_info when a non-pmult device was attached. */ 9895 osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 9896 if (osdinfo != NULL) { 9897 /* 9898 * We are re-probing port with a previously attached device. 9899 * Save previous device type and settings. 9900 */ 9901 prev_device_type = cportinfo->cport_dev_type; 9902 prev_device_settings = osdinfo->satadrv_settings; 9903 prev_device_state = osdinfo->satadrv_state; 9904 } 9905 if (flag == SATA_DEV_IDENTIFY_RETRY) { 9906 start_time = ddi_get_lbolt(); 9907 retry = B_TRUE; 9908 } 9909 retry_probe: 9910 9911 /* probe port */ 9912 mutex_enter(&cportinfo->cport_mutex); 9913 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 9914 cportinfo->cport_state |= SATA_STATE_PROBING; 9915 mutex_exit(&cportinfo->cport_mutex); 9916 9917 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 9918 (SATA_DIP(sata_hba_inst), sata_device); 9919 9920 mutex_enter(&cportinfo->cport_mutex); 9921 if (rval_probe != SATA_SUCCESS) { 9922 cportinfo->cport_state = SATA_PSTATE_FAILED; 9923 mutex_exit(&cportinfo->cport_mutex); 9924 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: " 9925 "SATA port %d probing failed", 9926 cportinfo->cport_addr.cport)); 9927 return (SATA_FAILURE); 9928 } 9929 9930 /* 9931 * update sata port state and set device type 9932 */ 9933 sata_update_port_info(sata_hba_inst, sata_device); 9934 cportinfo->cport_state &= ~SATA_STATE_PROBING; 9935 9936 /* 9937 * Sanity check - Port is active? Is the link active? 9938 * Is there any device attached? 9939 */ 9940 if ((cportinfo->cport_state & 9941 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 9942 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 9943 SATA_PORT_DEVLINK_UP) { 9944 /* 9945 * Port in non-usable state or no link active/no device. 9946 * Free info structure if necessary (direct attached drive 9947 * only, for now! 9948 */ 9949 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 9950 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 9951 /* Add here differentiation for device attached or not */ 9952 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 9953 mutex_exit(&cportinfo->cport_mutex); 9954 if (sdinfo != NULL) 9955 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 9956 return (SATA_SUCCESS); 9957 } 9958 9959 cportinfo->cport_state |= SATA_STATE_READY; 9960 cportinfo->cport_state |= SATA_STATE_PROBED; 9961 9962 cportinfo->cport_dev_type = sata_device->satadev_type; 9963 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 9964 9965 /* 9966 * If we are re-probing the port, there may be 9967 * sata_drive_info structure attached 9968 */ 9969 if (sata_device->satadev_type == SATA_DTYPE_NONE) { 9970 9971 /* 9972 * There is no device, so remove device info structure, 9973 * if necessary. 9974 */ 9975 /* Device change: Drive -> None */ 9976 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 9977 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 9978 if (sdinfo != NULL) { 9979 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 9980 sata_log(sata_hba_inst, CE_WARN, 9981 "SATA device detached " 9982 "from port %d", cportinfo->cport_addr.cport); 9983 } 9984 mutex_exit(&cportinfo->cport_mutex); 9985 return (SATA_SUCCESS); 9986 9987 } 9988 9989 if (sata_device->satadev_type != SATA_DTYPE_PMULT) { 9990 9991 /* Device (may) change: Drive -> Drive */ 9992 if (sdinfo == NULL) { 9993 /* 9994 * There is some device attached, but there is 9995 * no sata_drive_info structure - allocate one 9996 */ 9997 mutex_exit(&cportinfo->cport_mutex); 9998 sdinfo = kmem_zalloc( 9999 sizeof (sata_drive_info_t), KM_SLEEP); 10000 mutex_enter(&cportinfo->cport_mutex); 10001 /* 10002 * Recheck, that the port state did not change when we 10003 * released mutex. 10004 */ 10005 if (cportinfo->cport_state & SATA_STATE_READY) { 10006 SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo; 10007 sdinfo->satadrv_addr = cportinfo->cport_addr; 10008 sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT; 10009 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 10010 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 10011 } else { 10012 /* 10013 * Port is not in ready state, we 10014 * cannot attach a device. 10015 */ 10016 mutex_exit(&cportinfo->cport_mutex); 10017 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10018 return (SATA_SUCCESS); 10019 } 10020 /* 10021 * Since we are adding device, presumably new one, 10022 * indicate that it should be initalized, 10023 * as well as some internal framework states). 10024 */ 10025 init_device = B_TRUE; 10026 } 10027 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 10028 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual; 10029 } else { 10030 /* Device change: Drive -> PMult */ 10031 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 10032 if (sdinfo != NULL) { 10033 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10034 sata_log(sata_hba_inst, CE_WARN, 10035 "SATA device detached " 10036 "from port %d", cportinfo->cport_addr.cport); 10037 } 10038 10039 sata_log(sata_hba_inst, CE_WARN, 10040 "SATA port multiplier detected at port %d", 10041 cportinfo->cport_addr.cport); 10042 10043 mutex_exit(&cportinfo->cport_mutex); 10044 sata_alloc_pmult(sata_hba_inst, sata_device); 10045 sata_show_pmult_info(sata_hba_inst, sata_device); 10046 mutex_enter(&cportinfo->cport_mutex); 10047 10048 /* 10049 * Mark all the port multiplier port behind the port 10050 * multiplier behind with link events, so that the sata daemon 10051 * will update their status. 10052 */ 10053 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 10054 pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET; 10055 mutex_exit(&cportinfo->cport_mutex); 10056 return (SATA_SUCCESS); 10057 } 10058 mutex_exit(&cportinfo->cport_mutex); 10059 10060 /* 10061 * Figure out what kind of device we are really 10062 * dealing with. Failure of identifying device does not fail this 10063 * function. 10064 */ 10065 rval_probe = sata_probe_device(sata_hba_inst, sata_device); 10066 rval_init = SATA_FAILURE; 10067 mutex_enter(&cportinfo->cport_mutex); 10068 if (rval_probe == SATA_SUCCESS) { 10069 /* 10070 * If we are dealing with the same type of a device as before, 10071 * restore its settings flags. 10072 */ 10073 if (osdinfo != NULL && 10074 sata_device->satadev_type == prev_device_type) 10075 sdinfo->satadrv_settings = prev_device_settings; 10076 10077 mutex_exit(&cportinfo->cport_mutex); 10078 rval_init = SATA_SUCCESS; 10079 /* Set initial device features, if necessary */ 10080 if (init_device == B_TRUE) { 10081 rval_init = sata_initialize_device(sata_hba_inst, 10082 sdinfo); 10083 } 10084 if (rval_init == SATA_SUCCESS) 10085 return (rval_init); 10086 /* else we will retry if retry was asked for */ 10087 10088 } else { 10089 /* 10090 * If there was some device info before we probe the device, 10091 * restore previous device setting, so we can retry from scratch 10092 * later. Providing, of course, that device has not disapear 10093 * during probing process. 10094 */ 10095 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 10096 if (osdinfo != NULL) { 10097 cportinfo->cport_dev_type = prev_device_type; 10098 sdinfo->satadrv_type = prev_device_type; 10099 sdinfo->satadrv_state = prev_device_state; 10100 } 10101 } else { 10102 /* device is gone */ 10103 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10104 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 10105 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 10106 mutex_exit(&cportinfo->cport_mutex); 10107 return (SATA_SUCCESS); 10108 } 10109 mutex_exit(&cportinfo->cport_mutex); 10110 } 10111 10112 if (retry) { 10113 clock_t cur_time = ddi_get_lbolt(); 10114 /* 10115 * A device was not successfully identified or initialized. 10116 * Track retry time for device identification. 10117 */ 10118 if ((cur_time - start_time) < 10119 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 10120 /* sleep for a while */ 10121 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 10122 goto retry_probe; 10123 } 10124 /* else no more retries */ 10125 mutex_enter(&cportinfo->cport_mutex); 10126 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 10127 if (rval_init == SATA_RETRY) { 10128 /* 10129 * Setting drive features have failed, but 10130 * because the drive is still accessible, 10131 * keep it and emit a warning message. 10132 */ 10133 sata_log(sata_hba_inst, CE_WARN, 10134 "SATA device at port %d - desired " 10135 "drive features could not be set. " 10136 "Device may not operate as expected.", 10137 cportinfo->cport_addr.cport); 10138 } else { 10139 SATA_CPORTINFO_DRV_INFO(cportinfo)-> 10140 satadrv_state = SATA_DSTATE_FAILED; 10141 } 10142 } 10143 mutex_exit(&cportinfo->cport_mutex); 10144 } 10145 return (SATA_SUCCESS); 10146 } 10147 10148 /* 10149 * Reprobe a controller port that connected to a port multiplier. 10150 * 10151 * NOTE: No Mutex should be hold. 10152 */ 10153 static int 10154 sata_reprobe_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 10155 int flag) 10156 { 10157 _NOTE(ARGUNUSED(flag)) 10158 sata_cport_info_t *cportinfo; 10159 sata_pmult_info_t *pmultinfo; 10160 uint8_t cport = sata_device->satadev_addr.cport; 10161 int rval_probe; 10162 10163 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 10164 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 10165 10166 /* probe port */ 10167 mutex_enter(&cportinfo->cport_mutex); 10168 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 10169 cportinfo->cport_state |= SATA_STATE_PROBING; 10170 mutex_exit(&cportinfo->cport_mutex); 10171 10172 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 10173 (SATA_DIP(sata_hba_inst), sata_device); 10174 10175 mutex_enter(&cportinfo->cport_mutex); 10176 if (rval_probe != SATA_SUCCESS) { 10177 cportinfo->cport_state = SATA_PSTATE_FAILED; 10178 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmult: " 10179 "SATA port %d probing failed", cport)); 10180 sata_log(sata_hba_inst, CE_WARN, 10181 "SATA port multiplier detached at port %d", cport); 10182 mutex_exit(&cportinfo->cport_mutex); 10183 sata_free_pmult(sata_hba_inst, sata_device); 10184 return (SATA_FAILURE); 10185 } 10186 10187 /* 10188 * update sata port state and set device type 10189 */ 10190 sata_update_port_info(sata_hba_inst, sata_device); 10191 cportinfo->cport_state &= ~SATA_STATE_PROBING; 10192 cportinfo->cport_state |= SATA_STATE_PROBED; 10193 10194 /* 10195 * Sanity check - Port is active? Is the link active? 10196 * Is there any device attached? 10197 */ 10198 if ((cportinfo->cport_state & 10199 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 10200 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 10201 SATA_PORT_DEVLINK_UP || 10202 (sata_device->satadev_type == SATA_DTYPE_NONE)) { 10203 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 10204 mutex_exit(&cportinfo->cport_mutex); 10205 sata_free_pmult(sata_hba_inst, sata_device); 10206 sata_log(sata_hba_inst, CE_WARN, 10207 "SATA port multiplier detached at port %d", cport); 10208 return (SATA_SUCCESS); 10209 } 10210 10211 /* 10212 * Device changed: PMult -> Non-PMult 10213 * 10214 * This situation is uncommon, most possibly being caused by errors 10215 * after which the port multiplier is not correct initialized and 10216 * recognized. In that case the new device will be marked as unknown 10217 * and will not be automatically probed in this routine. Instead 10218 * system administrator could manually restart it via cfgadm(1M). 10219 */ 10220 if (sata_device->satadev_type != SATA_DTYPE_PMULT) { 10221 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 10222 mutex_exit(&cportinfo->cport_mutex); 10223 sata_free_pmult(sata_hba_inst, sata_device); 10224 sata_log(sata_hba_inst, CE_WARN, 10225 "SATA port multiplier detached at port %d", cport); 10226 return (SATA_FAILURE); 10227 } 10228 10229 /* 10230 * Now we know it is a port multiplier. However, if this is not the 10231 * previously attached port multiplier - they may have different 10232 * pmport numbers - we need to re-allocate data structures for every 10233 * pmport and drive. 10234 * 10235 * Port multipliers of the same model have identical values in these 10236 * registers, so it is still necessary to update the information of 10237 * all drives attached to the previous port multiplier afterwards. 10238 */ 10239 if ((sata_device->satadev_gscr.gscr0 != pmultinfo->pmult_gscr.gscr0) || 10240 (sata_device->satadev_gscr.gscr1 != pmultinfo->pmult_gscr.gscr1) || 10241 (sata_device->satadev_gscr.gscr2 != pmultinfo->pmult_gscr.gscr2)) { 10242 10243 /* Device changed: PMult -> another PMult */ 10244 mutex_exit(&cportinfo->cport_mutex); 10245 sata_free_pmult(sata_hba_inst, sata_device); 10246 sata_alloc_pmult(sata_hba_inst, sata_device); 10247 mutex_enter(&cportinfo->cport_mutex); 10248 10249 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 10250 "SATA port multiplier [changed] at port %d", cport); 10251 sata_log(sata_hba_inst, CE_WARN, 10252 "SATA port multiplier detected at port %d", cport); 10253 } 10254 10255 /* 10256 * Mark all the port multiplier port behind the port 10257 * multiplier behind with link events, so that the sata daemon 10258 * will update their status. 10259 */ 10260 pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET; 10261 mutex_exit(&cportinfo->cport_mutex); 10262 10263 return (SATA_SUCCESS); 10264 } 10265 10266 /* 10267 * Re-probe a port multiplier port, check for a device and attach info 10268 * structures when necessary. Identify Device data is fetched, if possible. 10269 * Assumption: sata address is already validated as port multiplier port. 10270 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of 10271 * the presence of a device and its type. 10272 * 10273 * flag arg specifies that the function should try multiple times to identify 10274 * device type and to initialize it, or it should return immediately on failure. 10275 * SATA_DEV_IDENTIFY_RETRY - retry 10276 * SATA_DEV_IDENTIFY_NORETRY - no retry 10277 * 10278 * SATA_FAILURE is returned if one of the operations failed. 10279 * 10280 * This function cannot be called in interrupt context - it may sleep. 10281 * 10282 * NOTE: Should be only called by sata_probe_port() in case target port is a 10283 * port multiplier port. 10284 * NOTE: No Mutex should be hold. 10285 */ 10286 static int 10287 sata_reprobe_pmport(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 10288 int flag) 10289 { 10290 sata_cport_info_t *cportinfo = NULL; 10291 sata_pmport_info_t *pmportinfo = NULL; 10292 sata_drive_info_t *sdinfo, *osdinfo; 10293 sata_device_t sdevice; 10294 boolean_t init_device = B_FALSE; 10295 int prev_device_type = SATA_DTYPE_NONE; 10296 int prev_device_settings = 0; 10297 int prev_device_state = 0; 10298 clock_t start_time; 10299 uint8_t cport = sata_device->satadev_addr.cport; 10300 uint8_t pmport = sata_device->satadev_addr.pmport; 10301 int rval; 10302 10303 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 10304 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 10305 osdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 10306 10307 if (osdinfo != NULL) { 10308 /* 10309 * We are re-probing port with a previously attached device. 10310 * Save previous device type and settings. 10311 */ 10312 prev_device_type = pmportinfo->pmport_dev_type; 10313 prev_device_settings = osdinfo->satadrv_settings; 10314 prev_device_state = osdinfo->satadrv_state; 10315 } 10316 10317 start_time = ddi_get_lbolt(); 10318 10319 /* check parent status */ 10320 mutex_enter(&cportinfo->cport_mutex); 10321 if ((cportinfo->cport_state & 10322 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 10323 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 10324 SATA_PORT_DEVLINK_UP) { 10325 mutex_exit(&cportinfo->cport_mutex); 10326 return (SATA_FAILURE); 10327 } 10328 mutex_exit(&cportinfo->cport_mutex); 10329 10330 retry_probe_pmport: 10331 10332 /* probe port */ 10333 mutex_enter(&pmportinfo->pmport_mutex); 10334 pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 10335 pmportinfo->pmport_state |= SATA_STATE_PROBING; 10336 mutex_exit(&pmportinfo->pmport_mutex); 10337 10338 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 10339 (SATA_DIP(sata_hba_inst), sata_device); 10340 10341 /* might need retry because we cannot touch registers. */ 10342 if (rval == SATA_FAILURE) { 10343 mutex_enter(&pmportinfo->pmport_mutex); 10344 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 10345 mutex_exit(&pmportinfo->pmport_mutex); 10346 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: " 10347 "SATA port %d:%d probing failed", 10348 cport, pmport)); 10349 return (SATA_FAILURE); 10350 } else if (rval == SATA_RETRY) { 10351 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: " 10352 "SATA port %d:%d probing failed, retrying...", 10353 cport, pmport)); 10354 clock_t cur_time = ddi_get_lbolt(); 10355 /* 10356 * A device was not successfully identified or initialized. 10357 * Track retry time for device identification. 10358 */ 10359 if ((cur_time - start_time) < 10360 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 10361 /* sleep for a while */ 10362 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 10363 goto retry_probe_pmport; 10364 } else { 10365 mutex_enter(&pmportinfo->pmport_mutex); 10366 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) 10367 SATA_PMPORTINFO_DRV_INFO(pmportinfo)-> 10368 satadrv_state = SATA_DSTATE_FAILED; 10369 mutex_exit(&pmportinfo->pmport_mutex); 10370 return (SATA_SUCCESS); 10371 } 10372 } 10373 10374 /* 10375 * Sanity check - Controller port is active? Is the link active? 10376 * Is it still a port multiplier? 10377 */ 10378 if ((cportinfo->cport_state & 10379 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 10380 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 10381 SATA_PORT_DEVLINK_UP || 10382 (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) { 10383 /* 10384 * Port in non-usable state or no link active/no 10385 * device. Free info structure. 10386 */ 10387 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 10388 10389 sdevice.satadev_addr.cport = cport; 10390 sdevice.satadev_addr.pmport = pmport; 10391 sdevice.satadev_addr.qual = SATA_ADDR_PMULT; 10392 mutex_exit(&cportinfo->cport_mutex); 10393 10394 sata_free_pmult(sata_hba_inst, &sdevice); 10395 return (SATA_FAILURE); 10396 } 10397 10398 /* SATA_SUCCESS NOW */ 10399 /* 10400 * update sata port state and set device type 10401 */ 10402 mutex_enter(&pmportinfo->pmport_mutex); 10403 sata_update_pmport_info(sata_hba_inst, sata_device); 10404 pmportinfo->pmport_state &= ~SATA_STATE_PROBING; 10405 10406 /* 10407 * Sanity check - Port is active? Is the link active? 10408 * Is there any device attached? 10409 */ 10410 if ((pmportinfo->pmport_state & 10411 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 10412 (pmportinfo->pmport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 10413 SATA_PORT_DEVLINK_UP) { 10414 /* 10415 * Port in non-usable state or no link active/no device. 10416 * Free info structure if necessary (direct attached drive 10417 * only, for now! 10418 */ 10419 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 10420 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 10421 /* Add here differentiation for device attached or not */ 10422 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 10423 mutex_exit(&pmportinfo->pmport_mutex); 10424 if (sdinfo != NULL) 10425 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10426 return (SATA_SUCCESS); 10427 } 10428 10429 pmportinfo->pmport_state |= SATA_STATE_READY; 10430 pmportinfo->pmport_dev_type = sata_device->satadev_type; 10431 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 10432 10433 /* 10434 * If we are re-probing the port, there may be 10435 * sata_drive_info structure attached 10436 * (or sata_pm_info, if PMult is supported). 10437 */ 10438 if (sata_device->satadev_type == SATA_DTYPE_NONE) { 10439 /* 10440 * There is no device, so remove device info structure, 10441 * if necessary. 10442 */ 10443 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 10444 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 10445 if (sdinfo != NULL) { 10446 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10447 sata_log(sata_hba_inst, CE_WARN, 10448 "SATA device detached from port %d:%d", 10449 cport, pmport); 10450 } 10451 mutex_exit(&pmportinfo->pmport_mutex); 10452 return (SATA_SUCCESS); 10453 } 10454 10455 /* this should not be a pmult */ 10456 ASSERT(sata_device->satadev_type != SATA_DTYPE_PMULT); 10457 if (sdinfo == NULL) { 10458 /* 10459 * There is some device attached, but there is 10460 * no sata_drive_info structure - allocate one 10461 */ 10462 mutex_exit(&pmportinfo->pmport_mutex); 10463 sdinfo = kmem_zalloc(sizeof (sata_drive_info_t), 10464 KM_SLEEP); 10465 mutex_enter(&pmportinfo->pmport_mutex); 10466 /* 10467 * Recheck, that the port state did not change when we 10468 * released mutex. 10469 */ 10470 if (pmportinfo->pmport_state & SATA_STATE_READY) { 10471 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = sdinfo; 10472 sdinfo->satadrv_addr = pmportinfo->pmport_addr; 10473 sdinfo->satadrv_addr.qual = SATA_ADDR_DPMPORT; 10474 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 10475 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 10476 } else { 10477 /* 10478 * Port is not in ready state, we 10479 * cannot attach a device. 10480 */ 10481 mutex_exit(&pmportinfo->pmport_mutex); 10482 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10483 return (SATA_SUCCESS); 10484 } 10485 /* 10486 * Since we are adding device, presumably new one, 10487 * indicate that it should be initalized, 10488 * as well as some internal framework states). 10489 */ 10490 init_device = B_TRUE; 10491 } 10492 10493 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN; 10494 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual; 10495 10496 mutex_exit(&pmportinfo->pmport_mutex); 10497 /* 10498 * Figure out what kind of device we are really 10499 * dealing with. 10500 */ 10501 rval = sata_probe_device(sata_hba_inst, sata_device); 10502 10503 mutex_enter(&pmportinfo->pmport_mutex); 10504 if (rval == SATA_SUCCESS) { 10505 /* 10506 * If we are dealing with the same type of a device as before, 10507 * restore its settings flags. 10508 */ 10509 if (osdinfo != NULL && 10510 sata_device->satadev_type == prev_device_type) 10511 sdinfo->satadrv_settings = prev_device_settings; 10512 10513 mutex_exit(&pmportinfo->pmport_mutex); 10514 /* Set initial device features, if necessary */ 10515 if (init_device == B_TRUE) { 10516 rval = sata_initialize_device(sata_hba_inst, sdinfo); 10517 } 10518 if (rval == SATA_SUCCESS) 10519 return (rval); 10520 } else { 10521 /* 10522 * If there was some device info before we probe the device, 10523 * restore previous device setting, so we can retry from scratch 10524 * later. Providing, of course, that device has not disappeared 10525 * during probing process. 10526 */ 10527 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 10528 if (osdinfo != NULL) { 10529 pmportinfo->pmport_dev_type = prev_device_type; 10530 sdinfo->satadrv_type = prev_device_type; 10531 sdinfo->satadrv_state = prev_device_state; 10532 } 10533 } else { 10534 /* device is gone */ 10535 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10536 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 10537 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 10538 mutex_exit(&pmportinfo->pmport_mutex); 10539 return (SATA_SUCCESS); 10540 } 10541 mutex_exit(&pmportinfo->pmport_mutex); 10542 } 10543 10544 if (flag == SATA_DEV_IDENTIFY_RETRY) { 10545 clock_t cur_time = ddi_get_lbolt(); 10546 /* 10547 * A device was not successfully identified or initialized. 10548 * Track retry time for device identification. 10549 */ 10550 if ((cur_time - start_time) < 10551 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 10552 /* sleep for a while */ 10553 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 10554 goto retry_probe_pmport; 10555 } else { 10556 mutex_enter(&pmportinfo->pmport_mutex); 10557 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) 10558 SATA_PMPORTINFO_DRV_INFO(pmportinfo)-> 10559 satadrv_state = SATA_DSTATE_FAILED; 10560 mutex_exit(&pmportinfo->pmport_mutex); 10561 } 10562 } 10563 return (SATA_SUCCESS); 10564 } 10565 10566 /* 10567 * Allocated related structure for a port multiplier and its device ports 10568 * 10569 * Port multiplier should be ready and probed, and related information like 10570 * the number of the device ports should be store in sata_device_t. 10571 * 10572 * NOTE: No Mutex should be hold. 10573 */ 10574 static void 10575 sata_alloc_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 10576 { 10577 dev_info_t *dip = SATA_DIP(sata_hba_inst); 10578 sata_cport_info_t *cportinfo = NULL; 10579 sata_pmult_info_t *pmultinfo = NULL; 10580 sata_pmport_info_t *pmportinfo = NULL; 10581 dev_t minor_number; 10582 char name[16]; 10583 uint8_t cport = sata_device->satadev_addr.cport; 10584 int npmport; 10585 10586 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 10587 10588 /* This function might be called while a port-mult is hot-plugged. */ 10589 mutex_enter(&cportinfo->cport_mutex); 10590 10591 /* dev_type's not updated when get called from sata_reprobe_port() */ 10592 cportinfo->cport_dev_type = SATA_DTYPE_PMULT; 10593 if (SATA_CPORTINFO_PMULT_INFO(cportinfo) == NULL) { 10594 /* Create a pmult_info structure */ 10595 SATA_CPORTINFO_PMULT_INFO(cportinfo) = 10596 kmem_zalloc(sizeof (sata_pmult_info_t), KM_SLEEP); 10597 } 10598 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 10599 10600 pmultinfo->pmult_addr = sata_device->satadev_addr; 10601 pmultinfo->pmult_addr.qual = SATA_ADDR_PMULT; 10602 pmultinfo->pmult_state = SATA_STATE_PROBING; 10603 pmultinfo->pmult_gscr = sata_device->satadev_gscr; 10604 pmultinfo->pmult_num_dev_ports = sata_device->satadev_add_info; 10605 10606 /* Initialize pmport_info structure */ 10607 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; 10608 npmport++) { 10609 10610 /* if everything is allocated, skip */ 10611 if (SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) != NULL) 10612 continue; 10613 10614 pmportinfo = kmem_zalloc(sizeof (sata_pmport_info_t), KM_SLEEP); 10615 mutex_init(&pmportinfo->pmport_mutex, NULL, MUTEX_DRIVER, NULL); 10616 mutex_exit(&cportinfo->cport_mutex); 10617 10618 mutex_enter(&pmportinfo->pmport_mutex); 10619 pmportinfo->pmport_addr.cport = cport; 10620 pmportinfo->pmport_addr.pmport = (uint8_t)npmport; 10621 pmportinfo->pmport_addr.qual = SATA_ADDR_PMPORT; 10622 pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 10623 mutex_exit(&pmportinfo->pmport_mutex); 10624 10625 mutex_enter(&cportinfo->cport_mutex); 10626 SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) = pmportinfo; 10627 10628 /* Create an attachment point */ 10629 minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip), 10630 cport, (uint8_t)npmport, SATA_ADDR_PMPORT); 10631 (void) sprintf(name, "%d.%d", cport, npmport); 10632 10633 if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number, 10634 DDI_NT_SATA_ATTACHMENT_POINT, 0) != DDI_SUCCESS) { 10635 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: " 10636 "cannot create SATA attachment point for " 10637 "port %d:%d", cport, npmport); 10638 } 10639 } 10640 10641 pmultinfo->pmult_state &= ~SATA_STATE_PROBING; 10642 pmultinfo->pmult_state |= (SATA_STATE_PROBED|SATA_STATE_READY); 10643 10644 mutex_exit(&cportinfo->cport_mutex); 10645 } 10646 10647 /* 10648 * Free data structures when a port multiplier is removed. 10649 * 10650 * NOTE: No Mutex should be hold. 10651 */ 10652 static void 10653 sata_free_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 10654 { 10655 sata_cport_info_t *cportinfo; 10656 sata_pmult_info_t *pmultinfo; 10657 sata_pmport_info_t *pmportinfo; 10658 sata_device_t pmport_device; 10659 sata_drive_info_t *sdinfo; 10660 dev_info_t *tdip; 10661 char name[16]; 10662 uint8_t cport = sata_device->satadev_addr.cport; 10663 int npmport; 10664 10665 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 10666 10667 /* This function might be called while port-mult is hot plugged. */ 10668 mutex_enter(&cportinfo->cport_mutex); 10669 10670 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 10671 ASSERT(pmultinfo != NULL); 10672 10673 /* Free pmport_info structure */ 10674 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; 10675 npmport++) { 10676 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport); 10677 if (pmportinfo == NULL) 10678 continue; 10679 mutex_exit(&cportinfo->cport_mutex); 10680 10681 mutex_enter(&pmportinfo->pmport_mutex); 10682 sdinfo = pmportinfo->pmport_sata_drive; 10683 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 10684 mutex_exit(&pmportinfo->pmport_mutex); 10685 10686 /* Remove attachment point. */ 10687 name[0] = '\0'; 10688 (void) sprintf(name, "%d.%d", cport, npmport); 10689 ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name); 10690 sata_log(sata_hba_inst, CE_NOTE, 10691 "Remove attachment point of port %d:%d", 10692 cport, npmport); 10693 10694 /* 10695 * Rumove target node 10696 */ 10697 bzero(&pmport_device, sizeof (sata_device_t)); 10698 pmport_device.satadev_rev = SATA_DEVICE_REV; 10699 pmport_device.satadev_addr.cport = cport; 10700 pmport_device.satadev_addr.pmport = (uint8_t)npmport; 10701 pmport_device.satadev_addr.qual = SATA_ADDR_DPMPORT; 10702 10703 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 10704 &(pmport_device.satadev_addr)); 10705 if (tdip != NULL && ndi_devi_offline(tdip, 10706 NDI_DEVI_REMOVE) != NDI_SUCCESS) { 10707 /* 10708 * Problem : 10709 * The target node remained attached. 10710 * This happens when the device file was open 10711 * or a node was waiting for resources. 10712 * Cannot do anything about it. 10713 */ 10714 SATA_LOG_D((sata_hba_inst, CE_WARN, 10715 "sata_free_pmult: could not unconfigure device " 10716 "before disconnecting the SATA port %d:%d", 10717 cport, npmport)); 10718 10719 /* 10720 * Set DEVICE REMOVED state in the target 10721 * node. It will prevent access to the device 10722 * even when a new device is attached, until 10723 * the old target node is released, removed and 10724 * recreated for a new device. 10725 */ 10726 sata_set_device_removed(tdip); 10727 10728 /* 10729 * Instruct event daemon to try the target 10730 * node cleanup later. 10731 */ 10732 sata_set_target_node_cleanup( 10733 sata_hba_inst, &(pmport_device.satadev_addr)); 10734 10735 } 10736 mutex_enter(&cportinfo->cport_mutex); 10737 10738 /* 10739 * Add here differentiation for device attached or not 10740 */ 10741 if (sdinfo != NULL) { 10742 sata_log(sata_hba_inst, CE_WARN, 10743 "SATA device detached from port %d:%d", 10744 cport, npmport); 10745 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10746 } 10747 10748 mutex_destroy(&pmportinfo->pmport_mutex); 10749 kmem_free(pmportinfo, sizeof (sata_pmport_info_t)); 10750 } 10751 10752 kmem_free(pmultinfo, sizeof (sata_pmult_info_t)); 10753 10754 cportinfo->cport_devp.cport_sata_pmult = NULL; 10755 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 10756 10757 sata_log(sata_hba_inst, CE_WARN, 10758 "SATA port multiplier detached at port %d", cport); 10759 10760 mutex_exit(&cportinfo->cport_mutex); 10761 } 10762 10763 /* 10764 * Initialize device 10765 * Specified device is initialized to a default state. 10766 * 10767 * Returns SATA_SUCCESS if all device features are set successfully, 10768 * SATA_RETRY if device is accessible but device features were not set 10769 * successfully, and SATA_FAILURE otherwise. 10770 */ 10771 static int 10772 sata_initialize_device(sata_hba_inst_t *sata_hba_inst, 10773 sata_drive_info_t *sdinfo) 10774 { 10775 int rval; 10776 10777 sata_save_drive_settings(sdinfo); 10778 10779 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 10780 10781 sata_init_write_cache_mode(sdinfo); 10782 10783 rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0); 10784 10785 /* Determine current data transfer mode */ 10786 if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) { 10787 sdinfo->satadrv_settings &= ~SATA_DEV_DMA; 10788 } else if ((sdinfo->satadrv_id.ai_validinfo & 10789 SATA_VALIDINFO_88) != 0 && 10790 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) { 10791 sdinfo->satadrv_settings |= SATA_DEV_DMA; 10792 } else if ((sdinfo->satadrv_id.ai_dworddma & 10793 SATA_MDMA_SEL_MASK) != 0) { 10794 sdinfo->satadrv_settings |= SATA_DEV_DMA; 10795 } else 10796 /* DMA supported, not no DMA transfer mode is selected !? */ 10797 sdinfo->satadrv_settings &= ~SATA_DEV_DMA; 10798 10799 if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) && 10800 (sdinfo->satadrv_id.ai_features86 & 0x20)) 10801 sdinfo->satadrv_power_level = SATA_POWER_STANDBY; 10802 else 10803 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 10804 10805 return (rval); 10806 } 10807 10808 10809 /* 10810 * Initialize write cache mode. 10811 * 10812 * The default write cache setting for SATA HDD is provided by sata_write_cache 10813 * static variable. ATAPI CD/DVDs devices have write cache default is 10814 * determined by sata_atapicdvd_write_cache static variable. 10815 * ATAPI tape devices have write cache default is determined by 10816 * sata_atapitape_write_cache static variable. 10817 * ATAPI disk devices have write cache default is determined by 10818 * sata_atapidisk_write_cache static variable. 10819 * 1 - enable 10820 * 0 - disable 10821 * any other value - current drive setting 10822 * 10823 * Although there is not reason to disable write cache on CD/DVD devices, 10824 * tape devices and ATAPI disk devices, the default setting control is provided 10825 * for the maximun flexibility. 10826 * 10827 * In the future, it may be overridden by the 10828 * disk-write-cache-enable property setting, if it is defined. 10829 * Returns SATA_SUCCESS if all device features are set successfully, 10830 * SATA_FAILURE otherwise. 10831 */ 10832 static void 10833 sata_init_write_cache_mode(sata_drive_info_t *sdinfo) 10834 { 10835 switch (sdinfo->satadrv_type) { 10836 case SATA_DTYPE_ATADISK: 10837 if (sata_write_cache == 1) 10838 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 10839 else if (sata_write_cache == 0) 10840 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 10841 /* 10842 * When sata_write_cache value is not 0 or 1, 10843 * a current setting of the drive's write cache is used. 10844 */ 10845 break; 10846 case SATA_DTYPE_ATAPICD: 10847 if (sata_atapicdvd_write_cache == 1) 10848 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 10849 else if (sata_atapicdvd_write_cache == 0) 10850 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 10851 /* 10852 * When sata_atapicdvd_write_cache value is not 0 or 1, 10853 * a current setting of the drive's write cache is used. 10854 */ 10855 break; 10856 case SATA_DTYPE_ATAPITAPE: 10857 if (sata_atapitape_write_cache == 1) 10858 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 10859 else if (sata_atapitape_write_cache == 0) 10860 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 10861 /* 10862 * When sata_atapitape_write_cache value is not 0 or 1, 10863 * a current setting of the drive's write cache is used. 10864 */ 10865 break; 10866 case SATA_DTYPE_ATAPIDISK: 10867 if (sata_atapidisk_write_cache == 1) 10868 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 10869 else if (sata_atapidisk_write_cache == 0) 10870 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 10871 /* 10872 * When sata_atapidisk_write_cache value is not 0 or 1, 10873 * a current setting of the drive's write cache is used. 10874 */ 10875 break; 10876 } 10877 } 10878 10879 10880 /* 10881 * Validate sata address. 10882 * Specified cport, pmport and qualifier has to match 10883 * passed sata_scsi configuration info. 10884 * The presence of an attached device is not verified. 10885 * 10886 * Returns 0 when address is valid, -1 otherwise. 10887 */ 10888 static int 10889 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport, 10890 int pmport, int qual) 10891 { 10892 if (qual == SATA_ADDR_DCPORT && pmport != 0) 10893 goto invalid_address; 10894 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 10895 goto invalid_address; 10896 if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) && 10897 ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) || 10898 (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) || 10899 (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport)))) 10900 goto invalid_address; 10901 10902 return (0); 10903 10904 invalid_address: 10905 return (-1); 10906 10907 } 10908 10909 /* 10910 * Validate scsi address 10911 * SCSI target address is translated into SATA cport/pmport and compared 10912 * with a controller port/device configuration. LUN has to be 0. 10913 * Returns 0 if a scsi target refers to an attached device, 10914 * returns 1 if address is valid but no valid device is attached, 10915 * returns 2 if address is valid but device type is unknown (not valid device), 10916 * returns -1 if bad address or device is of an unsupported type. 10917 * Upon return sata_device argument is set. 10918 * 10919 * Port multiplier is supported now. 10920 */ 10921 static int 10922 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst, 10923 struct scsi_address *ap, sata_device_t *sata_device) 10924 { 10925 int cport, pmport, qual, rval; 10926 10927 rval = -1; /* Invalid address */ 10928 if (ap->a_lun != 0) 10929 goto out; 10930 10931 qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 10932 cport = SCSI_TO_SATA_CPORT(ap->a_target); 10933 pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 10934 10935 if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT) 10936 goto out; 10937 10938 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) == 10939 0) { 10940 10941 sata_cport_info_t *cportinfo; 10942 sata_pmult_info_t *pmultinfo; 10943 sata_drive_info_t *sdinfo = NULL; 10944 10945 sata_device->satadev_addr.qual = qual; 10946 sata_device->satadev_addr.cport = cport; 10947 sata_device->satadev_addr.pmport = pmport; 10948 sata_device->satadev_rev = SATA_DEVICE_REV_1; 10949 10950 rval = 1; /* Valid sata address */ 10951 10952 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 10953 if (qual == SATA_ADDR_DCPORT) { 10954 if (cportinfo == NULL || 10955 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 10956 goto out; 10957 10958 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 10959 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN && 10960 sdinfo != NULL) { 10961 rval = 2; 10962 goto out; 10963 } 10964 10965 if ((cportinfo->cport_dev_type & 10966 SATA_VALID_DEV_TYPE) == 0) { 10967 rval = -1; 10968 goto out; 10969 } 10970 10971 } else if (qual == SATA_ADDR_DPMPORT) { 10972 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 10973 if (pmultinfo == NULL) { 10974 rval = -1; 10975 goto out; 10976 } 10977 if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) == 10978 NULL || 10979 SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 10980 pmport) == SATA_DTYPE_NONE) 10981 goto out; 10982 10983 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, 10984 pmport); 10985 if (SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 10986 pmport) == SATA_DTYPE_UNKNOWN && sdinfo != NULL) { 10987 rval = 2; 10988 goto out; 10989 } 10990 10991 if ((SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 10992 pmport) && SATA_VALID_DEV_TYPE) == 0) { 10993 rval = -1; 10994 goto out; 10995 } 10996 10997 } else { 10998 rval = -1; 10999 goto out; 11000 } 11001 if ((sdinfo == NULL) || 11002 (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0) 11003 goto out; 11004 11005 sata_device->satadev_type = sdinfo->satadrv_type; 11006 11007 return (0); 11008 } 11009 out: 11010 if (rval > 0) { 11011 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 11012 "sata_validate_scsi_address: no valid target %x lun %x", 11013 ap->a_target, ap->a_lun); 11014 } 11015 return (rval); 11016 } 11017 11018 /* 11019 * Find dip corresponding to passed device number 11020 * 11021 * Returns NULL if invalid device number is passed or device cannot be found, 11022 * Returns dip is device is found. 11023 */ 11024 static dev_info_t * 11025 sata_devt_to_devinfo(dev_t dev) 11026 { 11027 dev_info_t *dip; 11028 #ifndef __lock_lint 11029 struct devnames *dnp; 11030 major_t major = getmajor(dev); 11031 int instance = SATA_MINOR2INSTANCE(getminor(dev)); 11032 11033 if (major >= devcnt) 11034 return (NULL); 11035 11036 dnp = &devnamesp[major]; 11037 LOCK_DEV_OPS(&(dnp->dn_lock)); 11038 dip = dnp->dn_head; 11039 while (dip && (ddi_get_instance(dip) != instance)) { 11040 dip = ddi_get_next(dip); 11041 } 11042 UNLOCK_DEV_OPS(&(dnp->dn_lock)); 11043 #endif 11044 11045 return (dip); 11046 } 11047 11048 11049 /* 11050 * Probe device. 11051 * This function issues Identify Device command and initializes local 11052 * sata_drive_info structure if the device can be identified. 11053 * The device type is determined by examining Identify Device 11054 * command response. 11055 * If the sata_hba_inst has linked drive info structure for this 11056 * device address, the Identify Device data is stored into sata_drive_info 11057 * structure linked to the port info structure. 11058 * 11059 * sata_device has to refer to the valid sata port(s) for HBA described 11060 * by sata_hba_inst structure. 11061 * 11062 * Returns: 11063 * SATA_SUCCESS if device type was successfully probed and port-linked 11064 * drive info structure was updated; 11065 * SATA_FAILURE if there is no device, or device was not probed 11066 * successully; 11067 * SATA_RETRY if device probe can be retried later. 11068 * If a device cannot be identified, sata_device's dev_state and dev_type 11069 * fields are set to unknown. 11070 * There are no retries in this function. Any retries should be managed by 11071 * the caller. 11072 */ 11073 11074 11075 static int 11076 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 11077 { 11078 sata_pmport_info_t *pmportinfo; 11079 sata_drive_info_t *sdinfo; 11080 sata_drive_info_t new_sdinfo; /* local drive info struct */ 11081 int rval; 11082 11083 ASSERT((SATA_CPORT_STATE(sata_hba_inst, 11084 sata_device->satadev_addr.cport) & 11085 (SATA_STATE_PROBED | SATA_STATE_READY)) != 0); 11086 11087 sata_device->satadev_type = SATA_DTYPE_NONE; 11088 11089 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 11090 sata_device->satadev_addr.cport))); 11091 11092 if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) { 11093 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 11094 sata_device->satadev_addr.cport, 11095 sata_device->satadev_addr.pmport); 11096 ASSERT(pmportinfo != NULL); 11097 } 11098 11099 /* Get pointer to port-linked sata device info structure */ 11100 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 11101 if (sdinfo != NULL) { 11102 sdinfo->satadrv_state &= 11103 ~(SATA_STATE_PROBED | SATA_STATE_READY); 11104 sdinfo->satadrv_state |= SATA_STATE_PROBING; 11105 } else { 11106 /* No device to probe */ 11107 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 11108 sata_device->satadev_addr.cport))); 11109 sata_device->satadev_type = SATA_DTYPE_NONE; 11110 sata_device->satadev_state = SATA_STATE_UNKNOWN; 11111 return (SATA_FAILURE); 11112 } 11113 /* 11114 * Need to issue both types of identify device command and 11115 * determine device type by examining retreived data/status. 11116 * First, ATA Identify Device. 11117 */ 11118 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 11119 new_sdinfo.satadrv_addr = sata_device->satadev_addr; 11120 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 11121 sata_device->satadev_addr.cport))); 11122 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK; 11123 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 11124 if (rval == SATA_RETRY) { 11125 /* We may try to check for ATAPI device */ 11126 if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) { 11127 /* 11128 * HBA supports ATAPI - try to issue Identify Packet 11129 * Device command. 11130 */ 11131 new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI; 11132 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 11133 } 11134 } 11135 if (rval == SATA_SUCCESS) { 11136 /* 11137 * Got something responding positively to ATA Identify Device 11138 * or to Identify Packet Device cmd. 11139 * Save last used device type. 11140 */ 11141 sata_device->satadev_type = new_sdinfo.satadrv_type; 11142 11143 /* save device info, if possible */ 11144 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 11145 sata_device->satadev_addr.cport))); 11146 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 11147 if (sdinfo == NULL) { 11148 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 11149 sata_device->satadev_addr.cport))); 11150 return (SATA_FAILURE); 11151 } 11152 /* 11153 * Copy drive info into the port-linked drive info structure. 11154 */ 11155 *sdinfo = new_sdinfo; 11156 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 11157 sdinfo->satadrv_state |= SATA_STATE_PROBED; 11158 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 11159 SATA_CPORT_DEV_TYPE(sata_hba_inst, 11160 sata_device->satadev_addr.cport) = 11161 sdinfo->satadrv_type; 11162 else { /* SATA_ADDR_DPMPORT */ 11163 mutex_enter(&pmportinfo->pmport_mutex); 11164 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 11165 sata_device->satadev_addr.cport, 11166 sata_device->satadev_addr.pmport) = 11167 sdinfo->satadrv_type; 11168 mutex_exit(&pmportinfo->pmport_mutex); 11169 } 11170 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 11171 sata_device->satadev_addr.cport))); 11172 return (SATA_SUCCESS); 11173 } 11174 11175 /* 11176 * It may be SATA_RETRY or SATA_FAILURE return. 11177 * Looks like we cannot determine the device type at this time. 11178 */ 11179 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 11180 sata_device->satadev_addr.cport))); 11181 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 11182 if (sdinfo != NULL) { 11183 sata_device->satadev_type = SATA_DTYPE_UNKNOWN; 11184 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 11185 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 11186 sdinfo->satadrv_state |= SATA_STATE_PROBED; 11187 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 11188 SATA_CPORT_DEV_TYPE(sata_hba_inst, 11189 sata_device->satadev_addr.cport) = 11190 SATA_DTYPE_UNKNOWN; 11191 else { 11192 /* SATA_ADDR_DPMPORT */ 11193 mutex_enter(&pmportinfo->pmport_mutex); 11194 if ((SATA_PMULT_INFO(sata_hba_inst, 11195 sata_device->satadev_addr.cport) != NULL) && 11196 (SATA_PMPORT_INFO(sata_hba_inst, 11197 sata_device->satadev_addr.cport, 11198 sata_device->satadev_addr.pmport) != NULL)) 11199 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 11200 sata_device->satadev_addr.cport, 11201 sata_device->satadev_addr.pmport) = 11202 SATA_DTYPE_UNKNOWN; 11203 mutex_exit(&pmportinfo->pmport_mutex); 11204 } 11205 } 11206 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 11207 sata_device->satadev_addr.cport))); 11208 return (rval); 11209 } 11210 11211 11212 /* 11213 * Get pointer to sata_drive_info structure. 11214 * 11215 * The sata_device has to contain address (cport, pmport and qualifier) for 11216 * specified sata_scsi structure. 11217 * 11218 * Returns NULL if device address is not valid for this HBA configuration. 11219 * Otherwise, returns a pointer to sata_drive_info structure. 11220 * 11221 * This function should be called with a port mutex held. 11222 */ 11223 static sata_drive_info_t * 11224 sata_get_device_info(sata_hba_inst_t *sata_hba_inst, 11225 sata_device_t *sata_device) 11226 { 11227 uint8_t cport = sata_device->satadev_addr.cport; 11228 uint8_t pmport = sata_device->satadev_addr.pmport; 11229 uint8_t qual = sata_device->satadev_addr.qual; 11230 11231 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 11232 return (NULL); 11233 11234 if (!(SATA_CPORT_STATE(sata_hba_inst, cport) & 11235 (SATA_STATE_PROBED | SATA_STATE_READY))) 11236 /* Port not probed yet */ 11237 return (NULL); 11238 11239 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE) 11240 return (NULL); 11241 11242 if (qual == SATA_ADDR_DCPORT) { 11243 /* Request for a device on a controller port */ 11244 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == 11245 SATA_DTYPE_PMULT) 11246 /* Port multiplier attached */ 11247 return (NULL); 11248 return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport)); 11249 } 11250 if (qual == SATA_ADDR_DPMPORT) { 11251 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 11252 SATA_DTYPE_PMULT) 11253 return (NULL); 11254 11255 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) 11256 return (NULL); 11257 11258 if (!(SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) & 11259 (SATA_STATE_PROBED | SATA_STATE_READY))) 11260 /* Port multiplier port not probed yet */ 11261 return (NULL); 11262 11263 return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport)); 11264 } 11265 11266 /* we should not get here */ 11267 return (NULL); 11268 } 11269 11270 11271 /* 11272 * sata_identify_device. 11273 * Send Identify Device command to SATA HBA driver. 11274 * If command executes successfully, update sata_drive_info structure pointed 11275 * to by sdinfo argument, including Identify Device data. 11276 * If command fails, invalidate data in sata_drive_info. 11277 * 11278 * Cannot be called from interrupt level. 11279 * 11280 * Returns: 11281 * SATA_SUCCESS if the device was identified as a supported device, 11282 * SATA_RETRY if the device was not identified but could be retried, 11283 * SATA_FAILURE if the device was not identified and identify attempt 11284 * should not be retried. 11285 */ 11286 static int 11287 sata_identify_device(sata_hba_inst_t *sata_hba_inst, 11288 sata_drive_info_t *sdinfo) 11289 { 11290 uint16_t cfg_word; 11291 int rval; 11292 11293 /* fetch device identify data */ 11294 if ((rval = sata_fetch_device_identify_data(sata_hba_inst, 11295 sdinfo)) != SATA_SUCCESS) 11296 goto fail_unknown; 11297 11298 cfg_word = sdinfo->satadrv_id.ai_config; 11299 11300 /* Set the correct device type */ 11301 if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) { 11302 sdinfo->satadrv_type = SATA_DTYPE_ATADISK; 11303 } else if (cfg_word == SATA_CFA_TYPE) { 11304 /* It's a Compact Flash media via CF-to-SATA HDD adapter */ 11305 sdinfo->satadrv_type = SATA_DTYPE_ATADISK; 11306 } else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) { 11307 switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) { 11308 case SATA_ATAPI_CDROM_DEV: 11309 sdinfo->satadrv_type = SATA_DTYPE_ATAPICD; 11310 break; 11311 case SATA_ATAPI_SQACC_DEV: 11312 sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE; 11313 break; 11314 case SATA_ATAPI_DIRACC_DEV: 11315 sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK; 11316 break; 11317 default: 11318 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 11319 } 11320 } else { 11321 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 11322 } 11323 11324 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 11325 if (sdinfo->satadrv_capacity == 0) { 11326 /* Non-LBA disk. Too bad... */ 11327 sata_log(sata_hba_inst, CE_WARN, 11328 "SATA disk device at port %d does not support LBA", 11329 sdinfo->satadrv_addr.cport); 11330 rval = SATA_FAILURE; 11331 goto fail_unknown; 11332 } 11333 } 11334 #if 0 11335 /* Left for historical reason */ 11336 /* 11337 * Some initial version of SATA spec indicated that at least 11338 * UDMA mode 4 has to be supported. It is not metioned in 11339 * SerialATA 2.6, so this restriction is removed. 11340 */ 11341 /* Check for Ultra DMA modes 6 through 0 being supported */ 11342 for (i = 6; i >= 0; --i) { 11343 if (sdinfo->satadrv_id.ai_ultradma & (1 << i)) 11344 break; 11345 } 11346 11347 /* 11348 * At least UDMA 4 mode has to be supported. If mode 4 or 11349 * higher are not supported by the device, fail this 11350 * device. 11351 */ 11352 if (i < 4) { 11353 /* No required Ultra DMA mode supported */ 11354 sata_log(sata_hba_inst, CE_WARN, 11355 "SATA disk device at port %d does not support UDMA " 11356 "mode 4 or higher", sdinfo->satadrv_addr.cport); 11357 SATA_LOG_D((sata_hba_inst, CE_WARN, 11358 "mode 4 or higher required, %d supported", i)); 11359 rval = SATA_FAILURE; 11360 goto fail_unknown; 11361 } 11362 #endif 11363 11364 /* 11365 * For Disk devices, if it doesn't support UDMA mode, we would 11366 * like to return failure directly. 11367 */ 11368 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 11369 !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 && 11370 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) { 11371 sata_log(sata_hba_inst, CE_WARN, 11372 "SATA disk device at port %d does not support UDMA", 11373 sdinfo->satadrv_addr.cport); 11374 rval = SATA_FAILURE; 11375 goto fail_unknown; 11376 } 11377 11378 return (SATA_SUCCESS); 11379 11380 fail_unknown: 11381 /* Invalidate sata_drive_info ? */ 11382 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 11383 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 11384 return (rval); 11385 } 11386 11387 /* 11388 * Log/display device information 11389 */ 11390 static void 11391 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst, 11392 sata_drive_info_t *sdinfo) 11393 { 11394 int valid_version; 11395 char msg_buf[MAXPATHLEN]; 11396 int i; 11397 11398 /* Show HBA path */ 11399 (void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf); 11400 11401 cmn_err(CE_CONT, "?%s :\n", msg_buf); 11402 11403 switch (sdinfo->satadrv_type) { 11404 case SATA_DTYPE_ATADISK: 11405 (void) sprintf(msg_buf, "SATA disk device at"); 11406 break; 11407 11408 case SATA_DTYPE_ATAPICD: 11409 (void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at"); 11410 break; 11411 11412 case SATA_DTYPE_ATAPITAPE: 11413 (void) sprintf(msg_buf, "SATA tape (ATAPI) device at"); 11414 break; 11415 11416 case SATA_DTYPE_ATAPIDISK: 11417 (void) sprintf(msg_buf, "SATA disk (ATAPI) device at"); 11418 break; 11419 11420 case SATA_DTYPE_UNKNOWN: 11421 (void) sprintf(msg_buf, 11422 "Unsupported SATA device type (cfg 0x%x) at ", 11423 sdinfo->satadrv_id.ai_config); 11424 break; 11425 } 11426 11427 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT) 11428 cmn_err(CE_CONT, "?\t%s port %d\n", 11429 msg_buf, sdinfo->satadrv_addr.cport); 11430 else 11431 cmn_err(CE_CONT, "?\t%s port %d:%d\n", 11432 msg_buf, sdinfo->satadrv_addr.cport, 11433 sdinfo->satadrv_addr.pmport); 11434 11435 bcopy(&sdinfo->satadrv_id.ai_model, msg_buf, 11436 sizeof (sdinfo->satadrv_id.ai_model)); 11437 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model)); 11438 msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0'; 11439 cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf); 11440 11441 bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf, 11442 sizeof (sdinfo->satadrv_id.ai_fw)); 11443 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw)); 11444 msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0'; 11445 cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf); 11446 11447 bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf, 11448 sizeof (sdinfo->satadrv_id.ai_drvser)); 11449 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser)); 11450 msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0'; 11451 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 11452 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 11453 } else { 11454 /* 11455 * Some drives do not implement serial number and may 11456 * violate the spec by providing spaces rather than zeros 11457 * in serial number field. Scan the buffer to detect it. 11458 */ 11459 for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) { 11460 if (msg_buf[i] != '\0' && msg_buf[i] != ' ') 11461 break; 11462 } 11463 if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) { 11464 cmn_err(CE_CONT, "?\tserial number - none\n"); 11465 } else { 11466 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 11467 } 11468 } 11469 11470 #ifdef SATA_DEBUG 11471 if (sdinfo->satadrv_id.ai_majorversion != 0 && 11472 sdinfo->satadrv_id.ai_majorversion != 0xffff) { 11473 int i; 11474 for (i = 14; i >= 2; i--) { 11475 if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) { 11476 valid_version = i; 11477 break; 11478 } 11479 } 11480 cmn_err(CE_CONT, 11481 "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n", 11482 valid_version, 11483 sdinfo->satadrv_id.ai_majorversion, 11484 sdinfo->satadrv_id.ai_minorversion); 11485 } 11486 #endif 11487 /* Log some info */ 11488 cmn_err(CE_CONT, "?\tsupported features:\n"); 11489 msg_buf[0] = '\0'; 11490 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 11491 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) 11492 (void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN); 11493 else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) 11494 (void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN); 11495 } 11496 if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA) 11497 (void) strlcat(msg_buf, "DMA", MAXPATHLEN); 11498 if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) 11499 (void) strlcat(msg_buf, ", Native Command Queueing", 11500 MAXPATHLEN); 11501 if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ) 11502 (void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN); 11503 if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) && 11504 (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED)) 11505 (void) strlcat(msg_buf, ", SMART", MAXPATHLEN); 11506 if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) && 11507 (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED)) 11508 (void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN); 11509 cmn_err(CE_CONT, "?\t %s\n", msg_buf); 11510 if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2) 11511 cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n"); 11512 else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1) 11513 cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n"); 11514 if (sdinfo->satadrv_features_support & 11515 (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) { 11516 msg_buf[0] = '\0'; 11517 (void) snprintf(msg_buf, MAXPATHLEN, 11518 "Supported queue depth %d", 11519 sdinfo->satadrv_queue_depth); 11520 if (!(sata_func_enable & 11521 (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ))) 11522 (void) strlcat(msg_buf, 11523 " - queueing disabled globally", MAXPATHLEN); 11524 else if (sdinfo->satadrv_queue_depth > 11525 sdinfo->satadrv_max_queue_depth) { 11526 (void) snprintf(&msg_buf[strlen(msg_buf)], 11527 MAXPATHLEN - strlen(msg_buf), ", limited to %d", 11528 (int)sdinfo->satadrv_max_queue_depth); 11529 } 11530 cmn_err(CE_CONT, "?\t%s\n", msg_buf); 11531 } 11532 11533 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 11534 #ifdef __i386 11535 (void) sprintf(msg_buf, "\tcapacity = %llu sectors\n", 11536 sdinfo->satadrv_capacity); 11537 #else 11538 (void) sprintf(msg_buf, "\tcapacity = %lu sectors\n", 11539 sdinfo->satadrv_capacity); 11540 #endif 11541 cmn_err(CE_CONT, "?%s", msg_buf); 11542 } 11543 } 11544 11545 /* 11546 * Log/display port multiplier information 11547 */ 11548 static void 11549 sata_show_pmult_info(sata_hba_inst_t *sata_hba_inst, 11550 sata_device_t *sata_device) 11551 { 11552 _NOTE(ARGUNUSED(sata_hba_inst)) 11553 11554 char msg_buf[MAXPATHLEN]; 11555 uint32_t gscr0, gscr1, gscr2, gscr64; 11556 11557 gscr0 = sata_device->satadev_gscr.gscr0; 11558 gscr1 = sata_device->satadev_gscr.gscr1; 11559 gscr2 = sata_device->satadev_gscr.gscr2; 11560 gscr64 = sata_device->satadev_gscr.gscr64; 11561 11562 cmn_err(CE_CONT, "?Port Multiplier %d device-ports found at port %d", 11563 sata_device->satadev_add_info, sata_device->satadev_addr.cport); 11564 11565 (void) sprintf(msg_buf, "\tVendor_ID 0x%04x, Module_ID 0x%04x", 11566 gscr0 & 0xffff, (gscr0 >> 16) & 0xffff); 11567 cmn_err(CE_CONT, "?%s", msg_buf); 11568 11569 (void) strcpy(msg_buf, "\tSupport SATA PMP Spec "); 11570 if (gscr1 & (1 << 3)) 11571 (void) strlcat(msg_buf, "1.2", MAXPATHLEN); 11572 else if (gscr1 & (1 << 2)) 11573 (void) strlcat(msg_buf, "1.1", MAXPATHLEN); 11574 else if (gscr1 & (1 << 1)) 11575 (void) strlcat(msg_buf, "1.0", MAXPATHLEN); 11576 else 11577 (void) strlcat(msg_buf, "unknown", MAXPATHLEN); 11578 cmn_err(CE_CONT, "?%s", msg_buf); 11579 11580 (void) strcpy(msg_buf, "\tSupport "); 11581 if (gscr64 & (1 << 3)) 11582 (void) strlcat(msg_buf, "Asy-Notif, ", 11583 MAXPATHLEN); 11584 if (gscr64 & (1 << 2)) 11585 (void) strlcat(msg_buf, "Dyn-SSC, ", MAXPATHLEN); 11586 if (gscr64 & (1 << 1)) 11587 (void) strlcat(msg_buf, "Iss-PMREQ, ", MAXPATHLEN); 11588 if (gscr64 & (1 << 0)) 11589 (void) strlcat(msg_buf, "BIST", MAXPATHLEN); 11590 if ((gscr64 & 0xf) == 0) 11591 (void) strlcat(msg_buf, "nothing", MAXPATHLEN); 11592 cmn_err(CE_CONT, "?%s", msg_buf); 11593 11594 (void) sprintf(msg_buf, "\tNumber of exposed device fan-out ports: %d", 11595 gscr2 & SATA_PMULT_PORTNUM_MASK); 11596 cmn_err(CE_CONT, "?%s", msg_buf); 11597 } 11598 11599 /* 11600 * sata_save_drive_settings extracts current setting of the device and stores 11601 * it for future reference, in case the device setup would need to be restored 11602 * after the device reset. 11603 * 11604 * For all devices read ahead and write cache settings are saved, if the 11605 * device supports these features at all. 11606 * For ATAPI devices the Removable Media Status Notification setting is saved. 11607 */ 11608 static void 11609 sata_save_drive_settings(sata_drive_info_t *sdinfo) 11610 { 11611 if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) || 11612 SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) { 11613 11614 /* Current setting of Read Ahead (and Read Cache) */ 11615 if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id)) 11616 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 11617 else 11618 sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD; 11619 11620 /* Current setting of Write Cache */ 11621 if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id)) 11622 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 11623 else 11624 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 11625 } 11626 11627 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) { 11628 if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id)) 11629 sdinfo->satadrv_settings |= SATA_DEV_RMSN; 11630 else 11631 sdinfo->satadrv_settings &= ~SATA_DEV_RMSN; 11632 } 11633 } 11634 11635 11636 /* 11637 * sata_check_capacity function determines a disk capacity 11638 * and addressing mode (LBA28/LBA48) by examining a disk identify device data. 11639 * 11640 * NOTE: CHS mode is not supported! If a device does not support LBA, 11641 * this function is not called. 11642 * 11643 * Returns device capacity in number of blocks, i.e. largest addressable LBA+1 11644 */ 11645 static uint64_t 11646 sata_check_capacity(sata_drive_info_t *sdinfo) 11647 { 11648 uint64_t capacity = 0; 11649 int i; 11650 11651 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK || 11652 !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT) 11653 /* Capacity valid only for LBA-addressable disk devices */ 11654 return (0); 11655 11656 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) && 11657 (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) && 11658 (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) { 11659 /* LBA48 mode supported and enabled */ 11660 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 | 11661 SATA_DEV_F_LBA28; 11662 for (i = 3; i >= 0; --i) { 11663 capacity <<= 16; 11664 capacity += sdinfo->satadrv_id.ai_addrsecxt[i]; 11665 } 11666 } else { 11667 capacity = sdinfo->satadrv_id.ai_addrsec[1]; 11668 capacity <<= 16; 11669 capacity += sdinfo->satadrv_id.ai_addrsec[0]; 11670 if (capacity >= 0x1000000) 11671 /* LBA28 mode */ 11672 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28; 11673 } 11674 return (capacity); 11675 } 11676 11677 11678 /* 11679 * Allocate consistent buffer for DMA transfer 11680 * 11681 * Cannot be called from interrupt level or with mutex held - it may sleep. 11682 * 11683 * Returns pointer to allocated buffer structure, or NULL if allocation failed. 11684 */ 11685 static struct buf * 11686 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len) 11687 { 11688 struct scsi_address ap; 11689 struct buf *bp; 11690 ddi_dma_attr_t cur_dma_attr; 11691 11692 ASSERT(spx->txlt_sata_pkt != NULL); 11693 ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran; 11694 ap.a_target = SATA_TO_SCSI_TARGET( 11695 spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport, 11696 spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport, 11697 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual); 11698 ap.a_lun = 0; 11699 11700 bp = scsi_alloc_consistent_buf(&ap, NULL, len, 11701 B_READ, SLEEP_FUNC, NULL); 11702 11703 if (bp != NULL) { 11704 /* Allocate DMA resources for this buffer */ 11705 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 11706 /* 11707 * We use a local version of the dma_attr, to account 11708 * for a device addressing limitations. 11709 * sata_adjust_dma_attr() will handle sdinfo == NULL which 11710 * will cause dma attributes to be adjusted to a lowest 11711 * acceptable level. 11712 */ 11713 sata_adjust_dma_attr(NULL, 11714 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 11715 11716 if (sata_dma_buf_setup(spx, PKT_CONSISTENT, 11717 SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) { 11718 scsi_free_consistent_buf(bp); 11719 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 11720 bp = NULL; 11721 } 11722 } 11723 return (bp); 11724 } 11725 11726 /* 11727 * Release local buffer (consistent buffer for DMA transfer) allocated 11728 * via sata_alloc_local_buffer(). 11729 */ 11730 static void 11731 sata_free_local_buffer(sata_pkt_txlate_t *spx) 11732 { 11733 ASSERT(spx->txlt_sata_pkt != NULL); 11734 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL); 11735 11736 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0; 11737 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL; 11738 11739 sata_common_free_dma_rsrcs(spx); 11740 11741 /* Free buffer */ 11742 scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp); 11743 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 11744 } 11745 11746 /* 11747 * Allocate sata_pkt 11748 * Pkt structure version and embedded strcutures version are initialized. 11749 * sata_pkt and sata_pkt_txlate structures are cross-linked. 11750 * 11751 * Since this may be called in interrupt context by sata_scsi_init_pkt, 11752 * callback argument determines if it can sleep or not. 11753 * Hence, it should not be called from interrupt context. 11754 * 11755 * If successful, non-NULL pointer to a sata pkt is returned. 11756 * Upon failure, NULL pointer is returned. 11757 */ 11758 static sata_pkt_t * 11759 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t)) 11760 { 11761 sata_pkt_t *spkt; 11762 int kmsflag; 11763 11764 kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP; 11765 spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag); 11766 if (spkt == NULL) { 11767 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 11768 "sata_pkt_alloc: failed")); 11769 return (NULL); 11770 } 11771 spkt->satapkt_rev = SATA_PKT_REV; 11772 spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV; 11773 spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 11774 spkt->satapkt_framework_private = spx; 11775 spx->txlt_sata_pkt = spkt; 11776 return (spkt); 11777 } 11778 11779 /* 11780 * Free sata pkt allocated via sata_pkt_alloc() 11781 */ 11782 static void 11783 sata_pkt_free(sata_pkt_txlate_t *spx) 11784 { 11785 ASSERT(spx->txlt_sata_pkt != NULL); 11786 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL); 11787 kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t)); 11788 spx->txlt_sata_pkt = NULL; 11789 } 11790 11791 11792 /* 11793 * Adjust DMA attributes. 11794 * SCSI cmds block count is up to 24 bits, SATA cmd block count vary 11795 * from 8 bits to 16 bits, depending on a command being used. 11796 * Limiting max block count arbitrarily to 256 for all read/write 11797 * commands may affects performance, so check both the device and 11798 * controller capability before adjusting dma attributes. 11799 */ 11800 void 11801 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr, 11802 ddi_dma_attr_t *adj_dma_attr) 11803 { 11804 uint32_t count_max; 11805 11806 /* Copy original attributes */ 11807 *adj_dma_attr = *dma_attr; 11808 /* 11809 * Things to consider: device addressing capability, 11810 * "excessive" controller DMA capabilities. 11811 * If a device is being probed/initialized, there are 11812 * no device info - use default limits then. 11813 */ 11814 if (sdinfo == NULL) { 11815 count_max = dma_attr->dma_attr_granular * 0x100; 11816 if (dma_attr->dma_attr_count_max > count_max) 11817 adj_dma_attr->dma_attr_count_max = count_max; 11818 if (dma_attr->dma_attr_maxxfer > count_max) 11819 adj_dma_attr->dma_attr_maxxfer = count_max; 11820 return; 11821 } 11822 11823 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 11824 if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) { 11825 /* 11826 * 16-bit sector count may be used - we rely on 11827 * the assumption that only read and write cmds 11828 * will request more than 256 sectors worth of data 11829 */ 11830 count_max = adj_dma_attr->dma_attr_granular * 0x10000; 11831 } else { 11832 /* 11833 * 8-bit sector count will be used - default limits 11834 * for dma attributes 11835 */ 11836 count_max = adj_dma_attr->dma_attr_granular * 0x100; 11837 } 11838 /* 11839 * Adjust controler dma attributes, if necessary 11840 */ 11841 if (dma_attr->dma_attr_count_max > count_max) 11842 adj_dma_attr->dma_attr_count_max = count_max; 11843 if (dma_attr->dma_attr_maxxfer > count_max) 11844 adj_dma_attr->dma_attr_maxxfer = count_max; 11845 } 11846 } 11847 11848 11849 /* 11850 * Allocate DMA resources for the buffer 11851 * This function handles initial DMA resource allocation as well as 11852 * DMA window shift and may be called repeatedly for the same DMA window 11853 * until all DMA cookies in the DMA window are processed. 11854 * To guarantee that there is always a coherent set of cookies to process 11855 * by SATA HBA driver (observing alignment, device granularity, etc.), 11856 * the number of slots for DMA cookies is equal to lesser of a number of 11857 * cookies in a DMA window and a max number of scatter/gather entries. 11858 * 11859 * Returns DDI_SUCCESS upon successful operation. 11860 * Return failure code of a failing command or DDI_FAILURE when 11861 * internal cleanup failed. 11862 */ 11863 static int 11864 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags, 11865 int (*callback)(caddr_t), caddr_t arg, 11866 ddi_dma_attr_t *cur_dma_attr) 11867 { 11868 int rval; 11869 off_t offset; 11870 size_t size; 11871 int max_sg_len, req_len, i; 11872 uint_t dma_flags; 11873 struct buf *bp; 11874 uint64_t cur_txfer_len; 11875 11876 11877 ASSERT(spx->txlt_sata_pkt != NULL); 11878 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 11879 ASSERT(bp != NULL); 11880 11881 11882 if (spx->txlt_buf_dma_handle == NULL) { 11883 /* 11884 * No DMA resources allocated so far - this is a first call 11885 * for this sata pkt. 11886 */ 11887 rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst), 11888 cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle); 11889 11890 if (rval != DDI_SUCCESS) { 11891 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 11892 "sata_dma_buf_setup: no buf DMA resources %x", 11893 rval)); 11894 return (rval); 11895 } 11896 11897 if (bp->b_flags & B_READ) 11898 dma_flags = DDI_DMA_READ; 11899 else 11900 dma_flags = DDI_DMA_WRITE; 11901 11902 if (flags & PKT_CONSISTENT) 11903 dma_flags |= DDI_DMA_CONSISTENT; 11904 11905 if (flags & PKT_DMA_PARTIAL) 11906 dma_flags |= DDI_DMA_PARTIAL; 11907 11908 /* 11909 * Check buffer alignment and size against dma attributes 11910 * Consider dma_attr_align only. There may be requests 11911 * with the size lower than device granularity, but they 11912 * will not read/write from/to the device, so no adjustment 11913 * is necessary. The dma_attr_minxfer theoretically should 11914 * be considered, but no HBA driver is checking it. 11915 */ 11916 if (IS_P2ALIGNED(bp->b_un.b_addr, 11917 cur_dma_attr->dma_attr_align)) { 11918 rval = ddi_dma_buf_bind_handle( 11919 spx->txlt_buf_dma_handle, 11920 bp, dma_flags, callback, arg, 11921 &spx->txlt_dma_cookie, 11922 &spx->txlt_curwin_num_dma_cookies); 11923 } else { /* Buffer is not aligned */ 11924 11925 int (*ddicallback)(caddr_t); 11926 size_t bufsz; 11927 11928 /* Check id sleeping is allowed */ 11929 ddicallback = (callback == NULL_FUNC) ? 11930 DDI_DMA_DONTWAIT : DDI_DMA_SLEEP; 11931 11932 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 11933 "mis-aligned buffer: addr=0x%p, cnt=%lu", 11934 (void *)bp->b_un.b_addr, bp->b_bcount); 11935 11936 if (bp->b_flags & (B_PAGEIO|B_PHYS)) 11937 /* 11938 * CPU will need to access data in the buffer 11939 * (for copying) so map it. 11940 */ 11941 bp_mapin(bp); 11942 11943 ASSERT(spx->txlt_tmp_buf == NULL); 11944 11945 /* Buffer may be padded by ddi_dma_mem_alloc()! */ 11946 rval = ddi_dma_mem_alloc( 11947 spx->txlt_buf_dma_handle, 11948 bp->b_bcount, 11949 &sata_acc_attr, 11950 DDI_DMA_STREAMING, 11951 ddicallback, NULL, 11952 &spx->txlt_tmp_buf, 11953 &bufsz, 11954 &spx->txlt_tmp_buf_handle); 11955 11956 if (rval != DDI_SUCCESS) { 11957 /* DMA mapping failed */ 11958 (void) ddi_dma_free_handle( 11959 &spx->txlt_buf_dma_handle); 11960 spx->txlt_buf_dma_handle = NULL; 11961 #ifdef SATA_DEBUG 11962 mbuffail_count++; 11963 #endif 11964 SATADBG1(SATA_DBG_DMA_SETUP, 11965 spx->txlt_sata_hba_inst, 11966 "sata_dma_buf_setup: " 11967 "buf dma mem alloc failed %x\n", rval); 11968 return (rval); 11969 } 11970 ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf, 11971 cur_dma_attr->dma_attr_align)); 11972 11973 #ifdef SATA_DEBUG 11974 mbuf_count++; 11975 11976 if (bp->b_bcount != bufsz) 11977 /* 11978 * This will require special handling, because 11979 * DMA cookies will be based on the temporary 11980 * buffer size, not the original buffer 11981 * b_bcount, so the residue may have to 11982 * be counted differently. 11983 */ 11984 SATADBG2(SATA_DBG_DMA_SETUP, 11985 spx->txlt_sata_hba_inst, 11986 "sata_dma_buf_setup: bp size %x != " 11987 "bufsz %x\n", bp->b_bcount, bufsz); 11988 #endif 11989 if (dma_flags & DDI_DMA_WRITE) { 11990 /* 11991 * Write operation - copy data into 11992 * an aligned temporary buffer. Buffer will be 11993 * synced for device by ddi_dma_addr_bind_handle 11994 */ 11995 bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf, 11996 bp->b_bcount); 11997 } 11998 11999 rval = ddi_dma_addr_bind_handle( 12000 spx->txlt_buf_dma_handle, 12001 NULL, 12002 spx->txlt_tmp_buf, 12003 bufsz, dma_flags, ddicallback, 0, 12004 &spx->txlt_dma_cookie, 12005 &spx->txlt_curwin_num_dma_cookies); 12006 } 12007 12008 switch (rval) { 12009 case DDI_DMA_PARTIAL_MAP: 12010 SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 12011 "sata_dma_buf_setup: DMA Partial Map\n", NULL); 12012 /* 12013 * Partial DMA mapping. 12014 * Retrieve number of DMA windows for this request. 12015 */ 12016 if (ddi_dma_numwin(spx->txlt_buf_dma_handle, 12017 &spx->txlt_num_dma_win) != DDI_SUCCESS) { 12018 if (spx->txlt_tmp_buf != NULL) { 12019 ddi_dma_mem_free( 12020 &spx->txlt_tmp_buf_handle); 12021 spx->txlt_tmp_buf = NULL; 12022 } 12023 (void) ddi_dma_unbind_handle( 12024 spx->txlt_buf_dma_handle); 12025 (void) ddi_dma_free_handle( 12026 &spx->txlt_buf_dma_handle); 12027 spx->txlt_buf_dma_handle = NULL; 12028 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 12029 "sata_dma_buf_setup: numwin failed\n")); 12030 return (DDI_FAILURE); 12031 } 12032 SATADBG2(SATA_DBG_DMA_SETUP, 12033 spx->txlt_sata_hba_inst, 12034 "sata_dma_buf_setup: windows: %d, cookies: %d\n", 12035 spx->txlt_num_dma_win, 12036 spx->txlt_curwin_num_dma_cookies); 12037 spx->txlt_cur_dma_win = 0; 12038 break; 12039 12040 case DDI_DMA_MAPPED: 12041 /* DMA fully mapped */ 12042 spx->txlt_num_dma_win = 1; 12043 spx->txlt_cur_dma_win = 0; 12044 SATADBG1(SATA_DBG_DMA_SETUP, 12045 spx->txlt_sata_hba_inst, 12046 "sata_dma_buf_setup: windows: 1 " 12047 "cookies: %d\n", spx->txlt_curwin_num_dma_cookies); 12048 break; 12049 12050 default: 12051 /* DMA mapping failed */ 12052 if (spx->txlt_tmp_buf != NULL) { 12053 ddi_dma_mem_free( 12054 &spx->txlt_tmp_buf_handle); 12055 spx->txlt_tmp_buf = NULL; 12056 } 12057 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 12058 spx->txlt_buf_dma_handle = NULL; 12059 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 12060 "sata_dma_buf_setup: buf dma handle binding " 12061 "failed %x\n", rval)); 12062 return (rval); 12063 } 12064 spx->txlt_curwin_processed_dma_cookies = 0; 12065 spx->txlt_dma_cookie_list = NULL; 12066 } else { 12067 /* 12068 * DMA setup is reused. Check if we need to process more 12069 * cookies in current window, or to get next window, if any. 12070 */ 12071 12072 ASSERT(spx->txlt_curwin_processed_dma_cookies <= 12073 spx->txlt_curwin_num_dma_cookies); 12074 12075 if (spx->txlt_curwin_processed_dma_cookies == 12076 spx->txlt_curwin_num_dma_cookies) { 12077 /* 12078 * All cookies from current DMA window were processed. 12079 * Get next DMA window. 12080 */ 12081 spx->txlt_cur_dma_win++; 12082 if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) { 12083 (void) ddi_dma_getwin(spx->txlt_buf_dma_handle, 12084 spx->txlt_cur_dma_win, &offset, &size, 12085 &spx->txlt_dma_cookie, 12086 &spx->txlt_curwin_num_dma_cookies); 12087 spx->txlt_curwin_processed_dma_cookies = 0; 12088 } else { 12089 /* No more windows! End of request! */ 12090 /* What to do? - panic for now */ 12091 ASSERT(spx->txlt_cur_dma_win >= 12092 spx->txlt_num_dma_win); 12093 12094 spx->txlt_curwin_num_dma_cookies = 0; 12095 spx->txlt_curwin_processed_dma_cookies = 0; 12096 spx->txlt_sata_pkt-> 12097 satapkt_cmd.satacmd_num_dma_cookies = 0; 12098 return (DDI_SUCCESS); 12099 } 12100 } 12101 } 12102 /* There better be at least one DMA cookie outstanding */ 12103 ASSERT((spx->txlt_curwin_num_dma_cookies - 12104 spx->txlt_curwin_processed_dma_cookies) > 0); 12105 12106 if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) { 12107 /* The default cookie slot was used in previous run */ 12108 ASSERT(spx->txlt_curwin_processed_dma_cookies == 0); 12109 spx->txlt_dma_cookie_list = NULL; 12110 spx->txlt_dma_cookie_list_len = 0; 12111 } 12112 if (spx->txlt_curwin_processed_dma_cookies == 0) { 12113 /* 12114 * Processing a new DMA window - set-up dma cookies list. 12115 * We may reuse previously allocated cookie array if it is 12116 * possible. 12117 */ 12118 if (spx->txlt_dma_cookie_list != NULL && 12119 spx->txlt_dma_cookie_list_len < 12120 spx->txlt_curwin_num_dma_cookies) { 12121 /* 12122 * New DMA window contains more cookies than 12123 * the previous one. We need larger cookie list - free 12124 * the old one. 12125 */ 12126 (void) kmem_free(spx->txlt_dma_cookie_list, 12127 spx->txlt_dma_cookie_list_len * 12128 sizeof (ddi_dma_cookie_t)); 12129 spx->txlt_dma_cookie_list = NULL; 12130 spx->txlt_dma_cookie_list_len = 0; 12131 } 12132 if (spx->txlt_dma_cookie_list == NULL) { 12133 /* 12134 * Calculate lesser of number of cookies in this 12135 * DMA window and number of s/g entries. 12136 */ 12137 max_sg_len = cur_dma_attr->dma_attr_sgllen; 12138 req_len = MIN(max_sg_len, 12139 spx->txlt_curwin_num_dma_cookies); 12140 12141 /* Allocate new dma cookie array if necessary */ 12142 if (req_len == 1) { 12143 /* Only one cookie - no need for a list */ 12144 spx->txlt_dma_cookie_list = 12145 &spx->txlt_dma_cookie; 12146 spx->txlt_dma_cookie_list_len = 1; 12147 } else { 12148 /* 12149 * More than one cookie - try to allocate space. 12150 */ 12151 spx->txlt_dma_cookie_list = kmem_zalloc( 12152 sizeof (ddi_dma_cookie_t) * req_len, 12153 callback == NULL_FUNC ? KM_NOSLEEP : 12154 KM_SLEEP); 12155 if (spx->txlt_dma_cookie_list == NULL) { 12156 SATADBG1(SATA_DBG_DMA_SETUP, 12157 spx->txlt_sata_hba_inst, 12158 "sata_dma_buf_setup: cookie list " 12159 "allocation failed\n", NULL); 12160 /* 12161 * We could not allocate space for 12162 * neccessary number of dma cookies in 12163 * this window, so we fail this request. 12164 * Next invocation would try again to 12165 * allocate space for cookie list. 12166 * Note:Packet residue was not modified. 12167 */ 12168 return (DDI_DMA_NORESOURCES); 12169 } else { 12170 spx->txlt_dma_cookie_list_len = req_len; 12171 } 12172 } 12173 } 12174 /* 12175 * Fetch DMA cookies into cookie list in sata_pkt_txlate. 12176 * First cookie was already fetched. 12177 */ 12178 *(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie; 12179 cur_txfer_len = 12180 (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size; 12181 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1; 12182 spx->txlt_curwin_processed_dma_cookies++; 12183 for (i = 1; (i < spx->txlt_dma_cookie_list_len) && 12184 (i < spx->txlt_curwin_num_dma_cookies); i++) { 12185 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 12186 &spx->txlt_dma_cookie_list[i]); 12187 cur_txfer_len += 12188 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 12189 spx->txlt_curwin_processed_dma_cookies++; 12190 spx->txlt_sata_pkt-> 12191 satapkt_cmd.satacmd_num_dma_cookies += 1; 12192 } 12193 } else { 12194 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 12195 "sata_dma_buf_setup: sliding within DMA window, " 12196 "cur cookie %d, total cookies %d\n", 12197 spx->txlt_curwin_processed_dma_cookies, 12198 spx->txlt_curwin_num_dma_cookies); 12199 12200 /* 12201 * Not all cookies from the current dma window were used because 12202 * of s/g limitation. 12203 * There is no need to re-size the list - it was set at 12204 * optimal size, or only default entry is used (s/g = 1). 12205 */ 12206 if (spx->txlt_dma_cookie_list == NULL) { 12207 spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie; 12208 spx->txlt_dma_cookie_list_len = 1; 12209 } 12210 /* 12211 * Since we are processing remaining cookies in a DMA window, 12212 * there may be less of them than the number of entries in the 12213 * current dma cookie list. 12214 */ 12215 req_len = MIN(spx->txlt_dma_cookie_list_len, 12216 (spx->txlt_curwin_num_dma_cookies - 12217 spx->txlt_curwin_processed_dma_cookies)); 12218 12219 /* Fetch the next batch of cookies */ 12220 for (i = 0, cur_txfer_len = 0; i < req_len; i++) { 12221 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 12222 &spx->txlt_dma_cookie_list[i]); 12223 cur_txfer_len += 12224 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 12225 spx->txlt_sata_pkt-> 12226 satapkt_cmd.satacmd_num_dma_cookies++; 12227 spx->txlt_curwin_processed_dma_cookies++; 12228 } 12229 } 12230 12231 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0); 12232 12233 /* Point sata_cmd to the cookie list */ 12234 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = 12235 &spx->txlt_dma_cookie_list[0]; 12236 12237 /* Remember number of DMA cookies passed in sata packet */ 12238 spx->txlt_num_dma_cookies = 12239 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies; 12240 12241 ASSERT(cur_txfer_len != 0); 12242 if (cur_txfer_len <= bp->b_bcount) 12243 spx->txlt_total_residue -= cur_txfer_len; 12244 else { 12245 /* 12246 * Temporary DMA buffer has been padded by 12247 * ddi_dma_mem_alloc()! 12248 * This requires special handling, because DMA cookies are 12249 * based on the temporary buffer size, not the b_bcount, 12250 * and we have extra bytes to transfer - but the packet 12251 * residue has to stay correct because we will copy only 12252 * the requested number of bytes. 12253 */ 12254 spx->txlt_total_residue -= bp->b_bcount; 12255 } 12256 12257 return (DDI_SUCCESS); 12258 } 12259 12260 /* 12261 * Common routine for releasing DMA resources 12262 */ 12263 static void 12264 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx) 12265 { 12266 if (spx->txlt_buf_dma_handle != NULL) { 12267 if (spx->txlt_tmp_buf != NULL) { 12268 /* 12269 * Intermediate DMA buffer was allocated. 12270 * Free allocated buffer and associated access handle. 12271 */ 12272 ddi_dma_mem_free(&spx->txlt_tmp_buf_handle); 12273 spx->txlt_tmp_buf = NULL; 12274 } 12275 /* 12276 * Free DMA resources - cookies and handles 12277 */ 12278 /* ASSERT(spx->txlt_dma_cookie_list != NULL); */ 12279 if (spx->txlt_dma_cookie_list != NULL) { 12280 if (spx->txlt_dma_cookie_list != 12281 &spx->txlt_dma_cookie) { 12282 (void) kmem_free(spx->txlt_dma_cookie_list, 12283 spx->txlt_dma_cookie_list_len * 12284 sizeof (ddi_dma_cookie_t)); 12285 spx->txlt_dma_cookie_list = NULL; 12286 } 12287 } 12288 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle); 12289 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 12290 spx->txlt_buf_dma_handle = NULL; 12291 } 12292 } 12293 12294 /* 12295 * Free DMA resources 12296 * Used by the HBA driver to release DMA resources that it does not use. 12297 * 12298 * Returns Void 12299 */ 12300 void 12301 sata_free_dma_resources(sata_pkt_t *sata_pkt) 12302 { 12303 sata_pkt_txlate_t *spx; 12304 12305 if (sata_pkt == NULL) 12306 return; 12307 12308 spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 12309 12310 sata_common_free_dma_rsrcs(spx); 12311 } 12312 12313 /* 12314 * Fetch Device Identify data. 12315 * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type) 12316 * command to a device and get the device identify data. 12317 * The device_info structure has to be set to device type (for selecting proper 12318 * device identify command). 12319 * 12320 * Returns: 12321 * SATA_SUCCESS if cmd succeeded 12322 * SATA_RETRY if cmd was rejected and could be retried, 12323 * SATA_FAILURE if cmd failed and should not be retried (port error) 12324 * 12325 * Cannot be called in an interrupt context. 12326 */ 12327 12328 static int 12329 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst, 12330 sata_drive_info_t *sdinfo) 12331 { 12332 struct buf *bp; 12333 sata_pkt_t *spkt; 12334 sata_cmd_t *scmd; 12335 sata_pkt_txlate_t *spx; 12336 int rval; 12337 12338 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12339 spx->txlt_sata_hba_inst = sata_hba_inst; 12340 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12341 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12342 if (spkt == NULL) { 12343 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12344 return (SATA_RETRY); /* may retry later */ 12345 } 12346 /* address is needed now */ 12347 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12348 12349 /* 12350 * Allocate buffer for Identify Data return data 12351 */ 12352 bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t)); 12353 if (bp == NULL) { 12354 sata_pkt_free(spx); 12355 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12356 SATA_LOG_D((sata_hba_inst, CE_WARN, 12357 "sata_fetch_device_identify_data: " 12358 "cannot allocate buffer for ID")); 12359 return (SATA_RETRY); /* may retry later */ 12360 } 12361 12362 /* Fill sata_pkt */ 12363 sdinfo->satadrv_state = SATA_STATE_PROBING; 12364 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12365 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12366 /* Synchronous mode, no callback */ 12367 spkt->satapkt_comp = NULL; 12368 /* Timeout 30s */ 12369 spkt->satapkt_time = sata_default_pkt_time; 12370 12371 scmd = &spkt->satapkt_cmd; 12372 scmd->satacmd_bp = bp; 12373 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 12374 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 12375 12376 /* Build Identify Device cmd in the sata_pkt */ 12377 scmd->satacmd_addr_type = 0; /* N/A */ 12378 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 12379 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 12380 scmd->satacmd_lba_mid_lsb = 0; /* N/A */ 12381 scmd->satacmd_lba_high_lsb = 0; /* N/A */ 12382 scmd->satacmd_features_reg = 0; /* N/A */ 12383 scmd->satacmd_device_reg = 0; /* Always device 0 */ 12384 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 12385 /* Identify Packet Device cmd */ 12386 scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE; 12387 } else { 12388 /* Identify Device cmd - mandatory for all other devices */ 12389 scmd->satacmd_cmd_reg = SATAC_ID_DEVICE; 12390 } 12391 12392 /* Send pkt to SATA HBA driver */ 12393 rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt); 12394 12395 #ifdef SATA_INJECT_FAULTS 12396 sata_inject_pkt_fault(spkt, &rval, sata_fault_type); 12397 #endif 12398 12399 if (rval == SATA_TRAN_ACCEPTED && 12400 spkt->satapkt_reason == SATA_PKT_COMPLETED) { 12401 if (spx->txlt_buf_dma_handle != NULL) { 12402 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 12403 DDI_DMA_SYNC_FORKERNEL); 12404 ASSERT(rval == DDI_SUCCESS); 12405 } 12406 if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config & 12407 SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) { 12408 SATA_LOG_D((sata_hba_inst, CE_WARN, 12409 "SATA disk device at port %d - " 12410 "partial Identify Data", 12411 sdinfo->satadrv_addr.cport)); 12412 rval = SATA_RETRY; /* may retry later */ 12413 goto fail; 12414 } 12415 /* Update sata_drive_info */ 12416 bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id, 12417 sizeof (sata_id_t)); 12418 12419 sdinfo->satadrv_features_support = 0; 12420 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 12421 /* 12422 * Retrieve capacity (disks only) and addressing mode 12423 */ 12424 sdinfo->satadrv_capacity = sata_check_capacity(sdinfo); 12425 } else { 12426 /* 12427 * For ATAPI devices one would have to issue 12428 * Get Capacity cmd for media capacity. Not here. 12429 */ 12430 sdinfo->satadrv_capacity = 0; 12431 /* 12432 * Check what cdb length is supported 12433 */ 12434 if ((sdinfo->satadrv_id.ai_config & 12435 SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B) 12436 sdinfo->satadrv_atapi_cdb_len = 16; 12437 else 12438 sdinfo->satadrv_atapi_cdb_len = 12; 12439 } 12440 /* Setup supported features flags */ 12441 if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) 12442 sdinfo->satadrv_features_support |= SATA_DEV_F_DMA; 12443 12444 /* Check for SATA GEN and NCQ support */ 12445 if (sdinfo->satadrv_id.ai_satacap != 0 && 12446 sdinfo->satadrv_id.ai_satacap != 0xffff) { 12447 /* SATA compliance */ 12448 if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ) 12449 sdinfo->satadrv_features_support |= 12450 SATA_DEV_F_NCQ; 12451 if (sdinfo->satadrv_id.ai_satacap & 12452 (SATA_1_SPEED | SATA_2_SPEED)) { 12453 if (sdinfo->satadrv_id.ai_satacap & 12454 SATA_2_SPEED) 12455 sdinfo->satadrv_features_support |= 12456 SATA_DEV_F_SATA2; 12457 if (sdinfo->satadrv_id.ai_satacap & 12458 SATA_1_SPEED) 12459 sdinfo->satadrv_features_support |= 12460 SATA_DEV_F_SATA1; 12461 } else { 12462 sdinfo->satadrv_features_support |= 12463 SATA_DEV_F_SATA1; 12464 } 12465 } 12466 if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) && 12467 (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD)) 12468 sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ; 12469 12470 sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth; 12471 if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) || 12472 (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) { 12473 ++sdinfo->satadrv_queue_depth; 12474 /* Adjust according to controller capabilities */ 12475 sdinfo->satadrv_max_queue_depth = MIN( 12476 sdinfo->satadrv_queue_depth, 12477 SATA_QDEPTH(sata_hba_inst)); 12478 /* Adjust according to global queue depth limit */ 12479 sdinfo->satadrv_max_queue_depth = MIN( 12480 sdinfo->satadrv_max_queue_depth, 12481 sata_current_max_qdepth); 12482 if (sdinfo->satadrv_max_queue_depth == 0) 12483 sdinfo->satadrv_max_queue_depth = 1; 12484 } else 12485 sdinfo->satadrv_max_queue_depth = 1; 12486 12487 rval = SATA_SUCCESS; 12488 } else { 12489 /* 12490 * Woops, no Identify Data. 12491 */ 12492 if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) { 12493 rval = SATA_RETRY; /* may retry later */ 12494 } else if (rval == SATA_TRAN_ACCEPTED) { 12495 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR || 12496 spkt->satapkt_reason == SATA_PKT_ABORTED || 12497 spkt->satapkt_reason == SATA_PKT_TIMEOUT || 12498 spkt->satapkt_reason == SATA_PKT_RESET) 12499 rval = SATA_RETRY; /* may retry later */ 12500 else 12501 rval = SATA_FAILURE; 12502 } else { 12503 rval = SATA_FAILURE; 12504 } 12505 } 12506 fail: 12507 /* Free allocated resources */ 12508 sata_free_local_buffer(spx); 12509 sata_pkt_free(spx); 12510 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12511 12512 return (rval); 12513 } 12514 12515 12516 /* 12517 * Some devices may not come-up with default DMA mode (UDMA or MWDMA). 12518 * UDMA mode is checked first, followed by MWDMA mode. 12519 * set correctly, so this function is setting it to the highest supported level. 12520 * Older SATA spec required that the device supports at least DMA 4 mode and 12521 * UDMA mode is selected. It is not mentioned in SerialATA 2.6, so this 12522 * restriction has been removed. 12523 * 12524 * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported. 12525 * Returns SATA_FAILURE if proper DMA mode could not be selected. 12526 * 12527 * NOTE: This function should be called only if DMA mode is supported. 12528 */ 12529 static int 12530 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo) 12531 { 12532 sata_pkt_t *spkt; 12533 sata_cmd_t *scmd; 12534 sata_pkt_txlate_t *spx; 12535 int mode; 12536 uint8_t subcmd; 12537 int rval = SATA_SUCCESS; 12538 12539 ASSERT(sdinfo != NULL); 12540 ASSERT(sata_hba_inst != NULL); 12541 12542 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 && 12543 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) { 12544 /* Find highest Ultra DMA mode supported */ 12545 for (mode = 6; mode >= 0; --mode) { 12546 if (sdinfo->satadrv_id.ai_ultradma & (1 << mode)) 12547 break; 12548 } 12549 #if 0 12550 /* Left for historical reasons */ 12551 /* 12552 * Some initial version of SATA spec indicated that at least 12553 * UDMA mode 4 has to be supported. It is not mentioned in 12554 * SerialATA 2.6, so this restriction is removed. 12555 */ 12556 if (mode < 4) 12557 return (SATA_FAILURE); 12558 #endif 12559 12560 /* 12561 * We're still going to set DMA mode whatever is selected 12562 * by default 12563 * 12564 * We saw an old maxtor sata drive will select Ultra DMA and 12565 * Multi-Word DMA simultaneouly by default, which is going 12566 * to cause DMA command timed out, so we need to select DMA 12567 * mode even when it's already done by default 12568 */ 12569 12570 subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA; 12571 12572 } else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) { 12573 /* Find highest MultiWord DMA mode supported */ 12574 for (mode = 2; mode >= 0; --mode) { 12575 if (sdinfo->satadrv_id.ai_dworddma & (1 << mode)) 12576 break; 12577 } 12578 12579 /* 12580 * We're still going to set DMA mode whatever is selected 12581 * by default 12582 * 12583 * We saw an old maxtor sata drive will select Ultra DMA and 12584 * Multi-Word DMA simultaneouly by default, which is going 12585 * to cause DMA command timed out, so we need to select DMA 12586 * mode even when it's already done by default 12587 */ 12588 12589 subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA; 12590 } else 12591 return (SATA_SUCCESS); 12592 12593 /* 12594 * Set DMA mode via SET FEATURES COMMAND. 12595 * Prepare packet for SET FEATURES COMMAND. 12596 */ 12597 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12598 spx->txlt_sata_hba_inst = sata_hba_inst; 12599 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12600 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12601 if (spkt == NULL) { 12602 SATA_LOG_D((sata_hba_inst, CE_WARN, 12603 "sata_set_dma_mode: could not set DMA mode %", mode)); 12604 rval = SATA_FAILURE; 12605 goto done; 12606 } 12607 /* Fill sata_pkt */ 12608 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12609 /* Timeout 30s */ 12610 spkt->satapkt_time = sata_default_pkt_time; 12611 /* Synchronous mode, no callback, interrupts */ 12612 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12613 spkt->satapkt_comp = NULL; 12614 scmd = &spkt->satapkt_cmd; 12615 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 12616 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 12617 scmd->satacmd_addr_type = 0; 12618 scmd->satacmd_device_reg = 0; 12619 scmd->satacmd_status_reg = 0; 12620 scmd->satacmd_error_reg = 0; 12621 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 12622 scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE; 12623 scmd->satacmd_sec_count_lsb = subcmd | mode; 12624 12625 /* Transfer command to HBA */ 12626 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 12627 spkt) != SATA_TRAN_ACCEPTED || 12628 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 12629 /* Pkt execution failed */ 12630 rval = SATA_FAILURE; 12631 } 12632 done: 12633 12634 /* Free allocated resources */ 12635 if (spkt != NULL) 12636 sata_pkt_free(spx); 12637 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12638 12639 return (rval); 12640 } 12641 12642 12643 /* 12644 * Set device caching mode. 12645 * One of the following operations should be specified: 12646 * SATAC_SF_ENABLE_READ_AHEAD 12647 * SATAC_SF_DISABLE_READ_AHEAD 12648 * SATAC_SF_ENABLE_WRITE_CACHE 12649 * SATAC_SF_DISABLE_WRITE_CACHE 12650 * 12651 * If operation fails, system log messgage is emitted. 12652 * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if 12653 * command was sent but did not succeed, and SATA_FAILURE otherwise. 12654 */ 12655 12656 static int 12657 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 12658 int cache_op) 12659 { 12660 sata_pkt_t *spkt; 12661 sata_cmd_t *scmd; 12662 sata_pkt_txlate_t *spx; 12663 int rval = SATA_SUCCESS; 12664 int hba_rval; 12665 char *infop; 12666 12667 ASSERT(sdinfo != NULL); 12668 ASSERT(sata_hba_inst != NULL); 12669 ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD || 12670 cache_op == SATAC_SF_DISABLE_READ_AHEAD || 12671 cache_op == SATAC_SF_ENABLE_WRITE_CACHE || 12672 cache_op == SATAC_SF_DISABLE_WRITE_CACHE); 12673 12674 12675 /* Prepare packet for SET FEATURES COMMAND */ 12676 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12677 spx->txlt_sata_hba_inst = sata_hba_inst; 12678 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12679 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12680 if (spkt == NULL) { 12681 rval = SATA_FAILURE; 12682 goto failure; 12683 } 12684 /* Fill sata_pkt */ 12685 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12686 /* Timeout 30s */ 12687 spkt->satapkt_time = sata_default_pkt_time; 12688 /* Synchronous mode, no callback, interrupts */ 12689 spkt->satapkt_op_mode = 12690 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12691 spkt->satapkt_comp = NULL; 12692 scmd = &spkt->satapkt_cmd; 12693 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 12694 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 12695 scmd->satacmd_addr_type = 0; 12696 scmd->satacmd_device_reg = 0; 12697 scmd->satacmd_status_reg = 0; 12698 scmd->satacmd_error_reg = 0; 12699 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 12700 scmd->satacmd_features_reg = cache_op; 12701 12702 /* Transfer command to HBA */ 12703 hba_rval = (*SATA_START_FUNC(sata_hba_inst))( 12704 SATA_DIP(sata_hba_inst), spkt); 12705 12706 #ifdef SATA_INJECT_FAULTS 12707 sata_inject_pkt_fault(spkt, &rval, sata_fault_type); 12708 #endif 12709 12710 if ((hba_rval != SATA_TRAN_ACCEPTED) || 12711 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 12712 /* Pkt execution failed */ 12713 switch (cache_op) { 12714 case SATAC_SF_ENABLE_READ_AHEAD: 12715 infop = "enabling read ahead failed"; 12716 break; 12717 case SATAC_SF_DISABLE_READ_AHEAD: 12718 infop = "disabling read ahead failed"; 12719 break; 12720 case SATAC_SF_ENABLE_WRITE_CACHE: 12721 infop = "enabling write cache failed"; 12722 break; 12723 case SATAC_SF_DISABLE_WRITE_CACHE: 12724 infop = "disabling write cache failed"; 12725 break; 12726 } 12727 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 12728 rval = SATA_RETRY; 12729 } 12730 failure: 12731 /* Free allocated resources */ 12732 if (spkt != NULL) 12733 sata_pkt_free(spx); 12734 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12735 return (rval); 12736 } 12737 12738 /* 12739 * Set Removable Media Status Notification (enable/disable) 12740 * state == 0 , disable 12741 * state != 0 , enable 12742 * 12743 * If operation fails, system log messgage is emitted. 12744 * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise. 12745 */ 12746 12747 static int 12748 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 12749 int state) 12750 { 12751 sata_pkt_t *spkt; 12752 sata_cmd_t *scmd; 12753 sata_pkt_txlate_t *spx; 12754 int rval = SATA_SUCCESS; 12755 char *infop; 12756 12757 ASSERT(sdinfo != NULL); 12758 ASSERT(sata_hba_inst != NULL); 12759 12760 /* Prepare packet for SET FEATURES COMMAND */ 12761 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12762 spx->txlt_sata_hba_inst = sata_hba_inst; 12763 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12764 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12765 if (spkt == NULL) { 12766 rval = SATA_FAILURE; 12767 goto failure; 12768 } 12769 /* Fill sata_pkt */ 12770 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12771 /* Timeout 30s */ 12772 spkt->satapkt_time = sata_default_pkt_time; 12773 /* Synchronous mode, no callback, interrupts */ 12774 spkt->satapkt_op_mode = 12775 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12776 spkt->satapkt_comp = NULL; 12777 scmd = &spkt->satapkt_cmd; 12778 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 12779 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 12780 scmd->satacmd_addr_type = 0; 12781 scmd->satacmd_device_reg = 0; 12782 scmd->satacmd_status_reg = 0; 12783 scmd->satacmd_error_reg = 0; 12784 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 12785 if (state == 0) 12786 scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN; 12787 else 12788 scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN; 12789 12790 /* Transfer command to HBA */ 12791 if (((*SATA_START_FUNC(sata_hba_inst))( 12792 SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) || 12793 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 12794 /* Pkt execution failed */ 12795 if (state == 0) 12796 infop = "disabling Removable Media Status " 12797 "Notification failed"; 12798 else 12799 infop = "enabling Removable Media Status " 12800 "Notification failed"; 12801 12802 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 12803 rval = SATA_FAILURE; 12804 } 12805 failure: 12806 /* Free allocated resources */ 12807 if (spkt != NULL) 12808 sata_pkt_free(spx); 12809 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12810 return (rval); 12811 } 12812 12813 12814 /* 12815 * Update state and copy port ss* values from passed sata_device structure. 12816 * sata_address is validated - if not valid, nothing is changed in sata_scsi 12817 * configuration struct. 12818 * 12819 * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function 12820 * regardless of the state in device argument. 12821 * 12822 * Port mutex should be held while calling this function. 12823 */ 12824 static void 12825 sata_update_port_info(sata_hba_inst_t *sata_hba_inst, 12826 sata_device_t *sata_device) 12827 { 12828 sata_cport_info_t *cportinfo; 12829 12830 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT || 12831 sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) { 12832 if (SATA_NUM_CPORTS(sata_hba_inst) <= 12833 sata_device->satadev_addr.cport) 12834 return; 12835 12836 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 12837 sata_device->satadev_addr.cport); 12838 12839 ASSERT(mutex_owned(&cportinfo->cport_mutex)); 12840 cportinfo->cport_scr = sata_device->satadev_scr; 12841 12842 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 12843 cportinfo->cport_state &= ~(SATA_PSTATE_PWRON | 12844 SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED); 12845 cportinfo->cport_state |= 12846 sata_device->satadev_state & SATA_PSTATE_VALID; 12847 } 12848 } 12849 12850 void 12851 sata_update_pmport_info(sata_hba_inst_t *sata_hba_inst, 12852 sata_device_t *sata_device) 12853 { 12854 sata_pmport_info_t *pmportinfo; 12855 12856 if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT && 12857 sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) || 12858 SATA_NUM_PMPORTS(sata_hba_inst, 12859 sata_device->satadev_addr.cport) < 12860 sata_device->satadev_addr.pmport) { 12861 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 12862 "sata_update_port_info: error address %p.", 12863 &sata_device->satadev_addr); 12864 return; 12865 } 12866 12867 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 12868 sata_device->satadev_addr.cport, 12869 sata_device->satadev_addr.pmport); 12870 12871 ASSERT(mutex_owned(&pmportinfo->pmport_mutex)); 12872 pmportinfo->pmport_scr = sata_device->satadev_scr; 12873 12874 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 12875 pmportinfo->pmport_state &= 12876 ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED); 12877 pmportinfo->pmport_state |= 12878 sata_device->satadev_state & SATA_PSTATE_VALID; 12879 } 12880 12881 /* 12882 * Extract SATA port specification from an IOCTL argument. 12883 * 12884 * This function return the port the user land send us as is, unless it 12885 * cannot retrieve port spec, then -1 is returned. 12886 * 12887 * Support port multiplier. 12888 */ 12889 static int32_t 12890 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp) 12891 { 12892 int32_t port; 12893 12894 /* Extract port number from nvpair in dca structure */ 12895 if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) { 12896 SATA_LOG_D((sata_hba_inst, CE_NOTE, 12897 "sata_get_port_num: invalid port spec 0x%x in ioctl", 12898 port)); 12899 port = -1; 12900 } 12901 12902 return (port); 12903 } 12904 12905 /* 12906 * Get dev_info_t pointer to the device node pointed to by port argument. 12907 * NOTE: target argument is a value used in ioctls to identify 12908 * the AP - it is not a sata_address. 12909 * It is a combination of cport, pmport and address qualifier, encodded same 12910 * way as a scsi target number. 12911 * At this moment it carries only cport number. 12912 * 12913 * PMult hotplug is supported now. 12914 * 12915 * Returns dev_info_t pointer if target device was found, NULL otherwise. 12916 */ 12917 12918 static dev_info_t * 12919 sata_get_target_dip(dev_info_t *dip, uint8_t cport, uint8_t pmport) 12920 { 12921 dev_info_t *cdip = NULL; 12922 int target, tgt; 12923 int circ; 12924 uint8_t qual; 12925 12926 sata_hba_inst_t *sata_hba_inst; 12927 scsi_hba_tran_t *scsi_hba_tran; 12928 12929 /* Get target id */ 12930 scsi_hba_tran = ddi_get_driver_private(dip); 12931 if (scsi_hba_tran == NULL) 12932 return (NULL); 12933 12934 sata_hba_inst = scsi_hba_tran->tran_hba_private; 12935 12936 if (sata_hba_inst == NULL) 12937 return (NULL); 12938 12939 /* Identify a port-mult by cport_info.cport_dev_type */ 12940 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) 12941 qual = SATA_ADDR_DPMPORT; 12942 else 12943 qual = SATA_ADDR_DCPORT; 12944 12945 target = SATA_TO_SCSI_TARGET(cport, pmport, qual); 12946 12947 /* Retrieve target dip */ 12948 ndi_devi_enter(dip, &circ); 12949 for (cdip = ddi_get_child(dip); cdip != NULL; ) { 12950 dev_info_t *next = ddi_get_next_sibling(cdip); 12951 12952 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip, 12953 DDI_PROP_DONTPASS, "target", -1); 12954 if (tgt == -1) { 12955 /* 12956 * This is actually an error condition, but not 12957 * a fatal one. Just continue the search. 12958 */ 12959 cdip = next; 12960 continue; 12961 } 12962 12963 if (tgt == target) 12964 break; 12965 12966 cdip = next; 12967 } 12968 ndi_devi_exit(dip, circ); 12969 12970 return (cdip); 12971 } 12972 12973 /* 12974 * Get dev_info_t pointer to the device node pointed to by port argument. 12975 * NOTE: target argument is a value used in ioctls to identify 12976 * the AP - it is not a sata_address. 12977 * It is a combination of cport, pmport and address qualifier, encoded same 12978 * way as a scsi target number. 12979 * 12980 * Returns dev_info_t pointer if target device was found, NULL otherwise. 12981 */ 12982 12983 static dev_info_t * 12984 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr) 12985 { 12986 dev_info_t *cdip = NULL; 12987 int target, tgt; 12988 int circ; 12989 12990 target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual); 12991 12992 ndi_devi_enter(dip, &circ); 12993 for (cdip = ddi_get_child(dip); cdip != NULL; ) { 12994 dev_info_t *next = ddi_get_next_sibling(cdip); 12995 12996 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip, 12997 DDI_PROP_DONTPASS, "target", -1); 12998 if (tgt == -1) { 12999 /* 13000 * This is actually an error condition, but not 13001 * a fatal one. Just continue the search. 13002 */ 13003 cdip = next; 13004 continue; 13005 } 13006 13007 if (tgt == target) 13008 break; 13009 13010 cdip = next; 13011 } 13012 ndi_devi_exit(dip, circ); 13013 13014 return (cdip); 13015 } 13016 13017 /* 13018 * Process sata port disconnect request. 13019 * Normally, cfgadm sata plugin will try to offline (unconfigure) the device 13020 * before this request. Nevertheless, if a device is still configured, 13021 * we need to attempt to offline and unconfigure device. 13022 * Regardless of the unconfigure operation results the port is marked as 13023 * deactivated and no access to the attached device is possible. 13024 * If the target node remains because unconfigure operation failed, its state 13025 * will be set to DEVICE_REMOVED, preventing it to be used again when a device 13026 * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure 13027 * the device and remove old target node. 13028 * 13029 * This function invokes sata_hba_inst->satahba_tran-> 13030 * sata_tran_hotplug_ops->sata_tran_port_deactivate(). 13031 * If successful, the device structure (if any) attached to the specified port 13032 * is removed and state of the port marked appropriately. 13033 * Failure of the port_deactivate may keep port in the physically active state, 13034 * or may fail the port. 13035 * 13036 * NOTE: Port multiplier is supported. 13037 */ 13038 13039 static int 13040 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst, 13041 sata_device_t *sata_device) 13042 { 13043 sata_drive_info_t *sdinfo = NULL, *subsdinfo = NULL; 13044 sata_cport_info_t *cportinfo = NULL; 13045 sata_pmport_info_t *pmportinfo = NULL; 13046 sata_pmult_info_t *pmultinfo = NULL; 13047 sata_device_t subsdevice; 13048 int cport, pmport, qual; 13049 int rval = SATA_SUCCESS; 13050 int npmport = 0; 13051 int rv = 0; 13052 13053 cport = sata_device->satadev_addr.cport; 13054 pmport = sata_device->satadev_addr.pmport; 13055 qual = sata_device->satadev_addr.qual; 13056 13057 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 13058 if (qual == SATA_ADDR_DCPORT) 13059 qual = SATA_ADDR_CPORT; 13060 else 13061 qual = SATA_ADDR_PMPORT; 13062 13063 /* 13064 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran-> 13065 * sata_tran_hotplug_ops->sata_tran_port_deactivate(). 13066 * Do the sanity check. 13067 */ 13068 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) { 13069 /* No physical port deactivation supported. */ 13070 return (EINVAL); 13071 } 13072 13073 /* Check the current state of the port */ 13074 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 13075 (SATA_DIP(sata_hba_inst), sata_device); 13076 13077 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 13078 13079 /* 13080 * Processing port mulitiplier 13081 */ 13082 if (qual == SATA_ADDR_CPORT && 13083 SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) { 13084 mutex_enter(&cportinfo->cport_mutex); 13085 13086 /* Check controller port status */ 13087 sata_update_port_info(sata_hba_inst, sata_device); 13088 if (rval != SATA_SUCCESS || 13089 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 13090 /* 13091 * Device port status is unknown or it is in failed 13092 * state 13093 */ 13094 SATA_CPORT_STATE(sata_hba_inst, cport) = 13095 SATA_PSTATE_FAILED; 13096 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 13097 "sata_hba_ioctl: connect: failed to deactivate " 13098 "SATA port %d", cport); 13099 mutex_exit(&cportinfo->cport_mutex); 13100 return (EIO); 13101 } 13102 13103 /* Disconnect all sub-devices. */ 13104 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 13105 if (pmultinfo != NULL) { 13106 13107 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 13108 sata_hba_inst, cport); npmport ++) { 13109 subsdinfo = SATA_PMPORT_DRV_INFO( 13110 sata_hba_inst, cport, npmport); 13111 if (subsdinfo == NULL) 13112 continue; 13113 13114 subsdevice.satadev_addr = subsdinfo-> 13115 satadrv_addr; 13116 13117 mutex_exit(&cportinfo->cport_mutex); 13118 if (sata_ioctl_disconnect(sata_hba_inst, 13119 &subsdevice) == SATA_SUCCESS) { 13120 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 13121 "[Remove] device at port %d:%d " 13122 "successfully.", cport, npmport); 13123 } 13124 mutex_enter(&cportinfo->cport_mutex); 13125 } 13126 } 13127 13128 /* Disconnect the port multiplier */ 13129 cportinfo->cport_state &= ~SATA_STATE_READY; 13130 mutex_exit(&cportinfo->cport_mutex); 13131 13132 sata_device->satadev_addr.qual = qual; 13133 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 13134 (SATA_DIP(sata_hba_inst), sata_device); 13135 13136 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 13137 SE_NO_HINT); 13138 13139 mutex_enter(&cportinfo->cport_mutex); 13140 sata_update_port_info(sata_hba_inst, sata_device); 13141 if (rval != SATA_SUCCESS && 13142 sata_device->satadev_state & SATA_PSTATE_FAILED) { 13143 cportinfo->cport_state = SATA_PSTATE_FAILED; 13144 rv = EIO; 13145 } else { 13146 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 13147 } 13148 mutex_exit(&cportinfo->cport_mutex); 13149 13150 return (rv); 13151 } 13152 13153 /* 13154 * Process non-port-multiplier device - it could be a drive connected 13155 * to a port multiplier port or a controller port. 13156 */ 13157 if (qual == SATA_ADDR_PMPORT) { 13158 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 13159 mutex_enter(&pmportinfo->pmport_mutex); 13160 sata_update_pmport_info(sata_hba_inst, sata_device); 13161 if (rval != SATA_SUCCESS || 13162 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 13163 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 13164 SATA_PSTATE_FAILED; 13165 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 13166 "sata_hba_ioctl: connect: failed to deactivate " 13167 "SATA port %d:%d", cport, pmport); 13168 mutex_exit(&pmportinfo->pmport_mutex); 13169 return (EIO); 13170 } 13171 13172 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 13173 sdinfo = pmportinfo->pmport_sata_drive; 13174 ASSERT(sdinfo != NULL); 13175 } 13176 13177 /* 13178 * Set port's dev_state to not ready - this will disable 13179 * an access to a potentially attached device. 13180 */ 13181 pmportinfo->pmport_state &= ~SATA_STATE_READY; 13182 13183 /* Remove and release sata_drive info structure. */ 13184 if (sdinfo != NULL) { 13185 if ((sdinfo->satadrv_type & 13186 SATA_VALID_DEV_TYPE) != 0) { 13187 /* 13188 * If a target node exists, try to offline 13189 * a device and remove target node. 13190 */ 13191 mutex_exit(&pmportinfo->pmport_mutex); 13192 (void) sata_offline_device(sata_hba_inst, 13193 sata_device, sdinfo); 13194 mutex_enter(&pmportinfo->pmport_mutex); 13195 } 13196 13197 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 13198 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 13199 (void) kmem_free((void *)sdinfo, 13200 sizeof (sata_drive_info_t)); 13201 } 13202 mutex_exit(&pmportinfo->pmport_mutex); 13203 13204 } else if (qual == SATA_ADDR_CPORT) { 13205 mutex_enter(&cportinfo->cport_mutex); 13206 sata_update_port_info(sata_hba_inst, sata_device); 13207 if (rval != SATA_SUCCESS || 13208 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 13209 /* 13210 * Device port status is unknown or it is in failed 13211 * state 13212 */ 13213 SATA_CPORT_STATE(sata_hba_inst, cport) = 13214 SATA_PSTATE_FAILED; 13215 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 13216 "sata_hba_ioctl: connect: failed to deactivate " 13217 "SATA port %d", cport); 13218 mutex_exit(&cportinfo->cport_mutex); 13219 return (EIO); 13220 } 13221 13222 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 13223 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 13224 ASSERT(pmultinfo != NULL); 13225 } else if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 13226 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 13227 ASSERT(sdinfo != NULL); 13228 } 13229 cportinfo->cport_state &= ~SATA_STATE_READY; 13230 13231 if (sdinfo != NULL) { 13232 if ((sdinfo->satadrv_type & 13233 SATA_VALID_DEV_TYPE) != 0) { 13234 /* 13235 * If a target node exists, try to offline 13236 * a device and remove target node. 13237 */ 13238 mutex_exit(&cportinfo->cport_mutex); 13239 (void) sata_offline_device(sata_hba_inst, 13240 sata_device, sdinfo); 13241 mutex_enter(&cportinfo->cport_mutex); 13242 } 13243 13244 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 13245 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 13246 (void) kmem_free((void *)sdinfo, 13247 sizeof (sata_drive_info_t)); 13248 } 13249 mutex_exit(&cportinfo->cport_mutex); 13250 } 13251 13252 /* Just ask HBA driver to deactivate port */ 13253 sata_device->satadev_addr.qual = qual; 13254 13255 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 13256 (SATA_DIP(sata_hba_inst), sata_device); 13257 13258 /* 13259 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 13260 * without the hint (to force listener to investivate the state). 13261 */ 13262 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 13263 SE_NO_HINT); 13264 13265 if (qual == SATA_ADDR_PMPORT) { 13266 mutex_enter(&pmportinfo->pmport_mutex); 13267 sata_update_pmport_info(sata_hba_inst, sata_device); 13268 13269 if (rval != SATA_SUCCESS && 13270 sata_device->satadev_state & SATA_PSTATE_FAILED) { 13271 /* 13272 * Port deactivation failure - do not change port 13273 * state unless the state returned by HBA indicates a 13274 * port failure. 13275 * 13276 * NOTE: device structures were released, so devices 13277 * now are invisible! Port reset is needed to 13278 * re-enumerate devices. 13279 */ 13280 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 13281 rv = EIO; 13282 } else { 13283 /* 13284 * Deactivation succeded. From now on the sata framework 13285 * will not care what is happening to the device, until 13286 * the port is activated again. 13287 */ 13288 pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN; 13289 } 13290 mutex_exit(&pmportinfo->pmport_mutex); 13291 } else if (qual == SATA_ADDR_CPORT) { 13292 mutex_enter(&cportinfo->cport_mutex); 13293 sata_update_port_info(sata_hba_inst, sata_device); 13294 13295 if (rval != SATA_SUCCESS && 13296 sata_device->satadev_state & SATA_PSTATE_FAILED) { 13297 cportinfo->cport_state = SATA_PSTATE_FAILED; 13298 rv = EIO; 13299 } else { 13300 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 13301 } 13302 mutex_exit(&cportinfo->cport_mutex); 13303 } 13304 13305 return (rv); 13306 } 13307 13308 13309 13310 /* 13311 * Process sata port connect request 13312 * The sata cfgadm pluging will invoke this operation only if port was found 13313 * in the disconnect state (failed state is also treated as the disconnected 13314 * state). 13315 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->satahba_tran-> 13316 * sata_tran_hotplug_ops->sata_tran_port_activate(). 13317 * If successful and a device is found attached to the port, 13318 * the initialization sequence is executed to attach a device structure to 13319 * a port structure. The state of the port and a device would be set 13320 * appropriately. 13321 * The device is not set in configured state (system-wise) by this operation. 13322 * 13323 * Note, that activating the port may generate link events, 13324 * so it is important that following processing and the 13325 * event processing does not interfere with each other! 13326 * 13327 * This operation may remove port failed state and will 13328 * try to make port active and in good standing. 13329 * 13330 * NOTE: Port multiplier is supported. 13331 */ 13332 13333 static int 13334 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst, 13335 sata_device_t *sata_device) 13336 { 13337 sata_pmport_info_t *pmportinfo = NULL; 13338 uint8_t cport, pmport, qual; 13339 int rv = 0; 13340 13341 cport = sata_device->satadev_addr.cport; 13342 pmport = sata_device->satadev_addr.pmport; 13343 qual = sata_device->satadev_addr.qual; 13344 13345 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 13346 if (qual == SATA_ADDR_DCPORT) 13347 qual = SATA_ADDR_CPORT; 13348 else 13349 qual = SATA_ADDR_PMPORT; 13350 13351 if (qual == SATA_ADDR_PMPORT) 13352 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 13353 13354 /* 13355 * DEVCTL_AP_CONNECT would invoke sata_hba_inst-> 13356 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate(). 13357 * Perform sanity check now. 13358 */ 13359 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) { 13360 /* No physical port activation supported. */ 13361 return (EINVAL); 13362 } 13363 13364 /* Just ask HBA driver to activate port */ 13365 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 13366 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 13367 /* 13368 * Port activation failure. 13369 */ 13370 if (qual == SATA_ADDR_CPORT) { 13371 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 13372 cport)->cport_mutex); 13373 sata_update_port_info(sata_hba_inst, sata_device); 13374 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 13375 SATA_CPORT_STATE(sata_hba_inst, cport) = 13376 SATA_PSTATE_FAILED; 13377 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 13378 "sata_hba_ioctl: connect: failed to " 13379 "activate SATA port %d", cport); 13380 } 13381 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 13382 cport)->cport_mutex); 13383 } else { /* port multiplier device port */ 13384 mutex_enter(&pmportinfo->pmport_mutex); 13385 sata_update_pmport_info(sata_hba_inst, sata_device); 13386 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 13387 SATA_PMPORT_STATE(sata_hba_inst, cport, 13388 pmport) = SATA_PSTATE_FAILED; 13389 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 13390 "sata_hba_ioctl: connect: failed to " 13391 "activate SATA port %d:%d", cport, pmport); 13392 } 13393 mutex_exit(&pmportinfo->pmport_mutex); 13394 } 13395 return (EIO); 13396 } 13397 13398 /* Virgin port state - will be updated by the port re-probe. */ 13399 if (qual == SATA_ADDR_CPORT) { 13400 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 13401 cport)->cport_mutex); 13402 SATA_CPORT_STATE(sata_hba_inst, cport) = 0; 13403 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 13404 cport)->cport_mutex); 13405 } else { /* port multiplier device port */ 13406 mutex_enter(&pmportinfo->pmport_mutex); 13407 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0; 13408 mutex_exit(&pmportinfo->pmport_mutex); 13409 } 13410 13411 /* 13412 * Probe the port to find its state and attached device. 13413 */ 13414 if (sata_reprobe_port(sata_hba_inst, sata_device, 13415 SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE) 13416 rv = EIO; 13417 13418 /* 13419 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 13420 * without the hint 13421 */ 13422 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 13423 SE_NO_HINT); 13424 13425 /* 13426 * If there is a device attached to the port, emit 13427 * a message. 13428 */ 13429 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 13430 13431 if (qual == SATA_ADDR_CPORT) { 13432 if (sata_device->satadev_type == SATA_DTYPE_PMULT) { 13433 sata_log(sata_hba_inst, CE_WARN, 13434 "SATA port multiplier detected " 13435 "at port %d", cport); 13436 } else { 13437 sata_log(sata_hba_inst, CE_WARN, 13438 "SATA device detected at port %d", cport); 13439 if (sata_device->satadev_type == 13440 SATA_DTYPE_UNKNOWN) { 13441 /* 13442 * A device was not successfully identified 13443 */ 13444 sata_log(sata_hba_inst, CE_WARN, 13445 "Could not identify SATA " 13446 "device at port %d", cport); 13447 } 13448 } 13449 } else { /* port multiplier device port */ 13450 sata_log(sata_hba_inst, CE_WARN, 13451 "SATA device detected at port %d:%d", 13452 cport, pmport); 13453 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 13454 /* 13455 * A device was not successfully identified 13456 */ 13457 sata_log(sata_hba_inst, CE_WARN, 13458 "Could not identify SATA " 13459 "device at port %d:%d", cport, pmport); 13460 } 13461 } 13462 } 13463 13464 return (rv); 13465 } 13466 13467 13468 /* 13469 * Process sata device unconfigure request. 13470 * The unconfigure operation uses generic nexus operation to 13471 * offline a device. It leaves a target device node attached. 13472 * and obviously sata_drive_info attached as well, because 13473 * from the hardware point of view nothing has changed. 13474 */ 13475 static int 13476 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst, 13477 sata_device_t *sata_device) 13478 { 13479 int rv = 0; 13480 dev_info_t *tdip; 13481 13482 /* We are addressing attached device, not a port */ 13483 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 13484 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 13485 else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) 13486 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 13487 13488 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 13489 &sata_device->satadev_addr)) != NULL) { 13490 13491 if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) { 13492 SATA_LOG_D((sata_hba_inst, CE_WARN, 13493 "sata_hba_ioctl: unconfigure: " 13494 "failed to unconfigure device at SATA port %d:%d", 13495 sata_device->satadev_addr.cport, 13496 sata_device->satadev_addr.pmport)); 13497 rv = EIO; 13498 } 13499 /* 13500 * The target node devi_state should be marked with 13501 * DEVI_DEVICE_OFFLINE by ndi_devi_offline(). 13502 * This would be the indication for cfgadm that 13503 * the AP node occupant state is 'unconfigured'. 13504 */ 13505 13506 } else { 13507 /* 13508 * This would indicate a failure on the part of cfgadm 13509 * to detect correct state of the node prior to this 13510 * call - one cannot unconfigure non-existing device. 13511 */ 13512 SATA_LOG_D((sata_hba_inst, CE_WARN, 13513 "sata_hba_ioctl: unconfigure: " 13514 "attempt to unconfigure non-existing device " 13515 "at SATA port %d:%d", 13516 sata_device->satadev_addr.cport, 13517 sata_device->satadev_addr.pmport)); 13518 rv = ENXIO; 13519 } 13520 return (rv); 13521 } 13522 13523 /* 13524 * Process sata device configure request 13525 * If port is in a failed state, operation is aborted - one has to use 13526 * an explicit connect or port activate request to try to get a port into 13527 * non-failed mode. Port reset wil also work in such situation. 13528 * If the port is in disconnected (shutdown) state, the connect operation is 13529 * attempted prior to any other action. 13530 * When port is in the active state, there is a device attached and the target 13531 * node exists, a device was most likely offlined. 13532 * If target node does not exist, a new target node is created. In both cases 13533 * an attempt is made to online (configure) the device. 13534 * 13535 * NOTE: Port multiplier is supported. 13536 */ 13537 static int 13538 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst, 13539 sata_device_t *sata_device) 13540 { 13541 int cport, pmport, qual; 13542 int rval; 13543 boolean_t target = TRUE; 13544 sata_cport_info_t *cportinfo; 13545 sata_pmport_info_t *pmportinfo = NULL; 13546 dev_info_t *tdip; 13547 sata_drive_info_t *sdinfo; 13548 13549 cport = sata_device->satadev_addr.cport; 13550 pmport = sata_device->satadev_addr.pmport; 13551 qual = sata_device->satadev_addr.qual; 13552 13553 /* Get current port state */ 13554 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 13555 (SATA_DIP(sata_hba_inst), sata_device); 13556 13557 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 13558 if (qual == SATA_ADDR_DPMPORT) { 13559 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 13560 mutex_enter(&pmportinfo->pmport_mutex); 13561 sata_update_pmport_info(sata_hba_inst, sata_device); 13562 if (rval != SATA_SUCCESS || 13563 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 13564 /* 13565 * Obviously, device on a failed port is not visible 13566 */ 13567 mutex_exit(&pmportinfo->pmport_mutex); 13568 return (ENXIO); 13569 } 13570 mutex_exit(&pmportinfo->pmport_mutex); 13571 } else { 13572 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 13573 cport)->cport_mutex); 13574 sata_update_port_info(sata_hba_inst, sata_device); 13575 if (rval != SATA_SUCCESS || 13576 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 13577 /* 13578 * Obviously, device on a failed port is not visible 13579 */ 13580 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 13581 cport)->cport_mutex); 13582 return (ENXIO); 13583 } 13584 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 13585 cport)->cport_mutex); 13586 } 13587 13588 if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) { 13589 /* need to activate port */ 13590 target = FALSE; 13591 13592 /* Sanity check */ 13593 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) 13594 return (ENXIO); 13595 13596 /* Just let HBA driver to activate port */ 13597 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 13598 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 13599 /* 13600 * Port activation failure - do not change port state 13601 * unless the state returned by HBA indicates a port 13602 * failure. 13603 */ 13604 if (qual == SATA_ADDR_DPMPORT) { 13605 mutex_enter(&pmportinfo->pmport_mutex); 13606 sata_update_pmport_info(sata_hba_inst, 13607 sata_device); 13608 if (sata_device->satadev_state & 13609 SATA_PSTATE_FAILED) 13610 pmportinfo->pmport_state = 13611 SATA_PSTATE_FAILED; 13612 mutex_exit(&pmportinfo->pmport_mutex); 13613 } else { 13614 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 13615 cport)->cport_mutex); 13616 sata_update_port_info(sata_hba_inst, 13617 sata_device); 13618 if (sata_device->satadev_state & 13619 SATA_PSTATE_FAILED) 13620 cportinfo->cport_state = 13621 SATA_PSTATE_FAILED; 13622 mutex_exit(&SATA_CPORT_INFO( 13623 sata_hba_inst, cport)->cport_mutex); 13624 } 13625 } 13626 SATA_LOG_D((sata_hba_inst, CE_WARN, 13627 "sata_hba_ioctl: configure: " 13628 "failed to activate SATA port %d:%d", 13629 cport, pmport)); 13630 return (EIO); 13631 } 13632 /* 13633 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 13634 * without the hint. 13635 */ 13636 sata_gen_sysevent(sata_hba_inst, 13637 &sata_device->satadev_addr, SE_NO_HINT); 13638 13639 /* Virgin port state */ 13640 if (qual == SATA_ADDR_DPMPORT) { 13641 mutex_enter(&pmportinfo->pmport_mutex); 13642 pmportinfo->pmport_state = 0; 13643 mutex_exit(&pmportinfo->pmport_mutex); 13644 } else { 13645 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 13646 cport)-> cport_mutex); 13647 cportinfo->cport_state = 0; 13648 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 13649 cport)->cport_mutex); 13650 } 13651 /* 13652 * Always reprobe port, to get current device info. 13653 */ 13654 if (sata_reprobe_port(sata_hba_inst, sata_device, 13655 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 13656 return (EIO); 13657 13658 if (sata_device->satadev_type != SATA_DTYPE_NONE && target == FALSE) { 13659 if (qual == SATA_ADDR_DPMPORT) { 13660 /* 13661 * That's the transition from "inactive" port 13662 * to active one with device attached. 13663 */ 13664 sata_log(sata_hba_inst, CE_WARN, 13665 "SATA device detected at port %d:%d", 13666 cport, pmport); 13667 } else { 13668 /* 13669 * When PM is attached to the cport and cport is 13670 * activated, every PM device port needs to be reprobed. 13671 * We need to emit message for all devices detected 13672 * at port multiplier's device ports. 13673 * Add such code here. 13674 * For now, just inform about device attached to 13675 * cport. 13676 */ 13677 sata_log(sata_hba_inst, CE_WARN, 13678 "SATA device detected at port %d", cport); 13679 } 13680 } 13681 13682 /* 13683 * This is where real configuration operation starts. 13684 * 13685 * When PM is attached to the cport and cport is activated, 13686 * devices attached PM device ports may have to be configured 13687 * explicitly. This may change when port multiplier is supported. 13688 * For now, configure only disks and other valid target devices. 13689 */ 13690 if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) { 13691 if (qual == SATA_ADDR_DCPORT) { 13692 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 13693 /* 13694 * A device was not successfully identified 13695 */ 13696 sata_log(sata_hba_inst, CE_WARN, 13697 "Could not identify SATA " 13698 "device at port %d", cport); 13699 } 13700 } else { /* port multiplier device port */ 13701 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 13702 /* 13703 * A device was not successfully identified 13704 */ 13705 sata_log(sata_hba_inst, CE_WARN, 13706 "Could not identify SATA " 13707 "device at port %d:%d", cport, pmport); 13708 } 13709 } 13710 return (ENXIO); /* No device to configure */ 13711 } 13712 13713 /* 13714 * Here we may have a device in reset condition, 13715 * but because we are just configuring it, there is 13716 * no need to process the reset other than just 13717 * to clear device reset condition in the HBA driver. 13718 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will 13719 * cause a first command sent the HBA driver with the request 13720 * to clear device reset condition. 13721 */ 13722 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 13723 if (qual == SATA_ADDR_DPMPORT) 13724 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 13725 else 13726 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 13727 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 13728 if (sdinfo == NULL) { 13729 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 13730 return (ENXIO); 13731 } 13732 if (sdinfo->satadrv_event_flags & 13733 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 13734 sdinfo->satadrv_event_flags = 0; 13735 } 13736 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 13737 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 13738 13739 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 13740 &sata_device->satadev_addr)) != NULL) { 13741 /* 13742 * Target node exists. Verify, that it belongs 13743 * to existing, attached device and not to 13744 * a removed device. 13745 */ 13746 if (sata_check_device_removed(tdip) == B_TRUE) { 13747 if (qual == SATA_ADDR_DPMPORT) 13748 sata_log(sata_hba_inst, CE_WARN, 13749 "SATA device at port %d cannot be " 13750 "configured. " 13751 "Application(s) accessing " 13752 "previously attached device " 13753 "have to release it before newly " 13754 "inserted device can be made accessible.", 13755 cport); 13756 else 13757 sata_log(sata_hba_inst, CE_WARN, 13758 "SATA device at port %d:%d cannot be" 13759 "configured. " 13760 "Application(s) accessing " 13761 "previously attached device " 13762 "have to release it before newly " 13763 "inserted device can be made accessible.", 13764 cport, pmport); 13765 return (EIO); 13766 } 13767 /* 13768 * Device was not removed and re-inserted. 13769 * Try to online it. 13770 */ 13771 if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) { 13772 SATA_LOG_D((sata_hba_inst, CE_WARN, 13773 "sata_hba_ioctl: configure: " 13774 "onlining device at SATA port " 13775 "%d:%d failed", cport, pmport)); 13776 return (EIO); 13777 } 13778 13779 if (qual == SATA_ADDR_DPMPORT) { 13780 mutex_enter(&pmportinfo->pmport_mutex); 13781 pmportinfo->pmport_tgtnode_clean = B_TRUE; 13782 mutex_exit(&pmportinfo->pmport_mutex); 13783 } else { 13784 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 13785 cport)->cport_mutex); 13786 cportinfo-> cport_tgtnode_clean = B_TRUE; 13787 mutex_exit(&SATA_CPORT_INFO( 13788 sata_hba_inst, cport)->cport_mutex); 13789 } 13790 } else { 13791 /* 13792 * No target node - need to create a new target node. 13793 */ 13794 if (qual == SATA_ADDR_DPMPORT) { 13795 mutex_enter(&pmportinfo->pmport_mutex); 13796 pmportinfo->pmport_tgtnode_clean = B_TRUE; 13797 mutex_exit(&pmportinfo->pmport_mutex); 13798 } else { 13799 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 13800 cport_mutex); 13801 cportinfo-> cport_tgtnode_clean = B_TRUE; 13802 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 13803 cport_mutex); 13804 } 13805 13806 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst), 13807 sata_hba_inst, &sata_device->satadev_addr); 13808 if (tdip == NULL) { 13809 /* Configure operation failed */ 13810 SATA_LOG_D((sata_hba_inst, CE_WARN, 13811 "sata_hba_ioctl: configure: " 13812 "configuring SATA device at port %d:%d " 13813 "failed", cport, pmport)); 13814 return (EIO); 13815 } 13816 } 13817 return (0); 13818 } 13819 13820 13821 /* 13822 * Process ioctl deactivate port request. 13823 * Arbitrarily unconfigure attached device, if any. 13824 * Even if the unconfigure fails, proceed with the 13825 * port deactivation. 13826 * 13827 * NOTE: Port Multiplier is supported now. 13828 */ 13829 13830 static int 13831 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst, 13832 sata_device_t *sata_device) 13833 { 13834 int cport, pmport, qual; 13835 int rval, rv = 0; 13836 int npmport; 13837 sata_cport_info_t *cportinfo; 13838 sata_pmport_info_t *pmportinfo; 13839 sata_pmult_info_t *pmultinfo; 13840 dev_info_t *tdip; 13841 sata_drive_info_t *sdinfo = NULL; 13842 sata_device_t subsdevice; 13843 13844 /* Sanity check */ 13845 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) 13846 return (ENOTSUP); 13847 13848 cport = sata_device->satadev_addr.cport; 13849 pmport = sata_device->satadev_addr.pmport; 13850 qual = sata_device->satadev_addr.qual; 13851 13852 /* SCSI_TO_SATA_ADDR_QUAL() translate ap_id into a device qualifier */ 13853 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 13854 if (qual == SATA_ADDR_DCPORT) 13855 qual = SATA_ADDR_CPORT; 13856 else 13857 qual = SATA_ADDR_PMPORT; 13858 13859 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 13860 if (qual == SATA_ADDR_PMPORT) 13861 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 13862 13863 /* 13864 * Processing port multiplier 13865 */ 13866 if (qual == SATA_ADDR_CPORT && 13867 SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) { 13868 mutex_enter(&cportinfo->cport_mutex); 13869 13870 /* Deactivate all sub-deices */ 13871 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 13872 if (pmultinfo != NULL) { 13873 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 13874 sata_hba_inst, cport); npmport++) { 13875 13876 subsdevice.satadev_addr.cport = cport; 13877 subsdevice.satadev_addr.pmport = 13878 (uint8_t)npmport; 13879 subsdevice.satadev_addr.qual = 13880 SATA_ADDR_DPMPORT; 13881 13882 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 13883 "sata_hba_ioctl: deactivate: trying to " 13884 "deactivate SATA port %d:%d", 13885 cport, npmport); 13886 13887 mutex_exit(&cportinfo->cport_mutex); 13888 if (sata_ioctl_deactivate(sata_hba_inst, 13889 &subsdevice) == SATA_SUCCESS) { 13890 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 13891 "[Deactivate] device at port %d:%d " 13892 "successfully.", cport, npmport); 13893 } 13894 mutex_enter(&cportinfo->cport_mutex); 13895 } 13896 } 13897 13898 /* Deactivate the port multiplier now. */ 13899 cportinfo->cport_state &= ~SATA_STATE_READY; 13900 mutex_exit(&cportinfo->cport_mutex); 13901 13902 sata_device->satadev_addr.qual = qual; 13903 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 13904 (SATA_DIP(sata_hba_inst), sata_device); 13905 13906 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 13907 SE_NO_HINT); 13908 13909 mutex_enter(&cportinfo->cport_mutex); 13910 sata_update_port_info(sata_hba_inst, sata_device); 13911 if (rval != SATA_SUCCESS) { 13912 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 13913 cportinfo->cport_state = SATA_PSTATE_FAILED; 13914 } 13915 rv = EIO; 13916 } else { 13917 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 13918 } 13919 mutex_exit(&cportinfo->cport_mutex); 13920 13921 return (rv); 13922 } 13923 13924 /* 13925 * Process non-port-multiplier device - it could be a drive connected 13926 * to a port multiplier port or a controller port. 13927 */ 13928 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 13929 if (qual == SATA_ADDR_CPORT) { 13930 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 13931 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 13932 /* deal only with valid devices */ 13933 if ((cportinfo->cport_dev_type & 13934 SATA_VALID_DEV_TYPE) != 0) 13935 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 13936 } 13937 cportinfo->cport_state &= ~SATA_STATE_READY; 13938 } else { 13939 /* Port multiplier device port */ 13940 mutex_enter(&pmportinfo->pmport_mutex); 13941 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 13942 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE && 13943 (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0) 13944 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 13945 pmportinfo->pmport_state &= ~SATA_STATE_READY; 13946 mutex_exit(&pmportinfo->pmport_mutex); 13947 } 13948 13949 if (sdinfo != NULL) { 13950 /* 13951 * If a target node exists, try to offline a device and 13952 * to remove a target node. 13953 */ 13954 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 13955 cport_mutex); 13956 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 13957 &sata_device->satadev_addr); 13958 if (tdip != NULL) { 13959 /* target node exist */ 13960 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 13961 "sata_hba_ioctl: port deactivate: " 13962 "target node exists.", NULL); 13963 13964 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != 13965 NDI_SUCCESS) { 13966 SATA_LOG_D((sata_hba_inst, CE_WARN, 13967 "sata_hba_ioctl: port deactivate: " 13968 "failed to unconfigure device at port " 13969 "%d:%d before deactivating the port", 13970 cport, pmport)); 13971 /* 13972 * Set DEVICE REMOVED state in the target 13973 * node. It will prevent an access to 13974 * the device even when a new device is 13975 * attached, until the old target node is 13976 * released, removed and recreated for a new 13977 * device. 13978 */ 13979 sata_set_device_removed(tdip); 13980 13981 /* 13982 * Instruct the event daemon to try the 13983 * target node cleanup later. 13984 */ 13985 sata_set_target_node_cleanup(sata_hba_inst, 13986 &sata_device->satadev_addr); 13987 } 13988 } 13989 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 13990 cport_mutex); 13991 /* 13992 * In any case, remove and release sata_drive_info 13993 * structure. 13994 */ 13995 if (qual == SATA_ADDR_CPORT) { 13996 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 13997 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 13998 } else { /* port multiplier device port */ 13999 mutex_enter(&pmportinfo->pmport_mutex); 14000 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 14001 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 14002 mutex_exit(&pmportinfo->pmport_mutex); 14003 } 14004 (void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t)); 14005 } 14006 14007 if (qual == SATA_ADDR_CPORT) { 14008 cportinfo->cport_state &= ~(SATA_STATE_PROBED | 14009 SATA_STATE_PROBING); 14010 } else if (qual == SATA_ADDR_PMPORT) { 14011 mutex_enter(&pmportinfo->pmport_mutex); 14012 pmportinfo->pmport_state &= ~(SATA_STATE_PROBED | 14013 SATA_STATE_PROBING); 14014 mutex_exit(&pmportinfo->pmport_mutex); 14015 } 14016 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14017 14018 /* Just let HBA driver to deactivate port */ 14019 sata_device->satadev_addr.qual = qual; 14020 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 14021 (SATA_DIP(sata_hba_inst), sata_device); 14022 14023 /* 14024 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 14025 * without the hint 14026 */ 14027 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 14028 SE_NO_HINT); 14029 14030 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14031 sata_update_port_info(sata_hba_inst, sata_device); 14032 if (qual == SATA_ADDR_CPORT) { 14033 if (rval != SATA_SUCCESS) { 14034 /* 14035 * Port deactivation failure - do not change port state 14036 * unless the state returned by HBA indicates a port 14037 * failure. 14038 */ 14039 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 14040 SATA_CPORT_STATE(sata_hba_inst, cport) = 14041 SATA_PSTATE_FAILED; 14042 } 14043 SATA_LOG_D((sata_hba_inst, CE_WARN, 14044 "sata_hba_ioctl: port deactivate: " 14045 "cannot deactivate SATA port %d", cport)); 14046 rv = EIO; 14047 } else { 14048 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 14049 } 14050 } else { 14051 mutex_enter(&pmportinfo->pmport_mutex); 14052 if (rval != SATA_SUCCESS) { 14053 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 14054 SATA_PMPORT_STATE(sata_hba_inst, cport, 14055 pmport) = SATA_PSTATE_FAILED; 14056 } 14057 SATA_LOG_D((sata_hba_inst, CE_WARN, 14058 "sata_hba_ioctl: port deactivate: " 14059 "cannot deactivate SATA port %d:%d", 14060 cport, pmport)); 14061 rv = EIO; 14062 } else { 14063 pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN; 14064 } 14065 mutex_exit(&pmportinfo->pmport_mutex); 14066 } 14067 14068 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14069 14070 return (rv); 14071 } 14072 14073 /* 14074 * Process ioctl port activate request. 14075 * 14076 * NOTE: Port multiplier is supported now. 14077 */ 14078 static int 14079 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst, 14080 sata_device_t *sata_device) 14081 { 14082 int cport, pmport, qual; 14083 sata_cport_info_t *cportinfo; 14084 sata_pmport_info_t *pmportinfo = NULL; 14085 boolean_t dev_existed = TRUE; 14086 14087 /* Sanity check */ 14088 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) 14089 return (ENOTSUP); 14090 14091 cport = sata_device->satadev_addr.cport; 14092 pmport = sata_device->satadev_addr.pmport; 14093 qual = sata_device->satadev_addr.qual; 14094 14095 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 14096 14097 /* 14098 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL()) 14099 * is a device. But what we are dealing with is port/pmport. 14100 */ 14101 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 14102 if (qual == SATA_ADDR_DCPORT) 14103 sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT; 14104 else 14105 sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT; 14106 14107 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14108 if (qual == SATA_ADDR_PMPORT) { 14109 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 14110 if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN || 14111 pmportinfo->pmport_dev_type == SATA_DTYPE_NONE) 14112 dev_existed = FALSE; 14113 } else { /* cport */ 14114 if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN || 14115 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 14116 dev_existed = FALSE; 14117 } 14118 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14119 14120 /* Just let HBA driver to activate port, if necessary */ 14121 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 14122 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 14123 /* 14124 * Port activation failure - do not change port state unless 14125 * the state returned by HBA indicates a port failure. 14126 */ 14127 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14128 cport)->cport_mutex); 14129 sata_update_port_info(sata_hba_inst, sata_device); 14130 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 14131 if (qual == SATA_ADDR_PMPORT) { 14132 mutex_enter(&pmportinfo->pmport_mutex); 14133 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 14134 mutex_exit(&pmportinfo->pmport_mutex); 14135 } else 14136 cportinfo->cport_state = SATA_PSTATE_FAILED; 14137 14138 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14139 cport)->cport_mutex); 14140 SATA_LOG_D((sata_hba_inst, CE_WARN, 14141 "sata_hba_ioctl: port activate: cannot activate " 14142 "SATA port %d:%d", cport, pmport)); 14143 return (EIO); 14144 } 14145 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14146 } 14147 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14148 if (qual == SATA_ADDR_PMPORT) { 14149 mutex_enter(&pmportinfo->pmport_mutex); 14150 pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN; 14151 mutex_exit(&pmportinfo->pmport_mutex); 14152 } else 14153 cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN; 14154 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14155 14156 /* 14157 * Re-probe port to find its current state and possibly attached device. 14158 * Port re-probing may change the cportinfo device type if device is 14159 * found attached. 14160 * If port probing failed, the device type would be set to 14161 * SATA_DTYPE_NONE. 14162 */ 14163 (void) sata_reprobe_port(sata_hba_inst, sata_device, 14164 SATA_DEV_IDENTIFY_RETRY); 14165 14166 /* 14167 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 14168 * without the hint. 14169 */ 14170 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 14171 SE_NO_HINT); 14172 14173 if (dev_existed == FALSE) { 14174 if (qual == SATA_ADDR_PMPORT && 14175 pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 14176 /* 14177 * That's the transition from the "inactive" port state 14178 * or the active port without a device attached to the 14179 * active port state with a device attached. 14180 */ 14181 sata_log(sata_hba_inst, CE_WARN, 14182 "SATA device detected at port %d:%d", 14183 cport, pmport); 14184 } else if (qual == SATA_ADDR_CPORT && 14185 cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 14186 /* 14187 * That's the transition from the "inactive" port state 14188 * or the active port without a device attached to the 14189 * active port state with a device attached. 14190 */ 14191 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 14192 sata_log(sata_hba_inst, CE_WARN, 14193 "SATA device detected at port %d", cport); 14194 } else { 14195 sata_log(sata_hba_inst, CE_WARN, 14196 "SATA port multiplier detected at port %d", 14197 cport); 14198 } 14199 } 14200 } 14201 return (0); 14202 } 14203 14204 14205 14206 /* 14207 * Process ioctl reset port request. 14208 * 14209 * NOTE: Port-Multiplier is supported. 14210 */ 14211 static int 14212 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst, 14213 sata_device_t *sata_device) 14214 { 14215 int cport, pmport, qual; 14216 int rv = 0; 14217 14218 cport = sata_device->satadev_addr.cport; 14219 pmport = sata_device->satadev_addr.pmport; 14220 qual = sata_device->satadev_addr.qual; 14221 14222 /* 14223 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL()) 14224 * is a device. But what we are dealing with is port/pmport. 14225 */ 14226 if (qual == SATA_ADDR_DCPORT) 14227 sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT; 14228 else 14229 sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT; 14230 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT); 14231 14232 /* Sanity check */ 14233 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 14234 SATA_LOG_D((sata_hba_inst, CE_WARN, 14235 "sata_hba_ioctl: sata_hba_tran missing required " 14236 "function sata_tran_reset_dport")); 14237 return (ENOTSUP); 14238 } 14239 14240 /* Ask HBA to reset port */ 14241 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 14242 sata_device) != SATA_SUCCESS) { 14243 SATA_LOG_D((sata_hba_inst, CE_WARN, 14244 "sata_hba_ioctl: reset port: failed %d:%d", 14245 cport, pmport)); 14246 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 14247 cport_mutex); 14248 sata_update_port_info(sata_hba_inst, sata_device); 14249 if (qual == SATA_ADDR_CPORT) 14250 SATA_CPORT_STATE(sata_hba_inst, cport) = 14251 SATA_PSTATE_FAILED; 14252 else { 14253 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, 14254 pmport)); 14255 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 14256 SATA_PSTATE_FAILED; 14257 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, 14258 pmport)); 14259 } 14260 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 14261 cport_mutex); 14262 rv = EIO; 14263 } 14264 /* 14265 * Beacuse the port was reset, it should be probed and 14266 * attached device reinitialized. At this point the 14267 * port state is unknown - it's state is HBA-specific. 14268 * Re-probe port to get its state. 14269 */ 14270 if (sata_reprobe_port(sata_hba_inst, sata_device, 14271 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) { 14272 rv = EIO; 14273 } 14274 return (rv); 14275 } 14276 14277 /* 14278 * Process ioctl reset device request. 14279 * 14280 * NOTE: Port multiplier is supported. 14281 */ 14282 static int 14283 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst, 14284 sata_device_t *sata_device) 14285 { 14286 sata_drive_info_t *sdinfo = NULL; 14287 sata_pmult_info_t *pmultinfo = NULL; 14288 int cport, pmport; 14289 int rv = 0; 14290 14291 /* Sanity check */ 14292 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 14293 SATA_LOG_D((sata_hba_inst, CE_WARN, 14294 "sata_hba_ioctl: sata_hba_tran missing required " 14295 "function sata_tran_reset_dport")); 14296 return (ENOTSUP); 14297 } 14298 14299 cport = sata_device->satadev_addr.cport; 14300 pmport = sata_device->satadev_addr.pmport; 14301 14302 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14303 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) { 14304 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == 14305 SATA_DTYPE_PMULT) 14306 pmultinfo = SATA_CPORT_INFO(sata_hba_inst, cport)-> 14307 cport_devp.cport_sata_pmult; 14308 else 14309 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 14310 sata_device->satadev_addr.cport); 14311 } else { /* port multiplier */ 14312 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 14313 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 14314 sata_device->satadev_addr.cport, 14315 sata_device->satadev_addr.pmport); 14316 } 14317 if (sdinfo == NULL && pmultinfo == NULL) { 14318 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14319 return (EINVAL); 14320 } 14321 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14322 14323 /* Ask HBA to reset device */ 14324 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 14325 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 14326 SATA_LOG_D((sata_hba_inst, CE_WARN, 14327 "sata_hba_ioctl: reset device: failed at port %d:%d", 14328 cport, pmport)); 14329 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 14330 cport_mutex); 14331 sata_update_port_info(sata_hba_inst, sata_device); 14332 /* 14333 * Device info structure remains attached. Another device reset 14334 * or port disconnect/connect and re-probing is 14335 * needed to change it's state 14336 */ 14337 if (sdinfo != NULL) { 14338 sdinfo->satadrv_state &= ~SATA_STATE_READY; 14339 sdinfo->satadrv_state |= SATA_DSTATE_FAILED; 14340 } else if (pmultinfo != NULL) { 14341 pmultinfo->pmult_state &= ~SATA_STATE_READY; 14342 pmultinfo->pmult_state |= SATA_DSTATE_FAILED; 14343 } 14344 14345 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14346 rv = EIO; 14347 } 14348 /* 14349 * If attached device was a port multiplier, some extra processing 14350 * may be needed to bring it back. SATA specification requies a 14351 * mandatory software reset on host port to reliably enumerate a port 14352 * multiplier, the HBA driver should handle that after reset 14353 * operation. 14354 */ 14355 return (rv); 14356 } 14357 14358 14359 /* 14360 * Process ioctl reset all request. 14361 */ 14362 static int 14363 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst) 14364 { 14365 sata_device_t sata_device; 14366 int rv = 0; 14367 int tcport; 14368 int tpmport = 0; 14369 14370 sata_device.satadev_rev = SATA_DEVICE_REV; 14371 14372 /* 14373 * There is no protection here for configured devices. 14374 */ 14375 /* Sanity check */ 14376 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 14377 SATA_LOG_D((sata_hba_inst, CE_WARN, 14378 "sata_hba_ioctl: sata_hba_tran missing required " 14379 "function sata_tran_reset_dport")); 14380 return (ENOTSUP); 14381 } 14382 14383 /* 14384 * Need to lock all ports, not just one. 14385 * If any port is locked by event processing, fail the whole operation. 14386 * One port is already locked, but for simplicity lock it again. 14387 */ 14388 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) { 14389 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14390 cport_mutex); 14391 if (((SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14392 cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) { 14393 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14394 cport_mutex); 14395 rv = EBUSY; 14396 break; 14397 } else { 14398 /* 14399 * It is enough to lock cport in command-based 14400 * switching mode. 14401 */ 14402 SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14403 cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 14404 } 14405 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14406 cport_mutex); 14407 } 14408 14409 if (rv == 0) { 14410 /* 14411 * All cports were successfully locked. 14412 * Reset main SATA controller. 14413 * Set the device address to port 0, to have a valid device 14414 * address. 14415 */ 14416 sata_device.satadev_addr.qual = SATA_ADDR_CNTRL; 14417 sata_device.satadev_addr.cport = 0; 14418 sata_device.satadev_addr.pmport = 0; 14419 14420 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 14421 (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) { 14422 SATA_LOG_D((sata_hba_inst, CE_WARN, 14423 "sata_hba_ioctl: reset controller failed")); 14424 return (EIO); 14425 } 14426 /* 14427 * Because ports were reset, port states are unknown. 14428 * They should be re-probed to get their state and 14429 * attached devices should be reinitialized. 14430 */ 14431 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); 14432 tcport++) { 14433 sata_device.satadev_addr.cport = tcport; 14434 sata_device.satadev_addr.pmport = tpmport; 14435 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 14436 14437 /* 14438 * The sata_reprobe_port() will mark a 14439 * SATA_EVNT_DEVICE_RESET event on the port 14440 * multiplier, all its sub-ports will be probed by 14441 * sata daemon afterwards. 14442 */ 14443 if (sata_reprobe_port(sata_hba_inst, &sata_device, 14444 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 14445 rv = EIO; 14446 } 14447 } 14448 /* 14449 * Unlock all ports 14450 */ 14451 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) { 14452 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14453 cport_mutex); 14454 SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14455 cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY; 14456 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14457 cport_mutex); 14458 } 14459 14460 /* 14461 * This operation returns EFAULT if either reset 14462 * controller failed or a re-probing of any port failed. 14463 */ 14464 return (rv); 14465 } 14466 14467 14468 /* 14469 * Process ioctl port self test request. 14470 * 14471 * NOTE: Port multiplier code is not completed nor tested. 14472 */ 14473 static int 14474 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst, 14475 sata_device_t *sata_device) 14476 { 14477 int cport, pmport, qual; 14478 int rv = 0; 14479 14480 /* Sanity check */ 14481 if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL) 14482 return (ENOTSUP); 14483 14484 cport = sata_device->satadev_addr.cport; 14485 pmport = sata_device->satadev_addr.pmport; 14486 qual = sata_device->satadev_addr.qual; 14487 14488 /* 14489 * There is no protection here for a configured 14490 * device attached to this port. 14491 */ 14492 14493 if ((*SATA_SELFTEST_FUNC(sata_hba_inst)) 14494 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 14495 SATA_LOG_D((sata_hba_inst, CE_WARN, 14496 "sata_hba_ioctl: port selftest: " 14497 "failed port %d:%d", cport, pmport)); 14498 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 14499 cport_mutex); 14500 sata_update_port_info(sata_hba_inst, sata_device); 14501 if (qual == SATA_ADDR_CPORT) 14502 SATA_CPORT_STATE(sata_hba_inst, cport) = 14503 SATA_PSTATE_FAILED; 14504 else { /* port multiplier device port */ 14505 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, 14506 cport, pmport)); 14507 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 14508 SATA_PSTATE_FAILED; 14509 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, 14510 cport, pmport)); 14511 } 14512 14513 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 14514 cport_mutex); 14515 return (EIO); 14516 } 14517 /* 14518 * Beacuse the port was reset in the course of testing, it should be 14519 * re-probed and attached device state should be restored. At this 14520 * point the port state is unknown - it's state is HBA-specific. 14521 * Force port re-probing to get it into a known state. 14522 */ 14523 if (sata_reprobe_port(sata_hba_inst, sata_device, 14524 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 14525 rv = EIO; 14526 return (rv); 14527 } 14528 14529 14530 /* 14531 * sata_cfgadm_state: 14532 * Use the sata port state and state of the target node to figure out 14533 * the cfgadm_state. 14534 * 14535 * The port argument is a value with encoded cport, 14536 * pmport and address qualifier, in the same manner as a scsi target number. 14537 * SCSI_TO_SATA_CPORT macro extracts cport number, 14538 * SCSI_TO_SATA_PMPORT extracts pmport number and 14539 * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag. 14540 * 14541 * Port multiplier is supported. 14542 */ 14543 14544 static void 14545 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port, 14546 devctl_ap_state_t *ap_state) 14547 { 14548 uint8_t cport, pmport, qual; 14549 uint32_t port_state, pmult_state; 14550 uint32_t dev_type; 14551 sata_drive_info_t *sdinfo; 14552 14553 cport = SCSI_TO_SATA_CPORT(port); 14554 pmport = SCSI_TO_SATA_PMPORT(port); 14555 qual = SCSI_TO_SATA_ADDR_QUAL(port); 14556 14557 /* Check cport state */ 14558 port_state = SATA_CPORT_STATE(sata_hba_inst, cport); 14559 if (port_state & SATA_PSTATE_SHUTDOWN || 14560 port_state & SATA_PSTATE_FAILED) { 14561 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 14562 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 14563 if (port_state & SATA_PSTATE_FAILED) 14564 ap_state->ap_condition = AP_COND_FAILED; 14565 else 14566 ap_state->ap_condition = AP_COND_UNKNOWN; 14567 14568 return; 14569 } 14570 14571 /* cport state is okay. Now check pmport state */ 14572 if (qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) { 14573 /* Sanity check */ 14574 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 14575 SATA_DTYPE_PMULT || SATA_PMPORT_INFO(sata_hba_inst, 14576 cport, pmport) == NULL) 14577 return; 14578 port_state = SATA_PMPORT_STATE(sata_hba_inst, cport, pmport); 14579 if (port_state & SATA_PSTATE_SHUTDOWN || 14580 port_state & SATA_PSTATE_FAILED) { 14581 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 14582 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 14583 if (port_state & SATA_PSTATE_FAILED) 14584 ap_state->ap_condition = AP_COND_FAILED; 14585 else 14586 ap_state->ap_condition = AP_COND_UNKNOWN; 14587 14588 return; 14589 } 14590 } 14591 14592 /* Port is enabled and ready */ 14593 if (qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_CPORT) 14594 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, cport); 14595 else 14596 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport); 14597 14598 switch (dev_type) { 14599 case SATA_DTYPE_NONE: 14600 { 14601 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 14602 ap_state->ap_condition = AP_COND_OK; 14603 /* No device attached */ 14604 ap_state->ap_rstate = AP_RSTATE_EMPTY; 14605 break; 14606 } 14607 case SATA_DTYPE_PMULT: 14608 { 14609 /* Need to check port multiplier state */ 14610 ASSERT(qual == SATA_ADDR_DCPORT); 14611 pmult_state = SATA_PMULT_INFO(sata_hba_inst, cport)-> 14612 pmult_state; 14613 if (pmult_state & (SATA_PSTATE_SHUTDOWN|SATA_PSTATE_FAILED)) { 14614 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 14615 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 14616 if (pmult_state & SATA_PSTATE_FAILED) 14617 ap_state->ap_condition = AP_COND_FAILED; 14618 else 14619 ap_state->ap_condition = AP_COND_UNKNOWN; 14620 14621 return; 14622 } 14623 14624 /* Port multiplier is not configurable */ 14625 ap_state->ap_ostate = AP_OSTATE_CONFIGURED; 14626 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 14627 ap_state->ap_condition = AP_COND_OK; 14628 break; 14629 } 14630 14631 case SATA_DTYPE_ATADISK: 14632 case SATA_DTYPE_ATAPICD: 14633 case SATA_DTYPE_ATAPITAPE: 14634 case SATA_DTYPE_ATAPIDISK: 14635 { 14636 dev_info_t *tdip = NULL; 14637 dev_info_t *dip = NULL; 14638 int circ; 14639 14640 dip = SATA_DIP(sata_hba_inst); 14641 tdip = sata_get_target_dip(dip, cport, pmport); 14642 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 14643 if (tdip != NULL) { 14644 ndi_devi_enter(dip, &circ); 14645 mutex_enter(&(DEVI(tdip)->devi_lock)); 14646 if (DEVI_IS_DEVICE_REMOVED(tdip)) { 14647 /* 14648 * There could be the case where previously 14649 * configured and opened device was removed 14650 * and unknown device was plugged. 14651 * In such case we want to show a device, and 14652 * its configured or unconfigured state but 14653 * indicate unusable condition untill the 14654 * old target node is released and removed. 14655 */ 14656 ap_state->ap_condition = AP_COND_UNUSABLE; 14657 } else { 14658 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, 14659 cport)); 14660 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 14661 cport); 14662 if (sdinfo != NULL) { 14663 if ((sdinfo->satadrv_state & 14664 SATA_DSTATE_FAILED) != 0) 14665 ap_state->ap_condition = 14666 AP_COND_FAILED; 14667 else 14668 ap_state->ap_condition = 14669 AP_COND_OK; 14670 } else { 14671 ap_state->ap_condition = 14672 AP_COND_UNKNOWN; 14673 } 14674 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, 14675 cport)); 14676 } 14677 if ((DEVI_IS_DEVICE_OFFLINE(tdip)) || 14678 (DEVI_IS_DEVICE_DOWN(tdip))) { 14679 ap_state->ap_ostate = 14680 AP_OSTATE_UNCONFIGURED; 14681 } else { 14682 ap_state->ap_ostate = 14683 AP_OSTATE_CONFIGURED; 14684 } 14685 mutex_exit(&(DEVI(tdip)->devi_lock)); 14686 ndi_devi_exit(dip, circ); 14687 } else { 14688 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 14689 ap_state->ap_condition = AP_COND_UNKNOWN; 14690 } 14691 break; 14692 } 14693 default: 14694 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 14695 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 14696 ap_state->ap_condition = AP_COND_UNKNOWN; 14697 /* 14698 * This is actually internal error condition (non fatal), 14699 * because we have already checked all defined device types. 14700 */ 14701 SATA_LOG_D((sata_hba_inst, CE_WARN, 14702 "sata_cfgadm_state: Internal error: " 14703 "unknown device type")); 14704 break; 14705 } 14706 } 14707 14708 14709 /* 14710 * Process ioctl get device path request. 14711 * 14712 * NOTE: Port multiplier has no target dip. Devices connected to port 14713 * multiplier have target node attached to the HBA node. The only difference 14714 * between them and the directly-attached device node is a target address. 14715 */ 14716 static int 14717 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst, 14718 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 14719 { 14720 char path[MAXPATHLEN]; 14721 uint32_t size; 14722 dev_info_t *tdip; 14723 14724 (void) strcpy(path, "/devices"); 14725 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 14726 &sata_device->satadev_addr)) == NULL) { 14727 /* 14728 * No such device. If this is a request for a size, do not 14729 * return EINVAL for non-existing target, because cfgadm 14730 * will then indicate a meaningless ioctl failure. 14731 * If this is a request for a path, indicate invalid 14732 * argument. 14733 */ 14734 if (ioc->get_size == 0) 14735 return (EINVAL); 14736 } else { 14737 (void) ddi_pathname(tdip, path + strlen(path)); 14738 } 14739 size = strlen(path) + 1; 14740 14741 if (ioc->get_size != 0) { 14742 if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz, 14743 mode) != 0) 14744 return (EFAULT); 14745 } else { 14746 if (ioc->bufsiz != size) 14747 return (EINVAL); 14748 14749 else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz, 14750 mode) != 0) 14751 return (EFAULT); 14752 } 14753 return (0); 14754 } 14755 14756 /* 14757 * Process ioctl get attachment point type request. 14758 * 14759 * NOTE: Port multiplier is supported. 14760 */ 14761 static int 14762 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst, 14763 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 14764 { 14765 uint32_t type_len; 14766 const char *ap_type; 14767 int dev_type; 14768 14769 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 14770 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, 14771 sata_device->satadev_addr.cport); 14772 else /* pmport */ 14773 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, 14774 sata_device->satadev_addr.cport, 14775 sata_device->satadev_addr.pmport); 14776 14777 switch (dev_type) { 14778 case SATA_DTYPE_NONE: 14779 ap_type = "port"; 14780 break; 14781 14782 case SATA_DTYPE_ATADISK: 14783 case SATA_DTYPE_ATAPIDISK: 14784 ap_type = "disk"; 14785 break; 14786 14787 case SATA_DTYPE_ATAPICD: 14788 ap_type = "cd/dvd"; 14789 break; 14790 14791 case SATA_DTYPE_ATAPITAPE: 14792 ap_type = "tape"; 14793 break; 14794 14795 case SATA_DTYPE_PMULT: 14796 ap_type = "sata-pmult"; 14797 break; 14798 14799 case SATA_DTYPE_UNKNOWN: 14800 ap_type = "unknown"; 14801 break; 14802 14803 default: 14804 ap_type = "unsupported"; 14805 break; 14806 14807 } /* end of dev_type switch */ 14808 14809 type_len = strlen(ap_type) + 1; 14810 14811 if (ioc->get_size) { 14812 if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz, 14813 mode) != 0) 14814 return (EFAULT); 14815 } else { 14816 if (ioc->bufsiz != type_len) 14817 return (EINVAL); 14818 14819 if (ddi_copyout((void *)ap_type, ioc->buf, 14820 ioc->bufsiz, mode) != 0) 14821 return (EFAULT); 14822 } 14823 return (0); 14824 14825 } 14826 14827 /* 14828 * Process ioctl get device model info request. 14829 * This operation should return to cfgadm the device model 14830 * information string 14831 * 14832 * NOTE: Port multiplier is supported. 14833 */ 14834 static int 14835 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst, 14836 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 14837 { 14838 sata_drive_info_t *sdinfo; 14839 uint32_t info_len; 14840 char ap_info[SATA_ID_MODEL_LEN + 1]; 14841 14842 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14843 sata_device->satadev_addr.cport)->cport_mutex); 14844 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 14845 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 14846 sata_device->satadev_addr.cport); 14847 else /* port multiplier */ 14848 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 14849 sata_device->satadev_addr.cport, 14850 sata_device->satadev_addr.pmport); 14851 if (sdinfo == NULL) { 14852 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14853 sata_device->satadev_addr.cport)->cport_mutex); 14854 return (EINVAL); 14855 } 14856 14857 #ifdef _LITTLE_ENDIAN 14858 swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN); 14859 #else /* _LITTLE_ENDIAN */ 14860 bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN); 14861 #endif /* _LITTLE_ENDIAN */ 14862 14863 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14864 sata_device->satadev_addr.cport)->cport_mutex); 14865 14866 ap_info[SATA_ID_MODEL_LEN] = '\0'; 14867 14868 info_len = strlen(ap_info) + 1; 14869 14870 if (ioc->get_size) { 14871 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 14872 mode) != 0) 14873 return (EFAULT); 14874 } else { 14875 if (ioc->bufsiz < info_len) 14876 return (EINVAL); 14877 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 14878 mode) != 0) 14879 return (EFAULT); 14880 } 14881 return (0); 14882 } 14883 14884 14885 /* 14886 * Process ioctl get device firmware revision info request. 14887 * This operation should return to cfgadm the device firmware revision 14888 * information string 14889 * 14890 * Port multiplier is supported. 14891 */ 14892 static int 14893 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst, 14894 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 14895 { 14896 sata_drive_info_t *sdinfo; 14897 uint32_t info_len; 14898 char ap_info[SATA_ID_FW_LEN + 1]; 14899 14900 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14901 sata_device->satadev_addr.cport)->cport_mutex); 14902 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 14903 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 14904 sata_device->satadev_addr.cport); 14905 else /* port multiplier */ 14906 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 14907 sata_device->satadev_addr.cport, 14908 sata_device->satadev_addr.pmport); 14909 if (sdinfo == NULL) { 14910 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14911 sata_device->satadev_addr.cport)->cport_mutex); 14912 return (EINVAL); 14913 } 14914 14915 #ifdef _LITTLE_ENDIAN 14916 swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN); 14917 #else /* _LITTLE_ENDIAN */ 14918 bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN); 14919 #endif /* _LITTLE_ENDIAN */ 14920 14921 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14922 sata_device->satadev_addr.cport)->cport_mutex); 14923 14924 ap_info[SATA_ID_FW_LEN] = '\0'; 14925 14926 info_len = strlen(ap_info) + 1; 14927 14928 if (ioc->get_size) { 14929 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 14930 mode) != 0) 14931 return (EFAULT); 14932 } else { 14933 if (ioc->bufsiz < info_len) 14934 return (EINVAL); 14935 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 14936 mode) != 0) 14937 return (EFAULT); 14938 } 14939 return (0); 14940 } 14941 14942 14943 /* 14944 * Process ioctl get device serial number info request. 14945 * This operation should return to cfgadm the device serial number string. 14946 * 14947 * NOTE: Port multiplier is supported. 14948 */ 14949 static int 14950 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst, 14951 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 14952 { 14953 sata_drive_info_t *sdinfo; 14954 uint32_t info_len; 14955 char ap_info[SATA_ID_SERIAL_LEN + 1]; 14956 14957 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14958 sata_device->satadev_addr.cport)->cport_mutex); 14959 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 14960 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 14961 sata_device->satadev_addr.cport); 14962 else /* port multiplier */ 14963 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 14964 sata_device->satadev_addr.cport, 14965 sata_device->satadev_addr.pmport); 14966 if (sdinfo == NULL) { 14967 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14968 sata_device->satadev_addr.cport)->cport_mutex); 14969 return (EINVAL); 14970 } 14971 14972 #ifdef _LITTLE_ENDIAN 14973 swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN); 14974 #else /* _LITTLE_ENDIAN */ 14975 bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN); 14976 #endif /* _LITTLE_ENDIAN */ 14977 14978 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14979 sata_device->satadev_addr.cport)->cport_mutex); 14980 14981 ap_info[SATA_ID_SERIAL_LEN] = '\0'; 14982 14983 info_len = strlen(ap_info) + 1; 14984 14985 if (ioc->get_size) { 14986 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 14987 mode) != 0) 14988 return (EFAULT); 14989 } else { 14990 if (ioc->bufsiz < info_len) 14991 return (EINVAL); 14992 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 14993 mode) != 0) 14994 return (EFAULT); 14995 } 14996 return (0); 14997 } 14998 14999 15000 /* 15001 * Preset scsi extended sense data (to NO SENSE) 15002 * First 18 bytes of the sense data are preset to current valid sense 15003 * with a key NO SENSE data. 15004 * 15005 * Returns void 15006 */ 15007 static void 15008 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense) 15009 { 15010 sense->es_valid = 1; /* Valid sense */ 15011 sense->es_class = CLASS_EXTENDED_SENSE; /* 0x70 - current err */ 15012 sense->es_key = KEY_NO_SENSE; 15013 sense->es_info_1 = 0; 15014 sense->es_info_2 = 0; 15015 sense->es_info_3 = 0; 15016 sense->es_info_4 = 0; 15017 sense->es_add_len = 10; /* Additional length - replace with a def */ 15018 sense->es_cmd_info[0] = 0; 15019 sense->es_cmd_info[1] = 0; 15020 sense->es_cmd_info[2] = 0; 15021 sense->es_cmd_info[3] = 0; 15022 sense->es_add_code = 0; 15023 sense->es_qual_code = 0; 15024 } 15025 15026 /* 15027 * Register a legacy cmdk-style devid for the target (disk) device. 15028 * 15029 * Note: This function is called only when the HBA devinfo node has the 15030 * property "use-cmdk-devid-format" set. This property indicates that 15031 * devid compatible with old cmdk (target) driver is to be generated 15032 * for any target device attached to this controller. This will take 15033 * precedence over the devid generated by sd (target) driver. 15034 * This function is derived from cmdk_devid_setup() function in cmdk.c. 15035 */ 15036 static void 15037 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo) 15038 { 15039 char *hwid; 15040 int modlen; 15041 int serlen; 15042 int rval; 15043 ddi_devid_t devid; 15044 15045 /* 15046 * device ID is a concatanation of model number, "=", serial number. 15047 */ 15048 hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP); 15049 bcopy(&sdinfo->satadrv_id.ai_model, hwid, 15050 sizeof (sdinfo->satadrv_id.ai_model)); 15051 swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model)); 15052 modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model)); 15053 if (modlen == 0) 15054 goto err; 15055 hwid[modlen++] = '='; 15056 bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen], 15057 sizeof (sdinfo->satadrv_id.ai_drvser)); 15058 swab(&hwid[modlen], &hwid[modlen], 15059 sizeof (sdinfo->satadrv_id.ai_drvser)); 15060 serlen = sata_check_modser(&hwid[modlen], 15061 sizeof (sdinfo->satadrv_id.ai_drvser)); 15062 if (serlen == 0) 15063 goto err; 15064 hwid[modlen + serlen] = 0; /* terminate the hwid string */ 15065 15066 /* initialize/register devid */ 15067 if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL, 15068 (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) { 15069 rval = ddi_devid_register(dip, devid); 15070 /* 15071 * Free up the allocated devid buffer. 15072 * NOTE: This doesn't mean unregistering devid. 15073 */ 15074 ddi_devid_free(devid); 15075 } 15076 15077 if (rval != DDI_SUCCESS) 15078 cmn_err(CE_WARN, "sata: failed to create devid for the disk" 15079 " on port %d", sdinfo->satadrv_addr.cport); 15080 err: 15081 kmem_free(hwid, LEGACY_HWID_LEN); 15082 } 15083 15084 /* 15085 * valid model/serial string must contain a non-zero non-space characters. 15086 * trim trailing spaces/NULLs. 15087 */ 15088 static int 15089 sata_check_modser(char *buf, int buf_len) 15090 { 15091 boolean_t ret; 15092 char *s; 15093 int i; 15094 int tb; 15095 char ch; 15096 15097 ret = B_FALSE; 15098 s = buf; 15099 for (i = 0; i < buf_len; i++) { 15100 ch = *s++; 15101 if (ch != ' ' && ch != '\0') 15102 tb = i + 1; 15103 if (ch != ' ' && ch != '\0' && ch != '0') 15104 ret = B_TRUE; 15105 } 15106 15107 if (ret == B_FALSE) 15108 return (0); /* invalid string */ 15109 15110 return (tb); /* return length */ 15111 } 15112 15113 /* 15114 * sata_set_drive_features function compares current device features setting 15115 * with the saved device features settings and, if there is a difference, 15116 * it restores device features setting to the previously saved state. 15117 * It also arbitrarily tries to select the highest supported DMA mode. 15118 * Device Identify or Identify Packet Device data has to be current. 15119 * At the moment read ahead and write cache are considered for all devices. 15120 * For atapi devices, Removable Media Status Notification is set in addition 15121 * to common features. 15122 * 15123 * This function cannot be called in the interrupt context (it may sleep). 15124 * 15125 * The input argument sdinfo should point to the drive info structure 15126 * to be updated after features are set. Note, that only 15127 * device (packet) identify data is updated, not the flags indicating the 15128 * supported features. 15129 * 15130 * Returns SATA_SUCCESS if successful or there was nothing to do. 15131 * Device Identify data in the drive info structure pointed to by the sdinfo 15132 * arguments is updated even when no features were set or changed. 15133 * 15134 * Returns SATA_FAILURE if device features could not be set or DMA mode 15135 * for a disk cannot be set and device identify data cannot be fetched. 15136 * 15137 * Returns SATA_RETRY if device features could not be set (other than disk 15138 * DMA mode) but the device identify data was fetched successfully. 15139 * 15140 * Note: This function may fail the port, making it inaccessible. 15141 * In such case the explicit port disconnect/connect or physical device 15142 * detach/attach is required to re-evaluate port state again. 15143 */ 15144 15145 static int 15146 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst, 15147 sata_drive_info_t *sdinfo, int restore) 15148 { 15149 int rval = SATA_SUCCESS; 15150 int rval_set; 15151 sata_drive_info_t new_sdinfo; 15152 char *finfo = "sata_set_drive_features: cannot"; 15153 char *finfox; 15154 int cache_op; 15155 15156 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 15157 new_sdinfo.satadrv_addr = sdinfo->satadrv_addr; 15158 new_sdinfo.satadrv_type = sdinfo->satadrv_type; 15159 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 15160 /* 15161 * Cannot get device identification - caller may retry later 15162 */ 15163 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15164 "%s fetch device identify data\n", finfo); 15165 return (SATA_FAILURE); 15166 } 15167 finfox = (restore != 0) ? " restore device features" : 15168 " initialize device features\n"; 15169 15170 switch (sdinfo->satadrv_type) { 15171 case SATA_DTYPE_ATADISK: 15172 /* Arbitrarily set UDMA mode */ 15173 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) != 15174 SATA_SUCCESS) { 15175 SATA_LOG_D((sata_hba_inst, CE_WARN, 15176 "%s set UDMA mode\n", finfo)); 15177 return (SATA_FAILURE); 15178 } 15179 break; 15180 case SATA_DTYPE_ATAPICD: 15181 case SATA_DTYPE_ATAPITAPE: 15182 case SATA_DTYPE_ATAPIDISK: 15183 /* Set Removable Media Status Notification, if necessary */ 15184 if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) && 15185 restore != 0) { 15186 if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) && 15187 (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))|| 15188 ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) && 15189 SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) { 15190 /* Current setting does not match saved one */ 15191 if (sata_set_rmsn(sata_hba_inst, sdinfo, 15192 sdinfo->satadrv_settings & 15193 SATA_DEV_RMSN) != SATA_SUCCESS) 15194 rval = SATA_FAILURE; 15195 } 15196 } 15197 /* 15198 * We have to set Multiword DMA or UDMA, if it is supported, as 15199 * we want to use DMA transfer mode whenever possible. 15200 * Some devices require explicit setting of the DMA mode. 15201 */ 15202 if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) { 15203 /* Set highest supported DMA mode */ 15204 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) != 15205 SATA_SUCCESS) { 15206 SATA_LOG_D((sata_hba_inst, CE_WARN, 15207 "%s set UDMA mode\n", finfo)); 15208 rval = SATA_FAILURE; 15209 } 15210 } 15211 break; 15212 } 15213 15214 if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) && 15215 !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) { 15216 /* 15217 * neither READ AHEAD nor WRITE CACHE is supported 15218 * - do nothing 15219 */ 15220 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15221 "settable features not supported\n", NULL); 15222 goto update_sdinfo; 15223 } 15224 15225 if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) && 15226 (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) && 15227 (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) && 15228 (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) { 15229 /* 15230 * both READ AHEAD and WRITE CACHE are enabled 15231 * - Nothing to do 15232 */ 15233 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15234 "no device features to set\n", NULL); 15235 goto update_sdinfo; 15236 } 15237 15238 cache_op = 0; 15239 15240 if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) { 15241 if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) && 15242 !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) { 15243 /* Enable read ahead / read cache */ 15244 cache_op = SATAC_SF_ENABLE_READ_AHEAD; 15245 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15246 "enabling read cache\n", NULL); 15247 } else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) && 15248 SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) { 15249 /* Disable read ahead / read cache */ 15250 cache_op = SATAC_SF_DISABLE_READ_AHEAD; 15251 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15252 "disabling read cache\n", NULL); 15253 } 15254 15255 if (cache_op != 0) { 15256 /* Try to set read cache mode */ 15257 rval_set = sata_set_cache_mode(sata_hba_inst, 15258 &new_sdinfo, cache_op); 15259 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS) 15260 rval = rval_set; 15261 } 15262 } 15263 15264 cache_op = 0; 15265 15266 if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) { 15267 if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) && 15268 !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) { 15269 /* Enable write cache */ 15270 cache_op = SATAC_SF_ENABLE_WRITE_CACHE; 15271 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15272 "enabling write cache\n", NULL); 15273 } else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) && 15274 SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) { 15275 /* Disable write cache */ 15276 cache_op = SATAC_SF_DISABLE_WRITE_CACHE; 15277 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15278 "disabling write cache\n", NULL); 15279 } 15280 15281 if (cache_op != 0) { 15282 /* Try to set write cache mode */ 15283 rval_set = sata_set_cache_mode(sata_hba_inst, 15284 &new_sdinfo, cache_op); 15285 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS) 15286 rval = rval_set; 15287 } 15288 } 15289 if (rval != SATA_SUCCESS) 15290 SATA_LOG_D((sata_hba_inst, CE_WARN, 15291 "%s %s", finfo, finfox)); 15292 15293 update_sdinfo: 15294 /* 15295 * We need to fetch Device Identify data again 15296 */ 15297 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 15298 /* 15299 * Cannot get device identification - retry later 15300 */ 15301 SATA_LOG_D((sata_hba_inst, CE_WARN, 15302 "%s re-fetch device identify data\n", finfo)); 15303 rval = SATA_FAILURE; 15304 } 15305 /* Copy device sata info. */ 15306 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 15307 15308 return (rval); 15309 } 15310 15311 15312 /* 15313 * 15314 * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if 15315 * unable to determine. 15316 * 15317 * Cannot be called in an interrupt context. 15318 * 15319 * Called by sata_build_lsense_page_2f() 15320 */ 15321 15322 static int 15323 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst, 15324 sata_drive_info_t *sdinfo) 15325 { 15326 sata_pkt_t *spkt; 15327 sata_cmd_t *scmd; 15328 sata_pkt_txlate_t *spx; 15329 int rval; 15330 15331 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 15332 spx->txlt_sata_hba_inst = sata_hba_inst; 15333 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 15334 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 15335 if (spkt == NULL) { 15336 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15337 return (-1); 15338 } 15339 /* address is needed now */ 15340 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15341 15342 15343 /* Fill sata_pkt */ 15344 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15345 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 15346 /* Synchronous mode, no callback */ 15347 spkt->satapkt_comp = NULL; 15348 /* Timeout 30s */ 15349 spkt->satapkt_time = sata_default_pkt_time; 15350 15351 scmd = &spkt->satapkt_cmd; 15352 scmd->satacmd_flags.sata_special_regs = B_TRUE; 15353 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 15354 15355 /* Set up which registers need to be returned */ 15356 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE; 15357 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE; 15358 15359 /* Build SMART_RETURN_STATUS cmd in the sata_pkt */ 15360 scmd->satacmd_addr_type = 0; /* N/A */ 15361 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 15362 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 15363 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 15364 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 15365 scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS; 15366 scmd->satacmd_device_reg = 0; /* Always device 0 */ 15367 scmd->satacmd_cmd_reg = SATAC_SMART; 15368 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 15369 sdinfo->satadrv_addr.cport))); 15370 15371 15372 /* Send pkt to SATA HBA driver */ 15373 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 15374 SATA_TRAN_ACCEPTED || 15375 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 15376 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15377 sdinfo->satadrv_addr.cport))); 15378 /* 15379 * Whoops, no SMART RETURN STATUS 15380 */ 15381 rval = -1; 15382 } else { 15383 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15384 sdinfo->satadrv_addr.cport))); 15385 if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) { 15386 rval = -1; 15387 goto fail; 15388 } 15389 if (scmd->satacmd_status_reg & SATA_STATUS_ERR) { 15390 rval = -1; 15391 goto fail; 15392 } 15393 if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) && 15394 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2)) 15395 rval = 0; 15396 else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) && 15397 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4)) 15398 rval = 1; 15399 else { 15400 rval = -1; 15401 goto fail; 15402 } 15403 } 15404 fail: 15405 /* Free allocated resources */ 15406 sata_pkt_free(spx); 15407 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15408 15409 return (rval); 15410 } 15411 15412 /* 15413 * 15414 * Returns 0 if succeeded, -1 otherwise 15415 * 15416 * Cannot be called in an interrupt context. 15417 * 15418 */ 15419 static int 15420 sata_fetch_smart_data( 15421 sata_hba_inst_t *sata_hba_inst, 15422 sata_drive_info_t *sdinfo, 15423 struct smart_data *smart_data) 15424 { 15425 sata_pkt_t *spkt; 15426 sata_cmd_t *scmd; 15427 sata_pkt_txlate_t *spx; 15428 int rval; 15429 15430 #if ! defined(lint) 15431 ASSERT(sizeof (struct smart_data) == 512); 15432 #endif 15433 15434 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 15435 spx->txlt_sata_hba_inst = sata_hba_inst; 15436 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 15437 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 15438 if (spkt == NULL) { 15439 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15440 return (-1); 15441 } 15442 /* address is needed now */ 15443 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15444 15445 15446 /* Fill sata_pkt */ 15447 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15448 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 15449 /* Synchronous mode, no callback */ 15450 spkt->satapkt_comp = NULL; 15451 /* Timeout 30s */ 15452 spkt->satapkt_time = sata_default_pkt_time; 15453 15454 scmd = &spkt->satapkt_cmd; 15455 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 15456 15457 /* 15458 * Allocate buffer for SMART data 15459 */ 15460 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 15461 sizeof (struct smart_data)); 15462 if (scmd->satacmd_bp == NULL) { 15463 sata_pkt_free(spx); 15464 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15465 SATA_LOG_D((sata_hba_inst, CE_WARN, 15466 "sata_fetch_smart_data: " 15467 "cannot allocate buffer")); 15468 return (-1); 15469 } 15470 15471 15472 /* Build SMART_READ_DATA cmd in the sata_pkt */ 15473 scmd->satacmd_addr_type = 0; /* N/A */ 15474 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 15475 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 15476 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 15477 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 15478 scmd->satacmd_features_reg = SATA_SMART_READ_DATA; 15479 scmd->satacmd_device_reg = 0; /* Always device 0 */ 15480 scmd->satacmd_cmd_reg = SATAC_SMART; 15481 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 15482 sdinfo->satadrv_addr.cport))); 15483 15484 /* Send pkt to SATA HBA driver */ 15485 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 15486 SATA_TRAN_ACCEPTED || 15487 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 15488 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15489 sdinfo->satadrv_addr.cport))); 15490 /* 15491 * Whoops, no SMART DATA available 15492 */ 15493 rval = -1; 15494 goto fail; 15495 } else { 15496 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15497 sdinfo->satadrv_addr.cport))); 15498 if (spx->txlt_buf_dma_handle != NULL) { 15499 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 15500 DDI_DMA_SYNC_FORKERNEL); 15501 ASSERT(rval == DDI_SUCCESS); 15502 } 15503 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data, 15504 sizeof (struct smart_data)); 15505 } 15506 15507 fail: 15508 /* Free allocated resources */ 15509 sata_free_local_buffer(spx); 15510 sata_pkt_free(spx); 15511 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15512 15513 return (rval); 15514 } 15515 15516 /* 15517 * Used by LOG SENSE page 0x10 15518 * Reads (in synchronous mode) the self test log data using Read Log Ext cmd. 15519 * Note: cannot be called in the interrupt context. 15520 * 15521 * return 0 for success, -1 otherwise 15522 * 15523 */ 15524 static int 15525 sata_ext_smart_selftest_read_log( 15526 sata_hba_inst_t *sata_hba_inst, 15527 sata_drive_info_t *sdinfo, 15528 struct smart_ext_selftest_log *ext_selftest_log, 15529 uint16_t block_num) 15530 { 15531 sata_pkt_txlate_t *spx; 15532 sata_pkt_t *spkt; 15533 sata_cmd_t *scmd; 15534 int rval; 15535 15536 #if ! defined(lint) 15537 ASSERT(sizeof (struct smart_ext_selftest_log) == 512); 15538 #endif 15539 15540 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 15541 spx->txlt_sata_hba_inst = sata_hba_inst; 15542 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 15543 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 15544 if (spkt == NULL) { 15545 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15546 return (-1); 15547 } 15548 /* address is needed now */ 15549 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15550 15551 15552 /* Fill sata_pkt */ 15553 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15554 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 15555 /* Synchronous mode, no callback */ 15556 spkt->satapkt_comp = NULL; 15557 /* Timeout 30s */ 15558 spkt->satapkt_time = sata_default_pkt_time; 15559 15560 scmd = &spkt->satapkt_cmd; 15561 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 15562 15563 /* 15564 * Allocate buffer for SMART extended self-test log 15565 */ 15566 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 15567 sizeof (struct smart_ext_selftest_log)); 15568 if (scmd->satacmd_bp == NULL) { 15569 sata_pkt_free(spx); 15570 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15571 SATA_LOG_D((sata_hba_inst, CE_WARN, 15572 "sata_ext_smart_selftest_log: " 15573 "cannot allocate buffer")); 15574 return (-1); 15575 } 15576 15577 /* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */ 15578 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 15579 scmd->satacmd_sec_count_lsb = 1; /* One sector of selftest log */ 15580 scmd->satacmd_sec_count_msb = 0; /* One sector of selftest log */ 15581 scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE; 15582 scmd->satacmd_lba_low_msb = 0; 15583 scmd->satacmd_lba_mid_lsb = block_num & 0xff; 15584 scmd->satacmd_lba_mid_msb = block_num >> 8; 15585 scmd->satacmd_device_reg = 0; /* Always device 0 */ 15586 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 15587 15588 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 15589 sdinfo->satadrv_addr.cport))); 15590 15591 /* Send pkt to SATA HBA driver */ 15592 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 15593 SATA_TRAN_ACCEPTED || 15594 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 15595 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15596 sdinfo->satadrv_addr.cport))); 15597 15598 /* 15599 * Whoops, no SMART selftest log info available 15600 */ 15601 rval = -1; 15602 goto fail; 15603 } else { 15604 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15605 sdinfo->satadrv_addr.cport))); 15606 15607 if (spx->txlt_buf_dma_handle != NULL) { 15608 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 15609 DDI_DMA_SYNC_FORKERNEL); 15610 ASSERT(rval == DDI_SUCCESS); 15611 } 15612 bcopy(scmd->satacmd_bp->b_un.b_addr, 15613 (uint8_t *)ext_selftest_log, 15614 sizeof (struct smart_ext_selftest_log)); 15615 rval = 0; 15616 } 15617 15618 fail: 15619 /* Free allocated resources */ 15620 sata_free_local_buffer(spx); 15621 sata_pkt_free(spx); 15622 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15623 15624 return (rval); 15625 } 15626 15627 /* 15628 * Returns 0 for success, -1 otherwise 15629 * 15630 * SMART self-test log data is returned in buffer pointed to by selftest_log 15631 */ 15632 static int 15633 sata_smart_selftest_log( 15634 sata_hba_inst_t *sata_hba_inst, 15635 sata_drive_info_t *sdinfo, 15636 struct smart_selftest_log *selftest_log) 15637 { 15638 sata_pkt_t *spkt; 15639 sata_cmd_t *scmd; 15640 sata_pkt_txlate_t *spx; 15641 int rval; 15642 15643 #if ! defined(lint) 15644 ASSERT(sizeof (struct smart_selftest_log) == 512); 15645 #endif 15646 15647 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 15648 spx->txlt_sata_hba_inst = sata_hba_inst; 15649 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 15650 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 15651 if (spkt == NULL) { 15652 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15653 return (-1); 15654 } 15655 /* address is needed now */ 15656 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15657 15658 15659 /* Fill sata_pkt */ 15660 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15661 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 15662 /* Synchronous mode, no callback */ 15663 spkt->satapkt_comp = NULL; 15664 /* Timeout 30s */ 15665 spkt->satapkt_time = sata_default_pkt_time; 15666 15667 scmd = &spkt->satapkt_cmd; 15668 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 15669 15670 /* 15671 * Allocate buffer for SMART SELFTEST LOG 15672 */ 15673 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 15674 sizeof (struct smart_selftest_log)); 15675 if (scmd->satacmd_bp == NULL) { 15676 sata_pkt_free(spx); 15677 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15678 SATA_LOG_D((sata_hba_inst, CE_WARN, 15679 "sata_smart_selftest_log: " 15680 "cannot allocate buffer")); 15681 return (-1); 15682 } 15683 15684 /* Build SMART_READ_LOG cmd in the sata_pkt */ 15685 scmd->satacmd_addr_type = 0; /* N/A */ 15686 scmd->satacmd_sec_count_lsb = 1; /* One sector of SMART log */ 15687 scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE; 15688 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 15689 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 15690 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 15691 scmd->satacmd_device_reg = 0; /* Always device 0 */ 15692 scmd->satacmd_cmd_reg = SATAC_SMART; 15693 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 15694 sdinfo->satadrv_addr.cport))); 15695 15696 /* Send pkt to SATA HBA driver */ 15697 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 15698 SATA_TRAN_ACCEPTED || 15699 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 15700 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15701 sdinfo->satadrv_addr.cport))); 15702 /* 15703 * Whoops, no SMART DATA available 15704 */ 15705 rval = -1; 15706 goto fail; 15707 } else { 15708 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15709 sdinfo->satadrv_addr.cport))); 15710 if (spx->txlt_buf_dma_handle != NULL) { 15711 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 15712 DDI_DMA_SYNC_FORKERNEL); 15713 ASSERT(rval == DDI_SUCCESS); 15714 } 15715 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log, 15716 sizeof (struct smart_selftest_log)); 15717 rval = 0; 15718 } 15719 15720 fail: 15721 /* Free allocated resources */ 15722 sata_free_local_buffer(spx); 15723 sata_pkt_free(spx); 15724 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15725 15726 return (rval); 15727 } 15728 15729 15730 /* 15731 * Returns 0 for success, -1 otherwise 15732 * 15733 * SMART READ LOG data is returned in buffer pointed to by smart_log 15734 */ 15735 static int 15736 sata_smart_read_log( 15737 sata_hba_inst_t *sata_hba_inst, 15738 sata_drive_info_t *sdinfo, 15739 uint8_t *smart_log, /* where the data should be returned */ 15740 uint8_t which_log, /* which log should be returned */ 15741 uint8_t log_size) /* # of 512 bytes in log */ 15742 { 15743 sata_pkt_t *spkt; 15744 sata_cmd_t *scmd; 15745 sata_pkt_txlate_t *spx; 15746 int rval; 15747 15748 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 15749 spx->txlt_sata_hba_inst = sata_hba_inst; 15750 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 15751 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 15752 if (spkt == NULL) { 15753 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15754 return (-1); 15755 } 15756 /* address is needed now */ 15757 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15758 15759 15760 /* Fill sata_pkt */ 15761 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15762 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 15763 /* Synchronous mode, no callback */ 15764 spkt->satapkt_comp = NULL; 15765 /* Timeout 30s */ 15766 spkt->satapkt_time = sata_default_pkt_time; 15767 15768 scmd = &spkt->satapkt_cmd; 15769 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 15770 15771 /* 15772 * Allocate buffer for SMART READ LOG 15773 */ 15774 scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512); 15775 if (scmd->satacmd_bp == NULL) { 15776 sata_pkt_free(spx); 15777 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15778 SATA_LOG_D((sata_hba_inst, CE_WARN, 15779 "sata_smart_read_log: " "cannot allocate buffer")); 15780 return (-1); 15781 } 15782 15783 /* Build SMART_READ_LOG cmd in the sata_pkt */ 15784 scmd->satacmd_addr_type = 0; /* N/A */ 15785 scmd->satacmd_sec_count_lsb = log_size; /* what the caller asked for */ 15786 scmd->satacmd_lba_low_lsb = which_log; /* which log page */ 15787 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 15788 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 15789 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 15790 scmd->satacmd_device_reg = 0; /* Always device 0 */ 15791 scmd->satacmd_cmd_reg = SATAC_SMART; 15792 15793 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 15794 sdinfo->satadrv_addr.cport))); 15795 15796 /* Send pkt to SATA HBA driver */ 15797 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 15798 SATA_TRAN_ACCEPTED || 15799 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 15800 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15801 sdinfo->satadrv_addr.cport))); 15802 15803 /* 15804 * Whoops, no SMART DATA available 15805 */ 15806 rval = -1; 15807 goto fail; 15808 } else { 15809 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15810 sdinfo->satadrv_addr.cport))); 15811 15812 if (spx->txlt_buf_dma_handle != NULL) { 15813 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 15814 DDI_DMA_SYNC_FORKERNEL); 15815 ASSERT(rval == DDI_SUCCESS); 15816 } 15817 bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512); 15818 rval = 0; 15819 } 15820 15821 fail: 15822 /* Free allocated resources */ 15823 sata_free_local_buffer(spx); 15824 sata_pkt_free(spx); 15825 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15826 15827 return (rval); 15828 } 15829 15830 /* 15831 * Used by LOG SENSE page 0x10 15832 * 15833 * return 0 for success, -1 otherwise 15834 * 15835 */ 15836 static int 15837 sata_read_log_ext_directory( 15838 sata_hba_inst_t *sata_hba_inst, 15839 sata_drive_info_t *sdinfo, 15840 struct read_log_ext_directory *logdir) 15841 { 15842 sata_pkt_txlate_t *spx; 15843 sata_pkt_t *spkt; 15844 sata_cmd_t *scmd; 15845 int rval; 15846 15847 #if ! defined(lint) 15848 ASSERT(sizeof (struct read_log_ext_directory) == 512); 15849 #endif 15850 15851 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 15852 spx->txlt_sata_hba_inst = sata_hba_inst; 15853 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 15854 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 15855 if (spkt == NULL) { 15856 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15857 return (-1); 15858 } 15859 15860 /* Fill sata_pkt */ 15861 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15862 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 15863 /* Synchronous mode, no callback */ 15864 spkt->satapkt_comp = NULL; 15865 /* Timeout 30s */ 15866 spkt->satapkt_time = sata_default_pkt_time; 15867 15868 scmd = &spkt->satapkt_cmd; 15869 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 15870 15871 /* 15872 * Allocate buffer for SMART READ LOG EXTENDED command 15873 */ 15874 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 15875 sizeof (struct read_log_ext_directory)); 15876 if (scmd->satacmd_bp == NULL) { 15877 sata_pkt_free(spx); 15878 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15879 SATA_LOG_D((sata_hba_inst, CE_WARN, 15880 "sata_read_log_ext_directory: " 15881 "cannot allocate buffer")); 15882 return (-1); 15883 } 15884 15885 /* Build READ LOG EXT w/ log directory cmd in the sata_pkt */ 15886 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 15887 scmd->satacmd_sec_count_lsb = 1; /* One sector of directory */ 15888 scmd->satacmd_sec_count_msb = 0; /* One sector of directory */ 15889 scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY; 15890 scmd->satacmd_lba_low_msb = 0; 15891 scmd->satacmd_lba_mid_lsb = 0; 15892 scmd->satacmd_lba_mid_msb = 0; 15893 scmd->satacmd_device_reg = 0; /* Always device 0 */ 15894 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 15895 15896 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 15897 sdinfo->satadrv_addr.cport))); 15898 15899 /* Send pkt to SATA HBA driver */ 15900 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 15901 SATA_TRAN_ACCEPTED || 15902 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 15903 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15904 sdinfo->satadrv_addr.cport))); 15905 /* 15906 * Whoops, no SMART selftest log info available 15907 */ 15908 rval = -1; 15909 goto fail; 15910 } else { 15911 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15912 sdinfo->satadrv_addr.cport))); 15913 if (spx->txlt_buf_dma_handle != NULL) { 15914 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 15915 DDI_DMA_SYNC_FORKERNEL); 15916 ASSERT(rval == DDI_SUCCESS); 15917 } 15918 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir, 15919 sizeof (struct read_log_ext_directory)); 15920 rval = 0; 15921 } 15922 15923 fail: 15924 /* Free allocated resources */ 15925 sata_free_local_buffer(spx); 15926 sata_pkt_free(spx); 15927 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15928 15929 return (rval); 15930 } 15931 15932 /* 15933 * Set up error retrieval sata command for NCQ command error data 15934 * recovery. 15935 * 15936 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up, 15937 * returns SATA_FAILURE otherwise. 15938 */ 15939 static int 15940 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo) 15941 { 15942 #ifndef __lock_lint 15943 _NOTE(ARGUNUSED(sdinfo)) 15944 #endif 15945 15946 sata_pkt_t *spkt = spx->txlt_sata_pkt; 15947 sata_cmd_t *scmd; 15948 struct buf *bp; 15949 15950 /* Operation modes are up to the caller */ 15951 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 15952 15953 /* Synchronous mode, no callback - may be changed by the caller */ 15954 spkt->satapkt_comp = NULL; 15955 spkt->satapkt_time = sata_default_pkt_time; 15956 15957 scmd = &spkt->satapkt_cmd; 15958 bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t)); 15959 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 15960 15961 /* 15962 * Allocate dma_able buffer error data. 15963 * Buffer allocation will take care of buffer alignment and other DMA 15964 * attributes. 15965 */ 15966 bp = sata_alloc_local_buffer(spx, 15967 sizeof (struct sata_ncq_error_recovery_page)); 15968 if (bp == NULL) 15969 return (SATA_FAILURE); 15970 15971 bp_mapin(bp); /* make data buffer accessible */ 15972 scmd->satacmd_bp = bp; 15973 15974 /* 15975 * Set-up pointer to the buffer handle, so HBA can sync buffer 15976 * before accessing it. Handle is in usual place in translate struct. 15977 */ 15978 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle; 15979 15980 ASSERT(scmd->satacmd_num_dma_cookies != 0); 15981 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 15982 15983 return (SATA_SUCCESS); 15984 } 15985 15986 /* 15987 * sata_xlate_errors() is used to translate (S)ATA error 15988 * information to SCSI information returned in the SCSI 15989 * packet. 15990 */ 15991 static void 15992 sata_xlate_errors(sata_pkt_txlate_t *spx) 15993 { 15994 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 15995 struct scsi_extended_sense *sense; 15996 15997 scsipkt->pkt_reason = CMD_INCOMPLETE; 15998 *scsipkt->pkt_scbp = STATUS_CHECK; 15999 sense = sata_arq_sense(spx); 16000 16001 switch (spx->txlt_sata_pkt->satapkt_reason) { 16002 case SATA_PKT_PORT_ERROR: 16003 /* 16004 * We have no device data. Assume no data transfered. 16005 */ 16006 sense->es_key = KEY_HARDWARE_ERROR; 16007 break; 16008 16009 case SATA_PKT_DEV_ERROR: 16010 if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 16011 SATA_STATUS_ERR) { 16012 /* 16013 * determine dev error reason from error 16014 * reg content 16015 */ 16016 sata_decode_device_error(spx, sense); 16017 break; 16018 } 16019 /* No extended sense key - no info available */ 16020 break; 16021 16022 case SATA_PKT_TIMEOUT: 16023 scsipkt->pkt_reason = CMD_TIMEOUT; 16024 scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET; 16025 /* No extended sense key */ 16026 break; 16027 16028 case SATA_PKT_ABORTED: 16029 scsipkt->pkt_reason = CMD_ABORTED; 16030 scsipkt->pkt_statistics |= STAT_ABORTED; 16031 /* No extended sense key */ 16032 break; 16033 16034 case SATA_PKT_RESET: 16035 /* 16036 * pkt aborted either by an explicit reset request from 16037 * a host, or due to error recovery 16038 */ 16039 scsipkt->pkt_reason = CMD_RESET; 16040 scsipkt->pkt_statistics |= STAT_DEV_RESET; 16041 break; 16042 16043 default: 16044 scsipkt->pkt_reason = CMD_TRAN_ERR; 16045 break; 16046 } 16047 } 16048 16049 16050 16051 16052 /* 16053 * Log sata message 16054 * dev pathname msg line preceeds the logged message. 16055 */ 16056 16057 static void 16058 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...) 16059 { 16060 char pathname[128]; 16061 dev_info_t *dip = NULL; 16062 va_list ap; 16063 16064 mutex_enter(&sata_log_mutex); 16065 16066 va_start(ap, fmt); 16067 (void) vsprintf(sata_log_buf, fmt, ap); 16068 va_end(ap); 16069 16070 if (sata_hba_inst != NULL) { 16071 dip = SATA_DIP(sata_hba_inst); 16072 (void) ddi_pathname(dip, pathname); 16073 } else { 16074 pathname[0] = 0; 16075 } 16076 if (level == CE_CONT) { 16077 if (sata_debug_flags == 0) 16078 cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf); 16079 else 16080 cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf); 16081 } else { 16082 if (level != CE_NOTE) { 16083 cmn_err(level, "%s:\n %s", pathname, sata_log_buf); 16084 } else if (sata_msg) { 16085 cmn_err(level, "%s:\n %s", pathname, 16086 sata_log_buf); 16087 } 16088 } 16089 16090 /* sata trace debug */ 16091 sata_trace_debug(dip, sata_log_buf); 16092 16093 mutex_exit(&sata_log_mutex); 16094 } 16095 16096 16097 /* ******** Asynchronous HBA events handling & hotplugging support ******** */ 16098 16099 /* 16100 * Start or terminate the thread, depending on flag arg and current state 16101 */ 16102 static void 16103 sata_event_thread_control(int startstop) 16104 { 16105 static int sata_event_thread_terminating = 0; 16106 static int sata_event_thread_starting = 0; 16107 int i; 16108 16109 mutex_enter(&sata_event_mutex); 16110 16111 if (startstop == 0 && (sata_event_thread_starting == 1 || 16112 sata_event_thread_terminating == 1)) { 16113 mutex_exit(&sata_event_mutex); 16114 return; 16115 } 16116 if (startstop == 1 && sata_event_thread_starting == 1) { 16117 mutex_exit(&sata_event_mutex); 16118 return; 16119 } 16120 if (startstop == 1 && sata_event_thread_terminating == 1) { 16121 sata_event_thread_starting = 1; 16122 /* wait til terminate operation completes */ 16123 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 16124 while (sata_event_thread_terminating == 1) { 16125 if (i-- <= 0) { 16126 sata_event_thread_starting = 0; 16127 mutex_exit(&sata_event_mutex); 16128 #ifdef SATA_DEBUG 16129 cmn_err(CE_WARN, "sata_event_thread_control: " 16130 "timeout waiting for thread to terminate"); 16131 #endif 16132 return; 16133 } 16134 mutex_exit(&sata_event_mutex); 16135 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 16136 mutex_enter(&sata_event_mutex); 16137 } 16138 } 16139 if (startstop == 1) { 16140 if (sata_event_thread == NULL) { 16141 sata_event_thread = thread_create(NULL, 0, 16142 (void (*)())sata_event_daemon, 16143 &sata_hba_list, 0, &p0, TS_RUN, minclsyspri); 16144 } 16145 sata_event_thread_starting = 0; 16146 mutex_exit(&sata_event_mutex); 16147 return; 16148 } 16149 16150 /* 16151 * If we got here, thread may need to be terminated 16152 */ 16153 if (sata_event_thread != NULL) { 16154 int i; 16155 /* Signal event thread to go away */ 16156 sata_event_thread_terminating = 1; 16157 sata_event_thread_terminate = 1; 16158 cv_signal(&sata_event_cv); 16159 /* 16160 * Wait til daemon terminates. 16161 */ 16162 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 16163 while (sata_event_thread_terminate == 1) { 16164 mutex_exit(&sata_event_mutex); 16165 if (i-- <= 0) { 16166 /* Daemon did not go away !!! */ 16167 #ifdef SATA_DEBUG 16168 cmn_err(CE_WARN, "sata_event_thread_control: " 16169 "cannot terminate event daemon thread"); 16170 #endif 16171 mutex_enter(&sata_event_mutex); 16172 break; 16173 } 16174 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 16175 mutex_enter(&sata_event_mutex); 16176 } 16177 sata_event_thread_terminating = 0; 16178 } 16179 ASSERT(sata_event_thread_terminating == 0); 16180 ASSERT(sata_event_thread_starting == 0); 16181 mutex_exit(&sata_event_mutex); 16182 } 16183 16184 16185 /* 16186 * SATA HBA event notification function. 16187 * Events reported by SATA HBA drivers per HBA instance relate to a change in 16188 * a port and/or device state or a controller itself. 16189 * Events for different addresses/addr types cannot be combined. 16190 * A warning message is generated for each event type. 16191 * Events are not processed by this function, so only the 16192 * event flag(s)is set for an affected entity and the event thread is 16193 * waken up. Event daemon thread processes all events. 16194 * 16195 * NOTE: Since more than one event may be reported at the same time, one 16196 * cannot determine a sequence of events when opposite event are reported, eg. 16197 * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing 16198 * is taking precedence over reported events, i.e. may cause ignoring some 16199 * events. 16200 */ 16201 #define SATA_EVENT_MAX_MSG_LENGTH 79 16202 16203 void 16204 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event) 16205 { 16206 sata_hba_inst_t *sata_hba_inst = NULL; 16207 sata_address_t *saddr; 16208 sata_pmult_info_t *pmultinfo; 16209 sata_drive_info_t *sdinfo; 16210 sata_port_stats_t *pstats; 16211 sata_cport_info_t *cportinfo; 16212 sata_pmport_info_t *pmportinfo; 16213 int cport, pmport; 16214 char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1]; 16215 char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1]; 16216 char *lcp; 16217 static char *err_msg_evnt_1 = 16218 "sata_hba_event_notify: invalid port event 0x%x "; 16219 static char *err_msg_evnt_2 = 16220 "sata_hba_event_notify: invalid device event 0x%x "; 16221 int linkevent; 16222 16223 /* 16224 * There is a possibility that an event will be generated on HBA 16225 * that has not completed attachment or is detaching. We still want 16226 * to process events until HBA is detached. 16227 */ 16228 mutex_enter(&sata_mutex); 16229 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 16230 sata_hba_inst = sata_hba_inst->satahba_next) { 16231 if (SATA_DIP(sata_hba_inst) == dip) 16232 if (sata_hba_inst->satahba_attached == 1) 16233 break; 16234 } 16235 mutex_exit(&sata_mutex); 16236 if (sata_hba_inst == NULL) 16237 /* HBA not attached */ 16238 return; 16239 16240 ASSERT(sata_device != NULL); 16241 16242 /* 16243 * Validate address before - do not proceed with invalid address. 16244 */ 16245 saddr = &sata_device->satadev_addr; 16246 if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst)) 16247 return; 16248 16249 cport = saddr->cport; 16250 pmport = saddr->pmport; 16251 16252 buf1[0] = buf2[0] = '\0'; 16253 16254 /* 16255 * If event relates to port or device, check port state. 16256 * Port has to be initialized, or we cannot accept an event. 16257 */ 16258 if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT | 16259 SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT | SATA_ADDR_PMULT)) != 0) { 16260 mutex_enter(&sata_hba_inst->satahba_mutex); 16261 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 16262 mutex_exit(&sata_hba_inst->satahba_mutex); 16263 if (cportinfo == NULL || cportinfo->cport_state == 0) 16264 return; 16265 } 16266 16267 if ((saddr->qual & (SATA_ADDR_PMULT | SATA_ADDR_PMPORT | 16268 SATA_ADDR_DPMPORT)) != 0) { 16269 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 16270 SATA_LOG_D((sata_hba_inst, CE_WARN, 16271 "sata_hba_event_notify: Non-pmult device (0x%x)" 16272 "is attached to port %d, ignore pmult/pmport " 16273 "event 0x%x", cportinfo->cport_dev_type, 16274 cport, event)); 16275 return; 16276 } 16277 16278 mutex_enter(&cportinfo->cport_mutex); 16279 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 16280 mutex_exit(&cportinfo->cport_mutex); 16281 16282 /* 16283 * The daemon might be processing attachment of port 16284 * multiplier, in that case we should ignore events on its 16285 * sub-devices. 16286 * 16287 * NOTE: Only pmult_state is checked in sata_hba_event_notify. 16288 * The pmport_state is checked by sata daemon. 16289 */ 16290 if (pmultinfo == NULL || 16291 pmultinfo->pmult_state == SATA_STATE_UNKNOWN) { 16292 SATA_LOG_D((sata_hba_inst, CE_WARN, 16293 "sata_hba_event_notify: pmult is not" 16294 "available at port %d:%d, ignore event 0x%x", 16295 cport, pmport, event)); 16296 return; 16297 } 16298 } 16299 16300 if ((saddr->qual & 16301 (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) != 0) { 16302 16303 mutex_enter(&cportinfo->cport_mutex); 16304 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) { 16305 SATA_LOG_D((sata_hba_inst, CE_WARN, 16306 "sata_hba_event_notify: invalid/" 16307 "un-implemented port %d:%d (%d ports), " 16308 "ignore event 0x%x", cport, pmport, 16309 SATA_NUM_PMPORTS(sata_hba_inst, cport), event)); 16310 mutex_exit(&cportinfo->cport_mutex); 16311 return; 16312 } 16313 mutex_exit(&cportinfo->cport_mutex); 16314 16315 mutex_enter(&sata_hba_inst->satahba_mutex); 16316 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 16317 cport, pmport); 16318 mutex_exit(&sata_hba_inst->satahba_mutex); 16319 16320 /* pmport is implemented/valid? */ 16321 if (pmportinfo == NULL) { 16322 SATA_LOG_D((sata_hba_inst, CE_WARN, 16323 "sata_hba_event_notify: invalid/" 16324 "un-implemented port %d:%d, ignore " 16325 "event 0x%x", cport, pmport, event)); 16326 return; 16327 } 16328 } 16329 16330 /* 16331 * Events refer to devices, ports and controllers - each has 16332 * unique address. Events for different addresses cannot be combined. 16333 */ 16334 if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) { 16335 16336 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16337 16338 /* qualify this event(s) */ 16339 if ((event & SATA_EVNT_PORT_EVENTS) == 0) { 16340 /* Invalid event for the device port */ 16341 (void) sprintf(buf2, err_msg_evnt_1, 16342 event & SATA_EVNT_PORT_EVENTS); 16343 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16344 goto event_info; 16345 } 16346 if (saddr->qual == SATA_ADDR_CPORT) { 16347 /* Controller's device port event */ 16348 16349 (SATA_CPORT_INFO(sata_hba_inst, cport))-> 16350 cport_event_flags |= 16351 event & SATA_EVNT_PORT_EVENTS; 16352 pstats = 16353 &(SATA_CPORT_INFO(sata_hba_inst, cport))-> 16354 cport_stats; 16355 } else { 16356 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16357 mutex_enter(&pmportinfo->pmport_mutex); 16358 /* Port multiplier's device port event */ 16359 (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 16360 pmport_event_flags |= 16361 event & SATA_EVNT_PORT_EVENTS; 16362 pstats = 16363 &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 16364 pmport_stats; 16365 mutex_exit(&pmportinfo->pmport_mutex); 16366 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16367 } 16368 16369 /* 16370 * Add to statistics and log the message. We have to do it 16371 * here rather than in the event daemon, because there may be 16372 * multiple events occuring before they are processed. 16373 */ 16374 linkevent = event & 16375 (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED); 16376 if (linkevent) { 16377 if (linkevent == (SATA_EVNT_LINK_LOST | 16378 SATA_EVNT_LINK_ESTABLISHED)) { 16379 /* This is likely event combination */ 16380 (void) strlcat(buf1, "link lost/established, ", 16381 SATA_EVENT_MAX_MSG_LENGTH); 16382 16383 if (pstats->link_lost < 0xffffffffffffffffULL) 16384 pstats->link_lost++; 16385 if (pstats->link_established < 16386 0xffffffffffffffffULL) 16387 pstats->link_established++; 16388 linkevent = 0; 16389 } else if (linkevent & SATA_EVNT_LINK_LOST) { 16390 (void) strlcat(buf1, "link lost, ", 16391 SATA_EVENT_MAX_MSG_LENGTH); 16392 16393 if (pstats->link_lost < 0xffffffffffffffffULL) 16394 pstats->link_lost++; 16395 } else { 16396 (void) strlcat(buf1, "link established, ", 16397 SATA_EVENT_MAX_MSG_LENGTH); 16398 if (pstats->link_established < 16399 0xffffffffffffffffULL) 16400 pstats->link_established++; 16401 } 16402 } 16403 if (event & SATA_EVNT_DEVICE_ATTACHED) { 16404 (void) strlcat(buf1, "device attached, ", 16405 SATA_EVENT_MAX_MSG_LENGTH); 16406 if (pstats->device_attached < 0xffffffffffffffffULL) 16407 pstats->device_attached++; 16408 } 16409 if (event & SATA_EVNT_DEVICE_DETACHED) { 16410 (void) strlcat(buf1, "device detached, ", 16411 SATA_EVENT_MAX_MSG_LENGTH); 16412 if (pstats->device_detached < 0xffffffffffffffffULL) 16413 pstats->device_detached++; 16414 } 16415 if (event & SATA_EVNT_PWR_LEVEL_CHANGED) { 16416 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 16417 "port %d power level changed", cport); 16418 if (pstats->port_pwr_changed < 0xffffffffffffffffULL) 16419 pstats->port_pwr_changed++; 16420 } 16421 16422 if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) { 16423 /* There should be no other events for this address */ 16424 (void) sprintf(buf2, err_msg_evnt_1, 16425 event & ~SATA_EVNT_PORT_EVENTS); 16426 } 16427 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16428 16429 } else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) { 16430 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16431 16432 /* qualify this event */ 16433 if ((event & SATA_EVNT_DEVICE_RESET) == 0) { 16434 /* Invalid event for a device */ 16435 (void) sprintf(buf2, err_msg_evnt_2, 16436 event & SATA_EVNT_DEVICE_RESET); 16437 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16438 goto event_info; 16439 } 16440 /* drive event */ 16441 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 16442 if (sdinfo != NULL) { 16443 if (event & SATA_EVNT_DEVICE_RESET) { 16444 (void) strlcat(buf1, "device reset, ", 16445 SATA_EVENT_MAX_MSG_LENGTH); 16446 if (sdinfo->satadrv_stats.drive_reset < 16447 0xffffffffffffffffULL) 16448 sdinfo->satadrv_stats.drive_reset++; 16449 sdinfo->satadrv_event_flags |= 16450 SATA_EVNT_DEVICE_RESET; 16451 } 16452 } 16453 if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) { 16454 /* Invalid event for a device */ 16455 (void) sprintf(buf2, err_msg_evnt_2, 16456 event & ~SATA_EVNT_DRIVE_EVENTS); 16457 } 16458 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16459 } else if (saddr->qual == SATA_ADDR_PMULT) { 16460 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16461 16462 /* qualify this event */ 16463 if ((event & (SATA_EVNT_DEVICE_RESET | 16464 SATA_EVNT_PMULT_LINK_CHANGED)) == 0) { 16465 /* Invalid event for a port multiplier */ 16466 (void) sprintf(buf2, err_msg_evnt_2, 16467 event & SATA_EVNT_DEVICE_RESET); 16468 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16469 goto event_info; 16470 } 16471 16472 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 16473 16474 if (event & SATA_EVNT_DEVICE_RESET) { 16475 16476 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 16477 "[Reset] port-mult on cport %d", cport); 16478 pmultinfo->pmult_event_flags |= 16479 SATA_EVNT_DEVICE_RESET; 16480 (void) strlcat(buf1, "pmult reset, ", 16481 SATA_EVENT_MAX_MSG_LENGTH); 16482 } 16483 16484 if (event & SATA_EVNT_PMULT_LINK_CHANGED) { 16485 16486 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 16487 "pmult link changed on cport %d", cport); 16488 pmultinfo->pmult_event_flags |= 16489 SATA_EVNT_PMULT_LINK_CHANGED; 16490 (void) strlcat(buf1, "pmult link changed, ", 16491 SATA_EVENT_MAX_MSG_LENGTH); 16492 } 16493 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16494 16495 } else { 16496 if (saddr->qual != SATA_ADDR_NULL) { 16497 /* Wrong address qualifier */ 16498 SATA_LOG_D((sata_hba_inst, CE_WARN, 16499 "sata_hba_event_notify: invalid address 0x%x", 16500 *(uint32_t *)saddr)); 16501 return; 16502 } 16503 if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 || 16504 (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) { 16505 /* Invalid event for the controller */ 16506 SATA_LOG_D((sata_hba_inst, CE_WARN, 16507 "sata_hba_event_notify: invalid event 0x%x for " 16508 "controller", 16509 event & SATA_EVNT_CONTROLLER_EVENTS)); 16510 return; 16511 } 16512 buf1[0] = '\0'; 16513 /* This may be a frequent and not interesting event */ 16514 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 16515 "controller power level changed\n", NULL); 16516 16517 mutex_enter(&sata_hba_inst->satahba_mutex); 16518 if (sata_hba_inst->satahba_stats.ctrl_pwr_change < 16519 0xffffffffffffffffULL) 16520 sata_hba_inst->satahba_stats.ctrl_pwr_change++; 16521 16522 sata_hba_inst->satahba_event_flags |= 16523 SATA_EVNT_PWR_LEVEL_CHANGED; 16524 mutex_exit(&sata_hba_inst->satahba_mutex); 16525 } 16526 /* 16527 * If we got here, there is something to do with this HBA 16528 * instance. 16529 */ 16530 mutex_enter(&sata_hba_inst->satahba_mutex); 16531 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 16532 mutex_exit(&sata_hba_inst->satahba_mutex); 16533 mutex_enter(&sata_mutex); 16534 sata_event_pending |= SATA_EVNT_MAIN; /* global event indicator */ 16535 mutex_exit(&sata_mutex); 16536 16537 /* Tickle event thread */ 16538 mutex_enter(&sata_event_mutex); 16539 if (sata_event_thread_active == 0) 16540 cv_signal(&sata_event_cv); 16541 mutex_exit(&sata_event_mutex); 16542 16543 event_info: 16544 if (buf1[0] != '\0') { 16545 lcp = strrchr(buf1, ','); 16546 if (lcp != NULL) 16547 *lcp = '\0'; 16548 } 16549 if (saddr->qual == SATA_ADDR_CPORT || 16550 saddr->qual == SATA_ADDR_DCPORT) { 16551 if (buf1[0] != '\0') { 16552 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 16553 cport, buf1); 16554 } 16555 if (buf2[0] != '\0') { 16556 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 16557 cport, buf2); 16558 } 16559 } else if (saddr->qual == SATA_ADDR_PMPORT || 16560 saddr->qual == SATA_ADDR_DPMPORT) { 16561 if (buf1[0] != '\0') { 16562 sata_log(sata_hba_inst, CE_NOTE, 16563 "port %d pmport %d: %s\n", cport, pmport, buf1); 16564 } 16565 if (buf2[0] != '\0') { 16566 sata_log(sata_hba_inst, CE_NOTE, 16567 "port %d pmport %d: %s\n", cport, pmport, buf2); 16568 } 16569 } 16570 } 16571 16572 16573 /* 16574 * Event processing thread. 16575 * Arg is a pointer to the sata_hba_list pointer. 16576 * It is not really needed, because sata_hba_list is global and static 16577 */ 16578 static void 16579 sata_event_daemon(void *arg) 16580 { 16581 #ifndef __lock_lint 16582 _NOTE(ARGUNUSED(arg)) 16583 #endif 16584 sata_hba_inst_t *sata_hba_inst; 16585 clock_t lbolt; 16586 16587 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 16588 "SATA event daemon started\n", NULL); 16589 loop: 16590 /* 16591 * Process events here. Walk through all registered HBAs 16592 */ 16593 mutex_enter(&sata_mutex); 16594 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 16595 sata_hba_inst = sata_hba_inst->satahba_next) { 16596 ASSERT(sata_hba_inst != NULL); 16597 mutex_enter(&sata_hba_inst->satahba_mutex); 16598 if (sata_hba_inst->satahba_attached == 0 || 16599 (sata_hba_inst->satahba_event_flags & 16600 SATA_EVNT_SKIP) != 0) { 16601 mutex_exit(&sata_hba_inst->satahba_mutex); 16602 continue; 16603 } 16604 if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) { 16605 sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP; 16606 mutex_exit(&sata_hba_inst->satahba_mutex); 16607 mutex_exit(&sata_mutex); 16608 /* Got the controller with pending event */ 16609 sata_process_controller_events(sata_hba_inst); 16610 /* 16611 * Since global mutex was released, there is a 16612 * possibility that HBA list has changed, so start 16613 * over from the top. Just processed controller 16614 * will be passed-over because of the SKIP flag. 16615 */ 16616 goto loop; 16617 } 16618 mutex_exit(&sata_hba_inst->satahba_mutex); 16619 } 16620 /* Clear SKIP flag in all controllers */ 16621 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 16622 sata_hba_inst = sata_hba_inst->satahba_next) { 16623 mutex_enter(&sata_hba_inst->satahba_mutex); 16624 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP; 16625 mutex_exit(&sata_hba_inst->satahba_mutex); 16626 } 16627 mutex_exit(&sata_mutex); 16628 16629 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 16630 "SATA EVENT DAEMON suspending itself", NULL); 16631 16632 #ifdef SATA_DEBUG 16633 if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) { 16634 sata_log(sata_hba_inst, CE_WARN, 16635 "SATA EVENTS PROCESSING DISABLED\n"); 16636 thread_exit(); /* Daemon will not run again */ 16637 } 16638 #endif 16639 mutex_enter(&sata_event_mutex); 16640 sata_event_thread_active = 0; 16641 mutex_exit(&sata_event_mutex); 16642 /* 16643 * Go to sleep/suspend itself and wake up either because new event or 16644 * wait timeout. Exit if there is a termination request (driver 16645 * unload). 16646 */ 16647 do { 16648 lbolt = ddi_get_lbolt(); 16649 lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME); 16650 mutex_enter(&sata_event_mutex); 16651 (void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt); 16652 16653 if (sata_event_thread_active != 0) { 16654 mutex_exit(&sata_event_mutex); 16655 continue; 16656 } 16657 16658 /* Check if it is time to go away */ 16659 if (sata_event_thread_terminate == 1) { 16660 /* 16661 * It is up to the thread setting above flag to make 16662 * sure that this thread is not killed prematurely. 16663 */ 16664 sata_event_thread_terminate = 0; 16665 sata_event_thread = NULL; 16666 mutex_exit(&sata_event_mutex); 16667 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 16668 "SATA_EVENT_DAEMON_TERMINATING", NULL); 16669 thread_exit(); { _NOTE(NOT_REACHED) } 16670 } 16671 mutex_exit(&sata_event_mutex); 16672 } while (!(sata_event_pending & SATA_EVNT_MAIN)); 16673 16674 mutex_enter(&sata_event_mutex); 16675 sata_event_thread_active = 1; 16676 mutex_exit(&sata_event_mutex); 16677 16678 mutex_enter(&sata_mutex); 16679 sata_event_pending &= ~SATA_EVNT_MAIN; 16680 mutex_exit(&sata_mutex); 16681 16682 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 16683 "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL); 16684 16685 goto loop; 16686 } 16687 16688 /* 16689 * Specific HBA instance event processing. 16690 * 16691 * NOTE: At the moment, device event processing is limited to hard disks 16692 * only. 16693 * Port multiplier is supported now. 16694 */ 16695 static void 16696 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst) 16697 { 16698 int ncport; 16699 uint32_t event_flags; 16700 sata_address_t *saddr; 16701 sata_cport_info_t *cportinfo; 16702 sata_pmult_info_t *pmultinfo; 16703 16704 SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst, 16705 "Processing controller %d event(s)", 16706 ddi_get_instance(SATA_DIP(sata_hba_inst))); 16707 16708 mutex_enter(&sata_hba_inst->satahba_mutex); 16709 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN; 16710 event_flags = sata_hba_inst->satahba_event_flags; 16711 mutex_exit(&sata_hba_inst->satahba_mutex); 16712 /* 16713 * Process controller power change first 16714 * HERE 16715 */ 16716 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) 16717 sata_process_cntrl_pwr_level_change(sata_hba_inst); 16718 16719 /* 16720 * Search through ports/devices to identify affected port/device. 16721 * We may have to process events for more than one port/device. 16722 */ 16723 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 16724 /* 16725 * Not all ports may be processed in attach by the time we 16726 * get an event. Check if port info is initialized. 16727 */ 16728 mutex_enter(&sata_hba_inst->satahba_mutex); 16729 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 16730 mutex_exit(&sata_hba_inst->satahba_mutex); 16731 if (cportinfo == NULL || cportinfo->cport_state == NULL) 16732 continue; 16733 16734 /* We have initialized controller port info */ 16735 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 16736 event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 16737 cport_event_flags; 16738 /* Check if port was locked by IOCTL processing */ 16739 if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) { 16740 /* 16741 * We ignore port events because port is busy 16742 * with AP control processing. Set again 16743 * controller and main event flag, so that 16744 * events may be processed by the next daemon 16745 * run. 16746 */ 16747 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 16748 mutex_enter(&sata_hba_inst->satahba_mutex); 16749 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 16750 mutex_exit(&sata_hba_inst->satahba_mutex); 16751 mutex_enter(&sata_mutex); 16752 sata_event_pending |= SATA_EVNT_MAIN; 16753 mutex_exit(&sata_mutex); 16754 SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst, 16755 "Event processing postponed until " 16756 "AP control processing completes", 16757 NULL); 16758 /* Check other ports */ 16759 continue; 16760 } else { 16761 /* 16762 * Set BSY flag so that AP control would not 16763 * interfere with events processing for 16764 * this port. 16765 */ 16766 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 16767 cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY; 16768 } 16769 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 16770 16771 saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr; 16772 16773 if ((event_flags & 16774 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) { 16775 /* 16776 * Got port event. 16777 * We need some hierarchy of event processing as they 16778 * are affecting each other: 16779 * 1. port failed 16780 * 2. device detached/attached 16781 * 3. link events - link events may trigger device 16782 * detached or device attached events in some 16783 * circumstances. 16784 * 4. port power level changed 16785 */ 16786 if (event_flags & SATA_EVNT_PORT_FAILED) { 16787 sata_process_port_failed_event(sata_hba_inst, 16788 saddr); 16789 } 16790 if (event_flags & SATA_EVNT_DEVICE_DETACHED) { 16791 sata_process_device_detached(sata_hba_inst, 16792 saddr); 16793 } 16794 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) { 16795 sata_process_device_attached(sata_hba_inst, 16796 saddr); 16797 } 16798 if (event_flags & 16799 (SATA_EVNT_LINK_ESTABLISHED | 16800 SATA_EVNT_LINK_LOST)) { 16801 sata_process_port_link_events(sata_hba_inst, 16802 saddr); 16803 } 16804 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) { 16805 sata_process_port_pwr_change(sata_hba_inst, 16806 saddr); 16807 } 16808 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) { 16809 sata_process_target_node_cleanup( 16810 sata_hba_inst, saddr); 16811 } 16812 if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) { 16813 sata_process_device_autoonline( 16814 sata_hba_inst, saddr); 16815 } 16816 } 16817 16818 16819 /* 16820 * Scan port multiplier and all its sub-ports event flags. 16821 * The events are marked by 16822 * (1) sata_pmult_info.pmult_event_flags 16823 * (2) sata_pmport_info.pmport_event_flags 16824 */ 16825 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 16826 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 16827 /* 16828 * There should be another extra check: this 16829 * port multiplier still exists? 16830 */ 16831 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, 16832 ncport); 16833 16834 if (pmultinfo != NULL) { 16835 mutex_exit(&(SATA_CPORT_MUTEX( 16836 sata_hba_inst, ncport))); 16837 sata_process_pmult_events( 16838 sata_hba_inst, ncport); 16839 mutex_enter(&(SATA_CPORT_MUTEX( 16840 sata_hba_inst, ncport))); 16841 } else { 16842 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 16843 "Port-multiplier is gone. " 16844 "Ignore all sub-device events " 16845 "at port %d.", ncport); 16846 } 16847 } 16848 16849 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) != 16850 SATA_DTYPE_NONE) && 16851 (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) { 16852 if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)-> 16853 satadrv_event_flags & 16854 (SATA_EVNT_DEVICE_RESET | 16855 SATA_EVNT_INPROC_DEVICE_RESET)) { 16856 /* Have device event */ 16857 sata_process_device_reset(sata_hba_inst, 16858 saddr); 16859 } 16860 } 16861 /* Release PORT_BUSY flag */ 16862 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 16863 cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY; 16864 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 16865 16866 } /* End of loop through the controller SATA ports */ 16867 } 16868 16869 /* 16870 * Specific port multiplier instance event processing. At the moment, device 16871 * event processing is limited to link/attach event only. 16872 * 16873 * NOTE: power management event is not supported yet. 16874 */ 16875 static void 16876 sata_process_pmult_events(sata_hba_inst_t *sata_hba_inst, uint8_t cport) 16877 { 16878 sata_cport_info_t *cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 16879 sata_pmult_info_t *pmultinfo; 16880 sata_pmport_info_t *pmportinfo; 16881 sata_address_t *saddr; 16882 sata_device_t sata_device; 16883 uint32_t event_flags; 16884 int npmport; 16885 int rval; 16886 16887 SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst, 16888 "Processing pmult event(s) on cport %d of controller %d", 16889 cport, ddi_get_instance(SATA_DIP(sata_hba_inst))); 16890 16891 /* First process events on port multiplier */ 16892 mutex_enter(&cportinfo->cport_mutex); 16893 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 16894 event_flags = pmultinfo->pmult_event_flags; 16895 16896 /* 16897 * Reset event (of port multiplier) has higher priority because the 16898 * port multiplier itself might be failed or removed after reset. 16899 */ 16900 if (event_flags & SATA_EVNT_DEVICE_RESET) { 16901 /* 16902 * The status of the sub-links are uncertain, 16903 * so mark all sub-ports as RESET 16904 */ 16905 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 16906 sata_hba_inst, cport); npmport ++) { 16907 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 16908 cport, npmport); 16909 if (pmportinfo == NULL) { 16910 /* That's weird. */ 16911 SATA_LOG_D((sata_hba_inst, CE_WARN, 16912 "sata_hba_event_notify: " 16913 "invalid/un-implemented " 16914 "port %d:%d (%d ports), ", 16915 cport, npmport, SATA_NUM_PMPORTS( 16916 sata_hba_inst, cport))); 16917 continue; 16918 } 16919 16920 mutex_enter(&pmportinfo->pmport_mutex); 16921 16922 /* Mark all pmport to unknow state. */ 16923 pmportinfo->pmport_state = SATA_STATE_UNKNOWN; 16924 /* Mark all pmports with link events. */ 16925 pmportinfo->pmport_event_flags = 16926 (SATA_EVNT_LINK_ESTABLISHED|SATA_EVNT_LINK_LOST); 16927 mutex_exit(&pmportinfo->pmport_mutex); 16928 } 16929 16930 } else if (event_flags & SATA_EVNT_PMULT_LINK_CHANGED) { 16931 /* 16932 * We need probe the port multiplier to know what has 16933 * happened. 16934 */ 16935 bzero(&sata_device, sizeof (sata_device_t)); 16936 sata_device.satadev_rev = SATA_DEVICE_REV; 16937 sata_device.satadev_addr.cport = cport; 16938 sata_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT; 16939 sata_device.satadev_addr.qual = SATA_ADDR_PMULT; 16940 16941 mutex_exit(&cportinfo->cport_mutex); 16942 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 16943 (SATA_DIP(sata_hba_inst), &sata_device); 16944 mutex_enter(&cportinfo->cport_mutex); 16945 if (rval != SATA_SUCCESS) { 16946 /* Something went wrong? Fail the port */ 16947 cportinfo->cport_state = SATA_PSTATE_FAILED; 16948 mutex_exit(&cportinfo->cport_mutex); 16949 SATA_LOG_D((sata_hba_inst, CE_WARN, 16950 "SATA port %d probing failed", cport)); 16951 16952 /* PMult structure must be released. */ 16953 sata_free_pmult(sata_hba_inst, &sata_device); 16954 return; 16955 } 16956 16957 sata_update_port_info(sata_hba_inst, &sata_device); 16958 16959 /* 16960 * Sanity check - Port is active? Is the link active? 16961 * The device is still a port multiplier? 16962 */ 16963 if ((cportinfo->cport_state & 16964 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 16965 ((cportinfo->cport_scr.sstatus & 16966 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) || 16967 (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) { 16968 mutex_exit(&cportinfo->cport_mutex); 16969 16970 /* PMult structure must be released. */ 16971 sata_free_pmult(sata_hba_inst, &sata_device); 16972 return; 16973 } 16974 16975 /* Probed succeed, set port ready. */ 16976 cportinfo->cport_state |= 16977 SATA_STATE_PROBED | SATA_STATE_READY; 16978 } 16979 16980 /* Release port multiplier event flags. */ 16981 pmultinfo->pmult_event_flags &= 16982 ~(SATA_EVNT_DEVICE_RESET|SATA_EVNT_PMULT_LINK_CHANGED); 16983 mutex_exit(&cportinfo->cport_mutex); 16984 16985 /* 16986 * Check all sub-links. 16987 */ 16988 for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, cport); 16989 npmport ++) { 16990 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport); 16991 mutex_enter(&pmportinfo->pmport_mutex); 16992 event_flags = pmportinfo->pmport_event_flags; 16993 mutex_exit(&pmportinfo->pmport_mutex); 16994 saddr = &pmportinfo->pmport_addr; 16995 16996 if ((event_flags & 16997 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) { 16998 /* 16999 * Got port multiplier port event. 17000 * We need some hierarchy of event processing as they 17001 * are affecting each other: 17002 * 1. device detached/attached 17003 * 2. link events - link events may trigger device 17004 * detached or device attached events in some 17005 * circumstances. 17006 */ 17007 if (event_flags & SATA_EVNT_DEVICE_DETACHED) { 17008 sata_process_pmdevice_detached(sata_hba_inst, 17009 saddr); 17010 } 17011 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) { 17012 sata_process_pmdevice_attached(sata_hba_inst, 17013 saddr); 17014 } 17015 if (event_flags & SATA_EVNT_LINK_ESTABLISHED || 17016 event_flags & SATA_EVNT_LINK_LOST) { 17017 sata_process_pmport_link_events(sata_hba_inst, 17018 saddr); 17019 } 17020 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) { 17021 sata_process_target_node_cleanup( 17022 sata_hba_inst, saddr); 17023 } 17024 } 17025 17026 /* Checking drive event(s). */ 17027 mutex_enter(&pmportinfo->pmport_mutex); 17028 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE && 17029 pmportinfo->pmport_sata_drive != NULL) { 17030 event_flags = pmportinfo->pmport_sata_drive-> 17031 satadrv_event_flags; 17032 if (event_flags & (SATA_EVNT_DEVICE_RESET | 17033 SATA_EVNT_INPROC_DEVICE_RESET)) { 17034 17035 /* Have device event */ 17036 sata_process_pmdevice_reset(sata_hba_inst, 17037 saddr); 17038 } 17039 } 17040 mutex_exit(&pmportinfo->pmport_mutex); 17041 17042 /* Release PORT_BUSY flag */ 17043 mutex_enter(&cportinfo->cport_mutex); 17044 cportinfo->cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY; 17045 mutex_exit(&cportinfo->cport_mutex); 17046 } 17047 17048 SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst, 17049 "[DONE] pmult event(s) on cport %d of controller %d", 17050 cport, ddi_get_instance(SATA_DIP(sata_hba_inst))); 17051 } 17052 17053 /* 17054 * Process HBA power level change reported by HBA driver. 17055 * Not implemented at this time - event is ignored. 17056 */ 17057 static void 17058 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst) 17059 { 17060 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17061 "Processing controller power level change", NULL); 17062 17063 /* Ignoring it for now */ 17064 mutex_enter(&sata_hba_inst->satahba_mutex); 17065 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 17066 mutex_exit(&sata_hba_inst->satahba_mutex); 17067 } 17068 17069 /* 17070 * Process port power level change reported by HBA driver. 17071 * Not implemented at this time - event is ignored. 17072 */ 17073 static void 17074 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst, 17075 sata_address_t *saddr) 17076 { 17077 sata_cport_info_t *cportinfo; 17078 17079 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17080 "Processing port power level change", NULL); 17081 17082 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 17083 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17084 /* Reset event flag */ 17085 cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 17086 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17087 } 17088 17089 /* 17090 * Process port failure reported by HBA driver. 17091 * cports support only - no pmports. 17092 */ 17093 static void 17094 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst, 17095 sata_address_t *saddr) 17096 { 17097 sata_cport_info_t *cportinfo; 17098 17099 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 17100 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17101 /* Reset event flag first */ 17102 cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED; 17103 /* If the port is in SHUTDOWN or FAILED state, ignore this event. */ 17104 if ((cportinfo->cport_state & 17105 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) { 17106 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 17107 cport_mutex); 17108 return; 17109 } 17110 /* Fail the port */ 17111 cportinfo->cport_state = SATA_PSTATE_FAILED; 17112 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17113 sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport); 17114 } 17115 17116 /* 17117 * Device Reset Event processing. 17118 * The seqeunce is managed by 3 stage flags: 17119 * - reset event reported, 17120 * - reset event being processed, 17121 * - request to clear device reset state. 17122 * 17123 * NOTE: This function has to be entered with cport mutex held. It exits with 17124 * mutex held as well, but can release mutex during the processing. 17125 */ 17126 static void 17127 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst, 17128 sata_address_t *saddr) 17129 { 17130 sata_drive_info_t old_sdinfo; /* local copy of the drive info */ 17131 sata_drive_info_t *sdinfo; 17132 sata_cport_info_t *cportinfo; 17133 sata_device_t sata_device; 17134 int rval_probe, rval_set; 17135 17136 /* We only care about host sata cport for now */ 17137 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 17138 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 17139 /* 17140 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED 17141 * state, ignore reset event. 17142 */ 17143 if (((cportinfo->cport_state & 17144 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) || 17145 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 17146 sdinfo->satadrv_event_flags &= 17147 ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET); 17148 return; 17149 } 17150 17151 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) == 17152 SATA_DTYPE_PMULT)) { 17153 /* 17154 * Should not happened: this is already handled in 17155 * sata_hba_event_notify() 17156 */ 17157 mutex_exit(&cportinfo->cport_mutex); 17158 goto done; 17159 } 17160 17161 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) & 17162 SATA_VALID_DEV_TYPE) == 0) { 17163 /* 17164 * This should not happen - coding error. 17165 * But we can recover, so do not panic, just clean up 17166 * and if in debug mode, log the message. 17167 */ 17168 #ifdef SATA_DEBUG 17169 sata_log(sata_hba_inst, CE_WARN, 17170 "sata_process_device_reset: " 17171 "Invalid device type with sdinfo!", NULL); 17172 #endif 17173 sdinfo->satadrv_event_flags = 0; 17174 return; 17175 } 17176 17177 #ifdef SATA_DEBUG 17178 if ((sdinfo->satadrv_event_flags & 17179 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) { 17180 /* Nothing to do */ 17181 /* Something is weird - why we are processing dev reset? */ 17182 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17183 "No device reset event!!!!", NULL); 17184 17185 return; 17186 } 17187 if ((sdinfo->satadrv_event_flags & 17188 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 17189 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 17190 /* Something is weird - new device reset event */ 17191 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17192 "Overlapping device reset events!", NULL); 17193 } 17194 #endif 17195 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17196 "Processing port %d device reset", saddr->cport); 17197 17198 /* Clear event flag */ 17199 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET; 17200 17201 /* It seems that we always need to check the port state first */ 17202 sata_device.satadev_rev = SATA_DEVICE_REV; 17203 sata_device.satadev_addr = *saddr; 17204 /* 17205 * We have to exit mutex, because the HBA probe port function may 17206 * block on its own mutex. 17207 */ 17208 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17209 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 17210 (SATA_DIP(sata_hba_inst), &sata_device); 17211 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17212 sata_update_port_info(sata_hba_inst, &sata_device); 17213 if (rval_probe != SATA_SUCCESS) { 17214 /* Something went wrong? Fail the port */ 17215 cportinfo->cport_state = SATA_PSTATE_FAILED; 17216 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 17217 if (sdinfo != NULL) 17218 sdinfo->satadrv_event_flags = 0; 17219 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 17220 cport_mutex); 17221 SATA_LOG_D((sata_hba_inst, CE_WARN, 17222 "SATA port %d probing failed", 17223 saddr->cport)); 17224 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 17225 saddr->cport)->cport_mutex); 17226 return; 17227 } 17228 if ((sata_device.satadev_scr.sstatus & 17229 SATA_PORT_DEVLINK_UP_MASK) != 17230 SATA_PORT_DEVLINK_UP || 17231 sata_device.satadev_type == SATA_DTYPE_NONE) { 17232 /* 17233 * No device to process, anymore. Some other event processing 17234 * would or have already performed port info cleanup. 17235 * To be safe (HBA may need it), request clearing device 17236 * reset condition. 17237 */ 17238 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 17239 if (sdinfo != NULL) { 17240 sdinfo->satadrv_event_flags &= 17241 ~SATA_EVNT_INPROC_DEVICE_RESET; 17242 sdinfo->satadrv_event_flags |= 17243 SATA_EVNT_CLEAR_DEVICE_RESET; 17244 } 17245 return; 17246 } 17247 17248 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 17249 if (sdinfo == NULL) { 17250 return; 17251 } 17252 if ((sdinfo->satadrv_event_flags & 17253 SATA_EVNT_INPROC_DEVICE_RESET) == 0) { 17254 /* 17255 * Start tracking time for device feature restoration and 17256 * identification. Save current time (lbolt value). 17257 */ 17258 sdinfo->satadrv_reset_time = ddi_get_lbolt(); 17259 } 17260 /* Mark device reset processing as active */ 17261 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET; 17262 17263 old_sdinfo = *sdinfo; /* local copy of the drive info */ 17264 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17265 17266 rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1); 17267 17268 if (rval_set != SATA_SUCCESS) { 17269 /* 17270 * Restoring drive setting failed. 17271 * Probe the port first, to check if the port state has changed 17272 */ 17273 sata_device.satadev_rev = SATA_DEVICE_REV; 17274 sata_device.satadev_addr = *saddr; 17275 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 17276 /* probe port */ 17277 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 17278 (SATA_DIP(sata_hba_inst), &sata_device); 17279 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 17280 cport_mutex); 17281 if (rval_probe == SATA_SUCCESS && 17282 (sata_device.satadev_state & 17283 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 && 17284 (sata_device.satadev_scr.sstatus & 17285 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP && 17286 sata_device.satadev_type != SATA_DTYPE_NONE) { 17287 /* 17288 * We may retry this a bit later - in-process reset 17289 * condition should be already set. 17290 * Track retry time for device identification. 17291 */ 17292 if ((cportinfo->cport_dev_type & 17293 SATA_VALID_DEV_TYPE) != 0 && 17294 SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL && 17295 sdinfo->satadrv_reset_time != 0) { 17296 clock_t cur_time = ddi_get_lbolt(); 17297 /* 17298 * If the retry time limit was not 17299 * exceeded, retry. 17300 */ 17301 if ((cur_time - sdinfo->satadrv_reset_time) < 17302 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 17303 mutex_enter( 17304 &sata_hba_inst->satahba_mutex); 17305 sata_hba_inst->satahba_event_flags |= 17306 SATA_EVNT_MAIN; 17307 mutex_exit( 17308 &sata_hba_inst->satahba_mutex); 17309 mutex_enter(&sata_mutex); 17310 sata_event_pending |= SATA_EVNT_MAIN; 17311 mutex_exit(&sata_mutex); 17312 return; 17313 } 17314 if (rval_set == SATA_RETRY) { 17315 /* 17316 * Setting drive features failed, but 17317 * the drive is still accessible, 17318 * so emit a warning message before 17319 * return. 17320 */ 17321 mutex_exit(&SATA_CPORT_INFO( 17322 sata_hba_inst, 17323 saddr->cport)->cport_mutex); 17324 goto done; 17325 } 17326 } 17327 /* Fail the drive */ 17328 sdinfo->satadrv_state = SATA_DSTATE_FAILED; 17329 17330 sata_log(sata_hba_inst, CE_WARN, 17331 "SATA device at port %d - device failed", 17332 saddr->cport); 17333 } 17334 /* 17335 * No point of retrying - device failed or some other event 17336 * processing or already did or will do port info cleanup. 17337 * To be safe (HBA may need it), 17338 * request clearing device reset condition. 17339 */ 17340 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 17341 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET; 17342 sdinfo->satadrv_reset_time = 0; 17343 return; 17344 } 17345 done: 17346 /* 17347 * If setting of drive features failed, but the drive is still 17348 * accessible, emit a warning message. 17349 */ 17350 if (rval_set == SATA_RETRY) { 17351 sata_log(sata_hba_inst, CE_WARN, 17352 "SATA device at port %d - desired setting could not be " 17353 "restored after reset. Device may not operate as expected.", 17354 saddr->cport); 17355 } 17356 /* 17357 * Raise the flag indicating that the next sata command could 17358 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device 17359 * reset is reported. 17360 */ 17361 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17362 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 17363 sdinfo->satadrv_reset_time = 0; 17364 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) { 17365 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 17366 sdinfo->satadrv_event_flags &= 17367 ~SATA_EVNT_INPROC_DEVICE_RESET; 17368 sdinfo->satadrv_event_flags |= 17369 SATA_EVNT_CLEAR_DEVICE_RESET; 17370 } 17371 } 17372 } 17373 17374 17375 /* 17376 * Port Multiplier Port Device Reset Event processing. 17377 * 17378 * NOTE: This function has to be entered with pmport mutex held. It exits with 17379 * mutex held as well, but can release mutex during the processing. 17380 */ 17381 static void 17382 sata_process_pmdevice_reset(sata_hba_inst_t *sata_hba_inst, 17383 sata_address_t *saddr) 17384 { 17385 sata_drive_info_t old_sdinfo; /* local copy of the drive info */ 17386 sata_drive_info_t *sdinfo = NULL; 17387 sata_cport_info_t *cportinfo = NULL; 17388 sata_pmport_info_t *pmportinfo = NULL; 17389 sata_pmult_info_t *pminfo = NULL; 17390 sata_device_t sata_device; 17391 uint8_t cport = saddr->cport; 17392 uint8_t pmport = saddr->pmport; 17393 int rval; 17394 17395 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17396 "Processing drive reset at port %d:%d", cport, pmport); 17397 17398 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 17399 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 17400 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport); 17401 17402 /* 17403 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED 17404 * state, ignore reset event. 17405 */ 17406 if (((cportinfo->cport_state & 17407 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) || 17408 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 17409 sdinfo->satadrv_event_flags &= 17410 ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET); 17411 return; 17412 } 17413 17414 if ((pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) == 0) { 17415 /* 17416 * This should not happen - coding error. 17417 * But we can recover, so do not panic, just clean up 17418 * and if in debug mode, log the message. 17419 */ 17420 #ifdef SATA_DEBUG 17421 sata_log(sata_hba_inst, CE_WARN, 17422 "sata_process_pmdevice_reset: " 17423 "Invalid device type with sdinfo!", NULL); 17424 #endif 17425 sdinfo->satadrv_event_flags = 0; 17426 return; 17427 } 17428 17429 #ifdef SATA_DEBUG 17430 if ((sdinfo->satadrv_event_flags & 17431 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) { 17432 /* Nothing to do */ 17433 /* Something is weird - why we are processing dev reset? */ 17434 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17435 "No device reset event!!!!", NULL); 17436 17437 return; 17438 } 17439 if ((sdinfo->satadrv_event_flags & 17440 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 17441 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 17442 /* Something is weird - new device reset event */ 17443 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17444 "Overlapping device reset events!", NULL); 17445 } 17446 #endif 17447 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17448 "Processing port %d:%d device reset", cport, pmport); 17449 17450 /* Clear event flag */ 17451 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET; 17452 17453 /* It seems that we always need to check the port state first */ 17454 sata_device.satadev_rev = SATA_DEVICE_REV; 17455 sata_device.satadev_addr = *saddr; 17456 /* 17457 * We have to exit mutex, because the HBA probe port function may 17458 * block on its own mutex. 17459 */ 17460 mutex_exit(&pmportinfo->pmport_mutex); 17461 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 17462 (SATA_DIP(sata_hba_inst), &sata_device); 17463 mutex_enter(&pmportinfo->pmport_mutex); 17464 17465 sata_update_pmport_info(sata_hba_inst, &sata_device); 17466 if (rval != SATA_SUCCESS) { 17467 /* Something went wrong? Fail the port */ 17468 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 17469 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport, 17470 saddr->pmport); 17471 if (sdinfo != NULL) 17472 sdinfo->satadrv_event_flags = 0; 17473 mutex_exit(&pmportinfo->pmport_mutex); 17474 SATA_LOG_D((sata_hba_inst, CE_WARN, 17475 "SATA port %d:%d probing failed", 17476 saddr->cport, saddr->pmport)); 17477 mutex_enter(&pmportinfo->pmport_mutex); 17478 return; 17479 } 17480 if ((sata_device.satadev_scr.sstatus & 17481 SATA_PORT_DEVLINK_UP_MASK) != 17482 SATA_PORT_DEVLINK_UP || 17483 sata_device.satadev_type == SATA_DTYPE_NONE) { 17484 /* 17485 * No device to process, anymore. Some other event processing 17486 * would or have already performed port info cleanup. 17487 * To be safe (HBA may need it), request clearing device 17488 * reset condition. 17489 */ 17490 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport, 17491 saddr->pmport); 17492 if (sdinfo != NULL) { 17493 sdinfo->satadrv_event_flags &= 17494 ~SATA_EVNT_INPROC_DEVICE_RESET; 17495 /* must clear flags on cport */ 17496 pminfo = SATA_PMULT_INFO(sata_hba_inst, 17497 saddr->cport); 17498 pminfo->pmult_event_flags |= 17499 SATA_EVNT_CLEAR_DEVICE_RESET; 17500 } 17501 return; 17502 } 17503 17504 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport, 17505 saddr->pmport); 17506 if (sdinfo == NULL) { 17507 return; 17508 } 17509 if ((sdinfo->satadrv_event_flags & 17510 SATA_EVNT_INPROC_DEVICE_RESET) == 0) { 17511 /* 17512 * Start tracking time for device feature restoration and 17513 * identification. Save current time (lbolt value). 17514 */ 17515 sdinfo->satadrv_reset_time = ddi_get_lbolt(); 17516 } 17517 /* Mark device reset processing as active */ 17518 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET; 17519 17520 old_sdinfo = *sdinfo; /* local copy of the drive info */ 17521 mutex_exit(&pmportinfo->pmport_mutex); 17522 17523 if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) == 17524 SATA_FAILURE) { 17525 /* 17526 * Restoring drive setting failed. 17527 * Probe the port first, to check if the port state has changed 17528 */ 17529 sata_device.satadev_rev = SATA_DEVICE_REV; 17530 sata_device.satadev_addr = *saddr; 17531 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT; 17532 17533 /* probe port */ 17534 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 17535 (SATA_DIP(sata_hba_inst), &sata_device); 17536 mutex_enter(&pmportinfo->pmport_mutex); 17537 if (rval == SATA_SUCCESS && 17538 (sata_device.satadev_state & 17539 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 && 17540 (sata_device.satadev_scr.sstatus & 17541 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP && 17542 sata_device.satadev_type != SATA_DTYPE_NONE) { 17543 /* 17544 * We may retry this a bit later - in-process reset 17545 * condition should be already set. 17546 * Track retry time for device identification. 17547 */ 17548 if ((pmportinfo->pmport_dev_type & 17549 SATA_VALID_DEV_TYPE) != 0 && 17550 SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL && 17551 sdinfo->satadrv_reset_time != 0) { 17552 clock_t cur_time = ddi_get_lbolt(); 17553 /* 17554 * If the retry time limit was not 17555 * exceeded, retry. 17556 */ 17557 if ((cur_time - sdinfo->satadrv_reset_time) < 17558 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 17559 mutex_enter( 17560 &sata_hba_inst->satahba_mutex); 17561 sata_hba_inst->satahba_event_flags |= 17562 SATA_EVNT_MAIN; 17563 mutex_exit( 17564 &sata_hba_inst->satahba_mutex); 17565 mutex_enter(&sata_mutex); 17566 sata_event_pending |= SATA_EVNT_MAIN; 17567 mutex_exit(&sata_mutex); 17568 return; 17569 } 17570 } 17571 /* Fail the drive */ 17572 sdinfo->satadrv_state = SATA_DSTATE_FAILED; 17573 17574 sata_log(sata_hba_inst, CE_WARN, 17575 "SATA device at port %d:%d - device failed", 17576 saddr->cport, saddr->pmport); 17577 } else { 17578 /* 17579 * No point of retrying - some other event processing 17580 * would or already did port info cleanup. 17581 * To be safe (HBA may need it), 17582 * request clearing device reset condition. 17583 */ 17584 sdinfo->satadrv_event_flags |= 17585 SATA_EVNT_CLEAR_DEVICE_RESET; 17586 } 17587 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET; 17588 sdinfo->satadrv_reset_time = 0; 17589 return; 17590 } 17591 /* 17592 * Raise the flag indicating that the next sata command could 17593 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device 17594 * reset is reported. 17595 */ 17596 mutex_enter(&pmportinfo->pmport_mutex); 17597 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 17598 sdinfo->satadrv_reset_time = 0; 17599 if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) { 17600 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 17601 sdinfo->satadrv_event_flags &= 17602 ~SATA_EVNT_INPROC_DEVICE_RESET; 17603 /* must clear flags on cport */ 17604 pminfo = SATA_PMULT_INFO(sata_hba_inst, 17605 saddr->cport); 17606 pminfo->pmult_event_flags |= 17607 SATA_EVNT_CLEAR_DEVICE_RESET; 17608 } 17609 } 17610 } 17611 17612 /* 17613 * Port Link Events processing. 17614 * Every link established event may involve device reset (due to 17615 * COMRESET signal, equivalent of the hard reset) so arbitrarily 17616 * set device reset event for an attached device (if any). 17617 * If the port is in SHUTDOWN or FAILED state, ignore link events. 17618 * 17619 * The link established event processing varies, depending on the state 17620 * of the target node, HBA hotplugging capabilities, state of the port. 17621 * If the link is not active, the link established event is ignored. 17622 * If HBA cannot detect device attachment and there is no target node, 17623 * the link established event triggers device attach event processing. 17624 * Else, link established event triggers device reset event processing. 17625 * 17626 * The link lost event processing varies, depending on a HBA hotplugging 17627 * capability and the state of the port (link active or not active). 17628 * If the link is active, the lost link event is ignored. 17629 * If HBA cannot detect device removal, the lost link event triggers 17630 * device detached event processing after link lost timeout. 17631 * Else, the event is ignored. 17632 * 17633 * NOTE: Port multiplier ports events are handled by 17634 * sata_process_pmport_link_events(); 17635 */ 17636 static void 17637 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst, 17638 sata_address_t *saddr) 17639 { 17640 sata_device_t sata_device; 17641 sata_cport_info_t *cportinfo; 17642 sata_drive_info_t *sdinfo; 17643 uint32_t event_flags; 17644 int rval; 17645 17646 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17647 "Processing port %d link event(s)", saddr->cport); 17648 17649 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 17650 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17651 event_flags = cportinfo->cport_event_flags; 17652 17653 /* Reset event flags first */ 17654 cportinfo->cport_event_flags &= 17655 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST); 17656 17657 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */ 17658 if ((cportinfo->cport_state & 17659 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 17660 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 17661 cport_mutex); 17662 return; 17663 } 17664 17665 /* 17666 * For the sanity sake get current port state. 17667 * Set device address only. Other sata_device fields should be 17668 * set by HBA driver. 17669 */ 17670 sata_device.satadev_rev = SATA_DEVICE_REV; 17671 sata_device.satadev_addr = *saddr; 17672 /* 17673 * We have to exit mutex, because the HBA probe port function may 17674 * block on its own mutex. 17675 */ 17676 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17677 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 17678 (SATA_DIP(sata_hba_inst), &sata_device); 17679 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17680 sata_update_port_info(sata_hba_inst, &sata_device); 17681 if (rval != SATA_SUCCESS) { 17682 /* Something went wrong? Fail the port */ 17683 cportinfo->cport_state = SATA_PSTATE_FAILED; 17684 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 17685 cport_mutex); 17686 SATA_LOG_D((sata_hba_inst, CE_WARN, 17687 "SATA port %d probing failed", 17688 saddr->cport)); 17689 /* 17690 * We may want to release device info structure, but 17691 * it is not necessary. 17692 */ 17693 return; 17694 } else { 17695 /* port probed successfully */ 17696 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 17697 } 17698 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) { 17699 17700 if ((sata_device.satadev_scr.sstatus & 17701 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) { 17702 /* Ignore event */ 17703 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17704 "Ignoring port %d link established event - " 17705 "link down", 17706 saddr->cport); 17707 goto linklost; 17708 } 17709 17710 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17711 "Processing port %d link established event", 17712 saddr->cport); 17713 17714 /* 17715 * For the sanity sake check if a device is attached - check 17716 * return state of a port probing. 17717 */ 17718 if (sata_device.satadev_type != SATA_DTYPE_NONE) { 17719 /* 17720 * HBA port probe indicated that there is a device 17721 * attached. Check if the framework had device info 17722 * structure attached for this device. 17723 */ 17724 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 17725 ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) != 17726 NULL); 17727 17728 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 17729 if ((sdinfo->satadrv_type & 17730 SATA_VALID_DEV_TYPE) != 0) { 17731 /* 17732 * Dev info structure is present. 17733 * If dev_type is set to known type in 17734 * the framework's drive info struct 17735 * then the device existed before and 17736 * the link was probably lost 17737 * momentarily - in such case 17738 * we may want to check device 17739 * identity. 17740 * Identity check is not supported now. 17741 * 17742 * Link established event 17743 * triggers device reset event. 17744 */ 17745 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 17746 satadrv_event_flags |= 17747 SATA_EVNT_DEVICE_RESET; 17748 } 17749 } else if (cportinfo->cport_dev_type == 17750 SATA_DTYPE_NONE) { 17751 /* 17752 * We got new device attached! If HBA does not 17753 * generate device attached events, trigger it 17754 * here. 17755 */ 17756 if (!(SATA_FEATURES(sata_hba_inst) & 17757 SATA_CTLF_HOTPLUG)) { 17758 cportinfo->cport_event_flags |= 17759 SATA_EVNT_DEVICE_ATTACHED; 17760 } 17761 } 17762 /* Reset link lost timeout */ 17763 cportinfo->cport_link_lost_time = 0; 17764 } 17765 } 17766 linklost: 17767 if (event_flags & SATA_EVNT_LINK_LOST) { 17768 if ((sata_device.satadev_scr.sstatus & 17769 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) { 17770 /* Ignore event */ 17771 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17772 "Ignoring port %d link lost event - link is up", 17773 saddr->cport); 17774 goto done; 17775 } 17776 #ifdef SATA_DEBUG 17777 if (cportinfo->cport_link_lost_time == 0) { 17778 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17779 "Processing port %d link lost event", 17780 saddr->cport); 17781 } 17782 #endif 17783 /* 17784 * When HBA cannot generate device attached/detached events, 17785 * we need to track link lost time and eventually generate 17786 * device detach event. 17787 */ 17788 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) { 17789 /* We are tracking link lost time */ 17790 if (cportinfo->cport_link_lost_time == 0) { 17791 /* save current time (lbolt value) */ 17792 cportinfo->cport_link_lost_time = 17793 ddi_get_lbolt(); 17794 /* just keep link lost event */ 17795 cportinfo->cport_event_flags |= 17796 SATA_EVNT_LINK_LOST; 17797 } else { 17798 clock_t cur_time = ddi_get_lbolt(); 17799 if ((cur_time - 17800 cportinfo->cport_link_lost_time) >= 17801 drv_usectohz( 17802 SATA_EVNT_LINK_LOST_TIMEOUT)) { 17803 /* trigger device detach event */ 17804 cportinfo->cport_event_flags |= 17805 SATA_EVNT_DEVICE_DETACHED; 17806 cportinfo->cport_link_lost_time = 0; 17807 SATADBG1(SATA_DBG_EVENTS, 17808 sata_hba_inst, 17809 "Triggering port %d " 17810 "device detached event", 17811 saddr->cport); 17812 } else { 17813 /* keep link lost event */ 17814 cportinfo->cport_event_flags |= 17815 SATA_EVNT_LINK_LOST; 17816 } 17817 } 17818 } 17819 /* 17820 * We could change port state to disable/delay access to 17821 * the attached device until the link is recovered. 17822 */ 17823 } 17824 done: 17825 event_flags = cportinfo->cport_event_flags; 17826 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17827 if (event_flags != 0) { 17828 mutex_enter(&sata_hba_inst->satahba_mutex); 17829 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 17830 mutex_exit(&sata_hba_inst->satahba_mutex); 17831 mutex_enter(&sata_mutex); 17832 sata_event_pending |= SATA_EVNT_MAIN; 17833 mutex_exit(&sata_mutex); 17834 } 17835 } 17836 17837 /* 17838 * Port Multiplier Port Link Events processing. 17839 */ 17840 static void 17841 sata_process_pmport_link_events(sata_hba_inst_t *sata_hba_inst, 17842 sata_address_t *saddr) 17843 { 17844 sata_device_t sata_device; 17845 sata_pmport_info_t *pmportinfo = NULL; 17846 sata_drive_info_t *sdinfo = NULL; 17847 uint32_t event_flags; 17848 uint8_t cport = saddr->cport; 17849 uint8_t pmport = saddr->pmport; 17850 int rval; 17851 17852 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17853 "Processing port %d:%d link event(s)", 17854 cport, pmport); 17855 17856 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 17857 mutex_enter(&pmportinfo->pmport_mutex); 17858 event_flags = pmportinfo->pmport_event_flags; 17859 17860 /* Reset event flags first */ 17861 pmportinfo->pmport_event_flags &= 17862 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST); 17863 17864 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */ 17865 if ((pmportinfo->pmport_state & 17866 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 17867 mutex_exit(&pmportinfo->pmport_mutex); 17868 return; 17869 } 17870 17871 /* 17872 * For the sanity sake get current port state. 17873 * Set device address only. Other sata_device fields should be 17874 * set by HBA driver. 17875 */ 17876 sata_device.satadev_rev = SATA_DEVICE_REV; 17877 sata_device.satadev_addr = *saddr; 17878 /* 17879 * We have to exit mutex, because the HBA probe port function may 17880 * block on its own mutex. 17881 */ 17882 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 17883 saddr->pmport)); 17884 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 17885 (SATA_DIP(sata_hba_inst), &sata_device); 17886 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 17887 saddr->pmport)); 17888 sata_update_pmport_info(sata_hba_inst, &sata_device); 17889 if (rval != SATA_SUCCESS) { 17890 /* Something went wrong? Fail the port */ 17891 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 17892 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 17893 saddr->pmport)); 17894 SATA_LOG_D((sata_hba_inst, CE_WARN, 17895 "SATA port %d:%d probing failed", 17896 saddr->cport, saddr->pmport)); 17897 /* 17898 * We may want to release device info structure, but 17899 * it is not necessary. 17900 */ 17901 return; 17902 } else { 17903 /* port probed successfully */ 17904 pmportinfo->pmport_state |= 17905 SATA_STATE_PROBED | SATA_STATE_READY; 17906 } 17907 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, 17908 saddr->cport, saddr->pmport)); 17909 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, 17910 saddr->cport, saddr->pmport)); 17911 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) { 17912 17913 if ((sata_device.satadev_scr.sstatus & 17914 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) { 17915 /* Ignore event */ 17916 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17917 "Ignoring port %d:%d link established event - " 17918 "link down", 17919 saddr->cport, saddr->pmport); 17920 goto linklost; 17921 } 17922 17923 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17924 "Processing port %d:%d link established event", 17925 cport, pmport); 17926 17927 /* 17928 * For the sanity sake check if a device is attached - check 17929 * return state of a port probing. 17930 */ 17931 if (sata_device.satadev_type != SATA_DTYPE_NONE && 17932 sata_device.satadev_type != SATA_DTYPE_PMULT) { 17933 /* 17934 * HBA port probe indicated that there is a device 17935 * attached. Check if the framework had device info 17936 * structure attached for this device. 17937 */ 17938 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 17939 ASSERT(SATA_PMPORTINFO_DRV_INFO(pmportinfo) != 17940 NULL); 17941 17942 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 17943 if ((sdinfo->satadrv_type & 17944 SATA_VALID_DEV_TYPE) != 0) { 17945 /* 17946 * Dev info structure is present. 17947 * If dev_type is set to known type in 17948 * the framework's drive info struct 17949 * then the device existed before and 17950 * the link was probably lost 17951 * momentarily - in such case 17952 * we may want to check device 17953 * identity. 17954 * Identity check is not supported now. 17955 * 17956 * Link established event 17957 * triggers device reset event. 17958 */ 17959 (SATA_PMPORTINFO_DRV_INFO(pmportinfo))-> 17960 satadrv_event_flags |= 17961 SATA_EVNT_DEVICE_RESET; 17962 } 17963 } else if (pmportinfo->pmport_dev_type == 17964 SATA_DTYPE_NONE) { 17965 /* 17966 * We got new device attached! If HBA does not 17967 * generate device attached events, trigger it 17968 * here. 17969 */ 17970 if (!(SATA_FEATURES(sata_hba_inst) & 17971 SATA_CTLF_HOTPLUG)) { 17972 pmportinfo->pmport_event_flags |= 17973 SATA_EVNT_DEVICE_ATTACHED; 17974 } 17975 } 17976 /* Reset link lost timeout */ 17977 pmportinfo->pmport_link_lost_time = 0; 17978 } 17979 } 17980 linklost: 17981 if (event_flags & SATA_EVNT_LINK_LOST) { 17982 #ifdef SATA_DEBUG 17983 if (pmportinfo->pmport_link_lost_time == 0) { 17984 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17985 "Processing port %d:%d link lost event", 17986 saddr->cport, saddr->pmport); 17987 } 17988 #endif 17989 if ((sata_device.satadev_scr.sstatus & 17990 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) { 17991 /* Ignore event */ 17992 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17993 "Ignoring port %d:%d link lost event - link is up", 17994 saddr->cport, saddr->pmport); 17995 goto done; 17996 } 17997 /* 17998 * When HBA cannot generate device attached/detached events, 17999 * we need to track link lost time and eventually generate 18000 * device detach event. 18001 */ 18002 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) { 18003 /* We are tracking link lost time */ 18004 if (pmportinfo->pmport_link_lost_time == 0) { 18005 /* save current time (lbolt value) */ 18006 pmportinfo->pmport_link_lost_time = 18007 ddi_get_lbolt(); 18008 /* just keep link lost event */ 18009 pmportinfo->pmport_event_flags |= 18010 SATA_EVNT_LINK_LOST; 18011 } else { 18012 clock_t cur_time = ddi_get_lbolt(); 18013 if ((cur_time - 18014 pmportinfo->pmport_link_lost_time) >= 18015 drv_usectohz( 18016 SATA_EVNT_LINK_LOST_TIMEOUT)) { 18017 /* trigger device detach event */ 18018 pmportinfo->pmport_event_flags |= 18019 SATA_EVNT_DEVICE_DETACHED; 18020 pmportinfo->pmport_link_lost_time = 0; 18021 SATADBG2(SATA_DBG_EVENTS, 18022 sata_hba_inst, 18023 "Triggering port %d:%d " 18024 "device detached event", 18025 saddr->cport, saddr->pmport); 18026 } else { 18027 /* keep link lost event */ 18028 pmportinfo->pmport_event_flags |= 18029 SATA_EVNT_LINK_LOST; 18030 } 18031 } 18032 } 18033 /* 18034 * We could change port state to disable/delay access to 18035 * the attached device until the link is recovered. 18036 */ 18037 } 18038 done: 18039 event_flags = pmportinfo->pmport_event_flags; 18040 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 18041 saddr->pmport)); 18042 if (event_flags != 0) { 18043 mutex_enter(&sata_hba_inst->satahba_mutex); 18044 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 18045 mutex_exit(&sata_hba_inst->satahba_mutex); 18046 mutex_enter(&sata_mutex); 18047 sata_event_pending |= SATA_EVNT_MAIN; 18048 mutex_exit(&sata_mutex); 18049 } 18050 } 18051 18052 /* 18053 * Device Detached Event processing. 18054 * Port is probed to find if a device is really gone. If so, 18055 * the device info structure is detached from the SATA port info structure 18056 * and released. 18057 * Port status is updated. 18058 * 18059 * NOTE: Port multiplier ports events are handled by 18060 * sata_process_pmdevice_detached() 18061 */ 18062 static void 18063 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst, 18064 sata_address_t *saddr) 18065 { 18066 sata_cport_info_t *cportinfo; 18067 sata_pmport_info_t *pmportinfo; 18068 sata_drive_info_t *sdevinfo; 18069 sata_device_t sata_device; 18070 sata_address_t pmport_addr; 18071 char name[16]; 18072 uint8_t cport = saddr->cport; 18073 int npmport; 18074 int rval; 18075 18076 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18077 "Processing port %d device detached", saddr->cport); 18078 18079 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 18080 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18081 /* Clear event flag */ 18082 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED; 18083 18084 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */ 18085 if ((cportinfo->cport_state & 18086 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 18087 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18088 cport_mutex); 18089 return; 18090 } 18091 /* For sanity, re-probe the port */ 18092 sata_device.satadev_rev = SATA_DEVICE_REV; 18093 sata_device.satadev_addr = *saddr; 18094 18095 /* 18096 * We have to exit mutex, because the HBA probe port function may 18097 * block on its own mutex. 18098 */ 18099 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18100 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18101 (SATA_DIP(sata_hba_inst), &sata_device); 18102 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18103 sata_update_port_info(sata_hba_inst, &sata_device); 18104 if (rval != SATA_SUCCESS) { 18105 /* Something went wrong? Fail the port */ 18106 cportinfo->cport_state = SATA_PSTATE_FAILED; 18107 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18108 cport_mutex); 18109 SATA_LOG_D((sata_hba_inst, CE_WARN, 18110 "SATA port %d probing failed", 18111 saddr->cport)); 18112 /* 18113 * We may want to release device info structure, but 18114 * it is not necessary. 18115 */ 18116 return; 18117 } else { 18118 /* port probed successfully */ 18119 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 18120 } 18121 /* 18122 * Check if a device is still attached. For sanity, check also 18123 * link status - if no link, there is no device. 18124 */ 18125 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) == 18126 SATA_PORT_DEVLINK_UP && sata_device.satadev_type != 18127 SATA_DTYPE_NONE) { 18128 /* 18129 * Device is still attached - ignore detach event. 18130 */ 18131 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18132 cport_mutex); 18133 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18134 "Ignoring detach - device still attached to port %d", 18135 sata_device.satadev_addr.cport); 18136 return; 18137 } 18138 /* 18139 * We need to detach and release device info structure here 18140 */ 18141 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 18142 /* 18143 * A port-multiplier is removed. 18144 * 18145 * Calling sata_process_pmdevice_detached() does not work 18146 * here. The port multiplier is gone, so we cannot probe 18147 * sub-port any more and all pmult-related data structure must 18148 * be de-allocated immediately. Following structure of every 18149 * implemented sub-port behind the pmult are required to 18150 * released. 18151 * 18152 * - attachment point 18153 * - target node 18154 * - sata_drive_info 18155 * - sata_pmport_info 18156 */ 18157 for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, 18158 cport); npmport ++) { 18159 SATADBG2(SATA_DBG_PMULT|SATA_DBG_EVENTS_PROC, 18160 sata_hba_inst, 18161 "Detaching target node at port %d:%d", 18162 cport, npmport); 18163 18164 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 18165 18166 /* Remove attachment point. */ 18167 name[0] = '\0'; 18168 (void) sprintf(name, "%d.%d", cport, npmport); 18169 ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name); 18170 sata_log(sata_hba_inst, CE_NOTE, 18171 "Remove attachment point of port %d:%d", 18172 cport, npmport); 18173 18174 /* Remove target node */ 18175 pmport_addr.cport = cport; 18176 pmport_addr.pmport = (uint8_t)npmport; 18177 pmport_addr.qual = SATA_ADDR_PMPORT; 18178 sata_remove_target_node(sata_hba_inst, &pmport_addr); 18179 18180 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 18181 18182 /* Release sata_pmport_info & sata_drive_info. */ 18183 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 18184 cport, npmport); 18185 ASSERT(pmportinfo != NULL); 18186 18187 sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 18188 if (sdevinfo != NULL) { 18189 (void) kmem_free((void *) sdevinfo, 18190 sizeof (sata_drive_info_t)); 18191 } 18192 18193 /* Release sata_pmport_info at last */ 18194 (void) kmem_free((void *) pmportinfo, 18195 sizeof (sata_pmport_info_t)); 18196 } 18197 18198 /* Finally, release sata_pmult_info */ 18199 (void) kmem_free((void *) 18200 SATA_CPORTINFO_PMULT_INFO(cportinfo), 18201 sizeof (sata_pmult_info_t)); 18202 SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL; 18203 18204 sata_log(sata_hba_inst, CE_WARN, 18205 "SATA port-multiplier detached at port %d", cport); 18206 18207 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 18208 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 18209 saddr->cport)->cport_mutex); 18210 } else { 18211 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 18212 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 18213 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 18214 (void) kmem_free((void *)sdevinfo, 18215 sizeof (sata_drive_info_t)); 18216 } 18217 sata_log(sata_hba_inst, CE_WARN, 18218 "SATA device detached at port %d", cport); 18219 18220 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 18221 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 18222 saddr->cport)->cport_mutex); 18223 18224 /* 18225 * Try to offline a device and remove target node 18226 * if it still exists 18227 */ 18228 sata_remove_target_node(sata_hba_inst, saddr); 18229 } 18230 18231 18232 /* 18233 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 18234 * with the hint: SE_HINT_REMOVE 18235 */ 18236 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE); 18237 } 18238 18239 /* 18240 * Port Multiplier Port Device Deattached Event processing. 18241 * 18242 * NOTE: No Mutex should be hold. 18243 */ 18244 static void 18245 sata_process_pmdevice_detached(sata_hba_inst_t *sata_hba_inst, 18246 sata_address_t *saddr) 18247 { 18248 sata_pmport_info_t *pmportinfo; 18249 sata_drive_info_t *sdevinfo; 18250 sata_device_t sata_device; 18251 int rval; 18252 uint8_t cport, pmport; 18253 18254 cport = saddr->cport; 18255 pmport = saddr->pmport; 18256 18257 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18258 "Processing port %d:%d device detached", 18259 cport, pmport); 18260 18261 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 18262 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18263 18264 /* Clear event flag */ 18265 pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED; 18266 18267 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */ 18268 if ((pmportinfo->pmport_state & 18269 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 18270 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18271 return; 18272 } 18273 /* For sanity, re-probe the port */ 18274 sata_device.satadev_rev = SATA_DEVICE_REV; 18275 sata_device.satadev_addr = *saddr; 18276 18277 /* 18278 * We have to exit mutex, because the HBA probe port function may 18279 * block on its own mutex. 18280 */ 18281 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18282 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18283 (SATA_DIP(sata_hba_inst), &sata_device); 18284 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18285 sata_update_pmport_info(sata_hba_inst, &sata_device); 18286 if (rval != SATA_SUCCESS) { 18287 /* Something went wrong? Fail the port */ 18288 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 18289 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18290 SATA_LOG_D((sata_hba_inst, CE_WARN, 18291 "SATA port %d:%d probing failed", 18292 saddr->pmport)); 18293 /* 18294 * We may want to release device info structure, but 18295 * it is not necessary. 18296 */ 18297 return; 18298 } else { 18299 /* port probed successfully */ 18300 pmportinfo->pmport_state |= 18301 SATA_STATE_PROBED | SATA_STATE_READY; 18302 } 18303 /* 18304 * Check if a device is still attached. For sanity, check also 18305 * link status - if no link, there is no device. 18306 */ 18307 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) == 18308 SATA_PORT_DEVLINK_UP && sata_device.satadev_type != 18309 SATA_DTYPE_NONE) { 18310 /* 18311 * Device is still attached - ignore detach event. 18312 */ 18313 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18314 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18315 "Ignoring detach - device still attached to port %d", 18316 sata_device.satadev_addr.pmport); 18317 return; 18318 } 18319 /* 18320 * We need to detach and release device info structure here 18321 */ 18322 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 18323 sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 18324 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 18325 (void) kmem_free((void *)sdevinfo, 18326 sizeof (sata_drive_info_t)); 18327 } 18328 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 18329 /* 18330 * Device cannot be reached anymore, even if the target node may be 18331 * still present. 18332 */ 18333 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18334 18335 /* 18336 * Try to offline a device and remove target node if it still exists 18337 */ 18338 sata_remove_target_node(sata_hba_inst, saddr); 18339 18340 /* 18341 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 18342 * with the hint: SE_HINT_REMOVE 18343 */ 18344 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE); 18345 } 18346 18347 18348 /* 18349 * Device Attached Event processing. 18350 * Port state is checked to verify that a device is really attached. If so, 18351 * the device info structure is created and attached to the SATA port info 18352 * structure. 18353 * 18354 * If attached device cannot be identified or set-up, the retry for the 18355 * attach processing is set-up. Subsequent daemon run would try again to 18356 * identify the device, until the time limit is reached 18357 * (SATA_DEV_IDENTIFY_TIMEOUT). 18358 * 18359 * This function cannot be called in interrupt context (it may sleep). 18360 * 18361 * NOTE: Port multiplier ports events are handled by 18362 * sata_process_pmdevice_attached() 18363 */ 18364 static void 18365 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst, 18366 sata_address_t *saddr) 18367 { 18368 sata_cport_info_t *cportinfo = NULL; 18369 sata_drive_info_t *sdevinfo = NULL; 18370 sata_pmult_info_t *pmultinfo = NULL; 18371 sata_pmport_info_t *pmportinfo = NULL; 18372 sata_device_t sata_device; 18373 dev_info_t *tdip; 18374 uint32_t event_flags = 0, pmult_event_flags = 0; 18375 int rval; 18376 int npmport; 18377 18378 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18379 "Processing port %d device attached", saddr->cport); 18380 18381 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 18382 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18383 18384 /* Clear attach event flag first */ 18385 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED; 18386 18387 /* If the port is in SHUTDOWN or FAILED state, ignore event. */ 18388 if ((cportinfo->cport_state & 18389 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 18390 cportinfo->cport_dev_attach_time = 0; 18391 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18392 cport_mutex); 18393 return; 18394 } 18395 18396 /* 18397 * If the sata_drive_info structure is found attached to the port info, 18398 * despite the fact the device was removed and now it is re-attached, 18399 * the old drive info structure was not removed. 18400 * Arbitrarily release device info structure. 18401 */ 18402 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 18403 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 18404 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 18405 (void) kmem_free((void *)sdevinfo, 18406 sizeof (sata_drive_info_t)); 18407 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18408 "Arbitrarily detaching old device info.", NULL); 18409 } 18410 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 18411 18412 /* For sanity, re-probe the port */ 18413 sata_device.satadev_rev = SATA_DEVICE_REV; 18414 sata_device.satadev_addr = *saddr; 18415 18416 /* 18417 * We have to exit mutex, because the HBA probe port function may 18418 * block on its own mutex. 18419 */ 18420 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18421 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18422 (SATA_DIP(sata_hba_inst), &sata_device); 18423 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18424 sata_update_port_info(sata_hba_inst, &sata_device); 18425 if (rval != SATA_SUCCESS) { 18426 /* Something went wrong? Fail the port */ 18427 cportinfo->cport_state = SATA_PSTATE_FAILED; 18428 cportinfo->cport_dev_attach_time = 0; 18429 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18430 cport_mutex); 18431 SATA_LOG_D((sata_hba_inst, CE_WARN, 18432 "SATA port %d probing failed", 18433 saddr->cport)); 18434 return; 18435 } else { 18436 /* port probed successfully */ 18437 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 18438 } 18439 /* 18440 * Check if a device is still attached. For sanity, check also 18441 * link status - if no link, there is no device. 18442 */ 18443 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 18444 SATA_PORT_DEVLINK_UP || sata_device.satadev_type == 18445 SATA_DTYPE_NONE) { 18446 /* 18447 * No device - ignore attach event. 18448 */ 18449 cportinfo->cport_dev_attach_time = 0; 18450 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18451 cport_mutex); 18452 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18453 "Ignoring attach - no device connected to port %d", 18454 sata_device.satadev_addr.cport); 18455 return; 18456 } 18457 18458 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18459 /* 18460 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 18461 * with the hint: SE_HINT_INSERT 18462 */ 18463 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT); 18464 18465 /* 18466 * Port reprobing will take care of the creation of the device 18467 * info structure and determination of the device type. 18468 */ 18469 sata_device.satadev_addr = *saddr; 18470 (void) sata_reprobe_port(sata_hba_inst, &sata_device, 18471 SATA_DEV_IDENTIFY_NORETRY); 18472 18473 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18474 cport_mutex); 18475 if ((cportinfo->cport_state & SATA_STATE_READY) && 18476 (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) { 18477 /* Some device is attached to the port */ 18478 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) { 18479 /* 18480 * A device was not successfully attached. 18481 * Track retry time for device identification. 18482 */ 18483 if (cportinfo->cport_dev_attach_time != 0) { 18484 clock_t cur_time = ddi_get_lbolt(); 18485 /* 18486 * If the retry time limit was not exceeded, 18487 * reinstate attach event. 18488 */ 18489 if ((cur_time - 18490 cportinfo->cport_dev_attach_time) < 18491 drv_usectohz( 18492 SATA_DEV_IDENTIFY_TIMEOUT)) { 18493 /* OK, restore attach event */ 18494 cportinfo->cport_event_flags |= 18495 SATA_EVNT_DEVICE_ATTACHED; 18496 } else { 18497 /* Timeout - cannot identify device */ 18498 cportinfo->cport_dev_attach_time = 0; 18499 sata_log(sata_hba_inst, 18500 CE_WARN, 18501 "Could not identify SATA device " 18502 "at port %d", 18503 saddr->cport); 18504 } 18505 } else { 18506 /* 18507 * Start tracking time for device 18508 * identification. 18509 * Save current time (lbolt value). 18510 */ 18511 cportinfo->cport_dev_attach_time = 18512 ddi_get_lbolt(); 18513 /* Restore attach event */ 18514 cportinfo->cport_event_flags |= 18515 SATA_EVNT_DEVICE_ATTACHED; 18516 } 18517 } else if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 18518 cportinfo->cport_dev_attach_time = 0; 18519 sata_log(sata_hba_inst, CE_NOTE, 18520 "SATA port-multiplier detected at port %d", 18521 saddr->cport); 18522 18523 if (SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL) { 18524 /* Log the info of new port multiplier */ 18525 sata_show_pmult_info(sata_hba_inst, 18526 &sata_device); 18527 } 18528 18529 ASSERT(SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL); 18530 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 18531 for (npmport = 0; npmport < 18532 pmultinfo->pmult_num_dev_ports; npmport++) { 18533 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 18534 saddr->cport, npmport); 18535 ASSERT(pmportinfo != NULL); 18536 18537 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 18538 saddr->cport)->cport_mutex); 18539 mutex_enter(&pmportinfo->pmport_mutex); 18540 /* Marked all pmports with link events. */ 18541 pmportinfo->pmport_event_flags = 18542 SATA_EVNT_LINK_ESTABLISHED; 18543 pmult_event_flags |= 18544 pmportinfo->pmport_event_flags; 18545 mutex_exit(&pmportinfo->pmport_mutex); 18546 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 18547 saddr->cport)->cport_mutex); 18548 } 18549 /* Auto-online is not available for PMult now. */ 18550 18551 } else { 18552 /* 18553 * If device was successfully attached, the subsequent 18554 * action depends on a state of the 18555 * sata_auto_online variable. If it is set to zero. 18556 * an explicit 'configure' command will be needed to 18557 * configure it. If its value is non-zero, we will 18558 * attempt to online (configure) the device. 18559 * First, log the message indicating that a device 18560 * was attached. 18561 */ 18562 cportinfo->cport_dev_attach_time = 0; 18563 sata_log(sata_hba_inst, CE_WARN, 18564 "SATA device detected at port %d", saddr->cport); 18565 18566 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 18567 sata_drive_info_t new_sdinfo; 18568 18569 /* Log device info data */ 18570 new_sdinfo = *(SATA_CPORTINFO_DRV_INFO( 18571 cportinfo)); 18572 sata_show_drive_info(sata_hba_inst, 18573 &new_sdinfo); 18574 } 18575 18576 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 18577 saddr->cport)->cport_mutex); 18578 18579 /* 18580 * Make sure that there is no target node for that 18581 * device. If so, release it. It should not happen, 18582 * unless we had problem removing the node when 18583 * device was detached. 18584 */ 18585 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 18586 saddr->cport, saddr->pmport); 18587 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 18588 saddr->cport)->cport_mutex); 18589 if (tdip != NULL) { 18590 18591 #ifdef SATA_DEBUG 18592 if ((cportinfo->cport_event_flags & 18593 SATA_EVNT_TARGET_NODE_CLEANUP) == 0) 18594 sata_log(sata_hba_inst, CE_WARN, 18595 "sata_process_device_attached: " 18596 "old device target node exists!"); 18597 #endif 18598 /* 18599 * target node exists - try to unconfigure 18600 * device and remove the node. 18601 */ 18602 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 18603 saddr->cport)->cport_mutex); 18604 rval = ndi_devi_offline(tdip, 18605 NDI_DEVI_REMOVE); 18606 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 18607 saddr->cport)->cport_mutex); 18608 18609 if (rval == NDI_SUCCESS) { 18610 cportinfo->cport_event_flags &= 18611 ~SATA_EVNT_TARGET_NODE_CLEANUP; 18612 cportinfo->cport_tgtnode_clean = B_TRUE; 18613 } else { 18614 /* 18615 * PROBLEM - the target node remained 18616 * and it belongs to a previously 18617 * attached device. 18618 * This happens when the file was open 18619 * or the node was waiting for 18620 * resources at the time the 18621 * associated device was removed. 18622 * Instruct event daemon to retry the 18623 * cleanup later. 18624 */ 18625 sata_log(sata_hba_inst, 18626 CE_WARN, 18627 "Application(s) accessing " 18628 "previously attached SATA " 18629 "device have to release " 18630 "it before newly inserted " 18631 "device can be made accessible.", 18632 saddr->cport); 18633 cportinfo->cport_event_flags |= 18634 SATA_EVNT_TARGET_NODE_CLEANUP; 18635 cportinfo->cport_tgtnode_clean = 18636 B_FALSE; 18637 } 18638 } 18639 if (sata_auto_online != 0) { 18640 cportinfo->cport_event_flags |= 18641 SATA_EVNT_AUTOONLINE_DEVICE; 18642 } 18643 18644 } 18645 } else { 18646 cportinfo->cport_dev_attach_time = 0; 18647 } 18648 18649 event_flags = cportinfo->cport_event_flags; 18650 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18651 if (event_flags != 0 || pmult_event_flags != 0) { 18652 mutex_enter(&sata_hba_inst->satahba_mutex); 18653 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 18654 mutex_exit(&sata_hba_inst->satahba_mutex); 18655 mutex_enter(&sata_mutex); 18656 sata_event_pending |= SATA_EVNT_MAIN; 18657 mutex_exit(&sata_mutex); 18658 } 18659 } 18660 18661 /* 18662 * Port Multiplier Port Device Attached Event processing. 18663 * 18664 * NOTE: No Mutex should be hold. 18665 */ 18666 static void 18667 sata_process_pmdevice_attached(sata_hba_inst_t *sata_hba_inst, 18668 sata_address_t *saddr) 18669 { 18670 sata_pmport_info_t *pmportinfo; 18671 sata_drive_info_t *sdinfo; 18672 sata_device_t sata_device; 18673 dev_info_t *tdip; 18674 uint32_t event_flags; 18675 uint8_t cport = saddr->cport; 18676 uint8_t pmport = saddr->pmport; 18677 int rval; 18678 18679 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18680 "Processing port %d:%d device attached", cport, pmport); 18681 18682 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 18683 18684 mutex_enter(&pmportinfo->pmport_mutex); 18685 18686 /* Clear attach event flag first */ 18687 pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED; 18688 18689 /* If the port is in SHUTDOWN or FAILED state, ignore event. */ 18690 if ((pmportinfo->pmport_state & 18691 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 18692 pmportinfo->pmport_dev_attach_time = 0; 18693 mutex_exit(&pmportinfo->pmport_mutex); 18694 return; 18695 } 18696 18697 /* 18698 * If the sata_drive_info structure is found attached to the port info, 18699 * despite the fact the device was removed and now it is re-attached, 18700 * the old drive info structure was not removed. 18701 * Arbitrarily release device info structure. 18702 */ 18703 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 18704 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 18705 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 18706 (void) kmem_free((void *)sdinfo, 18707 sizeof (sata_drive_info_t)); 18708 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18709 "Arbitrarily detaching old device info.", NULL); 18710 } 18711 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 18712 18713 /* For sanity, re-probe the port */ 18714 sata_device.satadev_rev = SATA_DEVICE_REV; 18715 sata_device.satadev_addr = *saddr; 18716 18717 /* 18718 * We have to exit mutex, because the HBA probe port function may 18719 * block on its own mutex. 18720 */ 18721 mutex_exit(&pmportinfo->pmport_mutex); 18722 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18723 (SATA_DIP(sata_hba_inst), &sata_device); 18724 mutex_enter(&pmportinfo->pmport_mutex); 18725 18726 sata_update_pmport_info(sata_hba_inst, &sata_device); 18727 if (rval != SATA_SUCCESS) { 18728 /* Something went wrong? Fail the port */ 18729 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 18730 pmportinfo->pmport_dev_attach_time = 0; 18731 mutex_exit(&pmportinfo->pmport_mutex); 18732 SATA_LOG_D((sata_hba_inst, CE_WARN, 18733 "SATA port %d:%d probing failed", cport, pmport)); 18734 return; 18735 } else { 18736 /* pmport probed successfully */ 18737 pmportinfo->pmport_state |= 18738 SATA_STATE_PROBED | SATA_STATE_READY; 18739 } 18740 /* 18741 * Check if a device is still attached. For sanity, check also 18742 * link status - if no link, there is no device. 18743 */ 18744 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 18745 SATA_PORT_DEVLINK_UP || sata_device.satadev_type == 18746 SATA_DTYPE_NONE) { 18747 /* 18748 * No device - ignore attach event. 18749 */ 18750 pmportinfo->pmport_dev_attach_time = 0; 18751 mutex_exit(&pmportinfo->pmport_mutex); 18752 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18753 "Ignoring attach - no device connected to port %d:%d", 18754 cport, pmport); 18755 return; 18756 } 18757 18758 mutex_exit(&pmportinfo->pmport_mutex); 18759 /* 18760 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 18761 * with the hint: SE_HINT_INSERT 18762 */ 18763 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT); 18764 18765 /* 18766 * Port reprobing will take care of the creation of the device 18767 * info structure and determination of the device type. 18768 */ 18769 sata_device.satadev_addr = *saddr; 18770 (void) sata_reprobe_port(sata_hba_inst, &sata_device, 18771 SATA_DEV_IDENTIFY_NORETRY); 18772 18773 mutex_enter(&pmportinfo->pmport_mutex); 18774 if ((pmportinfo->pmport_state & SATA_STATE_READY) && 18775 (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE)) { 18776 /* Some device is attached to the port */ 18777 if (pmportinfo->pmport_dev_type == SATA_DTYPE_UNKNOWN) { 18778 /* 18779 * A device was not successfully attached. 18780 * Track retry time for device identification. 18781 */ 18782 if (pmportinfo->pmport_dev_attach_time != 0) { 18783 clock_t cur_time = ddi_get_lbolt(); 18784 /* 18785 * If the retry time limit was not exceeded, 18786 * reinstate attach event. 18787 */ 18788 if ((cur_time - 18789 pmportinfo->pmport_dev_attach_time) < 18790 drv_usectohz( 18791 SATA_DEV_IDENTIFY_TIMEOUT)) { 18792 /* OK, restore attach event */ 18793 pmportinfo->pmport_event_flags |= 18794 SATA_EVNT_DEVICE_ATTACHED; 18795 } else { 18796 /* Timeout - cannot identify device */ 18797 pmportinfo->pmport_dev_attach_time = 0; 18798 sata_log(sata_hba_inst, CE_WARN, 18799 "Could not identify SATA device " 18800 "at port %d:%d", 18801 cport, pmport); 18802 } 18803 } else { 18804 /* 18805 * Start tracking time for device 18806 * identification. 18807 * Save current time (lbolt value). 18808 */ 18809 pmportinfo->pmport_dev_attach_time = 18810 ddi_get_lbolt(); 18811 /* Restore attach event */ 18812 pmportinfo->pmport_event_flags |= 18813 SATA_EVNT_DEVICE_ATTACHED; 18814 } 18815 } else { 18816 /* 18817 * If device was successfully attached, the subsequent 18818 * action depends on a state of the 18819 * sata_auto_online variable. If it is set to zero. 18820 * an explicit 'configure' command will be needed to 18821 * configure it. If its value is non-zero, we will 18822 * attempt to online (configure) the device. 18823 * First, log the message indicating that a device 18824 * was attached. 18825 */ 18826 pmportinfo->pmport_dev_attach_time = 0; 18827 sata_log(sata_hba_inst, CE_WARN, 18828 "SATA device detected at port %d:%d", 18829 cport, pmport); 18830 18831 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 18832 sata_drive_info_t new_sdinfo; 18833 18834 /* Log device info data */ 18835 new_sdinfo = *(SATA_PMPORTINFO_DRV_INFO( 18836 pmportinfo)); 18837 sata_show_drive_info(sata_hba_inst, 18838 &new_sdinfo); 18839 } 18840 18841 mutex_exit(&pmportinfo->pmport_mutex); 18842 18843 /* 18844 * Make sure that there is no target node for that 18845 * device. If so, release it. It should not happen, 18846 * unless we had problem removing the node when 18847 * device was detached. 18848 */ 18849 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 18850 saddr->cport, saddr->pmport); 18851 mutex_enter(&pmportinfo->pmport_mutex); 18852 if (tdip != NULL) { 18853 18854 #ifdef SATA_DEBUG 18855 if ((pmportinfo->pmport_event_flags & 18856 SATA_EVNT_TARGET_NODE_CLEANUP) == 0) 18857 sata_log(sata_hba_inst, CE_WARN, 18858 "sata_process_device_attached: " 18859 "old device target node exists!"); 18860 #endif 18861 /* 18862 * target node exists - try to unconfigure 18863 * device and remove the node. 18864 */ 18865 mutex_exit(&pmportinfo->pmport_mutex); 18866 rval = ndi_devi_offline(tdip, 18867 NDI_DEVI_REMOVE); 18868 mutex_enter(&pmportinfo->pmport_mutex); 18869 18870 if (rval == NDI_SUCCESS) { 18871 pmportinfo->pmport_event_flags &= 18872 ~SATA_EVNT_TARGET_NODE_CLEANUP; 18873 pmportinfo->pmport_tgtnode_clean = 18874 B_TRUE; 18875 } else { 18876 /* 18877 * PROBLEM - the target node remained 18878 * and it belongs to a previously 18879 * attached device. 18880 * This happens when the file was open 18881 * or the node was waiting for 18882 * resources at the time the 18883 * associated device was removed. 18884 * Instruct event daemon to retry the 18885 * cleanup later. 18886 */ 18887 sata_log(sata_hba_inst, 18888 CE_WARN, 18889 "Application(s) accessing " 18890 "previously attached SATA " 18891 "device have to release " 18892 "it before newly inserted " 18893 "device can be made accessible." 18894 "at port %d:%d", 18895 cport, pmport); 18896 pmportinfo->pmport_event_flags |= 18897 SATA_EVNT_TARGET_NODE_CLEANUP; 18898 pmportinfo->pmport_tgtnode_clean = 18899 B_FALSE; 18900 } 18901 } 18902 if (sata_auto_online != 0) { 18903 pmportinfo->pmport_event_flags |= 18904 SATA_EVNT_AUTOONLINE_DEVICE; 18905 } 18906 18907 } 18908 } else { 18909 pmportinfo->pmport_dev_attach_time = 0; 18910 } 18911 18912 event_flags = pmportinfo->pmport_event_flags; 18913 mutex_exit(&pmportinfo->pmport_mutex); 18914 if (event_flags != 0) { 18915 mutex_enter(&sata_hba_inst->satahba_mutex); 18916 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 18917 mutex_exit(&sata_hba_inst->satahba_mutex); 18918 mutex_enter(&sata_mutex); 18919 sata_event_pending |= SATA_EVNT_MAIN; 18920 mutex_exit(&sata_mutex); 18921 } 18922 18923 /* clear the reset_in_progress events */ 18924 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 18925 if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) { 18926 /* must clear flags on cport */ 18927 sata_pmult_info_t *pminfo = 18928 SATA_PMULT_INFO(sata_hba_inst, 18929 saddr->cport); 18930 pminfo->pmult_event_flags |= 18931 SATA_EVNT_CLEAR_DEVICE_RESET; 18932 } 18933 } 18934 } 18935 18936 /* 18937 * Device Target Node Cleanup Event processing. 18938 * If the target node associated with a sata port device is in 18939 * DEVI_DEVICE_REMOVED state, an attempt is made to remove it. 18940 * If the target node cannot be removed, the event flag is left intact, 18941 * so that event daemon may re-run this function later. 18942 * 18943 * This function cannot be called in interrupt context (it may sleep). 18944 * 18945 * NOTE: Processes cport events only, not port multiplier ports. 18946 */ 18947 static void 18948 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, 18949 sata_address_t *saddr) 18950 { 18951 sata_cport_info_t *cportinfo; 18952 dev_info_t *tdip; 18953 18954 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18955 "Processing port %d device target node cleanup", saddr->cport); 18956 18957 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 18958 18959 /* 18960 * Check if there is target node for that device and it is in the 18961 * DEVI_DEVICE_REMOVED state. If so, release it. 18962 */ 18963 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport, 18964 saddr->pmport); 18965 if (tdip != NULL) { 18966 /* 18967 * target node exists - check if it is target node of 18968 * a removed device. 18969 */ 18970 if (sata_check_device_removed(tdip) == B_TRUE) { 18971 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18972 "sata_process_target_node_cleanup: " 18973 "old device target node exists!", NULL); 18974 /* 18975 * Unconfigure and remove the target node 18976 */ 18977 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) == 18978 NDI_SUCCESS) { 18979 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 18980 saddr->cport)->cport_mutex); 18981 cportinfo->cport_event_flags &= 18982 ~SATA_EVNT_TARGET_NODE_CLEANUP; 18983 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 18984 saddr->cport)->cport_mutex); 18985 return; 18986 } 18987 /* 18988 * Event daemon will retry the cleanup later. 18989 */ 18990 mutex_enter(&sata_hba_inst->satahba_mutex); 18991 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 18992 mutex_exit(&sata_hba_inst->satahba_mutex); 18993 mutex_enter(&sata_mutex); 18994 sata_event_pending |= SATA_EVNT_MAIN; 18995 mutex_exit(&sata_mutex); 18996 } 18997 } else { 18998 if (saddr->qual == SATA_ADDR_CPORT || 18999 saddr->qual == SATA_ADDR_DCPORT) { 19000 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 19001 saddr->cport)->cport_mutex); 19002 cportinfo->cport_event_flags &= 19003 ~SATA_EVNT_TARGET_NODE_CLEANUP; 19004 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19005 saddr->cport)->cport_mutex); 19006 } else { 19007 /* sanity check */ 19008 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) != 19009 SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst, 19010 saddr->cport) == NULL) 19011 return; 19012 if (SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, 19013 saddr->pmport) == NULL) 19014 return; 19015 19016 mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst, 19017 saddr->cport, saddr->pmport)->pmport_mutex); 19018 SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, 19019 saddr->pmport)->pmport_event_flags &= 19020 ~SATA_EVNT_TARGET_NODE_CLEANUP; 19021 mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst, 19022 saddr->cport, saddr->pmport)->pmport_mutex); 19023 } 19024 } 19025 } 19026 19027 /* 19028 * Device AutoOnline Event processing. 19029 * If attached device is to be onlined, an attempt is made to online this 19030 * device, but only if there is no lingering (old) target node present. 19031 * If the device cannot be onlined, the event flag is left intact, 19032 * so that event daemon may re-run this function later. 19033 * 19034 * This function cannot be called in interrupt context (it may sleep). 19035 * 19036 * NOTE: Processes cport events only, not port multiplier ports. 19037 */ 19038 static void 19039 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst, 19040 sata_address_t *saddr) 19041 { 19042 sata_cport_info_t *cportinfo; 19043 sata_drive_info_t *sdinfo; 19044 sata_device_t sata_device; 19045 dev_info_t *tdip; 19046 19047 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19048 "Processing port %d attached device auto-onlining", saddr->cport); 19049 19050 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 19051 19052 /* 19053 * Check if device is present and recognized. If not, reset event. 19054 */ 19055 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19056 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) { 19057 /* Nothing to online */ 19058 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE; 19059 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19060 saddr->cport)->cport_mutex); 19061 return; 19062 } 19063 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19064 19065 /* 19066 * Check if there is target node for this device and if it is in the 19067 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep 19068 * the event for later processing. 19069 */ 19070 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport, 19071 saddr->pmport); 19072 if (tdip != NULL) { 19073 /* 19074 * target node exists - check if it is target node of 19075 * a removed device. 19076 */ 19077 if (sata_check_device_removed(tdip) == B_TRUE) { 19078 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19079 "sata_process_device_autoonline: " 19080 "old device target node exists!", NULL); 19081 /* 19082 * Event daemon will retry device onlining later. 19083 */ 19084 mutex_enter(&sata_hba_inst->satahba_mutex); 19085 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 19086 mutex_exit(&sata_hba_inst->satahba_mutex); 19087 mutex_enter(&sata_mutex); 19088 sata_event_pending |= SATA_EVNT_MAIN; 19089 mutex_exit(&sata_mutex); 19090 return; 19091 } 19092 /* 19093 * If the target node is not in the 'removed" state, assume 19094 * that it belongs to this device. There is nothing more to do, 19095 * but reset the event. 19096 */ 19097 } else { 19098 19099 /* 19100 * Try to online the device 19101 * If there is any reset-related event, remove it. We are 19102 * configuring the device and no state restoring is needed. 19103 */ 19104 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 19105 saddr->cport)->cport_mutex); 19106 sata_device.satadev_addr = *saddr; 19107 if (saddr->qual == SATA_ADDR_CPORT) 19108 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 19109 else 19110 sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT; 19111 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 19112 if (sdinfo != NULL) { 19113 if (sdinfo->satadrv_event_flags & 19114 (SATA_EVNT_DEVICE_RESET | 19115 SATA_EVNT_INPROC_DEVICE_RESET)) 19116 sdinfo->satadrv_event_flags = 0; 19117 sdinfo->satadrv_event_flags |= 19118 SATA_EVNT_CLEAR_DEVICE_RESET; 19119 19120 /* Need to create a new target node. */ 19121 cportinfo->cport_tgtnode_clean = B_TRUE; 19122 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19123 saddr->cport)->cport_mutex); 19124 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst), 19125 sata_hba_inst, &sata_device.satadev_addr); 19126 if (tdip == NULL) { 19127 /* 19128 * Configure (onlining) failed. 19129 * We will NOT retry 19130 */ 19131 SATA_LOG_D((sata_hba_inst, CE_WARN, 19132 "sata_process_device_autoonline: " 19133 "configuring SATA device at port %d failed", 19134 saddr->cport)); 19135 } 19136 } else { 19137 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19138 saddr->cport)->cport_mutex); 19139 } 19140 19141 } 19142 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19143 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE; 19144 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19145 saddr->cport)->cport_mutex); 19146 } 19147 19148 19149 static void 19150 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr, 19151 int hint) 19152 { 19153 char ap[MAXPATHLEN]; 19154 nvlist_t *ev_attr_list = NULL; 19155 int err; 19156 19157 /* Allocate and build sysevent attribute list */ 19158 err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP); 19159 if (err != 0) { 19160 SATA_LOG_D((sata_hba_inst, CE_WARN, 19161 "sata_gen_sysevent: " 19162 "cannot allocate memory for sysevent attributes\n")); 19163 return; 19164 } 19165 /* Add hint attribute */ 19166 err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint)); 19167 if (err != 0) { 19168 SATA_LOG_D((sata_hba_inst, CE_WARN, 19169 "sata_gen_sysevent: " 19170 "failed to add DR_HINT attr for sysevent")); 19171 nvlist_free(ev_attr_list); 19172 return; 19173 } 19174 /* 19175 * Add AP attribute. 19176 * Get controller pathname and convert it into AP pathname by adding 19177 * a target number. 19178 */ 19179 (void) snprintf(ap, MAXPATHLEN, "/devices"); 19180 (void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap)); 19181 (void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d", 19182 SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual)); 19183 19184 err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap); 19185 if (err != 0) { 19186 SATA_LOG_D((sata_hba_inst, CE_WARN, 19187 "sata_gen_sysevent: " 19188 "failed to add DR_AP_ID attr for sysevent")); 19189 nvlist_free(ev_attr_list); 19190 return; 19191 } 19192 19193 /* Generate/log sysevent */ 19194 err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR, 19195 ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP); 19196 if (err != DDI_SUCCESS) { 19197 SATA_LOG_D((sata_hba_inst, CE_WARN, 19198 "sata_gen_sysevent: " 19199 "cannot log sysevent, err code %x\n", err)); 19200 } 19201 19202 nvlist_free(ev_attr_list); 19203 } 19204 19205 19206 19207 19208 /* 19209 * Set DEVI_DEVICE_REMOVED state in the SATA device target node. 19210 */ 19211 static void 19212 sata_set_device_removed(dev_info_t *tdip) 19213 { 19214 int circ; 19215 19216 ASSERT(tdip != NULL); 19217 19218 ndi_devi_enter(tdip, &circ); 19219 mutex_enter(&DEVI(tdip)->devi_lock); 19220 DEVI_SET_DEVICE_REMOVED(tdip); 19221 mutex_exit(&DEVI(tdip)->devi_lock); 19222 ndi_devi_exit(tdip, circ); 19223 } 19224 19225 19226 /* 19227 * Set internal event instructing event daemon to try 19228 * to perform the target node cleanup. 19229 */ 19230 static void 19231 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, 19232 sata_address_t *saddr) 19233 { 19234 if (saddr->qual == SATA_ADDR_CPORT || 19235 saddr->qual == SATA_ADDR_DCPORT) { 19236 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 19237 saddr->cport)->cport_mutex); 19238 SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |= 19239 SATA_EVNT_TARGET_NODE_CLEANUP; 19240 SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19241 cport_tgtnode_clean = B_FALSE; 19242 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19243 saddr->cport)->cport_mutex); 19244 } else { 19245 mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst, 19246 saddr->cport, saddr->pmport)->pmport_mutex); 19247 SATA_PMPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport, 19248 saddr->pmport) |= SATA_EVNT_TARGET_NODE_CLEANUP; 19249 SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, saddr->pmport)-> 19250 pmport_tgtnode_clean = B_FALSE; 19251 mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst, 19252 saddr->cport, saddr->pmport)->pmport_mutex); 19253 } 19254 mutex_enter(&sata_hba_inst->satahba_mutex); 19255 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 19256 mutex_exit(&sata_hba_inst->satahba_mutex); 19257 mutex_enter(&sata_mutex); 19258 sata_event_pending |= SATA_EVNT_MAIN; 19259 mutex_exit(&sata_mutex); 19260 } 19261 19262 19263 /* 19264 * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state, 19265 * i.e. check if the target node state indicates that it belongs to a removed 19266 * device. 19267 * 19268 * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state, 19269 * B_FALSE otherwise. 19270 */ 19271 static boolean_t 19272 sata_check_device_removed(dev_info_t *tdip) 19273 { 19274 ASSERT(tdip != NULL); 19275 19276 if (DEVI_IS_DEVICE_REMOVED(tdip)) 19277 return (B_TRUE); 19278 else 19279 return (B_FALSE); 19280 } 19281 19282 /* ************************ FAULT INJECTTION **************************** */ 19283 19284 #ifdef SATA_INJECT_FAULTS 19285 19286 static uint32_t sata_fault_count = 0; 19287 static uint32_t sata_fault_suspend_count = 0; 19288 19289 /* 19290 * Inject sata pkt fault 19291 * It modifies returned values of the sata packet. 19292 * It returns immediately if: 19293 * pkt fault injection is not enabled (via sata_inject_fault, 19294 * sata_inject_fault_count), or invalid fault is specified (sata_fault_type), 19295 * or pkt does not contain command to be faulted (set in sata_fault_cmd), or 19296 * pkt is not directed to specified fault controller/device 19297 * (sata_fault_ctrl_dev and sata_fault_device). 19298 * If fault controller is not specified, fault injection applies to all 19299 * controllers and devices. 19300 * 19301 * First argument is the pointer to the executed sata packet. 19302 * Second argument is a pointer to a value returned by the HBA tran_start 19303 * function. 19304 * Third argument specifies injected error. Injected sata packet faults 19305 * are the satapkt_reason values. 19306 * SATA_PKT_BUSY -1 Not completed, busy 19307 * SATA_PKT_DEV_ERROR 1 Device reported error 19308 * SATA_PKT_QUEUE_FULL 2 Not accepted, queue full 19309 * SATA_PKT_PORT_ERROR 3 Not completed, port error 19310 * SATA_PKT_CMD_UNSUPPORTED 4 Cmd unsupported 19311 * SATA_PKT_ABORTED 5 Aborted by request 19312 * SATA_PKT_TIMEOUT 6 Operation timeut 19313 * SATA_PKT_RESET 7 Aborted by reset request 19314 * 19315 * Additional global variables affecting the execution: 19316 * 19317 * sata_inject_fault_count variable specifies number of times in row the 19318 * error is injected. Value of -1 specifies permanent fault, ie. every time 19319 * the fault injection point is reached, the fault is injected and a pause 19320 * between fault injection specified by sata_inject_fault_pause_count is 19321 * ignored). Fault injection routine decrements sata_inject_fault_count 19322 * (if greater than zero) until it reaches 0. No fault is injected when 19323 * sata_inject_fault_count is 0 (zero). 19324 * 19325 * sata_inject_fault_pause_count variable specifies number of times a fault 19326 * injection is bypassed (pause between fault injections). 19327 * If set to 0, a fault is injected only a number of times specified by 19328 * sata_inject_fault_count. 19329 * 19330 * The fault counts are static, so for periodic errors they have to be manually 19331 * reset to start repetition sequence from scratch. 19332 * If the original value returned by the HBA tran_start function is not 19333 * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error 19334 * is injected (to avoid masking real problems); 19335 * 19336 * NOTE: In its current incarnation, this function should be invoked only for 19337 * commands executed in SYNCHRONOUS mode. 19338 */ 19339 19340 19341 static void 19342 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault) 19343 { 19344 19345 if (sata_inject_fault != SATA_INJECT_PKT_FAULT) 19346 return; 19347 19348 if (sata_inject_fault_count == 0) 19349 return; 19350 19351 if (fault == 0) 19352 return; 19353 19354 if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg) 19355 return; 19356 19357 if (sata_fault_ctrl != NULL) { 19358 sata_pkt_txlate_t *spx = 19359 (sata_pkt_txlate_t *)spkt->satapkt_framework_private; 19360 19361 if (sata_fault_ctrl != NULL && sata_fault_ctrl != 19362 spx->txlt_sata_hba_inst->satahba_dip) 19363 return; 19364 19365 if (sata_fault_device.satadev_addr.cport != 19366 spkt->satapkt_device.satadev_addr.cport || 19367 sata_fault_device.satadev_addr.pmport != 19368 spkt->satapkt_device.satadev_addr.pmport || 19369 sata_fault_device.satadev_addr.qual != 19370 spkt->satapkt_device.satadev_addr.qual) 19371 return; 19372 } 19373 19374 /* Modify pkt return parameters */ 19375 if (*rval != SATA_TRAN_ACCEPTED || 19376 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 19377 sata_fault_count = 0; 19378 sata_fault_suspend_count = 0; 19379 return; 19380 } 19381 if (sata_fault_count == 0 && sata_fault_suspend_count != 0) { 19382 /* Pause in the injection */ 19383 sata_fault_suspend_count -= 1; 19384 return; 19385 } 19386 19387 if (sata_fault_count == 0 && sata_fault_suspend_count == 0) { 19388 /* 19389 * Init inject fault cycle. If fault count is set to -1, 19390 * it is a permanent fault. 19391 */ 19392 if (sata_inject_fault_count != -1) { 19393 sata_fault_count = sata_inject_fault_count; 19394 sata_fault_suspend_count = 19395 sata_inject_fault_pause_count; 19396 if (sata_fault_suspend_count == 0) 19397 sata_inject_fault_count = 0; 19398 } 19399 } 19400 19401 if (sata_fault_count != 0) 19402 sata_fault_count -= 1; 19403 19404 switch (fault) { 19405 case SATA_PKT_BUSY: 19406 *rval = SATA_TRAN_BUSY; 19407 spkt->satapkt_reason = SATA_PKT_BUSY; 19408 break; 19409 19410 case SATA_PKT_QUEUE_FULL: 19411 *rval = SATA_TRAN_QUEUE_FULL; 19412 spkt->satapkt_reason = SATA_PKT_QUEUE_FULL; 19413 break; 19414 19415 case SATA_PKT_CMD_UNSUPPORTED: 19416 *rval = SATA_TRAN_CMD_UNSUPPORTED; 19417 spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED; 19418 break; 19419 19420 case SATA_PKT_PORT_ERROR: 19421 /* This is "rejected" command */ 19422 *rval = SATA_TRAN_PORT_ERROR; 19423 spkt->satapkt_reason = SATA_PKT_PORT_ERROR; 19424 /* Additional error setup could be done here - port state */ 19425 break; 19426 19427 case SATA_PKT_DEV_ERROR: 19428 spkt->satapkt_reason = SATA_PKT_DEV_ERROR; 19429 /* 19430 * Additional error setup could be done here 19431 */ 19432 break; 19433 19434 case SATA_PKT_ABORTED: 19435 spkt->satapkt_reason = SATA_PKT_ABORTED; 19436 break; 19437 19438 case SATA_PKT_TIMEOUT: 19439 spkt->satapkt_reason = SATA_PKT_TIMEOUT; 19440 /* Additional error setup could be done here */ 19441 break; 19442 19443 case SATA_PKT_RESET: 19444 spkt->satapkt_reason = SATA_PKT_RESET; 19445 /* 19446 * Additional error setup could be done here - device reset 19447 */ 19448 break; 19449 19450 default: 19451 break; 19452 } 19453 } 19454 19455 #endif 19456 19457 /* 19458 * SATA Trace Ring Buffer 19459 * ---------------------- 19460 * 19461 * Overview 19462 * 19463 * The SATA trace ring buffer is a ring buffer created and managed by 19464 * the SATA framework module that can be used by any module or driver 19465 * within the SATA framework to store debug messages. 19466 * 19467 * Ring Buffer Interfaces: 19468 * 19469 * sata_vtrace_debug() <-- Adds debug message to ring buffer 19470 * sata_trace_debug() <-- Wraps varargs into sata_vtrace_debug() 19471 * 19472 * Note that the sata_trace_debug() interface was created to give 19473 * consumers the flexibilty of sending debug messages to ring buffer 19474 * as variable arguments. Consumers can send type va_list debug 19475 * messages directly to sata_vtrace_debug(). The sata_trace_debug() 19476 * and sata_vtrace_debug() relationship is similar to that of 19477 * cmn_err(9F) and vcmn_err(9F). 19478 * 19479 * Below is a diagram of the SATA trace ring buffer interfaces and 19480 * sample consumers: 19481 * 19482 * +---------------------------------+ 19483 * | o o SATA Framework Module | 19484 * | o SATA o +------------------+ +------------------+ 19485 * |o Trace o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1| 19486 * |o R-Buf o |sata_trace_debug |<--+ +------------------+ 19487 * | o o +------------------+ | +------------------+ 19488 * | o o ^ | +--|SATA HBA Driver #2| 19489 * | | | +------------------+ 19490 * | +------------------+ | 19491 * | |SATA Debug Message| | 19492 * | +------------------+ | 19493 * +---------------------------------+ 19494 * 19495 * Supporting Routines: 19496 * 19497 * sata_trace_rbuf_alloc() <-- Initializes ring buffer 19498 * sata_trace_rbuf_free() <-- Destroys ring buffer 19499 * sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer 19500 * sata_trace_dmsg_free() <-- Destroys content of ring buffer 19501 * 19502 * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE. 19503 * The ring buffer size can be adjusted by setting dmsg_ring_size in 19504 * /etc/system to desired size in unit of bytes. 19505 * 19506 * The individual debug message size in the ring buffer is restricted 19507 * to DMSG_BUF_SIZE. 19508 */ 19509 void 19510 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap) 19511 { 19512 sata_trace_dmsg_t *dmsg; 19513 19514 if (sata_debug_rbuf == NULL) { 19515 return; 19516 } 19517 19518 /* 19519 * If max size of ring buffer is smaller than size 19520 * required for one debug message then just return 19521 * since we have no room for the debug message. 19522 */ 19523 if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) { 19524 return; 19525 } 19526 19527 mutex_enter(&sata_debug_rbuf->lock); 19528 19529 /* alloc or reuse on ring buffer */ 19530 dmsg = sata_trace_dmsg_alloc(); 19531 19532 if (dmsg == NULL) { 19533 /* resource allocation failed */ 19534 mutex_exit(&sata_debug_rbuf->lock); 19535 return; 19536 } 19537 19538 dmsg->dip = dip; 19539 gethrestime(&dmsg->timestamp); 19540 19541 (void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap); 19542 19543 mutex_exit(&sata_debug_rbuf->lock); 19544 } 19545 19546 void 19547 sata_trace_debug(dev_info_t *dip, const char *fmt, ...) 19548 { 19549 va_list ap; 19550 19551 va_start(ap, fmt); 19552 sata_vtrace_debug(dip, fmt, ap); 19553 va_end(ap); 19554 } 19555 19556 /* 19557 * This routine is used to manage debug messages 19558 * on ring buffer. 19559 */ 19560 static sata_trace_dmsg_t * 19561 sata_trace_dmsg_alloc(void) 19562 { 19563 sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp; 19564 19565 if (sata_debug_rbuf->looped == TRUE) { 19566 sata_debug_rbuf->dmsgp = dmsg->next; 19567 return (sata_debug_rbuf->dmsgp); 19568 } 19569 19570 /* 19571 * If we're looping for the first time, 19572 * connect the ring. 19573 */ 19574 if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) > 19575 sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) { 19576 dmsg->next = sata_debug_rbuf->dmsgh; 19577 sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh; 19578 sata_debug_rbuf->looped = TRUE; 19579 return (sata_debug_rbuf->dmsgp); 19580 } 19581 19582 /* If we've gotten this far then memory allocation is needed */ 19583 dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP); 19584 if (dmsg_alloc == NULL) { 19585 sata_debug_rbuf->allocfailed++; 19586 return (dmsg_alloc); 19587 } else { 19588 sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t); 19589 } 19590 19591 if (sata_debug_rbuf->dmsgp != NULL) { 19592 dmsg->next = dmsg_alloc; 19593 sata_debug_rbuf->dmsgp = dmsg->next; 19594 return (sata_debug_rbuf->dmsgp); 19595 } else { 19596 /* 19597 * We should only be here if we're initializing 19598 * the ring buffer. 19599 */ 19600 if (sata_debug_rbuf->dmsgh == NULL) { 19601 sata_debug_rbuf->dmsgh = dmsg_alloc; 19602 } else { 19603 /* Something is wrong */ 19604 kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t)); 19605 return (NULL); 19606 } 19607 19608 sata_debug_rbuf->dmsgp = dmsg_alloc; 19609 return (sata_debug_rbuf->dmsgp); 19610 } 19611 } 19612 19613 19614 /* 19615 * Free all messages on debug ring buffer. 19616 */ 19617 static void 19618 sata_trace_dmsg_free(void) 19619 { 19620 sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh; 19621 19622 while (dmsg != NULL) { 19623 dmsg_next = dmsg->next; 19624 kmem_free(dmsg, sizeof (sata_trace_dmsg_t)); 19625 19626 /* 19627 * If we've looped around the ring than we're done. 19628 */ 19629 if (dmsg_next == sata_debug_rbuf->dmsgh) { 19630 break; 19631 } else { 19632 dmsg = dmsg_next; 19633 } 19634 } 19635 } 19636 19637 19638 /* 19639 * This function can block 19640 */ 19641 static void 19642 sata_trace_rbuf_alloc(void) 19643 { 19644 sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP); 19645 19646 mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL); 19647 19648 if (dmsg_ring_size > 0) { 19649 sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size; 19650 } 19651 } 19652 19653 19654 static void 19655 sata_trace_rbuf_free(void) 19656 { 19657 sata_trace_dmsg_free(); 19658 mutex_destroy(&sata_debug_rbuf->lock); 19659 kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t)); 19660 } 19661 19662 /* 19663 * If SATA_DEBUG is not defined then this routine is called instead 19664 * of sata_log() via the SATA_LOG_D macro. 19665 */ 19666 static void 19667 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level, 19668 const char *fmt, ...) 19669 { 19670 #ifndef __lock_lint 19671 _NOTE(ARGUNUSED(level)) 19672 #endif 19673 19674 dev_info_t *dip = NULL; 19675 va_list ap; 19676 19677 if (sata_hba_inst != NULL) { 19678 dip = SATA_DIP(sata_hba_inst); 19679 } 19680 19681 va_start(ap, fmt); 19682 sata_vtrace_debug(dip, fmt, ap); 19683 va_end(ap); 19684 } 19685