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 * Copyright 2021 Racktop Systems, Inc. 30 */ 31 32 /* 33 * SATA Framework 34 * Generic SATA Host Adapter Implementation 35 */ 36 37 #include <sys/conf.h> 38 #include <sys/file.h> 39 #include <sys/ddi.h> 40 #include <sys/sunddi.h> 41 #include <sys/modctl.h> 42 #include <sys/cmn_err.h> 43 #include <sys/errno.h> 44 #include <sys/thread.h> 45 #include <sys/kstat.h> 46 #include <sys/note.h> 47 #include <sys/sysevent.h> 48 #include <sys/sysevent/eventdefs.h> 49 #include <sys/sysevent/dr.h> 50 #include <sys/taskq.h> 51 #include <sys/disp.h> 52 #include <sys/sdt.h> 53 54 #include <sys/sata/impl/sata.h> 55 #include <sys/sata/sata_hba.h> 56 #include <sys/sata/sata_defs.h> 57 #include <sys/sata/sata_cfgadm.h> 58 #include <sys/sata/sata_blacklist.h> 59 #include <sys/sata/sata_satl.h> 60 61 #include <sys/scsi/impl/spc3_types.h> 62 63 /* 64 * FMA header files 65 */ 66 #include <sys/ddifm.h> 67 #include <sys/fm/protocol.h> 68 #include <sys/fm/util.h> 69 #include <sys/fm/io/ddi.h> 70 71 /* Debug flags - defined in sata.h */ 72 int sata_debug_flags = 0; 73 int sata_msg = 0; 74 75 /* 76 * Flags enabling selected SATA HBA framework functionality 77 */ 78 #define SATA_ENABLE_QUEUING 1 79 #define SATA_ENABLE_NCQ 2 80 #define SATA_ENABLE_PROCESS_EVENTS 4 81 #define SATA_ENABLE_PMULT_FBS 8 /* FIS-Based Switching */ 82 int sata_func_enable = 83 SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ; 84 85 /* 86 * Global variable setting default maximum queue depth (NCQ or TCQ) 87 * Note:minimum queue depth is 1 88 */ 89 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */ 90 91 /* 92 * Currently used default NCQ/TCQ queue depth. It is set-up during the driver 93 * initialization, using value from sata_max_queue_depth 94 * It is adjusted to minimum supported by the controller and by the device, 95 * if queueing is enabled. 96 */ 97 static int sata_current_max_qdepth; 98 99 /* 100 * Global variable determining the default behavior after device hotpluggin. 101 * If non-zero, the hotplugged device is onlined (if possible) without explicit 102 * IOCTL request (AP_CONFIGURE). 103 * If zero, hotplugged device is identified, but not onlined. 104 * Enabling (AP_CONNECT) device port with an attached device does not result 105 * in device onlining regardless of the flag setting 106 */ 107 int sata_auto_online = 0; 108 109 #ifdef SATA_DEBUG 110 111 #define SATA_LOG_D(args) sata_log args 112 uint64_t mbuf_count = 0; 113 uint64_t mbuffail_count = 0; 114 115 sata_atapi_cmd_t sata_atapi_trace[64]; 116 uint32_t sata_atapi_trace_index = 0; 117 int sata_atapi_trace_save = 1; 118 static void sata_save_atapi_trace(sata_pkt_txlate_t *, int); 119 #define SATAATAPITRACE(spx, count) if (sata_atapi_trace_save) \ 120 sata_save_atapi_trace(spx, count); 121 122 #else 123 #define SATA_LOG_D(args) sata_trace_log args 124 #define SATAATAPITRACE(spx, count) 125 #endif 126 127 #if 0 128 static void 129 sata_test_atapi_packet_command(sata_hba_inst_t *, int); 130 #endif 131 132 #ifdef SATA_INJECT_FAULTS 133 134 #define SATA_INJECT_PKT_FAULT 1 135 uint32_t sata_inject_fault = 0; 136 137 uint32_t sata_inject_fault_count = 0; 138 uint32_t sata_inject_fault_pause_count = 0; 139 uint32_t sata_fault_type = 0; 140 uint32_t sata_fault_cmd = 0; 141 dev_info_t *sata_fault_ctrl = NULL; 142 sata_device_t sata_fault_device; 143 144 static void sata_inject_pkt_fault(sata_pkt_t *, int *, int); 145 146 #endif 147 148 #define LEGACY_HWID_LEN 64 /* Model (40) + Serial (20) + pad */ 149 150 static char sata_rev_tag[] = {"1.46"}; 151 152 /* 153 * SATA cb_ops functions 154 */ 155 static int sata_hba_open(dev_t *, int, int, cred_t *); 156 static int sata_hba_close(dev_t, int, int, cred_t *); 157 static int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 158 159 /* 160 * SCSA required entry points 161 */ 162 static int sata_scsi_tgt_init(dev_info_t *, dev_info_t *, 163 scsi_hba_tran_t *, struct scsi_device *); 164 static int sata_scsi_tgt_probe(struct scsi_device *, 165 int (*callback)(void)); 166 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *, 167 scsi_hba_tran_t *, struct scsi_device *); 168 static int sata_scsi_start(struct scsi_address *, struct scsi_pkt *); 169 static int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *); 170 static int sata_scsi_reset(struct scsi_address *, int); 171 static int sata_scsi_getcap(struct scsi_address *, char *, int); 172 static int sata_scsi_setcap(struct scsi_address *, char *, int, int); 173 static struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *, 174 struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t), 175 caddr_t); 176 static void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *); 177 static void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *); 178 static void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *); 179 180 /* 181 * SATA HBA interface functions are defined in sata_hba.h header file 182 */ 183 184 /* Event processing functions */ 185 static void sata_event_daemon(void *); 186 static void sata_event_thread_control(int); 187 static void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst); 188 static void sata_process_pmult_events(sata_hba_inst_t *, uint8_t); 189 static void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *); 190 static void sata_process_pmdevice_reset(sata_hba_inst_t *, sata_address_t *); 191 static void sata_process_port_failed_event(sata_hba_inst_t *, 192 sata_address_t *); 193 static void sata_process_port_link_events(sata_hba_inst_t *, 194 sata_address_t *); 195 static void sata_process_pmport_link_events(sata_hba_inst_t *, 196 sata_address_t *); 197 static void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *); 198 static void sata_process_pmdevice_detached(sata_hba_inst_t *, 199 sata_address_t *); 200 static void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *); 201 static void sata_process_pmdevice_attached(sata_hba_inst_t *, 202 sata_address_t *); 203 static void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *); 204 static void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *); 205 static void sata_process_target_node_cleanup(sata_hba_inst_t *, 206 sata_address_t *); 207 static void sata_process_device_autoonline(sata_hba_inst_t *, 208 sata_address_t *saddr); 209 210 /* 211 * Local translation functions 212 */ 213 static int sata_txlt_inquiry(sata_pkt_txlate_t *); 214 static int sata_txlt_test_unit_ready(sata_pkt_txlate_t *); 215 static int sata_txlt_start_stop_unit(sata_pkt_txlate_t *); 216 static int sata_txlt_read_capacity(sata_pkt_txlate_t *); 217 static int sata_txlt_read_capacity16(sata_pkt_txlate_t *); 218 static int sata_txlt_unmap(sata_pkt_txlate_t *); 219 static int sata_txlt_request_sense(sata_pkt_txlate_t *); 220 static int sata_txlt_read(sata_pkt_txlate_t *); 221 static int sata_txlt_write(sata_pkt_txlate_t *); 222 static int sata_txlt_log_sense(sata_pkt_txlate_t *); 223 static int sata_txlt_log_select(sata_pkt_txlate_t *); 224 static int sata_txlt_mode_sense(sata_pkt_txlate_t *); 225 static int sata_txlt_mode_select(sata_pkt_txlate_t *); 226 static int sata_txlt_ata_pass_thru(sata_pkt_txlate_t *); 227 static int sata_txlt_synchronize_cache(sata_pkt_txlate_t *); 228 static int sata_txlt_write_buffer(sata_pkt_txlate_t *); 229 static int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *); 230 231 static int sata_hba_start(sata_pkt_txlate_t *, int *); 232 static int sata_txlt_invalid_command(sata_pkt_txlate_t *); 233 static int sata_txlt_check_condition(sata_pkt_txlate_t *, uchar_t, uchar_t); 234 static int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *); 235 static int sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *); 236 static int sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *); 237 static void sata_txlt_rw_completion(sata_pkt_t *); 238 static void sata_txlt_nodata_cmd_completion(sata_pkt_t *); 239 static void sata_txlt_apt_completion(sata_pkt_t *sata_pkt); 240 static void sata_txlt_unmap_completion(sata_pkt_t *sata_pkt); 241 static void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *); 242 static int sata_emul_rw_completion(sata_pkt_txlate_t *); 243 static void sata_fill_ata_return_desc(sata_pkt_t *, uint8_t, uint8_t, 244 uint8_t); 245 static struct scsi_extended_sense *sata_immediate_error_response( 246 sata_pkt_txlate_t *, int); 247 static struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *); 248 249 static int sata_txlt_atapi(sata_pkt_txlate_t *); 250 static void sata_txlt_atapi_completion(sata_pkt_t *); 251 252 /* 253 * Local functions for ioctl 254 */ 255 static int32_t sata_get_port_num(sata_hba_inst_t *, struct devctl_iocdata *); 256 static void sata_cfgadm_state(sata_hba_inst_t *, int32_t, 257 devctl_ap_state_t *); 258 static dev_info_t *sata_get_target_dip(dev_info_t *, uint8_t, uint8_t); 259 static dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *); 260 static dev_info_t *sata_devt_to_devinfo(dev_t); 261 static int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *); 262 static int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *); 263 static int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *); 264 static int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *); 265 static int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *); 266 static int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *); 267 static int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *); 268 static int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *); 269 static int sata_ioctl_reset_all(sata_hba_inst_t *); 270 static int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *); 271 static int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *, 272 sata_ioctl_data_t *, int mode); 273 static int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *, 274 sata_ioctl_data_t *, int mode); 275 static int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *, 276 sata_ioctl_data_t *, int mode); 277 static int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *, 278 sata_ioctl_data_t *, int mode); 279 static int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *, 280 sata_device_t *, sata_ioctl_data_t *, int mode); 281 282 /* 283 * Local functions 284 */ 285 static void sata_remove_hba_instance(dev_info_t *); 286 static int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *); 287 static void sata_probe_ports(sata_hba_inst_t *); 288 static void sata_probe_pmports(sata_hba_inst_t *, uint8_t); 289 static int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int); 290 static int sata_reprobe_pmult(sata_hba_inst_t *, sata_device_t *, int); 291 static int sata_reprobe_pmport(sata_hba_inst_t *, sata_device_t *, int); 292 static int sata_alloc_pmult(sata_hba_inst_t *, sata_device_t *); 293 static void sata_free_pmult(sata_hba_inst_t *, sata_device_t *); 294 static int sata_add_device(dev_info_t *, sata_hba_inst_t *, sata_device_t *); 295 static int sata_offline_device(sata_hba_inst_t *, sata_device_t *, 296 sata_drive_info_t *); 297 static dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *, 298 sata_address_t *); 299 static void sata_remove_target_node(sata_hba_inst_t *, 300 sata_address_t *); 301 static int sata_validate_scsi_address(sata_hba_inst_t *, 302 struct scsi_address *, sata_device_t *); 303 static int sata_validate_sata_address(sata_hba_inst_t *, int, int, int); 304 static sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t)); 305 static void sata_pkt_free(sata_pkt_txlate_t *); 306 static int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t), 307 caddr_t, ddi_dma_attr_t *); 308 static void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *); 309 static int sata_probe_device(sata_hba_inst_t *, sata_device_t *); 310 static sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *, 311 sata_device_t *); 312 static int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *); 313 static void sata_reidentify_device(sata_pkt_txlate_t *); 314 static struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int); 315 static void sata_free_local_buffer(sata_pkt_txlate_t *); 316 static uint64_t sata_check_capacity(sata_drive_info_t *); 317 void sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *, 318 ddi_dma_attr_t *); 319 static int sata_fetch_device_identify_data(sata_hba_inst_t *, 320 sata_drive_info_t *); 321 static void sata_update_port_info(sata_hba_inst_t *, sata_device_t *); 322 static void sata_update_pmport_info(sata_hba_inst_t *, sata_device_t *); 323 static void sata_update_port_scr(sata_port_scr_t *, sata_device_t *); 324 static int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *); 325 static int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int); 326 static int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int); 327 static int sata_set_drive_features(sata_hba_inst_t *, 328 sata_drive_info_t *, int flag); 329 static void sata_init_write_cache_mode(sata_drive_info_t *sdinfo); 330 static int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *); 331 static void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *, 332 uint8_t *); 333 static int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *, 334 struct scsi_inquiry *); 335 static int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *); 336 static int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *); 337 static int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *); 338 static int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *); 339 static int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *); 340 static int sata_mode_select_page_8(sata_pkt_txlate_t *, 341 struct mode_cache_scsi3 *, int, int *, int *, int *); 342 static int sata_mode_select_page_1a(sata_pkt_txlate_t *, 343 struct mode_info_power_cond *, int, int *, int *, int *); 344 static int sata_mode_select_page_1c(sata_pkt_txlate_t *, 345 struct mode_info_excpt_page *, int, int *, int *, int *); 346 static int sata_mode_select_page_30(sata_pkt_txlate_t *, 347 struct mode_acoustic_management *, int, int *, int *, int *); 348 349 static int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *); 350 static int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *, 351 sata_hba_inst_t *); 352 static int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *, 353 sata_hba_inst_t *); 354 static int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *, 355 sata_hba_inst_t *); 356 static int sata_build_lsense_page_0e(sata_drive_info_t *, uint8_t *, 357 sata_pkt_txlate_t *); 358 359 static void sata_set_arq_data(sata_pkt_t *); 360 static void sata_build_read_verify_cmd(sata_cmd_t *, uint16_t, uint64_t); 361 static void sata_build_generic_cmd(sata_cmd_t *, uint8_t); 362 static uint8_t sata_get_standby_timer(uint8_t *timer); 363 364 static void sata_save_drive_settings(sata_drive_info_t *); 365 static void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *); 366 static void sata_show_pmult_info(sata_hba_inst_t *, sata_device_t *); 367 static void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...); 368 static void sata_trace_log(sata_hba_inst_t *, uint_t, const char *fmt, ...); 369 static int sata_fetch_smart_return_status(sata_hba_inst_t *, 370 sata_drive_info_t *); 371 static int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *, 372 struct smart_data *); 373 static int sata_smart_selftest_log(sata_hba_inst_t *, 374 sata_drive_info_t *, 375 struct smart_selftest_log *); 376 static int sata_ext_smart_selftest_read_log(sata_hba_inst_t *, 377 sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t); 378 static int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *, 379 uint8_t *, uint8_t, uint8_t); 380 static int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *, 381 struct read_log_ext_directory *); 382 static void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int); 383 static void sata_xlate_errors(sata_pkt_txlate_t *); 384 static void sata_decode_device_error(sata_pkt_txlate_t *, 385 struct scsi_extended_sense *); 386 static void sata_set_device_removed(dev_info_t *); 387 static boolean_t sata_check_device_removed(dev_info_t *); 388 static void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *); 389 static int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *, 390 sata_drive_info_t *); 391 static int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *, 392 sata_drive_info_t *); 393 static void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *); 394 static void sata_fixed_sense_data_preset(struct scsi_extended_sense *); 395 static void sata_target_devid_register(dev_info_t *, sata_drive_info_t *); 396 static int sata_check_modser(char *, int); 397 398 /* 399 * FMA 400 */ 401 static boolean_t sata_check_for_dma_error(dev_info_t *, sata_pkt_txlate_t *); 402 403 404 /* 405 * SATA Framework will ignore SATA HBA driver cb_ops structure and 406 * register following one with SCSA framework. 407 * Open & close are provided, so scsi framework will not use its own 408 */ 409 static struct cb_ops sata_cb_ops = { 410 sata_hba_open, /* open */ 411 sata_hba_close, /* close */ 412 nodev, /* strategy */ 413 nodev, /* print */ 414 nodev, /* dump */ 415 nodev, /* read */ 416 nodev, /* write */ 417 sata_hba_ioctl, /* ioctl */ 418 nodev, /* devmap */ 419 nodev, /* mmap */ 420 nodev, /* segmap */ 421 nochpoll, /* chpoll */ 422 ddi_prop_op, /* cb_prop_op */ 423 0, /* streamtab */ 424 D_NEW | D_MP, /* cb_flag */ 425 CB_REV, /* rev */ 426 nodev, /* aread */ 427 nodev /* awrite */ 428 }; 429 430 431 extern struct mod_ops mod_miscops; 432 extern uchar_t scsi_cdb_size[]; 433 434 static struct modlmisc modlmisc = { 435 &mod_miscops, /* Type of module */ 436 "SATA Module" /* module name */ 437 }; 438 439 440 static struct modlinkage modlinkage = { 441 MODREV_1, 442 (void *)&modlmisc, 443 NULL 444 }; 445 446 /* 447 * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero, 448 * i.e. when scsi_pkt has not timeout specified. 449 */ 450 static int sata_default_pkt_time = 60; /* 60 seconds */ 451 452 /* 453 * Intermediate buffer device access attributes - they are required, 454 * but not necessarily used. 455 */ 456 static ddi_device_acc_attr_t sata_acc_attr = { 457 DDI_DEVICE_ATTR_V0, 458 DDI_STRUCTURE_LE_ACC, 459 DDI_STRICTORDER_ACC 460 }; 461 462 463 /* 464 * Mutexes protecting structures in multithreaded operations. 465 * Because events are relatively rare, a single global mutex protecting 466 * data structures should be sufficient. To increase performance, add 467 * separate mutex per each sata port and use global mutex only to protect 468 * common data structures. 469 */ 470 static kmutex_t sata_mutex; /* protects sata_hba_list */ 471 static kmutex_t sata_log_mutex; /* protects log */ 472 473 static char sata_log_buf[256]; 474 475 /* 476 * sata trace debug 477 */ 478 static sata_trace_rbuf_t *sata_debug_rbuf; 479 static sata_trace_dmsg_t *sata_trace_dmsg_alloc(void); 480 static void sata_trace_dmsg_free(void); 481 static void sata_trace_rbuf_alloc(void); 482 static void sata_trace_rbuf_free(void); 483 484 int dmsg_ring_size = DMSG_RING_SIZE; 485 486 /* Default write cache setting for SATA hard disks */ 487 int sata_write_cache = 1; /* enabled */ 488 489 /* Default write cache setting for SATA ATAPI CD/DVD */ 490 int sata_atapicdvd_write_cache = 1; /* enabled */ 491 492 /* Default write cache setting for SATA ATAPI tape */ 493 int sata_atapitape_write_cache = 1; /* enabled */ 494 495 /* Default write cache setting for SATA ATAPI disk */ 496 int sata_atapidisk_write_cache = 1; /* enabled */ 497 498 /* 499 * Linked list of HBA instances 500 */ 501 static sata_hba_inst_t *sata_hba_list = NULL; 502 static sata_hba_inst_t *sata_hba_list_tail = NULL; 503 /* 504 * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran 505 * structure and in sata soft state. 506 */ 507 508 /* 509 * Event daemon related variables 510 */ 511 static kmutex_t sata_event_mutex; 512 static kcondvar_t sata_event_cv; 513 static kthread_t *sata_event_thread = NULL; 514 static int sata_event_thread_terminate = 0; 515 static int sata_event_pending = 0; 516 static int sata_event_thread_active = 0; 517 extern pri_t minclsyspri; 518 519 /* 520 * NCQ error recovery command 521 */ 522 static const sata_cmd_t sata_rle_cmd = { 523 SATA_CMD_REV, 524 NULL, 525 { 526 SATA_DIR_READ 527 }, 528 ATA_ADDR_LBA48, 529 0, 530 0, 531 0, 532 0, 533 0, 534 1, 535 READ_LOG_EXT_NCQ_ERROR_RECOVERY, 536 0, 537 0, 538 0, 539 SATAC_READ_LOG_EXT, 540 0, 541 0, 542 0, 543 }; 544 545 /* 546 * ATAPI error recovery CDB 547 */ 548 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = { 549 SCMD_REQUEST_SENSE, 550 0, /* Only fixed RQ format is supported */ 551 0, 552 0, 553 SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */ 554 0 555 }; 556 557 558 /* Warlock directives */ 559 560 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran)) 561 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device)) 562 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops)) 563 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense)) 564 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status)) 565 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr)) 566 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t)) 567 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state)) 568 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state)) 569 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list)) 570 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list)) 571 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next)) 572 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev)) 573 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \ 574 sata_hba_inst::satahba_scsi_tran)) 575 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran)) 576 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip)) 577 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached)) 578 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port)) 579 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex, 580 sata_hba_inst::satahba_event_flags)) 581 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 582 sata_cport_info::cport_devp)) 583 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp)) 584 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr)) 585 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 586 sata_cport_info::cport_dev_type)) 587 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type)) 588 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 589 sata_cport_info::cport_state)) 590 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state)) 591 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 592 sata_pmport_info::pmport_state)) 593 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state)) 594 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 595 sata_pmport_info::pmport_dev_type)) 596 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type)) 597 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 598 sata_pmport_info::pmport_sata_drive)) 599 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 600 sata_pmport_info::pmport_tgtnode_clean)) 601 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 602 sata_pmport_info::pmport_event_flags)) 603 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive)) 604 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port)) 605 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports)) 606 #ifdef SATA_DEBUG 607 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count)) 608 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count)) 609 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace)) 610 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index)) 611 #endif 612 613 /* End of warlock directives */ 614 615 /* ************** loadable module configuration functions ************** */ 616 617 int 618 _init() 619 { 620 int rval; 621 622 mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL); 623 mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL); 624 mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL); 625 cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL); 626 sata_trace_rbuf_alloc(); 627 if ((rval = mod_install(&modlinkage)) != 0) { 628 #ifdef SATA_DEBUG 629 cmn_err(CE_WARN, "sata: _init: mod_install failed\n"); 630 #endif 631 sata_trace_rbuf_free(); 632 mutex_destroy(&sata_log_mutex); 633 cv_destroy(&sata_event_cv); 634 mutex_destroy(&sata_event_mutex); 635 mutex_destroy(&sata_mutex); 636 } 637 return (rval); 638 } 639 640 int 641 _fini() 642 { 643 int rval; 644 645 if ((rval = mod_remove(&modlinkage)) != 0) 646 return (rval); 647 648 sata_trace_rbuf_free(); 649 mutex_destroy(&sata_log_mutex); 650 cv_destroy(&sata_event_cv); 651 mutex_destroy(&sata_event_mutex); 652 mutex_destroy(&sata_mutex); 653 return (rval); 654 } 655 656 int 657 _info(struct modinfo *modinfop) 658 { 659 return (mod_info(&modlinkage, modinfop)); 660 } 661 662 663 664 /* ********************* SATA HBA entry points ********************* */ 665 666 667 /* 668 * Called by SATA HBA from _init(). 669 * Registers HBA driver instance/sata framework pair with scsi framework, by 670 * calling scsi_hba_init(). 671 * 672 * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used 673 * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver 674 * cb_ops pointer in SATA HBA driver dev_ops structure. 675 * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors. 676 * 677 * Return status of the scsi_hba_init() is returned to a calling SATA HBA 678 * driver. 679 */ 680 int 681 sata_hba_init(struct modlinkage *modlp) 682 { 683 int rval; 684 struct dev_ops *hba_ops; 685 686 SATADBG1(SATA_DBG_HBA_IF, NULL, 687 "sata_hba_init: name %s \n", 688 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo); 689 /* 690 * Fill-up cb_ops and dev_ops when necessary 691 */ 692 hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops; 693 /* 694 * Provide pointer to SATA dev_ops 695 */ 696 hba_ops->devo_cb_ops = &sata_cb_ops; 697 698 /* 699 * Register SATA HBA with SCSI framework 700 */ 701 if ((rval = scsi_hba_init(modlp)) != 0) { 702 SATADBG1(SATA_DBG_HBA_IF, NULL, 703 "sata_hba_init: scsi hba init failed\n", NULL); 704 return (rval); 705 } 706 707 return (0); 708 } 709 710 711 /* HBA attach stages */ 712 #define HBA_ATTACH_STAGE_SATA_HBA_INST 1 713 #define HBA_ATTACH_STAGE_SCSI_ATTACHED 2 714 #define HBA_ATTACH_STAGE_SETUP 4 715 #define HBA_ATTACH_STAGE_LINKED 8 716 717 718 /* 719 * 720 * Called from SATA HBA driver's attach routine to attach an instance of 721 * the HBA. 722 * 723 * For DDI_ATTACH command: 724 * sata_hba_inst structure is allocated here and initialized with pointers to 725 * SATA framework implementation of required scsi tran functions. 726 * The scsi_tran's tran_hba_private field is used by SATA Framework to point 727 * to the soft structure (sata_hba_inst) allocated by SATA framework for 728 * SATA HBA instance related data. 729 * The scsi_tran's tran_hba_private field is used by SATA framework to 730 * store a pointer to per-HBA-instance of sata_hba_inst structure. 731 * The sata_hba_inst structure is cross-linked to scsi tran structure. 732 * Among other info, a pointer to sata_hba_tran structure is stored in 733 * sata_hba_inst. The sata_hba_inst structures for different HBA instances are 734 * linked together into the list, pointed to by sata_hba_list. 735 * On the first HBA instance attach the sata event thread is initialized. 736 * Attachment points are created for all SATA ports of the HBA being attached. 737 * All HBA instance's SATA ports are probed and type of plugged devices is 738 * determined. For each device of a supported type, a target node is created. 739 * 740 * DDI_SUCCESS is returned when attachment process is successful, 741 * DDI_FAILURE is returned otherwise. 742 * 743 * For DDI_RESUME command: 744 * Not implemented at this time (postponed until phase 2 of the development). 745 */ 746 int 747 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran, 748 ddi_attach_cmd_t cmd) 749 { 750 sata_hba_inst_t *sata_hba_inst; 751 scsi_hba_tran_t *scsi_tran = NULL; 752 int hba_attach_state = 0; 753 char taskq_name[MAXPATHLEN]; 754 755 SATADBG3(SATA_DBG_HBA_IF, NULL, 756 "sata_hba_attach: node %s (%s%d)\n", 757 ddi_node_name(dip), ddi_driver_name(dip), 758 ddi_get_instance(dip)); 759 760 if (cmd == DDI_RESUME) { 761 /* 762 * Postponed until phase 2 of the development 763 */ 764 return (DDI_FAILURE); 765 } 766 767 if (cmd != DDI_ATTACH) { 768 return (DDI_FAILURE); 769 } 770 771 /* cmd == DDI_ATTACH */ 772 773 if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) { 774 SATA_LOG_D((NULL, CE_WARN, 775 "sata_hba_attach: invalid sata_hba_tran")); 776 return (DDI_FAILURE); 777 } 778 /* 779 * Allocate and initialize SCSI tran structure. 780 * SATA copy of tran_bus_config is provided to create port nodes. 781 */ 782 scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP); 783 if (scsi_tran == NULL) 784 return (DDI_FAILURE); 785 /* 786 * Allocate soft structure for SATA HBA instance. 787 * There is a separate softstate for each HBA instance. 788 */ 789 sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP); 790 ASSERT(sata_hba_inst != NULL); /* this should not fail */ 791 mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL); 792 hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST; 793 794 /* 795 * scsi_trans's tran_hba_private is used by SATA Framework to point to 796 * soft structure allocated by SATA framework for 797 * SATA HBA instance related data. 798 */ 799 scsi_tran->tran_hba_private = sata_hba_inst; 800 scsi_tran->tran_tgt_private = NULL; 801 802 scsi_tran->tran_tgt_init = sata_scsi_tgt_init; 803 scsi_tran->tran_tgt_probe = sata_scsi_tgt_probe; 804 scsi_tran->tran_tgt_free = sata_scsi_tgt_free; 805 806 scsi_tran->tran_start = sata_scsi_start; 807 scsi_tran->tran_reset = sata_scsi_reset; 808 scsi_tran->tran_abort = sata_scsi_abort; 809 scsi_tran->tran_getcap = sata_scsi_getcap; 810 scsi_tran->tran_setcap = sata_scsi_setcap; 811 scsi_tran->tran_init_pkt = sata_scsi_init_pkt; 812 scsi_tran->tran_destroy_pkt = sata_scsi_destroy_pkt; 813 814 scsi_tran->tran_dmafree = sata_scsi_dmafree; 815 scsi_tran->tran_sync_pkt = sata_scsi_sync_pkt; 816 817 scsi_tran->tran_reset_notify = NULL; 818 scsi_tran->tran_get_bus_addr = NULL; 819 scsi_tran->tran_quiesce = NULL; 820 scsi_tran->tran_unquiesce = NULL; 821 scsi_tran->tran_bus_reset = NULL; 822 823 if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr, 824 scsi_tran, 0) != DDI_SUCCESS) { 825 #ifdef SATA_DEBUG 826 cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed", 827 ddi_driver_name(dip), ddi_get_instance(dip)); 828 #endif 829 goto fail; 830 } 831 hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED; 832 833 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) { 834 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, 835 "sata", 1) != DDI_PROP_SUCCESS) { 836 SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: " 837 "failed to create hba sata prop")); 838 goto fail; 839 } 840 } 841 842 /* 843 * Save pointers in hba instance soft state. 844 */ 845 sata_hba_inst->satahba_scsi_tran = scsi_tran; 846 sata_hba_inst->satahba_tran = sata_tran; 847 sata_hba_inst->satahba_dip = dip; 848 849 /* 850 * Create a task queue to handle emulated commands completion 851 * Use node name, dash, instance number as the queue name. 852 */ 853 taskq_name[0] = '\0'; 854 (void) strlcat(taskq_name, DEVI(dip)->devi_node_name, 855 sizeof (taskq_name)); 856 (void) snprintf(taskq_name + strlen(taskq_name), 857 sizeof (taskq_name) - strlen(taskq_name), 858 "-%d", DEVI(dip)->devi_instance); 859 sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1, 860 minclsyspri, 1, sata_tran->sata_tran_hba_num_cports * 4, 861 TASKQ_DYNAMIC); 862 863 hba_attach_state |= HBA_ATTACH_STAGE_SETUP; 864 865 /* 866 * Create events thread if not created yet. 867 */ 868 sata_event_thread_control(1); 869 870 /* 871 * Link this hba instance into the list. 872 */ 873 mutex_enter(&sata_mutex); 874 875 if (sata_hba_list == NULL) { 876 /* 877 * The first instance of HBA is attached. 878 * Set current/active default maximum NCQ/TCQ queue depth for 879 * all SATA devices. It is done here and now, to eliminate the 880 * possibility of the dynamic, programatic modification of the 881 * queue depth via global (and public) sata_max_queue_depth 882 * variable (this would require special handling in HBA drivers) 883 */ 884 sata_current_max_qdepth = sata_max_queue_depth; 885 if (sata_current_max_qdepth > 32) 886 sata_current_max_qdepth = 32; 887 else if (sata_current_max_qdepth < 1) 888 sata_current_max_qdepth = 1; 889 } 890 891 sata_hba_inst->satahba_next = NULL; 892 sata_hba_inst->satahba_prev = sata_hba_list_tail; 893 if (sata_hba_list == NULL) { 894 sata_hba_list = sata_hba_inst; 895 } 896 if (sata_hba_list_tail != NULL) { 897 sata_hba_list_tail->satahba_next = sata_hba_inst; 898 } 899 sata_hba_list_tail = sata_hba_inst; 900 mutex_exit(&sata_mutex); 901 hba_attach_state |= HBA_ATTACH_STAGE_LINKED; 902 903 /* 904 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl 905 * SATA HBA driver should not use its own open/close entry points. 906 * 907 * Make sure that instance number doesn't overflow 908 * when forming minor numbers. 909 */ 910 ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT)); 911 if (ddi_create_minor_node(dip, "devctl", S_IFCHR, 912 INST2DEVCTL(ddi_get_instance(dip)), 913 DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) { 914 #ifdef SATA_DEBUG 915 cmn_err(CE_WARN, "sata_hba_attach: " 916 "cannot create devctl minor node"); 917 #endif 918 goto fail; 919 } 920 921 922 /* 923 * Set-up kstats here, if necessary. 924 * (postponed until future phase of the development). 925 */ 926 927 /* 928 * Indicate that HBA is attached. This will enable events processing 929 * for this HBA. 930 */ 931 sata_hba_inst->satahba_attached = 1; 932 /* 933 * Probe controller ports. This operation will describe a current 934 * controller/port/multipliers/device configuration and will create 935 * attachment points. 936 * We may end-up with just a controller with no devices attached. 937 * For the ports with a supported device attached, device target nodes 938 * are created and devices are initialized. 939 */ 940 sata_probe_ports(sata_hba_inst); 941 942 return (DDI_SUCCESS); 943 944 fail: 945 if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) { 946 (void) sata_remove_hba_instance(dip); 947 if (sata_hba_list == NULL) 948 sata_event_thread_control(0); 949 } 950 951 if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) { 952 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata"); 953 taskq_destroy(sata_hba_inst->satahba_taskq); 954 } 955 956 if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED) 957 (void) scsi_hba_detach(dip); 958 959 if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) { 960 mutex_destroy(&sata_hba_inst->satahba_mutex); 961 kmem_free((void *)sata_hba_inst, 962 sizeof (struct sata_hba_inst)); 963 scsi_hba_tran_free(scsi_tran); 964 } 965 966 sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed", 967 ddi_driver_name(dip), ddi_get_instance(dip)); 968 969 return (DDI_FAILURE); 970 } 971 972 973 /* 974 * Called by SATA HBA from to detach an instance of the driver. 975 * 976 * For DDI_DETACH command: 977 * Free local structures allocated for SATA HBA instance during 978 * sata_hba_attach processing. 979 * 980 * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise. 981 * 982 * For DDI_SUSPEND command: 983 * Not implemented at this time (postponed until phase 2 of the development) 984 * Returnd DDI_SUCCESS. 985 * 986 * When the last HBA instance is detached, the event daemon is terminated. 987 * 988 * NOTE: Port multiplier is supported. 989 */ 990 int 991 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 992 { 993 dev_info_t *tdip; 994 sata_hba_inst_t *sata_hba_inst; 995 scsi_hba_tran_t *scsi_hba_tran; 996 sata_cport_info_t *cportinfo; 997 sata_pmult_info_t *pminfo; 998 sata_drive_info_t *sdinfo; 999 sata_device_t sdevice; 1000 int ncport, npmport; 1001 1002 SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n", 1003 ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip)); 1004 1005 switch (cmd) { 1006 case DDI_DETACH: 1007 1008 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1009 return (DDI_FAILURE); 1010 1011 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1012 if (sata_hba_inst == NULL) 1013 return (DDI_FAILURE); 1014 1015 if (scsi_hba_detach(dip) == DDI_FAILURE) { 1016 sata_hba_inst->satahba_attached = 1; 1017 return (DDI_FAILURE); 1018 } 1019 1020 /* 1021 * Free all target nodes - at this point 1022 * devices should be at least offlined 1023 * otherwise scsi_hba_detach() should not be called. 1024 */ 1025 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); 1026 ncport++) { 1027 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 1028 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 1029 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 1030 if (sdinfo != NULL) { 1031 tdip = sata_get_target_dip(dip, 1032 ncport, 0); 1033 if (tdip != NULL) { 1034 if (ndi_devi_offline(tdip, 1035 NDI_DEVI_REMOVE) != 1036 NDI_SUCCESS) { 1037 SATA_LOG_D(( 1038 sata_hba_inst, 1039 CE_WARN, 1040 "sata_hba_detach: " 1041 "Target node not " 1042 "removed !")); 1043 return (DDI_FAILURE); 1044 } 1045 } 1046 } 1047 } else { /* SATA_DTYPE_PMULT */ 1048 mutex_enter(&cportinfo->cport_mutex); 1049 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 1050 1051 if (pminfo == NULL) { 1052 SATA_LOG_D((sata_hba_inst, CE_WARN, 1053 "sata_hba_detach: Port multiplier " 1054 "not ready yet!")); 1055 mutex_exit(&cportinfo->cport_mutex); 1056 return (DDI_FAILURE); 1057 } 1058 1059 /* 1060 * Detach would fail if removal of any of the 1061 * target nodes is failed - albeit in that 1062 * case some of them may have been removed. 1063 */ 1064 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 1065 sata_hba_inst, ncport); npmport++) { 1066 tdip = sata_get_target_dip(dip, ncport, 1067 npmport); 1068 if (tdip != NULL) { 1069 if (ndi_devi_offline(tdip, 1070 NDI_DEVI_REMOVE) != 1071 NDI_SUCCESS) { 1072 SATA_LOG_D(( 1073 sata_hba_inst, 1074 CE_WARN, 1075 "sata_hba_detach: " 1076 "Target node not " 1077 "removed !")); 1078 mutex_exit(&cportinfo-> 1079 cport_mutex); 1080 return (DDI_FAILURE); 1081 } 1082 } 1083 } 1084 mutex_exit(&cportinfo->cport_mutex); 1085 } 1086 } 1087 /* 1088 * Disable sata event daemon processing for this HBA 1089 */ 1090 sata_hba_inst->satahba_attached = 0; 1091 1092 /* 1093 * Remove event daemon thread, if it is last HBA instance. 1094 */ 1095 1096 mutex_enter(&sata_mutex); 1097 if (sata_hba_list->satahba_next == NULL) { 1098 mutex_exit(&sata_mutex); 1099 sata_event_thread_control(0); 1100 mutex_enter(&sata_mutex); 1101 } 1102 mutex_exit(&sata_mutex); 1103 1104 /* Remove this HBA instance from the HBA list */ 1105 sata_remove_hba_instance(dip); 1106 1107 /* 1108 * At this point there should be no target nodes attached. 1109 * Detach and destroy device and port info structures. 1110 */ 1111 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); 1112 ncport++) { 1113 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 1114 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 1115 sdinfo = 1116 cportinfo->cport_devp.cport_sata_drive; 1117 if (sdinfo != NULL) { 1118 /* Release device structure */ 1119 kmem_free(sdinfo, 1120 sizeof (sata_drive_info_t)); 1121 } 1122 /* Release cport info */ 1123 mutex_destroy(&cportinfo->cport_mutex); 1124 kmem_free(cportinfo, 1125 sizeof (sata_cport_info_t)); 1126 } else { /* SATA_DTYPE_PMULT */ 1127 sdevice.satadev_addr.cport = (uint8_t)ncport; 1128 sdevice.satadev_addr.qual = SATA_ADDR_PMULT; 1129 sata_free_pmult(sata_hba_inst, &sdevice); 1130 } 1131 } 1132 1133 scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran); 1134 1135 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata"); 1136 1137 taskq_destroy(sata_hba_inst->satahba_taskq); 1138 1139 mutex_destroy(&sata_hba_inst->satahba_mutex); 1140 kmem_free((void *)sata_hba_inst, 1141 sizeof (struct sata_hba_inst)); 1142 1143 return (DDI_SUCCESS); 1144 1145 case DDI_SUSPEND: 1146 /* 1147 * Postponed until phase 2 1148 */ 1149 return (DDI_FAILURE); 1150 1151 default: 1152 return (DDI_FAILURE); 1153 } 1154 } 1155 1156 1157 /* 1158 * Called by an HBA drive from _fini() routine. 1159 * Unregisters SATA HBA instance/SATA framework pair from the scsi framework. 1160 */ 1161 void 1162 sata_hba_fini(struct modlinkage *modlp) 1163 { 1164 SATADBG1(SATA_DBG_HBA_IF, NULL, 1165 "sata_hba_fini: name %s\n", 1166 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo); 1167 1168 scsi_hba_fini(modlp); 1169 } 1170 1171 1172 /* 1173 * Default open and close routine for sata_hba framework. 1174 * 1175 */ 1176 /* 1177 * Open devctl node. 1178 * 1179 * Returns: 1180 * 0 if node was open successfully, error code otherwise. 1181 * 1182 * 1183 */ 1184 1185 static int 1186 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp) 1187 { 1188 #ifndef __lock_lint 1189 _NOTE(ARGUNUSED(credp)) 1190 #endif 1191 int rv = 0; 1192 dev_info_t *dip; 1193 scsi_hba_tran_t *scsi_hba_tran; 1194 sata_hba_inst_t *sata_hba_inst; 1195 1196 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL); 1197 1198 if (otyp != OTYP_CHR) 1199 return (EINVAL); 1200 1201 dip = sata_devt_to_devinfo(*devp); 1202 if (dip == NULL) 1203 return (ENXIO); 1204 1205 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1206 return (ENXIO); 1207 1208 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1209 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0) 1210 return (ENXIO); 1211 1212 mutex_enter(&sata_mutex); 1213 if (flags & FEXCL) { 1214 if (sata_hba_inst->satahba_open_flag != 0) { 1215 rv = EBUSY; 1216 } else { 1217 sata_hba_inst->satahba_open_flag = 1218 SATA_DEVCTL_EXOPENED; 1219 } 1220 } else { 1221 if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) { 1222 rv = EBUSY; 1223 } else { 1224 sata_hba_inst->satahba_open_flag = 1225 SATA_DEVCTL_SOPENED; 1226 } 1227 } 1228 mutex_exit(&sata_mutex); 1229 1230 return (rv); 1231 } 1232 1233 1234 /* 1235 * Close devctl node. 1236 * Returns: 1237 * 0 if node was closed successfully, error code otherwise. 1238 * 1239 */ 1240 1241 static int 1242 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp) 1243 { 1244 #ifndef __lock_lint 1245 _NOTE(ARGUNUSED(credp)) 1246 _NOTE(ARGUNUSED(flag)) 1247 #endif 1248 dev_info_t *dip; 1249 scsi_hba_tran_t *scsi_hba_tran; 1250 sata_hba_inst_t *sata_hba_inst; 1251 1252 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL); 1253 1254 if (otyp != OTYP_CHR) 1255 return (EINVAL); 1256 1257 dip = sata_devt_to_devinfo(dev); 1258 if (dip == NULL) 1259 return (ENXIO); 1260 1261 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1262 return (ENXIO); 1263 1264 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1265 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0) 1266 return (ENXIO); 1267 1268 mutex_enter(&sata_mutex); 1269 sata_hba_inst->satahba_open_flag = 0; 1270 mutex_exit(&sata_mutex); 1271 return (0); 1272 } 1273 1274 1275 1276 /* 1277 * Standard IOCTL commands for SATA hotplugging. 1278 * Implemented DEVCTL_AP commands: 1279 * DEVCTL_AP_CONNECT 1280 * DEVCTL_AP_DISCONNECT 1281 * DEVCTL_AP_CONFIGURE 1282 * DEVCTL_UNCONFIGURE 1283 * DEVCTL_AP_CONTROL 1284 * 1285 * Commands passed to default ndi ioctl handler: 1286 * DEVCTL_DEVICE_GETSTATE 1287 * DEVCTL_DEVICE_ONLINE 1288 * DEVCTL_DEVICE_OFFLINE 1289 * DEVCTL_DEVICE_REMOVE 1290 * DEVCTL_DEVICE_INSERT 1291 * DEVCTL_BUS_GETSTATE 1292 * 1293 * All other cmds are passed to HBA if it provide ioctl handler, or failed 1294 * if not. 1295 * 1296 * Returns: 1297 * 0 if successful, 1298 * error code if operation failed. 1299 * 1300 * Port Multiplier support is supported now. 1301 * 1302 * NOTE: qual should be SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT 1303 */ 1304 1305 static int 1306 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, 1307 int *rvalp) 1308 { 1309 #ifndef __lock_lint 1310 _NOTE(ARGUNUSED(credp)) 1311 _NOTE(ARGUNUSED(rvalp)) 1312 #endif 1313 int rv = 0; 1314 int32_t comp_port = -1; 1315 dev_info_t *dip; 1316 devctl_ap_state_t ap_state; 1317 struct devctl_iocdata *dcp = NULL; 1318 scsi_hba_tran_t *scsi_hba_tran; 1319 sata_hba_inst_t *sata_hba_inst; 1320 sata_device_t sata_device; 1321 sata_cport_info_t *cportinfo; 1322 int cport, pmport, qual; 1323 int rval = SATA_SUCCESS; 1324 1325 dip = sata_devt_to_devinfo(dev); 1326 if (dip == NULL) 1327 return (ENXIO); 1328 1329 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1330 return (ENXIO); 1331 1332 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1333 if (sata_hba_inst == NULL) 1334 return (ENXIO); 1335 1336 if (sata_hba_inst->satahba_tran == NULL) 1337 return (ENXIO); 1338 1339 switch (cmd) { 1340 1341 case DEVCTL_DEVICE_GETSTATE: 1342 case DEVCTL_DEVICE_ONLINE: 1343 case DEVCTL_DEVICE_OFFLINE: 1344 case DEVCTL_DEVICE_REMOVE: 1345 case DEVCTL_BUS_GETSTATE: 1346 /* 1347 * There may be more cases that we want to pass to default 1348 * handler rather than fail them. 1349 */ 1350 return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0)); 1351 } 1352 1353 /* read devctl ioctl data */ 1354 if (cmd != DEVCTL_AP_CONTROL && cmd >= DEVCTL_IOC && 1355 cmd <= DEVCTL_IOC_MAX) { 1356 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) 1357 return (EFAULT); 1358 1359 if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) == 1360 -1) { 1361 if (dcp) 1362 ndi_dc_freehdl(dcp); 1363 return (EINVAL); 1364 } 1365 1366 /* 1367 * According to SCSI_TO_SATA_ADDR_QUAL, qual should be either 1368 * SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT. 1369 */ 1370 cport = SCSI_TO_SATA_CPORT(comp_port); 1371 pmport = SCSI_TO_SATA_PMPORT(comp_port); 1372 qual = SCSI_TO_SATA_ADDR_QUAL(comp_port); 1373 1374 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, 1375 qual) != 0) { 1376 ndi_dc_freehdl(dcp); 1377 return (EINVAL); 1378 } 1379 1380 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 1381 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1382 cport_mutex); 1383 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) { 1384 /* 1385 * Cannot process ioctl request now. Come back later. 1386 */ 1387 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1388 cport_mutex); 1389 ndi_dc_freehdl(dcp); 1390 return (EBUSY); 1391 } 1392 /* Block event processing for this port */ 1393 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 1394 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1395 1396 sata_device.satadev_addr.cport = cport; 1397 sata_device.satadev_addr.pmport = pmport; 1398 sata_device.satadev_addr.qual = qual; 1399 sata_device.satadev_rev = SATA_DEVICE_REV; 1400 } 1401 1402 switch (cmd) { 1403 1404 case DEVCTL_AP_DISCONNECT: 1405 1406 /* 1407 * Normally, cfgadm sata plugin will try to offline 1408 * (unconfigure) device before this request. Nevertheless, 1409 * if a device is still configured, we need to 1410 * attempt to offline and unconfigure device first, and we will 1411 * deactivate the port regardless of the unconfigure 1412 * operation results. 1413 * 1414 */ 1415 rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device); 1416 1417 break; 1418 1419 case DEVCTL_AP_UNCONFIGURE: 1420 1421 /* 1422 * The unconfigure operation uses generic nexus operation to 1423 * offline a device. It leaves a target device node attached. 1424 * and obviously sata_drive_info attached as well, because 1425 * from the hardware point of view nothing has changed. 1426 */ 1427 rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device); 1428 break; 1429 1430 case DEVCTL_AP_CONNECT: 1431 { 1432 /* 1433 * The sata cfgadm pluging will invoke this operation only if 1434 * port was found in the disconnect state (failed state 1435 * is also treated as the disconnected state). 1436 * If port activation is successful and a device is found 1437 * attached to the port, the initialization sequence is 1438 * executed to probe the port and attach 1439 * a device structure to a port structure. The device is not 1440 * set in configured state (system-wise) by this operation. 1441 */ 1442 1443 rv = sata_ioctl_connect(sata_hba_inst, &sata_device); 1444 1445 break; 1446 } 1447 1448 case DEVCTL_AP_CONFIGURE: 1449 { 1450 /* 1451 * A port may be in an active or shutdown state. 1452 * If port is in a failed state, operation is aborted. 1453 * If a port is in a shutdown state, sata_tran_port_activate() 1454 * is invoked prior to any other operation. 1455 * 1456 * Onlining the device involves creating a new target node. 1457 * If there is an old target node present (belonging to 1458 * previously removed device), the operation is aborted - the 1459 * old node has to be released and removed before configure 1460 * operation is attempted. 1461 */ 1462 1463 rv = sata_ioctl_configure(sata_hba_inst, &sata_device); 1464 1465 break; 1466 } 1467 1468 case DEVCTL_AP_GETSTATE: 1469 1470 sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state); 1471 1472 ap_state.ap_last_change = (time_t)-1; 1473 ap_state.ap_error_code = 0; 1474 ap_state.ap_in_transition = 0; 1475 1476 /* Copy the return AP-state information to the user space */ 1477 if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) { 1478 rv = EFAULT; 1479 } 1480 break; 1481 1482 case DEVCTL_AP_CONTROL: 1483 { 1484 /* 1485 * Generic devctl for hardware specific functionality 1486 */ 1487 sata_ioctl_data_t ioc; 1488 1489 ASSERT(dcp == NULL); 1490 1491 /* Copy in user ioctl data first */ 1492 #ifdef _MULTI_DATAMODEL 1493 if (ddi_model_convert_from(mode & FMODELS) == 1494 DDI_MODEL_ILP32) { 1495 1496 sata_ioctl_data_32_t ioc32; 1497 1498 if (ddi_copyin((void *)arg, (void *)&ioc32, 1499 sizeof (ioc32), mode) != 0) { 1500 rv = EFAULT; 1501 break; 1502 } 1503 ioc.cmd = (uint_t)ioc32.cmd; 1504 ioc.port = (uint_t)ioc32.port; 1505 ioc.get_size = (uint_t)ioc32.get_size; 1506 ioc.buf = (caddr_t)(uintptr_t)ioc32.buf; 1507 ioc.bufsiz = (uint_t)ioc32.bufsiz; 1508 ioc.misc_arg = (uint_t)ioc32.misc_arg; 1509 } else 1510 #endif /* _MULTI_DATAMODEL */ 1511 if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc), 1512 mode) != 0) { 1513 return (EFAULT); 1514 } 1515 1516 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 1517 "sata_hba_ioctl: DEVCTL_AP_CONTROL " 1518 "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port); 1519 1520 /* 1521 * To avoid BE/LE and 32/64 issues, a get_size always returns 1522 * a 32-bit number. 1523 */ 1524 if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) { 1525 return (EINVAL); 1526 } 1527 /* validate address */ 1528 cport = SCSI_TO_SATA_CPORT(ioc.port); 1529 pmport = SCSI_TO_SATA_PMPORT(ioc.port); 1530 qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port); 1531 1532 SATADBG3(SATA_DBG_IOCTL_IF, sata_hba_inst, 1533 "sata_hba_ioctl: target port is %d:%d (%d)", 1534 cport, pmport, qual); 1535 1536 if (sata_validate_sata_address(sata_hba_inst, cport, 1537 pmport, qual) != 0) 1538 return (EINVAL); 1539 1540 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 1541 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1542 cport_mutex); 1543 /* Is the port locked by event processing daemon ? */ 1544 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) { 1545 /* 1546 * Cannot process ioctl request now. Come back later 1547 */ 1548 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1549 cport_mutex); 1550 return (EBUSY); 1551 } 1552 /* Block event processing for this port */ 1553 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 1554 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1555 1556 1557 sata_device.satadev_addr.cport = cport; 1558 sata_device.satadev_addr.pmport = pmport; 1559 sata_device.satadev_addr.qual = qual; 1560 sata_device.satadev_rev = SATA_DEVICE_REV; 1561 1562 switch (ioc.cmd) { 1563 1564 case SATA_CFGA_RESET_PORT: 1565 /* 1566 * There is no protection for configured device. 1567 */ 1568 rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device); 1569 break; 1570 1571 case SATA_CFGA_RESET_DEVICE: 1572 /* 1573 * There is no protection for configured device. 1574 */ 1575 rv = sata_ioctl_reset_device(sata_hba_inst, 1576 &sata_device); 1577 break; 1578 1579 case SATA_CFGA_RESET_ALL: 1580 /* 1581 * There is no protection for configured devices. 1582 */ 1583 rv = sata_ioctl_reset_all(sata_hba_inst); 1584 /* 1585 * We return here, because common return is for 1586 * a single port operation - we have already unlocked 1587 * all ports and no dc handle was allocated. 1588 */ 1589 return (rv); 1590 1591 case SATA_CFGA_PORT_DEACTIVATE: 1592 /* 1593 * Arbitrarily unconfigure attached device, if any. 1594 * Even if the unconfigure fails, proceed with the 1595 * port deactivation. 1596 */ 1597 rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device); 1598 1599 break; 1600 1601 case SATA_CFGA_PORT_ACTIVATE: 1602 1603 rv = sata_ioctl_activate(sata_hba_inst, &sata_device); 1604 break; 1605 1606 case SATA_CFGA_PORT_SELF_TEST: 1607 1608 rv = sata_ioctl_port_self_test(sata_hba_inst, 1609 &sata_device); 1610 break; 1611 1612 case SATA_CFGA_GET_DEVICE_PATH: 1613 1614 rv = sata_ioctl_get_device_path(sata_hba_inst, 1615 &sata_device, &ioc, mode); 1616 break; 1617 1618 case SATA_CFGA_GET_AP_TYPE: 1619 1620 rv = sata_ioctl_get_ap_type(sata_hba_inst, 1621 &sata_device, &ioc, mode); 1622 break; 1623 1624 case SATA_CFGA_GET_MODEL_INFO: 1625 1626 rv = sata_ioctl_get_model_info(sata_hba_inst, 1627 &sata_device, &ioc, mode); 1628 break; 1629 1630 case SATA_CFGA_GET_REVFIRMWARE_INFO: 1631 1632 rv = sata_ioctl_get_revfirmware_info(sata_hba_inst, 1633 &sata_device, &ioc, mode); 1634 break; 1635 1636 case SATA_CFGA_GET_SERIALNUMBER_INFO: 1637 1638 rv = sata_ioctl_get_serialnumber_info(sata_hba_inst, 1639 &sata_device, &ioc, mode); 1640 break; 1641 1642 default: 1643 rv = EINVAL; 1644 break; 1645 1646 } /* End of DEVCTL_AP_CONTROL cmd switch */ 1647 1648 break; 1649 } 1650 1651 default: 1652 { 1653 /* 1654 * If we got here, we got an IOCTL that SATA HBA Framework 1655 * does not recognize. Pass ioctl to HBA driver, in case 1656 * it could process it. 1657 */ 1658 sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran; 1659 dev_info_t *mydip = SATA_DIP(sata_hba_inst); 1660 1661 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 1662 "IOCTL 0x%2x not supported in SATA framework, " 1663 "passthrough to HBA", cmd); 1664 1665 if (sata_tran->sata_tran_ioctl == NULL) { 1666 rv = EINVAL; 1667 break; 1668 } 1669 rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg); 1670 if (rval != 0) { 1671 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 1672 "IOCTL 0x%2x failed in HBA", cmd); 1673 rv = rval; 1674 } 1675 break; 1676 } 1677 1678 } /* End of main IOCTL switch */ 1679 1680 if (dcp) { 1681 ndi_dc_freehdl(dcp); 1682 } 1683 1684 if (cmd >= DEVCTL_IOC && cmd <= DEVCTL_IOC_MAX) { 1685 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 1686 cport)->cport_mutex); 1687 cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY; 1688 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1689 } 1690 1691 return (rv); 1692 } 1693 1694 1695 /* 1696 * Create error retrieval sata packet 1697 * 1698 * A sata packet is allocated and set-up to contain specified error retrieval 1699 * command and appropriate dma-able data buffer. 1700 * No association with any scsi packet is made and no callback routine is 1701 * specified. 1702 * 1703 * Returns a pointer to sata packet upon successful packet creation. 1704 * Returns NULL, if packet cannot be created. 1705 */ 1706 sata_pkt_t * 1707 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device, 1708 int pkt_type) 1709 { 1710 sata_hba_inst_t *sata_hba_inst; 1711 sata_pkt_txlate_t *spx; 1712 sata_pkt_t *spkt; 1713 sata_drive_info_t *sdinfo; 1714 1715 mutex_enter(&sata_mutex); 1716 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 1717 sata_hba_inst = sata_hba_inst->satahba_next) { 1718 if (SATA_DIP(sata_hba_inst) == dip) 1719 break; 1720 } 1721 mutex_exit(&sata_mutex); 1722 ASSERT(sata_hba_inst != NULL); 1723 1724 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 1725 if (sdinfo == NULL) { 1726 sata_log(sata_hba_inst, CE_WARN, 1727 "sata: error recovery request for non-attached device at " 1728 "cport %d", sata_device->satadev_addr.cport); 1729 return (NULL); 1730 } 1731 1732 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 1733 spx->txlt_sata_hba_inst = sata_hba_inst; 1734 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 1735 spkt = sata_pkt_alloc(spx, NULL); 1736 if (spkt == NULL) { 1737 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1738 return (NULL); 1739 } 1740 /* address is needed now */ 1741 spkt->satapkt_device.satadev_addr = sata_device->satadev_addr; 1742 1743 switch (pkt_type) { 1744 case SATA_ERR_RETR_PKT_TYPE_NCQ: 1745 if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) { 1746 if (sata_check_for_dma_error(dip, spx)) { 1747 ddi_fm_service_impact(dip, 1748 DDI_SERVICE_UNAFFECTED); 1749 break; 1750 } 1751 return (spkt); 1752 } 1753 break; 1754 1755 case SATA_ERR_RETR_PKT_TYPE_ATAPI: 1756 if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) { 1757 if (sata_check_for_dma_error(dip, spx)) { 1758 ddi_fm_service_impact(dip, 1759 DDI_SERVICE_UNAFFECTED); 1760 break; 1761 } 1762 return (spkt); 1763 } 1764 break; 1765 1766 default: 1767 break; 1768 } 1769 1770 sata_pkt_free(spx); 1771 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1772 return (NULL); 1773 1774 } 1775 1776 1777 /* 1778 * Free error retrieval sata packet 1779 * 1780 * Free sata packet and any associated resources allocated previously by 1781 * sata_get_error_retrieval_pkt(). 1782 * 1783 * Void return. 1784 */ 1785 void 1786 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt) 1787 { 1788 sata_pkt_txlate_t *spx = 1789 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 1790 1791 ASSERT(sata_pkt != NULL); 1792 1793 sata_free_local_buffer(spx); 1794 sata_pkt_free(spx); 1795 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1796 1797 } 1798 1799 /* 1800 * Create READ PORT MULTIPLIER and WRITE PORT MULTIPLIER sata packet 1801 * 1802 * No association with any scsi packet is made and no callback routine is 1803 * specified. 1804 * 1805 * Returns a pointer to sata packet upon successful packet creation. 1806 * Returns NULL, if packet cannot be created. 1807 * 1808 * NOTE: Input/Output value includes 64 bits accoring to SATA Spec 2.6, 1809 * only lower 32 bits are available currently. 1810 */ 1811 sata_pkt_t * 1812 sata_get_rdwr_pmult_pkt(dev_info_t *dip, sata_device_t *sd, 1813 uint8_t regn, uint32_t regv, uint32_t type) 1814 { 1815 sata_hba_inst_t *sata_hba_inst; 1816 sata_pkt_txlate_t *spx; 1817 sata_pkt_t *spkt; 1818 sata_cmd_t *scmd; 1819 1820 /* Only READ/WRITE commands are accepted. */ 1821 ASSERT(type == SATA_RDWR_PMULT_PKT_TYPE_READ || 1822 type == SATA_RDWR_PMULT_PKT_TYPE_WRITE); 1823 1824 mutex_enter(&sata_mutex); 1825 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 1826 sata_hba_inst = sata_hba_inst->satahba_next) { 1827 if (SATA_DIP(sata_hba_inst) == dip) 1828 break; 1829 } 1830 mutex_exit(&sata_mutex); 1831 ASSERT(sata_hba_inst != NULL); 1832 1833 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 1834 spx->txlt_sata_hba_inst = sata_hba_inst; 1835 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 1836 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 1837 if (spkt == NULL) { 1838 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1839 return (NULL); 1840 } 1841 1842 /* 1843 * NOTE: We need to send this command to the port multiplier, 1844 * that means send to SATA_PMULT_HOSTPORT(0xf) pmport 1845 * 1846 * sata_device contains the address of actual target device, and the 1847 * pmport number in the command comes from the sata_device structure. 1848 */ 1849 spkt->satapkt_device.satadev_addr = sd->satadev_addr; 1850 spkt->satapkt_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT; 1851 spkt->satapkt_device.satadev_addr.qual = SATA_ADDR_PMULT; 1852 1853 /* Fill sata_pkt */ 1854 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING; 1855 spkt->satapkt_comp = NULL; /* Synchronous mode, no callback */ 1856 spkt->satapkt_time = 10; /* Timeout 10s */ 1857 1858 /* Build READ PORT MULTIPLIER cmd in the sata_pkt */ 1859 scmd = &spkt->satapkt_cmd; 1860 scmd->satacmd_features_reg = regn & 0xff; 1861 scmd->satacmd_features_reg_ext = (regn >> 8) & 0xff; 1862 scmd->satacmd_device_reg = sd->satadev_addr.pmport; 1863 scmd->satacmd_addr_type = 0; /* N/A */ 1864 1865 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 1866 1867 if (type == SATA_RDWR_PMULT_PKT_TYPE_READ) { 1868 scmd->satacmd_cmd_reg = SATAC_READ_PORTMULT; 1869 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 1870 scmd->satacmd_flags.sata_special_regs = 1; 1871 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1; 1872 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1; 1873 scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1; 1874 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1; 1875 } else if (type == SATA_RDWR_PMULT_PKT_TYPE_WRITE) { 1876 scmd->satacmd_cmd_reg = SATAC_WRITE_PORTMULT; 1877 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 1878 scmd->satacmd_sec_count_lsb = regv & 0xff; 1879 scmd->satacmd_lba_low_lsb = regv >> 8 & 0xff; 1880 scmd->satacmd_lba_mid_lsb = regv >> 16 & 0xff; 1881 scmd->satacmd_lba_high_lsb = regv >> 24 & 0xff; 1882 } 1883 1884 return (spkt); 1885 } 1886 1887 /* 1888 * Free sata packet and any associated resources allocated previously by 1889 * sata_get_rdwr_pmult_pkt(). 1890 * 1891 * Void return. 1892 */ 1893 void 1894 sata_free_rdwr_pmult_pkt(sata_pkt_t *sata_pkt) 1895 { 1896 sata_pkt_txlate_t *spx = 1897 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 1898 1899 /* Free allocated resources */ 1900 sata_pkt_free(spx); 1901 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1902 } 1903 1904 /* 1905 * Register a port multiplier to framework. 1906 * 1) Store the GSCR values in the previous allocated pmult_info strctures. 1907 * 2) Search in the blacklist and update the number of the device ports of the 1908 * port multiplier. 1909 * 1910 * Void return. 1911 */ 1912 void 1913 sata_register_pmult(dev_info_t *dip, sata_device_t *sd, sata_pmult_gscr_t *sg) 1914 { 1915 sata_hba_inst_t *sata_hba_inst = NULL; 1916 sata_pmult_info_t *pmultinfo; 1917 sata_pmult_bl_t *blp; 1918 int cport = sd->satadev_addr.cport; 1919 1920 mutex_enter(&sata_mutex); 1921 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 1922 sata_hba_inst = sata_hba_inst->satahba_next) { 1923 if (SATA_DIP(sata_hba_inst) == dip) 1924 if (sata_hba_inst->satahba_attached == 1) 1925 break; 1926 } 1927 mutex_exit(&sata_mutex); 1928 /* HBA not attached? */ 1929 if (sata_hba_inst == NULL) 1930 return; 1931 1932 /* Number of pmports */ 1933 sd->satadev_add_info = sg->gscr2 & SATA_PMULT_PORTNUM_MASK; 1934 1935 /* Check the blacklist */ 1936 for (blp = sata_pmult_blacklist; blp->bl_gscr0; blp++) { 1937 if (sg->gscr0 != blp->bl_gscr0 && blp->bl_gscr0) 1938 continue; 1939 if (sg->gscr1 != blp->bl_gscr1 && blp->bl_gscr1) 1940 continue; 1941 if (sg->gscr2 != blp->bl_gscr2 && blp->bl_gscr2) 1942 continue; 1943 1944 cmn_err(CE_WARN, "!Port multiplier is on the blacklist."); 1945 sd->satadev_add_info = blp->bl_flags; 1946 break; 1947 } 1948 1949 /* Register the port multiplier GSCR */ 1950 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 1951 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 1952 if (pmultinfo != NULL) { 1953 pmultinfo->pmult_gscr = *sg; 1954 pmultinfo->pmult_num_dev_ports = 1955 sd->satadev_add_info & SATA_PMULT_PORTNUM_MASK; 1956 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 1957 "Port multiplier registered at port %d", cport); 1958 } 1959 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 1960 } 1961 1962 /* 1963 * sata_split_model splits the model ID into vendor and product IDs. 1964 * It assumes that a vendor ID cannot be longer than 8 characters, and 1965 * that vendor and product ID are separated by a whitespace. 1966 */ 1967 void 1968 sata_split_model(char *model, char **vendor, char **product) 1969 { 1970 int i, modlen; 1971 char *vid, *pid; 1972 1973 /* 1974 * remove whitespace at the end of model 1975 */ 1976 for (i = SATA_ID_MODEL_LEN; i > 0; i--) 1977 if (model[i] == ' ' || model[i] == '\t' || model[i] == '\0') 1978 model[i] = '\0'; 1979 else 1980 break; 1981 1982 /* 1983 * try to split model into into vid/pid 1984 */ 1985 modlen = strlen(model); 1986 for (i = 0, pid = model; i < modlen; i++, pid++) 1987 if ((*pid == ' ') || (*pid == '\t')) 1988 break; 1989 1990 /* 1991 * only use vid if it is less than 8 chars (as in SCSI) 1992 */ 1993 if (i < modlen && i <= 8) { 1994 vid = model; 1995 /* 1996 * terminate vid, establish pid 1997 */ 1998 *pid++ = '\0'; 1999 } else { 2000 /* 2001 * vid will stay "ATA " 2002 */ 2003 vid = NULL; 2004 /* 2005 * model is all pid 2006 */ 2007 pid = model; 2008 } 2009 2010 *vendor = vid; 2011 *product = pid; 2012 } 2013 2014 /* 2015 * sata_name_child is for composing the name of the node 2016 * the format of the name is "target,0". 2017 */ 2018 static int 2019 sata_name_child(dev_info_t *dip, char *name, int namelen) 2020 { 2021 int target; 2022 2023 target = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 2024 DDI_PROP_DONTPASS, "target", -1); 2025 if (target == -1) 2026 return (DDI_FAILURE); 2027 (void) snprintf(name, namelen, "%x,0", target); 2028 return (DDI_SUCCESS); 2029 } 2030 2031 2032 2033 /* ****************** SCSA required entry points *********************** */ 2034 2035 /* 2036 * Implementation of scsi tran_tgt_init. 2037 * sata_scsi_tgt_init() initializes scsi_device structure 2038 * 2039 * If successful, DDI_SUCCESS is returned. 2040 * DDI_FAILURE is returned if addressed device does not exist 2041 */ 2042 2043 static int 2044 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip, 2045 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 2046 { 2047 #ifndef __lock_lint 2048 _NOTE(ARGUNUSED(hba_dip)) 2049 _NOTE(ARGUNUSED(tgt_dip)) 2050 #endif 2051 sata_device_t sata_device; 2052 sata_drive_info_t *sdinfo; 2053 struct sata_id *sid; 2054 sata_hba_inst_t *sata_hba_inst; 2055 char model[SATA_ID_MODEL_LEN + 1]; 2056 char fw[SATA_ID_FW_LEN + 1]; 2057 char *vid, *pid; 2058 2059 /* 2060 * Fail tran_tgt_init for .conf stub node 2061 */ 2062 if (ndi_dev_is_persistent_node(tgt_dip) == 0) { 2063 (void) ndi_merge_node(tgt_dip, sata_name_child); 2064 ddi_set_name_addr(tgt_dip, NULL); 2065 return (DDI_FAILURE); 2066 } 2067 2068 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 2069 2070 /* Validate scsi device address */ 2071 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 2072 &sata_device) != 0) 2073 return (DDI_FAILURE); 2074 2075 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2076 sata_device.satadev_addr.cport))); 2077 2078 /* sata_device now contains a valid sata address */ 2079 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 2080 if (sdinfo == NULL) { 2081 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2082 sata_device.satadev_addr.cport))); 2083 return (DDI_FAILURE); 2084 } 2085 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2086 sata_device.satadev_addr.cport))); 2087 2088 /* 2089 * Check if we need to create a legacy devid (i.e cmdk style) for 2090 * the target disks. 2091 * 2092 * HBA devinfo node will have the property "use-cmdk-devid-format" 2093 * if we need to create cmdk-style devid for all the disk devices 2094 * attached to this controller. This property may have been set 2095 * from HBA driver's .conf file or by the HBA driver in its 2096 * attach(9F) function. 2097 */ 2098 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 2099 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS, 2100 "use-cmdk-devid-format", 0) == 1)) { 2101 /* register a legacy devid for this target node */ 2102 sata_target_devid_register(tgt_dip, sdinfo); 2103 } 2104 2105 2106 /* 2107 * 'Identify Device Data' does not always fit in standard SCSI 2108 * INQUIRY data, so establish INQUIRY_* properties with full-form 2109 * of information. 2110 */ 2111 sid = &sdinfo->satadrv_id; 2112 #ifdef _LITTLE_ENDIAN 2113 swab(sid->ai_model, model, SATA_ID_MODEL_LEN); 2114 swab(sid->ai_fw, fw, SATA_ID_FW_LEN); 2115 #else /* _LITTLE_ENDIAN */ 2116 bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN); 2117 bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN); 2118 #endif /* _LITTLE_ENDIAN */ 2119 model[SATA_ID_MODEL_LEN] = 0; 2120 fw[SATA_ID_FW_LEN] = 0; 2121 2122 sata_split_model(model, &vid, &pid); 2123 2124 if (vid) 2125 (void) scsi_device_prop_update_inqstring(sd, INQUIRY_VENDOR_ID, 2126 vid, strlen(vid)); 2127 if (pid) 2128 (void) scsi_device_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID, 2129 pid, strlen(pid)); 2130 (void) scsi_device_prop_update_inqstring(sd, INQUIRY_REVISION_ID, 2131 fw, strlen(fw)); 2132 2133 return (DDI_SUCCESS); 2134 } 2135 2136 /* 2137 * Implementation of scsi tran_tgt_probe. 2138 * Probe target, by calling default scsi routine scsi_hba_probe() 2139 */ 2140 static int 2141 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void)) 2142 { 2143 sata_hba_inst_t *sata_hba_inst = 2144 (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private); 2145 int rval; 2146 uint32_t pm_cap; 2147 2148 rval = scsi_hba_probe(sd, callback); 2149 pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE | 2150 SATA_CAP_LOG_SENSE; 2151 2152 if (rval == SCSIPROBE_EXISTS) { 2153 /* 2154 * Set property "pm-capable" on the target device node, so that 2155 * the target driver will not try to fetch scsi cycle counters 2156 * before enabling device power-management. 2157 */ 2158 if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev, 2159 "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) { 2160 sata_log(sata_hba_inst, CE_WARN, 2161 "SATA device at port %d: " 2162 "will not be power-managed ", 2163 SCSI_TO_SATA_CPORT(sd->sd_address.a_target)); 2164 SATA_LOG_D((sata_hba_inst, CE_WARN, 2165 "failure updating pm-capable property")); 2166 } 2167 } 2168 return (rval); 2169 } 2170 2171 /* 2172 * Implementation of scsi tran_tgt_free. 2173 * Release all resources allocated for scsi_device 2174 */ 2175 static void 2176 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip, 2177 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 2178 { 2179 #ifndef __lock_lint 2180 _NOTE(ARGUNUSED(hba_dip)) 2181 #endif 2182 sata_device_t sata_device; 2183 sata_drive_info_t *sdinfo; 2184 sata_hba_inst_t *sata_hba_inst; 2185 ddi_devid_t devid; 2186 2187 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 2188 2189 /* Validate scsi device address */ 2190 /* 2191 * Note: tgt_free relates to the SCSA view of a device. If called, there 2192 * was a device at this address, so even if the sata framework internal 2193 * resources were alredy released because a device was detached, 2194 * this function should be executed as long as its actions do 2195 * not require the internal sata view of a device and the address 2196 * refers to a valid sata address. 2197 * Validating the address here means that we do not trust SCSA... 2198 */ 2199 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 2200 &sata_device) == -1) 2201 return; 2202 2203 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2204 sata_device.satadev_addr.cport))); 2205 2206 /* sata_device now should contain a valid sata address */ 2207 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 2208 if (sdinfo == NULL) { 2209 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2210 sata_device.satadev_addr.cport))); 2211 return; 2212 } 2213 /* 2214 * We did not allocate any resources in sata_scsi_tgt_init() 2215 * other than few properties. 2216 * Free them. 2217 */ 2218 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2219 sata_device.satadev_addr.cport))); 2220 (void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable"); 2221 2222 /* 2223 * If devid was previously created but not freed up from 2224 * sd(7D) driver (i.e during detach(9F)) then do it here. 2225 */ 2226 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 2227 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS, 2228 "use-cmdk-devid-format", 0) == 1) && 2229 (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) { 2230 ddi_devid_unregister(tgt_dip); 2231 ddi_devid_free(devid); 2232 } 2233 } 2234 2235 /* 2236 * Implementation of scsi tran_init_pkt 2237 * Upon successful return, scsi pkt buffer has DMA resources allocated. 2238 * 2239 * It seems that we should always allocate pkt, even if the address is 2240 * for non-existing device - just use some default for dma_attr. 2241 * The reason is that there is no way to communicate this to a caller here. 2242 * Subsequent call to sata_scsi_start may fail appropriately. 2243 * Simply returning NULL does not seem to discourage a target driver... 2244 * 2245 * Returns a pointer to initialized scsi_pkt, or NULL otherwise. 2246 */ 2247 static struct scsi_pkt * 2248 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt, 2249 struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags, 2250 int (*callback)(caddr_t), caddr_t arg) 2251 { 2252 sata_hba_inst_t *sata_hba_inst = 2253 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2254 dev_info_t *dip = SATA_DIP(sata_hba_inst); 2255 sata_device_t sata_device; 2256 sata_drive_info_t *sdinfo; 2257 sata_pkt_txlate_t *spx; 2258 ddi_dma_attr_t cur_dma_attr; 2259 int rval; 2260 boolean_t new_pkt = B_TRUE; 2261 2262 ASSERT(ap->a_hba_tran->tran_hba_dip == dip); 2263 2264 /* 2265 * We need to translate the address, even if it could be 2266 * a bogus one, for a non-existing device 2267 */ 2268 sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 2269 sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target); 2270 sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 2271 sata_device.satadev_rev = SATA_DEVICE_REV; 2272 2273 if (pkt == NULL) { 2274 /* 2275 * Have to allocate a brand new scsi packet. 2276 * We need to operate with auto request sense enabled. 2277 */ 2278 pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen, 2279 MAX(statuslen, SATA_MAX_SENSE_LEN), 2280 tgtlen, sizeof (sata_pkt_txlate_t), callback, arg); 2281 2282 if (pkt == NULL) 2283 return (NULL); 2284 2285 /* Fill scsi packet structure */ 2286 pkt->pkt_comp = (void (*)())NULL; 2287 pkt->pkt_time = 0; 2288 pkt->pkt_resid = 0; 2289 pkt->pkt_statistics = 0; 2290 pkt->pkt_reason = 0; 2291 2292 /* 2293 * pkt_hba_private will point to sata pkt txlate structure 2294 */ 2295 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2296 bzero(spx, sizeof (sata_pkt_txlate_t)); 2297 2298 spx->txlt_scsi_pkt = pkt; 2299 spx->txlt_sata_hba_inst = sata_hba_inst; 2300 2301 /* Allocate sata_pkt */ 2302 spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback); 2303 if (spx->txlt_sata_pkt == NULL) { 2304 /* Could not allocate sata pkt */ 2305 scsi_hba_pkt_free(ap, pkt); 2306 return (NULL); 2307 } 2308 /* Set sata address */ 2309 spx->txlt_sata_pkt->satapkt_device.satadev_addr = 2310 sata_device.satadev_addr; 2311 spx->txlt_sata_pkt->satapkt_device.satadev_rev = 2312 sata_device.satadev_rev; 2313 2314 if ((bp == NULL) || (bp->b_bcount == 0)) 2315 return (pkt); 2316 2317 spx->txlt_total_residue = bp->b_bcount; 2318 } else { 2319 new_pkt = B_FALSE; 2320 /* 2321 * Packet was preallocated/initialized by previous call 2322 */ 2323 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2324 2325 if ((bp == NULL) || (bp->b_bcount == 0)) { 2326 return (pkt); 2327 } 2328 2329 /* Pkt is available already: spx->txlt_scsi_pkt == pkt; */ 2330 } 2331 2332 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 2333 2334 /* 2335 * We use an adjusted version of the dma_attr, to account 2336 * for device addressing limitations. 2337 * sata_adjust_dma_attr() will handle sdinfo == NULL which may 2338 * happen when a device is not yet configured. 2339 */ 2340 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2341 sata_device.satadev_addr.cport))); 2342 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 2343 &spx->txlt_sata_pkt->satapkt_device); 2344 /* NULL sdinfo may be passsed to sata_adjust_dma_attr() */ 2345 sata_adjust_dma_attr(sdinfo, 2346 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 2347 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2348 sata_device.satadev_addr.cport))); 2349 /* 2350 * Allocate necessary DMA resources for the packet's data buffer 2351 * NOTE: 2352 * In case of read/write commands, DMA resource allocation here is 2353 * based on the premise that the transfer length specified in 2354 * the read/write scsi cdb will match exactly DMA resources - 2355 * returning correct packet residue is crucial. 2356 */ 2357 if ((rval = sata_dma_buf_setup(spx, flags, callback, arg, 2358 &cur_dma_attr)) != DDI_SUCCESS) { 2359 /* 2360 * If a DMA allocation request fails with 2361 * DDI_DMA_NOMAPPING, indicate the error by calling 2362 * bioerror(9F) with bp and an error code of EFAULT. 2363 * If a DMA allocation request fails with 2364 * DDI_DMA_TOOBIG, indicate the error by calling 2365 * bioerror(9F) with bp and an error code of EINVAL. 2366 * For DDI_DMA_NORESOURCES, we may have some of them allocated. 2367 * Request may be repeated later - there is no real error. 2368 */ 2369 switch (rval) { 2370 case DDI_DMA_NORESOURCES: 2371 bioerror(bp, 0); 2372 break; 2373 case DDI_DMA_NOMAPPING: 2374 case DDI_DMA_BADATTR: 2375 bioerror(bp, EFAULT); 2376 break; 2377 case DDI_DMA_TOOBIG: 2378 default: 2379 bioerror(bp, EINVAL); 2380 break; 2381 } 2382 goto fail; 2383 } 2384 2385 if (sata_check_for_dma_error(dip, spx)) { 2386 ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED); 2387 bioerror(bp, EFAULT); 2388 goto fail; 2389 } 2390 2391 success: 2392 /* Set number of bytes that are not yet accounted for */ 2393 pkt->pkt_resid = spx->txlt_total_residue; 2394 ASSERT(pkt->pkt_resid >= 0); 2395 2396 return (pkt); 2397 2398 fail: 2399 if (new_pkt == B_TRUE) { 2400 /* 2401 * Since this is a new packet, we can clean-up 2402 * everything 2403 */ 2404 sata_scsi_destroy_pkt(ap, pkt); 2405 } else { 2406 /* 2407 * This is a re-used packet. It will be target driver's 2408 * responsibility to eventually destroy it (which 2409 * will free allocated resources). 2410 * Here, we just "complete" the request, leaving 2411 * allocated resources intact, so the request may 2412 * be retried. 2413 */ 2414 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 2415 sata_pkt_free(spx); 2416 } 2417 return (NULL); 2418 } 2419 2420 /* 2421 * Implementation of scsi tran_start. 2422 * Translate scsi cmd into sata operation and return status. 2423 * ATAPI CDBs are passed to ATAPI devices - the device determines what commands 2424 * are supported. 2425 * For SATA hard disks, supported scsi commands: 2426 * SCMD_INQUIRY 2427 * SCMD_TEST_UNIT_READY 2428 * SCMD_START_STOP 2429 * SCMD_READ_CAPACITY 2430 * SCMD_SVC_ACTION_IN_G4 (READ CAPACITY (16)) 2431 * SCMD_REQUEST_SENSE 2432 * SCMD_LOG_SENSE_G1 2433 * SCMD_LOG_SELECT_G1 2434 * SCMD_MODE_SENSE (specific pages) 2435 * SCMD_MODE_SENSE_G1 (specific pages) 2436 * SCMD_MODE_SELECT (specific pages) 2437 * SCMD_MODE_SELECT_G1 (specific pages) 2438 * SCMD_SYNCHRONIZE_CACHE 2439 * SCMD_SYNCHRONIZE_CACHE_G1 2440 * SCMD_READ 2441 * SCMD_READ_G1 2442 * SCMD_READ_G4 2443 * SCMD_READ_G5 2444 * SCMD_WRITE 2445 * SCMD_WRITE_BUFFER 2446 * SCMD_WRITE_G1 2447 * SCMD_WRITE_G4 2448 * SCMD_WRITE_G5 2449 * SCMD_SEEK (noop) 2450 * SCMD_SDIAG 2451 * 2452 * All other commands are rejected as unsupported. 2453 * 2454 * Returns: 2455 * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver 2456 * for execution. TRAN_ACCEPT may be returned also if device was removed but 2457 * a callback could be scheduled. 2458 * TRAN_BADPKT if cmd was directed to invalid address. 2459 * TRAN_FATAL_ERROR is command was rejected due to hardware error, including 2460 * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device 2461 * was removed and there was no callback specified in scsi pkt. 2462 * TRAN_BUSY if command could not be executed becasue HBA driver or SATA 2463 * framework was busy performing some other operation(s). 2464 * 2465 */ 2466 static int 2467 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt) 2468 { 2469 sata_hba_inst_t *sata_hba_inst = 2470 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2471 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2472 sata_device_t *sdevice = &spx->txlt_sata_pkt->satapkt_device; 2473 sata_drive_info_t *sdinfo; 2474 struct buf *bp; 2475 uint8_t cport, pmport; 2476 boolean_t dev_gone = B_FALSE; 2477 int rval; 2478 2479 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2480 "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]); 2481 2482 ASSERT(spx != NULL && 2483 spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL); 2484 2485 cport = SCSI_TO_SATA_CPORT(ap->a_target); 2486 pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 2487 2488 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2489 2490 if (sdevice->satadev_addr.qual == SATA_ADDR_DCPORT) { 2491 sdinfo = sata_get_device_info(sata_hba_inst, sdevice); 2492 if (sdinfo == NULL || 2493 SATA_CPORT_INFO(sata_hba_inst, cport)-> 2494 cport_tgtnode_clean == B_FALSE || 2495 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 2496 dev_gone = B_TRUE; 2497 } 2498 } else if (sdevice->satadev_addr.qual == SATA_ADDR_DPMPORT) { 2499 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 2500 SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst, 2501 cport) == NULL) { 2502 dev_gone = B_TRUE; 2503 } else if (SATA_PMPORT_INFO(sata_hba_inst, cport, 2504 pmport) == NULL) { 2505 dev_gone = B_TRUE; 2506 } else { 2507 mutex_enter(&(SATA_PMPORT_MUTEX(sata_hba_inst, 2508 cport, pmport))); 2509 sdinfo = sata_get_device_info(sata_hba_inst, sdevice); 2510 if (sdinfo == NULL || 2511 SATA_PMPORT_INFO(sata_hba_inst, cport, pmport)-> 2512 pmport_tgtnode_clean == B_FALSE || 2513 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 2514 dev_gone = B_TRUE; 2515 } 2516 mutex_exit(&(SATA_PMPORT_MUTEX(sata_hba_inst, 2517 cport, pmport))); 2518 } 2519 } 2520 2521 if (dev_gone == B_TRUE) { 2522 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2523 pkt->pkt_reason = CMD_DEV_GONE; 2524 /* 2525 * The sd target driver is checking CMD_DEV_GONE pkt_reason 2526 * only in callback function (for normal requests) and 2527 * in the dump code path. 2528 * So, if the callback is available, we need to do 2529 * the callback rather than returning TRAN_FATAL_ERROR here. 2530 */ 2531 if (pkt->pkt_comp != NULL) { 2532 /* scsi callback required */ 2533 if (servicing_interrupt()) { 2534 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 2535 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 2536 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 2537 TASKQID_INVALID) { 2538 return (TRAN_BUSY); 2539 } 2540 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 2541 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 2542 spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 2543 /* Scheduling the callback failed */ 2544 return (TRAN_BUSY); 2545 } 2546 return (TRAN_ACCEPT); 2547 } 2548 /* No callback available */ 2549 return (TRAN_FATAL_ERROR); 2550 } 2551 2552 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 2553 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2554 rval = sata_txlt_atapi(spx); 2555 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2556 "sata_scsi_start atapi: rval %d\n", rval); 2557 return (rval); 2558 } 2559 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2560 2561 /* 2562 * Checking for power state, if it was on 2563 * STOPPED state, then the drive is not capable 2564 * of processing media access command. And 2565 * TEST_UNIT_READY, REQUEST_SENSE has special handling 2566 * in the function for different power state. 2567 */ 2568 if (((sdinfo->satadrv_power_level == SATA_POWER_STANDBY) || 2569 (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)) && 2570 (SATA_IS_MEDIUM_ACCESS_CMD(pkt->pkt_cdbp[0]))) { 2571 return (sata_txlt_check_condition(spx, KEY_NOT_READY, 2572 SD_SCSI_ASC_LU_NOT_READY)); 2573 } 2574 2575 /* ATA Disk commands processing starts here */ 2576 2577 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 2578 2579 switch (pkt->pkt_cdbp[0]) { 2580 2581 case SCMD_INQUIRY: 2582 /* Mapped to identify device */ 2583 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2584 bp_mapin(bp); 2585 rval = sata_txlt_inquiry(spx); 2586 break; 2587 2588 case SCMD_TEST_UNIT_READY: 2589 /* 2590 * SAT "SATA to ATA Translation" doc specifies translation 2591 * to ATA CHECK POWER MODE. 2592 */ 2593 rval = sata_txlt_test_unit_ready(spx); 2594 break; 2595 2596 case SCMD_START_STOP: 2597 /* Mapping depends on the command */ 2598 rval = sata_txlt_start_stop_unit(spx); 2599 break; 2600 2601 case SCMD_READ_CAPACITY: 2602 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2603 bp_mapin(bp); 2604 rval = sata_txlt_read_capacity(spx); 2605 break; 2606 2607 case SCMD_SVC_ACTION_IN_G4: /* READ CAPACITY (16) */ 2608 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2609 bp_mapin(bp); 2610 rval = sata_txlt_read_capacity16(spx); 2611 break; 2612 2613 case SCMD_REQUEST_SENSE: 2614 /* 2615 * Always No Sense, since we force ARQ 2616 */ 2617 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2618 bp_mapin(bp); 2619 rval = sata_txlt_request_sense(spx); 2620 break; 2621 2622 case SCMD_LOG_SENSE_G1: 2623 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2624 bp_mapin(bp); 2625 rval = sata_txlt_log_sense(spx); 2626 break; 2627 2628 case SCMD_LOG_SELECT_G1: 2629 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2630 bp_mapin(bp); 2631 rval = sata_txlt_log_select(spx); 2632 break; 2633 2634 case SCMD_MODE_SENSE: 2635 case SCMD_MODE_SENSE_G1: 2636 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2637 bp_mapin(bp); 2638 rval = sata_txlt_mode_sense(spx); 2639 break; 2640 2641 2642 case SCMD_MODE_SELECT: 2643 case SCMD_MODE_SELECT_G1: 2644 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2645 bp_mapin(bp); 2646 rval = sata_txlt_mode_select(spx); 2647 break; 2648 2649 case SCMD_SYNCHRONIZE_CACHE: 2650 case SCMD_SYNCHRONIZE_CACHE_G1: 2651 rval = sata_txlt_synchronize_cache(spx); 2652 break; 2653 2654 case SCMD_READ: 2655 case SCMD_READ_G1: 2656 case SCMD_READ_G4: 2657 case SCMD_READ_G5: 2658 rval = sata_txlt_read(spx); 2659 break; 2660 case SCMD_WRITE_BUFFER: 2661 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2662 bp_mapin(bp); 2663 rval = sata_txlt_write_buffer(spx); 2664 break; 2665 2666 case SCMD_WRITE: 2667 case SCMD_WRITE_G1: 2668 case SCMD_WRITE_G4: 2669 case SCMD_WRITE_G5: 2670 rval = sata_txlt_write(spx); 2671 break; 2672 2673 case SCMD_SEEK: 2674 rval = sata_txlt_nodata_cmd_immediate(spx); 2675 break; 2676 2677 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12: 2678 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16: 2679 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2680 bp_mapin(bp); 2681 rval = sata_txlt_ata_pass_thru(spx); 2682 break; 2683 2684 /* Other cases will be filed later */ 2685 /* postponed until phase 2 of the development */ 2686 case SPC3_CMD_UNMAP: 2687 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2688 bp_mapin(bp); 2689 rval = sata_txlt_unmap(spx); 2690 break; 2691 default: 2692 rval = sata_txlt_invalid_command(spx); 2693 break; 2694 } 2695 2696 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2697 "sata_scsi_start: rval %d\n", rval); 2698 2699 return (rval); 2700 } 2701 2702 /* 2703 * Implementation of scsi tran_abort. 2704 * Abort specific pkt or all packets. 2705 * 2706 * Returns 1 if one or more packets were aborted, returns 0 otherwise 2707 * 2708 * May be called from an interrupt level. 2709 */ 2710 static int 2711 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt) 2712 { 2713 sata_hba_inst_t *sata_hba_inst = 2714 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2715 sata_device_t sata_device; 2716 sata_pkt_t *sata_pkt; 2717 2718 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2719 "sata_scsi_abort: %s at target: 0x%x\n", 2720 scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target); 2721 2722 /* Validate address */ 2723 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) 2724 /* Invalid address */ 2725 return (0); 2726 2727 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2728 sata_device.satadev_addr.cport))); 2729 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) { 2730 /* invalid address */ 2731 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2732 sata_device.satadev_addr.cport))); 2733 return (0); 2734 } 2735 if (scsi_pkt == NULL) { 2736 /* 2737 * Abort all packets. 2738 * Although we do not have specific packet, we still need 2739 * dummy packet structure to pass device address to HBA. 2740 * Allocate one, without sleeping. Fail if pkt cannot be 2741 * allocated. 2742 */ 2743 sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP); 2744 if (sata_pkt == NULL) { 2745 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2746 sata_device.satadev_addr.cport))); 2747 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: " 2748 "could not allocate sata_pkt")); 2749 return (0); 2750 } 2751 sata_pkt->satapkt_rev = SATA_PKT_REV; 2752 sata_pkt->satapkt_device = sata_device; 2753 sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 2754 } else { 2755 if (scsi_pkt->pkt_ha_private == NULL) { 2756 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2757 sata_device.satadev_addr.cport))); 2758 return (0); /* Bad scsi pkt */ 2759 } 2760 /* extract pointer to sata pkt */ 2761 sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)-> 2762 txlt_sata_pkt; 2763 } 2764 2765 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2766 sata_device.satadev_addr.cport))); 2767 /* Send abort request to HBA */ 2768 if ((*SATA_ABORT_FUNC(sata_hba_inst)) 2769 (SATA_DIP(sata_hba_inst), sata_pkt, 2770 scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) == 2771 SATA_SUCCESS) { 2772 if (scsi_pkt == NULL) 2773 kmem_free(sata_pkt, sizeof (sata_pkt_t)); 2774 /* Success */ 2775 return (1); 2776 } 2777 /* Else, something did not go right */ 2778 if (scsi_pkt == NULL) 2779 kmem_free(sata_pkt, sizeof (sata_pkt_t)); 2780 /* Failure */ 2781 return (0); 2782 } 2783 2784 2785 /* 2786 * Implementation of scsi tran_reset. 2787 * RESET_ALL request is translated into port reset. 2788 * RESET_TARGET requests is translated into a device reset, 2789 * RESET_LUN request is accepted only for LUN 0 and translated into 2790 * device reset. 2791 * The target reset should cause all HBA active and queued packets to 2792 * be terminated and returned with pkt reason SATA_PKT_RESET prior to 2793 * the return. HBA should report reset event for the device. 2794 * 2795 * Returns 1 upon success, 0 upon failure. 2796 */ 2797 static int 2798 sata_scsi_reset(struct scsi_address *ap, int level) 2799 { 2800 sata_hba_inst_t *sata_hba_inst = 2801 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2802 sata_device_t sata_device; 2803 int val; 2804 2805 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2806 "sata_scsi_reset: level %d target: 0x%x\n", 2807 level, ap->a_target); 2808 2809 /* Validate address */ 2810 val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device); 2811 if (val == -1) 2812 /* Invalid address */ 2813 return (0); 2814 2815 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2816 sata_device.satadev_addr.cport))); 2817 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) { 2818 /* invalid address */ 2819 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2820 sata_device.satadev_addr.cport))); 2821 return (0); 2822 } 2823 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2824 sata_device.satadev_addr.cport))); 2825 if (level == RESET_ALL) { 2826 /* port reset */ 2827 if (sata_device.satadev_addr.qual == SATA_ADDR_DCPORT) 2828 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 2829 else 2830 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT; 2831 2832 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 2833 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS) 2834 return (1); 2835 else 2836 return (0); 2837 2838 } else if (val == 0 && 2839 (level == RESET_TARGET || level == RESET_LUN)) { 2840 /* reset device (device attached) */ 2841 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 2842 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS) 2843 return (1); 2844 else 2845 return (0); 2846 } 2847 return (0); 2848 } 2849 2850 2851 /* 2852 * Implementation of scsi tran_getcap (get transport/device capabilities). 2853 * Supported capabilities for SATA hard disks: 2854 * auto-rqsense (always supported) 2855 * tagged-qing (supported if HBA supports it) 2856 * untagged-qing (could be supported if disk supports it, but because 2857 * caching behavior allowing untagged queuing actually 2858 * results in reduced performance. sd tries to throttle 2859 * back to only 3 outstanding commands, which may 2860 * work for real SCSI disks, but with read ahead 2861 * caching, having more than 1 outstanding command 2862 * results in cache thrashing.) 2863 * sector_size 2864 * dma_max 2865 * interconnect-type (INTERCONNECT_SATA) 2866 * 2867 * Supported capabilities for ATAPI CD/DVD devices: 2868 * auto-rqsense (always supported) 2869 * sector_size 2870 * dma_max 2871 * max-cdb-length 2872 * interconnect-type (INTERCONNECT_SATA) 2873 * 2874 * Supported capabilities for ATAPI TAPE devices: 2875 * auto-rqsense (always supported) 2876 * dma_max 2877 * max-cdb-length 2878 * 2879 * Supported capabilities for SATA ATAPI hard disks: 2880 * auto-rqsense (always supported) 2881 * interconnect-type (INTERCONNECT_SATA) 2882 * max-cdb-length 2883 * 2884 * Request for other capabilities is rejected as unsupported. 2885 * 2886 * Returns supported capability value, or -1 if capability is unsuppported or 2887 * the address is invalid - no device. 2888 */ 2889 2890 static int 2891 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom) 2892 { 2893 2894 sata_hba_inst_t *sata_hba_inst = 2895 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2896 sata_device_t sata_device; 2897 sata_drive_info_t *sdinfo; 2898 ddi_dma_attr_t adj_dma_attr; 2899 int rval; 2900 2901 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2902 "sata_scsi_getcap: target: 0x%x, cap: %s\n", 2903 ap->a_target, cap); 2904 2905 /* 2906 * We want to process the capabilities on per port granularity. 2907 * So, we are specifically restricting ourselves to whom != 0 2908 * to exclude the controller wide handling. 2909 */ 2910 if (cap == NULL || whom == 0) 2911 return (-1); 2912 2913 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) { 2914 /* Invalid address */ 2915 return (-1); 2916 } 2917 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2918 sata_device.satadev_addr.cport))); 2919 if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) == 2920 NULL) { 2921 /* invalid address */ 2922 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2923 sata_device.satadev_addr.cport))); 2924 return (-1); 2925 } 2926 2927 switch (scsi_hba_lookup_capstr(cap)) { 2928 case SCSI_CAP_ARQ: 2929 rval = 1; /* ARQ supported, turned on */ 2930 break; 2931 2932 case SCSI_CAP_SECTOR_SIZE: 2933 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) 2934 rval = SATA_DISK_SECTOR_SIZE; /* fixed size */ 2935 else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) 2936 rval = SATA_ATAPI_SECTOR_SIZE; 2937 else rval = -1; 2938 break; 2939 2940 /* 2941 * untagged queuing cause a performance inversion because of 2942 * the way sd operates. Because of this reason we do not 2943 * use it when available. 2944 */ 2945 case SCSI_CAP_UNTAGGED_QING: 2946 if (sdinfo->satadrv_features_enabled & 2947 SATA_DEV_F_E_UNTAGGED_QING) 2948 rval = 1; /* Untagged queuing available */ 2949 else 2950 rval = -1; /* Untagged queuing not available */ 2951 break; 2952 2953 case SCSI_CAP_TAGGED_QING: 2954 if ((sdinfo->satadrv_features_enabled & 2955 SATA_DEV_F_E_TAGGED_QING) && 2956 (sdinfo->satadrv_max_queue_depth > 1)) 2957 rval = 1; /* Tagged queuing available */ 2958 else 2959 rval = -1; /* Tagged queuing not available */ 2960 break; 2961 2962 case SCSI_CAP_DMA_MAX: 2963 sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst), 2964 &adj_dma_attr); 2965 rval = (int)adj_dma_attr.dma_attr_maxxfer; 2966 /* We rely on the fact that dma_attr_maxxfer < 0x80000000 */ 2967 break; 2968 2969 case SCSI_CAP_INTERCONNECT_TYPE: 2970 rval = INTERCONNECT_SATA; /* SATA interconnect type */ 2971 break; 2972 2973 case SCSI_CAP_CDB_LEN: 2974 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) 2975 rval = sdinfo->satadrv_atapi_cdb_len; 2976 else 2977 rval = -1; 2978 break; 2979 2980 default: 2981 rval = -1; 2982 break; 2983 } 2984 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2985 sata_device.satadev_addr.cport))); 2986 return (rval); 2987 } 2988 2989 /* 2990 * Implementation of scsi tran_setcap 2991 * 2992 * Only SCSI_CAP_UNTAGGED_QING and SCSI_CAP_TAGGED_QING are changeable. 2993 * 2994 */ 2995 static int 2996 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom) 2997 { 2998 sata_hba_inst_t *sata_hba_inst = 2999 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 3000 sata_device_t sata_device; 3001 sata_drive_info_t *sdinfo; 3002 int rval; 3003 3004 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 3005 "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap); 3006 3007 /* 3008 * We want to process the capabilities on per port granularity. 3009 * So, we are specifically restricting ourselves to whom != 0 3010 * to exclude the controller wide handling. 3011 */ 3012 if (cap == NULL || whom == 0) { 3013 return (-1); 3014 } 3015 3016 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) { 3017 /* Invalid address */ 3018 return (-1); 3019 } 3020 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 3021 sata_device.satadev_addr.cport))); 3022 if ((sdinfo = sata_get_device_info(sata_hba_inst, 3023 &sata_device)) == NULL) { 3024 /* invalid address */ 3025 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3026 sata_device.satadev_addr.cport))); 3027 return (-1); 3028 } 3029 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 3030 sata_device.satadev_addr.cport))); 3031 3032 switch (scsi_hba_lookup_capstr(cap)) { 3033 case SCSI_CAP_ARQ: 3034 case SCSI_CAP_SECTOR_SIZE: 3035 case SCSI_CAP_DMA_MAX: 3036 case SCSI_CAP_INTERCONNECT_TYPE: 3037 rval = 0; 3038 break; 3039 case SCSI_CAP_UNTAGGED_QING: 3040 if (SATA_QDEPTH(sata_hba_inst) > 1) { 3041 rval = 1; 3042 if (value == 1) { 3043 sdinfo->satadrv_features_enabled |= 3044 SATA_DEV_F_E_UNTAGGED_QING; 3045 } else if (value == 0) { 3046 sdinfo->satadrv_features_enabled &= 3047 ~SATA_DEV_F_E_UNTAGGED_QING; 3048 } else { 3049 rval = -1; 3050 } 3051 } else { 3052 rval = 0; 3053 } 3054 break; 3055 case SCSI_CAP_TAGGED_QING: 3056 /* This can TCQ or NCQ */ 3057 if (sata_func_enable & SATA_ENABLE_QUEUING && 3058 ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ && 3059 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) || 3060 (sata_func_enable & SATA_ENABLE_NCQ && 3061 sdinfo->satadrv_features_support & SATA_DEV_F_NCQ && 3062 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) && 3063 (sdinfo->satadrv_max_queue_depth > 1)) { 3064 rval = 1; 3065 if (value == 1) { 3066 sdinfo->satadrv_features_enabled |= 3067 SATA_DEV_F_E_TAGGED_QING; 3068 } else if (value == 0) { 3069 sdinfo->satadrv_features_enabled &= 3070 ~SATA_DEV_F_E_TAGGED_QING; 3071 } else { 3072 rval = -1; 3073 } 3074 } else { 3075 rval = 0; 3076 } 3077 break; 3078 default: 3079 rval = -1; 3080 break; 3081 } 3082 return (rval); 3083 } 3084 3085 /* 3086 * Implementations of scsi tran_destroy_pkt. 3087 * Free resources allocated by sata_scsi_init_pkt() 3088 */ 3089 static void 3090 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 3091 { 3092 sata_pkt_txlate_t *spx; 3093 3094 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 3095 3096 sata_common_free_dma_rsrcs(spx); 3097 3098 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 3099 sata_pkt_free(spx); 3100 3101 scsi_hba_pkt_free(ap, pkt); 3102 } 3103 3104 /* 3105 * Implementation of scsi tran_dmafree. 3106 * Free DMA resources allocated by sata_scsi_init_pkt() 3107 */ 3108 3109 static void 3110 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt) 3111 { 3112 #ifndef __lock_lint 3113 _NOTE(ARGUNUSED(ap)) 3114 #endif 3115 sata_pkt_txlate_t *spx; 3116 3117 ASSERT(pkt != NULL); 3118 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 3119 3120 sata_common_free_dma_rsrcs(spx); 3121 } 3122 3123 /* 3124 * Implementation of scsi tran_sync_pkt. 3125 * 3126 * The assumption below is that pkt is unique - there is no need to check ap 3127 * 3128 * Synchronize DMA buffer and, if the intermediate buffer is used, copy data 3129 * into/from the real buffer. 3130 */ 3131 static void 3132 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 3133 { 3134 #ifndef __lock_lint 3135 _NOTE(ARGUNUSED(ap)) 3136 #endif 3137 int rval; 3138 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 3139 struct buf *bp; 3140 int direction; 3141 3142 ASSERT(spx != NULL); 3143 if (spx->txlt_buf_dma_handle != NULL) { 3144 direction = spx->txlt_sata_pkt-> 3145 satapkt_cmd.satacmd_flags.sata_data_direction; 3146 if (spx->txlt_sata_pkt != NULL && 3147 direction != SATA_DIR_NODATA_XFER) { 3148 if (spx->txlt_tmp_buf != NULL) { 3149 /* Intermediate DMA buffer used */ 3150 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3151 3152 if (direction & SATA_DIR_WRITE) { 3153 bcopy(bp->b_un.b_addr, 3154 spx->txlt_tmp_buf, bp->b_bcount); 3155 } 3156 } 3157 /* Sync the buffer for device or for CPU */ 3158 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 3159 (direction & SATA_DIR_WRITE) ? 3160 DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU); 3161 ASSERT(rval == DDI_SUCCESS); 3162 if (spx->txlt_tmp_buf != NULL && 3163 !(direction & SATA_DIR_WRITE)) { 3164 /* Intermediate DMA buffer used for read */ 3165 bcopy(spx->txlt_tmp_buf, 3166 bp->b_un.b_addr, bp->b_bcount); 3167 } 3168 3169 } 3170 } 3171 } 3172 3173 3174 3175 /* ******************* SATA - SCSI Translation functions **************** */ 3176 /* 3177 * SCSI to SATA pkt and command translation and SATA to SCSI status/error 3178 * translation. 3179 */ 3180 3181 /* 3182 * Checks if a device exists and can be access and translates common 3183 * scsi_pkt data to sata_pkt data. 3184 * 3185 * Flag argument indicates that a non-read/write ATA command may be sent 3186 * to HBA in arbitrary SYNC mode to execute this packet. 3187 * 3188 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and 3189 * sata_pkt was set-up. 3190 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not 3191 * exist and pkt_comp callback was scheduled. 3192 * Returns other TRAN_XXXXX values when error occured and command should be 3193 * rejected with the returned TRAN_XXXXX value. 3194 * 3195 * This function should be called with port mutex held. 3196 */ 3197 static int 3198 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason, int flag) 3199 { 3200 sata_drive_info_t *sdinfo; 3201 sata_device_t sata_device; 3202 const struct sata_cmd_flags sata_initial_cmd_flags = { 3203 SATA_DIR_NODATA_XFER, 3204 /* all other values to 0/FALSE */ 3205 }; 3206 /* 3207 * Pkt_reason has to be set if the pkt_comp callback is invoked, 3208 * and that implies TRAN_ACCEPT return value. Any other returned value 3209 * indicates that the scsi packet was not accepted (the reason will not 3210 * be checked by the scsi target driver). 3211 * To make debugging easier, we set pkt_reason to know value here. 3212 * It may be changed later when different completion reason is 3213 * determined. 3214 */ 3215 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 3216 *reason = CMD_TRAN_ERR; 3217 3218 /* Validate address */ 3219 switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst, 3220 &spx->txlt_scsi_pkt->pkt_address, &sata_device)) { 3221 3222 case -1: 3223 /* Invalid address or invalid device type */ 3224 return (TRAN_BADPKT); 3225 case 2: 3226 /* 3227 * Valid address but device type is unknown - Chack if it is 3228 * in the reset state and therefore in an indeterminate state. 3229 */ 3230 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3231 &spx->txlt_sata_pkt->satapkt_device); 3232 if (sdinfo != NULL && (sdinfo->satadrv_event_flags & 3233 (SATA_EVNT_DEVICE_RESET | 3234 SATA_EVNT_INPROC_DEVICE_RESET)) != 0) { 3235 if (!ddi_in_panic()) { 3236 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 3237 *reason = CMD_INCOMPLETE; 3238 SATADBG1(SATA_DBG_SCSI_IF, 3239 spx->txlt_sata_hba_inst, 3240 "sata_scsi_start: rejecting command " 3241 "because of device reset state\n", NULL); 3242 return (TRAN_BUSY); 3243 } 3244 } 3245 /* FALLTHROUGH */ 3246 case 1: 3247 /* valid address but no valid device - it has disappeared */ 3248 spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE; 3249 *reason = CMD_DEV_GONE; 3250 /* 3251 * The sd target driver is checking CMD_DEV_GONE pkt_reason 3252 * only in callback function (for normal requests) and 3253 * in the dump code path. 3254 * So, if the callback is available, we need to do 3255 * the callback rather than returning TRAN_FATAL_ERROR here. 3256 */ 3257 if (spx->txlt_scsi_pkt->pkt_comp != NULL) { 3258 /* scsi callback required */ 3259 if (servicing_interrupt()) { 3260 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3261 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3262 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 3263 TASKQID_INVALID) { 3264 return (TRAN_BUSY); 3265 } 3266 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3267 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3268 spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 3269 /* Scheduling the callback failed */ 3270 return (TRAN_BUSY); 3271 } 3272 3273 return (TRAN_ACCEPT); 3274 } 3275 return (TRAN_FATAL_ERROR); 3276 default: 3277 /* all OK; pkt reason will be overwritten later */ 3278 break; 3279 } 3280 /* 3281 * If pkt is to be executed in polling mode and a command will not be 3282 * emulated in SATA module (requires sending a non-read/write ATA 3283 * command to HBA driver in arbitrary SYNC mode) and we are in the 3284 * interrupt context and not in the panic dump, then reject the packet 3285 * to avoid a possible interrupt stack overrun or hang caused by 3286 * a potentially blocked interrupt. 3287 */ 3288 if (((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0 || flag != 0) && 3289 servicing_interrupt() && !ddi_in_panic()) { 3290 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 3291 "sata_scsi_start: rejecting synchronous command because " 3292 "of interrupt context\n", NULL); 3293 return (TRAN_BUSY); 3294 } 3295 3296 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3297 &spx->txlt_sata_pkt->satapkt_device); 3298 3299 /* 3300 * If device is in reset condition, reject the packet with 3301 * TRAN_BUSY, unless: 3302 * 1. system is panicking (dumping) 3303 * In such case only one thread is running and there is no way to 3304 * process reset. 3305 * 2. cfgadm operation is is progress (internal APCTL lock is set) 3306 * Some cfgadm operations involve drive commands, so reset condition 3307 * needs to be ignored for IOCTL operations. 3308 */ 3309 if ((sdinfo->satadrv_event_flags & 3310 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) { 3311 3312 if (!ddi_in_panic() && 3313 ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst, 3314 sata_device.satadev_addr.cport) & 3315 SATA_APCTL_LOCK_PORT_BUSY) == 0)) { 3316 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 3317 *reason = CMD_INCOMPLETE; 3318 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3319 "sata_scsi_start: rejecting command because " 3320 "of device reset state\n", NULL); 3321 return (TRAN_BUSY); 3322 } 3323 } 3324 3325 /* 3326 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by 3327 * sata_scsi_pkt_init() because pkt init had to work also with 3328 * non-existing devices. 3329 * Now we know that the packet was set-up for a real device, so its 3330 * type is known. 3331 */ 3332 spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type; 3333 3334 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags; 3335 if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst, 3336 sata_device.satadev_addr.cport)->cport_event_flags & 3337 SATA_APCTL_LOCK_PORT_BUSY) != 0) { 3338 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 3339 sata_ignore_dev_reset = B_TRUE; 3340 } 3341 /* 3342 * At this point the generic translation routine determined that the 3343 * scsi packet should be accepted. Packet completion reason may be 3344 * changed later when a different completion reason is determined. 3345 */ 3346 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 3347 *reason = CMD_CMPLT; 3348 3349 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) { 3350 /* Synchronous execution */ 3351 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH | 3352 SATA_OPMODE_POLLING; 3353 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 3354 sata_ignore_dev_reset = ddi_in_panic(); 3355 } else { 3356 /* Asynchronous execution */ 3357 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH | 3358 SATA_OPMODE_INTERRUPTS; 3359 } 3360 /* Convert queuing information */ 3361 if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG) 3362 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag = 3363 B_TRUE; 3364 else if (spx->txlt_scsi_pkt->pkt_flags & 3365 (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD)) 3366 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag = 3367 B_TRUE; 3368 3369 /* Always limit pkt time */ 3370 if (spx->txlt_scsi_pkt->pkt_time == 0) 3371 spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time; 3372 else 3373 /* Pass on scsi_pkt time */ 3374 spx->txlt_sata_pkt->satapkt_time = 3375 spx->txlt_scsi_pkt->pkt_time; 3376 3377 return (TRAN_ACCEPT); 3378 } 3379 3380 3381 /* 3382 * Translate ATA Identify Device data to SCSI Inquiry data. 3383 * This function may be called only for ATA devices. 3384 * This function should not be called for ATAPI devices - they 3385 * respond directly to SCSI Inquiry command. 3386 * 3387 * SATA Identify Device data has to be valid in sata_drive_info. 3388 * Buffer has to accomodate the inquiry length (36 bytes). 3389 * 3390 * This function should be called with a port mutex held. 3391 */ 3392 static void 3393 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst, 3394 sata_drive_info_t *sdinfo, uint8_t *buf) 3395 { 3396 3397 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 3398 struct sata_id *sid = &sdinfo->satadrv_id; 3399 3400 /* Start with a nice clean slate */ 3401 bzero((void *)inq, sizeof (struct scsi_inquiry)); 3402 3403 /* 3404 * Rely on the dev_type for setting paripheral qualifier. 3405 * Assume that DTYPE_RODIRECT applies to CD/DVD R/W devices. 3406 * It could be that DTYPE_OPTICAL could also qualify in the future. 3407 * ATAPI Inquiry may provide more data to the target driver. 3408 */ 3409 inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 3410 DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */ 3411 3412 /* CFA type device is not a removable media device */ 3413 inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) && 3414 (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0; 3415 inq->inq_qual = 0; /* Device type qualifier (obsolete in SCSI3? */ 3416 inq->inq_iso = 0; /* ISO version */ 3417 inq->inq_ecma = 0; /* ECMA version */ 3418 inq->inq_ansi = 3; /* ANSI version - SCSI 3 */ 3419 inq->inq_aenc = 0; /* Async event notification cap. */ 3420 inq->inq_trmiop = 0; /* Supports TERMINATE I/O PROC msg - NO */ 3421 inq->inq_normaca = 0; /* setting NACA bit supported - NO */ 3422 inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */ 3423 inq->inq_len = 31; /* Additional length */ 3424 inq->inq_dualp = 0; /* dual port device - NO */ 3425 inq->inq_reladdr = 0; /* Supports relative addressing - NO */ 3426 inq->inq_sync = 0; /* Supports synchronous data xfers - NO */ 3427 inq->inq_linked = 0; /* Supports linked commands - NO */ 3428 /* 3429 * Queuing support - controller has to 3430 * support some sort of command queuing. 3431 */ 3432 if (SATA_QDEPTH(sata_hba_inst) > 1) 3433 inq->inq_cmdque = 1; /* Supports command queueing - YES */ 3434 else 3435 inq->inq_cmdque = 0; /* Supports command queueing - NO */ 3436 inq->inq_sftre = 0; /* Supports Soft Reset option - NO ??? */ 3437 inq->inq_wbus32 = 0; /* Supports 32 bit wide data xfers - NO */ 3438 inq->inq_wbus16 = 0; /* Supports 16 bit wide data xfers - NO */ 3439 3440 #ifdef _LITTLE_ENDIAN 3441 /* Swap text fields to match SCSI format */ 3442 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 3443 swab(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 3444 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 3445 swab(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 3446 else 3447 swab(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 3448 #else /* _LITTLE_ENDIAN */ 3449 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 3450 bcopy(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 3451 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 3452 bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 3453 else 3454 bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 3455 #endif /* _LITTLE_ENDIAN */ 3456 } 3457 3458 3459 /* 3460 * Scsi response set up for invalid command (command not supported) 3461 * 3462 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3463 */ 3464 static int 3465 sata_txlt_invalid_command(sata_pkt_txlate_t *spx) 3466 { 3467 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3468 struct scsi_extended_sense *sense; 3469 3470 scsipkt->pkt_reason = CMD_CMPLT; 3471 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3472 STATE_SENT_CMD | STATE_GOT_STATUS; 3473 3474 *scsipkt->pkt_scbp = STATUS_CHECK; 3475 3476 sense = sata_arq_sense(spx); 3477 sense->es_key = KEY_ILLEGAL_REQUEST; 3478 sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE; 3479 3480 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3481 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3482 3483 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3484 scsipkt->pkt_comp != NULL) { 3485 /* scsi callback required */ 3486 if (servicing_interrupt()) { 3487 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3488 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3489 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 3490 TASKQID_INVALID) { 3491 return (TRAN_BUSY); 3492 } 3493 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3494 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3495 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 3496 /* Scheduling the callback failed */ 3497 return (TRAN_BUSY); 3498 } 3499 } 3500 return (TRAN_ACCEPT); 3501 } 3502 3503 /* 3504 * Scsi response set up for check condition with special sense key 3505 * and additional sense code. 3506 * 3507 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3508 */ 3509 static int 3510 sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code) 3511 { 3512 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 3513 int cport = SATA_TXLT_CPORT(spx); 3514 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3515 struct scsi_extended_sense *sense; 3516 3517 mutex_enter(&SATA_CPORT_MUTEX(shi, cport)); 3518 scsipkt->pkt_reason = CMD_CMPLT; 3519 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3520 STATE_SENT_CMD | STATE_GOT_STATUS; 3521 3522 *scsipkt->pkt_scbp = STATUS_CHECK; 3523 3524 sense = sata_arq_sense(spx); 3525 sense->es_key = key; 3526 sense->es_add_code = code; 3527 3528 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3529 3530 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3531 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3532 3533 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3534 scsipkt->pkt_comp != NULL) { 3535 /* scsi callback required */ 3536 if (servicing_interrupt()) { 3537 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3538 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3539 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 3540 TASKQID_INVALID) { 3541 return (TRAN_BUSY); 3542 } 3543 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3544 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3545 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 3546 /* Scheduling the callback failed */ 3547 return (TRAN_BUSY); 3548 } 3549 } 3550 return (TRAN_ACCEPT); 3551 } 3552 3553 /* 3554 * Scsi response setup for 3555 * emulated non-data command that requires no action/return data 3556 * 3557 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3558 */ 3559 static int 3560 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx) 3561 { 3562 int rval; 3563 int reason; 3564 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 3565 3566 mutex_enter(cport_mutex); 3567 3568 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 3569 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3570 mutex_exit(cport_mutex); 3571 return (rval); 3572 } 3573 mutex_exit(cport_mutex); 3574 3575 spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3576 STATE_SENT_CMD | STATE_GOT_STATUS; 3577 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 3578 *(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD; 3579 3580 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3581 "Scsi_pkt completion reason %x\n", 3582 spx->txlt_scsi_pkt->pkt_reason); 3583 3584 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 && 3585 spx->txlt_scsi_pkt->pkt_comp != NULL) { 3586 /* scsi callback required */ 3587 if (servicing_interrupt()) { 3588 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3589 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3590 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 3591 TASKQID_INVALID) { 3592 return (TRAN_BUSY); 3593 } 3594 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3595 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3596 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 3597 /* Scheduling the callback failed */ 3598 return (TRAN_BUSY); 3599 } 3600 } 3601 return (TRAN_ACCEPT); 3602 } 3603 3604 3605 /* 3606 * SATA translate command: Inquiry / Identify Device 3607 * Use cached Identify Device data for now, rather than issuing actual 3608 * Device Identify cmd request. If device is detached and re-attached, 3609 * asynchronous event processing should fetch and refresh Identify Device 3610 * data. 3611 * VPD pages supported now: 3612 * Vital Product Data page 3613 * Unit Serial Number page 3614 * Block Device Characteristics Page 3615 * ATA Information Page 3616 * 3617 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3618 */ 3619 3620 #define EVPD 1 /* Extended Vital Product Data flag */ 3621 #define CMDDT 2 /* Command Support Data - Obsolete */ 3622 #define INQUIRY_SUP_VPD_PAGE 0 /* Supported VPD Pages Page Code */ 3623 #define INQUIRY_USN_PAGE 0x80 /* Unit Serial Number Page Code */ 3624 #define INQUIRY_BDC_PAGE 0xB1 /* Block Device Characteristics Page */ 3625 /* Code */ 3626 #define INQUIRY_ATA_INFO_PAGE 0x89 /* ATA Information Page Code */ 3627 #define INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */ 3628 3629 static int 3630 sata_txlt_inquiry(sata_pkt_txlate_t *spx) 3631 { 3632 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3633 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3634 sata_drive_info_t *sdinfo; 3635 struct scsi_extended_sense *sense; 3636 int count; 3637 uint8_t *p; 3638 int i, j; 3639 uint8_t page_buf[1024]; /* Max length */ 3640 int rval, reason; 3641 ushort_t rate; 3642 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 3643 3644 /* 3645 * sata_txlt_generic_pkt_info() and sata_get_device_info() require 3646 * cport_mutex to be held while they are called. sdinfo is also 3647 * protected by cport_mutex, so we hold cport_mutex until after we've 3648 * finished using sdinfo. 3649 */ 3650 mutex_enter(cport_mutex); 3651 3652 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 3653 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3654 mutex_exit(cport_mutex); 3655 return (rval); 3656 } 3657 3658 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3659 &spx->txlt_sata_pkt->satapkt_device); 3660 3661 ASSERT(sdinfo != NULL); 3662 3663 scsipkt->pkt_reason = CMD_CMPLT; 3664 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3665 STATE_SENT_CMD | STATE_GOT_STATUS; 3666 3667 /* Reject not supported request */ 3668 if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */ 3669 *scsipkt->pkt_scbp = STATUS_CHECK; 3670 sense = sata_arq_sense(spx); 3671 sense->es_key = KEY_ILLEGAL_REQUEST; 3672 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3673 goto done; 3674 } 3675 3676 /* Valid Inquiry request */ 3677 *scsipkt->pkt_scbp = STATUS_GOOD; 3678 3679 if (bp == NULL || bp->b_un.b_addr == NULL || bp->b_bcount == 0) 3680 goto done; 3681 3682 /* 3683 * Because it is fully emulated command storing data 3684 * programatically in the specified buffer, release 3685 * preallocated DMA resources before storing data in the buffer, 3686 * so no unwanted DMA sync would take place. 3687 */ 3688 sata_scsi_dmafree(NULL, scsipkt); 3689 3690 if (!(scsipkt->pkt_cdbp[1] & EVPD)) { 3691 /* Standard Inquiry Data request */ 3692 struct scsi_inquiry inq; 3693 unsigned int bufsize; 3694 3695 sata_identdev_to_inquiry(spx->txlt_sata_hba_inst, 3696 sdinfo, (uint8_t *)&inq); 3697 /* Copy no more than requested */ 3698 count = MIN(bp->b_bcount, sizeof (struct scsi_inquiry)); 3699 bufsize = scsipkt->pkt_cdbp[4]; 3700 bufsize |= scsipkt->pkt_cdbp[3] << 8; 3701 count = MIN(count, bufsize); 3702 bcopy(&inq, bp->b_un.b_addr, count); 3703 3704 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3705 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 3706 bufsize - count : 0; 3707 goto done; 3708 } 3709 3710 /* 3711 * peripheral_qualifier = 0; 3712 * 3713 * We are dealing only with HD and will be 3714 * dealing with CD/DVD devices soon 3715 */ 3716 uint8_t peripheral_device_type = 3717 sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 3718 DTYPE_DIRECT : DTYPE_RODIRECT; 3719 3720 bzero(page_buf, sizeof (page_buf)); 3721 3722 switch ((uint_t)scsipkt->pkt_cdbp[2]) { 3723 case INQUIRY_SUP_VPD_PAGE: 3724 /* 3725 * Request for supported Vital Product Data pages. 3726 */ 3727 page_buf[0] = peripheral_device_type; 3728 page_buf[1] = INQUIRY_SUP_VPD_PAGE; 3729 page_buf[2] = 0; 3730 page_buf[3] = 4; /* page length */ 3731 page_buf[4] = INQUIRY_SUP_VPD_PAGE; 3732 page_buf[5] = INQUIRY_USN_PAGE; 3733 page_buf[6] = INQUIRY_BDC_PAGE; 3734 page_buf[7] = INQUIRY_ATA_INFO_PAGE; 3735 /* Copy no more than requested */ 3736 count = MIN(bp->b_bcount, 8); 3737 bcopy(page_buf, bp->b_un.b_addr, count); 3738 break; 3739 3740 case INQUIRY_USN_PAGE: 3741 /* 3742 * Request for Unit Serial Number page. 3743 * Set-up the page. 3744 */ 3745 page_buf[0] = peripheral_device_type; 3746 page_buf[1] = INQUIRY_USN_PAGE; 3747 page_buf[2] = 0; 3748 /* remaining page length */ 3749 page_buf[3] = SATA_ID_SERIAL_LEN; 3750 3751 /* 3752 * Copy serial number from Identify Device data 3753 * words into the inquiry page and swap bytes 3754 * when necessary. 3755 */ 3756 p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser); 3757 #ifdef _LITTLE_ENDIAN 3758 swab(p, &page_buf[4], SATA_ID_SERIAL_LEN); 3759 #else 3760 bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN); 3761 #endif 3762 /* 3763 * Least significant character of the serial 3764 * number shall appear as the last byte, 3765 * according to SBC-3 spec. 3766 * Count trailing spaces to determine the 3767 * necessary shift length. 3768 */ 3769 p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1]; 3770 for (j = 0; j < SATA_ID_SERIAL_LEN; j++) { 3771 if (*(p - j) != '\0' && *(p - j) != '\040') 3772 break; 3773 } 3774 3775 /* 3776 * Shift SN string right, so that the last 3777 * non-blank character would appear in last 3778 * byte of SN field in the page. 3779 * 'j' is the shift length. 3780 */ 3781 for (i = 0; i < (SATA_ID_SERIAL_LEN - j) && j != 0; i++, p--) 3782 *p = *(p - j); 3783 3784 /* 3785 * Add leading spaces - same number as the 3786 * shift size 3787 */ 3788 for (; j > 0; j--) 3789 page_buf[4 + j - 1] = '\040'; 3790 3791 count = MIN(bp->b_bcount, SATA_ID_SERIAL_LEN + 4); 3792 bcopy(page_buf, bp->b_un.b_addr, count); 3793 break; 3794 3795 case INQUIRY_BDC_PAGE: 3796 /* 3797 * Request for Block Device Characteristics 3798 * page. Set-up the page. 3799 */ 3800 page_buf[0] = peripheral_device_type; 3801 page_buf[1] = INQUIRY_BDC_PAGE; 3802 page_buf[2] = 0; 3803 /* remaining page length */ 3804 page_buf[3] = SATA_ID_BDC_LEN; 3805 3806 rate = sdinfo->satadrv_id.ai_medrotrate; 3807 page_buf[4] = (rate >> 8) & 0xff; 3808 page_buf[5] = rate & 0xff; 3809 page_buf[6] = 0; 3810 page_buf[7] = sdinfo->satadrv_id.ai_nomformfactor & 0xf; 3811 3812 count = MIN(bp->b_bcount, SATA_ID_BDC_LEN + 4); 3813 bcopy(page_buf, bp->b_un.b_addr, count); 3814 break; 3815 3816 case INQUIRY_ATA_INFO_PAGE: 3817 /* 3818 * Request for ATA Information page. 3819 */ 3820 page_buf[0] = peripheral_device_type; 3821 page_buf[1] = INQUIRY_ATA_INFO_PAGE; 3822 page_buf[2] = (SATA_ID_ATA_INFO_LEN >> 8) & 0xff; 3823 page_buf[3] = SATA_ID_ATA_INFO_LEN & 0xff; 3824 /* page_buf[4-7] reserved */ 3825 #ifdef _LITTLE_ENDIAN 3826 bcopy("ATA ", &page_buf[8], 8); 3827 swab(sdinfo->satadrv_id.ai_model, &page_buf[16], 16); 3828 if (strncmp(&sdinfo->satadrv_id.ai_fw[4], " ", 4) == 0) { 3829 swab(sdinfo->satadrv_id.ai_fw, &page_buf[32], 4); 3830 } else { 3831 swab(&sdinfo->satadrv_id.ai_fw[4], &page_buf[32], 4); 3832 } 3833 #else /* _LITTLE_ENDIAN */ 3834 bcopy("ATA ", &page_buf[8], 8); 3835 bcopy(sdinfo->satadrv_id.ai_model, &page_buf[16], 16); 3836 if (strncmp(&sdinfo->satadrv_id.ai_fw[4], " ", 4) == 0) { 3837 bcopy(sdinfo->satadrv_id.ai_fw, &page_buf[32], 4); 3838 } else { 3839 bcopy(&sdinfo->satadrv_id.ai_fw[4], &page_buf[32], 4); 3840 } 3841 #endif /* _LITTLE_ENDIAN */ 3842 /* 3843 * page_buf[36-55] which defines the device 3844 * signature is not defined at this 3845 * time. 3846 */ 3847 3848 /* Set the command code */ 3849 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 3850 page_buf[56] = SATAC_ID_DEVICE; 3851 } else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPI) { 3852 page_buf[56] = SATAC_ID_PACKET_DEVICE; 3853 } 3854 /* 3855 * If the command code, page_buf[56], is not 3856 * zero and if one of the identify commands 3857 * succeeds, return the identify data. 3858 */ 3859 if (page_buf[56] != 0) { 3860 sata_drive_info_t temp_info = { 3861 .satadrv_addr = sdinfo->satadrv_addr, 3862 .satadrv_type = sdinfo->satadrv_type, 3863 }; 3864 3865 /* 3866 * It appears calls to an HBA's start (sata_hba_start) 3867 * method (which sata_fetch_device_identify_data_retry() 3868 * calls) must not be done while holding cport_mutex. 3869 * 3870 * A packet's completion routine may call back into 3871 * the sata framework and deadlock (and all extant 3872 * calls to the HBA's start method either drop and 3873 * re-acquire cport_mutex, or never held cport_mutex). 3874 * 3875 * sdinfo is protected by cport_mutex, so we need to 3876 * obtain the SATA address and type from sdinfo 3877 * before releasing cport_mutex and submitting the 3878 * request. We reacquire cport_mutex to simplfy 3879 * cleanup after the done label. 3880 */ 3881 mutex_exit(cport_mutex); 3882 (void) sata_fetch_device_identify_data( 3883 spx->txlt_sata_hba_inst, &temp_info); 3884 mutex_enter(cport_mutex); 3885 3886 /* 3887 * If sata_fetch_device_identify_data() 3888 * fails, the bcopy() is harmless since we're copying 3889 * zeros back over zeros. If it succeeds, we're 3890 * copying over the portion of the response we need. 3891 */ 3892 bcopy(&temp_info.satadrv_id, &page_buf[60], 3893 sizeof (sata_id_t)); 3894 } 3895 3896 /* Need to copy out the page_buf to bp */ 3897 count = MIN(bp->b_bcount, SATA_ID_ATA_INFO_LEN + 4); 3898 bcopy(page_buf, bp->b_un.b_addr, count); 3899 break; 3900 3901 case INQUIRY_DEV_IDENTIFICATION_PAGE: 3902 /* 3903 * We may want to implement this page, when 3904 * identifiers are common for SATA devices 3905 * But not now. 3906 */ 3907 /*FALLTHROUGH*/ 3908 3909 default: 3910 /* Request for unsupported VPD page */ 3911 *scsipkt->pkt_scbp = STATUS_CHECK; 3912 sense = sata_arq_sense(spx); 3913 sense->es_key = KEY_ILLEGAL_REQUEST; 3914 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3915 goto done; 3916 } 3917 3918 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3919 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 3920 scsipkt->pkt_cdbp[4] - count : 0; 3921 3922 done: 3923 mutex_exit(cport_mutex); 3924 3925 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3926 "Scsi_pkt completion reason %x\n", 3927 scsipkt->pkt_reason); 3928 3929 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3930 scsipkt->pkt_comp != NULL) { 3931 /* scsi callback required */ 3932 if (servicing_interrupt()) { 3933 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3934 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3935 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 3936 TASKQID_INVALID) { 3937 return (TRAN_BUSY); 3938 } 3939 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3940 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3941 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 3942 /* Scheduling the callback failed */ 3943 return (TRAN_BUSY); 3944 } 3945 } 3946 return (TRAN_ACCEPT); 3947 } 3948 3949 /* 3950 * SATA translate command: Request Sense. 3951 * 3952 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3953 * At the moment this is an emulated command (ATA version for SATA hard disks). 3954 * May be translated into Check Power Mode command in the future. 3955 * 3956 * Note: There is a mismatch between already implemented Informational 3957 * Exception Mode Select page 0x1C and this function. 3958 * When MRIE bit is set in page 0x1C, Request Sense is supposed to return 3959 * NO SENSE and set additional sense code to the exception code - this is not 3960 * implemented here. 3961 */ 3962 static int 3963 sata_txlt_request_sense(sata_pkt_txlate_t *spx) 3964 { 3965 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3966 struct scsi_extended_sense sense; 3967 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3968 sata_drive_info_t *sdinfo; 3969 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 3970 int rval, reason, power_state = 0; 3971 kmutex_t *cport_mutex; 3972 3973 cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 3974 mutex_enter(cport_mutex); 3975 3976 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 3977 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3978 mutex_exit(cport_mutex); 3979 return (rval); 3980 } 3981 3982 scsipkt->pkt_reason = CMD_CMPLT; 3983 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3984 STATE_SENT_CMD | STATE_GOT_STATUS; 3985 *scsipkt->pkt_scbp = STATUS_GOOD; 3986 3987 /* 3988 * when CONTROL field's NACA bit == 1 3989 * return ILLEGAL_REQUEST 3990 */ 3991 if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) { 3992 mutex_exit(cport_mutex); 3993 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 3994 SD_SCSI_ASC_CMD_SEQUENCE_ERR)); 3995 } 3996 3997 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3998 &spx->txlt_sata_pkt->satapkt_device); 3999 ASSERT(sdinfo != NULL); 4000 4001 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH; 4002 4003 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE); 4004 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE; 4005 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4006 if (sata_hba_start(spx, &rval) != 0) { 4007 mutex_exit(cport_mutex); 4008 return (rval); 4009 } 4010 if (scmd->satacmd_error_reg != 0) { 4011 mutex_exit(cport_mutex); 4012 return (sata_txlt_check_condition(spx, KEY_NO_SENSE, 4013 SD_SCSI_ASC_NO_ADD_SENSE)); 4014 } 4015 4016 switch (scmd->satacmd_sec_count_lsb) { 4017 case SATA_PWRMODE_STANDBY: /* device in standby mode */ 4018 if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED) 4019 power_state = SATA_POWER_STOPPED; 4020 else { 4021 power_state = SATA_POWER_STANDBY; 4022 sdinfo->satadrv_power_level = SATA_POWER_STANDBY; 4023 } 4024 break; 4025 case SATA_PWRMODE_IDLE: /* device in idle mode */ 4026 power_state = SATA_POWER_IDLE; 4027 sdinfo->satadrv_power_level = SATA_POWER_IDLE; 4028 break; 4029 case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */ 4030 default: /* 0x40, 0x41 active mode */ 4031 if (sdinfo->satadrv_power_level == SATA_POWER_IDLE) 4032 power_state = SATA_POWER_IDLE; 4033 else { 4034 power_state = SATA_POWER_ACTIVE; 4035 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 4036 } 4037 break; 4038 } 4039 4040 mutex_exit(cport_mutex); 4041 4042 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4043 /* 4044 * Because it is fully emulated command storing data 4045 * programatically in the specified buffer, release 4046 * preallocated DMA resources before storing data in the buffer, 4047 * so no unwanted DMA sync would take place. 4048 */ 4049 int count = MIN(bp->b_bcount, 4050 sizeof (struct scsi_extended_sense)); 4051 sata_scsi_dmafree(NULL, scsipkt); 4052 bzero(&sense, sizeof (struct scsi_extended_sense)); 4053 sense.es_valid = 0; /* Valid LBA */ 4054 sense.es_class = 7; /* Response code 0x70 - current err */ 4055 sense.es_key = KEY_NO_SENSE; 4056 sense.es_add_len = 6; /* Additional length */ 4057 /* Copy no more than requested */ 4058 bcopy(&sense, bp->b_un.b_addr, count); 4059 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4060 scsipkt->pkt_resid = 0; 4061 switch (power_state) { 4062 case SATA_POWER_IDLE: 4063 case SATA_POWER_STANDBY: 4064 sense.es_add_code = 4065 SD_SCSI_ASC_LOW_POWER_CONDITION_ON; 4066 break; 4067 case SATA_POWER_STOPPED: 4068 sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE; 4069 break; 4070 case SATA_POWER_ACTIVE: 4071 default: 4072 break; 4073 } 4074 } 4075 4076 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4077 "Scsi_pkt completion reason %x\n", 4078 scsipkt->pkt_reason); 4079 4080 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4081 scsipkt->pkt_comp != NULL) { 4082 /* scsi callback required */ 4083 if (servicing_interrupt()) { 4084 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4085 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4086 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 4087 TASKQID_INVALID) { 4088 return (TRAN_BUSY); 4089 } 4090 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4091 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4092 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 4093 /* Scheduling the callback failed */ 4094 return (TRAN_BUSY); 4095 } 4096 } 4097 return (TRAN_ACCEPT); 4098 } 4099 4100 /* 4101 * SATA translate command: Test Unit Ready 4102 * (ATA version for SATA hard disks). 4103 * It is translated into the Check Power Mode command. 4104 * 4105 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4106 */ 4107 static int 4108 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx) 4109 { 4110 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4111 struct scsi_extended_sense *sense; 4112 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4113 sata_drive_info_t *sdinfo; 4114 int power_state; 4115 int rval, reason; 4116 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 4117 4118 mutex_enter(cport_mutex); 4119 4120 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 4121 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4122 mutex_exit(cport_mutex); 4123 return (rval); 4124 } 4125 4126 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4127 &spx->txlt_sata_pkt->satapkt_device); 4128 ASSERT(sdinfo != NULL); 4129 4130 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH; 4131 4132 /* send CHECK POWER MODE command */ 4133 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE); 4134 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE; 4135 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4136 if (sata_hba_start(spx, &rval) != 0) { 4137 mutex_exit(cport_mutex); 4138 return (rval); 4139 } 4140 4141 if (scmd->satacmd_error_reg != 0) { 4142 mutex_exit(cport_mutex); 4143 return (sata_txlt_check_condition(spx, KEY_NOT_READY, 4144 SD_SCSI_ASC_LU_NOT_RESPONSE)); 4145 } 4146 4147 power_state = scmd->satacmd_sec_count_lsb; 4148 4149 /* 4150 * return NOT READY when device in STOPPED mode 4151 */ 4152 if (power_state == SATA_PWRMODE_STANDBY && 4153 sdinfo->satadrv_power_level == SATA_POWER_STOPPED) { 4154 *scsipkt->pkt_scbp = STATUS_CHECK; 4155 sense = sata_arq_sense(spx); 4156 sense->es_key = KEY_NOT_READY; 4157 sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY; 4158 } else { 4159 /* 4160 * For other power mode, return GOOD status 4161 */ 4162 *scsipkt->pkt_scbp = STATUS_GOOD; 4163 } 4164 4165 scsipkt->pkt_reason = CMD_CMPLT; 4166 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4167 STATE_SENT_CMD | STATE_GOT_STATUS; 4168 4169 mutex_exit(cport_mutex); 4170 4171 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4172 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4173 4174 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4175 scsipkt->pkt_comp != NULL) { 4176 /* scsi callback required */ 4177 if (servicing_interrupt()) { 4178 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4179 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4180 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 4181 TASKQID_INVALID) { 4182 return (TRAN_BUSY); 4183 } 4184 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4185 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4186 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 4187 /* Scheduling the callback failed */ 4188 return (TRAN_BUSY); 4189 } 4190 } 4191 4192 return (TRAN_ACCEPT); 4193 } 4194 4195 /* 4196 * SATA translate command: Start Stop Unit 4197 * Translation depends on a command: 4198 * 4199 * Power condition bits will be supported 4200 * and the power level should be maintained by SATL, 4201 * When SATL received a command, it will check the 4202 * power level firstly, and return the status according 4203 * to SAT2 v2.6 and SAT-2 Standby Modifications 4204 * 4205 * SPC-4/SBC-3 SATL ATA power condition SATL SPC/SBC 4206 * ----------------------------------------------------------------------- 4207 * SSU_PC1 Active <==> ATA Active <==> SSU:start_bit =1 4208 * SSU_PC2 Idle <==> ATA Idle <==> N/A 4209 * SSU_PC3 Standby <==> ATA Standby <==> N/A 4210 * SSU_PC4 Stopped <==> ATA Standby <==> SSU:start_bit = 0 4211 * 4212 * Unload Media / NOT SUPPORTED YET 4213 * Load Media / NOT SUPPROTED YET 4214 * Immediate bit / NOT SUPPORTED YET (deferred error) 4215 * 4216 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 4217 * appropriate values in scsi_pkt fields. 4218 */ 4219 static int 4220 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx) 4221 { 4222 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4223 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4224 int rval, reason; 4225 sata_drive_info_t *sdinfo; 4226 sata_id_t *sata_id; 4227 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 4228 4229 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4230 "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1); 4231 4232 mutex_enter(cport_mutex); 4233 4234 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 4235 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4236 mutex_exit(cport_mutex); 4237 return (rval); 4238 } 4239 4240 if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) { 4241 /* IMMED bit - not supported */ 4242 mutex_exit(cport_mutex); 4243 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 4244 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4245 } 4246 4247 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH; 4248 spx->txlt_sata_pkt->satapkt_comp = NULL; 4249 4250 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4251 &spx->txlt_sata_pkt->satapkt_device); 4252 ASSERT(sdinfo != NULL); 4253 sata_id = &sdinfo->satadrv_id; 4254 4255 switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) { 4256 case 0: 4257 if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) { 4258 /* Load/Unload Media - invalid request */ 4259 goto err_out; 4260 } 4261 if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) { 4262 /* Start Unit */ 4263 sata_build_read_verify_cmd(scmd, 1, 5); 4264 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4265 /* Transfer command to HBA */ 4266 if (sata_hba_start(spx, &rval) != 0) { 4267 /* Pkt not accepted for execution */ 4268 mutex_exit(cport_mutex); 4269 return (rval); 4270 } 4271 if (scmd->satacmd_error_reg != 0) { 4272 goto err_out; 4273 } 4274 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 4275 } else { 4276 /* Stop Unit */ 4277 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 4278 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4279 if (sata_hba_start(spx, &rval) != 0) { 4280 mutex_exit(cport_mutex); 4281 return (rval); 4282 } else { 4283 if (scmd->satacmd_error_reg != 0) { 4284 goto err_out; 4285 } 4286 } 4287 /* ata standby immediate command */ 4288 sata_build_generic_cmd(scmd, SATAC_STANDBY_IM); 4289 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4290 if (sata_hba_start(spx, &rval) != 0) { 4291 mutex_exit(cport_mutex); 4292 return (rval); 4293 } 4294 if (scmd->satacmd_error_reg != 0) { 4295 goto err_out; 4296 } 4297 sdinfo->satadrv_power_level = SATA_POWER_STOPPED; 4298 } 4299 break; 4300 case 0x1: 4301 sata_build_generic_cmd(scmd, SATAC_IDLE); 4302 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4303 if (sata_hba_start(spx, &rval) != 0) { 4304 mutex_exit(cport_mutex); 4305 return (rval); 4306 } 4307 if (scmd->satacmd_error_reg != 0) { 4308 goto err_out; 4309 } 4310 sata_build_read_verify_cmd(scmd, 1, 5); 4311 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4312 /* Transfer command to HBA */ 4313 if (sata_hba_start(spx, &rval) != 0) { 4314 /* Pkt not accepted for execution */ 4315 mutex_exit(cport_mutex); 4316 return (rval); 4317 } else { 4318 if (scmd->satacmd_error_reg != 0) { 4319 goto err_out; 4320 } 4321 } 4322 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 4323 break; 4324 case 0x2: 4325 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 4326 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4327 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) { 4328 if (sata_hba_start(spx, &rval) != 0) { 4329 mutex_exit(cport_mutex); 4330 return (rval); 4331 } 4332 if (scmd->satacmd_error_reg != 0) { 4333 goto err_out; 4334 } 4335 } 4336 sata_build_generic_cmd(scmd, SATAC_IDLE); 4337 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4338 if (sata_hba_start(spx, &rval) != 0) { 4339 mutex_exit(cport_mutex); 4340 return (rval); 4341 } 4342 if (scmd->satacmd_error_reg != 0) { 4343 goto err_out; 4344 } 4345 if ((scsipkt->pkt_cdbp[3] & START_STOP_MODIFIER_MASK)) { 4346 /* 4347 * POWER CONDITION MODIFIER bit set 4348 * to 0x1 or larger it will be handled 4349 * on the same way as bit = 0x1 4350 */ 4351 if (!(sata_id->ai_cmdset84 & 4352 SATA_IDLE_UNLOAD_SUPPORTED)) { 4353 sdinfo->satadrv_power_level = SATA_POWER_IDLE; 4354 break; 4355 } 4356 sata_build_generic_cmd(scmd, SATAC_IDLE_IM); 4357 scmd->satacmd_features_reg = 0x44; 4358 scmd->satacmd_lba_low_lsb = 0x4c; 4359 scmd->satacmd_lba_mid_lsb = 0x4e; 4360 scmd->satacmd_lba_high_lsb = 0x55; 4361 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4362 if (sata_hba_start(spx, &rval) != 0) { 4363 mutex_exit(cport_mutex); 4364 return (rval); 4365 } 4366 if (scmd->satacmd_error_reg != 0) { 4367 goto err_out; 4368 } 4369 } 4370 sdinfo->satadrv_power_level = SATA_POWER_IDLE; 4371 break; 4372 case 0x3: 4373 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 4374 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4375 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) { 4376 if (sata_hba_start(spx, &rval) != 0) { 4377 mutex_exit(cport_mutex); 4378 return (rval); 4379 } 4380 if (scmd->satacmd_error_reg != 0) { 4381 goto err_out; 4382 } 4383 } 4384 sata_build_generic_cmd(scmd, SATAC_STANDBY); 4385 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4386 if (sata_hba_start(spx, &rval) != 0) { 4387 mutex_exit(cport_mutex); 4388 return (rval); 4389 } 4390 if (scmd->satacmd_error_reg != 0) { 4391 goto err_out; 4392 } 4393 sdinfo->satadrv_power_level = SATA_POWER_STANDBY; 4394 break; 4395 case 0x7: 4396 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE); 4397 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE; 4398 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4399 if (sata_hba_start(spx, &rval) != 0) { 4400 mutex_exit(cport_mutex); 4401 return (rval); 4402 } 4403 if (scmd->satacmd_error_reg != 0) { 4404 goto err_out; 4405 } 4406 switch (scmd->satacmd_sec_count_lsb) { 4407 case SATA_PWRMODE_STANDBY: 4408 sata_build_generic_cmd(scmd, SATAC_STANDBY); 4409 scmd->satacmd_sec_count_msb = sata_get_standby_timer( 4410 sdinfo->satadrv_standby_timer); 4411 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4412 if (sata_hba_start(spx, &rval) != 0) { 4413 mutex_exit(cport_mutex); 4414 return (rval); 4415 } else { 4416 if (scmd->satacmd_error_reg != 0) { 4417 goto err_out; 4418 } 4419 } 4420 break; 4421 case SATA_PWRMODE_IDLE: 4422 sata_build_generic_cmd(scmd, SATAC_IDLE); 4423 scmd->satacmd_sec_count_msb = sata_get_standby_timer( 4424 sdinfo->satadrv_standby_timer); 4425 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4426 if (sata_hba_start(spx, &rval) != 0) { 4427 mutex_exit(cport_mutex); 4428 return (rval); 4429 } else { 4430 if (scmd->satacmd_error_reg != 0) { 4431 goto err_out; 4432 } 4433 } 4434 break; 4435 case SATA_PWRMODE_ACTIVE_SPINDOWN: 4436 case SATA_PWRMODE_ACTIVE_SPINUP: 4437 case SATA_PWRMODE_ACTIVE: 4438 sata_build_generic_cmd(scmd, SATAC_IDLE); 4439 scmd->satacmd_sec_count_msb = sata_get_standby_timer( 4440 sdinfo->satadrv_standby_timer); 4441 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4442 if (sata_hba_start(spx, &rval) != 0) { 4443 mutex_exit(cport_mutex); 4444 return (rval); 4445 } 4446 if (scmd->satacmd_error_reg != 0) { 4447 goto err_out; 4448 } 4449 sata_build_read_verify_cmd(scmd, 1, 5); 4450 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4451 if (sata_hba_start(spx, &rval) != 0) { 4452 mutex_exit(cport_mutex); 4453 return (rval); 4454 } 4455 if (scmd->satacmd_error_reg != 0) { 4456 goto err_out; 4457 } 4458 break; 4459 default: 4460 goto err_out; 4461 } 4462 break; 4463 case 0xb: 4464 if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) == 4465 0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) { 4466 mutex_exit(cport_mutex); 4467 return (sata_txlt_check_condition(spx, 4468 KEY_ILLEGAL_REQUEST, 4469 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4470 } 4471 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 4472 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4473 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) { 4474 if (sata_hba_start(spx, &rval) != 0) { 4475 mutex_exit(cport_mutex); 4476 return (rval); 4477 } 4478 if (scmd->satacmd_error_reg != 0) { 4479 goto err_out; 4480 } 4481 sata_build_generic_cmd(scmd, SATAC_STANDBY_IM); 4482 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4483 if (sata_hba_start(spx, &rval) != 0) { 4484 mutex_exit(cport_mutex); 4485 return (rval); 4486 } 4487 if (scmd->satacmd_error_reg != 0) { 4488 goto err_out; 4489 } 4490 } 4491 bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4); 4492 break; 4493 default: 4494 err_out: 4495 mutex_exit(cport_mutex); 4496 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 4497 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4498 } 4499 4500 /* 4501 * Since it was a synchronous command, 4502 * a callback function will be called directly. 4503 */ 4504 mutex_exit(cport_mutex); 4505 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4506 "synchronous execution status %x\n", 4507 spx->txlt_sata_pkt->satapkt_reason); 4508 4509 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4510 scsipkt->pkt_comp != NULL) { 4511 sata_set_arq_data(spx->txlt_sata_pkt); 4512 if (servicing_interrupt()) { 4513 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4514 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4515 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 4516 TASKQID_INVALID) { 4517 return (TRAN_BUSY); 4518 } 4519 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4520 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4521 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 4522 /* Scheduling the callback failed */ 4523 return (TRAN_BUSY); 4524 } 4525 } 4526 else 4527 4528 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 4529 4530 return (TRAN_ACCEPT); 4531 4532 } 4533 4534 /* 4535 * SATA translate command: Read Capacity. 4536 * Emulated command for SATA disks. 4537 * Capacity is retrieved from cached Idenifty Device data. 4538 * Identify Device data shows effective disk capacity, not the native 4539 * capacity, which may be limitted by Set Max Address command. 4540 * This is ATA version for SATA hard disks. 4541 * 4542 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4543 */ 4544 static int 4545 sata_txlt_read_capacity(sata_pkt_txlate_t *spx) 4546 { 4547 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4548 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4549 sata_drive_info_t *sdinfo; 4550 uint64_t val; 4551 uint32_t lbsize = DEV_BSIZE; 4552 uchar_t *rbuf; 4553 int rval, reason; 4554 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 4555 4556 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4557 "sata_txlt_read_capacity: ", NULL); 4558 4559 mutex_enter(cport_mutex); 4560 4561 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 4562 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4563 mutex_exit(cport_mutex); 4564 return (rval); 4565 } 4566 4567 scsipkt->pkt_reason = CMD_CMPLT; 4568 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4569 STATE_SENT_CMD | STATE_GOT_STATUS; 4570 *scsipkt->pkt_scbp = STATUS_GOOD; 4571 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4572 /* 4573 * Because it is fully emulated command storing data 4574 * programatically in the specified buffer, release 4575 * preallocated DMA resources before storing data in the buffer, 4576 * so no unwanted DMA sync would take place. 4577 */ 4578 sata_scsi_dmafree(NULL, scsipkt); 4579 4580 sdinfo = sata_get_device_info( 4581 spx->txlt_sata_hba_inst, 4582 &spx->txlt_sata_pkt->satapkt_device); 4583 4584 /* 4585 * As per SBC-3, the "returned LBA" is either the highest 4586 * addressable LBA or 0xffffffff, whichever is smaller. 4587 */ 4588 val = MIN(sdinfo->satadrv_capacity - 1, UINT32_MAX); 4589 4590 if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) { 4591 /* physical/logical sector size word is valid */ 4592 4593 if (sdinfo->satadrv_id.ai_phys_sect_sz & 4594 SATA_L2PS_BIG_SECTORS) { 4595 /* if this set 117-118 words are valid */ 4596 lbsize = sdinfo->satadrv_id.ai_words_lsec[0] | 4597 (sdinfo->satadrv_id.ai_words_lsec[1] << 16); 4598 lbsize <<= 1; /* convert from words to bytes */ 4599 } 4600 } 4601 rbuf = (uchar_t *)bp->b_un.b_addr; 4602 /* Need to swap endians to match scsi format */ 4603 rbuf[0] = (val >> 24) & 0xff; 4604 rbuf[1] = (val >> 16) & 0xff; 4605 rbuf[2] = (val >> 8) & 0xff; 4606 rbuf[3] = val & 0xff; 4607 rbuf[4] = (lbsize >> 24) & 0xff; 4608 rbuf[5] = (lbsize >> 16) & 0xff; 4609 rbuf[6] = (lbsize >> 8) & 0xff; 4610 rbuf[7] = lbsize & 0xff; 4611 4612 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4613 scsipkt->pkt_resid = 0; 4614 4615 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n", 4616 sdinfo->satadrv_capacity -1); 4617 } 4618 mutex_exit(cport_mutex); 4619 /* 4620 * If a callback was requested, do it now. 4621 */ 4622 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4623 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4624 4625 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4626 scsipkt->pkt_comp != NULL) { 4627 /* scsi callback required */ 4628 if (servicing_interrupt()) { 4629 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4630 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4631 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 4632 TASKQID_INVALID) { 4633 return (TRAN_BUSY); 4634 } 4635 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4636 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4637 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 4638 /* Scheduling the callback failed */ 4639 return (TRAN_BUSY); 4640 } 4641 } 4642 4643 return (TRAN_ACCEPT); 4644 } 4645 4646 /* 4647 * SATA translate command: Read Capacity (16). 4648 * Emulated command for SATA disks. 4649 * Info is retrieved from cached Identify Device data. 4650 * Implemented to SBC-3 (draft 21) and SAT-2 (final) specifications. 4651 * 4652 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4653 */ 4654 static int 4655 sata_txlt_read_capacity16(sata_pkt_txlate_t *spx) 4656 { 4657 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4658 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4659 sata_drive_info_t *sdinfo; 4660 uint64_t val; 4661 uint16_t l2p_exp; 4662 uint32_t lbsize = DEV_BSIZE; 4663 uchar_t *rbuf; 4664 int rval, reason; 4665 #define TPE 0x80 4666 #define TPRZ 0x40 4667 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 4668 4669 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4670 "sata_txlt_read_capacity: ", NULL); 4671 4672 mutex_enter(cport_mutex); 4673 4674 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 4675 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4676 mutex_exit(cport_mutex); 4677 return (rval); 4678 } 4679 4680 scsipkt->pkt_reason = CMD_CMPLT; 4681 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4682 STATE_SENT_CMD | STATE_GOT_STATUS; 4683 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4684 /* 4685 * Because it is fully emulated command storing data 4686 * programatically in the specified buffer, release 4687 * preallocated DMA resources before storing data in the buffer, 4688 * so no unwanted DMA sync would take place. 4689 */ 4690 sata_scsi_dmafree(NULL, scsipkt); 4691 4692 /* Check SERVICE ACTION field */ 4693 if ((scsipkt->pkt_cdbp[1] & 0x1f) != 4694 SSVC_ACTION_READ_CAPACITY_G4) { 4695 mutex_exit(cport_mutex); 4696 return (sata_txlt_check_condition(spx, 4697 KEY_ILLEGAL_REQUEST, 4698 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4699 } 4700 4701 /* Check LBA field */ 4702 if ((scsipkt->pkt_cdbp[2] != 0) || 4703 (scsipkt->pkt_cdbp[3] != 0) || 4704 (scsipkt->pkt_cdbp[4] != 0) || 4705 (scsipkt->pkt_cdbp[5] != 0) || 4706 (scsipkt->pkt_cdbp[6] != 0) || 4707 (scsipkt->pkt_cdbp[7] != 0) || 4708 (scsipkt->pkt_cdbp[8] != 0) || 4709 (scsipkt->pkt_cdbp[9] != 0)) { 4710 mutex_exit(cport_mutex); 4711 return (sata_txlt_check_condition(spx, 4712 KEY_ILLEGAL_REQUEST, 4713 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4714 } 4715 4716 /* Check PMI bit */ 4717 if (scsipkt->pkt_cdbp[14] & 0x1) { 4718 mutex_exit(cport_mutex); 4719 return (sata_txlt_check_condition(spx, 4720 KEY_ILLEGAL_REQUEST, 4721 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4722 } 4723 4724 *scsipkt->pkt_scbp = STATUS_GOOD; 4725 4726 sdinfo = sata_get_device_info( 4727 spx->txlt_sata_hba_inst, 4728 &spx->txlt_sata_pkt->satapkt_device); 4729 4730 /* last logical block address */ 4731 val = MIN(sdinfo->satadrv_capacity - 1, 4732 SCSI_READ_CAPACITY16_MAX_LBA); 4733 4734 /* logical to physical block size exponent */ 4735 l2p_exp = 0; 4736 if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) { 4737 /* physical/logical sector size word is valid */ 4738 4739 if (sdinfo->satadrv_id.ai_phys_sect_sz & 4740 SATA_L2PS_HAS_MULT) { 4741 /* multiple logical sectors per phys sectors */ 4742 l2p_exp = 4743 sdinfo->satadrv_id.ai_phys_sect_sz & 4744 SATA_L2PS_EXP_MASK; 4745 } 4746 4747 if (sdinfo->satadrv_id.ai_phys_sect_sz & 4748 SATA_L2PS_BIG_SECTORS) { 4749 /* if this set 117-118 words are valid */ 4750 lbsize = sdinfo->satadrv_id.ai_words_lsec[0] | 4751 (sdinfo->satadrv_id.ai_words_lsec[1] << 16); 4752 lbsize <<= 1; /* convert from words to bytes */ 4753 } 4754 } 4755 4756 rbuf = (uchar_t *)bp->b_un.b_addr; 4757 bzero(rbuf, bp->b_bcount); 4758 4759 /* returned logical block address */ 4760 rbuf[0] = (val >> 56) & 0xff; 4761 rbuf[1] = (val >> 48) & 0xff; 4762 rbuf[2] = (val >> 40) & 0xff; 4763 rbuf[3] = (val >> 32) & 0xff; 4764 rbuf[4] = (val >> 24) & 0xff; 4765 rbuf[5] = (val >> 16) & 0xff; 4766 rbuf[6] = (val >> 8) & 0xff; 4767 rbuf[7] = val & 0xff; 4768 rbuf[8] = (lbsize >> 24) & 0xff; 4769 rbuf[9] = (lbsize >> 16) & 0xff; 4770 rbuf[10] = (lbsize >> 8) & 0xff; 4771 rbuf[11] = lbsize & 0xff; 4772 4773 /* p_type, prot_en, unspecified by SAT-2 */ 4774 /* rbuf[12] = 0; */ 4775 4776 /* p_i_exponent, undefined by SAT-2 */ 4777 /* logical blocks per physical block exponent */ 4778 rbuf[13] = l2p_exp; 4779 4780 /* 4781 * tpe and tprz as defined in T10/10-079 r0. 4782 * TRIM support is indicated by the relevant bit in the data 4783 * set management word. Read-after-trim behavior is indicated 4784 * by the additional bits in the identify device word. Of the 4785 * three defined possibilities, we only flag read-zero. 4786 */ 4787 if (sdinfo->satadrv_id.ai_dsm & SATA_DSM_TRIM) { 4788 rbuf[14] |= TPE; 4789 4790 if ((sdinfo->satadrv_id.ai_addsupported & 4791 SATA_DETERMINISTIC_READ) && 4792 (sdinfo->satadrv_id.ai_addsupported & 4793 SATA_READ_ZERO)) { 4794 rbuf[14] |= TPRZ; 4795 } 4796 } 4797 4798 /* lowest aligned logical block address = 0 (for now) */ 4799 /* rbuf[15] = 0; */ 4800 4801 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4802 scsipkt->pkt_resid = 0; 4803 4804 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%llu\n", 4805 sdinfo->satadrv_capacity -1); 4806 } 4807 4808 mutex_exit(cport_mutex); 4809 4810 /* 4811 * If a callback was requested, do it now. 4812 */ 4813 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4814 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4815 4816 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4817 scsipkt->pkt_comp != NULL) { 4818 /* scsi callback required */ 4819 if (servicing_interrupt()) { 4820 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4821 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4822 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 4823 TASKQID_INVALID) { 4824 return (TRAN_BUSY); 4825 } 4826 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4827 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4828 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 4829 /* Scheduling the callback failed */ 4830 return (TRAN_BUSY); 4831 } 4832 } 4833 4834 return (TRAN_ACCEPT); 4835 } 4836 4837 /* 4838 * Translate command: UNMAP 4839 * 4840 * The function cannot be called in interrupt context since it may sleep. 4841 */ 4842 static int 4843 sata_txlt_unmap(sata_pkt_txlate_t *spx) 4844 { 4845 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4846 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4847 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4848 uint16_t count = 0; 4849 int synch; 4850 int rval, reason; 4851 int i, x; 4852 int bdlen = 0; 4853 int ranges = 0; 4854 int paramlen = 8; 4855 uint8_t *data, *tmpbd; 4856 sata_drive_info_t *sdinfo; 4857 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 4858 #define TRIM 0x1 4859 4860 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4861 "sata_txlt_unmap: ", NULL); 4862 4863 mutex_enter(cport_mutex); 4864 4865 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4866 &spx->txlt_sata_pkt->satapkt_device); 4867 if (sdinfo != NULL) { 4868 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4869 "DSM support 0x%x, max number of 512 byte blocks of LBA " 4870 "range entries 0x%x\n", sdinfo->satadrv_id.ai_dsm, 4871 sdinfo->satadrv_id.ai_maxcount); 4872 } 4873 4874 rval = sata_txlt_generic_pkt_info(spx, &reason, 1); 4875 if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4876 mutex_exit(cport_mutex); 4877 return (rval); 4878 } 4879 4880 /* 4881 * Need to modify bp to have TRIM data instead of UNMAP data. 4882 * Start by getting the block descriptor data length by subtracting 4883 * the 8 byte parameter list header from the parameter list length. 4884 * The block descriptor size has to be a multiple of 16 bytes. 4885 */ 4886 bdlen = scsipkt->pkt_cdbp[7]; 4887 bdlen = (bdlen << 8) + scsipkt->pkt_cdbp[8] - paramlen; 4888 if ((bdlen < 0) || ((bdlen % 16) != 0) || 4889 ((bp != NULL) && (bdlen > (bp->b_bcount - paramlen)))) { 4890 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4891 "sata_txlt_unmap: invalid block descriptor length", NULL); 4892 mutex_exit(cport_mutex); 4893 return ((sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 4894 SD_SCSI_ASC_INVALID_FIELD_IN_CDB))); 4895 } 4896 /* 4897 * If there are no parameter data or block descriptors, it is not 4898 * considered an error so just complete the command without sending 4899 * TRIM. 4900 */ 4901 if ((bdlen == 0) || (bp == NULL) || (bp->b_un.b_addr == NULL) || 4902 (bp->b_bcount == 0)) { 4903 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4904 "sata_txlt_unmap: no parameter data or block descriptors", 4905 NULL); 4906 mutex_exit(cport_mutex); 4907 return (sata_txlt_unmap_nodata_cmd(spx)); 4908 } 4909 tmpbd = (uint8_t *)bp->b_un.b_addr + paramlen; 4910 data = kmem_zalloc(bdlen, KM_SLEEP); 4911 4912 /* 4913 * Loop through all the UNMAP block descriptors and convert the data 4914 * into TRIM format. 4915 */ 4916 for (i = 0, x = 0; i < bdlen; i += 16, x += 8) { 4917 /* get range length */ 4918 data[x] = tmpbd[i+7]; 4919 data[x+1] = tmpbd[i+6]; 4920 /* get LBA */ 4921 data[x+2] = tmpbd[i+5]; 4922 data[x+3] = tmpbd[i+4]; 4923 data[x+4] = tmpbd[i+3]; 4924 data[x+5] = tmpbd[i+2]; 4925 data[x+6] = tmpbd[i+11]; 4926 data[x+7] = tmpbd[i+10]; 4927 4928 ranges++; 4929 } 4930 4931 /* 4932 * The TRIM command expects the data buffer to be a multiple of 4933 * 512-byte blocks of range entries. This means that the UNMAP buffer 4934 * may be too small. Free the original DMA resources and create a 4935 * local buffer. 4936 */ 4937 sata_common_free_dma_rsrcs(spx); 4938 4939 /* 4940 * Get count of 512-byte blocks of range entries. The length 4941 * of a range entry is 8 bytes which means one count has 64 range 4942 * entries. 4943 */ 4944 count = (ranges + 63)/64; 4945 4946 /* Allocate a buffer that is a multiple of 512 bytes. */ 4947 mutex_exit(cport_mutex); 4948 bp = sata_alloc_local_buffer(spx, count * 512); 4949 if (bp == NULL) { 4950 SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst, 4951 "sata_txlt_unmap: " 4952 "cannot allocate buffer for TRIM command", NULL); 4953 kmem_free(data, bdlen); 4954 return (TRAN_BUSY); 4955 } 4956 bp_mapin(bp); /* make data buffer accessible */ 4957 mutex_enter(cport_mutex); 4958 4959 bzero(bp->b_un.b_addr, bp->b_bcount); 4960 bcopy(data, bp->b_un.b_addr, x); 4961 kmem_free(data, bdlen); 4962 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 4963 DDI_DMA_SYNC_FORDEV); 4964 ASSERT(rval == DDI_SUCCESS); 4965 4966 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 4967 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 4968 scmd->satacmd_cmd_reg = SATAC_DSM; 4969 scmd->satacmd_sec_count_msb = (count >> 8) & 0xff; 4970 scmd->satacmd_sec_count_lsb = count & 0xff; 4971 scmd->satacmd_features_reg = TRIM; 4972 scmd->satacmd_device_reg = SATA_ADH_LBA; 4973 scmd->satacmd_status_reg = 0; 4974 scmd->satacmd_error_reg = 0; 4975 4976 /* Start processing command */ 4977 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 4978 spx->txlt_sata_pkt->satapkt_comp = 4979 sata_txlt_unmap_completion; 4980 synch = FALSE; 4981 } else { 4982 synch = TRUE; 4983 } 4984 4985 if (sata_hba_start(spx, &rval) != 0) { 4986 mutex_exit(cport_mutex); 4987 return (rval); 4988 } 4989 4990 mutex_exit(cport_mutex); 4991 4992 if (synch) { 4993 sata_txlt_unmap_completion(spx->txlt_sata_pkt); 4994 } 4995 4996 return (TRAN_ACCEPT); 4997 } 4998 4999 /* 5000 * SATA translate command: Mode Sense. 5001 * Translated into appropriate SATA command or emulated. 5002 * Saved Values Page Control (03) are not supported. 5003 * 5004 * NOTE: only caching mode sense page is currently implemented. 5005 * 5006 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 5007 */ 5008 5009 #define LLBAA 0x10 /* Long LBA Accepted */ 5010 5011 static int 5012 sata_txlt_mode_sense(sata_pkt_txlate_t *spx) 5013 { 5014 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5015 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5016 sata_drive_info_t *sdinfo; 5017 sata_id_t *sata_id; 5018 struct scsi_extended_sense *sense; 5019 int len, bdlen, count, alc_len; 5020 int pc; /* Page Control code */ 5021 uint8_t *buf; /* mode sense buffer */ 5022 int rval, reason; 5023 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 5024 5025 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5026 "sata_txlt_mode_sense, pc %x page code 0x%02x\n", 5027 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 5028 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 5029 5030 if (servicing_interrupt()) { 5031 buf = kmem_zalloc(1024, KM_NOSLEEP); 5032 if (buf == NULL) { 5033 return (TRAN_BUSY); 5034 } 5035 } else { 5036 buf = kmem_zalloc(1024, KM_SLEEP); 5037 } 5038 5039 mutex_enter(cport_mutex); 5040 5041 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 5042 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5043 mutex_exit(cport_mutex); 5044 kmem_free(buf, 1024); 5045 return (rval); 5046 } 5047 5048 scsipkt->pkt_reason = CMD_CMPLT; 5049 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5050 STATE_SENT_CMD | STATE_GOT_STATUS; 5051 5052 pc = scsipkt->pkt_cdbp[2] >> 6; 5053 5054 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 5055 /* 5056 * Because it is fully emulated command storing data 5057 * programatically in the specified buffer, release 5058 * preallocated DMA resources before storing data in the buffer, 5059 * so no unwanted DMA sync would take place. 5060 */ 5061 sata_scsi_dmafree(NULL, scsipkt); 5062 5063 len = 0; 5064 bdlen = 0; 5065 if (!(scsipkt->pkt_cdbp[1] & 8)) { 5066 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 && 5067 (scsipkt->pkt_cdbp[1] & LLBAA)) 5068 bdlen = 16; 5069 else 5070 bdlen = 8; 5071 } 5072 /* Build mode parameter header */ 5073 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 5074 /* 4-byte mode parameter header */ 5075 buf[len++] = 0; /* mode data length */ 5076 buf[len++] = 0; /* medium type */ 5077 buf[len++] = 0; /* dev-specific param */ 5078 buf[len++] = bdlen; /* Block Descriptor length */ 5079 } else { 5080 /* 8-byte mode parameter header */ 5081 buf[len++] = 0; /* mode data length */ 5082 buf[len++] = 0; 5083 buf[len++] = 0; /* medium type */ 5084 buf[len++] = 0; /* dev-specific param */ 5085 if (bdlen == 16) 5086 buf[len++] = 1; /* long lba descriptor */ 5087 else 5088 buf[len++] = 0; 5089 buf[len++] = 0; 5090 buf[len++] = 0; /* Block Descriptor length */ 5091 buf[len++] = bdlen; 5092 } 5093 5094 sdinfo = sata_get_device_info( 5095 spx->txlt_sata_hba_inst, 5096 &spx->txlt_sata_pkt->satapkt_device); 5097 5098 /* Build block descriptor only if not disabled (DBD) */ 5099 if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) { 5100 /* Block descriptor - direct-access device format */ 5101 if (bdlen == 8) { 5102 /* build regular block descriptor */ 5103 buf[len++] = 5104 (sdinfo->satadrv_capacity >> 24) & 0xff; 5105 buf[len++] = 5106 (sdinfo->satadrv_capacity >> 16) & 0xff; 5107 buf[len++] = 5108 (sdinfo->satadrv_capacity >> 8) & 0xff; 5109 buf[len++] = sdinfo->satadrv_capacity & 0xff; 5110 buf[len++] = 0; /* density code */ 5111 buf[len++] = 0; 5112 if (sdinfo->satadrv_type == 5113 SATA_DTYPE_ATADISK) 5114 buf[len++] = 2; 5115 else 5116 /* ATAPI */ 5117 buf[len++] = 8; 5118 buf[len++] = 0; 5119 } else if (bdlen == 16) { 5120 /* Long LBA Accepted */ 5121 /* build long lba block descriptor */ 5122 #ifndef __lock_lint 5123 buf[len++] = 5124 (sdinfo->satadrv_capacity >> 56) & 0xff; 5125 buf[len++] = 5126 (sdinfo->satadrv_capacity >> 48) & 0xff; 5127 buf[len++] = 5128 (sdinfo->satadrv_capacity >> 40) & 0xff; 5129 buf[len++] = 5130 (sdinfo->satadrv_capacity >> 32) & 0xff; 5131 #endif 5132 buf[len++] = 5133 (sdinfo->satadrv_capacity >> 24) & 0xff; 5134 buf[len++] = 5135 (sdinfo->satadrv_capacity >> 16) & 0xff; 5136 buf[len++] = 5137 (sdinfo->satadrv_capacity >> 8) & 0xff; 5138 buf[len++] = sdinfo->satadrv_capacity & 0xff; 5139 buf[len++] = 0; 5140 buf[len++] = 0; /* density code */ 5141 buf[len++] = 0; 5142 buf[len++] = 0; 5143 if (sdinfo->satadrv_type == 5144 SATA_DTYPE_ATADISK) 5145 buf[len++] = 2; 5146 else 5147 /* ATAPI */ 5148 buf[len++] = 8; 5149 buf[len++] = 0; 5150 } 5151 } 5152 5153 sata_id = &sdinfo->satadrv_id; 5154 5155 /* 5156 * Add requested pages. 5157 * Page 3 and 4 are obsolete and we are not supporting them. 5158 * We deal now with: 5159 * caching (read/write cache control). 5160 * We should eventually deal with following mode pages: 5161 * error recovery (0x01), 5162 * power condition (0x1a), 5163 * exception control page (enables SMART) (0x1c), 5164 * enclosure management (ses), 5165 * protocol-specific port mode (port control). 5166 */ 5167 switch (scsipkt->pkt_cdbp[2] & 0x3f) { 5168 case MODEPAGE_RW_ERRRECOV: 5169 /* DAD_MODE_ERR_RECOV */ 5170 /* R/W recovery */ 5171 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 5172 break; 5173 case MODEPAGE_CACHING: 5174 /* DAD_MODE_CACHE */ 5175 /* Reject not supported request for saved parameters */ 5176 if (pc == 3) { 5177 *scsipkt->pkt_scbp = STATUS_CHECK; 5178 sense = sata_arq_sense(spx); 5179 sense->es_key = KEY_ILLEGAL_REQUEST; 5180 sense->es_add_code = 5181 SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED; 5182 goto done; 5183 } 5184 5185 /* caching */ 5186 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 5187 break; 5188 case MODEPAGE_INFO_EXCPT: 5189 /* exception cntrl */ 5190 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 5191 len += sata_build_msense_page_1c(sdinfo, pc, 5192 buf+len); 5193 } 5194 else 5195 goto err; 5196 break; 5197 case MODEPAGE_POWER_COND: 5198 /* DAD_MODE_POWER_COND */ 5199 /* power condition */ 5200 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 5201 break; 5202 5203 case MODEPAGE_ACOUSTIC_MANAG: 5204 /* acoustic management */ 5205 len += sata_build_msense_page_30(sdinfo, pc, buf+len); 5206 break; 5207 case MODEPAGE_ALLPAGES: 5208 /* all pages */ 5209 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 5210 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 5211 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 5212 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 5213 len += sata_build_msense_page_1c(sdinfo, pc, 5214 buf+len); 5215 } 5216 len += sata_build_msense_page_30(sdinfo, pc, buf+len); 5217 break; 5218 default: 5219 err: 5220 /* Invalid request */ 5221 *scsipkt->pkt_scbp = STATUS_CHECK; 5222 sense = sata_arq_sense(spx); 5223 sense->es_key = KEY_ILLEGAL_REQUEST; 5224 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5225 goto done; 5226 } 5227 5228 /* fix total mode data length */ 5229 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 5230 /* 4-byte mode parameter header */ 5231 buf[0] = len - 1; /* mode data length */ 5232 } else { 5233 buf[0] = (len -2) >> 8; 5234 buf[1] = (len -2) & 0xff; 5235 } 5236 5237 5238 /* Check allocation length */ 5239 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 5240 alc_len = scsipkt->pkt_cdbp[4]; 5241 } else { 5242 alc_len = scsipkt->pkt_cdbp[7]; 5243 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 5244 } 5245 /* 5246 * We do not check for possible parameters truncation 5247 * (alc_len < len) assuming that the target driver works 5248 * correctly. Just avoiding overrun. 5249 * Copy no more than requested and possible, buffer-wise. 5250 */ 5251 count = MIN(alc_len, len); 5252 count = MIN(bp->b_bcount, count); 5253 bcopy(buf, bp->b_un.b_addr, count); 5254 5255 scsipkt->pkt_state |= STATE_XFERRED_DATA; 5256 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 5257 } 5258 *scsipkt->pkt_scbp = STATUS_GOOD; 5259 done: 5260 mutex_exit(cport_mutex); 5261 (void) kmem_free(buf, 1024); 5262 5263 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5264 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5265 5266 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5267 scsipkt->pkt_comp != NULL) { 5268 /* scsi callback required */ 5269 if (servicing_interrupt()) { 5270 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5271 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 5272 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 5273 TASKQID_INVALID) { 5274 return (TRAN_BUSY); 5275 } 5276 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5277 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 5278 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 5279 /* Scheduling the callback failed */ 5280 return (TRAN_BUSY); 5281 } 5282 } 5283 5284 return (TRAN_ACCEPT); 5285 } 5286 5287 5288 /* 5289 * SATA translate command: Mode Select. 5290 * Translated into appropriate SATA command or emulated. 5291 * Saving parameters is not supported. 5292 * Changing device capacity is not supported (although theoretically 5293 * possible by executing SET FEATURES/SET MAX ADDRESS) 5294 * 5295 * Assumption is that the target driver is working correctly. 5296 * 5297 * More than one SATA command may be executed to perform operations specified 5298 * by mode select pages. The first error terminates further execution. 5299 * Operations performed successully are not backed-up in such case. 5300 * 5301 * NOTE: Implemented pages: 5302 * - caching page 5303 * - informational exception page 5304 * - acoustic management page 5305 * - power condition page 5306 * Caching setup is remembered so it could be re-stored in case of 5307 * an unexpected device reset. 5308 * 5309 * Returns TRAN_XXXX. 5310 * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields. 5311 */ 5312 5313 static int 5314 sata_txlt_mode_select(sata_pkt_txlate_t *spx) 5315 { 5316 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5317 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5318 struct scsi_extended_sense *sense; 5319 int len, pagelen, count, pllen; 5320 uint8_t *buf; /* mode select buffer */ 5321 int rval, stat, reason; 5322 uint_t nointr_flag; 5323 int dmod = 0; 5324 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 5325 5326 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5327 "sata_txlt_mode_select, pc %x page code 0x%02x\n", 5328 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 5329 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 5330 5331 mutex_enter(cport_mutex); 5332 5333 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 5334 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5335 mutex_exit(cport_mutex); 5336 return (rval); 5337 } 5338 5339 rval = TRAN_ACCEPT; 5340 5341 scsipkt->pkt_reason = CMD_CMPLT; 5342 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5343 STATE_SENT_CMD | STATE_GOT_STATUS; 5344 nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR; 5345 5346 /* Reject not supported request */ 5347 if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */ 5348 *scsipkt->pkt_scbp = STATUS_CHECK; 5349 sense = sata_arq_sense(spx); 5350 sense->es_key = KEY_ILLEGAL_REQUEST; 5351 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5352 goto done; 5353 } 5354 5355 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 5356 pllen = scsipkt->pkt_cdbp[4]; 5357 } else { 5358 pllen = scsipkt->pkt_cdbp[7]; 5359 pllen = (pllen << 8) | scsipkt->pkt_cdbp[7]; 5360 } 5361 5362 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 5363 5364 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) { 5365 buf = (uint8_t *)bp->b_un.b_addr; 5366 count = MIN(bp->b_bcount, pllen); 5367 scsipkt->pkt_state |= STATE_XFERRED_DATA; 5368 scsipkt->pkt_resid = 0; 5369 pllen = count; 5370 5371 /* 5372 * Check the header to skip the block descriptor(s) - we 5373 * do not support setting device capacity. 5374 * Existing macros do not recognize long LBA dscriptor, 5375 * hence manual calculation. 5376 */ 5377 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 5378 /* 6-bytes CMD, 4 bytes header */ 5379 if (count <= 4) 5380 goto done; /* header only */ 5381 len = buf[3] + 4; 5382 } else { 5383 /* 10-bytes CMD, 8 bytes header */ 5384 if (count <= 8) 5385 goto done; /* header only */ 5386 len = buf[6]; 5387 len = (len << 8) + buf[7] + 8; 5388 } 5389 if (len >= count) 5390 goto done; /* header + descriptor(s) only */ 5391 5392 pllen -= len; /* remaining data length */ 5393 5394 /* 5395 * We may be executing SATA command and want to execute it 5396 * in SYNCH mode, regardless of scsi_pkt setting. 5397 * Save scsi_pkt setting and indicate SYNCH mode 5398 */ 5399 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5400 scsipkt->pkt_comp != NULL) { 5401 scsipkt->pkt_flags |= FLAG_NOINTR; 5402 } 5403 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH; 5404 5405 /* 5406 * len is now the offset to a first mode select page 5407 * Process all pages 5408 */ 5409 while (pllen > 0) { 5410 switch ((int)buf[len]) { 5411 case MODEPAGE_CACHING: 5412 /* No support for SP (saving) */ 5413 if (scsipkt->pkt_cdbp[1] & 0x01) { 5414 *scsipkt->pkt_scbp = STATUS_CHECK; 5415 sense = sata_arq_sense(spx); 5416 sense->es_key = KEY_ILLEGAL_REQUEST; 5417 sense->es_add_code = 5418 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5419 goto done; 5420 } 5421 stat = sata_mode_select_page_8(spx, 5422 (struct mode_cache_scsi3 *)&buf[len], 5423 pllen, &pagelen, &rval, &dmod); 5424 /* 5425 * The pagelen value indicates the number of 5426 * parameter bytes already processed. 5427 * The rval is the return value from 5428 * sata_tran_start(). 5429 * The stat indicates the overall status of 5430 * the operation(s). 5431 */ 5432 if (stat != SATA_SUCCESS) 5433 /* 5434 * Page processing did not succeed - 5435 * all error info is already set-up, 5436 * just return 5437 */ 5438 pllen = 0; /* this breaks the loop */ 5439 else { 5440 len += pagelen; 5441 pllen -= pagelen; 5442 } 5443 break; 5444 5445 case MODEPAGE_INFO_EXCPT: 5446 stat = sata_mode_select_page_1c(spx, 5447 (struct mode_info_excpt_page *)&buf[len], 5448 pllen, &pagelen, &rval, &dmod); 5449 /* 5450 * The pagelen value indicates the number of 5451 * parameter bytes already processed. 5452 * The rval is the return value from 5453 * sata_tran_start(). 5454 * The stat indicates the overall status of 5455 * the operation(s). 5456 */ 5457 if (stat != SATA_SUCCESS) 5458 /* 5459 * Page processing did not succeed - 5460 * all error info is already set-up, 5461 * just return 5462 */ 5463 pllen = 0; /* this breaks the loop */ 5464 else { 5465 len += pagelen; 5466 pllen -= pagelen; 5467 } 5468 break; 5469 5470 case MODEPAGE_ACOUSTIC_MANAG: 5471 stat = sata_mode_select_page_30(spx, 5472 (struct mode_acoustic_management *) 5473 &buf[len], pllen, &pagelen, &rval, &dmod); 5474 /* 5475 * The pagelen value indicates the number of 5476 * parameter bytes already processed. 5477 * The rval is the return value from 5478 * sata_tran_start(). 5479 * The stat indicates the overall status of 5480 * the operation(s). 5481 */ 5482 if (stat != SATA_SUCCESS) 5483 /* 5484 * Page processing did not succeed - 5485 * all error info is already set-up, 5486 * just return 5487 */ 5488 pllen = 0; /* this breaks the loop */ 5489 else { 5490 len += pagelen; 5491 pllen -= pagelen; 5492 } 5493 5494 break; 5495 case MODEPAGE_POWER_COND: 5496 stat = sata_mode_select_page_1a(spx, 5497 (struct mode_info_power_cond *)&buf[len], 5498 pllen, &pagelen, &rval, &dmod); 5499 /* 5500 * The pagelen value indicates the number of 5501 * parameter bytes already processed. 5502 * The rval is the return value from 5503 * sata_tran_start(). 5504 * The stat indicates the overall status of 5505 * the operation(s). 5506 */ 5507 if (stat != SATA_SUCCESS) 5508 /* 5509 * Page processing did not succeed - 5510 * all error info is already set-up, 5511 * just return 5512 */ 5513 pllen = 0; /* this breaks the loop */ 5514 else { 5515 len += pagelen; 5516 pllen -= pagelen; 5517 } 5518 break; 5519 default: 5520 *scsipkt->pkt_scbp = STATUS_CHECK; 5521 sense = sata_arq_sense(spx); 5522 sense->es_key = KEY_ILLEGAL_REQUEST; 5523 sense->es_add_code = 5524 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 5525 goto done; 5526 } 5527 } 5528 } 5529 done: 5530 mutex_exit(cport_mutex); 5531 /* 5532 * If device parameters were modified, fetch and store the new 5533 * Identify Device data. Since port mutex could have been released 5534 * for accessing HBA driver, we need to re-check device existence. 5535 */ 5536 if (dmod != 0) { 5537 sata_drive_info_t new_sdinfo, *sdinfo; 5538 int rv = 0; 5539 5540 /* 5541 * Following statement has to be changed if this function is 5542 * used for devices other than SATA hard disks. 5543 */ 5544 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK; 5545 5546 new_sdinfo.satadrv_addr = 5547 spx->txlt_sata_pkt->satapkt_device.satadev_addr; 5548 rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst, 5549 &new_sdinfo); 5550 5551 mutex_enter(cport_mutex); 5552 /* 5553 * Since port mutex could have been released when 5554 * accessing HBA driver, we need to re-check that the 5555 * framework still holds the device info structure. 5556 */ 5557 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 5558 &spx->txlt_sata_pkt->satapkt_device); 5559 if (sdinfo != NULL) { 5560 /* 5561 * Device still has info structure in the 5562 * sata framework. Copy newly fetched info 5563 */ 5564 if (rv == 0) { 5565 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 5566 sata_save_drive_settings(sdinfo); 5567 } else { 5568 /* 5569 * Could not fetch new data - invalidate 5570 * sata_drive_info. That makes device 5571 * unusable. 5572 */ 5573 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 5574 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 5575 } 5576 } 5577 if (rv != 0 || sdinfo == NULL) { 5578 /* 5579 * This changes the overall mode select completion 5580 * reason to a failed one !!!!! 5581 */ 5582 *scsipkt->pkt_scbp = STATUS_CHECK; 5583 sense = sata_arq_sense(spx); 5584 scsipkt->pkt_reason = CMD_INCOMPLETE; 5585 rval = TRAN_ACCEPT; 5586 } 5587 mutex_exit(cport_mutex); 5588 } 5589 /* Restore the scsi pkt flags */ 5590 scsipkt->pkt_flags &= ~FLAG_NOINTR; 5591 scsipkt->pkt_flags |= nointr_flag; 5592 5593 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5594 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5595 5596 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5597 scsipkt->pkt_comp != NULL) { 5598 /* scsi callback required */ 5599 if (servicing_interrupt()) { 5600 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5601 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 5602 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 5603 TASKQID_INVALID) { 5604 return (TRAN_BUSY); 5605 } 5606 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5607 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 5608 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 5609 /* Scheduling the callback failed */ 5610 return (TRAN_BUSY); 5611 } 5612 } 5613 5614 return (rval); 5615 } 5616 5617 /* 5618 * Translate command: ATA Pass Through 5619 * Incomplete implementation. Only supports No-Data, PIO Data-In, and 5620 * PIO Data-Out protocols. Also supports CK_COND bit. 5621 * 5622 * Mapping of the incoming CDB bytes to the outgoing satacmd bytes is 5623 * described in Table 111 of SAT-2 (Draft 9). 5624 */ 5625 static int 5626 sata_txlt_ata_pass_thru(sata_pkt_txlate_t *spx) 5627 { 5628 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5629 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5630 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5631 int extend; 5632 uint64_t lba; 5633 uint16_t feature, sec_count; 5634 int t_len, synch; 5635 int rval, reason; 5636 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 5637 5638 mutex_enter(cport_mutex); 5639 5640 rval = sata_txlt_generic_pkt_info(spx, &reason, 1); 5641 if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5642 mutex_exit(cport_mutex); 5643 return (rval); 5644 } 5645 5646 /* T_DIR bit */ 5647 if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_T_DIR) 5648 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 5649 else 5650 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 5651 5652 /* MULTIPLE_COUNT field. If non-zero, invalid command (for now). */ 5653 if (((scsipkt->pkt_cdbp[1] >> 5) & 0x7) != 0) { 5654 mutex_exit(cport_mutex); 5655 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5656 } 5657 5658 /* OFFLINE field. If non-zero, invalid command (for now). */ 5659 if (((scsipkt->pkt_cdbp[2] >> 6) & 0x3) != 0) { 5660 mutex_exit(cport_mutex); 5661 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5662 } 5663 5664 /* PROTOCOL field */ 5665 switch ((scsipkt->pkt_cdbp[1] >> 1) & 0xf) { 5666 case SATL_APT_P_HW_RESET: 5667 case SATL_APT_P_SRST: 5668 case SATL_APT_P_DMA: 5669 case SATL_APT_P_DMA_QUEUED: 5670 case SATL_APT_P_DEV_DIAG: 5671 case SATL_APT_P_DEV_RESET: 5672 case SATL_APT_P_UDMA_IN: 5673 case SATL_APT_P_UDMA_OUT: 5674 case SATL_APT_P_FPDMA: 5675 case SATL_APT_P_RET_RESP: 5676 /* Not yet implemented */ 5677 default: 5678 mutex_exit(cport_mutex); 5679 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5680 5681 case SATL_APT_P_NON_DATA: 5682 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 5683 break; 5684 5685 case SATL_APT_P_PIO_DATA_IN: 5686 /* If PROTOCOL disagrees with T_DIR, invalid command */ 5687 if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_WRITE) { 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 in? */ 5697 mutex_exit(cport_mutex); 5698 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5699 } 5700 5701 break; 5702 5703 case SATL_APT_P_PIO_DATA_OUT: 5704 /* If PROTOCOL disagrees with T_DIR, invalid command */ 5705 if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_READ) { 5706 mutex_exit(cport_mutex); 5707 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5708 } 5709 5710 /* if there is a buffer, release its DMA resources */ 5711 if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) { 5712 sata_scsi_dmafree(NULL, scsipkt); 5713 } else { 5714 /* if there is no buffer, how do you PIO out? */ 5715 mutex_exit(cport_mutex); 5716 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5717 } 5718 5719 break; 5720 } 5721 5722 /* Parse the ATA cmd fields, transfer some straight to the satacmd */ 5723 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 5724 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12: 5725 feature = scsipkt->pkt_cdbp[3]; 5726 5727 sec_count = scsipkt->pkt_cdbp[4]; 5728 5729 lba = scsipkt->pkt_cdbp[8] & 0xf; 5730 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 5731 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 5732 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 5733 5734 scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] & 0xf0; 5735 scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[9]; 5736 5737 break; 5738 5739 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16: 5740 if (scsipkt->pkt_cdbp[1] & SATL_APT_BM_EXTEND) { 5741 extend = 1; 5742 5743 feature = scsipkt->pkt_cdbp[3]; 5744 feature = (feature << 8) | scsipkt->pkt_cdbp[4]; 5745 5746 sec_count = scsipkt->pkt_cdbp[5]; 5747 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[6]; 5748 5749 lba = scsipkt->pkt_cdbp[11]; 5750 lba = (lba << 8) | scsipkt->pkt_cdbp[12]; 5751 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 5752 lba = (lba << 8) | scsipkt->pkt_cdbp[10]; 5753 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 5754 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 5755 5756 scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13]; 5757 scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14]; 5758 } else { 5759 feature = scsipkt->pkt_cdbp[3]; 5760 5761 sec_count = scsipkt->pkt_cdbp[5]; 5762 5763 lba = scsipkt->pkt_cdbp[13] & 0xf; 5764 lba = (lba << 8) | scsipkt->pkt_cdbp[12]; 5765 lba = (lba << 8) | scsipkt->pkt_cdbp[10]; 5766 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 5767 5768 scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] & 5769 0xf0; 5770 scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14]; 5771 } 5772 5773 break; 5774 } 5775 5776 /* CK_COND bit */ 5777 if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) { 5778 if (extend) { 5779 scmd->satacmd_flags.sata_copy_out_sec_count_msb = 1; 5780 scmd->satacmd_flags.sata_copy_out_lba_low_msb = 1; 5781 scmd->satacmd_flags.sata_copy_out_lba_mid_msb = 1; 5782 scmd->satacmd_flags.sata_copy_out_lba_high_msb = 1; 5783 } 5784 5785 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1; 5786 scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1; 5787 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1; 5788 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1; 5789 scmd->satacmd_flags.sata_copy_out_device_reg = 1; 5790 scmd->satacmd_flags.sata_copy_out_error_reg = 1; 5791 } 5792 5793 /* Transfer remaining parsed ATA cmd values to the satacmd */ 5794 if (extend) { 5795 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 5796 5797 scmd->satacmd_features_reg_ext = (feature >> 8) & 0xff; 5798 scmd->satacmd_sec_count_msb = (sec_count >> 8) & 0xff; 5799 scmd->satacmd_lba_low_msb = (lba >> 8) & 0xff; 5800 scmd->satacmd_lba_mid_msb = (lba >> 8) & 0xff; 5801 scmd->satacmd_lba_high_msb = lba >> 40; 5802 } else { 5803 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 5804 5805 scmd->satacmd_features_reg_ext = 0; 5806 scmd->satacmd_sec_count_msb = 0; 5807 scmd->satacmd_lba_low_msb = 0; 5808 scmd->satacmd_lba_mid_msb = 0; 5809 scmd->satacmd_lba_high_msb = 0; 5810 } 5811 5812 scmd->satacmd_features_reg = feature & 0xff; 5813 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 5814 scmd->satacmd_lba_low_lsb = lba & 0xff; 5815 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 5816 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 5817 5818 /* Determine transfer length */ 5819 switch (scsipkt->pkt_cdbp[2] & 0x3) { /* T_LENGTH field */ 5820 case 1: 5821 t_len = feature; 5822 break; 5823 case 2: 5824 t_len = sec_count; 5825 break; 5826 default: 5827 t_len = 0; 5828 break; 5829 } 5830 5831 /* Adjust transfer length for the Byte Block bit */ 5832 if ((scsipkt->pkt_cdbp[2] >> 2) & 1) 5833 t_len *= SATA_DISK_SECTOR_SIZE; 5834 5835 /* Start processing command */ 5836 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5837 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_apt_completion; 5838 synch = FALSE; 5839 } else { 5840 synch = TRUE; 5841 } 5842 5843 if (sata_hba_start(spx, &rval) != 0) { 5844 mutex_exit(cport_mutex); 5845 return (rval); 5846 } 5847 5848 mutex_exit(cport_mutex); 5849 5850 if (synch) { 5851 sata_txlt_apt_completion(spx->txlt_sata_pkt); 5852 } 5853 5854 return (TRAN_ACCEPT); 5855 } 5856 5857 /* 5858 * Translate command: Log Sense 5859 */ 5860 static int 5861 sata_txlt_log_sense(sata_pkt_txlate_t *spx) 5862 { 5863 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5864 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5865 sata_drive_info_t *sdinfo; 5866 struct scsi_extended_sense *sense; 5867 int len, count, alc_len; 5868 int pc; /* Page Control code */ 5869 int page_code; /* Page code */ 5870 uint8_t *buf; /* log sense buffer */ 5871 int rval, reason; 5872 #define MAX_LOG_SENSE_PAGE_SIZE 512 5873 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 5874 5875 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5876 "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n", 5877 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 5878 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 5879 5880 if (servicing_interrupt()) { 5881 buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_NOSLEEP); 5882 if (buf == NULL) { 5883 return (TRAN_BUSY); 5884 } 5885 } else { 5886 buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP); 5887 } 5888 5889 mutex_enter(cport_mutex); 5890 5891 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 5892 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5893 mutex_exit(cport_mutex); 5894 kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 5895 return (rval); 5896 } 5897 5898 scsipkt->pkt_reason = CMD_CMPLT; 5899 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5900 STATE_SENT_CMD | STATE_GOT_STATUS; 5901 5902 pc = scsipkt->pkt_cdbp[2] >> 6; 5903 page_code = scsipkt->pkt_cdbp[2] & 0x3f; 5904 5905 /* Reject not supported request for all but cumulative values */ 5906 switch (pc) { 5907 case PC_CUMULATIVE_VALUES: 5908 break; 5909 default: 5910 *scsipkt->pkt_scbp = STATUS_CHECK; 5911 sense = sata_arq_sense(spx); 5912 sense->es_key = KEY_ILLEGAL_REQUEST; 5913 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5914 goto done; 5915 } 5916 5917 switch (page_code) { 5918 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 5919 case PAGE_CODE_SELF_TEST_RESULTS: 5920 case PAGE_CODE_INFORMATION_EXCEPTIONS: 5921 case PAGE_CODE_SMART_READ_DATA: 5922 case PAGE_CODE_START_STOP_CYCLE_COUNTER: 5923 break; 5924 default: 5925 *scsipkt->pkt_scbp = STATUS_CHECK; 5926 sense = sata_arq_sense(spx); 5927 sense->es_key = KEY_ILLEGAL_REQUEST; 5928 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5929 goto done; 5930 } 5931 5932 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 5933 /* 5934 * Because log sense uses local buffers for data retrieval from 5935 * the devices and sets the data programatically in the 5936 * original specified buffer, release preallocated DMA 5937 * resources before storing data in the original buffer, 5938 * so no unwanted DMA sync would take place. 5939 */ 5940 sata_id_t *sata_id; 5941 5942 sata_scsi_dmafree(NULL, scsipkt); 5943 5944 len = 0; 5945 5946 /* Build log parameter header */ 5947 buf[len++] = page_code; /* page code as in the CDB */ 5948 buf[len++] = 0; /* reserved */ 5949 buf[len++] = 0; /* Zero out page length for now (MSB) */ 5950 buf[len++] = 0; /* (LSB) */ 5951 5952 sdinfo = sata_get_device_info( 5953 spx->txlt_sata_hba_inst, 5954 &spx->txlt_sata_pkt->satapkt_device); 5955 5956 /* 5957 * Add requested pages. 5958 */ 5959 switch (page_code) { 5960 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 5961 len = sata_build_lsense_page_0(sdinfo, buf + len); 5962 break; 5963 case PAGE_CODE_SELF_TEST_RESULTS: 5964 sata_id = &sdinfo->satadrv_id; 5965 if ((! (sata_id->ai_cmdset84 & 5966 SATA_SMART_SELF_TEST_SUPPORTED)) || 5967 (! (sata_id->ai_features87 & 5968 SATA_SMART_SELF_TEST_SUPPORTED))) { 5969 *scsipkt->pkt_scbp = STATUS_CHECK; 5970 sense = sata_arq_sense(spx); 5971 sense->es_key = KEY_ILLEGAL_REQUEST; 5972 sense->es_add_code = 5973 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5974 5975 goto done; 5976 } 5977 len = sata_build_lsense_page_10(sdinfo, buf + len, 5978 spx->txlt_sata_hba_inst); 5979 break; 5980 case PAGE_CODE_INFORMATION_EXCEPTIONS: 5981 sata_id = &sdinfo->satadrv_id; 5982 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 5983 *scsipkt->pkt_scbp = STATUS_CHECK; 5984 sense = sata_arq_sense(spx); 5985 sense->es_key = KEY_ILLEGAL_REQUEST; 5986 sense->es_add_code = 5987 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5988 5989 goto done; 5990 } 5991 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 5992 *scsipkt->pkt_scbp = STATUS_CHECK; 5993 sense = sata_arq_sense(spx); 5994 sense->es_key = KEY_ABORTED_COMMAND; 5995 sense->es_add_code = 5996 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 5997 sense->es_qual_code = 5998 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 5999 6000 goto done; 6001 } 6002 6003 len = sata_build_lsense_page_2f(sdinfo, buf + len, 6004 spx->txlt_sata_hba_inst); 6005 break; 6006 case PAGE_CODE_SMART_READ_DATA: 6007 sata_id = &sdinfo->satadrv_id; 6008 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 6009 *scsipkt->pkt_scbp = STATUS_CHECK; 6010 sense = sata_arq_sense(spx); 6011 sense->es_key = KEY_ILLEGAL_REQUEST; 6012 sense->es_add_code = 6013 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 6014 6015 goto done; 6016 } 6017 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 6018 *scsipkt->pkt_scbp = STATUS_CHECK; 6019 sense = sata_arq_sense(spx); 6020 sense->es_key = KEY_ABORTED_COMMAND; 6021 sense->es_add_code = 6022 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 6023 sense->es_qual_code = 6024 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 6025 6026 goto done; 6027 } 6028 6029 /* This page doesn't include a page header */ 6030 len = sata_build_lsense_page_30(sdinfo, buf, 6031 spx->txlt_sata_hba_inst); 6032 goto no_header; 6033 case PAGE_CODE_START_STOP_CYCLE_COUNTER: 6034 sata_id = &sdinfo->satadrv_id; 6035 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 6036 *scsipkt->pkt_scbp = STATUS_CHECK; 6037 sense = sata_arq_sense(spx); 6038 sense->es_key = KEY_ILLEGAL_REQUEST; 6039 sense->es_add_code = 6040 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 6041 6042 goto done; 6043 } 6044 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 6045 *scsipkt->pkt_scbp = STATUS_CHECK; 6046 sense = sata_arq_sense(spx); 6047 sense->es_key = KEY_ABORTED_COMMAND; 6048 sense->es_add_code = 6049 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 6050 sense->es_qual_code = 6051 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 6052 6053 goto done; 6054 } 6055 len = sata_build_lsense_page_0e(sdinfo, buf, spx); 6056 goto no_header; 6057 default: 6058 /* Invalid request */ 6059 *scsipkt->pkt_scbp = STATUS_CHECK; 6060 sense = sata_arq_sense(spx); 6061 sense->es_key = KEY_ILLEGAL_REQUEST; 6062 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 6063 goto done; 6064 } 6065 6066 /* set parameter log sense data length */ 6067 buf[2] = len >> 8; /* log sense length (MSB) */ 6068 buf[3] = len & 0xff; /* log sense length (LSB) */ 6069 6070 len += SCSI_LOG_PAGE_HDR_LEN; 6071 ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE); 6072 6073 no_header: 6074 /* Check allocation length */ 6075 alc_len = scsipkt->pkt_cdbp[7]; 6076 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 6077 6078 /* 6079 * We do not check for possible parameters truncation 6080 * (alc_len < len) assuming that the target driver works 6081 * correctly. Just avoiding overrun. 6082 * Copy no more than requested and possible, buffer-wise. 6083 */ 6084 count = MIN(alc_len, len); 6085 count = MIN(bp->b_bcount, count); 6086 bcopy(buf, bp->b_un.b_addr, count); 6087 6088 scsipkt->pkt_state |= STATE_XFERRED_DATA; 6089 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 6090 } 6091 *scsipkt->pkt_scbp = STATUS_GOOD; 6092 done: 6093 mutex_exit(cport_mutex); 6094 (void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 6095 6096 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6097 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6098 6099 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6100 scsipkt->pkt_comp != NULL) { 6101 /* scsi callback required */ 6102 if (servicing_interrupt()) { 6103 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6104 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 6105 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 6106 TASKQID_INVALID) { 6107 return (TRAN_BUSY); 6108 } 6109 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6110 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 6111 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 6112 /* Scheduling the callback failed */ 6113 return (TRAN_BUSY); 6114 } 6115 } 6116 6117 return (TRAN_ACCEPT); 6118 } 6119 6120 /* 6121 * Translate command: Log Select 6122 * Not implemented at this time - returns invalid command response. 6123 */ 6124 static int 6125 sata_txlt_log_select(sata_pkt_txlate_t *spx) 6126 { 6127 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6128 "sata_txlt_log_select\n", NULL); 6129 6130 return (sata_txlt_invalid_command(spx)); 6131 } 6132 6133 6134 /* 6135 * Translate command: Read (various types). 6136 * Translated into appropriate type of ATA READ command 6137 * for SATA hard disks. 6138 * Both the device capabilities and requested operation mode are 6139 * considered. 6140 * 6141 * Following scsi cdb fields are ignored: 6142 * rdprotect, dpo, fua, fua_nv, group_number. 6143 * 6144 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 6145 * enable variable sata_func_enable), the capability of the controller and 6146 * capability of a device are checked and if both support queueing, read 6147 * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT 6148 * command rather than plain READ_XXX command. 6149 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 6150 * both the controller and device suport such functionality, the read 6151 * request will be translated to READ_FPDMA_QUEUED command. 6152 * In both cases the maximum queue depth is derived as minimum of: 6153 * HBA capability,device capability and sata_max_queue_depth variable setting. 6154 * The value passed to HBA driver is decremented by 1, because only 5 bits are 6155 * used to pass max queue depth value, and the maximum possible queue depth 6156 * is 32. 6157 * 6158 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 6159 * appropriate values in scsi_pkt fields. 6160 */ 6161 static int 6162 sata_txlt_read(sata_pkt_txlate_t *spx) 6163 { 6164 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6165 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6166 sata_drive_info_t *sdinfo; 6167 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 6168 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 6169 uint16_t sec_count; 6170 uint64_t lba; 6171 int rval, reason; 6172 int synch; 6173 6174 mutex_enter(cport_mutex); 6175 6176 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 6177 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 6178 mutex_exit(cport_mutex); 6179 return (rval); 6180 } 6181 6182 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 6183 &spx->txlt_sata_pkt->satapkt_device); 6184 6185 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 6186 /* 6187 * Extract LBA and sector count from scsi CDB. 6188 */ 6189 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 6190 case SCMD_READ: 6191 /* 6-byte scsi read cmd : 0x08 */ 6192 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 6193 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 6194 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6195 sec_count = scsipkt->pkt_cdbp[4]; 6196 /* sec_count 0 will be interpreted as 256 by a device */ 6197 break; 6198 case SCMD_READ_G1: 6199 /* 10-bytes scsi read command : 0x28 */ 6200 lba = scsipkt->pkt_cdbp[2]; 6201 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6202 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 6203 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 6204 sec_count = scsipkt->pkt_cdbp[7]; 6205 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 6206 break; 6207 case SCMD_READ_G5: 6208 /* 12-bytes scsi read command : 0xA8 */ 6209 lba = scsipkt->pkt_cdbp[2]; 6210 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6211 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 6212 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 6213 sec_count = scsipkt->pkt_cdbp[6]; 6214 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 6215 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 6216 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 6217 break; 6218 case SCMD_READ_G4: 6219 /* 16-bytes scsi read command : 0x88 */ 6220 lba = scsipkt->pkt_cdbp[2]; 6221 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6222 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 6223 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 6224 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 6225 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 6226 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 6227 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 6228 sec_count = scsipkt->pkt_cdbp[10]; 6229 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 6230 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 6231 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 6232 break; 6233 default: 6234 /* Unsupported command */ 6235 mutex_exit(cport_mutex); 6236 return (sata_txlt_invalid_command(spx)); 6237 } 6238 6239 /* 6240 * Check if specified address exceeds device capacity 6241 */ 6242 if ((lba >= sdinfo->satadrv_capacity) || 6243 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 6244 /* LBA out of range */ 6245 mutex_exit(cport_mutex); 6246 return (sata_txlt_lba_out_of_range(spx)); 6247 } 6248 6249 /* 6250 * For zero-length transfer, emulate good completion of the command 6251 * (reasons for rejecting the command were already checked). 6252 * No DMA resources were allocated. 6253 */ 6254 if (spx->txlt_dma_cookie_list == NULL) { 6255 mutex_exit(cport_mutex); 6256 return (sata_emul_rw_completion(spx)); 6257 } 6258 6259 /* 6260 * Build cmd block depending on the device capability and 6261 * requested operation mode. 6262 * Do not bother with non-dma mode - we are working only with 6263 * devices supporting DMA. 6264 */ 6265 scmd->satacmd_addr_type = ATA_ADDR_LBA; 6266 scmd->satacmd_device_reg = SATA_ADH_LBA; 6267 scmd->satacmd_cmd_reg = SATAC_READ_DMA; 6268 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 6269 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 6270 scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT; 6271 scmd->satacmd_sec_count_msb = sec_count >> 8; 6272 #ifndef __lock_lint 6273 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 6274 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 6275 scmd->satacmd_lba_high_msb = lba >> 40; 6276 #endif 6277 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 6278 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 6279 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 6280 } 6281 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 6282 scmd->satacmd_lba_low_lsb = lba & 0xff; 6283 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 6284 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 6285 scmd->satacmd_features_reg = 0; 6286 scmd->satacmd_status_reg = 0; 6287 scmd->satacmd_error_reg = 0; 6288 6289 /* 6290 * Check if queueing commands should be used and switch 6291 * to appropriate command if possible 6292 */ 6293 if (sata_func_enable & SATA_ENABLE_QUEUING) { 6294 boolean_t using_queuing; 6295 6296 /* Queuing supported by controller and device? */ 6297 if ((sata_func_enable & SATA_ENABLE_NCQ) && 6298 (sdinfo->satadrv_features_support & 6299 SATA_DEV_F_NCQ) && 6300 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 6301 SATA_CTLF_NCQ)) { 6302 using_queuing = B_TRUE; 6303 6304 /* NCQ supported - use FPDMA READ */ 6305 scmd->satacmd_cmd_reg = 6306 SATAC_READ_FPDMA_QUEUED; 6307 scmd->satacmd_features_reg_ext = 6308 scmd->satacmd_sec_count_msb; 6309 scmd->satacmd_sec_count_msb = 0; 6310 } else if ((sdinfo->satadrv_features_support & 6311 SATA_DEV_F_TCQ) && 6312 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 6313 SATA_CTLF_QCMD)) { 6314 using_queuing = B_TRUE; 6315 6316 /* Legacy queueing */ 6317 if (sdinfo->satadrv_features_support & 6318 SATA_DEV_F_LBA48) { 6319 scmd->satacmd_cmd_reg = 6320 SATAC_READ_DMA_QUEUED_EXT; 6321 scmd->satacmd_features_reg_ext = 6322 scmd->satacmd_sec_count_msb; 6323 scmd->satacmd_sec_count_msb = 0; 6324 } else { 6325 scmd->satacmd_cmd_reg = 6326 SATAC_READ_DMA_QUEUED; 6327 } 6328 } else /* NCQ nor legacy queuing not supported */ 6329 using_queuing = B_FALSE; 6330 6331 /* 6332 * If queuing, the sector count goes in the features register 6333 * and the secount count will contain the tag. 6334 */ 6335 if (using_queuing) { 6336 scmd->satacmd_features_reg = 6337 scmd->satacmd_sec_count_lsb; 6338 scmd->satacmd_sec_count_lsb = 0; 6339 scmd->satacmd_flags.sata_queued = B_TRUE; 6340 6341 /* Set-up maximum queue depth */ 6342 scmd->satacmd_flags.sata_max_queue_depth = 6343 sdinfo->satadrv_max_queue_depth - 1; 6344 } else if (sdinfo->satadrv_features_enabled & 6345 SATA_DEV_F_E_UNTAGGED_QING) { 6346 /* 6347 * Although NCQ/TCQ is not enabled, untagged queuing 6348 * may be still used. 6349 * Set-up the maximum untagged queue depth. 6350 * Use controller's queue depth from sata_hba_tran. 6351 * SATA HBA drivers may ignore this value and rely on 6352 * the internal limits.For drivers that do not 6353 * ignore untaged queue depth, limit the value to 6354 * SATA_MAX_QUEUE_DEPTH (32), as this is the 6355 * largest value that can be passed via 6356 * satacmd_flags.sata_max_queue_depth. 6357 */ 6358 scmd->satacmd_flags.sata_max_queue_depth = 6359 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ? 6360 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1; 6361 6362 } else { 6363 scmd->satacmd_flags.sata_max_queue_depth = 0; 6364 } 6365 } else 6366 scmd->satacmd_flags.sata_max_queue_depth = 0; 6367 6368 SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst, 6369 "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n", 6370 scmd->satacmd_cmd_reg, lba, sec_count); 6371 6372 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 6373 /* Need callback function */ 6374 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 6375 synch = FALSE; 6376 } else 6377 synch = TRUE; 6378 6379 /* Transfer command to HBA */ 6380 if (sata_hba_start(spx, &rval) != 0) { 6381 /* Pkt not accepted for execution */ 6382 mutex_exit(cport_mutex); 6383 return (rval); 6384 } 6385 mutex_exit(cport_mutex); 6386 /* 6387 * If execution is non-synchronous, 6388 * a callback function will handle potential errors, translate 6389 * the response and will do a callback to a target driver. 6390 * If it was synchronous, check execution status using the same 6391 * framework callback. 6392 */ 6393 if (synch) { 6394 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6395 "synchronous execution status %x\n", 6396 spx->txlt_sata_pkt->satapkt_reason); 6397 sata_txlt_rw_completion(spx->txlt_sata_pkt); 6398 } 6399 return (TRAN_ACCEPT); 6400 } 6401 6402 6403 /* 6404 * SATA translate command: Write (various types) 6405 * Translated into appropriate type of ATA WRITE command 6406 * for SATA hard disks. 6407 * Both the device capabilities and requested operation mode are 6408 * considered. 6409 * 6410 * Following scsi cdb fields are ignored: 6411 * rwprotect, dpo, fua, fua_nv, group_number. 6412 * 6413 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 6414 * enable variable sata_func_enable), the capability of the controller and 6415 * capability of a device are checked and if both support queueing, write 6416 * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT 6417 * command rather than plain WRITE_XXX command. 6418 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 6419 * both the controller and device suport such functionality, the write 6420 * request will be translated to WRITE_FPDMA_QUEUED command. 6421 * In both cases the maximum queue depth is derived as minimum of: 6422 * HBA capability,device capability and sata_max_queue_depth variable setting. 6423 * The value passed to HBA driver is decremented by 1, because only 5 bits are 6424 * used to pass max queue depth value, and the maximum possible queue depth 6425 * is 32. 6426 * 6427 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 6428 * appropriate values in scsi_pkt fields. 6429 */ 6430 static int 6431 sata_txlt_write(sata_pkt_txlate_t *spx) 6432 { 6433 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6434 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6435 sata_drive_info_t *sdinfo; 6436 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 6437 uint16_t sec_count; 6438 uint64_t lba; 6439 int rval, reason; 6440 int synch; 6441 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 6442 6443 mutex_enter(cport_mutex); 6444 6445 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 6446 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 6447 mutex_exit(cport_mutex); 6448 return (rval); 6449 } 6450 6451 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 6452 &spx->txlt_sata_pkt->satapkt_device); 6453 6454 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 6455 /* 6456 * Extract LBA and sector count from scsi CDB 6457 */ 6458 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 6459 case SCMD_WRITE: 6460 /* 6-byte scsi read cmd : 0x0A */ 6461 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 6462 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 6463 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6464 sec_count = scsipkt->pkt_cdbp[4]; 6465 /* sec_count 0 will be interpreted as 256 by a device */ 6466 break; 6467 case SCMD_WRITE_G1: 6468 /* 10-bytes scsi write command : 0x2A */ 6469 lba = scsipkt->pkt_cdbp[2]; 6470 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6471 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 6472 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 6473 sec_count = scsipkt->pkt_cdbp[7]; 6474 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 6475 break; 6476 case SCMD_WRITE_G5: 6477 /* 12-bytes scsi read command : 0xAA */ 6478 lba = scsipkt->pkt_cdbp[2]; 6479 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6480 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 6481 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 6482 sec_count = scsipkt->pkt_cdbp[6]; 6483 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 6484 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 6485 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 6486 break; 6487 case SCMD_WRITE_G4: 6488 /* 16-bytes scsi write command : 0x8A */ 6489 lba = scsipkt->pkt_cdbp[2]; 6490 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6491 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 6492 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 6493 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 6494 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 6495 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 6496 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 6497 sec_count = scsipkt->pkt_cdbp[10]; 6498 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 6499 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 6500 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 6501 break; 6502 default: 6503 /* Unsupported command */ 6504 mutex_exit(cport_mutex); 6505 return (sata_txlt_invalid_command(spx)); 6506 } 6507 6508 /* 6509 * Check if specified address and length exceeds device capacity 6510 */ 6511 if ((lba >= sdinfo->satadrv_capacity) || 6512 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 6513 /* LBA out of range */ 6514 mutex_exit(cport_mutex); 6515 return (sata_txlt_lba_out_of_range(spx)); 6516 } 6517 6518 /* 6519 * For zero-length transfer, emulate good completion of the command 6520 * (reasons for rejecting the command were already checked). 6521 * No DMA resources were allocated. 6522 */ 6523 if (spx->txlt_dma_cookie_list == NULL) { 6524 mutex_exit(cport_mutex); 6525 return (sata_emul_rw_completion(spx)); 6526 } 6527 6528 /* 6529 * Build cmd block depending on the device capability and 6530 * requested operation mode. 6531 * Do not bother with non-dma mode- we are working only with 6532 * devices supporting DMA. 6533 */ 6534 scmd->satacmd_addr_type = ATA_ADDR_LBA; 6535 scmd->satacmd_device_reg = SATA_ADH_LBA; 6536 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA; 6537 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 6538 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 6539 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT; 6540 scmd->satacmd_sec_count_msb = sec_count >> 8; 6541 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 6542 #ifndef __lock_lint 6543 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 6544 scmd->satacmd_lba_high_msb = lba >> 40; 6545 #endif 6546 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 6547 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 6548 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 6549 } 6550 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 6551 scmd->satacmd_lba_low_lsb = lba & 0xff; 6552 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 6553 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 6554 scmd->satacmd_features_reg = 0; 6555 scmd->satacmd_status_reg = 0; 6556 scmd->satacmd_error_reg = 0; 6557 6558 /* 6559 * Check if queueing commands should be used and switch 6560 * to appropriate command if possible 6561 */ 6562 if (sata_func_enable & SATA_ENABLE_QUEUING) { 6563 boolean_t using_queuing; 6564 6565 /* Queuing supported by controller and device? */ 6566 if ((sata_func_enable & SATA_ENABLE_NCQ) && 6567 (sdinfo->satadrv_features_support & 6568 SATA_DEV_F_NCQ) && 6569 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 6570 SATA_CTLF_NCQ)) { 6571 using_queuing = B_TRUE; 6572 6573 /* NCQ supported - use FPDMA WRITE */ 6574 scmd->satacmd_cmd_reg = 6575 SATAC_WRITE_FPDMA_QUEUED; 6576 scmd->satacmd_features_reg_ext = 6577 scmd->satacmd_sec_count_msb; 6578 scmd->satacmd_sec_count_msb = 0; 6579 } else if ((sdinfo->satadrv_features_support & 6580 SATA_DEV_F_TCQ) && 6581 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 6582 SATA_CTLF_QCMD)) { 6583 using_queuing = B_TRUE; 6584 6585 /* Legacy queueing */ 6586 if (sdinfo->satadrv_features_support & 6587 SATA_DEV_F_LBA48) { 6588 scmd->satacmd_cmd_reg = 6589 SATAC_WRITE_DMA_QUEUED_EXT; 6590 scmd->satacmd_features_reg_ext = 6591 scmd->satacmd_sec_count_msb; 6592 scmd->satacmd_sec_count_msb = 0; 6593 } else { 6594 scmd->satacmd_cmd_reg = 6595 SATAC_WRITE_DMA_QUEUED; 6596 } 6597 } else /* NCQ nor legacy queuing not supported */ 6598 using_queuing = B_FALSE; 6599 6600 if (using_queuing) { 6601 scmd->satacmd_features_reg = 6602 scmd->satacmd_sec_count_lsb; 6603 scmd->satacmd_sec_count_lsb = 0; 6604 scmd->satacmd_flags.sata_queued = B_TRUE; 6605 /* Set-up maximum queue depth */ 6606 scmd->satacmd_flags.sata_max_queue_depth = 6607 sdinfo->satadrv_max_queue_depth - 1; 6608 } else if (sdinfo->satadrv_features_enabled & 6609 SATA_DEV_F_E_UNTAGGED_QING) { 6610 /* 6611 * Although NCQ/TCQ is not enabled, untagged queuing 6612 * may be still used. 6613 * Set-up the maximum untagged queue depth. 6614 * Use controller's queue depth from sata_hba_tran. 6615 * SATA HBA drivers may ignore this value and rely on 6616 * the internal limits. For drivera that do not 6617 * ignore untaged queue depth, limit the value to 6618 * SATA_MAX_QUEUE_DEPTH (32), as this is the 6619 * largest value that can be passed via 6620 * satacmd_flags.sata_max_queue_depth. 6621 */ 6622 scmd->satacmd_flags.sata_max_queue_depth = 6623 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ? 6624 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1; 6625 6626 } else { 6627 scmd->satacmd_flags.sata_max_queue_depth = 0; 6628 } 6629 } else 6630 scmd->satacmd_flags.sata_max_queue_depth = 0; 6631 6632 SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6633 "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n", 6634 scmd->satacmd_cmd_reg, lba, sec_count); 6635 6636 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 6637 /* Need callback function */ 6638 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 6639 synch = FALSE; 6640 } else 6641 synch = TRUE; 6642 6643 /* Transfer command to HBA */ 6644 if (sata_hba_start(spx, &rval) != 0) { 6645 /* Pkt not accepted for execution */ 6646 mutex_exit(cport_mutex); 6647 return (rval); 6648 } 6649 mutex_exit(cport_mutex); 6650 6651 /* 6652 * If execution is non-synchronous, 6653 * a callback function will handle potential errors, translate 6654 * the response and will do a callback to a target driver. 6655 * If it was synchronous, check execution status using the same 6656 * framework callback. 6657 */ 6658 if (synch) { 6659 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6660 "synchronous execution status %x\n", 6661 spx->txlt_sata_pkt->satapkt_reason); 6662 sata_txlt_rw_completion(spx->txlt_sata_pkt); 6663 } 6664 return (TRAN_ACCEPT); 6665 } 6666 6667 6668 /* 6669 * Implements SCSI SBC WRITE BUFFER command download microcode option 6670 */ 6671 static int 6672 sata_txlt_write_buffer(sata_pkt_txlate_t *spx) 6673 { 6674 #define WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE 4 6675 #define WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE 5 6676 6677 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6678 struct sata_pkt *sata_pkt = spx->txlt_sata_pkt; 6679 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6680 6681 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 6682 struct scsi_extended_sense *sense; 6683 int rval, mode, sector_count, reason; 6684 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 6685 6686 mode = scsipkt->pkt_cdbp[1] & 0x1f; 6687 6688 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6689 "sata_txlt_write_buffer, mode 0x%x\n", mode); 6690 6691 mutex_enter(cport_mutex); 6692 6693 if ((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 6694 TRAN_ACCEPT) { 6695 mutex_exit(cport_mutex); 6696 return (rval); 6697 } 6698 6699 /* Use synchronous mode */ 6700 spx->txlt_sata_pkt->satapkt_op_mode 6701 |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 6702 6703 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 6704 6705 scsipkt->pkt_reason = CMD_CMPLT; 6706 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6707 STATE_SENT_CMD | STATE_GOT_STATUS; 6708 6709 /* 6710 * The SCSI to ATA translation specification only calls 6711 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE. 6712 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but 6713 * ATA 8 (draft) got rid of download microcode for temp 6714 * and it is even optional for ATA 7, so it may be aborted. 6715 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as 6716 * it is not specified and the buffer offset for SCSI is a 16-bit 6717 * value in bytes, but for ATA it is a 16-bit offset in 512 byte 6718 * sectors. Thus the offset really doesn't buy us anything. 6719 * If and when ATA 8 is stabilized and the SCSI to ATA specification 6720 * is revised, this can be revisisted. 6721 */ 6722 /* Reject not supported request */ 6723 switch (mode) { 6724 case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE: 6725 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP; 6726 break; 6727 case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE: 6728 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE; 6729 break; 6730 default: 6731 goto bad_param; 6732 } 6733 6734 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 6735 6736 scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE; 6737 if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0) 6738 goto bad_param; 6739 sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE; 6740 scmd->satacmd_sec_count_lsb = (uint8_t)sector_count; 6741 scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8; 6742 scmd->satacmd_lba_mid_lsb = 0; 6743 scmd->satacmd_lba_high_lsb = 0; 6744 scmd->satacmd_device_reg = 0; 6745 spx->txlt_sata_pkt->satapkt_comp = NULL; 6746 scmd->satacmd_addr_type = 0; 6747 6748 /* Transfer command to HBA */ 6749 if (sata_hba_start(spx, &rval) != 0) { 6750 /* Pkt not accepted for execution */ 6751 mutex_exit(cport_mutex); 6752 return (rval); 6753 } 6754 6755 mutex_exit(cport_mutex); 6756 6757 /* Then we need synchronous check the status of the disk */ 6758 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6759 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 6760 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 6761 scsipkt->pkt_reason = CMD_CMPLT; 6762 6763 /* Download commmand succeed, so probe and identify device */ 6764 sata_reidentify_device(spx); 6765 } else { 6766 /* Something went wrong, microcode download command failed */ 6767 scsipkt->pkt_reason = CMD_INCOMPLETE; 6768 *scsipkt->pkt_scbp = STATUS_CHECK; 6769 sense = sata_arq_sense(spx); 6770 switch (sata_pkt->satapkt_reason) { 6771 case SATA_PKT_PORT_ERROR: 6772 /* 6773 * We have no device data. Assume no data transfered. 6774 */ 6775 sense->es_key = KEY_HARDWARE_ERROR; 6776 break; 6777 6778 case SATA_PKT_DEV_ERROR: 6779 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 6780 SATA_STATUS_ERR) { 6781 /* 6782 * determine dev error reason from error 6783 * reg content 6784 */ 6785 sata_decode_device_error(spx, sense); 6786 break; 6787 } 6788 /* No extended sense key - no info available */ 6789 break; 6790 6791 case SATA_PKT_TIMEOUT: 6792 scsipkt->pkt_reason = CMD_TIMEOUT; 6793 scsipkt->pkt_statistics |= 6794 STAT_TIMEOUT | STAT_DEV_RESET; 6795 /* No extended sense key ? */ 6796 break; 6797 6798 case SATA_PKT_ABORTED: 6799 scsipkt->pkt_reason = CMD_ABORTED; 6800 scsipkt->pkt_statistics |= STAT_ABORTED; 6801 /* No extended sense key ? */ 6802 break; 6803 6804 case SATA_PKT_RESET: 6805 /* pkt aborted by an explicit reset from a host */ 6806 scsipkt->pkt_reason = CMD_RESET; 6807 scsipkt->pkt_statistics |= STAT_DEV_RESET; 6808 break; 6809 6810 default: 6811 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 6812 "sata_txlt_nodata_cmd_completion: " 6813 "invalid packet completion reason %d", 6814 sata_pkt->satapkt_reason)); 6815 scsipkt->pkt_reason = CMD_TRAN_ERR; 6816 break; 6817 } 6818 6819 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6820 "scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6821 6822 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 6823 /* scsi callback required */ 6824 scsi_hba_pkt_comp(scsipkt); 6825 } 6826 return (TRAN_ACCEPT); 6827 6828 bad_param: 6829 mutex_exit(cport_mutex); 6830 *scsipkt->pkt_scbp = STATUS_CHECK; 6831 sense = sata_arq_sense(spx); 6832 sense->es_key = KEY_ILLEGAL_REQUEST; 6833 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 6834 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6835 scsipkt->pkt_comp != NULL) { 6836 /* scsi callback required */ 6837 if (servicing_interrupt()) { 6838 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6839 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 6840 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 6841 TASKQID_INVALID) { 6842 return (TRAN_BUSY); 6843 } 6844 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6845 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 6846 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 6847 /* Scheduling the callback failed */ 6848 return (TRAN_BUSY); 6849 } 6850 } 6851 return (rval); 6852 } 6853 6854 /* 6855 * Re-identify device after doing a firmware download. 6856 */ 6857 static void 6858 sata_reidentify_device(sata_pkt_txlate_t *spx) 6859 { 6860 #define DOWNLOAD_WAIT_TIME_SECS 60 6861 #define DOWNLOAD_WAIT_INTERVAL_SECS 1 6862 int rval; 6863 int retry_cnt; 6864 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6865 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 6866 sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device; 6867 sata_drive_info_t *sdinfo; 6868 6869 /* 6870 * Before returning good status, probe device. 6871 * Device probing will get IDENTIFY DEVICE data, if possible. 6872 * The assumption is that the new microcode is applied by the 6873 * device. It is a caller responsibility to verify this. 6874 */ 6875 for (retry_cnt = 0; 6876 retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS; 6877 retry_cnt++) { 6878 rval = sata_probe_device(sata_hba_inst, &sata_device); 6879 6880 if (rval == SATA_SUCCESS) { /* Set default features */ 6881 sdinfo = sata_get_device_info(sata_hba_inst, 6882 &sata_device); 6883 if (sata_initialize_device(sata_hba_inst, sdinfo) != 6884 SATA_SUCCESS) { 6885 /* retry */ 6886 rval = sata_initialize_device(sata_hba_inst, 6887 sdinfo); 6888 if (rval == SATA_RETRY) 6889 sata_log(sata_hba_inst, CE_WARN, 6890 "SATA device at port %d pmport %d -" 6891 " default device features could not" 6892 " be set. Device may not operate " 6893 "as expected.", 6894 sata_device.satadev_addr.cport, 6895 sata_device.satadev_addr.pmport); 6896 } 6897 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 6898 scsi_hba_pkt_comp(scsipkt); 6899 return; 6900 } else if (rval == SATA_RETRY) { 6901 delay(drv_usectohz(1000000 * 6902 DOWNLOAD_WAIT_INTERVAL_SECS)); 6903 continue; 6904 } else /* failed - no reason to retry */ 6905 break; 6906 } 6907 6908 /* 6909 * Something went wrong, device probing failed. 6910 */ 6911 SATA_LOG_D((sata_hba_inst, CE_WARN, 6912 "Cannot probe device after downloading microcode\n")); 6913 6914 /* Reset device to force retrying the probe. */ 6915 (void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 6916 (SATA_DIP(sata_hba_inst), &sata_device); 6917 6918 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 6919 scsi_hba_pkt_comp(scsipkt); 6920 } 6921 6922 6923 /* 6924 * Translate command: Synchronize Cache. 6925 * Translates into Flush Cache command for SATA hard disks. 6926 * 6927 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 6928 * appropriate values in scsi_pkt fields. 6929 */ 6930 static int 6931 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx) 6932 { 6933 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6934 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 6935 int rval, reason; 6936 int synch; 6937 6938 mutex_enter(cport_mutex); 6939 6940 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 6941 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 6942 mutex_exit(cport_mutex); 6943 return (rval); 6944 } 6945 6946 scmd->satacmd_addr_type = 0; 6947 scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE; 6948 scmd->satacmd_device_reg = 0; 6949 scmd->satacmd_sec_count_lsb = 0; 6950 scmd->satacmd_lba_low_lsb = 0; 6951 scmd->satacmd_lba_mid_lsb = 0; 6952 scmd->satacmd_lba_high_lsb = 0; 6953 scmd->satacmd_features_reg = 0; 6954 scmd->satacmd_status_reg = 0; 6955 scmd->satacmd_error_reg = 0; 6956 6957 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6958 "sata_txlt_synchronize_cache\n", NULL); 6959 6960 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 6961 /* Need to set-up a callback function */ 6962 spx->txlt_sata_pkt->satapkt_comp = 6963 sata_txlt_nodata_cmd_completion; 6964 synch = FALSE; 6965 } else 6966 synch = TRUE; 6967 6968 /* Transfer command to HBA */ 6969 if (sata_hba_start(spx, &rval) != 0) { 6970 /* Pkt not accepted for execution */ 6971 mutex_exit(cport_mutex); 6972 return (rval); 6973 } 6974 mutex_exit(cport_mutex); 6975 6976 /* 6977 * If execution non-synchronous, it had to be completed 6978 * a callback function will handle potential errors, translate 6979 * the response and will do a callback to a target driver. 6980 * If it was synchronous, check status, using the same 6981 * framework callback. 6982 */ 6983 if (synch) { 6984 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6985 "synchronous execution status %x\n", 6986 spx->txlt_sata_pkt->satapkt_reason); 6987 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 6988 } 6989 return (TRAN_ACCEPT); 6990 } 6991 6992 6993 /* 6994 * Send pkt to SATA HBA driver 6995 * 6996 * This function may be called only if the operation is requested by scsi_pkt, 6997 * i.e. scsi_pkt is not NULL. 6998 * 6999 * This function has to be called with cport mutex held. It does release 7000 * the mutex when it calls HBA driver sata_tran_start function and 7001 * re-acquires it afterwards. 7002 * 7003 * If return value is 0, pkt was accepted, -1 otherwise 7004 * rval is set to appropriate sata_scsi_start return value. 7005 * 7006 * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not 7007 * have called the sata_pkt callback function for this packet. 7008 * 7009 * The scsi callback has to be performed by the caller of this routine. 7010 */ 7011 static int 7012 sata_hba_start(sata_pkt_txlate_t *spx, int *rval) 7013 { 7014 int stat; 7015 uint8_t cport = SATA_TXLT_CPORT(spx); 7016 uint8_t pmport = SATA_TXLT_PMPORT(spx); 7017 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 7018 sata_drive_info_t *sdinfo; 7019 sata_pmult_info_t *pminfo; 7020 sata_pmport_info_t *pmportinfo = NULL; 7021 sata_device_t *sata_device = NULL; 7022 uint8_t cmd; 7023 struct sata_cmd_flags cmd_flags; 7024 7025 ASSERT(spx->txlt_sata_pkt != NULL); 7026 7027 ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7028 7029 sdinfo = sata_get_device_info(sata_hba_inst, 7030 &spx->txlt_sata_pkt->satapkt_device); 7031 ASSERT(sdinfo != NULL); 7032 7033 /* Clear device reset state? */ 7034 /* qual should be XXX_DPMPORT, but add XXX_PMPORT in case */ 7035 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT || 7036 sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) { 7037 7038 /* 7039 * Get the pmult_info of the its parent port multiplier, all 7040 * sub-devices share a common device reset flags on in 7041 * pmult_info. 7042 */ 7043 pminfo = SATA_PMULT_INFO(sata_hba_inst, cport); 7044 pmportinfo = pminfo->pmult_dev_port[pmport]; 7045 ASSERT(pminfo != NULL); 7046 if (pminfo->pmult_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) { 7047 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 7048 sata_clear_dev_reset = B_TRUE; 7049 pminfo->pmult_event_flags &= 7050 ~SATA_EVNT_CLEAR_DEVICE_RESET; 7051 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 7052 "sata_hba_start: clearing device reset state" 7053 "on pmult.\n", NULL); 7054 } 7055 } else { 7056 if (sdinfo->satadrv_event_flags & 7057 SATA_EVNT_CLEAR_DEVICE_RESET) { 7058 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 7059 sata_clear_dev_reset = B_TRUE; 7060 sdinfo->satadrv_event_flags &= 7061 ~SATA_EVNT_CLEAR_DEVICE_RESET; 7062 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 7063 "sata_hba_start: clearing device reset state\n", 7064 NULL); 7065 } 7066 } 7067 7068 cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg; 7069 cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags; 7070 sata_device = &spx->txlt_sata_pkt->satapkt_device; 7071 7072 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7073 7074 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 7075 "Sata cmd 0x%2x\n", cmd); 7076 7077 stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 7078 spx->txlt_sata_pkt); 7079 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7080 /* 7081 * If sata pkt was accepted and executed in asynchronous mode, i.e. 7082 * with the sata callback, the sata_pkt could be already destroyed 7083 * by the time we check ther return status from the hba_start() 7084 * function, because sata_scsi_destroy_pkt() could have been already 7085 * called (perhaps in the interrupt context). So, in such case, there 7086 * should be no references to it. In other cases, sata_pkt still 7087 * exists. 7088 */ 7089 if (stat == SATA_TRAN_ACCEPTED) { 7090 /* 7091 * pkt accepted for execution. 7092 * If it was executed synchronously, it is already completed 7093 * and pkt completion_reason indicates completion status. 7094 */ 7095 *rval = TRAN_ACCEPT; 7096 return (0); 7097 } 7098 7099 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 7100 switch (stat) { 7101 case SATA_TRAN_QUEUE_FULL: 7102 /* 7103 * Controller detected queue full condition. 7104 */ 7105 SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst, 7106 "sata_hba_start: queue full\n", NULL); 7107 7108 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 7109 *spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL; 7110 7111 *rval = TRAN_BUSY; 7112 break; 7113 7114 case SATA_TRAN_PORT_ERROR: 7115 /* 7116 * Communication/link with device or general port error 7117 * detected before pkt execution begun. 7118 */ 7119 if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 7120 SATA_ADDR_CPORT || 7121 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 7122 SATA_ADDR_DCPORT) 7123 sata_log(sata_hba_inst, CE_CONT, 7124 "SATA port %d error", 7125 sata_device->satadev_addr.cport); 7126 else 7127 sata_log(sata_hba_inst, CE_CONT, 7128 "SATA port %d:%d error\n", 7129 sata_device->satadev_addr.cport, 7130 sata_device->satadev_addr.pmport); 7131 7132 /* 7133 * Update the port/device structure. 7134 * sata_pkt should be still valid. Since port error is 7135 * returned, sata_device content should reflect port 7136 * state - it means, that sata address have been changed, 7137 * because original packet's sata address refered to a device 7138 * attached to some port. 7139 */ 7140 if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT || 7141 sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) { 7142 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7143 mutex_enter(&pmportinfo->pmport_mutex); 7144 sata_update_pmport_info(sata_hba_inst, sata_device); 7145 mutex_exit(&pmportinfo->pmport_mutex); 7146 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7147 } else { 7148 sata_update_port_info(sata_hba_inst, sata_device); 7149 } 7150 7151 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 7152 *rval = TRAN_FATAL_ERROR; 7153 break; 7154 7155 case SATA_TRAN_CMD_UNSUPPORTED: 7156 /* 7157 * Command rejected by HBA as unsupported. It was HBA driver 7158 * that rejected the command, command was not sent to 7159 * an attached device. 7160 */ 7161 if ((sdinfo != NULL) && 7162 (sdinfo->satadrv_state & SATA_DSTATE_RESET)) 7163 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 7164 "sat_hba_start: cmd 0x%2x rejected " 7165 "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd); 7166 7167 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7168 (void) sata_txlt_invalid_command(spx); 7169 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7170 7171 *rval = TRAN_ACCEPT; 7172 break; 7173 7174 case SATA_TRAN_BUSY: 7175 /* 7176 * Command rejected by HBA because other operation prevents 7177 * accepting the packet, or device is in RESET condition. 7178 */ 7179 if (sdinfo != NULL) { 7180 sdinfo->satadrv_state = 7181 spx->txlt_sata_pkt->satapkt_device.satadev_state; 7182 7183 if (sdinfo->satadrv_state & SATA_DSTATE_RESET) { 7184 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 7185 "sata_hba_start: cmd 0x%2x rejected " 7186 "because of device reset condition\n", 7187 cmd); 7188 } else { 7189 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 7190 "sata_hba_start: cmd 0x%2x rejected " 7191 "with SATA_TRAN_BUSY status\n", 7192 cmd); 7193 } 7194 } 7195 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 7196 *rval = TRAN_BUSY; 7197 break; 7198 7199 default: 7200 /* Unrecognized HBA response */ 7201 SATA_LOG_D((sata_hba_inst, CE_WARN, 7202 "sata_hba_start: unrecognized HBA response " 7203 "to cmd : 0x%2x resp 0x%x", cmd, rval)); 7204 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 7205 *rval = TRAN_FATAL_ERROR; 7206 break; 7207 } 7208 7209 /* 7210 * If we got here, the packet was rejected. 7211 * Check if we need to remember reset state clearing request 7212 */ 7213 if (cmd_flags.sata_clear_dev_reset) { 7214 /* 7215 * Check if device is still configured - it may have 7216 * disapeared from the configuration 7217 */ 7218 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 7219 if (sdinfo != NULL) { 7220 /* 7221 * Restore the flag that requests clearing of 7222 * the device reset state, 7223 * so the next sata packet may carry it to HBA. 7224 */ 7225 if (sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT || 7226 sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT) { 7227 pminfo->pmult_event_flags |= 7228 SATA_EVNT_CLEAR_DEVICE_RESET; 7229 } else { 7230 sdinfo->satadrv_event_flags |= 7231 SATA_EVNT_CLEAR_DEVICE_RESET; 7232 } 7233 } 7234 } 7235 return (-1); 7236 } 7237 7238 /* 7239 * Scsi response setup for invalid LBA 7240 * 7241 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 7242 */ 7243 static int 7244 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx) 7245 { 7246 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7247 struct scsi_extended_sense *sense; 7248 7249 scsipkt->pkt_reason = CMD_CMPLT; 7250 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7251 STATE_SENT_CMD | STATE_GOT_STATUS; 7252 *scsipkt->pkt_scbp = STATUS_CHECK; 7253 7254 *scsipkt->pkt_scbp = STATUS_CHECK; 7255 sense = sata_arq_sense(spx); 7256 sense->es_key = KEY_ILLEGAL_REQUEST; 7257 sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE; 7258 7259 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 7260 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 7261 7262 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 7263 scsipkt->pkt_comp != NULL) { 7264 /* scsi callback required */ 7265 if (servicing_interrupt()) { 7266 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7267 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7268 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 7269 TASKQID_INVALID) { 7270 return (TRAN_BUSY); 7271 } 7272 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7273 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7274 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 7275 /* Scheduling the callback failed */ 7276 return (TRAN_BUSY); 7277 } 7278 } 7279 return (TRAN_ACCEPT); 7280 } 7281 7282 7283 /* 7284 * Analyze device status and error registers and translate them into 7285 * appropriate scsi sense codes. 7286 * NOTE: non-packet commands only for now 7287 */ 7288 static void 7289 sata_decode_device_error(sata_pkt_txlate_t *spx, 7290 struct scsi_extended_sense *sense) 7291 { 7292 uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg; 7293 7294 ASSERT(sense != NULL); 7295 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 7296 SATA_STATUS_ERR); 7297 7298 7299 if (err_reg & SATA_ERROR_ICRC) { 7300 sense->es_key = KEY_ABORTED_COMMAND; 7301 sense->es_add_code = 0x08; /* Communication failure */ 7302 return; 7303 } 7304 7305 if (err_reg & SATA_ERROR_UNC) { 7306 sense->es_key = KEY_MEDIUM_ERROR; 7307 /* Information bytes (LBA) need to be set by a caller */ 7308 return; 7309 } 7310 7311 /* ADD HERE: MC error bit handling for ATAPI CD/DVD */ 7312 if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) { 7313 sense->es_key = KEY_UNIT_ATTENTION; 7314 sense->es_add_code = 0x3a; /* No media present */ 7315 return; 7316 } 7317 7318 if (err_reg & SATA_ERROR_IDNF) { 7319 if (err_reg & SATA_ERROR_ABORT) { 7320 sense->es_key = KEY_ABORTED_COMMAND; 7321 } else { 7322 sense->es_key = KEY_ILLEGAL_REQUEST; 7323 sense->es_add_code = 0x21; /* LBA out of range */ 7324 } 7325 return; 7326 } 7327 7328 if (err_reg & SATA_ERROR_ABORT) { 7329 ASSERT(spx->txlt_sata_pkt != NULL); 7330 sense->es_key = KEY_ABORTED_COMMAND; 7331 return; 7332 } 7333 } 7334 7335 /* 7336 * Extract error LBA from sata_pkt.satapkt_cmd register fields 7337 */ 7338 static void 7339 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba) 7340 { 7341 sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd; 7342 7343 *lba = 0; 7344 if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) { 7345 *lba = sata_cmd->satacmd_lba_high_msb; 7346 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb; 7347 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb; 7348 } else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) { 7349 *lba = sata_cmd->satacmd_device_reg & 0xf; 7350 } 7351 *lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb; 7352 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb; 7353 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb; 7354 } 7355 7356 /* 7357 * This is fixed sense format - if LBA exceeds the info field size, 7358 * no valid info will be returned (valid bit in extended sense will 7359 * be set to 0). 7360 */ 7361 static struct scsi_extended_sense * 7362 sata_arq_sense(sata_pkt_txlate_t *spx) 7363 { 7364 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7365 struct scsi_arq_status *arqs; 7366 struct scsi_extended_sense *sense; 7367 7368 /* Fill ARQ sense data */ 7369 scsipkt->pkt_state |= STATE_ARQ_DONE; 7370 arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp; 7371 *(uchar_t *)&arqs->sts_status = STATUS_CHECK; 7372 *(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD; 7373 arqs->sts_rqpkt_reason = CMD_CMPLT; 7374 arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7375 STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS; 7376 arqs->sts_rqpkt_resid = 0; 7377 sense = &arqs->sts_sensedata; 7378 bzero(sense, sizeof (struct scsi_extended_sense)); 7379 sata_fixed_sense_data_preset(sense); 7380 return (sense); 7381 } 7382 7383 /* 7384 * ATA Pass Through support 7385 * Sets flags indicating that an invalid value was found in some 7386 * field in the command. It could be something illegal according to 7387 * the SAT-2 spec or it could be a feature that is not (yet?) 7388 * supported. 7389 */ 7390 static int 7391 sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *spx) 7392 { 7393 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7394 struct scsi_extended_sense *sense = sata_arq_sense(spx); 7395 7396 scsipkt->pkt_reason = CMD_CMPLT; 7397 *scsipkt->pkt_scbp = STATUS_CHECK; 7398 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7399 STATE_SENT_CMD | STATE_GOT_STATUS; 7400 7401 sense = sata_arq_sense(spx); 7402 sense->es_key = KEY_ILLEGAL_REQUEST; 7403 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 7404 7405 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 7406 scsipkt->pkt_comp != NULL) { 7407 /* scsi callback required */ 7408 if (servicing_interrupt()) { 7409 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7410 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7411 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 7412 TASKQID_INVALID) { 7413 return (TRAN_BUSY); 7414 } 7415 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7416 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7417 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 7418 /* Scheduling the callback failed */ 7419 return (TRAN_BUSY); 7420 } 7421 } 7422 7423 return (TRAN_ACCEPT); 7424 } 7425 7426 /* 7427 * The UNMAP command considers it not to be an error if the parameter length 7428 * or block descriptor length is 0. For this case, there is nothing for TRIM 7429 * to do so just complete the command. 7430 */ 7431 static int 7432 sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *spx) 7433 { 7434 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7435 7436 scsipkt->pkt_reason = CMD_CMPLT; 7437 *scsipkt->pkt_scbp = STATUS_GOOD; 7438 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7439 STATE_SENT_CMD | STATE_GOT_STATUS; 7440 7441 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 7442 scsipkt->pkt_comp != NULL) { 7443 /* scsi callback required */ 7444 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7445 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7446 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 7447 /* Scheduling the callback failed */ 7448 return (TRAN_BUSY); 7449 } 7450 } 7451 7452 return (TRAN_ACCEPT); 7453 } 7454 7455 /* 7456 * Emulated SATA Read/Write command completion for zero-length requests. 7457 * This request always succedes, so in synchronous mode it always returns 7458 * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the 7459 * callback cannot be scheduled. 7460 */ 7461 static int 7462 sata_emul_rw_completion(sata_pkt_txlate_t *spx) 7463 { 7464 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7465 7466 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7467 STATE_SENT_CMD | STATE_GOT_STATUS; 7468 scsipkt->pkt_reason = CMD_CMPLT; 7469 *scsipkt->pkt_scbp = STATUS_GOOD; 7470 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 7471 /* scsi callback required - have to schedule it */ 7472 if (servicing_interrupt()) { 7473 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7474 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7475 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 7476 TASKQID_INVALID) { 7477 return (TRAN_BUSY); 7478 } 7479 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7480 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7481 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 7482 /* Scheduling the callback failed */ 7483 return (TRAN_BUSY); 7484 } 7485 } 7486 return (TRAN_ACCEPT); 7487 } 7488 7489 7490 /* 7491 * Translate completion status of SATA read/write commands into scsi response. 7492 * pkt completion_reason is checked to determine the completion status. 7493 * Do scsi callback if necessary. 7494 * 7495 * Note: this function may be called also for synchronously executed 7496 * commands. 7497 * This function may be used only if scsi_pkt is non-NULL. 7498 */ 7499 static void 7500 sata_txlt_rw_completion(sata_pkt_t *sata_pkt) 7501 { 7502 sata_pkt_txlate_t *spx = 7503 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7504 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 7505 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7506 struct scsi_extended_sense *sense; 7507 uint64_t lba; 7508 struct buf *bp; 7509 int rval; 7510 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 7511 /* Normal completion */ 7512 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7513 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 7514 scsipkt->pkt_reason = CMD_CMPLT; 7515 *scsipkt->pkt_scbp = STATUS_GOOD; 7516 if (spx->txlt_tmp_buf != NULL) { 7517 /* Temporary buffer was used */ 7518 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 7519 if (bp->b_flags & B_READ) { 7520 rval = ddi_dma_sync( 7521 spx->txlt_buf_dma_handle, 0, 0, 7522 DDI_DMA_SYNC_FORCPU); 7523 ASSERT(rval == DDI_SUCCESS); 7524 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 7525 bp->b_bcount); 7526 } 7527 } 7528 } else { 7529 /* 7530 * Something went wrong - analyze return 7531 */ 7532 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7533 STATE_SENT_CMD | STATE_GOT_STATUS; 7534 scsipkt->pkt_reason = CMD_INCOMPLETE; 7535 *scsipkt->pkt_scbp = STATUS_CHECK; 7536 sense = sata_arq_sense(spx); 7537 ASSERT(sense != NULL); 7538 7539 /* 7540 * SATA_PKT_DEV_ERROR is the only case where we may be able to 7541 * extract from device registers the failing LBA. 7542 */ 7543 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 7544 if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) && 7545 (scmd->satacmd_lba_mid_msb != 0 || 7546 scmd->satacmd_lba_high_msb != 0)) { 7547 /* 7548 * We have problem reporting this cmd LBA 7549 * in fixed sense data format, because of 7550 * the size of the scsi LBA fields. 7551 */ 7552 sense->es_valid = 0; 7553 } else { 7554 sata_extract_error_lba(spx, &lba); 7555 sense->es_info_1 = (lba & 0xFF000000) >> 24; 7556 sense->es_info_2 = (lba & 0xFF0000) >> 16; 7557 sense->es_info_3 = (lba & 0xFF00) >> 8; 7558 sense->es_info_4 = lba & 0xFF; 7559 } 7560 } else { 7561 /* Invalid extended sense info */ 7562 sense->es_valid = 0; 7563 } 7564 7565 switch (sata_pkt->satapkt_reason) { 7566 case SATA_PKT_PORT_ERROR: 7567 /* We may want to handle DEV GONE state as well */ 7568 /* 7569 * We have no device data. Assume no data transfered. 7570 */ 7571 sense->es_key = KEY_HARDWARE_ERROR; 7572 break; 7573 7574 case SATA_PKT_DEV_ERROR: 7575 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 7576 SATA_STATUS_ERR) { 7577 /* 7578 * determine dev error reason from error 7579 * reg content 7580 */ 7581 sata_decode_device_error(spx, sense); 7582 if (sense->es_key == KEY_MEDIUM_ERROR) { 7583 switch (scmd->satacmd_cmd_reg) { 7584 case SATAC_READ_DMA: 7585 case SATAC_READ_DMA_EXT: 7586 case SATAC_READ_DMA_QUEUED: 7587 case SATAC_READ_DMA_QUEUED_EXT: 7588 case SATAC_READ_FPDMA_QUEUED: 7589 /* Unrecovered read error */ 7590 sense->es_add_code = 7591 SD_SCSI_ASC_UNREC_READ_ERR; 7592 break; 7593 case SATAC_WRITE_DMA: 7594 case SATAC_WRITE_DMA_EXT: 7595 case SATAC_WRITE_DMA_QUEUED: 7596 case SATAC_WRITE_DMA_QUEUED_EXT: 7597 case SATAC_WRITE_FPDMA_QUEUED: 7598 /* Write error */ 7599 sense->es_add_code = 7600 SD_SCSI_ASC_WRITE_ERR; 7601 break; 7602 default: 7603 /* Internal error */ 7604 SATA_LOG_D(( 7605 spx->txlt_sata_hba_inst, 7606 CE_WARN, 7607 "sata_txlt_rw_completion :" 7608 "internal error - invalid " 7609 "command 0x%2x", 7610 scmd->satacmd_cmd_reg)); 7611 break; 7612 } 7613 } 7614 break; 7615 } 7616 /* No extended sense key - no info available */ 7617 scsipkt->pkt_reason = CMD_INCOMPLETE; 7618 break; 7619 7620 case SATA_PKT_TIMEOUT: 7621 scsipkt->pkt_reason = CMD_TIMEOUT; 7622 scsipkt->pkt_statistics |= 7623 STAT_TIMEOUT | STAT_DEV_RESET; 7624 sense->es_key = KEY_ABORTED_COMMAND; 7625 break; 7626 7627 case SATA_PKT_ABORTED: 7628 scsipkt->pkt_reason = CMD_ABORTED; 7629 scsipkt->pkt_statistics |= STAT_ABORTED; 7630 sense->es_key = KEY_ABORTED_COMMAND; 7631 break; 7632 7633 case SATA_PKT_RESET: 7634 scsipkt->pkt_reason = CMD_RESET; 7635 scsipkt->pkt_statistics |= STAT_DEV_RESET; 7636 sense->es_key = KEY_ABORTED_COMMAND; 7637 break; 7638 7639 default: 7640 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 7641 "sata_txlt_rw_completion: " 7642 "invalid packet completion reason")); 7643 scsipkt->pkt_reason = CMD_TRAN_ERR; 7644 break; 7645 } 7646 } 7647 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 7648 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 7649 7650 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 7651 /* scsi callback required */ 7652 scsi_hba_pkt_comp(scsipkt); 7653 } 7654 7655 7656 /* 7657 * Translate completion status of non-data commands (i.e. commands returning 7658 * no data). 7659 * pkt completion_reason is checked to determine the completion status. 7660 * Do scsi callback if necessary (FLAG_NOINTR == 0) 7661 * 7662 * Note: this function may be called also for synchronously executed 7663 * commands. 7664 * This function may be used only if scsi_pkt is non-NULL. 7665 */ 7666 7667 static void 7668 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt) 7669 { 7670 sata_pkt_txlate_t *spx = 7671 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7672 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7673 7674 sata_set_arq_data(sata_pkt); 7675 7676 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 7677 /* scsi callback required */ 7678 scsi_hba_pkt_comp(scsipkt); 7679 } 7680 7681 /* 7682 * Completion handler for ATA Pass Through command 7683 */ 7684 static void 7685 sata_txlt_apt_completion(sata_pkt_t *sata_pkt) 7686 { 7687 sata_pkt_txlate_t *spx = 7688 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7689 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 7690 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7691 struct buf *bp; 7692 uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0; 7693 7694 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 7695 /* Normal completion */ 7696 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7697 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 7698 scsipkt->pkt_reason = CMD_CMPLT; 7699 *scsipkt->pkt_scbp = STATUS_GOOD; 7700 7701 /* 7702 * If the command has CK_COND set 7703 */ 7704 if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) { 7705 *scsipkt->pkt_scbp = STATUS_CHECK; 7706 sata_fill_ata_return_desc(sata_pkt, 7707 KEY_RECOVERABLE_ERROR, 7708 SD_SCSI_ASC_APT_INFO_AVAIL, 0x1d); 7709 } 7710 7711 if (spx->txlt_tmp_buf != NULL) { 7712 /* Temporary buffer was used */ 7713 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 7714 if (bp->b_flags & B_READ) { 7715 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 7716 bp->b_bcount); 7717 } 7718 } 7719 } else { 7720 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7721 STATE_SENT_CMD | STATE_GOT_STATUS; 7722 scsipkt->pkt_reason = CMD_INCOMPLETE; 7723 *scsipkt->pkt_scbp = STATUS_CHECK; 7724 7725 /* 7726 * If DF or ERR was set, the HBA should have copied out the 7727 * status and error registers to the satacmd structure. 7728 */ 7729 if (scmd->satacmd_status_reg & SATA_STATUS_DF) { 7730 sense_key = KEY_HARDWARE_ERROR; 7731 addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE; 7732 addl_sense_qual = 0; 7733 } else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) { 7734 if (scmd->satacmd_error_reg & SATA_ERROR_NM) { 7735 sense_key = KEY_NOT_READY; 7736 addl_sense_code = 7737 SD_SCSI_ASC_MEDIUM_NOT_PRESENT; 7738 addl_sense_qual = 0; 7739 } else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) { 7740 sense_key = KEY_MEDIUM_ERROR; 7741 addl_sense_code = SD_SCSI_ASC_UNREC_READ_ERR; 7742 addl_sense_qual = 0; 7743 } else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) { 7744 sense_key = KEY_DATA_PROTECT; 7745 addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED; 7746 addl_sense_qual = 0; 7747 } else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) { 7748 sense_key = KEY_ILLEGAL_REQUEST; 7749 addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE; 7750 addl_sense_qual = 0; 7751 } else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) { 7752 sense_key = KEY_ABORTED_COMMAND; 7753 addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE; 7754 addl_sense_qual = 0; 7755 } else if (scmd->satacmd_error_reg & SATA_ERROR_MC) { 7756 sense_key = KEY_UNIT_ATTENTION; 7757 addl_sense_code = 7758 SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED; 7759 addl_sense_qual = 0; 7760 } else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) { 7761 sense_key = KEY_UNIT_ATTENTION; 7762 addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ; 7763 addl_sense_qual = 0; 7764 } else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) { 7765 sense_key = KEY_ABORTED_COMMAND; 7766 addl_sense_code = 7767 SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR; 7768 addl_sense_qual = 0; 7769 } 7770 } 7771 7772 sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code, 7773 addl_sense_qual); 7774 } 7775 7776 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 7777 /* scsi callback required */ 7778 scsi_hba_pkt_comp(scsipkt); 7779 } 7780 7781 /* 7782 * Completion handler for unmap translation command 7783 */ 7784 static void 7785 sata_txlt_unmap_completion(sata_pkt_t *sata_pkt) 7786 { 7787 sata_pkt_txlate_t *spx = 7788 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7789 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 7790 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7791 struct buf *bp; 7792 uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0; 7793 7794 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 7795 /* Normal completion */ 7796 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7797 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 7798 scsipkt->pkt_reason = CMD_CMPLT; 7799 *scsipkt->pkt_scbp = STATUS_GOOD; 7800 7801 if (spx->txlt_tmp_buf != NULL) { 7802 /* Temporary buffer was used */ 7803 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 7804 if (bp->b_flags & B_READ) { 7805 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 7806 bp->b_bcount); 7807 } 7808 } 7809 } else { 7810 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7811 STATE_SENT_CMD | STATE_GOT_STATUS; 7812 scsipkt->pkt_reason = CMD_INCOMPLETE; 7813 *scsipkt->pkt_scbp = STATUS_CHECK; 7814 7815 /* 7816 * If DF or ERR was set, the HBA should have copied out the 7817 * status and error registers to the satacmd structure. 7818 */ 7819 if (scmd->satacmd_status_reg & SATA_STATUS_DF) { 7820 sense_key = KEY_HARDWARE_ERROR; 7821 addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE; 7822 addl_sense_qual = 0; 7823 } else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) { 7824 if (scmd->satacmd_error_reg & SATA_ERROR_NM) { 7825 sense_key = KEY_NOT_READY; 7826 addl_sense_code = 7827 SD_SCSI_ASC_MEDIUM_NOT_PRESENT; 7828 addl_sense_qual = 0; 7829 } else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) { 7830 sense_key = KEY_MEDIUM_ERROR; 7831 addl_sense_code = SD_SCSI_ASC_WRITE_ERR; 7832 addl_sense_qual = 0; 7833 } else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) { 7834 sense_key = KEY_DATA_PROTECT; 7835 addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED; 7836 addl_sense_qual = 0; 7837 } else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) { 7838 sense_key = KEY_ILLEGAL_REQUEST; 7839 addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE; 7840 addl_sense_qual = 0; 7841 } else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) { 7842 sense_key = KEY_ABORTED_COMMAND; 7843 addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE; 7844 addl_sense_qual = 0; 7845 } else if (scmd->satacmd_error_reg & SATA_ERROR_MC) { 7846 sense_key = KEY_UNIT_ATTENTION; 7847 addl_sense_code = 7848 SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED; 7849 addl_sense_qual = 0; 7850 } else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) { 7851 sense_key = KEY_UNIT_ATTENTION; 7852 addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ; 7853 addl_sense_qual = 0; 7854 } else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) { 7855 sense_key = KEY_ABORTED_COMMAND; 7856 addl_sense_code = 7857 SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR; 7858 addl_sense_qual = 0; 7859 } 7860 } 7861 7862 sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code, 7863 addl_sense_qual); 7864 } 7865 7866 sata_free_local_buffer(spx); 7867 7868 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 7869 /* scsi callback required */ 7870 scsi_hba_pkt_comp(scsipkt); 7871 } 7872 7873 /* 7874 * 7875 */ 7876 static void 7877 sata_fill_ata_return_desc(sata_pkt_t *sata_pkt, uint8_t sense_key, 7878 uint8_t addl_sense_code, uint8_t addl_sense_qual) 7879 { 7880 sata_pkt_txlate_t *spx = 7881 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7882 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 7883 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7884 struct sata_apt_sense_data *apt_sd = 7885 (struct sata_apt_sense_data *)scsipkt->pkt_scbp; 7886 struct scsi_descr_sense_hdr *sds = &(apt_sd->apt_sd_hdr); 7887 struct scsi_ata_status_ret_sense_descr *ata_ret_desc = 7888 &(apt_sd->apt_sd_sense); 7889 int extend = 0; 7890 7891 if ((scsipkt->pkt_cdbp[0] == SPC3_CMD_ATA_COMMAND_PASS_THROUGH16) && 7892 (scsipkt->pkt_cdbp[2] & SATL_APT_BM_EXTEND)) 7893 extend = 1; 7894 7895 scsipkt->pkt_state |= STATE_ARQ_DONE; 7896 7897 /* update the residual count */ 7898 *(uchar_t *)&apt_sd->apt_status = STATUS_CHECK; 7899 *(uchar_t *)&apt_sd->apt_rqpkt_status = STATUS_GOOD; 7900 apt_sd->apt_rqpkt_reason = CMD_CMPLT; 7901 apt_sd->apt_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7902 STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS; 7903 apt_sd->apt_rqpkt_resid = scsipkt->pkt_scblen - 7904 sizeof (struct sata_apt_sense_data); 7905 7906 /* 7907 * Fill in the Descriptor sense header 7908 */ 7909 bzero(sds, sizeof (struct scsi_descr_sense_hdr)); 7910 sds->ds_code = CODE_FMT_DESCR_CURRENT; 7911 sds->ds_class = CLASS_EXTENDED_SENSE; 7912 sds->ds_key = sense_key & 0xf; 7913 sds->ds_add_code = addl_sense_code; 7914 sds->ds_qual_code = addl_sense_qual; 7915 sds->ds_addl_sense_length = 7916 sizeof (struct scsi_ata_status_ret_sense_descr); 7917 7918 /* 7919 * Fill in the ATA Return descriptor sense data 7920 */ 7921 bzero(ata_ret_desc, sizeof (struct scsi_ata_status_ret_sense_descr)); 7922 ata_ret_desc->ars_descr_type = DESCR_ATA_STATUS_RETURN; 7923 ata_ret_desc->ars_addl_length = 0xc; 7924 ata_ret_desc->ars_error = scmd->satacmd_error_reg; 7925 ata_ret_desc->ars_sec_count_lsb = scmd->satacmd_sec_count_lsb; 7926 ata_ret_desc->ars_lba_low_lsb = scmd->satacmd_lba_low_lsb; 7927 ata_ret_desc->ars_lba_mid_lsb = scmd->satacmd_lba_mid_lsb; 7928 ata_ret_desc->ars_lba_high_lsb = scmd->satacmd_lba_high_lsb; 7929 ata_ret_desc->ars_device = scmd->satacmd_device_reg; 7930 ata_ret_desc->ars_status = scmd->satacmd_status_reg; 7931 7932 if (extend == 1) { 7933 ata_ret_desc->ars_extend = 1; 7934 ata_ret_desc->ars_sec_count_msb = scmd->satacmd_sec_count_msb; 7935 ata_ret_desc->ars_lba_low_msb = scmd->satacmd_lba_low_msb; 7936 ata_ret_desc->ars_lba_mid_msb = scmd->satacmd_lba_mid_msb; 7937 ata_ret_desc->ars_lba_high_msb = scmd->satacmd_lba_high_msb; 7938 } else { 7939 ata_ret_desc->ars_extend = 0; 7940 ata_ret_desc->ars_sec_count_msb = 0; 7941 ata_ret_desc->ars_lba_low_msb = 0; 7942 ata_ret_desc->ars_lba_mid_msb = 0; 7943 ata_ret_desc->ars_lba_high_msb = 0; 7944 } 7945 } 7946 7947 static void 7948 sata_set_arq_data(sata_pkt_t *sata_pkt) 7949 { 7950 sata_pkt_txlate_t *spx = 7951 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7952 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7953 struct scsi_extended_sense *sense; 7954 7955 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7956 STATE_SENT_CMD | STATE_GOT_STATUS; 7957 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 7958 /* Normal completion */ 7959 scsipkt->pkt_reason = CMD_CMPLT; 7960 *scsipkt->pkt_scbp = STATUS_GOOD; 7961 } else { 7962 /* Something went wrong */ 7963 scsipkt->pkt_reason = CMD_INCOMPLETE; 7964 *scsipkt->pkt_scbp = STATUS_CHECK; 7965 sense = sata_arq_sense(spx); 7966 switch (sata_pkt->satapkt_reason) { 7967 case SATA_PKT_PORT_ERROR: 7968 /* 7969 * We have no device data. Assume no data transfered. 7970 */ 7971 sense->es_key = KEY_HARDWARE_ERROR; 7972 break; 7973 7974 case SATA_PKT_DEV_ERROR: 7975 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 7976 SATA_STATUS_ERR) { 7977 /* 7978 * determine dev error reason from error 7979 * reg content 7980 */ 7981 sata_decode_device_error(spx, sense); 7982 break; 7983 } 7984 /* No extended sense key - no info available */ 7985 break; 7986 7987 case SATA_PKT_TIMEOUT: 7988 scsipkt->pkt_reason = CMD_TIMEOUT; 7989 scsipkt->pkt_statistics |= 7990 STAT_TIMEOUT | STAT_DEV_RESET; 7991 /* No extended sense key ? */ 7992 break; 7993 7994 case SATA_PKT_ABORTED: 7995 scsipkt->pkt_reason = CMD_ABORTED; 7996 scsipkt->pkt_statistics |= STAT_ABORTED; 7997 /* No extended sense key ? */ 7998 break; 7999 8000 case SATA_PKT_RESET: 8001 /* pkt aborted by an explicit reset from a host */ 8002 scsipkt->pkt_reason = CMD_RESET; 8003 scsipkt->pkt_statistics |= STAT_DEV_RESET; 8004 break; 8005 8006 default: 8007 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 8008 "sata_txlt_nodata_cmd_completion: " 8009 "invalid packet completion reason %d", 8010 sata_pkt->satapkt_reason)); 8011 scsipkt->pkt_reason = CMD_TRAN_ERR; 8012 break; 8013 } 8014 8015 } 8016 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 8017 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 8018 } 8019 8020 8021 /* 8022 * Build Mode sense R/W recovery page 8023 * NOT IMPLEMENTED 8024 */ 8025 8026 static int 8027 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 8028 { 8029 #ifndef __lock_lint 8030 _NOTE(ARGUNUSED(sdinfo)) 8031 _NOTE(ARGUNUSED(pcntrl)) 8032 _NOTE(ARGUNUSED(buf)) 8033 #endif 8034 return (0); 8035 } 8036 8037 /* 8038 * Build Mode sense caching page - scsi-3 implementation. 8039 * Page length distinguishes previous format from scsi-3 format. 8040 * buf must have space for 0x12 bytes. 8041 * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable. 8042 * 8043 */ 8044 static int 8045 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 8046 { 8047 struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf; 8048 sata_id_t *sata_id = &sdinfo->satadrv_id; 8049 8050 /* 8051 * Most of the fields are set to 0, being not supported and/or disabled 8052 */ 8053 bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3); 8054 8055 /* Saved paramters not supported */ 8056 if (pcntrl == 3) 8057 return (0); 8058 if (pcntrl == 0 || pcntrl == 2) { 8059 /* 8060 * For now treat current and default parameters as same 8061 * That may have to change, if target driver will complain 8062 */ 8063 page->mode_page.code = MODEPAGE_CACHING; /* PS = 0 */ 8064 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 8065 8066 if (SATA_READ_AHEAD_SUPPORTED(*sata_id) && 8067 !SATA_READ_AHEAD_ENABLED(*sata_id)) { 8068 page->dra = 1; /* Read Ahead disabled */ 8069 page->rcd = 1; /* Read Cache disabled */ 8070 } 8071 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) && 8072 SATA_WRITE_CACHE_ENABLED(*sata_id)) 8073 page->wce = 1; /* Write Cache enabled */ 8074 } else { 8075 /* Changeable parameters */ 8076 page->mode_page.code = MODEPAGE_CACHING; 8077 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 8078 if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) { 8079 page->dra = 1; 8080 page->rcd = 1; 8081 } 8082 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) 8083 page->wce = 1; 8084 } 8085 return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 8086 sizeof (struct mode_page)); 8087 } 8088 8089 /* 8090 * Build Mode sense exception cntrl page 8091 */ 8092 static int 8093 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 8094 { 8095 struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf; 8096 sata_id_t *sata_id = &sdinfo->satadrv_id; 8097 8098 /* 8099 * Most of the fields are set to 0, being not supported and/or disabled 8100 */ 8101 bzero(buf, PAGELENGTH_INFO_EXCPT); 8102 8103 page->mode_page.code = MODEPAGE_INFO_EXCPT; 8104 page->mode_page.length = PAGELENGTH_INFO_EXCPT; 8105 8106 /* Indicate that this is page is saveable */ 8107 page->mode_page.ps = 1; 8108 8109 /* 8110 * We will return the same data for default, current and saved page. 8111 * The only changeable bit is dexcpt and that bit is required 8112 * by the ATA specification to be preserved across power cycles. 8113 */ 8114 if (pcntrl != 1) { 8115 page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED); 8116 page->mrie = MRIE_ONLY_ON_REQUEST; 8117 } 8118 else 8119 page->dexcpt = 1; /* Only changeable parameter */ 8120 8121 return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)); 8122 } 8123 8124 8125 static int 8126 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 8127 { 8128 struct mode_acoustic_management *page = 8129 (struct mode_acoustic_management *)buf; 8130 sata_id_t *sata_id = &sdinfo->satadrv_id; 8131 8132 /* 8133 * Most of the fields are set to 0, being not supported and/or disabled 8134 */ 8135 bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT); 8136 8137 switch (pcntrl) { 8138 case P_CNTRL_DEFAULT: 8139 /* default paramters not supported */ 8140 return (0); 8141 8142 case P_CNTRL_CURRENT: 8143 case P_CNTRL_SAVED: 8144 /* Saved and current are supported and are identical */ 8145 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG; 8146 page->mode_page.length = 8147 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT; 8148 page->mode_page.ps = 1; 8149 8150 /* Word 83 indicates if feature is supported */ 8151 /* If feature is not supported */ 8152 if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) { 8153 page->acoustic_manag_enable = 8154 ACOUSTIC_DISABLED; 8155 } else { 8156 page->acoustic_manag_enable = 8157 ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT) 8158 != 0); 8159 /* Word 94 inidicates the value */ 8160 #ifdef _LITTLE_ENDIAN 8161 page->acoustic_manag_level = 8162 (uchar_t)sata_id->ai_acoustic; 8163 page->vendor_recommended_value = 8164 sata_id->ai_acoustic >> 8; 8165 #else 8166 page->acoustic_manag_level = 8167 sata_id->ai_acoustic >> 8; 8168 page->vendor_recommended_value = 8169 (uchar_t)sata_id->ai_acoustic; 8170 #endif 8171 } 8172 break; 8173 8174 case P_CNTRL_CHANGEABLE: 8175 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG; 8176 page->mode_page.length = 8177 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT; 8178 page->mode_page.ps = 1; 8179 8180 /* Word 83 indicates if the feature is supported */ 8181 if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) { 8182 page->acoustic_manag_enable = 8183 ACOUSTIC_ENABLED; 8184 page->acoustic_manag_level = 0xff; 8185 } 8186 break; 8187 } 8188 return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 8189 sizeof (struct mode_page)); 8190 } 8191 8192 8193 /* 8194 * Build Mode sense power condition page. 8195 */ 8196 static int 8197 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 8198 { 8199 struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf; 8200 sata_id_t *sata_id = &sdinfo->satadrv_id; 8201 8202 /* 8203 * Most of the fields are set to 0, being not supported and/or disabled 8204 * power condition page length was 0x0a 8205 */ 8206 bzero(buf, sizeof (struct mode_info_power_cond)); 8207 8208 if (pcntrl == P_CNTRL_DEFAULT) { 8209 /* default paramters not supported */ 8210 return (0); 8211 } 8212 8213 page->mode_page.code = MODEPAGE_POWER_COND; 8214 page->mode_page.length = sizeof (struct mode_info_power_cond); 8215 8216 if (sata_id->ai_cap & SATA_STANDBYTIMER) { 8217 page->standby = 1; 8218 bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer, 8219 sizeof (uchar_t) * 4); 8220 } 8221 8222 return (sizeof (struct mode_info_power_cond)); 8223 } 8224 8225 /* 8226 * Process mode select caching page 8 (scsi3 format only). 8227 * Read Ahead (same as read cache) and Write Cache may be turned on and off 8228 * if these features are supported by the device. If these features are not 8229 * supported, the command will be terminated with STATUS_CHECK. 8230 * This function fails only if the SET FEATURE command sent to 8231 * the device fails. The page format is not verified, assuming that the 8232 * target driver operates correctly - if parameters length is too short, 8233 * we just drop the page. 8234 * Two command may be sent if both Read Cache/Read Ahead and Write Cache 8235 * setting have to be changed. 8236 * SET FEATURE command is executed synchronously, i.e. we wait here until 8237 * it is completed, regardless of the scsi pkt directives. 8238 * 8239 * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e. 8240 * changing DRA will change RCD. 8241 * 8242 * More than one SATA command may be executed to perform operations specified 8243 * by mode select pages. The first error terminates further execution. 8244 * Operations performed successully are not backed-up in such case. 8245 * 8246 * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 8247 * If operation resulted in changing device setup, dmod flag should be set to 8248 * one (1). If parameters were not changed, dmod flag should be set to 0. 8249 * Upon return, if operation required sending command to the device, the rval 8250 * should be set to the value returned by sata_hba_start. If operation 8251 * did not require device access, rval should be set to TRAN_ACCEPT. 8252 * The pagelen should be set to the length of the page. 8253 * 8254 * This function has to be called with a port mutex held. 8255 * 8256 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 8257 */ 8258 int 8259 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page, 8260 int parmlen, int *pagelen, int *rval, int *dmod) 8261 { 8262 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 8263 sata_drive_info_t *sdinfo; 8264 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 8265 sata_id_t *sata_id; 8266 struct scsi_extended_sense *sense; 8267 int wce, dra; /* Current settings */ 8268 8269 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 8270 &spx->txlt_sata_pkt->satapkt_device); 8271 sata_id = &sdinfo->satadrv_id; 8272 *dmod = 0; 8273 8274 /* Verify parameters length. If too short, drop it */ 8275 if ((PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 8276 sizeof (struct mode_page)) > parmlen) { 8277 *scsipkt->pkt_scbp = STATUS_CHECK; 8278 sense = sata_arq_sense(spx); 8279 sense->es_key = KEY_ILLEGAL_REQUEST; 8280 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 8281 *pagelen = parmlen; 8282 *rval = TRAN_ACCEPT; 8283 return (SATA_FAILURE); 8284 } 8285 8286 *pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page); 8287 8288 /* Current setting of Read Ahead (and Read Cache) */ 8289 if (SATA_READ_AHEAD_ENABLED(*sata_id)) 8290 dra = 0; /* 0 == not disabled */ 8291 else 8292 dra = 1; 8293 /* Current setting of Write Cache */ 8294 if (SATA_WRITE_CACHE_ENABLED(*sata_id)) 8295 wce = 1; 8296 else 8297 wce = 0; 8298 8299 if (page->dra == dra && page->wce == wce && page->rcd == dra) { 8300 /* nothing to do */ 8301 *rval = TRAN_ACCEPT; 8302 return (SATA_SUCCESS); 8303 } 8304 8305 /* 8306 * Need to flip some setting 8307 * Set-up Internal SET FEATURES command(s) 8308 */ 8309 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 8310 scmd->satacmd_addr_type = 0; 8311 scmd->satacmd_device_reg = 0; 8312 scmd->satacmd_status_reg = 0; 8313 scmd->satacmd_error_reg = 0; 8314 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 8315 if (page->dra != dra || page->rcd != dra) { 8316 if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) { 8317 /* Need to flip read ahead setting */ 8318 if (dra == 0) 8319 /* Disable read ahead / read cache */ 8320 scmd->satacmd_features_reg = 8321 SATAC_SF_DISABLE_READ_AHEAD; 8322 else 8323 /* Enable read ahead / read cache */ 8324 scmd->satacmd_features_reg = 8325 SATAC_SF_ENABLE_READ_AHEAD; 8326 8327 /* Transfer command to HBA */ 8328 if (sata_hba_start(spx, rval) != 0) 8329 /* 8330 * Pkt not accepted for execution. 8331 */ 8332 return (SATA_FAILURE); 8333 8334 *dmod = 1; 8335 8336 /* Now process return */ 8337 if (spx->txlt_sata_pkt->satapkt_reason != 8338 SATA_PKT_COMPLETED) { 8339 goto failure; /* Terminate */ 8340 } 8341 } else { 8342 *scsipkt->pkt_scbp = STATUS_CHECK; 8343 sense = sata_arq_sense(spx); 8344 sense->es_key = KEY_ILLEGAL_REQUEST; 8345 sense->es_add_code = 8346 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 8347 *pagelen = parmlen; 8348 *rval = TRAN_ACCEPT; 8349 return (SATA_FAILURE); 8350 } 8351 } 8352 8353 /* Note that the packet is not removed, so it could be re-used */ 8354 if (page->wce != wce) { 8355 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) { 8356 /* Need to flip Write Cache setting */ 8357 if (page->wce == 1) 8358 /* Enable write cache */ 8359 scmd->satacmd_features_reg = 8360 SATAC_SF_ENABLE_WRITE_CACHE; 8361 else 8362 /* Disable write cache */ 8363 scmd->satacmd_features_reg = 8364 SATAC_SF_DISABLE_WRITE_CACHE; 8365 8366 /* Transfer command to HBA */ 8367 if (sata_hba_start(spx, rval) != 0) 8368 /* 8369 * Pkt not accepted for execution. 8370 */ 8371 return (SATA_FAILURE); 8372 8373 *dmod = 1; 8374 8375 /* Now process return */ 8376 if (spx->txlt_sata_pkt->satapkt_reason != 8377 SATA_PKT_COMPLETED) { 8378 goto failure; 8379 } 8380 } else { 8381 *scsipkt->pkt_scbp = STATUS_CHECK; 8382 sense = sata_arq_sense(spx); 8383 sense->es_key = KEY_ILLEGAL_REQUEST; 8384 sense->es_add_code = 8385 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 8386 *pagelen = parmlen; 8387 *rval = TRAN_ACCEPT; 8388 return (SATA_FAILURE); 8389 } 8390 } 8391 return (SATA_SUCCESS); 8392 8393 failure: 8394 sata_xlate_errors(spx); 8395 8396 return (SATA_FAILURE); 8397 } 8398 8399 /* 8400 * Process mode select informational exceptions control page 0x1c 8401 * 8402 * The only changeable bit is dexcpt (disable exceptions). 8403 * MRIE (method of reporting informational exceptions) must be 8404 * "only on request". 8405 * This page applies to informational exceptions that report 8406 * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh 8407 * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_). 8408 * Informational exception conditions occur as the result of background scan 8409 * errors, background self-test errors, or vendor specific events within a 8410 * logical unit. An informational exception condition may occur asynchronous 8411 * to any commands. 8412 * 8413 * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 8414 * If operation resulted in changing device setup, dmod flag should be set to 8415 * one (1). If parameters were not changed, dmod flag should be set to 0. 8416 * Upon return, if operation required sending command to the device, the rval 8417 * should be set to the value returned by sata_hba_start. If operation 8418 * did not require device access, rval should be set to TRAN_ACCEPT. 8419 * The pagelen should be set to the length of the page. 8420 * 8421 * This function has to be called with a port mutex held. 8422 * 8423 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 8424 * 8425 * Cannot be called in the interrupt context. 8426 */ 8427 static int 8428 sata_mode_select_page_1c( 8429 sata_pkt_txlate_t *spx, 8430 struct mode_info_excpt_page *page, 8431 int parmlen, 8432 int *pagelen, 8433 int *rval, 8434 int *dmod) 8435 { 8436 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 8437 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 8438 sata_drive_info_t *sdinfo; 8439 sata_id_t *sata_id; 8440 struct scsi_extended_sense *sense; 8441 8442 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 8443 &spx->txlt_sata_pkt->satapkt_device); 8444 sata_id = &sdinfo->satadrv_id; 8445 8446 *dmod = 0; 8447 8448 /* Verify parameters length. If too short, drop it */ 8449 if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) > parmlen) || 8450 page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) { 8451 *scsipkt->pkt_scbp = STATUS_CHECK; 8452 sense = sata_arq_sense(spx); 8453 sense->es_key = KEY_ILLEGAL_REQUEST; 8454 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 8455 *pagelen = parmlen; 8456 *rval = TRAN_ACCEPT; 8457 return (SATA_FAILURE); 8458 } 8459 8460 *pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page); 8461 8462 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 8463 *scsipkt->pkt_scbp = STATUS_CHECK; 8464 sense = sata_arq_sense(spx); 8465 sense->es_key = KEY_ILLEGAL_REQUEST; 8466 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 8467 *pagelen = parmlen; 8468 *rval = TRAN_ACCEPT; 8469 return (SATA_FAILURE); 8470 } 8471 8472 /* If already in the state requested, we are done */ 8473 if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 8474 /* nothing to do */ 8475 *rval = TRAN_ACCEPT; 8476 return (SATA_SUCCESS); 8477 } 8478 8479 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 8480 8481 /* Build SMART_ENABLE or SMART_DISABLE command */ 8482 scmd->satacmd_addr_type = 0; /* N/A */ 8483 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 8484 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 8485 scmd->satacmd_features_reg = page->dexcpt ? 8486 SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS; 8487 scmd->satacmd_device_reg = 0; /* Always device 0 */ 8488 scmd->satacmd_cmd_reg = SATAC_SMART; 8489 8490 /* Transfer command to HBA */ 8491 if (sata_hba_start(spx, rval) != 0) 8492 /* 8493 * Pkt not accepted for execution. 8494 */ 8495 return (SATA_FAILURE); 8496 8497 *dmod = 1; /* At least may have been modified */ 8498 8499 /* Now process return */ 8500 if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) 8501 return (SATA_SUCCESS); 8502 8503 /* Packet did not complete successfully */ 8504 sata_xlate_errors(spx); 8505 8506 return (SATA_FAILURE); 8507 } 8508 8509 /* 8510 * Process mode select acoustic management control page 0x30 8511 * 8512 * 8513 * This function has to be called with a port mutex held. 8514 * 8515 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 8516 * 8517 * Cannot be called in the interrupt context. 8518 */ 8519 int 8520 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct 8521 mode_acoustic_management *page, int parmlen, int *pagelen, 8522 int *rval, int *dmod) 8523 { 8524 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 8525 sata_drive_info_t *sdinfo; 8526 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 8527 sata_id_t *sata_id; 8528 struct scsi_extended_sense *sense; 8529 8530 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 8531 &spx->txlt_sata_pkt->satapkt_device); 8532 sata_id = &sdinfo->satadrv_id; 8533 *dmod = 0; 8534 8535 /* If parmlen is too short or the feature is not supported, drop it */ 8536 if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 8537 sizeof (struct mode_page)) > parmlen) || 8538 (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) { 8539 *scsipkt->pkt_scbp = STATUS_CHECK; 8540 sense = sata_arq_sense(spx); 8541 sense->es_key = KEY_ILLEGAL_REQUEST; 8542 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 8543 *pagelen = parmlen; 8544 *rval = TRAN_ACCEPT; 8545 return (SATA_FAILURE); 8546 } 8547 8548 *pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 8549 sizeof (struct mode_page); 8550 8551 /* 8552 * We can enable and disable acoustice management and 8553 * set the acoustic management level. 8554 */ 8555 8556 /* 8557 * Set-up Internal SET FEATURES command(s) 8558 */ 8559 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 8560 scmd->satacmd_addr_type = 0; 8561 scmd->satacmd_device_reg = 0; 8562 scmd->satacmd_status_reg = 0; 8563 scmd->satacmd_error_reg = 0; 8564 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 8565 if (page->acoustic_manag_enable) { 8566 scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC; 8567 scmd->satacmd_sec_count_lsb = page->acoustic_manag_level; 8568 } else { /* disabling acoustic management */ 8569 scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC; 8570 } 8571 8572 /* Transfer command to HBA */ 8573 if (sata_hba_start(spx, rval) != 0) 8574 /* 8575 * Pkt not accepted for execution. 8576 */ 8577 return (SATA_FAILURE); 8578 8579 /* Now process return */ 8580 if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) { 8581 sata_xlate_errors(spx); 8582 return (SATA_FAILURE); 8583 } 8584 8585 *dmod = 1; 8586 8587 return (SATA_SUCCESS); 8588 } 8589 8590 /* 8591 * Process mode select power condition page 0x1a 8592 * 8593 * This function has to be called with a port mutex held. 8594 * 8595 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 8596 * 8597 * Cannot be called in the interrupt context. 8598 */ 8599 int 8600 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct 8601 mode_info_power_cond *page, int parmlen, int *pagelen, 8602 int *rval, int *dmod) 8603 { 8604 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 8605 sata_drive_info_t *sdinfo; 8606 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 8607 sata_id_t *sata_id; 8608 struct scsi_extended_sense *sense; 8609 uint8_t ata_count; 8610 int i, len; 8611 8612 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 8613 &spx->txlt_sata_pkt->satapkt_device); 8614 sata_id = &sdinfo->satadrv_id; 8615 *dmod = 0; 8616 8617 len = sizeof (struct mode_info_power_cond); 8618 len += sizeof (struct mode_page); 8619 8620 /* If parmlen is too short or the feature is not supported, drop it */ 8621 if ((len < parmlen) || (page->idle == 1) || 8622 (!(sata_id->ai_cap & SATA_STANDBYTIMER) && page->standby == 1)) { 8623 *scsipkt->pkt_scbp = STATUS_CHECK; 8624 sense = sata_arq_sense(spx); 8625 sense->es_key = KEY_ILLEGAL_REQUEST; 8626 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 8627 *pagelen = parmlen; 8628 *rval = TRAN_ACCEPT; 8629 return (SATA_FAILURE); 8630 } 8631 8632 *pagelen = len; 8633 8634 /* 8635 * Set-up Internal STANDBY command(s) 8636 */ 8637 if (page->standby == 0) 8638 goto out; 8639 8640 ata_count = sata_get_standby_timer(page->standby_cond_timer); 8641 8642 scmd->satacmd_addr_type = 0; 8643 scmd->satacmd_sec_count_lsb = ata_count; 8644 scmd->satacmd_lba_low_lsb = 0; 8645 scmd->satacmd_lba_mid_lsb = 0; 8646 scmd->satacmd_lba_high_lsb = 0; 8647 scmd->satacmd_features_reg = 0; 8648 scmd->satacmd_device_reg = 0; 8649 scmd->satacmd_status_reg = 0; 8650 scmd->satacmd_cmd_reg = SATAC_STANDBY; 8651 scmd->satacmd_flags.sata_special_regs = B_TRUE; 8652 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 8653 8654 /* Transfer command to HBA */ 8655 if (sata_hba_start(spx, rval) != 0) { 8656 return (SATA_FAILURE); 8657 } else { 8658 if ((scmd->satacmd_error_reg != 0) || 8659 (spx->txlt_sata_pkt->satapkt_reason != 8660 SATA_PKT_COMPLETED)) { 8661 sata_xlate_errors(spx); 8662 return (SATA_FAILURE); 8663 } 8664 } 8665 8666 for (i = 0; i < 4; i++) { 8667 sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i]; 8668 } 8669 out: 8670 *dmod = 1; 8671 return (SATA_SUCCESS); 8672 } 8673 8674 /* 8675 * sata_build_lsense_page0() is used to create the 8676 * SCSI LOG SENSE page 0 (supported log pages) 8677 * 8678 * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e 8679 * (supported log pages, self-test results, informational exceptions 8680 * Sun vendor specific ATA SMART data, and start stop cycle counter). 8681 * 8682 * Takes a sata_drive_info t * and the address of a buffer 8683 * in which to create the page information. 8684 * 8685 * Returns the number of bytes valid in the buffer. 8686 */ 8687 static int 8688 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf) 8689 { 8690 struct log_parameter *lpp = (struct log_parameter *)buf; 8691 uint8_t *page_ptr = (uint8_t *)lpp->param_values; 8692 int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */ 8693 sata_id_t *sata_id = &sdinfo->satadrv_id; 8694 8695 lpp->param_code[0] = 0; 8696 lpp->param_code[1] = 0; 8697 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 8698 *page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES; 8699 8700 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 8701 if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) { 8702 *page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS; 8703 ++num_pages_supported; 8704 } 8705 *page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS; 8706 ++num_pages_supported; 8707 *page_ptr++ = PAGE_CODE_SMART_READ_DATA; 8708 ++num_pages_supported; 8709 *page_ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER; 8710 ++num_pages_supported; 8711 } 8712 8713 lpp->param_len = num_pages_supported; 8714 8715 return ((&lpp->param_values[0] - (uint8_t *)lpp) + 8716 num_pages_supported); 8717 } 8718 8719 /* 8720 * sata_build_lsense_page_10() is used to create the 8721 * SCSI LOG SENSE page 0x10 (self-test results) 8722 * 8723 * Takes a sata_drive_info t * and the address of a buffer 8724 * in which to create the page information as well as a sata_hba_inst_t *. 8725 * 8726 * Returns the number of bytes valid in the buffer. 8727 * 8728 * Note: Self test and SMART data is accessible in device log pages. 8729 * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors 8730 * of data can be transferred by a single command), or by the General Purpose 8731 * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors 8732 * - approximately 33MB - can be transferred by a single command. 8733 * The SCT Command response (either error or command) is the same for both 8734 * the SMART and GPL methods of issuing commands. 8735 * This function uses READ LOG EXT command when drive supports LBA48, and 8736 * SMART READ command otherwise. 8737 * 8738 * Since above commands are executed in a synchronous mode, this function 8739 * should not be called in an interrupt context. 8740 */ 8741 static int 8742 sata_build_lsense_page_10( 8743 sata_drive_info_t *sdinfo, 8744 uint8_t *buf, 8745 sata_hba_inst_t *sata_hba_inst) 8746 { 8747 struct log_parameter *lpp = (struct log_parameter *)buf; 8748 int rval; 8749 8750 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 8751 struct smart_ext_selftest_log *ext_selftest_log; 8752 8753 ext_selftest_log = kmem_zalloc( 8754 sizeof (struct smart_ext_selftest_log), KM_SLEEP); 8755 8756 rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo, 8757 ext_selftest_log, 0); 8758 if (rval == 0) { 8759 int index, start_index; 8760 struct smart_ext_selftest_log_entry *entry; 8761 static const struct smart_ext_selftest_log_entry empty = 8762 {0}; 8763 uint16_t block_num; 8764 int count; 8765 boolean_t only_one_block = B_FALSE; 8766 8767 index = ext_selftest_log-> 8768 smart_ext_selftest_log_index[0]; 8769 index |= ext_selftest_log-> 8770 smart_ext_selftest_log_index[1] << 8; 8771 if (index == 0) 8772 goto out; 8773 8774 --index; /* Correct for 0 origin */ 8775 start_index = index; /* remember where we started */ 8776 block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 8777 if (block_num != 0) { 8778 rval = sata_ext_smart_selftest_read_log( 8779 sata_hba_inst, sdinfo, ext_selftest_log, 8780 block_num); 8781 if (rval != 0) 8782 goto out; 8783 } 8784 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 8785 entry = 8786 &ext_selftest_log-> 8787 smart_ext_selftest_log_entries[index]; 8788 8789 for (count = 1; 8790 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 8791 ++count) { 8792 uint8_t status; 8793 uint8_t code; 8794 uint8_t sense_key; 8795 uint8_t add_sense_code; 8796 uint8_t add_sense_code_qual; 8797 8798 /* If this is an unused entry, we are done */ 8799 if (bcmp(entry, &empty, sizeof (empty)) == 0) { 8800 /* Broken firmware on some disks */ 8801 if (index + 1 == 8802 ENTRIES_PER_EXT_SELFTEST_LOG_BLK) { 8803 --entry; 8804 --index; 8805 if (bcmp(entry, &empty, 8806 sizeof (empty)) == 0) 8807 goto out; 8808 } else 8809 goto out; 8810 } 8811 8812 if (only_one_block && 8813 start_index == index) 8814 goto out; 8815 8816 lpp->param_code[0] = 0; 8817 lpp->param_code[1] = count; 8818 lpp->param_ctrl_flags = 8819 LOG_CTRL_LP | LOG_CTRL_LBIN; 8820 lpp->param_len = 8821 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 8822 8823 status = entry->smart_ext_selftest_log_status; 8824 status >>= 4; 8825 switch (status) { 8826 case 0: 8827 default: 8828 sense_key = KEY_NO_SENSE; 8829 add_sense_code = 8830 SD_SCSI_ASC_NO_ADD_SENSE; 8831 add_sense_code_qual = 0; 8832 break; 8833 case 1: 8834 sense_key = KEY_ABORTED_COMMAND; 8835 add_sense_code = 8836 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8837 add_sense_code_qual = SCSI_COMPONENT_81; 8838 break; 8839 case 2: 8840 sense_key = KEY_ABORTED_COMMAND; 8841 add_sense_code = 8842 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8843 add_sense_code_qual = SCSI_COMPONENT_82; 8844 break; 8845 case 3: 8846 sense_key = KEY_ABORTED_COMMAND; 8847 add_sense_code = 8848 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8849 add_sense_code_qual = SCSI_COMPONENT_83; 8850 break; 8851 case 4: 8852 sense_key = KEY_HARDWARE_ERROR; 8853 add_sense_code = 8854 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8855 add_sense_code_qual = SCSI_COMPONENT_84; 8856 break; 8857 case 5: 8858 sense_key = KEY_HARDWARE_ERROR; 8859 add_sense_code = 8860 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8861 add_sense_code_qual = SCSI_COMPONENT_85; 8862 break; 8863 case 6: 8864 sense_key = KEY_HARDWARE_ERROR; 8865 add_sense_code = 8866 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8867 add_sense_code_qual = SCSI_COMPONENT_86; 8868 break; 8869 case 7: 8870 sense_key = KEY_MEDIUM_ERROR; 8871 add_sense_code = 8872 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8873 add_sense_code_qual = SCSI_COMPONENT_87; 8874 break; 8875 case 8: 8876 sense_key = KEY_HARDWARE_ERROR; 8877 add_sense_code = 8878 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8879 add_sense_code_qual = SCSI_COMPONENT_88; 8880 break; 8881 } 8882 code = 0; /* unspecified */ 8883 status |= (code << 4); 8884 lpp->param_values[0] = status; 8885 lpp->param_values[1] = 0; /* unspecified */ 8886 lpp->param_values[2] = entry-> 8887 smart_ext_selftest_log_timestamp[1]; 8888 lpp->param_values[3] = entry-> 8889 smart_ext_selftest_log_timestamp[0]; 8890 if (status != 0) { 8891 lpp->param_values[4] = 0; 8892 lpp->param_values[5] = 0; 8893 lpp->param_values[6] = entry-> 8894 smart_ext_selftest_log_failing_lba 8895 [5]; 8896 lpp->param_values[7] = entry-> 8897 smart_ext_selftest_log_failing_lba 8898 [4]; 8899 lpp->param_values[8] = entry-> 8900 smart_ext_selftest_log_failing_lba 8901 [3]; 8902 lpp->param_values[9] = entry-> 8903 smart_ext_selftest_log_failing_lba 8904 [2]; 8905 lpp->param_values[10] = entry-> 8906 smart_ext_selftest_log_failing_lba 8907 [1]; 8908 lpp->param_values[11] = entry-> 8909 smart_ext_selftest_log_failing_lba 8910 [0]; 8911 } else { /* No bad block address */ 8912 lpp->param_values[4] = 0xff; 8913 lpp->param_values[5] = 0xff; 8914 lpp->param_values[6] = 0xff; 8915 lpp->param_values[7] = 0xff; 8916 lpp->param_values[8] = 0xff; 8917 lpp->param_values[9] = 0xff; 8918 lpp->param_values[10] = 0xff; 8919 lpp->param_values[11] = 0xff; 8920 } 8921 8922 lpp->param_values[12] = sense_key; 8923 lpp->param_values[13] = add_sense_code; 8924 lpp->param_values[14] = add_sense_code_qual; 8925 lpp->param_values[15] = 0; /* undefined */ 8926 8927 lpp = (struct log_parameter *) 8928 (((uint8_t *)lpp) + 8929 SCSI_LOG_PARAM_HDR_LEN + 8930 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 8931 8932 --index; /* Back up to previous entry */ 8933 if (index < 0) { 8934 if (block_num > 0) { 8935 --block_num; 8936 } else { 8937 struct read_log_ext_directory 8938 logdir; 8939 8940 rval = 8941 sata_read_log_ext_directory( 8942 sata_hba_inst, sdinfo, 8943 &logdir); 8944 if (rval == -1) 8945 goto out; 8946 if ((logdir.read_log_ext_vers 8947 [0] == 0) && 8948 (logdir.read_log_ext_vers 8949 [1] == 0)) 8950 goto out; 8951 block_num = 8952 logdir.read_log_ext_nblks 8953 [EXT_SMART_SELFTEST_LOG_PAGE 8954 - 1][0]; 8955 block_num |= logdir. 8956 read_log_ext_nblks 8957 [EXT_SMART_SELFTEST_LOG_PAGE 8958 - 1][1] << 8; 8959 --block_num; 8960 only_one_block = 8961 (block_num == 0); 8962 } 8963 rval = sata_ext_smart_selftest_read_log( 8964 sata_hba_inst, sdinfo, 8965 ext_selftest_log, block_num); 8966 if (rval != 0) 8967 goto out; 8968 8969 index = 8970 ENTRIES_PER_EXT_SELFTEST_LOG_BLK - 8971 1; 8972 } 8973 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 8974 entry = &ext_selftest_log-> 8975 smart_ext_selftest_log_entries[index]; 8976 } 8977 } 8978 out: 8979 kmem_free(ext_selftest_log, 8980 sizeof (struct smart_ext_selftest_log)); 8981 } else { 8982 struct smart_selftest_log *selftest_log; 8983 8984 selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log), 8985 KM_SLEEP); 8986 8987 rval = sata_smart_selftest_log(sata_hba_inst, sdinfo, 8988 selftest_log); 8989 8990 if (rval == 0) { 8991 int index; 8992 int count; 8993 struct smart_selftest_log_entry *entry; 8994 static const struct smart_selftest_log_entry empty = 8995 { 0 }; 8996 8997 index = selftest_log->smart_selftest_log_index; 8998 if (index == 0) 8999 goto done; 9000 --index; /* Correct for 0 origin */ 9001 entry = &selftest_log-> 9002 smart_selftest_log_entries[index]; 9003 for (count = 1; 9004 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 9005 ++count) { 9006 uint8_t status; 9007 uint8_t code; 9008 uint8_t sense_key; 9009 uint8_t add_sense_code; 9010 uint8_t add_sense_code_qual; 9011 9012 if (bcmp(entry, &empty, sizeof (empty)) == 0) 9013 goto done; 9014 9015 lpp->param_code[0] = 0; 9016 lpp->param_code[1] = count; 9017 lpp->param_ctrl_flags = 9018 LOG_CTRL_LP | LOG_CTRL_LBIN; 9019 lpp->param_len = 9020 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 9021 9022 status = entry->smart_selftest_log_status; 9023 status >>= 4; 9024 switch (status) { 9025 case 0: 9026 default: 9027 sense_key = KEY_NO_SENSE; 9028 add_sense_code = 9029 SD_SCSI_ASC_NO_ADD_SENSE; 9030 break; 9031 case 1: 9032 sense_key = KEY_ABORTED_COMMAND; 9033 add_sense_code = 9034 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9035 add_sense_code_qual = SCSI_COMPONENT_81; 9036 break; 9037 case 2: 9038 sense_key = KEY_ABORTED_COMMAND; 9039 add_sense_code = 9040 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9041 add_sense_code_qual = SCSI_COMPONENT_82; 9042 break; 9043 case 3: 9044 sense_key = KEY_ABORTED_COMMAND; 9045 add_sense_code = 9046 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9047 add_sense_code_qual = SCSI_COMPONENT_83; 9048 break; 9049 case 4: 9050 sense_key = KEY_HARDWARE_ERROR; 9051 add_sense_code = 9052 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9053 add_sense_code_qual = SCSI_COMPONENT_84; 9054 break; 9055 case 5: 9056 sense_key = KEY_HARDWARE_ERROR; 9057 add_sense_code = 9058 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9059 add_sense_code_qual = SCSI_COMPONENT_85; 9060 break; 9061 case 6: 9062 sense_key = KEY_HARDWARE_ERROR; 9063 add_sense_code = 9064 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9065 add_sense_code_qual = SCSI_COMPONENT_86; 9066 break; 9067 case 7: 9068 sense_key = KEY_MEDIUM_ERROR; 9069 add_sense_code = 9070 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9071 add_sense_code_qual = SCSI_COMPONENT_87; 9072 break; 9073 case 8: 9074 sense_key = KEY_HARDWARE_ERROR; 9075 add_sense_code = 9076 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9077 add_sense_code_qual = SCSI_COMPONENT_88; 9078 break; 9079 } 9080 code = 0; /* unspecified */ 9081 status |= (code << 4); 9082 lpp->param_values[0] = status; 9083 lpp->param_values[1] = 0; /* unspecified */ 9084 lpp->param_values[2] = entry-> 9085 smart_selftest_log_timestamp[1]; 9086 lpp->param_values[3] = entry-> 9087 smart_selftest_log_timestamp[0]; 9088 if (status != 0) { 9089 lpp->param_values[4] = 0; 9090 lpp->param_values[5] = 0; 9091 lpp->param_values[6] = 0; 9092 lpp->param_values[7] = 0; 9093 lpp->param_values[8] = entry-> 9094 smart_selftest_log_failing_lba[3]; 9095 lpp->param_values[9] = entry-> 9096 smart_selftest_log_failing_lba[2]; 9097 lpp->param_values[10] = entry-> 9098 smart_selftest_log_failing_lba[1]; 9099 lpp->param_values[11] = entry-> 9100 smart_selftest_log_failing_lba[0]; 9101 } else { /* No block address */ 9102 lpp->param_values[4] = 0xff; 9103 lpp->param_values[5] = 0xff; 9104 lpp->param_values[6] = 0xff; 9105 lpp->param_values[7] = 0xff; 9106 lpp->param_values[8] = 0xff; 9107 lpp->param_values[9] = 0xff; 9108 lpp->param_values[10] = 0xff; 9109 lpp->param_values[11] = 0xff; 9110 } 9111 lpp->param_values[12] = sense_key; 9112 lpp->param_values[13] = add_sense_code; 9113 lpp->param_values[14] = add_sense_code_qual; 9114 lpp->param_values[15] = 0; /* undefined */ 9115 9116 lpp = (struct log_parameter *) 9117 (((uint8_t *)lpp) + 9118 SCSI_LOG_PARAM_HDR_LEN + 9119 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 9120 --index; /* back up to previous entry */ 9121 if (index < 0) { 9122 index = 9123 NUM_SMART_SELFTEST_LOG_ENTRIES - 1; 9124 } 9125 entry = &selftest_log-> 9126 smart_selftest_log_entries[index]; 9127 } 9128 } 9129 done: 9130 kmem_free(selftest_log, sizeof (struct smart_selftest_log)); 9131 } 9132 9133 return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) * 9134 SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS); 9135 } 9136 9137 /* 9138 * sata_build_lsense_page_2f() is used to create the 9139 * SCSI LOG SENSE page 0x2f (informational exceptions) 9140 * 9141 * Takes a sata_drive_info t * and the address of a buffer 9142 * in which to create the page information as well as a sata_hba_inst_t *. 9143 * 9144 * Returns the number of bytes valid in the buffer. 9145 * 9146 * Because it invokes function(s) that send synchronously executed command 9147 * to the HBA, it cannot be called in the interrupt context. 9148 */ 9149 static int 9150 sata_build_lsense_page_2f( 9151 sata_drive_info_t *sdinfo, 9152 uint8_t *buf, 9153 sata_hba_inst_t *sata_hba_inst) 9154 { 9155 struct log_parameter *lpp = (struct log_parameter *)buf; 9156 int rval; 9157 uint8_t *smart_data; 9158 uint8_t temp; 9159 sata_id_t *sata_id; 9160 #define SMART_NO_TEMP 0xff 9161 9162 lpp->param_code[0] = 0; 9163 lpp->param_code[1] = 0; 9164 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 9165 9166 /* Now get the SMART status w.r.t. threshold exceeded */ 9167 rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo); 9168 switch (rval) { 9169 case 1: 9170 lpp->param_values[0] = SCSI_PREDICTED_FAILURE; 9171 lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE; 9172 break; 9173 case 0: 9174 case -1: /* failed to get data */ 9175 lpp->param_values[0] = 0; /* No failure predicted */ 9176 lpp->param_values[1] = 0; 9177 break; 9178 #if defined(SATA_DEBUG) 9179 default: 9180 cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value"); 9181 /* NOTREACHED */ 9182 #endif 9183 } 9184 9185 sata_id = &sdinfo->satadrv_id; 9186 if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP)) 9187 temp = SMART_NO_TEMP; 9188 else { 9189 /* Now get the temperature */ 9190 smart_data = kmem_zalloc(512, KM_SLEEP); 9191 rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data, 9192 SCT_STATUS_LOG_PAGE, 1); 9193 if (rval == -1) 9194 temp = SMART_NO_TEMP; 9195 else { 9196 temp = smart_data[200]; 9197 if (temp & 0x80) { 9198 if (temp & 0x7f) 9199 temp = 0; 9200 else 9201 temp = SMART_NO_TEMP; 9202 } 9203 } 9204 kmem_free(smart_data, 512); 9205 } 9206 9207 lpp->param_values[2] = temp; /* most recent temperature */ 9208 lpp->param_values[3] = 0; /* required vendor specific byte */ 9209 9210 lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN; 9211 9212 9213 return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN); 9214 } 9215 9216 /* 9217 * sata_build_lsense_page_30() is used to create the 9218 * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data). 9219 * 9220 * Takes a sata_drive_info t * and the address of a buffer 9221 * in which to create the page information as well as a sata_hba_inst_t *. 9222 * 9223 * Returns the number of bytes valid in the buffer. 9224 */ 9225 static int 9226 sata_build_lsense_page_30( 9227 sata_drive_info_t *sdinfo, 9228 uint8_t *buf, 9229 sata_hba_inst_t *sata_hba_inst) 9230 { 9231 struct smart_data *smart_data = (struct smart_data *)buf; 9232 int rval; 9233 9234 /* Now do the SMART READ DATA */ 9235 rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data); 9236 if (rval == -1) 9237 return (0); 9238 9239 return (sizeof (struct smart_data)); 9240 } 9241 9242 /* 9243 * sata_build_lsense_page_0e() is used to create the 9244 * SCSI LOG SENSE page 0e (start-stop cycle counter page) 9245 * 9246 * Date of Manufacture (0x0001) 9247 * YEAR = "0000" 9248 * WEEK = "00" 9249 * Accounting Date (0x0002) 9250 * 6 ASCII space character(20h) 9251 * Specified cycle count over device lifetime 9252 * VALUE - THRESH - the delta between max and min; 9253 * Accumulated start-stop cycles 9254 * VALUE - WORST - the accumulated cycles; 9255 * 9256 * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute 9257 * 9258 * Takes a sata_drive_info t * and the address of a buffer 9259 * in which to create the page information as well as a sata_hba_inst_t *. 9260 * 9261 * Returns the number of bytes valid in the buffer. 9262 */ 9263 static int 9264 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf, 9265 sata_pkt_txlate_t *spx) 9266 { 9267 struct start_stop_cycle_counter_log *log_page; 9268 int i, rval, index; 9269 uint8_t smart_data[512], id, value, worst, thresh; 9270 uint32_t max_count, cycles; 9271 9272 /* Now do the SMART READ DATA */ 9273 rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo, 9274 (struct smart_data *)smart_data); 9275 if (rval == -1) 9276 return (0); 9277 for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) { 9278 index = (i * 12) + 2; 9279 id = smart_data[index]; 9280 if (id != SMART_START_STOP_COUNT_ID) 9281 continue; 9282 else { 9283 thresh = smart_data[index + 2]; 9284 value = smart_data[index + 3]; 9285 worst = smart_data[index + 4]; 9286 break; 9287 } 9288 } 9289 if (id != SMART_START_STOP_COUNT_ID) 9290 return (0); 9291 max_count = value - thresh; 9292 cycles = value - worst; 9293 9294 log_page = (struct start_stop_cycle_counter_log *)buf; 9295 bzero(log_page, sizeof (struct start_stop_cycle_counter_log)); 9296 log_page->code = 0x0e; 9297 log_page->page_len_low = 0x24; 9298 9299 log_page->manufactor_date_low = 0x1; 9300 log_page->param_1.fmt_link = 0x1; /* 01b */ 9301 log_page->param_len_1 = 0x06; 9302 for (i = 0; i < 4; i++) { 9303 log_page->year_manu[i] = 0x30; 9304 if (i < 2) 9305 log_page->week_manu[i] = 0x30; 9306 } 9307 9308 log_page->account_date_low = 0x02; 9309 log_page->param_2.fmt_link = 0x01; /* 01b */ 9310 log_page->param_len_2 = 0x06; 9311 for (i = 0; i < 4; i++) { 9312 log_page->year_account[i] = 0x20; 9313 if (i < 2) 9314 log_page->week_account[i] = 0x20; 9315 } 9316 9317 log_page->lifetime_code_low = 0x03; 9318 log_page->param_3.fmt_link = 0x03; /* 11b */ 9319 log_page->param_len_3 = 0x04; 9320 /* VALUE - THRESH - the delta between max and min */ 9321 log_page->cycle_code_low = 0x04; 9322 log_page->param_4.fmt_link = 0x03; /* 11b */ 9323 log_page->param_len_4 = 0x04; 9324 /* WORST - THRESH - the distance from 'now' to min */ 9325 9326 for (i = 0; i < 4; i++) { 9327 log_page->cycle_lifetime[i] = 9328 (max_count >> (8 * (3 - i))) & 0xff; 9329 log_page->cycle_accumulated[i] = 9330 (cycles >> (8 * (3 - i))) & 0xff; 9331 } 9332 9333 return (sizeof (struct start_stop_cycle_counter_log)); 9334 } 9335 9336 /* 9337 * This function was used for build a ATA read verify sector command 9338 */ 9339 static void 9340 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba) 9341 { 9342 scmd->satacmd_cmd_reg = SATAC_RDVER; 9343 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 9344 scmd->satacmd_flags.sata_special_regs = B_TRUE; 9345 9346 scmd->satacmd_sec_count_lsb = sec & 0xff; 9347 scmd->satacmd_lba_low_lsb = lba & 0xff; 9348 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 9349 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 9350 scmd->satacmd_device_reg = (SATA_ADH_LBA | (lba >> 24) & 0xf); 9351 scmd->satacmd_features_reg = 0; 9352 scmd->satacmd_status_reg = 0; 9353 scmd->satacmd_error_reg = 0; 9354 } 9355 9356 /* 9357 * This function was used for building an ATA 9358 * command, and only command register need to 9359 * be defined, other register will be zero or na. 9360 */ 9361 static void 9362 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd) 9363 { 9364 scmd->satacmd_addr_type = 0; 9365 scmd->satacmd_cmd_reg = cmd; 9366 scmd->satacmd_device_reg = 0; 9367 scmd->satacmd_sec_count_lsb = 0; 9368 scmd->satacmd_lba_low_lsb = 0; 9369 scmd->satacmd_lba_mid_lsb = 0; 9370 scmd->satacmd_lba_high_lsb = 0; 9371 scmd->satacmd_features_reg = 0; 9372 scmd->satacmd_status_reg = 0; 9373 scmd->satacmd_error_reg = 0; 9374 scmd->satacmd_flags.sata_special_regs = B_TRUE; 9375 } 9376 9377 /* 9378 * This function was used for changing the standby 9379 * timer format from SCSI to ATA. 9380 */ 9381 static uint8_t 9382 sata_get_standby_timer(uint8_t *timer) 9383 { 9384 uint32_t i = 0, count = 0; 9385 uint8_t ata_count; 9386 9387 for (i = 0; i < 4; i++) { 9388 count = count << 8 | timer[i]; 9389 } 9390 9391 if (count == 0) 9392 return (0); 9393 9394 if (count >= 1 && count <= 12000) 9395 ata_count = (count -1) / 50 + 1; 9396 else if (count > 12000 && count <= 12600) 9397 ata_count = 0xfc; 9398 else if (count > 12601 && count <= 12750) 9399 ata_count = 0xff; 9400 else if (count > 12750 && count <= 17999) 9401 ata_count = 0xf1; 9402 else if (count > 18000 && count <= 198000) 9403 ata_count = count / 18000 + 240; 9404 else 9405 ata_count = 0xfd; 9406 return (ata_count); 9407 } 9408 9409 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */ 9410 9411 /* 9412 * Start command for ATAPI device. 9413 * This function processes scsi_pkt requests. 9414 * Now CD/DVD, tape and ATAPI disk devices are supported. 9415 * Most commands are packet without any translation into Packet Command. 9416 * Some may be trapped and executed as SATA commands (not clear which one). 9417 * 9418 * Returns TRAN_ACCEPT if command is accepted for execution (or completed 9419 * execution). 9420 * Returns other TRAN_XXXX codes if command is not accepted or completed 9421 * (see return values for sata_hba_start()). 9422 * 9423 * Note: 9424 * Inquiry cdb format differs between transport version 2 and 3. 9425 * However, the transport version 3 devices that were checked did not adhere 9426 * to the specification (ignored MSB of the allocation length). Therefore, 9427 * the transport version is not checked, but Inquiry allocation length is 9428 * truncated to 255 bytes if the original allocation length set-up by the 9429 * target driver is greater than 255 bytes. 9430 */ 9431 static int 9432 sata_txlt_atapi(sata_pkt_txlate_t *spx) 9433 { 9434 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 9435 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 9436 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 9437 sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx); 9438 sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba, 9439 &spx->txlt_sata_pkt->satapkt_device); 9440 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 9441 int cdblen; 9442 int rval, reason; 9443 int synch; 9444 union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp; 9445 9446 mutex_enter(cport_mutex); 9447 9448 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 9449 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 9450 mutex_exit(cport_mutex); 9451 return (rval); 9452 } 9453 9454 /* 9455 * ATAPI device executes some ATA commands in addition to those 9456 * commands sent via PACKET command. These ATA commands may be 9457 * executed by the regular SATA translation functions. None needs 9458 * to be captured now. 9459 * 9460 * Commands sent via PACKET command include: 9461 * MMC command set for ATAPI CD/DVD device 9462 * SSC command set for ATAPI TAPE device 9463 * SBC command set for ATAPI disk device 9464 * 9465 */ 9466 9467 /* Check the size of cdb */ 9468 9469 switch (GETGROUP(cdbp)) { 9470 case CDB_GROUPID_3: /* Reserved, per SPC-4 */ 9471 /* 9472 * opcodes 0x7e and 0x7f identify variable-length CDBs and 9473 * therefore require special handling. Return failure, for now. 9474 */ 9475 mutex_exit(cport_mutex); 9476 return (TRAN_BADPKT); 9477 9478 case CDB_GROUPID_6: /* Vendor-specific, per SPC-4 */ 9479 case CDB_GROUPID_7: /* Vendor-specific, per SPC-4 */ 9480 /* obtain length from the scsi_pkt */ 9481 cdblen = scsipkt->pkt_cdblen; 9482 break; 9483 9484 default: 9485 /* CDB's length is statically known, per SPC-4 */ 9486 cdblen = scsi_cdb_size[GETGROUP(cdbp)]; 9487 break; 9488 } 9489 9490 if (cdblen <= 0 || cdblen > sdinfo->satadrv_atapi_cdb_len) { 9491 sata_log(NULL, CE_WARN, 9492 "sata: invalid ATAPI cdb length %d", 9493 cdblen); 9494 mutex_exit(cport_mutex); 9495 return (TRAN_BADPKT); 9496 } 9497 9498 SATAATAPITRACE(spx, cdblen); 9499 9500 /* 9501 * For non-read/write commands we need to 9502 * map buffer 9503 */ 9504 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 9505 case SCMD_READ: 9506 case SCMD_READ_G1: 9507 case SCMD_READ_G5: 9508 case SCMD_READ_G4: 9509 case SCMD_WRITE: 9510 case SCMD_WRITE_G1: 9511 case SCMD_WRITE_G5: 9512 case SCMD_WRITE_G4: 9513 break; 9514 default: 9515 if (bp != NULL) { 9516 if (bp->b_flags & (B_PHYS | B_PAGEIO)) 9517 bp_mapin(bp); 9518 } 9519 break; 9520 } 9521 /* 9522 * scmd->satacmd_flags.sata_data_direction default - 9523 * SATA_DIR_NODATA_XFER - is set by 9524 * sata_txlt_generic_pkt_info(). 9525 */ 9526 if (scmd->satacmd_bp) { 9527 if (scmd->satacmd_bp->b_flags & B_READ) { 9528 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 9529 } else { 9530 scmd->satacmd_flags.sata_data_direction = 9531 SATA_DIR_WRITE; 9532 } 9533 } 9534 9535 /* 9536 * Set up ATAPI packet command. 9537 */ 9538 9539 sata_atapi_packet_cmd_setup(scmd, sdinfo); 9540 9541 /* Copy cdb into sata_cmd */ 9542 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 9543 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 9544 bcopy(cdbp, scmd->satacmd_acdb, cdblen); 9545 9546 /* See note in the command header */ 9547 if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) { 9548 if (scmd->satacmd_acdb[3] != 0) 9549 scmd->satacmd_acdb[4] = 255; 9550 } 9551 9552 #ifdef SATA_DEBUG 9553 if (sata_debug_flags & SATA_DBG_ATAPI) { 9554 uint8_t *p = scmd->satacmd_acdb; 9555 char buf[3 * SATA_ATAPI_MAX_CDB_LEN]; 9556 9557 (void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN, 9558 "%02x %02x %02x %02x %02x %02x %02x %02x " 9559 "%2x %02x %02x %02x %02x %02x %02x %02x", 9560 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 9561 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 9562 buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0'; 9563 cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf); 9564 } 9565 #endif 9566 9567 /* 9568 * Preset request sense data to NO SENSE. 9569 * If there is no way to get error information via Request Sense, 9570 * the packet request sense data would not have to be modified by HBA, 9571 * but it could be returned as is. 9572 */ 9573 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN); 9574 sata_fixed_sense_data_preset( 9575 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 9576 9577 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 9578 /* Need callback function */ 9579 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion; 9580 synch = FALSE; 9581 } else 9582 synch = TRUE; 9583 9584 /* Transfer command to HBA */ 9585 if (sata_hba_start(spx, &rval) != 0) { 9586 /* Pkt not accepted for execution */ 9587 mutex_exit(cport_mutex); 9588 return (rval); 9589 } 9590 mutex_exit(cport_mutex); 9591 /* 9592 * If execution is non-synchronous, 9593 * a callback function will handle potential errors, translate 9594 * the response and will do a callback to a target driver. 9595 * If it was synchronous, use the same framework callback to check 9596 * an execution status. 9597 */ 9598 if (synch) { 9599 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 9600 "synchronous execution status %x\n", 9601 spx->txlt_sata_pkt->satapkt_reason); 9602 sata_txlt_atapi_completion(spx->txlt_sata_pkt); 9603 } 9604 return (TRAN_ACCEPT); 9605 } 9606 9607 9608 /* 9609 * ATAPI Packet command completion. 9610 * 9611 * Failure of the command passed via Packet command are considered device 9612 * error. SATA HBA driver would have to retrieve error data (via Request 9613 * Sense command delivered via error retrieval sata packet) and copy it 9614 * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data. 9615 */ 9616 static void 9617 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt) 9618 { 9619 sata_pkt_txlate_t *spx = 9620 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 9621 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 9622 struct scsi_extended_sense *sense; 9623 struct buf *bp; 9624 int rval; 9625 9626 #ifdef SATA_DEBUG 9627 uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense; 9628 #endif 9629 9630 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 9631 STATE_SENT_CMD | STATE_GOT_STATUS; 9632 9633 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 9634 /* Normal completion */ 9635 if (sata_pkt->satapkt_cmd.satacmd_bp != NULL) 9636 scsipkt->pkt_state |= STATE_XFERRED_DATA; 9637 scsipkt->pkt_reason = CMD_CMPLT; 9638 *scsipkt->pkt_scbp = STATUS_GOOD; 9639 if (spx->txlt_tmp_buf != NULL) { 9640 /* Temporary buffer was used */ 9641 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 9642 if (bp->b_flags & B_READ) { 9643 rval = ddi_dma_sync( 9644 spx->txlt_buf_dma_handle, 0, 0, 9645 DDI_DMA_SYNC_FORCPU); 9646 ASSERT(rval == DDI_SUCCESS); 9647 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 9648 bp->b_bcount); 9649 } 9650 } 9651 } else { 9652 /* 9653 * Something went wrong - analyze return 9654 */ 9655 *scsipkt->pkt_scbp = STATUS_CHECK; 9656 sense = sata_arq_sense(spx); 9657 9658 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 9659 /* 9660 * pkt_reason should be CMD_CMPLT for DEVICE ERROR. 9661 * Under this condition ERR bit is set for ATA command, 9662 * and CHK bit set for ATAPI command. 9663 * 9664 * Please check st_intr & sdintr about how pkt_reason 9665 * is used. 9666 */ 9667 scsipkt->pkt_reason = CMD_CMPLT; 9668 9669 /* 9670 * We may not have ARQ data if there was a double 9671 * error. But sense data in sata packet was pre-set 9672 * with NO SENSE so it is valid even if HBA could 9673 * not retrieve a real sense data. 9674 * Just copy this sense data into scsi pkt sense area. 9675 */ 9676 bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense, 9677 SATA_ATAPI_MIN_RQSENSE_LEN); 9678 #ifdef SATA_DEBUG 9679 if (sata_debug_flags & SATA_DBG_SCSI_IF) { 9680 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 9681 "sata_txlt_atapi_completion: %02x\n" 9682 "RQSENSE: %02x %02x %02x %02x %02x %02x " 9683 " %02x %02x %02x %02x %02x %02x " 9684 " %02x %02x %02x %02x %02x %02x\n", 9685 scsipkt->pkt_reason, 9686 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 9687 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 9688 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 9689 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 9690 rqsp[16], rqsp[17]); 9691 } 9692 #endif 9693 } else { 9694 switch (sata_pkt->satapkt_reason) { 9695 case SATA_PKT_PORT_ERROR: 9696 /* 9697 * We have no device data. 9698 */ 9699 scsipkt->pkt_reason = CMD_INCOMPLETE; 9700 scsipkt->pkt_state &= ~(STATE_GOT_BUS | 9701 STATE_GOT_TARGET | STATE_SENT_CMD | 9702 STATE_GOT_STATUS); 9703 sense->es_key = KEY_HARDWARE_ERROR; 9704 break; 9705 9706 case SATA_PKT_TIMEOUT: 9707 scsipkt->pkt_reason = CMD_TIMEOUT; 9708 scsipkt->pkt_statistics |= 9709 STAT_TIMEOUT | STAT_DEV_RESET; 9710 /* 9711 * Need to check if HARDWARE_ERROR/ 9712 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more 9713 * appropriate. 9714 */ 9715 break; 9716 9717 case SATA_PKT_ABORTED: 9718 scsipkt->pkt_reason = CMD_ABORTED; 9719 scsipkt->pkt_statistics |= STAT_ABORTED; 9720 /* Should we set key COMMAND_ABPRTED? */ 9721 break; 9722 9723 case SATA_PKT_RESET: 9724 scsipkt->pkt_reason = CMD_RESET; 9725 scsipkt->pkt_statistics |= STAT_DEV_RESET; 9726 /* 9727 * May be we should set Unit Attention / 9728 * Reset. Perhaps the same should be 9729 * returned for disks.... 9730 */ 9731 sense->es_key = KEY_UNIT_ATTENTION; 9732 sense->es_add_code = SD_SCSI_ASC_RESET; 9733 break; 9734 9735 default: 9736 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 9737 "sata_txlt_atapi_completion: " 9738 "invalid packet completion reason")); 9739 scsipkt->pkt_reason = CMD_TRAN_ERR; 9740 scsipkt->pkt_state &= ~(STATE_GOT_BUS | 9741 STATE_GOT_TARGET | STATE_SENT_CMD | 9742 STATE_GOT_STATUS); 9743 break; 9744 } 9745 } 9746 } 9747 9748 SATAATAPITRACE(spx, 0); 9749 9750 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 9751 scsipkt->pkt_comp != NULL) { 9752 /* scsi callback required */ 9753 (*scsipkt->pkt_comp)(scsipkt); 9754 } 9755 } 9756 9757 /* 9758 * Set up error retrieval sata command for ATAPI Packet Command error data 9759 * recovery. 9760 * 9761 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up, 9762 * returns SATA_FAILURE otherwise. 9763 */ 9764 9765 static int 9766 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo) 9767 { 9768 sata_pkt_t *spkt = spx->txlt_sata_pkt; 9769 sata_cmd_t *scmd; 9770 struct buf *bp; 9771 9772 /* 9773 * Allocate dma-able buffer error data. 9774 * Buffer allocation will take care of buffer alignment and other DMA 9775 * attributes. 9776 */ 9777 bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN); 9778 if (bp == NULL) { 9779 SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst, 9780 "sata_get_err_retrieval_pkt: " 9781 "cannot allocate buffer for error data", NULL); 9782 return (SATA_FAILURE); 9783 } 9784 bp_mapin(bp); /* make data buffer accessible */ 9785 9786 /* Operation modes are up to the caller */ 9787 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 9788 9789 /* Synchronous mode, no callback - may be changed by the caller */ 9790 spkt->satapkt_comp = NULL; 9791 spkt->satapkt_time = sata_default_pkt_time; 9792 9793 scmd = &spkt->satapkt_cmd; 9794 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 9795 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 9796 9797 sata_atapi_packet_cmd_setup(scmd, sdinfo); 9798 9799 /* 9800 * Set-up acdb. Request Sense CDB (packet command content) is 9801 * not in DMA-able buffer. Its handling is HBA-specific (how 9802 * it is transfered into packet FIS). 9803 */ 9804 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 9805 bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN); 9806 /* Following zeroing of pad bytes may not be necessary */ 9807 bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN], 9808 sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN); 9809 9810 /* 9811 * Set-up pointer to the buffer handle, so HBA can sync buffer 9812 * before accessing it. Handle is in usual place in translate struct. 9813 */ 9814 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle; 9815 9816 /* 9817 * Preset request sense data to NO SENSE. 9818 * Here it is redundant, only for a symetry with scsi-originated 9819 * packets. It should not be used for anything but debugging. 9820 */ 9821 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN); 9822 sata_fixed_sense_data_preset( 9823 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 9824 9825 ASSERT(scmd->satacmd_num_dma_cookies != 0); 9826 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 9827 9828 return (SATA_SUCCESS); 9829 } 9830 9831 /* 9832 * Set-up ATAPI packet command. 9833 * Data transfer direction has to be set-up in sata_cmd structure prior to 9834 * calling this function. 9835 * 9836 * Returns void 9837 */ 9838 9839 static void 9840 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo) 9841 { 9842 scmd->satacmd_addr_type = 0; /* N/A */ 9843 scmd->satacmd_sec_count_lsb = 0; /* no tag */ 9844 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 9845 scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ; 9846 scmd->satacmd_lba_high_lsb = 9847 (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8); 9848 scmd->satacmd_cmd_reg = SATAC_PACKET; /* Command */ 9849 9850 /* 9851 * We want all data to be transfered via DMA. 9852 * But specify it only if drive supports DMA and DMA mode is 9853 * selected - some drives are sensitive about it. 9854 * Hopefully it wil work for all drives.... 9855 */ 9856 if (sdinfo->satadrv_settings & SATA_DEV_DMA) 9857 scmd->satacmd_features_reg = SATA_ATAPI_F_DMA; 9858 9859 /* 9860 * Features register requires special care for devices that use 9861 * Serial ATA bridge - they need an explicit specification of 9862 * the data transfer direction for Packet DMA commands. 9863 * Setting this bit is harmless if DMA is not used. 9864 * 9865 * Many drives do not implement word 80, specifying what ATA/ATAPI 9866 * spec they follow. 9867 * We are arbitrarily following the latest SerialATA 2.6 spec, 9868 * which uses ATA/ATAPI 6 specification for Identify Data, unless 9869 * ATA/ATAPI-7 support is explicitly indicated. 9870 */ 9871 if (sdinfo->satadrv_id.ai_majorversion != 0 && 9872 sdinfo->satadrv_id.ai_majorversion != 0xffff && 9873 (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) { 9874 /* 9875 * Specification of major version is valid and version 7 9876 * is supported. It does automatically imply that all 9877 * spec features are supported. For now, we assume that 9878 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete. 9879 */ 9880 if ((sdinfo->satadrv_id.ai_dirdma & 9881 SATA_ATAPI_ID_DMADIR_REQ) != 0) { 9882 if (scmd->satacmd_flags.sata_data_direction == 9883 SATA_DIR_READ) 9884 scmd->satacmd_features_reg |= 9885 SATA_ATAPI_F_DATA_DIR_READ; 9886 } 9887 } 9888 } 9889 9890 9891 #ifdef SATA_DEBUG 9892 9893 /* Display 18 bytes of Inquiry data */ 9894 static void 9895 sata_show_inqry_data(uint8_t *buf) 9896 { 9897 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 9898 uint8_t *p; 9899 9900 cmn_err(CE_NOTE, "Inquiry data:"); 9901 cmn_err(CE_NOTE, "device type %x", inq->inq_dtype); 9902 cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb); 9903 cmn_err(CE_NOTE, "version %x", inq->inq_ansi); 9904 cmn_err(CE_NOTE, "ATAPI transport version %d", 9905 SATA_ATAPI_TRANS_VERSION(inq)); 9906 cmn_err(CE_NOTE, "response data format %d, aenc %d", 9907 inq->inq_rdf, inq->inq_aenc); 9908 cmn_err(CE_NOTE, " additional length %d", inq->inq_len); 9909 cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs); 9910 p = (uint8_t *)inq->inq_vid; 9911 cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x " 9912 "%02x %02x %02x %02x", 9913 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 9914 p = (uint8_t *)inq->inq_vid; 9915 cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c", 9916 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 9917 9918 p = (uint8_t *)inq->inq_pid; 9919 cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x " 9920 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", 9921 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 9922 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 9923 p = (uint8_t *)inq->inq_pid; 9924 cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c " 9925 "%c %c %c %c %c %c %c %c", 9926 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 9927 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 9928 9929 p = (uint8_t *)inq->inq_revision; 9930 cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x", 9931 p[0], p[1], p[2], p[3]); 9932 p = (uint8_t *)inq->inq_revision; 9933 cmn_err(CE_NOTE, "revision: %c %c %c %c", 9934 p[0], p[1], p[2], p[3]); 9935 9936 } 9937 9938 9939 static void 9940 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count) 9941 { 9942 struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt; 9943 9944 if (scsi_pkt == NULL) 9945 return; 9946 if (count != 0) { 9947 /* saving cdb */ 9948 bzero(sata_atapi_trace[sata_atapi_trace_index].acdb, 9949 SATA_ATAPI_MAX_CDB_LEN); 9950 bcopy(scsi_pkt->pkt_cdbp, 9951 sata_atapi_trace[sata_atapi_trace_index].acdb, count); 9952 } else { 9953 bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)-> 9954 sts_sensedata, 9955 sata_atapi_trace[sata_atapi_trace_index].arqs, 9956 SATA_ATAPI_MIN_RQSENSE_LEN); 9957 sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason = 9958 scsi_pkt->pkt_reason; 9959 sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason = 9960 spx->txlt_sata_pkt->satapkt_reason; 9961 9962 if (++sata_atapi_trace_index >= 64) 9963 sata_atapi_trace_index = 0; 9964 } 9965 } 9966 9967 #endif 9968 9969 /* 9970 * Fetch inquiry data from ATAPI device 9971 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 9972 * 9973 * Note: 9974 * inqb pointer does not point to a DMA-able buffer. It is a local buffer 9975 * where the caller expects to see the inquiry data. 9976 * 9977 */ 9978 9979 static int 9980 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba, 9981 sata_address_t *saddr, struct scsi_inquiry *inq) 9982 { 9983 sata_pkt_txlate_t *spx; 9984 sata_pkt_t *spkt; 9985 struct buf *bp; 9986 sata_drive_info_t *sdinfo; 9987 sata_cmd_t *scmd; 9988 int rval; 9989 uint8_t *rqsp; 9990 dev_info_t *dip = SATA_DIP(sata_hba); 9991 #ifdef SATA_DEBUG 9992 char msg_buf[MAXPATHLEN]; 9993 #endif 9994 kmutex_t *cport_mutex; 9995 9996 ASSERT(sata_hba != NULL); 9997 9998 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 9999 spx->txlt_sata_hba_inst = sata_hba; 10000 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 10001 spkt = sata_pkt_alloc(spx, NULL); 10002 if (spkt == NULL) { 10003 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10004 return (SATA_FAILURE); 10005 } 10006 /* address is needed now */ 10007 spkt->satapkt_device.satadev_addr = *saddr; 10008 10009 /* scsi_inquiry size buffer */ 10010 bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry)); 10011 if (bp == NULL) { 10012 sata_pkt_free(spx); 10013 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10014 SATA_LOG_D((sata_hba, CE_WARN, 10015 "sata_get_atapi_inquiry_data: " 10016 "cannot allocate data buffer")); 10017 return (SATA_FAILURE); 10018 } 10019 bp_mapin(bp); /* make data buffer accessible */ 10020 10021 scmd = &spkt->satapkt_cmd; 10022 ASSERT(scmd->satacmd_num_dma_cookies != 0); 10023 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 10024 10025 /* Use synchronous mode */ 10026 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 10027 spkt->satapkt_comp = NULL; 10028 spkt->satapkt_time = sata_default_pkt_time; 10029 10030 /* Issue inquiry command - 6 bytes cdb, data transfer, read */ 10031 10032 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 10033 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 10034 10035 cport_mutex = &(SATA_CPORT_MUTEX(sata_hba, saddr->cport)); 10036 mutex_enter(cport_mutex); 10037 sdinfo = sata_get_device_info(sata_hba, 10038 &spx->txlt_sata_pkt->satapkt_device); 10039 if (sdinfo == NULL) { 10040 /* we have to be carefull about the disapearing device */ 10041 mutex_exit(cport_mutex); 10042 rval = SATA_FAILURE; 10043 goto cleanup; 10044 } 10045 sata_atapi_packet_cmd_setup(scmd, sdinfo); 10046 10047 /* 10048 * Set-up acdb. This works for atapi transport version 2 and later. 10049 */ 10050 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 10051 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 10052 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */ 10053 scmd->satacmd_acdb[1] = 0x00; 10054 scmd->satacmd_acdb[2] = 0x00; 10055 scmd->satacmd_acdb[3] = 0x00; 10056 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry); 10057 scmd->satacmd_acdb[5] = 0x00; 10058 10059 sata_fixed_sense_data_preset( 10060 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 10061 10062 /* Transfer command to HBA */ 10063 if (sata_hba_start(spx, &rval) != 0) { 10064 /* Pkt not accepted for execution */ 10065 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10066 "sata_get_atapi_inquiry_data: " 10067 "Packet not accepted for execution - ret: %02x", rval); 10068 mutex_exit(cport_mutex); 10069 rval = SATA_FAILURE; 10070 goto cleanup; 10071 } 10072 mutex_exit(cport_mutex); 10073 10074 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) { 10075 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10076 "sata_get_atapi_inquiry_data: " 10077 "Packet completed successfully - ret: %02x", rval); 10078 if (spx->txlt_buf_dma_handle != NULL) { 10079 /* 10080 * Sync buffer. Handle is in usual place in translate 10081 * struct. 10082 */ 10083 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 10084 DDI_DMA_SYNC_FORCPU); 10085 ASSERT(rval == DDI_SUCCESS); 10086 } 10087 10088 if (sata_check_for_dma_error(dip, spx)) { 10089 ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED); 10090 rval = SATA_FAILURE; 10091 } else { 10092 /* 10093 * Normal completion - copy data into caller's buffer 10094 */ 10095 bcopy(bp->b_un.b_addr, (uint8_t *)inq, 10096 sizeof (struct scsi_inquiry)); 10097 #ifdef SATA_DEBUG 10098 if (sata_debug_flags & SATA_DBG_ATAPI) { 10099 sata_show_inqry_data((uint8_t *)inq); 10100 } 10101 #endif 10102 rval = SATA_SUCCESS; 10103 } 10104 } else { 10105 /* 10106 * Something went wrong - analyze return - check rqsense data 10107 */ 10108 rval = SATA_FAILURE; 10109 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 10110 /* 10111 * ARQ data hopefull show something other than NO SENSE 10112 */ 10113 rqsp = scmd->satacmd_rqsense; 10114 #ifdef SATA_DEBUG 10115 if (sata_debug_flags & SATA_DBG_ATAPI) { 10116 msg_buf[0] = '\0'; 10117 (void) snprintf(msg_buf, MAXPATHLEN, 10118 "ATAPI packet completion reason: %02x\n" 10119 "RQSENSE: %02x %02x %02x %02x %02x %02x\n" 10120 " %02x %02x %02x %02x %02x %02x\n" 10121 " %02x %02x %02x %02x %02x %02x", 10122 spkt->satapkt_reason, 10123 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 10124 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 10125 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 10126 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 10127 rqsp[16], rqsp[17]); 10128 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 10129 "%s", msg_buf); 10130 } 10131 #endif 10132 } else { 10133 switch (spkt->satapkt_reason) { 10134 case SATA_PKT_PORT_ERROR: 10135 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10136 "sata_get_atapi_inquiry_data: " 10137 "packet reason: port error", NULL); 10138 break; 10139 10140 case SATA_PKT_TIMEOUT: 10141 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10142 "sata_get_atapi_inquiry_data: " 10143 "packet reason: timeout", NULL); 10144 break; 10145 10146 case SATA_PKT_ABORTED: 10147 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10148 "sata_get_atapi_inquiry_data: " 10149 "packet reason: aborted", NULL); 10150 break; 10151 10152 case SATA_PKT_RESET: 10153 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10154 "sata_get_atapi_inquiry_data: " 10155 "packet reason: reset\n", NULL); 10156 break; 10157 default: 10158 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10159 "sata_get_atapi_inquiry_data: " 10160 "invalid packet reason: %02x\n", 10161 spkt->satapkt_reason); 10162 break; 10163 } 10164 } 10165 } 10166 cleanup: 10167 sata_free_local_buffer(spx); 10168 sata_pkt_free(spx); 10169 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10170 return (rval); 10171 } 10172 10173 10174 10175 10176 10177 #if 0 10178 #ifdef SATA_DEBUG 10179 10180 /* 10181 * Test ATAPI packet command. 10182 * Single threaded test: send packet command in synch mode, process completion 10183 * 10184 */ 10185 static void 10186 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport) 10187 { 10188 sata_pkt_txlate_t *spx; 10189 sata_pkt_t *spkt; 10190 struct buf *bp; 10191 sata_device_t sata_device; 10192 sata_drive_info_t *sdinfo; 10193 sata_cmd_t *scmd; 10194 int rval; 10195 uint8_t *rqsp; 10196 10197 ASSERT(sata_hba_inst != NULL); 10198 sata_device.satadev_addr.cport = cport; 10199 sata_device.satadev_addr.pmport = 0; 10200 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 10201 sata_device.satadev_rev = SATA_DEVICE_REV; 10202 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 10203 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 10204 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 10205 if (sdinfo == NULL) { 10206 sata_log(sata_hba_inst, CE_WARN, 10207 "sata_test_atapi_packet_command: " 10208 "no device info for cport %d", 10209 sata_device.satadev_addr.cport); 10210 return; 10211 } 10212 10213 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 10214 spx->txlt_sata_hba_inst = sata_hba_inst; 10215 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 10216 spkt = sata_pkt_alloc(spx, NULL); 10217 if (spkt == NULL) { 10218 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10219 return; 10220 } 10221 /* address is needed now */ 10222 spkt->satapkt_device.satadev_addr = sata_device.satadev_addr; 10223 10224 /* 1024k buffer */ 10225 bp = sata_alloc_local_buffer(spx, 1024); 10226 if (bp == NULL) { 10227 sata_pkt_free(spx); 10228 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10229 sata_log(sata_hba_inst, CE_WARN, 10230 "sata_test_atapi_packet_command: " 10231 "cannot allocate data buffer"); 10232 return; 10233 } 10234 bp_mapin(bp); /* make data buffer accessible */ 10235 10236 scmd = &spkt->satapkt_cmd; 10237 ASSERT(scmd->satacmd_num_dma_cookies != 0); 10238 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 10239 10240 /* Use synchronous mode */ 10241 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 10242 10243 /* Synchronous mode, no callback - may be changed by the caller */ 10244 spkt->satapkt_comp = NULL; 10245 spkt->satapkt_time = sata_default_pkt_time; 10246 10247 /* Issue inquiry command - 6 bytes cdb, data transfer, read */ 10248 10249 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 10250 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 10251 10252 sata_atapi_packet_cmd_setup(scmd, sdinfo); 10253 10254 /* Set-up acdb. */ 10255 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 10256 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 10257 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */ 10258 scmd->satacmd_acdb[1] = 0x00; 10259 scmd->satacmd_acdb[2] = 0x00; 10260 scmd->satacmd_acdb[3] = 0x00; 10261 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry); 10262 scmd->satacmd_acdb[5] = 0x00; 10263 10264 sata_fixed_sense_data_preset( 10265 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 10266 10267 /* Transfer command to HBA */ 10268 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 10269 if (sata_hba_start(spx, &rval) != 0) { 10270 /* Pkt not accepted for execution */ 10271 sata_log(sata_hba_inst, CE_WARN, 10272 "sata_test_atapi_packet_command: " 10273 "Packet not accepted for execution - ret: %02x", rval); 10274 mutex_exit( 10275 &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 10276 goto cleanup; 10277 } 10278 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 10279 10280 if (spx->txlt_buf_dma_handle != NULL) { 10281 /* 10282 * Sync buffer. Handle is in usual place in translate struct. 10283 */ 10284 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 10285 DDI_DMA_SYNC_FORCPU); 10286 ASSERT(rval == DDI_SUCCESS); 10287 } 10288 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) { 10289 sata_log(sata_hba_inst, CE_WARN, 10290 "sata_test_atapi_packet_command: " 10291 "Packet completed successfully"); 10292 /* 10293 * Normal completion - show inquiry data 10294 */ 10295 sata_show_inqry_data((uint8_t *)bp->b_un.b_addr); 10296 } else { 10297 /* 10298 * Something went wrong - analyze return - check rqsense data 10299 */ 10300 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 10301 /* 10302 * ARQ data hopefull show something other than NO SENSE 10303 */ 10304 rqsp = scmd->satacmd_rqsense; 10305 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 10306 "ATAPI packet completion reason: %02x\n" 10307 "RQSENSE: %02x %02x %02x %02x %02x %02x " 10308 " %02x %02x %02x %02x %02x %02x " 10309 " %02x %02x %02x %02x %02x %02x\n", 10310 spkt->satapkt_reason, 10311 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 10312 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 10313 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 10314 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 10315 rqsp[16], rqsp[17]); 10316 } else { 10317 switch (spkt->satapkt_reason) { 10318 case SATA_PKT_PORT_ERROR: 10319 sata_log(sata_hba_inst, CE_WARN, 10320 "sata_test_atapi_packet_command: " 10321 "packet reason: port error\n"); 10322 break; 10323 10324 case SATA_PKT_TIMEOUT: 10325 sata_log(sata_hba_inst, CE_WARN, 10326 "sata_test_atapi_packet_command: " 10327 "packet reason: timeout\n"); 10328 break; 10329 10330 case SATA_PKT_ABORTED: 10331 sata_log(sata_hba_inst, CE_WARN, 10332 "sata_test_atapi_packet_command: " 10333 "packet reason: aborted\n"); 10334 break; 10335 10336 case SATA_PKT_RESET: 10337 sata_log(sata_hba_inst, CE_WARN, 10338 "sata_test_atapi_packet_command: " 10339 "packet reason: reset\n"); 10340 break; 10341 default: 10342 sata_log(sata_hba_inst, CE_WARN, 10343 "sata_test_atapi_packet_command: " 10344 "invalid packet reason: %02x\n", 10345 spkt->satapkt_reason); 10346 break; 10347 } 10348 } 10349 } 10350 cleanup: 10351 sata_free_local_buffer(spx); 10352 sata_pkt_free(spx); 10353 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10354 } 10355 10356 #endif /* SATA_DEBUG */ 10357 #endif /* 1 */ 10358 10359 10360 /* ************************** LOCAL HELPER FUNCTIONS *********************** */ 10361 10362 /* 10363 * Validate sata_tran info 10364 * SATA_FAILURE returns if structure is inconsistent or structure revision 10365 * does not match one used by the framework. 10366 * 10367 * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains 10368 * required function pointers. 10369 * Returns SATA_FAILURE otherwise. 10370 */ 10371 static int 10372 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran) 10373 { 10374 /* 10375 * SATA_TRAN_HBA_REV is the current (highest) revision number 10376 * of the SATA interface. 10377 */ 10378 if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) { 10379 sata_log(NULL, CE_WARN, 10380 "sata: invalid sata_hba_tran version %d for driver %s", 10381 sata_tran->sata_tran_hba_rev, ddi_driver_name(dip)); 10382 return (SATA_FAILURE); 10383 } 10384 10385 if (dip != sata_tran->sata_tran_hba_dip) { 10386 SATA_LOG_D((NULL, CE_WARN, 10387 "sata: inconsistent sata_tran_hba_dip " 10388 "%p / %p", sata_tran->sata_tran_hba_dip, dip)); 10389 return (SATA_FAILURE); 10390 } 10391 10392 if (sata_tran->sata_tran_probe_port == NULL || 10393 sata_tran->sata_tran_start == NULL || 10394 sata_tran->sata_tran_abort == NULL || 10395 sata_tran->sata_tran_reset_dport == NULL || 10396 sata_tran->sata_tran_hotplug_ops == NULL || 10397 sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL || 10398 sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate == 10399 NULL) { 10400 SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing " 10401 "required functions")); 10402 } 10403 return (SATA_SUCCESS); 10404 } 10405 10406 /* 10407 * Remove HBA instance from sata_hba_list. 10408 */ 10409 static void 10410 sata_remove_hba_instance(dev_info_t *dip) 10411 { 10412 sata_hba_inst_t *sata_hba_inst; 10413 10414 mutex_enter(&sata_mutex); 10415 for (sata_hba_inst = sata_hba_list; 10416 sata_hba_inst != (struct sata_hba_inst *)NULL; 10417 sata_hba_inst = sata_hba_inst->satahba_next) { 10418 if (sata_hba_inst->satahba_dip == dip) 10419 break; 10420 } 10421 10422 if (sata_hba_inst == (struct sata_hba_inst *)NULL) { 10423 #ifdef SATA_DEBUG 10424 cmn_err(CE_WARN, "sata_remove_hba_instance: " 10425 "unknown HBA instance\n"); 10426 #endif 10427 ASSERT(FALSE); 10428 } 10429 if (sata_hba_inst == sata_hba_list) { 10430 sata_hba_list = sata_hba_inst->satahba_next; 10431 if (sata_hba_list) { 10432 sata_hba_list->satahba_prev = 10433 (struct sata_hba_inst *)NULL; 10434 } 10435 if (sata_hba_inst == sata_hba_list_tail) { 10436 sata_hba_list_tail = NULL; 10437 } 10438 } else if (sata_hba_inst == sata_hba_list_tail) { 10439 sata_hba_list_tail = sata_hba_inst->satahba_prev; 10440 if (sata_hba_list_tail) { 10441 sata_hba_list_tail->satahba_next = 10442 (struct sata_hba_inst *)NULL; 10443 } 10444 } else { 10445 sata_hba_inst->satahba_prev->satahba_next = 10446 sata_hba_inst->satahba_next; 10447 sata_hba_inst->satahba_next->satahba_prev = 10448 sata_hba_inst->satahba_prev; 10449 } 10450 mutex_exit(&sata_mutex); 10451 } 10452 10453 /* 10454 * Probe all SATA ports of the specified HBA instance. 10455 * The assumption is that there are no target and attachment point minor nodes 10456 * created by the boot subsystems, so we do not need to prune device tree. 10457 * 10458 * This function is called only from sata_hba_attach(). It does not have to 10459 * be protected by controller mutex, because the hba_attached flag is not set 10460 * yet and no one would be touching this HBA instance other than this thread. 10461 * Determines if port is active and what type of the device is attached 10462 * (if any). Allocates necessary structures for each port. 10463 * 10464 * An AP (Attachement Point) node is created for each SATA device port even 10465 * when there is no device attached. 10466 */ 10467 10468 static void 10469 sata_probe_ports(sata_hba_inst_t *sata_hba_inst) 10470 { 10471 dev_info_t *dip = SATA_DIP(sata_hba_inst); 10472 int ncport; 10473 sata_cport_info_t *cportinfo; 10474 sata_drive_info_t *drive; 10475 sata_device_t sata_device; 10476 int rval; 10477 dev_t minor_number; 10478 char name[16]; 10479 clock_t start_time, cur_time; 10480 10481 /* 10482 * Probe controller ports first, to find port status and 10483 * any port multiplier attached. 10484 */ 10485 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 10486 /* allocate cport structure */ 10487 cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP); 10488 ASSERT(cportinfo != NULL); 10489 mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL); 10490 10491 mutex_enter(&cportinfo->cport_mutex); 10492 10493 cportinfo->cport_addr.cport = ncport; 10494 cportinfo->cport_addr.pmport = 0; 10495 cportinfo->cport_addr.qual = SATA_ADDR_CPORT; 10496 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 10497 cportinfo->cport_state |= SATA_STATE_PROBING; 10498 SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo; 10499 10500 /* 10501 * Regardless if a port is usable or not, create 10502 * an attachment point 10503 */ 10504 mutex_exit(&cportinfo->cport_mutex); 10505 minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip), 10506 ncport, 0, SATA_ADDR_CPORT); 10507 (void) sprintf(name, "%d", ncport); 10508 if (ddi_create_minor_node(dip, name, S_IFCHR, 10509 minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) != 10510 DDI_SUCCESS) { 10511 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: " 10512 "cannot create SATA attachment point for port %d", 10513 ncport); 10514 } 10515 10516 /* Probe port */ 10517 start_time = ddi_get_lbolt(); 10518 reprobe_cport: 10519 sata_device.satadev_addr.cport = ncport; 10520 sata_device.satadev_addr.pmport = 0; 10521 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 10522 sata_device.satadev_rev = SATA_DEVICE_REV; 10523 10524 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 10525 (dip, &sata_device); 10526 10527 mutex_enter(&cportinfo->cport_mutex); 10528 cportinfo->cport_scr = sata_device.satadev_scr; 10529 if (rval != SATA_SUCCESS) { 10530 /* Something went wrong? Fail the port */ 10531 cportinfo->cport_state = SATA_PSTATE_FAILED; 10532 mutex_exit(&cportinfo->cport_mutex); 10533 continue; 10534 } 10535 cportinfo->cport_state &= ~SATA_STATE_PROBING; 10536 cportinfo->cport_state |= SATA_STATE_PROBED; 10537 cportinfo->cport_dev_type = sata_device.satadev_type; 10538 10539 cportinfo->cport_state |= SATA_STATE_READY; 10540 if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) { 10541 mutex_exit(&cportinfo->cport_mutex); 10542 continue; 10543 } 10544 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 10545 /* 10546 * There is some device attached. 10547 * Allocate device info structure 10548 */ 10549 if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) { 10550 mutex_exit(&cportinfo->cport_mutex); 10551 SATA_CPORTINFO_DRV_INFO(cportinfo) = 10552 kmem_zalloc(sizeof (sata_drive_info_t), 10553 KM_SLEEP); 10554 mutex_enter(&cportinfo->cport_mutex); 10555 } 10556 drive = SATA_CPORTINFO_DRV_INFO(cportinfo); 10557 drive->satadrv_addr = cportinfo->cport_addr; 10558 drive->satadrv_addr.qual = SATA_ADDR_DCPORT; 10559 drive->satadrv_type = cportinfo->cport_dev_type; 10560 drive->satadrv_state = SATA_STATE_UNKNOWN; 10561 10562 mutex_exit(&cportinfo->cport_mutex); 10563 if (sata_add_device(dip, sata_hba_inst, &sata_device) != 10564 SATA_SUCCESS) { 10565 /* 10566 * Plugged device was not correctly identified. 10567 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT 10568 */ 10569 cur_time = ddi_get_lbolt(); 10570 if ((cur_time - start_time) < 10571 drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) { 10572 /* sleep for a while */ 10573 delay(drv_usectohz( 10574 SATA_DEV_RETRY_DLY)); 10575 goto reprobe_cport; 10576 } 10577 } 10578 } else { /* SATA_DTYPE_PMULT */ 10579 mutex_exit(&cportinfo->cport_mutex); 10580 10581 /* Allocate sata_pmult_info and sata_pmport_info */ 10582 if (sata_alloc_pmult(sata_hba_inst, &sata_device) != 10583 SATA_SUCCESS) 10584 continue; 10585 10586 /* Log the information of the port multiplier */ 10587 sata_show_pmult_info(sata_hba_inst, &sata_device); 10588 10589 /* Probe its pmports */ 10590 sata_probe_pmports(sata_hba_inst, ncport); 10591 } 10592 } 10593 } 10594 10595 /* 10596 * Probe all device ports behind a port multiplier. 10597 * 10598 * PMult-related structure should be allocated before by sata_alloc_pmult(). 10599 * 10600 * NOTE1: Only called from sata_probe_ports() 10601 * NOTE2: No mutex should be hold. 10602 */ 10603 static void 10604 sata_probe_pmports(sata_hba_inst_t *sata_hba_inst, uint8_t ncport) 10605 { 10606 dev_info_t *dip = SATA_DIP(sata_hba_inst); 10607 sata_pmult_info_t *pmultinfo = NULL; 10608 sata_pmport_info_t *pmportinfo = NULL; 10609 sata_drive_info_t *drive = NULL; 10610 sata_device_t sata_device; 10611 10612 clock_t start_time, cur_time; 10613 int npmport; 10614 int rval; 10615 10616 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, ncport); 10617 10618 /* Probe Port Multiplier ports */ 10619 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; npmport++) { 10620 pmportinfo = pmultinfo->pmult_dev_port[npmport]; 10621 start_time = ddi_get_lbolt(); 10622 reprobe_pmport: 10623 sata_device.satadev_addr.cport = ncport; 10624 sata_device.satadev_addr.pmport = npmport; 10625 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT; 10626 sata_device.satadev_rev = SATA_DEVICE_REV; 10627 10628 /* Let HBA driver probe it. */ 10629 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 10630 (dip, &sata_device); 10631 mutex_enter(&pmportinfo->pmport_mutex); 10632 10633 pmportinfo->pmport_scr = sata_device.satadev_scr; 10634 10635 if (rval != SATA_SUCCESS) { 10636 pmportinfo->pmport_state = 10637 SATA_PSTATE_FAILED; 10638 mutex_exit(&pmportinfo->pmport_mutex); 10639 continue; 10640 } 10641 pmportinfo->pmport_state &= ~SATA_STATE_PROBING; 10642 pmportinfo->pmport_state |= SATA_STATE_PROBED; 10643 pmportinfo->pmport_dev_type = sata_device.satadev_type; 10644 10645 pmportinfo->pmport_state |= SATA_STATE_READY; 10646 if (pmportinfo->pmport_dev_type == 10647 SATA_DTYPE_NONE) { 10648 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 10649 "no device found at port %d:%d", ncport, npmport); 10650 mutex_exit(&pmportinfo->pmport_mutex); 10651 continue; 10652 } 10653 /* Port multipliers cannot be chained */ 10654 ASSERT(pmportinfo->pmport_dev_type != SATA_DTYPE_PMULT); 10655 /* 10656 * There is something attached to Port 10657 * Multiplier device port 10658 * Allocate device info structure 10659 */ 10660 if (pmportinfo->pmport_sata_drive == NULL) { 10661 mutex_exit(&pmportinfo->pmport_mutex); 10662 pmportinfo->pmport_sata_drive = 10663 kmem_zalloc(sizeof (sata_drive_info_t), KM_SLEEP); 10664 mutex_enter(&pmportinfo->pmport_mutex); 10665 } 10666 drive = pmportinfo->pmport_sata_drive; 10667 drive->satadrv_addr.cport = pmportinfo->pmport_addr.cport; 10668 drive->satadrv_addr.pmport = npmport; 10669 drive->satadrv_addr.qual = SATA_ADDR_DPMPORT; 10670 drive->satadrv_type = pmportinfo-> pmport_dev_type; 10671 drive->satadrv_state = SATA_STATE_UNKNOWN; 10672 10673 mutex_exit(&pmportinfo->pmport_mutex); 10674 rval = sata_add_device(dip, sata_hba_inst, &sata_device); 10675 10676 if (rval != SATA_SUCCESS) { 10677 /* 10678 * Plugged device was not correctly identified. 10679 * Retry, within the SATA_DEV_IDENTIFY_TIMEOUT 10680 */ 10681 cur_time = ddi_get_lbolt(); 10682 if ((cur_time - start_time) < drv_usectohz( 10683 SATA_DEV_IDENTIFY_TIMEOUT)) { 10684 /* sleep for a while */ 10685 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 10686 goto reprobe_pmport; 10687 } 10688 } 10689 } 10690 } 10691 10692 /* 10693 * Add SATA device for specified HBA instance & port (SCSI target 10694 * device nodes). 10695 * This function is called (indirectly) only from sata_hba_attach(). 10696 * A target node is created when there is a supported type device attached, 10697 * but may be removed if it cannot be put online. 10698 * 10699 * This function cannot be called from an interrupt context. 10700 * 10701 * Create target nodes for disk, CD/DVD, Tape and ATAPI disk devices 10702 * 10703 * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when 10704 * device identification failed - adding a device could be retried. 10705 * 10706 */ 10707 static int 10708 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, 10709 sata_device_t *sata_device) 10710 { 10711 sata_cport_info_t *cportinfo; 10712 sata_pmult_info_t *pminfo; 10713 sata_pmport_info_t *pmportinfo; 10714 dev_info_t *cdip; /* child dip */ 10715 sata_address_t *saddr = &sata_device->satadev_addr; 10716 uint8_t cport, pmport; 10717 int rval; 10718 10719 cport = saddr->cport; 10720 pmport = saddr->pmport; 10721 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 10722 ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE); 10723 10724 /* 10725 * Some device is attached to a controller port. 10726 * We rely on controllers distinquishing between no-device, 10727 * attached port multiplier and other kind of attached device. 10728 * We need to get Identify Device data and determine 10729 * positively the dev type before trying to attach 10730 * the target driver. 10731 */ 10732 sata_device->satadev_rev = SATA_DEVICE_REV; 10733 switch (saddr->qual) { 10734 case SATA_ADDR_CPORT: 10735 /* 10736 * Add a non-port-multiplier device at controller port. 10737 */ 10738 saddr->qual = SATA_ADDR_DCPORT; 10739 10740 rval = sata_probe_device(sata_hba_inst, sata_device); 10741 if (rval != SATA_SUCCESS || 10742 sata_device->satadev_type == SATA_DTYPE_UNKNOWN) 10743 return (SATA_FAILURE); 10744 10745 mutex_enter(&cportinfo->cport_mutex); 10746 sata_show_drive_info(sata_hba_inst, 10747 SATA_CPORTINFO_DRV_INFO(cportinfo)); 10748 10749 if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) { 10750 /* 10751 * Could not determine device type or 10752 * a device is not supported. 10753 * Degrade this device to unknown. 10754 */ 10755 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 10756 mutex_exit(&cportinfo->cport_mutex); 10757 return (SATA_SUCCESS); 10758 } 10759 cportinfo->cport_dev_type = sata_device->satadev_type; 10760 cportinfo->cport_tgtnode_clean = B_TRUE; 10761 mutex_exit(&cportinfo->cport_mutex); 10762 10763 /* 10764 * Initialize device to the desired state. Even if it 10765 * fails, the device will still attach but syslog 10766 * will show the warning. 10767 */ 10768 if (sata_initialize_device(sata_hba_inst, 10769 SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) { 10770 /* Retry */ 10771 rval = sata_initialize_device(sata_hba_inst, 10772 SATA_CPORTINFO_DRV_INFO(cportinfo)); 10773 10774 if (rval == SATA_RETRY) 10775 sata_log(sata_hba_inst, CE_WARN, 10776 "SATA device at port %d - " 10777 "default device features could not be set." 10778 " Device may not operate as expected.", 10779 cport); 10780 } 10781 10782 cdip = sata_create_target_node(pdip, sata_hba_inst, saddr); 10783 if (cdip == NULL) { 10784 /* 10785 * Attaching target node failed. 10786 * We retain sata_drive_info structure... 10787 */ 10788 return (SATA_SUCCESS); 10789 } 10790 10791 mutex_enter(&cportinfo->cport_mutex); 10792 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 10793 satadrv_state = SATA_STATE_READY; 10794 mutex_exit(&cportinfo->cport_mutex); 10795 10796 break; 10797 10798 case SATA_ADDR_PMPORT: 10799 saddr->qual = SATA_ADDR_DPMPORT; 10800 10801 mutex_enter(&cportinfo->cport_mutex); 10802 /* It must be a Port Multiplier at the controller port */ 10803 ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT); 10804 10805 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 10806 pmportinfo = pminfo->pmult_dev_port[saddr->pmport]; 10807 mutex_exit(&cportinfo->cport_mutex); 10808 10809 rval = sata_probe_device(sata_hba_inst, sata_device); 10810 if (rval != SATA_SUCCESS || 10811 sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 10812 return (SATA_FAILURE); 10813 } 10814 10815 mutex_enter(&pmportinfo->pmport_mutex); 10816 sata_show_drive_info(sata_hba_inst, 10817 SATA_PMPORTINFO_DRV_INFO(pmportinfo)); 10818 10819 if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) { 10820 /* 10821 * Could not determine device type. 10822 * Degrade this device to unknown. 10823 */ 10824 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN; 10825 mutex_exit(&pmportinfo->pmport_mutex); 10826 return (SATA_SUCCESS); 10827 } 10828 pmportinfo->pmport_dev_type = sata_device->satadev_type; 10829 pmportinfo->pmport_tgtnode_clean = B_TRUE; 10830 mutex_exit(&pmportinfo->pmport_mutex); 10831 10832 /* 10833 * Initialize device to the desired state. 10834 * Even if it fails, the device will still 10835 * attach but syslog will show the warning. 10836 */ 10837 if (sata_initialize_device(sata_hba_inst, 10838 pmportinfo->pmport_sata_drive) != SATA_SUCCESS) { 10839 /* Retry */ 10840 rval = sata_initialize_device(sata_hba_inst, 10841 pmportinfo->pmport_sata_drive); 10842 10843 if (rval == SATA_RETRY) 10844 sata_log(sata_hba_inst, CE_WARN, 10845 "SATA device at port %d:%d - " 10846 "default device features could not be set." 10847 " Device may not operate as expected.", 10848 cport, pmport); 10849 } 10850 10851 cdip = sata_create_target_node(pdip, sata_hba_inst, saddr); 10852 if (cdip == NULL) { 10853 /* 10854 * Attaching target node failed. 10855 * We retain sata_drive_info structure... 10856 */ 10857 return (SATA_SUCCESS); 10858 } 10859 mutex_enter(&pmportinfo->pmport_mutex); 10860 pmportinfo->pmport_sata_drive->satadrv_state |= 10861 SATA_STATE_READY; 10862 mutex_exit(&pmportinfo->pmport_mutex); 10863 10864 break; 10865 10866 default: 10867 return (SATA_FAILURE); 10868 } 10869 10870 return (SATA_SUCCESS); 10871 } 10872 10873 /* 10874 * Clean up target node at specific address. 10875 * 10876 * NOTE: No Mutex should be hold. 10877 */ 10878 static int 10879 sata_offline_device(sata_hba_inst_t *sata_hba_inst, 10880 sata_device_t *sata_device, sata_drive_info_t *sdinfo) 10881 { 10882 uint8_t cport, pmport, qual; 10883 dev_info_t *tdip; 10884 10885 cport = sata_device->satadev_addr.cport; 10886 pmport = sata_device->satadev_addr.pmport; 10887 qual = sata_device->satadev_addr.qual; 10888 10889 if (qual == SATA_ADDR_DCPORT) { 10890 SATA_LOG_D((sata_hba_inst, CE_WARN, 10891 "sata_hba_ioctl: disconnect device at port %d", cport)); 10892 } else { 10893 SATA_LOG_D((sata_hba_inst, CE_WARN, 10894 "sata_hba_ioctl: disconnect device at port %d:%d", 10895 cport, pmport)); 10896 } 10897 10898 /* We are addressing attached device, not a port */ 10899 sata_device->satadev_addr.qual = 10900 sdinfo->satadrv_addr.qual; 10901 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 10902 &sata_device->satadev_addr); 10903 if (tdip != NULL && ndi_devi_offline(tdip, 10904 NDI_DEVI_REMOVE) != NDI_SUCCESS) { 10905 /* 10906 * Problem : 10907 * The target node remained attached. 10908 * This happens when the device file was open 10909 * or a node was waiting for resources. 10910 * Cannot do anything about it. 10911 */ 10912 if (qual == SATA_ADDR_DCPORT) { 10913 SATA_LOG_D((sata_hba_inst, CE_WARN, 10914 "sata_hba_ioctl: disconnect: could " 10915 "not unconfigure device before " 10916 "disconnecting the SATA port %d", 10917 cport)); 10918 } else { 10919 SATA_LOG_D((sata_hba_inst, CE_WARN, 10920 "sata_hba_ioctl: disconnect: could " 10921 "not unconfigure device before " 10922 "disconnecting the SATA port %d:%d", 10923 cport, pmport)); 10924 } 10925 /* 10926 * Set DEVICE REMOVED state in the target 10927 * node. It will prevent access to the device 10928 * even when a new device is attached, until 10929 * the old target node is released, removed and 10930 * recreated for a new device. 10931 */ 10932 sata_set_device_removed(tdip); 10933 10934 /* 10935 * Instruct event daemon to try the target 10936 * node cleanup later. 10937 */ 10938 sata_set_target_node_cleanup( 10939 sata_hba_inst, &sata_device->satadev_addr); 10940 } 10941 10942 10943 return (SATA_SUCCESS); 10944 } 10945 10946 10947 /* 10948 * Create scsi target node for attached device, create node properties and 10949 * attach the node. 10950 * The node could be removed if the device onlining fails. 10951 * 10952 * A dev_info_t pointer is returned if operation is successful, NULL is 10953 * returned otherwise. 10954 */ 10955 10956 static dev_info_t * 10957 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst, 10958 sata_address_t *sata_addr) 10959 { 10960 dev_info_t *cdip = NULL; 10961 int rval; 10962 char *nname = NULL; 10963 char **compatible = NULL; 10964 int ncompatible; 10965 struct scsi_inquiry inq; 10966 sata_device_t sata_device; 10967 sata_drive_info_t *sdinfo; 10968 int target; 10969 int i; 10970 10971 sata_device.satadev_rev = SATA_DEVICE_REV; 10972 sata_device.satadev_addr = *sata_addr; 10973 10974 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport))); 10975 10976 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 10977 10978 target = SATA_TO_SCSI_TARGET(sata_addr->cport, 10979 sata_addr->pmport, sata_addr->qual); 10980 10981 if (sdinfo == NULL) { 10982 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 10983 sata_addr->cport))); 10984 SATA_LOG_D((sata_hba_inst, CE_WARN, 10985 "sata_create_target_node: no sdinfo for target %x", 10986 target)); 10987 return (NULL); 10988 } 10989 10990 /* 10991 * create or get scsi inquiry data, expected by 10992 * scsi_hba_nodename_compatible_get() 10993 * SATA hard disks get Identify Data translated into Inguiry Data. 10994 * ATAPI devices respond directly to Inquiry request. 10995 */ 10996 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 10997 sata_identdev_to_inquiry(sata_hba_inst, sdinfo, 10998 (uint8_t *)&inq); 10999 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 11000 sata_addr->cport))); 11001 } else { /* Assume supported ATAPI device */ 11002 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 11003 sata_addr->cport))); 11004 if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr, 11005 &inq) == SATA_FAILURE) 11006 return (NULL); 11007 /* 11008 * Save supported ATAPI transport version 11009 */ 11010 sdinfo->satadrv_atapi_trans_ver = 11011 SATA_ATAPI_TRANS_VERSION(&inq); 11012 } 11013 11014 /* determine the node name and compatible */ 11015 scsi_hba_nodename_compatible_get(&inq, NULL, 11016 inq.inq_dtype, NULL, &nname, &compatible, &ncompatible); 11017 11018 #ifdef SATA_DEBUG 11019 if (sata_debug_flags & SATA_DBG_NODES) { 11020 if (nname == NULL) { 11021 cmn_err(CE_NOTE, "sata_create_target_node: " 11022 "cannot determine nodename for target %d\n", 11023 target); 11024 } else { 11025 cmn_err(CE_WARN, "sata_create_target_node: " 11026 "target %d nodename: %s\n", target, nname); 11027 } 11028 if (compatible == NULL) { 11029 cmn_err(CE_WARN, 11030 "sata_create_target_node: no compatible name\n"); 11031 } else { 11032 for (i = 0; i < ncompatible; i++) { 11033 cmn_err(CE_WARN, "sata_create_target_node: " 11034 "compatible name: %s\n", compatible[i]); 11035 } 11036 } 11037 } 11038 #endif 11039 11040 /* if nodename can't be determined, log error and exit */ 11041 if (nname == NULL) { 11042 SATA_LOG_D((sata_hba_inst, CE_WARN, 11043 "sata_create_target_node: cannot determine nodename " 11044 "for target %d\n", target)); 11045 scsi_hba_nodename_compatible_free(nname, compatible); 11046 return (NULL); 11047 } 11048 /* 11049 * Create scsi target node 11050 */ 11051 ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip); 11052 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 11053 "device-type", "scsi"); 11054 11055 if (rval != DDI_PROP_SUCCESS) { 11056 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 11057 "updating device_type prop failed %d", rval)); 11058 goto fail; 11059 } 11060 11061 /* 11062 * Create target node properties: target & lun 11063 */ 11064 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target); 11065 if (rval != DDI_PROP_SUCCESS) { 11066 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 11067 "updating target prop failed %d", rval)); 11068 goto fail; 11069 } 11070 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0); 11071 if (rval != DDI_PROP_SUCCESS) { 11072 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 11073 "updating target prop failed %d", rval)); 11074 goto fail; 11075 } 11076 11077 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 11078 /* 11079 * Add "variant" property 11080 */ 11081 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 11082 "variant", "atapi"); 11083 if (rval != DDI_PROP_SUCCESS) { 11084 SATA_LOG_D((sata_hba_inst, CE_WARN, 11085 "sata_create_target_node: variant atapi " 11086 "property could not be created: %d", rval)); 11087 goto fail; 11088 } 11089 } 11090 /* decorate the node with compatible */ 11091 if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible", 11092 compatible, ncompatible) != DDI_PROP_SUCCESS) { 11093 SATA_LOG_D((sata_hba_inst, CE_WARN, 11094 "sata_create_target_node: FAIL compatible props cdip 0x%p", 11095 (void *)cdip)); 11096 goto fail; 11097 } 11098 11099 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 11100 /* 11101 * Add "sata-phy" property 11102 */ 11103 if (ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "sata-phy", 11104 (int)sata_addr->cport) != DDI_PROP_SUCCESS) { 11105 SATA_LOG_D((sata_hba_inst, CE_WARN, 11106 "sata_create_target_node: failed to create " 11107 "\"sata-phy\" property: port %d", 11108 sata_addr->cport)); 11109 } 11110 } 11111 11112 11113 /* 11114 * Now, try to attach the driver. If probing of the device fails, 11115 * the target node may be removed 11116 */ 11117 rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH); 11118 11119 scsi_hba_nodename_compatible_free(nname, compatible); 11120 11121 if (rval == NDI_SUCCESS) 11122 return (cdip); 11123 11124 /* target node was removed - are we sure? */ 11125 return (NULL); 11126 11127 fail: 11128 scsi_hba_nodename_compatible_free(nname, compatible); 11129 ddi_prop_remove_all(cdip); 11130 rval = ndi_devi_free(cdip); 11131 if (rval != NDI_SUCCESS) { 11132 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 11133 "node removal failed %d", rval)); 11134 } 11135 sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: " 11136 "cannot create target node for SATA device at port %d", 11137 sata_addr->cport); 11138 return (NULL); 11139 } 11140 11141 /* 11142 * Remove a target node. 11143 */ 11144 static void 11145 sata_remove_target_node(sata_hba_inst_t *sata_hba_inst, 11146 sata_address_t *sata_addr) 11147 { 11148 dev_info_t *tdip; 11149 uint8_t cport = sata_addr->cport; 11150 uint8_t pmport = sata_addr->pmport; 11151 uint8_t qual = sata_addr->qual; 11152 11153 /* Note the sata daemon uses the address of the port/pmport */ 11154 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT); 11155 11156 /* Remove target node */ 11157 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), cport, pmport); 11158 if (tdip != NULL) { 11159 /* 11160 * Target node exists. Unconfigure device 11161 * then remove the target node (one ndi 11162 * operation). 11163 */ 11164 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) { 11165 /* 11166 * PROBLEM - no device, but target node remained. This 11167 * happens when the file was open or node was waiting 11168 * for resources. 11169 */ 11170 SATA_LOG_D((sata_hba_inst, CE_WARN, 11171 "sata_remove_target_node: " 11172 "Failed to remove target node for " 11173 "detached SATA device.")); 11174 /* 11175 * Set target node state to DEVI_DEVICE_REMOVED. But 11176 * re-check first that the node still exists. 11177 */ 11178 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 11179 cport, pmport); 11180 if (tdip != NULL) { 11181 sata_set_device_removed(tdip); 11182 /* 11183 * Instruct event daemon to retry the cleanup 11184 * later. 11185 */ 11186 sata_set_target_node_cleanup(sata_hba_inst, 11187 sata_addr); 11188 } 11189 } 11190 11191 if (qual == SATA_ADDR_CPORT) 11192 sata_log(sata_hba_inst, CE_WARN, 11193 "SATA device detached at port %d", cport); 11194 else 11195 sata_log(sata_hba_inst, CE_WARN, 11196 "SATA device detached at port %d:%d", 11197 cport, pmport); 11198 } 11199 #ifdef SATA_DEBUG 11200 else { 11201 if (qual == SATA_ADDR_CPORT) 11202 sata_log(sata_hba_inst, CE_WARN, 11203 "target node not found at port %d", cport); 11204 else 11205 sata_log(sata_hba_inst, CE_WARN, 11206 "target node not found at port %d:%d", 11207 cport, pmport); 11208 } 11209 #endif 11210 } 11211 11212 11213 /* 11214 * Re-probe sata port, check for a device and attach info 11215 * structures when necessary. Identify Device data is fetched, if possible. 11216 * Assumption: sata address is already validated. 11217 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of 11218 * the presence of a device and its type. 11219 * 11220 * flag arg specifies that the function should try multiple times to identify 11221 * device type and to initialize it, or it should return immediately on failure. 11222 * SATA_DEV_IDENTIFY_RETRY - retry 11223 * SATA_DEV_IDENTIFY_NORETRY - no retry 11224 * 11225 * SATA_FAILURE is returned if one of the operations failed. 11226 * 11227 * This function cannot be called in interrupt context - it may sleep. 11228 * 11229 * Note: Port multiplier is supported. 11230 */ 11231 static int 11232 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 11233 int flag) 11234 { 11235 sata_cport_info_t *cportinfo; 11236 sata_pmult_info_t *pmultinfo; 11237 sata_drive_info_t *sdinfo, *osdinfo; 11238 boolean_t init_device = B_FALSE; 11239 int prev_device_type = SATA_DTYPE_NONE; 11240 int prev_device_settings = 0; 11241 int prev_device_state = 0; 11242 clock_t start_time; 11243 int retry = B_FALSE; 11244 uint8_t cport = sata_device->satadev_addr.cport; 11245 int rval_probe, rval_init; 11246 11247 /* 11248 * If target is pmport, sata_reprobe_pmport() will handle it. 11249 */ 11250 if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT || 11251 sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) 11252 return (sata_reprobe_pmport(sata_hba_inst, sata_device, flag)); 11253 11254 /* We only care about host sata cport for now */ 11255 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 11256 sata_device->satadev_addr.cport); 11257 11258 /* 11259 * If a port multiplier was previously attached (we have no idea it 11260 * still there or not), sata_reprobe_pmult() will handle it. 11261 */ 11262 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) 11263 return (sata_reprobe_pmult(sata_hba_inst, sata_device, flag)); 11264 11265 /* Store sata_drive_info when a non-pmult device was attached. */ 11266 osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 11267 if (osdinfo != NULL) { 11268 /* 11269 * We are re-probing port with a previously attached device. 11270 * Save previous device type and settings. 11271 */ 11272 prev_device_type = cportinfo->cport_dev_type; 11273 prev_device_settings = osdinfo->satadrv_settings; 11274 prev_device_state = osdinfo->satadrv_state; 11275 } 11276 if (flag == SATA_DEV_IDENTIFY_RETRY) { 11277 start_time = ddi_get_lbolt(); 11278 retry = B_TRUE; 11279 } 11280 retry_probe: 11281 11282 /* probe port */ 11283 mutex_enter(&cportinfo->cport_mutex); 11284 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 11285 cportinfo->cport_state |= SATA_STATE_PROBING; 11286 mutex_exit(&cportinfo->cport_mutex); 11287 11288 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11289 (SATA_DIP(sata_hba_inst), sata_device); 11290 11291 mutex_enter(&cportinfo->cport_mutex); 11292 if (rval_probe != SATA_SUCCESS) { 11293 cportinfo->cport_state = SATA_PSTATE_FAILED; 11294 mutex_exit(&cportinfo->cport_mutex); 11295 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: " 11296 "SATA port %d probing failed", 11297 cportinfo->cport_addr.cport)); 11298 return (SATA_FAILURE); 11299 } 11300 11301 /* 11302 * update sata port state and set device type 11303 */ 11304 sata_update_port_info(sata_hba_inst, sata_device); 11305 cportinfo->cport_state &= ~SATA_STATE_PROBING; 11306 11307 /* 11308 * Sanity check - Port is active? Is the link active? 11309 * Is there any device attached? 11310 */ 11311 if ((cportinfo->cport_state & 11312 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 11313 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 11314 SATA_PORT_DEVLINK_UP) { 11315 /* 11316 * Port in non-usable state or no link active/no device. 11317 * Free info structure if necessary (direct attached drive 11318 * only, for now! 11319 */ 11320 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 11321 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 11322 /* Add here differentiation for device attached or not */ 11323 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 11324 mutex_exit(&cportinfo->cport_mutex); 11325 if (sdinfo != NULL) 11326 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11327 return (SATA_SUCCESS); 11328 } 11329 11330 cportinfo->cport_state |= SATA_STATE_READY; 11331 cportinfo->cport_state |= SATA_STATE_PROBED; 11332 11333 cportinfo->cport_dev_type = sata_device->satadev_type; 11334 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 11335 11336 /* 11337 * If we are re-probing the port, there may be 11338 * sata_drive_info structure attached 11339 */ 11340 if (sata_device->satadev_type == SATA_DTYPE_NONE) { 11341 11342 /* 11343 * There is no device, so remove device info structure, 11344 * if necessary. 11345 */ 11346 /* Device change: Drive -> None */ 11347 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 11348 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 11349 if (sdinfo != NULL) { 11350 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11351 sata_log(sata_hba_inst, CE_WARN, 11352 "SATA device detached " 11353 "from port %d", cportinfo->cport_addr.cport); 11354 } 11355 mutex_exit(&cportinfo->cport_mutex); 11356 return (SATA_SUCCESS); 11357 11358 } 11359 11360 if (sata_device->satadev_type != SATA_DTYPE_PMULT) { 11361 11362 /* Device (may) change: Drive -> Drive */ 11363 if (sdinfo == NULL) { 11364 /* 11365 * There is some device attached, but there is 11366 * no sata_drive_info structure - allocate one 11367 */ 11368 mutex_exit(&cportinfo->cport_mutex); 11369 sdinfo = kmem_zalloc( 11370 sizeof (sata_drive_info_t), KM_SLEEP); 11371 mutex_enter(&cportinfo->cport_mutex); 11372 /* 11373 * Recheck, that the port state did not change when we 11374 * released mutex. 11375 */ 11376 if (cportinfo->cport_state & SATA_STATE_READY) { 11377 SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo; 11378 sdinfo->satadrv_addr = cportinfo->cport_addr; 11379 sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT; 11380 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 11381 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 11382 } else { 11383 /* 11384 * Port is not in ready state, we 11385 * cannot attach a device. 11386 */ 11387 mutex_exit(&cportinfo->cport_mutex); 11388 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11389 return (SATA_SUCCESS); 11390 } 11391 /* 11392 * Since we are adding device, presumably new one, 11393 * indicate that it should be initalized, 11394 * as well as some internal framework states). 11395 */ 11396 init_device = B_TRUE; 11397 } 11398 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 11399 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual; 11400 } else { 11401 /* Device change: Drive -> PMult */ 11402 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 11403 if (sdinfo != NULL) { 11404 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11405 sata_log(sata_hba_inst, CE_WARN, 11406 "SATA device detached " 11407 "from port %d", cportinfo->cport_addr.cport); 11408 } 11409 11410 sata_log(sata_hba_inst, CE_WARN, 11411 "SATA port multiplier detected at port %d", 11412 cportinfo->cport_addr.cport); 11413 11414 mutex_exit(&cportinfo->cport_mutex); 11415 if (sata_alloc_pmult(sata_hba_inst, sata_device) != 11416 SATA_SUCCESS) 11417 return (SATA_FAILURE); 11418 sata_show_pmult_info(sata_hba_inst, sata_device); 11419 mutex_enter(&cportinfo->cport_mutex); 11420 11421 /* 11422 * Mark all the port multiplier port behind the port 11423 * multiplier behind with link events, so that the sata daemon 11424 * will update their status. 11425 */ 11426 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 11427 pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET; 11428 mutex_exit(&cportinfo->cport_mutex); 11429 return (SATA_SUCCESS); 11430 } 11431 mutex_exit(&cportinfo->cport_mutex); 11432 11433 /* 11434 * Figure out what kind of device we are really 11435 * dealing with. Failure of identifying device does not fail this 11436 * function. 11437 */ 11438 rval_probe = sata_probe_device(sata_hba_inst, sata_device); 11439 rval_init = SATA_FAILURE; 11440 mutex_enter(&cportinfo->cport_mutex); 11441 if (rval_probe == SATA_SUCCESS) { 11442 /* 11443 * If we are dealing with the same type of a device as before, 11444 * restore its settings flags. 11445 */ 11446 if (osdinfo != NULL && 11447 sata_device->satadev_type == prev_device_type) 11448 sdinfo->satadrv_settings = prev_device_settings; 11449 11450 mutex_exit(&cportinfo->cport_mutex); 11451 rval_init = SATA_SUCCESS; 11452 /* Set initial device features, if necessary */ 11453 if (init_device == B_TRUE) { 11454 rval_init = sata_initialize_device(sata_hba_inst, 11455 sdinfo); 11456 } 11457 if (rval_init == SATA_SUCCESS) 11458 return (rval_init); 11459 /* else we will retry if retry was asked for */ 11460 11461 } else { 11462 /* 11463 * If there was some device info before we probe the device, 11464 * restore previous device setting, so we can retry from scratch 11465 * later. Providing, of course, that device has not disapear 11466 * during probing process. 11467 */ 11468 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 11469 if (osdinfo != NULL) { 11470 cportinfo->cport_dev_type = prev_device_type; 11471 sdinfo->satadrv_type = prev_device_type; 11472 sdinfo->satadrv_state = prev_device_state; 11473 } 11474 } else { 11475 /* device is gone */ 11476 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11477 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 11478 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 11479 mutex_exit(&cportinfo->cport_mutex); 11480 return (SATA_SUCCESS); 11481 } 11482 mutex_exit(&cportinfo->cport_mutex); 11483 } 11484 11485 if (retry) { 11486 clock_t cur_time = ddi_get_lbolt(); 11487 /* 11488 * A device was not successfully identified or initialized. 11489 * Track retry time for device identification. 11490 */ 11491 if ((cur_time - start_time) < 11492 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 11493 /* sleep for a while */ 11494 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 11495 goto retry_probe; 11496 } 11497 /* else no more retries */ 11498 mutex_enter(&cportinfo->cport_mutex); 11499 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 11500 if (rval_init == SATA_RETRY) { 11501 /* 11502 * Setting drive features have failed, but 11503 * because the drive is still accessible, 11504 * keep it and emit a warning message. 11505 */ 11506 sata_log(sata_hba_inst, CE_WARN, 11507 "SATA device at port %d - desired " 11508 "drive features could not be set. " 11509 "Device may not operate as expected.", 11510 cportinfo->cport_addr.cport); 11511 } else { 11512 SATA_CPORTINFO_DRV_INFO(cportinfo)-> 11513 satadrv_state = SATA_DSTATE_FAILED; 11514 } 11515 } 11516 mutex_exit(&cportinfo->cport_mutex); 11517 } 11518 return (SATA_SUCCESS); 11519 } 11520 11521 /* 11522 * Reprobe a controller port that connected to a port multiplier. 11523 * 11524 * NOTE: No Mutex should be hold. 11525 */ 11526 static int 11527 sata_reprobe_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 11528 int flag) 11529 { 11530 _NOTE(ARGUNUSED(flag)) 11531 sata_cport_info_t *cportinfo; 11532 sata_pmult_info_t *pmultinfo; 11533 uint8_t cport = sata_device->satadev_addr.cport; 11534 int rval_probe; 11535 11536 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11537 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 11538 11539 /* probe port */ 11540 mutex_enter(&cportinfo->cport_mutex); 11541 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 11542 cportinfo->cport_state |= SATA_STATE_PROBING; 11543 mutex_exit(&cportinfo->cport_mutex); 11544 11545 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11546 (SATA_DIP(sata_hba_inst), sata_device); 11547 11548 mutex_enter(&cportinfo->cport_mutex); 11549 if (rval_probe != SATA_SUCCESS) { 11550 cportinfo->cport_state = SATA_PSTATE_FAILED; 11551 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmult: " 11552 "SATA port %d probing failed", cport)); 11553 sata_log(sata_hba_inst, CE_WARN, 11554 "SATA port multiplier detached at port %d", cport); 11555 mutex_exit(&cportinfo->cport_mutex); 11556 sata_free_pmult(sata_hba_inst, sata_device); 11557 return (SATA_FAILURE); 11558 } 11559 11560 /* 11561 * update sata port state and set device type 11562 */ 11563 sata_update_port_info(sata_hba_inst, sata_device); 11564 cportinfo->cport_state &= ~SATA_STATE_PROBING; 11565 cportinfo->cport_state |= SATA_STATE_PROBED; 11566 11567 /* 11568 * Sanity check - Port is active? Is the link active? 11569 * Is there any device attached? 11570 */ 11571 if ((cportinfo->cport_state & 11572 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 11573 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 11574 SATA_PORT_DEVLINK_UP || 11575 (sata_device->satadev_type == SATA_DTYPE_NONE)) { 11576 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 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_SUCCESS); 11582 } 11583 11584 /* 11585 * Device changed: PMult -> Non-PMult 11586 * 11587 * This situation is uncommon, most possibly being caused by errors 11588 * after which the port multiplier is not correct initialized and 11589 * recognized. In that case the new device will be marked as unknown 11590 * and will not be automatically probed in this routine. Instead 11591 * system administrator could manually restart it via cfgadm(1M). 11592 */ 11593 if (sata_device->satadev_type != SATA_DTYPE_PMULT) { 11594 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 11595 mutex_exit(&cportinfo->cport_mutex); 11596 sata_free_pmult(sata_hba_inst, sata_device); 11597 sata_log(sata_hba_inst, CE_WARN, 11598 "SATA port multiplier detached at port %d", cport); 11599 return (SATA_FAILURE); 11600 } 11601 11602 /* 11603 * Now we know it is a port multiplier. However, if this is not the 11604 * previously attached port multiplier - they may have different 11605 * pmport numbers - we need to re-allocate data structures for every 11606 * pmport and drive. 11607 * 11608 * Port multipliers of the same model have identical values in these 11609 * registers, so it is still necessary to update the information of 11610 * all drives attached to the previous port multiplier afterwards. 11611 */ 11612 /* Device changed: PMult -> another PMult */ 11613 mutex_exit(&cportinfo->cport_mutex); 11614 sata_free_pmult(sata_hba_inst, sata_device); 11615 if (sata_alloc_pmult(sata_hba_inst, sata_device) != SATA_SUCCESS) 11616 return (SATA_FAILURE); 11617 mutex_enter(&cportinfo->cport_mutex); 11618 11619 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 11620 "SATA port multiplier [changed] at port %d", cport); 11621 sata_log(sata_hba_inst, CE_WARN, 11622 "SATA port multiplier detected at port %d", cport); 11623 11624 /* 11625 * Mark all the port multiplier port behind the port 11626 * multiplier behind with link events, so that the sata daemon 11627 * will update their status. 11628 */ 11629 pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET; 11630 mutex_exit(&cportinfo->cport_mutex); 11631 11632 return (SATA_SUCCESS); 11633 } 11634 11635 /* 11636 * Re-probe a port multiplier port, check for a device and attach info 11637 * structures when necessary. Identify Device data is fetched, if possible. 11638 * Assumption: sata address is already validated as port multiplier port. 11639 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of 11640 * the presence of a device and its type. 11641 * 11642 * flag arg specifies that the function should try multiple times to identify 11643 * device type and to initialize it, or it should return immediately on failure. 11644 * SATA_DEV_IDENTIFY_RETRY - retry 11645 * SATA_DEV_IDENTIFY_NORETRY - no retry 11646 * 11647 * SATA_FAILURE is returned if one of the operations failed. 11648 * 11649 * This function cannot be called in interrupt context - it may sleep. 11650 * 11651 * NOTE: Should be only called by sata_probe_port() in case target port is a 11652 * port multiplier port. 11653 * NOTE: No Mutex should be hold. 11654 */ 11655 static int 11656 sata_reprobe_pmport(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 11657 int flag) 11658 { 11659 sata_cport_info_t *cportinfo = NULL; 11660 sata_pmport_info_t *pmportinfo = NULL; 11661 sata_drive_info_t *sdinfo, *osdinfo; 11662 sata_device_t sdevice; 11663 boolean_t init_device = B_FALSE; 11664 int prev_device_type = SATA_DTYPE_NONE; 11665 int prev_device_settings = 0; 11666 int prev_device_state = 0; 11667 clock_t start_time; 11668 uint8_t cport = sata_device->satadev_addr.cport; 11669 uint8_t pmport = sata_device->satadev_addr.pmport; 11670 int rval; 11671 11672 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11673 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 11674 osdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 11675 11676 if (osdinfo != NULL) { 11677 /* 11678 * We are re-probing port with a previously attached device. 11679 * Save previous device type and settings. 11680 */ 11681 prev_device_type = pmportinfo->pmport_dev_type; 11682 prev_device_settings = osdinfo->satadrv_settings; 11683 prev_device_state = osdinfo->satadrv_state; 11684 } 11685 11686 start_time = ddi_get_lbolt(); 11687 11688 /* check parent status */ 11689 mutex_enter(&cportinfo->cport_mutex); 11690 if ((cportinfo->cport_state & 11691 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 11692 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 11693 SATA_PORT_DEVLINK_UP) { 11694 mutex_exit(&cportinfo->cport_mutex); 11695 return (SATA_FAILURE); 11696 } 11697 mutex_exit(&cportinfo->cport_mutex); 11698 11699 retry_probe_pmport: 11700 11701 /* probe port */ 11702 mutex_enter(&pmportinfo->pmport_mutex); 11703 pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 11704 pmportinfo->pmport_state |= SATA_STATE_PROBING; 11705 mutex_exit(&pmportinfo->pmport_mutex); 11706 11707 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11708 (SATA_DIP(sata_hba_inst), sata_device); 11709 11710 /* might need retry because we cannot touch registers. */ 11711 if (rval == SATA_FAILURE) { 11712 mutex_enter(&pmportinfo->pmport_mutex); 11713 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 11714 mutex_exit(&pmportinfo->pmport_mutex); 11715 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: " 11716 "SATA port %d:%d probing failed", 11717 cport, pmport)); 11718 return (SATA_FAILURE); 11719 } else if (rval == SATA_RETRY) { 11720 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: " 11721 "SATA port %d:%d probing failed, retrying...", 11722 cport, pmport)); 11723 clock_t cur_time = ddi_get_lbolt(); 11724 /* 11725 * A device was not successfully identified or initialized. 11726 * Track retry time for device identification. 11727 */ 11728 if ((cur_time - start_time) < 11729 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 11730 /* sleep for a while */ 11731 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 11732 goto retry_probe_pmport; 11733 } else { 11734 mutex_enter(&pmportinfo->pmport_mutex); 11735 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) 11736 SATA_PMPORTINFO_DRV_INFO(pmportinfo)-> 11737 satadrv_state = SATA_DSTATE_FAILED; 11738 mutex_exit(&pmportinfo->pmport_mutex); 11739 return (SATA_SUCCESS); 11740 } 11741 } 11742 11743 /* 11744 * Sanity check - Controller port is active? Is the link active? 11745 * Is it still a port multiplier? 11746 */ 11747 if ((cportinfo->cport_state & 11748 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 11749 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 11750 SATA_PORT_DEVLINK_UP || 11751 (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) { 11752 /* 11753 * Port in non-usable state or no link active/no 11754 * device. Free info structure. 11755 */ 11756 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 11757 11758 sdevice.satadev_addr.cport = cport; 11759 sdevice.satadev_addr.pmport = pmport; 11760 sdevice.satadev_addr.qual = SATA_ADDR_PMULT; 11761 mutex_exit(&cportinfo->cport_mutex); 11762 11763 sata_free_pmult(sata_hba_inst, &sdevice); 11764 return (SATA_FAILURE); 11765 } 11766 11767 /* SATA_SUCCESS NOW */ 11768 /* 11769 * update sata port state and set device type 11770 */ 11771 mutex_enter(&pmportinfo->pmport_mutex); 11772 sata_update_pmport_info(sata_hba_inst, sata_device); 11773 pmportinfo->pmport_state &= ~SATA_STATE_PROBING; 11774 11775 /* 11776 * Sanity check - Port is active? Is the link active? 11777 * Is there any device attached? 11778 */ 11779 if ((pmportinfo->pmport_state & 11780 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 11781 (pmportinfo->pmport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 11782 SATA_PORT_DEVLINK_UP) { 11783 /* 11784 * Port in non-usable state or no link active/no device. 11785 * Free info structure if necessary (direct attached drive 11786 * only, for now! 11787 */ 11788 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 11789 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 11790 /* Add here differentiation for device attached or not */ 11791 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 11792 mutex_exit(&pmportinfo->pmport_mutex); 11793 if (sdinfo != NULL) 11794 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11795 return (SATA_SUCCESS); 11796 } 11797 11798 pmportinfo->pmport_state |= SATA_STATE_READY; 11799 pmportinfo->pmport_dev_type = sata_device->satadev_type; 11800 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 11801 11802 /* 11803 * If we are re-probing the port, there may be 11804 * sata_drive_info structure attached 11805 * (or sata_pm_info, if PMult is supported). 11806 */ 11807 if (sata_device->satadev_type == SATA_DTYPE_NONE) { 11808 /* 11809 * There is no device, so remove device info structure, 11810 * if necessary. 11811 */ 11812 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 11813 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 11814 if (sdinfo != NULL) { 11815 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11816 sata_log(sata_hba_inst, CE_WARN, 11817 "SATA device detached from port %d:%d", 11818 cport, pmport); 11819 } 11820 mutex_exit(&pmportinfo->pmport_mutex); 11821 return (SATA_SUCCESS); 11822 } 11823 11824 /* this should not be a pmult */ 11825 ASSERT(sata_device->satadev_type != SATA_DTYPE_PMULT); 11826 if (sdinfo == NULL) { 11827 /* 11828 * There is some device attached, but there is 11829 * no sata_drive_info structure - allocate one 11830 */ 11831 mutex_exit(&pmportinfo->pmport_mutex); 11832 sdinfo = kmem_zalloc(sizeof (sata_drive_info_t), 11833 KM_SLEEP); 11834 mutex_enter(&pmportinfo->pmport_mutex); 11835 /* 11836 * Recheck, that the port state did not change when we 11837 * released mutex. 11838 */ 11839 if (pmportinfo->pmport_state & SATA_STATE_READY) { 11840 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = sdinfo; 11841 sdinfo->satadrv_addr = pmportinfo->pmport_addr; 11842 sdinfo->satadrv_addr.qual = SATA_ADDR_DPMPORT; 11843 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 11844 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 11845 } else { 11846 /* 11847 * Port is not in ready state, we 11848 * cannot attach a device. 11849 */ 11850 mutex_exit(&pmportinfo->pmport_mutex); 11851 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11852 return (SATA_SUCCESS); 11853 } 11854 /* 11855 * Since we are adding device, presumably new one, 11856 * indicate that it should be initalized, 11857 * as well as some internal framework states). 11858 */ 11859 init_device = B_TRUE; 11860 } 11861 11862 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN; 11863 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual; 11864 11865 mutex_exit(&pmportinfo->pmport_mutex); 11866 /* 11867 * Figure out what kind of device we are really 11868 * dealing with. 11869 */ 11870 rval = sata_probe_device(sata_hba_inst, sata_device); 11871 11872 mutex_enter(&pmportinfo->pmport_mutex); 11873 if (rval == SATA_SUCCESS) { 11874 /* 11875 * If we are dealing with the same type of a device as before, 11876 * restore its settings flags. 11877 */ 11878 if (osdinfo != NULL && 11879 sata_device->satadev_type == prev_device_type) 11880 sdinfo->satadrv_settings = prev_device_settings; 11881 11882 mutex_exit(&pmportinfo->pmport_mutex); 11883 /* Set initial device features, if necessary */ 11884 if (init_device == B_TRUE) { 11885 rval = sata_initialize_device(sata_hba_inst, sdinfo); 11886 } 11887 if (rval == SATA_SUCCESS) 11888 return (rval); 11889 } else { 11890 /* 11891 * If there was some device info before we probe the device, 11892 * restore previous device setting, so we can retry from scratch 11893 * later. Providing, of course, that device has not disappeared 11894 * during probing process. 11895 */ 11896 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 11897 if (osdinfo != NULL) { 11898 pmportinfo->pmport_dev_type = prev_device_type; 11899 sdinfo->satadrv_type = prev_device_type; 11900 sdinfo->satadrv_state = prev_device_state; 11901 } 11902 } else { 11903 /* device is gone */ 11904 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11905 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 11906 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 11907 mutex_exit(&pmportinfo->pmport_mutex); 11908 return (SATA_SUCCESS); 11909 } 11910 mutex_exit(&pmportinfo->pmport_mutex); 11911 } 11912 11913 if (flag == SATA_DEV_IDENTIFY_RETRY) { 11914 clock_t cur_time = ddi_get_lbolt(); 11915 /* 11916 * A device was not successfully identified or initialized. 11917 * Track retry time for device identification. 11918 */ 11919 if ((cur_time - start_time) < 11920 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 11921 /* sleep for a while */ 11922 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 11923 goto retry_probe_pmport; 11924 } else { 11925 mutex_enter(&pmportinfo->pmport_mutex); 11926 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) 11927 SATA_PMPORTINFO_DRV_INFO(pmportinfo)-> 11928 satadrv_state = SATA_DSTATE_FAILED; 11929 mutex_exit(&pmportinfo->pmport_mutex); 11930 } 11931 } 11932 return (SATA_SUCCESS); 11933 } 11934 11935 /* 11936 * Allocated related structure for a port multiplier and its device ports 11937 * 11938 * Port multiplier should be ready and probed, and related information like 11939 * the number of the device ports should be store in sata_device_t. 11940 * 11941 * NOTE: No Mutex should be hold. 11942 */ 11943 static int 11944 sata_alloc_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 11945 { 11946 dev_info_t *dip = SATA_DIP(sata_hba_inst); 11947 sata_cport_info_t *cportinfo = NULL; 11948 sata_pmult_info_t *pmultinfo = NULL; 11949 sata_pmport_info_t *pmportinfo = NULL; 11950 sata_device_t sd; 11951 dev_t minor_number; 11952 char name[16]; 11953 uint8_t cport = sata_device->satadev_addr.cport; 11954 int rval; 11955 int npmport; 11956 11957 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11958 11959 /* This function might be called while a port-mult is hot-plugged. */ 11960 mutex_enter(&cportinfo->cport_mutex); 11961 11962 /* dev_type's not updated when get called from sata_reprobe_port() */ 11963 if (SATA_CPORTINFO_PMULT_INFO(cportinfo) == NULL) { 11964 /* Create a pmult_info structure */ 11965 SATA_CPORTINFO_PMULT_INFO(cportinfo) = 11966 kmem_zalloc(sizeof (sata_pmult_info_t), KM_SLEEP); 11967 } 11968 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 11969 11970 pmultinfo->pmult_addr = sata_device->satadev_addr; 11971 pmultinfo->pmult_addr.qual = SATA_ADDR_PMULT; 11972 pmultinfo->pmult_state = SATA_STATE_PROBING; 11973 11974 /* 11975 * Probe the port multiplier with qualifier SATA_ADDR_PMULT_SPEC, 11976 * The HBA driver should initialize and register the port multiplier, 11977 * sata_register_pmult() will fill following fields, 11978 * + sata_pmult_info.pmult_gscr 11979 * + sata_pmult_info.pmult_num_dev_ports 11980 */ 11981 sd.satadev_addr = sata_device->satadev_addr; 11982 sd.satadev_addr.qual = SATA_ADDR_PMULT_SPEC; 11983 mutex_exit(&cportinfo->cport_mutex); 11984 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11985 (SATA_DIP(sata_hba_inst), &sd); 11986 mutex_enter(&cportinfo->cport_mutex); 11987 11988 if (rval != SATA_SUCCESS || 11989 (sd.satadev_type != SATA_DTYPE_PMULT) || 11990 !(sd.satadev_state & SATA_DSTATE_PMULT_INIT)) { 11991 SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL; 11992 kmem_free(pmultinfo, sizeof (sata_pmult_info_t)); 11993 cportinfo->cport_state = SATA_PSTATE_FAILED; 11994 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 11995 mutex_exit(&cportinfo->cport_mutex); 11996 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 11997 "sata_alloc_pmult: failed to initialize pmult " 11998 "at port %d.", cport) 11999 return (SATA_FAILURE); 12000 } 12001 12002 /* Initialize pmport_info structure */ 12003 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; 12004 npmport++) { 12005 12006 /* if everything is allocated, skip */ 12007 if (SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) != NULL) 12008 continue; 12009 12010 pmportinfo = kmem_zalloc(sizeof (sata_pmport_info_t), KM_SLEEP); 12011 mutex_init(&pmportinfo->pmport_mutex, NULL, MUTEX_DRIVER, NULL); 12012 mutex_exit(&cportinfo->cport_mutex); 12013 12014 mutex_enter(&pmportinfo->pmport_mutex); 12015 pmportinfo->pmport_addr.cport = cport; 12016 pmportinfo->pmport_addr.pmport = (uint8_t)npmport; 12017 pmportinfo->pmport_addr.qual = SATA_ADDR_PMPORT; 12018 pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 12019 mutex_exit(&pmportinfo->pmport_mutex); 12020 12021 mutex_enter(&cportinfo->cport_mutex); 12022 SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) = pmportinfo; 12023 12024 /* Create an attachment point */ 12025 minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip), 12026 cport, (uint8_t)npmport, SATA_ADDR_PMPORT); 12027 (void) sprintf(name, "%d.%d", cport, npmport); 12028 12029 if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number, 12030 DDI_NT_SATA_ATTACHMENT_POINT, 0) != DDI_SUCCESS) { 12031 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: " 12032 "cannot create SATA attachment point for " 12033 "port %d:%d", cport, npmport); 12034 } 12035 } 12036 12037 pmultinfo->pmult_state &= ~SATA_STATE_PROBING; 12038 pmultinfo->pmult_state |= (SATA_STATE_PROBED|SATA_STATE_READY); 12039 cportinfo->cport_dev_type = SATA_DTYPE_PMULT; 12040 12041 mutex_exit(&cportinfo->cport_mutex); 12042 return (SATA_SUCCESS); 12043 } 12044 12045 /* 12046 * Free data structures when a port multiplier is removed. 12047 * 12048 * NOTE: No Mutex should be hold. 12049 */ 12050 static void 12051 sata_free_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 12052 { 12053 sata_cport_info_t *cportinfo; 12054 sata_pmult_info_t *pmultinfo; 12055 sata_pmport_info_t *pmportinfo; 12056 sata_device_t pmport_device; 12057 sata_drive_info_t *sdinfo; 12058 dev_info_t *tdip; 12059 char name[16]; 12060 uint8_t cport = sata_device->satadev_addr.cport; 12061 int npmport; 12062 12063 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 12064 12065 /* This function might be called while port-mult is hot plugged. */ 12066 mutex_enter(&cportinfo->cport_mutex); 12067 12068 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 12069 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 12070 ASSERT(pmultinfo != NULL); 12071 12072 /* Free pmport_info structure */ 12073 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; 12074 npmport++) { 12075 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport); 12076 if (pmportinfo == NULL) 12077 continue; 12078 mutex_exit(&cportinfo->cport_mutex); 12079 12080 mutex_enter(&pmportinfo->pmport_mutex); 12081 sdinfo = pmportinfo->pmport_sata_drive; 12082 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 12083 mutex_exit(&pmportinfo->pmport_mutex); 12084 12085 /* Remove attachment point. */ 12086 name[0] = '\0'; 12087 (void) sprintf(name, "%d.%d", cport, npmport); 12088 ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name); 12089 sata_log(sata_hba_inst, CE_NOTE, 12090 "Remove attachment point of port %d:%d", 12091 cport, npmport); 12092 12093 /* 12094 * Rumove target node 12095 */ 12096 bzero(&pmport_device, sizeof (sata_device_t)); 12097 pmport_device.satadev_rev = SATA_DEVICE_REV; 12098 pmport_device.satadev_addr.cport = cport; 12099 pmport_device.satadev_addr.pmport = (uint8_t)npmport; 12100 pmport_device.satadev_addr.qual = SATA_ADDR_DPMPORT; 12101 12102 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 12103 &(pmport_device.satadev_addr)); 12104 if (tdip != NULL && ndi_devi_offline(tdip, 12105 NDI_DEVI_REMOVE) != NDI_SUCCESS) { 12106 /* 12107 * Problem : 12108 * The target node remained attached. 12109 * This happens when the device file was open 12110 * or a node was waiting for resources. 12111 * Cannot do anything about it. 12112 */ 12113 SATA_LOG_D((sata_hba_inst, CE_WARN, 12114 "sata_free_pmult: could not unconfigure device " 12115 "before disconnecting the SATA port %d:%d", 12116 cport, npmport)); 12117 12118 /* 12119 * Set DEVICE REMOVED state in the target 12120 * node. It will prevent access to the device 12121 * even when a new device is attached, until 12122 * the old target node is released, removed and 12123 * recreated for a new device. 12124 */ 12125 sata_set_device_removed(tdip); 12126 12127 /* 12128 * Instruct event daemon to try the target 12129 * node cleanup later. 12130 */ 12131 sata_set_target_node_cleanup( 12132 sata_hba_inst, &(pmport_device.satadev_addr)); 12133 12134 } 12135 mutex_enter(&cportinfo->cport_mutex); 12136 12137 /* 12138 * Add here differentiation for device attached or not 12139 */ 12140 if (sdinfo != NULL) { 12141 sata_log(sata_hba_inst, CE_WARN, 12142 "SATA device detached from port %d:%d", 12143 cport, npmport); 12144 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 12145 } 12146 12147 mutex_destroy(&pmportinfo->pmport_mutex); 12148 kmem_free(pmportinfo, sizeof (sata_pmport_info_t)); 12149 } 12150 12151 kmem_free(pmultinfo, sizeof (sata_pmult_info_t)); 12152 12153 cportinfo->cport_devp.cport_sata_pmult = NULL; 12154 12155 sata_log(sata_hba_inst, CE_WARN, 12156 "SATA port multiplier detached at port %d", cport); 12157 12158 mutex_exit(&cportinfo->cport_mutex); 12159 } 12160 12161 /* 12162 * Initialize device 12163 * Specified device is initialized to a default state. 12164 * 12165 * Returns SATA_SUCCESS if all device features are set successfully, 12166 * SATA_RETRY if device is accessible but device features were not set 12167 * successfully, and SATA_FAILURE otherwise. 12168 */ 12169 static int 12170 sata_initialize_device(sata_hba_inst_t *sata_hba_inst, 12171 sata_drive_info_t *sdinfo) 12172 { 12173 int rval; 12174 12175 sata_save_drive_settings(sdinfo); 12176 12177 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 12178 12179 sata_init_write_cache_mode(sdinfo); 12180 12181 rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0); 12182 12183 /* Determine current data transfer mode */ 12184 if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) { 12185 sdinfo->satadrv_settings &= ~SATA_DEV_DMA; 12186 } else if ((sdinfo->satadrv_id.ai_validinfo & 12187 SATA_VALIDINFO_88) != 0 && 12188 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) { 12189 sdinfo->satadrv_settings |= SATA_DEV_DMA; 12190 } else if ((sdinfo->satadrv_id.ai_dworddma & 12191 SATA_MDMA_SEL_MASK) != 0) { 12192 sdinfo->satadrv_settings |= SATA_DEV_DMA; 12193 } else 12194 /* DMA supported, not no DMA transfer mode is selected !? */ 12195 sdinfo->satadrv_settings &= ~SATA_DEV_DMA; 12196 12197 if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) && 12198 (sdinfo->satadrv_id.ai_features86 & 0x20)) 12199 sdinfo->satadrv_power_level = SATA_POWER_STANDBY; 12200 else 12201 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 12202 12203 return (rval); 12204 } 12205 12206 12207 /* 12208 * Initialize write cache mode. 12209 * 12210 * The default write cache setting for SATA HDD is provided by sata_write_cache 12211 * static variable. ATAPI CD/DVDs devices have write cache default is 12212 * determined by sata_atapicdvd_write_cache static variable. 12213 * ATAPI tape devices have write cache default is determined by 12214 * sata_atapitape_write_cache static variable. 12215 * ATAPI disk devices have write cache default is determined by 12216 * sata_atapidisk_write_cache static variable. 12217 * 1 - enable 12218 * 0 - disable 12219 * any other value - current drive setting 12220 * 12221 * Although there is not reason to disable write cache on CD/DVD devices, 12222 * tape devices and ATAPI disk devices, the default setting control is provided 12223 * for the maximun flexibility. 12224 * 12225 * In the future, it may be overridden by the 12226 * disk-write-cache-enable property setting, if it is defined. 12227 * Returns SATA_SUCCESS if all device features are set successfully, 12228 * SATA_FAILURE otherwise. 12229 */ 12230 static void 12231 sata_init_write_cache_mode(sata_drive_info_t *sdinfo) 12232 { 12233 switch (sdinfo->satadrv_type) { 12234 case SATA_DTYPE_ATADISK: 12235 if (sata_write_cache == 1) 12236 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 12237 else if (sata_write_cache == 0) 12238 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 12239 /* 12240 * When sata_write_cache value is not 0 or 1, 12241 * a current setting of the drive's write cache is used. 12242 */ 12243 break; 12244 case SATA_DTYPE_ATAPICD: 12245 if (sata_atapicdvd_write_cache == 1) 12246 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 12247 else if (sata_atapicdvd_write_cache == 0) 12248 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 12249 /* 12250 * When sata_atapicdvd_write_cache value is not 0 or 1, 12251 * a current setting of the drive's write cache is used. 12252 */ 12253 break; 12254 case SATA_DTYPE_ATAPITAPE: 12255 if (sata_atapitape_write_cache == 1) 12256 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 12257 else if (sata_atapitape_write_cache == 0) 12258 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 12259 /* 12260 * When sata_atapitape_write_cache value is not 0 or 1, 12261 * a current setting of the drive's write cache is used. 12262 */ 12263 break; 12264 case SATA_DTYPE_ATAPIDISK: 12265 if (sata_atapidisk_write_cache == 1) 12266 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 12267 else if (sata_atapidisk_write_cache == 0) 12268 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 12269 /* 12270 * When sata_atapidisk_write_cache value is not 0 or 1, 12271 * a current setting of the drive's write cache is used. 12272 */ 12273 break; 12274 } 12275 } 12276 12277 12278 /* 12279 * Validate sata address. 12280 * Specified cport, pmport and qualifier has to match 12281 * passed sata_scsi configuration info. 12282 * The presence of an attached device is not verified. 12283 * 12284 * Returns 0 when address is valid, -1 otherwise. 12285 */ 12286 static int 12287 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport, 12288 int pmport, int qual) 12289 { 12290 if (qual == SATA_ADDR_DCPORT && pmport != 0) 12291 goto invalid_address; 12292 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 12293 goto invalid_address; 12294 if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) && 12295 ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) || 12296 (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) || 12297 (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport)))) 12298 goto invalid_address; 12299 12300 return (0); 12301 12302 invalid_address: 12303 return (-1); 12304 12305 } 12306 12307 /* 12308 * Validate scsi address 12309 * SCSI target address is translated into SATA cport/pmport and compared 12310 * with a controller port/device configuration. LUN has to be 0. 12311 * Returns 0 if a scsi target refers to an attached device, 12312 * returns 1 if address is valid but no valid device is attached, 12313 * returns 2 if address is valid but device type is unknown (not valid device), 12314 * returns -1 if bad address or device is of an unsupported type. 12315 * Upon return sata_device argument is set. 12316 * 12317 * Port multiplier is supported now. 12318 */ 12319 static int 12320 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst, 12321 struct scsi_address *ap, sata_device_t *sata_device) 12322 { 12323 int cport, pmport, qual, rval; 12324 12325 rval = -1; /* Invalid address */ 12326 if (ap->a_lun != 0) 12327 goto out; 12328 12329 qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 12330 cport = SCSI_TO_SATA_CPORT(ap->a_target); 12331 pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 12332 12333 if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT) 12334 goto out; 12335 12336 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) == 12337 0) { 12338 12339 sata_cport_info_t *cportinfo; 12340 sata_pmult_info_t *pmultinfo; 12341 sata_drive_info_t *sdinfo = NULL; 12342 12343 sata_device->satadev_addr.qual = qual; 12344 sata_device->satadev_addr.cport = cport; 12345 sata_device->satadev_addr.pmport = pmport; 12346 sata_device->satadev_rev = SATA_DEVICE_REV_1; 12347 12348 rval = 1; /* Valid sata address */ 12349 12350 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 12351 if (qual == SATA_ADDR_DCPORT) { 12352 if (cportinfo == NULL || 12353 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 12354 goto out; 12355 12356 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 12357 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN && 12358 sdinfo != NULL) { 12359 rval = 2; 12360 goto out; 12361 } 12362 12363 if ((cportinfo->cport_dev_type & 12364 SATA_VALID_DEV_TYPE) == 0) { 12365 rval = -1; 12366 goto out; 12367 } 12368 12369 } else if (qual == SATA_ADDR_DPMPORT) { 12370 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 12371 if (pmultinfo == NULL) { 12372 rval = -1; 12373 goto out; 12374 } 12375 if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) == 12376 NULL || 12377 SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 12378 pmport) == SATA_DTYPE_NONE) 12379 goto out; 12380 12381 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, 12382 pmport); 12383 if (SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 12384 pmport) == SATA_DTYPE_UNKNOWN && sdinfo != NULL) { 12385 rval = 2; 12386 goto out; 12387 } 12388 12389 if ((SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 12390 pmport) & SATA_VALID_DEV_TYPE) == 0) { 12391 rval = -1; 12392 goto out; 12393 } 12394 12395 } else { 12396 rval = -1; 12397 goto out; 12398 } 12399 if ((sdinfo == NULL) || 12400 (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0) 12401 goto out; 12402 12403 sata_device->satadev_type = sdinfo->satadrv_type; 12404 12405 return (0); 12406 } 12407 out: 12408 if (rval > 0) { 12409 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 12410 "sata_validate_scsi_address: no valid target %x lun %x", 12411 ap->a_target, ap->a_lun); 12412 } 12413 return (rval); 12414 } 12415 12416 /* 12417 * Find dip corresponding to passed device number 12418 * 12419 * Returns NULL if invalid device number is passed or device cannot be found, 12420 * Returns dip is device is found. 12421 */ 12422 static dev_info_t * 12423 sata_devt_to_devinfo(dev_t dev) 12424 { 12425 dev_info_t *dip; 12426 #ifndef __lock_lint 12427 struct devnames *dnp; 12428 major_t major = getmajor(dev); 12429 int instance = SATA_MINOR2INSTANCE(getminor(dev)); 12430 12431 if (major >= devcnt) 12432 return (NULL); 12433 12434 dnp = &devnamesp[major]; 12435 LOCK_DEV_OPS(&(dnp->dn_lock)); 12436 dip = dnp->dn_head; 12437 while (dip && (ddi_get_instance(dip) != instance)) { 12438 dip = ddi_get_next(dip); 12439 } 12440 UNLOCK_DEV_OPS(&(dnp->dn_lock)); 12441 #endif 12442 12443 return (dip); 12444 } 12445 12446 12447 /* 12448 * Probe device. 12449 * This function issues Identify Device command and initializes local 12450 * sata_drive_info structure if the device can be identified. 12451 * The device type is determined by examining Identify Device 12452 * command response. 12453 * If the sata_hba_inst has linked drive info structure for this 12454 * device address, the Identify Device data is stored into sata_drive_info 12455 * structure linked to the port info structure. 12456 * 12457 * sata_device has to refer to the valid sata port(s) for HBA described 12458 * by sata_hba_inst structure. 12459 * 12460 * Returns: 12461 * SATA_SUCCESS if device type was successfully probed and port-linked 12462 * drive info structure was updated; 12463 * SATA_FAILURE if there is no device, or device was not probed 12464 * successully; 12465 * SATA_RETRY if device probe can be retried later. 12466 * If a device cannot be identified, sata_device's dev_state and dev_type 12467 * fields are set to unknown. 12468 * There are no retries in this function. Any retries should be managed by 12469 * the caller. 12470 */ 12471 12472 12473 static int 12474 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 12475 { 12476 sata_pmport_info_t *pmportinfo; 12477 sata_drive_info_t *sdinfo; 12478 sata_drive_info_t new_sdinfo; /* local drive info struct */ 12479 int rval; 12480 12481 ASSERT((SATA_CPORT_STATE(sata_hba_inst, 12482 sata_device->satadev_addr.cport) & 12483 (SATA_STATE_PROBED | SATA_STATE_READY)) != 0); 12484 12485 sata_device->satadev_type = SATA_DTYPE_NONE; 12486 12487 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12488 sata_device->satadev_addr.cport))); 12489 12490 if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) { 12491 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 12492 sata_device->satadev_addr.cport, 12493 sata_device->satadev_addr.pmport); 12494 ASSERT(pmportinfo != NULL); 12495 } 12496 12497 /* Get pointer to port-linked sata device info structure */ 12498 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 12499 if (sdinfo != NULL) { 12500 sdinfo->satadrv_state &= 12501 ~(SATA_STATE_PROBED | SATA_STATE_READY); 12502 sdinfo->satadrv_state |= SATA_STATE_PROBING; 12503 } else { 12504 /* No device to probe */ 12505 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12506 sata_device->satadev_addr.cport))); 12507 sata_device->satadev_type = SATA_DTYPE_NONE; 12508 sata_device->satadev_state = SATA_STATE_UNKNOWN; 12509 return (SATA_FAILURE); 12510 } 12511 /* 12512 * Need to issue both types of identify device command and 12513 * determine device type by examining retreived data/status. 12514 * First, ATA Identify Device. 12515 */ 12516 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 12517 new_sdinfo.satadrv_addr = sata_device->satadev_addr; 12518 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12519 sata_device->satadev_addr.cport))); 12520 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK; 12521 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 12522 if (rval == SATA_RETRY) { 12523 /* We may try to check for ATAPI device */ 12524 if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) { 12525 /* 12526 * HBA supports ATAPI - try to issue Identify Packet 12527 * Device command. 12528 */ 12529 new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI; 12530 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 12531 } 12532 } 12533 if (rval == SATA_SUCCESS) { 12534 /* 12535 * Got something responding positively to ATA Identify Device 12536 * or to Identify Packet Device cmd. 12537 * Save last used device type. 12538 */ 12539 sata_device->satadev_type = new_sdinfo.satadrv_type; 12540 12541 /* save device info, if possible */ 12542 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12543 sata_device->satadev_addr.cport))); 12544 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 12545 if (sdinfo == NULL) { 12546 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12547 sata_device->satadev_addr.cport))); 12548 return (SATA_FAILURE); 12549 } 12550 /* 12551 * Copy drive info into the port-linked drive info structure. 12552 */ 12553 *sdinfo = new_sdinfo; 12554 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 12555 sdinfo->satadrv_state |= SATA_STATE_PROBED; 12556 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 12557 SATA_CPORT_DEV_TYPE(sata_hba_inst, 12558 sata_device->satadev_addr.cport) = 12559 sdinfo->satadrv_type; 12560 else { /* SATA_ADDR_DPMPORT */ 12561 mutex_enter(&pmportinfo->pmport_mutex); 12562 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 12563 sata_device->satadev_addr.cport, 12564 sata_device->satadev_addr.pmport) = 12565 sdinfo->satadrv_type; 12566 mutex_exit(&pmportinfo->pmport_mutex); 12567 } 12568 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12569 sata_device->satadev_addr.cport))); 12570 return (SATA_SUCCESS); 12571 } 12572 12573 /* 12574 * It may be SATA_RETRY or SATA_FAILURE return. 12575 * Looks like we cannot determine the device type at this time. 12576 */ 12577 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12578 sata_device->satadev_addr.cport))); 12579 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 12580 if (sdinfo != NULL) { 12581 sata_device->satadev_type = SATA_DTYPE_UNKNOWN; 12582 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 12583 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 12584 sdinfo->satadrv_state |= SATA_STATE_PROBED; 12585 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 12586 SATA_CPORT_DEV_TYPE(sata_hba_inst, 12587 sata_device->satadev_addr.cport) = 12588 SATA_DTYPE_UNKNOWN; 12589 else { 12590 /* SATA_ADDR_DPMPORT */ 12591 mutex_enter(&pmportinfo->pmport_mutex); 12592 if ((SATA_PMULT_INFO(sata_hba_inst, 12593 sata_device->satadev_addr.cport) != NULL) && 12594 (SATA_PMPORT_INFO(sata_hba_inst, 12595 sata_device->satadev_addr.cport, 12596 sata_device->satadev_addr.pmport) != NULL)) 12597 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 12598 sata_device->satadev_addr.cport, 12599 sata_device->satadev_addr.pmport) = 12600 SATA_DTYPE_UNKNOWN; 12601 mutex_exit(&pmportinfo->pmport_mutex); 12602 } 12603 } 12604 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12605 sata_device->satadev_addr.cport))); 12606 return (rval); 12607 } 12608 12609 12610 /* 12611 * Get pointer to sata_drive_info structure. 12612 * 12613 * The sata_device has to contain address (cport, pmport and qualifier) for 12614 * specified sata_scsi structure. 12615 * 12616 * Returns NULL if device address is not valid for this HBA configuration. 12617 * Otherwise, returns a pointer to sata_drive_info structure. 12618 * 12619 * This function should be called with a port mutex held. 12620 */ 12621 static sata_drive_info_t * 12622 sata_get_device_info(sata_hba_inst_t *sata_hba_inst, 12623 sata_device_t *sata_device) 12624 { 12625 uint8_t cport = sata_device->satadev_addr.cport; 12626 uint8_t pmport = sata_device->satadev_addr.pmport; 12627 uint8_t qual = sata_device->satadev_addr.qual; 12628 12629 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 12630 return (NULL); 12631 12632 if (!(SATA_CPORT_STATE(sata_hba_inst, cport) & 12633 (SATA_STATE_PROBED | SATA_STATE_READY))) 12634 /* Port not probed yet */ 12635 return (NULL); 12636 12637 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE) 12638 return (NULL); 12639 12640 if (qual == SATA_ADDR_DCPORT) { 12641 /* Request for a device on a controller port */ 12642 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == 12643 SATA_DTYPE_PMULT) 12644 /* Port multiplier attached */ 12645 return (NULL); 12646 return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport)); 12647 } 12648 if (qual == SATA_ADDR_DPMPORT) { 12649 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 12650 SATA_DTYPE_PMULT) 12651 return (NULL); 12652 12653 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) 12654 return (NULL); 12655 12656 if (!(SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) & 12657 (SATA_STATE_PROBED | SATA_STATE_READY))) 12658 /* Port multiplier port not probed yet */ 12659 return (NULL); 12660 12661 return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport)); 12662 } 12663 12664 /* we should not get here */ 12665 return (NULL); 12666 } 12667 12668 12669 /* 12670 * sata_identify_device. 12671 * Send Identify Device command to SATA HBA driver. 12672 * If command executes successfully, update sata_drive_info structure pointed 12673 * to by sdinfo argument, including Identify Device data. 12674 * If command fails, invalidate data in sata_drive_info. 12675 * 12676 * Cannot be called from interrupt level. 12677 * 12678 * Returns: 12679 * SATA_SUCCESS if the device was identified as a supported device, 12680 * SATA_RETRY if the device was not identified but could be retried, 12681 * SATA_FAILURE if the device was not identified and identify attempt 12682 * should not be retried. 12683 */ 12684 static int 12685 sata_identify_device(sata_hba_inst_t *sata_hba_inst, 12686 sata_drive_info_t *sdinfo) 12687 { 12688 uint16_t cfg_word; 12689 int rval; 12690 12691 /* fetch device identify data */ 12692 if ((rval = sata_fetch_device_identify_data(sata_hba_inst, 12693 sdinfo)) != SATA_SUCCESS) 12694 goto fail_unknown; 12695 12696 cfg_word = sdinfo->satadrv_id.ai_config; 12697 12698 /* Set the correct device type */ 12699 if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) { 12700 sdinfo->satadrv_type = SATA_DTYPE_ATADISK; 12701 } else if (cfg_word == SATA_CFA_TYPE) { 12702 /* It's a Compact Flash media via CF-to-SATA HDD adapter */ 12703 sdinfo->satadrv_type = SATA_DTYPE_ATADISK; 12704 } else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) { 12705 switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) { 12706 case SATA_ATAPI_CDROM_DEV: 12707 sdinfo->satadrv_type = SATA_DTYPE_ATAPICD; 12708 break; 12709 case SATA_ATAPI_SQACC_DEV: 12710 sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE; 12711 break; 12712 case SATA_ATAPI_DIRACC_DEV: 12713 sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK; 12714 break; 12715 case SATA_ATAPI_PROC_DEV: 12716 sdinfo->satadrv_type = SATA_DTYPE_ATAPIPROC; 12717 break; 12718 default: 12719 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 12720 } 12721 } else { 12722 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 12723 } 12724 12725 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 12726 if (sdinfo->satadrv_capacity == 0) { 12727 /* Non-LBA disk. Too bad... */ 12728 sata_log(sata_hba_inst, CE_WARN, 12729 "SATA disk device at port %d does not support LBA", 12730 sdinfo->satadrv_addr.cport); 12731 rval = SATA_FAILURE; 12732 goto fail_unknown; 12733 } 12734 } 12735 #if 0 12736 /* Left for historical reason */ 12737 /* 12738 * Some initial version of SATA spec indicated that at least 12739 * UDMA mode 4 has to be supported. It is not metioned in 12740 * SerialATA 2.6, so this restriction is removed. 12741 */ 12742 /* Check for Ultra DMA modes 6 through 0 being supported */ 12743 for (i = 6; i >= 0; --i) { 12744 if (sdinfo->satadrv_id.ai_ultradma & (1 << i)) 12745 break; 12746 } 12747 12748 /* 12749 * At least UDMA 4 mode has to be supported. If mode 4 or 12750 * higher are not supported by the device, fail this 12751 * device. 12752 */ 12753 if (i < 4) { 12754 /* No required Ultra DMA mode supported */ 12755 sata_log(sata_hba_inst, CE_WARN, 12756 "SATA disk device at port %d does not support UDMA " 12757 "mode 4 or higher", sdinfo->satadrv_addr.cport); 12758 SATA_LOG_D((sata_hba_inst, CE_WARN, 12759 "mode 4 or higher required, %d supported", i)); 12760 rval = SATA_FAILURE; 12761 goto fail_unknown; 12762 } 12763 #endif 12764 12765 /* 12766 * For Disk devices, if it doesn't support UDMA mode, we would 12767 * like to return failure directly. 12768 */ 12769 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 12770 !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 && 12771 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) { 12772 sata_log(sata_hba_inst, CE_WARN, 12773 "SATA disk device at port %d does not support UDMA", 12774 sdinfo->satadrv_addr.cport); 12775 rval = SATA_FAILURE; 12776 goto fail_unknown; 12777 } 12778 12779 return (SATA_SUCCESS); 12780 12781 fail_unknown: 12782 /* Invalidate sata_drive_info ? */ 12783 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 12784 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 12785 return (rval); 12786 } 12787 12788 /* 12789 * Log/display device information 12790 */ 12791 static void 12792 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst, 12793 sata_drive_info_t *sdinfo) 12794 { 12795 int valid_version; 12796 char msg_buf[MAXPATHLEN]; 12797 int i; 12798 12799 /* Show HBA path */ 12800 (void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf); 12801 12802 cmn_err(CE_CONT, "?%s :\n", msg_buf); 12803 12804 switch (sdinfo->satadrv_type) { 12805 case SATA_DTYPE_ATADISK: 12806 (void) sprintf(msg_buf, "SATA disk device at"); 12807 break; 12808 12809 case SATA_DTYPE_ATAPICD: 12810 (void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at"); 12811 break; 12812 12813 case SATA_DTYPE_ATAPITAPE: 12814 (void) sprintf(msg_buf, "SATA tape (ATAPI) device at"); 12815 break; 12816 12817 case SATA_DTYPE_ATAPIDISK: 12818 (void) sprintf(msg_buf, "SATA disk (ATAPI) device at"); 12819 break; 12820 12821 case SATA_DTYPE_ATAPIPROC: 12822 (void) sprintf(msg_buf, "SATA processor (ATAPI) device at"); 12823 break; 12824 12825 case SATA_DTYPE_UNKNOWN: 12826 (void) sprintf(msg_buf, 12827 "Unsupported SATA device type (cfg 0x%x) at ", 12828 sdinfo->satadrv_id.ai_config); 12829 break; 12830 } 12831 12832 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT) 12833 cmn_err(CE_CONT, "?\t%s port %d\n", 12834 msg_buf, sdinfo->satadrv_addr.cport); 12835 else 12836 cmn_err(CE_CONT, "?\t%s port %d:%d\n", 12837 msg_buf, sdinfo->satadrv_addr.cport, 12838 sdinfo->satadrv_addr.pmport); 12839 12840 bcopy(&sdinfo->satadrv_id.ai_model, msg_buf, 12841 sizeof (sdinfo->satadrv_id.ai_model)); 12842 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model)); 12843 msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0'; 12844 cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf); 12845 12846 bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf, 12847 sizeof (sdinfo->satadrv_id.ai_fw)); 12848 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw)); 12849 msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0'; 12850 cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf); 12851 12852 bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf, 12853 sizeof (sdinfo->satadrv_id.ai_drvser)); 12854 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser)); 12855 msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0'; 12856 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 12857 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 12858 } else { 12859 /* 12860 * Some drives do not implement serial number and may 12861 * violate the spec by providing spaces rather than zeros 12862 * in serial number field. Scan the buffer to detect it. 12863 */ 12864 for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) { 12865 if (msg_buf[i] != '\0' && msg_buf[i] != ' ') 12866 break; 12867 } 12868 if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) { 12869 cmn_err(CE_CONT, "?\tserial number - none\n"); 12870 } else { 12871 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 12872 } 12873 } 12874 12875 #ifdef SATA_DEBUG 12876 if (sdinfo->satadrv_id.ai_majorversion != 0 && 12877 sdinfo->satadrv_id.ai_majorversion != 0xffff) { 12878 int i; 12879 for (i = 14; i >= 2; i--) { 12880 if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) { 12881 valid_version = i; 12882 break; 12883 } 12884 } 12885 cmn_err(CE_CONT, 12886 "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n", 12887 valid_version, 12888 sdinfo->satadrv_id.ai_majorversion, 12889 sdinfo->satadrv_id.ai_minorversion); 12890 } 12891 #endif 12892 /* Log some info */ 12893 cmn_err(CE_CONT, "?\tsupported features:\n"); 12894 msg_buf[0] = '\0'; 12895 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 12896 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) 12897 (void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN); 12898 else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) 12899 (void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN); 12900 } 12901 if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA) 12902 (void) strlcat(msg_buf, "DMA", MAXPATHLEN); 12903 if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) 12904 (void) strlcat(msg_buf, ", Native Command Queueing", 12905 MAXPATHLEN); 12906 if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ) 12907 (void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN); 12908 if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) && 12909 (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED)) 12910 (void) strlcat(msg_buf, ", SMART", MAXPATHLEN); 12911 if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) && 12912 (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED)) 12913 (void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN); 12914 cmn_err(CE_CONT, "?\t %s\n", msg_buf); 12915 if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA3) 12916 cmn_err(CE_CONT, "?\tSATA Gen3 signaling speed (6.0Gbps)\n"); 12917 else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2) 12918 cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n"); 12919 else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1) 12920 cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n"); 12921 if (sdinfo->satadrv_features_support & 12922 (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) { 12923 msg_buf[0] = '\0'; 12924 (void) snprintf(msg_buf, MAXPATHLEN, 12925 "Supported queue depth %d", 12926 sdinfo->satadrv_queue_depth); 12927 if (!(sata_func_enable & 12928 (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ))) 12929 (void) strlcat(msg_buf, 12930 " - queueing disabled globally", MAXPATHLEN); 12931 else if (sdinfo->satadrv_queue_depth > 12932 sdinfo->satadrv_max_queue_depth) { 12933 (void) snprintf(&msg_buf[strlen(msg_buf)], 12934 MAXPATHLEN - strlen(msg_buf), ", limited to %d", 12935 (int)sdinfo->satadrv_max_queue_depth); 12936 } 12937 cmn_err(CE_CONT, "?\t%s\n", msg_buf); 12938 } 12939 12940 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 12941 (void) sprintf(msg_buf, "\tcapacity = %lu sectors\n", 12942 sdinfo->satadrv_capacity); 12943 cmn_err(CE_CONT, "?%s", msg_buf); 12944 } 12945 } 12946 12947 /* 12948 * Log/display port multiplier information 12949 * No Mutex should be hold. 12950 */ 12951 static void 12952 sata_show_pmult_info(sata_hba_inst_t *sata_hba_inst, 12953 sata_device_t *sata_device) 12954 { 12955 _NOTE(ARGUNUSED(sata_hba_inst)) 12956 12957 int cport = sata_device->satadev_addr.cport; 12958 sata_pmult_info_t *pmultinfo; 12959 char msg_buf[MAXPATHLEN]; 12960 uint32_t gscr0, gscr1, gscr2, gscr64; 12961 12962 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 12963 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 12964 if (pmultinfo == NULL) { 12965 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 12966 return; 12967 } 12968 12969 gscr0 = pmultinfo->pmult_gscr.gscr0; 12970 gscr1 = pmultinfo->pmult_gscr.gscr1; 12971 gscr2 = pmultinfo->pmult_gscr.gscr2; 12972 gscr64 = pmultinfo->pmult_gscr.gscr64; 12973 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 12974 12975 cmn_err(CE_CONT, "?Port Multiplier %d device-ports found at port %d", 12976 sata_device->satadev_add_info, sata_device->satadev_addr.cport); 12977 12978 (void) sprintf(msg_buf, "\tVendor_ID 0x%04x, Module_ID 0x%04x", 12979 gscr0 & 0xffff, (gscr0 >> 16) & 0xffff); 12980 cmn_err(CE_CONT, "?%s", msg_buf); 12981 12982 (void) strcpy(msg_buf, "\tSupport SATA PMP Spec "); 12983 if (gscr1 & (1 << 3)) 12984 (void) strlcat(msg_buf, "1.2", MAXPATHLEN); 12985 else if (gscr1 & (1 << 2)) 12986 (void) strlcat(msg_buf, "1.1", MAXPATHLEN); 12987 else if (gscr1 & (1 << 1)) 12988 (void) strlcat(msg_buf, "1.0", MAXPATHLEN); 12989 else 12990 (void) strlcat(msg_buf, "unknown", MAXPATHLEN); 12991 cmn_err(CE_CONT, "?%s", msg_buf); 12992 12993 (void) strcpy(msg_buf, "\tSupport "); 12994 if (gscr64 & (1 << 3)) 12995 (void) strlcat(msg_buf, "Asy-Notif, ", 12996 MAXPATHLEN); 12997 if (gscr64 & (1 << 2)) 12998 (void) strlcat(msg_buf, "Dyn-SSC, ", MAXPATHLEN); 12999 if (gscr64 & (1 << 1)) 13000 (void) strlcat(msg_buf, "Iss-PMREQ, ", MAXPATHLEN); 13001 if (gscr64 & (1 << 0)) 13002 (void) strlcat(msg_buf, "BIST", MAXPATHLEN); 13003 if ((gscr64 & 0xf) == 0) 13004 (void) strlcat(msg_buf, "nothing", MAXPATHLEN); 13005 cmn_err(CE_CONT, "?%s", msg_buf); 13006 13007 (void) sprintf(msg_buf, "\tNumber of exposed device fan-out ports: %d", 13008 gscr2 & SATA_PMULT_PORTNUM_MASK); 13009 cmn_err(CE_CONT, "?%s", msg_buf); 13010 } 13011 13012 /* 13013 * sata_save_drive_settings extracts current setting of the device and stores 13014 * it for future reference, in case the device setup would need to be restored 13015 * after the device reset. 13016 * 13017 * For all devices read ahead and write cache settings are saved, if the 13018 * device supports these features at all. 13019 * For ATAPI devices the Removable Media Status Notification setting is saved. 13020 */ 13021 static void 13022 sata_save_drive_settings(sata_drive_info_t *sdinfo) 13023 { 13024 if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) || 13025 SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) { 13026 13027 /* Current setting of Read Ahead (and Read Cache) */ 13028 if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id)) 13029 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 13030 else 13031 sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD; 13032 13033 /* Current setting of Write Cache */ 13034 if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id)) 13035 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 13036 else 13037 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 13038 } 13039 13040 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) { 13041 if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id)) 13042 sdinfo->satadrv_settings |= SATA_DEV_RMSN; 13043 else 13044 sdinfo->satadrv_settings &= ~SATA_DEV_RMSN; 13045 } 13046 } 13047 13048 13049 /* 13050 * sata_check_capacity function determines a disk capacity 13051 * and addressing mode (LBA28/LBA48) by examining a disk identify device data. 13052 * 13053 * NOTE: CHS mode is not supported! If a device does not support LBA, 13054 * this function is not called. 13055 * 13056 * Returns device capacity in number of blocks, i.e. largest addressable LBA+1 13057 */ 13058 static uint64_t 13059 sata_check_capacity(sata_drive_info_t *sdinfo) 13060 { 13061 uint64_t capacity = 0; 13062 int i; 13063 13064 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK || 13065 !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT) 13066 /* Capacity valid only for LBA-addressable disk devices */ 13067 return (0); 13068 13069 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) && 13070 (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) && 13071 (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) { 13072 /* LBA48 mode supported and enabled */ 13073 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 | 13074 SATA_DEV_F_LBA28; 13075 for (i = 3; i >= 0; --i) { 13076 capacity <<= 16; 13077 capacity += sdinfo->satadrv_id.ai_addrsecxt[i]; 13078 } 13079 } else { 13080 capacity = sdinfo->satadrv_id.ai_addrsec[1]; 13081 capacity <<= 16; 13082 capacity += sdinfo->satadrv_id.ai_addrsec[0]; 13083 if (capacity >= 0x1000000) 13084 /* LBA28 mode */ 13085 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28; 13086 } 13087 return (capacity); 13088 } 13089 13090 13091 /* 13092 * Allocate consistent buffer for DMA transfer 13093 * 13094 * Cannot be called from interrupt level or with mutex held - it may sleep. 13095 * 13096 * Returns pointer to allocated buffer structure, or NULL if allocation failed. 13097 */ 13098 static struct buf * 13099 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len) 13100 { 13101 struct scsi_address ap; 13102 struct buf *bp; 13103 ddi_dma_attr_t cur_dma_attr; 13104 13105 ASSERT(spx->txlt_sata_pkt != NULL); 13106 ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran; 13107 ap.a_target = SATA_TO_SCSI_TARGET( 13108 spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport, 13109 spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport, 13110 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual); 13111 ap.a_lun = 0; 13112 13113 bp = scsi_alloc_consistent_buf(&ap, NULL, len, 13114 B_READ, SLEEP_FUNC, NULL); 13115 13116 if (bp != NULL) { 13117 /* Allocate DMA resources for this buffer */ 13118 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 13119 /* 13120 * We use a local version of the dma_attr, to account 13121 * for a device addressing limitations. 13122 * sata_adjust_dma_attr() will handle sdinfo == NULL which 13123 * will cause dma attributes to be adjusted to a lowest 13124 * acceptable level. 13125 */ 13126 sata_adjust_dma_attr(NULL, 13127 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 13128 13129 if (sata_dma_buf_setup(spx, PKT_CONSISTENT, 13130 SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) { 13131 scsi_free_consistent_buf(bp); 13132 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 13133 bp = NULL; 13134 } 13135 } 13136 return (bp); 13137 } 13138 13139 /* 13140 * Release local buffer (consistent buffer for DMA transfer) allocated 13141 * via sata_alloc_local_buffer(). 13142 */ 13143 static void 13144 sata_free_local_buffer(sata_pkt_txlate_t *spx) 13145 { 13146 ASSERT(spx->txlt_sata_pkt != NULL); 13147 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL); 13148 13149 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0; 13150 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL; 13151 13152 sata_common_free_dma_rsrcs(spx); 13153 13154 /* Free buffer */ 13155 scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp); 13156 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 13157 } 13158 13159 /* 13160 * Allocate sata_pkt 13161 * Pkt structure version and embedded strcutures version are initialized. 13162 * sata_pkt and sata_pkt_txlate structures are cross-linked. 13163 * 13164 * Since this may be called in interrupt context by sata_scsi_init_pkt, 13165 * callback argument determines if it can sleep or not. 13166 * Hence, it should not be called from interrupt context. 13167 * 13168 * If successful, non-NULL pointer to a sata pkt is returned. 13169 * Upon failure, NULL pointer is returned. 13170 */ 13171 static sata_pkt_t * 13172 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t)) 13173 { 13174 sata_pkt_t *spkt; 13175 int kmsflag; 13176 13177 kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP; 13178 spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag); 13179 if (spkt == NULL) { 13180 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 13181 "sata_pkt_alloc: failed")); 13182 return (NULL); 13183 } 13184 spkt->satapkt_rev = SATA_PKT_REV; 13185 spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV; 13186 spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 13187 spkt->satapkt_framework_private = spx; 13188 spx->txlt_sata_pkt = spkt; 13189 return (spkt); 13190 } 13191 13192 /* 13193 * Free sata pkt allocated via sata_pkt_alloc() 13194 */ 13195 static void 13196 sata_pkt_free(sata_pkt_txlate_t *spx) 13197 { 13198 ASSERT(spx->txlt_sata_pkt != NULL); 13199 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL); 13200 kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t)); 13201 spx->txlt_sata_pkt = NULL; 13202 } 13203 13204 13205 /* 13206 * Adjust DMA attributes. 13207 * SCSI cmds block count is up to 24 bits, SATA cmd block count vary 13208 * from 8 bits to 16 bits, depending on a command being used. 13209 * Limiting max block count arbitrarily to 256 for all read/write 13210 * commands may affects performance, so check both the device and 13211 * controller capability before adjusting dma attributes. 13212 */ 13213 void 13214 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr, 13215 ddi_dma_attr_t *adj_dma_attr) 13216 { 13217 uint32_t count_max; 13218 13219 /* Copy original attributes */ 13220 *adj_dma_attr = *dma_attr; 13221 /* 13222 * Things to consider: device addressing capability, 13223 * "excessive" controller DMA capabilities. 13224 * If a device is being probed/initialized, there are 13225 * no device info - use default limits then. 13226 */ 13227 if (sdinfo == NULL) { 13228 count_max = dma_attr->dma_attr_granular * 0x100; 13229 if (dma_attr->dma_attr_count_max > count_max) 13230 adj_dma_attr->dma_attr_count_max = count_max; 13231 if (dma_attr->dma_attr_maxxfer > count_max) 13232 adj_dma_attr->dma_attr_maxxfer = count_max; 13233 return; 13234 } 13235 13236 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 13237 if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) { 13238 /* 13239 * 16-bit sector count may be used - we rely on 13240 * the assumption that only read and write cmds 13241 * will request more than 256 sectors worth of data 13242 */ 13243 count_max = adj_dma_attr->dma_attr_granular * 0x10000; 13244 } else { 13245 /* 13246 * 8-bit sector count will be used - default limits 13247 * for dma attributes 13248 */ 13249 count_max = adj_dma_attr->dma_attr_granular * 0x100; 13250 } 13251 /* 13252 * Adjust controler dma attributes, if necessary 13253 */ 13254 if (dma_attr->dma_attr_count_max > count_max) 13255 adj_dma_attr->dma_attr_count_max = count_max; 13256 if (dma_attr->dma_attr_maxxfer > count_max) 13257 adj_dma_attr->dma_attr_maxxfer = count_max; 13258 } 13259 } 13260 13261 13262 /* 13263 * Allocate DMA resources for the buffer 13264 * This function handles initial DMA resource allocation as well as 13265 * DMA window shift and may be called repeatedly for the same DMA window 13266 * until all DMA cookies in the DMA window are processed. 13267 * To guarantee that there is always a coherent set of cookies to process 13268 * by SATA HBA driver (observing alignment, device granularity, etc.), 13269 * the number of slots for DMA cookies is equal to lesser of a number of 13270 * cookies in a DMA window and a max number of scatter/gather entries. 13271 * 13272 * Returns DDI_SUCCESS upon successful operation. 13273 * Return failure code of a failing command or DDI_FAILURE when 13274 * internal cleanup failed. 13275 */ 13276 static int 13277 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags, 13278 int (*callback)(caddr_t), caddr_t arg, 13279 ddi_dma_attr_t *cur_dma_attr) 13280 { 13281 int rval; 13282 off_t offset; 13283 size_t size; 13284 int max_sg_len, req_len, i; 13285 uint_t dma_flags; 13286 struct buf *bp; 13287 uint64_t cur_txfer_len; 13288 13289 13290 ASSERT(spx->txlt_sata_pkt != NULL); 13291 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 13292 ASSERT(bp != NULL); 13293 13294 13295 if (spx->txlt_buf_dma_handle == NULL) { 13296 /* 13297 * No DMA resources allocated so far - this is a first call 13298 * for this sata pkt. 13299 */ 13300 rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst), 13301 cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle); 13302 13303 if (rval != DDI_SUCCESS) { 13304 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 13305 "sata_dma_buf_setup: no buf DMA resources %x", 13306 rval)); 13307 return (rval); 13308 } 13309 13310 if (bp->b_flags & B_READ) 13311 dma_flags = DDI_DMA_READ; 13312 else 13313 dma_flags = DDI_DMA_WRITE; 13314 13315 if (flags & PKT_CONSISTENT) 13316 dma_flags |= DDI_DMA_CONSISTENT; 13317 13318 if (flags & PKT_DMA_PARTIAL) 13319 dma_flags |= DDI_DMA_PARTIAL; 13320 13321 /* 13322 * Check buffer alignment and size against dma attributes 13323 * Consider dma_attr_align only. There may be requests 13324 * with the size lower than device granularity, but they 13325 * will not read/write from/to the device, so no adjustment 13326 * is necessary. The dma_attr_minxfer theoretically should 13327 * be considered, but no HBA driver is checking it. 13328 */ 13329 if (IS_P2ALIGNED(bp->b_un.b_addr, 13330 cur_dma_attr->dma_attr_align)) { 13331 rval = ddi_dma_buf_bind_handle( 13332 spx->txlt_buf_dma_handle, 13333 bp, dma_flags, callback, arg, 13334 &spx->txlt_dma_cookie, 13335 &spx->txlt_curwin_num_dma_cookies); 13336 } else { /* Buffer is not aligned */ 13337 13338 int (*ddicallback)(caddr_t); 13339 size_t bufsz; 13340 13341 /* Check id sleeping is allowed */ 13342 ddicallback = (callback == NULL_FUNC) ? 13343 DDI_DMA_DONTWAIT : DDI_DMA_SLEEP; 13344 13345 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 13346 "mis-aligned buffer: addr=0x%p, cnt=%lu", 13347 (void *)bp->b_un.b_addr, bp->b_bcount); 13348 13349 if (bp->b_flags & (B_PAGEIO|B_PHYS)) 13350 /* 13351 * CPU will need to access data in the buffer 13352 * (for copying) so map it. 13353 */ 13354 bp_mapin(bp); 13355 13356 ASSERT(spx->txlt_tmp_buf == NULL); 13357 13358 /* Buffer may be padded by ddi_dma_mem_alloc()! */ 13359 rval = ddi_dma_mem_alloc( 13360 spx->txlt_buf_dma_handle, 13361 bp->b_bcount, 13362 &sata_acc_attr, 13363 DDI_DMA_STREAMING, 13364 ddicallback, NULL, 13365 &spx->txlt_tmp_buf, 13366 &bufsz, 13367 &spx->txlt_tmp_buf_handle); 13368 13369 if (rval != DDI_SUCCESS) { 13370 /* DMA mapping failed */ 13371 (void) ddi_dma_free_handle( 13372 &spx->txlt_buf_dma_handle); 13373 spx->txlt_buf_dma_handle = NULL; 13374 #ifdef SATA_DEBUG 13375 mbuffail_count++; 13376 #endif 13377 SATADBG1(SATA_DBG_DMA_SETUP, 13378 spx->txlt_sata_hba_inst, 13379 "sata_dma_buf_setup: " 13380 "buf dma mem alloc failed %x\n", rval); 13381 return (rval); 13382 } 13383 ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf, 13384 cur_dma_attr->dma_attr_align)); 13385 13386 #ifdef SATA_DEBUG 13387 mbuf_count++; 13388 13389 if (bp->b_bcount != bufsz) 13390 /* 13391 * This will require special handling, because 13392 * DMA cookies will be based on the temporary 13393 * buffer size, not the original buffer 13394 * b_bcount, so the residue may have to 13395 * be counted differently. 13396 */ 13397 SATADBG2(SATA_DBG_DMA_SETUP, 13398 spx->txlt_sata_hba_inst, 13399 "sata_dma_buf_setup: bp size %x != " 13400 "bufsz %x\n", bp->b_bcount, bufsz); 13401 #endif 13402 if (dma_flags & DDI_DMA_WRITE) { 13403 /* 13404 * Write operation - copy data into 13405 * an aligned temporary buffer. Buffer will be 13406 * synced for device by ddi_dma_addr_bind_handle 13407 */ 13408 bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf, 13409 bp->b_bcount); 13410 } 13411 13412 rval = ddi_dma_addr_bind_handle( 13413 spx->txlt_buf_dma_handle, 13414 NULL, 13415 spx->txlt_tmp_buf, 13416 bufsz, dma_flags, ddicallback, 0, 13417 &spx->txlt_dma_cookie, 13418 &spx->txlt_curwin_num_dma_cookies); 13419 } 13420 13421 switch (rval) { 13422 case DDI_DMA_PARTIAL_MAP: 13423 SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 13424 "sata_dma_buf_setup: DMA Partial Map\n", NULL); 13425 /* 13426 * Partial DMA mapping. 13427 * Retrieve number of DMA windows for this request. 13428 */ 13429 if (ddi_dma_numwin(spx->txlt_buf_dma_handle, 13430 &spx->txlt_num_dma_win) != DDI_SUCCESS) { 13431 if (spx->txlt_tmp_buf != NULL) { 13432 ddi_dma_mem_free( 13433 &spx->txlt_tmp_buf_handle); 13434 spx->txlt_tmp_buf = NULL; 13435 } 13436 (void) ddi_dma_unbind_handle( 13437 spx->txlt_buf_dma_handle); 13438 (void) ddi_dma_free_handle( 13439 &spx->txlt_buf_dma_handle); 13440 spx->txlt_buf_dma_handle = NULL; 13441 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 13442 "sata_dma_buf_setup: numwin failed\n")); 13443 return (DDI_FAILURE); 13444 } 13445 SATADBG2(SATA_DBG_DMA_SETUP, 13446 spx->txlt_sata_hba_inst, 13447 "sata_dma_buf_setup: windows: %d, cookies: %d\n", 13448 spx->txlt_num_dma_win, 13449 spx->txlt_curwin_num_dma_cookies); 13450 spx->txlt_cur_dma_win = 0; 13451 break; 13452 13453 case DDI_DMA_MAPPED: 13454 /* DMA fully mapped */ 13455 spx->txlt_num_dma_win = 1; 13456 spx->txlt_cur_dma_win = 0; 13457 SATADBG1(SATA_DBG_DMA_SETUP, 13458 spx->txlt_sata_hba_inst, 13459 "sata_dma_buf_setup: windows: 1 " 13460 "cookies: %d\n", spx->txlt_curwin_num_dma_cookies); 13461 break; 13462 13463 default: 13464 /* DMA mapping failed */ 13465 if (spx->txlt_tmp_buf != NULL) { 13466 ddi_dma_mem_free( 13467 &spx->txlt_tmp_buf_handle); 13468 spx->txlt_tmp_buf = NULL; 13469 } 13470 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 13471 spx->txlt_buf_dma_handle = NULL; 13472 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 13473 "sata_dma_buf_setup: buf dma handle binding " 13474 "failed %x\n", rval)); 13475 return (rval); 13476 } 13477 spx->txlt_curwin_processed_dma_cookies = 0; 13478 spx->txlt_dma_cookie_list = NULL; 13479 } else { 13480 /* 13481 * DMA setup is reused. Check if we need to process more 13482 * cookies in current window, or to get next window, if any. 13483 */ 13484 13485 ASSERT(spx->txlt_curwin_processed_dma_cookies <= 13486 spx->txlt_curwin_num_dma_cookies); 13487 13488 if (spx->txlt_curwin_processed_dma_cookies == 13489 spx->txlt_curwin_num_dma_cookies) { 13490 /* 13491 * All cookies from current DMA window were processed. 13492 * Get next DMA window. 13493 */ 13494 spx->txlt_cur_dma_win++; 13495 if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) { 13496 (void) ddi_dma_getwin(spx->txlt_buf_dma_handle, 13497 spx->txlt_cur_dma_win, &offset, &size, 13498 &spx->txlt_dma_cookie, 13499 &spx->txlt_curwin_num_dma_cookies); 13500 spx->txlt_curwin_processed_dma_cookies = 0; 13501 } else { 13502 /* No more windows! End of request! */ 13503 /* What to do? - panic for now */ 13504 ASSERT(spx->txlt_cur_dma_win >= 13505 spx->txlt_num_dma_win); 13506 13507 spx->txlt_curwin_num_dma_cookies = 0; 13508 spx->txlt_curwin_processed_dma_cookies = 0; 13509 spx->txlt_sata_pkt-> 13510 satapkt_cmd.satacmd_num_dma_cookies = 0; 13511 return (DDI_SUCCESS); 13512 } 13513 } 13514 } 13515 /* There better be at least one DMA cookie outstanding */ 13516 ASSERT((spx->txlt_curwin_num_dma_cookies - 13517 spx->txlt_curwin_processed_dma_cookies) > 0); 13518 13519 if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) { 13520 /* The default cookie slot was used in previous run */ 13521 ASSERT(spx->txlt_curwin_processed_dma_cookies == 0); 13522 spx->txlt_dma_cookie_list = NULL; 13523 spx->txlt_dma_cookie_list_len = 0; 13524 } 13525 if (spx->txlt_curwin_processed_dma_cookies == 0) { 13526 /* 13527 * Processing a new DMA window - set-up dma cookies list. 13528 * We may reuse previously allocated cookie array if it is 13529 * possible. 13530 */ 13531 if (spx->txlt_dma_cookie_list != NULL && 13532 spx->txlt_dma_cookie_list_len < 13533 spx->txlt_curwin_num_dma_cookies) { 13534 /* 13535 * New DMA window contains more cookies than 13536 * the previous one. We need larger cookie list - free 13537 * the old one. 13538 */ 13539 (void) kmem_free(spx->txlt_dma_cookie_list, 13540 spx->txlt_dma_cookie_list_len * 13541 sizeof (ddi_dma_cookie_t)); 13542 spx->txlt_dma_cookie_list = NULL; 13543 spx->txlt_dma_cookie_list_len = 0; 13544 } 13545 if (spx->txlt_dma_cookie_list == NULL) { 13546 /* 13547 * Calculate lesser of number of cookies in this 13548 * DMA window and number of s/g entries. 13549 */ 13550 max_sg_len = cur_dma_attr->dma_attr_sgllen; 13551 req_len = MIN(max_sg_len, 13552 spx->txlt_curwin_num_dma_cookies); 13553 13554 /* Allocate new dma cookie array if necessary */ 13555 if (req_len == 1) { 13556 /* Only one cookie - no need for a list */ 13557 spx->txlt_dma_cookie_list = 13558 &spx->txlt_dma_cookie; 13559 spx->txlt_dma_cookie_list_len = 1; 13560 } else { 13561 /* 13562 * More than one cookie - try to allocate space. 13563 */ 13564 spx->txlt_dma_cookie_list = kmem_zalloc( 13565 sizeof (ddi_dma_cookie_t) * req_len, 13566 callback == NULL_FUNC ? KM_NOSLEEP : 13567 KM_SLEEP); 13568 if (spx->txlt_dma_cookie_list == NULL) { 13569 SATADBG1(SATA_DBG_DMA_SETUP, 13570 spx->txlt_sata_hba_inst, 13571 "sata_dma_buf_setup: cookie list " 13572 "allocation failed\n", NULL); 13573 /* 13574 * We could not allocate space for 13575 * neccessary number of dma cookies in 13576 * this window, so we fail this request. 13577 * Next invocation would try again to 13578 * allocate space for cookie list. 13579 * Note:Packet residue was not modified. 13580 */ 13581 return (DDI_DMA_NORESOURCES); 13582 } else { 13583 spx->txlt_dma_cookie_list_len = req_len; 13584 } 13585 } 13586 } 13587 /* 13588 * Fetch DMA cookies into cookie list in sata_pkt_txlate. 13589 * First cookie was already fetched. 13590 */ 13591 *(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie; 13592 cur_txfer_len = 13593 (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size; 13594 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1; 13595 spx->txlt_curwin_processed_dma_cookies++; 13596 for (i = 1; (i < spx->txlt_dma_cookie_list_len) && 13597 (i < spx->txlt_curwin_num_dma_cookies); i++) { 13598 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 13599 &spx->txlt_dma_cookie_list[i]); 13600 cur_txfer_len += 13601 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 13602 spx->txlt_curwin_processed_dma_cookies++; 13603 spx->txlt_sata_pkt-> 13604 satapkt_cmd.satacmd_num_dma_cookies += 1; 13605 } 13606 } else { 13607 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 13608 "sata_dma_buf_setup: sliding within DMA window, " 13609 "cur cookie %d, total cookies %d\n", 13610 spx->txlt_curwin_processed_dma_cookies, 13611 spx->txlt_curwin_num_dma_cookies); 13612 13613 /* 13614 * Not all cookies from the current dma window were used because 13615 * of s/g limitation. 13616 * There is no need to re-size the list - it was set at 13617 * optimal size, or only default entry is used (s/g = 1). 13618 */ 13619 if (spx->txlt_dma_cookie_list == NULL) { 13620 spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie; 13621 spx->txlt_dma_cookie_list_len = 1; 13622 } 13623 /* 13624 * Since we are processing remaining cookies in a DMA window, 13625 * there may be less of them than the number of entries in the 13626 * current dma cookie list. 13627 */ 13628 req_len = MIN(spx->txlt_dma_cookie_list_len, 13629 (spx->txlt_curwin_num_dma_cookies - 13630 spx->txlt_curwin_processed_dma_cookies)); 13631 13632 /* Fetch the next batch of cookies */ 13633 for (i = 0, cur_txfer_len = 0; i < req_len; i++) { 13634 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 13635 &spx->txlt_dma_cookie_list[i]); 13636 cur_txfer_len += 13637 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 13638 spx->txlt_sata_pkt-> 13639 satapkt_cmd.satacmd_num_dma_cookies++; 13640 spx->txlt_curwin_processed_dma_cookies++; 13641 } 13642 } 13643 13644 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0); 13645 13646 /* Point sata_cmd to the cookie list */ 13647 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = 13648 &spx->txlt_dma_cookie_list[0]; 13649 13650 /* Remember number of DMA cookies passed in sata packet */ 13651 spx->txlt_num_dma_cookies = 13652 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies; 13653 13654 ASSERT(cur_txfer_len != 0); 13655 if (cur_txfer_len <= bp->b_bcount) 13656 spx->txlt_total_residue -= cur_txfer_len; 13657 else { 13658 /* 13659 * Temporary DMA buffer has been padded by 13660 * ddi_dma_mem_alloc()! 13661 * This requires special handling, because DMA cookies are 13662 * based on the temporary buffer size, not the b_bcount, 13663 * and we have extra bytes to transfer - but the packet 13664 * residue has to stay correct because we will copy only 13665 * the requested number of bytes. 13666 */ 13667 spx->txlt_total_residue -= bp->b_bcount; 13668 } 13669 13670 return (DDI_SUCCESS); 13671 } 13672 13673 /* 13674 * Common routine for releasing DMA resources 13675 */ 13676 static void 13677 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx) 13678 { 13679 if (spx->txlt_buf_dma_handle != NULL) { 13680 if (spx->txlt_tmp_buf != NULL) { 13681 /* 13682 * Intermediate DMA buffer was allocated. 13683 * Free allocated buffer and associated access handle. 13684 */ 13685 ddi_dma_mem_free(&spx->txlt_tmp_buf_handle); 13686 spx->txlt_tmp_buf = NULL; 13687 } 13688 /* 13689 * Free DMA resources - cookies and handles 13690 */ 13691 /* ASSERT(spx->txlt_dma_cookie_list != NULL); */ 13692 if (spx->txlt_dma_cookie_list != NULL) { 13693 if (spx->txlt_dma_cookie_list != 13694 &spx->txlt_dma_cookie) { 13695 (void) kmem_free(spx->txlt_dma_cookie_list, 13696 spx->txlt_dma_cookie_list_len * 13697 sizeof (ddi_dma_cookie_t)); 13698 spx->txlt_dma_cookie_list = NULL; 13699 } 13700 } 13701 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle); 13702 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 13703 spx->txlt_buf_dma_handle = NULL; 13704 } 13705 } 13706 13707 /* 13708 * Free DMA resources 13709 * Used by the HBA driver to release DMA resources that it does not use. 13710 * 13711 * Returns Void 13712 */ 13713 void 13714 sata_free_dma_resources(sata_pkt_t *sata_pkt) 13715 { 13716 sata_pkt_txlate_t *spx; 13717 13718 if (sata_pkt == NULL) 13719 return; 13720 13721 spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 13722 13723 sata_common_free_dma_rsrcs(spx); 13724 } 13725 13726 /* 13727 * Fetch Device Identify data. 13728 * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type) 13729 * command to a device and get the device identify data. 13730 * The device_info structure has to be set to device type (for selecting proper 13731 * device identify command). 13732 * 13733 * Returns: 13734 * SATA_SUCCESS if cmd succeeded 13735 * SATA_RETRY if cmd was rejected and could be retried, 13736 * SATA_FAILURE if cmd failed and should not be retried (port error) 13737 * 13738 * Cannot be called in an interrupt context. 13739 */ 13740 13741 static int 13742 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst, 13743 sata_drive_info_t *sdinfo) 13744 { 13745 struct buf *bp; 13746 sata_pkt_t *spkt; 13747 sata_cmd_t *scmd; 13748 sata_pkt_txlate_t *spx; 13749 int rval; 13750 dev_info_t *dip = SATA_DIP(sata_hba_inst); 13751 13752 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13753 spx->txlt_sata_hba_inst = sata_hba_inst; 13754 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13755 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13756 if (spkt == NULL) { 13757 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13758 return (SATA_RETRY); /* may retry later */ 13759 } 13760 /* address is needed now */ 13761 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13762 13763 /* 13764 * Allocate buffer for Identify Data return data 13765 */ 13766 bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t)); 13767 if (bp == NULL) { 13768 sata_pkt_free(spx); 13769 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13770 SATA_LOG_D((sata_hba_inst, CE_WARN, 13771 "sata_fetch_device_identify_data: " 13772 "cannot allocate buffer for ID")); 13773 return (SATA_RETRY); /* may retry later */ 13774 } 13775 13776 /* Fill sata_pkt */ 13777 sdinfo->satadrv_state = SATA_STATE_PROBING; 13778 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13779 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13780 /* Synchronous mode, no callback */ 13781 spkt->satapkt_comp = NULL; 13782 /* Timeout 30s */ 13783 spkt->satapkt_time = sata_default_pkt_time; 13784 13785 scmd = &spkt->satapkt_cmd; 13786 scmd->satacmd_bp = bp; 13787 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 13788 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 13789 13790 /* Build Identify Device cmd in the sata_pkt */ 13791 scmd->satacmd_addr_type = 0; /* N/A */ 13792 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 13793 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 13794 scmd->satacmd_lba_mid_lsb = 0; /* N/A */ 13795 scmd->satacmd_lba_high_lsb = 0; /* N/A */ 13796 scmd->satacmd_features_reg = 0; /* N/A */ 13797 scmd->satacmd_device_reg = 0; /* Always device 0 */ 13798 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 13799 /* Identify Packet Device cmd */ 13800 scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE; 13801 } else { 13802 /* Identify Device cmd - mandatory for all other devices */ 13803 scmd->satacmd_cmd_reg = SATAC_ID_DEVICE; 13804 } 13805 13806 /* Send pkt to SATA HBA driver */ 13807 rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt); 13808 13809 #ifdef SATA_INJECT_FAULTS 13810 sata_inject_pkt_fault(spkt, &rval, sata_fault_type); 13811 #endif 13812 13813 if (rval == SATA_TRAN_ACCEPTED && 13814 spkt->satapkt_reason == SATA_PKT_COMPLETED) { 13815 if (spx->txlt_buf_dma_handle != NULL) { 13816 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 13817 DDI_DMA_SYNC_FORKERNEL); 13818 ASSERT(rval == DDI_SUCCESS); 13819 if (sata_check_for_dma_error(dip, spx)) { 13820 ddi_fm_service_impact(dip, 13821 DDI_SERVICE_UNAFFECTED); 13822 rval = SATA_RETRY; 13823 goto fail; 13824 } 13825 13826 } 13827 if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config & 13828 SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) { 13829 SATA_LOG_D((sata_hba_inst, CE_WARN, 13830 "SATA disk device at port %d - " 13831 "partial Identify Data", 13832 sdinfo->satadrv_addr.cport)); 13833 rval = SATA_RETRY; /* may retry later */ 13834 goto fail; 13835 } 13836 /* Update sata_drive_info */ 13837 bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id, 13838 sizeof (sata_id_t)); 13839 13840 sdinfo->satadrv_features_support = 0; 13841 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 13842 /* 13843 * Retrieve capacity (disks only) and addressing mode 13844 */ 13845 sdinfo->satadrv_capacity = sata_check_capacity(sdinfo); 13846 } else { 13847 /* 13848 * For ATAPI devices one would have to issue 13849 * Get Capacity cmd for media capacity. Not here. 13850 */ 13851 sdinfo->satadrv_capacity = 0; 13852 /* 13853 * Check what cdb length is supported 13854 */ 13855 if ((sdinfo->satadrv_id.ai_config & 13856 SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B) 13857 sdinfo->satadrv_atapi_cdb_len = 16; 13858 else 13859 sdinfo->satadrv_atapi_cdb_len = 12; 13860 } 13861 /* Setup supported features flags */ 13862 if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) 13863 sdinfo->satadrv_features_support |= SATA_DEV_F_DMA; 13864 13865 /* Check for SATA GEN and NCQ support */ 13866 if (sdinfo->satadrv_id.ai_satacap != 0 && 13867 sdinfo->satadrv_id.ai_satacap != 0xffff) { 13868 /* SATA compliance */ 13869 if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ) 13870 sdinfo->satadrv_features_support |= 13871 SATA_DEV_F_NCQ; 13872 if (sdinfo->satadrv_id.ai_satacap & 13873 (SATA_1_SPEED | SATA_2_SPEED | SATA_3_SPEED)) { 13874 if (sdinfo->satadrv_id.ai_satacap & 13875 SATA_3_SPEED) 13876 sdinfo->satadrv_features_support |= 13877 SATA_DEV_F_SATA3; 13878 if (sdinfo->satadrv_id.ai_satacap & 13879 SATA_2_SPEED) 13880 sdinfo->satadrv_features_support |= 13881 SATA_DEV_F_SATA2; 13882 if (sdinfo->satadrv_id.ai_satacap & 13883 SATA_1_SPEED) 13884 sdinfo->satadrv_features_support |= 13885 SATA_DEV_F_SATA1; 13886 } else { 13887 sdinfo->satadrv_features_support |= 13888 SATA_DEV_F_SATA1; 13889 } 13890 } 13891 if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) && 13892 (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD)) 13893 sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ; 13894 13895 sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth; 13896 if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) || 13897 (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) { 13898 ++sdinfo->satadrv_queue_depth; 13899 /* Adjust according to controller capabilities */ 13900 sdinfo->satadrv_max_queue_depth = MIN( 13901 sdinfo->satadrv_queue_depth, 13902 SATA_QDEPTH(sata_hba_inst)); 13903 /* Adjust according to global queue depth limit */ 13904 sdinfo->satadrv_max_queue_depth = MIN( 13905 sdinfo->satadrv_max_queue_depth, 13906 sata_current_max_qdepth); 13907 if (sdinfo->satadrv_max_queue_depth == 0) 13908 sdinfo->satadrv_max_queue_depth = 1; 13909 } else 13910 sdinfo->satadrv_max_queue_depth = 1; 13911 13912 rval = SATA_SUCCESS; 13913 } else { 13914 /* 13915 * Woops, no Identify Data. 13916 */ 13917 if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) { 13918 rval = SATA_RETRY; /* may retry later */ 13919 } else if (rval == SATA_TRAN_ACCEPTED) { 13920 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR || 13921 spkt->satapkt_reason == SATA_PKT_ABORTED || 13922 spkt->satapkt_reason == SATA_PKT_TIMEOUT || 13923 spkt->satapkt_reason == SATA_PKT_RESET) 13924 rval = SATA_RETRY; /* may retry later */ 13925 else 13926 rval = SATA_FAILURE; 13927 } else { 13928 rval = SATA_FAILURE; 13929 } 13930 } 13931 fail: 13932 /* Free allocated resources */ 13933 sata_free_local_buffer(spx); 13934 sata_pkt_free(spx); 13935 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13936 13937 return (rval); 13938 } 13939 13940 13941 /* 13942 * Some devices may not come-up with default DMA mode (UDMA or MWDMA). 13943 * UDMA mode is checked first, followed by MWDMA mode. 13944 * set correctly, so this function is setting it to the highest supported level. 13945 * Older SATA spec required that the device supports at least DMA 4 mode and 13946 * UDMA mode is selected. It is not mentioned in SerialATA 2.6, so this 13947 * restriction has been removed. 13948 * 13949 * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported. 13950 * Returns SATA_FAILURE if proper DMA mode could not be selected. 13951 * 13952 * NOTE: This function should be called only if DMA mode is supported. 13953 */ 13954 static int 13955 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo) 13956 { 13957 sata_pkt_t *spkt; 13958 sata_cmd_t *scmd; 13959 sata_pkt_txlate_t *spx; 13960 int i, mode; 13961 uint8_t subcmd; 13962 int rval = SATA_SUCCESS; 13963 13964 ASSERT(sdinfo != NULL); 13965 ASSERT(sata_hba_inst != NULL); 13966 13967 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 && 13968 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) { 13969 /* Find highest Ultra DMA mode supported */ 13970 for (mode = 6; mode >= 0; --mode) { 13971 if (sdinfo->satadrv_id.ai_ultradma & (1 << mode)) 13972 break; 13973 } 13974 #if 0 13975 /* Left for historical reasons */ 13976 /* 13977 * Some initial version of SATA spec indicated that at least 13978 * UDMA mode 4 has to be supported. It is not mentioned in 13979 * SerialATA 2.6, so this restriction is removed. 13980 */ 13981 if (mode < 4) 13982 return (SATA_FAILURE); 13983 #endif 13984 13985 /* 13986 * For disk, we're still going to set DMA mode whatever is 13987 * selected by default 13988 * 13989 * We saw an old maxtor sata drive will select Ultra DMA and 13990 * Multi-Word DMA simultaneouly by default, which is going 13991 * to cause DMA command timed out, so we need to select DMA 13992 * mode even when it's already done by default 13993 */ 13994 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) { 13995 13996 /* Find UDMA mode currently selected */ 13997 for (i = 6; i >= 0; --i) { 13998 if (sdinfo->satadrv_id.ai_ultradma & 13999 (1 << (i + 8))) 14000 break; 14001 } 14002 if (i >= mode) 14003 /* Nothing to do */ 14004 return (SATA_SUCCESS); 14005 } 14006 14007 subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA; 14008 14009 } else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) { 14010 /* Find highest MultiWord DMA mode supported */ 14011 for (mode = 2; mode >= 0; --mode) { 14012 if (sdinfo->satadrv_id.ai_dworddma & (1 << mode)) 14013 break; 14014 } 14015 14016 /* 14017 * For disk, We're still going to set DMA mode whatever is 14018 * selected by default 14019 * 14020 * We saw an old maxtor sata drive will select Ultra DMA and 14021 * Multi-Word DMA simultaneouly by default, which is going 14022 * to cause DMA command timed out, so we need to select DMA 14023 * mode even when it's already done by default 14024 */ 14025 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) { 14026 14027 /* Find highest MultiWord DMA mode selected */ 14028 for (i = 2; i >= 0; --i) { 14029 if (sdinfo->satadrv_id.ai_dworddma & 14030 (1 << (i + 8))) 14031 break; 14032 } 14033 if (i >= mode) 14034 /* Nothing to do */ 14035 return (SATA_SUCCESS); 14036 } 14037 14038 subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA; 14039 } else 14040 return (SATA_SUCCESS); 14041 14042 /* 14043 * Set DMA mode via SET FEATURES COMMAND. 14044 * Prepare packet for SET FEATURES COMMAND. 14045 */ 14046 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 14047 spx->txlt_sata_hba_inst = sata_hba_inst; 14048 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 14049 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 14050 if (spkt == NULL) { 14051 SATA_LOG_D((sata_hba_inst, CE_WARN, 14052 "sata_set_dma_mode: could not set DMA mode %d", mode)); 14053 rval = SATA_FAILURE; 14054 goto done; 14055 } 14056 /* Fill sata_pkt */ 14057 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 14058 /* Timeout 30s */ 14059 spkt->satapkt_time = sata_default_pkt_time; 14060 /* Synchronous mode, no callback, interrupts */ 14061 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 14062 spkt->satapkt_comp = NULL; 14063 scmd = &spkt->satapkt_cmd; 14064 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 14065 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 14066 scmd->satacmd_addr_type = 0; 14067 scmd->satacmd_device_reg = 0; 14068 scmd->satacmd_status_reg = 0; 14069 scmd->satacmd_error_reg = 0; 14070 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 14071 scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE; 14072 scmd->satacmd_sec_count_lsb = subcmd | mode; 14073 14074 /* Transfer command to HBA */ 14075 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 14076 spkt) != SATA_TRAN_ACCEPTED || 14077 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 14078 /* Pkt execution failed */ 14079 rval = SATA_FAILURE; 14080 } 14081 done: 14082 14083 /* Free allocated resources */ 14084 if (spkt != NULL) 14085 sata_pkt_free(spx); 14086 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 14087 14088 return (rval); 14089 } 14090 14091 14092 /* 14093 * Set device caching mode. 14094 * One of the following operations should be specified: 14095 * SATAC_SF_ENABLE_READ_AHEAD 14096 * SATAC_SF_DISABLE_READ_AHEAD 14097 * SATAC_SF_ENABLE_WRITE_CACHE 14098 * SATAC_SF_DISABLE_WRITE_CACHE 14099 * 14100 * If operation fails, system log messgage is emitted. 14101 * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if 14102 * command was sent but did not succeed, and SATA_FAILURE otherwise. 14103 */ 14104 14105 static int 14106 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 14107 int cache_op) 14108 { 14109 sata_pkt_t *spkt; 14110 sata_cmd_t *scmd; 14111 sata_pkt_txlate_t *spx; 14112 int rval = SATA_SUCCESS; 14113 int hba_rval; 14114 char *infop; 14115 14116 ASSERT(sdinfo != NULL); 14117 ASSERT(sata_hba_inst != NULL); 14118 ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD || 14119 cache_op == SATAC_SF_DISABLE_READ_AHEAD || 14120 cache_op == SATAC_SF_ENABLE_WRITE_CACHE || 14121 cache_op == SATAC_SF_DISABLE_WRITE_CACHE); 14122 14123 14124 /* Prepare packet for SET FEATURES COMMAND */ 14125 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 14126 spx->txlt_sata_hba_inst = sata_hba_inst; 14127 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 14128 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 14129 if (spkt == NULL) { 14130 rval = SATA_FAILURE; 14131 goto failure; 14132 } 14133 /* Fill sata_pkt */ 14134 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 14135 /* Timeout 30s */ 14136 spkt->satapkt_time = sata_default_pkt_time; 14137 /* Synchronous mode, no callback, interrupts */ 14138 spkt->satapkt_op_mode = 14139 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 14140 spkt->satapkt_comp = NULL; 14141 scmd = &spkt->satapkt_cmd; 14142 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 14143 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 14144 scmd->satacmd_addr_type = 0; 14145 scmd->satacmd_device_reg = 0; 14146 scmd->satacmd_status_reg = 0; 14147 scmd->satacmd_error_reg = 0; 14148 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 14149 scmd->satacmd_features_reg = cache_op; 14150 14151 /* Transfer command to HBA */ 14152 hba_rval = (*SATA_START_FUNC(sata_hba_inst))( 14153 SATA_DIP(sata_hba_inst), spkt); 14154 14155 #ifdef SATA_INJECT_FAULTS 14156 sata_inject_pkt_fault(spkt, &rval, sata_fault_type); 14157 #endif 14158 14159 if ((hba_rval != SATA_TRAN_ACCEPTED) || 14160 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 14161 /* Pkt execution failed */ 14162 switch (cache_op) { 14163 case SATAC_SF_ENABLE_READ_AHEAD: 14164 infop = "enabling read ahead failed"; 14165 break; 14166 case SATAC_SF_DISABLE_READ_AHEAD: 14167 infop = "disabling read ahead failed"; 14168 break; 14169 case SATAC_SF_ENABLE_WRITE_CACHE: 14170 infop = "enabling write cache failed"; 14171 break; 14172 case SATAC_SF_DISABLE_WRITE_CACHE: 14173 infop = "disabling write cache failed"; 14174 break; 14175 } 14176 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 14177 rval = SATA_RETRY; 14178 } 14179 failure: 14180 /* Free allocated resources */ 14181 if (spkt != NULL) 14182 sata_pkt_free(spx); 14183 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 14184 return (rval); 14185 } 14186 14187 /* 14188 * Set Removable Media Status Notification (enable/disable) 14189 * state == 0 , disable 14190 * state != 0 , enable 14191 * 14192 * If operation fails, system log messgage is emitted. 14193 * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise. 14194 */ 14195 14196 static int 14197 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 14198 int state) 14199 { 14200 sata_pkt_t *spkt; 14201 sata_cmd_t *scmd; 14202 sata_pkt_txlate_t *spx; 14203 int rval = SATA_SUCCESS; 14204 char *infop; 14205 14206 ASSERT(sdinfo != NULL); 14207 ASSERT(sata_hba_inst != NULL); 14208 14209 /* Prepare packet for SET FEATURES COMMAND */ 14210 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 14211 spx->txlt_sata_hba_inst = sata_hba_inst; 14212 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 14213 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 14214 if (spkt == NULL) { 14215 rval = SATA_FAILURE; 14216 goto failure; 14217 } 14218 /* Fill sata_pkt */ 14219 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 14220 /* Timeout 30s */ 14221 spkt->satapkt_time = sata_default_pkt_time; 14222 /* Synchronous mode, no callback, interrupts */ 14223 spkt->satapkt_op_mode = 14224 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 14225 spkt->satapkt_comp = NULL; 14226 scmd = &spkt->satapkt_cmd; 14227 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 14228 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 14229 scmd->satacmd_addr_type = 0; 14230 scmd->satacmd_device_reg = 0; 14231 scmd->satacmd_status_reg = 0; 14232 scmd->satacmd_error_reg = 0; 14233 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 14234 if (state == 0) 14235 scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN; 14236 else 14237 scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN; 14238 14239 /* Transfer command to HBA */ 14240 if (((*SATA_START_FUNC(sata_hba_inst))( 14241 SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) || 14242 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 14243 /* Pkt execution failed */ 14244 if (state == 0) 14245 infop = "disabling Removable Media Status " 14246 "Notification failed"; 14247 else 14248 infop = "enabling Removable Media Status " 14249 "Notification failed"; 14250 14251 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 14252 rval = SATA_FAILURE; 14253 } 14254 failure: 14255 /* Free allocated resources */ 14256 if (spkt != NULL) 14257 sata_pkt_free(spx); 14258 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 14259 return (rval); 14260 } 14261 14262 14263 /* 14264 * Update state and copy port ss* values from passed sata_device structure. 14265 * sata_address is validated - if not valid, nothing is changed in sata_scsi 14266 * configuration struct. 14267 * 14268 * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function 14269 * regardless of the state in device argument. 14270 * 14271 * Port mutex should be held while calling this function. 14272 */ 14273 static void 14274 sata_update_port_info(sata_hba_inst_t *sata_hba_inst, 14275 sata_device_t *sata_device) 14276 { 14277 sata_cport_info_t *cportinfo; 14278 14279 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT || 14280 sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) { 14281 if (SATA_NUM_CPORTS(sata_hba_inst) <= 14282 sata_device->satadev_addr.cport) 14283 return; 14284 14285 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 14286 sata_device->satadev_addr.cport); 14287 14288 ASSERT(mutex_owned(&cportinfo->cport_mutex)); 14289 cportinfo->cport_scr = sata_device->satadev_scr; 14290 14291 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 14292 cportinfo->cport_state &= ~(SATA_PSTATE_PWRON | 14293 SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED); 14294 cportinfo->cport_state |= 14295 sata_device->satadev_state & SATA_PSTATE_VALID; 14296 } 14297 } 14298 14299 void 14300 sata_update_pmport_info(sata_hba_inst_t *sata_hba_inst, 14301 sata_device_t *sata_device) 14302 { 14303 sata_pmport_info_t *pmportinfo; 14304 14305 if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT && 14306 sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) || 14307 SATA_NUM_PMPORTS(sata_hba_inst, 14308 sata_device->satadev_addr.cport) < 14309 sata_device->satadev_addr.pmport) { 14310 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 14311 "sata_update_port_info: error address %p.", 14312 &sata_device->satadev_addr); 14313 return; 14314 } 14315 14316 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 14317 sata_device->satadev_addr.cport, 14318 sata_device->satadev_addr.pmport); 14319 14320 ASSERT(mutex_owned(&pmportinfo->pmport_mutex)); 14321 pmportinfo->pmport_scr = sata_device->satadev_scr; 14322 14323 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 14324 pmportinfo->pmport_state &= 14325 ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED); 14326 pmportinfo->pmport_state |= 14327 sata_device->satadev_state & SATA_PSTATE_VALID; 14328 } 14329 14330 /* 14331 * Extract SATA port specification from an IOCTL argument. 14332 * 14333 * This function return the port the user land send us as is, unless it 14334 * cannot retrieve port spec, then -1 is returned. 14335 * 14336 * Support port multiplier. 14337 */ 14338 static int32_t 14339 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp) 14340 { 14341 int32_t port; 14342 14343 /* Extract port number from nvpair in dca structure */ 14344 if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) { 14345 SATA_LOG_D((sata_hba_inst, CE_NOTE, 14346 "sata_get_port_num: invalid port spec 0x%x in ioctl", 14347 port)); 14348 port = -1; 14349 } 14350 14351 return (port); 14352 } 14353 14354 /* 14355 * Get dev_info_t pointer to the device node pointed to by port argument. 14356 * NOTE: target argument is a value used in ioctls to identify 14357 * the AP - it is not a sata_address. 14358 * It is a combination of cport, pmport and address qualifier, encodded same 14359 * way as a scsi target number. 14360 * At this moment it carries only cport number. 14361 * 14362 * PMult hotplug is supported now. 14363 * 14364 * Returns dev_info_t pointer if target device was found, NULL otherwise. 14365 */ 14366 14367 static dev_info_t * 14368 sata_get_target_dip(dev_info_t *dip, uint8_t cport, uint8_t pmport) 14369 { 14370 dev_info_t *cdip = NULL; 14371 int target, tgt; 14372 int circ; 14373 uint8_t qual; 14374 14375 sata_hba_inst_t *sata_hba_inst; 14376 scsi_hba_tran_t *scsi_hba_tran; 14377 14378 /* Get target id */ 14379 scsi_hba_tran = ddi_get_driver_private(dip); 14380 if (scsi_hba_tran == NULL) 14381 return (NULL); 14382 14383 sata_hba_inst = scsi_hba_tran->tran_hba_private; 14384 14385 if (sata_hba_inst == NULL) 14386 return (NULL); 14387 14388 /* Identify a port-mult by cport_info.cport_dev_type */ 14389 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) 14390 qual = SATA_ADDR_DPMPORT; 14391 else 14392 qual = SATA_ADDR_DCPORT; 14393 14394 target = SATA_TO_SCSI_TARGET(cport, pmport, qual); 14395 14396 /* Retrieve target dip */ 14397 ndi_devi_enter(dip, &circ); 14398 for (cdip = ddi_get_child(dip); cdip != NULL; ) { 14399 dev_info_t *next = ddi_get_next_sibling(cdip); 14400 14401 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip, 14402 DDI_PROP_DONTPASS, "target", -1); 14403 if (tgt == -1) { 14404 /* 14405 * This is actually an error condition, but not 14406 * a fatal one. Just continue the search. 14407 */ 14408 cdip = next; 14409 continue; 14410 } 14411 14412 if (tgt == target) 14413 break; 14414 14415 cdip = next; 14416 } 14417 ndi_devi_exit(dip, circ); 14418 14419 return (cdip); 14420 } 14421 14422 /* 14423 * Get dev_info_t pointer to the device node pointed to by port argument. 14424 * NOTE: target argument is a value used in ioctls to identify 14425 * the AP - it is not a sata_address. 14426 * It is a combination of cport, pmport and address qualifier, encoded same 14427 * way as a scsi target number. 14428 * 14429 * Returns dev_info_t pointer if target device was found, NULL otherwise. 14430 */ 14431 14432 static dev_info_t * 14433 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr) 14434 { 14435 dev_info_t *cdip = NULL; 14436 int target, tgt; 14437 int circ; 14438 14439 target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual); 14440 14441 ndi_devi_enter(dip, &circ); 14442 for (cdip = ddi_get_child(dip); cdip != NULL; ) { 14443 dev_info_t *next = ddi_get_next_sibling(cdip); 14444 14445 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip, 14446 DDI_PROP_DONTPASS, "target", -1); 14447 if (tgt == -1) { 14448 /* 14449 * This is actually an error condition, but not 14450 * a fatal one. Just continue the search. 14451 */ 14452 cdip = next; 14453 continue; 14454 } 14455 14456 if (tgt == target) 14457 break; 14458 14459 cdip = next; 14460 } 14461 ndi_devi_exit(dip, circ); 14462 14463 return (cdip); 14464 } 14465 14466 /* 14467 * Process sata port disconnect request. 14468 * Normally, cfgadm sata plugin will try to offline (unconfigure) the device 14469 * before this request. Nevertheless, if a device is still configured, 14470 * we need to attempt to offline and unconfigure device. 14471 * Regardless of the unconfigure operation results the port is marked as 14472 * deactivated and no access to the attached device is possible. 14473 * If the target node remains because unconfigure operation failed, its state 14474 * will be set to DEVICE_REMOVED, preventing it to be used again when a device 14475 * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure 14476 * the device and remove old target node. 14477 * 14478 * This function invokes sata_hba_inst->satahba_tran-> 14479 * sata_tran_hotplug_ops->sata_tran_port_deactivate(). 14480 * If successful, the device structure (if any) attached to the specified port 14481 * is removed and state of the port marked appropriately. 14482 * Failure of the port_deactivate may keep port in the physically active state, 14483 * or may fail the port. 14484 * 14485 * NOTE: Port multiplier is supported. 14486 */ 14487 14488 static int 14489 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst, 14490 sata_device_t *sata_device) 14491 { 14492 sata_drive_info_t *sdinfo = NULL, *subsdinfo = NULL; 14493 sata_cport_info_t *cportinfo = NULL; 14494 sata_pmport_info_t *pmportinfo = NULL; 14495 sata_pmult_info_t *pmultinfo = NULL; 14496 sata_device_t subsdevice; 14497 int cport, pmport, qual; 14498 int rval = SATA_SUCCESS; 14499 int npmport = 0; 14500 int rv = 0; 14501 14502 cport = sata_device->satadev_addr.cport; 14503 pmport = sata_device->satadev_addr.pmport; 14504 qual = sata_device->satadev_addr.qual; 14505 14506 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 14507 if (qual == SATA_ADDR_DCPORT) 14508 qual = SATA_ADDR_CPORT; 14509 else 14510 qual = SATA_ADDR_PMPORT; 14511 14512 /* 14513 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran-> 14514 * sata_tran_hotplug_ops->sata_tran_port_deactivate(). 14515 * Do the sanity check. 14516 */ 14517 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) { 14518 /* No physical port deactivation supported. */ 14519 return (EINVAL); 14520 } 14521 14522 /* Check the current state of the port */ 14523 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 14524 (SATA_DIP(sata_hba_inst), sata_device); 14525 14526 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 14527 14528 /* 14529 * Processing port mulitiplier 14530 */ 14531 if (qual == SATA_ADDR_CPORT && 14532 SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) { 14533 mutex_enter(&cportinfo->cport_mutex); 14534 14535 /* Check controller port status */ 14536 sata_update_port_info(sata_hba_inst, sata_device); 14537 if (rval != SATA_SUCCESS || 14538 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 14539 /* 14540 * Device port status is unknown or it is in failed 14541 * state 14542 */ 14543 SATA_CPORT_STATE(sata_hba_inst, cport) = 14544 SATA_PSTATE_FAILED; 14545 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 14546 "sata_hba_ioctl: connect: failed to deactivate " 14547 "SATA port %d", cport); 14548 mutex_exit(&cportinfo->cport_mutex); 14549 return (EIO); 14550 } 14551 14552 /* Disconnect all sub-devices. */ 14553 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 14554 if (pmultinfo != NULL) { 14555 14556 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 14557 sata_hba_inst, cport); npmport ++) { 14558 subsdinfo = SATA_PMPORT_DRV_INFO( 14559 sata_hba_inst, cport, npmport); 14560 if (subsdinfo == NULL) 14561 continue; 14562 14563 subsdevice.satadev_addr = subsdinfo-> 14564 satadrv_addr; 14565 14566 mutex_exit(&cportinfo->cport_mutex); 14567 if (sata_ioctl_disconnect(sata_hba_inst, 14568 &subsdevice) == SATA_SUCCESS) { 14569 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 14570 "[Remove] device at port %d:%d " 14571 "successfully.", cport, npmport); 14572 } 14573 mutex_enter(&cportinfo->cport_mutex); 14574 } 14575 } 14576 14577 /* Disconnect the port multiplier */ 14578 cportinfo->cport_state &= ~SATA_STATE_READY; 14579 mutex_exit(&cportinfo->cport_mutex); 14580 14581 sata_device->satadev_addr.qual = qual; 14582 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 14583 (SATA_DIP(sata_hba_inst), sata_device); 14584 14585 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 14586 SE_NO_HINT); 14587 14588 mutex_enter(&cportinfo->cport_mutex); 14589 sata_update_port_info(sata_hba_inst, sata_device); 14590 if (rval != SATA_SUCCESS && 14591 sata_device->satadev_state & SATA_PSTATE_FAILED) { 14592 cportinfo->cport_state = SATA_PSTATE_FAILED; 14593 rv = EIO; 14594 } else { 14595 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 14596 } 14597 mutex_exit(&cportinfo->cport_mutex); 14598 14599 return (rv); 14600 } 14601 14602 /* 14603 * Process non-port-multiplier device - it could be a drive connected 14604 * to a port multiplier port or a controller port. 14605 */ 14606 if (qual == SATA_ADDR_PMPORT) { 14607 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 14608 mutex_enter(&pmportinfo->pmport_mutex); 14609 sata_update_pmport_info(sata_hba_inst, sata_device); 14610 if (rval != SATA_SUCCESS || 14611 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 14612 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 14613 SATA_PSTATE_FAILED; 14614 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 14615 "sata_hba_ioctl: connect: failed to deactivate " 14616 "SATA port %d:%d", cport, pmport); 14617 mutex_exit(&pmportinfo->pmport_mutex); 14618 return (EIO); 14619 } 14620 14621 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 14622 sdinfo = pmportinfo->pmport_sata_drive; 14623 ASSERT(sdinfo != NULL); 14624 } 14625 14626 /* 14627 * Set port's dev_state to not ready - this will disable 14628 * an access to a potentially attached device. 14629 */ 14630 pmportinfo->pmport_state &= ~SATA_STATE_READY; 14631 14632 /* Remove and release sata_drive info structure. */ 14633 if (sdinfo != NULL) { 14634 if ((sdinfo->satadrv_type & 14635 SATA_VALID_DEV_TYPE) != 0) { 14636 /* 14637 * If a target node exists, try to offline 14638 * a device and remove target node. 14639 */ 14640 mutex_exit(&pmportinfo->pmport_mutex); 14641 (void) sata_offline_device(sata_hba_inst, 14642 sata_device, sdinfo); 14643 mutex_enter(&pmportinfo->pmport_mutex); 14644 } 14645 14646 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 14647 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 14648 (void) kmem_free((void *)sdinfo, 14649 sizeof (sata_drive_info_t)); 14650 } 14651 mutex_exit(&pmportinfo->pmport_mutex); 14652 14653 } else if (qual == SATA_ADDR_CPORT) { 14654 mutex_enter(&cportinfo->cport_mutex); 14655 sata_update_port_info(sata_hba_inst, sata_device); 14656 if (rval != SATA_SUCCESS || 14657 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 14658 /* 14659 * Device port status is unknown or it is in failed 14660 * state 14661 */ 14662 SATA_CPORT_STATE(sata_hba_inst, cport) = 14663 SATA_PSTATE_FAILED; 14664 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 14665 "sata_hba_ioctl: connect: failed to deactivate " 14666 "SATA port %d", cport); 14667 mutex_exit(&cportinfo->cport_mutex); 14668 return (EIO); 14669 } 14670 14671 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 14672 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 14673 ASSERT(pmultinfo != NULL); 14674 } else if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 14675 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 14676 ASSERT(sdinfo != NULL); 14677 } 14678 cportinfo->cport_state &= ~SATA_STATE_READY; 14679 14680 if (sdinfo != NULL) { 14681 if ((sdinfo->satadrv_type & 14682 SATA_VALID_DEV_TYPE) != 0) { 14683 /* 14684 * If a target node exists, try to offline 14685 * a device and remove target node. 14686 */ 14687 mutex_exit(&cportinfo->cport_mutex); 14688 (void) sata_offline_device(sata_hba_inst, 14689 sata_device, sdinfo); 14690 mutex_enter(&cportinfo->cport_mutex); 14691 } 14692 14693 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 14694 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 14695 (void) kmem_free((void *)sdinfo, 14696 sizeof (sata_drive_info_t)); 14697 } 14698 mutex_exit(&cportinfo->cport_mutex); 14699 } 14700 14701 /* Just ask HBA driver to deactivate port */ 14702 sata_device->satadev_addr.qual = qual; 14703 14704 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 14705 (SATA_DIP(sata_hba_inst), sata_device); 14706 14707 /* 14708 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 14709 * without the hint (to force listener to investivate the state). 14710 */ 14711 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 14712 SE_NO_HINT); 14713 14714 if (qual == SATA_ADDR_PMPORT) { 14715 mutex_enter(&pmportinfo->pmport_mutex); 14716 sata_update_pmport_info(sata_hba_inst, sata_device); 14717 14718 if (rval != SATA_SUCCESS && 14719 sata_device->satadev_state & SATA_PSTATE_FAILED) { 14720 /* 14721 * Port deactivation failure - do not change port 14722 * state unless the state returned by HBA indicates a 14723 * port failure. 14724 * 14725 * NOTE: device structures were released, so devices 14726 * now are invisible! Port reset is needed to 14727 * re-enumerate devices. 14728 */ 14729 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 14730 rv = EIO; 14731 } else { 14732 /* 14733 * Deactivation succeded. From now on the sata framework 14734 * will not care what is happening to the device, until 14735 * the port is activated again. 14736 */ 14737 pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN; 14738 } 14739 mutex_exit(&pmportinfo->pmport_mutex); 14740 } else if (qual == SATA_ADDR_CPORT) { 14741 mutex_enter(&cportinfo->cport_mutex); 14742 sata_update_port_info(sata_hba_inst, sata_device); 14743 14744 if (rval != SATA_SUCCESS && 14745 sata_device->satadev_state & SATA_PSTATE_FAILED) { 14746 cportinfo->cport_state = SATA_PSTATE_FAILED; 14747 rv = EIO; 14748 } else { 14749 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 14750 } 14751 mutex_exit(&cportinfo->cport_mutex); 14752 } 14753 14754 return (rv); 14755 } 14756 14757 14758 14759 /* 14760 * Process sata port connect request 14761 * The sata cfgadm pluging will invoke this operation only if port was found 14762 * in the disconnect state (failed state is also treated as the disconnected 14763 * state). 14764 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->satahba_tran-> 14765 * sata_tran_hotplug_ops->sata_tran_port_activate(). 14766 * If successful and a device is found attached to the port, 14767 * the initialization sequence is executed to attach a device structure to 14768 * a port structure. The state of the port and a device would be set 14769 * appropriately. 14770 * The device is not set in configured state (system-wise) by this operation. 14771 * 14772 * Note, that activating the port may generate link events, 14773 * so it is important that following processing and the 14774 * event processing does not interfere with each other! 14775 * 14776 * This operation may remove port failed state and will 14777 * try to make port active and in good standing. 14778 * 14779 * NOTE: Port multiplier is supported. 14780 */ 14781 14782 static int 14783 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst, 14784 sata_device_t *sata_device) 14785 { 14786 sata_pmport_info_t *pmportinfo = NULL; 14787 uint8_t cport, pmport, qual; 14788 int rv = 0; 14789 14790 cport = sata_device->satadev_addr.cport; 14791 pmport = sata_device->satadev_addr.pmport; 14792 qual = sata_device->satadev_addr.qual; 14793 14794 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 14795 if (qual == SATA_ADDR_DCPORT) 14796 qual = SATA_ADDR_CPORT; 14797 else 14798 qual = SATA_ADDR_PMPORT; 14799 14800 if (qual == SATA_ADDR_PMPORT) 14801 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 14802 14803 /* 14804 * DEVCTL_AP_CONNECT would invoke sata_hba_inst-> 14805 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate(). 14806 * Perform sanity check now. 14807 */ 14808 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) { 14809 /* No physical port activation supported. */ 14810 return (EINVAL); 14811 } 14812 14813 /* Just ask HBA driver to activate port */ 14814 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 14815 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 14816 /* 14817 * Port activation failure. 14818 */ 14819 if (qual == SATA_ADDR_CPORT) { 14820 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14821 cport)->cport_mutex); 14822 sata_update_port_info(sata_hba_inst, sata_device); 14823 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 14824 SATA_CPORT_STATE(sata_hba_inst, cport) = 14825 SATA_PSTATE_FAILED; 14826 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 14827 "sata_hba_ioctl: connect: failed to " 14828 "activate SATA port %d", cport); 14829 } 14830 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14831 cport)->cport_mutex); 14832 } else { /* port multiplier device port */ 14833 mutex_enter(&pmportinfo->pmport_mutex); 14834 sata_update_pmport_info(sata_hba_inst, sata_device); 14835 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 14836 SATA_PMPORT_STATE(sata_hba_inst, cport, 14837 pmport) = SATA_PSTATE_FAILED; 14838 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 14839 "sata_hba_ioctl: connect: failed to " 14840 "activate SATA port %d:%d", cport, pmport); 14841 } 14842 mutex_exit(&pmportinfo->pmport_mutex); 14843 } 14844 return (EIO); 14845 } 14846 14847 /* Virgin port state - will be updated by the port re-probe. */ 14848 if (qual == SATA_ADDR_CPORT) { 14849 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14850 cport)->cport_mutex); 14851 SATA_CPORT_STATE(sata_hba_inst, cport) = 0; 14852 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14853 cport)->cport_mutex); 14854 } else { /* port multiplier device port */ 14855 mutex_enter(&pmportinfo->pmport_mutex); 14856 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0; 14857 mutex_exit(&pmportinfo->pmport_mutex); 14858 } 14859 14860 /* 14861 * Probe the port to find its state and attached device. 14862 */ 14863 if (sata_reprobe_port(sata_hba_inst, sata_device, 14864 SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE) 14865 rv = EIO; 14866 14867 /* 14868 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 14869 * without the hint 14870 */ 14871 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 14872 SE_NO_HINT); 14873 14874 /* 14875 * If there is a device attached to the port, emit 14876 * a message. 14877 */ 14878 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 14879 14880 if (qual == SATA_ADDR_CPORT) { 14881 if (sata_device->satadev_type == SATA_DTYPE_PMULT) { 14882 sata_log(sata_hba_inst, CE_WARN, 14883 "SATA port multiplier detected " 14884 "at port %d", cport); 14885 } else { 14886 sata_log(sata_hba_inst, CE_WARN, 14887 "SATA device detected at port %d", cport); 14888 if (sata_device->satadev_type == 14889 SATA_DTYPE_UNKNOWN) { 14890 /* 14891 * A device was not successfully identified 14892 */ 14893 sata_log(sata_hba_inst, CE_WARN, 14894 "Could not identify SATA " 14895 "device at port %d", cport); 14896 } 14897 } 14898 } else { /* port multiplier device port */ 14899 sata_log(sata_hba_inst, CE_WARN, 14900 "SATA device detected at port %d:%d", 14901 cport, pmport); 14902 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 14903 /* 14904 * A device was not successfully identified 14905 */ 14906 sata_log(sata_hba_inst, CE_WARN, 14907 "Could not identify SATA " 14908 "device at port %d:%d", cport, pmport); 14909 } 14910 } 14911 } 14912 14913 return (rv); 14914 } 14915 14916 14917 /* 14918 * Process sata device unconfigure request. 14919 * The unconfigure operation uses generic nexus operation to 14920 * offline a device. It leaves a target device node attached. 14921 * and obviously sata_drive_info attached as well, because 14922 * from the hardware point of view nothing has changed. 14923 */ 14924 static int 14925 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst, 14926 sata_device_t *sata_device) 14927 { 14928 int rv = 0; 14929 dev_info_t *tdip; 14930 14931 /* We are addressing attached device, not a port */ 14932 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 14933 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 14934 else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) 14935 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 14936 14937 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 14938 &sata_device->satadev_addr)) != NULL) { 14939 14940 if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) { 14941 SATA_LOG_D((sata_hba_inst, CE_WARN, 14942 "sata_hba_ioctl: unconfigure: " 14943 "failed to unconfigure device at SATA port %d:%d", 14944 sata_device->satadev_addr.cport, 14945 sata_device->satadev_addr.pmport)); 14946 rv = EIO; 14947 } 14948 /* 14949 * The target node devi_state should be marked with 14950 * DEVI_DEVICE_OFFLINE by ndi_devi_offline(). 14951 * This would be the indication for cfgadm that 14952 * the AP node occupant state is 'unconfigured'. 14953 */ 14954 14955 } else { 14956 /* 14957 * This would indicate a failure on the part of cfgadm 14958 * to detect correct state of the node prior to this 14959 * call - one cannot unconfigure non-existing device. 14960 */ 14961 SATA_LOG_D((sata_hba_inst, CE_WARN, 14962 "sata_hba_ioctl: unconfigure: " 14963 "attempt to unconfigure non-existing device " 14964 "at SATA port %d:%d", 14965 sata_device->satadev_addr.cport, 14966 sata_device->satadev_addr.pmport)); 14967 rv = ENXIO; 14968 } 14969 return (rv); 14970 } 14971 14972 /* 14973 * Process sata device configure request 14974 * If port is in a failed state, operation is aborted - one has to use 14975 * an explicit connect or port activate request to try to get a port into 14976 * non-failed mode. Port reset wil also work in such situation. 14977 * If the port is in disconnected (shutdown) state, the connect operation is 14978 * attempted prior to any other action. 14979 * When port is in the active state, there is a device attached and the target 14980 * node exists, a device was most likely offlined. 14981 * If target node does not exist, a new target node is created. In both cases 14982 * an attempt is made to online (configure) the device. 14983 * 14984 * NOTE: Port multiplier is supported. 14985 */ 14986 static int 14987 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst, 14988 sata_device_t *sata_device) 14989 { 14990 int cport, pmport, qual; 14991 int rval; 14992 boolean_t target = B_TRUE; 14993 sata_cport_info_t *cportinfo; 14994 sata_pmport_info_t *pmportinfo = NULL; 14995 dev_info_t *tdip; 14996 sata_drive_info_t *sdinfo; 14997 14998 cport = sata_device->satadev_addr.cport; 14999 pmport = sata_device->satadev_addr.pmport; 15000 qual = sata_device->satadev_addr.qual; 15001 15002 /* Get current port state */ 15003 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 15004 (SATA_DIP(sata_hba_inst), sata_device); 15005 15006 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 15007 if (qual == SATA_ADDR_DPMPORT) { 15008 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 15009 mutex_enter(&pmportinfo->pmport_mutex); 15010 sata_update_pmport_info(sata_hba_inst, sata_device); 15011 if (rval != SATA_SUCCESS || 15012 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 15013 /* 15014 * Obviously, device on a failed port is not visible 15015 */ 15016 mutex_exit(&pmportinfo->pmport_mutex); 15017 return (ENXIO); 15018 } 15019 mutex_exit(&pmportinfo->pmport_mutex); 15020 } else { 15021 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15022 cport)->cport_mutex); 15023 sata_update_port_info(sata_hba_inst, sata_device); 15024 if (rval != SATA_SUCCESS || 15025 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 15026 /* 15027 * Obviously, device on a failed port is not visible 15028 */ 15029 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15030 cport)->cport_mutex); 15031 return (ENXIO); 15032 } 15033 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15034 cport)->cport_mutex); 15035 } 15036 15037 if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) { 15038 /* need to activate port */ 15039 target = B_FALSE; 15040 15041 /* Sanity check */ 15042 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) 15043 return (ENXIO); 15044 15045 /* Just let HBA driver to activate port */ 15046 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 15047 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 15048 /* 15049 * Port activation failure - do not change port state 15050 * unless the state returned by HBA indicates a port 15051 * failure. 15052 */ 15053 if (qual == SATA_ADDR_DPMPORT) { 15054 mutex_enter(&pmportinfo->pmport_mutex); 15055 sata_update_pmport_info(sata_hba_inst, 15056 sata_device); 15057 if (sata_device->satadev_state & 15058 SATA_PSTATE_FAILED) 15059 pmportinfo->pmport_state = 15060 SATA_PSTATE_FAILED; 15061 mutex_exit(&pmportinfo->pmport_mutex); 15062 } else { 15063 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15064 cport)->cport_mutex); 15065 sata_update_port_info(sata_hba_inst, 15066 sata_device); 15067 if (sata_device->satadev_state & 15068 SATA_PSTATE_FAILED) 15069 cportinfo->cport_state = 15070 SATA_PSTATE_FAILED; 15071 mutex_exit(&SATA_CPORT_INFO( 15072 sata_hba_inst, cport)->cport_mutex); 15073 } 15074 } 15075 SATA_LOG_D((sata_hba_inst, CE_WARN, 15076 "sata_hba_ioctl: configure: " 15077 "failed to activate SATA port %d:%d", 15078 cport, pmport)); 15079 return (EIO); 15080 } 15081 /* 15082 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 15083 * without the hint. 15084 */ 15085 sata_gen_sysevent(sata_hba_inst, 15086 &sata_device->satadev_addr, SE_NO_HINT); 15087 15088 /* Virgin port state */ 15089 if (qual == SATA_ADDR_DPMPORT) { 15090 mutex_enter(&pmportinfo->pmport_mutex); 15091 pmportinfo->pmport_state = 0; 15092 mutex_exit(&pmportinfo->pmport_mutex); 15093 } else { 15094 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15095 cport)-> cport_mutex); 15096 cportinfo->cport_state = 0; 15097 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15098 cport)->cport_mutex); 15099 } 15100 /* 15101 * Always reprobe port, to get current device info. 15102 */ 15103 if (sata_reprobe_port(sata_hba_inst, sata_device, 15104 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 15105 return (EIO); 15106 15107 if (sata_device->satadev_type != SATA_DTYPE_NONE && target == B_FALSE) { 15108 if (qual == SATA_ADDR_DPMPORT) { 15109 /* 15110 * That's the transition from "inactive" port 15111 * to active one with device attached. 15112 */ 15113 sata_log(sata_hba_inst, CE_WARN, 15114 "SATA device detected at port %d:%d", 15115 cport, pmport); 15116 } else { 15117 /* 15118 * When PM is attached to the cport and cport is 15119 * activated, every PM device port needs to be reprobed. 15120 * We need to emit message for all devices detected 15121 * at port multiplier's device ports. 15122 * Add such code here. 15123 * For now, just inform about device attached to 15124 * cport. 15125 */ 15126 sata_log(sata_hba_inst, CE_WARN, 15127 "SATA device detected at port %d", cport); 15128 } 15129 } 15130 15131 /* 15132 * This is where real configuration operation starts. 15133 * 15134 * When PM is attached to the cport and cport is activated, 15135 * devices attached PM device ports may have to be configured 15136 * explicitly. This may change when port multiplier is supported. 15137 * For now, configure only disks and other valid target devices. 15138 */ 15139 if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) { 15140 if (qual == SATA_ADDR_DCPORT) { 15141 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 15142 /* 15143 * A device was not successfully identified 15144 */ 15145 sata_log(sata_hba_inst, CE_WARN, 15146 "Could not identify SATA " 15147 "device at port %d", cport); 15148 } 15149 } else { /* port multiplier device port */ 15150 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 15151 /* 15152 * A device was not successfully identified 15153 */ 15154 sata_log(sata_hba_inst, CE_WARN, 15155 "Could not identify SATA " 15156 "device at port %d:%d", cport, pmport); 15157 } 15158 } 15159 return (ENXIO); /* No device to configure */ 15160 } 15161 15162 /* 15163 * Here we may have a device in reset condition, 15164 * but because we are just configuring it, there is 15165 * no need to process the reset other than just 15166 * to clear device reset condition in the HBA driver. 15167 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will 15168 * cause a first command sent the HBA driver with the request 15169 * to clear device reset condition. 15170 */ 15171 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15172 if (qual == SATA_ADDR_DPMPORT) 15173 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 15174 else 15175 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 15176 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 15177 if (sdinfo == NULL) { 15178 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15179 return (ENXIO); 15180 } 15181 if (sdinfo->satadrv_event_flags & 15182 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 15183 sdinfo->satadrv_event_flags = 0; 15184 } 15185 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 15186 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15187 15188 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 15189 &sata_device->satadev_addr)) != NULL) { 15190 /* 15191 * Target node exists. Verify, that it belongs 15192 * to existing, attached device and not to 15193 * a removed device. 15194 */ 15195 if (sata_check_device_removed(tdip) == B_TRUE) { 15196 if (qual == SATA_ADDR_DPMPORT) 15197 sata_log(sata_hba_inst, CE_WARN, 15198 "SATA device at port %d cannot be " 15199 "configured. " 15200 "Application(s) accessing " 15201 "previously attached device " 15202 "have to release it before newly " 15203 "inserted device can be made accessible.", 15204 cport); 15205 else 15206 sata_log(sata_hba_inst, CE_WARN, 15207 "SATA device at port %d:%d cannot be" 15208 "configured. " 15209 "Application(s) accessing " 15210 "previously attached device " 15211 "have to release it before newly " 15212 "inserted device can be made accessible.", 15213 cport, pmport); 15214 return (EIO); 15215 } 15216 /* 15217 * Device was not removed and re-inserted. 15218 * Try to online it. 15219 */ 15220 if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) { 15221 SATA_LOG_D((sata_hba_inst, CE_WARN, 15222 "sata_hba_ioctl: configure: " 15223 "onlining device at SATA port " 15224 "%d:%d failed", cport, pmport)); 15225 return (EIO); 15226 } 15227 15228 if (qual == SATA_ADDR_DPMPORT) { 15229 mutex_enter(&pmportinfo->pmport_mutex); 15230 pmportinfo->pmport_tgtnode_clean = B_TRUE; 15231 mutex_exit(&pmportinfo->pmport_mutex); 15232 } else { 15233 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15234 cport)->cport_mutex); 15235 cportinfo-> cport_tgtnode_clean = B_TRUE; 15236 mutex_exit(&SATA_CPORT_INFO( 15237 sata_hba_inst, cport)->cport_mutex); 15238 } 15239 } else { 15240 /* 15241 * No target node - need to create a new target node. 15242 */ 15243 if (qual == SATA_ADDR_DPMPORT) { 15244 mutex_enter(&pmportinfo->pmport_mutex); 15245 pmportinfo->pmport_tgtnode_clean = B_TRUE; 15246 mutex_exit(&pmportinfo->pmport_mutex); 15247 } else { 15248 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15249 cport_mutex); 15250 cportinfo-> cport_tgtnode_clean = B_TRUE; 15251 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15252 cport_mutex); 15253 } 15254 15255 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst), 15256 sata_hba_inst, &sata_device->satadev_addr); 15257 if (tdip == NULL) { 15258 /* Configure operation failed */ 15259 SATA_LOG_D((sata_hba_inst, CE_WARN, 15260 "sata_hba_ioctl: configure: " 15261 "configuring SATA device at port %d:%d " 15262 "failed", cport, pmport)); 15263 return (EIO); 15264 } 15265 } 15266 return (0); 15267 } 15268 15269 15270 /* 15271 * Process ioctl deactivate port request. 15272 * Arbitrarily unconfigure attached device, if any. 15273 * Even if the unconfigure fails, proceed with the 15274 * port deactivation. 15275 * 15276 * NOTE: Port Multiplier is supported now. 15277 */ 15278 15279 static int 15280 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst, 15281 sata_device_t *sata_device) 15282 { 15283 int cport, pmport, qual; 15284 int rval, rv = 0; 15285 int npmport; 15286 sata_cport_info_t *cportinfo; 15287 sata_pmport_info_t *pmportinfo; 15288 sata_pmult_info_t *pmultinfo; 15289 dev_info_t *tdip; 15290 sata_drive_info_t *sdinfo = NULL; 15291 sata_device_t subsdevice; 15292 15293 /* Sanity check */ 15294 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) 15295 return (ENOTSUP); 15296 15297 cport = sata_device->satadev_addr.cport; 15298 pmport = sata_device->satadev_addr.pmport; 15299 qual = sata_device->satadev_addr.qual; 15300 15301 /* SCSI_TO_SATA_ADDR_QUAL() translate ap_id into a device qualifier */ 15302 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 15303 if (qual == SATA_ADDR_DCPORT) 15304 qual = SATA_ADDR_CPORT; 15305 else 15306 qual = SATA_ADDR_PMPORT; 15307 15308 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 15309 if (qual == SATA_ADDR_PMPORT) 15310 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 15311 15312 /* 15313 * Processing port multiplier 15314 */ 15315 if (qual == SATA_ADDR_CPORT && 15316 SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) { 15317 mutex_enter(&cportinfo->cport_mutex); 15318 15319 /* Deactivate all sub-deices */ 15320 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 15321 if (pmultinfo != NULL) { 15322 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 15323 sata_hba_inst, cport); npmport++) { 15324 15325 subsdevice.satadev_addr.cport = cport; 15326 subsdevice.satadev_addr.pmport = 15327 (uint8_t)npmport; 15328 subsdevice.satadev_addr.qual = 15329 SATA_ADDR_DPMPORT; 15330 15331 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 15332 "sata_hba_ioctl: deactivate: trying to " 15333 "deactivate SATA port %d:%d", 15334 cport, npmport); 15335 15336 mutex_exit(&cportinfo->cport_mutex); 15337 if (sata_ioctl_deactivate(sata_hba_inst, 15338 &subsdevice) == SATA_SUCCESS) { 15339 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 15340 "[Deactivate] device at port %d:%d " 15341 "successfully.", cport, npmport); 15342 } 15343 mutex_enter(&cportinfo->cport_mutex); 15344 } 15345 } 15346 15347 /* Deactivate the port multiplier now. */ 15348 cportinfo->cport_state &= ~SATA_STATE_READY; 15349 mutex_exit(&cportinfo->cport_mutex); 15350 15351 sata_device->satadev_addr.qual = qual; 15352 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 15353 (SATA_DIP(sata_hba_inst), sata_device); 15354 15355 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 15356 SE_NO_HINT); 15357 15358 mutex_enter(&cportinfo->cport_mutex); 15359 sata_update_port_info(sata_hba_inst, sata_device); 15360 if (rval != SATA_SUCCESS) { 15361 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 15362 cportinfo->cport_state = SATA_PSTATE_FAILED; 15363 } 15364 rv = EIO; 15365 } else { 15366 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 15367 } 15368 mutex_exit(&cportinfo->cport_mutex); 15369 15370 return (rv); 15371 } 15372 15373 /* 15374 * Process non-port-multiplier device - it could be a drive connected 15375 * to a port multiplier port or a controller port. 15376 */ 15377 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15378 if (qual == SATA_ADDR_CPORT) { 15379 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 15380 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 15381 /* deal only with valid devices */ 15382 if ((cportinfo->cport_dev_type & 15383 SATA_VALID_DEV_TYPE) != 0) 15384 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 15385 } 15386 cportinfo->cport_state &= ~SATA_STATE_READY; 15387 } else { 15388 /* Port multiplier device port */ 15389 mutex_enter(&pmportinfo->pmport_mutex); 15390 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 15391 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE && 15392 (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0) 15393 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 15394 pmportinfo->pmport_state &= ~SATA_STATE_READY; 15395 mutex_exit(&pmportinfo->pmport_mutex); 15396 } 15397 15398 if (sdinfo != NULL) { 15399 /* 15400 * If a target node exists, try to offline a device and 15401 * to remove a target node. 15402 */ 15403 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15404 cport_mutex); 15405 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 15406 &sata_device->satadev_addr); 15407 if (tdip != NULL) { 15408 /* target node exist */ 15409 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 15410 "sata_hba_ioctl: port deactivate: " 15411 "target node exists.", NULL); 15412 15413 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != 15414 NDI_SUCCESS) { 15415 SATA_LOG_D((sata_hba_inst, CE_WARN, 15416 "sata_hba_ioctl: port deactivate: " 15417 "failed to unconfigure device at port " 15418 "%d:%d before deactivating the port", 15419 cport, pmport)); 15420 /* 15421 * Set DEVICE REMOVED state in the target 15422 * node. It will prevent an access to 15423 * the device even when a new device is 15424 * attached, until the old target node is 15425 * released, removed and recreated for a new 15426 * device. 15427 */ 15428 sata_set_device_removed(tdip); 15429 15430 /* 15431 * Instruct the event daemon to try the 15432 * target node cleanup later. 15433 */ 15434 sata_set_target_node_cleanup(sata_hba_inst, 15435 &sata_device->satadev_addr); 15436 } 15437 } 15438 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15439 cport_mutex); 15440 /* 15441 * In any case, remove and release sata_drive_info 15442 * structure. 15443 */ 15444 if (qual == SATA_ADDR_CPORT) { 15445 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 15446 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 15447 } else { /* port multiplier device port */ 15448 mutex_enter(&pmportinfo->pmport_mutex); 15449 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 15450 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 15451 mutex_exit(&pmportinfo->pmport_mutex); 15452 } 15453 (void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t)); 15454 } 15455 15456 if (qual == SATA_ADDR_CPORT) { 15457 cportinfo->cport_state &= ~(SATA_STATE_PROBED | 15458 SATA_STATE_PROBING); 15459 } else if (qual == SATA_ADDR_PMPORT) { 15460 mutex_enter(&pmportinfo->pmport_mutex); 15461 pmportinfo->pmport_state &= ~(SATA_STATE_PROBED | 15462 SATA_STATE_PROBING); 15463 mutex_exit(&pmportinfo->pmport_mutex); 15464 } 15465 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15466 15467 /* Just let HBA driver to deactivate port */ 15468 sata_device->satadev_addr.qual = qual; 15469 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 15470 (SATA_DIP(sata_hba_inst), sata_device); 15471 15472 /* 15473 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 15474 * without the hint 15475 */ 15476 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 15477 SE_NO_HINT); 15478 15479 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15480 sata_update_port_info(sata_hba_inst, sata_device); 15481 if (qual == SATA_ADDR_CPORT) { 15482 if (rval != SATA_SUCCESS) { 15483 /* 15484 * Port deactivation failure - do not change port state 15485 * unless the state returned by HBA indicates a port 15486 * failure. 15487 */ 15488 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 15489 SATA_CPORT_STATE(sata_hba_inst, cport) = 15490 SATA_PSTATE_FAILED; 15491 } 15492 SATA_LOG_D((sata_hba_inst, CE_WARN, 15493 "sata_hba_ioctl: port deactivate: " 15494 "cannot deactivate SATA port %d", cport)); 15495 rv = EIO; 15496 } else { 15497 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 15498 } 15499 } else { 15500 mutex_enter(&pmportinfo->pmport_mutex); 15501 if (rval != SATA_SUCCESS) { 15502 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 15503 SATA_PMPORT_STATE(sata_hba_inst, cport, 15504 pmport) = SATA_PSTATE_FAILED; 15505 } 15506 SATA_LOG_D((sata_hba_inst, CE_WARN, 15507 "sata_hba_ioctl: port deactivate: " 15508 "cannot deactivate SATA port %d:%d", 15509 cport, pmport)); 15510 rv = EIO; 15511 } else { 15512 pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN; 15513 } 15514 mutex_exit(&pmportinfo->pmport_mutex); 15515 } 15516 15517 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15518 15519 return (rv); 15520 } 15521 15522 /* 15523 * Process ioctl port activate request. 15524 * 15525 * NOTE: Port multiplier is supported now. 15526 */ 15527 static int 15528 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst, 15529 sata_device_t *sata_device) 15530 { 15531 int cport, pmport, qual; 15532 sata_cport_info_t *cportinfo; 15533 sata_pmport_info_t *pmportinfo = NULL; 15534 boolean_t dev_existed = B_TRUE; 15535 15536 /* Sanity check */ 15537 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) 15538 return (ENOTSUP); 15539 15540 cport = sata_device->satadev_addr.cport; 15541 pmport = sata_device->satadev_addr.pmport; 15542 qual = sata_device->satadev_addr.qual; 15543 15544 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 15545 15546 /* 15547 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL()) 15548 * is a device. But what we are dealing with is port/pmport. 15549 */ 15550 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 15551 if (qual == SATA_ADDR_DCPORT) 15552 sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT; 15553 else 15554 sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT; 15555 15556 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15557 if (qual == SATA_ADDR_PMPORT) { 15558 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 15559 if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN || 15560 pmportinfo->pmport_dev_type == SATA_DTYPE_NONE) 15561 dev_existed = B_FALSE; 15562 } else { /* cport */ 15563 if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN || 15564 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 15565 dev_existed = B_FALSE; 15566 } 15567 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15568 15569 /* Just let HBA driver to activate port, if necessary */ 15570 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 15571 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 15572 /* 15573 * Port activation failure - do not change port state unless 15574 * the state returned by HBA indicates a port failure. 15575 */ 15576 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15577 cport)->cport_mutex); 15578 sata_update_port_info(sata_hba_inst, sata_device); 15579 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 15580 if (qual == SATA_ADDR_PMPORT) { 15581 mutex_enter(&pmportinfo->pmport_mutex); 15582 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 15583 mutex_exit(&pmportinfo->pmport_mutex); 15584 } else 15585 cportinfo->cport_state = SATA_PSTATE_FAILED; 15586 15587 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15588 cport)->cport_mutex); 15589 SATA_LOG_D((sata_hba_inst, CE_WARN, 15590 "sata_hba_ioctl: port activate: cannot activate " 15591 "SATA port %d:%d", cport, pmport)); 15592 return (EIO); 15593 } 15594 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15595 } 15596 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15597 if (qual == SATA_ADDR_PMPORT) { 15598 mutex_enter(&pmportinfo->pmport_mutex); 15599 pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN; 15600 mutex_exit(&pmportinfo->pmport_mutex); 15601 } else 15602 cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN; 15603 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15604 15605 /* 15606 * Re-probe port to find its current state and possibly attached device. 15607 * Port re-probing may change the cportinfo device type if device is 15608 * found attached. 15609 * If port probing failed, the device type would be set to 15610 * SATA_DTYPE_NONE. 15611 */ 15612 (void) sata_reprobe_port(sata_hba_inst, sata_device, 15613 SATA_DEV_IDENTIFY_RETRY); 15614 15615 /* 15616 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 15617 * without the hint. 15618 */ 15619 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 15620 SE_NO_HINT); 15621 15622 if (dev_existed == B_FALSE) { 15623 if (qual == SATA_ADDR_PMPORT && 15624 pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 15625 /* 15626 * That's the transition from the "inactive" port state 15627 * or the active port without a device attached to the 15628 * active port state with a device attached. 15629 */ 15630 sata_log(sata_hba_inst, CE_WARN, 15631 "SATA device detected at port %d:%d", 15632 cport, pmport); 15633 } else if (qual == SATA_ADDR_CPORT && 15634 cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 15635 /* 15636 * That's the transition from the "inactive" port state 15637 * or the active port without a device attached to the 15638 * active port state with a device attached. 15639 */ 15640 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 15641 sata_log(sata_hba_inst, CE_WARN, 15642 "SATA device detected at port %d", cport); 15643 } else { 15644 sata_log(sata_hba_inst, CE_WARN, 15645 "SATA port multiplier detected at port %d", 15646 cport); 15647 } 15648 } 15649 } 15650 return (0); 15651 } 15652 15653 15654 15655 /* 15656 * Process ioctl reset port request. 15657 * 15658 * NOTE: Port-Multiplier is supported. 15659 */ 15660 static int 15661 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst, 15662 sata_device_t *sata_device) 15663 { 15664 int cport, pmport, qual; 15665 int rv = 0; 15666 15667 cport = sata_device->satadev_addr.cport; 15668 pmport = sata_device->satadev_addr.pmport; 15669 qual = sata_device->satadev_addr.qual; 15670 15671 /* 15672 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL()) 15673 * is a device. But what we are dealing with is port/pmport. 15674 */ 15675 if (qual == SATA_ADDR_DCPORT) 15676 sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT; 15677 else 15678 sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT; 15679 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT); 15680 15681 /* Sanity check */ 15682 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 15683 SATA_LOG_D((sata_hba_inst, CE_WARN, 15684 "sata_hba_ioctl: sata_hba_tran missing required " 15685 "function sata_tran_reset_dport")); 15686 return (ENOTSUP); 15687 } 15688 15689 /* Ask HBA to reset port */ 15690 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 15691 sata_device) != SATA_SUCCESS) { 15692 SATA_LOG_D((sata_hba_inst, CE_WARN, 15693 "sata_hba_ioctl: reset port: failed %d:%d", 15694 cport, pmport)); 15695 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15696 cport_mutex); 15697 sata_update_port_info(sata_hba_inst, sata_device); 15698 if (qual == SATA_ADDR_CPORT) 15699 SATA_CPORT_STATE(sata_hba_inst, cport) = 15700 SATA_PSTATE_FAILED; 15701 else { 15702 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, 15703 pmport)); 15704 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 15705 SATA_PSTATE_FAILED; 15706 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, 15707 pmport)); 15708 } 15709 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15710 cport_mutex); 15711 rv = EIO; 15712 } 15713 15714 return (rv); 15715 } 15716 15717 /* 15718 * Process ioctl reset device request. 15719 * 15720 * NOTE: Port multiplier is supported. 15721 */ 15722 static int 15723 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst, 15724 sata_device_t *sata_device) 15725 { 15726 sata_drive_info_t *sdinfo = NULL; 15727 sata_pmult_info_t *pmultinfo = NULL; 15728 int cport, pmport; 15729 int rv = 0; 15730 15731 /* Sanity check */ 15732 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 15733 SATA_LOG_D((sata_hba_inst, CE_WARN, 15734 "sata_hba_ioctl: sata_hba_tran missing required " 15735 "function sata_tran_reset_dport")); 15736 return (ENOTSUP); 15737 } 15738 15739 cport = sata_device->satadev_addr.cport; 15740 pmport = sata_device->satadev_addr.pmport; 15741 15742 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15743 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) { 15744 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == 15745 SATA_DTYPE_PMULT) 15746 pmultinfo = SATA_CPORT_INFO(sata_hba_inst, cport)-> 15747 cport_devp.cport_sata_pmult; 15748 else 15749 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 15750 sata_device->satadev_addr.cport); 15751 } else { /* port multiplier */ 15752 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 15753 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 15754 sata_device->satadev_addr.cport, 15755 sata_device->satadev_addr.pmport); 15756 } 15757 if (sdinfo == NULL && pmultinfo == NULL) { 15758 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15759 return (EINVAL); 15760 } 15761 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15762 15763 /* Ask HBA to reset device */ 15764 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 15765 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 15766 SATA_LOG_D((sata_hba_inst, CE_WARN, 15767 "sata_hba_ioctl: reset device: failed at port %d:%d", 15768 cport, pmport)); 15769 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15770 cport_mutex); 15771 sata_update_port_info(sata_hba_inst, sata_device); 15772 /* 15773 * Device info structure remains attached. Another device reset 15774 * or port disconnect/connect and re-probing is 15775 * needed to change it's state 15776 */ 15777 if (sdinfo != NULL) { 15778 sdinfo->satadrv_state &= ~SATA_STATE_READY; 15779 sdinfo->satadrv_state |= SATA_DSTATE_FAILED; 15780 } else if (pmultinfo != NULL) { 15781 pmultinfo->pmult_state &= ~SATA_STATE_READY; 15782 pmultinfo->pmult_state |= SATA_DSTATE_FAILED; 15783 } 15784 15785 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15786 rv = EIO; 15787 } 15788 /* 15789 * If attached device was a port multiplier, some extra processing 15790 * may be needed to bring it back. SATA specification requies a 15791 * mandatory software reset on host port to reliably enumerate a port 15792 * multiplier, the HBA driver should handle that after reset 15793 * operation. 15794 */ 15795 return (rv); 15796 } 15797 15798 15799 /* 15800 * Process ioctl reset all request. 15801 */ 15802 static int 15803 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst) 15804 { 15805 sata_device_t sata_device; 15806 int rv = 0; 15807 int tcport; 15808 15809 sata_device.satadev_rev = SATA_DEVICE_REV; 15810 15811 /* 15812 * There is no protection here for configured devices. 15813 */ 15814 /* Sanity check */ 15815 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 15816 SATA_LOG_D((sata_hba_inst, CE_WARN, 15817 "sata_hba_ioctl: sata_hba_tran missing required " 15818 "function sata_tran_reset_dport")); 15819 return (ENOTSUP); 15820 } 15821 15822 /* 15823 * Need to lock all ports, not just one. 15824 * If any port is locked by event processing, fail the whole operation. 15825 * One port is already locked, but for simplicity lock it again. 15826 */ 15827 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) { 15828 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15829 cport_mutex); 15830 if (((SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15831 cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) { 15832 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15833 cport_mutex); 15834 rv = EBUSY; 15835 break; 15836 } else { 15837 /* 15838 * It is enough to lock cport in command-based 15839 * switching mode. 15840 */ 15841 SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15842 cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 15843 } 15844 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15845 cport_mutex); 15846 } 15847 15848 if (rv == 0) { 15849 /* 15850 * All cports were successfully locked. 15851 * Reset main SATA controller. 15852 * Set the device address to port 0, to have a valid device 15853 * address. 15854 */ 15855 sata_device.satadev_addr.qual = SATA_ADDR_CNTRL; 15856 sata_device.satadev_addr.cport = 0; 15857 sata_device.satadev_addr.pmport = 0; 15858 15859 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 15860 (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) { 15861 SATA_LOG_D((sata_hba_inst, CE_WARN, 15862 "sata_hba_ioctl: reset controller failed")); 15863 return (EIO); 15864 } 15865 } 15866 /* 15867 * Unlock all ports 15868 */ 15869 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) { 15870 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15871 cport_mutex); 15872 SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15873 cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY; 15874 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15875 cport_mutex); 15876 } 15877 15878 /* 15879 * This operation returns EFAULT if either reset 15880 * controller failed or a re-probing of any port failed. 15881 */ 15882 return (rv); 15883 } 15884 15885 15886 /* 15887 * Process ioctl port self test request. 15888 * 15889 * NOTE: Port multiplier code is not completed nor tested. 15890 */ 15891 static int 15892 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst, 15893 sata_device_t *sata_device) 15894 { 15895 int cport, pmport, qual; 15896 int rv = 0; 15897 15898 /* Sanity check */ 15899 if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL) 15900 return (ENOTSUP); 15901 15902 cport = sata_device->satadev_addr.cport; 15903 pmport = sata_device->satadev_addr.pmport; 15904 qual = sata_device->satadev_addr.qual; 15905 15906 /* 15907 * There is no protection here for a configured 15908 * device attached to this port. 15909 */ 15910 15911 if ((*SATA_SELFTEST_FUNC(sata_hba_inst)) 15912 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 15913 SATA_LOG_D((sata_hba_inst, CE_WARN, 15914 "sata_hba_ioctl: port selftest: " 15915 "failed port %d:%d", cport, pmport)); 15916 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15917 cport_mutex); 15918 sata_update_port_info(sata_hba_inst, sata_device); 15919 if (qual == SATA_ADDR_CPORT) 15920 SATA_CPORT_STATE(sata_hba_inst, cport) = 15921 SATA_PSTATE_FAILED; 15922 else { /* port multiplier device port */ 15923 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, 15924 cport, pmport)); 15925 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 15926 SATA_PSTATE_FAILED; 15927 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, 15928 cport, pmport)); 15929 } 15930 15931 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15932 cport_mutex); 15933 return (EIO); 15934 } 15935 /* 15936 * Beacuse the port was reset in the course of testing, it should be 15937 * re-probed and attached device state should be restored. At this 15938 * point the port state is unknown - it's state is HBA-specific. 15939 * Force port re-probing to get it into a known state. 15940 */ 15941 if (sata_reprobe_port(sata_hba_inst, sata_device, 15942 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 15943 rv = EIO; 15944 return (rv); 15945 } 15946 15947 15948 /* 15949 * sata_cfgadm_state: 15950 * Use the sata port state and state of the target node to figure out 15951 * the cfgadm_state. 15952 * 15953 * The port argument is a value with encoded cport, 15954 * pmport and address qualifier, in the same manner as a scsi target number. 15955 * SCSI_TO_SATA_CPORT macro extracts cport number, 15956 * SCSI_TO_SATA_PMPORT extracts pmport number and 15957 * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag. 15958 * 15959 * Port multiplier is supported. 15960 */ 15961 15962 static void 15963 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port, 15964 devctl_ap_state_t *ap_state) 15965 { 15966 uint8_t cport, pmport, qual; 15967 uint32_t port_state, pmult_state; 15968 uint32_t dev_type; 15969 sata_drive_info_t *sdinfo; 15970 15971 cport = SCSI_TO_SATA_CPORT(port); 15972 pmport = SCSI_TO_SATA_PMPORT(port); 15973 qual = SCSI_TO_SATA_ADDR_QUAL(port); 15974 15975 /* Check cport state */ 15976 port_state = SATA_CPORT_STATE(sata_hba_inst, cport); 15977 if (port_state & SATA_PSTATE_SHUTDOWN || 15978 port_state & SATA_PSTATE_FAILED) { 15979 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 15980 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 15981 if (port_state & SATA_PSTATE_FAILED) 15982 ap_state->ap_condition = AP_COND_FAILED; 15983 else 15984 ap_state->ap_condition = AP_COND_UNKNOWN; 15985 15986 return; 15987 } 15988 15989 /* cport state is okay. Now check pmport state */ 15990 if (qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) { 15991 /* Sanity check */ 15992 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 15993 SATA_DTYPE_PMULT || SATA_PMPORT_INFO(sata_hba_inst, 15994 cport, pmport) == NULL) 15995 return; 15996 port_state = SATA_PMPORT_STATE(sata_hba_inst, cport, pmport); 15997 if (port_state & SATA_PSTATE_SHUTDOWN || 15998 port_state & SATA_PSTATE_FAILED) { 15999 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 16000 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 16001 if (port_state & SATA_PSTATE_FAILED) 16002 ap_state->ap_condition = AP_COND_FAILED; 16003 else 16004 ap_state->ap_condition = AP_COND_UNKNOWN; 16005 16006 return; 16007 } 16008 } 16009 16010 /* Port is enabled and ready */ 16011 if (qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_CPORT) 16012 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, cport); 16013 else 16014 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport); 16015 16016 switch (dev_type) { 16017 case SATA_DTYPE_NONE: 16018 { 16019 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 16020 ap_state->ap_condition = AP_COND_OK; 16021 /* No device attached */ 16022 ap_state->ap_rstate = AP_RSTATE_EMPTY; 16023 break; 16024 } 16025 case SATA_DTYPE_PMULT: 16026 { 16027 /* Need to check port multiplier state */ 16028 ASSERT(qual == SATA_ADDR_DCPORT); 16029 pmult_state = SATA_PMULT_INFO(sata_hba_inst, cport)-> 16030 pmult_state; 16031 if (pmult_state & (SATA_PSTATE_SHUTDOWN|SATA_PSTATE_FAILED)) { 16032 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 16033 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 16034 if (pmult_state & SATA_PSTATE_FAILED) 16035 ap_state->ap_condition = AP_COND_FAILED; 16036 else 16037 ap_state->ap_condition = AP_COND_UNKNOWN; 16038 16039 return; 16040 } 16041 16042 /* Port multiplier is not configurable */ 16043 ap_state->ap_ostate = AP_OSTATE_CONFIGURED; 16044 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 16045 ap_state->ap_condition = AP_COND_OK; 16046 break; 16047 } 16048 16049 case SATA_DTYPE_ATADISK: 16050 case SATA_DTYPE_ATAPICD: 16051 case SATA_DTYPE_ATAPITAPE: 16052 case SATA_DTYPE_ATAPIDISK: 16053 { 16054 dev_info_t *tdip = NULL; 16055 dev_info_t *dip = NULL; 16056 int circ; 16057 16058 dip = SATA_DIP(sata_hba_inst); 16059 tdip = sata_get_target_dip(dip, cport, pmport); 16060 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 16061 if (tdip != NULL) { 16062 ndi_devi_enter(dip, &circ); 16063 mutex_enter(&(DEVI(tdip)->devi_lock)); 16064 if (DEVI_IS_DEVICE_REMOVED(tdip)) { 16065 /* 16066 * There could be the case where previously 16067 * configured and opened device was removed 16068 * and unknown device was plugged. 16069 * In such case we want to show a device, and 16070 * its configured or unconfigured state but 16071 * indicate unusable condition untill the 16072 * old target node is released and removed. 16073 */ 16074 ap_state->ap_condition = AP_COND_UNUSABLE; 16075 } else { 16076 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, 16077 cport)); 16078 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 16079 cport); 16080 if (sdinfo != NULL) { 16081 if ((sdinfo->satadrv_state & 16082 SATA_DSTATE_FAILED) != 0) 16083 ap_state->ap_condition = 16084 AP_COND_FAILED; 16085 else 16086 ap_state->ap_condition = 16087 AP_COND_OK; 16088 } else { 16089 ap_state->ap_condition = 16090 AP_COND_UNKNOWN; 16091 } 16092 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, 16093 cport)); 16094 } 16095 if ((DEVI_IS_DEVICE_OFFLINE(tdip)) || 16096 (DEVI_IS_DEVICE_DOWN(tdip))) { 16097 ap_state->ap_ostate = 16098 AP_OSTATE_UNCONFIGURED; 16099 } else { 16100 ap_state->ap_ostate = 16101 AP_OSTATE_CONFIGURED; 16102 } 16103 mutex_exit(&(DEVI(tdip)->devi_lock)); 16104 ndi_devi_exit(dip, circ); 16105 } else { 16106 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 16107 ap_state->ap_condition = AP_COND_UNKNOWN; 16108 } 16109 break; 16110 } 16111 case SATA_DTYPE_ATAPIPROC: 16112 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 16113 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 16114 ap_state->ap_condition = AP_COND_OK; 16115 break; 16116 default: 16117 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 16118 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 16119 ap_state->ap_condition = AP_COND_UNKNOWN; 16120 /* 16121 * This is actually internal error condition (non fatal), 16122 * because we have already checked all defined device types. 16123 */ 16124 SATA_LOG_D((sata_hba_inst, CE_WARN, 16125 "sata_cfgadm_state: Internal error: " 16126 "unknown device type")); 16127 break; 16128 } 16129 } 16130 16131 16132 /* 16133 * Process ioctl get device path request. 16134 * 16135 * NOTE: Port multiplier has no target dip. Devices connected to port 16136 * multiplier have target node attached to the HBA node. The only difference 16137 * between them and the directly-attached device node is a target address. 16138 */ 16139 static int 16140 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst, 16141 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 16142 { 16143 char path[MAXPATHLEN]; 16144 uint32_t size; 16145 dev_info_t *tdip; 16146 16147 (void) strcpy(path, "/devices"); 16148 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 16149 &sata_device->satadev_addr)) == NULL) { 16150 /* 16151 * No such device. If this is a request for a size, do not 16152 * return EINVAL for non-existing target, because cfgadm 16153 * will then indicate a meaningless ioctl failure. 16154 * If this is a request for a path, indicate invalid 16155 * argument. 16156 */ 16157 if (ioc->get_size == 0) 16158 return (EINVAL); 16159 } else { 16160 (void) ddi_pathname(tdip, path + strlen(path)); 16161 } 16162 size = strlen(path) + 1; 16163 16164 if (ioc->get_size != 0) { 16165 if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz, 16166 mode) != 0) 16167 return (EFAULT); 16168 } else { 16169 if (ioc->bufsiz != size) 16170 return (EINVAL); 16171 16172 else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz, 16173 mode) != 0) 16174 return (EFAULT); 16175 } 16176 return (0); 16177 } 16178 16179 /* 16180 * Process ioctl get attachment point type request. 16181 * 16182 * NOTE: Port multiplier is supported. 16183 */ 16184 static int 16185 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst, 16186 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 16187 { 16188 uint32_t type_len; 16189 const char *ap_type; 16190 int dev_type; 16191 16192 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 16193 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, 16194 sata_device->satadev_addr.cport); 16195 else /* pmport */ 16196 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, 16197 sata_device->satadev_addr.cport, 16198 sata_device->satadev_addr.pmport); 16199 16200 switch (dev_type) { 16201 case SATA_DTYPE_NONE: 16202 ap_type = "port"; 16203 break; 16204 16205 case SATA_DTYPE_ATADISK: 16206 case SATA_DTYPE_ATAPIDISK: 16207 ap_type = "disk"; 16208 break; 16209 16210 case SATA_DTYPE_ATAPICD: 16211 ap_type = "cd/dvd"; 16212 break; 16213 16214 case SATA_DTYPE_ATAPITAPE: 16215 ap_type = "tape"; 16216 break; 16217 16218 case SATA_DTYPE_ATAPIPROC: 16219 ap_type = "processor"; 16220 break; 16221 16222 case SATA_DTYPE_PMULT: 16223 ap_type = "sata-pmult"; 16224 break; 16225 16226 case SATA_DTYPE_UNKNOWN: 16227 ap_type = "unknown"; 16228 break; 16229 16230 default: 16231 ap_type = "unsupported"; 16232 break; 16233 16234 } /* end of dev_type switch */ 16235 16236 type_len = strlen(ap_type) + 1; 16237 16238 if (ioc->get_size) { 16239 if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz, 16240 mode) != 0) 16241 return (EFAULT); 16242 } else { 16243 if (ioc->bufsiz != type_len) 16244 return (EINVAL); 16245 16246 if (ddi_copyout((void *)ap_type, ioc->buf, 16247 ioc->bufsiz, mode) != 0) 16248 return (EFAULT); 16249 } 16250 return (0); 16251 16252 } 16253 16254 /* 16255 * Process ioctl get device model info request. 16256 * This operation should return to cfgadm the device model 16257 * information string 16258 * 16259 * NOTE: Port multiplier is supported. 16260 */ 16261 static int 16262 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst, 16263 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 16264 { 16265 sata_drive_info_t *sdinfo; 16266 uint32_t info_len; 16267 char ap_info[SATA_ID_MODEL_LEN + 1]; 16268 16269 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 16270 sata_device->satadev_addr.cport)->cport_mutex); 16271 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 16272 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 16273 sata_device->satadev_addr.cport); 16274 else /* port multiplier */ 16275 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 16276 sata_device->satadev_addr.cport, 16277 sata_device->satadev_addr.pmport); 16278 if (sdinfo == NULL) { 16279 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 16280 sata_device->satadev_addr.cport)->cport_mutex); 16281 return (EINVAL); 16282 } 16283 16284 #ifdef _LITTLE_ENDIAN 16285 swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN); 16286 #else /* _LITTLE_ENDIAN */ 16287 bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN); 16288 #endif /* _LITTLE_ENDIAN */ 16289 16290 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 16291 sata_device->satadev_addr.cport)->cport_mutex); 16292 16293 ap_info[SATA_ID_MODEL_LEN] = '\0'; 16294 16295 info_len = strlen(ap_info) + 1; 16296 16297 if (ioc->get_size) { 16298 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 16299 mode) != 0) 16300 return (EFAULT); 16301 } else { 16302 if (ioc->bufsiz < info_len) 16303 return (EINVAL); 16304 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 16305 mode) != 0) 16306 return (EFAULT); 16307 } 16308 return (0); 16309 } 16310 16311 16312 /* 16313 * Process ioctl get device firmware revision info request. 16314 * This operation should return to cfgadm the device firmware revision 16315 * information string 16316 * 16317 * Port multiplier is supported. 16318 */ 16319 static int 16320 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst, 16321 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 16322 { 16323 sata_drive_info_t *sdinfo; 16324 uint32_t info_len; 16325 char ap_info[SATA_ID_FW_LEN + 1]; 16326 16327 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 16328 sata_device->satadev_addr.cport)->cport_mutex); 16329 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 16330 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 16331 sata_device->satadev_addr.cport); 16332 else /* port multiplier */ 16333 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 16334 sata_device->satadev_addr.cport, 16335 sata_device->satadev_addr.pmport); 16336 if (sdinfo == NULL) { 16337 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 16338 sata_device->satadev_addr.cport)->cport_mutex); 16339 return (EINVAL); 16340 } 16341 16342 #ifdef _LITTLE_ENDIAN 16343 swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN); 16344 #else /* _LITTLE_ENDIAN */ 16345 bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN); 16346 #endif /* _LITTLE_ENDIAN */ 16347 16348 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 16349 sata_device->satadev_addr.cport)->cport_mutex); 16350 16351 ap_info[SATA_ID_FW_LEN] = '\0'; 16352 16353 info_len = strlen(ap_info) + 1; 16354 16355 if (ioc->get_size) { 16356 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 16357 mode) != 0) 16358 return (EFAULT); 16359 } else { 16360 if (ioc->bufsiz < info_len) 16361 return (EINVAL); 16362 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 16363 mode) != 0) 16364 return (EFAULT); 16365 } 16366 return (0); 16367 } 16368 16369 16370 /* 16371 * Process ioctl get device serial number info request. 16372 * This operation should return to cfgadm the device serial number string. 16373 * 16374 * NOTE: Port multiplier is supported. 16375 */ 16376 static int 16377 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst, 16378 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 16379 { 16380 sata_drive_info_t *sdinfo; 16381 uint32_t info_len; 16382 char ap_info[SATA_ID_SERIAL_LEN + 1]; 16383 16384 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 16385 sata_device->satadev_addr.cport)->cport_mutex); 16386 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 16387 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 16388 sata_device->satadev_addr.cport); 16389 else /* port multiplier */ 16390 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 16391 sata_device->satadev_addr.cport, 16392 sata_device->satadev_addr.pmport); 16393 if (sdinfo == NULL) { 16394 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 16395 sata_device->satadev_addr.cport)->cport_mutex); 16396 return (EINVAL); 16397 } 16398 16399 #ifdef _LITTLE_ENDIAN 16400 swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN); 16401 #else /* _LITTLE_ENDIAN */ 16402 bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN); 16403 #endif /* _LITTLE_ENDIAN */ 16404 16405 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 16406 sata_device->satadev_addr.cport)->cport_mutex); 16407 16408 ap_info[SATA_ID_SERIAL_LEN] = '\0'; 16409 16410 info_len = strlen(ap_info) + 1; 16411 16412 if (ioc->get_size) { 16413 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 16414 mode) != 0) 16415 return (EFAULT); 16416 } else { 16417 if (ioc->bufsiz < info_len) 16418 return (EINVAL); 16419 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 16420 mode) != 0) 16421 return (EFAULT); 16422 } 16423 return (0); 16424 } 16425 16426 16427 /* 16428 * Preset scsi extended sense data (to NO SENSE) 16429 * First 18 bytes of the sense data are preset to current valid sense 16430 * with a key NO SENSE data. 16431 * 16432 * Returns void 16433 */ 16434 static void 16435 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense) 16436 { 16437 sense->es_valid = 1; /* Valid sense */ 16438 sense->es_class = CLASS_EXTENDED_SENSE; /* 0x70 - current err */ 16439 sense->es_key = KEY_NO_SENSE; 16440 sense->es_info_1 = 0; 16441 sense->es_info_2 = 0; 16442 sense->es_info_3 = 0; 16443 sense->es_info_4 = 0; 16444 sense->es_add_len = 10; /* Additional length - replace with a def */ 16445 sense->es_cmd_info[0] = 0; 16446 sense->es_cmd_info[1] = 0; 16447 sense->es_cmd_info[2] = 0; 16448 sense->es_cmd_info[3] = 0; 16449 sense->es_add_code = 0; 16450 sense->es_qual_code = 0; 16451 } 16452 16453 /* 16454 * Register a legacy cmdk-style devid for the target (disk) device. 16455 * 16456 * Note: This function is called only when the HBA devinfo node has the 16457 * property "use-cmdk-devid-format" set. This property indicates that 16458 * devid compatible with old cmdk (target) driver is to be generated 16459 * for any target device attached to this controller. This will take 16460 * precedence over the devid generated by sd (target) driver. 16461 * This function is derived from cmdk_devid_setup() function in cmdk.c. 16462 */ 16463 static void 16464 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo) 16465 { 16466 char *hwid; 16467 int modlen; 16468 int serlen; 16469 int rval; 16470 ddi_devid_t devid; 16471 16472 /* 16473 * device ID is a concatanation of model number, "=", serial number. 16474 */ 16475 hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP); 16476 bcopy(&sdinfo->satadrv_id.ai_model, hwid, 16477 sizeof (sdinfo->satadrv_id.ai_model)); 16478 swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model)); 16479 modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model)); 16480 if (modlen == 0) 16481 goto err; 16482 hwid[modlen++] = '='; 16483 bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen], 16484 sizeof (sdinfo->satadrv_id.ai_drvser)); 16485 swab(&hwid[modlen], &hwid[modlen], 16486 sizeof (sdinfo->satadrv_id.ai_drvser)); 16487 serlen = sata_check_modser(&hwid[modlen], 16488 sizeof (sdinfo->satadrv_id.ai_drvser)); 16489 if (serlen == 0) 16490 goto err; 16491 hwid[modlen + serlen] = 0; /* terminate the hwid string */ 16492 16493 /* initialize/register devid */ 16494 if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL, 16495 (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) { 16496 rval = ddi_devid_register(dip, devid); 16497 /* 16498 * Free up the allocated devid buffer. 16499 * NOTE: This doesn't mean unregistering devid. 16500 */ 16501 ddi_devid_free(devid); 16502 } 16503 16504 if (rval != DDI_SUCCESS) 16505 cmn_err(CE_WARN, "sata: failed to create devid for the disk" 16506 " on port %d", sdinfo->satadrv_addr.cport); 16507 err: 16508 kmem_free(hwid, LEGACY_HWID_LEN); 16509 } 16510 16511 /* 16512 * valid model/serial string must contain a non-zero non-space characters. 16513 * trim trailing spaces/NULLs. 16514 */ 16515 static int 16516 sata_check_modser(char *buf, int buf_len) 16517 { 16518 boolean_t ret; 16519 char *s; 16520 int i; 16521 int tb; 16522 char ch; 16523 16524 ret = B_FALSE; 16525 s = buf; 16526 for (i = 0; i < buf_len; i++) { 16527 ch = *s++; 16528 if (ch != ' ' && ch != '\0') 16529 tb = i + 1; 16530 if (ch != ' ' && ch != '\0' && ch != '0') 16531 ret = B_TRUE; 16532 } 16533 16534 if (ret == B_FALSE) 16535 return (0); /* invalid string */ 16536 16537 return (tb); /* return length */ 16538 } 16539 16540 /* 16541 * sata_set_drive_features function compares current device features setting 16542 * with the saved device features settings and, if there is a difference, 16543 * it restores device features setting to the previously saved state. 16544 * It also arbitrarily tries to select the highest supported DMA mode. 16545 * Device Identify or Identify Packet Device data has to be current. 16546 * At the moment read ahead and write cache are considered for all devices. 16547 * For atapi devices, Removable Media Status Notification is set in addition 16548 * to common features. 16549 * 16550 * This function cannot be called in the interrupt context (it may sleep). 16551 * 16552 * The input argument sdinfo should point to the drive info structure 16553 * to be updated after features are set. Note, that only 16554 * device (packet) identify data is updated, not the flags indicating the 16555 * supported features. 16556 * 16557 * Returns SATA_SUCCESS if successful or there was nothing to do. 16558 * Device Identify data in the drive info structure pointed to by the sdinfo 16559 * arguments is updated even when no features were set or changed. 16560 * 16561 * Returns SATA_FAILURE if device features could not be set or DMA mode 16562 * for a disk cannot be set and device identify data cannot be fetched. 16563 * 16564 * Returns SATA_RETRY if device features could not be set (other than disk 16565 * DMA mode) but the device identify data was fetched successfully. 16566 * 16567 * Note: This function may fail the port, making it inaccessible. 16568 * In such case the explicit port disconnect/connect or physical device 16569 * detach/attach is required to re-evaluate port state again. 16570 */ 16571 16572 static int 16573 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst, 16574 sata_drive_info_t *sdinfo, int restore) 16575 { 16576 int rval = SATA_SUCCESS; 16577 int rval_set; 16578 sata_drive_info_t new_sdinfo; 16579 char *finfo = "sata_set_drive_features: cannot"; 16580 char *finfox; 16581 int cache_op; 16582 16583 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 16584 new_sdinfo.satadrv_addr = sdinfo->satadrv_addr; 16585 new_sdinfo.satadrv_type = sdinfo->satadrv_type; 16586 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 16587 /* 16588 * Cannot get device identification - caller may retry later 16589 */ 16590 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16591 "%s fetch device identify data\n", finfo); 16592 return (SATA_FAILURE); 16593 } 16594 finfox = (restore != 0) ? " restore device features" : 16595 " initialize device features\n"; 16596 16597 switch (sdinfo->satadrv_type) { 16598 case SATA_DTYPE_ATADISK: 16599 /* Arbitrarily set UDMA mode */ 16600 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) != 16601 SATA_SUCCESS) { 16602 SATA_LOG_D((sata_hba_inst, CE_WARN, 16603 "%s set UDMA mode\n", finfo)); 16604 return (SATA_FAILURE); 16605 } 16606 break; 16607 case SATA_DTYPE_ATAPICD: 16608 case SATA_DTYPE_ATAPITAPE: 16609 case SATA_DTYPE_ATAPIDISK: 16610 /* Set Removable Media Status Notification, if necessary */ 16611 if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) && 16612 restore != 0) { 16613 if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) && 16614 (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))|| 16615 ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) && 16616 SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) { 16617 /* Current setting does not match saved one */ 16618 if (sata_set_rmsn(sata_hba_inst, sdinfo, 16619 sdinfo->satadrv_settings & 16620 SATA_DEV_RMSN) != SATA_SUCCESS) 16621 rval = SATA_FAILURE; 16622 } 16623 } 16624 /* 16625 * We have to set Multiword DMA or UDMA, if it is supported, as 16626 * we want to use DMA transfer mode whenever possible. 16627 * Some devices require explicit setting of the DMA mode. 16628 */ 16629 if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) { 16630 /* Set highest supported DMA mode */ 16631 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) != 16632 SATA_SUCCESS) { 16633 SATA_LOG_D((sata_hba_inst, CE_WARN, 16634 "%s set UDMA mode\n", finfo)); 16635 rval = SATA_FAILURE; 16636 } 16637 } 16638 break; 16639 } 16640 16641 if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) && 16642 !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) { 16643 /* 16644 * neither READ AHEAD nor WRITE CACHE is supported 16645 * - do nothing 16646 */ 16647 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16648 "settable features not supported\n", NULL); 16649 goto update_sdinfo; 16650 } 16651 16652 if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) && 16653 (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) && 16654 (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) && 16655 (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) { 16656 /* 16657 * both READ AHEAD and WRITE CACHE are enabled 16658 * - Nothing to do 16659 */ 16660 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16661 "no device features to set\n", NULL); 16662 goto update_sdinfo; 16663 } 16664 16665 cache_op = 0; 16666 16667 if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) { 16668 if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) && 16669 !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) { 16670 /* Enable read ahead / read cache */ 16671 cache_op = SATAC_SF_ENABLE_READ_AHEAD; 16672 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16673 "enabling read cache\n", NULL); 16674 } else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) && 16675 SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) { 16676 /* Disable read ahead / read cache */ 16677 cache_op = SATAC_SF_DISABLE_READ_AHEAD; 16678 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16679 "disabling read cache\n", NULL); 16680 } 16681 16682 if (cache_op != 0) { 16683 /* Try to set read cache mode */ 16684 rval_set = sata_set_cache_mode(sata_hba_inst, 16685 &new_sdinfo, cache_op); 16686 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS) 16687 rval = rval_set; 16688 } 16689 } 16690 16691 cache_op = 0; 16692 16693 if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) { 16694 if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) && 16695 !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) { 16696 /* Enable write cache */ 16697 cache_op = SATAC_SF_ENABLE_WRITE_CACHE; 16698 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16699 "enabling write cache\n", NULL); 16700 } else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) && 16701 SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) { 16702 /* Disable write cache */ 16703 cache_op = SATAC_SF_DISABLE_WRITE_CACHE; 16704 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16705 "disabling write cache\n", NULL); 16706 } 16707 16708 if (cache_op != 0) { 16709 /* Try to set write cache mode */ 16710 rval_set = sata_set_cache_mode(sata_hba_inst, 16711 &new_sdinfo, cache_op); 16712 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS) 16713 rval = rval_set; 16714 } 16715 } 16716 if (rval != SATA_SUCCESS) 16717 SATA_LOG_D((sata_hba_inst, CE_WARN, 16718 "%s %s", finfo, finfox)); 16719 16720 update_sdinfo: 16721 /* 16722 * We need to fetch Device Identify data again 16723 */ 16724 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 16725 /* 16726 * Cannot get device identification - retry later 16727 */ 16728 SATA_LOG_D((sata_hba_inst, CE_WARN, 16729 "%s re-fetch device identify data\n", finfo)); 16730 rval = SATA_FAILURE; 16731 } 16732 /* Copy device sata info. */ 16733 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 16734 16735 return (rval); 16736 } 16737 16738 16739 /* 16740 * 16741 * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if 16742 * unable to determine. 16743 * 16744 * Cannot be called in an interrupt context. 16745 * 16746 * Called by sata_build_lsense_page_2f() 16747 */ 16748 16749 static int 16750 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst, 16751 sata_drive_info_t *sdinfo) 16752 { 16753 sata_pkt_t *spkt; 16754 sata_cmd_t *scmd; 16755 sata_pkt_txlate_t *spx; 16756 int rval; 16757 16758 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 16759 spx->txlt_sata_hba_inst = sata_hba_inst; 16760 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 16761 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 16762 if (spkt == NULL) { 16763 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16764 return (-1); 16765 } 16766 /* address is needed now */ 16767 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16768 16769 16770 /* Fill sata_pkt */ 16771 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16772 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 16773 /* Synchronous mode, no callback */ 16774 spkt->satapkt_comp = NULL; 16775 /* Timeout 30s */ 16776 spkt->satapkt_time = sata_default_pkt_time; 16777 16778 scmd = &spkt->satapkt_cmd; 16779 scmd->satacmd_flags.sata_special_regs = B_TRUE; 16780 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 16781 16782 /* Set up which registers need to be returned */ 16783 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE; 16784 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE; 16785 16786 /* Build SMART_RETURN_STATUS cmd in the sata_pkt */ 16787 scmd->satacmd_addr_type = 0; /* N/A */ 16788 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 16789 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 16790 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 16791 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 16792 scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS; 16793 scmd->satacmd_device_reg = 0; /* Always device 0 */ 16794 scmd->satacmd_cmd_reg = SATAC_SMART; 16795 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 16796 sdinfo->satadrv_addr.cport))); 16797 16798 16799 /* Send pkt to SATA HBA driver */ 16800 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 16801 SATA_TRAN_ACCEPTED || 16802 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 16803 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16804 sdinfo->satadrv_addr.cport))); 16805 /* 16806 * Whoops, no SMART RETURN STATUS 16807 */ 16808 rval = -1; 16809 } else { 16810 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16811 sdinfo->satadrv_addr.cport))); 16812 if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) { 16813 rval = -1; 16814 goto fail; 16815 } 16816 if (scmd->satacmd_status_reg & SATA_STATUS_ERR) { 16817 rval = -1; 16818 goto fail; 16819 } 16820 if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) && 16821 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2)) 16822 rval = 0; 16823 else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) && 16824 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4)) 16825 rval = 1; 16826 else { 16827 rval = -1; 16828 goto fail; 16829 } 16830 } 16831 fail: 16832 /* Free allocated resources */ 16833 sata_pkt_free(spx); 16834 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16835 16836 return (rval); 16837 } 16838 16839 /* 16840 * 16841 * Returns 0 if succeeded, -1 otherwise 16842 * 16843 * Cannot be called in an interrupt context. 16844 * 16845 */ 16846 static int 16847 sata_fetch_smart_data( 16848 sata_hba_inst_t *sata_hba_inst, 16849 sata_drive_info_t *sdinfo, 16850 struct smart_data *smart_data) 16851 { 16852 sata_pkt_t *spkt; 16853 sata_cmd_t *scmd; 16854 sata_pkt_txlate_t *spx; 16855 int rval; 16856 dev_info_t *dip = SATA_DIP(sata_hba_inst); 16857 16858 #if ! defined(lint) 16859 ASSERT(sizeof (struct smart_data) == 512); 16860 #endif 16861 16862 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 16863 spx->txlt_sata_hba_inst = sata_hba_inst; 16864 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 16865 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 16866 if (spkt == NULL) { 16867 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16868 return (-1); 16869 } 16870 /* address is needed now */ 16871 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16872 16873 16874 /* Fill sata_pkt */ 16875 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16876 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 16877 /* Synchronous mode, no callback */ 16878 spkt->satapkt_comp = NULL; 16879 /* Timeout 30s */ 16880 spkt->satapkt_time = sata_default_pkt_time; 16881 16882 scmd = &spkt->satapkt_cmd; 16883 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 16884 16885 /* 16886 * Allocate buffer for SMART data 16887 */ 16888 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 16889 sizeof (struct smart_data)); 16890 if (scmd->satacmd_bp == NULL) { 16891 sata_pkt_free(spx); 16892 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16893 SATA_LOG_D((sata_hba_inst, CE_WARN, 16894 "sata_fetch_smart_data: " 16895 "cannot allocate buffer")); 16896 return (-1); 16897 } 16898 16899 16900 /* Build SMART_READ_DATA cmd in the sata_pkt */ 16901 scmd->satacmd_addr_type = 0; /* N/A */ 16902 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 16903 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 16904 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 16905 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 16906 scmd->satacmd_features_reg = SATA_SMART_READ_DATA; 16907 scmd->satacmd_device_reg = 0; /* Always device 0 */ 16908 scmd->satacmd_cmd_reg = SATAC_SMART; 16909 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 16910 sdinfo->satadrv_addr.cport))); 16911 16912 /* Send pkt to SATA HBA driver */ 16913 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 16914 SATA_TRAN_ACCEPTED || 16915 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 16916 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16917 sdinfo->satadrv_addr.cport))); 16918 /* 16919 * Whoops, no SMART DATA available 16920 */ 16921 rval = -1; 16922 goto fail; 16923 } else { 16924 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16925 sdinfo->satadrv_addr.cport))); 16926 if (spx->txlt_buf_dma_handle != NULL) { 16927 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 16928 DDI_DMA_SYNC_FORKERNEL); 16929 ASSERT(rval == DDI_SUCCESS); 16930 if (sata_check_for_dma_error(dip, spx)) { 16931 ddi_fm_service_impact(dip, 16932 DDI_SERVICE_UNAFFECTED); 16933 rval = -1; 16934 goto fail; 16935 } 16936 } 16937 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data, 16938 sizeof (struct smart_data)); 16939 } 16940 16941 fail: 16942 /* Free allocated resources */ 16943 sata_free_local_buffer(spx); 16944 sata_pkt_free(spx); 16945 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16946 16947 return (rval); 16948 } 16949 16950 /* 16951 * Used by LOG SENSE page 0x10 16952 * Reads (in synchronous mode) the self test log data using Read Log Ext cmd. 16953 * Note: cannot be called in the interrupt context. 16954 * 16955 * return 0 for success, -1 otherwise 16956 * 16957 */ 16958 static int 16959 sata_ext_smart_selftest_read_log( 16960 sata_hba_inst_t *sata_hba_inst, 16961 sata_drive_info_t *sdinfo, 16962 struct smart_ext_selftest_log *ext_selftest_log, 16963 uint16_t block_num) 16964 { 16965 sata_pkt_txlate_t *spx; 16966 sata_pkt_t *spkt; 16967 sata_cmd_t *scmd; 16968 int rval; 16969 dev_info_t *dip = SATA_DIP(sata_hba_inst); 16970 16971 #if ! defined(lint) 16972 ASSERT(sizeof (struct smart_ext_selftest_log) == 512); 16973 #endif 16974 16975 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 16976 spx->txlt_sata_hba_inst = sata_hba_inst; 16977 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 16978 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 16979 if (spkt == NULL) { 16980 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16981 return (-1); 16982 } 16983 /* address is needed now */ 16984 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16985 16986 16987 /* Fill sata_pkt */ 16988 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16989 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 16990 /* Synchronous mode, no callback */ 16991 spkt->satapkt_comp = NULL; 16992 /* Timeout 30s */ 16993 spkt->satapkt_time = sata_default_pkt_time; 16994 16995 scmd = &spkt->satapkt_cmd; 16996 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 16997 16998 /* 16999 * Allocate buffer for SMART extended self-test log 17000 */ 17001 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 17002 sizeof (struct smart_ext_selftest_log)); 17003 if (scmd->satacmd_bp == NULL) { 17004 sata_pkt_free(spx); 17005 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17006 SATA_LOG_D((sata_hba_inst, CE_WARN, 17007 "sata_ext_smart_selftest_log: " 17008 "cannot allocate buffer")); 17009 return (-1); 17010 } 17011 17012 /* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */ 17013 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 17014 scmd->satacmd_sec_count_lsb = 1; /* One sector of selftest log */ 17015 scmd->satacmd_sec_count_msb = 0; /* One sector of selftest log */ 17016 scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE; 17017 scmd->satacmd_lba_low_msb = 0; 17018 scmd->satacmd_lba_mid_lsb = block_num & 0xff; 17019 scmd->satacmd_lba_mid_msb = block_num >> 8; 17020 scmd->satacmd_device_reg = 0; /* Always device 0 */ 17021 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 17022 17023 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 17024 sdinfo->satadrv_addr.cport))); 17025 17026 /* Send pkt to SATA HBA driver */ 17027 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 17028 SATA_TRAN_ACCEPTED || 17029 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 17030 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17031 sdinfo->satadrv_addr.cport))); 17032 17033 /* 17034 * Whoops, no SMART selftest log info available 17035 */ 17036 rval = -1; 17037 goto fail; 17038 } else { 17039 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17040 sdinfo->satadrv_addr.cport))); 17041 17042 if (spx->txlt_buf_dma_handle != NULL) { 17043 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 17044 DDI_DMA_SYNC_FORKERNEL); 17045 ASSERT(rval == DDI_SUCCESS); 17046 if (sata_check_for_dma_error(dip, spx)) { 17047 ddi_fm_service_impact(dip, 17048 DDI_SERVICE_UNAFFECTED); 17049 rval = -1; 17050 goto fail; 17051 } 17052 } 17053 bcopy(scmd->satacmd_bp->b_un.b_addr, 17054 (uint8_t *)ext_selftest_log, 17055 sizeof (struct smart_ext_selftest_log)); 17056 rval = 0; 17057 } 17058 17059 fail: 17060 /* Free allocated resources */ 17061 sata_free_local_buffer(spx); 17062 sata_pkt_free(spx); 17063 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17064 17065 return (rval); 17066 } 17067 17068 /* 17069 * Returns 0 for success, -1 otherwise 17070 * 17071 * SMART self-test log data is returned in buffer pointed to by selftest_log 17072 */ 17073 static int 17074 sata_smart_selftest_log( 17075 sata_hba_inst_t *sata_hba_inst, 17076 sata_drive_info_t *sdinfo, 17077 struct smart_selftest_log *selftest_log) 17078 { 17079 sata_pkt_t *spkt; 17080 sata_cmd_t *scmd; 17081 sata_pkt_txlate_t *spx; 17082 int rval; 17083 dev_info_t *dip = SATA_DIP(sata_hba_inst); 17084 17085 #if ! defined(lint) 17086 ASSERT(sizeof (struct smart_selftest_log) == 512); 17087 #endif 17088 17089 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 17090 spx->txlt_sata_hba_inst = sata_hba_inst; 17091 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 17092 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 17093 if (spkt == NULL) { 17094 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17095 return (-1); 17096 } 17097 /* address is needed now */ 17098 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 17099 17100 17101 /* Fill sata_pkt */ 17102 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 17103 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 17104 /* Synchronous mode, no callback */ 17105 spkt->satapkt_comp = NULL; 17106 /* Timeout 30s */ 17107 spkt->satapkt_time = sata_default_pkt_time; 17108 17109 scmd = &spkt->satapkt_cmd; 17110 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 17111 17112 /* 17113 * Allocate buffer for SMART SELFTEST LOG 17114 */ 17115 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 17116 sizeof (struct smart_selftest_log)); 17117 if (scmd->satacmd_bp == NULL) { 17118 sata_pkt_free(spx); 17119 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17120 SATA_LOG_D((sata_hba_inst, CE_WARN, 17121 "sata_smart_selftest_log: " 17122 "cannot allocate buffer")); 17123 return (-1); 17124 } 17125 17126 /* Build SMART_READ_LOG cmd in the sata_pkt */ 17127 scmd->satacmd_addr_type = 0; /* N/A */ 17128 scmd->satacmd_sec_count_lsb = 1; /* One sector of SMART log */ 17129 scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE; 17130 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 17131 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 17132 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 17133 scmd->satacmd_device_reg = 0; /* Always device 0 */ 17134 scmd->satacmd_cmd_reg = SATAC_SMART; 17135 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 17136 sdinfo->satadrv_addr.cport))); 17137 17138 /* Send pkt to SATA HBA driver */ 17139 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 17140 SATA_TRAN_ACCEPTED || 17141 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 17142 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17143 sdinfo->satadrv_addr.cport))); 17144 /* 17145 * Whoops, no SMART DATA available 17146 */ 17147 rval = -1; 17148 goto fail; 17149 } else { 17150 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17151 sdinfo->satadrv_addr.cport))); 17152 if (spx->txlt_buf_dma_handle != NULL) { 17153 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 17154 DDI_DMA_SYNC_FORKERNEL); 17155 ASSERT(rval == DDI_SUCCESS); 17156 if (sata_check_for_dma_error(dip, spx)) { 17157 ddi_fm_service_impact(dip, 17158 DDI_SERVICE_UNAFFECTED); 17159 rval = -1; 17160 goto fail; 17161 } 17162 } 17163 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log, 17164 sizeof (struct smart_selftest_log)); 17165 rval = 0; 17166 } 17167 17168 fail: 17169 /* Free allocated resources */ 17170 sata_free_local_buffer(spx); 17171 sata_pkt_free(spx); 17172 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17173 17174 return (rval); 17175 } 17176 17177 17178 /* 17179 * Returns 0 for success, -1 otherwise 17180 * 17181 * SMART READ LOG data is returned in buffer pointed to by smart_log 17182 */ 17183 static int 17184 sata_smart_read_log( 17185 sata_hba_inst_t *sata_hba_inst, 17186 sata_drive_info_t *sdinfo, 17187 uint8_t *smart_log, /* where the data should be returned */ 17188 uint8_t which_log, /* which log should be returned */ 17189 uint8_t log_size) /* # of 512 bytes in log */ 17190 { 17191 sata_pkt_t *spkt; 17192 sata_cmd_t *scmd; 17193 sata_pkt_txlate_t *spx; 17194 int rval; 17195 dev_info_t *dip = SATA_DIP(sata_hba_inst); 17196 17197 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 17198 spx->txlt_sata_hba_inst = sata_hba_inst; 17199 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 17200 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 17201 if (spkt == NULL) { 17202 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17203 return (-1); 17204 } 17205 /* address is needed now */ 17206 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 17207 17208 17209 /* Fill sata_pkt */ 17210 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 17211 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 17212 /* Synchronous mode, no callback */ 17213 spkt->satapkt_comp = NULL; 17214 /* Timeout 30s */ 17215 spkt->satapkt_time = sata_default_pkt_time; 17216 17217 scmd = &spkt->satapkt_cmd; 17218 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 17219 17220 /* 17221 * Allocate buffer for SMART READ LOG 17222 */ 17223 scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512); 17224 if (scmd->satacmd_bp == NULL) { 17225 sata_pkt_free(spx); 17226 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17227 SATA_LOG_D((sata_hba_inst, CE_WARN, 17228 "sata_smart_read_log: " "cannot allocate buffer")); 17229 return (-1); 17230 } 17231 17232 /* Build SMART_READ_LOG cmd in the sata_pkt */ 17233 scmd->satacmd_addr_type = 0; /* N/A */ 17234 scmd->satacmd_sec_count_lsb = log_size; /* what the caller asked for */ 17235 scmd->satacmd_lba_low_lsb = which_log; /* which log page */ 17236 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 17237 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 17238 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 17239 scmd->satacmd_device_reg = 0; /* Always device 0 */ 17240 scmd->satacmd_cmd_reg = SATAC_SMART; 17241 17242 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 17243 sdinfo->satadrv_addr.cport))); 17244 17245 /* Send pkt to SATA HBA driver */ 17246 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 17247 SATA_TRAN_ACCEPTED || 17248 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 17249 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17250 sdinfo->satadrv_addr.cport))); 17251 17252 /* 17253 * Whoops, no SMART DATA available 17254 */ 17255 rval = -1; 17256 goto fail; 17257 } else { 17258 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17259 sdinfo->satadrv_addr.cport))); 17260 17261 if (spx->txlt_buf_dma_handle != NULL) { 17262 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 17263 DDI_DMA_SYNC_FORKERNEL); 17264 ASSERT(rval == DDI_SUCCESS); 17265 if (sata_check_for_dma_error(dip, spx)) { 17266 ddi_fm_service_impact(dip, 17267 DDI_SERVICE_UNAFFECTED); 17268 rval = -1; 17269 goto fail; 17270 } 17271 } 17272 bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512); 17273 rval = 0; 17274 } 17275 17276 fail: 17277 /* Free allocated resources */ 17278 sata_free_local_buffer(spx); 17279 sata_pkt_free(spx); 17280 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17281 17282 return (rval); 17283 } 17284 17285 /* 17286 * Used by LOG SENSE page 0x10 17287 * 17288 * return 0 for success, -1 otherwise 17289 * 17290 */ 17291 static int 17292 sata_read_log_ext_directory( 17293 sata_hba_inst_t *sata_hba_inst, 17294 sata_drive_info_t *sdinfo, 17295 struct read_log_ext_directory *logdir) 17296 { 17297 sata_pkt_txlate_t *spx; 17298 sata_pkt_t *spkt; 17299 sata_cmd_t *scmd; 17300 int rval; 17301 dev_info_t *dip = SATA_DIP(sata_hba_inst); 17302 17303 #if ! defined(lint) 17304 ASSERT(sizeof (struct read_log_ext_directory) == 512); 17305 #endif 17306 17307 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 17308 spx->txlt_sata_hba_inst = sata_hba_inst; 17309 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 17310 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 17311 if (spkt == NULL) { 17312 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17313 return (-1); 17314 } 17315 17316 /* Fill sata_pkt */ 17317 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 17318 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 17319 /* Synchronous mode, no callback */ 17320 spkt->satapkt_comp = NULL; 17321 /* Timeout 30s */ 17322 spkt->satapkt_time = sata_default_pkt_time; 17323 17324 scmd = &spkt->satapkt_cmd; 17325 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 17326 17327 /* 17328 * Allocate buffer for SMART READ LOG EXTENDED command 17329 */ 17330 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 17331 sizeof (struct read_log_ext_directory)); 17332 if (scmd->satacmd_bp == NULL) { 17333 sata_pkt_free(spx); 17334 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17335 SATA_LOG_D((sata_hba_inst, CE_WARN, 17336 "sata_read_log_ext_directory: " 17337 "cannot allocate buffer")); 17338 return (-1); 17339 } 17340 17341 /* Build READ LOG EXT w/ log directory cmd in the sata_pkt */ 17342 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 17343 scmd->satacmd_sec_count_lsb = 1; /* One sector of directory */ 17344 scmd->satacmd_sec_count_msb = 0; /* One sector of directory */ 17345 scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY; 17346 scmd->satacmd_lba_low_msb = 0; 17347 scmd->satacmd_lba_mid_lsb = 0; 17348 scmd->satacmd_lba_mid_msb = 0; 17349 scmd->satacmd_device_reg = 0; /* Always device 0 */ 17350 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 17351 17352 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 17353 sdinfo->satadrv_addr.cport))); 17354 17355 /* Send pkt to SATA HBA driver */ 17356 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 17357 SATA_TRAN_ACCEPTED || 17358 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 17359 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17360 sdinfo->satadrv_addr.cport))); 17361 /* 17362 * Whoops, no SMART selftest log info available 17363 */ 17364 rval = -1; 17365 goto fail; 17366 } else { 17367 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17368 sdinfo->satadrv_addr.cport))); 17369 if (spx->txlt_buf_dma_handle != NULL) { 17370 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 17371 DDI_DMA_SYNC_FORKERNEL); 17372 ASSERT(rval == DDI_SUCCESS); 17373 if (sata_check_for_dma_error(dip, spx)) { 17374 ddi_fm_service_impact(dip, 17375 DDI_SERVICE_UNAFFECTED); 17376 rval = -1; 17377 goto fail; 17378 } 17379 } 17380 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir, 17381 sizeof (struct read_log_ext_directory)); 17382 rval = 0; 17383 } 17384 17385 fail: 17386 /* Free allocated resources */ 17387 sata_free_local_buffer(spx); 17388 sata_pkt_free(spx); 17389 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17390 17391 return (rval); 17392 } 17393 17394 /* 17395 * Set up error retrieval sata command for NCQ command error data 17396 * recovery. 17397 * 17398 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up, 17399 * returns SATA_FAILURE otherwise. 17400 */ 17401 static int 17402 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo) 17403 { 17404 #ifndef __lock_lint 17405 _NOTE(ARGUNUSED(sdinfo)) 17406 #endif 17407 17408 sata_pkt_t *spkt = spx->txlt_sata_pkt; 17409 sata_cmd_t *scmd; 17410 struct buf *bp; 17411 17412 /* Operation modes are up to the caller */ 17413 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 17414 17415 /* Synchronous mode, no callback - may be changed by the caller */ 17416 spkt->satapkt_comp = NULL; 17417 spkt->satapkt_time = sata_default_pkt_time; 17418 17419 scmd = &spkt->satapkt_cmd; 17420 bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t)); 17421 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 17422 17423 /* 17424 * Allocate dma_able buffer error data. 17425 * Buffer allocation will take care of buffer alignment and other DMA 17426 * attributes. 17427 */ 17428 bp = sata_alloc_local_buffer(spx, 17429 sizeof (struct sata_ncq_error_recovery_page)); 17430 if (bp == NULL) 17431 return (SATA_FAILURE); 17432 17433 bp_mapin(bp); /* make data buffer accessible */ 17434 scmd->satacmd_bp = bp; 17435 17436 /* 17437 * Set-up pointer to the buffer handle, so HBA can sync buffer 17438 * before accessing it. Handle is in usual place in translate struct. 17439 */ 17440 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle; 17441 17442 ASSERT(scmd->satacmd_num_dma_cookies != 0); 17443 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 17444 17445 return (SATA_SUCCESS); 17446 } 17447 17448 /* 17449 * sata_xlate_errors() is used to translate (S)ATA error 17450 * information to SCSI information returned in the SCSI 17451 * packet. 17452 */ 17453 static void 17454 sata_xlate_errors(sata_pkt_txlate_t *spx) 17455 { 17456 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 17457 struct scsi_extended_sense *sense; 17458 17459 scsipkt->pkt_reason = CMD_INCOMPLETE; 17460 *scsipkt->pkt_scbp = STATUS_CHECK; 17461 sense = sata_arq_sense(spx); 17462 17463 switch (spx->txlt_sata_pkt->satapkt_reason) { 17464 case SATA_PKT_PORT_ERROR: 17465 /* 17466 * We have no device data. Assume no data transfered. 17467 */ 17468 sense->es_key = KEY_HARDWARE_ERROR; 17469 break; 17470 17471 case SATA_PKT_DEV_ERROR: 17472 if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 17473 SATA_STATUS_ERR) { 17474 /* 17475 * determine dev error reason from error 17476 * reg content 17477 */ 17478 sata_decode_device_error(spx, sense); 17479 break; 17480 } 17481 /* No extended sense key - no info available */ 17482 break; 17483 17484 case SATA_PKT_TIMEOUT: 17485 scsipkt->pkt_reason = CMD_TIMEOUT; 17486 scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET; 17487 /* No extended sense key */ 17488 break; 17489 17490 case SATA_PKT_ABORTED: 17491 scsipkt->pkt_reason = CMD_ABORTED; 17492 scsipkt->pkt_statistics |= STAT_ABORTED; 17493 /* No extended sense key */ 17494 break; 17495 17496 case SATA_PKT_RESET: 17497 /* 17498 * pkt aborted either by an explicit reset request from 17499 * a host, or due to error recovery 17500 */ 17501 scsipkt->pkt_reason = CMD_RESET; 17502 scsipkt->pkt_statistics |= STAT_DEV_RESET; 17503 break; 17504 17505 default: 17506 scsipkt->pkt_reason = CMD_TRAN_ERR; 17507 break; 17508 } 17509 } 17510 17511 17512 17513 17514 /* 17515 * Log sata message 17516 * dev pathname msg line preceeds the logged message. 17517 */ 17518 17519 static void 17520 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...) 17521 { 17522 char pathname[128]; 17523 dev_info_t *dip = NULL; 17524 va_list ap; 17525 17526 mutex_enter(&sata_log_mutex); 17527 17528 va_start(ap, fmt); 17529 (void) vsprintf(sata_log_buf, fmt, ap); 17530 va_end(ap); 17531 17532 if (sata_hba_inst != NULL) { 17533 dip = SATA_DIP(sata_hba_inst); 17534 (void) ddi_pathname(dip, pathname); 17535 } else { 17536 pathname[0] = 0; 17537 } 17538 if (level == CE_CONT) { 17539 if (sata_debug_flags == 0) 17540 cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf); 17541 else 17542 cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf); 17543 } else { 17544 if (level != CE_NOTE) { 17545 cmn_err(level, "%s:\n %s", pathname, sata_log_buf); 17546 } else if (sata_msg) { 17547 cmn_err(level, "%s:\n %s", pathname, 17548 sata_log_buf); 17549 } 17550 } 17551 17552 /* sata trace debug */ 17553 sata_trace_debug(dip, sata_log_buf); 17554 17555 mutex_exit(&sata_log_mutex); 17556 } 17557 17558 17559 /* ******** Asynchronous HBA events handling & hotplugging support ******** */ 17560 17561 /* 17562 * Start or terminate the thread, depending on flag arg and current state 17563 */ 17564 static void 17565 sata_event_thread_control(int startstop) 17566 { 17567 static int sata_event_thread_terminating = 0; 17568 static int sata_event_thread_starting = 0; 17569 int i; 17570 17571 mutex_enter(&sata_event_mutex); 17572 17573 if (startstop == 0 && (sata_event_thread_starting == 1 || 17574 sata_event_thread_terminating == 1)) { 17575 mutex_exit(&sata_event_mutex); 17576 return; 17577 } 17578 if (startstop == 1 && sata_event_thread_starting == 1) { 17579 mutex_exit(&sata_event_mutex); 17580 return; 17581 } 17582 if (startstop == 1 && sata_event_thread_terminating == 1) { 17583 sata_event_thread_starting = 1; 17584 /* wait til terminate operation completes */ 17585 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 17586 while (sata_event_thread_terminating == 1) { 17587 if (i-- <= 0) { 17588 sata_event_thread_starting = 0; 17589 mutex_exit(&sata_event_mutex); 17590 #ifdef SATA_DEBUG 17591 cmn_err(CE_WARN, "sata_event_thread_control: " 17592 "timeout waiting for thread to terminate"); 17593 #endif 17594 return; 17595 } 17596 mutex_exit(&sata_event_mutex); 17597 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 17598 mutex_enter(&sata_event_mutex); 17599 } 17600 } 17601 if (startstop == 1) { 17602 if (sata_event_thread == NULL) { 17603 sata_event_thread = thread_create(NULL, 0, 17604 (void (*)())sata_event_daemon, 17605 &sata_hba_list, 0, &p0, TS_RUN, minclsyspri); 17606 } 17607 sata_event_thread_starting = 0; 17608 mutex_exit(&sata_event_mutex); 17609 return; 17610 } 17611 17612 /* 17613 * If we got here, thread may need to be terminated 17614 */ 17615 if (sata_event_thread != NULL) { 17616 int i; 17617 /* Signal event thread to go away */ 17618 sata_event_thread_terminating = 1; 17619 sata_event_thread_terminate = 1; 17620 cv_signal(&sata_event_cv); 17621 /* 17622 * Wait til daemon terminates. 17623 */ 17624 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 17625 while (sata_event_thread_terminate == 1) { 17626 mutex_exit(&sata_event_mutex); 17627 if (i-- <= 0) { 17628 /* Daemon did not go away !!! */ 17629 #ifdef SATA_DEBUG 17630 cmn_err(CE_WARN, "sata_event_thread_control: " 17631 "cannot terminate event daemon thread"); 17632 #endif 17633 mutex_enter(&sata_event_mutex); 17634 break; 17635 } 17636 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 17637 mutex_enter(&sata_event_mutex); 17638 } 17639 sata_event_thread_terminating = 0; 17640 } 17641 ASSERT(sata_event_thread_terminating == 0); 17642 ASSERT(sata_event_thread_starting == 0); 17643 mutex_exit(&sata_event_mutex); 17644 } 17645 17646 17647 /* 17648 * SATA HBA event notification function. 17649 * Events reported by SATA HBA drivers per HBA instance relate to a change in 17650 * a port and/or device state or a controller itself. 17651 * Events for different addresses/addr types cannot be combined. 17652 * A warning message is generated for each event type. 17653 * Events are not processed by this function, so only the 17654 * event flag(s)is set for an affected entity and the event thread is 17655 * waken up. Event daemon thread processes all events. 17656 * 17657 * NOTE: Since more than one event may be reported at the same time, one 17658 * cannot determine a sequence of events when opposite event are reported, eg. 17659 * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing 17660 * is taking precedence over reported events, i.e. may cause ignoring some 17661 * events. 17662 */ 17663 #define SATA_EVENT_MAX_MSG_LENGTH 79 17664 17665 void 17666 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event) 17667 { 17668 sata_hba_inst_t *sata_hba_inst = NULL; 17669 sata_address_t *saddr; 17670 sata_pmult_info_t *pmultinfo; 17671 sata_drive_info_t *sdinfo; 17672 sata_port_stats_t *pstats; 17673 sata_cport_info_t *cportinfo; 17674 sata_pmport_info_t *pmportinfo; 17675 int cport, pmport; 17676 char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1]; 17677 char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1]; 17678 char *lcp; 17679 static char *err_msg_evnt_1 = 17680 "sata_hba_event_notify: invalid port event 0x%x "; 17681 static char *err_msg_evnt_2 = 17682 "sata_hba_event_notify: invalid device event 0x%x "; 17683 int linkevent; 17684 17685 /* 17686 * There is a possibility that an event will be generated on HBA 17687 * that has not completed attachment or is detaching. We still want 17688 * to process events until HBA is detached. 17689 */ 17690 mutex_enter(&sata_mutex); 17691 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 17692 sata_hba_inst = sata_hba_inst->satahba_next) { 17693 if (SATA_DIP(sata_hba_inst) == dip) 17694 if (sata_hba_inst->satahba_attached == 1) 17695 break; 17696 } 17697 mutex_exit(&sata_mutex); 17698 if (sata_hba_inst == NULL) 17699 /* HBA not attached */ 17700 return; 17701 17702 ASSERT(sata_device != NULL); 17703 17704 /* 17705 * Validate address before - do not proceed with invalid address. 17706 */ 17707 saddr = &sata_device->satadev_addr; 17708 if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst)) 17709 return; 17710 17711 cport = saddr->cport; 17712 pmport = saddr->pmport; 17713 17714 buf1[0] = buf2[0] = '\0'; 17715 17716 /* 17717 * If event relates to port or device, check port state. 17718 * Port has to be initialized, or we cannot accept an event. 17719 */ 17720 if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT | 17721 SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT | SATA_ADDR_PMULT)) != 0) { 17722 mutex_enter(&sata_hba_inst->satahba_mutex); 17723 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 17724 mutex_exit(&sata_hba_inst->satahba_mutex); 17725 if (cportinfo == NULL || cportinfo->cport_state == 0) 17726 return; 17727 } 17728 17729 if ((saddr->qual & (SATA_ADDR_PMULT | SATA_ADDR_PMPORT | 17730 SATA_ADDR_DPMPORT)) != 0) { 17731 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 17732 SATA_LOG_D((sata_hba_inst, CE_WARN, 17733 "sata_hba_event_notify: Non-pmult device (0x%x)" 17734 "is attached to port %d, ignore pmult/pmport " 17735 "event 0x%x", cportinfo->cport_dev_type, 17736 cport, event)); 17737 return; 17738 } 17739 17740 mutex_enter(&cportinfo->cport_mutex); 17741 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 17742 mutex_exit(&cportinfo->cport_mutex); 17743 17744 /* 17745 * The daemon might be processing attachment of port 17746 * multiplier, in that case we should ignore events on its 17747 * sub-devices. 17748 * 17749 * NOTE: Only pmult_state is checked in sata_hba_event_notify. 17750 * The pmport_state is checked by sata daemon. 17751 */ 17752 if (pmultinfo == NULL || 17753 pmultinfo->pmult_state == SATA_STATE_UNKNOWN) { 17754 SATA_LOG_D((sata_hba_inst, CE_WARN, 17755 "sata_hba_event_notify: pmult is not" 17756 "available at port %d:%d, ignore event 0x%x", 17757 cport, pmport, event)); 17758 return; 17759 } 17760 } 17761 17762 if ((saddr->qual & 17763 (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) != 0) { 17764 17765 mutex_enter(&cportinfo->cport_mutex); 17766 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) { 17767 SATA_LOG_D((sata_hba_inst, CE_WARN, 17768 "sata_hba_event_notify: invalid/" 17769 "un-implemented port %d:%d (%d ports), " 17770 "ignore event 0x%x", cport, pmport, 17771 SATA_NUM_PMPORTS(sata_hba_inst, cport), event)); 17772 mutex_exit(&cportinfo->cport_mutex); 17773 return; 17774 } 17775 mutex_exit(&cportinfo->cport_mutex); 17776 17777 mutex_enter(&sata_hba_inst->satahba_mutex); 17778 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 17779 cport, pmport); 17780 mutex_exit(&sata_hba_inst->satahba_mutex); 17781 17782 /* pmport is implemented/valid? */ 17783 if (pmportinfo == NULL) { 17784 SATA_LOG_D((sata_hba_inst, CE_WARN, 17785 "sata_hba_event_notify: invalid/" 17786 "un-implemented port %d:%d, ignore " 17787 "event 0x%x", cport, pmport, event)); 17788 return; 17789 } 17790 } 17791 17792 /* 17793 * Events refer to devices, ports and controllers - each has 17794 * unique address. Events for different addresses cannot be combined. 17795 */ 17796 if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) { 17797 17798 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17799 17800 /* qualify this event(s) */ 17801 if ((event & SATA_EVNT_PORT_EVENTS) == 0) { 17802 /* Invalid event for the device port */ 17803 (void) sprintf(buf2, err_msg_evnt_1, 17804 event & SATA_EVNT_PORT_EVENTS); 17805 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17806 goto event_info; 17807 } 17808 if (saddr->qual == SATA_ADDR_CPORT) { 17809 /* Controller's device port event */ 17810 17811 (SATA_CPORT_INFO(sata_hba_inst, cport))-> 17812 cport_event_flags |= 17813 event & SATA_EVNT_PORT_EVENTS; 17814 pstats = 17815 &(SATA_CPORT_INFO(sata_hba_inst, cport))-> 17816 cport_stats; 17817 } else { 17818 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17819 mutex_enter(&pmportinfo->pmport_mutex); 17820 /* Port multiplier's device port event */ 17821 (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 17822 pmport_event_flags |= 17823 event & SATA_EVNT_PORT_EVENTS; 17824 pstats = 17825 &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 17826 pmport_stats; 17827 mutex_exit(&pmportinfo->pmport_mutex); 17828 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17829 } 17830 17831 /* 17832 * Add to statistics and log the message. We have to do it 17833 * here rather than in the event daemon, because there may be 17834 * multiple events occuring before they are processed. 17835 */ 17836 linkevent = event & 17837 (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED); 17838 if (linkevent) { 17839 if (linkevent == (SATA_EVNT_LINK_LOST | 17840 SATA_EVNT_LINK_ESTABLISHED)) { 17841 /* This is likely event combination */ 17842 (void) strlcat(buf1, "link lost/established, ", 17843 SATA_EVENT_MAX_MSG_LENGTH); 17844 17845 if (pstats->link_lost < 0xffffffffffffffffULL) 17846 pstats->link_lost++; 17847 if (pstats->link_established < 17848 0xffffffffffffffffULL) 17849 pstats->link_established++; 17850 linkevent = 0; 17851 } else if (linkevent & SATA_EVNT_LINK_LOST) { 17852 (void) strlcat(buf1, "link lost, ", 17853 SATA_EVENT_MAX_MSG_LENGTH); 17854 17855 if (pstats->link_lost < 0xffffffffffffffffULL) 17856 pstats->link_lost++; 17857 } else { 17858 (void) strlcat(buf1, "link established, ", 17859 SATA_EVENT_MAX_MSG_LENGTH); 17860 if (pstats->link_established < 17861 0xffffffffffffffffULL) 17862 pstats->link_established++; 17863 } 17864 } 17865 if (event & SATA_EVNT_DEVICE_ATTACHED) { 17866 (void) strlcat(buf1, "device attached, ", 17867 SATA_EVENT_MAX_MSG_LENGTH); 17868 if (pstats->device_attached < 0xffffffffffffffffULL) 17869 pstats->device_attached++; 17870 } 17871 if (event & SATA_EVNT_DEVICE_DETACHED) { 17872 (void) strlcat(buf1, "device detached, ", 17873 SATA_EVENT_MAX_MSG_LENGTH); 17874 if (pstats->device_detached < 0xffffffffffffffffULL) 17875 pstats->device_detached++; 17876 } 17877 if (event & SATA_EVNT_PWR_LEVEL_CHANGED) { 17878 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 17879 "port %d power level changed", cport); 17880 if (pstats->port_pwr_changed < 0xffffffffffffffffULL) 17881 pstats->port_pwr_changed++; 17882 } 17883 17884 if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) { 17885 /* There should be no other events for this address */ 17886 (void) sprintf(buf2, err_msg_evnt_1, 17887 event & ~SATA_EVNT_PORT_EVENTS); 17888 } 17889 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17890 17891 } else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) { 17892 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17893 17894 /* qualify this event */ 17895 if ((event & SATA_EVNT_DEVICE_RESET) == 0) { 17896 /* Invalid event for a device */ 17897 (void) sprintf(buf2, err_msg_evnt_2, 17898 event & SATA_EVNT_DEVICE_RESET); 17899 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17900 goto event_info; 17901 } 17902 /* drive event */ 17903 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 17904 if (sdinfo != NULL) { 17905 if (event & SATA_EVNT_DEVICE_RESET) { 17906 (void) strlcat(buf1, "device reset, ", 17907 SATA_EVENT_MAX_MSG_LENGTH); 17908 if (sdinfo->satadrv_stats.drive_reset < 17909 0xffffffffffffffffULL) 17910 sdinfo->satadrv_stats.drive_reset++; 17911 sdinfo->satadrv_event_flags |= 17912 SATA_EVNT_DEVICE_RESET; 17913 } 17914 } 17915 if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) { 17916 /* Invalid event for a device */ 17917 (void) sprintf(buf2, err_msg_evnt_2, 17918 event & ~SATA_EVNT_DRIVE_EVENTS); 17919 } 17920 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17921 } else if (saddr->qual == SATA_ADDR_PMULT) { 17922 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17923 17924 /* qualify this event */ 17925 if ((event & (SATA_EVNT_DEVICE_RESET | 17926 SATA_EVNT_PMULT_LINK_CHANGED)) == 0) { 17927 /* Invalid event for a port multiplier */ 17928 (void) sprintf(buf2, err_msg_evnt_2, 17929 event & SATA_EVNT_DEVICE_RESET); 17930 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17931 goto event_info; 17932 } 17933 17934 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 17935 17936 if (event & SATA_EVNT_DEVICE_RESET) { 17937 17938 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 17939 "[Reset] port-mult on cport %d", cport); 17940 pmultinfo->pmult_event_flags |= 17941 SATA_EVNT_DEVICE_RESET; 17942 (void) strlcat(buf1, "pmult reset, ", 17943 SATA_EVENT_MAX_MSG_LENGTH); 17944 } 17945 17946 if (event & SATA_EVNT_PMULT_LINK_CHANGED) { 17947 17948 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 17949 "pmult link changed on cport %d", cport); 17950 pmultinfo->pmult_event_flags |= 17951 SATA_EVNT_PMULT_LINK_CHANGED; 17952 (void) strlcat(buf1, "pmult link changed, ", 17953 SATA_EVENT_MAX_MSG_LENGTH); 17954 } 17955 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17956 17957 } else { 17958 if (saddr->qual != SATA_ADDR_NULL) { 17959 /* Wrong address qualifier */ 17960 SATA_LOG_D((sata_hba_inst, CE_WARN, 17961 "sata_hba_event_notify: invalid address 0x%x", 17962 *(uint32_t *)saddr)); 17963 return; 17964 } 17965 if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 || 17966 (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) { 17967 /* Invalid event for the controller */ 17968 SATA_LOG_D((sata_hba_inst, CE_WARN, 17969 "sata_hba_event_notify: invalid event 0x%x for " 17970 "controller", 17971 event & SATA_EVNT_CONTROLLER_EVENTS)); 17972 return; 17973 } 17974 buf1[0] = '\0'; 17975 /* This may be a frequent and not interesting event */ 17976 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 17977 "controller power level changed\n", NULL); 17978 17979 mutex_enter(&sata_hba_inst->satahba_mutex); 17980 if (sata_hba_inst->satahba_stats.ctrl_pwr_change < 17981 0xffffffffffffffffULL) 17982 sata_hba_inst->satahba_stats.ctrl_pwr_change++; 17983 17984 sata_hba_inst->satahba_event_flags |= 17985 SATA_EVNT_PWR_LEVEL_CHANGED; 17986 mutex_exit(&sata_hba_inst->satahba_mutex); 17987 } 17988 /* 17989 * If we got here, there is something to do with this HBA 17990 * instance. 17991 */ 17992 mutex_enter(&sata_hba_inst->satahba_mutex); 17993 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 17994 mutex_exit(&sata_hba_inst->satahba_mutex); 17995 mutex_enter(&sata_mutex); 17996 sata_event_pending |= SATA_EVNT_MAIN; /* global event indicator */ 17997 mutex_exit(&sata_mutex); 17998 17999 /* Tickle event thread */ 18000 mutex_enter(&sata_event_mutex); 18001 if (sata_event_thread_active == 0) 18002 cv_signal(&sata_event_cv); 18003 mutex_exit(&sata_event_mutex); 18004 18005 event_info: 18006 if (buf1[0] != '\0') { 18007 lcp = strrchr(buf1, ','); 18008 if (lcp != NULL) 18009 *lcp = '\0'; 18010 } 18011 if (saddr->qual == SATA_ADDR_CPORT || 18012 saddr->qual == SATA_ADDR_DCPORT) { 18013 if (buf1[0] != '\0') { 18014 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 18015 cport, buf1); 18016 } 18017 if (buf2[0] != '\0') { 18018 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 18019 cport, buf2); 18020 } 18021 } else if (saddr->qual == SATA_ADDR_PMPORT || 18022 saddr->qual == SATA_ADDR_DPMPORT) { 18023 if (buf1[0] != '\0') { 18024 sata_log(sata_hba_inst, CE_NOTE, 18025 "port %d pmport %d: %s\n", cport, pmport, buf1); 18026 } 18027 if (buf2[0] != '\0') { 18028 sata_log(sata_hba_inst, CE_NOTE, 18029 "port %d pmport %d: %s\n", cport, pmport, buf2); 18030 } 18031 } 18032 } 18033 18034 18035 /* 18036 * Event processing thread. 18037 * Arg is a pointer to the sata_hba_list pointer. 18038 * It is not really needed, because sata_hba_list is global and static 18039 */ 18040 static void 18041 sata_event_daemon(void *arg) 18042 { 18043 #ifndef __lock_lint 18044 _NOTE(ARGUNUSED(arg)) 18045 #endif 18046 sata_hba_inst_t *sata_hba_inst; 18047 clock_t delta; 18048 18049 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 18050 "SATA event daemon started\n", NULL); 18051 loop: 18052 /* 18053 * Process events here. Walk through all registered HBAs 18054 */ 18055 mutex_enter(&sata_mutex); 18056 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 18057 sata_hba_inst = sata_hba_inst->satahba_next) { 18058 ASSERT(sata_hba_inst != NULL); 18059 mutex_enter(&sata_hba_inst->satahba_mutex); 18060 if (sata_hba_inst->satahba_attached == 0 || 18061 (sata_hba_inst->satahba_event_flags & 18062 SATA_EVNT_SKIP) != 0) { 18063 mutex_exit(&sata_hba_inst->satahba_mutex); 18064 continue; 18065 } 18066 if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) { 18067 sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP; 18068 mutex_exit(&sata_hba_inst->satahba_mutex); 18069 mutex_exit(&sata_mutex); 18070 /* Got the controller with pending event */ 18071 sata_process_controller_events(sata_hba_inst); 18072 /* 18073 * Since global mutex was released, there is a 18074 * possibility that HBA list has changed, so start 18075 * over from the top. Just processed controller 18076 * will be passed-over because of the SKIP flag. 18077 */ 18078 goto loop; 18079 } 18080 mutex_exit(&sata_hba_inst->satahba_mutex); 18081 } 18082 /* Clear SKIP flag in all controllers */ 18083 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 18084 sata_hba_inst = sata_hba_inst->satahba_next) { 18085 mutex_enter(&sata_hba_inst->satahba_mutex); 18086 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP; 18087 mutex_exit(&sata_hba_inst->satahba_mutex); 18088 } 18089 mutex_exit(&sata_mutex); 18090 18091 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 18092 "SATA EVENT DAEMON suspending itself", NULL); 18093 18094 #ifdef SATA_DEBUG 18095 if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) { 18096 sata_log(sata_hba_inst, CE_WARN, 18097 "SATA EVENTS PROCESSING DISABLED\n"); 18098 thread_exit(); /* Daemon will not run again */ 18099 } 18100 #endif 18101 mutex_enter(&sata_event_mutex); 18102 sata_event_thread_active = 0; 18103 mutex_exit(&sata_event_mutex); 18104 /* 18105 * Go to sleep/suspend itself and wake up either because new event or 18106 * wait timeout. Exit if there is a termination request (driver 18107 * unload). 18108 */ 18109 delta = drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME); 18110 do { 18111 mutex_enter(&sata_event_mutex); 18112 (void) cv_reltimedwait(&sata_event_cv, &sata_event_mutex, 18113 delta, TR_CLOCK_TICK); 18114 18115 if (sata_event_thread_active != 0) { 18116 mutex_exit(&sata_event_mutex); 18117 continue; 18118 } 18119 18120 /* Check if it is time to go away */ 18121 if (sata_event_thread_terminate == 1) { 18122 /* 18123 * It is up to the thread setting above flag to make 18124 * sure that this thread is not killed prematurely. 18125 */ 18126 sata_event_thread_terminate = 0; 18127 sata_event_thread = NULL; 18128 mutex_exit(&sata_event_mutex); 18129 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 18130 "SATA_EVENT_DAEMON_TERMINATING", NULL); 18131 thread_exit(); { _NOTE(NOT_REACHED) } 18132 } 18133 mutex_exit(&sata_event_mutex); 18134 } while (!(sata_event_pending & SATA_EVNT_MAIN)); 18135 18136 mutex_enter(&sata_event_mutex); 18137 sata_event_thread_active = 1; 18138 mutex_exit(&sata_event_mutex); 18139 18140 mutex_enter(&sata_mutex); 18141 sata_event_pending &= ~SATA_EVNT_MAIN; 18142 mutex_exit(&sata_mutex); 18143 18144 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 18145 "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL); 18146 18147 goto loop; 18148 } 18149 18150 /* 18151 * Specific HBA instance event processing. 18152 * 18153 * NOTE: At the moment, device event processing is limited to hard disks 18154 * only. 18155 * Port multiplier is supported now. 18156 */ 18157 static void 18158 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst) 18159 { 18160 int ncport; 18161 uint32_t event_flags; 18162 sata_address_t *saddr; 18163 sata_cport_info_t *cportinfo; 18164 sata_pmult_info_t *pmultinfo; 18165 18166 SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst, 18167 "Processing controller %d event(s)", 18168 ddi_get_instance(SATA_DIP(sata_hba_inst))); 18169 18170 mutex_enter(&sata_hba_inst->satahba_mutex); 18171 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN; 18172 event_flags = sata_hba_inst->satahba_event_flags; 18173 mutex_exit(&sata_hba_inst->satahba_mutex); 18174 /* 18175 * Process controller power change first 18176 * HERE 18177 */ 18178 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) 18179 sata_process_cntrl_pwr_level_change(sata_hba_inst); 18180 18181 /* 18182 * Search through ports/devices to identify affected port/device. 18183 * We may have to process events for more than one port/device. 18184 */ 18185 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 18186 /* 18187 * Not all ports may be processed in attach by the time we 18188 * get an event. Check if port info is initialized. 18189 */ 18190 mutex_enter(&sata_hba_inst->satahba_mutex); 18191 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 18192 mutex_exit(&sata_hba_inst->satahba_mutex); 18193 if (cportinfo == NULL || cportinfo->cport_state == 0) 18194 continue; 18195 18196 /* We have initialized controller port info */ 18197 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 18198 event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 18199 cport_event_flags; 18200 /* Check if port was locked by IOCTL processing */ 18201 if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) { 18202 /* 18203 * We ignore port events because port is busy 18204 * with AP control processing. Set again 18205 * controller and main event flag, so that 18206 * events may be processed by the next daemon 18207 * run. 18208 */ 18209 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 18210 mutex_enter(&sata_hba_inst->satahba_mutex); 18211 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 18212 mutex_exit(&sata_hba_inst->satahba_mutex); 18213 mutex_enter(&sata_mutex); 18214 sata_event_pending |= SATA_EVNT_MAIN; 18215 mutex_exit(&sata_mutex); 18216 SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst, 18217 "Event processing postponed until " 18218 "AP control processing completes", 18219 NULL); 18220 /* Check other ports */ 18221 continue; 18222 } else { 18223 /* 18224 * Set BSY flag so that AP control would not 18225 * interfere with events processing for 18226 * this port. 18227 */ 18228 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 18229 cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY; 18230 } 18231 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 18232 18233 saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr; 18234 18235 if ((event_flags & 18236 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) { 18237 /* 18238 * Got port event. 18239 * We need some hierarchy of event processing as they 18240 * are affecting each other: 18241 * 1. port failed 18242 * 2. device detached/attached 18243 * 3. link events - link events may trigger device 18244 * detached or device attached events in some 18245 * circumstances. 18246 * 4. port power level changed 18247 */ 18248 if (event_flags & SATA_EVNT_PORT_FAILED) { 18249 sata_process_port_failed_event(sata_hba_inst, 18250 saddr); 18251 } 18252 if (event_flags & SATA_EVNT_DEVICE_DETACHED) { 18253 sata_process_device_detached(sata_hba_inst, 18254 saddr); 18255 } 18256 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) { 18257 sata_process_device_attached(sata_hba_inst, 18258 saddr); 18259 } 18260 if (event_flags & 18261 (SATA_EVNT_LINK_ESTABLISHED | 18262 SATA_EVNT_LINK_LOST)) { 18263 sata_process_port_link_events(sata_hba_inst, 18264 saddr); 18265 } 18266 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) { 18267 sata_process_port_pwr_change(sata_hba_inst, 18268 saddr); 18269 } 18270 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) { 18271 sata_process_target_node_cleanup( 18272 sata_hba_inst, saddr); 18273 } 18274 if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) { 18275 sata_process_device_autoonline( 18276 sata_hba_inst, saddr); 18277 } 18278 } 18279 18280 18281 /* 18282 * Scan port multiplier and all its sub-ports event flags. 18283 * The events are marked by 18284 * (1) sata_pmult_info.pmult_event_flags 18285 * (2) sata_pmport_info.pmport_event_flags 18286 */ 18287 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 18288 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 18289 /* 18290 * There should be another extra check: this 18291 * port multiplier still exists? 18292 */ 18293 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, 18294 ncport); 18295 18296 if (pmultinfo != NULL) { 18297 mutex_exit(&(SATA_CPORT_MUTEX( 18298 sata_hba_inst, ncport))); 18299 sata_process_pmult_events( 18300 sata_hba_inst, ncport); 18301 mutex_enter(&(SATA_CPORT_MUTEX( 18302 sata_hba_inst, ncport))); 18303 } else { 18304 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 18305 "Port-multiplier is gone. " 18306 "Ignore all sub-device events " 18307 "at port %d.", ncport); 18308 } 18309 } 18310 18311 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) != 18312 SATA_DTYPE_NONE) && 18313 (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) { 18314 if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)-> 18315 satadrv_event_flags & 18316 (SATA_EVNT_DEVICE_RESET | 18317 SATA_EVNT_INPROC_DEVICE_RESET)) { 18318 /* Have device event */ 18319 sata_process_device_reset(sata_hba_inst, 18320 saddr); 18321 } 18322 } 18323 /* Release PORT_BUSY flag */ 18324 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 18325 cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY; 18326 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 18327 18328 } /* End of loop through the controller SATA ports */ 18329 } 18330 18331 /* 18332 * Specific port multiplier instance event processing. At the moment, device 18333 * event processing is limited to link/attach event only. 18334 * 18335 * NOTE: power management event is not supported yet. 18336 */ 18337 static void 18338 sata_process_pmult_events(sata_hba_inst_t *sata_hba_inst, uint8_t cport) 18339 { 18340 sata_cport_info_t *cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 18341 sata_pmult_info_t *pmultinfo; 18342 sata_pmport_info_t *pmportinfo; 18343 sata_address_t *saddr; 18344 sata_device_t sata_device; 18345 uint32_t event_flags; 18346 int npmport; 18347 int rval; 18348 18349 SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst, 18350 "Processing pmult event(s) on cport %d of controller %d", 18351 cport, ddi_get_instance(SATA_DIP(sata_hba_inst))); 18352 18353 /* First process events on port multiplier */ 18354 mutex_enter(&cportinfo->cport_mutex); 18355 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 18356 event_flags = pmultinfo->pmult_event_flags; 18357 18358 /* 18359 * Reset event (of port multiplier) has higher priority because the 18360 * port multiplier itself might be failed or removed after reset. 18361 */ 18362 if (event_flags & SATA_EVNT_DEVICE_RESET) { 18363 /* 18364 * The status of the sub-links are uncertain, 18365 * so mark all sub-ports as RESET 18366 */ 18367 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 18368 sata_hba_inst, cport); npmport ++) { 18369 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 18370 cport, npmport); 18371 if (pmportinfo == NULL) { 18372 /* That's weird. */ 18373 SATA_LOG_D((sata_hba_inst, CE_WARN, 18374 "sata_hba_event_notify: " 18375 "invalid/un-implemented " 18376 "port %d:%d (%d ports), ", 18377 cport, npmport, SATA_NUM_PMPORTS( 18378 sata_hba_inst, cport))); 18379 continue; 18380 } 18381 18382 mutex_enter(&pmportinfo->pmport_mutex); 18383 18384 /* Mark all pmport to unknow state. */ 18385 pmportinfo->pmport_state = SATA_STATE_UNKNOWN; 18386 /* Mark all pmports with link events. */ 18387 pmportinfo->pmport_event_flags = 18388 (SATA_EVNT_LINK_ESTABLISHED|SATA_EVNT_LINK_LOST); 18389 mutex_exit(&pmportinfo->pmport_mutex); 18390 } 18391 18392 } else if (event_flags & SATA_EVNT_PMULT_LINK_CHANGED) { 18393 /* 18394 * We need probe the port multiplier to know what has 18395 * happened. 18396 */ 18397 bzero(&sata_device, sizeof (sata_device_t)); 18398 sata_device.satadev_rev = SATA_DEVICE_REV; 18399 sata_device.satadev_addr.cport = cport; 18400 sata_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT; 18401 sata_device.satadev_addr.qual = SATA_ADDR_PMULT; 18402 18403 mutex_exit(&cportinfo->cport_mutex); 18404 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18405 (SATA_DIP(sata_hba_inst), &sata_device); 18406 mutex_enter(&cportinfo->cport_mutex); 18407 if (rval != SATA_SUCCESS) { 18408 /* Something went wrong? Fail the port */ 18409 cportinfo->cport_state = SATA_PSTATE_FAILED; 18410 mutex_exit(&cportinfo->cport_mutex); 18411 SATA_LOG_D((sata_hba_inst, CE_WARN, 18412 "SATA port %d probing failed", cport)); 18413 18414 /* PMult structure must be released. */ 18415 sata_free_pmult(sata_hba_inst, &sata_device); 18416 return; 18417 } 18418 18419 sata_update_port_info(sata_hba_inst, &sata_device); 18420 18421 /* 18422 * Sanity check - Port is active? Is the link active? 18423 * The device is still a port multiplier? 18424 */ 18425 if ((cportinfo->cport_state & 18426 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 18427 ((cportinfo->cport_scr.sstatus & 18428 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) || 18429 (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) { 18430 mutex_exit(&cportinfo->cport_mutex); 18431 18432 /* PMult structure must be released. */ 18433 sata_free_pmult(sata_hba_inst, &sata_device); 18434 return; 18435 } 18436 18437 /* Probed succeed, set port ready. */ 18438 cportinfo->cport_state |= 18439 SATA_STATE_PROBED | SATA_STATE_READY; 18440 } 18441 18442 /* Release port multiplier event flags. */ 18443 pmultinfo->pmult_event_flags &= 18444 ~(SATA_EVNT_DEVICE_RESET|SATA_EVNT_PMULT_LINK_CHANGED); 18445 mutex_exit(&cportinfo->cport_mutex); 18446 18447 /* 18448 * Check all sub-links. 18449 */ 18450 for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, cport); 18451 npmport ++) { 18452 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport); 18453 mutex_enter(&pmportinfo->pmport_mutex); 18454 event_flags = pmportinfo->pmport_event_flags; 18455 mutex_exit(&pmportinfo->pmport_mutex); 18456 saddr = &pmportinfo->pmport_addr; 18457 18458 if ((event_flags & 18459 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) { 18460 /* 18461 * Got port multiplier port event. 18462 * We need some hierarchy of event processing as they 18463 * are affecting each other: 18464 * 1. device detached/attached 18465 * 2. link events - link events may trigger device 18466 * detached or device attached events in some 18467 * circumstances. 18468 */ 18469 if (event_flags & SATA_EVNT_DEVICE_DETACHED) { 18470 sata_process_pmdevice_detached(sata_hba_inst, 18471 saddr); 18472 } 18473 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) { 18474 sata_process_pmdevice_attached(sata_hba_inst, 18475 saddr); 18476 } 18477 if (event_flags & SATA_EVNT_LINK_ESTABLISHED || 18478 event_flags & SATA_EVNT_LINK_LOST) { 18479 sata_process_pmport_link_events(sata_hba_inst, 18480 saddr); 18481 } 18482 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) { 18483 sata_process_target_node_cleanup( 18484 sata_hba_inst, saddr); 18485 } 18486 } 18487 18488 /* Checking drive event(s). */ 18489 mutex_enter(&pmportinfo->pmport_mutex); 18490 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE && 18491 pmportinfo->pmport_sata_drive != NULL) { 18492 event_flags = pmportinfo->pmport_sata_drive-> 18493 satadrv_event_flags; 18494 if (event_flags & (SATA_EVNT_DEVICE_RESET | 18495 SATA_EVNT_INPROC_DEVICE_RESET)) { 18496 18497 /* Have device event */ 18498 sata_process_pmdevice_reset(sata_hba_inst, 18499 saddr); 18500 } 18501 } 18502 mutex_exit(&pmportinfo->pmport_mutex); 18503 18504 /* Release PORT_BUSY flag */ 18505 mutex_enter(&cportinfo->cport_mutex); 18506 cportinfo->cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY; 18507 mutex_exit(&cportinfo->cport_mutex); 18508 } 18509 18510 SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst, 18511 "[DONE] pmult event(s) on cport %d of controller %d", 18512 cport, ddi_get_instance(SATA_DIP(sata_hba_inst))); 18513 } 18514 18515 /* 18516 * Process HBA power level change reported by HBA driver. 18517 * Not implemented at this time - event is ignored. 18518 */ 18519 static void 18520 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst) 18521 { 18522 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18523 "Processing controller power level change", NULL); 18524 18525 /* Ignoring it for now */ 18526 mutex_enter(&sata_hba_inst->satahba_mutex); 18527 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 18528 mutex_exit(&sata_hba_inst->satahba_mutex); 18529 } 18530 18531 /* 18532 * Process port power level change reported by HBA driver. 18533 * Not implemented at this time - event is ignored. 18534 */ 18535 static void 18536 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst, 18537 sata_address_t *saddr) 18538 { 18539 sata_cport_info_t *cportinfo; 18540 18541 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18542 "Processing port power level change", NULL); 18543 18544 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 18545 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18546 /* Reset event flag */ 18547 cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 18548 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18549 } 18550 18551 /* 18552 * Process port failure reported by HBA driver. 18553 * cports support only - no pmports. 18554 */ 18555 static void 18556 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst, 18557 sata_address_t *saddr) 18558 { 18559 sata_cport_info_t *cportinfo; 18560 18561 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 18562 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18563 /* Reset event flag first */ 18564 cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED; 18565 /* If the port is in SHUTDOWN or FAILED state, ignore this event. */ 18566 if ((cportinfo->cport_state & 18567 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) { 18568 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18569 cport_mutex); 18570 return; 18571 } 18572 /* Fail the port */ 18573 cportinfo->cport_state = SATA_PSTATE_FAILED; 18574 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18575 sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport); 18576 } 18577 18578 /* 18579 * Device Reset Event processing. 18580 * The sequence is managed by 3 stage flags: 18581 * - reset event reported, 18582 * - reset event being processed, 18583 * - request to clear device reset state. 18584 * 18585 * NOTE: This function has to be entered with cport mutex held. It exits with 18586 * mutex held as well, but can release mutex during the processing. 18587 */ 18588 static void 18589 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst, 18590 sata_address_t *saddr) 18591 { 18592 sata_drive_info_t old_sdinfo; /* local copy of the drive info */ 18593 sata_drive_info_t *sdinfo; 18594 sata_cport_info_t *cportinfo; 18595 sata_device_t sata_device; 18596 int rval_probe, rval_set; 18597 18598 /* We only care about host sata cport for now */ 18599 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 18600 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 18601 /* 18602 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED 18603 * state, ignore reset event. 18604 */ 18605 if (((cportinfo->cport_state & 18606 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) || 18607 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 18608 sdinfo->satadrv_event_flags &= 18609 ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET); 18610 return; 18611 } 18612 18613 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) == 18614 SATA_DTYPE_PMULT)) { 18615 /* 18616 * Should not happened: this is already handled in 18617 * sata_hba_event_notify() 18618 */ 18619 mutex_exit(&cportinfo->cport_mutex); 18620 goto done; 18621 } 18622 18623 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) & 18624 SATA_VALID_DEV_TYPE) == 0) { 18625 /* 18626 * This should not happen - coding error. 18627 * But we can recover, so do not panic, just clean up 18628 * and if in debug mode, log the message. 18629 */ 18630 #ifdef SATA_DEBUG 18631 sata_log(sata_hba_inst, CE_WARN, 18632 "sata_process_device_reset: " 18633 "Invalid device type with sdinfo!", NULL); 18634 #endif 18635 sdinfo->satadrv_event_flags = 0; 18636 return; 18637 } 18638 18639 #ifdef SATA_DEBUG 18640 if ((sdinfo->satadrv_event_flags & 18641 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) { 18642 /* Nothing to do */ 18643 /* Something is weird - why we are processing dev reset? */ 18644 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18645 "No device reset event!!!!", NULL); 18646 18647 return; 18648 } 18649 if ((sdinfo->satadrv_event_flags & 18650 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 18651 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 18652 /* Something is weird - new device reset event */ 18653 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18654 "Overlapping device reset events!", NULL); 18655 } 18656 #endif 18657 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18658 "Processing port %d device reset", saddr->cport); 18659 18660 /* Clear event flag */ 18661 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET; 18662 18663 /* It seems that we always need to check the port state first */ 18664 sata_device.satadev_rev = SATA_DEVICE_REV; 18665 sata_device.satadev_addr = *saddr; 18666 /* 18667 * We have to exit mutex, because the HBA probe port function may 18668 * block on its own mutex. 18669 */ 18670 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18671 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18672 (SATA_DIP(sata_hba_inst), &sata_device); 18673 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18674 sata_update_port_info(sata_hba_inst, &sata_device); 18675 if (rval_probe != SATA_SUCCESS) { 18676 /* Something went wrong? Fail the port */ 18677 cportinfo->cport_state = SATA_PSTATE_FAILED; 18678 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 18679 if (sdinfo != NULL) 18680 sdinfo->satadrv_event_flags = 0; 18681 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18682 cport_mutex); 18683 SATA_LOG_D((sata_hba_inst, CE_WARN, 18684 "SATA port %d probing failed", 18685 saddr->cport)); 18686 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 18687 saddr->cport)->cport_mutex); 18688 return; 18689 } 18690 if ((sata_device.satadev_scr.sstatus & 18691 SATA_PORT_DEVLINK_UP_MASK) != 18692 SATA_PORT_DEVLINK_UP || 18693 sata_device.satadev_type == SATA_DTYPE_NONE) { 18694 /* 18695 * No device to process, anymore. Some other event processing 18696 * would or have already performed port info cleanup. 18697 * To be safe (HBA may need it), request clearing device 18698 * reset condition. 18699 */ 18700 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 18701 if (sdinfo != NULL) { 18702 sdinfo->satadrv_event_flags &= 18703 ~SATA_EVNT_INPROC_DEVICE_RESET; 18704 sdinfo->satadrv_event_flags |= 18705 SATA_EVNT_CLEAR_DEVICE_RESET; 18706 } 18707 return; 18708 } 18709 18710 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 18711 if (sdinfo == NULL) { 18712 return; 18713 } 18714 if ((sdinfo->satadrv_event_flags & 18715 SATA_EVNT_INPROC_DEVICE_RESET) == 0) { 18716 /* 18717 * Start tracking time for device feature restoration and 18718 * identification. Save current time (lbolt value). 18719 */ 18720 sdinfo->satadrv_reset_time = ddi_get_lbolt(); 18721 } 18722 /* Mark device reset processing as active */ 18723 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET; 18724 18725 old_sdinfo = *sdinfo; /* local copy of the drive info */ 18726 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18727 18728 rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1); 18729 18730 if (rval_set != SATA_SUCCESS) { 18731 /* 18732 * Restoring drive setting failed. 18733 * Probe the port first, to check if the port state has changed 18734 */ 18735 sata_device.satadev_rev = SATA_DEVICE_REV; 18736 sata_device.satadev_addr = *saddr; 18737 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 18738 /* probe port */ 18739 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18740 (SATA_DIP(sata_hba_inst), &sata_device); 18741 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18742 cport_mutex); 18743 if (rval_probe == SATA_SUCCESS && 18744 (sata_device.satadev_state & 18745 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 && 18746 (sata_device.satadev_scr.sstatus & 18747 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP && 18748 sata_device.satadev_type != SATA_DTYPE_NONE) { 18749 /* 18750 * We may retry this a bit later - in-process reset 18751 * condition should be already set. 18752 * Track retry time for device identification. 18753 */ 18754 if ((cportinfo->cport_dev_type & 18755 SATA_VALID_DEV_TYPE) != 0 && 18756 SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL && 18757 sdinfo->satadrv_reset_time != 0) { 18758 clock_t cur_time = ddi_get_lbolt(); 18759 /* 18760 * If the retry time limit was not 18761 * exceeded, retry. 18762 */ 18763 if ((cur_time - sdinfo->satadrv_reset_time) < 18764 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 18765 mutex_enter( 18766 &sata_hba_inst->satahba_mutex); 18767 sata_hba_inst->satahba_event_flags |= 18768 SATA_EVNT_MAIN; 18769 mutex_exit( 18770 &sata_hba_inst->satahba_mutex); 18771 mutex_enter(&sata_mutex); 18772 sata_event_pending |= SATA_EVNT_MAIN; 18773 mutex_exit(&sata_mutex); 18774 return; 18775 } 18776 if (rval_set == SATA_RETRY) { 18777 /* 18778 * Setting drive features failed, but 18779 * the drive is still accessible, 18780 * so emit a warning message before 18781 * return. 18782 */ 18783 mutex_exit(&SATA_CPORT_INFO( 18784 sata_hba_inst, 18785 saddr->cport)->cport_mutex); 18786 goto done; 18787 } 18788 } 18789 /* Fail the drive */ 18790 sdinfo->satadrv_state = SATA_DSTATE_FAILED; 18791 18792 sata_log(sata_hba_inst, CE_WARN, 18793 "SATA device at port %d - device failed", 18794 saddr->cport); 18795 18796 DTRACE_PROBE(port_failed_f); 18797 } 18798 /* 18799 * No point of retrying - device failed or some other event 18800 * processing or already did or will do port info cleanup. 18801 * To be safe (HBA may need it), 18802 * request clearing device reset condition. 18803 */ 18804 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 18805 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET; 18806 sdinfo->satadrv_reset_time = 0; 18807 return; 18808 } 18809 done: 18810 /* 18811 * If setting of drive features failed, but the drive is still 18812 * accessible, emit a warning message. 18813 */ 18814 if (rval_set == SATA_RETRY) { 18815 sata_log(sata_hba_inst, CE_WARN, 18816 "SATA device at port %d - desired setting could not be " 18817 "restored after reset. Device may not operate as expected.", 18818 saddr->cport); 18819 } 18820 /* 18821 * Raise the flag indicating that the next sata command could 18822 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device 18823 * reset is reported. 18824 */ 18825 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18826 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 18827 sdinfo->satadrv_reset_time = 0; 18828 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) { 18829 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 18830 sdinfo->satadrv_event_flags &= 18831 ~SATA_EVNT_INPROC_DEVICE_RESET; 18832 sdinfo->satadrv_event_flags |= 18833 SATA_EVNT_CLEAR_DEVICE_RESET; 18834 } 18835 } 18836 } 18837 18838 18839 /* 18840 * Port Multiplier Port Device Reset Event processing. 18841 * 18842 * NOTE: This function has to be entered with pmport mutex held. It exits with 18843 * mutex held as well, but can release mutex during the processing. 18844 */ 18845 static void 18846 sata_process_pmdevice_reset(sata_hba_inst_t *sata_hba_inst, 18847 sata_address_t *saddr) 18848 { 18849 sata_drive_info_t old_sdinfo; /* local copy of the drive info */ 18850 sata_drive_info_t *sdinfo = NULL; 18851 sata_cport_info_t *cportinfo = NULL; 18852 sata_pmport_info_t *pmportinfo = NULL; 18853 sata_pmult_info_t *pminfo = NULL; 18854 sata_device_t sata_device; 18855 uint8_t cport = saddr->cport; 18856 uint8_t pmport = saddr->pmport; 18857 int rval; 18858 18859 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18860 "Processing drive reset at port %d:%d", cport, pmport); 18861 18862 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 18863 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 18864 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport); 18865 18866 /* 18867 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED 18868 * state, ignore reset event. 18869 */ 18870 if (((cportinfo->cport_state & 18871 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) || 18872 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 18873 sdinfo->satadrv_event_flags &= 18874 ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET); 18875 return; 18876 } 18877 18878 if ((pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) == 0) { 18879 /* 18880 * This should not happen - coding error. 18881 * But we can recover, so do not panic, just clean up 18882 * and if in debug mode, log the message. 18883 */ 18884 #ifdef SATA_DEBUG 18885 sata_log(sata_hba_inst, CE_WARN, 18886 "sata_process_pmdevice_reset: " 18887 "Invalid device type with sdinfo!", NULL); 18888 #endif 18889 sdinfo->satadrv_event_flags = 0; 18890 return; 18891 } 18892 18893 #ifdef SATA_DEBUG 18894 if ((sdinfo->satadrv_event_flags & 18895 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) { 18896 /* Nothing to do */ 18897 /* Something is weird - why we are processing dev reset? */ 18898 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18899 "No device reset event!!!!", NULL); 18900 18901 return; 18902 } 18903 if ((sdinfo->satadrv_event_flags & 18904 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 18905 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 18906 /* Something is weird - new device reset event */ 18907 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18908 "Overlapping device reset events!", NULL); 18909 } 18910 #endif 18911 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18912 "Processing port %d:%d device reset", cport, pmport); 18913 18914 /* Clear event flag */ 18915 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET; 18916 18917 /* It seems that we always need to check the port state first */ 18918 sata_device.satadev_rev = SATA_DEVICE_REV; 18919 sata_device.satadev_addr = *saddr; 18920 /* 18921 * We have to exit mutex, because the HBA probe port function may 18922 * block on its own mutex. 18923 */ 18924 mutex_exit(&pmportinfo->pmport_mutex); 18925 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18926 (SATA_DIP(sata_hba_inst), &sata_device); 18927 mutex_enter(&pmportinfo->pmport_mutex); 18928 18929 sata_update_pmport_info(sata_hba_inst, &sata_device); 18930 if (rval != SATA_SUCCESS) { 18931 /* Something went wrong? Fail the port */ 18932 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 18933 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport, 18934 saddr->pmport); 18935 if (sdinfo != NULL) 18936 sdinfo->satadrv_event_flags = 0; 18937 mutex_exit(&pmportinfo->pmport_mutex); 18938 SATA_LOG_D((sata_hba_inst, CE_WARN, 18939 "SATA port %d:%d probing failed", 18940 saddr->cport, saddr->pmport)); 18941 mutex_enter(&pmportinfo->pmport_mutex); 18942 return; 18943 } 18944 if ((sata_device.satadev_scr.sstatus & 18945 SATA_PORT_DEVLINK_UP_MASK) != 18946 SATA_PORT_DEVLINK_UP || 18947 sata_device.satadev_type == SATA_DTYPE_NONE) { 18948 /* 18949 * No device to process, anymore. Some other event processing 18950 * would or have already performed port info cleanup. 18951 * To be safe (HBA may need it), request clearing device 18952 * reset condition. 18953 */ 18954 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport, 18955 saddr->pmport); 18956 if (sdinfo != NULL) { 18957 sdinfo->satadrv_event_flags &= 18958 ~SATA_EVNT_INPROC_DEVICE_RESET; 18959 /* must clear flags on cport */ 18960 pminfo = SATA_PMULT_INFO(sata_hba_inst, 18961 saddr->cport); 18962 pminfo->pmult_event_flags |= 18963 SATA_EVNT_CLEAR_DEVICE_RESET; 18964 } 18965 return; 18966 } 18967 18968 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport, 18969 saddr->pmport); 18970 if (sdinfo == NULL) { 18971 return; 18972 } 18973 if ((sdinfo->satadrv_event_flags & 18974 SATA_EVNT_INPROC_DEVICE_RESET) == 0) { 18975 /* 18976 * Start tracking time for device feature restoration and 18977 * identification. Save current time (lbolt value). 18978 */ 18979 sdinfo->satadrv_reset_time = ddi_get_lbolt(); 18980 } 18981 /* Mark device reset processing as active */ 18982 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET; 18983 18984 old_sdinfo = *sdinfo; /* local copy of the drive info */ 18985 mutex_exit(&pmportinfo->pmport_mutex); 18986 18987 if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) == 18988 SATA_FAILURE) { 18989 /* 18990 * Restoring drive setting failed. 18991 * Probe the port first, to check if the port state has changed 18992 */ 18993 sata_device.satadev_rev = SATA_DEVICE_REV; 18994 sata_device.satadev_addr = *saddr; 18995 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT; 18996 18997 /* probe port */ 18998 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18999 (SATA_DIP(sata_hba_inst), &sata_device); 19000 mutex_enter(&pmportinfo->pmport_mutex); 19001 if (rval == SATA_SUCCESS && 19002 (sata_device.satadev_state & 19003 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 && 19004 (sata_device.satadev_scr.sstatus & 19005 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP && 19006 sata_device.satadev_type != SATA_DTYPE_NONE) { 19007 /* 19008 * We may retry this a bit later - in-process reset 19009 * condition should be already set. 19010 * Track retry time for device identification. 19011 */ 19012 if ((pmportinfo->pmport_dev_type & 19013 SATA_VALID_DEV_TYPE) != 0 && 19014 SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL && 19015 sdinfo->satadrv_reset_time != 0) { 19016 clock_t cur_time = ddi_get_lbolt(); 19017 /* 19018 * If the retry time limit was not 19019 * exceeded, retry. 19020 */ 19021 if ((cur_time - sdinfo->satadrv_reset_time) < 19022 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 19023 mutex_enter( 19024 &sata_hba_inst->satahba_mutex); 19025 sata_hba_inst->satahba_event_flags |= 19026 SATA_EVNT_MAIN; 19027 mutex_exit( 19028 &sata_hba_inst->satahba_mutex); 19029 mutex_enter(&sata_mutex); 19030 sata_event_pending |= SATA_EVNT_MAIN; 19031 mutex_exit(&sata_mutex); 19032 return; 19033 } 19034 } 19035 /* Fail the drive */ 19036 sdinfo->satadrv_state = SATA_DSTATE_FAILED; 19037 19038 sata_log(sata_hba_inst, CE_WARN, 19039 "SATA device at port %d:%d - device failed", 19040 saddr->cport, saddr->pmport); 19041 } else { 19042 /* 19043 * No point of retrying - some other event processing 19044 * would or already did port info cleanup. 19045 * To be safe (HBA may need it), 19046 * request clearing device reset condition. 19047 */ 19048 sdinfo->satadrv_event_flags |= 19049 SATA_EVNT_CLEAR_DEVICE_RESET; 19050 } 19051 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET; 19052 sdinfo->satadrv_reset_time = 0; 19053 return; 19054 } 19055 /* 19056 * Raise the flag indicating that the next sata command could 19057 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device 19058 * reset is reported. 19059 */ 19060 mutex_enter(&pmportinfo->pmport_mutex); 19061 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 19062 sdinfo->satadrv_reset_time = 0; 19063 if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) { 19064 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 19065 sdinfo->satadrv_event_flags &= 19066 ~SATA_EVNT_INPROC_DEVICE_RESET; 19067 /* must clear flags on cport */ 19068 pminfo = SATA_PMULT_INFO(sata_hba_inst, 19069 saddr->cport); 19070 pminfo->pmult_event_flags |= 19071 SATA_EVNT_CLEAR_DEVICE_RESET; 19072 } 19073 } 19074 } 19075 19076 /* 19077 * Port Link Events processing. 19078 * Every link established event may involve device reset (due to 19079 * COMRESET signal, equivalent of the hard reset) so arbitrarily 19080 * set device reset event for an attached device (if any). 19081 * If the port is in SHUTDOWN or FAILED state, ignore link events. 19082 * 19083 * The link established event processing varies, depending on the state 19084 * of the target node, HBA hotplugging capabilities, state of the port. 19085 * If the link is not active, the link established event is ignored. 19086 * If HBA cannot detect device attachment and there is no target node, 19087 * the link established event triggers device attach event processing. 19088 * Else, link established event triggers device reset event processing. 19089 * 19090 * The link lost event processing varies, depending on a HBA hotplugging 19091 * capability and the state of the port (link active or not active). 19092 * If the link is active, the lost link event is ignored. 19093 * If HBA cannot detect device removal, the lost link event triggers 19094 * device detached event processing after link lost timeout. 19095 * Else, the event is ignored. 19096 * 19097 * NOTE: Port multiplier ports events are handled by 19098 * sata_process_pmport_link_events(); 19099 */ 19100 static void 19101 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst, 19102 sata_address_t *saddr) 19103 { 19104 sata_device_t sata_device; 19105 sata_cport_info_t *cportinfo; 19106 sata_drive_info_t *sdinfo; 19107 uint32_t event_flags; 19108 int rval; 19109 19110 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19111 "Processing port %d link event(s)", saddr->cport); 19112 19113 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 19114 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19115 event_flags = cportinfo->cport_event_flags; 19116 19117 /* Reset event flags first */ 19118 cportinfo->cport_event_flags &= 19119 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST); 19120 19121 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */ 19122 if ((cportinfo->cport_state & 19123 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 19124 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19125 cport_mutex); 19126 return; 19127 } 19128 19129 /* 19130 * For the sanity sake get current port state. 19131 * Set device address only. Other sata_device fields should be 19132 * set by HBA driver. 19133 */ 19134 sata_device.satadev_rev = SATA_DEVICE_REV; 19135 sata_device.satadev_addr = *saddr; 19136 /* 19137 * We have to exit mutex, because the HBA probe port function may 19138 * block on its own mutex. 19139 */ 19140 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19141 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 19142 (SATA_DIP(sata_hba_inst), &sata_device); 19143 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19144 sata_update_port_info(sata_hba_inst, &sata_device); 19145 if (rval != SATA_SUCCESS) { 19146 /* Something went wrong? Fail the port */ 19147 cportinfo->cport_state = SATA_PSTATE_FAILED; 19148 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19149 cport_mutex); 19150 SATA_LOG_D((sata_hba_inst, CE_WARN, 19151 "SATA port %d probing failed", 19152 saddr->cport)); 19153 /* 19154 * We may want to release device info structure, but 19155 * it is not necessary. 19156 */ 19157 return; 19158 } else { 19159 /* port probed successfully */ 19160 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 19161 } 19162 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) { 19163 19164 if ((sata_device.satadev_scr.sstatus & 19165 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) { 19166 /* Ignore event */ 19167 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19168 "Ignoring port %d link established event - " 19169 "link down", 19170 saddr->cport); 19171 goto linklost; 19172 } 19173 19174 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19175 "Processing port %d link established event", 19176 saddr->cport); 19177 19178 /* 19179 * For the sanity sake check if a device is attached - check 19180 * return state of a port probing. 19181 */ 19182 if (sata_device.satadev_type != SATA_DTYPE_NONE) { 19183 /* 19184 * HBA port probe indicated that there is a device 19185 * attached. Check if the framework had device info 19186 * structure attached for this device. 19187 */ 19188 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 19189 ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) != 19190 NULL); 19191 19192 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 19193 if ((sdinfo->satadrv_type & 19194 SATA_VALID_DEV_TYPE) != 0) { 19195 /* 19196 * Dev info structure is present. 19197 * If dev_type is set to known type in 19198 * the framework's drive info struct 19199 * then the device existed before and 19200 * the link was probably lost 19201 * momentarily - in such case 19202 * we may want to check device 19203 * identity. 19204 * Identity check is not supported now. 19205 * 19206 * Link established event 19207 * triggers device reset event. 19208 */ 19209 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 19210 satadrv_event_flags |= 19211 SATA_EVNT_DEVICE_RESET; 19212 } 19213 } else if (cportinfo->cport_dev_type == 19214 SATA_DTYPE_NONE) { 19215 /* 19216 * We got new device attached! If HBA does not 19217 * generate device attached events, trigger it 19218 * here. 19219 */ 19220 if (!(SATA_FEATURES(sata_hba_inst) & 19221 SATA_CTLF_HOTPLUG)) { 19222 cportinfo->cport_event_flags |= 19223 SATA_EVNT_DEVICE_ATTACHED; 19224 } 19225 } 19226 /* Reset link lost timeout */ 19227 cportinfo->cport_link_lost_time = 0; 19228 } 19229 } 19230 linklost: 19231 if (event_flags & SATA_EVNT_LINK_LOST) { 19232 if ((sata_device.satadev_scr.sstatus & 19233 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) { 19234 /* Ignore event */ 19235 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19236 "Ignoring port %d link lost event - link is up", 19237 saddr->cport); 19238 goto done; 19239 } 19240 #ifdef SATA_DEBUG 19241 if (cportinfo->cport_link_lost_time == 0) { 19242 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19243 "Processing port %d link lost event", 19244 saddr->cport); 19245 } 19246 #endif 19247 /* 19248 * When HBA cannot generate device attached/detached events, 19249 * we need to track link lost time and eventually generate 19250 * device detach event. 19251 */ 19252 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) { 19253 /* We are tracking link lost time */ 19254 if (cportinfo->cport_link_lost_time == 0) { 19255 /* save current time (lbolt value) */ 19256 cportinfo->cport_link_lost_time = 19257 ddi_get_lbolt(); 19258 /* just keep link lost event */ 19259 cportinfo->cport_event_flags |= 19260 SATA_EVNT_LINK_LOST; 19261 } else { 19262 clock_t cur_time = ddi_get_lbolt(); 19263 if ((cur_time - 19264 cportinfo->cport_link_lost_time) >= 19265 drv_usectohz( 19266 SATA_EVNT_LINK_LOST_TIMEOUT)) { 19267 /* trigger device detach event */ 19268 cportinfo->cport_event_flags |= 19269 SATA_EVNT_DEVICE_DETACHED; 19270 cportinfo->cport_link_lost_time = 0; 19271 SATADBG1(SATA_DBG_EVENTS, 19272 sata_hba_inst, 19273 "Triggering port %d " 19274 "device detached event", 19275 saddr->cport); 19276 } else { 19277 /* keep link lost event */ 19278 cportinfo->cport_event_flags |= 19279 SATA_EVNT_LINK_LOST; 19280 } 19281 } 19282 } 19283 /* 19284 * We could change port state to disable/delay access to 19285 * the attached device until the link is recovered. 19286 */ 19287 } 19288 done: 19289 event_flags = cportinfo->cport_event_flags; 19290 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19291 if (event_flags != 0) { 19292 mutex_enter(&sata_hba_inst->satahba_mutex); 19293 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 19294 mutex_exit(&sata_hba_inst->satahba_mutex); 19295 mutex_enter(&sata_mutex); 19296 sata_event_pending |= SATA_EVNT_MAIN; 19297 mutex_exit(&sata_mutex); 19298 } 19299 } 19300 19301 /* 19302 * Port Multiplier Port Link Events processing. 19303 */ 19304 static void 19305 sata_process_pmport_link_events(sata_hba_inst_t *sata_hba_inst, 19306 sata_address_t *saddr) 19307 { 19308 sata_device_t sata_device; 19309 sata_pmport_info_t *pmportinfo = NULL; 19310 sata_drive_info_t *sdinfo = NULL; 19311 uint32_t event_flags; 19312 uint8_t cport = saddr->cport; 19313 uint8_t pmport = saddr->pmport; 19314 int rval; 19315 19316 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19317 "Processing port %d:%d link event(s)", 19318 cport, pmport); 19319 19320 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 19321 mutex_enter(&pmportinfo->pmport_mutex); 19322 event_flags = pmportinfo->pmport_event_flags; 19323 19324 /* Reset event flags first */ 19325 pmportinfo->pmport_event_flags &= 19326 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST); 19327 19328 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */ 19329 if ((pmportinfo->pmport_state & 19330 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 19331 mutex_exit(&pmportinfo->pmport_mutex); 19332 return; 19333 } 19334 19335 /* 19336 * For the sanity sake get current port state. 19337 * Set device address only. Other sata_device fields should be 19338 * set by HBA driver. 19339 */ 19340 sata_device.satadev_rev = SATA_DEVICE_REV; 19341 sata_device.satadev_addr = *saddr; 19342 /* 19343 * We have to exit mutex, because the HBA probe port function may 19344 * block on its own mutex. 19345 */ 19346 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 19347 saddr->pmport)); 19348 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 19349 (SATA_DIP(sata_hba_inst), &sata_device); 19350 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 19351 saddr->pmport)); 19352 sata_update_pmport_info(sata_hba_inst, &sata_device); 19353 if (rval != SATA_SUCCESS) { 19354 /* Something went wrong? Fail the port */ 19355 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 19356 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 19357 saddr->pmport)); 19358 SATA_LOG_D((sata_hba_inst, CE_WARN, 19359 "SATA port %d:%d probing failed", 19360 saddr->cport, saddr->pmport)); 19361 /* 19362 * We may want to release device info structure, but 19363 * it is not necessary. 19364 */ 19365 return; 19366 } else { 19367 /* port probed successfully */ 19368 pmportinfo->pmport_state |= 19369 SATA_STATE_PROBED | SATA_STATE_READY; 19370 } 19371 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, 19372 saddr->cport, saddr->pmport)); 19373 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, 19374 saddr->cport, saddr->pmport)); 19375 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) { 19376 19377 if ((sata_device.satadev_scr.sstatus & 19378 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) { 19379 /* Ignore event */ 19380 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19381 "Ignoring port %d:%d link established event - " 19382 "link down", 19383 saddr->cport, saddr->pmport); 19384 goto linklost; 19385 } 19386 19387 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19388 "Processing port %d:%d link established event", 19389 cport, pmport); 19390 19391 /* 19392 * For the sanity sake check if a device is attached - check 19393 * return state of a port probing. 19394 */ 19395 if (sata_device.satadev_type != SATA_DTYPE_NONE && 19396 sata_device.satadev_type != SATA_DTYPE_PMULT) { 19397 /* 19398 * HBA port probe indicated that there is a device 19399 * attached. Check if the framework had device info 19400 * structure attached for this device. 19401 */ 19402 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 19403 ASSERT(SATA_PMPORTINFO_DRV_INFO(pmportinfo) != 19404 NULL); 19405 19406 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 19407 if ((sdinfo->satadrv_type & 19408 SATA_VALID_DEV_TYPE) != 0) { 19409 /* 19410 * Dev info structure is present. 19411 * If dev_type is set to known type in 19412 * the framework's drive info struct 19413 * then the device existed before and 19414 * the link was probably lost 19415 * momentarily - in such case 19416 * we may want to check device 19417 * identity. 19418 * Identity check is not supported now. 19419 * 19420 * Link established event 19421 * triggers device reset event. 19422 */ 19423 (SATA_PMPORTINFO_DRV_INFO(pmportinfo))-> 19424 satadrv_event_flags |= 19425 SATA_EVNT_DEVICE_RESET; 19426 } 19427 } else if (pmportinfo->pmport_dev_type == 19428 SATA_DTYPE_NONE) { 19429 /* 19430 * We got new device attached! If HBA does not 19431 * generate device attached events, trigger it 19432 * here. 19433 */ 19434 if (!(SATA_FEATURES(sata_hba_inst) & 19435 SATA_CTLF_HOTPLUG)) { 19436 pmportinfo->pmport_event_flags |= 19437 SATA_EVNT_DEVICE_ATTACHED; 19438 } 19439 } 19440 /* Reset link lost timeout */ 19441 pmportinfo->pmport_link_lost_time = 0; 19442 } 19443 } 19444 linklost: 19445 if (event_flags & SATA_EVNT_LINK_LOST) { 19446 #ifdef SATA_DEBUG 19447 if (pmportinfo->pmport_link_lost_time == 0) { 19448 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19449 "Processing port %d:%d link lost event", 19450 saddr->cport, saddr->pmport); 19451 } 19452 #endif 19453 if ((sata_device.satadev_scr.sstatus & 19454 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) { 19455 /* Ignore event */ 19456 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19457 "Ignoring port %d:%d link lost event - link is up", 19458 saddr->cport, saddr->pmport); 19459 goto done; 19460 } 19461 /* 19462 * When HBA cannot generate device attached/detached events, 19463 * we need to track link lost time and eventually generate 19464 * device detach event. 19465 */ 19466 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) { 19467 /* We are tracking link lost time */ 19468 if (pmportinfo->pmport_link_lost_time == 0) { 19469 /* save current time (lbolt value) */ 19470 pmportinfo->pmport_link_lost_time = 19471 ddi_get_lbolt(); 19472 /* just keep link lost event */ 19473 pmportinfo->pmport_event_flags |= 19474 SATA_EVNT_LINK_LOST; 19475 } else { 19476 clock_t cur_time = ddi_get_lbolt(); 19477 if ((cur_time - 19478 pmportinfo->pmport_link_lost_time) >= 19479 drv_usectohz( 19480 SATA_EVNT_LINK_LOST_TIMEOUT)) { 19481 /* trigger device detach event */ 19482 pmportinfo->pmport_event_flags |= 19483 SATA_EVNT_DEVICE_DETACHED; 19484 pmportinfo->pmport_link_lost_time = 0; 19485 SATADBG2(SATA_DBG_EVENTS, 19486 sata_hba_inst, 19487 "Triggering port %d:%d " 19488 "device detached event", 19489 saddr->cport, saddr->pmport); 19490 } else { 19491 /* keep link lost event */ 19492 pmportinfo->pmport_event_flags |= 19493 SATA_EVNT_LINK_LOST; 19494 } 19495 } 19496 } 19497 /* 19498 * We could change port state to disable/delay access to 19499 * the attached device until the link is recovered. 19500 */ 19501 } 19502 done: 19503 event_flags = pmportinfo->pmport_event_flags; 19504 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 19505 saddr->pmport)); 19506 if (event_flags != 0) { 19507 mutex_enter(&sata_hba_inst->satahba_mutex); 19508 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 19509 mutex_exit(&sata_hba_inst->satahba_mutex); 19510 mutex_enter(&sata_mutex); 19511 sata_event_pending |= SATA_EVNT_MAIN; 19512 mutex_exit(&sata_mutex); 19513 } 19514 } 19515 19516 /* 19517 * Device Detached Event processing. 19518 * Port is probed to find if a device is really gone. If so, 19519 * the device info structure is detached from the SATA port info structure 19520 * and released. 19521 * Port status is updated. 19522 * 19523 * NOTE: Port multiplier ports events are handled by 19524 * sata_process_pmdevice_detached() 19525 */ 19526 static void 19527 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst, 19528 sata_address_t *saddr) 19529 { 19530 sata_cport_info_t *cportinfo; 19531 sata_pmport_info_t *pmportinfo; 19532 sata_drive_info_t *sdevinfo; 19533 sata_device_t sata_device; 19534 sata_address_t pmport_addr; 19535 char name[16]; 19536 uint8_t cport = saddr->cport; 19537 int npmport; 19538 int rval; 19539 19540 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19541 "Processing port %d device detached", saddr->cport); 19542 19543 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 19544 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19545 /* Clear event flag */ 19546 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED; 19547 19548 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */ 19549 if ((cportinfo->cport_state & 19550 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 19551 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19552 cport_mutex); 19553 return; 19554 } 19555 /* For sanity, re-probe the port */ 19556 sata_device.satadev_rev = SATA_DEVICE_REV; 19557 sata_device.satadev_addr = *saddr; 19558 19559 /* 19560 * We have to exit mutex, because the HBA probe port function may 19561 * block on its own mutex. 19562 */ 19563 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19564 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 19565 (SATA_DIP(sata_hba_inst), &sata_device); 19566 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19567 sata_update_port_info(sata_hba_inst, &sata_device); 19568 if (rval != SATA_SUCCESS) { 19569 /* Something went wrong? Fail the port */ 19570 cportinfo->cport_state = SATA_PSTATE_FAILED; 19571 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19572 cport_mutex); 19573 SATA_LOG_D((sata_hba_inst, CE_WARN, 19574 "SATA port %d probing failed", 19575 saddr->cport)); 19576 /* 19577 * We may want to release device info structure, but 19578 * it is not necessary. 19579 */ 19580 return; 19581 } else { 19582 /* port probed successfully */ 19583 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 19584 } 19585 /* 19586 * Check if a device is still attached. For sanity, check also 19587 * link status - if no link, there is no device. 19588 */ 19589 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) == 19590 SATA_PORT_DEVLINK_UP && sata_device.satadev_type != 19591 SATA_DTYPE_NONE) { 19592 /* 19593 * Device is still attached - ignore detach event. 19594 */ 19595 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19596 cport_mutex); 19597 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19598 "Ignoring detach - device still attached to port %d", 19599 sata_device.satadev_addr.cport); 19600 return; 19601 } 19602 /* 19603 * We need to detach and release device info structure here 19604 */ 19605 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 19606 /* 19607 * A port-multiplier is removed. 19608 * 19609 * Calling sata_process_pmdevice_detached() does not work 19610 * here. The port multiplier is gone, so we cannot probe 19611 * sub-port any more and all pmult-related data structure must 19612 * be de-allocated immediately. Following structure of every 19613 * implemented sub-port behind the pmult are required to 19614 * released. 19615 * 19616 * - attachment point 19617 * - target node 19618 * - sata_drive_info 19619 * - sata_pmport_info 19620 */ 19621 for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, 19622 cport); npmport ++) { 19623 SATADBG2(SATA_DBG_PMULT|SATA_DBG_EVENTS_PROC, 19624 sata_hba_inst, 19625 "Detaching target node at port %d:%d", 19626 cport, npmport); 19627 19628 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 19629 19630 /* Remove attachment point. */ 19631 name[0] = '\0'; 19632 (void) sprintf(name, "%d.%d", cport, npmport); 19633 ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name); 19634 sata_log(sata_hba_inst, CE_NOTE, 19635 "Remove attachment point of port %d:%d", 19636 cport, npmport); 19637 19638 /* Remove target node */ 19639 pmport_addr.cport = cport; 19640 pmport_addr.pmport = (uint8_t)npmport; 19641 pmport_addr.qual = SATA_ADDR_PMPORT; 19642 sata_remove_target_node(sata_hba_inst, &pmport_addr); 19643 19644 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 19645 19646 /* Release sata_pmport_info & sata_drive_info. */ 19647 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 19648 cport, npmport); 19649 ASSERT(pmportinfo != NULL); 19650 19651 sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 19652 if (sdevinfo != NULL) { 19653 (void) kmem_free((void *) sdevinfo, 19654 sizeof (sata_drive_info_t)); 19655 } 19656 19657 /* Release sata_pmport_info at last */ 19658 (void) kmem_free((void *) pmportinfo, 19659 sizeof (sata_pmport_info_t)); 19660 } 19661 19662 /* Finally, release sata_pmult_info */ 19663 (void) kmem_free((void *) 19664 SATA_CPORTINFO_PMULT_INFO(cportinfo), 19665 sizeof (sata_pmult_info_t)); 19666 SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL; 19667 19668 sata_log(sata_hba_inst, CE_WARN, 19669 "SATA port-multiplier detached at port %d", cport); 19670 19671 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 19672 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19673 saddr->cport)->cport_mutex); 19674 } else { 19675 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 19676 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 19677 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 19678 (void) kmem_free((void *)sdevinfo, 19679 sizeof (sata_drive_info_t)); 19680 } 19681 sata_log(sata_hba_inst, CE_WARN, 19682 "SATA device detached at port %d", cport); 19683 19684 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 19685 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19686 saddr->cport)->cport_mutex); 19687 19688 /* 19689 * Try to offline a device and remove target node 19690 * if it still exists 19691 */ 19692 sata_remove_target_node(sata_hba_inst, saddr); 19693 } 19694 19695 19696 /* 19697 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 19698 * with the hint: SE_HINT_REMOVE 19699 */ 19700 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE); 19701 } 19702 19703 /* 19704 * Port Multiplier Port Device Deattached Event processing. 19705 * 19706 * NOTE: No Mutex should be hold. 19707 */ 19708 static void 19709 sata_process_pmdevice_detached(sata_hba_inst_t *sata_hba_inst, 19710 sata_address_t *saddr) 19711 { 19712 sata_pmport_info_t *pmportinfo; 19713 sata_drive_info_t *sdevinfo; 19714 sata_device_t sata_device; 19715 int rval; 19716 uint8_t cport, pmport; 19717 19718 cport = saddr->cport; 19719 pmport = saddr->pmport; 19720 19721 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19722 "Processing port %d:%d device detached", 19723 cport, pmport); 19724 19725 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 19726 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19727 19728 /* Clear event flag */ 19729 pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED; 19730 19731 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */ 19732 if ((pmportinfo->pmport_state & 19733 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 19734 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19735 return; 19736 } 19737 /* For sanity, re-probe the port */ 19738 sata_device.satadev_rev = SATA_DEVICE_REV; 19739 sata_device.satadev_addr = *saddr; 19740 19741 /* 19742 * We have to exit mutex, because the HBA probe port function may 19743 * block on its own mutex. 19744 */ 19745 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19746 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 19747 (SATA_DIP(sata_hba_inst), &sata_device); 19748 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19749 sata_update_pmport_info(sata_hba_inst, &sata_device); 19750 if (rval != SATA_SUCCESS) { 19751 /* Something went wrong? Fail the port */ 19752 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 19753 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19754 SATA_LOG_D((sata_hba_inst, CE_WARN, 19755 "SATA port %d:%d probing failed", 19756 saddr->pmport)); 19757 /* 19758 * We may want to release device info structure, but 19759 * it is not necessary. 19760 */ 19761 return; 19762 } else { 19763 /* port probed successfully */ 19764 pmportinfo->pmport_state |= 19765 SATA_STATE_PROBED | SATA_STATE_READY; 19766 } 19767 /* 19768 * Check if a device is still attached. For sanity, check also 19769 * link status - if no link, there is no device. 19770 */ 19771 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) == 19772 SATA_PORT_DEVLINK_UP && sata_device.satadev_type != 19773 SATA_DTYPE_NONE) { 19774 /* 19775 * Device is still attached - ignore detach event. 19776 */ 19777 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19778 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19779 "Ignoring detach - device still attached to port %d", 19780 sata_device.satadev_addr.pmport); 19781 return; 19782 } 19783 /* 19784 * We need to detach and release device info structure here 19785 */ 19786 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 19787 sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 19788 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 19789 (void) kmem_free((void *)sdevinfo, 19790 sizeof (sata_drive_info_t)); 19791 } 19792 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 19793 /* 19794 * Device cannot be reached anymore, even if the target node may be 19795 * still present. 19796 */ 19797 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19798 19799 /* 19800 * Try to offline a device and remove target node if it still exists 19801 */ 19802 sata_remove_target_node(sata_hba_inst, saddr); 19803 19804 /* 19805 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 19806 * with the hint: SE_HINT_REMOVE 19807 */ 19808 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE); 19809 } 19810 19811 19812 /* 19813 * Device Attached Event processing. 19814 * Port state is checked to verify that a device is really attached. If so, 19815 * the device info structure is created and attached to the SATA port info 19816 * structure. 19817 * 19818 * If attached device cannot be identified or set-up, the retry for the 19819 * attach processing is set-up. Subsequent daemon run would try again to 19820 * identify the device, until the time limit is reached 19821 * (SATA_DEV_IDENTIFY_TIMEOUT). 19822 * 19823 * This function cannot be called in interrupt context (it may sleep). 19824 * 19825 * NOTE: Port multiplier ports events are handled by 19826 * sata_process_pmdevice_attached() 19827 */ 19828 static void 19829 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst, 19830 sata_address_t *saddr) 19831 { 19832 sata_cport_info_t *cportinfo = NULL; 19833 sata_drive_info_t *sdevinfo = NULL; 19834 sata_pmult_info_t *pmultinfo = NULL; 19835 sata_pmport_info_t *pmportinfo = NULL; 19836 sata_device_t sata_device; 19837 dev_info_t *tdip; 19838 uint32_t event_flags = 0, pmult_event_flags = 0; 19839 int rval; 19840 int npmport; 19841 19842 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19843 "Processing port %d device attached", saddr->cport); 19844 19845 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 19846 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19847 19848 /* Clear attach event flag first */ 19849 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED; 19850 19851 /* If the port is in SHUTDOWN or FAILED state, ignore event. */ 19852 if ((cportinfo->cport_state & 19853 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 19854 cportinfo->cport_dev_attach_time = 0; 19855 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19856 cport_mutex); 19857 return; 19858 } 19859 19860 /* 19861 * If the sata_drive_info structure is found attached to the port info, 19862 * despite the fact the device was removed and now it is re-attached, 19863 * the old drive info structure was not removed. 19864 * Arbitrarily release device info structure. 19865 */ 19866 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 19867 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 19868 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 19869 (void) kmem_free((void *)sdevinfo, 19870 sizeof (sata_drive_info_t)); 19871 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19872 "Arbitrarily detaching old device info.", NULL); 19873 } 19874 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 19875 19876 /* For sanity, re-probe the port */ 19877 sata_device.satadev_rev = SATA_DEVICE_REV; 19878 sata_device.satadev_addr = *saddr; 19879 19880 /* 19881 * We have to exit mutex, because the HBA probe port function may 19882 * block on its own mutex. 19883 */ 19884 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19885 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 19886 (SATA_DIP(sata_hba_inst), &sata_device); 19887 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19888 sata_update_port_info(sata_hba_inst, &sata_device); 19889 if (rval != SATA_SUCCESS) { 19890 /* Something went wrong? Fail the port */ 19891 cportinfo->cport_state = SATA_PSTATE_FAILED; 19892 cportinfo->cport_dev_attach_time = 0; 19893 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19894 cport_mutex); 19895 SATA_LOG_D((sata_hba_inst, CE_WARN, 19896 "SATA port %d probing failed", 19897 saddr->cport)); 19898 return; 19899 } else { 19900 /* port probed successfully */ 19901 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 19902 } 19903 /* 19904 * Check if a device is still attached. For sanity, check also 19905 * link status - if no link, there is no device. 19906 */ 19907 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 19908 SATA_PORT_DEVLINK_UP || sata_device.satadev_type == 19909 SATA_DTYPE_NONE) { 19910 /* 19911 * No device - ignore attach event. 19912 */ 19913 cportinfo->cport_dev_attach_time = 0; 19914 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19915 cport_mutex); 19916 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19917 "Ignoring attach - no device connected to port %d", 19918 sata_device.satadev_addr.cport); 19919 return; 19920 } 19921 19922 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19923 /* 19924 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 19925 * with the hint: SE_HINT_INSERT 19926 */ 19927 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT); 19928 19929 /* 19930 * Port reprobing will take care of the creation of the device 19931 * info structure and determination of the device type. 19932 */ 19933 sata_device.satadev_addr = *saddr; 19934 (void) sata_reprobe_port(sata_hba_inst, &sata_device, 19935 SATA_DEV_IDENTIFY_NORETRY); 19936 19937 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19938 cport_mutex); 19939 if ((cportinfo->cport_state & SATA_STATE_READY) && 19940 (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) { 19941 /* Some device is attached to the port */ 19942 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) { 19943 /* 19944 * A device was not successfully attached. 19945 * Track retry time for device identification. 19946 */ 19947 if (cportinfo->cport_dev_attach_time != 0) { 19948 clock_t cur_time = ddi_get_lbolt(); 19949 /* 19950 * If the retry time limit was not exceeded, 19951 * reinstate attach event. 19952 */ 19953 if ((cur_time - 19954 cportinfo->cport_dev_attach_time) < 19955 drv_usectohz( 19956 SATA_DEV_IDENTIFY_TIMEOUT)) { 19957 /* OK, restore attach event */ 19958 cportinfo->cport_event_flags |= 19959 SATA_EVNT_DEVICE_ATTACHED; 19960 } else { 19961 /* Timeout - cannot identify device */ 19962 cportinfo->cport_dev_attach_time = 0; 19963 sata_log(sata_hba_inst, 19964 CE_WARN, 19965 "Could not identify SATA device " 19966 "at port %d", 19967 saddr->cport); 19968 } 19969 } else { 19970 /* 19971 * Start tracking time for device 19972 * identification. 19973 * Save current time (lbolt value). 19974 */ 19975 cportinfo->cport_dev_attach_time = 19976 ddi_get_lbolt(); 19977 /* Restore attach event */ 19978 cportinfo->cport_event_flags |= 19979 SATA_EVNT_DEVICE_ATTACHED; 19980 } 19981 } else if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 19982 cportinfo->cport_dev_attach_time = 0; 19983 sata_log(sata_hba_inst, CE_NOTE, 19984 "SATA port-multiplier detected at port %d", 19985 saddr->cport); 19986 19987 if (SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL) { 19988 /* Log the info of new port multiplier */ 19989 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19990 saddr->cport)->cport_mutex); 19991 sata_show_pmult_info(sata_hba_inst, 19992 &sata_device); 19993 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 19994 saddr->cport)->cport_mutex); 19995 } 19996 19997 ASSERT(SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL); 19998 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 19999 for (npmport = 0; npmport < 20000 pmultinfo->pmult_num_dev_ports; npmport++) { 20001 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 20002 saddr->cport, npmport); 20003 ASSERT(pmportinfo != NULL); 20004 20005 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20006 saddr->cport)->cport_mutex); 20007 mutex_enter(&pmportinfo->pmport_mutex); 20008 /* Marked all pmports with link events. */ 20009 pmportinfo->pmport_event_flags = 20010 SATA_EVNT_LINK_ESTABLISHED; 20011 pmult_event_flags |= 20012 pmportinfo->pmport_event_flags; 20013 mutex_exit(&pmportinfo->pmport_mutex); 20014 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20015 saddr->cport)->cport_mutex); 20016 } 20017 /* Auto-online is not available for PMult now. */ 20018 20019 } else { 20020 /* 20021 * If device was successfully attached, the subsequent 20022 * action depends on a state of the 20023 * sata_auto_online variable. If it is set to zero. 20024 * an explicit 'configure' command will be needed to 20025 * configure it. If its value is non-zero, we will 20026 * attempt to online (configure) the device. 20027 * First, log the message indicating that a device 20028 * was attached. 20029 */ 20030 cportinfo->cport_dev_attach_time = 0; 20031 sata_log(sata_hba_inst, CE_WARN, 20032 "SATA device detected at port %d", saddr->cport); 20033 20034 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 20035 sata_drive_info_t new_sdinfo; 20036 20037 /* Log device info data */ 20038 new_sdinfo = *(SATA_CPORTINFO_DRV_INFO( 20039 cportinfo)); 20040 sata_show_drive_info(sata_hba_inst, 20041 &new_sdinfo); 20042 } 20043 20044 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20045 saddr->cport)->cport_mutex); 20046 20047 /* 20048 * Make sure that there is no target node for that 20049 * device. If so, release it. It should not happen, 20050 * unless we had problem removing the node when 20051 * device was detached. 20052 */ 20053 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 20054 saddr->cport, saddr->pmport); 20055 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20056 saddr->cport)->cport_mutex); 20057 if (tdip != NULL) { 20058 20059 #ifdef SATA_DEBUG 20060 if ((cportinfo->cport_event_flags & 20061 SATA_EVNT_TARGET_NODE_CLEANUP) == 0) 20062 sata_log(sata_hba_inst, CE_WARN, 20063 "sata_process_device_attached: " 20064 "old device target node exists!"); 20065 #endif 20066 /* 20067 * target node exists - try to unconfigure 20068 * device and remove the node. 20069 */ 20070 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20071 saddr->cport)->cport_mutex); 20072 rval = ndi_devi_offline(tdip, 20073 NDI_DEVI_REMOVE); 20074 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20075 saddr->cport)->cport_mutex); 20076 20077 if (rval == NDI_SUCCESS) { 20078 cportinfo->cport_event_flags &= 20079 ~SATA_EVNT_TARGET_NODE_CLEANUP; 20080 cportinfo->cport_tgtnode_clean = B_TRUE; 20081 } else { 20082 /* 20083 * PROBLEM - the target node remained 20084 * and it belongs to a previously 20085 * attached device. 20086 * This happens when the file was open 20087 * or the node was waiting for 20088 * resources at the time the 20089 * associated device was removed. 20090 * Instruct event daemon to retry the 20091 * cleanup later. 20092 */ 20093 sata_log(sata_hba_inst, 20094 CE_WARN, 20095 "Application(s) accessing " 20096 "previously attached SATA " 20097 "device have to release " 20098 "it before newly inserted " 20099 "device can be made accessible.", 20100 saddr->cport); 20101 cportinfo->cport_event_flags |= 20102 SATA_EVNT_TARGET_NODE_CLEANUP; 20103 cportinfo->cport_tgtnode_clean = 20104 B_FALSE; 20105 } 20106 } 20107 if (sata_auto_online != 0) { 20108 cportinfo->cport_event_flags |= 20109 SATA_EVNT_AUTOONLINE_DEVICE; 20110 } 20111 20112 } 20113 } else { 20114 cportinfo->cport_dev_attach_time = 0; 20115 } 20116 20117 event_flags = cportinfo->cport_event_flags; 20118 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 20119 if (event_flags != 0 || pmult_event_flags != 0) { 20120 mutex_enter(&sata_hba_inst->satahba_mutex); 20121 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 20122 mutex_exit(&sata_hba_inst->satahba_mutex); 20123 mutex_enter(&sata_mutex); 20124 sata_event_pending |= SATA_EVNT_MAIN; 20125 mutex_exit(&sata_mutex); 20126 } 20127 } 20128 20129 /* 20130 * Port Multiplier Port Device Attached Event processing. 20131 * 20132 * NOTE: No Mutex should be hold. 20133 */ 20134 static void 20135 sata_process_pmdevice_attached(sata_hba_inst_t *sata_hba_inst, 20136 sata_address_t *saddr) 20137 { 20138 sata_pmport_info_t *pmportinfo; 20139 sata_drive_info_t *sdinfo; 20140 sata_device_t sata_device; 20141 dev_info_t *tdip; 20142 uint32_t event_flags; 20143 uint8_t cport = saddr->cport; 20144 uint8_t pmport = saddr->pmport; 20145 int rval; 20146 20147 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20148 "Processing port %d:%d device attached", cport, pmport); 20149 20150 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 20151 20152 mutex_enter(&pmportinfo->pmport_mutex); 20153 20154 /* Clear attach event flag first */ 20155 pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED; 20156 20157 /* If the port is in SHUTDOWN or FAILED state, ignore event. */ 20158 if ((pmportinfo->pmport_state & 20159 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 20160 pmportinfo->pmport_dev_attach_time = 0; 20161 mutex_exit(&pmportinfo->pmport_mutex); 20162 return; 20163 } 20164 20165 /* 20166 * If the sata_drive_info structure is found attached to the port info, 20167 * despite the fact the device was removed and now it is re-attached, 20168 * the old drive info structure was not removed. 20169 * Arbitrarily release device info structure. 20170 */ 20171 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 20172 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 20173 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 20174 (void) kmem_free((void *)sdinfo, 20175 sizeof (sata_drive_info_t)); 20176 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20177 "Arbitrarily detaching old device info.", NULL); 20178 } 20179 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 20180 20181 /* For sanity, re-probe the port */ 20182 sata_device.satadev_rev = SATA_DEVICE_REV; 20183 sata_device.satadev_addr = *saddr; 20184 20185 /* 20186 * We have to exit mutex, because the HBA probe port function may 20187 * block on its own mutex. 20188 */ 20189 mutex_exit(&pmportinfo->pmport_mutex); 20190 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 20191 (SATA_DIP(sata_hba_inst), &sata_device); 20192 mutex_enter(&pmportinfo->pmport_mutex); 20193 20194 sata_update_pmport_info(sata_hba_inst, &sata_device); 20195 if (rval != SATA_SUCCESS) { 20196 /* Something went wrong? Fail the port */ 20197 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 20198 pmportinfo->pmport_dev_attach_time = 0; 20199 mutex_exit(&pmportinfo->pmport_mutex); 20200 SATA_LOG_D((sata_hba_inst, CE_WARN, 20201 "SATA port %d:%d probing failed", cport, pmport)); 20202 return; 20203 } else { 20204 /* pmport probed successfully */ 20205 pmportinfo->pmport_state |= 20206 SATA_STATE_PROBED | SATA_STATE_READY; 20207 } 20208 /* 20209 * Check if a device is still attached. For sanity, check also 20210 * link status - if no link, there is no device. 20211 */ 20212 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 20213 SATA_PORT_DEVLINK_UP || sata_device.satadev_type == 20214 SATA_DTYPE_NONE) { 20215 /* 20216 * No device - ignore attach event. 20217 */ 20218 pmportinfo->pmport_dev_attach_time = 0; 20219 mutex_exit(&pmportinfo->pmport_mutex); 20220 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20221 "Ignoring attach - no device connected to port %d:%d", 20222 cport, pmport); 20223 return; 20224 } 20225 20226 mutex_exit(&pmportinfo->pmport_mutex); 20227 /* 20228 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 20229 * with the hint: SE_HINT_INSERT 20230 */ 20231 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT); 20232 20233 /* 20234 * Port reprobing will take care of the creation of the device 20235 * info structure and determination of the device type. 20236 */ 20237 sata_device.satadev_addr = *saddr; 20238 (void) sata_reprobe_port(sata_hba_inst, &sata_device, 20239 SATA_DEV_IDENTIFY_NORETRY); 20240 20241 mutex_enter(&pmportinfo->pmport_mutex); 20242 if ((pmportinfo->pmport_state & SATA_STATE_READY) && 20243 (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE)) { 20244 /* Some device is attached to the port */ 20245 if (pmportinfo->pmport_dev_type == SATA_DTYPE_UNKNOWN) { 20246 /* 20247 * A device was not successfully attached. 20248 * Track retry time for device identification. 20249 */ 20250 if (pmportinfo->pmport_dev_attach_time != 0) { 20251 clock_t cur_time = ddi_get_lbolt(); 20252 /* 20253 * If the retry time limit was not exceeded, 20254 * reinstate attach event. 20255 */ 20256 if ((cur_time - 20257 pmportinfo->pmport_dev_attach_time) < 20258 drv_usectohz( 20259 SATA_DEV_IDENTIFY_TIMEOUT)) { 20260 /* OK, restore attach event */ 20261 pmportinfo->pmport_event_flags |= 20262 SATA_EVNT_DEVICE_ATTACHED; 20263 } else { 20264 /* Timeout - cannot identify device */ 20265 pmportinfo->pmport_dev_attach_time = 0; 20266 sata_log(sata_hba_inst, CE_WARN, 20267 "Could not identify SATA device " 20268 "at port %d:%d", 20269 cport, pmport); 20270 } 20271 } else { 20272 /* 20273 * Start tracking time for device 20274 * identification. 20275 * Save current time (lbolt value). 20276 */ 20277 pmportinfo->pmport_dev_attach_time = 20278 ddi_get_lbolt(); 20279 /* Restore attach event */ 20280 pmportinfo->pmport_event_flags |= 20281 SATA_EVNT_DEVICE_ATTACHED; 20282 } 20283 } else { 20284 /* 20285 * If device was successfully attached, the subsequent 20286 * action depends on a state of the 20287 * sata_auto_online variable. If it is set to zero. 20288 * an explicit 'configure' command will be needed to 20289 * configure it. If its value is non-zero, we will 20290 * attempt to online (configure) the device. 20291 * First, log the message indicating that a device 20292 * was attached. 20293 */ 20294 pmportinfo->pmport_dev_attach_time = 0; 20295 sata_log(sata_hba_inst, CE_WARN, 20296 "SATA device detected at port %d:%d", 20297 cport, pmport); 20298 20299 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 20300 sata_drive_info_t new_sdinfo; 20301 20302 /* Log device info data */ 20303 new_sdinfo = *(SATA_PMPORTINFO_DRV_INFO( 20304 pmportinfo)); 20305 sata_show_drive_info(sata_hba_inst, 20306 &new_sdinfo); 20307 } 20308 20309 mutex_exit(&pmportinfo->pmport_mutex); 20310 20311 /* 20312 * Make sure that there is no target node for that 20313 * device. If so, release it. It should not happen, 20314 * unless we had problem removing the node when 20315 * device was detached. 20316 */ 20317 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 20318 saddr->cport, saddr->pmport); 20319 mutex_enter(&pmportinfo->pmport_mutex); 20320 if (tdip != NULL) { 20321 20322 #ifdef SATA_DEBUG 20323 if ((pmportinfo->pmport_event_flags & 20324 SATA_EVNT_TARGET_NODE_CLEANUP) == 0) 20325 sata_log(sata_hba_inst, CE_WARN, 20326 "sata_process_device_attached: " 20327 "old device target node exists!"); 20328 #endif 20329 /* 20330 * target node exists - try to unconfigure 20331 * device and remove the node. 20332 */ 20333 mutex_exit(&pmportinfo->pmport_mutex); 20334 rval = ndi_devi_offline(tdip, 20335 NDI_DEVI_REMOVE); 20336 mutex_enter(&pmportinfo->pmport_mutex); 20337 20338 if (rval == NDI_SUCCESS) { 20339 pmportinfo->pmport_event_flags &= 20340 ~SATA_EVNT_TARGET_NODE_CLEANUP; 20341 pmportinfo->pmport_tgtnode_clean = 20342 B_TRUE; 20343 } else { 20344 /* 20345 * PROBLEM - the target node remained 20346 * and it belongs to a previously 20347 * attached device. 20348 * This happens when the file was open 20349 * or the node was waiting for 20350 * resources at the time the 20351 * associated device was removed. 20352 * Instruct event daemon to retry the 20353 * cleanup later. 20354 */ 20355 sata_log(sata_hba_inst, 20356 CE_WARN, 20357 "Application(s) accessing " 20358 "previously attached SATA " 20359 "device have to release " 20360 "it before newly inserted " 20361 "device can be made accessible." 20362 "at port %d:%d", 20363 cport, pmport); 20364 pmportinfo->pmport_event_flags |= 20365 SATA_EVNT_TARGET_NODE_CLEANUP; 20366 pmportinfo->pmport_tgtnode_clean = 20367 B_FALSE; 20368 } 20369 } 20370 if (sata_auto_online != 0) { 20371 pmportinfo->pmport_event_flags |= 20372 SATA_EVNT_AUTOONLINE_DEVICE; 20373 } 20374 20375 } 20376 } else { 20377 pmportinfo->pmport_dev_attach_time = 0; 20378 } 20379 20380 event_flags = pmportinfo->pmport_event_flags; 20381 mutex_exit(&pmportinfo->pmport_mutex); 20382 if (event_flags != 0) { 20383 mutex_enter(&sata_hba_inst->satahba_mutex); 20384 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 20385 mutex_exit(&sata_hba_inst->satahba_mutex); 20386 mutex_enter(&sata_mutex); 20387 sata_event_pending |= SATA_EVNT_MAIN; 20388 mutex_exit(&sata_mutex); 20389 } 20390 20391 /* clear the reset_in_progress events */ 20392 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 20393 if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) { 20394 /* must clear flags on cport */ 20395 sata_pmult_info_t *pminfo = 20396 SATA_PMULT_INFO(sata_hba_inst, 20397 saddr->cport); 20398 pminfo->pmult_event_flags |= 20399 SATA_EVNT_CLEAR_DEVICE_RESET; 20400 } 20401 } 20402 } 20403 20404 /* 20405 * Device Target Node Cleanup Event processing. 20406 * If the target node associated with a sata port device is in 20407 * DEVI_DEVICE_REMOVED state, an attempt is made to remove it. 20408 * If the target node cannot be removed, the event flag is left intact, 20409 * so that event daemon may re-run this function later. 20410 * 20411 * This function cannot be called in interrupt context (it may sleep). 20412 * 20413 * NOTE: Processes cport events only, not port multiplier ports. 20414 */ 20415 static void 20416 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, 20417 sata_address_t *saddr) 20418 { 20419 sata_cport_info_t *cportinfo; 20420 dev_info_t *tdip; 20421 20422 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20423 "Processing port %d device target node cleanup", saddr->cport); 20424 20425 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 20426 20427 /* 20428 * Check if there is target node for that device and it is in the 20429 * DEVI_DEVICE_REMOVED state. If so, release it. 20430 */ 20431 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport, 20432 saddr->pmport); 20433 if (tdip != NULL) { 20434 /* 20435 * target node exists - check if it is target node of 20436 * a removed device. 20437 */ 20438 if (sata_check_device_removed(tdip) == B_TRUE) { 20439 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20440 "sata_process_target_node_cleanup: " 20441 "old device target node exists!", NULL); 20442 /* 20443 * Unconfigure and remove the target node 20444 */ 20445 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) == 20446 NDI_SUCCESS) { 20447 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20448 saddr->cport)->cport_mutex); 20449 cportinfo->cport_event_flags &= 20450 ~SATA_EVNT_TARGET_NODE_CLEANUP; 20451 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20452 saddr->cport)->cport_mutex); 20453 return; 20454 } 20455 /* 20456 * Event daemon will retry the cleanup later. 20457 */ 20458 mutex_enter(&sata_hba_inst->satahba_mutex); 20459 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 20460 mutex_exit(&sata_hba_inst->satahba_mutex); 20461 mutex_enter(&sata_mutex); 20462 sata_event_pending |= SATA_EVNT_MAIN; 20463 mutex_exit(&sata_mutex); 20464 } 20465 } else { 20466 if (saddr->qual == SATA_ADDR_CPORT || 20467 saddr->qual == SATA_ADDR_DCPORT) { 20468 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20469 saddr->cport)->cport_mutex); 20470 cportinfo->cport_event_flags &= 20471 ~SATA_EVNT_TARGET_NODE_CLEANUP; 20472 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20473 saddr->cport)->cport_mutex); 20474 } else { 20475 /* sanity check */ 20476 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) != 20477 SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst, 20478 saddr->cport) == NULL) 20479 return; 20480 if (SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, 20481 saddr->pmport) == NULL) 20482 return; 20483 20484 mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst, 20485 saddr->cport, saddr->pmport)->pmport_mutex); 20486 SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, 20487 saddr->pmport)->pmport_event_flags &= 20488 ~SATA_EVNT_TARGET_NODE_CLEANUP; 20489 mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst, 20490 saddr->cport, saddr->pmport)->pmport_mutex); 20491 } 20492 } 20493 } 20494 20495 /* 20496 * Device AutoOnline Event processing. 20497 * If attached device is to be onlined, an attempt is made to online this 20498 * device, but only if there is no lingering (old) target node present. 20499 * If the device cannot be onlined, the event flag is left intact, 20500 * so that event daemon may re-run this function later. 20501 * 20502 * This function cannot be called in interrupt context (it may sleep). 20503 * 20504 * NOTE: Processes cport events only, not port multiplier ports. 20505 */ 20506 static void 20507 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst, 20508 sata_address_t *saddr) 20509 { 20510 sata_cport_info_t *cportinfo; 20511 sata_drive_info_t *sdinfo; 20512 sata_device_t sata_device; 20513 dev_info_t *tdip; 20514 20515 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20516 "Processing port %d attached device auto-onlining", saddr->cport); 20517 20518 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 20519 20520 /* 20521 * Check if device is present and recognized. If not, reset event. 20522 */ 20523 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 20524 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) { 20525 /* Nothing to online */ 20526 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE; 20527 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20528 saddr->cport)->cport_mutex); 20529 return; 20530 } 20531 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 20532 20533 /* 20534 * Check if there is target node for this device and if it is in the 20535 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep 20536 * the event for later processing. 20537 */ 20538 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport, 20539 saddr->pmport); 20540 if (tdip != NULL) { 20541 /* 20542 * target node exists - check if it is target node of 20543 * a removed device. 20544 */ 20545 if (sata_check_device_removed(tdip) == B_TRUE) { 20546 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20547 "sata_process_device_autoonline: " 20548 "old device target node exists!", NULL); 20549 /* 20550 * Event daemon will retry device onlining later. 20551 */ 20552 mutex_enter(&sata_hba_inst->satahba_mutex); 20553 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 20554 mutex_exit(&sata_hba_inst->satahba_mutex); 20555 mutex_enter(&sata_mutex); 20556 sata_event_pending |= SATA_EVNT_MAIN; 20557 mutex_exit(&sata_mutex); 20558 return; 20559 } 20560 /* 20561 * If the target node is not in the 'removed" state, assume 20562 * that it belongs to this device. There is nothing more to do, 20563 * but reset the event. 20564 */ 20565 } else { 20566 20567 /* 20568 * Try to online the device 20569 * If there is any reset-related event, remove it. We are 20570 * configuring the device and no state restoring is needed. 20571 */ 20572 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20573 saddr->cport)->cport_mutex); 20574 sata_device.satadev_addr = *saddr; 20575 if (saddr->qual == SATA_ADDR_CPORT) 20576 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 20577 else 20578 sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT; 20579 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 20580 if (sdinfo != NULL) { 20581 if (sdinfo->satadrv_event_flags & 20582 (SATA_EVNT_DEVICE_RESET | 20583 SATA_EVNT_INPROC_DEVICE_RESET)) 20584 sdinfo->satadrv_event_flags = 0; 20585 sdinfo->satadrv_event_flags |= 20586 SATA_EVNT_CLEAR_DEVICE_RESET; 20587 20588 /* Need to create a new target node. */ 20589 cportinfo->cport_tgtnode_clean = B_TRUE; 20590 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20591 saddr->cport)->cport_mutex); 20592 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst), 20593 sata_hba_inst, &sata_device.satadev_addr); 20594 if (tdip == NULL) { 20595 /* 20596 * Configure (onlining) failed. 20597 * We will NOT retry 20598 */ 20599 SATA_LOG_D((sata_hba_inst, CE_WARN, 20600 "sata_process_device_autoonline: " 20601 "configuring SATA device at port %d failed", 20602 saddr->cport)); 20603 } 20604 } else { 20605 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20606 saddr->cport)->cport_mutex); 20607 } 20608 20609 } 20610 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 20611 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE; 20612 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20613 saddr->cport)->cport_mutex); 20614 } 20615 20616 20617 static void 20618 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr, 20619 int hint) 20620 { 20621 char ap[MAXPATHLEN]; 20622 nvlist_t *ev_attr_list = NULL; 20623 int err; 20624 20625 /* Allocate and build sysevent attribute list */ 20626 err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP); 20627 if (err != 0) { 20628 SATA_LOG_D((sata_hba_inst, CE_WARN, 20629 "sata_gen_sysevent: " 20630 "cannot allocate memory for sysevent attributes\n")); 20631 return; 20632 } 20633 /* Add hint attribute */ 20634 err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint)); 20635 if (err != 0) { 20636 SATA_LOG_D((sata_hba_inst, CE_WARN, 20637 "sata_gen_sysevent: " 20638 "failed to add DR_HINT attr for sysevent")); 20639 nvlist_free(ev_attr_list); 20640 return; 20641 } 20642 /* 20643 * Add AP attribute. 20644 * Get controller pathname and convert it into AP pathname by adding 20645 * a target number. 20646 */ 20647 (void) snprintf(ap, MAXPATHLEN, "/devices"); 20648 (void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap)); 20649 (void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d", 20650 SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual)); 20651 20652 err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap); 20653 if (err != 0) { 20654 SATA_LOG_D((sata_hba_inst, CE_WARN, 20655 "sata_gen_sysevent: " 20656 "failed to add DR_AP_ID attr for sysevent")); 20657 nvlist_free(ev_attr_list); 20658 return; 20659 } 20660 20661 /* Generate/log sysevent */ 20662 err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR, 20663 ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP); 20664 if (err != DDI_SUCCESS) { 20665 SATA_LOG_D((sata_hba_inst, CE_WARN, 20666 "sata_gen_sysevent: " 20667 "cannot log sysevent, err code %x\n", err)); 20668 } 20669 20670 nvlist_free(ev_attr_list); 20671 } 20672 20673 20674 20675 20676 /* 20677 * Set DEVI_DEVICE_REMOVED state in the SATA device target node. 20678 */ 20679 static void 20680 sata_set_device_removed(dev_info_t *tdip) 20681 { 20682 int circ; 20683 20684 ASSERT(tdip != NULL); 20685 20686 ndi_devi_enter(tdip, &circ); 20687 mutex_enter(&DEVI(tdip)->devi_lock); 20688 DEVI_SET_DEVICE_REMOVED(tdip); 20689 mutex_exit(&DEVI(tdip)->devi_lock); 20690 ndi_devi_exit(tdip, circ); 20691 } 20692 20693 20694 /* 20695 * Set internal event instructing event daemon to try 20696 * to perform the target node cleanup. 20697 */ 20698 static void 20699 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, 20700 sata_address_t *saddr) 20701 { 20702 if (saddr->qual == SATA_ADDR_CPORT || 20703 saddr->qual == SATA_ADDR_DCPORT) { 20704 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20705 saddr->cport)->cport_mutex); 20706 SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |= 20707 SATA_EVNT_TARGET_NODE_CLEANUP; 20708 SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 20709 cport_tgtnode_clean = B_FALSE; 20710 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20711 saddr->cport)->cport_mutex); 20712 } else { 20713 mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst, 20714 saddr->cport, saddr->pmport)->pmport_mutex); 20715 SATA_PMPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport, 20716 saddr->pmport) |= SATA_EVNT_TARGET_NODE_CLEANUP; 20717 SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, saddr->pmport)-> 20718 pmport_tgtnode_clean = B_FALSE; 20719 mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst, 20720 saddr->cport, saddr->pmport)->pmport_mutex); 20721 } 20722 mutex_enter(&sata_hba_inst->satahba_mutex); 20723 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 20724 mutex_exit(&sata_hba_inst->satahba_mutex); 20725 mutex_enter(&sata_mutex); 20726 sata_event_pending |= SATA_EVNT_MAIN; 20727 mutex_exit(&sata_mutex); 20728 } 20729 20730 20731 /* 20732 * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state, 20733 * i.e. check if the target node state indicates that it belongs to a removed 20734 * device. 20735 * 20736 * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state, 20737 * B_FALSE otherwise. 20738 */ 20739 static boolean_t 20740 sata_check_device_removed(dev_info_t *tdip) 20741 { 20742 ASSERT(tdip != NULL); 20743 20744 if (DEVI_IS_DEVICE_REMOVED(tdip)) 20745 return (B_TRUE); 20746 else 20747 return (B_FALSE); 20748 } 20749 20750 20751 /* 20752 * Check for DMA error. Return B_TRUE if error, B_FALSE otherwise. 20753 */ 20754 static boolean_t 20755 sata_check_for_dma_error(dev_info_t *dip, sata_pkt_txlate_t *spx) 20756 { 20757 int fm_capability = ddi_fm_capable(dip); 20758 ddi_fm_error_t de; 20759 20760 if (fm_capability & DDI_FM_DMACHK_CAPABLE) { 20761 if (spx->txlt_buf_dma_handle != NULL) { 20762 ddi_fm_dma_err_get(spx->txlt_buf_dma_handle, &de, 20763 DDI_FME_VERSION); 20764 if (de.fme_status != DDI_SUCCESS) 20765 return (B_TRUE); 20766 } 20767 } 20768 return (B_FALSE); 20769 } 20770 20771 20772 /* ************************ FAULT INJECTTION **************************** */ 20773 20774 #ifdef SATA_INJECT_FAULTS 20775 20776 static uint32_t sata_fault_count = 0; 20777 static uint32_t sata_fault_suspend_count = 0; 20778 20779 /* 20780 * Inject sata pkt fault 20781 * It modifies returned values of the sata packet. 20782 * It returns immediately if: 20783 * pkt fault injection is not enabled (via sata_inject_fault, 20784 * sata_inject_fault_count), or invalid fault is specified (sata_fault_type), 20785 * or pkt does not contain command to be faulted (set in sata_fault_cmd), or 20786 * pkt is not directed to specified fault controller/device 20787 * (sata_fault_ctrl_dev and sata_fault_device). 20788 * If fault controller is not specified, fault injection applies to all 20789 * controllers and devices. 20790 * 20791 * First argument is the pointer to the executed sata packet. 20792 * Second argument is a pointer to a value returned by the HBA tran_start 20793 * function. 20794 * Third argument specifies injected error. Injected sata packet faults 20795 * are the satapkt_reason values. 20796 * SATA_PKT_BUSY -1 Not completed, busy 20797 * SATA_PKT_DEV_ERROR 1 Device reported error 20798 * SATA_PKT_QUEUE_FULL 2 Not accepted, queue full 20799 * SATA_PKT_PORT_ERROR 3 Not completed, port error 20800 * SATA_PKT_CMD_UNSUPPORTED 4 Cmd unsupported 20801 * SATA_PKT_ABORTED 5 Aborted by request 20802 * SATA_PKT_TIMEOUT 6 Operation timeut 20803 * SATA_PKT_RESET 7 Aborted by reset request 20804 * 20805 * Additional global variables affecting the execution: 20806 * 20807 * sata_inject_fault_count variable specifies number of times in row the 20808 * error is injected. Value of -1 specifies permanent fault, ie. every time 20809 * the fault injection point is reached, the fault is injected and a pause 20810 * between fault injection specified by sata_inject_fault_pause_count is 20811 * ignored). Fault injection routine decrements sata_inject_fault_count 20812 * (if greater than zero) until it reaches 0. No fault is injected when 20813 * sata_inject_fault_count is 0 (zero). 20814 * 20815 * sata_inject_fault_pause_count variable specifies number of times a fault 20816 * injection is bypassed (pause between fault injections). 20817 * If set to 0, a fault is injected only a number of times specified by 20818 * sata_inject_fault_count. 20819 * 20820 * The fault counts are static, so for periodic errors they have to be manually 20821 * reset to start repetition sequence from scratch. 20822 * If the original value returned by the HBA tran_start function is not 20823 * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error 20824 * is injected (to avoid masking real problems); 20825 * 20826 * NOTE: In its current incarnation, this function should be invoked only for 20827 * commands executed in SYNCHRONOUS mode. 20828 */ 20829 20830 20831 static void 20832 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault) 20833 { 20834 20835 if (sata_inject_fault != SATA_INJECT_PKT_FAULT) 20836 return; 20837 20838 if (sata_inject_fault_count == 0) 20839 return; 20840 20841 if (fault == 0) 20842 return; 20843 20844 if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg) 20845 return; 20846 20847 if (sata_fault_ctrl != NULL) { 20848 sata_pkt_txlate_t *spx = 20849 (sata_pkt_txlate_t *)spkt->satapkt_framework_private; 20850 20851 if (sata_fault_ctrl != NULL && sata_fault_ctrl != 20852 spx->txlt_sata_hba_inst->satahba_dip) 20853 return; 20854 20855 if (sata_fault_device.satadev_addr.cport != 20856 spkt->satapkt_device.satadev_addr.cport || 20857 sata_fault_device.satadev_addr.pmport != 20858 spkt->satapkt_device.satadev_addr.pmport || 20859 sata_fault_device.satadev_addr.qual != 20860 spkt->satapkt_device.satadev_addr.qual) 20861 return; 20862 } 20863 20864 /* Modify pkt return parameters */ 20865 if (*rval != SATA_TRAN_ACCEPTED || 20866 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 20867 sata_fault_count = 0; 20868 sata_fault_suspend_count = 0; 20869 return; 20870 } 20871 if (sata_fault_count == 0 && sata_fault_suspend_count != 0) { 20872 /* Pause in the injection */ 20873 sata_fault_suspend_count -= 1; 20874 return; 20875 } 20876 20877 if (sata_fault_count == 0 && sata_fault_suspend_count == 0) { 20878 /* 20879 * Init inject fault cycle. If fault count is set to -1, 20880 * it is a permanent fault. 20881 */ 20882 if (sata_inject_fault_count != -1) { 20883 sata_fault_count = sata_inject_fault_count; 20884 sata_fault_suspend_count = 20885 sata_inject_fault_pause_count; 20886 if (sata_fault_suspend_count == 0) 20887 sata_inject_fault_count = 0; 20888 } 20889 } 20890 20891 if (sata_fault_count != 0) 20892 sata_fault_count -= 1; 20893 20894 switch (fault) { 20895 case SATA_PKT_BUSY: 20896 *rval = SATA_TRAN_BUSY; 20897 spkt->satapkt_reason = SATA_PKT_BUSY; 20898 break; 20899 20900 case SATA_PKT_QUEUE_FULL: 20901 *rval = SATA_TRAN_QUEUE_FULL; 20902 spkt->satapkt_reason = SATA_PKT_QUEUE_FULL; 20903 break; 20904 20905 case SATA_PKT_CMD_UNSUPPORTED: 20906 *rval = SATA_TRAN_CMD_UNSUPPORTED; 20907 spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED; 20908 break; 20909 20910 case SATA_PKT_PORT_ERROR: 20911 /* This is "rejected" command */ 20912 *rval = SATA_TRAN_PORT_ERROR; 20913 spkt->satapkt_reason = SATA_PKT_PORT_ERROR; 20914 /* Additional error setup could be done here - port state */ 20915 break; 20916 20917 case SATA_PKT_DEV_ERROR: 20918 spkt->satapkt_reason = SATA_PKT_DEV_ERROR; 20919 /* 20920 * Additional error setup could be done here 20921 */ 20922 break; 20923 20924 case SATA_PKT_ABORTED: 20925 spkt->satapkt_reason = SATA_PKT_ABORTED; 20926 break; 20927 20928 case SATA_PKT_TIMEOUT: 20929 spkt->satapkt_reason = SATA_PKT_TIMEOUT; 20930 /* Additional error setup could be done here */ 20931 break; 20932 20933 case SATA_PKT_RESET: 20934 spkt->satapkt_reason = SATA_PKT_RESET; 20935 /* 20936 * Additional error setup could be done here - device reset 20937 */ 20938 break; 20939 20940 default: 20941 break; 20942 } 20943 } 20944 20945 #endif 20946 20947 /* 20948 * SATA Trace Ring Buffer 20949 * ---------------------- 20950 * 20951 * Overview 20952 * 20953 * The SATA trace ring buffer is a ring buffer created and managed by 20954 * the SATA framework module that can be used by any module or driver 20955 * within the SATA framework to store debug messages. 20956 * 20957 * Ring Buffer Interfaces: 20958 * 20959 * sata_vtrace_debug() <-- Adds debug message to ring buffer 20960 * sata_trace_debug() <-- Wraps varargs into sata_vtrace_debug() 20961 * 20962 * Note that the sata_trace_debug() interface was created to give 20963 * consumers the flexibilty of sending debug messages to ring buffer 20964 * as variable arguments. Consumers can send type va_list debug 20965 * messages directly to sata_vtrace_debug(). The sata_trace_debug() 20966 * and sata_vtrace_debug() relationship is similar to that of 20967 * cmn_err(9F) and vcmn_err(9F). 20968 * 20969 * Below is a diagram of the SATA trace ring buffer interfaces and 20970 * sample consumers: 20971 * 20972 * +---------------------------------+ 20973 * | o o SATA Framework Module | 20974 * | o SATA o +------------------+ +------------------+ 20975 * |o Trace o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1| 20976 * |o R-Buf o |sata_trace_debug |<--+ +------------------+ 20977 * | o o +------------------+ | +------------------+ 20978 * | o o ^ | +--|SATA HBA Driver #2| 20979 * | | | +------------------+ 20980 * | +------------------+ | 20981 * | |SATA Debug Message| | 20982 * | +------------------+ | 20983 * +---------------------------------+ 20984 * 20985 * Supporting Routines: 20986 * 20987 * sata_trace_rbuf_alloc() <-- Initializes ring buffer 20988 * sata_trace_rbuf_free() <-- Destroys ring buffer 20989 * sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer 20990 * sata_trace_dmsg_free() <-- Destroys content of ring buffer 20991 * 20992 * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE. 20993 * The ring buffer size can be adjusted by setting dmsg_ring_size in 20994 * /etc/system to desired size in unit of bytes. 20995 * 20996 * The individual debug message size in the ring buffer is restricted 20997 * to DMSG_BUF_SIZE. 20998 */ 20999 void 21000 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap) 21001 { 21002 sata_trace_dmsg_t *dmsg; 21003 21004 if (sata_debug_rbuf == NULL) { 21005 return; 21006 } 21007 21008 /* 21009 * If max size of ring buffer is smaller than size 21010 * required for one debug message then just return 21011 * since we have no room for the debug message. 21012 */ 21013 if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) { 21014 return; 21015 } 21016 21017 mutex_enter(&sata_debug_rbuf->lock); 21018 21019 /* alloc or reuse on ring buffer */ 21020 dmsg = sata_trace_dmsg_alloc(); 21021 21022 if (dmsg == NULL) { 21023 /* resource allocation failed */ 21024 mutex_exit(&sata_debug_rbuf->lock); 21025 return; 21026 } 21027 21028 dmsg->dip = dip; 21029 gethrestime(&dmsg->timestamp); 21030 21031 (void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap); 21032 21033 mutex_exit(&sata_debug_rbuf->lock); 21034 } 21035 21036 void 21037 sata_trace_debug(dev_info_t *dip, const char *fmt, ...) 21038 { 21039 va_list ap; 21040 21041 va_start(ap, fmt); 21042 sata_vtrace_debug(dip, fmt, ap); 21043 va_end(ap); 21044 } 21045 21046 /* 21047 * This routine is used to manage debug messages 21048 * on ring buffer. 21049 */ 21050 static sata_trace_dmsg_t * 21051 sata_trace_dmsg_alloc(void) 21052 { 21053 sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp; 21054 21055 if (sata_debug_rbuf->looped == TRUE) { 21056 sata_debug_rbuf->dmsgp = dmsg->next; 21057 return (sata_debug_rbuf->dmsgp); 21058 } 21059 21060 /* 21061 * If we're looping for the first time, 21062 * connect the ring. 21063 */ 21064 if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) > 21065 sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) { 21066 dmsg->next = sata_debug_rbuf->dmsgh; 21067 sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh; 21068 sata_debug_rbuf->looped = TRUE; 21069 return (sata_debug_rbuf->dmsgp); 21070 } 21071 21072 /* If we've gotten this far then memory allocation is needed */ 21073 dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP); 21074 if (dmsg_alloc == NULL) { 21075 sata_debug_rbuf->allocfailed++; 21076 return (dmsg_alloc); 21077 } else { 21078 sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t); 21079 } 21080 21081 if (sata_debug_rbuf->dmsgp != NULL) { 21082 dmsg->next = dmsg_alloc; 21083 sata_debug_rbuf->dmsgp = dmsg->next; 21084 return (sata_debug_rbuf->dmsgp); 21085 } else { 21086 /* 21087 * We should only be here if we're initializing 21088 * the ring buffer. 21089 */ 21090 if (sata_debug_rbuf->dmsgh == NULL) { 21091 sata_debug_rbuf->dmsgh = dmsg_alloc; 21092 } else { 21093 /* Something is wrong */ 21094 kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t)); 21095 return (NULL); 21096 } 21097 21098 sata_debug_rbuf->dmsgp = dmsg_alloc; 21099 return (sata_debug_rbuf->dmsgp); 21100 } 21101 } 21102 21103 21104 /* 21105 * Free all messages on debug ring buffer. 21106 */ 21107 static void 21108 sata_trace_dmsg_free(void) 21109 { 21110 sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh; 21111 21112 while (dmsg != NULL) { 21113 dmsg_next = dmsg->next; 21114 kmem_free(dmsg, sizeof (sata_trace_dmsg_t)); 21115 21116 /* 21117 * If we've looped around the ring than we're done. 21118 */ 21119 if (dmsg_next == sata_debug_rbuf->dmsgh) { 21120 break; 21121 } else { 21122 dmsg = dmsg_next; 21123 } 21124 } 21125 } 21126 21127 21128 /* 21129 * This function can block 21130 */ 21131 static void 21132 sata_trace_rbuf_alloc(void) 21133 { 21134 sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP); 21135 21136 mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL); 21137 21138 if (dmsg_ring_size > 0) { 21139 sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size; 21140 } 21141 } 21142 21143 21144 static void 21145 sata_trace_rbuf_free(void) 21146 { 21147 sata_trace_dmsg_free(); 21148 mutex_destroy(&sata_debug_rbuf->lock); 21149 kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t)); 21150 } 21151 21152 /* 21153 * If SATA_DEBUG is not defined then this routine is called instead 21154 * of sata_log() via the SATA_LOG_D macro. 21155 */ 21156 static void 21157 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level, 21158 const char *fmt, ...) 21159 { 21160 #ifndef __lock_lint 21161 _NOTE(ARGUNUSED(level)) 21162 #endif 21163 21164 dev_info_t *dip = NULL; 21165 va_list ap; 21166 21167 if (sata_hba_inst != NULL) { 21168 dip = SATA_DIP(sata_hba_inst); 21169 } 21170 21171 va_start(ap, fmt); 21172 sata_vtrace_debug(dip, fmt, ap); 21173 va_end(ap); 21174 } 21175