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 2022 Racktop Systems, Inc. 30 * Copyright 2023 Oxide Computer Company 31 * Copyright 2023 Jason King 32 */ 33 34 /* 35 * SATA Framework 36 * Generic SATA Host Adapter Implementation 37 */ 38 39 #include <sys/conf.h> 40 #include <sys/file.h> 41 #include <sys/ddi.h> 42 #include <sys/sunddi.h> 43 #include <sys/modctl.h> 44 #include <sys/cmn_err.h> 45 #include <sys/errno.h> 46 #include <sys/thread.h> 47 #include <sys/kstat.h> 48 #include <sys/note.h> 49 #include <sys/sysevent.h> 50 #include <sys/sysevent/eventdefs.h> 51 #include <sys/sysevent/dr.h> 52 #include <sys/taskq.h> 53 #include <sys/disp.h> 54 #include <sys/sdt.h> 55 56 #include <sys/sata/impl/sata.h> 57 #include <sys/sata/sata_hba.h> 58 #include <sys/sata/sata_defs.h> 59 #include <sys/sata/sata_cfgadm.h> 60 #include <sys/sata/sata_blacklist.h> 61 #include <sys/sata/sata_satl.h> 62 63 #include <sys/scsi/impl/spc3_types.h> 64 65 /* 66 * FMA header files 67 */ 68 #include <sys/ddifm.h> 69 #include <sys/fm/protocol.h> 70 #include <sys/fm/util.h> 71 #include <sys/fm/io/ddi.h> 72 73 /* Debug flags - defined in sata.h */ 74 int sata_debug_flags = 0; 75 int sata_msg = 0; 76 77 /* 78 * Flags enabling selected SATA HBA framework functionality 79 */ 80 #define SATA_ENABLE_QUEUING 1 81 #define SATA_ENABLE_NCQ 2 82 #define SATA_ENABLE_PROCESS_EVENTS 4 83 #define SATA_ENABLE_PMULT_FBS 8 /* FIS-Based Switching */ 84 int sata_func_enable = 85 SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ; 86 87 /* 88 * Global variable setting default maximum queue depth (NCQ or TCQ) 89 * Note:minimum queue depth is 1 90 */ 91 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */ 92 93 /* 94 * Currently used default NCQ/TCQ queue depth. It is set-up during the driver 95 * initialization, using value from sata_max_queue_depth 96 * It is adjusted to minimum supported by the controller and by the device, 97 * if queueing is enabled. 98 */ 99 static int sata_current_max_qdepth; 100 101 /* 102 * Global variable determining the default behavior after device hotpluggin. 103 * If non-zero, the hotplugged device is onlined (if possible) without explicit 104 * IOCTL request (AP_CONFIGURE). 105 * If zero, hotplugged device is identified, but not onlined. 106 * Enabling (AP_CONNECT) device port with an attached device does not result 107 * in device onlining regardless of the flag setting 108 */ 109 int sata_auto_online = 0; 110 111 #ifdef SATA_DEBUG 112 113 #define SATA_LOG_D(args) sata_log args 114 uint64_t mbuf_count = 0; 115 uint64_t mbuffail_count = 0; 116 117 sata_atapi_cmd_t sata_atapi_trace[64]; 118 uint32_t sata_atapi_trace_index = 0; 119 int sata_atapi_trace_save = 1; 120 static void sata_save_atapi_trace(sata_pkt_txlate_t *, int); 121 #define SATAATAPITRACE(spx, count) \ 122 if (sata_atapi_trace_save) \ 123 sata_save_atapi_trace(spx, count) 124 125 #else 126 #define SATA_LOG_D(args) sata_trace_log args 127 #define SATAATAPITRACE(spx, count) 128 #endif 129 130 #if 0 131 static void 132 sata_test_atapi_packet_command(sata_hba_inst_t *, int); 133 #endif 134 135 #ifdef SATA_INJECT_FAULTS 136 137 #define SATA_INJECT_PKT_FAULT 1 138 uint32_t sata_inject_fault = 0; 139 140 uint32_t sata_inject_fault_count = 0; 141 uint32_t sata_inject_fault_pause_count = 0; 142 uint32_t sata_fault_type = 0; 143 uint32_t sata_fault_cmd = 0; 144 dev_info_t *sata_fault_ctrl = NULL; 145 sata_device_t sata_fault_device; 146 147 static void sata_inject_pkt_fault(sata_pkt_t *, int *, int); 148 149 #endif 150 151 #define LEGACY_HWID_LEN 64 /* Model (40) + Serial (20) + pad */ 152 153 /* 154 * SATA cb_ops functions 155 */ 156 static int sata_hba_open(dev_t *, int, int, cred_t *); 157 static int sata_hba_close(dev_t, int, int, cred_t *); 158 static int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 159 160 /* 161 * SCSA required entry points 162 */ 163 static int sata_scsi_tgt_init(dev_info_t *, dev_info_t *, 164 scsi_hba_tran_t *, struct scsi_device *); 165 static int sata_scsi_tgt_probe(struct scsi_device *, 166 int (*callback)(void)); 167 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *, 168 scsi_hba_tran_t *, struct scsi_device *); 169 static int sata_scsi_start(struct scsi_address *, struct scsi_pkt *); 170 static int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *); 171 static int sata_scsi_reset(struct scsi_address *, int); 172 static int sata_scsi_getcap(struct scsi_address *, char *, int); 173 static int sata_scsi_setcap(struct scsi_address *, char *, int, int); 174 static struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *, 175 struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t), 176 caddr_t); 177 static void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *); 178 static void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *); 179 static void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *); 180 181 /* 182 * SATA HBA interface functions are defined in sata_hba.h header file 183 */ 184 185 /* Event processing functions */ 186 static void sata_event_daemon(void *); 187 static void sata_event_thread_control(int); 188 static void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst); 189 static void sata_process_pmult_events(sata_hba_inst_t *, uint8_t); 190 static void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *); 191 static void sata_process_pmdevice_reset(sata_hba_inst_t *, sata_address_t *); 192 static void sata_process_port_failed_event(sata_hba_inst_t *, 193 sata_address_t *); 194 static void sata_process_port_link_events(sata_hba_inst_t *, 195 sata_address_t *); 196 static void sata_process_pmport_link_events(sata_hba_inst_t *, 197 sata_address_t *); 198 static void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *); 199 static void sata_process_pmdevice_detached(sata_hba_inst_t *, 200 sata_address_t *); 201 static void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *); 202 static void sata_process_pmdevice_attached(sata_hba_inst_t *, 203 sata_address_t *); 204 static void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *); 205 static void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *); 206 static void sata_process_target_node_cleanup(sata_hba_inst_t *, 207 sata_address_t *); 208 static void sata_process_device_autoonline(sata_hba_inst_t *, 209 sata_address_t *saddr); 210 211 /* 212 * Local translation functions 213 */ 214 static int sata_txlt_inquiry(sata_pkt_txlate_t *); 215 static int sata_txlt_test_unit_ready(sata_pkt_txlate_t *); 216 static int sata_txlt_start_stop_unit(sata_pkt_txlate_t *); 217 static int sata_txlt_read_capacity(sata_pkt_txlate_t *); 218 static int sata_txlt_read_capacity16(sata_pkt_txlate_t *); 219 static int sata_txlt_unmap(sata_pkt_txlate_t *); 220 static int sata_txlt_request_sense(sata_pkt_txlate_t *); 221 static int sata_txlt_read(sata_pkt_txlate_t *); 222 static int sata_txlt_write(sata_pkt_txlate_t *); 223 static int sata_txlt_log_sense(sata_pkt_txlate_t *); 224 static int sata_txlt_log_select(sata_pkt_txlate_t *); 225 static int sata_txlt_mode_sense(sata_pkt_txlate_t *); 226 static int sata_txlt_mode_select(sata_pkt_txlate_t *); 227 static int sata_txlt_ata_pass_thru(sata_pkt_txlate_t *); 228 static int sata_txlt_synchronize_cache(sata_pkt_txlate_t *); 229 static int sata_txlt_write_buffer(sata_pkt_txlate_t *); 230 static int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *); 231 232 static int sata_hba_start(sata_pkt_txlate_t *, int *); 233 static int sata_txlt_invalid_command(sata_pkt_txlate_t *); 234 static int sata_txlt_check_condition(sata_pkt_txlate_t *, uchar_t, uchar_t); 235 static int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *); 236 static int sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *); 237 static int sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *); 238 static void sata_txlt_rw_completion(sata_pkt_t *); 239 static void sata_txlt_nodata_cmd_completion(sata_pkt_t *); 240 static void sata_txlt_apt_completion(sata_pkt_t *sata_pkt); 241 static void sata_txlt_unmap_completion(sata_pkt_t *sata_pkt); 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_arq_sense(sata_pkt_txlate_t *); 246 247 static int sata_txlt_atapi(sata_pkt_txlate_t *); 248 static void sata_txlt_atapi_completion(sata_pkt_t *); 249 250 /* 251 * Local functions for ioctl 252 */ 253 static int32_t sata_get_port_num(sata_hba_inst_t *, struct devctl_iocdata *); 254 static void sata_cfgadm_state(sata_hba_inst_t *, int32_t, 255 devctl_ap_state_t *); 256 static dev_info_t *sata_get_target_dip(dev_info_t *, uint8_t, uint8_t); 257 static dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *); 258 static dev_info_t *sata_devt_to_devinfo(dev_t); 259 static int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *); 260 static int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *); 261 static int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *); 262 static int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *); 263 static int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *); 264 static int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *); 265 static int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *); 266 static int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *); 267 static int sata_ioctl_reset_all(sata_hba_inst_t *); 268 static int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *); 269 static int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *, 270 sata_ioctl_data_t *, int mode); 271 static int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *, 272 sata_ioctl_data_t *, int mode); 273 static int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *, 274 sata_ioctl_data_t *, int mode); 275 static int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *, 276 sata_ioctl_data_t *, int mode); 277 static int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *, 278 sata_device_t *, sata_ioctl_data_t *, int mode); 279 280 /* 281 * Local functions 282 */ 283 static void sata_remove_hba_instance(dev_info_t *); 284 static int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *); 285 static void sata_probe_ports(sata_hba_inst_t *); 286 static void sata_probe_pmports(sata_hba_inst_t *, uint8_t); 287 static int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int); 288 static int sata_reprobe_pmult(sata_hba_inst_t *, sata_device_t *, int); 289 static int sata_reprobe_pmport(sata_hba_inst_t *, sata_device_t *, int); 290 static int sata_alloc_pmult(sata_hba_inst_t *, sata_device_t *); 291 static void sata_free_pmult(sata_hba_inst_t *, sata_device_t *); 292 static int sata_add_device(dev_info_t *, sata_hba_inst_t *, sata_device_t *); 293 static int sata_offline_device(sata_hba_inst_t *, sata_device_t *, 294 sata_drive_info_t *); 295 static dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *, 296 sata_address_t *); 297 static void sata_remove_target_node(sata_hba_inst_t *, 298 sata_address_t *); 299 static int sata_validate_scsi_address(sata_hba_inst_t *, 300 struct scsi_address *, sata_device_t *); 301 static int sata_validate_sata_address(sata_hba_inst_t *, int, int, int); 302 static sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t)); 303 static void sata_pkt_free(sata_pkt_txlate_t *); 304 static int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t), 305 caddr_t, ddi_dma_attr_t *); 306 static void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *); 307 static int sata_probe_device(sata_hba_inst_t *, sata_device_t *); 308 static sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *, 309 sata_device_t *); 310 static int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *); 311 static void sata_reidentify_device(sata_pkt_txlate_t *); 312 static struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int); 313 static void sata_free_local_buffer(sata_pkt_txlate_t *); 314 static uint64_t sata_check_capacity(sata_drive_info_t *); 315 void sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *, 316 ddi_dma_attr_t *); 317 static int sata_fetch_device_identify_data(sata_hba_inst_t *, 318 sata_drive_info_t *); 319 static void sata_update_port_info(sata_hba_inst_t *, sata_device_t *); 320 static void sata_update_pmport_info(sata_hba_inst_t *, sata_device_t *); 321 static int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *); 322 static int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int); 323 static int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int); 324 static int sata_set_drive_features(sata_hba_inst_t *, 325 sata_drive_info_t *, int flag); 326 static void sata_init_write_cache_mode(sata_drive_info_t *sdinfo); 327 static int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *); 328 static void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *, 329 uint8_t *); 330 static int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *, 331 struct scsi_inquiry *); 332 static int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *); 333 static int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *); 334 static int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *); 335 static int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *); 336 static int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *); 337 static int sata_mode_select_page_8(sata_pkt_txlate_t *, 338 struct mode_cache_scsi3 *, int, int *, int *, int *); 339 static int sata_mode_select_page_1a(sata_pkt_txlate_t *, 340 struct mode_info_power_cond *, int, int *, int *, int *); 341 static int sata_mode_select_page_1c(sata_pkt_txlate_t *, 342 struct mode_info_excpt_page *, int, int *, int *, int *); 343 static int sata_mode_select_page_30(sata_pkt_txlate_t *, 344 struct mode_acoustic_management *, int, int *, int *, int *); 345 346 static int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *); 347 static int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *, 348 sata_hba_inst_t *); 349 static int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *, 350 sata_hba_inst_t *); 351 static int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *, 352 sata_hba_inst_t *); 353 static int sata_build_lsense_page_0e(sata_drive_info_t *, uint8_t *, 354 sata_pkt_txlate_t *); 355 356 static void sata_set_arq_data(sata_pkt_t *); 357 static void sata_build_read_verify_cmd(sata_cmd_t *, uint16_t, uint64_t); 358 static void sata_build_generic_cmd(sata_cmd_t *, uint8_t); 359 static uint8_t sata_get_standby_timer(uint8_t *timer); 360 361 static void sata_save_drive_settings(sata_drive_info_t *); 362 static void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *); 363 static void sata_show_pmult_info(sata_hba_inst_t *, sata_device_t *); 364 static void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...); 365 #ifndef SATA_DEBUG 366 static void sata_trace_log(sata_hba_inst_t *, uint_t, const char *fmt, ...); 367 #endif 368 static int sata_fetch_smart_return_status(sata_hba_inst_t *, 369 sata_drive_info_t *); 370 static int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *, 371 struct smart_data *); 372 static int sata_smart_selftest_log(sata_hba_inst_t *, 373 sata_drive_info_t *, 374 struct smart_selftest_log *); 375 static int sata_ext_smart_selftest_read_log(sata_hba_inst_t *, 376 sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t); 377 static int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *, 378 uint8_t *, uint8_t, uint8_t); 379 static int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *, 380 struct read_log_ext_directory *); 381 static void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int); 382 static void sata_xlate_errors(sata_pkt_txlate_t *); 383 static void sata_decode_device_error(sata_pkt_txlate_t *, 384 struct scsi_extended_sense *); 385 static void sata_set_device_removed(dev_info_t *); 386 static boolean_t sata_check_device_removed(dev_info_t *); 387 static void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *); 388 static int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *, 389 sata_drive_info_t *); 390 static int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *, 391 sata_drive_info_t *); 392 static void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *); 393 static void sata_fixed_sense_data_preset(struct scsi_extended_sense *); 394 static void sata_target_devid_register(dev_info_t *, sata_drive_info_t *); 395 static int sata_check_modser(char *, int); 396 397 /* 398 * FMA 399 */ 400 static boolean_t sata_check_for_dma_error(dev_info_t *, sata_pkt_txlate_t *); 401 402 403 /* 404 * SATA Framework will ignore SATA HBA driver cb_ops structure and 405 * register following one with SCSA framework. 406 * Open & close are provided, so scsi framework will not use its own 407 */ 408 static struct cb_ops sata_cb_ops = { 409 sata_hba_open, /* open */ 410 sata_hba_close, /* close */ 411 nodev, /* strategy */ 412 nodev, /* print */ 413 nodev, /* dump */ 414 nodev, /* read */ 415 nodev, /* write */ 416 sata_hba_ioctl, /* ioctl */ 417 nodev, /* devmap */ 418 nodev, /* mmap */ 419 nodev, /* segmap */ 420 nochpoll, /* chpoll */ 421 ddi_prop_op, /* cb_prop_op */ 422 0, /* streamtab */ 423 D_NEW | D_MP, /* cb_flag */ 424 CB_REV, /* rev */ 425 nodev, /* aread */ 426 nodev /* awrite */ 427 }; 428 429 430 extern struct mod_ops mod_miscops; 431 extern uchar_t scsi_cdb_size[]; 432 433 static struct modlmisc modlmisc = { 434 &mod_miscops, /* Type of module */ 435 "SATA Module" /* module name */ 436 }; 437 438 439 static struct modlinkage modlinkage = { 440 MODREV_1, 441 (void *)&modlmisc, 442 NULL 443 }; 444 445 /* 446 * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero, 447 * i.e. when scsi_pkt has not timeout specified. 448 */ 449 static int sata_default_pkt_time = 60; /* 60 seconds */ 450 451 /* 452 * Intermediate buffer device access attributes - they are required, 453 * but not necessarily used. 454 */ 455 static ddi_device_acc_attr_t sata_acc_attr = { 456 DDI_DEVICE_ATTR_V0, 457 DDI_STRUCTURE_LE_ACC, 458 DDI_STRICTORDER_ACC 459 }; 460 461 462 /* 463 * Mutexes protecting structures in multithreaded operations. 464 * Because events are relatively rare, a single global mutex protecting 465 * data structures should be sufficient. To increase performance, add 466 * separate mutex per each sata port and use global mutex only to protect 467 * common data structures. 468 */ 469 static kmutex_t sata_mutex; /* protects sata_hba_list */ 470 static kmutex_t sata_log_mutex; /* protects log */ 471 472 static char sata_log_buf[256]; 473 474 /* 475 * sata trace debug 476 */ 477 static sata_trace_rbuf_t *sata_debug_rbuf; 478 static sata_trace_dmsg_t *sata_trace_dmsg_alloc(void); 479 static void sata_trace_dmsg_free(void); 480 static void sata_trace_rbuf_alloc(void); 481 static void sata_trace_rbuf_free(void); 482 483 int dmsg_ring_size = DMSG_RING_SIZE; 484 485 /* Default write cache setting for SATA hard disks */ 486 int sata_write_cache = 1; /* enabled */ 487 488 /* Default write cache setting for SATA ATAPI CD/DVD */ 489 int sata_atapicdvd_write_cache = 1; /* enabled */ 490 491 /* Default write cache setting for SATA ATAPI tape */ 492 int sata_atapitape_write_cache = 1; /* enabled */ 493 494 /* Default write cache setting for SATA ATAPI disk */ 495 int sata_atapidisk_write_cache = 1; /* enabled */ 496 497 /* 498 * Linked list of HBA instances 499 */ 500 static sata_hba_inst_t *sata_hba_list = NULL; 501 static sata_hba_inst_t *sata_hba_list_tail = NULL; 502 /* 503 * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran 504 * structure and in sata soft state. 505 */ 506 507 /* 508 * Event daemon related variables 509 */ 510 static kmutex_t sata_event_mutex; 511 static kcondvar_t sata_event_cv; 512 static kthread_t *sata_event_thread = NULL; 513 static int sata_event_thread_terminate = 0; 514 static int sata_event_pending = 0; 515 static int sata_event_thread_active = 0; 516 extern pri_t minclsyspri; 517 518 /* 519 * NCQ error recovery command 520 */ 521 static const sata_cmd_t sata_rle_cmd = { 522 SATA_CMD_REV, 523 NULL, 524 { 525 SATA_DIR_READ 526 }, 527 ATA_ADDR_LBA48, 528 0, 529 0, 530 0, 531 0, 532 0, 533 1, 534 READ_LOG_EXT_NCQ_ERROR_RECOVERY, 535 0, 536 0, 537 0, 538 SATAC_READ_LOG_EXT, 539 0, 540 0, 541 0, 542 }; 543 544 /* 545 * ATAPI error recovery CDB 546 */ 547 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = { 548 SCMD_REQUEST_SENSE, 549 0, /* Only fixed RQ format is supported */ 550 0, 551 0, 552 SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */ 553 0 554 }; 555 556 557 /* Warlock directives */ 558 559 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran)) 560 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device)) 561 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops)) 562 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense)) 563 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status)) 564 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr)) 565 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t)) 566 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state)) 567 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state)) 568 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list)) 569 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list)) 570 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next)) 571 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev)) 572 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \ 573 sata_hba_inst::satahba_scsi_tran)) 574 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran)) 575 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip)) 576 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached)) 577 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port)) 578 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex, 579 sata_hba_inst::satahba_event_flags)) 580 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 581 sata_cport_info::cport_devp)) 582 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp)) 583 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr)) 584 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 585 sata_cport_info::cport_dev_type)) 586 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type)) 587 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 588 sata_cport_info::cport_state)) 589 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state)) 590 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 591 sata_pmport_info::pmport_state)) 592 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state)) 593 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 594 sata_pmport_info::pmport_dev_type)) 595 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type)) 596 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 597 sata_pmport_info::pmport_sata_drive)) 598 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 599 sata_pmport_info::pmport_tgtnode_clean)) 600 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \ 601 sata_pmport_info::pmport_event_flags)) 602 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive)) 603 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port)) 604 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports)) 605 #ifdef SATA_DEBUG 606 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count)) 607 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count)) 608 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace)) 609 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index)) 610 #endif 611 612 /* End of warlock directives */ 613 614 /* ************** loadable module configuration functions ************** */ 615 616 int 617 _init() 618 { 619 int rval; 620 621 mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL); 622 mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL); 623 mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL); 624 cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL); 625 sata_trace_rbuf_alloc(); 626 if ((rval = mod_install(&modlinkage)) != 0) { 627 #ifdef SATA_DEBUG 628 cmn_err(CE_WARN, "sata: _init: mod_install failed\n"); 629 #endif 630 sata_trace_rbuf_free(); 631 mutex_destroy(&sata_log_mutex); 632 cv_destroy(&sata_event_cv); 633 mutex_destroy(&sata_event_mutex); 634 mutex_destroy(&sata_mutex); 635 } 636 return (rval); 637 } 638 639 int 640 _fini() 641 { 642 int rval; 643 644 if ((rval = mod_remove(&modlinkage)) != 0) 645 return (rval); 646 647 sata_trace_rbuf_free(); 648 mutex_destroy(&sata_log_mutex); 649 cv_destroy(&sata_event_cv); 650 mutex_destroy(&sata_event_mutex); 651 mutex_destroy(&sata_mutex); 652 return (rval); 653 } 654 655 int 656 _info(struct modinfo *modinfop) 657 { 658 return (mod_info(&modlinkage, modinfop)); 659 } 660 661 662 663 /* ********************* SATA HBA entry points ********************* */ 664 665 666 /* 667 * Called by SATA HBA from _init(). 668 * Registers HBA driver instance/sata framework pair with scsi framework, by 669 * calling scsi_hba_init(). 670 * 671 * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used 672 * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver 673 * cb_ops pointer in SATA HBA driver dev_ops structure. 674 * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors. 675 * 676 * Return status of the scsi_hba_init() is returned to a calling SATA HBA 677 * driver. 678 */ 679 int 680 sata_hba_init(struct modlinkage *modlp) 681 { 682 int rval; 683 struct dev_ops *hba_ops; 684 685 SATADBG1(SATA_DBG_HBA_IF, NULL, 686 "sata_hba_init: name %s \n", 687 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo); 688 /* 689 * Fill-up cb_ops and dev_ops when necessary 690 */ 691 hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops; 692 /* 693 * Provide pointer to SATA dev_ops 694 */ 695 hba_ops->devo_cb_ops = &sata_cb_ops; 696 697 /* 698 * Register SATA HBA with SCSI framework 699 */ 700 if ((rval = scsi_hba_init(modlp)) != 0) { 701 SATADBG1(SATA_DBG_HBA_IF, NULL, 702 "sata_hba_init: scsi hba init failed\n", NULL); 703 return (rval); 704 } 705 706 return (0); 707 } 708 709 710 /* HBA attach stages */ 711 #define HBA_ATTACH_STAGE_SATA_HBA_INST 1 712 #define HBA_ATTACH_STAGE_SCSI_ATTACHED 2 713 #define HBA_ATTACH_STAGE_SETUP 4 714 #define HBA_ATTACH_STAGE_LINKED 8 715 716 717 /* 718 * 719 * Called from SATA HBA driver's attach routine to attach an instance of 720 * the HBA. 721 * 722 * For DDI_ATTACH command: 723 * sata_hba_inst structure is allocated here and initialized with pointers to 724 * SATA framework implementation of required scsi tran functions. 725 * The scsi_tran's tran_hba_private field is used by SATA Framework to point 726 * to the soft structure (sata_hba_inst) allocated by SATA framework for 727 * SATA HBA instance related data. 728 * The scsi_tran's tran_hba_private field is used by SATA framework to 729 * store a pointer to per-HBA-instance of sata_hba_inst structure. 730 * The sata_hba_inst structure is cross-linked to scsi tran structure. 731 * Among other info, a pointer to sata_hba_tran structure is stored in 732 * sata_hba_inst. The sata_hba_inst structures for different HBA instances are 733 * linked together into the list, pointed to by sata_hba_list. 734 * On the first HBA instance attach the sata event thread is initialized. 735 * Attachment points are created for all SATA ports of the HBA being attached. 736 * All HBA instance's SATA ports are probed and type of plugged devices is 737 * determined. For each device of a supported type, a target node is created. 738 * 739 * DDI_SUCCESS is returned when attachment process is successful, 740 * DDI_FAILURE is returned otherwise. 741 * 742 * For DDI_RESUME command: 743 * Not implemented at this time (postponed until phase 2 of the development). 744 */ 745 int 746 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran, 747 ddi_attach_cmd_t cmd) 748 { 749 sata_hba_inst_t *sata_hba_inst; 750 scsi_hba_tran_t *scsi_tran = NULL; 751 int hba_attach_state = 0; 752 char taskq_name[MAXPATHLEN]; 753 754 SATADBG3(SATA_DBG_HBA_IF, NULL, 755 "sata_hba_attach: node %s (%s%d)\n", 756 ddi_node_name(dip), ddi_driver_name(dip), 757 ddi_get_instance(dip)); 758 759 if (cmd == DDI_RESUME) { 760 /* 761 * Postponed until phase 2 of the development 762 */ 763 return (DDI_FAILURE); 764 } 765 766 if (cmd != DDI_ATTACH) { 767 return (DDI_FAILURE); 768 } 769 770 /* cmd == DDI_ATTACH */ 771 772 if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) { 773 SATA_LOG_D((NULL, CE_WARN, 774 "sata_hba_attach: invalid sata_hba_tran")); 775 return (DDI_FAILURE); 776 } 777 /* 778 * Allocate and initialize SCSI tran structure. 779 * SATA copy of tran_bus_config is provided to create port nodes. 780 */ 781 scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP); 782 if (scsi_tran == NULL) 783 return (DDI_FAILURE); 784 /* 785 * Allocate soft structure for SATA HBA instance. 786 * There is a separate softstate for each HBA instance. 787 */ 788 sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP); 789 ASSERT(sata_hba_inst != NULL); /* this should not fail */ 790 mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL); 791 hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST; 792 793 /* 794 * scsi_trans's tran_hba_private is used by SATA Framework to point to 795 * soft structure allocated by SATA framework for 796 * SATA HBA instance related data. 797 */ 798 scsi_tran->tran_hba_private = sata_hba_inst; 799 scsi_tran->tran_tgt_private = NULL; 800 801 scsi_tran->tran_tgt_init = sata_scsi_tgt_init; 802 scsi_tran->tran_tgt_probe = sata_scsi_tgt_probe; 803 scsi_tran->tran_tgt_free = sata_scsi_tgt_free; 804 805 scsi_tran->tran_start = sata_scsi_start; 806 scsi_tran->tran_reset = sata_scsi_reset; 807 scsi_tran->tran_abort = sata_scsi_abort; 808 scsi_tran->tran_getcap = sata_scsi_getcap; 809 scsi_tran->tran_setcap = sata_scsi_setcap; 810 scsi_tran->tran_init_pkt = sata_scsi_init_pkt; 811 scsi_tran->tran_destroy_pkt = sata_scsi_destroy_pkt; 812 813 scsi_tran->tran_dmafree = sata_scsi_dmafree; 814 scsi_tran->tran_sync_pkt = sata_scsi_sync_pkt; 815 816 scsi_tran->tran_reset_notify = NULL; 817 scsi_tran->tran_get_bus_addr = NULL; 818 scsi_tran->tran_quiesce = NULL; 819 scsi_tran->tran_unquiesce = NULL; 820 scsi_tran->tran_bus_reset = NULL; 821 822 if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr, 823 scsi_tran, 0) != DDI_SUCCESS) { 824 #ifdef SATA_DEBUG 825 cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed", 826 ddi_driver_name(dip), ddi_get_instance(dip)); 827 #endif 828 goto fail; 829 } 830 hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED; 831 832 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) { 833 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, 834 "sata", 1) != DDI_PROP_SUCCESS) { 835 SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: " 836 "failed to create hba sata prop")); 837 goto fail; 838 } 839 } 840 841 /* 842 * Save pointers in hba instance soft state. 843 */ 844 sata_hba_inst->satahba_scsi_tran = scsi_tran; 845 sata_hba_inst->satahba_tran = sata_tran; 846 sata_hba_inst->satahba_dip = dip; 847 848 /* 849 * Create a task queue to handle emulated commands completion 850 * Use node name, dash, instance number as the queue name. 851 */ 852 taskq_name[0] = '\0'; 853 (void) strlcat(taskq_name, DEVI(dip)->devi_node_name, 854 sizeof (taskq_name)); 855 (void) snprintf(taskq_name + strlen(taskq_name), 856 sizeof (taskq_name) - strlen(taskq_name), 857 "-%d", DEVI(dip)->devi_instance); 858 sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1, 859 minclsyspri, 1, sata_tran->sata_tran_hba_num_cports * 4, 860 TASKQ_DYNAMIC); 861 862 hba_attach_state |= HBA_ATTACH_STAGE_SETUP; 863 864 /* 865 * Create events thread if not created yet. 866 */ 867 sata_event_thread_control(1); 868 869 /* 870 * Link this hba instance into the list. 871 */ 872 mutex_enter(&sata_mutex); 873 874 if (sata_hba_list == NULL) { 875 /* 876 * The first instance of HBA is attached. 877 * Set current/active default maximum NCQ/TCQ queue depth for 878 * all SATA devices. It is done here and now, to eliminate the 879 * possibility of the dynamic, programatic modification of the 880 * queue depth via global (and public) sata_max_queue_depth 881 * variable (this would require special handling in HBA drivers) 882 */ 883 sata_current_max_qdepth = sata_max_queue_depth; 884 if (sata_current_max_qdepth > 32) 885 sata_current_max_qdepth = 32; 886 else if (sata_current_max_qdepth < 1) 887 sata_current_max_qdepth = 1; 888 } 889 890 sata_hba_inst->satahba_next = NULL; 891 sata_hba_inst->satahba_prev = sata_hba_list_tail; 892 if (sata_hba_list == NULL) { 893 sata_hba_list = sata_hba_inst; 894 } 895 if (sata_hba_list_tail != NULL) { 896 sata_hba_list_tail->satahba_next = sata_hba_inst; 897 } 898 sata_hba_list_tail = sata_hba_inst; 899 mutex_exit(&sata_mutex); 900 hba_attach_state |= HBA_ATTACH_STAGE_LINKED; 901 902 /* 903 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl 904 * SATA HBA driver should not use its own open/close entry points. 905 * 906 * Make sure that instance number doesn't overflow 907 * when forming minor numbers. 908 */ 909 ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT)); 910 if (ddi_create_minor_node(dip, "devctl", S_IFCHR, 911 INST2DEVCTL(ddi_get_instance(dip)), 912 DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) { 913 #ifdef SATA_DEBUG 914 cmn_err(CE_WARN, "sata_hba_attach: " 915 "cannot create devctl minor node"); 916 #endif 917 goto fail; 918 } 919 920 921 /* 922 * Set-up kstats here, if necessary. 923 * (postponed until future phase of the development). 924 */ 925 926 /* 927 * Indicate that HBA is attached. This will enable events processing 928 * for this HBA. 929 */ 930 sata_hba_inst->satahba_attached = 1; 931 /* 932 * Probe controller ports. This operation will describe a current 933 * controller/port/multipliers/device configuration and will create 934 * attachment points. 935 * We may end-up with just a controller with no devices attached. 936 * For the ports with a supported device attached, device target nodes 937 * are created and devices are initialized. 938 */ 939 sata_probe_ports(sata_hba_inst); 940 941 return (DDI_SUCCESS); 942 943 fail: 944 if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) { 945 (void) sata_remove_hba_instance(dip); 946 if (sata_hba_list == NULL) 947 sata_event_thread_control(0); 948 } 949 950 if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) { 951 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata"); 952 taskq_destroy(sata_hba_inst->satahba_taskq); 953 } 954 955 if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED) 956 (void) scsi_hba_detach(dip); 957 958 if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) { 959 mutex_destroy(&sata_hba_inst->satahba_mutex); 960 kmem_free((void *)sata_hba_inst, 961 sizeof (struct sata_hba_inst)); 962 scsi_hba_tran_free(scsi_tran); 963 } 964 965 sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed", 966 ddi_driver_name(dip), ddi_get_instance(dip)); 967 968 return (DDI_FAILURE); 969 } 970 971 972 /* 973 * Called by SATA HBA from to detach an instance of the driver. 974 * 975 * For DDI_DETACH command: 976 * Free local structures allocated for SATA HBA instance during 977 * sata_hba_attach processing. 978 * 979 * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise. 980 * 981 * For DDI_SUSPEND command: 982 * Not implemented at this time (postponed until phase 2 of the development) 983 * Returnd DDI_SUCCESS. 984 * 985 * When the last HBA instance is detached, the event daemon is terminated. 986 * 987 * NOTE: Port multiplier is supported. 988 */ 989 int 990 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 991 { 992 dev_info_t *tdip; 993 sata_hba_inst_t *sata_hba_inst; 994 scsi_hba_tran_t *scsi_hba_tran; 995 sata_cport_info_t *cportinfo; 996 sata_pmult_info_t *pminfo; 997 sata_drive_info_t *sdinfo; 998 sata_device_t sdevice; 999 int ncport, npmport; 1000 1001 SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n", 1002 ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip)); 1003 1004 switch (cmd) { 1005 case DDI_DETACH: 1006 1007 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1008 return (DDI_FAILURE); 1009 1010 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1011 if (sata_hba_inst == NULL) 1012 return (DDI_FAILURE); 1013 1014 if (scsi_hba_detach(dip) == DDI_FAILURE) { 1015 sata_hba_inst->satahba_attached = 1; 1016 return (DDI_FAILURE); 1017 } 1018 1019 /* 1020 * Free all target nodes - at this point 1021 * devices should be at least offlined 1022 * otherwise scsi_hba_detach() should not be called. 1023 */ 1024 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); 1025 ncport++) { 1026 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 1027 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 1028 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 1029 if (sdinfo != NULL) { 1030 tdip = sata_get_target_dip(dip, 1031 ncport, 0); 1032 if (tdip != NULL) { 1033 if (ndi_devi_offline(tdip, 1034 NDI_DEVI_REMOVE) != 1035 NDI_SUCCESS) { 1036 SATA_LOG_D(( 1037 sata_hba_inst, 1038 CE_WARN, 1039 "sata_hba_detach: " 1040 "Target node not " 1041 "removed !")); 1042 return (DDI_FAILURE); 1043 } 1044 } 1045 } 1046 } else { /* SATA_DTYPE_PMULT */ 1047 mutex_enter(&cportinfo->cport_mutex); 1048 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 1049 1050 if (pminfo == NULL) { 1051 SATA_LOG_D((sata_hba_inst, CE_WARN, 1052 "sata_hba_detach: Port multiplier " 1053 "not ready yet!")); 1054 mutex_exit(&cportinfo->cport_mutex); 1055 return (DDI_FAILURE); 1056 } 1057 1058 /* 1059 * Detach would fail if removal of any of the 1060 * target nodes is failed - albeit in that 1061 * case some of them may have been removed. 1062 */ 1063 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 1064 sata_hba_inst, ncport); npmport++) { 1065 tdip = sata_get_target_dip(dip, ncport, 1066 npmport); 1067 if (tdip != NULL) { 1068 if (ndi_devi_offline(tdip, 1069 NDI_DEVI_REMOVE) != 1070 NDI_SUCCESS) { 1071 SATA_LOG_D(( 1072 sata_hba_inst, 1073 CE_WARN, 1074 "sata_hba_detach: " 1075 "Target node not " 1076 "removed !")); 1077 mutex_exit(&cportinfo-> 1078 cport_mutex); 1079 return (DDI_FAILURE); 1080 } 1081 } 1082 } 1083 mutex_exit(&cportinfo->cport_mutex); 1084 } 1085 } 1086 /* 1087 * Disable sata event daemon processing for this HBA 1088 */ 1089 sata_hba_inst->satahba_attached = 0; 1090 1091 /* 1092 * Remove event daemon thread, if it is last HBA instance. 1093 */ 1094 1095 mutex_enter(&sata_mutex); 1096 if (sata_hba_list->satahba_next == NULL) { 1097 mutex_exit(&sata_mutex); 1098 sata_event_thread_control(0); 1099 mutex_enter(&sata_mutex); 1100 } 1101 mutex_exit(&sata_mutex); 1102 1103 /* Remove this HBA instance from the HBA list */ 1104 sata_remove_hba_instance(dip); 1105 1106 /* 1107 * At this point there should be no target nodes attached. 1108 * Detach and destroy device and port info structures. 1109 */ 1110 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); 1111 ncport++) { 1112 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 1113 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 1114 sdinfo = 1115 cportinfo->cport_devp.cport_sata_drive; 1116 if (sdinfo != NULL) { 1117 /* Release device structure */ 1118 kmem_free(sdinfo, 1119 sizeof (sata_drive_info_t)); 1120 } 1121 /* Release cport info */ 1122 mutex_destroy(&cportinfo->cport_mutex); 1123 kmem_free(cportinfo, 1124 sizeof (sata_cport_info_t)); 1125 } else { /* SATA_DTYPE_PMULT */ 1126 sdevice.satadev_addr.cport = (uint8_t)ncport; 1127 sdevice.satadev_addr.qual = SATA_ADDR_PMULT; 1128 sata_free_pmult(sata_hba_inst, &sdevice); 1129 } 1130 } 1131 1132 scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran); 1133 1134 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata"); 1135 1136 taskq_destroy(sata_hba_inst->satahba_taskq); 1137 1138 mutex_destroy(&sata_hba_inst->satahba_mutex); 1139 kmem_free((void *)sata_hba_inst, 1140 sizeof (struct sata_hba_inst)); 1141 1142 return (DDI_SUCCESS); 1143 1144 case DDI_SUSPEND: 1145 /* 1146 * Postponed until phase 2 1147 */ 1148 return (DDI_FAILURE); 1149 1150 default: 1151 return (DDI_FAILURE); 1152 } 1153 } 1154 1155 1156 /* 1157 * Called by an HBA drive from _fini() routine. 1158 * Unregisters SATA HBA instance/SATA framework pair from the scsi framework. 1159 */ 1160 void 1161 sata_hba_fini(struct modlinkage *modlp) 1162 { 1163 SATADBG1(SATA_DBG_HBA_IF, NULL, 1164 "sata_hba_fini: name %s\n", 1165 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo); 1166 1167 scsi_hba_fini(modlp); 1168 } 1169 1170 1171 /* 1172 * Default open and close routine for sata_hba framework. 1173 * 1174 */ 1175 /* 1176 * Open devctl node. 1177 * 1178 * Returns: 1179 * 0 if node was open successfully, error code otherwise. 1180 * 1181 * 1182 */ 1183 1184 static int 1185 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp) 1186 { 1187 #ifndef __lock_lint 1188 _NOTE(ARGUNUSED(credp)) 1189 #endif 1190 int rv = 0; 1191 dev_info_t *dip; 1192 scsi_hba_tran_t *scsi_hba_tran; 1193 sata_hba_inst_t *sata_hba_inst; 1194 1195 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL); 1196 1197 if (otyp != OTYP_CHR) 1198 return (EINVAL); 1199 1200 dip = sata_devt_to_devinfo(*devp); 1201 if (dip == NULL) 1202 return (ENXIO); 1203 1204 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1205 return (ENXIO); 1206 1207 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1208 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0) 1209 return (ENXIO); 1210 1211 mutex_enter(&sata_mutex); 1212 if (flags & FEXCL) { 1213 if (sata_hba_inst->satahba_open_flag != 0) { 1214 rv = EBUSY; 1215 } else { 1216 sata_hba_inst->satahba_open_flag = 1217 SATA_DEVCTL_EXOPENED; 1218 } 1219 } else { 1220 if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) { 1221 rv = EBUSY; 1222 } else { 1223 sata_hba_inst->satahba_open_flag = 1224 SATA_DEVCTL_SOPENED; 1225 } 1226 } 1227 mutex_exit(&sata_mutex); 1228 1229 return (rv); 1230 } 1231 1232 1233 /* 1234 * Close devctl node. 1235 * Returns: 1236 * 0 if node was closed successfully, error code otherwise. 1237 * 1238 */ 1239 1240 static int 1241 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp) 1242 { 1243 #ifndef __lock_lint 1244 _NOTE(ARGUNUSED(credp)) 1245 _NOTE(ARGUNUSED(flag)) 1246 #endif 1247 dev_info_t *dip; 1248 scsi_hba_tran_t *scsi_hba_tran; 1249 sata_hba_inst_t *sata_hba_inst; 1250 1251 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL); 1252 1253 if (otyp != OTYP_CHR) 1254 return (EINVAL); 1255 1256 dip = sata_devt_to_devinfo(dev); 1257 if (dip == NULL) 1258 return (ENXIO); 1259 1260 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1261 return (ENXIO); 1262 1263 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1264 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0) 1265 return (ENXIO); 1266 1267 mutex_enter(&sata_mutex); 1268 sata_hba_inst->satahba_open_flag = 0; 1269 mutex_exit(&sata_mutex); 1270 return (0); 1271 } 1272 1273 1274 1275 /* 1276 * Standard IOCTL commands for SATA hotplugging. 1277 * Implemented DEVCTL_AP commands: 1278 * DEVCTL_AP_CONNECT 1279 * DEVCTL_AP_DISCONNECT 1280 * DEVCTL_AP_CONFIGURE 1281 * DEVCTL_UNCONFIGURE 1282 * DEVCTL_AP_CONTROL 1283 * 1284 * Commands passed to default ndi ioctl handler: 1285 * DEVCTL_DEVICE_GETSTATE 1286 * DEVCTL_DEVICE_ONLINE 1287 * DEVCTL_DEVICE_OFFLINE 1288 * DEVCTL_DEVICE_REMOVE 1289 * DEVCTL_DEVICE_INSERT 1290 * DEVCTL_BUS_GETSTATE 1291 * 1292 * All other cmds are passed to HBA if it provide ioctl handler, or failed 1293 * if not. 1294 * 1295 * Returns: 1296 * 0 if successful, 1297 * error code if operation failed. 1298 * 1299 * Port Multiplier support is supported now. 1300 * 1301 * NOTE: qual should be SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT 1302 */ 1303 1304 static int 1305 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, 1306 int *rvalp) 1307 { 1308 #ifndef __lock_lint 1309 _NOTE(ARGUNUSED(credp)) 1310 _NOTE(ARGUNUSED(rvalp)) 1311 #endif 1312 int rv = 0; 1313 int32_t comp_port = -1; 1314 dev_info_t *dip; 1315 devctl_ap_state_t ap_state; 1316 struct devctl_iocdata *dcp = NULL; 1317 scsi_hba_tran_t *scsi_hba_tran; 1318 sata_hba_inst_t *sata_hba_inst; 1319 sata_device_t sata_device; 1320 sata_cport_info_t *cportinfo; 1321 int cport, pmport, qual; 1322 int rval = SATA_SUCCESS; 1323 1324 dip = sata_devt_to_devinfo(dev); 1325 if (dip == NULL) 1326 return (ENXIO); 1327 1328 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1329 return (ENXIO); 1330 1331 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1332 if (sata_hba_inst == NULL) 1333 return (ENXIO); 1334 1335 if (sata_hba_inst->satahba_tran == NULL) 1336 return (ENXIO); 1337 1338 switch (cmd) { 1339 1340 case DEVCTL_DEVICE_GETSTATE: 1341 case DEVCTL_DEVICE_ONLINE: 1342 case DEVCTL_DEVICE_OFFLINE: 1343 case DEVCTL_DEVICE_REMOVE: 1344 case DEVCTL_BUS_GETSTATE: 1345 /* 1346 * There may be more cases that we want to pass to default 1347 * handler rather than fail them. 1348 */ 1349 return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0)); 1350 } 1351 1352 cport = pmport = qual = 0; 1353 cportinfo = NULL; 1354 1355 /* read devctl ioctl data */ 1356 if (cmd != DEVCTL_AP_CONTROL && IS_DEVCTL(cmd)) { 1357 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) 1358 return (EFAULT); 1359 1360 if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) == 1361 -1) { 1362 if (dcp) 1363 ndi_dc_freehdl(dcp); 1364 return (EINVAL); 1365 } 1366 1367 /* 1368 * According to SCSI_TO_SATA_ADDR_QUAL, qual should be either 1369 * SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT. 1370 */ 1371 cport = SCSI_TO_SATA_CPORT(comp_port); 1372 pmport = SCSI_TO_SATA_PMPORT(comp_port); 1373 qual = SCSI_TO_SATA_ADDR_QUAL(comp_port); 1374 1375 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, 1376 qual) != 0) { 1377 ndi_dc_freehdl(dcp); 1378 return (EINVAL); 1379 } 1380 1381 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 1382 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1383 cport_mutex); 1384 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) { 1385 /* 1386 * Cannot process ioctl request now. Come back later. 1387 */ 1388 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1389 cport_mutex); 1390 ndi_dc_freehdl(dcp); 1391 return (EBUSY); 1392 } 1393 /* Block event processing for this port */ 1394 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 1395 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1396 1397 sata_device.satadev_addr.cport = cport; 1398 sata_device.satadev_addr.pmport = pmport; 1399 sata_device.satadev_addr.qual = qual; 1400 sata_device.satadev_rev = SATA_DEVICE_REV; 1401 } 1402 1403 switch (cmd) { 1404 1405 case DEVCTL_AP_DISCONNECT: 1406 1407 /* 1408 * Normally, cfgadm sata plugin will try to offline 1409 * (unconfigure) device before this request. Nevertheless, 1410 * if a device is still configured, we need to 1411 * attempt to offline and unconfigure device first, and we will 1412 * deactivate the port regardless of the unconfigure 1413 * operation results. 1414 * 1415 */ 1416 rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device); 1417 1418 break; 1419 1420 case DEVCTL_AP_UNCONFIGURE: 1421 1422 /* 1423 * The unconfigure operation uses generic nexus operation to 1424 * offline a device. It leaves a target device node attached. 1425 * and obviously sata_drive_info attached as well, because 1426 * from the hardware point of view nothing has changed. 1427 */ 1428 rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device); 1429 break; 1430 1431 case DEVCTL_AP_CONNECT: 1432 { 1433 /* 1434 * The sata cfgadm pluging will invoke this operation only if 1435 * port was found in the disconnect state (failed state 1436 * is also treated as the disconnected state). 1437 * If port activation is successful and a device is found 1438 * attached to the port, the initialization sequence is 1439 * executed to probe the port and attach 1440 * a device structure to a port structure. The device is not 1441 * set in configured state (system-wise) by this operation. 1442 */ 1443 1444 rv = sata_ioctl_connect(sata_hba_inst, &sata_device); 1445 1446 break; 1447 } 1448 1449 case DEVCTL_AP_CONFIGURE: 1450 { 1451 /* 1452 * A port may be in an active or shutdown state. 1453 * If port is in a failed state, operation is aborted. 1454 * If a port is in a shutdown state, sata_tran_port_activate() 1455 * is invoked prior to any other operation. 1456 * 1457 * Onlining the device involves creating a new target node. 1458 * If there is an old target node present (belonging to 1459 * previously removed device), the operation is aborted - the 1460 * old node has to be released and removed before configure 1461 * operation is attempted. 1462 */ 1463 1464 rv = sata_ioctl_configure(sata_hba_inst, &sata_device); 1465 1466 break; 1467 } 1468 1469 case DEVCTL_AP_GETSTATE: 1470 1471 sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state); 1472 1473 ap_state.ap_last_change = (time_t)-1; 1474 ap_state.ap_error_code = 0; 1475 ap_state.ap_in_transition = 0; 1476 1477 /* Copy the return AP-state information to the user space */ 1478 if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) { 1479 rv = EFAULT; 1480 } 1481 break; 1482 1483 case DEVCTL_AP_CONTROL: 1484 { 1485 /* 1486 * Generic devctl for hardware specific functionality 1487 */ 1488 sata_ioctl_data_t ioc; 1489 1490 ASSERT(dcp == NULL); 1491 1492 /* Copy in user ioctl data first */ 1493 #ifdef _MULTI_DATAMODEL 1494 if (ddi_model_convert_from(mode & FMODELS) == 1495 DDI_MODEL_ILP32) { 1496 1497 sata_ioctl_data_32_t ioc32; 1498 1499 if (ddi_copyin((void *)arg, (void *)&ioc32, 1500 sizeof (ioc32), mode) != 0) { 1501 rv = EFAULT; 1502 break; 1503 } 1504 ioc.cmd = (uint_t)ioc32.cmd; 1505 ioc.port = (uint_t)ioc32.port; 1506 ioc.get_size = (uint_t)ioc32.get_size; 1507 ioc.buf = (caddr_t)(uintptr_t)ioc32.buf; 1508 ioc.bufsiz = (uint_t)ioc32.bufsiz; 1509 ioc.misc_arg = (uint_t)ioc32.misc_arg; 1510 } else 1511 #endif /* _MULTI_DATAMODEL */ 1512 if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc), 1513 mode) != 0) { 1514 return (EFAULT); 1515 } 1516 1517 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 1518 "sata_hba_ioctl: DEVCTL_AP_CONTROL " 1519 "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port); 1520 1521 /* 1522 * To avoid BE/LE and 32/64 issues, a get_size always returns 1523 * a 32-bit number. 1524 */ 1525 if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) { 1526 return (EINVAL); 1527 } 1528 /* validate address */ 1529 cport = SCSI_TO_SATA_CPORT(ioc.port); 1530 pmport = SCSI_TO_SATA_PMPORT(ioc.port); 1531 qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port); 1532 1533 SATADBG3(SATA_DBG_IOCTL_IF, sata_hba_inst, 1534 "sata_hba_ioctl: target port is %d:%d (%d)", 1535 cport, pmport, qual); 1536 1537 if (sata_validate_sata_address(sata_hba_inst, cport, 1538 pmport, qual) != 0) 1539 return (EINVAL); 1540 1541 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 1542 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1543 cport_mutex); 1544 /* Is the port locked by event processing daemon ? */ 1545 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) { 1546 /* 1547 * Cannot process ioctl request now. Come back later 1548 */ 1549 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1550 cport_mutex); 1551 return (EBUSY); 1552 } 1553 /* Block event processing for this port */ 1554 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 1555 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1556 1557 1558 sata_device.satadev_addr.cport = cport; 1559 sata_device.satadev_addr.pmport = pmport; 1560 sata_device.satadev_addr.qual = qual; 1561 sata_device.satadev_rev = SATA_DEVICE_REV; 1562 1563 switch (ioc.cmd) { 1564 1565 case SATA_CFGA_RESET_PORT: 1566 /* 1567 * There is no protection for configured device. 1568 */ 1569 rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device); 1570 break; 1571 1572 case SATA_CFGA_RESET_DEVICE: 1573 /* 1574 * There is no protection for configured device. 1575 */ 1576 rv = sata_ioctl_reset_device(sata_hba_inst, 1577 &sata_device); 1578 break; 1579 1580 case SATA_CFGA_RESET_ALL: 1581 /* 1582 * There is no protection for configured devices. 1583 */ 1584 rv = sata_ioctl_reset_all(sata_hba_inst); 1585 /* 1586 * We return here, because common return is for 1587 * a single port operation - we have already unlocked 1588 * all ports and no dc handle was allocated. 1589 */ 1590 return (rv); 1591 1592 case SATA_CFGA_PORT_DEACTIVATE: 1593 /* 1594 * Arbitrarily unconfigure attached device, if any. 1595 * Even if the unconfigure fails, proceed with the 1596 * port deactivation. 1597 */ 1598 rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device); 1599 1600 break; 1601 1602 case SATA_CFGA_PORT_ACTIVATE: 1603 1604 rv = sata_ioctl_activate(sata_hba_inst, &sata_device); 1605 break; 1606 1607 case SATA_CFGA_PORT_SELF_TEST: 1608 1609 rv = sata_ioctl_port_self_test(sata_hba_inst, 1610 &sata_device); 1611 break; 1612 1613 case SATA_CFGA_GET_DEVICE_PATH: 1614 1615 rv = sata_ioctl_get_device_path(sata_hba_inst, 1616 &sata_device, &ioc, mode); 1617 break; 1618 1619 case SATA_CFGA_GET_AP_TYPE: 1620 1621 rv = sata_ioctl_get_ap_type(sata_hba_inst, 1622 &sata_device, &ioc, mode); 1623 break; 1624 1625 case SATA_CFGA_GET_MODEL_INFO: 1626 1627 rv = sata_ioctl_get_model_info(sata_hba_inst, 1628 &sata_device, &ioc, mode); 1629 break; 1630 1631 case SATA_CFGA_GET_REVFIRMWARE_INFO: 1632 1633 rv = sata_ioctl_get_revfirmware_info(sata_hba_inst, 1634 &sata_device, &ioc, mode); 1635 break; 1636 1637 case SATA_CFGA_GET_SERIALNUMBER_INFO: 1638 1639 rv = sata_ioctl_get_serialnumber_info(sata_hba_inst, 1640 &sata_device, &ioc, mode); 1641 break; 1642 1643 default: 1644 rv = EINVAL; 1645 break; 1646 1647 } /* End of DEVCTL_AP_CONTROL cmd switch */ 1648 1649 break; 1650 } 1651 1652 default: 1653 { 1654 /* 1655 * If we got here, we got an IOCTL that SATA HBA Framework 1656 * does not recognize. Pass ioctl to HBA driver, in case 1657 * it could process it. 1658 */ 1659 sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran; 1660 dev_info_t *mydip = SATA_DIP(sata_hba_inst); 1661 1662 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 1663 "IOCTL 0x%2x not supported in SATA framework, " 1664 "passthrough to HBA", cmd); 1665 1666 if (sata_tran->sata_tran_ioctl == NULL) { 1667 rv = EINVAL; 1668 break; 1669 } 1670 rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg); 1671 if (rval != 0) { 1672 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 1673 "IOCTL 0x%2x failed in HBA", cmd); 1674 rv = rval; 1675 } 1676 break; 1677 } 1678 1679 } /* End of main IOCTL switch */ 1680 1681 if (dcp) { 1682 ndi_dc_freehdl(dcp); 1683 } 1684 1685 if (IS_DEVCTL(cmd)) { 1686 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 1687 cport)->cport_mutex); 1688 cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY; 1689 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1690 } 1691 1692 return (rv); 1693 } 1694 1695 1696 /* 1697 * Create error retrieval sata packet 1698 * 1699 * A sata packet is allocated and set-up to contain specified error retrieval 1700 * command and appropriate dma-able data buffer. 1701 * No association with any scsi packet is made and no callback routine is 1702 * specified. 1703 * 1704 * Returns a pointer to sata packet upon successful packet creation. 1705 * Returns NULL, if packet cannot be created. 1706 */ 1707 sata_pkt_t * 1708 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device, 1709 int pkt_type) 1710 { 1711 sata_hba_inst_t *sata_hba_inst; 1712 sata_pkt_txlate_t *spx; 1713 sata_pkt_t *spkt; 1714 sata_drive_info_t *sdinfo; 1715 1716 mutex_enter(&sata_mutex); 1717 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 1718 sata_hba_inst = sata_hba_inst->satahba_next) { 1719 if (SATA_DIP(sata_hba_inst) == dip) 1720 break; 1721 } 1722 mutex_exit(&sata_mutex); 1723 ASSERT(sata_hba_inst != NULL); 1724 1725 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 1726 if (sdinfo == NULL) { 1727 sata_log(sata_hba_inst, CE_WARN, 1728 "sata: error recovery request for non-attached device at " 1729 "cport %d", sata_device->satadev_addr.cport); 1730 return (NULL); 1731 } 1732 1733 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 1734 spx->txlt_sata_hba_inst = sata_hba_inst; 1735 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 1736 spkt = sata_pkt_alloc(spx, NULL); 1737 if (spkt == NULL) { 1738 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1739 return (NULL); 1740 } 1741 /* address is needed now */ 1742 spkt->satapkt_device.satadev_addr = sata_device->satadev_addr; 1743 1744 switch (pkt_type) { 1745 case SATA_ERR_RETR_PKT_TYPE_NCQ: 1746 if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) { 1747 if (sata_check_for_dma_error(dip, spx)) { 1748 ddi_fm_service_impact(dip, 1749 DDI_SERVICE_UNAFFECTED); 1750 break; 1751 } 1752 return (spkt); 1753 } 1754 break; 1755 1756 case SATA_ERR_RETR_PKT_TYPE_ATAPI: 1757 if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) { 1758 if (sata_check_for_dma_error(dip, spx)) { 1759 ddi_fm_service_impact(dip, 1760 DDI_SERVICE_UNAFFECTED); 1761 break; 1762 } 1763 return (spkt); 1764 } 1765 break; 1766 1767 default: 1768 break; 1769 } 1770 1771 sata_pkt_free(spx); 1772 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1773 return (NULL); 1774 1775 } 1776 1777 1778 /* 1779 * Free error retrieval sata packet 1780 * 1781 * Free sata packet and any associated resources allocated previously by 1782 * sata_get_error_retrieval_pkt(). 1783 * 1784 * Void return. 1785 */ 1786 void 1787 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt) 1788 { 1789 sata_pkt_txlate_t *spx = 1790 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 1791 1792 ASSERT(sata_pkt != NULL); 1793 1794 sata_free_local_buffer(spx); 1795 sata_pkt_free(spx); 1796 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1797 1798 } 1799 1800 /* 1801 * Create READ PORT MULTIPLIER and WRITE PORT MULTIPLIER sata packet 1802 * 1803 * No association with any scsi packet is made and no callback routine is 1804 * specified. 1805 * 1806 * Returns a pointer to sata packet upon successful packet creation. 1807 * Returns NULL, if packet cannot be created. 1808 * 1809 * NOTE: Input/Output value includes 64 bits accoring to SATA Spec 2.6, 1810 * only lower 32 bits are available currently. 1811 */ 1812 sata_pkt_t * 1813 sata_get_rdwr_pmult_pkt(dev_info_t *dip, sata_device_t *sd, 1814 uint16_t regn, uint32_t regv, uint32_t type) 1815 { 1816 sata_hba_inst_t *sata_hba_inst; 1817 sata_pkt_txlate_t *spx; 1818 sata_pkt_t *spkt; 1819 sata_cmd_t *scmd; 1820 1821 /* Only READ/WRITE commands are accepted. */ 1822 ASSERT(type == SATA_RDWR_PMULT_PKT_TYPE_READ || 1823 type == SATA_RDWR_PMULT_PKT_TYPE_WRITE); 1824 1825 mutex_enter(&sata_mutex); 1826 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 1827 sata_hba_inst = sata_hba_inst->satahba_next) { 1828 if (SATA_DIP(sata_hba_inst) == dip) 1829 break; 1830 } 1831 mutex_exit(&sata_mutex); 1832 ASSERT(sata_hba_inst != NULL); 1833 1834 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 1835 spx->txlt_sata_hba_inst = sata_hba_inst; 1836 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 1837 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 1838 if (spkt == NULL) { 1839 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1840 return (NULL); 1841 } 1842 1843 /* 1844 * NOTE: We need to send this command to the port multiplier, 1845 * that means send to SATA_PMULT_HOSTPORT(0xf) pmport 1846 * 1847 * sata_device contains the address of actual target device, and the 1848 * pmport number in the command comes from the sata_device structure. 1849 */ 1850 spkt->satapkt_device.satadev_addr = sd->satadev_addr; 1851 spkt->satapkt_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT; 1852 spkt->satapkt_device.satadev_addr.qual = SATA_ADDR_PMULT; 1853 1854 /* Fill sata_pkt */ 1855 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING; 1856 spkt->satapkt_comp = NULL; /* Synchronous mode, no callback */ 1857 spkt->satapkt_time = 10; /* Timeout 10s */ 1858 1859 /* Build READ PORT MULTIPLIER cmd in the sata_pkt */ 1860 scmd = &spkt->satapkt_cmd; 1861 scmd->satacmd_features_reg = regn & 0xff; 1862 scmd->satacmd_features_reg_ext = (regn >> 8) & 0xff; 1863 scmd->satacmd_device_reg = sd->satadev_addr.pmport; 1864 scmd->satacmd_addr_type = 0; /* N/A */ 1865 1866 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 1867 1868 if (type == SATA_RDWR_PMULT_PKT_TYPE_READ) { 1869 scmd->satacmd_cmd_reg = SATAC_READ_PORTMULT; 1870 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 1871 scmd->satacmd_flags.sata_special_regs = 1; 1872 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1; 1873 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1; 1874 scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1; 1875 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1; 1876 } else if (type == SATA_RDWR_PMULT_PKT_TYPE_WRITE) { 1877 scmd->satacmd_cmd_reg = SATAC_WRITE_PORTMULT; 1878 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 1879 scmd->satacmd_sec_count_lsb = regv & 0xff; 1880 scmd->satacmd_lba_low_lsb = regv >> 8 & 0xff; 1881 scmd->satacmd_lba_mid_lsb = regv >> 16 & 0xff; 1882 scmd->satacmd_lba_high_lsb = regv >> 24 & 0xff; 1883 } 1884 1885 return (spkt); 1886 } 1887 1888 /* 1889 * Free sata packet and any associated resources allocated previously by 1890 * sata_get_rdwr_pmult_pkt(). 1891 * 1892 * Void return. 1893 */ 1894 void 1895 sata_free_rdwr_pmult_pkt(sata_pkt_t *sata_pkt) 1896 { 1897 sata_pkt_txlate_t *spx = 1898 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 1899 1900 /* Free allocated resources */ 1901 sata_pkt_free(spx); 1902 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1903 } 1904 1905 /* 1906 * Register a port multiplier to framework. 1907 * 1) Store the GSCR values in the previous allocated pmult_info strctures. 1908 * 2) Search in the blacklist and update the number of the device ports of the 1909 * port multiplier. 1910 * 1911 * Void return. 1912 */ 1913 void 1914 sata_register_pmult(dev_info_t *dip, sata_device_t *sd, sata_pmult_gscr_t *sg) 1915 { 1916 sata_hba_inst_t *sata_hba_inst = NULL; 1917 sata_pmult_info_t *pmultinfo; 1918 sata_pmult_bl_t *blp; 1919 int cport = sd->satadev_addr.cport; 1920 1921 mutex_enter(&sata_mutex); 1922 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 1923 sata_hba_inst = sata_hba_inst->satahba_next) { 1924 if (SATA_DIP(sata_hba_inst) == dip) 1925 if (sata_hba_inst->satahba_attached == 1) 1926 break; 1927 } 1928 mutex_exit(&sata_mutex); 1929 /* HBA not attached? */ 1930 if (sata_hba_inst == NULL) 1931 return; 1932 1933 /* Number of pmports */ 1934 sd->satadev_add_info = sg->gscr2 & SATA_PMULT_PORTNUM_MASK; 1935 1936 /* Check the blacklist */ 1937 for (blp = sata_pmult_blacklist; blp->bl_gscr0; blp++) { 1938 if (sg->gscr0 != blp->bl_gscr0 && blp->bl_gscr0) 1939 continue; 1940 if (sg->gscr1 != blp->bl_gscr1 && blp->bl_gscr1) 1941 continue; 1942 if (sg->gscr2 != blp->bl_gscr2 && blp->bl_gscr2) 1943 continue; 1944 1945 cmn_err(CE_WARN, "!Port multiplier is on the blacklist."); 1946 sd->satadev_add_info = blp->bl_flags; 1947 break; 1948 } 1949 1950 /* Register the port multiplier GSCR */ 1951 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 1952 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 1953 if (pmultinfo != NULL) { 1954 pmultinfo->pmult_gscr = *sg; 1955 pmultinfo->pmult_num_dev_ports = 1956 sd->satadev_add_info & SATA_PMULT_PORTNUM_MASK; 1957 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 1958 "Port multiplier registered at port %d", cport); 1959 } 1960 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 1961 } 1962 1963 /* 1964 * sata_split_model splits the model ID into vendor and product IDs. 1965 * It assumes that a vendor ID cannot be longer than 8 characters, and 1966 * that vendor and product ID are separated by a whitespace. 1967 */ 1968 void 1969 sata_split_model(char *model, char **vendor, char **product) 1970 { 1971 int i, modlen; 1972 char *vid, *pid; 1973 1974 /* 1975 * remove whitespace at the end of model 1976 */ 1977 for (i = SATA_ID_MODEL_LEN; i > 0; i--) 1978 if (model[i] == ' ' || model[i] == '\t' || model[i] == '\0') 1979 model[i] = '\0'; 1980 else 1981 break; 1982 1983 /* 1984 * try to split model into into vid/pid 1985 */ 1986 modlen = strlen(model); 1987 for (i = 0, pid = model; i < modlen; i++, pid++) 1988 if ((*pid == ' ') || (*pid == '\t')) 1989 break; 1990 1991 /* 1992 * only use vid if it is less than 8 chars (as in SCSI) 1993 */ 1994 if (i < modlen && i <= 8) { 1995 vid = model; 1996 /* 1997 * terminate vid, establish pid 1998 */ 1999 *pid++ = '\0'; 2000 } else { 2001 /* 2002 * vid will stay "ATA " 2003 */ 2004 vid = NULL; 2005 /* 2006 * model is all pid 2007 */ 2008 pid = model; 2009 } 2010 2011 *vendor = vid; 2012 *product = pid; 2013 } 2014 2015 /* 2016 * sata_name_child is for composing the name of the node 2017 * the format of the name is "target,0". 2018 */ 2019 static int 2020 sata_name_child(dev_info_t *dip, char *name, int namelen) 2021 { 2022 int target; 2023 2024 target = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 2025 DDI_PROP_DONTPASS, "target", -1); 2026 if (target == -1) 2027 return (DDI_FAILURE); 2028 (void) snprintf(name, namelen, "%x,0", target); 2029 return (DDI_SUCCESS); 2030 } 2031 2032 2033 2034 /* ****************** SCSA required entry points *********************** */ 2035 2036 /* 2037 * Implementation of scsi tran_tgt_init. 2038 * sata_scsi_tgt_init() initializes scsi_device structure 2039 * 2040 * If successful, DDI_SUCCESS is returned. 2041 * DDI_FAILURE is returned if addressed device does not exist 2042 */ 2043 2044 static int 2045 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip, 2046 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 2047 { 2048 #ifndef __lock_lint 2049 _NOTE(ARGUNUSED(hba_dip)) 2050 _NOTE(ARGUNUSED(tgt_dip)) 2051 #endif 2052 sata_device_t sata_device; 2053 sata_drive_info_t *sdinfo; 2054 struct sata_id *sid; 2055 sata_hba_inst_t *sata_hba_inst; 2056 char model[SATA_ID_MODEL_LEN + 1]; 2057 char fw[SATA_ID_FW_LEN + 1]; 2058 char *vid, *pid; 2059 2060 /* 2061 * Fail tran_tgt_init for .conf stub node 2062 */ 2063 if (ndi_dev_is_persistent_node(tgt_dip) == 0) { 2064 (void) ndi_merge_node(tgt_dip, sata_name_child); 2065 ddi_set_name_addr(tgt_dip, NULL); 2066 return (DDI_FAILURE); 2067 } 2068 2069 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 2070 2071 /* Validate scsi device address */ 2072 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 2073 &sata_device) != 0) 2074 return (DDI_FAILURE); 2075 2076 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2077 sata_device.satadev_addr.cport))); 2078 2079 /* sata_device now contains a valid sata address */ 2080 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 2081 if (sdinfo == NULL) { 2082 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2083 sata_device.satadev_addr.cport))); 2084 return (DDI_FAILURE); 2085 } 2086 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2087 sata_device.satadev_addr.cport))); 2088 2089 /* 2090 * Check if we need to create a legacy devid (i.e cmdk style) for 2091 * the target disks. 2092 * 2093 * HBA devinfo node will have the property "use-cmdk-devid-format" 2094 * if we need to create cmdk-style devid for all the disk devices 2095 * attached to this controller. This property may have been set 2096 * from HBA driver's .conf file or by the HBA driver in its 2097 * attach(9F) function. 2098 */ 2099 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 2100 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS, 2101 "use-cmdk-devid-format", 0) == 1)) { 2102 /* register a legacy devid for this target node */ 2103 sata_target_devid_register(tgt_dip, sdinfo); 2104 } 2105 2106 2107 /* 2108 * 'Identify Device Data' does not always fit in standard SCSI 2109 * INQUIRY data, so establish INQUIRY_* properties with full-form 2110 * of information. 2111 */ 2112 sid = &sdinfo->satadrv_id; 2113 #ifdef _LITTLE_ENDIAN 2114 swab(sid->ai_model, model, SATA_ID_MODEL_LEN); 2115 swab(sid->ai_fw, fw, SATA_ID_FW_LEN); 2116 #else /* _LITTLE_ENDIAN */ 2117 bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN); 2118 bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN); 2119 #endif /* _LITTLE_ENDIAN */ 2120 model[SATA_ID_MODEL_LEN] = 0; 2121 fw[SATA_ID_FW_LEN] = 0; 2122 2123 sata_split_model(model, &vid, &pid); 2124 2125 if (vid) 2126 (void) scsi_device_prop_update_inqstring(sd, INQUIRY_VENDOR_ID, 2127 vid, strlen(vid)); 2128 if (pid) 2129 (void) scsi_device_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID, 2130 pid, strlen(pid)); 2131 (void) scsi_device_prop_update_inqstring(sd, INQUIRY_REVISION_ID, 2132 fw, strlen(fw)); 2133 2134 return (DDI_SUCCESS); 2135 } 2136 2137 /* 2138 * Implementation of scsi tran_tgt_probe. 2139 * Probe target, by calling default scsi routine scsi_hba_probe() 2140 */ 2141 static int 2142 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void)) 2143 { 2144 sata_hba_inst_t *sata_hba_inst = 2145 (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private); 2146 int rval; 2147 uint32_t pm_cap; 2148 2149 rval = scsi_hba_probe(sd, callback); 2150 pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE | 2151 SATA_CAP_LOG_SENSE; 2152 2153 if (rval == SCSIPROBE_EXISTS) { 2154 /* 2155 * Set property "pm-capable" on the target device node, so that 2156 * the target driver will not try to fetch scsi cycle counters 2157 * before enabling device power-management. 2158 */ 2159 if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev, 2160 "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) { 2161 sata_log(sata_hba_inst, CE_WARN, 2162 "SATA device at port %d: " 2163 "will not be power-managed ", 2164 SCSI_TO_SATA_CPORT(sd->sd_address.a_target)); 2165 SATA_LOG_D((sata_hba_inst, CE_WARN, 2166 "failure updating pm-capable property")); 2167 } 2168 } 2169 return (rval); 2170 } 2171 2172 /* 2173 * Implementation of scsi tran_tgt_free. 2174 * Release all resources allocated for scsi_device 2175 */ 2176 static void 2177 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip, 2178 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 2179 { 2180 #ifndef __lock_lint 2181 _NOTE(ARGUNUSED(hba_dip)) 2182 #endif 2183 sata_device_t sata_device; 2184 sata_drive_info_t *sdinfo; 2185 sata_hba_inst_t *sata_hba_inst; 2186 ddi_devid_t devid; 2187 2188 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 2189 2190 /* Validate scsi device address */ 2191 /* 2192 * Note: tgt_free relates to the SCSA view of a device. If called, there 2193 * was a device at this address, so even if the sata framework internal 2194 * resources were alredy released because a device was detached, 2195 * this function should be executed as long as its actions do 2196 * not require the internal sata view of a device and the address 2197 * refers to a valid sata address. 2198 * Validating the address here means that we do not trust SCSA... 2199 */ 2200 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 2201 &sata_device) == -1) 2202 return; 2203 2204 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2205 sata_device.satadev_addr.cport))); 2206 2207 /* sata_device now should contain a valid sata address */ 2208 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 2209 if (sdinfo == NULL) { 2210 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2211 sata_device.satadev_addr.cport))); 2212 return; 2213 } 2214 /* 2215 * We did not allocate any resources in sata_scsi_tgt_init() 2216 * other than few properties. 2217 * Free them. 2218 */ 2219 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2220 sata_device.satadev_addr.cport))); 2221 (void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable"); 2222 2223 /* 2224 * If devid was previously created but not freed up from 2225 * sd(4D) driver (i.e during detach(9F)) then do it here. 2226 */ 2227 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 2228 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS, 2229 "use-cmdk-devid-format", 0) == 1) && 2230 (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) { 2231 ddi_devid_unregister(tgt_dip); 2232 ddi_devid_free(devid); 2233 } 2234 } 2235 2236 /* 2237 * Implementation of scsi tran_init_pkt 2238 * Upon successful return, scsi pkt buffer has DMA resources allocated. 2239 * 2240 * It seems that we should always allocate pkt, even if the address is 2241 * for non-existing device - just use some default for dma_attr. 2242 * The reason is that there is no way to communicate this to a caller here. 2243 * Subsequent call to sata_scsi_start may fail appropriately. 2244 * Simply returning NULL does not seem to discourage a target driver... 2245 * 2246 * Returns a pointer to initialized scsi_pkt, or NULL otherwise. 2247 */ 2248 static struct scsi_pkt * 2249 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt, 2250 struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags, 2251 int (*callback)(caddr_t), caddr_t arg) 2252 { 2253 sata_hba_inst_t *sata_hba_inst = 2254 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2255 dev_info_t *dip = SATA_DIP(sata_hba_inst); 2256 sata_device_t sata_device; 2257 sata_drive_info_t *sdinfo; 2258 sata_pkt_txlate_t *spx; 2259 ddi_dma_attr_t cur_dma_attr; 2260 int rval; 2261 boolean_t new_pkt = B_TRUE; 2262 2263 ASSERT(ap->a_hba_tran->tran_hba_dip == dip); 2264 2265 /* 2266 * We need to translate the address, even if it could be 2267 * a bogus one, for a non-existing device 2268 */ 2269 sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 2270 sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target); 2271 sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 2272 sata_device.satadev_rev = SATA_DEVICE_REV; 2273 2274 if (pkt == NULL) { 2275 /* 2276 * Have to allocate a brand new scsi packet. 2277 * We need to operate with auto request sense enabled. 2278 */ 2279 pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen, 2280 MAX(statuslen, SATA_MAX_SENSE_LEN), 2281 tgtlen, sizeof (sata_pkt_txlate_t), callback, arg); 2282 2283 if (pkt == NULL) 2284 return (NULL); 2285 2286 /* Fill scsi packet structure */ 2287 pkt->pkt_comp = (void (*)())NULL; 2288 pkt->pkt_time = 0; 2289 pkt->pkt_resid = 0; 2290 pkt->pkt_statistics = 0; 2291 pkt->pkt_reason = 0; 2292 2293 /* 2294 * pkt_hba_private will point to sata pkt txlate structure 2295 */ 2296 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2297 bzero(spx, sizeof (sata_pkt_txlate_t)); 2298 2299 spx->txlt_scsi_pkt = pkt; 2300 spx->txlt_sata_hba_inst = sata_hba_inst; 2301 2302 /* Allocate sata_pkt */ 2303 spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback); 2304 if (spx->txlt_sata_pkt == NULL) { 2305 /* Could not allocate sata pkt */ 2306 scsi_hba_pkt_free(ap, pkt); 2307 return (NULL); 2308 } 2309 /* Set sata address */ 2310 spx->txlt_sata_pkt->satapkt_device.satadev_addr = 2311 sata_device.satadev_addr; 2312 spx->txlt_sata_pkt->satapkt_device.satadev_rev = 2313 sata_device.satadev_rev; 2314 2315 if ((bp == NULL) || (bp->b_bcount == 0)) 2316 return (pkt); 2317 2318 spx->txlt_total_residue = bp->b_bcount; 2319 } else { 2320 new_pkt = B_FALSE; 2321 /* 2322 * Packet was preallocated/initialized by previous call 2323 */ 2324 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2325 2326 if ((bp == NULL) || (bp->b_bcount == 0)) { 2327 return (pkt); 2328 } 2329 2330 /* Pkt is available already: spx->txlt_scsi_pkt == pkt; */ 2331 } 2332 2333 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 2334 2335 /* 2336 * We use an adjusted version of the dma_attr, to account 2337 * for device addressing limitations. 2338 * sata_adjust_dma_attr() will handle sdinfo == NULL which may 2339 * happen when a device is not yet configured. 2340 */ 2341 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2342 sata_device.satadev_addr.cport))); 2343 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 2344 &spx->txlt_sata_pkt->satapkt_device); 2345 /* NULL sdinfo may be passsed to sata_adjust_dma_attr() */ 2346 sata_adjust_dma_attr(sdinfo, 2347 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 2348 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2349 sata_device.satadev_addr.cport))); 2350 /* 2351 * Allocate necessary DMA resources for the packet's data buffer 2352 * NOTE: 2353 * In case of read/write commands, DMA resource allocation here is 2354 * based on the premise that the transfer length specified in 2355 * the read/write scsi cdb will match exactly DMA resources - 2356 * returning correct packet residue is crucial. 2357 */ 2358 if ((rval = sata_dma_buf_setup(spx, flags, callback, arg, 2359 &cur_dma_attr)) != DDI_SUCCESS) { 2360 /* 2361 * If a DMA allocation request fails with 2362 * DDI_DMA_NOMAPPING, indicate the error by calling 2363 * bioerror(9F) with bp and an error code of EFAULT. 2364 * If a DMA allocation request fails with 2365 * DDI_DMA_TOOBIG, indicate the error by calling 2366 * bioerror(9F) with bp and an error code of EINVAL. 2367 * For DDI_DMA_NORESOURCES, we may have some of them allocated. 2368 * Request may be repeated later - there is no real error. 2369 */ 2370 switch (rval) { 2371 case DDI_DMA_NORESOURCES: 2372 bioerror(bp, 0); 2373 break; 2374 case DDI_DMA_NOMAPPING: 2375 case DDI_DMA_BADATTR: 2376 bioerror(bp, EFAULT); 2377 break; 2378 case DDI_DMA_TOOBIG: 2379 default: 2380 bioerror(bp, EINVAL); 2381 break; 2382 } 2383 goto fail; 2384 } 2385 2386 if (sata_check_for_dma_error(dip, spx)) { 2387 ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED); 2388 bioerror(bp, EFAULT); 2389 goto fail; 2390 } 2391 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 = NULL; 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 __unused, struct scsi_pkt *pkt) 3133 { 3134 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 3135 struct buf *bp; 3136 int direction; 3137 int rval; 3138 3139 ASSERT(spx != NULL); 3140 if (spx->txlt_buf_dma_handle == NULL) 3141 return; 3142 3143 if (spx->txlt_sata_pkt == NULL) 3144 return; 3145 3146 direction = spx->txlt_sata_pkt-> 3147 satapkt_cmd.satacmd_flags.sata_data_direction; 3148 3149 if (direction == SATA_DIR_NODATA_XFER) 3150 return; 3151 3152 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3153 3154 if (spx->txlt_tmp_buf != NULL && (direction & SATA_DIR_WRITE) != 0) { 3155 /* Intermediate DMA buffer used */ 3156 bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf, bp->b_bcount); 3157 } 3158 3159 /* Sync the buffer for device or for CPU */ 3160 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 3161 (direction & SATA_DIR_WRITE) ? 3162 DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU); 3163 ASSERT3S(rval, ==, DDI_SUCCESS); 3164 3165 if (spx->txlt_tmp_buf != NULL && !(direction & SATA_DIR_WRITE)) { 3166 /* Intermediate DMA buffer used for read */ 3167 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, bp->b_bcount); 3168 } 3169 } 3170 3171 3172 3173 /* ******************* SATA - SCSI Translation functions **************** */ 3174 /* 3175 * SCSI to SATA pkt and command translation and SATA to SCSI status/error 3176 * translation. 3177 */ 3178 3179 /* 3180 * Checks if a device exists and can be access and translates common 3181 * scsi_pkt data to sata_pkt data. 3182 * 3183 * Flag argument indicates that a non-read/write ATA command may be sent 3184 * to HBA in arbitrary SYNC mode to execute this packet. 3185 * 3186 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and 3187 * sata_pkt was set-up. 3188 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not 3189 * exist and pkt_comp callback was scheduled. 3190 * Returns other TRAN_XXXXX values when error occured and command should be 3191 * rejected with the returned TRAN_XXXXX value. 3192 * 3193 * This function should be called with port mutex held. 3194 */ 3195 static int 3196 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason, int flag) 3197 { 3198 sata_drive_info_t *sdinfo; 3199 sata_device_t sata_device; 3200 const struct sata_cmd_flags sata_initial_cmd_flags = { 3201 SATA_DIR_NODATA_XFER, 3202 /* all other values to 0/FALSE */ 3203 }; 3204 /* 3205 * Pkt_reason has to be set if the pkt_comp callback is invoked, 3206 * and that implies TRAN_ACCEPT return value. Any other returned value 3207 * indicates that the scsi packet was not accepted (the reason will not 3208 * be checked by the scsi target driver). 3209 * To make debugging easier, we set pkt_reason to know value here. 3210 * It may be changed later when different completion reason is 3211 * determined. 3212 */ 3213 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 3214 *reason = CMD_TRAN_ERR; 3215 3216 /* Validate address */ 3217 switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst, 3218 &spx->txlt_scsi_pkt->pkt_address, &sata_device)) { 3219 3220 case -1: 3221 /* Invalid address or invalid device type */ 3222 return (TRAN_BADPKT); 3223 case 2: 3224 /* 3225 * Valid address but device type is unknown - Chack if it is 3226 * in the reset state and therefore in an indeterminate state. 3227 */ 3228 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3229 &spx->txlt_sata_pkt->satapkt_device); 3230 if (sdinfo != NULL && (sdinfo->satadrv_event_flags & 3231 (SATA_EVNT_DEVICE_RESET | 3232 SATA_EVNT_INPROC_DEVICE_RESET)) != 0) { 3233 if (!ddi_in_panic()) { 3234 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 3235 *reason = CMD_INCOMPLETE; 3236 SATADBG1(SATA_DBG_SCSI_IF, 3237 spx->txlt_sata_hba_inst, 3238 "sata_scsi_start: rejecting command " 3239 "because of device reset state\n", NULL); 3240 return (TRAN_BUSY); 3241 } 3242 } 3243 /* FALLTHROUGH */ 3244 case 1: 3245 /* valid address but no valid device - it has disappeared */ 3246 spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE; 3247 *reason = CMD_DEV_GONE; 3248 /* 3249 * The sd target driver is checking CMD_DEV_GONE pkt_reason 3250 * only in callback function (for normal requests) and 3251 * in the dump code path. 3252 * So, if the callback is available, we need to do 3253 * the callback rather than returning TRAN_FATAL_ERROR here. 3254 */ 3255 if (spx->txlt_scsi_pkt->pkt_comp != NULL) { 3256 /* scsi callback required */ 3257 if (servicing_interrupt()) { 3258 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3259 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3260 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 3261 TASKQID_INVALID) { 3262 return (TRAN_BUSY); 3263 } 3264 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3265 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3266 spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 3267 /* Scheduling the callback failed */ 3268 return (TRAN_BUSY); 3269 } 3270 3271 return (TRAN_ACCEPT); 3272 } 3273 return (TRAN_FATAL_ERROR); 3274 default: 3275 /* all OK; pkt reason will be overwritten later */ 3276 break; 3277 } 3278 /* 3279 * If pkt is to be executed in polling mode and a command will not be 3280 * emulated in SATA module (requires sending a non-read/write ATA 3281 * command to HBA driver in arbitrary SYNC mode) and we are in the 3282 * interrupt context and not in the panic dump, then reject the packet 3283 * to avoid a possible interrupt stack overrun or hang caused by 3284 * a potentially blocked interrupt. 3285 */ 3286 if (((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0 || flag != 0) && 3287 servicing_interrupt() && !ddi_in_panic()) { 3288 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 3289 "sata_scsi_start: rejecting synchronous command because " 3290 "of interrupt context\n", NULL); 3291 return (TRAN_BUSY); 3292 } 3293 3294 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3295 &spx->txlt_sata_pkt->satapkt_device); 3296 3297 /* 3298 * If device is in reset condition, reject the packet with 3299 * TRAN_BUSY, unless: 3300 * 1. system is panicking (dumping) 3301 * In such case only one thread is running and there is no way to 3302 * process reset. 3303 * 2. cfgadm operation is is progress (internal APCTL lock is set) 3304 * Some cfgadm operations involve drive commands, so reset condition 3305 * needs to be ignored for IOCTL operations. 3306 */ 3307 if ((sdinfo->satadrv_event_flags & 3308 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) { 3309 3310 if (!ddi_in_panic() && 3311 ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst, 3312 sata_device.satadev_addr.cport) & 3313 SATA_APCTL_LOCK_PORT_BUSY) == 0)) { 3314 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 3315 *reason = CMD_INCOMPLETE; 3316 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3317 "sata_scsi_start: rejecting command because " 3318 "of device reset state\n", NULL); 3319 return (TRAN_BUSY); 3320 } 3321 } 3322 3323 /* 3324 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by 3325 * sata_scsi_pkt_init() because pkt init had to work also with 3326 * non-existing devices. 3327 * Now we know that the packet was set-up for a real device, so its 3328 * type is known. 3329 */ 3330 spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type; 3331 3332 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags; 3333 if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst, 3334 sata_device.satadev_addr.cport)->cport_event_flags & 3335 SATA_APCTL_LOCK_PORT_BUSY) != 0) { 3336 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 3337 sata_ignore_dev_reset = B_TRUE; 3338 } 3339 /* 3340 * At this point the generic translation routine determined that the 3341 * scsi packet should be accepted. Packet completion reason may be 3342 * changed later when a different completion reason is determined. 3343 */ 3344 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 3345 *reason = CMD_CMPLT; 3346 3347 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) { 3348 /* Synchronous execution */ 3349 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH | 3350 SATA_OPMODE_POLLING; 3351 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 3352 sata_ignore_dev_reset = ddi_in_panic(); 3353 } else { 3354 /* Asynchronous execution */ 3355 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH | 3356 SATA_OPMODE_INTERRUPTS; 3357 } 3358 /* Convert queuing information */ 3359 if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG) 3360 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag = 3361 B_TRUE; 3362 else if (spx->txlt_scsi_pkt->pkt_flags & 3363 (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD)) 3364 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag = 3365 B_TRUE; 3366 3367 /* Always limit pkt time */ 3368 if (spx->txlt_scsi_pkt->pkt_time == 0) 3369 spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time; 3370 else 3371 /* Pass on scsi_pkt time */ 3372 spx->txlt_sata_pkt->satapkt_time = 3373 spx->txlt_scsi_pkt->pkt_time; 3374 3375 return (TRAN_ACCEPT); 3376 } 3377 3378 3379 /* 3380 * Translate ATA Identify Device data to SCSI Inquiry data. 3381 * This function may be called only for ATA devices. 3382 * This function should not be called for ATAPI devices - they 3383 * respond directly to SCSI Inquiry command. 3384 * 3385 * SATA Identify Device data has to be valid in sata_drive_info. 3386 * Buffer has to accomodate the inquiry length (36 bytes). 3387 * 3388 * This function should be called with a port mutex held. 3389 */ 3390 static void 3391 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst, 3392 sata_drive_info_t *sdinfo, uint8_t *buf) 3393 { 3394 3395 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 3396 struct sata_id *sid = &sdinfo->satadrv_id; 3397 3398 /* Start with a nice clean slate */ 3399 bzero((void *)inq, sizeof (struct scsi_inquiry)); 3400 3401 /* 3402 * Rely on the dev_type for setting paripheral qualifier. 3403 * Assume that DTYPE_RODIRECT applies to CD/DVD R/W devices. 3404 * It could be that DTYPE_OPTICAL could also qualify in the future. 3405 * ATAPI Inquiry may provide more data to the target driver. 3406 */ 3407 inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 3408 DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */ 3409 3410 /* CFA type device is not a removable media device */ 3411 inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) && 3412 (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0; 3413 inq->inq_qual = 0; /* Device type qualifier (obsolete in SCSI3? */ 3414 inq->inq_iso = 0; /* ISO version */ 3415 inq->inq_ecma = 0; /* ECMA version */ 3416 inq->inq_ansi = 3; /* ANSI version - SCSI 3 */ 3417 inq->inq_aenc = 0; /* Async event notification cap. */ 3418 inq->inq_trmiop = 0; /* Supports TERMINATE I/O PROC msg - NO */ 3419 inq->inq_normaca = 0; /* setting NACA bit supported - NO */ 3420 inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */ 3421 inq->inq_len = 31; /* Additional length */ 3422 inq->inq_dualp = 0; /* dual port device - NO */ 3423 inq->inq_reladdr = 0; /* Supports relative addressing - NO */ 3424 inq->inq_sync = 0; /* Supports synchronous data xfers - NO */ 3425 inq->inq_linked = 0; /* Supports linked commands - NO */ 3426 /* 3427 * Queuing support - controller has to 3428 * support some sort of command queuing. 3429 */ 3430 if (SATA_QDEPTH(sata_hba_inst) > 1) 3431 inq->inq_cmdque = 1; /* Supports command queueing - YES */ 3432 else 3433 inq->inq_cmdque = 0; /* Supports command queueing - NO */ 3434 inq->inq_sftre = 0; /* Supports Soft Reset option - NO ??? */ 3435 inq->inq_wbus32 = 0; /* Supports 32 bit wide data xfers - NO */ 3436 inq->inq_wbus16 = 0; /* Supports 16 bit wide data xfers - NO */ 3437 3438 #ifdef _LITTLE_ENDIAN 3439 /* Swap text fields to match SCSI format */ 3440 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 3441 swab(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 3442 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 3443 swab(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 3444 else 3445 swab(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 3446 #else /* _LITTLE_ENDIAN */ 3447 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 3448 bcopy(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 3449 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 3450 bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 3451 else 3452 bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 3453 #endif /* _LITTLE_ENDIAN */ 3454 } 3455 3456 3457 /* 3458 * Scsi response set up for invalid command (command not supported) 3459 * 3460 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3461 */ 3462 static int 3463 sata_txlt_invalid_command(sata_pkt_txlate_t *spx) 3464 { 3465 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3466 struct scsi_extended_sense *sense; 3467 3468 scsipkt->pkt_reason = CMD_CMPLT; 3469 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3470 STATE_SENT_CMD | STATE_GOT_STATUS; 3471 3472 *scsipkt->pkt_scbp = STATUS_CHECK; 3473 3474 sense = sata_arq_sense(spx); 3475 sense->es_key = KEY_ILLEGAL_REQUEST; 3476 sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE; 3477 3478 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3479 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3480 3481 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3482 scsipkt->pkt_comp != NULL) { 3483 /* scsi callback required */ 3484 if (servicing_interrupt()) { 3485 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3486 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3487 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 3488 TASKQID_INVALID) { 3489 return (TRAN_BUSY); 3490 } 3491 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3492 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3493 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 3494 /* Scheduling the callback failed */ 3495 return (TRAN_BUSY); 3496 } 3497 } 3498 return (TRAN_ACCEPT); 3499 } 3500 3501 /* 3502 * Scsi response set up for check condition with special sense key 3503 * and additional sense code. 3504 * 3505 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3506 */ 3507 static int 3508 sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code) 3509 { 3510 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 3511 int cport = SATA_TXLT_CPORT(spx); 3512 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3513 struct scsi_extended_sense *sense; 3514 3515 mutex_enter(&SATA_CPORT_MUTEX(shi, cport)); 3516 scsipkt->pkt_reason = CMD_CMPLT; 3517 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3518 STATE_SENT_CMD | STATE_GOT_STATUS; 3519 3520 *scsipkt->pkt_scbp = STATUS_CHECK; 3521 3522 sense = sata_arq_sense(spx); 3523 sense->es_key = key; 3524 sense->es_add_code = code; 3525 3526 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3527 3528 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3529 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3530 3531 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3532 scsipkt->pkt_comp != NULL) { 3533 /* scsi callback required */ 3534 if (servicing_interrupt()) { 3535 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3536 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3537 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 3538 TASKQID_INVALID) { 3539 return (TRAN_BUSY); 3540 } 3541 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3542 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3543 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 3544 /* Scheduling the callback failed */ 3545 return (TRAN_BUSY); 3546 } 3547 } 3548 return (TRAN_ACCEPT); 3549 } 3550 3551 /* 3552 * Scsi response setup for 3553 * emulated non-data command that requires no action/return data 3554 * 3555 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3556 */ 3557 static int 3558 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx) 3559 { 3560 int rval; 3561 int reason; 3562 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 3563 3564 mutex_enter(cport_mutex); 3565 3566 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 3567 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3568 mutex_exit(cport_mutex); 3569 return (rval); 3570 } 3571 mutex_exit(cport_mutex); 3572 3573 spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3574 STATE_SENT_CMD | STATE_GOT_STATUS; 3575 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 3576 *(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD; 3577 3578 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3579 "Scsi_pkt completion reason %x\n", 3580 spx->txlt_scsi_pkt->pkt_reason); 3581 3582 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 && 3583 spx->txlt_scsi_pkt->pkt_comp != NULL) { 3584 /* scsi callback required */ 3585 if (servicing_interrupt()) { 3586 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3587 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3588 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 3589 TASKQID_INVALID) { 3590 return (TRAN_BUSY); 3591 } 3592 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3593 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3594 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 3595 /* Scheduling the callback failed */ 3596 return (TRAN_BUSY); 3597 } 3598 } 3599 return (TRAN_ACCEPT); 3600 } 3601 3602 3603 /* 3604 * SATA translate command: Inquiry / Identify Device 3605 * Use cached Identify Device data for now, rather than issuing actual 3606 * Device Identify cmd request. If device is detached and re-attached, 3607 * asynchronous event processing should fetch and refresh Identify Device 3608 * data. 3609 * VPD pages supported now: 3610 * Vital Product Data page 3611 * Unit Serial Number page 3612 * Block Device Characteristics Page 3613 * ATA Information Page 3614 * 3615 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3616 */ 3617 3618 #define EVPD 1 /* Extended Vital Product Data flag */ 3619 #define CMDDT 2 /* Command Support Data - Obsolete */ 3620 #define INQUIRY_SUP_VPD_PAGE 0 /* Supported VPD Pages Page Code */ 3621 #define INQUIRY_USN_PAGE 0x80 /* Unit Serial Number Page Code */ 3622 #define INQUIRY_BDC_PAGE 0xB1 /* Block Device Characteristics Page */ 3623 /* Code */ 3624 #define INQUIRY_ATA_INFO_PAGE 0x89 /* ATA Information Page Code */ 3625 #define INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Device identifiers */ 3626 3627 static int 3628 sata_txlt_inquiry(sata_pkt_txlate_t *spx) 3629 { 3630 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3631 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3632 sata_drive_info_t *sdinfo; 3633 struct scsi_extended_sense *sense; 3634 int count; 3635 uint8_t *p; 3636 int i, j; 3637 uint8_t page_buf[1024]; /* Max length */ 3638 int rval, reason; 3639 ushort_t rate; 3640 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 3641 3642 /* 3643 * sata_txlt_generic_pkt_info() and sata_get_device_info() require 3644 * cport_mutex to be held while they are called. sdinfo is also 3645 * protected by cport_mutex, so we hold cport_mutex until after we've 3646 * finished using sdinfo. 3647 */ 3648 mutex_enter(cport_mutex); 3649 3650 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 3651 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3652 mutex_exit(cport_mutex); 3653 return (rval); 3654 } 3655 3656 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3657 &spx->txlt_sata_pkt->satapkt_device); 3658 3659 ASSERT(sdinfo != NULL); 3660 3661 scsipkt->pkt_reason = CMD_CMPLT; 3662 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3663 STATE_SENT_CMD | STATE_GOT_STATUS; 3664 3665 /* Reject not supported request */ 3666 if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */ 3667 *scsipkt->pkt_scbp = STATUS_CHECK; 3668 sense = sata_arq_sense(spx); 3669 sense->es_key = KEY_ILLEGAL_REQUEST; 3670 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3671 goto done; 3672 } 3673 3674 /* Valid Inquiry request */ 3675 *scsipkt->pkt_scbp = STATUS_GOOD; 3676 3677 if (bp == NULL || bp->b_un.b_addr == NULL || bp->b_bcount == 0) 3678 goto done; 3679 3680 /* 3681 * Because it is fully emulated command storing data 3682 * programatically in the specified buffer, release 3683 * preallocated DMA resources before storing data in the buffer, 3684 * so no unwanted DMA sync would take place. 3685 */ 3686 sata_scsi_dmafree(NULL, scsipkt); 3687 3688 if (!(scsipkt->pkt_cdbp[1] & EVPD)) { 3689 /* Standard Inquiry Data request */ 3690 struct scsi_inquiry inq; 3691 unsigned int bufsize; 3692 3693 sata_identdev_to_inquiry(spx->txlt_sata_hba_inst, 3694 sdinfo, (uint8_t *)&inq); 3695 /* Copy no more than requested */ 3696 count = MIN(bp->b_bcount, sizeof (struct scsi_inquiry)); 3697 bufsize = scsipkt->pkt_cdbp[4]; 3698 bufsize |= scsipkt->pkt_cdbp[3] << 8; 3699 count = MIN(count, bufsize); 3700 bcopy(&inq, bp->b_un.b_addr, count); 3701 3702 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3703 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 3704 bufsize - count : 0; 3705 goto done; 3706 } 3707 3708 /* 3709 * peripheral_qualifier = 0; 3710 * 3711 * We are dealing only with HD and will be 3712 * dealing with CD/DVD devices soon 3713 */ 3714 uint8_t peripheral_device_type = 3715 sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 3716 DTYPE_DIRECT : DTYPE_RODIRECT; 3717 3718 bzero(page_buf, sizeof (page_buf)); 3719 3720 switch ((uint_t)scsipkt->pkt_cdbp[2]) { 3721 case INQUIRY_SUP_VPD_PAGE: 3722 /* 3723 * Request for supported Vital Product Data pages. 3724 */ 3725 page_buf[0] = peripheral_device_type; 3726 page_buf[1] = INQUIRY_SUP_VPD_PAGE; 3727 page_buf[2] = 0; 3728 page_buf[4] = INQUIRY_SUP_VPD_PAGE; 3729 page_buf[5] = INQUIRY_USN_PAGE; 3730 page_buf[6] = INQUIRY_BDC_PAGE; 3731 /* 3732 * If WWN info is present, provide a page for it. 3733 * Modern drives always have, but some legacy ones do not. 3734 */ 3735 if (sdinfo->satadrv_id.ai_naa_ieee_oui != 0) { 3736 page_buf[3] = 5; /* page length */ 3737 page_buf[7] = INQUIRY_DEV_IDENTIFICATION_PAGE; 3738 page_buf[8] = INQUIRY_ATA_INFO_PAGE; 3739 count = 9; 3740 } else { 3741 page_buf[3] = 4; /* page length */ 3742 page_buf[7] = INQUIRY_ATA_INFO_PAGE; 3743 count = 8; 3744 } 3745 /* Copy no more than requested */ 3746 count = MIN(bp->b_bcount, count); 3747 bcopy(page_buf, bp->b_un.b_addr, count); 3748 break; 3749 3750 case INQUIRY_USN_PAGE: 3751 /* 3752 * Request for Unit Serial Number page. 3753 * Set-up the page. 3754 */ 3755 page_buf[0] = peripheral_device_type; 3756 page_buf[1] = INQUIRY_USN_PAGE; 3757 page_buf[2] = 0; 3758 /* remaining page length */ 3759 page_buf[3] = SATA_ID_SERIAL_LEN; 3760 3761 /* 3762 * Copy serial number from Identify Device data 3763 * words into the inquiry page and swap bytes 3764 * when necessary. 3765 */ 3766 p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser); 3767 #ifdef _LITTLE_ENDIAN 3768 swab(p, &page_buf[4], SATA_ID_SERIAL_LEN); 3769 #else 3770 bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN); 3771 #endif 3772 /* 3773 * Least significant character of the serial 3774 * number shall appear as the last byte, 3775 * according to SBC-3 spec. 3776 * Count trailing spaces to determine the 3777 * necessary shift length. 3778 */ 3779 p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1]; 3780 for (j = 0; j < SATA_ID_SERIAL_LEN; j++) { 3781 if (*(p - j) != '\0' && *(p - j) != '\040') 3782 break; 3783 } 3784 3785 /* 3786 * Shift SN string right, so that the last 3787 * non-blank character would appear in last 3788 * byte of SN field in the page. 3789 * 'j' is the shift length. 3790 */ 3791 for (i = 0; i < (SATA_ID_SERIAL_LEN - j) && j != 0; i++, p--) 3792 *p = *(p - j); 3793 3794 /* 3795 * Add leading spaces - same number as the 3796 * shift size 3797 */ 3798 for (; j > 0; j--) 3799 page_buf[4 + j - 1] = '\040'; 3800 3801 count = MIN(bp->b_bcount, SATA_ID_SERIAL_LEN + 4); 3802 bcopy(page_buf, bp->b_un.b_addr, count); 3803 break; 3804 3805 case INQUIRY_BDC_PAGE: 3806 /* 3807 * Request for Block Device Characteristics 3808 * page. Set-up the page. 3809 */ 3810 page_buf[0] = peripheral_device_type; 3811 page_buf[1] = INQUIRY_BDC_PAGE; 3812 page_buf[2] = 0; 3813 /* remaining page length */ 3814 page_buf[3] = SATA_ID_BDC_LEN; 3815 3816 rate = sdinfo->satadrv_id.ai_medrotrate; 3817 page_buf[4] = (rate >> 8) & 0xff; 3818 page_buf[5] = rate & 0xff; 3819 page_buf[6] = 0; 3820 page_buf[7] = sdinfo->satadrv_id.ai_nomformfactor & 0xf; 3821 3822 count = MIN(bp->b_bcount, SATA_ID_BDC_LEN + 4); 3823 bcopy(page_buf, bp->b_un.b_addr, count); 3824 break; 3825 3826 case INQUIRY_ATA_INFO_PAGE: 3827 /* 3828 * Request for ATA Information page. 3829 */ 3830 page_buf[0] = peripheral_device_type; 3831 page_buf[1] = INQUIRY_ATA_INFO_PAGE; 3832 page_buf[2] = (SATA_ID_ATA_INFO_LEN >> 8) & 0xff; 3833 page_buf[3] = SATA_ID_ATA_INFO_LEN & 0xff; 3834 /* page_buf[4-7] reserved */ 3835 #ifdef _LITTLE_ENDIAN 3836 bcopy("ATA ", &page_buf[8], 8); 3837 swab(sdinfo->satadrv_id.ai_model, &page_buf[16], 16); 3838 if (strncmp(&sdinfo->satadrv_id.ai_fw[4], " ", 4) == 0) { 3839 swab(sdinfo->satadrv_id.ai_fw, &page_buf[32], 4); 3840 } else { 3841 swab(&sdinfo->satadrv_id.ai_fw[4], &page_buf[32], 4); 3842 } 3843 #else /* _LITTLE_ENDIAN */ 3844 bcopy("ATA ", &page_buf[8], 8); 3845 bcopy(sdinfo->satadrv_id.ai_model, &page_buf[16], 16); 3846 if (strncmp(&sdinfo->satadrv_id.ai_fw[4], " ", 4) == 0) { 3847 bcopy(sdinfo->satadrv_id.ai_fw, &page_buf[32], 4); 3848 } else { 3849 bcopy(&sdinfo->satadrv_id.ai_fw[4], &page_buf[32], 4); 3850 } 3851 #endif /* _LITTLE_ENDIAN */ 3852 /* 3853 * page_buf[36-55] which defines the device 3854 * signature is not defined at this 3855 * time. 3856 */ 3857 3858 /* Set the command code */ 3859 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 3860 page_buf[56] = SATAC_ID_DEVICE; 3861 } else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPI) { 3862 page_buf[56] = SATAC_ID_PACKET_DEVICE; 3863 } 3864 /* 3865 * If the command code, page_buf[56], is not 3866 * zero and if one of the identify commands 3867 * succeeds, return the identify data. 3868 */ 3869 if (page_buf[56] != 0) { 3870 sata_drive_info_t temp_info = { 3871 .satadrv_addr = sdinfo->satadrv_addr, 3872 .satadrv_type = sdinfo->satadrv_type, 3873 }; 3874 3875 /* 3876 * It appears calls to an HBA's start (sata_hba_start) 3877 * method (which sata_fetch_device_identify_data_retry() 3878 * calls) must not be done while holding cport_mutex. 3879 * 3880 * A packet's completion routine may call back into 3881 * the sata framework and deadlock (and all extant 3882 * calls to the HBA's start method either drop and 3883 * re-acquire cport_mutex, or never held cport_mutex). 3884 * 3885 * sdinfo is protected by cport_mutex, so we need to 3886 * obtain the SATA address and type from sdinfo 3887 * before releasing cport_mutex and submitting the 3888 * request. We reacquire cport_mutex to simplfy 3889 * cleanup after the done label. 3890 */ 3891 mutex_exit(cport_mutex); 3892 (void) sata_fetch_device_identify_data( 3893 spx->txlt_sata_hba_inst, &temp_info); 3894 mutex_enter(cport_mutex); 3895 3896 /* 3897 * If sata_fetch_device_identify_data() 3898 * fails, the bcopy() is harmless since we're copying 3899 * zeros back over zeros. If it succeeds, we're 3900 * copying over the portion of the response we need. 3901 */ 3902 bcopy(&temp_info.satadrv_id, &page_buf[60], 3903 sizeof (sata_id_t)); 3904 } 3905 3906 /* Need to copy out the page_buf to bp */ 3907 count = MIN(bp->b_bcount, SATA_ID_ATA_INFO_LEN + 4); 3908 bcopy(page_buf, bp->b_un.b_addr, count); 3909 break; 3910 3911 case INQUIRY_DEV_IDENTIFICATION_PAGE: 3912 if (sdinfo->satadrv_id.ai_naa_ieee_oui != 0) { 3913 /* 3914 * Page 83; SAT-5 requires this, and modern 3915 * SATA devices all support a WWN. 3916 */ 3917 page_buf[0] = peripheral_device_type; 3918 page_buf[1] = INQUIRY_DEV_IDENTIFICATION_PAGE; 3919 page_buf[2] = 0; 3920 page_buf[3] = 12; /* remaining length */ 3921 page_buf[4] = 0x01; /* protocol 0, code set 1 */ 3922 page_buf[5] = 0x03; /* LUN, NAA type */ 3923 page_buf[6] = 0; 3924 page_buf[7] = 0x08; /* length (64-bit WWN) */ 3925 #ifdef _LITTLE_ENDIAN 3926 swab(&sdinfo->satadrv_id.ai_naa_ieee_oui, &page_buf[8], 3927 8); 3928 #else 3929 bcopy(&sdinfo->satadrv_id.ai_naa_ieee_oui, 3930 &page_buf[8], 8); 3931 #endif 3932 /* header + designator */ 3933 count = MIN(bp->b_bcount, 12 + 4); 3934 bcopy(page_buf, bp->b_un.b_addr, count); 3935 break; 3936 } 3937 /* FALLTHROUGH */ 3938 3939 default: 3940 /* Request for unsupported VPD page */ 3941 *scsipkt->pkt_scbp = STATUS_CHECK; 3942 sense = sata_arq_sense(spx); 3943 sense->es_key = KEY_ILLEGAL_REQUEST; 3944 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3945 goto done; 3946 } 3947 3948 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3949 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 3950 scsipkt->pkt_cdbp[4] - count : 0; 3951 3952 done: 3953 mutex_exit(cport_mutex); 3954 3955 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3956 "Scsi_pkt completion reason %x\n", 3957 scsipkt->pkt_reason); 3958 3959 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3960 scsipkt->pkt_comp != NULL) { 3961 /* scsi callback required */ 3962 if (servicing_interrupt()) { 3963 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3964 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3965 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 3966 TASKQID_INVALID) { 3967 return (TRAN_BUSY); 3968 } 3969 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3970 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3971 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 3972 /* Scheduling the callback failed */ 3973 return (TRAN_BUSY); 3974 } 3975 } 3976 return (TRAN_ACCEPT); 3977 } 3978 3979 /* 3980 * SATA translate command: Request Sense. 3981 * 3982 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3983 * At the moment this is an emulated command (ATA version for SATA hard disks). 3984 * May be translated into Check Power Mode command in the future. 3985 * 3986 * Note: There is a mismatch between already implemented Informational 3987 * Exception Mode Select page 0x1C and this function. 3988 * When MRIE bit is set in page 0x1C, Request Sense is supposed to return 3989 * NO SENSE and set additional sense code to the exception code - this is not 3990 * implemented here. 3991 */ 3992 static int 3993 sata_txlt_request_sense(sata_pkt_txlate_t *spx) 3994 { 3995 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3996 struct scsi_extended_sense sense; 3997 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3998 sata_drive_info_t *sdinfo; 3999 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4000 int rval, reason, power_state = 0; 4001 kmutex_t *cport_mutex; 4002 4003 cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 4004 mutex_enter(cport_mutex); 4005 4006 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 4007 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4008 mutex_exit(cport_mutex); 4009 return (rval); 4010 } 4011 4012 scsipkt->pkt_reason = CMD_CMPLT; 4013 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4014 STATE_SENT_CMD | STATE_GOT_STATUS; 4015 *scsipkt->pkt_scbp = STATUS_GOOD; 4016 4017 /* 4018 * when CONTROL field's NACA bit == 1 4019 * return ILLEGAL_REQUEST 4020 */ 4021 if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) { 4022 mutex_exit(cport_mutex); 4023 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 4024 SD_SCSI_ASC_CMD_SEQUENCE_ERR)); 4025 } 4026 4027 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4028 &spx->txlt_sata_pkt->satapkt_device); 4029 ASSERT(sdinfo != NULL); 4030 4031 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH; 4032 4033 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE); 4034 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE; 4035 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4036 if (sata_hba_start(spx, &rval) != 0) { 4037 mutex_exit(cport_mutex); 4038 return (rval); 4039 } 4040 if (scmd->satacmd_error_reg != 0) { 4041 mutex_exit(cport_mutex); 4042 return (sata_txlt_check_condition(spx, KEY_NO_SENSE, 4043 SD_SCSI_ASC_NO_ADD_SENSE)); 4044 } 4045 4046 switch (scmd->satacmd_sec_count_lsb) { 4047 case SATA_PWRMODE_STANDBY: /* device in standby mode */ 4048 if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED) 4049 power_state = SATA_POWER_STOPPED; 4050 else { 4051 power_state = SATA_POWER_STANDBY; 4052 sdinfo->satadrv_power_level = SATA_POWER_STANDBY; 4053 } 4054 break; 4055 case SATA_PWRMODE_IDLE: /* device in idle mode */ 4056 power_state = SATA_POWER_IDLE; 4057 sdinfo->satadrv_power_level = SATA_POWER_IDLE; 4058 break; 4059 case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */ 4060 default: /* 0x40, 0x41 active mode */ 4061 if (sdinfo->satadrv_power_level == SATA_POWER_IDLE) 4062 power_state = SATA_POWER_IDLE; 4063 else { 4064 power_state = SATA_POWER_ACTIVE; 4065 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 4066 } 4067 break; 4068 } 4069 4070 mutex_exit(cport_mutex); 4071 4072 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4073 /* 4074 * Because it is fully emulated command storing data 4075 * programatically in the specified buffer, release 4076 * preallocated DMA resources before storing data in the buffer, 4077 * so no unwanted DMA sync would take place. 4078 */ 4079 int count = MIN(bp->b_bcount, 4080 sizeof (struct scsi_extended_sense)); 4081 sata_scsi_dmafree(NULL, scsipkt); 4082 bzero(&sense, sizeof (struct scsi_extended_sense)); 4083 sense.es_valid = 0; /* Valid LBA */ 4084 sense.es_class = 7; /* Response code 0x70 - current err */ 4085 sense.es_key = KEY_NO_SENSE; 4086 sense.es_add_len = 6; /* Additional length */ 4087 /* Copy no more than requested */ 4088 bcopy(&sense, bp->b_un.b_addr, count); 4089 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4090 scsipkt->pkt_resid = 0; 4091 switch (power_state) { 4092 case SATA_POWER_IDLE: 4093 case SATA_POWER_STANDBY: 4094 sense.es_add_code = 4095 SD_SCSI_ASC_LOW_POWER_CONDITION_ON; 4096 break; 4097 case SATA_POWER_STOPPED: 4098 sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE; 4099 break; 4100 case SATA_POWER_ACTIVE: 4101 default: 4102 break; 4103 } 4104 } 4105 4106 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4107 "Scsi_pkt completion reason %x\n", 4108 scsipkt->pkt_reason); 4109 4110 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4111 scsipkt->pkt_comp != NULL) { 4112 /* scsi callback required */ 4113 if (servicing_interrupt()) { 4114 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4115 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4116 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 4117 TASKQID_INVALID) { 4118 return (TRAN_BUSY); 4119 } 4120 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4121 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4122 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 4123 /* Scheduling the callback failed */ 4124 return (TRAN_BUSY); 4125 } 4126 } 4127 return (TRAN_ACCEPT); 4128 } 4129 4130 /* 4131 * SATA translate command: Test Unit Ready 4132 * (ATA version for SATA hard disks). 4133 * It is translated into the Check Power Mode command. 4134 * 4135 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4136 */ 4137 static int 4138 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx) 4139 { 4140 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4141 struct scsi_extended_sense *sense; 4142 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4143 sata_drive_info_t *sdinfo; 4144 int power_state; 4145 int rval, reason; 4146 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 4147 4148 mutex_enter(cport_mutex); 4149 4150 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 4151 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4152 mutex_exit(cport_mutex); 4153 return (rval); 4154 } 4155 4156 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4157 &spx->txlt_sata_pkt->satapkt_device); 4158 ASSERT(sdinfo != NULL); 4159 4160 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH; 4161 4162 /* send CHECK POWER MODE command */ 4163 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE); 4164 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE; 4165 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4166 if (sata_hba_start(spx, &rval) != 0) { 4167 mutex_exit(cport_mutex); 4168 return (rval); 4169 } 4170 4171 if (scmd->satacmd_error_reg != 0) { 4172 mutex_exit(cport_mutex); 4173 return (sata_txlt_check_condition(spx, KEY_NOT_READY, 4174 SD_SCSI_ASC_LU_NOT_RESPONSE)); 4175 } 4176 4177 power_state = scmd->satacmd_sec_count_lsb; 4178 4179 /* 4180 * return NOT READY when device in STOPPED mode 4181 */ 4182 if (power_state == SATA_PWRMODE_STANDBY && 4183 sdinfo->satadrv_power_level == SATA_POWER_STOPPED) { 4184 *scsipkt->pkt_scbp = STATUS_CHECK; 4185 sense = sata_arq_sense(spx); 4186 sense->es_key = KEY_NOT_READY; 4187 sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY; 4188 } else { 4189 /* 4190 * For other power mode, return GOOD status 4191 */ 4192 *scsipkt->pkt_scbp = STATUS_GOOD; 4193 } 4194 4195 scsipkt->pkt_reason = CMD_CMPLT; 4196 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4197 STATE_SENT_CMD | STATE_GOT_STATUS; 4198 4199 mutex_exit(cport_mutex); 4200 4201 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4202 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4203 4204 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4205 scsipkt->pkt_comp != NULL) { 4206 /* scsi callback required */ 4207 if (servicing_interrupt()) { 4208 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4209 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4210 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 4211 TASKQID_INVALID) { 4212 return (TRAN_BUSY); 4213 } 4214 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4215 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4216 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 4217 /* Scheduling the callback failed */ 4218 return (TRAN_BUSY); 4219 } 4220 } 4221 4222 return (TRAN_ACCEPT); 4223 } 4224 4225 /* 4226 * SATA translate command: Start Stop Unit 4227 * Translation depends on a command: 4228 * 4229 * Power condition bits will be supported 4230 * and the power level should be maintained by SATL, 4231 * When SATL received a command, it will check the 4232 * power level firstly, and return the status according 4233 * to SAT2 v2.6 and SAT-2 Standby Modifications 4234 * 4235 * SPC-4/SBC-3 SATL ATA power condition SATL SPC/SBC 4236 * ----------------------------------------------------------------------- 4237 * SSU_PC1 Active <==> ATA Active <==> SSU:start_bit =1 4238 * SSU_PC2 Idle <==> ATA Idle <==> N/A 4239 * SSU_PC3 Standby <==> ATA Standby <==> N/A 4240 * SSU_PC4 Stopped <==> ATA Standby <==> SSU:start_bit = 0 4241 * 4242 * Unload Media / NOT SUPPORTED YET 4243 * Load Media / NOT SUPPROTED YET 4244 * Immediate bit / NOT SUPPORTED YET (deferred error) 4245 * 4246 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 4247 * appropriate values in scsi_pkt fields. 4248 */ 4249 static int 4250 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx) 4251 { 4252 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4253 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4254 int rval, reason; 4255 sata_drive_info_t *sdinfo; 4256 sata_id_t *sata_id; 4257 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 4258 4259 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4260 "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1); 4261 4262 mutex_enter(cport_mutex); 4263 4264 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 4265 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4266 mutex_exit(cport_mutex); 4267 return (rval); 4268 } 4269 4270 if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) { 4271 /* IMMED bit - not supported */ 4272 mutex_exit(cport_mutex); 4273 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 4274 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4275 } 4276 4277 spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH; 4278 spx->txlt_sata_pkt->satapkt_comp = NULL; 4279 4280 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4281 &spx->txlt_sata_pkt->satapkt_device); 4282 ASSERT(sdinfo != NULL); 4283 sata_id = &sdinfo->satadrv_id; 4284 4285 switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) { 4286 case 0: 4287 if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) { 4288 /* Load/Unload Media - invalid request */ 4289 goto err_out; 4290 } 4291 if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) { 4292 /* Start Unit */ 4293 sata_build_read_verify_cmd(scmd, 1, 5); 4294 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4295 /* Transfer command to HBA */ 4296 if (sata_hba_start(spx, &rval) != 0) { 4297 /* Pkt not accepted for execution */ 4298 mutex_exit(cport_mutex); 4299 return (rval); 4300 } 4301 if (scmd->satacmd_error_reg != 0) { 4302 goto err_out; 4303 } 4304 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 4305 } else { 4306 /* Stop Unit */ 4307 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 4308 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4309 if (sata_hba_start(spx, &rval) != 0) { 4310 mutex_exit(cport_mutex); 4311 return (rval); 4312 } else { 4313 if (scmd->satacmd_error_reg != 0) { 4314 goto err_out; 4315 } 4316 } 4317 /* ata standby immediate command */ 4318 sata_build_generic_cmd(scmd, SATAC_STANDBY_IM); 4319 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4320 if (sata_hba_start(spx, &rval) != 0) { 4321 mutex_exit(cport_mutex); 4322 return (rval); 4323 } 4324 if (scmd->satacmd_error_reg != 0) { 4325 goto err_out; 4326 } 4327 sdinfo->satadrv_power_level = SATA_POWER_STOPPED; 4328 } 4329 break; 4330 case 0x1: 4331 sata_build_generic_cmd(scmd, SATAC_IDLE); 4332 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4333 if (sata_hba_start(spx, &rval) != 0) { 4334 mutex_exit(cport_mutex); 4335 return (rval); 4336 } 4337 if (scmd->satacmd_error_reg != 0) { 4338 goto err_out; 4339 } 4340 sata_build_read_verify_cmd(scmd, 1, 5); 4341 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4342 /* Transfer command to HBA */ 4343 if (sata_hba_start(spx, &rval) != 0) { 4344 /* Pkt not accepted for execution */ 4345 mutex_exit(cport_mutex); 4346 return (rval); 4347 } else { 4348 if (scmd->satacmd_error_reg != 0) { 4349 goto err_out; 4350 } 4351 } 4352 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 4353 break; 4354 case 0x2: 4355 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 4356 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4357 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) { 4358 if (sata_hba_start(spx, &rval) != 0) { 4359 mutex_exit(cport_mutex); 4360 return (rval); 4361 } 4362 if (scmd->satacmd_error_reg != 0) { 4363 goto err_out; 4364 } 4365 } 4366 sata_build_generic_cmd(scmd, SATAC_IDLE); 4367 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4368 if (sata_hba_start(spx, &rval) != 0) { 4369 mutex_exit(cport_mutex); 4370 return (rval); 4371 } 4372 if (scmd->satacmd_error_reg != 0) { 4373 goto err_out; 4374 } 4375 if ((scsipkt->pkt_cdbp[3] & START_STOP_MODIFIER_MASK)) { 4376 /* 4377 * POWER CONDITION MODIFIER bit set 4378 * to 0x1 or larger it will be handled 4379 * on the same way as bit = 0x1 4380 */ 4381 if (!(sata_id->ai_cmdset84 & 4382 SATA_IDLE_UNLOAD_SUPPORTED)) { 4383 sdinfo->satadrv_power_level = SATA_POWER_IDLE; 4384 break; 4385 } 4386 sata_build_generic_cmd(scmd, SATAC_IDLE_IM); 4387 scmd->satacmd_features_reg = 0x44; 4388 scmd->satacmd_lba_low_lsb = 0x4c; 4389 scmd->satacmd_lba_mid_lsb = 0x4e; 4390 scmd->satacmd_lba_high_lsb = 0x55; 4391 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4392 if (sata_hba_start(spx, &rval) != 0) { 4393 mutex_exit(cport_mutex); 4394 return (rval); 4395 } 4396 if (scmd->satacmd_error_reg != 0) { 4397 goto err_out; 4398 } 4399 } 4400 sdinfo->satadrv_power_level = SATA_POWER_IDLE; 4401 break; 4402 case 0x3: 4403 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 4404 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4405 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) { 4406 if (sata_hba_start(spx, &rval) != 0) { 4407 mutex_exit(cport_mutex); 4408 return (rval); 4409 } 4410 if (scmd->satacmd_error_reg != 0) { 4411 goto err_out; 4412 } 4413 } 4414 sata_build_generic_cmd(scmd, SATAC_STANDBY); 4415 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4416 if (sata_hba_start(spx, &rval) != 0) { 4417 mutex_exit(cport_mutex); 4418 return (rval); 4419 } 4420 if (scmd->satacmd_error_reg != 0) { 4421 goto err_out; 4422 } 4423 sdinfo->satadrv_power_level = SATA_POWER_STANDBY; 4424 break; 4425 case 0x7: 4426 sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE); 4427 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE; 4428 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4429 if (sata_hba_start(spx, &rval) != 0) { 4430 mutex_exit(cport_mutex); 4431 return (rval); 4432 } 4433 if (scmd->satacmd_error_reg != 0) { 4434 goto err_out; 4435 } 4436 switch (scmd->satacmd_sec_count_lsb) { 4437 case SATA_PWRMODE_STANDBY: 4438 sata_build_generic_cmd(scmd, SATAC_STANDBY); 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 } else { 4446 if (scmd->satacmd_error_reg != 0) { 4447 goto err_out; 4448 } 4449 } 4450 break; 4451 case SATA_PWRMODE_IDLE: 4452 sata_build_generic_cmd(scmd, SATAC_IDLE); 4453 scmd->satacmd_sec_count_msb = sata_get_standby_timer( 4454 sdinfo->satadrv_standby_timer); 4455 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4456 if (sata_hba_start(spx, &rval) != 0) { 4457 mutex_exit(cport_mutex); 4458 return (rval); 4459 } else { 4460 if (scmd->satacmd_error_reg != 0) { 4461 goto err_out; 4462 } 4463 } 4464 break; 4465 case SATA_PWRMODE_ACTIVE_SPINDOWN: 4466 case SATA_PWRMODE_ACTIVE_SPINUP: 4467 case SATA_PWRMODE_ACTIVE: 4468 sata_build_generic_cmd(scmd, SATAC_IDLE); 4469 scmd->satacmd_sec_count_msb = sata_get_standby_timer( 4470 sdinfo->satadrv_standby_timer); 4471 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4472 if (sata_hba_start(spx, &rval) != 0) { 4473 mutex_exit(cport_mutex); 4474 return (rval); 4475 } 4476 if (scmd->satacmd_error_reg != 0) { 4477 goto err_out; 4478 } 4479 sata_build_read_verify_cmd(scmd, 1, 5); 4480 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4481 if (sata_hba_start(spx, &rval) != 0) { 4482 mutex_exit(cport_mutex); 4483 return (rval); 4484 } 4485 if (scmd->satacmd_error_reg != 0) { 4486 goto err_out; 4487 } 4488 break; 4489 default: 4490 goto err_out; 4491 } 4492 break; 4493 case 0xb: 4494 if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) == 4495 0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) { 4496 mutex_exit(cport_mutex); 4497 return (sata_txlt_check_condition(spx, 4498 KEY_ILLEGAL_REQUEST, 4499 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4500 } 4501 sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE); 4502 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4503 if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) { 4504 if (sata_hba_start(spx, &rval) != 0) { 4505 mutex_exit(cport_mutex); 4506 return (rval); 4507 } 4508 if (scmd->satacmd_error_reg != 0) { 4509 goto err_out; 4510 } 4511 sata_build_generic_cmd(scmd, SATAC_STANDBY_IM); 4512 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 4513 if (sata_hba_start(spx, &rval) != 0) { 4514 mutex_exit(cport_mutex); 4515 return (rval); 4516 } 4517 if (scmd->satacmd_error_reg != 0) { 4518 goto err_out; 4519 } 4520 } 4521 bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4); 4522 break; 4523 default: 4524 err_out: 4525 mutex_exit(cport_mutex); 4526 return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 4527 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4528 } 4529 4530 /* 4531 * Since it was a synchronous command, 4532 * a callback function will be called directly. 4533 */ 4534 mutex_exit(cport_mutex); 4535 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4536 "synchronous execution status %x\n", 4537 spx->txlt_sata_pkt->satapkt_reason); 4538 4539 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4540 scsipkt->pkt_comp != NULL) { 4541 sata_set_arq_data(spx->txlt_sata_pkt); 4542 if (servicing_interrupt()) { 4543 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4544 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4545 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 4546 TASKQID_INVALID) { 4547 return (TRAN_BUSY); 4548 } 4549 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4550 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4551 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 4552 /* Scheduling the callback failed */ 4553 return (TRAN_BUSY); 4554 } 4555 } 4556 else 4557 4558 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 4559 4560 return (TRAN_ACCEPT); 4561 4562 } 4563 4564 /* 4565 * SATA translate command: Read Capacity. 4566 * Emulated command for SATA disks. 4567 * Capacity is retrieved from cached Idenifty Device data. 4568 * Identify Device data shows effective disk capacity, not the native 4569 * capacity, which may be limitted by Set Max Address command. 4570 * This is ATA version for SATA hard disks. 4571 * 4572 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4573 */ 4574 static int 4575 sata_txlt_read_capacity(sata_pkt_txlate_t *spx) 4576 { 4577 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4578 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4579 sata_drive_info_t *sdinfo; 4580 uint64_t val; 4581 uint32_t lbsize = DEV_BSIZE; 4582 uchar_t *rbuf; 4583 int rval, reason; 4584 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 4585 4586 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4587 "sata_txlt_read_capacity: ", NULL); 4588 4589 mutex_enter(cport_mutex); 4590 4591 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 4592 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4593 mutex_exit(cport_mutex); 4594 return (rval); 4595 } 4596 4597 scsipkt->pkt_reason = CMD_CMPLT; 4598 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4599 STATE_SENT_CMD | STATE_GOT_STATUS; 4600 *scsipkt->pkt_scbp = STATUS_GOOD; 4601 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4602 /* 4603 * Because it is fully emulated command storing data 4604 * programatically in the specified buffer, release 4605 * preallocated DMA resources before storing data in the buffer, 4606 * so no unwanted DMA sync would take place. 4607 */ 4608 sata_scsi_dmafree(NULL, scsipkt); 4609 4610 sdinfo = sata_get_device_info( 4611 spx->txlt_sata_hba_inst, 4612 &spx->txlt_sata_pkt->satapkt_device); 4613 4614 /* 4615 * As per SBC-3, the "returned LBA" is either the highest 4616 * addressable LBA or 0xffffffff, whichever is smaller. 4617 */ 4618 val = MIN(sdinfo->satadrv_capacity - 1, UINT32_MAX); 4619 4620 if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) { 4621 /* physical/logical sector size word is valid */ 4622 4623 if (sdinfo->satadrv_id.ai_phys_sect_sz & 4624 SATA_L2PS_BIG_SECTORS) { 4625 /* if this set 117-118 words are valid */ 4626 lbsize = sdinfo->satadrv_id.ai_words_lsec[0] | 4627 (sdinfo->satadrv_id.ai_words_lsec[1] << 16); 4628 lbsize <<= 1; /* convert from words to bytes */ 4629 } 4630 } 4631 rbuf = (uchar_t *)bp->b_un.b_addr; 4632 /* Need to swap endians to match scsi format */ 4633 rbuf[0] = (val >> 24) & 0xff; 4634 rbuf[1] = (val >> 16) & 0xff; 4635 rbuf[2] = (val >> 8) & 0xff; 4636 rbuf[3] = val & 0xff; 4637 rbuf[4] = (lbsize >> 24) & 0xff; 4638 rbuf[5] = (lbsize >> 16) & 0xff; 4639 rbuf[6] = (lbsize >> 8) & 0xff; 4640 rbuf[7] = lbsize & 0xff; 4641 4642 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4643 scsipkt->pkt_resid = 0; 4644 4645 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n", 4646 sdinfo->satadrv_capacity -1); 4647 } 4648 mutex_exit(cport_mutex); 4649 /* 4650 * If a callback was requested, do it now. 4651 */ 4652 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4653 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4654 4655 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4656 scsipkt->pkt_comp != NULL) { 4657 /* scsi callback required */ 4658 if (servicing_interrupt()) { 4659 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4660 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4661 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 4662 TASKQID_INVALID) { 4663 return (TRAN_BUSY); 4664 } 4665 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4666 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4667 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 4668 /* Scheduling the callback failed */ 4669 return (TRAN_BUSY); 4670 } 4671 } 4672 4673 return (TRAN_ACCEPT); 4674 } 4675 4676 /* 4677 * SATA translate command: Read Capacity (16). 4678 * Emulated command for SATA disks. 4679 * Info is retrieved from cached Identify Device data. 4680 * Implemented to SBC-3 (draft 21) and SAT-2 (final) specifications. 4681 * 4682 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 4683 */ 4684 static int 4685 sata_txlt_read_capacity16(sata_pkt_txlate_t *spx) 4686 { 4687 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4688 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4689 sata_drive_info_t *sdinfo; 4690 uint64_t val; 4691 uint16_t l2p_exp; 4692 uint32_t lbsize = DEV_BSIZE; 4693 uchar_t *rbuf; 4694 int rval, reason; 4695 #define TPE 0x80 4696 #define TPRZ 0x40 4697 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 4698 4699 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4700 "sata_txlt_read_capacity: ", NULL); 4701 4702 mutex_enter(cport_mutex); 4703 4704 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 4705 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4706 mutex_exit(cport_mutex); 4707 return (rval); 4708 } 4709 4710 scsipkt->pkt_reason = CMD_CMPLT; 4711 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4712 STATE_SENT_CMD | STATE_GOT_STATUS; 4713 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4714 /* 4715 * Because it is fully emulated command storing data 4716 * programatically in the specified buffer, release 4717 * preallocated DMA resources before storing data in the buffer, 4718 * so no unwanted DMA sync would take place. 4719 */ 4720 sata_scsi_dmafree(NULL, scsipkt); 4721 4722 /* Check SERVICE ACTION field */ 4723 if ((scsipkt->pkt_cdbp[1] & 0x1f) != 4724 SSVC_ACTION_READ_CAPACITY_G4) { 4725 mutex_exit(cport_mutex); 4726 return (sata_txlt_check_condition(spx, 4727 KEY_ILLEGAL_REQUEST, 4728 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4729 } 4730 4731 /* Check LBA field */ 4732 if ((scsipkt->pkt_cdbp[2] != 0) || 4733 (scsipkt->pkt_cdbp[3] != 0) || 4734 (scsipkt->pkt_cdbp[4] != 0) || 4735 (scsipkt->pkt_cdbp[5] != 0) || 4736 (scsipkt->pkt_cdbp[6] != 0) || 4737 (scsipkt->pkt_cdbp[7] != 0) || 4738 (scsipkt->pkt_cdbp[8] != 0) || 4739 (scsipkt->pkt_cdbp[9] != 0)) { 4740 mutex_exit(cport_mutex); 4741 return (sata_txlt_check_condition(spx, 4742 KEY_ILLEGAL_REQUEST, 4743 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4744 } 4745 4746 /* Check PMI bit */ 4747 if (scsipkt->pkt_cdbp[14] & 0x1) { 4748 mutex_exit(cport_mutex); 4749 return (sata_txlt_check_condition(spx, 4750 KEY_ILLEGAL_REQUEST, 4751 SD_SCSI_ASC_INVALID_FIELD_IN_CDB)); 4752 } 4753 4754 *scsipkt->pkt_scbp = STATUS_GOOD; 4755 4756 sdinfo = sata_get_device_info( 4757 spx->txlt_sata_hba_inst, 4758 &spx->txlt_sata_pkt->satapkt_device); 4759 4760 /* last logical block address */ 4761 val = MIN(sdinfo->satadrv_capacity - 1, 4762 SCSI_READ_CAPACITY16_MAX_LBA); 4763 4764 /* logical to physical block size exponent */ 4765 l2p_exp = 0; 4766 if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) { 4767 /* physical/logical sector size word is valid */ 4768 4769 if (sdinfo->satadrv_id.ai_phys_sect_sz & 4770 SATA_L2PS_HAS_MULT) { 4771 /* multiple logical sectors per phys sectors */ 4772 l2p_exp = 4773 sdinfo->satadrv_id.ai_phys_sect_sz & 4774 SATA_L2PS_EXP_MASK; 4775 } 4776 4777 if (sdinfo->satadrv_id.ai_phys_sect_sz & 4778 SATA_L2PS_BIG_SECTORS) { 4779 /* if this set 117-118 words are valid */ 4780 lbsize = sdinfo->satadrv_id.ai_words_lsec[0] | 4781 (sdinfo->satadrv_id.ai_words_lsec[1] << 16); 4782 lbsize <<= 1; /* convert from words to bytes */ 4783 } 4784 } 4785 4786 rbuf = (uchar_t *)bp->b_un.b_addr; 4787 bzero(rbuf, bp->b_bcount); 4788 4789 /* returned logical block address */ 4790 rbuf[0] = (val >> 56) & 0xff; 4791 rbuf[1] = (val >> 48) & 0xff; 4792 rbuf[2] = (val >> 40) & 0xff; 4793 rbuf[3] = (val >> 32) & 0xff; 4794 rbuf[4] = (val >> 24) & 0xff; 4795 rbuf[5] = (val >> 16) & 0xff; 4796 rbuf[6] = (val >> 8) & 0xff; 4797 rbuf[7] = val & 0xff; 4798 rbuf[8] = (lbsize >> 24) & 0xff; 4799 rbuf[9] = (lbsize >> 16) & 0xff; 4800 rbuf[10] = (lbsize >> 8) & 0xff; 4801 rbuf[11] = lbsize & 0xff; 4802 4803 /* p_type, prot_en, unspecified by SAT-2 */ 4804 /* rbuf[12] = 0; */ 4805 4806 /* p_i_exponent, undefined by SAT-2 */ 4807 /* logical blocks per physical block exponent */ 4808 rbuf[13] = l2p_exp; 4809 4810 /* 4811 * tpe and tprz as defined in T10/10-079 r0. 4812 * TRIM support is indicated by the relevant bit in the data 4813 * set management word. Read-after-trim behavior is indicated 4814 * by the additional bits in the identify device word. Of the 4815 * three defined possibilities, we only flag read-zero. 4816 */ 4817 if (sdinfo->satadrv_id.ai_dsm & SATA_DSM_TRIM) { 4818 rbuf[14] |= TPE; 4819 4820 if ((sdinfo->satadrv_id.ai_addsupported & 4821 SATA_DETERMINISTIC_READ) && 4822 (sdinfo->satadrv_id.ai_addsupported & 4823 SATA_READ_ZERO)) { 4824 rbuf[14] |= TPRZ; 4825 } 4826 } 4827 4828 /* lowest aligned logical block address = 0 (for now) */ 4829 /* rbuf[15] = 0; */ 4830 4831 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4832 scsipkt->pkt_resid = 0; 4833 4834 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%llu\n", 4835 sdinfo->satadrv_capacity -1); 4836 } 4837 4838 mutex_exit(cport_mutex); 4839 4840 /* 4841 * If a callback was requested, do it now. 4842 */ 4843 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4844 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4845 4846 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4847 scsipkt->pkt_comp != NULL) { 4848 /* scsi callback required */ 4849 if (servicing_interrupt()) { 4850 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4851 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4852 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 4853 TASKQID_INVALID) { 4854 return (TRAN_BUSY); 4855 } 4856 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4857 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 4858 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 4859 /* Scheduling the callback failed */ 4860 return (TRAN_BUSY); 4861 } 4862 } 4863 4864 return (TRAN_ACCEPT); 4865 } 4866 4867 /* 4868 * Translate command: UNMAP 4869 * 4870 * The function cannot be called in interrupt context since it may sleep. 4871 */ 4872 static int 4873 sata_txlt_unmap(sata_pkt_txlate_t *spx) 4874 { 4875 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4876 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4877 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4878 uint16_t count = 0; 4879 int synch; 4880 int rval, reason; 4881 int i, x; 4882 int bdlen = 0; 4883 int ranges = 0; 4884 int paramlen = 8; 4885 uint8_t *data, *tmpbd; 4886 sata_drive_info_t *sdinfo; 4887 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 4888 #define TRIM 0x1 4889 4890 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4891 "sata_txlt_unmap: ", NULL); 4892 4893 mutex_enter(cport_mutex); 4894 4895 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4896 &spx->txlt_sata_pkt->satapkt_device); 4897 if (sdinfo != NULL) { 4898 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4899 "DSM support 0x%x, max number of 512 byte blocks of LBA " 4900 "range entries 0x%x\n", sdinfo->satadrv_id.ai_dsm, 4901 sdinfo->satadrv_id.ai_maxcount); 4902 } 4903 4904 rval = sata_txlt_generic_pkt_info(spx, &reason, 1); 4905 if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4906 mutex_exit(cport_mutex); 4907 return (rval); 4908 } 4909 4910 /* 4911 * Need to modify bp to have TRIM data instead of UNMAP data. 4912 * Start by getting the block descriptor data length by subtracting 4913 * the 8 byte parameter list header from the parameter list length. 4914 * The block descriptor size has to be a multiple of 16 bytes. 4915 */ 4916 bdlen = scsipkt->pkt_cdbp[7]; 4917 bdlen = (bdlen << 8) + scsipkt->pkt_cdbp[8] - paramlen; 4918 if ((bdlen < 0) || ((bdlen % 16) != 0) || 4919 ((bp != NULL) && (bdlen > (bp->b_bcount - paramlen)))) { 4920 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4921 "sata_txlt_unmap: invalid block descriptor length", NULL); 4922 mutex_exit(cport_mutex); 4923 return ((sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST, 4924 SD_SCSI_ASC_INVALID_FIELD_IN_CDB))); 4925 } 4926 /* 4927 * If there are no parameter data or block descriptors, it is not 4928 * considered an error so just complete the command without sending 4929 * TRIM. 4930 */ 4931 if ((bdlen == 0) || (bp == NULL) || (bp->b_un.b_addr == NULL) || 4932 (bp->b_bcount == 0)) { 4933 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4934 "sata_txlt_unmap: no parameter data or block descriptors", 4935 NULL); 4936 mutex_exit(cport_mutex); 4937 return (sata_txlt_unmap_nodata_cmd(spx)); 4938 } 4939 tmpbd = (uint8_t *)bp->b_un.b_addr + paramlen; 4940 data = kmem_zalloc(bdlen, KM_SLEEP); 4941 4942 /* 4943 * Loop through all the UNMAP block descriptors and convert the data 4944 * into TRIM format. 4945 */ 4946 for (i = 0, x = 0; i < bdlen; i += 16, x += 8) { 4947 /* get range length */ 4948 data[x] = tmpbd[i+7]; 4949 data[x+1] = tmpbd[i+6]; 4950 /* get LBA */ 4951 data[x+2] = tmpbd[i+5]; 4952 data[x+3] = tmpbd[i+4]; 4953 data[x+4] = tmpbd[i+3]; 4954 data[x+5] = tmpbd[i+2]; 4955 data[x+6] = tmpbd[i+11]; 4956 data[x+7] = tmpbd[i+10]; 4957 4958 ranges++; 4959 } 4960 4961 /* 4962 * The TRIM command expects the data buffer to be a multiple of 4963 * 512-byte blocks of range entries. This means that the UNMAP buffer 4964 * may be too small. Free the original DMA resources and create a 4965 * local buffer. 4966 */ 4967 sata_common_free_dma_rsrcs(spx); 4968 4969 /* 4970 * Get count of 512-byte blocks of range entries. The length 4971 * of a range entry is 8 bytes which means one count has 64 range 4972 * entries. 4973 */ 4974 count = (ranges + 63)/64; 4975 4976 /* Allocate a buffer that is a multiple of 512 bytes. */ 4977 mutex_exit(cport_mutex); 4978 bp = sata_alloc_local_buffer(spx, count * 512); 4979 if (bp == NULL) { 4980 SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst, 4981 "sata_txlt_unmap: " 4982 "cannot allocate buffer for TRIM command", NULL); 4983 kmem_free(data, bdlen); 4984 return (TRAN_BUSY); 4985 } 4986 bp_mapin(bp); /* make data buffer accessible */ 4987 mutex_enter(cport_mutex); 4988 4989 bzero(bp->b_un.b_addr, bp->b_bcount); 4990 bcopy(data, bp->b_un.b_addr, x); 4991 kmem_free(data, bdlen); 4992 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 4993 DDI_DMA_SYNC_FORDEV); 4994 ASSERT(rval == DDI_SUCCESS); 4995 4996 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 4997 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 4998 scmd->satacmd_cmd_reg = SATAC_DSM; 4999 scmd->satacmd_sec_count_msb = (count >> 8) & 0xff; 5000 scmd->satacmd_sec_count_lsb = count & 0xff; 5001 scmd->satacmd_features_reg = TRIM; 5002 scmd->satacmd_device_reg = SATA_ADH_LBA; 5003 scmd->satacmd_status_reg = 0; 5004 scmd->satacmd_error_reg = 0; 5005 5006 /* Start processing command */ 5007 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5008 spx->txlt_sata_pkt->satapkt_comp = 5009 sata_txlt_unmap_completion; 5010 synch = FALSE; 5011 } else { 5012 synch = TRUE; 5013 } 5014 5015 if (sata_hba_start(spx, &rval) != 0) { 5016 mutex_exit(cport_mutex); 5017 return (rval); 5018 } 5019 5020 mutex_exit(cport_mutex); 5021 5022 if (synch) { 5023 sata_txlt_unmap_completion(spx->txlt_sata_pkt); 5024 } 5025 5026 return (TRAN_ACCEPT); 5027 } 5028 5029 /* 5030 * SATA translate command: Mode Sense. 5031 * Translated into appropriate SATA command or emulated. 5032 * Saved Values Page Control (03) are not supported. 5033 * 5034 * NOTE: only caching mode sense page is currently implemented. 5035 * 5036 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 5037 */ 5038 5039 #define LLBAA 0x10 /* Long LBA Accepted */ 5040 5041 static int 5042 sata_txlt_mode_sense(sata_pkt_txlate_t *spx) 5043 { 5044 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5045 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5046 sata_drive_info_t *sdinfo; 5047 sata_id_t *sata_id; 5048 struct scsi_extended_sense *sense; 5049 int len, bdlen, count, alc_len; 5050 int pc; /* Page Control code */ 5051 uint8_t *buf; /* mode sense buffer */ 5052 int rval, reason; 5053 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 5054 5055 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5056 "sata_txlt_mode_sense, pc %x page code 0x%02x\n", 5057 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 5058 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 5059 5060 if (servicing_interrupt()) { 5061 buf = kmem_zalloc(1024, KM_NOSLEEP); 5062 if (buf == NULL) { 5063 return (TRAN_BUSY); 5064 } 5065 } else { 5066 buf = kmem_zalloc(1024, KM_SLEEP); 5067 } 5068 5069 mutex_enter(cport_mutex); 5070 5071 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 5072 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5073 mutex_exit(cport_mutex); 5074 kmem_free(buf, 1024); 5075 return (rval); 5076 } 5077 5078 scsipkt->pkt_reason = CMD_CMPLT; 5079 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5080 STATE_SENT_CMD | STATE_GOT_STATUS; 5081 5082 pc = scsipkt->pkt_cdbp[2] >> 6; 5083 5084 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 5085 /* 5086 * Because it is fully emulated command storing data 5087 * programatically in the specified buffer, release 5088 * preallocated DMA resources before storing data in the buffer, 5089 * so no unwanted DMA sync would take place. 5090 */ 5091 sata_scsi_dmafree(NULL, scsipkt); 5092 5093 len = 0; 5094 bdlen = 0; 5095 if (!(scsipkt->pkt_cdbp[1] & 8)) { 5096 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 && 5097 (scsipkt->pkt_cdbp[1] & LLBAA)) 5098 bdlen = 16; 5099 else 5100 bdlen = 8; 5101 } 5102 /* Build mode parameter header */ 5103 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 5104 /* 4-byte mode parameter header */ 5105 buf[len++] = 0; /* mode data length */ 5106 buf[len++] = 0; /* medium type */ 5107 buf[len++] = 0; /* dev-specific param */ 5108 buf[len++] = bdlen; /* Block Descriptor length */ 5109 } else { 5110 /* 8-byte mode parameter header */ 5111 buf[len++] = 0; /* mode data length */ 5112 buf[len++] = 0; 5113 buf[len++] = 0; /* medium type */ 5114 buf[len++] = 0; /* dev-specific param */ 5115 if (bdlen == 16) 5116 buf[len++] = 1; /* long lba descriptor */ 5117 else 5118 buf[len++] = 0; 5119 buf[len++] = 0; 5120 buf[len++] = 0; /* Block Descriptor length */ 5121 buf[len++] = bdlen; 5122 } 5123 5124 sdinfo = sata_get_device_info( 5125 spx->txlt_sata_hba_inst, 5126 &spx->txlt_sata_pkt->satapkt_device); 5127 5128 /* Build block descriptor only if not disabled (DBD) */ 5129 if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) { 5130 /* Block descriptor - direct-access device format */ 5131 if (bdlen == 8) { 5132 /* build regular block descriptor */ 5133 buf[len++] = 5134 (sdinfo->satadrv_capacity >> 24) & 0xff; 5135 buf[len++] = 5136 (sdinfo->satadrv_capacity >> 16) & 0xff; 5137 buf[len++] = 5138 (sdinfo->satadrv_capacity >> 8) & 0xff; 5139 buf[len++] = sdinfo->satadrv_capacity & 0xff; 5140 buf[len++] = 0; /* density code */ 5141 buf[len++] = 0; 5142 if (sdinfo->satadrv_type == 5143 SATA_DTYPE_ATADISK) 5144 buf[len++] = 2; 5145 else 5146 /* ATAPI */ 5147 buf[len++] = 8; 5148 buf[len++] = 0; 5149 } else if (bdlen == 16) { 5150 /* Long LBA Accepted */ 5151 /* build long lba block descriptor */ 5152 #ifndef __lock_lint 5153 buf[len++] = 5154 (sdinfo->satadrv_capacity >> 56) & 0xff; 5155 buf[len++] = 5156 (sdinfo->satadrv_capacity >> 48) & 0xff; 5157 buf[len++] = 5158 (sdinfo->satadrv_capacity >> 40) & 0xff; 5159 buf[len++] = 5160 (sdinfo->satadrv_capacity >> 32) & 0xff; 5161 #endif 5162 buf[len++] = 5163 (sdinfo->satadrv_capacity >> 24) & 0xff; 5164 buf[len++] = 5165 (sdinfo->satadrv_capacity >> 16) & 0xff; 5166 buf[len++] = 5167 (sdinfo->satadrv_capacity >> 8) & 0xff; 5168 buf[len++] = sdinfo->satadrv_capacity & 0xff; 5169 buf[len++] = 0; 5170 buf[len++] = 0; /* density code */ 5171 buf[len++] = 0; 5172 buf[len++] = 0; 5173 if (sdinfo->satadrv_type == 5174 SATA_DTYPE_ATADISK) 5175 buf[len++] = 2; 5176 else 5177 /* ATAPI */ 5178 buf[len++] = 8; 5179 buf[len++] = 0; 5180 } 5181 } 5182 5183 sata_id = &sdinfo->satadrv_id; 5184 5185 /* 5186 * Add requested pages. 5187 * Page 3 and 4 are obsolete and we are not supporting them. 5188 * We deal now with: 5189 * caching (read/write cache control). 5190 * We should eventually deal with following mode pages: 5191 * error recovery (0x01), 5192 * power condition (0x1a), 5193 * exception control page (enables SMART) (0x1c), 5194 * enclosure management (ses), 5195 * protocol-specific port mode (port control). 5196 */ 5197 switch (scsipkt->pkt_cdbp[2] & 0x3f) { 5198 case MODEPAGE_RW_ERRRECOV: 5199 /* DAD_MODE_ERR_RECOV */ 5200 /* R/W recovery */ 5201 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 5202 break; 5203 case MODEPAGE_CACHING: 5204 /* DAD_MODE_CACHE */ 5205 /* Reject not supported request for saved parameters */ 5206 if (pc == 3) { 5207 *scsipkt->pkt_scbp = STATUS_CHECK; 5208 sense = sata_arq_sense(spx); 5209 sense->es_key = KEY_ILLEGAL_REQUEST; 5210 sense->es_add_code = 5211 SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED; 5212 goto done; 5213 } 5214 5215 /* caching */ 5216 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 5217 break; 5218 case MODEPAGE_INFO_EXCPT: 5219 /* exception cntrl */ 5220 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 5221 len += sata_build_msense_page_1c(sdinfo, pc, 5222 buf+len); 5223 } 5224 else 5225 goto err; 5226 break; 5227 case MODEPAGE_POWER_COND: 5228 /* DAD_MODE_POWER_COND */ 5229 /* power condition */ 5230 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 5231 break; 5232 5233 case MODEPAGE_ACOUSTIC_MANAG: 5234 /* acoustic management */ 5235 len += sata_build_msense_page_30(sdinfo, pc, buf+len); 5236 break; 5237 case MODEPAGE_ALLPAGES: 5238 /* all pages */ 5239 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 5240 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 5241 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 5242 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 5243 len += sata_build_msense_page_1c(sdinfo, pc, 5244 buf+len); 5245 } 5246 len += sata_build_msense_page_30(sdinfo, pc, buf+len); 5247 break; 5248 default: 5249 err: 5250 /* Invalid request */ 5251 *scsipkt->pkt_scbp = STATUS_CHECK; 5252 sense = sata_arq_sense(spx); 5253 sense->es_key = KEY_ILLEGAL_REQUEST; 5254 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5255 goto done; 5256 } 5257 5258 /* fix total mode data length */ 5259 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 5260 /* 4-byte mode parameter header */ 5261 buf[0] = len - 1; /* mode data length */ 5262 } else { 5263 buf[0] = (len -2) >> 8; 5264 buf[1] = (len -2) & 0xff; 5265 } 5266 5267 5268 /* Check allocation length */ 5269 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 5270 alc_len = scsipkt->pkt_cdbp[4]; 5271 } else { 5272 alc_len = scsipkt->pkt_cdbp[7]; 5273 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 5274 } 5275 /* 5276 * We do not check for possible parameters truncation 5277 * (alc_len < len) assuming that the target driver works 5278 * correctly. Just avoiding overrun. 5279 * Copy no more than requested and possible, buffer-wise. 5280 */ 5281 count = MIN(alc_len, len); 5282 count = MIN(bp->b_bcount, count); 5283 bcopy(buf, bp->b_un.b_addr, count); 5284 5285 scsipkt->pkt_state |= STATE_XFERRED_DATA; 5286 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 5287 } 5288 *scsipkt->pkt_scbp = STATUS_GOOD; 5289 done: 5290 mutex_exit(cport_mutex); 5291 (void) kmem_free(buf, 1024); 5292 5293 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5294 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5295 5296 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5297 scsipkt->pkt_comp != NULL) { 5298 /* scsi callback required */ 5299 if (servicing_interrupt()) { 5300 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5301 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 5302 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 5303 TASKQID_INVALID) { 5304 return (TRAN_BUSY); 5305 } 5306 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5307 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 5308 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 5309 /* Scheduling the callback failed */ 5310 return (TRAN_BUSY); 5311 } 5312 } 5313 5314 return (TRAN_ACCEPT); 5315 } 5316 5317 5318 /* 5319 * SATA translate command: Mode Select. 5320 * Translated into appropriate SATA command or emulated. 5321 * Saving parameters is not supported. 5322 * Changing device capacity is not supported (although theoretically 5323 * possible by executing SET FEATURES/SET MAX ADDRESS) 5324 * 5325 * Assumption is that the target driver is working correctly. 5326 * 5327 * More than one SATA command may be executed to perform operations specified 5328 * by mode select pages. The first error terminates further execution. 5329 * Operations performed successully are not backed-up in such case. 5330 * 5331 * NOTE: Implemented pages: 5332 * - caching page 5333 * - informational exception page 5334 * - acoustic management page 5335 * - power condition page 5336 * Caching setup is remembered so it could be re-stored in case of 5337 * an unexpected device reset. 5338 * 5339 * Returns TRAN_XXXX. 5340 * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields. 5341 */ 5342 5343 static int 5344 sata_txlt_mode_select(sata_pkt_txlate_t *spx) 5345 { 5346 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5347 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5348 struct scsi_extended_sense *sense; 5349 int len, pagelen, count, pllen; 5350 uint8_t *buf; /* mode select buffer */ 5351 int rval, stat, reason; 5352 uint_t nointr_flag; 5353 int dmod = 0; 5354 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 5355 5356 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5357 "sata_txlt_mode_select, pc %x page code 0x%02x\n", 5358 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 5359 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 5360 5361 mutex_enter(cport_mutex); 5362 5363 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 5364 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5365 mutex_exit(cport_mutex); 5366 return (rval); 5367 } 5368 5369 rval = TRAN_ACCEPT; 5370 5371 scsipkt->pkt_reason = CMD_CMPLT; 5372 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5373 STATE_SENT_CMD | STATE_GOT_STATUS; 5374 nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR; 5375 5376 /* Reject not supported request */ 5377 if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */ 5378 *scsipkt->pkt_scbp = STATUS_CHECK; 5379 sense = sata_arq_sense(spx); 5380 sense->es_key = KEY_ILLEGAL_REQUEST; 5381 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5382 goto done; 5383 } 5384 5385 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 5386 pllen = scsipkt->pkt_cdbp[4]; 5387 } else { 5388 pllen = scsipkt->pkt_cdbp[7]; 5389 pllen = (pllen << 8) | scsipkt->pkt_cdbp[7]; 5390 } 5391 5392 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 5393 5394 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) { 5395 buf = (uint8_t *)bp->b_un.b_addr; 5396 count = MIN(bp->b_bcount, pllen); 5397 scsipkt->pkt_state |= STATE_XFERRED_DATA; 5398 scsipkt->pkt_resid = 0; 5399 pllen = count; 5400 5401 /* 5402 * Check the header to skip the block descriptor(s) - we 5403 * do not support setting device capacity. 5404 * Existing macros do not recognize long LBA dscriptor, 5405 * hence manual calculation. 5406 */ 5407 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 5408 /* 6-bytes CMD, 4 bytes header */ 5409 if (count <= 4) 5410 goto done; /* header only */ 5411 len = buf[3] + 4; 5412 } else { 5413 /* 10-bytes CMD, 8 bytes header */ 5414 if (count <= 8) 5415 goto done; /* header only */ 5416 len = buf[6]; 5417 len = (len << 8) + buf[7] + 8; 5418 } 5419 if (len >= count) 5420 goto done; /* header + descriptor(s) only */ 5421 5422 pllen -= len; /* remaining data length */ 5423 5424 /* 5425 * We may be executing SATA command and want to execute it 5426 * in SYNCH mode, regardless of scsi_pkt setting. 5427 * Save scsi_pkt setting and indicate SYNCH mode 5428 */ 5429 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5430 scsipkt->pkt_comp != NULL) { 5431 scsipkt->pkt_flags |= FLAG_NOINTR; 5432 } 5433 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH; 5434 5435 /* 5436 * len is now the offset to a first mode select page 5437 * Process all pages 5438 */ 5439 while (pllen > 0) { 5440 switch ((int)buf[len]) { 5441 case MODEPAGE_CACHING: 5442 /* No support for SP (saving) */ 5443 if (scsipkt->pkt_cdbp[1] & 0x01) { 5444 *scsipkt->pkt_scbp = STATUS_CHECK; 5445 sense = sata_arq_sense(spx); 5446 sense->es_key = KEY_ILLEGAL_REQUEST; 5447 sense->es_add_code = 5448 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5449 goto done; 5450 } 5451 stat = sata_mode_select_page_8(spx, 5452 (struct mode_cache_scsi3 *)&buf[len], 5453 pllen, &pagelen, &rval, &dmod); 5454 /* 5455 * The pagelen value indicates the number of 5456 * parameter bytes already processed. 5457 * The rval is the return value from 5458 * sata_tran_start(). 5459 * The stat indicates the overall status of 5460 * the operation(s). 5461 */ 5462 if (stat != SATA_SUCCESS) 5463 /* 5464 * Page processing did not succeed - 5465 * all error info is already set-up, 5466 * just return 5467 */ 5468 pllen = 0; /* this breaks the loop */ 5469 else { 5470 len += pagelen; 5471 pllen -= pagelen; 5472 } 5473 break; 5474 5475 case MODEPAGE_INFO_EXCPT: 5476 stat = sata_mode_select_page_1c(spx, 5477 (struct mode_info_excpt_page *)&buf[len], 5478 pllen, &pagelen, &rval, &dmod); 5479 /* 5480 * The pagelen value indicates the number of 5481 * parameter bytes already processed. 5482 * The rval is the return value from 5483 * sata_tran_start(). 5484 * The stat indicates the overall status of 5485 * the operation(s). 5486 */ 5487 if (stat != SATA_SUCCESS) 5488 /* 5489 * Page processing did not succeed - 5490 * all error info is already set-up, 5491 * just return 5492 */ 5493 pllen = 0; /* this breaks the loop */ 5494 else { 5495 len += pagelen; 5496 pllen -= pagelen; 5497 } 5498 break; 5499 5500 case MODEPAGE_ACOUSTIC_MANAG: 5501 stat = sata_mode_select_page_30(spx, 5502 (struct mode_acoustic_management *) 5503 &buf[len], pllen, &pagelen, &rval, &dmod); 5504 /* 5505 * The pagelen value indicates the number of 5506 * parameter bytes already processed. 5507 * The rval is the return value from 5508 * sata_tran_start(). 5509 * The stat indicates the overall status of 5510 * the operation(s). 5511 */ 5512 if (stat != SATA_SUCCESS) 5513 /* 5514 * Page processing did not succeed - 5515 * all error info is already set-up, 5516 * just return 5517 */ 5518 pllen = 0; /* this breaks the loop */ 5519 else { 5520 len += pagelen; 5521 pllen -= pagelen; 5522 } 5523 5524 break; 5525 case MODEPAGE_POWER_COND: 5526 stat = sata_mode_select_page_1a(spx, 5527 (struct mode_info_power_cond *)&buf[len], 5528 pllen, &pagelen, &rval, &dmod); 5529 /* 5530 * The pagelen value indicates the number of 5531 * parameter bytes already processed. 5532 * The rval is the return value from 5533 * sata_tran_start(). 5534 * The stat indicates the overall status of 5535 * the operation(s). 5536 */ 5537 if (stat != SATA_SUCCESS) 5538 /* 5539 * Page processing did not succeed - 5540 * all error info is already set-up, 5541 * just return 5542 */ 5543 pllen = 0; /* this breaks the loop */ 5544 else { 5545 len += pagelen; 5546 pllen -= pagelen; 5547 } 5548 break; 5549 default: 5550 *scsipkt->pkt_scbp = STATUS_CHECK; 5551 sense = sata_arq_sense(spx); 5552 sense->es_key = KEY_ILLEGAL_REQUEST; 5553 sense->es_add_code = 5554 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 5555 goto done; 5556 } 5557 } 5558 } 5559 done: 5560 mutex_exit(cport_mutex); 5561 /* 5562 * If device parameters were modified, fetch and store the new 5563 * Identify Device data. Since port mutex could have been released 5564 * for accessing HBA driver, we need to re-check device existence. 5565 */ 5566 if (dmod != 0) { 5567 sata_drive_info_t new_sdinfo, *sdinfo; 5568 int rv = 0; 5569 5570 /* 5571 * Following statement has to be changed if this function is 5572 * used for devices other than SATA hard disks. 5573 */ 5574 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK; 5575 5576 new_sdinfo.satadrv_addr = 5577 spx->txlt_sata_pkt->satapkt_device.satadev_addr; 5578 rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst, 5579 &new_sdinfo); 5580 5581 mutex_enter(cport_mutex); 5582 /* 5583 * Since port mutex could have been released when 5584 * accessing HBA driver, we need to re-check that the 5585 * framework still holds the device info structure. 5586 */ 5587 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 5588 &spx->txlt_sata_pkt->satapkt_device); 5589 if (sdinfo != NULL) { 5590 /* 5591 * Device still has info structure in the 5592 * sata framework. Copy newly fetched info 5593 */ 5594 if (rv == 0) { 5595 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 5596 sata_save_drive_settings(sdinfo); 5597 } else { 5598 /* 5599 * Could not fetch new data - invalidate 5600 * sata_drive_info. That makes device 5601 * unusable. 5602 */ 5603 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 5604 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 5605 } 5606 } 5607 if (rv != 0 || sdinfo == NULL) { 5608 /* 5609 * This changes the overall mode select completion 5610 * reason to a failed one !!!!! 5611 */ 5612 *scsipkt->pkt_scbp = STATUS_CHECK; 5613 sense = sata_arq_sense(spx); 5614 scsipkt->pkt_reason = CMD_INCOMPLETE; 5615 rval = TRAN_ACCEPT; 5616 } 5617 mutex_exit(cport_mutex); 5618 } 5619 /* Restore the scsi pkt flags */ 5620 scsipkt->pkt_flags &= ~FLAG_NOINTR; 5621 scsipkt->pkt_flags |= nointr_flag; 5622 5623 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5624 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5625 5626 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5627 scsipkt->pkt_comp != NULL) { 5628 /* scsi callback required */ 5629 if (servicing_interrupt()) { 5630 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5631 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 5632 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 5633 TASKQID_INVALID) { 5634 return (TRAN_BUSY); 5635 } 5636 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5637 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 5638 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 5639 /* Scheduling the callback failed */ 5640 return (TRAN_BUSY); 5641 } 5642 } 5643 5644 return (rval); 5645 } 5646 5647 /* 5648 * Translate command: ATA Pass Through 5649 * Incomplete implementation. Only supports No-Data, PIO Data-In, and 5650 * PIO Data-Out protocols. Also supports CK_COND bit. 5651 * 5652 * Mapping of the incoming CDB bytes to the outgoing satacmd bytes is 5653 * described in Table 111 of SAT-2 (Draft 9). 5654 */ 5655 static int 5656 sata_txlt_ata_pass_thru(sata_pkt_txlate_t *spx) 5657 { 5658 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5659 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5660 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5661 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 5662 uint32_t xfer_len; 5663 int extend = 0; 5664 int synch, rval, reason; 5665 5666 mutex_enter(cport_mutex); 5667 5668 rval = sata_txlt_generic_pkt_info(spx, &reason, 1); 5669 if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5670 mutex_exit(cport_mutex); 5671 return (rval); 5672 } 5673 5674 /* T_DIR bit */ 5675 if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_T_DIR) 5676 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 5677 else 5678 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 5679 5680 /* MULTIPLE_COUNT field. If non-zero, invalid command (for now). */ 5681 if (((scsipkt->pkt_cdbp[1] >> 5) & 0x7) != 0) { 5682 mutex_exit(cport_mutex); 5683 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5684 } 5685 5686 /* OFFLINE field. If non-zero, invalid command (for now). */ 5687 if (((scsipkt->pkt_cdbp[2] >> 6) & 0x3) != 0) { 5688 mutex_exit(cport_mutex); 5689 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5690 } 5691 5692 /* PROTOCOL field */ 5693 switch ((scsipkt->pkt_cdbp[1] >> 1) & 0xf) { 5694 case SATL_APT_P_HW_RESET: 5695 case SATL_APT_P_SRST: 5696 case SATL_APT_P_DMA: 5697 case SATL_APT_P_DMA_QUEUED: 5698 case SATL_APT_P_DEV_DIAG: 5699 case SATL_APT_P_DEV_RESET: 5700 case SATL_APT_P_UDMA_IN: 5701 case SATL_APT_P_UDMA_OUT: 5702 case SATL_APT_P_FPDMA: 5703 case SATL_APT_P_RET_RESP: 5704 /* Not yet implemented */ 5705 default: 5706 mutex_exit(cport_mutex); 5707 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5708 5709 case SATL_APT_P_NON_DATA: 5710 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 5711 break; 5712 5713 case SATL_APT_P_PIO_DATA_IN: 5714 /* If PROTOCOL disagrees with T_DIR, invalid command */ 5715 if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_WRITE) { 5716 mutex_exit(cport_mutex); 5717 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5718 } 5719 5720 /* if there is a buffer, release its DMA resources */ 5721 if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) { 5722 sata_scsi_dmafree(NULL, scsipkt); 5723 } else { 5724 /* if there is no buffer, how do you PIO in? */ 5725 mutex_exit(cport_mutex); 5726 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5727 } 5728 5729 break; 5730 5731 case SATL_APT_P_PIO_DATA_OUT: 5732 /* If PROTOCOL disagrees with T_DIR, invalid command */ 5733 if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_READ) { 5734 mutex_exit(cport_mutex); 5735 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5736 } 5737 5738 /* if there is a buffer, release its DMA resources */ 5739 if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) { 5740 sata_scsi_dmafree(NULL, scsipkt); 5741 } else { 5742 /* if there is no buffer, how do you PIO out? */ 5743 mutex_exit(cport_mutex); 5744 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5745 } 5746 5747 break; 5748 } 5749 5750 /* Assume LBA28 by default */ 5751 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 5752 scmd->satacmd_lba_low_msb = 0; 5753 scmd->satacmd_lba_mid_msb = 0; 5754 scmd->satacmd_lba_high_msb = 0; 5755 5756 scmd->satacmd_features_reg_ext = 0; 5757 scmd->satacmd_sec_count_msb = 0; 5758 5759 /* Parse the ATA cmd fields, transfer some straight to the satacmd */ 5760 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 5761 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12: 5762 scmd->satacmd_lba_low_lsb = scsipkt->pkt_cdbp[5]; 5763 scmd->satacmd_lba_mid_lsb = scsipkt->pkt_cdbp[6]; 5764 scmd->satacmd_lba_high_lsb = scsipkt->pkt_cdbp[7]; 5765 5766 scmd->satacmd_features_reg = scsipkt->pkt_cdbp[3]; 5767 scmd->satacmd_sec_count_lsb = scsipkt->pkt_cdbp[4]; 5768 5769 scmd->satacmd_device_reg = scsipkt->pkt_cdbp[8]; 5770 scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[9]; 5771 break; 5772 5773 case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16: 5774 scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13]; 5775 scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14]; 5776 5777 scmd->satacmd_lba_low_lsb = scsipkt->pkt_cdbp[8]; 5778 scmd->satacmd_lba_mid_lsb = scsipkt->pkt_cdbp[10]; 5779 scmd->satacmd_lba_high_lsb = scsipkt->pkt_cdbp[12]; 5780 5781 scmd->satacmd_features_reg = scsipkt->pkt_cdbp[4]; 5782 scmd->satacmd_sec_count_lsb = scsipkt->pkt_cdbp[6]; 5783 5784 if (scsipkt->pkt_cdbp[1] & SATL_APT_BM_EXTEND) { 5785 extend = 1; 5786 5787 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 5788 scmd->satacmd_lba_low_msb = scsipkt->pkt_cdbp[7]; 5789 scmd->satacmd_lba_mid_msb = scsipkt->pkt_cdbp[9]; 5790 scmd->satacmd_lba_high_msb = scsipkt->pkt_cdbp[11]; 5791 5792 scmd->satacmd_features_reg_ext = scsipkt->pkt_cdbp[3]; 5793 scmd->satacmd_sec_count_msb = scsipkt->pkt_cdbp[5]; 5794 } 5795 break; 5796 5797 default: 5798 /* No other SCSI ops should ever reach this function */ 5799 cmn_err(CE_PANIC, "unexpected ATA pass-thru cmd %x", 5800 scsipkt->pkt_cdbp[0]); 5801 } 5802 5803 /* CK_COND bit */ 5804 if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) { 5805 if (extend) { 5806 scmd->satacmd_flags.sata_copy_out_sec_count_msb = 1; 5807 scmd->satacmd_flags.sata_copy_out_lba_low_msb = 1; 5808 scmd->satacmd_flags.sata_copy_out_lba_mid_msb = 1; 5809 scmd->satacmd_flags.sata_copy_out_lba_high_msb = 1; 5810 } 5811 5812 scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1; 5813 scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1; 5814 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1; 5815 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1; 5816 scmd->satacmd_flags.sata_copy_out_device_reg = 1; 5817 scmd->satacmd_flags.sata_copy_out_error_reg = 1; 5818 } 5819 5820 /* Determine transfer length */ 5821 switch (scsipkt->pkt_cdbp[2] & 0x03) { /* T_LENGTH field */ 5822 case 1: 5823 /* Length is in the FEATURE field */ 5824 xfer_len = (uint32_t)scmd->satacmd_features_reg_ext << 8 | 5825 scmd->satacmd_features_reg; 5826 5827 /* If BYTE_BLOCK is set, above value is in units of blocks */ 5828 if (((scsipkt->pkt_cdbp[2] >> 2) & 1) == 0) 5829 xfer_len *= SATA_DISK_SECTOR_SIZE; 5830 break; 5831 case 2: 5832 /* Length is in the COUNT field */ 5833 xfer_len = (uint32_t)scmd->satacmd_sec_count_msb << 8 | 5834 scmd->satacmd_sec_count_lsb; 5835 5836 /* If BYTE_BLOCK is set, above value is in units of blocks */ 5837 if (((scsipkt->pkt_cdbp[2] >> 2) & 1) == 0) 5838 xfer_len *= SATA_DISK_SECTOR_SIZE; 5839 break; 5840 case 3: 5841 /* 5842 * Length is transport specific. The spec is a bit vague on 5843 * this, but it seems like using buf->b_bcount is the most 5844 * reasonable analogue in our situation. b_bcount is in 5845 * units of bytes. 5846 */ 5847 xfer_len = bp->b_bcount; 5848 break; 5849 default: 5850 xfer_len = 0; 5851 } 5852 5853 /* Don't allow a transfer larger than what the struct buf supports */ 5854 if (xfer_len > bp->b_bcount) { 5855 mutex_exit(cport_mutex); 5856 return (sata_txlt_ata_pass_thru_illegal_cmd(spx)); 5857 } 5858 5859 /* Start processing command */ 5860 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5861 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_apt_completion; 5862 synch = FALSE; 5863 } else { 5864 synch = TRUE; 5865 } 5866 5867 if (sata_hba_start(spx, &rval) != 0) { 5868 mutex_exit(cport_mutex); 5869 return (rval); 5870 } 5871 5872 mutex_exit(cport_mutex); 5873 5874 if (synch) { 5875 sata_txlt_apt_completion(spx->txlt_sata_pkt); 5876 } 5877 5878 return (TRAN_ACCEPT); 5879 } 5880 5881 /* 5882 * Translate command: Log Sense 5883 */ 5884 static int 5885 sata_txlt_log_sense(sata_pkt_txlate_t *spx) 5886 { 5887 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5888 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5889 sata_drive_info_t *sdinfo; 5890 struct scsi_extended_sense *sense; 5891 int len, count, alc_len; 5892 int pc; /* Page Control code */ 5893 int page_code; /* Page code */ 5894 uint8_t *buf; /* log sense buffer */ 5895 int rval, reason; 5896 #define MAX_LOG_SENSE_PAGE_SIZE 512 5897 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 5898 5899 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5900 "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n", 5901 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 5902 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 5903 5904 if (servicing_interrupt()) { 5905 buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_NOSLEEP); 5906 if (buf == NULL) { 5907 return (TRAN_BUSY); 5908 } 5909 } else { 5910 buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP); 5911 } 5912 5913 mutex_enter(cport_mutex); 5914 5915 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 5916 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5917 mutex_exit(cport_mutex); 5918 kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 5919 return (rval); 5920 } 5921 5922 scsipkt->pkt_reason = CMD_CMPLT; 5923 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5924 STATE_SENT_CMD | STATE_GOT_STATUS; 5925 5926 pc = scsipkt->pkt_cdbp[2] >> 6; 5927 page_code = scsipkt->pkt_cdbp[2] & 0x3f; 5928 5929 /* Reject not supported request for all but cumulative values */ 5930 switch (pc) { 5931 case PC_CUMULATIVE_VALUES: 5932 break; 5933 default: 5934 *scsipkt->pkt_scbp = STATUS_CHECK; 5935 sense = sata_arq_sense(spx); 5936 sense->es_key = KEY_ILLEGAL_REQUEST; 5937 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5938 goto done; 5939 } 5940 5941 switch (page_code) { 5942 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 5943 case PAGE_CODE_SELF_TEST_RESULTS: 5944 case PAGE_CODE_INFORMATION_EXCEPTIONS: 5945 case PAGE_CODE_SMART_READ_DATA: 5946 case PAGE_CODE_START_STOP_CYCLE_COUNTER: 5947 break; 5948 default: 5949 *scsipkt->pkt_scbp = STATUS_CHECK; 5950 sense = sata_arq_sense(spx); 5951 sense->es_key = KEY_ILLEGAL_REQUEST; 5952 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5953 goto done; 5954 } 5955 5956 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 5957 /* 5958 * Because log sense uses local buffers for data retrieval from 5959 * the devices and sets the data programatically in the 5960 * original specified buffer, release preallocated DMA 5961 * resources before storing data in the original buffer, 5962 * so no unwanted DMA sync would take place. 5963 */ 5964 sata_id_t *sata_id; 5965 5966 sata_scsi_dmafree(NULL, scsipkt); 5967 5968 len = 0; 5969 5970 /* Build log parameter header */ 5971 buf[len++] = page_code; /* page code as in the CDB */ 5972 buf[len++] = 0; /* reserved */ 5973 buf[len++] = 0; /* Zero out page length for now (MSB) */ 5974 buf[len++] = 0; /* (LSB) */ 5975 5976 sdinfo = sata_get_device_info( 5977 spx->txlt_sata_hba_inst, 5978 &spx->txlt_sata_pkt->satapkt_device); 5979 5980 /* 5981 * Add requested pages. 5982 */ 5983 switch (page_code) { 5984 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 5985 len = sata_build_lsense_page_0(sdinfo, buf + len); 5986 break; 5987 case PAGE_CODE_SELF_TEST_RESULTS: 5988 sata_id = &sdinfo->satadrv_id; 5989 if ((! (sata_id->ai_cmdset84 & 5990 SATA_SMART_SELF_TEST_SUPPORTED)) || 5991 (! (sata_id->ai_features87 & 5992 SATA_SMART_SELF_TEST_SUPPORTED))) { 5993 *scsipkt->pkt_scbp = STATUS_CHECK; 5994 sense = sata_arq_sense(spx); 5995 sense->es_key = KEY_ILLEGAL_REQUEST; 5996 sense->es_add_code = 5997 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5998 5999 goto done; 6000 } 6001 len = sata_build_lsense_page_10(sdinfo, buf + len, 6002 spx->txlt_sata_hba_inst); 6003 break; 6004 case PAGE_CODE_INFORMATION_EXCEPTIONS: 6005 sata_id = &sdinfo->satadrv_id; 6006 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 6007 *scsipkt->pkt_scbp = STATUS_CHECK; 6008 sense = sata_arq_sense(spx); 6009 sense->es_key = KEY_ILLEGAL_REQUEST; 6010 sense->es_add_code = 6011 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 6012 6013 goto done; 6014 } 6015 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 6016 *scsipkt->pkt_scbp = STATUS_CHECK; 6017 sense = sata_arq_sense(spx); 6018 sense->es_key = KEY_ABORTED_COMMAND; 6019 sense->es_add_code = 6020 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 6021 sense->es_qual_code = 6022 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 6023 6024 goto done; 6025 } 6026 6027 len = sata_build_lsense_page_2f(sdinfo, buf + len, 6028 spx->txlt_sata_hba_inst); 6029 break; 6030 case PAGE_CODE_SMART_READ_DATA: 6031 sata_id = &sdinfo->satadrv_id; 6032 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 6033 *scsipkt->pkt_scbp = STATUS_CHECK; 6034 sense = sata_arq_sense(spx); 6035 sense->es_key = KEY_ILLEGAL_REQUEST; 6036 sense->es_add_code = 6037 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 6038 6039 goto done; 6040 } 6041 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 6042 *scsipkt->pkt_scbp = STATUS_CHECK; 6043 sense = sata_arq_sense(spx); 6044 sense->es_key = KEY_ABORTED_COMMAND; 6045 sense->es_add_code = 6046 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 6047 sense->es_qual_code = 6048 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 6049 6050 goto done; 6051 } 6052 6053 /* This page doesn't include a page header */ 6054 len = sata_build_lsense_page_30(sdinfo, buf, 6055 spx->txlt_sata_hba_inst); 6056 goto no_header; 6057 case PAGE_CODE_START_STOP_CYCLE_COUNTER: 6058 sata_id = &sdinfo->satadrv_id; 6059 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 6060 *scsipkt->pkt_scbp = STATUS_CHECK; 6061 sense = sata_arq_sense(spx); 6062 sense->es_key = KEY_ILLEGAL_REQUEST; 6063 sense->es_add_code = 6064 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 6065 6066 goto done; 6067 } 6068 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 6069 *scsipkt->pkt_scbp = STATUS_CHECK; 6070 sense = sata_arq_sense(spx); 6071 sense->es_key = KEY_ABORTED_COMMAND; 6072 sense->es_add_code = 6073 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 6074 sense->es_qual_code = 6075 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 6076 6077 goto done; 6078 } 6079 len = sata_build_lsense_page_0e(sdinfo, buf, spx); 6080 goto no_header; 6081 default: 6082 /* Invalid request */ 6083 *scsipkt->pkt_scbp = STATUS_CHECK; 6084 sense = sata_arq_sense(spx); 6085 sense->es_key = KEY_ILLEGAL_REQUEST; 6086 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 6087 goto done; 6088 } 6089 6090 /* set parameter log sense data length */ 6091 buf[2] = len >> 8; /* log sense length (MSB) */ 6092 buf[3] = len & 0xff; /* log sense length (LSB) */ 6093 6094 len += SCSI_LOG_PAGE_HDR_LEN; 6095 ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE); 6096 6097 no_header: 6098 /* Check allocation length */ 6099 alc_len = scsipkt->pkt_cdbp[7]; 6100 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 6101 6102 /* 6103 * We do not check for possible parameters truncation 6104 * (alc_len < len) assuming that the target driver works 6105 * correctly. Just avoiding overrun. 6106 * Copy no more than requested and possible, buffer-wise. 6107 */ 6108 count = MIN(alc_len, len); 6109 count = MIN(bp->b_bcount, count); 6110 bcopy(buf, bp->b_un.b_addr, count); 6111 6112 scsipkt->pkt_state |= STATE_XFERRED_DATA; 6113 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 6114 } 6115 *scsipkt->pkt_scbp = STATUS_GOOD; 6116 done: 6117 mutex_exit(cport_mutex); 6118 (void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 6119 6120 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6121 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6122 6123 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6124 scsipkt->pkt_comp != NULL) { 6125 /* scsi callback required */ 6126 if (servicing_interrupt()) { 6127 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6128 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 6129 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 6130 TASKQID_INVALID) { 6131 return (TRAN_BUSY); 6132 } 6133 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6134 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 6135 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 6136 /* Scheduling the callback failed */ 6137 return (TRAN_BUSY); 6138 } 6139 } 6140 6141 return (TRAN_ACCEPT); 6142 } 6143 6144 /* 6145 * Translate command: Log Select 6146 * Not implemented at this time - returns invalid command response. 6147 */ 6148 static int 6149 sata_txlt_log_select(sata_pkt_txlate_t *spx) 6150 { 6151 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6152 "sata_txlt_log_select\n", NULL); 6153 6154 return (sata_txlt_invalid_command(spx)); 6155 } 6156 6157 6158 /* 6159 * Translate command: Read (various types). 6160 * Translated into appropriate type of ATA READ command 6161 * for SATA hard disks. 6162 * Both the device capabilities and requested operation mode are 6163 * considered. 6164 * 6165 * Following scsi cdb fields are ignored: 6166 * rdprotect, dpo, fua, fua_nv, group_number. 6167 * 6168 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 6169 * enable variable sata_func_enable), the capability of the controller and 6170 * capability of a device are checked and if both support queueing, read 6171 * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT 6172 * command rather than plain READ_XXX command. 6173 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 6174 * both the controller and device suport such functionality, the read 6175 * request will be translated to READ_FPDMA_QUEUED command. 6176 * In both cases the maximum queue depth is derived as minimum of: 6177 * HBA capability,device capability and sata_max_queue_depth variable setting. 6178 * The value passed to HBA driver is decremented by 1, because only 5 bits are 6179 * used to pass max queue depth value, and the maximum possible queue depth 6180 * is 32. 6181 * 6182 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 6183 * appropriate values in scsi_pkt fields. 6184 */ 6185 static int 6186 sata_txlt_read(sata_pkt_txlate_t *spx) 6187 { 6188 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6189 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6190 sata_drive_info_t *sdinfo; 6191 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 6192 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 6193 uint16_t sec_count; 6194 uint64_t lba; 6195 int rval, reason; 6196 int synch; 6197 6198 mutex_enter(cport_mutex); 6199 6200 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 6201 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 6202 mutex_exit(cport_mutex); 6203 return (rval); 6204 } 6205 6206 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 6207 &spx->txlt_sata_pkt->satapkt_device); 6208 6209 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 6210 /* 6211 * Extract LBA and sector count from scsi CDB. 6212 */ 6213 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 6214 case SCMD_READ: 6215 /* 6-byte scsi read cmd : 0x08 */ 6216 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 6217 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 6218 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6219 sec_count = scsipkt->pkt_cdbp[4]; 6220 /* sec_count 0 will be interpreted as 256 by a device */ 6221 break; 6222 case SCMD_READ_G1: 6223 /* 10-bytes scsi read command : 0x28 */ 6224 lba = scsipkt->pkt_cdbp[2]; 6225 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6226 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 6227 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 6228 sec_count = scsipkt->pkt_cdbp[7]; 6229 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 6230 break; 6231 case SCMD_READ_G5: 6232 /* 12-bytes scsi read command : 0xA8 */ 6233 lba = scsipkt->pkt_cdbp[2]; 6234 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6235 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 6236 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 6237 sec_count = scsipkt->pkt_cdbp[6]; 6238 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 6239 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 6240 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 6241 break; 6242 case SCMD_READ_G4: 6243 /* 16-bytes scsi read command : 0x88 */ 6244 lba = scsipkt->pkt_cdbp[2]; 6245 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6246 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 6247 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 6248 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 6249 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 6250 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 6251 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 6252 sec_count = scsipkt->pkt_cdbp[10]; 6253 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 6254 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 6255 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 6256 break; 6257 default: 6258 /* Unsupported command */ 6259 mutex_exit(cport_mutex); 6260 return (sata_txlt_invalid_command(spx)); 6261 } 6262 6263 /* 6264 * Check if specified address exceeds device capacity 6265 */ 6266 if ((lba >= sdinfo->satadrv_capacity) || 6267 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 6268 /* LBA out of range */ 6269 mutex_exit(cport_mutex); 6270 return (sata_txlt_lba_out_of_range(spx)); 6271 } 6272 6273 /* 6274 * For zero-length transfer, emulate good completion of the command 6275 * (reasons for rejecting the command were already checked). 6276 * No DMA resources were allocated. 6277 */ 6278 if (spx->txlt_dma_cookie_list == NULL) { 6279 mutex_exit(cport_mutex); 6280 return (sata_emul_rw_completion(spx)); 6281 } 6282 6283 /* 6284 * Build cmd block depending on the device capability and 6285 * requested operation mode. 6286 * Do not bother with non-dma mode - we are working only with 6287 * devices supporting DMA. 6288 */ 6289 scmd->satacmd_addr_type = ATA_ADDR_LBA; 6290 scmd->satacmd_device_reg = SATA_ADH_LBA; 6291 scmd->satacmd_cmd_reg = SATAC_READ_DMA; 6292 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 6293 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 6294 scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT; 6295 scmd->satacmd_sec_count_msb = sec_count >> 8; 6296 #ifndef __lock_lint 6297 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 6298 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 6299 scmd->satacmd_lba_high_msb = lba >> 40; 6300 #endif 6301 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 6302 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 6303 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 6304 } 6305 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 6306 scmd->satacmd_lba_low_lsb = lba & 0xff; 6307 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 6308 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 6309 scmd->satacmd_features_reg = 0; 6310 scmd->satacmd_status_reg = 0; 6311 scmd->satacmd_error_reg = 0; 6312 6313 /* 6314 * Check if queueing commands should be used and switch 6315 * to appropriate command if possible 6316 */ 6317 if (sata_func_enable & SATA_ENABLE_QUEUING) { 6318 boolean_t using_queuing; 6319 6320 /* Queuing supported by controller and device? */ 6321 if ((sata_func_enable & SATA_ENABLE_NCQ) && 6322 (sdinfo->satadrv_features_support & 6323 SATA_DEV_F_NCQ) && 6324 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 6325 SATA_CTLF_NCQ)) { 6326 using_queuing = B_TRUE; 6327 6328 /* NCQ supported - use FPDMA READ */ 6329 scmd->satacmd_cmd_reg = 6330 SATAC_READ_FPDMA_QUEUED; 6331 scmd->satacmd_features_reg_ext = 6332 scmd->satacmd_sec_count_msb; 6333 scmd->satacmd_sec_count_msb = 0; 6334 } else if ((sdinfo->satadrv_features_support & 6335 SATA_DEV_F_TCQ) && 6336 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 6337 SATA_CTLF_QCMD)) { 6338 using_queuing = B_TRUE; 6339 6340 /* Legacy queueing */ 6341 if (sdinfo->satadrv_features_support & 6342 SATA_DEV_F_LBA48) { 6343 scmd->satacmd_cmd_reg = 6344 SATAC_READ_DMA_QUEUED_EXT; 6345 scmd->satacmd_features_reg_ext = 6346 scmd->satacmd_sec_count_msb; 6347 scmd->satacmd_sec_count_msb = 0; 6348 } else { 6349 scmd->satacmd_cmd_reg = 6350 SATAC_READ_DMA_QUEUED; 6351 } 6352 } else /* NCQ nor legacy queuing not supported */ 6353 using_queuing = B_FALSE; 6354 6355 /* 6356 * If queuing, the sector count goes in the features register 6357 * and the secount count will contain the tag. 6358 */ 6359 if (using_queuing) { 6360 scmd->satacmd_features_reg = 6361 scmd->satacmd_sec_count_lsb; 6362 scmd->satacmd_sec_count_lsb = 0; 6363 scmd->satacmd_flags.sata_queued = B_TRUE; 6364 6365 /* Set-up maximum queue depth */ 6366 scmd->satacmd_flags.sata_max_queue_depth = 6367 sdinfo->satadrv_max_queue_depth - 1; 6368 } else if (sdinfo->satadrv_features_enabled & 6369 SATA_DEV_F_E_UNTAGGED_QING) { 6370 /* 6371 * Although NCQ/TCQ is not enabled, untagged queuing 6372 * may be still used. 6373 * Set-up the maximum untagged queue depth. 6374 * Use controller's queue depth from sata_hba_tran. 6375 * SATA HBA drivers may ignore this value and rely on 6376 * the internal limits.For drivers that do not 6377 * ignore untaged queue depth, limit the value to 6378 * SATA_MAX_QUEUE_DEPTH (32), as this is the 6379 * largest value that can be passed via 6380 * satacmd_flags.sata_max_queue_depth. 6381 */ 6382 scmd->satacmd_flags.sata_max_queue_depth = 6383 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ? 6384 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1; 6385 6386 } else { 6387 scmd->satacmd_flags.sata_max_queue_depth = 0; 6388 } 6389 } else 6390 scmd->satacmd_flags.sata_max_queue_depth = 0; 6391 6392 SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst, 6393 "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n", 6394 scmd->satacmd_cmd_reg, lba, sec_count); 6395 6396 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 6397 /* Need callback function */ 6398 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 6399 synch = FALSE; 6400 } else 6401 synch = TRUE; 6402 6403 /* Transfer command to HBA */ 6404 if (sata_hba_start(spx, &rval) != 0) { 6405 /* Pkt not accepted for execution */ 6406 mutex_exit(cport_mutex); 6407 return (rval); 6408 } 6409 mutex_exit(cport_mutex); 6410 /* 6411 * If execution is non-synchronous, 6412 * a callback function will handle potential errors, translate 6413 * the response and will do a callback to a target driver. 6414 * If it was synchronous, check execution status using the same 6415 * framework callback. 6416 */ 6417 if (synch) { 6418 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6419 "synchronous execution status %x\n", 6420 spx->txlt_sata_pkt->satapkt_reason); 6421 sata_txlt_rw_completion(spx->txlt_sata_pkt); 6422 } 6423 return (TRAN_ACCEPT); 6424 } 6425 6426 6427 /* 6428 * SATA translate command: Write (various types) 6429 * Translated into appropriate type of ATA WRITE command 6430 * for SATA hard disks. 6431 * Both the device capabilities and requested operation mode are 6432 * considered. 6433 * 6434 * Following scsi cdb fields are ignored: 6435 * rwprotect, dpo, fua, fua_nv, group_number. 6436 * 6437 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 6438 * enable variable sata_func_enable), the capability of the controller and 6439 * capability of a device are checked and if both support queueing, write 6440 * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT 6441 * command rather than plain WRITE_XXX command. 6442 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 6443 * both the controller and device suport such functionality, the write 6444 * request will be translated to WRITE_FPDMA_QUEUED command. 6445 * In both cases the maximum queue depth is derived as minimum of: 6446 * HBA capability,device capability and sata_max_queue_depth variable setting. 6447 * The value passed to HBA driver is decremented by 1, because only 5 bits are 6448 * used to pass max queue depth value, and the maximum possible queue depth 6449 * is 32. 6450 * 6451 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 6452 * appropriate values in scsi_pkt fields. 6453 */ 6454 static int 6455 sata_txlt_write(sata_pkt_txlate_t *spx) 6456 { 6457 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6458 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6459 sata_drive_info_t *sdinfo; 6460 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 6461 uint16_t sec_count; 6462 uint64_t lba; 6463 int rval, reason; 6464 int synch; 6465 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 6466 6467 mutex_enter(cport_mutex); 6468 6469 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 6470 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 6471 mutex_exit(cport_mutex); 6472 return (rval); 6473 } 6474 6475 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 6476 &spx->txlt_sata_pkt->satapkt_device); 6477 6478 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 6479 /* 6480 * Extract LBA and sector count from scsi CDB 6481 */ 6482 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 6483 case SCMD_WRITE: 6484 /* 6-byte scsi read cmd : 0x0A */ 6485 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 6486 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 6487 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6488 sec_count = scsipkt->pkt_cdbp[4]; 6489 /* sec_count 0 will be interpreted as 256 by a device */ 6490 break; 6491 case SCMD_WRITE_G1: 6492 /* 10-bytes scsi write command : 0x2A */ 6493 lba = scsipkt->pkt_cdbp[2]; 6494 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6495 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 6496 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 6497 sec_count = scsipkt->pkt_cdbp[7]; 6498 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 6499 break; 6500 case SCMD_WRITE_G5: 6501 /* 12-bytes scsi read command : 0xAA */ 6502 lba = scsipkt->pkt_cdbp[2]; 6503 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6504 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 6505 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 6506 sec_count = scsipkt->pkt_cdbp[6]; 6507 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 6508 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 6509 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 6510 break; 6511 case SCMD_WRITE_G4: 6512 /* 16-bytes scsi write command : 0x8A */ 6513 lba = scsipkt->pkt_cdbp[2]; 6514 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 6515 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 6516 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 6517 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 6518 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 6519 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 6520 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 6521 sec_count = scsipkt->pkt_cdbp[10]; 6522 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 6523 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 6524 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 6525 break; 6526 default: 6527 /* Unsupported command */ 6528 mutex_exit(cport_mutex); 6529 return (sata_txlt_invalid_command(spx)); 6530 } 6531 6532 /* 6533 * Check if specified address and length exceeds device capacity 6534 */ 6535 if ((lba >= sdinfo->satadrv_capacity) || 6536 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 6537 /* LBA out of range */ 6538 mutex_exit(cport_mutex); 6539 return (sata_txlt_lba_out_of_range(spx)); 6540 } 6541 6542 /* 6543 * For zero-length transfer, emulate good completion of the command 6544 * (reasons for rejecting the command were already checked). 6545 * No DMA resources were allocated. 6546 */ 6547 if (spx->txlt_dma_cookie_list == NULL) { 6548 mutex_exit(cport_mutex); 6549 return (sata_emul_rw_completion(spx)); 6550 } 6551 6552 /* 6553 * Build cmd block depending on the device capability and 6554 * requested operation mode. 6555 * Do not bother with non-dma mode- we are working only with 6556 * devices supporting DMA. 6557 */ 6558 scmd->satacmd_addr_type = ATA_ADDR_LBA; 6559 scmd->satacmd_device_reg = SATA_ADH_LBA; 6560 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA; 6561 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 6562 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 6563 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT; 6564 scmd->satacmd_sec_count_msb = sec_count >> 8; 6565 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 6566 #ifndef __lock_lint 6567 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 6568 scmd->satacmd_lba_high_msb = lba >> 40; 6569 #endif 6570 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 6571 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 6572 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 6573 } 6574 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 6575 scmd->satacmd_lba_low_lsb = lba & 0xff; 6576 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 6577 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 6578 scmd->satacmd_features_reg = 0; 6579 scmd->satacmd_status_reg = 0; 6580 scmd->satacmd_error_reg = 0; 6581 6582 /* 6583 * Check if queueing commands should be used and switch 6584 * to appropriate command if possible 6585 */ 6586 if (sata_func_enable & SATA_ENABLE_QUEUING) { 6587 boolean_t using_queuing; 6588 6589 /* Queuing supported by controller and device? */ 6590 if ((sata_func_enable & SATA_ENABLE_NCQ) && 6591 (sdinfo->satadrv_features_support & 6592 SATA_DEV_F_NCQ) && 6593 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 6594 SATA_CTLF_NCQ)) { 6595 using_queuing = B_TRUE; 6596 6597 /* NCQ supported - use FPDMA WRITE */ 6598 scmd->satacmd_cmd_reg = 6599 SATAC_WRITE_FPDMA_QUEUED; 6600 scmd->satacmd_features_reg_ext = 6601 scmd->satacmd_sec_count_msb; 6602 scmd->satacmd_sec_count_msb = 0; 6603 } else if ((sdinfo->satadrv_features_support & 6604 SATA_DEV_F_TCQ) && 6605 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 6606 SATA_CTLF_QCMD)) { 6607 using_queuing = B_TRUE; 6608 6609 /* Legacy queueing */ 6610 if (sdinfo->satadrv_features_support & 6611 SATA_DEV_F_LBA48) { 6612 scmd->satacmd_cmd_reg = 6613 SATAC_WRITE_DMA_QUEUED_EXT; 6614 scmd->satacmd_features_reg_ext = 6615 scmd->satacmd_sec_count_msb; 6616 scmd->satacmd_sec_count_msb = 0; 6617 } else { 6618 scmd->satacmd_cmd_reg = 6619 SATAC_WRITE_DMA_QUEUED; 6620 } 6621 } else /* NCQ nor legacy queuing not supported */ 6622 using_queuing = B_FALSE; 6623 6624 if (using_queuing) { 6625 scmd->satacmd_features_reg = 6626 scmd->satacmd_sec_count_lsb; 6627 scmd->satacmd_sec_count_lsb = 0; 6628 scmd->satacmd_flags.sata_queued = B_TRUE; 6629 /* Set-up maximum queue depth */ 6630 scmd->satacmd_flags.sata_max_queue_depth = 6631 sdinfo->satadrv_max_queue_depth - 1; 6632 } else if (sdinfo->satadrv_features_enabled & 6633 SATA_DEV_F_E_UNTAGGED_QING) { 6634 /* 6635 * Although NCQ/TCQ is not enabled, untagged queuing 6636 * may be still used. 6637 * Set-up the maximum untagged queue depth. 6638 * Use controller's queue depth from sata_hba_tran. 6639 * SATA HBA drivers may ignore this value and rely on 6640 * the internal limits. For drivera that do not 6641 * ignore untaged queue depth, limit the value to 6642 * SATA_MAX_QUEUE_DEPTH (32), as this is the 6643 * largest value that can be passed via 6644 * satacmd_flags.sata_max_queue_depth. 6645 */ 6646 scmd->satacmd_flags.sata_max_queue_depth = 6647 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ? 6648 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1; 6649 6650 } else { 6651 scmd->satacmd_flags.sata_max_queue_depth = 0; 6652 } 6653 } else 6654 scmd->satacmd_flags.sata_max_queue_depth = 0; 6655 6656 SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6657 "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n", 6658 scmd->satacmd_cmd_reg, lba, sec_count); 6659 6660 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 6661 /* Need callback function */ 6662 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 6663 synch = FALSE; 6664 } else 6665 synch = TRUE; 6666 6667 /* Transfer command to HBA */ 6668 if (sata_hba_start(spx, &rval) != 0) { 6669 /* Pkt not accepted for execution */ 6670 mutex_exit(cport_mutex); 6671 return (rval); 6672 } 6673 mutex_exit(cport_mutex); 6674 6675 /* 6676 * If execution is non-synchronous, 6677 * a callback function will handle potential errors, translate 6678 * the response and will do a callback to a target driver. 6679 * If it was synchronous, check execution status using the same 6680 * framework callback. 6681 */ 6682 if (synch) { 6683 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6684 "synchronous execution status %x\n", 6685 spx->txlt_sata_pkt->satapkt_reason); 6686 sata_txlt_rw_completion(spx->txlt_sata_pkt); 6687 } 6688 return (TRAN_ACCEPT); 6689 } 6690 6691 6692 /* 6693 * Implements SCSI SBC WRITE BUFFER command download microcode option 6694 */ 6695 static int 6696 sata_txlt_write_buffer(sata_pkt_txlate_t *spx) 6697 { 6698 #define WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE 4 6699 #define WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE 5 6700 6701 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6702 struct sata_pkt *sata_pkt = spx->txlt_sata_pkt; 6703 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6704 6705 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 6706 struct scsi_extended_sense *sense; 6707 int rval, mode, sector_count, reason; 6708 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 6709 6710 mode = scsipkt->pkt_cdbp[1] & 0x1f; 6711 6712 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6713 "sata_txlt_write_buffer, mode 0x%x\n", mode); 6714 6715 mutex_enter(cport_mutex); 6716 6717 if ((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 6718 TRAN_ACCEPT) { 6719 mutex_exit(cport_mutex); 6720 return (rval); 6721 } 6722 6723 /* Use synchronous mode */ 6724 spx->txlt_sata_pkt->satapkt_op_mode 6725 |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 6726 6727 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 6728 6729 scsipkt->pkt_reason = CMD_CMPLT; 6730 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6731 STATE_SENT_CMD | STATE_GOT_STATUS; 6732 6733 /* 6734 * The SCSI to ATA translation specification only calls 6735 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE. 6736 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but 6737 * ATA 8 (draft) got rid of download microcode for temp 6738 * and it is even optional for ATA 7, so it may be aborted. 6739 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as 6740 * it is not specified and the buffer offset for SCSI is a 16-bit 6741 * value in bytes, but for ATA it is a 16-bit offset in 512 byte 6742 * sectors. Thus the offset really doesn't buy us anything. 6743 * If and when ATA 8 is stabilized and the SCSI to ATA specification 6744 * is revised, this can be revisisted. 6745 */ 6746 /* Reject not supported request */ 6747 switch (mode) { 6748 case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE: 6749 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP; 6750 break; 6751 case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE: 6752 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE; 6753 break; 6754 default: 6755 goto bad_param; 6756 } 6757 6758 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 6759 6760 scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE; 6761 if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0) 6762 goto bad_param; 6763 sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE; 6764 scmd->satacmd_sec_count_lsb = (uint8_t)sector_count; 6765 scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8; 6766 scmd->satacmd_lba_mid_lsb = 0; 6767 scmd->satacmd_lba_high_lsb = 0; 6768 scmd->satacmd_device_reg = 0; 6769 spx->txlt_sata_pkt->satapkt_comp = NULL; 6770 scmd->satacmd_addr_type = 0; 6771 6772 /* Transfer command to HBA */ 6773 if (sata_hba_start(spx, &rval) != 0) { 6774 /* Pkt not accepted for execution */ 6775 mutex_exit(cport_mutex); 6776 return (rval); 6777 } 6778 6779 mutex_exit(cport_mutex); 6780 6781 /* Then we need synchronous check the status of the disk */ 6782 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 6783 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 6784 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 6785 scsipkt->pkt_reason = CMD_CMPLT; 6786 6787 /* Download commmand succeed, so probe and identify device */ 6788 sata_reidentify_device(spx); 6789 } else { 6790 /* Something went wrong, microcode download command failed */ 6791 scsipkt->pkt_reason = CMD_INCOMPLETE; 6792 *scsipkt->pkt_scbp = STATUS_CHECK; 6793 sense = sata_arq_sense(spx); 6794 switch (sata_pkt->satapkt_reason) { 6795 case SATA_PKT_PORT_ERROR: 6796 /* 6797 * We have no device data. Assume no data transfered. 6798 */ 6799 sense->es_key = KEY_HARDWARE_ERROR; 6800 break; 6801 6802 case SATA_PKT_DEV_ERROR: 6803 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 6804 SATA_STATUS_ERR) { 6805 /* 6806 * determine dev error reason from error 6807 * reg content 6808 */ 6809 sata_decode_device_error(spx, sense); 6810 break; 6811 } 6812 /* No extended sense key - no info available */ 6813 break; 6814 6815 case SATA_PKT_TIMEOUT: 6816 scsipkt->pkt_reason = CMD_TIMEOUT; 6817 scsipkt->pkt_statistics |= 6818 STAT_TIMEOUT | STAT_DEV_RESET; 6819 /* No extended sense key ? */ 6820 break; 6821 6822 case SATA_PKT_ABORTED: 6823 scsipkt->pkt_reason = CMD_ABORTED; 6824 scsipkt->pkt_statistics |= STAT_ABORTED; 6825 /* No extended sense key ? */ 6826 break; 6827 6828 case SATA_PKT_RESET: 6829 /* pkt aborted by an explicit reset from a host */ 6830 scsipkt->pkt_reason = CMD_RESET; 6831 scsipkt->pkt_statistics |= STAT_DEV_RESET; 6832 break; 6833 6834 default: 6835 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 6836 "sata_txlt_nodata_cmd_completion: " 6837 "invalid packet completion reason %d", 6838 sata_pkt->satapkt_reason)); 6839 scsipkt->pkt_reason = CMD_TRAN_ERR; 6840 break; 6841 } 6842 6843 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6844 "scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 6845 6846 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 6847 /* scsi callback required */ 6848 scsi_hba_pkt_comp(scsipkt); 6849 } 6850 return (TRAN_ACCEPT); 6851 6852 bad_param: 6853 mutex_exit(cport_mutex); 6854 *scsipkt->pkt_scbp = STATUS_CHECK; 6855 sense = sata_arq_sense(spx); 6856 sense->es_key = KEY_ILLEGAL_REQUEST; 6857 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 6858 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 6859 scsipkt->pkt_comp != NULL) { 6860 /* scsi callback required */ 6861 if (servicing_interrupt()) { 6862 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6863 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 6864 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 6865 TASKQID_INVALID) { 6866 return (TRAN_BUSY); 6867 } 6868 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 6869 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 6870 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 6871 /* Scheduling the callback failed */ 6872 return (TRAN_BUSY); 6873 } 6874 } 6875 return (rval); 6876 } 6877 6878 /* 6879 * Re-identify device after doing a firmware download. 6880 */ 6881 static void 6882 sata_reidentify_device(sata_pkt_txlate_t *spx) 6883 { 6884 #define DOWNLOAD_WAIT_TIME_SECS 60 6885 #define DOWNLOAD_WAIT_INTERVAL_SECS 1 6886 int rval; 6887 int retry_cnt; 6888 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6889 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 6890 sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device; 6891 sata_drive_info_t *sdinfo; 6892 6893 /* 6894 * Before returning good status, probe device. 6895 * Device probing will get IDENTIFY DEVICE data, if possible. 6896 * The assumption is that the new microcode is applied by the 6897 * device. It is a caller responsibility to verify this. 6898 */ 6899 for (retry_cnt = 0; 6900 retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS; 6901 retry_cnt++) { 6902 rval = sata_probe_device(sata_hba_inst, &sata_device); 6903 6904 if (rval == SATA_SUCCESS) { /* Set default features */ 6905 sdinfo = sata_get_device_info(sata_hba_inst, 6906 &sata_device); 6907 if (sata_initialize_device(sata_hba_inst, sdinfo) != 6908 SATA_SUCCESS) { 6909 /* retry */ 6910 rval = sata_initialize_device(sata_hba_inst, 6911 sdinfo); 6912 if (rval == SATA_RETRY) 6913 sata_log(sata_hba_inst, CE_WARN, 6914 "SATA device at port %d pmport %d -" 6915 " default device features could not" 6916 " be set. Device may not operate " 6917 "as expected.", 6918 sata_device.satadev_addr.cport, 6919 sata_device.satadev_addr.pmport); 6920 } 6921 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 6922 scsi_hba_pkt_comp(scsipkt); 6923 return; 6924 } else if (rval == SATA_RETRY) { 6925 delay(drv_usectohz(1000000 * 6926 DOWNLOAD_WAIT_INTERVAL_SECS)); 6927 continue; 6928 } else /* failed - no reason to retry */ 6929 break; 6930 } 6931 6932 /* 6933 * Something went wrong, device probing failed. 6934 */ 6935 SATA_LOG_D((sata_hba_inst, CE_WARN, 6936 "Cannot probe device after downloading microcode\n")); 6937 6938 /* Reset device to force retrying the probe. */ 6939 (void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 6940 (SATA_DIP(sata_hba_inst), &sata_device); 6941 6942 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 6943 scsi_hba_pkt_comp(scsipkt); 6944 } 6945 6946 6947 /* 6948 * Translate command: Synchronize Cache. 6949 * Translates into Flush Cache command for SATA hard disks. 6950 * 6951 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 6952 * appropriate values in scsi_pkt fields. 6953 */ 6954 static int 6955 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx) 6956 { 6957 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6958 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 6959 int rval, reason; 6960 int synch; 6961 6962 mutex_enter(cport_mutex); 6963 6964 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) != 6965 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 6966 mutex_exit(cport_mutex); 6967 return (rval); 6968 } 6969 6970 scmd->satacmd_addr_type = 0; 6971 scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE; 6972 scmd->satacmd_device_reg = 0; 6973 scmd->satacmd_sec_count_lsb = 0; 6974 scmd->satacmd_lba_low_lsb = 0; 6975 scmd->satacmd_lba_mid_lsb = 0; 6976 scmd->satacmd_lba_high_lsb = 0; 6977 scmd->satacmd_features_reg = 0; 6978 scmd->satacmd_status_reg = 0; 6979 scmd->satacmd_error_reg = 0; 6980 6981 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 6982 "sata_txlt_synchronize_cache\n", NULL); 6983 6984 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 6985 /* Need to set-up a callback function */ 6986 spx->txlt_sata_pkt->satapkt_comp = 6987 sata_txlt_nodata_cmd_completion; 6988 synch = FALSE; 6989 } else 6990 synch = TRUE; 6991 6992 /* Transfer command to HBA */ 6993 if (sata_hba_start(spx, &rval) != 0) { 6994 /* Pkt not accepted for execution */ 6995 mutex_exit(cport_mutex); 6996 return (rval); 6997 } 6998 mutex_exit(cport_mutex); 6999 7000 /* 7001 * If execution non-synchronous, it had to be completed 7002 * a callback function will handle potential errors, translate 7003 * the response and will do a callback to a target driver. 7004 * If it was synchronous, check status, using the same 7005 * framework callback. 7006 */ 7007 if (synch) { 7008 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 7009 "synchronous execution status %x\n", 7010 spx->txlt_sata_pkt->satapkt_reason); 7011 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 7012 } 7013 return (TRAN_ACCEPT); 7014 } 7015 7016 7017 /* 7018 * Send pkt to SATA HBA driver 7019 * 7020 * This function may be called only if the operation is requested by scsi_pkt, 7021 * i.e. scsi_pkt is not NULL. 7022 * 7023 * This function has to be called with cport mutex held. It does release 7024 * the mutex when it calls HBA driver sata_tran_start function and 7025 * re-acquires it afterwards. 7026 * 7027 * If return value is 0, pkt was accepted, -1 otherwise 7028 * rval is set to appropriate sata_scsi_start return value. 7029 * 7030 * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not 7031 * have called the sata_pkt callback function for this packet. 7032 * 7033 * The scsi callback has to be performed by the caller of this routine. 7034 */ 7035 static int 7036 sata_hba_start(sata_pkt_txlate_t *spx, int *rval) 7037 { 7038 int stat; 7039 uint8_t cport = SATA_TXLT_CPORT(spx); 7040 uint8_t pmport = SATA_TXLT_PMPORT(spx); 7041 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 7042 sata_drive_info_t *sdinfo; 7043 sata_pmult_info_t *pminfo = NULL; 7044 sata_pmport_info_t *pmportinfo = NULL; 7045 sata_device_t *sata_device = NULL; 7046 uint8_t cmd; 7047 struct sata_cmd_flags cmd_flags; 7048 7049 ASSERT(spx->txlt_sata_pkt != NULL); 7050 7051 ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7052 7053 sdinfo = sata_get_device_info(sata_hba_inst, 7054 &spx->txlt_sata_pkt->satapkt_device); 7055 ASSERT(sdinfo != NULL); 7056 7057 /* Clear device reset state? */ 7058 /* qual should be XXX_DPMPORT, but add XXX_PMPORT in case */ 7059 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT || 7060 sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) { 7061 7062 /* 7063 * Get the pmult_info of the its parent port multiplier, all 7064 * sub-devices share a common device reset flags on in 7065 * pmult_info. 7066 */ 7067 pminfo = SATA_PMULT_INFO(sata_hba_inst, cport); 7068 pmportinfo = pminfo->pmult_dev_port[pmport]; 7069 ASSERT(pminfo != NULL); 7070 if (pminfo->pmult_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) { 7071 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 7072 sata_clear_dev_reset = B_TRUE; 7073 pminfo->pmult_event_flags &= 7074 ~SATA_EVNT_CLEAR_DEVICE_RESET; 7075 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 7076 "sata_hba_start: clearing device reset state" 7077 "on pmult.\n", NULL); 7078 } 7079 } else { 7080 if (sdinfo->satadrv_event_flags & 7081 SATA_EVNT_CLEAR_DEVICE_RESET) { 7082 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 7083 sata_clear_dev_reset = B_TRUE; 7084 sdinfo->satadrv_event_flags &= 7085 ~SATA_EVNT_CLEAR_DEVICE_RESET; 7086 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 7087 "sata_hba_start: clearing device reset state\n", 7088 NULL); 7089 } 7090 } 7091 7092 cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg; 7093 cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags; 7094 sata_device = &spx->txlt_sata_pkt->satapkt_device; 7095 7096 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7097 7098 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 7099 "Sata cmd 0x%2x\n", cmd); 7100 7101 stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 7102 spx->txlt_sata_pkt); 7103 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7104 /* 7105 * If sata pkt was accepted and executed in asynchronous mode, i.e. 7106 * with the sata callback, the sata_pkt could be already destroyed 7107 * by the time we check ther return status from the hba_start() 7108 * function, because sata_scsi_destroy_pkt() could have been already 7109 * called (perhaps in the interrupt context). So, in such case, there 7110 * should be no references to it. In other cases, sata_pkt still 7111 * exists. 7112 */ 7113 if (stat == SATA_TRAN_ACCEPTED) { 7114 /* 7115 * pkt accepted for execution. 7116 * If it was executed synchronously, it is already completed 7117 * and pkt completion_reason indicates completion status. 7118 */ 7119 *rval = TRAN_ACCEPT; 7120 return (0); 7121 } 7122 7123 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 7124 switch (stat) { 7125 case SATA_TRAN_QUEUE_FULL: 7126 /* 7127 * Controller detected queue full condition. 7128 */ 7129 SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst, 7130 "sata_hba_start: queue full\n", NULL); 7131 7132 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 7133 *spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL; 7134 7135 *rval = TRAN_BUSY; 7136 break; 7137 7138 case SATA_TRAN_PORT_ERROR: 7139 /* 7140 * Communication/link with device or general port error 7141 * detected before pkt execution begun. 7142 */ 7143 if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 7144 SATA_ADDR_CPORT || 7145 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 7146 SATA_ADDR_DCPORT) 7147 sata_log(sata_hba_inst, CE_CONT, 7148 "SATA port %d error", 7149 sata_device->satadev_addr.cport); 7150 else 7151 sata_log(sata_hba_inst, CE_CONT, 7152 "SATA port %d:%d error\n", 7153 sata_device->satadev_addr.cport, 7154 sata_device->satadev_addr.pmport); 7155 7156 /* 7157 * Update the port/device structure. 7158 * sata_pkt should be still valid. Since port error is 7159 * returned, sata_device content should reflect port 7160 * state - it means, that sata address have been changed, 7161 * because original packet's sata address refered to a device 7162 * attached to some port. 7163 */ 7164 if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT || 7165 sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) { 7166 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7167 mutex_enter(&pmportinfo->pmport_mutex); 7168 sata_update_pmport_info(sata_hba_inst, sata_device); 7169 mutex_exit(&pmportinfo->pmport_mutex); 7170 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7171 } else { 7172 sata_update_port_info(sata_hba_inst, sata_device); 7173 } 7174 7175 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 7176 *rval = TRAN_FATAL_ERROR; 7177 break; 7178 7179 case SATA_TRAN_CMD_UNSUPPORTED: 7180 /* 7181 * Command rejected by HBA as unsupported. It was HBA driver 7182 * that rejected the command, command was not sent to 7183 * an attached device. 7184 */ 7185 if ((sdinfo != NULL) && 7186 (sdinfo->satadrv_state & SATA_DSTATE_RESET)) 7187 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 7188 "sat_hba_start: cmd 0x%2x rejected " 7189 "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd); 7190 7191 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7192 (void) sata_txlt_invalid_command(spx); 7193 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 7194 7195 *rval = TRAN_ACCEPT; 7196 break; 7197 7198 case SATA_TRAN_BUSY: 7199 /* 7200 * Command rejected by HBA because other operation prevents 7201 * accepting the packet, or device is in RESET condition. 7202 */ 7203 if (sdinfo != NULL) { 7204 sdinfo->satadrv_state = 7205 spx->txlt_sata_pkt->satapkt_device.satadev_state; 7206 7207 if (sdinfo->satadrv_state & SATA_DSTATE_RESET) { 7208 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 7209 "sata_hba_start: cmd 0x%2x rejected " 7210 "because of device reset condition\n", 7211 cmd); 7212 } else { 7213 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 7214 "sata_hba_start: cmd 0x%2x rejected " 7215 "with SATA_TRAN_BUSY status\n", 7216 cmd); 7217 } 7218 } 7219 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 7220 *rval = TRAN_BUSY; 7221 break; 7222 7223 default: 7224 /* Unrecognized HBA response */ 7225 SATA_LOG_D((sata_hba_inst, CE_WARN, 7226 "sata_hba_start: unrecognized HBA response " 7227 "to cmd : 0x%2x resp 0x%x", cmd, rval)); 7228 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 7229 *rval = TRAN_FATAL_ERROR; 7230 break; 7231 } 7232 7233 /* 7234 * If we got here, the packet was rejected. 7235 * Check if we need to remember reset state clearing request 7236 */ 7237 if (cmd_flags.sata_clear_dev_reset) { 7238 /* 7239 * Check if device is still configured - it may have 7240 * disapeared from the configuration 7241 */ 7242 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 7243 if (sdinfo != NULL) { 7244 /* 7245 * Restore the flag that requests clearing of 7246 * the device reset state, 7247 * so the next sata packet may carry it to HBA. 7248 */ 7249 if (sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT || 7250 sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT) { 7251 pminfo->pmult_event_flags |= 7252 SATA_EVNT_CLEAR_DEVICE_RESET; 7253 } else { 7254 sdinfo->satadrv_event_flags |= 7255 SATA_EVNT_CLEAR_DEVICE_RESET; 7256 } 7257 } 7258 } 7259 return (-1); 7260 } 7261 7262 /* 7263 * Scsi response setup for invalid LBA 7264 * 7265 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 7266 */ 7267 static int 7268 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx) 7269 { 7270 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7271 struct scsi_extended_sense *sense; 7272 7273 scsipkt->pkt_reason = CMD_CMPLT; 7274 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7275 STATE_SENT_CMD | STATE_GOT_STATUS; 7276 *scsipkt->pkt_scbp = STATUS_CHECK; 7277 7278 *scsipkt->pkt_scbp = STATUS_CHECK; 7279 sense = sata_arq_sense(spx); 7280 sense->es_key = KEY_ILLEGAL_REQUEST; 7281 sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE; 7282 7283 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 7284 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 7285 7286 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 7287 scsipkt->pkt_comp != NULL) { 7288 /* scsi callback required */ 7289 if (servicing_interrupt()) { 7290 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7291 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7292 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 7293 TASKQID_INVALID) { 7294 return (TRAN_BUSY); 7295 } 7296 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7297 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7298 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 7299 /* Scheduling the callback failed */ 7300 return (TRAN_BUSY); 7301 } 7302 } 7303 return (TRAN_ACCEPT); 7304 } 7305 7306 7307 /* 7308 * Analyze device status and error registers and translate them into 7309 * appropriate scsi sense codes. 7310 * NOTE: non-packet commands only for now 7311 */ 7312 static void 7313 sata_decode_device_error(sata_pkt_txlate_t *spx, 7314 struct scsi_extended_sense *sense) 7315 { 7316 uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg; 7317 7318 ASSERT(sense != NULL); 7319 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 7320 SATA_STATUS_ERR); 7321 7322 7323 if (err_reg & SATA_ERROR_ICRC) { 7324 sense->es_key = KEY_ABORTED_COMMAND; 7325 sense->es_add_code = 0x08; /* Communication failure */ 7326 return; 7327 } 7328 7329 if (err_reg & SATA_ERROR_UNC) { 7330 sense->es_key = KEY_MEDIUM_ERROR; 7331 /* Information bytes (LBA) need to be set by a caller */ 7332 return; 7333 } 7334 7335 /* ADD HERE: MC error bit handling for ATAPI CD/DVD */ 7336 if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) { 7337 sense->es_key = KEY_UNIT_ATTENTION; 7338 sense->es_add_code = 0x3a; /* No media present */ 7339 return; 7340 } 7341 7342 if (err_reg & SATA_ERROR_IDNF) { 7343 if (err_reg & SATA_ERROR_ABORT) { 7344 sense->es_key = KEY_ABORTED_COMMAND; 7345 } else { 7346 sense->es_key = KEY_ILLEGAL_REQUEST; 7347 sense->es_add_code = 0x21; /* LBA out of range */ 7348 } 7349 return; 7350 } 7351 7352 if (err_reg & SATA_ERROR_ABORT) { 7353 ASSERT(spx->txlt_sata_pkt != NULL); 7354 sense->es_key = KEY_ABORTED_COMMAND; 7355 return; 7356 } 7357 } 7358 7359 /* 7360 * Extract error LBA from sata_pkt.satapkt_cmd register fields 7361 */ 7362 static void 7363 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba) 7364 { 7365 sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd; 7366 7367 *lba = 0; 7368 if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) { 7369 *lba = sata_cmd->satacmd_lba_high_msb; 7370 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb; 7371 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb; 7372 } else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) { 7373 *lba = sata_cmd->satacmd_device_reg & 0xf; 7374 } 7375 *lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb; 7376 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb; 7377 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb; 7378 } 7379 7380 /* 7381 * This is fixed sense format - if LBA exceeds the info field size, 7382 * no valid info will be returned (valid bit in extended sense will 7383 * be set to 0). 7384 */ 7385 static struct scsi_extended_sense * 7386 sata_arq_sense(sata_pkt_txlate_t *spx) 7387 { 7388 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7389 struct scsi_arq_status *arqs; 7390 struct scsi_extended_sense *sense; 7391 7392 /* Fill ARQ sense data */ 7393 scsipkt->pkt_state |= STATE_ARQ_DONE; 7394 arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp; 7395 *(uchar_t *)&arqs->sts_status = STATUS_CHECK; 7396 *(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD; 7397 arqs->sts_rqpkt_reason = CMD_CMPLT; 7398 arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7399 STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS; 7400 arqs->sts_rqpkt_resid = 0; 7401 sense = &arqs->sts_sensedata; 7402 bzero(sense, sizeof (struct scsi_extended_sense)); 7403 sata_fixed_sense_data_preset(sense); 7404 return (sense); 7405 } 7406 7407 /* 7408 * ATA Pass Through support 7409 * Sets flags indicating that an invalid value was found in some 7410 * field in the command. It could be something illegal according to 7411 * the SAT-2 spec or it could be a feature that is not (yet?) 7412 * supported. 7413 */ 7414 static int 7415 sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *spx) 7416 { 7417 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7418 struct scsi_extended_sense *sense = sata_arq_sense(spx); 7419 7420 scsipkt->pkt_reason = CMD_CMPLT; 7421 *scsipkt->pkt_scbp = STATUS_CHECK; 7422 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7423 STATE_SENT_CMD | STATE_GOT_STATUS; 7424 7425 sense = sata_arq_sense(spx); 7426 sense->es_key = KEY_ILLEGAL_REQUEST; 7427 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 7428 7429 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 7430 scsipkt->pkt_comp != NULL) { 7431 /* scsi callback required */ 7432 if (servicing_interrupt()) { 7433 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7434 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7435 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 7436 TASKQID_INVALID) { 7437 return (TRAN_BUSY); 7438 } 7439 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7440 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7441 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 7442 /* Scheduling the callback failed */ 7443 return (TRAN_BUSY); 7444 } 7445 } 7446 7447 return (TRAN_ACCEPT); 7448 } 7449 7450 /* 7451 * The UNMAP command considers it not to be an error if the parameter length 7452 * or block descriptor length is 0. For this case, there is nothing for TRIM 7453 * to do so just complete the command. 7454 */ 7455 static int 7456 sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *spx) 7457 { 7458 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7459 7460 scsipkt->pkt_reason = CMD_CMPLT; 7461 *scsipkt->pkt_scbp = STATUS_GOOD; 7462 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7463 STATE_SENT_CMD | STATE_GOT_STATUS; 7464 7465 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 7466 scsipkt->pkt_comp != NULL) { 7467 /* scsi callback required */ 7468 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7469 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7470 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 7471 /* Scheduling the callback failed */ 7472 return (TRAN_BUSY); 7473 } 7474 } 7475 7476 return (TRAN_ACCEPT); 7477 } 7478 7479 /* 7480 * Emulated SATA Read/Write command completion for zero-length requests. 7481 * This request always succedes, so in synchronous mode it always returns 7482 * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the 7483 * callback cannot be scheduled. 7484 */ 7485 static int 7486 sata_emul_rw_completion(sata_pkt_txlate_t *spx) 7487 { 7488 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7489 7490 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7491 STATE_SENT_CMD | STATE_GOT_STATUS; 7492 scsipkt->pkt_reason = CMD_CMPLT; 7493 *scsipkt->pkt_scbp = STATUS_GOOD; 7494 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 7495 /* scsi callback required - have to schedule it */ 7496 if (servicing_interrupt()) { 7497 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7498 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7499 (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == 7500 TASKQID_INVALID) { 7501 return (TRAN_BUSY); 7502 } 7503 } else if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 7504 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 7505 (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) { 7506 /* Scheduling the callback failed */ 7507 return (TRAN_BUSY); 7508 } 7509 } 7510 return (TRAN_ACCEPT); 7511 } 7512 7513 7514 /* 7515 * Translate completion status of SATA read/write commands into scsi response. 7516 * pkt completion_reason is checked to determine the completion status. 7517 * Do scsi callback if necessary. 7518 * 7519 * Note: this function may be called also for synchronously executed 7520 * commands. 7521 * This function may be used only if scsi_pkt is non-NULL. 7522 */ 7523 static void 7524 sata_txlt_rw_completion(sata_pkt_t *sata_pkt) 7525 { 7526 sata_pkt_txlate_t *spx = 7527 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7528 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 7529 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7530 struct scsi_extended_sense *sense; 7531 uint64_t lba; 7532 struct buf *bp; 7533 int rval; 7534 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 7535 /* Normal completion */ 7536 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7537 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 7538 scsipkt->pkt_reason = CMD_CMPLT; 7539 *scsipkt->pkt_scbp = STATUS_GOOD; 7540 if (spx->txlt_tmp_buf != NULL) { 7541 /* Temporary buffer was used */ 7542 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 7543 if (bp->b_flags & B_READ) { 7544 rval = ddi_dma_sync( 7545 spx->txlt_buf_dma_handle, 0, 0, 7546 DDI_DMA_SYNC_FORCPU); 7547 ASSERT(rval == DDI_SUCCESS); 7548 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 7549 bp->b_bcount); 7550 } 7551 } 7552 } else { 7553 /* 7554 * Something went wrong - analyze return 7555 */ 7556 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7557 STATE_SENT_CMD | STATE_GOT_STATUS; 7558 scsipkt->pkt_reason = CMD_INCOMPLETE; 7559 *scsipkt->pkt_scbp = STATUS_CHECK; 7560 sense = sata_arq_sense(spx); 7561 ASSERT(sense != NULL); 7562 7563 /* 7564 * SATA_PKT_DEV_ERROR is the only case where we may be able to 7565 * extract from device registers the failing LBA. 7566 */ 7567 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 7568 if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) && 7569 (scmd->satacmd_lba_mid_msb != 0 || 7570 scmd->satacmd_lba_high_msb != 0)) { 7571 /* 7572 * We have problem reporting this cmd LBA 7573 * in fixed sense data format, because of 7574 * the size of the scsi LBA fields. 7575 */ 7576 sense->es_valid = 0; 7577 } else { 7578 sata_extract_error_lba(spx, &lba); 7579 sense->es_info_1 = (lba & 0xFF000000) >> 24; 7580 sense->es_info_2 = (lba & 0xFF0000) >> 16; 7581 sense->es_info_3 = (lba & 0xFF00) >> 8; 7582 sense->es_info_4 = lba & 0xFF; 7583 } 7584 } else { 7585 /* Invalid extended sense info */ 7586 sense->es_valid = 0; 7587 } 7588 7589 switch (sata_pkt->satapkt_reason) { 7590 case SATA_PKT_PORT_ERROR: 7591 /* We may want to handle DEV GONE state as well */ 7592 /* 7593 * We have no device data. Assume no data transfered. 7594 */ 7595 sense->es_key = KEY_HARDWARE_ERROR; 7596 break; 7597 7598 case SATA_PKT_DEV_ERROR: 7599 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 7600 SATA_STATUS_ERR) { 7601 /* 7602 * determine dev error reason from error 7603 * reg content 7604 */ 7605 sata_decode_device_error(spx, sense); 7606 if (sense->es_key == KEY_MEDIUM_ERROR) { 7607 switch (scmd->satacmd_cmd_reg) { 7608 case SATAC_READ_DMA: 7609 case SATAC_READ_DMA_EXT: 7610 case SATAC_READ_DMA_QUEUED: 7611 case SATAC_READ_DMA_QUEUED_EXT: 7612 case SATAC_READ_FPDMA_QUEUED: 7613 /* Unrecovered read error */ 7614 sense->es_add_code = 7615 SD_SCSI_ASC_UNREC_READ_ERR; 7616 break; 7617 case SATAC_WRITE_DMA: 7618 case SATAC_WRITE_DMA_EXT: 7619 case SATAC_WRITE_DMA_QUEUED: 7620 case SATAC_WRITE_DMA_QUEUED_EXT: 7621 case SATAC_WRITE_FPDMA_QUEUED: 7622 /* Write error */ 7623 sense->es_add_code = 7624 SD_SCSI_ASC_WRITE_ERR; 7625 break; 7626 default: 7627 /* Internal error */ 7628 SATA_LOG_D(( 7629 spx->txlt_sata_hba_inst, 7630 CE_WARN, 7631 "sata_txlt_rw_completion :" 7632 "internal error - invalid " 7633 "command 0x%2x", 7634 scmd->satacmd_cmd_reg)); 7635 break; 7636 } 7637 } 7638 break; 7639 } 7640 /* No extended sense key - no info available */ 7641 scsipkt->pkt_reason = CMD_INCOMPLETE; 7642 break; 7643 7644 case SATA_PKT_TIMEOUT: 7645 scsipkt->pkt_reason = CMD_TIMEOUT; 7646 scsipkt->pkt_statistics |= 7647 STAT_TIMEOUT | STAT_DEV_RESET; 7648 sense->es_key = KEY_ABORTED_COMMAND; 7649 break; 7650 7651 case SATA_PKT_ABORTED: 7652 scsipkt->pkt_reason = CMD_ABORTED; 7653 scsipkt->pkt_statistics |= STAT_ABORTED; 7654 sense->es_key = KEY_ABORTED_COMMAND; 7655 break; 7656 7657 case SATA_PKT_RESET: 7658 scsipkt->pkt_reason = CMD_RESET; 7659 scsipkt->pkt_statistics |= STAT_DEV_RESET; 7660 sense->es_key = KEY_ABORTED_COMMAND; 7661 break; 7662 7663 default: 7664 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 7665 "sata_txlt_rw_completion: " 7666 "invalid packet completion reason")); 7667 scsipkt->pkt_reason = CMD_TRAN_ERR; 7668 break; 7669 } 7670 } 7671 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 7672 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 7673 7674 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 7675 /* scsi callback required */ 7676 scsi_hba_pkt_comp(scsipkt); 7677 } 7678 7679 7680 /* 7681 * Translate completion status of non-data commands (i.e. commands returning 7682 * no data). 7683 * pkt completion_reason is checked to determine the completion status. 7684 * Do scsi callback if necessary (FLAG_NOINTR == 0) 7685 * 7686 * Note: this function may be called also for synchronously executed 7687 * commands. 7688 * This function may be used only if scsi_pkt is non-NULL. 7689 */ 7690 7691 static void 7692 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt) 7693 { 7694 sata_pkt_txlate_t *spx = 7695 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7696 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7697 7698 sata_set_arq_data(sata_pkt); 7699 7700 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 7701 /* scsi callback required */ 7702 scsi_hba_pkt_comp(scsipkt); 7703 } 7704 7705 /* 7706 * Completion handler for ATA Pass Through command 7707 */ 7708 static void 7709 sata_txlt_apt_completion(sata_pkt_t *sata_pkt) 7710 { 7711 sata_pkt_txlate_t *spx = 7712 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7713 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 7714 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7715 struct buf *bp; 7716 uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0; 7717 7718 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 7719 /* Normal completion */ 7720 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7721 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 7722 scsipkt->pkt_reason = CMD_CMPLT; 7723 *scsipkt->pkt_scbp = STATUS_GOOD; 7724 7725 /* 7726 * If the command has CK_COND set 7727 */ 7728 if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) { 7729 *scsipkt->pkt_scbp = STATUS_CHECK; 7730 sata_fill_ata_return_desc(sata_pkt, 7731 KEY_RECOVERABLE_ERROR, 7732 SD_SCSI_ASC_APT_INFO_AVAIL, 0x1d); 7733 } 7734 7735 if (spx->txlt_tmp_buf != NULL) { 7736 /* Temporary buffer was used */ 7737 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 7738 if (bp->b_flags & B_READ) { 7739 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 7740 bp->b_bcount); 7741 } 7742 } 7743 } else { 7744 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7745 STATE_SENT_CMD | STATE_GOT_STATUS; 7746 scsipkt->pkt_reason = CMD_INCOMPLETE; 7747 *scsipkt->pkt_scbp = STATUS_CHECK; 7748 7749 /* 7750 * If DF or ERR was set, the HBA should have copied out the 7751 * status and error registers to the satacmd structure. 7752 */ 7753 if (scmd->satacmd_status_reg & SATA_STATUS_DF) { 7754 sense_key = KEY_HARDWARE_ERROR; 7755 addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE; 7756 addl_sense_qual = 0; 7757 } else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) { 7758 if (scmd->satacmd_error_reg & SATA_ERROR_NM) { 7759 sense_key = KEY_NOT_READY; 7760 addl_sense_code = 7761 SD_SCSI_ASC_MEDIUM_NOT_PRESENT; 7762 addl_sense_qual = 0; 7763 } else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) { 7764 sense_key = KEY_MEDIUM_ERROR; 7765 addl_sense_code = SD_SCSI_ASC_UNREC_READ_ERR; 7766 addl_sense_qual = 0; 7767 } else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) { 7768 sense_key = KEY_DATA_PROTECT; 7769 addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED; 7770 addl_sense_qual = 0; 7771 } else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) { 7772 sense_key = KEY_ILLEGAL_REQUEST; 7773 addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE; 7774 addl_sense_qual = 0; 7775 } else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) { 7776 sense_key = KEY_ABORTED_COMMAND; 7777 addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE; 7778 addl_sense_qual = 0; 7779 } else if (scmd->satacmd_error_reg & SATA_ERROR_MC) { 7780 sense_key = KEY_UNIT_ATTENTION; 7781 addl_sense_code = 7782 SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED; 7783 addl_sense_qual = 0; 7784 } else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) { 7785 sense_key = KEY_UNIT_ATTENTION; 7786 addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ; 7787 addl_sense_qual = 0; 7788 } else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) { 7789 sense_key = KEY_ABORTED_COMMAND; 7790 addl_sense_code = 7791 SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR; 7792 addl_sense_qual = 0; 7793 } 7794 } 7795 7796 sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code, 7797 addl_sense_qual); 7798 } 7799 7800 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 7801 /* scsi callback required */ 7802 scsi_hba_pkt_comp(scsipkt); 7803 } 7804 7805 /* 7806 * Completion handler for unmap translation command 7807 */ 7808 static void 7809 sata_txlt_unmap_completion(sata_pkt_t *sata_pkt) 7810 { 7811 sata_pkt_txlate_t *spx = 7812 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7813 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 7814 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7815 struct buf *bp; 7816 uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0; 7817 7818 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 7819 /* Normal completion */ 7820 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7821 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 7822 scsipkt->pkt_reason = CMD_CMPLT; 7823 *scsipkt->pkt_scbp = STATUS_GOOD; 7824 7825 if (spx->txlt_tmp_buf != NULL) { 7826 /* Temporary buffer was used */ 7827 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 7828 if (bp->b_flags & B_READ) { 7829 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 7830 bp->b_bcount); 7831 } 7832 } 7833 } else { 7834 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7835 STATE_SENT_CMD | STATE_GOT_STATUS; 7836 scsipkt->pkt_reason = CMD_INCOMPLETE; 7837 *scsipkt->pkt_scbp = STATUS_CHECK; 7838 7839 /* 7840 * If DF or ERR was set, the HBA should have copied out the 7841 * status and error registers to the satacmd structure. 7842 */ 7843 if (scmd->satacmd_status_reg & SATA_STATUS_DF) { 7844 sense_key = KEY_HARDWARE_ERROR; 7845 addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE; 7846 addl_sense_qual = 0; 7847 } else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) { 7848 if (scmd->satacmd_error_reg & SATA_ERROR_NM) { 7849 sense_key = KEY_NOT_READY; 7850 addl_sense_code = 7851 SD_SCSI_ASC_MEDIUM_NOT_PRESENT; 7852 addl_sense_qual = 0; 7853 } else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) { 7854 sense_key = KEY_MEDIUM_ERROR; 7855 addl_sense_code = SD_SCSI_ASC_WRITE_ERR; 7856 addl_sense_qual = 0; 7857 } else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) { 7858 sense_key = KEY_DATA_PROTECT; 7859 addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED; 7860 addl_sense_qual = 0; 7861 } else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) { 7862 sense_key = KEY_ILLEGAL_REQUEST; 7863 addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE; 7864 addl_sense_qual = 0; 7865 } else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) { 7866 sense_key = KEY_ABORTED_COMMAND; 7867 addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE; 7868 addl_sense_qual = 0; 7869 } else if (scmd->satacmd_error_reg & SATA_ERROR_MC) { 7870 sense_key = KEY_UNIT_ATTENTION; 7871 addl_sense_code = 7872 SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED; 7873 addl_sense_qual = 0; 7874 } else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) { 7875 sense_key = KEY_UNIT_ATTENTION; 7876 addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ; 7877 addl_sense_qual = 0; 7878 } else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) { 7879 sense_key = KEY_ABORTED_COMMAND; 7880 addl_sense_code = 7881 SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR; 7882 addl_sense_qual = 0; 7883 } 7884 } 7885 7886 sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code, 7887 addl_sense_qual); 7888 } 7889 7890 sata_free_local_buffer(spx); 7891 7892 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) 7893 /* scsi callback required */ 7894 scsi_hba_pkt_comp(scsipkt); 7895 } 7896 7897 /* 7898 * 7899 */ 7900 static void 7901 sata_fill_ata_return_desc(sata_pkt_t *sata_pkt, uint8_t sense_key, 7902 uint8_t addl_sense_code, uint8_t addl_sense_qual) 7903 { 7904 sata_pkt_txlate_t *spx = 7905 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7906 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 7907 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7908 struct sata_apt_sense_data *apt_sd = 7909 (struct sata_apt_sense_data *)scsipkt->pkt_scbp; 7910 struct scsi_descr_sense_hdr *sds = &(apt_sd->apt_sd_hdr); 7911 struct scsi_ata_status_ret_sense_descr *ata_ret_desc = 7912 &(apt_sd->apt_sd_sense); 7913 int extend = 0; 7914 7915 if ((scsipkt->pkt_cdbp[0] == SPC3_CMD_ATA_COMMAND_PASS_THROUGH16) && 7916 (scsipkt->pkt_cdbp[2] & SATL_APT_BM_EXTEND)) 7917 extend = 1; 7918 7919 scsipkt->pkt_state |= STATE_ARQ_DONE; 7920 7921 /* update the residual count */ 7922 *(uchar_t *)&apt_sd->apt_status = STATUS_CHECK; 7923 *(uchar_t *)&apt_sd->apt_rqpkt_status = STATUS_GOOD; 7924 apt_sd->apt_rqpkt_reason = CMD_CMPLT; 7925 apt_sd->apt_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7926 STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS; 7927 apt_sd->apt_rqpkt_resid = scsipkt->pkt_scblen - 7928 sizeof (struct sata_apt_sense_data); 7929 7930 /* 7931 * Fill in the Descriptor sense header 7932 */ 7933 bzero(sds, sizeof (struct scsi_descr_sense_hdr)); 7934 sds->ds_code = CODE_FMT_DESCR_CURRENT; 7935 sds->ds_class = CLASS_EXTENDED_SENSE; 7936 sds->ds_key = sense_key & 0xf; 7937 sds->ds_add_code = addl_sense_code; 7938 sds->ds_qual_code = addl_sense_qual; 7939 sds->ds_addl_sense_length = 7940 sizeof (struct scsi_ata_status_ret_sense_descr); 7941 7942 /* 7943 * Fill in the ATA Return descriptor sense data 7944 */ 7945 bzero(ata_ret_desc, sizeof (struct scsi_ata_status_ret_sense_descr)); 7946 ata_ret_desc->ars_descr_type = DESCR_ATA_STATUS_RETURN; 7947 ata_ret_desc->ars_addl_length = 0xc; 7948 ata_ret_desc->ars_error = scmd->satacmd_error_reg; 7949 ata_ret_desc->ars_sec_count_lsb = scmd->satacmd_sec_count_lsb; 7950 ata_ret_desc->ars_lba_low_lsb = scmd->satacmd_lba_low_lsb; 7951 ata_ret_desc->ars_lba_mid_lsb = scmd->satacmd_lba_mid_lsb; 7952 ata_ret_desc->ars_lba_high_lsb = scmd->satacmd_lba_high_lsb; 7953 ata_ret_desc->ars_device = scmd->satacmd_device_reg; 7954 ata_ret_desc->ars_status = scmd->satacmd_status_reg; 7955 7956 if (extend == 1) { 7957 ata_ret_desc->ars_extend = 1; 7958 ata_ret_desc->ars_sec_count_msb = scmd->satacmd_sec_count_msb; 7959 ata_ret_desc->ars_lba_low_msb = scmd->satacmd_lba_low_msb; 7960 ata_ret_desc->ars_lba_mid_msb = scmd->satacmd_lba_mid_msb; 7961 ata_ret_desc->ars_lba_high_msb = scmd->satacmd_lba_high_msb; 7962 } else { 7963 ata_ret_desc->ars_extend = 0; 7964 ata_ret_desc->ars_sec_count_msb = 0; 7965 ata_ret_desc->ars_lba_low_msb = 0; 7966 ata_ret_desc->ars_lba_mid_msb = 0; 7967 ata_ret_desc->ars_lba_high_msb = 0; 7968 } 7969 } 7970 7971 static void 7972 sata_set_arq_data(sata_pkt_t *sata_pkt) 7973 { 7974 sata_pkt_txlate_t *spx = 7975 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7976 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7977 struct scsi_extended_sense *sense; 7978 7979 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7980 STATE_SENT_CMD | STATE_GOT_STATUS; 7981 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 7982 /* Normal completion */ 7983 scsipkt->pkt_reason = CMD_CMPLT; 7984 *scsipkt->pkt_scbp = STATUS_GOOD; 7985 } else { 7986 /* Something went wrong */ 7987 scsipkt->pkt_reason = CMD_INCOMPLETE; 7988 *scsipkt->pkt_scbp = STATUS_CHECK; 7989 sense = sata_arq_sense(spx); 7990 switch (sata_pkt->satapkt_reason) { 7991 case SATA_PKT_PORT_ERROR: 7992 /* 7993 * We have no device data. Assume no data transfered. 7994 */ 7995 sense->es_key = KEY_HARDWARE_ERROR; 7996 break; 7997 7998 case SATA_PKT_DEV_ERROR: 7999 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 8000 SATA_STATUS_ERR) { 8001 /* 8002 * determine dev error reason from error 8003 * reg content 8004 */ 8005 sata_decode_device_error(spx, sense); 8006 break; 8007 } 8008 /* No extended sense key - no info available */ 8009 break; 8010 8011 case SATA_PKT_TIMEOUT: 8012 scsipkt->pkt_reason = CMD_TIMEOUT; 8013 scsipkt->pkt_statistics |= 8014 STAT_TIMEOUT | STAT_DEV_RESET; 8015 /* No extended sense key ? */ 8016 break; 8017 8018 case SATA_PKT_ABORTED: 8019 scsipkt->pkt_reason = CMD_ABORTED; 8020 scsipkt->pkt_statistics |= STAT_ABORTED; 8021 /* No extended sense key ? */ 8022 break; 8023 8024 case SATA_PKT_RESET: 8025 /* pkt aborted by an explicit reset from a host */ 8026 scsipkt->pkt_reason = CMD_RESET; 8027 scsipkt->pkt_statistics |= STAT_DEV_RESET; 8028 break; 8029 8030 default: 8031 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 8032 "sata_txlt_nodata_cmd_completion: " 8033 "invalid packet completion reason %d", 8034 sata_pkt->satapkt_reason)); 8035 scsipkt->pkt_reason = CMD_TRAN_ERR; 8036 break; 8037 } 8038 8039 } 8040 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 8041 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 8042 } 8043 8044 8045 /* 8046 * Build Mode sense R/W recovery page 8047 * NOT IMPLEMENTED 8048 */ 8049 8050 static int 8051 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 8052 { 8053 #ifndef __lock_lint 8054 _NOTE(ARGUNUSED(sdinfo)) 8055 _NOTE(ARGUNUSED(pcntrl)) 8056 _NOTE(ARGUNUSED(buf)) 8057 #endif 8058 return (0); 8059 } 8060 8061 /* 8062 * Build Mode sense caching page - scsi-3 implementation. 8063 * Page length distinguishes previous format from scsi-3 format. 8064 * buf must have space for 0x12 bytes. 8065 * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable. 8066 * 8067 */ 8068 static int 8069 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 8070 { 8071 struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf; 8072 sata_id_t *sata_id = &sdinfo->satadrv_id; 8073 8074 /* 8075 * Most of the fields are set to 0, being not supported and/or disabled 8076 */ 8077 bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3); 8078 8079 /* Saved paramters not supported */ 8080 if (pcntrl == 3) 8081 return (0); 8082 if (pcntrl == 0 || pcntrl == 2) { 8083 /* 8084 * For now treat current and default parameters as same 8085 * That may have to change, if target driver will complain 8086 */ 8087 page->mode_page.code = MODEPAGE_CACHING; /* PS = 0 */ 8088 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 8089 8090 if (SATA_READ_AHEAD_SUPPORTED(*sata_id) && 8091 !SATA_READ_AHEAD_ENABLED(*sata_id)) { 8092 page->dra = 1; /* Read Ahead disabled */ 8093 page->rcd = 1; /* Read Cache disabled */ 8094 } 8095 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) && 8096 SATA_WRITE_CACHE_ENABLED(*sata_id)) 8097 page->wce = 1; /* Write Cache enabled */ 8098 } else { 8099 /* Changeable parameters */ 8100 page->mode_page.code = MODEPAGE_CACHING; 8101 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 8102 if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) { 8103 page->dra = 1; 8104 page->rcd = 1; 8105 } 8106 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) 8107 page->wce = 1; 8108 } 8109 return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 8110 sizeof (struct mode_page)); 8111 } 8112 8113 /* 8114 * Build Mode sense exception cntrl page 8115 */ 8116 static int 8117 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 8118 { 8119 struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf; 8120 sata_id_t *sata_id = &sdinfo->satadrv_id; 8121 8122 /* 8123 * Most of the fields are set to 0, being not supported and/or disabled 8124 */ 8125 bzero(buf, PAGELENGTH_INFO_EXCPT); 8126 8127 page->mode_page.code = MODEPAGE_INFO_EXCPT; 8128 page->mode_page.length = PAGELENGTH_INFO_EXCPT; 8129 8130 /* Indicate that this is page is saveable */ 8131 page->mode_page.ps = 1; 8132 8133 /* 8134 * We will return the same data for default, current and saved page. 8135 * The only changeable bit is dexcpt and that bit is required 8136 * by the ATA specification to be preserved across power cycles. 8137 */ 8138 if (pcntrl != 1) { 8139 page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED); 8140 page->mrie = MRIE_ONLY_ON_REQUEST; 8141 } 8142 else 8143 page->dexcpt = 1; /* Only changeable parameter */ 8144 8145 return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)); 8146 } 8147 8148 8149 static int 8150 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 8151 { 8152 struct mode_acoustic_management *page = 8153 (struct mode_acoustic_management *)buf; 8154 sata_id_t *sata_id = &sdinfo->satadrv_id; 8155 8156 /* 8157 * Most of the fields are set to 0, being not supported and/or disabled 8158 */ 8159 bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT); 8160 8161 switch (pcntrl) { 8162 case P_CNTRL_DEFAULT: 8163 /* default paramters not supported */ 8164 return (0); 8165 8166 case P_CNTRL_CURRENT: 8167 case P_CNTRL_SAVED: 8168 /* Saved and current are supported and are identical */ 8169 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG; 8170 page->mode_page.length = 8171 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT; 8172 page->mode_page.ps = 1; 8173 8174 /* Word 83 indicates if feature is supported */ 8175 /* If feature is not supported */ 8176 if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) { 8177 page->acoustic_manag_enable = 8178 ACOUSTIC_DISABLED; 8179 } else { 8180 page->acoustic_manag_enable = 8181 ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT) 8182 != 0); 8183 /* Word 94 inidicates the value */ 8184 #ifdef _LITTLE_ENDIAN 8185 page->acoustic_manag_level = 8186 (uchar_t)sata_id->ai_acoustic; 8187 page->vendor_recommended_value = 8188 sata_id->ai_acoustic >> 8; 8189 #else 8190 page->acoustic_manag_level = 8191 sata_id->ai_acoustic >> 8; 8192 page->vendor_recommended_value = 8193 (uchar_t)sata_id->ai_acoustic; 8194 #endif 8195 } 8196 break; 8197 8198 case P_CNTRL_CHANGEABLE: 8199 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG; 8200 page->mode_page.length = 8201 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT; 8202 page->mode_page.ps = 1; 8203 8204 /* Word 83 indicates if the feature is supported */ 8205 if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) { 8206 page->acoustic_manag_enable = 8207 ACOUSTIC_ENABLED; 8208 page->acoustic_manag_level = 0xff; 8209 } 8210 break; 8211 } 8212 return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 8213 sizeof (struct mode_page)); 8214 } 8215 8216 8217 /* 8218 * Build Mode sense power condition page. 8219 */ 8220 static int 8221 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 8222 { 8223 struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf; 8224 sata_id_t *sata_id = &sdinfo->satadrv_id; 8225 8226 /* 8227 * Most of the fields are set to 0, being not supported and/or disabled 8228 * power condition page length was 0x0a 8229 */ 8230 bzero(buf, sizeof (struct mode_info_power_cond)); 8231 8232 if (pcntrl == P_CNTRL_DEFAULT) { 8233 /* default paramters not supported */ 8234 return (0); 8235 } 8236 8237 page->mode_page.code = MODEPAGE_POWER_COND; 8238 page->mode_page.length = sizeof (struct mode_info_power_cond); 8239 8240 if (sata_id->ai_cap & SATA_STANDBYTIMER) { 8241 page->standby = 1; 8242 bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer, 8243 sizeof (uchar_t) * 4); 8244 } 8245 8246 return (sizeof (struct mode_info_power_cond)); 8247 } 8248 8249 /* 8250 * Process mode select caching page 8 (scsi3 format only). 8251 * Read Ahead (same as read cache) and Write Cache may be turned on and off 8252 * if these features are supported by the device. If these features are not 8253 * supported, the command will be terminated with STATUS_CHECK. 8254 * This function fails only if the SET FEATURE command sent to 8255 * the device fails. The page format is not verified, assuming that the 8256 * target driver operates correctly - if parameters length is too short, 8257 * we just drop the page. 8258 * Two command may be sent if both Read Cache/Read Ahead and Write Cache 8259 * setting have to be changed. 8260 * SET FEATURE command is executed synchronously, i.e. we wait here until 8261 * it is completed, regardless of the scsi pkt directives. 8262 * 8263 * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e. 8264 * changing DRA will change RCD. 8265 * 8266 * More than one SATA command may be executed to perform operations specified 8267 * by mode select pages. The first error terminates further execution. 8268 * Operations performed successully are not backed-up in such case. 8269 * 8270 * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 8271 * If operation resulted in changing device setup, dmod flag should be set to 8272 * one (1). If parameters were not changed, dmod flag should be set to 0. 8273 * Upon return, if operation required sending command to the device, the rval 8274 * should be set to the value returned by sata_hba_start. If operation 8275 * did not require device access, rval should be set to TRAN_ACCEPT. 8276 * The pagelen should be set to the length of the page. 8277 * 8278 * This function has to be called with a port mutex held. 8279 * 8280 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 8281 */ 8282 int 8283 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page, 8284 int parmlen, int *pagelen, int *rval, int *dmod) 8285 { 8286 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 8287 sata_drive_info_t *sdinfo; 8288 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 8289 sata_id_t *sata_id; 8290 struct scsi_extended_sense *sense; 8291 int wce, dra; /* Current settings */ 8292 8293 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 8294 &spx->txlt_sata_pkt->satapkt_device); 8295 sata_id = &sdinfo->satadrv_id; 8296 *dmod = 0; 8297 8298 /* Verify parameters length. If too short, drop it */ 8299 if ((PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 8300 sizeof (struct mode_page)) > parmlen) { 8301 *scsipkt->pkt_scbp = STATUS_CHECK; 8302 sense = sata_arq_sense(spx); 8303 sense->es_key = KEY_ILLEGAL_REQUEST; 8304 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 8305 *pagelen = parmlen; 8306 *rval = TRAN_ACCEPT; 8307 return (SATA_FAILURE); 8308 } 8309 8310 *pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page); 8311 8312 /* Current setting of Read Ahead (and Read Cache) */ 8313 if (SATA_READ_AHEAD_ENABLED(*sata_id)) 8314 dra = 0; /* 0 == not disabled */ 8315 else 8316 dra = 1; 8317 /* Current setting of Write Cache */ 8318 if (SATA_WRITE_CACHE_ENABLED(*sata_id)) 8319 wce = 1; 8320 else 8321 wce = 0; 8322 8323 if (page->dra == dra && page->wce == wce && page->rcd == dra) { 8324 /* nothing to do */ 8325 *rval = TRAN_ACCEPT; 8326 return (SATA_SUCCESS); 8327 } 8328 8329 /* 8330 * Need to flip some setting 8331 * Set-up Internal SET FEATURES command(s) 8332 */ 8333 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 8334 scmd->satacmd_addr_type = 0; 8335 scmd->satacmd_device_reg = 0; 8336 scmd->satacmd_status_reg = 0; 8337 scmd->satacmd_error_reg = 0; 8338 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 8339 if (page->dra != dra || page->rcd != dra) { 8340 if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) { 8341 /* Need to flip read ahead setting */ 8342 if (dra == 0) 8343 /* Disable read ahead / read cache */ 8344 scmd->satacmd_features_reg = 8345 SATAC_SF_DISABLE_READ_AHEAD; 8346 else 8347 /* Enable read ahead / read cache */ 8348 scmd->satacmd_features_reg = 8349 SATAC_SF_ENABLE_READ_AHEAD; 8350 8351 /* Transfer command to HBA */ 8352 if (sata_hba_start(spx, rval) != 0) 8353 /* 8354 * Pkt not accepted for execution. 8355 */ 8356 return (SATA_FAILURE); 8357 8358 *dmod = 1; 8359 8360 /* Now process return */ 8361 if (spx->txlt_sata_pkt->satapkt_reason != 8362 SATA_PKT_COMPLETED) { 8363 goto failure; /* Terminate */ 8364 } 8365 } else { 8366 *scsipkt->pkt_scbp = STATUS_CHECK; 8367 sense = sata_arq_sense(spx); 8368 sense->es_key = KEY_ILLEGAL_REQUEST; 8369 sense->es_add_code = 8370 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 8371 *pagelen = parmlen; 8372 *rval = TRAN_ACCEPT; 8373 return (SATA_FAILURE); 8374 } 8375 } 8376 8377 /* Note that the packet is not removed, so it could be re-used */ 8378 if (page->wce != wce) { 8379 if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) { 8380 /* Need to flip Write Cache setting */ 8381 if (page->wce == 1) 8382 /* Enable write cache */ 8383 scmd->satacmd_features_reg = 8384 SATAC_SF_ENABLE_WRITE_CACHE; 8385 else 8386 /* Disable write cache */ 8387 scmd->satacmd_features_reg = 8388 SATAC_SF_DISABLE_WRITE_CACHE; 8389 8390 /* Transfer command to HBA */ 8391 if (sata_hba_start(spx, rval) != 0) 8392 /* 8393 * Pkt not accepted for execution. 8394 */ 8395 return (SATA_FAILURE); 8396 8397 *dmod = 1; 8398 8399 /* Now process return */ 8400 if (spx->txlt_sata_pkt->satapkt_reason != 8401 SATA_PKT_COMPLETED) { 8402 goto failure; 8403 } 8404 } else { 8405 *scsipkt->pkt_scbp = STATUS_CHECK; 8406 sense = sata_arq_sense(spx); 8407 sense->es_key = KEY_ILLEGAL_REQUEST; 8408 sense->es_add_code = 8409 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 8410 *pagelen = parmlen; 8411 *rval = TRAN_ACCEPT; 8412 return (SATA_FAILURE); 8413 } 8414 } 8415 return (SATA_SUCCESS); 8416 8417 failure: 8418 sata_xlate_errors(spx); 8419 8420 return (SATA_FAILURE); 8421 } 8422 8423 /* 8424 * Process mode select informational exceptions control page 0x1c 8425 * 8426 * The only changeable bit is dexcpt (disable exceptions). 8427 * MRIE (method of reporting informational exceptions) must be 8428 * "only on request". 8429 * This page applies to informational exceptions that report 8430 * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh 8431 * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_). 8432 * Informational exception conditions occur as the result of background scan 8433 * errors, background self-test errors, or vendor specific events within a 8434 * logical unit. An informational exception condition may occur asynchronous 8435 * to any commands. 8436 * 8437 * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 8438 * If operation resulted in changing device setup, dmod flag should be set to 8439 * one (1). If parameters were not changed, dmod flag should be set to 0. 8440 * Upon return, if operation required sending command to the device, the rval 8441 * should be set to the value returned by sata_hba_start. If operation 8442 * did not require device access, rval should be set to TRAN_ACCEPT. 8443 * The pagelen should be set to the length of the page. 8444 * 8445 * This function has to be called with a port mutex held. 8446 * 8447 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 8448 * 8449 * Cannot be called in the interrupt context. 8450 */ 8451 static int 8452 sata_mode_select_page_1c( 8453 sata_pkt_txlate_t *spx, 8454 struct mode_info_excpt_page *page, 8455 int parmlen, 8456 int *pagelen, 8457 int *rval, 8458 int *dmod) 8459 { 8460 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 8461 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 8462 sata_drive_info_t *sdinfo; 8463 sata_id_t *sata_id; 8464 struct scsi_extended_sense *sense; 8465 8466 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 8467 &spx->txlt_sata_pkt->satapkt_device); 8468 sata_id = &sdinfo->satadrv_id; 8469 8470 *dmod = 0; 8471 8472 /* Verify parameters length. If too short, drop it */ 8473 if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) > parmlen) || 8474 page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) { 8475 *scsipkt->pkt_scbp = STATUS_CHECK; 8476 sense = sata_arq_sense(spx); 8477 sense->es_key = KEY_ILLEGAL_REQUEST; 8478 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 8479 *pagelen = parmlen; 8480 *rval = TRAN_ACCEPT; 8481 return (SATA_FAILURE); 8482 } 8483 8484 *pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page); 8485 8486 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 8487 *scsipkt->pkt_scbp = STATUS_CHECK; 8488 sense = sata_arq_sense(spx); 8489 sense->es_key = KEY_ILLEGAL_REQUEST; 8490 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 8491 *pagelen = parmlen; 8492 *rval = TRAN_ACCEPT; 8493 return (SATA_FAILURE); 8494 } 8495 8496 /* If already in the state requested, we are done */ 8497 if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 8498 /* nothing to do */ 8499 *rval = TRAN_ACCEPT; 8500 return (SATA_SUCCESS); 8501 } 8502 8503 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 8504 8505 /* Build SMART_ENABLE or SMART_DISABLE command */ 8506 scmd->satacmd_addr_type = 0; /* N/A */ 8507 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 8508 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 8509 scmd->satacmd_features_reg = page->dexcpt ? 8510 SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS; 8511 scmd->satacmd_device_reg = 0; /* Always device 0 */ 8512 scmd->satacmd_cmd_reg = SATAC_SMART; 8513 8514 /* Transfer command to HBA */ 8515 if (sata_hba_start(spx, rval) != 0) 8516 /* 8517 * Pkt not accepted for execution. 8518 */ 8519 return (SATA_FAILURE); 8520 8521 *dmod = 1; /* At least may have been modified */ 8522 8523 /* Now process return */ 8524 if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) 8525 return (SATA_SUCCESS); 8526 8527 /* Packet did not complete successfully */ 8528 sata_xlate_errors(spx); 8529 8530 return (SATA_FAILURE); 8531 } 8532 8533 /* 8534 * Process mode select acoustic management control page 0x30 8535 * 8536 * 8537 * This function has to be called with a port mutex held. 8538 * 8539 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 8540 * 8541 * Cannot be called in the interrupt context. 8542 */ 8543 int 8544 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct 8545 mode_acoustic_management *page, int parmlen, int *pagelen, 8546 int *rval, int *dmod) 8547 { 8548 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 8549 sata_drive_info_t *sdinfo; 8550 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 8551 sata_id_t *sata_id; 8552 struct scsi_extended_sense *sense; 8553 8554 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 8555 &spx->txlt_sata_pkt->satapkt_device); 8556 sata_id = &sdinfo->satadrv_id; 8557 *dmod = 0; 8558 8559 /* If parmlen is too short or the feature is not supported, drop it */ 8560 if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 8561 sizeof (struct mode_page)) > parmlen) || 8562 (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) { 8563 *scsipkt->pkt_scbp = STATUS_CHECK; 8564 sense = sata_arq_sense(spx); 8565 sense->es_key = KEY_ILLEGAL_REQUEST; 8566 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 8567 *pagelen = parmlen; 8568 *rval = TRAN_ACCEPT; 8569 return (SATA_FAILURE); 8570 } 8571 8572 *pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 8573 sizeof (struct mode_page); 8574 8575 /* 8576 * We can enable and disable acoustice management and 8577 * set the acoustic management level. 8578 */ 8579 8580 /* 8581 * Set-up Internal SET FEATURES command(s) 8582 */ 8583 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 8584 scmd->satacmd_addr_type = 0; 8585 scmd->satacmd_device_reg = 0; 8586 scmd->satacmd_status_reg = 0; 8587 scmd->satacmd_error_reg = 0; 8588 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 8589 if (page->acoustic_manag_enable) { 8590 scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC; 8591 scmd->satacmd_sec_count_lsb = page->acoustic_manag_level; 8592 } else { /* disabling acoustic management */ 8593 scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC; 8594 } 8595 8596 /* Transfer command to HBA */ 8597 if (sata_hba_start(spx, rval) != 0) 8598 /* 8599 * Pkt not accepted for execution. 8600 */ 8601 return (SATA_FAILURE); 8602 8603 /* Now process return */ 8604 if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) { 8605 sata_xlate_errors(spx); 8606 return (SATA_FAILURE); 8607 } 8608 8609 *dmod = 1; 8610 8611 return (SATA_SUCCESS); 8612 } 8613 8614 /* 8615 * Process mode select power condition page 0x1a 8616 * 8617 * This function has to be called with a port mutex held. 8618 * 8619 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 8620 * 8621 * Cannot be called in the interrupt context. 8622 */ 8623 int 8624 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct 8625 mode_info_power_cond *page, int parmlen, int *pagelen, 8626 int *rval, int *dmod) 8627 { 8628 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 8629 sata_drive_info_t *sdinfo; 8630 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 8631 sata_id_t *sata_id; 8632 struct scsi_extended_sense *sense; 8633 uint8_t ata_count; 8634 int i, len; 8635 8636 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 8637 &spx->txlt_sata_pkt->satapkt_device); 8638 sata_id = &sdinfo->satadrv_id; 8639 *dmod = 0; 8640 8641 len = sizeof (struct mode_info_power_cond); 8642 len += sizeof (struct mode_page); 8643 8644 /* If parmlen is too short or the feature is not supported, drop it */ 8645 if ((len < parmlen) || (page->idle == 1) || 8646 (!(sata_id->ai_cap & SATA_STANDBYTIMER) && page->standby == 1)) { 8647 *scsipkt->pkt_scbp = STATUS_CHECK; 8648 sense = sata_arq_sense(spx); 8649 sense->es_key = KEY_ILLEGAL_REQUEST; 8650 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 8651 *pagelen = parmlen; 8652 *rval = TRAN_ACCEPT; 8653 return (SATA_FAILURE); 8654 } 8655 8656 *pagelen = len; 8657 8658 /* 8659 * Set-up Internal STANDBY command(s) 8660 */ 8661 if (page->standby == 0) 8662 goto out; 8663 8664 ata_count = sata_get_standby_timer(page->standby_cond_timer); 8665 8666 scmd->satacmd_addr_type = 0; 8667 scmd->satacmd_sec_count_lsb = ata_count; 8668 scmd->satacmd_lba_low_lsb = 0; 8669 scmd->satacmd_lba_mid_lsb = 0; 8670 scmd->satacmd_lba_high_lsb = 0; 8671 scmd->satacmd_features_reg = 0; 8672 scmd->satacmd_device_reg = 0; 8673 scmd->satacmd_status_reg = 0; 8674 scmd->satacmd_cmd_reg = SATAC_STANDBY; 8675 scmd->satacmd_flags.sata_special_regs = B_TRUE; 8676 scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE; 8677 8678 /* Transfer command to HBA */ 8679 if (sata_hba_start(spx, rval) != 0) { 8680 return (SATA_FAILURE); 8681 } else { 8682 if ((scmd->satacmd_error_reg != 0) || 8683 (spx->txlt_sata_pkt->satapkt_reason != 8684 SATA_PKT_COMPLETED)) { 8685 sata_xlate_errors(spx); 8686 return (SATA_FAILURE); 8687 } 8688 } 8689 8690 for (i = 0; i < 4; i++) { 8691 sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i]; 8692 } 8693 out: 8694 *dmod = 1; 8695 return (SATA_SUCCESS); 8696 } 8697 8698 /* 8699 * sata_build_lsense_page0() is used to create the 8700 * SCSI LOG SENSE page 0 (supported log pages) 8701 * 8702 * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e 8703 * (supported log pages, self-test results, informational exceptions 8704 * Sun vendor specific ATA SMART data, and start stop cycle counter). 8705 * 8706 * Takes a sata_drive_info t * and the address of a buffer 8707 * in which to create the page information. 8708 * 8709 * Returns the number of bytes valid in the buffer. 8710 */ 8711 static int 8712 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf) 8713 { 8714 struct log_parameter *lpp = (struct log_parameter *)buf; 8715 uint8_t *page_ptr = (uint8_t *)lpp->param_values; 8716 int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */ 8717 sata_id_t *sata_id = &sdinfo->satadrv_id; 8718 8719 lpp->param_code[0] = 0; 8720 lpp->param_code[1] = 0; 8721 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 8722 *page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES; 8723 8724 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 8725 if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) { 8726 *page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS; 8727 ++num_pages_supported; 8728 } 8729 *page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS; 8730 ++num_pages_supported; 8731 *page_ptr++ = PAGE_CODE_SMART_READ_DATA; 8732 ++num_pages_supported; 8733 *page_ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER; 8734 ++num_pages_supported; 8735 } 8736 8737 lpp->param_len = num_pages_supported; 8738 8739 return ((&lpp->param_values[0] - (uint8_t *)lpp) + 8740 num_pages_supported); 8741 } 8742 8743 /* 8744 * sata_build_lsense_page_10() is used to create the 8745 * SCSI LOG SENSE page 0x10 (self-test results) 8746 * 8747 * Takes a sata_drive_info t * and the address of a buffer 8748 * in which to create the page information as well as a sata_hba_inst_t *. 8749 * 8750 * Returns the number of bytes valid in the buffer. 8751 * 8752 * Note: Self test and SMART data is accessible in device log pages. 8753 * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors 8754 * of data can be transferred by a single command), or by the General Purpose 8755 * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors 8756 * - approximately 33MB - can be transferred by a single command. 8757 * The SCT Command response (either error or command) is the same for both 8758 * the SMART and GPL methods of issuing commands. 8759 * This function uses READ LOG EXT command when drive supports LBA48, and 8760 * SMART READ command otherwise. 8761 * 8762 * Since above commands are executed in a synchronous mode, this function 8763 * should not be called in an interrupt context. 8764 */ 8765 static int 8766 sata_build_lsense_page_10( 8767 sata_drive_info_t *sdinfo, 8768 uint8_t *buf, 8769 sata_hba_inst_t *sata_hba_inst) 8770 { 8771 struct log_parameter *lpp = (struct log_parameter *)buf; 8772 int rval; 8773 8774 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 8775 struct smart_ext_selftest_log *ext_selftest_log; 8776 8777 ext_selftest_log = kmem_zalloc( 8778 sizeof (struct smart_ext_selftest_log), KM_SLEEP); 8779 8780 rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo, 8781 ext_selftest_log, 0); 8782 if (rval == 0) { 8783 int index, start_index; 8784 struct smart_ext_selftest_log_entry *entry; 8785 static const struct smart_ext_selftest_log_entry empty = 8786 {0}; 8787 uint16_t block_num; 8788 int count; 8789 boolean_t only_one_block = B_FALSE; 8790 8791 index = ext_selftest_log-> 8792 smart_ext_selftest_log_index[0]; 8793 index |= ext_selftest_log-> 8794 smart_ext_selftest_log_index[1] << 8; 8795 if (index == 0) 8796 goto out; 8797 8798 --index; /* Correct for 0 origin */ 8799 start_index = index; /* remember where we started */ 8800 block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 8801 if (block_num != 0) { 8802 rval = sata_ext_smart_selftest_read_log( 8803 sata_hba_inst, sdinfo, ext_selftest_log, 8804 block_num); 8805 if (rval != 0) 8806 goto out; 8807 } 8808 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 8809 entry = 8810 &ext_selftest_log-> 8811 smart_ext_selftest_log_entries[index]; 8812 8813 for (count = 1; 8814 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 8815 ++count) { 8816 uint8_t status; 8817 uint8_t code; 8818 uint8_t sense_key; 8819 uint8_t add_sense_code; 8820 uint8_t add_sense_code_qual; 8821 8822 /* If this is an unused entry, we are done */ 8823 if (bcmp(entry, &empty, sizeof (empty)) == 0) { 8824 /* Broken firmware on some disks */ 8825 if (index + 1 == 8826 ENTRIES_PER_EXT_SELFTEST_LOG_BLK) { 8827 --entry; 8828 --index; 8829 if (bcmp(entry, &empty, 8830 sizeof (empty)) == 0) 8831 goto out; 8832 } else 8833 goto out; 8834 } 8835 8836 if (only_one_block && 8837 start_index == index) 8838 goto out; 8839 8840 lpp->param_code[0] = 0; 8841 lpp->param_code[1] = count; 8842 lpp->param_ctrl_flags = 8843 LOG_CTRL_LP | LOG_CTRL_LBIN; 8844 lpp->param_len = 8845 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 8846 8847 status = entry->smart_ext_selftest_log_status; 8848 status >>= 4; 8849 switch (status) { 8850 case 0: 8851 default: 8852 sense_key = KEY_NO_SENSE; 8853 add_sense_code = 8854 SD_SCSI_ASC_NO_ADD_SENSE; 8855 add_sense_code_qual = 0; 8856 break; 8857 case 1: 8858 sense_key = KEY_ABORTED_COMMAND; 8859 add_sense_code = 8860 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8861 add_sense_code_qual = SCSI_COMPONENT_81; 8862 break; 8863 case 2: 8864 sense_key = KEY_ABORTED_COMMAND; 8865 add_sense_code = 8866 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8867 add_sense_code_qual = SCSI_COMPONENT_82; 8868 break; 8869 case 3: 8870 sense_key = KEY_ABORTED_COMMAND; 8871 add_sense_code = 8872 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8873 add_sense_code_qual = SCSI_COMPONENT_83; 8874 break; 8875 case 4: 8876 sense_key = KEY_HARDWARE_ERROR; 8877 add_sense_code = 8878 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8879 add_sense_code_qual = SCSI_COMPONENT_84; 8880 break; 8881 case 5: 8882 sense_key = KEY_HARDWARE_ERROR; 8883 add_sense_code = 8884 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8885 add_sense_code_qual = SCSI_COMPONENT_85; 8886 break; 8887 case 6: 8888 sense_key = KEY_HARDWARE_ERROR; 8889 add_sense_code = 8890 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8891 add_sense_code_qual = SCSI_COMPONENT_86; 8892 break; 8893 case 7: 8894 sense_key = KEY_MEDIUM_ERROR; 8895 add_sense_code = 8896 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8897 add_sense_code_qual = SCSI_COMPONENT_87; 8898 break; 8899 case 8: 8900 sense_key = KEY_HARDWARE_ERROR; 8901 add_sense_code = 8902 DIAGNOSTIC_FAILURE_ON_COMPONENT; 8903 add_sense_code_qual = SCSI_COMPONENT_88; 8904 break; 8905 } 8906 code = 0; /* unspecified */ 8907 status |= (code << 4); 8908 lpp->param_values[0] = status; 8909 lpp->param_values[1] = 0; /* unspecified */ 8910 lpp->param_values[2] = entry-> 8911 smart_ext_selftest_log_timestamp[1]; 8912 lpp->param_values[3] = entry-> 8913 smart_ext_selftest_log_timestamp[0]; 8914 if (status != 0) { 8915 lpp->param_values[4] = 0; 8916 lpp->param_values[5] = 0; 8917 lpp->param_values[6] = entry-> 8918 smart_ext_selftest_log_failing_lba 8919 [5]; 8920 lpp->param_values[7] = entry-> 8921 smart_ext_selftest_log_failing_lba 8922 [4]; 8923 lpp->param_values[8] = entry-> 8924 smart_ext_selftest_log_failing_lba 8925 [3]; 8926 lpp->param_values[9] = entry-> 8927 smart_ext_selftest_log_failing_lba 8928 [2]; 8929 lpp->param_values[10] = entry-> 8930 smart_ext_selftest_log_failing_lba 8931 [1]; 8932 lpp->param_values[11] = entry-> 8933 smart_ext_selftest_log_failing_lba 8934 [0]; 8935 } else { /* No bad block address */ 8936 lpp->param_values[4] = 0xff; 8937 lpp->param_values[5] = 0xff; 8938 lpp->param_values[6] = 0xff; 8939 lpp->param_values[7] = 0xff; 8940 lpp->param_values[8] = 0xff; 8941 lpp->param_values[9] = 0xff; 8942 lpp->param_values[10] = 0xff; 8943 lpp->param_values[11] = 0xff; 8944 } 8945 8946 lpp->param_values[12] = sense_key; 8947 lpp->param_values[13] = add_sense_code; 8948 lpp->param_values[14] = add_sense_code_qual; 8949 lpp->param_values[15] = 0; /* undefined */ 8950 8951 lpp = (struct log_parameter *) 8952 (((uint8_t *)lpp) + 8953 SCSI_LOG_PARAM_HDR_LEN + 8954 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 8955 8956 --index; /* Back up to previous entry */ 8957 if (index < 0) { 8958 if (block_num > 0) { 8959 --block_num; 8960 } else { 8961 struct read_log_ext_directory 8962 logdir; 8963 8964 rval = 8965 sata_read_log_ext_directory( 8966 sata_hba_inst, sdinfo, 8967 &logdir); 8968 if (rval == -1) 8969 goto out; 8970 if ((logdir.read_log_ext_vers 8971 [0] == 0) && 8972 (logdir.read_log_ext_vers 8973 [1] == 0)) 8974 goto out; 8975 block_num = 8976 logdir.read_log_ext_nblks 8977 [EXT_SMART_SELFTEST_LOG_PAGE 8978 - 1][0]; 8979 block_num |= logdir. 8980 read_log_ext_nblks 8981 [EXT_SMART_SELFTEST_LOG_PAGE 8982 - 1][1] << 8; 8983 --block_num; 8984 only_one_block = 8985 (block_num == 0); 8986 } 8987 rval = sata_ext_smart_selftest_read_log( 8988 sata_hba_inst, sdinfo, 8989 ext_selftest_log, block_num); 8990 if (rval != 0) 8991 goto out; 8992 8993 index = 8994 ENTRIES_PER_EXT_SELFTEST_LOG_BLK - 8995 1; 8996 } 8997 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 8998 entry = &ext_selftest_log-> 8999 smart_ext_selftest_log_entries[index]; 9000 } 9001 } 9002 out: 9003 kmem_free(ext_selftest_log, 9004 sizeof (struct smart_ext_selftest_log)); 9005 } else { 9006 struct smart_selftest_log *selftest_log; 9007 9008 selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log), 9009 KM_SLEEP); 9010 9011 rval = sata_smart_selftest_log(sata_hba_inst, sdinfo, 9012 selftest_log); 9013 9014 if (rval == 0) { 9015 int index; 9016 int count; 9017 struct smart_selftest_log_entry *entry; 9018 static const struct smart_selftest_log_entry empty = 9019 { 0 }; 9020 9021 index = selftest_log->smart_selftest_log_index; 9022 if (index == 0) 9023 goto done; 9024 --index; /* Correct for 0 origin */ 9025 entry = &selftest_log-> 9026 smart_selftest_log_entries[index]; 9027 for (count = 1; 9028 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 9029 ++count) { 9030 uint8_t status; 9031 uint8_t code; 9032 uint8_t sense_key; 9033 uint8_t add_sense_code; 9034 uint8_t add_sense_code_qual = 0; 9035 9036 if (bcmp(entry, &empty, sizeof (empty)) == 0) 9037 goto done; 9038 9039 lpp->param_code[0] = 0; 9040 lpp->param_code[1] = count; 9041 lpp->param_ctrl_flags = 9042 LOG_CTRL_LP | LOG_CTRL_LBIN; 9043 lpp->param_len = 9044 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 9045 9046 status = entry->smart_selftest_log_status; 9047 status >>= 4; 9048 switch (status) { 9049 case 0: 9050 default: 9051 sense_key = KEY_NO_SENSE; 9052 add_sense_code = 9053 SD_SCSI_ASC_NO_ADD_SENSE; 9054 break; 9055 case 1: 9056 sense_key = KEY_ABORTED_COMMAND; 9057 add_sense_code = 9058 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9059 add_sense_code_qual = SCSI_COMPONENT_81; 9060 break; 9061 case 2: 9062 sense_key = KEY_ABORTED_COMMAND; 9063 add_sense_code = 9064 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9065 add_sense_code_qual = SCSI_COMPONENT_82; 9066 break; 9067 case 3: 9068 sense_key = KEY_ABORTED_COMMAND; 9069 add_sense_code = 9070 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9071 add_sense_code_qual = SCSI_COMPONENT_83; 9072 break; 9073 case 4: 9074 sense_key = KEY_HARDWARE_ERROR; 9075 add_sense_code = 9076 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9077 add_sense_code_qual = SCSI_COMPONENT_84; 9078 break; 9079 case 5: 9080 sense_key = KEY_HARDWARE_ERROR; 9081 add_sense_code = 9082 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9083 add_sense_code_qual = SCSI_COMPONENT_85; 9084 break; 9085 case 6: 9086 sense_key = KEY_HARDWARE_ERROR; 9087 add_sense_code = 9088 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9089 add_sense_code_qual = SCSI_COMPONENT_86; 9090 break; 9091 case 7: 9092 sense_key = KEY_MEDIUM_ERROR; 9093 add_sense_code = 9094 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9095 add_sense_code_qual = SCSI_COMPONENT_87; 9096 break; 9097 case 8: 9098 sense_key = KEY_HARDWARE_ERROR; 9099 add_sense_code = 9100 DIAGNOSTIC_FAILURE_ON_COMPONENT; 9101 add_sense_code_qual = SCSI_COMPONENT_88; 9102 break; 9103 } 9104 code = 0; /* unspecified */ 9105 status |= (code << 4); 9106 lpp->param_values[0] = status; 9107 lpp->param_values[1] = 0; /* unspecified */ 9108 lpp->param_values[2] = entry-> 9109 smart_selftest_log_timestamp[1]; 9110 lpp->param_values[3] = entry-> 9111 smart_selftest_log_timestamp[0]; 9112 if (status != 0) { 9113 lpp->param_values[4] = 0; 9114 lpp->param_values[5] = 0; 9115 lpp->param_values[6] = 0; 9116 lpp->param_values[7] = 0; 9117 lpp->param_values[8] = entry-> 9118 smart_selftest_log_failing_lba[3]; 9119 lpp->param_values[9] = entry-> 9120 smart_selftest_log_failing_lba[2]; 9121 lpp->param_values[10] = entry-> 9122 smart_selftest_log_failing_lba[1]; 9123 lpp->param_values[11] = entry-> 9124 smart_selftest_log_failing_lba[0]; 9125 } else { /* No block address */ 9126 lpp->param_values[4] = 0xff; 9127 lpp->param_values[5] = 0xff; 9128 lpp->param_values[6] = 0xff; 9129 lpp->param_values[7] = 0xff; 9130 lpp->param_values[8] = 0xff; 9131 lpp->param_values[9] = 0xff; 9132 lpp->param_values[10] = 0xff; 9133 lpp->param_values[11] = 0xff; 9134 } 9135 lpp->param_values[12] = sense_key; 9136 lpp->param_values[13] = add_sense_code; 9137 lpp->param_values[14] = add_sense_code_qual; 9138 lpp->param_values[15] = 0; /* undefined */ 9139 9140 lpp = (struct log_parameter *) 9141 (((uint8_t *)lpp) + 9142 SCSI_LOG_PARAM_HDR_LEN + 9143 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 9144 --index; /* back up to previous entry */ 9145 if (index < 0) { 9146 index = 9147 NUM_SMART_SELFTEST_LOG_ENTRIES - 1; 9148 } 9149 entry = &selftest_log-> 9150 smart_selftest_log_entries[index]; 9151 } 9152 } 9153 done: 9154 kmem_free(selftest_log, sizeof (struct smart_selftest_log)); 9155 } 9156 9157 return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) * 9158 SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS); 9159 } 9160 9161 /* 9162 * sata_build_lsense_page_2f() is used to create the 9163 * SCSI LOG SENSE page 0x2f (informational exceptions) 9164 * 9165 * Takes a sata_drive_info t * and the address of a buffer 9166 * in which to create the page information as well as a sata_hba_inst_t *. 9167 * 9168 * Returns the number of bytes valid in the buffer. 9169 * 9170 * Because it invokes function(s) that send synchronously executed command 9171 * to the HBA, it cannot be called in the interrupt context. 9172 */ 9173 static int 9174 sata_build_lsense_page_2f( 9175 sata_drive_info_t *sdinfo, 9176 uint8_t *buf, 9177 sata_hba_inst_t *sata_hba_inst) 9178 { 9179 struct log_parameter *lpp = (struct log_parameter *)buf; 9180 int rval; 9181 uint8_t *smart_data; 9182 uint8_t temp; 9183 sata_id_t *sata_id; 9184 #define SMART_NO_TEMP 0xff 9185 9186 lpp->param_code[0] = 0; 9187 lpp->param_code[1] = 0; 9188 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 9189 9190 /* Now get the SMART status w.r.t. threshold exceeded */ 9191 rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo); 9192 switch (rval) { 9193 case 1: 9194 lpp->param_values[0] = SCSI_PREDICTED_FAILURE; 9195 lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE; 9196 break; 9197 case 0: 9198 case -1: /* failed to get data */ 9199 lpp->param_values[0] = 0; /* No failure predicted */ 9200 lpp->param_values[1] = 0; 9201 break; 9202 #if defined(SATA_DEBUG) 9203 default: 9204 cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value"); 9205 /* NOTREACHED */ 9206 #endif 9207 } 9208 9209 sata_id = &sdinfo->satadrv_id; 9210 if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP)) 9211 temp = SMART_NO_TEMP; 9212 else { 9213 /* Now get the temperature */ 9214 smart_data = kmem_zalloc(512, KM_SLEEP); 9215 rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data, 9216 SCT_STATUS_LOG_PAGE, 1); 9217 if (rval == -1) 9218 temp = SMART_NO_TEMP; 9219 else { 9220 temp = smart_data[200]; 9221 if (temp & 0x80) { 9222 if (temp & 0x7f) 9223 temp = 0; 9224 else 9225 temp = SMART_NO_TEMP; 9226 } 9227 } 9228 kmem_free(smart_data, 512); 9229 } 9230 9231 lpp->param_values[2] = temp; /* most recent temperature */ 9232 lpp->param_values[3] = 0; /* required vendor specific byte */ 9233 9234 lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN; 9235 9236 9237 return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN); 9238 } 9239 9240 /* 9241 * sata_build_lsense_page_30() is used to create the 9242 * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data). 9243 * 9244 * Takes a sata_drive_info t * and the address of a buffer 9245 * in which to create the page information as well as a sata_hba_inst_t *. 9246 * 9247 * Returns the number of bytes valid in the buffer. 9248 */ 9249 static int 9250 sata_build_lsense_page_30( 9251 sata_drive_info_t *sdinfo, 9252 uint8_t *buf, 9253 sata_hba_inst_t *sata_hba_inst) 9254 { 9255 struct smart_data *smart_data = (struct smart_data *)buf; 9256 int rval; 9257 9258 /* Now do the SMART READ DATA */ 9259 rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data); 9260 if (rval == -1) 9261 return (0); 9262 9263 return (sizeof (struct smart_data)); 9264 } 9265 9266 /* 9267 * sata_build_lsense_page_0e() is used to create the 9268 * SCSI LOG SENSE page 0e (start-stop cycle counter page) 9269 * 9270 * Date of Manufacture (0x0001) 9271 * YEAR = "0000" 9272 * WEEK = "00" 9273 * Accounting Date (0x0002) 9274 * 6 ASCII space character(20h) 9275 * Specified cycle count over device lifetime 9276 * VALUE - THRESH - the delta between max and min; 9277 * Accumulated start-stop cycles 9278 * VALUE - WORST - the accumulated cycles; 9279 * 9280 * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute 9281 * 9282 * Takes a sata_drive_info t * and the address of a buffer 9283 * in which to create the page information as well as a sata_hba_inst_t *. 9284 * 9285 * Returns the number of bytes valid in the buffer. 9286 */ 9287 static int 9288 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf, 9289 sata_pkt_txlate_t *spx) 9290 { 9291 struct start_stop_cycle_counter_log *log_page; 9292 int i, rval, index; 9293 uint8_t smart_data[512], id, value, worst, thresh; 9294 uint32_t max_count, cycles; 9295 9296 /* Now do the SMART READ DATA */ 9297 rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo, 9298 (struct smart_data *)smart_data); 9299 if (rval == -1) 9300 return (0); 9301 for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) { 9302 index = (i * 12) + 2; 9303 id = smart_data[index]; 9304 if (id != SMART_START_STOP_COUNT_ID) 9305 continue; 9306 else { 9307 thresh = smart_data[index + 2]; 9308 value = smart_data[index + 3]; 9309 worst = smart_data[index + 4]; 9310 break; 9311 } 9312 } 9313 if (id != SMART_START_STOP_COUNT_ID) 9314 return (0); 9315 max_count = value - thresh; 9316 cycles = value - worst; 9317 9318 log_page = (struct start_stop_cycle_counter_log *)buf; 9319 bzero(log_page, sizeof (struct start_stop_cycle_counter_log)); 9320 log_page->code = 0x0e; 9321 log_page->page_len_low = 0x24; 9322 9323 log_page->manufactor_date_low = 0x1; 9324 log_page->param_1.fmt_link = 0x1; /* 01b */ 9325 log_page->param_len_1 = 0x06; 9326 for (i = 0; i < 4; i++) { 9327 log_page->year_manu[i] = 0x30; 9328 if (i < 2) 9329 log_page->week_manu[i] = 0x30; 9330 } 9331 9332 log_page->account_date_low = 0x02; 9333 log_page->param_2.fmt_link = 0x01; /* 01b */ 9334 log_page->param_len_2 = 0x06; 9335 for (i = 0; i < 4; i++) { 9336 log_page->year_account[i] = 0x20; 9337 if (i < 2) 9338 log_page->week_account[i] = 0x20; 9339 } 9340 9341 log_page->lifetime_code_low = 0x03; 9342 log_page->param_3.fmt_link = 0x03; /* 11b */ 9343 log_page->param_len_3 = 0x04; 9344 /* VALUE - THRESH - the delta between max and min */ 9345 log_page->cycle_code_low = 0x04; 9346 log_page->param_4.fmt_link = 0x03; /* 11b */ 9347 log_page->param_len_4 = 0x04; 9348 /* WORST - THRESH - the distance from 'now' to min */ 9349 9350 for (i = 0; i < 4; i++) { 9351 log_page->cycle_lifetime[i] = 9352 (max_count >> (8 * (3 - i))) & 0xff; 9353 log_page->cycle_accumulated[i] = 9354 (cycles >> (8 * (3 - i))) & 0xff; 9355 } 9356 9357 return (sizeof (struct start_stop_cycle_counter_log)); 9358 } 9359 9360 /* 9361 * This function was used for build a ATA read verify sector command 9362 */ 9363 static void 9364 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba) 9365 { 9366 scmd->satacmd_cmd_reg = SATAC_RDVER; 9367 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 9368 scmd->satacmd_flags.sata_special_regs = B_TRUE; 9369 9370 scmd->satacmd_sec_count_lsb = sec & 0xff; 9371 scmd->satacmd_lba_low_lsb = lba & 0xff; 9372 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 9373 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 9374 scmd->satacmd_device_reg = (SATA_ADH_LBA | ((lba >> 24) & 0xf)); 9375 scmd->satacmd_features_reg = 0; 9376 scmd->satacmd_status_reg = 0; 9377 scmd->satacmd_error_reg = 0; 9378 } 9379 9380 /* 9381 * This function was used for building an ATA 9382 * command, and only command register need to 9383 * be defined, other register will be zero or na. 9384 */ 9385 static void 9386 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd) 9387 { 9388 scmd->satacmd_addr_type = 0; 9389 scmd->satacmd_cmd_reg = cmd; 9390 scmd->satacmd_device_reg = 0; 9391 scmd->satacmd_sec_count_lsb = 0; 9392 scmd->satacmd_lba_low_lsb = 0; 9393 scmd->satacmd_lba_mid_lsb = 0; 9394 scmd->satacmd_lba_high_lsb = 0; 9395 scmd->satacmd_features_reg = 0; 9396 scmd->satacmd_status_reg = 0; 9397 scmd->satacmd_error_reg = 0; 9398 scmd->satacmd_flags.sata_special_regs = B_TRUE; 9399 } 9400 9401 /* 9402 * This function was used for changing the standby 9403 * timer format from SCSI to ATA. 9404 */ 9405 static uint8_t 9406 sata_get_standby_timer(uint8_t *timer) 9407 { 9408 uint32_t i = 0, count = 0; 9409 uint8_t ata_count; 9410 9411 for (i = 0; i < 4; i++) { 9412 count = count << 8 | timer[i]; 9413 } 9414 9415 if (count == 0) 9416 return (0); 9417 9418 if (count >= 1 && count <= 12000) 9419 ata_count = (count -1) / 50 + 1; 9420 else if (count > 12000 && count <= 12600) 9421 ata_count = 0xfc; 9422 else if (count > 12601 && count <= 12750) 9423 ata_count = 0xff; 9424 else if (count > 12750 && count <= 17999) 9425 ata_count = 0xf1; 9426 else if (count > 18000 && count <= 198000) 9427 ata_count = count / 18000 + 240; 9428 else 9429 ata_count = 0xfd; 9430 return (ata_count); 9431 } 9432 9433 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */ 9434 9435 /* 9436 * Start command for ATAPI device. 9437 * This function processes scsi_pkt requests. 9438 * Now CD/DVD, tape and ATAPI disk devices are supported. 9439 * Most commands are packet without any translation into Packet Command. 9440 * Some may be trapped and executed as SATA commands (not clear which one). 9441 * 9442 * Returns TRAN_ACCEPT if command is accepted for execution (or completed 9443 * execution). 9444 * Returns other TRAN_XXXX codes if command is not accepted or completed 9445 * (see return values for sata_hba_start()). 9446 * 9447 * Note: 9448 * Inquiry cdb format differs between transport version 2 and 3. 9449 * However, the transport version 3 devices that were checked did not adhere 9450 * to the specification (ignored MSB of the allocation length). Therefore, 9451 * the transport version is not checked, but Inquiry allocation length is 9452 * truncated to 255 bytes if the original allocation length set-up by the 9453 * target driver is greater than 255 bytes. 9454 */ 9455 static int 9456 sata_txlt_atapi(sata_pkt_txlate_t *spx) 9457 { 9458 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 9459 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 9460 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 9461 sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx); 9462 sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba, 9463 &spx->txlt_sata_pkt->satapkt_device); 9464 kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx)); 9465 int cdblen; 9466 int rval, reason; 9467 int synch; 9468 union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp; 9469 9470 mutex_enter(cport_mutex); 9471 9472 if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) != 9473 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 9474 mutex_exit(cport_mutex); 9475 return (rval); 9476 } 9477 9478 /* 9479 * ATAPI device executes some ATA commands in addition to those 9480 * commands sent via PACKET command. These ATA commands may be 9481 * executed by the regular SATA translation functions. None needs 9482 * to be captured now. 9483 * 9484 * Commands sent via PACKET command include: 9485 * MMC command set for ATAPI CD/DVD device 9486 * SSC command set for ATAPI TAPE device 9487 * SBC command set for ATAPI disk device 9488 * 9489 */ 9490 9491 /* Check the size of cdb */ 9492 9493 switch (GETGROUP(cdbp)) { 9494 case CDB_GROUPID_3: /* Reserved, per SPC-4 */ 9495 /* 9496 * opcodes 0x7e and 0x7f identify variable-length CDBs and 9497 * therefore require special handling. Return failure, for now. 9498 */ 9499 mutex_exit(cport_mutex); 9500 return (TRAN_BADPKT); 9501 9502 case CDB_GROUPID_6: /* Vendor-specific, per SPC-4 */ 9503 case CDB_GROUPID_7: /* Vendor-specific, per SPC-4 */ 9504 /* obtain length from the scsi_pkt */ 9505 cdblen = scsipkt->pkt_cdblen; 9506 break; 9507 9508 default: 9509 /* CDB's length is statically known, per SPC-4 */ 9510 cdblen = scsi_cdb_size[GETGROUP(cdbp)]; 9511 break; 9512 } 9513 9514 if (cdblen <= 0 || cdblen > sdinfo->satadrv_atapi_cdb_len) { 9515 sata_log(NULL, CE_WARN, 9516 "sata: invalid ATAPI cdb length %d", 9517 cdblen); 9518 mutex_exit(cport_mutex); 9519 return (TRAN_BADPKT); 9520 } 9521 9522 SATAATAPITRACE(spx, cdblen); 9523 9524 /* 9525 * For non-read/write commands we need to 9526 * map buffer 9527 */ 9528 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 9529 case SCMD_READ: 9530 case SCMD_READ_G1: 9531 case SCMD_READ_G5: 9532 case SCMD_READ_G4: 9533 case SCMD_WRITE: 9534 case SCMD_WRITE_G1: 9535 case SCMD_WRITE_G5: 9536 case SCMD_WRITE_G4: 9537 break; 9538 default: 9539 if (bp != NULL) { 9540 if (bp->b_flags & (B_PHYS | B_PAGEIO)) 9541 bp_mapin(bp); 9542 } 9543 break; 9544 } 9545 /* 9546 * scmd->satacmd_flags.sata_data_direction default - 9547 * SATA_DIR_NODATA_XFER - is set by 9548 * sata_txlt_generic_pkt_info(). 9549 */ 9550 if (scmd->satacmd_bp) { 9551 if (scmd->satacmd_bp->b_flags & B_READ) { 9552 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 9553 } else { 9554 scmd->satacmd_flags.sata_data_direction = 9555 SATA_DIR_WRITE; 9556 } 9557 } 9558 9559 /* 9560 * Set up ATAPI packet command. 9561 */ 9562 9563 sata_atapi_packet_cmd_setup(scmd, sdinfo); 9564 9565 /* Copy cdb into sata_cmd */ 9566 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 9567 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 9568 bcopy(cdbp, scmd->satacmd_acdb, cdblen); 9569 9570 /* See note in the command header */ 9571 if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) { 9572 if (scmd->satacmd_acdb[3] != 0) 9573 scmd->satacmd_acdb[4] = 255; 9574 } 9575 9576 #ifdef SATA_DEBUG 9577 if (sata_debug_flags & SATA_DBG_ATAPI) { 9578 uint8_t *p = scmd->satacmd_acdb; 9579 char buf[3 * SATA_ATAPI_MAX_CDB_LEN]; 9580 9581 (void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN, 9582 "%02x %02x %02x %02x %02x %02x %02x %02x " 9583 "%2x %02x %02x %02x %02x %02x %02x %02x", 9584 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 9585 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 9586 buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0'; 9587 cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf); 9588 } 9589 #endif 9590 9591 /* 9592 * Preset request sense data to NO SENSE. 9593 * If there is no way to get error information via Request Sense, 9594 * the packet request sense data would not have to be modified by HBA, 9595 * but it could be returned as is. 9596 */ 9597 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN); 9598 sata_fixed_sense_data_preset( 9599 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 9600 9601 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 9602 /* Need callback function */ 9603 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion; 9604 synch = FALSE; 9605 } else 9606 synch = TRUE; 9607 9608 /* Transfer command to HBA */ 9609 if (sata_hba_start(spx, &rval) != 0) { 9610 /* Pkt not accepted for execution */ 9611 mutex_exit(cport_mutex); 9612 return (rval); 9613 } 9614 mutex_exit(cport_mutex); 9615 /* 9616 * If execution is non-synchronous, 9617 * a callback function will handle potential errors, translate 9618 * the response and will do a callback to a target driver. 9619 * If it was synchronous, use the same framework callback to check 9620 * an execution status. 9621 */ 9622 if (synch) { 9623 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 9624 "synchronous execution status %x\n", 9625 spx->txlt_sata_pkt->satapkt_reason); 9626 sata_txlt_atapi_completion(spx->txlt_sata_pkt); 9627 } 9628 return (TRAN_ACCEPT); 9629 } 9630 9631 9632 /* 9633 * ATAPI Packet command completion. 9634 * 9635 * Failure of the command passed via Packet command are considered device 9636 * error. SATA HBA driver would have to retrieve error data (via Request 9637 * Sense command delivered via error retrieval sata packet) and copy it 9638 * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data. 9639 */ 9640 static void 9641 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt) 9642 { 9643 sata_pkt_txlate_t *spx = 9644 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 9645 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 9646 struct scsi_extended_sense *sense; 9647 struct buf *bp; 9648 int rval; 9649 9650 #ifdef SATA_DEBUG 9651 uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense; 9652 #endif 9653 9654 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 9655 STATE_SENT_CMD | STATE_GOT_STATUS; 9656 9657 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 9658 /* Normal completion */ 9659 if (sata_pkt->satapkt_cmd.satacmd_bp != NULL) 9660 scsipkt->pkt_state |= STATE_XFERRED_DATA; 9661 scsipkt->pkt_reason = CMD_CMPLT; 9662 *scsipkt->pkt_scbp = STATUS_GOOD; 9663 if (spx->txlt_tmp_buf != NULL) { 9664 /* Temporary buffer was used */ 9665 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 9666 if (bp->b_flags & B_READ) { 9667 rval = ddi_dma_sync( 9668 spx->txlt_buf_dma_handle, 0, 0, 9669 DDI_DMA_SYNC_FORCPU); 9670 ASSERT(rval == DDI_SUCCESS); 9671 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 9672 bp->b_bcount); 9673 } 9674 } 9675 } else { 9676 /* 9677 * Something went wrong - analyze return 9678 */ 9679 *scsipkt->pkt_scbp = STATUS_CHECK; 9680 sense = sata_arq_sense(spx); 9681 9682 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 9683 /* 9684 * pkt_reason should be CMD_CMPLT for DEVICE ERROR. 9685 * Under this condition ERR bit is set for ATA command, 9686 * and CHK bit set for ATAPI command. 9687 * 9688 * Please check st_intr & sdintr about how pkt_reason 9689 * is used. 9690 */ 9691 scsipkt->pkt_reason = CMD_CMPLT; 9692 9693 /* 9694 * We may not have ARQ data if there was a double 9695 * error. But sense data in sata packet was pre-set 9696 * with NO SENSE so it is valid even if HBA could 9697 * not retrieve a real sense data. 9698 * Just copy this sense data into scsi pkt sense area. 9699 */ 9700 bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense, 9701 SATA_ATAPI_MIN_RQSENSE_LEN); 9702 #ifdef SATA_DEBUG 9703 if (sata_debug_flags & SATA_DBG_SCSI_IF) { 9704 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 9705 "sata_txlt_atapi_completion: %02x\n" 9706 "RQSENSE: %02x %02x %02x %02x %02x %02x " 9707 " %02x %02x %02x %02x %02x %02x " 9708 " %02x %02x %02x %02x %02x %02x\n", 9709 scsipkt->pkt_reason, 9710 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 9711 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 9712 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 9713 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 9714 rqsp[16], rqsp[17]); 9715 } 9716 #endif 9717 } else { 9718 switch (sata_pkt->satapkt_reason) { 9719 case SATA_PKT_PORT_ERROR: 9720 /* 9721 * We have no device data. 9722 */ 9723 scsipkt->pkt_reason = CMD_INCOMPLETE; 9724 scsipkt->pkt_state &= ~(STATE_GOT_BUS | 9725 STATE_GOT_TARGET | STATE_SENT_CMD | 9726 STATE_GOT_STATUS); 9727 sense->es_key = KEY_HARDWARE_ERROR; 9728 break; 9729 9730 case SATA_PKT_TIMEOUT: 9731 scsipkt->pkt_reason = CMD_TIMEOUT; 9732 scsipkt->pkt_statistics |= 9733 STAT_TIMEOUT | STAT_DEV_RESET; 9734 /* 9735 * Need to check if HARDWARE_ERROR/ 9736 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more 9737 * appropriate. 9738 */ 9739 break; 9740 9741 case SATA_PKT_ABORTED: 9742 scsipkt->pkt_reason = CMD_ABORTED; 9743 scsipkt->pkt_statistics |= STAT_ABORTED; 9744 /* Should we set key COMMAND_ABPRTED? */ 9745 break; 9746 9747 case SATA_PKT_RESET: 9748 scsipkt->pkt_reason = CMD_RESET; 9749 scsipkt->pkt_statistics |= STAT_DEV_RESET; 9750 /* 9751 * May be we should set Unit Attention / 9752 * Reset. Perhaps the same should be 9753 * returned for disks.... 9754 */ 9755 sense->es_key = KEY_UNIT_ATTENTION; 9756 sense->es_add_code = SD_SCSI_ASC_RESET; 9757 break; 9758 9759 default: 9760 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 9761 "sata_txlt_atapi_completion: " 9762 "invalid packet completion reason")); 9763 scsipkt->pkt_reason = CMD_TRAN_ERR; 9764 scsipkt->pkt_state &= ~(STATE_GOT_BUS | 9765 STATE_GOT_TARGET | STATE_SENT_CMD | 9766 STATE_GOT_STATUS); 9767 break; 9768 } 9769 } 9770 } 9771 9772 SATAATAPITRACE(spx, 0); 9773 9774 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 9775 scsipkt->pkt_comp != NULL) { 9776 /* scsi callback required */ 9777 (*scsipkt->pkt_comp)(scsipkt); 9778 } 9779 } 9780 9781 /* 9782 * Set up error retrieval sata command for ATAPI Packet Command error data 9783 * recovery. 9784 * 9785 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up, 9786 * returns SATA_FAILURE otherwise. 9787 */ 9788 9789 static int 9790 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo) 9791 { 9792 sata_pkt_t *spkt = spx->txlt_sata_pkt; 9793 sata_cmd_t *scmd; 9794 struct buf *bp; 9795 9796 /* 9797 * Allocate dma-able buffer error data. 9798 * Buffer allocation will take care of buffer alignment and other DMA 9799 * attributes. 9800 */ 9801 bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN); 9802 if (bp == NULL) { 9803 SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst, 9804 "sata_get_err_retrieval_pkt: " 9805 "cannot allocate buffer for error data", NULL); 9806 return (SATA_FAILURE); 9807 } 9808 bp_mapin(bp); /* make data buffer accessible */ 9809 9810 /* Operation modes are up to the caller */ 9811 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 9812 9813 /* Synchronous mode, no callback - may be changed by the caller */ 9814 spkt->satapkt_comp = NULL; 9815 spkt->satapkt_time = sata_default_pkt_time; 9816 9817 scmd = &spkt->satapkt_cmd; 9818 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 9819 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 9820 9821 sata_atapi_packet_cmd_setup(scmd, sdinfo); 9822 9823 /* 9824 * Set-up acdb. Request Sense CDB (packet command content) is 9825 * not in DMA-able buffer. Its handling is HBA-specific (how 9826 * it is transfered into packet FIS). 9827 */ 9828 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 9829 bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN); 9830 /* Following zeroing of pad bytes may not be necessary */ 9831 bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN], 9832 sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN); 9833 9834 /* 9835 * Set-up pointer to the buffer handle, so HBA can sync buffer 9836 * before accessing it. Handle is in usual place in translate struct. 9837 */ 9838 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle; 9839 9840 /* 9841 * Preset request sense data to NO SENSE. 9842 * Here it is redundant, only for a symetry with scsi-originated 9843 * packets. It should not be used for anything but debugging. 9844 */ 9845 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN); 9846 sata_fixed_sense_data_preset( 9847 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 9848 9849 ASSERT(scmd->satacmd_num_dma_cookies != 0); 9850 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 9851 9852 return (SATA_SUCCESS); 9853 } 9854 9855 /* 9856 * Set-up ATAPI packet command. 9857 * Data transfer direction has to be set-up in sata_cmd structure prior to 9858 * calling this function. 9859 * 9860 * Returns void 9861 */ 9862 9863 static void 9864 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo) 9865 { 9866 scmd->satacmd_addr_type = 0; /* N/A */ 9867 scmd->satacmd_sec_count_lsb = 0; /* no tag */ 9868 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 9869 scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ; 9870 scmd->satacmd_lba_high_lsb = 9871 (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8); 9872 scmd->satacmd_cmd_reg = SATAC_PACKET; /* Command */ 9873 9874 /* 9875 * We want all data to be transfered via DMA. 9876 * But specify it only if drive supports DMA and DMA mode is 9877 * selected - some drives are sensitive about it. 9878 * Hopefully it wil work for all drives.... 9879 */ 9880 if (sdinfo->satadrv_settings & SATA_DEV_DMA) 9881 scmd->satacmd_features_reg = SATA_ATAPI_F_DMA; 9882 9883 /* 9884 * Features register requires special care for devices that use 9885 * Serial ATA bridge - they need an explicit specification of 9886 * the data transfer direction for Packet DMA commands. 9887 * Setting this bit is harmless if DMA is not used. 9888 * 9889 * Many drives do not implement word 80, specifying what ATA/ATAPI 9890 * spec they follow. 9891 * We are arbitrarily following the latest SerialATA 2.6 spec, 9892 * which uses ATA/ATAPI 6 specification for Identify Data, unless 9893 * ATA/ATAPI-7 support is explicitly indicated. 9894 */ 9895 if (sdinfo->satadrv_id.ai_majorversion != 0 && 9896 sdinfo->satadrv_id.ai_majorversion != 0xffff && 9897 (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) { 9898 /* 9899 * Specification of major version is valid and version 7 9900 * is supported. It does automatically imply that all 9901 * spec features are supported. For now, we assume that 9902 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete. 9903 */ 9904 if ((sdinfo->satadrv_id.ai_dirdma & 9905 SATA_ATAPI_ID_DMADIR_REQ) != 0) { 9906 if (scmd->satacmd_flags.sata_data_direction == 9907 SATA_DIR_READ) { 9908 scmd->satacmd_features_reg |= 9909 SATA_ATAPI_F_DATA_DIR_READ; 9910 } 9911 } 9912 } 9913 } 9914 9915 9916 #ifdef SATA_DEBUG 9917 9918 /* Display 18 bytes of Inquiry data */ 9919 static void 9920 sata_show_inqry_data(uint8_t *buf) 9921 { 9922 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 9923 uint8_t *p; 9924 9925 cmn_err(CE_NOTE, "Inquiry data:"); 9926 cmn_err(CE_NOTE, "device type %x", inq->inq_dtype); 9927 cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb); 9928 cmn_err(CE_NOTE, "version %x", inq->inq_ansi); 9929 cmn_err(CE_NOTE, "ATAPI transport version %d", 9930 SATA_ATAPI_TRANS_VERSION(inq)); 9931 cmn_err(CE_NOTE, "response data format %d, aenc %d", 9932 inq->inq_rdf, inq->inq_aenc); 9933 cmn_err(CE_NOTE, " additional length %d", inq->inq_len); 9934 cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs); 9935 p = (uint8_t *)inq->inq_vid; 9936 cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x " 9937 "%02x %02x %02x %02x", 9938 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 9939 p = (uint8_t *)inq->inq_vid; 9940 cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c", 9941 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 9942 9943 p = (uint8_t *)inq->inq_pid; 9944 cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x " 9945 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", 9946 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 9947 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 9948 p = (uint8_t *)inq->inq_pid; 9949 cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c " 9950 "%c %c %c %c %c %c %c %c", 9951 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 9952 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 9953 9954 p = (uint8_t *)inq->inq_revision; 9955 cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x", 9956 p[0], p[1], p[2], p[3]); 9957 p = (uint8_t *)inq->inq_revision; 9958 cmn_err(CE_NOTE, "revision: %c %c %c %c", 9959 p[0], p[1], p[2], p[3]); 9960 9961 } 9962 9963 9964 static void 9965 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count) 9966 { 9967 struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt; 9968 9969 if (scsi_pkt == NULL) 9970 return; 9971 if (count != 0) { 9972 /* saving cdb */ 9973 bzero(sata_atapi_trace[sata_atapi_trace_index].acdb, 9974 SATA_ATAPI_MAX_CDB_LEN); 9975 bcopy(scsi_pkt->pkt_cdbp, 9976 sata_atapi_trace[sata_atapi_trace_index].acdb, count); 9977 } else { 9978 bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)-> 9979 sts_sensedata, 9980 sata_atapi_trace[sata_atapi_trace_index].arqs, 9981 SATA_ATAPI_MIN_RQSENSE_LEN); 9982 sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason = 9983 scsi_pkt->pkt_reason; 9984 sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason = 9985 spx->txlt_sata_pkt->satapkt_reason; 9986 9987 if (++sata_atapi_trace_index >= 64) 9988 sata_atapi_trace_index = 0; 9989 } 9990 } 9991 9992 #endif 9993 9994 /* 9995 * Fetch inquiry data from ATAPI device 9996 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 9997 * 9998 * Note: 9999 * inqb pointer does not point to a DMA-able buffer. It is a local buffer 10000 * where the caller expects to see the inquiry data. 10001 * 10002 */ 10003 10004 static int 10005 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba, 10006 sata_address_t *saddr, struct scsi_inquiry *inq) 10007 { 10008 sata_pkt_txlate_t *spx; 10009 sata_pkt_t *spkt; 10010 struct buf *bp; 10011 sata_drive_info_t *sdinfo; 10012 sata_cmd_t *scmd; 10013 int rval; 10014 uint8_t *rqsp; 10015 dev_info_t *dip = SATA_DIP(sata_hba); 10016 #ifdef SATA_DEBUG 10017 char msg_buf[MAXPATHLEN]; 10018 #endif 10019 kmutex_t *cport_mutex; 10020 10021 ASSERT(sata_hba != NULL); 10022 10023 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 10024 spx->txlt_sata_hba_inst = sata_hba; 10025 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 10026 spkt = sata_pkt_alloc(spx, NULL); 10027 if (spkt == NULL) { 10028 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10029 return (SATA_FAILURE); 10030 } 10031 /* address is needed now */ 10032 spkt->satapkt_device.satadev_addr = *saddr; 10033 10034 /* scsi_inquiry size buffer */ 10035 bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry)); 10036 if (bp == NULL) { 10037 sata_pkt_free(spx); 10038 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10039 SATA_LOG_D((sata_hba, CE_WARN, 10040 "sata_get_atapi_inquiry_data: " 10041 "cannot allocate data buffer")); 10042 return (SATA_FAILURE); 10043 } 10044 bp_mapin(bp); /* make data buffer accessible */ 10045 10046 scmd = &spkt->satapkt_cmd; 10047 ASSERT(scmd->satacmd_num_dma_cookies != 0); 10048 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 10049 10050 /* Use synchronous mode */ 10051 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 10052 spkt->satapkt_comp = NULL; 10053 spkt->satapkt_time = sata_default_pkt_time; 10054 10055 /* Issue inquiry command - 6 bytes cdb, data transfer, read */ 10056 10057 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 10058 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 10059 10060 cport_mutex = &(SATA_CPORT_MUTEX(sata_hba, saddr->cport)); 10061 mutex_enter(cport_mutex); 10062 sdinfo = sata_get_device_info(sata_hba, 10063 &spx->txlt_sata_pkt->satapkt_device); 10064 if (sdinfo == NULL) { 10065 /* we have to be carefull about the disapearing device */ 10066 mutex_exit(cport_mutex); 10067 rval = SATA_FAILURE; 10068 goto cleanup; 10069 } 10070 sata_atapi_packet_cmd_setup(scmd, sdinfo); 10071 10072 /* 10073 * Set-up acdb. This works for atapi transport version 2 and later. 10074 */ 10075 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 10076 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 10077 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */ 10078 scmd->satacmd_acdb[1] = 0x00; 10079 scmd->satacmd_acdb[2] = 0x00; 10080 scmd->satacmd_acdb[3] = 0x00; 10081 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry); 10082 scmd->satacmd_acdb[5] = 0x00; 10083 10084 sata_fixed_sense_data_preset( 10085 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 10086 10087 /* Transfer command to HBA */ 10088 if (sata_hba_start(spx, &rval) != 0) { 10089 /* Pkt not accepted for execution */ 10090 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10091 "sata_get_atapi_inquiry_data: " 10092 "Packet not accepted for execution - ret: %02x", rval); 10093 mutex_exit(cport_mutex); 10094 rval = SATA_FAILURE; 10095 goto cleanup; 10096 } 10097 mutex_exit(cport_mutex); 10098 10099 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) { 10100 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10101 "sata_get_atapi_inquiry_data: " 10102 "Packet completed successfully - ret: %02x", rval); 10103 if (spx->txlt_buf_dma_handle != NULL) { 10104 /* 10105 * Sync buffer. Handle is in usual place in translate 10106 * struct. 10107 */ 10108 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 10109 DDI_DMA_SYNC_FORCPU); 10110 ASSERT(rval == DDI_SUCCESS); 10111 } 10112 10113 if (sata_check_for_dma_error(dip, spx)) { 10114 ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED); 10115 rval = SATA_FAILURE; 10116 } else { 10117 /* 10118 * Normal completion - copy data into caller's buffer 10119 */ 10120 bcopy(bp->b_un.b_addr, (uint8_t *)inq, 10121 sizeof (struct scsi_inquiry)); 10122 #ifdef SATA_DEBUG 10123 if (sata_debug_flags & SATA_DBG_ATAPI) { 10124 sata_show_inqry_data((uint8_t *)inq); 10125 } 10126 #endif 10127 rval = SATA_SUCCESS; 10128 } 10129 } else { 10130 /* 10131 * Something went wrong - analyze return - check rqsense data 10132 */ 10133 rval = SATA_FAILURE; 10134 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 10135 /* 10136 * ARQ data hopefull show something other than NO SENSE 10137 */ 10138 rqsp = scmd->satacmd_rqsense; 10139 #ifdef SATA_DEBUG 10140 if (sata_debug_flags & SATA_DBG_ATAPI) { 10141 msg_buf[0] = '\0'; 10142 (void) snprintf(msg_buf, MAXPATHLEN, 10143 "ATAPI packet completion reason: %02x\n" 10144 "RQSENSE: %02x %02x %02x %02x %02x %02x\n" 10145 " %02x %02x %02x %02x %02x %02x\n" 10146 " %02x %02x %02x %02x %02x %02x", 10147 spkt->satapkt_reason, 10148 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 10149 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 10150 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 10151 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 10152 rqsp[16], rqsp[17]); 10153 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 10154 "%s", msg_buf); 10155 } 10156 #endif 10157 } else { 10158 switch (spkt->satapkt_reason) { 10159 case SATA_PKT_PORT_ERROR: 10160 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10161 "sata_get_atapi_inquiry_data: " 10162 "packet reason: port error", NULL); 10163 break; 10164 10165 case SATA_PKT_TIMEOUT: 10166 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10167 "sata_get_atapi_inquiry_data: " 10168 "packet reason: timeout", NULL); 10169 break; 10170 10171 case SATA_PKT_ABORTED: 10172 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10173 "sata_get_atapi_inquiry_data: " 10174 "packet reason: aborted", NULL); 10175 break; 10176 10177 case SATA_PKT_RESET: 10178 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10179 "sata_get_atapi_inquiry_data: " 10180 "packet reason: reset\n", NULL); 10181 break; 10182 default: 10183 SATADBG1(SATA_DBG_ATAPI, sata_hba, 10184 "sata_get_atapi_inquiry_data: " 10185 "invalid packet reason: %02x\n", 10186 spkt->satapkt_reason); 10187 break; 10188 } 10189 } 10190 } 10191 cleanup: 10192 sata_free_local_buffer(spx); 10193 sata_pkt_free(spx); 10194 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10195 return (rval); 10196 } 10197 10198 10199 10200 10201 10202 #if 0 10203 #ifdef SATA_DEBUG 10204 10205 /* 10206 * Test ATAPI packet command. 10207 * Single threaded test: send packet command in synch mode, process completion 10208 * 10209 */ 10210 static void 10211 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport) 10212 { 10213 sata_pkt_txlate_t *spx; 10214 sata_pkt_t *spkt; 10215 struct buf *bp; 10216 sata_device_t sata_device; 10217 sata_drive_info_t *sdinfo; 10218 sata_cmd_t *scmd; 10219 int rval; 10220 uint8_t *rqsp; 10221 10222 ASSERT(sata_hba_inst != NULL); 10223 sata_device.satadev_addr.cport = cport; 10224 sata_device.satadev_addr.pmport = 0; 10225 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 10226 sata_device.satadev_rev = SATA_DEVICE_REV; 10227 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 10228 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 10229 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 10230 if (sdinfo == NULL) { 10231 sata_log(sata_hba_inst, CE_WARN, 10232 "sata_test_atapi_packet_command: " 10233 "no device info for cport %d", 10234 sata_device.satadev_addr.cport); 10235 return; 10236 } 10237 10238 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 10239 spx->txlt_sata_hba_inst = sata_hba_inst; 10240 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 10241 spkt = sata_pkt_alloc(spx, NULL); 10242 if (spkt == NULL) { 10243 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10244 return; 10245 } 10246 /* address is needed now */ 10247 spkt->satapkt_device.satadev_addr = sata_device.satadev_addr; 10248 10249 /* 1024k buffer */ 10250 bp = sata_alloc_local_buffer(spx, 1024); 10251 if (bp == NULL) { 10252 sata_pkt_free(spx); 10253 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10254 sata_log(sata_hba_inst, CE_WARN, 10255 "sata_test_atapi_packet_command: " 10256 "cannot allocate data buffer"); 10257 return; 10258 } 10259 bp_mapin(bp); /* make data buffer accessible */ 10260 10261 scmd = &spkt->satapkt_cmd; 10262 ASSERT(scmd->satacmd_num_dma_cookies != 0); 10263 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 10264 10265 /* Use synchronous mode */ 10266 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 10267 10268 /* Synchronous mode, no callback - may be changed by the caller */ 10269 spkt->satapkt_comp = NULL; 10270 spkt->satapkt_time = sata_default_pkt_time; 10271 10272 /* Issue inquiry command - 6 bytes cdb, data transfer, read */ 10273 10274 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 10275 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 10276 10277 sata_atapi_packet_cmd_setup(scmd, sdinfo); 10278 10279 /* Set-up acdb. */ 10280 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 10281 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 10282 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */ 10283 scmd->satacmd_acdb[1] = 0x00; 10284 scmd->satacmd_acdb[2] = 0x00; 10285 scmd->satacmd_acdb[3] = 0x00; 10286 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry); 10287 scmd->satacmd_acdb[5] = 0x00; 10288 10289 sata_fixed_sense_data_preset( 10290 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 10291 10292 /* Transfer command to HBA */ 10293 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 10294 if (sata_hba_start(spx, &rval) != 0) { 10295 /* Pkt not accepted for execution */ 10296 sata_log(sata_hba_inst, CE_WARN, 10297 "sata_test_atapi_packet_command: " 10298 "Packet not accepted for execution - ret: %02x", rval); 10299 mutex_exit( 10300 &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 10301 goto cleanup; 10302 } 10303 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 10304 10305 if (spx->txlt_buf_dma_handle != NULL) { 10306 /* 10307 * Sync buffer. Handle is in usual place in translate struct. 10308 */ 10309 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 10310 DDI_DMA_SYNC_FORCPU); 10311 ASSERT(rval == DDI_SUCCESS); 10312 } 10313 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) { 10314 sata_log(sata_hba_inst, CE_WARN, 10315 "sata_test_atapi_packet_command: " 10316 "Packet completed successfully"); 10317 /* 10318 * Normal completion - show inquiry data 10319 */ 10320 sata_show_inqry_data((uint8_t *)bp->b_un.b_addr); 10321 } else { 10322 /* 10323 * Something went wrong - analyze return - check rqsense data 10324 */ 10325 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 10326 /* 10327 * ARQ data hopefull show something other than NO SENSE 10328 */ 10329 rqsp = scmd->satacmd_rqsense; 10330 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 10331 "ATAPI packet completion reason: %02x\n" 10332 "RQSENSE: %02x %02x %02x %02x %02x %02x " 10333 " %02x %02x %02x %02x %02x %02x " 10334 " %02x %02x %02x %02x %02x %02x\n", 10335 spkt->satapkt_reason, 10336 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 10337 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 10338 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 10339 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 10340 rqsp[16], rqsp[17]); 10341 } else { 10342 switch (spkt->satapkt_reason) { 10343 case SATA_PKT_PORT_ERROR: 10344 sata_log(sata_hba_inst, CE_WARN, 10345 "sata_test_atapi_packet_command: " 10346 "packet reason: port error\n"); 10347 break; 10348 10349 case SATA_PKT_TIMEOUT: 10350 sata_log(sata_hba_inst, CE_WARN, 10351 "sata_test_atapi_packet_command: " 10352 "packet reason: timeout\n"); 10353 break; 10354 10355 case SATA_PKT_ABORTED: 10356 sata_log(sata_hba_inst, CE_WARN, 10357 "sata_test_atapi_packet_command: " 10358 "packet reason: aborted\n"); 10359 break; 10360 10361 case SATA_PKT_RESET: 10362 sata_log(sata_hba_inst, CE_WARN, 10363 "sata_test_atapi_packet_command: " 10364 "packet reason: reset\n"); 10365 break; 10366 default: 10367 sata_log(sata_hba_inst, CE_WARN, 10368 "sata_test_atapi_packet_command: " 10369 "invalid packet reason: %02x\n", 10370 spkt->satapkt_reason); 10371 break; 10372 } 10373 } 10374 } 10375 cleanup: 10376 sata_free_local_buffer(spx); 10377 sata_pkt_free(spx); 10378 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10379 } 10380 10381 #endif /* SATA_DEBUG */ 10382 #endif /* 1 */ 10383 10384 10385 /* ************************** LOCAL HELPER FUNCTIONS *********************** */ 10386 10387 /* 10388 * Validate sata_tran info 10389 * SATA_FAILURE returns if structure is inconsistent or structure revision 10390 * does not match one used by the framework. 10391 * 10392 * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains 10393 * required function pointers. 10394 * Returns SATA_FAILURE otherwise. 10395 */ 10396 static int 10397 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran) 10398 { 10399 /* 10400 * SATA_TRAN_HBA_REV is the current (highest) revision number 10401 * of the SATA interface. 10402 */ 10403 if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) { 10404 sata_log(NULL, CE_WARN, 10405 "sata: invalid sata_hba_tran version %d for driver %s", 10406 sata_tran->sata_tran_hba_rev, ddi_driver_name(dip)); 10407 return (SATA_FAILURE); 10408 } 10409 10410 if (dip != sata_tran->sata_tran_hba_dip) { 10411 SATA_LOG_D((NULL, CE_WARN, 10412 "sata: inconsistent sata_tran_hba_dip " 10413 "%p / %p", sata_tran->sata_tran_hba_dip, dip)); 10414 return (SATA_FAILURE); 10415 } 10416 10417 if (sata_tran->sata_tran_probe_port == NULL || 10418 sata_tran->sata_tran_start == NULL || 10419 sata_tran->sata_tran_abort == NULL || 10420 sata_tran->sata_tran_reset_dport == NULL || 10421 sata_tran->sata_tran_hotplug_ops == NULL || 10422 sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL || 10423 sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate == 10424 NULL) { 10425 SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing " 10426 "required functions")); 10427 } 10428 return (SATA_SUCCESS); 10429 } 10430 10431 /* 10432 * Remove HBA instance from sata_hba_list. 10433 */ 10434 static void 10435 sata_remove_hba_instance(dev_info_t *dip) 10436 { 10437 sata_hba_inst_t *sata_hba_inst; 10438 10439 mutex_enter(&sata_mutex); 10440 for (sata_hba_inst = sata_hba_list; 10441 sata_hba_inst != (struct sata_hba_inst *)NULL; 10442 sata_hba_inst = sata_hba_inst->satahba_next) { 10443 if (sata_hba_inst->satahba_dip == dip) 10444 break; 10445 } 10446 10447 if (sata_hba_inst == (struct sata_hba_inst *)NULL) { 10448 #ifdef SATA_DEBUG 10449 cmn_err(CE_WARN, "sata_remove_hba_instance: " 10450 "unknown HBA instance\n"); 10451 #endif 10452 ASSERT(FALSE); 10453 } 10454 if (sata_hba_inst == sata_hba_list) { 10455 sata_hba_list = sata_hba_inst->satahba_next; 10456 if (sata_hba_list) { 10457 sata_hba_list->satahba_prev = 10458 (struct sata_hba_inst *)NULL; 10459 } 10460 if (sata_hba_inst == sata_hba_list_tail) { 10461 sata_hba_list_tail = NULL; 10462 } 10463 } else if (sata_hba_inst == sata_hba_list_tail) { 10464 sata_hba_list_tail = sata_hba_inst->satahba_prev; 10465 if (sata_hba_list_tail) { 10466 sata_hba_list_tail->satahba_next = 10467 (struct sata_hba_inst *)NULL; 10468 } 10469 } else { 10470 sata_hba_inst->satahba_prev->satahba_next = 10471 sata_hba_inst->satahba_next; 10472 sata_hba_inst->satahba_next->satahba_prev = 10473 sata_hba_inst->satahba_prev; 10474 } 10475 mutex_exit(&sata_mutex); 10476 } 10477 10478 /* 10479 * Probe all SATA ports of the specified HBA instance. 10480 * The assumption is that there are no target and attachment point minor nodes 10481 * created by the boot subsystems, so we do not need to prune device tree. 10482 * 10483 * This function is called only from sata_hba_attach(). It does not have to 10484 * be protected by controller mutex, because the hba_attached flag is not set 10485 * yet and no one would be touching this HBA instance other than this thread. 10486 * Determines if port is active and what type of the device is attached 10487 * (if any). Allocates necessary structures for each port. 10488 * 10489 * An AP (Attachement Point) node is created for each SATA device port even 10490 * when there is no device attached. 10491 */ 10492 10493 static void 10494 sata_probe_ports(sata_hba_inst_t *sata_hba_inst) 10495 { 10496 dev_info_t *dip = SATA_DIP(sata_hba_inst); 10497 int ncport; 10498 sata_cport_info_t *cportinfo; 10499 sata_drive_info_t *drive; 10500 sata_device_t sata_device; 10501 int rval; 10502 dev_t minor_number; 10503 char name[16]; 10504 clock_t start_time, cur_time; 10505 10506 /* 10507 * Probe controller ports first, to find port status and 10508 * any port multiplier attached. 10509 */ 10510 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 10511 /* allocate cport structure */ 10512 cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP); 10513 ASSERT(cportinfo != NULL); 10514 mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL); 10515 10516 mutex_enter(&cportinfo->cport_mutex); 10517 10518 cportinfo->cport_addr.cport = ncport; 10519 cportinfo->cport_addr.pmport = 0; 10520 cportinfo->cport_addr.qual = SATA_ADDR_CPORT; 10521 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 10522 cportinfo->cport_state |= SATA_STATE_PROBING; 10523 SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo; 10524 10525 /* 10526 * Regardless if a port is usable or not, create 10527 * an attachment point 10528 */ 10529 mutex_exit(&cportinfo->cport_mutex); 10530 minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip), 10531 ncport, 0, SATA_ADDR_CPORT); 10532 (void) sprintf(name, "%d", ncport); 10533 if (ddi_create_minor_node(dip, name, S_IFCHR, 10534 minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) != 10535 DDI_SUCCESS) { 10536 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: " 10537 "cannot create SATA attachment point for port %d", 10538 ncport); 10539 } 10540 10541 /* Probe port */ 10542 start_time = ddi_get_lbolt(); 10543 reprobe_cport: 10544 sata_device.satadev_addr.cport = ncport; 10545 sata_device.satadev_addr.pmport = 0; 10546 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 10547 sata_device.satadev_rev = SATA_DEVICE_REV; 10548 10549 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 10550 (dip, &sata_device); 10551 10552 mutex_enter(&cportinfo->cport_mutex); 10553 cportinfo->cport_scr = sata_device.satadev_scr; 10554 if (rval != SATA_SUCCESS) { 10555 /* Something went wrong? Fail the port */ 10556 cportinfo->cport_state = SATA_PSTATE_FAILED; 10557 mutex_exit(&cportinfo->cport_mutex); 10558 continue; 10559 } 10560 cportinfo->cport_state &= ~SATA_STATE_PROBING; 10561 cportinfo->cport_state |= SATA_STATE_PROBED; 10562 cportinfo->cport_dev_type = sata_device.satadev_type; 10563 10564 cportinfo->cport_state |= SATA_STATE_READY; 10565 if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) { 10566 mutex_exit(&cportinfo->cport_mutex); 10567 continue; 10568 } 10569 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 10570 /* 10571 * There is some device attached. 10572 * Allocate device info structure 10573 */ 10574 if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) { 10575 mutex_exit(&cportinfo->cport_mutex); 10576 SATA_CPORTINFO_DRV_INFO(cportinfo) = 10577 kmem_zalloc(sizeof (sata_drive_info_t), 10578 KM_SLEEP); 10579 mutex_enter(&cportinfo->cport_mutex); 10580 } 10581 drive = SATA_CPORTINFO_DRV_INFO(cportinfo); 10582 drive->satadrv_addr = cportinfo->cport_addr; 10583 drive->satadrv_addr.qual = SATA_ADDR_DCPORT; 10584 drive->satadrv_type = cportinfo->cport_dev_type; 10585 drive->satadrv_state = SATA_STATE_UNKNOWN; 10586 10587 mutex_exit(&cportinfo->cport_mutex); 10588 if (sata_add_device(dip, sata_hba_inst, &sata_device) != 10589 SATA_SUCCESS) { 10590 /* 10591 * Plugged device was not correctly identified. 10592 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT 10593 */ 10594 cur_time = ddi_get_lbolt(); 10595 if ((cur_time - start_time) < 10596 drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) { 10597 /* sleep for a while */ 10598 delay(drv_usectohz( 10599 SATA_DEV_RETRY_DLY)); 10600 goto reprobe_cport; 10601 } 10602 } 10603 } else { /* SATA_DTYPE_PMULT */ 10604 mutex_exit(&cportinfo->cport_mutex); 10605 10606 /* Allocate sata_pmult_info and sata_pmport_info */ 10607 if (sata_alloc_pmult(sata_hba_inst, &sata_device) != 10608 SATA_SUCCESS) 10609 continue; 10610 10611 /* Log the information of the port multiplier */ 10612 sata_show_pmult_info(sata_hba_inst, &sata_device); 10613 10614 /* Probe its pmports */ 10615 sata_probe_pmports(sata_hba_inst, ncport); 10616 } 10617 } 10618 } 10619 10620 /* 10621 * Probe all device ports behind a port multiplier. 10622 * 10623 * PMult-related structure should be allocated before by sata_alloc_pmult(). 10624 * 10625 * NOTE1: Only called from sata_probe_ports() 10626 * NOTE2: No mutex should be hold. 10627 */ 10628 static void 10629 sata_probe_pmports(sata_hba_inst_t *sata_hba_inst, uint8_t ncport) 10630 { 10631 dev_info_t *dip = SATA_DIP(sata_hba_inst); 10632 sata_pmult_info_t *pmultinfo = NULL; 10633 sata_pmport_info_t *pmportinfo = NULL; 10634 sata_drive_info_t *drive = NULL; 10635 sata_device_t sata_device; 10636 10637 clock_t start_time, cur_time; 10638 int npmport; 10639 int rval; 10640 10641 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, ncport); 10642 10643 /* Probe Port Multiplier ports */ 10644 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; npmport++) { 10645 pmportinfo = pmultinfo->pmult_dev_port[npmport]; 10646 start_time = ddi_get_lbolt(); 10647 reprobe_pmport: 10648 sata_device.satadev_addr.cport = ncport; 10649 sata_device.satadev_addr.pmport = npmport; 10650 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT; 10651 sata_device.satadev_rev = SATA_DEVICE_REV; 10652 10653 /* Let HBA driver probe it. */ 10654 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 10655 (dip, &sata_device); 10656 mutex_enter(&pmportinfo->pmport_mutex); 10657 10658 pmportinfo->pmport_scr = sata_device.satadev_scr; 10659 10660 if (rval != SATA_SUCCESS) { 10661 pmportinfo->pmport_state = 10662 SATA_PSTATE_FAILED; 10663 mutex_exit(&pmportinfo->pmport_mutex); 10664 continue; 10665 } 10666 pmportinfo->pmport_state &= ~SATA_STATE_PROBING; 10667 pmportinfo->pmport_state |= SATA_STATE_PROBED; 10668 pmportinfo->pmport_dev_type = sata_device.satadev_type; 10669 10670 pmportinfo->pmport_state |= SATA_STATE_READY; 10671 if (pmportinfo->pmport_dev_type == 10672 SATA_DTYPE_NONE) { 10673 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 10674 "no device found at port %d:%d", ncport, npmport); 10675 mutex_exit(&pmportinfo->pmport_mutex); 10676 continue; 10677 } 10678 /* Port multipliers cannot be chained */ 10679 ASSERT(pmportinfo->pmport_dev_type != SATA_DTYPE_PMULT); 10680 /* 10681 * There is something attached to Port 10682 * Multiplier device port 10683 * Allocate device info structure 10684 */ 10685 if (pmportinfo->pmport_sata_drive == NULL) { 10686 mutex_exit(&pmportinfo->pmport_mutex); 10687 pmportinfo->pmport_sata_drive = 10688 kmem_zalloc(sizeof (sata_drive_info_t), KM_SLEEP); 10689 mutex_enter(&pmportinfo->pmport_mutex); 10690 } 10691 drive = pmportinfo->pmport_sata_drive; 10692 drive->satadrv_addr.cport = pmportinfo->pmport_addr.cport; 10693 drive->satadrv_addr.pmport = npmport; 10694 drive->satadrv_addr.qual = SATA_ADDR_DPMPORT; 10695 drive->satadrv_type = pmportinfo-> pmport_dev_type; 10696 drive->satadrv_state = SATA_STATE_UNKNOWN; 10697 10698 mutex_exit(&pmportinfo->pmport_mutex); 10699 rval = sata_add_device(dip, sata_hba_inst, &sata_device); 10700 10701 if (rval != SATA_SUCCESS) { 10702 /* 10703 * Plugged device was not correctly identified. 10704 * Retry, within the SATA_DEV_IDENTIFY_TIMEOUT 10705 */ 10706 cur_time = ddi_get_lbolt(); 10707 if ((cur_time - start_time) < drv_usectohz( 10708 SATA_DEV_IDENTIFY_TIMEOUT)) { 10709 /* sleep for a while */ 10710 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 10711 goto reprobe_pmport; 10712 } 10713 } 10714 } 10715 } 10716 10717 /* 10718 * Add SATA device for specified HBA instance & port (SCSI target 10719 * device nodes). 10720 * This function is called (indirectly) only from sata_hba_attach(). 10721 * A target node is created when there is a supported type device attached, 10722 * but may be removed if it cannot be put online. 10723 * 10724 * This function cannot be called from an interrupt context. 10725 * 10726 * Create target nodes for disk, CD/DVD, Tape and ATAPI disk devices 10727 * 10728 * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when 10729 * device identification failed - adding a device could be retried. 10730 * 10731 */ 10732 static int 10733 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, 10734 sata_device_t *sata_device) 10735 { 10736 sata_cport_info_t *cportinfo; 10737 sata_pmult_info_t *pminfo; 10738 sata_pmport_info_t *pmportinfo; 10739 dev_info_t *cdip; /* child dip */ 10740 sata_address_t *saddr = &sata_device->satadev_addr; 10741 uint8_t cport, pmport; 10742 int rval; 10743 10744 cport = saddr->cport; 10745 pmport = saddr->pmport; 10746 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 10747 ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE); 10748 10749 /* 10750 * Some device is attached to a controller port. 10751 * We rely on controllers distinquishing between no-device, 10752 * attached port multiplier and other kind of attached device. 10753 * We need to get Identify Device data and determine 10754 * positively the dev type before trying to attach 10755 * the target driver. 10756 */ 10757 sata_device->satadev_rev = SATA_DEVICE_REV; 10758 switch (saddr->qual) { 10759 case SATA_ADDR_CPORT: 10760 /* 10761 * Add a non-port-multiplier device at controller port. 10762 */ 10763 saddr->qual = SATA_ADDR_DCPORT; 10764 10765 rval = sata_probe_device(sata_hba_inst, sata_device); 10766 if (rval != SATA_SUCCESS || 10767 sata_device->satadev_type == SATA_DTYPE_UNKNOWN) 10768 return (SATA_FAILURE); 10769 10770 mutex_enter(&cportinfo->cport_mutex); 10771 sata_show_drive_info(sata_hba_inst, 10772 SATA_CPORTINFO_DRV_INFO(cportinfo)); 10773 10774 if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) { 10775 /* 10776 * Could not determine device type or 10777 * a device is not supported. 10778 * Degrade this device to unknown. 10779 */ 10780 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 10781 mutex_exit(&cportinfo->cport_mutex); 10782 return (SATA_SUCCESS); 10783 } 10784 cportinfo->cport_dev_type = sata_device->satadev_type; 10785 cportinfo->cport_tgtnode_clean = B_TRUE; 10786 mutex_exit(&cportinfo->cport_mutex); 10787 10788 /* 10789 * Initialize device to the desired state. Even if it 10790 * fails, the device will still attach but syslog 10791 * will show the warning. 10792 */ 10793 if (sata_initialize_device(sata_hba_inst, 10794 SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) { 10795 /* Retry */ 10796 rval = sata_initialize_device(sata_hba_inst, 10797 SATA_CPORTINFO_DRV_INFO(cportinfo)); 10798 10799 if (rval == SATA_RETRY) 10800 sata_log(sata_hba_inst, CE_WARN, 10801 "SATA device at port %d - " 10802 "default device features could not be set." 10803 " Device may not operate as expected.", 10804 cport); 10805 } 10806 10807 cdip = sata_create_target_node(pdip, sata_hba_inst, saddr); 10808 if (cdip == NULL) { 10809 /* 10810 * Attaching target node failed. 10811 * We retain sata_drive_info structure... 10812 */ 10813 return (SATA_SUCCESS); 10814 } 10815 10816 mutex_enter(&cportinfo->cport_mutex); 10817 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 10818 satadrv_state = SATA_STATE_READY; 10819 mutex_exit(&cportinfo->cport_mutex); 10820 10821 break; 10822 10823 case SATA_ADDR_PMPORT: 10824 saddr->qual = SATA_ADDR_DPMPORT; 10825 10826 mutex_enter(&cportinfo->cport_mutex); 10827 /* It must be a Port Multiplier at the controller port */ 10828 ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT); 10829 10830 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 10831 pmportinfo = pminfo->pmult_dev_port[saddr->pmport]; 10832 mutex_exit(&cportinfo->cport_mutex); 10833 10834 rval = sata_probe_device(sata_hba_inst, sata_device); 10835 if (rval != SATA_SUCCESS || 10836 sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 10837 return (SATA_FAILURE); 10838 } 10839 10840 mutex_enter(&pmportinfo->pmport_mutex); 10841 sata_show_drive_info(sata_hba_inst, 10842 SATA_PMPORTINFO_DRV_INFO(pmportinfo)); 10843 10844 if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) { 10845 /* 10846 * Could not determine device type. 10847 * Degrade this device to unknown. 10848 */ 10849 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN; 10850 mutex_exit(&pmportinfo->pmport_mutex); 10851 return (SATA_SUCCESS); 10852 } 10853 pmportinfo->pmport_dev_type = sata_device->satadev_type; 10854 pmportinfo->pmport_tgtnode_clean = B_TRUE; 10855 mutex_exit(&pmportinfo->pmport_mutex); 10856 10857 /* 10858 * Initialize device to the desired state. 10859 * Even if it fails, the device will still 10860 * attach but syslog will show the warning. 10861 */ 10862 if (sata_initialize_device(sata_hba_inst, 10863 pmportinfo->pmport_sata_drive) != SATA_SUCCESS) { 10864 /* Retry */ 10865 rval = sata_initialize_device(sata_hba_inst, 10866 pmportinfo->pmport_sata_drive); 10867 10868 if (rval == SATA_RETRY) 10869 sata_log(sata_hba_inst, CE_WARN, 10870 "SATA device at port %d:%d - " 10871 "default device features could not be set." 10872 " Device may not operate as expected.", 10873 cport, pmport); 10874 } 10875 10876 cdip = sata_create_target_node(pdip, sata_hba_inst, saddr); 10877 if (cdip == NULL) { 10878 /* 10879 * Attaching target node failed. 10880 * We retain sata_drive_info structure... 10881 */ 10882 return (SATA_SUCCESS); 10883 } 10884 mutex_enter(&pmportinfo->pmport_mutex); 10885 pmportinfo->pmport_sata_drive->satadrv_state |= 10886 SATA_STATE_READY; 10887 mutex_exit(&pmportinfo->pmport_mutex); 10888 10889 break; 10890 10891 default: 10892 return (SATA_FAILURE); 10893 } 10894 10895 return (SATA_SUCCESS); 10896 } 10897 10898 /* 10899 * Clean up target node at specific address. 10900 * 10901 * NOTE: No Mutex should be hold. 10902 */ 10903 static int 10904 sata_offline_device(sata_hba_inst_t *sata_hba_inst, 10905 sata_device_t *sata_device, sata_drive_info_t *sdinfo) 10906 { 10907 uint8_t cport, pmport, qual; 10908 dev_info_t *tdip; 10909 10910 cport = sata_device->satadev_addr.cport; 10911 pmport = sata_device->satadev_addr.pmport; 10912 qual = sata_device->satadev_addr.qual; 10913 10914 if (qual == SATA_ADDR_DCPORT) { 10915 SATA_LOG_D((sata_hba_inst, CE_WARN, 10916 "sata_hba_ioctl: disconnect device at port %d", cport)); 10917 } else { 10918 SATA_LOG_D((sata_hba_inst, CE_WARN, 10919 "sata_hba_ioctl: disconnect device at port %d:%d", 10920 cport, pmport)); 10921 } 10922 10923 /* We are addressing attached device, not a port */ 10924 sata_device->satadev_addr.qual = 10925 sdinfo->satadrv_addr.qual; 10926 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 10927 &sata_device->satadev_addr); 10928 if (tdip != NULL && ndi_devi_offline(tdip, 10929 NDI_DEVI_REMOVE) != NDI_SUCCESS) { 10930 /* 10931 * Problem : 10932 * The target node remained attached. 10933 * This happens when the device file was open 10934 * or a node was waiting for resources. 10935 * Cannot do anything about it. 10936 */ 10937 if (qual == SATA_ADDR_DCPORT) { 10938 SATA_LOG_D((sata_hba_inst, CE_WARN, 10939 "sata_hba_ioctl: disconnect: could " 10940 "not unconfigure device before " 10941 "disconnecting the SATA port %d", 10942 cport)); 10943 } else { 10944 SATA_LOG_D((sata_hba_inst, CE_WARN, 10945 "sata_hba_ioctl: disconnect: could " 10946 "not unconfigure device before " 10947 "disconnecting the SATA port %d:%d", 10948 cport, pmport)); 10949 } 10950 /* 10951 * Set DEVICE REMOVED state in the target 10952 * node. It will prevent access to the device 10953 * even when a new device is attached, until 10954 * the old target node is released, removed and 10955 * recreated for a new device. 10956 */ 10957 sata_set_device_removed(tdip); 10958 10959 /* 10960 * Instruct event daemon to try the target 10961 * node cleanup later. 10962 */ 10963 sata_set_target_node_cleanup( 10964 sata_hba_inst, &sata_device->satadev_addr); 10965 } 10966 10967 10968 return (SATA_SUCCESS); 10969 } 10970 10971 10972 /* 10973 * Create scsi target node for attached device, create node properties and 10974 * attach the node. 10975 * The node could be removed if the device onlining fails. 10976 * 10977 * A dev_info_t pointer is returned if operation is successful, NULL is 10978 * returned otherwise. 10979 */ 10980 10981 static dev_info_t * 10982 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst, 10983 sata_address_t *sata_addr) 10984 { 10985 dev_info_t *cdip = NULL; 10986 int rval; 10987 char *nname = NULL; 10988 char **compatible = NULL; 10989 int ncompatible; 10990 struct scsi_inquiry inq; 10991 sata_device_t sata_device; 10992 sata_drive_info_t *sdinfo; 10993 int target; 10994 int i; 10995 10996 sata_device.satadev_rev = SATA_DEVICE_REV; 10997 sata_device.satadev_addr = *sata_addr; 10998 10999 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport))); 11000 11001 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 11002 11003 target = SATA_TO_SCSI_TARGET(sata_addr->cport, 11004 sata_addr->pmport, sata_addr->qual); 11005 11006 if (sdinfo == NULL) { 11007 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 11008 sata_addr->cport))); 11009 SATA_LOG_D((sata_hba_inst, CE_WARN, 11010 "sata_create_target_node: no sdinfo for target %x", 11011 target)); 11012 return (NULL); 11013 } 11014 11015 /* 11016 * create or get scsi inquiry data, expected by 11017 * scsi_hba_nodename_compatible_get() 11018 * SATA hard disks get Identify Data translated into Inguiry Data. 11019 * ATAPI devices respond directly to Inquiry request. 11020 */ 11021 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 11022 sata_identdev_to_inquiry(sata_hba_inst, sdinfo, 11023 (uint8_t *)&inq); 11024 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 11025 sata_addr->cport))); 11026 } else { /* Assume supported ATAPI device */ 11027 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 11028 sata_addr->cport))); 11029 if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr, 11030 &inq) == SATA_FAILURE) 11031 return (NULL); 11032 /* 11033 * Save supported ATAPI transport version 11034 */ 11035 sdinfo->satadrv_atapi_trans_ver = 11036 SATA_ATAPI_TRANS_VERSION(&inq); 11037 } 11038 11039 /* determine the node name and compatible */ 11040 scsi_hba_nodename_compatible_get(&inq, NULL, 11041 inq.inq_dtype, NULL, &nname, &compatible, &ncompatible); 11042 11043 #ifdef SATA_DEBUG 11044 if (sata_debug_flags & SATA_DBG_NODES) { 11045 if (nname == NULL) { 11046 cmn_err(CE_NOTE, "sata_create_target_node: " 11047 "cannot determine nodename for target %d\n", 11048 target); 11049 } else { 11050 cmn_err(CE_WARN, "sata_create_target_node: " 11051 "target %d nodename: %s\n", target, nname); 11052 } 11053 if (compatible == NULL) { 11054 cmn_err(CE_WARN, 11055 "sata_create_target_node: no compatible name\n"); 11056 } else { 11057 for (i = 0; i < ncompatible; i++) { 11058 cmn_err(CE_WARN, "sata_create_target_node: " 11059 "compatible name: %s\n", compatible[i]); 11060 } 11061 } 11062 } 11063 #endif 11064 11065 /* if nodename can't be determined, log error and exit */ 11066 if (nname == NULL) { 11067 SATA_LOG_D((sata_hba_inst, CE_WARN, 11068 "sata_create_target_node: cannot determine nodename " 11069 "for target %d\n", target)); 11070 scsi_hba_nodename_compatible_free(nname, compatible); 11071 return (NULL); 11072 } 11073 /* 11074 * Create scsi target node 11075 */ 11076 ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip); 11077 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 11078 "device-type", "scsi"); 11079 11080 if (rval != DDI_PROP_SUCCESS) { 11081 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 11082 "updating device_type prop failed %d", rval)); 11083 goto fail; 11084 } 11085 11086 /* 11087 * Create target node properties: target & lun 11088 */ 11089 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target); 11090 if (rval != DDI_PROP_SUCCESS) { 11091 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 11092 "updating target prop failed %d", rval)); 11093 goto fail; 11094 } 11095 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0); 11096 if (rval != DDI_PROP_SUCCESS) { 11097 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 11098 "updating target prop failed %d", rval)); 11099 goto fail; 11100 } 11101 11102 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 11103 /* 11104 * Add "variant" property 11105 */ 11106 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 11107 "variant", "atapi"); 11108 if (rval != DDI_PROP_SUCCESS) { 11109 SATA_LOG_D((sata_hba_inst, CE_WARN, 11110 "sata_create_target_node: variant atapi " 11111 "property could not be created: %d", rval)); 11112 goto fail; 11113 } 11114 } 11115 /* decorate the node with compatible */ 11116 if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible", 11117 compatible, ncompatible) != DDI_PROP_SUCCESS) { 11118 SATA_LOG_D((sata_hba_inst, CE_WARN, 11119 "sata_create_target_node: FAIL compatible props cdip 0x%p", 11120 (void *)cdip)); 11121 goto fail; 11122 } 11123 11124 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 11125 /* 11126 * Add "sata-phy" property 11127 */ 11128 if (ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "sata-phy", 11129 (int)sata_addr->cport) != DDI_PROP_SUCCESS) { 11130 SATA_LOG_D((sata_hba_inst, CE_WARN, 11131 "sata_create_target_node: failed to create " 11132 "\"sata-phy\" property: port %d", 11133 sata_addr->cport)); 11134 } 11135 } 11136 11137 11138 /* 11139 * Now, try to attach the driver. If probing of the device fails, 11140 * the target node may be removed 11141 */ 11142 rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH); 11143 11144 scsi_hba_nodename_compatible_free(nname, compatible); 11145 11146 if (rval == NDI_SUCCESS) 11147 return (cdip); 11148 11149 /* target node was removed - are we sure? */ 11150 return (NULL); 11151 11152 fail: 11153 scsi_hba_nodename_compatible_free(nname, compatible); 11154 ddi_prop_remove_all(cdip); 11155 rval = ndi_devi_free(cdip); 11156 if (rval != NDI_SUCCESS) { 11157 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 11158 "node removal failed %d", rval)); 11159 } 11160 sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: " 11161 "cannot create target node for SATA device at port %d", 11162 sata_addr->cport); 11163 return (NULL); 11164 } 11165 11166 /* 11167 * Remove a target node. 11168 */ 11169 static void 11170 sata_remove_target_node(sata_hba_inst_t *sata_hba_inst, 11171 sata_address_t *sata_addr) 11172 { 11173 dev_info_t *tdip; 11174 uint8_t cport = sata_addr->cport; 11175 uint8_t pmport = sata_addr->pmport; 11176 uint8_t qual = sata_addr->qual; 11177 11178 /* Note the sata daemon uses the address of the port/pmport */ 11179 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT); 11180 11181 /* Remove target node */ 11182 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), cport, pmport); 11183 if (tdip != NULL) { 11184 /* 11185 * Target node exists. Unconfigure device 11186 * then remove the target node (one ndi 11187 * operation). 11188 */ 11189 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) { 11190 /* 11191 * PROBLEM - no device, but target node remained. This 11192 * happens when the file was open or node was waiting 11193 * for resources. 11194 */ 11195 SATA_LOG_D((sata_hba_inst, CE_WARN, 11196 "sata_remove_target_node: " 11197 "Failed to remove target node for " 11198 "detached SATA device.")); 11199 /* 11200 * Set target node state to DEVI_DEVICE_REMOVED. But 11201 * re-check first that the node still exists. 11202 */ 11203 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 11204 cport, pmport); 11205 if (tdip != NULL) { 11206 sata_set_device_removed(tdip); 11207 /* 11208 * Instruct event daemon to retry the cleanup 11209 * later. 11210 */ 11211 sata_set_target_node_cleanup(sata_hba_inst, 11212 sata_addr); 11213 } 11214 } 11215 11216 if (qual == SATA_ADDR_CPORT) 11217 sata_log(sata_hba_inst, CE_WARN, 11218 "SATA device detached at port %d", cport); 11219 else 11220 sata_log(sata_hba_inst, CE_WARN, 11221 "SATA device detached at port %d:%d", 11222 cport, pmport); 11223 } 11224 #ifdef SATA_DEBUG 11225 else { 11226 if (qual == SATA_ADDR_CPORT) 11227 sata_log(sata_hba_inst, CE_WARN, 11228 "target node not found at port %d", cport); 11229 else 11230 sata_log(sata_hba_inst, CE_WARN, 11231 "target node not found at port %d:%d", 11232 cport, pmport); 11233 } 11234 #endif 11235 } 11236 11237 11238 /* 11239 * Re-probe sata port, check for a device and attach info 11240 * structures when necessary. Identify Device data is fetched, if possible. 11241 * Assumption: sata address is already validated. 11242 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of 11243 * the presence of a device and its type. 11244 * 11245 * flag arg specifies that the function should try multiple times to identify 11246 * device type and to initialize it, or it should return immediately on failure. 11247 * SATA_DEV_IDENTIFY_RETRY - retry 11248 * SATA_DEV_IDENTIFY_NORETRY - no retry 11249 * 11250 * SATA_FAILURE is returned if one of the operations failed. 11251 * 11252 * This function cannot be called in interrupt context - it may sleep. 11253 * 11254 * Note: Port multiplier is supported. 11255 */ 11256 static int 11257 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 11258 int flag) 11259 { 11260 sata_cport_info_t *cportinfo; 11261 sata_pmult_info_t *pmultinfo; 11262 sata_drive_info_t *sdinfo, *osdinfo; 11263 boolean_t init_device = B_FALSE; 11264 int prev_device_type = SATA_DTYPE_NONE; 11265 int prev_device_settings = 0; 11266 int prev_device_state = 0; 11267 clock_t start_time = 0; 11268 int retry = B_FALSE; 11269 uint8_t cport = sata_device->satadev_addr.cport; 11270 int rval_probe, rval_init; 11271 11272 /* 11273 * If target is pmport, sata_reprobe_pmport() will handle it. 11274 */ 11275 if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT || 11276 sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) 11277 return (sata_reprobe_pmport(sata_hba_inst, sata_device, flag)); 11278 11279 /* We only care about host sata cport for now */ 11280 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 11281 sata_device->satadev_addr.cport); 11282 11283 /* 11284 * If a port multiplier was previously attached (we have no idea it 11285 * still there or not), sata_reprobe_pmult() will handle it. 11286 */ 11287 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) 11288 return (sata_reprobe_pmult(sata_hba_inst, sata_device, flag)); 11289 11290 /* Store sata_drive_info when a non-pmult device was attached. */ 11291 osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 11292 if (osdinfo != NULL) { 11293 /* 11294 * We are re-probing port with a previously attached device. 11295 * Save previous device type and settings. 11296 */ 11297 prev_device_type = cportinfo->cport_dev_type; 11298 prev_device_settings = osdinfo->satadrv_settings; 11299 prev_device_state = osdinfo->satadrv_state; 11300 } 11301 if (flag == SATA_DEV_IDENTIFY_RETRY) { 11302 start_time = ddi_get_lbolt(); 11303 retry = B_TRUE; 11304 } 11305 retry_probe: 11306 11307 /* probe port */ 11308 mutex_enter(&cportinfo->cport_mutex); 11309 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 11310 cportinfo->cport_state |= SATA_STATE_PROBING; 11311 mutex_exit(&cportinfo->cport_mutex); 11312 11313 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11314 (SATA_DIP(sata_hba_inst), sata_device); 11315 11316 mutex_enter(&cportinfo->cport_mutex); 11317 if (rval_probe != SATA_SUCCESS) { 11318 cportinfo->cport_state = SATA_PSTATE_FAILED; 11319 mutex_exit(&cportinfo->cport_mutex); 11320 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: " 11321 "SATA port %d probing failed", 11322 cportinfo->cport_addr.cport)); 11323 return (SATA_FAILURE); 11324 } 11325 11326 /* 11327 * update sata port state and set device type 11328 */ 11329 sata_update_port_info(sata_hba_inst, sata_device); 11330 cportinfo->cport_state &= ~SATA_STATE_PROBING; 11331 11332 /* 11333 * Sanity check - Port is active? Is the link active? 11334 * Is there any device attached? 11335 */ 11336 if ((cportinfo->cport_state & 11337 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 11338 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 11339 SATA_PORT_DEVLINK_UP) { 11340 /* 11341 * Port in non-usable state or no link active/no device. 11342 * Free info structure if necessary (direct attached drive 11343 * only, for now! 11344 */ 11345 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 11346 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 11347 /* Add here differentiation for device attached or not */ 11348 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 11349 mutex_exit(&cportinfo->cport_mutex); 11350 if (sdinfo != NULL) 11351 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11352 return (SATA_SUCCESS); 11353 } 11354 11355 cportinfo->cport_state |= SATA_STATE_READY; 11356 cportinfo->cport_state |= SATA_STATE_PROBED; 11357 11358 cportinfo->cport_dev_type = sata_device->satadev_type; 11359 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 11360 11361 /* 11362 * If we are re-probing the port, there may be 11363 * sata_drive_info structure attached 11364 */ 11365 if (sata_device->satadev_type == SATA_DTYPE_NONE) { 11366 11367 /* 11368 * There is no device, so remove device info structure, 11369 * if necessary. 11370 */ 11371 /* Device change: Drive -> None */ 11372 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 11373 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 11374 if (sdinfo != NULL) { 11375 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11376 sata_log(sata_hba_inst, CE_WARN, 11377 "SATA device detached " 11378 "from port %d", cportinfo->cport_addr.cport); 11379 } 11380 mutex_exit(&cportinfo->cport_mutex); 11381 return (SATA_SUCCESS); 11382 11383 } 11384 11385 if (sata_device->satadev_type != SATA_DTYPE_PMULT) { 11386 11387 /* Device (may) change: Drive -> Drive */ 11388 if (sdinfo == NULL) { 11389 /* 11390 * There is some device attached, but there is 11391 * no sata_drive_info structure - allocate one 11392 */ 11393 mutex_exit(&cportinfo->cport_mutex); 11394 sdinfo = kmem_zalloc( 11395 sizeof (sata_drive_info_t), KM_SLEEP); 11396 mutex_enter(&cportinfo->cport_mutex); 11397 /* 11398 * Recheck, that the port state did not change when we 11399 * released mutex. 11400 */ 11401 if (cportinfo->cport_state & SATA_STATE_READY) { 11402 SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo; 11403 sdinfo->satadrv_addr = cportinfo->cport_addr; 11404 sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT; 11405 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 11406 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 11407 } else { 11408 /* 11409 * Port is not in ready state, we 11410 * cannot attach a device. 11411 */ 11412 mutex_exit(&cportinfo->cport_mutex); 11413 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11414 return (SATA_SUCCESS); 11415 } 11416 /* 11417 * Since we are adding device, presumably new one, 11418 * indicate that it should be initalized, 11419 * as well as some internal framework states). 11420 */ 11421 init_device = B_TRUE; 11422 } 11423 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 11424 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual; 11425 } else { 11426 /* Device change: Drive -> PMult */ 11427 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 11428 if (sdinfo != NULL) { 11429 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11430 sata_log(sata_hba_inst, CE_WARN, 11431 "SATA device detached " 11432 "from port %d", cportinfo->cport_addr.cport); 11433 } 11434 11435 sata_log(sata_hba_inst, CE_WARN, 11436 "SATA port multiplier detected at port %d", 11437 cportinfo->cport_addr.cport); 11438 11439 mutex_exit(&cportinfo->cport_mutex); 11440 if (sata_alloc_pmult(sata_hba_inst, sata_device) != 11441 SATA_SUCCESS) 11442 return (SATA_FAILURE); 11443 sata_show_pmult_info(sata_hba_inst, sata_device); 11444 mutex_enter(&cportinfo->cport_mutex); 11445 11446 /* 11447 * Mark all the port multiplier port behind the port 11448 * multiplier behind with link events, so that the sata daemon 11449 * will update their status. 11450 */ 11451 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 11452 pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET; 11453 mutex_exit(&cportinfo->cport_mutex); 11454 return (SATA_SUCCESS); 11455 } 11456 mutex_exit(&cportinfo->cport_mutex); 11457 11458 /* 11459 * Figure out what kind of device we are really 11460 * dealing with. Failure of identifying device does not fail this 11461 * function. 11462 */ 11463 rval_probe = sata_probe_device(sata_hba_inst, sata_device); 11464 rval_init = SATA_FAILURE; 11465 mutex_enter(&cportinfo->cport_mutex); 11466 if (rval_probe == SATA_SUCCESS) { 11467 /* 11468 * If we are dealing with the same type of a device as before, 11469 * restore its settings flags. 11470 */ 11471 if (osdinfo != NULL && 11472 sata_device->satadev_type == prev_device_type) 11473 sdinfo->satadrv_settings = prev_device_settings; 11474 11475 mutex_exit(&cportinfo->cport_mutex); 11476 rval_init = SATA_SUCCESS; 11477 /* Set initial device features, if necessary */ 11478 if (init_device == B_TRUE) { 11479 rval_init = sata_initialize_device(sata_hba_inst, 11480 sdinfo); 11481 } 11482 if (rval_init == SATA_SUCCESS) 11483 return (rval_init); 11484 /* else we will retry if retry was asked for */ 11485 11486 } else { 11487 /* 11488 * If there was some device info before we probe the device, 11489 * restore previous device setting, so we can retry from scratch 11490 * later. Providing, of course, that device has not disapear 11491 * during probing process. 11492 */ 11493 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 11494 if (osdinfo != NULL) { 11495 cportinfo->cport_dev_type = prev_device_type; 11496 sdinfo->satadrv_type = prev_device_type; 11497 sdinfo->satadrv_state = prev_device_state; 11498 } 11499 } else { 11500 /* device is gone */ 11501 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11502 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 11503 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 11504 mutex_exit(&cportinfo->cport_mutex); 11505 return (SATA_SUCCESS); 11506 } 11507 mutex_exit(&cportinfo->cport_mutex); 11508 } 11509 11510 if (retry) { 11511 clock_t cur_time = ddi_get_lbolt(); 11512 /* 11513 * A device was not successfully identified or initialized. 11514 * Track retry time for device identification. 11515 */ 11516 if ((cur_time - start_time) < 11517 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 11518 /* sleep for a while */ 11519 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 11520 goto retry_probe; 11521 } 11522 /* else no more retries */ 11523 mutex_enter(&cportinfo->cport_mutex); 11524 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 11525 if (rval_init == SATA_RETRY) { 11526 /* 11527 * Setting drive features have failed, but 11528 * because the drive is still accessible, 11529 * keep it and emit a warning message. 11530 */ 11531 sata_log(sata_hba_inst, CE_WARN, 11532 "SATA device at port %d - desired " 11533 "drive features could not be set. " 11534 "Device may not operate as expected.", 11535 cportinfo->cport_addr.cport); 11536 } else { 11537 SATA_CPORTINFO_DRV_INFO(cportinfo)-> 11538 satadrv_state = SATA_DSTATE_FAILED; 11539 } 11540 } 11541 mutex_exit(&cportinfo->cport_mutex); 11542 } 11543 return (SATA_SUCCESS); 11544 } 11545 11546 /* 11547 * Reprobe a controller port that connected to a port multiplier. 11548 * 11549 * NOTE: No Mutex should be hold. 11550 */ 11551 static int 11552 sata_reprobe_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 11553 int flag) 11554 { 11555 _NOTE(ARGUNUSED(flag)) 11556 sata_cport_info_t *cportinfo; 11557 sata_pmult_info_t *pmultinfo; 11558 uint8_t cport = sata_device->satadev_addr.cport; 11559 int rval_probe; 11560 11561 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11562 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 11563 11564 /* probe port */ 11565 mutex_enter(&cportinfo->cport_mutex); 11566 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 11567 cportinfo->cport_state |= SATA_STATE_PROBING; 11568 mutex_exit(&cportinfo->cport_mutex); 11569 11570 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11571 (SATA_DIP(sata_hba_inst), sata_device); 11572 11573 mutex_enter(&cportinfo->cport_mutex); 11574 if (rval_probe != SATA_SUCCESS) { 11575 cportinfo->cport_state = SATA_PSTATE_FAILED; 11576 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmult: " 11577 "SATA port %d probing failed", cport)); 11578 sata_log(sata_hba_inst, CE_WARN, 11579 "SATA port multiplier detached at port %d", cport); 11580 mutex_exit(&cportinfo->cport_mutex); 11581 sata_free_pmult(sata_hba_inst, sata_device); 11582 return (SATA_FAILURE); 11583 } 11584 11585 /* 11586 * update sata port state and set device type 11587 */ 11588 sata_update_port_info(sata_hba_inst, sata_device); 11589 cportinfo->cport_state &= ~SATA_STATE_PROBING; 11590 cportinfo->cport_state |= SATA_STATE_PROBED; 11591 11592 /* 11593 * Sanity check - Port is active? Is the link active? 11594 * Is there any device attached? 11595 */ 11596 if ((cportinfo->cport_state & 11597 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 11598 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 11599 SATA_PORT_DEVLINK_UP || 11600 (sata_device->satadev_type == SATA_DTYPE_NONE)) { 11601 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 11602 mutex_exit(&cportinfo->cport_mutex); 11603 sata_free_pmult(sata_hba_inst, sata_device); 11604 sata_log(sata_hba_inst, CE_WARN, 11605 "SATA port multiplier detached at port %d", cport); 11606 return (SATA_SUCCESS); 11607 } 11608 11609 /* 11610 * Device changed: PMult -> Non-PMult 11611 * 11612 * This situation is uncommon, most possibly being caused by errors 11613 * after which the port multiplier is not correct initialized and 11614 * recognized. In that case the new device will be marked as unknown 11615 * and will not be automatically probed in this routine. Instead 11616 * system administrator could manually restart it via cfgadm(8). 11617 */ 11618 if (sata_device->satadev_type != SATA_DTYPE_PMULT) { 11619 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 11620 mutex_exit(&cportinfo->cport_mutex); 11621 sata_free_pmult(sata_hba_inst, sata_device); 11622 sata_log(sata_hba_inst, CE_WARN, 11623 "SATA port multiplier detached at port %d", cport); 11624 return (SATA_FAILURE); 11625 } 11626 11627 /* 11628 * Now we know it is a port multiplier. However, if this is not the 11629 * previously attached port multiplier - they may have different 11630 * pmport numbers - we need to re-allocate data structures for every 11631 * pmport and drive. 11632 * 11633 * Port multipliers of the same model have identical values in these 11634 * registers, so it is still necessary to update the information of 11635 * all drives attached to the previous port multiplier afterwards. 11636 */ 11637 /* Device changed: PMult -> another PMult */ 11638 mutex_exit(&cportinfo->cport_mutex); 11639 sata_free_pmult(sata_hba_inst, sata_device); 11640 if (sata_alloc_pmult(sata_hba_inst, sata_device) != SATA_SUCCESS) 11641 return (SATA_FAILURE); 11642 mutex_enter(&cportinfo->cport_mutex); 11643 11644 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 11645 "SATA port multiplier [changed] at port %d", cport); 11646 sata_log(sata_hba_inst, CE_WARN, 11647 "SATA port multiplier detected at port %d", cport); 11648 11649 /* 11650 * Mark all the port multiplier port behind the port 11651 * multiplier behind with link events, so that the sata daemon 11652 * will update their status. 11653 */ 11654 pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET; 11655 mutex_exit(&cportinfo->cport_mutex); 11656 11657 return (SATA_SUCCESS); 11658 } 11659 11660 /* 11661 * Re-probe a port multiplier port, check for a device and attach info 11662 * structures when necessary. Identify Device data is fetched, if possible. 11663 * Assumption: sata address is already validated as port multiplier port. 11664 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of 11665 * the presence of a device and its type. 11666 * 11667 * flag arg specifies that the function should try multiple times to identify 11668 * device type and to initialize it, or it should return immediately on failure. 11669 * SATA_DEV_IDENTIFY_RETRY - retry 11670 * SATA_DEV_IDENTIFY_NORETRY - no retry 11671 * 11672 * SATA_FAILURE is returned if one of the operations failed. 11673 * 11674 * This function cannot be called in interrupt context - it may sleep. 11675 * 11676 * NOTE: Should be only called by sata_probe_port() in case target port is a 11677 * port multiplier port. 11678 * NOTE: No Mutex should be hold. 11679 */ 11680 static int 11681 sata_reprobe_pmport(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 11682 int flag) 11683 { 11684 sata_cport_info_t *cportinfo = NULL; 11685 sata_pmport_info_t *pmportinfo = NULL; 11686 sata_drive_info_t *sdinfo, *osdinfo; 11687 sata_device_t sdevice; 11688 boolean_t init_device = B_FALSE; 11689 int prev_device_type = SATA_DTYPE_NONE; 11690 int prev_device_settings = 0; 11691 int prev_device_state = 0; 11692 clock_t start_time; 11693 uint8_t cport = sata_device->satadev_addr.cport; 11694 uint8_t pmport = sata_device->satadev_addr.pmport; 11695 int rval; 11696 11697 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11698 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 11699 osdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 11700 11701 if (osdinfo != NULL) { 11702 /* 11703 * We are re-probing port with a previously attached device. 11704 * Save previous device type and settings. 11705 */ 11706 prev_device_type = pmportinfo->pmport_dev_type; 11707 prev_device_settings = osdinfo->satadrv_settings; 11708 prev_device_state = osdinfo->satadrv_state; 11709 } 11710 11711 start_time = ddi_get_lbolt(); 11712 11713 /* check parent status */ 11714 mutex_enter(&cportinfo->cport_mutex); 11715 if ((cportinfo->cport_state & 11716 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 11717 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 11718 SATA_PORT_DEVLINK_UP) { 11719 mutex_exit(&cportinfo->cport_mutex); 11720 return (SATA_FAILURE); 11721 } 11722 mutex_exit(&cportinfo->cport_mutex); 11723 11724 retry_probe_pmport: 11725 11726 /* probe port */ 11727 mutex_enter(&pmportinfo->pmport_mutex); 11728 pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 11729 pmportinfo->pmport_state |= SATA_STATE_PROBING; 11730 mutex_exit(&pmportinfo->pmport_mutex); 11731 11732 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11733 (SATA_DIP(sata_hba_inst), sata_device); 11734 11735 /* might need retry because we cannot touch registers. */ 11736 if (rval == SATA_FAILURE) { 11737 mutex_enter(&pmportinfo->pmport_mutex); 11738 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 11739 mutex_exit(&pmportinfo->pmport_mutex); 11740 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: " 11741 "SATA port %d:%d probing failed", 11742 cport, pmport)); 11743 return (SATA_FAILURE); 11744 } else if (rval == SATA_RETRY) { 11745 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: " 11746 "SATA port %d:%d probing failed, retrying...", 11747 cport, pmport)); 11748 clock_t cur_time = ddi_get_lbolt(); 11749 /* 11750 * A device was not successfully identified or initialized. 11751 * Track retry time for device identification. 11752 */ 11753 if ((cur_time - start_time) < 11754 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 11755 /* sleep for a while */ 11756 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 11757 goto retry_probe_pmport; 11758 } else { 11759 mutex_enter(&pmportinfo->pmport_mutex); 11760 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) 11761 SATA_PMPORTINFO_DRV_INFO(pmportinfo)-> 11762 satadrv_state = SATA_DSTATE_FAILED; 11763 mutex_exit(&pmportinfo->pmport_mutex); 11764 return (SATA_SUCCESS); 11765 } 11766 } 11767 11768 /* 11769 * Sanity check - Controller port is active? Is the link active? 11770 * Is it still a port multiplier? 11771 */ 11772 if ((cportinfo->cport_state & 11773 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 11774 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 11775 SATA_PORT_DEVLINK_UP || 11776 (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) { 11777 /* 11778 * Port in non-usable state or no link active/no 11779 * device. Free info structure. 11780 */ 11781 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 11782 11783 sdevice.satadev_addr.cport = cport; 11784 sdevice.satadev_addr.pmport = pmport; 11785 sdevice.satadev_addr.qual = SATA_ADDR_PMULT; 11786 mutex_exit(&cportinfo->cport_mutex); 11787 11788 sata_free_pmult(sata_hba_inst, &sdevice); 11789 return (SATA_FAILURE); 11790 } 11791 11792 /* SATA_SUCCESS NOW */ 11793 /* 11794 * update sata port state and set device type 11795 */ 11796 mutex_enter(&pmportinfo->pmport_mutex); 11797 sata_update_pmport_info(sata_hba_inst, sata_device); 11798 pmportinfo->pmport_state &= ~SATA_STATE_PROBING; 11799 11800 /* 11801 * Sanity check - Port is active? Is the link active? 11802 * Is there any device attached? 11803 */ 11804 if ((pmportinfo->pmport_state & 11805 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 11806 (pmportinfo->pmport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 11807 SATA_PORT_DEVLINK_UP) { 11808 /* 11809 * Port in non-usable state or no link active/no device. 11810 * Free info structure if necessary (direct attached drive 11811 * only, for now! 11812 */ 11813 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 11814 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 11815 /* Add here differentiation for device attached or not */ 11816 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 11817 mutex_exit(&pmportinfo->pmport_mutex); 11818 if (sdinfo != NULL) 11819 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11820 return (SATA_SUCCESS); 11821 } 11822 11823 pmportinfo->pmport_state |= SATA_STATE_READY; 11824 pmportinfo->pmport_dev_type = sata_device->satadev_type; 11825 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 11826 11827 /* 11828 * If we are re-probing the port, there may be 11829 * sata_drive_info structure attached 11830 * (or sata_pm_info, if PMult is supported). 11831 */ 11832 if (sata_device->satadev_type == SATA_DTYPE_NONE) { 11833 /* 11834 * There is no device, so remove device info structure, 11835 * if necessary. 11836 */ 11837 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 11838 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 11839 if (sdinfo != NULL) { 11840 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11841 sata_log(sata_hba_inst, CE_WARN, 11842 "SATA device detached from port %d:%d", 11843 cport, pmport); 11844 } 11845 mutex_exit(&pmportinfo->pmport_mutex); 11846 return (SATA_SUCCESS); 11847 } 11848 11849 /* this should not be a pmult */ 11850 ASSERT(sata_device->satadev_type != SATA_DTYPE_PMULT); 11851 if (sdinfo == NULL) { 11852 /* 11853 * There is some device attached, but there is 11854 * no sata_drive_info structure - allocate one 11855 */ 11856 mutex_exit(&pmportinfo->pmport_mutex); 11857 sdinfo = kmem_zalloc(sizeof (sata_drive_info_t), 11858 KM_SLEEP); 11859 mutex_enter(&pmportinfo->pmport_mutex); 11860 /* 11861 * Recheck, that the port state did not change when we 11862 * released mutex. 11863 */ 11864 if (pmportinfo->pmport_state & SATA_STATE_READY) { 11865 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = sdinfo; 11866 sdinfo->satadrv_addr = pmportinfo->pmport_addr; 11867 sdinfo->satadrv_addr.qual = SATA_ADDR_DPMPORT; 11868 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 11869 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 11870 } else { 11871 /* 11872 * Port is not in ready state, we 11873 * cannot attach a device. 11874 */ 11875 mutex_exit(&pmportinfo->pmport_mutex); 11876 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11877 return (SATA_SUCCESS); 11878 } 11879 /* 11880 * Since we are adding device, presumably new one, 11881 * indicate that it should be initalized, 11882 * as well as some internal framework states). 11883 */ 11884 init_device = B_TRUE; 11885 } 11886 11887 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN; 11888 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual; 11889 11890 mutex_exit(&pmportinfo->pmport_mutex); 11891 /* 11892 * Figure out what kind of device we are really 11893 * dealing with. 11894 */ 11895 rval = sata_probe_device(sata_hba_inst, sata_device); 11896 11897 mutex_enter(&pmportinfo->pmport_mutex); 11898 if (rval == SATA_SUCCESS) { 11899 /* 11900 * If we are dealing with the same type of a device as before, 11901 * restore its settings flags. 11902 */ 11903 if (osdinfo != NULL && 11904 sata_device->satadev_type == prev_device_type) 11905 sdinfo->satadrv_settings = prev_device_settings; 11906 11907 mutex_exit(&pmportinfo->pmport_mutex); 11908 /* Set initial device features, if necessary */ 11909 if (init_device == B_TRUE) { 11910 rval = sata_initialize_device(sata_hba_inst, sdinfo); 11911 } 11912 if (rval == SATA_SUCCESS) 11913 return (rval); 11914 } else { 11915 /* 11916 * If there was some device info before we probe the device, 11917 * restore previous device setting, so we can retry from scratch 11918 * later. Providing, of course, that device has not disappeared 11919 * during probing process. 11920 */ 11921 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 11922 if (osdinfo != NULL) { 11923 pmportinfo->pmport_dev_type = prev_device_type; 11924 sdinfo->satadrv_type = prev_device_type; 11925 sdinfo->satadrv_state = prev_device_state; 11926 } 11927 } else { 11928 /* device is gone */ 11929 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 11930 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 11931 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 11932 mutex_exit(&pmportinfo->pmport_mutex); 11933 return (SATA_SUCCESS); 11934 } 11935 mutex_exit(&pmportinfo->pmport_mutex); 11936 } 11937 11938 if (flag == SATA_DEV_IDENTIFY_RETRY) { 11939 clock_t cur_time = ddi_get_lbolt(); 11940 /* 11941 * A device was not successfully identified or initialized. 11942 * Track retry time for device identification. 11943 */ 11944 if ((cur_time - start_time) < 11945 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 11946 /* sleep for a while */ 11947 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 11948 goto retry_probe_pmport; 11949 } else { 11950 mutex_enter(&pmportinfo->pmport_mutex); 11951 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) 11952 SATA_PMPORTINFO_DRV_INFO(pmportinfo)-> 11953 satadrv_state = SATA_DSTATE_FAILED; 11954 mutex_exit(&pmportinfo->pmport_mutex); 11955 } 11956 } 11957 return (SATA_SUCCESS); 11958 } 11959 11960 /* 11961 * Allocated related structure for a port multiplier and its device ports 11962 * 11963 * Port multiplier should be ready and probed, and related information like 11964 * the number of the device ports should be store in sata_device_t. 11965 * 11966 * NOTE: No Mutex should be hold. 11967 */ 11968 static int 11969 sata_alloc_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 11970 { 11971 dev_info_t *dip = SATA_DIP(sata_hba_inst); 11972 sata_cport_info_t *cportinfo = NULL; 11973 sata_pmult_info_t *pmultinfo = NULL; 11974 sata_pmport_info_t *pmportinfo = NULL; 11975 sata_device_t sd; 11976 dev_t minor_number; 11977 char name[16]; 11978 uint8_t cport = sata_device->satadev_addr.cport; 11979 int rval; 11980 int npmport; 11981 11982 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11983 11984 /* This function might be called while a port-mult is hot-plugged. */ 11985 mutex_enter(&cportinfo->cport_mutex); 11986 11987 /* dev_type's not updated when get called from sata_reprobe_port() */ 11988 if (SATA_CPORTINFO_PMULT_INFO(cportinfo) == NULL) { 11989 /* Create a pmult_info structure */ 11990 SATA_CPORTINFO_PMULT_INFO(cportinfo) = 11991 kmem_zalloc(sizeof (sata_pmult_info_t), KM_SLEEP); 11992 } 11993 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 11994 11995 pmultinfo->pmult_addr = sata_device->satadev_addr; 11996 pmultinfo->pmult_addr.qual = SATA_ADDR_PMULT; 11997 pmultinfo->pmult_state = SATA_STATE_PROBING; 11998 11999 /* 12000 * Probe the port multiplier with qualifier SATA_ADDR_PMULT_SPEC, 12001 * The HBA driver should initialize and register the port multiplier, 12002 * sata_register_pmult() will fill following fields, 12003 * + sata_pmult_info.pmult_gscr 12004 * + sata_pmult_info.pmult_num_dev_ports 12005 */ 12006 sd.satadev_addr = sata_device->satadev_addr; 12007 sd.satadev_addr.qual = SATA_ADDR_PMULT_SPEC; 12008 mutex_exit(&cportinfo->cport_mutex); 12009 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 12010 (SATA_DIP(sata_hba_inst), &sd); 12011 mutex_enter(&cportinfo->cport_mutex); 12012 12013 if (rval != SATA_SUCCESS || 12014 (sd.satadev_type != SATA_DTYPE_PMULT) || 12015 !(sd.satadev_state & SATA_DSTATE_PMULT_INIT)) { 12016 SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL; 12017 kmem_free(pmultinfo, sizeof (sata_pmult_info_t)); 12018 cportinfo->cport_state = SATA_PSTATE_FAILED; 12019 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 12020 mutex_exit(&cportinfo->cport_mutex); 12021 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 12022 "sata_alloc_pmult: failed to initialize pmult " 12023 "at port %d.", cport) 12024 return (SATA_FAILURE); 12025 } 12026 12027 /* Initialize pmport_info structure */ 12028 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; 12029 npmport++) { 12030 12031 /* if everything is allocated, skip */ 12032 if (SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) != NULL) 12033 continue; 12034 12035 pmportinfo = kmem_zalloc(sizeof (sata_pmport_info_t), KM_SLEEP); 12036 mutex_init(&pmportinfo->pmport_mutex, NULL, MUTEX_DRIVER, NULL); 12037 mutex_exit(&cportinfo->cport_mutex); 12038 12039 mutex_enter(&pmportinfo->pmport_mutex); 12040 pmportinfo->pmport_addr.cport = cport; 12041 pmportinfo->pmport_addr.pmport = (uint8_t)npmport; 12042 pmportinfo->pmport_addr.qual = SATA_ADDR_PMPORT; 12043 pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 12044 mutex_exit(&pmportinfo->pmport_mutex); 12045 12046 mutex_enter(&cportinfo->cport_mutex); 12047 SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) = pmportinfo; 12048 12049 /* Create an attachment point */ 12050 minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip), 12051 cport, (uint8_t)npmport, SATA_ADDR_PMPORT); 12052 (void) sprintf(name, "%d.%d", cport, npmport); 12053 12054 if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number, 12055 DDI_NT_SATA_ATTACHMENT_POINT, 0) != DDI_SUCCESS) { 12056 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: " 12057 "cannot create SATA attachment point for " 12058 "port %d:%d", cport, npmport); 12059 } 12060 } 12061 12062 pmultinfo->pmult_state &= ~SATA_STATE_PROBING; 12063 pmultinfo->pmult_state |= (SATA_STATE_PROBED|SATA_STATE_READY); 12064 cportinfo->cport_dev_type = SATA_DTYPE_PMULT; 12065 12066 mutex_exit(&cportinfo->cport_mutex); 12067 return (SATA_SUCCESS); 12068 } 12069 12070 /* 12071 * Free data structures when a port multiplier is removed. 12072 * 12073 * NOTE: No Mutex should be hold. 12074 */ 12075 static void 12076 sata_free_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 12077 { 12078 sata_cport_info_t *cportinfo; 12079 sata_pmult_info_t *pmultinfo; 12080 sata_pmport_info_t *pmportinfo; 12081 sata_device_t pmport_device; 12082 sata_drive_info_t *sdinfo; 12083 dev_info_t *tdip; 12084 char name[16]; 12085 uint8_t cport = sata_device->satadev_addr.cport; 12086 int npmport; 12087 12088 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 12089 12090 /* This function might be called while port-mult is hot plugged. */ 12091 mutex_enter(&cportinfo->cport_mutex); 12092 12093 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 12094 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 12095 ASSERT(pmultinfo != NULL); 12096 12097 /* Free pmport_info structure */ 12098 for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; 12099 npmport++) { 12100 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport); 12101 if (pmportinfo == NULL) 12102 continue; 12103 mutex_exit(&cportinfo->cport_mutex); 12104 12105 mutex_enter(&pmportinfo->pmport_mutex); 12106 sdinfo = pmportinfo->pmport_sata_drive; 12107 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 12108 mutex_exit(&pmportinfo->pmport_mutex); 12109 12110 /* Remove attachment point. */ 12111 name[0] = '\0'; 12112 (void) sprintf(name, "%d.%d", cport, npmport); 12113 ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name); 12114 sata_log(sata_hba_inst, CE_NOTE, 12115 "Remove attachment point of port %d:%d", 12116 cport, npmport); 12117 12118 /* 12119 * Rumove target node 12120 */ 12121 bzero(&pmport_device, sizeof (sata_device_t)); 12122 pmport_device.satadev_rev = SATA_DEVICE_REV; 12123 pmport_device.satadev_addr.cport = cport; 12124 pmport_device.satadev_addr.pmport = (uint8_t)npmport; 12125 pmport_device.satadev_addr.qual = SATA_ADDR_DPMPORT; 12126 12127 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 12128 &(pmport_device.satadev_addr)); 12129 if (tdip != NULL && ndi_devi_offline(tdip, 12130 NDI_DEVI_REMOVE) != NDI_SUCCESS) { 12131 /* 12132 * Problem : 12133 * The target node remained attached. 12134 * This happens when the device file was open 12135 * or a node was waiting for resources. 12136 * Cannot do anything about it. 12137 */ 12138 SATA_LOG_D((sata_hba_inst, CE_WARN, 12139 "sata_free_pmult: could not unconfigure device " 12140 "before disconnecting the SATA port %d:%d", 12141 cport, npmport)); 12142 12143 /* 12144 * Set DEVICE REMOVED state in the target 12145 * node. It will prevent access to the device 12146 * even when a new device is attached, until 12147 * the old target node is released, removed and 12148 * recreated for a new device. 12149 */ 12150 sata_set_device_removed(tdip); 12151 12152 /* 12153 * Instruct event daemon to try the target 12154 * node cleanup later. 12155 */ 12156 sata_set_target_node_cleanup( 12157 sata_hba_inst, &(pmport_device.satadev_addr)); 12158 12159 } 12160 mutex_enter(&cportinfo->cport_mutex); 12161 12162 /* 12163 * Add here differentiation for device attached or not 12164 */ 12165 if (sdinfo != NULL) { 12166 sata_log(sata_hba_inst, CE_WARN, 12167 "SATA device detached from port %d:%d", 12168 cport, npmport); 12169 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 12170 } 12171 12172 mutex_destroy(&pmportinfo->pmport_mutex); 12173 kmem_free(pmportinfo, sizeof (sata_pmport_info_t)); 12174 } 12175 12176 kmem_free(pmultinfo, sizeof (sata_pmult_info_t)); 12177 12178 cportinfo->cport_devp.cport_sata_pmult = NULL; 12179 12180 sata_log(sata_hba_inst, CE_WARN, 12181 "SATA port multiplier detached at port %d", cport); 12182 12183 mutex_exit(&cportinfo->cport_mutex); 12184 } 12185 12186 /* 12187 * Initialize device 12188 * Specified device is initialized to a default state. 12189 * 12190 * Returns SATA_SUCCESS if all device features are set successfully, 12191 * SATA_RETRY if device is accessible but device features were not set 12192 * successfully, and SATA_FAILURE otherwise. 12193 */ 12194 static int 12195 sata_initialize_device(sata_hba_inst_t *sata_hba_inst, 12196 sata_drive_info_t *sdinfo) 12197 { 12198 int rval; 12199 12200 sata_save_drive_settings(sdinfo); 12201 12202 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 12203 12204 sata_init_write_cache_mode(sdinfo); 12205 12206 rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0); 12207 12208 /* Determine current data transfer mode */ 12209 if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) { 12210 sdinfo->satadrv_settings &= ~SATA_DEV_DMA; 12211 } else if ((sdinfo->satadrv_id.ai_validinfo & 12212 SATA_VALIDINFO_88) != 0 && 12213 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) { 12214 sdinfo->satadrv_settings |= SATA_DEV_DMA; 12215 } else if ((sdinfo->satadrv_id.ai_dworddma & 12216 SATA_MDMA_SEL_MASK) != 0) { 12217 sdinfo->satadrv_settings |= SATA_DEV_DMA; 12218 } else 12219 /* DMA supported, not no DMA transfer mode is selected !? */ 12220 sdinfo->satadrv_settings &= ~SATA_DEV_DMA; 12221 12222 if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) && 12223 (sdinfo->satadrv_id.ai_features86 & 0x20)) 12224 sdinfo->satadrv_power_level = SATA_POWER_STANDBY; 12225 else 12226 sdinfo->satadrv_power_level = SATA_POWER_ACTIVE; 12227 12228 return (rval); 12229 } 12230 12231 12232 /* 12233 * Initialize write cache mode. 12234 * 12235 * The default write cache setting for SATA HDD is provided by sata_write_cache 12236 * static variable. ATAPI CD/DVDs devices have write cache default is 12237 * determined by sata_atapicdvd_write_cache static variable. 12238 * ATAPI tape devices have write cache default is determined by 12239 * sata_atapitape_write_cache static variable. 12240 * ATAPI disk devices have write cache default is determined by 12241 * sata_atapidisk_write_cache static variable. 12242 * 1 - enable 12243 * 0 - disable 12244 * any other value - current drive setting 12245 * 12246 * Although there is not reason to disable write cache on CD/DVD devices, 12247 * tape devices and ATAPI disk devices, the default setting control is provided 12248 * for the maximun flexibility. 12249 * 12250 * In the future, it may be overridden by the 12251 * disk-write-cache-enable property setting, if it is defined. 12252 * Returns SATA_SUCCESS if all device features are set successfully, 12253 * SATA_FAILURE otherwise. 12254 */ 12255 static void 12256 sata_init_write_cache_mode(sata_drive_info_t *sdinfo) 12257 { 12258 switch (sdinfo->satadrv_type) { 12259 case SATA_DTYPE_ATADISK: 12260 if (sata_write_cache == 1) 12261 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 12262 else if (sata_write_cache == 0) 12263 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 12264 /* 12265 * When sata_write_cache value is not 0 or 1, 12266 * a current setting of the drive's write cache is used. 12267 */ 12268 break; 12269 case SATA_DTYPE_ATAPICD: 12270 if (sata_atapicdvd_write_cache == 1) 12271 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 12272 else if (sata_atapicdvd_write_cache == 0) 12273 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 12274 /* 12275 * When sata_atapicdvd_write_cache value is not 0 or 1, 12276 * a current setting of the drive's write cache is used. 12277 */ 12278 break; 12279 case SATA_DTYPE_ATAPITAPE: 12280 if (sata_atapitape_write_cache == 1) 12281 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 12282 else if (sata_atapitape_write_cache == 0) 12283 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 12284 /* 12285 * When sata_atapitape_write_cache value is not 0 or 1, 12286 * a current setting of the drive's write cache is used. 12287 */ 12288 break; 12289 case SATA_DTYPE_ATAPIDISK: 12290 if (sata_atapidisk_write_cache == 1) 12291 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 12292 else if (sata_atapidisk_write_cache == 0) 12293 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 12294 /* 12295 * When sata_atapidisk_write_cache value is not 0 or 1, 12296 * a current setting of the drive's write cache is used. 12297 */ 12298 break; 12299 } 12300 } 12301 12302 12303 /* 12304 * Validate sata address. 12305 * Specified cport, pmport and qualifier has to match 12306 * passed sata_scsi configuration info. 12307 * The presence of an attached device is not verified. 12308 * 12309 * Returns 0 when address is valid, -1 otherwise. 12310 */ 12311 static int 12312 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport, 12313 int pmport, int qual) 12314 { 12315 if (qual == SATA_ADDR_DCPORT && pmport != 0) 12316 goto invalid_address; 12317 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 12318 goto invalid_address; 12319 if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) && 12320 ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) || 12321 (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) || 12322 (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport)))) 12323 goto invalid_address; 12324 12325 return (0); 12326 12327 invalid_address: 12328 return (-1); 12329 12330 } 12331 12332 /* 12333 * Validate scsi address 12334 * SCSI target address is translated into SATA cport/pmport and compared 12335 * with a controller port/device configuration. LUN has to be 0. 12336 * Returns 0 if a scsi target refers to an attached device, 12337 * returns 1 if address is valid but no valid device is attached, 12338 * returns 2 if address is valid but device type is unknown (not valid device), 12339 * returns -1 if bad address or device is of an unsupported type. 12340 * Upon return sata_device argument is set. 12341 * 12342 * Port multiplier is supported now. 12343 */ 12344 static int 12345 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst, 12346 struct scsi_address *ap, sata_device_t *sata_device) 12347 { 12348 int cport, pmport, qual, rval; 12349 12350 rval = -1; /* Invalid address */ 12351 if (ap->a_lun != 0) 12352 goto out; 12353 12354 qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 12355 cport = SCSI_TO_SATA_CPORT(ap->a_target); 12356 pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 12357 12358 if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT) 12359 goto out; 12360 12361 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) == 12362 0) { 12363 12364 sata_cport_info_t *cportinfo; 12365 sata_pmult_info_t *pmultinfo; 12366 sata_drive_info_t *sdinfo = NULL; 12367 12368 sata_device->satadev_addr.qual = qual; 12369 sata_device->satadev_addr.cport = cport; 12370 sata_device->satadev_addr.pmport = pmport; 12371 sata_device->satadev_rev = SATA_DEVICE_REV_1; 12372 12373 rval = 1; /* Valid sata address */ 12374 12375 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 12376 if (qual == SATA_ADDR_DCPORT) { 12377 if (cportinfo == NULL || 12378 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 12379 goto out; 12380 12381 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 12382 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN && 12383 sdinfo != NULL) { 12384 rval = 2; 12385 goto out; 12386 } 12387 12388 if ((cportinfo->cport_dev_type & 12389 SATA_VALID_DEV_TYPE) == 0) { 12390 rval = -1; 12391 goto out; 12392 } 12393 12394 } else if (qual == SATA_ADDR_DPMPORT) { 12395 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 12396 if (pmultinfo == NULL) { 12397 rval = -1; 12398 goto out; 12399 } 12400 if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) == 12401 NULL || 12402 SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 12403 pmport) == SATA_DTYPE_NONE) 12404 goto out; 12405 12406 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, 12407 pmport); 12408 if (SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 12409 pmport) == SATA_DTYPE_UNKNOWN && sdinfo != NULL) { 12410 rval = 2; 12411 goto out; 12412 } 12413 12414 if ((SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 12415 pmport) & SATA_VALID_DEV_TYPE) == 0) { 12416 rval = -1; 12417 goto out; 12418 } 12419 12420 } else { 12421 rval = -1; 12422 goto out; 12423 } 12424 if ((sdinfo == NULL) || 12425 (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0) 12426 goto out; 12427 12428 sata_device->satadev_type = sdinfo->satadrv_type; 12429 12430 return (0); 12431 } 12432 out: 12433 if (rval > 0) { 12434 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 12435 "sata_validate_scsi_address: no valid target %x lun %x", 12436 ap->a_target, ap->a_lun); 12437 } 12438 return (rval); 12439 } 12440 12441 /* 12442 * Find dip corresponding to passed device number 12443 * 12444 * Returns NULL if invalid device number is passed or device cannot be found, 12445 * Returns dip is device is found. 12446 */ 12447 static dev_info_t * 12448 sata_devt_to_devinfo(dev_t dev) 12449 { 12450 dev_info_t *dip; 12451 #ifndef __lock_lint 12452 struct devnames *dnp; 12453 major_t major = getmajor(dev); 12454 int instance = SATA_MINOR2INSTANCE(getminor(dev)); 12455 12456 if (major >= devcnt) 12457 return (NULL); 12458 12459 dnp = &devnamesp[major]; 12460 LOCK_DEV_OPS(&(dnp->dn_lock)); 12461 dip = dnp->dn_head; 12462 while (dip && (ddi_get_instance(dip) != instance)) { 12463 dip = ddi_get_next(dip); 12464 } 12465 UNLOCK_DEV_OPS(&(dnp->dn_lock)); 12466 #endif 12467 12468 return (dip); 12469 } 12470 12471 12472 /* 12473 * Probe device. 12474 * This function issues Identify Device command and initializes local 12475 * sata_drive_info structure if the device can be identified. 12476 * The device type is determined by examining Identify Device 12477 * command response. 12478 * If the sata_hba_inst has linked drive info structure for this 12479 * device address, the Identify Device data is stored into sata_drive_info 12480 * structure linked to the port info structure. 12481 * 12482 * sata_device has to refer to the valid sata port(s) for HBA described 12483 * by sata_hba_inst structure. 12484 * 12485 * Returns: 12486 * SATA_SUCCESS if device type was successfully probed and port-linked 12487 * drive info structure was updated; 12488 * SATA_FAILURE if there is no device, or device was not probed 12489 * successully; 12490 * SATA_RETRY if device probe can be retried later. 12491 * If a device cannot be identified, sata_device's dev_state and dev_type 12492 * fields are set to unknown. 12493 * There are no retries in this function. Any retries should be managed by 12494 * the caller. 12495 */ 12496 12497 12498 static int 12499 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 12500 { 12501 sata_pmport_info_t *pmportinfo = NULL; 12502 sata_drive_info_t *sdinfo; 12503 sata_drive_info_t new_sdinfo; /* local drive info struct */ 12504 int rval; 12505 12506 ASSERT((SATA_CPORT_STATE(sata_hba_inst, 12507 sata_device->satadev_addr.cport) & 12508 (SATA_STATE_PROBED | SATA_STATE_READY)) != 0); 12509 12510 sata_device->satadev_type = SATA_DTYPE_NONE; 12511 12512 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12513 sata_device->satadev_addr.cport))); 12514 12515 if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) { 12516 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 12517 sata_device->satadev_addr.cport, 12518 sata_device->satadev_addr.pmport); 12519 ASSERT(pmportinfo != NULL); 12520 } 12521 12522 /* Get pointer to port-linked sata device info structure */ 12523 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 12524 if (sdinfo != NULL) { 12525 sdinfo->satadrv_state &= 12526 ~(SATA_STATE_PROBED | SATA_STATE_READY); 12527 sdinfo->satadrv_state |= SATA_STATE_PROBING; 12528 } else { 12529 /* No device to probe */ 12530 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12531 sata_device->satadev_addr.cport))); 12532 sata_device->satadev_type = SATA_DTYPE_NONE; 12533 sata_device->satadev_state = SATA_STATE_UNKNOWN; 12534 return (SATA_FAILURE); 12535 } 12536 /* 12537 * Need to issue both types of identify device command and 12538 * determine device type by examining retreived data/status. 12539 * First, ATA Identify Device. 12540 */ 12541 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 12542 new_sdinfo.satadrv_addr = sata_device->satadev_addr; 12543 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12544 sata_device->satadev_addr.cport))); 12545 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK; 12546 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 12547 if (rval == SATA_RETRY) { 12548 /* We may try to check for ATAPI device */ 12549 if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) { 12550 /* 12551 * HBA supports ATAPI - try to issue Identify Packet 12552 * Device command. 12553 */ 12554 new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI; 12555 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 12556 } 12557 } 12558 if (rval == SATA_SUCCESS) { 12559 /* 12560 * Got something responding positively to ATA Identify Device 12561 * or to Identify Packet Device cmd. 12562 * Save last used device type. 12563 */ 12564 sata_device->satadev_type = new_sdinfo.satadrv_type; 12565 12566 /* save device info, if possible */ 12567 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12568 sata_device->satadev_addr.cport))); 12569 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 12570 if (sdinfo == NULL) { 12571 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12572 sata_device->satadev_addr.cport))); 12573 return (SATA_FAILURE); 12574 } 12575 /* 12576 * Copy drive info into the port-linked drive info structure. 12577 */ 12578 *sdinfo = new_sdinfo; 12579 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 12580 sdinfo->satadrv_state |= SATA_STATE_PROBED; 12581 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 12582 SATA_CPORT_DEV_TYPE(sata_hba_inst, 12583 sata_device->satadev_addr.cport) = 12584 sdinfo->satadrv_type; 12585 else { /* SATA_ADDR_DPMPORT */ 12586 mutex_enter(&pmportinfo->pmport_mutex); 12587 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 12588 sata_device->satadev_addr.cport, 12589 sata_device->satadev_addr.pmport) = 12590 sdinfo->satadrv_type; 12591 mutex_exit(&pmportinfo->pmport_mutex); 12592 } 12593 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12594 sata_device->satadev_addr.cport))); 12595 return (SATA_SUCCESS); 12596 } 12597 12598 /* 12599 * It may be SATA_RETRY or SATA_FAILURE return. 12600 * Looks like we cannot determine the device type at this time. 12601 */ 12602 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 12603 sata_device->satadev_addr.cport))); 12604 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 12605 if (sdinfo != NULL) { 12606 sata_device->satadev_type = SATA_DTYPE_UNKNOWN; 12607 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 12608 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 12609 sdinfo->satadrv_state |= SATA_STATE_PROBED; 12610 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 12611 SATA_CPORT_DEV_TYPE(sata_hba_inst, 12612 sata_device->satadev_addr.cport) = 12613 SATA_DTYPE_UNKNOWN; 12614 else { 12615 /* SATA_ADDR_DPMPORT */ 12616 mutex_enter(&pmportinfo->pmport_mutex); 12617 if ((SATA_PMULT_INFO(sata_hba_inst, 12618 sata_device->satadev_addr.cport) != NULL) && 12619 (SATA_PMPORT_INFO(sata_hba_inst, 12620 sata_device->satadev_addr.cport, 12621 sata_device->satadev_addr.pmport) != NULL)) 12622 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 12623 sata_device->satadev_addr.cport, 12624 sata_device->satadev_addr.pmport) = 12625 SATA_DTYPE_UNKNOWN; 12626 mutex_exit(&pmportinfo->pmport_mutex); 12627 } 12628 } 12629 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 12630 sata_device->satadev_addr.cport))); 12631 return (rval); 12632 } 12633 12634 12635 /* 12636 * Get pointer to sata_drive_info structure. 12637 * 12638 * The sata_device has to contain address (cport, pmport and qualifier) for 12639 * specified sata_scsi structure. 12640 * 12641 * Returns NULL if device address is not valid for this HBA configuration. 12642 * Otherwise, returns a pointer to sata_drive_info structure. 12643 * 12644 * This function should be called with a port mutex held. 12645 */ 12646 static sata_drive_info_t * 12647 sata_get_device_info(sata_hba_inst_t *sata_hba_inst, 12648 sata_device_t *sata_device) 12649 { 12650 uint8_t cport = sata_device->satadev_addr.cport; 12651 uint8_t pmport = sata_device->satadev_addr.pmport; 12652 uint8_t qual = sata_device->satadev_addr.qual; 12653 12654 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 12655 return (NULL); 12656 12657 if (!(SATA_CPORT_STATE(sata_hba_inst, cport) & 12658 (SATA_STATE_PROBED | SATA_STATE_READY))) 12659 /* Port not probed yet */ 12660 return (NULL); 12661 12662 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE) 12663 return (NULL); 12664 12665 if (qual == SATA_ADDR_DCPORT) { 12666 /* Request for a device on a controller port */ 12667 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == 12668 SATA_DTYPE_PMULT) 12669 /* Port multiplier attached */ 12670 return (NULL); 12671 return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport)); 12672 } 12673 if (qual == SATA_ADDR_DPMPORT) { 12674 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 12675 SATA_DTYPE_PMULT) 12676 return (NULL); 12677 12678 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) 12679 return (NULL); 12680 12681 if (!(SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) & 12682 (SATA_STATE_PROBED | SATA_STATE_READY))) 12683 /* Port multiplier port not probed yet */ 12684 return (NULL); 12685 12686 return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport)); 12687 } 12688 12689 /* we should not get here */ 12690 return (NULL); 12691 } 12692 12693 12694 /* 12695 * sata_identify_device. 12696 * Send Identify Device command to SATA HBA driver. 12697 * If command executes successfully, update sata_drive_info structure pointed 12698 * to by sdinfo argument, including Identify Device data. 12699 * If command fails, invalidate data in sata_drive_info. 12700 * 12701 * Cannot be called from interrupt level. 12702 * 12703 * Returns: 12704 * SATA_SUCCESS if the device was identified as a supported device, 12705 * SATA_RETRY if the device was not identified but could be retried, 12706 * SATA_FAILURE if the device was not identified and identify attempt 12707 * should not be retried. 12708 */ 12709 static int 12710 sata_identify_device(sata_hba_inst_t *sata_hba_inst, 12711 sata_drive_info_t *sdinfo) 12712 { 12713 uint16_t cfg_word; 12714 int rval; 12715 12716 /* fetch device identify data */ 12717 if ((rval = sata_fetch_device_identify_data(sata_hba_inst, 12718 sdinfo)) != SATA_SUCCESS) 12719 goto fail_unknown; 12720 12721 cfg_word = sdinfo->satadrv_id.ai_config; 12722 12723 /* Set the correct device type */ 12724 if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) { 12725 sdinfo->satadrv_type = SATA_DTYPE_ATADISK; 12726 } else if (cfg_word == SATA_CFA_TYPE) { 12727 /* It's a Compact Flash media via CF-to-SATA HDD adapter */ 12728 sdinfo->satadrv_type = SATA_DTYPE_ATADISK; 12729 } else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) { 12730 switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) { 12731 case SATA_ATAPI_CDROM_DEV: 12732 sdinfo->satadrv_type = SATA_DTYPE_ATAPICD; 12733 break; 12734 case SATA_ATAPI_SQACC_DEV: 12735 sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE; 12736 break; 12737 case SATA_ATAPI_DIRACC_DEV: 12738 sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK; 12739 break; 12740 case SATA_ATAPI_PROC_DEV: 12741 sdinfo->satadrv_type = SATA_DTYPE_ATAPIPROC; 12742 break; 12743 default: 12744 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 12745 } 12746 } else { 12747 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 12748 } 12749 12750 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 12751 if (sdinfo->satadrv_capacity == 0) { 12752 /* Non-LBA disk. Too bad... */ 12753 sata_log(sata_hba_inst, CE_WARN, 12754 "SATA disk device at port %d does not support LBA", 12755 sdinfo->satadrv_addr.cport); 12756 rval = SATA_FAILURE; 12757 goto fail_unknown; 12758 } 12759 } 12760 #if 0 12761 /* Left for historical reason */ 12762 /* 12763 * Some initial version of SATA spec indicated that at least 12764 * UDMA mode 4 has to be supported. It is not metioned in 12765 * SerialATA 2.6, so this restriction is removed. 12766 */ 12767 /* Check for Ultra DMA modes 6 through 0 being supported */ 12768 for (i = 6; i >= 0; --i) { 12769 if (sdinfo->satadrv_id.ai_ultradma & (1 << i)) 12770 break; 12771 } 12772 12773 /* 12774 * At least UDMA 4 mode has to be supported. If mode 4 or 12775 * higher are not supported by the device, fail this 12776 * device. 12777 */ 12778 if (i < 4) { 12779 /* No required Ultra DMA mode supported */ 12780 sata_log(sata_hba_inst, CE_WARN, 12781 "SATA disk device at port %d does not support UDMA " 12782 "mode 4 or higher", sdinfo->satadrv_addr.cport); 12783 SATA_LOG_D((sata_hba_inst, CE_WARN, 12784 "mode 4 or higher required, %d supported", i)); 12785 rval = SATA_FAILURE; 12786 goto fail_unknown; 12787 } 12788 #endif 12789 12790 /* 12791 * For Disk devices, if it doesn't support UDMA mode, we would 12792 * like to return failure directly. 12793 */ 12794 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 12795 !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 && 12796 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) { 12797 sata_log(sata_hba_inst, CE_WARN, 12798 "SATA disk device at port %d does not support UDMA", 12799 sdinfo->satadrv_addr.cport); 12800 rval = SATA_FAILURE; 12801 goto fail_unknown; 12802 } 12803 12804 return (SATA_SUCCESS); 12805 12806 fail_unknown: 12807 /* Invalidate sata_drive_info ? */ 12808 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 12809 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 12810 return (rval); 12811 } 12812 12813 /* 12814 * Log/display device information 12815 */ 12816 static void 12817 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst, 12818 sata_drive_info_t *sdinfo) 12819 { 12820 int valid_version = 0; 12821 char msg_buf[MAXPATHLEN]; 12822 int i; 12823 12824 /* Show HBA path */ 12825 (void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf); 12826 12827 cmn_err(CE_CONT, "?%s :\n", msg_buf); 12828 12829 switch (sdinfo->satadrv_type) { 12830 case SATA_DTYPE_ATADISK: 12831 (void) sprintf(msg_buf, "SATA disk device at"); 12832 break; 12833 12834 case SATA_DTYPE_ATAPICD: 12835 (void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at"); 12836 break; 12837 12838 case SATA_DTYPE_ATAPITAPE: 12839 (void) sprintf(msg_buf, "SATA tape (ATAPI) device at"); 12840 break; 12841 12842 case SATA_DTYPE_ATAPIDISK: 12843 (void) sprintf(msg_buf, "SATA disk (ATAPI) device at"); 12844 break; 12845 12846 case SATA_DTYPE_ATAPIPROC: 12847 (void) sprintf(msg_buf, "SATA processor (ATAPI) device at"); 12848 break; 12849 12850 case SATA_DTYPE_UNKNOWN: 12851 (void) sprintf(msg_buf, 12852 "Unsupported SATA device type (cfg 0x%x) at ", 12853 sdinfo->satadrv_id.ai_config); 12854 break; 12855 } 12856 12857 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT) 12858 cmn_err(CE_CONT, "?\t%s port %d\n", 12859 msg_buf, sdinfo->satadrv_addr.cport); 12860 else 12861 cmn_err(CE_CONT, "?\t%s port %d:%d\n", 12862 msg_buf, sdinfo->satadrv_addr.cport, 12863 sdinfo->satadrv_addr.pmport); 12864 12865 bcopy(&sdinfo->satadrv_id.ai_model, msg_buf, 12866 sizeof (sdinfo->satadrv_id.ai_model)); 12867 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model)); 12868 msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0'; 12869 cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf); 12870 12871 bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf, 12872 sizeof (sdinfo->satadrv_id.ai_fw)); 12873 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw)); 12874 msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0'; 12875 cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf); 12876 12877 bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf, 12878 sizeof (sdinfo->satadrv_id.ai_drvser)); 12879 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser)); 12880 msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0'; 12881 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 12882 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 12883 } else { 12884 /* 12885 * Some drives do not implement serial number and may 12886 * violate the spec by providing spaces rather than zeros 12887 * in serial number field. Scan the buffer to detect it. 12888 */ 12889 for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) { 12890 if (msg_buf[i] != '\0' && msg_buf[i] != ' ') 12891 break; 12892 } 12893 if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) { 12894 cmn_err(CE_CONT, "?\tserial number - none\n"); 12895 } else { 12896 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 12897 } 12898 } 12899 12900 #ifdef SATA_DEBUG 12901 if (sdinfo->satadrv_id.ai_majorversion != 0 && 12902 sdinfo->satadrv_id.ai_majorversion != 0xffff) { 12903 int i; 12904 for (i = 14; i >= 2; i--) { 12905 if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) { 12906 valid_version = i; 12907 break; 12908 } 12909 } 12910 cmn_err(CE_CONT, 12911 "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n", 12912 valid_version, 12913 sdinfo->satadrv_id.ai_majorversion, 12914 sdinfo->satadrv_id.ai_minorversion); 12915 } 12916 #endif 12917 /* Log some info */ 12918 cmn_err(CE_CONT, "?\tsupported features:\n"); 12919 msg_buf[0] = '\0'; 12920 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 12921 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) 12922 (void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN); 12923 else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) 12924 (void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN); 12925 } 12926 if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA) 12927 (void) strlcat(msg_buf, "DMA", MAXPATHLEN); 12928 if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) 12929 (void) strlcat(msg_buf, ", Native Command Queueing", 12930 MAXPATHLEN); 12931 if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ) 12932 (void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN); 12933 if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) && 12934 (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED)) 12935 (void) strlcat(msg_buf, ", SMART", MAXPATHLEN); 12936 if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) && 12937 (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED)) 12938 (void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN); 12939 cmn_err(CE_CONT, "?\t %s\n", msg_buf); 12940 if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA3) 12941 cmn_err(CE_CONT, "?\tSATA Gen3 signaling speed (6.0Gbps)\n"); 12942 else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2) 12943 cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n"); 12944 else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1) 12945 cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n"); 12946 if (sdinfo->satadrv_features_support & 12947 (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) { 12948 msg_buf[0] = '\0'; 12949 (void) snprintf(msg_buf, MAXPATHLEN, 12950 "Supported queue depth %d", 12951 sdinfo->satadrv_queue_depth); 12952 if (!(sata_func_enable & 12953 (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ))) 12954 (void) strlcat(msg_buf, 12955 " - queueing disabled globally", MAXPATHLEN); 12956 else if (sdinfo->satadrv_queue_depth > 12957 sdinfo->satadrv_max_queue_depth) { 12958 (void) snprintf(&msg_buf[strlen(msg_buf)], 12959 MAXPATHLEN - strlen(msg_buf), ", limited to %d", 12960 (int)sdinfo->satadrv_max_queue_depth); 12961 } 12962 cmn_err(CE_CONT, "?\t%s\n", msg_buf); 12963 } 12964 12965 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 12966 (void) sprintf(msg_buf, "\tcapacity = %lu sectors\n", 12967 sdinfo->satadrv_capacity); 12968 cmn_err(CE_CONT, "?%s", msg_buf); 12969 } 12970 } 12971 12972 /* 12973 * Log/display port multiplier information 12974 * No Mutex should be hold. 12975 */ 12976 static void 12977 sata_show_pmult_info(sata_hba_inst_t *sata_hba_inst, 12978 sata_device_t *sata_device) 12979 { 12980 _NOTE(ARGUNUSED(sata_hba_inst)) 12981 12982 int cport = sata_device->satadev_addr.cport; 12983 sata_pmult_info_t *pmultinfo; 12984 char msg_buf[MAXPATHLEN]; 12985 uint32_t gscr0, gscr1, gscr2, gscr64; 12986 12987 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 12988 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 12989 if (pmultinfo == NULL) { 12990 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 12991 return; 12992 } 12993 12994 gscr0 = pmultinfo->pmult_gscr.gscr0; 12995 gscr1 = pmultinfo->pmult_gscr.gscr1; 12996 gscr2 = pmultinfo->pmult_gscr.gscr2; 12997 gscr64 = pmultinfo->pmult_gscr.gscr64; 12998 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 12999 13000 cmn_err(CE_CONT, "?Port Multiplier %d device-ports found at port %d", 13001 sata_device->satadev_add_info, sata_device->satadev_addr.cport); 13002 13003 (void) sprintf(msg_buf, "\tVendor_ID 0x%04x, Module_ID 0x%04x", 13004 gscr0 & 0xffff, (gscr0 >> 16) & 0xffff); 13005 cmn_err(CE_CONT, "?%s", msg_buf); 13006 13007 (void) strcpy(msg_buf, "\tSupport SATA PMP Spec "); 13008 if (gscr1 & (1 << 3)) 13009 (void) strlcat(msg_buf, "1.2", MAXPATHLEN); 13010 else if (gscr1 & (1 << 2)) 13011 (void) strlcat(msg_buf, "1.1", MAXPATHLEN); 13012 else if (gscr1 & (1 << 1)) 13013 (void) strlcat(msg_buf, "1.0", MAXPATHLEN); 13014 else 13015 (void) strlcat(msg_buf, "unknown", MAXPATHLEN); 13016 cmn_err(CE_CONT, "?%s", msg_buf); 13017 13018 (void) strcpy(msg_buf, "\tSupport "); 13019 if (gscr64 & (1 << 3)) 13020 (void) strlcat(msg_buf, "Asy-Notif, ", 13021 MAXPATHLEN); 13022 if (gscr64 & (1 << 2)) 13023 (void) strlcat(msg_buf, "Dyn-SSC, ", MAXPATHLEN); 13024 if (gscr64 & (1 << 1)) 13025 (void) strlcat(msg_buf, "Iss-PMREQ, ", MAXPATHLEN); 13026 if (gscr64 & (1 << 0)) 13027 (void) strlcat(msg_buf, "BIST", MAXPATHLEN); 13028 if ((gscr64 & 0xf) == 0) 13029 (void) strlcat(msg_buf, "nothing", MAXPATHLEN); 13030 cmn_err(CE_CONT, "?%s", msg_buf); 13031 13032 (void) sprintf(msg_buf, "\tNumber of exposed device fan-out ports: %d", 13033 gscr2 & SATA_PMULT_PORTNUM_MASK); 13034 cmn_err(CE_CONT, "?%s", msg_buf); 13035 } 13036 13037 /* 13038 * sata_save_drive_settings extracts current setting of the device and stores 13039 * it for future reference, in case the device setup would need to be restored 13040 * after the device reset. 13041 * 13042 * For all devices read ahead and write cache settings are saved, if the 13043 * device supports these features at all. 13044 * For ATAPI devices the Removable Media Status Notification setting is saved. 13045 */ 13046 static void 13047 sata_save_drive_settings(sata_drive_info_t *sdinfo) 13048 { 13049 if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) || 13050 SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) { 13051 13052 /* Current setting of Read Ahead (and Read Cache) */ 13053 if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id)) 13054 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 13055 else 13056 sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD; 13057 13058 /* Current setting of Write Cache */ 13059 if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id)) 13060 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 13061 else 13062 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 13063 } 13064 13065 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) { 13066 if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id)) 13067 sdinfo->satadrv_settings |= SATA_DEV_RMSN; 13068 else 13069 sdinfo->satadrv_settings &= ~SATA_DEV_RMSN; 13070 } 13071 } 13072 13073 13074 /* 13075 * sata_check_capacity function determines a disk capacity 13076 * and addressing mode (LBA28/LBA48) by examining a disk identify device data. 13077 * 13078 * NOTE: CHS mode is not supported! If a device does not support LBA, 13079 * this function is not called. 13080 * 13081 * Returns device capacity in number of blocks, i.e. largest addressable LBA+1 13082 */ 13083 static uint64_t 13084 sata_check_capacity(sata_drive_info_t *sdinfo) 13085 { 13086 uint64_t capacity = 0; 13087 int i; 13088 13089 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK || 13090 (sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT) == 0) 13091 /* Capacity valid only for LBA-addressable disk devices */ 13092 return (0); 13093 13094 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) && 13095 (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) && 13096 (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) { 13097 /* LBA48 mode supported and enabled */ 13098 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 | 13099 SATA_DEV_F_LBA28; 13100 for (i = 3; i >= 0; --i) { 13101 capacity <<= 16; 13102 capacity += sdinfo->satadrv_id.ai_addrsecxt[i]; 13103 } 13104 } else { 13105 capacity = sdinfo->satadrv_id.ai_addrsec[1]; 13106 capacity <<= 16; 13107 capacity += sdinfo->satadrv_id.ai_addrsec[0]; 13108 if (capacity >= 0x1000000) 13109 /* LBA28 mode */ 13110 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28; 13111 } 13112 return (capacity); 13113 } 13114 13115 13116 /* 13117 * Allocate consistent buffer for DMA transfer 13118 * 13119 * Cannot be called from interrupt level or with mutex held - it may sleep. 13120 * 13121 * Returns pointer to allocated buffer structure, or NULL if allocation failed. 13122 */ 13123 static struct buf * 13124 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len) 13125 { 13126 struct scsi_address ap; 13127 struct buf *bp; 13128 ddi_dma_attr_t cur_dma_attr; 13129 13130 ASSERT(spx->txlt_sata_pkt != NULL); 13131 ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran; 13132 ap.a_target = SATA_TO_SCSI_TARGET( 13133 spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport, 13134 spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport, 13135 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual); 13136 ap.a_lun = 0; 13137 13138 bp = scsi_alloc_consistent_buf(&ap, NULL, len, 13139 B_READ, SLEEP_FUNC, NULL); 13140 13141 if (bp != NULL) { 13142 /* Allocate DMA resources for this buffer */ 13143 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 13144 /* 13145 * We use a local version of the dma_attr, to account 13146 * for a device addressing limitations. 13147 * sata_adjust_dma_attr() will handle sdinfo == NULL which 13148 * will cause dma attributes to be adjusted to a lowest 13149 * acceptable level. 13150 */ 13151 sata_adjust_dma_attr(NULL, 13152 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 13153 13154 if (sata_dma_buf_setup(spx, PKT_CONSISTENT, 13155 SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) { 13156 scsi_free_consistent_buf(bp); 13157 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 13158 bp = NULL; 13159 } 13160 } 13161 return (bp); 13162 } 13163 13164 /* 13165 * Release local buffer (consistent buffer for DMA transfer) allocated 13166 * via sata_alloc_local_buffer(). 13167 */ 13168 static void 13169 sata_free_local_buffer(sata_pkt_txlate_t *spx) 13170 { 13171 ASSERT(spx->txlt_sata_pkt != NULL); 13172 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL); 13173 13174 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0; 13175 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL; 13176 13177 sata_common_free_dma_rsrcs(spx); 13178 13179 /* Free buffer */ 13180 scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp); 13181 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 13182 } 13183 13184 /* 13185 * Allocate sata_pkt 13186 * Pkt structure version and embedded strcutures version are initialized. 13187 * sata_pkt and sata_pkt_txlate structures are cross-linked. 13188 * 13189 * Since this may be called in interrupt context by sata_scsi_init_pkt, 13190 * callback argument determines if it can sleep or not. 13191 * Hence, it should not be called from interrupt context. 13192 * 13193 * If successful, non-NULL pointer to a sata pkt is returned. 13194 * Upon failure, NULL pointer is returned. 13195 */ 13196 static sata_pkt_t * 13197 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t)) 13198 { 13199 sata_pkt_t *spkt; 13200 int kmsflag; 13201 13202 kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP; 13203 spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag); 13204 if (spkt == NULL) { 13205 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 13206 "sata_pkt_alloc: failed")); 13207 return (NULL); 13208 } 13209 spkt->satapkt_rev = SATA_PKT_REV; 13210 spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV; 13211 spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 13212 spkt->satapkt_framework_private = spx; 13213 spx->txlt_sata_pkt = spkt; 13214 return (spkt); 13215 } 13216 13217 /* 13218 * Free sata pkt allocated via sata_pkt_alloc() 13219 */ 13220 static void 13221 sata_pkt_free(sata_pkt_txlate_t *spx) 13222 { 13223 ASSERT(spx->txlt_sata_pkt != NULL); 13224 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL); 13225 kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t)); 13226 spx->txlt_sata_pkt = NULL; 13227 } 13228 13229 13230 /* 13231 * Adjust DMA attributes. 13232 * SCSI cmds block count is up to 24 bits, SATA cmd block count vary 13233 * from 8 bits to 16 bits, depending on a command being used. 13234 * Limiting max block count arbitrarily to 256 for all read/write 13235 * commands may affects performance, so check both the device and 13236 * controller capability before adjusting dma attributes. 13237 */ 13238 void 13239 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr, 13240 ddi_dma_attr_t *adj_dma_attr) 13241 { 13242 uint32_t count_max; 13243 13244 /* Copy original attributes */ 13245 *adj_dma_attr = *dma_attr; 13246 /* 13247 * Things to consider: device addressing capability, 13248 * "excessive" controller DMA capabilities. 13249 * If a device is being probed/initialized, there are 13250 * no device info - use default limits then. 13251 */ 13252 if (sdinfo == NULL) { 13253 count_max = dma_attr->dma_attr_granular * 0x100; 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 return; 13259 } 13260 13261 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 13262 if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) { 13263 /* 13264 * 16-bit sector count may be used - we rely on 13265 * the assumption that only read and write cmds 13266 * will request more than 256 sectors worth of data 13267 */ 13268 count_max = adj_dma_attr->dma_attr_granular * 0x10000; 13269 } else { 13270 /* 13271 * 8-bit sector count will be used - default limits 13272 * for dma attributes 13273 */ 13274 count_max = adj_dma_attr->dma_attr_granular * 0x100; 13275 } 13276 /* 13277 * Adjust controler dma attributes, if necessary 13278 */ 13279 if (dma_attr->dma_attr_count_max > count_max) 13280 adj_dma_attr->dma_attr_count_max = count_max; 13281 if (dma_attr->dma_attr_maxxfer > count_max) 13282 adj_dma_attr->dma_attr_maxxfer = count_max; 13283 } 13284 } 13285 13286 13287 /* 13288 * Allocate DMA resources for the buffer 13289 * This function handles initial DMA resource allocation as well as 13290 * DMA window shift and may be called repeatedly for the same DMA window 13291 * until all DMA cookies in the DMA window are processed. 13292 * To guarantee that there is always a coherent set of cookies to process 13293 * by SATA HBA driver (observing alignment, device granularity, etc.), 13294 * the number of slots for DMA cookies is equal to lesser of a number of 13295 * cookies in a DMA window and a max number of scatter/gather entries. 13296 * 13297 * Returns DDI_SUCCESS upon successful operation. 13298 * Return failure code of a failing command or DDI_FAILURE when 13299 * internal cleanup failed. 13300 */ 13301 static int 13302 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags, 13303 int (*callback)(caddr_t), caddr_t arg, 13304 ddi_dma_attr_t *cur_dma_attr) 13305 { 13306 int rval; 13307 off_t offset; 13308 size_t size; 13309 int max_sg_len, req_len, i; 13310 uint_t dma_flags; 13311 struct buf *bp; 13312 uint64_t cur_txfer_len; 13313 13314 13315 ASSERT(spx->txlt_sata_pkt != NULL); 13316 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 13317 ASSERT(bp != NULL); 13318 13319 13320 if (spx->txlt_buf_dma_handle == NULL) { 13321 /* 13322 * No DMA resources allocated so far - this is a first call 13323 * for this sata pkt. 13324 */ 13325 rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst), 13326 cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle); 13327 13328 if (rval != DDI_SUCCESS) { 13329 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 13330 "sata_dma_buf_setup: no buf DMA resources %x", 13331 rval)); 13332 return (rval); 13333 } 13334 13335 if (bp->b_flags & B_READ) 13336 dma_flags = DDI_DMA_READ; 13337 else 13338 dma_flags = DDI_DMA_WRITE; 13339 13340 if (flags & PKT_CONSISTENT) 13341 dma_flags |= DDI_DMA_CONSISTENT; 13342 13343 if (flags & PKT_DMA_PARTIAL) 13344 dma_flags |= DDI_DMA_PARTIAL; 13345 13346 /* 13347 * Check buffer alignment and size against dma attributes 13348 * Consider dma_attr_align only. There may be requests 13349 * with the size lower than device granularity, but they 13350 * will not read/write from/to the device, so no adjustment 13351 * is necessary. The dma_attr_minxfer theoretically should 13352 * be considered, but no HBA driver is checking it. 13353 */ 13354 if (IS_P2ALIGNED(bp->b_un.b_addr, 13355 cur_dma_attr->dma_attr_align)) { 13356 rval = ddi_dma_buf_bind_handle( 13357 spx->txlt_buf_dma_handle, 13358 bp, dma_flags, callback, arg, 13359 &spx->txlt_dma_cookie, 13360 &spx->txlt_curwin_num_dma_cookies); 13361 } else { /* Buffer is not aligned */ 13362 13363 int (*ddicallback)(caddr_t); 13364 size_t bufsz; 13365 13366 /* Check id sleeping is allowed */ 13367 ddicallback = (callback == NULL_FUNC) ? 13368 DDI_DMA_DONTWAIT : DDI_DMA_SLEEP; 13369 13370 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 13371 "mis-aligned buffer: addr=0x%p, cnt=%lu", 13372 (void *)bp->b_un.b_addr, bp->b_bcount); 13373 13374 if (bp->b_flags & (B_PAGEIO|B_PHYS)) 13375 /* 13376 * CPU will need to access data in the buffer 13377 * (for copying) so map it. 13378 */ 13379 bp_mapin(bp); 13380 13381 ASSERT(spx->txlt_tmp_buf == NULL); 13382 13383 /* Buffer may be padded by ddi_dma_mem_alloc()! */ 13384 rval = ddi_dma_mem_alloc( 13385 spx->txlt_buf_dma_handle, 13386 bp->b_bcount, 13387 &sata_acc_attr, 13388 DDI_DMA_STREAMING, 13389 ddicallback, NULL, 13390 &spx->txlt_tmp_buf, 13391 &bufsz, 13392 &spx->txlt_tmp_buf_handle); 13393 13394 if (rval != DDI_SUCCESS) { 13395 /* DMA mapping failed */ 13396 (void) ddi_dma_free_handle( 13397 &spx->txlt_buf_dma_handle); 13398 spx->txlt_buf_dma_handle = NULL; 13399 #ifdef SATA_DEBUG 13400 mbuffail_count++; 13401 #endif 13402 SATADBG1(SATA_DBG_DMA_SETUP, 13403 spx->txlt_sata_hba_inst, 13404 "sata_dma_buf_setup: " 13405 "buf dma mem alloc failed %x\n", rval); 13406 return (rval); 13407 } 13408 ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf, 13409 cur_dma_attr->dma_attr_align)); 13410 13411 #ifdef SATA_DEBUG 13412 mbuf_count++; 13413 13414 if (bp->b_bcount != bufsz) 13415 /* 13416 * This will require special handling, because 13417 * DMA cookies will be based on the temporary 13418 * buffer size, not the original buffer 13419 * b_bcount, so the residue may have to 13420 * be counted differently. 13421 */ 13422 SATADBG2(SATA_DBG_DMA_SETUP, 13423 spx->txlt_sata_hba_inst, 13424 "sata_dma_buf_setup: bp size %x != " 13425 "bufsz %x\n", bp->b_bcount, bufsz); 13426 #endif 13427 if (dma_flags & DDI_DMA_WRITE) { 13428 /* 13429 * Write operation - copy data into 13430 * an aligned temporary buffer. Buffer will be 13431 * synced for device by ddi_dma_addr_bind_handle 13432 */ 13433 bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf, 13434 bp->b_bcount); 13435 } 13436 13437 rval = ddi_dma_addr_bind_handle( 13438 spx->txlt_buf_dma_handle, 13439 NULL, 13440 spx->txlt_tmp_buf, 13441 bufsz, dma_flags, ddicallback, 0, 13442 &spx->txlt_dma_cookie, 13443 &spx->txlt_curwin_num_dma_cookies); 13444 } 13445 13446 switch (rval) { 13447 case DDI_DMA_PARTIAL_MAP: 13448 SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 13449 "sata_dma_buf_setup: DMA Partial Map\n", NULL); 13450 /* 13451 * Partial DMA mapping. 13452 * Retrieve number of DMA windows for this request. 13453 */ 13454 if (ddi_dma_numwin(spx->txlt_buf_dma_handle, 13455 &spx->txlt_num_dma_win) != DDI_SUCCESS) { 13456 if (spx->txlt_tmp_buf != NULL) { 13457 ddi_dma_mem_free( 13458 &spx->txlt_tmp_buf_handle); 13459 spx->txlt_tmp_buf = NULL; 13460 } 13461 (void) ddi_dma_unbind_handle( 13462 spx->txlt_buf_dma_handle); 13463 (void) ddi_dma_free_handle( 13464 &spx->txlt_buf_dma_handle); 13465 spx->txlt_buf_dma_handle = NULL; 13466 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 13467 "sata_dma_buf_setup: numwin failed\n")); 13468 return (DDI_FAILURE); 13469 } 13470 SATADBG2(SATA_DBG_DMA_SETUP, 13471 spx->txlt_sata_hba_inst, 13472 "sata_dma_buf_setup: windows: %d, cookies: %d\n", 13473 spx->txlt_num_dma_win, 13474 spx->txlt_curwin_num_dma_cookies); 13475 spx->txlt_cur_dma_win = 0; 13476 break; 13477 13478 case DDI_DMA_MAPPED: 13479 /* DMA fully mapped */ 13480 spx->txlt_num_dma_win = 1; 13481 spx->txlt_cur_dma_win = 0; 13482 SATADBG1(SATA_DBG_DMA_SETUP, 13483 spx->txlt_sata_hba_inst, 13484 "sata_dma_buf_setup: windows: 1 " 13485 "cookies: %d\n", spx->txlt_curwin_num_dma_cookies); 13486 break; 13487 13488 default: 13489 /* DMA mapping failed */ 13490 if (spx->txlt_tmp_buf != NULL) { 13491 ddi_dma_mem_free( 13492 &spx->txlt_tmp_buf_handle); 13493 spx->txlt_tmp_buf = NULL; 13494 } 13495 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 13496 spx->txlt_buf_dma_handle = NULL; 13497 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 13498 "sata_dma_buf_setup: buf dma handle binding " 13499 "failed %x\n", rval)); 13500 return (rval); 13501 } 13502 spx->txlt_curwin_processed_dma_cookies = 0; 13503 spx->txlt_dma_cookie_list = NULL; 13504 } else { 13505 /* 13506 * DMA setup is reused. Check if we need to process more 13507 * cookies in current window, or to get next window, if any. 13508 */ 13509 13510 ASSERT(spx->txlt_curwin_processed_dma_cookies <= 13511 spx->txlt_curwin_num_dma_cookies); 13512 13513 if (spx->txlt_curwin_processed_dma_cookies == 13514 spx->txlt_curwin_num_dma_cookies) { 13515 /* 13516 * All cookies from current DMA window were processed. 13517 * Get next DMA window. 13518 */ 13519 spx->txlt_cur_dma_win++; 13520 if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) { 13521 (void) ddi_dma_getwin(spx->txlt_buf_dma_handle, 13522 spx->txlt_cur_dma_win, &offset, &size, 13523 &spx->txlt_dma_cookie, 13524 &spx->txlt_curwin_num_dma_cookies); 13525 spx->txlt_curwin_processed_dma_cookies = 0; 13526 } else { 13527 /* No more windows! End of request! */ 13528 /* What to do? - panic for now */ 13529 ASSERT(spx->txlt_cur_dma_win >= 13530 spx->txlt_num_dma_win); 13531 13532 spx->txlt_curwin_num_dma_cookies = 0; 13533 spx->txlt_curwin_processed_dma_cookies = 0; 13534 spx->txlt_sata_pkt-> 13535 satapkt_cmd.satacmd_num_dma_cookies = 0; 13536 return (DDI_SUCCESS); 13537 } 13538 } 13539 } 13540 /* There better be at least one DMA cookie outstanding */ 13541 ASSERT((spx->txlt_curwin_num_dma_cookies - 13542 spx->txlt_curwin_processed_dma_cookies) > 0); 13543 13544 if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) { 13545 /* The default cookie slot was used in previous run */ 13546 ASSERT(spx->txlt_curwin_processed_dma_cookies == 0); 13547 spx->txlt_dma_cookie_list = NULL; 13548 spx->txlt_dma_cookie_list_len = 0; 13549 } 13550 if (spx->txlt_curwin_processed_dma_cookies == 0) { 13551 /* 13552 * Processing a new DMA window - set-up dma cookies list. 13553 * We may reuse previously allocated cookie array if it is 13554 * possible. 13555 */ 13556 if (spx->txlt_dma_cookie_list != NULL && 13557 spx->txlt_dma_cookie_list_len < 13558 spx->txlt_curwin_num_dma_cookies) { 13559 /* 13560 * New DMA window contains more cookies than 13561 * the previous one. We need larger cookie list - free 13562 * the old one. 13563 */ 13564 (void) kmem_free(spx->txlt_dma_cookie_list, 13565 spx->txlt_dma_cookie_list_len * 13566 sizeof (ddi_dma_cookie_t)); 13567 spx->txlt_dma_cookie_list = NULL; 13568 spx->txlt_dma_cookie_list_len = 0; 13569 } 13570 if (spx->txlt_dma_cookie_list == NULL) { 13571 /* 13572 * Calculate lesser of number of cookies in this 13573 * DMA window and number of s/g entries. 13574 */ 13575 max_sg_len = cur_dma_attr->dma_attr_sgllen; 13576 req_len = MIN(max_sg_len, 13577 spx->txlt_curwin_num_dma_cookies); 13578 13579 /* Allocate new dma cookie array if necessary */ 13580 if (req_len == 1) { 13581 /* Only one cookie - no need for a list */ 13582 spx->txlt_dma_cookie_list = 13583 &spx->txlt_dma_cookie; 13584 spx->txlt_dma_cookie_list_len = 1; 13585 } else { 13586 /* 13587 * More than one cookie - try to allocate space. 13588 */ 13589 spx->txlt_dma_cookie_list = kmem_zalloc( 13590 sizeof (ddi_dma_cookie_t) * req_len, 13591 callback == NULL_FUNC ? KM_NOSLEEP : 13592 KM_SLEEP); 13593 if (spx->txlt_dma_cookie_list == NULL) { 13594 SATADBG1(SATA_DBG_DMA_SETUP, 13595 spx->txlt_sata_hba_inst, 13596 "sata_dma_buf_setup: cookie list " 13597 "allocation failed\n", NULL); 13598 /* 13599 * We could not allocate space for 13600 * neccessary number of dma cookies in 13601 * this window, so we fail this request. 13602 * Next invocation would try again to 13603 * allocate space for cookie list. 13604 * Note:Packet residue was not modified. 13605 */ 13606 return (DDI_DMA_NORESOURCES); 13607 } else { 13608 spx->txlt_dma_cookie_list_len = req_len; 13609 } 13610 } 13611 } 13612 /* 13613 * Fetch DMA cookies into cookie list in sata_pkt_txlate. 13614 * First cookie was already fetched. 13615 */ 13616 *(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie; 13617 cur_txfer_len = 13618 (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size; 13619 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1; 13620 spx->txlt_curwin_processed_dma_cookies++; 13621 for (i = 1; (i < spx->txlt_dma_cookie_list_len) && 13622 (i < spx->txlt_curwin_num_dma_cookies); i++) { 13623 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 13624 &spx->txlt_dma_cookie_list[i]); 13625 cur_txfer_len += 13626 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 13627 spx->txlt_curwin_processed_dma_cookies++; 13628 spx->txlt_sata_pkt-> 13629 satapkt_cmd.satacmd_num_dma_cookies += 1; 13630 } 13631 } else { 13632 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 13633 "sata_dma_buf_setup: sliding within DMA window, " 13634 "cur cookie %d, total cookies %d\n", 13635 spx->txlt_curwin_processed_dma_cookies, 13636 spx->txlt_curwin_num_dma_cookies); 13637 13638 /* 13639 * Not all cookies from the current dma window were used because 13640 * of s/g limitation. 13641 * There is no need to re-size the list - it was set at 13642 * optimal size, or only default entry is used (s/g = 1). 13643 */ 13644 if (spx->txlt_dma_cookie_list == NULL) { 13645 spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie; 13646 spx->txlt_dma_cookie_list_len = 1; 13647 } 13648 /* 13649 * Since we are processing remaining cookies in a DMA window, 13650 * there may be less of them than the number of entries in the 13651 * current dma cookie list. 13652 */ 13653 req_len = MIN(spx->txlt_dma_cookie_list_len, 13654 (spx->txlt_curwin_num_dma_cookies - 13655 spx->txlt_curwin_processed_dma_cookies)); 13656 13657 /* Fetch the next batch of cookies */ 13658 for (i = 0, cur_txfer_len = 0; i < req_len; i++) { 13659 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 13660 &spx->txlt_dma_cookie_list[i]); 13661 cur_txfer_len += 13662 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 13663 spx->txlt_sata_pkt-> 13664 satapkt_cmd.satacmd_num_dma_cookies++; 13665 spx->txlt_curwin_processed_dma_cookies++; 13666 } 13667 } 13668 13669 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0); 13670 13671 /* Point sata_cmd to the cookie list */ 13672 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = 13673 &spx->txlt_dma_cookie_list[0]; 13674 13675 /* Remember number of DMA cookies passed in sata packet */ 13676 spx->txlt_num_dma_cookies = 13677 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies; 13678 13679 ASSERT(cur_txfer_len != 0); 13680 if (cur_txfer_len <= bp->b_bcount) 13681 spx->txlt_total_residue -= cur_txfer_len; 13682 else { 13683 /* 13684 * Temporary DMA buffer has been padded by 13685 * ddi_dma_mem_alloc()! 13686 * This requires special handling, because DMA cookies are 13687 * based on the temporary buffer size, not the b_bcount, 13688 * and we have extra bytes to transfer - but the packet 13689 * residue has to stay correct because we will copy only 13690 * the requested number of bytes. 13691 */ 13692 spx->txlt_total_residue -= bp->b_bcount; 13693 } 13694 13695 return (DDI_SUCCESS); 13696 } 13697 13698 /* 13699 * Common routine for releasing DMA resources 13700 */ 13701 static void 13702 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx) 13703 { 13704 if (spx->txlt_buf_dma_handle != NULL) { 13705 if (spx->txlt_tmp_buf != NULL) { 13706 /* 13707 * Intermediate DMA buffer was allocated. 13708 * Free allocated buffer and associated access handle. 13709 */ 13710 ddi_dma_mem_free(&spx->txlt_tmp_buf_handle); 13711 spx->txlt_tmp_buf = NULL; 13712 } 13713 /* 13714 * Free DMA resources - cookies and handles 13715 */ 13716 /* ASSERT(spx->txlt_dma_cookie_list != NULL); */ 13717 if (spx->txlt_dma_cookie_list != NULL) { 13718 if (spx->txlt_dma_cookie_list != 13719 &spx->txlt_dma_cookie) { 13720 (void) kmem_free(spx->txlt_dma_cookie_list, 13721 spx->txlt_dma_cookie_list_len * 13722 sizeof (ddi_dma_cookie_t)); 13723 spx->txlt_dma_cookie_list = NULL; 13724 } 13725 } 13726 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle); 13727 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 13728 spx->txlt_buf_dma_handle = NULL; 13729 } 13730 } 13731 13732 /* 13733 * Free DMA resources 13734 * Used by the HBA driver to release DMA resources that it does not use. 13735 * 13736 * Returns Void 13737 */ 13738 void 13739 sata_free_dma_resources(sata_pkt_t *sata_pkt) 13740 { 13741 sata_pkt_txlate_t *spx; 13742 13743 if (sata_pkt == NULL) 13744 return; 13745 13746 spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 13747 13748 sata_common_free_dma_rsrcs(spx); 13749 } 13750 13751 /* 13752 * Fetch Device Identify data. 13753 * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type) 13754 * command to a device and get the device identify data. 13755 * The device_info structure has to be set to device type (for selecting proper 13756 * device identify command). 13757 * 13758 * Returns: 13759 * SATA_SUCCESS if cmd succeeded 13760 * SATA_RETRY if cmd was rejected and could be retried, 13761 * SATA_FAILURE if cmd failed and should not be retried (port error) 13762 * 13763 * Cannot be called in an interrupt context. 13764 */ 13765 13766 static int 13767 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst, 13768 sata_drive_info_t *sdinfo) 13769 { 13770 struct buf *bp; 13771 sata_pkt_t *spkt; 13772 sata_cmd_t *scmd; 13773 sata_pkt_txlate_t *spx; 13774 int rval; 13775 dev_info_t *dip = SATA_DIP(sata_hba_inst); 13776 13777 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13778 spx->txlt_sata_hba_inst = sata_hba_inst; 13779 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13780 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13781 if (spkt == NULL) { 13782 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13783 return (SATA_RETRY); /* may retry later */ 13784 } 13785 /* address is needed now */ 13786 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13787 13788 /* 13789 * Allocate buffer for Identify Data return data 13790 */ 13791 bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t)); 13792 if (bp == NULL) { 13793 sata_pkt_free(spx); 13794 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13795 SATA_LOG_D((sata_hba_inst, CE_WARN, 13796 "sata_fetch_device_identify_data: " 13797 "cannot allocate buffer for ID")); 13798 return (SATA_RETRY); /* may retry later */ 13799 } 13800 13801 /* Fill sata_pkt */ 13802 sdinfo->satadrv_state = SATA_STATE_PROBING; 13803 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13804 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13805 /* Synchronous mode, no callback */ 13806 spkt->satapkt_comp = NULL; 13807 /* Timeout 30s */ 13808 spkt->satapkt_time = sata_default_pkt_time; 13809 13810 scmd = &spkt->satapkt_cmd; 13811 scmd->satacmd_bp = bp; 13812 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 13813 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 13814 13815 /* Build Identify Device cmd in the sata_pkt */ 13816 scmd->satacmd_addr_type = 0; /* N/A */ 13817 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 13818 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 13819 scmd->satacmd_lba_mid_lsb = 0; /* N/A */ 13820 scmd->satacmd_lba_high_lsb = 0; /* N/A */ 13821 scmd->satacmd_features_reg = 0; /* N/A */ 13822 scmd->satacmd_device_reg = 0; /* Always device 0 */ 13823 if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) { 13824 /* Identify Packet Device cmd */ 13825 scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE; 13826 } else { 13827 /* Identify Device cmd - mandatory for all other devices */ 13828 scmd->satacmd_cmd_reg = SATAC_ID_DEVICE; 13829 } 13830 13831 /* Send pkt to SATA HBA driver */ 13832 rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt); 13833 13834 #ifdef SATA_INJECT_FAULTS 13835 sata_inject_pkt_fault(spkt, &rval, sata_fault_type); 13836 #endif 13837 13838 if (rval == SATA_TRAN_ACCEPTED && 13839 spkt->satapkt_reason == SATA_PKT_COMPLETED) { 13840 if (spx->txlt_buf_dma_handle != NULL) { 13841 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 13842 DDI_DMA_SYNC_FORKERNEL); 13843 ASSERT(rval == DDI_SUCCESS); 13844 if (sata_check_for_dma_error(dip, spx)) { 13845 ddi_fm_service_impact(dip, 13846 DDI_SERVICE_UNAFFECTED); 13847 rval = SATA_RETRY; 13848 goto fail; 13849 } 13850 13851 } 13852 if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config & 13853 SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) { 13854 SATA_LOG_D((sata_hba_inst, CE_WARN, 13855 "SATA disk device at port %d - " 13856 "partial Identify Data", 13857 sdinfo->satadrv_addr.cport)); 13858 rval = SATA_RETRY; /* may retry later */ 13859 goto fail; 13860 } 13861 /* Update sata_drive_info */ 13862 bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id, 13863 sizeof (sata_id_t)); 13864 13865 sdinfo->satadrv_features_support = 0; 13866 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 13867 /* 13868 * Retrieve capacity (disks only) and addressing mode 13869 */ 13870 sdinfo->satadrv_capacity = sata_check_capacity(sdinfo); 13871 } else { 13872 /* 13873 * For ATAPI devices one would have to issue 13874 * Get Capacity cmd for media capacity. Not here. 13875 */ 13876 sdinfo->satadrv_capacity = 0; 13877 /* 13878 * Check what cdb length is supported 13879 */ 13880 if ((sdinfo->satadrv_id.ai_config & 13881 SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B) 13882 sdinfo->satadrv_atapi_cdb_len = 16; 13883 else 13884 sdinfo->satadrv_atapi_cdb_len = 12; 13885 } 13886 /* Setup supported features flags */ 13887 if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) 13888 sdinfo->satadrv_features_support |= SATA_DEV_F_DMA; 13889 13890 /* Check for SATA GEN and NCQ support */ 13891 if (sdinfo->satadrv_id.ai_satacap != 0 && 13892 sdinfo->satadrv_id.ai_satacap != 0xffff) { 13893 /* SATA compliance */ 13894 if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ) 13895 sdinfo->satadrv_features_support |= 13896 SATA_DEV_F_NCQ; 13897 if (sdinfo->satadrv_id.ai_satacap & 13898 (SATA_1_SPEED | SATA_2_SPEED | SATA_3_SPEED)) { 13899 if (sdinfo->satadrv_id.ai_satacap & 13900 SATA_3_SPEED) 13901 sdinfo->satadrv_features_support |= 13902 SATA_DEV_F_SATA3; 13903 if (sdinfo->satadrv_id.ai_satacap & 13904 SATA_2_SPEED) 13905 sdinfo->satadrv_features_support |= 13906 SATA_DEV_F_SATA2; 13907 if (sdinfo->satadrv_id.ai_satacap & 13908 SATA_1_SPEED) 13909 sdinfo->satadrv_features_support |= 13910 SATA_DEV_F_SATA1; 13911 } else { 13912 sdinfo->satadrv_features_support |= 13913 SATA_DEV_F_SATA1; 13914 } 13915 } 13916 if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) && 13917 (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD)) 13918 sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ; 13919 13920 sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth; 13921 if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) || 13922 (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) { 13923 ++sdinfo->satadrv_queue_depth; 13924 /* Adjust according to controller capabilities */ 13925 sdinfo->satadrv_max_queue_depth = MIN( 13926 sdinfo->satadrv_queue_depth, 13927 SATA_QDEPTH(sata_hba_inst)); 13928 /* Adjust according to global queue depth limit */ 13929 sdinfo->satadrv_max_queue_depth = MIN( 13930 sdinfo->satadrv_max_queue_depth, 13931 sata_current_max_qdepth); 13932 if (sdinfo->satadrv_max_queue_depth == 0) 13933 sdinfo->satadrv_max_queue_depth = 1; 13934 } else 13935 sdinfo->satadrv_max_queue_depth = 1; 13936 13937 rval = SATA_SUCCESS; 13938 } else { 13939 /* 13940 * Woops, no Identify Data. 13941 */ 13942 if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) { 13943 rval = SATA_RETRY; /* may retry later */ 13944 } else if (rval == SATA_TRAN_ACCEPTED) { 13945 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR || 13946 spkt->satapkt_reason == SATA_PKT_ABORTED || 13947 spkt->satapkt_reason == SATA_PKT_TIMEOUT || 13948 spkt->satapkt_reason == SATA_PKT_RESET) 13949 rval = SATA_RETRY; /* may retry later */ 13950 else 13951 rval = SATA_FAILURE; 13952 } else { 13953 rval = SATA_FAILURE; 13954 } 13955 } 13956 fail: 13957 /* Free allocated resources */ 13958 sata_free_local_buffer(spx); 13959 sata_pkt_free(spx); 13960 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13961 13962 return (rval); 13963 } 13964 13965 13966 /* 13967 * Some devices may not come-up with default DMA mode (UDMA or MWDMA). 13968 * UDMA mode is checked first, followed by MWDMA mode. 13969 * set correctly, so this function is setting it to the highest supported level. 13970 * Older SATA spec required that the device supports at least DMA 4 mode and 13971 * UDMA mode is selected. It is not mentioned in SerialATA 2.6, so this 13972 * restriction has been removed. 13973 * 13974 * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported. 13975 * Returns SATA_FAILURE if proper DMA mode could not be selected. 13976 * 13977 * NOTE: This function should be called only if DMA mode is supported. 13978 */ 13979 static int 13980 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo) 13981 { 13982 sata_pkt_t *spkt; 13983 sata_cmd_t *scmd; 13984 sata_pkt_txlate_t *spx; 13985 int i, mode; 13986 uint8_t subcmd; 13987 int rval = SATA_SUCCESS; 13988 13989 ASSERT(sdinfo != NULL); 13990 ASSERT(sata_hba_inst != NULL); 13991 13992 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 && 13993 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) { 13994 /* Find highest Ultra DMA mode supported */ 13995 for (mode = 6; mode >= 0; --mode) { 13996 if (sdinfo->satadrv_id.ai_ultradma & (1 << mode)) 13997 break; 13998 } 13999 #if 0 14000 /* Left for historical reasons */ 14001 /* 14002 * Some initial version of SATA spec indicated that at least 14003 * UDMA mode 4 has to be supported. It is not mentioned in 14004 * SerialATA 2.6, so this restriction is removed. 14005 */ 14006 if (mode < 4) 14007 return (SATA_FAILURE); 14008 #endif 14009 14010 /* 14011 * For disk, we're still going to set DMA mode whatever is 14012 * selected by default 14013 * 14014 * We saw an old maxtor sata drive will select Ultra DMA and 14015 * Multi-Word DMA simultaneouly by default, which is going 14016 * to cause DMA command timed out, so we need to select DMA 14017 * mode even when it's already done by default 14018 */ 14019 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) { 14020 14021 /* Find UDMA mode currently selected */ 14022 for (i = 6; i >= 0; --i) { 14023 if (sdinfo->satadrv_id.ai_ultradma & 14024 (1 << (i + 8))) 14025 break; 14026 } 14027 if (i >= mode) 14028 /* Nothing to do */ 14029 return (SATA_SUCCESS); 14030 } 14031 14032 subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA; 14033 14034 } else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) { 14035 /* Find highest MultiWord DMA mode supported */ 14036 for (mode = 2; mode >= 0; --mode) { 14037 if (sdinfo->satadrv_id.ai_dworddma & (1 << mode)) 14038 break; 14039 } 14040 14041 /* 14042 * For disk, We're still going to set DMA mode whatever is 14043 * selected by default 14044 * 14045 * We saw an old maxtor sata drive will select Ultra DMA and 14046 * Multi-Word DMA simultaneouly by default, which is going 14047 * to cause DMA command timed out, so we need to select DMA 14048 * mode even when it's already done by default 14049 */ 14050 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) { 14051 14052 /* Find highest MultiWord DMA mode selected */ 14053 for (i = 2; i >= 0; --i) { 14054 if (sdinfo->satadrv_id.ai_dworddma & 14055 (1 << (i + 8))) 14056 break; 14057 } 14058 if (i >= mode) 14059 /* Nothing to do */ 14060 return (SATA_SUCCESS); 14061 } 14062 14063 subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA; 14064 } else 14065 return (SATA_SUCCESS); 14066 14067 /* 14068 * Set DMA mode via SET FEATURES COMMAND. 14069 * Prepare packet for SET FEATURES COMMAND. 14070 */ 14071 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 14072 spx->txlt_sata_hba_inst = sata_hba_inst; 14073 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 14074 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 14075 if (spkt == NULL) { 14076 SATA_LOG_D((sata_hba_inst, CE_WARN, 14077 "sata_set_dma_mode: could not set DMA mode %d", mode)); 14078 rval = SATA_FAILURE; 14079 goto done; 14080 } 14081 /* Fill sata_pkt */ 14082 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 14083 /* Timeout 30s */ 14084 spkt->satapkt_time = sata_default_pkt_time; 14085 /* Synchronous mode, no callback, interrupts */ 14086 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 14087 spkt->satapkt_comp = NULL; 14088 scmd = &spkt->satapkt_cmd; 14089 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 14090 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 14091 scmd->satacmd_addr_type = 0; 14092 scmd->satacmd_device_reg = 0; 14093 scmd->satacmd_status_reg = 0; 14094 scmd->satacmd_error_reg = 0; 14095 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 14096 scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE; 14097 scmd->satacmd_sec_count_lsb = subcmd | mode; 14098 14099 /* Transfer command to HBA */ 14100 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 14101 spkt) != SATA_TRAN_ACCEPTED || 14102 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 14103 /* Pkt execution failed */ 14104 rval = SATA_FAILURE; 14105 } 14106 done: 14107 14108 /* Free allocated resources */ 14109 if (spkt != NULL) 14110 sata_pkt_free(spx); 14111 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 14112 14113 return (rval); 14114 } 14115 14116 14117 /* 14118 * Set device caching mode. 14119 * One of the following operations should be specified: 14120 * SATAC_SF_ENABLE_READ_AHEAD 14121 * SATAC_SF_DISABLE_READ_AHEAD 14122 * SATAC_SF_ENABLE_WRITE_CACHE 14123 * SATAC_SF_DISABLE_WRITE_CACHE 14124 * 14125 * If operation fails, system log messgage is emitted. 14126 * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if 14127 * command was sent but did not succeed, and SATA_FAILURE otherwise. 14128 */ 14129 14130 static int 14131 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 14132 int cache_op) 14133 { 14134 sata_pkt_t *spkt; 14135 sata_cmd_t *scmd; 14136 sata_pkt_txlate_t *spx; 14137 int rval = SATA_SUCCESS; 14138 int hba_rval; 14139 char *infop = NULL; 14140 14141 ASSERT(sdinfo != NULL); 14142 ASSERT(sata_hba_inst != NULL); 14143 ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD || 14144 cache_op == SATAC_SF_DISABLE_READ_AHEAD || 14145 cache_op == SATAC_SF_ENABLE_WRITE_CACHE || 14146 cache_op == SATAC_SF_DISABLE_WRITE_CACHE); 14147 14148 14149 /* Prepare packet for SET FEATURES COMMAND */ 14150 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 14151 spx->txlt_sata_hba_inst = sata_hba_inst; 14152 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 14153 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 14154 if (spkt == NULL) { 14155 rval = SATA_FAILURE; 14156 goto failure; 14157 } 14158 /* Fill sata_pkt */ 14159 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 14160 /* Timeout 30s */ 14161 spkt->satapkt_time = sata_default_pkt_time; 14162 /* Synchronous mode, no callback, interrupts */ 14163 spkt->satapkt_op_mode = 14164 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 14165 spkt->satapkt_comp = NULL; 14166 scmd = &spkt->satapkt_cmd; 14167 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 14168 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 14169 scmd->satacmd_addr_type = 0; 14170 scmd->satacmd_device_reg = 0; 14171 scmd->satacmd_status_reg = 0; 14172 scmd->satacmd_error_reg = 0; 14173 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 14174 scmd->satacmd_features_reg = cache_op; 14175 14176 /* Transfer command to HBA */ 14177 hba_rval = (*SATA_START_FUNC(sata_hba_inst))( 14178 SATA_DIP(sata_hba_inst), spkt); 14179 14180 #ifdef SATA_INJECT_FAULTS 14181 sata_inject_pkt_fault(spkt, &rval, sata_fault_type); 14182 #endif 14183 14184 if ((hba_rval != SATA_TRAN_ACCEPTED) || 14185 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 14186 /* Pkt execution failed */ 14187 switch (cache_op) { 14188 case SATAC_SF_ENABLE_READ_AHEAD: 14189 infop = "enabling read ahead failed"; 14190 break; 14191 case SATAC_SF_DISABLE_READ_AHEAD: 14192 infop = "disabling read ahead failed"; 14193 break; 14194 case SATAC_SF_ENABLE_WRITE_CACHE: 14195 infop = "enabling write cache failed"; 14196 break; 14197 case SATAC_SF_DISABLE_WRITE_CACHE: 14198 infop = "disabling write cache failed"; 14199 break; 14200 } 14201 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 14202 rval = SATA_RETRY; 14203 } 14204 failure: 14205 /* Free allocated resources */ 14206 if (spkt != NULL) 14207 sata_pkt_free(spx); 14208 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 14209 return (rval); 14210 } 14211 14212 /* 14213 * Set Removable Media Status Notification (enable/disable) 14214 * state == 0 , disable 14215 * state != 0 , enable 14216 * 14217 * If operation fails, system log messgage is emitted. 14218 * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise. 14219 */ 14220 14221 static int 14222 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 14223 int state) 14224 { 14225 sata_pkt_t *spkt; 14226 sata_cmd_t *scmd; 14227 sata_pkt_txlate_t *spx; 14228 int rval = SATA_SUCCESS; 14229 char *infop; 14230 14231 ASSERT(sdinfo != NULL); 14232 ASSERT(sata_hba_inst != NULL); 14233 14234 /* Prepare packet for SET FEATURES COMMAND */ 14235 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 14236 spx->txlt_sata_hba_inst = sata_hba_inst; 14237 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 14238 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 14239 if (spkt == NULL) { 14240 rval = SATA_FAILURE; 14241 goto failure; 14242 } 14243 /* Fill sata_pkt */ 14244 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 14245 /* Timeout 30s */ 14246 spkt->satapkt_time = sata_default_pkt_time; 14247 /* Synchronous mode, no callback, interrupts */ 14248 spkt->satapkt_op_mode = 14249 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 14250 spkt->satapkt_comp = NULL; 14251 scmd = &spkt->satapkt_cmd; 14252 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 14253 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 14254 scmd->satacmd_addr_type = 0; 14255 scmd->satacmd_device_reg = 0; 14256 scmd->satacmd_status_reg = 0; 14257 scmd->satacmd_error_reg = 0; 14258 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 14259 if (state == 0) 14260 scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN; 14261 else 14262 scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN; 14263 14264 /* Transfer command to HBA */ 14265 if (((*SATA_START_FUNC(sata_hba_inst))( 14266 SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) || 14267 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 14268 /* Pkt execution failed */ 14269 if (state == 0) 14270 infop = "disabling Removable Media Status " 14271 "Notification failed"; 14272 else 14273 infop = "enabling Removable Media Status " 14274 "Notification failed"; 14275 14276 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 14277 rval = SATA_FAILURE; 14278 } 14279 failure: 14280 /* Free allocated resources */ 14281 if (spkt != NULL) 14282 sata_pkt_free(spx); 14283 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 14284 return (rval); 14285 } 14286 14287 14288 /* 14289 * Update state and copy port ss* values from passed sata_device structure. 14290 * sata_address is validated - if not valid, nothing is changed in sata_scsi 14291 * configuration struct. 14292 * 14293 * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function 14294 * regardless of the state in device argument. 14295 * 14296 * Port mutex should be held while calling this function. 14297 */ 14298 static void 14299 sata_update_port_info(sata_hba_inst_t *sata_hba_inst, 14300 sata_device_t *sata_device) 14301 { 14302 sata_cport_info_t *cportinfo; 14303 14304 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT || 14305 sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) { 14306 if (SATA_NUM_CPORTS(sata_hba_inst) <= 14307 sata_device->satadev_addr.cport) 14308 return; 14309 14310 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 14311 sata_device->satadev_addr.cport); 14312 14313 ASSERT(mutex_owned(&cportinfo->cport_mutex)); 14314 cportinfo->cport_scr = sata_device->satadev_scr; 14315 14316 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 14317 cportinfo->cport_state &= ~(SATA_PSTATE_PWRON | 14318 SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED); 14319 cportinfo->cport_state |= 14320 sata_device->satadev_state & SATA_PSTATE_VALID; 14321 } 14322 } 14323 14324 void 14325 sata_update_pmport_info(sata_hba_inst_t *sata_hba_inst, 14326 sata_device_t *sata_device) 14327 { 14328 sata_pmport_info_t *pmportinfo; 14329 14330 if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT && 14331 sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) || 14332 SATA_NUM_PMPORTS(sata_hba_inst, 14333 sata_device->satadev_addr.cport) < 14334 sata_device->satadev_addr.pmport) { 14335 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 14336 "sata_update_port_info: error address %p.", 14337 &sata_device->satadev_addr); 14338 return; 14339 } 14340 14341 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 14342 sata_device->satadev_addr.cport, 14343 sata_device->satadev_addr.pmport); 14344 14345 ASSERT(mutex_owned(&pmportinfo->pmport_mutex)); 14346 pmportinfo->pmport_scr = sata_device->satadev_scr; 14347 14348 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 14349 pmportinfo->pmport_state &= 14350 ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED); 14351 pmportinfo->pmport_state |= 14352 sata_device->satadev_state & SATA_PSTATE_VALID; 14353 } 14354 14355 /* 14356 * Extract SATA port specification from an IOCTL argument. 14357 * 14358 * This function return the port the user land send us as is, unless it 14359 * cannot retrieve port spec, then -1 is returned. 14360 * 14361 * Support port multiplier. 14362 */ 14363 static int32_t 14364 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp) 14365 { 14366 int32_t port; 14367 14368 /* Extract port number from nvpair in dca structure */ 14369 if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) { 14370 SATA_LOG_D((sata_hba_inst, CE_NOTE, 14371 "sata_get_port_num: invalid port spec 0x%x in ioctl", 14372 port)); 14373 port = -1; 14374 } 14375 14376 return (port); 14377 } 14378 14379 /* 14380 * Get dev_info_t pointer to the device node pointed to by port argument. 14381 * NOTE: target argument is a value used in ioctls to identify 14382 * the AP - it is not a sata_address. 14383 * It is a combination of cport, pmport and address qualifier, encodded same 14384 * way as a scsi target number. 14385 * At this moment it carries only cport number. 14386 * 14387 * PMult hotplug is supported now. 14388 * 14389 * Returns dev_info_t pointer if target device was found, NULL otherwise. 14390 */ 14391 14392 static dev_info_t * 14393 sata_get_target_dip(dev_info_t *dip, uint8_t cport, uint8_t pmport) 14394 { 14395 dev_info_t *cdip = NULL; 14396 int target, tgt; 14397 uint8_t qual; 14398 14399 sata_hba_inst_t *sata_hba_inst; 14400 scsi_hba_tran_t *scsi_hba_tran; 14401 14402 /* Get target id */ 14403 scsi_hba_tran = ddi_get_driver_private(dip); 14404 if (scsi_hba_tran == NULL) 14405 return (NULL); 14406 14407 sata_hba_inst = scsi_hba_tran->tran_hba_private; 14408 14409 if (sata_hba_inst == NULL) 14410 return (NULL); 14411 14412 /* Identify a port-mult by cport_info.cport_dev_type */ 14413 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) 14414 qual = SATA_ADDR_DPMPORT; 14415 else 14416 qual = SATA_ADDR_DCPORT; 14417 14418 target = SATA_TO_SCSI_TARGET(cport, pmport, qual); 14419 14420 /* Retrieve target dip */ 14421 ndi_devi_enter(dip); 14422 for (cdip = ddi_get_child(dip); cdip != NULL; ) { 14423 dev_info_t *next = ddi_get_next_sibling(cdip); 14424 14425 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip, 14426 DDI_PROP_DONTPASS, "target", -1); 14427 if (tgt == -1) { 14428 /* 14429 * This is actually an error condition, but not 14430 * a fatal one. Just continue the search. 14431 */ 14432 cdip = next; 14433 continue; 14434 } 14435 14436 if (tgt == target) 14437 break; 14438 14439 cdip = next; 14440 } 14441 ndi_devi_exit(dip); 14442 14443 return (cdip); 14444 } 14445 14446 /* 14447 * Get dev_info_t pointer to the device node pointed to by port argument. 14448 * NOTE: target argument is a value used in ioctls to identify 14449 * the AP - it is not a sata_address. 14450 * It is a combination of cport, pmport and address qualifier, encoded same 14451 * way as a scsi target number. 14452 * 14453 * Returns dev_info_t pointer if target device was found, NULL otherwise. 14454 */ 14455 14456 static dev_info_t * 14457 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr) 14458 { 14459 dev_info_t *cdip = NULL; 14460 int target, tgt; 14461 14462 target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual); 14463 14464 ndi_devi_enter(dip); 14465 for (cdip = ddi_get_child(dip); cdip != NULL; ) { 14466 dev_info_t *next = ddi_get_next_sibling(cdip); 14467 14468 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip, 14469 DDI_PROP_DONTPASS, "target", -1); 14470 if (tgt == -1) { 14471 /* 14472 * This is actually an error condition, but not 14473 * a fatal one. Just continue the search. 14474 */ 14475 cdip = next; 14476 continue; 14477 } 14478 14479 if (tgt == target) 14480 break; 14481 14482 cdip = next; 14483 } 14484 ndi_devi_exit(dip); 14485 14486 return (cdip); 14487 } 14488 14489 /* 14490 * Process sata port disconnect request. 14491 * Normally, cfgadm sata plugin will try to offline (unconfigure) the device 14492 * before this request. Nevertheless, if a device is still configured, 14493 * we need to attempt to offline and unconfigure device. 14494 * Regardless of the unconfigure operation results the port is marked as 14495 * deactivated and no access to the attached device is possible. 14496 * If the target node remains because unconfigure operation failed, its state 14497 * will be set to DEVICE_REMOVED, preventing it to be used again when a device 14498 * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure 14499 * the device and remove old target node. 14500 * 14501 * This function invokes sata_hba_inst->satahba_tran-> 14502 * sata_tran_hotplug_ops->sata_tran_port_deactivate(). 14503 * If successful, the device structure (if any) attached to the specified port 14504 * is removed and state of the port marked appropriately. 14505 * Failure of the port_deactivate may keep port in the physically active state, 14506 * or may fail the port. 14507 * 14508 * NOTE: Port multiplier is supported. 14509 */ 14510 14511 static int 14512 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst, 14513 sata_device_t *sata_device) 14514 { 14515 sata_drive_info_t *sdinfo = NULL, *subsdinfo = NULL; 14516 sata_cport_info_t *cportinfo = NULL; 14517 sata_pmport_info_t *pmportinfo = NULL; 14518 sata_pmult_info_t *pmultinfo = NULL; 14519 sata_device_t subsdevice; 14520 int cport, pmport, qual; 14521 int rval = SATA_SUCCESS; 14522 int npmport = 0; 14523 int rv = 0; 14524 14525 cport = sata_device->satadev_addr.cport; 14526 pmport = sata_device->satadev_addr.pmport; 14527 qual = sata_device->satadev_addr.qual; 14528 14529 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 14530 if (qual == SATA_ADDR_DCPORT) 14531 qual = SATA_ADDR_CPORT; 14532 else 14533 qual = SATA_ADDR_PMPORT; 14534 14535 /* 14536 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran-> 14537 * sata_tran_hotplug_ops->sata_tran_port_deactivate(). 14538 * Do the sanity check. 14539 */ 14540 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) { 14541 /* No physical port deactivation supported. */ 14542 return (EINVAL); 14543 } 14544 14545 /* Check the current state of the port */ 14546 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 14547 (SATA_DIP(sata_hba_inst), sata_device); 14548 14549 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 14550 14551 /* 14552 * Processing port mulitiplier 14553 */ 14554 if (qual == SATA_ADDR_CPORT && 14555 SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) { 14556 mutex_enter(&cportinfo->cport_mutex); 14557 14558 /* Check controller port status */ 14559 sata_update_port_info(sata_hba_inst, sata_device); 14560 if (rval != SATA_SUCCESS || 14561 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 14562 /* 14563 * Device port status is unknown or it is in failed 14564 * state 14565 */ 14566 SATA_CPORT_STATE(sata_hba_inst, cport) = 14567 SATA_PSTATE_FAILED; 14568 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 14569 "sata_hba_ioctl: connect: failed to deactivate " 14570 "SATA port %d", cport); 14571 mutex_exit(&cportinfo->cport_mutex); 14572 return (EIO); 14573 } 14574 14575 /* Disconnect all sub-devices. */ 14576 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 14577 if (pmultinfo != NULL) { 14578 14579 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 14580 sata_hba_inst, cport); npmport ++) { 14581 subsdinfo = SATA_PMPORT_DRV_INFO( 14582 sata_hba_inst, cport, npmport); 14583 if (subsdinfo == NULL) 14584 continue; 14585 14586 subsdevice.satadev_addr = subsdinfo-> 14587 satadrv_addr; 14588 14589 mutex_exit(&cportinfo->cport_mutex); 14590 if (sata_ioctl_disconnect(sata_hba_inst, 14591 &subsdevice) == SATA_SUCCESS) { 14592 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 14593 "[Remove] device at port %d:%d " 14594 "successfully.", cport, npmport); 14595 } 14596 mutex_enter(&cportinfo->cport_mutex); 14597 } 14598 } 14599 14600 /* Disconnect the port multiplier */ 14601 cportinfo->cport_state &= ~SATA_STATE_READY; 14602 mutex_exit(&cportinfo->cport_mutex); 14603 14604 sata_device->satadev_addr.qual = qual; 14605 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 14606 (SATA_DIP(sata_hba_inst), sata_device); 14607 14608 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 14609 SE_NO_HINT); 14610 14611 mutex_enter(&cportinfo->cport_mutex); 14612 sata_update_port_info(sata_hba_inst, sata_device); 14613 if (rval != SATA_SUCCESS && 14614 sata_device->satadev_state & SATA_PSTATE_FAILED) { 14615 cportinfo->cport_state = SATA_PSTATE_FAILED; 14616 rv = EIO; 14617 } else { 14618 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 14619 } 14620 mutex_exit(&cportinfo->cport_mutex); 14621 14622 return (rv); 14623 } 14624 14625 /* 14626 * Process non-port-multiplier device - it could be a drive connected 14627 * to a port multiplier port or a controller port. 14628 */ 14629 if (qual == SATA_ADDR_PMPORT) { 14630 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 14631 mutex_enter(&pmportinfo->pmport_mutex); 14632 sata_update_pmport_info(sata_hba_inst, sata_device); 14633 if (rval != SATA_SUCCESS || 14634 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 14635 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 14636 SATA_PSTATE_FAILED; 14637 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 14638 "sata_hba_ioctl: connect: failed to deactivate " 14639 "SATA port %d:%d", cport, pmport); 14640 mutex_exit(&pmportinfo->pmport_mutex); 14641 return (EIO); 14642 } 14643 14644 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 14645 sdinfo = pmportinfo->pmport_sata_drive; 14646 ASSERT(sdinfo != NULL); 14647 } 14648 14649 /* 14650 * Set port's dev_state to not ready - this will disable 14651 * an access to a potentially attached device. 14652 */ 14653 pmportinfo->pmport_state &= ~SATA_STATE_READY; 14654 14655 /* Remove and release sata_drive info structure. */ 14656 if (sdinfo != NULL) { 14657 if ((sdinfo->satadrv_type & 14658 SATA_VALID_DEV_TYPE) != 0) { 14659 /* 14660 * If a target node exists, try to offline 14661 * a device and remove target node. 14662 */ 14663 mutex_exit(&pmportinfo->pmport_mutex); 14664 (void) sata_offline_device(sata_hba_inst, 14665 sata_device, sdinfo); 14666 mutex_enter(&pmportinfo->pmport_mutex); 14667 } 14668 14669 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 14670 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 14671 (void) kmem_free((void *)sdinfo, 14672 sizeof (sata_drive_info_t)); 14673 } 14674 mutex_exit(&pmportinfo->pmport_mutex); 14675 14676 } else if (qual == SATA_ADDR_CPORT) { 14677 mutex_enter(&cportinfo->cport_mutex); 14678 sata_update_port_info(sata_hba_inst, sata_device); 14679 if (rval != SATA_SUCCESS || 14680 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 14681 /* 14682 * Device port status is unknown or it is in failed 14683 * state 14684 */ 14685 SATA_CPORT_STATE(sata_hba_inst, cport) = 14686 SATA_PSTATE_FAILED; 14687 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 14688 "sata_hba_ioctl: connect: failed to deactivate " 14689 "SATA port %d", cport); 14690 mutex_exit(&cportinfo->cport_mutex); 14691 return (EIO); 14692 } 14693 14694 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 14695 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 14696 ASSERT(pmultinfo != NULL); 14697 } else if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 14698 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 14699 ASSERT(sdinfo != NULL); 14700 } 14701 cportinfo->cport_state &= ~SATA_STATE_READY; 14702 14703 if (sdinfo != NULL) { 14704 if ((sdinfo->satadrv_type & 14705 SATA_VALID_DEV_TYPE) != 0) { 14706 /* 14707 * If a target node exists, try to offline 14708 * a device and remove target node. 14709 */ 14710 mutex_exit(&cportinfo->cport_mutex); 14711 (void) sata_offline_device(sata_hba_inst, 14712 sata_device, sdinfo); 14713 mutex_enter(&cportinfo->cport_mutex); 14714 } 14715 14716 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 14717 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 14718 (void) kmem_free((void *)sdinfo, 14719 sizeof (sata_drive_info_t)); 14720 } 14721 mutex_exit(&cportinfo->cport_mutex); 14722 } 14723 14724 /* Just ask HBA driver to deactivate port */ 14725 sata_device->satadev_addr.qual = qual; 14726 14727 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 14728 (SATA_DIP(sata_hba_inst), sata_device); 14729 14730 /* 14731 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 14732 * without the hint (to force listener to investivate the state). 14733 */ 14734 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 14735 SE_NO_HINT); 14736 14737 if (qual == SATA_ADDR_PMPORT) { 14738 mutex_enter(&pmportinfo->pmport_mutex); 14739 sata_update_pmport_info(sata_hba_inst, sata_device); 14740 14741 if (rval != SATA_SUCCESS && 14742 sata_device->satadev_state & SATA_PSTATE_FAILED) { 14743 /* 14744 * Port deactivation failure - do not change port 14745 * state unless the state returned by HBA indicates a 14746 * port failure. 14747 * 14748 * NOTE: device structures were released, so devices 14749 * now are invisible! Port reset is needed to 14750 * re-enumerate devices. 14751 */ 14752 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 14753 rv = EIO; 14754 } else { 14755 /* 14756 * Deactivation succeded. From now on the sata framework 14757 * will not care what is happening to the device, until 14758 * the port is activated again. 14759 */ 14760 pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN; 14761 } 14762 mutex_exit(&pmportinfo->pmport_mutex); 14763 } else if (qual == SATA_ADDR_CPORT) { 14764 mutex_enter(&cportinfo->cport_mutex); 14765 sata_update_port_info(sata_hba_inst, sata_device); 14766 14767 if (rval != SATA_SUCCESS && 14768 sata_device->satadev_state & SATA_PSTATE_FAILED) { 14769 cportinfo->cport_state = SATA_PSTATE_FAILED; 14770 rv = EIO; 14771 } else { 14772 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 14773 } 14774 mutex_exit(&cportinfo->cport_mutex); 14775 } 14776 14777 return (rv); 14778 } 14779 14780 14781 14782 /* 14783 * Process sata port connect request 14784 * The sata cfgadm pluging will invoke this operation only if port was found 14785 * in the disconnect state (failed state is also treated as the disconnected 14786 * state). 14787 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->satahba_tran-> 14788 * sata_tran_hotplug_ops->sata_tran_port_activate(). 14789 * If successful and a device is found attached to the port, 14790 * the initialization sequence is executed to attach a device structure to 14791 * a port structure. The state of the port and a device would be set 14792 * appropriately. 14793 * The device is not set in configured state (system-wise) by this operation. 14794 * 14795 * Note, that activating the port may generate link events, 14796 * so it is important that following processing and the 14797 * event processing does not interfere with each other! 14798 * 14799 * This operation may remove port failed state and will 14800 * try to make port active and in good standing. 14801 * 14802 * NOTE: Port multiplier is supported. 14803 */ 14804 14805 static int 14806 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst, 14807 sata_device_t *sata_device) 14808 { 14809 sata_pmport_info_t *pmportinfo = NULL; 14810 uint8_t cport, pmport, qual; 14811 int rv = 0; 14812 14813 cport = sata_device->satadev_addr.cport; 14814 pmport = sata_device->satadev_addr.pmport; 14815 qual = sata_device->satadev_addr.qual; 14816 14817 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 14818 if (qual == SATA_ADDR_DCPORT) 14819 qual = SATA_ADDR_CPORT; 14820 else 14821 qual = SATA_ADDR_PMPORT; 14822 14823 if (qual == SATA_ADDR_PMPORT) 14824 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 14825 14826 /* 14827 * DEVCTL_AP_CONNECT would invoke sata_hba_inst-> 14828 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate(). 14829 * Perform sanity check now. 14830 */ 14831 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) { 14832 /* No physical port activation supported. */ 14833 return (EINVAL); 14834 } 14835 14836 /* Just ask HBA driver to activate port */ 14837 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 14838 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 14839 /* 14840 * Port activation failure. 14841 */ 14842 if (qual == SATA_ADDR_CPORT) { 14843 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14844 cport)->cport_mutex); 14845 sata_update_port_info(sata_hba_inst, sata_device); 14846 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 14847 SATA_CPORT_STATE(sata_hba_inst, cport) = 14848 SATA_PSTATE_FAILED; 14849 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 14850 "sata_hba_ioctl: connect: failed to " 14851 "activate SATA port %d", cport); 14852 } 14853 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14854 cport)->cport_mutex); 14855 } else { /* port multiplier device port */ 14856 mutex_enter(&pmportinfo->pmport_mutex); 14857 sata_update_pmport_info(sata_hba_inst, sata_device); 14858 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 14859 SATA_PMPORT_STATE(sata_hba_inst, cport, 14860 pmport) = SATA_PSTATE_FAILED; 14861 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 14862 "sata_hba_ioctl: connect: failed to " 14863 "activate SATA port %d:%d", cport, pmport); 14864 } 14865 mutex_exit(&pmportinfo->pmport_mutex); 14866 } 14867 return (EIO); 14868 } 14869 14870 /* Virgin port state - will be updated by the port re-probe. */ 14871 if (qual == SATA_ADDR_CPORT) { 14872 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14873 cport)->cport_mutex); 14874 SATA_CPORT_STATE(sata_hba_inst, cport) = 0; 14875 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 14876 cport)->cport_mutex); 14877 } else { /* port multiplier device port */ 14878 mutex_enter(&pmportinfo->pmport_mutex); 14879 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0; 14880 mutex_exit(&pmportinfo->pmport_mutex); 14881 } 14882 14883 /* 14884 * Probe the port to find its state and attached device. 14885 */ 14886 if (sata_reprobe_port(sata_hba_inst, sata_device, 14887 SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE) 14888 rv = EIO; 14889 14890 /* 14891 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 14892 * without the hint 14893 */ 14894 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 14895 SE_NO_HINT); 14896 14897 /* 14898 * If there is a device attached to the port, emit 14899 * a message. 14900 */ 14901 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 14902 14903 if (qual == SATA_ADDR_CPORT) { 14904 if (sata_device->satadev_type == SATA_DTYPE_PMULT) { 14905 sata_log(sata_hba_inst, CE_WARN, 14906 "SATA port multiplier detected " 14907 "at port %d", cport); 14908 } else { 14909 sata_log(sata_hba_inst, CE_WARN, 14910 "SATA device detected at port %d", cport); 14911 if (sata_device->satadev_type == 14912 SATA_DTYPE_UNKNOWN) { 14913 /* 14914 * A device was not successfully identified 14915 */ 14916 sata_log(sata_hba_inst, CE_WARN, 14917 "Could not identify SATA " 14918 "device at port %d", cport); 14919 } 14920 } 14921 } else { /* port multiplier device port */ 14922 sata_log(sata_hba_inst, CE_WARN, 14923 "SATA device detected at port %d:%d", 14924 cport, pmport); 14925 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 14926 /* 14927 * A device was not successfully identified 14928 */ 14929 sata_log(sata_hba_inst, CE_WARN, 14930 "Could not identify SATA " 14931 "device at port %d:%d", cport, pmport); 14932 } 14933 } 14934 } 14935 14936 return (rv); 14937 } 14938 14939 14940 /* 14941 * Process sata device unconfigure request. 14942 * The unconfigure operation uses generic nexus operation to 14943 * offline a device. It leaves a target device node attached. 14944 * and obviously sata_drive_info attached as well, because 14945 * from the hardware point of view nothing has changed. 14946 */ 14947 static int 14948 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst, 14949 sata_device_t *sata_device) 14950 { 14951 int rv = 0; 14952 dev_info_t *tdip; 14953 14954 /* We are addressing attached device, not a port */ 14955 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 14956 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 14957 else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) 14958 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 14959 14960 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 14961 &sata_device->satadev_addr)) != NULL) { 14962 14963 if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) { 14964 SATA_LOG_D((sata_hba_inst, CE_WARN, 14965 "sata_hba_ioctl: unconfigure: " 14966 "failed to unconfigure device at SATA port %d:%d", 14967 sata_device->satadev_addr.cport, 14968 sata_device->satadev_addr.pmport)); 14969 rv = EIO; 14970 } 14971 /* 14972 * The target node devi_state should be marked with 14973 * DEVI_DEVICE_OFFLINE by ndi_devi_offline(). 14974 * This would be the indication for cfgadm that 14975 * the AP node occupant state is 'unconfigured'. 14976 */ 14977 14978 } else { 14979 /* 14980 * This would indicate a failure on the part of cfgadm 14981 * to detect correct state of the node prior to this 14982 * call - one cannot unconfigure non-existing device. 14983 */ 14984 SATA_LOG_D((sata_hba_inst, CE_WARN, 14985 "sata_hba_ioctl: unconfigure: " 14986 "attempt to unconfigure non-existing device " 14987 "at SATA port %d:%d", 14988 sata_device->satadev_addr.cport, 14989 sata_device->satadev_addr.pmport)); 14990 rv = ENXIO; 14991 } 14992 return (rv); 14993 } 14994 14995 /* 14996 * Process sata device configure request 14997 * If port is in a failed state, operation is aborted - one has to use 14998 * an explicit connect or port activate request to try to get a port into 14999 * non-failed mode. Port reset wil also work in such situation. 15000 * If the port is in disconnected (shutdown) state, the connect operation is 15001 * attempted prior to any other action. 15002 * When port is in the active state, there is a device attached and the target 15003 * node exists, a device was most likely offlined. 15004 * If target node does not exist, a new target node is created. In both cases 15005 * an attempt is made to online (configure) the device. 15006 * 15007 * NOTE: Port multiplier is supported. 15008 */ 15009 static int 15010 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst, 15011 sata_device_t *sata_device) 15012 { 15013 int cport, pmport, qual; 15014 int rval; 15015 boolean_t target = B_TRUE; 15016 sata_cport_info_t *cportinfo; 15017 sata_pmport_info_t *pmportinfo = NULL; 15018 dev_info_t *tdip; 15019 sata_drive_info_t *sdinfo; 15020 15021 cport = sata_device->satadev_addr.cport; 15022 pmport = sata_device->satadev_addr.pmport; 15023 qual = sata_device->satadev_addr.qual; 15024 15025 /* Get current port state */ 15026 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 15027 (SATA_DIP(sata_hba_inst), sata_device); 15028 15029 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 15030 if (qual == SATA_ADDR_DPMPORT) { 15031 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 15032 mutex_enter(&pmportinfo->pmport_mutex); 15033 sata_update_pmport_info(sata_hba_inst, sata_device); 15034 if (rval != SATA_SUCCESS || 15035 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 15036 /* 15037 * Obviously, device on a failed port is not visible 15038 */ 15039 mutex_exit(&pmportinfo->pmport_mutex); 15040 return (ENXIO); 15041 } 15042 mutex_exit(&pmportinfo->pmport_mutex); 15043 } else { 15044 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15045 cport)->cport_mutex); 15046 sata_update_port_info(sata_hba_inst, sata_device); 15047 if (rval != SATA_SUCCESS || 15048 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 15049 /* 15050 * Obviously, device on a failed port is not visible 15051 */ 15052 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15053 cport)->cport_mutex); 15054 return (ENXIO); 15055 } 15056 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15057 cport)->cport_mutex); 15058 } 15059 15060 if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) { 15061 /* need to activate port */ 15062 target = B_FALSE; 15063 15064 /* Sanity check */ 15065 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) 15066 return (ENXIO); 15067 15068 /* Just let HBA driver to activate port */ 15069 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 15070 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 15071 /* 15072 * Port activation failure - do not change port state 15073 * unless the state returned by HBA indicates a port 15074 * failure. 15075 */ 15076 if (qual == SATA_ADDR_DPMPORT) { 15077 mutex_enter(&pmportinfo->pmport_mutex); 15078 sata_update_pmport_info(sata_hba_inst, 15079 sata_device); 15080 if (sata_device->satadev_state & 15081 SATA_PSTATE_FAILED) 15082 pmportinfo->pmport_state = 15083 SATA_PSTATE_FAILED; 15084 mutex_exit(&pmportinfo->pmport_mutex); 15085 } else { 15086 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15087 cport)->cport_mutex); 15088 sata_update_port_info(sata_hba_inst, 15089 sata_device); 15090 if (sata_device->satadev_state & 15091 SATA_PSTATE_FAILED) 15092 cportinfo->cport_state = 15093 SATA_PSTATE_FAILED; 15094 mutex_exit(&SATA_CPORT_INFO( 15095 sata_hba_inst, cport)->cport_mutex); 15096 } 15097 } 15098 SATA_LOG_D((sata_hba_inst, CE_WARN, 15099 "sata_hba_ioctl: configure: " 15100 "failed to activate SATA port %d:%d", 15101 cport, pmport)); 15102 return (EIO); 15103 } 15104 /* 15105 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 15106 * without the hint. 15107 */ 15108 sata_gen_sysevent(sata_hba_inst, 15109 &sata_device->satadev_addr, SE_NO_HINT); 15110 15111 /* Virgin port state */ 15112 if (qual == SATA_ADDR_DPMPORT) { 15113 mutex_enter(&pmportinfo->pmport_mutex); 15114 pmportinfo->pmport_state = 0; 15115 mutex_exit(&pmportinfo->pmport_mutex); 15116 } else { 15117 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15118 cport)-> cport_mutex); 15119 cportinfo->cport_state = 0; 15120 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15121 cport)->cport_mutex); 15122 } 15123 /* 15124 * Always reprobe port, to get current device info. 15125 */ 15126 if (sata_reprobe_port(sata_hba_inst, sata_device, 15127 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 15128 return (EIO); 15129 15130 if (sata_device->satadev_type != SATA_DTYPE_NONE && target == B_FALSE) { 15131 if (qual == SATA_ADDR_DPMPORT) { 15132 /* 15133 * That's the transition from "inactive" port 15134 * to active one with device attached. 15135 */ 15136 sata_log(sata_hba_inst, CE_WARN, 15137 "SATA device detected at port %d:%d", 15138 cport, pmport); 15139 } else { 15140 /* 15141 * When PM is attached to the cport and cport is 15142 * activated, every PM device port needs to be reprobed. 15143 * We need to emit message for all devices detected 15144 * at port multiplier's device ports. 15145 * Add such code here. 15146 * For now, just inform about device attached to 15147 * cport. 15148 */ 15149 sata_log(sata_hba_inst, CE_WARN, 15150 "SATA device detected at port %d", cport); 15151 } 15152 } 15153 15154 /* 15155 * This is where real configuration operation starts. 15156 * 15157 * When PM is attached to the cport and cport is activated, 15158 * devices attached PM device ports may have to be configured 15159 * explicitly. This may change when port multiplier is supported. 15160 * For now, configure only disks and other valid target devices. 15161 */ 15162 if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) { 15163 if (qual == SATA_ADDR_DCPORT) { 15164 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 15165 /* 15166 * A device was not successfully identified 15167 */ 15168 sata_log(sata_hba_inst, CE_WARN, 15169 "Could not identify SATA " 15170 "device at port %d", cport); 15171 } 15172 } else { /* port multiplier device port */ 15173 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 15174 /* 15175 * A device was not successfully identified 15176 */ 15177 sata_log(sata_hba_inst, CE_WARN, 15178 "Could not identify SATA " 15179 "device at port %d:%d", cport, pmport); 15180 } 15181 } 15182 return (ENXIO); /* No device to configure */ 15183 } 15184 15185 /* 15186 * Here we may have a device in reset condition, 15187 * but because we are just configuring it, there is 15188 * no need to process the reset other than just 15189 * to clear device reset condition in the HBA driver. 15190 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will 15191 * cause a first command sent the HBA driver with the request 15192 * to clear device reset condition. 15193 */ 15194 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15195 if (qual == SATA_ADDR_DPMPORT) 15196 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 15197 else 15198 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 15199 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 15200 if (sdinfo == NULL) { 15201 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15202 return (ENXIO); 15203 } 15204 if (sdinfo->satadrv_event_flags & 15205 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 15206 sdinfo->satadrv_event_flags = 0; 15207 } 15208 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 15209 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15210 15211 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 15212 &sata_device->satadev_addr)) != NULL) { 15213 /* 15214 * Target node exists. Verify, that it belongs 15215 * to existing, attached device and not to 15216 * a removed device. 15217 */ 15218 if (sata_check_device_removed(tdip) == B_TRUE) { 15219 if (qual == SATA_ADDR_DPMPORT) 15220 sata_log(sata_hba_inst, CE_WARN, 15221 "SATA device at port %d cannot be " 15222 "configured. " 15223 "Application(s) accessing " 15224 "previously attached device " 15225 "have to release it before newly " 15226 "inserted device can be made accessible.", 15227 cport); 15228 else 15229 sata_log(sata_hba_inst, CE_WARN, 15230 "SATA device at port %d:%d cannot be" 15231 "configured. " 15232 "Application(s) accessing " 15233 "previously attached device " 15234 "have to release it before newly " 15235 "inserted device can be made accessible.", 15236 cport, pmport); 15237 return (EIO); 15238 } 15239 /* 15240 * Device was not removed and re-inserted. 15241 * Try to online it. 15242 */ 15243 if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) { 15244 SATA_LOG_D((sata_hba_inst, CE_WARN, 15245 "sata_hba_ioctl: configure: " 15246 "onlining device at SATA port " 15247 "%d:%d failed", cport, pmport)); 15248 return (EIO); 15249 } 15250 15251 if (qual == SATA_ADDR_DPMPORT) { 15252 mutex_enter(&pmportinfo->pmport_mutex); 15253 pmportinfo->pmport_tgtnode_clean = B_TRUE; 15254 mutex_exit(&pmportinfo->pmport_mutex); 15255 } else { 15256 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15257 cport)->cport_mutex); 15258 cportinfo-> cport_tgtnode_clean = B_TRUE; 15259 mutex_exit(&SATA_CPORT_INFO( 15260 sata_hba_inst, cport)->cport_mutex); 15261 } 15262 } else { 15263 /* 15264 * No target node - need to create a new target node. 15265 */ 15266 if (qual == SATA_ADDR_DPMPORT) { 15267 mutex_enter(&pmportinfo->pmport_mutex); 15268 pmportinfo->pmport_tgtnode_clean = B_TRUE; 15269 mutex_exit(&pmportinfo->pmport_mutex); 15270 } else { 15271 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15272 cport_mutex); 15273 cportinfo-> cport_tgtnode_clean = B_TRUE; 15274 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15275 cport_mutex); 15276 } 15277 15278 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst), 15279 sata_hba_inst, &sata_device->satadev_addr); 15280 if (tdip == NULL) { 15281 /* Configure operation failed */ 15282 SATA_LOG_D((sata_hba_inst, CE_WARN, 15283 "sata_hba_ioctl: configure: " 15284 "configuring SATA device at port %d:%d " 15285 "failed", cport, pmport)); 15286 return (EIO); 15287 } 15288 } 15289 return (0); 15290 } 15291 15292 15293 /* 15294 * Process ioctl deactivate port request. 15295 * Arbitrarily unconfigure attached device, if any. 15296 * Even if the unconfigure fails, proceed with the 15297 * port deactivation. 15298 * 15299 * NOTE: Port Multiplier is supported now. 15300 */ 15301 15302 static int 15303 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst, 15304 sata_device_t *sata_device) 15305 { 15306 int cport, pmport, qual; 15307 int rval, rv = 0; 15308 int npmport; 15309 sata_cport_info_t *cportinfo; 15310 sata_pmport_info_t *pmportinfo; 15311 sata_pmult_info_t *pmultinfo; 15312 dev_info_t *tdip; 15313 sata_drive_info_t *sdinfo = NULL; 15314 sata_device_t subsdevice; 15315 15316 /* Sanity check */ 15317 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) 15318 return (ENOTSUP); 15319 15320 cport = sata_device->satadev_addr.cport; 15321 pmport = sata_device->satadev_addr.pmport; 15322 qual = sata_device->satadev_addr.qual; 15323 15324 /* SCSI_TO_SATA_ADDR_QUAL() translate ap_id into a device qualifier */ 15325 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 15326 if (qual == SATA_ADDR_DCPORT) 15327 qual = SATA_ADDR_CPORT; 15328 else 15329 qual = SATA_ADDR_PMPORT; 15330 15331 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 15332 if (qual == SATA_ADDR_PMPORT) 15333 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 15334 15335 /* 15336 * Processing port multiplier 15337 */ 15338 if (qual == SATA_ADDR_CPORT && 15339 SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) { 15340 mutex_enter(&cportinfo->cport_mutex); 15341 15342 /* Deactivate all sub-deices */ 15343 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 15344 if (pmultinfo != NULL) { 15345 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 15346 sata_hba_inst, cport); npmport++) { 15347 15348 subsdevice.satadev_addr.cport = cport; 15349 subsdevice.satadev_addr.pmport = 15350 (uint8_t)npmport; 15351 subsdevice.satadev_addr.qual = 15352 SATA_ADDR_DPMPORT; 15353 15354 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 15355 "sata_hba_ioctl: deactivate: trying to " 15356 "deactivate SATA port %d:%d", 15357 cport, npmport); 15358 15359 mutex_exit(&cportinfo->cport_mutex); 15360 if (sata_ioctl_deactivate(sata_hba_inst, 15361 &subsdevice) == SATA_SUCCESS) { 15362 SATADBG2(SATA_DBG_PMULT, sata_hba_inst, 15363 "[Deactivate] device at port %d:%d " 15364 "successfully.", cport, npmport); 15365 } 15366 mutex_enter(&cportinfo->cport_mutex); 15367 } 15368 } 15369 15370 /* Deactivate the port multiplier now. */ 15371 cportinfo->cport_state &= ~SATA_STATE_READY; 15372 mutex_exit(&cportinfo->cport_mutex); 15373 15374 sata_device->satadev_addr.qual = qual; 15375 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 15376 (SATA_DIP(sata_hba_inst), sata_device); 15377 15378 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 15379 SE_NO_HINT); 15380 15381 mutex_enter(&cportinfo->cport_mutex); 15382 sata_update_port_info(sata_hba_inst, sata_device); 15383 if (rval != SATA_SUCCESS) { 15384 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 15385 cportinfo->cport_state = SATA_PSTATE_FAILED; 15386 } 15387 rv = EIO; 15388 } else { 15389 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 15390 } 15391 mutex_exit(&cportinfo->cport_mutex); 15392 15393 return (rv); 15394 } 15395 15396 /* 15397 * Process non-port-multiplier device - it could be a drive connected 15398 * to a port multiplier port or a controller port. 15399 */ 15400 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15401 if (qual == SATA_ADDR_CPORT) { 15402 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 15403 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 15404 /* deal only with valid devices */ 15405 if ((cportinfo->cport_dev_type & 15406 SATA_VALID_DEV_TYPE) != 0) 15407 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 15408 } 15409 cportinfo->cport_state &= ~SATA_STATE_READY; 15410 } else { 15411 /* Port multiplier device port */ 15412 mutex_enter(&pmportinfo->pmport_mutex); 15413 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 15414 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE && 15415 (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0) 15416 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 15417 pmportinfo->pmport_state &= ~SATA_STATE_READY; 15418 mutex_exit(&pmportinfo->pmport_mutex); 15419 } 15420 15421 if (sdinfo != NULL) { 15422 /* 15423 * If a target node exists, try to offline a device and 15424 * to remove a target node. 15425 */ 15426 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15427 cport_mutex); 15428 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 15429 &sata_device->satadev_addr); 15430 if (tdip != NULL) { 15431 /* target node exist */ 15432 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 15433 "sata_hba_ioctl: port deactivate: " 15434 "target node exists.", NULL); 15435 15436 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != 15437 NDI_SUCCESS) { 15438 SATA_LOG_D((sata_hba_inst, CE_WARN, 15439 "sata_hba_ioctl: port deactivate: " 15440 "failed to unconfigure device at port " 15441 "%d:%d before deactivating the port", 15442 cport, pmport)); 15443 /* 15444 * Set DEVICE REMOVED state in the target 15445 * node. It will prevent an access to 15446 * the device even when a new device is 15447 * attached, until the old target node is 15448 * released, removed and recreated for a new 15449 * device. 15450 */ 15451 sata_set_device_removed(tdip); 15452 15453 /* 15454 * Instruct the event daemon to try the 15455 * target node cleanup later. 15456 */ 15457 sata_set_target_node_cleanup(sata_hba_inst, 15458 &sata_device->satadev_addr); 15459 } 15460 } 15461 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15462 cport_mutex); 15463 /* 15464 * In any case, remove and release sata_drive_info 15465 * structure. 15466 */ 15467 if (qual == SATA_ADDR_CPORT) { 15468 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 15469 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 15470 } else { /* port multiplier device port */ 15471 mutex_enter(&pmportinfo->pmport_mutex); 15472 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 15473 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 15474 mutex_exit(&pmportinfo->pmport_mutex); 15475 } 15476 (void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t)); 15477 } 15478 15479 if (qual == SATA_ADDR_CPORT) { 15480 cportinfo->cport_state &= ~(SATA_STATE_PROBED | 15481 SATA_STATE_PROBING); 15482 } else if (qual == SATA_ADDR_PMPORT) { 15483 mutex_enter(&pmportinfo->pmport_mutex); 15484 pmportinfo->pmport_state &= ~(SATA_STATE_PROBED | 15485 SATA_STATE_PROBING); 15486 mutex_exit(&pmportinfo->pmport_mutex); 15487 } 15488 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15489 15490 /* Just let HBA driver to deactivate port */ 15491 sata_device->satadev_addr.qual = qual; 15492 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 15493 (SATA_DIP(sata_hba_inst), sata_device); 15494 15495 /* 15496 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 15497 * without the hint 15498 */ 15499 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 15500 SE_NO_HINT); 15501 15502 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15503 sata_update_port_info(sata_hba_inst, sata_device); 15504 if (qual == SATA_ADDR_CPORT) { 15505 if (rval != SATA_SUCCESS) { 15506 /* 15507 * Port deactivation failure - do not change port state 15508 * unless the state returned by HBA indicates a port 15509 * failure. 15510 */ 15511 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 15512 SATA_CPORT_STATE(sata_hba_inst, cport) = 15513 SATA_PSTATE_FAILED; 15514 } 15515 SATA_LOG_D((sata_hba_inst, CE_WARN, 15516 "sata_hba_ioctl: port deactivate: " 15517 "cannot deactivate SATA port %d", cport)); 15518 rv = EIO; 15519 } else { 15520 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 15521 } 15522 } else { 15523 mutex_enter(&pmportinfo->pmport_mutex); 15524 if (rval != SATA_SUCCESS) { 15525 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 15526 SATA_PMPORT_STATE(sata_hba_inst, cport, 15527 pmport) = SATA_PSTATE_FAILED; 15528 } 15529 SATA_LOG_D((sata_hba_inst, CE_WARN, 15530 "sata_hba_ioctl: port deactivate: " 15531 "cannot deactivate SATA port %d:%d", 15532 cport, pmport)); 15533 rv = EIO; 15534 } else { 15535 pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN; 15536 } 15537 mutex_exit(&pmportinfo->pmport_mutex); 15538 } 15539 15540 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15541 15542 return (rv); 15543 } 15544 15545 /* 15546 * Process ioctl port activate request. 15547 * 15548 * NOTE: Port multiplier is supported now. 15549 */ 15550 static int 15551 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst, 15552 sata_device_t *sata_device) 15553 { 15554 int cport, pmport, qual; 15555 sata_cport_info_t *cportinfo; 15556 sata_pmport_info_t *pmportinfo = NULL; 15557 boolean_t dev_existed = B_TRUE; 15558 15559 /* Sanity check */ 15560 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) 15561 return (ENOTSUP); 15562 15563 cport = sata_device->satadev_addr.cport; 15564 pmport = sata_device->satadev_addr.pmport; 15565 qual = sata_device->satadev_addr.qual; 15566 15567 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 15568 15569 /* 15570 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL()) 15571 * is a device. But what we are dealing with is port/pmport. 15572 */ 15573 ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT); 15574 if (qual == SATA_ADDR_DCPORT) 15575 sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT; 15576 else 15577 sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT; 15578 15579 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15580 if (qual == SATA_ADDR_PMPORT) { 15581 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 15582 if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN || 15583 pmportinfo->pmport_dev_type == SATA_DTYPE_NONE) 15584 dev_existed = B_FALSE; 15585 } else { /* cport */ 15586 if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN || 15587 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 15588 dev_existed = B_FALSE; 15589 } 15590 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15591 15592 /* Just let HBA driver to activate port, if necessary */ 15593 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 15594 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 15595 /* 15596 * Port activation failure - do not change port state unless 15597 * the state returned by HBA indicates a port failure. 15598 */ 15599 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15600 cport)->cport_mutex); 15601 sata_update_port_info(sata_hba_inst, sata_device); 15602 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 15603 if (qual == SATA_ADDR_PMPORT) { 15604 mutex_enter(&pmportinfo->pmport_mutex); 15605 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 15606 mutex_exit(&pmportinfo->pmport_mutex); 15607 } else 15608 cportinfo->cport_state = SATA_PSTATE_FAILED; 15609 15610 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15611 cport)->cport_mutex); 15612 SATA_LOG_D((sata_hba_inst, CE_WARN, 15613 "sata_hba_ioctl: port activate: cannot activate " 15614 "SATA port %d:%d", cport, pmport)); 15615 return (EIO); 15616 } 15617 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15618 } 15619 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15620 if (qual == SATA_ADDR_PMPORT) { 15621 mutex_enter(&pmportinfo->pmport_mutex); 15622 pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN; 15623 mutex_exit(&pmportinfo->pmport_mutex); 15624 } else 15625 cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN; 15626 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15627 15628 /* 15629 * Re-probe port to find its current state and possibly attached device. 15630 * Port re-probing may change the cportinfo device type if device is 15631 * found attached. 15632 * If port probing failed, the device type would be set to 15633 * SATA_DTYPE_NONE. 15634 */ 15635 (void) sata_reprobe_port(sata_hba_inst, sata_device, 15636 SATA_DEV_IDENTIFY_RETRY); 15637 15638 /* 15639 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 15640 * without the hint. 15641 */ 15642 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 15643 SE_NO_HINT); 15644 15645 if (dev_existed == B_FALSE) { 15646 if (qual == SATA_ADDR_PMPORT && 15647 pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 15648 /* 15649 * That's the transition from the "inactive" port state 15650 * or the active port without a device attached to the 15651 * active port state with a device attached. 15652 */ 15653 sata_log(sata_hba_inst, CE_WARN, 15654 "SATA device detected at port %d:%d", 15655 cport, pmport); 15656 } else if (qual == SATA_ADDR_CPORT && 15657 cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 15658 /* 15659 * That's the transition from the "inactive" port state 15660 * or the active port without a device attached to the 15661 * active port state with a device attached. 15662 */ 15663 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 15664 sata_log(sata_hba_inst, CE_WARN, 15665 "SATA device detected at port %d", cport); 15666 } else { 15667 sata_log(sata_hba_inst, CE_WARN, 15668 "SATA port multiplier detected at port %d", 15669 cport); 15670 } 15671 } 15672 } 15673 return (0); 15674 } 15675 15676 15677 15678 /* 15679 * Process ioctl reset port request. 15680 * 15681 * NOTE: Port-Multiplier is supported. 15682 */ 15683 static int 15684 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst, 15685 sata_device_t *sata_device) 15686 { 15687 int cport, pmport, qual; 15688 int rv = 0; 15689 15690 cport = sata_device->satadev_addr.cport; 15691 pmport = sata_device->satadev_addr.pmport; 15692 qual = sata_device->satadev_addr.qual; 15693 15694 /* 15695 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL()) 15696 * is a device. But what we are dealing with is port/pmport. 15697 */ 15698 if (qual == SATA_ADDR_DCPORT) 15699 sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT; 15700 else 15701 sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT; 15702 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT); 15703 15704 /* Sanity check */ 15705 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 15706 SATA_LOG_D((sata_hba_inst, CE_WARN, 15707 "sata_hba_ioctl: sata_hba_tran missing required " 15708 "function sata_tran_reset_dport")); 15709 return (ENOTSUP); 15710 } 15711 15712 /* Ask HBA to reset port */ 15713 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 15714 sata_device) != SATA_SUCCESS) { 15715 SATA_LOG_D((sata_hba_inst, CE_WARN, 15716 "sata_hba_ioctl: reset port: failed %d:%d", 15717 cport, pmport)); 15718 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15719 cport_mutex); 15720 sata_update_port_info(sata_hba_inst, sata_device); 15721 if (qual == SATA_ADDR_CPORT) 15722 SATA_CPORT_STATE(sata_hba_inst, cport) = 15723 SATA_PSTATE_FAILED; 15724 else { 15725 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, 15726 pmport)); 15727 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 15728 SATA_PSTATE_FAILED; 15729 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, 15730 pmport)); 15731 } 15732 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15733 cport_mutex); 15734 rv = EIO; 15735 } 15736 15737 return (rv); 15738 } 15739 15740 /* 15741 * Process ioctl reset device request. 15742 * 15743 * NOTE: Port multiplier is supported. 15744 */ 15745 static int 15746 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst, 15747 sata_device_t *sata_device) 15748 { 15749 sata_drive_info_t *sdinfo = NULL; 15750 sata_pmult_info_t *pmultinfo = NULL; 15751 int cport, pmport; 15752 int rv = 0; 15753 15754 /* Sanity check */ 15755 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 15756 SATA_LOG_D((sata_hba_inst, CE_WARN, 15757 "sata_hba_ioctl: sata_hba_tran missing required " 15758 "function sata_tran_reset_dport")); 15759 return (ENOTSUP); 15760 } 15761 15762 cport = sata_device->satadev_addr.cport; 15763 pmport = sata_device->satadev_addr.pmport; 15764 15765 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15766 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) { 15767 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == 15768 SATA_DTYPE_PMULT) 15769 pmultinfo = SATA_CPORT_INFO(sata_hba_inst, cport)-> 15770 cport_devp.cport_sata_pmult; 15771 else 15772 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 15773 sata_device->satadev_addr.cport); 15774 } else { /* port multiplier */ 15775 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 15776 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 15777 sata_device->satadev_addr.cport, 15778 sata_device->satadev_addr.pmport); 15779 } 15780 if (sdinfo == NULL && pmultinfo == NULL) { 15781 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15782 return (EINVAL); 15783 } 15784 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15785 15786 /* Ask HBA to reset device */ 15787 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 15788 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 15789 SATA_LOG_D((sata_hba_inst, CE_WARN, 15790 "sata_hba_ioctl: reset device: failed at port %d:%d", 15791 cport, pmport)); 15792 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15793 cport_mutex); 15794 sata_update_port_info(sata_hba_inst, sata_device); 15795 /* 15796 * Device info structure remains attached. Another device reset 15797 * or port disconnect/connect and re-probing is 15798 * needed to change it's state 15799 */ 15800 if (sdinfo != NULL) { 15801 sdinfo->satadrv_state &= ~SATA_STATE_READY; 15802 sdinfo->satadrv_state |= SATA_DSTATE_FAILED; 15803 } else if (pmultinfo != NULL) { 15804 pmultinfo->pmult_state &= ~SATA_STATE_READY; 15805 pmultinfo->pmult_state |= SATA_DSTATE_FAILED; 15806 } 15807 15808 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 15809 rv = EIO; 15810 } 15811 /* 15812 * If attached device was a port multiplier, some extra processing 15813 * may be needed to bring it back. SATA specification requies a 15814 * mandatory software reset on host port to reliably enumerate a port 15815 * multiplier, the HBA driver should handle that after reset 15816 * operation. 15817 */ 15818 return (rv); 15819 } 15820 15821 15822 /* 15823 * Process ioctl reset all request. 15824 */ 15825 static int 15826 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst) 15827 { 15828 sata_device_t sata_device; 15829 int rv = 0; 15830 int tcport; 15831 15832 sata_device.satadev_rev = SATA_DEVICE_REV; 15833 15834 /* 15835 * There is no protection here for configured devices. 15836 */ 15837 /* Sanity check */ 15838 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 15839 SATA_LOG_D((sata_hba_inst, CE_WARN, 15840 "sata_hba_ioctl: sata_hba_tran missing required " 15841 "function sata_tran_reset_dport")); 15842 return (ENOTSUP); 15843 } 15844 15845 /* 15846 * Need to lock all ports, not just one. 15847 * If any port is locked by event processing, fail the whole operation. 15848 * One port is already locked, but for simplicity lock it again. 15849 */ 15850 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) { 15851 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15852 cport_mutex); 15853 if (((SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15854 cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) { 15855 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15856 cport_mutex); 15857 rv = EBUSY; 15858 break; 15859 } else { 15860 /* 15861 * It is enough to lock cport in command-based 15862 * switching mode. 15863 */ 15864 SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15865 cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 15866 } 15867 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15868 cport_mutex); 15869 } 15870 15871 if (rv == 0) { 15872 /* 15873 * All cports were successfully locked. 15874 * Reset main SATA controller. 15875 * Set the device address to port 0, to have a valid device 15876 * address. 15877 */ 15878 sata_device.satadev_addr.qual = SATA_ADDR_CNTRL; 15879 sata_device.satadev_addr.cport = 0; 15880 sata_device.satadev_addr.pmport = 0; 15881 15882 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 15883 (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) { 15884 SATA_LOG_D((sata_hba_inst, CE_WARN, 15885 "sata_hba_ioctl: reset controller failed")); 15886 return (EIO); 15887 } 15888 } 15889 /* 15890 * Unlock all ports 15891 */ 15892 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) { 15893 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15894 cport_mutex); 15895 SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15896 cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY; 15897 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 15898 cport_mutex); 15899 } 15900 15901 /* 15902 * This operation returns EFAULT if either reset 15903 * controller failed or a re-probing of any port failed. 15904 */ 15905 return (rv); 15906 } 15907 15908 15909 /* 15910 * Process ioctl port self test request. 15911 * 15912 * NOTE: Port multiplier code is not completed nor tested. 15913 */ 15914 static int 15915 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst, 15916 sata_device_t *sata_device) 15917 { 15918 int cport, pmport, qual; 15919 int rv = 0; 15920 15921 /* Sanity check */ 15922 if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL) 15923 return (ENOTSUP); 15924 15925 cport = sata_device->satadev_addr.cport; 15926 pmport = sata_device->satadev_addr.pmport; 15927 qual = sata_device->satadev_addr.qual; 15928 15929 /* 15930 * There is no protection here for a configured 15931 * device attached to this port. 15932 */ 15933 15934 if ((*SATA_SELFTEST_FUNC(sata_hba_inst)) 15935 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 15936 SATA_LOG_D((sata_hba_inst, CE_WARN, 15937 "sata_hba_ioctl: port selftest: " 15938 "failed port %d:%d", cport, pmport)); 15939 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15940 cport_mutex); 15941 sata_update_port_info(sata_hba_inst, sata_device); 15942 if (qual == SATA_ADDR_CPORT) 15943 SATA_CPORT_STATE(sata_hba_inst, cport) = 15944 SATA_PSTATE_FAILED; 15945 else { /* port multiplier device port */ 15946 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, 15947 cport, pmport)); 15948 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 15949 SATA_PSTATE_FAILED; 15950 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, 15951 cport, pmport)); 15952 } 15953 15954 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 15955 cport_mutex); 15956 return (EIO); 15957 } 15958 /* 15959 * Beacuse the port was reset in the course of testing, it should be 15960 * re-probed and attached device state should be restored. At this 15961 * point the port state is unknown - it's state is HBA-specific. 15962 * Force port re-probing to get it into a known state. 15963 */ 15964 if (sata_reprobe_port(sata_hba_inst, sata_device, 15965 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 15966 rv = EIO; 15967 return (rv); 15968 } 15969 15970 15971 /* 15972 * sata_cfgadm_state: 15973 * Use the sata port state and state of the target node to figure out 15974 * the cfgadm_state. 15975 * 15976 * The port argument is a value with encoded cport, 15977 * pmport and address qualifier, in the same manner as a scsi target number. 15978 * SCSI_TO_SATA_CPORT macro extracts cport number, 15979 * SCSI_TO_SATA_PMPORT extracts pmport number and 15980 * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag. 15981 * 15982 * Port multiplier is supported. 15983 */ 15984 15985 static void 15986 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port, 15987 devctl_ap_state_t *ap_state) 15988 { 15989 uint8_t cport, pmport, qual; 15990 uint32_t port_state, pmult_state; 15991 uint32_t dev_type; 15992 sata_drive_info_t *sdinfo; 15993 15994 cport = SCSI_TO_SATA_CPORT(port); 15995 pmport = SCSI_TO_SATA_PMPORT(port); 15996 qual = SCSI_TO_SATA_ADDR_QUAL(port); 15997 15998 /* Check cport state */ 15999 port_state = SATA_CPORT_STATE(sata_hba_inst, cport); 16000 if (port_state & SATA_PSTATE_SHUTDOWN || 16001 port_state & SATA_PSTATE_FAILED) { 16002 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 16003 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 16004 if (port_state & SATA_PSTATE_FAILED) 16005 ap_state->ap_condition = AP_COND_FAILED; 16006 else 16007 ap_state->ap_condition = AP_COND_UNKNOWN; 16008 16009 return; 16010 } 16011 16012 /* cport state is okay. Now check pmport state */ 16013 if (qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) { 16014 /* Sanity check */ 16015 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 16016 SATA_DTYPE_PMULT || SATA_PMPORT_INFO(sata_hba_inst, 16017 cport, pmport) == NULL) 16018 return; 16019 port_state = SATA_PMPORT_STATE(sata_hba_inst, cport, pmport); 16020 if (port_state & SATA_PSTATE_SHUTDOWN || 16021 port_state & SATA_PSTATE_FAILED) { 16022 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 16023 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 16024 if (port_state & SATA_PSTATE_FAILED) 16025 ap_state->ap_condition = AP_COND_FAILED; 16026 else 16027 ap_state->ap_condition = AP_COND_UNKNOWN; 16028 16029 return; 16030 } 16031 } 16032 16033 /* Port is enabled and ready */ 16034 if (qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_CPORT) 16035 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, cport); 16036 else 16037 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport); 16038 16039 switch (dev_type) { 16040 case SATA_DTYPE_NONE: 16041 { 16042 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 16043 ap_state->ap_condition = AP_COND_OK; 16044 /* No device attached */ 16045 ap_state->ap_rstate = AP_RSTATE_EMPTY; 16046 break; 16047 } 16048 case SATA_DTYPE_PMULT: 16049 { 16050 /* Need to check port multiplier state */ 16051 ASSERT(qual == SATA_ADDR_DCPORT); 16052 pmult_state = SATA_PMULT_INFO(sata_hba_inst, cport)-> 16053 pmult_state; 16054 if (pmult_state & (SATA_PSTATE_SHUTDOWN|SATA_PSTATE_FAILED)) { 16055 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 16056 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 16057 if (pmult_state & SATA_PSTATE_FAILED) 16058 ap_state->ap_condition = AP_COND_FAILED; 16059 else 16060 ap_state->ap_condition = AP_COND_UNKNOWN; 16061 16062 return; 16063 } 16064 16065 /* Port multiplier is not configurable */ 16066 ap_state->ap_ostate = AP_OSTATE_CONFIGURED; 16067 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 16068 ap_state->ap_condition = AP_COND_OK; 16069 break; 16070 } 16071 16072 case SATA_DTYPE_ATADISK: 16073 case SATA_DTYPE_ATAPICD: 16074 case SATA_DTYPE_ATAPITAPE: 16075 case SATA_DTYPE_ATAPIDISK: 16076 { 16077 dev_info_t *tdip = NULL; 16078 dev_info_t *dip = NULL; 16079 16080 dip = SATA_DIP(sata_hba_inst); 16081 tdip = sata_get_target_dip(dip, cport, pmport); 16082 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 16083 if (tdip != NULL) { 16084 ndi_devi_enter(dip); 16085 mutex_enter(&(DEVI(tdip)->devi_lock)); 16086 if (DEVI_IS_DEVICE_REMOVED(tdip)) { 16087 /* 16088 * There could be the case where previously 16089 * configured and opened device was removed 16090 * and unknown device was plugged. 16091 * In such case we want to show a device, and 16092 * its configured or unconfigured state but 16093 * indicate unusable condition untill the 16094 * old target node is released and removed. 16095 */ 16096 ap_state->ap_condition = AP_COND_UNUSABLE; 16097 } else { 16098 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, 16099 cport)); 16100 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 16101 cport); 16102 if (sdinfo != NULL) { 16103 if ((sdinfo->satadrv_state & 16104 SATA_DSTATE_FAILED) != 0) 16105 ap_state->ap_condition = 16106 AP_COND_FAILED; 16107 else 16108 ap_state->ap_condition = 16109 AP_COND_OK; 16110 } else { 16111 ap_state->ap_condition = 16112 AP_COND_UNKNOWN; 16113 } 16114 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, 16115 cport)); 16116 } 16117 if ((DEVI_IS_DEVICE_OFFLINE(tdip)) || 16118 (DEVI_IS_DEVICE_DOWN(tdip))) { 16119 ap_state->ap_ostate = 16120 AP_OSTATE_UNCONFIGURED; 16121 } else { 16122 ap_state->ap_ostate = 16123 AP_OSTATE_CONFIGURED; 16124 } 16125 mutex_exit(&(DEVI(tdip)->devi_lock)); 16126 ndi_devi_exit(dip); 16127 } else { 16128 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 16129 ap_state->ap_condition = AP_COND_UNKNOWN; 16130 } 16131 break; 16132 } 16133 case SATA_DTYPE_ATAPIPROC: 16134 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 16135 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 16136 ap_state->ap_condition = AP_COND_OK; 16137 break; 16138 default: 16139 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 16140 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 16141 ap_state->ap_condition = AP_COND_UNKNOWN; 16142 /* 16143 * This is actually internal error condition (non fatal), 16144 * because we have already checked all defined device types. 16145 */ 16146 SATA_LOG_D((sata_hba_inst, CE_WARN, 16147 "sata_cfgadm_state: Internal error: " 16148 "unknown device type")); 16149 break; 16150 } 16151 } 16152 16153 16154 /* 16155 * Process ioctl get device path request. 16156 * 16157 * NOTE: Port multiplier has no target dip. Devices connected to port 16158 * multiplier have target node attached to the HBA node. The only difference 16159 * between them and the directly-attached device node is a target address. 16160 */ 16161 static int 16162 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst, 16163 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 16164 { 16165 char path[MAXPATHLEN]; 16166 uint32_t size; 16167 dev_info_t *tdip; 16168 16169 (void) strcpy(path, "/devices"); 16170 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 16171 &sata_device->satadev_addr)) == NULL) { 16172 /* 16173 * No such device. If this is a request for a size, do not 16174 * return EINVAL for non-existing target, because cfgadm 16175 * will then indicate a meaningless ioctl failure. 16176 * If this is a request for a path, indicate invalid 16177 * argument. 16178 */ 16179 if (ioc->get_size == 0) 16180 return (EINVAL); 16181 } else { 16182 (void) ddi_pathname(tdip, path + strlen(path)); 16183 } 16184 size = strlen(path) + 1; 16185 16186 if (ioc->get_size != 0) { 16187 if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz, 16188 mode) != 0) 16189 return (EFAULT); 16190 } else { 16191 if (ioc->bufsiz != size) 16192 return (EINVAL); 16193 16194 else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz, 16195 mode) != 0) 16196 return (EFAULT); 16197 } 16198 return (0); 16199 } 16200 16201 /* 16202 * Process ioctl get attachment point type request. 16203 * 16204 * NOTE: Port multiplier is supported. 16205 */ 16206 static int 16207 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst, 16208 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 16209 { 16210 uint32_t type_len; 16211 const char *ap_type; 16212 int dev_type; 16213 16214 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 16215 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, 16216 sata_device->satadev_addr.cport); 16217 else /* pmport */ 16218 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, 16219 sata_device->satadev_addr.cport, 16220 sata_device->satadev_addr.pmport); 16221 16222 switch (dev_type) { 16223 case SATA_DTYPE_NONE: 16224 ap_type = "port"; 16225 break; 16226 16227 case SATA_DTYPE_ATADISK: 16228 case SATA_DTYPE_ATAPIDISK: 16229 ap_type = "disk"; 16230 break; 16231 16232 case SATA_DTYPE_ATAPICD: 16233 ap_type = "cd/dvd"; 16234 break; 16235 16236 case SATA_DTYPE_ATAPITAPE: 16237 ap_type = "tape"; 16238 break; 16239 16240 case SATA_DTYPE_ATAPIPROC: 16241 ap_type = "processor"; 16242 break; 16243 16244 case SATA_DTYPE_PMULT: 16245 ap_type = "sata-pmult"; 16246 break; 16247 16248 case SATA_DTYPE_UNKNOWN: 16249 ap_type = "unknown"; 16250 break; 16251 16252 default: 16253 ap_type = "unsupported"; 16254 break; 16255 16256 } /* end of dev_type switch */ 16257 16258 type_len = strlen(ap_type) + 1; 16259 16260 if (ioc->get_size) { 16261 if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz, 16262 mode) != 0) 16263 return (EFAULT); 16264 } else { 16265 if (ioc->bufsiz != type_len) 16266 return (EINVAL); 16267 16268 if (ddi_copyout((void *)ap_type, ioc->buf, 16269 ioc->bufsiz, mode) != 0) 16270 return (EFAULT); 16271 } 16272 return (0); 16273 16274 } 16275 16276 /* 16277 * Process ioctl get device model info request. 16278 * This operation should return to cfgadm the device model 16279 * information string 16280 * 16281 * NOTE: Port multiplier is supported. 16282 */ 16283 static int 16284 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst, 16285 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 16286 { 16287 sata_drive_info_t *sdinfo; 16288 uint32_t info_len; 16289 char ap_info[SATA_ID_MODEL_LEN + 1]; 16290 16291 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 16292 sata_device->satadev_addr.cport)->cport_mutex); 16293 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 16294 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 16295 sata_device->satadev_addr.cport); 16296 else /* port multiplier */ 16297 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 16298 sata_device->satadev_addr.cport, 16299 sata_device->satadev_addr.pmport); 16300 if (sdinfo == NULL) { 16301 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 16302 sata_device->satadev_addr.cport)->cport_mutex); 16303 return (EINVAL); 16304 } 16305 16306 #ifdef _LITTLE_ENDIAN 16307 swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN); 16308 #else /* _LITTLE_ENDIAN */ 16309 bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN); 16310 #endif /* _LITTLE_ENDIAN */ 16311 16312 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 16313 sata_device->satadev_addr.cport)->cport_mutex); 16314 16315 ap_info[SATA_ID_MODEL_LEN] = '\0'; 16316 16317 info_len = strlen(ap_info) + 1; 16318 16319 if (ioc->get_size) { 16320 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 16321 mode) != 0) 16322 return (EFAULT); 16323 } else { 16324 if (ioc->bufsiz < info_len) 16325 return (EINVAL); 16326 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 16327 mode) != 0) 16328 return (EFAULT); 16329 } 16330 return (0); 16331 } 16332 16333 16334 /* 16335 * Process ioctl get device firmware revision info request. 16336 * This operation should return to cfgadm the device firmware revision 16337 * information string 16338 * 16339 * Port multiplier is supported. 16340 */ 16341 static int 16342 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst, 16343 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 16344 { 16345 sata_drive_info_t *sdinfo; 16346 uint32_t info_len; 16347 char ap_info[SATA_ID_FW_LEN + 1]; 16348 16349 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 16350 sata_device->satadev_addr.cport)->cport_mutex); 16351 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 16352 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 16353 sata_device->satadev_addr.cport); 16354 else /* port multiplier */ 16355 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 16356 sata_device->satadev_addr.cport, 16357 sata_device->satadev_addr.pmport); 16358 if (sdinfo == NULL) { 16359 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 16360 sata_device->satadev_addr.cport)->cport_mutex); 16361 return (EINVAL); 16362 } 16363 16364 #ifdef _LITTLE_ENDIAN 16365 swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN); 16366 #else /* _LITTLE_ENDIAN */ 16367 bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN); 16368 #endif /* _LITTLE_ENDIAN */ 16369 16370 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 16371 sata_device->satadev_addr.cport)->cport_mutex); 16372 16373 ap_info[SATA_ID_FW_LEN] = '\0'; 16374 16375 info_len = strlen(ap_info) + 1; 16376 16377 if (ioc->get_size) { 16378 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 16379 mode) != 0) 16380 return (EFAULT); 16381 } else { 16382 if (ioc->bufsiz < info_len) 16383 return (EINVAL); 16384 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 16385 mode) != 0) 16386 return (EFAULT); 16387 } 16388 return (0); 16389 } 16390 16391 16392 /* 16393 * Process ioctl get device serial number info request. 16394 * This operation should return to cfgadm the device serial number string. 16395 * 16396 * NOTE: Port multiplier is supported. 16397 */ 16398 static int 16399 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst, 16400 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 16401 { 16402 sata_drive_info_t *sdinfo; 16403 uint32_t info_len; 16404 char ap_info[SATA_ID_SERIAL_LEN + 1]; 16405 16406 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 16407 sata_device->satadev_addr.cport)->cport_mutex); 16408 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 16409 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 16410 sata_device->satadev_addr.cport); 16411 else /* port multiplier */ 16412 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 16413 sata_device->satadev_addr.cport, 16414 sata_device->satadev_addr.pmport); 16415 if (sdinfo == NULL) { 16416 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 16417 sata_device->satadev_addr.cport)->cport_mutex); 16418 return (EINVAL); 16419 } 16420 16421 #ifdef _LITTLE_ENDIAN 16422 swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN); 16423 #else /* _LITTLE_ENDIAN */ 16424 bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN); 16425 #endif /* _LITTLE_ENDIAN */ 16426 16427 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 16428 sata_device->satadev_addr.cport)->cport_mutex); 16429 16430 ap_info[SATA_ID_SERIAL_LEN] = '\0'; 16431 16432 info_len = strlen(ap_info) + 1; 16433 16434 if (ioc->get_size) { 16435 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 16436 mode) != 0) 16437 return (EFAULT); 16438 } else { 16439 if (ioc->bufsiz < info_len) 16440 return (EINVAL); 16441 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 16442 mode) != 0) 16443 return (EFAULT); 16444 } 16445 return (0); 16446 } 16447 16448 16449 /* 16450 * Preset scsi extended sense data (to NO SENSE) 16451 * First 18 bytes of the sense data are preset to current valid sense 16452 * with a key NO SENSE data. 16453 * 16454 * Returns void 16455 */ 16456 static void 16457 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense) 16458 { 16459 sense->es_valid = 1; /* Valid sense */ 16460 sense->es_class = CLASS_EXTENDED_SENSE; /* 0x70 - current err */ 16461 sense->es_key = KEY_NO_SENSE; 16462 sense->es_info_1 = 0; 16463 sense->es_info_2 = 0; 16464 sense->es_info_3 = 0; 16465 sense->es_info_4 = 0; 16466 sense->es_add_len = 10; /* Additional length - replace with a def */ 16467 sense->es_cmd_info[0] = 0; 16468 sense->es_cmd_info[1] = 0; 16469 sense->es_cmd_info[2] = 0; 16470 sense->es_cmd_info[3] = 0; 16471 sense->es_add_code = 0; 16472 sense->es_qual_code = 0; 16473 } 16474 16475 /* 16476 * Register a legacy cmdk-style devid for the target (disk) device. 16477 * 16478 * Note: This function is called only when the HBA devinfo node has the 16479 * property "use-cmdk-devid-format" set. This property indicates that 16480 * devid compatible with old cmdk (target) driver is to be generated 16481 * for any target device attached to this controller. This will take 16482 * precedence over the devid generated by sd (target) driver. 16483 * This function is derived from cmdk_devid_setup() function in cmdk.c. 16484 */ 16485 static void 16486 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo) 16487 { 16488 char *hwid; 16489 int modlen; 16490 int serlen; 16491 int rval; 16492 ddi_devid_t devid; 16493 16494 /* 16495 * device ID is a concatanation of model number, "=", serial number. 16496 */ 16497 hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP); 16498 bcopy(&sdinfo->satadrv_id.ai_model, hwid, 16499 sizeof (sdinfo->satadrv_id.ai_model)); 16500 swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model)); 16501 modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model)); 16502 if (modlen == 0) 16503 goto err; 16504 hwid[modlen++] = '='; 16505 bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen], 16506 sizeof (sdinfo->satadrv_id.ai_drvser)); 16507 swab(&hwid[modlen], &hwid[modlen], 16508 sizeof (sdinfo->satadrv_id.ai_drvser)); 16509 serlen = sata_check_modser(&hwid[modlen], 16510 sizeof (sdinfo->satadrv_id.ai_drvser)); 16511 if (serlen == 0) 16512 goto err; 16513 hwid[modlen + serlen] = 0; /* terminate the hwid string */ 16514 16515 /* initialize/register devid */ 16516 if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL, 16517 (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) { 16518 rval = ddi_devid_register(dip, devid); 16519 /* 16520 * Free up the allocated devid buffer. 16521 * NOTE: This doesn't mean unregistering devid. 16522 */ 16523 ddi_devid_free(devid); 16524 } 16525 16526 if (rval != DDI_SUCCESS) 16527 cmn_err(CE_WARN, "sata: failed to create devid for the disk" 16528 " on port %d", sdinfo->satadrv_addr.cport); 16529 err: 16530 kmem_free(hwid, LEGACY_HWID_LEN); 16531 } 16532 16533 /* 16534 * valid model/serial string must contain a non-zero non-space characters. 16535 * trim trailing spaces/NULLs. 16536 */ 16537 static int 16538 sata_check_modser(char *buf, int buf_len) 16539 { 16540 boolean_t ret; 16541 char *s; 16542 int i; 16543 int tb; 16544 char ch; 16545 16546 ret = B_FALSE; 16547 s = buf; 16548 for (i = 0; i < buf_len; i++) { 16549 ch = *s++; 16550 if (ch != ' ' && ch != '\0') 16551 tb = i + 1; 16552 if (ch != ' ' && ch != '\0' && ch != '0') 16553 ret = B_TRUE; 16554 } 16555 16556 if (ret == B_FALSE) 16557 return (0); /* invalid string */ 16558 16559 return (tb); /* return length */ 16560 } 16561 16562 /* 16563 * sata_set_drive_features function compares current device features setting 16564 * with the saved device features settings and, if there is a difference, 16565 * it restores device features setting to the previously saved state. 16566 * It also arbitrarily tries to select the highest supported DMA mode. 16567 * Device Identify or Identify Packet Device data has to be current. 16568 * At the moment read ahead and write cache are considered for all devices. 16569 * For atapi devices, Removable Media Status Notification is set in addition 16570 * to common features. 16571 * 16572 * This function cannot be called in the interrupt context (it may sleep). 16573 * 16574 * The input argument sdinfo should point to the drive info structure 16575 * to be updated after features are set. Note, that only 16576 * device (packet) identify data is updated, not the flags indicating the 16577 * supported features. 16578 * 16579 * Returns SATA_SUCCESS if successful or there was nothing to do. 16580 * Device Identify data in the drive info structure pointed to by the sdinfo 16581 * arguments is updated even when no features were set or changed. 16582 * 16583 * Returns SATA_FAILURE if device features could not be set or DMA mode 16584 * for a disk cannot be set and device identify data cannot be fetched. 16585 * 16586 * Returns SATA_RETRY if device features could not be set (other than disk 16587 * DMA mode) but the device identify data was fetched successfully. 16588 * 16589 * Note: This function may fail the port, making it inaccessible. 16590 * In such case the explicit port disconnect/connect or physical device 16591 * detach/attach is required to re-evaluate port state again. 16592 */ 16593 16594 static int 16595 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst, 16596 sata_drive_info_t *sdinfo, int restore) 16597 { 16598 int rval = SATA_SUCCESS; 16599 int rval_set; 16600 sata_drive_info_t new_sdinfo; 16601 char *finfo = "sata_set_drive_features: cannot"; 16602 char *finfox; 16603 int cache_op; 16604 16605 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 16606 new_sdinfo.satadrv_addr = sdinfo->satadrv_addr; 16607 new_sdinfo.satadrv_type = sdinfo->satadrv_type; 16608 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 16609 /* 16610 * Cannot get device identification - caller may retry later 16611 */ 16612 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16613 "%s fetch device identify data\n", finfo); 16614 return (SATA_FAILURE); 16615 } 16616 finfox = (restore != 0) ? " restore device features" : 16617 " initialize device features\n"; 16618 16619 switch (sdinfo->satadrv_type) { 16620 case SATA_DTYPE_ATADISK: 16621 /* Arbitrarily set UDMA mode */ 16622 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) != 16623 SATA_SUCCESS) { 16624 SATA_LOG_D((sata_hba_inst, CE_WARN, 16625 "%s set UDMA mode\n", finfo)); 16626 return (SATA_FAILURE); 16627 } 16628 break; 16629 case SATA_DTYPE_ATAPICD: 16630 case SATA_DTYPE_ATAPITAPE: 16631 case SATA_DTYPE_ATAPIDISK: 16632 /* Set Removable Media Status Notification, if necessary */ 16633 if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) && 16634 restore != 0) { 16635 if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) && 16636 (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))|| 16637 ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) && 16638 SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) { 16639 /* Current setting does not match saved one */ 16640 if (sata_set_rmsn(sata_hba_inst, sdinfo, 16641 sdinfo->satadrv_settings & 16642 SATA_DEV_RMSN) != SATA_SUCCESS) 16643 rval = SATA_FAILURE; 16644 } 16645 } 16646 /* 16647 * We have to set Multiword DMA or UDMA, if it is supported, as 16648 * we want to use DMA transfer mode whenever possible. 16649 * Some devices require explicit setting of the DMA mode. 16650 */ 16651 if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) { 16652 /* Set highest supported DMA mode */ 16653 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) != 16654 SATA_SUCCESS) { 16655 SATA_LOG_D((sata_hba_inst, CE_WARN, 16656 "%s set UDMA mode\n", finfo)); 16657 rval = SATA_FAILURE; 16658 } 16659 } 16660 break; 16661 } 16662 16663 if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) && 16664 !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) { 16665 /* 16666 * neither READ AHEAD nor WRITE CACHE is supported 16667 * - do nothing 16668 */ 16669 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16670 "settable features not supported\n", NULL); 16671 goto update_sdinfo; 16672 } 16673 16674 if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) && 16675 (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) && 16676 (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) && 16677 (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) { 16678 /* 16679 * both READ AHEAD and WRITE CACHE are enabled 16680 * - Nothing to do 16681 */ 16682 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16683 "no device features to set\n", NULL); 16684 goto update_sdinfo; 16685 } 16686 16687 cache_op = 0; 16688 16689 if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) { 16690 if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) && 16691 !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) { 16692 /* Enable read ahead / read cache */ 16693 cache_op = SATAC_SF_ENABLE_READ_AHEAD; 16694 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16695 "enabling read cache\n", NULL); 16696 } else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) && 16697 SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) { 16698 /* Disable read ahead / read cache */ 16699 cache_op = SATAC_SF_DISABLE_READ_AHEAD; 16700 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16701 "disabling read cache\n", NULL); 16702 } 16703 16704 if (cache_op != 0) { 16705 /* Try to set read cache mode */ 16706 rval_set = sata_set_cache_mode(sata_hba_inst, 16707 &new_sdinfo, cache_op); 16708 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS) 16709 rval = rval_set; 16710 } 16711 } 16712 16713 cache_op = 0; 16714 16715 if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) { 16716 if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) && 16717 !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) { 16718 /* Enable write cache */ 16719 cache_op = SATAC_SF_ENABLE_WRITE_CACHE; 16720 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16721 "enabling write cache\n", NULL); 16722 } else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) && 16723 SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) { 16724 /* Disable write cache */ 16725 cache_op = SATAC_SF_DISABLE_WRITE_CACHE; 16726 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 16727 "disabling write cache\n", NULL); 16728 } 16729 16730 if (cache_op != 0) { 16731 /* Try to set write cache mode */ 16732 rval_set = sata_set_cache_mode(sata_hba_inst, 16733 &new_sdinfo, cache_op); 16734 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS) 16735 rval = rval_set; 16736 } 16737 } 16738 if (rval != SATA_SUCCESS) 16739 SATA_LOG_D((sata_hba_inst, CE_WARN, 16740 "%s %s", finfo, finfox)); 16741 16742 update_sdinfo: 16743 /* 16744 * We need to fetch Device Identify data again 16745 */ 16746 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 16747 /* 16748 * Cannot get device identification - retry later 16749 */ 16750 SATA_LOG_D((sata_hba_inst, CE_WARN, 16751 "%s re-fetch device identify data\n", finfo)); 16752 rval = SATA_FAILURE; 16753 } 16754 /* Copy device sata info. */ 16755 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 16756 16757 return (rval); 16758 } 16759 16760 16761 /* 16762 * 16763 * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if 16764 * unable to determine. 16765 * 16766 * Cannot be called in an interrupt context. 16767 * 16768 * Called by sata_build_lsense_page_2f() 16769 */ 16770 16771 static int 16772 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst, 16773 sata_drive_info_t *sdinfo) 16774 { 16775 sata_pkt_t *spkt; 16776 sata_cmd_t *scmd; 16777 sata_pkt_txlate_t *spx; 16778 int rval; 16779 16780 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 16781 spx->txlt_sata_hba_inst = sata_hba_inst; 16782 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 16783 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 16784 if (spkt == NULL) { 16785 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16786 return (-1); 16787 } 16788 /* address is needed now */ 16789 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16790 16791 16792 /* Fill sata_pkt */ 16793 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16794 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 16795 /* Synchronous mode, no callback */ 16796 spkt->satapkt_comp = NULL; 16797 /* Timeout 30s */ 16798 spkt->satapkt_time = sata_default_pkt_time; 16799 16800 scmd = &spkt->satapkt_cmd; 16801 scmd->satacmd_flags.sata_special_regs = B_TRUE; 16802 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 16803 16804 /* Set up which registers need to be returned */ 16805 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE; 16806 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE; 16807 16808 /* Build SMART_RETURN_STATUS cmd in the sata_pkt */ 16809 scmd->satacmd_addr_type = 0; /* N/A */ 16810 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 16811 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 16812 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 16813 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 16814 scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS; 16815 scmd->satacmd_device_reg = 0; /* Always device 0 */ 16816 scmd->satacmd_cmd_reg = SATAC_SMART; 16817 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 16818 sdinfo->satadrv_addr.cport))); 16819 16820 16821 /* Send pkt to SATA HBA driver */ 16822 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 16823 SATA_TRAN_ACCEPTED || 16824 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 16825 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16826 sdinfo->satadrv_addr.cport))); 16827 /* 16828 * Whoops, no SMART RETURN STATUS 16829 */ 16830 rval = -1; 16831 } else { 16832 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16833 sdinfo->satadrv_addr.cport))); 16834 if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) { 16835 rval = -1; 16836 goto fail; 16837 } 16838 if (scmd->satacmd_status_reg & SATA_STATUS_ERR) { 16839 rval = -1; 16840 goto fail; 16841 } 16842 if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) && 16843 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2)) 16844 rval = 0; 16845 else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) && 16846 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4)) 16847 rval = 1; 16848 else { 16849 rval = -1; 16850 goto fail; 16851 } 16852 } 16853 fail: 16854 /* Free allocated resources */ 16855 sata_pkt_free(spx); 16856 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16857 16858 return (rval); 16859 } 16860 16861 /* 16862 * 16863 * Returns 0 if succeeded, -1 otherwise 16864 * 16865 * Cannot be called in an interrupt context. 16866 * 16867 */ 16868 static int 16869 sata_fetch_smart_data(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 16870 struct smart_data *smart_data) 16871 { 16872 sata_pkt_t *spkt; 16873 sata_cmd_t *scmd; 16874 sata_pkt_txlate_t *spx; 16875 int rval = 0; 16876 dev_info_t *dip = SATA_DIP(sata_hba_inst); 16877 16878 #if ! defined(lint) 16879 ASSERT(sizeof (struct smart_data) == 512); 16880 #endif 16881 16882 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 16883 spx->txlt_sata_hba_inst = sata_hba_inst; 16884 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 16885 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 16886 if (spkt == NULL) { 16887 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16888 return (-1); 16889 } 16890 /* address is needed now */ 16891 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16892 16893 16894 /* Fill sata_pkt */ 16895 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 16896 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 16897 /* Synchronous mode, no callback */ 16898 spkt->satapkt_comp = NULL; 16899 /* Timeout 30s */ 16900 spkt->satapkt_time = sata_default_pkt_time; 16901 16902 scmd = &spkt->satapkt_cmd; 16903 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 16904 16905 /* 16906 * Allocate buffer for SMART data 16907 */ 16908 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 16909 sizeof (struct smart_data)); 16910 if (scmd->satacmd_bp == NULL) { 16911 sata_pkt_free(spx); 16912 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16913 SATA_LOG_D((sata_hba_inst, CE_WARN, 16914 "sata_fetch_smart_data: " 16915 "cannot allocate buffer")); 16916 return (-1); 16917 } 16918 16919 16920 /* Build SMART_READ_DATA cmd in the sata_pkt */ 16921 scmd->satacmd_addr_type = 0; /* N/A */ 16922 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 16923 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 16924 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 16925 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 16926 scmd->satacmd_features_reg = SATA_SMART_READ_DATA; 16927 scmd->satacmd_device_reg = 0; /* Always device 0 */ 16928 scmd->satacmd_cmd_reg = SATAC_SMART; 16929 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 16930 sdinfo->satadrv_addr.cport))); 16931 16932 /* Send pkt to SATA HBA driver */ 16933 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 16934 SATA_TRAN_ACCEPTED || 16935 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 16936 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16937 sdinfo->satadrv_addr.cport))); 16938 /* 16939 * Whoops, no SMART DATA available 16940 */ 16941 rval = -1; 16942 goto fail; 16943 } else { 16944 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 16945 sdinfo->satadrv_addr.cport))); 16946 if (spx->txlt_buf_dma_handle != NULL) { 16947 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 16948 DDI_DMA_SYNC_FORKERNEL); 16949 ASSERT(rval == DDI_SUCCESS); 16950 if (sata_check_for_dma_error(dip, spx)) { 16951 ddi_fm_service_impact(dip, 16952 DDI_SERVICE_UNAFFECTED); 16953 rval = -1; 16954 goto fail; 16955 } 16956 } 16957 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data, 16958 sizeof (struct smart_data)); 16959 } 16960 16961 fail: 16962 /* Free allocated resources */ 16963 sata_free_local_buffer(spx); 16964 sata_pkt_free(spx); 16965 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 16966 16967 return (rval); 16968 } 16969 16970 /* 16971 * Used by LOG SENSE page 0x10 16972 * Reads (in synchronous mode) the self test log data using Read Log Ext cmd. 16973 * Note: cannot be called in the interrupt context. 16974 * 16975 * return 0 for success, -1 otherwise 16976 * 16977 */ 16978 static int 16979 sata_ext_smart_selftest_read_log( 16980 sata_hba_inst_t *sata_hba_inst, 16981 sata_drive_info_t *sdinfo, 16982 struct smart_ext_selftest_log *ext_selftest_log, 16983 uint16_t block_num) 16984 { 16985 sata_pkt_txlate_t *spx; 16986 sata_pkt_t *spkt; 16987 sata_cmd_t *scmd; 16988 int rval; 16989 dev_info_t *dip = SATA_DIP(sata_hba_inst); 16990 16991 #if ! defined(lint) 16992 ASSERT(sizeof (struct smart_ext_selftest_log) == 512); 16993 #endif 16994 16995 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 16996 spx->txlt_sata_hba_inst = sata_hba_inst; 16997 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 16998 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 16999 if (spkt == NULL) { 17000 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17001 return (-1); 17002 } 17003 /* address is needed now */ 17004 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 17005 17006 17007 /* Fill sata_pkt */ 17008 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 17009 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 17010 /* Synchronous mode, no callback */ 17011 spkt->satapkt_comp = NULL; 17012 /* Timeout 30s */ 17013 spkt->satapkt_time = sata_default_pkt_time; 17014 17015 scmd = &spkt->satapkt_cmd; 17016 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 17017 17018 /* 17019 * Allocate buffer for SMART extended self-test log 17020 */ 17021 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 17022 sizeof (struct smart_ext_selftest_log)); 17023 if (scmd->satacmd_bp == NULL) { 17024 sata_pkt_free(spx); 17025 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17026 SATA_LOG_D((sata_hba_inst, CE_WARN, 17027 "sata_ext_smart_selftest_log: " 17028 "cannot allocate buffer")); 17029 return (-1); 17030 } 17031 17032 /* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */ 17033 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 17034 scmd->satacmd_sec_count_lsb = 1; /* One sector of selftest log */ 17035 scmd->satacmd_sec_count_msb = 0; /* One sector of selftest log */ 17036 scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE; 17037 scmd->satacmd_lba_low_msb = 0; 17038 scmd->satacmd_lba_mid_lsb = block_num & 0xff; 17039 scmd->satacmd_lba_mid_msb = block_num >> 8; 17040 scmd->satacmd_device_reg = 0; /* Always device 0 */ 17041 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 17042 17043 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 17044 sdinfo->satadrv_addr.cport))); 17045 17046 /* Send pkt to SATA HBA driver */ 17047 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 17048 SATA_TRAN_ACCEPTED || 17049 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 17050 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17051 sdinfo->satadrv_addr.cport))); 17052 17053 /* 17054 * Whoops, no SMART selftest log info available 17055 */ 17056 rval = -1; 17057 goto fail; 17058 } else { 17059 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17060 sdinfo->satadrv_addr.cport))); 17061 17062 if (spx->txlt_buf_dma_handle != NULL) { 17063 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 17064 DDI_DMA_SYNC_FORKERNEL); 17065 ASSERT(rval == DDI_SUCCESS); 17066 if (sata_check_for_dma_error(dip, spx)) { 17067 ddi_fm_service_impact(dip, 17068 DDI_SERVICE_UNAFFECTED); 17069 rval = -1; 17070 goto fail; 17071 } 17072 } 17073 bcopy(scmd->satacmd_bp->b_un.b_addr, 17074 (uint8_t *)ext_selftest_log, 17075 sizeof (struct smart_ext_selftest_log)); 17076 rval = 0; 17077 } 17078 17079 fail: 17080 /* Free allocated resources */ 17081 sata_free_local_buffer(spx); 17082 sata_pkt_free(spx); 17083 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17084 17085 return (rval); 17086 } 17087 17088 /* 17089 * Returns 0 for success, -1 otherwise 17090 * 17091 * SMART self-test log data is returned in buffer pointed to by selftest_log 17092 */ 17093 static int 17094 sata_smart_selftest_log( 17095 sata_hba_inst_t *sata_hba_inst, 17096 sata_drive_info_t *sdinfo, 17097 struct smart_selftest_log *selftest_log) 17098 { 17099 sata_pkt_t *spkt; 17100 sata_cmd_t *scmd; 17101 sata_pkt_txlate_t *spx; 17102 int rval; 17103 dev_info_t *dip = SATA_DIP(sata_hba_inst); 17104 17105 #if ! defined(lint) 17106 ASSERT(sizeof (struct smart_selftest_log) == 512); 17107 #endif 17108 17109 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 17110 spx->txlt_sata_hba_inst = sata_hba_inst; 17111 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 17112 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 17113 if (spkt == NULL) { 17114 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17115 return (-1); 17116 } 17117 /* address is needed now */ 17118 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 17119 17120 17121 /* Fill sata_pkt */ 17122 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 17123 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 17124 /* Synchronous mode, no callback */ 17125 spkt->satapkt_comp = NULL; 17126 /* Timeout 30s */ 17127 spkt->satapkt_time = sata_default_pkt_time; 17128 17129 scmd = &spkt->satapkt_cmd; 17130 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 17131 17132 /* 17133 * Allocate buffer for SMART SELFTEST LOG 17134 */ 17135 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 17136 sizeof (struct smart_selftest_log)); 17137 if (scmd->satacmd_bp == NULL) { 17138 sata_pkt_free(spx); 17139 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17140 SATA_LOG_D((sata_hba_inst, CE_WARN, 17141 "sata_smart_selftest_log: " 17142 "cannot allocate buffer")); 17143 return (-1); 17144 } 17145 17146 /* Build SMART_READ_LOG cmd in the sata_pkt */ 17147 scmd->satacmd_addr_type = 0; /* N/A */ 17148 scmd->satacmd_sec_count_lsb = 1; /* One sector of SMART log */ 17149 scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE; 17150 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 17151 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 17152 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 17153 scmd->satacmd_device_reg = 0; /* Always device 0 */ 17154 scmd->satacmd_cmd_reg = SATAC_SMART; 17155 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 17156 sdinfo->satadrv_addr.cport))); 17157 17158 /* Send pkt to SATA HBA driver */ 17159 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 17160 SATA_TRAN_ACCEPTED || 17161 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 17162 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17163 sdinfo->satadrv_addr.cport))); 17164 /* 17165 * Whoops, no SMART DATA available 17166 */ 17167 rval = -1; 17168 goto fail; 17169 } else { 17170 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17171 sdinfo->satadrv_addr.cport))); 17172 if (spx->txlt_buf_dma_handle != NULL) { 17173 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 17174 DDI_DMA_SYNC_FORKERNEL); 17175 ASSERT(rval == DDI_SUCCESS); 17176 if (sata_check_for_dma_error(dip, spx)) { 17177 ddi_fm_service_impact(dip, 17178 DDI_SERVICE_UNAFFECTED); 17179 rval = -1; 17180 goto fail; 17181 } 17182 } 17183 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log, 17184 sizeof (struct smart_selftest_log)); 17185 rval = 0; 17186 } 17187 17188 fail: 17189 /* Free allocated resources */ 17190 sata_free_local_buffer(spx); 17191 sata_pkt_free(spx); 17192 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17193 17194 return (rval); 17195 } 17196 17197 17198 /* 17199 * Returns 0 for success, -1 otherwise 17200 * 17201 * SMART READ LOG data is returned in buffer pointed to by smart_log 17202 */ 17203 static int 17204 sata_smart_read_log( 17205 sata_hba_inst_t *sata_hba_inst, 17206 sata_drive_info_t *sdinfo, 17207 uint8_t *smart_log, /* where the data should be returned */ 17208 uint8_t which_log, /* which log should be returned */ 17209 uint8_t log_size) /* # of 512 bytes in log */ 17210 { 17211 sata_pkt_t *spkt; 17212 sata_cmd_t *scmd; 17213 sata_pkt_txlate_t *spx; 17214 int rval; 17215 dev_info_t *dip = SATA_DIP(sata_hba_inst); 17216 17217 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 17218 spx->txlt_sata_hba_inst = sata_hba_inst; 17219 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 17220 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 17221 if (spkt == NULL) { 17222 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17223 return (-1); 17224 } 17225 /* address is needed now */ 17226 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 17227 17228 17229 /* Fill sata_pkt */ 17230 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 17231 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 17232 /* Synchronous mode, no callback */ 17233 spkt->satapkt_comp = NULL; 17234 /* Timeout 30s */ 17235 spkt->satapkt_time = sata_default_pkt_time; 17236 17237 scmd = &spkt->satapkt_cmd; 17238 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 17239 17240 /* 17241 * Allocate buffer for SMART READ LOG 17242 */ 17243 scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512); 17244 if (scmd->satacmd_bp == NULL) { 17245 sata_pkt_free(spx); 17246 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17247 SATA_LOG_D((sata_hba_inst, CE_WARN, 17248 "sata_smart_read_log: " "cannot allocate buffer")); 17249 return (-1); 17250 } 17251 17252 /* Build SMART_READ_LOG cmd in the sata_pkt */ 17253 scmd->satacmd_addr_type = 0; /* N/A */ 17254 scmd->satacmd_sec_count_lsb = log_size; /* what the caller asked for */ 17255 scmd->satacmd_lba_low_lsb = which_log; /* which log page */ 17256 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 17257 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 17258 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 17259 scmd->satacmd_device_reg = 0; /* Always device 0 */ 17260 scmd->satacmd_cmd_reg = SATAC_SMART; 17261 17262 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 17263 sdinfo->satadrv_addr.cport))); 17264 17265 /* Send pkt to SATA HBA driver */ 17266 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 17267 SATA_TRAN_ACCEPTED || 17268 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 17269 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17270 sdinfo->satadrv_addr.cport))); 17271 17272 /* 17273 * Whoops, no SMART DATA available 17274 */ 17275 rval = -1; 17276 goto fail; 17277 } else { 17278 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17279 sdinfo->satadrv_addr.cport))); 17280 17281 if (spx->txlt_buf_dma_handle != NULL) { 17282 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 17283 DDI_DMA_SYNC_FORKERNEL); 17284 ASSERT(rval == DDI_SUCCESS); 17285 if (sata_check_for_dma_error(dip, spx)) { 17286 ddi_fm_service_impact(dip, 17287 DDI_SERVICE_UNAFFECTED); 17288 rval = -1; 17289 goto fail; 17290 } 17291 } 17292 bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512); 17293 rval = 0; 17294 } 17295 17296 fail: 17297 /* Free allocated resources */ 17298 sata_free_local_buffer(spx); 17299 sata_pkt_free(spx); 17300 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17301 17302 return (rval); 17303 } 17304 17305 /* 17306 * Used by LOG SENSE page 0x10 17307 * 17308 * return 0 for success, -1 otherwise 17309 * 17310 */ 17311 static int 17312 sata_read_log_ext_directory( 17313 sata_hba_inst_t *sata_hba_inst, 17314 sata_drive_info_t *sdinfo, 17315 struct read_log_ext_directory *logdir) 17316 { 17317 sata_pkt_txlate_t *spx; 17318 sata_pkt_t *spkt; 17319 sata_cmd_t *scmd; 17320 int rval; 17321 dev_info_t *dip = SATA_DIP(sata_hba_inst); 17322 17323 #if ! defined(lint) 17324 ASSERT(sizeof (struct read_log_ext_directory) == 512); 17325 #endif 17326 17327 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 17328 spx->txlt_sata_hba_inst = sata_hba_inst; 17329 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 17330 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 17331 if (spkt == NULL) { 17332 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17333 return (-1); 17334 } 17335 17336 /* Fill sata_pkt */ 17337 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 17338 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 17339 /* Synchronous mode, no callback */ 17340 spkt->satapkt_comp = NULL; 17341 /* Timeout 30s */ 17342 spkt->satapkt_time = sata_default_pkt_time; 17343 17344 scmd = &spkt->satapkt_cmd; 17345 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 17346 17347 /* 17348 * Allocate buffer for SMART READ LOG EXTENDED command 17349 */ 17350 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 17351 sizeof (struct read_log_ext_directory)); 17352 if (scmd->satacmd_bp == NULL) { 17353 sata_pkt_free(spx); 17354 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17355 SATA_LOG_D((sata_hba_inst, CE_WARN, 17356 "sata_read_log_ext_directory: " 17357 "cannot allocate buffer")); 17358 return (-1); 17359 } 17360 17361 /* Build READ LOG EXT w/ log directory cmd in the sata_pkt */ 17362 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 17363 scmd->satacmd_sec_count_lsb = 1; /* One sector of directory */ 17364 scmd->satacmd_sec_count_msb = 0; /* One sector of directory */ 17365 scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY; 17366 scmd->satacmd_lba_low_msb = 0; 17367 scmd->satacmd_lba_mid_lsb = 0; 17368 scmd->satacmd_lba_mid_msb = 0; 17369 scmd->satacmd_device_reg = 0; /* Always device 0 */ 17370 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 17371 17372 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 17373 sdinfo->satadrv_addr.cport))); 17374 17375 /* Send pkt to SATA HBA driver */ 17376 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 17377 SATA_TRAN_ACCEPTED || 17378 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 17379 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17380 sdinfo->satadrv_addr.cport))); 17381 /* 17382 * Whoops, no SMART selftest log info available 17383 */ 17384 rval = -1; 17385 goto fail; 17386 } else { 17387 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 17388 sdinfo->satadrv_addr.cport))); 17389 if (spx->txlt_buf_dma_handle != NULL) { 17390 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 17391 DDI_DMA_SYNC_FORKERNEL); 17392 ASSERT(rval == DDI_SUCCESS); 17393 if (sata_check_for_dma_error(dip, spx)) { 17394 ddi_fm_service_impact(dip, 17395 DDI_SERVICE_UNAFFECTED); 17396 rval = -1; 17397 goto fail; 17398 } 17399 } 17400 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir, 17401 sizeof (struct read_log_ext_directory)); 17402 rval = 0; 17403 } 17404 17405 fail: 17406 /* Free allocated resources */ 17407 sata_free_local_buffer(spx); 17408 sata_pkt_free(spx); 17409 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 17410 17411 return (rval); 17412 } 17413 17414 /* 17415 * Set up error retrieval sata command for NCQ command error data 17416 * recovery. 17417 * 17418 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up, 17419 * returns SATA_FAILURE otherwise. 17420 */ 17421 static int 17422 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo) 17423 { 17424 #ifndef __lock_lint 17425 _NOTE(ARGUNUSED(sdinfo)) 17426 #endif 17427 17428 sata_pkt_t *spkt = spx->txlt_sata_pkt; 17429 sata_cmd_t *scmd; 17430 struct buf *bp; 17431 17432 /* Operation modes are up to the caller */ 17433 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 17434 17435 /* Synchronous mode, no callback - may be changed by the caller */ 17436 spkt->satapkt_comp = NULL; 17437 spkt->satapkt_time = sata_default_pkt_time; 17438 17439 scmd = &spkt->satapkt_cmd; 17440 bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t)); 17441 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 17442 17443 /* 17444 * Allocate dma_able buffer error data. 17445 * Buffer allocation will take care of buffer alignment and other DMA 17446 * attributes. 17447 */ 17448 bp = sata_alloc_local_buffer(spx, 17449 sizeof (struct sata_ncq_error_recovery_page)); 17450 if (bp == NULL) 17451 return (SATA_FAILURE); 17452 17453 bp_mapin(bp); /* make data buffer accessible */ 17454 scmd->satacmd_bp = bp; 17455 17456 /* 17457 * Set-up pointer to the buffer handle, so HBA can sync buffer 17458 * before accessing it. Handle is in usual place in translate struct. 17459 */ 17460 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle; 17461 17462 ASSERT(scmd->satacmd_num_dma_cookies != 0); 17463 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 17464 17465 return (SATA_SUCCESS); 17466 } 17467 17468 /* 17469 * sata_xlate_errors() is used to translate (S)ATA error 17470 * information to SCSI information returned in the SCSI 17471 * packet. 17472 */ 17473 static void 17474 sata_xlate_errors(sata_pkt_txlate_t *spx) 17475 { 17476 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 17477 struct scsi_extended_sense *sense; 17478 17479 scsipkt->pkt_reason = CMD_INCOMPLETE; 17480 *scsipkt->pkt_scbp = STATUS_CHECK; 17481 sense = sata_arq_sense(spx); 17482 17483 switch (spx->txlt_sata_pkt->satapkt_reason) { 17484 case SATA_PKT_PORT_ERROR: 17485 /* 17486 * We have no device data. Assume no data transfered. 17487 */ 17488 sense->es_key = KEY_HARDWARE_ERROR; 17489 break; 17490 17491 case SATA_PKT_DEV_ERROR: 17492 if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 17493 SATA_STATUS_ERR) { 17494 /* 17495 * determine dev error reason from error 17496 * reg content 17497 */ 17498 sata_decode_device_error(spx, sense); 17499 break; 17500 } 17501 /* No extended sense key - no info available */ 17502 break; 17503 17504 case SATA_PKT_TIMEOUT: 17505 scsipkt->pkt_reason = CMD_TIMEOUT; 17506 scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET; 17507 /* No extended sense key */ 17508 break; 17509 17510 case SATA_PKT_ABORTED: 17511 scsipkt->pkt_reason = CMD_ABORTED; 17512 scsipkt->pkt_statistics |= STAT_ABORTED; 17513 /* No extended sense key */ 17514 break; 17515 17516 case SATA_PKT_RESET: 17517 /* 17518 * pkt aborted either by an explicit reset request from 17519 * a host, or due to error recovery 17520 */ 17521 scsipkt->pkt_reason = CMD_RESET; 17522 scsipkt->pkt_statistics |= STAT_DEV_RESET; 17523 break; 17524 17525 default: 17526 scsipkt->pkt_reason = CMD_TRAN_ERR; 17527 break; 17528 } 17529 } 17530 17531 17532 17533 17534 /* 17535 * Log sata message 17536 * dev pathname msg line preceeds the logged message. 17537 */ 17538 17539 static void 17540 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...) 17541 { 17542 char pathname[128]; 17543 dev_info_t *dip = NULL; 17544 va_list ap; 17545 17546 mutex_enter(&sata_log_mutex); 17547 17548 va_start(ap, fmt); 17549 (void) vsprintf(sata_log_buf, fmt, ap); 17550 va_end(ap); 17551 17552 if (sata_hba_inst != NULL) { 17553 dip = SATA_DIP(sata_hba_inst); 17554 (void) ddi_pathname(dip, pathname); 17555 } else { 17556 pathname[0] = 0; 17557 } 17558 if (level == CE_CONT) { 17559 if (sata_debug_flags == 0) 17560 cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf); 17561 else 17562 cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf); 17563 } else { 17564 if (level != CE_NOTE) { 17565 cmn_err(level, "%s:\n %s", pathname, sata_log_buf); 17566 } else if (sata_msg) { 17567 cmn_err(level, "%s:\n %s", pathname, 17568 sata_log_buf); 17569 } 17570 } 17571 17572 /* sata trace debug */ 17573 sata_trace_debug(dip, sata_log_buf); 17574 17575 mutex_exit(&sata_log_mutex); 17576 } 17577 17578 17579 /* ******** Asynchronous HBA events handling & hotplugging support ******** */ 17580 17581 /* 17582 * Start or terminate the thread, depending on flag arg and current state 17583 */ 17584 static void 17585 sata_event_thread_control(int startstop) 17586 { 17587 static int sata_event_thread_terminating = 0; 17588 static int sata_event_thread_starting = 0; 17589 int i; 17590 17591 mutex_enter(&sata_event_mutex); 17592 17593 if (startstop == 0 && (sata_event_thread_starting == 1 || 17594 sata_event_thread_terminating == 1)) { 17595 mutex_exit(&sata_event_mutex); 17596 return; 17597 } 17598 if (startstop == 1 && sata_event_thread_starting == 1) { 17599 mutex_exit(&sata_event_mutex); 17600 return; 17601 } 17602 if (startstop == 1 && sata_event_thread_terminating == 1) { 17603 sata_event_thread_starting = 1; 17604 /* wait til terminate operation completes */ 17605 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 17606 while (sata_event_thread_terminating == 1) { 17607 if (i-- <= 0) { 17608 sata_event_thread_starting = 0; 17609 mutex_exit(&sata_event_mutex); 17610 #ifdef SATA_DEBUG 17611 cmn_err(CE_WARN, "sata_event_thread_control: " 17612 "timeout waiting for thread to terminate"); 17613 #endif 17614 return; 17615 } 17616 mutex_exit(&sata_event_mutex); 17617 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 17618 mutex_enter(&sata_event_mutex); 17619 } 17620 } 17621 if (startstop == 1) { 17622 if (sata_event_thread == NULL) { 17623 sata_event_thread = thread_create(NULL, 0, 17624 (void (*)())sata_event_daemon, 17625 &sata_hba_list, 0, &p0, TS_RUN, minclsyspri); 17626 } 17627 sata_event_thread_starting = 0; 17628 mutex_exit(&sata_event_mutex); 17629 return; 17630 } 17631 17632 /* 17633 * If we got here, thread may need to be terminated 17634 */ 17635 if (sata_event_thread != NULL) { 17636 int i; 17637 /* Signal event thread to go away */ 17638 sata_event_thread_terminating = 1; 17639 sata_event_thread_terminate = 1; 17640 cv_signal(&sata_event_cv); 17641 /* 17642 * Wait til daemon terminates. 17643 */ 17644 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 17645 while (sata_event_thread_terminate == 1) { 17646 mutex_exit(&sata_event_mutex); 17647 if (i-- <= 0) { 17648 /* Daemon did not go away !!! */ 17649 #ifdef SATA_DEBUG 17650 cmn_err(CE_WARN, "sata_event_thread_control: " 17651 "cannot terminate event daemon thread"); 17652 #endif 17653 mutex_enter(&sata_event_mutex); 17654 break; 17655 } 17656 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 17657 mutex_enter(&sata_event_mutex); 17658 } 17659 sata_event_thread_terminating = 0; 17660 } 17661 ASSERT(sata_event_thread_terminating == 0); 17662 ASSERT(sata_event_thread_starting == 0); 17663 mutex_exit(&sata_event_mutex); 17664 } 17665 17666 17667 /* 17668 * SATA HBA event notification function. 17669 * Events reported by SATA HBA drivers per HBA instance relate to a change in 17670 * a port and/or device state or a controller itself. 17671 * Events for different addresses/addr types cannot be combined. 17672 * A warning message is generated for each event type. 17673 * Events are not processed by this function, so only the 17674 * event flag(s)is set for an affected entity and the event thread is 17675 * waken up. Event daemon thread processes all events. 17676 * 17677 * NOTE: Since more than one event may be reported at the same time, one 17678 * cannot determine a sequence of events when opposite event are reported, eg. 17679 * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing 17680 * is taking precedence over reported events, i.e. may cause ignoring some 17681 * events. 17682 */ 17683 #define SATA_EVENT_MAX_MSG_LENGTH 79 17684 17685 void 17686 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event) 17687 { 17688 sata_hba_inst_t *sata_hba_inst = NULL; 17689 sata_address_t *saddr; 17690 sata_pmult_info_t *pmultinfo; 17691 sata_drive_info_t *sdinfo; 17692 sata_port_stats_t *pstats; 17693 sata_cport_info_t *cportinfo = NULL; 17694 sata_pmport_info_t *pmportinfo = NULL; 17695 int cport, pmport; 17696 char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1]; 17697 char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1]; 17698 char *lcp; 17699 static char *err_msg_evnt_1 = 17700 "sata_hba_event_notify: invalid port event 0x%x "; 17701 static char *err_msg_evnt_2 = 17702 "sata_hba_event_notify: invalid device event 0x%x "; 17703 int linkevent; 17704 17705 /* 17706 * There is a possibility that an event will be generated on HBA 17707 * that has not completed attachment or is detaching. We still want 17708 * to process events until HBA is detached. 17709 */ 17710 mutex_enter(&sata_mutex); 17711 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 17712 sata_hba_inst = sata_hba_inst->satahba_next) { 17713 if (SATA_DIP(sata_hba_inst) == dip) 17714 if (sata_hba_inst->satahba_attached == 1) 17715 break; 17716 } 17717 mutex_exit(&sata_mutex); 17718 if (sata_hba_inst == NULL) 17719 /* HBA not attached */ 17720 return; 17721 17722 ASSERT(sata_device != NULL); 17723 17724 /* 17725 * Validate address before - do not proceed with invalid address. 17726 */ 17727 saddr = &sata_device->satadev_addr; 17728 if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst)) 17729 return; 17730 17731 cport = saddr->cport; 17732 pmport = saddr->pmport; 17733 17734 buf1[0] = buf2[0] = '\0'; 17735 17736 /* 17737 * If event relates to port or device, check port state. 17738 * Port has to be initialized, or we cannot accept an event. 17739 */ 17740 if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT | 17741 SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT | SATA_ADDR_PMULT)) != 0) { 17742 mutex_enter(&sata_hba_inst->satahba_mutex); 17743 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 17744 mutex_exit(&sata_hba_inst->satahba_mutex); 17745 if (cportinfo == NULL || cportinfo->cport_state == 0) 17746 return; 17747 } 17748 17749 if ((saddr->qual & (SATA_ADDR_PMULT | SATA_ADDR_PMPORT | 17750 SATA_ADDR_DPMPORT)) != 0) { 17751 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 17752 SATA_LOG_D((sata_hba_inst, CE_WARN, 17753 "sata_hba_event_notify: Non-pmult device (0x%x)" 17754 "is attached to port %d, ignore pmult/pmport " 17755 "event 0x%x", cportinfo->cport_dev_type, 17756 cport, event)); 17757 return; 17758 } 17759 17760 mutex_enter(&cportinfo->cport_mutex); 17761 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 17762 mutex_exit(&cportinfo->cport_mutex); 17763 17764 /* 17765 * The daemon might be processing attachment of port 17766 * multiplier, in that case we should ignore events on its 17767 * sub-devices. 17768 * 17769 * NOTE: Only pmult_state is checked in sata_hba_event_notify. 17770 * The pmport_state is checked by sata daemon. 17771 */ 17772 if (pmultinfo == NULL || 17773 pmultinfo->pmult_state == SATA_STATE_UNKNOWN) { 17774 SATA_LOG_D((sata_hba_inst, CE_WARN, 17775 "sata_hba_event_notify: pmult is not" 17776 "available at port %d:%d, ignore event 0x%x", 17777 cport, pmport, event)); 17778 return; 17779 } 17780 } 17781 17782 if ((saddr->qual & 17783 (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) != 0) { 17784 17785 mutex_enter(&cportinfo->cport_mutex); 17786 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) { 17787 SATA_LOG_D((sata_hba_inst, CE_WARN, 17788 "sata_hba_event_notify: invalid/" 17789 "un-implemented port %d:%d (%d ports), " 17790 "ignore event 0x%x", cport, pmport, 17791 SATA_NUM_PMPORTS(sata_hba_inst, cport), event)); 17792 mutex_exit(&cportinfo->cport_mutex); 17793 return; 17794 } 17795 mutex_exit(&cportinfo->cport_mutex); 17796 17797 mutex_enter(&sata_hba_inst->satahba_mutex); 17798 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 17799 cport, pmport); 17800 mutex_exit(&sata_hba_inst->satahba_mutex); 17801 17802 /* pmport is implemented/valid? */ 17803 if (pmportinfo == NULL) { 17804 SATA_LOG_D((sata_hba_inst, CE_WARN, 17805 "sata_hba_event_notify: invalid/" 17806 "un-implemented port %d:%d, ignore " 17807 "event 0x%x", cport, pmport, event)); 17808 return; 17809 } 17810 } 17811 17812 /* 17813 * Events refer to devices, ports and controllers - each has 17814 * unique address. Events for different addresses cannot be combined. 17815 */ 17816 if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) { 17817 17818 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17819 17820 /* qualify this event(s) */ 17821 if ((event & SATA_EVNT_PORT_EVENTS) == 0) { 17822 /* Invalid event for the device port */ 17823 (void) sprintf(buf2, err_msg_evnt_1, 17824 event & SATA_EVNT_PORT_EVENTS); 17825 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17826 goto event_info; 17827 } 17828 if (saddr->qual == SATA_ADDR_CPORT) { 17829 /* Controller's device port event */ 17830 17831 (SATA_CPORT_INFO(sata_hba_inst, cport))-> 17832 cport_event_flags |= 17833 event & SATA_EVNT_PORT_EVENTS; 17834 pstats = 17835 &(SATA_CPORT_INFO(sata_hba_inst, cport))-> 17836 cport_stats; 17837 } else { 17838 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17839 mutex_enter(&pmportinfo->pmport_mutex); 17840 /* Port multiplier's device port event */ 17841 (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 17842 pmport_event_flags |= 17843 event & SATA_EVNT_PORT_EVENTS; 17844 pstats = 17845 &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 17846 pmport_stats; 17847 mutex_exit(&pmportinfo->pmport_mutex); 17848 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17849 } 17850 17851 /* 17852 * Add to statistics and log the message. We have to do it 17853 * here rather than in the event daemon, because there may be 17854 * multiple events occuring before they are processed. 17855 */ 17856 linkevent = event & 17857 (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED); 17858 if (linkevent) { 17859 if (linkevent == (SATA_EVNT_LINK_LOST | 17860 SATA_EVNT_LINK_ESTABLISHED)) { 17861 /* This is likely event combination */ 17862 (void) strlcat(buf1, "link lost/established, ", 17863 SATA_EVENT_MAX_MSG_LENGTH); 17864 17865 if (pstats->link_lost < 0xffffffffffffffffULL) 17866 pstats->link_lost++; 17867 if (pstats->link_established < 17868 0xffffffffffffffffULL) 17869 pstats->link_established++; 17870 linkevent = 0; 17871 } else if (linkevent & SATA_EVNT_LINK_LOST) { 17872 (void) strlcat(buf1, "link lost, ", 17873 SATA_EVENT_MAX_MSG_LENGTH); 17874 17875 if (pstats->link_lost < 0xffffffffffffffffULL) 17876 pstats->link_lost++; 17877 } else { 17878 (void) strlcat(buf1, "link established, ", 17879 SATA_EVENT_MAX_MSG_LENGTH); 17880 if (pstats->link_established < 17881 0xffffffffffffffffULL) 17882 pstats->link_established++; 17883 } 17884 } 17885 if (event & SATA_EVNT_DEVICE_ATTACHED) { 17886 (void) strlcat(buf1, "device attached, ", 17887 SATA_EVENT_MAX_MSG_LENGTH); 17888 if (pstats->device_attached < 0xffffffffffffffffULL) 17889 pstats->device_attached++; 17890 } 17891 if (event & SATA_EVNT_DEVICE_DETACHED) { 17892 (void) strlcat(buf1, "device detached, ", 17893 SATA_EVENT_MAX_MSG_LENGTH); 17894 if (pstats->device_detached < 0xffffffffffffffffULL) 17895 pstats->device_detached++; 17896 } 17897 if (event & SATA_EVNT_PWR_LEVEL_CHANGED) { 17898 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 17899 "port %d power level changed", cport); 17900 if (pstats->port_pwr_changed < 0xffffffffffffffffULL) 17901 pstats->port_pwr_changed++; 17902 } 17903 17904 if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) { 17905 /* There should be no other events for this address */ 17906 (void) sprintf(buf2, err_msg_evnt_1, 17907 event & ~SATA_EVNT_PORT_EVENTS); 17908 } 17909 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17910 17911 } else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) { 17912 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17913 17914 /* qualify this event */ 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_DEVICE_RESET); 17919 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17920 goto event_info; 17921 } 17922 /* drive event */ 17923 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 17924 if (sdinfo != NULL) { 17925 if (event & SATA_EVNT_DEVICE_RESET) { 17926 (void) strlcat(buf1, "device reset, ", 17927 SATA_EVENT_MAX_MSG_LENGTH); 17928 if (sdinfo->satadrv_stats.drive_reset < 17929 0xffffffffffffffffULL) 17930 sdinfo->satadrv_stats.drive_reset++; 17931 sdinfo->satadrv_event_flags |= 17932 SATA_EVNT_DEVICE_RESET; 17933 } 17934 } 17935 if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) { 17936 /* Invalid event for a device */ 17937 (void) sprintf(buf2, err_msg_evnt_2, 17938 event & ~SATA_EVNT_DRIVE_EVENTS); 17939 } 17940 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17941 } else if (saddr->qual == SATA_ADDR_PMULT) { 17942 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17943 17944 /* qualify this event */ 17945 if ((event & (SATA_EVNT_DEVICE_RESET | 17946 SATA_EVNT_PMULT_LINK_CHANGED)) == 0) { 17947 /* Invalid event for a port multiplier */ 17948 (void) sprintf(buf2, err_msg_evnt_2, 17949 event & SATA_EVNT_DEVICE_RESET); 17950 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17951 goto event_info; 17952 } 17953 17954 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 17955 17956 if (event & SATA_EVNT_DEVICE_RESET) { 17957 17958 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 17959 "[Reset] port-mult on cport %d", cport); 17960 pmultinfo->pmult_event_flags |= 17961 SATA_EVNT_DEVICE_RESET; 17962 (void) strlcat(buf1, "pmult reset, ", 17963 SATA_EVENT_MAX_MSG_LENGTH); 17964 } 17965 17966 if (event & SATA_EVNT_PMULT_LINK_CHANGED) { 17967 17968 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 17969 "pmult link changed on cport %d", cport); 17970 pmultinfo->pmult_event_flags |= 17971 SATA_EVNT_PMULT_LINK_CHANGED; 17972 (void) strlcat(buf1, "pmult link changed, ", 17973 SATA_EVENT_MAX_MSG_LENGTH); 17974 } 17975 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 17976 17977 } else { 17978 if (saddr->qual != SATA_ADDR_NULL) { 17979 /* Wrong address qualifier */ 17980 SATA_LOG_D((sata_hba_inst, CE_WARN, 17981 "sata_hba_event_notify: invalid address 0x%x", 17982 *(uint32_t *)saddr)); 17983 return; 17984 } 17985 if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 || 17986 (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) { 17987 /* Invalid event for the controller */ 17988 SATA_LOG_D((sata_hba_inst, CE_WARN, 17989 "sata_hba_event_notify: invalid event 0x%x for " 17990 "controller", 17991 event & SATA_EVNT_CONTROLLER_EVENTS)); 17992 return; 17993 } 17994 buf1[0] = '\0'; 17995 /* This may be a frequent and not interesting event */ 17996 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 17997 "controller power level changed\n", NULL); 17998 17999 mutex_enter(&sata_hba_inst->satahba_mutex); 18000 if (sata_hba_inst->satahba_stats.ctrl_pwr_change < 18001 0xffffffffffffffffULL) 18002 sata_hba_inst->satahba_stats.ctrl_pwr_change++; 18003 18004 sata_hba_inst->satahba_event_flags |= 18005 SATA_EVNT_PWR_LEVEL_CHANGED; 18006 mutex_exit(&sata_hba_inst->satahba_mutex); 18007 } 18008 /* 18009 * If we got here, there is something to do with this HBA 18010 * instance. 18011 */ 18012 mutex_enter(&sata_hba_inst->satahba_mutex); 18013 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 18014 mutex_exit(&sata_hba_inst->satahba_mutex); 18015 mutex_enter(&sata_mutex); 18016 sata_event_pending |= SATA_EVNT_MAIN; /* global event indicator */ 18017 mutex_exit(&sata_mutex); 18018 18019 /* Tickle event thread */ 18020 mutex_enter(&sata_event_mutex); 18021 if (sata_event_thread_active == 0) 18022 cv_signal(&sata_event_cv); 18023 mutex_exit(&sata_event_mutex); 18024 18025 event_info: 18026 if (buf1[0] != '\0') { 18027 lcp = strrchr(buf1, ','); 18028 if (lcp != NULL) 18029 *lcp = '\0'; 18030 } 18031 if (saddr->qual == SATA_ADDR_CPORT || 18032 saddr->qual == SATA_ADDR_DCPORT) { 18033 if (buf1[0] != '\0') { 18034 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 18035 cport, buf1); 18036 } 18037 if (buf2[0] != '\0') { 18038 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 18039 cport, buf2); 18040 } 18041 } else if (saddr->qual == SATA_ADDR_PMPORT || 18042 saddr->qual == SATA_ADDR_DPMPORT) { 18043 if (buf1[0] != '\0') { 18044 sata_log(sata_hba_inst, CE_NOTE, 18045 "port %d pmport %d: %s\n", cport, pmport, buf1); 18046 } 18047 if (buf2[0] != '\0') { 18048 sata_log(sata_hba_inst, CE_NOTE, 18049 "port %d pmport %d: %s\n", cport, pmport, buf2); 18050 } 18051 } 18052 } 18053 18054 18055 /* 18056 * Event processing thread. 18057 * Arg is a pointer to the sata_hba_list pointer. 18058 * It is not really needed, because sata_hba_list is global and static 18059 */ 18060 static void 18061 sata_event_daemon(void *arg) 18062 { 18063 #ifndef __lock_lint 18064 _NOTE(ARGUNUSED(arg)) 18065 #endif 18066 sata_hba_inst_t *sata_hba_inst; 18067 clock_t delta; 18068 18069 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 18070 "SATA event daemon started\n", NULL); 18071 loop: 18072 /* 18073 * Process events here. Walk through all registered HBAs 18074 */ 18075 mutex_enter(&sata_mutex); 18076 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 18077 sata_hba_inst = sata_hba_inst->satahba_next) { 18078 ASSERT(sata_hba_inst != NULL); 18079 mutex_enter(&sata_hba_inst->satahba_mutex); 18080 if (sata_hba_inst->satahba_attached == 0 || 18081 (sata_hba_inst->satahba_event_flags & 18082 SATA_EVNT_SKIP) != 0) { 18083 mutex_exit(&sata_hba_inst->satahba_mutex); 18084 continue; 18085 } 18086 if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) { 18087 sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP; 18088 mutex_exit(&sata_hba_inst->satahba_mutex); 18089 mutex_exit(&sata_mutex); 18090 /* Got the controller with pending event */ 18091 sata_process_controller_events(sata_hba_inst); 18092 /* 18093 * Since global mutex was released, there is a 18094 * possibility that HBA list has changed, so start 18095 * over from the top. Just processed controller 18096 * will be passed-over because of the SKIP flag. 18097 */ 18098 goto loop; 18099 } 18100 mutex_exit(&sata_hba_inst->satahba_mutex); 18101 } 18102 /* Clear SKIP flag in all controllers */ 18103 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 18104 sata_hba_inst = sata_hba_inst->satahba_next) { 18105 mutex_enter(&sata_hba_inst->satahba_mutex); 18106 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP; 18107 mutex_exit(&sata_hba_inst->satahba_mutex); 18108 } 18109 mutex_exit(&sata_mutex); 18110 18111 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 18112 "SATA EVENT DAEMON suspending itself", NULL); 18113 18114 #ifdef SATA_DEBUG 18115 if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) { 18116 sata_log(sata_hba_inst, CE_WARN, 18117 "SATA EVENTS PROCESSING DISABLED\n"); 18118 thread_exit(); /* Daemon will not run again */ 18119 } 18120 #endif 18121 mutex_enter(&sata_event_mutex); 18122 sata_event_thread_active = 0; 18123 mutex_exit(&sata_event_mutex); 18124 /* 18125 * Go to sleep/suspend itself and wake up either because new event or 18126 * wait timeout. Exit if there is a termination request (driver 18127 * unload). 18128 */ 18129 delta = drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME); 18130 do { 18131 mutex_enter(&sata_event_mutex); 18132 (void) cv_reltimedwait(&sata_event_cv, &sata_event_mutex, 18133 delta, TR_CLOCK_TICK); 18134 18135 if (sata_event_thread_active != 0) { 18136 mutex_exit(&sata_event_mutex); 18137 continue; 18138 } 18139 18140 /* Check if it is time to go away */ 18141 if (sata_event_thread_terminate == 1) { 18142 /* 18143 * It is up to the thread setting above flag to make 18144 * sure that this thread is not killed prematurely. 18145 */ 18146 sata_event_thread_terminate = 0; 18147 sata_event_thread = NULL; 18148 mutex_exit(&sata_event_mutex); 18149 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 18150 "SATA_EVENT_DAEMON_TERMINATING", NULL); 18151 thread_exit(); { _NOTE(NOT_REACHED) } 18152 } 18153 mutex_exit(&sata_event_mutex); 18154 } while (!(sata_event_pending & SATA_EVNT_MAIN)); 18155 18156 mutex_enter(&sata_event_mutex); 18157 sata_event_thread_active = 1; 18158 mutex_exit(&sata_event_mutex); 18159 18160 mutex_enter(&sata_mutex); 18161 sata_event_pending &= ~SATA_EVNT_MAIN; 18162 mutex_exit(&sata_mutex); 18163 18164 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 18165 "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL); 18166 18167 goto loop; 18168 } 18169 18170 /* 18171 * Specific HBA instance event processing. 18172 * 18173 * NOTE: At the moment, device event processing is limited to hard disks 18174 * only. 18175 * Port multiplier is supported now. 18176 */ 18177 static void 18178 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst) 18179 { 18180 int ncport; 18181 uint32_t event_flags; 18182 sata_address_t *saddr; 18183 sata_cport_info_t *cportinfo; 18184 sata_pmult_info_t *pmultinfo; 18185 18186 SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst, 18187 "Processing controller %d event(s)", 18188 ddi_get_instance(SATA_DIP(sata_hba_inst))); 18189 18190 mutex_enter(&sata_hba_inst->satahba_mutex); 18191 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN; 18192 event_flags = sata_hba_inst->satahba_event_flags; 18193 mutex_exit(&sata_hba_inst->satahba_mutex); 18194 /* 18195 * Process controller power change first 18196 * HERE 18197 */ 18198 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) 18199 sata_process_cntrl_pwr_level_change(sata_hba_inst); 18200 18201 /* 18202 * Search through ports/devices to identify affected port/device. 18203 * We may have to process events for more than one port/device. 18204 */ 18205 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 18206 /* 18207 * Not all ports may be processed in attach by the time we 18208 * get an event. Check if port info is initialized. 18209 */ 18210 mutex_enter(&sata_hba_inst->satahba_mutex); 18211 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 18212 mutex_exit(&sata_hba_inst->satahba_mutex); 18213 if (cportinfo == NULL || cportinfo->cport_state == 0) 18214 continue; 18215 18216 /* We have initialized controller port info */ 18217 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 18218 event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 18219 cport_event_flags; 18220 /* Check if port was locked by IOCTL processing */ 18221 if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) { 18222 /* 18223 * We ignore port events because port is busy 18224 * with AP control processing. Set again 18225 * controller and main event flag, so that 18226 * events may be processed by the next daemon 18227 * run. 18228 */ 18229 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 18230 mutex_enter(&sata_hba_inst->satahba_mutex); 18231 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 18232 mutex_exit(&sata_hba_inst->satahba_mutex); 18233 mutex_enter(&sata_mutex); 18234 sata_event_pending |= SATA_EVNT_MAIN; 18235 mutex_exit(&sata_mutex); 18236 SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst, 18237 "Event processing postponed until " 18238 "AP control processing completes", 18239 NULL); 18240 /* Check other ports */ 18241 continue; 18242 } else { 18243 /* 18244 * Set BSY flag so that AP control would not 18245 * interfere with events processing for 18246 * this port. 18247 */ 18248 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 18249 cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY; 18250 } 18251 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 18252 18253 saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr; 18254 18255 if ((event_flags & 18256 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) { 18257 /* 18258 * Got port event. 18259 * We need some hierarchy of event processing as they 18260 * are affecting each other: 18261 * 1. port failed 18262 * 2. device detached/attached 18263 * 3. link events - link events may trigger device 18264 * detached or device attached events in some 18265 * circumstances. 18266 * 4. port power level changed 18267 */ 18268 if (event_flags & SATA_EVNT_PORT_FAILED) { 18269 sata_process_port_failed_event(sata_hba_inst, 18270 saddr); 18271 } 18272 if (event_flags & SATA_EVNT_DEVICE_DETACHED) { 18273 sata_process_device_detached(sata_hba_inst, 18274 saddr); 18275 } 18276 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) { 18277 sata_process_device_attached(sata_hba_inst, 18278 saddr); 18279 } 18280 if (event_flags & 18281 (SATA_EVNT_LINK_ESTABLISHED | 18282 SATA_EVNT_LINK_LOST)) { 18283 sata_process_port_link_events(sata_hba_inst, 18284 saddr); 18285 } 18286 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) { 18287 sata_process_port_pwr_change(sata_hba_inst, 18288 saddr); 18289 } 18290 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) { 18291 sata_process_target_node_cleanup( 18292 sata_hba_inst, saddr); 18293 } 18294 if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) { 18295 sata_process_device_autoonline( 18296 sata_hba_inst, saddr); 18297 } 18298 } 18299 18300 18301 /* 18302 * Scan port multiplier and all its sub-ports event flags. 18303 * The events are marked by 18304 * (1) sata_pmult_info.pmult_event_flags 18305 * (2) sata_pmport_info.pmport_event_flags 18306 */ 18307 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 18308 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 18309 /* 18310 * There should be another extra check: this 18311 * port multiplier still exists? 18312 */ 18313 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, 18314 ncport); 18315 18316 if (pmultinfo != NULL) { 18317 mutex_exit(&(SATA_CPORT_MUTEX( 18318 sata_hba_inst, ncport))); 18319 sata_process_pmult_events( 18320 sata_hba_inst, ncport); 18321 mutex_enter(&(SATA_CPORT_MUTEX( 18322 sata_hba_inst, ncport))); 18323 } else { 18324 SATADBG1(SATA_DBG_PMULT, sata_hba_inst, 18325 "Port-multiplier is gone. " 18326 "Ignore all sub-device events " 18327 "at port %d.", ncport); 18328 } 18329 } 18330 18331 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) != 18332 SATA_DTYPE_NONE) && 18333 (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) { 18334 if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)-> 18335 satadrv_event_flags & 18336 (SATA_EVNT_DEVICE_RESET | 18337 SATA_EVNT_INPROC_DEVICE_RESET)) { 18338 /* Have device event */ 18339 sata_process_device_reset(sata_hba_inst, 18340 saddr); 18341 } 18342 } 18343 /* Release PORT_BUSY flag */ 18344 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 18345 cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY; 18346 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 18347 18348 } /* End of loop through the controller SATA ports */ 18349 } 18350 18351 /* 18352 * Specific port multiplier instance event processing. At the moment, device 18353 * event processing is limited to link/attach event only. 18354 * 18355 * NOTE: power management event is not supported yet. 18356 */ 18357 static void 18358 sata_process_pmult_events(sata_hba_inst_t *sata_hba_inst, uint8_t cport) 18359 { 18360 sata_cport_info_t *cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 18361 sata_pmult_info_t *pmultinfo; 18362 sata_pmport_info_t *pmportinfo; 18363 sata_address_t *saddr; 18364 sata_device_t sata_device; 18365 uint32_t event_flags; 18366 int npmport; 18367 int rval; 18368 18369 SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst, 18370 "Processing pmult event(s) on cport %d of controller %d", 18371 cport, ddi_get_instance(SATA_DIP(sata_hba_inst))); 18372 18373 /* First process events on port multiplier */ 18374 mutex_enter(&cportinfo->cport_mutex); 18375 pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport); 18376 event_flags = pmultinfo->pmult_event_flags; 18377 18378 /* 18379 * Reset event (of port multiplier) has higher priority because the 18380 * port multiplier itself might be failed or removed after reset. 18381 */ 18382 if (event_flags & SATA_EVNT_DEVICE_RESET) { 18383 /* 18384 * The status of the sub-links are uncertain, 18385 * so mark all sub-ports as RESET 18386 */ 18387 for (npmport = 0; npmport < SATA_NUM_PMPORTS( 18388 sata_hba_inst, cport); npmport ++) { 18389 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 18390 cport, npmport); 18391 if (pmportinfo == NULL) { 18392 /* That's weird. */ 18393 SATA_LOG_D((sata_hba_inst, CE_WARN, 18394 "sata_hba_event_notify: " 18395 "invalid/un-implemented " 18396 "port %d:%d (%d ports), ", 18397 cport, npmport, SATA_NUM_PMPORTS( 18398 sata_hba_inst, cport))); 18399 continue; 18400 } 18401 18402 mutex_enter(&pmportinfo->pmport_mutex); 18403 18404 /* Mark all pmport to unknow state. */ 18405 pmportinfo->pmport_state = SATA_STATE_UNKNOWN; 18406 /* Mark all pmports with link events. */ 18407 pmportinfo->pmport_event_flags = 18408 (SATA_EVNT_LINK_ESTABLISHED|SATA_EVNT_LINK_LOST); 18409 mutex_exit(&pmportinfo->pmport_mutex); 18410 } 18411 18412 } else if (event_flags & SATA_EVNT_PMULT_LINK_CHANGED) { 18413 /* 18414 * We need probe the port multiplier to know what has 18415 * happened. 18416 */ 18417 bzero(&sata_device, sizeof (sata_device_t)); 18418 sata_device.satadev_rev = SATA_DEVICE_REV; 18419 sata_device.satadev_addr.cport = cport; 18420 sata_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT; 18421 sata_device.satadev_addr.qual = SATA_ADDR_PMULT; 18422 18423 mutex_exit(&cportinfo->cport_mutex); 18424 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18425 (SATA_DIP(sata_hba_inst), &sata_device); 18426 mutex_enter(&cportinfo->cport_mutex); 18427 if (rval != SATA_SUCCESS) { 18428 /* Something went wrong? Fail the port */ 18429 cportinfo->cport_state = SATA_PSTATE_FAILED; 18430 mutex_exit(&cportinfo->cport_mutex); 18431 SATA_LOG_D((sata_hba_inst, CE_WARN, 18432 "SATA port %d probing failed", cport)); 18433 18434 /* PMult structure must be released. */ 18435 sata_free_pmult(sata_hba_inst, &sata_device); 18436 return; 18437 } 18438 18439 sata_update_port_info(sata_hba_inst, &sata_device); 18440 18441 /* 18442 * Sanity check - Port is active? Is the link active? 18443 * The device is still a port multiplier? 18444 */ 18445 if ((cportinfo->cport_state & 18446 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 18447 ((cportinfo->cport_scr.sstatus & 18448 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) || 18449 (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) { 18450 mutex_exit(&cportinfo->cport_mutex); 18451 18452 /* PMult structure must be released. */ 18453 sata_free_pmult(sata_hba_inst, &sata_device); 18454 return; 18455 } 18456 18457 /* Probed succeed, set port ready. */ 18458 cportinfo->cport_state |= 18459 SATA_STATE_PROBED | SATA_STATE_READY; 18460 } 18461 18462 /* Release port multiplier event flags. */ 18463 pmultinfo->pmult_event_flags &= 18464 ~(SATA_EVNT_DEVICE_RESET|SATA_EVNT_PMULT_LINK_CHANGED); 18465 mutex_exit(&cportinfo->cport_mutex); 18466 18467 /* 18468 * Check all sub-links. 18469 */ 18470 for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, cport); 18471 npmport ++) { 18472 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport); 18473 mutex_enter(&pmportinfo->pmport_mutex); 18474 event_flags = pmportinfo->pmport_event_flags; 18475 mutex_exit(&pmportinfo->pmport_mutex); 18476 saddr = &pmportinfo->pmport_addr; 18477 18478 if ((event_flags & 18479 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) { 18480 /* 18481 * Got port multiplier port event. 18482 * We need some hierarchy of event processing as they 18483 * are affecting each other: 18484 * 1. device detached/attached 18485 * 2. link events - link events may trigger device 18486 * detached or device attached events in some 18487 * circumstances. 18488 */ 18489 if (event_flags & SATA_EVNT_DEVICE_DETACHED) { 18490 sata_process_pmdevice_detached(sata_hba_inst, 18491 saddr); 18492 } 18493 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) { 18494 sata_process_pmdevice_attached(sata_hba_inst, 18495 saddr); 18496 } 18497 if (event_flags & SATA_EVNT_LINK_ESTABLISHED || 18498 event_flags & SATA_EVNT_LINK_LOST) { 18499 sata_process_pmport_link_events(sata_hba_inst, 18500 saddr); 18501 } 18502 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) { 18503 sata_process_target_node_cleanup( 18504 sata_hba_inst, saddr); 18505 } 18506 } 18507 18508 /* Checking drive event(s). */ 18509 mutex_enter(&pmportinfo->pmport_mutex); 18510 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE && 18511 pmportinfo->pmport_sata_drive != NULL) { 18512 event_flags = pmportinfo->pmport_sata_drive-> 18513 satadrv_event_flags; 18514 if (event_flags & (SATA_EVNT_DEVICE_RESET | 18515 SATA_EVNT_INPROC_DEVICE_RESET)) { 18516 18517 /* Have device event */ 18518 sata_process_pmdevice_reset(sata_hba_inst, 18519 saddr); 18520 } 18521 } 18522 mutex_exit(&pmportinfo->pmport_mutex); 18523 18524 /* Release PORT_BUSY flag */ 18525 mutex_enter(&cportinfo->cport_mutex); 18526 cportinfo->cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY; 18527 mutex_exit(&cportinfo->cport_mutex); 18528 } 18529 18530 SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst, 18531 "[DONE] pmult event(s) on cport %d of controller %d", 18532 cport, ddi_get_instance(SATA_DIP(sata_hba_inst))); 18533 } 18534 18535 /* 18536 * Process HBA power level change reported by HBA driver. 18537 * Not implemented at this time - event is ignored. 18538 */ 18539 static void 18540 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst) 18541 { 18542 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18543 "Processing controller power level change", NULL); 18544 18545 /* Ignoring it for now */ 18546 mutex_enter(&sata_hba_inst->satahba_mutex); 18547 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 18548 mutex_exit(&sata_hba_inst->satahba_mutex); 18549 } 18550 18551 /* 18552 * Process port power level change reported by HBA driver. 18553 * Not implemented at this time - event is ignored. 18554 */ 18555 static void 18556 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst, 18557 sata_address_t *saddr) 18558 { 18559 sata_cport_info_t *cportinfo; 18560 18561 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18562 "Processing port power level change", NULL); 18563 18564 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 18565 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18566 /* Reset event flag */ 18567 cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 18568 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18569 } 18570 18571 /* 18572 * Process port failure reported by HBA driver. 18573 * cports support only - no pmports. 18574 */ 18575 static void 18576 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst, 18577 sata_address_t *saddr) 18578 { 18579 sata_cport_info_t *cportinfo; 18580 18581 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 18582 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18583 /* Reset event flag first */ 18584 cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED; 18585 /* If the port is in SHUTDOWN or FAILED state, ignore this event. */ 18586 if ((cportinfo->cport_state & 18587 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) { 18588 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18589 cport_mutex); 18590 return; 18591 } 18592 /* Fail the port */ 18593 cportinfo->cport_state = SATA_PSTATE_FAILED; 18594 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18595 sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport); 18596 } 18597 18598 /* 18599 * Device Reset Event processing. 18600 * The sequence is managed by 3 stage flags: 18601 * - reset event reported, 18602 * - reset event being processed, 18603 * - request to clear device reset state. 18604 * 18605 * NOTE: This function has to be entered with cport mutex held. It exits with 18606 * mutex held as well, but can release mutex during the processing. 18607 */ 18608 static void 18609 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst, 18610 sata_address_t *saddr) 18611 { 18612 sata_drive_info_t old_sdinfo; /* local copy of the drive info */ 18613 sata_drive_info_t *sdinfo; 18614 sata_cport_info_t *cportinfo; 18615 sata_device_t sata_device; 18616 int rval_probe, rval_set; 18617 18618 /* We only care about host sata cport for now */ 18619 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 18620 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 18621 /* 18622 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED 18623 * state, ignore reset event. 18624 */ 18625 if (((cportinfo->cport_state & 18626 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) || 18627 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 18628 sdinfo->satadrv_event_flags &= 18629 ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET); 18630 return; 18631 } 18632 18633 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) == 18634 SATA_DTYPE_PMULT)) { 18635 /* 18636 * Should not happened: this is already handled in 18637 * sata_hba_event_notify() 18638 */ 18639 mutex_exit(&cportinfo->cport_mutex); 18640 goto done; 18641 } 18642 18643 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) & 18644 SATA_VALID_DEV_TYPE) == 0) { 18645 /* 18646 * This should not happen - coding error. 18647 * But we can recover, so do not panic, just clean up 18648 * and if in debug mode, log the message. 18649 */ 18650 #ifdef SATA_DEBUG 18651 sata_log(sata_hba_inst, CE_WARN, 18652 "sata_process_device_reset: " 18653 "Invalid device type with sdinfo!", NULL); 18654 #endif 18655 sdinfo->satadrv_event_flags = 0; 18656 return; 18657 } 18658 18659 #ifdef SATA_DEBUG 18660 if ((sdinfo->satadrv_event_flags & 18661 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) { 18662 /* Nothing to do */ 18663 /* Something is weird - why we are processing dev reset? */ 18664 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18665 "No device reset event!!!!", NULL); 18666 18667 return; 18668 } 18669 if ((sdinfo->satadrv_event_flags & 18670 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 18671 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 18672 /* Something is weird - new device reset event */ 18673 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18674 "Overlapping device reset events!", NULL); 18675 } 18676 #endif 18677 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18678 "Processing port %d device reset", saddr->cport); 18679 18680 /* Clear event flag */ 18681 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET; 18682 18683 /* It seems that we always need to check the port state first */ 18684 sata_device.satadev_rev = SATA_DEVICE_REV; 18685 sata_device.satadev_addr = *saddr; 18686 /* 18687 * We have to exit mutex, because the HBA probe port function may 18688 * block on its own mutex. 18689 */ 18690 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18691 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18692 (SATA_DIP(sata_hba_inst), &sata_device); 18693 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18694 sata_update_port_info(sata_hba_inst, &sata_device); 18695 if (rval_probe != SATA_SUCCESS) { 18696 /* Something went wrong? Fail the port */ 18697 cportinfo->cport_state = SATA_PSTATE_FAILED; 18698 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 18699 if (sdinfo != NULL) 18700 sdinfo->satadrv_event_flags = 0; 18701 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18702 cport_mutex); 18703 SATA_LOG_D((sata_hba_inst, CE_WARN, 18704 "SATA port %d probing failed", 18705 saddr->cport)); 18706 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 18707 saddr->cport)->cport_mutex); 18708 return; 18709 } 18710 if ((sata_device.satadev_scr.sstatus & 18711 SATA_PORT_DEVLINK_UP_MASK) != 18712 SATA_PORT_DEVLINK_UP || 18713 sata_device.satadev_type == SATA_DTYPE_NONE) { 18714 /* 18715 * No device to process, anymore. Some other event processing 18716 * would or have already performed port info cleanup. 18717 * To be safe (HBA may need it), request clearing device 18718 * reset condition. 18719 */ 18720 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 18721 if (sdinfo != NULL) { 18722 sdinfo->satadrv_event_flags &= 18723 ~SATA_EVNT_INPROC_DEVICE_RESET; 18724 sdinfo->satadrv_event_flags |= 18725 SATA_EVNT_CLEAR_DEVICE_RESET; 18726 } 18727 return; 18728 } 18729 18730 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 18731 if (sdinfo == NULL) { 18732 return; 18733 } 18734 if ((sdinfo->satadrv_event_flags & 18735 SATA_EVNT_INPROC_DEVICE_RESET) == 0) { 18736 /* 18737 * Start tracking time for device feature restoration and 18738 * identification. Save current time (lbolt value). 18739 */ 18740 sdinfo->satadrv_reset_time = ddi_get_lbolt(); 18741 } 18742 /* Mark device reset processing as active */ 18743 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET; 18744 18745 old_sdinfo = *sdinfo; /* local copy of the drive info */ 18746 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18747 18748 rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1); 18749 18750 if (rval_set != SATA_SUCCESS) { 18751 /* 18752 * Restoring drive setting failed. 18753 * Probe the port first, to check if the port state has changed 18754 */ 18755 sata_device.satadev_rev = SATA_DEVICE_REV; 18756 sata_device.satadev_addr = *saddr; 18757 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 18758 /* probe port */ 18759 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18760 (SATA_DIP(sata_hba_inst), &sata_device); 18761 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 18762 cport_mutex); 18763 if (rval_probe == SATA_SUCCESS && 18764 (sata_device.satadev_state & 18765 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 && 18766 (sata_device.satadev_scr.sstatus & 18767 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP && 18768 sata_device.satadev_type != SATA_DTYPE_NONE) { 18769 /* 18770 * We may retry this a bit later - in-process reset 18771 * condition should be already set. 18772 * Track retry time for device identification. 18773 */ 18774 if ((cportinfo->cport_dev_type & 18775 SATA_VALID_DEV_TYPE) != 0 && 18776 SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL && 18777 sdinfo->satadrv_reset_time != 0) { 18778 clock_t cur_time = ddi_get_lbolt(); 18779 /* 18780 * If the retry time limit was not 18781 * exceeded, retry. 18782 */ 18783 if ((cur_time - sdinfo->satadrv_reset_time) < 18784 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 18785 mutex_enter( 18786 &sata_hba_inst->satahba_mutex); 18787 sata_hba_inst->satahba_event_flags |= 18788 SATA_EVNT_MAIN; 18789 mutex_exit( 18790 &sata_hba_inst->satahba_mutex); 18791 mutex_enter(&sata_mutex); 18792 sata_event_pending |= SATA_EVNT_MAIN; 18793 mutex_exit(&sata_mutex); 18794 return; 18795 } 18796 if (rval_set == SATA_RETRY) { 18797 /* 18798 * Setting drive features failed, but 18799 * the drive is still accessible, 18800 * so emit a warning message before 18801 * return. 18802 */ 18803 mutex_exit(&SATA_CPORT_INFO( 18804 sata_hba_inst, 18805 saddr->cport)->cport_mutex); 18806 goto done; 18807 } 18808 } 18809 /* Fail the drive */ 18810 sdinfo->satadrv_state = SATA_DSTATE_FAILED; 18811 18812 sata_log(sata_hba_inst, CE_WARN, 18813 "SATA device at port %d - device failed", 18814 saddr->cport); 18815 18816 DTRACE_PROBE(port_failed_f); 18817 } 18818 /* 18819 * No point of retrying - device failed or some other event 18820 * processing or already did or will do port info cleanup. 18821 * To be safe (HBA may need it), 18822 * request clearing device reset condition. 18823 */ 18824 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 18825 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET; 18826 sdinfo->satadrv_reset_time = 0; 18827 return; 18828 } 18829 done: 18830 /* 18831 * If setting of drive features failed, but the drive is still 18832 * accessible, emit a warning message. 18833 */ 18834 if (rval_set == SATA_RETRY) { 18835 sata_log(sata_hba_inst, CE_WARN, 18836 "SATA device at port %d - desired setting could not be " 18837 "restored after reset. Device may not operate as expected.", 18838 saddr->cport); 18839 } 18840 /* 18841 * Raise the flag indicating that the next sata command could 18842 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device 18843 * reset is reported. 18844 */ 18845 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 18846 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 18847 sdinfo->satadrv_reset_time = 0; 18848 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) { 18849 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 18850 sdinfo->satadrv_event_flags &= 18851 ~SATA_EVNT_INPROC_DEVICE_RESET; 18852 sdinfo->satadrv_event_flags |= 18853 SATA_EVNT_CLEAR_DEVICE_RESET; 18854 } 18855 } 18856 } 18857 18858 18859 /* 18860 * Port Multiplier Port Device Reset Event processing. 18861 * 18862 * NOTE: This function has to be entered with pmport mutex held. It exits with 18863 * mutex held as well, but can release mutex during the processing. 18864 */ 18865 static void 18866 sata_process_pmdevice_reset(sata_hba_inst_t *sata_hba_inst, 18867 sata_address_t *saddr) 18868 { 18869 sata_drive_info_t old_sdinfo; /* local copy of the drive info */ 18870 sata_drive_info_t *sdinfo = NULL; 18871 sata_cport_info_t *cportinfo = NULL; 18872 sata_pmport_info_t *pmportinfo = NULL; 18873 sata_pmult_info_t *pminfo = NULL; 18874 sata_device_t sata_device; 18875 uint8_t cport = saddr->cport; 18876 uint8_t pmport = saddr->pmport; 18877 int rval; 18878 18879 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18880 "Processing drive reset at port %d:%d", cport, pmport); 18881 18882 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 18883 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 18884 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport); 18885 18886 /* 18887 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED 18888 * state, ignore reset event. 18889 */ 18890 if (((cportinfo->cport_state & 18891 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) || 18892 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 18893 sdinfo->satadrv_event_flags &= 18894 ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET); 18895 return; 18896 } 18897 18898 if ((pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) == 0) { 18899 /* 18900 * This should not happen - coding error. 18901 * But we can recover, so do not panic, just clean up 18902 * and if in debug mode, log the message. 18903 */ 18904 #ifdef SATA_DEBUG 18905 sata_log(sata_hba_inst, CE_WARN, 18906 "sata_process_pmdevice_reset: " 18907 "Invalid device type with sdinfo!", NULL); 18908 #endif 18909 sdinfo->satadrv_event_flags = 0; 18910 return; 18911 } 18912 18913 #ifdef SATA_DEBUG 18914 if ((sdinfo->satadrv_event_flags & 18915 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) { 18916 /* Nothing to do */ 18917 /* Something is weird - why we are processing dev reset? */ 18918 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18919 "No device reset event!!!!", NULL); 18920 18921 return; 18922 } 18923 if ((sdinfo->satadrv_event_flags & 18924 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 18925 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 18926 /* Something is weird - new device reset event */ 18927 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18928 "Overlapping device reset events!", NULL); 18929 } 18930 #endif 18931 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 18932 "Processing port %d:%d device reset", cport, pmport); 18933 18934 /* Clear event flag */ 18935 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET; 18936 18937 /* It seems that we always need to check the port state first */ 18938 sata_device.satadev_rev = SATA_DEVICE_REV; 18939 sata_device.satadev_addr = *saddr; 18940 /* 18941 * We have to exit mutex, because the HBA probe port function may 18942 * block on its own mutex. 18943 */ 18944 mutex_exit(&pmportinfo->pmport_mutex); 18945 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 18946 (SATA_DIP(sata_hba_inst), &sata_device); 18947 mutex_enter(&pmportinfo->pmport_mutex); 18948 18949 sata_update_pmport_info(sata_hba_inst, &sata_device); 18950 if (rval != SATA_SUCCESS) { 18951 /* Something went wrong? Fail the port */ 18952 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 18953 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport, 18954 saddr->pmport); 18955 if (sdinfo != NULL) 18956 sdinfo->satadrv_event_flags = 0; 18957 mutex_exit(&pmportinfo->pmport_mutex); 18958 SATA_LOG_D((sata_hba_inst, CE_WARN, 18959 "SATA port %d:%d probing failed", 18960 saddr->cport, saddr->pmport)); 18961 mutex_enter(&pmportinfo->pmport_mutex); 18962 return; 18963 } 18964 if ((sata_device.satadev_scr.sstatus & 18965 SATA_PORT_DEVLINK_UP_MASK) != 18966 SATA_PORT_DEVLINK_UP || 18967 sata_device.satadev_type == SATA_DTYPE_NONE) { 18968 /* 18969 * No device to process, anymore. Some other event processing 18970 * would or have already performed port info cleanup. 18971 * To be safe (HBA may need it), request clearing device 18972 * reset condition. 18973 */ 18974 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport, 18975 saddr->pmport); 18976 if (sdinfo != NULL) { 18977 sdinfo->satadrv_event_flags &= 18978 ~SATA_EVNT_INPROC_DEVICE_RESET; 18979 /* must clear flags on cport */ 18980 pminfo = SATA_PMULT_INFO(sata_hba_inst, 18981 saddr->cport); 18982 pminfo->pmult_event_flags |= 18983 SATA_EVNT_CLEAR_DEVICE_RESET; 18984 } 18985 return; 18986 } 18987 18988 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport, 18989 saddr->pmport); 18990 if (sdinfo == NULL) { 18991 return; 18992 } 18993 if ((sdinfo->satadrv_event_flags & 18994 SATA_EVNT_INPROC_DEVICE_RESET) == 0) { 18995 /* 18996 * Start tracking time for device feature restoration and 18997 * identification. Save current time (lbolt value). 18998 */ 18999 sdinfo->satadrv_reset_time = ddi_get_lbolt(); 19000 } 19001 /* Mark device reset processing as active */ 19002 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET; 19003 19004 old_sdinfo = *sdinfo; /* local copy of the drive info */ 19005 mutex_exit(&pmportinfo->pmport_mutex); 19006 19007 if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) == 19008 SATA_FAILURE) { 19009 /* 19010 * Restoring drive setting failed. 19011 * Probe the port first, to check if the port state has changed 19012 */ 19013 sata_device.satadev_rev = SATA_DEVICE_REV; 19014 sata_device.satadev_addr = *saddr; 19015 sata_device.satadev_addr.qual = SATA_ADDR_PMPORT; 19016 19017 /* probe port */ 19018 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 19019 (SATA_DIP(sata_hba_inst), &sata_device); 19020 mutex_enter(&pmportinfo->pmport_mutex); 19021 if (rval == SATA_SUCCESS && 19022 (sata_device.satadev_state & 19023 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 && 19024 (sata_device.satadev_scr.sstatus & 19025 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP && 19026 sata_device.satadev_type != SATA_DTYPE_NONE) { 19027 /* 19028 * We may retry this a bit later - in-process reset 19029 * condition should be already set. 19030 * Track retry time for device identification. 19031 */ 19032 if ((pmportinfo->pmport_dev_type & 19033 SATA_VALID_DEV_TYPE) != 0 && 19034 SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL && 19035 sdinfo->satadrv_reset_time != 0) { 19036 clock_t cur_time = ddi_get_lbolt(); 19037 /* 19038 * If the retry time limit was not 19039 * exceeded, retry. 19040 */ 19041 if ((cur_time - sdinfo->satadrv_reset_time) < 19042 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 19043 mutex_enter( 19044 &sata_hba_inst->satahba_mutex); 19045 sata_hba_inst->satahba_event_flags |= 19046 SATA_EVNT_MAIN; 19047 mutex_exit( 19048 &sata_hba_inst->satahba_mutex); 19049 mutex_enter(&sata_mutex); 19050 sata_event_pending |= SATA_EVNT_MAIN; 19051 mutex_exit(&sata_mutex); 19052 return; 19053 } 19054 } 19055 /* Fail the drive */ 19056 sdinfo->satadrv_state = SATA_DSTATE_FAILED; 19057 19058 sata_log(sata_hba_inst, CE_WARN, 19059 "SATA device at port %d:%d - device failed", 19060 saddr->cport, saddr->pmport); 19061 } else { 19062 /* 19063 * No point of retrying - some other event processing 19064 * would or already did port info cleanup. 19065 * To be safe (HBA may need it), 19066 * request clearing device reset condition. 19067 */ 19068 sdinfo->satadrv_event_flags |= 19069 SATA_EVNT_CLEAR_DEVICE_RESET; 19070 } 19071 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET; 19072 sdinfo->satadrv_reset_time = 0; 19073 return; 19074 } 19075 /* 19076 * Raise the flag indicating that the next sata command could 19077 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device 19078 * reset is reported. 19079 */ 19080 mutex_enter(&pmportinfo->pmport_mutex); 19081 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 19082 sdinfo->satadrv_reset_time = 0; 19083 if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) { 19084 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 19085 sdinfo->satadrv_event_flags &= 19086 ~SATA_EVNT_INPROC_DEVICE_RESET; 19087 /* must clear flags on cport */ 19088 pminfo = SATA_PMULT_INFO(sata_hba_inst, 19089 saddr->cport); 19090 pminfo->pmult_event_flags |= 19091 SATA_EVNT_CLEAR_DEVICE_RESET; 19092 } 19093 } 19094 } 19095 19096 /* 19097 * Port Link Events processing. 19098 * Every link established event may involve device reset (due to 19099 * COMRESET signal, equivalent of the hard reset) so arbitrarily 19100 * set device reset event for an attached device (if any). 19101 * If the port is in SHUTDOWN or FAILED state, ignore link events. 19102 * 19103 * The link established event processing varies, depending on the state 19104 * of the target node, HBA hotplugging capabilities, state of the port. 19105 * If the link is not active, the link established event is ignored. 19106 * If HBA cannot detect device attachment and there is no target node, 19107 * the link established event triggers device attach event processing. 19108 * Else, link established event triggers device reset event processing. 19109 * 19110 * The link lost event processing varies, depending on a HBA hotplugging 19111 * capability and the state of the port (link active or not active). 19112 * If the link is active, the lost link event is ignored. 19113 * If HBA cannot detect device removal, the lost link event triggers 19114 * device detached event processing after link lost timeout. 19115 * Else, the event is ignored. 19116 * 19117 * NOTE: Port multiplier ports events are handled by 19118 * sata_process_pmport_link_events(); 19119 */ 19120 static void 19121 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst, 19122 sata_address_t *saddr) 19123 { 19124 sata_device_t sata_device; 19125 sata_cport_info_t *cportinfo; 19126 sata_drive_info_t *sdinfo; 19127 uint32_t event_flags; 19128 int rval; 19129 19130 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19131 "Processing port %d link event(s)", saddr->cport); 19132 19133 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 19134 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19135 event_flags = cportinfo->cport_event_flags; 19136 19137 /* Reset event flags first */ 19138 cportinfo->cport_event_flags &= 19139 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST); 19140 19141 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */ 19142 if ((cportinfo->cport_state & 19143 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 19144 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19145 cport_mutex); 19146 return; 19147 } 19148 19149 /* 19150 * For the sanity sake get current port state. 19151 * Set device address only. Other sata_device fields should be 19152 * set by HBA driver. 19153 */ 19154 sata_device.satadev_rev = SATA_DEVICE_REV; 19155 sata_device.satadev_addr = *saddr; 19156 /* 19157 * We have to exit mutex, because the HBA probe port function may 19158 * block on its own mutex. 19159 */ 19160 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19161 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 19162 (SATA_DIP(sata_hba_inst), &sata_device); 19163 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19164 sata_update_port_info(sata_hba_inst, &sata_device); 19165 if (rval != SATA_SUCCESS) { 19166 /* Something went wrong? Fail the port */ 19167 cportinfo->cport_state = SATA_PSTATE_FAILED; 19168 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19169 cport_mutex); 19170 SATA_LOG_D((sata_hba_inst, CE_WARN, 19171 "SATA port %d probing failed", 19172 saddr->cport)); 19173 /* 19174 * We may want to release device info structure, but 19175 * it is not necessary. 19176 */ 19177 return; 19178 } else { 19179 /* port probed successfully */ 19180 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 19181 } 19182 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) { 19183 19184 if ((sata_device.satadev_scr.sstatus & 19185 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) { 19186 /* Ignore event */ 19187 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19188 "Ignoring port %d link established event - " 19189 "link down", 19190 saddr->cport); 19191 goto linklost; 19192 } 19193 19194 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19195 "Processing port %d link established event", 19196 saddr->cport); 19197 19198 /* 19199 * For the sanity sake check if a device is attached - check 19200 * return state of a port probing. 19201 */ 19202 if (sata_device.satadev_type != SATA_DTYPE_NONE) { 19203 /* 19204 * HBA port probe indicated that there is a device 19205 * attached. Check if the framework had device info 19206 * structure attached for this device. 19207 */ 19208 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 19209 ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) != 19210 NULL); 19211 19212 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 19213 if ((sdinfo->satadrv_type & 19214 SATA_VALID_DEV_TYPE) != 0) { 19215 /* 19216 * Dev info structure is present. 19217 * If dev_type is set to known type in 19218 * the framework's drive info struct 19219 * then the device existed before and 19220 * the link was probably lost 19221 * momentarily - in such case 19222 * we may want to check device 19223 * identity. 19224 * Identity check is not supported now. 19225 * 19226 * Link established event 19227 * triggers device reset event. 19228 */ 19229 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 19230 satadrv_event_flags |= 19231 SATA_EVNT_DEVICE_RESET; 19232 } 19233 } else if (cportinfo->cport_dev_type == 19234 SATA_DTYPE_NONE) { 19235 /* 19236 * We got new device attached! If HBA does not 19237 * generate device attached events, trigger it 19238 * here. 19239 */ 19240 if (!(SATA_FEATURES(sata_hba_inst) & 19241 SATA_CTLF_HOTPLUG)) { 19242 cportinfo->cport_event_flags |= 19243 SATA_EVNT_DEVICE_ATTACHED; 19244 } 19245 } 19246 /* Reset link lost timeout */ 19247 cportinfo->cport_link_lost_time = 0; 19248 } 19249 } 19250 linklost: 19251 if (event_flags & SATA_EVNT_LINK_LOST) { 19252 if ((sata_device.satadev_scr.sstatus & 19253 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) { 19254 /* Ignore event */ 19255 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19256 "Ignoring port %d link lost event - link is up", 19257 saddr->cport); 19258 goto done; 19259 } 19260 #ifdef SATA_DEBUG 19261 if (cportinfo->cport_link_lost_time == 0) { 19262 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19263 "Processing port %d link lost event", 19264 saddr->cport); 19265 } 19266 #endif 19267 /* 19268 * When HBA cannot generate device attached/detached events, 19269 * we need to track link lost time and eventually generate 19270 * device detach event. 19271 */ 19272 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) { 19273 /* We are tracking link lost time */ 19274 if (cportinfo->cport_link_lost_time == 0) { 19275 /* save current time (lbolt value) */ 19276 cportinfo->cport_link_lost_time = 19277 ddi_get_lbolt(); 19278 /* just keep link lost event */ 19279 cportinfo->cport_event_flags |= 19280 SATA_EVNT_LINK_LOST; 19281 } else { 19282 clock_t cur_time = ddi_get_lbolt(); 19283 if ((cur_time - 19284 cportinfo->cport_link_lost_time) >= 19285 drv_usectohz( 19286 SATA_EVNT_LINK_LOST_TIMEOUT)) { 19287 /* trigger device detach event */ 19288 cportinfo->cport_event_flags |= 19289 SATA_EVNT_DEVICE_DETACHED; 19290 cportinfo->cport_link_lost_time = 0; 19291 SATADBG1(SATA_DBG_EVENTS, 19292 sata_hba_inst, 19293 "Triggering port %d " 19294 "device detached event", 19295 saddr->cport); 19296 } else { 19297 /* keep link lost event */ 19298 cportinfo->cport_event_flags |= 19299 SATA_EVNT_LINK_LOST; 19300 } 19301 } 19302 } 19303 /* 19304 * We could change port state to disable/delay access to 19305 * the attached device until the link is recovered. 19306 */ 19307 } 19308 done: 19309 event_flags = cportinfo->cport_event_flags; 19310 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19311 if (event_flags != 0) { 19312 mutex_enter(&sata_hba_inst->satahba_mutex); 19313 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 19314 mutex_exit(&sata_hba_inst->satahba_mutex); 19315 mutex_enter(&sata_mutex); 19316 sata_event_pending |= SATA_EVNT_MAIN; 19317 mutex_exit(&sata_mutex); 19318 } 19319 } 19320 19321 /* 19322 * Port Multiplier Port Link Events processing. 19323 */ 19324 static void 19325 sata_process_pmport_link_events(sata_hba_inst_t *sata_hba_inst, 19326 sata_address_t *saddr) 19327 { 19328 sata_device_t sata_device; 19329 sata_pmport_info_t *pmportinfo = NULL; 19330 sata_drive_info_t *sdinfo = NULL; 19331 uint32_t event_flags; 19332 uint8_t cport = saddr->cport; 19333 uint8_t pmport = saddr->pmport; 19334 int rval; 19335 19336 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19337 "Processing port %d:%d link event(s)", 19338 cport, pmport); 19339 19340 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 19341 mutex_enter(&pmportinfo->pmport_mutex); 19342 event_flags = pmportinfo->pmport_event_flags; 19343 19344 /* Reset event flags first */ 19345 pmportinfo->pmport_event_flags &= 19346 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST); 19347 19348 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */ 19349 if ((pmportinfo->pmport_state & 19350 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 19351 mutex_exit(&pmportinfo->pmport_mutex); 19352 return; 19353 } 19354 19355 /* 19356 * For the sanity sake get current port state. 19357 * Set device address only. Other sata_device fields should be 19358 * set by HBA driver. 19359 */ 19360 sata_device.satadev_rev = SATA_DEVICE_REV; 19361 sata_device.satadev_addr = *saddr; 19362 /* 19363 * We have to exit mutex, because the HBA probe port function may 19364 * block on its own mutex. 19365 */ 19366 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 19367 saddr->pmport)); 19368 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 19369 (SATA_DIP(sata_hba_inst), &sata_device); 19370 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 19371 saddr->pmport)); 19372 sata_update_pmport_info(sata_hba_inst, &sata_device); 19373 if (rval != SATA_SUCCESS) { 19374 /* Something went wrong? Fail the port */ 19375 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 19376 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 19377 saddr->pmport)); 19378 SATA_LOG_D((sata_hba_inst, CE_WARN, 19379 "SATA port %d:%d probing failed", 19380 saddr->cport, saddr->pmport)); 19381 /* 19382 * We may want to release device info structure, but 19383 * it is not necessary. 19384 */ 19385 return; 19386 } else { 19387 /* port probed successfully */ 19388 pmportinfo->pmport_state |= 19389 SATA_STATE_PROBED | SATA_STATE_READY; 19390 } 19391 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, 19392 saddr->cport, saddr->pmport)); 19393 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, 19394 saddr->cport, saddr->pmport)); 19395 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) { 19396 19397 if ((sata_device.satadev_scr.sstatus & 19398 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) { 19399 /* Ignore event */ 19400 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19401 "Ignoring port %d:%d link established event - " 19402 "link down", 19403 saddr->cport, saddr->pmport); 19404 goto linklost; 19405 } 19406 19407 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19408 "Processing port %d:%d link established event", 19409 cport, pmport); 19410 19411 /* 19412 * For the sanity sake check if a device is attached - check 19413 * return state of a port probing. 19414 */ 19415 if (sata_device.satadev_type != SATA_DTYPE_NONE && 19416 sata_device.satadev_type != SATA_DTYPE_PMULT) { 19417 /* 19418 * HBA port probe indicated that there is a device 19419 * attached. Check if the framework had device info 19420 * structure attached for this device. 19421 */ 19422 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 19423 ASSERT(SATA_PMPORTINFO_DRV_INFO(pmportinfo) != 19424 NULL); 19425 19426 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 19427 if ((sdinfo->satadrv_type & 19428 SATA_VALID_DEV_TYPE) != 0) { 19429 /* 19430 * Dev info structure is present. 19431 * If dev_type is set to known type in 19432 * the framework's drive info struct 19433 * then the device existed before and 19434 * the link was probably lost 19435 * momentarily - in such case 19436 * we may want to check device 19437 * identity. 19438 * Identity check is not supported now. 19439 * 19440 * Link established event 19441 * triggers device reset event. 19442 */ 19443 (SATA_PMPORTINFO_DRV_INFO(pmportinfo))-> 19444 satadrv_event_flags |= 19445 SATA_EVNT_DEVICE_RESET; 19446 } 19447 } else if (pmportinfo->pmport_dev_type == 19448 SATA_DTYPE_NONE) { 19449 /* 19450 * We got new device attached! If HBA does not 19451 * generate device attached events, trigger it 19452 * here. 19453 */ 19454 if (!(SATA_FEATURES(sata_hba_inst) & 19455 SATA_CTLF_HOTPLUG)) { 19456 pmportinfo->pmport_event_flags |= 19457 SATA_EVNT_DEVICE_ATTACHED; 19458 } 19459 } 19460 /* Reset link lost timeout */ 19461 pmportinfo->pmport_link_lost_time = 0; 19462 } 19463 } 19464 linklost: 19465 if (event_flags & SATA_EVNT_LINK_LOST) { 19466 #ifdef SATA_DEBUG 19467 if (pmportinfo->pmport_link_lost_time == 0) { 19468 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19469 "Processing port %d:%d link lost event", 19470 saddr->cport, saddr->pmport); 19471 } 19472 #endif 19473 if ((sata_device.satadev_scr.sstatus & 19474 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) { 19475 /* Ignore event */ 19476 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19477 "Ignoring port %d:%d link lost event - link is up", 19478 saddr->cport, saddr->pmport); 19479 goto done; 19480 } 19481 /* 19482 * When HBA cannot generate device attached/detached events, 19483 * we need to track link lost time and eventually generate 19484 * device detach event. 19485 */ 19486 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) { 19487 /* We are tracking link lost time */ 19488 if (pmportinfo->pmport_link_lost_time == 0) { 19489 /* save current time (lbolt value) */ 19490 pmportinfo->pmport_link_lost_time = 19491 ddi_get_lbolt(); 19492 /* just keep link lost event */ 19493 pmportinfo->pmport_event_flags |= 19494 SATA_EVNT_LINK_LOST; 19495 } else { 19496 clock_t cur_time = ddi_get_lbolt(); 19497 if ((cur_time - 19498 pmportinfo->pmport_link_lost_time) >= 19499 drv_usectohz( 19500 SATA_EVNT_LINK_LOST_TIMEOUT)) { 19501 /* trigger device detach event */ 19502 pmportinfo->pmport_event_flags |= 19503 SATA_EVNT_DEVICE_DETACHED; 19504 pmportinfo->pmport_link_lost_time = 0; 19505 SATADBG2(SATA_DBG_EVENTS, 19506 sata_hba_inst, 19507 "Triggering port %d:%d " 19508 "device detached event", 19509 saddr->cport, saddr->pmport); 19510 } else { 19511 /* keep link lost event */ 19512 pmportinfo->pmport_event_flags |= 19513 SATA_EVNT_LINK_LOST; 19514 } 19515 } 19516 } 19517 /* 19518 * We could change port state to disable/delay access to 19519 * the attached device until the link is recovered. 19520 */ 19521 } 19522 done: 19523 event_flags = pmportinfo->pmport_event_flags; 19524 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport, 19525 saddr->pmport)); 19526 if (event_flags != 0) { 19527 mutex_enter(&sata_hba_inst->satahba_mutex); 19528 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 19529 mutex_exit(&sata_hba_inst->satahba_mutex); 19530 mutex_enter(&sata_mutex); 19531 sata_event_pending |= SATA_EVNT_MAIN; 19532 mutex_exit(&sata_mutex); 19533 } 19534 } 19535 19536 /* 19537 * Device Detached Event processing. 19538 * Port is probed to find if a device is really gone. If so, 19539 * the device info structure is detached from the SATA port info structure 19540 * and released. 19541 * Port status is updated. 19542 * 19543 * NOTE: Port multiplier ports events are handled by 19544 * sata_process_pmdevice_detached() 19545 */ 19546 static void 19547 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst, 19548 sata_address_t *saddr) 19549 { 19550 sata_cport_info_t *cportinfo; 19551 sata_pmport_info_t *pmportinfo; 19552 sata_drive_info_t *sdevinfo; 19553 sata_device_t sata_device; 19554 sata_address_t pmport_addr; 19555 char name[16]; 19556 uint8_t cport = saddr->cport; 19557 int npmport; 19558 int rval; 19559 19560 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19561 "Processing port %d device detached", saddr->cport); 19562 19563 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 19564 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19565 /* Clear event flag */ 19566 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED; 19567 19568 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */ 19569 if ((cportinfo->cport_state & 19570 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 19571 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19572 cport_mutex); 19573 return; 19574 } 19575 /* For sanity, re-probe the port */ 19576 sata_device.satadev_rev = SATA_DEVICE_REV; 19577 sata_device.satadev_addr = *saddr; 19578 19579 /* 19580 * We have to exit mutex, because the HBA probe port function may 19581 * block on its own mutex. 19582 */ 19583 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19584 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 19585 (SATA_DIP(sata_hba_inst), &sata_device); 19586 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19587 sata_update_port_info(sata_hba_inst, &sata_device); 19588 if (rval != SATA_SUCCESS) { 19589 /* Something went wrong? Fail the port */ 19590 cportinfo->cport_state = SATA_PSTATE_FAILED; 19591 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19592 cport_mutex); 19593 SATA_LOG_D((sata_hba_inst, CE_WARN, 19594 "SATA port %d probing failed", 19595 saddr->cport)); 19596 /* 19597 * We may want to release device info structure, but 19598 * it is not necessary. 19599 */ 19600 return; 19601 } else { 19602 /* port probed successfully */ 19603 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 19604 } 19605 /* 19606 * Check if a device is still attached. For sanity, check also 19607 * link status - if no link, there is no device. 19608 */ 19609 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) == 19610 SATA_PORT_DEVLINK_UP && sata_device.satadev_type != 19611 SATA_DTYPE_NONE) { 19612 /* 19613 * Device is still attached - ignore detach event. 19614 */ 19615 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19616 cport_mutex); 19617 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19618 "Ignoring detach - device still attached to port %d", 19619 sata_device.satadev_addr.cport); 19620 return; 19621 } 19622 /* 19623 * We need to detach and release device info structure here 19624 */ 19625 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 19626 /* 19627 * A port-multiplier is removed. 19628 * 19629 * Calling sata_process_pmdevice_detached() does not work 19630 * here. The port multiplier is gone, so we cannot probe 19631 * sub-port any more and all pmult-related data structure must 19632 * be de-allocated immediately. Following structure of every 19633 * implemented sub-port behind the pmult are required to 19634 * released. 19635 * 19636 * - attachment point 19637 * - target node 19638 * - sata_drive_info 19639 * - sata_pmport_info 19640 */ 19641 for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, 19642 cport); npmport ++) { 19643 SATADBG2(SATA_DBG_PMULT|SATA_DBG_EVENTS_PROC, 19644 sata_hba_inst, 19645 "Detaching target node at port %d:%d", 19646 cport, npmport); 19647 19648 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 19649 19650 /* Remove attachment point. */ 19651 name[0] = '\0'; 19652 (void) sprintf(name, "%d.%d", cport, npmport); 19653 ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name); 19654 sata_log(sata_hba_inst, CE_NOTE, 19655 "Remove attachment point of port %d:%d", 19656 cport, npmport); 19657 19658 /* Remove target node */ 19659 pmport_addr.cport = cport; 19660 pmport_addr.pmport = (uint8_t)npmport; 19661 pmport_addr.qual = SATA_ADDR_PMPORT; 19662 sata_remove_target_node(sata_hba_inst, &pmport_addr); 19663 19664 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 19665 19666 /* Release sata_pmport_info & sata_drive_info. */ 19667 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 19668 cport, npmport); 19669 ASSERT(pmportinfo != NULL); 19670 19671 sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 19672 if (sdevinfo != NULL) { 19673 (void) kmem_free((void *) sdevinfo, 19674 sizeof (sata_drive_info_t)); 19675 } 19676 19677 /* Release sata_pmport_info at last */ 19678 (void) kmem_free((void *) pmportinfo, 19679 sizeof (sata_pmport_info_t)); 19680 } 19681 19682 /* Finally, release sata_pmult_info */ 19683 (void) kmem_free((void *) 19684 SATA_CPORTINFO_PMULT_INFO(cportinfo), 19685 sizeof (sata_pmult_info_t)); 19686 SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL; 19687 19688 sata_log(sata_hba_inst, CE_WARN, 19689 "SATA port-multiplier detached at port %d", cport); 19690 19691 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 19692 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19693 saddr->cport)->cport_mutex); 19694 } else { 19695 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 19696 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 19697 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 19698 (void) kmem_free((void *)sdevinfo, 19699 sizeof (sata_drive_info_t)); 19700 } 19701 sata_log(sata_hba_inst, CE_WARN, 19702 "SATA device detached at port %d", cport); 19703 19704 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 19705 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 19706 saddr->cport)->cport_mutex); 19707 19708 /* 19709 * Try to offline a device and remove target node 19710 * if it still exists 19711 */ 19712 sata_remove_target_node(sata_hba_inst, saddr); 19713 } 19714 19715 19716 /* 19717 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 19718 * with the hint: SE_HINT_REMOVE 19719 */ 19720 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE); 19721 } 19722 19723 /* 19724 * Port Multiplier Port Device Deattached Event processing. 19725 * 19726 * NOTE: No Mutex should be hold. 19727 */ 19728 static void 19729 sata_process_pmdevice_detached(sata_hba_inst_t *sata_hba_inst, 19730 sata_address_t *saddr) 19731 { 19732 sata_pmport_info_t *pmportinfo; 19733 sata_drive_info_t *sdevinfo; 19734 sata_device_t sata_device; 19735 int rval; 19736 uint8_t cport, pmport; 19737 19738 cport = saddr->cport; 19739 pmport = saddr->pmport; 19740 19741 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19742 "Processing port %d:%d device detached", 19743 cport, pmport); 19744 19745 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 19746 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19747 19748 /* Clear event flag */ 19749 pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED; 19750 19751 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */ 19752 if ((pmportinfo->pmport_state & 19753 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 19754 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19755 return; 19756 } 19757 /* For sanity, re-probe the port */ 19758 sata_device.satadev_rev = SATA_DEVICE_REV; 19759 sata_device.satadev_addr = *saddr; 19760 19761 /* 19762 * We have to exit mutex, because the HBA probe port function may 19763 * block on its own mutex. 19764 */ 19765 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19766 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 19767 (SATA_DIP(sata_hba_inst), &sata_device); 19768 mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19769 sata_update_pmport_info(sata_hba_inst, &sata_device); 19770 if (rval != SATA_SUCCESS) { 19771 /* Something went wrong? Fail the port */ 19772 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 19773 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19774 SATA_LOG_D((sata_hba_inst, CE_WARN, 19775 "SATA port %d:%d probing failed", 19776 saddr->pmport)); 19777 /* 19778 * We may want to release device info structure, but 19779 * it is not necessary. 19780 */ 19781 return; 19782 } else { 19783 /* port probed successfully */ 19784 pmportinfo->pmport_state |= 19785 SATA_STATE_PROBED | SATA_STATE_READY; 19786 } 19787 /* 19788 * Check if a device is still attached. For sanity, check also 19789 * link status - if no link, there is no device. 19790 */ 19791 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) == 19792 SATA_PORT_DEVLINK_UP && sata_device.satadev_type != 19793 SATA_DTYPE_NONE) { 19794 /* 19795 * Device is still attached - ignore detach event. 19796 */ 19797 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19798 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19799 "Ignoring detach - device still attached to port %d", 19800 sata_device.satadev_addr.pmport); 19801 return; 19802 } 19803 /* 19804 * We need to detach and release device info structure here 19805 */ 19806 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 19807 sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 19808 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 19809 (void) kmem_free((void *)sdevinfo, 19810 sizeof (sata_drive_info_t)); 19811 } 19812 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 19813 /* 19814 * Device cannot be reached anymore, even if the target node may be 19815 * still present. 19816 */ 19817 mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport)); 19818 19819 /* 19820 * Try to offline a device and remove target node if it still exists 19821 */ 19822 sata_remove_target_node(sata_hba_inst, saddr); 19823 19824 /* 19825 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 19826 * with the hint: SE_HINT_REMOVE 19827 */ 19828 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE); 19829 } 19830 19831 19832 /* 19833 * Device Attached Event processing. 19834 * Port state is checked to verify that a device is really attached. If so, 19835 * the device info structure is created and attached to the SATA port info 19836 * structure. 19837 * 19838 * If attached device cannot be identified or set-up, the retry for the 19839 * attach processing is set-up. Subsequent daemon run would try again to 19840 * identify the device, until the time limit is reached 19841 * (SATA_DEV_IDENTIFY_TIMEOUT). 19842 * 19843 * This function cannot be called in interrupt context (it may sleep). 19844 * 19845 * NOTE: Port multiplier ports events are handled by 19846 * sata_process_pmdevice_attached() 19847 */ 19848 static void 19849 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst, 19850 sata_address_t *saddr) 19851 { 19852 sata_cport_info_t *cportinfo = NULL; 19853 sata_drive_info_t *sdevinfo = NULL; 19854 sata_pmult_info_t *pmultinfo = NULL; 19855 sata_pmport_info_t *pmportinfo = NULL; 19856 sata_device_t sata_device; 19857 dev_info_t *tdip; 19858 uint32_t event_flags = 0, pmult_event_flags = 0; 19859 int rval; 19860 int npmport; 19861 19862 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19863 "Processing port %d device attached", saddr->cport); 19864 19865 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 19866 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19867 19868 /* Clear attach event flag first */ 19869 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED; 19870 19871 /* If the port is in SHUTDOWN or FAILED state, ignore event. */ 19872 if ((cportinfo->cport_state & 19873 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 19874 cportinfo->cport_dev_attach_time = 0; 19875 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19876 cport_mutex); 19877 return; 19878 } 19879 19880 /* 19881 * If the sata_drive_info structure is found attached to the port info, 19882 * despite the fact the device was removed and now it is re-attached, 19883 * the old drive info structure was not removed. 19884 * Arbitrarily release device info structure. 19885 */ 19886 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 19887 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 19888 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 19889 (void) kmem_free((void *)sdevinfo, 19890 sizeof (sata_drive_info_t)); 19891 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19892 "Arbitrarily detaching old device info.", NULL); 19893 } 19894 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 19895 19896 /* For sanity, re-probe the port */ 19897 sata_device.satadev_rev = SATA_DEVICE_REV; 19898 sata_device.satadev_addr = *saddr; 19899 19900 /* 19901 * We have to exit mutex, because the HBA probe port function may 19902 * block on its own mutex. 19903 */ 19904 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19905 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 19906 (SATA_DIP(sata_hba_inst), &sata_device); 19907 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19908 sata_update_port_info(sata_hba_inst, &sata_device); 19909 if (rval != SATA_SUCCESS) { 19910 /* Something went wrong? Fail the port */ 19911 cportinfo->cport_state = SATA_PSTATE_FAILED; 19912 cportinfo->cport_dev_attach_time = 0; 19913 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19914 cport_mutex); 19915 SATA_LOG_D((sata_hba_inst, CE_WARN, 19916 "SATA port %d probing failed", 19917 saddr->cport)); 19918 return; 19919 } else { 19920 /* port probed successfully */ 19921 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 19922 } 19923 /* 19924 * Check if a device is still attached. For sanity, check also 19925 * link status - if no link, there is no device. 19926 */ 19927 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 19928 SATA_PORT_DEVLINK_UP || sata_device.satadev_type == 19929 SATA_DTYPE_NONE) { 19930 /* 19931 * No device - ignore attach event. 19932 */ 19933 cportinfo->cport_dev_attach_time = 0; 19934 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19935 cport_mutex); 19936 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 19937 "Ignoring attach - no device connected to port %d", 19938 sata_device.satadev_addr.cport); 19939 return; 19940 } 19941 19942 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 19943 /* 19944 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 19945 * with the hint: SE_HINT_INSERT 19946 */ 19947 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT); 19948 19949 /* 19950 * Port reprobing will take care of the creation of the device 19951 * info structure and determination of the device type. 19952 */ 19953 sata_device.satadev_addr = *saddr; 19954 (void) sata_reprobe_port(sata_hba_inst, &sata_device, 19955 SATA_DEV_IDENTIFY_NORETRY); 19956 19957 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 19958 cport_mutex); 19959 if ((cportinfo->cport_state & SATA_STATE_READY) && 19960 (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) { 19961 /* Some device is attached to the port */ 19962 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) { 19963 /* 19964 * A device was not successfully attached. 19965 * Track retry time for device identification. 19966 */ 19967 if (cportinfo->cport_dev_attach_time != 0) { 19968 clock_t cur_time = ddi_get_lbolt(); 19969 /* 19970 * If the retry time limit was not exceeded, 19971 * reinstate attach event. 19972 */ 19973 if ((cur_time - 19974 cportinfo->cport_dev_attach_time) < 19975 drv_usectohz( 19976 SATA_DEV_IDENTIFY_TIMEOUT)) { 19977 /* OK, restore attach event */ 19978 cportinfo->cport_event_flags |= 19979 SATA_EVNT_DEVICE_ATTACHED; 19980 } else { 19981 /* Timeout - cannot identify device */ 19982 cportinfo->cport_dev_attach_time = 0; 19983 sata_log(sata_hba_inst, 19984 CE_WARN, 19985 "Could not identify SATA device " 19986 "at port %d", 19987 saddr->cport); 19988 } 19989 } else { 19990 /* 19991 * Start tracking time for device 19992 * identification. 19993 * Save current time (lbolt value). 19994 */ 19995 cportinfo->cport_dev_attach_time = 19996 ddi_get_lbolt(); 19997 /* Restore attach event */ 19998 cportinfo->cport_event_flags |= 19999 SATA_EVNT_DEVICE_ATTACHED; 20000 } 20001 } else if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 20002 cportinfo->cport_dev_attach_time = 0; 20003 sata_log(sata_hba_inst, CE_NOTE, 20004 "SATA port-multiplier detected at port %d", 20005 saddr->cport); 20006 20007 if (SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL) { 20008 /* Log the info of new port multiplier */ 20009 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20010 saddr->cport)->cport_mutex); 20011 sata_show_pmult_info(sata_hba_inst, 20012 &sata_device); 20013 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20014 saddr->cport)->cport_mutex); 20015 } 20016 20017 ASSERT(SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL); 20018 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 20019 for (npmport = 0; npmport < 20020 pmultinfo->pmult_num_dev_ports; npmport++) { 20021 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 20022 saddr->cport, npmport); 20023 ASSERT(pmportinfo != NULL); 20024 20025 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20026 saddr->cport)->cport_mutex); 20027 mutex_enter(&pmportinfo->pmport_mutex); 20028 /* Marked all pmports with link events. */ 20029 pmportinfo->pmport_event_flags = 20030 SATA_EVNT_LINK_ESTABLISHED; 20031 pmult_event_flags |= 20032 pmportinfo->pmport_event_flags; 20033 mutex_exit(&pmportinfo->pmport_mutex); 20034 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20035 saddr->cport)->cport_mutex); 20036 } 20037 /* Auto-online is not available for PMult now. */ 20038 20039 } else { 20040 /* 20041 * If device was successfully attached, the subsequent 20042 * action depends on a state of the 20043 * sata_auto_online variable. If it is set to zero. 20044 * an explicit 'configure' command will be needed to 20045 * configure it. If its value is non-zero, we will 20046 * attempt to online (configure) the device. 20047 * First, log the message indicating that a device 20048 * was attached. 20049 */ 20050 cportinfo->cport_dev_attach_time = 0; 20051 sata_log(sata_hba_inst, CE_WARN, 20052 "SATA device detected at port %d", saddr->cport); 20053 20054 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 20055 sata_drive_info_t new_sdinfo; 20056 20057 /* Log device info data */ 20058 new_sdinfo = *(SATA_CPORTINFO_DRV_INFO( 20059 cportinfo)); 20060 sata_show_drive_info(sata_hba_inst, 20061 &new_sdinfo); 20062 } 20063 20064 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20065 saddr->cport)->cport_mutex); 20066 20067 /* 20068 * Make sure that there is no target node for that 20069 * device. If so, release it. It should not happen, 20070 * unless we had problem removing the node when 20071 * device was detached. 20072 */ 20073 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 20074 saddr->cport, saddr->pmport); 20075 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20076 saddr->cport)->cport_mutex); 20077 if (tdip != NULL) { 20078 20079 #ifdef SATA_DEBUG 20080 if ((cportinfo->cport_event_flags & 20081 SATA_EVNT_TARGET_NODE_CLEANUP) == 0) 20082 sata_log(sata_hba_inst, CE_WARN, 20083 "sata_process_device_attached: " 20084 "old device target node exists!"); 20085 #endif 20086 /* 20087 * target node exists - try to unconfigure 20088 * device and remove the node. 20089 */ 20090 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20091 saddr->cport)->cport_mutex); 20092 rval = ndi_devi_offline(tdip, 20093 NDI_DEVI_REMOVE); 20094 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20095 saddr->cport)->cport_mutex); 20096 20097 if (rval == NDI_SUCCESS) { 20098 cportinfo->cport_event_flags &= 20099 ~SATA_EVNT_TARGET_NODE_CLEANUP; 20100 cportinfo->cport_tgtnode_clean = B_TRUE; 20101 } else { 20102 /* 20103 * PROBLEM - the target node remained 20104 * and it belongs to a previously 20105 * attached device. 20106 * This happens when the file was open 20107 * or the node was waiting for 20108 * resources at the time the 20109 * associated device was removed. 20110 * Instruct event daemon to retry the 20111 * cleanup later. 20112 */ 20113 sata_log(sata_hba_inst, 20114 CE_WARN, 20115 "Application(s) accessing " 20116 "previously attached SATA " 20117 "device have to release " 20118 "it before newly inserted " 20119 "device can be made accessible.", 20120 saddr->cport); 20121 cportinfo->cport_event_flags |= 20122 SATA_EVNT_TARGET_NODE_CLEANUP; 20123 cportinfo->cport_tgtnode_clean = 20124 B_FALSE; 20125 } 20126 } 20127 if (sata_auto_online != 0) { 20128 cportinfo->cport_event_flags |= 20129 SATA_EVNT_AUTOONLINE_DEVICE; 20130 } 20131 20132 } 20133 } else { 20134 cportinfo->cport_dev_attach_time = 0; 20135 } 20136 20137 event_flags = cportinfo->cport_event_flags; 20138 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 20139 if (event_flags != 0 || pmult_event_flags != 0) { 20140 mutex_enter(&sata_hba_inst->satahba_mutex); 20141 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 20142 mutex_exit(&sata_hba_inst->satahba_mutex); 20143 mutex_enter(&sata_mutex); 20144 sata_event_pending |= SATA_EVNT_MAIN; 20145 mutex_exit(&sata_mutex); 20146 } 20147 } 20148 20149 /* 20150 * Port Multiplier Port Device Attached Event processing. 20151 * 20152 * NOTE: No Mutex should be hold. 20153 */ 20154 static void 20155 sata_process_pmdevice_attached(sata_hba_inst_t *sata_hba_inst, 20156 sata_address_t *saddr) 20157 { 20158 sata_pmport_info_t *pmportinfo; 20159 sata_drive_info_t *sdinfo; 20160 sata_device_t sata_device; 20161 dev_info_t *tdip; 20162 uint32_t event_flags; 20163 uint8_t cport = saddr->cport; 20164 uint8_t pmport = saddr->pmport; 20165 int rval; 20166 20167 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20168 "Processing port %d:%d device attached", cport, pmport); 20169 20170 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 20171 20172 mutex_enter(&pmportinfo->pmport_mutex); 20173 20174 /* Clear attach event flag first */ 20175 pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED; 20176 20177 /* If the port is in SHUTDOWN or FAILED state, ignore event. */ 20178 if ((pmportinfo->pmport_state & 20179 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 20180 pmportinfo->pmport_dev_attach_time = 0; 20181 mutex_exit(&pmportinfo->pmport_mutex); 20182 return; 20183 } 20184 20185 /* 20186 * If the sata_drive_info structure is found attached to the port info, 20187 * despite the fact the device was removed and now it is re-attached, 20188 * the old drive info structure was not removed. 20189 * Arbitrarily release device info structure. 20190 */ 20191 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 20192 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 20193 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 20194 (void) kmem_free((void *)sdinfo, 20195 sizeof (sata_drive_info_t)); 20196 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20197 "Arbitrarily detaching old device info.", NULL); 20198 } 20199 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 20200 20201 /* For sanity, re-probe the port */ 20202 sata_device.satadev_rev = SATA_DEVICE_REV; 20203 sata_device.satadev_addr = *saddr; 20204 20205 /* 20206 * We have to exit mutex, because the HBA probe port function may 20207 * block on its own mutex. 20208 */ 20209 mutex_exit(&pmportinfo->pmport_mutex); 20210 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 20211 (SATA_DIP(sata_hba_inst), &sata_device); 20212 mutex_enter(&pmportinfo->pmport_mutex); 20213 20214 sata_update_pmport_info(sata_hba_inst, &sata_device); 20215 if (rval != SATA_SUCCESS) { 20216 /* Something went wrong? Fail the port */ 20217 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 20218 pmportinfo->pmport_dev_attach_time = 0; 20219 mutex_exit(&pmportinfo->pmport_mutex); 20220 SATA_LOG_D((sata_hba_inst, CE_WARN, 20221 "SATA port %d:%d probing failed", cport, pmport)); 20222 return; 20223 } else { 20224 /* pmport probed successfully */ 20225 pmportinfo->pmport_state |= 20226 SATA_STATE_PROBED | SATA_STATE_READY; 20227 } 20228 /* 20229 * Check if a device is still attached. For sanity, check also 20230 * link status - if no link, there is no device. 20231 */ 20232 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 20233 SATA_PORT_DEVLINK_UP || sata_device.satadev_type == 20234 SATA_DTYPE_NONE) { 20235 /* 20236 * No device - ignore attach event. 20237 */ 20238 pmportinfo->pmport_dev_attach_time = 0; 20239 mutex_exit(&pmportinfo->pmport_mutex); 20240 SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20241 "Ignoring attach - no device connected to port %d:%d", 20242 cport, pmport); 20243 return; 20244 } 20245 20246 mutex_exit(&pmportinfo->pmport_mutex); 20247 /* 20248 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 20249 * with the hint: SE_HINT_INSERT 20250 */ 20251 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT); 20252 20253 /* 20254 * Port reprobing will take care of the creation of the device 20255 * info structure and determination of the device type. 20256 */ 20257 sata_device.satadev_addr = *saddr; 20258 (void) sata_reprobe_port(sata_hba_inst, &sata_device, 20259 SATA_DEV_IDENTIFY_NORETRY); 20260 20261 mutex_enter(&pmportinfo->pmport_mutex); 20262 if ((pmportinfo->pmport_state & SATA_STATE_READY) && 20263 (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE)) { 20264 /* Some device is attached to the port */ 20265 if (pmportinfo->pmport_dev_type == SATA_DTYPE_UNKNOWN) { 20266 /* 20267 * A device was not successfully attached. 20268 * Track retry time for device identification. 20269 */ 20270 if (pmportinfo->pmport_dev_attach_time != 0) { 20271 clock_t cur_time = ddi_get_lbolt(); 20272 /* 20273 * If the retry time limit was not exceeded, 20274 * reinstate attach event. 20275 */ 20276 if ((cur_time - 20277 pmportinfo->pmport_dev_attach_time) < 20278 drv_usectohz( 20279 SATA_DEV_IDENTIFY_TIMEOUT)) { 20280 /* OK, restore attach event */ 20281 pmportinfo->pmport_event_flags |= 20282 SATA_EVNT_DEVICE_ATTACHED; 20283 } else { 20284 /* Timeout - cannot identify device */ 20285 pmportinfo->pmport_dev_attach_time = 0; 20286 sata_log(sata_hba_inst, CE_WARN, 20287 "Could not identify SATA device " 20288 "at port %d:%d", 20289 cport, pmport); 20290 } 20291 } else { 20292 /* 20293 * Start tracking time for device 20294 * identification. 20295 * Save current time (lbolt value). 20296 */ 20297 pmportinfo->pmport_dev_attach_time = 20298 ddi_get_lbolt(); 20299 /* Restore attach event */ 20300 pmportinfo->pmport_event_flags |= 20301 SATA_EVNT_DEVICE_ATTACHED; 20302 } 20303 } else { 20304 /* 20305 * If device was successfully attached, the subsequent 20306 * action depends on a state of the 20307 * sata_auto_online variable. If it is set to zero. 20308 * an explicit 'configure' command will be needed to 20309 * configure it. If its value is non-zero, we will 20310 * attempt to online (configure) the device. 20311 * First, log the message indicating that a device 20312 * was attached. 20313 */ 20314 pmportinfo->pmport_dev_attach_time = 0; 20315 sata_log(sata_hba_inst, CE_WARN, 20316 "SATA device detected at port %d:%d", 20317 cport, pmport); 20318 20319 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 20320 sata_drive_info_t new_sdinfo; 20321 20322 /* Log device info data */ 20323 new_sdinfo = *(SATA_PMPORTINFO_DRV_INFO( 20324 pmportinfo)); 20325 sata_show_drive_info(sata_hba_inst, 20326 &new_sdinfo); 20327 } 20328 20329 mutex_exit(&pmportinfo->pmport_mutex); 20330 20331 /* 20332 * Make sure that there is no target node for that 20333 * device. If so, release it. It should not happen, 20334 * unless we had problem removing the node when 20335 * device was detached. 20336 */ 20337 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 20338 saddr->cport, saddr->pmport); 20339 mutex_enter(&pmportinfo->pmport_mutex); 20340 if (tdip != NULL) { 20341 20342 #ifdef SATA_DEBUG 20343 if ((pmportinfo->pmport_event_flags & 20344 SATA_EVNT_TARGET_NODE_CLEANUP) == 0) 20345 sata_log(sata_hba_inst, CE_WARN, 20346 "sata_process_device_attached: " 20347 "old device target node exists!"); 20348 #endif 20349 /* 20350 * target node exists - try to unconfigure 20351 * device and remove the node. 20352 */ 20353 mutex_exit(&pmportinfo->pmport_mutex); 20354 rval = ndi_devi_offline(tdip, 20355 NDI_DEVI_REMOVE); 20356 mutex_enter(&pmportinfo->pmport_mutex); 20357 20358 if (rval == NDI_SUCCESS) { 20359 pmportinfo->pmport_event_flags &= 20360 ~SATA_EVNT_TARGET_NODE_CLEANUP; 20361 pmportinfo->pmport_tgtnode_clean = 20362 B_TRUE; 20363 } else { 20364 /* 20365 * PROBLEM - the target node remained 20366 * and it belongs to a previously 20367 * attached device. 20368 * This happens when the file was open 20369 * or the node was waiting for 20370 * resources at the time the 20371 * associated device was removed. 20372 * Instruct event daemon to retry the 20373 * cleanup later. 20374 */ 20375 sata_log(sata_hba_inst, 20376 CE_WARN, 20377 "Application(s) accessing " 20378 "previously attached SATA " 20379 "device have to release " 20380 "it before newly inserted " 20381 "device can be made accessible." 20382 "at port %d:%d", 20383 cport, pmport); 20384 pmportinfo->pmport_event_flags |= 20385 SATA_EVNT_TARGET_NODE_CLEANUP; 20386 pmportinfo->pmport_tgtnode_clean = 20387 B_FALSE; 20388 } 20389 } 20390 if (sata_auto_online != 0) { 20391 pmportinfo->pmport_event_flags |= 20392 SATA_EVNT_AUTOONLINE_DEVICE; 20393 } 20394 20395 } 20396 } else { 20397 pmportinfo->pmport_dev_attach_time = 0; 20398 } 20399 20400 event_flags = pmportinfo->pmport_event_flags; 20401 mutex_exit(&pmportinfo->pmport_mutex); 20402 if (event_flags != 0) { 20403 mutex_enter(&sata_hba_inst->satahba_mutex); 20404 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 20405 mutex_exit(&sata_hba_inst->satahba_mutex); 20406 mutex_enter(&sata_mutex); 20407 sata_event_pending |= SATA_EVNT_MAIN; 20408 mutex_exit(&sata_mutex); 20409 } 20410 20411 /* clear the reset_in_progress events */ 20412 if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) { 20413 if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) { 20414 /* must clear flags on cport */ 20415 sata_pmult_info_t *pminfo = 20416 SATA_PMULT_INFO(sata_hba_inst, 20417 saddr->cport); 20418 pminfo->pmult_event_flags |= 20419 SATA_EVNT_CLEAR_DEVICE_RESET; 20420 } 20421 } 20422 } 20423 20424 /* 20425 * Device Target Node Cleanup Event processing. 20426 * If the target node associated with a sata port device is in 20427 * DEVI_DEVICE_REMOVED state, an attempt is made to remove it. 20428 * If the target node cannot be removed, the event flag is left intact, 20429 * so that event daemon may re-run this function later. 20430 * 20431 * This function cannot be called in interrupt context (it may sleep). 20432 * 20433 * NOTE: Processes cport events only, not port multiplier ports. 20434 */ 20435 static void 20436 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, 20437 sata_address_t *saddr) 20438 { 20439 sata_cport_info_t *cportinfo; 20440 dev_info_t *tdip; 20441 20442 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20443 "Processing port %d device target node cleanup", saddr->cport); 20444 20445 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 20446 20447 /* 20448 * Check if there is target node for that device and it is in the 20449 * DEVI_DEVICE_REMOVED state. If so, release it. 20450 */ 20451 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport, 20452 saddr->pmport); 20453 if (tdip != NULL) { 20454 /* 20455 * target node exists - check if it is target node of 20456 * a removed device. 20457 */ 20458 if (sata_check_device_removed(tdip) == B_TRUE) { 20459 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20460 "sata_process_target_node_cleanup: " 20461 "old device target node exists!", NULL); 20462 /* 20463 * Unconfigure and remove the target node 20464 */ 20465 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) == 20466 NDI_SUCCESS) { 20467 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20468 saddr->cport)->cport_mutex); 20469 cportinfo->cport_event_flags &= 20470 ~SATA_EVNT_TARGET_NODE_CLEANUP; 20471 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20472 saddr->cport)->cport_mutex); 20473 return; 20474 } 20475 /* 20476 * Event daemon will retry the cleanup later. 20477 */ 20478 mutex_enter(&sata_hba_inst->satahba_mutex); 20479 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 20480 mutex_exit(&sata_hba_inst->satahba_mutex); 20481 mutex_enter(&sata_mutex); 20482 sata_event_pending |= SATA_EVNT_MAIN; 20483 mutex_exit(&sata_mutex); 20484 } 20485 } else { 20486 if (saddr->qual == SATA_ADDR_CPORT || 20487 saddr->qual == SATA_ADDR_DCPORT) { 20488 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20489 saddr->cport)->cport_mutex); 20490 cportinfo->cport_event_flags &= 20491 ~SATA_EVNT_TARGET_NODE_CLEANUP; 20492 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20493 saddr->cport)->cport_mutex); 20494 } else { 20495 /* sanity check */ 20496 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) != 20497 SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst, 20498 saddr->cport) == NULL) 20499 return; 20500 if (SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, 20501 saddr->pmport) == NULL) 20502 return; 20503 20504 mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst, 20505 saddr->cport, saddr->pmport)->pmport_mutex); 20506 SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, 20507 saddr->pmport)->pmport_event_flags &= 20508 ~SATA_EVNT_TARGET_NODE_CLEANUP; 20509 mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst, 20510 saddr->cport, saddr->pmport)->pmport_mutex); 20511 } 20512 } 20513 } 20514 20515 /* 20516 * Device AutoOnline Event processing. 20517 * If attached device is to be onlined, an attempt is made to online this 20518 * device, but only if there is no lingering (old) target node present. 20519 * If the device cannot be onlined, the event flag is left intact, 20520 * so that event daemon may re-run this function later. 20521 * 20522 * This function cannot be called in interrupt context (it may sleep). 20523 * 20524 * NOTE: Processes cport events only, not port multiplier ports. 20525 */ 20526 static void 20527 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst, 20528 sata_address_t *saddr) 20529 { 20530 sata_cport_info_t *cportinfo; 20531 sata_drive_info_t *sdinfo; 20532 sata_device_t sata_device; 20533 dev_info_t *tdip; 20534 20535 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20536 "Processing port %d attached device auto-onlining", saddr->cport); 20537 20538 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 20539 20540 /* 20541 * Check if device is present and recognized. If not, reset event. 20542 */ 20543 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 20544 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) { 20545 /* Nothing to online */ 20546 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE; 20547 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20548 saddr->cport)->cport_mutex); 20549 return; 20550 } 20551 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 20552 20553 /* 20554 * Check if there is target node for this device and if it is in the 20555 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep 20556 * the event for later processing. 20557 */ 20558 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport, 20559 saddr->pmport); 20560 if (tdip != NULL) { 20561 /* 20562 * target node exists - check if it is target node of 20563 * a removed device. 20564 */ 20565 if (sata_check_device_removed(tdip) == B_TRUE) { 20566 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 20567 "sata_process_device_autoonline: " 20568 "old device target node exists!", NULL); 20569 /* 20570 * Event daemon will retry device onlining later. 20571 */ 20572 mutex_enter(&sata_hba_inst->satahba_mutex); 20573 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 20574 mutex_exit(&sata_hba_inst->satahba_mutex); 20575 mutex_enter(&sata_mutex); 20576 sata_event_pending |= SATA_EVNT_MAIN; 20577 mutex_exit(&sata_mutex); 20578 return; 20579 } 20580 /* 20581 * If the target node is not in the 'removed" state, assume 20582 * that it belongs to this device. There is nothing more to do, 20583 * but reset the event. 20584 */ 20585 } else { 20586 20587 /* 20588 * Try to online the device 20589 * If there is any reset-related event, remove it. We are 20590 * configuring the device and no state restoring is needed. 20591 */ 20592 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20593 saddr->cport)->cport_mutex); 20594 sata_device.satadev_addr = *saddr; 20595 if (saddr->qual == SATA_ADDR_CPORT) 20596 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 20597 else 20598 sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT; 20599 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 20600 if (sdinfo != NULL) { 20601 if (sdinfo->satadrv_event_flags & 20602 (SATA_EVNT_DEVICE_RESET | 20603 SATA_EVNT_INPROC_DEVICE_RESET)) 20604 sdinfo->satadrv_event_flags = 0; 20605 sdinfo->satadrv_event_flags |= 20606 SATA_EVNT_CLEAR_DEVICE_RESET; 20607 20608 /* Need to create a new target node. */ 20609 cportinfo->cport_tgtnode_clean = B_TRUE; 20610 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20611 saddr->cport)->cport_mutex); 20612 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst), 20613 sata_hba_inst, &sata_device.satadev_addr); 20614 if (tdip == NULL) { 20615 /* 20616 * Configure (onlining) failed. 20617 * We will NOT retry 20618 */ 20619 SATA_LOG_D((sata_hba_inst, CE_WARN, 20620 "sata_process_device_autoonline: " 20621 "configuring SATA device at port %d failed", 20622 saddr->cport)); 20623 } 20624 } else { 20625 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20626 saddr->cport)->cport_mutex); 20627 } 20628 20629 } 20630 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 20631 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE; 20632 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20633 saddr->cport)->cport_mutex); 20634 } 20635 20636 20637 static void 20638 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr, 20639 int hint) 20640 { 20641 char ap[MAXPATHLEN]; 20642 nvlist_t *ev_attr_list = NULL; 20643 int err; 20644 20645 /* Allocate and build sysevent attribute list */ 20646 err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP); 20647 if (err != 0) { 20648 SATA_LOG_D((sata_hba_inst, CE_WARN, 20649 "sata_gen_sysevent: " 20650 "cannot allocate memory for sysevent attributes\n")); 20651 return; 20652 } 20653 /* Add hint attribute */ 20654 err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint)); 20655 if (err != 0) { 20656 SATA_LOG_D((sata_hba_inst, CE_WARN, 20657 "sata_gen_sysevent: " 20658 "failed to add DR_HINT attr for sysevent")); 20659 nvlist_free(ev_attr_list); 20660 return; 20661 } 20662 /* 20663 * Add AP attribute. 20664 * Get controller pathname and convert it into AP pathname by adding 20665 * a target number. 20666 */ 20667 (void) snprintf(ap, MAXPATHLEN, "/devices"); 20668 (void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap)); 20669 (void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d", 20670 SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual)); 20671 20672 err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap); 20673 if (err != 0) { 20674 SATA_LOG_D((sata_hba_inst, CE_WARN, 20675 "sata_gen_sysevent: " 20676 "failed to add DR_AP_ID attr for sysevent")); 20677 nvlist_free(ev_attr_list); 20678 return; 20679 } 20680 20681 /* Generate/log sysevent */ 20682 err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR, 20683 ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP); 20684 if (err != DDI_SUCCESS) { 20685 SATA_LOG_D((sata_hba_inst, CE_WARN, 20686 "sata_gen_sysevent: " 20687 "cannot log sysevent, err code %x\n", err)); 20688 } 20689 20690 nvlist_free(ev_attr_list); 20691 } 20692 20693 20694 20695 20696 /* 20697 * Set DEVI_DEVICE_REMOVED state in the SATA device target node. 20698 */ 20699 static void 20700 sata_set_device_removed(dev_info_t *tdip) 20701 { 20702 ASSERT(tdip != NULL); 20703 20704 ndi_devi_enter(tdip); 20705 mutex_enter(&DEVI(tdip)->devi_lock); 20706 DEVI_SET_DEVICE_REMOVED(tdip); 20707 mutex_exit(&DEVI(tdip)->devi_lock); 20708 ndi_devi_exit(tdip); 20709 } 20710 20711 20712 /* 20713 * Set internal event instructing event daemon to try 20714 * to perform the target node cleanup. 20715 */ 20716 static void 20717 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, 20718 sata_address_t *saddr) 20719 { 20720 if (saddr->qual == SATA_ADDR_CPORT || 20721 saddr->qual == SATA_ADDR_DCPORT) { 20722 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 20723 saddr->cport)->cport_mutex); 20724 SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |= 20725 SATA_EVNT_TARGET_NODE_CLEANUP; 20726 SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 20727 cport_tgtnode_clean = B_FALSE; 20728 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 20729 saddr->cport)->cport_mutex); 20730 } else { 20731 mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst, 20732 saddr->cport, saddr->pmport)->pmport_mutex); 20733 SATA_PMPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport, 20734 saddr->pmport) |= SATA_EVNT_TARGET_NODE_CLEANUP; 20735 SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, saddr->pmport)-> 20736 pmport_tgtnode_clean = B_FALSE; 20737 mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst, 20738 saddr->cport, saddr->pmport)->pmport_mutex); 20739 } 20740 mutex_enter(&sata_hba_inst->satahba_mutex); 20741 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 20742 mutex_exit(&sata_hba_inst->satahba_mutex); 20743 mutex_enter(&sata_mutex); 20744 sata_event_pending |= SATA_EVNT_MAIN; 20745 mutex_exit(&sata_mutex); 20746 } 20747 20748 20749 /* 20750 * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state, 20751 * i.e. check if the target node state indicates that it belongs to a removed 20752 * device. 20753 * 20754 * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state, 20755 * B_FALSE otherwise. 20756 */ 20757 static boolean_t 20758 sata_check_device_removed(dev_info_t *tdip) 20759 { 20760 ASSERT(tdip != NULL); 20761 20762 if (DEVI_IS_DEVICE_REMOVED(tdip)) 20763 return (B_TRUE); 20764 else 20765 return (B_FALSE); 20766 } 20767 20768 20769 /* 20770 * Check for DMA error. Return B_TRUE if error, B_FALSE otherwise. 20771 */ 20772 static boolean_t 20773 sata_check_for_dma_error(dev_info_t *dip, sata_pkt_txlate_t *spx) 20774 { 20775 int fm_capability = ddi_fm_capable(dip); 20776 ddi_fm_error_t de; 20777 20778 if (fm_capability & DDI_FM_DMACHK_CAPABLE) { 20779 if (spx->txlt_buf_dma_handle != NULL) { 20780 ddi_fm_dma_err_get(spx->txlt_buf_dma_handle, &de, 20781 DDI_FME_VERSION); 20782 if (de.fme_status != DDI_SUCCESS) 20783 return (B_TRUE); 20784 } 20785 } 20786 return (B_FALSE); 20787 } 20788 20789 20790 /* ************************ FAULT INJECTTION **************************** */ 20791 20792 #ifdef SATA_INJECT_FAULTS 20793 20794 static uint32_t sata_fault_count = 0; 20795 static uint32_t sata_fault_suspend_count = 0; 20796 20797 /* 20798 * Inject sata pkt fault 20799 * It modifies returned values of the sata packet. 20800 * It returns immediately if: 20801 * pkt fault injection is not enabled (via sata_inject_fault, 20802 * sata_inject_fault_count), or invalid fault is specified (sata_fault_type), 20803 * or pkt does not contain command to be faulted (set in sata_fault_cmd), or 20804 * pkt is not directed to specified fault controller/device 20805 * (sata_fault_ctrl_dev and sata_fault_device). 20806 * If fault controller is not specified, fault injection applies to all 20807 * controllers and devices. 20808 * 20809 * First argument is the pointer to the executed sata packet. 20810 * Second argument is a pointer to a value returned by the HBA tran_start 20811 * function. 20812 * Third argument specifies injected error. Injected sata packet faults 20813 * are the satapkt_reason values. 20814 * SATA_PKT_BUSY -1 Not completed, busy 20815 * SATA_PKT_DEV_ERROR 1 Device reported error 20816 * SATA_PKT_QUEUE_FULL 2 Not accepted, queue full 20817 * SATA_PKT_PORT_ERROR 3 Not completed, port error 20818 * SATA_PKT_CMD_UNSUPPORTED 4 Cmd unsupported 20819 * SATA_PKT_ABORTED 5 Aborted by request 20820 * SATA_PKT_TIMEOUT 6 Operation timeut 20821 * SATA_PKT_RESET 7 Aborted by reset request 20822 * 20823 * Additional global variables affecting the execution: 20824 * 20825 * sata_inject_fault_count variable specifies number of times in row the 20826 * error is injected. Value of -1 specifies permanent fault, ie. every time 20827 * the fault injection point is reached, the fault is injected and a pause 20828 * between fault injection specified by sata_inject_fault_pause_count is 20829 * ignored). Fault injection routine decrements sata_inject_fault_count 20830 * (if greater than zero) until it reaches 0. No fault is injected when 20831 * sata_inject_fault_count is 0 (zero). 20832 * 20833 * sata_inject_fault_pause_count variable specifies number of times a fault 20834 * injection is bypassed (pause between fault injections). 20835 * If set to 0, a fault is injected only a number of times specified by 20836 * sata_inject_fault_count. 20837 * 20838 * The fault counts are static, so for periodic errors they have to be manually 20839 * reset to start repetition sequence from scratch. 20840 * If the original value returned by the HBA tran_start function is not 20841 * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error 20842 * is injected (to avoid masking real problems); 20843 * 20844 * NOTE: In its current incarnation, this function should be invoked only for 20845 * commands executed in SYNCHRONOUS mode. 20846 */ 20847 20848 20849 static void 20850 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault) 20851 { 20852 20853 if (sata_inject_fault != SATA_INJECT_PKT_FAULT) 20854 return; 20855 20856 if (sata_inject_fault_count == 0) 20857 return; 20858 20859 if (fault == 0) 20860 return; 20861 20862 if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg) 20863 return; 20864 20865 if (sata_fault_ctrl != NULL) { 20866 sata_pkt_txlate_t *spx = 20867 (sata_pkt_txlate_t *)spkt->satapkt_framework_private; 20868 20869 if (sata_fault_ctrl != NULL && sata_fault_ctrl != 20870 spx->txlt_sata_hba_inst->satahba_dip) 20871 return; 20872 20873 if (sata_fault_device.satadev_addr.cport != 20874 spkt->satapkt_device.satadev_addr.cport || 20875 sata_fault_device.satadev_addr.pmport != 20876 spkt->satapkt_device.satadev_addr.pmport || 20877 sata_fault_device.satadev_addr.qual != 20878 spkt->satapkt_device.satadev_addr.qual) 20879 return; 20880 } 20881 20882 /* Modify pkt return parameters */ 20883 if (*rval != SATA_TRAN_ACCEPTED || 20884 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 20885 sata_fault_count = 0; 20886 sata_fault_suspend_count = 0; 20887 return; 20888 } 20889 if (sata_fault_count == 0 && sata_fault_suspend_count != 0) { 20890 /* Pause in the injection */ 20891 sata_fault_suspend_count -= 1; 20892 return; 20893 } 20894 20895 if (sata_fault_count == 0 && sata_fault_suspend_count == 0) { 20896 /* 20897 * Init inject fault cycle. If fault count is set to -1, 20898 * it is a permanent fault. 20899 */ 20900 if (sata_inject_fault_count != -1) { 20901 sata_fault_count = sata_inject_fault_count; 20902 sata_fault_suspend_count = 20903 sata_inject_fault_pause_count; 20904 if (sata_fault_suspend_count == 0) 20905 sata_inject_fault_count = 0; 20906 } 20907 } 20908 20909 if (sata_fault_count != 0) 20910 sata_fault_count -= 1; 20911 20912 switch (fault) { 20913 case SATA_PKT_BUSY: 20914 *rval = SATA_TRAN_BUSY; 20915 spkt->satapkt_reason = SATA_PKT_BUSY; 20916 break; 20917 20918 case SATA_PKT_QUEUE_FULL: 20919 *rval = SATA_TRAN_QUEUE_FULL; 20920 spkt->satapkt_reason = SATA_PKT_QUEUE_FULL; 20921 break; 20922 20923 case SATA_PKT_CMD_UNSUPPORTED: 20924 *rval = SATA_TRAN_CMD_UNSUPPORTED; 20925 spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED; 20926 break; 20927 20928 case SATA_PKT_PORT_ERROR: 20929 /* This is "rejected" command */ 20930 *rval = SATA_TRAN_PORT_ERROR; 20931 spkt->satapkt_reason = SATA_PKT_PORT_ERROR; 20932 /* Additional error setup could be done here - port state */ 20933 break; 20934 20935 case SATA_PKT_DEV_ERROR: 20936 spkt->satapkt_reason = SATA_PKT_DEV_ERROR; 20937 /* 20938 * Additional error setup could be done here 20939 */ 20940 break; 20941 20942 case SATA_PKT_ABORTED: 20943 spkt->satapkt_reason = SATA_PKT_ABORTED; 20944 break; 20945 20946 case SATA_PKT_TIMEOUT: 20947 spkt->satapkt_reason = SATA_PKT_TIMEOUT; 20948 /* Additional error setup could be done here */ 20949 break; 20950 20951 case SATA_PKT_RESET: 20952 spkt->satapkt_reason = SATA_PKT_RESET; 20953 /* 20954 * Additional error setup could be done here - device reset 20955 */ 20956 break; 20957 20958 default: 20959 break; 20960 } 20961 } 20962 20963 #endif 20964 20965 /* 20966 * SATA Trace Ring Buffer 20967 * ---------------------- 20968 * 20969 * Overview 20970 * 20971 * The SATA trace ring buffer is a ring buffer created and managed by 20972 * the SATA framework module that can be used by any module or driver 20973 * within the SATA framework to store debug messages. 20974 * 20975 * Ring Buffer Interfaces: 20976 * 20977 * sata_vtrace_debug() <-- Adds debug message to ring buffer 20978 * sata_trace_debug() <-- Wraps varargs into sata_vtrace_debug() 20979 * 20980 * Note that the sata_trace_debug() interface was created to give 20981 * consumers the flexibilty of sending debug messages to ring buffer 20982 * as variable arguments. Consumers can send type va_list debug 20983 * messages directly to sata_vtrace_debug(). The sata_trace_debug() 20984 * and sata_vtrace_debug() relationship is similar to that of 20985 * cmn_err(9F) and vcmn_err(9F). 20986 * 20987 * Below is a diagram of the SATA trace ring buffer interfaces and 20988 * sample consumers: 20989 * 20990 * +---------------------------------+ 20991 * | o o SATA Framework Module | 20992 * | o SATA o +------------------+ +------------------+ 20993 * |o Trace o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1| 20994 * |o R-Buf o |sata_trace_debug |<--+ +------------------+ 20995 * | o o +------------------+ | +------------------+ 20996 * | o o ^ | +--|SATA HBA Driver #2| 20997 * | | | +------------------+ 20998 * | +------------------+ | 20999 * | |SATA Debug Message| | 21000 * | +------------------+ | 21001 * +---------------------------------+ 21002 * 21003 * Supporting Routines: 21004 * 21005 * sata_trace_rbuf_alloc() <-- Initializes ring buffer 21006 * sata_trace_rbuf_free() <-- Destroys ring buffer 21007 * sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer 21008 * sata_trace_dmsg_free() <-- Destroys content of ring buffer 21009 * 21010 * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE. 21011 * The ring buffer size can be adjusted by setting dmsg_ring_size in 21012 * /etc/system to desired size in unit of bytes. 21013 * 21014 * The individual debug message size in the ring buffer is restricted 21015 * to DMSG_BUF_SIZE. 21016 */ 21017 void 21018 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap) 21019 { 21020 sata_trace_dmsg_t *dmsg; 21021 21022 if (sata_debug_rbuf == NULL) { 21023 return; 21024 } 21025 21026 /* 21027 * If max size of ring buffer is smaller than size 21028 * required for one debug message then just return 21029 * since we have no room for the debug message. 21030 */ 21031 if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) { 21032 return; 21033 } 21034 21035 mutex_enter(&sata_debug_rbuf->lock); 21036 21037 /* alloc or reuse on ring buffer */ 21038 dmsg = sata_trace_dmsg_alloc(); 21039 21040 if (dmsg == NULL) { 21041 /* resource allocation failed */ 21042 mutex_exit(&sata_debug_rbuf->lock); 21043 return; 21044 } 21045 21046 dmsg->dip = dip; 21047 gethrestime(&dmsg->timestamp); 21048 21049 (void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap); 21050 21051 mutex_exit(&sata_debug_rbuf->lock); 21052 } 21053 21054 void 21055 sata_trace_debug(dev_info_t *dip, const char *fmt, ...) 21056 { 21057 va_list ap; 21058 21059 va_start(ap, fmt); 21060 sata_vtrace_debug(dip, fmt, ap); 21061 va_end(ap); 21062 } 21063 21064 /* 21065 * This routine is used to manage debug messages 21066 * on ring buffer. 21067 */ 21068 static sata_trace_dmsg_t * 21069 sata_trace_dmsg_alloc(void) 21070 { 21071 sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp; 21072 21073 if (sata_debug_rbuf->looped == TRUE) { 21074 sata_debug_rbuf->dmsgp = dmsg->next; 21075 return (sata_debug_rbuf->dmsgp); 21076 } 21077 21078 /* 21079 * If we're looping for the first time, 21080 * connect the ring. 21081 */ 21082 if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) > 21083 sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) { 21084 dmsg->next = sata_debug_rbuf->dmsgh; 21085 sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh; 21086 sata_debug_rbuf->looped = TRUE; 21087 return (sata_debug_rbuf->dmsgp); 21088 } 21089 21090 /* If we've gotten this far then memory allocation is needed */ 21091 dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP); 21092 if (dmsg_alloc == NULL) { 21093 sata_debug_rbuf->allocfailed++; 21094 return (dmsg_alloc); 21095 } else { 21096 sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t); 21097 } 21098 21099 if (sata_debug_rbuf->dmsgp != NULL) { 21100 dmsg->next = dmsg_alloc; 21101 sata_debug_rbuf->dmsgp = dmsg->next; 21102 return (sata_debug_rbuf->dmsgp); 21103 } else { 21104 /* 21105 * We should only be here if we're initializing 21106 * the ring buffer. 21107 */ 21108 if (sata_debug_rbuf->dmsgh == NULL) { 21109 sata_debug_rbuf->dmsgh = dmsg_alloc; 21110 } else { 21111 /* Something is wrong */ 21112 kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t)); 21113 return (NULL); 21114 } 21115 21116 sata_debug_rbuf->dmsgp = dmsg_alloc; 21117 return (sata_debug_rbuf->dmsgp); 21118 } 21119 } 21120 21121 21122 /* 21123 * Free all messages on debug ring buffer. 21124 */ 21125 static void 21126 sata_trace_dmsg_free(void) 21127 { 21128 sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh; 21129 21130 while (dmsg != NULL) { 21131 dmsg_next = dmsg->next; 21132 kmem_free(dmsg, sizeof (sata_trace_dmsg_t)); 21133 21134 /* 21135 * If we've looped around the ring than we're done. 21136 */ 21137 if (dmsg_next == sata_debug_rbuf->dmsgh) { 21138 break; 21139 } else { 21140 dmsg = dmsg_next; 21141 } 21142 } 21143 } 21144 21145 21146 /* 21147 * This function can block 21148 */ 21149 static void 21150 sata_trace_rbuf_alloc(void) 21151 { 21152 sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP); 21153 21154 mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL); 21155 21156 if (dmsg_ring_size > 0) { 21157 sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size; 21158 } 21159 } 21160 21161 21162 static void 21163 sata_trace_rbuf_free(void) 21164 { 21165 sata_trace_dmsg_free(); 21166 mutex_destroy(&sata_debug_rbuf->lock); 21167 kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t)); 21168 } 21169 21170 #ifndef SATA_DEBUG 21171 /* 21172 * If SATA_DEBUG is not defined then this routine is called instead 21173 * of sata_log() via the SATA_LOG_D macro. 21174 */ 21175 static void 21176 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level __unused, 21177 const char *fmt, ...) 21178 { 21179 dev_info_t *dip = NULL; 21180 va_list ap; 21181 21182 if (sata_hba_inst != NULL) { 21183 dip = SATA_DIP(sata_hba_inst); 21184 } 21185 21186 va_start(ap, fmt); 21187 sata_vtrace_debug(dip, fmt, ap); 21188 va_end(ap); 21189 } 21190 #endif /* SATA_DEBUG */ 21191