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.46"}; 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 int 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 * Register a port multiplier to framework. 1860 * 1) Store the GSCR values in the previous allocated pmult_info strctures. 1861 * 2) Search in the blacklist and update the number of the device ports of the 1862 * port multiplier. 1863 * 1864 * Void return. 1865 */ 1866 void 1867 sata_register_pmult(dev_info_t *dip, sata_device_t *sd, sata_pmult_gscr_t *sg) 1868 { 1869 sata_hba_inst_t *sata_hba_inst = NULL; 1870 sata_pmult_info_t *pmultinfo; 1871 sata_pmult_bl_t *blp; 1872 int cport = sd->satadev_addr.cport; 1873 1874 mutex_enter(&sata_mutex); 1875 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 1876 sata_hba_inst = sata_hba_inst->satahba_next) { 1877 if (SATA_DIP(sata_hba_inst) == dip) 1878 if (sata_hba_inst->satahba_attached == 1) 1879 break; 1880 } 1881 mutex_exit(&sata_mutex); 1882 /* HBA not attached? */ 1883 if (sata_hba_inst == NULL) 1884 return; 1885 1886 /* Number of pmports */ 1887 sd->satadev_add_info = sg->gscr2 & SATA_PMULT_PORTNUM_MASK; 1888 1889 /* Check the blacklist */ 1890 for (blp = sata_pmult_blacklist; blp->bl_gscr0; blp++) { 1891 if (sg->gscr0 != blp->bl_gscr0 && blp->bl_gscr0) 1892 continue; 1893 if (sg->gscr1 != blp->bl_gscr1 && blp->bl_gscr1) 1894 continue; 1895 if (sg->gscr2 != blp->bl_gscr2 && blp->bl_gscr2) 1896 continue; 1897 1898 cmn_err(CE_WARN, "!Port multiplier is on the blacklist."); 1899 sd->satadev_add_info = blp->bl_flags; 1900 break; 1901 } 1902 1903 /* Register the port multiplier GSCR */ 1904 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 1905 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 1906 if (pmultinfo != NULL) { 1907 pmultinfo->pmult_gscr = *sg; 1908 pmultinfo->pmult_num_dev_ports = 1909 sd->satadev_add_info & SATA_PMULT_PORTNUM_MASK; 1910 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 1911 "Port multiplier registered at port %d", cport); 1912 } 1913 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 1914 } 1915 1916 /* 1917 * sata_name_child is for composing the name of the node 1918 * the format of the name is "target,0". 1919 */ 1920 static int 1921 sata_name_child(dev_info_t *dip, char *name, int namelen) 1922 { 1923 int target; 1924 1925 target = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 1926 DDI_PROP_DONTPASS, "target", -1); 1927 if (target == -1) 1928 return (DDI_FAILURE); 1929 (void) snprintf(name, namelen, "%x,0", target); 1930 return (DDI_SUCCESS); 1931 } 1932 1933 1934 1935 /* ****************** SCSA required entry points *********************** */ 1936 1937 /* 1938 * Implementation of scsi tran_tgt_init. 1939 * sata_scsi_tgt_init() initializes scsi_device structure 1940 * 1941 * If successful, DDI_SUCCESS is returned. 1942 * DDI_FAILURE is returned if addressed device does not exist 1943 */ 1944 1945 static int 1946 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip, 1947 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 1948 { 1949 #ifndef __lock_lint 1950 _NOTE(ARGUNUSED(hba_dip)) 1951 _NOTE(ARGUNUSED(tgt_dip)) 1952 #endif 1953 sata_device_t sata_device; 1954 sata_drive_info_t *sdinfo; 1955 struct sata_id *sid; 1956 sata_hba_inst_t *sata_hba_inst; 1957 char model[SATA_ID_MODEL_LEN + 1]; 1958 char fw[SATA_ID_FW_LEN + 1]; 1959 char *vid, *pid; 1960 int i; 1961 1962 /* 1963 * Fail tran_tgt_init for .conf stub node 1964 */ 1965 if (ndi_dev_is_persistent_node(tgt_dip) == 0) { 1966 (void) ndi_merge_node(tgt_dip, sata_name_child); 1967 ddi_set_name_addr(tgt_dip, NULL); 1968 return (DDI_FAILURE); 1969 } 1970 1971 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 1972 1973 /* Validate scsi device address */ 1974 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 1975 &sata_device) != 0) 1976 return (DDI_FAILURE); 1977 1978 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 1979 sata_device.satadev_addr.cport))); 1980 1981 /* sata_device now contains a valid sata address */ 1982 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 1983 if (sdinfo == NULL) { 1984 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1985 sata_device.satadev_addr.cport))); 1986 return (DDI_FAILURE); 1987 } 1988 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1989 sata_device.satadev_addr.cport))); 1990 1991 /* 1992 * Check if we need to create a legacy devid (i.e cmdk style) for 1993 * the target disks. 1994 * 1995 * HBA devinfo node will have the property "use-cmdk-devid-format" 1996 * if we need to create cmdk-style devid for all the disk devices 1997 * attached to this controller. This property may have been set 1998 * from HBA driver's .conf file or by the HBA driver in its 1999 * attach(9F) function. 2000 */ 2001 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 2002 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS, 2003 "use-cmdk-devid-format", 0) == 1)) { 2004 /* register a legacy devid for this target node */ 2005 sata_target_devid_register(tgt_dip, sdinfo); 2006 } 2007 2008 2009 /* 2010 * 'Identify Device Data' does not always fit in standard SCSI 2011 * INQUIRY data, so establish INQUIRY_* properties with full-form 2012 * of information. 2013 */ 2014 sid = &sdinfo->satadrv_id; 2015 #ifdef _LITTLE_ENDIAN 2016 swab(sid->ai_model, model, SATA_ID_MODEL_LEN); 2017 swab(sid->ai_fw, fw, SATA_ID_FW_LEN); 2018 #else /* _LITTLE_ENDIAN */ 2019 bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN); 2020 bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN); 2021 #endif /* _LITTLE_ENDIAN */ 2022 model[SATA_ID_MODEL_LEN] = 0; 2023 fw[SATA_ID_FW_LEN] = 0; 2024 2025 /* split model into into vid/pid */ 2026 for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++) 2027 if ((*pid == ' ') || (*pid == '\t')) 2028 break; 2029 if (i < SATA_ID_MODEL_LEN) { 2030 vid = model; 2031 *pid++ = 0; /* terminate vid, establish pid */ 2032 } else { 2033 vid = NULL; /* vid will stay "ATA " */ 2034 pid = model; /* model is all pid */ 2035 } 2036 2037 if (vid) 2038 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_VENDOR_ID, 2039 vid, strlen(vid)); 2040 if (pid) 2041 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID, 2042 pid, strlen(pid)); 2043 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_REVISION_ID, 2044 fw, strlen(fw)); 2045 2046 return (DDI_SUCCESS); 2047 } 2048 2049 /* 2050 * Implementation of scsi tran_tgt_probe. 2051 * Probe target, by calling default scsi routine scsi_hba_probe() 2052 */ 2053 static int 2054 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void)) 2055 { 2056 sata_hba_inst_t *sata_hba_inst = 2057 (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private); 2058 int rval; 2059 uint32_t pm_cap; 2060 2061 rval = scsi_hba_probe(sd, callback); 2062 pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE | 2063 SATA_CAP_LOG_SENSE; 2064 2065 if (rval == SCSIPROBE_EXISTS) { 2066 /* 2067 * Set property "pm-capable" on the target device node, so that 2068 * the target driver will not try to fetch scsi cycle counters 2069 * before enabling device power-management. 2070 */ 2071 if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev, 2072 "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) { 2073 sata_log(sata_hba_inst, CE_WARN, 2074 "SATA device at port %d: " 2075 "will not be power-managed ", 2076 SCSI_TO_SATA_CPORT(sd->sd_address.a_target)); 2077 SATA_LOG_D((sata_hba_inst, CE_WARN, 2078 "failure updating pm-capable property")); 2079 } 2080 } 2081 return (rval); 2082 } 2083 2084 /* 2085 * Implementation of scsi tran_tgt_free. 2086 * Release all resources allocated for scsi_device 2087 */ 2088 static void 2089 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip, 2090 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 2091 { 2092 #ifndef __lock_lint 2093 _NOTE(ARGUNUSED(hba_dip)) 2094 #endif 2095 sata_device_t sata_device; 2096 sata_drive_info_t *sdinfo; 2097 sata_hba_inst_t *sata_hba_inst; 2098 ddi_devid_t devid; 2099 2100 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 2101 2102 /* Validate scsi device address */ 2103 /* 2104 * Note: tgt_free relates to the SCSA view of a device. If called, there 2105 * was a device at this address, so even if the sata framework internal 2106 * resources were alredy released because a device was detached, 2107 * this function should be executed as long as its actions do 2108 * not require the internal sata view of a device and the address 2109 * refers to a valid sata address. 2110 * Validating the address here means that we do not trust SCSA... 2111 */ 2112 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 2113 &sata_device) == -1) 2114 return; 2115 2116 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2117 sata_device.satadev_addr.cport))); 2118 2119 /* sata_device now should contain a valid sata address */ 2120 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 2121 if (sdinfo == NULL) { 2122 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2123 sata_device.satadev_addr.cport))); 2124 return; 2125 } 2126 /* 2127 * We did not allocate any resources in sata_scsi_tgt_init() 2128 * other than few properties. 2129 * Free them. 2130 */ 2131 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2132 sata_device.satadev_addr.cport))); 2133 (void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable"); 2134 2135 /* 2136 * If devid was previously created but not freed up from 2137 * sd(7D) driver (i.e during detach(9F)) then do it here. 2138 */ 2139 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 2140 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS, 2141 "use-cmdk-devid-format", 0) == 1) && 2142 (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) { 2143 ddi_devid_unregister(tgt_dip); 2144 ddi_devid_free(devid); 2145 } 2146 } 2147 2148 /* 2149 * Implementation of scsi tran_init_pkt 2150 * Upon successful return, scsi pkt buffer has DMA resources allocated. 2151 * 2152 * It seems that we should always allocate pkt, even if the address is 2153 * for non-existing device - just use some default for dma_attr. 2154 * The reason is that there is no way to communicate this to a caller here. 2155 * Subsequent call to sata_scsi_start may fail appropriately. 2156 * Simply returning NULL does not seem to discourage a target driver... 2157 * 2158 * Returns a pointer to initialized scsi_pkt, or NULL otherwise. 2159 */ 2160 static struct scsi_pkt * 2161 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt, 2162 struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags, 2163 int (*callback)(caddr_t), caddr_t arg) 2164 { 2165 sata_hba_inst_t *sata_hba_inst = 2166 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2167 dev_info_t *dip = SATA_DIP(sata_hba_inst); 2168 sata_device_t sata_device; 2169 sata_drive_info_t *sdinfo; 2170 sata_pkt_txlate_t *spx; 2171 ddi_dma_attr_t cur_dma_attr; 2172 int rval; 2173 boolean_t new_pkt = TRUE; 2174 2175 ASSERT(ap->a_hba_tran->tran_hba_dip == dip); 2176 2177 /* 2178 * We need to translate the address, even if it could be 2179 * a bogus one, for a non-existing device 2180 */ 2181 sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 2182 sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target); 2183 sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 2184 sata_device.satadev_rev = SATA_DEVICE_REV; 2185 2186 if (pkt == NULL) { 2187 /* 2188 * Have to allocate a brand new scsi packet. 2189 * We need to operate with auto request sense enabled. 2190 */ 2191 pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen, 2192 MAX(statuslen, sizeof (struct scsi_arq_status)), 2193 tgtlen, sizeof (sata_pkt_txlate_t), callback, arg); 2194 2195 if (pkt == NULL) 2196 return (NULL); 2197 2198 /* Fill scsi packet structure */ 2199 pkt->pkt_comp = (void (*)())NULL; 2200 pkt->pkt_time = 0; 2201 pkt->pkt_resid = 0; 2202 pkt->pkt_statistics = 0; 2203 pkt->pkt_reason = 0; 2204 2205 /* 2206 * pkt_hba_private will point to sata pkt txlate structure 2207 */ 2208 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2209 bzero(spx, sizeof (sata_pkt_txlate_t)); 2210 2211 spx->txlt_scsi_pkt = pkt; 2212 spx->txlt_sata_hba_inst = sata_hba_inst; 2213 2214 /* Allocate sata_pkt */ 2215 spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback); 2216 if (spx->txlt_sata_pkt == NULL) { 2217 /* Could not allocate sata pkt */ 2218 scsi_hba_pkt_free(ap, pkt); 2219 return (NULL); 2220 } 2221 /* Set sata address */ 2222 spx->txlt_sata_pkt->satapkt_device.satadev_addr = 2223 sata_device.satadev_addr; 2224 spx->txlt_sata_pkt->satapkt_device.satadev_rev = 2225 sata_device.satadev_rev; 2226 2227 if ((bp == NULL) || (bp->b_bcount == 0)) 2228 return (pkt); 2229 2230 spx->txlt_total_residue = bp->b_bcount; 2231 } else { 2232 new_pkt = FALSE; 2233 /* 2234 * Packet was preallocated/initialized by previous call 2235 */ 2236 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2237 2238 if ((bp == NULL) || (bp->b_bcount == 0)) { 2239 return (pkt); 2240 } 2241 2242 /* Pkt is available already: spx->txlt_scsi_pkt == pkt; */ 2243 } 2244 2245 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 2246 2247 /* 2248 * We use an adjusted version of the dma_attr, to account 2249 * for device addressing limitations. 2250 * sata_adjust_dma_attr() will handle sdinfo == NULL which may 2251 * happen when a device is not yet configured. 2252 */ 2253 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2254 sata_device.satadev_addr.cport))); 2255 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 2256 &spx->txlt_sata_pkt->satapkt_device); 2257 /* NULL sdinfo may be passsed to sata_adjust_dma_attr() */ 2258 sata_adjust_dma_attr(sdinfo, 2259 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 2260 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2261 sata_device.satadev_addr.cport))); 2262 /* 2263 * Allocate necessary DMA resources for the packet's data buffer 2264 * NOTE: 2265 * In case of read/write commands, DMA resource allocation here is 2266 * based on the premise that the transfer length specified in 2267 * the read/write scsi cdb will match exactly DMA resources - 2268 * returning correct packet residue is crucial. 2269 */ 2270 if ((rval = sata_dma_buf_setup(spx, flags, callback, arg, 2271 &cur_dma_attr)) != DDI_SUCCESS) { 2272 /* 2273 * If a DMA allocation request fails with 2274 * DDI_DMA_NOMAPPING, indicate the error by calling 2275 * bioerror(9F) with bp and an error code of EFAULT. 2276 * If a DMA allocation request fails with 2277 * DDI_DMA_TOOBIG, indicate the error by calling 2278 * bioerror(9F) with bp and an error code of EINVAL. 2279 * For DDI_DMA_NORESOURCES, we may have some of them allocated. 2280 * Request may be repeated later - there is no real error. 2281 */ 2282 switch (rval) { 2283 case DDI_DMA_NORESOURCES: 2284 bioerror(bp, 0); 2285 break; 2286 case DDI_DMA_NOMAPPING: 2287 case DDI_DMA_BADATTR: 2288 bioerror(bp, EFAULT); 2289 break; 2290 case DDI_DMA_TOOBIG: 2291 default: 2292 bioerror(bp, EINVAL); 2293 break; 2294 } 2295 if (new_pkt == TRUE) { 2296 /* 2297 * Since this is a new packet, we can clean-up 2298 * everything 2299 */ 2300 sata_scsi_destroy_pkt(ap, pkt); 2301 } else { 2302 /* 2303 * This is a re-used packet. It will be target driver's 2304 * responsibility to eventually destroy it (which 2305 * will free allocated resources). 2306 * Here, we just "complete" the request, leaving 2307 * allocated resources intact, so the request may 2308 * be retried. 2309 */ 2310 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 2311 sata_pkt_free(spx); 2312 } 2313 return (NULL); 2314 } 2315 /* Set number of bytes that are not yet accounted for */ 2316 pkt->pkt_resid = spx->txlt_total_residue; 2317 ASSERT(pkt->pkt_resid >= 0); 2318 2319 return (pkt); 2320 } 2321 2322 /* 2323 * Implementation of scsi tran_start. 2324 * Translate scsi cmd into sata operation and return status. 2325 * ATAPI CDBs are passed to ATAPI devices - the device determines what commands 2326 * are supported. 2327 * For SATA hard disks, supported scsi commands: 2328 * SCMD_INQUIRY 2329 * SCMD_TEST_UNIT_READY 2330 * SCMD_START_STOP 2331 * SCMD_READ_CAPACITY 2332 * SCMD_REQUEST_SENSE 2333 * SCMD_LOG_SENSE_G1 2334 * SCMD_LOG_SELECT_G1 2335 * SCMD_MODE_SENSE (specific pages) 2336 * SCMD_MODE_SENSE_G1 (specific pages) 2337 * SCMD_MODE_SELECT (specific pages) 2338 * SCMD_MODE_SELECT_G1 (specific pages) 2339 * SCMD_SYNCHRONIZE_CACHE 2340 * SCMD_SYNCHRONIZE_CACHE_G1 2341 * SCMD_READ 2342 * SCMD_READ_G1 2343 * SCMD_READ_G4 2344 * SCMD_READ_G5 2345 * SCMD_WRITE 2346 * SCMD_WRITE_BUFFER 2347 * SCMD_WRITE_G1 2348 * SCMD_WRITE_G4 2349 * SCMD_WRITE_G5 2350 * SCMD_SEEK (noop) 2351 * SCMD_SDIAG 2352 * 2353 * All other commands are rejected as unsupported. 2354 * 2355 * Returns: 2356 * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver 2357 * for execution. TRAN_ACCEPT may be returned also if device was removed but 2358 * a callback could be scheduled. 2359 * TRAN_BADPKT if cmd was directed to invalid address. 2360 * TRAN_FATAL_ERROR is command was rejected due to hardware error, including 2361 * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device 2362 * was removed and there was no callback specified in scsi pkt. 2363 * TRAN_BUSY if command could not be executed becasue HBA driver or SATA 2364 * framework was busy performing some other operation(s). 2365 * 2366 */ 2367 static int 2368 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt) 2369 { 2370 sata_hba_inst_t *sata_hba_inst = 2371 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2372 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2373 sata_device_t *sdevice = &spx->txlt_sata_pkt->satapkt_device; 2374 sata_drive_info_t *sdinfo; 2375 struct buf *bp; 2376 uint8_t cport, pmport; 2377 boolean_t dev_gone = B_FALSE; 2378 int rval; 2379 2380 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2381 "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]); 2382 2383 ASSERT(spx != NULL && 2384 spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL); 2385 2386 cport = SCSI_TO_SATA_CPORT(ap->a_target); 2387 pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 2388 2389 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2390 2391 if (sdevice->satadev_addr.qual == SATA_ADDR_DCPORT) { 2392 sdinfo = sata_get_device_info(sata_hba_inst, sdevice); 2393 if (sdinfo == NULL || 2394 SATA_CPORT_INFO(sata_hba_inst, cport)-> 2395 cport_tgtnode_clean == B_FALSE || 2396 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 2397 dev_gone = B_TRUE; 2398 } 2399 } else if (sdevice->satadev_addr.qual == SATA_ADDR_DPMPORT) { 2400 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 2401 SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst, 2402 cport) == NULL) { 2403 dev_gone = B_TRUE; 2404 } else if (SATA_PMPORT_INFO(sata_hba_inst, cport, 2405 pmport) == NULL) { 2406 dev_gone = B_TRUE; 2407 } else { 2408 mutex_enter(&(SATA_PMPORT_MUTEX(sata_hba_inst, 2409 cport, pmport))); 2410 sdinfo = sata_get_device_info(sata_hba_inst, sdevice); 2411 if (sdinfo == NULL || 2412 SATA_PMPORT_INFO(sata_hba_inst, cport, pmport)-> 2413 pmport_tgtnode_clean == B_FALSE || 2414 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 2415 dev_gone = B_TRUE; 2416 } 2417 mutex_exit(&(SATA_PMPORT_MUTEX(sata_hba_inst, 2418 cport, pmport))); 2419 } 2420 } 2421 2422 if (dev_gone == B_TRUE) { 2423 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2424 pkt->pkt_reason = CMD_DEV_GONE; 2425 /* 2426 * The sd target driver is checking CMD_DEV_GONE pkt_reason 2427 * only in callback function (for normal requests) and 2428 * in the dump code path. 2429 * So, if the callback is available, we need to do 2430 * the callback rather than returning TRAN_FATAL_ERROR here. 2431 */ 2432 if (pkt->pkt_comp != NULL) { 2433 /* scsi callback required */ 2434 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 2435 (task_func_t *)pkt->pkt_comp, 2436 (void *)pkt, TQ_SLEEP) == NULL) 2437 /* Scheduling the callback failed */ 2438 return (TRAN_BUSY); 2439 return (TRAN_ACCEPT); 2440 } 2441 /* No callback available */ 2442 return (TRAN_FATAL_ERROR); 2443 } 2444 2445 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 2446 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2447 rval = sata_txlt_atapi(spx); 2448 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2449 "sata_scsi_start atapi: rval %d\n", rval); 2450 return (rval); 2451 } 2452 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2453 2454 /* 2455 * Checking for power state, if it was on 2456 * STOPPED state, then the drive is not capable 2457 * of processing media access command. And 2458 * TEST_UNIT_READY, REQUEST_SENSE has special handling 2459 * in the function for different power state. 2460 */ 2461 if (((sdinfo->satadrv_power_level == SATA_POWER_STANDBY) || 2462 (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)) && 2463 (SATA_IS_MEDIUM_ACCESS_CMD(pkt->pkt_cdbp[0]))) { 2464 return (sata_txlt_check_condition(spx, KEY_NOT_READY, 2465 SD_SCSI_ASC_LU_NOT_READY)); 2466 } 2467 2468 /* ATA Disk commands processing starts here */ 2469 2470 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 2471 2472 switch (pkt->pkt_cdbp[0]) { 2473 2474 case SCMD_INQUIRY: 2475 /* Mapped to identify device */ 2476 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2477 bp_mapin(bp); 2478 rval = sata_txlt_inquiry(spx); 2479 break; 2480 2481 case SCMD_TEST_UNIT_READY: 2482 /* 2483 * SAT "SATA to ATA Translation" doc specifies translation 2484 * to ATA CHECK POWER MODE. 2485 */ 2486 rval = sata_txlt_test_unit_ready(spx); 2487 break; 2488 2489 case SCMD_START_STOP: 2490 /* Mapping depends on the command */ 2491 rval = sata_txlt_start_stop_unit(spx); 2492 break; 2493 2494 case SCMD_READ_CAPACITY: 2495 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2496 bp_mapin(bp); 2497 rval = sata_txlt_read_capacity(spx); 2498 break; 2499 2500 case SCMD_REQUEST_SENSE: 2501 /* 2502 * Always No Sense, since we force ARQ 2503 */ 2504 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2505 bp_mapin(bp); 2506 rval = sata_txlt_request_sense(spx); 2507 break; 2508 2509 case SCMD_LOG_SENSE_G1: 2510 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2511 bp_mapin(bp); 2512 rval = sata_txlt_log_sense(spx); 2513 break; 2514 2515 case SCMD_LOG_SELECT_G1: 2516 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2517 bp_mapin(bp); 2518 rval = sata_txlt_log_select(spx); 2519 break; 2520 2521 case SCMD_MODE_SENSE: 2522 case SCMD_MODE_SENSE_G1: 2523 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2524 bp_mapin(bp); 2525 rval = sata_txlt_mode_sense(spx); 2526 break; 2527 2528 2529 case SCMD_MODE_SELECT: 2530 case SCMD_MODE_SELECT_G1: 2531 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2532 bp_mapin(bp); 2533 rval = sata_txlt_mode_select(spx); 2534 break; 2535 2536 case SCMD_SYNCHRONIZE_CACHE: 2537 case SCMD_SYNCHRONIZE_CACHE_G1: 2538 rval = sata_txlt_synchronize_cache(spx); 2539 break; 2540 2541 case SCMD_READ: 2542 case SCMD_READ_G1: 2543 case SCMD_READ_G4: 2544 case SCMD_READ_G5: 2545 rval = sata_txlt_read(spx); 2546 break; 2547 case SCMD_WRITE_BUFFER: 2548 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2549 bp_mapin(bp); 2550 rval = sata_txlt_write_buffer(spx); 2551 break; 2552 2553 case SCMD_WRITE: 2554 case SCMD_WRITE_G1: 2555 case SCMD_WRITE_G4: 2556 case SCMD_WRITE_G5: 2557 rval = sata_txlt_write(spx); 2558 break; 2559 2560 case SCMD_SEEK: 2561 rval = sata_txlt_nodata_cmd_immediate(spx); 2562 break; 2563 2564 /* Other cases will be filed later */ 2565 /* postponed until phase 2 of the development */ 2566 default: 2567 rval = sata_txlt_invalid_command(spx); 2568 break; 2569 } 2570 2571 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2572 "sata_scsi_start: rval %d\n", rval); 2573 2574 return (rval); 2575 } 2576 2577 /* 2578 * Implementation of scsi tran_abort. 2579 * Abort specific pkt or all packets. 2580 * 2581 * Returns 1 if one or more packets were aborted, returns 0 otherwise 2582 * 2583 * May be called from an interrupt level. 2584 */ 2585 static int 2586 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt) 2587 { 2588 sata_hba_inst_t *sata_hba_inst = 2589 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2590 sata_device_t sata_device; 2591 sata_pkt_t *sata_pkt; 2592 2593 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2594 "sata_scsi_abort: %s at target: 0x%x\n", 2595 scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target); 2596 2597 /* Validate address */ 2598 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) 2599 /* Invalid address */ 2600 return (0); 2601 2602 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2603 sata_device.satadev_addr.cport))); 2604 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) { 2605 /* invalid address */ 2606 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2607 sata_device.satadev_addr.cport))); 2608 return (0); 2609 } 2610 if (scsi_pkt == NULL) { 2611 /* 2612 * Abort all packets. 2613 * Although we do not have specific packet, we still need 2614 * dummy packet structure to pass device address to HBA. 2615 * Allocate one, without sleeping. Fail if pkt cannot be 2616 * allocated. 2617 */ 2618 sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP); 2619 if (sata_pkt == NULL) { 2620 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2621 sata_device.satadev_addr.cport))); 2622 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: " 2623 "could not allocate sata_pkt")); 2624 return (0); 2625 } 2626 sata_pkt->satapkt_rev = SATA_PKT_REV; 2627 sata_pkt->satapkt_device = sata_device; 2628 sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 2629 } else { 2630 if (scsi_pkt->pkt_ha_private == NULL) { 2631 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2632 sata_device.satadev_addr.cport))); 2633 return (0); /* Bad scsi pkt */ 2634 } 2635 /* extract pointer to sata pkt */ 2636 sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)-> 2637 txlt_sata_pkt; 2638 } 2639 2640 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2641 sata_device.satadev_addr.cport))); 2642 /* Send abort request to HBA */ 2643 if ((*SATA_ABORT_FUNC(sata_hba_inst)) 2644 (SATA_DIP(sata_hba_inst), sata_pkt, 2645 scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) == 2646 SATA_SUCCESS) { 2647 if (scsi_pkt == NULL) 2648 kmem_free(sata_pkt, sizeof (sata_pkt_t)); 2649 /* Success */ 2650 return (1); 2651 } 2652 /* Else, something did not go right */ 2653 if (scsi_pkt == NULL) 2654 kmem_free(sata_pkt, sizeof (sata_pkt_t)); 2655 /* Failure */ 2656 return (0); 2657 } 2658 2659 2660 /* 2661 * Implementation of scsi tran_reset. 2662 * RESET_ALL request is translated into port reset. 2663 * RESET_TARGET requests is translated into a device reset, 2664 * RESET_LUN request is accepted only for LUN 0 and translated into 2665 * device reset. 2666 * The target reset should cause all HBA active and queued packets to 2667 * be terminated and returned with pkt reason SATA_PKT_RESET prior to 2668 * the return. HBA should report reset event for the device. 2669 * 2670 * Returns 1 upon success, 0 upon failure. 2671 */ 2672 static int 2673 sata_scsi_reset(struct scsi_address *ap, int level) 2674 { 2675 sata_hba_inst_t *sata_hba_inst = 2676 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2677 sata_device_t sata_device; 2678 int val; 2679 2680 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2681 "sata_scsi_reset: level %d target: 0x%x\n", 2682 level, ap->a_target); 2683 2684 /* Validate address */ 2685 val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device); 2686 if (val == -1) 2687 /* Invalid address */ 2688 return (0); 2689 2690 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2691 sata_device.satadev_addr.cport))); 2692 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) { 2693 /* invalid address */ 2694 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2695 sata_device.satadev_addr.cport))); 2696 return (0); 2697 } 2698 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2699 sata_device.satadev_addr.cport))); 2700 if (level == RESET_ALL) { 2701 /* port reset */ 2702 if (sata_device.satadev_addr.qual == SATA_ADDR_DCPORT) 2703 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 2704 else 2705 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT; 2706 2707 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 2708 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS) 2709 return (1); 2710 else 2711 return (0); 2712 2713 } else if (val == 0 && 2714 (level == RESET_TARGET || level == RESET_LUN)) { 2715 /* reset device (device attached) */ 2716 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 2717 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS) 2718 return (1); 2719 else 2720 return (0); 2721 } 2722 return (0); 2723 } 2724 2725 2726 /* 2727 * Implementation of scsi tran_getcap (get transport/device capabilities). 2728 * Supported capabilities for SATA hard disks: 2729 * auto-rqsense (always supported) 2730 * tagged-qing (supported if HBA supports it) 2731 * untagged-qing (could be supported if disk supports it, but because 2732 * caching behavior allowing untagged queuing actually 2733 * results in reduced performance. sd tries to throttle 2734 * back to only 3 outstanding commands, which may 2735 * work for real SCSI disks, but with read ahead 2736 * caching, having more than 1 outstanding command 2737 * results in cache thrashing.) 2738 * sector_size 2739 * dma_max 2740 * interconnect-type (INTERCONNECT_SATA) 2741 * 2742 * Supported capabilities for ATAPI CD/DVD devices: 2743 * auto-rqsense (always supported) 2744 * sector_size 2745 * dma_max 2746 * max-cdb-length 2747 * interconnect-type (INTERCONNECT_SATA) 2748 * 2749 * Supported capabilities for ATAPI TAPE devices: 2750 * auto-rqsense (always supported) 2751 * dma_max 2752 * max-cdb-length 2753 * 2754 * Supported capabilities for SATA ATAPI hard disks: 2755 * auto-rqsense (always supported) 2756 * interconnect-type (INTERCONNECT_SATA) 2757 * max-cdb-length 2758 * 2759 * Request for other capabilities is rejected as unsupported. 2760 * 2761 * Returns supported capability value, or -1 if capability is unsuppported or 2762 * the address is invalid - no device. 2763 */ 2764 2765 static int 2766 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom) 2767 { 2768 2769 sata_hba_inst_t *sata_hba_inst = 2770 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2771 sata_device_t sata_device; 2772 sata_drive_info_t *sdinfo; 2773 ddi_dma_attr_t adj_dma_attr; 2774 int rval; 2775 2776 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2777 "sata_scsi_getcap: target: 0x%x, cap: %s\n", 2778 ap->a_target, cap); 2779 2780 /* 2781 * We want to process the capabilities on per port granularity. 2782 * So, we are specifically restricting ourselves to whom != 0 2783 * to exclude the controller wide handling. 2784 */ 2785 if (cap == NULL || whom == 0) 2786 return (-1); 2787 2788 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) { 2789 /* Invalid address */ 2790 return (-1); 2791 } 2792 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2793 sata_device.satadev_addr.cport))); 2794 if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) == 2795 NULL) { 2796 /* invalid address */ 2797 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2798 sata_device.satadev_addr.cport))); 2799 return (-1); 2800 } 2801 2802 switch (scsi_hba_lookup_capstr(cap)) { 2803 case SCSI_CAP_ARQ: 2804 rval = 1; /* ARQ supported, turned on */ 2805 break; 2806 2807 case SCSI_CAP_SECTOR_SIZE: 2808 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) 2809 rval = SATA_DISK_SECTOR_SIZE; /* fixed size */ 2810 else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) 2811 rval = SATA_ATAPI_SECTOR_SIZE; 2812 else rval = -1; 2813 break; 2814 2815 /* 2816 * untagged queuing cause a performance inversion because of 2817 * the way sd operates. Because of this reason we do not 2818 * use it when available. 2819 */ 2820 case SCSI_CAP_UNTAGGED_QING: 2821 if (sdinfo->satadrv_features_enabled & 2822 SATA_DEV_F_E_UNTAGGED_QING) 2823 rval = 1; /* Untagged queuing available */ 2824 else 2825 rval = -1; /* Untagged queuing not available */ 2826 break; 2827 2828 case SCSI_CAP_TAGGED_QING: 2829 if ((sdinfo->satadrv_features_enabled & 2830 SATA_DEV_F_E_TAGGED_QING) && 2831 (sdinfo->satadrv_max_queue_depth > 1)) 2832 rval = 1; /* Tagged queuing available */ 2833 else 2834 rval = -1; /* Tagged queuing not available */ 2835 break; 2836 2837 case SCSI_CAP_DMA_MAX: 2838 sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst), 2839 &adj_dma_attr); 2840 rval = (int)adj_dma_attr.dma_attr_maxxfer; 2841 /* We rely on the fact that dma_attr_maxxfer < 0x80000000 */ 2842 break; 2843 2844 case SCSI_CAP_INTERCONNECT_TYPE: 2845 rval = INTERCONNECT_SATA; /* SATA interconnect type */ 2846 break; 2847 2848 case SCSI_CAP_CDB_LEN: 2849 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) 2850 rval = sdinfo->satadrv_atapi_cdb_len; 2851 else 2852 rval = -1; 2853 break; 2854 2855 default: 2856 rval = -1; 2857 break; 2858 } 2859 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2860 sata_device.satadev_addr.cport))); 2861 return (rval); 2862 } 2863 2864 /* 2865 * Implementation of scsi tran_setcap 2866 * 2867 * Only SCSI_CAP_UNTAGGED_QING and SCSI_CAP_TAGGED_QING are changeable. 2868 * 2869 */ 2870 static int 2871 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom) 2872 { 2873 sata_hba_inst_t *sata_hba_inst = 2874 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2875 sata_device_t sata_device; 2876 sata_drive_info_t *sdinfo; 2877 int rval; 2878 2879 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2880 "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap); 2881 2882 /* 2883 * We want to process the capabilities on per port granularity. 2884 * So, we are specifically restricting ourselves to whom != 0 2885 * to exclude the controller wide handling. 2886 */ 2887 if (cap == NULL || whom == 0) { 2888 return (-1); 2889 } 2890 2891 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) { 2892 /* Invalid address */ 2893 return (-1); 2894 } 2895 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2896 sata_device.satadev_addr.cport))); 2897 if ((sdinfo = sata_get_device_info(sata_hba_inst, 2898 &sata_device)) == NULL) { 2899 /* invalid address */ 2900 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2901 sata_device.satadev_addr.cport))); 2902 return (-1); 2903 } 2904 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2905 sata_device.satadev_addr.cport))); 2906 2907 switch (scsi_hba_lookup_capstr(cap)) { 2908 case SCSI_CAP_ARQ: 2909 case SCSI_CAP_SECTOR_SIZE: 2910 case SCSI_CAP_DMA_MAX: 2911 case SCSI_CAP_INTERCONNECT_TYPE: 2912 rval = 0; 2913 break; 2914 case SCSI_CAP_UNTAGGED_QING: 2915 if (SATA_QDEPTH(sata_hba_inst) > 1) { 2916 rval = 1; 2917 if (value == 1) { 2918 sdinfo->satadrv_features_enabled |= 2919 SATA_DEV_F_E_UNTAGGED_QING; 2920 } else if (value == 0) { 2921 sdinfo->satadrv_features_enabled &= 2922 ~SATA_DEV_F_E_UNTAGGED_QING; 2923 } else { 2924 rval = -1; 2925 } 2926 } else { 2927 rval = 0; 2928 } 2929 break; 2930 case SCSI_CAP_TAGGED_QING: 2931 /* This can TCQ or NCQ */ 2932 if (sata_func_enable & SATA_ENABLE_QUEUING && 2933 ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ && 2934 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) || 2935 (sata_func_enable & SATA_ENABLE_NCQ && 2936 sdinfo->satadrv_features_support & SATA_DEV_F_NCQ && 2937 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) && 2938 (sdinfo->satadrv_max_queue_depth > 1)) { 2939 rval = 1; 2940 if (value == 1) { 2941 sdinfo->satadrv_features_enabled |= 2942 SATA_DEV_F_E_TAGGED_QING; 2943 } else if (value == 0) { 2944 sdinfo->satadrv_features_enabled &= 2945 ~SATA_DEV_F_E_TAGGED_QING; 2946 } else { 2947 rval = -1; 2948 } 2949 } else { 2950 rval = 0; 2951 } 2952 break; 2953 default: 2954 rval = -1; 2955 break; 2956 } 2957 return (rval); 2958 } 2959 2960 /* 2961 * Implementations of scsi tran_destroy_pkt. 2962 * Free resources allocated by sata_scsi_init_pkt() 2963 */ 2964 static void 2965 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 2966 { 2967 sata_pkt_txlate_t *spx; 2968 2969 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2970 2971 sata_common_free_dma_rsrcs(spx); 2972 2973 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 2974 sata_pkt_free(spx); 2975 2976 scsi_hba_pkt_free(ap, pkt); 2977 } 2978 2979 /* 2980 * Implementation of scsi tran_dmafree. 2981 * Free DMA resources allocated by sata_scsi_init_pkt() 2982 */ 2983 2984 static void 2985 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt) 2986 { 2987 #ifndef __lock_lint 2988 _NOTE(ARGUNUSED(ap)) 2989 #endif 2990 sata_pkt_txlate_t *spx; 2991 2992 ASSERT(pkt != NULL); 2993 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2994 2995 sata_common_free_dma_rsrcs(spx); 2996 } 2997 2998 /* 2999 * Implementation of scsi tran_sync_pkt. 3000 * 3001 * The assumption below is that pkt is unique - there is no need to check ap 3002 * 3003 * Synchronize DMA buffer and, if the intermediate buffer is used, copy data 3004 * into/from the real buffer. 3005 */ 3006 static void 3007 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 3008 { 3009 #ifndef __lock_lint 3010 _NOTE(ARGUNUSED(ap)) 3011 #endif 3012 int rval; 3013 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 3014 struct buf *bp; 3015 int direction; 3016 3017 ASSERT(spx != NULL); 3018 if (spx->txlt_buf_dma_handle != NULL) { 3019 direction = spx->txlt_sata_pkt-> 3020 satapkt_cmd.satacmd_flags.sata_data_direction; 3021 if (spx->txlt_sata_pkt != NULL && 3022 direction != SATA_DIR_NODATA_XFER) { 3023 if (spx->txlt_tmp_buf != NULL) { 3024 /* Intermediate DMA buffer used */ 3025 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3026 3027 if (direction & SATA_DIR_WRITE) { 3028 bcopy(bp->b_un.b_addr, 3029 spx->txlt_tmp_buf, bp->b_bcount); 3030 } 3031 } 3032 /* Sync the buffer for device or for CPU */ 3033 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 3034 (direction & SATA_DIR_WRITE) ? 3035 DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU); 3036 ASSERT(rval == DDI_SUCCESS); 3037 if (spx->txlt_tmp_buf != NULL && 3038 !(direction & SATA_DIR_WRITE)) { 3039 /* Intermediate DMA buffer used for read */ 3040 bcopy(spx->txlt_tmp_buf, 3041 bp->b_un.b_addr, bp->b_bcount); 3042 } 3043 3044 } 3045 } 3046 } 3047 3048 3049 3050 /* ******************* SATA - SCSI Translation functions **************** */ 3051 /* 3052 * SCSI to SATA pkt and command translation and SATA to SCSI status/error 3053 * translation. 3054 */ 3055 3056 /* 3057 * Checks if a device exists and can be access and translates common 3058 * scsi_pkt data to sata_pkt data. 3059 * 3060 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and 3061 * sata_pkt was set-up. 3062 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not 3063 * exist and pkt_comp callback was scheduled. 3064 * Returns other TRAN_XXXXX values when error occured and command should be 3065 * rejected with the returned TRAN_XXXXX value. 3066 * 3067 * This function should be called with port mutex held. 3068 */ 3069 static int 3070 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason) 3071 { 3072 sata_drive_info_t *sdinfo; 3073 sata_device_t sata_device; 3074 const struct sata_cmd_flags sata_initial_cmd_flags = { 3075 SATA_DIR_NODATA_XFER, 3076 /* all other values to 0/FALSE */ 3077 }; 3078 /* 3079 * Pkt_reason has to be set if the pkt_comp callback is invoked, 3080 * and that implies TRAN_ACCEPT return value. Any other returned value 3081 * indicates that the scsi packet was not accepted (the reason will not 3082 * be checked by the scsi target driver). 3083 * To make debugging easier, we set pkt_reason to know value here. 3084 * It may be changed later when different completion reason is 3085 * determined. 3086 */ 3087 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 3088 *reason = CMD_TRAN_ERR; 3089 3090 /* Validate address */ 3091 switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst, 3092 &spx->txlt_scsi_pkt->pkt_address, &sata_device)) { 3093 3094 case -1: 3095 /* Invalid address or invalid device type */ 3096 return (TRAN_BADPKT); 3097 case 2: 3098 /* 3099 * Valid address but device type is unknown - Chack if it is 3100 * in the reset state and therefore in an indeterminate state. 3101 */ 3102 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3103 &spx->txlt_sata_pkt->satapkt_device); 3104 if (sdinfo != NULL && (sdinfo->satadrv_event_flags & 3105 (SATA_EVNT_DEVICE_RESET | 3106 SATA_EVNT_INPROC_DEVICE_RESET)) != 0) { 3107 if (!ddi_in_panic()) { 3108 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 3109 *reason = CMD_INCOMPLETE; 3110 SATADBG1(SATA_DBG_SCSI_IF, 3111 spx->txlt_sata_hba_inst, 3112 "sata_scsi_start: rejecting command " 3113 "because of device reset state\n", NULL); 3114 return (TRAN_BUSY); 3115 } 3116 } 3117 /* FALLTHROUGH */ 3118 case 1: 3119 /* valid address but no valid device - it has disappeared */ 3120 spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE; 3121 *reason = CMD_DEV_GONE; 3122 /* 3123 * The sd target driver is checking CMD_DEV_GONE pkt_reason 3124 * only in callback function (for normal requests) and 3125 * in the dump code path. 3126 * So, if the callback is available, we need to do 3127 * the callback rather than returning TRAN_FATAL_ERROR here. 3128 */ 3129 if (spx->txlt_scsi_pkt->pkt_comp != NULL) { 3130 /* scsi callback required */ 3131 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3132 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3133 (void *)spx->txlt_scsi_pkt, 3134 TQ_SLEEP) == NULL) 3135 /* Scheduling the callback failed */ 3136 return (TRAN_BUSY); 3137 3138 return (TRAN_ACCEPT); 3139 } 3140 return (TRAN_FATAL_ERROR); 3141 default: 3142 /* all OK; pkt reason will be overwritten later */ 3143 break; 3144 } 3145 /* 3146 * If in an interrupt context, reject packet if it is to be 3147 * executed in polling mode 3148 */ 3149 if (servicing_interrupt() && 3150 (spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) { 3151 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 3152 "sata_scsi_start: rejecting synchronous command because " 3153 "of interrupt context\n", NULL); 3154 return (TRAN_BUSY); 3155 } 3156 3157 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3158 &spx->txlt_sata_pkt->satapkt_device); 3159 3160 /* 3161 * If device is in reset condition, reject the packet with 3162 * TRAN_BUSY, unless: 3163 * 1. system is panicking (dumping) 3164 * In such case only one thread is running and there is no way to 3165 * process reset. 3166 * 2. cfgadm operation is is progress (internal APCTL lock is set) 3167 * Some cfgadm operations involve drive commands, so reset condition 3168 * needs to be ignored for IOCTL operations. 3169 */ 3170 if ((sdinfo->satadrv_event_flags & 3171 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) { 3172 3173 if (!ddi_in_panic() && 3174 ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst, 3175 sata_device.satadev_addr.cport) & 3176 SATA_APCTL_LOCK_PORT_BUSY) == 0)) { 3177 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 3178 *reason = CMD_INCOMPLETE; 3179 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3180 "sata_scsi_start: rejecting command because " 3181 "of device reset state\n", NULL); 3182 return (TRAN_BUSY); 3183 } 3184 } 3185 3186 /* 3187 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by 3188 * sata_scsi_pkt_init() because pkt init had to work also with 3189 * non-existing devices. 3190 * Now we know that the packet was set-up for a real device, so its 3191 * type is known. 3192 */ 3193 spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type; 3194 3195 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags; 3196 if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst, 3197 sata_device.satadev_addr.cport)->cport_event_flags & 3198 SATA_APCTL_LOCK_PORT_BUSY) != 0) { 3199 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 3200 sata_ignore_dev_reset = B_TRUE; 3201 } 3202 /* 3203 * At this point the generic translation routine determined that the 3204 * scsi packet should be accepted. Packet completion reason may be 3205 * changed later when a different completion reason is determined. 3206 */ 3207 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 3208 *reason = CMD_CMPLT; 3209 3210 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) { 3211 /* Synchronous execution */ 3212 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH | 3213 SATA_OPMODE_POLLING; 3214 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 3215 sata_ignore_dev_reset = ddi_in_panic(); 3216 } else { 3217 /* Asynchronous execution */ 3218 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH | 3219 SATA_OPMODE_INTERRUPTS; 3220 } 3221 /* Convert queuing information */ 3222 if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG) 3223 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag = 3224 B_TRUE; 3225 else if (spx->txlt_scsi_pkt->pkt_flags & 3226 (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD)) 3227 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag = 3228 B_TRUE; 3229 3230 /* Always limit pkt time */ 3231 if (spx->txlt_scsi_pkt->pkt_time == 0) 3232 spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time; 3233 else 3234 /* Pass on scsi_pkt time */ 3235 spx->txlt_sata_pkt->satapkt_time = 3236 spx->txlt_scsi_pkt->pkt_time; 3237 3238 return (TRAN_ACCEPT); 3239 } 3240 3241 3242 /* 3243 * Translate ATA Identify Device data to SCSI Inquiry data. 3244 * This function may be called only for ATA devices. 3245 * This function should not be called for ATAPI devices - they 3246 * respond directly to SCSI Inquiry command. 3247 * 3248 * SATA Identify Device data has to be valid in sata_drive_info. 3249 * Buffer has to accomodate the inquiry length (36 bytes). 3250 * 3251 * This function should be called with a port mutex held. 3252 */ 3253 static void 3254 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst, 3255 sata_drive_info_t *sdinfo, uint8_t *buf) 3256 { 3257 3258 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 3259 struct sata_id *sid = &sdinfo->satadrv_id; 3260 3261 /* Start with a nice clean slate */ 3262 bzero((void *)inq, sizeof (struct scsi_inquiry)); 3263 3264 /* 3265 * Rely on the dev_type for setting paripheral qualifier. 3266 * Assume that DTYPE_RODIRECT applies to CD/DVD R/W devices. 3267 * It could be that DTYPE_OPTICAL could also qualify in the future. 3268 * ATAPI Inquiry may provide more data to the target driver. 3269 */ 3270 inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 3271 DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */ 3272 3273 /* CFA type device is not a removable media device */ 3274 inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) && 3275 (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0; 3276 inq->inq_qual = 0; /* Device type qualifier (obsolete in SCSI3? */ 3277 inq->inq_iso = 0; /* ISO version */ 3278 inq->inq_ecma = 0; /* ECMA version */ 3279 inq->inq_ansi = 3; /* ANSI version - SCSI 3 */ 3280 inq->inq_aenc = 0; /* Async event notification cap. */ 3281 inq->inq_trmiop = 0; /* Supports TERMINATE I/O PROC msg - NO */ 3282 inq->inq_normaca = 0; /* setting NACA bit supported - NO */ 3283 inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */ 3284 inq->inq_len = 31; /* Additional length */ 3285 inq->inq_dualp = 0; /* dual port device - NO */ 3286 inq->inq_reladdr = 0; /* Supports relative addressing - NO */ 3287 inq->inq_sync = 0; /* Supports synchronous data xfers - NO */ 3288 inq->inq_linked = 0; /* Supports linked commands - NO */ 3289 /* 3290 * Queuing support - controller has to 3291 * support some sort of command queuing. 3292 */ 3293 if (SATA_QDEPTH(sata_hba_inst) > 1) 3294 inq->inq_cmdque = 1; /* Supports command queueing - YES */ 3295 else 3296 inq->inq_cmdque = 0; /* Supports command queueing - NO */ 3297 inq->inq_sftre = 0; /* Supports Soft Reset option - NO ??? */ 3298 inq->inq_wbus32 = 0; /* Supports 32 bit wide data xfers - NO */ 3299 inq->inq_wbus16 = 0; /* Supports 16 bit wide data xfers - NO */ 3300 3301 #ifdef _LITTLE_ENDIAN 3302 /* Swap text fields to match SCSI format */ 3303 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 3304 swab(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 3305 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 3306 swab(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 3307 else 3308 swab(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 3309 #else /* _LITTLE_ENDIAN */ 3310 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 3311 bcopy(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 3312 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 3313 bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 3314 else 3315 bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 3316 #endif /* _LITTLE_ENDIAN */ 3317 } 3318 3319 3320 /* 3321 * Scsi response set up for invalid command (command not supported) 3322 * 3323 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3324 */ 3325 static int 3326 sata_txlt_invalid_command(sata_pkt_txlate_t *spx) 3327 { 3328 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3329 struct scsi_extended_sense *sense; 3330 3331 scsipkt->pkt_reason = CMD_CMPLT; 3332 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3333 STATE_SENT_CMD | STATE_GOT_STATUS; 3334 3335 *scsipkt->pkt_scbp = STATUS_CHECK; 3336 3337 sense = sata_arq_sense(spx); 3338 sense->es_key = KEY_ILLEGAL_REQUEST; 3339 sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE; 3340 3341 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3342 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3343 3344 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3345 scsipkt->pkt_comp != NULL) 3346 /* scsi callback required */ 3347 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3348 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3349 (void *)spx->txlt_scsi_pkt, 3350 TQ_SLEEP) == NULL) 3351 /* Scheduling the callback failed */ 3352 return (TRAN_BUSY); 3353 return (TRAN_ACCEPT); 3354 } 3355 3356 /* 3357 * Scsi response set up for check condition with special sense key 3358 * and additional sense code. 3359 * 3360 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3361 */ 3362 static int 3363 sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code) 3364 { 3365 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 3366 int cport = SATA_TXLT_CPORT(spx); 3367 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3368 struct scsi_extended_sense *sense; 3369 3370 mutex_enter(&SATA_CPORT_MUTEX(shi, cport)); 3371 scsipkt->pkt_reason = CMD_CMPLT; 3372 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3373 STATE_SENT_CMD | STATE_GOT_STATUS; 3374 3375 *scsipkt->pkt_scbp = STATUS_CHECK; 3376 3377 sense = sata_arq_sense(spx); 3378 sense->es_key = key; 3379 sense->es_add_code = code; 3380 3381 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3382 3383 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3384 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3385 3386 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 3387 /* scsi callback required */ 3388 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3389 (task_func_t *)scsi_hba_pkt_comp, 3390 (void *)spx->txlt_scsi_pkt, 3391 TQ_SLEEP) == NULL) 3392 /* Scheduling the callback failed */ 3393 return (TRAN_BUSY); 3394 return (TRAN_ACCEPT); 3395 } 3396 3397 /* 3398 * Scsi response setup for 3399 * emulated non-data command that requires no action/return data 3400 * 3401 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3402 */ 3403 static int 3404 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx) 3405 { 3406 int rval; 3407 int reason; 3408 3409 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3410 3411 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3412 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3413 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3414 return (rval); 3415 } 3416 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3417 3418 spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3419 STATE_SENT_CMD | STATE_GOT_STATUS; 3420 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 3421 *(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD; 3422 3423 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3424 "Scsi_pkt completion reason %x\n", 3425 spx->txlt_scsi_pkt->pkt_reason); 3426 3427 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 && 3428 spx->txlt_scsi_pkt->pkt_comp != NULL) 3429 /* scsi callback required */ 3430 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3431 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3432 (void *)spx->txlt_scsi_pkt, 3433 TQ_SLEEP) == NULL) 3434 /* Scheduling the callback failed */ 3435 return (TRAN_BUSY); 3436 return (TRAN_ACCEPT); 3437 } 3438 3439 3440 /* 3441 * SATA translate command: Inquiry / Identify Device 3442 * Use cached Identify Device data for now, rather than issuing actual 3443 * Device Identify cmd request. If device is detached and re-attached, 3444 * asynchronous event processing should fetch and refresh Identify Device 3445 * data. 3446 * Two VPD pages are supported now: 3447 * Vital Product Data page 3448 * Unit Serial Number page 3449 * 3450 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3451 */ 3452 3453 #define EVPD 1 /* Extended Vital Product Data flag */ 3454 #define CMDDT 2 /* Command Support Data - Obsolete */ 3455 #define INQUIRY_SUP_VPD_PAGE 0 /* Supported VDP Pages Page COde */ 3456 #define INQUIRY_USN_PAGE 0x80 /* Unit Serial Number Page Code */ 3457 #define INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */ 3458 3459 static int 3460 sata_txlt_inquiry(sata_pkt_txlate_t *spx) 3461 { 3462 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3463 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3464 sata_drive_info_t *sdinfo; 3465 struct scsi_extended_sense *sense; 3466 int count; 3467 uint8_t *p; 3468 int i, j; 3469 uint8_t page_buf[0xff]; /* Max length */ 3470 int rval, reason; 3471 3472 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3473 3474 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3475 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3476 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3477 return (rval); 3478 } 3479 3480 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3481 &spx->txlt_sata_pkt->satapkt_device); 3482 3483 ASSERT(sdinfo != NULL); 3484 3485 scsipkt->pkt_reason = CMD_CMPLT; 3486 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3487 STATE_SENT_CMD | STATE_GOT_STATUS; 3488 3489 /* Reject not supported request */ 3490 if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */ 3491 *scsipkt->pkt_scbp = STATUS_CHECK; 3492 sense = sata_arq_sense(spx); 3493 sense->es_key = KEY_ILLEGAL_REQUEST; 3494 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3495 goto done; 3496 } 3497 3498 /* Valid Inquiry request */ 3499 *scsipkt->pkt_scbp = STATUS_GOOD; 3500 3501 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 3502 3503 /* 3504 * Because it is fully emulated command storing data 3505 * programatically in the specified buffer, release 3506 * preallocated DMA resources before storing data in the buffer, 3507 * so no unwanted DMA sync would take place. 3508 */ 3509 sata_scsi_dmafree(NULL, scsipkt); 3510 3511 if (!(scsipkt->pkt_cdbp[1] & EVPD)) { 3512 /* Standard Inquiry Data request */ 3513 struct scsi_inquiry inq; 3514 unsigned int bufsize; 3515 3516 sata_identdev_to_inquiry(spx->txlt_sata_hba_inst, 3517 sdinfo, (uint8_t *)&inq); 3518 /* Copy no more than requested */ 3519 count = MIN(bp->b_bcount, 3520 sizeof (struct scsi_inquiry)); 3521 bufsize = scsipkt->pkt_cdbp[4]; 3522 bufsize |= scsipkt->pkt_cdbp[3] << 8; 3523 count = MIN(count, bufsize); 3524 bcopy(&inq, bp->b_un.b_addr, count); 3525 3526 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3527 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 3528 bufsize - count : 0; 3529 } else { 3530 /* 3531 * peripheral_qualifier = 0; 3532 * 3533 * We are dealing only with HD and will be 3534 * dealing with CD/DVD devices soon 3535 */ 3536 uint8_t peripheral_device_type = 3537 sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 3538 DTYPE_DIRECT : DTYPE_RODIRECT; 3539 3540 switch ((uint_t)scsipkt->pkt_cdbp[2]) { 3541 case INQUIRY_SUP_VPD_PAGE: 3542 /* 3543 * Request for suported Vital Product Data 3544 * pages - assuming only 2 page codes 3545 * supported. 3546 */ 3547 page_buf[0] = peripheral_device_type; 3548 page_buf[1] = INQUIRY_SUP_VPD_PAGE; 3549 page_buf[2] = 0; 3550 page_buf[3] = 2; /* page length */ 3551 page_buf[4] = INQUIRY_SUP_VPD_PAGE; 3552 page_buf[5] = INQUIRY_USN_PAGE; 3553 /* Copy no more than requested */ 3554 count = MIN(bp->b_bcount, 6); 3555 bcopy(page_buf, bp->b_un.b_addr, count); 3556 break; 3557 3558 case INQUIRY_USN_PAGE: 3559 /* 3560 * Request for Unit Serial Number page. 3561 * Set-up the page. 3562 */ 3563 page_buf[0] = peripheral_device_type; 3564 page_buf[1] = INQUIRY_USN_PAGE; 3565 page_buf[2] = 0; 3566 /* remaining page length */ 3567 page_buf[3] = SATA_ID_SERIAL_LEN; 3568 3569 /* 3570 * Copy serial number from Identify Device data 3571 * words into the inquiry page and swap bytes 3572 * when necessary. 3573 */ 3574 p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser); 3575 #ifdef _LITTLE_ENDIAN 3576 swab(p, &page_buf[4], SATA_ID_SERIAL_LEN); 3577 #else 3578 bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN); 3579 #endif 3580 /* 3581 * Least significant character of the serial 3582 * number shall appear as the last byte, 3583 * according to SBC-3 spec. 3584 * Count trailing spaces to determine the 3585 * necessary shift length. 3586 */ 3587 p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1]; 3588 for (j = 0; j < SATA_ID_SERIAL_LEN; j++) { 3589 if (*(p - j) != '\0' && 3590 *(p - j) != '\040') 3591 break; 3592 } 3593 3594 /* 3595 * Shift SN string right, so that the last 3596 * non-blank character would appear in last 3597 * byte of SN field in the page. 3598 * 'j' is the shift length. 3599 */ 3600 for (i = 0; 3601 i < (SATA_ID_SERIAL_LEN - j) && j != 0; 3602 i++, p--) 3603 *p = *(p - j); 3604 3605 /* 3606 * Add leading spaces - same number as the 3607 * shift size 3608 */ 3609 for (; j > 0; j--) 3610 page_buf[4 + j - 1] = '\040'; 3611 3612 count = MIN(bp->b_bcount, 3613 SATA_ID_SERIAL_LEN + 4); 3614 bcopy(page_buf, bp->b_un.b_addr, count); 3615 break; 3616 3617 case INQUIRY_DEV_IDENTIFICATION_PAGE: 3618 /* 3619 * We may want to implement this page, when 3620 * identifiers are common for SATA devices 3621 * But not now. 3622 */ 3623 /*FALLTHROUGH*/ 3624 3625 default: 3626 /* Request for unsupported VPD page */ 3627 *scsipkt->pkt_scbp = STATUS_CHECK; 3628 sense = sata_arq_sense(spx); 3629 sense->es_key = KEY_ILLEGAL_REQUEST; 3630 sense->es_add_code = 3631 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3632 goto done; 3633 } 3634 } 3635 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3636 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 3637 scsipkt->pkt_cdbp[4] - count : 0; 3638 } 3639 done: 3640 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3641 3642 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3643 "Scsi_pkt completion reason %x\n", 3644 scsipkt->pkt_reason); 3645 3646 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3647 scsipkt->pkt_comp != NULL) { 3648 /* scsi callback required */ 3649 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3650 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3651 TQ_SLEEP) == NULL) 3652 /* Scheduling the callback failed */ 3653 return (TRAN_BUSY); 3654 } 3655 return (TRAN_ACCEPT); 3656 } 3657 3658 /* 3659 * SATA translate command: Request Sense. 3660 * 3661 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3662 * At the moment this is an emulated command (ATA version for SATA hard disks). 3663 * May be translated into Check Power Mode command in the future. 3664 * 3665 * Note: There is a mismatch between already implemented Informational 3666 * Exception Mode Select page 0x1C and this function. 3667 * When MRIE bit is set in page 0x1C, Request Sense is supposed to return 3668 * NO SENSE and set additional sense code to the exception code - this is not 3669 * implemented here. 3670 */ 3671 static int 3672 sata_txlt_request_sense(sata_pkt_txlate_t *spx) 3673 { 3674 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3675 struct scsi_extended_sense sense; 3676 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3677 sata_drive_info_t *sdinfo; 3678 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 3679 int rval, reason, power_state = 0; 3680 3681 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3682 3683 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3684 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3685 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3686 return (rval); 3687 } 3688 3689 scsipkt->pkt_reason = CMD_CMPLT; 3690 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3691 STATE_SENT_CMD | STATE_GOT_STATUS; 3692 *scsipkt->pkt_scbp = STATUS_GOOD; 3693 3694 /* 3695 * when CONTROL field's NACA bit == 1 3696 * return ILLEGAL_REQUEST 3697 */ 3698 if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) { 3699 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3700 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 3701 SD_SCSI_ASC_CMD_SEQUENCE_ERR)); 3702 } 3703 3704 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3705 &spx->txlt_sata_pkt->satapkt_device); 3706 ASSERT(sdinfo != NULL); 3707 3708 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH; 3709 3710 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE); 3711 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE; 3712 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 3713 if (sata_hba_start(spx, &rval) != 0) { 3714 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3715 return (rval); 3716 } else { 3717 if (scmd->satacmd_error_reg != 0) { 3718 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3719 return (sata_txlt_check_condition(spx, KEY_NO_SENSE, 3720 SD_SCSI_ASC_NO_ADD_SENSE)); 3721 } 3722 } 3723 3724 switch (scmd->satacmd_sec_count_lsb) { 3725 case SATA_PWRMODE_STANDBY: /* device in standby mode */ 3726 if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED) 3727 power_state = SATA_POWER_STOPPED; 3728 else { 3729 power_state = SATA_POWER_STANDBY; 3730 sdinfo->satadrv_power_level = SATA_POWER_STANDBY; 3731 } 3732 break; 3733 case SATA_PWRMODE_IDLE: /* device in idle mode */ 3734 power_state = SATA_POWER_IDLE; 3735 sdinfo->satadrv_power_level = SATA_POWER_IDLE; 3736 break; 3737 case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */ 3738 default: /* 0x40, 0x41 active mode */ 3739 if (sdinfo->satadrv_power_level == SATA_POWER_IDLE) 3740 power_state = SATA_POWER_IDLE; 3741 else { 3742 power_state = SATA_POWER_ACTIVE; 3743 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 3744 } 3745 break; 3746 } 3747 3748 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3749 3750 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 3751 /* 3752 * Because it is fully emulated command storing data 3753 * programatically in the specified buffer, release 3754 * preallocated DMA resources before storing data in the buffer, 3755 * so no unwanted DMA sync would take place. 3756 */ 3757 int count = MIN(bp->b_bcount, 3758 sizeof (struct scsi_extended_sense)); 3759 sata_scsi_dmafree(NULL, scsipkt); 3760 bzero(&sense, sizeof (struct scsi_extended_sense)); 3761 sense.es_valid = 0; /* Valid LBA */ 3762 sense.es_class = 7; /* Response code 0x70 - current err */ 3763 sense.es_key = KEY_NO_SENSE; 3764 sense.es_add_len = 6; /* Additional length */ 3765 /* Copy no more than requested */ 3766 bcopy(&sense, bp->b_un.b_addr, count); 3767 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3768 scsipkt->pkt_resid = 0; 3769 switch (power_state) { 3770 case SATA_POWER_IDLE: 3771 case SATA_POWER_STANDBY: 3772 sense.es_add_code = 3773 SD_SCSI_ASC_LOW_POWER_CONDITION_ON; 3774 break; 3775 case SATA_POWER_STOPPED: 3776 sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE; 3777 break; 3778 case SATA_POWER_ACTIVE: 3779 default: 3780 break; 3781 } 3782 } 3783 3784 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3785 "Scsi_pkt completion reason %x\n", 3786 scsipkt->pkt_reason); 3787 3788 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 3789 /* scsi callback required */ 3790 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3791 (task_func_t *)scsi_hba_pkt_comp, (void *) scsipkt, 3792 TQ_SLEEP) == NULL) 3793 /* Scheduling the callback failed */ 3794 return (TRAN_BUSY); 3795 return (TRAN_ACCEPT); 3796 } 3797 3798 /* 3799 * SATA translate command: Test Unit Ready 3800 * (ATA version for SATA hard disks). 3801 * It is translated into the Check Power Mode command. 3802 * 3803 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3804 */ 3805 static int 3806 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx) 3807 { 3808 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3809 struct scsi_extended_sense *sense; 3810 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 3811 sata_drive_info_t *sdinfo; 3812 int power_state; 3813 int rval, reason; 3814 3815 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3816 3817 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3818 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3819 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3820 return (rval); 3821 } 3822 3823 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3824 &spx->txlt_sata_pkt->satapkt_device); 3825 ASSERT(sdinfo != NULL); 3826 3827 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH; 3828 3829 /* send CHECK POWER MODE command */ 3830 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE); 3831 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE; 3832 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 3833 if (sata_hba_start(spx, &rval) != 0) { 3834 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3835 return (rval); 3836 } else { 3837 if (scmd->satacmd_error_reg != 0) { 3838 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3839 return (sata_txlt_check_condition(spx, KEY_NOT_READY, 3840 SD_SCSI_ASC_LU_NOT_RESPONSE)); 3841 } 3842 } 3843 3844 power_state = scmd->satacmd_sec_count_lsb; 3845 3846 /* 3847 * return NOT READY when device in STOPPED mode 3848 */ 3849 if (power_state == SATA_PWRMODE_STANDBY && 3850 sdinfo->satadrv_power_level == SATA_POWER_STOPPED) { 3851 *scsipkt->pkt_scbp = STATUS_CHECK; 3852 sense = sata_arq_sense(spx); 3853 sense->es_key = KEY_NOT_READY; 3854 sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY; 3855 } else { 3856 /* 3857 * For other power mode, return GOOD status 3858 */ 3859 *scsipkt->pkt_scbp = STATUS_GOOD; 3860 } 3861 3862 scsipkt->pkt_reason = CMD_CMPLT; 3863 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3864 STATE_SENT_CMD | STATE_GOT_STATUS; 3865 3866 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3867 3868 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3869 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3870 3871 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 3872 /* scsi callback required */ 3873 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3874 (task_func_t *)scsi_hba_pkt_comp, (void *) scsipkt, 3875 TQ_SLEEP) == NULL) 3876 /* Scheduling the callback failed */ 3877 return (TRAN_BUSY); 3878 3879 return (TRAN_ACCEPT); 3880 } 3881 3882 /* 3883 * SATA translate command: Start Stop Unit 3884 * Translation depends on a command: 3885 * 3886 * Power condition bits will be supported 3887 * and the power level should be maintained by SATL, 3888 * When SATL received a command, it will check the 3889 * power level firstly, and return the status according 3890 * to SAT2 v2.6 and SAT-2 Standby Modifications 3891 * 3892 * SPC-4/SBC-3 SATL ATA power condition SATL SPC/SBC 3893 * ----------------------------------------------------------------------- 3894 * SSU_PC1 Active <==> ATA Active <==> SSU:start_bit =1 3895 * SSU_PC2 Idle <==> ATA Idle <==> N/A 3896 * SSU_PC3 Standby <==> ATA Standby <==> N/A 3897 * SSU_PC4 Stopped <==> ATA Standby <==> SSU:start_bit = 0 3898 * 3899 * Unload Media / NOT SUPPORTED YET 3900 * Load Media / NOT SUPPROTED YET 3901 * Immediate bit / NOT SUPPORTED YET (deferred error) 3902 * 3903 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 3904 * appropriate values in scsi_pkt fields. 3905 */ 3906 static int 3907 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx) 3908 { 3909 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3910 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 3911 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 3912 int cport = SATA_TXLT_CPORT(spx); 3913 int rval, reason; 3914 sata_drive_info_t *sdinfo; 3915 sata_id_t *sata_id; 3916 3917 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3918 "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1); 3919 3920 mutex_enter(&SATA_CPORT_MUTEX(shi, cport)); 3921 3922 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3923 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3924 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3925 return (rval); 3926 } 3927 3928 if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) { 3929 /* IMMED bit - not supported */ 3930 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3931 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 3932 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 3933 } 3934 3935 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH; 3936 spx->txlt_sata_pkt->satapkt_comp = NULL; 3937 3938 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3939 &spx->txlt_sata_pkt->satapkt_device); 3940 ASSERT(sdinfo != NULL); 3941 sata_id = &sdinfo->satadrv_id; 3942 3943 switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) { 3944 case 0: 3945 if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) { 3946 /* Load/Unload Media - invalid request */ 3947 goto err_out; 3948 } 3949 if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) { 3950 /* Start Unit */ 3951 sata_build_read_verify_cmd(scmd, 1, 5); 3952 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 3953 /* Transfer command to HBA */ 3954 if (sata_hba_start(spx, &rval) != 0) { 3955 /* Pkt not accepted for execution */ 3956 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3957 return (rval); 3958 } else { 3959 if (scmd->satacmd_error_reg != 0) { 3960 goto err_out; 3961 } 3962 } 3963 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 3964 } else { 3965 /* Stop Unit */ 3966 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 3967 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 3968 if (sata_hba_start(spx, &rval) != 0) { 3969 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3970 return (rval); 3971 } else { 3972 if (scmd->satacmd_error_reg != 0) { 3973 goto err_out; 3974 } 3975 } 3976 /* ata standby immediate command */ 3977 sata_build_generic_cmd(scmd, SATAC_STANDBY_IM); 3978 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 3979 if (sata_hba_start(spx, &rval) != 0) { 3980 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3981 return (rval); 3982 } else { 3983 if (scmd->satacmd_error_reg != 0) { 3984 goto err_out; 3985 } 3986 } 3987 sdinfo->satadrv_power_level = SATA_POWER_STOPPED; 3988 } 3989 break; 3990 case 0x1: 3991 sata_build_generic_cmd(scmd, SATAC_IDLE); 3992 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 3993 if (sata_hba_start(spx, &rval) != 0) { 3994 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3995 return (rval); 3996 } else { 3997 if (scmd->satacmd_error_reg != 0) { 3998 goto err_out; 3999 } 4000 } 4001 sata_build_read_verify_cmd(scmd, 1, 5); 4002 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4003 /* Transfer command to HBA */ 4004 if (sata_hba_start(spx, &rval) != 0) { 4005 /* Pkt not accepted for execution */ 4006 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4007 return (rval); 4008 } else { 4009 if (scmd->satacmd_error_reg != 0) { 4010 goto err_out; 4011 } 4012 } 4013 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 4014 break; 4015 case 0x2: 4016 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 4017 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4018 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) { 4019 if (sata_hba_start(spx, &rval) != 0) { 4020 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4021 return (rval); 4022 } else { 4023 if (scmd->satacmd_error_reg != 0) { 4024 goto err_out; 4025 } 4026 } 4027 } 4028 sata_build_generic_cmd(scmd, SATAC_IDLE); 4029 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4030 if (sata_hba_start(spx, &rval) != 0) { 4031 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4032 return (rval); 4033 } else { 4034 if (scmd->satacmd_error_reg != 0) { 4035 goto err_out; 4036 } 4037 } 4038 if ((scsipkt->pkt_cdbp[3] & START_STOP_MODIFIER_MASK)) { 4039 /* 4040 * POWER CONDITION MODIFIER bit set 4041 * to 0x1 or larger it will be handled 4042 * on the same way as bit = 0x1 4043 */ 4044 if (!(sata_id->ai_cmdset84 & 4045 SATA_IDLE_UNLOAD_SUPPORTED)) { 4046 sdinfo->satadrv_power_level = SATA_POWER_IDLE; 4047 break; 4048 } 4049 sata_build_generic_cmd(scmd, SATAC_IDLE_IM); 4050 scmd->satacmd_features_reg = 0x44; 4051 scmd->satacmd_lba_low_lsb = 0x4c; 4052 scmd->satacmd_lba_mid_lsb = 0x4e; 4053 scmd->satacmd_lba_high_lsb = 0x55; 4054 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4055 if (sata_hba_start(spx, &rval) != 0) { 4056 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4057 return (rval); 4058 } else { 4059 if (scmd->satacmd_error_reg != 0) { 4060 goto err_out; 4061 } 4062 } 4063 } 4064 sdinfo->satadrv_power_level = SATA_POWER_IDLE; 4065 break; 4066 case 0x3: 4067 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 4068 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4069 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) { 4070 if (sata_hba_start(spx, &rval) != 0) { 4071 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4072 return (rval); 4073 } else { 4074 if (scmd->satacmd_error_reg != 0) { 4075 goto err_out; 4076 } 4077 } 4078 } 4079 sata_build_generic_cmd(scmd, SATAC_STANDBY); 4080 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4081 if (sata_hba_start(spx, &rval) != 0) { 4082 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4083 return (rval); 4084 } else { 4085 if (scmd->satacmd_error_reg != 0) { 4086 goto err_out; 4087 } 4088 } 4089 sdinfo->satadrv_power_level = SATA_POWER_STANDBY; 4090 break; 4091 case 0x7: 4092 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE); 4093 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE; 4094 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4095 if (sata_hba_start(spx, &rval) != 0) { 4096 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4097 return (rval); 4098 } else { 4099 if (scmd->satacmd_error_reg != 0) { 4100 goto err_out; 4101 } 4102 } 4103 switch (scmd->satacmd_sec_count_lsb) { 4104 case SATA_PWRMODE_STANDBY: 4105 sata_build_generic_cmd(scmd, SATAC_STANDBY); 4106 scmd->satacmd_sec_count_msb = sata_get_standby_timer( 4107 sdinfo->satadrv_standby_timer); 4108 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4109 if (sata_hba_start(spx, &rval) != 0) { 4110 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4111 return (rval); 4112 } else { 4113 if (scmd->satacmd_error_reg != 0) { 4114 goto err_out; 4115 } 4116 } 4117 break; 4118 case SATA_PWRMODE_IDLE: 4119 sata_build_generic_cmd(scmd, SATAC_IDLE); 4120 scmd->satacmd_sec_count_msb = sata_get_standby_timer( 4121 sdinfo->satadrv_standby_timer); 4122 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4123 if (sata_hba_start(spx, &rval) != 0) { 4124 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4125 return (rval); 4126 } else { 4127 if (scmd->satacmd_error_reg != 0) { 4128 goto err_out; 4129 } 4130 } 4131 break; 4132 case SATA_PWRMODE_ACTIVE_SPINDOWN: 4133 case SATA_PWRMODE_ACTIVE_SPINUP: 4134 case SATA_PWRMODE_ACTIVE: 4135 sata_build_generic_cmd(scmd, SATAC_IDLE); 4136 scmd->satacmd_sec_count_msb = sata_get_standby_timer( 4137 sdinfo->satadrv_standby_timer); 4138 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4139 if (sata_hba_start(spx, &rval) != 0) { 4140 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4141 return (rval); 4142 } else { 4143 if (scmd->satacmd_error_reg != 0) { 4144 goto err_out; 4145 } 4146 } 4147 sata_build_read_verify_cmd(scmd, 1, 5); 4148 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4149 if (sata_hba_start(spx, &rval) != 0) { 4150 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4151 return (rval); 4152 } else { 4153 if (scmd->satacmd_error_reg != 0) { 4154 goto err_out; 4155 } 4156 } 4157 break; 4158 default: 4159 goto err_out; 4160 } 4161 break; 4162 case 0xb: 4163 if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) == 4164 0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) { 4165 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4166 return (sata_txlt_check_condition(spx, 4167 KEY_ILLEGAL_REQUEST, 4168 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4169 } 4170 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 4171 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4172 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) { 4173 if (sata_hba_start(spx, &rval) != 0) { 4174 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4175 return (rval); 4176 } else { 4177 if (scmd->satacmd_error_reg != 0) { 4178 goto err_out; 4179 } 4180 } 4181 sata_build_generic_cmd(scmd, SATAC_STANDBY_IM); 4182 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4183 if (sata_hba_start(spx, &rval) != 0) { 4184 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4185 return (rval); 4186 } else { 4187 if (scmd->satacmd_error_reg != 0) { 4188 goto err_out; 4189 } 4190 } 4191 } 4192 bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4); 4193 break; 4194 default: 4195 err_out: 4196 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4197 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 4198 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4199 } 4200 4201 /* 4202 * Since it was a synchronous command, 4203 * a callback function will be called directly. 4204 */ 4205 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4206 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4207 "synchronous execution status %x\n", 4208 spx->txlt_sata_pkt->satapkt_reason); 4209 4210 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) { 4211 sata_set_arq_data(spx->txlt_sata_pkt); 4212 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4213 (task_func_t *)scsi_hba_pkt_comp, (void *) scsipkt, 4214 TQ_SLEEP) == 0) { 4215 return (TRAN_BUSY); 4216 } 4217 } 4218 else 4219 4220 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 4221 4222 return (TRAN_ACCEPT); 4223 4224 } 4225 4226 /* 4227 * SATA translate command: Read Capacity. 4228 * Emulated command for SATA disks. 4229 * Capacity is retrieved from cached Idenifty Device data. 4230 * Identify Device data shows effective disk capacity, not the native 4231 * capacity, which may be limitted by Set Max Address command. 4232 * This is ATA version for SATA hard disks. 4233 * 4234 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4235 */ 4236 static int 4237 sata_txlt_read_capacity(sata_pkt_txlate_t *spx) 4238 { 4239 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4240 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4241 sata_drive_info_t *sdinfo; 4242 uint64_t val; 4243 uchar_t *rbuf; 4244 int rval, reason; 4245 4246 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4247 "sata_txlt_read_capacity: ", NULL); 4248 4249 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4250 4251 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 4252 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4253 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4254 return (rval); 4255 } 4256 4257 scsipkt->pkt_reason = CMD_CMPLT; 4258 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4259 STATE_SENT_CMD | STATE_GOT_STATUS; 4260 *scsipkt->pkt_scbp = STATUS_GOOD; 4261 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4262 /* 4263 * Because it is fully emulated command storing data 4264 * programatically in the specified buffer, release 4265 * preallocated DMA resources before storing data in the buffer, 4266 * so no unwanted DMA sync would take place. 4267 */ 4268 sata_scsi_dmafree(NULL, scsipkt); 4269 4270 sdinfo = sata_get_device_info( 4271 spx->txlt_sata_hba_inst, 4272 &spx->txlt_sata_pkt->satapkt_device); 4273 /* Last logical block address */ 4274 val = sdinfo->satadrv_capacity - 1; 4275 rbuf = (uchar_t *)bp->b_un.b_addr; 4276 /* Need to swap endians to match scsi format */ 4277 rbuf[0] = (val >> 24) & 0xff; 4278 rbuf[1] = (val >> 16) & 0xff; 4279 rbuf[2] = (val >> 8) & 0xff; 4280 rbuf[3] = val & 0xff; 4281 /* block size - always 512 bytes, for now */ 4282 rbuf[4] = 0; 4283 rbuf[5] = 0; 4284 rbuf[6] = 0x02; 4285 rbuf[7] = 0; 4286 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4287 scsipkt->pkt_resid = 0; 4288 4289 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n", 4290 sdinfo->satadrv_capacity -1); 4291 } 4292 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4293 /* 4294 * If a callback was requested, do it now. 4295 */ 4296 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4297 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4298 4299 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4300 scsipkt->pkt_comp != NULL) 4301 /* scsi callback required */ 4302 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4303 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4304 TQ_SLEEP) == NULL) 4305 /* Scheduling the callback failed */ 4306 return (TRAN_BUSY); 4307 4308 return (TRAN_ACCEPT); 4309 } 4310 4311 /* 4312 * SATA translate command: Mode Sense. 4313 * Translated into appropriate SATA command or emulated. 4314 * Saved Values Page Control (03) are not supported. 4315 * 4316 * NOTE: only caching mode sense page is currently implemented. 4317 * 4318 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4319 */ 4320 4321 #define LLBAA 0x10 /* Long LBA Accepted */ 4322 4323 static int 4324 sata_txlt_mode_sense(sata_pkt_txlate_t *spx) 4325 { 4326 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4327 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4328 sata_drive_info_t *sdinfo; 4329 sata_id_t *sata_id; 4330 struct scsi_extended_sense *sense; 4331 int len, bdlen, count, alc_len; 4332 int pc; /* Page Control code */ 4333 uint8_t *buf; /* mode sense buffer */ 4334 int rval, reason; 4335 4336 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4337 "sata_txlt_mode_sense, pc %x page code 0x%02x\n", 4338 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 4339 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 4340 4341 buf = kmem_zalloc(1024, KM_SLEEP); 4342 4343 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4344 4345 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 4346 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4347 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4348 kmem_free(buf, 1024); 4349 return (rval); 4350 } 4351 4352 scsipkt->pkt_reason = CMD_CMPLT; 4353 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4354 STATE_SENT_CMD | STATE_GOT_STATUS; 4355 4356 pc = scsipkt->pkt_cdbp[2] >> 6; 4357 4358 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4359 /* 4360 * Because it is fully emulated command storing data 4361 * programatically in the specified buffer, release 4362 * preallocated DMA resources before storing data in the buffer, 4363 * so no unwanted DMA sync would take place. 4364 */ 4365 sata_scsi_dmafree(NULL, scsipkt); 4366 4367 len = 0; 4368 bdlen = 0; 4369 if (!(scsipkt->pkt_cdbp[1] & 8)) { 4370 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 && 4371 (scsipkt->pkt_cdbp[1] & LLBAA)) 4372 bdlen = 16; 4373 else 4374 bdlen = 8; 4375 } 4376 /* Build mode parameter header */ 4377 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 4378 /* 4-byte mode parameter header */ 4379 buf[len++] = 0; /* mode data length */ 4380 buf[len++] = 0; /* medium type */ 4381 buf[len++] = 0; /* dev-specific param */ 4382 buf[len++] = bdlen; /* Block Descriptor length */ 4383 } else { 4384 /* 8-byte mode parameter header */ 4385 buf[len++] = 0; /* mode data length */ 4386 buf[len++] = 0; 4387 buf[len++] = 0; /* medium type */ 4388 buf[len++] = 0; /* dev-specific param */ 4389 if (bdlen == 16) 4390 buf[len++] = 1; /* long lba descriptor */ 4391 else 4392 buf[len++] = 0; 4393 buf[len++] = 0; 4394 buf[len++] = 0; /* Block Descriptor length */ 4395 buf[len++] = bdlen; 4396 } 4397 4398 sdinfo = sata_get_device_info( 4399 spx->txlt_sata_hba_inst, 4400 &spx->txlt_sata_pkt->satapkt_device); 4401 4402 /* Build block descriptor only if not disabled (DBD) */ 4403 if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) { 4404 /* Block descriptor - direct-access device format */ 4405 if (bdlen == 8) { 4406 /* build regular block descriptor */ 4407 buf[len++] = 4408 (sdinfo->satadrv_capacity >> 24) & 0xff; 4409 buf[len++] = 4410 (sdinfo->satadrv_capacity >> 16) & 0xff; 4411 buf[len++] = 4412 (sdinfo->satadrv_capacity >> 8) & 0xff; 4413 buf[len++] = sdinfo->satadrv_capacity & 0xff; 4414 buf[len++] = 0; /* density code */ 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 } else if (bdlen == 16) { 4424 /* Long LBA Accepted */ 4425 /* build long lba block descriptor */ 4426 #ifndef __lock_lint 4427 buf[len++] = 4428 (sdinfo->satadrv_capacity >> 56) & 0xff; 4429 buf[len++] = 4430 (sdinfo->satadrv_capacity >> 48) & 0xff; 4431 buf[len++] = 4432 (sdinfo->satadrv_capacity >> 40) & 0xff; 4433 buf[len++] = 4434 (sdinfo->satadrv_capacity >> 32) & 0xff; 4435 #endif 4436 buf[len++] = 4437 (sdinfo->satadrv_capacity >> 24) & 0xff; 4438 buf[len++] = 4439 (sdinfo->satadrv_capacity >> 16) & 0xff; 4440 buf[len++] = 4441 (sdinfo->satadrv_capacity >> 8) & 0xff; 4442 buf[len++] = sdinfo->satadrv_capacity & 0xff; 4443 buf[len++] = 0; 4444 buf[len++] = 0; /* density code */ 4445 buf[len++] = 0; 4446 buf[len++] = 0; 4447 if (sdinfo->satadrv_type == 4448 SATA_DTYPE_ATADISK) 4449 buf[len++] = 2; 4450 else 4451 /* ATAPI */ 4452 buf[len++] = 8; 4453 buf[len++] = 0; 4454 } 4455 } 4456 4457 sata_id = &sdinfo->satadrv_id; 4458 4459 /* 4460 * Add requested pages. 4461 * Page 3 and 4 are obsolete and we are not supporting them. 4462 * We deal now with: 4463 * caching (read/write cache control). 4464 * We should eventually deal with following mode pages: 4465 * error recovery (0x01), 4466 * power condition (0x1a), 4467 * exception control page (enables SMART) (0x1c), 4468 * enclosure management (ses), 4469 * protocol-specific port mode (port control). 4470 */ 4471 switch (scsipkt->pkt_cdbp[2] & 0x3f) { 4472 case MODEPAGE_RW_ERRRECOV: 4473 /* DAD_MODE_ERR_RECOV */ 4474 /* R/W recovery */ 4475 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 4476 break; 4477 case MODEPAGE_CACHING: 4478 /* DAD_MODE_CACHE */ 4479 /* Reject not supported request for saved parameters */ 4480 if (pc == 3) { 4481 *scsipkt->pkt_scbp = STATUS_CHECK; 4482 sense = sata_arq_sense(spx); 4483 sense->es_key = KEY_ILLEGAL_REQUEST; 4484 sense->es_add_code = 4485 SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED; 4486 goto done; 4487 } 4488 4489 /* caching */ 4490 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 4491 break; 4492 case MODEPAGE_INFO_EXCPT: 4493 /* exception cntrl */ 4494 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 4495 len += sata_build_msense_page_1c(sdinfo, pc, 4496 buf+len); 4497 } 4498 else 4499 goto err; 4500 break; 4501 case MODEPAGE_POWER_COND: 4502 /* DAD_MODE_POWER_COND */ 4503 /* power condition */ 4504 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 4505 break; 4506 4507 case MODEPAGE_ACOUSTIC_MANAG: 4508 /* acoustic management */ 4509 len += sata_build_msense_page_30(sdinfo, pc, buf+len); 4510 break; 4511 case MODEPAGE_ALLPAGES: 4512 /* all pages */ 4513 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 4514 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 4515 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 4516 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 4517 len += sata_build_msense_page_1c(sdinfo, pc, 4518 buf+len); 4519 } 4520 len += sata_build_msense_page_30(sdinfo, pc, buf+len); 4521 break; 4522 default: 4523 err: 4524 /* Invalid request */ 4525 *scsipkt->pkt_scbp = STATUS_CHECK; 4526 sense = sata_arq_sense(spx); 4527 sense->es_key = KEY_ILLEGAL_REQUEST; 4528 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4529 goto done; 4530 } 4531 4532 /* fix total mode data length */ 4533 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 4534 /* 4-byte mode parameter header */ 4535 buf[0] = len - 1; /* mode data length */ 4536 } else { 4537 buf[0] = (len -2) >> 8; 4538 buf[1] = (len -2) & 0xff; 4539 } 4540 4541 4542 /* Check allocation length */ 4543 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 4544 alc_len = scsipkt->pkt_cdbp[4]; 4545 } else { 4546 alc_len = scsipkt->pkt_cdbp[7]; 4547 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 4548 } 4549 /* 4550 * We do not check for possible parameters truncation 4551 * (alc_len < len) assuming that the target driver works 4552 * correctly. Just avoiding overrun. 4553 * Copy no more than requested and possible, buffer-wise. 4554 */ 4555 count = MIN(alc_len, len); 4556 count = MIN(bp->b_bcount, count); 4557 bcopy(buf, bp->b_un.b_addr, count); 4558 4559 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4560 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 4561 } 4562 *scsipkt->pkt_scbp = STATUS_GOOD; 4563 done: 4564 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4565 (void) kmem_free(buf, 1024); 4566 4567 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4568 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4569 4570 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4571 scsipkt->pkt_comp != NULL) 4572 /* scsi callback required */ 4573 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4574 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4575 TQ_SLEEP) == NULL) 4576 /* Scheduling the callback failed */ 4577 return (TRAN_BUSY); 4578 4579 return (TRAN_ACCEPT); 4580 } 4581 4582 4583 /* 4584 * SATA translate command: Mode Select. 4585 * Translated into appropriate SATA command or emulated. 4586 * Saving parameters is not supported. 4587 * Changing device capacity is not supported (although theoretically 4588 * possible by executing SET FEATURES/SET MAX ADDRESS) 4589 * 4590 * Assumption is that the target driver is working correctly. 4591 * 4592 * More than one SATA command may be executed to perform operations specified 4593 * by mode select pages. The first error terminates further execution. 4594 * Operations performed successully are not backed-up in such case. 4595 * 4596 * NOTE: Implemented pages: 4597 * - caching page 4598 * - informational exception page 4599 * - acoustic management page 4600 * - power condition page 4601 * Caching setup is remembered so it could be re-stored in case of 4602 * an unexpected device reset. 4603 * 4604 * Returns TRAN_XXXX. 4605 * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields. 4606 */ 4607 4608 static int 4609 sata_txlt_mode_select(sata_pkt_txlate_t *spx) 4610 { 4611 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4612 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4613 struct scsi_extended_sense *sense; 4614 int len, pagelen, count, pllen; 4615 uint8_t *buf; /* mode select buffer */ 4616 int rval, stat, reason; 4617 uint_t nointr_flag; 4618 int dmod = 0; 4619 4620 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4621 "sata_txlt_mode_select, pc %x page code 0x%02x\n", 4622 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 4623 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 4624 4625 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4626 4627 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 4628 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4629 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4630 return (rval); 4631 } 4632 /* 4633 * If in interrupt context, reject this packet because it may result 4634 * in issuing a synchronous command to HBA. 4635 */ 4636 if (servicing_interrupt()) { 4637 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 4638 "sata_txlt_mode_select: rejecting command because " 4639 "of interrupt context\n", NULL); 4640 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4641 return (TRAN_BUSY); 4642 } 4643 4644 rval = TRAN_ACCEPT; 4645 4646 scsipkt->pkt_reason = CMD_CMPLT; 4647 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4648 STATE_SENT_CMD | STATE_GOT_STATUS; 4649 nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR; 4650 4651 /* Reject not supported request */ 4652 if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */ 4653 *scsipkt->pkt_scbp = STATUS_CHECK; 4654 sense = sata_arq_sense(spx); 4655 sense->es_key = KEY_ILLEGAL_REQUEST; 4656 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4657 goto done; 4658 } 4659 4660 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 4661 pllen = scsipkt->pkt_cdbp[4]; 4662 } else { 4663 pllen = scsipkt->pkt_cdbp[7]; 4664 pllen = (pllen << 8) | scsipkt->pkt_cdbp[7]; 4665 } 4666 4667 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 4668 4669 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) { 4670 buf = (uint8_t *)bp->b_un.b_addr; 4671 count = MIN(bp->b_bcount, pllen); 4672 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4673 scsipkt->pkt_resid = 0; 4674 pllen = count; 4675 4676 /* 4677 * Check the header to skip the block descriptor(s) - we 4678 * do not support setting device capacity. 4679 * Existing macros do not recognize long LBA dscriptor, 4680 * hence manual calculation. 4681 */ 4682 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 4683 /* 6-bytes CMD, 4 bytes header */ 4684 if (count <= 4) 4685 goto done; /* header only */ 4686 len = buf[3] + 4; 4687 } else { 4688 /* 10-bytes CMD, 8 bytes header */ 4689 if (count <= 8) 4690 goto done; /* header only */ 4691 len = buf[6]; 4692 len = (len << 8) + buf[7] + 8; 4693 } 4694 if (len >= count) 4695 goto done; /* header + descriptor(s) only */ 4696 4697 pllen -= len; /* remaining data length */ 4698 4699 /* 4700 * We may be executing SATA command and want to execute it 4701 * in SYNCH mode, regardless of scsi_pkt setting. 4702 * Save scsi_pkt setting and indicate SYNCH mode 4703 */ 4704 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4705 scsipkt->pkt_comp != NULL) { 4706 scsipkt->pkt_flags |= FLAG_NOINTR; 4707 } 4708 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH; 4709 4710 /* 4711 * len is now the offset to a first mode select page 4712 * Process all pages 4713 */ 4714 while (pllen > 0) { 4715 switch ((int)buf[len]) { 4716 case MODEPAGE_CACHING: 4717 /* No support for SP (saving) */ 4718 if (scsipkt->pkt_cdbp[1] & 0x01) { 4719 *scsipkt->pkt_scbp = STATUS_CHECK; 4720 sense = sata_arq_sense(spx); 4721 sense->es_key = KEY_ILLEGAL_REQUEST; 4722 sense->es_add_code = 4723 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4724 goto done; 4725 } 4726 stat = sata_mode_select_page_8(spx, 4727 (struct mode_cache_scsi3 *)&buf[len], 4728 pllen, &pagelen, &rval, &dmod); 4729 /* 4730 * The pagelen value indicates the number of 4731 * parameter bytes already processed. 4732 * The rval is the return value from 4733 * sata_tran_start(). 4734 * The stat indicates the overall status of 4735 * the operation(s). 4736 */ 4737 if (stat != SATA_SUCCESS) 4738 /* 4739 * Page processing did not succeed - 4740 * all error info is already set-up, 4741 * just return 4742 */ 4743 pllen = 0; /* this breaks the loop */ 4744 else { 4745 len += pagelen; 4746 pllen -= pagelen; 4747 } 4748 break; 4749 4750 case MODEPAGE_INFO_EXCPT: 4751 stat = sata_mode_select_page_1c(spx, 4752 (struct mode_info_excpt_page *)&buf[len], 4753 pllen, &pagelen, &rval, &dmod); 4754 /* 4755 * The pagelen value indicates the number of 4756 * parameter bytes already processed. 4757 * The rval is the return value from 4758 * sata_tran_start(). 4759 * The stat indicates the overall status of 4760 * the operation(s). 4761 */ 4762 if (stat != SATA_SUCCESS) 4763 /* 4764 * Page processing did not succeed - 4765 * all error info is already set-up, 4766 * just return 4767 */ 4768 pllen = 0; /* this breaks the loop */ 4769 else { 4770 len += pagelen; 4771 pllen -= pagelen; 4772 } 4773 break; 4774 4775 case MODEPAGE_ACOUSTIC_MANAG: 4776 stat = sata_mode_select_page_30(spx, 4777 (struct mode_acoustic_management *) 4778 &buf[len], pllen, &pagelen, &rval, &dmod); 4779 /* 4780 * The pagelen value indicates the number of 4781 * parameter bytes already processed. 4782 * The rval is the return value from 4783 * sata_tran_start(). 4784 * The stat indicates the overall status of 4785 * the operation(s). 4786 */ 4787 if (stat != SATA_SUCCESS) 4788 /* 4789 * Page processing did not succeed - 4790 * all error info is already set-up, 4791 * just return 4792 */ 4793 pllen = 0; /* this breaks the loop */ 4794 else { 4795 len += pagelen; 4796 pllen -= pagelen; 4797 } 4798 4799 break; 4800 case MODEPAGE_POWER_COND: 4801 stat = sata_mode_select_page_1a(spx, 4802 (struct mode_info_power_cond *)&buf[len], 4803 pllen, &pagelen, &rval, &dmod); 4804 /* 4805 * The pagelen value indicates the number of 4806 * parameter bytes already processed. 4807 * The rval is the return value from 4808 * sata_tran_start(). 4809 * The stat indicates the overall status of 4810 * the operation(s). 4811 */ 4812 if (stat != SATA_SUCCESS) 4813 /* 4814 * Page processing did not succeed - 4815 * all error info is already set-up, 4816 * just return 4817 */ 4818 pllen = 0; /* this breaks the loop */ 4819 else { 4820 len += pagelen; 4821 pllen -= pagelen; 4822 } 4823 break; 4824 default: 4825 *scsipkt->pkt_scbp = STATUS_CHECK; 4826 sense = sata_arq_sense(spx); 4827 sense->es_key = KEY_ILLEGAL_REQUEST; 4828 sense->es_add_code = 4829 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 4830 goto done; 4831 } 4832 } 4833 } 4834 done: 4835 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4836 /* 4837 * If device parameters were modified, fetch and store the new 4838 * Identify Device data. Since port mutex could have been released 4839 * for accessing HBA driver, we need to re-check device existence. 4840 */ 4841 if (dmod != 0) { 4842 sata_drive_info_t new_sdinfo, *sdinfo; 4843 int rv = 0; 4844 4845 /* 4846 * Following statement has to be changed if this function is 4847 * used for devices other than SATA hard disks. 4848 */ 4849 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK; 4850 4851 new_sdinfo.satadrv_addr = 4852 spx->txlt_sata_pkt->satapkt_device.satadev_addr; 4853 rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst, 4854 &new_sdinfo); 4855 4856 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4857 /* 4858 * Since port mutex could have been released when 4859 * accessing HBA driver, we need to re-check that the 4860 * framework still holds the device info structure. 4861 */ 4862 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4863 &spx->txlt_sata_pkt->satapkt_device); 4864 if (sdinfo != NULL) { 4865 /* 4866 * Device still has info structure in the 4867 * sata framework. Copy newly fetched info 4868 */ 4869 if (rv == 0) { 4870 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 4871 sata_save_drive_settings(sdinfo); 4872 } else { 4873 /* 4874 * Could not fetch new data - invalidate 4875 * sata_drive_info. That makes device 4876 * unusable. 4877 */ 4878 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 4879 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 4880 } 4881 } 4882 if (rv != 0 || sdinfo == NULL) { 4883 /* 4884 * This changes the overall mode select completion 4885 * reason to a failed one !!!!! 4886 */ 4887 *scsipkt->pkt_scbp = STATUS_CHECK; 4888 sense = sata_arq_sense(spx); 4889 scsipkt->pkt_reason = CMD_INCOMPLETE; 4890 rval = TRAN_ACCEPT; 4891 } 4892 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4893 } 4894 /* Restore the scsi pkt flags */ 4895 scsipkt->pkt_flags &= ~FLAG_NOINTR; 4896 scsipkt->pkt_flags |= nointr_flag; 4897 4898 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4899 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4900 4901 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4902 scsipkt->pkt_comp != NULL) 4903 /* scsi callback required */ 4904 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4905 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4906 TQ_SLEEP) == NULL) 4907 /* Scheduling the callback failed */ 4908 return (TRAN_BUSY); 4909 4910 return (rval); 4911 } 4912 4913 4914 4915 /* 4916 * Translate command: Log Sense 4917 */ 4918 static int 4919 sata_txlt_log_sense(sata_pkt_txlate_t *spx) 4920 { 4921 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4922 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4923 sata_drive_info_t *sdinfo; 4924 struct scsi_extended_sense *sense; 4925 int len, count, alc_len; 4926 int pc; /* Page Control code */ 4927 int page_code; /* Page code */ 4928 uint8_t *buf; /* log sense buffer */ 4929 int rval, reason; 4930 #define MAX_LOG_SENSE_PAGE_SIZE 512 4931 4932 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4933 "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n", 4934 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 4935 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 4936 4937 buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP); 4938 4939 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4940 4941 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 4942 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4943 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4944 kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 4945 return (rval); 4946 } 4947 /* 4948 * If in interrupt context, reject this packet because it may result 4949 * in issuing a synchronous command to HBA. 4950 */ 4951 if (servicing_interrupt()) { 4952 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 4953 "sata_log_sense: rejecting command because " 4954 "of interrupt context\n", NULL); 4955 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4956 kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 4957 return (TRAN_BUSY); 4958 } 4959 4960 scsipkt->pkt_reason = CMD_CMPLT; 4961 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4962 STATE_SENT_CMD | STATE_GOT_STATUS; 4963 4964 pc = scsipkt->pkt_cdbp[2] >> 6; 4965 page_code = scsipkt->pkt_cdbp[2] & 0x3f; 4966 4967 /* Reject not supported request for all but cumulative values */ 4968 switch (pc) { 4969 case PC_CUMULATIVE_VALUES: 4970 break; 4971 default: 4972 *scsipkt->pkt_scbp = STATUS_CHECK; 4973 sense = sata_arq_sense(spx); 4974 sense->es_key = KEY_ILLEGAL_REQUEST; 4975 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4976 goto done; 4977 } 4978 4979 switch (page_code) { 4980 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 4981 case PAGE_CODE_SELF_TEST_RESULTS: 4982 case PAGE_CODE_INFORMATION_EXCEPTIONS: 4983 case PAGE_CODE_SMART_READ_DATA: 4984 case PAGE_CODE_START_STOP_CYCLE_COUNTER: 4985 break; 4986 default: 4987 *scsipkt->pkt_scbp = STATUS_CHECK; 4988 sense = sata_arq_sense(spx); 4989 sense->es_key = KEY_ILLEGAL_REQUEST; 4990 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4991 goto done; 4992 } 4993 4994 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4995 /* 4996 * Because log sense uses local buffers for data retrieval from 4997 * the devices and sets the data programatically in the 4998 * original specified buffer, release preallocated DMA 4999 * resources before storing data in the original buffer, 5000 * so no unwanted DMA sync would take place. 5001 */ 5002 sata_id_t *sata_id; 5003 5004 sata_scsi_dmafree(NULL, scsipkt); 5005 5006 len = 0; 5007 5008 /* Build log parameter header */ 5009 buf[len++] = page_code; /* page code as in the CDB */ 5010 buf[len++] = 0; /* reserved */ 5011 buf[len++] = 0; /* Zero out page length for now (MSB) */ 5012 buf[len++] = 0; /* (LSB) */ 5013 5014 sdinfo = sata_get_device_info( 5015 spx->txlt_sata_hba_inst, 5016 &spx->txlt_sata_pkt->satapkt_device); 5017 5018 /* 5019 * Add requested pages. 5020 */ 5021 switch (page_code) { 5022 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 5023 len = sata_build_lsense_page_0(sdinfo, buf + len); 5024 break; 5025 case PAGE_CODE_SELF_TEST_RESULTS: 5026 sata_id = &sdinfo->satadrv_id; 5027 if ((! (sata_id->ai_cmdset84 & 5028 SATA_SMART_SELF_TEST_SUPPORTED)) || 5029 (! (sata_id->ai_features87 & 5030 SATA_SMART_SELF_TEST_SUPPORTED))) { 5031 *scsipkt->pkt_scbp = STATUS_CHECK; 5032 sense = sata_arq_sense(spx); 5033 sense->es_key = KEY_ILLEGAL_REQUEST; 5034 sense->es_add_code = 5035 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5036 5037 goto done; 5038 } 5039 len = sata_build_lsense_page_10(sdinfo, buf + len, 5040 spx->txlt_sata_hba_inst); 5041 break; 5042 case PAGE_CODE_INFORMATION_EXCEPTIONS: 5043 sata_id = &sdinfo->satadrv_id; 5044 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 5045 *scsipkt->pkt_scbp = STATUS_CHECK; 5046 sense = sata_arq_sense(spx); 5047 sense->es_key = KEY_ILLEGAL_REQUEST; 5048 sense->es_add_code = 5049 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5050 5051 goto done; 5052 } 5053 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 5054 *scsipkt->pkt_scbp = STATUS_CHECK; 5055 sense = sata_arq_sense(spx); 5056 sense->es_key = KEY_ABORTED_COMMAND; 5057 sense->es_add_code = 5058 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 5059 sense->es_qual_code = 5060 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 5061 5062 goto done; 5063 } 5064 5065 len = sata_build_lsense_page_2f(sdinfo, buf + len, 5066 spx->txlt_sata_hba_inst); 5067 break; 5068 case PAGE_CODE_SMART_READ_DATA: 5069 sata_id = &sdinfo->satadrv_id; 5070 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 5071 *scsipkt->pkt_scbp = STATUS_CHECK; 5072 sense = sata_arq_sense(spx); 5073 sense->es_key = KEY_ILLEGAL_REQUEST; 5074 sense->es_add_code = 5075 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5076 5077 goto done; 5078 } 5079 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 5080 *scsipkt->pkt_scbp = STATUS_CHECK; 5081 sense = sata_arq_sense(spx); 5082 sense->es_key = KEY_ABORTED_COMMAND; 5083 sense->es_add_code = 5084 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 5085 sense->es_qual_code = 5086 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 5087 5088 goto done; 5089 } 5090 5091 /* This page doesn't include a page header */ 5092 len = sata_build_lsense_page_30(sdinfo, buf, 5093 spx->txlt_sata_hba_inst); 5094 goto no_header; 5095 case PAGE_CODE_START_STOP_CYCLE_COUNTER: 5096 sata_id = &sdinfo->satadrv_id; 5097 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 5098 *scsipkt->pkt_scbp = STATUS_CHECK; 5099 sense = sata_arq_sense(spx); 5100 sense->es_key = KEY_ILLEGAL_REQUEST; 5101 sense->es_add_code = 5102 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5103 5104 goto done; 5105 } 5106 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 5107 *scsipkt->pkt_scbp = STATUS_CHECK; 5108 sense = sata_arq_sense(spx); 5109 sense->es_key = KEY_ABORTED_COMMAND; 5110 sense->es_add_code = 5111 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 5112 sense->es_qual_code = 5113 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 5114 5115 goto done; 5116 } 5117 len = sata_build_lsense_page_0e(sdinfo, buf, spx); 5118 goto no_header; 5119 default: 5120 /* Invalid request */ 5121 *scsipkt->pkt_scbp = STATUS_CHECK; 5122 sense = sata_arq_sense(spx); 5123 sense->es_key = KEY_ILLEGAL_REQUEST; 5124 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5125 goto done; 5126 } 5127 5128 /* set parameter log sense data length */ 5129 buf[2] = len >> 8; /* log sense length (MSB) */ 5130 buf[3] = len & 0xff; /* log sense length (LSB) */ 5131 5132 len += SCSI_LOG_PAGE_HDR_LEN; 5133 ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE); 5134 5135 no_header: 5136 /* Check allocation length */ 5137 alc_len = scsipkt->pkt_cdbp[7]; 5138 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 5139 5140 /* 5141 * We do not check for possible parameters truncation 5142 * (alc_len < len) assuming that the target driver works 5143 * correctly. Just avoiding overrun. 5144 * Copy no more than requested and possible, buffer-wise. 5145 */ 5146 count = MIN(alc_len, len); 5147 count = MIN(bp->b_bcount, count); 5148 bcopy(buf, bp->b_un.b_addr, count); 5149 5150 scsipkt->pkt_state |= STATE_XFERRED_DATA; 5151 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 5152 } 5153 *scsipkt->pkt_scbp = STATUS_GOOD; 5154 done: 5155 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5156 (void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 5157 5158 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5159 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5160 5161 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5162 scsipkt->pkt_comp != NULL) 5163 /* scsi callback required */ 5164 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5165 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 5166 TQ_SLEEP) == NULL) 5167 /* Scheduling the callback failed */ 5168 return (TRAN_BUSY); 5169 5170 return (TRAN_ACCEPT); 5171 } 5172 5173 /* 5174 * Translate command: Log Select 5175 * Not implemented at this time - returns invalid command response. 5176 */ 5177 static int 5178 sata_txlt_log_select(sata_pkt_txlate_t *spx) 5179 { 5180 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5181 "sata_txlt_log_select\n", NULL); 5182 5183 return (sata_txlt_invalid_command(spx)); 5184 } 5185 5186 5187 /* 5188 * Translate command: Read (various types). 5189 * Translated into appropriate type of ATA READ command 5190 * for SATA hard disks. 5191 * Both the device capabilities and requested operation mode are 5192 * considered. 5193 * 5194 * Following scsi cdb fields are ignored: 5195 * rdprotect, dpo, fua, fua_nv, group_number. 5196 * 5197 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 5198 * enable variable sata_func_enable), the capability of the controller and 5199 * capability of a device are checked and if both support queueing, read 5200 * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT 5201 * command rather than plain READ_XXX command. 5202 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 5203 * both the controller and device suport such functionality, the read 5204 * request will be translated to READ_FPDMA_QUEUED command. 5205 * In both cases the maximum queue depth is derived as minimum of: 5206 * HBA capability,device capability and sata_max_queue_depth variable setting. 5207 * The value passed to HBA driver is decremented by 1, because only 5 bits are 5208 * used to pass max queue depth value, and the maximum possible queue depth 5209 * is 32. 5210 * 5211 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 5212 * appropriate values in scsi_pkt fields. 5213 */ 5214 static int 5215 sata_txlt_read(sata_pkt_txlate_t *spx) 5216 { 5217 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5218 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5219 sata_drive_info_t *sdinfo; 5220 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 5221 int cport = SATA_TXLT_CPORT(spx); 5222 uint16_t sec_count; 5223 uint64_t lba; 5224 int rval, reason; 5225 int synch; 5226 5227 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5228 5229 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 5230 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5231 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5232 return (rval); 5233 } 5234 5235 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 5236 &spx->txlt_sata_pkt->satapkt_device); 5237 5238 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 5239 /* 5240 * Extract LBA and sector count from scsi CDB. 5241 */ 5242 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 5243 case SCMD_READ: 5244 /* 6-byte scsi read cmd : 0x08 */ 5245 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 5246 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 5247 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5248 sec_count = scsipkt->pkt_cdbp[4]; 5249 /* sec_count 0 will be interpreted as 256 by a device */ 5250 break; 5251 case SCMD_READ_G1: 5252 /* 10-bytes scsi read command : 0x28 */ 5253 lba = scsipkt->pkt_cdbp[2]; 5254 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5255 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5256 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5257 sec_count = scsipkt->pkt_cdbp[7]; 5258 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 5259 break; 5260 case SCMD_READ_G5: 5261 /* 12-bytes scsi read command : 0xA8 */ 5262 lba = scsipkt->pkt_cdbp[2]; 5263 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5264 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5265 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5266 sec_count = scsipkt->pkt_cdbp[6]; 5267 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 5268 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 5269 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 5270 break; 5271 case SCMD_READ_G4: 5272 /* 16-bytes scsi read command : 0x88 */ 5273 lba = scsipkt->pkt_cdbp[2]; 5274 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5275 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5276 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5277 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 5278 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 5279 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 5280 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 5281 sec_count = scsipkt->pkt_cdbp[10]; 5282 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 5283 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 5284 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 5285 break; 5286 default: 5287 /* Unsupported command */ 5288 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5289 return (sata_txlt_invalid_command(spx)); 5290 } 5291 5292 /* 5293 * Check if specified address exceeds device capacity 5294 */ 5295 if ((lba >= sdinfo->satadrv_capacity) || 5296 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 5297 /* LBA out of range */ 5298 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5299 return (sata_txlt_lba_out_of_range(spx)); 5300 } 5301 5302 /* 5303 * For zero-length transfer, emulate good completion of the command 5304 * (reasons for rejecting the command were already checked). 5305 * No DMA resources were allocated. 5306 */ 5307 if (spx->txlt_dma_cookie_list == NULL) { 5308 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5309 return (sata_emul_rw_completion(spx)); 5310 } 5311 5312 /* 5313 * Build cmd block depending on the device capability and 5314 * requested operation mode. 5315 * Do not bother with non-dma mode - we are working only with 5316 * devices supporting DMA. 5317 */ 5318 scmd->satacmd_addr_type = ATA_ADDR_LBA; 5319 scmd->satacmd_device_reg = SATA_ADH_LBA; 5320 scmd->satacmd_cmd_reg = SATAC_READ_DMA; 5321 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 5322 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 5323 scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT; 5324 scmd->satacmd_sec_count_msb = sec_count >> 8; 5325 #ifndef __lock_lint 5326 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 5327 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 5328 scmd->satacmd_lba_high_msb = lba >> 40; 5329 #endif 5330 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 5331 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 5332 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 5333 } 5334 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 5335 scmd->satacmd_lba_low_lsb = lba & 0xff; 5336 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 5337 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 5338 scmd->satacmd_features_reg = 0; 5339 scmd->satacmd_status_reg = 0; 5340 scmd->satacmd_error_reg = 0; 5341 5342 /* 5343 * Check if queueing commands should be used and switch 5344 * to appropriate command if possible 5345 */ 5346 if (sata_func_enable & SATA_ENABLE_QUEUING) { 5347 boolean_t using_queuing; 5348 5349 /* Queuing supported by controller and device? */ 5350 if ((sata_func_enable & SATA_ENABLE_NCQ) && 5351 (sdinfo->satadrv_features_support & 5352 SATA_DEV_F_NCQ) && 5353 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 5354 SATA_CTLF_NCQ)) { 5355 using_queuing = B_TRUE; 5356 5357 /* NCQ supported - use FPDMA READ */ 5358 scmd->satacmd_cmd_reg = 5359 SATAC_READ_FPDMA_QUEUED; 5360 scmd->satacmd_features_reg_ext = 5361 scmd->satacmd_sec_count_msb; 5362 scmd->satacmd_sec_count_msb = 0; 5363 } else if ((sdinfo->satadrv_features_support & 5364 SATA_DEV_F_TCQ) && 5365 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 5366 SATA_CTLF_QCMD)) { 5367 using_queuing = B_TRUE; 5368 5369 /* Legacy queueing */ 5370 if (sdinfo->satadrv_features_support & 5371 SATA_DEV_F_LBA48) { 5372 scmd->satacmd_cmd_reg = 5373 SATAC_READ_DMA_QUEUED_EXT; 5374 scmd->satacmd_features_reg_ext = 5375 scmd->satacmd_sec_count_msb; 5376 scmd->satacmd_sec_count_msb = 0; 5377 } else { 5378 scmd->satacmd_cmd_reg = 5379 SATAC_READ_DMA_QUEUED; 5380 } 5381 } else /* NCQ nor legacy queuing not supported */ 5382 using_queuing = B_FALSE; 5383 5384 /* 5385 * If queuing, the sector count goes in the features register 5386 * and the secount count will contain the tag. 5387 */ 5388 if (using_queuing) { 5389 scmd->satacmd_features_reg = 5390 scmd->satacmd_sec_count_lsb; 5391 scmd->satacmd_sec_count_lsb = 0; 5392 scmd->satacmd_flags.sata_queued = B_TRUE; 5393 5394 /* Set-up maximum queue depth */ 5395 scmd->satacmd_flags.sata_max_queue_depth = 5396 sdinfo->satadrv_max_queue_depth - 1; 5397 } else if (sdinfo->satadrv_features_enabled & 5398 SATA_DEV_F_E_UNTAGGED_QING) { 5399 /* 5400 * Although NCQ/TCQ is not enabled, untagged queuing 5401 * may be still used. 5402 * Set-up the maximum untagged queue depth. 5403 * Use controller's queue depth from sata_hba_tran. 5404 * SATA HBA drivers may ignore this value and rely on 5405 * the internal limits.For drivers that do not 5406 * ignore untaged queue depth, limit the value to 5407 * SATA_MAX_QUEUE_DEPTH (32), as this is the 5408 * largest value that can be passed via 5409 * satacmd_flags.sata_max_queue_depth. 5410 */ 5411 scmd->satacmd_flags.sata_max_queue_depth = 5412 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ? 5413 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1; 5414 5415 } else { 5416 scmd->satacmd_flags.sata_max_queue_depth = 0; 5417 } 5418 } else 5419 scmd->satacmd_flags.sata_max_queue_depth = 0; 5420 5421 SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst, 5422 "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n", 5423 scmd->satacmd_cmd_reg, lba, sec_count); 5424 5425 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5426 /* Need callback function */ 5427 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 5428 synch = FALSE; 5429 } else 5430 synch = TRUE; 5431 5432 /* Transfer command to HBA */ 5433 if (sata_hba_start(spx, &rval) != 0) { 5434 /* Pkt not accepted for execution */ 5435 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5436 return (rval); 5437 } 5438 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5439 /* 5440 * If execution is non-synchronous, 5441 * a callback function will handle potential errors, translate 5442 * the response and will do a callback to a target driver. 5443 * If it was synchronous, check execution status using the same 5444 * framework callback. 5445 */ 5446 if (synch) { 5447 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5448 "synchronous execution status %x\n", 5449 spx->txlt_sata_pkt->satapkt_reason); 5450 sata_txlt_rw_completion(spx->txlt_sata_pkt); 5451 } 5452 return (TRAN_ACCEPT); 5453 } 5454 5455 5456 /* 5457 * SATA translate command: Write (various types) 5458 * Translated into appropriate type of ATA WRITE command 5459 * for SATA hard disks. 5460 * Both the device capabilities and requested operation mode are 5461 * considered. 5462 * 5463 * Following scsi cdb fields are ignored: 5464 * rwprotect, dpo, fua, fua_nv, group_number. 5465 * 5466 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 5467 * enable variable sata_func_enable), the capability of the controller and 5468 * capability of a device are checked and if both support queueing, write 5469 * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT 5470 * command rather than plain WRITE_XXX command. 5471 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 5472 * both the controller and device suport such functionality, the write 5473 * request will be translated to WRITE_FPDMA_QUEUED command. 5474 * In both cases the maximum queue depth is derived as minimum of: 5475 * HBA capability,device capability and sata_max_queue_depth variable setting. 5476 * The value passed to HBA driver is decremented by 1, because only 5 bits are 5477 * used to pass max queue depth value, and the maximum possible queue depth 5478 * is 32. 5479 * 5480 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 5481 * appropriate values in scsi_pkt fields. 5482 */ 5483 static int 5484 sata_txlt_write(sata_pkt_txlate_t *spx) 5485 { 5486 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5487 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5488 sata_drive_info_t *sdinfo; 5489 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 5490 int cport = SATA_TXLT_CPORT(spx); 5491 uint16_t sec_count; 5492 uint64_t lba; 5493 int rval, reason; 5494 int synch; 5495 5496 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5497 5498 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 5499 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5500 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5501 return (rval); 5502 } 5503 5504 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 5505 &spx->txlt_sata_pkt->satapkt_device); 5506 5507 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 5508 /* 5509 * Extract LBA and sector count from scsi CDB 5510 */ 5511 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 5512 case SCMD_WRITE: 5513 /* 6-byte scsi read cmd : 0x0A */ 5514 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 5515 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 5516 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5517 sec_count = scsipkt->pkt_cdbp[4]; 5518 /* sec_count 0 will be interpreted as 256 by a device */ 5519 break; 5520 case SCMD_WRITE_G1: 5521 /* 10-bytes scsi write command : 0x2A */ 5522 lba = scsipkt->pkt_cdbp[2]; 5523 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5524 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5525 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5526 sec_count = scsipkt->pkt_cdbp[7]; 5527 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 5528 break; 5529 case SCMD_WRITE_G5: 5530 /* 12-bytes scsi read command : 0xAA */ 5531 lba = scsipkt->pkt_cdbp[2]; 5532 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5533 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5534 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5535 sec_count = scsipkt->pkt_cdbp[6]; 5536 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 5537 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 5538 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 5539 break; 5540 case SCMD_WRITE_G4: 5541 /* 16-bytes scsi write command : 0x8A */ 5542 lba = scsipkt->pkt_cdbp[2]; 5543 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 5544 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 5545 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5546 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 5547 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 5548 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 5549 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 5550 sec_count = scsipkt->pkt_cdbp[10]; 5551 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 5552 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 5553 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 5554 break; 5555 default: 5556 /* Unsupported command */ 5557 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5558 return (sata_txlt_invalid_command(spx)); 5559 } 5560 5561 /* 5562 * Check if specified address and length exceeds device capacity 5563 */ 5564 if ((lba >= sdinfo->satadrv_capacity) || 5565 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 5566 /* LBA out of range */ 5567 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5568 return (sata_txlt_lba_out_of_range(spx)); 5569 } 5570 5571 /* 5572 * For zero-length transfer, emulate good completion of the command 5573 * (reasons for rejecting the command were already checked). 5574 * No DMA resources were allocated. 5575 */ 5576 if (spx->txlt_dma_cookie_list == NULL) { 5577 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5578 return (sata_emul_rw_completion(spx)); 5579 } 5580 5581 /* 5582 * Build cmd block depending on the device capability and 5583 * requested operation mode. 5584 * Do not bother with non-dma mode- we are working only with 5585 * devices supporting DMA. 5586 */ 5587 scmd->satacmd_addr_type = ATA_ADDR_LBA; 5588 scmd->satacmd_device_reg = SATA_ADH_LBA; 5589 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA; 5590 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 5591 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 5592 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT; 5593 scmd->satacmd_sec_count_msb = sec_count >> 8; 5594 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 5595 #ifndef __lock_lint 5596 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 5597 scmd->satacmd_lba_high_msb = lba >> 40; 5598 #endif 5599 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 5600 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 5601 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 5602 } 5603 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 5604 scmd->satacmd_lba_low_lsb = lba & 0xff; 5605 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 5606 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 5607 scmd->satacmd_features_reg = 0; 5608 scmd->satacmd_status_reg = 0; 5609 scmd->satacmd_error_reg = 0; 5610 5611 /* 5612 * Check if queueing commands should be used and switch 5613 * to appropriate command if possible 5614 */ 5615 if (sata_func_enable & SATA_ENABLE_QUEUING) { 5616 boolean_t using_queuing; 5617 5618 /* Queuing supported by controller and device? */ 5619 if ((sata_func_enable & SATA_ENABLE_NCQ) && 5620 (sdinfo->satadrv_features_support & 5621 SATA_DEV_F_NCQ) && 5622 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 5623 SATA_CTLF_NCQ)) { 5624 using_queuing = B_TRUE; 5625 5626 /* NCQ supported - use FPDMA WRITE */ 5627 scmd->satacmd_cmd_reg = 5628 SATAC_WRITE_FPDMA_QUEUED; 5629 scmd->satacmd_features_reg_ext = 5630 scmd->satacmd_sec_count_msb; 5631 scmd->satacmd_sec_count_msb = 0; 5632 } else if ((sdinfo->satadrv_features_support & 5633 SATA_DEV_F_TCQ) && 5634 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 5635 SATA_CTLF_QCMD)) { 5636 using_queuing = B_TRUE; 5637 5638 /* Legacy queueing */ 5639 if (sdinfo->satadrv_features_support & 5640 SATA_DEV_F_LBA48) { 5641 scmd->satacmd_cmd_reg = 5642 SATAC_WRITE_DMA_QUEUED_EXT; 5643 scmd->satacmd_features_reg_ext = 5644 scmd->satacmd_sec_count_msb; 5645 scmd->satacmd_sec_count_msb = 0; 5646 } else { 5647 scmd->satacmd_cmd_reg = 5648 SATAC_WRITE_DMA_QUEUED; 5649 } 5650 } else /* NCQ nor legacy queuing not supported */ 5651 using_queuing = B_FALSE; 5652 5653 if (using_queuing) { 5654 scmd->satacmd_features_reg = 5655 scmd->satacmd_sec_count_lsb; 5656 scmd->satacmd_sec_count_lsb = 0; 5657 scmd->satacmd_flags.sata_queued = B_TRUE; 5658 /* Set-up maximum queue depth */ 5659 scmd->satacmd_flags.sata_max_queue_depth = 5660 sdinfo->satadrv_max_queue_depth - 1; 5661 } else if (sdinfo->satadrv_features_enabled & 5662 SATA_DEV_F_E_UNTAGGED_QING) { 5663 /* 5664 * Although NCQ/TCQ is not enabled, untagged queuing 5665 * may be still used. 5666 * Set-up the maximum untagged queue depth. 5667 * Use controller's queue depth from sata_hba_tran. 5668 * SATA HBA drivers may ignore this value and rely on 5669 * the internal limits. For drivera that do not 5670 * ignore untaged queue depth, limit the value to 5671 * SATA_MAX_QUEUE_DEPTH (32), as this is the 5672 * largest value that can be passed via 5673 * satacmd_flags.sata_max_queue_depth. 5674 */ 5675 scmd->satacmd_flags.sata_max_queue_depth = 5676 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ? 5677 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1; 5678 5679 } else { 5680 scmd->satacmd_flags.sata_max_queue_depth = 0; 5681 } 5682 } else 5683 scmd->satacmd_flags.sata_max_queue_depth = 0; 5684 5685 SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5686 "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n", 5687 scmd->satacmd_cmd_reg, lba, sec_count); 5688 5689 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5690 /* Need callback function */ 5691 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 5692 synch = FALSE; 5693 } else 5694 synch = TRUE; 5695 5696 /* Transfer command to HBA */ 5697 if (sata_hba_start(spx, &rval) != 0) { 5698 /* Pkt not accepted for execution */ 5699 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5700 return (rval); 5701 } 5702 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5703 5704 /* 5705 * If execution is non-synchronous, 5706 * a callback function will handle potential errors, translate 5707 * the response and will do a callback to a target driver. 5708 * If it was synchronous, check execution status using the same 5709 * framework callback. 5710 */ 5711 if (synch) { 5712 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5713 "synchronous execution status %x\n", 5714 spx->txlt_sata_pkt->satapkt_reason); 5715 sata_txlt_rw_completion(spx->txlt_sata_pkt); 5716 } 5717 return (TRAN_ACCEPT); 5718 } 5719 5720 5721 /* 5722 * Implements SCSI SBC WRITE BUFFER command download microcode option 5723 */ 5724 static int 5725 sata_txlt_write_buffer(sata_pkt_txlate_t *spx) 5726 { 5727 #define WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE 4 5728 #define WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE 5 5729 5730 sata_hba_inst_t *sata_hba_inst = SATA_TXLT_HBA_INST(spx); 5731 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5732 struct sata_pkt *sata_pkt = spx->txlt_sata_pkt; 5733 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5734 5735 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5736 struct scsi_extended_sense *sense; 5737 int rval, mode, sector_count, reason; 5738 int cport = SATA_TXLT_CPORT(spx); 5739 5740 mode = scsipkt->pkt_cdbp[1] & 0x1f; 5741 5742 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5743 "sata_txlt_write_buffer, mode 0x%x\n", mode); 5744 5745 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5746 5747 if ((rval = sata_txlt_generic_pkt_info(spx, &reason)) != TRAN_ACCEPT) { 5748 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5749 return (rval); 5750 } 5751 /* 5752 * If in interrupt context, reject this packet because it would issue 5753 * a synchronous command to HBA. 5754 */ 5755 if (servicing_interrupt()) { 5756 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 5757 "sata_txlt_write_buffer: rejecting command because " 5758 "of interrupt context\n", NULL); 5759 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5760 return (TRAN_BUSY); 5761 } 5762 5763 /* Use synchronous mode */ 5764 spx->txlt_sata_pkt->satapkt_op_mode 5765 |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 5766 5767 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 5768 5769 scsipkt->pkt_reason = CMD_CMPLT; 5770 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5771 STATE_SENT_CMD | STATE_GOT_STATUS; 5772 5773 /* 5774 * The SCSI to ATA translation specification only calls 5775 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE. 5776 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but 5777 * ATA 8 (draft) got rid of download microcode for temp 5778 * and it is even optional for ATA 7, so it may be aborted. 5779 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as 5780 * it is not specified and the buffer offset for SCSI is a 16-bit 5781 * value in bytes, but for ATA it is a 16-bit offset in 512 byte 5782 * sectors. Thus the offset really doesn't buy us anything. 5783 * If and when ATA 8 is stabilized and the SCSI to ATA specification 5784 * is revised, this can be revisisted. 5785 */ 5786 /* Reject not supported request */ 5787 switch (mode) { 5788 case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE: 5789 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP; 5790 break; 5791 case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE: 5792 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE; 5793 break; 5794 default: 5795 goto bad_param; 5796 } 5797 5798 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 5799 5800 scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE; 5801 if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0) 5802 goto bad_param; 5803 sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE; 5804 scmd->satacmd_sec_count_lsb = (uint8_t)sector_count; 5805 scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8; 5806 scmd->satacmd_lba_mid_lsb = 0; 5807 scmd->satacmd_lba_high_lsb = 0; 5808 scmd->satacmd_device_reg = 0; 5809 spx->txlt_sata_pkt->satapkt_comp = NULL; 5810 scmd->satacmd_addr_type = 0; 5811 5812 /* Transfer command to HBA */ 5813 if (sata_hba_start(spx, &rval) != 0) { 5814 /* Pkt not accepted for execution */ 5815 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 5816 return (rval); 5817 } 5818 5819 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 5820 5821 /* Then we need synchronous check the status of the disk */ 5822 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5823 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 5824 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 5825 scsipkt->pkt_reason = CMD_CMPLT; 5826 5827 /* Download commmand succeed, so probe and identify device */ 5828 sata_reidentify_device(spx); 5829 } else { 5830 /* Something went wrong, microcode download command failed */ 5831 scsipkt->pkt_reason = CMD_INCOMPLETE; 5832 *scsipkt->pkt_scbp = STATUS_CHECK; 5833 sense = sata_arq_sense(spx); 5834 switch (sata_pkt->satapkt_reason) { 5835 case SATA_PKT_PORT_ERROR: 5836 /* 5837 * We have no device data. Assume no data transfered. 5838 */ 5839 sense->es_key = KEY_HARDWARE_ERROR; 5840 break; 5841 5842 case SATA_PKT_DEV_ERROR: 5843 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 5844 SATA_STATUS_ERR) { 5845 /* 5846 * determine dev error reason from error 5847 * reg content 5848 */ 5849 sata_decode_device_error(spx, sense); 5850 break; 5851 } 5852 /* No extended sense key - no info available */ 5853 break; 5854 5855 case SATA_PKT_TIMEOUT: 5856 scsipkt->pkt_reason = CMD_TIMEOUT; 5857 scsipkt->pkt_statistics |= 5858 STAT_TIMEOUT | STAT_DEV_RESET; 5859 /* No extended sense key ? */ 5860 break; 5861 5862 case SATA_PKT_ABORTED: 5863 scsipkt->pkt_reason = CMD_ABORTED; 5864 scsipkt->pkt_statistics |= STAT_ABORTED; 5865 /* No extended sense key ? */ 5866 break; 5867 5868 case SATA_PKT_RESET: 5869 /* pkt aborted by an explicit reset from a host */ 5870 scsipkt->pkt_reason = CMD_RESET; 5871 scsipkt->pkt_statistics |= STAT_DEV_RESET; 5872 break; 5873 5874 default: 5875 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 5876 "sata_txlt_nodata_cmd_completion: " 5877 "invalid packet completion reason %d", 5878 sata_pkt->satapkt_reason)); 5879 scsipkt->pkt_reason = CMD_TRAN_ERR; 5880 break; 5881 } 5882 5883 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5884 "scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5885 5886 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 5887 /* scsi callback required */ 5888 scsi_hba_pkt_comp(scsipkt); 5889 } 5890 return (TRAN_ACCEPT); 5891 5892 bad_param: 5893 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5894 *scsipkt->pkt_scbp = STATUS_CHECK; 5895 sense = sata_arq_sense(spx); 5896 sense->es_key = KEY_ILLEGAL_REQUEST; 5897 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5898 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5899 scsipkt->pkt_comp != NULL) { 5900 /* scsi callback required */ 5901 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5902 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 5903 TQ_SLEEP) == 0) { 5904 /* Scheduling the callback failed */ 5905 rval = TRAN_BUSY; 5906 } 5907 } 5908 return (rval); 5909 } 5910 5911 /* 5912 * Re-identify device after doing a firmware download. 5913 */ 5914 static void 5915 sata_reidentify_device(sata_pkt_txlate_t *spx) 5916 { 5917 #define DOWNLOAD_WAIT_TIME_SECS 60 5918 #define DOWNLOAD_WAIT_INTERVAL_SECS 1 5919 int rval; 5920 int retry_cnt; 5921 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5922 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 5923 sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device; 5924 sata_drive_info_t *sdinfo; 5925 5926 /* 5927 * Before returning good status, probe device. 5928 * Device probing will get IDENTIFY DEVICE data, if possible. 5929 * The assumption is that the new microcode is applied by the 5930 * device. It is a caller responsibility to verify this. 5931 */ 5932 for (retry_cnt = 0; 5933 retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS; 5934 retry_cnt++) { 5935 rval = sata_probe_device(sata_hba_inst, &sata_device); 5936 5937 if (rval == SATA_SUCCESS) { /* Set default features */ 5938 sdinfo = sata_get_device_info(sata_hba_inst, 5939 &sata_device); 5940 if (sata_initialize_device(sata_hba_inst, sdinfo) != 5941 SATA_SUCCESS) { 5942 /* retry */ 5943 rval = sata_initialize_device(sata_hba_inst, 5944 sdinfo); 5945 if (rval == SATA_RETRY) 5946 sata_log(sata_hba_inst, CE_WARN, 5947 "SATA device at port %d pmport %d -" 5948 " default device features could not" 5949 " be set. Device may not operate " 5950 "as expected.", 5951 sata_device.satadev_addr.cport, 5952 sata_device.satadev_addr.pmport); 5953 } 5954 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 5955 scsi_hba_pkt_comp(scsipkt); 5956 return; 5957 } else if (rval == SATA_RETRY) { 5958 delay(drv_usectohz(1000000 * 5959 DOWNLOAD_WAIT_INTERVAL_SECS)); 5960 continue; 5961 } else /* failed - no reason to retry */ 5962 break; 5963 } 5964 5965 /* 5966 * Something went wrong, device probing failed. 5967 */ 5968 SATA_LOG_D((sata_hba_inst, CE_WARN, 5969 "Cannot probe device after downloading microcode\n")); 5970 5971 /* Reset device to force retrying the probe. */ 5972 (void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 5973 (SATA_DIP(sata_hba_inst), &sata_device); 5974 5975 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 5976 scsi_hba_pkt_comp(scsipkt); 5977 } 5978 5979 5980 /* 5981 * Translate command: Synchronize Cache. 5982 * Translates into Flush Cache command for SATA hard disks. 5983 * 5984 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 5985 * appropriate values in scsi_pkt fields. 5986 */ 5987 static int 5988 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx) 5989 { 5990 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5991 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 5992 int cport = SATA_TXLT_CPORT(spx); 5993 int rval, reason; 5994 int synch; 5995 5996 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5997 5998 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 5999 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 6000 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 6001 return (rval); 6002 } 6003 6004 scmd->satacmd_addr_type = 0; 6005 scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE; 6006 scmd->satacmd_device_reg = 0; 6007 scmd->satacmd_sec_count_lsb = 0; 6008 scmd->satacmd_lba_low_lsb = 0; 6009 scmd->satacmd_lba_mid_lsb = 0; 6010 scmd->satacmd_lba_high_lsb = 0; 6011 scmd->satacmd_features_reg = 0; 6012 scmd->satacmd_status_reg = 0; 6013 scmd->satacmd_error_reg = 0; 6014 6015 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6016 "sata_txlt_synchronize_cache\n", NULL); 6017 6018 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 6019 /* Need to set-up a callback function */ 6020 spx->txlt_sata_pkt->satapkt_comp = 6021 sata_txlt_nodata_cmd_completion; 6022 synch = FALSE; 6023 } else 6024 synch = TRUE; 6025 6026 /* Transfer command to HBA */ 6027 if (sata_hba_start(spx, &rval) != 0) { 6028 /* Pkt not accepted for execution */ 6029 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 6030 return (rval); 6031 } 6032 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 6033 6034 /* 6035 * If execution non-synchronous, it had to be completed 6036 * a callback function will handle potential errors, translate 6037 * the response and will do a callback to a target driver. 6038 * If it was synchronous, check status, using the same 6039 * framework callback. 6040 */ 6041 if (synch) { 6042 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6043 "synchronous execution status %x\n", 6044 spx->txlt_sata_pkt->satapkt_reason); 6045 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 6046 } 6047 return (TRAN_ACCEPT); 6048 } 6049 6050 6051 /* 6052 * Send pkt to SATA HBA driver 6053 * 6054 * This function may be called only if the operation is requested by scsi_pkt, 6055 * i.e. scsi_pkt is not NULL. 6056 * 6057 * This function has to be called with cport mutex held. It does release 6058 * the mutex when it calls HBA driver sata_tran_start function and 6059 * re-acquires it afterwards. 6060 * 6061 * If return value is 0, pkt was accepted, -1 otherwise 6062 * rval is set to appropriate sata_scsi_start return value. 6063 * 6064 * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not 6065 * have called the sata_pkt callback function for this packet. 6066 * 6067 * The scsi callback has to be performed by the caller of this routine. 6068 */ 6069 static int 6070 sata_hba_start(sata_pkt_txlate_t *spx, int *rval) 6071 { 6072 int stat; 6073 uint8_t cport = SATA_TXLT_CPORT(spx); 6074 uint8_t pmport = SATA_TXLT_PMPORT(spx); 6075 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 6076 sata_drive_info_t *sdinfo; 6077 sata_pmult_info_t *pminfo; 6078 sata_pmport_info_t *pmportinfo = NULL; 6079 sata_device_t *sata_device = NULL; 6080 uint8_t cmd; 6081 struct sata_cmd_flags cmd_flags; 6082 6083 ASSERT(spx->txlt_sata_pkt != NULL); 6084 6085 ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6086 6087 sdinfo = sata_get_device_info(sata_hba_inst, 6088 &spx->txlt_sata_pkt->satapkt_device); 6089 ASSERT(sdinfo != NULL); 6090 6091 /* Clear device reset state? */ 6092 /* qual should be XXX_DPMPORT, but add XXX_PMPORT in case */ 6093 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT || 6094 sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) { 6095 6096 /* 6097 * Get the pmult_info of the its parent port multiplier, all 6098 * sub-devices share a common device reset flags on in 6099 * pmult_info. 6100 */ 6101 pminfo = SATA_PMULT_INFO(sata_hba_inst, cport); 6102 pmportinfo = pminfo->pmult_dev_port[pmport]; 6103 ASSERT(pminfo != NULL); 6104 if (pminfo->pmult_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) { 6105 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 6106 sata_clear_dev_reset = B_TRUE; 6107 pminfo->pmult_event_flags &= 6108 ~SATA_EVNT_CLEAR_DEVICE_RESET; 6109 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6110 "sata_hba_start: clearing device reset state" 6111 "on pmult.\n", NULL); 6112 } 6113 } else { 6114 if (sdinfo->satadrv_event_flags & 6115 SATA_EVNT_CLEAR_DEVICE_RESET) { 6116 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 6117 sata_clear_dev_reset = B_TRUE; 6118 sdinfo->satadrv_event_flags &= 6119 ~SATA_EVNT_CLEAR_DEVICE_RESET; 6120 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6121 "sata_hba_start: clearing device reset state\n", 6122 NULL); 6123 } 6124 } 6125 6126 cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg; 6127 cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags; 6128 sata_device = &spx->txlt_sata_pkt->satapkt_device; 6129 6130 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6131 6132 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6133 "Sata cmd 0x%2x\n", cmd); 6134 6135 stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 6136 spx->txlt_sata_pkt); 6137 6138 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6139 /* 6140 * If sata pkt was accepted and executed in asynchronous mode, i.e. 6141 * with the sata callback, the sata_pkt could be already destroyed 6142 * by the time we check ther return status from the hba_start() 6143 * function, because sata_scsi_destroy_pkt() could have been already 6144 * called (perhaps in the interrupt context). So, in such case, there 6145 * should be no references to it. In other cases, sata_pkt still 6146 * exists. 6147 */ 6148 if (stat == SATA_TRAN_ACCEPTED) { 6149 /* 6150 * pkt accepted for execution. 6151 * If it was executed synchronously, it is already completed 6152 * and pkt completion_reason indicates completion status. 6153 */ 6154 *rval = TRAN_ACCEPT; 6155 return (0); 6156 } 6157 6158 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 6159 switch (stat) { 6160 case SATA_TRAN_QUEUE_FULL: 6161 /* 6162 * Controller detected queue full condition. 6163 */ 6164 SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst, 6165 "sata_hba_start: queue full\n", NULL); 6166 6167 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 6168 *spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL; 6169 6170 *rval = TRAN_BUSY; 6171 break; 6172 6173 case SATA_TRAN_PORT_ERROR: 6174 /* 6175 * Communication/link with device or general port error 6176 * detected before pkt execution begun. 6177 */ 6178 if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 6179 SATA_ADDR_CPORT || 6180 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 6181 SATA_ADDR_DCPORT) 6182 sata_log(sata_hba_inst, CE_CONT, 6183 "SATA port %d error", 6184 sata_device->satadev_addr.cport); 6185 else 6186 sata_log(sata_hba_inst, CE_CONT, 6187 "SATA port %d:%d error\n", 6188 sata_device->satadev_addr.cport, 6189 sata_device->satadev_addr.pmport); 6190 6191 /* 6192 * Update the port/device structure. 6193 * sata_pkt should be still valid. Since port error is 6194 * returned, sata_device content should reflect port 6195 * state - it means, that sata address have been changed, 6196 * because original packet's sata address refered to a device 6197 * attached to some port. 6198 */ 6199 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT || 6200 sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) { 6201 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6202 mutex_enter(&pmportinfo->pmport_mutex); 6203 sata_update_pmport_info(sata_hba_inst, sata_device); 6204 mutex_exit(&pmportinfo->pmport_mutex); 6205 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6206 } else { 6207 sata_update_port_info(sata_hba_inst, sata_device); 6208 } 6209 6210 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 6211 *rval = TRAN_FATAL_ERROR; 6212 break; 6213 6214 case SATA_TRAN_CMD_UNSUPPORTED: 6215 /* 6216 * Command rejected by HBA as unsupported. It was HBA driver 6217 * that rejected the command, command was not sent to 6218 * an attached device. 6219 */ 6220 if ((sdinfo != NULL) && 6221 (sdinfo->satadrv_state & SATA_DSTATE_RESET)) 6222 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6223 "sat_hba_start: cmd 0x%2x rejected " 6224 "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd); 6225 6226 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6227 (void) sata_txlt_invalid_command(spx); 6228 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 6229 6230 *rval = TRAN_ACCEPT; 6231 break; 6232 6233 case SATA_TRAN_BUSY: 6234 /* 6235 * Command rejected by HBA because other operation prevents 6236 * accepting the packet, or device is in RESET condition. 6237 */ 6238 if (sdinfo != NULL) { 6239 sdinfo->satadrv_state = 6240 spx->txlt_sata_pkt->satapkt_device.satadev_state; 6241 6242 if (sdinfo->satadrv_state & SATA_DSTATE_RESET) { 6243 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6244 "sata_hba_start: cmd 0x%2x rejected " 6245 "because of device reset condition\n", 6246 cmd); 6247 } else { 6248 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 6249 "sata_hba_start: cmd 0x%2x rejected " 6250 "with SATA_TRAN_BUSY status\n", 6251 cmd); 6252 } 6253 } 6254 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 6255 *rval = TRAN_BUSY; 6256 break; 6257 6258 default: 6259 /* Unrecognized HBA response */ 6260 SATA_LOG_D((sata_hba_inst, CE_WARN, 6261 "sata_hba_start: unrecognized HBA response " 6262 "to cmd : 0x%2x resp 0x%x", cmd, rval)); 6263 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 6264 *rval = TRAN_FATAL_ERROR; 6265 break; 6266 } 6267 6268 /* 6269 * If we got here, the packet was rejected. 6270 * Check if we need to remember reset state clearing request 6271 */ 6272 if (cmd_flags.sata_clear_dev_reset) { 6273 /* 6274 * Check if device is still configured - it may have 6275 * disapeared from the configuration 6276 */ 6277 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 6278 if (sdinfo != NULL) { 6279 /* 6280 * Restore the flag that requests clearing of 6281 * the device reset state, 6282 * so the next sata packet may carry it to HBA. 6283 */ 6284 if (sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT || 6285 sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT) { 6286 pminfo->pmult_event_flags |= 6287 SATA_EVNT_CLEAR_DEVICE_RESET; 6288 } else { 6289 sdinfo->satadrv_event_flags |= 6290 SATA_EVNT_CLEAR_DEVICE_RESET; 6291 } 6292 } 6293 } 6294 return (-1); 6295 } 6296 6297 /* 6298 * Scsi response setup for invalid LBA 6299 * 6300 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 6301 */ 6302 static int 6303 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx) 6304 { 6305 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6306 struct scsi_extended_sense *sense; 6307 6308 scsipkt->pkt_reason = CMD_CMPLT; 6309 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6310 STATE_SENT_CMD | STATE_GOT_STATUS; 6311 *scsipkt->pkt_scbp = STATUS_CHECK; 6312 6313 *scsipkt->pkt_scbp = STATUS_CHECK; 6314 sense = sata_arq_sense(spx); 6315 sense->es_key = KEY_ILLEGAL_REQUEST; 6316 sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE; 6317 6318 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6319 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6320 6321 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6322 scsipkt->pkt_comp != NULL) 6323 /* scsi callback required */ 6324 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6325 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 6326 TQ_SLEEP) == NULL) 6327 /* Scheduling the callback failed */ 6328 return (TRAN_BUSY); 6329 return (TRAN_ACCEPT); 6330 } 6331 6332 6333 /* 6334 * Analyze device status and error registers and translate them into 6335 * appropriate scsi sense codes. 6336 * NOTE: non-packet commands only for now 6337 */ 6338 static void 6339 sata_decode_device_error(sata_pkt_txlate_t *spx, 6340 struct scsi_extended_sense *sense) 6341 { 6342 uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg; 6343 6344 ASSERT(sense != NULL); 6345 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 6346 SATA_STATUS_ERR); 6347 6348 6349 if (err_reg & SATA_ERROR_ICRC) { 6350 sense->es_key = KEY_ABORTED_COMMAND; 6351 sense->es_add_code = 0x08; /* Communication failure */ 6352 return; 6353 } 6354 6355 if (err_reg & SATA_ERROR_UNC) { 6356 sense->es_key = KEY_MEDIUM_ERROR; 6357 /* Information bytes (LBA) need to be set by a caller */ 6358 return; 6359 } 6360 6361 /* ADD HERE: MC error bit handling for ATAPI CD/DVD */ 6362 if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) { 6363 sense->es_key = KEY_UNIT_ATTENTION; 6364 sense->es_add_code = 0x3a; /* No media present */ 6365 return; 6366 } 6367 6368 if (err_reg & SATA_ERROR_IDNF) { 6369 if (err_reg & SATA_ERROR_ABORT) { 6370 sense->es_key = KEY_ABORTED_COMMAND; 6371 } else { 6372 sense->es_key = KEY_ILLEGAL_REQUEST; 6373 sense->es_add_code = 0x21; /* LBA out of range */ 6374 } 6375 return; 6376 } 6377 6378 if (err_reg & SATA_ERROR_ABORT) { 6379 ASSERT(spx->txlt_sata_pkt != NULL); 6380 sense->es_key = KEY_ABORTED_COMMAND; 6381 return; 6382 } 6383 } 6384 6385 /* 6386 * Extract error LBA from sata_pkt.satapkt_cmd register fields 6387 */ 6388 static void 6389 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba) 6390 { 6391 sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd; 6392 6393 *lba = 0; 6394 if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) { 6395 *lba = sata_cmd->satacmd_lba_high_msb; 6396 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb; 6397 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb; 6398 } else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) { 6399 *lba = sata_cmd->satacmd_device_reg & 0xf; 6400 } 6401 *lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb; 6402 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb; 6403 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb; 6404 } 6405 6406 /* 6407 * This is fixed sense format - if LBA exceeds the info field size, 6408 * no valid info will be returned (valid bit in extended sense will 6409 * be set to 0). 6410 */ 6411 static struct scsi_extended_sense * 6412 sata_arq_sense(sata_pkt_txlate_t *spx) 6413 { 6414 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6415 struct scsi_arq_status *arqs; 6416 struct scsi_extended_sense *sense; 6417 6418 /* Fill ARQ sense data */ 6419 scsipkt->pkt_state |= STATE_ARQ_DONE; 6420 arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp; 6421 *(uchar_t *)&arqs->sts_status = STATUS_CHECK; 6422 *(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD; 6423 arqs->sts_rqpkt_reason = CMD_CMPLT; 6424 arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6425 STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS; 6426 arqs->sts_rqpkt_resid = 0; 6427 sense = &arqs->sts_sensedata; 6428 bzero(sense, sizeof (struct scsi_extended_sense)); 6429 sata_fixed_sense_data_preset(sense); 6430 return (sense); 6431 } 6432 6433 6434 /* 6435 * Emulated SATA Read/Write command completion for zero-length requests. 6436 * This request always succedes, so in synchronous mode it always returns 6437 * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the 6438 * callback cannot be scheduled. 6439 */ 6440 static int 6441 sata_emul_rw_completion(sata_pkt_txlate_t *spx) 6442 { 6443 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6444 6445 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6446 STATE_SENT_CMD | STATE_GOT_STATUS; 6447 scsipkt->pkt_reason = CMD_CMPLT; 6448 *scsipkt->pkt_scbp = STATUS_GOOD; 6449 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 6450 /* scsi callback required - have to schedule it */ 6451 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6452 (task_func_t *)scsipkt->pkt_comp, 6453 (void *)scsipkt, TQ_SLEEP) == NULL) 6454 /* Scheduling the callback failed */ 6455 return (TRAN_BUSY); 6456 } 6457 return (TRAN_ACCEPT); 6458 } 6459 6460 6461 /* 6462 * Translate completion status of SATA read/write commands into scsi response. 6463 * pkt completion_reason is checked to determine the completion status. 6464 * Do scsi callback if necessary. 6465 * 6466 * Note: this function may be called also for synchronously executed 6467 * commands. 6468 * This function may be used only if scsi_pkt is non-NULL. 6469 */ 6470 static void 6471 sata_txlt_rw_completion(sata_pkt_t *sata_pkt) 6472 { 6473 sata_pkt_txlate_t *spx = 6474 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 6475 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 6476 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6477 struct scsi_extended_sense *sense; 6478 uint64_t lba; 6479 struct buf *bp; 6480 int rval; 6481 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 6482 /* Normal completion */ 6483 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6484 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 6485 scsipkt->pkt_reason = CMD_CMPLT; 6486 *scsipkt->pkt_scbp = STATUS_GOOD; 6487 if (spx->txlt_tmp_buf != NULL) { 6488 /* Temporary buffer was used */ 6489 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 6490 if (bp->b_flags & B_READ) { 6491 rval = ddi_dma_sync( 6492 spx->txlt_buf_dma_handle, 0, 0, 6493 DDI_DMA_SYNC_FORCPU); 6494 ASSERT(rval == DDI_SUCCESS); 6495 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 6496 bp->b_bcount); 6497 } 6498 } 6499 } else { 6500 /* 6501 * Something went wrong - analyze return 6502 */ 6503 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6504 STATE_SENT_CMD | STATE_GOT_STATUS; 6505 scsipkt->pkt_reason = CMD_INCOMPLETE; 6506 *scsipkt->pkt_scbp = STATUS_CHECK; 6507 sense = sata_arq_sense(spx); 6508 ASSERT(sense != NULL); 6509 6510 /* 6511 * SATA_PKT_DEV_ERROR is the only case where we may be able to 6512 * extract from device registers the failing LBA. 6513 */ 6514 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 6515 if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) && 6516 (scmd->satacmd_lba_mid_msb != 0 || 6517 scmd->satacmd_lba_high_msb != 0)) { 6518 /* 6519 * We have problem reporting this cmd LBA 6520 * in fixed sense data format, because of 6521 * the size of the scsi LBA fields. 6522 */ 6523 sense->es_valid = 0; 6524 } else { 6525 sata_extract_error_lba(spx, &lba); 6526 sense->es_info_1 = (lba & 0xFF000000) >> 24; 6527 sense->es_info_2 = (lba & 0xFF0000) >> 16; 6528 sense->es_info_3 = (lba & 0xFF00) >> 8; 6529 sense->es_info_4 = lba & 0xFF; 6530 } 6531 } else { 6532 /* Invalid extended sense info */ 6533 sense->es_valid = 0; 6534 } 6535 6536 switch (sata_pkt->satapkt_reason) { 6537 case SATA_PKT_PORT_ERROR: 6538 /* We may want to handle DEV GONE state as well */ 6539 /* 6540 * We have no device data. Assume no data transfered. 6541 */ 6542 sense->es_key = KEY_HARDWARE_ERROR; 6543 break; 6544 6545 case SATA_PKT_DEV_ERROR: 6546 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 6547 SATA_STATUS_ERR) { 6548 /* 6549 * determine dev error reason from error 6550 * reg content 6551 */ 6552 sata_decode_device_error(spx, sense); 6553 if (sense->es_key == KEY_MEDIUM_ERROR) { 6554 switch (scmd->satacmd_cmd_reg) { 6555 case SATAC_READ_DMA: 6556 case SATAC_READ_DMA_EXT: 6557 case SATAC_READ_DMA_QUEUED: 6558 case SATAC_READ_DMA_QUEUED_EXT: 6559 case SATAC_READ_FPDMA_QUEUED: 6560 /* Unrecovered read error */ 6561 sense->es_add_code = 6562 SD_SCSI_ASC_UNREC_READ_ERR; 6563 break; 6564 case SATAC_WRITE_DMA: 6565 case SATAC_WRITE_DMA_EXT: 6566 case SATAC_WRITE_DMA_QUEUED: 6567 case SATAC_WRITE_DMA_QUEUED_EXT: 6568 case SATAC_WRITE_FPDMA_QUEUED: 6569 /* Write error */ 6570 sense->es_add_code = 6571 SD_SCSI_ASC_WRITE_ERR; 6572 break; 6573 default: 6574 /* Internal error */ 6575 SATA_LOG_D(( 6576 spx->txlt_sata_hba_inst, 6577 CE_WARN, 6578 "sata_txlt_rw_completion :" 6579 "internal error - invalid " 6580 "command 0x%2x", 6581 scmd->satacmd_cmd_reg)); 6582 break; 6583 } 6584 } 6585 break; 6586 } 6587 /* No extended sense key - no info available */ 6588 scsipkt->pkt_reason = CMD_INCOMPLETE; 6589 break; 6590 6591 case SATA_PKT_TIMEOUT: 6592 scsipkt->pkt_reason = CMD_TIMEOUT; 6593 scsipkt->pkt_statistics |= 6594 STAT_TIMEOUT | STAT_DEV_RESET; 6595 sense->es_key = KEY_ABORTED_COMMAND; 6596 break; 6597 6598 case SATA_PKT_ABORTED: 6599 scsipkt->pkt_reason = CMD_ABORTED; 6600 scsipkt->pkt_statistics |= STAT_ABORTED; 6601 sense->es_key = KEY_ABORTED_COMMAND; 6602 break; 6603 6604 case SATA_PKT_RESET: 6605 scsipkt->pkt_reason = CMD_RESET; 6606 scsipkt->pkt_statistics |= STAT_DEV_RESET; 6607 sense->es_key = KEY_ABORTED_COMMAND; 6608 break; 6609 6610 default: 6611 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 6612 "sata_txlt_rw_completion: " 6613 "invalid packet completion reason")); 6614 scsipkt->pkt_reason = CMD_TRAN_ERR; 6615 break; 6616 } 6617 } 6618 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6619 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6620 6621 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 6622 /* scsi callback required */ 6623 scsi_hba_pkt_comp(scsipkt); 6624 } 6625 6626 6627 /* 6628 * Translate completion status of non-data commands (i.e. commands returning 6629 * no data). 6630 * pkt completion_reason is checked to determine the completion status. 6631 * Do scsi callback if necessary (FLAG_NOINTR == 0) 6632 * 6633 * Note: this function may be called also for synchronously executed 6634 * commands. 6635 * This function may be used only if scsi_pkt is non-NULL. 6636 */ 6637 6638 static void 6639 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt) 6640 { 6641 sata_pkt_txlate_t *spx = 6642 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 6643 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6644 6645 sata_set_arq_data(sata_pkt); 6646 6647 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 6648 /* scsi callback required */ 6649 scsi_hba_pkt_comp(scsipkt); 6650 } 6651 6652 static void 6653 sata_set_arq_data(sata_pkt_t *sata_pkt) 6654 { 6655 sata_pkt_txlate_t *spx = 6656 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 6657 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6658 struct scsi_extended_sense *sense; 6659 6660 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6661 STATE_SENT_CMD | STATE_GOT_STATUS; 6662 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 6663 /* Normal completion */ 6664 scsipkt->pkt_reason = CMD_CMPLT; 6665 *scsipkt->pkt_scbp = STATUS_GOOD; 6666 } else { 6667 /* Something went wrong */ 6668 scsipkt->pkt_reason = CMD_INCOMPLETE; 6669 *scsipkt->pkt_scbp = STATUS_CHECK; 6670 sense = sata_arq_sense(spx); 6671 switch (sata_pkt->satapkt_reason) { 6672 case SATA_PKT_PORT_ERROR: 6673 /* 6674 * We have no device data. Assume no data transfered. 6675 */ 6676 sense->es_key = KEY_HARDWARE_ERROR; 6677 break; 6678 6679 case SATA_PKT_DEV_ERROR: 6680 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 6681 SATA_STATUS_ERR) { 6682 /* 6683 * determine dev error reason from error 6684 * reg content 6685 */ 6686 sata_decode_device_error(spx, sense); 6687 break; 6688 } 6689 /* No extended sense key - no info available */ 6690 break; 6691 6692 case SATA_PKT_TIMEOUT: 6693 scsipkt->pkt_reason = CMD_TIMEOUT; 6694 scsipkt->pkt_statistics |= 6695 STAT_TIMEOUT | STAT_DEV_RESET; 6696 /* No extended sense key ? */ 6697 break; 6698 6699 case SATA_PKT_ABORTED: 6700 scsipkt->pkt_reason = CMD_ABORTED; 6701 scsipkt->pkt_statistics |= STAT_ABORTED; 6702 /* No extended sense key ? */ 6703 break; 6704 6705 case SATA_PKT_RESET: 6706 /* pkt aborted by an explicit reset from a host */ 6707 scsipkt->pkt_reason = CMD_RESET; 6708 scsipkt->pkt_statistics |= STAT_DEV_RESET; 6709 break; 6710 6711 default: 6712 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 6713 "sata_txlt_nodata_cmd_completion: " 6714 "invalid packet completion reason %d", 6715 sata_pkt->satapkt_reason)); 6716 scsipkt->pkt_reason = CMD_TRAN_ERR; 6717 break; 6718 } 6719 6720 } 6721 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6722 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6723 } 6724 6725 6726 /* 6727 * Build Mode sense R/W recovery page 6728 * NOT IMPLEMENTED 6729 */ 6730 6731 static int 6732 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6733 { 6734 #ifndef __lock_lint 6735 _NOTE(ARGUNUSED(sdinfo)) 6736 _NOTE(ARGUNUSED(pcntrl)) 6737 _NOTE(ARGUNUSED(buf)) 6738 #endif 6739 return (0); 6740 } 6741 6742 /* 6743 * Build Mode sense caching page - scsi-3 implementation. 6744 * Page length distinguishes previous format from scsi-3 format. 6745 * buf must have space for 0x12 bytes. 6746 * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable. 6747 * 6748 */ 6749 static int 6750 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6751 { 6752 struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf; 6753 sata_id_t *sata_id = &sdinfo->satadrv_id; 6754 6755 /* 6756 * Most of the fields are set to 0, being not supported and/or disabled 6757 */ 6758 bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3); 6759 6760 /* Saved paramters not supported */ 6761 if (pcntrl == 3) 6762 return (0); 6763 if (pcntrl == 0 || pcntrl == 2) { 6764 /* 6765 * For now treat current and default parameters as same 6766 * That may have to change, if target driver will complain 6767 */ 6768 page->mode_page.code = MODEPAGE_CACHING; /* PS = 0 */ 6769 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 6770 6771 if (SATA_READ_AHEAD_SUPPORTED(*sata_id) && 6772 !SATA_READ_AHEAD_ENABLED(*sata_id)) { 6773 page->dra = 1; /* Read Ahead disabled */ 6774 page->rcd = 1; /* Read Cache disabled */ 6775 } 6776 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) && 6777 SATA_WRITE_CACHE_ENABLED(*sata_id)) 6778 page->wce = 1; /* Write Cache enabled */ 6779 } else { 6780 /* Changeable parameters */ 6781 page->mode_page.code = MODEPAGE_CACHING; 6782 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 6783 if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) { 6784 page->dra = 1; 6785 page->rcd = 1; 6786 } 6787 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) 6788 page->wce = 1; 6789 } 6790 return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 6791 sizeof (struct mode_page)); 6792 } 6793 6794 /* 6795 * Build Mode sense exception cntrl page 6796 */ 6797 static int 6798 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6799 { 6800 struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf; 6801 sata_id_t *sata_id = &sdinfo->satadrv_id; 6802 6803 /* 6804 * Most of the fields are set to 0, being not supported and/or disabled 6805 */ 6806 bzero(buf, PAGELENGTH_INFO_EXCPT); 6807 6808 page->mode_page.code = MODEPAGE_INFO_EXCPT; 6809 page->mode_page.length = PAGELENGTH_INFO_EXCPT; 6810 6811 /* Indicate that this is page is saveable */ 6812 page->mode_page.ps = 1; 6813 6814 /* 6815 * We will return the same data for default, current and saved page. 6816 * The only changeable bit is dexcpt and that bit is required 6817 * by the ATA specification to be preserved across power cycles. 6818 */ 6819 if (pcntrl != 1) { 6820 page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED); 6821 page->mrie = MRIE_ONLY_ON_REQUEST; 6822 } 6823 else 6824 page->dexcpt = 1; /* Only changeable parameter */ 6825 6826 return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)); 6827 } 6828 6829 6830 static int 6831 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6832 { 6833 struct mode_acoustic_management *page = 6834 (struct mode_acoustic_management *)buf; 6835 sata_id_t *sata_id = &sdinfo->satadrv_id; 6836 6837 /* 6838 * Most of the fields are set to 0, being not supported and/or disabled 6839 */ 6840 bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT); 6841 6842 switch (pcntrl) { 6843 case P_CNTRL_DEFAULT: 6844 /* default paramters not supported */ 6845 return (0); 6846 6847 case P_CNTRL_CURRENT: 6848 case P_CNTRL_SAVED: 6849 /* Saved and current are supported and are identical */ 6850 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG; 6851 page->mode_page.length = 6852 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT; 6853 page->mode_page.ps = 1; 6854 6855 /* Word 83 indicates if feature is supported */ 6856 /* If feature is not supported */ 6857 if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) { 6858 page->acoustic_manag_enable = 6859 ACOUSTIC_DISABLED; 6860 } else { 6861 page->acoustic_manag_enable = 6862 ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT) 6863 != 0); 6864 /* Word 94 inidicates the value */ 6865 #ifdef _LITTLE_ENDIAN 6866 page->acoustic_manag_level = 6867 (uchar_t)sata_id->ai_acoustic; 6868 page->vendor_recommended_value = 6869 sata_id->ai_acoustic >> 8; 6870 #else 6871 page->acoustic_manag_level = 6872 sata_id->ai_acoustic >> 8; 6873 page->vendor_recommended_value = 6874 (uchar_t)sata_id->ai_acoustic; 6875 #endif 6876 } 6877 break; 6878 6879 case P_CNTRL_CHANGEABLE: 6880 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG; 6881 page->mode_page.length = 6882 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT; 6883 page->mode_page.ps = 1; 6884 6885 /* Word 83 indicates if the feature is supported */ 6886 if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) { 6887 page->acoustic_manag_enable = 6888 ACOUSTIC_ENABLED; 6889 page->acoustic_manag_level = 0xff; 6890 } 6891 break; 6892 } 6893 return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 6894 sizeof (struct mode_page)); 6895 } 6896 6897 6898 /* 6899 * Build Mode sense power condition page. 6900 */ 6901 static int 6902 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6903 { 6904 struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf; 6905 sata_id_t *sata_id = &sdinfo->satadrv_id; 6906 6907 /* 6908 * Most of the fields are set to 0, being not supported and/or disabled 6909 * power condition page length was 0x0a 6910 */ 6911 bzero(buf, sizeof (struct mode_info_power_cond)); 6912 6913 if (pcntrl == P_CNTRL_DEFAULT) { 6914 /* default paramters not supported */ 6915 return (0); 6916 } 6917 6918 page->mode_page.code = MODEPAGE_POWER_COND; 6919 page->mode_page.length = sizeof (struct mode_info_power_cond); 6920 6921 if (sata_id->ai_cap && SATA_STANDBYTIMER) { 6922 page->standby = 1; 6923 bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer, 6924 sizeof (uchar_t) * 4); 6925 } 6926 6927 return (sizeof (struct mode_info_power_cond)); 6928 } 6929 6930 /* 6931 * Process mode select caching page 8 (scsi3 format only). 6932 * Read Ahead (same as read cache) and Write Cache may be turned on and off 6933 * if these features are supported by the device. If these features are not 6934 * supported, the command will be terminated with STATUS_CHECK. 6935 * This function fails only if the SET FEATURE command sent to 6936 * the device fails. The page format is not varified, assuming that the 6937 * target driver operates correctly - if parameters length is too short, 6938 * we just drop the page. 6939 * Two command may be sent if both Read Cache/Read Ahead and Write Cache 6940 * setting have to be changed. 6941 * SET FEATURE command is executed synchronously, i.e. we wait here until 6942 * it is completed, regardless of the scsi pkt directives. 6943 * 6944 * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e. 6945 * changing DRA will change RCD. 6946 * 6947 * More than one SATA command may be executed to perform operations specified 6948 * by mode select pages. The first error terminates further execution. 6949 * Operations performed successully are not backed-up in such case. 6950 * 6951 * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 6952 * If operation resulted in changing device setup, dmod flag should be set to 6953 * one (1). If parameters were not changed, dmod flag should be set to 0. 6954 * Upon return, if operation required sending command to the device, the rval 6955 * should be set to the value returned by sata_hba_start. If operation 6956 * did not require device access, rval should be set to TRAN_ACCEPT. 6957 * The pagelen should be set to the length of the page. 6958 * 6959 * This function has to be called with a port mutex held. 6960 * 6961 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 6962 */ 6963 int 6964 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page, 6965 int parmlen, int *pagelen, int *rval, int *dmod) 6966 { 6967 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6968 sata_drive_info_t *sdinfo; 6969 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6970 sata_id_t *sata_id; 6971 struct scsi_extended_sense *sense; 6972 int wce, dra; /* Current settings */ 6973 6974 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 6975 &spx->txlt_sata_pkt->satapkt_device); 6976 sata_id = &sdinfo->satadrv_id; 6977 *dmod = 0; 6978 6979 /* Verify parameters length. If too short, drop it */ 6980 if ((PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 6981 sizeof (struct mode_page)) > parmlen) { 6982 *scsipkt->pkt_scbp = STATUS_CHECK; 6983 sense = sata_arq_sense(spx); 6984 sense->es_key = KEY_ILLEGAL_REQUEST; 6985 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 6986 *pagelen = parmlen; 6987 *rval = TRAN_ACCEPT; 6988 return (SATA_FAILURE); 6989 } 6990 6991 *pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page); 6992 6993 /* Current setting of Read Ahead (and Read Cache) */ 6994 if (SATA_READ_AHEAD_ENABLED(*sata_id)) 6995 dra = 0; /* 0 == not disabled */ 6996 else 6997 dra = 1; 6998 /* Current setting of Write Cache */ 6999 if (SATA_WRITE_CACHE_ENABLED(*sata_id)) 7000 wce = 1; 7001 else 7002 wce = 0; 7003 7004 if (page->dra == dra && page->wce == wce && page->rcd == dra) { 7005 /* nothing to do */ 7006 *rval = TRAN_ACCEPT; 7007 return (SATA_SUCCESS); 7008 } 7009 7010 /* 7011 * Need to flip some setting 7012 * Set-up Internal SET FEATURES command(s) 7013 */ 7014 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 7015 scmd->satacmd_addr_type = 0; 7016 scmd->satacmd_device_reg = 0; 7017 scmd->satacmd_status_reg = 0; 7018 scmd->satacmd_error_reg = 0; 7019 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 7020 if (page->dra != dra || page->rcd != dra) { 7021 if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) { 7022 /* Need to flip read ahead setting */ 7023 if (dra == 0) 7024 /* Disable read ahead / read cache */ 7025 scmd->satacmd_features_reg = 7026 SATAC_SF_DISABLE_READ_AHEAD; 7027 else 7028 /* Enable read ahead / read cache */ 7029 scmd->satacmd_features_reg = 7030 SATAC_SF_ENABLE_READ_AHEAD; 7031 7032 /* Transfer command to HBA */ 7033 if (sata_hba_start(spx, rval) != 0) 7034 /* 7035 * Pkt not accepted for execution. 7036 */ 7037 return (SATA_FAILURE); 7038 7039 *dmod = 1; 7040 7041 /* Now process return */ 7042 if (spx->txlt_sata_pkt->satapkt_reason != 7043 SATA_PKT_COMPLETED) { 7044 goto failure; /* Terminate */ 7045 } 7046 } else { 7047 *scsipkt->pkt_scbp = STATUS_CHECK; 7048 sense = sata_arq_sense(spx); 7049 sense->es_key = KEY_ILLEGAL_REQUEST; 7050 sense->es_add_code = 7051 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 7052 *pagelen = parmlen; 7053 *rval = TRAN_ACCEPT; 7054 return (SATA_FAILURE); 7055 } 7056 } 7057 7058 /* Note that the packet is not removed, so it could be re-used */ 7059 if (page->wce != wce) { 7060 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) { 7061 /* Need to flip Write Cache setting */ 7062 if (page->wce == 1) 7063 /* Enable write cache */ 7064 scmd->satacmd_features_reg = 7065 SATAC_SF_ENABLE_WRITE_CACHE; 7066 else 7067 /* Disable write cache */ 7068 scmd->satacmd_features_reg = 7069 SATAC_SF_DISABLE_WRITE_CACHE; 7070 7071 /* Transfer command to HBA */ 7072 if (sata_hba_start(spx, rval) != 0) 7073 /* 7074 * Pkt not accepted for execution. 7075 */ 7076 return (SATA_FAILURE); 7077 7078 *dmod = 1; 7079 7080 /* Now process return */ 7081 if (spx->txlt_sata_pkt->satapkt_reason != 7082 SATA_PKT_COMPLETED) { 7083 goto failure; 7084 } 7085 } else { 7086 *scsipkt->pkt_scbp = STATUS_CHECK; 7087 sense = sata_arq_sense(spx); 7088 sense->es_key = KEY_ILLEGAL_REQUEST; 7089 sense->es_add_code = 7090 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 7091 *pagelen = parmlen; 7092 *rval = TRAN_ACCEPT; 7093 return (SATA_FAILURE); 7094 } 7095 } 7096 return (SATA_SUCCESS); 7097 7098 failure: 7099 sata_xlate_errors(spx); 7100 7101 return (SATA_FAILURE); 7102 } 7103 7104 /* 7105 * Process mode select informational exceptions control page 0x1c 7106 * 7107 * The only changeable bit is dexcpt (disable exceptions). 7108 * MRIE (method of reporting informational exceptions) must be 7109 * "only on request". 7110 * This page applies to informational exceptions that report 7111 * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh 7112 * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_). 7113 * Informational exception conditions occur as the result of background scan 7114 * errors, background self-test errors, or vendor specific events within a 7115 * logical unit. An informational exception condition may occur asynchronous 7116 * to any commands. 7117 * 7118 * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 7119 * If operation resulted in changing device setup, dmod flag should be set to 7120 * one (1). If parameters were not changed, dmod flag should be set to 0. 7121 * Upon return, if operation required sending command to the device, the rval 7122 * should be set to the value returned by sata_hba_start. If operation 7123 * did not require device access, rval should be set to TRAN_ACCEPT. 7124 * The pagelen should be set to the length of the page. 7125 * 7126 * This function has to be called with a port mutex held. 7127 * 7128 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 7129 * 7130 * Cannot be called in the interrupt context. 7131 */ 7132 static int 7133 sata_mode_select_page_1c( 7134 sata_pkt_txlate_t *spx, 7135 struct mode_info_excpt_page *page, 7136 int parmlen, 7137 int *pagelen, 7138 int *rval, 7139 int *dmod) 7140 { 7141 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7142 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 7143 sata_drive_info_t *sdinfo; 7144 sata_id_t *sata_id; 7145 struct scsi_extended_sense *sense; 7146 7147 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 7148 &spx->txlt_sata_pkt->satapkt_device); 7149 sata_id = &sdinfo->satadrv_id; 7150 7151 *dmod = 0; 7152 7153 /* Verify parameters length. If too short, drop it */ 7154 if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) > parmlen) || 7155 page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) { 7156 *scsipkt->pkt_scbp = STATUS_CHECK; 7157 sense = sata_arq_sense(spx); 7158 sense->es_key = KEY_ILLEGAL_REQUEST; 7159 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 7160 *pagelen = parmlen; 7161 *rval = TRAN_ACCEPT; 7162 return (SATA_FAILURE); 7163 } 7164 7165 *pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page); 7166 7167 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 7168 *scsipkt->pkt_scbp = STATUS_CHECK; 7169 sense = sata_arq_sense(spx); 7170 sense->es_key = KEY_ILLEGAL_REQUEST; 7171 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 7172 *pagelen = parmlen; 7173 *rval = TRAN_ACCEPT; 7174 return (SATA_FAILURE); 7175 } 7176 7177 /* If already in the state requested, we are done */ 7178 if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 7179 /* nothing to do */ 7180 *rval = TRAN_ACCEPT; 7181 return (SATA_SUCCESS); 7182 } 7183 7184 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 7185 7186 /* Build SMART_ENABLE or SMART_DISABLE command */ 7187 scmd->satacmd_addr_type = 0; /* N/A */ 7188 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 7189 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 7190 scmd->satacmd_features_reg = page->dexcpt ? 7191 SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS; 7192 scmd->satacmd_device_reg = 0; /* Always device 0 */ 7193 scmd->satacmd_cmd_reg = SATAC_SMART; 7194 7195 /* Transfer command to HBA */ 7196 if (sata_hba_start(spx, rval) != 0) 7197 /* 7198 * Pkt not accepted for execution. 7199 */ 7200 return (SATA_FAILURE); 7201 7202 *dmod = 1; /* At least may have been modified */ 7203 7204 /* Now process return */ 7205 if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) 7206 return (SATA_SUCCESS); 7207 7208 /* Packet did not complete successfully */ 7209 sata_xlate_errors(spx); 7210 7211 return (SATA_FAILURE); 7212 } 7213 7214 /* 7215 * Process mode select acoustic management control page 0x30 7216 * 7217 * 7218 * This function has to be called with a port mutex held. 7219 * 7220 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 7221 * 7222 * Cannot be called in the interrupt context. 7223 */ 7224 int 7225 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct 7226 mode_acoustic_management *page, int parmlen, int *pagelen, 7227 int *rval, int *dmod) 7228 { 7229 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7230 sata_drive_info_t *sdinfo; 7231 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 7232 sata_id_t *sata_id; 7233 struct scsi_extended_sense *sense; 7234 7235 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 7236 &spx->txlt_sata_pkt->satapkt_device); 7237 sata_id = &sdinfo->satadrv_id; 7238 *dmod = 0; 7239 7240 /* If parmlen is too short or the feature is not supported, drop it */ 7241 if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 7242 sizeof (struct mode_page)) > parmlen) || 7243 (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) { 7244 *scsipkt->pkt_scbp = STATUS_CHECK; 7245 sense = sata_arq_sense(spx); 7246 sense->es_key = KEY_ILLEGAL_REQUEST; 7247 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 7248 *pagelen = parmlen; 7249 *rval = TRAN_ACCEPT; 7250 return (SATA_FAILURE); 7251 } 7252 7253 *pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 7254 sizeof (struct mode_page); 7255 7256 /* 7257 * We can enable and disable acoustice management and 7258 * set the acoustic management level. 7259 */ 7260 7261 /* 7262 * Set-up Internal SET FEATURES command(s) 7263 */ 7264 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 7265 scmd->satacmd_addr_type = 0; 7266 scmd->satacmd_device_reg = 0; 7267 scmd->satacmd_status_reg = 0; 7268 scmd->satacmd_error_reg = 0; 7269 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 7270 if (page->acoustic_manag_enable) { 7271 scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC; 7272 scmd->satacmd_sec_count_lsb = page->acoustic_manag_level; 7273 } else { /* disabling acoustic management */ 7274 scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC; 7275 } 7276 7277 /* Transfer command to HBA */ 7278 if (sata_hba_start(spx, rval) != 0) 7279 /* 7280 * Pkt not accepted for execution. 7281 */ 7282 return (SATA_FAILURE); 7283 7284 /* Now process return */ 7285 if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) { 7286 sata_xlate_errors(spx); 7287 return (SATA_FAILURE); 7288 } 7289 7290 *dmod = 1; 7291 7292 return (SATA_SUCCESS); 7293 } 7294 7295 /* 7296 * Process mode select power condition page 0x1a 7297 * 7298 * This function has to be called with a port mutex held. 7299 * 7300 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 7301 * 7302 * Cannot be called in the interrupt context. 7303 */ 7304 int 7305 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct 7306 mode_info_power_cond *page, int parmlen, int *pagelen, 7307 int *rval, int *dmod) 7308 { 7309 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7310 sata_drive_info_t *sdinfo; 7311 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 7312 sata_id_t *sata_id; 7313 struct scsi_extended_sense *sense; 7314 uint8_t ata_count; 7315 int i, len; 7316 7317 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 7318 &spx->txlt_sata_pkt->satapkt_device); 7319 sata_id = &sdinfo->satadrv_id; 7320 *dmod = 0; 7321 7322 len = sizeof (struct mode_info_power_cond); 7323 len += sizeof (struct mode_page); 7324 7325 /* If parmlen is too short or the feature is not supported, drop it */ 7326 if ((len < parmlen) || (page->idle == 1) || 7327 (!(sata_id->ai_cap && SATA_STANDBYTIMER) && page->standby == 1)) { 7328 *scsipkt->pkt_scbp = STATUS_CHECK; 7329 sense = sata_arq_sense(spx); 7330 sense->es_key = KEY_ILLEGAL_REQUEST; 7331 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 7332 *pagelen = parmlen; 7333 *rval = TRAN_ACCEPT; 7334 return (SATA_FAILURE); 7335 } 7336 7337 *pagelen = len; 7338 7339 /* 7340 * Set-up Internal STANDBY command(s) 7341 */ 7342 if (page->standby == 0) 7343 goto out; 7344 7345 ata_count = sata_get_standby_timer(page->standby_cond_timer); 7346 7347 scmd->satacmd_addr_type = 0; 7348 scmd->satacmd_sec_count_lsb = ata_count; 7349 scmd->satacmd_lba_low_lsb = 0; 7350 scmd->satacmd_lba_mid_lsb = 0; 7351 scmd->satacmd_lba_high_lsb = 0; 7352 scmd->satacmd_features_reg = 0; 7353 scmd->satacmd_device_reg = 0; 7354 scmd->satacmd_status_reg = 0; 7355 scmd->satacmd_cmd_reg = SATAC_STANDBY; 7356 scmd->satacmd_flags.sata_special_regs = B_TRUE; 7357 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 7358 7359 /* Transfer command to HBA */ 7360 if (sata_hba_start(spx, rval) != 0) { 7361 return (SATA_FAILURE); 7362 } else { 7363 if ((scmd->satacmd_error_reg != 0) || 7364 (spx->txlt_sata_pkt->satapkt_reason != 7365 SATA_PKT_COMPLETED)) { 7366 sata_xlate_errors(spx); 7367 return (SATA_FAILURE); 7368 } 7369 } 7370 7371 for (i = 0; i < 4; i++) { 7372 sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i]; 7373 } 7374 out: 7375 *dmod = 1; 7376 return (SATA_SUCCESS); 7377 } 7378 7379 /* 7380 * sata_build_lsense_page0() is used to create the 7381 * SCSI LOG SENSE page 0 (supported log pages) 7382 * 7383 * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e 7384 * (supported log pages, self-test results, informational exceptions 7385 * Sun vendor specific ATA SMART data, and start stop cycle counter). 7386 * 7387 * Takes a sata_drive_info t * and the address of a buffer 7388 * in which to create the page information. 7389 * 7390 * Returns the number of bytes valid in the buffer. 7391 */ 7392 static int 7393 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf) 7394 { 7395 struct log_parameter *lpp = (struct log_parameter *)buf; 7396 uint8_t *page_ptr = (uint8_t *)lpp->param_values; 7397 int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */ 7398 sata_id_t *sata_id = &sdinfo->satadrv_id; 7399 7400 lpp->param_code[0] = 0; 7401 lpp->param_code[1] = 0; 7402 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 7403 *page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES; 7404 7405 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 7406 if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) { 7407 *page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS; 7408 ++num_pages_supported; 7409 } 7410 *page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS; 7411 ++num_pages_supported; 7412 *page_ptr++ = PAGE_CODE_SMART_READ_DATA; 7413 ++num_pages_supported; 7414 *page_ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER; 7415 ++num_pages_supported; 7416 } 7417 7418 lpp->param_len = num_pages_supported; 7419 7420 return ((&lpp->param_values[0] - (uint8_t *)lpp) + 7421 num_pages_supported); 7422 } 7423 7424 /* 7425 * sata_build_lsense_page_10() is used to create the 7426 * SCSI LOG SENSE page 0x10 (self-test results) 7427 * 7428 * Takes a sata_drive_info t * and the address of a buffer 7429 * in which to create the page information as well as a sata_hba_inst_t *. 7430 * 7431 * Returns the number of bytes valid in the buffer. 7432 * 7433 * Note: Self test and SMART data is accessible in device log pages. 7434 * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors 7435 * of data can be transferred by a single command), or by the General Purpose 7436 * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors 7437 * - approximately 33MB - can be transferred by a single command. 7438 * The SCT Command response (either error or command) is the same for both 7439 * the SMART and GPL methods of issuing commands. 7440 * This function uses READ LOG EXT command when drive supports LBA48, and 7441 * SMART READ command otherwise. 7442 * 7443 * Since above commands are executed in a synchronous mode, this function 7444 * should not be called in an interrupt context. 7445 */ 7446 static int 7447 sata_build_lsense_page_10( 7448 sata_drive_info_t *sdinfo, 7449 uint8_t *buf, 7450 sata_hba_inst_t *sata_hba_inst) 7451 { 7452 struct log_parameter *lpp = (struct log_parameter *)buf; 7453 int rval; 7454 7455 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 7456 struct smart_ext_selftest_log *ext_selftest_log; 7457 7458 ext_selftest_log = kmem_zalloc( 7459 sizeof (struct smart_ext_selftest_log), KM_SLEEP); 7460 7461 rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo, 7462 ext_selftest_log, 0); 7463 if (rval == 0) { 7464 int index, start_index; 7465 struct smart_ext_selftest_log_entry *entry; 7466 static const struct smart_ext_selftest_log_entry empty = 7467 {0}; 7468 uint16_t block_num; 7469 int count; 7470 boolean_t only_one_block = B_FALSE; 7471 7472 index = ext_selftest_log-> 7473 smart_ext_selftest_log_index[0]; 7474 index |= ext_selftest_log-> 7475 smart_ext_selftest_log_index[1] << 8; 7476 if (index == 0) 7477 goto out; 7478 7479 --index; /* Correct for 0 origin */ 7480 start_index = index; /* remember where we started */ 7481 block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 7482 if (block_num != 0) { 7483 rval = sata_ext_smart_selftest_read_log( 7484 sata_hba_inst, sdinfo, ext_selftest_log, 7485 block_num); 7486 if (rval != 0) 7487 goto out; 7488 } 7489 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 7490 entry = 7491 &ext_selftest_log-> 7492 smart_ext_selftest_log_entries[index]; 7493 7494 for (count = 1; 7495 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 7496 ++count) { 7497 uint8_t status; 7498 uint8_t code; 7499 uint8_t sense_key; 7500 uint8_t add_sense_code; 7501 uint8_t add_sense_code_qual; 7502 7503 /* If this is an unused entry, we are done */ 7504 if (bcmp(entry, &empty, sizeof (empty)) == 0) { 7505 /* Broken firmware on some disks */ 7506 if (index + 1 == 7507 ENTRIES_PER_EXT_SELFTEST_LOG_BLK) { 7508 --entry; 7509 --index; 7510 if (bcmp(entry, &empty, 7511 sizeof (empty)) == 0) 7512 goto out; 7513 } else 7514 goto out; 7515 } 7516 7517 if (only_one_block && 7518 start_index == index) 7519 goto out; 7520 7521 lpp->param_code[0] = 0; 7522 lpp->param_code[1] = count; 7523 lpp->param_ctrl_flags = 7524 LOG_CTRL_LP | LOG_CTRL_LBIN; 7525 lpp->param_len = 7526 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 7527 7528 status = entry->smart_ext_selftest_log_status; 7529 status >>= 4; 7530 switch (status) { 7531 case 0: 7532 default: 7533 sense_key = KEY_NO_SENSE; 7534 add_sense_code = 7535 SD_SCSI_ASC_NO_ADD_SENSE; 7536 add_sense_code_qual = 0; 7537 break; 7538 case 1: 7539 sense_key = KEY_ABORTED_COMMAND; 7540 add_sense_code = 7541 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7542 add_sense_code_qual = SCSI_COMPONENT_81; 7543 break; 7544 case 2: 7545 sense_key = KEY_ABORTED_COMMAND; 7546 add_sense_code = 7547 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7548 add_sense_code_qual = SCSI_COMPONENT_82; 7549 break; 7550 case 3: 7551 sense_key = KEY_ABORTED_COMMAND; 7552 add_sense_code = 7553 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7554 add_sense_code_qual = SCSI_COMPONENT_83; 7555 break; 7556 case 4: 7557 sense_key = KEY_HARDWARE_ERROR; 7558 add_sense_code = 7559 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7560 add_sense_code_qual = SCSI_COMPONENT_84; 7561 break; 7562 case 5: 7563 sense_key = KEY_HARDWARE_ERROR; 7564 add_sense_code = 7565 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7566 add_sense_code_qual = SCSI_COMPONENT_85; 7567 break; 7568 case 6: 7569 sense_key = KEY_HARDWARE_ERROR; 7570 add_sense_code = 7571 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7572 add_sense_code_qual = SCSI_COMPONENT_86; 7573 break; 7574 case 7: 7575 sense_key = KEY_MEDIUM_ERROR; 7576 add_sense_code = 7577 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7578 add_sense_code_qual = SCSI_COMPONENT_87; 7579 break; 7580 case 8: 7581 sense_key = KEY_HARDWARE_ERROR; 7582 add_sense_code = 7583 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7584 add_sense_code_qual = SCSI_COMPONENT_88; 7585 break; 7586 } 7587 code = 0; /* unspecified */ 7588 status |= (code << 4); 7589 lpp->param_values[0] = status; 7590 lpp->param_values[1] = 0; /* unspecified */ 7591 lpp->param_values[2] = entry-> 7592 smart_ext_selftest_log_timestamp[1]; 7593 lpp->param_values[3] = entry-> 7594 smart_ext_selftest_log_timestamp[0]; 7595 if (status != 0) { 7596 lpp->param_values[4] = 0; 7597 lpp->param_values[5] = 0; 7598 lpp->param_values[6] = entry-> 7599 smart_ext_selftest_log_failing_lba 7600 [5]; 7601 lpp->param_values[7] = entry-> 7602 smart_ext_selftest_log_failing_lba 7603 [4]; 7604 lpp->param_values[8] = entry-> 7605 smart_ext_selftest_log_failing_lba 7606 [3]; 7607 lpp->param_values[9] = entry-> 7608 smart_ext_selftest_log_failing_lba 7609 [2]; 7610 lpp->param_values[10] = entry-> 7611 smart_ext_selftest_log_failing_lba 7612 [1]; 7613 lpp->param_values[11] = entry-> 7614 smart_ext_selftest_log_failing_lba 7615 [0]; 7616 } else { /* No bad block address */ 7617 lpp->param_values[4] = 0xff; 7618 lpp->param_values[5] = 0xff; 7619 lpp->param_values[6] = 0xff; 7620 lpp->param_values[7] = 0xff; 7621 lpp->param_values[8] = 0xff; 7622 lpp->param_values[9] = 0xff; 7623 lpp->param_values[10] = 0xff; 7624 lpp->param_values[11] = 0xff; 7625 } 7626 7627 lpp->param_values[12] = sense_key; 7628 lpp->param_values[13] = add_sense_code; 7629 lpp->param_values[14] = add_sense_code_qual; 7630 lpp->param_values[15] = 0; /* undefined */ 7631 7632 lpp = (struct log_parameter *) 7633 (((uint8_t *)lpp) + 7634 SCSI_LOG_PARAM_HDR_LEN + 7635 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 7636 7637 --index; /* Back up to previous entry */ 7638 if (index < 0) { 7639 if (block_num > 0) { 7640 --block_num; 7641 } else { 7642 struct read_log_ext_directory 7643 logdir; 7644 7645 rval = 7646 sata_read_log_ext_directory( 7647 sata_hba_inst, sdinfo, 7648 &logdir); 7649 if (rval == -1) 7650 goto out; 7651 if ((logdir.read_log_ext_vers 7652 [0] == 0) && 7653 (logdir.read_log_ext_vers 7654 [1] == 0)) 7655 goto out; 7656 block_num = 7657 logdir.read_log_ext_nblks 7658 [EXT_SMART_SELFTEST_LOG_PAGE 7659 - 1][0]; 7660 block_num |= logdir. 7661 read_log_ext_nblks 7662 [EXT_SMART_SELFTEST_LOG_PAGE 7663 - 1][1] << 8; 7664 --block_num; 7665 only_one_block = 7666 (block_num == 0); 7667 } 7668 rval = sata_ext_smart_selftest_read_log( 7669 sata_hba_inst, sdinfo, 7670 ext_selftest_log, block_num); 7671 if (rval != 0) 7672 goto out; 7673 7674 index = 7675 ENTRIES_PER_EXT_SELFTEST_LOG_BLK - 7676 1; 7677 } 7678 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 7679 entry = &ext_selftest_log-> 7680 smart_ext_selftest_log_entries[index]; 7681 } 7682 } 7683 out: 7684 kmem_free(ext_selftest_log, 7685 sizeof (struct smart_ext_selftest_log)); 7686 } else { 7687 struct smart_selftest_log *selftest_log; 7688 7689 selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log), 7690 KM_SLEEP); 7691 7692 rval = sata_smart_selftest_log(sata_hba_inst, sdinfo, 7693 selftest_log); 7694 7695 if (rval == 0) { 7696 int index; 7697 int count; 7698 struct smart_selftest_log_entry *entry; 7699 static const struct smart_selftest_log_entry empty = 7700 { 0 }; 7701 7702 index = selftest_log->smart_selftest_log_index; 7703 if (index == 0) 7704 goto done; 7705 --index; /* Correct for 0 origin */ 7706 entry = &selftest_log-> 7707 smart_selftest_log_entries[index]; 7708 for (count = 1; 7709 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 7710 ++count) { 7711 uint8_t status; 7712 uint8_t code; 7713 uint8_t sense_key; 7714 uint8_t add_sense_code; 7715 uint8_t add_sense_code_qual; 7716 7717 if (bcmp(entry, &empty, sizeof (empty)) == 0) 7718 goto done; 7719 7720 lpp->param_code[0] = 0; 7721 lpp->param_code[1] = count; 7722 lpp->param_ctrl_flags = 7723 LOG_CTRL_LP | LOG_CTRL_LBIN; 7724 lpp->param_len = 7725 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 7726 7727 status = entry->smart_selftest_log_status; 7728 status >>= 4; 7729 switch (status) { 7730 case 0: 7731 default: 7732 sense_key = KEY_NO_SENSE; 7733 add_sense_code = 7734 SD_SCSI_ASC_NO_ADD_SENSE; 7735 break; 7736 case 1: 7737 sense_key = KEY_ABORTED_COMMAND; 7738 add_sense_code = 7739 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7740 add_sense_code_qual = SCSI_COMPONENT_81; 7741 break; 7742 case 2: 7743 sense_key = KEY_ABORTED_COMMAND; 7744 add_sense_code = 7745 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7746 add_sense_code_qual = SCSI_COMPONENT_82; 7747 break; 7748 case 3: 7749 sense_key = KEY_ABORTED_COMMAND; 7750 add_sense_code = 7751 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7752 add_sense_code_qual = SCSI_COMPONENT_83; 7753 break; 7754 case 4: 7755 sense_key = KEY_HARDWARE_ERROR; 7756 add_sense_code = 7757 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7758 add_sense_code_qual = SCSI_COMPONENT_84; 7759 break; 7760 case 5: 7761 sense_key = KEY_HARDWARE_ERROR; 7762 add_sense_code = 7763 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7764 add_sense_code_qual = SCSI_COMPONENT_85; 7765 break; 7766 case 6: 7767 sense_key = KEY_HARDWARE_ERROR; 7768 add_sense_code = 7769 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7770 add_sense_code_qual = SCSI_COMPONENT_86; 7771 break; 7772 case 7: 7773 sense_key = KEY_MEDIUM_ERROR; 7774 add_sense_code = 7775 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7776 add_sense_code_qual = SCSI_COMPONENT_87; 7777 break; 7778 case 8: 7779 sense_key = KEY_HARDWARE_ERROR; 7780 add_sense_code = 7781 DIAGNOSTIC_FAILURE_ON_COMPONENT; 7782 add_sense_code_qual = SCSI_COMPONENT_88; 7783 break; 7784 } 7785 code = 0; /* unspecified */ 7786 status |= (code << 4); 7787 lpp->param_values[0] = status; 7788 lpp->param_values[1] = 0; /* unspecified */ 7789 lpp->param_values[2] = entry-> 7790 smart_selftest_log_timestamp[1]; 7791 lpp->param_values[3] = entry-> 7792 smart_selftest_log_timestamp[0]; 7793 if (status != 0) { 7794 lpp->param_values[4] = 0; 7795 lpp->param_values[5] = 0; 7796 lpp->param_values[6] = 0; 7797 lpp->param_values[7] = 0; 7798 lpp->param_values[8] = entry-> 7799 smart_selftest_log_failing_lba[3]; 7800 lpp->param_values[9] = entry-> 7801 smart_selftest_log_failing_lba[2]; 7802 lpp->param_values[10] = entry-> 7803 smart_selftest_log_failing_lba[1]; 7804 lpp->param_values[11] = entry-> 7805 smart_selftest_log_failing_lba[0]; 7806 } else { /* No block address */ 7807 lpp->param_values[4] = 0xff; 7808 lpp->param_values[5] = 0xff; 7809 lpp->param_values[6] = 0xff; 7810 lpp->param_values[7] = 0xff; 7811 lpp->param_values[8] = 0xff; 7812 lpp->param_values[9] = 0xff; 7813 lpp->param_values[10] = 0xff; 7814 lpp->param_values[11] = 0xff; 7815 } 7816 lpp->param_values[12] = sense_key; 7817 lpp->param_values[13] = add_sense_code; 7818 lpp->param_values[14] = add_sense_code_qual; 7819 lpp->param_values[15] = 0; /* undefined */ 7820 7821 lpp = (struct log_parameter *) 7822 (((uint8_t *)lpp) + 7823 SCSI_LOG_PARAM_HDR_LEN + 7824 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 7825 --index; /* back up to previous entry */ 7826 if (index < 0) { 7827 index = 7828 NUM_SMART_SELFTEST_LOG_ENTRIES - 1; 7829 } 7830 entry = &selftest_log-> 7831 smart_selftest_log_entries[index]; 7832 } 7833 } 7834 done: 7835 kmem_free(selftest_log, sizeof (struct smart_selftest_log)); 7836 } 7837 7838 return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) * 7839 SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS); 7840 } 7841 7842 /* 7843 * sata_build_lsense_page_2f() is used to create the 7844 * SCSI LOG SENSE page 0x2f (informational exceptions) 7845 * 7846 * Takes a sata_drive_info t * and the address of a buffer 7847 * in which to create the page information as well as a sata_hba_inst_t *. 7848 * 7849 * Returns the number of bytes valid in the buffer. 7850 * 7851 * Because it invokes function(s) that send synchronously executed command 7852 * to the HBA, it cannot be called in the interrupt context. 7853 */ 7854 static int 7855 sata_build_lsense_page_2f( 7856 sata_drive_info_t *sdinfo, 7857 uint8_t *buf, 7858 sata_hba_inst_t *sata_hba_inst) 7859 { 7860 struct log_parameter *lpp = (struct log_parameter *)buf; 7861 int rval; 7862 uint8_t *smart_data; 7863 uint8_t temp; 7864 sata_id_t *sata_id; 7865 #define SMART_NO_TEMP 0xff 7866 7867 lpp->param_code[0] = 0; 7868 lpp->param_code[1] = 0; 7869 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 7870 7871 /* Now get the SMART status w.r.t. threshold exceeded */ 7872 rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo); 7873 switch (rval) { 7874 case 1: 7875 lpp->param_values[0] = SCSI_PREDICTED_FAILURE; 7876 lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE; 7877 break; 7878 case 0: 7879 case -1: /* failed to get data */ 7880 lpp->param_values[0] = 0; /* No failure predicted */ 7881 lpp->param_values[1] = 0; 7882 break; 7883 #if defined(SATA_DEBUG) 7884 default: 7885 cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value"); 7886 /* NOTREACHED */ 7887 #endif 7888 } 7889 7890 sata_id = &sdinfo->satadrv_id; 7891 if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP)) 7892 temp = SMART_NO_TEMP; 7893 else { 7894 /* Now get the temperature */ 7895 smart_data = kmem_zalloc(512, KM_SLEEP); 7896 rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data, 7897 SCT_STATUS_LOG_PAGE, 1); 7898 if (rval == -1) 7899 temp = SMART_NO_TEMP; 7900 else { 7901 temp = smart_data[200]; 7902 if (temp & 0x80) { 7903 if (temp & 0x7f) 7904 temp = 0; 7905 else 7906 temp = SMART_NO_TEMP; 7907 } 7908 } 7909 kmem_free(smart_data, 512); 7910 } 7911 7912 lpp->param_values[2] = temp; /* most recent temperature */ 7913 lpp->param_values[3] = 0; /* required vendor specific byte */ 7914 7915 lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN; 7916 7917 7918 return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN); 7919 } 7920 7921 /* 7922 * sata_build_lsense_page_30() is used to create the 7923 * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data). 7924 * 7925 * Takes a sata_drive_info t * and the address of a buffer 7926 * in which to create the page information as well as a sata_hba_inst_t *. 7927 * 7928 * Returns the number of bytes valid in the buffer. 7929 */ 7930 static int 7931 sata_build_lsense_page_30( 7932 sata_drive_info_t *sdinfo, 7933 uint8_t *buf, 7934 sata_hba_inst_t *sata_hba_inst) 7935 { 7936 struct smart_data *smart_data = (struct smart_data *)buf; 7937 int rval; 7938 7939 /* Now do the SMART READ DATA */ 7940 rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data); 7941 if (rval == -1) 7942 return (0); 7943 7944 return (sizeof (struct smart_data)); 7945 } 7946 7947 /* 7948 * sata_build_lsense_page_0e() is used to create the 7949 * SCSI LOG SENSE page 0e (start-stop cycle counter page) 7950 * 7951 * Date of Manufacture (0x0001) 7952 * YEAR = "0000" 7953 * WEEK = "00" 7954 * Accounting Date (0x0002) 7955 * 6 ASCII space character(20h) 7956 * Specified cycle count over device lifetime 7957 * VALUE - THRESH - the delta between max and min; 7958 * Accumulated start-stop cycles 7959 * VALUE - WORST - the accumulated cycles; 7960 * 7961 * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute 7962 * 7963 * Takes a sata_drive_info t * and the address of a buffer 7964 * in which to create the page information as well as a sata_hba_inst_t *. 7965 * 7966 * Returns the number of bytes valid in the buffer. 7967 */ 7968 static int 7969 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf, 7970 sata_pkt_txlate_t *spx) 7971 { 7972 struct start_stop_cycle_counter_log *log_page; 7973 int i, rval, index; 7974 uint8_t smart_data[512], id, value, worst, thresh; 7975 uint32_t max_count, cycles; 7976 7977 /* Now do the SMART READ DATA */ 7978 rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo, 7979 (struct smart_data *)smart_data); 7980 if (rval == -1) 7981 return (0); 7982 for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) { 7983 index = (i * 12) + 2; 7984 id = smart_data[index]; 7985 if (id != SMART_START_STOP_COUNT_ID) 7986 continue; 7987 else { 7988 thresh = smart_data[index + 2]; 7989 value = smart_data[index + 3]; 7990 worst = smart_data[index + 4]; 7991 break; 7992 } 7993 } 7994 if (id != SMART_START_STOP_COUNT_ID) 7995 return (0); 7996 max_count = value - thresh; 7997 cycles = value - worst; 7998 7999 log_page = (struct start_stop_cycle_counter_log *)buf; 8000 bzero(log_page, sizeof (struct start_stop_cycle_counter_log)); 8001 log_page->code = 0x0e; 8002 log_page->page_len_low = 0x24; 8003 8004 log_page->manufactor_date_low = 0x1; 8005 log_page->param_1.fmt_link = 0x1; /* 01b */ 8006 log_page->param_len_1 = 0x06; 8007 for (i = 0; i < 4; i++) { 8008 log_page->year_manu[i] = 0x30; 8009 if (i < 2) 8010 log_page->week_manu[i] = 0x30; 8011 } 8012 8013 log_page->account_date_low = 0x02; 8014 log_page->param_2.fmt_link = 0x01; /* 01b */ 8015 log_page->param_len_2 = 0x06; 8016 for (i = 0; i < 4; i++) { 8017 log_page->year_account[i] = 0x20; 8018 if (i < 2) 8019 log_page->week_account[i] = 0x20; 8020 } 8021 8022 log_page->lifetime_code_low = 0x03; 8023 log_page->param_3.fmt_link = 0x03; /* 11b */ 8024 log_page->param_len_3 = 0x04; 8025 /* VALUE - THRESH - the delta between max and min */ 8026 log_page->cycle_code_low = 0x04; 8027 log_page->param_4.fmt_link = 0x03; /* 11b */ 8028 log_page->param_len_4 = 0x04; 8029 /* WORST - THRESH - the distance from 'now' to min */ 8030 8031 for (i = 0; i < 4; i++) { 8032 log_page->cycle_lifetime[i] = 8033 (max_count >> (8 * (3 - i))) & 0xff; 8034 log_page->cycle_accumulated[i] = 8035 (cycles >> (8 * (3 - i))) & 0xff; 8036 } 8037 8038 return (sizeof (struct start_stop_cycle_counter_log)); 8039 } 8040 8041 /* 8042 * This function was used for build a ATA read verify sector command 8043 */ 8044 static void 8045 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba) 8046 { 8047 scmd->satacmd_cmd_reg = SATAC_RDVER; 8048 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 8049 scmd->satacmd_flags.sata_special_regs = B_TRUE; 8050 8051 scmd->satacmd_sec_count_lsb = sec & 0xff; 8052 scmd->satacmd_lba_low_lsb = lba & 0xff; 8053 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 8054 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 8055 scmd->satacmd_device_reg = (SATA_ADH_LBA | (lba >> 24) & 0xf); 8056 scmd->satacmd_features_reg = 0; 8057 scmd->satacmd_status_reg = 0; 8058 scmd->satacmd_error_reg = 0; 8059 } 8060 8061 /* 8062 * This function was used for building an ATA 8063 * command, and only command register need to 8064 * be defined, other register will be zero or na. 8065 */ 8066 static void 8067 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd) 8068 { 8069 scmd->satacmd_addr_type = 0; 8070 scmd->satacmd_cmd_reg = cmd; 8071 scmd->satacmd_device_reg = 0; 8072 scmd->satacmd_sec_count_lsb = 0; 8073 scmd->satacmd_lba_low_lsb = 0; 8074 scmd->satacmd_lba_mid_lsb = 0; 8075 scmd->satacmd_lba_high_lsb = 0; 8076 scmd->satacmd_features_reg = 0; 8077 scmd->satacmd_status_reg = 0; 8078 scmd->satacmd_error_reg = 0; 8079 scmd->satacmd_flags.sata_special_regs = B_TRUE; 8080 } 8081 8082 /* 8083 * This function was used for changing the standby 8084 * timer format from SCSI to ATA. 8085 */ 8086 static uint8_t 8087 sata_get_standby_timer(uint8_t *timer) 8088 { 8089 uint32_t i = 0, count = 0; 8090 uint8_t ata_count; 8091 8092 for (i = 0; i < 4; i++) { 8093 count = count << 8 | timer[i]; 8094 } 8095 8096 if (count == 0) 8097 return (0); 8098 8099 if (count >= 1 && count <= 12000) 8100 ata_count = (count -1) / 50 + 1; 8101 else if (count > 12000 && count <= 12600) 8102 ata_count = 0xfc; 8103 else if (count > 12601 && count <= 12750) 8104 ata_count = 0xff; 8105 else if (count > 12750 && count <= 17999) 8106 ata_count = 0xf1; 8107 else if (count > 18000 && count <= 198000) 8108 ata_count = count / 18000 + 240; 8109 else 8110 ata_count = 0xfd; 8111 return (ata_count); 8112 } 8113 8114 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */ 8115 8116 /* 8117 * Start command for ATAPI device. 8118 * This function processes scsi_pkt requests. 8119 * Now CD/DVD, tape and ATAPI disk devices are supported. 8120 * Most commands are packet without any translation into Packet Command. 8121 * Some may be trapped and executed as SATA commands (not clear which one). 8122 * 8123 * Returns TRAN_ACCEPT if command is accepted for execution (or completed 8124 * execution). 8125 * Returns other TRAN_XXXX codes if command is not accepted or completed 8126 * (see return values for sata_hba_start()). 8127 * 8128 * Note: 8129 * Inquiry cdb format differs between transport version 2 and 3. 8130 * However, the transport version 3 devices that were checked did not adhere 8131 * to the specification (ignored MSB of the allocation length). Therefore, 8132 * the transport version is not checked, but Inquiry allocation length is 8133 * truncated to 255 bytes if the original allocation length set-up by the 8134 * target driver is greater than 255 bytes. 8135 */ 8136 static int 8137 sata_txlt_atapi(sata_pkt_txlate_t *spx) 8138 { 8139 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 8140 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 8141 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 8142 sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx); 8143 sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba, 8144 &spx->txlt_sata_pkt->satapkt_device); 8145 int cport = SATA_TXLT_CPORT(spx); 8146 int cdblen; 8147 int rval, reason; 8148 int synch; 8149 union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp; 8150 8151 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 8152 8153 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 8154 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 8155 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 8156 return (rval); 8157 } 8158 8159 /* 8160 * ATAPI device executes some ATA commands in addition to those 8161 * commands sent via PACKET command. These ATA commands may be 8162 * executed by the regular SATA translation functions. None needs 8163 * to be captured now. 8164 * 8165 * Commands sent via PACKET command include: 8166 * MMC command set for ATAPI CD/DVD device 8167 * SSC command set for ATAPI TAPE device 8168 * SBC command set for ATAPI disk device 8169 * 8170 */ 8171 8172 /* Check the size of cdb */ 8173 cdblen = scsi_cdb_size[GETGROUP(cdbp)]; 8174 if (cdblen > sdinfo->satadrv_atapi_cdb_len) { 8175 sata_log(NULL, CE_WARN, 8176 "sata: invalid ATAPI cdb length %d", 8177 scsipkt->pkt_cdblen); 8178 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 8179 return (TRAN_BADPKT); 8180 } 8181 8182 SATAATAPITRACE(spx, cdblen); 8183 8184 /* 8185 * For non-read/write commands we need to 8186 * map buffer 8187 */ 8188 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 8189 case SCMD_READ: 8190 case SCMD_READ_G1: 8191 case SCMD_READ_G5: 8192 case SCMD_READ_G4: 8193 case SCMD_WRITE: 8194 case SCMD_WRITE_G1: 8195 case SCMD_WRITE_G5: 8196 case SCMD_WRITE_G4: 8197 break; 8198 default: 8199 if (bp != NULL) { 8200 if (bp->b_flags & (B_PHYS | B_PAGEIO)) 8201 bp_mapin(bp); 8202 } 8203 break; 8204 } 8205 /* 8206 * scmd->satacmd_flags.sata_data_direction default - 8207 * SATA_DIR_NODATA_XFER - is set by 8208 * sata_txlt_generic_pkt_info(). 8209 */ 8210 if (scmd->satacmd_bp) { 8211 if (scmd->satacmd_bp->b_flags & B_READ) { 8212 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 8213 } else { 8214 scmd->satacmd_flags.sata_data_direction = 8215 SATA_DIR_WRITE; 8216 } 8217 } 8218 8219 /* 8220 * Set up ATAPI packet command. 8221 */ 8222 8223 sata_atapi_packet_cmd_setup(scmd, sdinfo); 8224 8225 /* Copy cdb into sata_cmd */ 8226 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 8227 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 8228 bcopy(cdbp, scmd->satacmd_acdb, cdblen); 8229 8230 /* See note in the command header */ 8231 if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) { 8232 if (scmd->satacmd_acdb[3] != 0) 8233 scmd->satacmd_acdb[4] = 255; 8234 } 8235 8236 #ifdef SATA_DEBUG 8237 if (sata_debug_flags & SATA_DBG_ATAPI) { 8238 uint8_t *p = scmd->satacmd_acdb; 8239 char buf[3 * SATA_ATAPI_MAX_CDB_LEN]; 8240 8241 (void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN, 8242 "%02x %02x %02x %02x %02x %02x %02x %02x " 8243 "%2x %02x %02x %02x %02x %02x %02x %02x", 8244 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 8245 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 8246 buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0'; 8247 cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf); 8248 } 8249 #endif 8250 8251 /* 8252 * Preset request sense data to NO SENSE. 8253 * If there is no way to get error information via Request Sense, 8254 * the packet request sense data would not have to be modified by HBA, 8255 * but it could be returned as is. 8256 */ 8257 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN); 8258 sata_fixed_sense_data_preset( 8259 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 8260 8261 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 8262 /* Need callback function */ 8263 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion; 8264 synch = FALSE; 8265 } else 8266 synch = TRUE; 8267 8268 /* Transfer command to HBA */ 8269 if (sata_hba_start(spx, &rval) != 0) { 8270 /* Pkt not accepted for execution */ 8271 mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport)); 8272 return (rval); 8273 } 8274 mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport)); 8275 /* 8276 * If execution is non-synchronous, 8277 * a callback function will handle potential errors, translate 8278 * the response and will do a callback to a target driver. 8279 * If it was synchronous, use the same framework callback to check 8280 * an execution status. 8281 */ 8282 if (synch) { 8283 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 8284 "synchronous execution status %x\n", 8285 spx->txlt_sata_pkt->satapkt_reason); 8286 sata_txlt_atapi_completion(spx->txlt_sata_pkt); 8287 } 8288 return (TRAN_ACCEPT); 8289 } 8290 8291 8292 /* 8293 * ATAPI Packet command completion. 8294 * 8295 * Failure of the command passed via Packet command are considered device 8296 * error. SATA HBA driver would have to retrieve error data (via Request 8297 * Sense command delivered via error retrieval sata packet) and copy it 8298 * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data. 8299 */ 8300 static void 8301 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt) 8302 { 8303 sata_pkt_txlate_t *spx = 8304 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 8305 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 8306 struct scsi_extended_sense *sense; 8307 struct buf *bp; 8308 int rval; 8309 8310 #ifdef SATA_DEBUG 8311 uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense; 8312 #endif 8313 8314 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 8315 STATE_SENT_CMD | STATE_GOT_STATUS; 8316 8317 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 8318 /* Normal completion */ 8319 if (sata_pkt->satapkt_cmd.satacmd_bp != NULL) 8320 scsipkt->pkt_state |= STATE_XFERRED_DATA; 8321 scsipkt->pkt_reason = CMD_CMPLT; 8322 *scsipkt->pkt_scbp = STATUS_GOOD; 8323 if (spx->txlt_tmp_buf != NULL) { 8324 /* Temporary buffer was used */ 8325 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 8326 if (bp->b_flags & B_READ) { 8327 rval = ddi_dma_sync( 8328 spx->txlt_buf_dma_handle, 0, 0, 8329 DDI_DMA_SYNC_FORCPU); 8330 ASSERT(rval == DDI_SUCCESS); 8331 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 8332 bp->b_bcount); 8333 } 8334 } 8335 } else { 8336 /* 8337 * Something went wrong - analyze return 8338 */ 8339 *scsipkt->pkt_scbp = STATUS_CHECK; 8340 sense = sata_arq_sense(spx); 8341 8342 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 8343 /* 8344 * pkt_reason should be CMD_CMPLT for DEVICE ERROR. 8345 * Under this condition ERR bit is set for ATA command, 8346 * and CHK bit set for ATAPI command. 8347 * 8348 * Please check st_intr & sdintr about how pkt_reason 8349 * is used. 8350 */ 8351 scsipkt->pkt_reason = CMD_CMPLT; 8352 8353 /* 8354 * We may not have ARQ data if there was a double 8355 * error. But sense data in sata packet was pre-set 8356 * with NO SENSE so it is valid even if HBA could 8357 * not retrieve a real sense data. 8358 * Just copy this sense data into scsi pkt sense area. 8359 */ 8360 bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense, 8361 SATA_ATAPI_MIN_RQSENSE_LEN); 8362 #ifdef SATA_DEBUG 8363 if (sata_debug_flags & SATA_DBG_SCSI_IF) { 8364 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 8365 "sata_txlt_atapi_completion: %02x\n" 8366 "RQSENSE: %02x %02x %02x %02x %02x %02x " 8367 " %02x %02x %02x %02x %02x %02x " 8368 " %02x %02x %02x %02x %02x %02x\n", 8369 scsipkt->pkt_reason, 8370 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 8371 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 8372 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 8373 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 8374 rqsp[16], rqsp[17]); 8375 } 8376 #endif 8377 } else { 8378 switch (sata_pkt->satapkt_reason) { 8379 case SATA_PKT_PORT_ERROR: 8380 /* 8381 * We have no device data. 8382 */ 8383 scsipkt->pkt_reason = CMD_INCOMPLETE; 8384 scsipkt->pkt_state &= ~(STATE_GOT_BUS | 8385 STATE_GOT_TARGET | STATE_SENT_CMD | 8386 STATE_GOT_STATUS); 8387 sense->es_key = KEY_HARDWARE_ERROR; 8388 break; 8389 8390 case SATA_PKT_TIMEOUT: 8391 scsipkt->pkt_reason = CMD_TIMEOUT; 8392 scsipkt->pkt_statistics |= 8393 STAT_TIMEOUT | STAT_DEV_RESET; 8394 /* 8395 * Need to check if HARDWARE_ERROR/ 8396 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more 8397 * appropriate. 8398 */ 8399 break; 8400 8401 case SATA_PKT_ABORTED: 8402 scsipkt->pkt_reason = CMD_ABORTED; 8403 scsipkt->pkt_statistics |= STAT_ABORTED; 8404 /* Should we set key COMMAND_ABPRTED? */ 8405 break; 8406 8407 case SATA_PKT_RESET: 8408 scsipkt->pkt_reason = CMD_RESET; 8409 scsipkt->pkt_statistics |= STAT_DEV_RESET; 8410 /* 8411 * May be we should set Unit Attention / 8412 * Reset. Perhaps the same should be 8413 * returned for disks.... 8414 */ 8415 sense->es_key = KEY_UNIT_ATTENTION; 8416 sense->es_add_code = SD_SCSI_ASC_RESET; 8417 break; 8418 8419 default: 8420 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 8421 "sata_txlt_atapi_completion: " 8422 "invalid packet completion reason")); 8423 scsipkt->pkt_reason = CMD_TRAN_ERR; 8424 scsipkt->pkt_state &= ~(STATE_GOT_BUS | 8425 STATE_GOT_TARGET | STATE_SENT_CMD | 8426 STATE_GOT_STATUS); 8427 break; 8428 } 8429 } 8430 } 8431 8432 SATAATAPITRACE(spx, 0); 8433 8434 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 8435 scsipkt->pkt_comp != NULL) { 8436 /* scsi callback required */ 8437 (*scsipkt->pkt_comp)(scsipkt); 8438 } 8439 } 8440 8441 /* 8442 * Set up error retrieval sata command for ATAPI Packet Command error data 8443 * recovery. 8444 * 8445 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up, 8446 * returns SATA_FAILURE otherwise. 8447 */ 8448 8449 static int 8450 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo) 8451 { 8452 sata_pkt_t *spkt = spx->txlt_sata_pkt; 8453 sata_cmd_t *scmd; 8454 struct buf *bp; 8455 8456 /* 8457 * Allocate dma-able buffer error data. 8458 * Buffer allocation will take care of buffer alignment and other DMA 8459 * attributes. 8460 */ 8461 bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN); 8462 if (bp == NULL) { 8463 SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst, 8464 "sata_get_err_retrieval_pkt: " 8465 "cannot allocate buffer for error data", NULL); 8466 return (SATA_FAILURE); 8467 } 8468 bp_mapin(bp); /* make data buffer accessible */ 8469 8470 /* Operation modes are up to the caller */ 8471 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 8472 8473 /* Synchronous mode, no callback - may be changed by the caller */ 8474 spkt->satapkt_comp = NULL; 8475 spkt->satapkt_time = sata_default_pkt_time; 8476 8477 scmd = &spkt->satapkt_cmd; 8478 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 8479 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 8480 8481 sata_atapi_packet_cmd_setup(scmd, sdinfo); 8482 8483 /* 8484 * Set-up acdb. Request Sense CDB (packet command content) is 8485 * not in DMA-able buffer. Its handling is HBA-specific (how 8486 * it is transfered into packet FIS). 8487 */ 8488 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 8489 bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN); 8490 /* Following zeroing of pad bytes may not be necessary */ 8491 bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN], 8492 sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN); 8493 8494 /* 8495 * Set-up pointer to the buffer handle, so HBA can sync buffer 8496 * before accessing it. Handle is in usual place in translate struct. 8497 */ 8498 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle; 8499 8500 /* 8501 * Preset request sense data to NO SENSE. 8502 * Here it is redundant, only for a symetry with scsi-originated 8503 * packets. It should not be used for anything but debugging. 8504 */ 8505 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN); 8506 sata_fixed_sense_data_preset( 8507 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 8508 8509 ASSERT(scmd->satacmd_num_dma_cookies != 0); 8510 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 8511 8512 return (SATA_SUCCESS); 8513 } 8514 8515 /* 8516 * Set-up ATAPI packet command. 8517 * Data transfer direction has to be set-up in sata_cmd structure prior to 8518 * calling this function. 8519 * 8520 * Returns void 8521 */ 8522 8523 static void 8524 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo) 8525 { 8526 scmd->satacmd_addr_type = 0; /* N/A */ 8527 scmd->satacmd_sec_count_lsb = 0; /* no tag */ 8528 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 8529 scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ; 8530 scmd->satacmd_lba_high_lsb = 8531 (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8); 8532 scmd->satacmd_cmd_reg = SATAC_PACKET; /* Command */ 8533 8534 /* 8535 * We want all data to be transfered via DMA. 8536 * But specify it only if drive supports DMA and DMA mode is 8537 * selected - some drives are sensitive about it. 8538 * Hopefully it wil work for all drives.... 8539 */ 8540 if (sdinfo->satadrv_settings & SATA_DEV_DMA) 8541 scmd->satacmd_features_reg = SATA_ATAPI_F_DMA; 8542 8543 /* 8544 * Features register requires special care for devices that use 8545 * Serial ATA bridge - they need an explicit specification of 8546 * the data transfer direction for Packet DMA commands. 8547 * Setting this bit is harmless if DMA is not used. 8548 * 8549 * Many drives do not implement word 80, specifying what ATA/ATAPI 8550 * spec they follow. 8551 * We are arbitrarily following the latest SerialATA 2.6 spec, 8552 * which uses ATA/ATAPI 6 specification for Identify Data, unless 8553 * ATA/ATAPI-7 support is explicitly indicated. 8554 */ 8555 if (sdinfo->satadrv_id.ai_majorversion != 0 && 8556 sdinfo->satadrv_id.ai_majorversion != 0xffff && 8557 (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) { 8558 /* 8559 * Specification of major version is valid and version 7 8560 * is supported. It does automatically imply that all 8561 * spec features are supported. For now, we assume that 8562 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete. 8563 */ 8564 if ((sdinfo->satadrv_id.ai_dirdma & 8565 SATA_ATAPI_ID_DMADIR_REQ) != 0) { 8566 if (scmd->satacmd_flags.sata_data_direction == 8567 SATA_DIR_READ) 8568 scmd->satacmd_features_reg |= 8569 SATA_ATAPI_F_DATA_DIR_READ; 8570 } 8571 } 8572 } 8573 8574 8575 #ifdef SATA_DEBUG 8576 8577 /* Display 18 bytes of Inquiry data */ 8578 static void 8579 sata_show_inqry_data(uint8_t *buf) 8580 { 8581 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 8582 uint8_t *p; 8583 8584 cmn_err(CE_NOTE, "Inquiry data:"); 8585 cmn_err(CE_NOTE, "device type %x", inq->inq_dtype); 8586 cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb); 8587 cmn_err(CE_NOTE, "version %x", inq->inq_ansi); 8588 cmn_err(CE_NOTE, "ATAPI transport version %d", 8589 SATA_ATAPI_TRANS_VERSION(inq)); 8590 cmn_err(CE_NOTE, "response data format %d, aenc %d", 8591 inq->inq_rdf, inq->inq_aenc); 8592 cmn_err(CE_NOTE, " additional length %d", inq->inq_len); 8593 cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs); 8594 p = (uint8_t *)inq->inq_vid; 8595 cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x " 8596 "%02x %02x %02x %02x", 8597 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 8598 p = (uint8_t *)inq->inq_vid; 8599 cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c", 8600 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 8601 8602 p = (uint8_t *)inq->inq_pid; 8603 cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x " 8604 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", 8605 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 8606 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 8607 p = (uint8_t *)inq->inq_pid; 8608 cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c " 8609 "%c %c %c %c %c %c %c %c", 8610 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 8611 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 8612 8613 p = (uint8_t *)inq->inq_revision; 8614 cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x", 8615 p[0], p[1], p[2], p[3]); 8616 p = (uint8_t *)inq->inq_revision; 8617 cmn_err(CE_NOTE, "revision: %c %c %c %c", 8618 p[0], p[1], p[2], p[3]); 8619 8620 } 8621 8622 8623 static void 8624 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count) 8625 { 8626 struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt; 8627 8628 if (scsi_pkt == NULL) 8629 return; 8630 if (count != 0) { 8631 /* saving cdb */ 8632 bzero(sata_atapi_trace[sata_atapi_trace_index].acdb, 8633 SATA_ATAPI_MAX_CDB_LEN); 8634 bcopy(scsi_pkt->pkt_cdbp, 8635 sata_atapi_trace[sata_atapi_trace_index].acdb, count); 8636 } else { 8637 bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)-> 8638 sts_sensedata, 8639 sata_atapi_trace[sata_atapi_trace_index].arqs, 8640 SATA_ATAPI_MIN_RQSENSE_LEN); 8641 sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason = 8642 scsi_pkt->pkt_reason; 8643 sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason = 8644 spx->txlt_sata_pkt->satapkt_reason; 8645 8646 if (++sata_atapi_trace_index >= 64) 8647 sata_atapi_trace_index = 0; 8648 } 8649 } 8650 8651 #endif 8652 8653 /* 8654 * Fetch inquiry data from ATAPI device 8655 * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise. 8656 * 8657 * Note: 8658 * inqb pointer does not point to a DMA-able buffer. It is a local buffer 8659 * where the caller expects to see the inquiry data. 8660 * 8661 */ 8662 8663 static int 8664 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba, 8665 sata_address_t *saddr, struct scsi_inquiry *inq) 8666 { 8667 sata_pkt_txlate_t *spx; 8668 sata_pkt_t *spkt; 8669 struct buf *bp; 8670 sata_drive_info_t *sdinfo; 8671 sata_cmd_t *scmd; 8672 int rval; 8673 uint8_t *rqsp; 8674 #ifdef SATA_DEBUG 8675 char msg_buf[MAXPATHLEN]; 8676 #endif 8677 8678 ASSERT(sata_hba != NULL); 8679 8680 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 8681 spx->txlt_sata_hba_inst = sata_hba; 8682 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 8683 spkt = sata_pkt_alloc(spx, NULL); 8684 if (spkt == NULL) { 8685 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 8686 return (SATA_FAILURE); 8687 } 8688 /* address is needed now */ 8689 spkt->satapkt_device.satadev_addr = *saddr; 8690 8691 /* scsi_inquiry size buffer */ 8692 bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry)); 8693 if (bp == NULL) { 8694 sata_pkt_free(spx); 8695 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 8696 SATA_LOG_D((sata_hba, CE_WARN, 8697 "sata_get_atapi_inquiry_data: " 8698 "cannot allocate data buffer")); 8699 return (SATA_FAILURE); 8700 } 8701 bp_mapin(bp); /* make data buffer accessible */ 8702 8703 scmd = &spkt->satapkt_cmd; 8704 ASSERT(scmd->satacmd_num_dma_cookies != 0); 8705 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 8706 8707 /* Use synchronous mode */ 8708 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 8709 spkt->satapkt_comp = NULL; 8710 spkt->satapkt_time = sata_default_pkt_time; 8711 8712 /* Issue inquiry command - 6 bytes cdb, data transfer, read */ 8713 8714 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 8715 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 8716 8717 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 8718 sdinfo = sata_get_device_info(sata_hba, 8719 &spx->txlt_sata_pkt->satapkt_device); 8720 if (sdinfo == NULL) { 8721 /* we have to be carefull about the disapearing device */ 8722 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 8723 rval = SATA_FAILURE; 8724 goto cleanup; 8725 } 8726 sata_atapi_packet_cmd_setup(scmd, sdinfo); 8727 8728 /* 8729 * Set-up acdb. This works for atapi transport version 2 and later. 8730 */ 8731 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 8732 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 8733 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */ 8734 scmd->satacmd_acdb[1] = 0x00; 8735 scmd->satacmd_acdb[2] = 0x00; 8736 scmd->satacmd_acdb[3] = 0x00; 8737 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry); 8738 scmd->satacmd_acdb[5] = 0x00; 8739 8740 sata_fixed_sense_data_preset( 8741 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 8742 8743 /* Transfer command to HBA */ 8744 if (sata_hba_start(spx, &rval) != 0) { 8745 /* Pkt not accepted for execution */ 8746 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8747 "sata_get_atapi_inquiry_data: " 8748 "Packet not accepted for execution - ret: %02x", rval); 8749 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 8750 rval = SATA_FAILURE; 8751 goto cleanup; 8752 } 8753 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 8754 8755 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) { 8756 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8757 "sata_get_atapi_inquiry_data: " 8758 "Packet completed successfully - ret: %02x", rval); 8759 if (spx->txlt_buf_dma_handle != NULL) { 8760 /* 8761 * Sync buffer. Handle is in usual place in translate 8762 * struct. 8763 */ 8764 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 8765 DDI_DMA_SYNC_FORCPU); 8766 ASSERT(rval == DDI_SUCCESS); 8767 } 8768 /* 8769 * Normal completion - copy data into caller's buffer 8770 */ 8771 bcopy(bp->b_un.b_addr, (uint8_t *)inq, 8772 sizeof (struct scsi_inquiry)); 8773 #ifdef SATA_DEBUG 8774 if (sata_debug_flags & SATA_DBG_ATAPI) { 8775 sata_show_inqry_data((uint8_t *)inq); 8776 } 8777 #endif 8778 rval = SATA_SUCCESS; 8779 } else { 8780 /* 8781 * Something went wrong - analyze return - check rqsense data 8782 */ 8783 rval = SATA_FAILURE; 8784 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 8785 /* 8786 * ARQ data hopefull show something other than NO SENSE 8787 */ 8788 rqsp = scmd->satacmd_rqsense; 8789 #ifdef SATA_DEBUG 8790 if (sata_debug_flags & SATA_DBG_ATAPI) { 8791 msg_buf[0] = '\0'; 8792 (void) snprintf(msg_buf, MAXPATHLEN, 8793 "ATAPI packet completion reason: %02x\n" 8794 "RQSENSE: %02x %02x %02x %02x %02x %02x\n" 8795 " %02x %02x %02x %02x %02x %02x\n" 8796 " %02x %02x %02x %02x %02x %02x", 8797 spkt->satapkt_reason, 8798 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 8799 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 8800 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 8801 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 8802 rqsp[16], rqsp[17]); 8803 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 8804 "%s", msg_buf); 8805 } 8806 #endif 8807 } else { 8808 switch (spkt->satapkt_reason) { 8809 case SATA_PKT_PORT_ERROR: 8810 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8811 "sata_get_atapi_inquiry_data: " 8812 "packet reason: port error", NULL); 8813 break; 8814 8815 case SATA_PKT_TIMEOUT: 8816 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8817 "sata_get_atapi_inquiry_data: " 8818 "packet reason: timeout", NULL); 8819 break; 8820 8821 case SATA_PKT_ABORTED: 8822 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8823 "sata_get_atapi_inquiry_data: " 8824 "packet reason: aborted", NULL); 8825 break; 8826 8827 case SATA_PKT_RESET: 8828 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8829 "sata_get_atapi_inquiry_data: " 8830 "packet reason: reset\n", NULL); 8831 break; 8832 default: 8833 SATADBG1(SATA_DBG_ATAPI, sata_hba, 8834 "sata_get_atapi_inquiry_data: " 8835 "invalid packet reason: %02x\n", 8836 spkt->satapkt_reason); 8837 break; 8838 } 8839 } 8840 } 8841 cleanup: 8842 sata_free_local_buffer(spx); 8843 sata_pkt_free(spx); 8844 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 8845 return (rval); 8846 } 8847 8848 8849 8850 8851 8852 #if 0 8853 #ifdef SATA_DEBUG 8854 8855 /* 8856 * Test ATAPI packet command. 8857 * Single threaded test: send packet command in synch mode, process completion 8858 * 8859 */ 8860 static void 8861 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport) 8862 { 8863 sata_pkt_txlate_t *spx; 8864 sata_pkt_t *spkt; 8865 struct buf *bp; 8866 sata_device_t sata_device; 8867 sata_drive_info_t *sdinfo; 8868 sata_cmd_t *scmd; 8869 int rval; 8870 uint8_t *rqsp; 8871 8872 ASSERT(sata_hba_inst != NULL); 8873 sata_device.satadev_addr.cport = cport; 8874 sata_device.satadev_addr.pmport = 0; 8875 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 8876 sata_device.satadev_rev = SATA_DEVICE_REV; 8877 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 8878 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 8879 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 8880 if (sdinfo == NULL) { 8881 sata_log(sata_hba_inst, CE_WARN, 8882 "sata_test_atapi_packet_command: " 8883 "no device info for cport %d", 8884 sata_device.satadev_addr.cport); 8885 return; 8886 } 8887 8888 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 8889 spx->txlt_sata_hba_inst = sata_hba_inst; 8890 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 8891 spkt = sata_pkt_alloc(spx, NULL); 8892 if (spkt == NULL) { 8893 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 8894 return; 8895 } 8896 /* address is needed now */ 8897 spkt->satapkt_device.satadev_addr = sata_device.satadev_addr; 8898 8899 /* 1024k buffer */ 8900 bp = sata_alloc_local_buffer(spx, 1024); 8901 if (bp == NULL) { 8902 sata_pkt_free(spx); 8903 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 8904 sata_log(sata_hba_inst, CE_WARN, 8905 "sata_test_atapi_packet_command: " 8906 "cannot allocate data buffer"); 8907 return; 8908 } 8909 bp_mapin(bp); /* make data buffer accessible */ 8910 8911 scmd = &spkt->satapkt_cmd; 8912 ASSERT(scmd->satacmd_num_dma_cookies != 0); 8913 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 8914 8915 /* Use synchronous mode */ 8916 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 8917 8918 /* Synchronous mode, no callback - may be changed by the caller */ 8919 spkt->satapkt_comp = NULL; 8920 spkt->satapkt_time = sata_default_pkt_time; 8921 8922 /* Issue inquiry command - 6 bytes cdb, data transfer, read */ 8923 8924 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 8925 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 8926 8927 sata_atapi_packet_cmd_setup(scmd, sdinfo); 8928 8929 /* Set-up acdb. */ 8930 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 8931 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 8932 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */ 8933 scmd->satacmd_acdb[1] = 0x00; 8934 scmd->satacmd_acdb[2] = 0x00; 8935 scmd->satacmd_acdb[3] = 0x00; 8936 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry); 8937 scmd->satacmd_acdb[5] = 0x00; 8938 8939 sata_fixed_sense_data_preset( 8940 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 8941 8942 /* Transfer command to HBA */ 8943 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 8944 if (sata_hba_start(spx, &rval) != 0) { 8945 /* Pkt not accepted for execution */ 8946 sata_log(sata_hba_inst, CE_WARN, 8947 "sata_test_atapi_packet_command: " 8948 "Packet not accepted for execution - ret: %02x", rval); 8949 mutex_exit( 8950 &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 8951 goto cleanup; 8952 } 8953 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 8954 8955 if (spx->txlt_buf_dma_handle != NULL) { 8956 /* 8957 * Sync buffer. Handle is in usual place in translate struct. 8958 */ 8959 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 8960 DDI_DMA_SYNC_FORCPU); 8961 ASSERT(rval == DDI_SUCCESS); 8962 } 8963 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) { 8964 sata_log(sata_hba_inst, CE_WARN, 8965 "sata_test_atapi_packet_command: " 8966 "Packet completed successfully"); 8967 /* 8968 * Normal completion - show inquiry data 8969 */ 8970 sata_show_inqry_data((uint8_t *)bp->b_un.b_addr); 8971 } else { 8972 /* 8973 * Something went wrong - analyze return - check rqsense data 8974 */ 8975 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 8976 /* 8977 * ARQ data hopefull show something other than NO SENSE 8978 */ 8979 rqsp = scmd->satacmd_rqsense; 8980 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 8981 "ATAPI packet completion reason: %02x\n" 8982 "RQSENSE: %02x %02x %02x %02x %02x %02x " 8983 " %02x %02x %02x %02x %02x %02x " 8984 " %02x %02x %02x %02x %02x %02x\n", 8985 spkt->satapkt_reason, 8986 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 8987 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 8988 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 8989 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 8990 rqsp[16], rqsp[17]); 8991 } else { 8992 switch (spkt->satapkt_reason) { 8993 case SATA_PKT_PORT_ERROR: 8994 sata_log(sata_hba_inst, CE_WARN, 8995 "sata_test_atapi_packet_command: " 8996 "packet reason: port error\n"); 8997 break; 8998 8999 case SATA_PKT_TIMEOUT: 9000 sata_log(sata_hba_inst, CE_WARN, 9001 "sata_test_atapi_packet_command: " 9002 "packet reason: timeout\n"); 9003 break; 9004 9005 case SATA_PKT_ABORTED: 9006 sata_log(sata_hba_inst, CE_WARN, 9007 "sata_test_atapi_packet_command: " 9008 "packet reason: aborted\n"); 9009 break; 9010 9011 case SATA_PKT_RESET: 9012 sata_log(sata_hba_inst, CE_WARN, 9013 "sata_test_atapi_packet_command: " 9014 "packet reason: reset\n"); 9015 break; 9016 default: 9017 sata_log(sata_hba_inst, CE_WARN, 9018 "sata_test_atapi_packet_command: " 9019 "invalid packet reason: %02x\n", 9020 spkt->satapkt_reason); 9021 break; 9022 } 9023 } 9024 } 9025 cleanup: 9026 sata_free_local_buffer(spx); 9027 sata_pkt_free(spx); 9028 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 9029 } 9030 9031 #endif /* SATA_DEBUG */ 9032 #endif /* 1 */ 9033 9034 9035 /* ************************** LOCAL HELPER FUNCTIONS *********************** */ 9036 9037 /* 9038 * Validate sata_tran info 9039 * SATA_FAILURE returns if structure is inconsistent or structure revision 9040 * does not match one used by the framework. 9041 * 9042 * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains 9043 * required function pointers. 9044 * Returns SATA_FAILURE otherwise. 9045 */ 9046 static int 9047 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran) 9048 { 9049 /* 9050 * SATA_TRAN_HBA_REV is the current (highest) revision number 9051 * of the SATA interface. 9052 */ 9053 if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) { 9054 sata_log(NULL, CE_WARN, 9055 "sata: invalid sata_hba_tran version %d for driver %s", 9056 sata_tran->sata_tran_hba_rev, ddi_driver_name(dip)); 9057 return (SATA_FAILURE); 9058 } 9059 9060 if (dip != sata_tran->sata_tran_hba_dip) { 9061 SATA_LOG_D((NULL, CE_WARN, 9062 "sata: inconsistent sata_tran_hba_dip " 9063 "%p / %p", sata_tran->sata_tran_hba_dip, dip)); 9064 return (SATA_FAILURE); 9065 } 9066 9067 if (sata_tran->sata_tran_probe_port == NULL || 9068 sata_tran->sata_tran_start == NULL || 9069 sata_tran->sata_tran_abort == NULL || 9070 sata_tran->sata_tran_reset_dport == NULL || 9071 sata_tran->sata_tran_hotplug_ops == NULL || 9072 sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL || 9073 sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate == 9074 NULL) { 9075 SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing " 9076 "required functions")); 9077 } 9078 return (SATA_SUCCESS); 9079 } 9080 9081 /* 9082 * Remove HBA instance from sata_hba_list. 9083 */ 9084 static void 9085 sata_remove_hba_instance(dev_info_t *dip) 9086 { 9087 sata_hba_inst_t *sata_hba_inst; 9088 9089 mutex_enter(&sata_mutex); 9090 for (sata_hba_inst = sata_hba_list; 9091 sata_hba_inst != (struct sata_hba_inst *)NULL; 9092 sata_hba_inst = sata_hba_inst->satahba_next) { 9093 if (sata_hba_inst->satahba_dip == dip) 9094 break; 9095 } 9096 9097 if (sata_hba_inst == (struct sata_hba_inst *)NULL) { 9098 #ifdef SATA_DEBUG 9099 cmn_err(CE_WARN, "sata_remove_hba_instance: " 9100 "unknown HBA instance\n"); 9101 #endif 9102 ASSERT(FALSE); 9103 } 9104 if (sata_hba_inst == sata_hba_list) { 9105 sata_hba_list = sata_hba_inst->satahba_next; 9106 if (sata_hba_list) { 9107 sata_hba_list->satahba_prev = 9108 (struct sata_hba_inst *)NULL; 9109 } 9110 if (sata_hba_inst == sata_hba_list_tail) { 9111 sata_hba_list_tail = NULL; 9112 } 9113 } else if (sata_hba_inst == sata_hba_list_tail) { 9114 sata_hba_list_tail = sata_hba_inst->satahba_prev; 9115 if (sata_hba_list_tail) { 9116 sata_hba_list_tail->satahba_next = 9117 (struct sata_hba_inst *)NULL; 9118 } 9119 } else { 9120 sata_hba_inst->satahba_prev->satahba_next = 9121 sata_hba_inst->satahba_next; 9122 sata_hba_inst->satahba_next->satahba_prev = 9123 sata_hba_inst->satahba_prev; 9124 } 9125 mutex_exit(&sata_mutex); 9126 } 9127 9128 /* 9129 * Probe all SATA ports of the specified HBA instance. 9130 * The assumption is that there are no target and attachment point minor nodes 9131 * created by the boot subsystems, so we do not need to prune device tree. 9132 * 9133 * This function is called only from sata_hba_attach(). It does not have to 9134 * be protected by controller mutex, because the hba_attached flag is not set 9135 * yet and no one would be touching this HBA instance other than this thread. 9136 * Determines if port is active and what type of the device is attached 9137 * (if any). Allocates necessary structures for each port. 9138 * 9139 * An AP (Attachement Point) node is created for each SATA device port even 9140 * when there is no device attached. 9141 */ 9142 9143 static void 9144 sata_probe_ports(sata_hba_inst_t *sata_hba_inst) 9145 { 9146 dev_info_t *dip = SATA_DIP(sata_hba_inst); 9147 int ncport; 9148 sata_cport_info_t *cportinfo; 9149 sata_drive_info_t *drive; 9150 sata_device_t sata_device; 9151 int rval; 9152 dev_t minor_number; 9153 char name[16]; 9154 clock_t start_time, cur_time; 9155 9156 /* 9157 * Probe controller ports first, to find port status and 9158 * any port multiplier attached. 9159 */ 9160 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 9161 /* allocate cport structure */ 9162 cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP); 9163 ASSERT(cportinfo != NULL); 9164 mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL); 9165 9166 mutex_enter(&cportinfo->cport_mutex); 9167 9168 cportinfo->cport_addr.cport = ncport; 9169 cportinfo->cport_addr.pmport = 0; 9170 cportinfo->cport_addr.qual = SATA_ADDR_CPORT; 9171 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 9172 cportinfo->cport_state |= SATA_STATE_PROBING; 9173 SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo; 9174 9175 /* 9176 * Regardless if a port is usable or not, create 9177 * an attachment point 9178 */ 9179 mutex_exit(&cportinfo->cport_mutex); 9180 minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip), 9181 ncport, 0, SATA_ADDR_CPORT); 9182 (void) sprintf(name, "%d", ncport); 9183 if (ddi_create_minor_node(dip, name, S_IFCHR, 9184 minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) != 9185 DDI_SUCCESS) { 9186 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: " 9187 "cannot create SATA attachment point for port %d", 9188 ncport); 9189 } 9190 9191 /* Probe port */ 9192 start_time = ddi_get_lbolt(); 9193 reprobe_cport: 9194 sata_device.satadev_addr.cport = ncport; 9195 sata_device.satadev_addr.pmport = 0; 9196 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 9197 sata_device.satadev_rev = SATA_DEVICE_REV; 9198 9199 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 9200 (dip, &sata_device); 9201 9202 mutex_enter(&cportinfo->cport_mutex); 9203 cportinfo->cport_scr = sata_device.satadev_scr; 9204 if (rval != SATA_SUCCESS) { 9205 /* Something went wrong? Fail the port */ 9206 cportinfo->cport_state = SATA_PSTATE_FAILED; 9207 mutex_exit(&cportinfo->cport_mutex); 9208 continue; 9209 } 9210 cportinfo->cport_state &= ~SATA_STATE_PROBING; 9211 cportinfo->cport_state |= SATA_STATE_PROBED; 9212 cportinfo->cport_dev_type = sata_device.satadev_type; 9213 9214 cportinfo->cport_state |= SATA_STATE_READY; 9215 if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) { 9216 mutex_exit(&cportinfo->cport_mutex); 9217 continue; 9218 } 9219 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 9220 /* 9221 * There is some device attached. 9222 * Allocate device info structure 9223 */ 9224 if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) { 9225 mutex_exit(&cportinfo->cport_mutex); 9226 SATA_CPORTINFO_DRV_INFO(cportinfo) = 9227 kmem_zalloc(sizeof (sata_drive_info_t), 9228 KM_SLEEP); 9229 mutex_enter(&cportinfo->cport_mutex); 9230 } 9231 drive = SATA_CPORTINFO_DRV_INFO(cportinfo); 9232 drive->satadrv_addr = cportinfo->cport_addr; 9233 drive->satadrv_addr.qual = SATA_ADDR_DCPORT; 9234 drive->satadrv_type = cportinfo->cport_dev_type; 9235 drive->satadrv_state = SATA_STATE_UNKNOWN; 9236 9237 mutex_exit(&cportinfo->cport_mutex); 9238 if (sata_add_device(dip, sata_hba_inst, &sata_device) != 9239 SATA_SUCCESS) { 9240 /* 9241 * Plugged device was not correctly identified. 9242 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT 9243 */ 9244 cur_time = ddi_get_lbolt(); 9245 if ((cur_time - start_time) < 9246 drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) { 9247 /* sleep for a while */ 9248 delay(drv_usectohz( 9249 SATA_DEV_RETRY_DLY)); 9250 goto reprobe_cport; 9251 } 9252 } 9253 } else { /* SATA_DTYPE_PMULT */ 9254 mutex_exit(&cportinfo->cport_mutex); 9255 9256 /* Allocate sata_pmult_info and sata_pmport_info */ 9257 if (sata_alloc_pmult(sata_hba_inst, &sata_device) != 9258 SATA_SUCCESS) 9259 continue; 9260 9261 /* Log the information of the port multiplier */ 9262 sata_show_pmult_info(sata_hba_inst, &sata_device); 9263 9264 /* Probe its pmports */ 9265 sata_probe_pmports(sata_hba_inst, ncport); 9266 } 9267 } 9268 } 9269 9270 /* 9271 * Probe all device ports behind a port multiplier. 9272 * 9273 * PMult-related structure should be allocated before by sata_alloc_pmult(). 9274 * 9275 * NOTE1: Only called from sata_probe_ports() 9276 * NOTE2: No mutex should be hold. 9277 */ 9278 static void 9279 sata_probe_pmports(sata_hba_inst_t *sata_hba_inst, uint8_t ncport) 9280 { 9281 dev_info_t *dip = SATA_DIP(sata_hba_inst); 9282 sata_pmult_info_t *pmultinfo = NULL; 9283 sata_pmport_info_t *pmportinfo = NULL; 9284 sata_drive_info_t *drive = NULL; 9285 sata_device_t sata_device; 9286 9287 clock_t start_time, cur_time; 9288 int npmport; 9289 int rval; 9290 9291 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, ncport); 9292 9293 /* Probe Port Multiplier ports */ 9294 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; npmport++) { 9295 pmportinfo = pmultinfo->pmult_dev_port[npmport]; 9296 start_time = ddi_get_lbolt(); 9297 reprobe_pmport: 9298 sata_device.satadev_addr.cport = ncport; 9299 sata_device.satadev_addr.pmport = npmport; 9300 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT; 9301 sata_device.satadev_rev = SATA_DEVICE_REV; 9302 9303 /* Let HBA driver probe it. */ 9304 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 9305 (dip, &sata_device); 9306 mutex_enter(&pmportinfo->pmport_mutex); 9307 9308 pmportinfo->pmport_scr = sata_device.satadev_scr; 9309 9310 if (rval != SATA_SUCCESS) { 9311 pmportinfo->pmport_state = 9312 SATA_PSTATE_FAILED; 9313 mutex_exit(&pmportinfo->pmport_mutex); 9314 continue; 9315 } 9316 pmportinfo->pmport_state &= ~SATA_STATE_PROBING; 9317 pmportinfo->pmport_state |= SATA_STATE_PROBED; 9318 pmportinfo->pmport_dev_type = sata_device.satadev_type; 9319 9320 pmportinfo->pmport_state |= SATA_STATE_READY; 9321 if (pmportinfo->pmport_dev_type == 9322 SATA_DTYPE_NONE) { 9323 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 9324 "no device found at port %d:%d", ncport, npmport); 9325 mutex_exit(&pmportinfo->pmport_mutex); 9326 continue; 9327 } 9328 /* Port multipliers cannot be chained */ 9329 ASSERT(pmportinfo->pmport_dev_type != SATA_DTYPE_PMULT); 9330 /* 9331 * There is something attached to Port 9332 * Multiplier device port 9333 * Allocate device info structure 9334 */ 9335 if (pmportinfo->pmport_sata_drive == NULL) { 9336 mutex_exit(&pmportinfo->pmport_mutex); 9337 pmportinfo->pmport_sata_drive = 9338 kmem_zalloc(sizeof (sata_drive_info_t), KM_SLEEP); 9339 mutex_enter(&pmportinfo->pmport_mutex); 9340 } 9341 drive = pmportinfo->pmport_sata_drive; 9342 drive->satadrv_addr.cport = pmportinfo->pmport_addr.cport; 9343 drive->satadrv_addr.pmport = npmport; 9344 drive->satadrv_addr.qual = SATA_ADDR_DPMPORT; 9345 drive->satadrv_type = pmportinfo-> pmport_dev_type; 9346 drive->satadrv_state = SATA_STATE_UNKNOWN; 9347 9348 mutex_exit(&pmportinfo->pmport_mutex); 9349 rval = sata_add_device(dip, sata_hba_inst, &sata_device); 9350 9351 if (rval != SATA_SUCCESS) { 9352 /* 9353 * Plugged device was not correctly identified. 9354 * Retry, within the SATA_DEV_IDENTIFY_TIMEOUT 9355 */ 9356 cur_time = ddi_get_lbolt(); 9357 if ((cur_time - start_time) < drv_usectohz( 9358 SATA_DEV_IDENTIFY_TIMEOUT)) { 9359 /* sleep for a while */ 9360 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 9361 goto reprobe_pmport; 9362 } 9363 } 9364 } 9365 } 9366 9367 /* 9368 * Add SATA device for specified HBA instance & port (SCSI target 9369 * device nodes). 9370 * This function is called (indirectly) only from sata_hba_attach(). 9371 * A target node is created when there is a supported type device attached, 9372 * but may be removed if it cannot be put online. 9373 * 9374 * This function cannot be called from an interrupt context. 9375 * 9376 * Create target nodes for disk, CD/DVD, Tape and ATAPI disk devices 9377 * 9378 * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when 9379 * device identification failed - adding a device could be retried. 9380 * 9381 */ 9382 static int 9383 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, 9384 sata_device_t *sata_device) 9385 { 9386 sata_cport_info_t *cportinfo; 9387 sata_pmult_info_t *pminfo; 9388 sata_pmport_info_t *pmportinfo; 9389 dev_info_t *cdip; /* child dip */ 9390 sata_address_t *saddr = &sata_device->satadev_addr; 9391 uint8_t cport, pmport; 9392 int rval; 9393 9394 cport = saddr->cport; 9395 pmport = saddr->pmport; 9396 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 9397 ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE); 9398 9399 /* 9400 * Some device is attached to a controller port. 9401 * We rely on controllers distinquishing between no-device, 9402 * attached port multiplier and other kind of attached device. 9403 * We need to get Identify Device data and determine 9404 * positively the dev type before trying to attach 9405 * the target driver. 9406 */ 9407 sata_device->satadev_rev = SATA_DEVICE_REV; 9408 switch (saddr->qual) { 9409 case SATA_ADDR_CPORT: 9410 /* 9411 * Add a non-port-multiplier device at controller port. 9412 */ 9413 saddr->qual = SATA_ADDR_DCPORT; 9414 9415 rval = sata_probe_device(sata_hba_inst, sata_device); 9416 if (rval != SATA_SUCCESS || 9417 sata_device->satadev_type == SATA_DTYPE_UNKNOWN) 9418 return (SATA_FAILURE); 9419 9420 mutex_enter(&cportinfo->cport_mutex); 9421 sata_show_drive_info(sata_hba_inst, 9422 SATA_CPORTINFO_DRV_INFO(cportinfo)); 9423 9424 if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) { 9425 /* 9426 * Could not determine device type or 9427 * a device is not supported. 9428 * Degrade this device to unknown. 9429 */ 9430 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 9431 mutex_exit(&cportinfo->cport_mutex); 9432 return (SATA_SUCCESS); 9433 } 9434 cportinfo->cport_dev_type = sata_device->satadev_type; 9435 cportinfo->cport_tgtnode_clean = B_TRUE; 9436 mutex_exit(&cportinfo->cport_mutex); 9437 9438 /* 9439 * Initialize device to the desired state. Even if it 9440 * fails, the device will still attach but syslog 9441 * will show the warning. 9442 */ 9443 if (sata_initialize_device(sata_hba_inst, 9444 SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) { 9445 /* Retry */ 9446 rval = sata_initialize_device(sata_hba_inst, 9447 SATA_CPORTINFO_DRV_INFO(cportinfo)); 9448 9449 if (rval == SATA_RETRY) 9450 sata_log(sata_hba_inst, CE_WARN, 9451 "SATA device at port %d - " 9452 "default device features could not be set." 9453 " Device may not operate as expected.", 9454 cport); 9455 } 9456 9457 cdip = sata_create_target_node(pdip, sata_hba_inst, saddr); 9458 if (cdip == NULL) { 9459 /* 9460 * Attaching target node failed. 9461 * We retain sata_drive_info structure... 9462 */ 9463 return (SATA_SUCCESS); 9464 } 9465 9466 mutex_enter(&cportinfo->cport_mutex); 9467 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 9468 satadrv_state = SATA_STATE_READY; 9469 mutex_exit(&cportinfo->cport_mutex); 9470 9471 break; 9472 9473 case SATA_ADDR_PMPORT: 9474 saddr->qual = SATA_ADDR_DPMPORT; 9475 9476 mutex_enter(&cportinfo->cport_mutex); 9477 /* It must be a Port Multiplier at the controller port */ 9478 ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT); 9479 9480 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 9481 pmportinfo = pminfo->pmult_dev_port[saddr->pmport]; 9482 mutex_exit(&cportinfo->cport_mutex); 9483 9484 rval = sata_probe_device(sata_hba_inst, sata_device); 9485 if (rval != SATA_SUCCESS || 9486 sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 9487 return (SATA_FAILURE); 9488 } 9489 9490 mutex_enter(&pmportinfo->pmport_mutex); 9491 sata_show_drive_info(sata_hba_inst, 9492 SATA_PMPORTINFO_DRV_INFO(pmportinfo)); 9493 9494 if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) { 9495 /* 9496 * Could not determine device type. 9497 * Degrade this device to unknown. 9498 */ 9499 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN; 9500 mutex_exit(&pmportinfo->pmport_mutex); 9501 return (SATA_SUCCESS); 9502 } 9503 pmportinfo->pmport_dev_type = sata_device->satadev_type; 9504 pmportinfo->pmport_tgtnode_clean = B_TRUE; 9505 mutex_exit(&pmportinfo->pmport_mutex); 9506 9507 /* 9508 * Initialize device to the desired state. 9509 * Even if it fails, the device will still 9510 * attach but syslog will show the warning. 9511 */ 9512 if (sata_initialize_device(sata_hba_inst, 9513 pmportinfo->pmport_sata_drive) != SATA_SUCCESS) { 9514 /* Retry */ 9515 rval = sata_initialize_device(sata_hba_inst, 9516 pmportinfo->pmport_sata_drive); 9517 9518 if (rval == SATA_RETRY) 9519 sata_log(sata_hba_inst, CE_WARN, 9520 "SATA device at port %d:%d - " 9521 "default device features could not be set." 9522 " Device may not operate as expected.", 9523 cport, pmport); 9524 } 9525 9526 cdip = sata_create_target_node(pdip, sata_hba_inst, saddr); 9527 if (cdip == NULL) { 9528 /* 9529 * Attaching target node failed. 9530 * We retain sata_drive_info structure... 9531 */ 9532 return (SATA_SUCCESS); 9533 } 9534 mutex_enter(&pmportinfo->pmport_mutex); 9535 pmportinfo->pmport_sata_drive->satadrv_state |= 9536 SATA_STATE_READY; 9537 mutex_exit(&pmportinfo->pmport_mutex); 9538 9539 break; 9540 9541 default: 9542 return (SATA_FAILURE); 9543 } 9544 9545 return (SATA_SUCCESS); 9546 } 9547 9548 /* 9549 * Clean up target node at specific address. 9550 * 9551 * NOTE: No Mutex should be hold. 9552 */ 9553 static int 9554 sata_offline_device(sata_hba_inst_t *sata_hba_inst, 9555 sata_device_t *sata_device, sata_drive_info_t *sdinfo) 9556 { 9557 uint8_t cport, pmport, qual; 9558 dev_info_t *tdip; 9559 9560 cport = sata_device->satadev_addr.cport; 9561 pmport = sata_device->satadev_addr.pmport; 9562 qual = sata_device->satadev_addr.qual; 9563 9564 if (qual == SATA_ADDR_DCPORT) { 9565 SATA_LOG_D((sata_hba_inst, CE_WARN, 9566 "sata_hba_ioctl: disconnect device at port %d", cport)); 9567 } else { 9568 SATA_LOG_D((sata_hba_inst, CE_WARN, 9569 "sata_hba_ioctl: disconnect device at port %d:%d", 9570 cport, pmport)); 9571 } 9572 9573 /* We are addressing attached device, not a port */ 9574 sata_device->satadev_addr.qual = 9575 sdinfo->satadrv_addr.qual; 9576 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 9577 &sata_device->satadev_addr); 9578 if (tdip != NULL && ndi_devi_offline(tdip, 9579 NDI_DEVI_REMOVE) != NDI_SUCCESS) { 9580 /* 9581 * Problem : 9582 * The target node remained attached. 9583 * This happens when the device file was open 9584 * or a node was waiting for resources. 9585 * Cannot do anything about it. 9586 */ 9587 if (qual == SATA_ADDR_DCPORT) { 9588 SATA_LOG_D((sata_hba_inst, CE_WARN, 9589 "sata_hba_ioctl: disconnect: could " 9590 "not unconfigure device before " 9591 "disconnecting the SATA port %d", 9592 cport)); 9593 } else { 9594 SATA_LOG_D((sata_hba_inst, CE_WARN, 9595 "sata_hba_ioctl: disconnect: could " 9596 "not unconfigure device before " 9597 "disconnecting the SATA port %d:%d", 9598 cport, pmport)); 9599 } 9600 /* 9601 * Set DEVICE REMOVED state in the target 9602 * node. It will prevent access to the device 9603 * even when a new device is attached, until 9604 * the old target node is released, removed and 9605 * recreated for a new device. 9606 */ 9607 sata_set_device_removed(tdip); 9608 9609 /* 9610 * Instruct event daemon to try the target 9611 * node cleanup later. 9612 */ 9613 sata_set_target_node_cleanup( 9614 sata_hba_inst, &sata_device->satadev_addr); 9615 } 9616 9617 9618 return (SATA_SUCCESS); 9619 } 9620 9621 9622 /* 9623 * Create scsi target node for attached device, create node properties and 9624 * attach the node. 9625 * The node could be removed if the device onlining fails. 9626 * 9627 * A dev_info_t pointer is returned if operation is successful, NULL is 9628 * returned otherwise. 9629 */ 9630 9631 static dev_info_t * 9632 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst, 9633 sata_address_t *sata_addr) 9634 { 9635 dev_info_t *cdip = NULL; 9636 int rval; 9637 char *nname = NULL; 9638 char **compatible = NULL; 9639 int ncompatible; 9640 struct scsi_inquiry inq; 9641 sata_device_t sata_device; 9642 sata_drive_info_t *sdinfo; 9643 int target; 9644 int i; 9645 9646 sata_device.satadev_rev = SATA_DEVICE_REV; 9647 sata_device.satadev_addr = *sata_addr; 9648 9649 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport))); 9650 9651 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 9652 9653 target = SATA_TO_SCSI_TARGET(sata_addr->cport, 9654 sata_addr->pmport, sata_addr->qual); 9655 9656 if (sdinfo == NULL) { 9657 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9658 sata_addr->cport))); 9659 SATA_LOG_D((sata_hba_inst, CE_WARN, 9660 "sata_create_target_node: no sdinfo for target %x", 9661 target)); 9662 return (NULL); 9663 } 9664 9665 /* 9666 * create or get scsi inquiry data, expected by 9667 * scsi_hba_nodename_compatible_get() 9668 * SATA hard disks get Identify Data translated into Inguiry Data. 9669 * ATAPI devices respond directly to Inquiry request. 9670 */ 9671 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 9672 sata_identdev_to_inquiry(sata_hba_inst, sdinfo, 9673 (uint8_t *)&inq); 9674 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9675 sata_addr->cport))); 9676 } else { /* Assume supported ATAPI device */ 9677 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9678 sata_addr->cport))); 9679 if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr, 9680 &inq) == SATA_FAILURE) 9681 return (NULL); 9682 /* 9683 * Save supported ATAPI transport version 9684 */ 9685 sdinfo->satadrv_atapi_trans_ver = 9686 SATA_ATAPI_TRANS_VERSION(&inq); 9687 } 9688 9689 /* determine the node name and compatible */ 9690 scsi_hba_nodename_compatible_get(&inq, NULL, 9691 inq.inq_dtype, NULL, &nname, &compatible, &ncompatible); 9692 9693 #ifdef SATA_DEBUG 9694 if (sata_debug_flags & SATA_DBG_NODES) { 9695 if (nname == NULL) { 9696 cmn_err(CE_NOTE, "sata_create_target_node: " 9697 "cannot determine nodename for target %d\n", 9698 target); 9699 } else { 9700 cmn_err(CE_WARN, "sata_create_target_node: " 9701 "target %d nodename: %s\n", target, nname); 9702 } 9703 if (compatible == NULL) { 9704 cmn_err(CE_WARN, 9705 "sata_create_target_node: no compatible name\n"); 9706 } else { 9707 for (i = 0; i < ncompatible; i++) { 9708 cmn_err(CE_WARN, "sata_create_target_node: " 9709 "compatible name: %s\n", compatible[i]); 9710 } 9711 } 9712 } 9713 #endif 9714 9715 /* if nodename can't be determined, log error and exit */ 9716 if (nname == NULL) { 9717 SATA_LOG_D((sata_hba_inst, CE_WARN, 9718 "sata_create_target_node: cannot determine nodename " 9719 "for target %d\n", target)); 9720 scsi_hba_nodename_compatible_free(nname, compatible); 9721 return (NULL); 9722 } 9723 /* 9724 * Create scsi target node 9725 */ 9726 ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip); 9727 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 9728 "device-type", "scsi"); 9729 9730 if (rval != DDI_PROP_SUCCESS) { 9731 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 9732 "updating device_type prop failed %d", rval)); 9733 goto fail; 9734 } 9735 9736 /* 9737 * Create target node properties: target & lun 9738 */ 9739 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target); 9740 if (rval != DDI_PROP_SUCCESS) { 9741 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 9742 "updating target prop failed %d", rval)); 9743 goto fail; 9744 } 9745 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0); 9746 if (rval != DDI_PROP_SUCCESS) { 9747 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 9748 "updating target prop failed %d", rval)); 9749 goto fail; 9750 } 9751 9752 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 9753 /* 9754 * Add "variant" property 9755 */ 9756 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 9757 "variant", "atapi"); 9758 if (rval != DDI_PROP_SUCCESS) { 9759 SATA_LOG_D((sata_hba_inst, CE_WARN, 9760 "sata_create_target_node: variant atapi " 9761 "property could not be created: %d", rval)); 9762 goto fail; 9763 } 9764 } 9765 /* decorate the node with compatible */ 9766 if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible", 9767 compatible, ncompatible) != DDI_PROP_SUCCESS) { 9768 SATA_LOG_D((sata_hba_inst, CE_WARN, 9769 "sata_create_target_node: FAIL compatible props cdip 0x%p", 9770 (void *)cdip)); 9771 goto fail; 9772 } 9773 9774 9775 /* 9776 * Now, try to attach the driver. If probing of the device fails, 9777 * the target node may be removed 9778 */ 9779 rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH); 9780 9781 scsi_hba_nodename_compatible_free(nname, compatible); 9782 9783 if (rval == NDI_SUCCESS) 9784 return (cdip); 9785 9786 /* target node was removed - are we sure? */ 9787 return (NULL); 9788 9789 fail: 9790 scsi_hba_nodename_compatible_free(nname, compatible); 9791 ddi_prop_remove_all(cdip); 9792 rval = ndi_devi_free(cdip); 9793 if (rval != NDI_SUCCESS) { 9794 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 9795 "node removal failed %d", rval)); 9796 } 9797 sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: " 9798 "cannot create target node for SATA device at port %d", 9799 sata_addr->cport); 9800 return (NULL); 9801 } 9802 9803 /* 9804 * Remove a target node. 9805 */ 9806 static void 9807 sata_remove_target_node(sata_hba_inst_t *sata_hba_inst, 9808 sata_address_t *sata_addr) 9809 { 9810 dev_info_t *tdip; 9811 uint8_t cport = sata_addr->cport; 9812 uint8_t pmport = sata_addr->pmport; 9813 uint8_t qual = sata_addr->qual; 9814 9815 /* Note the sata daemon uses the address of the port/pmport */ 9816 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT); 9817 9818 /* Remove target node */ 9819 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), cport, pmport); 9820 if (tdip != NULL) { 9821 /* 9822 * Target node exists. Unconfigure device 9823 * then remove the target node (one ndi 9824 * operation). 9825 */ 9826 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) { 9827 /* 9828 * PROBLEM - no device, but target node remained. This 9829 * happens when the file was open or node was waiting 9830 * for resources. 9831 */ 9832 SATA_LOG_D((sata_hba_inst, CE_WARN, 9833 "sata_remove_target_node: " 9834 "Failed to remove target node for " 9835 "detached SATA device.")); 9836 /* 9837 * Set target node state to DEVI_DEVICE_REMOVED. But 9838 * re-check first that the node still exists. 9839 */ 9840 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 9841 cport, pmport); 9842 if (tdip != NULL) { 9843 sata_set_device_removed(tdip); 9844 /* 9845 * Instruct event daemon to retry the cleanup 9846 * later. 9847 */ 9848 sata_set_target_node_cleanup(sata_hba_inst, 9849 sata_addr); 9850 } 9851 } 9852 9853 if (qual == SATA_ADDR_CPORT) 9854 sata_log(sata_hba_inst, CE_WARN, 9855 "SATA device detached at port %d", cport); 9856 else 9857 sata_log(sata_hba_inst, CE_WARN, 9858 "SATA device detached at port %d:%d", 9859 cport, pmport); 9860 } 9861 #ifdef SATA_DEBUG 9862 else { 9863 if (qual == SATA_ADDR_CPORT) 9864 sata_log(sata_hba_inst, CE_WARN, 9865 "target node not found at port %d", cport); 9866 else 9867 sata_log(sata_hba_inst, CE_WARN, 9868 "target node not found at port %d:%d", 9869 cport, pmport); 9870 } 9871 #endif 9872 } 9873 9874 9875 /* 9876 * Re-probe sata port, check for a device and attach info 9877 * structures when necessary. Identify Device data is fetched, if possible. 9878 * Assumption: sata address is already validated. 9879 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of 9880 * the presence of a device and its type. 9881 * 9882 * flag arg specifies that the function should try multiple times to identify 9883 * device type and to initialize it, or it should return immediately on failure. 9884 * SATA_DEV_IDENTIFY_RETRY - retry 9885 * SATA_DEV_IDENTIFY_NORETRY - no retry 9886 * 9887 * SATA_FAILURE is returned if one of the operations failed. 9888 * 9889 * This function cannot be called in interrupt context - it may sleep. 9890 * 9891 * Note: Port multiplier is supported. 9892 */ 9893 static int 9894 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 9895 int flag) 9896 { 9897 sata_cport_info_t *cportinfo; 9898 sata_pmult_info_t *pmultinfo; 9899 sata_drive_info_t *sdinfo, *osdinfo; 9900 boolean_t init_device = B_FALSE; 9901 int prev_device_type = SATA_DTYPE_NONE; 9902 int prev_device_settings = 0; 9903 int prev_device_state = 0; 9904 clock_t start_time; 9905 int retry = B_FALSE; 9906 uint8_t cport = sata_device->satadev_addr.cport; 9907 int rval_probe, rval_init; 9908 9909 /* 9910 * If target is pmport, sata_reprobe_pmport() will handle it. 9911 */ 9912 if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT || 9913 sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) 9914 return (sata_reprobe_pmport(sata_hba_inst, sata_device, flag)); 9915 9916 /* We only care about host sata cport for now */ 9917 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 9918 sata_device->satadev_addr.cport); 9919 9920 /* 9921 * If a port multiplier was previously attached (we have no idea it 9922 * still there or not), sata_reprobe_pmult() will handle it. 9923 */ 9924 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) 9925 return (sata_reprobe_pmult(sata_hba_inst, sata_device, flag)); 9926 9927 /* Store sata_drive_info when a non-pmult device was attached. */ 9928 osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 9929 if (osdinfo != NULL) { 9930 /* 9931 * We are re-probing port with a previously attached device. 9932 * Save previous device type and settings. 9933 */ 9934 prev_device_type = cportinfo->cport_dev_type; 9935 prev_device_settings = osdinfo->satadrv_settings; 9936 prev_device_state = osdinfo->satadrv_state; 9937 } 9938 if (flag == SATA_DEV_IDENTIFY_RETRY) { 9939 start_time = ddi_get_lbolt(); 9940 retry = B_TRUE; 9941 } 9942 retry_probe: 9943 9944 /* probe port */ 9945 mutex_enter(&cportinfo->cport_mutex); 9946 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 9947 cportinfo->cport_state |= SATA_STATE_PROBING; 9948 mutex_exit(&cportinfo->cport_mutex); 9949 9950 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 9951 (SATA_DIP(sata_hba_inst), sata_device); 9952 9953 mutex_enter(&cportinfo->cport_mutex); 9954 if (rval_probe != SATA_SUCCESS) { 9955 cportinfo->cport_state = SATA_PSTATE_FAILED; 9956 mutex_exit(&cportinfo->cport_mutex); 9957 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: " 9958 "SATA port %d probing failed", 9959 cportinfo->cport_addr.cport)); 9960 return (SATA_FAILURE); 9961 } 9962 9963 /* 9964 * update sata port state and set device type 9965 */ 9966 sata_update_port_info(sata_hba_inst, sata_device); 9967 cportinfo->cport_state &= ~SATA_STATE_PROBING; 9968 9969 /* 9970 * Sanity check - Port is active? Is the link active? 9971 * Is there any device attached? 9972 */ 9973 if ((cportinfo->cport_state & 9974 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 9975 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 9976 SATA_PORT_DEVLINK_UP) { 9977 /* 9978 * Port in non-usable state or no link active/no device. 9979 * Free info structure if necessary (direct attached drive 9980 * only, for now! 9981 */ 9982 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 9983 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 9984 /* Add here differentiation for device attached or not */ 9985 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 9986 mutex_exit(&cportinfo->cport_mutex); 9987 if (sdinfo != NULL) 9988 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 9989 return (SATA_SUCCESS); 9990 } 9991 9992 cportinfo->cport_state |= SATA_STATE_READY; 9993 cportinfo->cport_state |= SATA_STATE_PROBED; 9994 9995 cportinfo->cport_dev_type = sata_device->satadev_type; 9996 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 9997 9998 /* 9999 * If we are re-probing the port, there may be 10000 * sata_drive_info structure attached 10001 */ 10002 if (sata_device->satadev_type == SATA_DTYPE_NONE) { 10003 10004 /* 10005 * There is no device, so remove device info structure, 10006 * if necessary. 10007 */ 10008 /* Device change: Drive -> None */ 10009 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 10010 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 10011 if (sdinfo != NULL) { 10012 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10013 sata_log(sata_hba_inst, CE_WARN, 10014 "SATA device detached " 10015 "from port %d", cportinfo->cport_addr.cport); 10016 } 10017 mutex_exit(&cportinfo->cport_mutex); 10018 return (SATA_SUCCESS); 10019 10020 } 10021 10022 if (sata_device->satadev_type != SATA_DTYPE_PMULT) { 10023 10024 /* Device (may) change: Drive -> Drive */ 10025 if (sdinfo == NULL) { 10026 /* 10027 * There is some device attached, but there is 10028 * no sata_drive_info structure - allocate one 10029 */ 10030 mutex_exit(&cportinfo->cport_mutex); 10031 sdinfo = kmem_zalloc( 10032 sizeof (sata_drive_info_t), KM_SLEEP); 10033 mutex_enter(&cportinfo->cport_mutex); 10034 /* 10035 * Recheck, that the port state did not change when we 10036 * released mutex. 10037 */ 10038 if (cportinfo->cport_state & SATA_STATE_READY) { 10039 SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo; 10040 sdinfo->satadrv_addr = cportinfo->cport_addr; 10041 sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT; 10042 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 10043 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 10044 } else { 10045 /* 10046 * Port is not in ready state, we 10047 * cannot attach a device. 10048 */ 10049 mutex_exit(&cportinfo->cport_mutex); 10050 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10051 return (SATA_SUCCESS); 10052 } 10053 /* 10054 * Since we are adding device, presumably new one, 10055 * indicate that it should be initalized, 10056 * as well as some internal framework states). 10057 */ 10058 init_device = B_TRUE; 10059 } 10060 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 10061 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual; 10062 } else { 10063 /* Device change: Drive -> PMult */ 10064 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 10065 if (sdinfo != NULL) { 10066 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10067 sata_log(sata_hba_inst, CE_WARN, 10068 "SATA device detached " 10069 "from port %d", cportinfo->cport_addr.cport); 10070 } 10071 10072 sata_log(sata_hba_inst, CE_WARN, 10073 "SATA port multiplier detected at port %d", 10074 cportinfo->cport_addr.cport); 10075 10076 mutex_exit(&cportinfo->cport_mutex); 10077 if (sata_alloc_pmult(sata_hba_inst, sata_device) != 10078 SATA_SUCCESS) 10079 return (SATA_FAILURE); 10080 sata_show_pmult_info(sata_hba_inst, sata_device); 10081 mutex_enter(&cportinfo->cport_mutex); 10082 10083 /* 10084 * Mark all the port multiplier port behind the port 10085 * multiplier behind with link events, so that the sata daemon 10086 * will update their status. 10087 */ 10088 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 10089 pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET; 10090 mutex_exit(&cportinfo->cport_mutex); 10091 return (SATA_SUCCESS); 10092 } 10093 mutex_exit(&cportinfo->cport_mutex); 10094 10095 /* 10096 * Figure out what kind of device we are really 10097 * dealing with. Failure of identifying device does not fail this 10098 * function. 10099 */ 10100 rval_probe = sata_probe_device(sata_hba_inst, sata_device); 10101 rval_init = SATA_FAILURE; 10102 mutex_enter(&cportinfo->cport_mutex); 10103 if (rval_probe == SATA_SUCCESS) { 10104 /* 10105 * If we are dealing with the same type of a device as before, 10106 * restore its settings flags. 10107 */ 10108 if (osdinfo != NULL && 10109 sata_device->satadev_type == prev_device_type) 10110 sdinfo->satadrv_settings = prev_device_settings; 10111 10112 mutex_exit(&cportinfo->cport_mutex); 10113 rval_init = SATA_SUCCESS; 10114 /* Set initial device features, if necessary */ 10115 if (init_device == B_TRUE) { 10116 rval_init = sata_initialize_device(sata_hba_inst, 10117 sdinfo); 10118 } 10119 if (rval_init == SATA_SUCCESS) 10120 return (rval_init); 10121 /* else we will retry if retry was asked for */ 10122 10123 } else { 10124 /* 10125 * If there was some device info before we probe the device, 10126 * restore previous device setting, so we can retry from scratch 10127 * later. Providing, of course, that device has not disapear 10128 * during probing process. 10129 */ 10130 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 10131 if (osdinfo != NULL) { 10132 cportinfo->cport_dev_type = prev_device_type; 10133 sdinfo->satadrv_type = prev_device_type; 10134 sdinfo->satadrv_state = prev_device_state; 10135 } 10136 } else { 10137 /* device is gone */ 10138 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10139 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 10140 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 10141 mutex_exit(&cportinfo->cport_mutex); 10142 return (SATA_SUCCESS); 10143 } 10144 mutex_exit(&cportinfo->cport_mutex); 10145 } 10146 10147 if (retry) { 10148 clock_t cur_time = ddi_get_lbolt(); 10149 /* 10150 * A device was not successfully identified or initialized. 10151 * Track retry time for device identification. 10152 */ 10153 if ((cur_time - start_time) < 10154 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 10155 /* sleep for a while */ 10156 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 10157 goto retry_probe; 10158 } 10159 /* else no more retries */ 10160 mutex_enter(&cportinfo->cport_mutex); 10161 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 10162 if (rval_init == SATA_RETRY) { 10163 /* 10164 * Setting drive features have failed, but 10165 * because the drive is still accessible, 10166 * keep it and emit a warning message. 10167 */ 10168 sata_log(sata_hba_inst, CE_WARN, 10169 "SATA device at port %d - desired " 10170 "drive features could not be set. " 10171 "Device may not operate as expected.", 10172 cportinfo->cport_addr.cport); 10173 } else { 10174 SATA_CPORTINFO_DRV_INFO(cportinfo)-> 10175 satadrv_state = SATA_DSTATE_FAILED; 10176 } 10177 } 10178 mutex_exit(&cportinfo->cport_mutex); 10179 } 10180 return (SATA_SUCCESS); 10181 } 10182 10183 /* 10184 * Reprobe a controller port that connected to a port multiplier. 10185 * 10186 * NOTE: No Mutex should be hold. 10187 */ 10188 static int 10189 sata_reprobe_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 10190 int flag) 10191 { 10192 _NOTE(ARGUNUSED(flag)) 10193 sata_cport_info_t *cportinfo; 10194 sata_pmult_info_t *pmultinfo; 10195 uint8_t cport = sata_device->satadev_addr.cport; 10196 int rval_probe; 10197 10198 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 10199 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 10200 10201 /* probe port */ 10202 mutex_enter(&cportinfo->cport_mutex); 10203 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 10204 cportinfo->cport_state |= SATA_STATE_PROBING; 10205 mutex_exit(&cportinfo->cport_mutex); 10206 10207 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 10208 (SATA_DIP(sata_hba_inst), sata_device); 10209 10210 mutex_enter(&cportinfo->cport_mutex); 10211 if (rval_probe != SATA_SUCCESS) { 10212 cportinfo->cport_state = SATA_PSTATE_FAILED; 10213 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmult: " 10214 "SATA port %d probing failed", cport)); 10215 sata_log(sata_hba_inst, CE_WARN, 10216 "SATA port multiplier detached at port %d", cport); 10217 mutex_exit(&cportinfo->cport_mutex); 10218 sata_free_pmult(sata_hba_inst, sata_device); 10219 return (SATA_FAILURE); 10220 } 10221 10222 /* 10223 * update sata port state and set device type 10224 */ 10225 sata_update_port_info(sata_hba_inst, sata_device); 10226 cportinfo->cport_state &= ~SATA_STATE_PROBING; 10227 cportinfo->cport_state |= SATA_STATE_PROBED; 10228 10229 /* 10230 * Sanity check - Port is active? Is the link active? 10231 * Is there any device attached? 10232 */ 10233 if ((cportinfo->cport_state & 10234 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 10235 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 10236 SATA_PORT_DEVLINK_UP || 10237 (sata_device->satadev_type == SATA_DTYPE_NONE)) { 10238 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 10239 mutex_exit(&cportinfo->cport_mutex); 10240 sata_free_pmult(sata_hba_inst, sata_device); 10241 sata_log(sata_hba_inst, CE_WARN, 10242 "SATA port multiplier detached at port %d", cport); 10243 return (SATA_SUCCESS); 10244 } 10245 10246 /* 10247 * Device changed: PMult -> Non-PMult 10248 * 10249 * This situation is uncommon, most possibly being caused by errors 10250 * after which the port multiplier is not correct initialized and 10251 * recognized. In that case the new device will be marked as unknown 10252 * and will not be automatically probed in this routine. Instead 10253 * system administrator could manually restart it via cfgadm(1M). 10254 */ 10255 if (sata_device->satadev_type != SATA_DTYPE_PMULT) { 10256 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 10257 mutex_exit(&cportinfo->cport_mutex); 10258 sata_free_pmult(sata_hba_inst, sata_device); 10259 sata_log(sata_hba_inst, CE_WARN, 10260 "SATA port multiplier detached at port %d", cport); 10261 return (SATA_FAILURE); 10262 } 10263 10264 /* 10265 * Now we know it is a port multiplier. However, if this is not the 10266 * previously attached port multiplier - they may have different 10267 * pmport numbers - we need to re-allocate data structures for every 10268 * pmport and drive. 10269 * 10270 * Port multipliers of the same model have identical values in these 10271 * registers, so it is still necessary to update the information of 10272 * all drives attached to the previous port multiplier afterwards. 10273 */ 10274 /* Device changed: PMult -> another PMult */ 10275 mutex_exit(&cportinfo->cport_mutex); 10276 sata_free_pmult(sata_hba_inst, sata_device); 10277 if (sata_alloc_pmult(sata_hba_inst, sata_device) != SATA_SUCCESS) 10278 return (SATA_FAILURE); 10279 mutex_enter(&cportinfo->cport_mutex); 10280 10281 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 10282 "SATA port multiplier [changed] at port %d", cport); 10283 sata_log(sata_hba_inst, CE_WARN, 10284 "SATA port multiplier detected at port %d", cport); 10285 10286 /* 10287 * Mark all the port multiplier port behind the port 10288 * multiplier behind with link events, so that the sata daemon 10289 * will update their status. 10290 */ 10291 pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET; 10292 mutex_exit(&cportinfo->cport_mutex); 10293 10294 return (SATA_SUCCESS); 10295 } 10296 10297 /* 10298 * Re-probe a port multiplier port, check for a device and attach info 10299 * structures when necessary. Identify Device data is fetched, if possible. 10300 * Assumption: sata address is already validated as port multiplier port. 10301 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of 10302 * the presence of a device and its type. 10303 * 10304 * flag arg specifies that the function should try multiple times to identify 10305 * device type and to initialize it, or it should return immediately on failure. 10306 * SATA_DEV_IDENTIFY_RETRY - retry 10307 * SATA_DEV_IDENTIFY_NORETRY - no retry 10308 * 10309 * SATA_FAILURE is returned if one of the operations failed. 10310 * 10311 * This function cannot be called in interrupt context - it may sleep. 10312 * 10313 * NOTE: Should be only called by sata_probe_port() in case target port is a 10314 * port multiplier port. 10315 * NOTE: No Mutex should be hold. 10316 */ 10317 static int 10318 sata_reprobe_pmport(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 10319 int flag) 10320 { 10321 sata_cport_info_t *cportinfo = NULL; 10322 sata_pmport_info_t *pmportinfo = NULL; 10323 sata_drive_info_t *sdinfo, *osdinfo; 10324 sata_device_t sdevice; 10325 boolean_t init_device = B_FALSE; 10326 int prev_device_type = SATA_DTYPE_NONE; 10327 int prev_device_settings = 0; 10328 int prev_device_state = 0; 10329 clock_t start_time; 10330 uint8_t cport = sata_device->satadev_addr.cport; 10331 uint8_t pmport = sata_device->satadev_addr.pmport; 10332 int rval; 10333 10334 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 10335 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 10336 osdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 10337 10338 if (osdinfo != NULL) { 10339 /* 10340 * We are re-probing port with a previously attached device. 10341 * Save previous device type and settings. 10342 */ 10343 prev_device_type = pmportinfo->pmport_dev_type; 10344 prev_device_settings = osdinfo->satadrv_settings; 10345 prev_device_state = osdinfo->satadrv_state; 10346 } 10347 10348 start_time = ddi_get_lbolt(); 10349 10350 /* check parent status */ 10351 mutex_enter(&cportinfo->cport_mutex); 10352 if ((cportinfo->cport_state & 10353 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 10354 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 10355 SATA_PORT_DEVLINK_UP) { 10356 mutex_exit(&cportinfo->cport_mutex); 10357 return (SATA_FAILURE); 10358 } 10359 mutex_exit(&cportinfo->cport_mutex); 10360 10361 retry_probe_pmport: 10362 10363 /* probe port */ 10364 mutex_enter(&pmportinfo->pmport_mutex); 10365 pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 10366 pmportinfo->pmport_state |= SATA_STATE_PROBING; 10367 mutex_exit(&pmportinfo->pmport_mutex); 10368 10369 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 10370 (SATA_DIP(sata_hba_inst), sata_device); 10371 10372 /* might need retry because we cannot touch registers. */ 10373 if (rval == SATA_FAILURE) { 10374 mutex_enter(&pmportinfo->pmport_mutex); 10375 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 10376 mutex_exit(&pmportinfo->pmport_mutex); 10377 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: " 10378 "SATA port %d:%d probing failed", 10379 cport, pmport)); 10380 return (SATA_FAILURE); 10381 } else if (rval == SATA_RETRY) { 10382 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: " 10383 "SATA port %d:%d probing failed, retrying...", 10384 cport, pmport)); 10385 clock_t cur_time = ddi_get_lbolt(); 10386 /* 10387 * A device was not successfully identified or initialized. 10388 * Track retry time for device identification. 10389 */ 10390 if ((cur_time - start_time) < 10391 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 10392 /* sleep for a while */ 10393 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 10394 goto retry_probe_pmport; 10395 } else { 10396 mutex_enter(&pmportinfo->pmport_mutex); 10397 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) 10398 SATA_PMPORTINFO_DRV_INFO(pmportinfo)-> 10399 satadrv_state = SATA_DSTATE_FAILED; 10400 mutex_exit(&pmportinfo->pmport_mutex); 10401 return (SATA_SUCCESS); 10402 } 10403 } 10404 10405 /* 10406 * Sanity check - Controller port is active? Is the link active? 10407 * Is it still a port multiplier? 10408 */ 10409 if ((cportinfo->cport_state & 10410 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 10411 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 10412 SATA_PORT_DEVLINK_UP || 10413 (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) { 10414 /* 10415 * Port in non-usable state or no link active/no 10416 * device. Free info structure. 10417 */ 10418 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 10419 10420 sdevice.satadev_addr.cport = cport; 10421 sdevice.satadev_addr.pmport = pmport; 10422 sdevice.satadev_addr.qual = SATA_ADDR_PMULT; 10423 mutex_exit(&cportinfo->cport_mutex); 10424 10425 sata_free_pmult(sata_hba_inst, &sdevice); 10426 return (SATA_FAILURE); 10427 } 10428 10429 /* SATA_SUCCESS NOW */ 10430 /* 10431 * update sata port state and set device type 10432 */ 10433 mutex_enter(&pmportinfo->pmport_mutex); 10434 sata_update_pmport_info(sata_hba_inst, sata_device); 10435 pmportinfo->pmport_state &= ~SATA_STATE_PROBING; 10436 10437 /* 10438 * Sanity check - Port is active? Is the link active? 10439 * Is there any device attached? 10440 */ 10441 if ((pmportinfo->pmport_state & 10442 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 10443 (pmportinfo->pmport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 10444 SATA_PORT_DEVLINK_UP) { 10445 /* 10446 * Port in non-usable state or no link active/no device. 10447 * Free info structure if necessary (direct attached drive 10448 * only, for now! 10449 */ 10450 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 10451 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 10452 /* Add here differentiation for device attached or not */ 10453 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 10454 mutex_exit(&pmportinfo->pmport_mutex); 10455 if (sdinfo != NULL) 10456 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10457 return (SATA_SUCCESS); 10458 } 10459 10460 pmportinfo->pmport_state |= SATA_STATE_READY; 10461 pmportinfo->pmport_dev_type = sata_device->satadev_type; 10462 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 10463 10464 /* 10465 * If we are re-probing the port, there may be 10466 * sata_drive_info structure attached 10467 * (or sata_pm_info, if PMult is supported). 10468 */ 10469 if (sata_device->satadev_type == SATA_DTYPE_NONE) { 10470 /* 10471 * There is no device, so remove device info structure, 10472 * if necessary. 10473 */ 10474 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 10475 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 10476 if (sdinfo != NULL) { 10477 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10478 sata_log(sata_hba_inst, CE_WARN, 10479 "SATA device detached from port %d:%d", 10480 cport, pmport); 10481 } 10482 mutex_exit(&pmportinfo->pmport_mutex); 10483 return (SATA_SUCCESS); 10484 } 10485 10486 /* this should not be a pmult */ 10487 ASSERT(sata_device->satadev_type != SATA_DTYPE_PMULT); 10488 if (sdinfo == NULL) { 10489 /* 10490 * There is some device attached, but there is 10491 * no sata_drive_info structure - allocate one 10492 */ 10493 mutex_exit(&pmportinfo->pmport_mutex); 10494 sdinfo = kmem_zalloc(sizeof (sata_drive_info_t), 10495 KM_SLEEP); 10496 mutex_enter(&pmportinfo->pmport_mutex); 10497 /* 10498 * Recheck, that the port state did not change when we 10499 * released mutex. 10500 */ 10501 if (pmportinfo->pmport_state & SATA_STATE_READY) { 10502 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = sdinfo; 10503 sdinfo->satadrv_addr = pmportinfo->pmport_addr; 10504 sdinfo->satadrv_addr.qual = SATA_ADDR_DPMPORT; 10505 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 10506 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 10507 } else { 10508 /* 10509 * Port is not in ready state, we 10510 * cannot attach a device. 10511 */ 10512 mutex_exit(&pmportinfo->pmport_mutex); 10513 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10514 return (SATA_SUCCESS); 10515 } 10516 /* 10517 * Since we are adding device, presumably new one, 10518 * indicate that it should be initalized, 10519 * as well as some internal framework states). 10520 */ 10521 init_device = B_TRUE; 10522 } 10523 10524 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN; 10525 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual; 10526 10527 mutex_exit(&pmportinfo->pmport_mutex); 10528 /* 10529 * Figure out what kind of device we are really 10530 * dealing with. 10531 */ 10532 rval = sata_probe_device(sata_hba_inst, sata_device); 10533 10534 mutex_enter(&pmportinfo->pmport_mutex); 10535 if (rval == SATA_SUCCESS) { 10536 /* 10537 * If we are dealing with the same type of a device as before, 10538 * restore its settings flags. 10539 */ 10540 if (osdinfo != NULL && 10541 sata_device->satadev_type == prev_device_type) 10542 sdinfo->satadrv_settings = prev_device_settings; 10543 10544 mutex_exit(&pmportinfo->pmport_mutex); 10545 /* Set initial device features, if necessary */ 10546 if (init_device == B_TRUE) { 10547 rval = sata_initialize_device(sata_hba_inst, sdinfo); 10548 } 10549 if (rval == SATA_SUCCESS) 10550 return (rval); 10551 } else { 10552 /* 10553 * If there was some device info before we probe the device, 10554 * restore previous device setting, so we can retry from scratch 10555 * later. Providing, of course, that device has not disappeared 10556 * during probing process. 10557 */ 10558 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 10559 if (osdinfo != NULL) { 10560 pmportinfo->pmport_dev_type = prev_device_type; 10561 sdinfo->satadrv_type = prev_device_type; 10562 sdinfo->satadrv_state = prev_device_state; 10563 } 10564 } else { 10565 /* device is gone */ 10566 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10567 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 10568 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 10569 mutex_exit(&pmportinfo->pmport_mutex); 10570 return (SATA_SUCCESS); 10571 } 10572 mutex_exit(&pmportinfo->pmport_mutex); 10573 } 10574 10575 if (flag == SATA_DEV_IDENTIFY_RETRY) { 10576 clock_t cur_time = ddi_get_lbolt(); 10577 /* 10578 * A device was not successfully identified or initialized. 10579 * Track retry time for device identification. 10580 */ 10581 if ((cur_time - start_time) < 10582 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 10583 /* sleep for a while */ 10584 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 10585 goto retry_probe_pmport; 10586 } else { 10587 mutex_enter(&pmportinfo->pmport_mutex); 10588 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) 10589 SATA_PMPORTINFO_DRV_INFO(pmportinfo)-> 10590 satadrv_state = SATA_DSTATE_FAILED; 10591 mutex_exit(&pmportinfo->pmport_mutex); 10592 } 10593 } 10594 return (SATA_SUCCESS); 10595 } 10596 10597 /* 10598 * Allocated related structure for a port multiplier and its device ports 10599 * 10600 * Port multiplier should be ready and probed, and related information like 10601 * the number of the device ports should be store in sata_device_t. 10602 * 10603 * NOTE: No Mutex should be hold. 10604 */ 10605 static int 10606 sata_alloc_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 10607 { 10608 dev_info_t *dip = SATA_DIP(sata_hba_inst); 10609 sata_cport_info_t *cportinfo = NULL; 10610 sata_pmult_info_t *pmultinfo = NULL; 10611 sata_pmport_info_t *pmportinfo = NULL; 10612 sata_device_t sd; 10613 dev_t minor_number; 10614 char name[16]; 10615 uint8_t cport = sata_device->satadev_addr.cport; 10616 int rval; 10617 int npmport; 10618 10619 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 10620 10621 /* This function might be called while a port-mult is hot-plugged. */ 10622 mutex_enter(&cportinfo->cport_mutex); 10623 10624 /* dev_type's not updated when get called from sata_reprobe_port() */ 10625 cportinfo->cport_dev_type = SATA_DTYPE_PMULT; 10626 if (SATA_CPORTINFO_PMULT_INFO(cportinfo) == NULL) { 10627 /* Create a pmult_info structure */ 10628 SATA_CPORTINFO_PMULT_INFO(cportinfo) = 10629 kmem_zalloc(sizeof (sata_pmult_info_t), KM_SLEEP); 10630 } 10631 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 10632 10633 pmultinfo->pmult_addr = sata_device->satadev_addr; 10634 pmultinfo->pmult_addr.qual = SATA_ADDR_PMULT; 10635 pmultinfo->pmult_state = SATA_STATE_PROBING; 10636 10637 /* 10638 * Probe the port multiplier with qualifier SATA_ADDR_PMULT_SPEC, 10639 * The HBA driver should initialize and register the port multiplier, 10640 * sata_register_pmult() will fill following fields, 10641 * + sata_pmult_info.pmult_gscr 10642 * + sata_pmult_info.pmult_num_dev_ports 10643 */ 10644 sd.satadev_addr = sata_device->satadev_addr; 10645 sd.satadev_addr.qual = SATA_ADDR_PMULT_SPEC; 10646 mutex_exit(&cportinfo->cport_mutex); 10647 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 10648 (SATA_DIP(sata_hba_inst), &sd); 10649 mutex_enter(&cportinfo->cport_mutex); 10650 10651 if (rval != SATA_SUCCESS || 10652 (sd.satadev_type != SATA_DTYPE_PMULT) || 10653 !(sd.satadev_state & SATA_DSTATE_PMULT_INIT)) { 10654 SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL; 10655 kmem_free(pmultinfo, sizeof (sata_pmult_info_t)); 10656 cportinfo->cport_state = SATA_PSTATE_FAILED; 10657 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 10658 mutex_exit(&cportinfo->cport_mutex); 10659 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 10660 "sata_alloc_pmult: failed to initialize pmult " 10661 "at port %d.", cport) 10662 return (SATA_FAILURE); 10663 } 10664 10665 /* Initialize pmport_info structure */ 10666 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; 10667 npmport++) { 10668 10669 /* if everything is allocated, skip */ 10670 if (SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) != NULL) 10671 continue; 10672 10673 pmportinfo = kmem_zalloc(sizeof (sata_pmport_info_t), KM_SLEEP); 10674 mutex_init(&pmportinfo->pmport_mutex, NULL, MUTEX_DRIVER, NULL); 10675 mutex_exit(&cportinfo->cport_mutex); 10676 10677 mutex_enter(&pmportinfo->pmport_mutex); 10678 pmportinfo->pmport_addr.cport = cport; 10679 pmportinfo->pmport_addr.pmport = (uint8_t)npmport; 10680 pmportinfo->pmport_addr.qual = SATA_ADDR_PMPORT; 10681 pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 10682 mutex_exit(&pmportinfo->pmport_mutex); 10683 10684 mutex_enter(&cportinfo->cport_mutex); 10685 SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) = pmportinfo; 10686 10687 /* Create an attachment point */ 10688 minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip), 10689 cport, (uint8_t)npmport, SATA_ADDR_PMPORT); 10690 (void) sprintf(name, "%d.%d", cport, npmport); 10691 10692 if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number, 10693 DDI_NT_SATA_ATTACHMENT_POINT, 0) != DDI_SUCCESS) { 10694 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: " 10695 "cannot create SATA attachment point for " 10696 "port %d:%d", cport, npmport); 10697 } 10698 } 10699 10700 pmultinfo->pmult_state &= ~SATA_STATE_PROBING; 10701 pmultinfo->pmult_state |= (SATA_STATE_PROBED|SATA_STATE_READY); 10702 10703 mutex_exit(&cportinfo->cport_mutex); 10704 return (SATA_SUCCESS); 10705 } 10706 10707 /* 10708 * Free data structures when a port multiplier is removed. 10709 * 10710 * NOTE: No Mutex should be hold. 10711 */ 10712 static void 10713 sata_free_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 10714 { 10715 sata_cport_info_t *cportinfo; 10716 sata_pmult_info_t *pmultinfo; 10717 sata_pmport_info_t *pmportinfo; 10718 sata_device_t pmport_device; 10719 sata_drive_info_t *sdinfo; 10720 dev_info_t *tdip; 10721 char name[16]; 10722 uint8_t cport = sata_device->satadev_addr.cport; 10723 int npmport; 10724 10725 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 10726 10727 /* This function might be called while port-mult is hot plugged. */ 10728 mutex_enter(&cportinfo->cport_mutex); 10729 10730 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 10731 ASSERT(pmultinfo != NULL); 10732 10733 /* Free pmport_info structure */ 10734 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; 10735 npmport++) { 10736 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport); 10737 if (pmportinfo == NULL) 10738 continue; 10739 mutex_exit(&cportinfo->cport_mutex); 10740 10741 mutex_enter(&pmportinfo->pmport_mutex); 10742 sdinfo = pmportinfo->pmport_sata_drive; 10743 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 10744 mutex_exit(&pmportinfo->pmport_mutex); 10745 10746 /* Remove attachment point. */ 10747 name[0] = '\0'; 10748 (void) sprintf(name, "%d.%d", cport, npmport); 10749 ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name); 10750 sata_log(sata_hba_inst, CE_NOTE, 10751 "Remove attachment point of port %d:%d", 10752 cport, npmport); 10753 10754 /* 10755 * Rumove target node 10756 */ 10757 bzero(&pmport_device, sizeof (sata_device_t)); 10758 pmport_device.satadev_rev = SATA_DEVICE_REV; 10759 pmport_device.satadev_addr.cport = cport; 10760 pmport_device.satadev_addr.pmport = (uint8_t)npmport; 10761 pmport_device.satadev_addr.qual = SATA_ADDR_DPMPORT; 10762 10763 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 10764 &(pmport_device.satadev_addr)); 10765 if (tdip != NULL && ndi_devi_offline(tdip, 10766 NDI_DEVI_REMOVE) != NDI_SUCCESS) { 10767 /* 10768 * Problem : 10769 * The target node remained attached. 10770 * This happens when the device file was open 10771 * or a node was waiting for resources. 10772 * Cannot do anything about it. 10773 */ 10774 SATA_LOG_D((sata_hba_inst, CE_WARN, 10775 "sata_free_pmult: could not unconfigure device " 10776 "before disconnecting the SATA port %d:%d", 10777 cport, npmport)); 10778 10779 /* 10780 * Set DEVICE REMOVED state in the target 10781 * node. It will prevent access to the device 10782 * even when a new device is attached, until 10783 * the old target node is released, removed and 10784 * recreated for a new device. 10785 */ 10786 sata_set_device_removed(tdip); 10787 10788 /* 10789 * Instruct event daemon to try the target 10790 * node cleanup later. 10791 */ 10792 sata_set_target_node_cleanup( 10793 sata_hba_inst, &(pmport_device.satadev_addr)); 10794 10795 } 10796 mutex_enter(&cportinfo->cport_mutex); 10797 10798 /* 10799 * Add here differentiation for device attached or not 10800 */ 10801 if (sdinfo != NULL) { 10802 sata_log(sata_hba_inst, CE_WARN, 10803 "SATA device detached from port %d:%d", 10804 cport, npmport); 10805 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 10806 } 10807 10808 mutex_destroy(&pmportinfo->pmport_mutex); 10809 kmem_free(pmportinfo, sizeof (sata_pmport_info_t)); 10810 } 10811 10812 kmem_free(pmultinfo, sizeof (sata_pmult_info_t)); 10813 10814 cportinfo->cport_devp.cport_sata_pmult = NULL; 10815 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 10816 10817 sata_log(sata_hba_inst, CE_WARN, 10818 "SATA port multiplier detached at port %d", cport); 10819 10820 mutex_exit(&cportinfo->cport_mutex); 10821 } 10822 10823 /* 10824 * Initialize device 10825 * Specified device is initialized to a default state. 10826 * 10827 * Returns SATA_SUCCESS if all device features are set successfully, 10828 * SATA_RETRY if device is accessible but device features were not set 10829 * successfully, and SATA_FAILURE otherwise. 10830 */ 10831 static int 10832 sata_initialize_device(sata_hba_inst_t *sata_hba_inst, 10833 sata_drive_info_t *sdinfo) 10834 { 10835 int rval; 10836 10837 sata_save_drive_settings(sdinfo); 10838 10839 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 10840 10841 sata_init_write_cache_mode(sdinfo); 10842 10843 rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0); 10844 10845 /* Determine current data transfer mode */ 10846 if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) { 10847 sdinfo->satadrv_settings &= ~SATA_DEV_DMA; 10848 } else if ((sdinfo->satadrv_id.ai_validinfo & 10849 SATA_VALIDINFO_88) != 0 && 10850 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) { 10851 sdinfo->satadrv_settings |= SATA_DEV_DMA; 10852 } else if ((sdinfo->satadrv_id.ai_dworddma & 10853 SATA_MDMA_SEL_MASK) != 0) { 10854 sdinfo->satadrv_settings |= SATA_DEV_DMA; 10855 } else 10856 /* DMA supported, not no DMA transfer mode is selected !? */ 10857 sdinfo->satadrv_settings &= ~SATA_DEV_DMA; 10858 10859 if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) && 10860 (sdinfo->satadrv_id.ai_features86 & 0x20)) 10861 sdinfo->satadrv_power_level = SATA_POWER_STANDBY; 10862 else 10863 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 10864 10865 return (rval); 10866 } 10867 10868 10869 /* 10870 * Initialize write cache mode. 10871 * 10872 * The default write cache setting for SATA HDD is provided by sata_write_cache 10873 * static variable. ATAPI CD/DVDs devices have write cache default is 10874 * determined by sata_atapicdvd_write_cache static variable. 10875 * ATAPI tape devices have write cache default is determined by 10876 * sata_atapitape_write_cache static variable. 10877 * ATAPI disk devices have write cache default is determined by 10878 * sata_atapidisk_write_cache static variable. 10879 * 1 - enable 10880 * 0 - disable 10881 * any other value - current drive setting 10882 * 10883 * Although there is not reason to disable write cache on CD/DVD devices, 10884 * tape devices and ATAPI disk devices, the default setting control is provided 10885 * for the maximun flexibility. 10886 * 10887 * In the future, it may be overridden by the 10888 * disk-write-cache-enable property setting, if it is defined. 10889 * Returns SATA_SUCCESS if all device features are set successfully, 10890 * SATA_FAILURE otherwise. 10891 */ 10892 static void 10893 sata_init_write_cache_mode(sata_drive_info_t *sdinfo) 10894 { 10895 switch (sdinfo->satadrv_type) { 10896 case SATA_DTYPE_ATADISK: 10897 if (sata_write_cache == 1) 10898 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 10899 else if (sata_write_cache == 0) 10900 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 10901 /* 10902 * When sata_write_cache value is not 0 or 1, 10903 * a current setting of the drive's write cache is used. 10904 */ 10905 break; 10906 case SATA_DTYPE_ATAPICD: 10907 if (sata_atapicdvd_write_cache == 1) 10908 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 10909 else if (sata_atapicdvd_write_cache == 0) 10910 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 10911 /* 10912 * When sata_atapicdvd_write_cache value is not 0 or 1, 10913 * a current setting of the drive's write cache is used. 10914 */ 10915 break; 10916 case SATA_DTYPE_ATAPITAPE: 10917 if (sata_atapitape_write_cache == 1) 10918 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 10919 else if (sata_atapitape_write_cache == 0) 10920 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 10921 /* 10922 * When sata_atapitape_write_cache value is not 0 or 1, 10923 * a current setting of the drive's write cache is used. 10924 */ 10925 break; 10926 case SATA_DTYPE_ATAPIDISK: 10927 if (sata_atapidisk_write_cache == 1) 10928 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 10929 else if (sata_atapidisk_write_cache == 0) 10930 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 10931 /* 10932 * When sata_atapidisk_write_cache value is not 0 or 1, 10933 * a current setting of the drive's write cache is used. 10934 */ 10935 break; 10936 } 10937 } 10938 10939 10940 /* 10941 * Validate sata address. 10942 * Specified cport, pmport and qualifier has to match 10943 * passed sata_scsi configuration info. 10944 * The presence of an attached device is not verified. 10945 * 10946 * Returns 0 when address is valid, -1 otherwise. 10947 */ 10948 static int 10949 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport, 10950 int pmport, int qual) 10951 { 10952 if (qual == SATA_ADDR_DCPORT && pmport != 0) 10953 goto invalid_address; 10954 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 10955 goto invalid_address; 10956 if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) && 10957 ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) || 10958 (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) || 10959 (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport)))) 10960 goto invalid_address; 10961 10962 return (0); 10963 10964 invalid_address: 10965 return (-1); 10966 10967 } 10968 10969 /* 10970 * Validate scsi address 10971 * SCSI target address is translated into SATA cport/pmport and compared 10972 * with a controller port/device configuration. LUN has to be 0. 10973 * Returns 0 if a scsi target refers to an attached device, 10974 * returns 1 if address is valid but no valid device is attached, 10975 * returns 2 if address is valid but device type is unknown (not valid device), 10976 * returns -1 if bad address or device is of an unsupported type. 10977 * Upon return sata_device argument is set. 10978 * 10979 * Port multiplier is supported now. 10980 */ 10981 static int 10982 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst, 10983 struct scsi_address *ap, sata_device_t *sata_device) 10984 { 10985 int cport, pmport, qual, rval; 10986 10987 rval = -1; /* Invalid address */ 10988 if (ap->a_lun != 0) 10989 goto out; 10990 10991 qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 10992 cport = SCSI_TO_SATA_CPORT(ap->a_target); 10993 pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 10994 10995 if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT) 10996 goto out; 10997 10998 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) == 10999 0) { 11000 11001 sata_cport_info_t *cportinfo; 11002 sata_pmult_info_t *pmultinfo; 11003 sata_drive_info_t *sdinfo = NULL; 11004 11005 sata_device->satadev_addr.qual = qual; 11006 sata_device->satadev_addr.cport = cport; 11007 sata_device->satadev_addr.pmport = pmport; 11008 sata_device->satadev_rev = SATA_DEVICE_REV_1; 11009 11010 rval = 1; /* Valid sata address */ 11011 11012 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11013 if (qual == SATA_ADDR_DCPORT) { 11014 if (cportinfo == NULL || 11015 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 11016 goto out; 11017 11018 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 11019 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN && 11020 sdinfo != NULL) { 11021 rval = 2; 11022 goto out; 11023 } 11024 11025 if ((cportinfo->cport_dev_type & 11026 SATA_VALID_DEV_TYPE) == 0) { 11027 rval = -1; 11028 goto out; 11029 } 11030 11031 } else if (qual == SATA_ADDR_DPMPORT) { 11032 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 11033 if (pmultinfo == NULL) { 11034 rval = -1; 11035 goto out; 11036 } 11037 if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) == 11038 NULL || 11039 SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 11040 pmport) == SATA_DTYPE_NONE) 11041 goto out; 11042 11043 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, 11044 pmport); 11045 if (SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 11046 pmport) == SATA_DTYPE_UNKNOWN && sdinfo != NULL) { 11047 rval = 2; 11048 goto out; 11049 } 11050 11051 if ((SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 11052 pmport) && SATA_VALID_DEV_TYPE) == 0) { 11053 rval = -1; 11054 goto out; 11055 } 11056 11057 } else { 11058 rval = -1; 11059 goto out; 11060 } 11061 if ((sdinfo == NULL) || 11062 (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0) 11063 goto out; 11064 11065 sata_device->satadev_type = sdinfo->satadrv_type; 11066 11067 return (0); 11068 } 11069 out: 11070 if (rval > 0) { 11071 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 11072 "sata_validate_scsi_address: no valid target %x lun %x", 11073 ap->a_target, ap->a_lun); 11074 } 11075 return (rval); 11076 } 11077 11078 /* 11079 * Find dip corresponding to passed device number 11080 * 11081 * Returns NULL if invalid device number is passed or device cannot be found, 11082 * Returns dip is device is found. 11083 */ 11084 static dev_info_t * 11085 sata_devt_to_devinfo(dev_t dev) 11086 { 11087 dev_info_t *dip; 11088 #ifndef __lock_lint 11089 struct devnames *dnp; 11090 major_t major = getmajor(dev); 11091 int instance = SATA_MINOR2INSTANCE(getminor(dev)); 11092 11093 if (major >= devcnt) 11094 return (NULL); 11095 11096 dnp = &devnamesp[major]; 11097 LOCK_DEV_OPS(&(dnp->dn_lock)); 11098 dip = dnp->dn_head; 11099 while (dip && (ddi_get_instance(dip) != instance)) { 11100 dip = ddi_get_next(dip); 11101 } 11102 UNLOCK_DEV_OPS(&(dnp->dn_lock)); 11103 #endif 11104 11105 return (dip); 11106 } 11107 11108 11109 /* 11110 * Probe device. 11111 * This function issues Identify Device command and initializes local 11112 * sata_drive_info structure if the device can be identified. 11113 * The device type is determined by examining Identify Device 11114 * command response. 11115 * If the sata_hba_inst has linked drive info structure for this 11116 * device address, the Identify Device data is stored into sata_drive_info 11117 * structure linked to the port info structure. 11118 * 11119 * sata_device has to refer to the valid sata port(s) for HBA described 11120 * by sata_hba_inst structure. 11121 * 11122 * Returns: 11123 * SATA_SUCCESS if device type was successfully probed and port-linked 11124 * drive info structure was updated; 11125 * SATA_FAILURE if there is no device, or device was not probed 11126 * successully; 11127 * SATA_RETRY if device probe can be retried later. 11128 * If a device cannot be identified, sata_device's dev_state and dev_type 11129 * fields are set to unknown. 11130 * There are no retries in this function. Any retries should be managed by 11131 * the caller. 11132 */ 11133 11134 11135 static int 11136 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 11137 { 11138 sata_pmport_info_t *pmportinfo; 11139 sata_drive_info_t *sdinfo; 11140 sata_drive_info_t new_sdinfo; /* local drive info struct */ 11141 int rval; 11142 11143 ASSERT((SATA_CPORT_STATE(sata_hba_inst, 11144 sata_device->satadev_addr.cport) & 11145 (SATA_STATE_PROBED | SATA_STATE_READY)) != 0); 11146 11147 sata_device->satadev_type = SATA_DTYPE_NONE; 11148 11149 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 11150 sata_device->satadev_addr.cport))); 11151 11152 if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) { 11153 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 11154 sata_device->satadev_addr.cport, 11155 sata_device->satadev_addr.pmport); 11156 ASSERT(pmportinfo != NULL); 11157 } 11158 11159 /* Get pointer to port-linked sata device info structure */ 11160 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 11161 if (sdinfo != NULL) { 11162 sdinfo->satadrv_state &= 11163 ~(SATA_STATE_PROBED | SATA_STATE_READY); 11164 sdinfo->satadrv_state |= SATA_STATE_PROBING; 11165 } else { 11166 /* No device to probe */ 11167 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 11168 sata_device->satadev_addr.cport))); 11169 sata_device->satadev_type = SATA_DTYPE_NONE; 11170 sata_device->satadev_state = SATA_STATE_UNKNOWN; 11171 return (SATA_FAILURE); 11172 } 11173 /* 11174 * Need to issue both types of identify device command and 11175 * determine device type by examining retreived data/status. 11176 * First, ATA Identify Device. 11177 */ 11178 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 11179 new_sdinfo.satadrv_addr = sata_device->satadev_addr; 11180 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 11181 sata_device->satadev_addr.cport))); 11182 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK; 11183 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 11184 if (rval == SATA_RETRY) { 11185 /* We may try to check for ATAPI device */ 11186 if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) { 11187 /* 11188 * HBA supports ATAPI - try to issue Identify Packet 11189 * Device command. 11190 */ 11191 new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI; 11192 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 11193 } 11194 } 11195 if (rval == SATA_SUCCESS) { 11196 /* 11197 * Got something responding positively to ATA Identify Device 11198 * or to Identify Packet Device cmd. 11199 * Save last used device type. 11200 */ 11201 sata_device->satadev_type = new_sdinfo.satadrv_type; 11202 11203 /* save device info, if possible */ 11204 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 11205 sata_device->satadev_addr.cport))); 11206 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 11207 if (sdinfo == NULL) { 11208 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 11209 sata_device->satadev_addr.cport))); 11210 return (SATA_FAILURE); 11211 } 11212 /* 11213 * Copy drive info into the port-linked drive info structure. 11214 */ 11215 *sdinfo = new_sdinfo; 11216 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 11217 sdinfo->satadrv_state |= SATA_STATE_PROBED; 11218 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 11219 SATA_CPORT_DEV_TYPE(sata_hba_inst, 11220 sata_device->satadev_addr.cport) = 11221 sdinfo->satadrv_type; 11222 else { /* SATA_ADDR_DPMPORT */ 11223 mutex_enter(&pmportinfo->pmport_mutex); 11224 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 11225 sata_device->satadev_addr.cport, 11226 sata_device->satadev_addr.pmport) = 11227 sdinfo->satadrv_type; 11228 mutex_exit(&pmportinfo->pmport_mutex); 11229 } 11230 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 11231 sata_device->satadev_addr.cport))); 11232 return (SATA_SUCCESS); 11233 } 11234 11235 /* 11236 * It may be SATA_RETRY or SATA_FAILURE return. 11237 * Looks like we cannot determine the device type at this time. 11238 */ 11239 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 11240 sata_device->satadev_addr.cport))); 11241 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 11242 if (sdinfo != NULL) { 11243 sata_device->satadev_type = SATA_DTYPE_UNKNOWN; 11244 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 11245 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 11246 sdinfo->satadrv_state |= SATA_STATE_PROBED; 11247 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 11248 SATA_CPORT_DEV_TYPE(sata_hba_inst, 11249 sata_device->satadev_addr.cport) = 11250 SATA_DTYPE_UNKNOWN; 11251 else { 11252 /* SATA_ADDR_DPMPORT */ 11253 mutex_enter(&pmportinfo->pmport_mutex); 11254 if ((SATA_PMULT_INFO(sata_hba_inst, 11255 sata_device->satadev_addr.cport) != NULL) && 11256 (SATA_PMPORT_INFO(sata_hba_inst, 11257 sata_device->satadev_addr.cport, 11258 sata_device->satadev_addr.pmport) != NULL)) 11259 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 11260 sata_device->satadev_addr.cport, 11261 sata_device->satadev_addr.pmport) = 11262 SATA_DTYPE_UNKNOWN; 11263 mutex_exit(&pmportinfo->pmport_mutex); 11264 } 11265 } 11266 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 11267 sata_device->satadev_addr.cport))); 11268 return (rval); 11269 } 11270 11271 11272 /* 11273 * Get pointer to sata_drive_info structure. 11274 * 11275 * The sata_device has to contain address (cport, pmport and qualifier) for 11276 * specified sata_scsi structure. 11277 * 11278 * Returns NULL if device address is not valid for this HBA configuration. 11279 * Otherwise, returns a pointer to sata_drive_info structure. 11280 * 11281 * This function should be called with a port mutex held. 11282 */ 11283 static sata_drive_info_t * 11284 sata_get_device_info(sata_hba_inst_t *sata_hba_inst, 11285 sata_device_t *sata_device) 11286 { 11287 uint8_t cport = sata_device->satadev_addr.cport; 11288 uint8_t pmport = sata_device->satadev_addr.pmport; 11289 uint8_t qual = sata_device->satadev_addr.qual; 11290 11291 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 11292 return (NULL); 11293 11294 if (!(SATA_CPORT_STATE(sata_hba_inst, cport) & 11295 (SATA_STATE_PROBED | SATA_STATE_READY))) 11296 /* Port not probed yet */ 11297 return (NULL); 11298 11299 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE) 11300 return (NULL); 11301 11302 if (qual == SATA_ADDR_DCPORT) { 11303 /* Request for a device on a controller port */ 11304 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == 11305 SATA_DTYPE_PMULT) 11306 /* Port multiplier attached */ 11307 return (NULL); 11308 return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport)); 11309 } 11310 if (qual == SATA_ADDR_DPMPORT) { 11311 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 11312 SATA_DTYPE_PMULT) 11313 return (NULL); 11314 11315 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) 11316 return (NULL); 11317 11318 if (!(SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) & 11319 (SATA_STATE_PROBED | SATA_STATE_READY))) 11320 /* Port multiplier port not probed yet */ 11321 return (NULL); 11322 11323 return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport)); 11324 } 11325 11326 /* we should not get here */ 11327 return (NULL); 11328 } 11329 11330 11331 /* 11332 * sata_identify_device. 11333 * Send Identify Device command to SATA HBA driver. 11334 * If command executes successfully, update sata_drive_info structure pointed 11335 * to by sdinfo argument, including Identify Device data. 11336 * If command fails, invalidate data in sata_drive_info. 11337 * 11338 * Cannot be called from interrupt level. 11339 * 11340 * Returns: 11341 * SATA_SUCCESS if the device was identified as a supported device, 11342 * SATA_RETRY if the device was not identified but could be retried, 11343 * SATA_FAILURE if the device was not identified and identify attempt 11344 * should not be retried. 11345 */ 11346 static int 11347 sata_identify_device(sata_hba_inst_t *sata_hba_inst, 11348 sata_drive_info_t *sdinfo) 11349 { 11350 uint16_t cfg_word; 11351 int rval; 11352 11353 /* fetch device identify data */ 11354 if ((rval = sata_fetch_device_identify_data(sata_hba_inst, 11355 sdinfo)) != SATA_SUCCESS) 11356 goto fail_unknown; 11357 11358 cfg_word = sdinfo->satadrv_id.ai_config; 11359 11360 /* Set the correct device type */ 11361 if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) { 11362 sdinfo->satadrv_type = SATA_DTYPE_ATADISK; 11363 } else if (cfg_word == SATA_CFA_TYPE) { 11364 /* It's a Compact Flash media via CF-to-SATA HDD adapter */ 11365 sdinfo->satadrv_type = SATA_DTYPE_ATADISK; 11366 } else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) { 11367 switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) { 11368 case SATA_ATAPI_CDROM_DEV: 11369 sdinfo->satadrv_type = SATA_DTYPE_ATAPICD; 11370 break; 11371 case SATA_ATAPI_SQACC_DEV: 11372 sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE; 11373 break; 11374 case SATA_ATAPI_DIRACC_DEV: 11375 sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK; 11376 break; 11377 default: 11378 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 11379 } 11380 } else { 11381 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 11382 } 11383 11384 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 11385 if (sdinfo->satadrv_capacity == 0) { 11386 /* Non-LBA disk. Too bad... */ 11387 sata_log(sata_hba_inst, CE_WARN, 11388 "SATA disk device at port %d does not support LBA", 11389 sdinfo->satadrv_addr.cport); 11390 rval = SATA_FAILURE; 11391 goto fail_unknown; 11392 } 11393 } 11394 #if 0 11395 /* Left for historical reason */ 11396 /* 11397 * Some initial version of SATA spec indicated that at least 11398 * UDMA mode 4 has to be supported. It is not metioned in 11399 * SerialATA 2.6, so this restriction is removed. 11400 */ 11401 /* Check for Ultra DMA modes 6 through 0 being supported */ 11402 for (i = 6; i >= 0; --i) { 11403 if (sdinfo->satadrv_id.ai_ultradma & (1 << i)) 11404 break; 11405 } 11406 11407 /* 11408 * At least UDMA 4 mode has to be supported. If mode 4 or 11409 * higher are not supported by the device, fail this 11410 * device. 11411 */ 11412 if (i < 4) { 11413 /* No required Ultra DMA mode supported */ 11414 sata_log(sata_hba_inst, CE_WARN, 11415 "SATA disk device at port %d does not support UDMA " 11416 "mode 4 or higher", sdinfo->satadrv_addr.cport); 11417 SATA_LOG_D((sata_hba_inst, CE_WARN, 11418 "mode 4 or higher required, %d supported", i)); 11419 rval = SATA_FAILURE; 11420 goto fail_unknown; 11421 } 11422 #endif 11423 11424 /* 11425 * For Disk devices, if it doesn't support UDMA mode, we would 11426 * like to return failure directly. 11427 */ 11428 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 11429 !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 && 11430 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) { 11431 sata_log(sata_hba_inst, CE_WARN, 11432 "SATA disk device at port %d does not support UDMA", 11433 sdinfo->satadrv_addr.cport); 11434 rval = SATA_FAILURE; 11435 goto fail_unknown; 11436 } 11437 11438 return (SATA_SUCCESS); 11439 11440 fail_unknown: 11441 /* Invalidate sata_drive_info ? */ 11442 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 11443 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 11444 return (rval); 11445 } 11446 11447 /* 11448 * Log/display device information 11449 */ 11450 static void 11451 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst, 11452 sata_drive_info_t *sdinfo) 11453 { 11454 int valid_version; 11455 char msg_buf[MAXPATHLEN]; 11456 int i; 11457 11458 /* Show HBA path */ 11459 (void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf); 11460 11461 cmn_err(CE_CONT, "?%s :\n", msg_buf); 11462 11463 switch (sdinfo->satadrv_type) { 11464 case SATA_DTYPE_ATADISK: 11465 (void) sprintf(msg_buf, "SATA disk device at"); 11466 break; 11467 11468 case SATA_DTYPE_ATAPICD: 11469 (void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at"); 11470 break; 11471 11472 case SATA_DTYPE_ATAPITAPE: 11473 (void) sprintf(msg_buf, "SATA tape (ATAPI) device at"); 11474 break; 11475 11476 case SATA_DTYPE_ATAPIDISK: 11477 (void) sprintf(msg_buf, "SATA disk (ATAPI) device at"); 11478 break; 11479 11480 case SATA_DTYPE_UNKNOWN: 11481 (void) sprintf(msg_buf, 11482 "Unsupported SATA device type (cfg 0x%x) at ", 11483 sdinfo->satadrv_id.ai_config); 11484 break; 11485 } 11486 11487 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT) 11488 cmn_err(CE_CONT, "?\t%s port %d\n", 11489 msg_buf, sdinfo->satadrv_addr.cport); 11490 else 11491 cmn_err(CE_CONT, "?\t%s port %d:%d\n", 11492 msg_buf, sdinfo->satadrv_addr.cport, 11493 sdinfo->satadrv_addr.pmport); 11494 11495 bcopy(&sdinfo->satadrv_id.ai_model, msg_buf, 11496 sizeof (sdinfo->satadrv_id.ai_model)); 11497 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model)); 11498 msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0'; 11499 cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf); 11500 11501 bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf, 11502 sizeof (sdinfo->satadrv_id.ai_fw)); 11503 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw)); 11504 msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0'; 11505 cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf); 11506 11507 bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf, 11508 sizeof (sdinfo->satadrv_id.ai_drvser)); 11509 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser)); 11510 msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0'; 11511 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 11512 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 11513 } else { 11514 /* 11515 * Some drives do not implement serial number and may 11516 * violate the spec by providing spaces rather than zeros 11517 * in serial number field. Scan the buffer to detect it. 11518 */ 11519 for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) { 11520 if (msg_buf[i] != '\0' && msg_buf[i] != ' ') 11521 break; 11522 } 11523 if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) { 11524 cmn_err(CE_CONT, "?\tserial number - none\n"); 11525 } else { 11526 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 11527 } 11528 } 11529 11530 #ifdef SATA_DEBUG 11531 if (sdinfo->satadrv_id.ai_majorversion != 0 && 11532 sdinfo->satadrv_id.ai_majorversion != 0xffff) { 11533 int i; 11534 for (i = 14; i >= 2; i--) { 11535 if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) { 11536 valid_version = i; 11537 break; 11538 } 11539 } 11540 cmn_err(CE_CONT, 11541 "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n", 11542 valid_version, 11543 sdinfo->satadrv_id.ai_majorversion, 11544 sdinfo->satadrv_id.ai_minorversion); 11545 } 11546 #endif 11547 /* Log some info */ 11548 cmn_err(CE_CONT, "?\tsupported features:\n"); 11549 msg_buf[0] = '\0'; 11550 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 11551 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) 11552 (void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN); 11553 else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) 11554 (void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN); 11555 } 11556 if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA) 11557 (void) strlcat(msg_buf, "DMA", MAXPATHLEN); 11558 if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) 11559 (void) strlcat(msg_buf, ", Native Command Queueing", 11560 MAXPATHLEN); 11561 if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ) 11562 (void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN); 11563 if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) && 11564 (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED)) 11565 (void) strlcat(msg_buf, ", SMART", MAXPATHLEN); 11566 if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) && 11567 (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED)) 11568 (void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN); 11569 cmn_err(CE_CONT, "?\t %s\n", msg_buf); 11570 if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2) 11571 cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n"); 11572 else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1) 11573 cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n"); 11574 if (sdinfo->satadrv_features_support & 11575 (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) { 11576 msg_buf[0] = '\0'; 11577 (void) snprintf(msg_buf, MAXPATHLEN, 11578 "Supported queue depth %d", 11579 sdinfo->satadrv_queue_depth); 11580 if (!(sata_func_enable & 11581 (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ))) 11582 (void) strlcat(msg_buf, 11583 " - queueing disabled globally", MAXPATHLEN); 11584 else if (sdinfo->satadrv_queue_depth > 11585 sdinfo->satadrv_max_queue_depth) { 11586 (void) snprintf(&msg_buf[strlen(msg_buf)], 11587 MAXPATHLEN - strlen(msg_buf), ", limited to %d", 11588 (int)sdinfo->satadrv_max_queue_depth); 11589 } 11590 cmn_err(CE_CONT, "?\t%s\n", msg_buf); 11591 } 11592 11593 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 11594 #ifdef __i386 11595 (void) sprintf(msg_buf, "\tcapacity = %llu sectors\n", 11596 sdinfo->satadrv_capacity); 11597 #else 11598 (void) sprintf(msg_buf, "\tcapacity = %lu sectors\n", 11599 sdinfo->satadrv_capacity); 11600 #endif 11601 cmn_err(CE_CONT, "?%s", msg_buf); 11602 } 11603 } 11604 11605 /* 11606 * Log/display port multiplier information 11607 * No Mutex should be hold. 11608 */ 11609 static void 11610 sata_show_pmult_info(sata_hba_inst_t *sata_hba_inst, 11611 sata_device_t *sata_device) 11612 { 11613 _NOTE(ARGUNUSED(sata_hba_inst)) 11614 11615 int cport = sata_device->satadev_addr.cport; 11616 sata_pmult_info_t *pmultinfo; 11617 char msg_buf[MAXPATHLEN]; 11618 uint32_t gscr0, gscr1, gscr2, gscr64; 11619 11620 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 11621 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 11622 if (pmultinfo == NULL) 11623 return; 11624 11625 gscr0 = pmultinfo->pmult_gscr.gscr0; 11626 gscr1 = pmultinfo->pmult_gscr.gscr1; 11627 gscr2 = pmultinfo->pmult_gscr.gscr2; 11628 gscr64 = pmultinfo->pmult_gscr.gscr64; 11629 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 11630 11631 cmn_err(CE_CONT, "?Port Multiplier %d device-ports found at port %d", 11632 sata_device->satadev_add_info, sata_device->satadev_addr.cport); 11633 11634 (void) sprintf(msg_buf, "\tVendor_ID 0x%04x, Module_ID 0x%04x", 11635 gscr0 & 0xffff, (gscr0 >> 16) & 0xffff); 11636 cmn_err(CE_CONT, "?%s", msg_buf); 11637 11638 (void) strcpy(msg_buf, "\tSupport SATA PMP Spec "); 11639 if (gscr1 & (1 << 3)) 11640 (void) strlcat(msg_buf, "1.2", MAXPATHLEN); 11641 else if (gscr1 & (1 << 2)) 11642 (void) strlcat(msg_buf, "1.1", MAXPATHLEN); 11643 else if (gscr1 & (1 << 1)) 11644 (void) strlcat(msg_buf, "1.0", MAXPATHLEN); 11645 else 11646 (void) strlcat(msg_buf, "unknown", MAXPATHLEN); 11647 cmn_err(CE_CONT, "?%s", msg_buf); 11648 11649 (void) strcpy(msg_buf, "\tSupport "); 11650 if (gscr64 & (1 << 3)) 11651 (void) strlcat(msg_buf, "Asy-Notif, ", 11652 MAXPATHLEN); 11653 if (gscr64 & (1 << 2)) 11654 (void) strlcat(msg_buf, "Dyn-SSC, ", MAXPATHLEN); 11655 if (gscr64 & (1 << 1)) 11656 (void) strlcat(msg_buf, "Iss-PMREQ, ", MAXPATHLEN); 11657 if (gscr64 & (1 << 0)) 11658 (void) strlcat(msg_buf, "BIST", MAXPATHLEN); 11659 if ((gscr64 & 0xf) == 0) 11660 (void) strlcat(msg_buf, "nothing", MAXPATHLEN); 11661 cmn_err(CE_CONT, "?%s", msg_buf); 11662 11663 (void) sprintf(msg_buf, "\tNumber of exposed device fan-out ports: %d", 11664 gscr2 & SATA_PMULT_PORTNUM_MASK); 11665 cmn_err(CE_CONT, "?%s", msg_buf); 11666 } 11667 11668 /* 11669 * sata_save_drive_settings extracts current setting of the device and stores 11670 * it for future reference, in case the device setup would need to be restored 11671 * after the device reset. 11672 * 11673 * For all devices read ahead and write cache settings are saved, if the 11674 * device supports these features at all. 11675 * For ATAPI devices the Removable Media Status Notification setting is saved. 11676 */ 11677 static void 11678 sata_save_drive_settings(sata_drive_info_t *sdinfo) 11679 { 11680 if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) || 11681 SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) { 11682 11683 /* Current setting of Read Ahead (and Read Cache) */ 11684 if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id)) 11685 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 11686 else 11687 sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD; 11688 11689 /* Current setting of Write Cache */ 11690 if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id)) 11691 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 11692 else 11693 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 11694 } 11695 11696 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) { 11697 if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id)) 11698 sdinfo->satadrv_settings |= SATA_DEV_RMSN; 11699 else 11700 sdinfo->satadrv_settings &= ~SATA_DEV_RMSN; 11701 } 11702 } 11703 11704 11705 /* 11706 * sata_check_capacity function determines a disk capacity 11707 * and addressing mode (LBA28/LBA48) by examining a disk identify device data. 11708 * 11709 * NOTE: CHS mode is not supported! If a device does not support LBA, 11710 * this function is not called. 11711 * 11712 * Returns device capacity in number of blocks, i.e. largest addressable LBA+1 11713 */ 11714 static uint64_t 11715 sata_check_capacity(sata_drive_info_t *sdinfo) 11716 { 11717 uint64_t capacity = 0; 11718 int i; 11719 11720 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK || 11721 !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT) 11722 /* Capacity valid only for LBA-addressable disk devices */ 11723 return (0); 11724 11725 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) && 11726 (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) && 11727 (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) { 11728 /* LBA48 mode supported and enabled */ 11729 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 | 11730 SATA_DEV_F_LBA28; 11731 for (i = 3; i >= 0; --i) { 11732 capacity <<= 16; 11733 capacity += sdinfo->satadrv_id.ai_addrsecxt[i]; 11734 } 11735 } else { 11736 capacity = sdinfo->satadrv_id.ai_addrsec[1]; 11737 capacity <<= 16; 11738 capacity += sdinfo->satadrv_id.ai_addrsec[0]; 11739 if (capacity >= 0x1000000) 11740 /* LBA28 mode */ 11741 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28; 11742 } 11743 return (capacity); 11744 } 11745 11746 11747 /* 11748 * Allocate consistent buffer for DMA transfer 11749 * 11750 * Cannot be called from interrupt level or with mutex held - it may sleep. 11751 * 11752 * Returns pointer to allocated buffer structure, or NULL if allocation failed. 11753 */ 11754 static struct buf * 11755 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len) 11756 { 11757 struct scsi_address ap; 11758 struct buf *bp; 11759 ddi_dma_attr_t cur_dma_attr; 11760 11761 ASSERT(spx->txlt_sata_pkt != NULL); 11762 ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran; 11763 ap.a_target = SATA_TO_SCSI_TARGET( 11764 spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport, 11765 spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport, 11766 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual); 11767 ap.a_lun = 0; 11768 11769 bp = scsi_alloc_consistent_buf(&ap, NULL, len, 11770 B_READ, SLEEP_FUNC, NULL); 11771 11772 if (bp != NULL) { 11773 /* Allocate DMA resources for this buffer */ 11774 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 11775 /* 11776 * We use a local version of the dma_attr, to account 11777 * for a device addressing limitations. 11778 * sata_adjust_dma_attr() will handle sdinfo == NULL which 11779 * will cause dma attributes to be adjusted to a lowest 11780 * acceptable level. 11781 */ 11782 sata_adjust_dma_attr(NULL, 11783 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 11784 11785 if (sata_dma_buf_setup(spx, PKT_CONSISTENT, 11786 SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) { 11787 scsi_free_consistent_buf(bp); 11788 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 11789 bp = NULL; 11790 } 11791 } 11792 return (bp); 11793 } 11794 11795 /* 11796 * Release local buffer (consistent buffer for DMA transfer) allocated 11797 * via sata_alloc_local_buffer(). 11798 */ 11799 static void 11800 sata_free_local_buffer(sata_pkt_txlate_t *spx) 11801 { 11802 ASSERT(spx->txlt_sata_pkt != NULL); 11803 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL); 11804 11805 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0; 11806 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL; 11807 11808 sata_common_free_dma_rsrcs(spx); 11809 11810 /* Free buffer */ 11811 scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp); 11812 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 11813 } 11814 11815 /* 11816 * Allocate sata_pkt 11817 * Pkt structure version and embedded strcutures version are initialized. 11818 * sata_pkt and sata_pkt_txlate structures are cross-linked. 11819 * 11820 * Since this may be called in interrupt context by sata_scsi_init_pkt, 11821 * callback argument determines if it can sleep or not. 11822 * Hence, it should not be called from interrupt context. 11823 * 11824 * If successful, non-NULL pointer to a sata pkt is returned. 11825 * Upon failure, NULL pointer is returned. 11826 */ 11827 static sata_pkt_t * 11828 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t)) 11829 { 11830 sata_pkt_t *spkt; 11831 int kmsflag; 11832 11833 kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP; 11834 spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag); 11835 if (spkt == NULL) { 11836 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 11837 "sata_pkt_alloc: failed")); 11838 return (NULL); 11839 } 11840 spkt->satapkt_rev = SATA_PKT_REV; 11841 spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV; 11842 spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 11843 spkt->satapkt_framework_private = spx; 11844 spx->txlt_sata_pkt = spkt; 11845 return (spkt); 11846 } 11847 11848 /* 11849 * Free sata pkt allocated via sata_pkt_alloc() 11850 */ 11851 static void 11852 sata_pkt_free(sata_pkt_txlate_t *spx) 11853 { 11854 ASSERT(spx->txlt_sata_pkt != NULL); 11855 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL); 11856 kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t)); 11857 spx->txlt_sata_pkt = NULL; 11858 } 11859 11860 11861 /* 11862 * Adjust DMA attributes. 11863 * SCSI cmds block count is up to 24 bits, SATA cmd block count vary 11864 * from 8 bits to 16 bits, depending on a command being used. 11865 * Limiting max block count arbitrarily to 256 for all read/write 11866 * commands may affects performance, so check both the device and 11867 * controller capability before adjusting dma attributes. 11868 */ 11869 void 11870 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr, 11871 ddi_dma_attr_t *adj_dma_attr) 11872 { 11873 uint32_t count_max; 11874 11875 /* Copy original attributes */ 11876 *adj_dma_attr = *dma_attr; 11877 /* 11878 * Things to consider: device addressing capability, 11879 * "excessive" controller DMA capabilities. 11880 * If a device is being probed/initialized, there are 11881 * no device info - use default limits then. 11882 */ 11883 if (sdinfo == NULL) { 11884 count_max = dma_attr->dma_attr_granular * 0x100; 11885 if (dma_attr->dma_attr_count_max > count_max) 11886 adj_dma_attr->dma_attr_count_max = count_max; 11887 if (dma_attr->dma_attr_maxxfer > count_max) 11888 adj_dma_attr->dma_attr_maxxfer = count_max; 11889 return; 11890 } 11891 11892 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 11893 if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) { 11894 /* 11895 * 16-bit sector count may be used - we rely on 11896 * the assumption that only read and write cmds 11897 * will request more than 256 sectors worth of data 11898 */ 11899 count_max = adj_dma_attr->dma_attr_granular * 0x10000; 11900 } else { 11901 /* 11902 * 8-bit sector count will be used - default limits 11903 * for dma attributes 11904 */ 11905 count_max = adj_dma_attr->dma_attr_granular * 0x100; 11906 } 11907 /* 11908 * Adjust controler dma attributes, if necessary 11909 */ 11910 if (dma_attr->dma_attr_count_max > count_max) 11911 adj_dma_attr->dma_attr_count_max = count_max; 11912 if (dma_attr->dma_attr_maxxfer > count_max) 11913 adj_dma_attr->dma_attr_maxxfer = count_max; 11914 } 11915 } 11916 11917 11918 /* 11919 * Allocate DMA resources for the buffer 11920 * This function handles initial DMA resource allocation as well as 11921 * DMA window shift and may be called repeatedly for the same DMA window 11922 * until all DMA cookies in the DMA window are processed. 11923 * To guarantee that there is always a coherent set of cookies to process 11924 * by SATA HBA driver (observing alignment, device granularity, etc.), 11925 * the number of slots for DMA cookies is equal to lesser of a number of 11926 * cookies in a DMA window and a max number of scatter/gather entries. 11927 * 11928 * Returns DDI_SUCCESS upon successful operation. 11929 * Return failure code of a failing command or DDI_FAILURE when 11930 * internal cleanup failed. 11931 */ 11932 static int 11933 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags, 11934 int (*callback)(caddr_t), caddr_t arg, 11935 ddi_dma_attr_t *cur_dma_attr) 11936 { 11937 int rval; 11938 off_t offset; 11939 size_t size; 11940 int max_sg_len, req_len, i; 11941 uint_t dma_flags; 11942 struct buf *bp; 11943 uint64_t cur_txfer_len; 11944 11945 11946 ASSERT(spx->txlt_sata_pkt != NULL); 11947 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 11948 ASSERT(bp != NULL); 11949 11950 11951 if (spx->txlt_buf_dma_handle == NULL) { 11952 /* 11953 * No DMA resources allocated so far - this is a first call 11954 * for this sata pkt. 11955 */ 11956 rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst), 11957 cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle); 11958 11959 if (rval != DDI_SUCCESS) { 11960 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 11961 "sata_dma_buf_setup: no buf DMA resources %x", 11962 rval)); 11963 return (rval); 11964 } 11965 11966 if (bp->b_flags & B_READ) 11967 dma_flags = DDI_DMA_READ; 11968 else 11969 dma_flags = DDI_DMA_WRITE; 11970 11971 if (flags & PKT_CONSISTENT) 11972 dma_flags |= DDI_DMA_CONSISTENT; 11973 11974 if (flags & PKT_DMA_PARTIAL) 11975 dma_flags |= DDI_DMA_PARTIAL; 11976 11977 /* 11978 * Check buffer alignment and size against dma attributes 11979 * Consider dma_attr_align only. There may be requests 11980 * with the size lower than device granularity, but they 11981 * will not read/write from/to the device, so no adjustment 11982 * is necessary. The dma_attr_minxfer theoretically should 11983 * be considered, but no HBA driver is checking it. 11984 */ 11985 if (IS_P2ALIGNED(bp->b_un.b_addr, 11986 cur_dma_attr->dma_attr_align)) { 11987 rval = ddi_dma_buf_bind_handle( 11988 spx->txlt_buf_dma_handle, 11989 bp, dma_flags, callback, arg, 11990 &spx->txlt_dma_cookie, 11991 &spx->txlt_curwin_num_dma_cookies); 11992 } else { /* Buffer is not aligned */ 11993 11994 int (*ddicallback)(caddr_t); 11995 size_t bufsz; 11996 11997 /* Check id sleeping is allowed */ 11998 ddicallback = (callback == NULL_FUNC) ? 11999 DDI_DMA_DONTWAIT : DDI_DMA_SLEEP; 12000 12001 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 12002 "mis-aligned buffer: addr=0x%p, cnt=%lu", 12003 (void *)bp->b_un.b_addr, bp->b_bcount); 12004 12005 if (bp->b_flags & (B_PAGEIO|B_PHYS)) 12006 /* 12007 * CPU will need to access data in the buffer 12008 * (for copying) so map it. 12009 */ 12010 bp_mapin(bp); 12011 12012 ASSERT(spx->txlt_tmp_buf == NULL); 12013 12014 /* Buffer may be padded by ddi_dma_mem_alloc()! */ 12015 rval = ddi_dma_mem_alloc( 12016 spx->txlt_buf_dma_handle, 12017 bp->b_bcount, 12018 &sata_acc_attr, 12019 DDI_DMA_STREAMING, 12020 ddicallback, NULL, 12021 &spx->txlt_tmp_buf, 12022 &bufsz, 12023 &spx->txlt_tmp_buf_handle); 12024 12025 if (rval != DDI_SUCCESS) { 12026 /* DMA mapping failed */ 12027 (void) ddi_dma_free_handle( 12028 &spx->txlt_buf_dma_handle); 12029 spx->txlt_buf_dma_handle = NULL; 12030 #ifdef SATA_DEBUG 12031 mbuffail_count++; 12032 #endif 12033 SATADBG1(SATA_DBG_DMA_SETUP, 12034 spx->txlt_sata_hba_inst, 12035 "sata_dma_buf_setup: " 12036 "buf dma mem alloc failed %x\n", rval); 12037 return (rval); 12038 } 12039 ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf, 12040 cur_dma_attr->dma_attr_align)); 12041 12042 #ifdef SATA_DEBUG 12043 mbuf_count++; 12044 12045 if (bp->b_bcount != bufsz) 12046 /* 12047 * This will require special handling, because 12048 * DMA cookies will be based on the temporary 12049 * buffer size, not the original buffer 12050 * b_bcount, so the residue may have to 12051 * be counted differently. 12052 */ 12053 SATADBG2(SATA_DBG_DMA_SETUP, 12054 spx->txlt_sata_hba_inst, 12055 "sata_dma_buf_setup: bp size %x != " 12056 "bufsz %x\n", bp->b_bcount, bufsz); 12057 #endif 12058 if (dma_flags & DDI_DMA_WRITE) { 12059 /* 12060 * Write operation - copy data into 12061 * an aligned temporary buffer. Buffer will be 12062 * synced for device by ddi_dma_addr_bind_handle 12063 */ 12064 bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf, 12065 bp->b_bcount); 12066 } 12067 12068 rval = ddi_dma_addr_bind_handle( 12069 spx->txlt_buf_dma_handle, 12070 NULL, 12071 spx->txlt_tmp_buf, 12072 bufsz, dma_flags, ddicallback, 0, 12073 &spx->txlt_dma_cookie, 12074 &spx->txlt_curwin_num_dma_cookies); 12075 } 12076 12077 switch (rval) { 12078 case DDI_DMA_PARTIAL_MAP: 12079 SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 12080 "sata_dma_buf_setup: DMA Partial Map\n", NULL); 12081 /* 12082 * Partial DMA mapping. 12083 * Retrieve number of DMA windows for this request. 12084 */ 12085 if (ddi_dma_numwin(spx->txlt_buf_dma_handle, 12086 &spx->txlt_num_dma_win) != DDI_SUCCESS) { 12087 if (spx->txlt_tmp_buf != NULL) { 12088 ddi_dma_mem_free( 12089 &spx->txlt_tmp_buf_handle); 12090 spx->txlt_tmp_buf = NULL; 12091 } 12092 (void) ddi_dma_unbind_handle( 12093 spx->txlt_buf_dma_handle); 12094 (void) ddi_dma_free_handle( 12095 &spx->txlt_buf_dma_handle); 12096 spx->txlt_buf_dma_handle = NULL; 12097 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 12098 "sata_dma_buf_setup: numwin failed\n")); 12099 return (DDI_FAILURE); 12100 } 12101 SATADBG2(SATA_DBG_DMA_SETUP, 12102 spx->txlt_sata_hba_inst, 12103 "sata_dma_buf_setup: windows: %d, cookies: %d\n", 12104 spx->txlt_num_dma_win, 12105 spx->txlt_curwin_num_dma_cookies); 12106 spx->txlt_cur_dma_win = 0; 12107 break; 12108 12109 case DDI_DMA_MAPPED: 12110 /* DMA fully mapped */ 12111 spx->txlt_num_dma_win = 1; 12112 spx->txlt_cur_dma_win = 0; 12113 SATADBG1(SATA_DBG_DMA_SETUP, 12114 spx->txlt_sata_hba_inst, 12115 "sata_dma_buf_setup: windows: 1 " 12116 "cookies: %d\n", spx->txlt_curwin_num_dma_cookies); 12117 break; 12118 12119 default: 12120 /* DMA mapping failed */ 12121 if (spx->txlt_tmp_buf != NULL) { 12122 ddi_dma_mem_free( 12123 &spx->txlt_tmp_buf_handle); 12124 spx->txlt_tmp_buf = NULL; 12125 } 12126 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 12127 spx->txlt_buf_dma_handle = NULL; 12128 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 12129 "sata_dma_buf_setup: buf dma handle binding " 12130 "failed %x\n", rval)); 12131 return (rval); 12132 } 12133 spx->txlt_curwin_processed_dma_cookies = 0; 12134 spx->txlt_dma_cookie_list = NULL; 12135 } else { 12136 /* 12137 * DMA setup is reused. Check if we need to process more 12138 * cookies in current window, or to get next window, if any. 12139 */ 12140 12141 ASSERT(spx->txlt_curwin_processed_dma_cookies <= 12142 spx->txlt_curwin_num_dma_cookies); 12143 12144 if (spx->txlt_curwin_processed_dma_cookies == 12145 spx->txlt_curwin_num_dma_cookies) { 12146 /* 12147 * All cookies from current DMA window were processed. 12148 * Get next DMA window. 12149 */ 12150 spx->txlt_cur_dma_win++; 12151 if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) { 12152 (void) ddi_dma_getwin(spx->txlt_buf_dma_handle, 12153 spx->txlt_cur_dma_win, &offset, &size, 12154 &spx->txlt_dma_cookie, 12155 &spx->txlt_curwin_num_dma_cookies); 12156 spx->txlt_curwin_processed_dma_cookies = 0; 12157 } else { 12158 /* No more windows! End of request! */ 12159 /* What to do? - panic for now */ 12160 ASSERT(spx->txlt_cur_dma_win >= 12161 spx->txlt_num_dma_win); 12162 12163 spx->txlt_curwin_num_dma_cookies = 0; 12164 spx->txlt_curwin_processed_dma_cookies = 0; 12165 spx->txlt_sata_pkt-> 12166 satapkt_cmd.satacmd_num_dma_cookies = 0; 12167 return (DDI_SUCCESS); 12168 } 12169 } 12170 } 12171 /* There better be at least one DMA cookie outstanding */ 12172 ASSERT((spx->txlt_curwin_num_dma_cookies - 12173 spx->txlt_curwin_processed_dma_cookies) > 0); 12174 12175 if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) { 12176 /* The default cookie slot was used in previous run */ 12177 ASSERT(spx->txlt_curwin_processed_dma_cookies == 0); 12178 spx->txlt_dma_cookie_list = NULL; 12179 spx->txlt_dma_cookie_list_len = 0; 12180 } 12181 if (spx->txlt_curwin_processed_dma_cookies == 0) { 12182 /* 12183 * Processing a new DMA window - set-up dma cookies list. 12184 * We may reuse previously allocated cookie array if it is 12185 * possible. 12186 */ 12187 if (spx->txlt_dma_cookie_list != NULL && 12188 spx->txlt_dma_cookie_list_len < 12189 spx->txlt_curwin_num_dma_cookies) { 12190 /* 12191 * New DMA window contains more cookies than 12192 * the previous one. We need larger cookie list - free 12193 * the old one. 12194 */ 12195 (void) kmem_free(spx->txlt_dma_cookie_list, 12196 spx->txlt_dma_cookie_list_len * 12197 sizeof (ddi_dma_cookie_t)); 12198 spx->txlt_dma_cookie_list = NULL; 12199 spx->txlt_dma_cookie_list_len = 0; 12200 } 12201 if (spx->txlt_dma_cookie_list == NULL) { 12202 /* 12203 * Calculate lesser of number of cookies in this 12204 * DMA window and number of s/g entries. 12205 */ 12206 max_sg_len = cur_dma_attr->dma_attr_sgllen; 12207 req_len = MIN(max_sg_len, 12208 spx->txlt_curwin_num_dma_cookies); 12209 12210 /* Allocate new dma cookie array if necessary */ 12211 if (req_len == 1) { 12212 /* Only one cookie - no need for a list */ 12213 spx->txlt_dma_cookie_list = 12214 &spx->txlt_dma_cookie; 12215 spx->txlt_dma_cookie_list_len = 1; 12216 } else { 12217 /* 12218 * More than one cookie - try to allocate space. 12219 */ 12220 spx->txlt_dma_cookie_list = kmem_zalloc( 12221 sizeof (ddi_dma_cookie_t) * req_len, 12222 callback == NULL_FUNC ? KM_NOSLEEP : 12223 KM_SLEEP); 12224 if (spx->txlt_dma_cookie_list == NULL) { 12225 SATADBG1(SATA_DBG_DMA_SETUP, 12226 spx->txlt_sata_hba_inst, 12227 "sata_dma_buf_setup: cookie list " 12228 "allocation failed\n", NULL); 12229 /* 12230 * We could not allocate space for 12231 * neccessary number of dma cookies in 12232 * this window, so we fail this request. 12233 * Next invocation would try again to 12234 * allocate space for cookie list. 12235 * Note:Packet residue was not modified. 12236 */ 12237 return (DDI_DMA_NORESOURCES); 12238 } else { 12239 spx->txlt_dma_cookie_list_len = req_len; 12240 } 12241 } 12242 } 12243 /* 12244 * Fetch DMA cookies into cookie list in sata_pkt_txlate. 12245 * First cookie was already fetched. 12246 */ 12247 *(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie; 12248 cur_txfer_len = 12249 (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size; 12250 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1; 12251 spx->txlt_curwin_processed_dma_cookies++; 12252 for (i = 1; (i < spx->txlt_dma_cookie_list_len) && 12253 (i < spx->txlt_curwin_num_dma_cookies); i++) { 12254 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 12255 &spx->txlt_dma_cookie_list[i]); 12256 cur_txfer_len += 12257 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 12258 spx->txlt_curwin_processed_dma_cookies++; 12259 spx->txlt_sata_pkt-> 12260 satapkt_cmd.satacmd_num_dma_cookies += 1; 12261 } 12262 } else { 12263 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 12264 "sata_dma_buf_setup: sliding within DMA window, " 12265 "cur cookie %d, total cookies %d\n", 12266 spx->txlt_curwin_processed_dma_cookies, 12267 spx->txlt_curwin_num_dma_cookies); 12268 12269 /* 12270 * Not all cookies from the current dma window were used because 12271 * of s/g limitation. 12272 * There is no need to re-size the list - it was set at 12273 * optimal size, or only default entry is used (s/g = 1). 12274 */ 12275 if (spx->txlt_dma_cookie_list == NULL) { 12276 spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie; 12277 spx->txlt_dma_cookie_list_len = 1; 12278 } 12279 /* 12280 * Since we are processing remaining cookies in a DMA window, 12281 * there may be less of them than the number of entries in the 12282 * current dma cookie list. 12283 */ 12284 req_len = MIN(spx->txlt_dma_cookie_list_len, 12285 (spx->txlt_curwin_num_dma_cookies - 12286 spx->txlt_curwin_processed_dma_cookies)); 12287 12288 /* Fetch the next batch of cookies */ 12289 for (i = 0, cur_txfer_len = 0; i < req_len; i++) { 12290 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 12291 &spx->txlt_dma_cookie_list[i]); 12292 cur_txfer_len += 12293 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 12294 spx->txlt_sata_pkt-> 12295 satapkt_cmd.satacmd_num_dma_cookies++; 12296 spx->txlt_curwin_processed_dma_cookies++; 12297 } 12298 } 12299 12300 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0); 12301 12302 /* Point sata_cmd to the cookie list */ 12303 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = 12304 &spx->txlt_dma_cookie_list[0]; 12305 12306 /* Remember number of DMA cookies passed in sata packet */ 12307 spx->txlt_num_dma_cookies = 12308 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies; 12309 12310 ASSERT(cur_txfer_len != 0); 12311 if (cur_txfer_len <= bp->b_bcount) 12312 spx->txlt_total_residue -= cur_txfer_len; 12313 else { 12314 /* 12315 * Temporary DMA buffer has been padded by 12316 * ddi_dma_mem_alloc()! 12317 * This requires special handling, because DMA cookies are 12318 * based on the temporary buffer size, not the b_bcount, 12319 * and we have extra bytes to transfer - but the packet 12320 * residue has to stay correct because we will copy only 12321 * the requested number of bytes. 12322 */ 12323 spx->txlt_total_residue -= bp->b_bcount; 12324 } 12325 12326 return (DDI_SUCCESS); 12327 } 12328 12329 /* 12330 * Common routine for releasing DMA resources 12331 */ 12332 static void 12333 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx) 12334 { 12335 if (spx->txlt_buf_dma_handle != NULL) { 12336 if (spx->txlt_tmp_buf != NULL) { 12337 /* 12338 * Intermediate DMA buffer was allocated. 12339 * Free allocated buffer and associated access handle. 12340 */ 12341 ddi_dma_mem_free(&spx->txlt_tmp_buf_handle); 12342 spx->txlt_tmp_buf = NULL; 12343 } 12344 /* 12345 * Free DMA resources - cookies and handles 12346 */ 12347 /* ASSERT(spx->txlt_dma_cookie_list != NULL); */ 12348 if (spx->txlt_dma_cookie_list != NULL) { 12349 if (spx->txlt_dma_cookie_list != 12350 &spx->txlt_dma_cookie) { 12351 (void) kmem_free(spx->txlt_dma_cookie_list, 12352 spx->txlt_dma_cookie_list_len * 12353 sizeof (ddi_dma_cookie_t)); 12354 spx->txlt_dma_cookie_list = NULL; 12355 } 12356 } 12357 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle); 12358 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 12359 spx->txlt_buf_dma_handle = NULL; 12360 } 12361 } 12362 12363 /* 12364 * Free DMA resources 12365 * Used by the HBA driver to release DMA resources that it does not use. 12366 * 12367 * Returns Void 12368 */ 12369 void 12370 sata_free_dma_resources(sata_pkt_t *sata_pkt) 12371 { 12372 sata_pkt_txlate_t *spx; 12373 12374 if (sata_pkt == NULL) 12375 return; 12376 12377 spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 12378 12379 sata_common_free_dma_rsrcs(spx); 12380 } 12381 12382 /* 12383 * Fetch Device Identify data. 12384 * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type) 12385 * command to a device and get the device identify data. 12386 * The device_info structure has to be set to device type (for selecting proper 12387 * device identify command). 12388 * 12389 * Returns: 12390 * SATA_SUCCESS if cmd succeeded 12391 * SATA_RETRY if cmd was rejected and could be retried, 12392 * SATA_FAILURE if cmd failed and should not be retried (port error) 12393 * 12394 * Cannot be called in an interrupt context. 12395 */ 12396 12397 static int 12398 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst, 12399 sata_drive_info_t *sdinfo) 12400 { 12401 struct buf *bp; 12402 sata_pkt_t *spkt; 12403 sata_cmd_t *scmd; 12404 sata_pkt_txlate_t *spx; 12405 int rval; 12406 12407 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12408 spx->txlt_sata_hba_inst = sata_hba_inst; 12409 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12410 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12411 if (spkt == NULL) { 12412 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12413 return (SATA_RETRY); /* may retry later */ 12414 } 12415 /* address is needed now */ 12416 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12417 12418 /* 12419 * Allocate buffer for Identify Data return data 12420 */ 12421 bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t)); 12422 if (bp == NULL) { 12423 sata_pkt_free(spx); 12424 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12425 SATA_LOG_D((sata_hba_inst, CE_WARN, 12426 "sata_fetch_device_identify_data: " 12427 "cannot allocate buffer for ID")); 12428 return (SATA_RETRY); /* may retry later */ 12429 } 12430 12431 /* Fill sata_pkt */ 12432 sdinfo->satadrv_state = SATA_STATE_PROBING; 12433 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12434 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12435 /* Synchronous mode, no callback */ 12436 spkt->satapkt_comp = NULL; 12437 /* Timeout 30s */ 12438 spkt->satapkt_time = sata_default_pkt_time; 12439 12440 scmd = &spkt->satapkt_cmd; 12441 scmd->satacmd_bp = bp; 12442 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 12443 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 12444 12445 /* Build Identify Device cmd in the sata_pkt */ 12446 scmd->satacmd_addr_type = 0; /* N/A */ 12447 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 12448 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 12449 scmd->satacmd_lba_mid_lsb = 0; /* N/A */ 12450 scmd->satacmd_lba_high_lsb = 0; /* N/A */ 12451 scmd->satacmd_features_reg = 0; /* N/A */ 12452 scmd->satacmd_device_reg = 0; /* Always device 0 */ 12453 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 12454 /* Identify Packet Device cmd */ 12455 scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE; 12456 } else { 12457 /* Identify Device cmd - mandatory for all other devices */ 12458 scmd->satacmd_cmd_reg = SATAC_ID_DEVICE; 12459 } 12460 12461 /* Send pkt to SATA HBA driver */ 12462 rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt); 12463 12464 #ifdef SATA_INJECT_FAULTS 12465 sata_inject_pkt_fault(spkt, &rval, sata_fault_type); 12466 #endif 12467 12468 if (rval == SATA_TRAN_ACCEPTED && 12469 spkt->satapkt_reason == SATA_PKT_COMPLETED) { 12470 if (spx->txlt_buf_dma_handle != NULL) { 12471 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 12472 DDI_DMA_SYNC_FORKERNEL); 12473 ASSERT(rval == DDI_SUCCESS); 12474 } 12475 if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config & 12476 SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) { 12477 SATA_LOG_D((sata_hba_inst, CE_WARN, 12478 "SATA disk device at port %d - " 12479 "partial Identify Data", 12480 sdinfo->satadrv_addr.cport)); 12481 rval = SATA_RETRY; /* may retry later */ 12482 goto fail; 12483 } 12484 /* Update sata_drive_info */ 12485 bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id, 12486 sizeof (sata_id_t)); 12487 12488 sdinfo->satadrv_features_support = 0; 12489 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 12490 /* 12491 * Retrieve capacity (disks only) and addressing mode 12492 */ 12493 sdinfo->satadrv_capacity = sata_check_capacity(sdinfo); 12494 } else { 12495 /* 12496 * For ATAPI devices one would have to issue 12497 * Get Capacity cmd for media capacity. Not here. 12498 */ 12499 sdinfo->satadrv_capacity = 0; 12500 /* 12501 * Check what cdb length is supported 12502 */ 12503 if ((sdinfo->satadrv_id.ai_config & 12504 SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B) 12505 sdinfo->satadrv_atapi_cdb_len = 16; 12506 else 12507 sdinfo->satadrv_atapi_cdb_len = 12; 12508 } 12509 /* Setup supported features flags */ 12510 if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) 12511 sdinfo->satadrv_features_support |= SATA_DEV_F_DMA; 12512 12513 /* Check for SATA GEN and NCQ support */ 12514 if (sdinfo->satadrv_id.ai_satacap != 0 && 12515 sdinfo->satadrv_id.ai_satacap != 0xffff) { 12516 /* SATA compliance */ 12517 if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ) 12518 sdinfo->satadrv_features_support |= 12519 SATA_DEV_F_NCQ; 12520 if (sdinfo->satadrv_id.ai_satacap & 12521 (SATA_1_SPEED | SATA_2_SPEED)) { 12522 if (sdinfo->satadrv_id.ai_satacap & 12523 SATA_2_SPEED) 12524 sdinfo->satadrv_features_support |= 12525 SATA_DEV_F_SATA2; 12526 if (sdinfo->satadrv_id.ai_satacap & 12527 SATA_1_SPEED) 12528 sdinfo->satadrv_features_support |= 12529 SATA_DEV_F_SATA1; 12530 } else { 12531 sdinfo->satadrv_features_support |= 12532 SATA_DEV_F_SATA1; 12533 } 12534 } 12535 if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) && 12536 (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD)) 12537 sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ; 12538 12539 sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth; 12540 if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) || 12541 (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) { 12542 ++sdinfo->satadrv_queue_depth; 12543 /* Adjust according to controller capabilities */ 12544 sdinfo->satadrv_max_queue_depth = MIN( 12545 sdinfo->satadrv_queue_depth, 12546 SATA_QDEPTH(sata_hba_inst)); 12547 /* Adjust according to global queue depth limit */ 12548 sdinfo->satadrv_max_queue_depth = MIN( 12549 sdinfo->satadrv_max_queue_depth, 12550 sata_current_max_qdepth); 12551 if (sdinfo->satadrv_max_queue_depth == 0) 12552 sdinfo->satadrv_max_queue_depth = 1; 12553 } else 12554 sdinfo->satadrv_max_queue_depth = 1; 12555 12556 rval = SATA_SUCCESS; 12557 } else { 12558 /* 12559 * Woops, no Identify Data. 12560 */ 12561 if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) { 12562 rval = SATA_RETRY; /* may retry later */ 12563 } else if (rval == SATA_TRAN_ACCEPTED) { 12564 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR || 12565 spkt->satapkt_reason == SATA_PKT_ABORTED || 12566 spkt->satapkt_reason == SATA_PKT_TIMEOUT || 12567 spkt->satapkt_reason == SATA_PKT_RESET) 12568 rval = SATA_RETRY; /* may retry later */ 12569 else 12570 rval = SATA_FAILURE; 12571 } else { 12572 rval = SATA_FAILURE; 12573 } 12574 } 12575 fail: 12576 /* Free allocated resources */ 12577 sata_free_local_buffer(spx); 12578 sata_pkt_free(spx); 12579 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12580 12581 return (rval); 12582 } 12583 12584 12585 /* 12586 * Some devices may not come-up with default DMA mode (UDMA or MWDMA). 12587 * UDMA mode is checked first, followed by MWDMA mode. 12588 * set correctly, so this function is setting it to the highest supported level. 12589 * Older SATA spec required that the device supports at least DMA 4 mode and 12590 * UDMA mode is selected. It is not mentioned in SerialATA 2.6, so this 12591 * restriction has been removed. 12592 * 12593 * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported. 12594 * Returns SATA_FAILURE if proper DMA mode could not be selected. 12595 * 12596 * NOTE: This function should be called only if DMA mode is supported. 12597 */ 12598 static int 12599 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo) 12600 { 12601 sata_pkt_t *spkt; 12602 sata_cmd_t *scmd; 12603 sata_pkt_txlate_t *spx; 12604 int mode; 12605 uint8_t subcmd; 12606 int rval = SATA_SUCCESS; 12607 12608 ASSERT(sdinfo != NULL); 12609 ASSERT(sata_hba_inst != NULL); 12610 12611 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 && 12612 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) { 12613 /* Find highest Ultra DMA mode supported */ 12614 for (mode = 6; mode >= 0; --mode) { 12615 if (sdinfo->satadrv_id.ai_ultradma & (1 << mode)) 12616 break; 12617 } 12618 #if 0 12619 /* Left for historical reasons */ 12620 /* 12621 * Some initial version of SATA spec indicated that at least 12622 * UDMA mode 4 has to be supported. It is not mentioned in 12623 * SerialATA 2.6, so this restriction is removed. 12624 */ 12625 if (mode < 4) 12626 return (SATA_FAILURE); 12627 #endif 12628 12629 /* 12630 * We're still going to set DMA mode whatever is selected 12631 * by default 12632 * 12633 * We saw an old maxtor sata drive will select Ultra DMA and 12634 * Multi-Word DMA simultaneouly by default, which is going 12635 * to cause DMA command timed out, so we need to select DMA 12636 * mode even when it's already done by default 12637 */ 12638 12639 subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA; 12640 12641 } else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) { 12642 /* Find highest MultiWord DMA mode supported */ 12643 for (mode = 2; mode >= 0; --mode) { 12644 if (sdinfo->satadrv_id.ai_dworddma & (1 << mode)) 12645 break; 12646 } 12647 12648 /* 12649 * We're still going to set DMA mode whatever is selected 12650 * by default 12651 * 12652 * We saw an old maxtor sata drive will select Ultra DMA and 12653 * Multi-Word DMA simultaneouly by default, which is going 12654 * to cause DMA command timed out, so we need to select DMA 12655 * mode even when it's already done by default 12656 */ 12657 12658 subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA; 12659 } else 12660 return (SATA_SUCCESS); 12661 12662 /* 12663 * Set DMA mode via SET FEATURES COMMAND. 12664 * Prepare packet for SET FEATURES COMMAND. 12665 */ 12666 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12667 spx->txlt_sata_hba_inst = sata_hba_inst; 12668 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12669 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12670 if (spkt == NULL) { 12671 SATA_LOG_D((sata_hba_inst, CE_WARN, 12672 "sata_set_dma_mode: could not set DMA mode %", mode)); 12673 rval = SATA_FAILURE; 12674 goto done; 12675 } 12676 /* Fill sata_pkt */ 12677 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12678 /* Timeout 30s */ 12679 spkt->satapkt_time = sata_default_pkt_time; 12680 /* Synchronous mode, no callback, interrupts */ 12681 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12682 spkt->satapkt_comp = NULL; 12683 scmd = &spkt->satapkt_cmd; 12684 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 12685 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 12686 scmd->satacmd_addr_type = 0; 12687 scmd->satacmd_device_reg = 0; 12688 scmd->satacmd_status_reg = 0; 12689 scmd->satacmd_error_reg = 0; 12690 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 12691 scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE; 12692 scmd->satacmd_sec_count_lsb = subcmd | mode; 12693 12694 /* Transfer command to HBA */ 12695 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 12696 spkt) != SATA_TRAN_ACCEPTED || 12697 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 12698 /* Pkt execution failed */ 12699 rval = SATA_FAILURE; 12700 } 12701 done: 12702 12703 /* Free allocated resources */ 12704 if (spkt != NULL) 12705 sata_pkt_free(spx); 12706 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12707 12708 return (rval); 12709 } 12710 12711 12712 /* 12713 * Set device caching mode. 12714 * One of the following operations should be specified: 12715 * SATAC_SF_ENABLE_READ_AHEAD 12716 * SATAC_SF_DISABLE_READ_AHEAD 12717 * SATAC_SF_ENABLE_WRITE_CACHE 12718 * SATAC_SF_DISABLE_WRITE_CACHE 12719 * 12720 * If operation fails, system log messgage is emitted. 12721 * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if 12722 * command was sent but did not succeed, and SATA_FAILURE otherwise. 12723 */ 12724 12725 static int 12726 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 12727 int cache_op) 12728 { 12729 sata_pkt_t *spkt; 12730 sata_cmd_t *scmd; 12731 sata_pkt_txlate_t *spx; 12732 int rval = SATA_SUCCESS; 12733 int hba_rval; 12734 char *infop; 12735 12736 ASSERT(sdinfo != NULL); 12737 ASSERT(sata_hba_inst != NULL); 12738 ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD || 12739 cache_op == SATAC_SF_DISABLE_READ_AHEAD || 12740 cache_op == SATAC_SF_ENABLE_WRITE_CACHE || 12741 cache_op == SATAC_SF_DISABLE_WRITE_CACHE); 12742 12743 12744 /* Prepare packet for SET FEATURES COMMAND */ 12745 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12746 spx->txlt_sata_hba_inst = sata_hba_inst; 12747 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12748 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12749 if (spkt == NULL) { 12750 rval = SATA_FAILURE; 12751 goto failure; 12752 } 12753 /* Fill sata_pkt */ 12754 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12755 /* Timeout 30s */ 12756 spkt->satapkt_time = sata_default_pkt_time; 12757 /* Synchronous mode, no callback, interrupts */ 12758 spkt->satapkt_op_mode = 12759 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12760 spkt->satapkt_comp = NULL; 12761 scmd = &spkt->satapkt_cmd; 12762 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 12763 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 12764 scmd->satacmd_addr_type = 0; 12765 scmd->satacmd_device_reg = 0; 12766 scmd->satacmd_status_reg = 0; 12767 scmd->satacmd_error_reg = 0; 12768 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 12769 scmd->satacmd_features_reg = cache_op; 12770 12771 /* Transfer command to HBA */ 12772 hba_rval = (*SATA_START_FUNC(sata_hba_inst))( 12773 SATA_DIP(sata_hba_inst), spkt); 12774 12775 #ifdef SATA_INJECT_FAULTS 12776 sata_inject_pkt_fault(spkt, &rval, sata_fault_type); 12777 #endif 12778 12779 if ((hba_rval != SATA_TRAN_ACCEPTED) || 12780 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 12781 /* Pkt execution failed */ 12782 switch (cache_op) { 12783 case SATAC_SF_ENABLE_READ_AHEAD: 12784 infop = "enabling read ahead failed"; 12785 break; 12786 case SATAC_SF_DISABLE_READ_AHEAD: 12787 infop = "disabling read ahead failed"; 12788 break; 12789 case SATAC_SF_ENABLE_WRITE_CACHE: 12790 infop = "enabling write cache failed"; 12791 break; 12792 case SATAC_SF_DISABLE_WRITE_CACHE: 12793 infop = "disabling write cache failed"; 12794 break; 12795 } 12796 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 12797 rval = SATA_RETRY; 12798 } 12799 failure: 12800 /* Free allocated resources */ 12801 if (spkt != NULL) 12802 sata_pkt_free(spx); 12803 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12804 return (rval); 12805 } 12806 12807 /* 12808 * Set Removable Media Status Notification (enable/disable) 12809 * state == 0 , disable 12810 * state != 0 , enable 12811 * 12812 * If operation fails, system log messgage is emitted. 12813 * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise. 12814 */ 12815 12816 static int 12817 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 12818 int state) 12819 { 12820 sata_pkt_t *spkt; 12821 sata_cmd_t *scmd; 12822 sata_pkt_txlate_t *spx; 12823 int rval = SATA_SUCCESS; 12824 char *infop; 12825 12826 ASSERT(sdinfo != NULL); 12827 ASSERT(sata_hba_inst != NULL); 12828 12829 /* Prepare packet for SET FEATURES COMMAND */ 12830 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12831 spx->txlt_sata_hba_inst = sata_hba_inst; 12832 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12833 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12834 if (spkt == NULL) { 12835 rval = SATA_FAILURE; 12836 goto failure; 12837 } 12838 /* Fill sata_pkt */ 12839 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12840 /* Timeout 30s */ 12841 spkt->satapkt_time = sata_default_pkt_time; 12842 /* Synchronous mode, no callback, interrupts */ 12843 spkt->satapkt_op_mode = 12844 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12845 spkt->satapkt_comp = NULL; 12846 scmd = &spkt->satapkt_cmd; 12847 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 12848 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 12849 scmd->satacmd_addr_type = 0; 12850 scmd->satacmd_device_reg = 0; 12851 scmd->satacmd_status_reg = 0; 12852 scmd->satacmd_error_reg = 0; 12853 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 12854 if (state == 0) 12855 scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN; 12856 else 12857 scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN; 12858 12859 /* Transfer command to HBA */ 12860 if (((*SATA_START_FUNC(sata_hba_inst))( 12861 SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) || 12862 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 12863 /* Pkt execution failed */ 12864 if (state == 0) 12865 infop = "disabling Removable Media Status " 12866 "Notification failed"; 12867 else 12868 infop = "enabling Removable Media Status " 12869 "Notification failed"; 12870 12871 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 12872 rval = SATA_FAILURE; 12873 } 12874 failure: 12875 /* Free allocated resources */ 12876 if (spkt != NULL) 12877 sata_pkt_free(spx); 12878 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12879 return (rval); 12880 } 12881 12882 12883 /* 12884 * Update state and copy port ss* values from passed sata_device structure. 12885 * sata_address is validated - if not valid, nothing is changed in sata_scsi 12886 * configuration struct. 12887 * 12888 * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function 12889 * regardless of the state in device argument. 12890 * 12891 * Port mutex should be held while calling this function. 12892 */ 12893 static void 12894 sata_update_port_info(sata_hba_inst_t *sata_hba_inst, 12895 sata_device_t *sata_device) 12896 { 12897 sata_cport_info_t *cportinfo; 12898 12899 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT || 12900 sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) { 12901 if (SATA_NUM_CPORTS(sata_hba_inst) <= 12902 sata_device->satadev_addr.cport) 12903 return; 12904 12905 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 12906 sata_device->satadev_addr.cport); 12907 12908 ASSERT(mutex_owned(&cportinfo->cport_mutex)); 12909 cportinfo->cport_scr = sata_device->satadev_scr; 12910 12911 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 12912 cportinfo->cport_state &= ~(SATA_PSTATE_PWRON | 12913 SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED); 12914 cportinfo->cport_state |= 12915 sata_device->satadev_state & SATA_PSTATE_VALID; 12916 } 12917 } 12918 12919 void 12920 sata_update_pmport_info(sata_hba_inst_t *sata_hba_inst, 12921 sata_device_t *sata_device) 12922 { 12923 sata_pmport_info_t *pmportinfo; 12924 12925 if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT && 12926 sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) || 12927 SATA_NUM_PMPORTS(sata_hba_inst, 12928 sata_device->satadev_addr.cport) < 12929 sata_device->satadev_addr.pmport) { 12930 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 12931 "sata_update_port_info: error address %p.", 12932 &sata_device->satadev_addr); 12933 return; 12934 } 12935 12936 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 12937 sata_device->satadev_addr.cport, 12938 sata_device->satadev_addr.pmport); 12939 12940 ASSERT(mutex_owned(&pmportinfo->pmport_mutex)); 12941 pmportinfo->pmport_scr = sata_device->satadev_scr; 12942 12943 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 12944 pmportinfo->pmport_state &= 12945 ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED); 12946 pmportinfo->pmport_state |= 12947 sata_device->satadev_state & SATA_PSTATE_VALID; 12948 } 12949 12950 /* 12951 * Extract SATA port specification from an IOCTL argument. 12952 * 12953 * This function return the port the user land send us as is, unless it 12954 * cannot retrieve port spec, then -1 is returned. 12955 * 12956 * Support port multiplier. 12957 */ 12958 static int32_t 12959 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp) 12960 { 12961 int32_t port; 12962 12963 /* Extract port number from nvpair in dca structure */ 12964 if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) { 12965 SATA_LOG_D((sata_hba_inst, CE_NOTE, 12966 "sata_get_port_num: invalid port spec 0x%x in ioctl", 12967 port)); 12968 port = -1; 12969 } 12970 12971 return (port); 12972 } 12973 12974 /* 12975 * Get dev_info_t pointer to the device node pointed to by port argument. 12976 * NOTE: target argument is a value used in ioctls to identify 12977 * the AP - it is not a sata_address. 12978 * It is a combination of cport, pmport and address qualifier, encodded same 12979 * way as a scsi target number. 12980 * At this moment it carries only cport number. 12981 * 12982 * PMult hotplug is supported now. 12983 * 12984 * Returns dev_info_t pointer if target device was found, NULL otherwise. 12985 */ 12986 12987 static dev_info_t * 12988 sata_get_target_dip(dev_info_t *dip, uint8_t cport, uint8_t pmport) 12989 { 12990 dev_info_t *cdip = NULL; 12991 int target, tgt; 12992 int circ; 12993 uint8_t qual; 12994 12995 sata_hba_inst_t *sata_hba_inst; 12996 scsi_hba_tran_t *scsi_hba_tran; 12997 12998 /* Get target id */ 12999 scsi_hba_tran = ddi_get_driver_private(dip); 13000 if (scsi_hba_tran == NULL) 13001 return (NULL); 13002 13003 sata_hba_inst = scsi_hba_tran->tran_hba_private; 13004 13005 if (sata_hba_inst == NULL) 13006 return (NULL); 13007 13008 /* Identify a port-mult by cport_info.cport_dev_type */ 13009 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) 13010 qual = SATA_ADDR_DPMPORT; 13011 else 13012 qual = SATA_ADDR_DCPORT; 13013 13014 target = SATA_TO_SCSI_TARGET(cport, pmport, qual); 13015 13016 /* Retrieve target dip */ 13017 ndi_devi_enter(dip, &circ); 13018 for (cdip = ddi_get_child(dip); cdip != NULL; ) { 13019 dev_info_t *next = ddi_get_next_sibling(cdip); 13020 13021 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip, 13022 DDI_PROP_DONTPASS, "target", -1); 13023 if (tgt == -1) { 13024 /* 13025 * This is actually an error condition, but not 13026 * a fatal one. Just continue the search. 13027 */ 13028 cdip = next; 13029 continue; 13030 } 13031 13032 if (tgt == target) 13033 break; 13034 13035 cdip = next; 13036 } 13037 ndi_devi_exit(dip, circ); 13038 13039 return (cdip); 13040 } 13041 13042 /* 13043 * Get dev_info_t pointer to the device node pointed to by port argument. 13044 * NOTE: target argument is a value used in ioctls to identify 13045 * the AP - it is not a sata_address. 13046 * It is a combination of cport, pmport and address qualifier, encoded same 13047 * way as a scsi target number. 13048 * 13049 * Returns dev_info_t pointer if target device was found, NULL otherwise. 13050 */ 13051 13052 static dev_info_t * 13053 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr) 13054 { 13055 dev_info_t *cdip = NULL; 13056 int target, tgt; 13057 int circ; 13058 13059 target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual); 13060 13061 ndi_devi_enter(dip, &circ); 13062 for (cdip = ddi_get_child(dip); cdip != NULL; ) { 13063 dev_info_t *next = ddi_get_next_sibling(cdip); 13064 13065 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip, 13066 DDI_PROP_DONTPASS, "target", -1); 13067 if (tgt == -1) { 13068 /* 13069 * This is actually an error condition, but not 13070 * a fatal one. Just continue the search. 13071 */ 13072 cdip = next; 13073 continue; 13074 } 13075 13076 if (tgt == target) 13077 break; 13078 13079 cdip = next; 13080 } 13081 ndi_devi_exit(dip, circ); 13082 13083 return (cdip); 13084 } 13085 13086 /* 13087 * Process sata port disconnect request. 13088 * Normally, cfgadm sata plugin will try to offline (unconfigure) the device 13089 * before this request. Nevertheless, if a device is still configured, 13090 * we need to attempt to offline and unconfigure device. 13091 * Regardless of the unconfigure operation results the port is marked as 13092 * deactivated and no access to the attached device is possible. 13093 * If the target node remains because unconfigure operation failed, its state 13094 * will be set to DEVICE_REMOVED, preventing it to be used again when a device 13095 * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure 13096 * the device and remove old target node. 13097 * 13098 * This function invokes sata_hba_inst->satahba_tran-> 13099 * sata_tran_hotplug_ops->sata_tran_port_deactivate(). 13100 * If successful, the device structure (if any) attached to the specified port 13101 * is removed and state of the port marked appropriately. 13102 * Failure of the port_deactivate may keep port in the physically active state, 13103 * or may fail the port. 13104 * 13105 * NOTE: Port multiplier is supported. 13106 */ 13107 13108 static int 13109 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst, 13110 sata_device_t *sata_device) 13111 { 13112 sata_drive_info_t *sdinfo = NULL, *subsdinfo = NULL; 13113 sata_cport_info_t *cportinfo = NULL; 13114 sata_pmport_info_t *pmportinfo = NULL; 13115 sata_pmult_info_t *pmultinfo = NULL; 13116 sata_device_t subsdevice; 13117 int cport, pmport, qual; 13118 int rval = SATA_SUCCESS; 13119 int npmport = 0; 13120 int rv = 0; 13121 13122 cport = sata_device->satadev_addr.cport; 13123 pmport = sata_device->satadev_addr.pmport; 13124 qual = sata_device->satadev_addr.qual; 13125 13126 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 13127 if (qual == SATA_ADDR_DCPORT) 13128 qual = SATA_ADDR_CPORT; 13129 else 13130 qual = SATA_ADDR_PMPORT; 13131 13132 /* 13133 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran-> 13134 * sata_tran_hotplug_ops->sata_tran_port_deactivate(). 13135 * Do the sanity check. 13136 */ 13137 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) { 13138 /* No physical port deactivation supported. */ 13139 return (EINVAL); 13140 } 13141 13142 /* Check the current state of the port */ 13143 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 13144 (SATA_DIP(sata_hba_inst), sata_device); 13145 13146 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 13147 13148 /* 13149 * Processing port mulitiplier 13150 */ 13151 if (qual == SATA_ADDR_CPORT && 13152 SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) { 13153 mutex_enter(&cportinfo->cport_mutex); 13154 13155 /* Check controller port status */ 13156 sata_update_port_info(sata_hba_inst, sata_device); 13157 if (rval != SATA_SUCCESS || 13158 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 13159 /* 13160 * Device port status is unknown or it is in failed 13161 * state 13162 */ 13163 SATA_CPORT_STATE(sata_hba_inst, cport) = 13164 SATA_PSTATE_FAILED; 13165 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 13166 "sata_hba_ioctl: connect: failed to deactivate " 13167 "SATA port %d", cport); 13168 mutex_exit(&cportinfo->cport_mutex); 13169 return (EIO); 13170 } 13171 13172 /* Disconnect all sub-devices. */ 13173 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 13174 if (pmultinfo != NULL) { 13175 13176 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 13177 sata_hba_inst, cport); npmport ++) { 13178 subsdinfo = SATA_PMPORT_DRV_INFO( 13179 sata_hba_inst, cport, npmport); 13180 if (subsdinfo == NULL) 13181 continue; 13182 13183 subsdevice.satadev_addr = subsdinfo-> 13184 satadrv_addr; 13185 13186 mutex_exit(&cportinfo->cport_mutex); 13187 if (sata_ioctl_disconnect(sata_hba_inst, 13188 &subsdevice) == SATA_SUCCESS) { 13189 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 13190 "[Remove] device at port %d:%d " 13191 "successfully.", cport, npmport); 13192 } 13193 mutex_enter(&cportinfo->cport_mutex); 13194 } 13195 } 13196 13197 /* Disconnect the port multiplier */ 13198 cportinfo->cport_state &= ~SATA_STATE_READY; 13199 mutex_exit(&cportinfo->cport_mutex); 13200 13201 sata_device->satadev_addr.qual = qual; 13202 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 13203 (SATA_DIP(sata_hba_inst), sata_device); 13204 13205 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 13206 SE_NO_HINT); 13207 13208 mutex_enter(&cportinfo->cport_mutex); 13209 sata_update_port_info(sata_hba_inst, sata_device); 13210 if (rval != SATA_SUCCESS && 13211 sata_device->satadev_state & SATA_PSTATE_FAILED) { 13212 cportinfo->cport_state = SATA_PSTATE_FAILED; 13213 rv = EIO; 13214 } else { 13215 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 13216 } 13217 mutex_exit(&cportinfo->cport_mutex); 13218 13219 return (rv); 13220 } 13221 13222 /* 13223 * Process non-port-multiplier device - it could be a drive connected 13224 * to a port multiplier port or a controller port. 13225 */ 13226 if (qual == SATA_ADDR_PMPORT) { 13227 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 13228 mutex_enter(&pmportinfo->pmport_mutex); 13229 sata_update_pmport_info(sata_hba_inst, sata_device); 13230 if (rval != SATA_SUCCESS || 13231 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 13232 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 13233 SATA_PSTATE_FAILED; 13234 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 13235 "sata_hba_ioctl: connect: failed to deactivate " 13236 "SATA port %d:%d", cport, pmport); 13237 mutex_exit(&pmportinfo->pmport_mutex); 13238 return (EIO); 13239 } 13240 13241 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 13242 sdinfo = pmportinfo->pmport_sata_drive; 13243 ASSERT(sdinfo != NULL); 13244 } 13245 13246 /* 13247 * Set port's dev_state to not ready - this will disable 13248 * an access to a potentially attached device. 13249 */ 13250 pmportinfo->pmport_state &= ~SATA_STATE_READY; 13251 13252 /* Remove and release sata_drive info structure. */ 13253 if (sdinfo != NULL) { 13254 if ((sdinfo->satadrv_type & 13255 SATA_VALID_DEV_TYPE) != 0) { 13256 /* 13257 * If a target node exists, try to offline 13258 * a device and remove target node. 13259 */ 13260 mutex_exit(&pmportinfo->pmport_mutex); 13261 (void) sata_offline_device(sata_hba_inst, 13262 sata_device, sdinfo); 13263 mutex_enter(&pmportinfo->pmport_mutex); 13264 } 13265 13266 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 13267 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 13268 (void) kmem_free((void *)sdinfo, 13269 sizeof (sata_drive_info_t)); 13270 } 13271 mutex_exit(&pmportinfo->pmport_mutex); 13272 13273 } else if (qual == SATA_ADDR_CPORT) { 13274 mutex_enter(&cportinfo->cport_mutex); 13275 sata_update_port_info(sata_hba_inst, sata_device); 13276 if (rval != SATA_SUCCESS || 13277 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 13278 /* 13279 * Device port status is unknown or it is in failed 13280 * state 13281 */ 13282 SATA_CPORT_STATE(sata_hba_inst, cport) = 13283 SATA_PSTATE_FAILED; 13284 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 13285 "sata_hba_ioctl: connect: failed to deactivate " 13286 "SATA port %d", cport); 13287 mutex_exit(&cportinfo->cport_mutex); 13288 return (EIO); 13289 } 13290 13291 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 13292 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 13293 ASSERT(pmultinfo != NULL); 13294 } else if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 13295 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 13296 ASSERT(sdinfo != NULL); 13297 } 13298 cportinfo->cport_state &= ~SATA_STATE_READY; 13299 13300 if (sdinfo != NULL) { 13301 if ((sdinfo->satadrv_type & 13302 SATA_VALID_DEV_TYPE) != 0) { 13303 /* 13304 * If a target node exists, try to offline 13305 * a device and remove target node. 13306 */ 13307 mutex_exit(&cportinfo->cport_mutex); 13308 (void) sata_offline_device(sata_hba_inst, 13309 sata_device, sdinfo); 13310 mutex_enter(&cportinfo->cport_mutex); 13311 } 13312 13313 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 13314 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 13315 (void) kmem_free((void *)sdinfo, 13316 sizeof (sata_drive_info_t)); 13317 } 13318 mutex_exit(&cportinfo->cport_mutex); 13319 } 13320 13321 /* Just ask HBA driver to deactivate port */ 13322 sata_device->satadev_addr.qual = qual; 13323 13324 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 13325 (SATA_DIP(sata_hba_inst), sata_device); 13326 13327 /* 13328 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 13329 * without the hint (to force listener to investivate the state). 13330 */ 13331 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 13332 SE_NO_HINT); 13333 13334 if (qual == SATA_ADDR_PMPORT) { 13335 mutex_enter(&pmportinfo->pmport_mutex); 13336 sata_update_pmport_info(sata_hba_inst, sata_device); 13337 13338 if (rval != SATA_SUCCESS && 13339 sata_device->satadev_state & SATA_PSTATE_FAILED) { 13340 /* 13341 * Port deactivation failure - do not change port 13342 * state unless the state returned by HBA indicates a 13343 * port failure. 13344 * 13345 * NOTE: device structures were released, so devices 13346 * now are invisible! Port reset is needed to 13347 * re-enumerate devices. 13348 */ 13349 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 13350 rv = EIO; 13351 } else { 13352 /* 13353 * Deactivation succeded. From now on the sata framework 13354 * will not care what is happening to the device, until 13355 * the port is activated again. 13356 */ 13357 pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN; 13358 } 13359 mutex_exit(&pmportinfo->pmport_mutex); 13360 } else if (qual == SATA_ADDR_CPORT) { 13361 mutex_enter(&cportinfo->cport_mutex); 13362 sata_update_port_info(sata_hba_inst, sata_device); 13363 13364 if (rval != SATA_SUCCESS && 13365 sata_device->satadev_state & SATA_PSTATE_FAILED) { 13366 cportinfo->cport_state = SATA_PSTATE_FAILED; 13367 rv = EIO; 13368 } else { 13369 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 13370 } 13371 mutex_exit(&cportinfo->cport_mutex); 13372 } 13373 13374 return (rv); 13375 } 13376 13377 13378 13379 /* 13380 * Process sata port connect request 13381 * The sata cfgadm pluging will invoke this operation only if port was found 13382 * in the disconnect state (failed state is also treated as the disconnected 13383 * state). 13384 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->satahba_tran-> 13385 * sata_tran_hotplug_ops->sata_tran_port_activate(). 13386 * If successful and a device is found attached to the port, 13387 * the initialization sequence is executed to attach a device structure to 13388 * a port structure. The state of the port and a device would be set 13389 * appropriately. 13390 * The device is not set in configured state (system-wise) by this operation. 13391 * 13392 * Note, that activating the port may generate link events, 13393 * so it is important that following processing and the 13394 * event processing does not interfere with each other! 13395 * 13396 * This operation may remove port failed state and will 13397 * try to make port active and in good standing. 13398 * 13399 * NOTE: Port multiplier is supported. 13400 */ 13401 13402 static int 13403 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst, 13404 sata_device_t *sata_device) 13405 { 13406 sata_pmport_info_t *pmportinfo = NULL; 13407 uint8_t cport, pmport, qual; 13408 int rv = 0; 13409 13410 cport = sata_device->satadev_addr.cport; 13411 pmport = sata_device->satadev_addr.pmport; 13412 qual = sata_device->satadev_addr.qual; 13413 13414 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 13415 if (qual == SATA_ADDR_DCPORT) 13416 qual = SATA_ADDR_CPORT; 13417 else 13418 qual = SATA_ADDR_PMPORT; 13419 13420 if (qual == SATA_ADDR_PMPORT) 13421 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 13422 13423 /* 13424 * DEVCTL_AP_CONNECT would invoke sata_hba_inst-> 13425 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate(). 13426 * Perform sanity check now. 13427 */ 13428 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) { 13429 /* No physical port activation supported. */ 13430 return (EINVAL); 13431 } 13432 13433 /* Just ask HBA driver to activate port */ 13434 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 13435 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 13436 /* 13437 * Port activation failure. 13438 */ 13439 if (qual == SATA_ADDR_CPORT) { 13440 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 13441 cport)->cport_mutex); 13442 sata_update_port_info(sata_hba_inst, sata_device); 13443 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 13444 SATA_CPORT_STATE(sata_hba_inst, cport) = 13445 SATA_PSTATE_FAILED; 13446 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 13447 "sata_hba_ioctl: connect: failed to " 13448 "activate SATA port %d", cport); 13449 } 13450 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 13451 cport)->cport_mutex); 13452 } else { /* port multiplier device port */ 13453 mutex_enter(&pmportinfo->pmport_mutex); 13454 sata_update_pmport_info(sata_hba_inst, sata_device); 13455 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 13456 SATA_PMPORT_STATE(sata_hba_inst, cport, 13457 pmport) = SATA_PSTATE_FAILED; 13458 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 13459 "sata_hba_ioctl: connect: failed to " 13460 "activate SATA port %d:%d", cport, pmport); 13461 } 13462 mutex_exit(&pmportinfo->pmport_mutex); 13463 } 13464 return (EIO); 13465 } 13466 13467 /* Virgin port state - will be updated by the port re-probe. */ 13468 if (qual == SATA_ADDR_CPORT) { 13469 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 13470 cport)->cport_mutex); 13471 SATA_CPORT_STATE(sata_hba_inst, cport) = 0; 13472 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 13473 cport)->cport_mutex); 13474 } else { /* port multiplier device port */ 13475 mutex_enter(&pmportinfo->pmport_mutex); 13476 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0; 13477 mutex_exit(&pmportinfo->pmport_mutex); 13478 } 13479 13480 /* 13481 * Probe the port to find its state and attached device. 13482 */ 13483 if (sata_reprobe_port(sata_hba_inst, sata_device, 13484 SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE) 13485 rv = EIO; 13486 13487 /* 13488 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 13489 * without the hint 13490 */ 13491 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 13492 SE_NO_HINT); 13493 13494 /* 13495 * If there is a device attached to the port, emit 13496 * a message. 13497 */ 13498 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 13499 13500 if (qual == SATA_ADDR_CPORT) { 13501 if (sata_device->satadev_type == SATA_DTYPE_PMULT) { 13502 sata_log(sata_hba_inst, CE_WARN, 13503 "SATA port multiplier detected " 13504 "at port %d", cport); 13505 } else { 13506 sata_log(sata_hba_inst, CE_WARN, 13507 "SATA device detected at port %d", cport); 13508 if (sata_device->satadev_type == 13509 SATA_DTYPE_UNKNOWN) { 13510 /* 13511 * A device was not successfully identified 13512 */ 13513 sata_log(sata_hba_inst, CE_WARN, 13514 "Could not identify SATA " 13515 "device at port %d", cport); 13516 } 13517 } 13518 } else { /* port multiplier device port */ 13519 sata_log(sata_hba_inst, CE_WARN, 13520 "SATA device detected at port %d:%d", 13521 cport, pmport); 13522 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 13523 /* 13524 * A device was not successfully identified 13525 */ 13526 sata_log(sata_hba_inst, CE_WARN, 13527 "Could not identify SATA " 13528 "device at port %d:%d", cport, pmport); 13529 } 13530 } 13531 } 13532 13533 return (rv); 13534 } 13535 13536 13537 /* 13538 * Process sata device unconfigure request. 13539 * The unconfigure operation uses generic nexus operation to 13540 * offline a device. It leaves a target device node attached. 13541 * and obviously sata_drive_info attached as well, because 13542 * from the hardware point of view nothing has changed. 13543 */ 13544 static int 13545 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst, 13546 sata_device_t *sata_device) 13547 { 13548 int rv = 0; 13549 dev_info_t *tdip; 13550 13551 /* We are addressing attached device, not a port */ 13552 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 13553 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 13554 else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) 13555 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 13556 13557 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 13558 &sata_device->satadev_addr)) != NULL) { 13559 13560 if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) { 13561 SATA_LOG_D((sata_hba_inst, CE_WARN, 13562 "sata_hba_ioctl: unconfigure: " 13563 "failed to unconfigure device at SATA port %d:%d", 13564 sata_device->satadev_addr.cport, 13565 sata_device->satadev_addr.pmport)); 13566 rv = EIO; 13567 } 13568 /* 13569 * The target node devi_state should be marked with 13570 * DEVI_DEVICE_OFFLINE by ndi_devi_offline(). 13571 * This would be the indication for cfgadm that 13572 * the AP node occupant state is 'unconfigured'. 13573 */ 13574 13575 } else { 13576 /* 13577 * This would indicate a failure on the part of cfgadm 13578 * to detect correct state of the node prior to this 13579 * call - one cannot unconfigure non-existing device. 13580 */ 13581 SATA_LOG_D((sata_hba_inst, CE_WARN, 13582 "sata_hba_ioctl: unconfigure: " 13583 "attempt to unconfigure non-existing device " 13584 "at SATA port %d:%d", 13585 sata_device->satadev_addr.cport, 13586 sata_device->satadev_addr.pmport)); 13587 rv = ENXIO; 13588 } 13589 return (rv); 13590 } 13591 13592 /* 13593 * Process sata device configure request 13594 * If port is in a failed state, operation is aborted - one has to use 13595 * an explicit connect or port activate request to try to get a port into 13596 * non-failed mode. Port reset wil also work in such situation. 13597 * If the port is in disconnected (shutdown) state, the connect operation is 13598 * attempted prior to any other action. 13599 * When port is in the active state, there is a device attached and the target 13600 * node exists, a device was most likely offlined. 13601 * If target node does not exist, a new target node is created. In both cases 13602 * an attempt is made to online (configure) the device. 13603 * 13604 * NOTE: Port multiplier is supported. 13605 */ 13606 static int 13607 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst, 13608 sata_device_t *sata_device) 13609 { 13610 int cport, pmport, qual; 13611 int rval; 13612 boolean_t target = TRUE; 13613 sata_cport_info_t *cportinfo; 13614 sata_pmport_info_t *pmportinfo = NULL; 13615 dev_info_t *tdip; 13616 sata_drive_info_t *sdinfo; 13617 13618 cport = sata_device->satadev_addr.cport; 13619 pmport = sata_device->satadev_addr.pmport; 13620 qual = sata_device->satadev_addr.qual; 13621 13622 /* Get current port state */ 13623 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 13624 (SATA_DIP(sata_hba_inst), sata_device); 13625 13626 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 13627 if (qual == SATA_ADDR_DPMPORT) { 13628 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 13629 mutex_enter(&pmportinfo->pmport_mutex); 13630 sata_update_pmport_info(sata_hba_inst, sata_device); 13631 if (rval != SATA_SUCCESS || 13632 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 13633 /* 13634 * Obviously, device on a failed port is not visible 13635 */ 13636 mutex_exit(&pmportinfo->pmport_mutex); 13637 return (ENXIO); 13638 } 13639 mutex_exit(&pmportinfo->pmport_mutex); 13640 } else { 13641 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 13642 cport)->cport_mutex); 13643 sata_update_port_info(sata_hba_inst, sata_device); 13644 if (rval != SATA_SUCCESS || 13645 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 13646 /* 13647 * Obviously, device on a failed port is not visible 13648 */ 13649 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 13650 cport)->cport_mutex); 13651 return (ENXIO); 13652 } 13653 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 13654 cport)->cport_mutex); 13655 } 13656 13657 if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) { 13658 /* need to activate port */ 13659 target = FALSE; 13660 13661 /* Sanity check */ 13662 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) 13663 return (ENXIO); 13664 13665 /* Just let HBA driver to activate port */ 13666 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 13667 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 13668 /* 13669 * Port activation failure - do not change port state 13670 * unless the state returned by HBA indicates a port 13671 * failure. 13672 */ 13673 if (qual == SATA_ADDR_DPMPORT) { 13674 mutex_enter(&pmportinfo->pmport_mutex); 13675 sata_update_pmport_info(sata_hba_inst, 13676 sata_device); 13677 if (sata_device->satadev_state & 13678 SATA_PSTATE_FAILED) 13679 pmportinfo->pmport_state = 13680 SATA_PSTATE_FAILED; 13681 mutex_exit(&pmportinfo->pmport_mutex); 13682 } else { 13683 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 13684 cport)->cport_mutex); 13685 sata_update_port_info(sata_hba_inst, 13686 sata_device); 13687 if (sata_device->satadev_state & 13688 SATA_PSTATE_FAILED) 13689 cportinfo->cport_state = 13690 SATA_PSTATE_FAILED; 13691 mutex_exit(&SATA_CPORT_INFO( 13692 sata_hba_inst, cport)->cport_mutex); 13693 } 13694 } 13695 SATA_LOG_D((sata_hba_inst, CE_WARN, 13696 "sata_hba_ioctl: configure: " 13697 "failed to activate SATA port %d:%d", 13698 cport, pmport)); 13699 return (EIO); 13700 } 13701 /* 13702 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 13703 * without the hint. 13704 */ 13705 sata_gen_sysevent(sata_hba_inst, 13706 &sata_device->satadev_addr, SE_NO_HINT); 13707 13708 /* Virgin port state */ 13709 if (qual == SATA_ADDR_DPMPORT) { 13710 mutex_enter(&pmportinfo->pmport_mutex); 13711 pmportinfo->pmport_state = 0; 13712 mutex_exit(&pmportinfo->pmport_mutex); 13713 } else { 13714 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 13715 cport)-> cport_mutex); 13716 cportinfo->cport_state = 0; 13717 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 13718 cport)->cport_mutex); 13719 } 13720 /* 13721 * Always reprobe port, to get current device info. 13722 */ 13723 if (sata_reprobe_port(sata_hba_inst, sata_device, 13724 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 13725 return (EIO); 13726 13727 if (sata_device->satadev_type != SATA_DTYPE_NONE && target == FALSE) { 13728 if (qual == SATA_ADDR_DPMPORT) { 13729 /* 13730 * That's the transition from "inactive" port 13731 * to active one with device attached. 13732 */ 13733 sata_log(sata_hba_inst, CE_WARN, 13734 "SATA device detected at port %d:%d", 13735 cport, pmport); 13736 } else { 13737 /* 13738 * When PM is attached to the cport and cport is 13739 * activated, every PM device port needs to be reprobed. 13740 * We need to emit message for all devices detected 13741 * at port multiplier's device ports. 13742 * Add such code here. 13743 * For now, just inform about device attached to 13744 * cport. 13745 */ 13746 sata_log(sata_hba_inst, CE_WARN, 13747 "SATA device detected at port %d", cport); 13748 } 13749 } 13750 13751 /* 13752 * This is where real configuration operation starts. 13753 * 13754 * When PM is attached to the cport and cport is activated, 13755 * devices attached PM device ports may have to be configured 13756 * explicitly. This may change when port multiplier is supported. 13757 * For now, configure only disks and other valid target devices. 13758 */ 13759 if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) { 13760 if (qual == SATA_ADDR_DCPORT) { 13761 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 13762 /* 13763 * A device was not successfully identified 13764 */ 13765 sata_log(sata_hba_inst, CE_WARN, 13766 "Could not identify SATA " 13767 "device at port %d", cport); 13768 } 13769 } else { /* port multiplier device port */ 13770 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 13771 /* 13772 * A device was not successfully identified 13773 */ 13774 sata_log(sata_hba_inst, CE_WARN, 13775 "Could not identify SATA " 13776 "device at port %d:%d", cport, pmport); 13777 } 13778 } 13779 return (ENXIO); /* No device to configure */ 13780 } 13781 13782 /* 13783 * Here we may have a device in reset condition, 13784 * but because we are just configuring it, there is 13785 * no need to process the reset other than just 13786 * to clear device reset condition in the HBA driver. 13787 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will 13788 * cause a first command sent the HBA driver with the request 13789 * to clear device reset condition. 13790 */ 13791 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 13792 if (qual == SATA_ADDR_DPMPORT) 13793 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 13794 else 13795 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 13796 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 13797 if (sdinfo == NULL) { 13798 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 13799 return (ENXIO); 13800 } 13801 if (sdinfo->satadrv_event_flags & 13802 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 13803 sdinfo->satadrv_event_flags = 0; 13804 } 13805 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 13806 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 13807 13808 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 13809 &sata_device->satadev_addr)) != NULL) { 13810 /* 13811 * Target node exists. Verify, that it belongs 13812 * to existing, attached device and not to 13813 * a removed device. 13814 */ 13815 if (sata_check_device_removed(tdip) == B_TRUE) { 13816 if (qual == SATA_ADDR_DPMPORT) 13817 sata_log(sata_hba_inst, CE_WARN, 13818 "SATA device at port %d cannot be " 13819 "configured. " 13820 "Application(s) accessing " 13821 "previously attached device " 13822 "have to release it before newly " 13823 "inserted device can be made accessible.", 13824 cport); 13825 else 13826 sata_log(sata_hba_inst, CE_WARN, 13827 "SATA device at port %d:%d cannot be" 13828 "configured. " 13829 "Application(s) accessing " 13830 "previously attached device " 13831 "have to release it before newly " 13832 "inserted device can be made accessible.", 13833 cport, pmport); 13834 return (EIO); 13835 } 13836 /* 13837 * Device was not removed and re-inserted. 13838 * Try to online it. 13839 */ 13840 if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) { 13841 SATA_LOG_D((sata_hba_inst, CE_WARN, 13842 "sata_hba_ioctl: configure: " 13843 "onlining device at SATA port " 13844 "%d:%d failed", cport, pmport)); 13845 return (EIO); 13846 } 13847 13848 if (qual == SATA_ADDR_DPMPORT) { 13849 mutex_enter(&pmportinfo->pmport_mutex); 13850 pmportinfo->pmport_tgtnode_clean = B_TRUE; 13851 mutex_exit(&pmportinfo->pmport_mutex); 13852 } else { 13853 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 13854 cport)->cport_mutex); 13855 cportinfo-> cport_tgtnode_clean = B_TRUE; 13856 mutex_exit(&SATA_CPORT_INFO( 13857 sata_hba_inst, cport)->cport_mutex); 13858 } 13859 } else { 13860 /* 13861 * No target node - need to create a new target node. 13862 */ 13863 if (qual == SATA_ADDR_DPMPORT) { 13864 mutex_enter(&pmportinfo->pmport_mutex); 13865 pmportinfo->pmport_tgtnode_clean = B_TRUE; 13866 mutex_exit(&pmportinfo->pmport_mutex); 13867 } else { 13868 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 13869 cport_mutex); 13870 cportinfo-> cport_tgtnode_clean = B_TRUE; 13871 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 13872 cport_mutex); 13873 } 13874 13875 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst), 13876 sata_hba_inst, &sata_device->satadev_addr); 13877 if (tdip == NULL) { 13878 /* Configure operation failed */ 13879 SATA_LOG_D((sata_hba_inst, CE_WARN, 13880 "sata_hba_ioctl: configure: " 13881 "configuring SATA device at port %d:%d " 13882 "failed", cport, pmport)); 13883 return (EIO); 13884 } 13885 } 13886 return (0); 13887 } 13888 13889 13890 /* 13891 * Process ioctl deactivate port request. 13892 * Arbitrarily unconfigure attached device, if any. 13893 * Even if the unconfigure fails, proceed with the 13894 * port deactivation. 13895 * 13896 * NOTE: Port Multiplier is supported now. 13897 */ 13898 13899 static int 13900 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst, 13901 sata_device_t *sata_device) 13902 { 13903 int cport, pmport, qual; 13904 int rval, rv = 0; 13905 int npmport; 13906 sata_cport_info_t *cportinfo; 13907 sata_pmport_info_t *pmportinfo; 13908 sata_pmult_info_t *pmultinfo; 13909 dev_info_t *tdip; 13910 sata_drive_info_t *sdinfo = NULL; 13911 sata_device_t subsdevice; 13912 13913 /* Sanity check */ 13914 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) 13915 return (ENOTSUP); 13916 13917 cport = sata_device->satadev_addr.cport; 13918 pmport = sata_device->satadev_addr.pmport; 13919 qual = sata_device->satadev_addr.qual; 13920 13921 /* SCSI_TO_SATA_ADDR_QUAL() translate ap_id into a device qualifier */ 13922 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 13923 if (qual == SATA_ADDR_DCPORT) 13924 qual = SATA_ADDR_CPORT; 13925 else 13926 qual = SATA_ADDR_PMPORT; 13927 13928 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 13929 if (qual == SATA_ADDR_PMPORT) 13930 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 13931 13932 /* 13933 * Processing port multiplier 13934 */ 13935 if (qual == SATA_ADDR_CPORT && 13936 SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) { 13937 mutex_enter(&cportinfo->cport_mutex); 13938 13939 /* Deactivate all sub-deices */ 13940 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 13941 if (pmultinfo != NULL) { 13942 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 13943 sata_hba_inst, cport); npmport++) { 13944 13945 subsdevice.satadev_addr.cport = cport; 13946 subsdevice.satadev_addr.pmport = 13947 (uint8_t)npmport; 13948 subsdevice.satadev_addr.qual = 13949 SATA_ADDR_DPMPORT; 13950 13951 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 13952 "sata_hba_ioctl: deactivate: trying to " 13953 "deactivate SATA port %d:%d", 13954 cport, npmport); 13955 13956 mutex_exit(&cportinfo->cport_mutex); 13957 if (sata_ioctl_deactivate(sata_hba_inst, 13958 &subsdevice) == SATA_SUCCESS) { 13959 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 13960 "[Deactivate] device at port %d:%d " 13961 "successfully.", cport, npmport); 13962 } 13963 mutex_enter(&cportinfo->cport_mutex); 13964 } 13965 } 13966 13967 /* Deactivate the port multiplier now. */ 13968 cportinfo->cport_state &= ~SATA_STATE_READY; 13969 mutex_exit(&cportinfo->cport_mutex); 13970 13971 sata_device->satadev_addr.qual = qual; 13972 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 13973 (SATA_DIP(sata_hba_inst), sata_device); 13974 13975 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 13976 SE_NO_HINT); 13977 13978 mutex_enter(&cportinfo->cport_mutex); 13979 sata_update_port_info(sata_hba_inst, sata_device); 13980 if (rval != SATA_SUCCESS) { 13981 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 13982 cportinfo->cport_state = SATA_PSTATE_FAILED; 13983 } 13984 rv = EIO; 13985 } else { 13986 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 13987 } 13988 mutex_exit(&cportinfo->cport_mutex); 13989 13990 return (rv); 13991 } 13992 13993 /* 13994 * Process non-port-multiplier device - it could be a drive connected 13995 * to a port multiplier port or a controller port. 13996 */ 13997 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 13998 if (qual == SATA_ADDR_CPORT) { 13999 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 14000 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 14001 /* deal only with valid devices */ 14002 if ((cportinfo->cport_dev_type & 14003 SATA_VALID_DEV_TYPE) != 0) 14004 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 14005 } 14006 cportinfo->cport_state &= ~SATA_STATE_READY; 14007 } else { 14008 /* Port multiplier device port */ 14009 mutex_enter(&pmportinfo->pmport_mutex); 14010 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 14011 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE && 14012 (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0) 14013 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 14014 pmportinfo->pmport_state &= ~SATA_STATE_READY; 14015 mutex_exit(&pmportinfo->pmport_mutex); 14016 } 14017 14018 if (sdinfo != NULL) { 14019 /* 14020 * If a target node exists, try to offline a device and 14021 * to remove a target node. 14022 */ 14023 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 14024 cport_mutex); 14025 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 14026 &sata_device->satadev_addr); 14027 if (tdip != NULL) { 14028 /* target node exist */ 14029 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 14030 "sata_hba_ioctl: port deactivate: " 14031 "target node exists.", NULL); 14032 14033 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != 14034 NDI_SUCCESS) { 14035 SATA_LOG_D((sata_hba_inst, CE_WARN, 14036 "sata_hba_ioctl: port deactivate: " 14037 "failed to unconfigure device at port " 14038 "%d:%d before deactivating the port", 14039 cport, pmport)); 14040 /* 14041 * Set DEVICE REMOVED state in the target 14042 * node. It will prevent an access to 14043 * the device even when a new device is 14044 * attached, until the old target node is 14045 * released, removed and recreated for a new 14046 * device. 14047 */ 14048 sata_set_device_removed(tdip); 14049 14050 /* 14051 * Instruct the event daemon to try the 14052 * target node cleanup later. 14053 */ 14054 sata_set_target_node_cleanup(sata_hba_inst, 14055 &sata_device->satadev_addr); 14056 } 14057 } 14058 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 14059 cport_mutex); 14060 /* 14061 * In any case, remove and release sata_drive_info 14062 * structure. 14063 */ 14064 if (qual == SATA_ADDR_CPORT) { 14065 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 14066 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 14067 } else { /* port multiplier device port */ 14068 mutex_enter(&pmportinfo->pmport_mutex); 14069 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 14070 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 14071 mutex_exit(&pmportinfo->pmport_mutex); 14072 } 14073 (void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t)); 14074 } 14075 14076 if (qual == SATA_ADDR_CPORT) { 14077 cportinfo->cport_state &= ~(SATA_STATE_PROBED | 14078 SATA_STATE_PROBING); 14079 } else if (qual == SATA_ADDR_PMPORT) { 14080 mutex_enter(&pmportinfo->pmport_mutex); 14081 pmportinfo->pmport_state &= ~(SATA_STATE_PROBED | 14082 SATA_STATE_PROBING); 14083 mutex_exit(&pmportinfo->pmport_mutex); 14084 } 14085 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14086 14087 /* Just let HBA driver to deactivate port */ 14088 sata_device->satadev_addr.qual = qual; 14089 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 14090 (SATA_DIP(sata_hba_inst), sata_device); 14091 14092 /* 14093 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 14094 * without the hint 14095 */ 14096 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 14097 SE_NO_HINT); 14098 14099 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14100 sata_update_port_info(sata_hba_inst, sata_device); 14101 if (qual == SATA_ADDR_CPORT) { 14102 if (rval != SATA_SUCCESS) { 14103 /* 14104 * Port deactivation failure - do not change port state 14105 * unless the state returned by HBA indicates a port 14106 * failure. 14107 */ 14108 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 14109 SATA_CPORT_STATE(sata_hba_inst, cport) = 14110 SATA_PSTATE_FAILED; 14111 } 14112 SATA_LOG_D((sata_hba_inst, CE_WARN, 14113 "sata_hba_ioctl: port deactivate: " 14114 "cannot deactivate SATA port %d", cport)); 14115 rv = EIO; 14116 } else { 14117 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 14118 } 14119 } else { 14120 mutex_enter(&pmportinfo->pmport_mutex); 14121 if (rval != SATA_SUCCESS) { 14122 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 14123 SATA_PMPORT_STATE(sata_hba_inst, cport, 14124 pmport) = SATA_PSTATE_FAILED; 14125 } 14126 SATA_LOG_D((sata_hba_inst, CE_WARN, 14127 "sata_hba_ioctl: port deactivate: " 14128 "cannot deactivate SATA port %d:%d", 14129 cport, pmport)); 14130 rv = EIO; 14131 } else { 14132 pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN; 14133 } 14134 mutex_exit(&pmportinfo->pmport_mutex); 14135 } 14136 14137 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14138 14139 return (rv); 14140 } 14141 14142 /* 14143 * Process ioctl port activate request. 14144 * 14145 * NOTE: Port multiplier is supported now. 14146 */ 14147 static int 14148 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst, 14149 sata_device_t *sata_device) 14150 { 14151 int cport, pmport, qual; 14152 sata_cport_info_t *cportinfo; 14153 sata_pmport_info_t *pmportinfo = NULL; 14154 boolean_t dev_existed = TRUE; 14155 14156 /* Sanity check */ 14157 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) 14158 return (ENOTSUP); 14159 14160 cport = sata_device->satadev_addr.cport; 14161 pmport = sata_device->satadev_addr.pmport; 14162 qual = sata_device->satadev_addr.qual; 14163 14164 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 14165 14166 /* 14167 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL()) 14168 * is a device. But what we are dealing with is port/pmport. 14169 */ 14170 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 14171 if (qual == SATA_ADDR_DCPORT) 14172 sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT; 14173 else 14174 sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT; 14175 14176 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14177 if (qual == SATA_ADDR_PMPORT) { 14178 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 14179 if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN || 14180 pmportinfo->pmport_dev_type == SATA_DTYPE_NONE) 14181 dev_existed = FALSE; 14182 } else { /* cport */ 14183 if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN || 14184 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 14185 dev_existed = FALSE; 14186 } 14187 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14188 14189 /* Just let HBA driver to activate port, if necessary */ 14190 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 14191 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 14192 /* 14193 * Port activation failure - do not change port state unless 14194 * the state returned by HBA indicates a port failure. 14195 */ 14196 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14197 cport)->cport_mutex); 14198 sata_update_port_info(sata_hba_inst, sata_device); 14199 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 14200 if (qual == SATA_ADDR_PMPORT) { 14201 mutex_enter(&pmportinfo->pmport_mutex); 14202 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 14203 mutex_exit(&pmportinfo->pmport_mutex); 14204 } else 14205 cportinfo->cport_state = SATA_PSTATE_FAILED; 14206 14207 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14208 cport)->cport_mutex); 14209 SATA_LOG_D((sata_hba_inst, CE_WARN, 14210 "sata_hba_ioctl: port activate: cannot activate " 14211 "SATA port %d:%d", cport, pmport)); 14212 return (EIO); 14213 } 14214 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14215 } 14216 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14217 if (qual == SATA_ADDR_PMPORT) { 14218 mutex_enter(&pmportinfo->pmport_mutex); 14219 pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN; 14220 mutex_exit(&pmportinfo->pmport_mutex); 14221 } else 14222 cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN; 14223 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14224 14225 /* 14226 * Re-probe port to find its current state and possibly attached device. 14227 * Port re-probing may change the cportinfo device type if device is 14228 * found attached. 14229 * If port probing failed, the device type would be set to 14230 * SATA_DTYPE_NONE. 14231 */ 14232 (void) sata_reprobe_port(sata_hba_inst, sata_device, 14233 SATA_DEV_IDENTIFY_RETRY); 14234 14235 /* 14236 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 14237 * without the hint. 14238 */ 14239 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 14240 SE_NO_HINT); 14241 14242 if (dev_existed == FALSE) { 14243 if (qual == SATA_ADDR_PMPORT && 14244 pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 14245 /* 14246 * That's the transition from the "inactive" port state 14247 * or the active port without a device attached to the 14248 * active port state with a device attached. 14249 */ 14250 sata_log(sata_hba_inst, CE_WARN, 14251 "SATA device detected at port %d:%d", 14252 cport, pmport); 14253 } else if (qual == SATA_ADDR_CPORT && 14254 cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 14255 /* 14256 * That's the transition from the "inactive" port state 14257 * or the active port without a device attached to the 14258 * active port state with a device attached. 14259 */ 14260 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 14261 sata_log(sata_hba_inst, CE_WARN, 14262 "SATA device detected at port %d", cport); 14263 } else { 14264 sata_log(sata_hba_inst, CE_WARN, 14265 "SATA port multiplier detected at port %d", 14266 cport); 14267 } 14268 } 14269 } 14270 return (0); 14271 } 14272 14273 14274 14275 /* 14276 * Process ioctl reset port request. 14277 * 14278 * NOTE: Port-Multiplier is supported. 14279 */ 14280 static int 14281 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst, 14282 sata_device_t *sata_device) 14283 { 14284 int cport, pmport, qual; 14285 int rv = 0; 14286 14287 cport = sata_device->satadev_addr.cport; 14288 pmport = sata_device->satadev_addr.pmport; 14289 qual = sata_device->satadev_addr.qual; 14290 14291 /* 14292 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL()) 14293 * is a device. But what we are dealing with is port/pmport. 14294 */ 14295 if (qual == SATA_ADDR_DCPORT) 14296 sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT; 14297 else 14298 sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT; 14299 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT); 14300 14301 /* Sanity check */ 14302 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 14303 SATA_LOG_D((sata_hba_inst, CE_WARN, 14304 "sata_hba_ioctl: sata_hba_tran missing required " 14305 "function sata_tran_reset_dport")); 14306 return (ENOTSUP); 14307 } 14308 14309 /* Ask HBA to reset port */ 14310 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 14311 sata_device) != SATA_SUCCESS) { 14312 SATA_LOG_D((sata_hba_inst, CE_WARN, 14313 "sata_hba_ioctl: reset port: failed %d:%d", 14314 cport, pmport)); 14315 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 14316 cport_mutex); 14317 sata_update_port_info(sata_hba_inst, sata_device); 14318 if (qual == SATA_ADDR_CPORT) 14319 SATA_CPORT_STATE(sata_hba_inst, cport) = 14320 SATA_PSTATE_FAILED; 14321 else { 14322 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, 14323 pmport)); 14324 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 14325 SATA_PSTATE_FAILED; 14326 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, 14327 pmport)); 14328 } 14329 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 14330 cport_mutex); 14331 rv = EIO; 14332 } 14333 /* 14334 * Beacuse the port was reset, it should be probed and 14335 * attached device reinitialized. At this point the 14336 * port state is unknown - it's state is HBA-specific. 14337 * Re-probe port to get its state. 14338 */ 14339 if (sata_reprobe_port(sata_hba_inst, sata_device, 14340 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) { 14341 rv = EIO; 14342 } 14343 return (rv); 14344 } 14345 14346 /* 14347 * Process ioctl reset device request. 14348 * 14349 * NOTE: Port multiplier is supported. 14350 */ 14351 static int 14352 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst, 14353 sata_device_t *sata_device) 14354 { 14355 sata_drive_info_t *sdinfo = NULL; 14356 sata_pmult_info_t *pmultinfo = NULL; 14357 int cport, pmport; 14358 int rv = 0; 14359 14360 /* Sanity check */ 14361 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 14362 SATA_LOG_D((sata_hba_inst, CE_WARN, 14363 "sata_hba_ioctl: sata_hba_tran missing required " 14364 "function sata_tran_reset_dport")); 14365 return (ENOTSUP); 14366 } 14367 14368 cport = sata_device->satadev_addr.cport; 14369 pmport = sata_device->satadev_addr.pmport; 14370 14371 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14372 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) { 14373 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == 14374 SATA_DTYPE_PMULT) 14375 pmultinfo = SATA_CPORT_INFO(sata_hba_inst, cport)-> 14376 cport_devp.cport_sata_pmult; 14377 else 14378 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 14379 sata_device->satadev_addr.cport); 14380 } else { /* port multiplier */ 14381 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 14382 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 14383 sata_device->satadev_addr.cport, 14384 sata_device->satadev_addr.pmport); 14385 } 14386 if (sdinfo == NULL && pmultinfo == NULL) { 14387 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14388 return (EINVAL); 14389 } 14390 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14391 14392 /* Ask HBA to reset device */ 14393 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 14394 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 14395 SATA_LOG_D((sata_hba_inst, CE_WARN, 14396 "sata_hba_ioctl: reset device: failed at port %d:%d", 14397 cport, pmport)); 14398 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 14399 cport_mutex); 14400 sata_update_port_info(sata_hba_inst, sata_device); 14401 /* 14402 * Device info structure remains attached. Another device reset 14403 * or port disconnect/connect and re-probing is 14404 * needed to change it's state 14405 */ 14406 if (sdinfo != NULL) { 14407 sdinfo->satadrv_state &= ~SATA_STATE_READY; 14408 sdinfo->satadrv_state |= SATA_DSTATE_FAILED; 14409 } else if (pmultinfo != NULL) { 14410 pmultinfo->pmult_state &= ~SATA_STATE_READY; 14411 pmultinfo->pmult_state |= SATA_DSTATE_FAILED; 14412 } 14413 14414 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 14415 rv = EIO; 14416 } 14417 /* 14418 * If attached device was a port multiplier, some extra processing 14419 * may be needed to bring it back. SATA specification requies a 14420 * mandatory software reset on host port to reliably enumerate a port 14421 * multiplier, the HBA driver should handle that after reset 14422 * operation. 14423 */ 14424 return (rv); 14425 } 14426 14427 14428 /* 14429 * Process ioctl reset all request. 14430 */ 14431 static int 14432 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst) 14433 { 14434 sata_device_t sata_device; 14435 int rv = 0; 14436 int tcport; 14437 int tpmport = 0; 14438 14439 sata_device.satadev_rev = SATA_DEVICE_REV; 14440 14441 /* 14442 * There is no protection here for configured devices. 14443 */ 14444 /* Sanity check */ 14445 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 14446 SATA_LOG_D((sata_hba_inst, CE_WARN, 14447 "sata_hba_ioctl: sata_hba_tran missing required " 14448 "function sata_tran_reset_dport")); 14449 return (ENOTSUP); 14450 } 14451 14452 /* 14453 * Need to lock all ports, not just one. 14454 * If any port is locked by event processing, fail the whole operation. 14455 * One port is already locked, but for simplicity lock it again. 14456 */ 14457 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) { 14458 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14459 cport_mutex); 14460 if (((SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14461 cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) { 14462 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14463 cport_mutex); 14464 rv = EBUSY; 14465 break; 14466 } else { 14467 /* 14468 * It is enough to lock cport in command-based 14469 * switching mode. 14470 */ 14471 SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14472 cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 14473 } 14474 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14475 cport_mutex); 14476 } 14477 14478 if (rv == 0) { 14479 /* 14480 * All cports were successfully locked. 14481 * Reset main SATA controller. 14482 * Set the device address to port 0, to have a valid device 14483 * address. 14484 */ 14485 sata_device.satadev_addr.qual = SATA_ADDR_CNTRL; 14486 sata_device.satadev_addr.cport = 0; 14487 sata_device.satadev_addr.pmport = 0; 14488 14489 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 14490 (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) { 14491 SATA_LOG_D((sata_hba_inst, CE_WARN, 14492 "sata_hba_ioctl: reset controller failed")); 14493 return (EIO); 14494 } 14495 /* 14496 * Because ports were reset, port states are unknown. 14497 * They should be re-probed to get their state and 14498 * attached devices should be reinitialized. 14499 */ 14500 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); 14501 tcport++) { 14502 sata_device.satadev_addr.cport = tcport; 14503 sata_device.satadev_addr.pmport = tpmport; 14504 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 14505 14506 /* 14507 * The sata_reprobe_port() will mark a 14508 * SATA_EVNT_DEVICE_RESET event on the port 14509 * multiplier, all its sub-ports will be probed by 14510 * sata daemon afterwards. 14511 */ 14512 if (sata_reprobe_port(sata_hba_inst, &sata_device, 14513 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 14514 rv = EIO; 14515 } 14516 } 14517 /* 14518 * Unlock all ports 14519 */ 14520 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) { 14521 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14522 cport_mutex); 14523 SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14524 cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY; 14525 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 14526 cport_mutex); 14527 } 14528 14529 /* 14530 * This operation returns EFAULT if either reset 14531 * controller failed or a re-probing of any port failed. 14532 */ 14533 return (rv); 14534 } 14535 14536 14537 /* 14538 * Process ioctl port self test request. 14539 * 14540 * NOTE: Port multiplier code is not completed nor tested. 14541 */ 14542 static int 14543 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst, 14544 sata_device_t *sata_device) 14545 { 14546 int cport, pmport, qual; 14547 int rv = 0; 14548 14549 /* Sanity check */ 14550 if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL) 14551 return (ENOTSUP); 14552 14553 cport = sata_device->satadev_addr.cport; 14554 pmport = sata_device->satadev_addr.pmport; 14555 qual = sata_device->satadev_addr.qual; 14556 14557 /* 14558 * There is no protection here for a configured 14559 * device attached to this port. 14560 */ 14561 14562 if ((*SATA_SELFTEST_FUNC(sata_hba_inst)) 14563 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 14564 SATA_LOG_D((sata_hba_inst, CE_WARN, 14565 "sata_hba_ioctl: port selftest: " 14566 "failed port %d:%d", cport, pmport)); 14567 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 14568 cport_mutex); 14569 sata_update_port_info(sata_hba_inst, sata_device); 14570 if (qual == SATA_ADDR_CPORT) 14571 SATA_CPORT_STATE(sata_hba_inst, cport) = 14572 SATA_PSTATE_FAILED; 14573 else { /* port multiplier device port */ 14574 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, 14575 cport, pmport)); 14576 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 14577 SATA_PSTATE_FAILED; 14578 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, 14579 cport, pmport)); 14580 } 14581 14582 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 14583 cport_mutex); 14584 return (EIO); 14585 } 14586 /* 14587 * Beacuse the port was reset in the course of testing, it should be 14588 * re-probed and attached device state should be restored. At this 14589 * point the port state is unknown - it's state is HBA-specific. 14590 * Force port re-probing to get it into a known state. 14591 */ 14592 if (sata_reprobe_port(sata_hba_inst, sata_device, 14593 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 14594 rv = EIO; 14595 return (rv); 14596 } 14597 14598 14599 /* 14600 * sata_cfgadm_state: 14601 * Use the sata port state and state of the target node to figure out 14602 * the cfgadm_state. 14603 * 14604 * The port argument is a value with encoded cport, 14605 * pmport and address qualifier, in the same manner as a scsi target number. 14606 * SCSI_TO_SATA_CPORT macro extracts cport number, 14607 * SCSI_TO_SATA_PMPORT extracts pmport number and 14608 * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag. 14609 * 14610 * Port multiplier is supported. 14611 */ 14612 14613 static void 14614 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port, 14615 devctl_ap_state_t *ap_state) 14616 { 14617 uint8_t cport, pmport, qual; 14618 uint32_t port_state, pmult_state; 14619 uint32_t dev_type; 14620 sata_drive_info_t *sdinfo; 14621 14622 cport = SCSI_TO_SATA_CPORT(port); 14623 pmport = SCSI_TO_SATA_PMPORT(port); 14624 qual = SCSI_TO_SATA_ADDR_QUAL(port); 14625 14626 /* Check cport state */ 14627 port_state = SATA_CPORT_STATE(sata_hba_inst, cport); 14628 if (port_state & SATA_PSTATE_SHUTDOWN || 14629 port_state & SATA_PSTATE_FAILED) { 14630 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 14631 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 14632 if (port_state & SATA_PSTATE_FAILED) 14633 ap_state->ap_condition = AP_COND_FAILED; 14634 else 14635 ap_state->ap_condition = AP_COND_UNKNOWN; 14636 14637 return; 14638 } 14639 14640 /* cport state is okay. Now check pmport state */ 14641 if (qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) { 14642 /* Sanity check */ 14643 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 14644 SATA_DTYPE_PMULT || SATA_PMPORT_INFO(sata_hba_inst, 14645 cport, pmport) == NULL) 14646 return; 14647 port_state = SATA_PMPORT_STATE(sata_hba_inst, cport, pmport); 14648 if (port_state & SATA_PSTATE_SHUTDOWN || 14649 port_state & SATA_PSTATE_FAILED) { 14650 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 14651 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 14652 if (port_state & SATA_PSTATE_FAILED) 14653 ap_state->ap_condition = AP_COND_FAILED; 14654 else 14655 ap_state->ap_condition = AP_COND_UNKNOWN; 14656 14657 return; 14658 } 14659 } 14660 14661 /* Port is enabled and ready */ 14662 if (qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_CPORT) 14663 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, cport); 14664 else 14665 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport); 14666 14667 switch (dev_type) { 14668 case SATA_DTYPE_NONE: 14669 { 14670 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 14671 ap_state->ap_condition = AP_COND_OK; 14672 /* No device attached */ 14673 ap_state->ap_rstate = AP_RSTATE_EMPTY; 14674 break; 14675 } 14676 case SATA_DTYPE_PMULT: 14677 { 14678 /* Need to check port multiplier state */ 14679 ASSERT(qual == SATA_ADDR_DCPORT); 14680 pmult_state = SATA_PMULT_INFO(sata_hba_inst, cport)-> 14681 pmult_state; 14682 if (pmult_state & (SATA_PSTATE_SHUTDOWN|SATA_PSTATE_FAILED)) { 14683 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 14684 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 14685 if (pmult_state & SATA_PSTATE_FAILED) 14686 ap_state->ap_condition = AP_COND_FAILED; 14687 else 14688 ap_state->ap_condition = AP_COND_UNKNOWN; 14689 14690 return; 14691 } 14692 14693 /* Port multiplier is not configurable */ 14694 ap_state->ap_ostate = AP_OSTATE_CONFIGURED; 14695 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 14696 ap_state->ap_condition = AP_COND_OK; 14697 break; 14698 } 14699 14700 case SATA_DTYPE_ATADISK: 14701 case SATA_DTYPE_ATAPICD: 14702 case SATA_DTYPE_ATAPITAPE: 14703 case SATA_DTYPE_ATAPIDISK: 14704 { 14705 dev_info_t *tdip = NULL; 14706 dev_info_t *dip = NULL; 14707 int circ; 14708 14709 dip = SATA_DIP(sata_hba_inst); 14710 tdip = sata_get_target_dip(dip, cport, pmport); 14711 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 14712 if (tdip != NULL) { 14713 ndi_devi_enter(dip, &circ); 14714 mutex_enter(&(DEVI(tdip)->devi_lock)); 14715 if (DEVI_IS_DEVICE_REMOVED(tdip)) { 14716 /* 14717 * There could be the case where previously 14718 * configured and opened device was removed 14719 * and unknown device was plugged. 14720 * In such case we want to show a device, and 14721 * its configured or unconfigured state but 14722 * indicate unusable condition untill the 14723 * old target node is released and removed. 14724 */ 14725 ap_state->ap_condition = AP_COND_UNUSABLE; 14726 } else { 14727 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, 14728 cport)); 14729 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 14730 cport); 14731 if (sdinfo != NULL) { 14732 if ((sdinfo->satadrv_state & 14733 SATA_DSTATE_FAILED) != 0) 14734 ap_state->ap_condition = 14735 AP_COND_FAILED; 14736 else 14737 ap_state->ap_condition = 14738 AP_COND_OK; 14739 } else { 14740 ap_state->ap_condition = 14741 AP_COND_UNKNOWN; 14742 } 14743 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, 14744 cport)); 14745 } 14746 if ((DEVI_IS_DEVICE_OFFLINE(tdip)) || 14747 (DEVI_IS_DEVICE_DOWN(tdip))) { 14748 ap_state->ap_ostate = 14749 AP_OSTATE_UNCONFIGURED; 14750 } else { 14751 ap_state->ap_ostate = 14752 AP_OSTATE_CONFIGURED; 14753 } 14754 mutex_exit(&(DEVI(tdip)->devi_lock)); 14755 ndi_devi_exit(dip, circ); 14756 } else { 14757 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 14758 ap_state->ap_condition = AP_COND_UNKNOWN; 14759 } 14760 break; 14761 } 14762 default: 14763 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 14764 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 14765 ap_state->ap_condition = AP_COND_UNKNOWN; 14766 /* 14767 * This is actually internal error condition (non fatal), 14768 * because we have already checked all defined device types. 14769 */ 14770 SATA_LOG_D((sata_hba_inst, CE_WARN, 14771 "sata_cfgadm_state: Internal error: " 14772 "unknown device type")); 14773 break; 14774 } 14775 } 14776 14777 14778 /* 14779 * Process ioctl get device path request. 14780 * 14781 * NOTE: Port multiplier has no target dip. Devices connected to port 14782 * multiplier have target node attached to the HBA node. The only difference 14783 * between them and the directly-attached device node is a target address. 14784 */ 14785 static int 14786 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst, 14787 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 14788 { 14789 char path[MAXPATHLEN]; 14790 uint32_t size; 14791 dev_info_t *tdip; 14792 14793 (void) strcpy(path, "/devices"); 14794 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 14795 &sata_device->satadev_addr)) == NULL) { 14796 /* 14797 * No such device. If this is a request for a size, do not 14798 * return EINVAL for non-existing target, because cfgadm 14799 * will then indicate a meaningless ioctl failure. 14800 * If this is a request for a path, indicate invalid 14801 * argument. 14802 */ 14803 if (ioc->get_size == 0) 14804 return (EINVAL); 14805 } else { 14806 (void) ddi_pathname(tdip, path + strlen(path)); 14807 } 14808 size = strlen(path) + 1; 14809 14810 if (ioc->get_size != 0) { 14811 if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz, 14812 mode) != 0) 14813 return (EFAULT); 14814 } else { 14815 if (ioc->bufsiz != size) 14816 return (EINVAL); 14817 14818 else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz, 14819 mode) != 0) 14820 return (EFAULT); 14821 } 14822 return (0); 14823 } 14824 14825 /* 14826 * Process ioctl get attachment point type request. 14827 * 14828 * NOTE: Port multiplier is supported. 14829 */ 14830 static int 14831 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst, 14832 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 14833 { 14834 uint32_t type_len; 14835 const char *ap_type; 14836 int dev_type; 14837 14838 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 14839 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, 14840 sata_device->satadev_addr.cport); 14841 else /* pmport */ 14842 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, 14843 sata_device->satadev_addr.cport, 14844 sata_device->satadev_addr.pmport); 14845 14846 switch (dev_type) { 14847 case SATA_DTYPE_NONE: 14848 ap_type = "port"; 14849 break; 14850 14851 case SATA_DTYPE_ATADISK: 14852 case SATA_DTYPE_ATAPIDISK: 14853 ap_type = "disk"; 14854 break; 14855 14856 case SATA_DTYPE_ATAPICD: 14857 ap_type = "cd/dvd"; 14858 break; 14859 14860 case SATA_DTYPE_ATAPITAPE: 14861 ap_type = "tape"; 14862 break; 14863 14864 case SATA_DTYPE_PMULT: 14865 ap_type = "sata-pmult"; 14866 break; 14867 14868 case SATA_DTYPE_UNKNOWN: 14869 ap_type = "unknown"; 14870 break; 14871 14872 default: 14873 ap_type = "unsupported"; 14874 break; 14875 14876 } /* end of dev_type switch */ 14877 14878 type_len = strlen(ap_type) + 1; 14879 14880 if (ioc->get_size) { 14881 if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz, 14882 mode) != 0) 14883 return (EFAULT); 14884 } else { 14885 if (ioc->bufsiz != type_len) 14886 return (EINVAL); 14887 14888 if (ddi_copyout((void *)ap_type, ioc->buf, 14889 ioc->bufsiz, mode) != 0) 14890 return (EFAULT); 14891 } 14892 return (0); 14893 14894 } 14895 14896 /* 14897 * Process ioctl get device model info request. 14898 * This operation should return to cfgadm the device model 14899 * information string 14900 * 14901 * NOTE: Port multiplier is supported. 14902 */ 14903 static int 14904 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst, 14905 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 14906 { 14907 sata_drive_info_t *sdinfo; 14908 uint32_t info_len; 14909 char ap_info[SATA_ID_MODEL_LEN + 1]; 14910 14911 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14912 sata_device->satadev_addr.cport)->cport_mutex); 14913 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 14914 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 14915 sata_device->satadev_addr.cport); 14916 else /* port multiplier */ 14917 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 14918 sata_device->satadev_addr.cport, 14919 sata_device->satadev_addr.pmport); 14920 if (sdinfo == NULL) { 14921 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14922 sata_device->satadev_addr.cport)->cport_mutex); 14923 return (EINVAL); 14924 } 14925 14926 #ifdef _LITTLE_ENDIAN 14927 swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN); 14928 #else /* _LITTLE_ENDIAN */ 14929 bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN); 14930 #endif /* _LITTLE_ENDIAN */ 14931 14932 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14933 sata_device->satadev_addr.cport)->cport_mutex); 14934 14935 ap_info[SATA_ID_MODEL_LEN] = '\0'; 14936 14937 info_len = strlen(ap_info) + 1; 14938 14939 if (ioc->get_size) { 14940 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 14941 mode) != 0) 14942 return (EFAULT); 14943 } else { 14944 if (ioc->bufsiz < info_len) 14945 return (EINVAL); 14946 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 14947 mode) != 0) 14948 return (EFAULT); 14949 } 14950 return (0); 14951 } 14952 14953 14954 /* 14955 * Process ioctl get device firmware revision info request. 14956 * This operation should return to cfgadm the device firmware revision 14957 * information string 14958 * 14959 * Port multiplier is supported. 14960 */ 14961 static int 14962 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst, 14963 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 14964 { 14965 sata_drive_info_t *sdinfo; 14966 uint32_t info_len; 14967 char ap_info[SATA_ID_FW_LEN + 1]; 14968 14969 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14970 sata_device->satadev_addr.cport)->cport_mutex); 14971 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 14972 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 14973 sata_device->satadev_addr.cport); 14974 else /* port multiplier */ 14975 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 14976 sata_device->satadev_addr.cport, 14977 sata_device->satadev_addr.pmport); 14978 if (sdinfo == NULL) { 14979 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14980 sata_device->satadev_addr.cport)->cport_mutex); 14981 return (EINVAL); 14982 } 14983 14984 #ifdef _LITTLE_ENDIAN 14985 swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN); 14986 #else /* _LITTLE_ENDIAN */ 14987 bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN); 14988 #endif /* _LITTLE_ENDIAN */ 14989 14990 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14991 sata_device->satadev_addr.cport)->cport_mutex); 14992 14993 ap_info[SATA_ID_FW_LEN] = '\0'; 14994 14995 info_len = strlen(ap_info) + 1; 14996 14997 if (ioc->get_size) { 14998 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 14999 mode) != 0) 15000 return (EFAULT); 15001 } else { 15002 if (ioc->bufsiz < info_len) 15003 return (EINVAL); 15004 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 15005 mode) != 0) 15006 return (EFAULT); 15007 } 15008 return (0); 15009 } 15010 15011 15012 /* 15013 * Process ioctl get device serial number info request. 15014 * This operation should return to cfgadm the device serial number string. 15015 * 15016 * NOTE: Port multiplier is supported. 15017 */ 15018 static int 15019 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst, 15020 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 15021 { 15022 sata_drive_info_t *sdinfo; 15023 uint32_t info_len; 15024 char ap_info[SATA_ID_SERIAL_LEN + 1]; 15025 15026 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15027 sata_device->satadev_addr.cport)->cport_mutex); 15028 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 15029 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 15030 sata_device->satadev_addr.cport); 15031 else /* port multiplier */ 15032 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 15033 sata_device->satadev_addr.cport, 15034 sata_device->satadev_addr.pmport); 15035 if (sdinfo == NULL) { 15036 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15037 sata_device->satadev_addr.cport)->cport_mutex); 15038 return (EINVAL); 15039 } 15040 15041 #ifdef _LITTLE_ENDIAN 15042 swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN); 15043 #else /* _LITTLE_ENDIAN */ 15044 bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN); 15045 #endif /* _LITTLE_ENDIAN */ 15046 15047 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15048 sata_device->satadev_addr.cport)->cport_mutex); 15049 15050 ap_info[SATA_ID_SERIAL_LEN] = '\0'; 15051 15052 info_len = strlen(ap_info) + 1; 15053 15054 if (ioc->get_size) { 15055 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 15056 mode) != 0) 15057 return (EFAULT); 15058 } else { 15059 if (ioc->bufsiz < info_len) 15060 return (EINVAL); 15061 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 15062 mode) != 0) 15063 return (EFAULT); 15064 } 15065 return (0); 15066 } 15067 15068 15069 /* 15070 * Preset scsi extended sense data (to NO SENSE) 15071 * First 18 bytes of the sense data are preset to current valid sense 15072 * with a key NO SENSE data. 15073 * 15074 * Returns void 15075 */ 15076 static void 15077 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense) 15078 { 15079 sense->es_valid = 1; /* Valid sense */ 15080 sense->es_class = CLASS_EXTENDED_SENSE; /* 0x70 - current err */ 15081 sense->es_key = KEY_NO_SENSE; 15082 sense->es_info_1 = 0; 15083 sense->es_info_2 = 0; 15084 sense->es_info_3 = 0; 15085 sense->es_info_4 = 0; 15086 sense->es_add_len = 10; /* Additional length - replace with a def */ 15087 sense->es_cmd_info[0] = 0; 15088 sense->es_cmd_info[1] = 0; 15089 sense->es_cmd_info[2] = 0; 15090 sense->es_cmd_info[3] = 0; 15091 sense->es_add_code = 0; 15092 sense->es_qual_code = 0; 15093 } 15094 15095 /* 15096 * Register a legacy cmdk-style devid for the target (disk) device. 15097 * 15098 * Note: This function is called only when the HBA devinfo node has the 15099 * property "use-cmdk-devid-format" set. This property indicates that 15100 * devid compatible with old cmdk (target) driver is to be generated 15101 * for any target device attached to this controller. This will take 15102 * precedence over the devid generated by sd (target) driver. 15103 * This function is derived from cmdk_devid_setup() function in cmdk.c. 15104 */ 15105 static void 15106 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo) 15107 { 15108 char *hwid; 15109 int modlen; 15110 int serlen; 15111 int rval; 15112 ddi_devid_t devid; 15113 15114 /* 15115 * device ID is a concatanation of model number, "=", serial number. 15116 */ 15117 hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP); 15118 bcopy(&sdinfo->satadrv_id.ai_model, hwid, 15119 sizeof (sdinfo->satadrv_id.ai_model)); 15120 swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model)); 15121 modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model)); 15122 if (modlen == 0) 15123 goto err; 15124 hwid[modlen++] = '='; 15125 bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen], 15126 sizeof (sdinfo->satadrv_id.ai_drvser)); 15127 swab(&hwid[modlen], &hwid[modlen], 15128 sizeof (sdinfo->satadrv_id.ai_drvser)); 15129 serlen = sata_check_modser(&hwid[modlen], 15130 sizeof (sdinfo->satadrv_id.ai_drvser)); 15131 if (serlen == 0) 15132 goto err; 15133 hwid[modlen + serlen] = 0; /* terminate the hwid string */ 15134 15135 /* initialize/register devid */ 15136 if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL, 15137 (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) { 15138 rval = ddi_devid_register(dip, devid); 15139 /* 15140 * Free up the allocated devid buffer. 15141 * NOTE: This doesn't mean unregistering devid. 15142 */ 15143 ddi_devid_free(devid); 15144 } 15145 15146 if (rval != DDI_SUCCESS) 15147 cmn_err(CE_WARN, "sata: failed to create devid for the disk" 15148 " on port %d", sdinfo->satadrv_addr.cport); 15149 err: 15150 kmem_free(hwid, LEGACY_HWID_LEN); 15151 } 15152 15153 /* 15154 * valid model/serial string must contain a non-zero non-space characters. 15155 * trim trailing spaces/NULLs. 15156 */ 15157 static int 15158 sata_check_modser(char *buf, int buf_len) 15159 { 15160 boolean_t ret; 15161 char *s; 15162 int i; 15163 int tb; 15164 char ch; 15165 15166 ret = B_FALSE; 15167 s = buf; 15168 for (i = 0; i < buf_len; i++) { 15169 ch = *s++; 15170 if (ch != ' ' && ch != '\0') 15171 tb = i + 1; 15172 if (ch != ' ' && ch != '\0' && ch != '0') 15173 ret = B_TRUE; 15174 } 15175 15176 if (ret == B_FALSE) 15177 return (0); /* invalid string */ 15178 15179 return (tb); /* return length */ 15180 } 15181 15182 /* 15183 * sata_set_drive_features function compares current device features setting 15184 * with the saved device features settings and, if there is a difference, 15185 * it restores device features setting to the previously saved state. 15186 * It also arbitrarily tries to select the highest supported DMA mode. 15187 * Device Identify or Identify Packet Device data has to be current. 15188 * At the moment read ahead and write cache are considered for all devices. 15189 * For atapi devices, Removable Media Status Notification is set in addition 15190 * to common features. 15191 * 15192 * This function cannot be called in the interrupt context (it may sleep). 15193 * 15194 * The input argument sdinfo should point to the drive info structure 15195 * to be updated after features are set. Note, that only 15196 * device (packet) identify data is updated, not the flags indicating the 15197 * supported features. 15198 * 15199 * Returns SATA_SUCCESS if successful or there was nothing to do. 15200 * Device Identify data in the drive info structure pointed to by the sdinfo 15201 * arguments is updated even when no features were set or changed. 15202 * 15203 * Returns SATA_FAILURE if device features could not be set or DMA mode 15204 * for a disk cannot be set and device identify data cannot be fetched. 15205 * 15206 * Returns SATA_RETRY if device features could not be set (other than disk 15207 * DMA mode) but the device identify data was fetched successfully. 15208 * 15209 * Note: This function may fail the port, making it inaccessible. 15210 * In such case the explicit port disconnect/connect or physical device 15211 * detach/attach is required to re-evaluate port state again. 15212 */ 15213 15214 static int 15215 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst, 15216 sata_drive_info_t *sdinfo, int restore) 15217 { 15218 int rval = SATA_SUCCESS; 15219 int rval_set; 15220 sata_drive_info_t new_sdinfo; 15221 char *finfo = "sata_set_drive_features: cannot"; 15222 char *finfox; 15223 int cache_op; 15224 15225 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 15226 new_sdinfo.satadrv_addr = sdinfo->satadrv_addr; 15227 new_sdinfo.satadrv_type = sdinfo->satadrv_type; 15228 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 15229 /* 15230 * Cannot get device identification - caller may retry later 15231 */ 15232 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15233 "%s fetch device identify data\n", finfo); 15234 return (SATA_FAILURE); 15235 } 15236 finfox = (restore != 0) ? " restore device features" : 15237 " initialize device features\n"; 15238 15239 switch (sdinfo->satadrv_type) { 15240 case SATA_DTYPE_ATADISK: 15241 /* Arbitrarily set UDMA mode */ 15242 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) != 15243 SATA_SUCCESS) { 15244 SATA_LOG_D((sata_hba_inst, CE_WARN, 15245 "%s set UDMA mode\n", finfo)); 15246 return (SATA_FAILURE); 15247 } 15248 break; 15249 case SATA_DTYPE_ATAPICD: 15250 case SATA_DTYPE_ATAPITAPE: 15251 case SATA_DTYPE_ATAPIDISK: 15252 /* Set Removable Media Status Notification, if necessary */ 15253 if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) && 15254 restore != 0) { 15255 if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) && 15256 (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))|| 15257 ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) && 15258 SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) { 15259 /* Current setting does not match saved one */ 15260 if (sata_set_rmsn(sata_hba_inst, sdinfo, 15261 sdinfo->satadrv_settings & 15262 SATA_DEV_RMSN) != SATA_SUCCESS) 15263 rval = SATA_FAILURE; 15264 } 15265 } 15266 /* 15267 * We have to set Multiword DMA or UDMA, if it is supported, as 15268 * we want to use DMA transfer mode whenever possible. 15269 * Some devices require explicit setting of the DMA mode. 15270 */ 15271 if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) { 15272 /* Set highest supported DMA mode */ 15273 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) != 15274 SATA_SUCCESS) { 15275 SATA_LOG_D((sata_hba_inst, CE_WARN, 15276 "%s set UDMA mode\n", finfo)); 15277 rval = SATA_FAILURE; 15278 } 15279 } 15280 break; 15281 } 15282 15283 if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) && 15284 !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) { 15285 /* 15286 * neither READ AHEAD nor WRITE CACHE is supported 15287 * - do nothing 15288 */ 15289 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15290 "settable features not supported\n", NULL); 15291 goto update_sdinfo; 15292 } 15293 15294 if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) && 15295 (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) && 15296 (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) && 15297 (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) { 15298 /* 15299 * both READ AHEAD and WRITE CACHE are enabled 15300 * - Nothing to do 15301 */ 15302 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15303 "no device features to set\n", NULL); 15304 goto update_sdinfo; 15305 } 15306 15307 cache_op = 0; 15308 15309 if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) { 15310 if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) && 15311 !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) { 15312 /* Enable read ahead / read cache */ 15313 cache_op = SATAC_SF_ENABLE_READ_AHEAD; 15314 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15315 "enabling read cache\n", NULL); 15316 } else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) && 15317 SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) { 15318 /* Disable read ahead / read cache */ 15319 cache_op = SATAC_SF_DISABLE_READ_AHEAD; 15320 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15321 "disabling read cache\n", NULL); 15322 } 15323 15324 if (cache_op != 0) { 15325 /* Try to set read cache mode */ 15326 rval_set = sata_set_cache_mode(sata_hba_inst, 15327 &new_sdinfo, cache_op); 15328 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS) 15329 rval = rval_set; 15330 } 15331 } 15332 15333 cache_op = 0; 15334 15335 if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) { 15336 if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) && 15337 !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) { 15338 /* Enable write cache */ 15339 cache_op = SATAC_SF_ENABLE_WRITE_CACHE; 15340 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15341 "enabling write cache\n", NULL); 15342 } else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) && 15343 SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) { 15344 /* Disable write cache */ 15345 cache_op = SATAC_SF_DISABLE_WRITE_CACHE; 15346 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 15347 "disabling write cache\n", NULL); 15348 } 15349 15350 if (cache_op != 0) { 15351 /* Try to set write cache mode */ 15352 rval_set = sata_set_cache_mode(sata_hba_inst, 15353 &new_sdinfo, cache_op); 15354 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS) 15355 rval = rval_set; 15356 } 15357 } 15358 if (rval != SATA_SUCCESS) 15359 SATA_LOG_D((sata_hba_inst, CE_WARN, 15360 "%s %s", finfo, finfox)); 15361 15362 update_sdinfo: 15363 /* 15364 * We need to fetch Device Identify data again 15365 */ 15366 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 15367 /* 15368 * Cannot get device identification - retry later 15369 */ 15370 SATA_LOG_D((sata_hba_inst, CE_WARN, 15371 "%s re-fetch device identify data\n", finfo)); 15372 rval = SATA_FAILURE; 15373 } 15374 /* Copy device sata info. */ 15375 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 15376 15377 return (rval); 15378 } 15379 15380 15381 /* 15382 * 15383 * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if 15384 * unable to determine. 15385 * 15386 * Cannot be called in an interrupt context. 15387 * 15388 * Called by sata_build_lsense_page_2f() 15389 */ 15390 15391 static int 15392 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst, 15393 sata_drive_info_t *sdinfo) 15394 { 15395 sata_pkt_t *spkt; 15396 sata_cmd_t *scmd; 15397 sata_pkt_txlate_t *spx; 15398 int rval; 15399 15400 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 15401 spx->txlt_sata_hba_inst = sata_hba_inst; 15402 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 15403 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 15404 if (spkt == NULL) { 15405 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15406 return (-1); 15407 } 15408 /* address is needed now */ 15409 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15410 15411 15412 /* Fill sata_pkt */ 15413 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15414 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 15415 /* Synchronous mode, no callback */ 15416 spkt->satapkt_comp = NULL; 15417 /* Timeout 30s */ 15418 spkt->satapkt_time = sata_default_pkt_time; 15419 15420 scmd = &spkt->satapkt_cmd; 15421 scmd->satacmd_flags.sata_special_regs = B_TRUE; 15422 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 15423 15424 /* Set up which registers need to be returned */ 15425 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE; 15426 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE; 15427 15428 /* Build SMART_RETURN_STATUS cmd in the sata_pkt */ 15429 scmd->satacmd_addr_type = 0; /* N/A */ 15430 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 15431 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 15432 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 15433 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 15434 scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS; 15435 scmd->satacmd_device_reg = 0; /* Always device 0 */ 15436 scmd->satacmd_cmd_reg = SATAC_SMART; 15437 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 15438 sdinfo->satadrv_addr.cport))); 15439 15440 15441 /* Send pkt to SATA HBA driver */ 15442 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 15443 SATA_TRAN_ACCEPTED || 15444 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 15445 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15446 sdinfo->satadrv_addr.cport))); 15447 /* 15448 * Whoops, no SMART RETURN STATUS 15449 */ 15450 rval = -1; 15451 } else { 15452 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15453 sdinfo->satadrv_addr.cport))); 15454 if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) { 15455 rval = -1; 15456 goto fail; 15457 } 15458 if (scmd->satacmd_status_reg & SATA_STATUS_ERR) { 15459 rval = -1; 15460 goto fail; 15461 } 15462 if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) && 15463 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2)) 15464 rval = 0; 15465 else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) && 15466 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4)) 15467 rval = 1; 15468 else { 15469 rval = -1; 15470 goto fail; 15471 } 15472 } 15473 fail: 15474 /* Free allocated resources */ 15475 sata_pkt_free(spx); 15476 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15477 15478 return (rval); 15479 } 15480 15481 /* 15482 * 15483 * Returns 0 if succeeded, -1 otherwise 15484 * 15485 * Cannot be called in an interrupt context. 15486 * 15487 */ 15488 static int 15489 sata_fetch_smart_data( 15490 sata_hba_inst_t *sata_hba_inst, 15491 sata_drive_info_t *sdinfo, 15492 struct smart_data *smart_data) 15493 { 15494 sata_pkt_t *spkt; 15495 sata_cmd_t *scmd; 15496 sata_pkt_txlate_t *spx; 15497 int rval; 15498 15499 #if ! defined(lint) 15500 ASSERT(sizeof (struct smart_data) == 512); 15501 #endif 15502 15503 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 15504 spx->txlt_sata_hba_inst = sata_hba_inst; 15505 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 15506 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 15507 if (spkt == NULL) { 15508 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15509 return (-1); 15510 } 15511 /* address is needed now */ 15512 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15513 15514 15515 /* Fill sata_pkt */ 15516 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15517 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 15518 /* Synchronous mode, no callback */ 15519 spkt->satapkt_comp = NULL; 15520 /* Timeout 30s */ 15521 spkt->satapkt_time = sata_default_pkt_time; 15522 15523 scmd = &spkt->satapkt_cmd; 15524 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 15525 15526 /* 15527 * Allocate buffer for SMART data 15528 */ 15529 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 15530 sizeof (struct smart_data)); 15531 if (scmd->satacmd_bp == NULL) { 15532 sata_pkt_free(spx); 15533 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15534 SATA_LOG_D((sata_hba_inst, CE_WARN, 15535 "sata_fetch_smart_data: " 15536 "cannot allocate buffer")); 15537 return (-1); 15538 } 15539 15540 15541 /* Build SMART_READ_DATA cmd in the sata_pkt */ 15542 scmd->satacmd_addr_type = 0; /* N/A */ 15543 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 15544 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 15545 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 15546 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 15547 scmd->satacmd_features_reg = SATA_SMART_READ_DATA; 15548 scmd->satacmd_device_reg = 0; /* Always device 0 */ 15549 scmd->satacmd_cmd_reg = SATAC_SMART; 15550 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 15551 sdinfo->satadrv_addr.cport))); 15552 15553 /* Send pkt to SATA HBA driver */ 15554 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 15555 SATA_TRAN_ACCEPTED || 15556 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 15557 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15558 sdinfo->satadrv_addr.cport))); 15559 /* 15560 * Whoops, no SMART DATA available 15561 */ 15562 rval = -1; 15563 goto fail; 15564 } else { 15565 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15566 sdinfo->satadrv_addr.cport))); 15567 if (spx->txlt_buf_dma_handle != NULL) { 15568 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 15569 DDI_DMA_SYNC_FORKERNEL); 15570 ASSERT(rval == DDI_SUCCESS); 15571 } 15572 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data, 15573 sizeof (struct smart_data)); 15574 } 15575 15576 fail: 15577 /* Free allocated resources */ 15578 sata_free_local_buffer(spx); 15579 sata_pkt_free(spx); 15580 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15581 15582 return (rval); 15583 } 15584 15585 /* 15586 * Used by LOG SENSE page 0x10 15587 * Reads (in synchronous mode) the self test log data using Read Log Ext cmd. 15588 * Note: cannot be called in the interrupt context. 15589 * 15590 * return 0 for success, -1 otherwise 15591 * 15592 */ 15593 static int 15594 sata_ext_smart_selftest_read_log( 15595 sata_hba_inst_t *sata_hba_inst, 15596 sata_drive_info_t *sdinfo, 15597 struct smart_ext_selftest_log *ext_selftest_log, 15598 uint16_t block_num) 15599 { 15600 sata_pkt_txlate_t *spx; 15601 sata_pkt_t *spkt; 15602 sata_cmd_t *scmd; 15603 int rval; 15604 15605 #if ! defined(lint) 15606 ASSERT(sizeof (struct smart_ext_selftest_log) == 512); 15607 #endif 15608 15609 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 15610 spx->txlt_sata_hba_inst = sata_hba_inst; 15611 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 15612 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 15613 if (spkt == NULL) { 15614 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15615 return (-1); 15616 } 15617 /* address is needed now */ 15618 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15619 15620 15621 /* Fill sata_pkt */ 15622 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15623 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 15624 /* Synchronous mode, no callback */ 15625 spkt->satapkt_comp = NULL; 15626 /* Timeout 30s */ 15627 spkt->satapkt_time = sata_default_pkt_time; 15628 15629 scmd = &spkt->satapkt_cmd; 15630 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 15631 15632 /* 15633 * Allocate buffer for SMART extended self-test log 15634 */ 15635 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 15636 sizeof (struct smart_ext_selftest_log)); 15637 if (scmd->satacmd_bp == NULL) { 15638 sata_pkt_free(spx); 15639 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15640 SATA_LOG_D((sata_hba_inst, CE_WARN, 15641 "sata_ext_smart_selftest_log: " 15642 "cannot allocate buffer")); 15643 return (-1); 15644 } 15645 15646 /* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */ 15647 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 15648 scmd->satacmd_sec_count_lsb = 1; /* One sector of selftest log */ 15649 scmd->satacmd_sec_count_msb = 0; /* One sector of selftest log */ 15650 scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE; 15651 scmd->satacmd_lba_low_msb = 0; 15652 scmd->satacmd_lba_mid_lsb = block_num & 0xff; 15653 scmd->satacmd_lba_mid_msb = block_num >> 8; 15654 scmd->satacmd_device_reg = 0; /* Always device 0 */ 15655 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 15656 15657 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 15658 sdinfo->satadrv_addr.cport))); 15659 15660 /* Send pkt to SATA HBA driver */ 15661 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 15662 SATA_TRAN_ACCEPTED || 15663 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 15664 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15665 sdinfo->satadrv_addr.cport))); 15666 15667 /* 15668 * Whoops, no SMART selftest log info available 15669 */ 15670 rval = -1; 15671 goto fail; 15672 } else { 15673 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15674 sdinfo->satadrv_addr.cport))); 15675 15676 if (spx->txlt_buf_dma_handle != NULL) { 15677 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 15678 DDI_DMA_SYNC_FORKERNEL); 15679 ASSERT(rval == DDI_SUCCESS); 15680 } 15681 bcopy(scmd->satacmd_bp->b_un.b_addr, 15682 (uint8_t *)ext_selftest_log, 15683 sizeof (struct smart_ext_selftest_log)); 15684 rval = 0; 15685 } 15686 15687 fail: 15688 /* Free allocated resources */ 15689 sata_free_local_buffer(spx); 15690 sata_pkt_free(spx); 15691 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15692 15693 return (rval); 15694 } 15695 15696 /* 15697 * Returns 0 for success, -1 otherwise 15698 * 15699 * SMART self-test log data is returned in buffer pointed to by selftest_log 15700 */ 15701 static int 15702 sata_smart_selftest_log( 15703 sata_hba_inst_t *sata_hba_inst, 15704 sata_drive_info_t *sdinfo, 15705 struct smart_selftest_log *selftest_log) 15706 { 15707 sata_pkt_t *spkt; 15708 sata_cmd_t *scmd; 15709 sata_pkt_txlate_t *spx; 15710 int rval; 15711 15712 #if ! defined(lint) 15713 ASSERT(sizeof (struct smart_selftest_log) == 512); 15714 #endif 15715 15716 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 15717 spx->txlt_sata_hba_inst = sata_hba_inst; 15718 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 15719 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 15720 if (spkt == NULL) { 15721 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15722 return (-1); 15723 } 15724 /* address is needed now */ 15725 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15726 15727 15728 /* Fill sata_pkt */ 15729 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15730 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 15731 /* Synchronous mode, no callback */ 15732 spkt->satapkt_comp = NULL; 15733 /* Timeout 30s */ 15734 spkt->satapkt_time = sata_default_pkt_time; 15735 15736 scmd = &spkt->satapkt_cmd; 15737 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 15738 15739 /* 15740 * Allocate buffer for SMART SELFTEST LOG 15741 */ 15742 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 15743 sizeof (struct smart_selftest_log)); 15744 if (scmd->satacmd_bp == NULL) { 15745 sata_pkt_free(spx); 15746 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15747 SATA_LOG_D((sata_hba_inst, CE_WARN, 15748 "sata_smart_selftest_log: " 15749 "cannot allocate buffer")); 15750 return (-1); 15751 } 15752 15753 /* Build SMART_READ_LOG cmd in the sata_pkt */ 15754 scmd->satacmd_addr_type = 0; /* N/A */ 15755 scmd->satacmd_sec_count_lsb = 1; /* One sector of SMART log */ 15756 scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE; 15757 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 15758 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 15759 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 15760 scmd->satacmd_device_reg = 0; /* Always device 0 */ 15761 scmd->satacmd_cmd_reg = SATAC_SMART; 15762 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 15763 sdinfo->satadrv_addr.cport))); 15764 15765 /* Send pkt to SATA HBA driver */ 15766 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 15767 SATA_TRAN_ACCEPTED || 15768 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 15769 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15770 sdinfo->satadrv_addr.cport))); 15771 /* 15772 * Whoops, no SMART DATA available 15773 */ 15774 rval = -1; 15775 goto fail; 15776 } else { 15777 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15778 sdinfo->satadrv_addr.cport))); 15779 if (spx->txlt_buf_dma_handle != NULL) { 15780 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 15781 DDI_DMA_SYNC_FORKERNEL); 15782 ASSERT(rval == DDI_SUCCESS); 15783 } 15784 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log, 15785 sizeof (struct smart_selftest_log)); 15786 rval = 0; 15787 } 15788 15789 fail: 15790 /* Free allocated resources */ 15791 sata_free_local_buffer(spx); 15792 sata_pkt_free(spx); 15793 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15794 15795 return (rval); 15796 } 15797 15798 15799 /* 15800 * Returns 0 for success, -1 otherwise 15801 * 15802 * SMART READ LOG data is returned in buffer pointed to by smart_log 15803 */ 15804 static int 15805 sata_smart_read_log( 15806 sata_hba_inst_t *sata_hba_inst, 15807 sata_drive_info_t *sdinfo, 15808 uint8_t *smart_log, /* where the data should be returned */ 15809 uint8_t which_log, /* which log should be returned */ 15810 uint8_t log_size) /* # of 512 bytes in log */ 15811 { 15812 sata_pkt_t *spkt; 15813 sata_cmd_t *scmd; 15814 sata_pkt_txlate_t *spx; 15815 int rval; 15816 15817 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 15818 spx->txlt_sata_hba_inst = sata_hba_inst; 15819 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 15820 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 15821 if (spkt == NULL) { 15822 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15823 return (-1); 15824 } 15825 /* address is needed now */ 15826 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15827 15828 15829 /* Fill sata_pkt */ 15830 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15831 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 15832 /* Synchronous mode, no callback */ 15833 spkt->satapkt_comp = NULL; 15834 /* Timeout 30s */ 15835 spkt->satapkt_time = sata_default_pkt_time; 15836 15837 scmd = &spkt->satapkt_cmd; 15838 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 15839 15840 /* 15841 * Allocate buffer for SMART READ LOG 15842 */ 15843 scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512); 15844 if (scmd->satacmd_bp == NULL) { 15845 sata_pkt_free(spx); 15846 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15847 SATA_LOG_D((sata_hba_inst, CE_WARN, 15848 "sata_smart_read_log: " "cannot allocate buffer")); 15849 return (-1); 15850 } 15851 15852 /* Build SMART_READ_LOG cmd in the sata_pkt */ 15853 scmd->satacmd_addr_type = 0; /* N/A */ 15854 scmd->satacmd_sec_count_lsb = log_size; /* what the caller asked for */ 15855 scmd->satacmd_lba_low_lsb = which_log; /* which log page */ 15856 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 15857 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 15858 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 15859 scmd->satacmd_device_reg = 0; /* Always device 0 */ 15860 scmd->satacmd_cmd_reg = SATAC_SMART; 15861 15862 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 15863 sdinfo->satadrv_addr.cport))); 15864 15865 /* Send pkt to SATA HBA driver */ 15866 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 15867 SATA_TRAN_ACCEPTED || 15868 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 15869 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15870 sdinfo->satadrv_addr.cport))); 15871 15872 /* 15873 * Whoops, no SMART DATA available 15874 */ 15875 rval = -1; 15876 goto fail; 15877 } else { 15878 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15879 sdinfo->satadrv_addr.cport))); 15880 15881 if (spx->txlt_buf_dma_handle != NULL) { 15882 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 15883 DDI_DMA_SYNC_FORKERNEL); 15884 ASSERT(rval == DDI_SUCCESS); 15885 } 15886 bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512); 15887 rval = 0; 15888 } 15889 15890 fail: 15891 /* Free allocated resources */ 15892 sata_free_local_buffer(spx); 15893 sata_pkt_free(spx); 15894 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15895 15896 return (rval); 15897 } 15898 15899 /* 15900 * Used by LOG SENSE page 0x10 15901 * 15902 * return 0 for success, -1 otherwise 15903 * 15904 */ 15905 static int 15906 sata_read_log_ext_directory( 15907 sata_hba_inst_t *sata_hba_inst, 15908 sata_drive_info_t *sdinfo, 15909 struct read_log_ext_directory *logdir) 15910 { 15911 sata_pkt_txlate_t *spx; 15912 sata_pkt_t *spkt; 15913 sata_cmd_t *scmd; 15914 int rval; 15915 15916 #if ! defined(lint) 15917 ASSERT(sizeof (struct read_log_ext_directory) == 512); 15918 #endif 15919 15920 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 15921 spx->txlt_sata_hba_inst = sata_hba_inst; 15922 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 15923 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 15924 if (spkt == NULL) { 15925 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15926 return (-1); 15927 } 15928 15929 /* Fill sata_pkt */ 15930 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 15931 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 15932 /* Synchronous mode, no callback */ 15933 spkt->satapkt_comp = NULL; 15934 /* Timeout 30s */ 15935 spkt->satapkt_time = sata_default_pkt_time; 15936 15937 scmd = &spkt->satapkt_cmd; 15938 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 15939 15940 /* 15941 * Allocate buffer for SMART READ LOG EXTENDED command 15942 */ 15943 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 15944 sizeof (struct read_log_ext_directory)); 15945 if (scmd->satacmd_bp == NULL) { 15946 sata_pkt_free(spx); 15947 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15948 SATA_LOG_D((sata_hba_inst, CE_WARN, 15949 "sata_read_log_ext_directory: " 15950 "cannot allocate buffer")); 15951 return (-1); 15952 } 15953 15954 /* Build READ LOG EXT w/ log directory cmd in the sata_pkt */ 15955 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 15956 scmd->satacmd_sec_count_lsb = 1; /* One sector of directory */ 15957 scmd->satacmd_sec_count_msb = 0; /* One sector of directory */ 15958 scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY; 15959 scmd->satacmd_lba_low_msb = 0; 15960 scmd->satacmd_lba_mid_lsb = 0; 15961 scmd->satacmd_lba_mid_msb = 0; 15962 scmd->satacmd_device_reg = 0; /* Always device 0 */ 15963 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 15964 15965 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 15966 sdinfo->satadrv_addr.cport))); 15967 15968 /* Send pkt to SATA HBA driver */ 15969 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 15970 SATA_TRAN_ACCEPTED || 15971 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 15972 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15973 sdinfo->satadrv_addr.cport))); 15974 /* 15975 * Whoops, no SMART selftest log info available 15976 */ 15977 rval = -1; 15978 goto fail; 15979 } else { 15980 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 15981 sdinfo->satadrv_addr.cport))); 15982 if (spx->txlt_buf_dma_handle != NULL) { 15983 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 15984 DDI_DMA_SYNC_FORKERNEL); 15985 ASSERT(rval == DDI_SUCCESS); 15986 } 15987 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir, 15988 sizeof (struct read_log_ext_directory)); 15989 rval = 0; 15990 } 15991 15992 fail: 15993 /* Free allocated resources */ 15994 sata_free_local_buffer(spx); 15995 sata_pkt_free(spx); 15996 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 15997 15998 return (rval); 15999 } 16000 16001 /* 16002 * Set up error retrieval sata command for NCQ command error data 16003 * recovery. 16004 * 16005 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up, 16006 * returns SATA_FAILURE otherwise. 16007 */ 16008 static int 16009 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo) 16010 { 16011 #ifndef __lock_lint 16012 _NOTE(ARGUNUSED(sdinfo)) 16013 #endif 16014 16015 sata_pkt_t *spkt = spx->txlt_sata_pkt; 16016 sata_cmd_t *scmd; 16017 struct buf *bp; 16018 16019 /* Operation modes are up to the caller */ 16020 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 16021 16022 /* Synchronous mode, no callback - may be changed by the caller */ 16023 spkt->satapkt_comp = NULL; 16024 spkt->satapkt_time = sata_default_pkt_time; 16025 16026 scmd = &spkt->satapkt_cmd; 16027 bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t)); 16028 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 16029 16030 /* 16031 * Allocate dma_able buffer error data. 16032 * Buffer allocation will take care of buffer alignment and other DMA 16033 * attributes. 16034 */ 16035 bp = sata_alloc_local_buffer(spx, 16036 sizeof (struct sata_ncq_error_recovery_page)); 16037 if (bp == NULL) 16038 return (SATA_FAILURE); 16039 16040 bp_mapin(bp); /* make data buffer accessible */ 16041 scmd->satacmd_bp = bp; 16042 16043 /* 16044 * Set-up pointer to the buffer handle, so HBA can sync buffer 16045 * before accessing it. Handle is in usual place in translate struct. 16046 */ 16047 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle; 16048 16049 ASSERT(scmd->satacmd_num_dma_cookies != 0); 16050 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 16051 16052 return (SATA_SUCCESS); 16053 } 16054 16055 /* 16056 * sata_xlate_errors() is used to translate (S)ATA error 16057 * information to SCSI information returned in the SCSI 16058 * packet. 16059 */ 16060 static void 16061 sata_xlate_errors(sata_pkt_txlate_t *spx) 16062 { 16063 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 16064 struct scsi_extended_sense *sense; 16065 16066 scsipkt->pkt_reason = CMD_INCOMPLETE; 16067 *scsipkt->pkt_scbp = STATUS_CHECK; 16068 sense = sata_arq_sense(spx); 16069 16070 switch (spx->txlt_sata_pkt->satapkt_reason) { 16071 case SATA_PKT_PORT_ERROR: 16072 /* 16073 * We have no device data. Assume no data transfered. 16074 */ 16075 sense->es_key = KEY_HARDWARE_ERROR; 16076 break; 16077 16078 case SATA_PKT_DEV_ERROR: 16079 if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 16080 SATA_STATUS_ERR) { 16081 /* 16082 * determine dev error reason from error 16083 * reg content 16084 */ 16085 sata_decode_device_error(spx, sense); 16086 break; 16087 } 16088 /* No extended sense key - no info available */ 16089 break; 16090 16091 case SATA_PKT_TIMEOUT: 16092 scsipkt->pkt_reason = CMD_TIMEOUT; 16093 scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET; 16094 /* No extended sense key */ 16095 break; 16096 16097 case SATA_PKT_ABORTED: 16098 scsipkt->pkt_reason = CMD_ABORTED; 16099 scsipkt->pkt_statistics |= STAT_ABORTED; 16100 /* No extended sense key */ 16101 break; 16102 16103 case SATA_PKT_RESET: 16104 /* 16105 * pkt aborted either by an explicit reset request from 16106 * a host, or due to error recovery 16107 */ 16108 scsipkt->pkt_reason = CMD_RESET; 16109 scsipkt->pkt_statistics |= STAT_DEV_RESET; 16110 break; 16111 16112 default: 16113 scsipkt->pkt_reason = CMD_TRAN_ERR; 16114 break; 16115 } 16116 } 16117 16118 16119 16120 16121 /* 16122 * Log sata message 16123 * dev pathname msg line preceeds the logged message. 16124 */ 16125 16126 static void 16127 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...) 16128 { 16129 char pathname[128]; 16130 dev_info_t *dip = NULL; 16131 va_list ap; 16132 16133 mutex_enter(&sata_log_mutex); 16134 16135 va_start(ap, fmt); 16136 (void) vsprintf(sata_log_buf, fmt, ap); 16137 va_end(ap); 16138 16139 if (sata_hba_inst != NULL) { 16140 dip = SATA_DIP(sata_hba_inst); 16141 (void) ddi_pathname(dip, pathname); 16142 } else { 16143 pathname[0] = 0; 16144 } 16145 if (level == CE_CONT) { 16146 if (sata_debug_flags == 0) 16147 cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf); 16148 else 16149 cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf); 16150 } else { 16151 if (level != CE_NOTE) { 16152 cmn_err(level, "%s:\n %s", pathname, sata_log_buf); 16153 } else if (sata_msg) { 16154 cmn_err(level, "%s:\n %s", pathname, 16155 sata_log_buf); 16156 } 16157 } 16158 16159 /* sata trace debug */ 16160 sata_trace_debug(dip, sata_log_buf); 16161 16162 mutex_exit(&sata_log_mutex); 16163 } 16164 16165 16166 /* ******** Asynchronous HBA events handling & hotplugging support ******** */ 16167 16168 /* 16169 * Start or terminate the thread, depending on flag arg and current state 16170 */ 16171 static void 16172 sata_event_thread_control(int startstop) 16173 { 16174 static int sata_event_thread_terminating = 0; 16175 static int sata_event_thread_starting = 0; 16176 int i; 16177 16178 mutex_enter(&sata_event_mutex); 16179 16180 if (startstop == 0 && (sata_event_thread_starting == 1 || 16181 sata_event_thread_terminating == 1)) { 16182 mutex_exit(&sata_event_mutex); 16183 return; 16184 } 16185 if (startstop == 1 && sata_event_thread_starting == 1) { 16186 mutex_exit(&sata_event_mutex); 16187 return; 16188 } 16189 if (startstop == 1 && sata_event_thread_terminating == 1) { 16190 sata_event_thread_starting = 1; 16191 /* wait til terminate operation completes */ 16192 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 16193 while (sata_event_thread_terminating == 1) { 16194 if (i-- <= 0) { 16195 sata_event_thread_starting = 0; 16196 mutex_exit(&sata_event_mutex); 16197 #ifdef SATA_DEBUG 16198 cmn_err(CE_WARN, "sata_event_thread_control: " 16199 "timeout waiting for thread to terminate"); 16200 #endif 16201 return; 16202 } 16203 mutex_exit(&sata_event_mutex); 16204 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 16205 mutex_enter(&sata_event_mutex); 16206 } 16207 } 16208 if (startstop == 1) { 16209 if (sata_event_thread == NULL) { 16210 sata_event_thread = thread_create(NULL, 0, 16211 (void (*)())sata_event_daemon, 16212 &sata_hba_list, 0, &p0, TS_RUN, minclsyspri); 16213 } 16214 sata_event_thread_starting = 0; 16215 mutex_exit(&sata_event_mutex); 16216 return; 16217 } 16218 16219 /* 16220 * If we got here, thread may need to be terminated 16221 */ 16222 if (sata_event_thread != NULL) { 16223 int i; 16224 /* Signal event thread to go away */ 16225 sata_event_thread_terminating = 1; 16226 sata_event_thread_terminate = 1; 16227 cv_signal(&sata_event_cv); 16228 /* 16229 * Wait til daemon terminates. 16230 */ 16231 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 16232 while (sata_event_thread_terminate == 1) { 16233 mutex_exit(&sata_event_mutex); 16234 if (i-- <= 0) { 16235 /* Daemon did not go away !!! */ 16236 #ifdef SATA_DEBUG 16237 cmn_err(CE_WARN, "sata_event_thread_control: " 16238 "cannot terminate event daemon thread"); 16239 #endif 16240 mutex_enter(&sata_event_mutex); 16241 break; 16242 } 16243 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 16244 mutex_enter(&sata_event_mutex); 16245 } 16246 sata_event_thread_terminating = 0; 16247 } 16248 ASSERT(sata_event_thread_terminating == 0); 16249 ASSERT(sata_event_thread_starting == 0); 16250 mutex_exit(&sata_event_mutex); 16251 } 16252 16253 16254 /* 16255 * SATA HBA event notification function. 16256 * Events reported by SATA HBA drivers per HBA instance relate to a change in 16257 * a port and/or device state or a controller itself. 16258 * Events for different addresses/addr types cannot be combined. 16259 * A warning message is generated for each event type. 16260 * Events are not processed by this function, so only the 16261 * event flag(s)is set for an affected entity and the event thread is 16262 * waken up. Event daemon thread processes all events. 16263 * 16264 * NOTE: Since more than one event may be reported at the same time, one 16265 * cannot determine a sequence of events when opposite event are reported, eg. 16266 * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing 16267 * is taking precedence over reported events, i.e. may cause ignoring some 16268 * events. 16269 */ 16270 #define SATA_EVENT_MAX_MSG_LENGTH 79 16271 16272 void 16273 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event) 16274 { 16275 sata_hba_inst_t *sata_hba_inst = NULL; 16276 sata_address_t *saddr; 16277 sata_pmult_info_t *pmultinfo; 16278 sata_drive_info_t *sdinfo; 16279 sata_port_stats_t *pstats; 16280 sata_cport_info_t *cportinfo; 16281 sata_pmport_info_t *pmportinfo; 16282 int cport, pmport; 16283 char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1]; 16284 char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1]; 16285 char *lcp; 16286 static char *err_msg_evnt_1 = 16287 "sata_hba_event_notify: invalid port event 0x%x "; 16288 static char *err_msg_evnt_2 = 16289 "sata_hba_event_notify: invalid device event 0x%x "; 16290 int linkevent; 16291 16292 /* 16293 * There is a possibility that an event will be generated on HBA 16294 * that has not completed attachment or is detaching. We still want 16295 * to process events until HBA is detached. 16296 */ 16297 mutex_enter(&sata_mutex); 16298 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 16299 sata_hba_inst = sata_hba_inst->satahba_next) { 16300 if (SATA_DIP(sata_hba_inst) == dip) 16301 if (sata_hba_inst->satahba_attached == 1) 16302 break; 16303 } 16304 mutex_exit(&sata_mutex); 16305 if (sata_hba_inst == NULL) 16306 /* HBA not attached */ 16307 return; 16308 16309 ASSERT(sata_device != NULL); 16310 16311 /* 16312 * Validate address before - do not proceed with invalid address. 16313 */ 16314 saddr = &sata_device->satadev_addr; 16315 if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst)) 16316 return; 16317 16318 cport = saddr->cport; 16319 pmport = saddr->pmport; 16320 16321 buf1[0] = buf2[0] = '\0'; 16322 16323 /* 16324 * If event relates to port or device, check port state. 16325 * Port has to be initialized, or we cannot accept an event. 16326 */ 16327 if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT | 16328 SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT | SATA_ADDR_PMULT)) != 0) { 16329 mutex_enter(&sata_hba_inst->satahba_mutex); 16330 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 16331 mutex_exit(&sata_hba_inst->satahba_mutex); 16332 if (cportinfo == NULL || cportinfo->cport_state == 0) 16333 return; 16334 } 16335 16336 if ((saddr->qual & (SATA_ADDR_PMULT | SATA_ADDR_PMPORT | 16337 SATA_ADDR_DPMPORT)) != 0) { 16338 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 16339 SATA_LOG_D((sata_hba_inst, CE_WARN, 16340 "sata_hba_event_notify: Non-pmult device (0x%x)" 16341 "is attached to port %d, ignore pmult/pmport " 16342 "event 0x%x", cportinfo->cport_dev_type, 16343 cport, event)); 16344 return; 16345 } 16346 16347 mutex_enter(&cportinfo->cport_mutex); 16348 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 16349 mutex_exit(&cportinfo->cport_mutex); 16350 16351 /* 16352 * The daemon might be processing attachment of port 16353 * multiplier, in that case we should ignore events on its 16354 * sub-devices. 16355 * 16356 * NOTE: Only pmult_state is checked in sata_hba_event_notify. 16357 * The pmport_state is checked by sata daemon. 16358 */ 16359 if (pmultinfo == NULL || 16360 pmultinfo->pmult_state == SATA_STATE_UNKNOWN) { 16361 SATA_LOG_D((sata_hba_inst, CE_WARN, 16362 "sata_hba_event_notify: pmult is not" 16363 "available at port %d:%d, ignore event 0x%x", 16364 cport, pmport, event)); 16365 return; 16366 } 16367 } 16368 16369 if ((saddr->qual & 16370 (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) != 0) { 16371 16372 mutex_enter(&cportinfo->cport_mutex); 16373 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) { 16374 SATA_LOG_D((sata_hba_inst, CE_WARN, 16375 "sata_hba_event_notify: invalid/" 16376 "un-implemented port %d:%d (%d ports), " 16377 "ignore event 0x%x", cport, pmport, 16378 SATA_NUM_PMPORTS(sata_hba_inst, cport), event)); 16379 mutex_exit(&cportinfo->cport_mutex); 16380 return; 16381 } 16382 mutex_exit(&cportinfo->cport_mutex); 16383 16384 mutex_enter(&sata_hba_inst->satahba_mutex); 16385 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 16386 cport, pmport); 16387 mutex_exit(&sata_hba_inst->satahba_mutex); 16388 16389 /* pmport is implemented/valid? */ 16390 if (pmportinfo == NULL) { 16391 SATA_LOG_D((sata_hba_inst, CE_WARN, 16392 "sata_hba_event_notify: invalid/" 16393 "un-implemented port %d:%d, ignore " 16394 "event 0x%x", cport, pmport, event)); 16395 return; 16396 } 16397 } 16398 16399 /* 16400 * Events refer to devices, ports and controllers - each has 16401 * unique address. Events for different addresses cannot be combined. 16402 */ 16403 if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) { 16404 16405 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16406 16407 /* qualify this event(s) */ 16408 if ((event & SATA_EVNT_PORT_EVENTS) == 0) { 16409 /* Invalid event for the device port */ 16410 (void) sprintf(buf2, err_msg_evnt_1, 16411 event & SATA_EVNT_PORT_EVENTS); 16412 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16413 goto event_info; 16414 } 16415 if (saddr->qual == SATA_ADDR_CPORT) { 16416 /* Controller's device port event */ 16417 16418 (SATA_CPORT_INFO(sata_hba_inst, cport))-> 16419 cport_event_flags |= 16420 event & SATA_EVNT_PORT_EVENTS; 16421 pstats = 16422 &(SATA_CPORT_INFO(sata_hba_inst, cport))-> 16423 cport_stats; 16424 } else { 16425 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16426 mutex_enter(&pmportinfo->pmport_mutex); 16427 /* Port multiplier's device port event */ 16428 (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 16429 pmport_event_flags |= 16430 event & SATA_EVNT_PORT_EVENTS; 16431 pstats = 16432 &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 16433 pmport_stats; 16434 mutex_exit(&pmportinfo->pmport_mutex); 16435 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16436 } 16437 16438 /* 16439 * Add to statistics and log the message. We have to do it 16440 * here rather than in the event daemon, because there may be 16441 * multiple events occuring before they are processed. 16442 */ 16443 linkevent = event & 16444 (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED); 16445 if (linkevent) { 16446 if (linkevent == (SATA_EVNT_LINK_LOST | 16447 SATA_EVNT_LINK_ESTABLISHED)) { 16448 /* This is likely event combination */ 16449 (void) strlcat(buf1, "link lost/established, ", 16450 SATA_EVENT_MAX_MSG_LENGTH); 16451 16452 if (pstats->link_lost < 0xffffffffffffffffULL) 16453 pstats->link_lost++; 16454 if (pstats->link_established < 16455 0xffffffffffffffffULL) 16456 pstats->link_established++; 16457 linkevent = 0; 16458 } else if (linkevent & SATA_EVNT_LINK_LOST) { 16459 (void) strlcat(buf1, "link lost, ", 16460 SATA_EVENT_MAX_MSG_LENGTH); 16461 16462 if (pstats->link_lost < 0xffffffffffffffffULL) 16463 pstats->link_lost++; 16464 } else { 16465 (void) strlcat(buf1, "link established, ", 16466 SATA_EVENT_MAX_MSG_LENGTH); 16467 if (pstats->link_established < 16468 0xffffffffffffffffULL) 16469 pstats->link_established++; 16470 } 16471 } 16472 if (event & SATA_EVNT_DEVICE_ATTACHED) { 16473 (void) strlcat(buf1, "device attached, ", 16474 SATA_EVENT_MAX_MSG_LENGTH); 16475 if (pstats->device_attached < 0xffffffffffffffffULL) 16476 pstats->device_attached++; 16477 } 16478 if (event & SATA_EVNT_DEVICE_DETACHED) { 16479 (void) strlcat(buf1, "device detached, ", 16480 SATA_EVENT_MAX_MSG_LENGTH); 16481 if (pstats->device_detached < 0xffffffffffffffffULL) 16482 pstats->device_detached++; 16483 } 16484 if (event & SATA_EVNT_PWR_LEVEL_CHANGED) { 16485 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 16486 "port %d power level changed", cport); 16487 if (pstats->port_pwr_changed < 0xffffffffffffffffULL) 16488 pstats->port_pwr_changed++; 16489 } 16490 16491 if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) { 16492 /* There should be no other events for this address */ 16493 (void) sprintf(buf2, err_msg_evnt_1, 16494 event & ~SATA_EVNT_PORT_EVENTS); 16495 } 16496 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16497 16498 } else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) { 16499 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16500 16501 /* qualify this event */ 16502 if ((event & SATA_EVNT_DEVICE_RESET) == 0) { 16503 /* Invalid event for a device */ 16504 (void) sprintf(buf2, err_msg_evnt_2, 16505 event & SATA_EVNT_DEVICE_RESET); 16506 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16507 goto event_info; 16508 } 16509 /* drive event */ 16510 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 16511 if (sdinfo != NULL) { 16512 if (event & SATA_EVNT_DEVICE_RESET) { 16513 (void) strlcat(buf1, "device reset, ", 16514 SATA_EVENT_MAX_MSG_LENGTH); 16515 if (sdinfo->satadrv_stats.drive_reset < 16516 0xffffffffffffffffULL) 16517 sdinfo->satadrv_stats.drive_reset++; 16518 sdinfo->satadrv_event_flags |= 16519 SATA_EVNT_DEVICE_RESET; 16520 } 16521 } 16522 if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) { 16523 /* Invalid event for a device */ 16524 (void) sprintf(buf2, err_msg_evnt_2, 16525 event & ~SATA_EVNT_DRIVE_EVENTS); 16526 } 16527 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16528 } else if (saddr->qual == SATA_ADDR_PMULT) { 16529 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16530 16531 /* qualify this event */ 16532 if ((event & (SATA_EVNT_DEVICE_RESET | 16533 SATA_EVNT_PMULT_LINK_CHANGED)) == 0) { 16534 /* Invalid event for a port multiplier */ 16535 (void) sprintf(buf2, err_msg_evnt_2, 16536 event & SATA_EVNT_DEVICE_RESET); 16537 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16538 goto event_info; 16539 } 16540 16541 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 16542 16543 if (event & SATA_EVNT_DEVICE_RESET) { 16544 16545 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 16546 "[Reset] port-mult on cport %d", cport); 16547 pmultinfo->pmult_event_flags |= 16548 SATA_EVNT_DEVICE_RESET; 16549 (void) strlcat(buf1, "pmult reset, ", 16550 SATA_EVENT_MAX_MSG_LENGTH); 16551 } 16552 16553 if (event & SATA_EVNT_PMULT_LINK_CHANGED) { 16554 16555 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 16556 "pmult link changed on cport %d", cport); 16557 pmultinfo->pmult_event_flags |= 16558 SATA_EVNT_PMULT_LINK_CHANGED; 16559 (void) strlcat(buf1, "pmult link changed, ", 16560 SATA_EVENT_MAX_MSG_LENGTH); 16561 } 16562 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 16563 16564 } else { 16565 if (saddr->qual != SATA_ADDR_NULL) { 16566 /* Wrong address qualifier */ 16567 SATA_LOG_D((sata_hba_inst, CE_WARN, 16568 "sata_hba_event_notify: invalid address 0x%x", 16569 *(uint32_t *)saddr)); 16570 return; 16571 } 16572 if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 || 16573 (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) { 16574 /* Invalid event for the controller */ 16575 SATA_LOG_D((sata_hba_inst, CE_WARN, 16576 "sata_hba_event_notify: invalid event 0x%x for " 16577 "controller", 16578 event & SATA_EVNT_CONTROLLER_EVENTS)); 16579 return; 16580 } 16581 buf1[0] = '\0'; 16582 /* This may be a frequent and not interesting event */ 16583 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 16584 "controller power level changed\n", NULL); 16585 16586 mutex_enter(&sata_hba_inst->satahba_mutex); 16587 if (sata_hba_inst->satahba_stats.ctrl_pwr_change < 16588 0xffffffffffffffffULL) 16589 sata_hba_inst->satahba_stats.ctrl_pwr_change++; 16590 16591 sata_hba_inst->satahba_event_flags |= 16592 SATA_EVNT_PWR_LEVEL_CHANGED; 16593 mutex_exit(&sata_hba_inst->satahba_mutex); 16594 } 16595 /* 16596 * If we got here, there is something to do with this HBA 16597 * instance. 16598 */ 16599 mutex_enter(&sata_hba_inst->satahba_mutex); 16600 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 16601 mutex_exit(&sata_hba_inst->satahba_mutex); 16602 mutex_enter(&sata_mutex); 16603 sata_event_pending |= SATA_EVNT_MAIN; /* global event indicator */ 16604 mutex_exit(&sata_mutex); 16605 16606 /* Tickle event thread */ 16607 mutex_enter(&sata_event_mutex); 16608 if (sata_event_thread_active == 0) 16609 cv_signal(&sata_event_cv); 16610 mutex_exit(&sata_event_mutex); 16611 16612 event_info: 16613 if (buf1[0] != '\0') { 16614 lcp = strrchr(buf1, ','); 16615 if (lcp != NULL) 16616 *lcp = '\0'; 16617 } 16618 if (saddr->qual == SATA_ADDR_CPORT || 16619 saddr->qual == SATA_ADDR_DCPORT) { 16620 if (buf1[0] != '\0') { 16621 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 16622 cport, buf1); 16623 } 16624 if (buf2[0] != '\0') { 16625 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 16626 cport, buf2); 16627 } 16628 } else if (saddr->qual == SATA_ADDR_PMPORT || 16629 saddr->qual == SATA_ADDR_DPMPORT) { 16630 if (buf1[0] != '\0') { 16631 sata_log(sata_hba_inst, CE_NOTE, 16632 "port %d pmport %d: %s\n", cport, pmport, buf1); 16633 } 16634 if (buf2[0] != '\0') { 16635 sata_log(sata_hba_inst, CE_NOTE, 16636 "port %d pmport %d: %s\n", cport, pmport, buf2); 16637 } 16638 } 16639 } 16640 16641 16642 /* 16643 * Event processing thread. 16644 * Arg is a pointer to the sata_hba_list pointer. 16645 * It is not really needed, because sata_hba_list is global and static 16646 */ 16647 static void 16648 sata_event_daemon(void *arg) 16649 { 16650 #ifndef __lock_lint 16651 _NOTE(ARGUNUSED(arg)) 16652 #endif 16653 sata_hba_inst_t *sata_hba_inst; 16654 clock_t lbolt; 16655 16656 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 16657 "SATA event daemon started\n", NULL); 16658 loop: 16659 /* 16660 * Process events here. Walk through all registered HBAs 16661 */ 16662 mutex_enter(&sata_mutex); 16663 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 16664 sata_hba_inst = sata_hba_inst->satahba_next) { 16665 ASSERT(sata_hba_inst != NULL); 16666 mutex_enter(&sata_hba_inst->satahba_mutex); 16667 if (sata_hba_inst->satahba_attached == 0 || 16668 (sata_hba_inst->satahba_event_flags & 16669 SATA_EVNT_SKIP) != 0) { 16670 mutex_exit(&sata_hba_inst->satahba_mutex); 16671 continue; 16672 } 16673 if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) { 16674 sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP; 16675 mutex_exit(&sata_hba_inst->satahba_mutex); 16676 mutex_exit(&sata_mutex); 16677 /* Got the controller with pending event */ 16678 sata_process_controller_events(sata_hba_inst); 16679 /* 16680 * Since global mutex was released, there is a 16681 * possibility that HBA list has changed, so start 16682 * over from the top. Just processed controller 16683 * will be passed-over because of the SKIP flag. 16684 */ 16685 goto loop; 16686 } 16687 mutex_exit(&sata_hba_inst->satahba_mutex); 16688 } 16689 /* Clear SKIP flag in all controllers */ 16690 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 16691 sata_hba_inst = sata_hba_inst->satahba_next) { 16692 mutex_enter(&sata_hba_inst->satahba_mutex); 16693 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP; 16694 mutex_exit(&sata_hba_inst->satahba_mutex); 16695 } 16696 mutex_exit(&sata_mutex); 16697 16698 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 16699 "SATA EVENT DAEMON suspending itself", NULL); 16700 16701 #ifdef SATA_DEBUG 16702 if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) { 16703 sata_log(sata_hba_inst, CE_WARN, 16704 "SATA EVENTS PROCESSING DISABLED\n"); 16705 thread_exit(); /* Daemon will not run again */ 16706 } 16707 #endif 16708 mutex_enter(&sata_event_mutex); 16709 sata_event_thread_active = 0; 16710 mutex_exit(&sata_event_mutex); 16711 /* 16712 * Go to sleep/suspend itself and wake up either because new event or 16713 * wait timeout. Exit if there is a termination request (driver 16714 * unload). 16715 */ 16716 do { 16717 lbolt = ddi_get_lbolt(); 16718 lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME); 16719 mutex_enter(&sata_event_mutex); 16720 (void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt); 16721 16722 if (sata_event_thread_active != 0) { 16723 mutex_exit(&sata_event_mutex); 16724 continue; 16725 } 16726 16727 /* Check if it is time to go away */ 16728 if (sata_event_thread_terminate == 1) { 16729 /* 16730 * It is up to the thread setting above flag to make 16731 * sure that this thread is not killed prematurely. 16732 */ 16733 sata_event_thread_terminate = 0; 16734 sata_event_thread = NULL; 16735 mutex_exit(&sata_event_mutex); 16736 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 16737 "SATA_EVENT_DAEMON_TERMINATING", NULL); 16738 thread_exit(); { _NOTE(NOT_REACHED) } 16739 } 16740 mutex_exit(&sata_event_mutex); 16741 } while (!(sata_event_pending & SATA_EVNT_MAIN)); 16742 16743 mutex_enter(&sata_event_mutex); 16744 sata_event_thread_active = 1; 16745 mutex_exit(&sata_event_mutex); 16746 16747 mutex_enter(&sata_mutex); 16748 sata_event_pending &= ~SATA_EVNT_MAIN; 16749 mutex_exit(&sata_mutex); 16750 16751 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 16752 "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL); 16753 16754 goto loop; 16755 } 16756 16757 /* 16758 * Specific HBA instance event processing. 16759 * 16760 * NOTE: At the moment, device event processing is limited to hard disks 16761 * only. 16762 * Port multiplier is supported now. 16763 */ 16764 static void 16765 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst) 16766 { 16767 int ncport; 16768 uint32_t event_flags; 16769 sata_address_t *saddr; 16770 sata_cport_info_t *cportinfo; 16771 sata_pmult_info_t *pmultinfo; 16772 16773 SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst, 16774 "Processing controller %d event(s)", 16775 ddi_get_instance(SATA_DIP(sata_hba_inst))); 16776 16777 mutex_enter(&sata_hba_inst->satahba_mutex); 16778 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN; 16779 event_flags = sata_hba_inst->satahba_event_flags; 16780 mutex_exit(&sata_hba_inst->satahba_mutex); 16781 /* 16782 * Process controller power change first 16783 * HERE 16784 */ 16785 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) 16786 sata_process_cntrl_pwr_level_change(sata_hba_inst); 16787 16788 /* 16789 * Search through ports/devices to identify affected port/device. 16790 * We may have to process events for more than one port/device. 16791 */ 16792 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 16793 /* 16794 * Not all ports may be processed in attach by the time we 16795 * get an event. Check if port info is initialized. 16796 */ 16797 mutex_enter(&sata_hba_inst->satahba_mutex); 16798 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 16799 mutex_exit(&sata_hba_inst->satahba_mutex); 16800 if (cportinfo == NULL || cportinfo->cport_state == NULL) 16801 continue; 16802 16803 /* We have initialized controller port info */ 16804 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 16805 event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 16806 cport_event_flags; 16807 /* Check if port was locked by IOCTL processing */ 16808 if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) { 16809 /* 16810 * We ignore port events because port is busy 16811 * with AP control processing. Set again 16812 * controller and main event flag, so that 16813 * events may be processed by the next daemon 16814 * run. 16815 */ 16816 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 16817 mutex_enter(&sata_hba_inst->satahba_mutex); 16818 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 16819 mutex_exit(&sata_hba_inst->satahba_mutex); 16820 mutex_enter(&sata_mutex); 16821 sata_event_pending |= SATA_EVNT_MAIN; 16822 mutex_exit(&sata_mutex); 16823 SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst, 16824 "Event processing postponed until " 16825 "AP control processing completes", 16826 NULL); 16827 /* Check other ports */ 16828 continue; 16829 } else { 16830 /* 16831 * Set BSY flag so that AP control would not 16832 * interfere with events processing for 16833 * this port. 16834 */ 16835 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 16836 cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY; 16837 } 16838 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 16839 16840 saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr; 16841 16842 if ((event_flags & 16843 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) { 16844 /* 16845 * Got port event. 16846 * We need some hierarchy of event processing as they 16847 * are affecting each other: 16848 * 1. port failed 16849 * 2. device detached/attached 16850 * 3. link events - link events may trigger device 16851 * detached or device attached events in some 16852 * circumstances. 16853 * 4. port power level changed 16854 */ 16855 if (event_flags & SATA_EVNT_PORT_FAILED) { 16856 sata_process_port_failed_event(sata_hba_inst, 16857 saddr); 16858 } 16859 if (event_flags & SATA_EVNT_DEVICE_DETACHED) { 16860 sata_process_device_detached(sata_hba_inst, 16861 saddr); 16862 } 16863 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) { 16864 sata_process_device_attached(sata_hba_inst, 16865 saddr); 16866 } 16867 if (event_flags & 16868 (SATA_EVNT_LINK_ESTABLISHED | 16869 SATA_EVNT_LINK_LOST)) { 16870 sata_process_port_link_events(sata_hba_inst, 16871 saddr); 16872 } 16873 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) { 16874 sata_process_port_pwr_change(sata_hba_inst, 16875 saddr); 16876 } 16877 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) { 16878 sata_process_target_node_cleanup( 16879 sata_hba_inst, saddr); 16880 } 16881 if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) { 16882 sata_process_device_autoonline( 16883 sata_hba_inst, saddr); 16884 } 16885 } 16886 16887 16888 /* 16889 * Scan port multiplier and all its sub-ports event flags. 16890 * The events are marked by 16891 * (1) sata_pmult_info.pmult_event_flags 16892 * (2) sata_pmport_info.pmport_event_flags 16893 */ 16894 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 16895 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 16896 /* 16897 * There should be another extra check: this 16898 * port multiplier still exists? 16899 */ 16900 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, 16901 ncport); 16902 16903 if (pmultinfo != NULL) { 16904 mutex_exit(&(SATA_CPORT_MUTEX( 16905 sata_hba_inst, ncport))); 16906 sata_process_pmult_events( 16907 sata_hba_inst, ncport); 16908 mutex_enter(&(SATA_CPORT_MUTEX( 16909 sata_hba_inst, ncport))); 16910 } else { 16911 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 16912 "Port-multiplier is gone. " 16913 "Ignore all sub-device events " 16914 "at port %d.", ncport); 16915 } 16916 } 16917 16918 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) != 16919 SATA_DTYPE_NONE) && 16920 (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) { 16921 if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)-> 16922 satadrv_event_flags & 16923 (SATA_EVNT_DEVICE_RESET | 16924 SATA_EVNT_INPROC_DEVICE_RESET)) { 16925 /* Have device event */ 16926 sata_process_device_reset(sata_hba_inst, 16927 saddr); 16928 } 16929 } 16930 /* Release PORT_BUSY flag */ 16931 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 16932 cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY; 16933 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 16934 16935 } /* End of loop through the controller SATA ports */ 16936 } 16937 16938 /* 16939 * Specific port multiplier instance event processing. At the moment, device 16940 * event processing is limited to link/attach event only. 16941 * 16942 * NOTE: power management event is not supported yet. 16943 */ 16944 static void 16945 sata_process_pmult_events(sata_hba_inst_t *sata_hba_inst, uint8_t cport) 16946 { 16947 sata_cport_info_t *cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 16948 sata_pmult_info_t *pmultinfo; 16949 sata_pmport_info_t *pmportinfo; 16950 sata_address_t *saddr; 16951 sata_device_t sata_device; 16952 uint32_t event_flags; 16953 int npmport; 16954 int rval; 16955 16956 SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst, 16957 "Processing pmult event(s) on cport %d of controller %d", 16958 cport, ddi_get_instance(SATA_DIP(sata_hba_inst))); 16959 16960 /* First process events on port multiplier */ 16961 mutex_enter(&cportinfo->cport_mutex); 16962 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 16963 event_flags = pmultinfo->pmult_event_flags; 16964 16965 /* 16966 * Reset event (of port multiplier) has higher priority because the 16967 * port multiplier itself might be failed or removed after reset. 16968 */ 16969 if (event_flags & SATA_EVNT_DEVICE_RESET) { 16970 /* 16971 * The status of the sub-links are uncertain, 16972 * so mark all sub-ports as RESET 16973 */ 16974 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 16975 sata_hba_inst, cport); npmport ++) { 16976 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 16977 cport, npmport); 16978 if (pmportinfo == NULL) { 16979 /* That's weird. */ 16980 SATA_LOG_D((sata_hba_inst, CE_WARN, 16981 "sata_hba_event_notify: " 16982 "invalid/un-implemented " 16983 "port %d:%d (%d ports), ", 16984 cport, npmport, SATA_NUM_PMPORTS( 16985 sata_hba_inst, cport))); 16986 continue; 16987 } 16988 16989 mutex_enter(&pmportinfo->pmport_mutex); 16990 16991 /* Mark all pmport to unknow state. */ 16992 pmportinfo->pmport_state = SATA_STATE_UNKNOWN; 16993 /* Mark all pmports with link events. */ 16994 pmportinfo->pmport_event_flags = 16995 (SATA_EVNT_LINK_ESTABLISHED|SATA_EVNT_LINK_LOST); 16996 mutex_exit(&pmportinfo->pmport_mutex); 16997 } 16998 16999 } else if (event_flags & SATA_EVNT_PMULT_LINK_CHANGED) { 17000 /* 17001 * We need probe the port multiplier to know what has 17002 * happened. 17003 */ 17004 bzero(&sata_device, sizeof (sata_device_t)); 17005 sata_device.satadev_rev = SATA_DEVICE_REV; 17006 sata_device.satadev_addr.cport = cport; 17007 sata_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT; 17008 sata_device.satadev_addr.qual = SATA_ADDR_PMULT; 17009 17010 mutex_exit(&cportinfo->cport_mutex); 17011 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 17012 (SATA_DIP(sata_hba_inst), &sata_device); 17013 mutex_enter(&cportinfo->cport_mutex); 17014 if (rval != SATA_SUCCESS) { 17015 /* Something went wrong? Fail the port */ 17016 cportinfo->cport_state = SATA_PSTATE_FAILED; 17017 mutex_exit(&cportinfo->cport_mutex); 17018 SATA_LOG_D((sata_hba_inst, CE_WARN, 17019 "SATA port %d probing failed", cport)); 17020 17021 /* PMult structure must be released. */ 17022 sata_free_pmult(sata_hba_inst, &sata_device); 17023 return; 17024 } 17025 17026 sata_update_port_info(sata_hba_inst, &sata_device); 17027 17028 /* 17029 * Sanity check - Port is active? Is the link active? 17030 * The device is still a port multiplier? 17031 */ 17032 if ((cportinfo->cport_state & 17033 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 17034 ((cportinfo->cport_scr.sstatus & 17035 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) || 17036 (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) { 17037 mutex_exit(&cportinfo->cport_mutex); 17038 17039 /* PMult structure must be released. */ 17040 sata_free_pmult(sata_hba_inst, &sata_device); 17041 return; 17042 } 17043 17044 /* Probed succeed, set port ready. */ 17045 cportinfo->cport_state |= 17046 SATA_STATE_PROBED | SATA_STATE_READY; 17047 } 17048 17049 /* Release port multiplier event flags. */ 17050 pmultinfo->pmult_event_flags &= 17051 ~(SATA_EVNT_DEVICE_RESET|SATA_EVNT_PMULT_LINK_CHANGED); 17052 mutex_exit(&cportinfo->cport_mutex); 17053 17054 /* 17055 * Check all sub-links. 17056 */ 17057 for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, cport); 17058 npmport ++) { 17059 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport); 17060 mutex_enter(&pmportinfo->pmport_mutex); 17061 event_flags = pmportinfo->pmport_event_flags; 17062 mutex_exit(&pmportinfo->pmport_mutex); 17063 saddr = &pmportinfo->pmport_addr; 17064 17065 if ((event_flags & 17066 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) { 17067 /* 17068 * Got port multiplier port event. 17069 * We need some hierarchy of event processing as they 17070 * are affecting each other: 17071 * 1. device detached/attached 17072 * 2. link events - link events may trigger device 17073 * detached or device attached events in some 17074 * circumstances. 17075 */ 17076 if (event_flags & SATA_EVNT_DEVICE_DETACHED) { 17077 sata_process_pmdevice_detached(sata_hba_inst, 17078 saddr); 17079 } 17080 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) { 17081 sata_process_pmdevice_attached(sata_hba_inst, 17082 saddr); 17083 } 17084 if (event_flags & SATA_EVNT_LINK_ESTABLISHED || 17085 event_flags & SATA_EVNT_LINK_LOST) { 17086 sata_process_pmport_link_events(sata_hba_inst, 17087 saddr); 17088 } 17089 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) { 17090 sata_process_target_node_cleanup( 17091 sata_hba_inst, saddr); 17092 } 17093 } 17094 17095 /* Checking drive event(s). */ 17096 mutex_enter(&pmportinfo->pmport_mutex); 17097 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE && 17098 pmportinfo->pmport_sata_drive != NULL) { 17099 event_flags = pmportinfo->pmport_sata_drive-> 17100 satadrv_event_flags; 17101 if (event_flags & (SATA_EVNT_DEVICE_RESET | 17102 SATA_EVNT_INPROC_DEVICE_RESET)) { 17103 17104 /* Have device event */ 17105 sata_process_pmdevice_reset(sata_hba_inst, 17106 saddr); 17107 } 17108 } 17109 mutex_exit(&pmportinfo->pmport_mutex); 17110 17111 /* Release PORT_BUSY flag */ 17112 mutex_enter(&cportinfo->cport_mutex); 17113 cportinfo->cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY; 17114 mutex_exit(&cportinfo->cport_mutex); 17115 } 17116 17117 SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst, 17118 "[DONE] pmult event(s) on cport %d of controller %d", 17119 cport, ddi_get_instance(SATA_DIP(sata_hba_inst))); 17120 } 17121 17122 /* 17123 * Process HBA power level change reported by HBA driver. 17124 * Not implemented at this time - event is ignored. 17125 */ 17126 static void 17127 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst) 17128 { 17129 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17130 "Processing controller power level change", NULL); 17131 17132 /* Ignoring it for now */ 17133 mutex_enter(&sata_hba_inst->satahba_mutex); 17134 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 17135 mutex_exit(&sata_hba_inst->satahba_mutex); 17136 } 17137 17138 /* 17139 * Process port power level change reported by HBA driver. 17140 * Not implemented at this time - event is ignored. 17141 */ 17142 static void 17143 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst, 17144 sata_address_t *saddr) 17145 { 17146 sata_cport_info_t *cportinfo; 17147 17148 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17149 "Processing port power level change", NULL); 17150 17151 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 17152 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17153 /* Reset event flag */ 17154 cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 17155 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17156 } 17157 17158 /* 17159 * Process port failure reported by HBA driver. 17160 * cports support only - no pmports. 17161 */ 17162 static void 17163 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst, 17164 sata_address_t *saddr) 17165 { 17166 sata_cport_info_t *cportinfo; 17167 17168 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 17169 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17170 /* Reset event flag first */ 17171 cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED; 17172 /* If the port is in SHUTDOWN or FAILED state, ignore this event. */ 17173 if ((cportinfo->cport_state & 17174 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) { 17175 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 17176 cport_mutex); 17177 return; 17178 } 17179 /* Fail the port */ 17180 cportinfo->cport_state = SATA_PSTATE_FAILED; 17181 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17182 sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport); 17183 } 17184 17185 /* 17186 * Device Reset Event processing. 17187 * The seqeunce is managed by 3 stage flags: 17188 * - reset event reported, 17189 * - reset event being processed, 17190 * - request to clear device reset state. 17191 * 17192 * NOTE: This function has to be entered with cport mutex held. It exits with 17193 * mutex held as well, but can release mutex during the processing. 17194 */ 17195 static void 17196 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst, 17197 sata_address_t *saddr) 17198 { 17199 sata_drive_info_t old_sdinfo; /* local copy of the drive info */ 17200 sata_drive_info_t *sdinfo; 17201 sata_cport_info_t *cportinfo; 17202 sata_device_t sata_device; 17203 int rval_probe, rval_set; 17204 17205 /* We only care about host sata cport for now */ 17206 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 17207 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 17208 /* 17209 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED 17210 * state, ignore reset event. 17211 */ 17212 if (((cportinfo->cport_state & 17213 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) || 17214 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 17215 sdinfo->satadrv_event_flags &= 17216 ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET); 17217 return; 17218 } 17219 17220 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) == 17221 SATA_DTYPE_PMULT)) { 17222 /* 17223 * Should not happened: this is already handled in 17224 * sata_hba_event_notify() 17225 */ 17226 mutex_exit(&cportinfo->cport_mutex); 17227 goto done; 17228 } 17229 17230 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) & 17231 SATA_VALID_DEV_TYPE) == 0) { 17232 /* 17233 * This should not happen - coding error. 17234 * But we can recover, so do not panic, just clean up 17235 * and if in debug mode, log the message. 17236 */ 17237 #ifdef SATA_DEBUG 17238 sata_log(sata_hba_inst, CE_WARN, 17239 "sata_process_device_reset: " 17240 "Invalid device type with sdinfo!", NULL); 17241 #endif 17242 sdinfo->satadrv_event_flags = 0; 17243 return; 17244 } 17245 17246 #ifdef SATA_DEBUG 17247 if ((sdinfo->satadrv_event_flags & 17248 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) { 17249 /* Nothing to do */ 17250 /* Something is weird - why we are processing dev reset? */ 17251 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17252 "No device reset event!!!!", NULL); 17253 17254 return; 17255 } 17256 if ((sdinfo->satadrv_event_flags & 17257 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 17258 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 17259 /* Something is weird - new device reset event */ 17260 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17261 "Overlapping device reset events!", NULL); 17262 } 17263 #endif 17264 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17265 "Processing port %d device reset", saddr->cport); 17266 17267 /* Clear event flag */ 17268 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET; 17269 17270 /* It seems that we always need to check the port state first */ 17271 sata_device.satadev_rev = SATA_DEVICE_REV; 17272 sata_device.satadev_addr = *saddr; 17273 /* 17274 * We have to exit mutex, because the HBA probe port function may 17275 * block on its own mutex. 17276 */ 17277 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17278 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 17279 (SATA_DIP(sata_hba_inst), &sata_device); 17280 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17281 sata_update_port_info(sata_hba_inst, &sata_device); 17282 if (rval_probe != SATA_SUCCESS) { 17283 /* Something went wrong? Fail the port */ 17284 cportinfo->cport_state = SATA_PSTATE_FAILED; 17285 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 17286 if (sdinfo != NULL) 17287 sdinfo->satadrv_event_flags = 0; 17288 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 17289 cport_mutex); 17290 SATA_LOG_D((sata_hba_inst, CE_WARN, 17291 "SATA port %d probing failed", 17292 saddr->cport)); 17293 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 17294 saddr->cport)->cport_mutex); 17295 return; 17296 } 17297 if ((sata_device.satadev_scr.sstatus & 17298 SATA_PORT_DEVLINK_UP_MASK) != 17299 SATA_PORT_DEVLINK_UP || 17300 sata_device.satadev_type == SATA_DTYPE_NONE) { 17301 /* 17302 * No device to process, anymore. Some other event processing 17303 * would or have already performed port info cleanup. 17304 * To be safe (HBA may need it), request clearing device 17305 * reset condition. 17306 */ 17307 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 17308 if (sdinfo != NULL) { 17309 sdinfo->satadrv_event_flags &= 17310 ~SATA_EVNT_INPROC_DEVICE_RESET; 17311 sdinfo->satadrv_event_flags |= 17312 SATA_EVNT_CLEAR_DEVICE_RESET; 17313 } 17314 return; 17315 } 17316 17317 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 17318 if (sdinfo == NULL) { 17319 return; 17320 } 17321 if ((sdinfo->satadrv_event_flags & 17322 SATA_EVNT_INPROC_DEVICE_RESET) == 0) { 17323 /* 17324 * Start tracking time for device feature restoration and 17325 * identification. Save current time (lbolt value). 17326 */ 17327 sdinfo->satadrv_reset_time = ddi_get_lbolt(); 17328 } 17329 /* Mark device reset processing as active */ 17330 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET; 17331 17332 old_sdinfo = *sdinfo; /* local copy of the drive info */ 17333 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17334 17335 rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1); 17336 17337 if (rval_set != SATA_SUCCESS) { 17338 /* 17339 * Restoring drive setting failed. 17340 * Probe the port first, to check if the port state has changed 17341 */ 17342 sata_device.satadev_rev = SATA_DEVICE_REV; 17343 sata_device.satadev_addr = *saddr; 17344 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 17345 /* probe port */ 17346 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 17347 (SATA_DIP(sata_hba_inst), &sata_device); 17348 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 17349 cport_mutex); 17350 if (rval_probe == SATA_SUCCESS && 17351 (sata_device.satadev_state & 17352 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 && 17353 (sata_device.satadev_scr.sstatus & 17354 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP && 17355 sata_device.satadev_type != SATA_DTYPE_NONE) { 17356 /* 17357 * We may retry this a bit later - in-process reset 17358 * condition should be already set. 17359 * Track retry time for device identification. 17360 */ 17361 if ((cportinfo->cport_dev_type & 17362 SATA_VALID_DEV_TYPE) != 0 && 17363 SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL && 17364 sdinfo->satadrv_reset_time != 0) { 17365 clock_t cur_time = ddi_get_lbolt(); 17366 /* 17367 * If the retry time limit was not 17368 * exceeded, retry. 17369 */ 17370 if ((cur_time - sdinfo->satadrv_reset_time) < 17371 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 17372 mutex_enter( 17373 &sata_hba_inst->satahba_mutex); 17374 sata_hba_inst->satahba_event_flags |= 17375 SATA_EVNT_MAIN; 17376 mutex_exit( 17377 &sata_hba_inst->satahba_mutex); 17378 mutex_enter(&sata_mutex); 17379 sata_event_pending |= SATA_EVNT_MAIN; 17380 mutex_exit(&sata_mutex); 17381 return; 17382 } 17383 if (rval_set == SATA_RETRY) { 17384 /* 17385 * Setting drive features failed, but 17386 * the drive is still accessible, 17387 * so emit a warning message before 17388 * return. 17389 */ 17390 mutex_exit(&SATA_CPORT_INFO( 17391 sata_hba_inst, 17392 saddr->cport)->cport_mutex); 17393 goto done; 17394 } 17395 } 17396 /* Fail the drive */ 17397 sdinfo->satadrv_state = SATA_DSTATE_FAILED; 17398 17399 sata_log(sata_hba_inst, CE_WARN, 17400 "SATA device at port %d - device failed", 17401 saddr->cport); 17402 } 17403 /* 17404 * No point of retrying - device failed or some other event 17405 * processing or already did or will do port info cleanup. 17406 * To be safe (HBA may need it), 17407 * request clearing device reset condition. 17408 */ 17409 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 17410 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET; 17411 sdinfo->satadrv_reset_time = 0; 17412 return; 17413 } 17414 done: 17415 /* 17416 * If setting of drive features failed, but the drive is still 17417 * accessible, emit a warning message. 17418 */ 17419 if (rval_set == SATA_RETRY) { 17420 sata_log(sata_hba_inst, CE_WARN, 17421 "SATA device at port %d - desired setting could not be " 17422 "restored after reset. Device may not operate as expected.", 17423 saddr->cport); 17424 } 17425 /* 17426 * Raise the flag indicating that the next sata command could 17427 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device 17428 * reset is reported. 17429 */ 17430 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17431 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 17432 sdinfo->satadrv_reset_time = 0; 17433 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) { 17434 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 17435 sdinfo->satadrv_event_flags &= 17436 ~SATA_EVNT_INPROC_DEVICE_RESET; 17437 sdinfo->satadrv_event_flags |= 17438 SATA_EVNT_CLEAR_DEVICE_RESET; 17439 } 17440 } 17441 } 17442 17443 17444 /* 17445 * Port Multiplier Port Device Reset Event processing. 17446 * 17447 * NOTE: This function has to be entered with pmport mutex held. It exits with 17448 * mutex held as well, but can release mutex during the processing. 17449 */ 17450 static void 17451 sata_process_pmdevice_reset(sata_hba_inst_t *sata_hba_inst, 17452 sata_address_t *saddr) 17453 { 17454 sata_drive_info_t old_sdinfo; /* local copy of the drive info */ 17455 sata_drive_info_t *sdinfo = NULL; 17456 sata_cport_info_t *cportinfo = NULL; 17457 sata_pmport_info_t *pmportinfo = NULL; 17458 sata_pmult_info_t *pminfo = NULL; 17459 sata_device_t sata_device; 17460 uint8_t cport = saddr->cport; 17461 uint8_t pmport = saddr->pmport; 17462 int rval; 17463 17464 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17465 "Processing drive reset at port %d:%d", cport, pmport); 17466 17467 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 17468 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 17469 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport); 17470 17471 /* 17472 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED 17473 * state, ignore reset event. 17474 */ 17475 if (((cportinfo->cport_state & 17476 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) || 17477 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 17478 sdinfo->satadrv_event_flags &= 17479 ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET); 17480 return; 17481 } 17482 17483 if ((pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) == 0) { 17484 /* 17485 * This should not happen - coding error. 17486 * But we can recover, so do not panic, just clean up 17487 * and if in debug mode, log the message. 17488 */ 17489 #ifdef SATA_DEBUG 17490 sata_log(sata_hba_inst, CE_WARN, 17491 "sata_process_pmdevice_reset: " 17492 "Invalid device type with sdinfo!", NULL); 17493 #endif 17494 sdinfo->satadrv_event_flags = 0; 17495 return; 17496 } 17497 17498 #ifdef SATA_DEBUG 17499 if ((sdinfo->satadrv_event_flags & 17500 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) { 17501 /* Nothing to do */ 17502 /* Something is weird - why we are processing dev reset? */ 17503 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17504 "No device reset event!!!!", NULL); 17505 17506 return; 17507 } 17508 if ((sdinfo->satadrv_event_flags & 17509 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 17510 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 17511 /* Something is weird - new device reset event */ 17512 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17513 "Overlapping device reset events!", NULL); 17514 } 17515 #endif 17516 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17517 "Processing port %d:%d device reset", cport, pmport); 17518 17519 /* Clear event flag */ 17520 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET; 17521 17522 /* It seems that we always need to check the port state first */ 17523 sata_device.satadev_rev = SATA_DEVICE_REV; 17524 sata_device.satadev_addr = *saddr; 17525 /* 17526 * We have to exit mutex, because the HBA probe port function may 17527 * block on its own mutex. 17528 */ 17529 mutex_exit(&pmportinfo->pmport_mutex); 17530 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 17531 (SATA_DIP(sata_hba_inst), &sata_device); 17532 mutex_enter(&pmportinfo->pmport_mutex); 17533 17534 sata_update_pmport_info(sata_hba_inst, &sata_device); 17535 if (rval != SATA_SUCCESS) { 17536 /* Something went wrong? Fail the port */ 17537 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 17538 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport, 17539 saddr->pmport); 17540 if (sdinfo != NULL) 17541 sdinfo->satadrv_event_flags = 0; 17542 mutex_exit(&pmportinfo->pmport_mutex); 17543 SATA_LOG_D((sata_hba_inst, CE_WARN, 17544 "SATA port %d:%d probing failed", 17545 saddr->cport, saddr->pmport)); 17546 mutex_enter(&pmportinfo->pmport_mutex); 17547 return; 17548 } 17549 if ((sata_device.satadev_scr.sstatus & 17550 SATA_PORT_DEVLINK_UP_MASK) != 17551 SATA_PORT_DEVLINK_UP || 17552 sata_device.satadev_type == SATA_DTYPE_NONE) { 17553 /* 17554 * No device to process, anymore. Some other event processing 17555 * would or have already performed port info cleanup. 17556 * To be safe (HBA may need it), request clearing device 17557 * reset condition. 17558 */ 17559 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport, 17560 saddr->pmport); 17561 if (sdinfo != NULL) { 17562 sdinfo->satadrv_event_flags &= 17563 ~SATA_EVNT_INPROC_DEVICE_RESET; 17564 /* must clear flags on cport */ 17565 pminfo = SATA_PMULT_INFO(sata_hba_inst, 17566 saddr->cport); 17567 pminfo->pmult_event_flags |= 17568 SATA_EVNT_CLEAR_DEVICE_RESET; 17569 } 17570 return; 17571 } 17572 17573 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport, 17574 saddr->pmport); 17575 if (sdinfo == NULL) { 17576 return; 17577 } 17578 if ((sdinfo->satadrv_event_flags & 17579 SATA_EVNT_INPROC_DEVICE_RESET) == 0) { 17580 /* 17581 * Start tracking time for device feature restoration and 17582 * identification. Save current time (lbolt value). 17583 */ 17584 sdinfo->satadrv_reset_time = ddi_get_lbolt(); 17585 } 17586 /* Mark device reset processing as active */ 17587 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET; 17588 17589 old_sdinfo = *sdinfo; /* local copy of the drive info */ 17590 mutex_exit(&pmportinfo->pmport_mutex); 17591 17592 if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) == 17593 SATA_FAILURE) { 17594 /* 17595 * Restoring drive setting failed. 17596 * Probe the port first, to check if the port state has changed 17597 */ 17598 sata_device.satadev_rev = SATA_DEVICE_REV; 17599 sata_device.satadev_addr = *saddr; 17600 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT; 17601 17602 /* probe port */ 17603 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 17604 (SATA_DIP(sata_hba_inst), &sata_device); 17605 mutex_enter(&pmportinfo->pmport_mutex); 17606 if (rval == SATA_SUCCESS && 17607 (sata_device.satadev_state & 17608 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 && 17609 (sata_device.satadev_scr.sstatus & 17610 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP && 17611 sata_device.satadev_type != SATA_DTYPE_NONE) { 17612 /* 17613 * We may retry this a bit later - in-process reset 17614 * condition should be already set. 17615 * Track retry time for device identification. 17616 */ 17617 if ((pmportinfo->pmport_dev_type & 17618 SATA_VALID_DEV_TYPE) != 0 && 17619 SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL && 17620 sdinfo->satadrv_reset_time != 0) { 17621 clock_t cur_time = ddi_get_lbolt(); 17622 /* 17623 * If the retry time limit was not 17624 * exceeded, retry. 17625 */ 17626 if ((cur_time - sdinfo->satadrv_reset_time) < 17627 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 17628 mutex_enter( 17629 &sata_hba_inst->satahba_mutex); 17630 sata_hba_inst->satahba_event_flags |= 17631 SATA_EVNT_MAIN; 17632 mutex_exit( 17633 &sata_hba_inst->satahba_mutex); 17634 mutex_enter(&sata_mutex); 17635 sata_event_pending |= SATA_EVNT_MAIN; 17636 mutex_exit(&sata_mutex); 17637 return; 17638 } 17639 } 17640 /* Fail the drive */ 17641 sdinfo->satadrv_state = SATA_DSTATE_FAILED; 17642 17643 sata_log(sata_hba_inst, CE_WARN, 17644 "SATA device at port %d:%d - device failed", 17645 saddr->cport, saddr->pmport); 17646 } else { 17647 /* 17648 * No point of retrying - some other event processing 17649 * would or already did port info cleanup. 17650 * To be safe (HBA may need it), 17651 * request clearing device reset condition. 17652 */ 17653 sdinfo->satadrv_event_flags |= 17654 SATA_EVNT_CLEAR_DEVICE_RESET; 17655 } 17656 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET; 17657 sdinfo->satadrv_reset_time = 0; 17658 return; 17659 } 17660 /* 17661 * Raise the flag indicating that the next sata command could 17662 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device 17663 * reset is reported. 17664 */ 17665 mutex_enter(&pmportinfo->pmport_mutex); 17666 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 17667 sdinfo->satadrv_reset_time = 0; 17668 if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) { 17669 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 17670 sdinfo->satadrv_event_flags &= 17671 ~SATA_EVNT_INPROC_DEVICE_RESET; 17672 /* must clear flags on cport */ 17673 pminfo = SATA_PMULT_INFO(sata_hba_inst, 17674 saddr->cport); 17675 pminfo->pmult_event_flags |= 17676 SATA_EVNT_CLEAR_DEVICE_RESET; 17677 } 17678 } 17679 } 17680 17681 /* 17682 * Port Link Events processing. 17683 * Every link established event may involve device reset (due to 17684 * COMRESET signal, equivalent of the hard reset) so arbitrarily 17685 * set device reset event for an attached device (if any). 17686 * If the port is in SHUTDOWN or FAILED state, ignore link events. 17687 * 17688 * The link established event processing varies, depending on the state 17689 * of the target node, HBA hotplugging capabilities, state of the port. 17690 * If the link is not active, the link established event is ignored. 17691 * If HBA cannot detect device attachment and there is no target node, 17692 * the link established event triggers device attach event processing. 17693 * Else, link established event triggers device reset event processing. 17694 * 17695 * The link lost event processing varies, depending on a HBA hotplugging 17696 * capability and the state of the port (link active or not active). 17697 * If the link is active, the lost link event is ignored. 17698 * If HBA cannot detect device removal, the lost link event triggers 17699 * device detached event processing after link lost timeout. 17700 * Else, the event is ignored. 17701 * 17702 * NOTE: Port multiplier ports events are handled by 17703 * sata_process_pmport_link_events(); 17704 */ 17705 static void 17706 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst, 17707 sata_address_t *saddr) 17708 { 17709 sata_device_t sata_device; 17710 sata_cport_info_t *cportinfo; 17711 sata_drive_info_t *sdinfo; 17712 uint32_t event_flags; 17713 int rval; 17714 17715 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17716 "Processing port %d link event(s)", saddr->cport); 17717 17718 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 17719 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17720 event_flags = cportinfo->cport_event_flags; 17721 17722 /* Reset event flags first */ 17723 cportinfo->cport_event_flags &= 17724 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST); 17725 17726 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */ 17727 if ((cportinfo->cport_state & 17728 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 17729 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 17730 cport_mutex); 17731 return; 17732 } 17733 17734 /* 17735 * For the sanity sake get current port state. 17736 * Set device address only. Other sata_device fields should be 17737 * set by HBA driver. 17738 */ 17739 sata_device.satadev_rev = SATA_DEVICE_REV; 17740 sata_device.satadev_addr = *saddr; 17741 /* 17742 * We have to exit mutex, because the HBA probe port function may 17743 * block on its own mutex. 17744 */ 17745 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17746 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 17747 (SATA_DIP(sata_hba_inst), &sata_device); 17748 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17749 sata_update_port_info(sata_hba_inst, &sata_device); 17750 if (rval != SATA_SUCCESS) { 17751 /* Something went wrong? Fail the port */ 17752 cportinfo->cport_state = SATA_PSTATE_FAILED; 17753 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 17754 cport_mutex); 17755 SATA_LOG_D((sata_hba_inst, CE_WARN, 17756 "SATA port %d probing failed", 17757 saddr->cport)); 17758 /* 17759 * We may want to release device info structure, but 17760 * it is not necessary. 17761 */ 17762 return; 17763 } else { 17764 /* port probed successfully */ 17765 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 17766 } 17767 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) { 17768 17769 if ((sata_device.satadev_scr.sstatus & 17770 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) { 17771 /* Ignore event */ 17772 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17773 "Ignoring port %d link established event - " 17774 "link down", 17775 saddr->cport); 17776 goto linklost; 17777 } 17778 17779 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17780 "Processing port %d link established event", 17781 saddr->cport); 17782 17783 /* 17784 * For the sanity sake check if a device is attached - check 17785 * return state of a port probing. 17786 */ 17787 if (sata_device.satadev_type != SATA_DTYPE_NONE) { 17788 /* 17789 * HBA port probe indicated that there is a device 17790 * attached. Check if the framework had device info 17791 * structure attached for this device. 17792 */ 17793 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 17794 ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) != 17795 NULL); 17796 17797 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 17798 if ((sdinfo->satadrv_type & 17799 SATA_VALID_DEV_TYPE) != 0) { 17800 /* 17801 * Dev info structure is present. 17802 * If dev_type is set to known type in 17803 * the framework's drive info struct 17804 * then the device existed before and 17805 * the link was probably lost 17806 * momentarily - in such case 17807 * we may want to check device 17808 * identity. 17809 * Identity check is not supported now. 17810 * 17811 * Link established event 17812 * triggers device reset event. 17813 */ 17814 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 17815 satadrv_event_flags |= 17816 SATA_EVNT_DEVICE_RESET; 17817 } 17818 } else if (cportinfo->cport_dev_type == 17819 SATA_DTYPE_NONE) { 17820 /* 17821 * We got new device attached! If HBA does not 17822 * generate device attached events, trigger it 17823 * here. 17824 */ 17825 if (!(SATA_FEATURES(sata_hba_inst) & 17826 SATA_CTLF_HOTPLUG)) { 17827 cportinfo->cport_event_flags |= 17828 SATA_EVNT_DEVICE_ATTACHED; 17829 } 17830 } 17831 /* Reset link lost timeout */ 17832 cportinfo->cport_link_lost_time = 0; 17833 } 17834 } 17835 linklost: 17836 if (event_flags & SATA_EVNT_LINK_LOST) { 17837 if ((sata_device.satadev_scr.sstatus & 17838 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) { 17839 /* Ignore event */ 17840 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17841 "Ignoring port %d link lost event - link is up", 17842 saddr->cport); 17843 goto done; 17844 } 17845 #ifdef SATA_DEBUG 17846 if (cportinfo->cport_link_lost_time == 0) { 17847 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17848 "Processing port %d link lost event", 17849 saddr->cport); 17850 } 17851 #endif 17852 /* 17853 * When HBA cannot generate device attached/detached events, 17854 * we need to track link lost time and eventually generate 17855 * device detach event. 17856 */ 17857 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) { 17858 /* We are tracking link lost time */ 17859 if (cportinfo->cport_link_lost_time == 0) { 17860 /* save current time (lbolt value) */ 17861 cportinfo->cport_link_lost_time = 17862 ddi_get_lbolt(); 17863 /* just keep link lost event */ 17864 cportinfo->cport_event_flags |= 17865 SATA_EVNT_LINK_LOST; 17866 } else { 17867 clock_t cur_time = ddi_get_lbolt(); 17868 if ((cur_time - 17869 cportinfo->cport_link_lost_time) >= 17870 drv_usectohz( 17871 SATA_EVNT_LINK_LOST_TIMEOUT)) { 17872 /* trigger device detach event */ 17873 cportinfo->cport_event_flags |= 17874 SATA_EVNT_DEVICE_DETACHED; 17875 cportinfo->cport_link_lost_time = 0; 17876 SATADBG1(SATA_DBG_EVENTS, 17877 sata_hba_inst, 17878 "Triggering port %d " 17879 "device detached event", 17880 saddr->cport); 17881 } else { 17882 /* keep link lost event */ 17883 cportinfo->cport_event_flags |= 17884 SATA_EVNT_LINK_LOST; 17885 } 17886 } 17887 } 17888 /* 17889 * We could change port state to disable/delay access to 17890 * the attached device until the link is recovered. 17891 */ 17892 } 17893 done: 17894 event_flags = cportinfo->cport_event_flags; 17895 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 17896 if (event_flags != 0) { 17897 mutex_enter(&sata_hba_inst->satahba_mutex); 17898 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 17899 mutex_exit(&sata_hba_inst->satahba_mutex); 17900 mutex_enter(&sata_mutex); 17901 sata_event_pending |= SATA_EVNT_MAIN; 17902 mutex_exit(&sata_mutex); 17903 } 17904 } 17905 17906 /* 17907 * Port Multiplier Port Link Events processing. 17908 */ 17909 static void 17910 sata_process_pmport_link_events(sata_hba_inst_t *sata_hba_inst, 17911 sata_address_t *saddr) 17912 { 17913 sata_device_t sata_device; 17914 sata_pmport_info_t *pmportinfo = NULL; 17915 sata_drive_info_t *sdinfo = NULL; 17916 uint32_t event_flags; 17917 uint8_t cport = saddr->cport; 17918 uint8_t pmport = saddr->pmport; 17919 int rval; 17920 17921 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17922 "Processing port %d:%d link event(s)", 17923 cport, pmport); 17924 17925 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 17926 mutex_enter(&pmportinfo->pmport_mutex); 17927 event_flags = pmportinfo->pmport_event_flags; 17928 17929 /* Reset event flags first */ 17930 pmportinfo->pmport_event_flags &= 17931 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST); 17932 17933 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */ 17934 if ((pmportinfo->pmport_state & 17935 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 17936 mutex_exit(&pmportinfo->pmport_mutex); 17937 return; 17938 } 17939 17940 /* 17941 * For the sanity sake get current port state. 17942 * Set device address only. Other sata_device fields should be 17943 * set by HBA driver. 17944 */ 17945 sata_device.satadev_rev = SATA_DEVICE_REV; 17946 sata_device.satadev_addr = *saddr; 17947 /* 17948 * We have to exit mutex, because the HBA probe port function may 17949 * block on its own mutex. 17950 */ 17951 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 17952 saddr->pmport)); 17953 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 17954 (SATA_DIP(sata_hba_inst), &sata_device); 17955 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 17956 saddr->pmport)); 17957 sata_update_pmport_info(sata_hba_inst, &sata_device); 17958 if (rval != SATA_SUCCESS) { 17959 /* Something went wrong? Fail the port */ 17960 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 17961 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 17962 saddr->pmport)); 17963 SATA_LOG_D((sata_hba_inst, CE_WARN, 17964 "SATA port %d:%d probing failed", 17965 saddr->cport, saddr->pmport)); 17966 /* 17967 * We may want to release device info structure, but 17968 * it is not necessary. 17969 */ 17970 return; 17971 } else { 17972 /* port probed successfully */ 17973 pmportinfo->pmport_state |= 17974 SATA_STATE_PROBED | SATA_STATE_READY; 17975 } 17976 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, 17977 saddr->cport, saddr->pmport)); 17978 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, 17979 saddr->cport, saddr->pmport)); 17980 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) { 17981 17982 if ((sata_device.satadev_scr.sstatus & 17983 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) { 17984 /* Ignore event */ 17985 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17986 "Ignoring port %d:%d link established event - " 17987 "link down", 17988 saddr->cport, saddr->pmport); 17989 goto linklost; 17990 } 17991 17992 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 17993 "Processing port %d:%d link established event", 17994 cport, pmport); 17995 17996 /* 17997 * For the sanity sake check if a device is attached - check 17998 * return state of a port probing. 17999 */ 18000 if (sata_device.satadev_type != SATA_DTYPE_NONE && 18001 sata_device.satadev_type != SATA_DTYPE_PMULT) { 18002 /* 18003 * HBA port probe indicated that there is a device 18004 * attached. Check if the framework had device info 18005 * structure attached for this device. 18006 */ 18007 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 18008 ASSERT(SATA_PMPORTINFO_DRV_INFO(pmportinfo) != 18009 NULL); 18010 18011 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 18012 if ((sdinfo->satadrv_type & 18013 SATA_VALID_DEV_TYPE) != 0) { 18014 /* 18015 * Dev info structure is present. 18016 * If dev_type is set to known type in 18017 * the framework's drive info struct 18018 * then the device existed before and 18019 * the link was probably lost 18020 * momentarily - in such case 18021 * we may want to check device 18022 * identity. 18023 * Identity check is not supported now. 18024 * 18025 * Link established event 18026 * triggers device reset event. 18027 */ 18028 (SATA_PMPORTINFO_DRV_INFO(pmportinfo))-> 18029 satadrv_event_flags |= 18030 SATA_EVNT_DEVICE_RESET; 18031 } 18032 } else if (pmportinfo->pmport_dev_type == 18033 SATA_DTYPE_NONE) { 18034 /* 18035 * We got new device attached! If HBA does not 18036 * generate device attached events, trigger it 18037 * here. 18038 */ 18039 if (!(SATA_FEATURES(sata_hba_inst) & 18040 SATA_CTLF_HOTPLUG)) { 18041 pmportinfo->pmport_event_flags |= 18042 SATA_EVNT_DEVICE_ATTACHED; 18043 } 18044 } 18045 /* Reset link lost timeout */ 18046 pmportinfo->pmport_link_lost_time = 0; 18047 } 18048 } 18049 linklost: 18050 if (event_flags & SATA_EVNT_LINK_LOST) { 18051 #ifdef SATA_DEBUG 18052 if (pmportinfo->pmport_link_lost_time == 0) { 18053 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18054 "Processing port %d:%d link lost event", 18055 saddr->cport, saddr->pmport); 18056 } 18057 #endif 18058 if ((sata_device.satadev_scr.sstatus & 18059 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) { 18060 /* Ignore event */ 18061 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18062 "Ignoring port %d:%d link lost event - link is up", 18063 saddr->cport, saddr->pmport); 18064 goto done; 18065 } 18066 /* 18067 * When HBA cannot generate device attached/detached events, 18068 * we need to track link lost time and eventually generate 18069 * device detach event. 18070 */ 18071 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) { 18072 /* We are tracking link lost time */ 18073 if (pmportinfo->pmport_link_lost_time == 0) { 18074 /* save current time (lbolt value) */ 18075 pmportinfo->pmport_link_lost_time = 18076 ddi_get_lbolt(); 18077 /* just keep link lost event */ 18078 pmportinfo->pmport_event_flags |= 18079 SATA_EVNT_LINK_LOST; 18080 } else { 18081 clock_t cur_time = ddi_get_lbolt(); 18082 if ((cur_time - 18083 pmportinfo->pmport_link_lost_time) >= 18084 drv_usectohz( 18085 SATA_EVNT_LINK_LOST_TIMEOUT)) { 18086 /* trigger device detach event */ 18087 pmportinfo->pmport_event_flags |= 18088 SATA_EVNT_DEVICE_DETACHED; 18089 pmportinfo->pmport_link_lost_time = 0; 18090 SATADBG2(SATA_DBG_EVENTS, 18091 sata_hba_inst, 18092 "Triggering port %d:%d " 18093 "device detached event", 18094 saddr->cport, saddr->pmport); 18095 } else { 18096 /* keep link lost event */ 18097 pmportinfo->pmport_event_flags |= 18098 SATA_EVNT_LINK_LOST; 18099 } 18100 } 18101 } 18102 /* 18103 * We could change port state to disable/delay access to 18104 * the attached device until the link is recovered. 18105 */ 18106 } 18107 done: 18108 event_flags = pmportinfo->pmport_event_flags; 18109 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 18110 saddr->pmport)); 18111 if (event_flags != 0) { 18112 mutex_enter(&sata_hba_inst->satahba_mutex); 18113 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 18114 mutex_exit(&sata_hba_inst->satahba_mutex); 18115 mutex_enter(&sata_mutex); 18116 sata_event_pending |= SATA_EVNT_MAIN; 18117 mutex_exit(&sata_mutex); 18118 } 18119 } 18120 18121 /* 18122 * Device Detached Event processing. 18123 * Port is probed to find if a device is really gone. If so, 18124 * the device info structure is detached from the SATA port info structure 18125 * and released. 18126 * Port status is updated. 18127 * 18128 * NOTE: Port multiplier ports events are handled by 18129 * sata_process_pmdevice_detached() 18130 */ 18131 static void 18132 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst, 18133 sata_address_t *saddr) 18134 { 18135 sata_cport_info_t *cportinfo; 18136 sata_pmport_info_t *pmportinfo; 18137 sata_drive_info_t *sdevinfo; 18138 sata_device_t sata_device; 18139 sata_address_t pmport_addr; 18140 char name[16]; 18141 uint8_t cport = saddr->cport; 18142 int npmport; 18143 int rval; 18144 18145 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18146 "Processing port %d device detached", saddr->cport); 18147 18148 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 18149 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18150 /* Clear event flag */ 18151 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED; 18152 18153 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */ 18154 if ((cportinfo->cport_state & 18155 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 18156 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18157 cport_mutex); 18158 return; 18159 } 18160 /* For sanity, re-probe the port */ 18161 sata_device.satadev_rev = SATA_DEVICE_REV; 18162 sata_device.satadev_addr = *saddr; 18163 18164 /* 18165 * We have to exit mutex, because the HBA probe port function may 18166 * block on its own mutex. 18167 */ 18168 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18169 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18170 (SATA_DIP(sata_hba_inst), &sata_device); 18171 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18172 sata_update_port_info(sata_hba_inst, &sata_device); 18173 if (rval != SATA_SUCCESS) { 18174 /* Something went wrong? Fail the port */ 18175 cportinfo->cport_state = SATA_PSTATE_FAILED; 18176 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18177 cport_mutex); 18178 SATA_LOG_D((sata_hba_inst, CE_WARN, 18179 "SATA port %d probing failed", 18180 saddr->cport)); 18181 /* 18182 * We may want to release device info structure, but 18183 * it is not necessary. 18184 */ 18185 return; 18186 } else { 18187 /* port probed successfully */ 18188 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 18189 } 18190 /* 18191 * Check if a device is still attached. For sanity, check also 18192 * link status - if no link, there is no device. 18193 */ 18194 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) == 18195 SATA_PORT_DEVLINK_UP && sata_device.satadev_type != 18196 SATA_DTYPE_NONE) { 18197 /* 18198 * Device is still attached - ignore detach event. 18199 */ 18200 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18201 cport_mutex); 18202 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18203 "Ignoring detach - device still attached to port %d", 18204 sata_device.satadev_addr.cport); 18205 return; 18206 } 18207 /* 18208 * We need to detach and release device info structure here 18209 */ 18210 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 18211 /* 18212 * A port-multiplier is removed. 18213 * 18214 * Calling sata_process_pmdevice_detached() does not work 18215 * here. The port multiplier is gone, so we cannot probe 18216 * sub-port any more and all pmult-related data structure must 18217 * be de-allocated immediately. Following structure of every 18218 * implemented sub-port behind the pmult are required to 18219 * released. 18220 * 18221 * - attachment point 18222 * - target node 18223 * - sata_drive_info 18224 * - sata_pmport_info 18225 */ 18226 for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, 18227 cport); npmport ++) { 18228 SATADBG2(SATA_DBG_PMULT|SATA_DBG_EVENTS_PROC, 18229 sata_hba_inst, 18230 "Detaching target node at port %d:%d", 18231 cport, npmport); 18232 18233 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 18234 18235 /* Remove attachment point. */ 18236 name[0] = '\0'; 18237 (void) sprintf(name, "%d.%d", cport, npmport); 18238 ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name); 18239 sata_log(sata_hba_inst, CE_NOTE, 18240 "Remove attachment point of port %d:%d", 18241 cport, npmport); 18242 18243 /* Remove target node */ 18244 pmport_addr.cport = cport; 18245 pmport_addr.pmport = (uint8_t)npmport; 18246 pmport_addr.qual = SATA_ADDR_PMPORT; 18247 sata_remove_target_node(sata_hba_inst, &pmport_addr); 18248 18249 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 18250 18251 /* Release sata_pmport_info & sata_drive_info. */ 18252 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 18253 cport, npmport); 18254 ASSERT(pmportinfo != NULL); 18255 18256 sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 18257 if (sdevinfo != NULL) { 18258 (void) kmem_free((void *) sdevinfo, 18259 sizeof (sata_drive_info_t)); 18260 } 18261 18262 /* Release sata_pmport_info at last */ 18263 (void) kmem_free((void *) pmportinfo, 18264 sizeof (sata_pmport_info_t)); 18265 } 18266 18267 /* Finally, release sata_pmult_info */ 18268 (void) kmem_free((void *) 18269 SATA_CPORTINFO_PMULT_INFO(cportinfo), 18270 sizeof (sata_pmult_info_t)); 18271 SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL; 18272 18273 sata_log(sata_hba_inst, CE_WARN, 18274 "SATA port-multiplier detached at port %d", cport); 18275 18276 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 18277 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 18278 saddr->cport)->cport_mutex); 18279 } else { 18280 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 18281 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 18282 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 18283 (void) kmem_free((void *)sdevinfo, 18284 sizeof (sata_drive_info_t)); 18285 } 18286 sata_log(sata_hba_inst, CE_WARN, 18287 "SATA device detached at port %d", cport); 18288 18289 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 18290 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 18291 saddr->cport)->cport_mutex); 18292 18293 /* 18294 * Try to offline a device and remove target node 18295 * if it still exists 18296 */ 18297 sata_remove_target_node(sata_hba_inst, saddr); 18298 } 18299 18300 18301 /* 18302 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 18303 * with the hint: SE_HINT_REMOVE 18304 */ 18305 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE); 18306 } 18307 18308 /* 18309 * Port Multiplier Port Device Deattached Event processing. 18310 * 18311 * NOTE: No Mutex should be hold. 18312 */ 18313 static void 18314 sata_process_pmdevice_detached(sata_hba_inst_t *sata_hba_inst, 18315 sata_address_t *saddr) 18316 { 18317 sata_pmport_info_t *pmportinfo; 18318 sata_drive_info_t *sdevinfo; 18319 sata_device_t sata_device; 18320 int rval; 18321 uint8_t cport, pmport; 18322 18323 cport = saddr->cport; 18324 pmport = saddr->pmport; 18325 18326 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18327 "Processing port %d:%d device detached", 18328 cport, pmport); 18329 18330 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 18331 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18332 18333 /* Clear event flag */ 18334 pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED; 18335 18336 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */ 18337 if ((pmportinfo->pmport_state & 18338 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 18339 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18340 return; 18341 } 18342 /* For sanity, re-probe the port */ 18343 sata_device.satadev_rev = SATA_DEVICE_REV; 18344 sata_device.satadev_addr = *saddr; 18345 18346 /* 18347 * We have to exit mutex, because the HBA probe port function may 18348 * block on its own mutex. 18349 */ 18350 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18351 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18352 (SATA_DIP(sata_hba_inst), &sata_device); 18353 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18354 sata_update_pmport_info(sata_hba_inst, &sata_device); 18355 if (rval != SATA_SUCCESS) { 18356 /* Something went wrong? Fail the port */ 18357 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 18358 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18359 SATA_LOG_D((sata_hba_inst, CE_WARN, 18360 "SATA port %d:%d probing failed", 18361 saddr->pmport)); 18362 /* 18363 * We may want to release device info structure, but 18364 * it is not necessary. 18365 */ 18366 return; 18367 } else { 18368 /* port probed successfully */ 18369 pmportinfo->pmport_state |= 18370 SATA_STATE_PROBED | SATA_STATE_READY; 18371 } 18372 /* 18373 * Check if a device is still attached. For sanity, check also 18374 * link status - if no link, there is no device. 18375 */ 18376 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) == 18377 SATA_PORT_DEVLINK_UP && sata_device.satadev_type != 18378 SATA_DTYPE_NONE) { 18379 /* 18380 * Device is still attached - ignore detach event. 18381 */ 18382 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18383 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18384 "Ignoring detach - device still attached to port %d", 18385 sata_device.satadev_addr.pmport); 18386 return; 18387 } 18388 /* 18389 * We need to detach and release device info structure here 18390 */ 18391 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 18392 sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 18393 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 18394 (void) kmem_free((void *)sdevinfo, 18395 sizeof (sata_drive_info_t)); 18396 } 18397 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 18398 /* 18399 * Device cannot be reached anymore, even if the target node may be 18400 * still present. 18401 */ 18402 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 18403 18404 /* 18405 * Try to offline a device and remove target node if it still exists 18406 */ 18407 sata_remove_target_node(sata_hba_inst, saddr); 18408 18409 /* 18410 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 18411 * with the hint: SE_HINT_REMOVE 18412 */ 18413 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE); 18414 } 18415 18416 18417 /* 18418 * Device Attached Event processing. 18419 * Port state is checked to verify that a device is really attached. If so, 18420 * the device info structure is created and attached to the SATA port info 18421 * structure. 18422 * 18423 * If attached device cannot be identified or set-up, the retry for the 18424 * attach processing is set-up. Subsequent daemon run would try again to 18425 * identify the device, until the time limit is reached 18426 * (SATA_DEV_IDENTIFY_TIMEOUT). 18427 * 18428 * This function cannot be called in interrupt context (it may sleep). 18429 * 18430 * NOTE: Port multiplier ports events are handled by 18431 * sata_process_pmdevice_attached() 18432 */ 18433 static void 18434 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst, 18435 sata_address_t *saddr) 18436 { 18437 sata_cport_info_t *cportinfo = NULL; 18438 sata_drive_info_t *sdevinfo = NULL; 18439 sata_pmult_info_t *pmultinfo = NULL; 18440 sata_pmport_info_t *pmportinfo = NULL; 18441 sata_device_t sata_device; 18442 dev_info_t *tdip; 18443 uint32_t event_flags = 0, pmult_event_flags = 0; 18444 int rval; 18445 int npmport; 18446 18447 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18448 "Processing port %d device attached", saddr->cport); 18449 18450 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 18451 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18452 18453 /* Clear attach event flag first */ 18454 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED; 18455 18456 /* If the port is in SHUTDOWN or FAILED state, ignore event. */ 18457 if ((cportinfo->cport_state & 18458 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 18459 cportinfo->cport_dev_attach_time = 0; 18460 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18461 cport_mutex); 18462 return; 18463 } 18464 18465 /* 18466 * If the sata_drive_info structure is found attached to the port info, 18467 * despite the fact the device was removed and now it is re-attached, 18468 * the old drive info structure was not removed. 18469 * Arbitrarily release device info structure. 18470 */ 18471 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 18472 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 18473 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 18474 (void) kmem_free((void *)sdevinfo, 18475 sizeof (sata_drive_info_t)); 18476 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18477 "Arbitrarily detaching old device info.", NULL); 18478 } 18479 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 18480 18481 /* For sanity, re-probe the port */ 18482 sata_device.satadev_rev = SATA_DEVICE_REV; 18483 sata_device.satadev_addr = *saddr; 18484 18485 /* 18486 * We have to exit mutex, because the HBA probe port function may 18487 * block on its own mutex. 18488 */ 18489 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18490 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18491 (SATA_DIP(sata_hba_inst), &sata_device); 18492 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18493 sata_update_port_info(sata_hba_inst, &sata_device); 18494 if (rval != SATA_SUCCESS) { 18495 /* Something went wrong? Fail the port */ 18496 cportinfo->cport_state = SATA_PSTATE_FAILED; 18497 cportinfo->cport_dev_attach_time = 0; 18498 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18499 cport_mutex); 18500 SATA_LOG_D((sata_hba_inst, CE_WARN, 18501 "SATA port %d probing failed", 18502 saddr->cport)); 18503 return; 18504 } else { 18505 /* port probed successfully */ 18506 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 18507 } 18508 /* 18509 * Check if a device is still attached. For sanity, check also 18510 * link status - if no link, there is no device. 18511 */ 18512 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 18513 SATA_PORT_DEVLINK_UP || sata_device.satadev_type == 18514 SATA_DTYPE_NONE) { 18515 /* 18516 * No device - ignore attach event. 18517 */ 18518 cportinfo->cport_dev_attach_time = 0; 18519 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18520 cport_mutex); 18521 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18522 "Ignoring attach - no device connected to port %d", 18523 sata_device.satadev_addr.cport); 18524 return; 18525 } 18526 18527 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18528 /* 18529 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 18530 * with the hint: SE_HINT_INSERT 18531 */ 18532 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT); 18533 18534 /* 18535 * Port reprobing will take care of the creation of the device 18536 * info structure and determination of the device type. 18537 */ 18538 sata_device.satadev_addr = *saddr; 18539 (void) sata_reprobe_port(sata_hba_inst, &sata_device, 18540 SATA_DEV_IDENTIFY_NORETRY); 18541 18542 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18543 cport_mutex); 18544 if ((cportinfo->cport_state & SATA_STATE_READY) && 18545 (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) { 18546 /* Some device is attached to the port */ 18547 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) { 18548 /* 18549 * A device was not successfully attached. 18550 * Track retry time for device identification. 18551 */ 18552 if (cportinfo->cport_dev_attach_time != 0) { 18553 clock_t cur_time = ddi_get_lbolt(); 18554 /* 18555 * If the retry time limit was not exceeded, 18556 * reinstate attach event. 18557 */ 18558 if ((cur_time - 18559 cportinfo->cport_dev_attach_time) < 18560 drv_usectohz( 18561 SATA_DEV_IDENTIFY_TIMEOUT)) { 18562 /* OK, restore attach event */ 18563 cportinfo->cport_event_flags |= 18564 SATA_EVNT_DEVICE_ATTACHED; 18565 } else { 18566 /* Timeout - cannot identify device */ 18567 cportinfo->cport_dev_attach_time = 0; 18568 sata_log(sata_hba_inst, 18569 CE_WARN, 18570 "Could not identify SATA device " 18571 "at port %d", 18572 saddr->cport); 18573 } 18574 } else { 18575 /* 18576 * Start tracking time for device 18577 * identification. 18578 * Save current time (lbolt value). 18579 */ 18580 cportinfo->cport_dev_attach_time = 18581 ddi_get_lbolt(); 18582 /* Restore attach event */ 18583 cportinfo->cport_event_flags |= 18584 SATA_EVNT_DEVICE_ATTACHED; 18585 } 18586 } else if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 18587 cportinfo->cport_dev_attach_time = 0; 18588 sata_log(sata_hba_inst, CE_NOTE, 18589 "SATA port-multiplier detected at port %d", 18590 saddr->cport); 18591 18592 if (SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL) { 18593 /* Log the info of new port multiplier */ 18594 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 18595 saddr->cport)->cport_mutex); 18596 sata_show_pmult_info(sata_hba_inst, 18597 &sata_device); 18598 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 18599 saddr->cport)->cport_mutex); 18600 } 18601 18602 ASSERT(SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL); 18603 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 18604 for (npmport = 0; npmport < 18605 pmultinfo->pmult_num_dev_ports; npmport++) { 18606 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 18607 saddr->cport, npmport); 18608 ASSERT(pmportinfo != NULL); 18609 18610 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 18611 saddr->cport)->cport_mutex); 18612 mutex_enter(&pmportinfo->pmport_mutex); 18613 /* Marked all pmports with link events. */ 18614 pmportinfo->pmport_event_flags = 18615 SATA_EVNT_LINK_ESTABLISHED; 18616 pmult_event_flags |= 18617 pmportinfo->pmport_event_flags; 18618 mutex_exit(&pmportinfo->pmport_mutex); 18619 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 18620 saddr->cport)->cport_mutex); 18621 } 18622 /* Auto-online is not available for PMult now. */ 18623 18624 } else { 18625 /* 18626 * If device was successfully attached, the subsequent 18627 * action depends on a state of the 18628 * sata_auto_online variable. If it is set to zero. 18629 * an explicit 'configure' command will be needed to 18630 * configure it. If its value is non-zero, we will 18631 * attempt to online (configure) the device. 18632 * First, log the message indicating that a device 18633 * was attached. 18634 */ 18635 cportinfo->cport_dev_attach_time = 0; 18636 sata_log(sata_hba_inst, CE_WARN, 18637 "SATA device detected at port %d", saddr->cport); 18638 18639 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 18640 sata_drive_info_t new_sdinfo; 18641 18642 /* Log device info data */ 18643 new_sdinfo = *(SATA_CPORTINFO_DRV_INFO( 18644 cportinfo)); 18645 sata_show_drive_info(sata_hba_inst, 18646 &new_sdinfo); 18647 } 18648 18649 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 18650 saddr->cport)->cport_mutex); 18651 18652 /* 18653 * Make sure that there is no target node for that 18654 * device. If so, release it. It should not happen, 18655 * unless we had problem removing the node when 18656 * device was detached. 18657 */ 18658 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 18659 saddr->cport, saddr->pmport); 18660 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 18661 saddr->cport)->cport_mutex); 18662 if (tdip != NULL) { 18663 18664 #ifdef SATA_DEBUG 18665 if ((cportinfo->cport_event_flags & 18666 SATA_EVNT_TARGET_NODE_CLEANUP) == 0) 18667 sata_log(sata_hba_inst, CE_WARN, 18668 "sata_process_device_attached: " 18669 "old device target node exists!"); 18670 #endif 18671 /* 18672 * target node exists - try to unconfigure 18673 * device and remove the node. 18674 */ 18675 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 18676 saddr->cport)->cport_mutex); 18677 rval = ndi_devi_offline(tdip, 18678 NDI_DEVI_REMOVE); 18679 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 18680 saddr->cport)->cport_mutex); 18681 18682 if (rval == NDI_SUCCESS) { 18683 cportinfo->cport_event_flags &= 18684 ~SATA_EVNT_TARGET_NODE_CLEANUP; 18685 cportinfo->cport_tgtnode_clean = B_TRUE; 18686 } else { 18687 /* 18688 * PROBLEM - the target node remained 18689 * and it belongs to a previously 18690 * attached device. 18691 * This happens when the file was open 18692 * or the node was waiting for 18693 * resources at the time the 18694 * associated device was removed. 18695 * Instruct event daemon to retry the 18696 * cleanup later. 18697 */ 18698 sata_log(sata_hba_inst, 18699 CE_WARN, 18700 "Application(s) accessing " 18701 "previously attached SATA " 18702 "device have to release " 18703 "it before newly inserted " 18704 "device can be made accessible.", 18705 saddr->cport); 18706 cportinfo->cport_event_flags |= 18707 SATA_EVNT_TARGET_NODE_CLEANUP; 18708 cportinfo->cport_tgtnode_clean = 18709 B_FALSE; 18710 } 18711 } 18712 if (sata_auto_online != 0) { 18713 cportinfo->cport_event_flags |= 18714 SATA_EVNT_AUTOONLINE_DEVICE; 18715 } 18716 18717 } 18718 } else { 18719 cportinfo->cport_dev_attach_time = 0; 18720 } 18721 18722 event_flags = cportinfo->cport_event_flags; 18723 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18724 if (event_flags != 0 || pmult_event_flags != 0) { 18725 mutex_enter(&sata_hba_inst->satahba_mutex); 18726 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 18727 mutex_exit(&sata_hba_inst->satahba_mutex); 18728 mutex_enter(&sata_mutex); 18729 sata_event_pending |= SATA_EVNT_MAIN; 18730 mutex_exit(&sata_mutex); 18731 } 18732 } 18733 18734 /* 18735 * Port Multiplier Port Device Attached Event processing. 18736 * 18737 * NOTE: No Mutex should be hold. 18738 */ 18739 static void 18740 sata_process_pmdevice_attached(sata_hba_inst_t *sata_hba_inst, 18741 sata_address_t *saddr) 18742 { 18743 sata_pmport_info_t *pmportinfo; 18744 sata_drive_info_t *sdinfo; 18745 sata_device_t sata_device; 18746 dev_info_t *tdip; 18747 uint32_t event_flags; 18748 uint8_t cport = saddr->cport; 18749 uint8_t pmport = saddr->pmport; 18750 int rval; 18751 18752 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18753 "Processing port %d:%d device attached", cport, pmport); 18754 18755 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 18756 18757 mutex_enter(&pmportinfo->pmport_mutex); 18758 18759 /* Clear attach event flag first */ 18760 pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED; 18761 18762 /* If the port is in SHUTDOWN or FAILED state, ignore event. */ 18763 if ((pmportinfo->pmport_state & 18764 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 18765 pmportinfo->pmport_dev_attach_time = 0; 18766 mutex_exit(&pmportinfo->pmport_mutex); 18767 return; 18768 } 18769 18770 /* 18771 * If the sata_drive_info structure is found attached to the port info, 18772 * despite the fact the device was removed and now it is re-attached, 18773 * the old drive info structure was not removed. 18774 * Arbitrarily release device info structure. 18775 */ 18776 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 18777 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 18778 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 18779 (void) kmem_free((void *)sdinfo, 18780 sizeof (sata_drive_info_t)); 18781 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18782 "Arbitrarily detaching old device info.", NULL); 18783 } 18784 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 18785 18786 /* For sanity, re-probe the port */ 18787 sata_device.satadev_rev = SATA_DEVICE_REV; 18788 sata_device.satadev_addr = *saddr; 18789 18790 /* 18791 * We have to exit mutex, because the HBA probe port function may 18792 * block on its own mutex. 18793 */ 18794 mutex_exit(&pmportinfo->pmport_mutex); 18795 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18796 (SATA_DIP(sata_hba_inst), &sata_device); 18797 mutex_enter(&pmportinfo->pmport_mutex); 18798 18799 sata_update_pmport_info(sata_hba_inst, &sata_device); 18800 if (rval != SATA_SUCCESS) { 18801 /* Something went wrong? Fail the port */ 18802 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 18803 pmportinfo->pmport_dev_attach_time = 0; 18804 mutex_exit(&pmportinfo->pmport_mutex); 18805 SATA_LOG_D((sata_hba_inst, CE_WARN, 18806 "SATA port %d:%d probing failed", cport, pmport)); 18807 return; 18808 } else { 18809 /* pmport probed successfully */ 18810 pmportinfo->pmport_state |= 18811 SATA_STATE_PROBED | SATA_STATE_READY; 18812 } 18813 /* 18814 * Check if a device is still attached. For sanity, check also 18815 * link status - if no link, there is no device. 18816 */ 18817 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 18818 SATA_PORT_DEVLINK_UP || sata_device.satadev_type == 18819 SATA_DTYPE_NONE) { 18820 /* 18821 * No device - ignore attach event. 18822 */ 18823 pmportinfo->pmport_dev_attach_time = 0; 18824 mutex_exit(&pmportinfo->pmport_mutex); 18825 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18826 "Ignoring attach - no device connected to port %d:%d", 18827 cport, pmport); 18828 return; 18829 } 18830 18831 mutex_exit(&pmportinfo->pmport_mutex); 18832 /* 18833 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 18834 * with the hint: SE_HINT_INSERT 18835 */ 18836 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT); 18837 18838 /* 18839 * Port reprobing will take care of the creation of the device 18840 * info structure and determination of the device type. 18841 */ 18842 sata_device.satadev_addr = *saddr; 18843 (void) sata_reprobe_port(sata_hba_inst, &sata_device, 18844 SATA_DEV_IDENTIFY_NORETRY); 18845 18846 mutex_enter(&pmportinfo->pmport_mutex); 18847 if ((pmportinfo->pmport_state & SATA_STATE_READY) && 18848 (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE)) { 18849 /* Some device is attached to the port */ 18850 if (pmportinfo->pmport_dev_type == SATA_DTYPE_UNKNOWN) { 18851 /* 18852 * A device was not successfully attached. 18853 * Track retry time for device identification. 18854 */ 18855 if (pmportinfo->pmport_dev_attach_time != 0) { 18856 clock_t cur_time = ddi_get_lbolt(); 18857 /* 18858 * If the retry time limit was not exceeded, 18859 * reinstate attach event. 18860 */ 18861 if ((cur_time - 18862 pmportinfo->pmport_dev_attach_time) < 18863 drv_usectohz( 18864 SATA_DEV_IDENTIFY_TIMEOUT)) { 18865 /* OK, restore attach event */ 18866 pmportinfo->pmport_event_flags |= 18867 SATA_EVNT_DEVICE_ATTACHED; 18868 } else { 18869 /* Timeout - cannot identify device */ 18870 pmportinfo->pmport_dev_attach_time = 0; 18871 sata_log(sata_hba_inst, CE_WARN, 18872 "Could not identify SATA device " 18873 "at port %d:%d", 18874 cport, pmport); 18875 } 18876 } else { 18877 /* 18878 * Start tracking time for device 18879 * identification. 18880 * Save current time (lbolt value). 18881 */ 18882 pmportinfo->pmport_dev_attach_time = 18883 ddi_get_lbolt(); 18884 /* Restore attach event */ 18885 pmportinfo->pmport_event_flags |= 18886 SATA_EVNT_DEVICE_ATTACHED; 18887 } 18888 } else { 18889 /* 18890 * If device was successfully attached, the subsequent 18891 * action depends on a state of the 18892 * sata_auto_online variable. If it is set to zero. 18893 * an explicit 'configure' command will be needed to 18894 * configure it. If its value is non-zero, we will 18895 * attempt to online (configure) the device. 18896 * First, log the message indicating that a device 18897 * was attached. 18898 */ 18899 pmportinfo->pmport_dev_attach_time = 0; 18900 sata_log(sata_hba_inst, CE_WARN, 18901 "SATA device detected at port %d:%d", 18902 cport, pmport); 18903 18904 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 18905 sata_drive_info_t new_sdinfo; 18906 18907 /* Log device info data */ 18908 new_sdinfo = *(SATA_PMPORTINFO_DRV_INFO( 18909 pmportinfo)); 18910 sata_show_drive_info(sata_hba_inst, 18911 &new_sdinfo); 18912 } 18913 18914 mutex_exit(&pmportinfo->pmport_mutex); 18915 18916 /* 18917 * Make sure that there is no target node for that 18918 * device. If so, release it. It should not happen, 18919 * unless we had problem removing the node when 18920 * device was detached. 18921 */ 18922 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 18923 saddr->cport, saddr->pmport); 18924 mutex_enter(&pmportinfo->pmport_mutex); 18925 if (tdip != NULL) { 18926 18927 #ifdef SATA_DEBUG 18928 if ((pmportinfo->pmport_event_flags & 18929 SATA_EVNT_TARGET_NODE_CLEANUP) == 0) 18930 sata_log(sata_hba_inst, CE_WARN, 18931 "sata_process_device_attached: " 18932 "old device target node exists!"); 18933 #endif 18934 /* 18935 * target node exists - try to unconfigure 18936 * device and remove the node. 18937 */ 18938 mutex_exit(&pmportinfo->pmport_mutex); 18939 rval = ndi_devi_offline(tdip, 18940 NDI_DEVI_REMOVE); 18941 mutex_enter(&pmportinfo->pmport_mutex); 18942 18943 if (rval == NDI_SUCCESS) { 18944 pmportinfo->pmport_event_flags &= 18945 ~SATA_EVNT_TARGET_NODE_CLEANUP; 18946 pmportinfo->pmport_tgtnode_clean = 18947 B_TRUE; 18948 } else { 18949 /* 18950 * PROBLEM - the target node remained 18951 * and it belongs to a previously 18952 * attached device. 18953 * This happens when the file was open 18954 * or the node was waiting for 18955 * resources at the time the 18956 * associated device was removed. 18957 * Instruct event daemon to retry the 18958 * cleanup later. 18959 */ 18960 sata_log(sata_hba_inst, 18961 CE_WARN, 18962 "Application(s) accessing " 18963 "previously attached SATA " 18964 "device have to release " 18965 "it before newly inserted " 18966 "device can be made accessible." 18967 "at port %d:%d", 18968 cport, pmport); 18969 pmportinfo->pmport_event_flags |= 18970 SATA_EVNT_TARGET_NODE_CLEANUP; 18971 pmportinfo->pmport_tgtnode_clean = 18972 B_FALSE; 18973 } 18974 } 18975 if (sata_auto_online != 0) { 18976 pmportinfo->pmport_event_flags |= 18977 SATA_EVNT_AUTOONLINE_DEVICE; 18978 } 18979 18980 } 18981 } else { 18982 pmportinfo->pmport_dev_attach_time = 0; 18983 } 18984 18985 event_flags = pmportinfo->pmport_event_flags; 18986 mutex_exit(&pmportinfo->pmport_mutex); 18987 if (event_flags != 0) { 18988 mutex_enter(&sata_hba_inst->satahba_mutex); 18989 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 18990 mutex_exit(&sata_hba_inst->satahba_mutex); 18991 mutex_enter(&sata_mutex); 18992 sata_event_pending |= SATA_EVNT_MAIN; 18993 mutex_exit(&sata_mutex); 18994 } 18995 18996 /* clear the reset_in_progress events */ 18997 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 18998 if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) { 18999 /* must clear flags on cport */ 19000 sata_pmult_info_t *pminfo = 19001 SATA_PMULT_INFO(sata_hba_inst, 19002 saddr->cport); 19003 pminfo->pmult_event_flags |= 19004 SATA_EVNT_CLEAR_DEVICE_RESET; 19005 } 19006 } 19007 } 19008 19009 /* 19010 * Device Target Node Cleanup Event processing. 19011 * If the target node associated with a sata port device is in 19012 * DEVI_DEVICE_REMOVED state, an attempt is made to remove it. 19013 * If the target node cannot be removed, the event flag is left intact, 19014 * so that event daemon may re-run this function later. 19015 * 19016 * This function cannot be called in interrupt context (it may sleep). 19017 * 19018 * NOTE: Processes cport events only, not port multiplier ports. 19019 */ 19020 static void 19021 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, 19022 sata_address_t *saddr) 19023 { 19024 sata_cport_info_t *cportinfo; 19025 dev_info_t *tdip; 19026 19027 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19028 "Processing port %d device target node cleanup", saddr->cport); 19029 19030 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 19031 19032 /* 19033 * Check if there is target node for that device and it is in the 19034 * DEVI_DEVICE_REMOVED state. If so, release it. 19035 */ 19036 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport, 19037 saddr->pmport); 19038 if (tdip != NULL) { 19039 /* 19040 * target node exists - check if it is target node of 19041 * a removed device. 19042 */ 19043 if (sata_check_device_removed(tdip) == B_TRUE) { 19044 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19045 "sata_process_target_node_cleanup: " 19046 "old device target node exists!", NULL); 19047 /* 19048 * Unconfigure and remove the target node 19049 */ 19050 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) == 19051 NDI_SUCCESS) { 19052 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 19053 saddr->cport)->cport_mutex); 19054 cportinfo->cport_event_flags &= 19055 ~SATA_EVNT_TARGET_NODE_CLEANUP; 19056 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19057 saddr->cport)->cport_mutex); 19058 return; 19059 } 19060 /* 19061 * Event daemon will retry the cleanup later. 19062 */ 19063 mutex_enter(&sata_hba_inst->satahba_mutex); 19064 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 19065 mutex_exit(&sata_hba_inst->satahba_mutex); 19066 mutex_enter(&sata_mutex); 19067 sata_event_pending |= SATA_EVNT_MAIN; 19068 mutex_exit(&sata_mutex); 19069 } 19070 } else { 19071 if (saddr->qual == SATA_ADDR_CPORT || 19072 saddr->qual == SATA_ADDR_DCPORT) { 19073 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 19074 saddr->cport)->cport_mutex); 19075 cportinfo->cport_event_flags &= 19076 ~SATA_EVNT_TARGET_NODE_CLEANUP; 19077 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19078 saddr->cport)->cport_mutex); 19079 } else { 19080 /* sanity check */ 19081 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) != 19082 SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst, 19083 saddr->cport) == NULL) 19084 return; 19085 if (SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, 19086 saddr->pmport) == NULL) 19087 return; 19088 19089 mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst, 19090 saddr->cport, saddr->pmport)->pmport_mutex); 19091 SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, 19092 saddr->pmport)->pmport_event_flags &= 19093 ~SATA_EVNT_TARGET_NODE_CLEANUP; 19094 mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst, 19095 saddr->cport, saddr->pmport)->pmport_mutex); 19096 } 19097 } 19098 } 19099 19100 /* 19101 * Device AutoOnline Event processing. 19102 * If attached device is to be onlined, an attempt is made to online this 19103 * device, but only if there is no lingering (old) target node present. 19104 * If the device cannot be onlined, the event flag is left intact, 19105 * so that event daemon may re-run this function later. 19106 * 19107 * This function cannot be called in interrupt context (it may sleep). 19108 * 19109 * NOTE: Processes cport events only, not port multiplier ports. 19110 */ 19111 static void 19112 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst, 19113 sata_address_t *saddr) 19114 { 19115 sata_cport_info_t *cportinfo; 19116 sata_drive_info_t *sdinfo; 19117 sata_device_t sata_device; 19118 dev_info_t *tdip; 19119 19120 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19121 "Processing port %d attached device auto-onlining", saddr->cport); 19122 19123 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 19124 19125 /* 19126 * Check if device is present and recognized. If not, reset event. 19127 */ 19128 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19129 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) { 19130 /* Nothing to online */ 19131 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE; 19132 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19133 saddr->cport)->cport_mutex); 19134 return; 19135 } 19136 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19137 19138 /* 19139 * Check if there is target node for this device and if it is in the 19140 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep 19141 * the event for later processing. 19142 */ 19143 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport, 19144 saddr->pmport); 19145 if (tdip != NULL) { 19146 /* 19147 * target node exists - check if it is target node of 19148 * a removed device. 19149 */ 19150 if (sata_check_device_removed(tdip) == B_TRUE) { 19151 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19152 "sata_process_device_autoonline: " 19153 "old device target node exists!", NULL); 19154 /* 19155 * Event daemon will retry device onlining later. 19156 */ 19157 mutex_enter(&sata_hba_inst->satahba_mutex); 19158 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 19159 mutex_exit(&sata_hba_inst->satahba_mutex); 19160 mutex_enter(&sata_mutex); 19161 sata_event_pending |= SATA_EVNT_MAIN; 19162 mutex_exit(&sata_mutex); 19163 return; 19164 } 19165 /* 19166 * If the target node is not in the 'removed" state, assume 19167 * that it belongs to this device. There is nothing more to do, 19168 * but reset the event. 19169 */ 19170 } else { 19171 19172 /* 19173 * Try to online the device 19174 * If there is any reset-related event, remove it. We are 19175 * configuring the device and no state restoring is needed. 19176 */ 19177 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 19178 saddr->cport)->cport_mutex); 19179 sata_device.satadev_addr = *saddr; 19180 if (saddr->qual == SATA_ADDR_CPORT) 19181 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 19182 else 19183 sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT; 19184 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 19185 if (sdinfo != NULL) { 19186 if (sdinfo->satadrv_event_flags & 19187 (SATA_EVNT_DEVICE_RESET | 19188 SATA_EVNT_INPROC_DEVICE_RESET)) 19189 sdinfo->satadrv_event_flags = 0; 19190 sdinfo->satadrv_event_flags |= 19191 SATA_EVNT_CLEAR_DEVICE_RESET; 19192 19193 /* Need to create a new target node. */ 19194 cportinfo->cport_tgtnode_clean = B_TRUE; 19195 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19196 saddr->cport)->cport_mutex); 19197 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst), 19198 sata_hba_inst, &sata_device.satadev_addr); 19199 if (tdip == NULL) { 19200 /* 19201 * Configure (onlining) failed. 19202 * We will NOT retry 19203 */ 19204 SATA_LOG_D((sata_hba_inst, CE_WARN, 19205 "sata_process_device_autoonline: " 19206 "configuring SATA device at port %d failed", 19207 saddr->cport)); 19208 } 19209 } else { 19210 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19211 saddr->cport)->cport_mutex); 19212 } 19213 19214 } 19215 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19216 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE; 19217 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19218 saddr->cport)->cport_mutex); 19219 } 19220 19221 19222 static void 19223 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr, 19224 int hint) 19225 { 19226 char ap[MAXPATHLEN]; 19227 nvlist_t *ev_attr_list = NULL; 19228 int err; 19229 19230 /* Allocate and build sysevent attribute list */ 19231 err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP); 19232 if (err != 0) { 19233 SATA_LOG_D((sata_hba_inst, CE_WARN, 19234 "sata_gen_sysevent: " 19235 "cannot allocate memory for sysevent attributes\n")); 19236 return; 19237 } 19238 /* Add hint attribute */ 19239 err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint)); 19240 if (err != 0) { 19241 SATA_LOG_D((sata_hba_inst, CE_WARN, 19242 "sata_gen_sysevent: " 19243 "failed to add DR_HINT attr for sysevent")); 19244 nvlist_free(ev_attr_list); 19245 return; 19246 } 19247 /* 19248 * Add AP attribute. 19249 * Get controller pathname and convert it into AP pathname by adding 19250 * a target number. 19251 */ 19252 (void) snprintf(ap, MAXPATHLEN, "/devices"); 19253 (void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap)); 19254 (void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d", 19255 SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual)); 19256 19257 err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap); 19258 if (err != 0) { 19259 SATA_LOG_D((sata_hba_inst, CE_WARN, 19260 "sata_gen_sysevent: " 19261 "failed to add DR_AP_ID attr for sysevent")); 19262 nvlist_free(ev_attr_list); 19263 return; 19264 } 19265 19266 /* Generate/log sysevent */ 19267 err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR, 19268 ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP); 19269 if (err != DDI_SUCCESS) { 19270 SATA_LOG_D((sata_hba_inst, CE_WARN, 19271 "sata_gen_sysevent: " 19272 "cannot log sysevent, err code %x\n", err)); 19273 } 19274 19275 nvlist_free(ev_attr_list); 19276 } 19277 19278 19279 19280 19281 /* 19282 * Set DEVI_DEVICE_REMOVED state in the SATA device target node. 19283 */ 19284 static void 19285 sata_set_device_removed(dev_info_t *tdip) 19286 { 19287 int circ; 19288 19289 ASSERT(tdip != NULL); 19290 19291 ndi_devi_enter(tdip, &circ); 19292 mutex_enter(&DEVI(tdip)->devi_lock); 19293 DEVI_SET_DEVICE_REMOVED(tdip); 19294 mutex_exit(&DEVI(tdip)->devi_lock); 19295 ndi_devi_exit(tdip, circ); 19296 } 19297 19298 19299 /* 19300 * Set internal event instructing event daemon to try 19301 * to perform the target node cleanup. 19302 */ 19303 static void 19304 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, 19305 sata_address_t *saddr) 19306 { 19307 if (saddr->qual == SATA_ADDR_CPORT || 19308 saddr->qual == SATA_ADDR_DCPORT) { 19309 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 19310 saddr->cport)->cport_mutex); 19311 SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |= 19312 SATA_EVNT_TARGET_NODE_CLEANUP; 19313 SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19314 cport_tgtnode_clean = B_FALSE; 19315 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19316 saddr->cport)->cport_mutex); 19317 } else { 19318 mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst, 19319 saddr->cport, saddr->pmport)->pmport_mutex); 19320 SATA_PMPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport, 19321 saddr->pmport) |= SATA_EVNT_TARGET_NODE_CLEANUP; 19322 SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, saddr->pmport)-> 19323 pmport_tgtnode_clean = B_FALSE; 19324 mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst, 19325 saddr->cport, saddr->pmport)->pmport_mutex); 19326 } 19327 mutex_enter(&sata_hba_inst->satahba_mutex); 19328 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 19329 mutex_exit(&sata_hba_inst->satahba_mutex); 19330 mutex_enter(&sata_mutex); 19331 sata_event_pending |= SATA_EVNT_MAIN; 19332 mutex_exit(&sata_mutex); 19333 } 19334 19335 19336 /* 19337 * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state, 19338 * i.e. check if the target node state indicates that it belongs to a removed 19339 * device. 19340 * 19341 * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state, 19342 * B_FALSE otherwise. 19343 */ 19344 static boolean_t 19345 sata_check_device_removed(dev_info_t *tdip) 19346 { 19347 ASSERT(tdip != NULL); 19348 19349 if (DEVI_IS_DEVICE_REMOVED(tdip)) 19350 return (B_TRUE); 19351 else 19352 return (B_FALSE); 19353 } 19354 19355 /* ************************ FAULT INJECTTION **************************** */ 19356 19357 #ifdef SATA_INJECT_FAULTS 19358 19359 static uint32_t sata_fault_count = 0; 19360 static uint32_t sata_fault_suspend_count = 0; 19361 19362 /* 19363 * Inject sata pkt fault 19364 * It modifies returned values of the sata packet. 19365 * It returns immediately if: 19366 * pkt fault injection is not enabled (via sata_inject_fault, 19367 * sata_inject_fault_count), or invalid fault is specified (sata_fault_type), 19368 * or pkt does not contain command to be faulted (set in sata_fault_cmd), or 19369 * pkt is not directed to specified fault controller/device 19370 * (sata_fault_ctrl_dev and sata_fault_device). 19371 * If fault controller is not specified, fault injection applies to all 19372 * controllers and devices. 19373 * 19374 * First argument is the pointer to the executed sata packet. 19375 * Second argument is a pointer to a value returned by the HBA tran_start 19376 * function. 19377 * Third argument specifies injected error. Injected sata packet faults 19378 * are the satapkt_reason values. 19379 * SATA_PKT_BUSY -1 Not completed, busy 19380 * SATA_PKT_DEV_ERROR 1 Device reported error 19381 * SATA_PKT_QUEUE_FULL 2 Not accepted, queue full 19382 * SATA_PKT_PORT_ERROR 3 Not completed, port error 19383 * SATA_PKT_CMD_UNSUPPORTED 4 Cmd unsupported 19384 * SATA_PKT_ABORTED 5 Aborted by request 19385 * SATA_PKT_TIMEOUT 6 Operation timeut 19386 * SATA_PKT_RESET 7 Aborted by reset request 19387 * 19388 * Additional global variables affecting the execution: 19389 * 19390 * sata_inject_fault_count variable specifies number of times in row the 19391 * error is injected. Value of -1 specifies permanent fault, ie. every time 19392 * the fault injection point is reached, the fault is injected and a pause 19393 * between fault injection specified by sata_inject_fault_pause_count is 19394 * ignored). Fault injection routine decrements sata_inject_fault_count 19395 * (if greater than zero) until it reaches 0. No fault is injected when 19396 * sata_inject_fault_count is 0 (zero). 19397 * 19398 * sata_inject_fault_pause_count variable specifies number of times a fault 19399 * injection is bypassed (pause between fault injections). 19400 * If set to 0, a fault is injected only a number of times specified by 19401 * sata_inject_fault_count. 19402 * 19403 * The fault counts are static, so for periodic errors they have to be manually 19404 * reset to start repetition sequence from scratch. 19405 * If the original value returned by the HBA tran_start function is not 19406 * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error 19407 * is injected (to avoid masking real problems); 19408 * 19409 * NOTE: In its current incarnation, this function should be invoked only for 19410 * commands executed in SYNCHRONOUS mode. 19411 */ 19412 19413 19414 static void 19415 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault) 19416 { 19417 19418 if (sata_inject_fault != SATA_INJECT_PKT_FAULT) 19419 return; 19420 19421 if (sata_inject_fault_count == 0) 19422 return; 19423 19424 if (fault == 0) 19425 return; 19426 19427 if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg) 19428 return; 19429 19430 if (sata_fault_ctrl != NULL) { 19431 sata_pkt_txlate_t *spx = 19432 (sata_pkt_txlate_t *)spkt->satapkt_framework_private; 19433 19434 if (sata_fault_ctrl != NULL && sata_fault_ctrl != 19435 spx->txlt_sata_hba_inst->satahba_dip) 19436 return; 19437 19438 if (sata_fault_device.satadev_addr.cport != 19439 spkt->satapkt_device.satadev_addr.cport || 19440 sata_fault_device.satadev_addr.pmport != 19441 spkt->satapkt_device.satadev_addr.pmport || 19442 sata_fault_device.satadev_addr.qual != 19443 spkt->satapkt_device.satadev_addr.qual) 19444 return; 19445 } 19446 19447 /* Modify pkt return parameters */ 19448 if (*rval != SATA_TRAN_ACCEPTED || 19449 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 19450 sata_fault_count = 0; 19451 sata_fault_suspend_count = 0; 19452 return; 19453 } 19454 if (sata_fault_count == 0 && sata_fault_suspend_count != 0) { 19455 /* Pause in the injection */ 19456 sata_fault_suspend_count -= 1; 19457 return; 19458 } 19459 19460 if (sata_fault_count == 0 && sata_fault_suspend_count == 0) { 19461 /* 19462 * Init inject fault cycle. If fault count is set to -1, 19463 * it is a permanent fault. 19464 */ 19465 if (sata_inject_fault_count != -1) { 19466 sata_fault_count = sata_inject_fault_count; 19467 sata_fault_suspend_count = 19468 sata_inject_fault_pause_count; 19469 if (sata_fault_suspend_count == 0) 19470 sata_inject_fault_count = 0; 19471 } 19472 } 19473 19474 if (sata_fault_count != 0) 19475 sata_fault_count -= 1; 19476 19477 switch (fault) { 19478 case SATA_PKT_BUSY: 19479 *rval = SATA_TRAN_BUSY; 19480 spkt->satapkt_reason = SATA_PKT_BUSY; 19481 break; 19482 19483 case SATA_PKT_QUEUE_FULL: 19484 *rval = SATA_TRAN_QUEUE_FULL; 19485 spkt->satapkt_reason = SATA_PKT_QUEUE_FULL; 19486 break; 19487 19488 case SATA_PKT_CMD_UNSUPPORTED: 19489 *rval = SATA_TRAN_CMD_UNSUPPORTED; 19490 spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED; 19491 break; 19492 19493 case SATA_PKT_PORT_ERROR: 19494 /* This is "rejected" command */ 19495 *rval = SATA_TRAN_PORT_ERROR; 19496 spkt->satapkt_reason = SATA_PKT_PORT_ERROR; 19497 /* Additional error setup could be done here - port state */ 19498 break; 19499 19500 case SATA_PKT_DEV_ERROR: 19501 spkt->satapkt_reason = SATA_PKT_DEV_ERROR; 19502 /* 19503 * Additional error setup could be done here 19504 */ 19505 break; 19506 19507 case SATA_PKT_ABORTED: 19508 spkt->satapkt_reason = SATA_PKT_ABORTED; 19509 break; 19510 19511 case SATA_PKT_TIMEOUT: 19512 spkt->satapkt_reason = SATA_PKT_TIMEOUT; 19513 /* Additional error setup could be done here */ 19514 break; 19515 19516 case SATA_PKT_RESET: 19517 spkt->satapkt_reason = SATA_PKT_RESET; 19518 /* 19519 * Additional error setup could be done here - device reset 19520 */ 19521 break; 19522 19523 default: 19524 break; 19525 } 19526 } 19527 19528 #endif 19529 19530 /* 19531 * SATA Trace Ring Buffer 19532 * ---------------------- 19533 * 19534 * Overview 19535 * 19536 * The SATA trace ring buffer is a ring buffer created and managed by 19537 * the SATA framework module that can be used by any module or driver 19538 * within the SATA framework to store debug messages. 19539 * 19540 * Ring Buffer Interfaces: 19541 * 19542 * sata_vtrace_debug() <-- Adds debug message to ring buffer 19543 * sata_trace_debug() <-- Wraps varargs into sata_vtrace_debug() 19544 * 19545 * Note that the sata_trace_debug() interface was created to give 19546 * consumers the flexibilty of sending debug messages to ring buffer 19547 * as variable arguments. Consumers can send type va_list debug 19548 * messages directly to sata_vtrace_debug(). The sata_trace_debug() 19549 * and sata_vtrace_debug() relationship is similar to that of 19550 * cmn_err(9F) and vcmn_err(9F). 19551 * 19552 * Below is a diagram of the SATA trace ring buffer interfaces and 19553 * sample consumers: 19554 * 19555 * +---------------------------------+ 19556 * | o o SATA Framework Module | 19557 * | o SATA o +------------------+ +------------------+ 19558 * |o Trace o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1| 19559 * |o R-Buf o |sata_trace_debug |<--+ +------------------+ 19560 * | o o +------------------+ | +------------------+ 19561 * | o o ^ | +--|SATA HBA Driver #2| 19562 * | | | +------------------+ 19563 * | +------------------+ | 19564 * | |SATA Debug Message| | 19565 * | +------------------+ | 19566 * +---------------------------------+ 19567 * 19568 * Supporting Routines: 19569 * 19570 * sata_trace_rbuf_alloc() <-- Initializes ring buffer 19571 * sata_trace_rbuf_free() <-- Destroys ring buffer 19572 * sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer 19573 * sata_trace_dmsg_free() <-- Destroys content of ring buffer 19574 * 19575 * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE. 19576 * The ring buffer size can be adjusted by setting dmsg_ring_size in 19577 * /etc/system to desired size in unit of bytes. 19578 * 19579 * The individual debug message size in the ring buffer is restricted 19580 * to DMSG_BUF_SIZE. 19581 */ 19582 void 19583 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap) 19584 { 19585 sata_trace_dmsg_t *dmsg; 19586 19587 if (sata_debug_rbuf == NULL) { 19588 return; 19589 } 19590 19591 /* 19592 * If max size of ring buffer is smaller than size 19593 * required for one debug message then just return 19594 * since we have no room for the debug message. 19595 */ 19596 if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) { 19597 return; 19598 } 19599 19600 mutex_enter(&sata_debug_rbuf->lock); 19601 19602 /* alloc or reuse on ring buffer */ 19603 dmsg = sata_trace_dmsg_alloc(); 19604 19605 if (dmsg == NULL) { 19606 /* resource allocation failed */ 19607 mutex_exit(&sata_debug_rbuf->lock); 19608 return; 19609 } 19610 19611 dmsg->dip = dip; 19612 gethrestime(&dmsg->timestamp); 19613 19614 (void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap); 19615 19616 mutex_exit(&sata_debug_rbuf->lock); 19617 } 19618 19619 void 19620 sata_trace_debug(dev_info_t *dip, const char *fmt, ...) 19621 { 19622 va_list ap; 19623 19624 va_start(ap, fmt); 19625 sata_vtrace_debug(dip, fmt, ap); 19626 va_end(ap); 19627 } 19628 19629 /* 19630 * This routine is used to manage debug messages 19631 * on ring buffer. 19632 */ 19633 static sata_trace_dmsg_t * 19634 sata_trace_dmsg_alloc(void) 19635 { 19636 sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp; 19637 19638 if (sata_debug_rbuf->looped == TRUE) { 19639 sata_debug_rbuf->dmsgp = dmsg->next; 19640 return (sata_debug_rbuf->dmsgp); 19641 } 19642 19643 /* 19644 * If we're looping for the first time, 19645 * connect the ring. 19646 */ 19647 if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) > 19648 sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) { 19649 dmsg->next = sata_debug_rbuf->dmsgh; 19650 sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh; 19651 sata_debug_rbuf->looped = TRUE; 19652 return (sata_debug_rbuf->dmsgp); 19653 } 19654 19655 /* If we've gotten this far then memory allocation is needed */ 19656 dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP); 19657 if (dmsg_alloc == NULL) { 19658 sata_debug_rbuf->allocfailed++; 19659 return (dmsg_alloc); 19660 } else { 19661 sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t); 19662 } 19663 19664 if (sata_debug_rbuf->dmsgp != NULL) { 19665 dmsg->next = dmsg_alloc; 19666 sata_debug_rbuf->dmsgp = dmsg->next; 19667 return (sata_debug_rbuf->dmsgp); 19668 } else { 19669 /* 19670 * We should only be here if we're initializing 19671 * the ring buffer. 19672 */ 19673 if (sata_debug_rbuf->dmsgh == NULL) { 19674 sata_debug_rbuf->dmsgh = dmsg_alloc; 19675 } else { 19676 /* Something is wrong */ 19677 kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t)); 19678 return (NULL); 19679 } 19680 19681 sata_debug_rbuf->dmsgp = dmsg_alloc; 19682 return (sata_debug_rbuf->dmsgp); 19683 } 19684 } 19685 19686 19687 /* 19688 * Free all messages on debug ring buffer. 19689 */ 19690 static void 19691 sata_trace_dmsg_free(void) 19692 { 19693 sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh; 19694 19695 while (dmsg != NULL) { 19696 dmsg_next = dmsg->next; 19697 kmem_free(dmsg, sizeof (sata_trace_dmsg_t)); 19698 19699 /* 19700 * If we've looped around the ring than we're done. 19701 */ 19702 if (dmsg_next == sata_debug_rbuf->dmsgh) { 19703 break; 19704 } else { 19705 dmsg = dmsg_next; 19706 } 19707 } 19708 } 19709 19710 19711 /* 19712 * This function can block 19713 */ 19714 static void 19715 sata_trace_rbuf_alloc(void) 19716 { 19717 sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP); 19718 19719 mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL); 19720 19721 if (dmsg_ring_size > 0) { 19722 sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size; 19723 } 19724 } 19725 19726 19727 static void 19728 sata_trace_rbuf_free(void) 19729 { 19730 sata_trace_dmsg_free(); 19731 mutex_destroy(&sata_debug_rbuf->lock); 19732 kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t)); 19733 } 19734 19735 /* 19736 * If SATA_DEBUG is not defined then this routine is called instead 19737 * of sata_log() via the SATA_LOG_D macro. 19738 */ 19739 static void 19740 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level, 19741 const char *fmt, ...) 19742 { 19743 #ifndef __lock_lint 19744 _NOTE(ARGUNUSED(level)) 19745 #endif 19746 19747 dev_info_t *dip = NULL; 19748 va_list ap; 19749 19750 if (sata_hba_inst != NULL) { 19751 dip = SATA_DIP(sata_hba_inst); 19752 } 19753 19754 va_start(ap, fmt); 19755 sata_vtrace_debug(dip, fmt, ap); 19756 va_end(ap); 19757 } 19758