1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. 24 */ 25 /* 26 * Copyright 2017 Nexenta Systems, Inc. All rights reserved. 27 * Copyright 2016 Argo Technologies SA 28 * Copyright 2019 Joyent, Inc. 29 */ 30 31 /* 32 * SATA Framework 33 * Generic SATA Host Adapter Implementation 34 */ 35 36 #include <sys/conf.h> 37 #include <sys/file.h> 38 #include <sys/ddi.h> 39 #include <sys/sunddi.h> 40 #include <sys/modctl.h> 41 #include <sys/cmn_err.h> 42 #include <sys/errno.h> 43 #include <sys/thread.h> 44 #include <sys/kstat.h> 45 #include <sys/note.h> 46 #include <sys/sysevent.h> 47 #include <sys/sysevent/eventdefs.h> 48 #include <sys/sysevent/dr.h> 49 #include <sys/taskq.h> 50 #include <sys/disp.h> 51 #include <sys/sdt.h> 52 53 #include <sys/sata/impl/sata.h> 54 #include <sys/sata/sata_hba.h> 55 #include <sys/sata/sata_defs.h> 56 #include <sys/sata/sata_cfgadm.h> 57 #include <sys/sata/sata_blacklist.h> 58 #include <sys/sata/sata_satl.h> 59 60 #include <sys/scsi/impl/spc3_types.h> 61 62 /* 63 * FMA header files 64 */ 65 #include <sys/ddifm.h> 66 #include <sys/fm/protocol.h> 67 #include <sys/fm/util.h> 68 #include <sys/fm/io/ddi.h> 69 70 /* Debug flags - defined in sata.h */ 71 int sata_debug_flags = 0; 72 int sata_msg = 0; 73 74 /* 75 * Flags enabling selected SATA HBA framework functionality 76 */ 77 #define SATA_ENABLE_QUEUING 1 78 #define SATA_ENABLE_NCQ 2 79 #define SATA_ENABLE_PROCESS_EVENTS 4 80 #define SATA_ENABLE_PMULT_FBS 8 /* FIS-Based Switching */ 81 int sata_func_enable = 82 SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ; 83 84 /* 85 * Global variable setting default maximum queue depth (NCQ or TCQ) 86 * Note:minimum queue depth is 1 87 */ 88 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */ 89 90 /* 91 * Currently used default NCQ/TCQ queue depth. It is set-up during the driver 92 * initialization, using value from sata_max_queue_depth 93 * It is adjusted to minimum supported by the controller and by the device, 94 * if queueing is enabled. 95 */ 96 static int sata_current_max_qdepth; 97 98 /* 99 * Global variable determining the default behavior after device hotpluggin. 100 * If non-zero, the hotplugged device is onlined (if possible) without explicit 101 * IOCTL request (AP_CONFIGURE). 102 * If zero, hotplugged device is identified, but not onlined. 103 * Enabling (AP_CONNECT) device port with an attached device does not result 104 * in device onlining regardless of the flag setting 105 */ 106 int sata_auto_online = 0; 107 108 #ifdef SATA_DEBUG 109 110 #define SATA_LOG_D(args) sata_log args 111 uint64_t mbuf_count = 0; 112 uint64_t mbuffail_count = 0; 113 114 sata_atapi_cmd_t sata_atapi_trace[64]; 115 uint32_t sata_atapi_trace_index = 0; 116 int sata_atapi_trace_save = 1; 117 static void sata_save_atapi_trace(sata_pkt_txlate_t *, int); 118 #define SATAATAPITRACE(spx, count) if (sata_atapi_trace_save) \ 119 sata_save_atapi_trace(spx, count); 120 121 #else 122 #define SATA_LOG_D(args) sata_trace_log args 123 #define SATAATAPITRACE(spx, count) 124 #endif 125 126 #if 0 127 static void 128 sata_test_atapi_packet_command(sata_hba_inst_t *, int); 129 #endif 130 131 #ifdef SATA_INJECT_FAULTS 132 133 #define SATA_INJECT_PKT_FAULT 1 134 uint32_t sata_inject_fault = 0; 135 136 uint32_t sata_inject_fault_count = 0; 137 uint32_t sata_inject_fault_pause_count = 0; 138 uint32_t sata_fault_type = 0; 139 uint32_t sata_fault_cmd = 0; 140 dev_info_t *sata_fault_ctrl = NULL; 141 sata_device_t sata_fault_device; 142 143 static void sata_inject_pkt_fault(sata_pkt_t *, int *, int); 144 145 #endif 146 147 #define LEGACY_HWID_LEN 64 /* Model (40) + Serial (20) + pad */ 148 149 static char sata_rev_tag[] = {"1.46"}; 150 151 /* 152 * SATA cb_ops functions 153 */ 154 static int sata_hba_open(dev_t *, int, int, cred_t *); 155 static int sata_hba_close(dev_t, int, int, cred_t *); 156 static int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 157 158 /* 159 * SCSA required entry points 160 */ 161 static int sata_scsi_tgt_init(dev_info_t *, dev_info_t *, 162 scsi_hba_tran_t *, struct scsi_device *); 163 static int sata_scsi_tgt_probe(struct scsi_device *, 164 int (*callback)(void)); 165 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *, 166 scsi_hba_tran_t *, struct scsi_device *); 167 static int sata_scsi_start(struct scsi_address *, struct scsi_pkt *); 168 static int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *); 169 static int sata_scsi_reset(struct scsi_address *, int); 170 static int sata_scsi_getcap(struct scsi_address *, char *, int); 171 static int sata_scsi_setcap(struct scsi_address *, char *, int, int); 172 static struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *, 173 struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t), 174 caddr_t); 175 static void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *); 176 static void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *); 177 static void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *); 178 179 /* 180 * SATA HBA interface functions are defined in sata_hba.h header file 181 */ 182 183 /* Event processing functions */ 184 static void sata_event_daemon(void *); 185 static void sata_event_thread_control(int); 186 static void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst); 187 static void sata_process_pmult_events(sata_hba_inst_t *, uint8_t); 188 static void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *); 189 static void sata_process_pmdevice_reset(sata_hba_inst_t *, sata_address_t *); 190 static void sata_process_port_failed_event(sata_hba_inst_t *, 191 sata_address_t *); 192 static void sata_process_port_link_events(sata_hba_inst_t *, 193 sata_address_t *); 194 static void sata_process_pmport_link_events(sata_hba_inst_t *, 195 sata_address_t *); 196 static void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *); 197 static void sata_process_pmdevice_detached(sata_hba_inst_t *, 198 sata_address_t *); 199 static void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *); 200 static void sata_process_pmdevice_attached(sata_hba_inst_t *, 201 sata_address_t *); 202 static void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *); 203 static void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *); 204 static void sata_process_target_node_cleanup(sata_hba_inst_t *, 205 sata_address_t *); 206 static void sata_process_device_autoonline(sata_hba_inst_t *, 207 sata_address_t *saddr); 208 209 /* 210 * Local translation functions 211 */ 212 static int sata_txlt_inquiry(sata_pkt_txlate_t *); 213 static int sata_txlt_test_unit_ready(sata_pkt_txlate_t *); 214 static int sata_txlt_start_stop_unit(sata_pkt_txlate_t *); 215 static int sata_txlt_read_capacity(sata_pkt_txlate_t *); 216 static int sata_txlt_read_capacity16(sata_pkt_txlate_t *); 217 static int sata_txlt_unmap(sata_pkt_txlate_t *); 218 static int sata_txlt_request_sense(sata_pkt_txlate_t *); 219 static int sata_txlt_read(sata_pkt_txlate_t *); 220 static int sata_txlt_write(sata_pkt_txlate_t *); 221 static int sata_txlt_log_sense(sata_pkt_txlate_t *); 222 static int sata_txlt_log_select(sata_pkt_txlate_t *); 223 static int sata_txlt_mode_sense(sata_pkt_txlate_t *); 224 static int sata_txlt_mode_select(sata_pkt_txlate_t *); 225 static int sata_txlt_ata_pass_thru(sata_pkt_txlate_t *); 226 static int sata_txlt_synchronize_cache(sata_pkt_txlate_t *); 227 static int sata_txlt_write_buffer(sata_pkt_txlate_t *); 228 static int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *); 229 230 static int sata_hba_start(sata_pkt_txlate_t *, int *); 231 static int sata_txlt_invalid_command(sata_pkt_txlate_t *); 232 static int sata_txlt_check_condition(sata_pkt_txlate_t *, uchar_t, uchar_t); 233 static int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *); 234 static int sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *); 235 static int sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *); 236 static void sata_txlt_rw_completion(sata_pkt_t *); 237 static void sata_txlt_nodata_cmd_completion(sata_pkt_t *); 238 static void sata_txlt_apt_completion(sata_pkt_t *sata_pkt); 239 static void sata_txlt_unmap_completion(sata_pkt_t *sata_pkt); 240 static void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *); 241 static int sata_emul_rw_completion(sata_pkt_txlate_t *); 242 static void sata_fill_ata_return_desc(sata_pkt_t *, uint8_t, uint8_t, 243 uint8_t); 244 static struct scsi_extended_sense *sata_immediate_error_response( 245 sata_pkt_txlate_t *, int); 246 static struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *); 247 248 static int sata_txlt_atapi(sata_pkt_txlate_t *); 249 static void sata_txlt_atapi_completion(sata_pkt_t *); 250 251 /* 252 * Local functions for ioctl 253 */ 254 static int32_t sata_get_port_num(sata_hba_inst_t *, struct devctl_iocdata *); 255 static void sata_cfgadm_state(sata_hba_inst_t *, int32_t, 256 devctl_ap_state_t *); 257 static dev_info_t *sata_get_target_dip(dev_info_t *, uint8_t, uint8_t); 258 static dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *); 259 static dev_info_t *sata_devt_to_devinfo(dev_t); 260 static int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *); 261 static int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *); 262 static int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *); 263 static int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *); 264 static int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *); 265 static int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *); 266 static int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *); 267 static int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *); 268 static int sata_ioctl_reset_all(sata_hba_inst_t *); 269 static int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *); 270 static int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *, 271 sata_ioctl_data_t *, int mode); 272 static int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *, 273 sata_ioctl_data_t *, int mode); 274 static int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *, 275 sata_ioctl_data_t *, int mode); 276 static int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *, 277 sata_ioctl_data_t *, int mode); 278 static int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *, 279 sata_device_t *, sata_ioctl_data_t *, int mode); 280 281 /* 282 * Local functions 283 */ 284 static void sata_remove_hba_instance(dev_info_t *); 285 static int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *); 286 static void sata_probe_ports(sata_hba_inst_t *); 287 static void sata_probe_pmports(sata_hba_inst_t *, uint8_t); 288 static int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int); 289 static int sata_reprobe_pmult(sata_hba_inst_t *, sata_device_t *, int); 290 static int sata_reprobe_pmport(sata_hba_inst_t *, sata_device_t *, int); 291 static int sata_alloc_pmult(sata_hba_inst_t *, sata_device_t *); 292 static void sata_free_pmult(sata_hba_inst_t *, sata_device_t *); 293 static int sata_add_device(dev_info_t *, sata_hba_inst_t *, sata_device_t *); 294 static int sata_offline_device(sata_hba_inst_t *, sata_device_t *, 295 sata_drive_info_t *); 296 static dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *, 297 sata_address_t *); 298 static void sata_remove_target_node(sata_hba_inst_t *, 299 sata_address_t *); 300 static int sata_validate_scsi_address(sata_hba_inst_t *, 301 struct scsi_address *, sata_device_t *); 302 static int sata_validate_sata_address(sata_hba_inst_t *, int, int, int); 303 static sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t)); 304 static void sata_pkt_free(sata_pkt_txlate_t *); 305 static int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t), 306 caddr_t, ddi_dma_attr_t *); 307 static void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *); 308 static int sata_probe_device(sata_hba_inst_t *, sata_device_t *); 309 static sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *, 310 sata_device_t *); 311 static int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *); 312 static void sata_reidentify_device(sata_pkt_txlate_t *); 313 static struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int); 314 static void sata_free_local_buffer(sata_pkt_txlate_t *); 315 static uint64_t sata_check_capacity(sata_drive_info_t *); 316 void sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *, 317 ddi_dma_attr_t *); 318 static int sata_fetch_device_identify_data(sata_hba_inst_t *, 319 sata_drive_info_t *); 320 static void sata_update_port_info(sata_hba_inst_t *, sata_device_t *); 321 static void sata_update_pmport_info(sata_hba_inst_t *, sata_device_t *); 322 static void sata_update_port_scr(sata_port_scr_t *, sata_device_t *); 323 static int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *); 324 static int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int); 325 static int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int); 326 static int sata_set_drive_features(sata_hba_inst_t *, 327 sata_drive_info_t *, int flag); 328 static void sata_init_write_cache_mode(sata_drive_info_t *sdinfo); 329 static int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *); 330 static void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *, 331 uint8_t *); 332 static int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *, 333 struct scsi_inquiry *); 334 static int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *); 335 static int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *); 336 static int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *); 337 static int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *); 338 static int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *); 339 static int sata_mode_select_page_8(sata_pkt_txlate_t *, 340 struct mode_cache_scsi3 *, int, int *, int *, int *); 341 static int sata_mode_select_page_1a(sata_pkt_txlate_t *, 342 struct mode_info_power_cond *, int, int *, int *, int *); 343 static int sata_mode_select_page_1c(sata_pkt_txlate_t *, 344 struct mode_info_excpt_page *, int, int *, int *, int *); 345 static int sata_mode_select_page_30(sata_pkt_txlate_t *, 346 struct mode_acoustic_management *, int, int *, int *, int *); 347 348 static int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *); 349 static int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *, 350 sata_hba_inst_t *); 351 static int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *, 352 sata_hba_inst_t *); 353 static int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *, 354 sata_hba_inst_t *); 355 static int sata_build_lsense_page_0e(sata_drive_info_t *, uint8_t *, 356 sata_pkt_txlate_t *); 357 358 static void sata_set_arq_data(sata_pkt_t *); 359 static void sata_build_read_verify_cmd(sata_cmd_t *, uint16_t, uint64_t); 360 static void sata_build_generic_cmd(sata_cmd_t *, uint8_t); 361 static uint8_t sata_get_standby_timer(uint8_t *timer); 362 363 static void sata_save_drive_settings(sata_drive_info_t *); 364 static void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *); 365 static void sata_show_pmult_info(sata_hba_inst_t *, sata_device_t *); 366 static void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...); 367 static void sata_trace_log(sata_hba_inst_t *, uint_t, const char *fmt, ...); 368 static int sata_fetch_smart_return_status(sata_hba_inst_t *, 369 sata_drive_info_t *); 370 static int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *, 371 struct smart_data *); 372 static int sata_smart_selftest_log(sata_hba_inst_t *, 373 sata_drive_info_t *, 374 struct smart_selftest_log *); 375 static int sata_ext_smart_selftest_read_log(sata_hba_inst_t *, 376 sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t); 377 static int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *, 378 uint8_t *, uint8_t, uint8_t); 379 static int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *, 380 struct read_log_ext_directory *); 381 static void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int); 382 static void sata_xlate_errors(sata_pkt_txlate_t *); 383 static void sata_decode_device_error(sata_pkt_txlate_t *, 384 struct scsi_extended_sense *); 385 static void sata_set_device_removed(dev_info_t *); 386 static boolean_t sata_check_device_removed(dev_info_t *); 387 static void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *); 388 static int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *, 389 sata_drive_info_t *); 390 static int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *, 391 sata_drive_info_t *); 392 static void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *); 393 static void sata_fixed_sense_data_preset(struct scsi_extended_sense *); 394 static void sata_target_devid_register(dev_info_t *, sata_drive_info_t *); 395 static int sata_check_modser(char *, int); 396 397 /* 398 * FMA 399 */ 400 static boolean_t sata_check_for_dma_error(dev_info_t *, sata_pkt_txlate_t *); 401 402 403 /* 404 * SATA Framework will ignore SATA HBA driver cb_ops structure and 405 * register following one with SCSA framework. 406 * Open & close are provided, so scsi framework will not use its own 407 */ 408 static struct cb_ops sata_cb_ops = { 409 sata_hba_open, /* open */ 410 sata_hba_close, /* close */ 411 nodev, /* strategy */ 412 nodev, /* print */ 413 nodev, /* dump */ 414 nodev, /* read */ 415 nodev, /* write */ 416 sata_hba_ioctl, /* ioctl */ 417 nodev, /* devmap */ 418 nodev, /* mmap */ 419 nodev, /* segmap */ 420 nochpoll, /* chpoll */ 421 ddi_prop_op, /* cb_prop_op */ 422 0, /* streamtab */ 423 D_NEW | D_MP, /* cb_flag */ 424 CB_REV, /* rev */ 425 nodev, /* aread */ 426 nodev /* awrite */ 427 }; 428 429 430 extern struct mod_ops mod_miscops; 431 extern uchar_t scsi_cdb_size[]; 432 433 static struct modlmisc modlmisc = { 434 &mod_miscops, /* Type of module */ 435 "SATA Module" /* module name */ 436 }; 437 438 439 static struct modlinkage modlinkage = { 440 MODREV_1, 441 (void *)&modlmisc, 442 NULL 443 }; 444 445 /* 446 * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero, 447 * i.e. when scsi_pkt has not timeout specified. 448 */ 449 static int sata_default_pkt_time = 60; /* 60 seconds */ 450 451 /* 452 * Intermediate buffer device access attributes - they are required, 453 * but not necessarily used. 454 */ 455 static ddi_device_acc_attr_t sata_acc_attr = { 456 DDI_DEVICE_ATTR_V0, 457 DDI_STRUCTURE_LE_ACC, 458 DDI_STRICTORDER_ACC 459 }; 460 461 462 /* 463 * Mutexes protecting structures in multithreaded operations. 464 * Because events are relatively rare, a single global mutex protecting 465 * data structures should be sufficient. To increase performance, add 466 * separate mutex per each sata port and use global mutex only to protect 467 * common data structures. 468 */ 469 static kmutex_t sata_mutex; /* protects sata_hba_list */ 470 static kmutex_t sata_log_mutex; /* protects log */ 471 472 static char sata_log_buf[256]; 473 474 /* 475 * sata trace debug 476 */ 477 static sata_trace_rbuf_t *sata_debug_rbuf; 478 static sata_trace_dmsg_t *sata_trace_dmsg_alloc(void); 479 static void sata_trace_dmsg_free(void); 480 static void sata_trace_rbuf_alloc(void); 481 static void sata_trace_rbuf_free(void); 482 483 int dmsg_ring_size = DMSG_RING_SIZE; 484 485 /* Default write cache setting for SATA hard disks */ 486 int sata_write_cache = 1; /* enabled */ 487 488 /* Default write cache setting for SATA ATAPI CD/DVD */ 489 int sata_atapicdvd_write_cache = 1; /* enabled */ 490 491 /* Default write cache setting for SATA ATAPI tape */ 492 int sata_atapitape_write_cache = 1; /* enabled */ 493 494 /* Default write cache setting for SATA ATAPI disk */ 495 int sata_atapidisk_write_cache = 1; /* enabled */ 496 497 /* 498 * Linked list of HBA instances 499 */ 500 static sata_hba_inst_t *sata_hba_list = NULL; 501 static sata_hba_inst_t *sata_hba_list_tail = NULL; 502 /* 503 * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran 504 * structure and in sata soft state. 505 */ 506 507 /* 508 * Event daemon related variables 509 */ 510 static kmutex_t sata_event_mutex; 511 static kcondvar_t sata_event_cv; 512 static kthread_t *sata_event_thread = NULL; 513 static int sata_event_thread_terminate = 0; 514 static int sata_event_pending = 0; 515 static int sata_event_thread_active = 0; 516 extern pri_t minclsyspri; 517 518 /* 519 * NCQ error recovery command 520 */ 521 static const sata_cmd_t sata_rle_cmd = { 522 SATA_CMD_REV, 523 NULL, 524 { 525 SATA_DIR_READ 526 }, 527 ATA_ADDR_LBA48, 528 0, 529 0, 530 0, 531 0, 532 0, 533 1, 534 READ_LOG_EXT_NCQ_ERROR_RECOVERY, 535 0, 536 0, 537 0, 538 SATAC_READ_LOG_EXT, 539 0, 540 0, 541 0, 542 }; 543 544 /* 545 * ATAPI error recovery CDB 546 */ 547 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = { 548 SCMD_REQUEST_SENSE, 549 0, /* Only fixed RQ format is supported */ 550 0, 551 0, 552 SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */ 553 0 554 }; 555 556 557 /* Warlock directives */ 558 559 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran)) 560 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device)) 561 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops)) 562 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense)) 563 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status)) 564 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr)) 565 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t)) 566 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state)) 567 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state)) 568 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list)) 569 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list)) 570 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next)) 571 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev)) 572 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \ 573 sata_hba_inst::satahba_scsi_tran)) 574 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran)) 575 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip)) 576 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached)) 577 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port)) 578 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex, 579 sata_hba_inst::satahba_event_flags)) 580 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 581 sata_cport_info::cport_devp)) 582 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp)) 583 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr)) 584 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 585 sata_cport_info::cport_dev_type)) 586 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type)) 587 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 588 sata_cport_info::cport_state)) 589 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state)) 590 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 591 sata_pmport_info::pmport_state)) 592 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state)) 593 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 594 sata_pmport_info::pmport_dev_type)) 595 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type)) 596 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 597 sata_pmport_info::pmport_sata_drive)) 598 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 599 sata_pmport_info::pmport_tgtnode_clean)) 600 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 601 sata_pmport_info::pmport_event_flags)) 602 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive)) 603 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port)) 604 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports)) 605 #ifdef SATA_DEBUG 606 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count)) 607 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count)) 608 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace)) 609 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index)) 610 #endif 611 612 /* End of warlock directives */ 613 614 /* ************** loadable module configuration functions ************** */ 615 616 int 617 _init() 618 { 619 int rval; 620 621 mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL); 622 mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL); 623 mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL); 624 cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL); 625 sata_trace_rbuf_alloc(); 626 if ((rval = mod_install(&modlinkage)) != 0) { 627 #ifdef SATA_DEBUG 628 cmn_err(CE_WARN, "sata: _init: mod_install failed\n"); 629 #endif 630 sata_trace_rbuf_free(); 631 mutex_destroy(&sata_log_mutex); 632 cv_destroy(&sata_event_cv); 633 mutex_destroy(&sata_event_mutex); 634 mutex_destroy(&sata_mutex); 635 } 636 return (rval); 637 } 638 639 int 640 _fini() 641 { 642 int rval; 643 644 if ((rval = mod_remove(&modlinkage)) != 0) 645 return (rval); 646 647 sata_trace_rbuf_free(); 648 mutex_destroy(&sata_log_mutex); 649 cv_destroy(&sata_event_cv); 650 mutex_destroy(&sata_event_mutex); 651 mutex_destroy(&sata_mutex); 652 return (rval); 653 } 654 655 int 656 _info(struct modinfo *modinfop) 657 { 658 return (mod_info(&modlinkage, modinfop)); 659 } 660 661 662 663 /* ********************* SATA HBA entry points ********************* */ 664 665 666 /* 667 * Called by SATA HBA from _init(). 668 * Registers HBA driver instance/sata framework pair with scsi framework, by 669 * calling scsi_hba_init(). 670 * 671 * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used 672 * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver 673 * cb_ops pointer in SATA HBA driver dev_ops structure. 674 * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors. 675 * 676 * Return status of the scsi_hba_init() is returned to a calling SATA HBA 677 * driver. 678 */ 679 int 680 sata_hba_init(struct modlinkage *modlp) 681 { 682 int rval; 683 struct dev_ops *hba_ops; 684 685 SATADBG1(SATA_DBG_HBA_IF, NULL, 686 "sata_hba_init: name %s \n", 687 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo); 688 /* 689 * Fill-up cb_ops and dev_ops when necessary 690 */ 691 hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops; 692 /* 693 * Provide pointer to SATA dev_ops 694 */ 695 hba_ops->devo_cb_ops = &sata_cb_ops; 696 697 /* 698 * Register SATA HBA with SCSI framework 699 */ 700 if ((rval = scsi_hba_init(modlp)) != 0) { 701 SATADBG1(SATA_DBG_HBA_IF, NULL, 702 "sata_hba_init: scsi hba init failed\n", NULL); 703 return (rval); 704 } 705 706 return (0); 707 } 708 709 710 /* HBA attach stages */ 711 #define HBA_ATTACH_STAGE_SATA_HBA_INST 1 712 #define HBA_ATTACH_STAGE_SCSI_ATTACHED 2 713 #define HBA_ATTACH_STAGE_SETUP 4 714 #define HBA_ATTACH_STAGE_LINKED 8 715 716 717 /* 718 * 719 * Called from SATA HBA driver's attach routine to attach an instance of 720 * the HBA. 721 * 722 * For DDI_ATTACH command: 723 * sata_hba_inst structure is allocated here and initialized with pointers to 724 * SATA framework implementation of required scsi tran functions. 725 * The scsi_tran's tran_hba_private field is used by SATA Framework to point 726 * to the soft structure (sata_hba_inst) allocated by SATA framework for 727 * SATA HBA instance related data. 728 * The scsi_tran's tran_hba_private field is used by SATA framework to 729 * store a pointer to per-HBA-instance of sata_hba_inst structure. 730 * The sata_hba_inst structure is cross-linked to scsi tran structure. 731 * Among other info, a pointer to sata_hba_tran structure is stored in 732 * sata_hba_inst. The sata_hba_inst structures for different HBA instances are 733 * linked together into the list, pointed to by sata_hba_list. 734 * On the first HBA instance attach the sata event thread is initialized. 735 * Attachment points are created for all SATA ports of the HBA being attached. 736 * All HBA instance's SATA ports are probed and type of plugged devices is 737 * determined. For each device of a supported type, a target node is created. 738 * 739 * DDI_SUCCESS is returned when attachment process is successful, 740 * DDI_FAILURE is returned otherwise. 741 * 742 * For DDI_RESUME command: 743 * Not implemented at this time (postponed until phase 2 of the development). 744 */ 745 int 746 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran, 747 ddi_attach_cmd_t cmd) 748 { 749 sata_hba_inst_t *sata_hba_inst; 750 scsi_hba_tran_t *scsi_tran = NULL; 751 int hba_attach_state = 0; 752 char taskq_name[MAXPATHLEN]; 753 754 SATADBG3(SATA_DBG_HBA_IF, NULL, 755 "sata_hba_attach: node %s (%s%d)\n", 756 ddi_node_name(dip), ddi_driver_name(dip), 757 ddi_get_instance(dip)); 758 759 if (cmd == DDI_RESUME) { 760 /* 761 * Postponed until phase 2 of the development 762 */ 763 return (DDI_FAILURE); 764 } 765 766 if (cmd != DDI_ATTACH) { 767 return (DDI_FAILURE); 768 } 769 770 /* cmd == DDI_ATTACH */ 771 772 if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) { 773 SATA_LOG_D((NULL, CE_WARN, 774 "sata_hba_attach: invalid sata_hba_tran")); 775 return (DDI_FAILURE); 776 } 777 /* 778 * Allocate and initialize SCSI tran structure. 779 * SATA copy of tran_bus_config is provided to create port nodes. 780 */ 781 scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP); 782 if (scsi_tran == NULL) 783 return (DDI_FAILURE); 784 /* 785 * Allocate soft structure for SATA HBA instance. 786 * There is a separate softstate for each HBA instance. 787 */ 788 sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP); 789 ASSERT(sata_hba_inst != NULL); /* this should not fail */ 790 mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL); 791 hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST; 792 793 /* 794 * scsi_trans's tran_hba_private is used by SATA Framework to point to 795 * soft structure allocated by SATA framework for 796 * SATA HBA instance related data. 797 */ 798 scsi_tran->tran_hba_private = sata_hba_inst; 799 scsi_tran->tran_tgt_private = NULL; 800 801 scsi_tran->tran_tgt_init = sata_scsi_tgt_init; 802 scsi_tran->tran_tgt_probe = sata_scsi_tgt_probe; 803 scsi_tran->tran_tgt_free = sata_scsi_tgt_free; 804 805 scsi_tran->tran_start = sata_scsi_start; 806 scsi_tran->tran_reset = sata_scsi_reset; 807 scsi_tran->tran_abort = sata_scsi_abort; 808 scsi_tran->tran_getcap = sata_scsi_getcap; 809 scsi_tran->tran_setcap = sata_scsi_setcap; 810 scsi_tran->tran_init_pkt = sata_scsi_init_pkt; 811 scsi_tran->tran_destroy_pkt = sata_scsi_destroy_pkt; 812 813 scsi_tran->tran_dmafree = sata_scsi_dmafree; 814 scsi_tran->tran_sync_pkt = sata_scsi_sync_pkt; 815 816 scsi_tran->tran_reset_notify = NULL; 817 scsi_tran->tran_get_bus_addr = NULL; 818 scsi_tran->tran_quiesce = NULL; 819 scsi_tran->tran_unquiesce = NULL; 820 scsi_tran->tran_bus_reset = NULL; 821 822 if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr, 823 scsi_tran, 0) != DDI_SUCCESS) { 824 #ifdef SATA_DEBUG 825 cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed", 826 ddi_driver_name(dip), ddi_get_instance(dip)); 827 #endif 828 goto fail; 829 } 830 hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED; 831 832 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) { 833 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, 834 "sata", 1) != DDI_PROP_SUCCESS) { 835 SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: " 836 "failed to create hba sata prop")); 837 goto fail; 838 } 839 } 840 841 /* 842 * Save pointers in hba instance soft state. 843 */ 844 sata_hba_inst->satahba_scsi_tran = scsi_tran; 845 sata_hba_inst->satahba_tran = sata_tran; 846 sata_hba_inst->satahba_dip = dip; 847 848 /* 849 * Create a task queue to handle emulated commands completion 850 * Use node name, dash, instance number as the queue name. 851 */ 852 taskq_name[0] = '\0'; 853 (void) strlcat(taskq_name, DEVI(dip)->devi_node_name, 854 sizeof (taskq_name)); 855 (void) snprintf(taskq_name + strlen(taskq_name), 856 sizeof (taskq_name) - strlen(taskq_name), 857 "-%d", DEVI(dip)->devi_instance); 858 sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1, 859 minclsyspri, 1, sata_tran->sata_tran_hba_num_cports * 4, 860 TASKQ_DYNAMIC); 861 862 hba_attach_state |= HBA_ATTACH_STAGE_SETUP; 863 864 /* 865 * Create events thread if not created yet. 866 */ 867 sata_event_thread_control(1); 868 869 /* 870 * Link this hba instance into the list. 871 */ 872 mutex_enter(&sata_mutex); 873 874 if (sata_hba_list == NULL) { 875 /* 876 * The first instance of HBA is attached. 877 * Set current/active default maximum NCQ/TCQ queue depth for 878 * all SATA devices. It is done here and now, to eliminate the 879 * possibility of the dynamic, programatic modification of the 880 * queue depth via global (and public) sata_max_queue_depth 881 * variable (this would require special handling in HBA drivers) 882 */ 883 sata_current_max_qdepth = sata_max_queue_depth; 884 if (sata_current_max_qdepth > 32) 885 sata_current_max_qdepth = 32; 886 else if (sata_current_max_qdepth < 1) 887 sata_current_max_qdepth = 1; 888 } 889 890 sata_hba_inst->satahba_next = NULL; 891 sata_hba_inst->satahba_prev = sata_hba_list_tail; 892 if (sata_hba_list == NULL) { 893 sata_hba_list = sata_hba_inst; 894 } 895 if (sata_hba_list_tail != NULL) { 896 sata_hba_list_tail->satahba_next = sata_hba_inst; 897 } 898 sata_hba_list_tail = sata_hba_inst; 899 mutex_exit(&sata_mutex); 900 hba_attach_state |= HBA_ATTACH_STAGE_LINKED; 901 902 /* 903 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl 904 * SATA HBA driver should not use its own open/close entry points. 905 * 906 * Make sure that instance number doesn't overflow 907 * when forming minor numbers. 908 */ 909 ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT)); 910 if (ddi_create_minor_node(dip, "devctl", S_IFCHR, 911 INST2DEVCTL(ddi_get_instance(dip)), 912 DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) { 913 #ifdef SATA_DEBUG 914 cmn_err(CE_WARN, "sata_hba_attach: " 915 "cannot create devctl minor node"); 916 #endif 917 goto fail; 918 } 919 920 921 /* 922 * Set-up kstats here, if necessary. 923 * (postponed until future phase of the development). 924 */ 925 926 /* 927 * Indicate that HBA is attached. This will enable events processing 928 * for this HBA. 929 */ 930 sata_hba_inst->satahba_attached = 1; 931 /* 932 * Probe controller ports. This operation will describe a current 933 * controller/port/multipliers/device configuration and will create 934 * attachment points. 935 * We may end-up with just a controller with no devices attached. 936 * For the ports with a supported device attached, device target nodes 937 * are created and devices are initialized. 938 */ 939 sata_probe_ports(sata_hba_inst); 940 941 return (DDI_SUCCESS); 942 943 fail: 944 if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) { 945 (void) sata_remove_hba_instance(dip); 946 if (sata_hba_list == NULL) 947 sata_event_thread_control(0); 948 } 949 950 if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) { 951 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata"); 952 taskq_destroy(sata_hba_inst->satahba_taskq); 953 } 954 955 if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED) 956 (void) scsi_hba_detach(dip); 957 958 if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) { 959 mutex_destroy(&sata_hba_inst->satahba_mutex); 960 kmem_free((void *)sata_hba_inst, 961 sizeof (struct sata_hba_inst)); 962 scsi_hba_tran_free(scsi_tran); 963 } 964 965 sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed", 966 ddi_driver_name(dip), ddi_get_instance(dip)); 967 968 return (DDI_FAILURE); 969 } 970 971 972 /* 973 * Called by SATA HBA from to detach an instance of the driver. 974 * 975 * For DDI_DETACH command: 976 * Free local structures allocated for SATA HBA instance during 977 * sata_hba_attach processing. 978 * 979 * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise. 980 * 981 * For DDI_SUSPEND command: 982 * Not implemented at this time (postponed until phase 2 of the development) 983 * Returnd DDI_SUCCESS. 984 * 985 * When the last HBA instance is detached, the event daemon is terminated. 986 * 987 * NOTE: Port multiplier is supported. 988 */ 989 int 990 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 991 { 992 dev_info_t *tdip; 993 sata_hba_inst_t *sata_hba_inst; 994 scsi_hba_tran_t *scsi_hba_tran; 995 sata_cport_info_t *cportinfo; 996 sata_pmult_info_t *pminfo; 997 sata_drive_info_t *sdinfo; 998 sata_device_t sdevice; 999 int ncport, npmport; 1000 1001 SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n", 1002 ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip)); 1003 1004 switch (cmd) { 1005 case DDI_DETACH: 1006 1007 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1008 return (DDI_FAILURE); 1009 1010 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1011 if (sata_hba_inst == NULL) 1012 return (DDI_FAILURE); 1013 1014 if (scsi_hba_detach(dip) == DDI_FAILURE) { 1015 sata_hba_inst->satahba_attached = 1; 1016 return (DDI_FAILURE); 1017 } 1018 1019 /* 1020 * Free all target nodes - at this point 1021 * devices should be at least offlined 1022 * otherwise scsi_hba_detach() should not be called. 1023 */ 1024 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); 1025 ncport++) { 1026 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 1027 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 1028 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 1029 if (sdinfo != NULL) { 1030 tdip = sata_get_target_dip(dip, 1031 ncport, 0); 1032 if (tdip != NULL) { 1033 if (ndi_devi_offline(tdip, 1034 NDI_DEVI_REMOVE) != 1035 NDI_SUCCESS) { 1036 SATA_LOG_D(( 1037 sata_hba_inst, 1038 CE_WARN, 1039 "sata_hba_detach: " 1040 "Target node not " 1041 "removed !")); 1042 return (DDI_FAILURE); 1043 } 1044 } 1045 } 1046 } else { /* SATA_DTYPE_PMULT */ 1047 mutex_enter(&cportinfo->cport_mutex); 1048 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 1049 1050 if (pminfo == NULL) { 1051 SATA_LOG_D((sata_hba_inst, CE_WARN, 1052 "sata_hba_detach: Port multiplier " 1053 "not ready yet!")); 1054 mutex_exit(&cportinfo->cport_mutex); 1055 return (DDI_FAILURE); 1056 } 1057 1058 /* 1059 * Detach would fail if removal of any of the 1060 * target nodes is failed - albeit in that 1061 * case some of them may have been removed. 1062 */ 1063 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 1064 sata_hba_inst, ncport); npmport++) { 1065 tdip = sata_get_target_dip(dip, ncport, 1066 npmport); 1067 if (tdip != NULL) { 1068 if (ndi_devi_offline(tdip, 1069 NDI_DEVI_REMOVE) != 1070 NDI_SUCCESS) { 1071 SATA_LOG_D(( 1072 sata_hba_inst, 1073 CE_WARN, 1074 "sata_hba_detach: " 1075 "Target node not " 1076 "removed !")); 1077 mutex_exit(&cportinfo-> 1078 cport_mutex); 1079 return (DDI_FAILURE); 1080 } 1081 } 1082 } 1083 mutex_exit(&cportinfo->cport_mutex); 1084 } 1085 } 1086 /* 1087 * Disable sata event daemon processing for this HBA 1088 */ 1089 sata_hba_inst->satahba_attached = 0; 1090 1091 /* 1092 * Remove event daemon thread, if it is last HBA instance. 1093 */ 1094 1095 mutex_enter(&sata_mutex); 1096 if (sata_hba_list->satahba_next == NULL) { 1097 mutex_exit(&sata_mutex); 1098 sata_event_thread_control(0); 1099 mutex_enter(&sata_mutex); 1100 } 1101 mutex_exit(&sata_mutex); 1102 1103 /* Remove this HBA instance from the HBA list */ 1104 sata_remove_hba_instance(dip); 1105 1106 /* 1107 * At this point there should be no target nodes attached. 1108 * Detach and destroy device and port info structures. 1109 */ 1110 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); 1111 ncport++) { 1112 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 1113 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 1114 sdinfo = 1115 cportinfo->cport_devp.cport_sata_drive; 1116 if (sdinfo != NULL) { 1117 /* Release device structure */ 1118 kmem_free(sdinfo, 1119 sizeof (sata_drive_info_t)); 1120 } 1121 /* Release cport info */ 1122 mutex_destroy(&cportinfo->cport_mutex); 1123 kmem_free(cportinfo, 1124 sizeof (sata_cport_info_t)); 1125 } else { /* SATA_DTYPE_PMULT */ 1126 sdevice.satadev_addr.cport = (uint8_t)ncport; 1127 sdevice.satadev_addr.qual = SATA_ADDR_PMULT; 1128 sata_free_pmult(sata_hba_inst, &sdevice); 1129 } 1130 } 1131 1132 scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran); 1133 1134 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata"); 1135 1136 taskq_destroy(sata_hba_inst->satahba_taskq); 1137 1138 mutex_destroy(&sata_hba_inst->satahba_mutex); 1139 kmem_free((void *)sata_hba_inst, 1140 sizeof (struct sata_hba_inst)); 1141 1142 return (DDI_SUCCESS); 1143 1144 case DDI_SUSPEND: 1145 /* 1146 * Postponed until phase 2 1147 */ 1148 return (DDI_FAILURE); 1149 1150 default: 1151 return (DDI_FAILURE); 1152 } 1153 } 1154 1155 1156 /* 1157 * Called by an HBA drive from _fini() routine. 1158 * Unregisters SATA HBA instance/SATA framework pair from the scsi framework. 1159 */ 1160 void 1161 sata_hba_fini(struct modlinkage *modlp) 1162 { 1163 SATADBG1(SATA_DBG_HBA_IF, NULL, 1164 "sata_hba_fini: name %s\n", 1165 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo); 1166 1167 scsi_hba_fini(modlp); 1168 } 1169 1170 1171 /* 1172 * Default open and close routine for sata_hba framework. 1173 * 1174 */ 1175 /* 1176 * Open devctl node. 1177 * 1178 * Returns: 1179 * 0 if node was open successfully, error code otherwise. 1180 * 1181 * 1182 */ 1183 1184 static int 1185 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp) 1186 { 1187 #ifndef __lock_lint 1188 _NOTE(ARGUNUSED(credp)) 1189 #endif 1190 int rv = 0; 1191 dev_info_t *dip; 1192 scsi_hba_tran_t *scsi_hba_tran; 1193 sata_hba_inst_t *sata_hba_inst; 1194 1195 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL); 1196 1197 if (otyp != OTYP_CHR) 1198 return (EINVAL); 1199 1200 dip = sata_devt_to_devinfo(*devp); 1201 if (dip == NULL) 1202 return (ENXIO); 1203 1204 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1205 return (ENXIO); 1206 1207 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1208 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0) 1209 return (ENXIO); 1210 1211 mutex_enter(&sata_mutex); 1212 if (flags & FEXCL) { 1213 if (sata_hba_inst->satahba_open_flag != 0) { 1214 rv = EBUSY; 1215 } else { 1216 sata_hba_inst->satahba_open_flag = 1217 SATA_DEVCTL_EXOPENED; 1218 } 1219 } else { 1220 if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) { 1221 rv = EBUSY; 1222 } else { 1223 sata_hba_inst->satahba_open_flag = 1224 SATA_DEVCTL_SOPENED; 1225 } 1226 } 1227 mutex_exit(&sata_mutex); 1228 1229 return (rv); 1230 } 1231 1232 1233 /* 1234 * Close devctl node. 1235 * Returns: 1236 * 0 if node was closed successfully, error code otherwise. 1237 * 1238 */ 1239 1240 static int 1241 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp) 1242 { 1243 #ifndef __lock_lint 1244 _NOTE(ARGUNUSED(credp)) 1245 _NOTE(ARGUNUSED(flag)) 1246 #endif 1247 dev_info_t *dip; 1248 scsi_hba_tran_t *scsi_hba_tran; 1249 sata_hba_inst_t *sata_hba_inst; 1250 1251 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL); 1252 1253 if (otyp != OTYP_CHR) 1254 return (EINVAL); 1255 1256 dip = sata_devt_to_devinfo(dev); 1257 if (dip == NULL) 1258 return (ENXIO); 1259 1260 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1261 return (ENXIO); 1262 1263 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1264 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0) 1265 return (ENXIO); 1266 1267 mutex_enter(&sata_mutex); 1268 sata_hba_inst->satahba_open_flag = 0; 1269 mutex_exit(&sata_mutex); 1270 return (0); 1271 } 1272 1273 1274 1275 /* 1276 * Standard IOCTL commands for SATA hotplugging. 1277 * Implemented DEVCTL_AP commands: 1278 * DEVCTL_AP_CONNECT 1279 * DEVCTL_AP_DISCONNECT 1280 * DEVCTL_AP_CONFIGURE 1281 * DEVCTL_UNCONFIGURE 1282 * DEVCTL_AP_CONTROL 1283 * 1284 * Commands passed to default ndi ioctl handler: 1285 * DEVCTL_DEVICE_GETSTATE 1286 * DEVCTL_DEVICE_ONLINE 1287 * DEVCTL_DEVICE_OFFLINE 1288 * DEVCTL_DEVICE_REMOVE 1289 * DEVCTL_DEVICE_INSERT 1290 * DEVCTL_BUS_GETSTATE 1291 * 1292 * All other cmds are passed to HBA if it provide ioctl handler, or failed 1293 * if not. 1294 * 1295 * Returns: 1296 * 0 if successful, 1297 * error code if operation failed. 1298 * 1299 * Port Multiplier support is supported now. 1300 * 1301 * NOTE: qual should be SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT 1302 */ 1303 1304 static int 1305 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, 1306 int *rvalp) 1307 { 1308 #ifndef __lock_lint 1309 _NOTE(ARGUNUSED(credp)) 1310 _NOTE(ARGUNUSED(rvalp)) 1311 #endif 1312 int rv = 0; 1313 int32_t comp_port = -1; 1314 dev_info_t *dip; 1315 devctl_ap_state_t ap_state; 1316 struct devctl_iocdata *dcp = NULL; 1317 scsi_hba_tran_t *scsi_hba_tran; 1318 sata_hba_inst_t *sata_hba_inst; 1319 sata_device_t sata_device; 1320 sata_cport_info_t *cportinfo; 1321 int cport, pmport, qual; 1322 int rval = SATA_SUCCESS; 1323 1324 dip = sata_devt_to_devinfo(dev); 1325 if (dip == NULL) 1326 return (ENXIO); 1327 1328 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1329 return (ENXIO); 1330 1331 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1332 if (sata_hba_inst == NULL) 1333 return (ENXIO); 1334 1335 if (sata_hba_inst->satahba_tran == NULL) 1336 return (ENXIO); 1337 1338 switch (cmd) { 1339 1340 case DEVCTL_DEVICE_GETSTATE: 1341 case DEVCTL_DEVICE_ONLINE: 1342 case DEVCTL_DEVICE_OFFLINE: 1343 case DEVCTL_DEVICE_REMOVE: 1344 case DEVCTL_BUS_GETSTATE: 1345 /* 1346 * There may be more cases that we want to pass to default 1347 * handler rather than fail them. 1348 */ 1349 return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0)); 1350 } 1351 1352 /* read devctl ioctl data */ 1353 if (cmd != DEVCTL_AP_CONTROL && cmd >= DEVCTL_IOC && 1354 cmd <= DEVCTL_IOC_MAX) { 1355 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) 1356 return (EFAULT); 1357 1358 if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) == 1359 -1) { 1360 if (dcp) 1361 ndi_dc_freehdl(dcp); 1362 return (EINVAL); 1363 } 1364 1365 /* 1366 * According to SCSI_TO_SATA_ADDR_QUAL, qual should be either 1367 * SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT. 1368 */ 1369 cport = SCSI_TO_SATA_CPORT(comp_port); 1370 pmport = SCSI_TO_SATA_PMPORT(comp_port); 1371 qual = SCSI_TO_SATA_ADDR_QUAL(comp_port); 1372 1373 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, 1374 qual) != 0) { 1375 ndi_dc_freehdl(dcp); 1376 return (EINVAL); 1377 } 1378 1379 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 1380 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1381 cport_mutex); 1382 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) { 1383 /* 1384 * Cannot process ioctl request now. Come back later. 1385 */ 1386 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1387 cport_mutex); 1388 ndi_dc_freehdl(dcp); 1389 return (EBUSY); 1390 } 1391 /* Block event processing for this port */ 1392 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 1393 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1394 1395 sata_device.satadev_addr.cport = cport; 1396 sata_device.satadev_addr.pmport = pmport; 1397 sata_device.satadev_addr.qual = qual; 1398 sata_device.satadev_rev = SATA_DEVICE_REV; 1399 } 1400 1401 switch (cmd) { 1402 1403 case DEVCTL_AP_DISCONNECT: 1404 1405 /* 1406 * Normally, cfgadm sata plugin will try to offline 1407 * (unconfigure) device before this request. Nevertheless, 1408 * if a device is still configured, we need to 1409 * attempt to offline and unconfigure device first, and we will 1410 * deactivate the port regardless of the unconfigure 1411 * operation results. 1412 * 1413 */ 1414 rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device); 1415 1416 break; 1417 1418 case DEVCTL_AP_UNCONFIGURE: 1419 1420 /* 1421 * The unconfigure operation uses generic nexus operation to 1422 * offline a device. It leaves a target device node attached. 1423 * and obviously sata_drive_info attached as well, because 1424 * from the hardware point of view nothing has changed. 1425 */ 1426 rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device); 1427 break; 1428 1429 case DEVCTL_AP_CONNECT: 1430 { 1431 /* 1432 * The sata cfgadm pluging will invoke this operation only if 1433 * port was found in the disconnect state (failed state 1434 * is also treated as the disconnected state). 1435 * If port activation is successful and a device is found 1436 * attached to the port, the initialization sequence is 1437 * executed to probe the port and attach 1438 * a device structure to a port structure. The device is not 1439 * set in configured state (system-wise) by this operation. 1440 */ 1441 1442 rv = sata_ioctl_connect(sata_hba_inst, &sata_device); 1443 1444 break; 1445 } 1446 1447 case DEVCTL_AP_CONFIGURE: 1448 { 1449 /* 1450 * A port may be in an active or shutdown state. 1451 * If port is in a failed state, operation is aborted. 1452 * If a port is in a shutdown state, sata_tran_port_activate() 1453 * is invoked prior to any other operation. 1454 * 1455 * Onlining the device involves creating a new target node. 1456 * If there is an old target node present (belonging to 1457 * previously removed device), the operation is aborted - the 1458 * old node has to be released and removed before configure 1459 * operation is attempted. 1460 */ 1461 1462 rv = sata_ioctl_configure(sata_hba_inst, &sata_device); 1463 1464 break; 1465 } 1466 1467 case DEVCTL_AP_GETSTATE: 1468 1469 sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state); 1470 1471 ap_state.ap_last_change = (time_t)-1; 1472 ap_state.ap_error_code = 0; 1473 ap_state.ap_in_transition = 0; 1474 1475 /* Copy the return AP-state information to the user space */ 1476 if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) { 1477 rv = EFAULT; 1478 } 1479 break; 1480 1481 case DEVCTL_AP_CONTROL: 1482 { 1483 /* 1484 * Generic devctl for hardware specific functionality 1485 */ 1486 sata_ioctl_data_t ioc; 1487 1488 ASSERT(dcp == NULL); 1489 1490 /* Copy in user ioctl data first */ 1491 #ifdef _MULTI_DATAMODEL 1492 if (ddi_model_convert_from(mode & FMODELS) == 1493 DDI_MODEL_ILP32) { 1494 1495 sata_ioctl_data_32_t ioc32; 1496 1497 if (ddi_copyin((void *)arg, (void *)&ioc32, 1498 sizeof (ioc32), mode) != 0) { 1499 rv = EFAULT; 1500 break; 1501 } 1502 ioc.cmd = (uint_t)ioc32.cmd; 1503 ioc.port = (uint_t)ioc32.port; 1504 ioc.get_size = (uint_t)ioc32.get_size; 1505 ioc.buf = (caddr_t)(uintptr_t)ioc32.buf; 1506 ioc.bufsiz = (uint_t)ioc32.bufsiz; 1507 ioc.misc_arg = (uint_t)ioc32.misc_arg; 1508 } else 1509 #endif /* _MULTI_DATAMODEL */ 1510 if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc), 1511 mode) != 0) { 1512 return (EFAULT); 1513 } 1514 1515 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 1516 "sata_hba_ioctl: DEVCTL_AP_CONTROL " 1517 "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port); 1518 1519 /* 1520 * To avoid BE/LE and 32/64 issues, a get_size always returns 1521 * a 32-bit number. 1522 */ 1523 if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) { 1524 return (EINVAL); 1525 } 1526 /* validate address */ 1527 cport = SCSI_TO_SATA_CPORT(ioc.port); 1528 pmport = SCSI_TO_SATA_PMPORT(ioc.port); 1529 qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port); 1530 1531 SATADBG3(SATA_DBG_IOCTL_IF, sata_hba_inst, 1532 "sata_hba_ioctl: target port is %d:%d (%d)", 1533 cport, pmport, qual); 1534 1535 if (sata_validate_sata_address(sata_hba_inst, cport, 1536 pmport, qual) != 0) 1537 return (EINVAL); 1538 1539 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 1540 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1541 cport_mutex); 1542 /* Is the port locked by event processing daemon ? */ 1543 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) { 1544 /* 1545 * Cannot process ioctl request now. Come back later 1546 */ 1547 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1548 cport_mutex); 1549 return (EBUSY); 1550 } 1551 /* Block event processing for this port */ 1552 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 1553 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1554 1555 1556 sata_device.satadev_addr.cport = cport; 1557 sata_device.satadev_addr.pmport = pmport; 1558 sata_device.satadev_addr.qual = qual; 1559 sata_device.satadev_rev = SATA_DEVICE_REV; 1560 1561 switch (ioc.cmd) { 1562 1563 case SATA_CFGA_RESET_PORT: 1564 /* 1565 * There is no protection for configured device. 1566 */ 1567 rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device); 1568 break; 1569 1570 case SATA_CFGA_RESET_DEVICE: 1571 /* 1572 * There is no protection for configured device. 1573 */ 1574 rv = sata_ioctl_reset_device(sata_hba_inst, 1575 &sata_device); 1576 break; 1577 1578 case SATA_CFGA_RESET_ALL: 1579 /* 1580 * There is no protection for configured devices. 1581 */ 1582 rv = sata_ioctl_reset_all(sata_hba_inst); 1583 /* 1584 * We return here, because common return is for 1585 * a single port operation - we have already unlocked 1586 * all ports and no dc handle was allocated. 1587 */ 1588 return (rv); 1589 1590 case SATA_CFGA_PORT_DEACTIVATE: 1591 /* 1592 * Arbitrarily unconfigure attached device, if any. 1593 * Even if the unconfigure fails, proceed with the 1594 * port deactivation. 1595 */ 1596 rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device); 1597 1598 break; 1599 1600 case SATA_CFGA_PORT_ACTIVATE: 1601 1602 rv = sata_ioctl_activate(sata_hba_inst, &sata_device); 1603 break; 1604 1605 case SATA_CFGA_PORT_SELF_TEST: 1606 1607 rv = sata_ioctl_port_self_test(sata_hba_inst, 1608 &sata_device); 1609 break; 1610 1611 case SATA_CFGA_GET_DEVICE_PATH: 1612 1613 rv = sata_ioctl_get_device_path(sata_hba_inst, 1614 &sata_device, &ioc, mode); 1615 break; 1616 1617 case SATA_CFGA_GET_AP_TYPE: 1618 1619 rv = sata_ioctl_get_ap_type(sata_hba_inst, 1620 &sata_device, &ioc, mode); 1621 break; 1622 1623 case SATA_CFGA_GET_MODEL_INFO: 1624 1625 rv = sata_ioctl_get_model_info(sata_hba_inst, 1626 &sata_device, &ioc, mode); 1627 break; 1628 1629 case SATA_CFGA_GET_REVFIRMWARE_INFO: 1630 1631 rv = sata_ioctl_get_revfirmware_info(sata_hba_inst, 1632 &sata_device, &ioc, mode); 1633 break; 1634 1635 case SATA_CFGA_GET_SERIALNUMBER_INFO: 1636 1637 rv = sata_ioctl_get_serialnumber_info(sata_hba_inst, 1638 &sata_device, &ioc, mode); 1639 break; 1640 1641 default: 1642 rv = EINVAL; 1643 break; 1644 1645 } /* End of DEVCTL_AP_CONTROL cmd switch */ 1646 1647 break; 1648 } 1649 1650 default: 1651 { 1652 /* 1653 * If we got here, we got an IOCTL that SATA HBA Framework 1654 * does not recognize. Pass ioctl to HBA driver, in case 1655 * it could process it. 1656 */ 1657 sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran; 1658 dev_info_t *mydip = SATA_DIP(sata_hba_inst); 1659 1660 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 1661 "IOCTL 0x%2x not supported in SATA framework, " 1662 "passthrough to HBA", cmd); 1663 1664 if (sata_tran->sata_tran_ioctl == NULL) { 1665 rv = EINVAL; 1666 break; 1667 } 1668 rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg); 1669 if (rval != 0) { 1670 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 1671 "IOCTL 0x%2x failed in HBA", cmd); 1672 rv = rval; 1673 } 1674 break; 1675 } 1676 1677 } /* End of main IOCTL switch */ 1678 1679 if (dcp) { 1680 ndi_dc_freehdl(dcp); 1681 } 1682 1683 if (cmd >= DEVCTL_IOC && cmd <= DEVCTL_IOC_MAX) { 1684 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 1685 cport)->cport_mutex); 1686 cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY; 1687 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1688 } 1689 1690 return (rv); 1691 } 1692 1693 1694 /* 1695 * Create error retrieval sata packet 1696 * 1697 * A sata packet is allocated and set-up to contain specified error retrieval 1698 * command and appropriate dma-able data buffer. 1699 * No association with any scsi packet is made and no callback routine is 1700 * specified. 1701 * 1702 * Returns a pointer to sata packet upon successful packet creation. 1703 * Returns NULL, if packet cannot be created. 1704 */ 1705 sata_pkt_t * 1706 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device, 1707 int pkt_type) 1708 { 1709 sata_hba_inst_t *sata_hba_inst; 1710 sata_pkt_txlate_t *spx; 1711 sata_pkt_t *spkt; 1712 sata_drive_info_t *sdinfo; 1713 1714 mutex_enter(&sata_mutex); 1715 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 1716 sata_hba_inst = sata_hba_inst->satahba_next) { 1717 if (SATA_DIP(sata_hba_inst) == dip) 1718 break; 1719 } 1720 mutex_exit(&sata_mutex); 1721 ASSERT(sata_hba_inst != NULL); 1722 1723 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 1724 if (sdinfo == NULL) { 1725 sata_log(sata_hba_inst, CE_WARN, 1726 "sata: error recovery request for non-attached device at " 1727 "cport %d", sata_device->satadev_addr.cport); 1728 return (NULL); 1729 } 1730 1731 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 1732 spx->txlt_sata_hba_inst = sata_hba_inst; 1733 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 1734 spkt = sata_pkt_alloc(spx, NULL); 1735 if (spkt == NULL) { 1736 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1737 return (NULL); 1738 } 1739 /* address is needed now */ 1740 spkt->satapkt_device.satadev_addr = sata_device->satadev_addr; 1741 1742 switch (pkt_type) { 1743 case SATA_ERR_RETR_PKT_TYPE_NCQ: 1744 if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) { 1745 if (sata_check_for_dma_error(dip, spx)) { 1746 ddi_fm_service_impact(dip, 1747 DDI_SERVICE_UNAFFECTED); 1748 break; 1749 } 1750 return (spkt); 1751 } 1752 break; 1753 1754 case SATA_ERR_RETR_PKT_TYPE_ATAPI: 1755 if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) { 1756 if (sata_check_for_dma_error(dip, spx)) { 1757 ddi_fm_service_impact(dip, 1758 DDI_SERVICE_UNAFFECTED); 1759 break; 1760 } 1761 return (spkt); 1762 } 1763 break; 1764 1765 default: 1766 break; 1767 } 1768 1769 sata_pkt_free(spx); 1770 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1771 return (NULL); 1772 1773 } 1774 1775 1776 /* 1777 * Free error retrieval sata packet 1778 * 1779 * Free sata packet and any associated resources allocated previously by 1780 * sata_get_error_retrieval_pkt(). 1781 * 1782 * Void return. 1783 */ 1784 void 1785 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt) 1786 { 1787 sata_pkt_txlate_t *spx = 1788 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 1789 1790 ASSERT(sata_pkt != NULL); 1791 1792 sata_free_local_buffer(spx); 1793 sata_pkt_free(spx); 1794 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1795 1796 } 1797 1798 /* 1799 * Create READ PORT MULTIPLIER and WRITE PORT MULTIPLIER sata packet 1800 * 1801 * No association with any scsi packet is made and no callback routine is 1802 * specified. 1803 * 1804 * Returns a pointer to sata packet upon successful packet creation. 1805 * Returns NULL, if packet cannot be created. 1806 * 1807 * NOTE: Input/Output value includes 64 bits accoring to SATA Spec 2.6, 1808 * only lower 32 bits are available currently. 1809 */ 1810 sata_pkt_t * 1811 sata_get_rdwr_pmult_pkt(dev_info_t *dip, sata_device_t *sd, 1812 uint8_t regn, uint32_t regv, uint32_t type) 1813 { 1814 sata_hba_inst_t *sata_hba_inst; 1815 sata_pkt_txlate_t *spx; 1816 sata_pkt_t *spkt; 1817 sata_cmd_t *scmd; 1818 1819 /* Only READ/WRITE commands are accepted. */ 1820 ASSERT(type == SATA_RDWR_PMULT_PKT_TYPE_READ || 1821 type == SATA_RDWR_PMULT_PKT_TYPE_WRITE); 1822 1823 mutex_enter(&sata_mutex); 1824 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 1825 sata_hba_inst = sata_hba_inst->satahba_next) { 1826 if (SATA_DIP(sata_hba_inst) == dip) 1827 break; 1828 } 1829 mutex_exit(&sata_mutex); 1830 ASSERT(sata_hba_inst != NULL); 1831 1832 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 1833 spx->txlt_sata_hba_inst = sata_hba_inst; 1834 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 1835 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 1836 if (spkt == NULL) { 1837 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1838 return (NULL); 1839 } 1840 1841 /* 1842 * NOTE: We need to send this command to the port multiplier, 1843 * that means send to SATA_PMULT_HOSTPORT(0xf) pmport 1844 * 1845 * sata_device contains the address of actual target device, and the 1846 * pmport number in the command comes from the sata_device structure. 1847 */ 1848 spkt->satapkt_device.satadev_addr = sd->satadev_addr; 1849 spkt->satapkt_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT; 1850 spkt->satapkt_device.satadev_addr.qual = SATA_ADDR_PMULT; 1851 1852 /* Fill sata_pkt */ 1853 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING; 1854 spkt->satapkt_comp = NULL; /* Synchronous mode, no callback */ 1855 spkt->satapkt_time = 10; /* Timeout 10s */ 1856 1857 /* Build READ PORT MULTIPLIER cmd in the sata_pkt */ 1858 scmd = &spkt->satapkt_cmd; 1859 scmd->satacmd_features_reg = regn & 0xff; 1860 scmd->satacmd_features_reg_ext = (regn >> 8) & 0xff; 1861 scmd->satacmd_device_reg = sd->satadev_addr.pmport; 1862 scmd->satacmd_addr_type = 0; /* N/A */ 1863 1864 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 1865 1866 if (type == SATA_RDWR_PMULT_PKT_TYPE_READ) { 1867 scmd->satacmd_cmd_reg = SATAC_READ_PORTMULT; 1868 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 1869 scmd->satacmd_flags.sata_special_regs = 1; 1870 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1; 1871 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1; 1872 scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1; 1873 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1; 1874 } else if (type == SATA_RDWR_PMULT_PKT_TYPE_WRITE) { 1875 scmd->satacmd_cmd_reg = SATAC_WRITE_PORTMULT; 1876 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 1877 scmd->satacmd_sec_count_lsb = regv & 0xff; 1878 scmd->satacmd_lba_low_lsb = regv >> 8 & 0xff; 1879 scmd->satacmd_lba_mid_lsb = regv >> 16 & 0xff; 1880 scmd->satacmd_lba_high_lsb = regv >> 24 & 0xff; 1881 } 1882 1883 return (spkt); 1884 } 1885 1886 /* 1887 * Free sata packet and any associated resources allocated previously by 1888 * sata_get_rdwr_pmult_pkt(). 1889 * 1890 * Void return. 1891 */ 1892 void 1893 sata_free_rdwr_pmult_pkt(sata_pkt_t *sata_pkt) 1894 { 1895 sata_pkt_txlate_t *spx = 1896 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 1897 1898 /* Free allocated resources */ 1899 sata_pkt_free(spx); 1900 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1901 } 1902 1903 /* 1904 * Register a port multiplier to framework. 1905 * 1) Store the GSCR values in the previous allocated pmult_info strctures. 1906 * 2) Search in the blacklist and update the number of the device ports of the 1907 * port multiplier. 1908 * 1909 * Void return. 1910 */ 1911 void 1912 sata_register_pmult(dev_info_t *dip, sata_device_t *sd, sata_pmult_gscr_t *sg) 1913 { 1914 sata_hba_inst_t *sata_hba_inst = NULL; 1915 sata_pmult_info_t *pmultinfo; 1916 sata_pmult_bl_t *blp; 1917 int cport = sd->satadev_addr.cport; 1918 1919 mutex_enter(&sata_mutex); 1920 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 1921 sata_hba_inst = sata_hba_inst->satahba_next) { 1922 if (SATA_DIP(sata_hba_inst) == dip) 1923 if (sata_hba_inst->satahba_attached == 1) 1924 break; 1925 } 1926 mutex_exit(&sata_mutex); 1927 /* HBA not attached? */ 1928 if (sata_hba_inst == NULL) 1929 return; 1930 1931 /* Number of pmports */ 1932 sd->satadev_add_info = sg->gscr2 & SATA_PMULT_PORTNUM_MASK; 1933 1934 /* Check the blacklist */ 1935 for (blp = sata_pmult_blacklist; blp->bl_gscr0; blp++) { 1936 if (sg->gscr0 != blp->bl_gscr0 && blp->bl_gscr0) 1937 continue; 1938 if (sg->gscr1 != blp->bl_gscr1 && blp->bl_gscr1) 1939 continue; 1940 if (sg->gscr2 != blp->bl_gscr2 && blp->bl_gscr2) 1941 continue; 1942 1943 cmn_err(CE_WARN, "!Port multiplier is on the blacklist."); 1944 sd->satadev_add_info = blp->bl_flags; 1945 break; 1946 } 1947 1948 /* Register the port multiplier GSCR */ 1949 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 1950 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 1951 if (pmultinfo != NULL) { 1952 pmultinfo->pmult_gscr = *sg; 1953 pmultinfo->pmult_num_dev_ports = 1954 sd->satadev_add_info & SATA_PMULT_PORTNUM_MASK; 1955 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 1956 "Port multiplier registered at port %d", cport); 1957 } 1958 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 1959 } 1960 1961 /* 1962 * sata_split_model splits the model ID into vendor and product IDs. 1963 * It assumes that a vendor ID cannot be longer than 8 characters, and 1964 * that vendor and product ID are separated by a whitespace. 1965 */ 1966 void 1967 sata_split_model(char *model, char **vendor, char **product) 1968 { 1969 int i, modlen; 1970 char *vid, *pid; 1971 1972 /* 1973 * remove whitespace at the end of model 1974 */ 1975 for (i = SATA_ID_MODEL_LEN; i > 0; i--) 1976 if (model[i] == ' ' || model[i] == '\t' || model[i] == '\0') 1977 model[i] = '\0'; 1978 else 1979 break; 1980 1981 /* 1982 * try to split model into into vid/pid 1983 */ 1984 modlen = strlen(model); 1985 for (i = 0, pid = model; i < modlen; i++, pid++) 1986 if ((*pid == ' ') || (*pid == '\t')) 1987 break; 1988 1989 /* 1990 * only use vid if it is less than 8 chars (as in SCSI) 1991 */ 1992 if (i < modlen && i <= 8) { 1993 vid = model; 1994 /* 1995 * terminate vid, establish pid 1996 */ 1997 *pid++ = '\0'; 1998 } else { 1999 /* 2000 * vid will stay "ATA " 2001 */ 2002 vid = NULL; 2003 /* 2004 * model is all pid 2005 */ 2006 pid = model; 2007 } 2008 2009 *vendor = vid; 2010 *product = pid; 2011 } 2012 2013 /* 2014 * sata_name_child is for composing the name of the node 2015 * the format of the name is "target,0". 2016 */ 2017 static int 2018 sata_name_child(dev_info_t *dip, char *name, int namelen) 2019 { 2020 int target; 2021 2022 target = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 2023 DDI_PROP_DONTPASS, "target", -1); 2024 if (target == -1) 2025 return (DDI_FAILURE); 2026 (void) snprintf(name, namelen, "%x,0", target); 2027 return (DDI_SUCCESS); 2028 } 2029 2030 2031 2032 /* ****************** SCSA required entry points *********************** */ 2033 2034 /* 2035 * Implementation of scsi tran_tgt_init. 2036 * sata_scsi_tgt_init() initializes scsi_device structure 2037 * 2038 * If successful, DDI_SUCCESS is returned. 2039 * DDI_FAILURE is returned if addressed device does not exist 2040 */ 2041 2042 static int 2043 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip, 2044 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 2045 { 2046 #ifndef __lock_lint 2047 _NOTE(ARGUNUSED(hba_dip)) 2048 _NOTE(ARGUNUSED(tgt_dip)) 2049 #endif 2050 sata_device_t sata_device; 2051 sata_drive_info_t *sdinfo; 2052 struct sata_id *sid; 2053 sata_hba_inst_t *sata_hba_inst; 2054 char model[SATA_ID_MODEL_LEN + 1]; 2055 char fw[SATA_ID_FW_LEN + 1]; 2056 char *vid, *pid; 2057 2058 /* 2059 * Fail tran_tgt_init for .conf stub node 2060 */ 2061 if (ndi_dev_is_persistent_node(tgt_dip) == 0) { 2062 (void) ndi_merge_node(tgt_dip, sata_name_child); 2063 ddi_set_name_addr(tgt_dip, NULL); 2064 return (DDI_FAILURE); 2065 } 2066 2067 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 2068 2069 /* Validate scsi device address */ 2070 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 2071 &sata_device) != 0) 2072 return (DDI_FAILURE); 2073 2074 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2075 sata_device.satadev_addr.cport))); 2076 2077 /* sata_device now contains a valid sata address */ 2078 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 2079 if (sdinfo == NULL) { 2080 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2081 sata_device.satadev_addr.cport))); 2082 return (DDI_FAILURE); 2083 } 2084 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2085 sata_device.satadev_addr.cport))); 2086 2087 /* 2088 * Check if we need to create a legacy devid (i.e cmdk style) for 2089 * the target disks. 2090 * 2091 * HBA devinfo node will have the property "use-cmdk-devid-format" 2092 * if we need to create cmdk-style devid for all the disk devices 2093 * attached to this controller. This property may have been set 2094 * from HBA driver's .conf file or by the HBA driver in its 2095 * attach(9F) function. 2096 */ 2097 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 2098 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS, 2099 "use-cmdk-devid-format", 0) == 1)) { 2100 /* register a legacy devid for this target node */ 2101 sata_target_devid_register(tgt_dip, sdinfo); 2102 } 2103 2104 2105 /* 2106 * 'Identify Device Data' does not always fit in standard SCSI 2107 * INQUIRY data, so establish INQUIRY_* properties with full-form 2108 * of information. 2109 */ 2110 sid = &sdinfo->satadrv_id; 2111 #ifdef _LITTLE_ENDIAN 2112 swab(sid->ai_model, model, SATA_ID_MODEL_LEN); 2113 swab(sid->ai_fw, fw, SATA_ID_FW_LEN); 2114 #else /* _LITTLE_ENDIAN */ 2115 bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN); 2116 bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN); 2117 #endif /* _LITTLE_ENDIAN */ 2118 model[SATA_ID_MODEL_LEN] = 0; 2119 fw[SATA_ID_FW_LEN] = 0; 2120 2121 sata_split_model(model, &vid, &pid); 2122 2123 if (vid) 2124 (void) scsi_device_prop_update_inqstring(sd, INQUIRY_VENDOR_ID, 2125 vid, strlen(vid)); 2126 if (pid) 2127 (void) scsi_device_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID, 2128 pid, strlen(pid)); 2129 (void) scsi_device_prop_update_inqstring(sd, INQUIRY_REVISION_ID, 2130 fw, strlen(fw)); 2131 2132 return (DDI_SUCCESS); 2133 } 2134 2135 /* 2136 * Implementation of scsi tran_tgt_probe. 2137 * Probe target, by calling default scsi routine scsi_hba_probe() 2138 */ 2139 static int 2140 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void)) 2141 { 2142 sata_hba_inst_t *sata_hba_inst = 2143 (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private); 2144 int rval; 2145 uint32_t pm_cap; 2146 2147 rval = scsi_hba_probe(sd, callback); 2148 pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE | 2149 SATA_CAP_LOG_SENSE; 2150 2151 if (rval == SCSIPROBE_EXISTS) { 2152 /* 2153 * Set property "pm-capable" on the target device node, so that 2154 * the target driver will not try to fetch scsi cycle counters 2155 * before enabling device power-management. 2156 */ 2157 if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev, 2158 "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) { 2159 sata_log(sata_hba_inst, CE_WARN, 2160 "SATA device at port %d: " 2161 "will not be power-managed ", 2162 SCSI_TO_SATA_CPORT(sd->sd_address.a_target)); 2163 SATA_LOG_D((sata_hba_inst, CE_WARN, 2164 "failure updating pm-capable property")); 2165 } 2166 } 2167 return (rval); 2168 } 2169 2170 /* 2171 * Implementation of scsi tran_tgt_free. 2172 * Release all resources allocated for scsi_device 2173 */ 2174 static void 2175 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip, 2176 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 2177 { 2178 #ifndef __lock_lint 2179 _NOTE(ARGUNUSED(hba_dip)) 2180 #endif 2181 sata_device_t sata_device; 2182 sata_drive_info_t *sdinfo; 2183 sata_hba_inst_t *sata_hba_inst; 2184 ddi_devid_t devid; 2185 2186 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 2187 2188 /* Validate scsi device address */ 2189 /* 2190 * Note: tgt_free relates to the SCSA view of a device. If called, there 2191 * was a device at this address, so even if the sata framework internal 2192 * resources were alredy released because a device was detached, 2193 * this function should be executed as long as its actions do 2194 * not require the internal sata view of a device and the address 2195 * refers to a valid sata address. 2196 * Validating the address here means that we do not trust SCSA... 2197 */ 2198 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 2199 &sata_device) == -1) 2200 return; 2201 2202 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2203 sata_device.satadev_addr.cport))); 2204 2205 /* sata_device now should contain a valid sata address */ 2206 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 2207 if (sdinfo == NULL) { 2208 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2209 sata_device.satadev_addr.cport))); 2210 return; 2211 } 2212 /* 2213 * We did not allocate any resources in sata_scsi_tgt_init() 2214 * other than few properties. 2215 * Free them. 2216 */ 2217 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2218 sata_device.satadev_addr.cport))); 2219 (void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable"); 2220 2221 /* 2222 * If devid was previously created but not freed up from 2223 * sd(7D) driver (i.e during detach(9F)) then do it here. 2224 */ 2225 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 2226 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS, 2227 "use-cmdk-devid-format", 0) == 1) && 2228 (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) { 2229 ddi_devid_unregister(tgt_dip); 2230 ddi_devid_free(devid); 2231 } 2232 } 2233 2234 /* 2235 * Implementation of scsi tran_init_pkt 2236 * Upon successful return, scsi pkt buffer has DMA resources allocated. 2237 * 2238 * It seems that we should always allocate pkt, even if the address is 2239 * for non-existing device - just use some default for dma_attr. 2240 * The reason is that there is no way to communicate this to a caller here. 2241 * Subsequent call to sata_scsi_start may fail appropriately. 2242 * Simply returning NULL does not seem to discourage a target driver... 2243 * 2244 * Returns a pointer to initialized scsi_pkt, or NULL otherwise. 2245 */ 2246 static struct scsi_pkt * 2247 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt, 2248 struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags, 2249 int (*callback)(caddr_t), caddr_t arg) 2250 { 2251 sata_hba_inst_t *sata_hba_inst = 2252 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2253 dev_info_t *dip = SATA_DIP(sata_hba_inst); 2254 sata_device_t sata_device; 2255 sata_drive_info_t *sdinfo; 2256 sata_pkt_txlate_t *spx; 2257 ddi_dma_attr_t cur_dma_attr; 2258 int rval; 2259 boolean_t new_pkt = B_TRUE; 2260 2261 ASSERT(ap->a_hba_tran->tran_hba_dip == dip); 2262 2263 /* 2264 * We need to translate the address, even if it could be 2265 * a bogus one, for a non-existing device 2266 */ 2267 sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 2268 sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target); 2269 sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 2270 sata_device.satadev_rev = SATA_DEVICE_REV; 2271 2272 if (pkt == NULL) { 2273 /* 2274 * Have to allocate a brand new scsi packet. 2275 * We need to operate with auto request sense enabled. 2276 */ 2277 pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen, 2278 MAX(statuslen, SATA_MAX_SENSE_LEN), 2279 tgtlen, sizeof (sata_pkt_txlate_t), callback, arg); 2280 2281 if (pkt == NULL) 2282 return (NULL); 2283 2284 /* Fill scsi packet structure */ 2285 pkt->pkt_comp = (void (*)())NULL; 2286 pkt->pkt_time = 0; 2287 pkt->pkt_resid = 0; 2288 pkt->pkt_statistics = 0; 2289 pkt->pkt_reason = 0; 2290 2291 /* 2292 * pkt_hba_private will point to sata pkt txlate structure 2293 */ 2294 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2295 bzero(spx, sizeof (sata_pkt_txlate_t)); 2296 2297 spx->txlt_scsi_pkt = pkt; 2298 spx->txlt_sata_hba_inst = sata_hba_inst; 2299 2300 /* Allocate sata_pkt */ 2301 spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback); 2302 if (spx->txlt_sata_pkt == NULL) { 2303 /* Could not allocate sata pkt */ 2304 scsi_hba_pkt_free(ap, pkt); 2305 return (NULL); 2306 } 2307 /* Set sata address */ 2308 spx->txlt_sata_pkt->satapkt_device.satadev_addr = 2309 sata_device.satadev_addr; 2310 spx->txlt_sata_pkt->satapkt_device.satadev_rev = 2311 sata_device.satadev_rev; 2312 2313 if ((bp == NULL) || (bp->b_bcount == 0)) 2314 return (pkt); 2315 2316 spx->txlt_total_residue = bp->b_bcount; 2317 } else { 2318 new_pkt = B_FALSE; 2319 /* 2320 * Packet was preallocated/initialized by previous call 2321 */ 2322 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2323 2324 if ((bp == NULL) || (bp->b_bcount == 0)) { 2325 return (pkt); 2326 } 2327 2328 /* Pkt is available already: spx->txlt_scsi_pkt == pkt; */ 2329 } 2330 2331 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 2332 2333 /* 2334 * We use an adjusted version of the dma_attr, to account 2335 * for device addressing limitations. 2336 * sata_adjust_dma_attr() will handle sdinfo == NULL which may 2337 * happen when a device is not yet configured. 2338 */ 2339 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2340 sata_device.satadev_addr.cport))); 2341 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 2342 &spx->txlt_sata_pkt->satapkt_device); 2343 /* NULL sdinfo may be passsed to sata_adjust_dma_attr() */ 2344 sata_adjust_dma_attr(sdinfo, 2345 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 2346 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2347 sata_device.satadev_addr.cport))); 2348 /* 2349 * Allocate necessary DMA resources for the packet's data buffer 2350 * NOTE: 2351 * In case of read/write commands, DMA resource allocation here is 2352 * based on the premise that the transfer length specified in 2353 * the read/write scsi cdb will match exactly DMA resources - 2354 * returning correct packet residue is crucial. 2355 */ 2356 if ((rval = sata_dma_buf_setup(spx, flags, callback, arg, 2357 &cur_dma_attr)) != DDI_SUCCESS) { 2358 /* 2359 * If a DMA allocation request fails with 2360 * DDI_DMA_NOMAPPING, indicate the error by calling 2361 * bioerror(9F) with bp and an error code of EFAULT. 2362 * If a DMA allocation request fails with 2363 * DDI_DMA_TOOBIG, indicate the error by calling 2364 * bioerror(9F) with bp and an error code of EINVAL. 2365 * For DDI_DMA_NORESOURCES, we may have some of them allocated. 2366 * Request may be repeated later - there is no real error. 2367 */ 2368 switch (rval) { 2369 case DDI_DMA_NORESOURCES: 2370 bioerror(bp, 0); 2371 break; 2372 case DDI_DMA_NOMAPPING: 2373 case DDI_DMA_BADATTR: 2374 bioerror(bp, EFAULT); 2375 break; 2376 case DDI_DMA_TOOBIG: 2377 default: 2378 bioerror(bp, EINVAL); 2379 break; 2380 } 2381 goto fail; 2382 } 2383 2384 if (sata_check_for_dma_error(dip, spx)) { 2385 ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED); 2386 bioerror(bp, EFAULT); 2387 goto fail; 2388 } 2389 2390 success: 2391 /* Set number of bytes that are not yet accounted for */ 2392 pkt->pkt_resid = spx->txlt_total_residue; 2393 ASSERT(pkt->pkt_resid >= 0); 2394 2395 return (pkt); 2396 2397 fail: 2398 if (new_pkt == B_TRUE) { 2399 /* 2400 * Since this is a new packet, we can clean-up 2401 * everything 2402 */ 2403 sata_scsi_destroy_pkt(ap, pkt); 2404 } else { 2405 /* 2406 * This is a re-used packet. It will be target driver's 2407 * responsibility to eventually destroy it (which 2408 * will free allocated resources). 2409 * Here, we just "complete" the request, leaving 2410 * allocated resources intact, so the request may 2411 * be retried. 2412 */ 2413 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 2414 sata_pkt_free(spx); 2415 } 2416 return (NULL); 2417 } 2418 2419 /* 2420 * Implementation of scsi tran_start. 2421 * Translate scsi cmd into sata operation and return status. 2422 * ATAPI CDBs are passed to ATAPI devices - the device determines what commands 2423 * are supported. 2424 * For SATA hard disks, supported scsi commands: 2425 * SCMD_INQUIRY 2426 * SCMD_TEST_UNIT_READY 2427 * SCMD_START_STOP 2428 * SCMD_READ_CAPACITY 2429 * SCMD_SVC_ACTION_IN_G4 (READ CAPACITY (16)) 2430 * SCMD_REQUEST_SENSE 2431 * SCMD_LOG_SENSE_G1 2432 * SCMD_LOG_SELECT_G1 2433 * SCMD_MODE_SENSE (specific pages) 2434 * SCMD_MODE_SENSE_G1 (specific pages) 2435 * SCMD_MODE_SELECT (specific pages) 2436 * SCMD_MODE_SELECT_G1 (specific pages) 2437 * SCMD_SYNCHRONIZE_CACHE 2438 * SCMD_SYNCHRONIZE_CACHE_G1 2439 * SCMD_READ 2440 * SCMD_READ_G1 2441 * SCMD_READ_G4 2442 * SCMD_READ_G5 2443 * SCMD_WRITE 2444 * SCMD_WRITE_BUFFER 2445 * SCMD_WRITE_G1 2446 * SCMD_WRITE_G4 2447 * SCMD_WRITE_G5 2448 * SCMD_SEEK (noop) 2449 * SCMD_SDIAG 2450 * 2451 * All other commands are rejected as unsupported. 2452 * 2453 * Returns: 2454 * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver 2455 * for execution. TRAN_ACCEPT may be returned also if device was removed but 2456 * a callback could be scheduled. 2457 * TRAN_BADPKT if cmd was directed to invalid address. 2458 * TRAN_FATAL_ERROR is command was rejected due to hardware error, including 2459 * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device 2460 * was removed and there was no callback specified in scsi pkt. 2461 * TRAN_BUSY if command could not be executed becasue HBA driver or SATA 2462 * framework was busy performing some other operation(s). 2463 * 2464 */ 2465 static int 2466 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt) 2467 { 2468 sata_hba_inst_t *sata_hba_inst = 2469 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2470 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2471 sata_device_t *sdevice = &spx->txlt_sata_pkt->satapkt_device; 2472 sata_drive_info_t *sdinfo; 2473 struct buf *bp; 2474 uint8_t cport, pmport; 2475 boolean_t dev_gone = B_FALSE; 2476 int rval; 2477 2478 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2479 "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]); 2480 2481 ASSERT(spx != NULL && 2482 spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL); 2483 2484 cport = SCSI_TO_SATA_CPORT(ap->a_target); 2485 pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 2486 2487 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2488 2489 if (sdevice->satadev_addr.qual == SATA_ADDR_DCPORT) { 2490 sdinfo = sata_get_device_info(sata_hba_inst, sdevice); 2491 if (sdinfo == NULL || 2492 SATA_CPORT_INFO(sata_hba_inst, cport)-> 2493 cport_tgtnode_clean == B_FALSE || 2494 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 2495 dev_gone = B_TRUE; 2496 } 2497 } else if (sdevice->satadev_addr.qual == SATA_ADDR_DPMPORT) { 2498 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 2499 SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst, 2500 cport) == NULL) { 2501 dev_gone = B_TRUE; 2502 } else if (SATA_PMPORT_INFO(sata_hba_inst, cport, 2503 pmport) == NULL) { 2504 dev_gone = B_TRUE; 2505 } else { 2506 mutex_enter(&(SATA_PMPORT_MUTEX(sata_hba_inst, 2507 cport, pmport))); 2508 sdinfo = sata_get_device_info(sata_hba_inst, sdevice); 2509 if (sdinfo == NULL || 2510 SATA_PMPORT_INFO(sata_hba_inst, cport, pmport)-> 2511 pmport_tgtnode_clean == B_FALSE || 2512 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 2513 dev_gone = B_TRUE; 2514 } 2515 mutex_exit(&(SATA_PMPORT_MUTEX(sata_hba_inst, 2516 cport, pmport))); 2517 } 2518 } 2519 2520 if (dev_gone == B_TRUE) { 2521 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2522 pkt->pkt_reason = CMD_DEV_GONE; 2523 /* 2524 * The sd target driver is checking CMD_DEV_GONE pkt_reason 2525 * only in callback function (for normal requests) and 2526 * in the dump code path. 2527 * So, if the callback is available, we need to do 2528 * the callback rather than returning TRAN_FATAL_ERROR here. 2529 */ 2530 if (pkt->pkt_comp != NULL) { 2531 /* scsi callback required */ 2532 if (servicing_interrupt()) { 2533 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 2534 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 2535 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 2536 TASKQID_INVALID) { 2537 return (TRAN_BUSY); 2538 } 2539 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 2540 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 2541 spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 2542 /* Scheduling the callback failed */ 2543 return (TRAN_BUSY); 2544 } 2545 return (TRAN_ACCEPT); 2546 } 2547 /* No callback available */ 2548 return (TRAN_FATAL_ERROR); 2549 } 2550 2551 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 2552 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2553 rval = sata_txlt_atapi(spx); 2554 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2555 "sata_scsi_start atapi: rval %d\n", rval); 2556 return (rval); 2557 } 2558 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2559 2560 /* 2561 * Checking for power state, if it was on 2562 * STOPPED state, then the drive is not capable 2563 * of processing media access command. And 2564 * TEST_UNIT_READY, REQUEST_SENSE has special handling 2565 * in the function for different power state. 2566 */ 2567 if (((sdinfo->satadrv_power_level == SATA_POWER_STANDBY) || 2568 (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)) && 2569 (SATA_IS_MEDIUM_ACCESS_CMD(pkt->pkt_cdbp[0]))) { 2570 return (sata_txlt_check_condition(spx, KEY_NOT_READY, 2571 SD_SCSI_ASC_LU_NOT_READY)); 2572 } 2573 2574 /* ATA Disk commands processing starts here */ 2575 2576 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 2577 2578 switch (pkt->pkt_cdbp[0]) { 2579 2580 case SCMD_INQUIRY: 2581 /* Mapped to identify device */ 2582 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2583 bp_mapin(bp); 2584 rval = sata_txlt_inquiry(spx); 2585 break; 2586 2587 case SCMD_TEST_UNIT_READY: 2588 /* 2589 * SAT "SATA to ATA Translation" doc specifies translation 2590 * to ATA CHECK POWER MODE. 2591 */ 2592 rval = sata_txlt_test_unit_ready(spx); 2593 break; 2594 2595 case SCMD_START_STOP: 2596 /* Mapping depends on the command */ 2597 rval = sata_txlt_start_stop_unit(spx); 2598 break; 2599 2600 case SCMD_READ_CAPACITY: 2601 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2602 bp_mapin(bp); 2603 rval = sata_txlt_read_capacity(spx); 2604 break; 2605 2606 case SCMD_SVC_ACTION_IN_G4: /* READ CAPACITY (16) */ 2607 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2608 bp_mapin(bp); 2609 rval = sata_txlt_read_capacity16(spx); 2610 break; 2611 2612 case SCMD_REQUEST_SENSE: 2613 /* 2614 * Always No Sense, since we force ARQ 2615 */ 2616 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2617 bp_mapin(bp); 2618 rval = sata_txlt_request_sense(spx); 2619 break; 2620 2621 case SCMD_LOG_SENSE_G1: 2622 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2623 bp_mapin(bp); 2624 rval = sata_txlt_log_sense(spx); 2625 break; 2626 2627 case SCMD_LOG_SELECT_G1: 2628 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2629 bp_mapin(bp); 2630 rval = sata_txlt_log_select(spx); 2631 break; 2632 2633 case SCMD_MODE_SENSE: 2634 case SCMD_MODE_SENSE_G1: 2635 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2636 bp_mapin(bp); 2637 rval = sata_txlt_mode_sense(spx); 2638 break; 2639 2640 2641 case SCMD_MODE_SELECT: 2642 case SCMD_MODE_SELECT_G1: 2643 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2644 bp_mapin(bp); 2645 rval = sata_txlt_mode_select(spx); 2646 break; 2647 2648 case SCMD_SYNCHRONIZE_CACHE: 2649 case SCMD_SYNCHRONIZE_CACHE_G1: 2650 rval = sata_txlt_synchronize_cache(spx); 2651 break; 2652 2653 case SCMD_READ: 2654 case SCMD_READ_G1: 2655 case SCMD_READ_G4: 2656 case SCMD_READ_G5: 2657 rval = sata_txlt_read(spx); 2658 break; 2659 case SCMD_WRITE_BUFFER: 2660 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2661 bp_mapin(bp); 2662 rval = sata_txlt_write_buffer(spx); 2663 break; 2664 2665 case SCMD_WRITE: 2666 case SCMD_WRITE_G1: 2667 case SCMD_WRITE_G4: 2668 case SCMD_WRITE_G5: 2669 rval = sata_txlt_write(spx); 2670 break; 2671 2672 case SCMD_SEEK: 2673 rval = sata_txlt_nodata_cmd_immediate(spx); 2674 break; 2675 2676 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12: 2677 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16: 2678 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2679 bp_mapin(bp); 2680 rval = sata_txlt_ata_pass_thru(spx); 2681 break; 2682 2683 /* Other cases will be filed later */ 2684 /* postponed until phase 2 of the development */ 2685 case SPC3_CMD_UNMAP: 2686 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2687 bp_mapin(bp); 2688 rval = sata_txlt_unmap(spx); 2689 break; 2690 default: 2691 rval = sata_txlt_invalid_command(spx); 2692 break; 2693 } 2694 2695 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2696 "sata_scsi_start: rval %d\n", rval); 2697 2698 return (rval); 2699 } 2700 2701 /* 2702 * Implementation of scsi tran_abort. 2703 * Abort specific pkt or all packets. 2704 * 2705 * Returns 1 if one or more packets were aborted, returns 0 otherwise 2706 * 2707 * May be called from an interrupt level. 2708 */ 2709 static int 2710 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt) 2711 { 2712 sata_hba_inst_t *sata_hba_inst = 2713 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2714 sata_device_t sata_device; 2715 sata_pkt_t *sata_pkt; 2716 2717 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2718 "sata_scsi_abort: %s at target: 0x%x\n", 2719 scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target); 2720 2721 /* Validate address */ 2722 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) 2723 /* Invalid address */ 2724 return (0); 2725 2726 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2727 sata_device.satadev_addr.cport))); 2728 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) { 2729 /* invalid address */ 2730 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2731 sata_device.satadev_addr.cport))); 2732 return (0); 2733 } 2734 if (scsi_pkt == NULL) { 2735 /* 2736 * Abort all packets. 2737 * Although we do not have specific packet, we still need 2738 * dummy packet structure to pass device address to HBA. 2739 * Allocate one, without sleeping. Fail if pkt cannot be 2740 * allocated. 2741 */ 2742 sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP); 2743 if (sata_pkt == NULL) { 2744 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2745 sata_device.satadev_addr.cport))); 2746 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: " 2747 "could not allocate sata_pkt")); 2748 return (0); 2749 } 2750 sata_pkt->satapkt_rev = SATA_PKT_REV; 2751 sata_pkt->satapkt_device = sata_device; 2752 sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 2753 } else { 2754 if (scsi_pkt->pkt_ha_private == NULL) { 2755 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2756 sata_device.satadev_addr.cport))); 2757 return (0); /* Bad scsi pkt */ 2758 } 2759 /* extract pointer to sata pkt */ 2760 sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)-> 2761 txlt_sata_pkt; 2762 } 2763 2764 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2765 sata_device.satadev_addr.cport))); 2766 /* Send abort request to HBA */ 2767 if ((*SATA_ABORT_FUNC(sata_hba_inst)) 2768 (SATA_DIP(sata_hba_inst), sata_pkt, 2769 scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) == 2770 SATA_SUCCESS) { 2771 if (scsi_pkt == NULL) 2772 kmem_free(sata_pkt, sizeof (sata_pkt_t)); 2773 /* Success */ 2774 return (1); 2775 } 2776 /* Else, something did not go right */ 2777 if (scsi_pkt == NULL) 2778 kmem_free(sata_pkt, sizeof (sata_pkt_t)); 2779 /* Failure */ 2780 return (0); 2781 } 2782 2783 2784 /* 2785 * Implementation of scsi tran_reset. 2786 * RESET_ALL request is translated into port reset. 2787 * RESET_TARGET requests is translated into a device reset, 2788 * RESET_LUN request is accepted only for LUN 0 and translated into 2789 * device reset. 2790 * The target reset should cause all HBA active and queued packets to 2791 * be terminated and returned with pkt reason SATA_PKT_RESET prior to 2792 * the return. HBA should report reset event for the device. 2793 * 2794 * Returns 1 upon success, 0 upon failure. 2795 */ 2796 static int 2797 sata_scsi_reset(struct scsi_address *ap, int level) 2798 { 2799 sata_hba_inst_t *sata_hba_inst = 2800 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2801 sata_device_t sata_device; 2802 int val; 2803 2804 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2805 "sata_scsi_reset: level %d target: 0x%x\n", 2806 level, ap->a_target); 2807 2808 /* Validate address */ 2809 val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device); 2810 if (val == -1) 2811 /* Invalid address */ 2812 return (0); 2813 2814 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2815 sata_device.satadev_addr.cport))); 2816 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) { 2817 /* invalid address */ 2818 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2819 sata_device.satadev_addr.cport))); 2820 return (0); 2821 } 2822 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2823 sata_device.satadev_addr.cport))); 2824 if (level == RESET_ALL) { 2825 /* port reset */ 2826 if (sata_device.satadev_addr.qual == SATA_ADDR_DCPORT) 2827 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 2828 else 2829 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT; 2830 2831 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 2832 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS) 2833 return (1); 2834 else 2835 return (0); 2836 2837 } else if (val == 0 && 2838 (level == RESET_TARGET || level == RESET_LUN)) { 2839 /* reset device (device attached) */ 2840 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 2841 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS) 2842 return (1); 2843 else 2844 return (0); 2845 } 2846 return (0); 2847 } 2848 2849 2850 /* 2851 * Implementation of scsi tran_getcap (get transport/device capabilities). 2852 * Supported capabilities for SATA hard disks: 2853 * auto-rqsense (always supported) 2854 * tagged-qing (supported if HBA supports it) 2855 * untagged-qing (could be supported if disk supports it, but because 2856 * caching behavior allowing untagged queuing actually 2857 * results in reduced performance. sd tries to throttle 2858 * back to only 3 outstanding commands, which may 2859 * work for real SCSI disks, but with read ahead 2860 * caching, having more than 1 outstanding command 2861 * results in cache thrashing.) 2862 * sector_size 2863 * dma_max 2864 * interconnect-type (INTERCONNECT_SATA) 2865 * 2866 * Supported capabilities for ATAPI CD/DVD devices: 2867 * auto-rqsense (always supported) 2868 * sector_size 2869 * dma_max 2870 * max-cdb-length 2871 * interconnect-type (INTERCONNECT_SATA) 2872 * 2873 * Supported capabilities for ATAPI TAPE devices: 2874 * auto-rqsense (always supported) 2875 * dma_max 2876 * max-cdb-length 2877 * 2878 * Supported capabilities for SATA ATAPI hard disks: 2879 * auto-rqsense (always supported) 2880 * interconnect-type (INTERCONNECT_SATA) 2881 * max-cdb-length 2882 * 2883 * Request for other capabilities is rejected as unsupported. 2884 * 2885 * Returns supported capability value, or -1 if capability is unsuppported or 2886 * the address is invalid - no device. 2887 */ 2888 2889 static int 2890 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom) 2891 { 2892 2893 sata_hba_inst_t *sata_hba_inst = 2894 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2895 sata_device_t sata_device; 2896 sata_drive_info_t *sdinfo; 2897 ddi_dma_attr_t adj_dma_attr; 2898 int rval; 2899 2900 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2901 "sata_scsi_getcap: target: 0x%x, cap: %s\n", 2902 ap->a_target, cap); 2903 2904 /* 2905 * We want to process the capabilities on per port granularity. 2906 * So, we are specifically restricting ourselves to whom != 0 2907 * to exclude the controller wide handling. 2908 */ 2909 if (cap == NULL || whom == 0) 2910 return (-1); 2911 2912 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) { 2913 /* Invalid address */ 2914 return (-1); 2915 } 2916 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2917 sata_device.satadev_addr.cport))); 2918 if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) == 2919 NULL) { 2920 /* invalid address */ 2921 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2922 sata_device.satadev_addr.cport))); 2923 return (-1); 2924 } 2925 2926 switch (scsi_hba_lookup_capstr(cap)) { 2927 case SCSI_CAP_ARQ: 2928 rval = 1; /* ARQ supported, turned on */ 2929 break; 2930 2931 case SCSI_CAP_SECTOR_SIZE: 2932 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) 2933 rval = SATA_DISK_SECTOR_SIZE; /* fixed size */ 2934 else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) 2935 rval = SATA_ATAPI_SECTOR_SIZE; 2936 else rval = -1; 2937 break; 2938 2939 /* 2940 * untagged queuing cause a performance inversion because of 2941 * the way sd operates. Because of this reason we do not 2942 * use it when available. 2943 */ 2944 case SCSI_CAP_UNTAGGED_QING: 2945 if (sdinfo->satadrv_features_enabled & 2946 SATA_DEV_F_E_UNTAGGED_QING) 2947 rval = 1; /* Untagged queuing available */ 2948 else 2949 rval = -1; /* Untagged queuing not available */ 2950 break; 2951 2952 case SCSI_CAP_TAGGED_QING: 2953 if ((sdinfo->satadrv_features_enabled & 2954 SATA_DEV_F_E_TAGGED_QING) && 2955 (sdinfo->satadrv_max_queue_depth > 1)) 2956 rval = 1; /* Tagged queuing available */ 2957 else 2958 rval = -1; /* Tagged queuing not available */ 2959 break; 2960 2961 case SCSI_CAP_DMA_MAX: 2962 sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst), 2963 &adj_dma_attr); 2964 rval = (int)adj_dma_attr.dma_attr_maxxfer; 2965 /* We rely on the fact that dma_attr_maxxfer < 0x80000000 */ 2966 break; 2967 2968 case SCSI_CAP_INTERCONNECT_TYPE: 2969 rval = INTERCONNECT_SATA; /* SATA interconnect type */ 2970 break; 2971 2972 case SCSI_CAP_CDB_LEN: 2973 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) 2974 rval = sdinfo->satadrv_atapi_cdb_len; 2975 else 2976 rval = -1; 2977 break; 2978 2979 default: 2980 rval = -1; 2981 break; 2982 } 2983 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2984 sata_device.satadev_addr.cport))); 2985 return (rval); 2986 } 2987 2988 /* 2989 * Implementation of scsi tran_setcap 2990 * 2991 * Only SCSI_CAP_UNTAGGED_QING and SCSI_CAP_TAGGED_QING are changeable. 2992 * 2993 */ 2994 static int 2995 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom) 2996 { 2997 sata_hba_inst_t *sata_hba_inst = 2998 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2999 sata_device_t sata_device; 3000 sata_drive_info_t *sdinfo; 3001 int rval; 3002 3003 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 3004 "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap); 3005 3006 /* 3007 * We want to process the capabilities on per port granularity. 3008 * So, we are specifically restricting ourselves to whom != 0 3009 * to exclude the controller wide handling. 3010 */ 3011 if (cap == NULL || whom == 0) { 3012 return (-1); 3013 } 3014 3015 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) { 3016 /* Invalid address */ 3017 return (-1); 3018 } 3019 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 3020 sata_device.satadev_addr.cport))); 3021 if ((sdinfo = sata_get_device_info(sata_hba_inst, 3022 &sata_device)) == NULL) { 3023 /* invalid address */ 3024 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3025 sata_device.satadev_addr.cport))); 3026 return (-1); 3027 } 3028 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3029 sata_device.satadev_addr.cport))); 3030 3031 switch (scsi_hba_lookup_capstr(cap)) { 3032 case SCSI_CAP_ARQ: 3033 case SCSI_CAP_SECTOR_SIZE: 3034 case SCSI_CAP_DMA_MAX: 3035 case SCSI_CAP_INTERCONNECT_TYPE: 3036 rval = 0; 3037 break; 3038 case SCSI_CAP_UNTAGGED_QING: 3039 if (SATA_QDEPTH(sata_hba_inst) > 1) { 3040 rval = 1; 3041 if (value == 1) { 3042 sdinfo->satadrv_features_enabled |= 3043 SATA_DEV_F_E_UNTAGGED_QING; 3044 } else if (value == 0) { 3045 sdinfo->satadrv_features_enabled &= 3046 ~SATA_DEV_F_E_UNTAGGED_QING; 3047 } else { 3048 rval = -1; 3049 } 3050 } else { 3051 rval = 0; 3052 } 3053 break; 3054 case SCSI_CAP_TAGGED_QING: 3055 /* This can TCQ or NCQ */ 3056 if (sata_func_enable & SATA_ENABLE_QUEUING && 3057 ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ && 3058 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) || 3059 (sata_func_enable & SATA_ENABLE_NCQ && 3060 sdinfo->satadrv_features_support & SATA_DEV_F_NCQ && 3061 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) && 3062 (sdinfo->satadrv_max_queue_depth > 1)) { 3063 rval = 1; 3064 if (value == 1) { 3065 sdinfo->satadrv_features_enabled |= 3066 SATA_DEV_F_E_TAGGED_QING; 3067 } else if (value == 0) { 3068 sdinfo->satadrv_features_enabled &= 3069 ~SATA_DEV_F_E_TAGGED_QING; 3070 } else { 3071 rval = -1; 3072 } 3073 } else { 3074 rval = 0; 3075 } 3076 break; 3077 default: 3078 rval = -1; 3079 break; 3080 } 3081 return (rval); 3082 } 3083 3084 /* 3085 * Implementations of scsi tran_destroy_pkt. 3086 * Free resources allocated by sata_scsi_init_pkt() 3087 */ 3088 static void 3089 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 3090 { 3091 sata_pkt_txlate_t *spx; 3092 3093 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 3094 3095 sata_common_free_dma_rsrcs(spx); 3096 3097 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 3098 sata_pkt_free(spx); 3099 3100 scsi_hba_pkt_free(ap, pkt); 3101 } 3102 3103 /* 3104 * Implementation of scsi tran_dmafree. 3105 * Free DMA resources allocated by sata_scsi_init_pkt() 3106 */ 3107 3108 static void 3109 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt) 3110 { 3111 #ifndef __lock_lint 3112 _NOTE(ARGUNUSED(ap)) 3113 #endif 3114 sata_pkt_txlate_t *spx; 3115 3116 ASSERT(pkt != NULL); 3117 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 3118 3119 sata_common_free_dma_rsrcs(spx); 3120 } 3121 3122 /* 3123 * Implementation of scsi tran_sync_pkt. 3124 * 3125 * The assumption below is that pkt is unique - there is no need to check ap 3126 * 3127 * Synchronize DMA buffer and, if the intermediate buffer is used, copy data 3128 * into/from the real buffer. 3129 */ 3130 static void 3131 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 3132 { 3133 #ifndef __lock_lint 3134 _NOTE(ARGUNUSED(ap)) 3135 #endif 3136 int rval; 3137 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 3138 struct buf *bp; 3139 int direction; 3140 3141 ASSERT(spx != NULL); 3142 if (spx->txlt_buf_dma_handle != NULL) { 3143 direction = spx->txlt_sata_pkt-> 3144 satapkt_cmd.satacmd_flags.sata_data_direction; 3145 if (spx->txlt_sata_pkt != NULL && 3146 direction != SATA_DIR_NODATA_XFER) { 3147 if (spx->txlt_tmp_buf != NULL) { 3148 /* Intermediate DMA buffer used */ 3149 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3150 3151 if (direction & SATA_DIR_WRITE) { 3152 bcopy(bp->b_un.b_addr, 3153 spx->txlt_tmp_buf, bp->b_bcount); 3154 } 3155 } 3156 /* Sync the buffer for device or for CPU */ 3157 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 3158 (direction & SATA_DIR_WRITE) ? 3159 DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU); 3160 ASSERT(rval == DDI_SUCCESS); 3161 if (spx->txlt_tmp_buf != NULL && 3162 !(direction & SATA_DIR_WRITE)) { 3163 /* Intermediate DMA buffer used for read */ 3164 bcopy(spx->txlt_tmp_buf, 3165 bp->b_un.b_addr, bp->b_bcount); 3166 } 3167 3168 } 3169 } 3170 } 3171 3172 3173 3174 /* ******************* SATA - SCSI Translation functions **************** */ 3175 /* 3176 * SCSI to SATA pkt and command translation and SATA to SCSI status/error 3177 * translation. 3178 */ 3179 3180 /* 3181 * Checks if a device exists and can be access and translates common 3182 * scsi_pkt data to sata_pkt data. 3183 * 3184 * Flag argument indicates that a non-read/write ATA command may be sent 3185 * to HBA in arbitrary SYNC mode to execute this packet. 3186 * 3187 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and 3188 * sata_pkt was set-up. 3189 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not 3190 * exist and pkt_comp callback was scheduled. 3191 * Returns other TRAN_XXXXX values when error occured and command should be 3192 * rejected with the returned TRAN_XXXXX value. 3193 * 3194 * This function should be called with port mutex held. 3195 */ 3196 static int 3197 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason, int flag) 3198 { 3199 sata_drive_info_t *sdinfo; 3200 sata_device_t sata_device; 3201 const struct sata_cmd_flags sata_initial_cmd_flags = { 3202 SATA_DIR_NODATA_XFER, 3203 /* all other values to 0/FALSE */ 3204 }; 3205 /* 3206 * Pkt_reason has to be set if the pkt_comp callback is invoked, 3207 * and that implies TRAN_ACCEPT return value. Any other returned value 3208 * indicates that the scsi packet was not accepted (the reason will not 3209 * be checked by the scsi target driver). 3210 * To make debugging easier, we set pkt_reason to know value here. 3211 * It may be changed later when different completion reason is 3212 * determined. 3213 */ 3214 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 3215 *reason = CMD_TRAN_ERR; 3216 3217 /* Validate address */ 3218 switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst, 3219 &spx->txlt_scsi_pkt->pkt_address, &sata_device)) { 3220 3221 case -1: 3222 /* Invalid address or invalid device type */ 3223 return (TRAN_BADPKT); 3224 case 2: 3225 /* 3226 * Valid address but device type is unknown - Chack if it is 3227 * in the reset state and therefore in an indeterminate state. 3228 */ 3229 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3230 &spx->txlt_sata_pkt->satapkt_device); 3231 if (sdinfo != NULL && (sdinfo->satadrv_event_flags & 3232 (SATA_EVNT_DEVICE_RESET | 3233 SATA_EVNT_INPROC_DEVICE_RESET)) != 0) { 3234 if (!ddi_in_panic()) { 3235 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 3236 *reason = CMD_INCOMPLETE; 3237 SATADBG1(SATA_DBG_SCSI_IF, 3238 spx->txlt_sata_hba_inst, 3239 "sata_scsi_start: rejecting command " 3240 "because of device reset state\n", NULL); 3241 return (TRAN_BUSY); 3242 } 3243 } 3244 /* FALLTHROUGH */ 3245 case 1: 3246 /* valid address but no valid device - it has disappeared */ 3247 spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE; 3248 *reason = CMD_DEV_GONE; 3249 /* 3250 * The sd target driver is checking CMD_DEV_GONE pkt_reason 3251 * only in callback function (for normal requests) and 3252 * in the dump code path. 3253 * So, if the callback is available, we need to do 3254 * the callback rather than returning TRAN_FATAL_ERROR here. 3255 */ 3256 if (spx->txlt_scsi_pkt->pkt_comp != NULL) { 3257 /* scsi callback required */ 3258 if (servicing_interrupt()) { 3259 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3260 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3261 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 3262 TASKQID_INVALID) { 3263 return (TRAN_BUSY); 3264 } 3265 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3266 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3267 spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 3268 /* Scheduling the callback failed */ 3269 return (TRAN_BUSY); 3270 } 3271 3272 return (TRAN_ACCEPT); 3273 } 3274 return (TRAN_FATAL_ERROR); 3275 default: 3276 /* all OK; pkt reason will be overwritten later */ 3277 break; 3278 } 3279 /* 3280 * If pkt is to be executed in polling mode and a command will not be 3281 * emulated in SATA module (requires sending a non-read/write ATA 3282 * command to HBA driver in arbitrary SYNC mode) and we are in the 3283 * interrupt context and not in the panic dump, then reject the packet 3284 * to avoid a possible interrupt stack overrun or hang caused by 3285 * a potentially blocked interrupt. 3286 */ 3287 if (((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0 || flag != 0) && 3288 servicing_interrupt() && !ddi_in_panic()) { 3289 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 3290 "sata_scsi_start: rejecting synchronous command because " 3291 "of interrupt context\n", NULL); 3292 return (TRAN_BUSY); 3293 } 3294 3295 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3296 &spx->txlt_sata_pkt->satapkt_device); 3297 3298 /* 3299 * If device is in reset condition, reject the packet with 3300 * TRAN_BUSY, unless: 3301 * 1. system is panicking (dumping) 3302 * In such case only one thread is running and there is no way to 3303 * process reset. 3304 * 2. cfgadm operation is is progress (internal APCTL lock is set) 3305 * Some cfgadm operations involve drive commands, so reset condition 3306 * needs to be ignored for IOCTL operations. 3307 */ 3308 if ((sdinfo->satadrv_event_flags & 3309 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) { 3310 3311 if (!ddi_in_panic() && 3312 ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst, 3313 sata_device.satadev_addr.cport) & 3314 SATA_APCTL_LOCK_PORT_BUSY) == 0)) { 3315 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 3316 *reason = CMD_INCOMPLETE; 3317 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3318 "sata_scsi_start: rejecting command because " 3319 "of device reset state\n", NULL); 3320 return (TRAN_BUSY); 3321 } 3322 } 3323 3324 /* 3325 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by 3326 * sata_scsi_pkt_init() because pkt init had to work also with 3327 * non-existing devices. 3328 * Now we know that the packet was set-up for a real device, so its 3329 * type is known. 3330 */ 3331 spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type; 3332 3333 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags; 3334 if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst, 3335 sata_device.satadev_addr.cport)->cport_event_flags & 3336 SATA_APCTL_LOCK_PORT_BUSY) != 0) { 3337 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 3338 sata_ignore_dev_reset = B_TRUE; 3339 } 3340 /* 3341 * At this point the generic translation routine determined that the 3342 * scsi packet should be accepted. Packet completion reason may be 3343 * changed later when a different completion reason is determined. 3344 */ 3345 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 3346 *reason = CMD_CMPLT; 3347 3348 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) { 3349 /* Synchronous execution */ 3350 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH | 3351 SATA_OPMODE_POLLING; 3352 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 3353 sata_ignore_dev_reset = ddi_in_panic(); 3354 } else { 3355 /* Asynchronous execution */ 3356 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH | 3357 SATA_OPMODE_INTERRUPTS; 3358 } 3359 /* Convert queuing information */ 3360 if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG) 3361 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag = 3362 B_TRUE; 3363 else if (spx->txlt_scsi_pkt->pkt_flags & 3364 (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD)) 3365 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag = 3366 B_TRUE; 3367 3368 /* Always limit pkt time */ 3369 if (spx->txlt_scsi_pkt->pkt_time == 0) 3370 spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time; 3371 else 3372 /* Pass on scsi_pkt time */ 3373 spx->txlt_sata_pkt->satapkt_time = 3374 spx->txlt_scsi_pkt->pkt_time; 3375 3376 return (TRAN_ACCEPT); 3377 } 3378 3379 3380 /* 3381 * Translate ATA Identify Device data to SCSI Inquiry data. 3382 * This function may be called only for ATA devices. 3383 * This function should not be called for ATAPI devices - they 3384 * respond directly to SCSI Inquiry command. 3385 * 3386 * SATA Identify Device data has to be valid in sata_drive_info. 3387 * Buffer has to accomodate the inquiry length (36 bytes). 3388 * 3389 * This function should be called with a port mutex held. 3390 */ 3391 static void 3392 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst, 3393 sata_drive_info_t *sdinfo, uint8_t *buf) 3394 { 3395 3396 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 3397 struct sata_id *sid = &sdinfo->satadrv_id; 3398 3399 /* Start with a nice clean slate */ 3400 bzero((void *)inq, sizeof (struct scsi_inquiry)); 3401 3402 /* 3403 * Rely on the dev_type for setting paripheral qualifier. 3404 * Assume that DTYPE_RODIRECT applies to CD/DVD R/W devices. 3405 * It could be that DTYPE_OPTICAL could also qualify in the future. 3406 * ATAPI Inquiry may provide more data to the target driver. 3407 */ 3408 inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 3409 DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */ 3410 3411 /* CFA type device is not a removable media device */ 3412 inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) && 3413 (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0; 3414 inq->inq_qual = 0; /* Device type qualifier (obsolete in SCSI3? */ 3415 inq->inq_iso = 0; /* ISO version */ 3416 inq->inq_ecma = 0; /* ECMA version */ 3417 inq->inq_ansi = 3; /* ANSI version - SCSI 3 */ 3418 inq->inq_aenc = 0; /* Async event notification cap. */ 3419 inq->inq_trmiop = 0; /* Supports TERMINATE I/O PROC msg - NO */ 3420 inq->inq_normaca = 0; /* setting NACA bit supported - NO */ 3421 inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */ 3422 inq->inq_len = 31; /* Additional length */ 3423 inq->inq_dualp = 0; /* dual port device - NO */ 3424 inq->inq_reladdr = 0; /* Supports relative addressing - NO */ 3425 inq->inq_sync = 0; /* Supports synchronous data xfers - NO */ 3426 inq->inq_linked = 0; /* Supports linked commands - NO */ 3427 /* 3428 * Queuing support - controller has to 3429 * support some sort of command queuing. 3430 */ 3431 if (SATA_QDEPTH(sata_hba_inst) > 1) 3432 inq->inq_cmdque = 1; /* Supports command queueing - YES */ 3433 else 3434 inq->inq_cmdque = 0; /* Supports command queueing - NO */ 3435 inq->inq_sftre = 0; /* Supports Soft Reset option - NO ??? */ 3436 inq->inq_wbus32 = 0; /* Supports 32 bit wide data xfers - NO */ 3437 inq->inq_wbus16 = 0; /* Supports 16 bit wide data xfers - NO */ 3438 3439 #ifdef _LITTLE_ENDIAN 3440 /* Swap text fields to match SCSI format */ 3441 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 3442 swab(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 3443 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 3444 swab(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 3445 else 3446 swab(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 3447 #else /* _LITTLE_ENDIAN */ 3448 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 3449 bcopy(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 3450 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 3451 bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 3452 else 3453 bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 3454 #endif /* _LITTLE_ENDIAN */ 3455 } 3456 3457 3458 /* 3459 * Scsi response set up for invalid command (command not supported) 3460 * 3461 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3462 */ 3463 static int 3464 sata_txlt_invalid_command(sata_pkt_txlate_t *spx) 3465 { 3466 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3467 struct scsi_extended_sense *sense; 3468 3469 scsipkt->pkt_reason = CMD_CMPLT; 3470 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3471 STATE_SENT_CMD | STATE_GOT_STATUS; 3472 3473 *scsipkt->pkt_scbp = STATUS_CHECK; 3474 3475 sense = sata_arq_sense(spx); 3476 sense->es_key = KEY_ILLEGAL_REQUEST; 3477 sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE; 3478 3479 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3480 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3481 3482 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3483 scsipkt->pkt_comp != NULL) { 3484 /* scsi callback required */ 3485 if (servicing_interrupt()) { 3486 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3487 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3488 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 3489 TASKQID_INVALID) { 3490 return (TRAN_BUSY); 3491 } 3492 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3493 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3494 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 3495 /* Scheduling the callback failed */ 3496 return (TRAN_BUSY); 3497 } 3498 } 3499 return (TRAN_ACCEPT); 3500 } 3501 3502 /* 3503 * Scsi response set up for check condition with special sense key 3504 * and additional sense code. 3505 * 3506 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3507 */ 3508 static int 3509 sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code) 3510 { 3511 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 3512 int cport = SATA_TXLT_CPORT(spx); 3513 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3514 struct scsi_extended_sense *sense; 3515 3516 mutex_enter(&SATA_CPORT_MUTEX(shi, cport)); 3517 scsipkt->pkt_reason = CMD_CMPLT; 3518 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3519 STATE_SENT_CMD | STATE_GOT_STATUS; 3520 3521 *scsipkt->pkt_scbp = STATUS_CHECK; 3522 3523 sense = sata_arq_sense(spx); 3524 sense->es_key = key; 3525 sense->es_add_code = code; 3526 3527 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3528 3529 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3530 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3531 3532 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3533 scsipkt->pkt_comp != NULL) { 3534 /* scsi callback required */ 3535 if (servicing_interrupt()) { 3536 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3537 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3538 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 3539 TASKQID_INVALID) { 3540 return (TRAN_BUSY); 3541 } 3542 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3543 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3544 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 3545 /* Scheduling the callback failed */ 3546 return (TRAN_BUSY); 3547 } 3548 } 3549 return (TRAN_ACCEPT); 3550 } 3551 3552 /* 3553 * Scsi response setup for 3554 * emulated non-data command that requires no action/return data 3555 * 3556 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3557 */ 3558 static int 3559 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx) 3560 { 3561 int rval; 3562 int reason; 3563 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 3564 3565 mutex_enter(cport_mutex); 3566 3567 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 3568 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3569 mutex_exit(cport_mutex); 3570 return (rval); 3571 } 3572 mutex_exit(cport_mutex); 3573 3574 spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3575 STATE_SENT_CMD | STATE_GOT_STATUS; 3576 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 3577 *(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD; 3578 3579 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3580 "Scsi_pkt completion reason %x\n", 3581 spx->txlt_scsi_pkt->pkt_reason); 3582 3583 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 && 3584 spx->txlt_scsi_pkt->pkt_comp != NULL) { 3585 /* scsi callback required */ 3586 if (servicing_interrupt()) { 3587 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3588 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3589 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 3590 TASKQID_INVALID) { 3591 return (TRAN_BUSY); 3592 } 3593 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3594 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3595 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 3596 /* Scheduling the callback failed */ 3597 return (TRAN_BUSY); 3598 } 3599 } 3600 return (TRAN_ACCEPT); 3601 } 3602 3603 3604 /* 3605 * SATA translate command: Inquiry / Identify Device 3606 * Use cached Identify Device data for now, rather than issuing actual 3607 * Device Identify cmd request. If device is detached and re-attached, 3608 * asynchronous event processing should fetch and refresh Identify Device 3609 * data. 3610 * VPD pages supported now: 3611 * Vital Product Data page 3612 * Unit Serial Number page 3613 * Block Device Characteristics Page 3614 * ATA Information Page 3615 * 3616 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3617 */ 3618 3619 #define EVPD 1 /* Extended Vital Product Data flag */ 3620 #define CMDDT 2 /* Command Support Data - Obsolete */ 3621 #define INQUIRY_SUP_VPD_PAGE 0 /* Supported VPD Pages Page Code */ 3622 #define INQUIRY_USN_PAGE 0x80 /* Unit Serial Number Page Code */ 3623 #define INQUIRY_BDC_PAGE 0xB1 /* Block Device Characteristics Page */ 3624 /* Code */ 3625 #define INQUIRY_ATA_INFO_PAGE 0x89 /* ATA Information Page Code */ 3626 #define INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */ 3627 3628 static int 3629 sata_txlt_inquiry(sata_pkt_txlate_t *spx) 3630 { 3631 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3632 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3633 sata_drive_info_t *sdinfo; 3634 struct scsi_extended_sense *sense; 3635 int count; 3636 uint8_t *p; 3637 int i, j; 3638 uint8_t page_buf[1024]; /* Max length */ 3639 int rval, reason; 3640 ushort_t rate; 3641 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 3642 3643 mutex_enter(cport_mutex); 3644 3645 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 3646 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3647 mutex_exit(cport_mutex); 3648 return (rval); 3649 } 3650 3651 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3652 &spx->txlt_sata_pkt->satapkt_device); 3653 3654 ASSERT(sdinfo != NULL); 3655 3656 scsipkt->pkt_reason = CMD_CMPLT; 3657 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3658 STATE_SENT_CMD | STATE_GOT_STATUS; 3659 3660 /* Reject not supported request */ 3661 if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */ 3662 *scsipkt->pkt_scbp = STATUS_CHECK; 3663 sense = sata_arq_sense(spx); 3664 sense->es_key = KEY_ILLEGAL_REQUEST; 3665 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3666 goto done; 3667 } 3668 3669 /* Valid Inquiry request */ 3670 *scsipkt->pkt_scbp = STATUS_GOOD; 3671 3672 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 3673 3674 /* 3675 * Because it is fully emulated command storing data 3676 * programatically in the specified buffer, release 3677 * preallocated DMA resources before storing data in the buffer, 3678 * so no unwanted DMA sync would take place. 3679 */ 3680 sata_scsi_dmafree(NULL, scsipkt); 3681 3682 if (!(scsipkt->pkt_cdbp[1] & EVPD)) { 3683 /* Standard Inquiry Data request */ 3684 struct scsi_inquiry inq; 3685 unsigned int bufsize; 3686 3687 sata_identdev_to_inquiry(spx->txlt_sata_hba_inst, 3688 sdinfo, (uint8_t *)&inq); 3689 /* Copy no more than requested */ 3690 count = MIN(bp->b_bcount, 3691 sizeof (struct scsi_inquiry)); 3692 bufsize = scsipkt->pkt_cdbp[4]; 3693 bufsize |= scsipkt->pkt_cdbp[3] << 8; 3694 count = MIN(count, bufsize); 3695 bcopy(&inq, bp->b_un.b_addr, count); 3696 3697 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3698 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 3699 bufsize - count : 0; 3700 } else { 3701 /* 3702 * peripheral_qualifier = 0; 3703 * 3704 * We are dealing only with HD and will be 3705 * dealing with CD/DVD devices soon 3706 */ 3707 uint8_t peripheral_device_type = 3708 sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 3709 DTYPE_DIRECT : DTYPE_RODIRECT; 3710 3711 bzero(page_buf, sizeof (page_buf)); 3712 3713 switch ((uint_t)scsipkt->pkt_cdbp[2]) { 3714 case INQUIRY_SUP_VPD_PAGE: 3715 /* 3716 * Request for supported Vital Product Data 3717 * pages. 3718 */ 3719 page_buf[0] = peripheral_device_type; 3720 page_buf[1] = INQUIRY_SUP_VPD_PAGE; 3721 page_buf[2] = 0; 3722 page_buf[3] = 4; /* page length */ 3723 page_buf[4] = INQUIRY_SUP_VPD_PAGE; 3724 page_buf[5] = INQUIRY_USN_PAGE; 3725 page_buf[6] = INQUIRY_BDC_PAGE; 3726 page_buf[7] = INQUIRY_ATA_INFO_PAGE; 3727 /* Copy no more than requested */ 3728 count = MIN(bp->b_bcount, 8); 3729 bcopy(page_buf, bp->b_un.b_addr, count); 3730 break; 3731 3732 case INQUIRY_USN_PAGE: 3733 /* 3734 * Request for Unit Serial Number page. 3735 * Set-up the page. 3736 */ 3737 page_buf[0] = peripheral_device_type; 3738 page_buf[1] = INQUIRY_USN_PAGE; 3739 page_buf[2] = 0; 3740 /* remaining page length */ 3741 page_buf[3] = SATA_ID_SERIAL_LEN; 3742 3743 /* 3744 * Copy serial number from Identify Device data 3745 * words into the inquiry page and swap bytes 3746 * when necessary. 3747 */ 3748 p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser); 3749 #ifdef _LITTLE_ENDIAN 3750 swab(p, &page_buf[4], SATA_ID_SERIAL_LEN); 3751 #else 3752 bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN); 3753 #endif 3754 /* 3755 * Least significant character of the serial 3756 * number shall appear as the last byte, 3757 * according to SBC-3 spec. 3758 * Count trailing spaces to determine the 3759 * necessary shift length. 3760 */ 3761 p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1]; 3762 for (j = 0; j < SATA_ID_SERIAL_LEN; j++) { 3763 if (*(p - j) != '\0' && 3764 *(p - j) != '\040') 3765 break; 3766 } 3767 3768 /* 3769 * Shift SN string right, so that the last 3770 * non-blank character would appear in last 3771 * byte of SN field in the page. 3772 * 'j' is the shift length. 3773 */ 3774 for (i = 0; 3775 i < (SATA_ID_SERIAL_LEN - j) && j != 0; 3776 i++, p--) 3777 *p = *(p - j); 3778 3779 /* 3780 * Add leading spaces - same number as the 3781 * shift size 3782 */ 3783 for (; j > 0; j--) 3784 page_buf[4 + j - 1] = '\040'; 3785 3786 count = MIN(bp->b_bcount, 3787 SATA_ID_SERIAL_LEN + 4); 3788 bcopy(page_buf, bp->b_un.b_addr, count); 3789 break; 3790 3791 case INQUIRY_BDC_PAGE: 3792 /* 3793 * Request for Block Device Characteristics 3794 * page. Set-up the page. 3795 */ 3796 page_buf[0] = peripheral_device_type; 3797 page_buf[1] = INQUIRY_BDC_PAGE; 3798 page_buf[2] = 0; 3799 /* remaining page length */ 3800 page_buf[3] = SATA_ID_BDC_LEN; 3801 3802 rate = sdinfo->satadrv_id.ai_medrotrate; 3803 page_buf[4] = (rate >> 8) & 0xff; 3804 page_buf[5] = rate & 0xff; 3805 page_buf[6] = 0; 3806 page_buf[7] = sdinfo->satadrv_id. 3807 ai_nomformfactor & 0xf; 3808 3809 count = MIN(bp->b_bcount, 3810 SATA_ID_BDC_LEN + 4); 3811 bcopy(page_buf, bp->b_un.b_addr, count); 3812 break; 3813 3814 case INQUIRY_ATA_INFO_PAGE: 3815 /* 3816 * Request for ATA Information page. 3817 */ 3818 page_buf[0] = peripheral_device_type; 3819 page_buf[1] = INQUIRY_ATA_INFO_PAGE; 3820 page_buf[2] = (SATA_ID_ATA_INFO_LEN >> 8) & 3821 0xff; 3822 page_buf[3] = SATA_ID_ATA_INFO_LEN & 0xff; 3823 /* page_buf[4-7] reserved */ 3824 #ifdef _LITTLE_ENDIAN 3825 bcopy("ATA ", &page_buf[8], 8); 3826 swab(sdinfo->satadrv_id.ai_model, 3827 &page_buf[16], 16); 3828 if (strncmp(&sdinfo->satadrv_id.ai_fw[4], 3829 " ", 4) == 0) { 3830 swab(sdinfo->satadrv_id.ai_fw, 3831 &page_buf[32], 4); 3832 } else { 3833 swab(&sdinfo->satadrv_id.ai_fw[4], 3834 &page_buf[32], 4); 3835 } 3836 #else /* _LITTLE_ENDIAN */ 3837 bcopy("ATA ", &page_buf[8], 8); 3838 bcopy(sdinfo->satadrv_id.ai_model, 3839 &page_buf[16], 16); 3840 if (strncmp(&sdinfo->satadrv_id.ai_fw[4], 3841 " ", 4) == 0) { 3842 bcopy(sdinfo->satadrv_id.ai_fw, 3843 &page_buf[32], 4); 3844 } else { 3845 bcopy(&sdinfo->satadrv_id.ai_fw[4], 3846 &page_buf[32], 4); 3847 } 3848 #endif /* _LITTLE_ENDIAN */ 3849 /* 3850 * page_buf[36-55] which defines the device 3851 * signature is not defined at this 3852 * time. 3853 */ 3854 3855 /* Set the command code */ 3856 if (sdinfo->satadrv_type == 3857 SATA_DTYPE_ATADISK) { 3858 page_buf[56] = SATAC_ID_DEVICE; 3859 } else if (sdinfo->satadrv_type == 3860 SATA_DTYPE_ATAPI) { 3861 page_buf[56] = SATAC_ID_PACKET_DEVICE; 3862 } 3863 /* 3864 * If the command code, page_buf[56], is not 3865 * zero and if one of the identify commands 3866 * succeeds, return the identify data. 3867 */ 3868 if ((page_buf[56] != 0) && 3869 (sata_fetch_device_identify_data( 3870 spx->txlt_sata_hba_inst, sdinfo) == 3871 SATA_SUCCESS)) { 3872 bcopy(&sdinfo->satadrv_id, 3873 &page_buf[60], sizeof (sata_id_t)); 3874 } 3875 3876 /* Need to copy out the page_buf to bp */ 3877 count = MIN(bp->b_bcount, 3878 SATA_ID_ATA_INFO_LEN + 4); 3879 bcopy(page_buf, bp->b_un.b_addr, count); 3880 break; 3881 3882 case INQUIRY_DEV_IDENTIFICATION_PAGE: 3883 /* 3884 * We may want to implement this page, when 3885 * identifiers are common for SATA devices 3886 * But not now. 3887 */ 3888 /*FALLTHROUGH*/ 3889 3890 default: 3891 /* Request for unsupported VPD page */ 3892 *scsipkt->pkt_scbp = STATUS_CHECK; 3893 sense = sata_arq_sense(spx); 3894 sense->es_key = KEY_ILLEGAL_REQUEST; 3895 sense->es_add_code = 3896 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3897 goto done; 3898 } 3899 } 3900 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3901 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 3902 scsipkt->pkt_cdbp[4] - count : 0; 3903 } 3904 done: 3905 mutex_exit(cport_mutex); 3906 3907 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3908 "Scsi_pkt completion reason %x\n", 3909 scsipkt->pkt_reason); 3910 3911 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3912 scsipkt->pkt_comp != NULL) { 3913 /* scsi callback required */ 3914 if (servicing_interrupt()) { 3915 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3916 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3917 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 3918 TASKQID_INVALID) { 3919 return (TRAN_BUSY); 3920 } 3921 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3922 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3923 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 3924 /* Scheduling the callback failed */ 3925 return (TRAN_BUSY); 3926 } 3927 } 3928 return (TRAN_ACCEPT); 3929 } 3930 3931 /* 3932 * SATA translate command: Request Sense. 3933 * 3934 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3935 * At the moment this is an emulated command (ATA version for SATA hard disks). 3936 * May be translated into Check Power Mode command in the future. 3937 * 3938 * Note: There is a mismatch between already implemented Informational 3939 * Exception Mode Select page 0x1C and this function. 3940 * When MRIE bit is set in page 0x1C, Request Sense is supposed to return 3941 * NO SENSE and set additional sense code to the exception code - this is not 3942 * implemented here. 3943 */ 3944 static int 3945 sata_txlt_request_sense(sata_pkt_txlate_t *spx) 3946 { 3947 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3948 struct scsi_extended_sense sense; 3949 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3950 sata_drive_info_t *sdinfo; 3951 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 3952 int rval, reason, power_state = 0; 3953 kmutex_t *cport_mutex; 3954 3955 cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 3956 mutex_enter(cport_mutex); 3957 3958 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 3959 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3960 mutex_exit(cport_mutex); 3961 return (rval); 3962 } 3963 3964 scsipkt->pkt_reason = CMD_CMPLT; 3965 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3966 STATE_SENT_CMD | STATE_GOT_STATUS; 3967 *scsipkt->pkt_scbp = STATUS_GOOD; 3968 3969 /* 3970 * when CONTROL field's NACA bit == 1 3971 * return ILLEGAL_REQUEST 3972 */ 3973 if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) { 3974 mutex_exit(cport_mutex); 3975 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 3976 SD_SCSI_ASC_CMD_SEQUENCE_ERR)); 3977 } 3978 3979 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3980 &spx->txlt_sata_pkt->satapkt_device); 3981 ASSERT(sdinfo != NULL); 3982 3983 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH; 3984 3985 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE); 3986 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE; 3987 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 3988 if (sata_hba_start(spx, &rval) != 0) { 3989 mutex_exit(cport_mutex); 3990 return (rval); 3991 } 3992 if (scmd->satacmd_error_reg != 0) { 3993 mutex_exit(cport_mutex); 3994 return (sata_txlt_check_condition(spx, KEY_NO_SENSE, 3995 SD_SCSI_ASC_NO_ADD_SENSE)); 3996 } 3997 3998 switch (scmd->satacmd_sec_count_lsb) { 3999 case SATA_PWRMODE_STANDBY: /* device in standby mode */ 4000 if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED) 4001 power_state = SATA_POWER_STOPPED; 4002 else { 4003 power_state = SATA_POWER_STANDBY; 4004 sdinfo->satadrv_power_level = SATA_POWER_STANDBY; 4005 } 4006 break; 4007 case SATA_PWRMODE_IDLE: /* device in idle mode */ 4008 power_state = SATA_POWER_IDLE; 4009 sdinfo->satadrv_power_level = SATA_POWER_IDLE; 4010 break; 4011 case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */ 4012 default: /* 0x40, 0x41 active mode */ 4013 if (sdinfo->satadrv_power_level == SATA_POWER_IDLE) 4014 power_state = SATA_POWER_IDLE; 4015 else { 4016 power_state = SATA_POWER_ACTIVE; 4017 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 4018 } 4019 break; 4020 } 4021 4022 mutex_exit(cport_mutex); 4023 4024 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4025 /* 4026 * Because it is fully emulated command storing data 4027 * programatically in the specified buffer, release 4028 * preallocated DMA resources before storing data in the buffer, 4029 * so no unwanted DMA sync would take place. 4030 */ 4031 int count = MIN(bp->b_bcount, 4032 sizeof (struct scsi_extended_sense)); 4033 sata_scsi_dmafree(NULL, scsipkt); 4034 bzero(&sense, sizeof (struct scsi_extended_sense)); 4035 sense.es_valid = 0; /* Valid LBA */ 4036 sense.es_class = 7; /* Response code 0x70 - current err */ 4037 sense.es_key = KEY_NO_SENSE; 4038 sense.es_add_len = 6; /* Additional length */ 4039 /* Copy no more than requested */ 4040 bcopy(&sense, bp->b_un.b_addr, count); 4041 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4042 scsipkt->pkt_resid = 0; 4043 switch (power_state) { 4044 case SATA_POWER_IDLE: 4045 case SATA_POWER_STANDBY: 4046 sense.es_add_code = 4047 SD_SCSI_ASC_LOW_POWER_CONDITION_ON; 4048 break; 4049 case SATA_POWER_STOPPED: 4050 sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE; 4051 break; 4052 case SATA_POWER_ACTIVE: 4053 default: 4054 break; 4055 } 4056 } 4057 4058 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4059 "Scsi_pkt completion reason %x\n", 4060 scsipkt->pkt_reason); 4061 4062 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4063 scsipkt->pkt_comp != NULL) { 4064 /* scsi callback required */ 4065 if (servicing_interrupt()) { 4066 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4067 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4068 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 4069 TASKQID_INVALID) { 4070 return (TRAN_BUSY); 4071 } 4072 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4073 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4074 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 4075 /* Scheduling the callback failed */ 4076 return (TRAN_BUSY); 4077 } 4078 } 4079 return (TRAN_ACCEPT); 4080 } 4081 4082 /* 4083 * SATA translate command: Test Unit Ready 4084 * (ATA version for SATA hard disks). 4085 * It is translated into the Check Power Mode command. 4086 * 4087 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4088 */ 4089 static int 4090 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx) 4091 { 4092 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4093 struct scsi_extended_sense *sense; 4094 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4095 sata_drive_info_t *sdinfo; 4096 int power_state; 4097 int rval, reason; 4098 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 4099 4100 mutex_enter(cport_mutex); 4101 4102 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 4103 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4104 mutex_exit(cport_mutex); 4105 return (rval); 4106 } 4107 4108 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4109 &spx->txlt_sata_pkt->satapkt_device); 4110 ASSERT(sdinfo != NULL); 4111 4112 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH; 4113 4114 /* send CHECK POWER MODE command */ 4115 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE); 4116 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE; 4117 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4118 if (sata_hba_start(spx, &rval) != 0) { 4119 mutex_exit(cport_mutex); 4120 return (rval); 4121 } 4122 4123 if (scmd->satacmd_error_reg != 0) { 4124 mutex_exit(cport_mutex); 4125 return (sata_txlt_check_condition(spx, KEY_NOT_READY, 4126 SD_SCSI_ASC_LU_NOT_RESPONSE)); 4127 } 4128 4129 power_state = scmd->satacmd_sec_count_lsb; 4130 4131 /* 4132 * return NOT READY when device in STOPPED mode 4133 */ 4134 if (power_state == SATA_PWRMODE_STANDBY && 4135 sdinfo->satadrv_power_level == SATA_POWER_STOPPED) { 4136 *scsipkt->pkt_scbp = STATUS_CHECK; 4137 sense = sata_arq_sense(spx); 4138 sense->es_key = KEY_NOT_READY; 4139 sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY; 4140 } else { 4141 /* 4142 * For other power mode, return GOOD status 4143 */ 4144 *scsipkt->pkt_scbp = STATUS_GOOD; 4145 } 4146 4147 scsipkt->pkt_reason = CMD_CMPLT; 4148 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4149 STATE_SENT_CMD | STATE_GOT_STATUS; 4150 4151 mutex_exit(cport_mutex); 4152 4153 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4154 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4155 4156 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4157 scsipkt->pkt_comp != NULL) { 4158 /* scsi callback required */ 4159 if (servicing_interrupt()) { 4160 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4161 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4162 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 4163 TASKQID_INVALID) { 4164 return (TRAN_BUSY); 4165 } 4166 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4167 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4168 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 4169 /* Scheduling the callback failed */ 4170 return (TRAN_BUSY); 4171 } 4172 } 4173 4174 return (TRAN_ACCEPT); 4175 } 4176 4177 /* 4178 * SATA translate command: Start Stop Unit 4179 * Translation depends on a command: 4180 * 4181 * Power condition bits will be supported 4182 * and the power level should be maintained by SATL, 4183 * When SATL received a command, it will check the 4184 * power level firstly, and return the status according 4185 * to SAT2 v2.6 and SAT-2 Standby Modifications 4186 * 4187 * SPC-4/SBC-3 SATL ATA power condition SATL SPC/SBC 4188 * ----------------------------------------------------------------------- 4189 * SSU_PC1 Active <==> ATA Active <==> SSU:start_bit =1 4190 * SSU_PC2 Idle <==> ATA Idle <==> N/A 4191 * SSU_PC3 Standby <==> ATA Standby <==> N/A 4192 * SSU_PC4 Stopped <==> ATA Standby <==> SSU:start_bit = 0 4193 * 4194 * Unload Media / NOT SUPPORTED YET 4195 * Load Media / NOT SUPPROTED YET 4196 * Immediate bit / NOT SUPPORTED YET (deferred error) 4197 * 4198 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 4199 * appropriate values in scsi_pkt fields. 4200 */ 4201 static int 4202 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx) 4203 { 4204 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4205 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4206 int rval, reason; 4207 sata_drive_info_t *sdinfo; 4208 sata_id_t *sata_id; 4209 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 4210 4211 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4212 "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1); 4213 4214 mutex_enter(cport_mutex); 4215 4216 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 4217 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4218 mutex_exit(cport_mutex); 4219 return (rval); 4220 } 4221 4222 if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) { 4223 /* IMMED bit - not supported */ 4224 mutex_exit(cport_mutex); 4225 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 4226 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4227 } 4228 4229 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH; 4230 spx->txlt_sata_pkt->satapkt_comp = NULL; 4231 4232 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4233 &spx->txlt_sata_pkt->satapkt_device); 4234 ASSERT(sdinfo != NULL); 4235 sata_id = &sdinfo->satadrv_id; 4236 4237 switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) { 4238 case 0: 4239 if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) { 4240 /* Load/Unload Media - invalid request */ 4241 goto err_out; 4242 } 4243 if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) { 4244 /* Start Unit */ 4245 sata_build_read_verify_cmd(scmd, 1, 5); 4246 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4247 /* Transfer command to HBA */ 4248 if (sata_hba_start(spx, &rval) != 0) { 4249 /* Pkt not accepted for execution */ 4250 mutex_exit(cport_mutex); 4251 return (rval); 4252 } 4253 if (scmd->satacmd_error_reg != 0) { 4254 goto err_out; 4255 } 4256 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 4257 } else { 4258 /* Stop Unit */ 4259 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 4260 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4261 if (sata_hba_start(spx, &rval) != 0) { 4262 mutex_exit(cport_mutex); 4263 return (rval); 4264 } else { 4265 if (scmd->satacmd_error_reg != 0) { 4266 goto err_out; 4267 } 4268 } 4269 /* ata standby immediate command */ 4270 sata_build_generic_cmd(scmd, SATAC_STANDBY_IM); 4271 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4272 if (sata_hba_start(spx, &rval) != 0) { 4273 mutex_exit(cport_mutex); 4274 return (rval); 4275 } 4276 if (scmd->satacmd_error_reg != 0) { 4277 goto err_out; 4278 } 4279 sdinfo->satadrv_power_level = SATA_POWER_STOPPED; 4280 } 4281 break; 4282 case 0x1: 4283 sata_build_generic_cmd(scmd, SATAC_IDLE); 4284 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4285 if (sata_hba_start(spx, &rval) != 0) { 4286 mutex_exit(cport_mutex); 4287 return (rval); 4288 } 4289 if (scmd->satacmd_error_reg != 0) { 4290 goto err_out; 4291 } 4292 sata_build_read_verify_cmd(scmd, 1, 5); 4293 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4294 /* Transfer command to HBA */ 4295 if (sata_hba_start(spx, &rval) != 0) { 4296 /* Pkt not accepted for execution */ 4297 mutex_exit(cport_mutex); 4298 return (rval); 4299 } else { 4300 if (scmd->satacmd_error_reg != 0) { 4301 goto err_out; 4302 } 4303 } 4304 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 4305 break; 4306 case 0x2: 4307 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 4308 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4309 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) { 4310 if (sata_hba_start(spx, &rval) != 0) { 4311 mutex_exit(cport_mutex); 4312 return (rval); 4313 } 4314 if (scmd->satacmd_error_reg != 0) { 4315 goto err_out; 4316 } 4317 } 4318 sata_build_generic_cmd(scmd, SATAC_IDLE); 4319 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4320 if (sata_hba_start(spx, &rval) != 0) { 4321 mutex_exit(cport_mutex); 4322 return (rval); 4323 } 4324 if (scmd->satacmd_error_reg != 0) { 4325 goto err_out; 4326 } 4327 if ((scsipkt->pkt_cdbp[3] & START_STOP_MODIFIER_MASK)) { 4328 /* 4329 * POWER CONDITION MODIFIER bit set 4330 * to 0x1 or larger it will be handled 4331 * on the same way as bit = 0x1 4332 */ 4333 if (!(sata_id->ai_cmdset84 & 4334 SATA_IDLE_UNLOAD_SUPPORTED)) { 4335 sdinfo->satadrv_power_level = SATA_POWER_IDLE; 4336 break; 4337 } 4338 sata_build_generic_cmd(scmd, SATAC_IDLE_IM); 4339 scmd->satacmd_features_reg = 0x44; 4340 scmd->satacmd_lba_low_lsb = 0x4c; 4341 scmd->satacmd_lba_mid_lsb = 0x4e; 4342 scmd->satacmd_lba_high_lsb = 0x55; 4343 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4344 if (sata_hba_start(spx, &rval) != 0) { 4345 mutex_exit(cport_mutex); 4346 return (rval); 4347 } 4348 if (scmd->satacmd_error_reg != 0) { 4349 goto err_out; 4350 } 4351 } 4352 sdinfo->satadrv_power_level = SATA_POWER_IDLE; 4353 break; 4354 case 0x3: 4355 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 4356 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4357 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) { 4358 if (sata_hba_start(spx, &rval) != 0) { 4359 mutex_exit(cport_mutex); 4360 return (rval); 4361 } 4362 if (scmd->satacmd_error_reg != 0) { 4363 goto err_out; 4364 } 4365 } 4366 sata_build_generic_cmd(scmd, SATAC_STANDBY); 4367 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4368 if (sata_hba_start(spx, &rval) != 0) { 4369 mutex_exit(cport_mutex); 4370 return (rval); 4371 } 4372 if (scmd->satacmd_error_reg != 0) { 4373 goto err_out; 4374 } 4375 sdinfo->satadrv_power_level = SATA_POWER_STANDBY; 4376 break; 4377 case 0x7: 4378 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE); 4379 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE; 4380 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4381 if (sata_hba_start(spx, &rval) != 0) { 4382 mutex_exit(cport_mutex); 4383 return (rval); 4384 } 4385 if (scmd->satacmd_error_reg != 0) { 4386 goto err_out; 4387 } 4388 switch (scmd->satacmd_sec_count_lsb) { 4389 case SATA_PWRMODE_STANDBY: 4390 sata_build_generic_cmd(scmd, SATAC_STANDBY); 4391 scmd->satacmd_sec_count_msb = sata_get_standby_timer( 4392 sdinfo->satadrv_standby_timer); 4393 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4394 if (sata_hba_start(spx, &rval) != 0) { 4395 mutex_exit(cport_mutex); 4396 return (rval); 4397 } else { 4398 if (scmd->satacmd_error_reg != 0) { 4399 goto err_out; 4400 } 4401 } 4402 break; 4403 case SATA_PWRMODE_IDLE: 4404 sata_build_generic_cmd(scmd, SATAC_IDLE); 4405 scmd->satacmd_sec_count_msb = sata_get_standby_timer( 4406 sdinfo->satadrv_standby_timer); 4407 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4408 if (sata_hba_start(spx, &rval) != 0) { 4409 mutex_exit(cport_mutex); 4410 return (rval); 4411 } else { 4412 if (scmd->satacmd_error_reg != 0) { 4413 goto err_out; 4414 } 4415 } 4416 break; 4417 case SATA_PWRMODE_ACTIVE_SPINDOWN: 4418 case SATA_PWRMODE_ACTIVE_SPINUP: 4419 case SATA_PWRMODE_ACTIVE: 4420 sata_build_generic_cmd(scmd, SATAC_IDLE); 4421 scmd->satacmd_sec_count_msb = sata_get_standby_timer( 4422 sdinfo->satadrv_standby_timer); 4423 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4424 if (sata_hba_start(spx, &rval) != 0) { 4425 mutex_exit(cport_mutex); 4426 return (rval); 4427 } 4428 if (scmd->satacmd_error_reg != 0) { 4429 goto err_out; 4430 } 4431 sata_build_read_verify_cmd(scmd, 1, 5); 4432 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4433 if (sata_hba_start(spx, &rval) != 0) { 4434 mutex_exit(cport_mutex); 4435 return (rval); 4436 } 4437 if (scmd->satacmd_error_reg != 0) { 4438 goto err_out; 4439 } 4440 break; 4441 default: 4442 goto err_out; 4443 } 4444 break; 4445 case 0xb: 4446 if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) == 4447 0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) { 4448 mutex_exit(cport_mutex); 4449 return (sata_txlt_check_condition(spx, 4450 KEY_ILLEGAL_REQUEST, 4451 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4452 } 4453 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 4454 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4455 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) { 4456 if (sata_hba_start(spx, &rval) != 0) { 4457 mutex_exit(cport_mutex); 4458 return (rval); 4459 } 4460 if (scmd->satacmd_error_reg != 0) { 4461 goto err_out; 4462 } 4463 sata_build_generic_cmd(scmd, SATAC_STANDBY_IM); 4464 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4465 if (sata_hba_start(spx, &rval) != 0) { 4466 mutex_exit(cport_mutex); 4467 return (rval); 4468 } 4469 if (scmd->satacmd_error_reg != 0) { 4470 goto err_out; 4471 } 4472 } 4473 bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4); 4474 break; 4475 default: 4476 err_out: 4477 mutex_exit(cport_mutex); 4478 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 4479 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4480 } 4481 4482 /* 4483 * Since it was a synchronous command, 4484 * a callback function will be called directly. 4485 */ 4486 mutex_exit(cport_mutex); 4487 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4488 "synchronous execution status %x\n", 4489 spx->txlt_sata_pkt->satapkt_reason); 4490 4491 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4492 scsipkt->pkt_comp != NULL) { 4493 sata_set_arq_data(spx->txlt_sata_pkt); 4494 if (servicing_interrupt()) { 4495 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4496 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4497 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 4498 TASKQID_INVALID) { 4499 return (TRAN_BUSY); 4500 } 4501 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4502 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4503 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 4504 /* Scheduling the callback failed */ 4505 return (TRAN_BUSY); 4506 } 4507 } 4508 else 4509 4510 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 4511 4512 return (TRAN_ACCEPT); 4513 4514 } 4515 4516 /* 4517 * SATA translate command: Read Capacity. 4518 * Emulated command for SATA disks. 4519 * Capacity is retrieved from cached Idenifty Device data. 4520 * Identify Device data shows effective disk capacity, not the native 4521 * capacity, which may be limitted by Set Max Address command. 4522 * This is ATA version for SATA hard disks. 4523 * 4524 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4525 */ 4526 static int 4527 sata_txlt_read_capacity(sata_pkt_txlate_t *spx) 4528 { 4529 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4530 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4531 sata_drive_info_t *sdinfo; 4532 uint64_t val; 4533 uint32_t lbsize = DEV_BSIZE; 4534 uchar_t *rbuf; 4535 int rval, reason; 4536 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 4537 4538 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4539 "sata_txlt_read_capacity: ", NULL); 4540 4541 mutex_enter(cport_mutex); 4542 4543 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 4544 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4545 mutex_exit(cport_mutex); 4546 return (rval); 4547 } 4548 4549 scsipkt->pkt_reason = CMD_CMPLT; 4550 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4551 STATE_SENT_CMD | STATE_GOT_STATUS; 4552 *scsipkt->pkt_scbp = STATUS_GOOD; 4553 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4554 /* 4555 * Because it is fully emulated command storing data 4556 * programatically in the specified buffer, release 4557 * preallocated DMA resources before storing data in the buffer, 4558 * so no unwanted DMA sync would take place. 4559 */ 4560 sata_scsi_dmafree(NULL, scsipkt); 4561 4562 sdinfo = sata_get_device_info( 4563 spx->txlt_sata_hba_inst, 4564 &spx->txlt_sata_pkt->satapkt_device); 4565 4566 /* 4567 * As per SBC-3, the "returned LBA" is either the highest 4568 * addressable LBA or 0xffffffff, whichever is smaller. 4569 */ 4570 val = MIN(sdinfo->satadrv_capacity - 1, UINT32_MAX); 4571 4572 if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) { 4573 /* physical/logical sector size word is valid */ 4574 4575 if (sdinfo->satadrv_id.ai_phys_sect_sz & 4576 SATA_L2PS_BIG_SECTORS) { 4577 /* if this set 117-118 words are valid */ 4578 lbsize = sdinfo->satadrv_id.ai_words_lsec[0] | 4579 (sdinfo->satadrv_id.ai_words_lsec[1] << 16); 4580 lbsize <<= 1; /* convert from words to bytes */ 4581 } 4582 } 4583 rbuf = (uchar_t *)bp->b_un.b_addr; 4584 /* Need to swap endians to match scsi format */ 4585 rbuf[0] = (val >> 24) & 0xff; 4586 rbuf[1] = (val >> 16) & 0xff; 4587 rbuf[2] = (val >> 8) & 0xff; 4588 rbuf[3] = val & 0xff; 4589 rbuf[4] = (lbsize >> 24) & 0xff; 4590 rbuf[5] = (lbsize >> 16) & 0xff; 4591 rbuf[6] = (lbsize >> 8) & 0xff; 4592 rbuf[7] = lbsize & 0xff; 4593 4594 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4595 scsipkt->pkt_resid = 0; 4596 4597 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n", 4598 sdinfo->satadrv_capacity -1); 4599 } 4600 mutex_exit(cport_mutex); 4601 /* 4602 * If a callback was requested, do it now. 4603 */ 4604 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4605 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4606 4607 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4608 scsipkt->pkt_comp != NULL) { 4609 /* scsi callback required */ 4610 if (servicing_interrupt()) { 4611 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4612 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4613 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 4614 TASKQID_INVALID) { 4615 return (TRAN_BUSY); 4616 } 4617 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4618 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4619 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 4620 /* Scheduling the callback failed */ 4621 return (TRAN_BUSY); 4622 } 4623 } 4624 4625 return (TRAN_ACCEPT); 4626 } 4627 4628 /* 4629 * SATA translate command: Read Capacity (16). 4630 * Emulated command for SATA disks. 4631 * Info is retrieved from cached Identify Device data. 4632 * Implemented to SBC-3 (draft 21) and SAT-2 (final) specifications. 4633 * 4634 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4635 */ 4636 static int 4637 sata_txlt_read_capacity16(sata_pkt_txlate_t *spx) 4638 { 4639 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4640 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4641 sata_drive_info_t *sdinfo; 4642 uint64_t val; 4643 uint16_t l2p_exp; 4644 uint32_t lbsize = DEV_BSIZE; 4645 uchar_t *rbuf; 4646 int rval, reason; 4647 #define TPE 0x80 4648 #define TPRZ 0x40 4649 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 4650 4651 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4652 "sata_txlt_read_capacity: ", NULL); 4653 4654 mutex_enter(cport_mutex); 4655 4656 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 4657 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4658 mutex_exit(cport_mutex); 4659 return (rval); 4660 } 4661 4662 scsipkt->pkt_reason = CMD_CMPLT; 4663 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4664 STATE_SENT_CMD | STATE_GOT_STATUS; 4665 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4666 /* 4667 * Because it is fully emulated command storing data 4668 * programatically in the specified buffer, release 4669 * preallocated DMA resources before storing data in the buffer, 4670 * so no unwanted DMA sync would take place. 4671 */ 4672 sata_scsi_dmafree(NULL, scsipkt); 4673 4674 /* Check SERVICE ACTION field */ 4675 if ((scsipkt->pkt_cdbp[1] & 0x1f) != 4676 SSVC_ACTION_READ_CAPACITY_G4) { 4677 mutex_exit(cport_mutex); 4678 return (sata_txlt_check_condition(spx, 4679 KEY_ILLEGAL_REQUEST, 4680 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4681 } 4682 4683 /* Check LBA field */ 4684 if ((scsipkt->pkt_cdbp[2] != 0) || 4685 (scsipkt->pkt_cdbp[3] != 0) || 4686 (scsipkt->pkt_cdbp[4] != 0) || 4687 (scsipkt->pkt_cdbp[5] != 0) || 4688 (scsipkt->pkt_cdbp[6] != 0) || 4689 (scsipkt->pkt_cdbp[7] != 0) || 4690 (scsipkt->pkt_cdbp[8] != 0) || 4691 (scsipkt->pkt_cdbp[9] != 0)) { 4692 mutex_exit(cport_mutex); 4693 return (sata_txlt_check_condition(spx, 4694 KEY_ILLEGAL_REQUEST, 4695 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4696 } 4697 4698 /* Check PMI bit */ 4699 if (scsipkt->pkt_cdbp[14] & 0x1) { 4700 mutex_exit(cport_mutex); 4701 return (sata_txlt_check_condition(spx, 4702 KEY_ILLEGAL_REQUEST, 4703 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4704 } 4705 4706 *scsipkt->pkt_scbp = STATUS_GOOD; 4707 4708 sdinfo = sata_get_device_info( 4709 spx->txlt_sata_hba_inst, 4710 &spx->txlt_sata_pkt->satapkt_device); 4711 4712 /* last logical block address */ 4713 val = MIN(sdinfo->satadrv_capacity - 1, 4714 SCSI_READ_CAPACITY16_MAX_LBA); 4715 4716 /* logical to physical block size exponent */ 4717 l2p_exp = 0; 4718 if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) { 4719 /* physical/logical sector size word is valid */ 4720 4721 if (sdinfo->satadrv_id.ai_phys_sect_sz & 4722 SATA_L2PS_HAS_MULT) { 4723 /* multiple logical sectors per phys sectors */ 4724 l2p_exp = 4725 sdinfo->satadrv_id.ai_phys_sect_sz & 4726 SATA_L2PS_EXP_MASK; 4727 } 4728 4729 if (sdinfo->satadrv_id.ai_phys_sect_sz & 4730 SATA_L2PS_BIG_SECTORS) { 4731 /* if this set 117-118 words are valid */ 4732 lbsize = sdinfo->satadrv_id.ai_words_lsec[0] | 4733 (sdinfo->satadrv_id.ai_words_lsec[1] << 16); 4734 lbsize <<= 1; /* convert from words to bytes */ 4735 } 4736 } 4737 4738 rbuf = (uchar_t *)bp->b_un.b_addr; 4739 bzero(rbuf, bp->b_bcount); 4740 4741 /* returned logical block address */ 4742 rbuf[0] = (val >> 56) & 0xff; 4743 rbuf[1] = (val >> 48) & 0xff; 4744 rbuf[2] = (val >> 40) & 0xff; 4745 rbuf[3] = (val >> 32) & 0xff; 4746 rbuf[4] = (val >> 24) & 0xff; 4747 rbuf[5] = (val >> 16) & 0xff; 4748 rbuf[6] = (val >> 8) & 0xff; 4749 rbuf[7] = val & 0xff; 4750 rbuf[8] = (lbsize >> 24) & 0xff; 4751 rbuf[9] = (lbsize >> 16) & 0xff; 4752 rbuf[10] = (lbsize >> 8) & 0xff; 4753 rbuf[11] = lbsize & 0xff; 4754 4755 /* p_type, prot_en, unspecified by SAT-2 */ 4756 /* rbuf[12] = 0; */ 4757 4758 /* p_i_exponent, undefined by SAT-2 */ 4759 /* logical blocks per physical block exponent */ 4760 rbuf[13] = l2p_exp; 4761 4762 /* 4763 * tpe and tprz as defined in T10/10-079 r0. 4764 * TRIM support is indicated by the relevant bit in the data 4765 * set management word. Read-after-trim behavior is indicated 4766 * by the additional bits in the identify device word. Of the 4767 * three defined possibilities, we only flag read-zero. 4768 */ 4769 if (sdinfo->satadrv_id.ai_dsm & SATA_DSM_TRIM) { 4770 rbuf[14] |= TPE; 4771 4772 if ((sdinfo->satadrv_id.ai_addsupported & 4773 SATA_DETERMINISTIC_READ) && 4774 (sdinfo->satadrv_id.ai_addsupported & 4775 SATA_READ_ZERO)) { 4776 rbuf[14] |= TPRZ; 4777 } 4778 } 4779 4780 /* lowest aligned logical block address = 0 (for now) */ 4781 /* rbuf[15] = 0; */ 4782 4783 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4784 scsipkt->pkt_resid = 0; 4785 4786 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%llu\n", 4787 sdinfo->satadrv_capacity -1); 4788 } 4789 4790 mutex_exit(cport_mutex); 4791 4792 /* 4793 * If a callback was requested, do it now. 4794 */ 4795 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4796 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4797 4798 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4799 scsipkt->pkt_comp != NULL) { 4800 /* scsi callback required */ 4801 if (servicing_interrupt()) { 4802 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4803 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4804 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 4805 TASKQID_INVALID) { 4806 return (TRAN_BUSY); 4807 } 4808 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4809 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4810 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 4811 /* Scheduling the callback failed */ 4812 return (TRAN_BUSY); 4813 } 4814 } 4815 4816 return (TRAN_ACCEPT); 4817 } 4818 4819 /* 4820 * Translate command: UNMAP 4821 * 4822 * The function cannot be called in interrupt context since it may sleep. 4823 */ 4824 static int 4825 sata_txlt_unmap(sata_pkt_txlate_t *spx) 4826 { 4827 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4828 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4829 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4830 uint16_t count = 0; 4831 int synch; 4832 int rval, reason; 4833 int i, x; 4834 int bdlen = 0; 4835 int ranges = 0; 4836 int paramlen = 8; 4837 uint8_t *data, *tmpbd; 4838 sata_drive_info_t *sdinfo; 4839 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 4840 #define TRIM 0x1 4841 4842 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4843 "sata_txlt_unmap: ", NULL); 4844 4845 mutex_enter(cport_mutex); 4846 4847 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4848 &spx->txlt_sata_pkt->satapkt_device); 4849 if (sdinfo != NULL) { 4850 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4851 "DSM support 0x%x, max number of 512 byte blocks of LBA " 4852 "range entries 0x%x\n", sdinfo->satadrv_id.ai_dsm, 4853 sdinfo->satadrv_id.ai_maxcount); 4854 } 4855 4856 rval = sata_txlt_generic_pkt_info(spx, &reason, 1); 4857 if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4858 mutex_exit(cport_mutex); 4859 return (rval); 4860 } 4861 4862 /* 4863 * Need to modify bp to have TRIM data instead of UNMAP data. 4864 * Start by getting the block descriptor data length by subtracting 4865 * the 8 byte parameter list header from the parameter list length. 4866 * The block descriptor size has to be a multiple of 16 bytes. 4867 */ 4868 bdlen = scsipkt->pkt_cdbp[7]; 4869 bdlen = (bdlen << 8) + scsipkt->pkt_cdbp[8] - paramlen; 4870 if ((bdlen < 0) || ((bdlen % 16) != 0) || 4871 ((bp != NULL) && (bdlen > (bp->b_bcount - paramlen)))) { 4872 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4873 "sata_txlt_unmap: invalid block descriptor length", NULL); 4874 mutex_exit(cport_mutex); 4875 return ((sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 4876 SD_SCSI_ASC_INVALID_FIELD_IN_CDB))); 4877 } 4878 /* 4879 * If there are no parameter data or block descriptors, it is not 4880 * considered an error so just complete the command without sending 4881 * TRIM. 4882 */ 4883 if ((bdlen == 0) || (bp == NULL) || (bp->b_un.b_addr == NULL) || 4884 (bp->b_bcount == 0)) { 4885 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4886 "sata_txlt_unmap: no parameter data or block descriptors", 4887 NULL); 4888 mutex_exit(cport_mutex); 4889 return (sata_txlt_unmap_nodata_cmd(spx)); 4890 } 4891 tmpbd = (uint8_t *)bp->b_un.b_addr + paramlen; 4892 data = kmem_zalloc(bdlen, KM_SLEEP); 4893 4894 /* 4895 * Loop through all the UNMAP block descriptors and convert the data 4896 * into TRIM format. 4897 */ 4898 for (i = 0, x = 0; i < bdlen; i += 16, x += 8) { 4899 /* get range length */ 4900 data[x] = tmpbd[i+7]; 4901 data[x+1] = tmpbd[i+6]; 4902 /* get LBA */ 4903 data[x+2] = tmpbd[i+5]; 4904 data[x+3] = tmpbd[i+4]; 4905 data[x+4] = tmpbd[i+3]; 4906 data[x+5] = tmpbd[i+2]; 4907 data[x+6] = tmpbd[i+11]; 4908 data[x+7] = tmpbd[i+10]; 4909 4910 ranges++; 4911 } 4912 4913 /* 4914 * The TRIM command expects the data buffer to be a multiple of 4915 * 512-byte blocks of range entries. This means that the UNMAP buffer 4916 * may be too small. Free the original DMA resources and create a 4917 * local buffer. 4918 */ 4919 sata_common_free_dma_rsrcs(spx); 4920 4921 /* 4922 * Get count of 512-byte blocks of range entries. The length 4923 * of a range entry is 8 bytes which means one count has 64 range 4924 * entries. 4925 */ 4926 count = (ranges + 63)/64; 4927 4928 /* Allocate a buffer that is a multiple of 512 bytes. */ 4929 mutex_exit(cport_mutex); 4930 bp = sata_alloc_local_buffer(spx, count * 512); 4931 if (bp == NULL) { 4932 SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst, 4933 "sata_txlt_unmap: " 4934 "cannot allocate buffer for TRIM command", NULL); 4935 kmem_free(data, bdlen); 4936 return (TRAN_BUSY); 4937 } 4938 bp_mapin(bp); /* make data buffer accessible */ 4939 mutex_enter(cport_mutex); 4940 4941 bzero(bp->b_un.b_addr, bp->b_bcount); 4942 bcopy(data, bp->b_un.b_addr, x); 4943 kmem_free(data, bdlen); 4944 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 4945 DDI_DMA_SYNC_FORDEV); 4946 ASSERT(rval == DDI_SUCCESS); 4947 4948 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 4949 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 4950 scmd->satacmd_cmd_reg = SATAC_DSM; 4951 scmd->satacmd_sec_count_msb = (count >> 8) & 0xff; 4952 scmd->satacmd_sec_count_lsb = count & 0xff; 4953 scmd->satacmd_features_reg = TRIM; 4954 scmd->satacmd_device_reg = SATA_ADH_LBA; 4955 scmd->satacmd_status_reg = 0; 4956 scmd->satacmd_error_reg = 0; 4957 4958 /* Start processing command */ 4959 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 4960 spx->txlt_sata_pkt->satapkt_comp = 4961 sata_txlt_unmap_completion; 4962 synch = FALSE; 4963 } else { 4964 synch = TRUE; 4965 } 4966 4967 if (sata_hba_start(spx, &rval) != 0) { 4968 mutex_exit(cport_mutex); 4969 return (rval); 4970 } 4971 4972 mutex_exit(cport_mutex); 4973 4974 if (synch) { 4975 sata_txlt_unmap_completion(spx->txlt_sata_pkt); 4976 } 4977 4978 return (TRAN_ACCEPT); 4979 } 4980 4981 /* 4982 * SATA translate command: Mode Sense. 4983 * Translated into appropriate SATA command or emulated. 4984 * Saved Values Page Control (03) are not supported. 4985 * 4986 * NOTE: only caching mode sense page is currently implemented. 4987 * 4988 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4989 */ 4990 4991 #define LLBAA 0x10 /* Long LBA Accepted */ 4992 4993 static int 4994 sata_txlt_mode_sense(sata_pkt_txlate_t *spx) 4995 { 4996 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4997 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4998 sata_drive_info_t *sdinfo; 4999 sata_id_t *sata_id; 5000 struct scsi_extended_sense *sense; 5001 int len, bdlen, count, alc_len; 5002 int pc; /* Page Control code */ 5003 uint8_t *buf; /* mode sense buffer */ 5004 int rval, reason; 5005 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 5006 5007 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5008 "sata_txlt_mode_sense, pc %x page code 0x%02x\n", 5009 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 5010 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 5011 5012 if (servicing_interrupt()) { 5013 buf = kmem_zalloc(1024, KM_NOSLEEP); 5014 if (buf == NULL) { 5015 return (TRAN_BUSY); 5016 } 5017 } else { 5018 buf = kmem_zalloc(1024, KM_SLEEP); 5019 } 5020 5021 mutex_enter(cport_mutex); 5022 5023 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 5024 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5025 mutex_exit(cport_mutex); 5026 kmem_free(buf, 1024); 5027 return (rval); 5028 } 5029 5030 scsipkt->pkt_reason = CMD_CMPLT; 5031 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5032 STATE_SENT_CMD | STATE_GOT_STATUS; 5033 5034 pc = scsipkt->pkt_cdbp[2] >> 6; 5035 5036 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 5037 /* 5038 * Because it is fully emulated command storing data 5039 * programatically in the specified buffer, release 5040 * preallocated DMA resources before storing data in the buffer, 5041 * so no unwanted DMA sync would take place. 5042 */ 5043 sata_scsi_dmafree(NULL, scsipkt); 5044 5045 len = 0; 5046 bdlen = 0; 5047 if (!(scsipkt->pkt_cdbp[1] & 8)) { 5048 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 && 5049 (scsipkt->pkt_cdbp[1] & LLBAA)) 5050 bdlen = 16; 5051 else 5052 bdlen = 8; 5053 } 5054 /* Build mode parameter header */ 5055 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 5056 /* 4-byte mode parameter header */ 5057 buf[len++] = 0; /* mode data length */ 5058 buf[len++] = 0; /* medium type */ 5059 buf[len++] = 0; /* dev-specific param */ 5060 buf[len++] = bdlen; /* Block Descriptor length */ 5061 } else { 5062 /* 8-byte mode parameter header */ 5063 buf[len++] = 0; /* mode data length */ 5064 buf[len++] = 0; 5065 buf[len++] = 0; /* medium type */ 5066 buf[len++] = 0; /* dev-specific param */ 5067 if (bdlen == 16) 5068 buf[len++] = 1; /* long lba descriptor */ 5069 else 5070 buf[len++] = 0; 5071 buf[len++] = 0; 5072 buf[len++] = 0; /* Block Descriptor length */ 5073 buf[len++] = bdlen; 5074 } 5075 5076 sdinfo = sata_get_device_info( 5077 spx->txlt_sata_hba_inst, 5078 &spx->txlt_sata_pkt->satapkt_device); 5079 5080 /* Build block descriptor only if not disabled (DBD) */ 5081 if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) { 5082 /* Block descriptor - direct-access device format */ 5083 if (bdlen == 8) { 5084 /* build regular block descriptor */ 5085 buf[len++] = 5086 (sdinfo->satadrv_capacity >> 24) & 0xff; 5087 buf[len++] = 5088 (sdinfo->satadrv_capacity >> 16) & 0xff; 5089 buf[len++] = 5090 (sdinfo->satadrv_capacity >> 8) & 0xff; 5091 buf[len++] = sdinfo->satadrv_capacity & 0xff; 5092 buf[len++] = 0; /* density code */ 5093 buf[len++] = 0; 5094 if (sdinfo->satadrv_type == 5095 SATA_DTYPE_ATADISK) 5096 buf[len++] = 2; 5097 else 5098 /* ATAPI */ 5099 buf[len++] = 8; 5100 buf[len++] = 0; 5101 } else if (bdlen == 16) { 5102 /* Long LBA Accepted */ 5103 /* build long lba block descriptor */ 5104 #ifndef __lock_lint 5105 buf[len++] = 5106 (sdinfo->satadrv_capacity >> 56) & 0xff; 5107 buf[len++] = 5108 (sdinfo->satadrv_capacity >> 48) & 0xff; 5109 buf[len++] = 5110 (sdinfo->satadrv_capacity >> 40) & 0xff; 5111 buf[len++] = 5112 (sdinfo->satadrv_capacity >> 32) & 0xff; 5113 #endif 5114 buf[len++] = 5115 (sdinfo->satadrv_capacity >> 24) & 0xff; 5116 buf[len++] = 5117 (sdinfo->satadrv_capacity >> 16) & 0xff; 5118 buf[len++] = 5119 (sdinfo->satadrv_capacity >> 8) & 0xff; 5120 buf[len++] = sdinfo->satadrv_capacity & 0xff; 5121 buf[len++] = 0; 5122 buf[len++] = 0; /* density code */ 5123 buf[len++] = 0; 5124 buf[len++] = 0; 5125 if (sdinfo->satadrv_type == 5126 SATA_DTYPE_ATADISK) 5127 buf[len++] = 2; 5128 else 5129 /* ATAPI */ 5130 buf[len++] = 8; 5131 buf[len++] = 0; 5132 } 5133 } 5134 5135 sata_id = &sdinfo->satadrv_id; 5136 5137 /* 5138 * Add requested pages. 5139 * Page 3 and 4 are obsolete and we are not supporting them. 5140 * We deal now with: 5141 * caching (read/write cache control). 5142 * We should eventually deal with following mode pages: 5143 * error recovery (0x01), 5144 * power condition (0x1a), 5145 * exception control page (enables SMART) (0x1c), 5146 * enclosure management (ses), 5147 * protocol-specific port mode (port control). 5148 */ 5149 switch (scsipkt->pkt_cdbp[2] & 0x3f) { 5150 case MODEPAGE_RW_ERRRECOV: 5151 /* DAD_MODE_ERR_RECOV */ 5152 /* R/W recovery */ 5153 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 5154 break; 5155 case MODEPAGE_CACHING: 5156 /* DAD_MODE_CACHE */ 5157 /* Reject not supported request for saved parameters */ 5158 if (pc == 3) { 5159 *scsipkt->pkt_scbp = STATUS_CHECK; 5160 sense = sata_arq_sense(spx); 5161 sense->es_key = KEY_ILLEGAL_REQUEST; 5162 sense->es_add_code = 5163 SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED; 5164 goto done; 5165 } 5166 5167 /* caching */ 5168 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 5169 break; 5170 case MODEPAGE_INFO_EXCPT: 5171 /* exception cntrl */ 5172 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 5173 len += sata_build_msense_page_1c(sdinfo, pc, 5174 buf+len); 5175 } 5176 else 5177 goto err; 5178 break; 5179 case MODEPAGE_POWER_COND: 5180 /* DAD_MODE_POWER_COND */ 5181 /* power condition */ 5182 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 5183 break; 5184 5185 case MODEPAGE_ACOUSTIC_MANAG: 5186 /* acoustic management */ 5187 len += sata_build_msense_page_30(sdinfo, pc, buf+len); 5188 break; 5189 case MODEPAGE_ALLPAGES: 5190 /* all pages */ 5191 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 5192 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 5193 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 5194 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 5195 len += sata_build_msense_page_1c(sdinfo, pc, 5196 buf+len); 5197 } 5198 len += sata_build_msense_page_30(sdinfo, pc, buf+len); 5199 break; 5200 default: 5201 err: 5202 /* Invalid request */ 5203 *scsipkt->pkt_scbp = STATUS_CHECK; 5204 sense = sata_arq_sense(spx); 5205 sense->es_key = KEY_ILLEGAL_REQUEST; 5206 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5207 goto done; 5208 } 5209 5210 /* fix total mode data length */ 5211 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 5212 /* 4-byte mode parameter header */ 5213 buf[0] = len - 1; /* mode data length */ 5214 } else { 5215 buf[0] = (len -2) >> 8; 5216 buf[1] = (len -2) & 0xff; 5217 } 5218 5219 5220 /* Check allocation length */ 5221 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 5222 alc_len = scsipkt->pkt_cdbp[4]; 5223 } else { 5224 alc_len = scsipkt->pkt_cdbp[7]; 5225 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 5226 } 5227 /* 5228 * We do not check for possible parameters truncation 5229 * (alc_len < len) assuming that the target driver works 5230 * correctly. Just avoiding overrun. 5231 * Copy no more than requested and possible, buffer-wise. 5232 */ 5233 count = MIN(alc_len, len); 5234 count = MIN(bp->b_bcount, count); 5235 bcopy(buf, bp->b_un.b_addr, count); 5236 5237 scsipkt->pkt_state |= STATE_XFERRED_DATA; 5238 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 5239 } 5240 *scsipkt->pkt_scbp = STATUS_GOOD; 5241 done: 5242 mutex_exit(cport_mutex); 5243 (void) kmem_free(buf, 1024); 5244 5245 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5246 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5247 5248 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5249 scsipkt->pkt_comp != NULL) { 5250 /* scsi callback required */ 5251 if (servicing_interrupt()) { 5252 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5253 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 5254 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 5255 TASKQID_INVALID) { 5256 return (TRAN_BUSY); 5257 } 5258 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5259 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 5260 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 5261 /* Scheduling the callback failed */ 5262 return (TRAN_BUSY); 5263 } 5264 } 5265 5266 return (TRAN_ACCEPT); 5267 } 5268 5269 5270 /* 5271 * SATA translate command: Mode Select. 5272 * Translated into appropriate SATA command or emulated. 5273 * Saving parameters is not supported. 5274 * Changing device capacity is not supported (although theoretically 5275 * possible by executing SET FEATURES/SET MAX ADDRESS) 5276 * 5277 * Assumption is that the target driver is working correctly. 5278 * 5279 * More than one SATA command may be executed to perform operations specified 5280 * by mode select pages. The first error terminates further execution. 5281 * Operations performed successully are not backed-up in such case. 5282 * 5283 * NOTE: Implemented pages: 5284 * - caching page 5285 * - informational exception page 5286 * - acoustic management page 5287 * - power condition page 5288 * Caching setup is remembered so it could be re-stored in case of 5289 * an unexpected device reset. 5290 * 5291 * Returns TRAN_XXXX. 5292 * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields. 5293 */ 5294 5295 static int 5296 sata_txlt_mode_select(sata_pkt_txlate_t *spx) 5297 { 5298 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5299 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5300 struct scsi_extended_sense *sense; 5301 int len, pagelen, count, pllen; 5302 uint8_t *buf; /* mode select buffer */ 5303 int rval, stat, reason; 5304 uint_t nointr_flag; 5305 int dmod = 0; 5306 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 5307 5308 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5309 "sata_txlt_mode_select, pc %x page code 0x%02x\n", 5310 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 5311 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 5312 5313 mutex_enter(cport_mutex); 5314 5315 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 5316 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5317 mutex_exit(cport_mutex); 5318 return (rval); 5319 } 5320 5321 rval = TRAN_ACCEPT; 5322 5323 scsipkt->pkt_reason = CMD_CMPLT; 5324 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5325 STATE_SENT_CMD | STATE_GOT_STATUS; 5326 nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR; 5327 5328 /* Reject not supported request */ 5329 if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */ 5330 *scsipkt->pkt_scbp = STATUS_CHECK; 5331 sense = sata_arq_sense(spx); 5332 sense->es_key = KEY_ILLEGAL_REQUEST; 5333 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5334 goto done; 5335 } 5336 5337 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 5338 pllen = scsipkt->pkt_cdbp[4]; 5339 } else { 5340 pllen = scsipkt->pkt_cdbp[7]; 5341 pllen = (pllen << 8) | scsipkt->pkt_cdbp[7]; 5342 } 5343 5344 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 5345 5346 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) { 5347 buf = (uint8_t *)bp->b_un.b_addr; 5348 count = MIN(bp->b_bcount, pllen); 5349 scsipkt->pkt_state |= STATE_XFERRED_DATA; 5350 scsipkt->pkt_resid = 0; 5351 pllen = count; 5352 5353 /* 5354 * Check the header to skip the block descriptor(s) - we 5355 * do not support setting device capacity. 5356 * Existing macros do not recognize long LBA dscriptor, 5357 * hence manual calculation. 5358 */ 5359 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 5360 /* 6-bytes CMD, 4 bytes header */ 5361 if (count <= 4) 5362 goto done; /* header only */ 5363 len = buf[3] + 4; 5364 } else { 5365 /* 10-bytes CMD, 8 bytes header */ 5366 if (count <= 8) 5367 goto done; /* header only */ 5368 len = buf[6]; 5369 len = (len << 8) + buf[7] + 8; 5370 } 5371 if (len >= count) 5372 goto done; /* header + descriptor(s) only */ 5373 5374 pllen -= len; /* remaining data length */ 5375 5376 /* 5377 * We may be executing SATA command and want to execute it 5378 * in SYNCH mode, regardless of scsi_pkt setting. 5379 * Save scsi_pkt setting and indicate SYNCH mode 5380 */ 5381 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5382 scsipkt->pkt_comp != NULL) { 5383 scsipkt->pkt_flags |= FLAG_NOINTR; 5384 } 5385 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH; 5386 5387 /* 5388 * len is now the offset to a first mode select page 5389 * Process all pages 5390 */ 5391 while (pllen > 0) { 5392 switch ((int)buf[len]) { 5393 case MODEPAGE_CACHING: 5394 /* No support for SP (saving) */ 5395 if (scsipkt->pkt_cdbp[1] & 0x01) { 5396 *scsipkt->pkt_scbp = STATUS_CHECK; 5397 sense = sata_arq_sense(spx); 5398 sense->es_key = KEY_ILLEGAL_REQUEST; 5399 sense->es_add_code = 5400 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5401 goto done; 5402 } 5403 stat = sata_mode_select_page_8(spx, 5404 (struct mode_cache_scsi3 *)&buf[len], 5405 pllen, &pagelen, &rval, &dmod); 5406 /* 5407 * The pagelen value indicates the number of 5408 * parameter bytes already processed. 5409 * The rval is the return value from 5410 * sata_tran_start(). 5411 * The stat indicates the overall status of 5412 * the operation(s). 5413 */ 5414 if (stat != SATA_SUCCESS) 5415 /* 5416 * Page processing did not succeed - 5417 * all error info is already set-up, 5418 * just return 5419 */ 5420 pllen = 0; /* this breaks the loop */ 5421 else { 5422 len += pagelen; 5423 pllen -= pagelen; 5424 } 5425 break; 5426 5427 case MODEPAGE_INFO_EXCPT: 5428 stat = sata_mode_select_page_1c(spx, 5429 (struct mode_info_excpt_page *)&buf[len], 5430 pllen, &pagelen, &rval, &dmod); 5431 /* 5432 * The pagelen value indicates the number of 5433 * parameter bytes already processed. 5434 * The rval is the return value from 5435 * sata_tran_start(). 5436 * The stat indicates the overall status of 5437 * the operation(s). 5438 */ 5439 if (stat != SATA_SUCCESS) 5440 /* 5441 * Page processing did not succeed - 5442 * all error info is already set-up, 5443 * just return 5444 */ 5445 pllen = 0; /* this breaks the loop */ 5446 else { 5447 len += pagelen; 5448 pllen -= pagelen; 5449 } 5450 break; 5451 5452 case MODEPAGE_ACOUSTIC_MANAG: 5453 stat = sata_mode_select_page_30(spx, 5454 (struct mode_acoustic_management *) 5455 &buf[len], pllen, &pagelen, &rval, &dmod); 5456 /* 5457 * The pagelen value indicates the number of 5458 * parameter bytes already processed. 5459 * The rval is the return value from 5460 * sata_tran_start(). 5461 * The stat indicates the overall status of 5462 * the operation(s). 5463 */ 5464 if (stat != SATA_SUCCESS) 5465 /* 5466 * Page processing did not succeed - 5467 * all error info is already set-up, 5468 * just return 5469 */ 5470 pllen = 0; /* this breaks the loop */ 5471 else { 5472 len += pagelen; 5473 pllen -= pagelen; 5474 } 5475 5476 break; 5477 case MODEPAGE_POWER_COND: 5478 stat = sata_mode_select_page_1a(spx, 5479 (struct mode_info_power_cond *)&buf[len], 5480 pllen, &pagelen, &rval, &dmod); 5481 /* 5482 * The pagelen value indicates the number of 5483 * parameter bytes already processed. 5484 * The rval is the return value from 5485 * sata_tran_start(). 5486 * The stat indicates the overall status of 5487 * the operation(s). 5488 */ 5489 if (stat != SATA_SUCCESS) 5490 /* 5491 * Page processing did not succeed - 5492 * all error info is already set-up, 5493 * just return 5494 */ 5495 pllen = 0; /* this breaks the loop */ 5496 else { 5497 len += pagelen; 5498 pllen -= pagelen; 5499 } 5500 break; 5501 default: 5502 *scsipkt->pkt_scbp = STATUS_CHECK; 5503 sense = sata_arq_sense(spx); 5504 sense->es_key = KEY_ILLEGAL_REQUEST; 5505 sense->es_add_code = 5506 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 5507 goto done; 5508 } 5509 } 5510 } 5511 done: 5512 mutex_exit(cport_mutex); 5513 /* 5514 * If device parameters were modified, fetch and store the new 5515 * Identify Device data. Since port mutex could have been released 5516 * for accessing HBA driver, we need to re-check device existence. 5517 */ 5518 if (dmod != 0) { 5519 sata_drive_info_t new_sdinfo, *sdinfo; 5520 int rv = 0; 5521 5522 /* 5523 * Following statement has to be changed if this function is 5524 * used for devices other than SATA hard disks. 5525 */ 5526 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK; 5527 5528 new_sdinfo.satadrv_addr = 5529 spx->txlt_sata_pkt->satapkt_device.satadev_addr; 5530 rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst, 5531 &new_sdinfo); 5532 5533 mutex_enter(cport_mutex); 5534 /* 5535 * Since port mutex could have been released when 5536 * accessing HBA driver, we need to re-check that the 5537 * framework still holds the device info structure. 5538 */ 5539 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 5540 &spx->txlt_sata_pkt->satapkt_device); 5541 if (sdinfo != NULL) { 5542 /* 5543 * Device still has info structure in the 5544 * sata framework. Copy newly fetched info 5545 */ 5546 if (rv == 0) { 5547 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 5548 sata_save_drive_settings(sdinfo); 5549 } else { 5550 /* 5551 * Could not fetch new data - invalidate 5552 * sata_drive_info. That makes device 5553 * unusable. 5554 */ 5555 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 5556 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 5557 } 5558 } 5559 if (rv != 0 || sdinfo == NULL) { 5560 /* 5561 * This changes the overall mode select completion 5562 * reason to a failed one !!!!! 5563 */ 5564 *scsipkt->pkt_scbp = STATUS_CHECK; 5565 sense = sata_arq_sense(spx); 5566 scsipkt->pkt_reason = CMD_INCOMPLETE; 5567 rval = TRAN_ACCEPT; 5568 } 5569 mutex_exit(cport_mutex); 5570 } 5571 /* Restore the scsi pkt flags */ 5572 scsipkt->pkt_flags &= ~FLAG_NOINTR; 5573 scsipkt->pkt_flags |= nointr_flag; 5574 5575 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5576 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5577 5578 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5579 scsipkt->pkt_comp != NULL) { 5580 /* scsi callback required */ 5581 if (servicing_interrupt()) { 5582 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5583 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 5584 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 5585 TASKQID_INVALID) { 5586 return (TRAN_BUSY); 5587 } 5588 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5589 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 5590 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 5591 /* Scheduling the callback failed */ 5592 return (TRAN_BUSY); 5593 } 5594 } 5595 5596 return (rval); 5597 } 5598 5599 /* 5600 * Translate command: ATA Pass Through 5601 * Incomplete implementation. Only supports No-Data, PIO Data-In, and 5602 * PIO Data-Out protocols. Also supports CK_COND bit. 5603 * 5604 * Mapping of the incoming CDB bytes to the outgoing satacmd bytes is 5605 * described in Table 111 of SAT-2 (Draft 9). 5606 */ 5607 static int 5608 sata_txlt_ata_pass_thru(sata_pkt_txlate_t *spx) 5609 { 5610 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5611 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5612 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5613 int extend; 5614 uint64_t lba; 5615 uint16_t feature, sec_count; 5616 int t_len, synch; 5617 int rval, reason; 5618 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 5619 5620 mutex_enter(cport_mutex); 5621 5622 rval = sata_txlt_generic_pkt_info(spx, &reason, 1); 5623 if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5624 mutex_exit(cport_mutex); 5625 return (rval); 5626 } 5627 5628 /* T_DIR bit */ 5629 if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_T_DIR) 5630 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 5631 else 5632 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 5633 5634 /* MULTIPLE_COUNT field. If non-zero, invalid command (for now). */ 5635 if (((scsipkt->pkt_cdbp[1] >> 5) & 0x7) != 0) { 5636 mutex_exit(cport_mutex); 5637 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5638 } 5639 5640 /* OFFLINE field. If non-zero, invalid command (for now). */ 5641 if (((scsipkt->pkt_cdbp[2] >> 6) & 0x3) != 0) { 5642 mutex_exit(cport_mutex); 5643 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5644 } 5645 5646 /* PROTOCOL field */ 5647 switch ((scsipkt->pkt_cdbp[1] >> 1) & 0xf) { 5648 case SATL_APT_P_HW_RESET: 5649 case SATL_APT_P_SRST: 5650 case SATL_APT_P_DMA: 5651 case SATL_APT_P_DMA_QUEUED: 5652 case SATL_APT_P_DEV_DIAG: 5653 case SATL_APT_P_DEV_RESET: 5654 case SATL_APT_P_UDMA_IN: 5655 case SATL_APT_P_UDMA_OUT: 5656 case SATL_APT_P_FPDMA: 5657 case SATL_APT_P_RET_RESP: 5658 /* Not yet implemented */ 5659 default: 5660 mutex_exit(cport_mutex); 5661 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5662 5663 case SATL_APT_P_NON_DATA: 5664 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 5665 break; 5666 5667 case SATL_APT_P_PIO_DATA_IN: 5668 /* If PROTOCOL disagrees with T_DIR, invalid command */ 5669 if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_WRITE) { 5670 mutex_exit(cport_mutex); 5671 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5672 } 5673 5674 /* if there is a buffer, release its DMA resources */ 5675 if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) { 5676 sata_scsi_dmafree(NULL, scsipkt); 5677 } else { 5678 /* if there is no buffer, how do you PIO in? */ 5679 mutex_exit(cport_mutex); 5680 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5681 } 5682 5683 break; 5684 5685 case SATL_APT_P_PIO_DATA_OUT: 5686 /* If PROTOCOL disagrees with T_DIR, invalid command */ 5687 if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_READ) { 5688 mutex_exit(cport_mutex); 5689 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5690 } 5691 5692 /* if there is a buffer, release its DMA resources */ 5693 if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) { 5694 sata_scsi_dmafree(NULL, scsipkt); 5695 } else { 5696 /* if there is no buffer, how do you PIO out? */ 5697 mutex_exit(cport_mutex); 5698 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5699 } 5700 5701 break; 5702 } 5703 5704 /* Parse the ATA cmd fields, transfer some straight to the satacmd */ 5705 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 5706 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12: 5707 feature = scsipkt->pkt_cdbp[3]; 5708 5709 sec_count = scsipkt->pkt_cdbp[4]; 5710 5711 lba = scsipkt->pkt_cdbp[8] & 0xf; 5712 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 5713 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 5714 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5715 5716 scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] & 0xf0; 5717 scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[9]; 5718 5719 break; 5720 5721 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16: 5722 if (scsipkt->pkt_cdbp[1] & SATL_APT_BM_EXTEND) { 5723 extend = 1; 5724 5725 feature = scsipkt->pkt_cdbp[3]; 5726 feature = (feature << 8) | scsipkt->pkt_cdbp[4]; 5727 5728 sec_count = scsipkt->pkt_cdbp[5]; 5729 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[6]; 5730 5731 lba = scsipkt->pkt_cdbp[11]; 5732 lba = (lba << 8) | scsipkt->pkt_cdbp[12]; 5733 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 5734 lba = (lba << 8) | scsipkt->pkt_cdbp[10]; 5735 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 5736 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 5737 5738 scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13]; 5739 scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14]; 5740 } else { 5741 feature = scsipkt->pkt_cdbp[3]; 5742 5743 sec_count = scsipkt->pkt_cdbp[5]; 5744 5745 lba = scsipkt->pkt_cdbp[13] & 0xf; 5746 lba = (lba << 8) | scsipkt->pkt_cdbp[12]; 5747 lba = (lba << 8) | scsipkt->pkt_cdbp[10]; 5748 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 5749 5750 scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] & 5751 0xf0; 5752 scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14]; 5753 } 5754 5755 break; 5756 } 5757 5758 /* CK_COND bit */ 5759 if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) { 5760 if (extend) { 5761 scmd->satacmd_flags.sata_copy_out_sec_count_msb = 1; 5762 scmd->satacmd_flags.sata_copy_out_lba_low_msb = 1; 5763 scmd->satacmd_flags.sata_copy_out_lba_mid_msb = 1; 5764 scmd->satacmd_flags.sata_copy_out_lba_high_msb = 1; 5765 } 5766 5767 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1; 5768 scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1; 5769 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1; 5770 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1; 5771 scmd->satacmd_flags.sata_copy_out_device_reg = 1; 5772 scmd->satacmd_flags.sata_copy_out_error_reg = 1; 5773 } 5774 5775 /* Transfer remaining parsed ATA cmd values to the satacmd */ 5776 if (extend) { 5777 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 5778 5779 scmd->satacmd_features_reg_ext = (feature >> 8) & 0xff; 5780 scmd->satacmd_sec_count_msb = (sec_count >> 8) & 0xff; 5781 scmd->satacmd_lba_low_msb = (lba >> 8) & 0xff; 5782 scmd->satacmd_lba_mid_msb = (lba >> 8) & 0xff; 5783 scmd->satacmd_lba_high_msb = lba >> 40; 5784 } else { 5785 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 5786 5787 scmd->satacmd_features_reg_ext = 0; 5788 scmd->satacmd_sec_count_msb = 0; 5789 scmd->satacmd_lba_low_msb = 0; 5790 scmd->satacmd_lba_mid_msb = 0; 5791 scmd->satacmd_lba_high_msb = 0; 5792 } 5793 5794 scmd->satacmd_features_reg = feature & 0xff; 5795 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 5796 scmd->satacmd_lba_low_lsb = lba & 0xff; 5797 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 5798 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 5799 5800 /* Determine transfer length */ 5801 switch (scsipkt->pkt_cdbp[2] & 0x3) { /* T_LENGTH field */ 5802 case 1: 5803 t_len = feature; 5804 break; 5805 case 2: 5806 t_len = sec_count; 5807 break; 5808 default: 5809 t_len = 0; 5810 break; 5811 } 5812 5813 /* Adjust transfer length for the Byte Block bit */ 5814 if ((scsipkt->pkt_cdbp[2] >> 2) & 1) 5815 t_len *= SATA_DISK_SECTOR_SIZE; 5816 5817 /* Start processing command */ 5818 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5819 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_apt_completion; 5820 synch = FALSE; 5821 } else { 5822 synch = TRUE; 5823 } 5824 5825 if (sata_hba_start(spx, &rval) != 0) { 5826 mutex_exit(cport_mutex); 5827 return (rval); 5828 } 5829 5830 mutex_exit(cport_mutex); 5831 5832 if (synch) { 5833 sata_txlt_apt_completion(spx->txlt_sata_pkt); 5834 } 5835 5836 return (TRAN_ACCEPT); 5837 } 5838 5839 /* 5840 * Translate command: Log Sense 5841 */ 5842 static int 5843 sata_txlt_log_sense(sata_pkt_txlate_t *spx) 5844 { 5845 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5846 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5847 sata_drive_info_t *sdinfo; 5848 struct scsi_extended_sense *sense; 5849 int len, count, alc_len; 5850 int pc; /* Page Control code */ 5851 int page_code; /* Page code */ 5852 uint8_t *buf; /* log sense buffer */ 5853 int rval, reason; 5854 #define MAX_LOG_SENSE_PAGE_SIZE 512 5855 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 5856 5857 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5858 "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n", 5859 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 5860 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 5861 5862 if (servicing_interrupt()) { 5863 buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_NOSLEEP); 5864 if (buf == NULL) { 5865 return (TRAN_BUSY); 5866 } 5867 } else { 5868 buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP); 5869 } 5870 5871 mutex_enter(cport_mutex); 5872 5873 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 5874 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5875 mutex_exit(cport_mutex); 5876 kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 5877 return (rval); 5878 } 5879 5880 scsipkt->pkt_reason = CMD_CMPLT; 5881 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5882 STATE_SENT_CMD | STATE_GOT_STATUS; 5883 5884 pc = scsipkt->pkt_cdbp[2] >> 6; 5885 page_code = scsipkt->pkt_cdbp[2] & 0x3f; 5886 5887 /* Reject not supported request for all but cumulative values */ 5888 switch (pc) { 5889 case PC_CUMULATIVE_VALUES: 5890 break; 5891 default: 5892 *scsipkt->pkt_scbp = STATUS_CHECK; 5893 sense = sata_arq_sense(spx); 5894 sense->es_key = KEY_ILLEGAL_REQUEST; 5895 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5896 goto done; 5897 } 5898 5899 switch (page_code) { 5900 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 5901 case PAGE_CODE_SELF_TEST_RESULTS: 5902 case PAGE_CODE_INFORMATION_EXCEPTIONS: 5903 case PAGE_CODE_SMART_READ_DATA: 5904 case PAGE_CODE_START_STOP_CYCLE_COUNTER: 5905 break; 5906 default: 5907 *scsipkt->pkt_scbp = STATUS_CHECK; 5908 sense = sata_arq_sense(spx); 5909 sense->es_key = KEY_ILLEGAL_REQUEST; 5910 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5911 goto done; 5912 } 5913 5914 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 5915 /* 5916 * Because log sense uses local buffers for data retrieval from 5917 * the devices and sets the data programatically in the 5918 * original specified buffer, release preallocated DMA 5919 * resources before storing data in the original buffer, 5920 * so no unwanted DMA sync would take place. 5921 */ 5922 sata_id_t *sata_id; 5923 5924 sata_scsi_dmafree(NULL, scsipkt); 5925 5926 len = 0; 5927 5928 /* Build log parameter header */ 5929 buf[len++] = page_code; /* page code as in the CDB */ 5930 buf[len++] = 0; /* reserved */ 5931 buf[len++] = 0; /* Zero out page length for now (MSB) */ 5932 buf[len++] = 0; /* (LSB) */ 5933 5934 sdinfo = sata_get_device_info( 5935 spx->txlt_sata_hba_inst, 5936 &spx->txlt_sata_pkt->satapkt_device); 5937 5938 /* 5939 * Add requested pages. 5940 */ 5941 switch (page_code) { 5942 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 5943 len = sata_build_lsense_page_0(sdinfo, buf + len); 5944 break; 5945 case PAGE_CODE_SELF_TEST_RESULTS: 5946 sata_id = &sdinfo->satadrv_id; 5947 if ((! (sata_id->ai_cmdset84 & 5948 SATA_SMART_SELF_TEST_SUPPORTED)) || 5949 (! (sata_id->ai_features87 & 5950 SATA_SMART_SELF_TEST_SUPPORTED))) { 5951 *scsipkt->pkt_scbp = STATUS_CHECK; 5952 sense = sata_arq_sense(spx); 5953 sense->es_key = KEY_ILLEGAL_REQUEST; 5954 sense->es_add_code = 5955 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5956 5957 goto done; 5958 } 5959 len = sata_build_lsense_page_10(sdinfo, buf + len, 5960 spx->txlt_sata_hba_inst); 5961 break; 5962 case PAGE_CODE_INFORMATION_EXCEPTIONS: 5963 sata_id = &sdinfo->satadrv_id; 5964 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 5965 *scsipkt->pkt_scbp = STATUS_CHECK; 5966 sense = sata_arq_sense(spx); 5967 sense->es_key = KEY_ILLEGAL_REQUEST; 5968 sense->es_add_code = 5969 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5970 5971 goto done; 5972 } 5973 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 5974 *scsipkt->pkt_scbp = STATUS_CHECK; 5975 sense = sata_arq_sense(spx); 5976 sense->es_key = KEY_ABORTED_COMMAND; 5977 sense->es_add_code = 5978 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 5979 sense->es_qual_code = 5980 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 5981 5982 goto done; 5983 } 5984 5985 len = sata_build_lsense_page_2f(sdinfo, buf + len, 5986 spx->txlt_sata_hba_inst); 5987 break; 5988 case PAGE_CODE_SMART_READ_DATA: 5989 sata_id = &sdinfo->satadrv_id; 5990 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 5991 *scsipkt->pkt_scbp = STATUS_CHECK; 5992 sense = sata_arq_sense(spx); 5993 sense->es_key = KEY_ILLEGAL_REQUEST; 5994 sense->es_add_code = 5995 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5996 5997 goto done; 5998 } 5999 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 6000 *scsipkt->pkt_scbp = STATUS_CHECK; 6001 sense = sata_arq_sense(spx); 6002 sense->es_key = KEY_ABORTED_COMMAND; 6003 sense->es_add_code = 6004 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 6005 sense->es_qual_code = 6006 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 6007 6008 goto done; 6009 } 6010 6011 /* This page doesn't include a page header */ 6012 len = sata_build_lsense_page_30(sdinfo, buf, 6013 spx->txlt_sata_hba_inst); 6014 goto no_header; 6015 case PAGE_CODE_START_STOP_CYCLE_COUNTER: 6016 sata_id = &sdinfo->satadrv_id; 6017 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 6018 *scsipkt->pkt_scbp = STATUS_CHECK; 6019 sense = sata_arq_sense(spx); 6020 sense->es_key = KEY_ILLEGAL_REQUEST; 6021 sense->es_add_code = 6022 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 6023 6024 goto done; 6025 } 6026 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 6027 *scsipkt->pkt_scbp = STATUS_CHECK; 6028 sense = sata_arq_sense(spx); 6029 sense->es_key = KEY_ABORTED_COMMAND; 6030 sense->es_add_code = 6031 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 6032 sense->es_qual_code = 6033 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 6034 6035 goto done; 6036 } 6037 len = sata_build_lsense_page_0e(sdinfo, buf, spx); 6038 goto no_header; 6039 default: 6040 /* Invalid request */ 6041 *scsipkt->pkt_scbp = STATUS_CHECK; 6042 sense = sata_arq_sense(spx); 6043 sense->es_key = KEY_ILLEGAL_REQUEST; 6044 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 6045 goto done; 6046 } 6047 6048 /* set parameter log sense data length */ 6049 buf[2] = len >> 8; /* log sense length (MSB) */ 6050 buf[3] = len & 0xff; /* log sense length (LSB) */ 6051 6052 len += SCSI_LOG_PAGE_HDR_LEN; 6053 ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE); 6054 6055 no_header: 6056 /* Check allocation length */ 6057 alc_len = scsipkt->pkt_cdbp[7]; 6058 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 6059 6060 /* 6061 * We do not check for possible parameters truncation 6062 * (alc_len < len) assuming that the target driver works 6063 * correctly. Just avoiding overrun. 6064 * Copy no more than requested and possible, buffer-wise. 6065 */ 6066 count = MIN(alc_len, len); 6067 count = MIN(bp->b_bcount, count); 6068 bcopy(buf, bp->b_un.b_addr, count); 6069 6070 scsipkt->pkt_state |= STATE_XFERRED_DATA; 6071 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 6072 } 6073 *scsipkt->pkt_scbp = STATUS_GOOD; 6074 done: 6075 mutex_exit(cport_mutex); 6076 (void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 6077 6078 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6079 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6080 6081 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6082 scsipkt->pkt_comp != NULL) { 6083 /* scsi callback required */ 6084 if (servicing_interrupt()) { 6085 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6086 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 6087 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 6088 TASKQID_INVALID) { 6089 return (TRAN_BUSY); 6090 } 6091 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6092 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 6093 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 6094 /* Scheduling the callback failed */ 6095 return (TRAN_BUSY); 6096 } 6097 } 6098 6099 return (TRAN_ACCEPT); 6100 } 6101 6102 /* 6103 * Translate command: Log Select 6104 * Not implemented at this time - returns invalid command response. 6105 */ 6106 static int 6107 sata_txlt_log_select(sata_pkt_txlate_t *spx) 6108 { 6109 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6110 "sata_txlt_log_select\n", NULL); 6111 6112 return (sata_txlt_invalid_command(spx)); 6113 } 6114 6115 6116 /* 6117 * Translate command: Read (various types). 6118 * Translated into appropriate type of ATA READ command 6119 * for SATA hard disks. 6120 * Both the device capabilities and requested operation mode are 6121 * considered. 6122 * 6123 * Following scsi cdb fields are ignored: 6124 * rdprotect, dpo, fua, fua_nv, group_number. 6125 * 6126 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 6127 * enable variable sata_func_enable), the capability of the controller and 6128 * capability of a device are checked and if both support queueing, read 6129 * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT 6130 * command rather than plain READ_XXX command. 6131 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 6132 * both the controller and device suport such functionality, the read 6133 * request will be translated to READ_FPDMA_QUEUED command. 6134 * In both cases the maximum queue depth is derived as minimum of: 6135 * HBA capability,device capability and sata_max_queue_depth variable setting. 6136 * The value passed to HBA driver is decremented by 1, because only 5 bits are 6137 * used to pass max queue depth value, and the maximum possible queue depth 6138 * is 32. 6139 * 6140 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 6141 * appropriate values in scsi_pkt fields. 6142 */ 6143 static int 6144 sata_txlt_read(sata_pkt_txlate_t *spx) 6145 { 6146 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6147 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6148 sata_drive_info_t *sdinfo; 6149 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 6150 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 6151 uint16_t sec_count; 6152 uint64_t lba; 6153 int rval, reason; 6154 int synch; 6155 6156 mutex_enter(cport_mutex); 6157 6158 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 6159 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 6160 mutex_exit(cport_mutex); 6161 return (rval); 6162 } 6163 6164 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 6165 &spx->txlt_sata_pkt->satapkt_device); 6166 6167 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 6168 /* 6169 * Extract LBA and sector count from scsi CDB. 6170 */ 6171 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 6172 case SCMD_READ: 6173 /* 6-byte scsi read cmd : 0x08 */ 6174 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 6175 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 6176 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6177 sec_count = scsipkt->pkt_cdbp[4]; 6178 /* sec_count 0 will be interpreted as 256 by a device */ 6179 break; 6180 case SCMD_READ_G1: 6181 /* 10-bytes scsi read command : 0x28 */ 6182 lba = scsipkt->pkt_cdbp[2]; 6183 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6184 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 6185 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 6186 sec_count = scsipkt->pkt_cdbp[7]; 6187 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 6188 break; 6189 case SCMD_READ_G5: 6190 /* 12-bytes scsi read command : 0xA8 */ 6191 lba = scsipkt->pkt_cdbp[2]; 6192 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6193 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 6194 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 6195 sec_count = scsipkt->pkt_cdbp[6]; 6196 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 6197 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 6198 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 6199 break; 6200 case SCMD_READ_G4: 6201 /* 16-bytes scsi read command : 0x88 */ 6202 lba = scsipkt->pkt_cdbp[2]; 6203 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6204 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 6205 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 6206 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 6207 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 6208 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 6209 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 6210 sec_count = scsipkt->pkt_cdbp[10]; 6211 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 6212 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 6213 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 6214 break; 6215 default: 6216 /* Unsupported command */ 6217 mutex_exit(cport_mutex); 6218 return (sata_txlt_invalid_command(spx)); 6219 } 6220 6221 /* 6222 * Check if specified address exceeds device capacity 6223 */ 6224 if ((lba >= sdinfo->satadrv_capacity) || 6225 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 6226 /* LBA out of range */ 6227 mutex_exit(cport_mutex); 6228 return (sata_txlt_lba_out_of_range(spx)); 6229 } 6230 6231 /* 6232 * For zero-length transfer, emulate good completion of the command 6233 * (reasons for rejecting the command were already checked). 6234 * No DMA resources were allocated. 6235 */ 6236 if (spx->txlt_dma_cookie_list == NULL) { 6237 mutex_exit(cport_mutex); 6238 return (sata_emul_rw_completion(spx)); 6239 } 6240 6241 /* 6242 * Build cmd block depending on the device capability and 6243 * requested operation mode. 6244 * Do not bother with non-dma mode - we are working only with 6245 * devices supporting DMA. 6246 */ 6247 scmd->satacmd_addr_type = ATA_ADDR_LBA; 6248 scmd->satacmd_device_reg = SATA_ADH_LBA; 6249 scmd->satacmd_cmd_reg = SATAC_READ_DMA; 6250 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 6251 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 6252 scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT; 6253 scmd->satacmd_sec_count_msb = sec_count >> 8; 6254 #ifndef __lock_lint 6255 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 6256 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 6257 scmd->satacmd_lba_high_msb = lba >> 40; 6258 #endif 6259 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 6260 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 6261 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 6262 } 6263 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 6264 scmd->satacmd_lba_low_lsb = lba & 0xff; 6265 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 6266 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 6267 scmd->satacmd_features_reg = 0; 6268 scmd->satacmd_status_reg = 0; 6269 scmd->satacmd_error_reg = 0; 6270 6271 /* 6272 * Check if queueing commands should be used and switch 6273 * to appropriate command if possible 6274 */ 6275 if (sata_func_enable & SATA_ENABLE_QUEUING) { 6276 boolean_t using_queuing; 6277 6278 /* Queuing supported by controller and device? */ 6279 if ((sata_func_enable & SATA_ENABLE_NCQ) && 6280 (sdinfo->satadrv_features_support & 6281 SATA_DEV_F_NCQ) && 6282 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 6283 SATA_CTLF_NCQ)) { 6284 using_queuing = B_TRUE; 6285 6286 /* NCQ supported - use FPDMA READ */ 6287 scmd->satacmd_cmd_reg = 6288 SATAC_READ_FPDMA_QUEUED; 6289 scmd->satacmd_features_reg_ext = 6290 scmd->satacmd_sec_count_msb; 6291 scmd->satacmd_sec_count_msb = 0; 6292 } else if ((sdinfo->satadrv_features_support & 6293 SATA_DEV_F_TCQ) && 6294 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 6295 SATA_CTLF_QCMD)) { 6296 using_queuing = B_TRUE; 6297 6298 /* Legacy queueing */ 6299 if (sdinfo->satadrv_features_support & 6300 SATA_DEV_F_LBA48) { 6301 scmd->satacmd_cmd_reg = 6302 SATAC_READ_DMA_QUEUED_EXT; 6303 scmd->satacmd_features_reg_ext = 6304 scmd->satacmd_sec_count_msb; 6305 scmd->satacmd_sec_count_msb = 0; 6306 } else { 6307 scmd->satacmd_cmd_reg = 6308 SATAC_READ_DMA_QUEUED; 6309 } 6310 } else /* NCQ nor legacy queuing not supported */ 6311 using_queuing = B_FALSE; 6312 6313 /* 6314 * If queuing, the sector count goes in the features register 6315 * and the secount count will contain the tag. 6316 */ 6317 if (using_queuing) { 6318 scmd->satacmd_features_reg = 6319 scmd->satacmd_sec_count_lsb; 6320 scmd->satacmd_sec_count_lsb = 0; 6321 scmd->satacmd_flags.sata_queued = B_TRUE; 6322 6323 /* Set-up maximum queue depth */ 6324 scmd->satacmd_flags.sata_max_queue_depth = 6325 sdinfo->satadrv_max_queue_depth - 1; 6326 } else if (sdinfo->satadrv_features_enabled & 6327 SATA_DEV_F_E_UNTAGGED_QING) { 6328 /* 6329 * Although NCQ/TCQ is not enabled, untagged queuing 6330 * may be still used. 6331 * Set-up the maximum untagged queue depth. 6332 * Use controller's queue depth from sata_hba_tran. 6333 * SATA HBA drivers may ignore this value and rely on 6334 * the internal limits.For drivers that do not 6335 * ignore untaged queue depth, limit the value to 6336 * SATA_MAX_QUEUE_DEPTH (32), as this is the 6337 * largest value that can be passed via 6338 * satacmd_flags.sata_max_queue_depth. 6339 */ 6340 scmd->satacmd_flags.sata_max_queue_depth = 6341 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ? 6342 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1; 6343 6344 } else { 6345 scmd->satacmd_flags.sata_max_queue_depth = 0; 6346 } 6347 } else 6348 scmd->satacmd_flags.sata_max_queue_depth = 0; 6349 6350 SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst, 6351 "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n", 6352 scmd->satacmd_cmd_reg, lba, sec_count); 6353 6354 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 6355 /* Need callback function */ 6356 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 6357 synch = FALSE; 6358 } else 6359 synch = TRUE; 6360 6361 /* Transfer command to HBA */ 6362 if (sata_hba_start(spx, &rval) != 0) { 6363 /* Pkt not accepted for execution */ 6364 mutex_exit(cport_mutex); 6365 return (rval); 6366 } 6367 mutex_exit(cport_mutex); 6368 /* 6369 * If execution is non-synchronous, 6370 * a callback function will handle potential errors, translate 6371 * the response and will do a callback to a target driver. 6372 * If it was synchronous, check execution status using the same 6373 * framework callback. 6374 */ 6375 if (synch) { 6376 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6377 "synchronous execution status %x\n", 6378 spx->txlt_sata_pkt->satapkt_reason); 6379 sata_txlt_rw_completion(spx->txlt_sata_pkt); 6380 } 6381 return (TRAN_ACCEPT); 6382 } 6383 6384 6385 /* 6386 * SATA translate command: Write (various types) 6387 * Translated into appropriate type of ATA WRITE command 6388 * for SATA hard disks. 6389 * Both the device capabilities and requested operation mode are 6390 * considered. 6391 * 6392 * Following scsi cdb fields are ignored: 6393 * rwprotect, dpo, fua, fua_nv, group_number. 6394 * 6395 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 6396 * enable variable sata_func_enable), the capability of the controller and 6397 * capability of a device are checked and if both support queueing, write 6398 * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT 6399 * command rather than plain WRITE_XXX command. 6400 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 6401 * both the controller and device suport such functionality, the write 6402 * request will be translated to WRITE_FPDMA_QUEUED command. 6403 * In both cases the maximum queue depth is derived as minimum of: 6404 * HBA capability,device capability and sata_max_queue_depth variable setting. 6405 * The value passed to HBA driver is decremented by 1, because only 5 bits are 6406 * used to pass max queue depth value, and the maximum possible queue depth 6407 * is 32. 6408 * 6409 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 6410 * appropriate values in scsi_pkt fields. 6411 */ 6412 static int 6413 sata_txlt_write(sata_pkt_txlate_t *spx) 6414 { 6415 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6416 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6417 sata_drive_info_t *sdinfo; 6418 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 6419 uint16_t sec_count; 6420 uint64_t lba; 6421 int rval, reason; 6422 int synch; 6423 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 6424 6425 mutex_enter(cport_mutex); 6426 6427 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 6428 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 6429 mutex_exit(cport_mutex); 6430 return (rval); 6431 } 6432 6433 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 6434 &spx->txlt_sata_pkt->satapkt_device); 6435 6436 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 6437 /* 6438 * Extract LBA and sector count from scsi CDB 6439 */ 6440 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 6441 case SCMD_WRITE: 6442 /* 6-byte scsi read cmd : 0x0A */ 6443 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 6444 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 6445 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6446 sec_count = scsipkt->pkt_cdbp[4]; 6447 /* sec_count 0 will be interpreted as 256 by a device */ 6448 break; 6449 case SCMD_WRITE_G1: 6450 /* 10-bytes scsi write command : 0x2A */ 6451 lba = scsipkt->pkt_cdbp[2]; 6452 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6453 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 6454 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 6455 sec_count = scsipkt->pkt_cdbp[7]; 6456 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 6457 break; 6458 case SCMD_WRITE_G5: 6459 /* 12-bytes scsi read command : 0xAA */ 6460 lba = scsipkt->pkt_cdbp[2]; 6461 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6462 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 6463 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 6464 sec_count = scsipkt->pkt_cdbp[6]; 6465 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 6466 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 6467 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 6468 break; 6469 case SCMD_WRITE_G4: 6470 /* 16-bytes scsi write command : 0x8A */ 6471 lba = scsipkt->pkt_cdbp[2]; 6472 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6473 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 6474 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 6475 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 6476 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 6477 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 6478 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 6479 sec_count = scsipkt->pkt_cdbp[10]; 6480 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 6481 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 6482 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 6483 break; 6484 default: 6485 /* Unsupported command */ 6486 mutex_exit(cport_mutex); 6487 return (sata_txlt_invalid_command(spx)); 6488 } 6489 6490 /* 6491 * Check if specified address and length exceeds device capacity 6492 */ 6493 if ((lba >= sdinfo->satadrv_capacity) || 6494 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 6495 /* LBA out of range */ 6496 mutex_exit(cport_mutex); 6497 return (sata_txlt_lba_out_of_range(spx)); 6498 } 6499 6500 /* 6501 * For zero-length transfer, emulate good completion of the command 6502 * (reasons for rejecting the command were already checked). 6503 * No DMA resources were allocated. 6504 */ 6505 if (spx->txlt_dma_cookie_list == NULL) { 6506 mutex_exit(cport_mutex); 6507 return (sata_emul_rw_completion(spx)); 6508 } 6509 6510 /* 6511 * Build cmd block depending on the device capability and 6512 * requested operation mode. 6513 * Do not bother with non-dma mode- we are working only with 6514 * devices supporting DMA. 6515 */ 6516 scmd->satacmd_addr_type = ATA_ADDR_LBA; 6517 scmd->satacmd_device_reg = SATA_ADH_LBA; 6518 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA; 6519 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 6520 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 6521 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT; 6522 scmd->satacmd_sec_count_msb = sec_count >> 8; 6523 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 6524 #ifndef __lock_lint 6525 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 6526 scmd->satacmd_lba_high_msb = lba >> 40; 6527 #endif 6528 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 6529 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 6530 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 6531 } 6532 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 6533 scmd->satacmd_lba_low_lsb = lba & 0xff; 6534 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 6535 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 6536 scmd->satacmd_features_reg = 0; 6537 scmd->satacmd_status_reg = 0; 6538 scmd->satacmd_error_reg = 0; 6539 6540 /* 6541 * Check if queueing commands should be used and switch 6542 * to appropriate command if possible 6543 */ 6544 if (sata_func_enable & SATA_ENABLE_QUEUING) { 6545 boolean_t using_queuing; 6546 6547 /* Queuing supported by controller and device? */ 6548 if ((sata_func_enable & SATA_ENABLE_NCQ) && 6549 (sdinfo->satadrv_features_support & 6550 SATA_DEV_F_NCQ) && 6551 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 6552 SATA_CTLF_NCQ)) { 6553 using_queuing = B_TRUE; 6554 6555 /* NCQ supported - use FPDMA WRITE */ 6556 scmd->satacmd_cmd_reg = 6557 SATAC_WRITE_FPDMA_QUEUED; 6558 scmd->satacmd_features_reg_ext = 6559 scmd->satacmd_sec_count_msb; 6560 scmd->satacmd_sec_count_msb = 0; 6561 } else if ((sdinfo->satadrv_features_support & 6562 SATA_DEV_F_TCQ) && 6563 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 6564 SATA_CTLF_QCMD)) { 6565 using_queuing = B_TRUE; 6566 6567 /* Legacy queueing */ 6568 if (sdinfo->satadrv_features_support & 6569 SATA_DEV_F_LBA48) { 6570 scmd->satacmd_cmd_reg = 6571 SATAC_WRITE_DMA_QUEUED_EXT; 6572 scmd->satacmd_features_reg_ext = 6573 scmd->satacmd_sec_count_msb; 6574 scmd->satacmd_sec_count_msb = 0; 6575 } else { 6576 scmd->satacmd_cmd_reg = 6577 SATAC_WRITE_DMA_QUEUED; 6578 } 6579 } else /* NCQ nor legacy queuing not supported */ 6580 using_queuing = B_FALSE; 6581 6582 if (using_queuing) { 6583 scmd->satacmd_features_reg = 6584 scmd->satacmd_sec_count_lsb; 6585 scmd->satacmd_sec_count_lsb = 0; 6586 scmd->satacmd_flags.sata_queued = B_TRUE; 6587 /* Set-up maximum queue depth */ 6588 scmd->satacmd_flags.sata_max_queue_depth = 6589 sdinfo->satadrv_max_queue_depth - 1; 6590 } else if (sdinfo->satadrv_features_enabled & 6591 SATA_DEV_F_E_UNTAGGED_QING) { 6592 /* 6593 * Although NCQ/TCQ is not enabled, untagged queuing 6594 * may be still used. 6595 * Set-up the maximum untagged queue depth. 6596 * Use controller's queue depth from sata_hba_tran. 6597 * SATA HBA drivers may ignore this value and rely on 6598 * the internal limits. For drivera that do not 6599 * ignore untaged queue depth, limit the value to 6600 * SATA_MAX_QUEUE_DEPTH (32), as this is the 6601 * largest value that can be passed via 6602 * satacmd_flags.sata_max_queue_depth. 6603 */ 6604 scmd->satacmd_flags.sata_max_queue_depth = 6605 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ? 6606 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1; 6607 6608 } else { 6609 scmd->satacmd_flags.sata_max_queue_depth = 0; 6610 } 6611 } else 6612 scmd->satacmd_flags.sata_max_queue_depth = 0; 6613 6614 SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6615 "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n", 6616 scmd->satacmd_cmd_reg, lba, sec_count); 6617 6618 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 6619 /* Need callback function */ 6620 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 6621 synch = FALSE; 6622 } else 6623 synch = TRUE; 6624 6625 /* Transfer command to HBA */ 6626 if (sata_hba_start(spx, &rval) != 0) { 6627 /* Pkt not accepted for execution */ 6628 mutex_exit(cport_mutex); 6629 return (rval); 6630 } 6631 mutex_exit(cport_mutex); 6632 6633 /* 6634 * If execution is non-synchronous, 6635 * a callback function will handle potential errors, translate 6636 * the response and will do a callback to a target driver. 6637 * If it was synchronous, check execution status using the same 6638 * framework callback. 6639 */ 6640 if (synch) { 6641 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6642 "synchronous execution status %x\n", 6643 spx->txlt_sata_pkt->satapkt_reason); 6644 sata_txlt_rw_completion(spx->txlt_sata_pkt); 6645 } 6646 return (TRAN_ACCEPT); 6647 } 6648 6649 6650 /* 6651 * Implements SCSI SBC WRITE BUFFER command download microcode option 6652 */ 6653 static int 6654 sata_txlt_write_buffer(sata_pkt_txlate_t *spx) 6655 { 6656 #define WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE 4 6657 #define WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE 5 6658 6659 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6660 struct sata_pkt *sata_pkt = spx->txlt_sata_pkt; 6661 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6662 6663 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 6664 struct scsi_extended_sense *sense; 6665 int rval, mode, sector_count, reason; 6666 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 6667 6668 mode = scsipkt->pkt_cdbp[1] & 0x1f; 6669 6670 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6671 "sata_txlt_write_buffer, mode 0x%x\n", mode); 6672 6673 mutex_enter(cport_mutex); 6674 6675 if ((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 6676 TRAN_ACCEPT) { 6677 mutex_exit(cport_mutex); 6678 return (rval); 6679 } 6680 6681 /* Use synchronous mode */ 6682 spx->txlt_sata_pkt->satapkt_op_mode 6683 |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 6684 6685 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 6686 6687 scsipkt->pkt_reason = CMD_CMPLT; 6688 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6689 STATE_SENT_CMD | STATE_GOT_STATUS; 6690 6691 /* 6692 * The SCSI to ATA translation specification only calls 6693 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE. 6694 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but 6695 * ATA 8 (draft) got rid of download microcode for temp 6696 * and it is even optional for ATA 7, so it may be aborted. 6697 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as 6698 * it is not specified and the buffer offset for SCSI is a 16-bit 6699 * value in bytes, but for ATA it is a 16-bit offset in 512 byte 6700 * sectors. Thus the offset really doesn't buy us anything. 6701 * If and when ATA 8 is stabilized and the SCSI to ATA specification 6702 * is revised, this can be revisisted. 6703 */ 6704 /* Reject not supported request */ 6705 switch (mode) { 6706 case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE: 6707 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP; 6708 break; 6709 case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE: 6710 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE; 6711 break; 6712 default: 6713 goto bad_param; 6714 } 6715 6716 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 6717 6718 scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE; 6719 if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0) 6720 goto bad_param; 6721 sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE; 6722 scmd->satacmd_sec_count_lsb = (uint8_t)sector_count; 6723 scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8; 6724 scmd->satacmd_lba_mid_lsb = 0; 6725 scmd->satacmd_lba_high_lsb = 0; 6726 scmd->satacmd_device_reg = 0; 6727 spx->txlt_sata_pkt->satapkt_comp = NULL; 6728 scmd->satacmd_addr_type = 0; 6729 6730 /* Transfer command to HBA */ 6731 if (sata_hba_start(spx, &rval) != 0) { 6732 /* Pkt not accepted for execution */ 6733 mutex_exit(cport_mutex); 6734 return (rval); 6735 } 6736 6737 mutex_exit(cport_mutex); 6738 6739 /* Then we need synchronous check the status of the disk */ 6740 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6741 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 6742 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 6743 scsipkt->pkt_reason = CMD_CMPLT; 6744 6745 /* Download commmand succeed, so probe and identify device */ 6746 sata_reidentify_device(spx); 6747 } else { 6748 /* Something went wrong, microcode download command failed */ 6749 scsipkt->pkt_reason = CMD_INCOMPLETE; 6750 *scsipkt->pkt_scbp = STATUS_CHECK; 6751 sense = sata_arq_sense(spx); 6752 switch (sata_pkt->satapkt_reason) { 6753 case SATA_PKT_PORT_ERROR: 6754 /* 6755 * We have no device data. Assume no data transfered. 6756 */ 6757 sense->es_key = KEY_HARDWARE_ERROR; 6758 break; 6759 6760 case SATA_PKT_DEV_ERROR: 6761 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 6762 SATA_STATUS_ERR) { 6763 /* 6764 * determine dev error reason from error 6765 * reg content 6766 */ 6767 sata_decode_device_error(spx, sense); 6768 break; 6769 } 6770 /* No extended sense key - no info available */ 6771 break; 6772 6773 case SATA_PKT_TIMEOUT: 6774 scsipkt->pkt_reason = CMD_TIMEOUT; 6775 scsipkt->pkt_statistics |= 6776 STAT_TIMEOUT | STAT_DEV_RESET; 6777 /* No extended sense key ? */ 6778 break; 6779 6780 case SATA_PKT_ABORTED: 6781 scsipkt->pkt_reason = CMD_ABORTED; 6782 scsipkt->pkt_statistics |= STAT_ABORTED; 6783 /* No extended sense key ? */ 6784 break; 6785 6786 case SATA_PKT_RESET: 6787 /* pkt aborted by an explicit reset from a host */ 6788 scsipkt->pkt_reason = CMD_RESET; 6789 scsipkt->pkt_statistics |= STAT_DEV_RESET; 6790 break; 6791 6792 default: 6793 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 6794 "sata_txlt_nodata_cmd_completion: " 6795 "invalid packet completion reason %d", 6796 sata_pkt->satapkt_reason)); 6797 scsipkt->pkt_reason = CMD_TRAN_ERR; 6798 break; 6799 } 6800 6801 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6802 "scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6803 6804 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 6805 /* scsi callback required */ 6806 scsi_hba_pkt_comp(scsipkt); 6807 } 6808 return (TRAN_ACCEPT); 6809 6810 bad_param: 6811 mutex_exit(cport_mutex); 6812 *scsipkt->pkt_scbp = STATUS_CHECK; 6813 sense = sata_arq_sense(spx); 6814 sense->es_key = KEY_ILLEGAL_REQUEST; 6815 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 6816 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6817 scsipkt->pkt_comp != NULL) { 6818 /* scsi callback required */ 6819 if (servicing_interrupt()) { 6820 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6821 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 6822 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 6823 TASKQID_INVALID) { 6824 return (TRAN_BUSY); 6825 } 6826 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6827 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 6828 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 6829 /* Scheduling the callback failed */ 6830 return (TRAN_BUSY); 6831 } 6832 } 6833 return (rval); 6834 } 6835 6836 /* 6837 * Re-identify device after doing a firmware download. 6838 */ 6839 static void 6840 sata_reidentify_device(sata_pkt_txlate_t *spx) 6841 { 6842 #define DOWNLOAD_WAIT_TIME_SECS 60 6843 #define DOWNLOAD_WAIT_INTERVAL_SECS 1 6844 int rval; 6845 int retry_cnt; 6846 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6847 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 6848 sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device; 6849 sata_drive_info_t *sdinfo; 6850 6851 /* 6852 * Before returning good status, probe device. 6853 * Device probing will get IDENTIFY DEVICE data, if possible. 6854 * The assumption is that the new microcode is applied by the 6855 * device. It is a caller responsibility to verify this. 6856 */ 6857 for (retry_cnt = 0; 6858 retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS; 6859 retry_cnt++) { 6860 rval = sata_probe_device(sata_hba_inst, &sata_device); 6861 6862 if (rval == SATA_SUCCESS) { /* Set default features */ 6863 sdinfo = sata_get_device_info(sata_hba_inst, 6864 &sata_device); 6865 if (sata_initialize_device(sata_hba_inst, sdinfo) != 6866 SATA_SUCCESS) { 6867 /* retry */ 6868 rval = sata_initialize_device(sata_hba_inst, 6869 sdinfo); 6870 if (rval == SATA_RETRY) 6871 sata_log(sata_hba_inst, CE_WARN, 6872 "SATA device at port %d pmport %d -" 6873 " default device features could not" 6874 " be set. Device may not operate " 6875 "as expected.", 6876 sata_device.satadev_addr.cport, 6877 sata_device.satadev_addr.pmport); 6878 } 6879 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 6880 scsi_hba_pkt_comp(scsipkt); 6881 return; 6882 } else if (rval == SATA_RETRY) { 6883 delay(drv_usectohz(1000000 * 6884 DOWNLOAD_WAIT_INTERVAL_SECS)); 6885 continue; 6886 } else /* failed - no reason to retry */ 6887 break; 6888 } 6889 6890 /* 6891 * Something went wrong, device probing failed. 6892 */ 6893 SATA_LOG_D((sata_hba_inst, CE_WARN, 6894 "Cannot probe device after downloading microcode\n")); 6895 6896 /* Reset device to force retrying the probe. */ 6897 (void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 6898 (SATA_DIP(sata_hba_inst), &sata_device); 6899 6900 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 6901 scsi_hba_pkt_comp(scsipkt); 6902 } 6903 6904 6905 /* 6906 * Translate command: Synchronize Cache. 6907 * Translates into Flush Cache command for SATA hard disks. 6908 * 6909 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 6910 * appropriate values in scsi_pkt fields. 6911 */ 6912 static int 6913 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx) 6914 { 6915 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6916 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 6917 int rval, reason; 6918 int synch; 6919 6920 mutex_enter(cport_mutex); 6921 6922 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 6923 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 6924 mutex_exit(cport_mutex); 6925 return (rval); 6926 } 6927 6928 scmd->satacmd_addr_type = 0; 6929 scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE; 6930 scmd->satacmd_device_reg = 0; 6931 scmd->satacmd_sec_count_lsb = 0; 6932 scmd->satacmd_lba_low_lsb = 0; 6933 scmd->satacmd_lba_mid_lsb = 0; 6934 scmd->satacmd_lba_high_lsb = 0; 6935 scmd->satacmd_features_reg = 0; 6936 scmd->satacmd_status_reg = 0; 6937 scmd->satacmd_error_reg = 0; 6938 6939 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6940 "sata_txlt_synchronize_cache\n", NULL); 6941 6942 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 6943 /* Need to set-up a callback function */ 6944 spx->txlt_sata_pkt->satapkt_comp = 6945 sata_txlt_nodata_cmd_completion; 6946 synch = FALSE; 6947 } else 6948 synch = TRUE; 6949 6950 /* Transfer command to HBA */ 6951 if (sata_hba_start(spx, &rval) != 0) { 6952 /* Pkt not accepted for execution */ 6953 mutex_exit(cport_mutex); 6954 return (rval); 6955 } 6956 mutex_exit(cport_mutex); 6957 6958 /* 6959 * If execution non-synchronous, it had to be completed 6960 * a callback function will handle potential errors, translate 6961 * the response and will do a callback to a target driver. 6962 * If it was synchronous, check status, using the same 6963 * framework callback. 6964 */ 6965 if (synch) { 6966 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6967 "synchronous execution status %x\n", 6968 spx->txlt_sata_pkt->satapkt_reason); 6969 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 6970 } 6971 return (TRAN_ACCEPT); 6972 } 6973 6974 6975 /* 6976 * Send pkt to SATA HBA driver 6977 * 6978 * This function may be called only if the operation is requested by scsi_pkt, 6979 * i.e. scsi_pkt is not NULL. 6980 * 6981 * This function has to be called with cport mutex held. It does release 6982 * the mutex when it calls HBA driver sata_tran_start function and 6983 * re-acquires it afterwards. 6984 * 6985 * If return value is 0, pkt was accepted, -1 otherwise 6986 * rval is set to appropriate sata_scsi_start return value. 6987 * 6988 * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not 6989 * have called the sata_pkt callback function for this packet. 6990 * 6991 * The scsi callback has to be performed by the caller of this routine. 6992 */ 6993 static int 6994 sata_hba_start(sata_pkt_txlate_t *spx, int *rval) 6995 { 6996 int stat; 6997 uint8_t cport = SATA_TXLT_CPORT(spx); 6998 uint8_t pmport = SATA_TXLT_PMPORT(spx); 6999 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 7000 sata_drive_info_t *sdinfo; 7001 sata_pmult_info_t *pminfo; 7002 sata_pmport_info_t *pmportinfo = NULL; 7003 sata_device_t *sata_device = NULL; 7004 uint8_t cmd; 7005 struct sata_cmd_flags cmd_flags; 7006 7007 ASSERT(spx->txlt_sata_pkt != NULL); 7008 7009 ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7010 7011 sdinfo = sata_get_device_info(sata_hba_inst, 7012 &spx->txlt_sata_pkt->satapkt_device); 7013 ASSERT(sdinfo != NULL); 7014 7015 /* Clear device reset state? */ 7016 /* qual should be XXX_DPMPORT, but add XXX_PMPORT in case */ 7017 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT || 7018 sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) { 7019 7020 /* 7021 * Get the pmult_info of the its parent port multiplier, all 7022 * sub-devices share a common device reset flags on in 7023 * pmult_info. 7024 */ 7025 pminfo = SATA_PMULT_INFO(sata_hba_inst, cport); 7026 pmportinfo = pminfo->pmult_dev_port[pmport]; 7027 ASSERT(pminfo != NULL); 7028 if (pminfo->pmult_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) { 7029 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 7030 sata_clear_dev_reset = B_TRUE; 7031 pminfo->pmult_event_flags &= 7032 ~SATA_EVNT_CLEAR_DEVICE_RESET; 7033 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 7034 "sata_hba_start: clearing device reset state" 7035 "on pmult.\n", NULL); 7036 } 7037 } else { 7038 if (sdinfo->satadrv_event_flags & 7039 SATA_EVNT_CLEAR_DEVICE_RESET) { 7040 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 7041 sata_clear_dev_reset = B_TRUE; 7042 sdinfo->satadrv_event_flags &= 7043 ~SATA_EVNT_CLEAR_DEVICE_RESET; 7044 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 7045 "sata_hba_start: clearing device reset state\n", 7046 NULL); 7047 } 7048 } 7049 7050 cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg; 7051 cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags; 7052 sata_device = &spx->txlt_sata_pkt->satapkt_device; 7053 7054 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7055 7056 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 7057 "Sata cmd 0x%2x\n", cmd); 7058 7059 stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 7060 spx->txlt_sata_pkt); 7061 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7062 /* 7063 * If sata pkt was accepted and executed in asynchronous mode, i.e. 7064 * with the sata callback, the sata_pkt could be already destroyed 7065 * by the time we check ther return status from the hba_start() 7066 * function, because sata_scsi_destroy_pkt() could have been already 7067 * called (perhaps in the interrupt context). So, in such case, there 7068 * should be no references to it. In other cases, sata_pkt still 7069 * exists. 7070 */ 7071 if (stat == SATA_TRAN_ACCEPTED) { 7072 /* 7073 * pkt accepted for execution. 7074 * If it was executed synchronously, it is already completed 7075 * and pkt completion_reason indicates completion status. 7076 */ 7077 *rval = TRAN_ACCEPT; 7078 return (0); 7079 } 7080 7081 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 7082 switch (stat) { 7083 case SATA_TRAN_QUEUE_FULL: 7084 /* 7085 * Controller detected queue full condition. 7086 */ 7087 SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst, 7088 "sata_hba_start: queue full\n", NULL); 7089 7090 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 7091 *spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL; 7092 7093 *rval = TRAN_BUSY; 7094 break; 7095 7096 case SATA_TRAN_PORT_ERROR: 7097 /* 7098 * Communication/link with device or general port error 7099 * detected before pkt execution begun. 7100 */ 7101 if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 7102 SATA_ADDR_CPORT || 7103 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 7104 SATA_ADDR_DCPORT) 7105 sata_log(sata_hba_inst, CE_CONT, 7106 "SATA port %d error", 7107 sata_device->satadev_addr.cport); 7108 else 7109 sata_log(sata_hba_inst, CE_CONT, 7110 "SATA port %d:%d error\n", 7111 sata_device->satadev_addr.cport, 7112 sata_device->satadev_addr.pmport); 7113 7114 /* 7115 * Update the port/device structure. 7116 * sata_pkt should be still valid. Since port error is 7117 * returned, sata_device content should reflect port 7118 * state - it means, that sata address have been changed, 7119 * because original packet's sata address refered to a device 7120 * attached to some port. 7121 */ 7122 if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT || 7123 sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) { 7124 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7125 mutex_enter(&pmportinfo->pmport_mutex); 7126 sata_update_pmport_info(sata_hba_inst, sata_device); 7127 mutex_exit(&pmportinfo->pmport_mutex); 7128 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7129 } else { 7130 sata_update_port_info(sata_hba_inst, sata_device); 7131 } 7132 7133 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 7134 *rval = TRAN_FATAL_ERROR; 7135 break; 7136 7137 case SATA_TRAN_CMD_UNSUPPORTED: 7138 /* 7139 * Command rejected by HBA as unsupported. It was HBA driver 7140 * that rejected the command, command was not sent to 7141 * an attached device. 7142 */ 7143 if ((sdinfo != NULL) && 7144 (sdinfo->satadrv_state & SATA_DSTATE_RESET)) 7145 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 7146 "sat_hba_start: cmd 0x%2x rejected " 7147 "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd); 7148 7149 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7150 (void) sata_txlt_invalid_command(spx); 7151 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7152 7153 *rval = TRAN_ACCEPT; 7154 break; 7155 7156 case SATA_TRAN_BUSY: 7157 /* 7158 * Command rejected by HBA because other operation prevents 7159 * accepting the packet, or device is in RESET condition. 7160 */ 7161 if (sdinfo != NULL) { 7162 sdinfo->satadrv_state = 7163 spx->txlt_sata_pkt->satapkt_device.satadev_state; 7164 7165 if (sdinfo->satadrv_state & SATA_DSTATE_RESET) { 7166 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 7167 "sata_hba_start: cmd 0x%2x rejected " 7168 "because of device reset condition\n", 7169 cmd); 7170 } else { 7171 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 7172 "sata_hba_start: cmd 0x%2x rejected " 7173 "with SATA_TRAN_BUSY status\n", 7174 cmd); 7175 } 7176 } 7177 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 7178 *rval = TRAN_BUSY; 7179 break; 7180 7181 default: 7182 /* Unrecognized HBA response */ 7183 SATA_LOG_D((sata_hba_inst, CE_WARN, 7184 "sata_hba_start: unrecognized HBA response " 7185 "to cmd : 0x%2x resp 0x%x", cmd, rval)); 7186 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 7187 *rval = TRAN_FATAL_ERROR; 7188 break; 7189 } 7190 7191 /* 7192 * If we got here, the packet was rejected. 7193 * Check if we need to remember reset state clearing request 7194 */ 7195 if (cmd_flags.sata_clear_dev_reset) { 7196 /* 7197 * Check if device is still configured - it may have 7198 * disapeared from the configuration 7199 */ 7200 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 7201 if (sdinfo != NULL) { 7202 /* 7203 * Restore the flag that requests clearing of 7204 * the device reset state, 7205 * so the next sata packet may carry it to HBA. 7206 */ 7207 if (sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT || 7208 sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT) { 7209 pminfo->pmult_event_flags |= 7210 SATA_EVNT_CLEAR_DEVICE_RESET; 7211 } else { 7212 sdinfo->satadrv_event_flags |= 7213 SATA_EVNT_CLEAR_DEVICE_RESET; 7214 } 7215 } 7216 } 7217 return (-1); 7218 } 7219 7220 /* 7221 * Scsi response setup for invalid LBA 7222 * 7223 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 7224 */ 7225 static int 7226 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx) 7227 { 7228 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7229 struct scsi_extended_sense *sense; 7230 7231 scsipkt->pkt_reason = CMD_CMPLT; 7232 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7233 STATE_SENT_CMD | STATE_GOT_STATUS; 7234 *scsipkt->pkt_scbp = STATUS_CHECK; 7235 7236 *scsipkt->pkt_scbp = STATUS_CHECK; 7237 sense = sata_arq_sense(spx); 7238 sense->es_key = KEY_ILLEGAL_REQUEST; 7239 sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE; 7240 7241 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 7242 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 7243 7244 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 7245 scsipkt->pkt_comp != NULL) { 7246 /* scsi callback required */ 7247 if (servicing_interrupt()) { 7248 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7249 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7250 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 7251 TASKQID_INVALID) { 7252 return (TRAN_BUSY); 7253 } 7254 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7255 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7256 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 7257 /* Scheduling the callback failed */ 7258 return (TRAN_BUSY); 7259 } 7260 } 7261 return (TRAN_ACCEPT); 7262 } 7263 7264 7265 /* 7266 * Analyze device status and error registers and translate them into 7267 * appropriate scsi sense codes. 7268 * NOTE: non-packet commands only for now 7269 */ 7270 static void 7271 sata_decode_device_error(sata_pkt_txlate_t *spx, 7272 struct scsi_extended_sense *sense) 7273 { 7274 uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg; 7275 7276 ASSERT(sense != NULL); 7277 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 7278 SATA_STATUS_ERR); 7279 7280 7281 if (err_reg & SATA_ERROR_ICRC) { 7282 sense->es_key = KEY_ABORTED_COMMAND; 7283 sense->es_add_code = 0x08; /* Communication failure */ 7284 return; 7285 } 7286 7287 if (err_reg & SATA_ERROR_UNC) { 7288 sense->es_key = KEY_MEDIUM_ERROR; 7289 /* Information bytes (LBA) need to be set by a caller */ 7290 return; 7291 } 7292 7293 /* ADD HERE: MC error bit handling for ATAPI CD/DVD */ 7294 if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) { 7295 sense->es_key = KEY_UNIT_ATTENTION; 7296 sense->es_add_code = 0x3a; /* No media present */ 7297 return; 7298 } 7299 7300 if (err_reg & SATA_ERROR_IDNF) { 7301 if (err_reg & SATA_ERROR_ABORT) { 7302 sense->es_key = KEY_ABORTED_COMMAND; 7303 } else { 7304 sense->es_key = KEY_ILLEGAL_REQUEST; 7305 sense->es_add_code = 0x21; /* LBA out of range */ 7306 } 7307 return; 7308 } 7309 7310 if (err_reg & SATA_ERROR_ABORT) { 7311 ASSERT(spx->txlt_sata_pkt != NULL); 7312 sense->es_key = KEY_ABORTED_COMMAND; 7313 return; 7314 } 7315 } 7316 7317 /* 7318 * Extract error LBA from sata_pkt.satapkt_cmd register fields 7319 */ 7320 static void 7321 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba) 7322 { 7323 sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd; 7324 7325 *lba = 0; 7326 if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) { 7327 *lba = sata_cmd->satacmd_lba_high_msb; 7328 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb; 7329 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb; 7330 } else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) { 7331 *lba = sata_cmd->satacmd_device_reg & 0xf; 7332 } 7333 *lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb; 7334 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb; 7335 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb; 7336 } 7337 7338 /* 7339 * This is fixed sense format - if LBA exceeds the info field size, 7340 * no valid info will be returned (valid bit in extended sense will 7341 * be set to 0). 7342 */ 7343 static struct scsi_extended_sense * 7344 sata_arq_sense(sata_pkt_txlate_t *spx) 7345 { 7346 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7347 struct scsi_arq_status *arqs; 7348 struct scsi_extended_sense *sense; 7349 7350 /* Fill ARQ sense data */ 7351 scsipkt->pkt_state |= STATE_ARQ_DONE; 7352 arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp; 7353 *(uchar_t *)&arqs->sts_status = STATUS_CHECK; 7354 *(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD; 7355 arqs->sts_rqpkt_reason = CMD_CMPLT; 7356 arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7357 STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS; 7358 arqs->sts_rqpkt_resid = 0; 7359 sense = &arqs->sts_sensedata; 7360 bzero(sense, sizeof (struct scsi_extended_sense)); 7361 sata_fixed_sense_data_preset(sense); 7362 return (sense); 7363 } 7364 7365 /* 7366 * ATA Pass Through support 7367 * Sets flags indicating that an invalid value was found in some 7368 * field in the command. It could be something illegal according to 7369 * the SAT-2 spec or it could be a feature that is not (yet?) 7370 * supported. 7371 */ 7372 static int 7373 sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *spx) 7374 { 7375 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7376 struct scsi_extended_sense *sense = sata_arq_sense(spx); 7377 7378 scsipkt->pkt_reason = CMD_CMPLT; 7379 *scsipkt->pkt_scbp = STATUS_CHECK; 7380 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7381 STATE_SENT_CMD | STATE_GOT_STATUS; 7382 7383 sense = sata_arq_sense(spx); 7384 sense->es_key = KEY_ILLEGAL_REQUEST; 7385 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 7386 7387 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 7388 scsipkt->pkt_comp != NULL) { 7389 /* scsi callback required */ 7390 if (servicing_interrupt()) { 7391 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7392 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7393 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 7394 TASKQID_INVALID) { 7395 return (TRAN_BUSY); 7396 } 7397 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7398 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7399 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 7400 /* Scheduling the callback failed */ 7401 return (TRAN_BUSY); 7402 } 7403 } 7404 7405 return (TRAN_ACCEPT); 7406 } 7407 7408 /* 7409 * The UNMAP command considers it not to be an error if the parameter length 7410 * or block descriptor length is 0. For this case, there is nothing for TRIM 7411 * to do so just complete the command. 7412 */ 7413 static int 7414 sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *spx) 7415 { 7416 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7417 7418 scsipkt->pkt_reason = CMD_CMPLT; 7419 *scsipkt->pkt_scbp = STATUS_GOOD; 7420 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7421 STATE_SENT_CMD | STATE_GOT_STATUS; 7422 7423 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 7424 scsipkt->pkt_comp != NULL) { 7425 /* scsi callback required */ 7426 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7427 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7428 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 7429 /* Scheduling the callback failed */ 7430 return (TRAN_BUSY); 7431 } 7432 } 7433 7434 return (TRAN_ACCEPT); 7435 } 7436 7437 /* 7438 * Emulated SATA Read/Write command completion for zero-length requests. 7439 * This request always succedes, so in synchronous mode it always returns 7440 * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the 7441 * callback cannot be scheduled. 7442 */ 7443 static int 7444 sata_emul_rw_completion(sata_pkt_txlate_t *spx) 7445 { 7446 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7447 7448 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7449 STATE_SENT_CMD | STATE_GOT_STATUS; 7450 scsipkt->pkt_reason = CMD_CMPLT; 7451 *scsipkt->pkt_scbp = STATUS_GOOD; 7452 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 7453 /* scsi callback required - have to schedule it */ 7454 if (servicing_interrupt()) { 7455 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7456 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7457 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 7458 TASKQID_INVALID) { 7459 return (TRAN_BUSY); 7460 } 7461 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7462 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7463 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 7464 /* Scheduling the callback failed */ 7465 return (TRAN_BUSY); 7466 } 7467 } 7468 return (TRAN_ACCEPT); 7469 } 7470 7471 7472 /* 7473 * Translate completion status of SATA read/write commands into scsi response. 7474 * pkt completion_reason is checked to determine the completion status. 7475 * Do scsi callback if necessary. 7476 * 7477 * Note: this function may be called also for synchronously executed 7478 * commands. 7479 * This function may be used only if scsi_pkt is non-NULL. 7480 */ 7481 static void 7482 sata_txlt_rw_completion(sata_pkt_t *sata_pkt) 7483 { 7484 sata_pkt_txlate_t *spx = 7485 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7486 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 7487 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7488 struct scsi_extended_sense *sense; 7489 uint64_t lba; 7490 struct buf *bp; 7491 int rval; 7492 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 7493 /* Normal completion */ 7494 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7495 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 7496 scsipkt->pkt_reason = CMD_CMPLT; 7497 *scsipkt->pkt_scbp = STATUS_GOOD; 7498 if (spx->txlt_tmp_buf != NULL) { 7499 /* Temporary buffer was used */ 7500 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 7501 if (bp->b_flags & B_READ) { 7502 rval = ddi_dma_sync( 7503 spx->txlt_buf_dma_handle, 0, 0, 7504 DDI_DMA_SYNC_FORCPU); 7505 ASSERT(rval == DDI_SUCCESS); 7506 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 7507 bp->b_bcount); 7508 } 7509 } 7510 } else { 7511 /* 7512 * Something went wrong - analyze return 7513 */ 7514 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7515 STATE_SENT_CMD | STATE_GOT_STATUS; 7516 scsipkt->pkt_reason = CMD_INCOMPLETE; 7517 *scsipkt->pkt_scbp = STATUS_CHECK; 7518 sense = sata_arq_sense(spx); 7519 ASSERT(sense != NULL); 7520 7521 /* 7522 * SATA_PKT_DEV_ERROR is the only case where we may be able to 7523 * extract from device registers the failing LBA. 7524 */ 7525 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 7526 if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) && 7527 (scmd->satacmd_lba_mid_msb != 0 || 7528 scmd->satacmd_lba_high_msb != 0)) { 7529 /* 7530 * We have problem reporting this cmd LBA 7531 * in fixed sense data format, because of 7532 * the size of the scsi LBA fields. 7533 */ 7534 sense->es_valid = 0; 7535 } else { 7536 sata_extract_error_lba(spx, &lba); 7537 sense->es_info_1 = (lba & 0xFF000000) >> 24; 7538 sense->es_info_2 = (lba & 0xFF0000) >> 16; 7539 sense->es_info_3 = (lba & 0xFF00) >> 8; 7540 sense->es_info_4 = lba & 0xFF; 7541 } 7542 } else { 7543 /* Invalid extended sense info */ 7544 sense->es_valid = 0; 7545 } 7546 7547 switch (sata_pkt->satapkt_reason) { 7548 case SATA_PKT_PORT_ERROR: 7549 /* We may want to handle DEV GONE state as well */ 7550 /* 7551 * We have no device data. Assume no data transfered. 7552 */ 7553 sense->es_key = KEY_HARDWARE_ERROR; 7554 break; 7555 7556 case SATA_PKT_DEV_ERROR: 7557 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 7558 SATA_STATUS_ERR) { 7559 /* 7560 * determine dev error reason from error 7561 * reg content 7562 */ 7563 sata_decode_device_error(spx, sense); 7564 if (sense->es_key == KEY_MEDIUM_ERROR) { 7565 switch (scmd->satacmd_cmd_reg) { 7566 case SATAC_READ_DMA: 7567 case SATAC_READ_DMA_EXT: 7568 case SATAC_READ_DMA_QUEUED: 7569 case SATAC_READ_DMA_QUEUED_EXT: 7570 case SATAC_READ_FPDMA_QUEUED: 7571 /* Unrecovered read error */ 7572 sense->es_add_code = 7573 SD_SCSI_ASC_UNREC_READ_ERR; 7574 break; 7575 case SATAC_WRITE_DMA: 7576 case SATAC_WRITE_DMA_EXT: 7577 case SATAC_WRITE_DMA_QUEUED: 7578 case SATAC_WRITE_DMA_QUEUED_EXT: 7579 case SATAC_WRITE_FPDMA_QUEUED: 7580 /* Write error */ 7581 sense->es_add_code = 7582 SD_SCSI_ASC_WRITE_ERR; 7583 break; 7584 default: 7585 /* Internal error */ 7586 SATA_LOG_D(( 7587 spx->txlt_sata_hba_inst, 7588 CE_WARN, 7589 "sata_txlt_rw_completion :" 7590 "internal error - invalid " 7591 "command 0x%2x", 7592 scmd->satacmd_cmd_reg)); 7593 break; 7594 } 7595 } 7596 break; 7597 } 7598 /* No extended sense key - no info available */ 7599 scsipkt->pkt_reason = CMD_INCOMPLETE; 7600 break; 7601 7602 case SATA_PKT_TIMEOUT: 7603 scsipkt->pkt_reason = CMD_TIMEOUT; 7604 scsipkt->pkt_statistics |= 7605 STAT_TIMEOUT | STAT_DEV_RESET; 7606 sense->es_key = KEY_ABORTED_COMMAND; 7607 break; 7608 7609 case SATA_PKT_ABORTED: 7610 scsipkt->pkt_reason = CMD_ABORTED; 7611 scsipkt->pkt_statistics |= STAT_ABORTED; 7612 sense->es_key = KEY_ABORTED_COMMAND; 7613 break; 7614 7615 case SATA_PKT_RESET: 7616 scsipkt->pkt_reason = CMD_RESET; 7617 scsipkt->pkt_statistics |= STAT_DEV_RESET; 7618 sense->es_key = KEY_ABORTED_COMMAND; 7619 break; 7620 7621 default: 7622 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 7623 "sata_txlt_rw_completion: " 7624 "invalid packet completion reason")); 7625 scsipkt->pkt_reason = CMD_TRAN_ERR; 7626 break; 7627 } 7628 } 7629 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 7630 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 7631 7632 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 7633 /* scsi callback required */ 7634 scsi_hba_pkt_comp(scsipkt); 7635 } 7636 7637 7638 /* 7639 * Translate completion status of non-data commands (i.e. commands returning 7640 * no data). 7641 * pkt completion_reason is checked to determine the completion status. 7642 * Do scsi callback if necessary (FLAG_NOINTR == 0) 7643 * 7644 * Note: this function may be called also for synchronously executed 7645 * commands. 7646 * This function may be used only if scsi_pkt is non-NULL. 7647 */ 7648 7649 static void 7650 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt) 7651 { 7652 sata_pkt_txlate_t *spx = 7653 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7654 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7655 7656 sata_set_arq_data(sata_pkt); 7657 7658 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 7659 /* scsi callback required */ 7660 scsi_hba_pkt_comp(scsipkt); 7661 } 7662 7663 /* 7664 * Completion handler for ATA Pass Through command 7665 */ 7666 static void 7667 sata_txlt_apt_completion(sata_pkt_t *sata_pkt) 7668 { 7669 sata_pkt_txlate_t *spx = 7670 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7671 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 7672 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7673 struct buf *bp; 7674 uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0; 7675 7676 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 7677 /* Normal completion */ 7678 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7679 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 7680 scsipkt->pkt_reason = CMD_CMPLT; 7681 *scsipkt->pkt_scbp = STATUS_GOOD; 7682 7683 /* 7684 * If the command has CK_COND set 7685 */ 7686 if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) { 7687 *scsipkt->pkt_scbp = STATUS_CHECK; 7688 sata_fill_ata_return_desc(sata_pkt, 7689 KEY_RECOVERABLE_ERROR, 7690 SD_SCSI_ASC_APT_INFO_AVAIL, 0x1d); 7691 } 7692 7693 if (spx->txlt_tmp_buf != NULL) { 7694 /* Temporary buffer was used */ 7695 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 7696 if (bp->b_flags & B_READ) { 7697 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 7698 bp->b_bcount); 7699 } 7700 } 7701 } else { 7702 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7703 STATE_SENT_CMD | STATE_GOT_STATUS; 7704 scsipkt->pkt_reason = CMD_INCOMPLETE; 7705 *scsipkt->pkt_scbp = STATUS_CHECK; 7706 7707 /* 7708 * If DF or ERR was set, the HBA should have copied out the 7709 * status and error registers to the satacmd structure. 7710 */ 7711 if (scmd->satacmd_status_reg & SATA_STATUS_DF) { 7712 sense_key = KEY_HARDWARE_ERROR; 7713 addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE; 7714 addl_sense_qual = 0; 7715 } else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) { 7716 if (scmd->satacmd_error_reg & SATA_ERROR_NM) { 7717 sense_key = KEY_NOT_READY; 7718 addl_sense_code = 7719 SD_SCSI_ASC_MEDIUM_NOT_PRESENT; 7720 addl_sense_qual = 0; 7721 } else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) { 7722 sense_key = KEY_MEDIUM_ERROR; 7723 addl_sense_code = SD_SCSI_ASC_UNREC_READ_ERR; 7724 addl_sense_qual = 0; 7725 } else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) { 7726 sense_key = KEY_DATA_PROTECT; 7727 addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED; 7728 addl_sense_qual = 0; 7729 } else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) { 7730 sense_key = KEY_ILLEGAL_REQUEST; 7731 addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE; 7732 addl_sense_qual = 0; 7733 } else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) { 7734 sense_key = KEY_ABORTED_COMMAND; 7735 addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE; 7736 addl_sense_qual = 0; 7737 } else if (scmd->satacmd_error_reg & SATA_ERROR_MC) { 7738 sense_key = KEY_UNIT_ATTENTION; 7739 addl_sense_code = 7740 SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED; 7741 addl_sense_qual = 0; 7742 } else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) { 7743 sense_key = KEY_UNIT_ATTENTION; 7744 addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ; 7745 addl_sense_qual = 0; 7746 } else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) { 7747 sense_key = KEY_ABORTED_COMMAND; 7748 addl_sense_code = 7749 SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR; 7750 addl_sense_qual = 0; 7751 } 7752 } 7753 7754 sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code, 7755 addl_sense_qual); 7756 } 7757 7758 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 7759 /* scsi callback required */ 7760 scsi_hba_pkt_comp(scsipkt); 7761 } 7762 7763 /* 7764 * Completion handler for unmap translation command 7765 */ 7766 static void 7767 sata_txlt_unmap_completion(sata_pkt_t *sata_pkt) 7768 { 7769 sata_pkt_txlate_t *spx = 7770 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7771 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 7772 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7773 struct buf *bp; 7774 uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0; 7775 7776 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 7777 /* Normal completion */ 7778 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7779 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 7780 scsipkt->pkt_reason = CMD_CMPLT; 7781 *scsipkt->pkt_scbp = STATUS_GOOD; 7782 7783 if (spx->txlt_tmp_buf != NULL) { 7784 /* Temporary buffer was used */ 7785 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 7786 if (bp->b_flags & B_READ) { 7787 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 7788 bp->b_bcount); 7789 } 7790 } 7791 } else { 7792 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7793 STATE_SENT_CMD | STATE_GOT_STATUS; 7794 scsipkt->pkt_reason = CMD_INCOMPLETE; 7795 *scsipkt->pkt_scbp = STATUS_CHECK; 7796 7797 /* 7798 * If DF or ERR was set, the HBA should have copied out the 7799 * status and error registers to the satacmd structure. 7800 */ 7801 if (scmd->satacmd_status_reg & SATA_STATUS_DF) { 7802 sense_key = KEY_HARDWARE_ERROR; 7803 addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE; 7804 addl_sense_qual = 0; 7805 } else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) { 7806 if (scmd->satacmd_error_reg & SATA_ERROR_NM) { 7807 sense_key = KEY_NOT_READY; 7808 addl_sense_code = 7809 SD_SCSI_ASC_MEDIUM_NOT_PRESENT; 7810 addl_sense_qual = 0; 7811 } else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) { 7812 sense_key = KEY_MEDIUM_ERROR; 7813 addl_sense_code = SD_SCSI_ASC_WRITE_ERR; 7814 addl_sense_qual = 0; 7815 } else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) { 7816 sense_key = KEY_DATA_PROTECT; 7817 addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED; 7818 addl_sense_qual = 0; 7819 } else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) { 7820 sense_key = KEY_ILLEGAL_REQUEST; 7821 addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE; 7822 addl_sense_qual = 0; 7823 } else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) { 7824 sense_key = KEY_ABORTED_COMMAND; 7825 addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE; 7826 addl_sense_qual = 0; 7827 } else if (scmd->satacmd_error_reg & SATA_ERROR_MC) { 7828 sense_key = KEY_UNIT_ATTENTION; 7829 addl_sense_code = 7830 SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED; 7831 addl_sense_qual = 0; 7832 } else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) { 7833 sense_key = KEY_UNIT_ATTENTION; 7834 addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ; 7835 addl_sense_qual = 0; 7836 } else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) { 7837 sense_key = KEY_ABORTED_COMMAND; 7838 addl_sense_code = 7839 SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR; 7840 addl_sense_qual = 0; 7841 } 7842 } 7843 7844 sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code, 7845 addl_sense_qual); 7846 } 7847 7848 sata_free_local_buffer(spx); 7849 7850 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 7851 /* scsi callback required */ 7852 scsi_hba_pkt_comp(scsipkt); 7853 } 7854 7855 /* 7856 * 7857 */ 7858 static void 7859 sata_fill_ata_return_desc(sata_pkt_t *sata_pkt, uint8_t sense_key, 7860 uint8_t addl_sense_code, uint8_t addl_sense_qual) 7861 { 7862 sata_pkt_txlate_t *spx = 7863 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7864 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 7865 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7866 struct sata_apt_sense_data *apt_sd = 7867 (struct sata_apt_sense_data *)scsipkt->pkt_scbp; 7868 struct scsi_descr_sense_hdr *sds = &(apt_sd->apt_sd_hdr); 7869 struct scsi_ata_status_ret_sense_descr *ata_ret_desc = 7870 &(apt_sd->apt_sd_sense); 7871 int extend = 0; 7872 7873 if ((scsipkt->pkt_cdbp[0] == SPC3_CMD_ATA_COMMAND_PASS_THROUGH16) && 7874 (scsipkt->pkt_cdbp[2] & SATL_APT_BM_EXTEND)) 7875 extend = 1; 7876 7877 scsipkt->pkt_state |= STATE_ARQ_DONE; 7878 7879 /* update the residual count */ 7880 *(uchar_t *)&apt_sd->apt_status = STATUS_CHECK; 7881 *(uchar_t *)&apt_sd->apt_rqpkt_status = STATUS_GOOD; 7882 apt_sd->apt_rqpkt_reason = CMD_CMPLT; 7883 apt_sd->apt_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7884 STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS; 7885 apt_sd->apt_rqpkt_resid = scsipkt->pkt_scblen - 7886 sizeof (struct sata_apt_sense_data); 7887 7888 /* 7889 * Fill in the Descriptor sense header 7890 */ 7891 bzero(sds, sizeof (struct scsi_descr_sense_hdr)); 7892 sds->ds_code = CODE_FMT_DESCR_CURRENT; 7893 sds->ds_class = CLASS_EXTENDED_SENSE; 7894 sds->ds_key = sense_key & 0xf; 7895 sds->ds_add_code = addl_sense_code; 7896 sds->ds_qual_code = addl_sense_qual; 7897 sds->ds_addl_sense_length = 7898 sizeof (struct scsi_ata_status_ret_sense_descr); 7899 7900 /* 7901 * Fill in the ATA Return descriptor sense data 7902 */ 7903 bzero(ata_ret_desc, sizeof (struct scsi_ata_status_ret_sense_descr)); 7904 ata_ret_desc->ars_descr_type = DESCR_ATA_STATUS_RETURN; 7905 ata_ret_desc->ars_addl_length = 0xc; 7906 ata_ret_desc->ars_error = scmd->satacmd_error_reg; 7907 ata_ret_desc->ars_sec_count_lsb = scmd->satacmd_sec_count_lsb; 7908 ata_ret_desc->ars_lba_low_lsb = scmd->satacmd_lba_low_lsb; 7909 ata_ret_desc->ars_lba_mid_lsb = scmd->satacmd_lba_mid_lsb; 7910 ata_ret_desc->ars_lba_high_lsb = scmd->satacmd_lba_high_lsb; 7911 ata_ret_desc->ars_device = scmd->satacmd_device_reg; 7912 ata_ret_desc->ars_status = scmd->satacmd_status_reg; 7913 7914 if (extend == 1) { 7915 ata_ret_desc->ars_extend = 1; 7916 ata_ret_desc->ars_sec_count_msb = scmd->satacmd_sec_count_msb; 7917 ata_ret_desc->ars_lba_low_msb = scmd->satacmd_lba_low_msb; 7918 ata_ret_desc->ars_lba_mid_msb = scmd->satacmd_lba_mid_msb; 7919 ata_ret_desc->ars_lba_high_msb = scmd->satacmd_lba_high_msb; 7920 } else { 7921 ata_ret_desc->ars_extend = 0; 7922 ata_ret_desc->ars_sec_count_msb = 0; 7923 ata_ret_desc->ars_lba_low_msb = 0; 7924 ata_ret_desc->ars_lba_mid_msb = 0; 7925 ata_ret_desc->ars_lba_high_msb = 0; 7926 } 7927 } 7928 7929 static void 7930 sata_set_arq_data(sata_pkt_t *sata_pkt) 7931 { 7932 sata_pkt_txlate_t *spx = 7933 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7934 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7935 struct scsi_extended_sense *sense; 7936 7937 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7938 STATE_SENT_CMD | STATE_GOT_STATUS; 7939 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 7940 /* Normal completion */ 7941 scsipkt->pkt_reason = CMD_CMPLT; 7942 *scsipkt->pkt_scbp = STATUS_GOOD; 7943 } else { 7944 /* Something went wrong */ 7945 scsipkt->pkt_reason = CMD_INCOMPLETE; 7946 *scsipkt->pkt_scbp = STATUS_CHECK; 7947 sense = sata_arq_sense(spx); 7948 switch (sata_pkt->satapkt_reason) { 7949 case SATA_PKT_PORT_ERROR: 7950 /* 7951 * We have no device data. Assume no data transfered. 7952 */ 7953 sense->es_key = KEY_HARDWARE_ERROR; 7954 break; 7955 7956 case SATA_PKT_DEV_ERROR: 7957 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 7958 SATA_STATUS_ERR) { 7959 /* 7960 * determine dev error reason from error 7961 * reg content 7962 */ 7963 sata_decode_device_error(spx, sense); 7964 break; 7965 } 7966 /* No extended sense key - no info available */ 7967 break; 7968 7969 case SATA_PKT_TIMEOUT: 7970 scsipkt->pkt_reason = CMD_TIMEOUT; 7971 scsipkt->pkt_statistics |= 7972 STAT_TIMEOUT | STAT_DEV_RESET; 7973 /* No extended sense key ? */ 7974 break; 7975 7976 case SATA_PKT_ABORTED: 7977 scsipkt->pkt_reason = CMD_ABORTED; 7978 scsipkt->pkt_statistics |= STAT_ABORTED; 7979 /* No extended sense key ? */ 7980 break; 7981 7982 case SATA_PKT_RESET: 7983 /* pkt aborted by an explicit reset from a host */ 7984 scsipkt->pkt_reason = CMD_RESET; 7985 scsipkt->pkt_statistics |= STAT_DEV_RESET; 7986 break; 7987 7988 default: 7989 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 7990 "sata_txlt_nodata_cmd_completion: " 7991 "invalid packet completion reason %d", 7992 sata_pkt->satapkt_reason)); 7993 scsipkt->pkt_reason = CMD_TRAN_ERR; 7994 break; 7995 } 7996 7997 } 7998 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 7999 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 8000 } 8001 8002 8003 /* 8004 * Build Mode sense R/W recovery page 8005 * NOT IMPLEMENTED 8006 */ 8007 8008 static int 8009 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 8010 { 8011 #ifndef __lock_lint 8012 _NOTE(ARGUNUSED(sdinfo)) 8013 _NOTE(ARGUNUSED(pcntrl)) 8014 _NOTE(ARGUNUSED(buf)) 8015 #endif 8016 return (0); 8017 } 8018 8019 /* 8020 * Build Mode sense caching page - scsi-3 implementation. 8021 * Page length distinguishes previous format from scsi-3 format. 8022 * buf must have space for 0x12 bytes. 8023 * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable. 8024 * 8025 */ 8026 static int 8027 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 8028 { 8029 struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf; 8030 sata_id_t *sata_id = &sdinfo->satadrv_id; 8031 8032 /* 8033 * Most of the fields are set to 0, being not supported and/or disabled 8034 */ 8035 bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3); 8036 8037 /* Saved paramters not supported */ 8038 if (pcntrl == 3) 8039 return (0); 8040 if (pcntrl == 0 || pcntrl == 2) { 8041 /* 8042 * For now treat current and default parameters as same 8043 * That may have to change, if target driver will complain 8044 */ 8045 page->mode_page.code = MODEPAGE_CACHING; /* PS = 0 */ 8046 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 8047 8048 if (SATA_READ_AHEAD_SUPPORTED(*sata_id) && 8049 !SATA_READ_AHEAD_ENABLED(*sata_id)) { 8050 page->dra = 1; /* Read Ahead disabled */ 8051 page->rcd = 1; /* Read Cache disabled */ 8052 } 8053 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) && 8054 SATA_WRITE_CACHE_ENABLED(*sata_id)) 8055 page->wce = 1; /* Write Cache enabled */ 8056 } else { 8057 /* Changeable parameters */ 8058 page->mode_page.code = MODEPAGE_CACHING; 8059 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 8060 if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) { 8061 page->dra = 1; 8062 page->rcd = 1; 8063 } 8064 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) 8065 page->wce = 1; 8066 } 8067 return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 8068 sizeof (struct mode_page)); 8069 } 8070 8071 /* 8072 * Build Mode sense exception cntrl page 8073 */ 8074 static int 8075 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 8076 { 8077 struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf; 8078 sata_id_t *sata_id = &sdinfo->satadrv_id; 8079 8080 /* 8081 * Most of the fields are set to 0, being not supported and/or disabled 8082 */ 8083 bzero(buf, PAGELENGTH_INFO_EXCPT); 8084 8085 page->mode_page.code = MODEPAGE_INFO_EXCPT; 8086 page->mode_page.length = PAGELENGTH_INFO_EXCPT; 8087 8088 /* Indicate that this is page is saveable */ 8089 page->mode_page.ps = 1; 8090 8091 /* 8092 * We will return the same data for default, current and saved page. 8093 * The only changeable bit is dexcpt and that bit is required 8094 * by the ATA specification to be preserved across power cycles. 8095 */ 8096 if (pcntrl != 1) { 8097 page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED); 8098 page->mrie = MRIE_ONLY_ON_REQUEST; 8099 } 8100 else 8101 page->dexcpt = 1; /* Only changeable parameter */ 8102 8103 return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)); 8104 } 8105 8106 8107 static int 8108 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 8109 { 8110 struct mode_acoustic_management *page = 8111 (struct mode_acoustic_management *)buf; 8112 sata_id_t *sata_id = &sdinfo->satadrv_id; 8113 8114 /* 8115 * Most of the fields are set to 0, being not supported and/or disabled 8116 */ 8117 bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT); 8118 8119 switch (pcntrl) { 8120 case P_CNTRL_DEFAULT: 8121 /* default paramters not supported */ 8122 return (0); 8123 8124 case P_CNTRL_CURRENT: 8125 case P_CNTRL_SAVED: 8126 /* Saved and current are supported and are identical */ 8127 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG; 8128 page->mode_page.length = 8129 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT; 8130 page->mode_page.ps = 1; 8131 8132 /* Word 83 indicates if feature is supported */ 8133 /* If feature is not supported */ 8134 if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) { 8135 page->acoustic_manag_enable = 8136 ACOUSTIC_DISABLED; 8137 } else { 8138 page->acoustic_manag_enable = 8139 ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT) 8140 != 0); 8141 /* Word 94 inidicates the value */ 8142 #ifdef _LITTLE_ENDIAN 8143 page->acoustic_manag_level = 8144 (uchar_t)sata_id->ai_acoustic; 8145 page->vendor_recommended_value = 8146 sata_id->ai_acoustic >> 8; 8147 #else 8148 page->acoustic_manag_level = 8149 sata_id->ai_acoustic >> 8; 8150 page->vendor_recommended_value = 8151 (uchar_t)sata_id->ai_acoustic; 8152 #endif 8153 } 8154 break; 8155 8156 case P_CNTRL_CHANGEABLE: 8157 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG; 8158 page->mode_page.length = 8159 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT; 8160 page->mode_page.ps = 1; 8161 8162 /* Word 83 indicates if the feature is supported */ 8163 if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) { 8164 page->acoustic_manag_enable = 8165 ACOUSTIC_ENABLED; 8166 page->acoustic_manag_level = 0xff; 8167 } 8168 break; 8169 } 8170 return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 8171 sizeof (struct mode_page)); 8172 } 8173 8174 8175 /* 8176 * Build Mode sense power condition page. 8177 */ 8178 static int 8179 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 8180 { 8181 struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf; 8182 sata_id_t *sata_id = &sdinfo->satadrv_id; 8183 8184 /* 8185 * Most of the fields are set to 0, being not supported and/or disabled 8186 * power condition page length was 0x0a 8187 */ 8188 bzero(buf, sizeof (struct mode_info_power_cond)); 8189 8190 if (pcntrl == P_CNTRL_DEFAULT) { 8191 /* default paramters not supported */ 8192 return (0); 8193 } 8194 8195 page->mode_page.code = MODEPAGE_POWER_COND; 8196 page->mode_page.length = sizeof (struct mode_info_power_cond); 8197 8198 if (sata_id->ai_cap & SATA_STANDBYTIMER) { 8199 page->standby = 1; 8200 bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer, 8201 sizeof (uchar_t) * 4); 8202 } 8203 8204 return (sizeof (struct mode_info_power_cond)); 8205 } 8206 8207 /* 8208 * Process mode select caching page 8 (scsi3 format only). 8209 * Read Ahead (same as read cache) and Write Cache may be turned on and off 8210 * if these features are supported by the device. If these features are not 8211 * supported, the command will be terminated with STATUS_CHECK. 8212 * This function fails only if the SET FEATURE command sent to 8213 * the device fails. The page format is not verified, assuming that the 8214 * target driver operates correctly - if parameters length is too short, 8215 * we just drop the page. 8216 * Two command may be sent if both Read Cache/Read Ahead and Write Cache 8217 * setting have to be changed. 8218 * SET FEATURE command is executed synchronously, i.e. we wait here until 8219 * it is completed, regardless of the scsi pkt directives. 8220 * 8221 * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e. 8222 * changing DRA will change RCD. 8223 * 8224 * More than one SATA command may be executed to perform operations specified 8225 * by mode select pages. The first error terminates further execution. 8226 * Operations performed successully are not backed-up in such case. 8227 * 8228 * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 8229 * If operation resulted in changing device setup, dmod flag should be set to 8230 * one (1). If parameters were not changed, dmod flag should be set to 0. 8231 * Upon return, if operation required sending command to the device, the rval 8232 * should be set to the value returned by sata_hba_start. If operation 8233 * did not require device access, rval should be set to TRAN_ACCEPT. 8234 * The pagelen should be set to the length of the page. 8235 * 8236 * This function has to be called with a port mutex held. 8237 * 8238 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 8239 */ 8240 int 8241 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page, 8242 int parmlen, int *pagelen, int *rval, int *dmod) 8243 { 8244 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 8245 sata_drive_info_t *sdinfo; 8246 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 8247 sata_id_t *sata_id; 8248 struct scsi_extended_sense *sense; 8249 int wce, dra; /* Current settings */ 8250 8251 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 8252 &spx->txlt_sata_pkt->satapkt_device); 8253 sata_id = &sdinfo->satadrv_id; 8254 *dmod = 0; 8255 8256 /* Verify parameters length. If too short, drop it */ 8257 if ((PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 8258 sizeof (struct mode_page)) > parmlen) { 8259 *scsipkt->pkt_scbp = STATUS_CHECK; 8260 sense = sata_arq_sense(spx); 8261 sense->es_key = KEY_ILLEGAL_REQUEST; 8262 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 8263 *pagelen = parmlen; 8264 *rval = TRAN_ACCEPT; 8265 return (SATA_FAILURE); 8266 } 8267 8268 *pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page); 8269 8270 /* Current setting of Read Ahead (and Read Cache) */ 8271 if (SATA_READ_AHEAD_ENABLED(*sata_id)) 8272 dra = 0; /* 0 == not disabled */ 8273 else 8274 dra = 1; 8275 /* Current setting of Write Cache */ 8276 if (SATA_WRITE_CACHE_ENABLED(*sata_id)) 8277 wce = 1; 8278 else 8279 wce = 0; 8280 8281 if (page->dra == dra && page->wce == wce && page->rcd == dra) { 8282 /* nothing to do */ 8283 *rval = TRAN_ACCEPT; 8284 return (SATA_SUCCESS); 8285 } 8286 8287 /* 8288 * Need to flip some setting 8289 * Set-up Internal SET FEATURES command(s) 8290 */ 8291 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 8292 scmd->satacmd_addr_type = 0; 8293 scmd->satacmd_device_reg = 0; 8294 scmd->satacmd_status_reg = 0; 8295 scmd->satacmd_error_reg = 0; 8296 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 8297 if (page->dra != dra || page->rcd != dra) { 8298 if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) { 8299 /* Need to flip read ahead setting */ 8300 if (dra == 0) 8301 /* Disable read ahead / read cache */ 8302 scmd->satacmd_features_reg = 8303 SATAC_SF_DISABLE_READ_AHEAD; 8304 else 8305 /* Enable read ahead / read cache */ 8306 scmd->satacmd_features_reg = 8307 SATAC_SF_ENABLE_READ_AHEAD; 8308 8309 /* Transfer command to HBA */ 8310 if (sata_hba_start(spx, rval) != 0) 8311 /* 8312 * Pkt not accepted for execution. 8313 */ 8314 return (SATA_FAILURE); 8315 8316 *dmod = 1; 8317 8318 /* Now process return */ 8319 if (spx->txlt_sata_pkt->satapkt_reason != 8320 SATA_PKT_COMPLETED) { 8321 goto failure; /* Terminate */ 8322 } 8323 } else { 8324 *scsipkt->pkt_scbp = STATUS_CHECK; 8325 sense = sata_arq_sense(spx); 8326 sense->es_key = KEY_ILLEGAL_REQUEST; 8327 sense->es_add_code = 8328 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 8329 *pagelen = parmlen; 8330 *rval = TRAN_ACCEPT; 8331 return (SATA_FAILURE); 8332 } 8333 } 8334 8335 /* Note that the packet is not removed, so it could be re-used */ 8336 if (page->wce != wce) { 8337 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) { 8338 /* Need to flip Write Cache setting */ 8339 if (page->wce == 1) 8340 /* Enable write cache */ 8341 scmd->satacmd_features_reg = 8342 SATAC_SF_ENABLE_WRITE_CACHE; 8343 else 8344 /* Disable write cache */ 8345 scmd->satacmd_features_reg = 8346 SATAC_SF_DISABLE_WRITE_CACHE; 8347 8348 /* Transfer command to HBA */ 8349 if (sata_hba_start(spx, rval) != 0) 8350 /* 8351 * Pkt not accepted for execution. 8352 */ 8353 return (SATA_FAILURE); 8354 8355 *dmod = 1; 8356 8357 /* Now process return */ 8358 if (spx->txlt_sata_pkt->satapkt_reason != 8359 SATA_PKT_COMPLETED) { 8360 goto failure; 8361 } 8362 } else { 8363 *scsipkt->pkt_scbp = STATUS_CHECK; 8364 sense = sata_arq_sense(spx); 8365 sense->es_key = KEY_ILLEGAL_REQUEST; 8366 sense->es_add_code = 8367 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 8368 *pagelen = parmlen; 8369 *rval = TRAN_ACCEPT; 8370 return (SATA_FAILURE); 8371 } 8372 } 8373 return (SATA_SUCCESS); 8374 8375 failure: 8376 sata_xlate_errors(spx); 8377 8378 return (SATA_FAILURE); 8379 } 8380 8381 /* 8382 * Process mode select informational exceptions control page 0x1c 8383 * 8384 * The only changeable bit is dexcpt (disable exceptions). 8385 * MRIE (method of reporting informational exceptions) must be 8386 * "only on request". 8387 * This page applies to informational exceptions that report 8388 * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh 8389 * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_). 8390 * Informational exception conditions occur as the result of background scan 8391 * errors, background self-test errors, or vendor specific events within a 8392 * logical unit. An informational exception condition may occur asynchronous 8393 * to any commands. 8394 * 8395 * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 8396 * If operation resulted in changing device setup, dmod flag should be set to 8397 * one (1). If parameters were not changed, dmod flag should be set to 0. 8398 * Upon return, if operation required sending command to the device, the rval 8399 * should be set to the value returned by sata_hba_start. If operation 8400 * did not require device access, rval should be set to TRAN_ACCEPT. 8401 * The pagelen should be set to the length of the page. 8402 * 8403 * This function has to be called with a port mutex held. 8404 * 8405 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 8406 * 8407 * Cannot be called in the interrupt context. 8408 */ 8409 static int 8410 sata_mode_select_page_1c( 8411 sata_pkt_txlate_t *spx, 8412 struct mode_info_excpt_page *page, 8413 int parmlen, 8414 int *pagelen, 8415 int *rval, 8416 int *dmod) 8417 { 8418 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 8419 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 8420 sata_drive_info_t *sdinfo; 8421 sata_id_t *sata_id; 8422 struct scsi_extended_sense *sense; 8423 8424 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 8425 &spx->txlt_sata_pkt->satapkt_device); 8426 sata_id = &sdinfo->satadrv_id; 8427 8428 *dmod = 0; 8429 8430 /* Verify parameters length. If too short, drop it */ 8431 if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) > parmlen) || 8432 page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) { 8433 *scsipkt->pkt_scbp = STATUS_CHECK; 8434 sense = sata_arq_sense(spx); 8435 sense->es_key = KEY_ILLEGAL_REQUEST; 8436 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 8437 *pagelen = parmlen; 8438 *rval = TRAN_ACCEPT; 8439 return (SATA_FAILURE); 8440 } 8441 8442 *pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page); 8443 8444 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 8445 *scsipkt->pkt_scbp = STATUS_CHECK; 8446 sense = sata_arq_sense(spx); 8447 sense->es_key = KEY_ILLEGAL_REQUEST; 8448 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 8449 *pagelen = parmlen; 8450 *rval = TRAN_ACCEPT; 8451 return (SATA_FAILURE); 8452 } 8453 8454 /* If already in the state requested, we are done */ 8455 if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 8456 /* nothing to do */ 8457 *rval = TRAN_ACCEPT; 8458 return (SATA_SUCCESS); 8459 } 8460 8461 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 8462 8463 /* Build SMART_ENABLE or SMART_DISABLE command */ 8464 scmd->satacmd_addr_type = 0; /* N/A */ 8465 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 8466 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 8467 scmd->satacmd_features_reg = page->dexcpt ? 8468 SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS; 8469 scmd->satacmd_device_reg = 0; /* Always device 0 */ 8470 scmd->satacmd_cmd_reg = SATAC_SMART; 8471 8472 /* Transfer command to HBA */ 8473 if (sata_hba_start(spx, rval) != 0) 8474 /* 8475 * Pkt not accepted for execution. 8476 */ 8477 return (SATA_FAILURE); 8478 8479 *dmod = 1; /* At least may have been modified */ 8480 8481 /* Now process return */ 8482 if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) 8483 return (SATA_SUCCESS); 8484 8485 /* Packet did not complete successfully */ 8486 sata_xlate_errors(spx); 8487 8488 return (SATA_FAILURE); 8489 } 8490 8491 /* 8492 * Process mode select acoustic management control page 0x30 8493 * 8494 * 8495 * This function has to be called with a port mutex held. 8496 * 8497 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 8498 * 8499 * Cannot be called in the interrupt context. 8500 */ 8501 int 8502 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct 8503 mode_acoustic_management *page, int parmlen, int *pagelen, 8504 int *rval, int *dmod) 8505 { 8506 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 8507 sata_drive_info_t *sdinfo; 8508 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 8509 sata_id_t *sata_id; 8510 struct scsi_extended_sense *sense; 8511 8512 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 8513 &spx->txlt_sata_pkt->satapkt_device); 8514 sata_id = &sdinfo->satadrv_id; 8515 *dmod = 0; 8516 8517 /* If parmlen is too short or the feature is not supported, drop it */ 8518 if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 8519 sizeof (struct mode_page)) > parmlen) || 8520 (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) { 8521 *scsipkt->pkt_scbp = STATUS_CHECK; 8522 sense = sata_arq_sense(spx); 8523 sense->es_key = KEY_ILLEGAL_REQUEST; 8524 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 8525 *pagelen = parmlen; 8526 *rval = TRAN_ACCEPT; 8527 return (SATA_FAILURE); 8528 } 8529 8530 *pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 8531 sizeof (struct mode_page); 8532 8533 /* 8534 * We can enable and disable acoustice management and 8535 * set the acoustic management level. 8536 */ 8537 8538 /* 8539 * Set-up Internal SET FEATURES command(s) 8540 */ 8541 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 8542 scmd->satacmd_addr_type = 0; 8543 scmd->satacmd_device_reg = 0; 8544 scmd->satacmd_status_reg = 0; 8545 scmd->satacmd_error_reg = 0; 8546 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 8547 if (page->acoustic_manag_enable) { 8548 scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC; 8549 scmd->satacmd_sec_count_lsb = page->acoustic_manag_level; 8550 } else { /* disabling acoustic management */ 8551 scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC; 8552 } 8553 8554 /* Transfer command to HBA */ 8555 if (sata_hba_start(spx, rval) != 0) 8556 /* 8557 * Pkt not accepted for execution. 8558 */ 8559 return (SATA_FAILURE); 8560 8561 /* Now process return */ 8562 if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) { 8563 sata_xlate_errors(spx); 8564 return (SATA_FAILURE); 8565 } 8566 8567 *dmod = 1; 8568 8569 return (SATA_SUCCESS); 8570 } 8571 8572 /* 8573 * Process mode select power condition page 0x1a 8574 * 8575 * This function has to be called with a port mutex held. 8576 * 8577 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 8578 * 8579 * Cannot be called in the interrupt context. 8580 */ 8581 int 8582 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct 8583 mode_info_power_cond *page, int parmlen, int *pagelen, 8584 int *rval, int *dmod) 8585 { 8586 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 8587 sata_drive_info_t *sdinfo; 8588 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 8589 sata_id_t *sata_id; 8590 struct scsi_extended_sense *sense; 8591 uint8_t ata_count; 8592 int i, len; 8593 8594 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 8595 &spx->txlt_sata_pkt->satapkt_device); 8596 sata_id = &sdinfo->satadrv_id; 8597 *dmod = 0; 8598 8599 len = sizeof (struct mode_info_power_cond); 8600 len += sizeof (struct mode_page); 8601 8602 /* If parmlen is too short or the feature is not supported, drop it */ 8603 if ((len < parmlen) || (page->idle == 1) || 8604 (!(sata_id->ai_cap & SATA_STANDBYTIMER) && page->standby == 1)) { 8605 *scsipkt->pkt_scbp = STATUS_CHECK; 8606 sense = sata_arq_sense(spx); 8607 sense->es_key = KEY_ILLEGAL_REQUEST; 8608 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 8609 *pagelen = parmlen; 8610 *rval = TRAN_ACCEPT; 8611 return (SATA_FAILURE); 8612 } 8613 8614 *pagelen = len; 8615 8616 /* 8617 * Set-up Internal STANDBY command(s) 8618 */ 8619 if (page->standby == 0) 8620 goto out; 8621 8622 ata_count = sata_get_standby_timer(page->standby_cond_timer); 8623 8624 scmd->satacmd_addr_type = 0; 8625 scmd->satacmd_sec_count_lsb = ata_count; 8626 scmd->satacmd_lba_low_lsb = 0; 8627 scmd->satacmd_lba_mid_lsb = 0; 8628 scmd->satacmd_lba_high_lsb = 0; 8629 scmd->satacmd_features_reg = 0; 8630 scmd->satacmd_device_reg = 0; 8631 scmd->satacmd_status_reg = 0; 8632 scmd->satacmd_cmd_reg = SATAC_STANDBY; 8633 scmd->satacmd_flags.sata_special_regs = B_TRUE; 8634 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 8635 8636 /* Transfer command to HBA */ 8637 if (sata_hba_start(spx, rval) != 0) { 8638 return (SATA_FAILURE); 8639 } else { 8640 if ((scmd->satacmd_error_reg != 0) || 8641 (spx->txlt_sata_pkt->satapkt_reason != 8642 SATA_PKT_COMPLETED)) { 8643 sata_xlate_errors(spx); 8644 return (SATA_FAILURE); 8645 } 8646 } 8647 8648 for (i = 0; i < 4; i++) { 8649 sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i]; 8650 } 8651 out: 8652 *dmod = 1; 8653 return (SATA_SUCCESS); 8654 } 8655 8656 /* 8657 * sata_build_lsense_page0() is used to create the 8658 * SCSI LOG SENSE page 0 (supported log pages) 8659 * 8660 * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e 8661 * (supported log pages, self-test results, informational exceptions 8662 * Sun vendor specific ATA SMART data, and start stop cycle counter). 8663 * 8664 * Takes a sata_drive_info t * and the address of a buffer 8665 * in which to create the page information. 8666 * 8667 * Returns the number of bytes valid in the buffer. 8668 */ 8669 static int 8670 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf) 8671 { 8672 struct log_parameter *lpp = (struct log_parameter *)buf; 8673 uint8_t *page_ptr = (uint8_t *)lpp->param_values; 8674 int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */ 8675 sata_id_t *sata_id = &sdinfo->satadrv_id; 8676 8677 lpp->param_code[0] = 0; 8678 lpp->param_code[1] = 0; 8679 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 8680 *page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES; 8681 8682 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 8683 if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) { 8684 *page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS; 8685 ++num_pages_supported; 8686 } 8687 *page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS; 8688 ++num_pages_supported; 8689 *page_ptr++ = PAGE_CODE_SMART_READ_DATA; 8690 ++num_pages_supported; 8691 *page_ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER; 8692 ++num_pages_supported; 8693 } 8694 8695 lpp->param_len = num_pages_supported; 8696 8697 return ((&lpp->param_values[0] - (uint8_t *)lpp) + 8698 num_pages_supported); 8699 } 8700 8701 /* 8702 * sata_build_lsense_page_10() is used to create the 8703 * SCSI LOG SENSE page 0x10 (self-test results) 8704 * 8705 * Takes a sata_drive_info t * and the address of a buffer 8706 * in which to create the page information as well as a sata_hba_inst_t *. 8707 * 8708 * Returns the number of bytes valid in the buffer. 8709 * 8710 * Note: Self test and SMART data is accessible in device log pages. 8711 * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors 8712 * of data can be transferred by a single command), or by the General Purpose 8713 * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors 8714 * - approximately 33MB - can be transferred by a single command. 8715 * The SCT Command response (either error or command) is the same for both 8716 * the SMART and GPL methods of issuing commands. 8717 * This function uses READ LOG EXT command when drive supports LBA48, and 8718 * SMART READ command otherwise. 8719 * 8720 * Since above commands are executed in a synchronous mode, this function 8721 * should not be called in an interrupt context. 8722 */ 8723 static int 8724 sata_build_lsense_page_10( 8725 sata_drive_info_t *sdinfo, 8726 uint8_t *buf, 8727 sata_hba_inst_t *sata_hba_inst) 8728 { 8729 struct log_parameter *lpp = (struct log_parameter *)buf; 8730 int rval; 8731 8732 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 8733 struct smart_ext_selftest_log *ext_selftest_log; 8734 8735 ext_selftest_log = kmem_zalloc( 8736 sizeof (struct smart_ext_selftest_log), KM_SLEEP); 8737 8738 rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo, 8739 ext_selftest_log, 0); 8740 if (rval == 0) { 8741 int index, start_index; 8742 struct smart_ext_selftest_log_entry *entry; 8743 static const struct smart_ext_selftest_log_entry empty = 8744 {0}; 8745 uint16_t block_num; 8746 int count; 8747 boolean_t only_one_block = B_FALSE; 8748 8749 index = ext_selftest_log-> 8750 smart_ext_selftest_log_index[0]; 8751 index |= ext_selftest_log-> 8752 smart_ext_selftest_log_index[1] << 8; 8753 if (index == 0) 8754 goto out; 8755 8756 --index; /* Correct for 0 origin */ 8757 start_index = index; /* remember where we started */ 8758 block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 8759 if (block_num != 0) { 8760 rval = sata_ext_smart_selftest_read_log( 8761 sata_hba_inst, sdinfo, ext_selftest_log, 8762 block_num); 8763 if (rval != 0) 8764 goto out; 8765 } 8766 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 8767 entry = 8768 &ext_selftest_log-> 8769 smart_ext_selftest_log_entries[index]; 8770 8771 for (count = 1; 8772 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 8773 ++count) { 8774 uint8_t status; 8775 uint8_t code; 8776 uint8_t sense_key; 8777 uint8_t add_sense_code; 8778 uint8_t add_sense_code_qual; 8779 8780 /* If this is an unused entry, we are done */ 8781 if (bcmp(entry, &empty, sizeof (empty)) == 0) { 8782 /* Broken firmware on some disks */ 8783 if (index + 1 == 8784 ENTRIES_PER_EXT_SELFTEST_LOG_BLK) { 8785 --entry; 8786 --index; 8787 if (bcmp(entry, &empty, 8788 sizeof (empty)) == 0) 8789 goto out; 8790 } else 8791 goto out; 8792 } 8793 8794 if (only_one_block && 8795 start_index == index) 8796 goto out; 8797 8798 lpp->param_code[0] = 0; 8799 lpp->param_code[1] = count; 8800 lpp->param_ctrl_flags = 8801 LOG_CTRL_LP | LOG_CTRL_LBIN; 8802 lpp->param_len = 8803 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 8804 8805 status = entry->smart_ext_selftest_log_status; 8806 status >>= 4; 8807 switch (status) { 8808 case 0: 8809 default: 8810 sense_key = KEY_NO_SENSE; 8811 add_sense_code = 8812 SD_SCSI_ASC_NO_ADD_SENSE; 8813 add_sense_code_qual = 0; 8814 break; 8815 case 1: 8816 sense_key = KEY_ABORTED_COMMAND; 8817 add_sense_code = 8818 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8819 add_sense_code_qual = SCSI_COMPONENT_81; 8820 break; 8821 case 2: 8822 sense_key = KEY_ABORTED_COMMAND; 8823 add_sense_code = 8824 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8825 add_sense_code_qual = SCSI_COMPONENT_82; 8826 break; 8827 case 3: 8828 sense_key = KEY_ABORTED_COMMAND; 8829 add_sense_code = 8830 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8831 add_sense_code_qual = SCSI_COMPONENT_83; 8832 break; 8833 case 4: 8834 sense_key = KEY_HARDWARE_ERROR; 8835 add_sense_code = 8836 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8837 add_sense_code_qual = SCSI_COMPONENT_84; 8838 break; 8839 case 5: 8840 sense_key = KEY_HARDWARE_ERROR; 8841 add_sense_code = 8842 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8843 add_sense_code_qual = SCSI_COMPONENT_85; 8844 break; 8845 case 6: 8846 sense_key = KEY_HARDWARE_ERROR; 8847 add_sense_code = 8848 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8849 add_sense_code_qual = SCSI_COMPONENT_86; 8850 break; 8851 case 7: 8852 sense_key = KEY_MEDIUM_ERROR; 8853 add_sense_code = 8854 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8855 add_sense_code_qual = SCSI_COMPONENT_87; 8856 break; 8857 case 8: 8858 sense_key = KEY_HARDWARE_ERROR; 8859 add_sense_code = 8860 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8861 add_sense_code_qual = SCSI_COMPONENT_88; 8862 break; 8863 } 8864 code = 0; /* unspecified */ 8865 status |= (code << 4); 8866 lpp->param_values[0] = status; 8867 lpp->param_values[1] = 0; /* unspecified */ 8868 lpp->param_values[2] = entry-> 8869 smart_ext_selftest_log_timestamp[1]; 8870 lpp->param_values[3] = entry-> 8871 smart_ext_selftest_log_timestamp[0]; 8872 if (status != 0) { 8873 lpp->param_values[4] = 0; 8874 lpp->param_values[5] = 0; 8875 lpp->param_values[6] = entry-> 8876 smart_ext_selftest_log_failing_lba 8877 [5]; 8878 lpp->param_values[7] = entry-> 8879 smart_ext_selftest_log_failing_lba 8880 [4]; 8881 lpp->param_values[8] = entry-> 8882 smart_ext_selftest_log_failing_lba 8883 [3]; 8884 lpp->param_values[9] = entry-> 8885 smart_ext_selftest_log_failing_lba 8886 [2]; 8887 lpp->param_values[10] = entry-> 8888 smart_ext_selftest_log_failing_lba 8889 [1]; 8890 lpp->param_values[11] = entry-> 8891 smart_ext_selftest_log_failing_lba 8892 [0]; 8893 } else { /* No bad block address */ 8894 lpp->param_values[4] = 0xff; 8895 lpp->param_values[5] = 0xff; 8896 lpp->param_values[6] = 0xff; 8897 lpp->param_values[7] = 0xff; 8898 lpp->param_values[8] = 0xff; 8899 lpp->param_values[9] = 0xff; 8900 lpp->param_values[10] = 0xff; 8901 lpp->param_values[11] = 0xff; 8902 } 8903 8904 lpp->param_values[12] = sense_key; 8905 lpp->param_values[13] = add_sense_code; 8906 lpp->param_values[14] = add_sense_code_qual; 8907 lpp->param_values[15] = 0; /* undefined */ 8908 8909 lpp = (struct log_parameter *) 8910 (((uint8_t *)lpp) + 8911 SCSI_LOG_PARAM_HDR_LEN + 8912 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 8913 8914 --index; /* Back up to previous entry */ 8915 if (index < 0) { 8916 if (block_num > 0) { 8917 --block_num; 8918 } else { 8919 struct read_log_ext_directory 8920 logdir; 8921 8922 rval = 8923 sata_read_log_ext_directory( 8924 sata_hba_inst, sdinfo, 8925 &logdir); 8926 if (rval == -1) 8927 goto out; 8928 if ((logdir.read_log_ext_vers 8929 [0] == 0) && 8930 (logdir.read_log_ext_vers 8931 [1] == 0)) 8932 goto out; 8933 block_num = 8934 logdir.read_log_ext_nblks 8935 [EXT_SMART_SELFTEST_LOG_PAGE 8936 - 1][0]; 8937 block_num |= logdir. 8938 read_log_ext_nblks 8939 [EXT_SMART_SELFTEST_LOG_PAGE 8940 - 1][1] << 8; 8941 --block_num; 8942 only_one_block = 8943 (block_num == 0); 8944 } 8945 rval = sata_ext_smart_selftest_read_log( 8946 sata_hba_inst, sdinfo, 8947 ext_selftest_log, block_num); 8948 if (rval != 0) 8949 goto out; 8950 8951 index = 8952 ENTRIES_PER_EXT_SELFTEST_LOG_BLK - 8953 1; 8954 } 8955 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 8956 entry = &ext_selftest_log-> 8957 smart_ext_selftest_log_entries[index]; 8958 } 8959 } 8960 out: 8961 kmem_free(ext_selftest_log, 8962 sizeof (struct smart_ext_selftest_log)); 8963 } else { 8964 struct smart_selftest_log *selftest_log; 8965 8966 selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log), 8967 KM_SLEEP); 8968 8969 rval = sata_smart_selftest_log(sata_hba_inst, sdinfo, 8970 selftest_log); 8971 8972 if (rval == 0) { 8973 int index; 8974 int count; 8975 struct smart_selftest_log_entry *entry; 8976 static const struct smart_selftest_log_entry empty = 8977 { 0 }; 8978 8979 index = selftest_log->smart_selftest_log_index; 8980 if (index == 0) 8981 goto done; 8982 --index; /* Correct for 0 origin */ 8983 entry = &selftest_log-> 8984 smart_selftest_log_entries[index]; 8985 for (count = 1; 8986 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 8987 ++count) { 8988 uint8_t status; 8989 uint8_t code; 8990 uint8_t sense_key; 8991 uint8_t add_sense_code; 8992 uint8_t add_sense_code_qual; 8993 8994 if (bcmp(entry, &empty, sizeof (empty)) == 0) 8995 goto done; 8996 8997 lpp->param_code[0] = 0; 8998 lpp->param_code[1] = count; 8999 lpp->param_ctrl_flags = 9000 LOG_CTRL_LP | LOG_CTRL_LBIN; 9001 lpp->param_len = 9002 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 9003 9004 status = entry->smart_selftest_log_status; 9005 status >>= 4; 9006 switch (status) { 9007 case 0: 9008 default: 9009 sense_key = KEY_NO_SENSE; 9010 add_sense_code = 9011 SD_SCSI_ASC_NO_ADD_SENSE; 9012 break; 9013 case 1: 9014 sense_key = KEY_ABORTED_COMMAND; 9015 add_sense_code = 9016 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9017 add_sense_code_qual = SCSI_COMPONENT_81; 9018 break; 9019 case 2: 9020 sense_key = KEY_ABORTED_COMMAND; 9021 add_sense_code = 9022 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9023 add_sense_code_qual = SCSI_COMPONENT_82; 9024 break; 9025 case 3: 9026 sense_key = KEY_ABORTED_COMMAND; 9027 add_sense_code = 9028 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9029 add_sense_code_qual = SCSI_COMPONENT_83; 9030 break; 9031 case 4: 9032 sense_key = KEY_HARDWARE_ERROR; 9033 add_sense_code = 9034 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9035 add_sense_code_qual = SCSI_COMPONENT_84; 9036 break; 9037 case 5: 9038 sense_key = KEY_HARDWARE_ERROR; 9039 add_sense_code = 9040 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9041 add_sense_code_qual = SCSI_COMPONENT_85; 9042 break; 9043 case 6: 9044 sense_key = KEY_HARDWARE_ERROR; 9045 add_sense_code = 9046 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9047 add_sense_code_qual = SCSI_COMPONENT_86; 9048 break; 9049 case 7: 9050 sense_key = KEY_MEDIUM_ERROR; 9051 add_sense_code = 9052 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9053 add_sense_code_qual = SCSI_COMPONENT_87; 9054 break; 9055 case 8: 9056 sense_key = KEY_HARDWARE_ERROR; 9057 add_sense_code = 9058 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9059 add_sense_code_qual = SCSI_COMPONENT_88; 9060 break; 9061 } 9062 code = 0; /* unspecified */ 9063 status |= (code << 4); 9064 lpp->param_values[0] = status; 9065 lpp->param_values[1] = 0; /* unspecified */ 9066 lpp->param_values[2] = entry-> 9067 smart_selftest_log_timestamp[1]; 9068 lpp->param_values[3] = entry-> 9069 smart_selftest_log_timestamp[0]; 9070 if (status != 0) { 9071 lpp->param_values[4] = 0; 9072 lpp->param_values[5] = 0; 9073 lpp->param_values[6] = 0; 9074 lpp->param_values[7] = 0; 9075 lpp->param_values[8] = entry-> 9076 smart_selftest_log_failing_lba[3]; 9077 lpp->param_values[9] = entry-> 9078 smart_selftest_log_failing_lba[2]; 9079 lpp->param_values[10] = entry-> 9080 smart_selftest_log_failing_lba[1]; 9081 lpp->param_values[11] = entry-> 9082 smart_selftest_log_failing_lba[0]; 9083 } else { /* No block address */ 9084 lpp->param_values[4] = 0xff; 9085 lpp->param_values[5] = 0xff; 9086 lpp->param_values[6] = 0xff; 9087 lpp->param_values[7] = 0xff; 9088 lpp->param_values[8] = 0xff; 9089 lpp->param_values[9] = 0xff; 9090 lpp->param_values[10] = 0xff; 9091 lpp->param_values[11] = 0xff; 9092 } 9093 lpp->param_values[12] = sense_key; 9094 lpp->param_values[13] = add_sense_code; 9095 lpp->param_values[14] = add_sense_code_qual; 9096 lpp->param_values[15] = 0; /* undefined */ 9097 9098 lpp = (struct log_parameter *) 9099 (((uint8_t *)lpp) + 9100 SCSI_LOG_PARAM_HDR_LEN + 9101 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 9102 --index; /* back up to previous entry */ 9103 if (index < 0) { 9104 index = 9105 NUM_SMART_SELFTEST_LOG_ENTRIES - 1; 9106 } 9107 entry = &selftest_log-> 9108 smart_selftest_log_entries[index]; 9109 } 9110 } 9111 done: 9112 kmem_free(selftest_log, sizeof (struct smart_selftest_log)); 9113 } 9114 9115 return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) * 9116 SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS); 9117 } 9118 9119 /* 9120 * sata_build_lsense_page_2f() is used to create the 9121 * SCSI LOG SENSE page 0x2f (informational exceptions) 9122 * 9123 * Takes a sata_drive_info t * and the address of a buffer 9124 * in which to create the page information as well as a sata_hba_inst_t *. 9125 * 9126 * Returns the number of bytes valid in the buffer. 9127 * 9128 * Because it invokes function(s) that send synchronously executed command 9129 * to the HBA, it cannot be called in the interrupt context. 9130 */ 9131 static int 9132 sata_build_lsense_page_2f( 9133 sata_drive_info_t *sdinfo, 9134 uint8_t *buf, 9135 sata_hba_inst_t *sata_hba_inst) 9136 { 9137 struct log_parameter *lpp = (struct log_parameter *)buf; 9138 int rval; 9139 uint8_t *smart_data; 9140 uint8_t temp; 9141 sata_id_t *sata_id; 9142 #define SMART_NO_TEMP 0xff 9143 9144 lpp->param_code[0] = 0; 9145 lpp->param_code[1] = 0; 9146 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 9147 9148 /* Now get the SMART status w.r.t. threshold exceeded */ 9149 rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo); 9150 switch (rval) { 9151 case 1: 9152 lpp->param_values[0] = SCSI_PREDICTED_FAILURE; 9153 lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE; 9154 break; 9155 case 0: 9156 case -1: /* failed to get data */ 9157 lpp->param_values[0] = 0; /* No failure predicted */ 9158 lpp->param_values[1] = 0; 9159 break; 9160 #if defined(SATA_DEBUG) 9161 default: 9162 cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value"); 9163 /* NOTREACHED */ 9164 #endif 9165 } 9166 9167 sata_id = &sdinfo->satadrv_id; 9168 if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP)) 9169 temp = SMART_NO_TEMP; 9170 else { 9171 /* Now get the temperature */ 9172 smart_data = kmem_zalloc(512, KM_SLEEP); 9173 rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data, 9174 SCT_STATUS_LOG_PAGE, 1); 9175 if (rval == -1) 9176 temp = SMART_NO_TEMP; 9177 else { 9178 temp = smart_data[200]; 9179 if (temp & 0x80) { 9180 if (temp & 0x7f) 9181 temp = 0; 9182 else 9183 temp = SMART_NO_TEMP; 9184 } 9185 } 9186 kmem_free(smart_data, 512); 9187 } 9188 9189 lpp->param_values[2] = temp; /* most recent temperature */ 9190 lpp->param_values[3] = 0; /* required vendor specific byte */ 9191 9192 lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN; 9193 9194 9195 return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN); 9196 } 9197 9198 /* 9199 * sata_build_lsense_page_30() is used to create the 9200 * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data). 9201 * 9202 * Takes a sata_drive_info t * and the address of a buffer 9203 * in which to create the page information as well as a sata_hba_inst_t *. 9204 * 9205 * Returns the number of bytes valid in the buffer. 9206 */ 9207 static int 9208 sata_build_lsense_page_30( 9209 sata_drive_info_t *sdinfo, 9210 uint8_t *buf, 9211 sata_hba_inst_t *sata_hba_inst) 9212 { 9213 struct smart_data *smart_data = (struct smart_data *)buf; 9214 int rval; 9215 9216 /* Now do the SMART READ DATA */ 9217 rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data); 9218 if (rval == -1) 9219 return (0); 9220 9221 return (sizeof (struct smart_data)); 9222 } 9223 9224 /* 9225 * sata_build_lsense_page_0e() is used to create the 9226 * SCSI LOG SENSE page 0e (start-stop cycle counter page) 9227 * 9228 * Date of Manufacture (0x0001) 9229 * YEAR = "0000" 9230 * WEEK = "00" 9231 * Accounting Date (0x0002) 9232 * 6 ASCII space character(20h) 9233 * Specified cycle count over device lifetime 9234 * VALUE - THRESH - the delta between max and min; 9235 * Accumulated start-stop cycles 9236 * VALUE - WORST - the accumulated cycles; 9237 * 9238 * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute 9239 * 9240 * Takes a sata_drive_info t * and the address of a buffer 9241 * in which to create the page information as well as a sata_hba_inst_t *. 9242 * 9243 * Returns the number of bytes valid in the buffer. 9244 */ 9245 static int 9246 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf, 9247 sata_pkt_txlate_t *spx) 9248 { 9249 struct start_stop_cycle_counter_log *log_page; 9250 int i, rval, index; 9251 uint8_t smart_data[512], id, value, worst, thresh; 9252 uint32_t max_count, cycles; 9253 9254 /* Now do the SMART READ DATA */ 9255 rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo, 9256 (struct smart_data *)smart_data); 9257 if (rval == -1) 9258 return (0); 9259 for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) { 9260 index = (i * 12) + 2; 9261 id = smart_data[index]; 9262 if (id != SMART_START_STOP_COUNT_ID) 9263 continue; 9264 else { 9265 thresh = smart_data[index + 2]; 9266 value = smart_data[index + 3]; 9267 worst = smart_data[index + 4]; 9268 break; 9269 } 9270 } 9271 if (id != SMART_START_STOP_COUNT_ID) 9272 return (0); 9273 max_count = value - thresh; 9274 cycles = value - worst; 9275 9276 log_page = (struct start_stop_cycle_counter_log *)buf; 9277 bzero(log_page, sizeof (struct start_stop_cycle_counter_log)); 9278 log_page->code = 0x0e; 9279 log_page->page_len_low = 0x24; 9280 9281 log_page->manufactor_date_low = 0x1; 9282 log_page->param_1.fmt_link = 0x1; /* 01b */ 9283 log_page->param_len_1 = 0x06; 9284 for (i = 0; i < 4; i++) { 9285 log_page->year_manu[i] = 0x30; 9286 if (i < 2) 9287 log_page->week_manu[i] = 0x30; 9288 } 9289 9290 log_page->account_date_low = 0x02; 9291 log_page->param_2.fmt_link = 0x01; /* 01b */ 9292 log_page->param_len_2 = 0x06; 9293 for (i = 0; i < 4; i++) { 9294 log_page->year_account[i] = 0x20; 9295 if (i < 2) 9296 log_page->week_account[i] = 0x20; 9297 } 9298 9299 log_page->lifetime_code_low = 0x03; 9300 log_page->param_3.fmt_link = 0x03; /* 11b */ 9301 log_page->param_len_3 = 0x04; 9302 /* VALUE - THRESH - the delta between max and min */ 9303 log_page->cycle_code_low = 0x04; 9304 log_page->param_4.fmt_link = 0x03; /* 11b */ 9305 log_page->param_len_4 = 0x04; 9306 /* WORST - THRESH - the distance from 'now' to min */ 9307 9308 for (i = 0; i < 4; i++) { 9309 log_page->cycle_lifetime[i] = 9310 (max_count >> (8 * (3 - i))) & 0xff; 9311 log_page->cycle_accumulated[i] = 9312 (cycles >> (8 * (3 - i))) & 0xff; 9313 } 9314 9315 return (sizeof (struct start_stop_cycle_counter_log)); 9316 } 9317 9318 /* 9319 * This function was used for build a ATA read verify sector command 9320 */ 9321 static void 9322 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba) 9323 { 9324 scmd->satacmd_cmd_reg = SATAC_RDVER; 9325 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 9326 scmd->satacmd_flags.sata_special_regs = B_TRUE; 9327 9328 scmd->satacmd_sec_count_lsb = sec & 0xff; 9329 scmd->satacmd_lba_low_lsb = lba & 0xff; 9330 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 9331 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 9332 scmd->satacmd_device_reg = (SATA_ADH_LBA | (lba >> 24) & 0xf); 9333 scmd->satacmd_features_reg = 0; 9334 scmd->satacmd_status_reg = 0; 9335 scmd->satacmd_error_reg = 0; 9336 } 9337 9338 /* 9339 * This function was used for building an ATA 9340 * command, and only command register need to 9341 * be defined, other register will be zero or na. 9342 */ 9343 static void 9344 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd) 9345 { 9346 scmd->satacmd_addr_type = 0; 9347 scmd->satacmd_cmd_reg = cmd; 9348 scmd->satacmd_device_reg = 0; 9349 scmd->satacmd_sec_count_lsb = 0; 9350 scmd->satacmd_lba_low_lsb = 0; 9351 scmd->satacmd_lba_mid_lsb = 0; 9352 scmd->satacmd_lba_high_lsb = 0; 9353 scmd->satacmd_features_reg = 0; 9354 scmd->satacmd_status_reg = 0; 9355 scmd->satacmd_error_reg = 0; 9356 scmd->satacmd_flags.sata_special_regs = B_TRUE; 9357 } 9358 9359 /* 9360 * This function was used for changing the standby 9361 * timer format from SCSI to ATA. 9362 */ 9363 static uint8_t 9364 sata_get_standby_timer(uint8_t *timer) 9365 { 9366 uint32_t i = 0, count = 0; 9367 uint8_t ata_count; 9368 9369 for (i = 0; i < 4; i++) { 9370 count = count << 8 | timer[i]; 9371 } 9372 9373 if (count == 0) 9374 return (0); 9375 9376 if (count >= 1 && count <= 12000) 9377 ata_count = (count -1) / 50 + 1; 9378 else if (count > 12000 && count <= 12600) 9379 ata_count = 0xfc; 9380 else if (count > 12601 && count <= 12750) 9381 ata_count = 0xff; 9382 else if (count > 12750 && count <= 17999) 9383 ata_count = 0xf1; 9384 else if (count > 18000 && count <= 198000) 9385 ata_count = count / 18000 + 240; 9386 else 9387 ata_count = 0xfd; 9388 return (ata_count); 9389 } 9390 9391 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */ 9392 9393 /* 9394 * Start command for ATAPI device. 9395 * This function processes scsi_pkt requests. 9396 * Now CD/DVD, tape and ATAPI disk devices are supported. 9397 * Most commands are packet without any translation into Packet Command. 9398 * Some may be trapped and executed as SATA commands (not clear which one). 9399 * 9400 * Returns TRAN_ACCEPT if command is accepted for execution (or completed 9401 * execution). 9402 * Returns other TRAN_XXXX codes if command is not accepted or completed 9403 * (see return values for sata_hba_start()). 9404 * 9405 * Note: 9406 * Inquiry cdb format differs between transport version 2 and 3. 9407 * However, the transport version 3 devices that were checked did not adhere 9408 * to the specification (ignored MSB of the allocation length). Therefore, 9409 * the transport version is not checked, but Inquiry allocation length is 9410 * truncated to 255 bytes if the original allocation length set-up by the 9411 * target driver is greater than 255 bytes. 9412 */ 9413 static int 9414 sata_txlt_atapi(sata_pkt_txlate_t *spx) 9415 { 9416 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 9417 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 9418 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 9419 sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx); 9420 sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba, 9421 &spx->txlt_sata_pkt->satapkt_device); 9422 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 9423 int cdblen; 9424 int rval, reason; 9425 int synch; 9426 union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp; 9427 9428 mutex_enter(cport_mutex); 9429 9430 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 9431 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 9432 mutex_exit(cport_mutex); 9433 return (rval); 9434 } 9435 9436 /* 9437 * ATAPI device executes some ATA commands in addition to those 9438 * commands sent via PACKET command. These ATA commands may be 9439 * executed by the regular SATA translation functions. None needs 9440 * to be captured now. 9441 * 9442 * Commands sent via PACKET command include: 9443 * MMC command set for ATAPI CD/DVD device 9444 * SSC command set for ATAPI TAPE device 9445 * SBC command set for ATAPI disk device 9446 * 9447 */ 9448 9449 /* Check the size of cdb */ 9450 9451 switch (GETGROUP(cdbp)) { 9452 case CDB_GROUPID_3: /* Reserved, per SPC-4 */ 9453 /* 9454 * opcodes 0x7e and 0x7f identify variable-length CDBs and 9455 * therefore require special handling. Return failure, for now. 9456 */ 9457 mutex_exit(cport_mutex); 9458 return (TRAN_BADPKT); 9459 9460 case CDB_GROUPID_6: /* Vendor-specific, per SPC-4 */ 9461 case CDB_GROUPID_7: /* Vendor-specific, per SPC-4 */ 9462 /* obtain length from the scsi_pkt */ 9463 cdblen = scsipkt->pkt_cdblen; 9464 break; 9465 9466 default: 9467 /* CDB's length is statically known, per SPC-4 */ 9468 cdblen = scsi_cdb_size[GETGROUP(cdbp)]; 9469 break; 9470 } 9471 9472 if (cdblen <= 0 || cdblen > sdinfo->satadrv_atapi_cdb_len) { 9473 sata_log(NULL, CE_WARN, 9474 "sata: invalid ATAPI cdb length %d", 9475 cdblen); 9476 mutex_exit(cport_mutex); 9477 return (TRAN_BADPKT); 9478 } 9479 9480 SATAATAPITRACE(spx, cdblen); 9481 9482 /* 9483 * For non-read/write commands we need to 9484 * map buffer 9485 */ 9486 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 9487 case SCMD_READ: 9488 case SCMD_READ_G1: 9489 case SCMD_READ_G5: 9490 case SCMD_READ_G4: 9491 case SCMD_WRITE: 9492 case SCMD_WRITE_G1: 9493 case SCMD_WRITE_G5: 9494 case SCMD_WRITE_G4: 9495 break; 9496 default: 9497 if (bp != NULL) { 9498 if (bp->b_flags & (B_PHYS | B_PAGEIO)) 9499 bp_mapin(bp); 9500 } 9501 break; 9502 } 9503 /* 9504 * scmd->satacmd_flags.sata_data_direction default - 9505 * SATA_DIR_NODATA_XFER - is set by 9506 * sata_txlt_generic_pkt_info(). 9507 */ 9508 if (scmd->satacmd_bp) { 9509 if (scmd->satacmd_bp->b_flags & B_READ) { 9510 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 9511 } else { 9512 scmd->satacmd_flags.sata_data_direction = 9513 SATA_DIR_WRITE; 9514 } 9515 } 9516 9517 /* 9518 * Set up ATAPI packet command. 9519 */ 9520 9521 sata_atapi_packet_cmd_setup(scmd, sdinfo); 9522 9523 /* Copy cdb into sata_cmd */ 9524 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 9525 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 9526 bcopy(cdbp, scmd->satacmd_acdb, cdblen); 9527 9528 /* See note in the command header */ 9529 if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) { 9530 if (scmd->satacmd_acdb[3] != 0) 9531 scmd->satacmd_acdb[4] = 255; 9532 } 9533 9534 #ifdef SATA_DEBUG 9535 if (sata_debug_flags & SATA_DBG_ATAPI) { 9536 uint8_t *p = scmd->satacmd_acdb; 9537 char buf[3 * SATA_ATAPI_MAX_CDB_LEN]; 9538 9539 (void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN, 9540 "%02x %02x %02x %02x %02x %02x %02x %02x " 9541 "%2x %02x %02x %02x %02x %02x %02x %02x", 9542 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 9543 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 9544 buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0'; 9545 cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf); 9546 } 9547 #endif 9548 9549 /* 9550 * Preset request sense data to NO SENSE. 9551 * If there is no way to get error information via Request Sense, 9552 * the packet request sense data would not have to be modified by HBA, 9553 * but it could be returned as is. 9554 */ 9555 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN); 9556 sata_fixed_sense_data_preset( 9557 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 9558 9559 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 9560 /* Need callback function */ 9561 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion; 9562 synch = FALSE; 9563 } else 9564 synch = TRUE; 9565 9566 /* Transfer command to HBA */ 9567 if (sata_hba_start(spx, &rval) != 0) { 9568 /* Pkt not accepted for execution */ 9569 mutex_exit(cport_mutex); 9570 return (rval); 9571 } 9572 mutex_exit(cport_mutex); 9573 /* 9574 * If execution is non-synchronous, 9575 * a callback function will handle potential errors, translate 9576 * the response and will do a callback to a target driver. 9577 * If it was synchronous, use the same framework callback to check 9578 * an execution status. 9579 */ 9580 if (synch) { 9581 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 9582 "synchronous execution status %x\n", 9583 spx->txlt_sata_pkt->satapkt_reason); 9584 sata_txlt_atapi_completion(spx->txlt_sata_pkt); 9585 } 9586 return (TRAN_ACCEPT); 9587 } 9588 9589 9590 /* 9591 * ATAPI Packet command completion. 9592 * 9593 * Failure of the command passed via Packet command are considered device 9594 * error. SATA HBA driver would have to retrieve error data (via Request 9595 * Sense command delivered via error retrieval sata packet) and copy it 9596 * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data. 9597 */ 9598 static void 9599 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt) 9600 { 9601 sata_pkt_txlate_t *spx = 9602 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 9603 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 9604 struct scsi_extended_sense *sense; 9605 struct buf *bp; 9606 int rval; 9607 9608 #ifdef SATA_DEBUG 9609 uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense; 9610 #endif 9611 9612 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 9613 STATE_SENT_CMD | STATE_GOT_STATUS; 9614 9615 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 9616 /* Normal completion */ 9617 if (sata_pkt->satapkt_cmd.satacmd_bp != NULL) 9618 scsipkt->pkt_state |= STATE_XFERRED_DATA; 9619 scsipkt->pkt_reason = CMD_CMPLT; 9620 *scsipkt->pkt_scbp = STATUS_GOOD; 9621 if (spx->txlt_tmp_buf != NULL) { 9622 /* Temporary buffer was used */ 9623 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 9624 if (bp->b_flags & B_READ) { 9625 rval = ddi_dma_sync( 9626 spx->txlt_buf_dma_handle, 0, 0, 9627 DDI_DMA_SYNC_FORCPU); 9628 ASSERT(rval == DDI_SUCCESS); 9629 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 9630 bp->b_bcount); 9631 } 9632 } 9633 } else { 9634 /* 9635 * Something went wrong - analyze return 9636 */ 9637 *scsipkt->pkt_scbp = STATUS_CHECK; 9638 sense = sata_arq_sense(spx); 9639 9640 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 9641 /* 9642 * pkt_reason should be CMD_CMPLT for DEVICE ERROR. 9643 * Under this condition ERR bit is set for ATA command, 9644 * and CHK bit set for ATAPI command. 9645 * 9646 * Please check st_intr & sdintr about how pkt_reason 9647 * is used. 9648 */ 9649 scsipkt->pkt_reason = CMD_CMPLT; 9650 9651 /* 9652 * We may not have ARQ data if there was a double 9653 * error. But sense data in sata packet was pre-set 9654 * with NO SENSE so it is valid even if HBA could 9655 * not retrieve a real sense data. 9656 * Just copy this sense data into scsi pkt sense area. 9657 */ 9658 bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense, 9659 SATA_ATAPI_MIN_RQSENSE_LEN); 9660 #ifdef SATA_DEBUG 9661 if (sata_debug_flags & SATA_DBG_SCSI_IF) { 9662 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 9663 "sata_txlt_atapi_completion: %02x\n" 9664 "RQSENSE: %02x %02x %02x %02x %02x %02x " 9665 " %02x %02x %02x %02x %02x %02x " 9666 " %02x %02x %02x %02x %02x %02x\n", 9667 scsipkt->pkt_reason, 9668 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 9669 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 9670 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 9671 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 9672 rqsp[16], rqsp[17]); 9673 } 9674 #endif 9675 } else { 9676 switch (sata_pkt->satapkt_reason) { 9677 case SATA_PKT_PORT_ERROR: 9678 /* 9679 * We have no device data. 9680 */ 9681 scsipkt->pkt_reason = CMD_INCOMPLETE; 9682 scsipkt->pkt_state &= ~(STATE_GOT_BUS | 9683 STATE_GOT_TARGET | STATE_SENT_CMD | 9684 STATE_GOT_STATUS); 9685 sense->es_key = KEY_HARDWARE_ERROR; 9686 break; 9687 9688 case SATA_PKT_TIMEOUT: 9689 scsipkt->pkt_reason = CMD_TIMEOUT; 9690 scsipkt->pkt_statistics |= 9691 STAT_TIMEOUT | STAT_DEV_RESET; 9692 /* 9693 * Need to check if HARDWARE_ERROR/ 9694 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more 9695 * appropriate. 9696 */ 9697 break; 9698 9699 case SATA_PKT_ABORTED: 9700 scsipkt->pkt_reason = CMD_ABORTED; 9701 scsipkt->pkt_statistics |= STAT_ABORTED; 9702 /* Should we set key COMMAND_ABPRTED? */ 9703 break; 9704 9705 case SATA_PKT_RESET: 9706 scsipkt->pkt_reason = CMD_RESET; 9707 scsipkt->pkt_statistics |= STAT_DEV_RESET; 9708 /* 9709 * May be we should set Unit Attention / 9710 * Reset. Perhaps the same should be 9711 * returned for disks.... 9712 */ 9713 sense->es_key = KEY_UNIT_ATTENTION; 9714 sense->es_add_code = SD_SCSI_ASC_RESET; 9715 break; 9716 9717 default: 9718 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 9719 "sata_txlt_atapi_completion: " 9720 "invalid packet completion reason")); 9721 scsipkt->pkt_reason = CMD_TRAN_ERR; 9722 scsipkt->pkt_state &= ~(STATE_GOT_BUS | 9723 STATE_GOT_TARGET | STATE_SENT_CMD | 9724 STATE_GOT_STATUS); 9725 break; 9726 } 9727 } 9728 } 9729 9730 SATAATAPITRACE(spx, 0); 9731 9732 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 9733 scsipkt->pkt_comp != NULL) { 9734 /* scsi callback required */ 9735 (*scsipkt->pkt_comp)(scsipkt); 9736 } 9737 } 9738 9739 /* 9740 * Set up error retrieval sata command for ATAPI Packet Command error data 9741 * recovery. 9742 * 9743 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up, 9744 * returns SATA_FAILURE otherwise. 9745 */ 9746 9747 static int 9748 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo) 9749 { 9750 sata_pkt_t *spkt = spx->txlt_sata_pkt; 9751 sata_cmd_t *scmd; 9752 struct buf *bp; 9753 9754 /* 9755 * Allocate dma-able buffer error data. 9756 * Buffer allocation will take care of buffer alignment and other DMA 9757 * attributes. 9758 */ 9759 bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN); 9760 if (bp == NULL) { 9761 SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst, 9762 "sata_get_err_retrieval_pkt: " 9763 "cannot allocate buffer for error data", NULL); 9764 return (SATA_FAILURE); 9765 } 9766 bp_mapin(bp); /* make data buffer accessible */ 9767 9768 /* Operation modes are up to the caller */ 9769 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 9770 9771 /* Synchronous mode, no callback - may be changed by the caller */ 9772 spkt->satapkt_comp = NULL; 9773 spkt->satapkt_time = sata_default_pkt_time; 9774 9775 scmd = &spkt->satapkt_cmd; 9776 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 9777 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 9778 9779 sata_atapi_packet_cmd_setup(scmd, sdinfo); 9780 9781 /* 9782 * Set-up acdb. Request Sense CDB (packet command content) is 9783 * not in DMA-able buffer. Its handling is HBA-specific (how 9784 * it is transfered into packet FIS). 9785 */ 9786 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 9787 bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN); 9788 /* Following zeroing of pad bytes may not be necessary */ 9789 bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN], 9790 sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN); 9791 9792 /* 9793 * Set-up pointer to the buffer handle, so HBA can sync buffer 9794 * before accessing it. Handle is in usual place in translate struct. 9795 */ 9796 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle; 9797 9798 /* 9799 * Preset request sense data to NO SENSE. 9800 * Here it is redundant, only for a symetry with scsi-originated 9801 * packets. It should not be used for anything but debugging. 9802 */ 9803 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN); 9804 sata_fixed_sense_data_preset( 9805 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 9806 9807 ASSERT(scmd->satacmd_num_dma_cookies != 0); 9808 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 9809 9810 return (SATA_SUCCESS); 9811 } 9812 9813 /* 9814 * Set-up ATAPI packet command. 9815 * Data transfer direction has to be set-up in sata_cmd structure prior to 9816 * calling this function. 9817 * 9818 * Returns void 9819 */ 9820 9821 static void 9822 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo) 9823 { 9824 scmd->satacmd_addr_type = 0; /* N/A */ 9825 scmd->satacmd_sec_count_lsb = 0; /* no tag */ 9826 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 9827 scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ; 9828 scmd->satacmd_lba_high_lsb = 9829 (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8); 9830 scmd->satacmd_cmd_reg = SATAC_PACKET; /* Command */ 9831 9832 /* 9833 * We want all data to be transfered via DMA. 9834 * But specify it only if drive supports DMA and DMA mode is 9835 * selected - some drives are sensitive about it. 9836 * Hopefully it wil work for all drives.... 9837 */ 9838 if (sdinfo->satadrv_settings & SATA_DEV_DMA) 9839 scmd->satacmd_features_reg = SATA_ATAPI_F_DMA; 9840 9841 /* 9842 * Features register requires special care for devices that use 9843 * Serial ATA bridge - they need an explicit specification of 9844 * the data transfer direction for Packet DMA commands. 9845 * Setting this bit is harmless if DMA is not used. 9846 * 9847 * Many drives do not implement word 80, specifying what ATA/ATAPI 9848 * spec they follow. 9849 * We are arbitrarily following the latest SerialATA 2.6 spec, 9850 * which uses ATA/ATAPI 6 specification for Identify Data, unless 9851 * ATA/ATAPI-7 support is explicitly indicated. 9852 */ 9853 if (sdinfo->satadrv_id.ai_majorversion != 0 && 9854 sdinfo->satadrv_id.ai_majorversion != 0xffff && 9855 (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) { 9856 /* 9857 * Specification of major version is valid and version 7 9858 * is supported. It does automatically imply that all 9859 * spec features are supported. For now, we assume that 9860 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete. 9861 */ 9862 if ((sdinfo->satadrv_id.ai_dirdma & 9863 SATA_ATAPI_ID_DMADIR_REQ) != 0) { 9864 if (scmd->satacmd_flags.sata_data_direction == 9865 SATA_DIR_READ) 9866 scmd->satacmd_features_reg |= 9867 SATA_ATAPI_F_DATA_DIR_READ; 9868 } 9869 } 9870 } 9871 9872 9873 #ifdef SATA_DEBUG 9874 9875 /* Display 18 bytes of Inquiry data */ 9876 static void 9877 sata_show_inqry_data(uint8_t *buf) 9878 { 9879 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 9880 uint8_t *p; 9881 9882 cmn_err(CE_NOTE, "Inquiry data:"); 9883 cmn_err(CE_NOTE, "device type %x", inq->inq_dtype); 9884 cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb); 9885 cmn_err(CE_NOTE, "version %x", inq->inq_ansi); 9886 cmn_err(CE_NOTE, "ATAPI transport version %d", 9887 SATA_ATAPI_TRANS_VERSION(inq)); 9888 cmn_err(CE_NOTE, "response data format %d, aenc %d", 9889 inq->inq_rdf, inq->inq_aenc); 9890 cmn_err(CE_NOTE, " additional length %d", inq->inq_len); 9891 cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs); 9892 p = (uint8_t *)inq->inq_vid; 9893 cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x " 9894 "%02x %02x %02x %02x", 9895 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 9896 p = (uint8_t *)inq->inq_vid; 9897 cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c", 9898 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 9899 9900 p = (uint8_t *)inq->inq_pid; 9901 cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x " 9902 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", 9903 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 9904 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 9905 p = (uint8_t *)inq->inq_pid; 9906 cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c " 9907 "%c %c %c %c %c %c %c %c", 9908 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 9909 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 9910 9911 p = (uint8_t *)inq->inq_revision; 9912 cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x", 9913 p[0], p[1], p[2], p[3]); 9914 p = (uint8_t *)inq->inq_revision; 9915 cmn_err(CE_NOTE, "revision: %c %c %c %c", 9916 p[0], p[1], p[2], p[3]); 9917 9918 } 9919 9920 9921 static void 9922 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count) 9923 { 9924 struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt; 9925 9926 if (scsi_pkt == NULL) 9927 return; 9928 if (count != 0) { 9929 /* saving cdb */ 9930 bzero(sata_atapi_trace[sata_atapi_trace_index].acdb, 9931 SATA_ATAPI_MAX_CDB_LEN); 9932 bcopy(scsi_pkt->pkt_cdbp, 9933 sata_atapi_trace[sata_atapi_trace_index].acdb, count); 9934 } else { 9935 bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)-> 9936 sts_sensedata, 9937 sata_atapi_trace[sata_atapi_trace_index].arqs, 9938 SATA_ATAPI_MIN_RQSENSE_LEN); 9939 sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason = 9940 scsi_pkt->pkt_reason; 9941 sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason = 9942 spx->txlt_sata_pkt->satapkt_reason; 9943 9944 if (++sata_atapi_trace_index >= 64) 9945 sata_atapi_trace_index = 0; 9946 } 9947 } 9948 9949 #endif 9950 9951 /* 9952 * Fetch inquiry data from ATAPI device 9953 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 9954 * 9955 * Note: 9956 * inqb pointer does not point to a DMA-able buffer. It is a local buffer 9957 * where the caller expects to see the inquiry data. 9958 * 9959 */ 9960 9961 static int 9962 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba, 9963 sata_address_t *saddr, struct scsi_inquiry *inq) 9964 { 9965 sata_pkt_txlate_t *spx; 9966 sata_pkt_t *spkt; 9967 struct buf *bp; 9968 sata_drive_info_t *sdinfo; 9969 sata_cmd_t *scmd; 9970 int rval; 9971 uint8_t *rqsp; 9972 dev_info_t *dip = SATA_DIP(sata_hba); 9973 #ifdef SATA_DEBUG 9974 char msg_buf[MAXPATHLEN]; 9975 #endif 9976 kmutex_t *cport_mutex; 9977 9978 ASSERT(sata_hba != NULL); 9979 9980 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 9981 spx->txlt_sata_hba_inst = sata_hba; 9982 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 9983 spkt = sata_pkt_alloc(spx, NULL); 9984 if (spkt == NULL) { 9985 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 9986 return (SATA_FAILURE); 9987 } 9988 /* address is needed now */ 9989 spkt->satapkt_device.satadev_addr = *saddr; 9990 9991 /* scsi_inquiry size buffer */ 9992 bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry)); 9993 if (bp == NULL) { 9994 sata_pkt_free(spx); 9995 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 9996 SATA_LOG_D((sata_hba, CE_WARN, 9997 "sata_get_atapi_inquiry_data: " 9998 "cannot allocate data buffer")); 9999 return (SATA_FAILURE); 10000 } 10001 bp_mapin(bp); /* make data buffer accessible */ 10002 10003 scmd = &spkt->satapkt_cmd; 10004 ASSERT(scmd->satacmd_num_dma_cookies != 0); 10005 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 10006 10007 /* Use synchronous mode */ 10008 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 10009 spkt->satapkt_comp = NULL; 10010 spkt->satapkt_time = sata_default_pkt_time; 10011 10012 /* Issue inquiry command - 6 bytes cdb, data transfer, read */ 10013 10014 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 10015 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 10016 10017 cport_mutex = &(SATA_CPORT_MUTEX(sata_hba, saddr->cport)); 10018 mutex_enter(cport_mutex); 10019 sdinfo = sata_get_device_info(sata_hba, 10020 &spx->txlt_sata_pkt->satapkt_device); 10021 if (sdinfo == NULL) { 10022 /* we have to be carefull about the disapearing device */ 10023 mutex_exit(cport_mutex); 10024 rval = SATA_FAILURE; 10025 goto cleanup; 10026 } 10027 sata_atapi_packet_cmd_setup(scmd, sdinfo); 10028 10029 /* 10030 * Set-up acdb. This works for atapi transport version 2 and later. 10031 */ 10032 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 10033 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 10034 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */ 10035 scmd->satacmd_acdb[1] = 0x00; 10036 scmd->satacmd_acdb[2] = 0x00; 10037 scmd->satacmd_acdb[3] = 0x00; 10038 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry); 10039 scmd->satacmd_acdb[5] = 0x00; 10040 10041 sata_fixed_sense_data_preset( 10042 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 10043 10044 /* Transfer command to HBA */ 10045 if (sata_hba_start(spx, &rval) != 0) { 10046 /* Pkt not accepted for execution */ 10047 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10048 "sata_get_atapi_inquiry_data: " 10049 "Packet not accepted for execution - ret: %02x", rval); 10050 mutex_exit(cport_mutex); 10051 rval = SATA_FAILURE; 10052 goto cleanup; 10053 } 10054 mutex_exit(cport_mutex); 10055 10056 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) { 10057 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10058 "sata_get_atapi_inquiry_data: " 10059 "Packet completed successfully - ret: %02x", rval); 10060 if (spx->txlt_buf_dma_handle != NULL) { 10061 /* 10062 * Sync buffer. Handle is in usual place in translate 10063 * struct. 10064 */ 10065 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 10066 DDI_DMA_SYNC_FORCPU); 10067 ASSERT(rval == DDI_SUCCESS); 10068 } 10069 10070 if (sata_check_for_dma_error(dip, spx)) { 10071 ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED); 10072 rval = SATA_FAILURE; 10073 } else { 10074 /* 10075 * Normal completion - copy data into caller's buffer 10076 */ 10077 bcopy(bp->b_un.b_addr, (uint8_t *)inq, 10078 sizeof (struct scsi_inquiry)); 10079 #ifdef SATA_DEBUG 10080 if (sata_debug_flags & SATA_DBG_ATAPI) { 10081 sata_show_inqry_data((uint8_t *)inq); 10082 } 10083 #endif 10084 rval = SATA_SUCCESS; 10085 } 10086 } else { 10087 /* 10088 * Something went wrong - analyze return - check rqsense data 10089 */ 10090 rval = SATA_FAILURE; 10091 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 10092 /* 10093 * ARQ data hopefull show something other than NO SENSE 10094 */ 10095 rqsp = scmd->satacmd_rqsense; 10096 #ifdef SATA_DEBUG 10097 if (sata_debug_flags & SATA_DBG_ATAPI) { 10098 msg_buf[0] = '\0'; 10099 (void) snprintf(msg_buf, MAXPATHLEN, 10100 "ATAPI packet completion reason: %02x\n" 10101 "RQSENSE: %02x %02x %02x %02x %02x %02x\n" 10102 " %02x %02x %02x %02x %02x %02x\n" 10103 " %02x %02x %02x %02x %02x %02x", 10104 spkt->satapkt_reason, 10105 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 10106 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 10107 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 10108 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 10109 rqsp[16], rqsp[17]); 10110 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 10111 "%s", msg_buf); 10112 } 10113 #endif 10114 } else { 10115 switch (spkt->satapkt_reason) { 10116 case SATA_PKT_PORT_ERROR: 10117 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10118 "sata_get_atapi_inquiry_data: " 10119 "packet reason: port error", NULL); 10120 break; 10121 10122 case SATA_PKT_TIMEOUT: 10123 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10124 "sata_get_atapi_inquiry_data: " 10125 "packet reason: timeout", NULL); 10126 break; 10127 10128 case SATA_PKT_ABORTED: 10129 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10130 "sata_get_atapi_inquiry_data: " 10131 "packet reason: aborted", NULL); 10132 break; 10133 10134 case SATA_PKT_RESET: 10135 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10136 "sata_get_atapi_inquiry_data: " 10137 "packet reason: reset\n", NULL); 10138 break; 10139 default: 10140 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10141 "sata_get_atapi_inquiry_data: " 10142 "invalid packet reason: %02x\n", 10143 spkt->satapkt_reason); 10144 break; 10145 } 10146 } 10147 } 10148 cleanup: 10149 sata_free_local_buffer(spx); 10150 sata_pkt_free(spx); 10151 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10152 return (rval); 10153 } 10154 10155 10156 10157 10158 10159 #if 0 10160 #ifdef SATA_DEBUG 10161 10162 /* 10163 * Test ATAPI packet command. 10164 * Single threaded test: send packet command in synch mode, process completion 10165 * 10166 */ 10167 static void 10168 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport) 10169 { 10170 sata_pkt_txlate_t *spx; 10171 sata_pkt_t *spkt; 10172 struct buf *bp; 10173 sata_device_t sata_device; 10174 sata_drive_info_t *sdinfo; 10175 sata_cmd_t *scmd; 10176 int rval; 10177 uint8_t *rqsp; 10178 10179 ASSERT(sata_hba_inst != NULL); 10180 sata_device.satadev_addr.cport = cport; 10181 sata_device.satadev_addr.pmport = 0; 10182 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 10183 sata_device.satadev_rev = SATA_DEVICE_REV; 10184 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 10185 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 10186 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 10187 if (sdinfo == NULL) { 10188 sata_log(sata_hba_inst, CE_WARN, 10189 "sata_test_atapi_packet_command: " 10190 "no device info for cport %d", 10191 sata_device.satadev_addr.cport); 10192 return; 10193 } 10194 10195 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 10196 spx->txlt_sata_hba_inst = sata_hba_inst; 10197 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 10198 spkt = sata_pkt_alloc(spx, NULL); 10199 if (spkt == NULL) { 10200 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10201 return; 10202 } 10203 /* address is needed now */ 10204 spkt->satapkt_device.satadev_addr = sata_device.satadev_addr; 10205 10206 /* 1024k buffer */ 10207 bp = sata_alloc_local_buffer(spx, 1024); 10208 if (bp == NULL) { 10209 sata_pkt_free(spx); 10210 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10211 sata_log(sata_hba_inst, CE_WARN, 10212 "sata_test_atapi_packet_command: " 10213 "cannot allocate data buffer"); 10214 return; 10215 } 10216 bp_mapin(bp); /* make data buffer accessible */ 10217 10218 scmd = &spkt->satapkt_cmd; 10219 ASSERT(scmd->satacmd_num_dma_cookies != 0); 10220 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 10221 10222 /* Use synchronous mode */ 10223 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 10224 10225 /* Synchronous mode, no callback - may be changed by the caller */ 10226 spkt->satapkt_comp = NULL; 10227 spkt->satapkt_time = sata_default_pkt_time; 10228 10229 /* Issue inquiry command - 6 bytes cdb, data transfer, read */ 10230 10231 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 10232 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 10233 10234 sata_atapi_packet_cmd_setup(scmd, sdinfo); 10235 10236 /* Set-up acdb. */ 10237 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 10238 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 10239 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */ 10240 scmd->satacmd_acdb[1] = 0x00; 10241 scmd->satacmd_acdb[2] = 0x00; 10242 scmd->satacmd_acdb[3] = 0x00; 10243 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry); 10244 scmd->satacmd_acdb[5] = 0x00; 10245 10246 sata_fixed_sense_data_preset( 10247 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 10248 10249 /* Transfer command to HBA */ 10250 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 10251 if (sata_hba_start(spx, &rval) != 0) { 10252 /* Pkt not accepted for execution */ 10253 sata_log(sata_hba_inst, CE_WARN, 10254 "sata_test_atapi_packet_command: " 10255 "Packet not accepted for execution - ret: %02x", rval); 10256 mutex_exit( 10257 &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 10258 goto cleanup; 10259 } 10260 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 10261 10262 if (spx->txlt_buf_dma_handle != NULL) { 10263 /* 10264 * Sync buffer. Handle is in usual place in translate struct. 10265 */ 10266 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 10267 DDI_DMA_SYNC_FORCPU); 10268 ASSERT(rval == DDI_SUCCESS); 10269 } 10270 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) { 10271 sata_log(sata_hba_inst, CE_WARN, 10272 "sata_test_atapi_packet_command: " 10273 "Packet completed successfully"); 10274 /* 10275 * Normal completion - show inquiry data 10276 */ 10277 sata_show_inqry_data((uint8_t *)bp->b_un.b_addr); 10278 } else { 10279 /* 10280 * Something went wrong - analyze return - check rqsense data 10281 */ 10282 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 10283 /* 10284 * ARQ data hopefull show something other than NO SENSE 10285 */ 10286 rqsp = scmd->satacmd_rqsense; 10287 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 10288 "ATAPI packet completion reason: %02x\n" 10289 "RQSENSE: %02x %02x %02x %02x %02x %02x " 10290 " %02x %02x %02x %02x %02x %02x " 10291 " %02x %02x %02x %02x %02x %02x\n", 10292 spkt->satapkt_reason, 10293 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 10294 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 10295 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 10296 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 10297 rqsp[16], rqsp[17]); 10298 } else { 10299 switch (spkt->satapkt_reason) { 10300 case SATA_PKT_PORT_ERROR: 10301 sata_log(sata_hba_inst, CE_WARN, 10302 "sata_test_atapi_packet_command: " 10303 "packet reason: port error\n"); 10304 break; 10305 10306 case SATA_PKT_TIMEOUT: 10307 sata_log(sata_hba_inst, CE_WARN, 10308 "sata_test_atapi_packet_command: " 10309 "packet reason: timeout\n"); 10310 break; 10311 10312 case SATA_PKT_ABORTED: 10313 sata_log(sata_hba_inst, CE_WARN, 10314 "sata_test_atapi_packet_command: " 10315 "packet reason: aborted\n"); 10316 break; 10317 10318 case SATA_PKT_RESET: 10319 sata_log(sata_hba_inst, CE_WARN, 10320 "sata_test_atapi_packet_command: " 10321 "packet reason: reset\n"); 10322 break; 10323 default: 10324 sata_log(sata_hba_inst, CE_WARN, 10325 "sata_test_atapi_packet_command: " 10326 "invalid packet reason: %02x\n", 10327 spkt->satapkt_reason); 10328 break; 10329 } 10330 } 10331 } 10332 cleanup: 10333 sata_free_local_buffer(spx); 10334 sata_pkt_free(spx); 10335 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10336 } 10337 10338 #endif /* SATA_DEBUG */ 10339 #endif /* 1 */ 10340 10341 10342 /* ************************** LOCAL HELPER FUNCTIONS *********************** */ 10343 10344 /* 10345 * Validate sata_tran info 10346 * SATA_FAILURE returns if structure is inconsistent or structure revision 10347 * does not match one used by the framework. 10348 * 10349 * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains 10350 * required function pointers. 10351 * Returns SATA_FAILURE otherwise. 10352 */ 10353 static int 10354 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran) 10355 { 10356 /* 10357 * SATA_TRAN_HBA_REV is the current (highest) revision number 10358 * of the SATA interface. 10359 */ 10360 if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) { 10361 sata_log(NULL, CE_WARN, 10362 "sata: invalid sata_hba_tran version %d for driver %s", 10363 sata_tran->sata_tran_hba_rev, ddi_driver_name(dip)); 10364 return (SATA_FAILURE); 10365 } 10366 10367 if (dip != sata_tran->sata_tran_hba_dip) { 10368 SATA_LOG_D((NULL, CE_WARN, 10369 "sata: inconsistent sata_tran_hba_dip " 10370 "%p / %p", sata_tran->sata_tran_hba_dip, dip)); 10371 return (SATA_FAILURE); 10372 } 10373 10374 if (sata_tran->sata_tran_probe_port == NULL || 10375 sata_tran->sata_tran_start == NULL || 10376 sata_tran->sata_tran_abort == NULL || 10377 sata_tran->sata_tran_reset_dport == NULL || 10378 sata_tran->sata_tran_hotplug_ops == NULL || 10379 sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL || 10380 sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate == 10381 NULL) { 10382 SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing " 10383 "required functions")); 10384 } 10385 return (SATA_SUCCESS); 10386 } 10387 10388 /* 10389 * Remove HBA instance from sata_hba_list. 10390 */ 10391 static void 10392 sata_remove_hba_instance(dev_info_t *dip) 10393 { 10394 sata_hba_inst_t *sata_hba_inst; 10395 10396 mutex_enter(&sata_mutex); 10397 for (sata_hba_inst = sata_hba_list; 10398 sata_hba_inst != (struct sata_hba_inst *)NULL; 10399 sata_hba_inst = sata_hba_inst->satahba_next) { 10400 if (sata_hba_inst->satahba_dip == dip) 10401 break; 10402 } 10403 10404 if (sata_hba_inst == (struct sata_hba_inst *)NULL) { 10405 #ifdef SATA_DEBUG 10406 cmn_err(CE_WARN, "sata_remove_hba_instance: " 10407 "unknown HBA instance\n"); 10408 #endif 10409 ASSERT(FALSE); 10410 } 10411 if (sata_hba_inst == sata_hba_list) { 10412 sata_hba_list = sata_hba_inst->satahba_next; 10413 if (sata_hba_list) { 10414 sata_hba_list->satahba_prev = 10415 (struct sata_hba_inst *)NULL; 10416 } 10417 if (sata_hba_inst == sata_hba_list_tail) { 10418 sata_hba_list_tail = NULL; 10419 } 10420 } else if (sata_hba_inst == sata_hba_list_tail) { 10421 sata_hba_list_tail = sata_hba_inst->satahba_prev; 10422 if (sata_hba_list_tail) { 10423 sata_hba_list_tail->satahba_next = 10424 (struct sata_hba_inst *)NULL; 10425 } 10426 } else { 10427 sata_hba_inst->satahba_prev->satahba_next = 10428 sata_hba_inst->satahba_next; 10429 sata_hba_inst->satahba_next->satahba_prev = 10430 sata_hba_inst->satahba_prev; 10431 } 10432 mutex_exit(&sata_mutex); 10433 } 10434 10435 /* 10436 * Probe all SATA ports of the specified HBA instance. 10437 * The assumption is that there are no target and attachment point minor nodes 10438 * created by the boot subsystems, so we do not need to prune device tree. 10439 * 10440 * This function is called only from sata_hba_attach(). It does not have to 10441 * be protected by controller mutex, because the hba_attached flag is not set 10442 * yet and no one would be touching this HBA instance other than this thread. 10443 * Determines if port is active and what type of the device is attached 10444 * (if any). Allocates necessary structures for each port. 10445 * 10446 * An AP (Attachement Point) node is created for each SATA device port even 10447 * when there is no device attached. 10448 */ 10449 10450 static void 10451 sata_probe_ports(sata_hba_inst_t *sata_hba_inst) 10452 { 10453 dev_info_t *dip = SATA_DIP(sata_hba_inst); 10454 int ncport; 10455 sata_cport_info_t *cportinfo; 10456 sata_drive_info_t *drive; 10457 sata_device_t sata_device; 10458 int rval; 10459 dev_t minor_number; 10460 char name[16]; 10461 clock_t start_time, cur_time; 10462 10463 /* 10464 * Probe controller ports first, to find port status and 10465 * any port multiplier attached. 10466 */ 10467 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 10468 /* allocate cport structure */ 10469 cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP); 10470 ASSERT(cportinfo != NULL); 10471 mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL); 10472 10473 mutex_enter(&cportinfo->cport_mutex); 10474 10475 cportinfo->cport_addr.cport = ncport; 10476 cportinfo->cport_addr.pmport = 0; 10477 cportinfo->cport_addr.qual = SATA_ADDR_CPORT; 10478 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 10479 cportinfo->cport_state |= SATA_STATE_PROBING; 10480 SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo; 10481 10482 /* 10483 * Regardless if a port is usable or not, create 10484 * an attachment point 10485 */ 10486 mutex_exit(&cportinfo->cport_mutex); 10487 minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip), 10488 ncport, 0, SATA_ADDR_CPORT); 10489 (void) sprintf(name, "%d", ncport); 10490 if (ddi_create_minor_node(dip, name, S_IFCHR, 10491 minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) != 10492 DDI_SUCCESS) { 10493 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: " 10494 "cannot create SATA attachment point for port %d", 10495 ncport); 10496 } 10497 10498 /* Probe port */ 10499 start_time = ddi_get_lbolt(); 10500 reprobe_cport: 10501 sata_device.satadev_addr.cport = ncport; 10502 sata_device.satadev_addr.pmport = 0; 10503 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 10504 sata_device.satadev_rev = SATA_DEVICE_REV; 10505 10506 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 10507 (dip, &sata_device); 10508 10509 mutex_enter(&cportinfo->cport_mutex); 10510 cportinfo->cport_scr = sata_device.satadev_scr; 10511 if (rval != SATA_SUCCESS) { 10512 /* Something went wrong? Fail the port */ 10513 cportinfo->cport_state = SATA_PSTATE_FAILED; 10514 mutex_exit(&cportinfo->cport_mutex); 10515 continue; 10516 } 10517 cportinfo->cport_state &= ~SATA_STATE_PROBING; 10518 cportinfo->cport_state |= SATA_STATE_PROBED; 10519 cportinfo->cport_dev_type = sata_device.satadev_type; 10520 10521 cportinfo->cport_state |= SATA_STATE_READY; 10522 if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) { 10523 mutex_exit(&cportinfo->cport_mutex); 10524 continue; 10525 } 10526 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 10527 /* 10528 * There is some device attached. 10529 * Allocate device info structure 10530 */ 10531 if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) { 10532 mutex_exit(&cportinfo->cport_mutex); 10533 SATA_CPORTINFO_DRV_INFO(cportinfo) = 10534 kmem_zalloc(sizeof (sata_drive_info_t), 10535 KM_SLEEP); 10536 mutex_enter(&cportinfo->cport_mutex); 10537 } 10538 drive = SATA_CPORTINFO_DRV_INFO(cportinfo); 10539 drive->satadrv_addr = cportinfo->cport_addr; 10540 drive->satadrv_addr.qual = SATA_ADDR_DCPORT; 10541 drive->satadrv_type = cportinfo->cport_dev_type; 10542 drive->satadrv_state = SATA_STATE_UNKNOWN; 10543 10544 mutex_exit(&cportinfo->cport_mutex); 10545 if (sata_add_device(dip, sata_hba_inst, &sata_device) != 10546 SATA_SUCCESS) { 10547 /* 10548 * Plugged device was not correctly identified. 10549 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT 10550 */ 10551 cur_time = ddi_get_lbolt(); 10552 if ((cur_time - start_time) < 10553 drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) { 10554 /* sleep for a while */ 10555 delay(drv_usectohz( 10556 SATA_DEV_RETRY_DLY)); 10557 goto reprobe_cport; 10558 } 10559 } 10560 } else { /* SATA_DTYPE_PMULT */ 10561 mutex_exit(&cportinfo->cport_mutex); 10562 10563 /* Allocate sata_pmult_info and sata_pmport_info */ 10564 if (sata_alloc_pmult(sata_hba_inst, &sata_device) != 10565 SATA_SUCCESS) 10566 continue; 10567 10568 /* Log the information of the port multiplier */ 10569 sata_show_pmult_info(sata_hba_inst, &sata_device); 10570 10571 /* Probe its pmports */ 10572 sata_probe_pmports(sata_hba_inst, ncport); 10573 } 10574 } 10575 } 10576 10577 /* 10578 * Probe all device ports behind a port multiplier. 10579 * 10580 * PMult-related structure should be allocated before by sata_alloc_pmult(). 10581 * 10582 * NOTE1: Only called from sata_probe_ports() 10583 * NOTE2: No mutex should be hold. 10584 */ 10585 static void 10586 sata_probe_pmports(sata_hba_inst_t *sata_hba_inst, uint8_t ncport) 10587 { 10588 dev_info_t *dip = SATA_DIP(sata_hba_inst); 10589 sata_pmult_info_t *pmultinfo = NULL; 10590 sata_pmport_info_t *pmportinfo = NULL; 10591 sata_drive_info_t *drive = NULL; 10592 sata_device_t sata_device; 10593 10594 clock_t start_time, cur_time; 10595 int npmport; 10596 int rval; 10597 10598 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, ncport); 10599 10600 /* Probe Port Multiplier ports */ 10601 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; npmport++) { 10602 pmportinfo = pmultinfo->pmult_dev_port[npmport]; 10603 start_time = ddi_get_lbolt(); 10604 reprobe_pmport: 10605 sata_device.satadev_addr.cport = ncport; 10606 sata_device.satadev_addr.pmport = npmport; 10607 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT; 10608 sata_device.satadev_rev = SATA_DEVICE_REV; 10609 10610 /* Let HBA driver probe it. */ 10611 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 10612 (dip, &sata_device); 10613 mutex_enter(&pmportinfo->pmport_mutex); 10614 10615 pmportinfo->pmport_scr = sata_device.satadev_scr; 10616 10617 if (rval != SATA_SUCCESS) { 10618 pmportinfo->pmport_state = 10619 SATA_PSTATE_FAILED; 10620 mutex_exit(&pmportinfo->pmport_mutex); 10621 continue; 10622 } 10623 pmportinfo->pmport_state &= ~SATA_STATE_PROBING; 10624 pmportinfo->pmport_state |= SATA_STATE_PROBED; 10625 pmportinfo->pmport_dev_type = sata_device.satadev_type; 10626 10627 pmportinfo->pmport_state |= SATA_STATE_READY; 10628 if (pmportinfo->pmport_dev_type == 10629 SATA_DTYPE_NONE) { 10630 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 10631 "no device found at port %d:%d", ncport, npmport); 10632 mutex_exit(&pmportinfo->pmport_mutex); 10633 continue; 10634 } 10635 /* Port multipliers cannot be chained */ 10636 ASSERT(pmportinfo->pmport_dev_type != SATA_DTYPE_PMULT); 10637 /* 10638 * There is something attached to Port 10639 * Multiplier device port 10640 * Allocate device info structure 10641 */ 10642 if (pmportinfo->pmport_sata_drive == NULL) { 10643 mutex_exit(&pmportinfo->pmport_mutex); 10644 pmportinfo->pmport_sata_drive = 10645 kmem_zalloc(sizeof (sata_drive_info_t), KM_SLEEP); 10646 mutex_enter(&pmportinfo->pmport_mutex); 10647 } 10648 drive = pmportinfo->pmport_sata_drive; 10649 drive->satadrv_addr.cport = pmportinfo->pmport_addr.cport; 10650 drive->satadrv_addr.pmport = npmport; 10651 drive->satadrv_addr.qual = SATA_ADDR_DPMPORT; 10652 drive->satadrv_type = pmportinfo-> pmport_dev_type; 10653 drive->satadrv_state = SATA_STATE_UNKNOWN; 10654 10655 mutex_exit(&pmportinfo->pmport_mutex); 10656 rval = sata_add_device(dip, sata_hba_inst, &sata_device); 10657 10658 if (rval != SATA_SUCCESS) { 10659 /* 10660 * Plugged device was not correctly identified. 10661 * Retry, within the SATA_DEV_IDENTIFY_TIMEOUT 10662 */ 10663 cur_time = ddi_get_lbolt(); 10664 if ((cur_time - start_time) < drv_usectohz( 10665 SATA_DEV_IDENTIFY_TIMEOUT)) { 10666 /* sleep for a while */ 10667 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 10668 goto reprobe_pmport; 10669 } 10670 } 10671 } 10672 } 10673 10674 /* 10675 * Add SATA device for specified HBA instance & port (SCSI target 10676 * device nodes). 10677 * This function is called (indirectly) only from sata_hba_attach(). 10678 * A target node is created when there is a supported type device attached, 10679 * but may be removed if it cannot be put online. 10680 * 10681 * This function cannot be called from an interrupt context. 10682 * 10683 * Create target nodes for disk, CD/DVD, Tape and ATAPI disk devices 10684 * 10685 * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when 10686 * device identification failed - adding a device could be retried. 10687 * 10688 */ 10689 static int 10690 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, 10691 sata_device_t *sata_device) 10692 { 10693 sata_cport_info_t *cportinfo; 10694 sata_pmult_info_t *pminfo; 10695 sata_pmport_info_t *pmportinfo; 10696 dev_info_t *cdip; /* child dip */ 10697 sata_address_t *saddr = &sata_device->satadev_addr; 10698 uint8_t cport, pmport; 10699 int rval; 10700 10701 cport = saddr->cport; 10702 pmport = saddr->pmport; 10703 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 10704 ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE); 10705 10706 /* 10707 * Some device is attached to a controller port. 10708 * We rely on controllers distinquishing between no-device, 10709 * attached port multiplier and other kind of attached device. 10710 * We need to get Identify Device data and determine 10711 * positively the dev type before trying to attach 10712 * the target driver. 10713 */ 10714 sata_device->satadev_rev = SATA_DEVICE_REV; 10715 switch (saddr->qual) { 10716 case SATA_ADDR_CPORT: 10717 /* 10718 * Add a non-port-multiplier device at controller port. 10719 */ 10720 saddr->qual = SATA_ADDR_DCPORT; 10721 10722 rval = sata_probe_device(sata_hba_inst, sata_device); 10723 if (rval != SATA_SUCCESS || 10724 sata_device->satadev_type == SATA_DTYPE_UNKNOWN) 10725 return (SATA_FAILURE); 10726 10727 mutex_enter(&cportinfo->cport_mutex); 10728 sata_show_drive_info(sata_hba_inst, 10729 SATA_CPORTINFO_DRV_INFO(cportinfo)); 10730 10731 if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) { 10732 /* 10733 * Could not determine device type or 10734 * a device is not supported. 10735 * Degrade this device to unknown. 10736 */ 10737 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 10738 mutex_exit(&cportinfo->cport_mutex); 10739 return (SATA_SUCCESS); 10740 } 10741 cportinfo->cport_dev_type = sata_device->satadev_type; 10742 cportinfo->cport_tgtnode_clean = B_TRUE; 10743 mutex_exit(&cportinfo->cport_mutex); 10744 10745 /* 10746 * Initialize device to the desired state. Even if it 10747 * fails, the device will still attach but syslog 10748 * will show the warning. 10749 */ 10750 if (sata_initialize_device(sata_hba_inst, 10751 SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) { 10752 /* Retry */ 10753 rval = sata_initialize_device(sata_hba_inst, 10754 SATA_CPORTINFO_DRV_INFO(cportinfo)); 10755 10756 if (rval == SATA_RETRY) 10757 sata_log(sata_hba_inst, CE_WARN, 10758 "SATA device at port %d - " 10759 "default device features could not be set." 10760 " Device may not operate as expected.", 10761 cport); 10762 } 10763 10764 cdip = sata_create_target_node(pdip, sata_hba_inst, saddr); 10765 if (cdip == NULL) { 10766 /* 10767 * Attaching target node failed. 10768 * We retain sata_drive_info structure... 10769 */ 10770 return (SATA_SUCCESS); 10771 } 10772 10773 mutex_enter(&cportinfo->cport_mutex); 10774 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 10775 satadrv_state = SATA_STATE_READY; 10776 mutex_exit(&cportinfo->cport_mutex); 10777 10778 break; 10779 10780 case SATA_ADDR_PMPORT: 10781 saddr->qual = SATA_ADDR_DPMPORT; 10782 10783 mutex_enter(&cportinfo->cport_mutex); 10784 /* It must be a Port Multiplier at the controller port */ 10785 ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT); 10786 10787 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 10788 pmportinfo = pminfo->pmult_dev_port[saddr->pmport]; 10789 mutex_exit(&cportinfo->cport_mutex); 10790 10791 rval = sata_probe_device(sata_hba_inst, sata_device); 10792 if (rval != SATA_SUCCESS || 10793 sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 10794 return (SATA_FAILURE); 10795 } 10796 10797 mutex_enter(&pmportinfo->pmport_mutex); 10798 sata_show_drive_info(sata_hba_inst, 10799 SATA_PMPORTINFO_DRV_INFO(pmportinfo)); 10800 10801 if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) { 10802 /* 10803 * Could not determine device type. 10804 * Degrade this device to unknown. 10805 */ 10806 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN; 10807 mutex_exit(&pmportinfo->pmport_mutex); 10808 return (SATA_SUCCESS); 10809 } 10810 pmportinfo->pmport_dev_type = sata_device->satadev_type; 10811 pmportinfo->pmport_tgtnode_clean = B_TRUE; 10812 mutex_exit(&pmportinfo->pmport_mutex); 10813 10814 /* 10815 * Initialize device to the desired state. 10816 * Even if it fails, the device will still 10817 * attach but syslog will show the warning. 10818 */ 10819 if (sata_initialize_device(sata_hba_inst, 10820 pmportinfo->pmport_sata_drive) != SATA_SUCCESS) { 10821 /* Retry */ 10822 rval = sata_initialize_device(sata_hba_inst, 10823 pmportinfo->pmport_sata_drive); 10824 10825 if (rval == SATA_RETRY) 10826 sata_log(sata_hba_inst, CE_WARN, 10827 "SATA device at port %d:%d - " 10828 "default device features could not be set." 10829 " Device may not operate as expected.", 10830 cport, pmport); 10831 } 10832 10833 cdip = sata_create_target_node(pdip, sata_hba_inst, saddr); 10834 if (cdip == NULL) { 10835 /* 10836 * Attaching target node failed. 10837 * We retain sata_drive_info structure... 10838 */ 10839 return (SATA_SUCCESS); 10840 } 10841 mutex_enter(&pmportinfo->pmport_mutex); 10842 pmportinfo->pmport_sata_drive->satadrv_state |= 10843 SATA_STATE_READY; 10844 mutex_exit(&pmportinfo->pmport_mutex); 10845 10846 break; 10847 10848 default: 10849 return (SATA_FAILURE); 10850 } 10851 10852 return (SATA_SUCCESS); 10853 } 10854 10855 /* 10856 * Clean up target node at specific address. 10857 * 10858 * NOTE: No Mutex should be hold. 10859 */ 10860 static int 10861 sata_offline_device(sata_hba_inst_t *sata_hba_inst, 10862 sata_device_t *sata_device, sata_drive_info_t *sdinfo) 10863 { 10864 uint8_t cport, pmport, qual; 10865 dev_info_t *tdip; 10866 10867 cport = sata_device->satadev_addr.cport; 10868 pmport = sata_device->satadev_addr.pmport; 10869 qual = sata_device->satadev_addr.qual; 10870 10871 if (qual == SATA_ADDR_DCPORT) { 10872 SATA_LOG_D((sata_hba_inst, CE_WARN, 10873 "sata_hba_ioctl: disconnect device at port %d", cport)); 10874 } else { 10875 SATA_LOG_D((sata_hba_inst, CE_WARN, 10876 "sata_hba_ioctl: disconnect device at port %d:%d", 10877 cport, pmport)); 10878 } 10879 10880 /* We are addressing attached device, not a port */ 10881 sata_device->satadev_addr.qual = 10882 sdinfo->satadrv_addr.qual; 10883 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 10884 &sata_device->satadev_addr); 10885 if (tdip != NULL && ndi_devi_offline(tdip, 10886 NDI_DEVI_REMOVE) != NDI_SUCCESS) { 10887 /* 10888 * Problem : 10889 * The target node remained attached. 10890 * This happens when the device file was open 10891 * or a node was waiting for resources. 10892 * Cannot do anything about it. 10893 */ 10894 if (qual == SATA_ADDR_DCPORT) { 10895 SATA_LOG_D((sata_hba_inst, CE_WARN, 10896 "sata_hba_ioctl: disconnect: could " 10897 "not unconfigure device before " 10898 "disconnecting the SATA port %d", 10899 cport)); 10900 } else { 10901 SATA_LOG_D((sata_hba_inst, CE_WARN, 10902 "sata_hba_ioctl: disconnect: could " 10903 "not unconfigure device before " 10904 "disconnecting the SATA port %d:%d", 10905 cport, pmport)); 10906 } 10907 /* 10908 * Set DEVICE REMOVED state in the target 10909 * node. It will prevent access to the device 10910 * even when a new device is attached, until 10911 * the old target node is released, removed and 10912 * recreated for a new device. 10913 */ 10914 sata_set_device_removed(tdip); 10915 10916 /* 10917 * Instruct event daemon to try the target 10918 * node cleanup later. 10919 */ 10920 sata_set_target_node_cleanup( 10921 sata_hba_inst, &sata_device->satadev_addr); 10922 } 10923 10924 10925 return (SATA_SUCCESS); 10926 } 10927 10928 10929 /* 10930 * Create scsi target node for attached device, create node properties and 10931 * attach the node. 10932 * The node could be removed if the device onlining fails. 10933 * 10934 * A dev_info_t pointer is returned if operation is successful, NULL is 10935 * returned otherwise. 10936 */ 10937 10938 static dev_info_t * 10939 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst, 10940 sata_address_t *sata_addr) 10941 { 10942 dev_info_t *cdip = NULL; 10943 int rval; 10944 char *nname = NULL; 10945 char **compatible = NULL; 10946 int ncompatible; 10947 struct scsi_inquiry inq; 10948 sata_device_t sata_device; 10949 sata_drive_info_t *sdinfo; 10950 int target; 10951 int i; 10952 10953 sata_device.satadev_rev = SATA_DEVICE_REV; 10954 sata_device.satadev_addr = *sata_addr; 10955 10956 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport))); 10957 10958 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 10959 10960 target = SATA_TO_SCSI_TARGET(sata_addr->cport, 10961 sata_addr->pmport, sata_addr->qual); 10962 10963 if (sdinfo == NULL) { 10964 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 10965 sata_addr->cport))); 10966 SATA_LOG_D((sata_hba_inst, CE_WARN, 10967 "sata_create_target_node: no sdinfo for target %x", 10968 target)); 10969 return (NULL); 10970 } 10971 10972 /* 10973 * create or get scsi inquiry data, expected by 10974 * scsi_hba_nodename_compatible_get() 10975 * SATA hard disks get Identify Data translated into Inguiry Data. 10976 * ATAPI devices respond directly to Inquiry request. 10977 */ 10978 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 10979 sata_identdev_to_inquiry(sata_hba_inst, sdinfo, 10980 (uint8_t *)&inq); 10981 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 10982 sata_addr->cport))); 10983 } else { /* Assume supported ATAPI device */ 10984 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 10985 sata_addr->cport))); 10986 if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr, 10987 &inq) == SATA_FAILURE) 10988 return (NULL); 10989 /* 10990 * Save supported ATAPI transport version 10991 */ 10992 sdinfo->satadrv_atapi_trans_ver = 10993 SATA_ATAPI_TRANS_VERSION(&inq); 10994 } 10995 10996 /* determine the node name and compatible */ 10997 scsi_hba_nodename_compatible_get(&inq, NULL, 10998 inq.inq_dtype, NULL, &nname, &compatible, &ncompatible); 10999 11000 #ifdef SATA_DEBUG 11001 if (sata_debug_flags & SATA_DBG_NODES) { 11002 if (nname == NULL) { 11003 cmn_err(CE_NOTE, "sata_create_target_node: " 11004 "cannot determine nodename for target %d\n", 11005 target); 11006 } else { 11007 cmn_err(CE_WARN, "sata_create_target_node: " 11008 "target %d nodename: %s\n", target, nname); 11009 } 11010 if (compatible == NULL) { 11011 cmn_err(CE_WARN, 11012 "sata_create_target_node: no compatible name\n"); 11013 } else { 11014 for (i = 0; i < ncompatible; i++) { 11015 cmn_err(CE_WARN, "sata_create_target_node: " 11016 "compatible name: %s\n", compatible[i]); 11017 } 11018 } 11019 } 11020 #endif 11021 11022 /* if nodename can't be determined, log error and exit */ 11023 if (nname == NULL) { 11024 SATA_LOG_D((sata_hba_inst, CE_WARN, 11025 "sata_create_target_node: cannot determine nodename " 11026 "for target %d\n", target)); 11027 scsi_hba_nodename_compatible_free(nname, compatible); 11028 return (NULL); 11029 } 11030 /* 11031 * Create scsi target node 11032 */ 11033 ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip); 11034 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 11035 "device-type", "scsi"); 11036 11037 if (rval != DDI_PROP_SUCCESS) { 11038 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 11039 "updating device_type prop failed %d", rval)); 11040 goto fail; 11041 } 11042 11043 /* 11044 * Create target node properties: target & lun 11045 */ 11046 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target); 11047 if (rval != DDI_PROP_SUCCESS) { 11048 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 11049 "updating target prop failed %d", rval)); 11050 goto fail; 11051 } 11052 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0); 11053 if (rval != DDI_PROP_SUCCESS) { 11054 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 11055 "updating target prop failed %d", rval)); 11056 goto fail; 11057 } 11058 11059 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 11060 /* 11061 * Add "variant" property 11062 */ 11063 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 11064 "variant", "atapi"); 11065 if (rval != DDI_PROP_SUCCESS) { 11066 SATA_LOG_D((sata_hba_inst, CE_WARN, 11067 "sata_create_target_node: variant atapi " 11068 "property could not be created: %d", rval)); 11069 goto fail; 11070 } 11071 } 11072 /* decorate the node with compatible */ 11073 if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible", 11074 compatible, ncompatible) != DDI_PROP_SUCCESS) { 11075 SATA_LOG_D((sata_hba_inst, CE_WARN, 11076 "sata_create_target_node: FAIL compatible props cdip 0x%p", 11077 (void *)cdip)); 11078 goto fail; 11079 } 11080 11081 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 11082 /* 11083 * Add "sata-phy" property 11084 */ 11085 if (ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "sata-phy", 11086 (int)sata_addr->cport) != DDI_PROP_SUCCESS) { 11087 SATA_LOG_D((sata_hba_inst, CE_WARN, 11088 "sata_create_target_node: failed to create " 11089 "\"sata-phy\" property: port %d", 11090 sata_addr->cport)); 11091 } 11092 } 11093 11094 11095 /* 11096 * Now, try to attach the driver. If probing of the device fails, 11097 * the target node may be removed 11098 */ 11099 rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH); 11100 11101 scsi_hba_nodename_compatible_free(nname, compatible); 11102 11103 if (rval == NDI_SUCCESS) 11104 return (cdip); 11105 11106 /* target node was removed - are we sure? */ 11107 return (NULL); 11108 11109 fail: 11110 scsi_hba_nodename_compatible_free(nname, compatible); 11111 ddi_prop_remove_all(cdip); 11112 rval = ndi_devi_free(cdip); 11113 if (rval != NDI_SUCCESS) { 11114 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 11115 "node removal failed %d", rval)); 11116 } 11117 sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: " 11118 "cannot create target node for SATA device at port %d", 11119 sata_addr->cport); 11120 return (NULL); 11121 } 11122 11123 /* 11124 * Remove a target node. 11125 */ 11126 static void 11127 sata_remove_target_node(sata_hba_inst_t *sata_hba_inst, 11128 sata_address_t *sata_addr) 11129 { 11130 dev_info_t *tdip; 11131 uint8_t cport = sata_addr->cport; 11132 uint8_t pmport = sata_addr->pmport; 11133 uint8_t qual = sata_addr->qual; 11134 11135 /* Note the sata daemon uses the address of the port/pmport */ 11136 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT); 11137 11138 /* Remove target node */ 11139 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), cport, pmport); 11140 if (tdip != NULL) { 11141 /* 11142 * Target node exists. Unconfigure device 11143 * then remove the target node (one ndi 11144 * operation). 11145 */ 11146 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) { 11147 /* 11148 * PROBLEM - no device, but target node remained. This 11149 * happens when the file was open or node was waiting 11150 * for resources. 11151 */ 11152 SATA_LOG_D((sata_hba_inst, CE_WARN, 11153 "sata_remove_target_node: " 11154 "Failed to remove target node for " 11155 "detached SATA device.")); 11156 /* 11157 * Set target node state to DEVI_DEVICE_REMOVED. But 11158 * re-check first that the node still exists. 11159 */ 11160 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 11161 cport, pmport); 11162 if (tdip != NULL) { 11163 sata_set_device_removed(tdip); 11164 /* 11165 * Instruct event daemon to retry the cleanup 11166 * later. 11167 */ 11168 sata_set_target_node_cleanup(sata_hba_inst, 11169 sata_addr); 11170 } 11171 } 11172 11173 if (qual == SATA_ADDR_CPORT) 11174 sata_log(sata_hba_inst, CE_WARN, 11175 "SATA device detached at port %d", cport); 11176 else 11177 sata_log(sata_hba_inst, CE_WARN, 11178 "SATA device detached at port %d:%d", 11179 cport, pmport); 11180 } 11181 #ifdef SATA_DEBUG 11182 else { 11183 if (qual == SATA_ADDR_CPORT) 11184 sata_log(sata_hba_inst, CE_WARN, 11185 "target node not found at port %d", cport); 11186 else 11187 sata_log(sata_hba_inst, CE_WARN, 11188 "target node not found at port %d:%d", 11189 cport, pmport); 11190 } 11191 #endif 11192 } 11193 11194 11195 /* 11196 * Re-probe sata port, check for a device and attach info 11197 * structures when necessary. Identify Device data is fetched, if possible. 11198 * Assumption: sata address is already validated. 11199 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of 11200 * the presence of a device and its type. 11201 * 11202 * flag arg specifies that the function should try multiple times to identify 11203 * device type and to initialize it, or it should return immediately on failure. 11204 * SATA_DEV_IDENTIFY_RETRY - retry 11205 * SATA_DEV_IDENTIFY_NORETRY - no retry 11206 * 11207 * SATA_FAILURE is returned if one of the operations failed. 11208 * 11209 * This function cannot be called in interrupt context - it may sleep. 11210 * 11211 * Note: Port multiplier is supported. 11212 */ 11213 static int 11214 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 11215 int flag) 11216 { 11217 sata_cport_info_t *cportinfo; 11218 sata_pmult_info_t *pmultinfo; 11219 sata_drive_info_t *sdinfo, *osdinfo; 11220 boolean_t init_device = B_FALSE; 11221 int prev_device_type = SATA_DTYPE_NONE; 11222 int prev_device_settings = 0; 11223 int prev_device_state = 0; 11224 clock_t start_time; 11225 int retry = B_FALSE; 11226 uint8_t cport = sata_device->satadev_addr.cport; 11227 int rval_probe, rval_init; 11228 11229 /* 11230 * If target is pmport, sata_reprobe_pmport() will handle it. 11231 */ 11232 if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT || 11233 sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) 11234 return (sata_reprobe_pmport(sata_hba_inst, sata_device, flag)); 11235 11236 /* We only care about host sata cport for now */ 11237 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 11238 sata_device->satadev_addr.cport); 11239 11240 /* 11241 * If a port multiplier was previously attached (we have no idea it 11242 * still there or not), sata_reprobe_pmult() will handle it. 11243 */ 11244 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) 11245 return (sata_reprobe_pmult(sata_hba_inst, sata_device, flag)); 11246 11247 /* Store sata_drive_info when a non-pmult device was attached. */ 11248 osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 11249 if (osdinfo != NULL) { 11250 /* 11251 * We are re-probing port with a previously attached device. 11252 * Save previous device type and settings. 11253 */ 11254 prev_device_type = cportinfo->cport_dev_type; 11255 prev_device_settings = osdinfo->satadrv_settings; 11256 prev_device_state = osdinfo->satadrv_state; 11257 } 11258 if (flag == SATA_DEV_IDENTIFY_RETRY) { 11259 start_time = ddi_get_lbolt(); 11260 retry = B_TRUE; 11261 } 11262 retry_probe: 11263 11264 /* probe port */ 11265 mutex_enter(&cportinfo->cport_mutex); 11266 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 11267 cportinfo->cport_state |= SATA_STATE_PROBING; 11268 mutex_exit(&cportinfo->cport_mutex); 11269 11270 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11271 (SATA_DIP(sata_hba_inst), sata_device); 11272 11273 mutex_enter(&cportinfo->cport_mutex); 11274 if (rval_probe != SATA_SUCCESS) { 11275 cportinfo->cport_state = SATA_PSTATE_FAILED; 11276 mutex_exit(&cportinfo->cport_mutex); 11277 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: " 11278 "SATA port %d probing failed", 11279 cportinfo->cport_addr.cport)); 11280 return (SATA_FAILURE); 11281 } 11282 11283 /* 11284 * update sata port state and set device type 11285 */ 11286 sata_update_port_info(sata_hba_inst, sata_device); 11287 cportinfo->cport_state &= ~SATA_STATE_PROBING; 11288 11289 /* 11290 * Sanity check - Port is active? Is the link active? 11291 * Is there any device attached? 11292 */ 11293 if ((cportinfo->cport_state & 11294 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 11295 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 11296 SATA_PORT_DEVLINK_UP) { 11297 /* 11298 * Port in non-usable state or no link active/no device. 11299 * Free info structure if necessary (direct attached drive 11300 * only, for now! 11301 */ 11302 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 11303 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 11304 /* Add here differentiation for device attached or not */ 11305 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 11306 mutex_exit(&cportinfo->cport_mutex); 11307 if (sdinfo != NULL) 11308 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11309 return (SATA_SUCCESS); 11310 } 11311 11312 cportinfo->cport_state |= SATA_STATE_READY; 11313 cportinfo->cport_state |= SATA_STATE_PROBED; 11314 11315 cportinfo->cport_dev_type = sata_device->satadev_type; 11316 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 11317 11318 /* 11319 * If we are re-probing the port, there may be 11320 * sata_drive_info structure attached 11321 */ 11322 if (sata_device->satadev_type == SATA_DTYPE_NONE) { 11323 11324 /* 11325 * There is no device, so remove device info structure, 11326 * if necessary. 11327 */ 11328 /* Device change: Drive -> None */ 11329 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 11330 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 11331 if (sdinfo != NULL) { 11332 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11333 sata_log(sata_hba_inst, CE_WARN, 11334 "SATA device detached " 11335 "from port %d", cportinfo->cport_addr.cport); 11336 } 11337 mutex_exit(&cportinfo->cport_mutex); 11338 return (SATA_SUCCESS); 11339 11340 } 11341 11342 if (sata_device->satadev_type != SATA_DTYPE_PMULT) { 11343 11344 /* Device (may) change: Drive -> Drive */ 11345 if (sdinfo == NULL) { 11346 /* 11347 * There is some device attached, but there is 11348 * no sata_drive_info structure - allocate one 11349 */ 11350 mutex_exit(&cportinfo->cport_mutex); 11351 sdinfo = kmem_zalloc( 11352 sizeof (sata_drive_info_t), KM_SLEEP); 11353 mutex_enter(&cportinfo->cport_mutex); 11354 /* 11355 * Recheck, that the port state did not change when we 11356 * released mutex. 11357 */ 11358 if (cportinfo->cport_state & SATA_STATE_READY) { 11359 SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo; 11360 sdinfo->satadrv_addr = cportinfo->cport_addr; 11361 sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT; 11362 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 11363 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 11364 } else { 11365 /* 11366 * Port is not in ready state, we 11367 * cannot attach a device. 11368 */ 11369 mutex_exit(&cportinfo->cport_mutex); 11370 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11371 return (SATA_SUCCESS); 11372 } 11373 /* 11374 * Since we are adding device, presumably new one, 11375 * indicate that it should be initalized, 11376 * as well as some internal framework states). 11377 */ 11378 init_device = B_TRUE; 11379 } 11380 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 11381 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual; 11382 } else { 11383 /* Device change: Drive -> PMult */ 11384 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 11385 if (sdinfo != NULL) { 11386 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11387 sata_log(sata_hba_inst, CE_WARN, 11388 "SATA device detached " 11389 "from port %d", cportinfo->cport_addr.cport); 11390 } 11391 11392 sata_log(sata_hba_inst, CE_WARN, 11393 "SATA port multiplier detected at port %d", 11394 cportinfo->cport_addr.cport); 11395 11396 mutex_exit(&cportinfo->cport_mutex); 11397 if (sata_alloc_pmult(sata_hba_inst, sata_device) != 11398 SATA_SUCCESS) 11399 return (SATA_FAILURE); 11400 sata_show_pmult_info(sata_hba_inst, sata_device); 11401 mutex_enter(&cportinfo->cport_mutex); 11402 11403 /* 11404 * Mark all the port multiplier port behind the port 11405 * multiplier behind with link events, so that the sata daemon 11406 * will update their status. 11407 */ 11408 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 11409 pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET; 11410 mutex_exit(&cportinfo->cport_mutex); 11411 return (SATA_SUCCESS); 11412 } 11413 mutex_exit(&cportinfo->cport_mutex); 11414 11415 /* 11416 * Figure out what kind of device we are really 11417 * dealing with. Failure of identifying device does not fail this 11418 * function. 11419 */ 11420 rval_probe = sata_probe_device(sata_hba_inst, sata_device); 11421 rval_init = SATA_FAILURE; 11422 mutex_enter(&cportinfo->cport_mutex); 11423 if (rval_probe == SATA_SUCCESS) { 11424 /* 11425 * If we are dealing with the same type of a device as before, 11426 * restore its settings flags. 11427 */ 11428 if (osdinfo != NULL && 11429 sata_device->satadev_type == prev_device_type) 11430 sdinfo->satadrv_settings = prev_device_settings; 11431 11432 mutex_exit(&cportinfo->cport_mutex); 11433 rval_init = SATA_SUCCESS; 11434 /* Set initial device features, if necessary */ 11435 if (init_device == B_TRUE) { 11436 rval_init = sata_initialize_device(sata_hba_inst, 11437 sdinfo); 11438 } 11439 if (rval_init == SATA_SUCCESS) 11440 return (rval_init); 11441 /* else we will retry if retry was asked for */ 11442 11443 } else { 11444 /* 11445 * If there was some device info before we probe the device, 11446 * restore previous device setting, so we can retry from scratch 11447 * later. Providing, of course, that device has not disapear 11448 * during probing process. 11449 */ 11450 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 11451 if (osdinfo != NULL) { 11452 cportinfo->cport_dev_type = prev_device_type; 11453 sdinfo->satadrv_type = prev_device_type; 11454 sdinfo->satadrv_state = prev_device_state; 11455 } 11456 } else { 11457 /* device is gone */ 11458 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11459 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 11460 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 11461 mutex_exit(&cportinfo->cport_mutex); 11462 return (SATA_SUCCESS); 11463 } 11464 mutex_exit(&cportinfo->cport_mutex); 11465 } 11466 11467 if (retry) { 11468 clock_t cur_time = ddi_get_lbolt(); 11469 /* 11470 * A device was not successfully identified or initialized. 11471 * Track retry time for device identification. 11472 */ 11473 if ((cur_time - start_time) < 11474 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 11475 /* sleep for a while */ 11476 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 11477 goto retry_probe; 11478 } 11479 /* else no more retries */ 11480 mutex_enter(&cportinfo->cport_mutex); 11481 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 11482 if (rval_init == SATA_RETRY) { 11483 /* 11484 * Setting drive features have failed, but 11485 * because the drive is still accessible, 11486 * keep it and emit a warning message. 11487 */ 11488 sata_log(sata_hba_inst, CE_WARN, 11489 "SATA device at port %d - desired " 11490 "drive features could not be set. " 11491 "Device may not operate as expected.", 11492 cportinfo->cport_addr.cport); 11493 } else { 11494 SATA_CPORTINFO_DRV_INFO(cportinfo)-> 11495 satadrv_state = SATA_DSTATE_FAILED; 11496 } 11497 } 11498 mutex_exit(&cportinfo->cport_mutex); 11499 } 11500 return (SATA_SUCCESS); 11501 } 11502 11503 /* 11504 * Reprobe a controller port that connected to a port multiplier. 11505 * 11506 * NOTE: No Mutex should be hold. 11507 */ 11508 static int 11509 sata_reprobe_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 11510 int flag) 11511 { 11512 _NOTE(ARGUNUSED(flag)) 11513 sata_cport_info_t *cportinfo; 11514 sata_pmult_info_t *pmultinfo; 11515 uint8_t cport = sata_device->satadev_addr.cport; 11516 int rval_probe; 11517 11518 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11519 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 11520 11521 /* probe port */ 11522 mutex_enter(&cportinfo->cport_mutex); 11523 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 11524 cportinfo->cport_state |= SATA_STATE_PROBING; 11525 mutex_exit(&cportinfo->cport_mutex); 11526 11527 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11528 (SATA_DIP(sata_hba_inst), sata_device); 11529 11530 mutex_enter(&cportinfo->cport_mutex); 11531 if (rval_probe != SATA_SUCCESS) { 11532 cportinfo->cport_state = SATA_PSTATE_FAILED; 11533 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmult: " 11534 "SATA port %d probing failed", cport)); 11535 sata_log(sata_hba_inst, CE_WARN, 11536 "SATA port multiplier detached at port %d", cport); 11537 mutex_exit(&cportinfo->cport_mutex); 11538 sata_free_pmult(sata_hba_inst, sata_device); 11539 return (SATA_FAILURE); 11540 } 11541 11542 /* 11543 * update sata port state and set device type 11544 */ 11545 sata_update_port_info(sata_hba_inst, sata_device); 11546 cportinfo->cport_state &= ~SATA_STATE_PROBING; 11547 cportinfo->cport_state |= SATA_STATE_PROBED; 11548 11549 /* 11550 * Sanity check - Port is active? Is the link active? 11551 * Is there any device attached? 11552 */ 11553 if ((cportinfo->cport_state & 11554 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 11555 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 11556 SATA_PORT_DEVLINK_UP || 11557 (sata_device->satadev_type == SATA_DTYPE_NONE)) { 11558 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 11559 mutex_exit(&cportinfo->cport_mutex); 11560 sata_free_pmult(sata_hba_inst, sata_device); 11561 sata_log(sata_hba_inst, CE_WARN, 11562 "SATA port multiplier detached at port %d", cport); 11563 return (SATA_SUCCESS); 11564 } 11565 11566 /* 11567 * Device changed: PMult -> Non-PMult 11568 * 11569 * This situation is uncommon, most possibly being caused by errors 11570 * after which the port multiplier is not correct initialized and 11571 * recognized. In that case the new device will be marked as unknown 11572 * and will not be automatically probed in this routine. Instead 11573 * system administrator could manually restart it via cfgadm(1M). 11574 */ 11575 if (sata_device->satadev_type != SATA_DTYPE_PMULT) { 11576 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 11577 mutex_exit(&cportinfo->cport_mutex); 11578 sata_free_pmult(sata_hba_inst, sata_device); 11579 sata_log(sata_hba_inst, CE_WARN, 11580 "SATA port multiplier detached at port %d", cport); 11581 return (SATA_FAILURE); 11582 } 11583 11584 /* 11585 * Now we know it is a port multiplier. However, if this is not the 11586 * previously attached port multiplier - they may have different 11587 * pmport numbers - we need to re-allocate data structures for every 11588 * pmport and drive. 11589 * 11590 * Port multipliers of the same model have identical values in these 11591 * registers, so it is still necessary to update the information of 11592 * all drives attached to the previous port multiplier afterwards. 11593 */ 11594 /* Device changed: PMult -> another PMult */ 11595 mutex_exit(&cportinfo->cport_mutex); 11596 sata_free_pmult(sata_hba_inst, sata_device); 11597 if (sata_alloc_pmult(sata_hba_inst, sata_device) != SATA_SUCCESS) 11598 return (SATA_FAILURE); 11599 mutex_enter(&cportinfo->cport_mutex); 11600 11601 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 11602 "SATA port multiplier [changed] at port %d", cport); 11603 sata_log(sata_hba_inst, CE_WARN, 11604 "SATA port multiplier detected at port %d", cport); 11605 11606 /* 11607 * Mark all the port multiplier port behind the port 11608 * multiplier behind with link events, so that the sata daemon 11609 * will update their status. 11610 */ 11611 pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET; 11612 mutex_exit(&cportinfo->cport_mutex); 11613 11614 return (SATA_SUCCESS); 11615 } 11616 11617 /* 11618 * Re-probe a port multiplier port, check for a device and attach info 11619 * structures when necessary. Identify Device data is fetched, if possible. 11620 * Assumption: sata address is already validated as port multiplier port. 11621 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of 11622 * the presence of a device and its type. 11623 * 11624 * flag arg specifies that the function should try multiple times to identify 11625 * device type and to initialize it, or it should return immediately on failure. 11626 * SATA_DEV_IDENTIFY_RETRY - retry 11627 * SATA_DEV_IDENTIFY_NORETRY - no retry 11628 * 11629 * SATA_FAILURE is returned if one of the operations failed. 11630 * 11631 * This function cannot be called in interrupt context - it may sleep. 11632 * 11633 * NOTE: Should be only called by sata_probe_port() in case target port is a 11634 * port multiplier port. 11635 * NOTE: No Mutex should be hold. 11636 */ 11637 static int 11638 sata_reprobe_pmport(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 11639 int flag) 11640 { 11641 sata_cport_info_t *cportinfo = NULL; 11642 sata_pmport_info_t *pmportinfo = NULL; 11643 sata_drive_info_t *sdinfo, *osdinfo; 11644 sata_device_t sdevice; 11645 boolean_t init_device = B_FALSE; 11646 int prev_device_type = SATA_DTYPE_NONE; 11647 int prev_device_settings = 0; 11648 int prev_device_state = 0; 11649 clock_t start_time; 11650 uint8_t cport = sata_device->satadev_addr.cport; 11651 uint8_t pmport = sata_device->satadev_addr.pmport; 11652 int rval; 11653 11654 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11655 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 11656 osdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 11657 11658 if (osdinfo != NULL) { 11659 /* 11660 * We are re-probing port with a previously attached device. 11661 * Save previous device type and settings. 11662 */ 11663 prev_device_type = pmportinfo->pmport_dev_type; 11664 prev_device_settings = osdinfo->satadrv_settings; 11665 prev_device_state = osdinfo->satadrv_state; 11666 } 11667 11668 start_time = ddi_get_lbolt(); 11669 11670 /* check parent status */ 11671 mutex_enter(&cportinfo->cport_mutex); 11672 if ((cportinfo->cport_state & 11673 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 11674 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 11675 SATA_PORT_DEVLINK_UP) { 11676 mutex_exit(&cportinfo->cport_mutex); 11677 return (SATA_FAILURE); 11678 } 11679 mutex_exit(&cportinfo->cport_mutex); 11680 11681 retry_probe_pmport: 11682 11683 /* probe port */ 11684 mutex_enter(&pmportinfo->pmport_mutex); 11685 pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 11686 pmportinfo->pmport_state |= SATA_STATE_PROBING; 11687 mutex_exit(&pmportinfo->pmport_mutex); 11688 11689 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11690 (SATA_DIP(sata_hba_inst), sata_device); 11691 11692 /* might need retry because we cannot touch registers. */ 11693 if (rval == SATA_FAILURE) { 11694 mutex_enter(&pmportinfo->pmport_mutex); 11695 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 11696 mutex_exit(&pmportinfo->pmport_mutex); 11697 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: " 11698 "SATA port %d:%d probing failed", 11699 cport, pmport)); 11700 return (SATA_FAILURE); 11701 } else if (rval == SATA_RETRY) { 11702 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: " 11703 "SATA port %d:%d probing failed, retrying...", 11704 cport, pmport)); 11705 clock_t cur_time = ddi_get_lbolt(); 11706 /* 11707 * A device was not successfully identified or initialized. 11708 * Track retry time for device identification. 11709 */ 11710 if ((cur_time - start_time) < 11711 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 11712 /* sleep for a while */ 11713 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 11714 goto retry_probe_pmport; 11715 } else { 11716 mutex_enter(&pmportinfo->pmport_mutex); 11717 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) 11718 SATA_PMPORTINFO_DRV_INFO(pmportinfo)-> 11719 satadrv_state = SATA_DSTATE_FAILED; 11720 mutex_exit(&pmportinfo->pmport_mutex); 11721 return (SATA_SUCCESS); 11722 } 11723 } 11724 11725 /* 11726 * Sanity check - Controller port is active? Is the link active? 11727 * Is it still a port multiplier? 11728 */ 11729 if ((cportinfo->cport_state & 11730 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 11731 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 11732 SATA_PORT_DEVLINK_UP || 11733 (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) { 11734 /* 11735 * Port in non-usable state or no link active/no 11736 * device. Free info structure. 11737 */ 11738 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 11739 11740 sdevice.satadev_addr.cport = cport; 11741 sdevice.satadev_addr.pmport = pmport; 11742 sdevice.satadev_addr.qual = SATA_ADDR_PMULT; 11743 mutex_exit(&cportinfo->cport_mutex); 11744 11745 sata_free_pmult(sata_hba_inst, &sdevice); 11746 return (SATA_FAILURE); 11747 } 11748 11749 /* SATA_SUCCESS NOW */ 11750 /* 11751 * update sata port state and set device type 11752 */ 11753 mutex_enter(&pmportinfo->pmport_mutex); 11754 sata_update_pmport_info(sata_hba_inst, sata_device); 11755 pmportinfo->pmport_state &= ~SATA_STATE_PROBING; 11756 11757 /* 11758 * Sanity check - Port is active? Is the link active? 11759 * Is there any device attached? 11760 */ 11761 if ((pmportinfo->pmport_state & 11762 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 11763 (pmportinfo->pmport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 11764 SATA_PORT_DEVLINK_UP) { 11765 /* 11766 * Port in non-usable state or no link active/no device. 11767 * Free info structure if necessary (direct attached drive 11768 * only, for now! 11769 */ 11770 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 11771 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 11772 /* Add here differentiation for device attached or not */ 11773 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 11774 mutex_exit(&pmportinfo->pmport_mutex); 11775 if (sdinfo != NULL) 11776 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11777 return (SATA_SUCCESS); 11778 } 11779 11780 pmportinfo->pmport_state |= SATA_STATE_READY; 11781 pmportinfo->pmport_dev_type = sata_device->satadev_type; 11782 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 11783 11784 /* 11785 * If we are re-probing the port, there may be 11786 * sata_drive_info structure attached 11787 * (or sata_pm_info, if PMult is supported). 11788 */ 11789 if (sata_device->satadev_type == SATA_DTYPE_NONE) { 11790 /* 11791 * There is no device, so remove device info structure, 11792 * if necessary. 11793 */ 11794 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 11795 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 11796 if (sdinfo != NULL) { 11797 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11798 sata_log(sata_hba_inst, CE_WARN, 11799 "SATA device detached from port %d:%d", 11800 cport, pmport); 11801 } 11802 mutex_exit(&pmportinfo->pmport_mutex); 11803 return (SATA_SUCCESS); 11804 } 11805 11806 /* this should not be a pmult */ 11807 ASSERT(sata_device->satadev_type != SATA_DTYPE_PMULT); 11808 if (sdinfo == NULL) { 11809 /* 11810 * There is some device attached, but there is 11811 * no sata_drive_info structure - allocate one 11812 */ 11813 mutex_exit(&pmportinfo->pmport_mutex); 11814 sdinfo = kmem_zalloc(sizeof (sata_drive_info_t), 11815 KM_SLEEP); 11816 mutex_enter(&pmportinfo->pmport_mutex); 11817 /* 11818 * Recheck, that the port state did not change when we 11819 * released mutex. 11820 */ 11821 if (pmportinfo->pmport_state & SATA_STATE_READY) { 11822 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = sdinfo; 11823 sdinfo->satadrv_addr = pmportinfo->pmport_addr; 11824 sdinfo->satadrv_addr.qual = SATA_ADDR_DPMPORT; 11825 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 11826 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 11827 } else { 11828 /* 11829 * Port is not in ready state, we 11830 * cannot attach a device. 11831 */ 11832 mutex_exit(&pmportinfo->pmport_mutex); 11833 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11834 return (SATA_SUCCESS); 11835 } 11836 /* 11837 * Since we are adding device, presumably new one, 11838 * indicate that it should be initalized, 11839 * as well as some internal framework states). 11840 */ 11841 init_device = B_TRUE; 11842 } 11843 11844 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN; 11845 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual; 11846 11847 mutex_exit(&pmportinfo->pmport_mutex); 11848 /* 11849 * Figure out what kind of device we are really 11850 * dealing with. 11851 */ 11852 rval = sata_probe_device(sata_hba_inst, sata_device); 11853 11854 mutex_enter(&pmportinfo->pmport_mutex); 11855 if (rval == SATA_SUCCESS) { 11856 /* 11857 * If we are dealing with the same type of a device as before, 11858 * restore its settings flags. 11859 */ 11860 if (osdinfo != NULL && 11861 sata_device->satadev_type == prev_device_type) 11862 sdinfo->satadrv_settings = prev_device_settings; 11863 11864 mutex_exit(&pmportinfo->pmport_mutex); 11865 /* Set initial device features, if necessary */ 11866 if (init_device == B_TRUE) { 11867 rval = sata_initialize_device(sata_hba_inst, sdinfo); 11868 } 11869 if (rval == SATA_SUCCESS) 11870 return (rval); 11871 } else { 11872 /* 11873 * If there was some device info before we probe the device, 11874 * restore previous device setting, so we can retry from scratch 11875 * later. Providing, of course, that device has not disappeared 11876 * during probing process. 11877 */ 11878 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 11879 if (osdinfo != NULL) { 11880 pmportinfo->pmport_dev_type = prev_device_type; 11881 sdinfo->satadrv_type = prev_device_type; 11882 sdinfo->satadrv_state = prev_device_state; 11883 } 11884 } else { 11885 /* device is gone */ 11886 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11887 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 11888 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 11889 mutex_exit(&pmportinfo->pmport_mutex); 11890 return (SATA_SUCCESS); 11891 } 11892 mutex_exit(&pmportinfo->pmport_mutex); 11893 } 11894 11895 if (flag == SATA_DEV_IDENTIFY_RETRY) { 11896 clock_t cur_time = ddi_get_lbolt(); 11897 /* 11898 * A device was not successfully identified or initialized. 11899 * Track retry time for device identification. 11900 */ 11901 if ((cur_time - start_time) < 11902 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 11903 /* sleep for a while */ 11904 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 11905 goto retry_probe_pmport; 11906 } else { 11907 mutex_enter(&pmportinfo->pmport_mutex); 11908 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) 11909 SATA_PMPORTINFO_DRV_INFO(pmportinfo)-> 11910 satadrv_state = SATA_DSTATE_FAILED; 11911 mutex_exit(&pmportinfo->pmport_mutex); 11912 } 11913 } 11914 return (SATA_SUCCESS); 11915 } 11916 11917 /* 11918 * Allocated related structure for a port multiplier and its device ports 11919 * 11920 * Port multiplier should be ready and probed, and related information like 11921 * the number of the device ports should be store in sata_device_t. 11922 * 11923 * NOTE: No Mutex should be hold. 11924 */ 11925 static int 11926 sata_alloc_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 11927 { 11928 dev_info_t *dip = SATA_DIP(sata_hba_inst); 11929 sata_cport_info_t *cportinfo = NULL; 11930 sata_pmult_info_t *pmultinfo = NULL; 11931 sata_pmport_info_t *pmportinfo = NULL; 11932 sata_device_t sd; 11933 dev_t minor_number; 11934 char name[16]; 11935 uint8_t cport = sata_device->satadev_addr.cport; 11936 int rval; 11937 int npmport; 11938 11939 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11940 11941 /* This function might be called while a port-mult is hot-plugged. */ 11942 mutex_enter(&cportinfo->cport_mutex); 11943 11944 /* dev_type's not updated when get called from sata_reprobe_port() */ 11945 if (SATA_CPORTINFO_PMULT_INFO(cportinfo) == NULL) { 11946 /* Create a pmult_info structure */ 11947 SATA_CPORTINFO_PMULT_INFO(cportinfo) = 11948 kmem_zalloc(sizeof (sata_pmult_info_t), KM_SLEEP); 11949 } 11950 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 11951 11952 pmultinfo->pmult_addr = sata_device->satadev_addr; 11953 pmultinfo->pmult_addr.qual = SATA_ADDR_PMULT; 11954 pmultinfo->pmult_state = SATA_STATE_PROBING; 11955 11956 /* 11957 * Probe the port multiplier with qualifier SATA_ADDR_PMULT_SPEC, 11958 * The HBA driver should initialize and register the port multiplier, 11959 * sata_register_pmult() will fill following fields, 11960 * + sata_pmult_info.pmult_gscr 11961 * + sata_pmult_info.pmult_num_dev_ports 11962 */ 11963 sd.satadev_addr = sata_device->satadev_addr; 11964 sd.satadev_addr.qual = SATA_ADDR_PMULT_SPEC; 11965 mutex_exit(&cportinfo->cport_mutex); 11966 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11967 (SATA_DIP(sata_hba_inst), &sd); 11968 mutex_enter(&cportinfo->cport_mutex); 11969 11970 if (rval != SATA_SUCCESS || 11971 (sd.satadev_type != SATA_DTYPE_PMULT) || 11972 !(sd.satadev_state & SATA_DSTATE_PMULT_INIT)) { 11973 SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL; 11974 kmem_free(pmultinfo, sizeof (sata_pmult_info_t)); 11975 cportinfo->cport_state = SATA_PSTATE_FAILED; 11976 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 11977 mutex_exit(&cportinfo->cport_mutex); 11978 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 11979 "sata_alloc_pmult: failed to initialize pmult " 11980 "at port %d.", cport) 11981 return (SATA_FAILURE); 11982 } 11983 11984 /* Initialize pmport_info structure */ 11985 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; 11986 npmport++) { 11987 11988 /* if everything is allocated, skip */ 11989 if (SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) != NULL) 11990 continue; 11991 11992 pmportinfo = kmem_zalloc(sizeof (sata_pmport_info_t), KM_SLEEP); 11993 mutex_init(&pmportinfo->pmport_mutex, NULL, MUTEX_DRIVER, NULL); 11994 mutex_exit(&cportinfo->cport_mutex); 11995 11996 mutex_enter(&pmportinfo->pmport_mutex); 11997 pmportinfo->pmport_addr.cport = cport; 11998 pmportinfo->pmport_addr.pmport = (uint8_t)npmport; 11999 pmportinfo->pmport_addr.qual = SATA_ADDR_PMPORT; 12000 pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 12001 mutex_exit(&pmportinfo->pmport_mutex); 12002 12003 mutex_enter(&cportinfo->cport_mutex); 12004 SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) = pmportinfo; 12005 12006 /* Create an attachment point */ 12007 minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip), 12008 cport, (uint8_t)npmport, SATA_ADDR_PMPORT); 12009 (void) sprintf(name, "%d.%d", cport, npmport); 12010 12011 if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number, 12012 DDI_NT_SATA_ATTACHMENT_POINT, 0) != DDI_SUCCESS) { 12013 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: " 12014 "cannot create SATA attachment point for " 12015 "port %d:%d", cport, npmport); 12016 } 12017 } 12018 12019 pmultinfo->pmult_state &= ~SATA_STATE_PROBING; 12020 pmultinfo->pmult_state |= (SATA_STATE_PROBED|SATA_STATE_READY); 12021 cportinfo->cport_dev_type = SATA_DTYPE_PMULT; 12022 12023 mutex_exit(&cportinfo->cport_mutex); 12024 return (SATA_SUCCESS); 12025 } 12026 12027 /* 12028 * Free data structures when a port multiplier is removed. 12029 * 12030 * NOTE: No Mutex should be hold. 12031 */ 12032 static void 12033 sata_free_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 12034 { 12035 sata_cport_info_t *cportinfo; 12036 sata_pmult_info_t *pmultinfo; 12037 sata_pmport_info_t *pmportinfo; 12038 sata_device_t pmport_device; 12039 sata_drive_info_t *sdinfo; 12040 dev_info_t *tdip; 12041 char name[16]; 12042 uint8_t cport = sata_device->satadev_addr.cport; 12043 int npmport; 12044 12045 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 12046 12047 /* This function might be called while port-mult is hot plugged. */ 12048 mutex_enter(&cportinfo->cport_mutex); 12049 12050 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 12051 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 12052 ASSERT(pmultinfo != NULL); 12053 12054 /* Free pmport_info structure */ 12055 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; 12056 npmport++) { 12057 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport); 12058 if (pmportinfo == NULL) 12059 continue; 12060 mutex_exit(&cportinfo->cport_mutex); 12061 12062 mutex_enter(&pmportinfo->pmport_mutex); 12063 sdinfo = pmportinfo->pmport_sata_drive; 12064 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 12065 mutex_exit(&pmportinfo->pmport_mutex); 12066 12067 /* Remove attachment point. */ 12068 name[0] = '\0'; 12069 (void) sprintf(name, "%d.%d", cport, npmport); 12070 ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name); 12071 sata_log(sata_hba_inst, CE_NOTE, 12072 "Remove attachment point of port %d:%d", 12073 cport, npmport); 12074 12075 /* 12076 * Rumove target node 12077 */ 12078 bzero(&pmport_device, sizeof (sata_device_t)); 12079 pmport_device.satadev_rev = SATA_DEVICE_REV; 12080 pmport_device.satadev_addr.cport = cport; 12081 pmport_device.satadev_addr.pmport = (uint8_t)npmport; 12082 pmport_device.satadev_addr.qual = SATA_ADDR_DPMPORT; 12083 12084 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 12085 &(pmport_device.satadev_addr)); 12086 if (tdip != NULL && ndi_devi_offline(tdip, 12087 NDI_DEVI_REMOVE) != NDI_SUCCESS) { 12088 /* 12089 * Problem : 12090 * The target node remained attached. 12091 * This happens when the device file was open 12092 * or a node was waiting for resources. 12093 * Cannot do anything about it. 12094 */ 12095 SATA_LOG_D((sata_hba_inst, CE_WARN, 12096 "sata_free_pmult: could not unconfigure device " 12097 "before disconnecting the SATA port %d:%d", 12098 cport, npmport)); 12099 12100 /* 12101 * Set DEVICE REMOVED state in the target 12102 * node. It will prevent access to the device 12103 * even when a new device is attached, until 12104 * the old target node is released, removed and 12105 * recreated for a new device. 12106 */ 12107 sata_set_device_removed(tdip); 12108 12109 /* 12110 * Instruct event daemon to try the target 12111 * node cleanup later. 12112 */ 12113 sata_set_target_node_cleanup( 12114 sata_hba_inst, &(pmport_device.satadev_addr)); 12115 12116 } 12117 mutex_enter(&cportinfo->cport_mutex); 12118 12119 /* 12120 * Add here differentiation for device attached or not 12121 */ 12122 if (sdinfo != NULL) { 12123 sata_log(sata_hba_inst, CE_WARN, 12124 "SATA device detached from port %d:%d", 12125 cport, npmport); 12126 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 12127 } 12128 12129 mutex_destroy(&pmportinfo->pmport_mutex); 12130 kmem_free(pmportinfo, sizeof (sata_pmport_info_t)); 12131 } 12132 12133 kmem_free(pmultinfo, sizeof (sata_pmult_info_t)); 12134 12135 cportinfo->cport_devp.cport_sata_pmult = NULL; 12136 12137 sata_log(sata_hba_inst, CE_WARN, 12138 "SATA port multiplier detached at port %d", cport); 12139 12140 mutex_exit(&cportinfo->cport_mutex); 12141 } 12142 12143 /* 12144 * Initialize device 12145 * Specified device is initialized to a default state. 12146 * 12147 * Returns SATA_SUCCESS if all device features are set successfully, 12148 * SATA_RETRY if device is accessible but device features were not set 12149 * successfully, and SATA_FAILURE otherwise. 12150 */ 12151 static int 12152 sata_initialize_device(sata_hba_inst_t *sata_hba_inst, 12153 sata_drive_info_t *sdinfo) 12154 { 12155 int rval; 12156 12157 sata_save_drive_settings(sdinfo); 12158 12159 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 12160 12161 sata_init_write_cache_mode(sdinfo); 12162 12163 rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0); 12164 12165 /* Determine current data transfer mode */ 12166 if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) { 12167 sdinfo->satadrv_settings &= ~SATA_DEV_DMA; 12168 } else if ((sdinfo->satadrv_id.ai_validinfo & 12169 SATA_VALIDINFO_88) != 0 && 12170 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) { 12171 sdinfo->satadrv_settings |= SATA_DEV_DMA; 12172 } else if ((sdinfo->satadrv_id.ai_dworddma & 12173 SATA_MDMA_SEL_MASK) != 0) { 12174 sdinfo->satadrv_settings |= SATA_DEV_DMA; 12175 } else 12176 /* DMA supported, not no DMA transfer mode is selected !? */ 12177 sdinfo->satadrv_settings &= ~SATA_DEV_DMA; 12178 12179 if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) && 12180 (sdinfo->satadrv_id.ai_features86 & 0x20)) 12181 sdinfo->satadrv_power_level = SATA_POWER_STANDBY; 12182 else 12183 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 12184 12185 return (rval); 12186 } 12187 12188 12189 /* 12190 * Initialize write cache mode. 12191 * 12192 * The default write cache setting for SATA HDD is provided by sata_write_cache 12193 * static variable. ATAPI CD/DVDs devices have write cache default is 12194 * determined by sata_atapicdvd_write_cache static variable. 12195 * ATAPI tape devices have write cache default is determined by 12196 * sata_atapitape_write_cache static variable. 12197 * ATAPI disk devices have write cache default is determined by 12198 * sata_atapidisk_write_cache static variable. 12199 * 1 - enable 12200 * 0 - disable 12201 * any other value - current drive setting 12202 * 12203 * Although there is not reason to disable write cache on CD/DVD devices, 12204 * tape devices and ATAPI disk devices, the default setting control is provided 12205 * for the maximun flexibility. 12206 * 12207 * In the future, it may be overridden by the 12208 * disk-write-cache-enable property setting, if it is defined. 12209 * Returns SATA_SUCCESS if all device features are set successfully, 12210 * SATA_FAILURE otherwise. 12211 */ 12212 static void 12213 sata_init_write_cache_mode(sata_drive_info_t *sdinfo) 12214 { 12215 switch (sdinfo->satadrv_type) { 12216 case SATA_DTYPE_ATADISK: 12217 if (sata_write_cache == 1) 12218 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 12219 else if (sata_write_cache == 0) 12220 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 12221 /* 12222 * When sata_write_cache value is not 0 or 1, 12223 * a current setting of the drive's write cache is used. 12224 */ 12225 break; 12226 case SATA_DTYPE_ATAPICD: 12227 if (sata_atapicdvd_write_cache == 1) 12228 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 12229 else if (sata_atapicdvd_write_cache == 0) 12230 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 12231 /* 12232 * When sata_atapicdvd_write_cache value is not 0 or 1, 12233 * a current setting of the drive's write cache is used. 12234 */ 12235 break; 12236 case SATA_DTYPE_ATAPITAPE: 12237 if (sata_atapitape_write_cache == 1) 12238 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 12239 else if (sata_atapitape_write_cache == 0) 12240 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 12241 /* 12242 * When sata_atapitape_write_cache value is not 0 or 1, 12243 * a current setting of the drive's write cache is used. 12244 */ 12245 break; 12246 case SATA_DTYPE_ATAPIDISK: 12247 if (sata_atapidisk_write_cache == 1) 12248 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 12249 else if (sata_atapidisk_write_cache == 0) 12250 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 12251 /* 12252 * When sata_atapidisk_write_cache value is not 0 or 1, 12253 * a current setting of the drive's write cache is used. 12254 */ 12255 break; 12256 } 12257 } 12258 12259 12260 /* 12261 * Validate sata address. 12262 * Specified cport, pmport and qualifier has to match 12263 * passed sata_scsi configuration info. 12264 * The presence of an attached device is not verified. 12265 * 12266 * Returns 0 when address is valid, -1 otherwise. 12267 */ 12268 static int 12269 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport, 12270 int pmport, int qual) 12271 { 12272 if (qual == SATA_ADDR_DCPORT && pmport != 0) 12273 goto invalid_address; 12274 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 12275 goto invalid_address; 12276 if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) && 12277 ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) || 12278 (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) || 12279 (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport)))) 12280 goto invalid_address; 12281 12282 return (0); 12283 12284 invalid_address: 12285 return (-1); 12286 12287 } 12288 12289 /* 12290 * Validate scsi address 12291 * SCSI target address is translated into SATA cport/pmport and compared 12292 * with a controller port/device configuration. LUN has to be 0. 12293 * Returns 0 if a scsi target refers to an attached device, 12294 * returns 1 if address is valid but no valid device is attached, 12295 * returns 2 if address is valid but device type is unknown (not valid device), 12296 * returns -1 if bad address or device is of an unsupported type. 12297 * Upon return sata_device argument is set. 12298 * 12299 * Port multiplier is supported now. 12300 */ 12301 static int 12302 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst, 12303 struct scsi_address *ap, sata_device_t *sata_device) 12304 { 12305 int cport, pmport, qual, rval; 12306 12307 rval = -1; /* Invalid address */ 12308 if (ap->a_lun != 0) 12309 goto out; 12310 12311 qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 12312 cport = SCSI_TO_SATA_CPORT(ap->a_target); 12313 pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 12314 12315 if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT) 12316 goto out; 12317 12318 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) == 12319 0) { 12320 12321 sata_cport_info_t *cportinfo; 12322 sata_pmult_info_t *pmultinfo; 12323 sata_drive_info_t *sdinfo = NULL; 12324 12325 sata_device->satadev_addr.qual = qual; 12326 sata_device->satadev_addr.cport = cport; 12327 sata_device->satadev_addr.pmport = pmport; 12328 sata_device->satadev_rev = SATA_DEVICE_REV_1; 12329 12330 rval = 1; /* Valid sata address */ 12331 12332 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 12333 if (qual == SATA_ADDR_DCPORT) { 12334 if (cportinfo == NULL || 12335 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 12336 goto out; 12337 12338 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 12339 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN && 12340 sdinfo != NULL) { 12341 rval = 2; 12342 goto out; 12343 } 12344 12345 if ((cportinfo->cport_dev_type & 12346 SATA_VALID_DEV_TYPE) == 0) { 12347 rval = -1; 12348 goto out; 12349 } 12350 12351 } else if (qual == SATA_ADDR_DPMPORT) { 12352 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 12353 if (pmultinfo == NULL) { 12354 rval = -1; 12355 goto out; 12356 } 12357 if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) == 12358 NULL || 12359 SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 12360 pmport) == SATA_DTYPE_NONE) 12361 goto out; 12362 12363 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, 12364 pmport); 12365 if (SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 12366 pmport) == SATA_DTYPE_UNKNOWN && sdinfo != NULL) { 12367 rval = 2; 12368 goto out; 12369 } 12370 12371 if ((SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 12372 pmport) & SATA_VALID_DEV_TYPE) == 0) { 12373 rval = -1; 12374 goto out; 12375 } 12376 12377 } else { 12378 rval = -1; 12379 goto out; 12380 } 12381 if ((sdinfo == NULL) || 12382 (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0) 12383 goto out; 12384 12385 sata_device->satadev_type = sdinfo->satadrv_type; 12386 12387 return (0); 12388 } 12389 out: 12390 if (rval > 0) { 12391 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 12392 "sata_validate_scsi_address: no valid target %x lun %x", 12393 ap->a_target, ap->a_lun); 12394 } 12395 return (rval); 12396 } 12397 12398 /* 12399 * Find dip corresponding to passed device number 12400 * 12401 * Returns NULL if invalid device number is passed or device cannot be found, 12402 * Returns dip is device is found. 12403 */ 12404 static dev_info_t * 12405 sata_devt_to_devinfo(dev_t dev) 12406 { 12407 dev_info_t *dip; 12408 #ifndef __lock_lint 12409 struct devnames *dnp; 12410 major_t major = getmajor(dev); 12411 int instance = SATA_MINOR2INSTANCE(getminor(dev)); 12412 12413 if (major >= devcnt) 12414 return (NULL); 12415 12416 dnp = &devnamesp[major]; 12417 LOCK_DEV_OPS(&(dnp->dn_lock)); 12418 dip = dnp->dn_head; 12419 while (dip && (ddi_get_instance(dip) != instance)) { 12420 dip = ddi_get_next(dip); 12421 } 12422 UNLOCK_DEV_OPS(&(dnp->dn_lock)); 12423 #endif 12424 12425 return (dip); 12426 } 12427 12428 12429 /* 12430 * Probe device. 12431 * This function issues Identify Device command and initializes local 12432 * sata_drive_info structure if the device can be identified. 12433 * The device type is determined by examining Identify Device 12434 * command response. 12435 * If the sata_hba_inst has linked drive info structure for this 12436 * device address, the Identify Device data is stored into sata_drive_info 12437 * structure linked to the port info structure. 12438 * 12439 * sata_device has to refer to the valid sata port(s) for HBA described 12440 * by sata_hba_inst structure. 12441 * 12442 * Returns: 12443 * SATA_SUCCESS if device type was successfully probed and port-linked 12444 * drive info structure was updated; 12445 * SATA_FAILURE if there is no device, or device was not probed 12446 * successully; 12447 * SATA_RETRY if device probe can be retried later. 12448 * If a device cannot be identified, sata_device's dev_state and dev_type 12449 * fields are set to unknown. 12450 * There are no retries in this function. Any retries should be managed by 12451 * the caller. 12452 */ 12453 12454 12455 static int 12456 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 12457 { 12458 sata_pmport_info_t *pmportinfo; 12459 sata_drive_info_t *sdinfo; 12460 sata_drive_info_t new_sdinfo; /* local drive info struct */ 12461 int rval; 12462 12463 ASSERT((SATA_CPORT_STATE(sata_hba_inst, 12464 sata_device->satadev_addr.cport) & 12465 (SATA_STATE_PROBED | SATA_STATE_READY)) != 0); 12466 12467 sata_device->satadev_type = SATA_DTYPE_NONE; 12468 12469 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12470 sata_device->satadev_addr.cport))); 12471 12472 if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) { 12473 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 12474 sata_device->satadev_addr.cport, 12475 sata_device->satadev_addr.pmport); 12476 ASSERT(pmportinfo != NULL); 12477 } 12478 12479 /* Get pointer to port-linked sata device info structure */ 12480 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 12481 if (sdinfo != NULL) { 12482 sdinfo->satadrv_state &= 12483 ~(SATA_STATE_PROBED | SATA_STATE_READY); 12484 sdinfo->satadrv_state |= SATA_STATE_PROBING; 12485 } else { 12486 /* No device to probe */ 12487 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12488 sata_device->satadev_addr.cport))); 12489 sata_device->satadev_type = SATA_DTYPE_NONE; 12490 sata_device->satadev_state = SATA_STATE_UNKNOWN; 12491 return (SATA_FAILURE); 12492 } 12493 /* 12494 * Need to issue both types of identify device command and 12495 * determine device type by examining retreived data/status. 12496 * First, ATA Identify Device. 12497 */ 12498 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 12499 new_sdinfo.satadrv_addr = sata_device->satadev_addr; 12500 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12501 sata_device->satadev_addr.cport))); 12502 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK; 12503 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 12504 if (rval == SATA_RETRY) { 12505 /* We may try to check for ATAPI device */ 12506 if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) { 12507 /* 12508 * HBA supports ATAPI - try to issue Identify Packet 12509 * Device command. 12510 */ 12511 new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI; 12512 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 12513 } 12514 } 12515 if (rval == SATA_SUCCESS) { 12516 /* 12517 * Got something responding positively to ATA Identify Device 12518 * or to Identify Packet Device cmd. 12519 * Save last used device type. 12520 */ 12521 sata_device->satadev_type = new_sdinfo.satadrv_type; 12522 12523 /* save device info, if possible */ 12524 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12525 sata_device->satadev_addr.cport))); 12526 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 12527 if (sdinfo == NULL) { 12528 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12529 sata_device->satadev_addr.cport))); 12530 return (SATA_FAILURE); 12531 } 12532 /* 12533 * Copy drive info into the port-linked drive info structure. 12534 */ 12535 *sdinfo = new_sdinfo; 12536 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 12537 sdinfo->satadrv_state |= SATA_STATE_PROBED; 12538 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 12539 SATA_CPORT_DEV_TYPE(sata_hba_inst, 12540 sata_device->satadev_addr.cport) = 12541 sdinfo->satadrv_type; 12542 else { /* SATA_ADDR_DPMPORT */ 12543 mutex_enter(&pmportinfo->pmport_mutex); 12544 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 12545 sata_device->satadev_addr.cport, 12546 sata_device->satadev_addr.pmport) = 12547 sdinfo->satadrv_type; 12548 mutex_exit(&pmportinfo->pmport_mutex); 12549 } 12550 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12551 sata_device->satadev_addr.cport))); 12552 return (SATA_SUCCESS); 12553 } 12554 12555 /* 12556 * It may be SATA_RETRY or SATA_FAILURE return. 12557 * Looks like we cannot determine the device type at this time. 12558 */ 12559 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12560 sata_device->satadev_addr.cport))); 12561 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 12562 if (sdinfo != NULL) { 12563 sata_device->satadev_type = SATA_DTYPE_UNKNOWN; 12564 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 12565 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 12566 sdinfo->satadrv_state |= SATA_STATE_PROBED; 12567 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 12568 SATA_CPORT_DEV_TYPE(sata_hba_inst, 12569 sata_device->satadev_addr.cport) = 12570 SATA_DTYPE_UNKNOWN; 12571 else { 12572 /* SATA_ADDR_DPMPORT */ 12573 mutex_enter(&pmportinfo->pmport_mutex); 12574 if ((SATA_PMULT_INFO(sata_hba_inst, 12575 sata_device->satadev_addr.cport) != NULL) && 12576 (SATA_PMPORT_INFO(sata_hba_inst, 12577 sata_device->satadev_addr.cport, 12578 sata_device->satadev_addr.pmport) != NULL)) 12579 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 12580 sata_device->satadev_addr.cport, 12581 sata_device->satadev_addr.pmport) = 12582 SATA_DTYPE_UNKNOWN; 12583 mutex_exit(&pmportinfo->pmport_mutex); 12584 } 12585 } 12586 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12587 sata_device->satadev_addr.cport))); 12588 return (rval); 12589 } 12590 12591 12592 /* 12593 * Get pointer to sata_drive_info structure. 12594 * 12595 * The sata_device has to contain address (cport, pmport and qualifier) for 12596 * specified sata_scsi structure. 12597 * 12598 * Returns NULL if device address is not valid for this HBA configuration. 12599 * Otherwise, returns a pointer to sata_drive_info structure. 12600 * 12601 * This function should be called with a port mutex held. 12602 */ 12603 static sata_drive_info_t * 12604 sata_get_device_info(sata_hba_inst_t *sata_hba_inst, 12605 sata_device_t *sata_device) 12606 { 12607 uint8_t cport = sata_device->satadev_addr.cport; 12608 uint8_t pmport = sata_device->satadev_addr.pmport; 12609 uint8_t qual = sata_device->satadev_addr.qual; 12610 12611 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 12612 return (NULL); 12613 12614 if (!(SATA_CPORT_STATE(sata_hba_inst, cport) & 12615 (SATA_STATE_PROBED | SATA_STATE_READY))) 12616 /* Port not probed yet */ 12617 return (NULL); 12618 12619 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE) 12620 return (NULL); 12621 12622 if (qual == SATA_ADDR_DCPORT) { 12623 /* Request for a device on a controller port */ 12624 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == 12625 SATA_DTYPE_PMULT) 12626 /* Port multiplier attached */ 12627 return (NULL); 12628 return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport)); 12629 } 12630 if (qual == SATA_ADDR_DPMPORT) { 12631 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 12632 SATA_DTYPE_PMULT) 12633 return (NULL); 12634 12635 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) 12636 return (NULL); 12637 12638 if (!(SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) & 12639 (SATA_STATE_PROBED | SATA_STATE_READY))) 12640 /* Port multiplier port not probed yet */ 12641 return (NULL); 12642 12643 return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport)); 12644 } 12645 12646 /* we should not get here */ 12647 return (NULL); 12648 } 12649 12650 12651 /* 12652 * sata_identify_device. 12653 * Send Identify Device command to SATA HBA driver. 12654 * If command executes successfully, update sata_drive_info structure pointed 12655 * to by sdinfo argument, including Identify Device data. 12656 * If command fails, invalidate data in sata_drive_info. 12657 * 12658 * Cannot be called from interrupt level. 12659 * 12660 * Returns: 12661 * SATA_SUCCESS if the device was identified as a supported device, 12662 * SATA_RETRY if the device was not identified but could be retried, 12663 * SATA_FAILURE if the device was not identified and identify attempt 12664 * should not be retried. 12665 */ 12666 static int 12667 sata_identify_device(sata_hba_inst_t *sata_hba_inst, 12668 sata_drive_info_t *sdinfo) 12669 { 12670 uint16_t cfg_word; 12671 int rval; 12672 12673 /* fetch device identify data */ 12674 if ((rval = sata_fetch_device_identify_data(sata_hba_inst, 12675 sdinfo)) != SATA_SUCCESS) 12676 goto fail_unknown; 12677 12678 cfg_word = sdinfo->satadrv_id.ai_config; 12679 12680 /* Set the correct device type */ 12681 if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) { 12682 sdinfo->satadrv_type = SATA_DTYPE_ATADISK; 12683 } else if (cfg_word == SATA_CFA_TYPE) { 12684 /* It's a Compact Flash media via CF-to-SATA HDD adapter */ 12685 sdinfo->satadrv_type = SATA_DTYPE_ATADISK; 12686 } else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) { 12687 switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) { 12688 case SATA_ATAPI_CDROM_DEV: 12689 sdinfo->satadrv_type = SATA_DTYPE_ATAPICD; 12690 break; 12691 case SATA_ATAPI_SQACC_DEV: 12692 sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE; 12693 break; 12694 case SATA_ATAPI_DIRACC_DEV: 12695 sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK; 12696 break; 12697 case SATA_ATAPI_PROC_DEV: 12698 sdinfo->satadrv_type = SATA_DTYPE_ATAPIPROC; 12699 break; 12700 default: 12701 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 12702 } 12703 } else { 12704 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 12705 } 12706 12707 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 12708 if (sdinfo->satadrv_capacity == 0) { 12709 /* Non-LBA disk. Too bad... */ 12710 sata_log(sata_hba_inst, CE_WARN, 12711 "SATA disk device at port %d does not support LBA", 12712 sdinfo->satadrv_addr.cport); 12713 rval = SATA_FAILURE; 12714 goto fail_unknown; 12715 } 12716 } 12717 #if 0 12718 /* Left for historical reason */ 12719 /* 12720 * Some initial version of SATA spec indicated that at least 12721 * UDMA mode 4 has to be supported. It is not metioned in 12722 * SerialATA 2.6, so this restriction is removed. 12723 */ 12724 /* Check for Ultra DMA modes 6 through 0 being supported */ 12725 for (i = 6; i >= 0; --i) { 12726 if (sdinfo->satadrv_id.ai_ultradma & (1 << i)) 12727 break; 12728 } 12729 12730 /* 12731 * At least UDMA 4 mode has to be supported. If mode 4 or 12732 * higher are not supported by the device, fail this 12733 * device. 12734 */ 12735 if (i < 4) { 12736 /* No required Ultra DMA mode supported */ 12737 sata_log(sata_hba_inst, CE_WARN, 12738 "SATA disk device at port %d does not support UDMA " 12739 "mode 4 or higher", sdinfo->satadrv_addr.cport); 12740 SATA_LOG_D((sata_hba_inst, CE_WARN, 12741 "mode 4 or higher required, %d supported", i)); 12742 rval = SATA_FAILURE; 12743 goto fail_unknown; 12744 } 12745 #endif 12746 12747 /* 12748 * For Disk devices, if it doesn't support UDMA mode, we would 12749 * like to return failure directly. 12750 */ 12751 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 12752 !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 && 12753 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) { 12754 sata_log(sata_hba_inst, CE_WARN, 12755 "SATA disk device at port %d does not support UDMA", 12756 sdinfo->satadrv_addr.cport); 12757 rval = SATA_FAILURE; 12758 goto fail_unknown; 12759 } 12760 12761 return (SATA_SUCCESS); 12762 12763 fail_unknown: 12764 /* Invalidate sata_drive_info ? */ 12765 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 12766 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 12767 return (rval); 12768 } 12769 12770 /* 12771 * Log/display device information 12772 */ 12773 static void 12774 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst, 12775 sata_drive_info_t *sdinfo) 12776 { 12777 int valid_version; 12778 char msg_buf[MAXPATHLEN]; 12779 int i; 12780 12781 /* Show HBA path */ 12782 (void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf); 12783 12784 cmn_err(CE_CONT, "?%s :\n", msg_buf); 12785 12786 switch (sdinfo->satadrv_type) { 12787 case SATA_DTYPE_ATADISK: 12788 (void) sprintf(msg_buf, "SATA disk device at"); 12789 break; 12790 12791 case SATA_DTYPE_ATAPICD: 12792 (void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at"); 12793 break; 12794 12795 case SATA_DTYPE_ATAPITAPE: 12796 (void) sprintf(msg_buf, "SATA tape (ATAPI) device at"); 12797 break; 12798 12799 case SATA_DTYPE_ATAPIDISK: 12800 (void) sprintf(msg_buf, "SATA disk (ATAPI) device at"); 12801 break; 12802 12803 case SATA_DTYPE_ATAPIPROC: 12804 (void) sprintf(msg_buf, "SATA processor (ATAPI) device at"); 12805 break; 12806 12807 case SATA_DTYPE_UNKNOWN: 12808 (void) sprintf(msg_buf, 12809 "Unsupported SATA device type (cfg 0x%x) at ", 12810 sdinfo->satadrv_id.ai_config); 12811 break; 12812 } 12813 12814 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT) 12815 cmn_err(CE_CONT, "?\t%s port %d\n", 12816 msg_buf, sdinfo->satadrv_addr.cport); 12817 else 12818 cmn_err(CE_CONT, "?\t%s port %d:%d\n", 12819 msg_buf, sdinfo->satadrv_addr.cport, 12820 sdinfo->satadrv_addr.pmport); 12821 12822 bcopy(&sdinfo->satadrv_id.ai_model, msg_buf, 12823 sizeof (sdinfo->satadrv_id.ai_model)); 12824 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model)); 12825 msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0'; 12826 cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf); 12827 12828 bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf, 12829 sizeof (sdinfo->satadrv_id.ai_fw)); 12830 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw)); 12831 msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0'; 12832 cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf); 12833 12834 bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf, 12835 sizeof (sdinfo->satadrv_id.ai_drvser)); 12836 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser)); 12837 msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0'; 12838 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 12839 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 12840 } else { 12841 /* 12842 * Some drives do not implement serial number and may 12843 * violate the spec by providing spaces rather than zeros 12844 * in serial number field. Scan the buffer to detect it. 12845 */ 12846 for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) { 12847 if (msg_buf[i] != '\0' && msg_buf[i] != ' ') 12848 break; 12849 } 12850 if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) { 12851 cmn_err(CE_CONT, "?\tserial number - none\n"); 12852 } else { 12853 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 12854 } 12855 } 12856 12857 #ifdef SATA_DEBUG 12858 if (sdinfo->satadrv_id.ai_majorversion != 0 && 12859 sdinfo->satadrv_id.ai_majorversion != 0xffff) { 12860 int i; 12861 for (i = 14; i >= 2; i--) { 12862 if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) { 12863 valid_version = i; 12864 break; 12865 } 12866 } 12867 cmn_err(CE_CONT, 12868 "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n", 12869 valid_version, 12870 sdinfo->satadrv_id.ai_majorversion, 12871 sdinfo->satadrv_id.ai_minorversion); 12872 } 12873 #endif 12874 /* Log some info */ 12875 cmn_err(CE_CONT, "?\tsupported features:\n"); 12876 msg_buf[0] = '\0'; 12877 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 12878 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) 12879 (void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN); 12880 else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) 12881 (void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN); 12882 } 12883 if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA) 12884 (void) strlcat(msg_buf, "DMA", MAXPATHLEN); 12885 if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) 12886 (void) strlcat(msg_buf, ", Native Command Queueing", 12887 MAXPATHLEN); 12888 if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ) 12889 (void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN); 12890 if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) && 12891 (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED)) 12892 (void) strlcat(msg_buf, ", SMART", MAXPATHLEN); 12893 if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) && 12894 (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED)) 12895 (void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN); 12896 cmn_err(CE_CONT, "?\t %s\n", msg_buf); 12897 if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA3) 12898 cmn_err(CE_CONT, "?\tSATA Gen3 signaling speed (6.0Gbps)\n"); 12899 else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2) 12900 cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n"); 12901 else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1) 12902 cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n"); 12903 if (sdinfo->satadrv_features_support & 12904 (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) { 12905 msg_buf[0] = '\0'; 12906 (void) snprintf(msg_buf, MAXPATHLEN, 12907 "Supported queue depth %d", 12908 sdinfo->satadrv_queue_depth); 12909 if (!(sata_func_enable & 12910 (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ))) 12911 (void) strlcat(msg_buf, 12912 " - queueing disabled globally", MAXPATHLEN); 12913 else if (sdinfo->satadrv_queue_depth > 12914 sdinfo->satadrv_max_queue_depth) { 12915 (void) snprintf(&msg_buf[strlen(msg_buf)], 12916 MAXPATHLEN - strlen(msg_buf), ", limited to %d", 12917 (int)sdinfo->satadrv_max_queue_depth); 12918 } 12919 cmn_err(CE_CONT, "?\t%s\n", msg_buf); 12920 } 12921 12922 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 12923 (void) sprintf(msg_buf, "\tcapacity = %lu sectors\n", 12924 sdinfo->satadrv_capacity); 12925 cmn_err(CE_CONT, "?%s", msg_buf); 12926 } 12927 } 12928 12929 /* 12930 * Log/display port multiplier information 12931 * No Mutex should be hold. 12932 */ 12933 static void 12934 sata_show_pmult_info(sata_hba_inst_t *sata_hba_inst, 12935 sata_device_t *sata_device) 12936 { 12937 _NOTE(ARGUNUSED(sata_hba_inst)) 12938 12939 int cport = sata_device->satadev_addr.cport; 12940 sata_pmult_info_t *pmultinfo; 12941 char msg_buf[MAXPATHLEN]; 12942 uint32_t gscr0, gscr1, gscr2, gscr64; 12943 12944 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 12945 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 12946 if (pmultinfo == NULL) { 12947 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 12948 return; 12949 } 12950 12951 gscr0 = pmultinfo->pmult_gscr.gscr0; 12952 gscr1 = pmultinfo->pmult_gscr.gscr1; 12953 gscr2 = pmultinfo->pmult_gscr.gscr2; 12954 gscr64 = pmultinfo->pmult_gscr.gscr64; 12955 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 12956 12957 cmn_err(CE_CONT, "?Port Multiplier %d device-ports found at port %d", 12958 sata_device->satadev_add_info, sata_device->satadev_addr.cport); 12959 12960 (void) sprintf(msg_buf, "\tVendor_ID 0x%04x, Module_ID 0x%04x", 12961 gscr0 & 0xffff, (gscr0 >> 16) & 0xffff); 12962 cmn_err(CE_CONT, "?%s", msg_buf); 12963 12964 (void) strcpy(msg_buf, "\tSupport SATA PMP Spec "); 12965 if (gscr1 & (1 << 3)) 12966 (void) strlcat(msg_buf, "1.2", MAXPATHLEN); 12967 else if (gscr1 & (1 << 2)) 12968 (void) strlcat(msg_buf, "1.1", MAXPATHLEN); 12969 else if (gscr1 & (1 << 1)) 12970 (void) strlcat(msg_buf, "1.0", MAXPATHLEN); 12971 else 12972 (void) strlcat(msg_buf, "unknown", MAXPATHLEN); 12973 cmn_err(CE_CONT, "?%s", msg_buf); 12974 12975 (void) strcpy(msg_buf, "\tSupport "); 12976 if (gscr64 & (1 << 3)) 12977 (void) strlcat(msg_buf, "Asy-Notif, ", 12978 MAXPATHLEN); 12979 if (gscr64 & (1 << 2)) 12980 (void) strlcat(msg_buf, "Dyn-SSC, ", MAXPATHLEN); 12981 if (gscr64 & (1 << 1)) 12982 (void) strlcat(msg_buf, "Iss-PMREQ, ", MAXPATHLEN); 12983 if (gscr64 & (1 << 0)) 12984 (void) strlcat(msg_buf, "BIST", MAXPATHLEN); 12985 if ((gscr64 & 0xf) == 0) 12986 (void) strlcat(msg_buf, "nothing", MAXPATHLEN); 12987 cmn_err(CE_CONT, "?%s", msg_buf); 12988 12989 (void) sprintf(msg_buf, "\tNumber of exposed device fan-out ports: %d", 12990 gscr2 & SATA_PMULT_PORTNUM_MASK); 12991 cmn_err(CE_CONT, "?%s", msg_buf); 12992 } 12993 12994 /* 12995 * sata_save_drive_settings extracts current setting of the device and stores 12996 * it for future reference, in case the device setup would need to be restored 12997 * after the device reset. 12998 * 12999 * For all devices read ahead and write cache settings are saved, if the 13000 * device supports these features at all. 13001 * For ATAPI devices the Removable Media Status Notification setting is saved. 13002 */ 13003 static void 13004 sata_save_drive_settings(sata_drive_info_t *sdinfo) 13005 { 13006 if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) || 13007 SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) { 13008 13009 /* Current setting of Read Ahead (and Read Cache) */ 13010 if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id)) 13011 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 13012 else 13013 sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD; 13014 13015 /* Current setting of Write Cache */ 13016 if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id)) 13017 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 13018 else 13019 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 13020 } 13021 13022 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) { 13023 if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id)) 13024 sdinfo->satadrv_settings |= SATA_DEV_RMSN; 13025 else 13026 sdinfo->satadrv_settings &= ~SATA_DEV_RMSN; 13027 } 13028 } 13029 13030 13031 /* 13032 * sata_check_capacity function determines a disk capacity 13033 * and addressing mode (LBA28/LBA48) by examining a disk identify device data. 13034 * 13035 * NOTE: CHS mode is not supported! If a device does not support LBA, 13036 * this function is not called. 13037 * 13038 * Returns device capacity in number of blocks, i.e. largest addressable LBA+1 13039 */ 13040 static uint64_t 13041 sata_check_capacity(sata_drive_info_t *sdinfo) 13042 { 13043 uint64_t capacity = 0; 13044 int i; 13045 13046 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK || 13047 !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT) 13048 /* Capacity valid only for LBA-addressable disk devices */ 13049 return (0); 13050 13051 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) && 13052 (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) && 13053 (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) { 13054 /* LBA48 mode supported and enabled */ 13055 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 | 13056 SATA_DEV_F_LBA28; 13057 for (i = 3; i >= 0; --i) { 13058 capacity <<= 16; 13059 capacity += sdinfo->satadrv_id.ai_addrsecxt[i]; 13060 } 13061 } else { 13062 capacity = sdinfo->satadrv_id.ai_addrsec[1]; 13063 capacity <<= 16; 13064 capacity += sdinfo->satadrv_id.ai_addrsec[0]; 13065 if (capacity >= 0x1000000) 13066 /* LBA28 mode */ 13067 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28; 13068 } 13069 return (capacity); 13070 } 13071 13072 13073 /* 13074 * Allocate consistent buffer for DMA transfer 13075 * 13076 * Cannot be called from interrupt level or with mutex held - it may sleep. 13077 * 13078 * Returns pointer to allocated buffer structure, or NULL if allocation failed. 13079 */ 13080 static struct buf * 13081 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len) 13082 { 13083 struct scsi_address ap; 13084 struct buf *bp; 13085 ddi_dma_attr_t cur_dma_attr; 13086 13087 ASSERT(spx->txlt_sata_pkt != NULL); 13088 ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran; 13089 ap.a_target = SATA_TO_SCSI_TARGET( 13090 spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport, 13091 spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport, 13092 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual); 13093 ap.a_lun = 0; 13094 13095 bp = scsi_alloc_consistent_buf(&ap, NULL, len, 13096 B_READ, SLEEP_FUNC, NULL); 13097 13098 if (bp != NULL) { 13099 /* Allocate DMA resources for this buffer */ 13100 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 13101 /* 13102 * We use a local version of the dma_attr, to account 13103 * for a device addressing limitations. 13104 * sata_adjust_dma_attr() will handle sdinfo == NULL which 13105 * will cause dma attributes to be adjusted to a lowest 13106 * acceptable level. 13107 */ 13108 sata_adjust_dma_attr(NULL, 13109 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 13110 13111 if (sata_dma_buf_setup(spx, PKT_CONSISTENT, 13112 SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) { 13113 scsi_free_consistent_buf(bp); 13114 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 13115 bp = NULL; 13116 } 13117 } 13118 return (bp); 13119 } 13120 13121 /* 13122 * Release local buffer (consistent buffer for DMA transfer) allocated 13123 * via sata_alloc_local_buffer(). 13124 */ 13125 static void 13126 sata_free_local_buffer(sata_pkt_txlate_t *spx) 13127 { 13128 ASSERT(spx->txlt_sata_pkt != NULL); 13129 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL); 13130 13131 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0; 13132 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL; 13133 13134 sata_common_free_dma_rsrcs(spx); 13135 13136 /* Free buffer */ 13137 scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp); 13138 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 13139 } 13140 13141 /* 13142 * Allocate sata_pkt 13143 * Pkt structure version and embedded strcutures version are initialized. 13144 * sata_pkt and sata_pkt_txlate structures are cross-linked. 13145 * 13146 * Since this may be called in interrupt context by sata_scsi_init_pkt, 13147 * callback argument determines if it can sleep or not. 13148 * Hence, it should not be called from interrupt context. 13149 * 13150 * If successful, non-NULL pointer to a sata pkt is returned. 13151 * Upon failure, NULL pointer is returned. 13152 */ 13153 static sata_pkt_t * 13154 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t)) 13155 { 13156 sata_pkt_t *spkt; 13157 int kmsflag; 13158 13159 kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP; 13160 spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag); 13161 if (spkt == NULL) { 13162 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 13163 "sata_pkt_alloc: failed")); 13164 return (NULL); 13165 } 13166 spkt->satapkt_rev = SATA_PKT_REV; 13167 spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV; 13168 spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 13169 spkt->satapkt_framework_private = spx; 13170 spx->txlt_sata_pkt = spkt; 13171 return (spkt); 13172 } 13173 13174 /* 13175 * Free sata pkt allocated via sata_pkt_alloc() 13176 */ 13177 static void 13178 sata_pkt_free(sata_pkt_txlate_t *spx) 13179 { 13180 ASSERT(spx->txlt_sata_pkt != NULL); 13181 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL); 13182 kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t)); 13183 spx->txlt_sata_pkt = NULL; 13184 } 13185 13186 13187 /* 13188 * Adjust DMA attributes. 13189 * SCSI cmds block count is up to 24 bits, SATA cmd block count vary 13190 * from 8 bits to 16 bits, depending on a command being used. 13191 * Limiting max block count arbitrarily to 256 for all read/write 13192 * commands may affects performance, so check both the device and 13193 * controller capability before adjusting dma attributes. 13194 */ 13195 void 13196 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr, 13197 ddi_dma_attr_t *adj_dma_attr) 13198 { 13199 uint32_t count_max; 13200 13201 /* Copy original attributes */ 13202 *adj_dma_attr = *dma_attr; 13203 /* 13204 * Things to consider: device addressing capability, 13205 * "excessive" controller DMA capabilities. 13206 * If a device is being probed/initialized, there are 13207 * no device info - use default limits then. 13208 */ 13209 if (sdinfo == NULL) { 13210 count_max = dma_attr->dma_attr_granular * 0x100; 13211 if (dma_attr->dma_attr_count_max > count_max) 13212 adj_dma_attr->dma_attr_count_max = count_max; 13213 if (dma_attr->dma_attr_maxxfer > count_max) 13214 adj_dma_attr->dma_attr_maxxfer = count_max; 13215 return; 13216 } 13217 13218 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 13219 if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) { 13220 /* 13221 * 16-bit sector count may be used - we rely on 13222 * the assumption that only read and write cmds 13223 * will request more than 256 sectors worth of data 13224 */ 13225 count_max = adj_dma_attr->dma_attr_granular * 0x10000; 13226 } else { 13227 /* 13228 * 8-bit sector count will be used - default limits 13229 * for dma attributes 13230 */ 13231 count_max = adj_dma_attr->dma_attr_granular * 0x100; 13232 } 13233 /* 13234 * Adjust controler dma attributes, if necessary 13235 */ 13236 if (dma_attr->dma_attr_count_max > count_max) 13237 adj_dma_attr->dma_attr_count_max = count_max; 13238 if (dma_attr->dma_attr_maxxfer > count_max) 13239 adj_dma_attr->dma_attr_maxxfer = count_max; 13240 } 13241 } 13242 13243 13244 /* 13245 * Allocate DMA resources for the buffer 13246 * This function handles initial DMA resource allocation as well as 13247 * DMA window shift and may be called repeatedly for the same DMA window 13248 * until all DMA cookies in the DMA window are processed. 13249 * To guarantee that there is always a coherent set of cookies to process 13250 * by SATA HBA driver (observing alignment, device granularity, etc.), 13251 * the number of slots for DMA cookies is equal to lesser of a number of 13252 * cookies in a DMA window and a max number of scatter/gather entries. 13253 * 13254 * Returns DDI_SUCCESS upon successful operation. 13255 * Return failure code of a failing command or DDI_FAILURE when 13256 * internal cleanup failed. 13257 */ 13258 static int 13259 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags, 13260 int (*callback)(caddr_t), caddr_t arg, 13261 ddi_dma_attr_t *cur_dma_attr) 13262 { 13263 int rval; 13264 off_t offset; 13265 size_t size; 13266 int max_sg_len, req_len, i; 13267 uint_t dma_flags; 13268 struct buf *bp; 13269 uint64_t cur_txfer_len; 13270 13271 13272 ASSERT(spx->txlt_sata_pkt != NULL); 13273 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 13274 ASSERT(bp != NULL); 13275 13276 13277 if (spx->txlt_buf_dma_handle == NULL) { 13278 /* 13279 * No DMA resources allocated so far - this is a first call 13280 * for this sata pkt. 13281 */ 13282 rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst), 13283 cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle); 13284 13285 if (rval != DDI_SUCCESS) { 13286 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 13287 "sata_dma_buf_setup: no buf DMA resources %x", 13288 rval)); 13289 return (rval); 13290 } 13291 13292 if (bp->b_flags & B_READ) 13293 dma_flags = DDI_DMA_READ; 13294 else 13295 dma_flags = DDI_DMA_WRITE; 13296 13297 if (flags & PKT_CONSISTENT) 13298 dma_flags |= DDI_DMA_CONSISTENT; 13299 13300 if (flags & PKT_DMA_PARTIAL) 13301 dma_flags |= DDI_DMA_PARTIAL; 13302 13303 /* 13304 * Check buffer alignment and size against dma attributes 13305 * Consider dma_attr_align only. There may be requests 13306 * with the size lower than device granularity, but they 13307 * will not read/write from/to the device, so no adjustment 13308 * is necessary. The dma_attr_minxfer theoretically should 13309 * be considered, but no HBA driver is checking it. 13310 */ 13311 if (IS_P2ALIGNED(bp->b_un.b_addr, 13312 cur_dma_attr->dma_attr_align)) { 13313 rval = ddi_dma_buf_bind_handle( 13314 spx->txlt_buf_dma_handle, 13315 bp, dma_flags, callback, arg, 13316 &spx->txlt_dma_cookie, 13317 &spx->txlt_curwin_num_dma_cookies); 13318 } else { /* Buffer is not aligned */ 13319 13320 int (*ddicallback)(caddr_t); 13321 size_t bufsz; 13322 13323 /* Check id sleeping is allowed */ 13324 ddicallback = (callback == NULL_FUNC) ? 13325 DDI_DMA_DONTWAIT : DDI_DMA_SLEEP; 13326 13327 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 13328 "mis-aligned buffer: addr=0x%p, cnt=%lu", 13329 (void *)bp->b_un.b_addr, bp->b_bcount); 13330 13331 if (bp->b_flags & (B_PAGEIO|B_PHYS)) 13332 /* 13333 * CPU will need to access data in the buffer 13334 * (for copying) so map it. 13335 */ 13336 bp_mapin(bp); 13337 13338 ASSERT(spx->txlt_tmp_buf == NULL); 13339 13340 /* Buffer may be padded by ddi_dma_mem_alloc()! */ 13341 rval = ddi_dma_mem_alloc( 13342 spx->txlt_buf_dma_handle, 13343 bp->b_bcount, 13344 &sata_acc_attr, 13345 DDI_DMA_STREAMING, 13346 ddicallback, NULL, 13347 &spx->txlt_tmp_buf, 13348 &bufsz, 13349 &spx->txlt_tmp_buf_handle); 13350 13351 if (rval != DDI_SUCCESS) { 13352 /* DMA mapping failed */ 13353 (void) ddi_dma_free_handle( 13354 &spx->txlt_buf_dma_handle); 13355 spx->txlt_buf_dma_handle = NULL; 13356 #ifdef SATA_DEBUG 13357 mbuffail_count++; 13358 #endif 13359 SATADBG1(SATA_DBG_DMA_SETUP, 13360 spx->txlt_sata_hba_inst, 13361 "sata_dma_buf_setup: " 13362 "buf dma mem alloc failed %x\n", rval); 13363 return (rval); 13364 } 13365 ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf, 13366 cur_dma_attr->dma_attr_align)); 13367 13368 #ifdef SATA_DEBUG 13369 mbuf_count++; 13370 13371 if (bp->b_bcount != bufsz) 13372 /* 13373 * This will require special handling, because 13374 * DMA cookies will be based on the temporary 13375 * buffer size, not the original buffer 13376 * b_bcount, so the residue may have to 13377 * be counted differently. 13378 */ 13379 SATADBG2(SATA_DBG_DMA_SETUP, 13380 spx->txlt_sata_hba_inst, 13381 "sata_dma_buf_setup: bp size %x != " 13382 "bufsz %x\n", bp->b_bcount, bufsz); 13383 #endif 13384 if (dma_flags & DDI_DMA_WRITE) { 13385 /* 13386 * Write operation - copy data into 13387 * an aligned temporary buffer. Buffer will be 13388 * synced for device by ddi_dma_addr_bind_handle 13389 */ 13390 bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf, 13391 bp->b_bcount); 13392 } 13393 13394 rval = ddi_dma_addr_bind_handle( 13395 spx->txlt_buf_dma_handle, 13396 NULL, 13397 spx->txlt_tmp_buf, 13398 bufsz, dma_flags, ddicallback, 0, 13399 &spx->txlt_dma_cookie, 13400 &spx->txlt_curwin_num_dma_cookies); 13401 } 13402 13403 switch (rval) { 13404 case DDI_DMA_PARTIAL_MAP: 13405 SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 13406 "sata_dma_buf_setup: DMA Partial Map\n", NULL); 13407 /* 13408 * Partial DMA mapping. 13409 * Retrieve number of DMA windows for this request. 13410 */ 13411 if (ddi_dma_numwin(spx->txlt_buf_dma_handle, 13412 &spx->txlt_num_dma_win) != DDI_SUCCESS) { 13413 if (spx->txlt_tmp_buf != NULL) { 13414 ddi_dma_mem_free( 13415 &spx->txlt_tmp_buf_handle); 13416 spx->txlt_tmp_buf = NULL; 13417 } 13418 (void) ddi_dma_unbind_handle( 13419 spx->txlt_buf_dma_handle); 13420 (void) ddi_dma_free_handle( 13421 &spx->txlt_buf_dma_handle); 13422 spx->txlt_buf_dma_handle = NULL; 13423 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 13424 "sata_dma_buf_setup: numwin failed\n")); 13425 return (DDI_FAILURE); 13426 } 13427 SATADBG2(SATA_DBG_DMA_SETUP, 13428 spx->txlt_sata_hba_inst, 13429 "sata_dma_buf_setup: windows: %d, cookies: %d\n", 13430 spx->txlt_num_dma_win, 13431 spx->txlt_curwin_num_dma_cookies); 13432 spx->txlt_cur_dma_win = 0; 13433 break; 13434 13435 case DDI_DMA_MAPPED: 13436 /* DMA fully mapped */ 13437 spx->txlt_num_dma_win = 1; 13438 spx->txlt_cur_dma_win = 0; 13439 SATADBG1(SATA_DBG_DMA_SETUP, 13440 spx->txlt_sata_hba_inst, 13441 "sata_dma_buf_setup: windows: 1 " 13442 "cookies: %d\n", spx->txlt_curwin_num_dma_cookies); 13443 break; 13444 13445 default: 13446 /* DMA mapping failed */ 13447 if (spx->txlt_tmp_buf != NULL) { 13448 ddi_dma_mem_free( 13449 &spx->txlt_tmp_buf_handle); 13450 spx->txlt_tmp_buf = NULL; 13451 } 13452 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 13453 spx->txlt_buf_dma_handle = NULL; 13454 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 13455 "sata_dma_buf_setup: buf dma handle binding " 13456 "failed %x\n", rval)); 13457 return (rval); 13458 } 13459 spx->txlt_curwin_processed_dma_cookies = 0; 13460 spx->txlt_dma_cookie_list = NULL; 13461 } else { 13462 /* 13463 * DMA setup is reused. Check if we need to process more 13464 * cookies in current window, or to get next window, if any. 13465 */ 13466 13467 ASSERT(spx->txlt_curwin_processed_dma_cookies <= 13468 spx->txlt_curwin_num_dma_cookies); 13469 13470 if (spx->txlt_curwin_processed_dma_cookies == 13471 spx->txlt_curwin_num_dma_cookies) { 13472 /* 13473 * All cookies from current DMA window were processed. 13474 * Get next DMA window. 13475 */ 13476 spx->txlt_cur_dma_win++; 13477 if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) { 13478 (void) ddi_dma_getwin(spx->txlt_buf_dma_handle, 13479 spx->txlt_cur_dma_win, &offset, &size, 13480 &spx->txlt_dma_cookie, 13481 &spx->txlt_curwin_num_dma_cookies); 13482 spx->txlt_curwin_processed_dma_cookies = 0; 13483 } else { 13484 /* No more windows! End of request! */ 13485 /* What to do? - panic for now */ 13486 ASSERT(spx->txlt_cur_dma_win >= 13487 spx->txlt_num_dma_win); 13488 13489 spx->txlt_curwin_num_dma_cookies = 0; 13490 spx->txlt_curwin_processed_dma_cookies = 0; 13491 spx->txlt_sata_pkt-> 13492 satapkt_cmd.satacmd_num_dma_cookies = 0; 13493 return (DDI_SUCCESS); 13494 } 13495 } 13496 } 13497 /* There better be at least one DMA cookie outstanding */ 13498 ASSERT((spx->txlt_curwin_num_dma_cookies - 13499 spx->txlt_curwin_processed_dma_cookies) > 0); 13500 13501 if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) { 13502 /* The default cookie slot was used in previous run */ 13503 ASSERT(spx->txlt_curwin_processed_dma_cookies == 0); 13504 spx->txlt_dma_cookie_list = NULL; 13505 spx->txlt_dma_cookie_list_len = 0; 13506 } 13507 if (spx->txlt_curwin_processed_dma_cookies == 0) { 13508 /* 13509 * Processing a new DMA window - set-up dma cookies list. 13510 * We may reuse previously allocated cookie array if it is 13511 * possible. 13512 */ 13513 if (spx->txlt_dma_cookie_list != NULL && 13514 spx->txlt_dma_cookie_list_len < 13515 spx->txlt_curwin_num_dma_cookies) { 13516 /* 13517 * New DMA window contains more cookies than 13518 * the previous one. We need larger cookie list - free 13519 * the old one. 13520 */ 13521 (void) kmem_free(spx->txlt_dma_cookie_list, 13522 spx->txlt_dma_cookie_list_len * 13523 sizeof (ddi_dma_cookie_t)); 13524 spx->txlt_dma_cookie_list = NULL; 13525 spx->txlt_dma_cookie_list_len = 0; 13526 } 13527 if (spx->txlt_dma_cookie_list == NULL) { 13528 /* 13529 * Calculate lesser of number of cookies in this 13530 * DMA window and number of s/g entries. 13531 */ 13532 max_sg_len = cur_dma_attr->dma_attr_sgllen; 13533 req_len = MIN(max_sg_len, 13534 spx->txlt_curwin_num_dma_cookies); 13535 13536 /* Allocate new dma cookie array if necessary */ 13537 if (req_len == 1) { 13538 /* Only one cookie - no need for a list */ 13539 spx->txlt_dma_cookie_list = 13540 &spx->txlt_dma_cookie; 13541 spx->txlt_dma_cookie_list_len = 1; 13542 } else { 13543 /* 13544 * More than one cookie - try to allocate space. 13545 */ 13546 spx->txlt_dma_cookie_list = kmem_zalloc( 13547 sizeof (ddi_dma_cookie_t) * req_len, 13548 callback == NULL_FUNC ? KM_NOSLEEP : 13549 KM_SLEEP); 13550 if (spx->txlt_dma_cookie_list == NULL) { 13551 SATADBG1(SATA_DBG_DMA_SETUP, 13552 spx->txlt_sata_hba_inst, 13553 "sata_dma_buf_setup: cookie list " 13554 "allocation failed\n", NULL); 13555 /* 13556 * We could not allocate space for 13557 * neccessary number of dma cookies in 13558 * this window, so we fail this request. 13559 * Next invocation would try again to 13560 * allocate space for cookie list. 13561 * Note:Packet residue was not modified. 13562 */ 13563 return (DDI_DMA_NORESOURCES); 13564 } else { 13565 spx->txlt_dma_cookie_list_len = req_len; 13566 } 13567 } 13568 } 13569 /* 13570 * Fetch DMA cookies into cookie list in sata_pkt_txlate. 13571 * First cookie was already fetched. 13572 */ 13573 *(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie; 13574 cur_txfer_len = 13575 (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size; 13576 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1; 13577 spx->txlt_curwin_processed_dma_cookies++; 13578 for (i = 1; (i < spx->txlt_dma_cookie_list_len) && 13579 (i < spx->txlt_curwin_num_dma_cookies); i++) { 13580 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 13581 &spx->txlt_dma_cookie_list[i]); 13582 cur_txfer_len += 13583 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 13584 spx->txlt_curwin_processed_dma_cookies++; 13585 spx->txlt_sata_pkt-> 13586 satapkt_cmd.satacmd_num_dma_cookies += 1; 13587 } 13588 } else { 13589 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 13590 "sata_dma_buf_setup: sliding within DMA window, " 13591 "cur cookie %d, total cookies %d\n", 13592 spx->txlt_curwin_processed_dma_cookies, 13593 spx->txlt_curwin_num_dma_cookies); 13594 13595 /* 13596 * Not all cookies from the current dma window were used because 13597 * of s/g limitation. 13598 * There is no need to re-size the list - it was set at 13599 * optimal size, or only default entry is used (s/g = 1). 13600 */ 13601 if (spx->txlt_dma_cookie_list == NULL) { 13602 spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie; 13603 spx->txlt_dma_cookie_list_len = 1; 13604 } 13605 /* 13606 * Since we are processing remaining cookies in a DMA window, 13607 * there may be less of them than the number of entries in the 13608 * current dma cookie list. 13609 */ 13610 req_len = MIN(spx->txlt_dma_cookie_list_len, 13611 (spx->txlt_curwin_num_dma_cookies - 13612 spx->txlt_curwin_processed_dma_cookies)); 13613 13614 /* Fetch the next batch of cookies */ 13615 for (i = 0, cur_txfer_len = 0; i < req_len; i++) { 13616 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 13617 &spx->txlt_dma_cookie_list[i]); 13618 cur_txfer_len += 13619 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 13620 spx->txlt_sata_pkt-> 13621 satapkt_cmd.satacmd_num_dma_cookies++; 13622 spx->txlt_curwin_processed_dma_cookies++; 13623 } 13624 } 13625 13626 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0); 13627 13628 /* Point sata_cmd to the cookie list */ 13629 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = 13630 &spx->txlt_dma_cookie_list[0]; 13631 13632 /* Remember number of DMA cookies passed in sata packet */ 13633 spx->txlt_num_dma_cookies = 13634 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies; 13635 13636 ASSERT(cur_txfer_len != 0); 13637 if (cur_txfer_len <= bp->b_bcount) 13638 spx->txlt_total_residue -= cur_txfer_len; 13639 else { 13640 /* 13641 * Temporary DMA buffer has been padded by 13642 * ddi_dma_mem_alloc()! 13643 * This requires special handling, because DMA cookies are 13644 * based on the temporary buffer size, not the b_bcount, 13645 * and we have extra bytes to transfer - but the packet 13646 * residue has to stay correct because we will copy only 13647 * the requested number of bytes. 13648 */ 13649 spx->txlt_total_residue -= bp->b_bcount; 13650 } 13651 13652 return (DDI_SUCCESS); 13653 } 13654 13655 /* 13656 * Common routine for releasing DMA resources 13657 */ 13658 static void 13659 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx) 13660 { 13661 if (spx->txlt_buf_dma_handle != NULL) { 13662 if (spx->txlt_tmp_buf != NULL) { 13663 /* 13664 * Intermediate DMA buffer was allocated. 13665 * Free allocated buffer and associated access handle. 13666 */ 13667 ddi_dma_mem_free(&spx->txlt_tmp_buf_handle); 13668 spx->txlt_tmp_buf = NULL; 13669 } 13670 /* 13671 * Free DMA resources - cookies and handles 13672 */ 13673 /* ASSERT(spx->txlt_dma_cookie_list != NULL); */ 13674 if (spx->txlt_dma_cookie_list != NULL) { 13675 if (spx->txlt_dma_cookie_list != 13676 &spx->txlt_dma_cookie) { 13677 (void) kmem_free(spx->txlt_dma_cookie_list, 13678 spx->txlt_dma_cookie_list_len * 13679 sizeof (ddi_dma_cookie_t)); 13680 spx->txlt_dma_cookie_list = NULL; 13681 } 13682 } 13683 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle); 13684 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 13685 spx->txlt_buf_dma_handle = NULL; 13686 } 13687 } 13688 13689 /* 13690 * Free DMA resources 13691 * Used by the HBA driver to release DMA resources that it does not use. 13692 * 13693 * Returns Void 13694 */ 13695 void 13696 sata_free_dma_resources(sata_pkt_t *sata_pkt) 13697 { 13698 sata_pkt_txlate_t *spx; 13699 13700 if (sata_pkt == NULL) 13701 return; 13702 13703 spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 13704 13705 sata_common_free_dma_rsrcs(spx); 13706 } 13707 13708 /* 13709 * Fetch Device Identify data. 13710 * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type) 13711 * command to a device and get the device identify data. 13712 * The device_info structure has to be set to device type (for selecting proper 13713 * device identify command). 13714 * 13715 * Returns: 13716 * SATA_SUCCESS if cmd succeeded 13717 * SATA_RETRY if cmd was rejected and could be retried, 13718 * SATA_FAILURE if cmd failed and should not be retried (port error) 13719 * 13720 * Cannot be called in an interrupt context. 13721 */ 13722 13723 static int 13724 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst, 13725 sata_drive_info_t *sdinfo) 13726 { 13727 struct buf *bp; 13728 sata_pkt_t *spkt; 13729 sata_cmd_t *scmd; 13730 sata_pkt_txlate_t *spx; 13731 int rval; 13732 dev_info_t *dip = SATA_DIP(sata_hba_inst); 13733 13734 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13735 spx->txlt_sata_hba_inst = sata_hba_inst; 13736 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13737 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13738 if (spkt == NULL) { 13739 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13740 return (SATA_RETRY); /* may retry later */ 13741 } 13742 /* address is needed now */ 13743 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13744 13745 /* 13746 * Allocate buffer for Identify Data return data 13747 */ 13748 bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t)); 13749 if (bp == NULL) { 13750 sata_pkt_free(spx); 13751 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13752 SATA_LOG_D((sata_hba_inst, CE_WARN, 13753 "sata_fetch_device_identify_data: " 13754 "cannot allocate buffer for ID")); 13755 return (SATA_RETRY); /* may retry later */ 13756 } 13757 13758 /* Fill sata_pkt */ 13759 sdinfo->satadrv_state = SATA_STATE_PROBING; 13760 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13761 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13762 /* Synchronous mode, no callback */ 13763 spkt->satapkt_comp = NULL; 13764 /* Timeout 30s */ 13765 spkt->satapkt_time = sata_default_pkt_time; 13766 13767 scmd = &spkt->satapkt_cmd; 13768 scmd->satacmd_bp = bp; 13769 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 13770 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 13771 13772 /* Build Identify Device cmd in the sata_pkt */ 13773 scmd->satacmd_addr_type = 0; /* N/A */ 13774 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 13775 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 13776 scmd->satacmd_lba_mid_lsb = 0; /* N/A */ 13777 scmd->satacmd_lba_high_lsb = 0; /* N/A */ 13778 scmd->satacmd_features_reg = 0; /* N/A */ 13779 scmd->satacmd_device_reg = 0; /* Always device 0 */ 13780 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 13781 /* Identify Packet Device cmd */ 13782 scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE; 13783 } else { 13784 /* Identify Device cmd - mandatory for all other devices */ 13785 scmd->satacmd_cmd_reg = SATAC_ID_DEVICE; 13786 } 13787 13788 /* Send pkt to SATA HBA driver */ 13789 rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt); 13790 13791 #ifdef SATA_INJECT_FAULTS 13792 sata_inject_pkt_fault(spkt, &rval, sata_fault_type); 13793 #endif 13794 13795 if (rval == SATA_TRAN_ACCEPTED && 13796 spkt->satapkt_reason == SATA_PKT_COMPLETED) { 13797 if (spx->txlt_buf_dma_handle != NULL) { 13798 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 13799 DDI_DMA_SYNC_FORKERNEL); 13800 ASSERT(rval == DDI_SUCCESS); 13801 if (sata_check_for_dma_error(dip, spx)) { 13802 ddi_fm_service_impact(dip, 13803 DDI_SERVICE_UNAFFECTED); 13804 rval = SATA_RETRY; 13805 goto fail; 13806 } 13807 13808 } 13809 if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config & 13810 SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) { 13811 SATA_LOG_D((sata_hba_inst, CE_WARN, 13812 "SATA disk device at port %d - " 13813 "partial Identify Data", 13814 sdinfo->satadrv_addr.cport)); 13815 rval = SATA_RETRY; /* may retry later */ 13816 goto fail; 13817 } 13818 /* Update sata_drive_info */ 13819 bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id, 13820 sizeof (sata_id_t)); 13821 13822 sdinfo->satadrv_features_support = 0; 13823 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 13824 /* 13825 * Retrieve capacity (disks only) and addressing mode 13826 */ 13827 sdinfo->satadrv_capacity = sata_check_capacity(sdinfo); 13828 } else { 13829 /* 13830 * For ATAPI devices one would have to issue 13831 * Get Capacity cmd for media capacity. Not here. 13832 */ 13833 sdinfo->satadrv_capacity = 0; 13834 /* 13835 * Check what cdb length is supported 13836 */ 13837 if ((sdinfo->satadrv_id.ai_config & 13838 SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B) 13839 sdinfo->satadrv_atapi_cdb_len = 16; 13840 else 13841 sdinfo->satadrv_atapi_cdb_len = 12; 13842 } 13843 /* Setup supported features flags */ 13844 if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) 13845 sdinfo->satadrv_features_support |= SATA_DEV_F_DMA; 13846 13847 /* Check for SATA GEN and NCQ support */ 13848 if (sdinfo->satadrv_id.ai_satacap != 0 && 13849 sdinfo->satadrv_id.ai_satacap != 0xffff) { 13850 /* SATA compliance */ 13851 if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ) 13852 sdinfo->satadrv_features_support |= 13853 SATA_DEV_F_NCQ; 13854 if (sdinfo->satadrv_id.ai_satacap & 13855 (SATA_1_SPEED | SATA_2_SPEED | SATA_3_SPEED)) { 13856 if (sdinfo->satadrv_id.ai_satacap & 13857 SATA_3_SPEED) 13858 sdinfo->satadrv_features_support |= 13859 SATA_DEV_F_SATA3; 13860 if (sdinfo->satadrv_id.ai_satacap & 13861 SATA_2_SPEED) 13862 sdinfo->satadrv_features_support |= 13863 SATA_DEV_F_SATA2; 13864 if (sdinfo->satadrv_id.ai_satacap & 13865 SATA_1_SPEED) 13866 sdinfo->satadrv_features_support |= 13867 SATA_DEV_F_SATA1; 13868 } else { 13869 sdinfo->satadrv_features_support |= 13870 SATA_DEV_F_SATA1; 13871 } 13872 } 13873 if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) && 13874 (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD)) 13875 sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ; 13876 13877 sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth; 13878 if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) || 13879 (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) { 13880 ++sdinfo->satadrv_queue_depth; 13881 /* Adjust according to controller capabilities */ 13882 sdinfo->satadrv_max_queue_depth = MIN( 13883 sdinfo->satadrv_queue_depth, 13884 SATA_QDEPTH(sata_hba_inst)); 13885 /* Adjust according to global queue depth limit */ 13886 sdinfo->satadrv_max_queue_depth = MIN( 13887 sdinfo->satadrv_max_queue_depth, 13888 sata_current_max_qdepth); 13889 if (sdinfo->satadrv_max_queue_depth == 0) 13890 sdinfo->satadrv_max_queue_depth = 1; 13891 } else 13892 sdinfo->satadrv_max_queue_depth = 1; 13893 13894 rval = SATA_SUCCESS; 13895 } else { 13896 /* 13897 * Woops, no Identify Data. 13898 */ 13899 if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) { 13900 rval = SATA_RETRY; /* may retry later */ 13901 } else if (rval == SATA_TRAN_ACCEPTED) { 13902 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR || 13903 spkt->satapkt_reason == SATA_PKT_ABORTED || 13904 spkt->satapkt_reason == SATA_PKT_TIMEOUT || 13905 spkt->satapkt_reason == SATA_PKT_RESET) 13906 rval = SATA_RETRY; /* may retry later */ 13907 else 13908 rval = SATA_FAILURE; 13909 } else { 13910 rval = SATA_FAILURE; 13911 } 13912 } 13913 fail: 13914 /* Free allocated resources */ 13915 sata_free_local_buffer(spx); 13916 sata_pkt_free(spx); 13917 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13918 13919 return (rval); 13920 } 13921 13922 13923 /* 13924 * Some devices may not come-up with default DMA mode (UDMA or MWDMA). 13925 * UDMA mode is checked first, followed by MWDMA mode. 13926 * set correctly, so this function is setting it to the highest supported level. 13927 * Older SATA spec required that the device supports at least DMA 4 mode and 13928 * UDMA mode is selected. It is not mentioned in SerialATA 2.6, so this 13929 * restriction has been removed. 13930 * 13931 * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported. 13932 * Returns SATA_FAILURE if proper DMA mode could not be selected. 13933 * 13934 * NOTE: This function should be called only if DMA mode is supported. 13935 */ 13936 static int 13937 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo) 13938 { 13939 sata_pkt_t *spkt; 13940 sata_cmd_t *scmd; 13941 sata_pkt_txlate_t *spx; 13942 int i, mode; 13943 uint8_t subcmd; 13944 int rval = SATA_SUCCESS; 13945 13946 ASSERT(sdinfo != NULL); 13947 ASSERT(sata_hba_inst != NULL); 13948 13949 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 && 13950 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) { 13951 /* Find highest Ultra DMA mode supported */ 13952 for (mode = 6; mode >= 0; --mode) { 13953 if (sdinfo->satadrv_id.ai_ultradma & (1 << mode)) 13954 break; 13955 } 13956 #if 0 13957 /* Left for historical reasons */ 13958 /* 13959 * Some initial version of SATA spec indicated that at least 13960 * UDMA mode 4 has to be supported. It is not mentioned in 13961 * SerialATA 2.6, so this restriction is removed. 13962 */ 13963 if (mode < 4) 13964 return (SATA_FAILURE); 13965 #endif 13966 13967 /* 13968 * For disk, we're still going to set DMA mode whatever is 13969 * selected by default 13970 * 13971 * We saw an old maxtor sata drive will select Ultra DMA and 13972 * Multi-Word DMA simultaneouly by default, which is going 13973 * to cause DMA command timed out, so we need to select DMA 13974 * mode even when it's already done by default 13975 */ 13976 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) { 13977 13978 /* Find UDMA mode currently selected */ 13979 for (i = 6; i >= 0; --i) { 13980 if (sdinfo->satadrv_id.ai_ultradma & 13981 (1 << (i + 8))) 13982 break; 13983 } 13984 if (i >= mode) 13985 /* Nothing to do */ 13986 return (SATA_SUCCESS); 13987 } 13988 13989 subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA; 13990 13991 } else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) { 13992 /* Find highest MultiWord DMA mode supported */ 13993 for (mode = 2; mode >= 0; --mode) { 13994 if (sdinfo->satadrv_id.ai_dworddma & (1 << mode)) 13995 break; 13996 } 13997 13998 /* 13999 * For disk, We're still going to set DMA mode whatever is 14000 * selected by default 14001 * 14002 * We saw an old maxtor sata drive will select Ultra DMA and 14003 * Multi-Word DMA simultaneouly by default, which is going 14004 * to cause DMA command timed out, so we need to select DMA 14005 * mode even when it's already done by default 14006 */ 14007 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) { 14008 14009 /* Find highest MultiWord DMA mode selected */ 14010 for (i = 2; i >= 0; --i) { 14011 if (sdinfo->satadrv_id.ai_dworddma & 14012 (1 << (i + 8))) 14013 break; 14014 } 14015 if (i >= mode) 14016 /* Nothing to do */ 14017 return (SATA_SUCCESS); 14018 } 14019 14020 subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA; 14021 } else 14022 return (SATA_SUCCESS); 14023 14024 /* 14025 * Set DMA mode via SET FEATURES COMMAND. 14026 * Prepare packet for SET FEATURES COMMAND. 14027 */ 14028 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 14029 spx->txlt_sata_hba_inst = sata_hba_inst; 14030 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 14031 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 14032 if (spkt == NULL) { 14033 SATA_LOG_D((sata_hba_inst, CE_WARN, 14034 "sata_set_dma_mode: could not set DMA mode %d", mode)); 14035 rval = SATA_FAILURE; 14036 goto done; 14037 } 14038 /* Fill sata_pkt */ 14039 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 14040 /* Timeout 30s */ 14041 spkt->satapkt_time = sata_default_pkt_time; 14042 /* Synchronous mode, no callback, interrupts */ 14043 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 14044 spkt->satapkt_comp = NULL; 14045 scmd = &spkt->satapkt_cmd; 14046 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 14047 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 14048 scmd->satacmd_addr_type = 0; 14049 scmd->satacmd_device_reg = 0; 14050 scmd->satacmd_status_reg = 0; 14051 scmd->satacmd_error_reg = 0; 14052 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 14053 scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE; 14054 scmd->satacmd_sec_count_lsb = subcmd | mode; 14055 14056 /* Transfer command to HBA */ 14057 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 14058 spkt) != SATA_TRAN_ACCEPTED || 14059 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 14060 /* Pkt execution failed */ 14061 rval = SATA_FAILURE; 14062 } 14063 done: 14064 14065 /* Free allocated resources */ 14066 if (spkt != NULL) 14067 sata_pkt_free(spx); 14068 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 14069 14070 return (rval); 14071 } 14072 14073 14074 /* 14075 * Set device caching mode. 14076 * One of the following operations should be specified: 14077 * SATAC_SF_ENABLE_READ_AHEAD 14078 * SATAC_SF_DISABLE_READ_AHEAD 14079 * SATAC_SF_ENABLE_WRITE_CACHE 14080 * SATAC_SF_DISABLE_WRITE_CACHE 14081 * 14082 * If operation fails, system log messgage is emitted. 14083 * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if 14084 * command was sent but did not succeed, and SATA_FAILURE otherwise. 14085 */ 14086 14087 static int 14088 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 14089 int cache_op) 14090 { 14091 sata_pkt_t *spkt; 14092 sata_cmd_t *scmd; 14093 sata_pkt_txlate_t *spx; 14094 int rval = SATA_SUCCESS; 14095 int hba_rval; 14096 char *infop; 14097 14098 ASSERT(sdinfo != NULL); 14099 ASSERT(sata_hba_inst != NULL); 14100 ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD || 14101 cache_op == SATAC_SF_DISABLE_READ_AHEAD || 14102 cache_op == SATAC_SF_ENABLE_WRITE_CACHE || 14103 cache_op == SATAC_SF_DISABLE_WRITE_CACHE); 14104 14105 14106 /* Prepare packet for SET FEATURES COMMAND */ 14107 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 14108 spx->txlt_sata_hba_inst = sata_hba_inst; 14109 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 14110 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 14111 if (spkt == NULL) { 14112 rval = SATA_FAILURE; 14113 goto failure; 14114 } 14115 /* Fill sata_pkt */ 14116 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 14117 /* Timeout 30s */ 14118 spkt->satapkt_time = sata_default_pkt_time; 14119 /* Synchronous mode, no callback, interrupts */ 14120 spkt->satapkt_op_mode = 14121 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 14122 spkt->satapkt_comp = NULL; 14123 scmd = &spkt->satapkt_cmd; 14124 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 14125 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 14126 scmd->satacmd_addr_type = 0; 14127 scmd->satacmd_device_reg = 0; 14128 scmd->satacmd_status_reg = 0; 14129 scmd->satacmd_error_reg = 0; 14130 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 14131 scmd->satacmd_features_reg = cache_op; 14132 14133 /* Transfer command to HBA */ 14134 hba_rval = (*SATA_START_FUNC(sata_hba_inst))( 14135 SATA_DIP(sata_hba_inst), spkt); 14136 14137 #ifdef SATA_INJECT_FAULTS 14138 sata_inject_pkt_fault(spkt, &rval, sata_fault_type); 14139 #endif 14140 14141 if ((hba_rval != SATA_TRAN_ACCEPTED) || 14142 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 14143 /* Pkt execution failed */ 14144 switch (cache_op) { 14145 case SATAC_SF_ENABLE_READ_AHEAD: 14146 infop = "enabling read ahead failed"; 14147 break; 14148 case SATAC_SF_DISABLE_READ_AHEAD: 14149 infop = "disabling read ahead failed"; 14150 break; 14151 case SATAC_SF_ENABLE_WRITE_CACHE: 14152 infop = "enabling write cache failed"; 14153 break; 14154 case SATAC_SF_DISABLE_WRITE_CACHE: 14155 infop = "disabling write cache failed"; 14156 break; 14157 } 14158 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 14159 rval = SATA_RETRY; 14160 } 14161 failure: 14162 /* Free allocated resources */ 14163 if (spkt != NULL) 14164 sata_pkt_free(spx); 14165 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 14166 return (rval); 14167 } 14168 14169 /* 14170 * Set Removable Media Status Notification (enable/disable) 14171 * state == 0 , disable 14172 * state != 0 , enable 14173 * 14174 * If operation fails, system log messgage is emitted. 14175 * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise. 14176 */ 14177 14178 static int 14179 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 14180 int state) 14181 { 14182 sata_pkt_t *spkt; 14183 sata_cmd_t *scmd; 14184 sata_pkt_txlate_t *spx; 14185 int rval = SATA_SUCCESS; 14186 char *infop; 14187 14188 ASSERT(sdinfo != NULL); 14189 ASSERT(sata_hba_inst != NULL); 14190 14191 /* Prepare packet for SET FEATURES COMMAND */ 14192 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 14193 spx->txlt_sata_hba_inst = sata_hba_inst; 14194 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 14195 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 14196 if (spkt == NULL) { 14197 rval = SATA_FAILURE; 14198 goto failure; 14199 } 14200 /* Fill sata_pkt */ 14201 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 14202 /* Timeout 30s */ 14203 spkt->satapkt_time = sata_default_pkt_time; 14204 /* Synchronous mode, no callback, interrupts */ 14205 spkt->satapkt_op_mode = 14206 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 14207 spkt->satapkt_comp = NULL; 14208 scmd = &spkt->satapkt_cmd; 14209 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 14210 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 14211 scmd->satacmd_addr_type = 0; 14212 scmd->satacmd_device_reg = 0; 14213 scmd->satacmd_status_reg = 0; 14214 scmd->satacmd_error_reg = 0; 14215 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 14216 if (state == 0) 14217 scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN; 14218 else 14219 scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN; 14220 14221 /* Transfer command to HBA */ 14222 if (((*SATA_START_FUNC(sata_hba_inst))( 14223 SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) || 14224 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 14225 /* Pkt execution failed */ 14226 if (state == 0) 14227 infop = "disabling Removable Media Status " 14228 "Notification failed"; 14229 else 14230 infop = "enabling Removable Media Status " 14231 "Notification failed"; 14232 14233 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 14234 rval = SATA_FAILURE; 14235 } 14236 failure: 14237 /* Free allocated resources */ 14238 if (spkt != NULL) 14239 sata_pkt_free(spx); 14240 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 14241 return (rval); 14242 } 14243 14244 14245 /* 14246 * Update state and copy port ss* values from passed sata_device structure. 14247 * sata_address is validated - if not valid, nothing is changed in sata_scsi 14248 * configuration struct. 14249 * 14250 * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function 14251 * regardless of the state in device argument. 14252 * 14253 * Port mutex should be held while calling this function. 14254 */ 14255 static void 14256 sata_update_port_info(sata_hba_inst_t *sata_hba_inst, 14257 sata_device_t *sata_device) 14258 { 14259 sata_cport_info_t *cportinfo; 14260 14261 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT || 14262 sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) { 14263 if (SATA_NUM_CPORTS(sata_hba_inst) <= 14264 sata_device->satadev_addr.cport) 14265 return; 14266 14267 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 14268 sata_device->satadev_addr.cport); 14269 14270 ASSERT(mutex_owned(&cportinfo->cport_mutex)); 14271 cportinfo->cport_scr = sata_device->satadev_scr; 14272 14273 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 14274 cportinfo->cport_state &= ~(SATA_PSTATE_PWRON | 14275 SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED); 14276 cportinfo->cport_state |= 14277 sata_device->satadev_state & SATA_PSTATE_VALID; 14278 } 14279 } 14280 14281 void 14282 sata_update_pmport_info(sata_hba_inst_t *sata_hba_inst, 14283 sata_device_t *sata_device) 14284 { 14285 sata_pmport_info_t *pmportinfo; 14286 14287 if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT && 14288 sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) || 14289 SATA_NUM_PMPORTS(sata_hba_inst, 14290 sata_device->satadev_addr.cport) < 14291 sata_device->satadev_addr.pmport) { 14292 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 14293 "sata_update_port_info: error address %p.", 14294 &sata_device->satadev_addr); 14295 return; 14296 } 14297 14298 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 14299 sata_device->satadev_addr.cport, 14300 sata_device->satadev_addr.pmport); 14301 14302 ASSERT(mutex_owned(&pmportinfo->pmport_mutex)); 14303 pmportinfo->pmport_scr = sata_device->satadev_scr; 14304 14305 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 14306 pmportinfo->pmport_state &= 14307 ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED); 14308 pmportinfo->pmport_state |= 14309 sata_device->satadev_state & SATA_PSTATE_VALID; 14310 } 14311 14312 /* 14313 * Extract SATA port specification from an IOCTL argument. 14314 * 14315 * This function return the port the user land send us as is, unless it 14316 * cannot retrieve port spec, then -1 is returned. 14317 * 14318 * Support port multiplier. 14319 */ 14320 static int32_t 14321 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp) 14322 { 14323 int32_t port; 14324 14325 /* Extract port number from nvpair in dca structure */ 14326 if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) { 14327 SATA_LOG_D((sata_hba_inst, CE_NOTE, 14328 "sata_get_port_num: invalid port spec 0x%x in ioctl", 14329 port)); 14330 port = -1; 14331 } 14332 14333 return (port); 14334 } 14335 14336 /* 14337 * Get dev_info_t pointer to the device node pointed to by port argument. 14338 * NOTE: target argument is a value used in ioctls to identify 14339 * the AP - it is not a sata_address. 14340 * It is a combination of cport, pmport and address qualifier, encodded same 14341 * way as a scsi target number. 14342 * At this moment it carries only cport number. 14343 * 14344 * PMult hotplug is supported now. 14345 * 14346 * Returns dev_info_t pointer if target device was found, NULL otherwise. 14347 */ 14348 14349 static dev_info_t * 14350 sata_get_target_dip(dev_info_t *dip, uint8_t cport, uint8_t pmport) 14351 { 14352 dev_info_t *cdip = NULL; 14353 int target, tgt; 14354 int circ; 14355 uint8_t qual; 14356 14357 sata_hba_inst_t *sata_hba_inst; 14358 scsi_hba_tran_t *scsi_hba_tran; 14359 14360 /* Get target id */ 14361 scsi_hba_tran = ddi_get_driver_private(dip); 14362 if (scsi_hba_tran == NULL) 14363 return (NULL); 14364 14365 sata_hba_inst = scsi_hba_tran->tran_hba_private; 14366 14367 if (sata_hba_inst == NULL) 14368 return (NULL); 14369 14370 /* Identify a port-mult by cport_info.cport_dev_type */ 14371 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) 14372 qual = SATA_ADDR_DPMPORT; 14373 else 14374 qual = SATA_ADDR_DCPORT; 14375 14376 target = SATA_TO_SCSI_TARGET(cport, pmport, qual); 14377 14378 /* Retrieve target dip */ 14379 ndi_devi_enter(dip, &circ); 14380 for (cdip = ddi_get_child(dip); cdip != NULL; ) { 14381 dev_info_t *next = ddi_get_next_sibling(cdip); 14382 14383 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip, 14384 DDI_PROP_DONTPASS, "target", -1); 14385 if (tgt == -1) { 14386 /* 14387 * This is actually an error condition, but not 14388 * a fatal one. Just continue the search. 14389 */ 14390 cdip = next; 14391 continue; 14392 } 14393 14394 if (tgt == target) 14395 break; 14396 14397 cdip = next; 14398 } 14399 ndi_devi_exit(dip, circ); 14400 14401 return (cdip); 14402 } 14403 14404 /* 14405 * Get dev_info_t pointer to the device node pointed to by port argument. 14406 * NOTE: target argument is a value used in ioctls to identify 14407 * the AP - it is not a sata_address. 14408 * It is a combination of cport, pmport and address qualifier, encoded same 14409 * way as a scsi target number. 14410 * 14411 * Returns dev_info_t pointer if target device was found, NULL otherwise. 14412 */ 14413 14414 static dev_info_t * 14415 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr) 14416 { 14417 dev_info_t *cdip = NULL; 14418 int target, tgt; 14419 int circ; 14420 14421 target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual); 14422 14423 ndi_devi_enter(dip, &circ); 14424 for (cdip = ddi_get_child(dip); cdip != NULL; ) { 14425 dev_info_t *next = ddi_get_next_sibling(cdip); 14426 14427 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip, 14428 DDI_PROP_DONTPASS, "target", -1); 14429 if (tgt == -1) { 14430 /* 14431 * This is actually an error condition, but not 14432 * a fatal one. Just continue the search. 14433 */ 14434 cdip = next; 14435 continue; 14436 } 14437 14438 if (tgt == target) 14439 break; 14440 14441 cdip = next; 14442 } 14443 ndi_devi_exit(dip, circ); 14444 14445 return (cdip); 14446 } 14447 14448 /* 14449 * Process sata port disconnect request. 14450 * Normally, cfgadm sata plugin will try to offline (unconfigure) the device 14451 * before this request. Nevertheless, if a device is still configured, 14452 * we need to attempt to offline and unconfigure device. 14453 * Regardless of the unconfigure operation results the port is marked as 14454 * deactivated and no access to the attached device is possible. 14455 * If the target node remains because unconfigure operation failed, its state 14456 * will be set to DEVICE_REMOVED, preventing it to be used again when a device 14457 * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure 14458 * the device and remove old target node. 14459 * 14460 * This function invokes sata_hba_inst->satahba_tran-> 14461 * sata_tran_hotplug_ops->sata_tran_port_deactivate(). 14462 * If successful, the device structure (if any) attached to the specified port 14463 * is removed and state of the port marked appropriately. 14464 * Failure of the port_deactivate may keep port in the physically active state, 14465 * or may fail the port. 14466 * 14467 * NOTE: Port multiplier is supported. 14468 */ 14469 14470 static int 14471 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst, 14472 sata_device_t *sata_device) 14473 { 14474 sata_drive_info_t *sdinfo = NULL, *subsdinfo = NULL; 14475 sata_cport_info_t *cportinfo = NULL; 14476 sata_pmport_info_t *pmportinfo = NULL; 14477 sata_pmult_info_t *pmultinfo = NULL; 14478 sata_device_t subsdevice; 14479 int cport, pmport, qual; 14480 int rval = SATA_SUCCESS; 14481 int npmport = 0; 14482 int rv = 0; 14483 14484 cport = sata_device->satadev_addr.cport; 14485 pmport = sata_device->satadev_addr.pmport; 14486 qual = sata_device->satadev_addr.qual; 14487 14488 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 14489 if (qual == SATA_ADDR_DCPORT) 14490 qual = SATA_ADDR_CPORT; 14491 else 14492 qual = SATA_ADDR_PMPORT; 14493 14494 /* 14495 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran-> 14496 * sata_tran_hotplug_ops->sata_tran_port_deactivate(). 14497 * Do the sanity check. 14498 */ 14499 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) { 14500 /* No physical port deactivation supported. */ 14501 return (EINVAL); 14502 } 14503 14504 /* Check the current state of the port */ 14505 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 14506 (SATA_DIP(sata_hba_inst), sata_device); 14507 14508 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 14509 14510 /* 14511 * Processing port mulitiplier 14512 */ 14513 if (qual == SATA_ADDR_CPORT && 14514 SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) { 14515 mutex_enter(&cportinfo->cport_mutex); 14516 14517 /* Check controller port status */ 14518 sata_update_port_info(sata_hba_inst, sata_device); 14519 if (rval != SATA_SUCCESS || 14520 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 14521 /* 14522 * Device port status is unknown or it is in failed 14523 * state 14524 */ 14525 SATA_CPORT_STATE(sata_hba_inst, cport) = 14526 SATA_PSTATE_FAILED; 14527 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 14528 "sata_hba_ioctl: connect: failed to deactivate " 14529 "SATA port %d", cport); 14530 mutex_exit(&cportinfo->cport_mutex); 14531 return (EIO); 14532 } 14533 14534 /* Disconnect all sub-devices. */ 14535 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 14536 if (pmultinfo != NULL) { 14537 14538 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 14539 sata_hba_inst, cport); npmport ++) { 14540 subsdinfo = SATA_PMPORT_DRV_INFO( 14541 sata_hba_inst, cport, npmport); 14542 if (subsdinfo == NULL) 14543 continue; 14544 14545 subsdevice.satadev_addr = subsdinfo-> 14546 satadrv_addr; 14547 14548 mutex_exit(&cportinfo->cport_mutex); 14549 if (sata_ioctl_disconnect(sata_hba_inst, 14550 &subsdevice) == SATA_SUCCESS) { 14551 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 14552 "[Remove] device at port %d:%d " 14553 "successfully.", cport, npmport); 14554 } 14555 mutex_enter(&cportinfo->cport_mutex); 14556 } 14557 } 14558 14559 /* Disconnect the port multiplier */ 14560 cportinfo->cport_state &= ~SATA_STATE_READY; 14561 mutex_exit(&cportinfo->cport_mutex); 14562 14563 sata_device->satadev_addr.qual = qual; 14564 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 14565 (SATA_DIP(sata_hba_inst), sata_device); 14566 14567 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 14568 SE_NO_HINT); 14569 14570 mutex_enter(&cportinfo->cport_mutex); 14571 sata_update_port_info(sata_hba_inst, sata_device); 14572 if (rval != SATA_SUCCESS && 14573 sata_device->satadev_state & SATA_PSTATE_FAILED) { 14574 cportinfo->cport_state = SATA_PSTATE_FAILED; 14575 rv = EIO; 14576 } else { 14577 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 14578 } 14579 mutex_exit(&cportinfo->cport_mutex); 14580 14581 return (rv); 14582 } 14583 14584 /* 14585 * Process non-port-multiplier device - it could be a drive connected 14586 * to a port multiplier port or a controller port. 14587 */ 14588 if (qual == SATA_ADDR_PMPORT) { 14589 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 14590 mutex_enter(&pmportinfo->pmport_mutex); 14591 sata_update_pmport_info(sata_hba_inst, sata_device); 14592 if (rval != SATA_SUCCESS || 14593 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 14594 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 14595 SATA_PSTATE_FAILED; 14596 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 14597 "sata_hba_ioctl: connect: failed to deactivate " 14598 "SATA port %d:%d", cport, pmport); 14599 mutex_exit(&pmportinfo->pmport_mutex); 14600 return (EIO); 14601 } 14602 14603 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 14604 sdinfo = pmportinfo->pmport_sata_drive; 14605 ASSERT(sdinfo != NULL); 14606 } 14607 14608 /* 14609 * Set port's dev_state to not ready - this will disable 14610 * an access to a potentially attached device. 14611 */ 14612 pmportinfo->pmport_state &= ~SATA_STATE_READY; 14613 14614 /* Remove and release sata_drive info structure. */ 14615 if (sdinfo != NULL) { 14616 if ((sdinfo->satadrv_type & 14617 SATA_VALID_DEV_TYPE) != 0) { 14618 /* 14619 * If a target node exists, try to offline 14620 * a device and remove target node. 14621 */ 14622 mutex_exit(&pmportinfo->pmport_mutex); 14623 (void) sata_offline_device(sata_hba_inst, 14624 sata_device, sdinfo); 14625 mutex_enter(&pmportinfo->pmport_mutex); 14626 } 14627 14628 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 14629 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 14630 (void) kmem_free((void *)sdinfo, 14631 sizeof (sata_drive_info_t)); 14632 } 14633 mutex_exit(&pmportinfo->pmport_mutex); 14634 14635 } else if (qual == SATA_ADDR_CPORT) { 14636 mutex_enter(&cportinfo->cport_mutex); 14637 sata_update_port_info(sata_hba_inst, sata_device); 14638 if (rval != SATA_SUCCESS || 14639 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 14640 /* 14641 * Device port status is unknown or it is in failed 14642 * state 14643 */ 14644 SATA_CPORT_STATE(sata_hba_inst, cport) = 14645 SATA_PSTATE_FAILED; 14646 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 14647 "sata_hba_ioctl: connect: failed to deactivate " 14648 "SATA port %d", cport); 14649 mutex_exit(&cportinfo->cport_mutex); 14650 return (EIO); 14651 } 14652 14653 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 14654 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 14655 ASSERT(pmultinfo != NULL); 14656 } else if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 14657 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 14658 ASSERT(sdinfo != NULL); 14659 } 14660 cportinfo->cport_state &= ~SATA_STATE_READY; 14661 14662 if (sdinfo != NULL) { 14663 if ((sdinfo->satadrv_type & 14664 SATA_VALID_DEV_TYPE) != 0) { 14665 /* 14666 * If a target node exists, try to offline 14667 * a device and remove target node. 14668 */ 14669 mutex_exit(&cportinfo->cport_mutex); 14670 (void) sata_offline_device(sata_hba_inst, 14671 sata_device, sdinfo); 14672 mutex_enter(&cportinfo->cport_mutex); 14673 } 14674 14675 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 14676 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 14677 (void) kmem_free((void *)sdinfo, 14678 sizeof (sata_drive_info_t)); 14679 } 14680 mutex_exit(&cportinfo->cport_mutex); 14681 } 14682 14683 /* Just ask HBA driver to deactivate port */ 14684 sata_device->satadev_addr.qual = qual; 14685 14686 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 14687 (SATA_DIP(sata_hba_inst), sata_device); 14688 14689 /* 14690 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 14691 * without the hint (to force listener to investivate the state). 14692 */ 14693 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 14694 SE_NO_HINT); 14695 14696 if (qual == SATA_ADDR_PMPORT) { 14697 mutex_enter(&pmportinfo->pmport_mutex); 14698 sata_update_pmport_info(sata_hba_inst, sata_device); 14699 14700 if (rval != SATA_SUCCESS && 14701 sata_device->satadev_state & SATA_PSTATE_FAILED) { 14702 /* 14703 * Port deactivation failure - do not change port 14704 * state unless the state returned by HBA indicates a 14705 * port failure. 14706 * 14707 * NOTE: device structures were released, so devices 14708 * now are invisible! Port reset is needed to 14709 * re-enumerate devices. 14710 */ 14711 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 14712 rv = EIO; 14713 } else { 14714 /* 14715 * Deactivation succeded. From now on the sata framework 14716 * will not care what is happening to the device, until 14717 * the port is activated again. 14718 */ 14719 pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN; 14720 } 14721 mutex_exit(&pmportinfo->pmport_mutex); 14722 } else if (qual == SATA_ADDR_CPORT) { 14723 mutex_enter(&cportinfo->cport_mutex); 14724 sata_update_port_info(sata_hba_inst, sata_device); 14725 14726 if (rval != SATA_SUCCESS && 14727 sata_device->satadev_state & SATA_PSTATE_FAILED) { 14728 cportinfo->cport_state = SATA_PSTATE_FAILED; 14729 rv = EIO; 14730 } else { 14731 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 14732 } 14733 mutex_exit(&cportinfo->cport_mutex); 14734 } 14735 14736 return (rv); 14737 } 14738 14739 14740 14741 /* 14742 * Process sata port connect request 14743 * The sata cfgadm pluging will invoke this operation only if port was found 14744 * in the disconnect state (failed state is also treated as the disconnected 14745 * state). 14746 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->satahba_tran-> 14747 * sata_tran_hotplug_ops->sata_tran_port_activate(). 14748 * If successful and a device is found attached to the port, 14749 * the initialization sequence is executed to attach a device structure to 14750 * a port structure. The state of the port and a device would be set 14751 * appropriately. 14752 * The device is not set in configured state (system-wise) by this operation. 14753 * 14754 * Note, that activating the port may generate link events, 14755 * so it is important that following processing and the 14756 * event processing does not interfere with each other! 14757 * 14758 * This operation may remove port failed state and will 14759 * try to make port active and in good standing. 14760 * 14761 * NOTE: Port multiplier is supported. 14762 */ 14763 14764 static int 14765 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst, 14766 sata_device_t *sata_device) 14767 { 14768 sata_pmport_info_t *pmportinfo = NULL; 14769 uint8_t cport, pmport, qual; 14770 int rv = 0; 14771 14772 cport = sata_device->satadev_addr.cport; 14773 pmport = sata_device->satadev_addr.pmport; 14774 qual = sata_device->satadev_addr.qual; 14775 14776 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 14777 if (qual == SATA_ADDR_DCPORT) 14778 qual = SATA_ADDR_CPORT; 14779 else 14780 qual = SATA_ADDR_PMPORT; 14781 14782 if (qual == SATA_ADDR_PMPORT) 14783 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 14784 14785 /* 14786 * DEVCTL_AP_CONNECT would invoke sata_hba_inst-> 14787 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate(). 14788 * Perform sanity check now. 14789 */ 14790 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) { 14791 /* No physical port activation supported. */ 14792 return (EINVAL); 14793 } 14794 14795 /* Just ask HBA driver to activate port */ 14796 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 14797 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 14798 /* 14799 * Port activation failure. 14800 */ 14801 if (qual == SATA_ADDR_CPORT) { 14802 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14803 cport)->cport_mutex); 14804 sata_update_port_info(sata_hba_inst, sata_device); 14805 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 14806 SATA_CPORT_STATE(sata_hba_inst, cport) = 14807 SATA_PSTATE_FAILED; 14808 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 14809 "sata_hba_ioctl: connect: failed to " 14810 "activate SATA port %d", cport); 14811 } 14812 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14813 cport)->cport_mutex); 14814 } else { /* port multiplier device port */ 14815 mutex_enter(&pmportinfo->pmport_mutex); 14816 sata_update_pmport_info(sata_hba_inst, sata_device); 14817 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 14818 SATA_PMPORT_STATE(sata_hba_inst, cport, 14819 pmport) = SATA_PSTATE_FAILED; 14820 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 14821 "sata_hba_ioctl: connect: failed to " 14822 "activate SATA port %d:%d", cport, pmport); 14823 } 14824 mutex_exit(&pmportinfo->pmport_mutex); 14825 } 14826 return (EIO); 14827 } 14828 14829 /* Virgin port state - will be updated by the port re-probe. */ 14830 if (qual == SATA_ADDR_CPORT) { 14831 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14832 cport)->cport_mutex); 14833 SATA_CPORT_STATE(sata_hba_inst, cport) = 0; 14834 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14835 cport)->cport_mutex); 14836 } else { /* port multiplier device port */ 14837 mutex_enter(&pmportinfo->pmport_mutex); 14838 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0; 14839 mutex_exit(&pmportinfo->pmport_mutex); 14840 } 14841 14842 /* 14843 * Probe the port to find its state and attached device. 14844 */ 14845 if (sata_reprobe_port(sata_hba_inst, sata_device, 14846 SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE) 14847 rv = EIO; 14848 14849 /* 14850 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 14851 * without the hint 14852 */ 14853 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 14854 SE_NO_HINT); 14855 14856 /* 14857 * If there is a device attached to the port, emit 14858 * a message. 14859 */ 14860 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 14861 14862 if (qual == SATA_ADDR_CPORT) { 14863 if (sata_device->satadev_type == SATA_DTYPE_PMULT) { 14864 sata_log(sata_hba_inst, CE_WARN, 14865 "SATA port multiplier detected " 14866 "at port %d", cport); 14867 } else { 14868 sata_log(sata_hba_inst, CE_WARN, 14869 "SATA device detected at port %d", cport); 14870 if (sata_device->satadev_type == 14871 SATA_DTYPE_UNKNOWN) { 14872 /* 14873 * A device was not successfully identified 14874 */ 14875 sata_log(sata_hba_inst, CE_WARN, 14876 "Could not identify SATA " 14877 "device at port %d", cport); 14878 } 14879 } 14880 } else { /* port multiplier device port */ 14881 sata_log(sata_hba_inst, CE_WARN, 14882 "SATA device detected at port %d:%d", 14883 cport, pmport); 14884 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 14885 /* 14886 * A device was not successfully identified 14887 */ 14888 sata_log(sata_hba_inst, CE_WARN, 14889 "Could not identify SATA " 14890 "device at port %d:%d", cport, pmport); 14891 } 14892 } 14893 } 14894 14895 return (rv); 14896 } 14897 14898 14899 /* 14900 * Process sata device unconfigure request. 14901 * The unconfigure operation uses generic nexus operation to 14902 * offline a device. It leaves a target device node attached. 14903 * and obviously sata_drive_info attached as well, because 14904 * from the hardware point of view nothing has changed. 14905 */ 14906 static int 14907 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst, 14908 sata_device_t *sata_device) 14909 { 14910 int rv = 0; 14911 dev_info_t *tdip; 14912 14913 /* We are addressing attached device, not a port */ 14914 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 14915 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 14916 else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) 14917 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 14918 14919 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 14920 &sata_device->satadev_addr)) != NULL) { 14921 14922 if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) { 14923 SATA_LOG_D((sata_hba_inst, CE_WARN, 14924 "sata_hba_ioctl: unconfigure: " 14925 "failed to unconfigure device at SATA port %d:%d", 14926 sata_device->satadev_addr.cport, 14927 sata_device->satadev_addr.pmport)); 14928 rv = EIO; 14929 } 14930 /* 14931 * The target node devi_state should be marked with 14932 * DEVI_DEVICE_OFFLINE by ndi_devi_offline(). 14933 * This would be the indication for cfgadm that 14934 * the AP node occupant state is 'unconfigured'. 14935 */ 14936 14937 } else { 14938 /* 14939 * This would indicate a failure on the part of cfgadm 14940 * to detect correct state of the node prior to this 14941 * call - one cannot unconfigure non-existing device. 14942 */ 14943 SATA_LOG_D((sata_hba_inst, CE_WARN, 14944 "sata_hba_ioctl: unconfigure: " 14945 "attempt to unconfigure non-existing device " 14946 "at SATA port %d:%d", 14947 sata_device->satadev_addr.cport, 14948 sata_device->satadev_addr.pmport)); 14949 rv = ENXIO; 14950 } 14951 return (rv); 14952 } 14953 14954 /* 14955 * Process sata device configure request 14956 * If port is in a failed state, operation is aborted - one has to use 14957 * an explicit connect or port activate request to try to get a port into 14958 * non-failed mode. Port reset wil also work in such situation. 14959 * If the port is in disconnected (shutdown) state, the connect operation is 14960 * attempted prior to any other action. 14961 * When port is in the active state, there is a device attached and the target 14962 * node exists, a device was most likely offlined. 14963 * If target node does not exist, a new target node is created. In both cases 14964 * an attempt is made to online (configure) the device. 14965 * 14966 * NOTE: Port multiplier is supported. 14967 */ 14968 static int 14969 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst, 14970 sata_device_t *sata_device) 14971 { 14972 int cport, pmport, qual; 14973 int rval; 14974 boolean_t target = B_TRUE; 14975 sata_cport_info_t *cportinfo; 14976 sata_pmport_info_t *pmportinfo = NULL; 14977 dev_info_t *tdip; 14978 sata_drive_info_t *sdinfo; 14979 14980 cport = sata_device->satadev_addr.cport; 14981 pmport = sata_device->satadev_addr.pmport; 14982 qual = sata_device->satadev_addr.qual; 14983 14984 /* Get current port state */ 14985 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 14986 (SATA_DIP(sata_hba_inst), sata_device); 14987 14988 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 14989 if (qual == SATA_ADDR_DPMPORT) { 14990 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 14991 mutex_enter(&pmportinfo->pmport_mutex); 14992 sata_update_pmport_info(sata_hba_inst, sata_device); 14993 if (rval != SATA_SUCCESS || 14994 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 14995 /* 14996 * Obviously, device on a failed port is not visible 14997 */ 14998 mutex_exit(&pmportinfo->pmport_mutex); 14999 return (ENXIO); 15000 } 15001 mutex_exit(&pmportinfo->pmport_mutex); 15002 } else { 15003 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15004 cport)->cport_mutex); 15005 sata_update_port_info(sata_hba_inst, sata_device); 15006 if (rval != SATA_SUCCESS || 15007 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 15008 /* 15009 * Obviously, device on a failed port is not visible 15010 */ 15011 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15012 cport)->cport_mutex); 15013 return (ENXIO); 15014 } 15015 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15016 cport)->cport_mutex); 15017 } 15018 15019 if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) { 15020 /* need to activate port */ 15021 target = B_FALSE; 15022 15023 /* Sanity check */ 15024 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) 15025 return (ENXIO); 15026 15027 /* Just let HBA driver to activate port */ 15028 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 15029 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 15030 /* 15031 * Port activation failure - do not change port state 15032 * unless the state returned by HBA indicates a port 15033 * failure. 15034 */ 15035 if (qual == SATA_ADDR_DPMPORT) { 15036 mutex_enter(&pmportinfo->pmport_mutex); 15037 sata_update_pmport_info(sata_hba_inst, 15038 sata_device); 15039 if (sata_device->satadev_state & 15040 SATA_PSTATE_FAILED) 15041 pmportinfo->pmport_state = 15042 SATA_PSTATE_FAILED; 15043 mutex_exit(&pmportinfo->pmport_mutex); 15044 } else { 15045 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15046 cport)->cport_mutex); 15047 sata_update_port_info(sata_hba_inst, 15048 sata_device); 15049 if (sata_device->satadev_state & 15050 SATA_PSTATE_FAILED) 15051 cportinfo->cport_state = 15052 SATA_PSTATE_FAILED; 15053 mutex_exit(&SATA_CPORT_INFO( 15054 sata_hba_inst, cport)->cport_mutex); 15055 } 15056 } 15057 SATA_LOG_D((sata_hba_inst, CE_WARN, 15058 "sata_hba_ioctl: configure: " 15059 "failed to activate SATA port %d:%d", 15060 cport, pmport)); 15061 return (EIO); 15062 } 15063 /* 15064 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 15065 * without the hint. 15066 */ 15067 sata_gen_sysevent(sata_hba_inst, 15068 &sata_device->satadev_addr, SE_NO_HINT); 15069 15070 /* Virgin port state */ 15071 if (qual == SATA_ADDR_DPMPORT) { 15072 mutex_enter(&pmportinfo->pmport_mutex); 15073 pmportinfo->pmport_state = 0; 15074 mutex_exit(&pmportinfo->pmport_mutex); 15075 } else { 15076 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15077 cport)-> cport_mutex); 15078 cportinfo->cport_state = 0; 15079 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15080 cport)->cport_mutex); 15081 } 15082 /* 15083 * Always reprobe port, to get current device info. 15084 */ 15085 if (sata_reprobe_port(sata_hba_inst, sata_device, 15086 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 15087 return (EIO); 15088 15089 if (sata_device->satadev_type != SATA_DTYPE_NONE && target == B_FALSE) { 15090 if (qual == SATA_ADDR_DPMPORT) { 15091 /* 15092 * That's the transition from "inactive" port 15093 * to active one with device attached. 15094 */ 15095 sata_log(sata_hba_inst, CE_WARN, 15096 "SATA device detected at port %d:%d", 15097 cport, pmport); 15098 } else { 15099 /* 15100 * When PM is attached to the cport and cport is 15101 * activated, every PM device port needs to be reprobed. 15102 * We need to emit message for all devices detected 15103 * at port multiplier's device ports. 15104 * Add such code here. 15105 * For now, just inform about device attached to 15106 * cport. 15107 */ 15108 sata_log(sata_hba_inst, CE_WARN, 15109 "SATA device detected at port %d", cport); 15110 } 15111 } 15112 15113 /* 15114 * This is where real configuration operation starts. 15115 * 15116 * When PM is attached to the cport and cport is activated, 15117 * devices attached PM device ports may have to be configured 15118 * explicitly. This may change when port multiplier is supported. 15119 * For now, configure only disks and other valid target devices. 15120 */ 15121 if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) { 15122 if (qual == SATA_ADDR_DCPORT) { 15123 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 15124 /* 15125 * A device was not successfully identified 15126 */ 15127 sata_log(sata_hba_inst, CE_WARN, 15128 "Could not identify SATA " 15129 "device at port %d", cport); 15130 } 15131 } else { /* port multiplier device port */ 15132 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 15133 /* 15134 * A device was not successfully identified 15135 */ 15136 sata_log(sata_hba_inst, CE_WARN, 15137 "Could not identify SATA " 15138 "device at port %d:%d", cport, pmport); 15139 } 15140 } 15141 return (ENXIO); /* No device to configure */ 15142 } 15143 15144 /* 15145 * Here we may have a device in reset condition, 15146 * but because we are just configuring it, there is 15147 * no need to process the reset other than just 15148 * to clear device reset condition in the HBA driver. 15149 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will 15150 * cause a first command sent the HBA driver with the request 15151 * to clear device reset condition. 15152 */ 15153 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15154 if (qual == SATA_ADDR_DPMPORT) 15155 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 15156 else 15157 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 15158 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 15159 if (sdinfo == NULL) { 15160 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15161 return (ENXIO); 15162 } 15163 if (sdinfo->satadrv_event_flags & 15164 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 15165 sdinfo->satadrv_event_flags = 0; 15166 } 15167 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 15168 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15169 15170 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 15171 &sata_device->satadev_addr)) != NULL) { 15172 /* 15173 * Target node exists. Verify, that it belongs 15174 * to existing, attached device and not to 15175 * a removed device. 15176 */ 15177 if (sata_check_device_removed(tdip) == B_TRUE) { 15178 if (qual == SATA_ADDR_DPMPORT) 15179 sata_log(sata_hba_inst, CE_WARN, 15180 "SATA device at port %d cannot be " 15181 "configured. " 15182 "Application(s) accessing " 15183 "previously attached device " 15184 "have to release it before newly " 15185 "inserted device can be made accessible.", 15186 cport); 15187 else 15188 sata_log(sata_hba_inst, CE_WARN, 15189 "SATA device at port %d:%d cannot be" 15190 "configured. " 15191 "Application(s) accessing " 15192 "previously attached device " 15193 "have to release it before newly " 15194 "inserted device can be made accessible.", 15195 cport, pmport); 15196 return (EIO); 15197 } 15198 /* 15199 * Device was not removed and re-inserted. 15200 * Try to online it. 15201 */ 15202 if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) { 15203 SATA_LOG_D((sata_hba_inst, CE_WARN, 15204 "sata_hba_ioctl: configure: " 15205 "onlining device at SATA port " 15206 "%d:%d failed", cport, pmport)); 15207 return (EIO); 15208 } 15209 15210 if (qual == SATA_ADDR_DPMPORT) { 15211 mutex_enter(&pmportinfo->pmport_mutex); 15212 pmportinfo->pmport_tgtnode_clean = B_TRUE; 15213 mutex_exit(&pmportinfo->pmport_mutex); 15214 } else { 15215 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15216 cport)->cport_mutex); 15217 cportinfo-> cport_tgtnode_clean = B_TRUE; 15218 mutex_exit(&SATA_CPORT_INFO( 15219 sata_hba_inst, cport)->cport_mutex); 15220 } 15221 } else { 15222 /* 15223 * No target node - need to create a new target node. 15224 */ 15225 if (qual == SATA_ADDR_DPMPORT) { 15226 mutex_enter(&pmportinfo->pmport_mutex); 15227 pmportinfo->pmport_tgtnode_clean = B_TRUE; 15228 mutex_exit(&pmportinfo->pmport_mutex); 15229 } else { 15230 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15231 cport_mutex); 15232 cportinfo-> cport_tgtnode_clean = B_TRUE; 15233 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15234 cport_mutex); 15235 } 15236 15237 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst), 15238 sata_hba_inst, &sata_device->satadev_addr); 15239 if (tdip == NULL) { 15240 /* Configure operation failed */ 15241 SATA_LOG_D((sata_hba_inst, CE_WARN, 15242 "sata_hba_ioctl: configure: " 15243 "configuring SATA device at port %d:%d " 15244 "failed", cport, pmport)); 15245 return (EIO); 15246 } 15247 } 15248 return (0); 15249 } 15250 15251 15252 /* 15253 * Process ioctl deactivate port request. 15254 * Arbitrarily unconfigure attached device, if any. 15255 * Even if the unconfigure fails, proceed with the 15256 * port deactivation. 15257 * 15258 * NOTE: Port Multiplier is supported now. 15259 */ 15260 15261 static int 15262 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst, 15263 sata_device_t *sata_device) 15264 { 15265 int cport, pmport, qual; 15266 int rval, rv = 0; 15267 int npmport; 15268 sata_cport_info_t *cportinfo; 15269 sata_pmport_info_t *pmportinfo; 15270 sata_pmult_info_t *pmultinfo; 15271 dev_info_t *tdip; 15272 sata_drive_info_t *sdinfo = NULL; 15273 sata_device_t subsdevice; 15274 15275 /* Sanity check */ 15276 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) 15277 return (ENOTSUP); 15278 15279 cport = sata_device->satadev_addr.cport; 15280 pmport = sata_device->satadev_addr.pmport; 15281 qual = sata_device->satadev_addr.qual; 15282 15283 /* SCSI_TO_SATA_ADDR_QUAL() translate ap_id into a device qualifier */ 15284 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 15285 if (qual == SATA_ADDR_DCPORT) 15286 qual = SATA_ADDR_CPORT; 15287 else 15288 qual = SATA_ADDR_PMPORT; 15289 15290 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 15291 if (qual == SATA_ADDR_PMPORT) 15292 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 15293 15294 /* 15295 * Processing port multiplier 15296 */ 15297 if (qual == SATA_ADDR_CPORT && 15298 SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) { 15299 mutex_enter(&cportinfo->cport_mutex); 15300 15301 /* Deactivate all sub-deices */ 15302 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 15303 if (pmultinfo != NULL) { 15304 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 15305 sata_hba_inst, cport); npmport++) { 15306 15307 subsdevice.satadev_addr.cport = cport; 15308 subsdevice.satadev_addr.pmport = 15309 (uint8_t)npmport; 15310 subsdevice.satadev_addr.qual = 15311 SATA_ADDR_DPMPORT; 15312 15313 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 15314 "sata_hba_ioctl: deactivate: trying to " 15315 "deactivate SATA port %d:%d", 15316 cport, npmport); 15317 15318 mutex_exit(&cportinfo->cport_mutex); 15319 if (sata_ioctl_deactivate(sata_hba_inst, 15320 &subsdevice) == SATA_SUCCESS) { 15321 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 15322 "[Deactivate] device at port %d:%d " 15323 "successfully.", cport, npmport); 15324 } 15325 mutex_enter(&cportinfo->cport_mutex); 15326 } 15327 } 15328 15329 /* Deactivate the port multiplier now. */ 15330 cportinfo->cport_state &= ~SATA_STATE_READY; 15331 mutex_exit(&cportinfo->cport_mutex); 15332 15333 sata_device->satadev_addr.qual = qual; 15334 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 15335 (SATA_DIP(sata_hba_inst), sata_device); 15336 15337 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 15338 SE_NO_HINT); 15339 15340 mutex_enter(&cportinfo->cport_mutex); 15341 sata_update_port_info(sata_hba_inst, sata_device); 15342 if (rval != SATA_SUCCESS) { 15343 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 15344 cportinfo->cport_state = SATA_PSTATE_FAILED; 15345 } 15346 rv = EIO; 15347 } else { 15348 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 15349 } 15350 mutex_exit(&cportinfo->cport_mutex); 15351 15352 return (rv); 15353 } 15354 15355 /* 15356 * Process non-port-multiplier device - it could be a drive connected 15357 * to a port multiplier port or a controller port. 15358 */ 15359 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15360 if (qual == SATA_ADDR_CPORT) { 15361 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 15362 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 15363 /* deal only with valid devices */ 15364 if ((cportinfo->cport_dev_type & 15365 SATA_VALID_DEV_TYPE) != 0) 15366 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 15367 } 15368 cportinfo->cport_state &= ~SATA_STATE_READY; 15369 } else { 15370 /* Port multiplier device port */ 15371 mutex_enter(&pmportinfo->pmport_mutex); 15372 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 15373 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE && 15374 (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0) 15375 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 15376 pmportinfo->pmport_state &= ~SATA_STATE_READY; 15377 mutex_exit(&pmportinfo->pmport_mutex); 15378 } 15379 15380 if (sdinfo != NULL) { 15381 /* 15382 * If a target node exists, try to offline a device and 15383 * to remove a target node. 15384 */ 15385 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15386 cport_mutex); 15387 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 15388 &sata_device->satadev_addr); 15389 if (tdip != NULL) { 15390 /* target node exist */ 15391 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 15392 "sata_hba_ioctl: port deactivate: " 15393 "target node exists.", NULL); 15394 15395 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != 15396 NDI_SUCCESS) { 15397 SATA_LOG_D((sata_hba_inst, CE_WARN, 15398 "sata_hba_ioctl: port deactivate: " 15399 "failed to unconfigure device at port " 15400 "%d:%d before deactivating the port", 15401 cport, pmport)); 15402 /* 15403 * Set DEVICE REMOVED state in the target 15404 * node. It will prevent an access to 15405 * the device even when a new device is 15406 * attached, until the old target node is 15407 * released, removed and recreated for a new 15408 * device. 15409 */ 15410 sata_set_device_removed(tdip); 15411 15412 /* 15413 * Instruct the event daemon to try the 15414 * target node cleanup later. 15415 */ 15416 sata_set_target_node_cleanup(sata_hba_inst, 15417 &sata_device->satadev_addr); 15418 } 15419 } 15420 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15421 cport_mutex); 15422 /* 15423 * In any case, remove and release sata_drive_info 15424 * structure. 15425 */ 15426 if (qual == SATA_ADDR_CPORT) { 15427 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 15428 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 15429 } else { /* port multiplier device port */ 15430 mutex_enter(&pmportinfo->pmport_mutex); 15431 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 15432 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 15433 mutex_exit(&pmportinfo->pmport_mutex); 15434 } 15435 (void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t)); 15436 } 15437 15438 if (qual == SATA_ADDR_CPORT) { 15439 cportinfo->cport_state &= ~(SATA_STATE_PROBED | 15440 SATA_STATE_PROBING); 15441 } else if (qual == SATA_ADDR_PMPORT) { 15442 mutex_enter(&pmportinfo->pmport_mutex); 15443 pmportinfo->pmport_state &= ~(SATA_STATE_PROBED | 15444 SATA_STATE_PROBING); 15445 mutex_exit(&pmportinfo->pmport_mutex); 15446 } 15447 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15448 15449 /* Just let HBA driver to deactivate port */ 15450 sata_device->satadev_addr.qual = qual; 15451 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 15452 (SATA_DIP(sata_hba_inst), sata_device); 15453 15454 /* 15455 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 15456 * without the hint 15457 */ 15458 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 15459 SE_NO_HINT); 15460 15461 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15462 sata_update_port_info(sata_hba_inst, sata_device); 15463 if (qual == SATA_ADDR_CPORT) { 15464 if (rval != SATA_SUCCESS) { 15465 /* 15466 * Port deactivation failure - do not change port state 15467 * unless the state returned by HBA indicates a port 15468 * failure. 15469 */ 15470 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 15471 SATA_CPORT_STATE(sata_hba_inst, cport) = 15472 SATA_PSTATE_FAILED; 15473 } 15474 SATA_LOG_D((sata_hba_inst, CE_WARN, 15475 "sata_hba_ioctl: port deactivate: " 15476 "cannot deactivate SATA port %d", cport)); 15477 rv = EIO; 15478 } else { 15479 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 15480 } 15481 } else { 15482 mutex_enter(&pmportinfo->pmport_mutex); 15483 if (rval != SATA_SUCCESS) { 15484 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 15485 SATA_PMPORT_STATE(sata_hba_inst, cport, 15486 pmport) = SATA_PSTATE_FAILED; 15487 } 15488 SATA_LOG_D((sata_hba_inst, CE_WARN, 15489 "sata_hba_ioctl: port deactivate: " 15490 "cannot deactivate SATA port %d:%d", 15491 cport, pmport)); 15492 rv = EIO; 15493 } else { 15494 pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN; 15495 } 15496 mutex_exit(&pmportinfo->pmport_mutex); 15497 } 15498 15499 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15500 15501 return (rv); 15502 } 15503 15504 /* 15505 * Process ioctl port activate request. 15506 * 15507 * NOTE: Port multiplier is supported now. 15508 */ 15509 static int 15510 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst, 15511 sata_device_t *sata_device) 15512 { 15513 int cport, pmport, qual; 15514 sata_cport_info_t *cportinfo; 15515 sata_pmport_info_t *pmportinfo = NULL; 15516 boolean_t dev_existed = B_TRUE; 15517 15518 /* Sanity check */ 15519 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) 15520 return (ENOTSUP); 15521 15522 cport = sata_device->satadev_addr.cport; 15523 pmport = sata_device->satadev_addr.pmport; 15524 qual = sata_device->satadev_addr.qual; 15525 15526 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 15527 15528 /* 15529 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL()) 15530 * is a device. But what we are dealing with is port/pmport. 15531 */ 15532 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 15533 if (qual == SATA_ADDR_DCPORT) 15534 sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT; 15535 else 15536 sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT; 15537 15538 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15539 if (qual == SATA_ADDR_PMPORT) { 15540 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 15541 if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN || 15542 pmportinfo->pmport_dev_type == SATA_DTYPE_NONE) 15543 dev_existed = B_FALSE; 15544 } else { /* cport */ 15545 if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN || 15546 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 15547 dev_existed = B_FALSE; 15548 } 15549 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15550 15551 /* Just let HBA driver to activate port, if necessary */ 15552 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 15553 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 15554 /* 15555 * Port activation failure - do not change port state unless 15556 * the state returned by HBA indicates a port failure. 15557 */ 15558 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15559 cport)->cport_mutex); 15560 sata_update_port_info(sata_hba_inst, sata_device); 15561 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 15562 if (qual == SATA_ADDR_PMPORT) { 15563 mutex_enter(&pmportinfo->pmport_mutex); 15564 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 15565 mutex_exit(&pmportinfo->pmport_mutex); 15566 } else 15567 cportinfo->cport_state = SATA_PSTATE_FAILED; 15568 15569 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15570 cport)->cport_mutex); 15571 SATA_LOG_D((sata_hba_inst, CE_WARN, 15572 "sata_hba_ioctl: port activate: cannot activate " 15573 "SATA port %d:%d", cport, pmport)); 15574 return (EIO); 15575 } 15576 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15577 } 15578 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15579 if (qual == SATA_ADDR_PMPORT) { 15580 mutex_enter(&pmportinfo->pmport_mutex); 15581 pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN; 15582 mutex_exit(&pmportinfo->pmport_mutex); 15583 } else 15584 cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN; 15585 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15586 15587 /* 15588 * Re-probe port to find its current state and possibly attached device. 15589 * Port re-probing may change the cportinfo device type if device is 15590 * found attached. 15591 * If port probing failed, the device type would be set to 15592 * SATA_DTYPE_NONE. 15593 */ 15594 (void) sata_reprobe_port(sata_hba_inst, sata_device, 15595 SATA_DEV_IDENTIFY_RETRY); 15596 15597 /* 15598 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 15599 * without the hint. 15600 */ 15601 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 15602 SE_NO_HINT); 15603 15604 if (dev_existed == B_FALSE) { 15605 if (qual == SATA_ADDR_PMPORT && 15606 pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 15607 /* 15608 * That's the transition from the "inactive" port state 15609 * or the active port without a device attached to the 15610 * active port state with a device attached. 15611 */ 15612 sata_log(sata_hba_inst, CE_WARN, 15613 "SATA device detected at port %d:%d", 15614 cport, pmport); 15615 } else if (qual == SATA_ADDR_CPORT && 15616 cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 15617 /* 15618 * That's the transition from the "inactive" port state 15619 * or the active port without a device attached to the 15620 * active port state with a device attached. 15621 */ 15622 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 15623 sata_log(sata_hba_inst, CE_WARN, 15624 "SATA device detected at port %d", cport); 15625 } else { 15626 sata_log(sata_hba_inst, CE_WARN, 15627 "SATA port multiplier detected at port %d", 15628 cport); 15629 } 15630 } 15631 } 15632 return (0); 15633 } 15634 15635 15636 15637 /* 15638 * Process ioctl reset port request. 15639 * 15640 * NOTE: Port-Multiplier is supported. 15641 */ 15642 static int 15643 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst, 15644 sata_device_t *sata_device) 15645 { 15646 int cport, pmport, qual; 15647 int rv = 0; 15648 15649 cport = sata_device->satadev_addr.cport; 15650 pmport = sata_device->satadev_addr.pmport; 15651 qual = sata_device->satadev_addr.qual; 15652 15653 /* 15654 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL()) 15655 * is a device. But what we are dealing with is port/pmport. 15656 */ 15657 if (qual == SATA_ADDR_DCPORT) 15658 sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT; 15659 else 15660 sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT; 15661 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT); 15662 15663 /* Sanity check */ 15664 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 15665 SATA_LOG_D((sata_hba_inst, CE_WARN, 15666 "sata_hba_ioctl: sata_hba_tran missing required " 15667 "function sata_tran_reset_dport")); 15668 return (ENOTSUP); 15669 } 15670 15671 /* Ask HBA to reset port */ 15672 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 15673 sata_device) != SATA_SUCCESS) { 15674 SATA_LOG_D((sata_hba_inst, CE_WARN, 15675 "sata_hba_ioctl: reset port: failed %d:%d", 15676 cport, pmport)); 15677 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15678 cport_mutex); 15679 sata_update_port_info(sata_hba_inst, sata_device); 15680 if (qual == SATA_ADDR_CPORT) 15681 SATA_CPORT_STATE(sata_hba_inst, cport) = 15682 SATA_PSTATE_FAILED; 15683 else { 15684 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, 15685 pmport)); 15686 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 15687 SATA_PSTATE_FAILED; 15688 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, 15689 pmport)); 15690 } 15691 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15692 cport_mutex); 15693 rv = EIO; 15694 } 15695 15696 return (rv); 15697 } 15698 15699 /* 15700 * Process ioctl reset device request. 15701 * 15702 * NOTE: Port multiplier is supported. 15703 */ 15704 static int 15705 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst, 15706 sata_device_t *sata_device) 15707 { 15708 sata_drive_info_t *sdinfo = NULL; 15709 sata_pmult_info_t *pmultinfo = NULL; 15710 int cport, pmport; 15711 int rv = 0; 15712 15713 /* Sanity check */ 15714 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 15715 SATA_LOG_D((sata_hba_inst, CE_WARN, 15716 "sata_hba_ioctl: sata_hba_tran missing required " 15717 "function sata_tran_reset_dport")); 15718 return (ENOTSUP); 15719 } 15720 15721 cport = sata_device->satadev_addr.cport; 15722 pmport = sata_device->satadev_addr.pmport; 15723 15724 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15725 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) { 15726 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == 15727 SATA_DTYPE_PMULT) 15728 pmultinfo = SATA_CPORT_INFO(sata_hba_inst, cport)-> 15729 cport_devp.cport_sata_pmult; 15730 else 15731 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 15732 sata_device->satadev_addr.cport); 15733 } else { /* port multiplier */ 15734 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 15735 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 15736 sata_device->satadev_addr.cport, 15737 sata_device->satadev_addr.pmport); 15738 } 15739 if (sdinfo == NULL && pmultinfo == NULL) { 15740 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15741 return (EINVAL); 15742 } 15743 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15744 15745 /* Ask HBA to reset device */ 15746 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 15747 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 15748 SATA_LOG_D((sata_hba_inst, CE_WARN, 15749 "sata_hba_ioctl: reset device: failed at port %d:%d", 15750 cport, pmport)); 15751 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15752 cport_mutex); 15753 sata_update_port_info(sata_hba_inst, sata_device); 15754 /* 15755 * Device info structure remains attached. Another device reset 15756 * or port disconnect/connect and re-probing is 15757 * needed to change it's state 15758 */ 15759 if (sdinfo != NULL) { 15760 sdinfo->satadrv_state &= ~SATA_STATE_READY; 15761 sdinfo->satadrv_state |= SATA_DSTATE_FAILED; 15762 } else if (pmultinfo != NULL) { 15763 pmultinfo->pmult_state &= ~SATA_STATE_READY; 15764 pmultinfo->pmult_state |= SATA_DSTATE_FAILED; 15765 } 15766 15767 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15768 rv = EIO; 15769 } 15770 /* 15771 * If attached device was a port multiplier, some extra processing 15772 * may be needed to bring it back. SATA specification requies a 15773 * mandatory software reset on host port to reliably enumerate a port 15774 * multiplier, the HBA driver should handle that after reset 15775 * operation. 15776 */ 15777 return (rv); 15778 } 15779 15780 15781 /* 15782 * Process ioctl reset all request. 15783 */ 15784 static int 15785 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst) 15786 { 15787 sata_device_t sata_device; 15788 int rv = 0; 15789 int tcport; 15790 15791 sata_device.satadev_rev = SATA_DEVICE_REV; 15792 15793 /* 15794 * There is no protection here for configured devices. 15795 */ 15796 /* Sanity check */ 15797 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 15798 SATA_LOG_D((sata_hba_inst, CE_WARN, 15799 "sata_hba_ioctl: sata_hba_tran missing required " 15800 "function sata_tran_reset_dport")); 15801 return (ENOTSUP); 15802 } 15803 15804 /* 15805 * Need to lock all ports, not just one. 15806 * If any port is locked by event processing, fail the whole operation. 15807 * One port is already locked, but for simplicity lock it again. 15808 */ 15809 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) { 15810 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15811 cport_mutex); 15812 if (((SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15813 cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) { 15814 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15815 cport_mutex); 15816 rv = EBUSY; 15817 break; 15818 } else { 15819 /* 15820 * It is enough to lock cport in command-based 15821 * switching mode. 15822 */ 15823 SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15824 cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 15825 } 15826 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15827 cport_mutex); 15828 } 15829 15830 if (rv == 0) { 15831 /* 15832 * All cports were successfully locked. 15833 * Reset main SATA controller. 15834 * Set the device address to port 0, to have a valid device 15835 * address. 15836 */ 15837 sata_device.satadev_addr.qual = SATA_ADDR_CNTRL; 15838 sata_device.satadev_addr.cport = 0; 15839 sata_device.satadev_addr.pmport = 0; 15840 15841 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 15842 (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) { 15843 SATA_LOG_D((sata_hba_inst, CE_WARN, 15844 "sata_hba_ioctl: reset controller failed")); 15845 return (EIO); 15846 } 15847 } 15848 /* 15849 * Unlock all ports 15850 */ 15851 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) { 15852 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15853 cport_mutex); 15854 SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15855 cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY; 15856 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15857 cport_mutex); 15858 } 15859 15860 /* 15861 * This operation returns EFAULT if either reset 15862 * controller failed or a re-probing of any port failed. 15863 */ 15864 return (rv); 15865 } 15866 15867 15868 /* 15869 * Process ioctl port self test request. 15870 * 15871 * NOTE: Port multiplier code is not completed nor tested. 15872 */ 15873 static int 15874 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst, 15875 sata_device_t *sata_device) 15876 { 15877 int cport, pmport, qual; 15878 int rv = 0; 15879 15880 /* Sanity check */ 15881 if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL) 15882 return (ENOTSUP); 15883 15884 cport = sata_device->satadev_addr.cport; 15885 pmport = sata_device->satadev_addr.pmport; 15886 qual = sata_device->satadev_addr.qual; 15887 15888 /* 15889 * There is no protection here for a configured 15890 * device attached to this port. 15891 */ 15892 15893 if ((*SATA_SELFTEST_FUNC(sata_hba_inst)) 15894 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 15895 SATA_LOG_D((sata_hba_inst, CE_WARN, 15896 "sata_hba_ioctl: port selftest: " 15897 "failed port %d:%d", cport, pmport)); 15898 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15899 cport_mutex); 15900 sata_update_port_info(sata_hba_inst, sata_device); 15901 if (qual == SATA_ADDR_CPORT) 15902 SATA_CPORT_STATE(sata_hba_inst, cport) = 15903 SATA_PSTATE_FAILED; 15904 else { /* port multiplier device port */ 15905 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, 15906 cport, pmport)); 15907 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 15908 SATA_PSTATE_FAILED; 15909 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, 15910 cport, pmport)); 15911 } 15912 15913 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15914 cport_mutex); 15915 return (EIO); 15916 } 15917 /* 15918 * Beacuse the port was reset in the course of testing, it should be 15919 * re-probed and attached device state should be restored. At this 15920 * point the port state is unknown - it's state is HBA-specific. 15921 * Force port re-probing to get it into a known state. 15922 */ 15923 if (sata_reprobe_port(sata_hba_inst, sata_device, 15924 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 15925 rv = EIO; 15926 return (rv); 15927 } 15928 15929 15930 /* 15931 * sata_cfgadm_state: 15932 * Use the sata port state and state of the target node to figure out 15933 * the cfgadm_state. 15934 * 15935 * The port argument is a value with encoded cport, 15936 * pmport and address qualifier, in the same manner as a scsi target number. 15937 * SCSI_TO_SATA_CPORT macro extracts cport number, 15938 * SCSI_TO_SATA_PMPORT extracts pmport number and 15939 * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag. 15940 * 15941 * Port multiplier is supported. 15942 */ 15943 15944 static void 15945 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port, 15946 devctl_ap_state_t *ap_state) 15947 { 15948 uint8_t cport, pmport, qual; 15949 uint32_t port_state, pmult_state; 15950 uint32_t dev_type; 15951 sata_drive_info_t *sdinfo; 15952 15953 cport = SCSI_TO_SATA_CPORT(port); 15954 pmport = SCSI_TO_SATA_PMPORT(port); 15955 qual = SCSI_TO_SATA_ADDR_QUAL(port); 15956 15957 /* Check cport state */ 15958 port_state = SATA_CPORT_STATE(sata_hba_inst, cport); 15959 if (port_state & SATA_PSTATE_SHUTDOWN || 15960 port_state & SATA_PSTATE_FAILED) { 15961 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 15962 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 15963 if (port_state & SATA_PSTATE_FAILED) 15964 ap_state->ap_condition = AP_COND_FAILED; 15965 else 15966 ap_state->ap_condition = AP_COND_UNKNOWN; 15967 15968 return; 15969 } 15970 15971 /* cport state is okay. Now check pmport state */ 15972 if (qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) { 15973 /* Sanity check */ 15974 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 15975 SATA_DTYPE_PMULT || SATA_PMPORT_INFO(sata_hba_inst, 15976 cport, pmport) == NULL) 15977 return; 15978 port_state = SATA_PMPORT_STATE(sata_hba_inst, cport, pmport); 15979 if (port_state & SATA_PSTATE_SHUTDOWN || 15980 port_state & SATA_PSTATE_FAILED) { 15981 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 15982 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 15983 if (port_state & SATA_PSTATE_FAILED) 15984 ap_state->ap_condition = AP_COND_FAILED; 15985 else 15986 ap_state->ap_condition = AP_COND_UNKNOWN; 15987 15988 return; 15989 } 15990 } 15991 15992 /* Port is enabled and ready */ 15993 if (qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_CPORT) 15994 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, cport); 15995 else 15996 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport); 15997 15998 switch (dev_type) { 15999 case SATA_DTYPE_NONE: 16000 { 16001 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 16002 ap_state->ap_condition = AP_COND_OK; 16003 /* No device attached */ 16004 ap_state->ap_rstate = AP_RSTATE_EMPTY; 16005 break; 16006 } 16007 case SATA_DTYPE_PMULT: 16008 { 16009 /* Need to check port multiplier state */ 16010 ASSERT(qual == SATA_ADDR_DCPORT); 16011 pmult_state = SATA_PMULT_INFO(sata_hba_inst, cport)-> 16012 pmult_state; 16013 if (pmult_state & (SATA_PSTATE_SHUTDOWN|SATA_PSTATE_FAILED)) { 16014 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 16015 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 16016 if (pmult_state & SATA_PSTATE_FAILED) 16017 ap_state->ap_condition = AP_COND_FAILED; 16018 else 16019 ap_state->ap_condition = AP_COND_UNKNOWN; 16020 16021 return; 16022 } 16023 16024 /* Port multiplier is not configurable */ 16025 ap_state->ap_ostate = AP_OSTATE_CONFIGURED; 16026 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 16027 ap_state->ap_condition = AP_COND_OK; 16028 break; 16029 } 16030 16031 case SATA_DTYPE_ATADISK: 16032 case SATA_DTYPE_ATAPICD: 16033 case SATA_DTYPE_ATAPITAPE: 16034 case SATA_DTYPE_ATAPIDISK: 16035 { 16036 dev_info_t *tdip = NULL; 16037 dev_info_t *dip = NULL; 16038 int circ; 16039 16040 dip = SATA_DIP(sata_hba_inst); 16041 tdip = sata_get_target_dip(dip, cport, pmport); 16042 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 16043 if (tdip != NULL) { 16044 ndi_devi_enter(dip, &circ); 16045 mutex_enter(&(DEVI(tdip)->devi_lock)); 16046 if (DEVI_IS_DEVICE_REMOVED(tdip)) { 16047 /* 16048 * There could be the case where previously 16049 * configured and opened device was removed 16050 * and unknown device was plugged. 16051 * In such case we want to show a device, and 16052 * its configured or unconfigured state but 16053 * indicate unusable condition untill the 16054 * old target node is released and removed. 16055 */ 16056 ap_state->ap_condition = AP_COND_UNUSABLE; 16057 } else { 16058 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, 16059 cport)); 16060 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 16061 cport); 16062 if (sdinfo != NULL) { 16063 if ((sdinfo->satadrv_state & 16064 SATA_DSTATE_FAILED) != 0) 16065 ap_state->ap_condition = 16066 AP_COND_FAILED; 16067 else 16068 ap_state->ap_condition = 16069 AP_COND_OK; 16070 } else { 16071 ap_state->ap_condition = 16072 AP_COND_UNKNOWN; 16073 } 16074 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, 16075 cport)); 16076 } 16077 if ((DEVI_IS_DEVICE_OFFLINE(tdip)) || 16078 (DEVI_IS_DEVICE_DOWN(tdip))) { 16079 ap_state->ap_ostate = 16080 AP_OSTATE_UNCONFIGURED; 16081 } else { 16082 ap_state->ap_ostate = 16083 AP_OSTATE_CONFIGURED; 16084 } 16085 mutex_exit(&(DEVI(tdip)->devi_lock)); 16086 ndi_devi_exit(dip, circ); 16087 } else { 16088 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 16089 ap_state->ap_condition = AP_COND_UNKNOWN; 16090 } 16091 break; 16092 } 16093 case SATA_DTYPE_ATAPIPROC: 16094 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 16095 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 16096 ap_state->ap_condition = AP_COND_OK; 16097 break; 16098 default: 16099 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 16100 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 16101 ap_state->ap_condition = AP_COND_UNKNOWN; 16102 /* 16103 * This is actually internal error condition (non fatal), 16104 * because we have already checked all defined device types. 16105 */ 16106 SATA_LOG_D((sata_hba_inst, CE_WARN, 16107 "sata_cfgadm_state: Internal error: " 16108 "unknown device type")); 16109 break; 16110 } 16111 } 16112 16113 16114 /* 16115 * Process ioctl get device path request. 16116 * 16117 * NOTE: Port multiplier has no target dip. Devices connected to port 16118 * multiplier have target node attached to the HBA node. The only difference 16119 * between them and the directly-attached device node is a target address. 16120 */ 16121 static int 16122 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst, 16123 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 16124 { 16125 char path[MAXPATHLEN]; 16126 uint32_t size; 16127 dev_info_t *tdip; 16128 16129 (void) strcpy(path, "/devices"); 16130 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 16131 &sata_device->satadev_addr)) == NULL) { 16132 /* 16133 * No such device. If this is a request for a size, do not 16134 * return EINVAL for non-existing target, because cfgadm 16135 * will then indicate a meaningless ioctl failure. 16136 * If this is a request for a path, indicate invalid 16137 * argument. 16138 */ 16139 if (ioc->get_size == 0) 16140 return (EINVAL); 16141 } else { 16142 (void) ddi_pathname(tdip, path + strlen(path)); 16143 } 16144 size = strlen(path) + 1; 16145 16146 if (ioc->get_size != 0) { 16147 if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz, 16148 mode) != 0) 16149 return (EFAULT); 16150 } else { 16151 if (ioc->bufsiz != size) 16152 return (EINVAL); 16153 16154 else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz, 16155 mode) != 0) 16156 return (EFAULT); 16157 } 16158 return (0); 16159 } 16160 16161 /* 16162 * Process ioctl get attachment point type request. 16163 * 16164 * NOTE: Port multiplier is supported. 16165 */ 16166 static int 16167 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst, 16168 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 16169 { 16170 uint32_t type_len; 16171 const char *ap_type; 16172 int dev_type; 16173 16174 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 16175 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, 16176 sata_device->satadev_addr.cport); 16177 else /* pmport */ 16178 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, 16179 sata_device->satadev_addr.cport, 16180 sata_device->satadev_addr.pmport); 16181 16182 switch (dev_type) { 16183 case SATA_DTYPE_NONE: 16184 ap_type = "port"; 16185 break; 16186 16187 case SATA_DTYPE_ATADISK: 16188 case SATA_DTYPE_ATAPIDISK: 16189 ap_type = "disk"; 16190 break; 16191 16192 case SATA_DTYPE_ATAPICD: 16193 ap_type = "cd/dvd"; 16194 break; 16195 16196 case SATA_DTYPE_ATAPITAPE: 16197 ap_type = "tape"; 16198 break; 16199 16200 case SATA_DTYPE_ATAPIPROC: 16201 ap_type = "processor"; 16202 break; 16203 16204 case SATA_DTYPE_PMULT: 16205 ap_type = "sata-pmult"; 16206 break; 16207 16208 case SATA_DTYPE_UNKNOWN: 16209 ap_type = "unknown"; 16210 break; 16211 16212 default: 16213 ap_type = "unsupported"; 16214 break; 16215 16216 } /* end of dev_type switch */ 16217 16218 type_len = strlen(ap_type) + 1; 16219 16220 if (ioc->get_size) { 16221 if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz, 16222 mode) != 0) 16223 return (EFAULT); 16224 } else { 16225 if (ioc->bufsiz != type_len) 16226 return (EINVAL); 16227 16228 if (ddi_copyout((void *)ap_type, ioc->buf, 16229 ioc->bufsiz, mode) != 0) 16230 return (EFAULT); 16231 } 16232 return (0); 16233 16234 } 16235 16236 /* 16237 * Process ioctl get device model info request. 16238 * This operation should return to cfgadm the device model 16239 * information string 16240 * 16241 * NOTE: Port multiplier is supported. 16242 */ 16243 static int 16244 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst, 16245 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 16246 { 16247 sata_drive_info_t *sdinfo; 16248 uint32_t info_len; 16249 char ap_info[SATA_ID_MODEL_LEN + 1]; 16250 16251 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 16252 sata_device->satadev_addr.cport)->cport_mutex); 16253 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 16254 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 16255 sata_device->satadev_addr.cport); 16256 else /* port multiplier */ 16257 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 16258 sata_device->satadev_addr.cport, 16259 sata_device->satadev_addr.pmport); 16260 if (sdinfo == NULL) { 16261 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 16262 sata_device->satadev_addr.cport)->cport_mutex); 16263 return (EINVAL); 16264 } 16265 16266 #ifdef _LITTLE_ENDIAN 16267 swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN); 16268 #else /* _LITTLE_ENDIAN */ 16269 bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN); 16270 #endif /* _LITTLE_ENDIAN */ 16271 16272 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 16273 sata_device->satadev_addr.cport)->cport_mutex); 16274 16275 ap_info[SATA_ID_MODEL_LEN] = '\0'; 16276 16277 info_len = strlen(ap_info) + 1; 16278 16279 if (ioc->get_size) { 16280 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 16281 mode) != 0) 16282 return (EFAULT); 16283 } else { 16284 if (ioc->bufsiz < info_len) 16285 return (EINVAL); 16286 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 16287 mode) != 0) 16288 return (EFAULT); 16289 } 16290 return (0); 16291 } 16292 16293 16294 /* 16295 * Process ioctl get device firmware revision info request. 16296 * This operation should return to cfgadm the device firmware revision 16297 * information string 16298 * 16299 * Port multiplier is supported. 16300 */ 16301 static int 16302 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst, 16303 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 16304 { 16305 sata_drive_info_t *sdinfo; 16306 uint32_t info_len; 16307 char ap_info[SATA_ID_FW_LEN + 1]; 16308 16309 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 16310 sata_device->satadev_addr.cport)->cport_mutex); 16311 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 16312 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 16313 sata_device->satadev_addr.cport); 16314 else /* port multiplier */ 16315 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 16316 sata_device->satadev_addr.cport, 16317 sata_device->satadev_addr.pmport); 16318 if (sdinfo == NULL) { 16319 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 16320 sata_device->satadev_addr.cport)->cport_mutex); 16321 return (EINVAL); 16322 } 16323 16324 #ifdef _LITTLE_ENDIAN 16325 swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN); 16326 #else /* _LITTLE_ENDIAN */ 16327 bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN); 16328 #endif /* _LITTLE_ENDIAN */ 16329 16330 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 16331 sata_device->satadev_addr.cport)->cport_mutex); 16332 16333 ap_info[SATA_ID_FW_LEN] = '\0'; 16334 16335 info_len = strlen(ap_info) + 1; 16336 16337 if (ioc->get_size) { 16338 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 16339 mode) != 0) 16340 return (EFAULT); 16341 } else { 16342 if (ioc->bufsiz < info_len) 16343 return (EINVAL); 16344 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 16345 mode) != 0) 16346 return (EFAULT); 16347 } 16348 return (0); 16349 } 16350 16351 16352 /* 16353 * Process ioctl get device serial number info request. 16354 * This operation should return to cfgadm the device serial number string. 16355 * 16356 * NOTE: Port multiplier is supported. 16357 */ 16358 static int 16359 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst, 16360 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 16361 { 16362 sata_drive_info_t *sdinfo; 16363 uint32_t info_len; 16364 char ap_info[SATA_ID_SERIAL_LEN + 1]; 16365 16366 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 16367 sata_device->satadev_addr.cport)->cport_mutex); 16368 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 16369 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 16370 sata_device->satadev_addr.cport); 16371 else /* port multiplier */ 16372 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 16373 sata_device->satadev_addr.cport, 16374 sata_device->satadev_addr.pmport); 16375 if (sdinfo == NULL) { 16376 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 16377 sata_device->satadev_addr.cport)->cport_mutex); 16378 return (EINVAL); 16379 } 16380 16381 #ifdef _LITTLE_ENDIAN 16382 swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN); 16383 #else /* _LITTLE_ENDIAN */ 16384 bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN); 16385 #endif /* _LITTLE_ENDIAN */ 16386 16387 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 16388 sata_device->satadev_addr.cport)->cport_mutex); 16389 16390 ap_info[SATA_ID_SERIAL_LEN] = '\0'; 16391 16392 info_len = strlen(ap_info) + 1; 16393 16394 if (ioc->get_size) { 16395 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 16396 mode) != 0) 16397 return (EFAULT); 16398 } else { 16399 if (ioc->bufsiz < info_len) 16400 return (EINVAL); 16401 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 16402 mode) != 0) 16403 return (EFAULT); 16404 } 16405 return (0); 16406 } 16407 16408 16409 /* 16410 * Preset scsi extended sense data (to NO SENSE) 16411 * First 18 bytes of the sense data are preset to current valid sense 16412 * with a key NO SENSE data. 16413 * 16414 * Returns void 16415 */ 16416 static void 16417 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense) 16418 { 16419 sense->es_valid = 1; /* Valid sense */ 16420 sense->es_class = CLASS_EXTENDED_SENSE; /* 0x70 - current err */ 16421 sense->es_key = KEY_NO_SENSE; 16422 sense->es_info_1 = 0; 16423 sense->es_info_2 = 0; 16424 sense->es_info_3 = 0; 16425 sense->es_info_4 = 0; 16426 sense->es_add_len = 10; /* Additional length - replace with a def */ 16427 sense->es_cmd_info[0] = 0; 16428 sense->es_cmd_info[1] = 0; 16429 sense->es_cmd_info[2] = 0; 16430 sense->es_cmd_info[3] = 0; 16431 sense->es_add_code = 0; 16432 sense->es_qual_code = 0; 16433 } 16434 16435 /* 16436 * Register a legacy cmdk-style devid for the target (disk) device. 16437 * 16438 * Note: This function is called only when the HBA devinfo node has the 16439 * property "use-cmdk-devid-format" set. This property indicates that 16440 * devid compatible with old cmdk (target) driver is to be generated 16441 * for any target device attached to this controller. This will take 16442 * precedence over the devid generated by sd (target) driver. 16443 * This function is derived from cmdk_devid_setup() function in cmdk.c. 16444 */ 16445 static void 16446 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo) 16447 { 16448 char *hwid; 16449 int modlen; 16450 int serlen; 16451 int rval; 16452 ddi_devid_t devid; 16453 16454 /* 16455 * device ID is a concatanation of model number, "=", serial number. 16456 */ 16457 hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP); 16458 bcopy(&sdinfo->satadrv_id.ai_model, hwid, 16459 sizeof (sdinfo->satadrv_id.ai_model)); 16460 swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model)); 16461 modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model)); 16462 if (modlen == 0) 16463 goto err; 16464 hwid[modlen++] = '='; 16465 bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen], 16466 sizeof (sdinfo->satadrv_id.ai_drvser)); 16467 swab(&hwid[modlen], &hwid[modlen], 16468 sizeof (sdinfo->satadrv_id.ai_drvser)); 16469 serlen = sata_check_modser(&hwid[modlen], 16470 sizeof (sdinfo->satadrv_id.ai_drvser)); 16471 if (serlen == 0) 16472 goto err; 16473 hwid[modlen + serlen] = 0; /* terminate the hwid string */ 16474 16475 /* initialize/register devid */ 16476 if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL, 16477 (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) { 16478 rval = ddi_devid_register(dip, devid); 16479 /* 16480 * Free up the allocated devid buffer. 16481 * NOTE: This doesn't mean unregistering devid. 16482 */ 16483 ddi_devid_free(devid); 16484 } 16485 16486 if (rval != DDI_SUCCESS) 16487 cmn_err(CE_WARN, "sata: failed to create devid for the disk" 16488 " on port %d", sdinfo->satadrv_addr.cport); 16489 err: 16490 kmem_free(hwid, LEGACY_HWID_LEN); 16491 } 16492 16493 /* 16494 * valid model/serial string must contain a non-zero non-space characters. 16495 * trim trailing spaces/NULLs. 16496 */ 16497 static int 16498 sata_check_modser(char *buf, int buf_len) 16499 { 16500 boolean_t ret; 16501 char *s; 16502 int i; 16503 int tb; 16504 char ch; 16505 16506 ret = B_FALSE; 16507 s = buf; 16508 for (i = 0; i < buf_len; i++) { 16509 ch = *s++; 16510 if (ch != ' ' && ch != '\0') 16511 tb = i + 1; 16512 if (ch != ' ' && ch != '\0' && ch != '0') 16513 ret = B_TRUE; 16514 } 16515 16516 if (ret == B_FALSE) 16517 return (0); /* invalid string */ 16518 16519 return (tb); /* return length */ 16520 } 16521 16522 /* 16523 * sata_set_drive_features function compares current device features setting 16524 * with the saved device features settings and, if there is a difference, 16525 * it restores device features setting to the previously saved state. 16526 * It also arbitrarily tries to select the highest supported DMA mode. 16527 * Device Identify or Identify Packet Device data has to be current. 16528 * At the moment read ahead and write cache are considered for all devices. 16529 * For atapi devices, Removable Media Status Notification is set in addition 16530 * to common features. 16531 * 16532 * This function cannot be called in the interrupt context (it may sleep). 16533 * 16534 * The input argument sdinfo should point to the drive info structure 16535 * to be updated after features are set. Note, that only 16536 * device (packet) identify data is updated, not the flags indicating the 16537 * supported features. 16538 * 16539 * Returns SATA_SUCCESS if successful or there was nothing to do. 16540 * Device Identify data in the drive info structure pointed to by the sdinfo 16541 * arguments is updated even when no features were set or changed. 16542 * 16543 * Returns SATA_FAILURE if device features could not be set or DMA mode 16544 * for a disk cannot be set and device identify data cannot be fetched. 16545 * 16546 * Returns SATA_RETRY if device features could not be set (other than disk 16547 * DMA mode) but the device identify data was fetched successfully. 16548 * 16549 * Note: This function may fail the port, making it inaccessible. 16550 * In such case the explicit port disconnect/connect or physical device 16551 * detach/attach is required to re-evaluate port state again. 16552 */ 16553 16554 static int 16555 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst, 16556 sata_drive_info_t *sdinfo, int restore) 16557 { 16558 int rval = SATA_SUCCESS; 16559 int rval_set; 16560 sata_drive_info_t new_sdinfo; 16561 char *finfo = "sata_set_drive_features: cannot"; 16562 char *finfox; 16563 int cache_op; 16564 16565 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 16566 new_sdinfo.satadrv_addr = sdinfo->satadrv_addr; 16567 new_sdinfo.satadrv_type = sdinfo->satadrv_type; 16568 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 16569 /* 16570 * Cannot get device identification - caller may retry later 16571 */ 16572 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16573 "%s fetch device identify data\n", finfo); 16574 return (SATA_FAILURE); 16575 } 16576 finfox = (restore != 0) ? " restore device features" : 16577 " initialize device features\n"; 16578 16579 switch (sdinfo->satadrv_type) { 16580 case SATA_DTYPE_ATADISK: 16581 /* Arbitrarily set UDMA mode */ 16582 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) != 16583 SATA_SUCCESS) { 16584 SATA_LOG_D((sata_hba_inst, CE_WARN, 16585 "%s set UDMA mode\n", finfo)); 16586 return (SATA_FAILURE); 16587 } 16588 break; 16589 case SATA_DTYPE_ATAPICD: 16590 case SATA_DTYPE_ATAPITAPE: 16591 case SATA_DTYPE_ATAPIDISK: 16592 /* Set Removable Media Status Notification, if necessary */ 16593 if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) && 16594 restore != 0) { 16595 if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) && 16596 (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))|| 16597 ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) && 16598 SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) { 16599 /* Current setting does not match saved one */ 16600 if (sata_set_rmsn(sata_hba_inst, sdinfo, 16601 sdinfo->satadrv_settings & 16602 SATA_DEV_RMSN) != SATA_SUCCESS) 16603 rval = SATA_FAILURE; 16604 } 16605 } 16606 /* 16607 * We have to set Multiword DMA or UDMA, if it is supported, as 16608 * we want to use DMA transfer mode whenever possible. 16609 * Some devices require explicit setting of the DMA mode. 16610 */ 16611 if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) { 16612 /* Set highest supported DMA mode */ 16613 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) != 16614 SATA_SUCCESS) { 16615 SATA_LOG_D((sata_hba_inst, CE_WARN, 16616 "%s set UDMA mode\n", finfo)); 16617 rval = SATA_FAILURE; 16618 } 16619 } 16620 break; 16621 } 16622 16623 if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) && 16624 !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) { 16625 /* 16626 * neither READ AHEAD nor WRITE CACHE is supported 16627 * - do nothing 16628 */ 16629 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16630 "settable features not supported\n", NULL); 16631 goto update_sdinfo; 16632 } 16633 16634 if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) && 16635 (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) && 16636 (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) && 16637 (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) { 16638 /* 16639 * both READ AHEAD and WRITE CACHE are enabled 16640 * - Nothing to do 16641 */ 16642 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16643 "no device features to set\n", NULL); 16644 goto update_sdinfo; 16645 } 16646 16647 cache_op = 0; 16648 16649 if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) { 16650 if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) && 16651 !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) { 16652 /* Enable read ahead / read cache */ 16653 cache_op = SATAC_SF_ENABLE_READ_AHEAD; 16654 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16655 "enabling read cache\n", NULL); 16656 } else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) && 16657 SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) { 16658 /* Disable read ahead / read cache */ 16659 cache_op = SATAC_SF_DISABLE_READ_AHEAD; 16660 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16661 "disabling read cache\n", NULL); 16662 } 16663 16664 if (cache_op != 0) { 16665 /* Try to set read cache mode */ 16666 rval_set = sata_set_cache_mode(sata_hba_inst, 16667 &new_sdinfo, cache_op); 16668 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS) 16669 rval = rval_set; 16670 } 16671 } 16672 16673 cache_op = 0; 16674 16675 if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) { 16676 if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) && 16677 !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) { 16678 /* Enable write cache */ 16679 cache_op = SATAC_SF_ENABLE_WRITE_CACHE; 16680 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16681 "enabling write cache\n", NULL); 16682 } else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) && 16683 SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) { 16684 /* Disable write cache */ 16685 cache_op = SATAC_SF_DISABLE_WRITE_CACHE; 16686 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16687 "disabling write cache\n", NULL); 16688 } 16689 16690 if (cache_op != 0) { 16691 /* Try to set write cache mode */ 16692 rval_set = sata_set_cache_mode(sata_hba_inst, 16693 &new_sdinfo, cache_op); 16694 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS) 16695 rval = rval_set; 16696 } 16697 } 16698 if (rval != SATA_SUCCESS) 16699 SATA_LOG_D((sata_hba_inst, CE_WARN, 16700 "%s %s", finfo, finfox)); 16701 16702 update_sdinfo: 16703 /* 16704 * We need to fetch Device Identify data again 16705 */ 16706 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 16707 /* 16708 * Cannot get device identification - retry later 16709 */ 16710 SATA_LOG_D((sata_hba_inst, CE_WARN, 16711 "%s re-fetch device identify data\n", finfo)); 16712 rval = SATA_FAILURE; 16713 } 16714 /* Copy device sata info. */ 16715 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 16716 16717 return (rval); 16718 } 16719 16720 16721 /* 16722 * 16723 * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if 16724 * unable to determine. 16725 * 16726 * Cannot be called in an interrupt context. 16727 * 16728 * Called by sata_build_lsense_page_2f() 16729 */ 16730 16731 static int 16732 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst, 16733 sata_drive_info_t *sdinfo) 16734 { 16735 sata_pkt_t *spkt; 16736 sata_cmd_t *scmd; 16737 sata_pkt_txlate_t *spx; 16738 int rval; 16739 16740 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 16741 spx->txlt_sata_hba_inst = sata_hba_inst; 16742 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 16743 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 16744 if (spkt == NULL) { 16745 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16746 return (-1); 16747 } 16748 /* address is needed now */ 16749 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16750 16751 16752 /* Fill sata_pkt */ 16753 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16754 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 16755 /* Synchronous mode, no callback */ 16756 spkt->satapkt_comp = NULL; 16757 /* Timeout 30s */ 16758 spkt->satapkt_time = sata_default_pkt_time; 16759 16760 scmd = &spkt->satapkt_cmd; 16761 scmd->satacmd_flags.sata_special_regs = B_TRUE; 16762 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 16763 16764 /* Set up which registers need to be returned */ 16765 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE; 16766 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE; 16767 16768 /* Build SMART_RETURN_STATUS cmd in the sata_pkt */ 16769 scmd->satacmd_addr_type = 0; /* N/A */ 16770 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 16771 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 16772 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 16773 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 16774 scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS; 16775 scmd->satacmd_device_reg = 0; /* Always device 0 */ 16776 scmd->satacmd_cmd_reg = SATAC_SMART; 16777 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 16778 sdinfo->satadrv_addr.cport))); 16779 16780 16781 /* Send pkt to SATA HBA driver */ 16782 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 16783 SATA_TRAN_ACCEPTED || 16784 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 16785 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16786 sdinfo->satadrv_addr.cport))); 16787 /* 16788 * Whoops, no SMART RETURN STATUS 16789 */ 16790 rval = -1; 16791 } else { 16792 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16793 sdinfo->satadrv_addr.cport))); 16794 if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) { 16795 rval = -1; 16796 goto fail; 16797 } 16798 if (scmd->satacmd_status_reg & SATA_STATUS_ERR) { 16799 rval = -1; 16800 goto fail; 16801 } 16802 if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) && 16803 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2)) 16804 rval = 0; 16805 else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) && 16806 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4)) 16807 rval = 1; 16808 else { 16809 rval = -1; 16810 goto fail; 16811 } 16812 } 16813 fail: 16814 /* Free allocated resources */ 16815 sata_pkt_free(spx); 16816 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16817 16818 return (rval); 16819 } 16820 16821 /* 16822 * 16823 * Returns 0 if succeeded, -1 otherwise 16824 * 16825 * Cannot be called in an interrupt context. 16826 * 16827 */ 16828 static int 16829 sata_fetch_smart_data( 16830 sata_hba_inst_t *sata_hba_inst, 16831 sata_drive_info_t *sdinfo, 16832 struct smart_data *smart_data) 16833 { 16834 sata_pkt_t *spkt; 16835 sata_cmd_t *scmd; 16836 sata_pkt_txlate_t *spx; 16837 int rval; 16838 dev_info_t *dip = SATA_DIP(sata_hba_inst); 16839 16840 #if ! defined(lint) 16841 ASSERT(sizeof (struct smart_data) == 512); 16842 #endif 16843 16844 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 16845 spx->txlt_sata_hba_inst = sata_hba_inst; 16846 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 16847 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 16848 if (spkt == NULL) { 16849 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16850 return (-1); 16851 } 16852 /* address is needed now */ 16853 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16854 16855 16856 /* Fill sata_pkt */ 16857 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16858 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 16859 /* Synchronous mode, no callback */ 16860 spkt->satapkt_comp = NULL; 16861 /* Timeout 30s */ 16862 spkt->satapkt_time = sata_default_pkt_time; 16863 16864 scmd = &spkt->satapkt_cmd; 16865 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 16866 16867 /* 16868 * Allocate buffer for SMART data 16869 */ 16870 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 16871 sizeof (struct smart_data)); 16872 if (scmd->satacmd_bp == NULL) { 16873 sata_pkt_free(spx); 16874 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16875 SATA_LOG_D((sata_hba_inst, CE_WARN, 16876 "sata_fetch_smart_data: " 16877 "cannot allocate buffer")); 16878 return (-1); 16879 } 16880 16881 16882 /* Build SMART_READ_DATA cmd in the sata_pkt */ 16883 scmd->satacmd_addr_type = 0; /* N/A */ 16884 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 16885 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 16886 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 16887 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 16888 scmd->satacmd_features_reg = SATA_SMART_READ_DATA; 16889 scmd->satacmd_device_reg = 0; /* Always device 0 */ 16890 scmd->satacmd_cmd_reg = SATAC_SMART; 16891 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 16892 sdinfo->satadrv_addr.cport))); 16893 16894 /* Send pkt to SATA HBA driver */ 16895 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 16896 SATA_TRAN_ACCEPTED || 16897 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 16898 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16899 sdinfo->satadrv_addr.cport))); 16900 /* 16901 * Whoops, no SMART DATA available 16902 */ 16903 rval = -1; 16904 goto fail; 16905 } else { 16906 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16907 sdinfo->satadrv_addr.cport))); 16908 if (spx->txlt_buf_dma_handle != NULL) { 16909 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 16910 DDI_DMA_SYNC_FORKERNEL); 16911 ASSERT(rval == DDI_SUCCESS); 16912 if (sata_check_for_dma_error(dip, spx)) { 16913 ddi_fm_service_impact(dip, 16914 DDI_SERVICE_UNAFFECTED); 16915 rval = -1; 16916 goto fail; 16917 } 16918 } 16919 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data, 16920 sizeof (struct smart_data)); 16921 } 16922 16923 fail: 16924 /* Free allocated resources */ 16925 sata_free_local_buffer(spx); 16926 sata_pkt_free(spx); 16927 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16928 16929 return (rval); 16930 } 16931 16932 /* 16933 * Used by LOG SENSE page 0x10 16934 * Reads (in synchronous mode) the self test log data using Read Log Ext cmd. 16935 * Note: cannot be called in the interrupt context. 16936 * 16937 * return 0 for success, -1 otherwise 16938 * 16939 */ 16940 static int 16941 sata_ext_smart_selftest_read_log( 16942 sata_hba_inst_t *sata_hba_inst, 16943 sata_drive_info_t *sdinfo, 16944 struct smart_ext_selftest_log *ext_selftest_log, 16945 uint16_t block_num) 16946 { 16947 sata_pkt_txlate_t *spx; 16948 sata_pkt_t *spkt; 16949 sata_cmd_t *scmd; 16950 int rval; 16951 dev_info_t *dip = SATA_DIP(sata_hba_inst); 16952 16953 #if ! defined(lint) 16954 ASSERT(sizeof (struct smart_ext_selftest_log) == 512); 16955 #endif 16956 16957 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 16958 spx->txlt_sata_hba_inst = sata_hba_inst; 16959 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 16960 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 16961 if (spkt == NULL) { 16962 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16963 return (-1); 16964 } 16965 /* address is needed now */ 16966 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16967 16968 16969 /* Fill sata_pkt */ 16970 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16971 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 16972 /* Synchronous mode, no callback */ 16973 spkt->satapkt_comp = NULL; 16974 /* Timeout 30s */ 16975 spkt->satapkt_time = sata_default_pkt_time; 16976 16977 scmd = &spkt->satapkt_cmd; 16978 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 16979 16980 /* 16981 * Allocate buffer for SMART extended self-test log 16982 */ 16983 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 16984 sizeof (struct smart_ext_selftest_log)); 16985 if (scmd->satacmd_bp == NULL) { 16986 sata_pkt_free(spx); 16987 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16988 SATA_LOG_D((sata_hba_inst, CE_WARN, 16989 "sata_ext_smart_selftest_log: " 16990 "cannot allocate buffer")); 16991 return (-1); 16992 } 16993 16994 /* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */ 16995 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 16996 scmd->satacmd_sec_count_lsb = 1; /* One sector of selftest log */ 16997 scmd->satacmd_sec_count_msb = 0; /* One sector of selftest log */ 16998 scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE; 16999 scmd->satacmd_lba_low_msb = 0; 17000 scmd->satacmd_lba_mid_lsb = block_num & 0xff; 17001 scmd->satacmd_lba_mid_msb = block_num >> 8; 17002 scmd->satacmd_device_reg = 0; /* Always device 0 */ 17003 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 17004 17005 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 17006 sdinfo->satadrv_addr.cport))); 17007 17008 /* Send pkt to SATA HBA driver */ 17009 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 17010 SATA_TRAN_ACCEPTED || 17011 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 17012 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17013 sdinfo->satadrv_addr.cport))); 17014 17015 /* 17016 * Whoops, no SMART selftest log info available 17017 */ 17018 rval = -1; 17019 goto fail; 17020 } else { 17021 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17022 sdinfo->satadrv_addr.cport))); 17023 17024 if (spx->txlt_buf_dma_handle != NULL) { 17025 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 17026 DDI_DMA_SYNC_FORKERNEL); 17027 ASSERT(rval == DDI_SUCCESS); 17028 if (sata_check_for_dma_error(dip, spx)) { 17029 ddi_fm_service_impact(dip, 17030 DDI_SERVICE_UNAFFECTED); 17031 rval = -1; 17032 goto fail; 17033 } 17034 } 17035 bcopy(scmd->satacmd_bp->b_un.b_addr, 17036 (uint8_t *)ext_selftest_log, 17037 sizeof (struct smart_ext_selftest_log)); 17038 rval = 0; 17039 } 17040 17041 fail: 17042 /* Free allocated resources */ 17043 sata_free_local_buffer(spx); 17044 sata_pkt_free(spx); 17045 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17046 17047 return (rval); 17048 } 17049 17050 /* 17051 * Returns 0 for success, -1 otherwise 17052 * 17053 * SMART self-test log data is returned in buffer pointed to by selftest_log 17054 */ 17055 static int 17056 sata_smart_selftest_log( 17057 sata_hba_inst_t *sata_hba_inst, 17058 sata_drive_info_t *sdinfo, 17059 struct smart_selftest_log *selftest_log) 17060 { 17061 sata_pkt_t *spkt; 17062 sata_cmd_t *scmd; 17063 sata_pkt_txlate_t *spx; 17064 int rval; 17065 dev_info_t *dip = SATA_DIP(sata_hba_inst); 17066 17067 #if ! defined(lint) 17068 ASSERT(sizeof (struct smart_selftest_log) == 512); 17069 #endif 17070 17071 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 17072 spx->txlt_sata_hba_inst = sata_hba_inst; 17073 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 17074 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 17075 if (spkt == NULL) { 17076 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17077 return (-1); 17078 } 17079 /* address is needed now */ 17080 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 17081 17082 17083 /* Fill sata_pkt */ 17084 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 17085 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 17086 /* Synchronous mode, no callback */ 17087 spkt->satapkt_comp = NULL; 17088 /* Timeout 30s */ 17089 spkt->satapkt_time = sata_default_pkt_time; 17090 17091 scmd = &spkt->satapkt_cmd; 17092 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 17093 17094 /* 17095 * Allocate buffer for SMART SELFTEST LOG 17096 */ 17097 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 17098 sizeof (struct smart_selftest_log)); 17099 if (scmd->satacmd_bp == NULL) { 17100 sata_pkt_free(spx); 17101 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17102 SATA_LOG_D((sata_hba_inst, CE_WARN, 17103 "sata_smart_selftest_log: " 17104 "cannot allocate buffer")); 17105 return (-1); 17106 } 17107 17108 /* Build SMART_READ_LOG cmd in the sata_pkt */ 17109 scmd->satacmd_addr_type = 0; /* N/A */ 17110 scmd->satacmd_sec_count_lsb = 1; /* One sector of SMART log */ 17111 scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE; 17112 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 17113 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 17114 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 17115 scmd->satacmd_device_reg = 0; /* Always device 0 */ 17116 scmd->satacmd_cmd_reg = SATAC_SMART; 17117 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 17118 sdinfo->satadrv_addr.cport))); 17119 17120 /* Send pkt to SATA HBA driver */ 17121 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 17122 SATA_TRAN_ACCEPTED || 17123 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 17124 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17125 sdinfo->satadrv_addr.cport))); 17126 /* 17127 * Whoops, no SMART DATA available 17128 */ 17129 rval = -1; 17130 goto fail; 17131 } else { 17132 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17133 sdinfo->satadrv_addr.cport))); 17134 if (spx->txlt_buf_dma_handle != NULL) { 17135 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 17136 DDI_DMA_SYNC_FORKERNEL); 17137 ASSERT(rval == DDI_SUCCESS); 17138 if (sata_check_for_dma_error(dip, spx)) { 17139 ddi_fm_service_impact(dip, 17140 DDI_SERVICE_UNAFFECTED); 17141 rval = -1; 17142 goto fail; 17143 } 17144 } 17145 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log, 17146 sizeof (struct smart_selftest_log)); 17147 rval = 0; 17148 } 17149 17150 fail: 17151 /* Free allocated resources */ 17152 sata_free_local_buffer(spx); 17153 sata_pkt_free(spx); 17154 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17155 17156 return (rval); 17157 } 17158 17159 17160 /* 17161 * Returns 0 for success, -1 otherwise 17162 * 17163 * SMART READ LOG data is returned in buffer pointed to by smart_log 17164 */ 17165 static int 17166 sata_smart_read_log( 17167 sata_hba_inst_t *sata_hba_inst, 17168 sata_drive_info_t *sdinfo, 17169 uint8_t *smart_log, /* where the data should be returned */ 17170 uint8_t which_log, /* which log should be returned */ 17171 uint8_t log_size) /* # of 512 bytes in log */ 17172 { 17173 sata_pkt_t *spkt; 17174 sata_cmd_t *scmd; 17175 sata_pkt_txlate_t *spx; 17176 int rval; 17177 dev_info_t *dip = SATA_DIP(sata_hba_inst); 17178 17179 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 17180 spx->txlt_sata_hba_inst = sata_hba_inst; 17181 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 17182 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 17183 if (spkt == NULL) { 17184 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17185 return (-1); 17186 } 17187 /* address is needed now */ 17188 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 17189 17190 17191 /* Fill sata_pkt */ 17192 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 17193 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 17194 /* Synchronous mode, no callback */ 17195 spkt->satapkt_comp = NULL; 17196 /* Timeout 30s */ 17197 spkt->satapkt_time = sata_default_pkt_time; 17198 17199 scmd = &spkt->satapkt_cmd; 17200 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 17201 17202 /* 17203 * Allocate buffer for SMART READ LOG 17204 */ 17205 scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512); 17206 if (scmd->satacmd_bp == NULL) { 17207 sata_pkt_free(spx); 17208 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17209 SATA_LOG_D((sata_hba_inst, CE_WARN, 17210 "sata_smart_read_log: " "cannot allocate buffer")); 17211 return (-1); 17212 } 17213 17214 /* Build SMART_READ_LOG cmd in the sata_pkt */ 17215 scmd->satacmd_addr_type = 0; /* N/A */ 17216 scmd->satacmd_sec_count_lsb = log_size; /* what the caller asked for */ 17217 scmd->satacmd_lba_low_lsb = which_log; /* which log page */ 17218 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 17219 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 17220 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 17221 scmd->satacmd_device_reg = 0; /* Always device 0 */ 17222 scmd->satacmd_cmd_reg = SATAC_SMART; 17223 17224 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 17225 sdinfo->satadrv_addr.cport))); 17226 17227 /* Send pkt to SATA HBA driver */ 17228 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 17229 SATA_TRAN_ACCEPTED || 17230 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 17231 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17232 sdinfo->satadrv_addr.cport))); 17233 17234 /* 17235 * Whoops, no SMART DATA available 17236 */ 17237 rval = -1; 17238 goto fail; 17239 } else { 17240 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17241 sdinfo->satadrv_addr.cport))); 17242 17243 if (spx->txlt_buf_dma_handle != NULL) { 17244 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 17245 DDI_DMA_SYNC_FORKERNEL); 17246 ASSERT(rval == DDI_SUCCESS); 17247 if (sata_check_for_dma_error(dip, spx)) { 17248 ddi_fm_service_impact(dip, 17249 DDI_SERVICE_UNAFFECTED); 17250 rval = -1; 17251 goto fail; 17252 } 17253 } 17254 bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512); 17255 rval = 0; 17256 } 17257 17258 fail: 17259 /* Free allocated resources */ 17260 sata_free_local_buffer(spx); 17261 sata_pkt_free(spx); 17262 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17263 17264 return (rval); 17265 } 17266 17267 /* 17268 * Used by LOG SENSE page 0x10 17269 * 17270 * return 0 for success, -1 otherwise 17271 * 17272 */ 17273 static int 17274 sata_read_log_ext_directory( 17275 sata_hba_inst_t *sata_hba_inst, 17276 sata_drive_info_t *sdinfo, 17277 struct read_log_ext_directory *logdir) 17278 { 17279 sata_pkt_txlate_t *spx; 17280 sata_pkt_t *spkt; 17281 sata_cmd_t *scmd; 17282 int rval; 17283 dev_info_t *dip = SATA_DIP(sata_hba_inst); 17284 17285 #if ! defined(lint) 17286 ASSERT(sizeof (struct read_log_ext_directory) == 512); 17287 #endif 17288 17289 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 17290 spx->txlt_sata_hba_inst = sata_hba_inst; 17291 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 17292 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 17293 if (spkt == NULL) { 17294 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17295 return (-1); 17296 } 17297 17298 /* Fill sata_pkt */ 17299 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 17300 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 17301 /* Synchronous mode, no callback */ 17302 spkt->satapkt_comp = NULL; 17303 /* Timeout 30s */ 17304 spkt->satapkt_time = sata_default_pkt_time; 17305 17306 scmd = &spkt->satapkt_cmd; 17307 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 17308 17309 /* 17310 * Allocate buffer for SMART READ LOG EXTENDED command 17311 */ 17312 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 17313 sizeof (struct read_log_ext_directory)); 17314 if (scmd->satacmd_bp == NULL) { 17315 sata_pkt_free(spx); 17316 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17317 SATA_LOG_D((sata_hba_inst, CE_WARN, 17318 "sata_read_log_ext_directory: " 17319 "cannot allocate buffer")); 17320 return (-1); 17321 } 17322 17323 /* Build READ LOG EXT w/ log directory cmd in the sata_pkt */ 17324 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 17325 scmd->satacmd_sec_count_lsb = 1; /* One sector of directory */ 17326 scmd->satacmd_sec_count_msb = 0; /* One sector of directory */ 17327 scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY; 17328 scmd->satacmd_lba_low_msb = 0; 17329 scmd->satacmd_lba_mid_lsb = 0; 17330 scmd->satacmd_lba_mid_msb = 0; 17331 scmd->satacmd_device_reg = 0; /* Always device 0 */ 17332 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 17333 17334 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 17335 sdinfo->satadrv_addr.cport))); 17336 17337 /* Send pkt to SATA HBA driver */ 17338 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 17339 SATA_TRAN_ACCEPTED || 17340 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 17341 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17342 sdinfo->satadrv_addr.cport))); 17343 /* 17344 * Whoops, no SMART selftest log info available 17345 */ 17346 rval = -1; 17347 goto fail; 17348 } else { 17349 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17350 sdinfo->satadrv_addr.cport))); 17351 if (spx->txlt_buf_dma_handle != NULL) { 17352 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 17353 DDI_DMA_SYNC_FORKERNEL); 17354 ASSERT(rval == DDI_SUCCESS); 17355 if (sata_check_for_dma_error(dip, spx)) { 17356 ddi_fm_service_impact(dip, 17357 DDI_SERVICE_UNAFFECTED); 17358 rval = -1; 17359 goto fail; 17360 } 17361 } 17362 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir, 17363 sizeof (struct read_log_ext_directory)); 17364 rval = 0; 17365 } 17366 17367 fail: 17368 /* Free allocated resources */ 17369 sata_free_local_buffer(spx); 17370 sata_pkt_free(spx); 17371 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17372 17373 return (rval); 17374 } 17375 17376 /* 17377 * Set up error retrieval sata command for NCQ command error data 17378 * recovery. 17379 * 17380 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up, 17381 * returns SATA_FAILURE otherwise. 17382 */ 17383 static int 17384 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo) 17385 { 17386 #ifndef __lock_lint 17387 _NOTE(ARGUNUSED(sdinfo)) 17388 #endif 17389 17390 sata_pkt_t *spkt = spx->txlt_sata_pkt; 17391 sata_cmd_t *scmd; 17392 struct buf *bp; 17393 17394 /* Operation modes are up to the caller */ 17395 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 17396 17397 /* Synchronous mode, no callback - may be changed by the caller */ 17398 spkt->satapkt_comp = NULL; 17399 spkt->satapkt_time = sata_default_pkt_time; 17400 17401 scmd = &spkt->satapkt_cmd; 17402 bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t)); 17403 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 17404 17405 /* 17406 * Allocate dma_able buffer error data. 17407 * Buffer allocation will take care of buffer alignment and other DMA 17408 * attributes. 17409 */ 17410 bp = sata_alloc_local_buffer(spx, 17411 sizeof (struct sata_ncq_error_recovery_page)); 17412 if (bp == NULL) 17413 return (SATA_FAILURE); 17414 17415 bp_mapin(bp); /* make data buffer accessible */ 17416 scmd->satacmd_bp = bp; 17417 17418 /* 17419 * Set-up pointer to the buffer handle, so HBA can sync buffer 17420 * before accessing it. Handle is in usual place in translate struct. 17421 */ 17422 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle; 17423 17424 ASSERT(scmd->satacmd_num_dma_cookies != 0); 17425 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 17426 17427 return (SATA_SUCCESS); 17428 } 17429 17430 /* 17431 * sata_xlate_errors() is used to translate (S)ATA error 17432 * information to SCSI information returned in the SCSI 17433 * packet. 17434 */ 17435 static void 17436 sata_xlate_errors(sata_pkt_txlate_t *spx) 17437 { 17438 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 17439 struct scsi_extended_sense *sense; 17440 17441 scsipkt->pkt_reason = CMD_INCOMPLETE; 17442 *scsipkt->pkt_scbp = STATUS_CHECK; 17443 sense = sata_arq_sense(spx); 17444 17445 switch (spx->txlt_sata_pkt->satapkt_reason) { 17446 case SATA_PKT_PORT_ERROR: 17447 /* 17448 * We have no device data. Assume no data transfered. 17449 */ 17450 sense->es_key = KEY_HARDWARE_ERROR; 17451 break; 17452 17453 case SATA_PKT_DEV_ERROR: 17454 if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 17455 SATA_STATUS_ERR) { 17456 /* 17457 * determine dev error reason from error 17458 * reg content 17459 */ 17460 sata_decode_device_error(spx, sense); 17461 break; 17462 } 17463 /* No extended sense key - no info available */ 17464 break; 17465 17466 case SATA_PKT_TIMEOUT: 17467 scsipkt->pkt_reason = CMD_TIMEOUT; 17468 scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET; 17469 /* No extended sense key */ 17470 break; 17471 17472 case SATA_PKT_ABORTED: 17473 scsipkt->pkt_reason = CMD_ABORTED; 17474 scsipkt->pkt_statistics |= STAT_ABORTED; 17475 /* No extended sense key */ 17476 break; 17477 17478 case SATA_PKT_RESET: 17479 /* 17480 * pkt aborted either by an explicit reset request from 17481 * a host, or due to error recovery 17482 */ 17483 scsipkt->pkt_reason = CMD_RESET; 17484 scsipkt->pkt_statistics |= STAT_DEV_RESET; 17485 break; 17486 17487 default: 17488 scsipkt->pkt_reason = CMD_TRAN_ERR; 17489 break; 17490 } 17491 } 17492 17493 17494 17495 17496 /* 17497 * Log sata message 17498 * dev pathname msg line preceeds the logged message. 17499 */ 17500 17501 static void 17502 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...) 17503 { 17504 char pathname[128]; 17505 dev_info_t *dip = NULL; 17506 va_list ap; 17507 17508 mutex_enter(&sata_log_mutex); 17509 17510 va_start(ap, fmt); 17511 (void) vsprintf(sata_log_buf, fmt, ap); 17512 va_end(ap); 17513 17514 if (sata_hba_inst != NULL) { 17515 dip = SATA_DIP(sata_hba_inst); 17516 (void) ddi_pathname(dip, pathname); 17517 } else { 17518 pathname[0] = 0; 17519 } 17520 if (level == CE_CONT) { 17521 if (sata_debug_flags == 0) 17522 cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf); 17523 else 17524 cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf); 17525 } else { 17526 if (level != CE_NOTE) { 17527 cmn_err(level, "%s:\n %s", pathname, sata_log_buf); 17528 } else if (sata_msg) { 17529 cmn_err(level, "%s:\n %s", pathname, 17530 sata_log_buf); 17531 } 17532 } 17533 17534 /* sata trace debug */ 17535 sata_trace_debug(dip, sata_log_buf); 17536 17537 mutex_exit(&sata_log_mutex); 17538 } 17539 17540 17541 /* ******** Asynchronous HBA events handling & hotplugging support ******** */ 17542 17543 /* 17544 * Start or terminate the thread, depending on flag arg and current state 17545 */ 17546 static void 17547 sata_event_thread_control(int startstop) 17548 { 17549 static int sata_event_thread_terminating = 0; 17550 static int sata_event_thread_starting = 0; 17551 int i; 17552 17553 mutex_enter(&sata_event_mutex); 17554 17555 if (startstop == 0 && (sata_event_thread_starting == 1 || 17556 sata_event_thread_terminating == 1)) { 17557 mutex_exit(&sata_event_mutex); 17558 return; 17559 } 17560 if (startstop == 1 && sata_event_thread_starting == 1) { 17561 mutex_exit(&sata_event_mutex); 17562 return; 17563 } 17564 if (startstop == 1 && sata_event_thread_terminating == 1) { 17565 sata_event_thread_starting = 1; 17566 /* wait til terminate operation completes */ 17567 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 17568 while (sata_event_thread_terminating == 1) { 17569 if (i-- <= 0) { 17570 sata_event_thread_starting = 0; 17571 mutex_exit(&sata_event_mutex); 17572 #ifdef SATA_DEBUG 17573 cmn_err(CE_WARN, "sata_event_thread_control: " 17574 "timeout waiting for thread to terminate"); 17575 #endif 17576 return; 17577 } 17578 mutex_exit(&sata_event_mutex); 17579 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 17580 mutex_enter(&sata_event_mutex); 17581 } 17582 } 17583 if (startstop == 1) { 17584 if (sata_event_thread == NULL) { 17585 sata_event_thread = thread_create(NULL, 0, 17586 (void (*)())sata_event_daemon, 17587 &sata_hba_list, 0, &p0, TS_RUN, minclsyspri); 17588 } 17589 sata_event_thread_starting = 0; 17590 mutex_exit(&sata_event_mutex); 17591 return; 17592 } 17593 17594 /* 17595 * If we got here, thread may need to be terminated 17596 */ 17597 if (sata_event_thread != NULL) { 17598 int i; 17599 /* Signal event thread to go away */ 17600 sata_event_thread_terminating = 1; 17601 sata_event_thread_terminate = 1; 17602 cv_signal(&sata_event_cv); 17603 /* 17604 * Wait til daemon terminates. 17605 */ 17606 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 17607 while (sata_event_thread_terminate == 1) { 17608 mutex_exit(&sata_event_mutex); 17609 if (i-- <= 0) { 17610 /* Daemon did not go away !!! */ 17611 #ifdef SATA_DEBUG 17612 cmn_err(CE_WARN, "sata_event_thread_control: " 17613 "cannot terminate event daemon thread"); 17614 #endif 17615 mutex_enter(&sata_event_mutex); 17616 break; 17617 } 17618 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 17619 mutex_enter(&sata_event_mutex); 17620 } 17621 sata_event_thread_terminating = 0; 17622 } 17623 ASSERT(sata_event_thread_terminating == 0); 17624 ASSERT(sata_event_thread_starting == 0); 17625 mutex_exit(&sata_event_mutex); 17626 } 17627 17628 17629 /* 17630 * SATA HBA event notification function. 17631 * Events reported by SATA HBA drivers per HBA instance relate to a change in 17632 * a port and/or device state or a controller itself. 17633 * Events for different addresses/addr types cannot be combined. 17634 * A warning message is generated for each event type. 17635 * Events are not processed by this function, so only the 17636 * event flag(s)is set for an affected entity and the event thread is 17637 * waken up. Event daemon thread processes all events. 17638 * 17639 * NOTE: Since more than one event may be reported at the same time, one 17640 * cannot determine a sequence of events when opposite event are reported, eg. 17641 * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing 17642 * is taking precedence over reported events, i.e. may cause ignoring some 17643 * events. 17644 */ 17645 #define SATA_EVENT_MAX_MSG_LENGTH 79 17646 17647 void 17648 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event) 17649 { 17650 sata_hba_inst_t *sata_hba_inst = NULL; 17651 sata_address_t *saddr; 17652 sata_pmult_info_t *pmultinfo; 17653 sata_drive_info_t *sdinfo; 17654 sata_port_stats_t *pstats; 17655 sata_cport_info_t *cportinfo; 17656 sata_pmport_info_t *pmportinfo; 17657 int cport, pmport; 17658 char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1]; 17659 char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1]; 17660 char *lcp; 17661 static char *err_msg_evnt_1 = 17662 "sata_hba_event_notify: invalid port event 0x%x "; 17663 static char *err_msg_evnt_2 = 17664 "sata_hba_event_notify: invalid device event 0x%x "; 17665 int linkevent; 17666 17667 /* 17668 * There is a possibility that an event will be generated on HBA 17669 * that has not completed attachment or is detaching. We still want 17670 * to process events until HBA is detached. 17671 */ 17672 mutex_enter(&sata_mutex); 17673 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 17674 sata_hba_inst = sata_hba_inst->satahba_next) { 17675 if (SATA_DIP(sata_hba_inst) == dip) 17676 if (sata_hba_inst->satahba_attached == 1) 17677 break; 17678 } 17679 mutex_exit(&sata_mutex); 17680 if (sata_hba_inst == NULL) 17681 /* HBA not attached */ 17682 return; 17683 17684 ASSERT(sata_device != NULL); 17685 17686 /* 17687 * Validate address before - do not proceed with invalid address. 17688 */ 17689 saddr = &sata_device->satadev_addr; 17690 if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst)) 17691 return; 17692 17693 cport = saddr->cport; 17694 pmport = saddr->pmport; 17695 17696 buf1[0] = buf2[0] = '\0'; 17697 17698 /* 17699 * If event relates to port or device, check port state. 17700 * Port has to be initialized, or we cannot accept an event. 17701 */ 17702 if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT | 17703 SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT | SATA_ADDR_PMULT)) != 0) { 17704 mutex_enter(&sata_hba_inst->satahba_mutex); 17705 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 17706 mutex_exit(&sata_hba_inst->satahba_mutex); 17707 if (cportinfo == NULL || cportinfo->cport_state == 0) 17708 return; 17709 } 17710 17711 if ((saddr->qual & (SATA_ADDR_PMULT | SATA_ADDR_PMPORT | 17712 SATA_ADDR_DPMPORT)) != 0) { 17713 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 17714 SATA_LOG_D((sata_hba_inst, CE_WARN, 17715 "sata_hba_event_notify: Non-pmult device (0x%x)" 17716 "is attached to port %d, ignore pmult/pmport " 17717 "event 0x%x", cportinfo->cport_dev_type, 17718 cport, event)); 17719 return; 17720 } 17721 17722 mutex_enter(&cportinfo->cport_mutex); 17723 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 17724 mutex_exit(&cportinfo->cport_mutex); 17725 17726 /* 17727 * The daemon might be processing attachment of port 17728 * multiplier, in that case we should ignore events on its 17729 * sub-devices. 17730 * 17731 * NOTE: Only pmult_state is checked in sata_hba_event_notify. 17732 * The pmport_state is checked by sata daemon. 17733 */ 17734 if (pmultinfo == NULL || 17735 pmultinfo->pmult_state == SATA_STATE_UNKNOWN) { 17736 SATA_LOG_D((sata_hba_inst, CE_WARN, 17737 "sata_hba_event_notify: pmult is not" 17738 "available at port %d:%d, ignore event 0x%x", 17739 cport, pmport, event)); 17740 return; 17741 } 17742 } 17743 17744 if ((saddr->qual & 17745 (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) != 0) { 17746 17747 mutex_enter(&cportinfo->cport_mutex); 17748 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) { 17749 SATA_LOG_D((sata_hba_inst, CE_WARN, 17750 "sata_hba_event_notify: invalid/" 17751 "un-implemented port %d:%d (%d ports), " 17752 "ignore event 0x%x", cport, pmport, 17753 SATA_NUM_PMPORTS(sata_hba_inst, cport), event)); 17754 mutex_exit(&cportinfo->cport_mutex); 17755 return; 17756 } 17757 mutex_exit(&cportinfo->cport_mutex); 17758 17759 mutex_enter(&sata_hba_inst->satahba_mutex); 17760 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 17761 cport, pmport); 17762 mutex_exit(&sata_hba_inst->satahba_mutex); 17763 17764 /* pmport is implemented/valid? */ 17765 if (pmportinfo == NULL) { 17766 SATA_LOG_D((sata_hba_inst, CE_WARN, 17767 "sata_hba_event_notify: invalid/" 17768 "un-implemented port %d:%d, ignore " 17769 "event 0x%x", cport, pmport, event)); 17770 return; 17771 } 17772 } 17773 17774 /* 17775 * Events refer to devices, ports and controllers - each has 17776 * unique address. Events for different addresses cannot be combined. 17777 */ 17778 if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) { 17779 17780 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17781 17782 /* qualify this event(s) */ 17783 if ((event & SATA_EVNT_PORT_EVENTS) == 0) { 17784 /* Invalid event for the device port */ 17785 (void) sprintf(buf2, err_msg_evnt_1, 17786 event & SATA_EVNT_PORT_EVENTS); 17787 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17788 goto event_info; 17789 } 17790 if (saddr->qual == SATA_ADDR_CPORT) { 17791 /* Controller's device port event */ 17792 17793 (SATA_CPORT_INFO(sata_hba_inst, cport))-> 17794 cport_event_flags |= 17795 event & SATA_EVNT_PORT_EVENTS; 17796 pstats = 17797 &(SATA_CPORT_INFO(sata_hba_inst, cport))-> 17798 cport_stats; 17799 } else { 17800 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17801 mutex_enter(&pmportinfo->pmport_mutex); 17802 /* Port multiplier's device port event */ 17803 (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 17804 pmport_event_flags |= 17805 event & SATA_EVNT_PORT_EVENTS; 17806 pstats = 17807 &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 17808 pmport_stats; 17809 mutex_exit(&pmportinfo->pmport_mutex); 17810 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17811 } 17812 17813 /* 17814 * Add to statistics and log the message. We have to do it 17815 * here rather than in the event daemon, because there may be 17816 * multiple events occuring before they are processed. 17817 */ 17818 linkevent = event & 17819 (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED); 17820 if (linkevent) { 17821 if (linkevent == (SATA_EVNT_LINK_LOST | 17822 SATA_EVNT_LINK_ESTABLISHED)) { 17823 /* This is likely event combination */ 17824 (void) strlcat(buf1, "link lost/established, ", 17825 SATA_EVENT_MAX_MSG_LENGTH); 17826 17827 if (pstats->link_lost < 0xffffffffffffffffULL) 17828 pstats->link_lost++; 17829 if (pstats->link_established < 17830 0xffffffffffffffffULL) 17831 pstats->link_established++; 17832 linkevent = 0; 17833 } else if (linkevent & SATA_EVNT_LINK_LOST) { 17834 (void) strlcat(buf1, "link lost, ", 17835 SATA_EVENT_MAX_MSG_LENGTH); 17836 17837 if (pstats->link_lost < 0xffffffffffffffffULL) 17838 pstats->link_lost++; 17839 } else { 17840 (void) strlcat(buf1, "link established, ", 17841 SATA_EVENT_MAX_MSG_LENGTH); 17842 if (pstats->link_established < 17843 0xffffffffffffffffULL) 17844 pstats->link_established++; 17845 } 17846 } 17847 if (event & SATA_EVNT_DEVICE_ATTACHED) { 17848 (void) strlcat(buf1, "device attached, ", 17849 SATA_EVENT_MAX_MSG_LENGTH); 17850 if (pstats->device_attached < 0xffffffffffffffffULL) 17851 pstats->device_attached++; 17852 } 17853 if (event & SATA_EVNT_DEVICE_DETACHED) { 17854 (void) strlcat(buf1, "device detached, ", 17855 SATA_EVENT_MAX_MSG_LENGTH); 17856 if (pstats->device_detached < 0xffffffffffffffffULL) 17857 pstats->device_detached++; 17858 } 17859 if (event & SATA_EVNT_PWR_LEVEL_CHANGED) { 17860 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 17861 "port %d power level changed", cport); 17862 if (pstats->port_pwr_changed < 0xffffffffffffffffULL) 17863 pstats->port_pwr_changed++; 17864 } 17865 17866 if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) { 17867 /* There should be no other events for this address */ 17868 (void) sprintf(buf2, err_msg_evnt_1, 17869 event & ~SATA_EVNT_PORT_EVENTS); 17870 } 17871 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17872 17873 } else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) { 17874 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17875 17876 /* qualify this event */ 17877 if ((event & SATA_EVNT_DEVICE_RESET) == 0) { 17878 /* Invalid event for a device */ 17879 (void) sprintf(buf2, err_msg_evnt_2, 17880 event & SATA_EVNT_DEVICE_RESET); 17881 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17882 goto event_info; 17883 } 17884 /* drive event */ 17885 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 17886 if (sdinfo != NULL) { 17887 if (event & SATA_EVNT_DEVICE_RESET) { 17888 (void) strlcat(buf1, "device reset, ", 17889 SATA_EVENT_MAX_MSG_LENGTH); 17890 if (sdinfo->satadrv_stats.drive_reset < 17891 0xffffffffffffffffULL) 17892 sdinfo->satadrv_stats.drive_reset++; 17893 sdinfo->satadrv_event_flags |= 17894 SATA_EVNT_DEVICE_RESET; 17895 } 17896 } 17897 if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) { 17898 /* Invalid event for a device */ 17899 (void) sprintf(buf2, err_msg_evnt_2, 17900 event & ~SATA_EVNT_DRIVE_EVENTS); 17901 } 17902 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17903 } else if (saddr->qual == SATA_ADDR_PMULT) { 17904 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17905 17906 /* qualify this event */ 17907 if ((event & (SATA_EVNT_DEVICE_RESET | 17908 SATA_EVNT_PMULT_LINK_CHANGED)) == 0) { 17909 /* Invalid event for a port multiplier */ 17910 (void) sprintf(buf2, err_msg_evnt_2, 17911 event & SATA_EVNT_DEVICE_RESET); 17912 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17913 goto event_info; 17914 } 17915 17916 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 17917 17918 if (event & SATA_EVNT_DEVICE_RESET) { 17919 17920 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 17921 "[Reset] port-mult on cport %d", cport); 17922 pmultinfo->pmult_event_flags |= 17923 SATA_EVNT_DEVICE_RESET; 17924 (void) strlcat(buf1, "pmult reset, ", 17925 SATA_EVENT_MAX_MSG_LENGTH); 17926 } 17927 17928 if (event & SATA_EVNT_PMULT_LINK_CHANGED) { 17929 17930 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 17931 "pmult link changed on cport %d", cport); 17932 pmultinfo->pmult_event_flags |= 17933 SATA_EVNT_PMULT_LINK_CHANGED; 17934 (void) strlcat(buf1, "pmult link changed, ", 17935 SATA_EVENT_MAX_MSG_LENGTH); 17936 } 17937 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17938 17939 } else { 17940 if (saddr->qual != SATA_ADDR_NULL) { 17941 /* Wrong address qualifier */ 17942 SATA_LOG_D((sata_hba_inst, CE_WARN, 17943 "sata_hba_event_notify: invalid address 0x%x", 17944 *(uint32_t *)saddr)); 17945 return; 17946 } 17947 if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 || 17948 (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) { 17949 /* Invalid event for the controller */ 17950 SATA_LOG_D((sata_hba_inst, CE_WARN, 17951 "sata_hba_event_notify: invalid event 0x%x for " 17952 "controller", 17953 event & SATA_EVNT_CONTROLLER_EVENTS)); 17954 return; 17955 } 17956 buf1[0] = '\0'; 17957 /* This may be a frequent and not interesting event */ 17958 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 17959 "controller power level changed\n", NULL); 17960 17961 mutex_enter(&sata_hba_inst->satahba_mutex); 17962 if (sata_hba_inst->satahba_stats.ctrl_pwr_change < 17963 0xffffffffffffffffULL) 17964 sata_hba_inst->satahba_stats.ctrl_pwr_change++; 17965 17966 sata_hba_inst->satahba_event_flags |= 17967 SATA_EVNT_PWR_LEVEL_CHANGED; 17968 mutex_exit(&sata_hba_inst->satahba_mutex); 17969 } 17970 /* 17971 * If we got here, there is something to do with this HBA 17972 * instance. 17973 */ 17974 mutex_enter(&sata_hba_inst->satahba_mutex); 17975 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 17976 mutex_exit(&sata_hba_inst->satahba_mutex); 17977 mutex_enter(&sata_mutex); 17978 sata_event_pending |= SATA_EVNT_MAIN; /* global event indicator */ 17979 mutex_exit(&sata_mutex); 17980 17981 /* Tickle event thread */ 17982 mutex_enter(&sata_event_mutex); 17983 if (sata_event_thread_active == 0) 17984 cv_signal(&sata_event_cv); 17985 mutex_exit(&sata_event_mutex); 17986 17987 event_info: 17988 if (buf1[0] != '\0') { 17989 lcp = strrchr(buf1, ','); 17990 if (lcp != NULL) 17991 *lcp = '\0'; 17992 } 17993 if (saddr->qual == SATA_ADDR_CPORT || 17994 saddr->qual == SATA_ADDR_DCPORT) { 17995 if (buf1[0] != '\0') { 17996 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 17997 cport, buf1); 17998 } 17999 if (buf2[0] != '\0') { 18000 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 18001 cport, buf2); 18002 } 18003 } else if (saddr->qual == SATA_ADDR_PMPORT || 18004 saddr->qual == SATA_ADDR_DPMPORT) { 18005 if (buf1[0] != '\0') { 18006 sata_log(sata_hba_inst, CE_NOTE, 18007 "port %d pmport %d: %s\n", cport, pmport, buf1); 18008 } 18009 if (buf2[0] != '\0') { 18010 sata_log(sata_hba_inst, CE_NOTE, 18011 "port %d pmport %d: %s\n", cport, pmport, buf2); 18012 } 18013 } 18014 } 18015 18016 18017 /* 18018 * Event processing thread. 18019 * Arg is a pointer to the sata_hba_list pointer. 18020 * It is not really needed, because sata_hba_list is global and static 18021 */ 18022 static void 18023 sata_event_daemon(void *arg) 18024 { 18025 #ifndef __lock_lint 18026 _NOTE(ARGUNUSED(arg)) 18027 #endif 18028 sata_hba_inst_t *sata_hba_inst; 18029 clock_t delta; 18030 18031 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 18032 "SATA event daemon started\n", NULL); 18033 loop: 18034 /* 18035 * Process events here. Walk through all registered HBAs 18036 */ 18037 mutex_enter(&sata_mutex); 18038 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 18039 sata_hba_inst = sata_hba_inst->satahba_next) { 18040 ASSERT(sata_hba_inst != NULL); 18041 mutex_enter(&sata_hba_inst->satahba_mutex); 18042 if (sata_hba_inst->satahba_attached == 0 || 18043 (sata_hba_inst->satahba_event_flags & 18044 SATA_EVNT_SKIP) != 0) { 18045 mutex_exit(&sata_hba_inst->satahba_mutex); 18046 continue; 18047 } 18048 if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) { 18049 sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP; 18050 mutex_exit(&sata_hba_inst->satahba_mutex); 18051 mutex_exit(&sata_mutex); 18052 /* Got the controller with pending event */ 18053 sata_process_controller_events(sata_hba_inst); 18054 /* 18055 * Since global mutex was released, there is a 18056 * possibility that HBA list has changed, so start 18057 * over from the top. Just processed controller 18058 * will be passed-over because of the SKIP flag. 18059 */ 18060 goto loop; 18061 } 18062 mutex_exit(&sata_hba_inst->satahba_mutex); 18063 } 18064 /* Clear SKIP flag in all controllers */ 18065 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 18066 sata_hba_inst = sata_hba_inst->satahba_next) { 18067 mutex_enter(&sata_hba_inst->satahba_mutex); 18068 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP; 18069 mutex_exit(&sata_hba_inst->satahba_mutex); 18070 } 18071 mutex_exit(&sata_mutex); 18072 18073 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 18074 "SATA EVENT DAEMON suspending itself", NULL); 18075 18076 #ifdef SATA_DEBUG 18077 if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) { 18078 sata_log(sata_hba_inst, CE_WARN, 18079 "SATA EVENTS PROCESSING DISABLED\n"); 18080 thread_exit(); /* Daemon will not run again */ 18081 } 18082 #endif 18083 mutex_enter(&sata_event_mutex); 18084 sata_event_thread_active = 0; 18085 mutex_exit(&sata_event_mutex); 18086 /* 18087 * Go to sleep/suspend itself and wake up either because new event or 18088 * wait timeout. Exit if there is a termination request (driver 18089 * unload). 18090 */ 18091 delta = drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME); 18092 do { 18093 mutex_enter(&sata_event_mutex); 18094 (void) cv_reltimedwait(&sata_event_cv, &sata_event_mutex, 18095 delta, TR_CLOCK_TICK); 18096 18097 if (sata_event_thread_active != 0) { 18098 mutex_exit(&sata_event_mutex); 18099 continue; 18100 } 18101 18102 /* Check if it is time to go away */ 18103 if (sata_event_thread_terminate == 1) { 18104 /* 18105 * It is up to the thread setting above flag to make 18106 * sure that this thread is not killed prematurely. 18107 */ 18108 sata_event_thread_terminate = 0; 18109 sata_event_thread = NULL; 18110 mutex_exit(&sata_event_mutex); 18111 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 18112 "SATA_EVENT_DAEMON_TERMINATING", NULL); 18113 thread_exit(); { _NOTE(NOT_REACHED) } 18114 } 18115 mutex_exit(&sata_event_mutex); 18116 } while (!(sata_event_pending & SATA_EVNT_MAIN)); 18117 18118 mutex_enter(&sata_event_mutex); 18119 sata_event_thread_active = 1; 18120 mutex_exit(&sata_event_mutex); 18121 18122 mutex_enter(&sata_mutex); 18123 sata_event_pending &= ~SATA_EVNT_MAIN; 18124 mutex_exit(&sata_mutex); 18125 18126 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 18127 "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL); 18128 18129 goto loop; 18130 } 18131 18132 /* 18133 * Specific HBA instance event processing. 18134 * 18135 * NOTE: At the moment, device event processing is limited to hard disks 18136 * only. 18137 * Port multiplier is supported now. 18138 */ 18139 static void 18140 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst) 18141 { 18142 int ncport; 18143 uint32_t event_flags; 18144 sata_address_t *saddr; 18145 sata_cport_info_t *cportinfo; 18146 sata_pmult_info_t *pmultinfo; 18147 18148 SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst, 18149 "Processing controller %d event(s)", 18150 ddi_get_instance(SATA_DIP(sata_hba_inst))); 18151 18152 mutex_enter(&sata_hba_inst->satahba_mutex); 18153 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN; 18154 event_flags = sata_hba_inst->satahba_event_flags; 18155 mutex_exit(&sata_hba_inst->satahba_mutex); 18156 /* 18157 * Process controller power change first 18158 * HERE 18159 */ 18160 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) 18161 sata_process_cntrl_pwr_level_change(sata_hba_inst); 18162 18163 /* 18164 * Search through ports/devices to identify affected port/device. 18165 * We may have to process events for more than one port/device. 18166 */ 18167 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 18168 /* 18169 * Not all ports may be processed in attach by the time we 18170 * get an event. Check if port info is initialized. 18171 */ 18172 mutex_enter(&sata_hba_inst->satahba_mutex); 18173 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 18174 mutex_exit(&sata_hba_inst->satahba_mutex); 18175 if (cportinfo == NULL || cportinfo->cport_state == 0) 18176 continue; 18177 18178 /* We have initialized controller port info */ 18179 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 18180 event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 18181 cport_event_flags; 18182 /* Check if port was locked by IOCTL processing */ 18183 if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) { 18184 /* 18185 * We ignore port events because port is busy 18186 * with AP control processing. Set again 18187 * controller and main event flag, so that 18188 * events may be processed by the next daemon 18189 * run. 18190 */ 18191 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 18192 mutex_enter(&sata_hba_inst->satahba_mutex); 18193 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 18194 mutex_exit(&sata_hba_inst->satahba_mutex); 18195 mutex_enter(&sata_mutex); 18196 sata_event_pending |= SATA_EVNT_MAIN; 18197 mutex_exit(&sata_mutex); 18198 SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst, 18199 "Event processing postponed until " 18200 "AP control processing completes", 18201 NULL); 18202 /* Check other ports */ 18203 continue; 18204 } else { 18205 /* 18206 * Set BSY flag so that AP control would not 18207 * interfere with events processing for 18208 * this port. 18209 */ 18210 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 18211 cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY; 18212 } 18213 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 18214 18215 saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr; 18216 18217 if ((event_flags & 18218 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) { 18219 /* 18220 * Got port event. 18221 * We need some hierarchy of event processing as they 18222 * are affecting each other: 18223 * 1. port failed 18224 * 2. device detached/attached 18225 * 3. link events - link events may trigger device 18226 * detached or device attached events in some 18227 * circumstances. 18228 * 4. port power level changed 18229 */ 18230 if (event_flags & SATA_EVNT_PORT_FAILED) { 18231 sata_process_port_failed_event(sata_hba_inst, 18232 saddr); 18233 } 18234 if (event_flags & SATA_EVNT_DEVICE_DETACHED) { 18235 sata_process_device_detached(sata_hba_inst, 18236 saddr); 18237 } 18238 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) { 18239 sata_process_device_attached(sata_hba_inst, 18240 saddr); 18241 } 18242 if (event_flags & 18243 (SATA_EVNT_LINK_ESTABLISHED | 18244 SATA_EVNT_LINK_LOST)) { 18245 sata_process_port_link_events(sata_hba_inst, 18246 saddr); 18247 } 18248 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) { 18249 sata_process_port_pwr_change(sata_hba_inst, 18250 saddr); 18251 } 18252 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) { 18253 sata_process_target_node_cleanup( 18254 sata_hba_inst, saddr); 18255 } 18256 if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) { 18257 sata_process_device_autoonline( 18258 sata_hba_inst, saddr); 18259 } 18260 } 18261 18262 18263 /* 18264 * Scan port multiplier and all its sub-ports event flags. 18265 * The events are marked by 18266 * (1) sata_pmult_info.pmult_event_flags 18267 * (2) sata_pmport_info.pmport_event_flags 18268 */ 18269 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 18270 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 18271 /* 18272 * There should be another extra check: this 18273 * port multiplier still exists? 18274 */ 18275 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, 18276 ncport); 18277 18278 if (pmultinfo != NULL) { 18279 mutex_exit(&(SATA_CPORT_MUTEX( 18280 sata_hba_inst, ncport))); 18281 sata_process_pmult_events( 18282 sata_hba_inst, ncport); 18283 mutex_enter(&(SATA_CPORT_MUTEX( 18284 sata_hba_inst, ncport))); 18285 } else { 18286 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 18287 "Port-multiplier is gone. " 18288 "Ignore all sub-device events " 18289 "at port %d.", ncport); 18290 } 18291 } 18292 18293 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) != 18294 SATA_DTYPE_NONE) && 18295 (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) { 18296 if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)-> 18297 satadrv_event_flags & 18298 (SATA_EVNT_DEVICE_RESET | 18299 SATA_EVNT_INPROC_DEVICE_RESET)) { 18300 /* Have device event */ 18301 sata_process_device_reset(sata_hba_inst, 18302 saddr); 18303 } 18304 } 18305 /* Release PORT_BUSY flag */ 18306 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 18307 cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY; 18308 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 18309 18310 } /* End of loop through the controller SATA ports */ 18311 } 18312 18313 /* 18314 * Specific port multiplier instance event processing. At the moment, device 18315 * event processing is limited to link/attach event only. 18316 * 18317 * NOTE: power management event is not supported yet. 18318 */ 18319 static void 18320 sata_process_pmult_events(sata_hba_inst_t *sata_hba_inst, uint8_t cport) 18321 { 18322 sata_cport_info_t *cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 18323 sata_pmult_info_t *pmultinfo; 18324 sata_pmport_info_t *pmportinfo; 18325 sata_address_t *saddr; 18326 sata_device_t sata_device; 18327 uint32_t event_flags; 18328 int npmport; 18329 int rval; 18330 18331 SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst, 18332 "Processing pmult event(s) on cport %d of controller %d", 18333 cport, ddi_get_instance(SATA_DIP(sata_hba_inst))); 18334 18335 /* First process events on port multiplier */ 18336 mutex_enter(&cportinfo->cport_mutex); 18337 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 18338 event_flags = pmultinfo->pmult_event_flags; 18339 18340 /* 18341 * Reset event (of port multiplier) has higher priority because the 18342 * port multiplier itself might be failed or removed after reset. 18343 */ 18344 if (event_flags & SATA_EVNT_DEVICE_RESET) { 18345 /* 18346 * The status of the sub-links are uncertain, 18347 * so mark all sub-ports as RESET 18348 */ 18349 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 18350 sata_hba_inst, cport); npmport ++) { 18351 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 18352 cport, npmport); 18353 if (pmportinfo == NULL) { 18354 /* That's weird. */ 18355 SATA_LOG_D((sata_hba_inst, CE_WARN, 18356 "sata_hba_event_notify: " 18357 "invalid/un-implemented " 18358 "port %d:%d (%d ports), ", 18359 cport, npmport, SATA_NUM_PMPORTS( 18360 sata_hba_inst, cport))); 18361 continue; 18362 } 18363 18364 mutex_enter(&pmportinfo->pmport_mutex); 18365 18366 /* Mark all pmport to unknow state. */ 18367 pmportinfo->pmport_state = SATA_STATE_UNKNOWN; 18368 /* Mark all pmports with link events. */ 18369 pmportinfo->pmport_event_flags = 18370 (SATA_EVNT_LINK_ESTABLISHED|SATA_EVNT_LINK_LOST); 18371 mutex_exit(&pmportinfo->pmport_mutex); 18372 } 18373 18374 } else if (event_flags & SATA_EVNT_PMULT_LINK_CHANGED) { 18375 /* 18376 * We need probe the port multiplier to know what has 18377 * happened. 18378 */ 18379 bzero(&sata_device, sizeof (sata_device_t)); 18380 sata_device.satadev_rev = SATA_DEVICE_REV; 18381 sata_device.satadev_addr.cport = cport; 18382 sata_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT; 18383 sata_device.satadev_addr.qual = SATA_ADDR_PMULT; 18384 18385 mutex_exit(&cportinfo->cport_mutex); 18386 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18387 (SATA_DIP(sata_hba_inst), &sata_device); 18388 mutex_enter(&cportinfo->cport_mutex); 18389 if (rval != SATA_SUCCESS) { 18390 /* Something went wrong? Fail the port */ 18391 cportinfo->cport_state = SATA_PSTATE_FAILED; 18392 mutex_exit(&cportinfo->cport_mutex); 18393 SATA_LOG_D((sata_hba_inst, CE_WARN, 18394 "SATA port %d probing failed", cport)); 18395 18396 /* PMult structure must be released. */ 18397 sata_free_pmult(sata_hba_inst, &sata_device); 18398 return; 18399 } 18400 18401 sata_update_port_info(sata_hba_inst, &sata_device); 18402 18403 /* 18404 * Sanity check - Port is active? Is the link active? 18405 * The device is still a port multiplier? 18406 */ 18407 if ((cportinfo->cport_state & 18408 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 18409 ((cportinfo->cport_scr.sstatus & 18410 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) || 18411 (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) { 18412 mutex_exit(&cportinfo->cport_mutex); 18413 18414 /* PMult structure must be released. */ 18415 sata_free_pmult(sata_hba_inst, &sata_device); 18416 return; 18417 } 18418 18419 /* Probed succeed, set port ready. */ 18420 cportinfo->cport_state |= 18421 SATA_STATE_PROBED | SATA_STATE_READY; 18422 } 18423 18424 /* Release port multiplier event flags. */ 18425 pmultinfo->pmult_event_flags &= 18426 ~(SATA_EVNT_DEVICE_RESET|SATA_EVNT_PMULT_LINK_CHANGED); 18427 mutex_exit(&cportinfo->cport_mutex); 18428 18429 /* 18430 * Check all sub-links. 18431 */ 18432 for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, cport); 18433 npmport ++) { 18434 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport); 18435 mutex_enter(&pmportinfo->pmport_mutex); 18436 event_flags = pmportinfo->pmport_event_flags; 18437 mutex_exit(&pmportinfo->pmport_mutex); 18438 saddr = &pmportinfo->pmport_addr; 18439 18440 if ((event_flags & 18441 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) { 18442 /* 18443 * Got port multiplier port event. 18444 * We need some hierarchy of event processing as they 18445 * are affecting each other: 18446 * 1. device detached/attached 18447 * 2. link events - link events may trigger device 18448 * detached or device attached events in some 18449 * circumstances. 18450 */ 18451 if (event_flags & SATA_EVNT_DEVICE_DETACHED) { 18452 sata_process_pmdevice_detached(sata_hba_inst, 18453 saddr); 18454 } 18455 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) { 18456 sata_process_pmdevice_attached(sata_hba_inst, 18457 saddr); 18458 } 18459 if (event_flags & SATA_EVNT_LINK_ESTABLISHED || 18460 event_flags & SATA_EVNT_LINK_LOST) { 18461 sata_process_pmport_link_events(sata_hba_inst, 18462 saddr); 18463 } 18464 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) { 18465 sata_process_target_node_cleanup( 18466 sata_hba_inst, saddr); 18467 } 18468 } 18469 18470 /* Checking drive event(s). */ 18471 mutex_enter(&pmportinfo->pmport_mutex); 18472 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE && 18473 pmportinfo->pmport_sata_drive != NULL) { 18474 event_flags = pmportinfo->pmport_sata_drive-> 18475 satadrv_event_flags; 18476 if (event_flags & (SATA_EVNT_DEVICE_RESET | 18477 SATA_EVNT_INPROC_DEVICE_RESET)) { 18478 18479 /* Have device event */ 18480 sata_process_pmdevice_reset(sata_hba_inst, 18481 saddr); 18482 } 18483 } 18484 mutex_exit(&pmportinfo->pmport_mutex); 18485 18486 /* Release PORT_BUSY flag */ 18487 mutex_enter(&cportinfo->cport_mutex); 18488 cportinfo->cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY; 18489 mutex_exit(&cportinfo->cport_mutex); 18490 } 18491 18492 SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst, 18493 "[DONE] pmult event(s) on cport %d of controller %d", 18494 cport, ddi_get_instance(SATA_DIP(sata_hba_inst))); 18495 } 18496 18497 /* 18498 * Process HBA power level change reported by HBA driver. 18499 * Not implemented at this time - event is ignored. 18500 */ 18501 static void 18502 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst) 18503 { 18504 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18505 "Processing controller power level change", NULL); 18506 18507 /* Ignoring it for now */ 18508 mutex_enter(&sata_hba_inst->satahba_mutex); 18509 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 18510 mutex_exit(&sata_hba_inst->satahba_mutex); 18511 } 18512 18513 /* 18514 * Process port power level change reported by HBA driver. 18515 * Not implemented at this time - event is ignored. 18516 */ 18517 static void 18518 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst, 18519 sata_address_t *saddr) 18520 { 18521 sata_cport_info_t *cportinfo; 18522 18523 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18524 "Processing port power level change", NULL); 18525 18526 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 18527 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18528 /* Reset event flag */ 18529 cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 18530 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18531 } 18532 18533 /* 18534 * Process port failure reported by HBA driver. 18535 * cports support only - no pmports. 18536 */ 18537 static void 18538 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst, 18539 sata_address_t *saddr) 18540 { 18541 sata_cport_info_t *cportinfo; 18542 18543 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 18544 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18545 /* Reset event flag first */ 18546 cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED; 18547 /* If the port is in SHUTDOWN or FAILED state, ignore this event. */ 18548 if ((cportinfo->cport_state & 18549 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) { 18550 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18551 cport_mutex); 18552 return; 18553 } 18554 /* Fail the port */ 18555 cportinfo->cport_state = SATA_PSTATE_FAILED; 18556 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18557 sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport); 18558 } 18559 18560 /* 18561 * Device Reset Event processing. 18562 * The sequence is managed by 3 stage flags: 18563 * - reset event reported, 18564 * - reset event being processed, 18565 * - request to clear device reset state. 18566 * 18567 * NOTE: This function has to be entered with cport mutex held. It exits with 18568 * mutex held as well, but can release mutex during the processing. 18569 */ 18570 static void 18571 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst, 18572 sata_address_t *saddr) 18573 { 18574 sata_drive_info_t old_sdinfo; /* local copy of the drive info */ 18575 sata_drive_info_t *sdinfo; 18576 sata_cport_info_t *cportinfo; 18577 sata_device_t sata_device; 18578 int rval_probe, rval_set; 18579 18580 /* We only care about host sata cport for now */ 18581 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 18582 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 18583 /* 18584 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED 18585 * state, ignore reset event. 18586 */ 18587 if (((cportinfo->cport_state & 18588 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) || 18589 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 18590 sdinfo->satadrv_event_flags &= 18591 ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET); 18592 return; 18593 } 18594 18595 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) == 18596 SATA_DTYPE_PMULT)) { 18597 /* 18598 * Should not happened: this is already handled in 18599 * sata_hba_event_notify() 18600 */ 18601 mutex_exit(&cportinfo->cport_mutex); 18602 goto done; 18603 } 18604 18605 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) & 18606 SATA_VALID_DEV_TYPE) == 0) { 18607 /* 18608 * This should not happen - coding error. 18609 * But we can recover, so do not panic, just clean up 18610 * and if in debug mode, log the message. 18611 */ 18612 #ifdef SATA_DEBUG 18613 sata_log(sata_hba_inst, CE_WARN, 18614 "sata_process_device_reset: " 18615 "Invalid device type with sdinfo!", NULL); 18616 #endif 18617 sdinfo->satadrv_event_flags = 0; 18618 return; 18619 } 18620 18621 #ifdef SATA_DEBUG 18622 if ((sdinfo->satadrv_event_flags & 18623 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) { 18624 /* Nothing to do */ 18625 /* Something is weird - why we are processing dev reset? */ 18626 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18627 "No device reset event!!!!", NULL); 18628 18629 return; 18630 } 18631 if ((sdinfo->satadrv_event_flags & 18632 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 18633 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 18634 /* Something is weird - new device reset event */ 18635 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18636 "Overlapping device reset events!", NULL); 18637 } 18638 #endif 18639 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18640 "Processing port %d device reset", saddr->cport); 18641 18642 /* Clear event flag */ 18643 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET; 18644 18645 /* It seems that we always need to check the port state first */ 18646 sata_device.satadev_rev = SATA_DEVICE_REV; 18647 sata_device.satadev_addr = *saddr; 18648 /* 18649 * We have to exit mutex, because the HBA probe port function may 18650 * block on its own mutex. 18651 */ 18652 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18653 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18654 (SATA_DIP(sata_hba_inst), &sata_device); 18655 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18656 sata_update_port_info(sata_hba_inst, &sata_device); 18657 if (rval_probe != SATA_SUCCESS) { 18658 /* Something went wrong? Fail the port */ 18659 cportinfo->cport_state = SATA_PSTATE_FAILED; 18660 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 18661 if (sdinfo != NULL) 18662 sdinfo->satadrv_event_flags = 0; 18663 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18664 cport_mutex); 18665 SATA_LOG_D((sata_hba_inst, CE_WARN, 18666 "SATA port %d probing failed", 18667 saddr->cport)); 18668 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 18669 saddr->cport)->cport_mutex); 18670 return; 18671 } 18672 if ((sata_device.satadev_scr.sstatus & 18673 SATA_PORT_DEVLINK_UP_MASK) != 18674 SATA_PORT_DEVLINK_UP || 18675 sata_device.satadev_type == SATA_DTYPE_NONE) { 18676 /* 18677 * No device to process, anymore. Some other event processing 18678 * would or have already performed port info cleanup. 18679 * To be safe (HBA may need it), request clearing device 18680 * reset condition. 18681 */ 18682 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 18683 if (sdinfo != NULL) { 18684 sdinfo->satadrv_event_flags &= 18685 ~SATA_EVNT_INPROC_DEVICE_RESET; 18686 sdinfo->satadrv_event_flags |= 18687 SATA_EVNT_CLEAR_DEVICE_RESET; 18688 } 18689 return; 18690 } 18691 18692 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 18693 if (sdinfo == NULL) { 18694 return; 18695 } 18696 if ((sdinfo->satadrv_event_flags & 18697 SATA_EVNT_INPROC_DEVICE_RESET) == 0) { 18698 /* 18699 * Start tracking time for device feature restoration and 18700 * identification. Save current time (lbolt value). 18701 */ 18702 sdinfo->satadrv_reset_time = ddi_get_lbolt(); 18703 } 18704 /* Mark device reset processing as active */ 18705 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET; 18706 18707 old_sdinfo = *sdinfo; /* local copy of the drive info */ 18708 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18709 18710 rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1); 18711 18712 if (rval_set != SATA_SUCCESS) { 18713 /* 18714 * Restoring drive setting failed. 18715 * Probe the port first, to check if the port state has changed 18716 */ 18717 sata_device.satadev_rev = SATA_DEVICE_REV; 18718 sata_device.satadev_addr = *saddr; 18719 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 18720 /* probe port */ 18721 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18722 (SATA_DIP(sata_hba_inst), &sata_device); 18723 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18724 cport_mutex); 18725 if (rval_probe == SATA_SUCCESS && 18726 (sata_device.satadev_state & 18727 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 && 18728 (sata_device.satadev_scr.sstatus & 18729 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP && 18730 sata_device.satadev_type != SATA_DTYPE_NONE) { 18731 /* 18732 * We may retry this a bit later - in-process reset 18733 * condition should be already set. 18734 * Track retry time for device identification. 18735 */ 18736 if ((cportinfo->cport_dev_type & 18737 SATA_VALID_DEV_TYPE) != 0 && 18738 SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL && 18739 sdinfo->satadrv_reset_time != 0) { 18740 clock_t cur_time = ddi_get_lbolt(); 18741 /* 18742 * If the retry time limit was not 18743 * exceeded, retry. 18744 */ 18745 if ((cur_time - sdinfo->satadrv_reset_time) < 18746 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 18747 mutex_enter( 18748 &sata_hba_inst->satahba_mutex); 18749 sata_hba_inst->satahba_event_flags |= 18750 SATA_EVNT_MAIN; 18751 mutex_exit( 18752 &sata_hba_inst->satahba_mutex); 18753 mutex_enter(&sata_mutex); 18754 sata_event_pending |= SATA_EVNT_MAIN; 18755 mutex_exit(&sata_mutex); 18756 return; 18757 } 18758 if (rval_set == SATA_RETRY) { 18759 /* 18760 * Setting drive features failed, but 18761 * the drive is still accessible, 18762 * so emit a warning message before 18763 * return. 18764 */ 18765 mutex_exit(&SATA_CPORT_INFO( 18766 sata_hba_inst, 18767 saddr->cport)->cport_mutex); 18768 goto done; 18769 } 18770 } 18771 /* Fail the drive */ 18772 sdinfo->satadrv_state = SATA_DSTATE_FAILED; 18773 18774 sata_log(sata_hba_inst, CE_WARN, 18775 "SATA device at port %d - device failed", 18776 saddr->cport); 18777 18778 DTRACE_PROBE(port_failed_f); 18779 } 18780 /* 18781 * No point of retrying - device failed or some other event 18782 * processing or already did or will do port info cleanup. 18783 * To be safe (HBA may need it), 18784 * request clearing device reset condition. 18785 */ 18786 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 18787 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET; 18788 sdinfo->satadrv_reset_time = 0; 18789 return; 18790 } 18791 done: 18792 /* 18793 * If setting of drive features failed, but the drive is still 18794 * accessible, emit a warning message. 18795 */ 18796 if (rval_set == SATA_RETRY) { 18797 sata_log(sata_hba_inst, CE_WARN, 18798 "SATA device at port %d - desired setting could not be " 18799 "restored after reset. Device may not operate as expected.", 18800 saddr->cport); 18801 } 18802 /* 18803 * Raise the flag indicating that the next sata command could 18804 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device 18805 * reset is reported. 18806 */ 18807 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18808 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 18809 sdinfo->satadrv_reset_time = 0; 18810 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) { 18811 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 18812 sdinfo->satadrv_event_flags &= 18813 ~SATA_EVNT_INPROC_DEVICE_RESET; 18814 sdinfo->satadrv_event_flags |= 18815 SATA_EVNT_CLEAR_DEVICE_RESET; 18816 } 18817 } 18818 } 18819 18820 18821 /* 18822 * Port Multiplier Port Device Reset Event processing. 18823 * 18824 * NOTE: This function has to be entered with pmport mutex held. It exits with 18825 * mutex held as well, but can release mutex during the processing. 18826 */ 18827 static void 18828 sata_process_pmdevice_reset(sata_hba_inst_t *sata_hba_inst, 18829 sata_address_t *saddr) 18830 { 18831 sata_drive_info_t old_sdinfo; /* local copy of the drive info */ 18832 sata_drive_info_t *sdinfo = NULL; 18833 sata_cport_info_t *cportinfo = NULL; 18834 sata_pmport_info_t *pmportinfo = NULL; 18835 sata_pmult_info_t *pminfo = NULL; 18836 sata_device_t sata_device; 18837 uint8_t cport = saddr->cport; 18838 uint8_t pmport = saddr->pmport; 18839 int rval; 18840 18841 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18842 "Processing drive reset at port %d:%d", cport, pmport); 18843 18844 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 18845 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 18846 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport); 18847 18848 /* 18849 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED 18850 * state, ignore reset event. 18851 */ 18852 if (((cportinfo->cport_state & 18853 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) || 18854 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 18855 sdinfo->satadrv_event_flags &= 18856 ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET); 18857 return; 18858 } 18859 18860 if ((pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) == 0) { 18861 /* 18862 * This should not happen - coding error. 18863 * But we can recover, so do not panic, just clean up 18864 * and if in debug mode, log the message. 18865 */ 18866 #ifdef SATA_DEBUG 18867 sata_log(sata_hba_inst, CE_WARN, 18868 "sata_process_pmdevice_reset: " 18869 "Invalid device type with sdinfo!", NULL); 18870 #endif 18871 sdinfo->satadrv_event_flags = 0; 18872 return; 18873 } 18874 18875 #ifdef SATA_DEBUG 18876 if ((sdinfo->satadrv_event_flags & 18877 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) { 18878 /* Nothing to do */ 18879 /* Something is weird - why we are processing dev reset? */ 18880 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18881 "No device reset event!!!!", NULL); 18882 18883 return; 18884 } 18885 if ((sdinfo->satadrv_event_flags & 18886 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 18887 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 18888 /* Something is weird - new device reset event */ 18889 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18890 "Overlapping device reset events!", NULL); 18891 } 18892 #endif 18893 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18894 "Processing port %d:%d device reset", cport, pmport); 18895 18896 /* Clear event flag */ 18897 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET; 18898 18899 /* It seems that we always need to check the port state first */ 18900 sata_device.satadev_rev = SATA_DEVICE_REV; 18901 sata_device.satadev_addr = *saddr; 18902 /* 18903 * We have to exit mutex, because the HBA probe port function may 18904 * block on its own mutex. 18905 */ 18906 mutex_exit(&pmportinfo->pmport_mutex); 18907 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18908 (SATA_DIP(sata_hba_inst), &sata_device); 18909 mutex_enter(&pmportinfo->pmport_mutex); 18910 18911 sata_update_pmport_info(sata_hba_inst, &sata_device); 18912 if (rval != SATA_SUCCESS) { 18913 /* Something went wrong? Fail the port */ 18914 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 18915 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport, 18916 saddr->pmport); 18917 if (sdinfo != NULL) 18918 sdinfo->satadrv_event_flags = 0; 18919 mutex_exit(&pmportinfo->pmport_mutex); 18920 SATA_LOG_D((sata_hba_inst, CE_WARN, 18921 "SATA port %d:%d probing failed", 18922 saddr->cport, saddr->pmport)); 18923 mutex_enter(&pmportinfo->pmport_mutex); 18924 return; 18925 } 18926 if ((sata_device.satadev_scr.sstatus & 18927 SATA_PORT_DEVLINK_UP_MASK) != 18928 SATA_PORT_DEVLINK_UP || 18929 sata_device.satadev_type == SATA_DTYPE_NONE) { 18930 /* 18931 * No device to process, anymore. Some other event processing 18932 * would or have already performed port info cleanup. 18933 * To be safe (HBA may need it), request clearing device 18934 * reset condition. 18935 */ 18936 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport, 18937 saddr->pmport); 18938 if (sdinfo != NULL) { 18939 sdinfo->satadrv_event_flags &= 18940 ~SATA_EVNT_INPROC_DEVICE_RESET; 18941 /* must clear flags on cport */ 18942 pminfo = SATA_PMULT_INFO(sata_hba_inst, 18943 saddr->cport); 18944 pminfo->pmult_event_flags |= 18945 SATA_EVNT_CLEAR_DEVICE_RESET; 18946 } 18947 return; 18948 } 18949 18950 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport, 18951 saddr->pmport); 18952 if (sdinfo == NULL) { 18953 return; 18954 } 18955 if ((sdinfo->satadrv_event_flags & 18956 SATA_EVNT_INPROC_DEVICE_RESET) == 0) { 18957 /* 18958 * Start tracking time for device feature restoration and 18959 * identification. Save current time (lbolt value). 18960 */ 18961 sdinfo->satadrv_reset_time = ddi_get_lbolt(); 18962 } 18963 /* Mark device reset processing as active */ 18964 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET; 18965 18966 old_sdinfo = *sdinfo; /* local copy of the drive info */ 18967 mutex_exit(&pmportinfo->pmport_mutex); 18968 18969 if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) == 18970 SATA_FAILURE) { 18971 /* 18972 * Restoring drive setting failed. 18973 * Probe the port first, to check if the port state has changed 18974 */ 18975 sata_device.satadev_rev = SATA_DEVICE_REV; 18976 sata_device.satadev_addr = *saddr; 18977 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT; 18978 18979 /* probe port */ 18980 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18981 (SATA_DIP(sata_hba_inst), &sata_device); 18982 mutex_enter(&pmportinfo->pmport_mutex); 18983 if (rval == SATA_SUCCESS && 18984 (sata_device.satadev_state & 18985 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 && 18986 (sata_device.satadev_scr.sstatus & 18987 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP && 18988 sata_device.satadev_type != SATA_DTYPE_NONE) { 18989 /* 18990 * We may retry this a bit later - in-process reset 18991 * condition should be already set. 18992 * Track retry time for device identification. 18993 */ 18994 if ((pmportinfo->pmport_dev_type & 18995 SATA_VALID_DEV_TYPE) != 0 && 18996 SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL && 18997 sdinfo->satadrv_reset_time != 0) { 18998 clock_t cur_time = ddi_get_lbolt(); 18999 /* 19000 * If the retry time limit was not 19001 * exceeded, retry. 19002 */ 19003 if ((cur_time - sdinfo->satadrv_reset_time) < 19004 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 19005 mutex_enter( 19006 &sata_hba_inst->satahba_mutex); 19007 sata_hba_inst->satahba_event_flags |= 19008 SATA_EVNT_MAIN; 19009 mutex_exit( 19010 &sata_hba_inst->satahba_mutex); 19011 mutex_enter(&sata_mutex); 19012 sata_event_pending |= SATA_EVNT_MAIN; 19013 mutex_exit(&sata_mutex); 19014 return; 19015 } 19016 } 19017 /* Fail the drive */ 19018 sdinfo->satadrv_state = SATA_DSTATE_FAILED; 19019 19020 sata_log(sata_hba_inst, CE_WARN, 19021 "SATA device at port %d:%d - device failed", 19022 saddr->cport, saddr->pmport); 19023 } else { 19024 /* 19025 * No point of retrying - some other event processing 19026 * would or already did port info cleanup. 19027 * To be safe (HBA may need it), 19028 * request clearing device reset condition. 19029 */ 19030 sdinfo->satadrv_event_flags |= 19031 SATA_EVNT_CLEAR_DEVICE_RESET; 19032 } 19033 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET; 19034 sdinfo->satadrv_reset_time = 0; 19035 return; 19036 } 19037 /* 19038 * Raise the flag indicating that the next sata command could 19039 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device 19040 * reset is reported. 19041 */ 19042 mutex_enter(&pmportinfo->pmport_mutex); 19043 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 19044 sdinfo->satadrv_reset_time = 0; 19045 if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) { 19046 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 19047 sdinfo->satadrv_event_flags &= 19048 ~SATA_EVNT_INPROC_DEVICE_RESET; 19049 /* must clear flags on cport */ 19050 pminfo = SATA_PMULT_INFO(sata_hba_inst, 19051 saddr->cport); 19052 pminfo->pmult_event_flags |= 19053 SATA_EVNT_CLEAR_DEVICE_RESET; 19054 } 19055 } 19056 } 19057 19058 /* 19059 * Port Link Events processing. 19060 * Every link established event may involve device reset (due to 19061 * COMRESET signal, equivalent of the hard reset) so arbitrarily 19062 * set device reset event for an attached device (if any). 19063 * If the port is in SHUTDOWN or FAILED state, ignore link events. 19064 * 19065 * The link established event processing varies, depending on the state 19066 * of the target node, HBA hotplugging capabilities, state of the port. 19067 * If the link is not active, the link established event is ignored. 19068 * If HBA cannot detect device attachment and there is no target node, 19069 * the link established event triggers device attach event processing. 19070 * Else, link established event triggers device reset event processing. 19071 * 19072 * The link lost event processing varies, depending on a HBA hotplugging 19073 * capability and the state of the port (link active or not active). 19074 * If the link is active, the lost link event is ignored. 19075 * If HBA cannot detect device removal, the lost link event triggers 19076 * device detached event processing after link lost timeout. 19077 * Else, the event is ignored. 19078 * 19079 * NOTE: Port multiplier ports events are handled by 19080 * sata_process_pmport_link_events(); 19081 */ 19082 static void 19083 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst, 19084 sata_address_t *saddr) 19085 { 19086 sata_device_t sata_device; 19087 sata_cport_info_t *cportinfo; 19088 sata_drive_info_t *sdinfo; 19089 uint32_t event_flags; 19090 int rval; 19091 19092 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19093 "Processing port %d link event(s)", saddr->cport); 19094 19095 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 19096 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19097 event_flags = cportinfo->cport_event_flags; 19098 19099 /* Reset event flags first */ 19100 cportinfo->cport_event_flags &= 19101 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST); 19102 19103 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */ 19104 if ((cportinfo->cport_state & 19105 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 19106 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19107 cport_mutex); 19108 return; 19109 } 19110 19111 /* 19112 * For the sanity sake get current port state. 19113 * Set device address only. Other sata_device fields should be 19114 * set by HBA driver. 19115 */ 19116 sata_device.satadev_rev = SATA_DEVICE_REV; 19117 sata_device.satadev_addr = *saddr; 19118 /* 19119 * We have to exit mutex, because the HBA probe port function may 19120 * block on its own mutex. 19121 */ 19122 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19123 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 19124 (SATA_DIP(sata_hba_inst), &sata_device); 19125 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19126 sata_update_port_info(sata_hba_inst, &sata_device); 19127 if (rval != SATA_SUCCESS) { 19128 /* Something went wrong? Fail the port */ 19129 cportinfo->cport_state = SATA_PSTATE_FAILED; 19130 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19131 cport_mutex); 19132 SATA_LOG_D((sata_hba_inst, CE_WARN, 19133 "SATA port %d probing failed", 19134 saddr->cport)); 19135 /* 19136 * We may want to release device info structure, but 19137 * it is not necessary. 19138 */ 19139 return; 19140 } else { 19141 /* port probed successfully */ 19142 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 19143 } 19144 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) { 19145 19146 if ((sata_device.satadev_scr.sstatus & 19147 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) { 19148 /* Ignore event */ 19149 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19150 "Ignoring port %d link established event - " 19151 "link down", 19152 saddr->cport); 19153 goto linklost; 19154 } 19155 19156 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19157 "Processing port %d link established event", 19158 saddr->cport); 19159 19160 /* 19161 * For the sanity sake check if a device is attached - check 19162 * return state of a port probing. 19163 */ 19164 if (sata_device.satadev_type != SATA_DTYPE_NONE) { 19165 /* 19166 * HBA port probe indicated that there is a device 19167 * attached. Check if the framework had device info 19168 * structure attached for this device. 19169 */ 19170 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 19171 ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) != 19172 NULL); 19173 19174 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 19175 if ((sdinfo->satadrv_type & 19176 SATA_VALID_DEV_TYPE) != 0) { 19177 /* 19178 * Dev info structure is present. 19179 * If dev_type is set to known type in 19180 * the framework's drive info struct 19181 * then the device existed before and 19182 * the link was probably lost 19183 * momentarily - in such case 19184 * we may want to check device 19185 * identity. 19186 * Identity check is not supported now. 19187 * 19188 * Link established event 19189 * triggers device reset event. 19190 */ 19191 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 19192 satadrv_event_flags |= 19193 SATA_EVNT_DEVICE_RESET; 19194 } 19195 } else if (cportinfo->cport_dev_type == 19196 SATA_DTYPE_NONE) { 19197 /* 19198 * We got new device attached! If HBA does not 19199 * generate device attached events, trigger it 19200 * here. 19201 */ 19202 if (!(SATA_FEATURES(sata_hba_inst) & 19203 SATA_CTLF_HOTPLUG)) { 19204 cportinfo->cport_event_flags |= 19205 SATA_EVNT_DEVICE_ATTACHED; 19206 } 19207 } 19208 /* Reset link lost timeout */ 19209 cportinfo->cport_link_lost_time = 0; 19210 } 19211 } 19212 linklost: 19213 if (event_flags & SATA_EVNT_LINK_LOST) { 19214 if ((sata_device.satadev_scr.sstatus & 19215 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) { 19216 /* Ignore event */ 19217 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19218 "Ignoring port %d link lost event - link is up", 19219 saddr->cport); 19220 goto done; 19221 } 19222 #ifdef SATA_DEBUG 19223 if (cportinfo->cport_link_lost_time == 0) { 19224 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19225 "Processing port %d link lost event", 19226 saddr->cport); 19227 } 19228 #endif 19229 /* 19230 * When HBA cannot generate device attached/detached events, 19231 * we need to track link lost time and eventually generate 19232 * device detach event. 19233 */ 19234 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) { 19235 /* We are tracking link lost time */ 19236 if (cportinfo->cport_link_lost_time == 0) { 19237 /* save current time (lbolt value) */ 19238 cportinfo->cport_link_lost_time = 19239 ddi_get_lbolt(); 19240 /* just keep link lost event */ 19241 cportinfo->cport_event_flags |= 19242 SATA_EVNT_LINK_LOST; 19243 } else { 19244 clock_t cur_time = ddi_get_lbolt(); 19245 if ((cur_time - 19246 cportinfo->cport_link_lost_time) >= 19247 drv_usectohz( 19248 SATA_EVNT_LINK_LOST_TIMEOUT)) { 19249 /* trigger device detach event */ 19250 cportinfo->cport_event_flags |= 19251 SATA_EVNT_DEVICE_DETACHED; 19252 cportinfo->cport_link_lost_time = 0; 19253 SATADBG1(SATA_DBG_EVENTS, 19254 sata_hba_inst, 19255 "Triggering port %d " 19256 "device detached event", 19257 saddr->cport); 19258 } else { 19259 /* keep link lost event */ 19260 cportinfo->cport_event_flags |= 19261 SATA_EVNT_LINK_LOST; 19262 } 19263 } 19264 } 19265 /* 19266 * We could change port state to disable/delay access to 19267 * the attached device until the link is recovered. 19268 */ 19269 } 19270 done: 19271 event_flags = cportinfo->cport_event_flags; 19272 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19273 if (event_flags != 0) { 19274 mutex_enter(&sata_hba_inst->satahba_mutex); 19275 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 19276 mutex_exit(&sata_hba_inst->satahba_mutex); 19277 mutex_enter(&sata_mutex); 19278 sata_event_pending |= SATA_EVNT_MAIN; 19279 mutex_exit(&sata_mutex); 19280 } 19281 } 19282 19283 /* 19284 * Port Multiplier Port Link Events processing. 19285 */ 19286 static void 19287 sata_process_pmport_link_events(sata_hba_inst_t *sata_hba_inst, 19288 sata_address_t *saddr) 19289 { 19290 sata_device_t sata_device; 19291 sata_pmport_info_t *pmportinfo = NULL; 19292 sata_drive_info_t *sdinfo = NULL; 19293 uint32_t event_flags; 19294 uint8_t cport = saddr->cport; 19295 uint8_t pmport = saddr->pmport; 19296 int rval; 19297 19298 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19299 "Processing port %d:%d link event(s)", 19300 cport, pmport); 19301 19302 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 19303 mutex_enter(&pmportinfo->pmport_mutex); 19304 event_flags = pmportinfo->pmport_event_flags; 19305 19306 /* Reset event flags first */ 19307 pmportinfo->pmport_event_flags &= 19308 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST); 19309 19310 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */ 19311 if ((pmportinfo->pmport_state & 19312 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 19313 mutex_exit(&pmportinfo->pmport_mutex); 19314 return; 19315 } 19316 19317 /* 19318 * For the sanity sake get current port state. 19319 * Set device address only. Other sata_device fields should be 19320 * set by HBA driver. 19321 */ 19322 sata_device.satadev_rev = SATA_DEVICE_REV; 19323 sata_device.satadev_addr = *saddr; 19324 /* 19325 * We have to exit mutex, because the HBA probe port function may 19326 * block on its own mutex. 19327 */ 19328 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 19329 saddr->pmport)); 19330 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 19331 (SATA_DIP(sata_hba_inst), &sata_device); 19332 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 19333 saddr->pmport)); 19334 sata_update_pmport_info(sata_hba_inst, &sata_device); 19335 if (rval != SATA_SUCCESS) { 19336 /* Something went wrong? Fail the port */ 19337 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 19338 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 19339 saddr->pmport)); 19340 SATA_LOG_D((sata_hba_inst, CE_WARN, 19341 "SATA port %d:%d probing failed", 19342 saddr->cport, saddr->pmport)); 19343 /* 19344 * We may want to release device info structure, but 19345 * it is not necessary. 19346 */ 19347 return; 19348 } else { 19349 /* port probed successfully */ 19350 pmportinfo->pmport_state |= 19351 SATA_STATE_PROBED | SATA_STATE_READY; 19352 } 19353 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, 19354 saddr->cport, saddr->pmport)); 19355 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, 19356 saddr->cport, saddr->pmport)); 19357 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) { 19358 19359 if ((sata_device.satadev_scr.sstatus & 19360 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) { 19361 /* Ignore event */ 19362 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19363 "Ignoring port %d:%d link established event - " 19364 "link down", 19365 saddr->cport, saddr->pmport); 19366 goto linklost; 19367 } 19368 19369 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19370 "Processing port %d:%d link established event", 19371 cport, pmport); 19372 19373 /* 19374 * For the sanity sake check if a device is attached - check 19375 * return state of a port probing. 19376 */ 19377 if (sata_device.satadev_type != SATA_DTYPE_NONE && 19378 sata_device.satadev_type != SATA_DTYPE_PMULT) { 19379 /* 19380 * HBA port probe indicated that there is a device 19381 * attached. Check if the framework had device info 19382 * structure attached for this device. 19383 */ 19384 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 19385 ASSERT(SATA_PMPORTINFO_DRV_INFO(pmportinfo) != 19386 NULL); 19387 19388 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 19389 if ((sdinfo->satadrv_type & 19390 SATA_VALID_DEV_TYPE) != 0) { 19391 /* 19392 * Dev info structure is present. 19393 * If dev_type is set to known type in 19394 * the framework's drive info struct 19395 * then the device existed before and 19396 * the link was probably lost 19397 * momentarily - in such case 19398 * we may want to check device 19399 * identity. 19400 * Identity check is not supported now. 19401 * 19402 * Link established event 19403 * triggers device reset event. 19404 */ 19405 (SATA_PMPORTINFO_DRV_INFO(pmportinfo))-> 19406 satadrv_event_flags |= 19407 SATA_EVNT_DEVICE_RESET; 19408 } 19409 } else if (pmportinfo->pmport_dev_type == 19410 SATA_DTYPE_NONE) { 19411 /* 19412 * We got new device attached! If HBA does not 19413 * generate device attached events, trigger it 19414 * here. 19415 */ 19416 if (!(SATA_FEATURES(sata_hba_inst) & 19417 SATA_CTLF_HOTPLUG)) { 19418 pmportinfo->pmport_event_flags |= 19419 SATA_EVNT_DEVICE_ATTACHED; 19420 } 19421 } 19422 /* Reset link lost timeout */ 19423 pmportinfo->pmport_link_lost_time = 0; 19424 } 19425 } 19426 linklost: 19427 if (event_flags & SATA_EVNT_LINK_LOST) { 19428 #ifdef SATA_DEBUG 19429 if (pmportinfo->pmport_link_lost_time == 0) { 19430 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19431 "Processing port %d:%d link lost event", 19432 saddr->cport, saddr->pmport); 19433 } 19434 #endif 19435 if ((sata_device.satadev_scr.sstatus & 19436 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) { 19437 /* Ignore event */ 19438 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19439 "Ignoring port %d:%d link lost event - link is up", 19440 saddr->cport, saddr->pmport); 19441 goto done; 19442 } 19443 /* 19444 * When HBA cannot generate device attached/detached events, 19445 * we need to track link lost time and eventually generate 19446 * device detach event. 19447 */ 19448 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) { 19449 /* We are tracking link lost time */ 19450 if (pmportinfo->pmport_link_lost_time == 0) { 19451 /* save current time (lbolt value) */ 19452 pmportinfo->pmport_link_lost_time = 19453 ddi_get_lbolt(); 19454 /* just keep link lost event */ 19455 pmportinfo->pmport_event_flags |= 19456 SATA_EVNT_LINK_LOST; 19457 } else { 19458 clock_t cur_time = ddi_get_lbolt(); 19459 if ((cur_time - 19460 pmportinfo->pmport_link_lost_time) >= 19461 drv_usectohz( 19462 SATA_EVNT_LINK_LOST_TIMEOUT)) { 19463 /* trigger device detach event */ 19464 pmportinfo->pmport_event_flags |= 19465 SATA_EVNT_DEVICE_DETACHED; 19466 pmportinfo->pmport_link_lost_time = 0; 19467 SATADBG2(SATA_DBG_EVENTS, 19468 sata_hba_inst, 19469 "Triggering port %d:%d " 19470 "device detached event", 19471 saddr->cport, saddr->pmport); 19472 } else { 19473 /* keep link lost event */ 19474 pmportinfo->pmport_event_flags |= 19475 SATA_EVNT_LINK_LOST; 19476 } 19477 } 19478 } 19479 /* 19480 * We could change port state to disable/delay access to 19481 * the attached device until the link is recovered. 19482 */ 19483 } 19484 done: 19485 event_flags = pmportinfo->pmport_event_flags; 19486 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 19487 saddr->pmport)); 19488 if (event_flags != 0) { 19489 mutex_enter(&sata_hba_inst->satahba_mutex); 19490 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 19491 mutex_exit(&sata_hba_inst->satahba_mutex); 19492 mutex_enter(&sata_mutex); 19493 sata_event_pending |= SATA_EVNT_MAIN; 19494 mutex_exit(&sata_mutex); 19495 } 19496 } 19497 19498 /* 19499 * Device Detached Event processing. 19500 * Port is probed to find if a device is really gone. If so, 19501 * the device info structure is detached from the SATA port info structure 19502 * and released. 19503 * Port status is updated. 19504 * 19505 * NOTE: Port multiplier ports events are handled by 19506 * sata_process_pmdevice_detached() 19507 */ 19508 static void 19509 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst, 19510 sata_address_t *saddr) 19511 { 19512 sata_cport_info_t *cportinfo; 19513 sata_pmport_info_t *pmportinfo; 19514 sata_drive_info_t *sdevinfo; 19515 sata_device_t sata_device; 19516 sata_address_t pmport_addr; 19517 char name[16]; 19518 uint8_t cport = saddr->cport; 19519 int npmport; 19520 int rval; 19521 19522 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19523 "Processing port %d device detached", saddr->cport); 19524 19525 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 19526 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19527 /* Clear event flag */ 19528 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED; 19529 19530 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */ 19531 if ((cportinfo->cport_state & 19532 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 19533 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19534 cport_mutex); 19535 return; 19536 } 19537 /* For sanity, re-probe the port */ 19538 sata_device.satadev_rev = SATA_DEVICE_REV; 19539 sata_device.satadev_addr = *saddr; 19540 19541 /* 19542 * We have to exit mutex, because the HBA probe port function may 19543 * block on its own mutex. 19544 */ 19545 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19546 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 19547 (SATA_DIP(sata_hba_inst), &sata_device); 19548 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19549 sata_update_port_info(sata_hba_inst, &sata_device); 19550 if (rval != SATA_SUCCESS) { 19551 /* Something went wrong? Fail the port */ 19552 cportinfo->cport_state = SATA_PSTATE_FAILED; 19553 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19554 cport_mutex); 19555 SATA_LOG_D((sata_hba_inst, CE_WARN, 19556 "SATA port %d probing failed", 19557 saddr->cport)); 19558 /* 19559 * We may want to release device info structure, but 19560 * it is not necessary. 19561 */ 19562 return; 19563 } else { 19564 /* port probed successfully */ 19565 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 19566 } 19567 /* 19568 * Check if a device is still attached. For sanity, check also 19569 * link status - if no link, there is no device. 19570 */ 19571 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) == 19572 SATA_PORT_DEVLINK_UP && sata_device.satadev_type != 19573 SATA_DTYPE_NONE) { 19574 /* 19575 * Device is still attached - ignore detach event. 19576 */ 19577 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19578 cport_mutex); 19579 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19580 "Ignoring detach - device still attached to port %d", 19581 sata_device.satadev_addr.cport); 19582 return; 19583 } 19584 /* 19585 * We need to detach and release device info structure here 19586 */ 19587 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 19588 /* 19589 * A port-multiplier is removed. 19590 * 19591 * Calling sata_process_pmdevice_detached() does not work 19592 * here. The port multiplier is gone, so we cannot probe 19593 * sub-port any more and all pmult-related data structure must 19594 * be de-allocated immediately. Following structure of every 19595 * implemented sub-port behind the pmult are required to 19596 * released. 19597 * 19598 * - attachment point 19599 * - target node 19600 * - sata_drive_info 19601 * - sata_pmport_info 19602 */ 19603 for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, 19604 cport); npmport ++) { 19605 SATADBG2(SATA_DBG_PMULT|SATA_DBG_EVENTS_PROC, 19606 sata_hba_inst, 19607 "Detaching target node at port %d:%d", 19608 cport, npmport); 19609 19610 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 19611 19612 /* Remove attachment point. */ 19613 name[0] = '\0'; 19614 (void) sprintf(name, "%d.%d", cport, npmport); 19615 ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name); 19616 sata_log(sata_hba_inst, CE_NOTE, 19617 "Remove attachment point of port %d:%d", 19618 cport, npmport); 19619 19620 /* Remove target node */ 19621 pmport_addr.cport = cport; 19622 pmport_addr.pmport = (uint8_t)npmport; 19623 pmport_addr.qual = SATA_ADDR_PMPORT; 19624 sata_remove_target_node(sata_hba_inst, &pmport_addr); 19625 19626 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 19627 19628 /* Release sata_pmport_info & sata_drive_info. */ 19629 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 19630 cport, npmport); 19631 ASSERT(pmportinfo != NULL); 19632 19633 sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 19634 if (sdevinfo != NULL) { 19635 (void) kmem_free((void *) sdevinfo, 19636 sizeof (sata_drive_info_t)); 19637 } 19638 19639 /* Release sata_pmport_info at last */ 19640 (void) kmem_free((void *) pmportinfo, 19641 sizeof (sata_pmport_info_t)); 19642 } 19643 19644 /* Finally, release sata_pmult_info */ 19645 (void) kmem_free((void *) 19646 SATA_CPORTINFO_PMULT_INFO(cportinfo), 19647 sizeof (sata_pmult_info_t)); 19648 SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL; 19649 19650 sata_log(sata_hba_inst, CE_WARN, 19651 "SATA port-multiplier detached at port %d", cport); 19652 19653 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 19654 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19655 saddr->cport)->cport_mutex); 19656 } else { 19657 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 19658 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 19659 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 19660 (void) kmem_free((void *)sdevinfo, 19661 sizeof (sata_drive_info_t)); 19662 } 19663 sata_log(sata_hba_inst, CE_WARN, 19664 "SATA device detached at port %d", cport); 19665 19666 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 19667 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19668 saddr->cport)->cport_mutex); 19669 19670 /* 19671 * Try to offline a device and remove target node 19672 * if it still exists 19673 */ 19674 sata_remove_target_node(sata_hba_inst, saddr); 19675 } 19676 19677 19678 /* 19679 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 19680 * with the hint: SE_HINT_REMOVE 19681 */ 19682 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE); 19683 } 19684 19685 /* 19686 * Port Multiplier Port Device Deattached Event processing. 19687 * 19688 * NOTE: No Mutex should be hold. 19689 */ 19690 static void 19691 sata_process_pmdevice_detached(sata_hba_inst_t *sata_hba_inst, 19692 sata_address_t *saddr) 19693 { 19694 sata_pmport_info_t *pmportinfo; 19695 sata_drive_info_t *sdevinfo; 19696 sata_device_t sata_device; 19697 int rval; 19698 uint8_t cport, pmport; 19699 19700 cport = saddr->cport; 19701 pmport = saddr->pmport; 19702 19703 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19704 "Processing port %d:%d device detached", 19705 cport, pmport); 19706 19707 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 19708 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19709 19710 /* Clear event flag */ 19711 pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED; 19712 19713 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */ 19714 if ((pmportinfo->pmport_state & 19715 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 19716 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19717 return; 19718 } 19719 /* For sanity, re-probe the port */ 19720 sata_device.satadev_rev = SATA_DEVICE_REV; 19721 sata_device.satadev_addr = *saddr; 19722 19723 /* 19724 * We have to exit mutex, because the HBA probe port function may 19725 * block on its own mutex. 19726 */ 19727 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19728 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 19729 (SATA_DIP(sata_hba_inst), &sata_device); 19730 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19731 sata_update_pmport_info(sata_hba_inst, &sata_device); 19732 if (rval != SATA_SUCCESS) { 19733 /* Something went wrong? Fail the port */ 19734 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 19735 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19736 SATA_LOG_D((sata_hba_inst, CE_WARN, 19737 "SATA port %d:%d probing failed", 19738 saddr->pmport)); 19739 /* 19740 * We may want to release device info structure, but 19741 * it is not necessary. 19742 */ 19743 return; 19744 } else { 19745 /* port probed successfully */ 19746 pmportinfo->pmport_state |= 19747 SATA_STATE_PROBED | SATA_STATE_READY; 19748 } 19749 /* 19750 * Check if a device is still attached. For sanity, check also 19751 * link status - if no link, there is no device. 19752 */ 19753 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) == 19754 SATA_PORT_DEVLINK_UP && sata_device.satadev_type != 19755 SATA_DTYPE_NONE) { 19756 /* 19757 * Device is still attached - ignore detach event. 19758 */ 19759 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19760 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19761 "Ignoring detach - device still attached to port %d", 19762 sata_device.satadev_addr.pmport); 19763 return; 19764 } 19765 /* 19766 * We need to detach and release device info structure here 19767 */ 19768 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 19769 sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 19770 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 19771 (void) kmem_free((void *)sdevinfo, 19772 sizeof (sata_drive_info_t)); 19773 } 19774 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 19775 /* 19776 * Device cannot be reached anymore, even if the target node may be 19777 * still present. 19778 */ 19779 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19780 19781 /* 19782 * Try to offline a device and remove target node if it still exists 19783 */ 19784 sata_remove_target_node(sata_hba_inst, saddr); 19785 19786 /* 19787 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 19788 * with the hint: SE_HINT_REMOVE 19789 */ 19790 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE); 19791 } 19792 19793 19794 /* 19795 * Device Attached Event processing. 19796 * Port state is checked to verify that a device is really attached. If so, 19797 * the device info structure is created and attached to the SATA port info 19798 * structure. 19799 * 19800 * If attached device cannot be identified or set-up, the retry for the 19801 * attach processing is set-up. Subsequent daemon run would try again to 19802 * identify the device, until the time limit is reached 19803 * (SATA_DEV_IDENTIFY_TIMEOUT). 19804 * 19805 * This function cannot be called in interrupt context (it may sleep). 19806 * 19807 * NOTE: Port multiplier ports events are handled by 19808 * sata_process_pmdevice_attached() 19809 */ 19810 static void 19811 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst, 19812 sata_address_t *saddr) 19813 { 19814 sata_cport_info_t *cportinfo = NULL; 19815 sata_drive_info_t *sdevinfo = NULL; 19816 sata_pmult_info_t *pmultinfo = NULL; 19817 sata_pmport_info_t *pmportinfo = NULL; 19818 sata_device_t sata_device; 19819 dev_info_t *tdip; 19820 uint32_t event_flags = 0, pmult_event_flags = 0; 19821 int rval; 19822 int npmport; 19823 19824 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19825 "Processing port %d device attached", saddr->cport); 19826 19827 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 19828 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19829 19830 /* Clear attach event flag first */ 19831 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED; 19832 19833 /* If the port is in SHUTDOWN or FAILED state, ignore event. */ 19834 if ((cportinfo->cport_state & 19835 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 19836 cportinfo->cport_dev_attach_time = 0; 19837 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19838 cport_mutex); 19839 return; 19840 } 19841 19842 /* 19843 * If the sata_drive_info structure is found attached to the port info, 19844 * despite the fact the device was removed and now it is re-attached, 19845 * the old drive info structure was not removed. 19846 * Arbitrarily release device info structure. 19847 */ 19848 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 19849 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 19850 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 19851 (void) kmem_free((void *)sdevinfo, 19852 sizeof (sata_drive_info_t)); 19853 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19854 "Arbitrarily detaching old device info.", NULL); 19855 } 19856 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 19857 19858 /* For sanity, re-probe the port */ 19859 sata_device.satadev_rev = SATA_DEVICE_REV; 19860 sata_device.satadev_addr = *saddr; 19861 19862 /* 19863 * We have to exit mutex, because the HBA probe port function may 19864 * block on its own mutex. 19865 */ 19866 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19867 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 19868 (SATA_DIP(sata_hba_inst), &sata_device); 19869 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19870 sata_update_port_info(sata_hba_inst, &sata_device); 19871 if (rval != SATA_SUCCESS) { 19872 /* Something went wrong? Fail the port */ 19873 cportinfo->cport_state = SATA_PSTATE_FAILED; 19874 cportinfo->cport_dev_attach_time = 0; 19875 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19876 cport_mutex); 19877 SATA_LOG_D((sata_hba_inst, CE_WARN, 19878 "SATA port %d probing failed", 19879 saddr->cport)); 19880 return; 19881 } else { 19882 /* port probed successfully */ 19883 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 19884 } 19885 /* 19886 * Check if a device is still attached. For sanity, check also 19887 * link status - if no link, there is no device. 19888 */ 19889 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 19890 SATA_PORT_DEVLINK_UP || sata_device.satadev_type == 19891 SATA_DTYPE_NONE) { 19892 /* 19893 * No device - ignore attach event. 19894 */ 19895 cportinfo->cport_dev_attach_time = 0; 19896 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19897 cport_mutex); 19898 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19899 "Ignoring attach - no device connected to port %d", 19900 sata_device.satadev_addr.cport); 19901 return; 19902 } 19903 19904 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19905 /* 19906 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 19907 * with the hint: SE_HINT_INSERT 19908 */ 19909 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT); 19910 19911 /* 19912 * Port reprobing will take care of the creation of the device 19913 * info structure and determination of the device type. 19914 */ 19915 sata_device.satadev_addr = *saddr; 19916 (void) sata_reprobe_port(sata_hba_inst, &sata_device, 19917 SATA_DEV_IDENTIFY_NORETRY); 19918 19919 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19920 cport_mutex); 19921 if ((cportinfo->cport_state & SATA_STATE_READY) && 19922 (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) { 19923 /* Some device is attached to the port */ 19924 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) { 19925 /* 19926 * A device was not successfully attached. 19927 * Track retry time for device identification. 19928 */ 19929 if (cportinfo->cport_dev_attach_time != 0) { 19930 clock_t cur_time = ddi_get_lbolt(); 19931 /* 19932 * If the retry time limit was not exceeded, 19933 * reinstate attach event. 19934 */ 19935 if ((cur_time - 19936 cportinfo->cport_dev_attach_time) < 19937 drv_usectohz( 19938 SATA_DEV_IDENTIFY_TIMEOUT)) { 19939 /* OK, restore attach event */ 19940 cportinfo->cport_event_flags |= 19941 SATA_EVNT_DEVICE_ATTACHED; 19942 } else { 19943 /* Timeout - cannot identify device */ 19944 cportinfo->cport_dev_attach_time = 0; 19945 sata_log(sata_hba_inst, 19946 CE_WARN, 19947 "Could not identify SATA device " 19948 "at port %d", 19949 saddr->cport); 19950 } 19951 } else { 19952 /* 19953 * Start tracking time for device 19954 * identification. 19955 * Save current time (lbolt value). 19956 */ 19957 cportinfo->cport_dev_attach_time = 19958 ddi_get_lbolt(); 19959 /* Restore attach event */ 19960 cportinfo->cport_event_flags |= 19961 SATA_EVNT_DEVICE_ATTACHED; 19962 } 19963 } else if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 19964 cportinfo->cport_dev_attach_time = 0; 19965 sata_log(sata_hba_inst, CE_NOTE, 19966 "SATA port-multiplier detected at port %d", 19967 saddr->cport); 19968 19969 if (SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL) { 19970 /* Log the info of new port multiplier */ 19971 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19972 saddr->cport)->cport_mutex); 19973 sata_show_pmult_info(sata_hba_inst, 19974 &sata_device); 19975 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 19976 saddr->cport)->cport_mutex); 19977 } 19978 19979 ASSERT(SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL); 19980 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 19981 for (npmport = 0; npmport < 19982 pmultinfo->pmult_num_dev_ports; npmport++) { 19983 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 19984 saddr->cport, npmport); 19985 ASSERT(pmportinfo != NULL); 19986 19987 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19988 saddr->cport)->cport_mutex); 19989 mutex_enter(&pmportinfo->pmport_mutex); 19990 /* Marked all pmports with link events. */ 19991 pmportinfo->pmport_event_flags = 19992 SATA_EVNT_LINK_ESTABLISHED; 19993 pmult_event_flags |= 19994 pmportinfo->pmport_event_flags; 19995 mutex_exit(&pmportinfo->pmport_mutex); 19996 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 19997 saddr->cport)->cport_mutex); 19998 } 19999 /* Auto-online is not available for PMult now. */ 20000 20001 } else { 20002 /* 20003 * If device was successfully attached, the subsequent 20004 * action depends on a state of the 20005 * sata_auto_online variable. If it is set to zero. 20006 * an explicit 'configure' command will be needed to 20007 * configure it. If its value is non-zero, we will 20008 * attempt to online (configure) the device. 20009 * First, log the message indicating that a device 20010 * was attached. 20011 */ 20012 cportinfo->cport_dev_attach_time = 0; 20013 sata_log(sata_hba_inst, CE_WARN, 20014 "SATA device detected at port %d", saddr->cport); 20015 20016 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 20017 sata_drive_info_t new_sdinfo; 20018 20019 /* Log device info data */ 20020 new_sdinfo = *(SATA_CPORTINFO_DRV_INFO( 20021 cportinfo)); 20022 sata_show_drive_info(sata_hba_inst, 20023 &new_sdinfo); 20024 } 20025 20026 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20027 saddr->cport)->cport_mutex); 20028 20029 /* 20030 * Make sure that there is no target node for that 20031 * device. If so, release it. It should not happen, 20032 * unless we had problem removing the node when 20033 * device was detached. 20034 */ 20035 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 20036 saddr->cport, saddr->pmport); 20037 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20038 saddr->cport)->cport_mutex); 20039 if (tdip != NULL) { 20040 20041 #ifdef SATA_DEBUG 20042 if ((cportinfo->cport_event_flags & 20043 SATA_EVNT_TARGET_NODE_CLEANUP) == 0) 20044 sata_log(sata_hba_inst, CE_WARN, 20045 "sata_process_device_attached: " 20046 "old device target node exists!"); 20047 #endif 20048 /* 20049 * target node exists - try to unconfigure 20050 * device and remove the node. 20051 */ 20052 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20053 saddr->cport)->cport_mutex); 20054 rval = ndi_devi_offline(tdip, 20055 NDI_DEVI_REMOVE); 20056 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20057 saddr->cport)->cport_mutex); 20058 20059 if (rval == NDI_SUCCESS) { 20060 cportinfo->cport_event_flags &= 20061 ~SATA_EVNT_TARGET_NODE_CLEANUP; 20062 cportinfo->cport_tgtnode_clean = B_TRUE; 20063 } else { 20064 /* 20065 * PROBLEM - the target node remained 20066 * and it belongs to a previously 20067 * attached device. 20068 * This happens when the file was open 20069 * or the node was waiting for 20070 * resources at the time the 20071 * associated device was removed. 20072 * Instruct event daemon to retry the 20073 * cleanup later. 20074 */ 20075 sata_log(sata_hba_inst, 20076 CE_WARN, 20077 "Application(s) accessing " 20078 "previously attached SATA " 20079 "device have to release " 20080 "it before newly inserted " 20081 "device can be made accessible.", 20082 saddr->cport); 20083 cportinfo->cport_event_flags |= 20084 SATA_EVNT_TARGET_NODE_CLEANUP; 20085 cportinfo->cport_tgtnode_clean = 20086 B_FALSE; 20087 } 20088 } 20089 if (sata_auto_online != 0) { 20090 cportinfo->cport_event_flags |= 20091 SATA_EVNT_AUTOONLINE_DEVICE; 20092 } 20093 20094 } 20095 } else { 20096 cportinfo->cport_dev_attach_time = 0; 20097 } 20098 20099 event_flags = cportinfo->cport_event_flags; 20100 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 20101 if (event_flags != 0 || pmult_event_flags != 0) { 20102 mutex_enter(&sata_hba_inst->satahba_mutex); 20103 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 20104 mutex_exit(&sata_hba_inst->satahba_mutex); 20105 mutex_enter(&sata_mutex); 20106 sata_event_pending |= SATA_EVNT_MAIN; 20107 mutex_exit(&sata_mutex); 20108 } 20109 } 20110 20111 /* 20112 * Port Multiplier Port Device Attached Event processing. 20113 * 20114 * NOTE: No Mutex should be hold. 20115 */ 20116 static void 20117 sata_process_pmdevice_attached(sata_hba_inst_t *sata_hba_inst, 20118 sata_address_t *saddr) 20119 { 20120 sata_pmport_info_t *pmportinfo; 20121 sata_drive_info_t *sdinfo; 20122 sata_device_t sata_device; 20123 dev_info_t *tdip; 20124 uint32_t event_flags; 20125 uint8_t cport = saddr->cport; 20126 uint8_t pmport = saddr->pmport; 20127 int rval; 20128 20129 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20130 "Processing port %d:%d device attached", cport, pmport); 20131 20132 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 20133 20134 mutex_enter(&pmportinfo->pmport_mutex); 20135 20136 /* Clear attach event flag first */ 20137 pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED; 20138 20139 /* If the port is in SHUTDOWN or FAILED state, ignore event. */ 20140 if ((pmportinfo->pmport_state & 20141 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 20142 pmportinfo->pmport_dev_attach_time = 0; 20143 mutex_exit(&pmportinfo->pmport_mutex); 20144 return; 20145 } 20146 20147 /* 20148 * If the sata_drive_info structure is found attached to the port info, 20149 * despite the fact the device was removed and now it is re-attached, 20150 * the old drive info structure was not removed. 20151 * Arbitrarily release device info structure. 20152 */ 20153 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 20154 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 20155 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 20156 (void) kmem_free((void *)sdinfo, 20157 sizeof (sata_drive_info_t)); 20158 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20159 "Arbitrarily detaching old device info.", NULL); 20160 } 20161 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 20162 20163 /* For sanity, re-probe the port */ 20164 sata_device.satadev_rev = SATA_DEVICE_REV; 20165 sata_device.satadev_addr = *saddr; 20166 20167 /* 20168 * We have to exit mutex, because the HBA probe port function may 20169 * block on its own mutex. 20170 */ 20171 mutex_exit(&pmportinfo->pmport_mutex); 20172 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 20173 (SATA_DIP(sata_hba_inst), &sata_device); 20174 mutex_enter(&pmportinfo->pmport_mutex); 20175 20176 sata_update_pmport_info(sata_hba_inst, &sata_device); 20177 if (rval != SATA_SUCCESS) { 20178 /* Something went wrong? Fail the port */ 20179 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 20180 pmportinfo->pmport_dev_attach_time = 0; 20181 mutex_exit(&pmportinfo->pmport_mutex); 20182 SATA_LOG_D((sata_hba_inst, CE_WARN, 20183 "SATA port %d:%d probing failed", cport, pmport)); 20184 return; 20185 } else { 20186 /* pmport probed successfully */ 20187 pmportinfo->pmport_state |= 20188 SATA_STATE_PROBED | SATA_STATE_READY; 20189 } 20190 /* 20191 * Check if a device is still attached. For sanity, check also 20192 * link status - if no link, there is no device. 20193 */ 20194 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 20195 SATA_PORT_DEVLINK_UP || sata_device.satadev_type == 20196 SATA_DTYPE_NONE) { 20197 /* 20198 * No device - ignore attach event. 20199 */ 20200 pmportinfo->pmport_dev_attach_time = 0; 20201 mutex_exit(&pmportinfo->pmport_mutex); 20202 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20203 "Ignoring attach - no device connected to port %d:%d", 20204 cport, pmport); 20205 return; 20206 } 20207 20208 mutex_exit(&pmportinfo->pmport_mutex); 20209 /* 20210 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 20211 * with the hint: SE_HINT_INSERT 20212 */ 20213 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT); 20214 20215 /* 20216 * Port reprobing will take care of the creation of the device 20217 * info structure and determination of the device type. 20218 */ 20219 sata_device.satadev_addr = *saddr; 20220 (void) sata_reprobe_port(sata_hba_inst, &sata_device, 20221 SATA_DEV_IDENTIFY_NORETRY); 20222 20223 mutex_enter(&pmportinfo->pmport_mutex); 20224 if ((pmportinfo->pmport_state & SATA_STATE_READY) && 20225 (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE)) { 20226 /* Some device is attached to the port */ 20227 if (pmportinfo->pmport_dev_type == SATA_DTYPE_UNKNOWN) { 20228 /* 20229 * A device was not successfully attached. 20230 * Track retry time for device identification. 20231 */ 20232 if (pmportinfo->pmport_dev_attach_time != 0) { 20233 clock_t cur_time = ddi_get_lbolt(); 20234 /* 20235 * If the retry time limit was not exceeded, 20236 * reinstate attach event. 20237 */ 20238 if ((cur_time - 20239 pmportinfo->pmport_dev_attach_time) < 20240 drv_usectohz( 20241 SATA_DEV_IDENTIFY_TIMEOUT)) { 20242 /* OK, restore attach event */ 20243 pmportinfo->pmport_event_flags |= 20244 SATA_EVNT_DEVICE_ATTACHED; 20245 } else { 20246 /* Timeout - cannot identify device */ 20247 pmportinfo->pmport_dev_attach_time = 0; 20248 sata_log(sata_hba_inst, CE_WARN, 20249 "Could not identify SATA device " 20250 "at port %d:%d", 20251 cport, pmport); 20252 } 20253 } else { 20254 /* 20255 * Start tracking time for device 20256 * identification. 20257 * Save current time (lbolt value). 20258 */ 20259 pmportinfo->pmport_dev_attach_time = 20260 ddi_get_lbolt(); 20261 /* Restore attach event */ 20262 pmportinfo->pmport_event_flags |= 20263 SATA_EVNT_DEVICE_ATTACHED; 20264 } 20265 } else { 20266 /* 20267 * If device was successfully attached, the subsequent 20268 * action depends on a state of the 20269 * sata_auto_online variable. If it is set to zero. 20270 * an explicit 'configure' command will be needed to 20271 * configure it. If its value is non-zero, we will 20272 * attempt to online (configure) the device. 20273 * First, log the message indicating that a device 20274 * was attached. 20275 */ 20276 pmportinfo->pmport_dev_attach_time = 0; 20277 sata_log(sata_hba_inst, CE_WARN, 20278 "SATA device detected at port %d:%d", 20279 cport, pmport); 20280 20281 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 20282 sata_drive_info_t new_sdinfo; 20283 20284 /* Log device info data */ 20285 new_sdinfo = *(SATA_PMPORTINFO_DRV_INFO( 20286 pmportinfo)); 20287 sata_show_drive_info(sata_hba_inst, 20288 &new_sdinfo); 20289 } 20290 20291 mutex_exit(&pmportinfo->pmport_mutex); 20292 20293 /* 20294 * Make sure that there is no target node for that 20295 * device. If so, release it. It should not happen, 20296 * unless we had problem removing the node when 20297 * device was detached. 20298 */ 20299 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 20300 saddr->cport, saddr->pmport); 20301 mutex_enter(&pmportinfo->pmport_mutex); 20302 if (tdip != NULL) { 20303 20304 #ifdef SATA_DEBUG 20305 if ((pmportinfo->pmport_event_flags & 20306 SATA_EVNT_TARGET_NODE_CLEANUP) == 0) 20307 sata_log(sata_hba_inst, CE_WARN, 20308 "sata_process_device_attached: " 20309 "old device target node exists!"); 20310 #endif 20311 /* 20312 * target node exists - try to unconfigure 20313 * device and remove the node. 20314 */ 20315 mutex_exit(&pmportinfo->pmport_mutex); 20316 rval = ndi_devi_offline(tdip, 20317 NDI_DEVI_REMOVE); 20318 mutex_enter(&pmportinfo->pmport_mutex); 20319 20320 if (rval == NDI_SUCCESS) { 20321 pmportinfo->pmport_event_flags &= 20322 ~SATA_EVNT_TARGET_NODE_CLEANUP; 20323 pmportinfo->pmport_tgtnode_clean = 20324 B_TRUE; 20325 } else { 20326 /* 20327 * PROBLEM - the target node remained 20328 * and it belongs to a previously 20329 * attached device. 20330 * This happens when the file was open 20331 * or the node was waiting for 20332 * resources at the time the 20333 * associated device was removed. 20334 * Instruct event daemon to retry the 20335 * cleanup later. 20336 */ 20337 sata_log(sata_hba_inst, 20338 CE_WARN, 20339 "Application(s) accessing " 20340 "previously attached SATA " 20341 "device have to release " 20342 "it before newly inserted " 20343 "device can be made accessible." 20344 "at port %d:%d", 20345 cport, pmport); 20346 pmportinfo->pmport_event_flags |= 20347 SATA_EVNT_TARGET_NODE_CLEANUP; 20348 pmportinfo->pmport_tgtnode_clean = 20349 B_FALSE; 20350 } 20351 } 20352 if (sata_auto_online != 0) { 20353 pmportinfo->pmport_event_flags |= 20354 SATA_EVNT_AUTOONLINE_DEVICE; 20355 } 20356 20357 } 20358 } else { 20359 pmportinfo->pmport_dev_attach_time = 0; 20360 } 20361 20362 event_flags = pmportinfo->pmport_event_flags; 20363 mutex_exit(&pmportinfo->pmport_mutex); 20364 if (event_flags != 0) { 20365 mutex_enter(&sata_hba_inst->satahba_mutex); 20366 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 20367 mutex_exit(&sata_hba_inst->satahba_mutex); 20368 mutex_enter(&sata_mutex); 20369 sata_event_pending |= SATA_EVNT_MAIN; 20370 mutex_exit(&sata_mutex); 20371 } 20372 20373 /* clear the reset_in_progress events */ 20374 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 20375 if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) { 20376 /* must clear flags on cport */ 20377 sata_pmult_info_t *pminfo = 20378 SATA_PMULT_INFO(sata_hba_inst, 20379 saddr->cport); 20380 pminfo->pmult_event_flags |= 20381 SATA_EVNT_CLEAR_DEVICE_RESET; 20382 } 20383 } 20384 } 20385 20386 /* 20387 * Device Target Node Cleanup Event processing. 20388 * If the target node associated with a sata port device is in 20389 * DEVI_DEVICE_REMOVED state, an attempt is made to remove it. 20390 * If the target node cannot be removed, the event flag is left intact, 20391 * so that event daemon may re-run this function later. 20392 * 20393 * This function cannot be called in interrupt context (it may sleep). 20394 * 20395 * NOTE: Processes cport events only, not port multiplier ports. 20396 */ 20397 static void 20398 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, 20399 sata_address_t *saddr) 20400 { 20401 sata_cport_info_t *cportinfo; 20402 dev_info_t *tdip; 20403 20404 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20405 "Processing port %d device target node cleanup", saddr->cport); 20406 20407 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 20408 20409 /* 20410 * Check if there is target node for that device and it is in the 20411 * DEVI_DEVICE_REMOVED state. If so, release it. 20412 */ 20413 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport, 20414 saddr->pmport); 20415 if (tdip != NULL) { 20416 /* 20417 * target node exists - check if it is target node of 20418 * a removed device. 20419 */ 20420 if (sata_check_device_removed(tdip) == B_TRUE) { 20421 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20422 "sata_process_target_node_cleanup: " 20423 "old device target node exists!", NULL); 20424 /* 20425 * Unconfigure and remove the target node 20426 */ 20427 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) == 20428 NDI_SUCCESS) { 20429 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20430 saddr->cport)->cport_mutex); 20431 cportinfo->cport_event_flags &= 20432 ~SATA_EVNT_TARGET_NODE_CLEANUP; 20433 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20434 saddr->cport)->cport_mutex); 20435 return; 20436 } 20437 /* 20438 * Event daemon will retry the cleanup later. 20439 */ 20440 mutex_enter(&sata_hba_inst->satahba_mutex); 20441 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 20442 mutex_exit(&sata_hba_inst->satahba_mutex); 20443 mutex_enter(&sata_mutex); 20444 sata_event_pending |= SATA_EVNT_MAIN; 20445 mutex_exit(&sata_mutex); 20446 } 20447 } else { 20448 if (saddr->qual == SATA_ADDR_CPORT || 20449 saddr->qual == SATA_ADDR_DCPORT) { 20450 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20451 saddr->cport)->cport_mutex); 20452 cportinfo->cport_event_flags &= 20453 ~SATA_EVNT_TARGET_NODE_CLEANUP; 20454 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20455 saddr->cport)->cport_mutex); 20456 } else { 20457 /* sanity check */ 20458 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) != 20459 SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst, 20460 saddr->cport) == NULL) 20461 return; 20462 if (SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, 20463 saddr->pmport) == NULL) 20464 return; 20465 20466 mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst, 20467 saddr->cport, saddr->pmport)->pmport_mutex); 20468 SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, 20469 saddr->pmport)->pmport_event_flags &= 20470 ~SATA_EVNT_TARGET_NODE_CLEANUP; 20471 mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst, 20472 saddr->cport, saddr->pmport)->pmport_mutex); 20473 } 20474 } 20475 } 20476 20477 /* 20478 * Device AutoOnline Event processing. 20479 * If attached device is to be onlined, an attempt is made to online this 20480 * device, but only if there is no lingering (old) target node present. 20481 * If the device cannot be onlined, the event flag is left intact, 20482 * so that event daemon may re-run this function later. 20483 * 20484 * This function cannot be called in interrupt context (it may sleep). 20485 * 20486 * NOTE: Processes cport events only, not port multiplier ports. 20487 */ 20488 static void 20489 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst, 20490 sata_address_t *saddr) 20491 { 20492 sata_cport_info_t *cportinfo; 20493 sata_drive_info_t *sdinfo; 20494 sata_device_t sata_device; 20495 dev_info_t *tdip; 20496 20497 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20498 "Processing port %d attached device auto-onlining", saddr->cport); 20499 20500 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 20501 20502 /* 20503 * Check if device is present and recognized. If not, reset event. 20504 */ 20505 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 20506 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) { 20507 /* Nothing to online */ 20508 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE; 20509 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20510 saddr->cport)->cport_mutex); 20511 return; 20512 } 20513 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 20514 20515 /* 20516 * Check if there is target node for this device and if it is in the 20517 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep 20518 * the event for later processing. 20519 */ 20520 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport, 20521 saddr->pmport); 20522 if (tdip != NULL) { 20523 /* 20524 * target node exists - check if it is target node of 20525 * a removed device. 20526 */ 20527 if (sata_check_device_removed(tdip) == B_TRUE) { 20528 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20529 "sata_process_device_autoonline: " 20530 "old device target node exists!", NULL); 20531 /* 20532 * Event daemon will retry device onlining later. 20533 */ 20534 mutex_enter(&sata_hba_inst->satahba_mutex); 20535 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 20536 mutex_exit(&sata_hba_inst->satahba_mutex); 20537 mutex_enter(&sata_mutex); 20538 sata_event_pending |= SATA_EVNT_MAIN; 20539 mutex_exit(&sata_mutex); 20540 return; 20541 } 20542 /* 20543 * If the target node is not in the 'removed" state, assume 20544 * that it belongs to this device. There is nothing more to do, 20545 * but reset the event. 20546 */ 20547 } else { 20548 20549 /* 20550 * Try to online the device 20551 * If there is any reset-related event, remove it. We are 20552 * configuring the device and no state restoring is needed. 20553 */ 20554 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20555 saddr->cport)->cport_mutex); 20556 sata_device.satadev_addr = *saddr; 20557 if (saddr->qual == SATA_ADDR_CPORT) 20558 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 20559 else 20560 sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT; 20561 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 20562 if (sdinfo != NULL) { 20563 if (sdinfo->satadrv_event_flags & 20564 (SATA_EVNT_DEVICE_RESET | 20565 SATA_EVNT_INPROC_DEVICE_RESET)) 20566 sdinfo->satadrv_event_flags = 0; 20567 sdinfo->satadrv_event_flags |= 20568 SATA_EVNT_CLEAR_DEVICE_RESET; 20569 20570 /* Need to create a new target node. */ 20571 cportinfo->cport_tgtnode_clean = B_TRUE; 20572 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20573 saddr->cport)->cport_mutex); 20574 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst), 20575 sata_hba_inst, &sata_device.satadev_addr); 20576 if (tdip == NULL) { 20577 /* 20578 * Configure (onlining) failed. 20579 * We will NOT retry 20580 */ 20581 SATA_LOG_D((sata_hba_inst, CE_WARN, 20582 "sata_process_device_autoonline: " 20583 "configuring SATA device at port %d failed", 20584 saddr->cport)); 20585 } 20586 } else { 20587 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20588 saddr->cport)->cport_mutex); 20589 } 20590 20591 } 20592 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 20593 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE; 20594 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20595 saddr->cport)->cport_mutex); 20596 } 20597 20598 20599 static void 20600 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr, 20601 int hint) 20602 { 20603 char ap[MAXPATHLEN]; 20604 nvlist_t *ev_attr_list = NULL; 20605 int err; 20606 20607 /* Allocate and build sysevent attribute list */ 20608 err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP); 20609 if (err != 0) { 20610 SATA_LOG_D((sata_hba_inst, CE_WARN, 20611 "sata_gen_sysevent: " 20612 "cannot allocate memory for sysevent attributes\n")); 20613 return; 20614 } 20615 /* Add hint attribute */ 20616 err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint)); 20617 if (err != 0) { 20618 SATA_LOG_D((sata_hba_inst, CE_WARN, 20619 "sata_gen_sysevent: " 20620 "failed to add DR_HINT attr for sysevent")); 20621 nvlist_free(ev_attr_list); 20622 return; 20623 } 20624 /* 20625 * Add AP attribute. 20626 * Get controller pathname and convert it into AP pathname by adding 20627 * a target number. 20628 */ 20629 (void) snprintf(ap, MAXPATHLEN, "/devices"); 20630 (void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap)); 20631 (void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d", 20632 SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual)); 20633 20634 err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap); 20635 if (err != 0) { 20636 SATA_LOG_D((sata_hba_inst, CE_WARN, 20637 "sata_gen_sysevent: " 20638 "failed to add DR_AP_ID attr for sysevent")); 20639 nvlist_free(ev_attr_list); 20640 return; 20641 } 20642 20643 /* Generate/log sysevent */ 20644 err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR, 20645 ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP); 20646 if (err != DDI_SUCCESS) { 20647 SATA_LOG_D((sata_hba_inst, CE_WARN, 20648 "sata_gen_sysevent: " 20649 "cannot log sysevent, err code %x\n", err)); 20650 } 20651 20652 nvlist_free(ev_attr_list); 20653 } 20654 20655 20656 20657 20658 /* 20659 * Set DEVI_DEVICE_REMOVED state in the SATA device target node. 20660 */ 20661 static void 20662 sata_set_device_removed(dev_info_t *tdip) 20663 { 20664 int circ; 20665 20666 ASSERT(tdip != NULL); 20667 20668 ndi_devi_enter(tdip, &circ); 20669 mutex_enter(&DEVI(tdip)->devi_lock); 20670 DEVI_SET_DEVICE_REMOVED(tdip); 20671 mutex_exit(&DEVI(tdip)->devi_lock); 20672 ndi_devi_exit(tdip, circ); 20673 } 20674 20675 20676 /* 20677 * Set internal event instructing event daemon to try 20678 * to perform the target node cleanup. 20679 */ 20680 static void 20681 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, 20682 sata_address_t *saddr) 20683 { 20684 if (saddr->qual == SATA_ADDR_CPORT || 20685 saddr->qual == SATA_ADDR_DCPORT) { 20686 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20687 saddr->cport)->cport_mutex); 20688 SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |= 20689 SATA_EVNT_TARGET_NODE_CLEANUP; 20690 SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 20691 cport_tgtnode_clean = B_FALSE; 20692 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20693 saddr->cport)->cport_mutex); 20694 } else { 20695 mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst, 20696 saddr->cport, saddr->pmport)->pmport_mutex); 20697 SATA_PMPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport, 20698 saddr->pmport) |= SATA_EVNT_TARGET_NODE_CLEANUP; 20699 SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, saddr->pmport)-> 20700 pmport_tgtnode_clean = B_FALSE; 20701 mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst, 20702 saddr->cport, saddr->pmport)->pmport_mutex); 20703 } 20704 mutex_enter(&sata_hba_inst->satahba_mutex); 20705 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 20706 mutex_exit(&sata_hba_inst->satahba_mutex); 20707 mutex_enter(&sata_mutex); 20708 sata_event_pending |= SATA_EVNT_MAIN; 20709 mutex_exit(&sata_mutex); 20710 } 20711 20712 20713 /* 20714 * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state, 20715 * i.e. check if the target node state indicates that it belongs to a removed 20716 * device. 20717 * 20718 * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state, 20719 * B_FALSE otherwise. 20720 */ 20721 static boolean_t 20722 sata_check_device_removed(dev_info_t *tdip) 20723 { 20724 ASSERT(tdip != NULL); 20725 20726 if (DEVI_IS_DEVICE_REMOVED(tdip)) 20727 return (B_TRUE); 20728 else 20729 return (B_FALSE); 20730 } 20731 20732 20733 /* 20734 * Check for DMA error. Return B_TRUE if error, B_FALSE otherwise. 20735 */ 20736 static boolean_t 20737 sata_check_for_dma_error(dev_info_t *dip, sata_pkt_txlate_t *spx) 20738 { 20739 int fm_capability = ddi_fm_capable(dip); 20740 ddi_fm_error_t de; 20741 20742 if (fm_capability & DDI_FM_DMACHK_CAPABLE) { 20743 if (spx->txlt_buf_dma_handle != NULL) { 20744 ddi_fm_dma_err_get(spx->txlt_buf_dma_handle, &de, 20745 DDI_FME_VERSION); 20746 if (de.fme_status != DDI_SUCCESS) 20747 return (B_TRUE); 20748 } 20749 } 20750 return (B_FALSE); 20751 } 20752 20753 20754 /* ************************ FAULT INJECTTION **************************** */ 20755 20756 #ifdef SATA_INJECT_FAULTS 20757 20758 static uint32_t sata_fault_count = 0; 20759 static uint32_t sata_fault_suspend_count = 0; 20760 20761 /* 20762 * Inject sata pkt fault 20763 * It modifies returned values of the sata packet. 20764 * It returns immediately if: 20765 * pkt fault injection is not enabled (via sata_inject_fault, 20766 * sata_inject_fault_count), or invalid fault is specified (sata_fault_type), 20767 * or pkt does not contain command to be faulted (set in sata_fault_cmd), or 20768 * pkt is not directed to specified fault controller/device 20769 * (sata_fault_ctrl_dev and sata_fault_device). 20770 * If fault controller is not specified, fault injection applies to all 20771 * controllers and devices. 20772 * 20773 * First argument is the pointer to the executed sata packet. 20774 * Second argument is a pointer to a value returned by the HBA tran_start 20775 * function. 20776 * Third argument specifies injected error. Injected sata packet faults 20777 * are the satapkt_reason values. 20778 * SATA_PKT_BUSY -1 Not completed, busy 20779 * SATA_PKT_DEV_ERROR 1 Device reported error 20780 * SATA_PKT_QUEUE_FULL 2 Not accepted, queue full 20781 * SATA_PKT_PORT_ERROR 3 Not completed, port error 20782 * SATA_PKT_CMD_UNSUPPORTED 4 Cmd unsupported 20783 * SATA_PKT_ABORTED 5 Aborted by request 20784 * SATA_PKT_TIMEOUT 6 Operation timeut 20785 * SATA_PKT_RESET 7 Aborted by reset request 20786 * 20787 * Additional global variables affecting the execution: 20788 * 20789 * sata_inject_fault_count variable specifies number of times in row the 20790 * error is injected. Value of -1 specifies permanent fault, ie. every time 20791 * the fault injection point is reached, the fault is injected and a pause 20792 * between fault injection specified by sata_inject_fault_pause_count is 20793 * ignored). Fault injection routine decrements sata_inject_fault_count 20794 * (if greater than zero) until it reaches 0. No fault is injected when 20795 * sata_inject_fault_count is 0 (zero). 20796 * 20797 * sata_inject_fault_pause_count variable specifies number of times a fault 20798 * injection is bypassed (pause between fault injections). 20799 * If set to 0, a fault is injected only a number of times specified by 20800 * sata_inject_fault_count. 20801 * 20802 * The fault counts are static, so for periodic errors they have to be manually 20803 * reset to start repetition sequence from scratch. 20804 * If the original value returned by the HBA tran_start function is not 20805 * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error 20806 * is injected (to avoid masking real problems); 20807 * 20808 * NOTE: In its current incarnation, this function should be invoked only for 20809 * commands executed in SYNCHRONOUS mode. 20810 */ 20811 20812 20813 static void 20814 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault) 20815 { 20816 20817 if (sata_inject_fault != SATA_INJECT_PKT_FAULT) 20818 return; 20819 20820 if (sata_inject_fault_count == 0) 20821 return; 20822 20823 if (fault == 0) 20824 return; 20825 20826 if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg) 20827 return; 20828 20829 if (sata_fault_ctrl != NULL) { 20830 sata_pkt_txlate_t *spx = 20831 (sata_pkt_txlate_t *)spkt->satapkt_framework_private; 20832 20833 if (sata_fault_ctrl != NULL && sata_fault_ctrl != 20834 spx->txlt_sata_hba_inst->satahba_dip) 20835 return; 20836 20837 if (sata_fault_device.satadev_addr.cport != 20838 spkt->satapkt_device.satadev_addr.cport || 20839 sata_fault_device.satadev_addr.pmport != 20840 spkt->satapkt_device.satadev_addr.pmport || 20841 sata_fault_device.satadev_addr.qual != 20842 spkt->satapkt_device.satadev_addr.qual) 20843 return; 20844 } 20845 20846 /* Modify pkt return parameters */ 20847 if (*rval != SATA_TRAN_ACCEPTED || 20848 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 20849 sata_fault_count = 0; 20850 sata_fault_suspend_count = 0; 20851 return; 20852 } 20853 if (sata_fault_count == 0 && sata_fault_suspend_count != 0) { 20854 /* Pause in the injection */ 20855 sata_fault_suspend_count -= 1; 20856 return; 20857 } 20858 20859 if (sata_fault_count == 0 && sata_fault_suspend_count == 0) { 20860 /* 20861 * Init inject fault cycle. If fault count is set to -1, 20862 * it is a permanent fault. 20863 */ 20864 if (sata_inject_fault_count != -1) { 20865 sata_fault_count = sata_inject_fault_count; 20866 sata_fault_suspend_count = 20867 sata_inject_fault_pause_count; 20868 if (sata_fault_suspend_count == 0) 20869 sata_inject_fault_count = 0; 20870 } 20871 } 20872 20873 if (sata_fault_count != 0) 20874 sata_fault_count -= 1; 20875 20876 switch (fault) { 20877 case SATA_PKT_BUSY: 20878 *rval = SATA_TRAN_BUSY; 20879 spkt->satapkt_reason = SATA_PKT_BUSY; 20880 break; 20881 20882 case SATA_PKT_QUEUE_FULL: 20883 *rval = SATA_TRAN_QUEUE_FULL; 20884 spkt->satapkt_reason = SATA_PKT_QUEUE_FULL; 20885 break; 20886 20887 case SATA_PKT_CMD_UNSUPPORTED: 20888 *rval = SATA_TRAN_CMD_UNSUPPORTED; 20889 spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED; 20890 break; 20891 20892 case SATA_PKT_PORT_ERROR: 20893 /* This is "rejected" command */ 20894 *rval = SATA_TRAN_PORT_ERROR; 20895 spkt->satapkt_reason = SATA_PKT_PORT_ERROR; 20896 /* Additional error setup could be done here - port state */ 20897 break; 20898 20899 case SATA_PKT_DEV_ERROR: 20900 spkt->satapkt_reason = SATA_PKT_DEV_ERROR; 20901 /* 20902 * Additional error setup could be done here 20903 */ 20904 break; 20905 20906 case SATA_PKT_ABORTED: 20907 spkt->satapkt_reason = SATA_PKT_ABORTED; 20908 break; 20909 20910 case SATA_PKT_TIMEOUT: 20911 spkt->satapkt_reason = SATA_PKT_TIMEOUT; 20912 /* Additional error setup could be done here */ 20913 break; 20914 20915 case SATA_PKT_RESET: 20916 spkt->satapkt_reason = SATA_PKT_RESET; 20917 /* 20918 * Additional error setup could be done here - device reset 20919 */ 20920 break; 20921 20922 default: 20923 break; 20924 } 20925 } 20926 20927 #endif 20928 20929 /* 20930 * SATA Trace Ring Buffer 20931 * ---------------------- 20932 * 20933 * Overview 20934 * 20935 * The SATA trace ring buffer is a ring buffer created and managed by 20936 * the SATA framework module that can be used by any module or driver 20937 * within the SATA framework to store debug messages. 20938 * 20939 * Ring Buffer Interfaces: 20940 * 20941 * sata_vtrace_debug() <-- Adds debug message to ring buffer 20942 * sata_trace_debug() <-- Wraps varargs into sata_vtrace_debug() 20943 * 20944 * Note that the sata_trace_debug() interface was created to give 20945 * consumers the flexibilty of sending debug messages to ring buffer 20946 * as variable arguments. Consumers can send type va_list debug 20947 * messages directly to sata_vtrace_debug(). The sata_trace_debug() 20948 * and sata_vtrace_debug() relationship is similar to that of 20949 * cmn_err(9F) and vcmn_err(9F). 20950 * 20951 * Below is a diagram of the SATA trace ring buffer interfaces and 20952 * sample consumers: 20953 * 20954 * +---------------------------------+ 20955 * | o o SATA Framework Module | 20956 * | o SATA o +------------------+ +------------------+ 20957 * |o Trace o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1| 20958 * |o R-Buf o |sata_trace_debug |<--+ +------------------+ 20959 * | o o +------------------+ | +------------------+ 20960 * | o o ^ | +--|SATA HBA Driver #2| 20961 * | | | +------------------+ 20962 * | +------------------+ | 20963 * | |SATA Debug Message| | 20964 * | +------------------+ | 20965 * +---------------------------------+ 20966 * 20967 * Supporting Routines: 20968 * 20969 * sata_trace_rbuf_alloc() <-- Initializes ring buffer 20970 * sata_trace_rbuf_free() <-- Destroys ring buffer 20971 * sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer 20972 * sata_trace_dmsg_free() <-- Destroys content of ring buffer 20973 * 20974 * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE. 20975 * The ring buffer size can be adjusted by setting dmsg_ring_size in 20976 * /etc/system to desired size in unit of bytes. 20977 * 20978 * The individual debug message size in the ring buffer is restricted 20979 * to DMSG_BUF_SIZE. 20980 */ 20981 void 20982 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap) 20983 { 20984 sata_trace_dmsg_t *dmsg; 20985 20986 if (sata_debug_rbuf == NULL) { 20987 return; 20988 } 20989 20990 /* 20991 * If max size of ring buffer is smaller than size 20992 * required for one debug message then just return 20993 * since we have no room for the debug message. 20994 */ 20995 if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) { 20996 return; 20997 } 20998 20999 mutex_enter(&sata_debug_rbuf->lock); 21000 21001 /* alloc or reuse on ring buffer */ 21002 dmsg = sata_trace_dmsg_alloc(); 21003 21004 if (dmsg == NULL) { 21005 /* resource allocation failed */ 21006 mutex_exit(&sata_debug_rbuf->lock); 21007 return; 21008 } 21009 21010 dmsg->dip = dip; 21011 gethrestime(&dmsg->timestamp); 21012 21013 (void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap); 21014 21015 mutex_exit(&sata_debug_rbuf->lock); 21016 } 21017 21018 void 21019 sata_trace_debug(dev_info_t *dip, const char *fmt, ...) 21020 { 21021 va_list ap; 21022 21023 va_start(ap, fmt); 21024 sata_vtrace_debug(dip, fmt, ap); 21025 va_end(ap); 21026 } 21027 21028 /* 21029 * This routine is used to manage debug messages 21030 * on ring buffer. 21031 */ 21032 static sata_trace_dmsg_t * 21033 sata_trace_dmsg_alloc(void) 21034 { 21035 sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp; 21036 21037 if (sata_debug_rbuf->looped == TRUE) { 21038 sata_debug_rbuf->dmsgp = dmsg->next; 21039 return (sata_debug_rbuf->dmsgp); 21040 } 21041 21042 /* 21043 * If we're looping for the first time, 21044 * connect the ring. 21045 */ 21046 if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) > 21047 sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) { 21048 dmsg->next = sata_debug_rbuf->dmsgh; 21049 sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh; 21050 sata_debug_rbuf->looped = TRUE; 21051 return (sata_debug_rbuf->dmsgp); 21052 } 21053 21054 /* If we've gotten this far then memory allocation is needed */ 21055 dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP); 21056 if (dmsg_alloc == NULL) { 21057 sata_debug_rbuf->allocfailed++; 21058 return (dmsg_alloc); 21059 } else { 21060 sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t); 21061 } 21062 21063 if (sata_debug_rbuf->dmsgp != NULL) { 21064 dmsg->next = dmsg_alloc; 21065 sata_debug_rbuf->dmsgp = dmsg->next; 21066 return (sata_debug_rbuf->dmsgp); 21067 } else { 21068 /* 21069 * We should only be here if we're initializing 21070 * the ring buffer. 21071 */ 21072 if (sata_debug_rbuf->dmsgh == NULL) { 21073 sata_debug_rbuf->dmsgh = dmsg_alloc; 21074 } else { 21075 /* Something is wrong */ 21076 kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t)); 21077 return (NULL); 21078 } 21079 21080 sata_debug_rbuf->dmsgp = dmsg_alloc; 21081 return (sata_debug_rbuf->dmsgp); 21082 } 21083 } 21084 21085 21086 /* 21087 * Free all messages on debug ring buffer. 21088 */ 21089 static void 21090 sata_trace_dmsg_free(void) 21091 { 21092 sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh; 21093 21094 while (dmsg != NULL) { 21095 dmsg_next = dmsg->next; 21096 kmem_free(dmsg, sizeof (sata_trace_dmsg_t)); 21097 21098 /* 21099 * If we've looped around the ring than we're done. 21100 */ 21101 if (dmsg_next == sata_debug_rbuf->dmsgh) { 21102 break; 21103 } else { 21104 dmsg = dmsg_next; 21105 } 21106 } 21107 } 21108 21109 21110 /* 21111 * This function can block 21112 */ 21113 static void 21114 sata_trace_rbuf_alloc(void) 21115 { 21116 sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP); 21117 21118 mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL); 21119 21120 if (dmsg_ring_size > 0) { 21121 sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size; 21122 } 21123 } 21124 21125 21126 static void 21127 sata_trace_rbuf_free(void) 21128 { 21129 sata_trace_dmsg_free(); 21130 mutex_destroy(&sata_debug_rbuf->lock); 21131 kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t)); 21132 } 21133 21134 /* 21135 * If SATA_DEBUG is not defined then this routine is called instead 21136 * of sata_log() via the SATA_LOG_D macro. 21137 */ 21138 static void 21139 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level, 21140 const char *fmt, ...) 21141 { 21142 #ifndef __lock_lint 21143 _NOTE(ARGUNUSED(level)) 21144 #endif 21145 21146 dev_info_t *dip = NULL; 21147 va_list ap; 21148 21149 if (sata_hba_inst != NULL) { 21150 dip = SATA_DIP(sata_hba_inst); 21151 } 21152 21153 va_start(ap, fmt); 21154 sata_vtrace_debug(dip, fmt, ap); 21155 va_end(ap); 21156 } 21157