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 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 28 /* 29 * SATA Framework 30 * Generic SATA Host Adapter Implementation 31 */ 32 33 #include <sys/conf.h> 34 #include <sys/file.h> 35 #include <sys/ddi.h> 36 #include <sys/sunddi.h> 37 #include <sys/modctl.h> 38 #include <sys/cmn_err.h> 39 #include <sys/errno.h> 40 #include <sys/thread.h> 41 #include <sys/kstat.h> 42 #include <sys/note.h> 43 #include <sys/sysevent.h> 44 #include <sys/sysevent/eventdefs.h> 45 #include <sys/sysevent/dr.h> 46 #include <sys/taskq.h> 47 #include <sys/disp.h> 48 49 #include <sys/sata/impl/sata.h> 50 #include <sys/sata/sata_hba.h> 51 #include <sys/sata/sata_defs.h> 52 #include <sys/sata/sata_cfgadm.h> 53 54 /* Debug flags - defined in sata.h */ 55 int sata_debug_flags = 0; 56 int sata_msg = 0; 57 58 /* 59 * Flags enabling selected SATA HBA framework functionality 60 */ 61 #define SATA_ENABLE_QUEUING 1 62 #define SATA_ENABLE_NCQ 2 63 #define SATA_ENABLE_PROCESS_EVENTS 4 64 int sata_func_enable = 65 SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ; 66 67 /* 68 * Global variable setting default maximum queue depth (NCQ or TCQ) 69 * Note:minimum queue depth is 1 70 */ 71 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */ 72 73 /* 74 * Currently used default NCQ/TCQ queue depth. It is set-up during the driver 75 * initialization, using value from sata_max_queue_depth 76 * It is adjusted to minimum supported by the controller and by the device, 77 * if queueing is enabled. 78 */ 79 static int sata_current_max_qdepth; 80 81 /* 82 * Global variable determining the default behavior after device hotpluggin. 83 * If non-zero, the hotplugged device is onlined (if possible) without explicit 84 * IOCTL request (AP_CONFIGURE). 85 * If zero, hotplugged device is identified, but not onlined. 86 * Enabling (AP_CONNECT) device port with an attached device does not result 87 * in device onlining regardless of the flag setting 88 */ 89 int sata_auto_online = 0; 90 91 #ifdef SATA_DEBUG 92 93 #define SATA_LOG_D(args) sata_log args 94 uint64_t mbuf_count = 0; 95 uint64_t mbuffail_count = 0; 96 97 sata_atapi_cmd_t sata_atapi_trace[64]; 98 uint32_t sata_atapi_trace_index = 0; 99 int sata_atapi_trace_save = 1; 100 static void sata_save_atapi_trace(sata_pkt_txlate_t *, int); 101 #define SATAATAPITRACE(spx, count) if (sata_atapi_trace_save) \ 102 sata_save_atapi_trace(spx, count); 103 104 #else 105 #define SATA_LOG_D(arg) 106 #define SATAATAPITRACE(spx, count) 107 #endif 108 109 #if 0 110 static void 111 sata_test_atapi_packet_command(sata_hba_inst_t *, int); 112 #endif 113 114 #ifdef SATA_INJECT_FAULTS 115 116 #define SATA_INJECT_PKT_FAULT 1 117 uint32_t sata_inject_fault = 0; 118 119 uint32_t sata_inject_fault_count = 0; 120 uint32_t sata_inject_fault_pause_count = 0; 121 uint32_t sata_fault_type = 0; 122 uint32_t sata_fault_cmd = 0; 123 dev_info_t *sata_fault_ctrl = NULL; 124 sata_device_t sata_fault_device; 125 126 static void sata_inject_pkt_fault(sata_pkt_t *, int *, int); 127 128 #endif 129 130 #define LEGACY_HWID_LEN 64 /* Model (40) + Serial (20) + pad */ 131 132 static char sata_rev_tag[] = {"1.39"}; 133 134 /* 135 * SATA cb_ops functions 136 */ 137 static int sata_hba_open(dev_t *, int, int, cred_t *); 138 static int sata_hba_close(dev_t, int, int, cred_t *); 139 static int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 140 141 /* 142 * SCSA required entry points 143 */ 144 static int sata_scsi_tgt_init(dev_info_t *, dev_info_t *, 145 scsi_hba_tran_t *, struct scsi_device *); 146 static int sata_scsi_tgt_probe(struct scsi_device *, 147 int (*callback)(void)); 148 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *, 149 scsi_hba_tran_t *, struct scsi_device *); 150 static int sata_scsi_start(struct scsi_address *, struct scsi_pkt *); 151 static int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *); 152 static int sata_scsi_reset(struct scsi_address *, int); 153 static int sata_scsi_getcap(struct scsi_address *, char *, int); 154 static int sata_scsi_setcap(struct scsi_address *, char *, int, int); 155 static struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *, 156 struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t), 157 caddr_t); 158 static void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *); 159 static void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *); 160 static void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *); 161 162 /* 163 * SATA HBA interface functions are defined in sata_hba.h header file 164 */ 165 166 /* Event processing functions */ 167 static void sata_event_daemon(void *); 168 static void sata_event_thread_control(int); 169 static void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst); 170 static void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *); 171 static void sata_process_port_failed_event(sata_hba_inst_t *, 172 sata_address_t *); 173 static void sata_process_port_link_events(sata_hba_inst_t *, 174 sata_address_t *); 175 static void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *); 176 static void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *); 177 static void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *); 178 static void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *); 179 static void sata_process_target_node_cleanup(sata_hba_inst_t *, 180 sata_address_t *); 181 static void sata_process_device_autoonline(sata_hba_inst_t *, 182 sata_address_t *saddr); 183 184 /* 185 * Local translation functions 186 */ 187 static int sata_txlt_inquiry(sata_pkt_txlate_t *); 188 static int sata_txlt_test_unit_ready(sata_pkt_txlate_t *); 189 static int sata_txlt_start_stop_unit(sata_pkt_txlate_t *); 190 static int sata_txlt_read_capacity(sata_pkt_txlate_t *); 191 static int sata_txlt_request_sense(sata_pkt_txlate_t *); 192 static int sata_txlt_read(sata_pkt_txlate_t *); 193 static int sata_txlt_write(sata_pkt_txlate_t *); 194 static int sata_txlt_log_sense(sata_pkt_txlate_t *); 195 static int sata_txlt_log_select(sata_pkt_txlate_t *); 196 static int sata_txlt_mode_sense(sata_pkt_txlate_t *); 197 static int sata_txlt_mode_select(sata_pkt_txlate_t *); 198 static int sata_txlt_synchronize_cache(sata_pkt_txlate_t *); 199 static int sata_txlt_write_buffer(sata_pkt_txlate_t *); 200 static int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *); 201 202 static int sata_hba_start(sata_pkt_txlate_t *, int *); 203 static int sata_txlt_invalid_command(sata_pkt_txlate_t *); 204 static int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *); 205 static void sata_txlt_rw_completion(sata_pkt_t *); 206 static void sata_txlt_nodata_cmd_completion(sata_pkt_t *); 207 static void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *); 208 static int sata_emul_rw_completion(sata_pkt_txlate_t *); 209 static struct scsi_extended_sense *sata_immediate_error_response( 210 sata_pkt_txlate_t *, int); 211 static struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *); 212 213 static int sata_txlt_atapi(sata_pkt_txlate_t *); 214 static void sata_txlt_atapi_completion(sata_pkt_t *); 215 216 /* 217 * Local functions for ioctl 218 */ 219 static int32_t sata_get_port_num(sata_hba_inst_t *, struct devctl_iocdata *); 220 static void sata_cfgadm_state(sata_hba_inst_t *, int32_t, 221 devctl_ap_state_t *); 222 static dev_info_t *sata_get_target_dip(dev_info_t *, int32_t); 223 static dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *); 224 static dev_info_t *sata_devt_to_devinfo(dev_t); 225 static int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *); 226 static int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *); 227 static int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *); 228 static int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *); 229 static int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *); 230 static int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *); 231 static int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *); 232 static int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *); 233 static int sata_ioctl_reset_all(sata_hba_inst_t *); 234 static int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *); 235 static int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *, 236 sata_ioctl_data_t *, int mode); 237 static int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *, 238 sata_ioctl_data_t *, int mode); 239 static int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *, 240 sata_ioctl_data_t *, int mode); 241 static int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *, 242 sata_ioctl_data_t *, int mode); 243 static int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *, 244 sata_device_t *, sata_ioctl_data_t *, int mode); 245 246 /* 247 * Local functions 248 */ 249 static void sata_remove_hba_instance(dev_info_t *); 250 static int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *); 251 static void sata_probe_ports(sata_hba_inst_t *); 252 static int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int); 253 static int sata_add_device(dev_info_t *, sata_hba_inst_t *, int cport, 254 int pmport); 255 static dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *, 256 sata_address_t *); 257 static int sata_validate_scsi_address(sata_hba_inst_t *, 258 struct scsi_address *, sata_device_t *); 259 static int sata_validate_sata_address(sata_hba_inst_t *, int, int, int); 260 static sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t)); 261 static void sata_pkt_free(sata_pkt_txlate_t *); 262 static int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t), 263 caddr_t, ddi_dma_attr_t *); 264 static void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *); 265 static int sata_probe_device(sata_hba_inst_t *, sata_device_t *); 266 static sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *, 267 sata_device_t *); 268 static int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *); 269 static void sata_reidentify_device(sata_pkt_txlate_t *); 270 static struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int); 271 static void sata_free_local_buffer(sata_pkt_txlate_t *); 272 static uint64_t sata_check_capacity(sata_drive_info_t *); 273 void sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *, 274 ddi_dma_attr_t *); 275 static int sata_fetch_device_identify_data(sata_hba_inst_t *, 276 sata_drive_info_t *); 277 static void sata_update_port_info(sata_hba_inst_t *, sata_device_t *); 278 static void sata_update_port_scr(sata_port_scr_t *, sata_device_t *); 279 static int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *); 280 static int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int); 281 static int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int); 282 static int sata_set_drive_features(sata_hba_inst_t *, 283 sata_drive_info_t *, int flag); 284 static void sata_init_write_cache_mode(sata_drive_info_t *sdinfo); 285 static int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *); 286 static void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *, 287 uint8_t *); 288 static int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *, 289 struct scsi_inquiry *); 290 static int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *); 291 static int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *); 292 static int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *); 293 static int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *); 294 static int sata_mode_select_page_8(sata_pkt_txlate_t *, 295 struct mode_cache_scsi3 *, int, int *, int *, int *); 296 static int sata_mode_select_page_1c(sata_pkt_txlate_t *, 297 struct mode_info_excpt_page *, int, int *, int *, int *); 298 static int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *); 299 static int sata_mode_select_page_30(sata_pkt_txlate_t *, 300 struct mode_acoustic_management *, int, int *, int *, int *); 301 302 static int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *); 303 static int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *, 304 sata_hba_inst_t *); 305 static int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *, 306 sata_hba_inst_t *); 307 static int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *, 308 sata_hba_inst_t *); 309 static void sata_save_drive_settings(sata_drive_info_t *); 310 static void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *); 311 static void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...); 312 static int sata_fetch_smart_return_status(sata_hba_inst_t *, 313 sata_drive_info_t *); 314 static int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *, 315 struct smart_data *); 316 static int sata_smart_selftest_log(sata_hba_inst_t *, 317 sata_drive_info_t *, 318 struct smart_selftest_log *); 319 static int sata_ext_smart_selftest_read_log(sata_hba_inst_t *, 320 sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t); 321 static int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *, 322 uint8_t *, uint8_t, uint8_t); 323 static int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *, 324 struct read_log_ext_directory *); 325 static void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int); 326 static void sata_xlate_errors(sata_pkt_txlate_t *); 327 static void sata_decode_device_error(sata_pkt_txlate_t *, 328 struct scsi_extended_sense *); 329 static void sata_set_device_removed(dev_info_t *); 330 static boolean_t sata_check_device_removed(dev_info_t *); 331 static void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *); 332 static int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *, 333 sata_drive_info_t *); 334 static int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *, 335 sata_drive_info_t *); 336 static void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *); 337 static void sata_fixed_sense_data_preset(struct scsi_extended_sense *); 338 static void sata_target_devid_register(dev_info_t *, sata_drive_info_t *); 339 static int sata_check_modser(char *, int); 340 341 342 343 /* 344 * SATA Framework will ignore SATA HBA driver cb_ops structure and 345 * register following one with SCSA framework. 346 * Open & close are provided, so scsi framework will not use its own 347 */ 348 static struct cb_ops sata_cb_ops = { 349 sata_hba_open, /* open */ 350 sata_hba_close, /* close */ 351 nodev, /* strategy */ 352 nodev, /* print */ 353 nodev, /* dump */ 354 nodev, /* read */ 355 nodev, /* write */ 356 sata_hba_ioctl, /* ioctl */ 357 nodev, /* devmap */ 358 nodev, /* mmap */ 359 nodev, /* segmap */ 360 nochpoll, /* chpoll */ 361 ddi_prop_op, /* cb_prop_op */ 362 0, /* streamtab */ 363 D_NEW | D_MP, /* cb_flag */ 364 CB_REV, /* rev */ 365 nodev, /* aread */ 366 nodev /* awrite */ 367 }; 368 369 370 extern struct mod_ops mod_miscops; 371 extern uchar_t scsi_cdb_size[]; 372 373 static struct modlmisc modlmisc = { 374 &mod_miscops, /* Type of module */ 375 "SATA Module" /* module name */ 376 }; 377 378 379 static struct modlinkage modlinkage = { 380 MODREV_1, 381 (void *)&modlmisc, 382 NULL 383 }; 384 385 /* 386 * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero, 387 * i.e. when scsi_pkt has not timeout specified. 388 */ 389 static int sata_default_pkt_time = 60; /* 60 seconds */ 390 391 /* 392 * Intermediate buffer device access attributes - they are required, 393 * but not necessarily used. 394 */ 395 static ddi_device_acc_attr_t sata_acc_attr = { 396 DDI_DEVICE_ATTR_V0, 397 DDI_STRUCTURE_LE_ACC, 398 DDI_STRICTORDER_ACC 399 }; 400 401 402 /* 403 * Mutexes protecting structures in multithreaded operations. 404 * Because events are relatively rare, a single global mutex protecting 405 * data structures should be sufficient. To increase performance, add 406 * separate mutex per each sata port and use global mutex only to protect 407 * common data structures. 408 */ 409 static kmutex_t sata_mutex; /* protects sata_hba_list */ 410 static kmutex_t sata_log_mutex; /* protects log */ 411 412 static char sata_log_buf[256]; 413 414 /* Default write cache setting for SATA hard disks */ 415 int sata_write_cache = 1; /* enabled */ 416 417 /* Default write cache setting for SATA ATAPI CD/DVD */ 418 int sata_atapicdvd_write_cache = 1; /* enabled */ 419 420 /* 421 * Linked list of HBA instances 422 */ 423 static sata_hba_inst_t *sata_hba_list = NULL; 424 static sata_hba_inst_t *sata_hba_list_tail = NULL; 425 /* 426 * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran 427 * structure and in sata soft state. 428 */ 429 430 /* 431 * Event daemon related variables 432 */ 433 static kmutex_t sata_event_mutex; 434 static kcondvar_t sata_event_cv; 435 static kthread_t *sata_event_thread = NULL; 436 static int sata_event_thread_terminate = 0; 437 static int sata_event_pending = 0; 438 static int sata_event_thread_active = 0; 439 extern pri_t minclsyspri; 440 441 /* 442 * NCQ error recovery command 443 */ 444 static const sata_cmd_t sata_rle_cmd = { 445 SATA_CMD_REV, 446 NULL, 447 { 448 SATA_DIR_READ 449 }, 450 ATA_ADDR_LBA48, 451 0, 452 0, 453 0, 454 0, 455 0, 456 1, 457 READ_LOG_EXT_NCQ_ERROR_RECOVERY, 458 0, 459 0, 460 0, 461 SATAC_READ_LOG_EXT, 462 0, 463 0, 464 0, 465 }; 466 467 /* 468 * ATAPI error recovery CDB 469 */ 470 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = { 471 SCMD_REQUEST_SENSE, 472 0, /* Only fixed RQ format is supported */ 473 0, 474 0, 475 SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */ 476 0 477 }; 478 479 480 /* Warlock directives */ 481 482 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran)) 483 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device)) 484 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops)) 485 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense)) 486 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status)) 487 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr)) 488 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t)) 489 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state)) 490 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state)) 491 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list)) 492 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list)) 493 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next)) 494 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev)) 495 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \ 496 sata_hba_inst::satahba_scsi_tran)) 497 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran)) 498 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip)) 499 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached)) 500 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port)) 501 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex, 502 sata_hba_inst::satahba_event_flags)) 503 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 504 sata_cport_info::cport_devp)) 505 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp)) 506 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr)) 507 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 508 sata_cport_info::cport_dev_type)) 509 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type)) 510 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 511 sata_cport_info::cport_state)) 512 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state)) 513 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \ 514 sata_pmport_info::pmport_state)) 515 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state)) 516 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type)) 517 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive)) 518 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port)) 519 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports)) 520 #ifdef SATA_DEBUG 521 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count)) 522 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count)) 523 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace)) 524 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index)) 525 #endif 526 527 /* End of warlock directives */ 528 529 /* ************** loadable module configuration functions ************** */ 530 531 int 532 _init() 533 { 534 int rval; 535 536 mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL); 537 mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL); 538 mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL); 539 cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL); 540 if ((rval = mod_install(&modlinkage)) != 0) { 541 #ifdef SATA_DEBUG 542 cmn_err(CE_WARN, "sata: _init: mod_install failed\n"); 543 #endif 544 mutex_destroy(&sata_log_mutex); 545 cv_destroy(&sata_event_cv); 546 mutex_destroy(&sata_event_mutex); 547 mutex_destroy(&sata_mutex); 548 } 549 return (rval); 550 } 551 552 int 553 _fini() 554 { 555 int rval; 556 557 if ((rval = mod_remove(&modlinkage)) != 0) 558 return (rval); 559 560 mutex_destroy(&sata_log_mutex); 561 cv_destroy(&sata_event_cv); 562 mutex_destroy(&sata_event_mutex); 563 mutex_destroy(&sata_mutex); 564 return (rval); 565 } 566 567 int 568 _info(struct modinfo *modinfop) 569 { 570 return (mod_info(&modlinkage, modinfop)); 571 } 572 573 574 575 /* ********************* SATA HBA entry points ********************* */ 576 577 578 /* 579 * Called by SATA HBA from _init(). 580 * Registers HBA driver instance/sata framework pair with scsi framework, by 581 * calling scsi_hba_init(). 582 * 583 * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used 584 * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver 585 * cb_ops pointer in SATA HBA driver dev_ops structure. 586 * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors. 587 * 588 * Return status of the scsi_hba_init() is returned to a calling SATA HBA 589 * driver. 590 */ 591 int 592 sata_hba_init(struct modlinkage *modlp) 593 { 594 int rval; 595 struct dev_ops *hba_ops; 596 597 SATADBG1(SATA_DBG_HBA_IF, NULL, 598 "sata_hba_init: name %s \n", 599 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo); 600 /* 601 * Fill-up cb_ops and dev_ops when necessary 602 */ 603 hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops; 604 /* 605 * Provide pointer to SATA dev_ops 606 */ 607 hba_ops->devo_cb_ops = &sata_cb_ops; 608 609 /* 610 * Register SATA HBA with SCSI framework 611 */ 612 if ((rval = scsi_hba_init(modlp)) != 0) { 613 SATADBG1(SATA_DBG_HBA_IF, NULL, 614 "sata_hba_init: scsi hba init failed\n", NULL); 615 return (rval); 616 } 617 618 return (0); 619 } 620 621 622 /* HBA attach stages */ 623 #define HBA_ATTACH_STAGE_SATA_HBA_INST 1 624 #define HBA_ATTACH_STAGE_SCSI_ATTACHED 2 625 #define HBA_ATTACH_STAGE_SETUP 4 626 #define HBA_ATTACH_STAGE_LINKED 8 627 628 629 /* 630 * 631 * Called from SATA HBA driver's attach routine to attach an instance of 632 * the HBA. 633 * 634 * For DDI_ATTACH command: 635 * sata_hba_inst structure is allocated here and initialized with pointers to 636 * SATA framework implementation of required scsi tran functions. 637 * The scsi_tran's tran_hba_private field is used by SATA Framework to point 638 * to the soft structure (sata_hba_inst) allocated by SATA framework for 639 * SATA HBA instance related data. 640 * The scsi_tran's tran_hba_private field is used by SATA framework to 641 * store a pointer to per-HBA-instance of sata_hba_inst structure. 642 * The sata_hba_inst structure is cross-linked to scsi tran structure. 643 * Among other info, a pointer to sata_hba_tran structure is stored in 644 * sata_hba_inst. The sata_hba_inst structures for different HBA instances are 645 * linked together into the list, pointed to by sata_hba_list. 646 * On the first HBA instance attach the sata event thread is initialized. 647 * Attachment points are created for all SATA ports of the HBA being attached. 648 * All HBA instance's SATA ports are probed and type of plugged devices is 649 * determined. For each device of a supported type, a target node is created. 650 * 651 * DDI_SUCCESS is returned when attachment process is successful, 652 * DDI_FAILURE is returned otherwise. 653 * 654 * For DDI_RESUME command: 655 * Not implemented at this time (postponed until phase 2 of the development). 656 */ 657 int 658 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran, 659 ddi_attach_cmd_t cmd) 660 { 661 sata_hba_inst_t *sata_hba_inst; 662 scsi_hba_tran_t *scsi_tran = NULL; 663 int hba_attach_state = 0; 664 char taskq_name[MAXPATHLEN]; 665 666 SATADBG3(SATA_DBG_HBA_IF, NULL, 667 "sata_hba_attach: node %s (%s%d)\n", 668 ddi_node_name(dip), ddi_driver_name(dip), 669 ddi_get_instance(dip)); 670 671 if (cmd == DDI_RESUME) { 672 /* 673 * Postponed until phase 2 of the development 674 */ 675 return (DDI_FAILURE); 676 } 677 678 if (cmd != DDI_ATTACH) { 679 return (DDI_FAILURE); 680 } 681 682 /* cmd == DDI_ATTACH */ 683 684 if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) { 685 SATA_LOG_D((NULL, CE_WARN, 686 "sata_hba_attach: invalid sata_hba_tran")); 687 return (DDI_FAILURE); 688 } 689 /* 690 * Allocate and initialize SCSI tran structure. 691 * SATA copy of tran_bus_config is provided to create port nodes. 692 */ 693 scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP); 694 if (scsi_tran == NULL) 695 return (DDI_FAILURE); 696 /* 697 * Allocate soft structure for SATA HBA instance. 698 * There is a separate softstate for each HBA instance. 699 */ 700 sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP); 701 ASSERT(sata_hba_inst != NULL); /* this should not fail */ 702 mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL); 703 hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST; 704 705 /* 706 * scsi_trans's tran_hba_private is used by SATA Framework to point to 707 * soft structure allocated by SATA framework for 708 * SATA HBA instance related data. 709 */ 710 scsi_tran->tran_hba_private = sata_hba_inst; 711 scsi_tran->tran_tgt_private = NULL; 712 713 scsi_tran->tran_tgt_init = sata_scsi_tgt_init; 714 scsi_tran->tran_tgt_probe = sata_scsi_tgt_probe; 715 scsi_tran->tran_tgt_free = sata_scsi_tgt_free; 716 717 scsi_tran->tran_start = sata_scsi_start; 718 scsi_tran->tran_reset = sata_scsi_reset; 719 scsi_tran->tran_abort = sata_scsi_abort; 720 scsi_tran->tran_getcap = sata_scsi_getcap; 721 scsi_tran->tran_setcap = sata_scsi_setcap; 722 scsi_tran->tran_init_pkt = sata_scsi_init_pkt; 723 scsi_tran->tran_destroy_pkt = sata_scsi_destroy_pkt; 724 725 scsi_tran->tran_dmafree = sata_scsi_dmafree; 726 scsi_tran->tran_sync_pkt = sata_scsi_sync_pkt; 727 728 scsi_tran->tran_reset_notify = NULL; 729 scsi_tran->tran_get_bus_addr = NULL; 730 scsi_tran->tran_quiesce = NULL; 731 scsi_tran->tran_unquiesce = NULL; 732 scsi_tran->tran_bus_reset = NULL; 733 734 if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr, 735 scsi_tran, 0) != DDI_SUCCESS) { 736 #ifdef SATA_DEBUG 737 cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed", 738 ddi_driver_name(dip), ddi_get_instance(dip)); 739 #endif 740 goto fail; 741 } 742 hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED; 743 744 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) { 745 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, 746 "sata", 1) != DDI_PROP_SUCCESS) { 747 SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: " 748 "failed to create hba sata prop")); 749 goto fail; 750 } 751 } 752 753 /* 754 * Save pointers in hba instance soft state. 755 */ 756 sata_hba_inst->satahba_scsi_tran = scsi_tran; 757 sata_hba_inst->satahba_tran = sata_tran; 758 sata_hba_inst->satahba_dip = dip; 759 760 /* 761 * Create a task queue to handle emulated commands completion 762 * Use node name, dash, instance number as the queue name. 763 */ 764 taskq_name[0] = '\0'; 765 (void) strlcat(taskq_name, DEVI(dip)->devi_node_name, 766 sizeof (taskq_name)); 767 (void) snprintf(taskq_name + strlen(taskq_name), 768 sizeof (taskq_name) - strlen(taskq_name), 769 "-%d", DEVI(dip)->devi_instance); 770 sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1, 771 minclsyspri, 1, sata_tran->sata_tran_hba_num_cports, 772 TASKQ_DYNAMIC); 773 774 hba_attach_state |= HBA_ATTACH_STAGE_SETUP; 775 776 /* 777 * Create events thread if not created yet. 778 */ 779 sata_event_thread_control(1); 780 781 /* 782 * Link this hba instance into the list. 783 */ 784 mutex_enter(&sata_mutex); 785 786 if (sata_hba_list == NULL) { 787 /* 788 * The first instance of HBA is attached. 789 * Set current/active default maximum NCQ/TCQ queue depth for 790 * all SATA devices. It is done here and now, to eliminate the 791 * possibility of the dynamic, programatic modification of the 792 * queue depth via global (and public) sata_max_queue_depth 793 * variable (this would require special handling in HBA drivers) 794 */ 795 sata_current_max_qdepth = sata_max_queue_depth; 796 if (sata_current_max_qdepth > 32) 797 sata_current_max_qdepth = 32; 798 else if (sata_current_max_qdepth < 1) 799 sata_current_max_qdepth = 1; 800 } 801 802 sata_hba_inst->satahba_next = NULL; 803 sata_hba_inst->satahba_prev = sata_hba_list_tail; 804 if (sata_hba_list == NULL) { 805 sata_hba_list = sata_hba_inst; 806 } 807 if (sata_hba_list_tail != NULL) { 808 sata_hba_list_tail->satahba_next = sata_hba_inst; 809 } 810 sata_hba_list_tail = sata_hba_inst; 811 mutex_exit(&sata_mutex); 812 hba_attach_state |= HBA_ATTACH_STAGE_LINKED; 813 814 /* 815 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl 816 * SATA HBA driver should not use its own open/close entry points. 817 * 818 * Make sure that instance number doesn't overflow 819 * when forming minor numbers. 820 */ 821 ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT)); 822 if (ddi_create_minor_node(dip, "devctl", S_IFCHR, 823 INST2DEVCTL(ddi_get_instance(dip)), 824 DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) { 825 #ifdef SATA_DEBUG 826 cmn_err(CE_WARN, "sata_hba_attach: " 827 "cannot create devctl minor node"); 828 #endif 829 goto fail; 830 } 831 832 833 /* 834 * Set-up kstats here, if necessary. 835 * (postponed until future phase of the development). 836 */ 837 838 /* 839 * Indicate that HBA is attached. This will enable events processing 840 * for this HBA. 841 */ 842 sata_hba_inst->satahba_attached = 1; 843 /* 844 * Probe controller ports. This operation will describe a current 845 * controller/port/multipliers/device configuration and will create 846 * attachment points. 847 * We may end-up with just a controller with no devices attached. 848 * For the ports with a supported device attached, device target nodes 849 * are created and devices are initialized. 850 */ 851 sata_probe_ports(sata_hba_inst); 852 853 return (DDI_SUCCESS); 854 855 fail: 856 if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) { 857 (void) sata_remove_hba_instance(dip); 858 if (sata_hba_list == NULL) 859 sata_event_thread_control(0); 860 } 861 862 if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) { 863 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata"); 864 taskq_destroy(sata_hba_inst->satahba_taskq); 865 } 866 867 if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED) 868 (void) scsi_hba_detach(dip); 869 870 if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) { 871 mutex_destroy(&sata_hba_inst->satahba_mutex); 872 kmem_free((void *)sata_hba_inst, 873 sizeof (struct sata_hba_inst)); 874 scsi_hba_tran_free(scsi_tran); 875 } 876 877 sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed", 878 ddi_driver_name(dip), ddi_get_instance(dip)); 879 880 return (DDI_FAILURE); 881 } 882 883 884 /* 885 * Called by SATA HBA from to detach an instance of the driver. 886 * 887 * For DDI_DETACH command: 888 * Free local structures allocated for SATA HBA instance during 889 * sata_hba_attach processing. 890 * 891 * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise. 892 * 893 * For DDI_SUSPEND command: 894 * Not implemented at this time (postponed until phase 2 of the development) 895 * Returnd DDI_SUCCESS. 896 * 897 * When the last HBA instance is detached, the event daemon is terminated. 898 * 899 * NOTE: cport support only, no port multiplier support. 900 */ 901 int 902 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 903 { 904 dev_info_t *tdip; 905 sata_hba_inst_t *sata_hba_inst; 906 scsi_hba_tran_t *scsi_hba_tran; 907 sata_cport_info_t *cportinfo; 908 sata_drive_info_t *sdinfo; 909 int ncport; 910 911 SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n", 912 ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip)); 913 914 switch (cmd) { 915 case DDI_DETACH: 916 917 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 918 return (DDI_FAILURE); 919 920 sata_hba_inst = scsi_hba_tran->tran_hba_private; 921 if (sata_hba_inst == NULL) 922 return (DDI_FAILURE); 923 924 if (scsi_hba_detach(dip) == DDI_FAILURE) { 925 sata_hba_inst->satahba_attached = 1; 926 return (DDI_FAILURE); 927 } 928 929 /* 930 * Free all target nodes - at this point 931 * devices should be at least offlined 932 * otherwise scsi_hba_detach() should not be called. 933 */ 934 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); 935 ncport++) { 936 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 937 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 938 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 939 if (sdinfo != NULL) { 940 tdip = sata_get_target_dip(dip, 941 ncport); 942 if (tdip != NULL) { 943 if (ndi_devi_offline(tdip, 944 NDI_DEVI_REMOVE) != 945 NDI_SUCCESS) { 946 SATA_LOG_D(( 947 sata_hba_inst, 948 CE_WARN, 949 "sata_hba_detach: " 950 "Target node not " 951 "removed !")); 952 return (DDI_FAILURE); 953 } 954 } 955 } 956 } 957 } 958 /* 959 * Disable sata event daemon processing for this HBA 960 */ 961 sata_hba_inst->satahba_attached = 0; 962 963 /* 964 * Remove event daemon thread, if it is last HBA instance. 965 */ 966 967 mutex_enter(&sata_mutex); 968 if (sata_hba_list->satahba_next == NULL) { 969 mutex_exit(&sata_mutex); 970 sata_event_thread_control(0); 971 mutex_enter(&sata_mutex); 972 } 973 mutex_exit(&sata_mutex); 974 975 /* Remove this HBA instance from the HBA list */ 976 sata_remove_hba_instance(dip); 977 978 /* 979 * At this point there should be no target nodes attached. 980 * Detach and destroy device and port info structures. 981 */ 982 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); 983 ncport++) { 984 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 985 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 986 sdinfo = 987 cportinfo->cport_devp.cport_sata_drive; 988 if (sdinfo != NULL) { 989 /* Release device structure */ 990 kmem_free(sdinfo, 991 sizeof (sata_drive_info_t)); 992 } 993 /* Release cport info */ 994 mutex_destroy(&cportinfo->cport_mutex); 995 kmem_free(cportinfo, 996 sizeof (sata_cport_info_t)); 997 } 998 } 999 1000 scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran); 1001 1002 (void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata"); 1003 1004 taskq_destroy(sata_hba_inst->satahba_taskq); 1005 1006 mutex_destroy(&sata_hba_inst->satahba_mutex); 1007 kmem_free((void *)sata_hba_inst, 1008 sizeof (struct sata_hba_inst)); 1009 1010 return (DDI_SUCCESS); 1011 1012 case DDI_SUSPEND: 1013 /* 1014 * Postponed until phase 2 1015 */ 1016 return (DDI_FAILURE); 1017 1018 default: 1019 return (DDI_FAILURE); 1020 } 1021 } 1022 1023 1024 /* 1025 * Called by an HBA drive from _fini() routine. 1026 * Unregisters SATA HBA instance/SATA framework pair from the scsi framework. 1027 */ 1028 void 1029 sata_hba_fini(struct modlinkage *modlp) 1030 { 1031 SATADBG1(SATA_DBG_HBA_IF, NULL, 1032 "sata_hba_fini: name %s\n", 1033 ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo); 1034 1035 scsi_hba_fini(modlp); 1036 } 1037 1038 1039 /* 1040 * Default open and close routine for sata_hba framework. 1041 * 1042 */ 1043 /* 1044 * Open devctl node. 1045 * 1046 * Returns: 1047 * 0 if node was open successfully, error code otherwise. 1048 * 1049 * 1050 */ 1051 1052 static int 1053 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp) 1054 { 1055 #ifndef __lock_lint 1056 _NOTE(ARGUNUSED(credp)) 1057 #endif 1058 int rv = 0; 1059 dev_info_t *dip; 1060 scsi_hba_tran_t *scsi_hba_tran; 1061 sata_hba_inst_t *sata_hba_inst; 1062 1063 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL); 1064 1065 if (otyp != OTYP_CHR) 1066 return (EINVAL); 1067 1068 dip = sata_devt_to_devinfo(*devp); 1069 if (dip == NULL) 1070 return (ENXIO); 1071 1072 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1073 return (ENXIO); 1074 1075 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1076 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0) 1077 return (ENXIO); 1078 1079 mutex_enter(&sata_mutex); 1080 if (flags & FEXCL) { 1081 if (sata_hba_inst->satahba_open_flag != 0) { 1082 rv = EBUSY; 1083 } else { 1084 sata_hba_inst->satahba_open_flag = 1085 SATA_DEVCTL_EXOPENED; 1086 } 1087 } else { 1088 if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) { 1089 rv = EBUSY; 1090 } else { 1091 sata_hba_inst->satahba_open_flag = 1092 SATA_DEVCTL_SOPENED; 1093 } 1094 } 1095 mutex_exit(&sata_mutex); 1096 1097 return (rv); 1098 } 1099 1100 1101 /* 1102 * Close devctl node. 1103 * Returns: 1104 * 0 if node was closed successfully, error code otherwise. 1105 * 1106 */ 1107 1108 static int 1109 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp) 1110 { 1111 #ifndef __lock_lint 1112 _NOTE(ARGUNUSED(credp)) 1113 _NOTE(ARGUNUSED(flag)) 1114 #endif 1115 dev_info_t *dip; 1116 scsi_hba_tran_t *scsi_hba_tran; 1117 sata_hba_inst_t *sata_hba_inst; 1118 1119 SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL); 1120 1121 if (otyp != OTYP_CHR) 1122 return (EINVAL); 1123 1124 dip = sata_devt_to_devinfo(dev); 1125 if (dip == NULL) 1126 return (ENXIO); 1127 1128 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1129 return (ENXIO); 1130 1131 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1132 if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0) 1133 return (ENXIO); 1134 1135 mutex_enter(&sata_mutex); 1136 sata_hba_inst->satahba_open_flag = 0; 1137 mutex_exit(&sata_mutex); 1138 return (0); 1139 } 1140 1141 1142 1143 /* 1144 * Standard IOCTL commands for SATA hotplugging. 1145 * Implemented DEVCTL_AP commands: 1146 * DEVCTL_AP_CONNECT 1147 * DEVCTL_AP_DISCONNECT 1148 * DEVCTL_AP_CONFIGURE 1149 * DEVCTL_UNCONFIGURE 1150 * DEVCTL_AP_CONTROL 1151 * 1152 * Commands passed to default ndi ioctl handler: 1153 * DEVCTL_DEVICE_GETSTATE 1154 * DEVCTL_DEVICE_ONLINE 1155 * DEVCTL_DEVICE_OFFLINE 1156 * DEVCTL_DEVICE_REMOVE 1157 * DEVCTL_DEVICE_INSERT 1158 * DEVCTL_BUS_GETSTATE 1159 * 1160 * All other cmds are passed to HBA if it provide ioctl handler, or failed 1161 * if not. 1162 * 1163 * Returns: 1164 * 0 if successful, 1165 * error code if operation failed. 1166 * 1167 * NOTE: Port Multiplier is not supported. 1168 * 1169 */ 1170 1171 static int 1172 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, 1173 int *rvalp) 1174 { 1175 #ifndef __lock_lint 1176 _NOTE(ARGUNUSED(credp)) 1177 _NOTE(ARGUNUSED(rvalp)) 1178 #endif 1179 int rv = 0; 1180 int32_t comp_port = -1; 1181 dev_info_t *dip; 1182 devctl_ap_state_t ap_state; 1183 struct devctl_iocdata *dcp = NULL; 1184 scsi_hba_tran_t *scsi_hba_tran; 1185 sata_hba_inst_t *sata_hba_inst; 1186 sata_device_t sata_device; 1187 sata_cport_info_t *cportinfo; 1188 int cport, pmport, qual; 1189 int rval = SATA_SUCCESS; 1190 1191 dip = sata_devt_to_devinfo(dev); 1192 if (dip == NULL) 1193 return (ENXIO); 1194 1195 if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL) 1196 return (ENXIO); 1197 1198 sata_hba_inst = scsi_hba_tran->tran_hba_private; 1199 if (sata_hba_inst == NULL) 1200 return (ENXIO); 1201 1202 if (sata_hba_inst->satahba_tran == NULL) 1203 return (ENXIO); 1204 1205 switch (cmd) { 1206 1207 case DEVCTL_DEVICE_GETSTATE: 1208 case DEVCTL_DEVICE_ONLINE: 1209 case DEVCTL_DEVICE_OFFLINE: 1210 case DEVCTL_DEVICE_REMOVE: 1211 case DEVCTL_BUS_GETSTATE: 1212 /* 1213 * There may be more cases that we want to pass to default 1214 * handler rather than fail them. 1215 */ 1216 return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0)); 1217 } 1218 1219 /* read devctl ioctl data */ 1220 if (cmd != DEVCTL_AP_CONTROL) { 1221 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) 1222 return (EFAULT); 1223 1224 if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) == 1225 -1) { 1226 if (dcp) 1227 ndi_dc_freehdl(dcp); 1228 return (EINVAL); 1229 } 1230 1231 cport = SCSI_TO_SATA_CPORT(comp_port); 1232 pmport = SCSI_TO_SATA_PMPORT(comp_port); 1233 /* Only cport is considered now, i.e. SATA_ADDR_CPORT */ 1234 qual = SATA_ADDR_CPORT; 1235 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, 1236 qual) != 0) { 1237 ndi_dc_freehdl(dcp); 1238 return (EINVAL); 1239 } 1240 1241 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 1242 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1243 cport_mutex); 1244 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) { 1245 /* 1246 * Cannot process ioctl request now. Come back later. 1247 */ 1248 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1249 cport_mutex); 1250 ndi_dc_freehdl(dcp); 1251 return (EBUSY); 1252 } 1253 /* Block event processing for this port */ 1254 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 1255 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1256 1257 sata_device.satadev_addr.cport = cport; 1258 sata_device.satadev_addr.pmport = pmport; 1259 sata_device.satadev_addr.qual = qual; 1260 sata_device.satadev_rev = SATA_DEVICE_REV; 1261 } 1262 1263 switch (cmd) { 1264 1265 case DEVCTL_AP_DISCONNECT: 1266 1267 /* 1268 * Normally, cfgadm sata plugin will try to offline 1269 * (unconfigure) device before this request. Nevertheless, 1270 * if a device is still configured, we need to 1271 * attempt to offline and unconfigure device first, and we will 1272 * deactivate the port regardless of the unconfigure 1273 * operation results. 1274 * 1275 */ 1276 rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device); 1277 1278 break; 1279 1280 case DEVCTL_AP_UNCONFIGURE: 1281 1282 /* 1283 * The unconfigure operation uses generic nexus operation to 1284 * offline a device. It leaves a target device node attached. 1285 * and obviously sata_drive_info attached as well, because 1286 * from the hardware point of view nothing has changed. 1287 */ 1288 rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device); 1289 break; 1290 1291 case DEVCTL_AP_CONNECT: 1292 { 1293 /* 1294 * The sata cfgadm pluging will invoke this operation only if 1295 * port was found in the disconnect state (failed state 1296 * is also treated as the disconnected state). 1297 * If port activation is successful and a device is found 1298 * attached to the port, the initialization sequence is 1299 * executed to probe the port and attach 1300 * a device structure to a port structure. The device is not 1301 * set in configured state (system-wise) by this operation. 1302 */ 1303 1304 rv = sata_ioctl_connect(sata_hba_inst, &sata_device); 1305 1306 break; 1307 } 1308 1309 case DEVCTL_AP_CONFIGURE: 1310 { 1311 /* 1312 * A port may be in an active or shutdown state. 1313 * If port is in a failed state, operation is aborted. 1314 * If a port is in a shutdown state, sata_tran_port_activate() 1315 * is invoked prior to any other operation. 1316 * 1317 * Onlining the device involves creating a new target node. 1318 * If there is an old target node present (belonging to 1319 * previously removed device), the operation is aborted - the 1320 * old node has to be released and removed before configure 1321 * operation is attempted. 1322 */ 1323 1324 rv = sata_ioctl_configure(sata_hba_inst, &sata_device); 1325 1326 break; 1327 } 1328 1329 case DEVCTL_AP_GETSTATE: 1330 1331 sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state); 1332 1333 ap_state.ap_last_change = (time_t)-1; 1334 ap_state.ap_error_code = 0; 1335 ap_state.ap_in_transition = 0; 1336 1337 /* Copy the return AP-state information to the user space */ 1338 if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) { 1339 rv = EFAULT; 1340 } 1341 break; 1342 1343 case DEVCTL_AP_CONTROL: 1344 { 1345 /* 1346 * Generic devctl for hardware specific functionality 1347 */ 1348 sata_ioctl_data_t ioc; 1349 1350 ASSERT(dcp == NULL); 1351 1352 /* Copy in user ioctl data first */ 1353 #ifdef _MULTI_DATAMODEL 1354 if (ddi_model_convert_from(mode & FMODELS) == 1355 DDI_MODEL_ILP32) { 1356 1357 sata_ioctl_data_32_t ioc32; 1358 1359 if (ddi_copyin((void *)arg, (void *)&ioc32, 1360 sizeof (ioc32), mode) != 0) { 1361 rv = EFAULT; 1362 break; 1363 } 1364 ioc.cmd = (uint_t)ioc32.cmd; 1365 ioc.port = (uint_t)ioc32.port; 1366 ioc.get_size = (uint_t)ioc32.get_size; 1367 ioc.buf = (caddr_t)(uintptr_t)ioc32.buf; 1368 ioc.bufsiz = (uint_t)ioc32.bufsiz; 1369 ioc.misc_arg = (uint_t)ioc32.misc_arg; 1370 } else 1371 #endif /* _MULTI_DATAMODEL */ 1372 if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc), 1373 mode) != 0) { 1374 return (EFAULT); 1375 } 1376 1377 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 1378 "sata_hba_ioctl: DEVCTL_AP_CONTROL " 1379 "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port); 1380 1381 /* 1382 * To avoid BE/LE and 32/64 issues, a get_size always returns 1383 * a 32-bit number. 1384 */ 1385 if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) { 1386 return (EINVAL); 1387 } 1388 /* validate address */ 1389 cport = SCSI_TO_SATA_CPORT(ioc.port); 1390 pmport = SCSI_TO_SATA_PMPORT(ioc.port); 1391 qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port); 1392 1393 /* Override address qualifier - handle cport only for now */ 1394 qual = SATA_ADDR_CPORT; 1395 1396 if (sata_validate_sata_address(sata_hba_inst, cport, 1397 pmport, qual) != 0) 1398 return (EINVAL); 1399 1400 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 1401 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1402 cport_mutex); 1403 /* Is the port locked by event processing daemon ? */ 1404 if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) { 1405 /* 1406 * Cannot process ioctl request now. Come back later 1407 */ 1408 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 1409 cport_mutex); 1410 return (EBUSY); 1411 } 1412 /* Block event processing for this port */ 1413 cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 1414 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1415 1416 1417 sata_device.satadev_addr.cport = cport; 1418 sata_device.satadev_addr.pmport = pmport; 1419 sata_device.satadev_addr.qual = qual; 1420 sata_device.satadev_rev = SATA_DEVICE_REV; 1421 1422 switch (ioc.cmd) { 1423 1424 case SATA_CFGA_RESET_PORT: 1425 /* 1426 * There is no protection for configured device. 1427 */ 1428 rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device); 1429 break; 1430 1431 case SATA_CFGA_RESET_DEVICE: 1432 /* 1433 * There is no protection for configured device. 1434 */ 1435 rv = sata_ioctl_reset_device(sata_hba_inst, 1436 &sata_device); 1437 break; 1438 1439 case SATA_CFGA_RESET_ALL: 1440 /* 1441 * There is no protection for configured devices. 1442 */ 1443 rv = sata_ioctl_reset_all(sata_hba_inst); 1444 /* 1445 * We return here, because common return is for 1446 * a single port operation - we have already unlocked 1447 * all ports and no dc handle was allocated. 1448 */ 1449 return (rv); 1450 1451 case SATA_CFGA_PORT_DEACTIVATE: 1452 /* 1453 * Arbitrarily unconfigure attached device, if any. 1454 * Even if the unconfigure fails, proceed with the 1455 * port deactivation. 1456 */ 1457 rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device); 1458 1459 break; 1460 1461 case SATA_CFGA_PORT_ACTIVATE: 1462 1463 rv = sata_ioctl_activate(sata_hba_inst, &sata_device); 1464 break; 1465 1466 case SATA_CFGA_PORT_SELF_TEST: 1467 1468 rv = sata_ioctl_port_self_test(sata_hba_inst, 1469 &sata_device); 1470 break; 1471 1472 case SATA_CFGA_GET_DEVICE_PATH: 1473 if (qual == SATA_ADDR_CPORT) 1474 sata_device.satadev_addr.qual = 1475 SATA_ADDR_DCPORT; 1476 else 1477 sata_device.satadev_addr.qual = 1478 SATA_ADDR_DPMPORT; 1479 rv = sata_ioctl_get_device_path(sata_hba_inst, 1480 &sata_device, &ioc, mode); 1481 break; 1482 1483 case SATA_CFGA_GET_AP_TYPE: 1484 1485 rv = sata_ioctl_get_ap_type(sata_hba_inst, 1486 &sata_device, &ioc, mode); 1487 break; 1488 1489 case SATA_CFGA_GET_MODEL_INFO: 1490 1491 rv = sata_ioctl_get_model_info(sata_hba_inst, 1492 &sata_device, &ioc, mode); 1493 break; 1494 1495 case SATA_CFGA_GET_REVFIRMWARE_INFO: 1496 1497 rv = sata_ioctl_get_revfirmware_info(sata_hba_inst, 1498 &sata_device, &ioc, mode); 1499 break; 1500 1501 case SATA_CFGA_GET_SERIALNUMBER_INFO: 1502 1503 rv = sata_ioctl_get_serialnumber_info(sata_hba_inst, 1504 &sata_device, &ioc, mode); 1505 break; 1506 1507 default: 1508 rv = EINVAL; 1509 break; 1510 1511 } /* End of DEVCTL_AP_CONTROL cmd switch */ 1512 1513 break; 1514 } 1515 1516 default: 1517 { 1518 /* 1519 * If we got here, we got an IOCTL that SATA HBA Framework 1520 * does not recognize. Pass ioctl to HBA driver, in case 1521 * it could process it. 1522 */ 1523 sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran; 1524 dev_info_t *mydip = SATA_DIP(sata_hba_inst); 1525 1526 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 1527 "IOCTL 0x%2x not supported in SATA framework, " 1528 "passthrough to HBA", cmd); 1529 1530 if (sata_tran->sata_tran_ioctl == NULL) { 1531 rv = EINVAL; 1532 break; 1533 } 1534 rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg); 1535 if (rval != 0) { 1536 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 1537 "IOCTL 0x%2x failed in HBA", cmd); 1538 rv = rval; 1539 } 1540 break; 1541 } 1542 1543 } /* End of main IOCTL switch */ 1544 1545 if (dcp) { 1546 ndi_dc_freehdl(dcp); 1547 } 1548 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1549 cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY; 1550 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 1551 1552 return (rv); 1553 } 1554 1555 1556 /* 1557 * Create error retrieval sata packet 1558 * 1559 * A sata packet is allocated and set-up to contain specified error retrieval 1560 * command and appropriate dma-able data buffer. 1561 * No association with any scsi packet is made and no callback routine is 1562 * specified. 1563 * 1564 * Returns a pointer to sata packet upon successfull packet creation. 1565 * Returns NULL, if packet cannot be created. 1566 */ 1567 sata_pkt_t * 1568 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device, 1569 int pkt_type) 1570 { 1571 sata_hba_inst_t *sata_hba_inst; 1572 sata_pkt_txlate_t *spx; 1573 sata_pkt_t *spkt; 1574 sata_drive_info_t *sdinfo; 1575 1576 mutex_enter(&sata_mutex); 1577 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 1578 sata_hba_inst = sata_hba_inst->satahba_next) { 1579 if (SATA_DIP(sata_hba_inst) == dip) 1580 break; 1581 } 1582 mutex_exit(&sata_mutex); 1583 ASSERT(sata_hba_inst != NULL); 1584 1585 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 1586 if (sdinfo == NULL) { 1587 sata_log(sata_hba_inst, CE_WARN, 1588 "sata: error recovery request for non-attached device at " 1589 "cport %d", sata_device->satadev_addr.cport); 1590 return (NULL); 1591 } 1592 1593 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 1594 spx->txlt_sata_hba_inst = sata_hba_inst; 1595 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 1596 spkt = sata_pkt_alloc(spx, NULL); 1597 if (spkt == NULL) { 1598 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1599 return (NULL); 1600 } 1601 /* address is needed now */ 1602 spkt->satapkt_device.satadev_addr = sata_device->satadev_addr; 1603 1604 switch (pkt_type) { 1605 case SATA_ERR_RETR_PKT_TYPE_NCQ: 1606 if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) 1607 return (spkt); 1608 break; 1609 1610 case SATA_ERR_RETR_PKT_TYPE_ATAPI: 1611 if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) 1612 return (spkt); 1613 break; 1614 1615 default: 1616 break; 1617 } 1618 1619 sata_pkt_free(spx); 1620 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1621 return (NULL); 1622 1623 } 1624 1625 1626 /* 1627 * Free error retrieval sata packet 1628 * 1629 * Free sata packet and any associated resources allocated previously by 1630 * sata_get_error_retrieval_pkt(). 1631 * 1632 * Void return. 1633 */ 1634 void 1635 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt) 1636 { 1637 sata_pkt_txlate_t *spx = 1638 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 1639 1640 ASSERT(sata_pkt != NULL); 1641 1642 sata_free_local_buffer(spx); 1643 sata_pkt_free(spx); 1644 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 1645 1646 } 1647 1648 /* 1649 * sata_name_child is for composing the name of the node 1650 * the format of the name is "target,0". 1651 */ 1652 static int 1653 sata_name_child(dev_info_t *dip, char *name, int namelen) 1654 { 1655 int target; 1656 1657 target = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 1658 DDI_PROP_DONTPASS, "target", -1); 1659 if (target == -1) 1660 return (DDI_FAILURE); 1661 (void) snprintf(name, namelen, "%x,0", target); 1662 return (DDI_SUCCESS); 1663 } 1664 1665 1666 1667 /* ****************** SCSA required entry points *********************** */ 1668 1669 /* 1670 * Implementation of scsi tran_tgt_init. 1671 * sata_scsi_tgt_init() initializes scsi_device structure 1672 * 1673 * If successful, DDI_SUCCESS is returned. 1674 * DDI_FAILURE is returned if addressed device does not exist 1675 */ 1676 1677 static int 1678 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip, 1679 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 1680 { 1681 #ifndef __lock_lint 1682 _NOTE(ARGUNUSED(hba_dip)) 1683 _NOTE(ARGUNUSED(tgt_dip)) 1684 #endif 1685 sata_device_t sata_device; 1686 sata_drive_info_t *sdinfo; 1687 struct sata_id *sid; 1688 sata_hba_inst_t *sata_hba_inst; 1689 char model[SATA_ID_MODEL_LEN + 1]; 1690 char fw[SATA_ID_FW_LEN + 1]; 1691 char *vid, *pid; 1692 int i; 1693 1694 /* 1695 * Fail tran_tgt_init for .conf stub node 1696 */ 1697 if (ndi_dev_is_persistent_node(tgt_dip) == 0) { 1698 (void) ndi_merge_node(tgt_dip, sata_name_child); 1699 ddi_set_name_addr(tgt_dip, NULL); 1700 return (DDI_FAILURE); 1701 } 1702 1703 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 1704 1705 /* Validate scsi device address */ 1706 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 1707 &sata_device) != 0) 1708 return (DDI_FAILURE); 1709 1710 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 1711 sata_device.satadev_addr.cport))); 1712 1713 /* sata_device now contains a valid sata address */ 1714 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 1715 if (sdinfo == NULL) { 1716 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1717 sata_device.satadev_addr.cport))); 1718 return (DDI_FAILURE); 1719 } 1720 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1721 sata_device.satadev_addr.cport))); 1722 1723 /* 1724 * Check if we need to create a legacy devid (i.e cmdk style) for 1725 * the target disks. 1726 * 1727 * HBA devinfo node will have the property "use-cmdk-devid-format" 1728 * if we need to create cmdk-style devid for all the disk devices 1729 * attached to this controller. This property may have been set 1730 * from HBA driver's .conf file or by the HBA driver in its 1731 * attach(9F) function. 1732 */ 1733 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 1734 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS, 1735 "use-cmdk-devid-format", 0) == 1)) { 1736 /* register a legacy devid for this target node */ 1737 sata_target_devid_register(tgt_dip, sdinfo); 1738 } 1739 1740 1741 /* 1742 * 'Identify Device Data' does not always fit in standard SCSI 1743 * INQUIRY data, so establish INQUIRY_* properties with full-form 1744 * of information. 1745 */ 1746 sid = &sdinfo->satadrv_id; 1747 #ifdef _LITTLE_ENDIAN 1748 swab(sid->ai_model, model, SATA_ID_MODEL_LEN); 1749 swab(sid->ai_fw, fw, SATA_ID_FW_LEN); 1750 #else /* _LITTLE_ENDIAN */ 1751 bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN); 1752 bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN); 1753 #endif /* _LITTLE_ENDIAN */ 1754 model[SATA_ID_MODEL_LEN] = 0; 1755 fw[SATA_ID_FW_LEN] = 0; 1756 1757 /* split model into into vid/pid */ 1758 for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++) 1759 if ((*pid == ' ') || (*pid == '\t')) 1760 break; 1761 if (i < SATA_ID_MODEL_LEN) { 1762 vid = model; 1763 *pid++ = 0; /* terminate vid, establish pid */ 1764 } else { 1765 vid = NULL; /* vid will stay "ATA " */ 1766 pid = model; /* model is all pid */ 1767 } 1768 1769 if (vid) 1770 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_VENDOR_ID, 1771 vid, strlen(vid)); 1772 if (pid) 1773 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID, 1774 pid, strlen(pid)); 1775 (void) scsi_hba_prop_update_inqstring(sd, INQUIRY_REVISION_ID, 1776 fw, strlen(fw)); 1777 1778 return (DDI_SUCCESS); 1779 } 1780 1781 /* 1782 * Implementation of scsi tran_tgt_probe. 1783 * Probe target, by calling default scsi routine scsi_hba_probe() 1784 */ 1785 static int 1786 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void)) 1787 { 1788 sata_hba_inst_t *sata_hba_inst = 1789 (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private); 1790 int rval; 1791 1792 rval = scsi_hba_probe(sd, callback); 1793 1794 if (rval == SCSIPROBE_EXISTS) { 1795 /* 1796 * Set property "pm-capable" on the target device node, so that 1797 * the target driver will not try to fetch scsi cycle counters 1798 * before enabling device power-management. 1799 */ 1800 if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev, 1801 "pm-capable", 1)) != DDI_PROP_SUCCESS) { 1802 sata_log(sata_hba_inst, CE_WARN, 1803 "SATA device at port %d: " 1804 "will not be power-managed ", 1805 SCSI_TO_SATA_CPORT(sd->sd_address.a_target)); 1806 SATA_LOG_D((sata_hba_inst, CE_WARN, 1807 "failure updating pm-capable property")); 1808 } 1809 } 1810 return (rval); 1811 } 1812 1813 /* 1814 * Implementation of scsi tran_tgt_free. 1815 * Release all resources allocated for scsi_device 1816 */ 1817 static void 1818 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip, 1819 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 1820 { 1821 #ifndef __lock_lint 1822 _NOTE(ARGUNUSED(hba_dip)) 1823 #endif 1824 sata_device_t sata_device; 1825 sata_drive_info_t *sdinfo; 1826 sata_hba_inst_t *sata_hba_inst; 1827 ddi_devid_t devid; 1828 1829 sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private); 1830 1831 /* Validate scsi device address */ 1832 if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address, 1833 &sata_device) != 0) 1834 return; 1835 1836 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 1837 sata_device.satadev_addr.cport))); 1838 1839 /* sata_device now should contain a valid sata address */ 1840 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 1841 if (sdinfo == NULL) { 1842 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1843 sata_device.satadev_addr.cport))); 1844 return; 1845 } 1846 /* 1847 * We did not allocate any resources in sata_scsi_tgt_init() 1848 * other than few properties. 1849 * Free them. 1850 */ 1851 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1852 sata_device.satadev_addr.cport))); 1853 (void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable"); 1854 1855 /* 1856 * If devid was previously created but not freed up from 1857 * sd(7D) driver (i.e during detach(9F)) then do it here. 1858 */ 1859 if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) && 1860 (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS, 1861 "use-cmdk-devid-format", 0) == 1) && 1862 (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) { 1863 ddi_devid_unregister(tgt_dip); 1864 ddi_devid_free(devid); 1865 } 1866 } 1867 1868 /* 1869 * Implementation of scsi tran_init_pkt 1870 * Upon successful return, scsi pkt buffer has DMA resources allocated. 1871 * 1872 * It seems that we should always allocate pkt, even if the address is 1873 * for non-existing device - just use some default for dma_attr. 1874 * The reason is that there is no way to communicate this to a caller here. 1875 * Subsequent call to sata_scsi_start may fail appropriately. 1876 * Simply returning NULL does not seem to discourage a target driver... 1877 * 1878 * Returns a pointer to initialized scsi_pkt, or NULL otherwise. 1879 */ 1880 static struct scsi_pkt * 1881 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt, 1882 struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags, 1883 int (*callback)(caddr_t), caddr_t arg) 1884 { 1885 sata_hba_inst_t *sata_hba_inst = 1886 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 1887 dev_info_t *dip = SATA_DIP(sata_hba_inst); 1888 sata_device_t sata_device; 1889 sata_drive_info_t *sdinfo; 1890 sata_pkt_txlate_t *spx; 1891 ddi_dma_attr_t cur_dma_attr; 1892 int rval; 1893 boolean_t new_pkt = TRUE; 1894 1895 ASSERT(ap->a_hba_tran->tran_hba_dip == dip); 1896 1897 /* 1898 * We need to translate the address, even if it could be 1899 * a bogus one, for a non-existing device 1900 */ 1901 sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 1902 sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target); 1903 sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 1904 sata_device.satadev_rev = SATA_DEVICE_REV; 1905 1906 if (pkt == NULL) { 1907 /* 1908 * Have to allocate a brand new scsi packet. 1909 * We need to operate with auto request sense enabled. 1910 */ 1911 pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen, 1912 MAX(statuslen, sizeof (struct scsi_arq_status)), 1913 tgtlen, sizeof (sata_pkt_txlate_t), callback, arg); 1914 1915 if (pkt == NULL) 1916 return (NULL); 1917 1918 /* Fill scsi packet structure */ 1919 pkt->pkt_comp = (void (*)())NULL; 1920 pkt->pkt_time = 0; 1921 pkt->pkt_resid = 0; 1922 pkt->pkt_statistics = 0; 1923 pkt->pkt_reason = 0; 1924 1925 /* 1926 * pkt_hba_private will point to sata pkt txlate structure 1927 */ 1928 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 1929 bzero(spx, sizeof (sata_pkt_txlate_t)); 1930 1931 spx->txlt_scsi_pkt = pkt; 1932 spx->txlt_sata_hba_inst = sata_hba_inst; 1933 1934 /* Allocate sata_pkt */ 1935 spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback); 1936 if (spx->txlt_sata_pkt == NULL) { 1937 /* Could not allocate sata pkt */ 1938 scsi_hba_pkt_free(ap, pkt); 1939 return (NULL); 1940 } 1941 /* Set sata address */ 1942 spx->txlt_sata_pkt->satapkt_device.satadev_addr = 1943 sata_device.satadev_addr; 1944 spx->txlt_sata_pkt->satapkt_device.satadev_rev = 1945 sata_device.satadev_rev; 1946 1947 if ((bp == NULL) || (bp->b_bcount == 0)) 1948 return (pkt); 1949 1950 spx->txlt_total_residue = bp->b_bcount; 1951 } else { 1952 new_pkt = FALSE; 1953 /* 1954 * Packet was preallocated/initialized by previous call 1955 */ 1956 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 1957 1958 if ((bp == NULL) || (bp->b_bcount == 0)) { 1959 return (pkt); 1960 } 1961 1962 /* Pkt is available already: spx->txlt_scsi_pkt == pkt; */ 1963 } 1964 1965 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 1966 1967 /* 1968 * We use an adjusted version of the dma_attr, to account 1969 * for device addressing limitations. 1970 * sata_adjust_dma_attr() will handle sdinfo == NULL which may 1971 * happen when a device is not yet configured. 1972 */ 1973 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 1974 sata_device.satadev_addr.cport))); 1975 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 1976 &spx->txlt_sata_pkt->satapkt_device); 1977 /* NULL sdinfo may be passsed to sata_adjust_dma_attr() */ 1978 sata_adjust_dma_attr(sdinfo, 1979 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 1980 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 1981 sata_device.satadev_addr.cport))); 1982 /* 1983 * Allocate necessary DMA resources for the packet's data buffer 1984 * NOTE: 1985 * In case of read/write commands, DMA resource allocation here is 1986 * based on the premise that the transfer length specified in 1987 * the read/write scsi cdb will match exactly DMA resources - 1988 * returning correct packet residue is crucial. 1989 */ 1990 if ((rval = sata_dma_buf_setup(spx, flags, callback, arg, 1991 &cur_dma_attr)) != DDI_SUCCESS) { 1992 /* 1993 * If a DMA allocation request fails with 1994 * DDI_DMA_NOMAPPING, indicate the error by calling 1995 * bioerror(9F) with bp and an error code of EFAULT. 1996 * If a DMA allocation request fails with 1997 * DDI_DMA_TOOBIG, indicate the error by calling 1998 * bioerror(9F) with bp and an error code of EINVAL. 1999 * For DDI_DMA_NORESOURCES, we may have some of them allocated. 2000 * Request may be repeated later - there is no real error. 2001 */ 2002 switch (rval) { 2003 case DDI_DMA_NORESOURCES: 2004 bioerror(bp, 0); 2005 break; 2006 case DDI_DMA_NOMAPPING: 2007 case DDI_DMA_BADATTR: 2008 bioerror(bp, EFAULT); 2009 break; 2010 case DDI_DMA_TOOBIG: 2011 default: 2012 bioerror(bp, EINVAL); 2013 break; 2014 } 2015 if (new_pkt == TRUE) { 2016 /* 2017 * Since this is a new packet, we can clean-up 2018 * everything 2019 */ 2020 sata_scsi_destroy_pkt(ap, pkt); 2021 } else { 2022 /* 2023 * This is a re-used packet. It will be target driver's 2024 * responsibility to eventually destroy it (which 2025 * will free allocated resources). 2026 * Here, we just "complete" the request, leaving 2027 * allocated resources intact, so the request may 2028 * be retried. 2029 */ 2030 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 2031 sata_pkt_free(spx); 2032 } 2033 return (NULL); 2034 } 2035 /* Set number of bytes that are not yet accounted for */ 2036 pkt->pkt_resid = spx->txlt_total_residue; 2037 ASSERT(pkt->pkt_resid >= 0); 2038 2039 return (pkt); 2040 } 2041 2042 /* 2043 * Implementation of scsi tran_start. 2044 * Translate scsi cmd into sata operation and return status. 2045 * ATAPI CDBs are passed to ATAPI devices - the device determines what commands 2046 * are supported. 2047 * For SATA hard disks, supported scsi commands: 2048 * SCMD_INQUIRY 2049 * SCMD_TEST_UNIT_READY 2050 * SCMD_START_STOP 2051 * SCMD_READ_CAPACITY 2052 * SCMD_REQUEST_SENSE 2053 * SCMD_LOG_SENSE_G1 2054 * SCMD_LOG_SELECT_G1 2055 * SCMD_MODE_SENSE (specific pages) 2056 * SCMD_MODE_SENSE_G1 (specific pages) 2057 * SCMD_MODE_SELECT (specific pages) 2058 * SCMD_MODE_SELECT_G1 (specific pages) 2059 * SCMD_SYNCHRONIZE_CACHE 2060 * SCMD_SYNCHRONIZE_CACHE_G1 2061 * SCMD_READ 2062 * SCMD_READ_G1 2063 * SCMD_READ_G4 2064 * SCMD_READ_G5 2065 * SCMD_WRITE 2066 * SCMD_WRITE_BUFFER 2067 * SCMD_WRITE_G1 2068 * SCMD_WRITE_G4 2069 * SCMD_WRITE_G5 2070 * SCMD_SEEK (noop) 2071 * SCMD_SDIAG 2072 * 2073 * All other commands are rejected as unsupported. 2074 * 2075 * Returns: 2076 * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver 2077 * for execution. TRAN_ACCEPT may be returned also if device was removed but 2078 * a callback could be scheduled. 2079 * TRAN_BADPKT if cmd was directed to invalid address. 2080 * TRAN_FATAL_ERROR is command was rejected due to hardware error, including 2081 * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device 2082 * was removed and there was no callback specified in scsi pkt. 2083 * TRAN_BUSY if command could not be executed becasue HBA driver or SATA 2084 * framework was busy performing some other operation(s). 2085 * 2086 */ 2087 static int 2088 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt) 2089 { 2090 sata_hba_inst_t *sata_hba_inst = 2091 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2092 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2093 sata_drive_info_t *sdinfo; 2094 struct buf *bp; 2095 int cport; 2096 int rval; 2097 2098 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2099 "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]); 2100 2101 ASSERT(spx != NULL && 2102 spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL); 2103 2104 cport = SCSI_TO_SATA_CPORT(ap->a_target); 2105 2106 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2107 sdinfo = sata_get_device_info(sata_hba_inst, 2108 &spx->txlt_sata_pkt->satapkt_device); 2109 if (sdinfo == NULL || 2110 SATA_CPORT_INFO(sata_hba_inst, cport)->cport_tgtnode_clean == 2111 B_FALSE || 2112 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 2113 2114 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2115 pkt->pkt_reason = CMD_DEV_GONE; 2116 /* 2117 * The sd target driver is checking CMD_DEV_GONE pkt_reason 2118 * only in callback function (for normal requests) and 2119 * in the dump code path. 2120 * So, if the callback is available, we need to do 2121 * the callback rather than returning TRAN_FATAL_ERROR here. 2122 */ 2123 if (pkt->pkt_comp != NULL) { 2124 /* scsi callback required */ 2125 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 2126 (task_func_t *)pkt->pkt_comp, 2127 (void *)pkt, TQ_SLEEP) == NULL) 2128 /* Scheduling the callback failed */ 2129 return (TRAN_BUSY); 2130 return (TRAN_ACCEPT); 2131 } 2132 /* No callback available */ 2133 return (TRAN_FATAL_ERROR); 2134 } 2135 2136 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) { 2137 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2138 rval = sata_txlt_atapi(spx); 2139 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2140 "sata_scsi_start atapi: rval %d\n", rval); 2141 return (rval); 2142 } 2143 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 2144 2145 /* ATA Disk commands processing starts here */ 2146 2147 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 2148 2149 switch (pkt->pkt_cdbp[0]) { 2150 2151 case SCMD_INQUIRY: 2152 /* Mapped to identify device */ 2153 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2154 bp_mapin(bp); 2155 rval = sata_txlt_inquiry(spx); 2156 break; 2157 2158 case SCMD_TEST_UNIT_READY: 2159 /* 2160 * SAT "SATA to ATA Translation" doc specifies translation 2161 * to ATA CHECK POWER MODE. 2162 */ 2163 rval = sata_txlt_test_unit_ready(spx); 2164 break; 2165 2166 case SCMD_START_STOP: 2167 /* Mapping depends on the command */ 2168 rval = sata_txlt_start_stop_unit(spx); 2169 break; 2170 2171 case SCMD_READ_CAPACITY: 2172 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2173 bp_mapin(bp); 2174 rval = sata_txlt_read_capacity(spx); 2175 break; 2176 2177 case SCMD_REQUEST_SENSE: 2178 /* 2179 * Always No Sense, since we force ARQ 2180 */ 2181 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2182 bp_mapin(bp); 2183 rval = sata_txlt_request_sense(spx); 2184 break; 2185 2186 case SCMD_LOG_SENSE_G1: 2187 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2188 bp_mapin(bp); 2189 rval = sata_txlt_log_sense(spx); 2190 break; 2191 2192 case SCMD_LOG_SELECT_G1: 2193 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2194 bp_mapin(bp); 2195 rval = sata_txlt_log_select(spx); 2196 break; 2197 2198 case SCMD_MODE_SENSE: 2199 case SCMD_MODE_SENSE_G1: 2200 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2201 bp_mapin(bp); 2202 rval = sata_txlt_mode_sense(spx); 2203 break; 2204 2205 2206 case SCMD_MODE_SELECT: 2207 case SCMD_MODE_SELECT_G1: 2208 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2209 bp_mapin(bp); 2210 rval = sata_txlt_mode_select(spx); 2211 break; 2212 2213 case SCMD_SYNCHRONIZE_CACHE: 2214 case SCMD_SYNCHRONIZE_CACHE_G1: 2215 rval = sata_txlt_synchronize_cache(spx); 2216 break; 2217 2218 case SCMD_READ: 2219 case SCMD_READ_G1: 2220 case SCMD_READ_G4: 2221 case SCMD_READ_G5: 2222 rval = sata_txlt_read(spx); 2223 break; 2224 case SCMD_WRITE_BUFFER: 2225 if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO))) 2226 bp_mapin(bp); 2227 rval = sata_txlt_write_buffer(spx); 2228 break; 2229 2230 case SCMD_WRITE: 2231 case SCMD_WRITE_G1: 2232 case SCMD_WRITE_G4: 2233 case SCMD_WRITE_G5: 2234 rval = sata_txlt_write(spx); 2235 break; 2236 2237 case SCMD_SEEK: 2238 rval = sata_txlt_nodata_cmd_immediate(spx); 2239 break; 2240 2241 /* Other cases will be filed later */ 2242 /* postponed until phase 2 of the development */ 2243 default: 2244 rval = sata_txlt_invalid_command(spx); 2245 break; 2246 } 2247 2248 SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst, 2249 "sata_scsi_start: rval %d\n", rval); 2250 2251 return (rval); 2252 } 2253 2254 /* 2255 * Implementation of scsi tran_abort. 2256 * Abort specific pkt or all packets. 2257 * 2258 * Returns 1 if one or more packets were aborted, returns 0 otherwise 2259 * 2260 * May be called from an interrupt level. 2261 */ 2262 static int 2263 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt) 2264 { 2265 sata_hba_inst_t *sata_hba_inst = 2266 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2267 sata_device_t sata_device; 2268 sata_pkt_t *sata_pkt; 2269 2270 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2271 "sata_scsi_abort: %s at target: 0x%x\n", 2272 scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target); 2273 2274 /* Validate address */ 2275 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) 2276 /* Invalid address */ 2277 return (0); 2278 2279 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2280 sata_device.satadev_addr.cport))); 2281 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) { 2282 /* invalid address */ 2283 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2284 sata_device.satadev_addr.cport))); 2285 return (0); 2286 } 2287 if (scsi_pkt == NULL) { 2288 /* 2289 * Abort all packets. 2290 * Although we do not have specific packet, we still need 2291 * dummy packet structure to pass device address to HBA. 2292 * Allocate one, without sleeping. Fail if pkt cannot be 2293 * allocated. 2294 */ 2295 sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP); 2296 if (sata_pkt == NULL) { 2297 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2298 sata_device.satadev_addr.cport))); 2299 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: " 2300 "could not allocate sata_pkt")); 2301 return (0); 2302 } 2303 sata_pkt->satapkt_rev = SATA_PKT_REV; 2304 sata_pkt->satapkt_device = sata_device; 2305 sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 2306 } else { 2307 if (scsi_pkt->pkt_ha_private == NULL) { 2308 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2309 sata_device.satadev_addr.cport))); 2310 return (0); /* Bad scsi pkt */ 2311 } 2312 /* extract pointer to sata pkt */ 2313 sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)-> 2314 txlt_sata_pkt; 2315 } 2316 2317 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2318 sata_device.satadev_addr.cport))); 2319 /* Send abort request to HBA */ 2320 if ((*SATA_ABORT_FUNC(sata_hba_inst)) 2321 (SATA_DIP(sata_hba_inst), sata_pkt, 2322 scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) == 2323 SATA_SUCCESS) { 2324 if (scsi_pkt == NULL) 2325 kmem_free(sata_pkt, sizeof (sata_pkt_t)); 2326 /* Success */ 2327 return (1); 2328 } 2329 /* Else, something did not go right */ 2330 if (scsi_pkt == NULL) 2331 kmem_free(sata_pkt, sizeof (sata_pkt_t)); 2332 /* Failure */ 2333 return (0); 2334 } 2335 2336 2337 /* 2338 * Implementation of scsi tran_reset. 2339 * RESET_ALL request is translated into port reset. 2340 * RESET_TARGET requests is translated into a device reset, 2341 * RESET_LUN request is accepted only for LUN 0 and translated into 2342 * device reset. 2343 * The target reset should cause all HBA active and queued packets to 2344 * be terminated and returned with pkt reason SATA_PKT_RESET prior to 2345 * the return. HBA should report reset event for the device. 2346 * 2347 * Returns 1 upon success, 0 upon failure. 2348 */ 2349 static int 2350 sata_scsi_reset(struct scsi_address *ap, int level) 2351 { 2352 sata_hba_inst_t *sata_hba_inst = 2353 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2354 sata_device_t sata_device; 2355 int val; 2356 2357 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2358 "sata_scsi_reset: level %d target: 0x%x\n", 2359 level, ap->a_target); 2360 2361 /* Validate address */ 2362 val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device); 2363 if (val == -1) 2364 /* Invalid address */ 2365 return (0); 2366 2367 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2368 sata_device.satadev_addr.cport))); 2369 if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) { 2370 /* invalid address */ 2371 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2372 sata_device.satadev_addr.cport))); 2373 return (0); 2374 } 2375 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2376 sata_device.satadev_addr.cport))); 2377 if (level == RESET_ALL) { 2378 /* port reset - cport only */ 2379 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 2380 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 2381 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS) 2382 return (1); 2383 else 2384 return (0); 2385 2386 } else if (val == 0 && 2387 (level == RESET_TARGET || level == RESET_LUN)) { 2388 /* reset device (device attached) */ 2389 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 2390 (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS) 2391 return (1); 2392 else 2393 return (0); 2394 } 2395 return (0); 2396 } 2397 2398 2399 /* 2400 * Implementation of scsi tran_getcap (get transport/device capabilities). 2401 * Supported capabilities for SATA hard disks: 2402 * auto-rqsense (always supported) 2403 * tagged-qing (supported if HBA supports it) 2404 * untagged-qing (could be supported if disk supports it, but because 2405 * caching behavior allowing untagged queuing actually 2406 * results in reduced performance. sd tries to throttle 2407 * back to only 3 outstanding commands, which may 2408 * work for real SCSI disks, but with read ahead 2409 * caching, having more than 1 outstanding command 2410 * results in cache thrashing.) 2411 * sector_size 2412 * dma_max 2413 * interconnect-type (INTERCONNECT_SATA) 2414 * 2415 * Supported capabilities for ATAPI devices (CD/DVD): 2416 * auto-rqsense (always supported) 2417 * sector_size 2418 * dma_max 2419 * interconnect-type (INTERCONNECT_SATA) 2420 * 2421 * Request for other capabilities is rejected as unsupported. 2422 * 2423 * Returns supported capability value, or -1 if capability is unsuppported or 2424 * the address is invalid - no device. 2425 */ 2426 2427 static int 2428 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom) 2429 { 2430 2431 sata_hba_inst_t *sata_hba_inst = 2432 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2433 sata_device_t sata_device; 2434 sata_drive_info_t *sdinfo; 2435 ddi_dma_attr_t adj_dma_attr; 2436 int rval; 2437 2438 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2439 "sata_scsi_getcap: target: 0x%x, cap: %s\n", 2440 ap->a_target, cap); 2441 2442 /* 2443 * We want to process the capabilities on per port granularity. 2444 * So, we are specifically restricting ourselves to whom != 0 2445 * to exclude the controller wide handling. 2446 */ 2447 if (cap == NULL || whom == 0) 2448 return (-1); 2449 2450 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) { 2451 /* Invalid address */ 2452 return (-1); 2453 } 2454 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2455 sata_device.satadev_addr.cport))); 2456 if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) == 2457 NULL) { 2458 /* invalid address */ 2459 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2460 sata_device.satadev_addr.cport))); 2461 return (-1); 2462 } 2463 2464 switch (scsi_hba_lookup_capstr(cap)) { 2465 case SCSI_CAP_ARQ: 2466 rval = 1; /* ARQ supported, turned on */ 2467 break; 2468 2469 case SCSI_CAP_SECTOR_SIZE: 2470 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) 2471 rval = SATA_DISK_SECTOR_SIZE; /* fixed size */ 2472 else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) 2473 rval = SATA_ATAPI_SECTOR_SIZE; 2474 else rval = -1; 2475 break; 2476 2477 /* 2478 * untagged queuing cause a performance inversion because of 2479 * the way sd operates. Because of this reason we do not 2480 * use it when available. 2481 */ 2482 case SCSI_CAP_UNTAGGED_QING: 2483 if (sdinfo->satadrv_features_enabled & 2484 SATA_DEV_F_E_UNTAGGED_QING) 2485 rval = 1; /* Untagged queuing available */ 2486 else 2487 rval = -1; /* Untagged queuing not available */ 2488 break; 2489 2490 case SCSI_CAP_TAGGED_QING: 2491 if ((sdinfo->satadrv_features_enabled & 2492 SATA_DEV_F_E_TAGGED_QING) && 2493 (sdinfo->satadrv_max_queue_depth > 1)) 2494 rval = 1; /* Tagged queuing available */ 2495 else 2496 rval = -1; /* Tagged queuing not available */ 2497 break; 2498 2499 case SCSI_CAP_DMA_MAX: 2500 sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst), 2501 &adj_dma_attr); 2502 rval = (int)adj_dma_attr.dma_attr_maxxfer; 2503 /* We rely on the fact that dma_attr_maxxfer < 0x80000000 */ 2504 break; 2505 2506 case SCSI_CAP_INTERCONNECT_TYPE: 2507 rval = INTERCONNECT_SATA; /* SATA interconnect type */ 2508 break; 2509 2510 default: 2511 rval = -1; 2512 break; 2513 } 2514 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2515 sata_device.satadev_addr.cport))); 2516 return (rval); 2517 } 2518 2519 /* 2520 * Implementation of scsi tran_setcap 2521 * 2522 * Only SCSI_CAP_UNTAGGED_QING and SCSI_CAP_TAGGED_QING are changeable. 2523 * 2524 */ 2525 static int 2526 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom) 2527 { 2528 sata_hba_inst_t *sata_hba_inst = 2529 (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private); 2530 sata_device_t sata_device; 2531 sata_drive_info_t *sdinfo; 2532 int rval; 2533 2534 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 2535 "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap); 2536 2537 /* 2538 * We want to process the capabilities on per port granularity. 2539 * So, we are specifically restricting ourselves to whom != 0 2540 * to exclude the controller wide handling. 2541 */ 2542 if (cap == NULL || whom == 0) { 2543 return (-1); 2544 } 2545 2546 if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) { 2547 /* Invalid address */ 2548 return (-1); 2549 } 2550 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 2551 sata_device.satadev_addr.cport))); 2552 if ((sdinfo = sata_get_device_info(sata_hba_inst, 2553 &sata_device)) == NULL) { 2554 /* invalid address */ 2555 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2556 sata_device.satadev_addr.cport))); 2557 return (-1); 2558 } 2559 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 2560 sata_device.satadev_addr.cport))); 2561 2562 switch (scsi_hba_lookup_capstr(cap)) { 2563 case SCSI_CAP_ARQ: 2564 case SCSI_CAP_SECTOR_SIZE: 2565 case SCSI_CAP_DMA_MAX: 2566 case SCSI_CAP_INTERCONNECT_TYPE: 2567 rval = 0; 2568 break; 2569 case SCSI_CAP_UNTAGGED_QING: 2570 if (SATA_QDEPTH(sata_hba_inst) > 1) { 2571 rval = 1; 2572 if (value == 1) { 2573 sdinfo->satadrv_features_enabled |= 2574 SATA_DEV_F_E_UNTAGGED_QING; 2575 } else if (value == 0) { 2576 sdinfo->satadrv_features_enabled &= 2577 ~SATA_DEV_F_E_UNTAGGED_QING; 2578 } else { 2579 rval = -1; 2580 } 2581 } else { 2582 rval = 0; 2583 } 2584 break; 2585 case SCSI_CAP_TAGGED_QING: 2586 /* This can TCQ or NCQ */ 2587 if (sata_func_enable & SATA_ENABLE_QUEUING && 2588 ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ && 2589 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) || 2590 (sata_func_enable & SATA_ENABLE_NCQ && 2591 sdinfo->satadrv_features_support & SATA_DEV_F_NCQ && 2592 SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) && 2593 (sdinfo->satadrv_max_queue_depth > 1)) { 2594 rval = 1; 2595 if (value == 1) { 2596 sdinfo->satadrv_features_enabled |= 2597 SATA_DEV_F_E_TAGGED_QING; 2598 } else if (value == 0) { 2599 sdinfo->satadrv_features_enabled &= 2600 ~SATA_DEV_F_E_TAGGED_QING; 2601 } else { 2602 rval = -1; 2603 } 2604 } else { 2605 rval = 0; 2606 } 2607 break; 2608 default: 2609 rval = -1; 2610 break; 2611 } 2612 return (rval); 2613 } 2614 2615 /* 2616 * Implementations of scsi tran_destroy_pkt. 2617 * Free resources allocated by sata_scsi_init_pkt() 2618 */ 2619 static void 2620 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 2621 { 2622 sata_pkt_txlate_t *spx; 2623 2624 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2625 2626 sata_common_free_dma_rsrcs(spx); 2627 2628 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 2629 sata_pkt_free(spx); 2630 2631 scsi_hba_pkt_free(ap, pkt); 2632 } 2633 2634 /* 2635 * Implementation of scsi tran_dmafree. 2636 * Free DMA resources allocated by sata_scsi_init_pkt() 2637 */ 2638 2639 static void 2640 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt) 2641 { 2642 #ifndef __lock_lint 2643 _NOTE(ARGUNUSED(ap)) 2644 #endif 2645 sata_pkt_txlate_t *spx; 2646 2647 ASSERT(pkt != NULL); 2648 spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2649 2650 sata_common_free_dma_rsrcs(spx); 2651 } 2652 2653 /* 2654 * Implementation of scsi tran_sync_pkt. 2655 * 2656 * The assumption below is that pkt is unique - there is no need to check ap 2657 * 2658 * Synchronize DMA buffer and, if the intermediate buffer is used, copy data 2659 * into/from the real buffer. 2660 */ 2661 static void 2662 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 2663 { 2664 #ifndef __lock_lint 2665 _NOTE(ARGUNUSED(ap)) 2666 #endif 2667 int rval; 2668 sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private; 2669 struct buf *bp; 2670 int direction; 2671 2672 ASSERT(spx != NULL); 2673 if (spx->txlt_buf_dma_handle != NULL) { 2674 direction = spx->txlt_sata_pkt-> 2675 satapkt_cmd.satacmd_flags.sata_data_direction; 2676 if (spx->txlt_sata_pkt != NULL && 2677 direction != SATA_DIR_NODATA_XFER) { 2678 if (spx->txlt_tmp_buf != NULL) { 2679 /* Intermediate DMA buffer used */ 2680 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 2681 2682 if (direction & SATA_DIR_WRITE) { 2683 bcopy(bp->b_un.b_addr, 2684 spx->txlt_tmp_buf, bp->b_bcount); 2685 } 2686 } 2687 /* Sync the buffer for device or for CPU */ 2688 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 2689 (direction & SATA_DIR_WRITE) ? 2690 DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU); 2691 ASSERT(rval == DDI_SUCCESS); 2692 if (spx->txlt_tmp_buf != NULL && 2693 !(direction & SATA_DIR_WRITE)) { 2694 /* Intermediate DMA buffer used for read */ 2695 bcopy(spx->txlt_tmp_buf, 2696 bp->b_un.b_addr, bp->b_bcount); 2697 } 2698 2699 } 2700 } 2701 } 2702 2703 2704 2705 /* ******************* SATA - SCSI Translation functions **************** */ 2706 /* 2707 * SCSI to SATA pkt and command translation and SATA to SCSI status/error 2708 * translation. 2709 */ 2710 2711 /* 2712 * Checks if a device exists and can be access and translates common 2713 * scsi_pkt data to sata_pkt data. 2714 * 2715 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and 2716 * sata_pkt was set-up. 2717 * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not 2718 * exist and pkt_comp callback was scheduled. 2719 * Returns other TRAN_XXXXX values when error occured and command should be 2720 * rejected with the returned TRAN_XXXXX value. 2721 * 2722 * This function should be called with port mutex held. 2723 */ 2724 static int 2725 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason) 2726 { 2727 sata_drive_info_t *sdinfo; 2728 sata_device_t sata_device; 2729 const struct sata_cmd_flags sata_initial_cmd_flags = { 2730 SATA_DIR_NODATA_XFER, 2731 /* all other values to 0/FALSE */ 2732 }; 2733 /* 2734 * Pkt_reason has to be set if the pkt_comp callback is invoked, 2735 * and that implies TRAN_ACCEPT return value. Any other returned value 2736 * indicates that the scsi packet was not accepted (the reason will not 2737 * be checked by the scsi target driver). 2738 * To make debugging easier, we set pkt_reason to know value here. 2739 * It may be changed later when different completion reason is 2740 * determined. 2741 */ 2742 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 2743 *reason = CMD_TRAN_ERR; 2744 2745 /* Validate address */ 2746 switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst, 2747 &spx->txlt_scsi_pkt->pkt_address, &sata_device)) { 2748 2749 case -1: 2750 /* Invalid address or invalid device type */ 2751 return (TRAN_BADPKT); 2752 case 1: 2753 /* valid address but no device - it has disappeared ? */ 2754 spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE; 2755 *reason = CMD_DEV_GONE; 2756 /* 2757 * The sd target driver is checking CMD_DEV_GONE pkt_reason 2758 * only in callback function (for normal requests) and 2759 * in the dump code path. 2760 * So, if the callback is available, we need to do 2761 * the callback rather than returning TRAN_FATAL_ERROR here. 2762 */ 2763 if (spx->txlt_scsi_pkt->pkt_comp != NULL) { 2764 /* scsi callback required */ 2765 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 2766 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 2767 (void *)spx->txlt_scsi_pkt, 2768 TQ_SLEEP) == NULL) 2769 /* Scheduling the callback failed */ 2770 return (TRAN_BUSY); 2771 2772 return (TRAN_ACCEPT); 2773 } 2774 return (TRAN_FATAL_ERROR); 2775 default: 2776 /* all OK; pkt reason will be overwritten later */ 2777 break; 2778 } 2779 /* 2780 * If in an interrupt context, reject packet if it is to be 2781 * executed in polling mode 2782 */ 2783 if (servicing_interrupt() && 2784 (spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) { 2785 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 2786 "sata_scsi_start: rejecting synchronous command because " 2787 "of interrupt context\n", NULL); 2788 return (TRAN_BUSY); 2789 } 2790 2791 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 2792 &spx->txlt_sata_pkt->satapkt_device); 2793 2794 /* 2795 * If device is in reset condition, reject the packet with 2796 * TRAN_BUSY, unless: 2797 * 1. system is panicking (dumping) 2798 * In such case only one thread is running and there is no way to 2799 * process reset. 2800 * 2. cfgadm operation is is progress (internal APCTL lock is set) 2801 * Some cfgadm operations involve drive commands, so reset condition 2802 * needs to be ignored for IOCTL operations. 2803 */ 2804 if ((sdinfo->satadrv_event_flags & 2805 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) { 2806 2807 if (!ddi_in_panic() && 2808 ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst, 2809 sata_device.satadev_addr.cport) & 2810 SATA_APCTL_LOCK_PORT_BUSY) == 0)) { 2811 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 2812 *reason = CMD_INCOMPLETE; 2813 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 2814 "sata_scsi_start: rejecting command because " 2815 "of device reset state\n", NULL); 2816 return (TRAN_BUSY); 2817 } 2818 } 2819 2820 /* 2821 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by 2822 * sata_scsi_pkt_init() because pkt init had to work also with 2823 * non-existing devices. 2824 * Now we know that the packet was set-up for a real device, so its 2825 * type is known. 2826 */ 2827 spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type; 2828 2829 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags; 2830 if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst, 2831 sata_device.satadev_addr.cport)->cport_event_flags & 2832 SATA_APCTL_LOCK_PORT_BUSY) != 0) { 2833 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 2834 sata_ignore_dev_reset = B_TRUE; 2835 } 2836 /* 2837 * At this point the generic translation routine determined that the 2838 * scsi packet should be accepted. Packet completion reason may be 2839 * changed later when a different completion reason is determined. 2840 */ 2841 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 2842 *reason = CMD_CMPLT; 2843 2844 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) { 2845 /* Synchronous execution */ 2846 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH | 2847 SATA_OPMODE_POLLING; 2848 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 2849 sata_ignore_dev_reset = ddi_in_panic(); 2850 } else { 2851 /* Asynchronous execution */ 2852 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH | 2853 SATA_OPMODE_INTERRUPTS; 2854 } 2855 /* Convert queuing information */ 2856 if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG) 2857 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag = 2858 B_TRUE; 2859 else if (spx->txlt_scsi_pkt->pkt_flags & 2860 (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD)) 2861 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag = 2862 B_TRUE; 2863 2864 /* Always limit pkt time */ 2865 if (spx->txlt_scsi_pkt->pkt_time == 0) 2866 spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time; 2867 else 2868 /* Pass on scsi_pkt time */ 2869 spx->txlt_sata_pkt->satapkt_time = 2870 spx->txlt_scsi_pkt->pkt_time; 2871 2872 return (TRAN_ACCEPT); 2873 } 2874 2875 2876 /* 2877 * Translate ATA Identify Device data to SCSI Inquiry data. 2878 * This function may be called only for ATA devices. 2879 * This function should not be called for ATAPI devices - they 2880 * respond directly to SCSI Inquiry command. 2881 * 2882 * SATA Identify Device data has to be valid in sata_rive_info. 2883 * Buffer has to accomodate the inquiry length (36 bytes). 2884 * 2885 * This function should be called with a port mutex held. 2886 */ 2887 static void 2888 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst, 2889 sata_drive_info_t *sdinfo, uint8_t *buf) 2890 { 2891 2892 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 2893 struct sata_id *sid = &sdinfo->satadrv_id; 2894 2895 /* Start with a nice clean slate */ 2896 bzero((void *)inq, sizeof (struct scsi_inquiry)); 2897 2898 /* 2899 * Rely on the dev_type for setting paripheral qualifier. 2900 * Assume that DTYPE_RODIRECT applies to CD/DVD R/W devices. 2901 * It could be that DTYPE_OPTICAL could also qualify in the future. 2902 * ATAPI Inquiry may provide more data to the target driver. 2903 */ 2904 inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 2905 DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */ 2906 2907 inq->inq_rmb = sid->ai_config & SATA_REM_MEDIA ? 1 : 0; 2908 inq->inq_qual = 0; /* Device type qualifier (obsolete in SCSI3? */ 2909 inq->inq_iso = 0; /* ISO version */ 2910 inq->inq_ecma = 0; /* ECMA version */ 2911 inq->inq_ansi = 3; /* ANSI version - SCSI 3 */ 2912 inq->inq_aenc = 0; /* Async event notification cap. */ 2913 inq->inq_trmiop = 0; /* Supports TERMINATE I/O PROC msg - NO */ 2914 inq->inq_normaca = 0; /* setting NACA bit supported - NO */ 2915 inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */ 2916 inq->inq_len = 31; /* Additional length */ 2917 inq->inq_dualp = 0; /* dual port device - NO */ 2918 inq->inq_reladdr = 0; /* Supports relative addressing - NO */ 2919 inq->inq_sync = 0; /* Supports synchronous data xfers - NO */ 2920 inq->inq_linked = 0; /* Supports linked commands - NO */ 2921 /* 2922 * Queuing support - controller has to 2923 * support some sort of command queuing. 2924 */ 2925 if (SATA_QDEPTH(sata_hba_inst) > 1) 2926 inq->inq_cmdque = 1; /* Supports command queueing - YES */ 2927 else 2928 inq->inq_cmdque = 0; /* Supports command queueing - NO */ 2929 inq->inq_sftre = 0; /* Supports Soft Reset option - NO ??? */ 2930 inq->inq_wbus32 = 0; /* Supports 32 bit wide data xfers - NO */ 2931 inq->inq_wbus16 = 0; /* Supports 16 bit wide data xfers - NO */ 2932 2933 #ifdef _LITTLE_ENDIAN 2934 /* Swap text fields to match SCSI format */ 2935 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 2936 swab(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 2937 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 2938 swab(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 2939 else 2940 swab(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 2941 #else /* _LITTLE_ENDIAN */ 2942 bcopy("ATA ", inq->inq_vid, 8); /* Vendor ID */ 2943 bcopy(sid->ai_model, inq->inq_pid, 16); /* Product ID */ 2944 if (strncmp(&sid->ai_fw[4], " ", 4) == 0) 2945 bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */ 2946 else 2947 bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */ 2948 #endif /* _LITTLE_ENDIAN */ 2949 } 2950 2951 2952 /* 2953 * Scsi response set up for invalid command (command not supported) 2954 * 2955 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 2956 */ 2957 static int 2958 sata_txlt_invalid_command(sata_pkt_txlate_t *spx) 2959 { 2960 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 2961 struct scsi_extended_sense *sense; 2962 2963 scsipkt->pkt_reason = CMD_CMPLT; 2964 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 2965 STATE_SENT_CMD | STATE_GOT_STATUS; 2966 2967 *scsipkt->pkt_scbp = STATUS_CHECK; 2968 2969 sense = sata_arq_sense(spx); 2970 sense->es_key = KEY_ILLEGAL_REQUEST; 2971 sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE; 2972 2973 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 2974 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 2975 2976 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 2977 scsipkt->pkt_comp != NULL) 2978 /* scsi callback required */ 2979 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 2980 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 2981 (void *)spx->txlt_scsi_pkt, 2982 TQ_SLEEP) == NULL) 2983 /* Scheduling the callback failed */ 2984 return (TRAN_BUSY); 2985 return (TRAN_ACCEPT); 2986 } 2987 2988 /* 2989 * Scsi response setup for 2990 * emulated non-data command that requires no action/return data 2991 * 2992 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 2993 */ 2994 static int 2995 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx) 2996 { 2997 int rval; 2998 int reason; 2999 3000 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3001 3002 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3003 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3004 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3005 return (rval); 3006 } 3007 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3008 3009 spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3010 STATE_SENT_CMD | STATE_GOT_STATUS; 3011 spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT; 3012 *(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD; 3013 3014 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3015 "Scsi_pkt completion reason %x\n", 3016 spx->txlt_scsi_pkt->pkt_reason); 3017 3018 if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 && 3019 spx->txlt_scsi_pkt->pkt_comp != NULL) 3020 /* scsi callback required */ 3021 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3022 (task_func_t *)spx->txlt_scsi_pkt->pkt_comp, 3023 (void *)spx->txlt_scsi_pkt, 3024 TQ_SLEEP) == NULL) 3025 /* Scheduling the callback failed */ 3026 return (TRAN_BUSY); 3027 return (TRAN_ACCEPT); 3028 } 3029 3030 3031 /* 3032 * SATA translate command: Inquiry / Identify Device 3033 * Use cached Identify Device data for now, rather than issuing actual 3034 * Device Identify cmd request. If device is detached and re-attached, 3035 * asynchromous event processing should fetch and refresh Identify Device 3036 * data. 3037 * Two VPD pages are supported now: 3038 * Vital Product Data page 3039 * Unit Serial Number page 3040 * 3041 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3042 */ 3043 3044 #define EVPD 1 /* Extended Vital Product Data flag */ 3045 #define CMDDT 2 /* Command Support Data - Obsolete */ 3046 #define INQUIRY_SUP_VPD_PAGE 0 /* Supported VDP Pages Page COde */ 3047 #define INQUIRY_USN_PAGE 0x80 /* Unit Serial Number Page Code */ 3048 #define INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */ 3049 3050 static int 3051 sata_txlt_inquiry(sata_pkt_txlate_t *spx) 3052 { 3053 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3054 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3055 sata_drive_info_t *sdinfo; 3056 struct scsi_extended_sense *sense; 3057 int count; 3058 uint8_t *p; 3059 int i, j; 3060 uint8_t page_buf[0xff]; /* Max length */ 3061 int rval, reason; 3062 3063 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3064 3065 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3066 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3067 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3068 return (rval); 3069 } 3070 3071 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 3072 &spx->txlt_sata_pkt->satapkt_device); 3073 3074 ASSERT(sdinfo != NULL); 3075 3076 scsipkt->pkt_reason = CMD_CMPLT; 3077 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3078 STATE_SENT_CMD | STATE_GOT_STATUS; 3079 3080 /* Reject not supported request */ 3081 if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */ 3082 *scsipkt->pkt_scbp = STATUS_CHECK; 3083 sense = sata_arq_sense(spx); 3084 sense->es_key = KEY_ILLEGAL_REQUEST; 3085 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3086 goto done; 3087 } 3088 3089 /* Valid Inquiry request */ 3090 *scsipkt->pkt_scbp = STATUS_GOOD; 3091 3092 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 3093 3094 /* 3095 * Because it is fully emulated command storing data 3096 * programatically in the specified buffer, release 3097 * preallocated DMA resources before storing data in the buffer, 3098 * so no unwanted DMA sync would take place. 3099 */ 3100 sata_scsi_dmafree(NULL, scsipkt); 3101 3102 if (!(scsipkt->pkt_cdbp[1] & EVPD)) { 3103 /* Standard Inquiry Data request */ 3104 struct scsi_inquiry inq; 3105 unsigned int bufsize; 3106 3107 sata_identdev_to_inquiry(spx->txlt_sata_hba_inst, 3108 sdinfo, (uint8_t *)&inq); 3109 /* Copy no more than requested */ 3110 count = MIN(bp->b_bcount, 3111 sizeof (struct scsi_inquiry)); 3112 bufsize = scsipkt->pkt_cdbp[4]; 3113 bufsize |= scsipkt->pkt_cdbp[3] << 8; 3114 count = MIN(count, bufsize); 3115 bcopy(&inq, bp->b_un.b_addr, count); 3116 3117 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3118 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 3119 bufsize - count : 0; 3120 } else { 3121 /* 3122 * peripheral_qualifier = 0; 3123 * 3124 * We are dealing only with HD and will be 3125 * dealing with CD/DVD devices soon 3126 */ 3127 uint8_t peripheral_device_type = 3128 sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 3129 DTYPE_DIRECT : DTYPE_RODIRECT; 3130 3131 switch ((uint_t)scsipkt->pkt_cdbp[2]) { 3132 case INQUIRY_SUP_VPD_PAGE: 3133 /* 3134 * Request for suported Vital Product Data 3135 * pages - assuming only 2 page codes 3136 * supported 3137 */ 3138 page_buf[0] = peripheral_device_type; 3139 page_buf[1] = INQUIRY_SUP_VPD_PAGE; 3140 page_buf[2] = 0; 3141 page_buf[3] = 2; /* page length */ 3142 page_buf[4] = INQUIRY_SUP_VPD_PAGE; 3143 page_buf[5] = INQUIRY_USN_PAGE; 3144 /* Copy no more than requested */ 3145 count = MIN(bp->b_bcount, 6); 3146 bcopy(page_buf, bp->b_un.b_addr, count); 3147 break; 3148 case INQUIRY_USN_PAGE: 3149 /* 3150 * Request for Unit Serial Number page 3151 */ 3152 page_buf[0] = peripheral_device_type; 3153 page_buf[1] = INQUIRY_USN_PAGE; 3154 page_buf[2] = 0; 3155 page_buf[3] = 20; /* remaining page length */ 3156 p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser); 3157 #ifdef _LITTLE_ENDIAN 3158 swab(p, &page_buf[4], 20); 3159 #else 3160 bcopy(p, &page_buf[4], 20); 3161 #endif 3162 for (i = 0; i < 20; i++) { 3163 if (page_buf[4 + i] == '\0' || 3164 page_buf[4 + i] == '\040') { 3165 break; 3166 } 3167 } 3168 /* 3169 * 'i' contains string length. 3170 * 3171 * Least significant character of the serial 3172 * number shall appear as the last byte, 3173 * according to SBC-3 spec. 3174 */ 3175 p = &page_buf[20 + 4 - 1]; 3176 for (j = i; j > 0; j--, p--) { 3177 *p = *(p - 20 + i); 3178 } 3179 p = &page_buf[4]; 3180 for (j = 20 - i; j > 0; j--) { 3181 *p++ = '\040'; 3182 } 3183 count = MIN(bp->b_bcount, 24); 3184 bcopy(page_buf, bp->b_un.b_addr, count); 3185 break; 3186 3187 case INQUIRY_DEV_IDENTIFICATION_PAGE: 3188 /* 3189 * We may want to implement this page, when 3190 * identifiers are common for SATA devices 3191 * But not now. 3192 */ 3193 /*FALLTHROUGH*/ 3194 3195 default: 3196 /* Request for unsupported VPD page */ 3197 *scsipkt->pkt_scbp = STATUS_CHECK; 3198 sense = sata_arq_sense(spx); 3199 sense->es_key = KEY_ILLEGAL_REQUEST; 3200 sense->es_add_code = 3201 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3202 goto done; 3203 } 3204 } 3205 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3206 scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ? 3207 scsipkt->pkt_cdbp[4] - count : 0; 3208 } 3209 done: 3210 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3211 3212 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3213 "Scsi_pkt completion reason %x\n", 3214 scsipkt->pkt_reason); 3215 3216 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3217 scsipkt->pkt_comp != NULL) { 3218 /* scsi callback required */ 3219 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3220 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3221 TQ_SLEEP) == NULL) 3222 /* Scheduling the callback failed */ 3223 return (TRAN_BUSY); 3224 } 3225 return (TRAN_ACCEPT); 3226 } 3227 3228 /* 3229 * SATA translate command: Request Sense. 3230 * Emulated command (ATA version for SATA hard disks) 3231 * Always NO SENSE, because any sense data should be reported by ARQ sense. 3232 * 3233 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3234 * 3235 * Note: There is a mismatch between already implemented Informational 3236 * Exception Mode Select page 0x1C and this function. 3237 * When MRIE bit is set in page 0x1C, Request Sense is supposed to return 3238 * NO SENSE and set additional sense code to the exception code - this is not 3239 * implemented here. 3240 */ 3241 static int 3242 sata_txlt_request_sense(sata_pkt_txlate_t *spx) 3243 { 3244 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3245 struct scsi_extended_sense sense; 3246 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3247 int rval, reason; 3248 3249 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3250 3251 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3252 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3253 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3254 return (rval); 3255 } 3256 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3257 3258 3259 scsipkt->pkt_reason = CMD_CMPLT; 3260 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3261 STATE_SENT_CMD | STATE_GOT_STATUS; 3262 *scsipkt->pkt_scbp = STATUS_GOOD; 3263 3264 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 3265 /* 3266 * Because it is fully emulated command storing data 3267 * programatically in the specified buffer, release 3268 * preallocated DMA resources before storing data in the buffer, 3269 * so no unwanted DMA sync would take place. 3270 */ 3271 int count = MIN(bp->b_bcount, 3272 sizeof (struct scsi_extended_sense)); 3273 sata_scsi_dmafree(NULL, scsipkt); 3274 bzero(&sense, sizeof (struct scsi_extended_sense)); 3275 sense.es_valid = 0; /* Valid LBA */ 3276 sense.es_class = 7; /* Response code 0x70 - current err */ 3277 sense.es_key = KEY_NO_SENSE; 3278 sense.es_add_len = 6; /* Additional length */ 3279 /* Copy no more than requested */ 3280 bcopy(&sense, bp->b_un.b_addr, count); 3281 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3282 scsipkt->pkt_resid = 0; 3283 } 3284 3285 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3286 "Scsi_pkt completion reason %x\n", 3287 scsipkt->pkt_reason); 3288 3289 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3290 scsipkt->pkt_comp != NULL) 3291 /* scsi callback required */ 3292 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3293 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3294 TQ_SLEEP) == NULL) 3295 /* Scheduling the callback failed */ 3296 return (TRAN_BUSY); 3297 return (TRAN_ACCEPT); 3298 } 3299 3300 /* 3301 * SATA translate command: Test Unit Ready 3302 * At the moment this is an emulated command (ATA version for SATA hard disks). 3303 * May be translated into Check Power Mode command in the future 3304 * 3305 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3306 */ 3307 static int 3308 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx) 3309 { 3310 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3311 struct scsi_extended_sense *sense; 3312 int power_state; 3313 int rval, reason; 3314 3315 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3316 3317 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3318 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3319 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3320 return (rval); 3321 } 3322 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3323 3324 /* At this moment, emulate it rather than execute anything */ 3325 power_state = SATA_PWRMODE_ACTIVE; 3326 3327 scsipkt->pkt_reason = CMD_CMPLT; 3328 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3329 STATE_SENT_CMD | STATE_GOT_STATUS; 3330 3331 switch (power_state) { 3332 case SATA_PWRMODE_ACTIVE: 3333 case SATA_PWRMODE_IDLE: 3334 *scsipkt->pkt_scbp = STATUS_GOOD; 3335 break; 3336 default: 3337 /* PWR mode standby */ 3338 *scsipkt->pkt_scbp = STATUS_CHECK; 3339 sense = sata_arq_sense(spx); 3340 sense->es_key = KEY_NOT_READY; 3341 sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY; 3342 break; 3343 } 3344 3345 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3346 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3347 3348 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3349 scsipkt->pkt_comp != NULL) 3350 /* scsi callback required */ 3351 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3352 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3353 TQ_SLEEP) == NULL) 3354 /* Scheduling the callback failed */ 3355 return (TRAN_BUSY); 3356 3357 return (TRAN_ACCEPT); 3358 } 3359 3360 3361 /* 3362 * SATA translate command: Start Stop Unit 3363 * Translation depends on a command: 3364 * Start Unit translated into Idle Immediate 3365 * Stop Unit translated into Standby Immediate 3366 * Unload Media / NOT SUPPORTED YET 3367 * Load Media / NOT SUPPROTED YET 3368 * Power condition bits are ignored, so is Immediate bit 3369 * Requesting synchronous execution. 3370 * 3371 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 3372 * appropriate values in scsi_pkt fields. 3373 */ 3374 static int 3375 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx) 3376 { 3377 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3378 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 3379 struct scsi_extended_sense *sense; 3380 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 3381 int cport = SATA_TXLT_CPORT(spx); 3382 int rval, reason; 3383 int synch; 3384 3385 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3386 "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1); 3387 3388 mutex_enter(&SATA_CPORT_MUTEX(shi, cport)); 3389 3390 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3391 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3392 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3393 return (rval); 3394 } 3395 3396 if (scsipkt->pkt_cdbp[4] & 2) { 3397 /* Load/Unload Media - invalid request */ 3398 *scsipkt->pkt_scbp = STATUS_CHECK; 3399 sense = sata_arq_sense(spx); 3400 sense->es_key = KEY_ILLEGAL_REQUEST; 3401 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3402 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3403 3404 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3405 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3406 3407 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3408 scsipkt->pkt_comp != NULL) 3409 /* scsi callback required */ 3410 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3411 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3412 TQ_SLEEP) == NULL) 3413 /* Scheduling the callback failed */ 3414 return (TRAN_BUSY); 3415 3416 return (TRAN_ACCEPT); 3417 } 3418 scmd->satacmd_addr_type = 0; 3419 scmd->satacmd_sec_count_lsb = 0; 3420 scmd->satacmd_lba_low_lsb = 0; 3421 scmd->satacmd_lba_mid_lsb = 0; 3422 scmd->satacmd_lba_high_lsb = 0; 3423 scmd->satacmd_features_reg = 0; 3424 scmd->satacmd_device_reg = 0; 3425 scmd->satacmd_status_reg = 0; 3426 if (scsipkt->pkt_cdbp[4] & 1) { 3427 /* Start Unit */ 3428 scmd->satacmd_cmd_reg = SATAC_IDLE_IM; 3429 } else { 3430 /* Stop Unit */ 3431 scmd->satacmd_cmd_reg = SATAC_STANDBY_IM; 3432 } 3433 3434 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 3435 /* Need to set-up a callback function */ 3436 spx->txlt_sata_pkt->satapkt_comp = 3437 sata_txlt_nodata_cmd_completion; 3438 synch = FALSE; 3439 } else { 3440 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH; 3441 synch = TRUE; 3442 } 3443 3444 /* Transfer command to HBA */ 3445 if (sata_hba_start(spx, &rval) != 0) { 3446 /* Pkt not accepted for execution */ 3447 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3448 return (rval); 3449 } 3450 3451 /* 3452 * If execution is non-synchronous, 3453 * a callback function will handle potential errors, translate 3454 * the response and will do a callback to a target driver. 3455 * If it was synchronous, check execution status using the same 3456 * framework callback. 3457 */ 3458 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 3459 if (synch) { 3460 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3461 "synchronous execution status %x\n", 3462 spx->txlt_sata_pkt->satapkt_reason); 3463 3464 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 3465 } 3466 return (TRAN_ACCEPT); 3467 3468 } 3469 3470 3471 /* 3472 * SATA translate command: Read Capacity. 3473 * Emulated command for SATA disks. 3474 * Capacity is retrieved from cached Idenifty Device data. 3475 * Identify Device data shows effective disk capacity, not the native 3476 * capacity, which may be limitted by Set Max Address command. 3477 * This is ATA version for SATA hard disks. 3478 * 3479 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3480 */ 3481 static int 3482 sata_txlt_read_capacity(sata_pkt_txlate_t *spx) 3483 { 3484 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3485 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3486 sata_drive_info_t *sdinfo; 3487 uint64_t val; 3488 uchar_t *rbuf; 3489 int rval, reason; 3490 3491 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3492 "sata_txlt_read_capacity: ", NULL); 3493 3494 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3495 3496 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3497 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3498 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3499 return (rval); 3500 } 3501 3502 scsipkt->pkt_reason = CMD_CMPLT; 3503 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3504 STATE_SENT_CMD | STATE_GOT_STATUS; 3505 *scsipkt->pkt_scbp = STATUS_GOOD; 3506 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 3507 /* 3508 * Because it is fully emulated command storing data 3509 * programatically in the specified buffer, release 3510 * preallocated DMA resources before storing data in the buffer, 3511 * so no unwanted DMA sync would take place. 3512 */ 3513 sata_scsi_dmafree(NULL, scsipkt); 3514 3515 sdinfo = sata_get_device_info( 3516 spx->txlt_sata_hba_inst, 3517 &spx->txlt_sata_pkt->satapkt_device); 3518 /* Last logical block address */ 3519 val = sdinfo->satadrv_capacity - 1; 3520 rbuf = (uchar_t *)bp->b_un.b_addr; 3521 /* Need to swap endians to match scsi format */ 3522 rbuf[0] = (val >> 24) & 0xff; 3523 rbuf[1] = (val >> 16) & 0xff; 3524 rbuf[2] = (val >> 8) & 0xff; 3525 rbuf[3] = val & 0xff; 3526 /* block size - always 512 bytes, for now */ 3527 rbuf[4] = 0; 3528 rbuf[5] = 0; 3529 rbuf[6] = 0x02; 3530 rbuf[7] = 0; 3531 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3532 scsipkt->pkt_resid = 0; 3533 3534 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n", 3535 sdinfo->satadrv_capacity -1); 3536 } 3537 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3538 /* 3539 * If a callback was requested, do it now. 3540 */ 3541 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3542 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3543 3544 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3545 scsipkt->pkt_comp != NULL) 3546 /* scsi callback required */ 3547 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3548 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3549 TQ_SLEEP) == NULL) 3550 /* Scheduling the callback failed */ 3551 return (TRAN_BUSY); 3552 3553 return (TRAN_ACCEPT); 3554 } 3555 3556 /* 3557 * SATA translate command: Mode Sense. 3558 * Translated into appropriate SATA command or emulated. 3559 * Saved Values Page Control (03) are not supported. 3560 * 3561 * NOTE: only caching mode sense page is currently implemented. 3562 * 3563 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 3564 */ 3565 3566 static int 3567 sata_txlt_mode_sense(sata_pkt_txlate_t *spx) 3568 { 3569 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3570 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3571 sata_drive_info_t *sdinfo; 3572 sata_id_t *sata_id; 3573 struct scsi_extended_sense *sense; 3574 int len, bdlen, count, alc_len; 3575 int pc; /* Page Control code */ 3576 uint8_t *buf; /* mode sense buffer */ 3577 int rval, reason; 3578 3579 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3580 "sata_txlt_mode_sense, pc %x page code 0x%02x\n", 3581 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 3582 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 3583 3584 buf = kmem_zalloc(1024, KM_SLEEP); 3585 3586 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3587 3588 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3589 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3590 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3591 kmem_free(buf, 1024); 3592 return (rval); 3593 } 3594 3595 scsipkt->pkt_reason = CMD_CMPLT; 3596 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3597 STATE_SENT_CMD | STATE_GOT_STATUS; 3598 3599 pc = scsipkt->pkt_cdbp[2] >> 6; 3600 3601 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 3602 /* 3603 * Because it is fully emulated command storing data 3604 * programatically in the specified buffer, release 3605 * preallocated DMA resources before storing data in the buffer, 3606 * so no unwanted DMA sync would take place. 3607 */ 3608 sata_scsi_dmafree(NULL, scsipkt); 3609 3610 len = 0; 3611 bdlen = 0; 3612 if (!(scsipkt->pkt_cdbp[1] & 8)) { 3613 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 && 3614 (scsipkt->pkt_cdbp[0] & 0x10)) 3615 bdlen = 16; 3616 else 3617 bdlen = 8; 3618 } 3619 /* Build mode parameter header */ 3620 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 3621 /* 4-byte mode parameter header */ 3622 buf[len++] = 0; /* mode data length */ 3623 buf[len++] = 0; /* medium type */ 3624 buf[len++] = 0; /* dev-specific param */ 3625 buf[len++] = bdlen; /* Block Descriptor length */ 3626 } else { 3627 /* 8-byte mode parameter header */ 3628 buf[len++] = 0; /* mode data length */ 3629 buf[len++] = 0; 3630 buf[len++] = 0; /* medium type */ 3631 buf[len++] = 0; /* dev-specific param */ 3632 if (bdlen == 16) 3633 buf[len++] = 1; /* long lba descriptor */ 3634 else 3635 buf[len++] = 0; 3636 buf[len++] = 0; 3637 buf[len++] = 0; /* Block Descriptor length */ 3638 buf[len++] = bdlen; 3639 } 3640 3641 sdinfo = sata_get_device_info( 3642 spx->txlt_sata_hba_inst, 3643 &spx->txlt_sata_pkt->satapkt_device); 3644 3645 /* Build block descriptor only if not disabled (DBD) */ 3646 if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) { 3647 /* Block descriptor - direct-access device format */ 3648 if (bdlen == 8) { 3649 /* build regular block descriptor */ 3650 buf[len++] = 3651 (sdinfo->satadrv_capacity >> 24) & 0xff; 3652 buf[len++] = 3653 (sdinfo->satadrv_capacity >> 16) & 0xff; 3654 buf[len++] = 3655 (sdinfo->satadrv_capacity >> 8) & 0xff; 3656 buf[len++] = sdinfo->satadrv_capacity & 0xff; 3657 buf[len++] = 0; /* density code */ 3658 buf[len++] = 0; 3659 if (sdinfo->satadrv_type == 3660 SATA_DTYPE_ATADISK) 3661 buf[len++] = 2; 3662 else 3663 /* ATAPI */ 3664 buf[len++] = 8; 3665 buf[len++] = 0; 3666 } else if (bdlen == 16) { 3667 /* Long LBA Accepted */ 3668 /* build long lba block descriptor */ 3669 #ifndef __lock_lint 3670 buf[len++] = 3671 (sdinfo->satadrv_capacity >> 56) & 0xff; 3672 buf[len++] = 3673 (sdinfo->satadrv_capacity >> 48) & 0xff; 3674 buf[len++] = 3675 (sdinfo->satadrv_capacity >> 40) & 0xff; 3676 buf[len++] = 3677 (sdinfo->satadrv_capacity >> 32) & 0xff; 3678 #endif 3679 buf[len++] = 3680 (sdinfo->satadrv_capacity >> 24) & 0xff; 3681 buf[len++] = 3682 (sdinfo->satadrv_capacity >> 16) & 0xff; 3683 buf[len++] = 3684 (sdinfo->satadrv_capacity >> 8) & 0xff; 3685 buf[len++] = sdinfo->satadrv_capacity & 0xff; 3686 buf[len++] = 0; 3687 buf[len++] = 0; /* density code */ 3688 buf[len++] = 0; 3689 buf[len++] = 0; 3690 if (sdinfo->satadrv_type == 3691 SATA_DTYPE_ATADISK) 3692 buf[len++] = 2; 3693 else 3694 /* ATAPI */ 3695 buf[len++] = 8; 3696 buf[len++] = 0; 3697 } 3698 } 3699 3700 sata_id = &sdinfo->satadrv_id; 3701 3702 /* 3703 * Add requested pages. 3704 * Page 3 and 4 are obsolete and we are not supporting them. 3705 * We deal now with: 3706 * caching (read/write cache control). 3707 * We should eventually deal with following mode pages: 3708 * error recovery (0x01), 3709 * power condition (0x1a), 3710 * exception control page (enables SMART) (0x1c), 3711 * enclosure management (ses), 3712 * protocol-specific port mode (port control). 3713 */ 3714 switch (scsipkt->pkt_cdbp[2] & 0x3f) { 3715 case MODEPAGE_RW_ERRRECOV: 3716 /* DAD_MODE_ERR_RECOV */ 3717 /* R/W recovery */ 3718 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 3719 break; 3720 case MODEPAGE_CACHING: 3721 /* DAD_MODE_CACHE */ 3722 /* Reject not supported request for saved parameters */ 3723 if (pc == 3) { 3724 *scsipkt->pkt_scbp = STATUS_CHECK; 3725 sense = sata_arq_sense(spx); 3726 sense->es_key = KEY_ILLEGAL_REQUEST; 3727 sense->es_add_code = 3728 SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED; 3729 goto done; 3730 } 3731 3732 /* caching */ 3733 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 3734 break; 3735 case MODEPAGE_INFO_EXCPT: 3736 /* exception cntrl */ 3737 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 3738 len += sata_build_msense_page_1c(sdinfo, pc, 3739 buf+len); 3740 } 3741 else 3742 goto err; 3743 break; 3744 case MODEPAGE_POWER_COND: 3745 /* DAD_MODE_POWER_COND */ 3746 /* power condition */ 3747 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 3748 break; 3749 3750 case MODEPAGE_ACOUSTIC_MANAG: 3751 /* acoustic management */ 3752 len += sata_build_msense_page_30(sdinfo, pc, buf+len); 3753 break; 3754 case MODEPAGE_ALLPAGES: 3755 /* all pages */ 3756 len += sata_build_msense_page_1(sdinfo, pc, buf+len); 3757 len += sata_build_msense_page_8(sdinfo, pc, buf+len); 3758 len += sata_build_msense_page_1a(sdinfo, pc, buf+len); 3759 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 3760 len += sata_build_msense_page_1c(sdinfo, pc, 3761 buf+len); 3762 } 3763 len += sata_build_msense_page_30(sdinfo, pc, buf+len); 3764 break; 3765 default: 3766 err: 3767 /* Invalid request */ 3768 *scsipkt->pkt_scbp = STATUS_CHECK; 3769 sense = sata_arq_sense(spx); 3770 sense->es_key = KEY_ILLEGAL_REQUEST; 3771 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3772 goto done; 3773 } 3774 3775 /* fix total mode data length */ 3776 if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 3777 /* 4-byte mode parameter header */ 3778 buf[0] = len - 1; /* mode data length */ 3779 } else { 3780 buf[0] = (len -2) >> 8; 3781 buf[1] = (len -2) & 0xff; 3782 } 3783 3784 3785 /* Check allocation length */ 3786 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) { 3787 alc_len = scsipkt->pkt_cdbp[4]; 3788 } else { 3789 alc_len = scsipkt->pkt_cdbp[7]; 3790 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 3791 } 3792 /* 3793 * We do not check for possible parameters truncation 3794 * (alc_len < len) assuming that the target driver works 3795 * correctly. Just avoiding overrun. 3796 * Copy no more than requested and possible, buffer-wise. 3797 */ 3798 count = MIN(alc_len, len); 3799 count = MIN(bp->b_bcount, count); 3800 bcopy(buf, bp->b_un.b_addr, count); 3801 3802 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3803 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 3804 } 3805 *scsipkt->pkt_scbp = STATUS_GOOD; 3806 done: 3807 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3808 (void) kmem_free(buf, 1024); 3809 3810 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3811 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 3812 3813 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3814 scsipkt->pkt_comp != NULL) 3815 /* scsi callback required */ 3816 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 3817 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 3818 TQ_SLEEP) == NULL) 3819 /* Scheduling the callback failed */ 3820 return (TRAN_BUSY); 3821 3822 return (TRAN_ACCEPT); 3823 } 3824 3825 3826 /* 3827 * SATA translate command: Mode Select. 3828 * Translated into appropriate SATA command or emulated. 3829 * Saving parameters is not supported. 3830 * Changing device capacity is not supported (although theoretically 3831 * possible by executing SET FEATURES/SET MAX ADDRESS) 3832 * 3833 * Assumption is that the target driver is working correctly. 3834 * 3835 * More than one SATA command may be executed to perform operations specified 3836 * by mode select pages. The first error terminates further execution. 3837 * Operations performed successully are not backed-up in such case. 3838 * 3839 * NOTE: Implemented pages: 3840 * - caching page 3841 * - informational exception page 3842 * - acoustic management page 3843 * Caching setup is remembered so it could be re-stored in case of 3844 * an unexpected device reset. 3845 * 3846 * Returns TRAN_XXXX. 3847 * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields. 3848 */ 3849 3850 static int 3851 sata_txlt_mode_select(sata_pkt_txlate_t *spx) 3852 { 3853 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 3854 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 3855 struct scsi_extended_sense *sense; 3856 int len, pagelen, count, pllen; 3857 uint8_t *buf; /* mode select buffer */ 3858 int rval, stat, reason; 3859 uint_t nointr_flag; 3860 int dmod = 0; 3861 3862 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 3863 "sata_txlt_mode_select, pc %x page code 0x%02x\n", 3864 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 3865 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 3866 3867 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 3868 3869 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 3870 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 3871 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3872 return (rval); 3873 } 3874 /* 3875 * If in interrupt context, reject this packet because it may result 3876 * in issuing a synchronous command to HBA. 3877 */ 3878 if (servicing_interrupt()) { 3879 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 3880 "sata_txlt_mode_select: rejecting command because " 3881 "of interrupt context\n", NULL); 3882 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 3883 return (TRAN_BUSY); 3884 } 3885 3886 rval = TRAN_ACCEPT; 3887 3888 scsipkt->pkt_reason = CMD_CMPLT; 3889 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 3890 STATE_SENT_CMD | STATE_GOT_STATUS; 3891 nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR; 3892 3893 /* Reject not supported request */ 3894 if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */ 3895 *scsipkt->pkt_scbp = STATUS_CHECK; 3896 sense = sata_arq_sense(spx); 3897 sense->es_key = KEY_ILLEGAL_REQUEST; 3898 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3899 goto done; 3900 } 3901 3902 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 3903 pllen = scsipkt->pkt_cdbp[4]; 3904 } else { 3905 pllen = scsipkt->pkt_cdbp[7]; 3906 pllen = (pllen << 8) | scsipkt->pkt_cdbp[7]; 3907 } 3908 3909 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 3910 3911 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) { 3912 buf = (uint8_t *)bp->b_un.b_addr; 3913 count = MIN(bp->b_bcount, pllen); 3914 scsipkt->pkt_state |= STATE_XFERRED_DATA; 3915 scsipkt->pkt_resid = 0; 3916 pllen = count; 3917 3918 /* 3919 * Check the header to skip the block descriptor(s) - we 3920 * do not support setting device capacity. 3921 * Existing macros do not recognize long LBA dscriptor, 3922 * hence manual calculation. 3923 */ 3924 if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) { 3925 /* 6-bytes CMD, 4 bytes header */ 3926 if (count <= 4) 3927 goto done; /* header only */ 3928 len = buf[3] + 4; 3929 } else { 3930 /* 10-bytes CMD, 8 bytes header */ 3931 if (count <= 8) 3932 goto done; /* header only */ 3933 len = buf[6]; 3934 len = (len << 8) + buf[7] + 8; 3935 } 3936 if (len >= count) 3937 goto done; /* header + descriptor(s) only */ 3938 3939 pllen -= len; /* remaining data length */ 3940 3941 /* 3942 * We may be executing SATA command and want to execute it 3943 * in SYNCH mode, regardless of scsi_pkt setting. 3944 * Save scsi_pkt setting and indicate SYNCH mode 3945 */ 3946 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 3947 scsipkt->pkt_comp != NULL) { 3948 scsipkt->pkt_flags |= FLAG_NOINTR; 3949 } 3950 spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH; 3951 3952 /* 3953 * len is now the offset to a first mode select page 3954 * Process all pages 3955 */ 3956 while (pllen > 0) { 3957 switch ((int)buf[len]) { 3958 case MODEPAGE_CACHING: 3959 /* No support for SP (saving) */ 3960 if (scsipkt->pkt_cdbp[1] & 0x01) { 3961 *scsipkt->pkt_scbp = STATUS_CHECK; 3962 sense = sata_arq_sense(spx); 3963 sense->es_key = KEY_ILLEGAL_REQUEST; 3964 sense->es_add_code = 3965 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 3966 goto done; 3967 } 3968 stat = sata_mode_select_page_8(spx, 3969 (struct mode_cache_scsi3 *)&buf[len], 3970 pllen, &pagelen, &rval, &dmod); 3971 /* 3972 * The pagelen value indicates the number of 3973 * parameter bytes already processed. 3974 * The rval is the return value from 3975 * sata_tran_start(). 3976 * The stat indicates the overall status of 3977 * the operation(s). 3978 */ 3979 if (stat != SATA_SUCCESS) 3980 /* 3981 * Page processing did not succeed - 3982 * all error info is already set-up, 3983 * just return 3984 */ 3985 pllen = 0; /* this breaks the loop */ 3986 else { 3987 len += pagelen; 3988 pllen -= pagelen; 3989 } 3990 break; 3991 3992 case MODEPAGE_INFO_EXCPT: 3993 stat = sata_mode_select_page_1c(spx, 3994 (struct mode_info_excpt_page *)&buf[len], 3995 pllen, &pagelen, &rval, &dmod); 3996 /* 3997 * The pagelen value indicates the number of 3998 * parameter bytes already processed. 3999 * The rval is the return value from 4000 * sata_tran_start(). 4001 * The stat indicates the overall status of 4002 * the operation(s). 4003 */ 4004 if (stat != SATA_SUCCESS) 4005 /* 4006 * Page processing did not succeed - 4007 * all error info is already set-up, 4008 * just return 4009 */ 4010 pllen = 0; /* this breaks the loop */ 4011 else { 4012 len += pagelen; 4013 pllen -= pagelen; 4014 } 4015 break; 4016 4017 case MODEPAGE_ACOUSTIC_MANAG: 4018 stat = sata_mode_select_page_30(spx, 4019 (struct mode_acoustic_management *) 4020 &buf[len], pllen, &pagelen, &rval, &dmod); 4021 /* 4022 * The pagelen value indicates the number of 4023 * parameter bytes already processed. 4024 * The rval is the return value from 4025 * sata_tran_start(). 4026 * The stat indicates the overall status of 4027 * the operation(s). 4028 */ 4029 if (stat != SATA_SUCCESS) 4030 /* 4031 * Page processing did not succeed - 4032 * all error info is already set-up, 4033 * just return 4034 */ 4035 pllen = 0; /* this breaks the loop */ 4036 else { 4037 len += pagelen; 4038 pllen -= pagelen; 4039 } 4040 4041 break; 4042 default: 4043 *scsipkt->pkt_scbp = STATUS_CHECK; 4044 sense = sata_arq_sense(spx); 4045 sense->es_key = KEY_ILLEGAL_REQUEST; 4046 sense->es_add_code = 4047 SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 4048 goto done; 4049 } 4050 } 4051 } 4052 done: 4053 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4054 /* 4055 * If device parameters were modified, fetch and store the new 4056 * Identify Device data. Since port mutex could have been released 4057 * for accessing HBA driver, we need to re-check device existence. 4058 */ 4059 if (dmod != 0) { 4060 sata_drive_info_t new_sdinfo, *sdinfo; 4061 int rv = 0; 4062 4063 /* 4064 * Following statement has to be changed if this function is 4065 * used for devices other than SATA hard disks. 4066 */ 4067 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK; 4068 4069 new_sdinfo.satadrv_addr = 4070 spx->txlt_sata_pkt->satapkt_device.satadev_addr; 4071 rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst, 4072 &new_sdinfo); 4073 4074 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4075 /* 4076 * Since port mutex could have been released when 4077 * accessing HBA driver, we need to re-check that the 4078 * framework still holds the device info structure. 4079 */ 4080 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4081 &spx->txlt_sata_pkt->satapkt_device); 4082 if (sdinfo != NULL) { 4083 /* 4084 * Device still has info structure in the 4085 * sata framework. Copy newly fetched info 4086 */ 4087 if (rv == 0) { 4088 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 4089 sata_save_drive_settings(sdinfo); 4090 } else { 4091 /* 4092 * Could not fetch new data - invalidate 4093 * sata_drive_info. That makes device 4094 * unusable. 4095 */ 4096 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 4097 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 4098 } 4099 } 4100 if (rv != 0 || sdinfo == NULL) { 4101 /* 4102 * This changes the overall mode select completion 4103 * reason to a failed one !!!!! 4104 */ 4105 *scsipkt->pkt_scbp = STATUS_CHECK; 4106 sense = sata_arq_sense(spx); 4107 scsipkt->pkt_reason = CMD_INCOMPLETE; 4108 rval = TRAN_ACCEPT; 4109 } 4110 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4111 } 4112 /* Restore the scsi pkt flags */ 4113 scsipkt->pkt_flags &= ~FLAG_NOINTR; 4114 scsipkt->pkt_flags |= nointr_flag; 4115 4116 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4117 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4118 4119 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4120 scsipkt->pkt_comp != NULL) 4121 /* scsi callback required */ 4122 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4123 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4124 TQ_SLEEP) == NULL) 4125 /* Scheduling the callback failed */ 4126 return (TRAN_BUSY); 4127 4128 return (rval); 4129 } 4130 4131 4132 4133 /* 4134 * Translate command: Log Sense 4135 */ 4136 static int 4137 sata_txlt_log_sense(sata_pkt_txlate_t *spx) 4138 { 4139 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4140 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4141 sata_drive_info_t *sdinfo; 4142 struct scsi_extended_sense *sense; 4143 int len, count, alc_len; 4144 int pc; /* Page Control code */ 4145 int page_code; /* Page code */ 4146 uint8_t *buf; /* log sense buffer */ 4147 int rval, reason; 4148 #define MAX_LOG_SENSE_PAGE_SIZE 512 4149 4150 SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4151 "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n", 4152 spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6, 4153 spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f); 4154 4155 buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP); 4156 4157 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4158 4159 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 4160 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4161 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4162 kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 4163 return (rval); 4164 } 4165 /* 4166 * If in interrupt context, reject this packet because it may result 4167 * in issuing a synchronous command to HBA. 4168 */ 4169 if (servicing_interrupt()) { 4170 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 4171 "sata_log_sense: rejecting command because " 4172 "of interrupt context\n", NULL); 4173 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4174 kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 4175 return (TRAN_BUSY); 4176 } 4177 4178 scsipkt->pkt_reason = CMD_CMPLT; 4179 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4180 STATE_SENT_CMD | STATE_GOT_STATUS; 4181 4182 pc = scsipkt->pkt_cdbp[2] >> 6; 4183 page_code = scsipkt->pkt_cdbp[2] & 0x3f; 4184 4185 /* Reject not supported request for all but cumulative values */ 4186 switch (pc) { 4187 case PC_CUMULATIVE_VALUES: 4188 break; 4189 default: 4190 *scsipkt->pkt_scbp = STATUS_CHECK; 4191 sense = sata_arq_sense(spx); 4192 sense->es_key = KEY_ILLEGAL_REQUEST; 4193 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4194 goto done; 4195 } 4196 4197 switch (page_code) { 4198 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 4199 case PAGE_CODE_SELF_TEST_RESULTS: 4200 case PAGE_CODE_INFORMATION_EXCEPTIONS: 4201 case PAGE_CODE_SMART_READ_DATA: 4202 break; 4203 default: 4204 *scsipkt->pkt_scbp = STATUS_CHECK; 4205 sense = sata_arq_sense(spx); 4206 sense->es_key = KEY_ILLEGAL_REQUEST; 4207 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4208 goto done; 4209 } 4210 4211 if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) { 4212 /* 4213 * Because log sense uses local buffers for data retrieval from 4214 * the devices and sets the data programatically in the 4215 * original specified buffer, release preallocated DMA 4216 * resources before storing data in the original buffer, 4217 * so no unwanted DMA sync would take place. 4218 */ 4219 sata_id_t *sata_id; 4220 4221 sata_scsi_dmafree(NULL, scsipkt); 4222 4223 len = 0; 4224 4225 /* Build log parameter header */ 4226 buf[len++] = page_code; /* page code as in the CDB */ 4227 buf[len++] = 0; /* reserved */ 4228 buf[len++] = 0; /* Zero out page length for now (MSB) */ 4229 buf[len++] = 0; /* (LSB) */ 4230 4231 sdinfo = sata_get_device_info( 4232 spx->txlt_sata_hba_inst, 4233 &spx->txlt_sata_pkt->satapkt_device); 4234 4235 /* 4236 * Add requested pages. 4237 */ 4238 switch (page_code) { 4239 case PAGE_CODE_GET_SUPPORTED_LOG_PAGES: 4240 len = sata_build_lsense_page_0(sdinfo, buf + len); 4241 break; 4242 case PAGE_CODE_SELF_TEST_RESULTS: 4243 sata_id = &sdinfo->satadrv_id; 4244 if ((! (sata_id->ai_cmdset84 & 4245 SATA_SMART_SELF_TEST_SUPPORTED)) || 4246 (! (sata_id->ai_features87 & 4247 SATA_SMART_SELF_TEST_SUPPORTED))) { 4248 *scsipkt->pkt_scbp = STATUS_CHECK; 4249 sense = sata_arq_sense(spx); 4250 sense->es_key = KEY_ILLEGAL_REQUEST; 4251 sense->es_add_code = 4252 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4253 4254 goto done; 4255 } 4256 len = sata_build_lsense_page_10(sdinfo, buf + len, 4257 spx->txlt_sata_hba_inst); 4258 break; 4259 case PAGE_CODE_INFORMATION_EXCEPTIONS: 4260 sata_id = &sdinfo->satadrv_id; 4261 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 4262 *scsipkt->pkt_scbp = STATUS_CHECK; 4263 sense = sata_arq_sense(spx); 4264 sense->es_key = KEY_ILLEGAL_REQUEST; 4265 sense->es_add_code = 4266 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4267 4268 goto done; 4269 } 4270 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 4271 *scsipkt->pkt_scbp = STATUS_CHECK; 4272 sense = sata_arq_sense(spx); 4273 sense->es_key = KEY_ABORTED_COMMAND; 4274 sense->es_add_code = 4275 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 4276 sense->es_qual_code = 4277 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 4278 4279 goto done; 4280 } 4281 4282 len = sata_build_lsense_page_2f(sdinfo, buf + len, 4283 spx->txlt_sata_hba_inst); 4284 break; 4285 case PAGE_CODE_SMART_READ_DATA: 4286 sata_id = &sdinfo->satadrv_id; 4287 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 4288 *scsipkt->pkt_scbp = STATUS_CHECK; 4289 sense = sata_arq_sense(spx); 4290 sense->es_key = KEY_ILLEGAL_REQUEST; 4291 sense->es_add_code = 4292 SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4293 4294 goto done; 4295 } 4296 if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 4297 *scsipkt->pkt_scbp = STATUS_CHECK; 4298 sense = sata_arq_sense(spx); 4299 sense->es_key = KEY_ABORTED_COMMAND; 4300 sense->es_add_code = 4301 SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED; 4302 sense->es_qual_code = 4303 SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED; 4304 4305 goto done; 4306 } 4307 4308 /* This page doesn't include a page header */ 4309 len = sata_build_lsense_page_30(sdinfo, buf, 4310 spx->txlt_sata_hba_inst); 4311 goto no_header; 4312 default: 4313 /* Invalid request */ 4314 *scsipkt->pkt_scbp = STATUS_CHECK; 4315 sense = sata_arq_sense(spx); 4316 sense->es_key = KEY_ILLEGAL_REQUEST; 4317 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 4318 goto done; 4319 } 4320 4321 /* set parameter log sense data length */ 4322 buf[2] = len >> 8; /* log sense length (MSB) */ 4323 buf[3] = len & 0xff; /* log sense length (LSB) */ 4324 4325 len += SCSI_LOG_PAGE_HDR_LEN; 4326 ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE); 4327 4328 no_header: 4329 /* Check allocation length */ 4330 alc_len = scsipkt->pkt_cdbp[7]; 4331 alc_len = (len << 8) | scsipkt->pkt_cdbp[8]; 4332 4333 /* 4334 * We do not check for possible parameters truncation 4335 * (alc_len < len) assuming that the target driver works 4336 * correctly. Just avoiding overrun. 4337 * Copy no more than requested and possible, buffer-wise. 4338 */ 4339 count = MIN(alc_len, len); 4340 count = MIN(bp->b_bcount, count); 4341 bcopy(buf, bp->b_un.b_addr, count); 4342 4343 scsipkt->pkt_state |= STATE_XFERRED_DATA; 4344 scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0; 4345 } 4346 *scsipkt->pkt_scbp = STATUS_GOOD; 4347 done: 4348 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4349 (void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE); 4350 4351 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4352 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 4353 4354 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 4355 scsipkt->pkt_comp != NULL) 4356 /* scsi callback required */ 4357 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 4358 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 4359 TQ_SLEEP) == NULL) 4360 /* Scheduling the callback failed */ 4361 return (TRAN_BUSY); 4362 4363 return (TRAN_ACCEPT); 4364 } 4365 4366 /* 4367 * Translate command: Log Select 4368 * Not implemented at this time - returns invalid command response. 4369 */ 4370 static int 4371 sata_txlt_log_select(sata_pkt_txlate_t *spx) 4372 { 4373 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4374 "sata_txlt_log_select\n", NULL); 4375 4376 return (sata_txlt_invalid_command(spx)); 4377 } 4378 4379 4380 /* 4381 * Translate command: Read (various types). 4382 * Translated into appropriate type of ATA READ command 4383 * for SATA hard disks. 4384 * Both the device capabilities and requested operation mode are 4385 * considered. 4386 * 4387 * Following scsi cdb fields are ignored: 4388 * rdprotect, dpo, fua, fua_nv, group_number. 4389 * 4390 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 4391 * enable variable sata_func_enable), the capability of the controller and 4392 * capability of a device are checked and if both support queueing, read 4393 * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT 4394 * command rather than plain READ_XXX command. 4395 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 4396 * both the controller and device suport such functionality, the read 4397 * request will be translated to READ_FPDMA_QUEUED command. 4398 * In both cases the maximum queue depth is derived as minimum of: 4399 * HBA capability,device capability and sata_max_queue_depth variable setting. 4400 * The value passed to HBA driver is decremented by 1, because only 5 bits are 4401 * used to pass max queue depth value, and the maximum possible queue depth 4402 * is 32. 4403 * 4404 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 4405 * appropriate values in scsi_pkt fields. 4406 */ 4407 static int 4408 sata_txlt_read(sata_pkt_txlate_t *spx) 4409 { 4410 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4411 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4412 sata_drive_info_t *sdinfo; 4413 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 4414 int cport = SATA_TXLT_CPORT(spx); 4415 uint16_t sec_count; 4416 uint64_t lba; 4417 int rval, reason; 4418 int synch; 4419 4420 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4421 4422 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 4423 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4424 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4425 return (rval); 4426 } 4427 4428 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4429 &spx->txlt_sata_pkt->satapkt_device); 4430 4431 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 4432 /* 4433 * Extract LBA and sector count from scsi CDB. 4434 */ 4435 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 4436 case SCMD_READ: 4437 /* 6-byte scsi read cmd : 0x08 */ 4438 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 4439 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 4440 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4441 sec_count = scsipkt->pkt_cdbp[4]; 4442 /* sec_count 0 will be interpreted as 256 by a device */ 4443 break; 4444 case SCMD_READ_G1: 4445 /* 10-bytes scsi read command : 0x28 */ 4446 lba = scsipkt->pkt_cdbp[2]; 4447 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4448 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 4449 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 4450 sec_count = scsipkt->pkt_cdbp[7]; 4451 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 4452 break; 4453 case SCMD_READ_G5: 4454 /* 12-bytes scsi read command : 0xA8 */ 4455 lba = scsipkt->pkt_cdbp[2]; 4456 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4457 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 4458 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 4459 sec_count = scsipkt->pkt_cdbp[6]; 4460 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 4461 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 4462 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 4463 break; 4464 case SCMD_READ_G4: 4465 /* 16-bytes scsi read command : 0x88 */ 4466 lba = scsipkt->pkt_cdbp[2]; 4467 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4468 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 4469 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 4470 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 4471 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 4472 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 4473 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 4474 sec_count = scsipkt->pkt_cdbp[10]; 4475 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 4476 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 4477 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 4478 break; 4479 default: 4480 /* Unsupported command */ 4481 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4482 return (sata_txlt_invalid_command(spx)); 4483 } 4484 4485 /* 4486 * Check if specified address exceeds device capacity 4487 */ 4488 if ((lba >= sdinfo->satadrv_capacity) || 4489 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 4490 /* LBA out of range */ 4491 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4492 return (sata_txlt_lba_out_of_range(spx)); 4493 } 4494 4495 /* 4496 * For zero-length transfer, emulate good completion of the command 4497 * (reasons for rejecting the command were already checked). 4498 * No DMA resources were allocated. 4499 */ 4500 if (spx->txlt_dma_cookie_list == NULL) { 4501 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4502 return (sata_emul_rw_completion(spx)); 4503 } 4504 4505 /* 4506 * Build cmd block depending on the device capability and 4507 * requested operation mode. 4508 * Do not bother with non-dma mode - we are working only with 4509 * devices supporting DMA. 4510 */ 4511 scmd->satacmd_addr_type = ATA_ADDR_LBA; 4512 scmd->satacmd_device_reg = SATA_ADH_LBA; 4513 scmd->satacmd_cmd_reg = SATAC_READ_DMA; 4514 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 4515 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 4516 scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT; 4517 scmd->satacmd_sec_count_msb = sec_count >> 8; 4518 #ifndef __lock_lint 4519 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 4520 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 4521 scmd->satacmd_lba_high_msb = lba >> 40; 4522 #endif 4523 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 4524 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 4525 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 4526 } 4527 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 4528 scmd->satacmd_lba_low_lsb = lba & 0xff; 4529 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 4530 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 4531 scmd->satacmd_features_reg = 0; 4532 scmd->satacmd_status_reg = 0; 4533 scmd->satacmd_error_reg = 0; 4534 4535 /* 4536 * Check if queueing commands should be used and switch 4537 * to appropriate command if possible 4538 */ 4539 if (sata_func_enable & SATA_ENABLE_QUEUING) { 4540 boolean_t using_queuing; 4541 4542 /* Queuing supported by controller and device? */ 4543 if ((sata_func_enable & SATA_ENABLE_NCQ) && 4544 (sdinfo->satadrv_features_support & 4545 SATA_DEV_F_NCQ) && 4546 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 4547 SATA_CTLF_NCQ)) { 4548 using_queuing = B_TRUE; 4549 4550 /* NCQ supported - use FPDMA READ */ 4551 scmd->satacmd_cmd_reg = 4552 SATAC_READ_FPDMA_QUEUED; 4553 scmd->satacmd_features_reg_ext = 4554 scmd->satacmd_sec_count_msb; 4555 scmd->satacmd_sec_count_msb = 0; 4556 } else if ((sdinfo->satadrv_features_support & 4557 SATA_DEV_F_TCQ) && 4558 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 4559 SATA_CTLF_QCMD)) { 4560 using_queuing = B_TRUE; 4561 4562 /* Legacy queueing */ 4563 if (sdinfo->satadrv_features_support & 4564 SATA_DEV_F_LBA48) { 4565 scmd->satacmd_cmd_reg = 4566 SATAC_READ_DMA_QUEUED_EXT; 4567 scmd->satacmd_features_reg_ext = 4568 scmd->satacmd_sec_count_msb; 4569 scmd->satacmd_sec_count_msb = 0; 4570 } else { 4571 scmd->satacmd_cmd_reg = 4572 SATAC_READ_DMA_QUEUED; 4573 } 4574 } else /* NCQ nor legacy queuing not supported */ 4575 using_queuing = B_FALSE; 4576 4577 /* 4578 * If queuing, the sector count goes in the features register 4579 * and the secount count will contain the tag. 4580 */ 4581 if (using_queuing) { 4582 scmd->satacmd_features_reg = 4583 scmd->satacmd_sec_count_lsb; 4584 scmd->satacmd_sec_count_lsb = 0; 4585 scmd->satacmd_flags.sata_queued = B_TRUE; 4586 4587 /* Set-up maximum queue depth */ 4588 scmd->satacmd_flags.sata_max_queue_depth = 4589 sdinfo->satadrv_max_queue_depth - 1; 4590 } else if (sdinfo->satadrv_features_enabled & 4591 SATA_DEV_F_E_UNTAGGED_QING) { 4592 /* 4593 * Although NCQ/TCQ is not enabled, untagged queuing 4594 * may be still used. 4595 * Set-up the maximum untagged queue depth. 4596 * Use controller's queue depth from sata_hba_tran. 4597 * SATA HBA drivers may ignore this value and rely on 4598 * the internal limits.For drivers that do not 4599 * ignore untaged queue depth, limit the value to 4600 * SATA_MAX_QUEUE_DEPTH (32), as this is the 4601 * largest value that can be passed via 4602 * satacmd_flags.sata_max_queue_depth. 4603 */ 4604 scmd->satacmd_flags.sata_max_queue_depth = 4605 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ? 4606 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1; 4607 4608 } else { 4609 scmd->satacmd_flags.sata_max_queue_depth = 0; 4610 } 4611 } else 4612 scmd->satacmd_flags.sata_max_queue_depth = 0; 4613 4614 SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst, 4615 "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n", 4616 scmd->satacmd_cmd_reg, lba, sec_count); 4617 4618 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 4619 /* Need callback function */ 4620 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 4621 synch = FALSE; 4622 } else 4623 synch = TRUE; 4624 4625 /* Transfer command to HBA */ 4626 if (sata_hba_start(spx, &rval) != 0) { 4627 /* Pkt not accepted for execution */ 4628 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4629 return (rval); 4630 } 4631 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4632 /* 4633 * If execution is non-synchronous, 4634 * a callback function will handle potential errors, translate 4635 * the response and will do a callback to a target driver. 4636 * If it was synchronous, check execution status using the same 4637 * framework callback. 4638 */ 4639 if (synch) { 4640 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4641 "synchronous execution status %x\n", 4642 spx->txlt_sata_pkt->satapkt_reason); 4643 sata_txlt_rw_completion(spx->txlt_sata_pkt); 4644 } 4645 return (TRAN_ACCEPT); 4646 } 4647 4648 4649 /* 4650 * SATA translate command: Write (various types) 4651 * Translated into appropriate type of ATA WRITE command 4652 * for SATA hard disks. 4653 * Both the device capabilities and requested operation mode are 4654 * considered. 4655 * 4656 * Following scsi cdb fields are ignored: 4657 * rwprotect, dpo, fua, fua_nv, group_number. 4658 * 4659 * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework 4660 * enable variable sata_func_enable), the capability of the controller and 4661 * capability of a device are checked and if both support queueing, write 4662 * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT 4663 * command rather than plain WRITE_XXX command. 4664 * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and 4665 * both the controller and device suport such functionality, the write 4666 * request will be translated to WRITE_FPDMA_QUEUED command. 4667 * In both cases the maximum queue depth is derived as minimum of: 4668 * HBA capability,device capability and sata_max_queue_depth variable setting. 4669 * The value passed to HBA driver is decremented by 1, because only 5 bits are 4670 * used to pass max queue depth value, and the maximum possible queue depth 4671 * is 32. 4672 * 4673 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 4674 * appropriate values in scsi_pkt fields. 4675 */ 4676 static int 4677 sata_txlt_write(sata_pkt_txlate_t *spx) 4678 { 4679 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4680 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4681 sata_drive_info_t *sdinfo; 4682 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 4683 int cport = SATA_TXLT_CPORT(spx); 4684 uint16_t sec_count; 4685 uint64_t lba; 4686 int rval, reason; 4687 int synch; 4688 4689 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4690 4691 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 4692 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 4693 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4694 return (rval); 4695 } 4696 4697 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 4698 &spx->txlt_sata_pkt->satapkt_device); 4699 4700 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 4701 /* 4702 * Extract LBA and sector count from scsi CDB 4703 */ 4704 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 4705 case SCMD_WRITE: 4706 /* 6-byte scsi read cmd : 0x0A */ 4707 lba = (scsipkt->pkt_cdbp[1] & 0x1f); 4708 lba = (lba << 8) | scsipkt->pkt_cdbp[2]; 4709 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4710 sec_count = scsipkt->pkt_cdbp[4]; 4711 /* sec_count 0 will be interpreted as 256 by a device */ 4712 break; 4713 case SCMD_WRITE_G1: 4714 /* 10-bytes scsi write command : 0x2A */ 4715 lba = scsipkt->pkt_cdbp[2]; 4716 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4717 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 4718 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 4719 sec_count = scsipkt->pkt_cdbp[7]; 4720 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 4721 break; 4722 case SCMD_WRITE_G5: 4723 /* 12-bytes scsi read command : 0xAA */ 4724 lba = scsipkt->pkt_cdbp[2]; 4725 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4726 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 4727 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 4728 sec_count = scsipkt->pkt_cdbp[6]; 4729 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7]; 4730 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8]; 4731 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9]; 4732 break; 4733 case SCMD_WRITE_G4: 4734 /* 16-bytes scsi write command : 0x8A */ 4735 lba = scsipkt->pkt_cdbp[2]; 4736 lba = (lba << 8) | scsipkt->pkt_cdbp[3]; 4737 lba = (lba << 8) | scsipkt->pkt_cdbp[4]; 4738 lba = (lba << 8) | scsipkt->pkt_cdbp[5]; 4739 lba = (lba << 8) | scsipkt->pkt_cdbp[6]; 4740 lba = (lba << 8) | scsipkt->pkt_cdbp[7]; 4741 lba = (lba << 8) | scsipkt->pkt_cdbp[8]; 4742 lba = (lba << 8) | scsipkt->pkt_cdbp[9]; 4743 sec_count = scsipkt->pkt_cdbp[10]; 4744 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11]; 4745 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12]; 4746 sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13]; 4747 break; 4748 default: 4749 /* Unsupported command */ 4750 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4751 return (sata_txlt_invalid_command(spx)); 4752 } 4753 4754 /* 4755 * Check if specified address and length exceeds device capacity 4756 */ 4757 if ((lba >= sdinfo->satadrv_capacity) || 4758 ((lba + sec_count) > sdinfo->satadrv_capacity)) { 4759 /* LBA out of range */ 4760 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4761 return (sata_txlt_lba_out_of_range(spx)); 4762 } 4763 4764 /* 4765 * For zero-length transfer, emulate good completion of the command 4766 * (reasons for rejecting the command were already checked). 4767 * No DMA resources were allocated. 4768 */ 4769 if (spx->txlt_dma_cookie_list == NULL) { 4770 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4771 return (sata_emul_rw_completion(spx)); 4772 } 4773 4774 /* 4775 * Build cmd block depending on the device capability and 4776 * requested operation mode. 4777 * Do not bother with non-dma mode- we are working only with 4778 * devices supporting DMA. 4779 */ 4780 scmd->satacmd_addr_type = ATA_ADDR_LBA; 4781 scmd->satacmd_device_reg = SATA_ADH_LBA; 4782 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA; 4783 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 4784 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 4785 scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT; 4786 scmd->satacmd_sec_count_msb = sec_count >> 8; 4787 scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff; 4788 #ifndef __lock_lint 4789 scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff; 4790 scmd->satacmd_lba_high_msb = lba >> 40; 4791 #endif 4792 } else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) { 4793 scmd->satacmd_addr_type = ATA_ADDR_LBA28; 4794 scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf); 4795 } 4796 scmd->satacmd_sec_count_lsb = sec_count & 0xff; 4797 scmd->satacmd_lba_low_lsb = lba & 0xff; 4798 scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff; 4799 scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff; 4800 scmd->satacmd_features_reg = 0; 4801 scmd->satacmd_status_reg = 0; 4802 scmd->satacmd_error_reg = 0; 4803 4804 /* 4805 * Check if queueing commands should be used and switch 4806 * to appropriate command if possible 4807 */ 4808 if (sata_func_enable & SATA_ENABLE_QUEUING) { 4809 boolean_t using_queuing; 4810 4811 /* Queuing supported by controller and device? */ 4812 if ((sata_func_enable & SATA_ENABLE_NCQ) && 4813 (sdinfo->satadrv_features_support & 4814 SATA_DEV_F_NCQ) && 4815 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 4816 SATA_CTLF_NCQ)) { 4817 using_queuing = B_TRUE; 4818 4819 /* NCQ supported - use FPDMA WRITE */ 4820 scmd->satacmd_cmd_reg = 4821 SATAC_WRITE_FPDMA_QUEUED; 4822 scmd->satacmd_features_reg_ext = 4823 scmd->satacmd_sec_count_msb; 4824 scmd->satacmd_sec_count_msb = 0; 4825 } else if ((sdinfo->satadrv_features_support & 4826 SATA_DEV_F_TCQ) && 4827 (SATA_FEATURES(spx->txlt_sata_hba_inst) & 4828 SATA_CTLF_QCMD)) { 4829 using_queuing = B_TRUE; 4830 4831 /* Legacy queueing */ 4832 if (sdinfo->satadrv_features_support & 4833 SATA_DEV_F_LBA48) { 4834 scmd->satacmd_cmd_reg = 4835 SATAC_WRITE_DMA_QUEUED_EXT; 4836 scmd->satacmd_features_reg_ext = 4837 scmd->satacmd_sec_count_msb; 4838 scmd->satacmd_sec_count_msb = 0; 4839 } else { 4840 scmd->satacmd_cmd_reg = 4841 SATAC_WRITE_DMA_QUEUED; 4842 } 4843 } else /* NCQ nor legacy queuing not supported */ 4844 using_queuing = B_FALSE; 4845 4846 if (using_queuing) { 4847 scmd->satacmd_features_reg = 4848 scmd->satacmd_sec_count_lsb; 4849 scmd->satacmd_sec_count_lsb = 0; 4850 scmd->satacmd_flags.sata_queued = B_TRUE; 4851 /* Set-up maximum queue depth */ 4852 scmd->satacmd_flags.sata_max_queue_depth = 4853 sdinfo->satadrv_max_queue_depth - 1; 4854 } else if (sdinfo->satadrv_features_enabled & 4855 SATA_DEV_F_E_UNTAGGED_QING) { 4856 /* 4857 * Although NCQ/TCQ is not enabled, untagged queuing 4858 * may be still used. 4859 * Set-up the maximum untagged queue depth. 4860 * Use controller's queue depth from sata_hba_tran. 4861 * SATA HBA drivers may ignore this value and rely on 4862 * the internal limits. For drivera that do not 4863 * ignore untaged queue depth, limit the value to 4864 * SATA_MAX_QUEUE_DEPTH (32), as this is the 4865 * largest value that can be passed via 4866 * satacmd_flags.sata_max_queue_depth. 4867 */ 4868 scmd->satacmd_flags.sata_max_queue_depth = 4869 SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ? 4870 SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1; 4871 4872 } else { 4873 scmd->satacmd_flags.sata_max_queue_depth = 0; 4874 } 4875 } else 4876 scmd->satacmd_flags.sata_max_queue_depth = 0; 4877 4878 SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4879 "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n", 4880 scmd->satacmd_cmd_reg, lba, sec_count); 4881 4882 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 4883 /* Need callback function */ 4884 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion; 4885 synch = FALSE; 4886 } else 4887 synch = TRUE; 4888 4889 /* Transfer command to HBA */ 4890 if (sata_hba_start(spx, &rval) != 0) { 4891 /* Pkt not accepted for execution */ 4892 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4893 return (rval); 4894 } 4895 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 4896 4897 /* 4898 * If execution is non-synchronous, 4899 * a callback function will handle potential errors, translate 4900 * the response and will do a callback to a target driver. 4901 * If it was synchronous, check execution status using the same 4902 * framework callback. 4903 */ 4904 if (synch) { 4905 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4906 "synchronous execution status %x\n", 4907 spx->txlt_sata_pkt->satapkt_reason); 4908 sata_txlt_rw_completion(spx->txlt_sata_pkt); 4909 } 4910 return (TRAN_ACCEPT); 4911 } 4912 4913 4914 /* 4915 * Implements SCSI SBC WRITE BUFFER command download microcode option 4916 */ 4917 static int 4918 sata_txlt_write_buffer(sata_pkt_txlate_t *spx) 4919 { 4920 #define WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE 4 4921 #define WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE 5 4922 4923 sata_hba_inst_t *sata_hba_inst = SATA_TXLT_HBA_INST(spx); 4924 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 4925 struct sata_pkt *sata_pkt = spx->txlt_sata_pkt; 4926 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 4927 4928 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 4929 struct scsi_extended_sense *sense; 4930 int rval, mode, sector_count, reason; 4931 int cport = SATA_TXLT_CPORT(spx); 4932 4933 mode = scsipkt->pkt_cdbp[1] & 0x1f; 4934 4935 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 4936 "sata_txlt_write_buffer, mode 0x%x\n", mode); 4937 4938 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 4939 4940 if ((rval = sata_txlt_generic_pkt_info(spx, &reason)) != TRAN_ACCEPT) { 4941 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4942 return (rval); 4943 } 4944 /* 4945 * If in interrupt context, reject this packet because it would issue 4946 * a synchronous command to HBA. 4947 */ 4948 if (servicing_interrupt()) { 4949 SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst, 4950 "sata_txlt_write_buffer: rejecting command because " 4951 "of interrupt context\n", NULL); 4952 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 4953 return (TRAN_BUSY); 4954 } 4955 4956 /* Use synchronous mode */ 4957 spx->txlt_sata_pkt->satapkt_op_mode 4958 |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 4959 4960 scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE; 4961 4962 scsipkt->pkt_reason = CMD_CMPLT; 4963 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 4964 STATE_SENT_CMD | STATE_GOT_STATUS; 4965 4966 /* 4967 * The SCSI to ATA translation specification only calls 4968 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE. 4969 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but 4970 * ATA 8 (draft) got rid of download microcode for temp 4971 * and it is even optional for ATA 7, so it may be aborted. 4972 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as 4973 * it is not specified and the buffer offset for SCSI is a 16-bit 4974 * value in bytes, but for ATA it is a 16-bit offset in 512 byte 4975 * sectors. Thus the offset really doesn't buy us anything. 4976 * If and when ATA 8 is stabilized and the SCSI to ATA specification 4977 * is revised, this can be revisisted. 4978 */ 4979 /* Reject not supported request */ 4980 switch (mode) { 4981 case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE: 4982 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP; 4983 break; 4984 case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE: 4985 scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE; 4986 break; 4987 default: 4988 goto bad_param; 4989 } 4990 4991 *scsipkt->pkt_scbp = STATUS_GOOD; /* Presumed outcome */ 4992 4993 scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE; 4994 if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0) 4995 goto bad_param; 4996 sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE; 4997 scmd->satacmd_sec_count_lsb = (uint8_t)sector_count; 4998 scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8; 4999 scmd->satacmd_lba_mid_lsb = 0; 5000 scmd->satacmd_lba_high_lsb = 0; 5001 scmd->satacmd_device_reg = 0; 5002 spx->txlt_sata_pkt->satapkt_comp = NULL; 5003 scmd->satacmd_addr_type = 0; 5004 5005 /* Transfer command to HBA */ 5006 if (sata_hba_start(spx, &rval) != 0) { 5007 /* Pkt not accepted for execution */ 5008 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 5009 return (rval); 5010 } 5011 5012 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport)); 5013 5014 /* Then we need synchronous check the status of the disk */ 5015 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5016 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 5017 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 5018 scsipkt->pkt_reason = CMD_CMPLT; 5019 5020 /* Download commmand succeed, so probe and identify device */ 5021 sata_reidentify_device(spx); 5022 } else { 5023 /* Something went wrong, microcode download command failed */ 5024 scsipkt->pkt_reason = CMD_INCOMPLETE; 5025 *scsipkt->pkt_scbp = STATUS_CHECK; 5026 sense = sata_arq_sense(spx); 5027 switch (sata_pkt->satapkt_reason) { 5028 case SATA_PKT_PORT_ERROR: 5029 /* 5030 * We have no device data. Assume no data transfered. 5031 */ 5032 sense->es_key = KEY_HARDWARE_ERROR; 5033 break; 5034 5035 case SATA_PKT_DEV_ERROR: 5036 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 5037 SATA_STATUS_ERR) { 5038 /* 5039 * determine dev error reason from error 5040 * reg content 5041 */ 5042 sata_decode_device_error(spx, sense); 5043 break; 5044 } 5045 /* No extended sense key - no info available */ 5046 break; 5047 5048 case SATA_PKT_TIMEOUT: 5049 scsipkt->pkt_reason = CMD_TIMEOUT; 5050 scsipkt->pkt_statistics |= 5051 STAT_TIMEOUT | STAT_DEV_RESET; 5052 /* No extended sense key ? */ 5053 break; 5054 5055 case SATA_PKT_ABORTED: 5056 scsipkt->pkt_reason = CMD_ABORTED; 5057 scsipkt->pkt_statistics |= STAT_ABORTED; 5058 /* No extended sense key ? */ 5059 break; 5060 5061 case SATA_PKT_RESET: 5062 /* pkt aborted by an explicit reset from a host */ 5063 scsipkt->pkt_reason = CMD_RESET; 5064 scsipkt->pkt_statistics |= STAT_DEV_RESET; 5065 break; 5066 5067 default: 5068 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 5069 "sata_txlt_nodata_cmd_completion: " 5070 "invalid packet completion reason %d", 5071 sata_pkt->satapkt_reason)); 5072 scsipkt->pkt_reason = CMD_TRAN_ERR; 5073 break; 5074 } 5075 5076 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5077 "scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5078 5079 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5080 scsipkt->pkt_comp != NULL) 5081 /* scsi callback required */ 5082 (*scsipkt->pkt_comp)(scsipkt); 5083 } 5084 return (TRAN_ACCEPT); 5085 5086 bad_param: 5087 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5088 *scsipkt->pkt_scbp = STATUS_CHECK; 5089 sense = sata_arq_sense(spx); 5090 sense->es_key = KEY_ILLEGAL_REQUEST; 5091 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 5092 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5093 scsipkt->pkt_comp != NULL) { 5094 /* scsi callback required */ 5095 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5096 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 5097 TQ_SLEEP) == 0) { 5098 /* Scheduling the callback failed */ 5099 rval = TRAN_BUSY; 5100 } 5101 } 5102 return (rval); 5103 } 5104 5105 /* 5106 * Re-identify device after doing a firmware download. 5107 */ 5108 static void 5109 sata_reidentify_device(sata_pkt_txlate_t *spx) 5110 { 5111 #define DOWNLOAD_WAIT_TIME_SECS 60 5112 #define DOWNLOAD_WAIT_INTERVAL_SECS 1 5113 int rval; 5114 int retry_cnt; 5115 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5116 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 5117 sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device; 5118 sata_drive_info_t *sdinfo; 5119 5120 /* 5121 * Before returning good status, probe device. 5122 * Device probing will get IDENTIFY DEVICE data, if possible. 5123 * The assumption is that the new microcode is applied by the 5124 * device. It is a caller responsibility to verify this. 5125 */ 5126 for (retry_cnt = 0; 5127 retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS; 5128 retry_cnt++) { 5129 rval = sata_probe_device(sata_hba_inst, &sata_device); 5130 5131 if (rval == SATA_SUCCESS) { /* Set default features */ 5132 sdinfo = sata_get_device_info(sata_hba_inst, 5133 &sata_device); 5134 if (sata_initialize_device(sata_hba_inst, sdinfo) != 5135 SATA_SUCCESS) { 5136 /* retry */ 5137 rval = sata_initialize_device(sata_hba_inst, 5138 sdinfo); 5139 if (rval == SATA_RETRY) 5140 sata_log(sata_hba_inst, CE_WARN, 5141 "SATA device at port %d pmport %d -" 5142 " default device features could not" 5143 " be set. Device may not operate " 5144 "as expected.", 5145 sata_device.satadev_addr.cport, 5146 sata_device.satadev_addr.pmport); 5147 } 5148 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5149 scsipkt->pkt_comp != NULL) 5150 (*scsipkt->pkt_comp)(scsipkt); 5151 return; 5152 } else if (rval == SATA_RETRY) { 5153 delay(drv_usectohz(1000000 * 5154 DOWNLOAD_WAIT_INTERVAL_SECS)); 5155 continue; 5156 } else /* failed - no reason to retry */ 5157 break; 5158 } 5159 5160 /* 5161 * Something went wrong, device probing failed. 5162 */ 5163 SATA_LOG_D((sata_hba_inst, CE_WARN, 5164 "Cannot probe device after downloading microcode\n")); 5165 5166 /* Reset device to force retrying the probe. */ 5167 (void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 5168 (SATA_DIP(sata_hba_inst), &sata_device); 5169 5170 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5171 scsipkt->pkt_comp != NULL) 5172 (*scsipkt->pkt_comp)(scsipkt); 5173 } 5174 5175 5176 /* 5177 * Translate command: Synchronize Cache. 5178 * Translates into Flush Cache command for SATA hard disks. 5179 * 5180 * Returns TRAN_ACCEPT or code returned by sata_hba_start() and 5181 * appropriate values in scsi_pkt fields. 5182 */ 5183 static int 5184 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx) 5185 { 5186 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 5187 sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx); 5188 int cport = SATA_TXLT_CPORT(spx); 5189 int rval, reason; 5190 int synch; 5191 5192 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 5193 5194 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 5195 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 5196 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 5197 return (rval); 5198 } 5199 5200 scmd->satacmd_addr_type = 0; 5201 scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE; 5202 scmd->satacmd_device_reg = 0; 5203 scmd->satacmd_sec_count_lsb = 0; 5204 scmd->satacmd_lba_low_lsb = 0; 5205 scmd->satacmd_lba_mid_lsb = 0; 5206 scmd->satacmd_lba_high_lsb = 0; 5207 scmd->satacmd_features_reg = 0; 5208 scmd->satacmd_status_reg = 0; 5209 scmd->satacmd_error_reg = 0; 5210 5211 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5212 "sata_txlt_synchronize_cache\n", NULL); 5213 5214 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5215 /* Need to set-up a callback function */ 5216 spx->txlt_sata_pkt->satapkt_comp = 5217 sata_txlt_nodata_cmd_completion; 5218 synch = FALSE; 5219 } else 5220 synch = TRUE; 5221 5222 /* Transfer command to HBA */ 5223 if (sata_hba_start(spx, &rval) != 0) { 5224 /* Pkt not accepted for execution */ 5225 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5226 return (rval); 5227 } 5228 mutex_exit(&SATA_CPORT_MUTEX(shi, cport)); 5229 5230 /* 5231 * If execution non-synchronous, it had to be completed 5232 * a callback function will handle potential errors, translate 5233 * the response and will do a callback to a target driver. 5234 * If it was synchronous, check status, using the same 5235 * framework callback. 5236 */ 5237 if (synch) { 5238 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5239 "synchronous execution status %x\n", 5240 spx->txlt_sata_pkt->satapkt_reason); 5241 sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt); 5242 } 5243 return (TRAN_ACCEPT); 5244 } 5245 5246 5247 /* 5248 * Send pkt to SATA HBA driver 5249 * 5250 * This function may be called only if the operation is requested by scsi_pkt, 5251 * i.e. scsi_pkt is not NULL. 5252 * 5253 * This function has to be called with cport mutex held. It does release 5254 * the mutex when it calls HBA driver sata_tran_start function and 5255 * re-acquires it afterwards. 5256 * 5257 * If return value is 0, pkt was accepted, -1 otherwise 5258 * rval is set to appropriate sata_scsi_start return value. 5259 * 5260 * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not 5261 * have called the sata_pkt callback function for this packet. 5262 * 5263 * The scsi callback has to be performed by the caller of this routine. 5264 * 5265 * Note 2: No port multiplier support for now. 5266 */ 5267 static int 5268 sata_hba_start(sata_pkt_txlate_t *spx, int *rval) 5269 { 5270 int stat, cport; 5271 sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst; 5272 sata_drive_info_t *sdinfo; 5273 sata_device_t *sata_device; 5274 uint8_t cmd; 5275 struct sata_cmd_flags cmd_flags; 5276 5277 ASSERT(spx->txlt_sata_pkt != NULL); 5278 5279 cport = SATA_TXLT_CPORT(spx); 5280 ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport))); 5281 5282 sdinfo = sata_get_device_info(sata_hba_inst, 5283 &spx->txlt_sata_pkt->satapkt_device); 5284 ASSERT(sdinfo != NULL); 5285 5286 /* Clear device reset state? */ 5287 if (sdinfo->satadrv_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) { 5288 spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags. 5289 sata_clear_dev_reset = B_TRUE; 5290 sdinfo->satadrv_event_flags &= ~SATA_EVNT_CLEAR_DEVICE_RESET; 5291 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 5292 "sata_hba_start: clearing device reset state\n", NULL); 5293 } 5294 cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg; 5295 cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags; 5296 sata_device = &spx->txlt_sata_pkt->satapkt_device; 5297 5298 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 5299 5300 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5301 "Sata cmd 0x%2x\n", cmd); 5302 5303 stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 5304 spx->txlt_sata_pkt); 5305 5306 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 5307 /* 5308 * If sata pkt was accepted and executed in asynchronous mode, i.e. 5309 * with the sata callback, the sata_pkt could be already destroyed 5310 * by the time we check ther return status from the hba_start() 5311 * function, because sata_scsi_destroy_pkt() could have been already 5312 * called (perhaps in the interrupt context). So, in such case, there 5313 * should be no references to it. In other cases, sata_pkt still 5314 * exists. 5315 */ 5316 if (stat == SATA_TRAN_ACCEPTED) { 5317 /* 5318 * pkt accepted for execution. 5319 * If it was executed synchronously, it is already completed 5320 * and pkt completion_reason indicates completion status. 5321 */ 5322 *rval = TRAN_ACCEPT; 5323 return (0); 5324 } 5325 5326 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 5327 switch (stat) { 5328 case SATA_TRAN_QUEUE_FULL: 5329 /* 5330 * Controller detected queue full condition. 5331 */ 5332 SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst, 5333 "sata_hba_start: queue full\n", NULL); 5334 5335 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 5336 *spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL; 5337 5338 *rval = TRAN_BUSY; 5339 break; 5340 5341 case SATA_TRAN_PORT_ERROR: 5342 /* 5343 * Communication/link with device or general port error 5344 * detected before pkt execution begun. 5345 */ 5346 if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 5347 SATA_ADDR_CPORT || 5348 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual == 5349 SATA_ADDR_DCPORT) 5350 sata_log(sata_hba_inst, CE_CONT, 5351 "SATA port %d error", 5352 sata_device->satadev_addr.cport); 5353 else 5354 sata_log(sata_hba_inst, CE_CONT, 5355 "SATA port %d pmport %d error\n", 5356 sata_device->satadev_addr.cport, 5357 sata_device->satadev_addr.pmport); 5358 5359 /* 5360 * Update the port/device structure. 5361 * sata_pkt should be still valid. Since port error is 5362 * returned, sata_device content should reflect port 5363 * state - it means, that sata address have been changed, 5364 * because original packet's sata address refered to a device 5365 * attached to some port. 5366 */ 5367 sata_update_port_info(sata_hba_inst, sata_device); 5368 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 5369 *rval = TRAN_FATAL_ERROR; 5370 break; 5371 5372 case SATA_TRAN_CMD_UNSUPPORTED: 5373 /* 5374 * Command rejected by HBA as unsupported. It was HBA driver 5375 * that rejected the command, command was not sent to 5376 * an attached device. 5377 */ 5378 if ((sdinfo != NULL) && 5379 (sdinfo->satadrv_state & SATA_DSTATE_RESET)) 5380 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 5381 "sat_hba_start: cmd 0x%2x rejected " 5382 "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd); 5383 5384 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 5385 (void) sata_txlt_invalid_command(spx); 5386 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 5387 5388 *rval = TRAN_ACCEPT; 5389 break; 5390 5391 case SATA_TRAN_BUSY: 5392 /* 5393 * Command rejected by HBA because other operation prevents 5394 * accepting the packet, or device is in RESET condition. 5395 */ 5396 if (sdinfo != NULL) { 5397 sdinfo->satadrv_state = 5398 spx->txlt_sata_pkt->satapkt_device.satadev_state; 5399 5400 if (sdinfo->satadrv_state & SATA_DSTATE_RESET) { 5401 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 5402 "sata_hba_start: cmd 0x%2x rejected " 5403 "because of device reset condition\n", 5404 cmd); 5405 } else { 5406 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 5407 "sata_hba_start: cmd 0x%2x rejected " 5408 "with SATA_TRAN_BUSY status\n", 5409 cmd); 5410 } 5411 } 5412 spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE; 5413 *rval = TRAN_BUSY; 5414 break; 5415 5416 default: 5417 /* Unrecognized HBA response */ 5418 SATA_LOG_D((sata_hba_inst, CE_WARN, 5419 "sata_hba_start: unrecognized HBA response " 5420 "to cmd : 0x%2x resp 0x%x", cmd, rval)); 5421 spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR; 5422 *rval = TRAN_FATAL_ERROR; 5423 break; 5424 } 5425 5426 /* 5427 * If we got here, the packet was rejected. 5428 * Check if we need to remember reset state clearing request 5429 */ 5430 if (cmd_flags.sata_clear_dev_reset) { 5431 /* 5432 * Check if device is still configured - it may have 5433 * disapeared from the configuration 5434 */ 5435 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 5436 if (sdinfo != NULL) { 5437 /* 5438 * Restore the flag that requests clearing of 5439 * the device reset state, 5440 * so the next sata packet may carry it to HBA. 5441 */ 5442 sdinfo->satadrv_event_flags |= 5443 SATA_EVNT_CLEAR_DEVICE_RESET; 5444 } 5445 } 5446 return (-1); 5447 } 5448 5449 /* 5450 * Scsi response setup for invalid LBA 5451 * 5452 * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields. 5453 */ 5454 static int 5455 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx) 5456 { 5457 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5458 struct scsi_extended_sense *sense; 5459 5460 scsipkt->pkt_reason = CMD_CMPLT; 5461 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5462 STATE_SENT_CMD | STATE_GOT_STATUS; 5463 *scsipkt->pkt_scbp = STATUS_CHECK; 5464 5465 *scsipkt->pkt_scbp = STATUS_CHECK; 5466 sense = sata_arq_sense(spx); 5467 sense->es_key = KEY_ILLEGAL_REQUEST; 5468 sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE; 5469 5470 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5471 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5472 5473 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5474 scsipkt->pkt_comp != NULL) 5475 /* scsi callback required */ 5476 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5477 (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt, 5478 TQ_SLEEP) == NULL) 5479 /* Scheduling the callback failed */ 5480 return (TRAN_BUSY); 5481 return (TRAN_ACCEPT); 5482 } 5483 5484 5485 /* 5486 * Analyze device status and error registers and translate them into 5487 * appropriate scsi sense codes. 5488 * NOTE: non-packet commands only for now 5489 */ 5490 static void 5491 sata_decode_device_error(sata_pkt_txlate_t *spx, 5492 struct scsi_extended_sense *sense) 5493 { 5494 uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg; 5495 5496 ASSERT(sense != NULL); 5497 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 5498 SATA_STATUS_ERR); 5499 5500 5501 if (err_reg & SATA_ERROR_ICRC) { 5502 sense->es_key = KEY_ABORTED_COMMAND; 5503 sense->es_add_code = 0x08; /* Communication failure */ 5504 return; 5505 } 5506 5507 if (err_reg & SATA_ERROR_UNC) { 5508 sense->es_key = KEY_MEDIUM_ERROR; 5509 /* Information bytes (LBA) need to be set by a caller */ 5510 return; 5511 } 5512 5513 /* ADD HERE: MC error bit handling for ATAPI CD/DVD */ 5514 if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) { 5515 sense->es_key = KEY_UNIT_ATTENTION; 5516 sense->es_add_code = 0x3a; /* No media present */ 5517 return; 5518 } 5519 5520 if (err_reg & SATA_ERROR_IDNF) { 5521 if (err_reg & SATA_ERROR_ABORT) { 5522 sense->es_key = KEY_ABORTED_COMMAND; 5523 } else { 5524 sense->es_key = KEY_ILLEGAL_REQUEST; 5525 sense->es_add_code = 0x21; /* LBA out of range */ 5526 } 5527 return; 5528 } 5529 5530 if (err_reg & SATA_ERROR_ABORT) { 5531 ASSERT(spx->txlt_sata_pkt != NULL); 5532 sense->es_key = KEY_ABORTED_COMMAND; 5533 return; 5534 } 5535 } 5536 5537 /* 5538 * Extract error LBA from sata_pkt.satapkt_cmd register fields 5539 */ 5540 static void 5541 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba) 5542 { 5543 sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd; 5544 5545 *lba = 0; 5546 if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) { 5547 *lba = sata_cmd->satacmd_lba_high_msb; 5548 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb; 5549 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb; 5550 } else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) { 5551 *lba = sata_cmd->satacmd_device_reg & 0xf; 5552 } 5553 *lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb; 5554 *lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb; 5555 *lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb; 5556 } 5557 5558 /* 5559 * This is fixed sense format - if LBA exceeds the info field size, 5560 * no valid info will be returned (valid bit in extended sense will 5561 * be set to 0). 5562 */ 5563 static struct scsi_extended_sense * 5564 sata_arq_sense(sata_pkt_txlate_t *spx) 5565 { 5566 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5567 struct scsi_arq_status *arqs; 5568 struct scsi_extended_sense *sense; 5569 5570 /* Fill ARQ sense data */ 5571 scsipkt->pkt_state |= STATE_ARQ_DONE; 5572 arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp; 5573 *(uchar_t *)&arqs->sts_status = STATUS_CHECK; 5574 *(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD; 5575 arqs->sts_rqpkt_reason = CMD_CMPLT; 5576 arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5577 STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS; 5578 arqs->sts_rqpkt_resid = 0; 5579 sense = &arqs->sts_sensedata; 5580 bzero(sense, sizeof (struct scsi_extended_sense)); 5581 sata_fixed_sense_data_preset(sense); 5582 return (sense); 5583 } 5584 5585 5586 /* 5587 * Emulated SATA Read/Write command completion for zero-length requests. 5588 * This request always succedes, so in synchronous mode it always returns 5589 * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the 5590 * callback cannot be scheduled. 5591 */ 5592 static int 5593 sata_emul_rw_completion(sata_pkt_txlate_t *spx) 5594 { 5595 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5596 5597 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5598 STATE_SENT_CMD | STATE_GOT_STATUS; 5599 scsipkt->pkt_reason = CMD_CMPLT; 5600 *scsipkt->pkt_scbp = STATUS_GOOD; 5601 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 5602 /* scsi callback required - have to schedule it */ 5603 if (taskq_dispatch(SATA_TXLT_TASKQ(spx), 5604 (task_func_t *)scsipkt->pkt_comp, 5605 (void *)scsipkt, TQ_SLEEP) == NULL) 5606 /* Scheduling the callback failed */ 5607 return (TRAN_BUSY); 5608 } 5609 return (TRAN_ACCEPT); 5610 } 5611 5612 5613 /* 5614 * Translate completion status of SATA read/write commands into scsi response. 5615 * pkt completion_reason is checked to determine the completion status. 5616 * Do scsi callback if necessary. 5617 * 5618 * Note: this function may be called also for synchronously executed 5619 * commands. 5620 * This function may be used only if scsi_pkt is non-NULL. 5621 */ 5622 static void 5623 sata_txlt_rw_completion(sata_pkt_t *sata_pkt) 5624 { 5625 sata_pkt_txlate_t *spx = 5626 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 5627 sata_cmd_t *scmd = &sata_pkt->satapkt_cmd; 5628 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5629 struct scsi_extended_sense *sense; 5630 uint64_t lba; 5631 struct buf *bp; 5632 int rval; 5633 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 5634 /* Normal completion */ 5635 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5636 STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS; 5637 scsipkt->pkt_reason = CMD_CMPLT; 5638 *scsipkt->pkt_scbp = STATUS_GOOD; 5639 if (spx->txlt_tmp_buf != NULL) { 5640 /* Temporary buffer was used */ 5641 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 5642 if (bp->b_flags & B_READ) { 5643 rval = ddi_dma_sync( 5644 spx->txlt_buf_dma_handle, 0, 0, 5645 DDI_DMA_SYNC_FORCPU); 5646 ASSERT(rval == DDI_SUCCESS); 5647 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 5648 bp->b_bcount); 5649 } 5650 } 5651 } else { 5652 /* 5653 * Something went wrong - analyze return 5654 */ 5655 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5656 STATE_SENT_CMD | STATE_GOT_STATUS; 5657 scsipkt->pkt_reason = CMD_INCOMPLETE; 5658 *scsipkt->pkt_scbp = STATUS_CHECK; 5659 sense = sata_arq_sense(spx); 5660 ASSERT(sense != NULL); 5661 5662 /* 5663 * SATA_PKT_DEV_ERROR is the only case where we may be able to 5664 * extract from device registers the failing LBA. 5665 */ 5666 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 5667 if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) && 5668 (scmd->satacmd_lba_mid_msb != 0 || 5669 scmd->satacmd_lba_high_msb != 0)) { 5670 /* 5671 * We have problem reporting this cmd LBA 5672 * in fixed sense data format, because of 5673 * the size of the scsi LBA fields. 5674 */ 5675 sense->es_valid = 0; 5676 } else { 5677 sata_extract_error_lba(spx, &lba); 5678 sense->es_info_1 = (lba & 0xFF000000) >> 24; 5679 sense->es_info_2 = (lba & 0xFF0000) >> 16; 5680 sense->es_info_3 = (lba & 0xFF00) >> 8; 5681 sense->es_info_4 = lba & 0xFF; 5682 } 5683 } else { 5684 /* Invalid extended sense info */ 5685 sense->es_valid = 0; 5686 } 5687 5688 switch (sata_pkt->satapkt_reason) { 5689 case SATA_PKT_PORT_ERROR: 5690 /* We may want to handle DEV GONE state as well */ 5691 /* 5692 * We have no device data. Assume no data transfered. 5693 */ 5694 sense->es_key = KEY_HARDWARE_ERROR; 5695 break; 5696 5697 case SATA_PKT_DEV_ERROR: 5698 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 5699 SATA_STATUS_ERR) { 5700 /* 5701 * determine dev error reason from error 5702 * reg content 5703 */ 5704 sata_decode_device_error(spx, sense); 5705 if (sense->es_key == KEY_MEDIUM_ERROR) { 5706 switch (scmd->satacmd_cmd_reg) { 5707 case SATAC_READ_DMA: 5708 case SATAC_READ_DMA_EXT: 5709 case SATAC_READ_DMA_QUEUED: 5710 case SATAC_READ_DMA_QUEUED_EXT: 5711 case SATAC_READ_FPDMA_QUEUED: 5712 /* Unrecovered read error */ 5713 sense->es_add_code = 5714 SD_SCSI_ASC_UNREC_READ_ERR; 5715 break; 5716 case SATAC_WRITE_DMA: 5717 case SATAC_WRITE_DMA_EXT: 5718 case SATAC_WRITE_DMA_QUEUED: 5719 case SATAC_WRITE_DMA_QUEUED_EXT: 5720 case SATAC_WRITE_FPDMA_QUEUED: 5721 /* Write error */ 5722 sense->es_add_code = 5723 SD_SCSI_ASC_WRITE_ERR; 5724 break; 5725 default: 5726 /* Internal error */ 5727 SATA_LOG_D(( 5728 spx->txlt_sata_hba_inst, 5729 CE_WARN, 5730 "sata_txlt_rw_completion :" 5731 "internal error - invalid " 5732 "command 0x%2x", 5733 scmd->satacmd_cmd_reg)); 5734 break; 5735 } 5736 } 5737 break; 5738 } 5739 /* No extended sense key - no info available */ 5740 scsipkt->pkt_reason = CMD_INCOMPLETE; 5741 break; 5742 5743 case SATA_PKT_TIMEOUT: 5744 scsipkt->pkt_reason = CMD_TIMEOUT; 5745 scsipkt->pkt_statistics |= 5746 STAT_TIMEOUT | STAT_DEV_RESET; 5747 sense->es_key = KEY_ABORTED_COMMAND; 5748 break; 5749 5750 case SATA_PKT_ABORTED: 5751 scsipkt->pkt_reason = CMD_ABORTED; 5752 scsipkt->pkt_statistics |= STAT_ABORTED; 5753 sense->es_key = KEY_ABORTED_COMMAND; 5754 break; 5755 5756 case SATA_PKT_RESET: 5757 scsipkt->pkt_reason = CMD_RESET; 5758 scsipkt->pkt_statistics |= STAT_DEV_RESET; 5759 sense->es_key = KEY_ABORTED_COMMAND; 5760 break; 5761 5762 default: 5763 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 5764 "sata_txlt_rw_completion: " 5765 "invalid packet completion reason")); 5766 scsipkt->pkt_reason = CMD_TRAN_ERR; 5767 break; 5768 } 5769 } 5770 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5771 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5772 5773 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5774 scsipkt->pkt_comp != NULL) 5775 /* scsi callback required */ 5776 (*scsipkt->pkt_comp)(scsipkt); 5777 } 5778 5779 5780 /* 5781 * Translate completion status of non-data commands (i.e. commands returning 5782 * no data). 5783 * pkt completion_reason is checked to determine the completion status. 5784 * Do scsi callback if necessary (FLAG_NOINTR == 0) 5785 * 5786 * Note: this function may be called also for synchronously executed 5787 * commands. 5788 * This function may be used only if scsi_pkt is non-NULL. 5789 */ 5790 5791 static void 5792 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt) 5793 { 5794 sata_pkt_txlate_t *spx = 5795 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 5796 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 5797 struct scsi_extended_sense *sense; 5798 5799 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 5800 STATE_SENT_CMD | STATE_GOT_STATUS; 5801 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 5802 /* Normal completion */ 5803 scsipkt->pkt_reason = CMD_CMPLT; 5804 *scsipkt->pkt_scbp = STATUS_GOOD; 5805 } else { 5806 /* Something went wrong */ 5807 scsipkt->pkt_reason = CMD_INCOMPLETE; 5808 *scsipkt->pkt_scbp = STATUS_CHECK; 5809 sense = sata_arq_sense(spx); 5810 switch (sata_pkt->satapkt_reason) { 5811 case SATA_PKT_PORT_ERROR: 5812 /* 5813 * We have no device data. Assume no data transfered. 5814 */ 5815 sense->es_key = KEY_HARDWARE_ERROR; 5816 break; 5817 5818 case SATA_PKT_DEV_ERROR: 5819 if (sata_pkt->satapkt_cmd.satacmd_status_reg & 5820 SATA_STATUS_ERR) { 5821 /* 5822 * determine dev error reason from error 5823 * reg content 5824 */ 5825 sata_decode_device_error(spx, sense); 5826 break; 5827 } 5828 /* No extended sense key - no info available */ 5829 break; 5830 5831 case SATA_PKT_TIMEOUT: 5832 scsipkt->pkt_reason = CMD_TIMEOUT; 5833 scsipkt->pkt_statistics |= 5834 STAT_TIMEOUT | STAT_DEV_RESET; 5835 /* No extended sense key ? */ 5836 break; 5837 5838 case SATA_PKT_ABORTED: 5839 scsipkt->pkt_reason = CMD_ABORTED; 5840 scsipkt->pkt_statistics |= STAT_ABORTED; 5841 /* No extended sense key ? */ 5842 break; 5843 5844 case SATA_PKT_RESET: 5845 /* pkt aborted by an explicit reset from a host */ 5846 scsipkt->pkt_reason = CMD_RESET; 5847 scsipkt->pkt_statistics |= STAT_DEV_RESET; 5848 break; 5849 5850 default: 5851 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 5852 "sata_txlt_nodata_cmd_completion: " 5853 "invalid packet completion reason %d", 5854 sata_pkt->satapkt_reason)); 5855 scsipkt->pkt_reason = CMD_TRAN_ERR; 5856 break; 5857 } 5858 5859 } 5860 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 5861 "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason); 5862 5863 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 5864 scsipkt->pkt_comp != NULL) 5865 /* scsi callback required */ 5866 (*scsipkt->pkt_comp)(scsipkt); 5867 } 5868 5869 5870 /* 5871 * Build Mode sense R/W recovery page 5872 * NOT IMPLEMENTED 5873 */ 5874 5875 static int 5876 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 5877 { 5878 #ifndef __lock_lint 5879 _NOTE(ARGUNUSED(sdinfo)) 5880 _NOTE(ARGUNUSED(pcntrl)) 5881 _NOTE(ARGUNUSED(buf)) 5882 #endif 5883 return (0); 5884 } 5885 5886 /* 5887 * Build Mode sense caching page - scsi-3 implementation. 5888 * Page length distinguishes previous format from scsi-3 format. 5889 * buf must have space for 0x12 bytes. 5890 * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable. 5891 * 5892 */ 5893 static int 5894 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 5895 { 5896 struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf; 5897 sata_id_t *sata_id = &sdinfo->satadrv_id; 5898 5899 /* 5900 * Most of the fields are set to 0, being not supported and/or disabled 5901 */ 5902 bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3); 5903 5904 /* Saved paramters not supported */ 5905 if (pcntrl == 3) 5906 return (0); 5907 if (pcntrl == 0 || pcntrl == 2) { 5908 /* 5909 * For now treat current and default parameters as same 5910 * That may have to change, if target driver will complain 5911 */ 5912 page->mode_page.code = MODEPAGE_CACHING; /* PS = 0 */ 5913 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 5914 5915 if ((sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) && 5916 !(sata_id->ai_features85 & SATA_LOOK_AHEAD)) { 5917 page->dra = 1; /* Read Ahead disabled */ 5918 page->rcd = 1; /* Read Cache disabled */ 5919 } 5920 if ((sata_id->ai_cmdset82 & SATA_WRITE_CACHE) && 5921 (sata_id->ai_features85 & SATA_WRITE_CACHE)) 5922 page->wce = 1; /* Write Cache enabled */ 5923 } else { 5924 /* Changeable parameters */ 5925 page->mode_page.code = MODEPAGE_CACHING; 5926 page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3; 5927 if (sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) { 5928 page->dra = 1; 5929 page->rcd = 1; 5930 } 5931 if (sata_id->ai_cmdset82 & SATA_WRITE_CACHE) 5932 page->wce = 1; 5933 } 5934 return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 5935 sizeof (struct mode_page)); 5936 } 5937 5938 /* 5939 * Build Mode sense exception cntrl page 5940 */ 5941 static int 5942 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 5943 { 5944 struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf; 5945 sata_id_t *sata_id = &sdinfo->satadrv_id; 5946 5947 /* 5948 * Most of the fields are set to 0, being not supported and/or disabled 5949 */ 5950 bzero(buf, PAGELENGTH_INFO_EXCPT); 5951 5952 page->mode_page.code = MODEPAGE_INFO_EXCPT; 5953 page->mode_page.length = PAGELENGTH_INFO_EXCPT; 5954 5955 /* Indicate that this is page is saveable */ 5956 page->mode_page.ps = 1; 5957 5958 /* 5959 * We will return the same data for default, current and saved page. 5960 * The only changeable bit is dexcpt and that bit is required 5961 * by the ATA specification to be preserved across power cycles. 5962 */ 5963 if (pcntrl != 1) { 5964 page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED); 5965 page->mrie = MRIE_ONLY_ON_REQUEST; 5966 } 5967 else 5968 page->dexcpt = 1; /* Only changeable parameter */ 5969 5970 return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_info_excpt_page)); 5971 } 5972 5973 5974 static int 5975 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 5976 { 5977 struct mode_acoustic_management *page = 5978 (struct mode_acoustic_management *)buf; 5979 sata_id_t *sata_id = &sdinfo->satadrv_id; 5980 5981 /* 5982 * Most of the fields are set to 0, being not supported and/or disabled 5983 */ 5984 bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT); 5985 5986 switch (pcntrl) { 5987 case P_CNTRL_DEFAULT: 5988 /* default paramters not supported */ 5989 return (0); 5990 5991 case P_CNTRL_CURRENT: 5992 case P_CNTRL_SAVED: 5993 /* Saved and current are supported and are identical */ 5994 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG; 5995 page->mode_page.length = 5996 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT; 5997 page->mode_page.ps = 1; 5998 5999 /* Word 83 indicates if feature is supported */ 6000 /* If feature is not supported */ 6001 if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) { 6002 page->acoustic_manag_enable = 6003 ACOUSTIC_DISABLED; 6004 } else { 6005 page->acoustic_manag_enable = 6006 ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT) 6007 != 0); 6008 /* Word 94 inidicates the value */ 6009 #ifdef _LITTLE_ENDIAN 6010 page->acoustic_manag_level = 6011 (uchar_t)sata_id->ai_acoustic; 6012 page->vendor_recommended_value = 6013 sata_id->ai_acoustic >> 8; 6014 #else 6015 page->acoustic_manag_level = 6016 sata_id->ai_acoustic >> 8; 6017 page->vendor_recommended_value = 6018 (uchar_t)sata_id->ai_acoustic; 6019 #endif 6020 } 6021 break; 6022 6023 case P_CNTRL_CHANGEABLE: 6024 page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG; 6025 page->mode_page.length = 6026 PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT; 6027 page->mode_page.ps = 1; 6028 6029 /* Word 83 indicates if the feature is supported */ 6030 if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) { 6031 page->acoustic_manag_enable = 6032 ACOUSTIC_ENABLED; 6033 page->acoustic_manag_level = 0xff; 6034 } 6035 break; 6036 } 6037 return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 6038 sizeof (struct mode_page)); 6039 } 6040 6041 6042 /* 6043 * Build Mode sense power condition page 6044 * NOT IMPLEMENTED. 6045 */ 6046 static int 6047 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf) 6048 { 6049 #ifndef __lock_lint 6050 _NOTE(ARGUNUSED(sdinfo)) 6051 _NOTE(ARGUNUSED(pcntrl)) 6052 _NOTE(ARGUNUSED(buf)) 6053 #endif 6054 return (0); 6055 } 6056 6057 6058 /* 6059 * Process mode select caching page 8 (scsi3 format only). 6060 * Read Ahead (same as read cache) and Write Cache may be turned on and off 6061 * if these features are supported by the device. If these features are not 6062 * supported, quietly ignore them. 6063 * This function fails only if the SET FEATURE command sent to 6064 * the device fails. The page format is not varified, assuming that the 6065 * target driver operates correctly - if parameters length is too short, 6066 * we just drop the page. 6067 * Two command may be sent if both Read Cache/Read Ahead and Write Cache 6068 * setting have to be changed. 6069 * SET FEATURE command is executed synchronously, i.e. we wait here until 6070 * it is completed, regardless of the scsi pkt directives. 6071 * 6072 * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e. 6073 * changing DRA will change RCD. 6074 * 6075 * More than one SATA command may be executed to perform operations specified 6076 * by mode select pages. The first error terminates further execution. 6077 * Operations performed successully are not backed-up in such case. 6078 * 6079 * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 6080 * If operation resulted in changing device setup, dmod flag should be set to 6081 * one (1). If parameters were not changed, dmod flag should be set to 0. 6082 * Upon return, if operation required sending command to the device, the rval 6083 * should be set to the value returned by sata_hba_start. If operation 6084 * did not require device access, rval should be set to TRAN_ACCEPT. 6085 * The pagelen should be set to the length of the page. 6086 * 6087 * This function has to be called with a port mutex held. 6088 * 6089 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 6090 */ 6091 int 6092 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page, 6093 int parmlen, int *pagelen, int *rval, int *dmod) 6094 { 6095 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6096 sata_drive_info_t *sdinfo; 6097 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6098 sata_id_t *sata_id; 6099 struct scsi_extended_sense *sense; 6100 int wce, dra; /* Current settings */ 6101 6102 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 6103 &spx->txlt_sata_pkt->satapkt_device); 6104 sata_id = &sdinfo->satadrv_id; 6105 *dmod = 0; 6106 6107 /* Verify parameters length. If too short, drop it */ 6108 if (PAGELENGTH_DAD_MODE_CACHE_SCSI3 + 6109 sizeof (struct mode_page) < parmlen) { 6110 *scsipkt->pkt_scbp = STATUS_CHECK; 6111 sense = sata_arq_sense(spx); 6112 sense->es_key = KEY_ILLEGAL_REQUEST; 6113 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 6114 *pagelen = parmlen; 6115 *rval = TRAN_ACCEPT; 6116 return (SATA_FAILURE); 6117 } 6118 6119 *pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page); 6120 6121 /* 6122 * We can manipulate only write cache and read ahead 6123 * (read cache) setting. 6124 */ 6125 if (!(sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) && 6126 !(sata_id->ai_cmdset82 & SATA_WRITE_CACHE)) { 6127 /* 6128 * None of the features is supported - ignore 6129 */ 6130 *rval = TRAN_ACCEPT; 6131 return (SATA_SUCCESS); 6132 } 6133 6134 /* Current setting of Read Ahead (and Read Cache) */ 6135 if (sata_id->ai_features85 & SATA_LOOK_AHEAD) 6136 dra = 0; /* 0 == not disabled */ 6137 else 6138 dra = 1; 6139 /* Current setting of Write Cache */ 6140 if (sata_id->ai_features85 & SATA_WRITE_CACHE) 6141 wce = 1; 6142 else 6143 wce = 0; 6144 6145 if (page->dra == dra && page->wce == wce && page->rcd == dra) { 6146 /* nothing to do */ 6147 *rval = TRAN_ACCEPT; 6148 return (SATA_SUCCESS); 6149 } 6150 6151 /* 6152 * Need to flip some setting 6153 * Set-up Internal SET FEATURES command(s) 6154 */ 6155 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 6156 scmd->satacmd_addr_type = 0; 6157 scmd->satacmd_device_reg = 0; 6158 scmd->satacmd_status_reg = 0; 6159 scmd->satacmd_error_reg = 0; 6160 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 6161 if (page->dra != dra || page->rcd != dra) { 6162 /* Need to flip read ahead setting */ 6163 if (dra == 0) 6164 /* Disable read ahead / read cache */ 6165 scmd->satacmd_features_reg = 6166 SATAC_SF_DISABLE_READ_AHEAD; 6167 else 6168 /* Enable read ahead / read cache */ 6169 scmd->satacmd_features_reg = 6170 SATAC_SF_ENABLE_READ_AHEAD; 6171 6172 /* Transfer command to HBA */ 6173 if (sata_hba_start(spx, rval) != 0) 6174 /* 6175 * Pkt not accepted for execution. 6176 */ 6177 return (SATA_FAILURE); 6178 6179 *dmod = 1; 6180 6181 /* Now process return */ 6182 if (spx->txlt_sata_pkt->satapkt_reason != 6183 SATA_PKT_COMPLETED) { 6184 goto failure; /* Terminate */ 6185 } 6186 } 6187 6188 /* Note that the packet is not removed, so it could be re-used */ 6189 if (page->wce != wce) { 6190 /* Need to flip Write Cache setting */ 6191 if (page->wce == 1) 6192 /* Enable write cache */ 6193 scmd->satacmd_features_reg = 6194 SATAC_SF_ENABLE_WRITE_CACHE; 6195 else 6196 /* Disable write cache */ 6197 scmd->satacmd_features_reg = 6198 SATAC_SF_DISABLE_WRITE_CACHE; 6199 6200 /* Transfer command to HBA */ 6201 if (sata_hba_start(spx, rval) != 0) 6202 /* 6203 * Pkt not accepted for execution. 6204 */ 6205 return (SATA_FAILURE); 6206 6207 *dmod = 1; 6208 6209 /* Now process return */ 6210 if (spx->txlt_sata_pkt->satapkt_reason != 6211 SATA_PKT_COMPLETED) { 6212 goto failure; 6213 } 6214 } 6215 return (SATA_SUCCESS); 6216 6217 failure: 6218 sata_xlate_errors(spx); 6219 6220 return (SATA_FAILURE); 6221 } 6222 6223 /* 6224 * Process mode select informational exceptions control page 0x1c 6225 * 6226 * The only changeable bit is dexcpt (disable exceptions). 6227 * MRIE (method of reporting informational exceptions) must be 6228 * "only on request". 6229 * This page applies to informational exceptions that report 6230 * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh 6231 * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_). 6232 * Informational exception conditions occur as the result of background scan 6233 * errors, background self-test errors, or vendor specific events within a 6234 * logical unit. An informational exception condition may occur asynchronous 6235 * to any commands. 6236 * 6237 * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise. 6238 * If operation resulted in changing device setup, dmod flag should be set to 6239 * one (1). If parameters were not changed, dmod flag should be set to 0. 6240 * Upon return, if operation required sending command to the device, the rval 6241 * should be set to the value returned by sata_hba_start. If operation 6242 * did not require device access, rval should be set to TRAN_ACCEPT. 6243 * The pagelen should be set to the length of the page. 6244 * 6245 * This function has to be called with a port mutex held. 6246 * 6247 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 6248 * 6249 * Cannot be called in the interrupt context. 6250 */ 6251 static int 6252 sata_mode_select_page_1c( 6253 sata_pkt_txlate_t *spx, 6254 struct mode_info_excpt_page *page, 6255 int parmlen, 6256 int *pagelen, 6257 int *rval, 6258 int *dmod) 6259 { 6260 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6261 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6262 sata_drive_info_t *sdinfo; 6263 sata_id_t *sata_id; 6264 struct scsi_extended_sense *sense; 6265 6266 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 6267 &spx->txlt_sata_pkt->satapkt_device); 6268 sata_id = &sdinfo->satadrv_id; 6269 6270 *dmod = 0; 6271 6272 /* Verify parameters length. If too short, drop it */ 6273 if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) < parmlen) || 6274 page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) { 6275 *scsipkt->pkt_scbp = STATUS_CHECK; 6276 sense = sata_arq_sense(spx); 6277 sense->es_key = KEY_ILLEGAL_REQUEST; 6278 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 6279 *pagelen = parmlen; 6280 *rval = TRAN_ACCEPT; 6281 return (SATA_FAILURE); 6282 } 6283 6284 *pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page); 6285 6286 if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) { 6287 *scsipkt->pkt_scbp = STATUS_CHECK; 6288 sense = sata_arq_sense(spx); 6289 sense->es_key = KEY_ILLEGAL_REQUEST; 6290 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB; 6291 *pagelen = parmlen; 6292 *rval = TRAN_ACCEPT; 6293 return (SATA_FAILURE); 6294 } 6295 6296 /* If already in the state requested, we are done */ 6297 if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) { 6298 /* nothing to do */ 6299 *rval = TRAN_ACCEPT; 6300 return (SATA_SUCCESS); 6301 } 6302 6303 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 6304 6305 /* Build SMART_ENABLE or SMART_DISABLE command */ 6306 scmd->satacmd_addr_type = 0; /* N/A */ 6307 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 6308 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 6309 scmd->satacmd_features_reg = page->dexcpt ? 6310 SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS; 6311 scmd->satacmd_device_reg = 0; /* Always device 0 */ 6312 scmd->satacmd_cmd_reg = SATAC_SMART; 6313 6314 /* Transfer command to HBA */ 6315 if (sata_hba_start(spx, rval) != 0) 6316 /* 6317 * Pkt not accepted for execution. 6318 */ 6319 return (SATA_FAILURE); 6320 6321 *dmod = 1; /* At least may have been modified */ 6322 6323 /* Now process return */ 6324 if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) 6325 return (SATA_SUCCESS); 6326 6327 /* Packet did not complete successfully */ 6328 sata_xlate_errors(spx); 6329 6330 return (SATA_FAILURE); 6331 } 6332 6333 /* 6334 * Process mode select acoustic management control page 0x30 6335 * 6336 * 6337 * This function has to be called with a port mutex held. 6338 * 6339 * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise. 6340 * 6341 * Cannot be called in the interrupt context. 6342 */ 6343 int 6344 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct 6345 mode_acoustic_management *page, int parmlen, int *pagelen, 6346 int *rval, int *dmod) 6347 { 6348 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 6349 sata_drive_info_t *sdinfo; 6350 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 6351 sata_id_t *sata_id; 6352 struct scsi_extended_sense *sense; 6353 6354 sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst, 6355 &spx->txlt_sata_pkt->satapkt_device); 6356 sata_id = &sdinfo->satadrv_id; 6357 *dmod = 0; 6358 6359 /* If parmlen is too short or the feature is not supported, drop it */ 6360 if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 6361 sizeof (struct mode_page)) < parmlen) || 6362 (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) { 6363 *scsipkt->pkt_scbp = STATUS_CHECK; 6364 sense = sata_arq_sense(spx); 6365 sense->es_key = KEY_ILLEGAL_REQUEST; 6366 sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST; 6367 *pagelen = parmlen; 6368 *rval = TRAN_ACCEPT; 6369 return (SATA_FAILURE); 6370 } 6371 6372 *pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT + 6373 sizeof (struct mode_page); 6374 6375 /* 6376 * We can enable and disable acoustice management and 6377 * set the acoustic management level. 6378 */ 6379 6380 /* 6381 * Set-up Internal SET FEATURES command(s) 6382 */ 6383 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 6384 scmd->satacmd_addr_type = 0; 6385 scmd->satacmd_device_reg = 0; 6386 scmd->satacmd_status_reg = 0; 6387 scmd->satacmd_error_reg = 0; 6388 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 6389 if (page->acoustic_manag_enable) { 6390 scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC; 6391 scmd->satacmd_sec_count_lsb = page->acoustic_manag_level; 6392 } else { /* disabling acoustic management */ 6393 scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC; 6394 } 6395 6396 /* Transfer command to HBA */ 6397 if (sata_hba_start(spx, rval) != 0) 6398 /* 6399 * Pkt not accepted for execution. 6400 */ 6401 return (SATA_FAILURE); 6402 6403 /* Now process return */ 6404 if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) { 6405 sata_xlate_errors(spx); 6406 return (SATA_FAILURE); 6407 } 6408 6409 *dmod = 1; 6410 6411 return (SATA_SUCCESS); 6412 } 6413 6414 6415 6416 6417 /* 6418 * sata_build_lsense_page0() is used to create the 6419 * SCSI LOG SENSE page 0 (supported log pages) 6420 * 6421 * Currently supported pages are 0, 0x10, 0x2f and 0x30 6422 * (supported log pages, self-test results, informational exceptions 6423 * and Sun vendor specific ATA SMART data). 6424 * 6425 * Takes a sata_drive_info t * and the address of a buffer 6426 * in which to create the page information. 6427 * 6428 * Returns the number of bytes valid in the buffer. 6429 */ 6430 static int 6431 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf) 6432 { 6433 struct log_parameter *lpp = (struct log_parameter *)buf; 6434 uint8_t *page_ptr = (uint8_t *)lpp->param_values; 6435 int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */ 6436 sata_id_t *sata_id = &sdinfo->satadrv_id; 6437 6438 lpp->param_code[0] = 0; 6439 lpp->param_code[1] = 0; 6440 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 6441 *page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES; 6442 6443 if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) { 6444 if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) { 6445 *page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS; 6446 ++num_pages_supported; 6447 } 6448 *page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS; 6449 ++num_pages_supported; 6450 *page_ptr++ = PAGE_CODE_SMART_READ_DATA; 6451 ++num_pages_supported; 6452 } 6453 6454 lpp->param_len = num_pages_supported; 6455 6456 return ((&lpp->param_values[0] - (uint8_t *)lpp) + 6457 num_pages_supported); 6458 } 6459 6460 /* 6461 * sata_build_lsense_page_10() is used to create the 6462 * SCSI LOG SENSE page 0x10 (self-test results) 6463 * 6464 * Takes a sata_drive_info t * and the address of a buffer 6465 * in which to create the page information as well as a sata_hba_inst_t *. 6466 * 6467 * Returns the number of bytes valid in the buffer. 6468 * 6469 * Note: Self test and SMART data is accessible in device log pages. 6470 * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors 6471 * of data can be transferred by a single command), or by the General Purpose 6472 * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors 6473 * - approximately 33MB - can be transferred by a single command. 6474 * The SCT Command response (either error or command) is the same for both 6475 * the SMART and GPL methods of issuing commands. 6476 * This function uses READ LOG EXT command when drive supports LBA48, and 6477 * SMART READ command otherwise. 6478 * 6479 * Since above commands are executed in a synchronous mode, this function 6480 * should not be called in an interrupt context. 6481 */ 6482 static int 6483 sata_build_lsense_page_10( 6484 sata_drive_info_t *sdinfo, 6485 uint8_t *buf, 6486 sata_hba_inst_t *sata_hba_inst) 6487 { 6488 struct log_parameter *lpp = (struct log_parameter *)buf; 6489 int rval; 6490 6491 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) { 6492 struct smart_ext_selftest_log *ext_selftest_log; 6493 6494 ext_selftest_log = kmem_zalloc( 6495 sizeof (struct smart_ext_selftest_log), KM_SLEEP); 6496 6497 rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo, 6498 ext_selftest_log, 0); 6499 if (rval == 0) { 6500 int index, start_index; 6501 struct smart_ext_selftest_log_entry *entry; 6502 static const struct smart_ext_selftest_log_entry empty = 6503 {0}; 6504 uint16_t block_num; 6505 int count; 6506 boolean_t only_one_block = B_FALSE; 6507 6508 index = ext_selftest_log-> 6509 smart_ext_selftest_log_index[0]; 6510 index |= ext_selftest_log-> 6511 smart_ext_selftest_log_index[1] << 8; 6512 if (index == 0) 6513 goto out; 6514 6515 --index; /* Correct for 0 origin */ 6516 start_index = index; /* remember where we started */ 6517 block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 6518 if (block_num != 0) { 6519 rval = sata_ext_smart_selftest_read_log( 6520 sata_hba_inst, sdinfo, ext_selftest_log, 6521 block_num); 6522 if (rval != 0) 6523 goto out; 6524 } 6525 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 6526 entry = 6527 &ext_selftest_log-> 6528 smart_ext_selftest_log_entries[index]; 6529 6530 for (count = 1; 6531 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 6532 ++count) { 6533 uint8_t status; 6534 uint8_t code; 6535 uint8_t sense_key; 6536 uint8_t add_sense_code; 6537 uint8_t add_sense_code_qual; 6538 6539 /* If this is an unused entry, we are done */ 6540 if (bcmp(entry, &empty, sizeof (empty)) == 0) { 6541 /* Broken firmware on some disks */ 6542 if (index + 1 == 6543 ENTRIES_PER_EXT_SELFTEST_LOG_BLK) { 6544 --entry; 6545 --index; 6546 if (bcmp(entry, &empty, 6547 sizeof (empty)) == 0) 6548 goto out; 6549 } else 6550 goto out; 6551 } 6552 6553 if (only_one_block && 6554 start_index == index) 6555 goto out; 6556 6557 lpp->param_code[0] = 0; 6558 lpp->param_code[1] = count; 6559 lpp->param_ctrl_flags = 6560 LOG_CTRL_LP | LOG_CTRL_LBIN; 6561 lpp->param_len = 6562 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 6563 6564 status = entry->smart_ext_selftest_log_status; 6565 status >>= 4; 6566 switch (status) { 6567 case 0: 6568 default: 6569 sense_key = KEY_NO_SENSE; 6570 add_sense_code = 6571 SD_SCSI_ASC_NO_ADD_SENSE; 6572 add_sense_code_qual = 0; 6573 break; 6574 case 1: 6575 sense_key = KEY_ABORTED_COMMAND; 6576 add_sense_code = 6577 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6578 add_sense_code_qual = SCSI_COMPONENT_81; 6579 break; 6580 case 2: 6581 sense_key = KEY_ABORTED_COMMAND; 6582 add_sense_code = 6583 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6584 add_sense_code_qual = SCSI_COMPONENT_82; 6585 break; 6586 case 3: 6587 sense_key = KEY_ABORTED_COMMAND; 6588 add_sense_code = 6589 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6590 add_sense_code_qual = SCSI_COMPONENT_83; 6591 break; 6592 case 4: 6593 sense_key = KEY_HARDWARE_ERROR; 6594 add_sense_code = 6595 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6596 add_sense_code_qual = SCSI_COMPONENT_84; 6597 break; 6598 case 5: 6599 sense_key = KEY_HARDWARE_ERROR; 6600 add_sense_code = 6601 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6602 add_sense_code_qual = SCSI_COMPONENT_85; 6603 break; 6604 case 6: 6605 sense_key = KEY_HARDWARE_ERROR; 6606 add_sense_code = 6607 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6608 add_sense_code_qual = SCSI_COMPONENT_86; 6609 break; 6610 case 7: 6611 sense_key = KEY_MEDIUM_ERROR; 6612 add_sense_code = 6613 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6614 add_sense_code_qual = SCSI_COMPONENT_87; 6615 break; 6616 case 8: 6617 sense_key = KEY_HARDWARE_ERROR; 6618 add_sense_code = 6619 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6620 add_sense_code_qual = SCSI_COMPONENT_88; 6621 break; 6622 } 6623 code = 0; /* unspecified */ 6624 status |= (code << 4); 6625 lpp->param_values[0] = status; 6626 lpp->param_values[1] = 0; /* unspecified */ 6627 lpp->param_values[2] = entry-> 6628 smart_ext_selftest_log_timestamp[1]; 6629 lpp->param_values[3] = entry-> 6630 smart_ext_selftest_log_timestamp[0]; 6631 if (status != 0) { 6632 lpp->param_values[4] = 0; 6633 lpp->param_values[5] = 0; 6634 lpp->param_values[6] = entry-> 6635 smart_ext_selftest_log_failing_lba 6636 [5]; 6637 lpp->param_values[7] = entry-> 6638 smart_ext_selftest_log_failing_lba 6639 [4]; 6640 lpp->param_values[8] = entry-> 6641 smart_ext_selftest_log_failing_lba 6642 [3]; 6643 lpp->param_values[9] = entry-> 6644 smart_ext_selftest_log_failing_lba 6645 [2]; 6646 lpp->param_values[10] = entry-> 6647 smart_ext_selftest_log_failing_lba 6648 [1]; 6649 lpp->param_values[11] = entry-> 6650 smart_ext_selftest_log_failing_lba 6651 [0]; 6652 } else { /* No bad block address */ 6653 lpp->param_values[4] = 0xff; 6654 lpp->param_values[5] = 0xff; 6655 lpp->param_values[6] = 0xff; 6656 lpp->param_values[7] = 0xff; 6657 lpp->param_values[8] = 0xff; 6658 lpp->param_values[9] = 0xff; 6659 lpp->param_values[10] = 0xff; 6660 lpp->param_values[11] = 0xff; 6661 } 6662 6663 lpp->param_values[12] = sense_key; 6664 lpp->param_values[13] = add_sense_code; 6665 lpp->param_values[14] = add_sense_code_qual; 6666 lpp->param_values[15] = 0; /* undefined */ 6667 6668 lpp = (struct log_parameter *) 6669 (((uint8_t *)lpp) + 6670 SCSI_LOG_PARAM_HDR_LEN + 6671 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 6672 6673 --index; /* Back up to previous entry */ 6674 if (index < 0) { 6675 if (block_num > 0) { 6676 --block_num; 6677 } else { 6678 struct read_log_ext_directory 6679 logdir; 6680 6681 rval = 6682 sata_read_log_ext_directory( 6683 sata_hba_inst, sdinfo, 6684 &logdir); 6685 if (rval == -1) 6686 goto out; 6687 if ((logdir.read_log_ext_vers 6688 [0] == 0) && 6689 (logdir.read_log_ext_vers 6690 [1] == 0)) 6691 goto out; 6692 block_num = 6693 logdir.read_log_ext_nblks 6694 [EXT_SMART_SELFTEST_LOG_PAGE 6695 - 1][0]; 6696 block_num |= logdir. 6697 read_log_ext_nblks 6698 [EXT_SMART_SELFTEST_LOG_PAGE 6699 - 1][1] << 8; 6700 --block_num; 6701 only_one_block = 6702 (block_num == 0); 6703 } 6704 rval = sata_ext_smart_selftest_read_log( 6705 sata_hba_inst, sdinfo, 6706 ext_selftest_log, block_num); 6707 if (rval != 0) 6708 goto out; 6709 6710 index = 6711 ENTRIES_PER_EXT_SELFTEST_LOG_BLK - 6712 1; 6713 } 6714 index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK; 6715 entry = &ext_selftest_log-> 6716 smart_ext_selftest_log_entries[index]; 6717 } 6718 } 6719 out: 6720 kmem_free(ext_selftest_log, 6721 sizeof (struct smart_ext_selftest_log)); 6722 } else { 6723 struct smart_selftest_log *selftest_log; 6724 6725 selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log), 6726 KM_SLEEP); 6727 6728 rval = sata_smart_selftest_log(sata_hba_inst, sdinfo, 6729 selftest_log); 6730 6731 if (rval == 0) { 6732 int index; 6733 int count; 6734 struct smart_selftest_log_entry *entry; 6735 static const struct smart_selftest_log_entry empty = 6736 { 0 }; 6737 6738 index = selftest_log->smart_selftest_log_index; 6739 if (index == 0) 6740 goto done; 6741 --index; /* Correct for 0 origin */ 6742 entry = &selftest_log-> 6743 smart_selftest_log_entries[index]; 6744 for (count = 1; 6745 count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS; 6746 ++count) { 6747 uint8_t status; 6748 uint8_t code; 6749 uint8_t sense_key; 6750 uint8_t add_sense_code; 6751 uint8_t add_sense_code_qual; 6752 6753 if (bcmp(entry, &empty, sizeof (empty)) == 0) 6754 goto done; 6755 6756 lpp->param_code[0] = 0; 6757 lpp->param_code[1] = count; 6758 lpp->param_ctrl_flags = 6759 LOG_CTRL_LP | LOG_CTRL_LBIN; 6760 lpp->param_len = 6761 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN; 6762 6763 status = entry->smart_selftest_log_status; 6764 status >>= 4; 6765 switch (status) { 6766 case 0: 6767 default: 6768 sense_key = KEY_NO_SENSE; 6769 add_sense_code = 6770 SD_SCSI_ASC_NO_ADD_SENSE; 6771 break; 6772 case 1: 6773 sense_key = KEY_ABORTED_COMMAND; 6774 add_sense_code = 6775 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6776 add_sense_code_qual = SCSI_COMPONENT_81; 6777 break; 6778 case 2: 6779 sense_key = KEY_ABORTED_COMMAND; 6780 add_sense_code = 6781 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6782 add_sense_code_qual = SCSI_COMPONENT_82; 6783 break; 6784 case 3: 6785 sense_key = KEY_ABORTED_COMMAND; 6786 add_sense_code = 6787 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6788 add_sense_code_qual = SCSI_COMPONENT_83; 6789 break; 6790 case 4: 6791 sense_key = KEY_HARDWARE_ERROR; 6792 add_sense_code = 6793 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6794 add_sense_code_qual = SCSI_COMPONENT_84; 6795 break; 6796 case 5: 6797 sense_key = KEY_HARDWARE_ERROR; 6798 add_sense_code = 6799 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6800 add_sense_code_qual = SCSI_COMPONENT_85; 6801 break; 6802 case 6: 6803 sense_key = KEY_HARDWARE_ERROR; 6804 add_sense_code = 6805 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6806 add_sense_code_qual = SCSI_COMPONENT_86; 6807 break; 6808 case 7: 6809 sense_key = KEY_MEDIUM_ERROR; 6810 add_sense_code = 6811 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6812 add_sense_code_qual = SCSI_COMPONENT_87; 6813 break; 6814 case 8: 6815 sense_key = KEY_HARDWARE_ERROR; 6816 add_sense_code = 6817 DIAGNOSTIC_FAILURE_ON_COMPONENT; 6818 add_sense_code_qual = SCSI_COMPONENT_88; 6819 break; 6820 } 6821 code = 0; /* unspecified */ 6822 status |= (code << 4); 6823 lpp->param_values[0] = status; 6824 lpp->param_values[1] = 0; /* unspecified */ 6825 lpp->param_values[2] = entry-> 6826 smart_selftest_log_timestamp[1]; 6827 lpp->param_values[3] = entry-> 6828 smart_selftest_log_timestamp[0]; 6829 if (status != 0) { 6830 lpp->param_values[4] = 0; 6831 lpp->param_values[5] = 0; 6832 lpp->param_values[6] = 0; 6833 lpp->param_values[7] = 0; 6834 lpp->param_values[8] = entry-> 6835 smart_selftest_log_failing_lba[3]; 6836 lpp->param_values[9] = entry-> 6837 smart_selftest_log_failing_lba[2]; 6838 lpp->param_values[10] = entry-> 6839 smart_selftest_log_failing_lba[1]; 6840 lpp->param_values[11] = entry-> 6841 smart_selftest_log_failing_lba[0]; 6842 } else { /* No block address */ 6843 lpp->param_values[4] = 0xff; 6844 lpp->param_values[5] = 0xff; 6845 lpp->param_values[6] = 0xff; 6846 lpp->param_values[7] = 0xff; 6847 lpp->param_values[8] = 0xff; 6848 lpp->param_values[9] = 0xff; 6849 lpp->param_values[10] = 0xff; 6850 lpp->param_values[11] = 0xff; 6851 } 6852 lpp->param_values[12] = sense_key; 6853 lpp->param_values[13] = add_sense_code; 6854 lpp->param_values[14] = add_sense_code_qual; 6855 lpp->param_values[15] = 0; /* undefined */ 6856 6857 lpp = (struct log_parameter *) 6858 (((uint8_t *)lpp) + 6859 SCSI_LOG_PARAM_HDR_LEN + 6860 SCSI_LOG_SENSE_SELFTEST_PARAM_LEN); 6861 --index; /* back up to previous entry */ 6862 if (index < 0) { 6863 index = 6864 NUM_SMART_SELFTEST_LOG_ENTRIES - 1; 6865 } 6866 entry = &selftest_log-> 6867 smart_selftest_log_entries[index]; 6868 } 6869 } 6870 done: 6871 kmem_free(selftest_log, sizeof (struct smart_selftest_log)); 6872 } 6873 6874 return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) * 6875 SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS); 6876 } 6877 6878 /* 6879 * sata_build_lsense_page_2f() is used to create the 6880 * SCSI LOG SENSE page 0x2f (informational exceptions) 6881 * 6882 * Takes a sata_drive_info t * and the address of a buffer 6883 * in which to create the page information as well as a sata_hba_inst_t *. 6884 * 6885 * Returns the number of bytes valid in the buffer. 6886 * 6887 * Because it invokes function(s) that send synchronously executed command 6888 * to the HBA, it cannot be called in the interrupt context. 6889 */ 6890 static int 6891 sata_build_lsense_page_2f( 6892 sata_drive_info_t *sdinfo, 6893 uint8_t *buf, 6894 sata_hba_inst_t *sata_hba_inst) 6895 { 6896 struct log_parameter *lpp = (struct log_parameter *)buf; 6897 int rval; 6898 uint8_t *smart_data; 6899 uint8_t temp; 6900 sata_id_t *sata_id; 6901 #define SMART_NO_TEMP 0xff 6902 6903 lpp->param_code[0] = 0; 6904 lpp->param_code[1] = 0; 6905 lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN; 6906 6907 /* Now get the SMART status w.r.t. threshold exceeded */ 6908 rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo); 6909 switch (rval) { 6910 case 1: 6911 lpp->param_values[0] = SCSI_PREDICTED_FAILURE; 6912 lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE; 6913 break; 6914 case 0: 6915 case -1: /* failed to get data */ 6916 lpp->param_values[0] = 0; /* No failure predicted */ 6917 lpp->param_values[1] = 0; 6918 break; 6919 #if defined(SATA_DEBUG) 6920 default: 6921 cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value"); 6922 /* NOTREACHED */ 6923 #endif 6924 } 6925 6926 sata_id = &sdinfo->satadrv_id; 6927 if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP)) 6928 temp = SMART_NO_TEMP; 6929 else { 6930 /* Now get the temperature */ 6931 smart_data = kmem_zalloc(512, KM_SLEEP); 6932 rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data, 6933 SCT_STATUS_LOG_PAGE, 1); 6934 if (rval == -1) 6935 temp = SMART_NO_TEMP; 6936 else { 6937 temp = smart_data[200]; 6938 if (temp & 0x80) { 6939 if (temp & 0x7f) 6940 temp = 0; 6941 else 6942 temp = SMART_NO_TEMP; 6943 } 6944 } 6945 kmem_free(smart_data, 512); 6946 } 6947 6948 lpp->param_values[2] = temp; /* most recent temperature */ 6949 lpp->param_values[3] = 0; /* required vendor specific byte */ 6950 6951 lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN; 6952 6953 6954 return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN); 6955 } 6956 6957 /* 6958 * sata_build_lsense_page_30() is used to create the 6959 * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data). 6960 * 6961 * Takes a sata_drive_info t * and the address of a buffer 6962 * in which to create the page information as well as a sata_hba_inst_t *. 6963 * 6964 * Returns the number of bytes valid in the buffer. 6965 */ 6966 static int 6967 sata_build_lsense_page_30( 6968 sata_drive_info_t *sdinfo, 6969 uint8_t *buf, 6970 sata_hba_inst_t *sata_hba_inst) 6971 { 6972 struct smart_data *smart_data = (struct smart_data *)buf; 6973 int rval; 6974 6975 /* Now do the SMART READ DATA */ 6976 rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data); 6977 if (rval == -1) 6978 return (0); 6979 6980 return (sizeof (struct smart_data)); 6981 } 6982 6983 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */ 6984 6985 /* 6986 * Start command for ATAPI device. 6987 * This function processes scsi_pkt requests. 6988 * Only CD/DVD devices are supported. 6989 * Most commands are packet without any translation into Packet Command. 6990 * Some may be trapped and executed as SATA commands (not clear which one). 6991 * 6992 * Returns TRAN_ACCEPT if command is accepted for execution (or completed 6993 * execution). 6994 * Returns other TRAN_XXXX codes if command is not accepted or completed 6995 * (see return values for sata_hba_start()). 6996 * 6997 * Note: 6998 * Inquiry cdb format differs between transport version 2 and 3. 6999 * However, the transport version 3 devices that were checked did not adhere 7000 * to the specification (ignored MSB of the allocation length). Therefore, 7001 * the transport version is not checked, but Inquiry allocation length is 7002 * truncated to 255 bytes if the original allocation length set-up by the 7003 * target driver is greater than 255 bytes. 7004 */ 7005 static int 7006 sata_txlt_atapi(sata_pkt_txlate_t *spx) 7007 { 7008 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7009 sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd; 7010 struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 7011 sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx); 7012 sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba, 7013 &spx->txlt_sata_pkt->satapkt_device); 7014 int cport = SATA_TXLT_CPORT(spx); 7015 int cdblen; 7016 int rval, reason; 7017 int synch; 7018 union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp; 7019 7020 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 7021 7022 if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) != 7023 TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) { 7024 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 7025 return (rval); 7026 } 7027 7028 /* 7029 * ATAPI device executes some ATA commands in addition to MMC command 7030 * set. These ATA commands may be executed by the regular SATA 7031 * translation functions. None needs to be captured now. 7032 * Other commands belong to MMC command set and are delivered 7033 * to ATAPI device via Packet Command. 7034 */ 7035 7036 /* Check the size of cdb */ 7037 cdblen = scsi_cdb_size[GETGROUP(cdbp)]; 7038 if (cdblen > sdinfo->satadrv_atapi_cdb_len) { 7039 sata_log(NULL, CE_WARN, 7040 "sata: invalid ATAPI cdb length %d", 7041 scsipkt->pkt_cdblen); 7042 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 7043 return (TRAN_BADPKT); 7044 } 7045 7046 SATAATAPITRACE(spx, cdblen); 7047 7048 /* 7049 * For non-read/write commands we need to 7050 * map buffer 7051 */ 7052 switch ((uint_t)scsipkt->pkt_cdbp[0]) { 7053 case SCMD_READ: 7054 case SCMD_READ_G1: 7055 case SCMD_READ_G5: 7056 case SCMD_READ_G4: 7057 case SCMD_WRITE: 7058 case SCMD_WRITE_G1: 7059 case SCMD_WRITE_G5: 7060 case SCMD_WRITE_G4: 7061 break; 7062 default: 7063 if (bp != NULL) { 7064 if (bp->b_flags & (B_PHYS | B_PAGEIO)) 7065 bp_mapin(bp); 7066 } 7067 break; 7068 } 7069 /* 7070 * scmd->satacmd_flags.sata_data_direction default - 7071 * SATA_DIR_NODATA_XFER - is set by 7072 * sata_txlt_generic_pkt_info(). 7073 */ 7074 if (scmd->satacmd_bp) { 7075 if (scmd->satacmd_bp->b_flags & B_READ) { 7076 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 7077 } else { 7078 scmd->satacmd_flags.sata_data_direction = 7079 SATA_DIR_WRITE; 7080 } 7081 } 7082 7083 /* 7084 * Set up ATAPI packet command. 7085 */ 7086 7087 sata_atapi_packet_cmd_setup(scmd, sdinfo); 7088 7089 /* Copy cdb into sata_cmd */ 7090 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 7091 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 7092 bcopy(cdbp, scmd->satacmd_acdb, cdblen); 7093 7094 /* See note in the command header */ 7095 if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) { 7096 if (scmd->satacmd_acdb[3] != 0) 7097 scmd->satacmd_acdb[4] = 255; 7098 } 7099 7100 #ifdef SATA_DEBUG 7101 if (sata_debug_flags & SATA_DBG_ATAPI) { 7102 uint8_t *p = scmd->satacmd_acdb; 7103 char buf[3 * SATA_ATAPI_MAX_CDB_LEN]; 7104 7105 (void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN, 7106 "%02x %02x %02x %02x %02x %02x %02x %02x " 7107 "%2x %02x %02x %02x %02x %02x %02x %02x", 7108 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 7109 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 7110 buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0'; 7111 cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf); 7112 } 7113 #endif 7114 7115 /* 7116 * Preset request sense data to NO SENSE. 7117 * If there is no way to get error information via Request Sense, 7118 * the packet request sense data would not have to be modified by HBA, 7119 * but it could be returned as is. 7120 */ 7121 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN); 7122 sata_fixed_sense_data_preset( 7123 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 7124 7125 if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) { 7126 /* Need callback function */ 7127 spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion; 7128 synch = FALSE; 7129 } else 7130 synch = TRUE; 7131 7132 /* Transfer command to HBA */ 7133 if (sata_hba_start(spx, &rval) != 0) { 7134 /* Pkt not accepted for execution */ 7135 mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport)); 7136 return (rval); 7137 } 7138 mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport)); 7139 /* 7140 * If execution is non-synchronous, 7141 * a callback function will handle potential errors, translate 7142 * the response and will do a callback to a target driver. 7143 * If it was synchronous, use the same framework callback to check 7144 * an execution status. 7145 */ 7146 if (synch) { 7147 SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, 7148 "synchronous execution status %x\n", 7149 spx->txlt_sata_pkt->satapkt_reason); 7150 sata_txlt_atapi_completion(spx->txlt_sata_pkt); 7151 } 7152 return (TRAN_ACCEPT); 7153 } 7154 7155 7156 /* 7157 * ATAPI Packet command completion. 7158 * 7159 * Failure of the command passed via Packet command are considered device 7160 * error. SATA HBA driver would have to retrieve error data (via Request 7161 * Sense command delivered via error retrieval sata packet) and copy it 7162 * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data. 7163 */ 7164 static void 7165 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt) 7166 { 7167 sata_pkt_txlate_t *spx = 7168 (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 7169 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 7170 struct scsi_extended_sense *sense; 7171 struct buf *bp; 7172 int rval; 7173 7174 #ifdef SATA_DEBUG 7175 uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense; 7176 #endif 7177 7178 scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 7179 STATE_SENT_CMD | STATE_GOT_STATUS; 7180 7181 if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) { 7182 /* Normal completion */ 7183 if (sata_pkt->satapkt_cmd.satacmd_bp != NULL) 7184 scsipkt->pkt_state |= STATE_XFERRED_DATA; 7185 scsipkt->pkt_reason = CMD_CMPLT; 7186 *scsipkt->pkt_scbp = STATUS_GOOD; 7187 if (spx->txlt_tmp_buf != NULL) { 7188 /* Temporary buffer was used */ 7189 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 7190 if (bp->b_flags & B_READ) { 7191 rval = ddi_dma_sync( 7192 spx->txlt_buf_dma_handle, 0, 0, 7193 DDI_DMA_SYNC_FORCPU); 7194 ASSERT(rval == DDI_SUCCESS); 7195 bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr, 7196 bp->b_bcount); 7197 } 7198 } 7199 } else { 7200 /* 7201 * Something went wrong - analyze return 7202 */ 7203 *scsipkt->pkt_scbp = STATUS_CHECK; 7204 sense = sata_arq_sense(spx); 7205 7206 if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 7207 scsipkt->pkt_reason = CMD_INCOMPLETE; 7208 /* 7209 * We may not have ARQ data if there was a double 7210 * error. But sense data in sata packet was pre-set 7211 * with NO SENSE so it is valid even if HBA could 7212 * not retrieve a real sense data. 7213 * Just copy this sense data into scsi pkt sense area. 7214 */ 7215 bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense, 7216 SATA_ATAPI_MIN_RQSENSE_LEN); 7217 #ifdef SATA_DEBUG 7218 if (sata_debug_flags & SATA_DBG_SCSI_IF) { 7219 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 7220 "sata_txlt_atapi_completion: %02x\n" 7221 "RQSENSE: %02x %02x %02x %02x %02x %02x " 7222 " %02x %02x %02x %02x %02x %02x " 7223 " %02x %02x %02x %02x %02x %02x\n", 7224 scsipkt->pkt_reason, 7225 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 7226 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 7227 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 7228 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 7229 rqsp[16], rqsp[17]); 7230 } 7231 #endif 7232 } else { 7233 switch (sata_pkt->satapkt_reason) { 7234 case SATA_PKT_PORT_ERROR: 7235 /* 7236 * We have no device data. 7237 */ 7238 scsipkt->pkt_reason = CMD_INCOMPLETE; 7239 scsipkt->pkt_state &= ~(STATE_GOT_BUS | 7240 STATE_GOT_TARGET | STATE_SENT_CMD | 7241 STATE_GOT_STATUS); 7242 sense->es_key = KEY_HARDWARE_ERROR; 7243 break; 7244 7245 case SATA_PKT_TIMEOUT: 7246 scsipkt->pkt_reason = CMD_TIMEOUT; 7247 scsipkt->pkt_statistics |= 7248 STAT_TIMEOUT | STAT_DEV_RESET; 7249 /* 7250 * Need to check if HARDWARE_ERROR/ 7251 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more 7252 * appropriate. 7253 */ 7254 break; 7255 7256 case SATA_PKT_ABORTED: 7257 scsipkt->pkt_reason = CMD_ABORTED; 7258 scsipkt->pkt_statistics |= STAT_ABORTED; 7259 /* Should we set key COMMAND_ABPRTED? */ 7260 break; 7261 7262 case SATA_PKT_RESET: 7263 scsipkt->pkt_reason = CMD_RESET; 7264 scsipkt->pkt_statistics |= STAT_DEV_RESET; 7265 /* 7266 * May be we should set Unit Attention / 7267 * Reset. Perhaps the same should be 7268 * returned for disks.... 7269 */ 7270 sense->es_key = KEY_UNIT_ATTENTION; 7271 sense->es_add_code = SD_SCSI_ASC_RESET; 7272 break; 7273 7274 default: 7275 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 7276 "sata_txlt_atapi_completion: " 7277 "invalid packet completion reason")); 7278 scsipkt->pkt_reason = CMD_TRAN_ERR; 7279 scsipkt->pkt_state &= ~(STATE_GOT_BUS | 7280 STATE_GOT_TARGET | STATE_SENT_CMD | 7281 STATE_GOT_STATUS); 7282 break; 7283 } 7284 } 7285 } 7286 7287 SATAATAPITRACE(spx, 0); 7288 7289 if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 && 7290 scsipkt->pkt_comp != NULL) { 7291 /* scsi callback required */ 7292 (*scsipkt->pkt_comp)(scsipkt); 7293 } 7294 } 7295 7296 /* 7297 * Set up error retrieval sata command for ATAPI Packet Command error data 7298 * recovery. 7299 * 7300 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up, 7301 * returns SATA_FAILURE otherwise. 7302 */ 7303 7304 static int 7305 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo) 7306 { 7307 sata_pkt_t *spkt = spx->txlt_sata_pkt; 7308 sata_cmd_t *scmd; 7309 struct buf *bp; 7310 7311 /* 7312 * Allocate dma-able buffer error data. 7313 * Buffer allocation will take care of buffer alignment and other DMA 7314 * attributes. 7315 */ 7316 bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN); 7317 if (bp == NULL) { 7318 SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst, 7319 "sata_get_err_retrieval_pkt: " 7320 "cannot allocate buffer for error data", NULL); 7321 return (SATA_FAILURE); 7322 } 7323 bp_mapin(bp); /* make data buffer accessible */ 7324 7325 /* Operation modes are up to the caller */ 7326 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 7327 7328 /* Synchronous mode, no callback - may be changed by the caller */ 7329 spkt->satapkt_comp = NULL; 7330 spkt->satapkt_time = sata_default_pkt_time; 7331 7332 scmd = &spkt->satapkt_cmd; 7333 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 7334 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 7335 7336 sata_atapi_packet_cmd_setup(scmd, sdinfo); 7337 7338 /* 7339 * Set-up acdb. Request Sense CDB (packet command content) is 7340 * not in DMA-able buffer. Its handling is HBA-specific (how 7341 * it is transfered into packet FIS). 7342 */ 7343 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 7344 bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN); 7345 /* Following zeroing of pad bytes may not be necessary */ 7346 bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN], 7347 sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN); 7348 7349 /* 7350 * Set-up pointer to the buffer handle, so HBA can sync buffer 7351 * before accessing it. Handle is in usual place in translate struct. 7352 */ 7353 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle; 7354 7355 /* 7356 * Preset request sense data to NO SENSE. 7357 * Here it is redundant, only for a symetry with scsi-originated 7358 * packets. It should not be used for anything but debugging. 7359 */ 7360 bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN); 7361 sata_fixed_sense_data_preset( 7362 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 7363 7364 ASSERT(scmd->satacmd_num_dma_cookies != 0); 7365 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 7366 7367 return (SATA_SUCCESS); 7368 } 7369 7370 /* 7371 * Set-up ATAPI packet command. 7372 * Data transfer direction has to be set-up in sata_cmd structure prior to 7373 * calling this function. 7374 * 7375 * Returns void 7376 */ 7377 7378 static void 7379 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo) 7380 { 7381 scmd->satacmd_addr_type = 0; /* N/A */ 7382 scmd->satacmd_sec_count_lsb = 0; /* no tag */ 7383 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 7384 scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ; 7385 scmd->satacmd_lba_high_lsb = 7386 (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8); 7387 scmd->satacmd_cmd_reg = SATAC_PACKET; /* Command */ 7388 7389 /* 7390 * We want all data to be transfered via DMA. 7391 * But specify it only if drive supports DMA and DMA mode is 7392 * selected - some drives are sensitive about it. 7393 * Hopefully it wil work for all drives.... 7394 */ 7395 if (sdinfo->satadrv_settings & SATA_DEV_DMA) 7396 scmd->satacmd_features_reg = SATA_ATAPI_F_DMA; 7397 7398 /* 7399 * Features register requires special care for devices that use 7400 * Serial ATA bridge - they need an explicit specification of 7401 * the data transfer direction for Packet DMA commands. 7402 * Setting this bit is harmless if DMA is not used. 7403 * 7404 * Many drives do not implement word 80, specifying what ATA/ATAPI 7405 * spec they follow. 7406 * We are arbitrarily following the latest SerialATA 2.6 spec, 7407 * which uses ATA/ATAPI 6 specification for Identify Data, unless 7408 * ATA/ATAPI-7 support is explicitly indicated. 7409 */ 7410 if (sdinfo->satadrv_id.ai_majorversion != 0 && 7411 sdinfo->satadrv_id.ai_majorversion != 0xffff && 7412 (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) { 7413 /* 7414 * Specification of major version is valid and version 7 7415 * is supported. It does automatically imply that all 7416 * spec features are supported. For now, we assume that 7417 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete. 7418 */ 7419 if ((sdinfo->satadrv_id.ai_dirdma & 7420 SATA_ATAPI_ID_DMADIR_REQ) != 0) { 7421 if (scmd->satacmd_flags.sata_data_direction == 7422 SATA_DIR_READ) 7423 scmd->satacmd_features_reg |= 7424 SATA_ATAPI_F_DATA_DIR_READ; 7425 } 7426 } 7427 } 7428 7429 7430 #ifdef SATA_DEBUG 7431 7432 /* Display 18 bytes of Inquiry data */ 7433 static void 7434 sata_show_inqry_data(uint8_t *buf) 7435 { 7436 struct scsi_inquiry *inq = (struct scsi_inquiry *)buf; 7437 uint8_t *p; 7438 7439 cmn_err(CE_NOTE, "Inquiry data:"); 7440 cmn_err(CE_NOTE, "device type %x", inq->inq_dtype); 7441 cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb); 7442 cmn_err(CE_NOTE, "version %x", inq->inq_ansi); 7443 cmn_err(CE_NOTE, "ATAPI transport version %d", 7444 SATA_ATAPI_TRANS_VERSION(inq)); 7445 cmn_err(CE_NOTE, "response data format %d, aenc %d", 7446 inq->inq_rdf, inq->inq_aenc); 7447 cmn_err(CE_NOTE, " additional length %d", inq->inq_len); 7448 cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs); 7449 p = (uint8_t *)inq->inq_vid; 7450 cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x " 7451 "%02x %02x %02x %02x", 7452 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 7453 p = (uint8_t *)inq->inq_vid; 7454 cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c", 7455 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 7456 7457 p = (uint8_t *)inq->inq_pid; 7458 cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x " 7459 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", 7460 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 7461 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 7462 p = (uint8_t *)inq->inq_pid; 7463 cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c " 7464 "%c %c %c %c %c %c %c %c", 7465 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 7466 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 7467 7468 p = (uint8_t *)inq->inq_revision; 7469 cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x", 7470 p[0], p[1], p[2], p[3]); 7471 p = (uint8_t *)inq->inq_revision; 7472 cmn_err(CE_NOTE, "revision: %c %c %c %c", 7473 p[0], p[1], p[2], p[3]); 7474 7475 } 7476 7477 7478 static void 7479 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count) 7480 { 7481 struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt; 7482 7483 if (scsi_pkt == NULL) 7484 return; 7485 if (count != 0) { 7486 /* saving cdb */ 7487 bzero(sata_atapi_trace[sata_atapi_trace_index].acdb, 7488 SATA_ATAPI_MAX_CDB_LEN); 7489 bcopy(scsi_pkt->pkt_cdbp, 7490 sata_atapi_trace[sata_atapi_trace_index].acdb, count); 7491 } else { 7492 bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)-> 7493 sts_sensedata, 7494 sata_atapi_trace[sata_atapi_trace_index].arqs, 7495 SATA_ATAPI_MIN_RQSENSE_LEN); 7496 sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason = 7497 scsi_pkt->pkt_reason; 7498 sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason = 7499 spx->txlt_sata_pkt->satapkt_reason; 7500 7501 if (++sata_atapi_trace_index >= 64) 7502 sata_atapi_trace_index = 0; 7503 } 7504 } 7505 7506 #endif 7507 7508 /* 7509 * Fetch inquiry data from ATAPI device 7510 * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise. 7511 * 7512 * Note: 7513 * inqb pointer does not point to a DMA-able buffer. It is a local buffer 7514 * where the caller expects to see the inquiry data. 7515 * 7516 */ 7517 7518 static int 7519 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba, 7520 sata_address_t *saddr, struct scsi_inquiry *inq) 7521 { 7522 sata_pkt_txlate_t *spx; 7523 sata_pkt_t *spkt; 7524 struct buf *bp; 7525 sata_drive_info_t *sdinfo; 7526 sata_cmd_t *scmd; 7527 int rval; 7528 uint8_t *rqsp; 7529 #ifdef SATA_DEBUG 7530 char msg_buf[MAXPATHLEN]; 7531 #endif 7532 7533 ASSERT(sata_hba != NULL); 7534 7535 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 7536 spx->txlt_sata_hba_inst = sata_hba; 7537 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 7538 spkt = sata_pkt_alloc(spx, NULL); 7539 if (spkt == NULL) { 7540 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 7541 return (SATA_FAILURE); 7542 } 7543 /* address is needed now */ 7544 spkt->satapkt_device.satadev_addr = *saddr; 7545 7546 /* scsi_inquiry size buffer */ 7547 bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry)); 7548 if (bp == NULL) { 7549 sata_pkt_free(spx); 7550 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 7551 SATA_LOG_D((sata_hba, CE_WARN, 7552 "sata_get_atapi_inquiry_data: " 7553 "cannot allocate data buffer")); 7554 return (SATA_FAILURE); 7555 } 7556 bp_mapin(bp); /* make data buffer accessible */ 7557 7558 scmd = &spkt->satapkt_cmd; 7559 ASSERT(scmd->satacmd_num_dma_cookies != 0); 7560 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 7561 7562 /* Use synchronous mode */ 7563 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 7564 spkt->satapkt_comp = NULL; 7565 spkt->satapkt_time = sata_default_pkt_time; 7566 7567 /* Issue inquiry command - 6 bytes cdb, data transfer, read */ 7568 7569 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 7570 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 7571 7572 mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx))); 7573 sdinfo = sata_get_device_info(sata_hba, 7574 &spx->txlt_sata_pkt->satapkt_device); 7575 if (sdinfo == NULL) { 7576 /* we have to be carefull about the disapearing device */ 7577 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 7578 rval = SATA_FAILURE; 7579 goto cleanup; 7580 } 7581 sata_atapi_packet_cmd_setup(scmd, sdinfo); 7582 7583 /* 7584 * Set-up acdb. This works for atapi transport version 2 and later. 7585 */ 7586 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 7587 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 7588 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */ 7589 scmd->satacmd_acdb[1] = 0x00; 7590 scmd->satacmd_acdb[2] = 0x00; 7591 scmd->satacmd_acdb[3] = 0x00; 7592 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry); 7593 scmd->satacmd_acdb[5] = 0x00; 7594 7595 sata_fixed_sense_data_preset( 7596 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 7597 7598 /* Transfer command to HBA */ 7599 if (sata_hba_start(spx, &rval) != 0) { 7600 /* Pkt not accepted for execution */ 7601 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7602 "sata_get_atapi_inquiry_data: " 7603 "Packet not accepted for execution - ret: %02x", rval); 7604 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 7605 rval = SATA_FAILURE; 7606 goto cleanup; 7607 } 7608 mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx))); 7609 7610 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) { 7611 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7612 "sata_get_atapi_inquiry_data: " 7613 "Packet completed successfully - ret: %02x", rval); 7614 if (spx->txlt_buf_dma_handle != NULL) { 7615 /* 7616 * Sync buffer. Handle is in usual place in translate 7617 * struct. 7618 */ 7619 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 7620 DDI_DMA_SYNC_FORCPU); 7621 ASSERT(rval == DDI_SUCCESS); 7622 } 7623 /* 7624 * Normal completion - copy data into caller's buffer 7625 */ 7626 bcopy(bp->b_un.b_addr, (uint8_t *)inq, 7627 sizeof (struct scsi_inquiry)); 7628 #ifdef SATA_DEBUG 7629 if (sata_debug_flags & SATA_DBG_ATAPI) { 7630 sata_show_inqry_data((uint8_t *)inq); 7631 } 7632 #endif 7633 rval = SATA_SUCCESS; 7634 } else { 7635 /* 7636 * Something went wrong - analyze return - check rqsense data 7637 */ 7638 rval = SATA_FAILURE; 7639 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 7640 /* 7641 * ARQ data hopefull show something other than NO SENSE 7642 */ 7643 rqsp = scmd->satacmd_rqsense; 7644 #ifdef SATA_DEBUG 7645 if (sata_debug_flags & SATA_DBG_ATAPI) { 7646 msg_buf[0] = '\0'; 7647 (void) snprintf(msg_buf, MAXPATHLEN, 7648 "ATAPI packet completion reason: %02x\n" 7649 "RQSENSE: %02x %02x %02x %02x %02x %02x\n" 7650 " %02x %02x %02x %02x %02x %02x\n" 7651 " %02x %02x %02x %02x %02x %02x", 7652 spkt->satapkt_reason, 7653 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 7654 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 7655 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 7656 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 7657 rqsp[16], rqsp[17]); 7658 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 7659 "%s", msg_buf); 7660 } 7661 #endif 7662 } else { 7663 switch (spkt->satapkt_reason) { 7664 case SATA_PKT_PORT_ERROR: 7665 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7666 "sata_get_atapi_inquiry_data: " 7667 "packet reason: port error", NULL); 7668 break; 7669 7670 case SATA_PKT_TIMEOUT: 7671 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7672 "sata_get_atapi_inquiry_data: " 7673 "packet reason: timeout", NULL); 7674 break; 7675 7676 case SATA_PKT_ABORTED: 7677 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7678 "sata_get_atapi_inquiry_data: " 7679 "packet reason: aborted", NULL); 7680 break; 7681 7682 case SATA_PKT_RESET: 7683 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7684 "sata_get_atapi_inquiry_data: " 7685 "packet reason: reset\n", NULL); 7686 break; 7687 default: 7688 SATADBG1(SATA_DBG_ATAPI, sata_hba, 7689 "sata_get_atapi_inquiry_data: " 7690 "invalid packet reason: %02x\n", 7691 spkt->satapkt_reason); 7692 break; 7693 } 7694 } 7695 } 7696 cleanup: 7697 sata_free_local_buffer(spx); 7698 sata_pkt_free(spx); 7699 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 7700 return (rval); 7701 } 7702 7703 7704 7705 7706 7707 #if 0 7708 #ifdef SATA_DEBUG 7709 7710 /* 7711 * Test ATAPI packet command. 7712 * Single threaded test: send packet command in synch mode, process completion 7713 * 7714 */ 7715 static void 7716 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport) 7717 { 7718 sata_pkt_txlate_t *spx; 7719 sata_pkt_t *spkt; 7720 struct buf *bp; 7721 sata_device_t sata_device; 7722 sata_drive_info_t *sdinfo; 7723 sata_cmd_t *scmd; 7724 int rval; 7725 uint8_t *rqsp; 7726 7727 ASSERT(sata_hba_inst != NULL); 7728 sata_device.satadev_addr.cport = cport; 7729 sata_device.satadev_addr.pmport = 0; 7730 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 7731 sata_device.satadev_rev = SATA_DEVICE_REV; 7732 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 7733 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 7734 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 7735 if (sdinfo == NULL) { 7736 sata_log(sata_hba_inst, CE_WARN, 7737 "sata_test_atapi_packet_command: " 7738 "no device info for cport %d", 7739 sata_device.satadev_addr.cport); 7740 return; 7741 } 7742 7743 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 7744 spx->txlt_sata_hba_inst = sata_hba_inst; 7745 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 7746 spkt = sata_pkt_alloc(spx, NULL); 7747 if (spkt == NULL) { 7748 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 7749 return; 7750 } 7751 /* address is needed now */ 7752 spkt->satapkt_device.satadev_addr = sata_device.satadev_addr; 7753 7754 /* 1024k buffer */ 7755 bp = sata_alloc_local_buffer(spx, 1024); 7756 if (bp == NULL) { 7757 sata_pkt_free(spx); 7758 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 7759 sata_log(sata_hba_inst, CE_WARN, 7760 "sata_test_atapi_packet_command: " 7761 "cannot allocate data buffer"); 7762 return; 7763 } 7764 bp_mapin(bp); /* make data buffer accessible */ 7765 7766 scmd = &spkt->satapkt_cmd; 7767 ASSERT(scmd->satacmd_num_dma_cookies != 0); 7768 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 7769 7770 /* Use synchronous mode */ 7771 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 7772 7773 /* Synchronous mode, no callback - may be changed by the caller */ 7774 spkt->satapkt_comp = NULL; 7775 spkt->satapkt_time = sata_default_pkt_time; 7776 7777 /* Issue inquiry command - 6 bytes cdb, data transfer, read */ 7778 7779 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 7780 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 7781 7782 sata_atapi_packet_cmd_setup(scmd, sdinfo); 7783 7784 /* Set-up acdb. */ 7785 scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len; 7786 bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN); 7787 scmd->satacmd_acdb[0] = 0x12; /* Inquiry */ 7788 scmd->satacmd_acdb[1] = 0x00; 7789 scmd->satacmd_acdb[2] = 0x00; 7790 scmd->satacmd_acdb[3] = 0x00; 7791 scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry); 7792 scmd->satacmd_acdb[5] = 0x00; 7793 7794 sata_fixed_sense_data_preset( 7795 (struct scsi_extended_sense *)scmd->satacmd_rqsense); 7796 7797 /* Transfer command to HBA */ 7798 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 7799 if (sata_hba_start(spx, &rval) != 0) { 7800 /* Pkt not accepted for execution */ 7801 sata_log(sata_hba_inst, CE_WARN, 7802 "sata_test_atapi_packet_command: " 7803 "Packet not accepted for execution - ret: %02x", rval); 7804 mutex_exit( 7805 &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 7806 goto cleanup; 7807 } 7808 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 7809 7810 if (spx->txlt_buf_dma_handle != NULL) { 7811 /* 7812 * Sync buffer. Handle is in usual place in translate struct. 7813 */ 7814 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 7815 DDI_DMA_SYNC_FORCPU); 7816 ASSERT(rval == DDI_SUCCESS); 7817 } 7818 if (spkt->satapkt_reason == SATA_PKT_COMPLETED) { 7819 sata_log(sata_hba_inst, CE_WARN, 7820 "sata_test_atapi_packet_command: " 7821 "Packet completed successfully"); 7822 /* 7823 * Normal completion - show inquiry data 7824 */ 7825 sata_show_inqry_data((uint8_t *)bp->b_un.b_addr); 7826 } else { 7827 /* 7828 * Something went wrong - analyze return - check rqsense data 7829 */ 7830 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) { 7831 /* 7832 * ARQ data hopefull show something other than NO SENSE 7833 */ 7834 rqsp = scmd->satacmd_rqsense; 7835 sata_log(spx->txlt_sata_hba_inst, CE_WARN, 7836 "ATAPI packet completion reason: %02x\n" 7837 "RQSENSE: %02x %02x %02x %02x %02x %02x " 7838 " %02x %02x %02x %02x %02x %02x " 7839 " %02x %02x %02x %02x %02x %02x\n", 7840 spkt->satapkt_reason, 7841 rqsp[0], rqsp[1], rqsp[2], rqsp[3], 7842 rqsp[4], rqsp[5], rqsp[6], rqsp[7], 7843 rqsp[8], rqsp[9], rqsp[10], rqsp[11], 7844 rqsp[12], rqsp[13], rqsp[14], rqsp[15], 7845 rqsp[16], rqsp[17]); 7846 } else { 7847 switch (spkt->satapkt_reason) { 7848 case SATA_PKT_PORT_ERROR: 7849 sata_log(sata_hba_inst, CE_WARN, 7850 "sata_test_atapi_packet_command: " 7851 "packet reason: port error\n"); 7852 break; 7853 7854 case SATA_PKT_TIMEOUT: 7855 sata_log(sata_hba_inst, CE_WARN, 7856 "sata_test_atapi_packet_command: " 7857 "packet reason: timeout\n"); 7858 break; 7859 7860 case SATA_PKT_ABORTED: 7861 sata_log(sata_hba_inst, CE_WARN, 7862 "sata_test_atapi_packet_command: " 7863 "packet reason: aborted\n"); 7864 break; 7865 7866 case SATA_PKT_RESET: 7867 sata_log(sata_hba_inst, CE_WARN, 7868 "sata_test_atapi_packet_command: " 7869 "packet reason: reset\n"); 7870 break; 7871 default: 7872 sata_log(sata_hba_inst, CE_WARN, 7873 "sata_test_atapi_packet_command: " 7874 "invalid packet reason: %02x\n", 7875 spkt->satapkt_reason); 7876 break; 7877 } 7878 } 7879 } 7880 cleanup: 7881 sata_free_local_buffer(spx); 7882 sata_pkt_free(spx); 7883 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 7884 } 7885 7886 #endif /* SATA_DEBUG */ 7887 #endif /* 1 */ 7888 7889 7890 /* ************************** LOCAL HELPER FUNCTIONS *********************** */ 7891 7892 /* 7893 * Validate sata_tran info 7894 * SATA_FAILURE returns if structure is inconsistent or structure revision 7895 * does not match one used by the framework. 7896 * 7897 * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains 7898 * required function pointers. 7899 * Returns SATA_FAILURE otherwise. 7900 */ 7901 static int 7902 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran) 7903 { 7904 /* 7905 * SATA_TRAN_HBA_REV is the current (highest) revision number 7906 * of the SATA interface. 7907 */ 7908 if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) { 7909 sata_log(NULL, CE_WARN, 7910 "sata: invalid sata_hba_tran version %d for driver %s", 7911 sata_tran->sata_tran_hba_rev, ddi_driver_name(dip)); 7912 return (SATA_FAILURE); 7913 } 7914 7915 if (dip != sata_tran->sata_tran_hba_dip) { 7916 SATA_LOG_D((NULL, CE_WARN, 7917 "sata: inconsistent sata_tran_hba_dip " 7918 "%p / %p", sata_tran->sata_tran_hba_dip, dip)); 7919 return (SATA_FAILURE); 7920 } 7921 7922 if (sata_tran->sata_tran_probe_port == NULL || 7923 sata_tran->sata_tran_start == NULL || 7924 sata_tran->sata_tran_abort == NULL || 7925 sata_tran->sata_tran_reset_dport == NULL || 7926 sata_tran->sata_tran_hotplug_ops == NULL || 7927 sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL || 7928 sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate == 7929 NULL) { 7930 SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing " 7931 "required functions")); 7932 } 7933 return (SATA_SUCCESS); 7934 } 7935 7936 /* 7937 * Remove HBA instance from sata_hba_list. 7938 */ 7939 static void 7940 sata_remove_hba_instance(dev_info_t *dip) 7941 { 7942 sata_hba_inst_t *sata_hba_inst; 7943 7944 mutex_enter(&sata_mutex); 7945 for (sata_hba_inst = sata_hba_list; 7946 sata_hba_inst != (struct sata_hba_inst *)NULL; 7947 sata_hba_inst = sata_hba_inst->satahba_next) { 7948 if (sata_hba_inst->satahba_dip == dip) 7949 break; 7950 } 7951 7952 if (sata_hba_inst == (struct sata_hba_inst *)NULL) { 7953 #ifdef SATA_DEBUG 7954 cmn_err(CE_WARN, "sata_remove_hba_instance: " 7955 "unknown HBA instance\n"); 7956 #endif 7957 ASSERT(FALSE); 7958 } 7959 if (sata_hba_inst == sata_hba_list) { 7960 sata_hba_list = sata_hba_inst->satahba_next; 7961 if (sata_hba_list) { 7962 sata_hba_list->satahba_prev = 7963 (struct sata_hba_inst *)NULL; 7964 } 7965 if (sata_hba_inst == sata_hba_list_tail) { 7966 sata_hba_list_tail = NULL; 7967 } 7968 } else if (sata_hba_inst == sata_hba_list_tail) { 7969 sata_hba_list_tail = sata_hba_inst->satahba_prev; 7970 if (sata_hba_list_tail) { 7971 sata_hba_list_tail->satahba_next = 7972 (struct sata_hba_inst *)NULL; 7973 } 7974 } else { 7975 sata_hba_inst->satahba_prev->satahba_next = 7976 sata_hba_inst->satahba_next; 7977 sata_hba_inst->satahba_next->satahba_prev = 7978 sata_hba_inst->satahba_prev; 7979 } 7980 mutex_exit(&sata_mutex); 7981 } 7982 7983 7984 7985 7986 7987 /* 7988 * Probe all SATA ports of the specified HBA instance. 7989 * The assumption is that there are no target and attachment point minor nodes 7990 * created by the boot subsystems, so we do not need to prune device tree. 7991 * 7992 * This function is called only from sata_hba_attach(). It does not have to 7993 * be protected by controller mutex, because the hba_attached flag is not set 7994 * yet and no one would be touching this HBA instance other than this thread. 7995 * Determines if port is active and what type of the device is attached 7996 * (if any). Allocates necessary structures for each port. 7997 * 7998 * An AP (Attachement Point) node is created for each SATA device port even 7999 * when there is no device attached. 8000 */ 8001 8002 static void 8003 sata_probe_ports(sata_hba_inst_t *sata_hba_inst) 8004 { 8005 dev_info_t *dip = SATA_DIP(sata_hba_inst); 8006 int ncport, npmport; 8007 sata_cport_info_t *cportinfo; 8008 sata_drive_info_t *drive; 8009 sata_pmult_info_t *pminfo; 8010 sata_pmport_info_t *pmportinfo; 8011 sata_device_t sata_device; 8012 int rval; 8013 dev_t minor_number; 8014 char name[16]; 8015 clock_t start_time, cur_time; 8016 8017 /* 8018 * Probe controller ports first, to find port status and 8019 * any port multiplier attached. 8020 */ 8021 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 8022 /* allocate cport structure */ 8023 cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP); 8024 ASSERT(cportinfo != NULL); 8025 mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL); 8026 8027 mutex_enter(&cportinfo->cport_mutex); 8028 8029 cportinfo->cport_addr.cport = ncport; 8030 cportinfo->cport_addr.pmport = 0; 8031 cportinfo->cport_addr.qual = SATA_ADDR_CPORT; 8032 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 8033 cportinfo->cport_state |= SATA_STATE_PROBING; 8034 SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo; 8035 8036 /* 8037 * Regardless if a port is usable or not, create 8038 * an attachment point 8039 */ 8040 mutex_exit(&cportinfo->cport_mutex); 8041 minor_number = 8042 SATA_MAKE_AP_MINOR(ddi_get_instance(dip), ncport, 0, 0); 8043 (void) sprintf(name, "%d", ncport); 8044 if (ddi_create_minor_node(dip, name, S_IFCHR, 8045 minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) != 8046 DDI_SUCCESS) { 8047 sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: " 8048 "cannot create SATA attachment point for port %d", 8049 ncport); 8050 } 8051 8052 /* Probe port */ 8053 start_time = ddi_get_lbolt(); 8054 reprobe_cport: 8055 sata_device.satadev_addr.cport = ncport; 8056 sata_device.satadev_addr.pmport = 0; 8057 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 8058 sata_device.satadev_rev = SATA_DEVICE_REV; 8059 8060 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 8061 (dip, &sata_device); 8062 8063 mutex_enter(&cportinfo->cport_mutex); 8064 sata_update_port_scr(&cportinfo->cport_scr, &sata_device); 8065 if (rval != SATA_SUCCESS) { 8066 /* Something went wrong? Fail the port */ 8067 cportinfo->cport_state = SATA_PSTATE_FAILED; 8068 mutex_exit(&cportinfo->cport_mutex); 8069 continue; 8070 } 8071 cportinfo->cport_state &= ~SATA_STATE_PROBING; 8072 cportinfo->cport_state |= SATA_STATE_PROBED; 8073 cportinfo->cport_dev_type = sata_device.satadev_type; 8074 8075 cportinfo->cport_state |= SATA_STATE_READY; 8076 if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) { 8077 mutex_exit(&cportinfo->cport_mutex); 8078 continue; 8079 } 8080 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 8081 /* 8082 * There is some device attached. 8083 * Allocate device info structure 8084 */ 8085 if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) { 8086 mutex_exit(&cportinfo->cport_mutex); 8087 SATA_CPORTINFO_DRV_INFO(cportinfo) = 8088 kmem_zalloc(sizeof (sata_drive_info_t), 8089 KM_SLEEP); 8090 mutex_enter(&cportinfo->cport_mutex); 8091 } 8092 drive = SATA_CPORTINFO_DRV_INFO(cportinfo); 8093 drive->satadrv_addr = cportinfo->cport_addr; 8094 drive->satadrv_addr.qual = SATA_ADDR_DCPORT; 8095 drive->satadrv_type = cportinfo->cport_dev_type; 8096 drive->satadrv_state = SATA_STATE_UNKNOWN; 8097 8098 mutex_exit(&cportinfo->cport_mutex); 8099 if (sata_add_device(dip, sata_hba_inst, ncport, 0) != 8100 SATA_SUCCESS) { 8101 /* 8102 * Plugged device was not correctly identified. 8103 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT 8104 */ 8105 cur_time = ddi_get_lbolt(); 8106 if ((cur_time - start_time) < 8107 drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) { 8108 /* sleep for a while */ 8109 delay(drv_usectohz( 8110 SATA_DEV_RETRY_DLY)); 8111 goto reprobe_cport; 8112 } 8113 } 8114 } else { 8115 mutex_exit(&cportinfo->cport_mutex); 8116 ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT); 8117 pminfo = kmem_zalloc(sizeof (sata_pmult_info_t), 8118 KM_SLEEP); 8119 mutex_enter(&cportinfo->cport_mutex); 8120 ASSERT(pminfo != NULL); 8121 SATA_CPORTINFO_PMULT_INFO(cportinfo) = pminfo; 8122 pminfo->pmult_addr.cport = cportinfo->cport_addr.cport; 8123 pminfo->pmult_addr.pmport = SATA_PMULT_HOSTPORT; 8124 pminfo->pmult_addr.qual = SATA_ADDR_PMPORT; 8125 pminfo->pmult_num_dev_ports = 8126 sata_device.satadev_add_info; 8127 mutex_init(&pminfo->pmult_mutex, NULL, MUTEX_DRIVER, 8128 NULL); 8129 pminfo->pmult_state = SATA_STATE_PROBING; 8130 mutex_exit(&cportinfo->cport_mutex); 8131 8132 /* Probe Port Multiplier ports */ 8133 for (npmport = 0; 8134 npmport < pminfo->pmult_num_dev_ports; 8135 npmport++) { 8136 pmportinfo = kmem_zalloc( 8137 sizeof (sata_pmport_info_t), KM_SLEEP); 8138 mutex_enter(&cportinfo->cport_mutex); 8139 ASSERT(pmportinfo != NULL); 8140 pmportinfo->pmport_addr.cport = ncport; 8141 pmportinfo->pmport_addr.pmport = npmport; 8142 pmportinfo->pmport_addr.qual = 8143 SATA_ADDR_PMPORT; 8144 pminfo->pmult_dev_port[npmport] = pmportinfo; 8145 8146 mutex_init(&pmportinfo->pmport_mutex, NULL, 8147 MUTEX_DRIVER, NULL); 8148 8149 mutex_exit(&cportinfo->cport_mutex); 8150 8151 /* Create an attachment point */ 8152 minor_number = SATA_MAKE_AP_MINOR( 8153 ddi_get_instance(dip), ncport, npmport, 1); 8154 (void) sprintf(name, "%d.%d", ncport, npmport); 8155 if (ddi_create_minor_node(dip, name, S_IFCHR, 8156 minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 8157 0) != DDI_SUCCESS) { 8158 sata_log(sata_hba_inst, CE_WARN, 8159 "sata_hba_attach: " 8160 "cannot create SATA attachment " 8161 "point for port %d pmult port %d", 8162 ncport, npmport); 8163 } 8164 8165 start_time = ddi_get_lbolt(); 8166 reprobe_pmport: 8167 sata_device.satadev_addr.pmport = npmport; 8168 sata_device.satadev_addr.qual = 8169 SATA_ADDR_PMPORT; 8170 8171 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 8172 (dip, &sata_device); 8173 mutex_enter(&cportinfo->cport_mutex); 8174 8175 /* sata_update_port_info() */ 8176 sata_update_port_scr(&pmportinfo->pmport_scr, 8177 &sata_device); 8178 8179 if (rval != SATA_SUCCESS) { 8180 pmportinfo->pmport_state = 8181 SATA_PSTATE_FAILED; 8182 mutex_exit(&cportinfo->cport_mutex); 8183 continue; 8184 } 8185 pmportinfo->pmport_state &= 8186 ~SATA_STATE_PROBING; 8187 pmportinfo->pmport_state |= SATA_STATE_PROBED; 8188 pmportinfo->pmport_dev_type = 8189 sata_device.satadev_type; 8190 8191 pmportinfo->pmport_state |= SATA_STATE_READY; 8192 if (pmportinfo->pmport_dev_type == 8193 SATA_DTYPE_NONE) { 8194 mutex_exit(&cportinfo->cport_mutex); 8195 continue; 8196 } 8197 /* Port multipliers cannot be chained */ 8198 ASSERT(pmportinfo->pmport_dev_type != 8199 SATA_DTYPE_PMULT); 8200 /* 8201 * There is something attached to Port 8202 * Multiplier device port 8203 * Allocate device info structure 8204 */ 8205 if (pmportinfo->pmport_sata_drive == NULL) { 8206 mutex_exit(&cportinfo->cport_mutex); 8207 pmportinfo->pmport_sata_drive = 8208 kmem_zalloc( 8209 sizeof (sata_drive_info_t), 8210 KM_SLEEP); 8211 mutex_enter(&cportinfo->cport_mutex); 8212 } 8213 drive = pmportinfo->pmport_sata_drive; 8214 drive->satadrv_addr.cport = 8215 pmportinfo->pmport_addr.cport; 8216 drive->satadrv_addr.pmport = npmport; 8217 drive->satadrv_addr.qual = SATA_ADDR_DPMPORT; 8218 drive->satadrv_type = pmportinfo-> 8219 pmport_dev_type; 8220 drive->satadrv_state = SATA_STATE_UNKNOWN; 8221 8222 mutex_exit(&cportinfo->cport_mutex); 8223 if (sata_add_device(dip, sata_hba_inst, ncport, 8224 npmport) != SATA_SUCCESS) { 8225 /* 8226 * Plugged device was not correctly 8227 * identified. Retry, within the 8228 * SATA_DEV_IDENTIFY_TIMEOUT 8229 */ 8230 cur_time = ddi_get_lbolt(); 8231 if ((cur_time - start_time) < 8232 drv_usectohz( 8233 SATA_DEV_IDENTIFY_TIMEOUT)) { 8234 /* sleep for a while */ 8235 delay(drv_usectohz( 8236 SATA_DEV_RETRY_DLY)); 8237 goto reprobe_pmport; 8238 } 8239 } 8240 } 8241 pmportinfo->pmport_state = 8242 SATA_STATE_PROBED | SATA_STATE_READY; 8243 } 8244 } 8245 } 8246 8247 /* 8248 * Add SATA device for specified HBA instance & port (SCSI target 8249 * device nodes). 8250 * This function is called (indirectly) only from sata_hba_attach(). 8251 * A target node is created when there is a supported type device attached, 8252 * but may be removed if it cannot be put online. 8253 * 8254 * This function cannot be called from an interrupt context. 8255 * 8256 * ONLY DISK TARGET NODES ARE CREATED NOW 8257 * 8258 * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when 8259 * device identification failed - adding a device could be retried. 8260 * 8261 */ 8262 static int 8263 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, int cport, 8264 int pmport) 8265 { 8266 sata_cport_info_t *cportinfo; 8267 sata_pmult_info_t *pminfo; 8268 sata_pmport_info_t *pmportinfo; 8269 dev_info_t *cdip; /* child dip */ 8270 sata_device_t sata_device; 8271 int rval; 8272 8273 8274 8275 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 8276 ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE); 8277 mutex_enter(&cportinfo->cport_mutex); 8278 /* 8279 * Some device is attached to a controller port. 8280 * We rely on controllers distinquishing between no-device, 8281 * attached port multiplier and other kind of attached device. 8282 * We need to get Identify Device data and determine 8283 * positively the dev type before trying to attach 8284 * the target driver. 8285 */ 8286 sata_device.satadev_rev = SATA_DEVICE_REV; 8287 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 8288 /* 8289 * Not port multiplier. 8290 */ 8291 sata_device.satadev_addr = cportinfo->cport_addr; 8292 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 8293 mutex_exit(&cportinfo->cport_mutex); 8294 8295 rval = sata_probe_device(sata_hba_inst, &sata_device); 8296 if (rval != SATA_SUCCESS || 8297 sata_device.satadev_type == SATA_DTYPE_UNKNOWN) 8298 return (SATA_FAILURE); 8299 8300 mutex_enter(&cportinfo->cport_mutex); 8301 sata_show_drive_info(sata_hba_inst, 8302 SATA_CPORTINFO_DRV_INFO(cportinfo)); 8303 8304 if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) { 8305 /* 8306 * Could not determine device type or 8307 * a device is not supported. 8308 * Degrade this device to unknown. 8309 */ 8310 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 8311 mutex_exit(&cportinfo->cport_mutex); 8312 return (SATA_SUCCESS); 8313 } 8314 cportinfo->cport_dev_type = sata_device.satadev_type; 8315 cportinfo->cport_tgtnode_clean = B_TRUE; 8316 mutex_exit(&cportinfo->cport_mutex); 8317 8318 /* 8319 * Initialize device to the desired state. Even if it 8320 * fails, the device will still attach but syslog 8321 * will show the warning. 8322 */ 8323 if (sata_initialize_device(sata_hba_inst, 8324 SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) { 8325 /* Retry */ 8326 rval = sata_initialize_device(sata_hba_inst, 8327 SATA_CPORTINFO_DRV_INFO(cportinfo)); 8328 8329 if (rval == SATA_RETRY) 8330 sata_log(sata_hba_inst, CE_WARN, 8331 "SATA device at port %d - " 8332 "default device features could not be set." 8333 " Device may not operate as expected.", 8334 cportinfo->cport_addr.cport); 8335 } 8336 8337 cdip = sata_create_target_node(pdip, sata_hba_inst, 8338 &sata_device.satadev_addr); 8339 mutex_enter(&cportinfo->cport_mutex); 8340 if (cdip == NULL) { 8341 /* 8342 * Attaching target node failed. 8343 * We retain sata_drive_info structure... 8344 */ 8345 mutex_exit(&cportinfo->cport_mutex); 8346 return (SATA_SUCCESS); 8347 } 8348 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 8349 satadrv_state = SATA_STATE_READY; 8350 } else { 8351 /* This must be Port Multiplier type */ 8352 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 8353 SATA_LOG_D((sata_hba_inst, CE_WARN, 8354 "sata_add_device: " 8355 "unrecognized dev type %x", 8356 cportinfo->cport_dev_type)); 8357 mutex_exit(&cportinfo->cport_mutex); 8358 return (SATA_SUCCESS); 8359 } 8360 pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 8361 pmportinfo = pminfo->pmult_dev_port[pmport]; 8362 sata_device.satadev_addr = pmportinfo->pmport_addr; 8363 sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT; 8364 mutex_exit(&cportinfo->cport_mutex); 8365 8366 rval = sata_probe_device(sata_hba_inst, &sata_device); 8367 if (rval != SATA_SUCCESS || 8368 sata_device.satadev_type == SATA_DTYPE_UNKNOWN) { 8369 return (SATA_FAILURE); 8370 } 8371 mutex_enter(&cportinfo->cport_mutex); 8372 sata_show_drive_info(sata_hba_inst, 8373 SATA_CPORTINFO_DRV_INFO(cportinfo)); 8374 8375 if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) { 8376 /* 8377 * Could not determine device type. 8378 * Degrade this device to unknown. 8379 */ 8380 pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN; 8381 mutex_exit(&cportinfo->cport_mutex); 8382 return (SATA_SUCCESS); 8383 } 8384 pmportinfo->pmport_dev_type = sata_device.satadev_type; 8385 pmportinfo->pmport_tgtnode_clean = B_TRUE; 8386 mutex_exit(&cportinfo->cport_mutex); 8387 8388 /* 8389 * Initialize device to the desired state. 8390 * Even if it fails, the device will still 8391 * attach but syslog will show the warning. 8392 */ 8393 if (sata_initialize_device(sata_hba_inst, 8394 pmportinfo->pmport_sata_drive) != SATA_SUCCESS) { 8395 /* Retry */ 8396 rval = sata_initialize_device(sata_hba_inst, 8397 pmportinfo->pmport_sata_drive); 8398 8399 if (rval == SATA_RETRY) 8400 sata_log(sata_hba_inst, CE_WARN, 8401 "SATA device at port %d pmport %d - " 8402 "default device features could not be set." 8403 " Device may not operate as expected.", 8404 pmportinfo->pmport_addr.cport, 8405 pmportinfo->pmport_addr.pmport); 8406 } 8407 cdip = sata_create_target_node(pdip, sata_hba_inst, 8408 &sata_device.satadev_addr); 8409 mutex_enter(&cportinfo->cport_mutex); 8410 if (cdip == NULL) { 8411 /* 8412 * Attaching target node failed. 8413 * We retain sata_drive_info structure... 8414 */ 8415 mutex_exit(&cportinfo->cport_mutex); 8416 return (SATA_SUCCESS); 8417 } 8418 pmportinfo->pmport_sata_drive->satadrv_state |= 8419 SATA_STATE_READY; 8420 } 8421 mutex_exit(&cportinfo->cport_mutex); 8422 return (SATA_SUCCESS); 8423 } 8424 8425 8426 8427 /* 8428 * Create scsi target node for attached device, create node properties and 8429 * attach the node. 8430 * The node could be removed if the device onlining fails. 8431 * 8432 * A dev_info_t pointer is returned if operation is successful, NULL is 8433 * returned otherwise. 8434 * 8435 * No port multiplier support. 8436 */ 8437 8438 static dev_info_t * 8439 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst, 8440 sata_address_t *sata_addr) 8441 { 8442 dev_info_t *cdip = NULL; 8443 int rval; 8444 char *nname = NULL; 8445 char **compatible = NULL; 8446 int ncompatible; 8447 struct scsi_inquiry inq; 8448 sata_device_t sata_device; 8449 sata_drive_info_t *sdinfo; 8450 int target; 8451 int i; 8452 8453 sata_device.satadev_rev = SATA_DEVICE_REV; 8454 sata_device.satadev_addr = *sata_addr; 8455 8456 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport))); 8457 8458 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 8459 8460 target = SATA_TO_SCSI_TARGET(sata_addr->cport, 8461 sata_addr->pmport, sata_addr->qual); 8462 8463 if (sdinfo == NULL) { 8464 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 8465 sata_addr->cport))); 8466 SATA_LOG_D((sata_hba_inst, CE_WARN, 8467 "sata_create_target_node: no sdinfo for target %x", 8468 target)); 8469 return (NULL); 8470 } 8471 8472 /* 8473 * create or get scsi inquiry data, expected by 8474 * scsi_hba_nodename_compatible_get() 8475 * SATA hard disks get Identify Data translated into Inguiry Data. 8476 * ATAPI devices respond directly to Inquiry request. 8477 */ 8478 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 8479 sata_identdev_to_inquiry(sata_hba_inst, sdinfo, 8480 (uint8_t *)&inq); 8481 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 8482 sata_addr->cport))); 8483 } else { /* Assume supported ATAPI device */ 8484 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 8485 sata_addr->cport))); 8486 if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr, 8487 &inq) == SATA_FAILURE) 8488 return (NULL); 8489 /* 8490 * Save supported ATAPI transport version 8491 */ 8492 sdinfo->satadrv_atapi_trans_ver = 8493 SATA_ATAPI_TRANS_VERSION(&inq); 8494 } 8495 8496 /* determine the node name and compatible */ 8497 scsi_hba_nodename_compatible_get(&inq, NULL, 8498 inq.inq_dtype, NULL, &nname, &compatible, &ncompatible); 8499 8500 #ifdef SATA_DEBUG 8501 if (sata_debug_flags & SATA_DBG_NODES) { 8502 if (nname == NULL) { 8503 cmn_err(CE_NOTE, "sata_create_target_node: " 8504 "cannot determine nodename for target %d\n", 8505 target); 8506 } else { 8507 cmn_err(CE_WARN, "sata_create_target_node: " 8508 "target %d nodename: %s\n", target, nname); 8509 } 8510 if (compatible == NULL) { 8511 cmn_err(CE_WARN, 8512 "sata_create_target_node: no compatible name\n"); 8513 } else { 8514 for (i = 0; i < ncompatible; i++) { 8515 cmn_err(CE_WARN, "sata_create_target_node: " 8516 "compatible name: %s\n", compatible[i]); 8517 } 8518 } 8519 } 8520 #endif 8521 8522 /* if nodename can't be determined, log error and exit */ 8523 if (nname == NULL) { 8524 SATA_LOG_D((sata_hba_inst, CE_WARN, 8525 "sata_create_target_node: cannot determine nodename " 8526 "for target %d\n", target)); 8527 scsi_hba_nodename_compatible_free(nname, compatible); 8528 return (NULL); 8529 } 8530 /* 8531 * Create scsi target node 8532 */ 8533 ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip); 8534 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 8535 "device-type", "scsi"); 8536 8537 if (rval != DDI_PROP_SUCCESS) { 8538 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 8539 "updating device_type prop failed %d", rval)); 8540 goto fail; 8541 } 8542 8543 /* 8544 * Create target node properties: target & lun 8545 */ 8546 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target); 8547 if (rval != DDI_PROP_SUCCESS) { 8548 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 8549 "updating target prop failed %d", rval)); 8550 goto fail; 8551 } 8552 rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0); 8553 if (rval != DDI_PROP_SUCCESS) { 8554 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 8555 "updating target prop failed %d", rval)); 8556 goto fail; 8557 } 8558 8559 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) { 8560 /* 8561 * Add "variant" property 8562 */ 8563 rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 8564 "variant", "atapi"); 8565 if (rval != DDI_PROP_SUCCESS) { 8566 SATA_LOG_D((sata_hba_inst, CE_WARN, 8567 "sata_create_target_node: variant atapi " 8568 "property could not be created: %d", rval)); 8569 goto fail; 8570 } 8571 } 8572 /* decorate the node with compatible */ 8573 if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible", 8574 compatible, ncompatible) != DDI_PROP_SUCCESS) { 8575 SATA_LOG_D((sata_hba_inst, CE_WARN, 8576 "sata_create_target_node: FAIL compatible props cdip 0x%p", 8577 (void *)cdip)); 8578 goto fail; 8579 } 8580 8581 8582 /* 8583 * Now, try to attach the driver. If probing of the device fails, 8584 * the target node may be removed 8585 */ 8586 rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH); 8587 8588 scsi_hba_nodename_compatible_free(nname, compatible); 8589 8590 if (rval == NDI_SUCCESS) 8591 return (cdip); 8592 8593 /* target node was removed - are we sure? */ 8594 return (NULL); 8595 8596 fail: 8597 scsi_hba_nodename_compatible_free(nname, compatible); 8598 ddi_prop_remove_all(cdip); 8599 rval = ndi_devi_free(cdip); 8600 if (rval != NDI_SUCCESS) { 8601 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: " 8602 "node removal failed %d", rval)); 8603 } 8604 sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: " 8605 "cannot create target node for SATA device at port %d", 8606 sata_addr->cport); 8607 return (NULL); 8608 } 8609 8610 8611 8612 /* 8613 * Re-probe sata port, check for a device and attach info 8614 * structures when necessary. Identify Device data is fetched, if possible. 8615 * Assumption: sata address is already validated. 8616 * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of 8617 * the presence of a device and its type. 8618 * 8619 * flag arg specifies that the function should try multiple times to identify 8620 * device type and to initialize it, or it should return immediately on failure. 8621 * SATA_DEV_IDENTIFY_RETRY - retry 8622 * SATA_DEV_IDENTIFY_NORETRY - no retry 8623 * 8624 * SATA_FAILURE is returned if one of the operations failed. 8625 * 8626 * This function cannot be called in interrupt context - it may sleep. 8627 * 8628 * NOte: Port multiplier is not supported yet, although there may be some 8629 * pieces of code referencing to it. 8630 */ 8631 static int 8632 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device, 8633 int flag) 8634 { 8635 sata_cport_info_t *cportinfo; 8636 sata_drive_info_t *sdinfo, *osdinfo; 8637 boolean_t init_device = B_FALSE; 8638 int prev_device_type = SATA_DTYPE_NONE; 8639 int prev_device_settings = 0; 8640 int prev_device_state = 0; 8641 clock_t start_time; 8642 int retry = B_FALSE; 8643 int rval_probe, rval_init; 8644 8645 /* We only care about host sata cport for now */ 8646 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 8647 sata_device->satadev_addr.cport); 8648 osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 8649 if (osdinfo != NULL) { 8650 /* 8651 * We are re-probing port with a previously attached device. 8652 * Save previous device type and settings. 8653 */ 8654 prev_device_type = cportinfo->cport_dev_type; 8655 prev_device_settings = osdinfo->satadrv_settings; 8656 prev_device_state = osdinfo->satadrv_state; 8657 } 8658 if (flag == SATA_DEV_IDENTIFY_RETRY) { 8659 start_time = ddi_get_lbolt(); 8660 retry = B_TRUE; 8661 } 8662 retry_probe: 8663 8664 /* probe port */ 8665 mutex_enter(&cportinfo->cport_mutex); 8666 cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK; 8667 cportinfo->cport_state |= SATA_STATE_PROBING; 8668 mutex_exit(&cportinfo->cport_mutex); 8669 8670 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 8671 (SATA_DIP(sata_hba_inst), sata_device); 8672 8673 mutex_enter(&cportinfo->cport_mutex); 8674 if (rval_probe != SATA_SUCCESS) { 8675 cportinfo->cport_state = SATA_PSTATE_FAILED; 8676 mutex_exit(&cportinfo->cport_mutex); 8677 SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: " 8678 "SATA port %d probing failed", 8679 cportinfo->cport_addr.cport)); 8680 return (SATA_FAILURE); 8681 } 8682 8683 /* 8684 * update sata port state and set device type 8685 */ 8686 sata_update_port_info(sata_hba_inst, sata_device); 8687 cportinfo->cport_state &= ~SATA_STATE_PROBING; 8688 8689 /* 8690 * Sanity check - Port is active? Is the link active? 8691 * Is there any device attached? 8692 */ 8693 if ((cportinfo->cport_state & 8694 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) || 8695 (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 8696 SATA_PORT_DEVLINK_UP) { 8697 /* 8698 * Port in non-usable state or no link active/no device. 8699 * Free info structure if necessary (direct attached drive 8700 * only, for now! 8701 */ 8702 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 8703 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 8704 /* Add here differentiation for device attached or not */ 8705 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 8706 mutex_exit(&cportinfo->cport_mutex); 8707 if (sdinfo != NULL) 8708 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 8709 return (SATA_SUCCESS); 8710 } 8711 8712 cportinfo->cport_state |= SATA_STATE_READY; 8713 cportinfo->cport_dev_type = sata_device->satadev_type; 8714 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 8715 8716 /* 8717 * If we are re-probing the port, there may be 8718 * sata_drive_info structure attached 8719 * (or sata_pm_info, if PMult is supported). 8720 */ 8721 if (sata_device->satadev_type == SATA_DTYPE_NONE) { 8722 /* 8723 * There is no device, so remove device info structure, 8724 * if necessary. 8725 * Only direct attached drive is considered now, until 8726 * port multiplier is supported. If the previously 8727 * attached device was a port multiplier, we would need 8728 * to take care of devices attached beyond the port 8729 * multiplier. 8730 */ 8731 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 8732 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 8733 if (sdinfo != NULL) { 8734 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 8735 sata_log(sata_hba_inst, CE_WARN, 8736 "SATA device detached " 8737 "from port %d", cportinfo->cport_addr.cport); 8738 } 8739 mutex_exit(&cportinfo->cport_mutex); 8740 return (SATA_SUCCESS); 8741 } 8742 8743 if (sata_device->satadev_type != SATA_DTYPE_PMULT) { 8744 if (sdinfo == NULL) { 8745 /* 8746 * There is some device attached, but there is 8747 * no sata_drive_info structure - allocate one 8748 */ 8749 mutex_exit(&cportinfo->cport_mutex); 8750 sdinfo = kmem_zalloc( 8751 sizeof (sata_drive_info_t), KM_SLEEP); 8752 mutex_enter(&cportinfo->cport_mutex); 8753 /* 8754 * Recheck, that the port state did not change when we 8755 * released mutex. 8756 */ 8757 if (cportinfo->cport_state & SATA_STATE_READY) { 8758 SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo; 8759 sdinfo->satadrv_addr = cportinfo->cport_addr; 8760 sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT; 8761 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 8762 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 8763 } else { 8764 /* 8765 * Port is not in ready state, we 8766 * cannot attach a device. 8767 */ 8768 mutex_exit(&cportinfo->cport_mutex); 8769 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 8770 return (SATA_SUCCESS); 8771 } 8772 /* 8773 * Since we are adding device, presumably new one, 8774 * indicate that it should be initalized, 8775 * as well as some internal framework states). 8776 */ 8777 init_device = B_TRUE; 8778 } 8779 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 8780 sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual; 8781 } else { 8782 /* 8783 * The device is a port multiplier - not handled now. 8784 */ 8785 cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN; 8786 mutex_exit(&cportinfo->cport_mutex); 8787 return (SATA_SUCCESS); 8788 } 8789 mutex_exit(&cportinfo->cport_mutex); 8790 /* 8791 * Figure out what kind of device we are really 8792 * dealing with. Failure of identifying device does not fail this 8793 * function. 8794 */ 8795 rval_probe = sata_probe_device(sata_hba_inst, sata_device); 8796 rval_init = SATA_FAILURE; 8797 mutex_enter(&cportinfo->cport_mutex); 8798 if (rval_probe == SATA_SUCCESS) { 8799 /* 8800 * If we are dealing with the same type of a device as before, 8801 * restore its settings flags. 8802 */ 8803 if (osdinfo != NULL && 8804 sata_device->satadev_type == prev_device_type) 8805 sdinfo->satadrv_settings = prev_device_settings; 8806 8807 mutex_exit(&cportinfo->cport_mutex); 8808 rval_init = SATA_SUCCESS; 8809 /* Set initial device features, if necessary */ 8810 if (init_device == B_TRUE) { 8811 rval_init = sata_initialize_device(sata_hba_inst, 8812 sdinfo); 8813 } 8814 if (rval_init == SATA_SUCCESS) 8815 return (rval_init); 8816 /* else we will retry if retry was asked for */ 8817 8818 } else { 8819 /* 8820 * If there was some device info before we probe the device, 8821 * restore previous device setting, so we can retry from scratch 8822 * later. Providing, of course, that device has not disapear 8823 * during probing process. 8824 */ 8825 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 8826 if (osdinfo != NULL) { 8827 cportinfo->cport_dev_type = prev_device_type; 8828 sdinfo->satadrv_type = prev_device_type; 8829 sdinfo->satadrv_state = prev_device_state; 8830 } 8831 } else { 8832 /* device is gone */ 8833 kmem_free(sdinfo, sizeof (sata_drive_info_t)); 8834 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 8835 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 8836 mutex_exit(&cportinfo->cport_mutex); 8837 return (SATA_SUCCESS); 8838 } 8839 mutex_exit(&cportinfo->cport_mutex); 8840 } 8841 8842 if (retry) { 8843 clock_t cur_time = ddi_get_lbolt(); 8844 /* 8845 * A device was not successfully identified or initialized. 8846 * Track retry time for device identification. 8847 */ 8848 if ((cur_time - start_time) < 8849 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 8850 /* sleep for a while */ 8851 delay(drv_usectohz(SATA_DEV_RETRY_DLY)); 8852 goto retry_probe; 8853 } 8854 /* else no more retries */ 8855 mutex_enter(&cportinfo->cport_mutex); 8856 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 8857 if (rval_init == SATA_RETRY) { 8858 /* 8859 * Setting drive features have failed, but 8860 * because the drive is still accessible, 8861 * keep it and emit a warning message. 8862 */ 8863 sata_log(sata_hba_inst, CE_WARN, 8864 "SATA device at port %d - desired " 8865 "drive features could not be set. " 8866 "Device may not operate as expected.", 8867 cportinfo->cport_addr.cport); 8868 } else { 8869 SATA_CPORTINFO_DRV_INFO(cportinfo)-> 8870 satadrv_state = SATA_DSTATE_FAILED; 8871 } 8872 } 8873 mutex_exit(&cportinfo->cport_mutex); 8874 } 8875 return (SATA_SUCCESS); 8876 } 8877 8878 /* 8879 * Initialize device 8880 * Specified device is initialized to a default state. 8881 * 8882 * Returns SATA_SUCCESS if all device features are set successfully, 8883 * SATA_RETRY if device is accessible but device features were not set 8884 * successfully, and SATA_FAILURE otherwise. 8885 */ 8886 static int 8887 sata_initialize_device(sata_hba_inst_t *sata_hba_inst, 8888 sata_drive_info_t *sdinfo) 8889 { 8890 int rval; 8891 8892 sata_save_drive_settings(sdinfo); 8893 8894 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 8895 8896 sata_init_write_cache_mode(sdinfo); 8897 8898 rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0); 8899 8900 /* Determine current data transfer mode */ 8901 if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) { 8902 sdinfo->satadrv_settings &= ~SATA_DEV_DMA; 8903 } else if ((sdinfo->satadrv_id.ai_validinfo & 8904 SATA_VALIDINFO_88) != 0 && 8905 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) { 8906 sdinfo->satadrv_settings |= SATA_DEV_DMA; 8907 } else if ((sdinfo->satadrv_id.ai_dworddma & 8908 SATA_MDMA_SEL_MASK) != 0) { 8909 sdinfo->satadrv_settings |= SATA_DEV_DMA; 8910 } else 8911 /* DMA supported, not no DMA transfer mode is selected !? */ 8912 sdinfo->satadrv_settings &= ~SATA_DEV_DMA; 8913 8914 return (rval); 8915 } 8916 8917 8918 /* 8919 * Initialize write cache mode. 8920 * 8921 * The default write cache setting for SATA HDD is provided by sata_write_cache 8922 * static variable. ATAPI CD/DVDs devices have write cache default is 8923 * determined by sata_atapicdvd_write_cache static variable. 8924 * 1 - enable 8925 * 0 - disable 8926 * any other value - current drive setting 8927 * 8928 * Although there is not reason to disable write cache on CD/DVD devices, 8929 * the default setting control is provided for the maximun flexibility. 8930 * 8931 * In the future, it may be overridden by the 8932 * disk-write-cache-enable property setting, if it is defined. 8933 * Returns SATA_SUCCESS if all device features are set successfully, 8934 * SATA_FAILURE otherwise. 8935 */ 8936 static void 8937 sata_init_write_cache_mode(sata_drive_info_t *sdinfo) 8938 { 8939 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 8940 if (sata_write_cache == 1) 8941 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 8942 else if (sata_write_cache == 0) 8943 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 8944 /* 8945 * When sata_write_cache value is not 0 or 1, 8946 * a current setting of the drive's write cache is used. 8947 */ 8948 } else { /* Assume ATAPI CD/DVD device */ 8949 if (sata_atapicdvd_write_cache == 1) 8950 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 8951 else if (sata_atapicdvd_write_cache == 0) 8952 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 8953 /* 8954 * When sata_write_cache value is not 0 or 1, 8955 * a current setting of the drive's write cache is used. 8956 */ 8957 } 8958 } 8959 8960 8961 /* 8962 * Validate sata address. 8963 * Specified cport, pmport and qualifier has to match 8964 * passed sata_scsi configuration info. 8965 * The presence of an attached device is not verified. 8966 * 8967 * Returns 0 when address is valid, -1 otherwise. 8968 */ 8969 static int 8970 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport, 8971 int pmport, int qual) 8972 { 8973 if (qual == SATA_ADDR_DCPORT && pmport != 0) 8974 goto invalid_address; 8975 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 8976 goto invalid_address; 8977 if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) && 8978 ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) || 8979 (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) || 8980 (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport)))) 8981 goto invalid_address; 8982 8983 return (0); 8984 8985 invalid_address: 8986 return (-1); 8987 8988 } 8989 8990 /* 8991 * Validate scsi address 8992 * SCSI target address is translated into SATA cport/pmport and compared 8993 * with a controller port/device configuration. LUN has to be 0. 8994 * Returns 0 if a scsi target refers to an attached device, 8995 * returns 1 if address is valid but device is not attached, 8996 * returns -1 if bad address or device is of an unsupported type. 8997 * Upon return sata_device argument is set. 8998 */ 8999 static int 9000 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst, 9001 struct scsi_address *ap, sata_device_t *sata_device) 9002 { 9003 int cport, pmport, qual, rval; 9004 9005 rval = -1; /* Invalid address */ 9006 if (ap->a_lun != 0) 9007 goto out; 9008 9009 qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target); 9010 cport = SCSI_TO_SATA_CPORT(ap->a_target); 9011 pmport = SCSI_TO_SATA_PMPORT(ap->a_target); 9012 9013 if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT) 9014 goto out; 9015 9016 if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) == 9017 0) { 9018 9019 sata_cport_info_t *cportinfo; 9020 sata_pmult_info_t *pmultinfo; 9021 sata_drive_info_t *sdinfo = NULL; 9022 9023 rval = 1; /* Valid sata address */ 9024 9025 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 9026 if (qual == SATA_ADDR_DCPORT) { 9027 if (cportinfo == NULL || 9028 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 9029 goto out; 9030 9031 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT || 9032 (cportinfo->cport_dev_type & 9033 SATA_VALID_DEV_TYPE) == 0) { 9034 rval = -1; 9035 goto out; 9036 } 9037 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 9038 9039 } else if (qual == SATA_ADDR_DPMPORT) { 9040 pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo); 9041 if (pmultinfo == NULL) { 9042 rval = -1; 9043 goto out; 9044 } 9045 if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) == 9046 NULL || 9047 SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, 9048 pmport) == SATA_DTYPE_NONE) 9049 goto out; 9050 9051 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, 9052 pmport); 9053 } else { 9054 rval = -1; 9055 goto out; 9056 } 9057 if ((sdinfo == NULL) || 9058 (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0) 9059 goto out; 9060 9061 sata_device->satadev_type = sdinfo->satadrv_type; 9062 sata_device->satadev_addr.qual = qual; 9063 sata_device->satadev_addr.cport = cport; 9064 sata_device->satadev_addr.pmport = pmport; 9065 sata_device->satadev_rev = SATA_DEVICE_REV_1; 9066 return (0); 9067 } 9068 out: 9069 if (rval == 1) { 9070 SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst, 9071 "sata_validate_scsi_address: no valid target %x lun %x", 9072 ap->a_target, ap->a_lun); 9073 } 9074 return (rval); 9075 } 9076 9077 /* 9078 * Find dip corresponding to passed device number 9079 * 9080 * Returns NULL if invalid device number is passed or device cannot be found, 9081 * Returns dip is device is found. 9082 */ 9083 static dev_info_t * 9084 sata_devt_to_devinfo(dev_t dev) 9085 { 9086 dev_info_t *dip; 9087 #ifndef __lock_lint 9088 struct devnames *dnp; 9089 major_t major = getmajor(dev); 9090 int instance = SATA_MINOR2INSTANCE(getminor(dev)); 9091 9092 if (major >= devcnt) 9093 return (NULL); 9094 9095 dnp = &devnamesp[major]; 9096 LOCK_DEV_OPS(&(dnp->dn_lock)); 9097 dip = dnp->dn_head; 9098 while (dip && (ddi_get_instance(dip) != instance)) { 9099 dip = ddi_get_next(dip); 9100 } 9101 UNLOCK_DEV_OPS(&(dnp->dn_lock)); 9102 #endif 9103 9104 return (dip); 9105 } 9106 9107 9108 /* 9109 * Probe device. 9110 * This function issues Identify Device command and initializes local 9111 * sata_drive_info structure if the device can be identified. 9112 * The device type is determined by examining Identify Device 9113 * command response. 9114 * If the sata_hba_inst has linked drive info structure for this 9115 * device address, the Identify Device data is stored into sata_drive_info 9116 * structure linked to the port info structure. 9117 * 9118 * sata_device has to refer to the valid sata port(s) for HBA described 9119 * by sata_hba_inst structure. 9120 * 9121 * Returns: 9122 * SATA_SUCCESS if device type was successfully probed and port-linked 9123 * drive info structure was updated; 9124 * SATA_FAILURE if there is no device, or device was not probed 9125 * successully; 9126 * SATA_RETRY if device probe can be retried later. 9127 * If a device cannot be identified, sata_device's dev_state and dev_type 9128 * fields are set to unknown. 9129 * There are no retries in this function. Any retries should be managed by 9130 * the caller. 9131 */ 9132 9133 9134 static int 9135 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device) 9136 { 9137 sata_drive_info_t *sdinfo; 9138 sata_drive_info_t new_sdinfo; /* local drive info struct */ 9139 int rval; 9140 9141 ASSERT((SATA_CPORT_STATE(sata_hba_inst, 9142 sata_device->satadev_addr.cport) & 9143 (SATA_STATE_PROBED | SATA_STATE_READY)) != 0); 9144 9145 sata_device->satadev_type = SATA_DTYPE_NONE; 9146 9147 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 9148 sata_device->satadev_addr.cport))); 9149 9150 /* Get pointer to port-linked sata device info structure */ 9151 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 9152 if (sdinfo != NULL) { 9153 sdinfo->satadrv_state &= 9154 ~(SATA_STATE_PROBED | SATA_STATE_READY); 9155 sdinfo->satadrv_state |= SATA_STATE_PROBING; 9156 } else { 9157 /* No device to probe */ 9158 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9159 sata_device->satadev_addr.cport))); 9160 sata_device->satadev_type = SATA_DTYPE_NONE; 9161 sata_device->satadev_state = SATA_STATE_UNKNOWN; 9162 return (SATA_FAILURE); 9163 } 9164 /* 9165 * Need to issue both types of identify device command and 9166 * determine device type by examining retreived data/status. 9167 * First, ATA Identify Device. 9168 */ 9169 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 9170 new_sdinfo.satadrv_addr = sata_device->satadev_addr; 9171 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9172 sata_device->satadev_addr.cport))); 9173 new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK; 9174 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 9175 if (rval == SATA_RETRY) { 9176 /* We may try to check for ATAPI device */ 9177 if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) { 9178 /* 9179 * HBA supports ATAPI - try to issue Identify Packet 9180 * Device command. 9181 */ 9182 new_sdinfo.satadrv_type = SATA_DTYPE_ATAPICD; 9183 rval = sata_identify_device(sata_hba_inst, &new_sdinfo); 9184 } 9185 } 9186 if (rval == SATA_SUCCESS) { 9187 /* 9188 * Got something responding positively to ATA Identify Device 9189 * or to Identify Packet Device cmd. 9190 * Save last used device type. 9191 */ 9192 sata_device->satadev_type = new_sdinfo.satadrv_type; 9193 9194 /* save device info, if possible */ 9195 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 9196 sata_device->satadev_addr.cport))); 9197 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 9198 if (sdinfo == NULL) { 9199 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9200 sata_device->satadev_addr.cport))); 9201 return (SATA_FAILURE); 9202 } 9203 /* 9204 * Copy drive info into the port-linked drive info structure. 9205 */ 9206 *sdinfo = new_sdinfo; 9207 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 9208 sdinfo->satadrv_state |= SATA_STATE_PROBED; 9209 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 9210 SATA_CPORT_DEV_TYPE(sata_hba_inst, 9211 sata_device->satadev_addr.cport) = 9212 sdinfo->satadrv_type; 9213 else /* SATA_ADDR_DPMPORT */ 9214 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 9215 sata_device->satadev_addr.cport, 9216 sata_device->satadev_addr.pmport) = 9217 sdinfo->satadrv_type; 9218 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9219 sata_device->satadev_addr.cport))); 9220 return (SATA_SUCCESS); 9221 } 9222 9223 /* 9224 * It may be SATA_RETRY or SATA_FAILURE return. 9225 * Looks like we cannot determine the device type at this time. 9226 */ 9227 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 9228 sata_device->satadev_addr.cport))); 9229 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 9230 if (sdinfo != NULL) { 9231 sata_device->satadev_type = SATA_DTYPE_UNKNOWN; 9232 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 9233 sdinfo->satadrv_state &= ~SATA_STATE_PROBING; 9234 sdinfo->satadrv_state |= SATA_STATE_PROBED; 9235 if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) 9236 SATA_CPORT_DEV_TYPE(sata_hba_inst, 9237 sata_device->satadev_addr.cport) = 9238 SATA_DTYPE_UNKNOWN; 9239 else { 9240 /* SATA_ADDR_DPMPORT */ 9241 if ((SATA_PMULT_INFO(sata_hba_inst, 9242 sata_device->satadev_addr.cport) != NULL) && 9243 (SATA_PMPORT_INFO(sata_hba_inst, 9244 sata_device->satadev_addr.cport, 9245 sata_device->satadev_addr.pmport) != NULL)) 9246 SATA_PMPORT_DEV_TYPE(sata_hba_inst, 9247 sata_device->satadev_addr.cport, 9248 sata_device->satadev_addr.pmport) = 9249 SATA_DTYPE_UNKNOWN; 9250 } 9251 } 9252 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 9253 sata_device->satadev_addr.cport))); 9254 return (rval); 9255 } 9256 9257 9258 /* 9259 * Get pointer to sata_drive_info structure. 9260 * 9261 * The sata_device has to contain address (cport, pmport and qualifier) for 9262 * specified sata_scsi structure. 9263 * 9264 * Returns NULL if device address is not valid for this HBA configuration. 9265 * Otherwise, returns a pointer to sata_drive_info structure. 9266 * 9267 * This function should be called with a port mutex held. 9268 */ 9269 static sata_drive_info_t * 9270 sata_get_device_info(sata_hba_inst_t *sata_hba_inst, 9271 sata_device_t *sata_device) 9272 { 9273 uint8_t cport = sata_device->satadev_addr.cport; 9274 uint8_t pmport = sata_device->satadev_addr.pmport; 9275 uint8_t qual = sata_device->satadev_addr.qual; 9276 9277 if (cport >= SATA_NUM_CPORTS(sata_hba_inst)) 9278 return (NULL); 9279 9280 if (!(SATA_CPORT_STATE(sata_hba_inst, cport) & 9281 (SATA_STATE_PROBED | SATA_STATE_READY))) 9282 /* Port not probed yet */ 9283 return (NULL); 9284 9285 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE) 9286 return (NULL); 9287 9288 if (qual == SATA_ADDR_DCPORT) { 9289 /* Request for a device on a controller port */ 9290 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == 9291 SATA_DTYPE_PMULT) 9292 /* Port multiplier attached */ 9293 return (NULL); 9294 return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport)); 9295 } 9296 if (qual == SATA_ADDR_DPMPORT) { 9297 if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != 9298 SATA_DTYPE_PMULT) 9299 return (NULL); 9300 9301 if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) 9302 return (NULL); 9303 9304 return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport)); 9305 } 9306 9307 /* we should not get here */ 9308 return (NULL); 9309 } 9310 9311 9312 /* 9313 * sata_identify_device. 9314 * Send Identify Device command to SATA HBA driver. 9315 * If command executes successfully, update sata_drive_info structure pointed 9316 * to by sdinfo argument, including Identify Device data. 9317 * If command fails, invalidate data in sata_drive_info. 9318 * 9319 * Cannot be called from interrupt level. 9320 * 9321 * Returns: 9322 * SATA_SUCCESS if the device was identified as a supported device, 9323 * SATA_RETRY if the device was not identified but could be retried, 9324 * SATA_FAILURE if the device was not identified and identify attempt 9325 * should not be retried. 9326 */ 9327 static int 9328 sata_identify_device(sata_hba_inst_t *sata_hba_inst, 9329 sata_drive_info_t *sdinfo) 9330 { 9331 uint16_t cfg_word; 9332 int rval; 9333 9334 /* fetch device identify data */ 9335 if ((rval = sata_fetch_device_identify_data(sata_hba_inst, 9336 sdinfo)) != 0) 9337 goto fail_unknown; 9338 9339 cfg_word = sdinfo->satadrv_id.ai_config; 9340 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK && 9341 (cfg_word & SATA_ATA_TYPE_MASK) != SATA_ATA_TYPE) { 9342 /* Change device type to reflect Identify Device data */ 9343 if (((cfg_word & SATA_ATAPI_TYPE_MASK) == 9344 SATA_ATAPI_TYPE) && 9345 ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) == 9346 SATA_ATAPI_CDROM_DEV)) { 9347 sdinfo->satadrv_type = SATA_DTYPE_ATAPICD; 9348 } else { 9349 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 9350 } 9351 } else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD && 9352 (((cfg_word & SATA_ATAPI_TYPE_MASK) != SATA_ATAPI_TYPE) || 9353 ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) != SATA_ATAPI_CDROM_DEV))) { 9354 /* Change device type to reflect Identify Device data ! */ 9355 if ((sdinfo->satadrv_id.ai_config & SATA_ATA_TYPE_MASK) == 9356 SATA_ATA_TYPE) { 9357 sdinfo->satadrv_type = SATA_DTYPE_ATADISK; 9358 } else { 9359 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 9360 } 9361 } 9362 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 9363 if (sdinfo->satadrv_capacity == 0) { 9364 /* Non-LBA disk. Too bad... */ 9365 sata_log(sata_hba_inst, CE_WARN, 9366 "SATA disk device at port %d does not support LBA", 9367 sdinfo->satadrv_addr.cport); 9368 rval = SATA_FAILURE; 9369 goto fail_unknown; 9370 } 9371 } 9372 #if 0 9373 /* Left for historical reason */ 9374 /* 9375 * Some initial version of SATA spec indicated that at least 9376 * UDMA mode 4 has to be supported. It is not metioned in 9377 * SerialATA 2.6, so this restriction is removed. 9378 */ 9379 /* Check for Ultra DMA modes 6 through 0 being supported */ 9380 for (i = 6; i >= 0; --i) { 9381 if (sdinfo->satadrv_id.ai_ultradma & (1 << i)) 9382 break; 9383 } 9384 9385 /* 9386 * At least UDMA 4 mode has to be supported. If mode 4 or 9387 * higher are not supported by the device, fail this 9388 * device. 9389 */ 9390 if (i < 4) { 9391 /* No required Ultra DMA mode supported */ 9392 sata_log(sata_hba_inst, CE_WARN, 9393 "SATA disk device at port %d does not support UDMA " 9394 "mode 4 or higher", sdinfo->satadrv_addr.cport); 9395 SATA_LOG_D((sata_hba_inst, CE_WARN, 9396 "mode 4 or higher required, %d supported", i)); 9397 rval = SATA_FAILURE; 9398 goto fail_unknown; 9399 } 9400 #endif 9401 9402 return (SATA_SUCCESS); 9403 9404 fail_unknown: 9405 /* Invalidate sata_drive_info ? */ 9406 sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN; 9407 sdinfo->satadrv_state = SATA_STATE_UNKNOWN; 9408 return (rval); 9409 } 9410 9411 /* 9412 * Log/display device information 9413 */ 9414 static void 9415 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst, 9416 sata_drive_info_t *sdinfo) 9417 { 9418 int valid_version; 9419 char msg_buf[MAXPATHLEN]; 9420 int i; 9421 9422 /* Show HBA path */ 9423 (void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf); 9424 9425 cmn_err(CE_CONT, "?%s :\n", msg_buf); 9426 9427 if (sdinfo->satadrv_type == SATA_DTYPE_UNKNOWN) { 9428 (void) sprintf(msg_buf, 9429 "Unsupported SATA device type (cfg 0x%x) at ", 9430 sdinfo->satadrv_id.ai_config); 9431 } else { 9432 (void) sprintf(msg_buf, "SATA %s device at", 9433 sdinfo->satadrv_type == SATA_DTYPE_ATADISK ? 9434 "disk":"CD/DVD (ATAPI)"); 9435 } 9436 if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT) 9437 cmn_err(CE_CONT, "?\t%s port %d\n", 9438 msg_buf, sdinfo->satadrv_addr.cport); 9439 else 9440 cmn_err(CE_CONT, "?\t%s port %d pmport %d\n", 9441 msg_buf, sdinfo->satadrv_addr.cport, 9442 sdinfo->satadrv_addr.pmport); 9443 9444 bcopy(&sdinfo->satadrv_id.ai_model, msg_buf, 9445 sizeof (sdinfo->satadrv_id.ai_model)); 9446 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model)); 9447 msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0'; 9448 cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf); 9449 9450 bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf, 9451 sizeof (sdinfo->satadrv_id.ai_fw)); 9452 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw)); 9453 msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0'; 9454 cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf); 9455 9456 bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf, 9457 sizeof (sdinfo->satadrv_id.ai_drvser)); 9458 swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser)); 9459 msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0'; 9460 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 9461 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 9462 } else { 9463 /* Assuming ATAPI CD/DVD */ 9464 /* 9465 * SOme drives do not implement serial number and may 9466 * violate the spec by provinding spaces rather than zeros 9467 * in serial number field. Scan the buffer to detect it. 9468 */ 9469 for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) { 9470 if (msg_buf[i] != '\0' && msg_buf[i] != ' ') 9471 break; 9472 } 9473 if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) { 9474 cmn_err(CE_CONT, "?\tserial number - none\n"); 9475 } else { 9476 cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf); 9477 } 9478 } 9479 9480 #ifdef SATA_DEBUG 9481 if (sdinfo->satadrv_id.ai_majorversion != 0 && 9482 sdinfo->satadrv_id.ai_majorversion != 0xffff) { 9483 int i; 9484 for (i = 14; i >= 2; i--) { 9485 if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) { 9486 valid_version = i; 9487 break; 9488 } 9489 } 9490 cmn_err(CE_CONT, 9491 "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n", 9492 valid_version, 9493 sdinfo->satadrv_id.ai_majorversion, 9494 sdinfo->satadrv_id.ai_minorversion); 9495 } 9496 #endif 9497 /* Log some info */ 9498 cmn_err(CE_CONT, "?\tsupported features:\n"); 9499 msg_buf[0] = '\0'; 9500 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 9501 if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) 9502 (void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN); 9503 else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) 9504 (void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN); 9505 } 9506 if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA) 9507 (void) strlcat(msg_buf, "DMA", MAXPATHLEN); 9508 if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) 9509 (void) strlcat(msg_buf, ", Native Command Queueing", 9510 MAXPATHLEN); 9511 if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ) 9512 (void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN); 9513 if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) && 9514 (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED)) 9515 (void) strlcat(msg_buf, ", SMART", MAXPATHLEN); 9516 if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) && 9517 (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED)) 9518 (void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN); 9519 cmn_err(CE_CONT, "?\t %s\n", msg_buf); 9520 if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2) 9521 cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n"); 9522 else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1) 9523 cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n"); 9524 if (sdinfo->satadrv_features_support & 9525 (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) { 9526 msg_buf[0] = '\0'; 9527 (void) snprintf(msg_buf, MAXPATHLEN, 9528 "Supported queue depth %d", 9529 sdinfo->satadrv_queue_depth); 9530 if (!(sata_func_enable & 9531 (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ))) 9532 (void) strlcat(msg_buf, 9533 " - queueing disabled globally", MAXPATHLEN); 9534 else if (sdinfo->satadrv_queue_depth > 9535 sdinfo->satadrv_max_queue_depth) { 9536 (void) snprintf(&msg_buf[strlen(msg_buf)], 9537 MAXPATHLEN - strlen(msg_buf), ", limited to %d", 9538 (int)sdinfo->satadrv_max_queue_depth); 9539 } 9540 cmn_err(CE_CONT, "?\t%s\n", msg_buf); 9541 } 9542 9543 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 9544 #ifdef __i386 9545 (void) sprintf(msg_buf, "\tcapacity = %llu sectors\n", 9546 sdinfo->satadrv_capacity); 9547 #else 9548 (void) sprintf(msg_buf, "\tcapacity = %lu sectors\n", 9549 sdinfo->satadrv_capacity); 9550 #endif 9551 cmn_err(CE_CONT, "?%s", msg_buf); 9552 } 9553 } 9554 9555 9556 /* 9557 * sata_save_drive_settings extracts current setting of the device and stores 9558 * it for future reference, in case the device setup would need to be restored 9559 * after the device reset. 9560 * 9561 * For all devices read ahead and write cache settings are saved, if the 9562 * device supports these features at all. 9563 * For ATAPI devices the Removable Media Status Notification setting is saved. 9564 */ 9565 static void 9566 sata_save_drive_settings(sata_drive_info_t *sdinfo) 9567 { 9568 if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) || 9569 (sdinfo->satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) { 9570 9571 /* Current setting of Read Ahead (and Read Cache) */ 9572 if (sdinfo->satadrv_id.ai_features85 & SATA_LOOK_AHEAD) 9573 sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD; 9574 else 9575 sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD; 9576 9577 /* Current setting of Write Cache */ 9578 if (sdinfo->satadrv_id.ai_features85 & SATA_WRITE_CACHE) 9579 sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE; 9580 else 9581 sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE; 9582 } 9583 9584 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) { 9585 if (sdinfo->satadrv_id.ai_cmdset83 & SATA_RM_STATUS_NOTIFIC) 9586 sdinfo->satadrv_settings |= SATA_DEV_RMSN; 9587 else 9588 sdinfo->satadrv_settings &= ~SATA_DEV_RMSN; 9589 } 9590 } 9591 9592 9593 /* 9594 * sata_check_capacity function determines a disk capacity 9595 * and addressing mode (LBA28/LBA48) by examining a disk identify device data. 9596 * 9597 * NOTE: CHS mode is not supported! If a device does not support LBA, 9598 * this function is not called. 9599 * 9600 * Returns device capacity in number of blocks, i.e. largest addressable LBA+1 9601 */ 9602 static uint64_t 9603 sata_check_capacity(sata_drive_info_t *sdinfo) 9604 { 9605 uint64_t capacity = 0; 9606 int i; 9607 9608 if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK || 9609 !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT) 9610 /* Capacity valid only for LBA-addressable disk devices */ 9611 return (0); 9612 9613 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) && 9614 (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) && 9615 (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) { 9616 /* LBA48 mode supported and enabled */ 9617 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 | 9618 SATA_DEV_F_LBA28; 9619 for (i = 3; i >= 0; --i) { 9620 capacity <<= 16; 9621 capacity += sdinfo->satadrv_id.ai_addrsecxt[i]; 9622 } 9623 } else { 9624 capacity = sdinfo->satadrv_id.ai_addrsec[1]; 9625 capacity <<= 16; 9626 capacity += sdinfo->satadrv_id.ai_addrsec[0]; 9627 if (capacity >= 0x1000000) 9628 /* LBA28 mode */ 9629 sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28; 9630 } 9631 return (capacity); 9632 } 9633 9634 9635 /* 9636 * Allocate consistent buffer for DMA transfer 9637 * 9638 * Cannot be called from interrupt level or with mutex held - it may sleep. 9639 * 9640 * Returns pointer to allocated buffer structure, or NULL if allocation failed. 9641 */ 9642 static struct buf * 9643 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len) 9644 { 9645 struct scsi_address ap; 9646 struct buf *bp; 9647 ddi_dma_attr_t cur_dma_attr; 9648 9649 ASSERT(spx->txlt_sata_pkt != NULL); 9650 ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran; 9651 ap.a_target = SATA_TO_SCSI_TARGET( 9652 spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport, 9653 spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport, 9654 spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual); 9655 ap.a_lun = 0; 9656 9657 bp = scsi_alloc_consistent_buf(&ap, NULL, len, 9658 B_READ, SLEEP_FUNC, NULL); 9659 9660 if (bp != NULL) { 9661 /* Allocate DMA resources for this buffer */ 9662 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp; 9663 /* 9664 * We use a local version of the dma_attr, to account 9665 * for a device addressing limitations. 9666 * sata_adjust_dma_attr() will handle sdinfo == NULL which 9667 * will cause dma attributes to be adjusted to a lowest 9668 * acceptable level. 9669 */ 9670 sata_adjust_dma_attr(NULL, 9671 SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr); 9672 9673 if (sata_dma_buf_setup(spx, PKT_CONSISTENT, 9674 SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) { 9675 scsi_free_consistent_buf(bp); 9676 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 9677 bp = NULL; 9678 } 9679 } 9680 return (bp); 9681 } 9682 9683 /* 9684 * Release local buffer (consistent buffer for DMA transfer) allocated 9685 * via sata_alloc_local_buffer(). 9686 */ 9687 static void 9688 sata_free_local_buffer(sata_pkt_txlate_t *spx) 9689 { 9690 ASSERT(spx->txlt_sata_pkt != NULL); 9691 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL); 9692 9693 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0; 9694 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL; 9695 9696 if (spx->txlt_buf_dma_handle != NULL) { 9697 /* Free DMA resources */ 9698 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle); 9699 ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 9700 spx->txlt_buf_dma_handle = 0; 9701 9702 if (spx->txlt_dma_cookie_list != &spx->txlt_dma_cookie) { 9703 kmem_free(spx->txlt_dma_cookie_list, 9704 spx->txlt_dma_cookie_list_len * 9705 sizeof (ddi_dma_cookie_t)); 9706 spx->txlt_dma_cookie_list = NULL; 9707 spx->txlt_dma_cookie_list_len = 0; 9708 } 9709 } 9710 9711 /* Free buffer */ 9712 scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp); 9713 spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL; 9714 } 9715 9716 9717 9718 9719 /* 9720 * Allocate sata_pkt 9721 * Pkt structure version and embedded strcutures version are initialized. 9722 * sata_pkt and sata_pkt_txlate structures are cross-linked. 9723 * 9724 * Since this may be called in interrupt context by sata_scsi_init_pkt, 9725 * callback argument determines if it can sleep or not. 9726 * Hence, it should not be called from interrupt context. 9727 * 9728 * If successful, non-NULL pointer to a sata pkt is returned. 9729 * Upon failure, NULL pointer is returned. 9730 */ 9731 static sata_pkt_t * 9732 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t)) 9733 { 9734 sata_pkt_t *spkt; 9735 int kmsflag; 9736 9737 kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP; 9738 spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag); 9739 if (spkt == NULL) { 9740 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 9741 "sata_pkt_alloc: failed")); 9742 return (NULL); 9743 } 9744 spkt->satapkt_rev = SATA_PKT_REV; 9745 spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV; 9746 spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV; 9747 spkt->satapkt_framework_private = spx; 9748 spx->txlt_sata_pkt = spkt; 9749 return (spkt); 9750 } 9751 9752 /* 9753 * Free sata pkt allocated via sata_pkt_alloc() 9754 */ 9755 static void 9756 sata_pkt_free(sata_pkt_txlate_t *spx) 9757 { 9758 ASSERT(spx->txlt_sata_pkt != NULL); 9759 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL); 9760 kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t)); 9761 spx->txlt_sata_pkt = NULL; 9762 } 9763 9764 9765 /* 9766 * Adjust DMA attributes. 9767 * SCSI cmds block count is up to 24 bits, SATA cmd block count vary 9768 * from 8 bits to 16 bits, depending on a command being used. 9769 * Limiting max block count arbitrarily to 256 for all read/write 9770 * commands may affects performance, so check both the device and 9771 * controller capability before adjusting dma attributes. 9772 */ 9773 void 9774 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr, 9775 ddi_dma_attr_t *adj_dma_attr) 9776 { 9777 uint32_t count_max; 9778 9779 /* Copy original attributes */ 9780 *adj_dma_attr = *dma_attr; 9781 /* 9782 * Things to consider: device addressing capability, 9783 * "excessive" controller DMA capabilities. 9784 * If a device is being probed/initialized, there are 9785 * no device info - use default limits then. 9786 */ 9787 if (sdinfo == NULL) { 9788 count_max = dma_attr->dma_attr_granular * 0x100; 9789 if (dma_attr->dma_attr_count_max > count_max) 9790 adj_dma_attr->dma_attr_count_max = count_max; 9791 if (dma_attr->dma_attr_maxxfer > count_max) 9792 adj_dma_attr->dma_attr_maxxfer = count_max; 9793 return; 9794 } 9795 9796 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 9797 if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) { 9798 /* 9799 * 16-bit sector count may be used - we rely on 9800 * the assumption that only read and write cmds 9801 * will request more than 256 sectors worth of data 9802 */ 9803 count_max = adj_dma_attr->dma_attr_granular * 0x10000; 9804 } else { 9805 /* 9806 * 8-bit sector count will be used - default limits 9807 * for dma attributes 9808 */ 9809 count_max = adj_dma_attr->dma_attr_granular * 0x100; 9810 } 9811 /* 9812 * Adjust controler dma attributes, if necessary 9813 */ 9814 if (dma_attr->dma_attr_count_max > count_max) 9815 adj_dma_attr->dma_attr_count_max = count_max; 9816 if (dma_attr->dma_attr_maxxfer > count_max) 9817 adj_dma_attr->dma_attr_maxxfer = count_max; 9818 } 9819 } 9820 9821 9822 /* 9823 * Allocate DMA resources for the buffer 9824 * This function handles initial DMA resource allocation as well as 9825 * DMA window shift and may be called repeatedly for the same DMA window 9826 * until all DMA cookies in the DMA window are processed. 9827 * To guarantee that there is always a coherent set of cookies to process 9828 * by SATA HBA driver (observing alignment, device granularity, etc.), 9829 * the number of slots for DMA cookies is equal to lesser of a number of 9830 * cookies in a DMA window and a max number of scatter/gather entries. 9831 * 9832 * Returns DDI_SUCCESS upon successful operation. 9833 * Return failure code of a failing command or DDI_FAILURE when 9834 * internal cleanup failed. 9835 */ 9836 static int 9837 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags, 9838 int (*callback)(caddr_t), caddr_t arg, 9839 ddi_dma_attr_t *cur_dma_attr) 9840 { 9841 int rval; 9842 off_t offset; 9843 size_t size; 9844 int max_sg_len, req_len, i; 9845 uint_t dma_flags; 9846 struct buf *bp; 9847 uint64_t cur_txfer_len; 9848 9849 9850 ASSERT(spx->txlt_sata_pkt != NULL); 9851 bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp; 9852 ASSERT(bp != NULL); 9853 9854 9855 if (spx->txlt_buf_dma_handle == NULL) { 9856 /* 9857 * No DMA resources allocated so far - this is a first call 9858 * for this sata pkt. 9859 */ 9860 rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst), 9861 cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle); 9862 9863 if (rval != DDI_SUCCESS) { 9864 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 9865 "sata_dma_buf_setup: no buf DMA resources %x", 9866 rval)); 9867 return (rval); 9868 } 9869 9870 if (bp->b_flags & B_READ) 9871 dma_flags = DDI_DMA_READ; 9872 else 9873 dma_flags = DDI_DMA_WRITE; 9874 9875 if (flags & PKT_CONSISTENT) 9876 dma_flags |= DDI_DMA_CONSISTENT; 9877 9878 if (flags & PKT_DMA_PARTIAL) 9879 dma_flags |= DDI_DMA_PARTIAL; 9880 9881 /* 9882 * Check buffer alignment and size against dma attributes 9883 * Consider dma_attr_align only. There may be requests 9884 * with the size lower than device granularity, but they 9885 * will not read/write from/to the device, so no adjustment 9886 * is necessary. The dma_attr_minxfer theoretically should 9887 * be considered, but no HBA driver is checking it. 9888 */ 9889 if (IS_P2ALIGNED(bp->b_un.b_addr, 9890 cur_dma_attr->dma_attr_align)) { 9891 rval = ddi_dma_buf_bind_handle( 9892 spx->txlt_buf_dma_handle, 9893 bp, dma_flags, callback, arg, 9894 &spx->txlt_dma_cookie, 9895 &spx->txlt_curwin_num_dma_cookies); 9896 } else { /* Buffer is not aligned */ 9897 9898 int (*ddicallback)(caddr_t); 9899 size_t bufsz; 9900 9901 /* Check id sleeping is allowed */ 9902 ddicallback = (callback == NULL_FUNC) ? 9903 DDI_DMA_DONTWAIT : DDI_DMA_SLEEP; 9904 9905 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 9906 "mis-aligned buffer: addr=0x%p, cnt=%lu", 9907 (void *)bp->b_un.b_addr, bp->b_bcount); 9908 9909 if (bp->b_flags & (B_PAGEIO|B_PHYS)) 9910 /* 9911 * CPU will need to access data in the buffer 9912 * (for copying) so map it. 9913 */ 9914 bp_mapin(bp); 9915 9916 ASSERT(spx->txlt_tmp_buf == NULL); 9917 9918 /* Buffer may be padded by ddi_dma_mem_alloc()! */ 9919 rval = ddi_dma_mem_alloc( 9920 spx->txlt_buf_dma_handle, 9921 bp->b_bcount, 9922 &sata_acc_attr, 9923 DDI_DMA_STREAMING, 9924 ddicallback, NULL, 9925 &spx->txlt_tmp_buf, 9926 &bufsz, 9927 &spx->txlt_tmp_buf_handle); 9928 9929 if (rval != DDI_SUCCESS) { 9930 /* DMA mapping failed */ 9931 (void) ddi_dma_free_handle( 9932 &spx->txlt_buf_dma_handle); 9933 spx->txlt_buf_dma_handle = NULL; 9934 #ifdef SATA_DEBUG 9935 mbuffail_count++; 9936 #endif 9937 SATADBG1(SATA_DBG_DMA_SETUP, 9938 spx->txlt_sata_hba_inst, 9939 "sata_dma_buf_setup: " 9940 "buf dma mem alloc failed %x\n", rval); 9941 return (rval); 9942 } 9943 ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf, 9944 cur_dma_attr->dma_attr_align)); 9945 9946 #ifdef SATA_DEBUG 9947 mbuf_count++; 9948 9949 if (bp->b_bcount != bufsz) 9950 /* 9951 * This will require special handling, because 9952 * DMA cookies will be based on the temporary 9953 * buffer size, not the original buffer 9954 * b_bcount, so the residue may have to 9955 * be counted differently. 9956 */ 9957 SATADBG2(SATA_DBG_DMA_SETUP, 9958 spx->txlt_sata_hba_inst, 9959 "sata_dma_buf_setup: bp size %x != " 9960 "bufsz %x\n", bp->b_bcount, bufsz); 9961 #endif 9962 if (dma_flags & DDI_DMA_WRITE) { 9963 /* 9964 * Write operation - copy data into 9965 * an aligned temporary buffer. Buffer will be 9966 * synced for device by ddi_dma_addr_bind_handle 9967 */ 9968 bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf, 9969 bp->b_bcount); 9970 } 9971 9972 rval = ddi_dma_addr_bind_handle( 9973 spx->txlt_buf_dma_handle, 9974 NULL, 9975 spx->txlt_tmp_buf, 9976 bufsz, dma_flags, ddicallback, 0, 9977 &spx->txlt_dma_cookie, 9978 &spx->txlt_curwin_num_dma_cookies); 9979 } 9980 9981 switch (rval) { 9982 case DDI_DMA_PARTIAL_MAP: 9983 SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 9984 "sata_dma_buf_setup: DMA Partial Map\n", NULL); 9985 /* 9986 * Partial DMA mapping. 9987 * Retrieve number of DMA windows for this request. 9988 */ 9989 if (ddi_dma_numwin(spx->txlt_buf_dma_handle, 9990 &spx->txlt_num_dma_win) != DDI_SUCCESS) { 9991 if (spx->txlt_tmp_buf != NULL) { 9992 ddi_dma_mem_free( 9993 &spx->txlt_tmp_buf_handle); 9994 spx->txlt_tmp_buf = NULL; 9995 } 9996 (void) ddi_dma_unbind_handle( 9997 spx->txlt_buf_dma_handle); 9998 (void) ddi_dma_free_handle( 9999 &spx->txlt_buf_dma_handle); 10000 spx->txlt_buf_dma_handle = NULL; 10001 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 10002 "sata_dma_buf_setup: numwin failed\n")); 10003 return (DDI_FAILURE); 10004 } 10005 SATADBG2(SATA_DBG_DMA_SETUP, 10006 spx->txlt_sata_hba_inst, 10007 "sata_dma_buf_setup: windows: %d, cookies: %d\n", 10008 spx->txlt_num_dma_win, 10009 spx->txlt_curwin_num_dma_cookies); 10010 spx->txlt_cur_dma_win = 0; 10011 break; 10012 10013 case DDI_DMA_MAPPED: 10014 /* DMA fully mapped */ 10015 spx->txlt_num_dma_win = 1; 10016 spx->txlt_cur_dma_win = 0; 10017 SATADBG1(SATA_DBG_DMA_SETUP, 10018 spx->txlt_sata_hba_inst, 10019 "sata_dma_buf_setup: windows: 1 " 10020 "cookies: %d\n", spx->txlt_curwin_num_dma_cookies); 10021 break; 10022 10023 default: 10024 /* DMA mapping failed */ 10025 if (spx->txlt_tmp_buf != NULL) { 10026 ddi_dma_mem_free( 10027 &spx->txlt_tmp_buf_handle); 10028 spx->txlt_tmp_buf = NULL; 10029 } 10030 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 10031 spx->txlt_buf_dma_handle = NULL; 10032 SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN, 10033 "sata_dma_buf_setup: buf dma handle binding " 10034 "failed %x\n", rval)); 10035 return (rval); 10036 } 10037 spx->txlt_curwin_processed_dma_cookies = 0; 10038 spx->txlt_dma_cookie_list = NULL; 10039 } else { 10040 /* 10041 * DMA setup is reused. Check if we need to process more 10042 * cookies in current window, or to get next window, if any. 10043 */ 10044 10045 ASSERT(spx->txlt_curwin_processed_dma_cookies <= 10046 spx->txlt_curwin_num_dma_cookies); 10047 10048 if (spx->txlt_curwin_processed_dma_cookies == 10049 spx->txlt_curwin_num_dma_cookies) { 10050 /* 10051 * All cookies from current DMA window were processed. 10052 * Get next DMA window. 10053 */ 10054 spx->txlt_cur_dma_win++; 10055 if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) { 10056 (void) ddi_dma_getwin(spx->txlt_buf_dma_handle, 10057 spx->txlt_cur_dma_win, &offset, &size, 10058 &spx->txlt_dma_cookie, 10059 &spx->txlt_curwin_num_dma_cookies); 10060 spx->txlt_curwin_processed_dma_cookies = 0; 10061 } else { 10062 /* No more windows! End of request! */ 10063 /* What to do? - panic for now */ 10064 ASSERT(spx->txlt_cur_dma_win >= 10065 spx->txlt_num_dma_win); 10066 10067 spx->txlt_curwin_num_dma_cookies = 0; 10068 spx->txlt_curwin_processed_dma_cookies = 0; 10069 spx->txlt_sata_pkt-> 10070 satapkt_cmd.satacmd_num_dma_cookies = 0; 10071 return (DDI_SUCCESS); 10072 } 10073 } 10074 } 10075 /* There better be at least one DMA cookie outstanding */ 10076 ASSERT((spx->txlt_curwin_num_dma_cookies - 10077 spx->txlt_curwin_processed_dma_cookies) > 0); 10078 10079 if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) { 10080 /* The default cookie slot was used in previous run */ 10081 ASSERT(spx->txlt_curwin_processed_dma_cookies == 0); 10082 spx->txlt_dma_cookie_list = NULL; 10083 spx->txlt_dma_cookie_list_len = 0; 10084 } 10085 if (spx->txlt_curwin_processed_dma_cookies == 0) { 10086 /* 10087 * Processing a new DMA window - set-up dma cookies list. 10088 * We may reuse previously allocated cookie array if it is 10089 * possible. 10090 */ 10091 if (spx->txlt_dma_cookie_list != NULL && 10092 spx->txlt_dma_cookie_list_len < 10093 spx->txlt_curwin_num_dma_cookies) { 10094 /* 10095 * New DMA window contains more cookies than 10096 * the previous one. We need larger cookie list - free 10097 * the old one. 10098 */ 10099 (void) kmem_free(spx->txlt_dma_cookie_list, 10100 spx->txlt_dma_cookie_list_len * 10101 sizeof (ddi_dma_cookie_t)); 10102 spx->txlt_dma_cookie_list = NULL; 10103 spx->txlt_dma_cookie_list_len = 0; 10104 } 10105 if (spx->txlt_dma_cookie_list == NULL) { 10106 /* 10107 * Calculate lesser of number of cookies in this 10108 * DMA window and number of s/g entries. 10109 */ 10110 max_sg_len = cur_dma_attr->dma_attr_sgllen; 10111 req_len = MIN(max_sg_len, 10112 spx->txlt_curwin_num_dma_cookies); 10113 10114 /* Allocate new dma cookie array if necessary */ 10115 if (req_len == 1) { 10116 /* Only one cookie - no need for a list */ 10117 spx->txlt_dma_cookie_list = 10118 &spx->txlt_dma_cookie; 10119 spx->txlt_dma_cookie_list_len = 1; 10120 } else { 10121 /* 10122 * More than one cookie - try to allocate space. 10123 */ 10124 spx->txlt_dma_cookie_list = kmem_zalloc( 10125 sizeof (ddi_dma_cookie_t) * req_len, 10126 callback == NULL_FUNC ? KM_NOSLEEP : 10127 KM_SLEEP); 10128 if (spx->txlt_dma_cookie_list == NULL) { 10129 SATADBG1(SATA_DBG_DMA_SETUP, 10130 spx->txlt_sata_hba_inst, 10131 "sata_dma_buf_setup: cookie list " 10132 "allocation failed\n", NULL); 10133 /* 10134 * We could not allocate space for 10135 * neccessary number of dma cookies in 10136 * this window, so we fail this request. 10137 * Next invocation would try again to 10138 * allocate space for cookie list. 10139 * Note:Packet residue was not modified. 10140 */ 10141 return (DDI_DMA_NORESOURCES); 10142 } else { 10143 spx->txlt_dma_cookie_list_len = req_len; 10144 } 10145 } 10146 } 10147 /* 10148 * Fetch DMA cookies into cookie list in sata_pkt_txlate. 10149 * First cookie was already fetched. 10150 */ 10151 *(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie; 10152 cur_txfer_len = 10153 (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size; 10154 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1; 10155 spx->txlt_curwin_processed_dma_cookies++; 10156 for (i = 1; (i < spx->txlt_dma_cookie_list_len) && 10157 (i < spx->txlt_curwin_num_dma_cookies); i++) { 10158 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 10159 &spx->txlt_dma_cookie_list[i]); 10160 cur_txfer_len += 10161 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 10162 spx->txlt_curwin_processed_dma_cookies++; 10163 spx->txlt_sata_pkt-> 10164 satapkt_cmd.satacmd_num_dma_cookies += 1; 10165 } 10166 } else { 10167 SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst, 10168 "sata_dma_buf_setup: sliding within DMA window, " 10169 "cur cookie %d, total cookies %d\n", 10170 spx->txlt_curwin_processed_dma_cookies, 10171 spx->txlt_curwin_num_dma_cookies); 10172 10173 /* 10174 * Not all cookies from the current dma window were used because 10175 * of s/g limitation. 10176 * There is no need to re-size the list - it was set at 10177 * optimal size, or only default entry is used (s/g = 1). 10178 */ 10179 if (spx->txlt_dma_cookie_list == NULL) { 10180 spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie; 10181 spx->txlt_dma_cookie_list_len = 1; 10182 } 10183 /* 10184 * Since we are processing remaining cookies in a DMA window, 10185 * there may be less of them than the number of entries in the 10186 * current dma cookie list. 10187 */ 10188 req_len = MIN(spx->txlt_dma_cookie_list_len, 10189 (spx->txlt_curwin_num_dma_cookies - 10190 spx->txlt_curwin_processed_dma_cookies)); 10191 10192 /* Fetch the next batch of cookies */ 10193 for (i = 0, cur_txfer_len = 0; i < req_len; i++) { 10194 ddi_dma_nextcookie(spx->txlt_buf_dma_handle, 10195 &spx->txlt_dma_cookie_list[i]); 10196 cur_txfer_len += 10197 (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size; 10198 spx->txlt_sata_pkt-> 10199 satapkt_cmd.satacmd_num_dma_cookies++; 10200 spx->txlt_curwin_processed_dma_cookies++; 10201 } 10202 } 10203 10204 ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0); 10205 10206 /* Point sata_cmd to the cookie list */ 10207 spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = 10208 &spx->txlt_dma_cookie_list[0]; 10209 10210 /* Remember number of DMA cookies passed in sata packet */ 10211 spx->txlt_num_dma_cookies = 10212 spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies; 10213 10214 ASSERT(cur_txfer_len != 0); 10215 if (cur_txfer_len <= bp->b_bcount) 10216 spx->txlt_total_residue -= cur_txfer_len; 10217 else { 10218 /* 10219 * Temporary DMA buffer has been padded by 10220 * ddi_dma_mem_alloc()! 10221 * This requires special handling, because DMA cookies are 10222 * based on the temporary buffer size, not the b_bcount, 10223 * and we have extra bytes to transfer - but the packet 10224 * residue has to stay correct because we will copy only 10225 * the requested number of bytes. 10226 */ 10227 spx->txlt_total_residue -= bp->b_bcount; 10228 } 10229 10230 return (DDI_SUCCESS); 10231 } 10232 10233 /* 10234 * Common routine for releasing DMA resources 10235 */ 10236 static void 10237 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx) 10238 { 10239 if (spx->txlt_buf_dma_handle != NULL) { 10240 if (spx->txlt_tmp_buf != NULL) { 10241 /* 10242 * Intermediate DMA buffer was allocated. 10243 * Free allocated buffer and associated access handle. 10244 */ 10245 ddi_dma_mem_free(&spx->txlt_tmp_buf_handle); 10246 spx->txlt_tmp_buf = NULL; 10247 } 10248 /* 10249 * Free DMA resources - cookies and handles 10250 */ 10251 /* ASSERT(spx->txlt_dma_cookie_list != NULL); */ 10252 if (spx->txlt_dma_cookie_list != NULL) { 10253 if (spx->txlt_dma_cookie_list != 10254 &spx->txlt_dma_cookie) { 10255 (void) kmem_free(spx->txlt_dma_cookie_list, 10256 spx->txlt_dma_cookie_list_len * 10257 sizeof (ddi_dma_cookie_t)); 10258 spx->txlt_dma_cookie_list = NULL; 10259 } 10260 } 10261 (void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle); 10262 (void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle); 10263 spx->txlt_buf_dma_handle = NULL; 10264 } 10265 } 10266 10267 /* 10268 * Free DMA resources 10269 * Used by the HBA driver to release DMA resources that it does not use. 10270 * 10271 * Returns Void 10272 */ 10273 void 10274 sata_free_dma_resources(sata_pkt_t *sata_pkt) 10275 { 10276 sata_pkt_txlate_t *spx; 10277 10278 if (sata_pkt == NULL) 10279 return; 10280 10281 spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private; 10282 10283 sata_common_free_dma_rsrcs(spx); 10284 } 10285 10286 /* 10287 * Fetch Device Identify data. 10288 * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type) 10289 * command to a device and get the device identify data. 10290 * The device_info structure has to be set to device type (for selecting proper 10291 * device identify command). 10292 * 10293 * Returns: 10294 * SATA_SUCCESS if cmd succeeded 10295 * SATA_RETRY if cmd was rejected and could be retried, 10296 * SATA_FAILURE if cmd failed and should not be retried (port error) 10297 * 10298 * Cannot be called in an interrupt context. 10299 */ 10300 10301 static int 10302 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst, 10303 sata_drive_info_t *sdinfo) 10304 { 10305 struct buf *bp; 10306 sata_pkt_t *spkt; 10307 sata_cmd_t *scmd; 10308 sata_pkt_txlate_t *spx; 10309 int rval; 10310 10311 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 10312 spx->txlt_sata_hba_inst = sata_hba_inst; 10313 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 10314 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 10315 if (spkt == NULL) { 10316 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10317 return (SATA_RETRY); /* may retry later */ 10318 } 10319 /* address is needed now */ 10320 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 10321 10322 /* 10323 * Allocate buffer for Identify Data return data 10324 */ 10325 bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t)); 10326 if (bp == NULL) { 10327 sata_pkt_free(spx); 10328 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10329 SATA_LOG_D((sata_hba_inst, CE_WARN, 10330 "sata_fetch_device_identify_data: " 10331 "cannot allocate buffer for ID")); 10332 return (SATA_RETRY); /* may retry later */ 10333 } 10334 10335 /* Fill sata_pkt */ 10336 sdinfo->satadrv_state = SATA_STATE_PROBING; 10337 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 10338 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 10339 /* Synchronous mode, no callback */ 10340 spkt->satapkt_comp = NULL; 10341 /* Timeout 30s */ 10342 spkt->satapkt_time = sata_default_pkt_time; 10343 10344 scmd = &spkt->satapkt_cmd; 10345 scmd->satacmd_bp = bp; 10346 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 10347 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 10348 10349 /* Build Identify Device cmd in the sata_pkt */ 10350 scmd->satacmd_addr_type = 0; /* N/A */ 10351 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 10352 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 10353 scmd->satacmd_lba_mid_lsb = 0; /* N/A */ 10354 scmd->satacmd_lba_high_lsb = 0; /* N/A */ 10355 scmd->satacmd_features_reg = 0; /* N/A */ 10356 scmd->satacmd_device_reg = 0; /* Always device 0 */ 10357 if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) { 10358 /* Identify Packet Device cmd */ 10359 scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE; 10360 } else { 10361 /* Identify Device cmd - mandatory for all other devices */ 10362 scmd->satacmd_cmd_reg = SATAC_ID_DEVICE; 10363 } 10364 10365 /* Send pkt to SATA HBA driver */ 10366 rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt); 10367 10368 #ifdef SATA_INJECT_FAULTS 10369 sata_inject_pkt_fault(spkt, &rval, sata_fault_type); 10370 #endif 10371 10372 if (rval == SATA_TRAN_ACCEPTED && 10373 spkt->satapkt_reason == SATA_PKT_COMPLETED) { 10374 if (spx->txlt_buf_dma_handle != NULL) { 10375 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 10376 DDI_DMA_SYNC_FORKERNEL); 10377 ASSERT(rval == DDI_SUCCESS); 10378 } 10379 if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config & 10380 SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) { 10381 SATA_LOG_D((sata_hba_inst, CE_WARN, 10382 "SATA disk device at port %d - " 10383 "partial Identify Data", 10384 sdinfo->satadrv_addr.cport)); 10385 rval = SATA_RETRY; /* may retry later */ 10386 goto fail; 10387 } 10388 /* Update sata_drive_info */ 10389 bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id, 10390 sizeof (sata_id_t)); 10391 10392 sdinfo->satadrv_features_support = 0; 10393 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 10394 /* 10395 * Retrieve capacity (disks only) and addressing mode 10396 */ 10397 sdinfo->satadrv_capacity = sata_check_capacity(sdinfo); 10398 } else { 10399 /* 10400 * For ATAPI devices one would have to issue 10401 * Get Capacity cmd for media capacity. Not here. 10402 */ 10403 sdinfo->satadrv_capacity = 0; 10404 /* 10405 * Check what cdb length is supported 10406 */ 10407 if ((sdinfo->satadrv_id.ai_config & 10408 SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B) 10409 sdinfo->satadrv_atapi_cdb_len = 16; 10410 else 10411 sdinfo->satadrv_atapi_cdb_len = 12; 10412 } 10413 /* Setup supported features flags */ 10414 if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) 10415 sdinfo->satadrv_features_support |= SATA_DEV_F_DMA; 10416 10417 /* Check for SATA GEN and NCQ support */ 10418 if (sdinfo->satadrv_id.ai_satacap != 0 && 10419 sdinfo->satadrv_id.ai_satacap != 0xffff) { 10420 /* SATA compliance */ 10421 if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ) 10422 sdinfo->satadrv_features_support |= 10423 SATA_DEV_F_NCQ; 10424 if (sdinfo->satadrv_id.ai_satacap & 10425 (SATA_1_SPEED | SATA_2_SPEED)) { 10426 if (sdinfo->satadrv_id.ai_satacap & 10427 SATA_2_SPEED) 10428 sdinfo->satadrv_features_support |= 10429 SATA_DEV_F_SATA2; 10430 if (sdinfo->satadrv_id.ai_satacap & 10431 SATA_1_SPEED) 10432 sdinfo->satadrv_features_support |= 10433 SATA_DEV_F_SATA1; 10434 } else { 10435 sdinfo->satadrv_features_support |= 10436 SATA_DEV_F_SATA1; 10437 } 10438 } 10439 if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) && 10440 (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD)) 10441 sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ; 10442 10443 sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth; 10444 if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) || 10445 (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) { 10446 ++sdinfo->satadrv_queue_depth; 10447 /* Adjust according to controller capabilities */ 10448 sdinfo->satadrv_max_queue_depth = MIN( 10449 sdinfo->satadrv_queue_depth, 10450 SATA_QDEPTH(sata_hba_inst)); 10451 /* Adjust according to global queue depth limit */ 10452 sdinfo->satadrv_max_queue_depth = MIN( 10453 sdinfo->satadrv_max_queue_depth, 10454 sata_current_max_qdepth); 10455 if (sdinfo->satadrv_max_queue_depth == 0) 10456 sdinfo->satadrv_max_queue_depth = 1; 10457 } else 10458 sdinfo->satadrv_max_queue_depth = 1; 10459 10460 rval = SATA_SUCCESS; 10461 } else { 10462 /* 10463 * Woops, no Identify Data. 10464 */ 10465 if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) { 10466 rval = SATA_RETRY; /* may retry later */ 10467 } else if (rval == SATA_TRAN_ACCEPTED) { 10468 if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR || 10469 spkt->satapkt_reason == SATA_PKT_ABORTED || 10470 spkt->satapkt_reason == SATA_PKT_TIMEOUT || 10471 spkt->satapkt_reason == SATA_PKT_RESET) 10472 rval = SATA_RETRY; /* may retry later */ 10473 else 10474 rval = SATA_FAILURE; 10475 } else { 10476 rval = SATA_FAILURE; 10477 } 10478 } 10479 fail: 10480 /* Free allocated resources */ 10481 sata_free_local_buffer(spx); 10482 sata_pkt_free(spx); 10483 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10484 10485 return (rval); 10486 } 10487 10488 10489 /* 10490 * Some devices may not come-up with default DMA mode (UDMA or MWDMA). 10491 * UDMA mode is checked first, followed by MWDMA mode. 10492 * set correctly, so this function is setting it to the highest supported level. 10493 * Older SATA spec required that the device supports at least DMA 4 mode and 10494 * UDMA mode is selected. It is not mentioned in SerialATA 2.6, so this 10495 * restriction has been removed. 10496 * 10497 * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported. 10498 * Returns SATA_FAILURE if proper DMA mode could not be selected. 10499 * 10500 * NOTE: This function should be called only if DMA mode is supported. 10501 */ 10502 static int 10503 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo) 10504 { 10505 sata_pkt_t *spkt; 10506 sata_cmd_t *scmd; 10507 sata_pkt_txlate_t *spx; 10508 int i, mode; 10509 uint8_t subcmd; 10510 int rval = SATA_SUCCESS; 10511 10512 ASSERT(sdinfo != NULL); 10513 ASSERT(sata_hba_inst != NULL); 10514 10515 if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 && 10516 (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) { 10517 /* Find highest Ultra DMA mode supported */ 10518 for (mode = 6; mode >= 0; --mode) { 10519 if (sdinfo->satadrv_id.ai_ultradma & (1 << mode)) 10520 break; 10521 } 10522 #if 0 10523 /* Left for historical reasons */ 10524 /* 10525 * Some initial version of SATA spec indicated that at least 10526 * UDMA mode 4 has to be supported. It is not mentioned in 10527 * SerialATA 2.6, so this restriction is removed. 10528 */ 10529 if (mode < 4) 10530 return (SATA_FAILURE); 10531 #endif 10532 /* Find UDMA mode currently selected */ 10533 for (i = 6; i >= 0; --i) { 10534 if (sdinfo->satadrv_id.ai_ultradma & (1 << (i + 8))) 10535 break; 10536 } 10537 if (i >= mode) 10538 /* Nothing to do */ 10539 return (SATA_SUCCESS); 10540 10541 subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA; 10542 10543 } else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) { 10544 /* Find highest MultiWord DMA mode supported */ 10545 for (mode = 2; mode >= 0; --mode) { 10546 if (sdinfo->satadrv_id.ai_dworddma & (1 << mode)) 10547 break; 10548 } 10549 /* Find highest MultiWord DMA mode selected */ 10550 for (i = 2; i >= 0; --i) { 10551 if (sdinfo->satadrv_id.ai_dworddma & (1 << (i + 8))) 10552 break; 10553 } 10554 if (i >= mode) 10555 /* Nothing to do */ 10556 return (SATA_SUCCESS); 10557 10558 subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA; 10559 } else 10560 return (SATA_SUCCESS); 10561 10562 /* 10563 * Set DMA mode via SET FEATURES COMMAND. 10564 * Prepare packet for SET FEATURES COMMAND. 10565 */ 10566 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 10567 spx->txlt_sata_hba_inst = sata_hba_inst; 10568 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 10569 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 10570 if (spkt == NULL) { 10571 SATA_LOG_D((sata_hba_inst, CE_WARN, 10572 "sata_set_dma_mode: could not set DMA mode %", mode)); 10573 rval = SATA_FAILURE; 10574 goto done; 10575 } 10576 /* Fill sata_pkt */ 10577 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 10578 /* Timeout 30s */ 10579 spkt->satapkt_time = sata_default_pkt_time; 10580 /* Synchronous mode, no callback, interrupts */ 10581 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 10582 spkt->satapkt_comp = NULL; 10583 scmd = &spkt->satapkt_cmd; 10584 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 10585 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 10586 scmd->satacmd_addr_type = 0; 10587 scmd->satacmd_device_reg = 0; 10588 scmd->satacmd_status_reg = 0; 10589 scmd->satacmd_error_reg = 0; 10590 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 10591 scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE; 10592 scmd->satacmd_sec_count_lsb = subcmd | mode; 10593 10594 /* Transfer command to HBA */ 10595 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 10596 spkt) != SATA_TRAN_ACCEPTED || 10597 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 10598 /* Pkt execution failed */ 10599 rval = SATA_FAILURE; 10600 } 10601 done: 10602 10603 /* Free allocated resources */ 10604 if (spkt != NULL) 10605 sata_pkt_free(spx); 10606 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10607 10608 return (rval); 10609 } 10610 10611 10612 /* 10613 * Set device caching mode. 10614 * One of the following operations should be specified: 10615 * SATAC_SF_ENABLE_READ_AHEAD 10616 * SATAC_SF_DISABLE_READ_AHEAD 10617 * SATAC_SF_ENABLE_WRITE_CACHE 10618 * SATAC_SF_DISABLE_WRITE_CACHE 10619 * 10620 * If operation fails, system log messgage is emitted. 10621 * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if 10622 * command was sent but did not succeed, and SATA_FAILURE otherwise. 10623 */ 10624 10625 static int 10626 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 10627 int cache_op) 10628 { 10629 sata_pkt_t *spkt; 10630 sata_cmd_t *scmd; 10631 sata_pkt_txlate_t *spx; 10632 int rval = SATA_SUCCESS; 10633 int hba_rval; 10634 char *infop; 10635 10636 ASSERT(sdinfo != NULL); 10637 ASSERT(sata_hba_inst != NULL); 10638 ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD || 10639 cache_op == SATAC_SF_DISABLE_READ_AHEAD || 10640 cache_op == SATAC_SF_ENABLE_WRITE_CACHE || 10641 cache_op == SATAC_SF_DISABLE_WRITE_CACHE); 10642 10643 10644 /* Prepare packet for SET FEATURES COMMAND */ 10645 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 10646 spx->txlt_sata_hba_inst = sata_hba_inst; 10647 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 10648 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 10649 if (spkt == NULL) { 10650 rval = SATA_FAILURE; 10651 goto failure; 10652 } 10653 /* Fill sata_pkt */ 10654 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 10655 /* Timeout 30s */ 10656 spkt->satapkt_time = sata_default_pkt_time; 10657 /* Synchronous mode, no callback, interrupts */ 10658 spkt->satapkt_op_mode = 10659 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 10660 spkt->satapkt_comp = NULL; 10661 scmd = &spkt->satapkt_cmd; 10662 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 10663 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 10664 scmd->satacmd_addr_type = 0; 10665 scmd->satacmd_device_reg = 0; 10666 scmd->satacmd_status_reg = 0; 10667 scmd->satacmd_error_reg = 0; 10668 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 10669 scmd->satacmd_features_reg = cache_op; 10670 10671 /* Transfer command to HBA */ 10672 hba_rval = (*SATA_START_FUNC(sata_hba_inst))( 10673 SATA_DIP(sata_hba_inst), spkt); 10674 10675 #ifdef SATA_INJECT_FAULTS 10676 sata_inject_pkt_fault(spkt, &rval, sata_fault_type); 10677 #endif 10678 10679 if ((hba_rval != SATA_TRAN_ACCEPTED) || 10680 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 10681 /* Pkt execution failed */ 10682 switch (cache_op) { 10683 case SATAC_SF_ENABLE_READ_AHEAD: 10684 infop = "enabling read ahead failed"; 10685 break; 10686 case SATAC_SF_DISABLE_READ_AHEAD: 10687 infop = "disabling read ahead failed"; 10688 break; 10689 case SATAC_SF_ENABLE_WRITE_CACHE: 10690 infop = "enabling write cache failed"; 10691 break; 10692 case SATAC_SF_DISABLE_WRITE_CACHE: 10693 infop = "disabling write cache failed"; 10694 break; 10695 } 10696 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 10697 rval = SATA_RETRY; 10698 } 10699 failure: 10700 /* Free allocated resources */ 10701 if (spkt != NULL) 10702 sata_pkt_free(spx); 10703 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10704 return (rval); 10705 } 10706 10707 /* 10708 * Set Removable Media Status Notification (enable/disable) 10709 * state == 0 , disable 10710 * state != 0 , enable 10711 * 10712 * If operation fails, system log messgage is emitted. 10713 * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise. 10714 */ 10715 10716 static int 10717 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo, 10718 int state) 10719 { 10720 sata_pkt_t *spkt; 10721 sata_cmd_t *scmd; 10722 sata_pkt_txlate_t *spx; 10723 int rval = SATA_SUCCESS; 10724 char *infop; 10725 10726 ASSERT(sdinfo != NULL); 10727 ASSERT(sata_hba_inst != NULL); 10728 10729 /* Prepare packet for SET FEATURES COMMAND */ 10730 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 10731 spx->txlt_sata_hba_inst = sata_hba_inst; 10732 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 10733 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 10734 if (spkt == NULL) { 10735 rval = SATA_FAILURE; 10736 goto failure; 10737 } 10738 /* Fill sata_pkt */ 10739 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 10740 /* Timeout 30s */ 10741 spkt->satapkt_time = sata_default_pkt_time; 10742 /* Synchronous mode, no callback, interrupts */ 10743 spkt->satapkt_op_mode = 10744 SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 10745 spkt->satapkt_comp = NULL; 10746 scmd = &spkt->satapkt_cmd; 10747 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 10748 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 10749 scmd->satacmd_addr_type = 0; 10750 scmd->satacmd_device_reg = 0; 10751 scmd->satacmd_status_reg = 0; 10752 scmd->satacmd_error_reg = 0; 10753 scmd->satacmd_cmd_reg = SATAC_SET_FEATURES; 10754 if (state == 0) 10755 scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN; 10756 else 10757 scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN; 10758 10759 /* Transfer command to HBA */ 10760 if (((*SATA_START_FUNC(sata_hba_inst))( 10761 SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) || 10762 (spkt->satapkt_reason != SATA_PKT_COMPLETED)) { 10763 /* Pkt execution failed */ 10764 if (state == 0) 10765 infop = "disabling Removable Media Status " 10766 "Notification failed"; 10767 else 10768 infop = "enabling Removable Media Status " 10769 "Notification failed"; 10770 10771 SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop)); 10772 rval = SATA_FAILURE; 10773 } 10774 failure: 10775 /* Free allocated resources */ 10776 if (spkt != NULL) 10777 sata_pkt_free(spx); 10778 (void) kmem_free(spx, sizeof (sata_pkt_txlate_t)); 10779 return (rval); 10780 } 10781 10782 10783 /* 10784 * Update port SCR block 10785 */ 10786 static void 10787 sata_update_port_scr(sata_port_scr_t *port_scr, sata_device_t *device) 10788 { 10789 port_scr->sstatus = device->satadev_scr.sstatus; 10790 port_scr->serror = device->satadev_scr.serror; 10791 port_scr->scontrol = device->satadev_scr.scontrol; 10792 port_scr->sactive = device->satadev_scr.sactive; 10793 port_scr->snotific = device->satadev_scr.snotific; 10794 } 10795 10796 /* 10797 * Update state and copy port ss* values from passed sata_device structure. 10798 * sata_address is validated - if not valid, nothing is changed in sata_scsi 10799 * configuration struct. 10800 * 10801 * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function 10802 * regardless of the state in device argument. 10803 * 10804 * Port mutex should be held while calling this function. 10805 */ 10806 static void 10807 sata_update_port_info(sata_hba_inst_t *sata_hba_inst, 10808 sata_device_t *sata_device) 10809 { 10810 ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, 10811 sata_device->satadev_addr.cport))); 10812 10813 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT || 10814 sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) { 10815 10816 sata_cport_info_t *cportinfo; 10817 10818 if (SATA_NUM_CPORTS(sata_hba_inst) <= 10819 sata_device->satadev_addr.cport) 10820 return; 10821 10822 cportinfo = SATA_CPORT_INFO(sata_hba_inst, 10823 sata_device->satadev_addr.cport); 10824 sata_update_port_scr(&cportinfo->cport_scr, sata_device); 10825 10826 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 10827 cportinfo->cport_state &= ~(SATA_PSTATE_PWRON | 10828 SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED); 10829 cportinfo->cport_state |= 10830 sata_device->satadev_state & SATA_PSTATE_VALID; 10831 } else { 10832 sata_pmport_info_t *pmportinfo; 10833 10834 if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT) || 10835 (sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) || 10836 SATA_NUM_PMPORTS(sata_hba_inst, 10837 sata_device->satadev_addr.cport) < 10838 sata_device->satadev_addr.pmport) 10839 return; 10840 10841 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 10842 sata_device->satadev_addr.cport, 10843 sata_device->satadev_addr.pmport); 10844 sata_update_port_scr(&pmportinfo->pmport_scr, sata_device); 10845 10846 /* Preserve SATA_PSTATE_SHUTDOWN flag */ 10847 pmportinfo->pmport_state &= 10848 ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | 10849 SATA_PSTATE_FAILED); 10850 pmportinfo->pmport_state |= 10851 sata_device->satadev_state & SATA_PSTATE_VALID; 10852 } 10853 } 10854 10855 10856 10857 /* 10858 * Extract SATA port specification from an IOCTL argument. 10859 * 10860 * This function return the port the user land send us as is, unless it 10861 * cannot retrieve port spec, then -1 is returned. 10862 * 10863 * Note: Only cport - no port multiplier port. 10864 */ 10865 static int32_t 10866 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp) 10867 { 10868 int32_t port; 10869 10870 /* Extract port number from nvpair in dca structure */ 10871 if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) { 10872 SATA_LOG_D((sata_hba_inst, CE_NOTE, 10873 "sata_get_port_num: invalid port spec 0x%x in ioctl", 10874 port)); 10875 port = -1; 10876 } 10877 10878 return (port); 10879 } 10880 10881 /* 10882 * Get dev_info_t pointer to the device node pointed to by port argument. 10883 * NOTE: target argument is a value used in ioctls to identify 10884 * the AP - it is not a sata_address. 10885 * It is a combination of cport, pmport and address qualifier, encodded same 10886 * way as a scsi target number. 10887 * At this moment it carries only cport number. 10888 * 10889 * No PMult hotplug support. 10890 * 10891 * Returns dev_info_t pointer if target device was found, NULL otherwise. 10892 */ 10893 10894 static dev_info_t * 10895 sata_get_target_dip(dev_info_t *dip, int32_t port) 10896 { 10897 dev_info_t *cdip = NULL; 10898 int target, tgt; 10899 int ncport; 10900 int circ; 10901 10902 ncport = port & SATA_CFGA_CPORT_MASK; 10903 target = SATA_TO_SCSI_TARGET(ncport, 0, SATA_ADDR_DCPORT); 10904 10905 ndi_devi_enter(dip, &circ); 10906 for (cdip = ddi_get_child(dip); cdip != NULL; ) { 10907 dev_info_t *next = ddi_get_next_sibling(cdip); 10908 10909 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip, 10910 DDI_PROP_DONTPASS, "target", -1); 10911 if (tgt == -1) { 10912 /* 10913 * This is actually an error condition, but not 10914 * a fatal one. Just continue the search. 10915 */ 10916 cdip = next; 10917 continue; 10918 } 10919 10920 if (tgt == target) 10921 break; 10922 10923 cdip = next; 10924 } 10925 ndi_devi_exit(dip, circ); 10926 10927 return (cdip); 10928 } 10929 10930 /* 10931 * Get dev_info_t pointer to the device node pointed to by port argument. 10932 * NOTE: target argument is a value used in ioctls to identify 10933 * the AP - it is not a sata_address. 10934 * It is a combination of cport, pmport and address qualifier, encoded same 10935 * way as a scsi target number. 10936 * At this moment it carries only cport number. 10937 * 10938 * No PMult hotplug support. 10939 * 10940 * Returns dev_info_t pointer if target device was found, NULL otherwise. 10941 */ 10942 10943 static dev_info_t * 10944 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr) 10945 { 10946 dev_info_t *cdip = NULL; 10947 int target, tgt; 10948 int circ; 10949 10950 target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual); 10951 10952 ndi_devi_enter(dip, &circ); 10953 for (cdip = ddi_get_child(dip); cdip != NULL; ) { 10954 dev_info_t *next = ddi_get_next_sibling(cdip); 10955 10956 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip, 10957 DDI_PROP_DONTPASS, "target", -1); 10958 if (tgt == -1) { 10959 /* 10960 * This is actually an error condition, but not 10961 * a fatal one. Just continue the search. 10962 */ 10963 cdip = next; 10964 continue; 10965 } 10966 10967 if (tgt == target) 10968 break; 10969 10970 cdip = next; 10971 } 10972 ndi_devi_exit(dip, circ); 10973 10974 return (cdip); 10975 } 10976 10977 /* 10978 * Process sata port disconnect request. 10979 * Normally, cfgadm sata plugin will try to offline (unconfigure) the device 10980 * before this request. Nevertheless, if a device is still configured, 10981 * we need to attempt to offline and unconfigure device. 10982 * Regardless of the unconfigure operation results the port is marked as 10983 * deactivated and no access to the attached device is possible. 10984 * If the target node remains because unconfigure operation failed, its state 10985 * will be set to DEVICE_REMOVED, preventing it to be used again when a device 10986 * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure 10987 * the device and remove old target node. 10988 * 10989 * This function invokes sata_hba_inst->satahba_tran-> 10990 * sata_tran_hotplug_ops->sata_tran_port_deactivate(). 10991 * If successful, the device structure (if any) attached to the specified port 10992 * is removed and state of the port marked appropriately. 10993 * Failure of the port_deactivate may keep port in the physically active state, 10994 * or may fail the port. 10995 * 10996 * NOTE: Port multiplier code is not completed nor tested. 10997 */ 10998 10999 static int 11000 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst, 11001 sata_device_t *sata_device) 11002 { 11003 sata_drive_info_t *sdinfo = NULL; 11004 sata_cport_info_t *cportinfo = NULL; 11005 sata_pmport_info_t *pmportinfo = NULL; 11006 sata_pmult_info_t *pmultinfo = NULL; 11007 dev_info_t *tdip; 11008 int cport, pmport, qual; 11009 int rval = SATA_SUCCESS; 11010 int rv = 0; 11011 11012 cport = sata_device->satadev_addr.cport; 11013 pmport = sata_device->satadev_addr.pmport; 11014 qual = sata_device->satadev_addr.qual; 11015 11016 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT); 11017 11018 /* 11019 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran-> 11020 * sata_tran_hotplug_ops->sata_tran_port_deactivate(). 11021 * Do the sanity check. 11022 */ 11023 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) { 11024 /* No physical port deactivation supported. */ 11025 return (EINVAL); 11026 } 11027 11028 /* Check the current state of the port */ 11029 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11030 (SATA_DIP(sata_hba_inst), sata_device); 11031 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11032 sata_update_port_info(sata_hba_inst, sata_device); 11033 if (rval != SATA_SUCCESS || 11034 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 11035 /* Device port status is unknown or it is in failed state */ 11036 if (qual == SATA_ADDR_PMPORT) { 11037 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 11038 SATA_PSTATE_FAILED; 11039 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 11040 "sata_hba_ioctl: connect: failed to deactivate " 11041 "SATA port %d", cport); 11042 } else { 11043 SATA_CPORT_STATE(sata_hba_inst, cport) = 11044 SATA_PSTATE_FAILED; 11045 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 11046 "sata_hba_ioctl: connect: failed to deactivate " 11047 "SATA port %d:%d", cport, pmport); 11048 } 11049 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 11050 cport)->cport_mutex); 11051 return (EIO); 11052 } 11053 /* 11054 * Set port's dev_state to not ready - this will disable 11055 * an access to a potentially attached device. 11056 */ 11057 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11058 if (qual == SATA_ADDR_PMPORT) { 11059 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 11060 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 11061 sdinfo = pmportinfo->pmport_sata_drive; 11062 ASSERT(sdinfo != NULL); 11063 } 11064 pmportinfo->pmport_state &= ~SATA_STATE_READY; 11065 } else { 11066 /* Assuming cport */ 11067 11068 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 11069 if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) { 11070 pmultinfo = 11071 cportinfo->cport_devp.cport_sata_pmult; 11072 ASSERT(pmultinfo != NULL); 11073 } else { 11074 sdinfo = cportinfo->cport_devp.cport_sata_drive; 11075 } 11076 } 11077 cportinfo->cport_state &= ~SATA_STATE_READY; 11078 } 11079 if (sdinfo != NULL) { 11080 if ((sdinfo->satadrv_type & (SATA_VALID_DEV_TYPE)) != 0) { 11081 /* 11082 * If a target node exists, try to offline 11083 * a device and remove target node. 11084 */ 11085 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 11086 cport)->cport_mutex); 11087 /* We are addressing attached device, not a port */ 11088 sata_device->satadev_addr.qual = 11089 sdinfo->satadrv_addr.qual; 11090 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 11091 &sata_device->satadev_addr); 11092 if (tdip != NULL && ndi_devi_offline(tdip, 11093 NDI_DEVI_REMOVE) != NDI_SUCCESS) { 11094 /* 11095 * Problem 11096 * The target node remained attached. 11097 * This happens when the device file was open 11098 * or a node was waiting for resources. 11099 * Cannot do anything about it. 11100 */ 11101 if (qual == SATA_ADDR_CPORT) { 11102 SATA_LOG_D((sata_hba_inst, CE_WARN, 11103 "sata_hba_ioctl: disconnect: could " 11104 "not unconfigure device before " 11105 "disconnecting the SATA port %d", 11106 cport)); 11107 } else { 11108 SATA_LOG_D((sata_hba_inst, CE_WARN, 11109 "sata_hba_ioctl: disconnect: could " 11110 "not unconfigure device before " 11111 "disconnecting the SATA port %d:%d", 11112 cport, pmport)); 11113 } 11114 /* 11115 * Set DEVICE REMOVED state in the target 11116 * node. It will prevent access to the device 11117 * even when a new device is attached, until 11118 * the old target node is released, removed and 11119 * recreated for a new device. 11120 */ 11121 sata_set_device_removed(tdip); 11122 11123 /* 11124 * Instruct event daemon to try the target 11125 * node cleanup later. 11126 */ 11127 sata_set_target_node_cleanup( 11128 sata_hba_inst, &sata_device->satadev_addr); 11129 } 11130 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 11131 cport)->cport_mutex); 11132 } 11133 11134 /* Remove and release sata_drive info structure. */ 11135 if (pmportinfo != NULL) { 11136 SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport) = 11137 NULL; 11138 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 11139 } else { 11140 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 11141 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 11142 } 11143 (void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t)); 11144 } 11145 #if 0 11146 else if (pmultinfo != NULL) { 11147 /* 11148 * Port Multiplier itself needs special handling. 11149 * All device ports need to be processed here! 11150 */ 11151 } 11152 #endif 11153 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11154 /* Just ask HBA driver to deactivate port */ 11155 /* sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; */ 11156 11157 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 11158 (SATA_DIP(sata_hba_inst), sata_device); 11159 11160 /* 11161 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 11162 * without the hint (to force listener to investivate the state). 11163 */ 11164 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 11165 SE_NO_HINT); 11166 11167 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11168 sata_update_port_info(sata_hba_inst, sata_device); 11169 11170 if (rval != SATA_SUCCESS) { 11171 /* 11172 * Port deactivation failure - do not 11173 * change port state unless the state 11174 * returned by HBA indicates a port failure. 11175 * NOTE: device structures were released, so devices now are 11176 * invisible! Port reset is needed to re-enumerate devices. 11177 */ 11178 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 11179 if (pmportinfo != NULL) 11180 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 11181 else 11182 cportinfo->cport_state = SATA_PSTATE_FAILED; 11183 rv = EIO; 11184 } 11185 } else { 11186 /* 11187 * Deactivation succeded. From now on the sata framework 11188 * will not care what is happening to the device, until 11189 * the port is activated again. 11190 */ 11191 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 11192 } 11193 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11194 return (rv); 11195 } 11196 11197 11198 11199 /* 11200 * Process sata port connect request 11201 * The sata cfgadm pluging will invoke this operation only if port was found 11202 * in the disconnect state (failed state is also treated as the disconnected 11203 * state). 11204 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->satahba_tran-> 11205 * sata_tran_hotplug_ops->sata_tran_port_activate(). 11206 * If successful and a device is found attached to the port, 11207 * the initialization sequence is executed to attach a device structure to 11208 * a port structure. The state of the port and a device would be set 11209 * appropriately. 11210 * The device is not set in configured state (system-wise) by this operation. 11211 * 11212 * Note, that activating the port may generate link events, 11213 * so it is important that following processing and the 11214 * event processing does not interfere with each other! 11215 * 11216 * This operation may remove port failed state and will 11217 * try to make port active and in good standing. 11218 * 11219 * NOTE: Port multiplier code is not completed nor tested. 11220 */ 11221 11222 static int 11223 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst, 11224 sata_device_t *sata_device) 11225 { 11226 int cport, pmport, qual; 11227 int rv = 0; 11228 11229 cport = sata_device->satadev_addr.cport; 11230 pmport = sata_device->satadev_addr.pmport; 11231 qual = sata_device->satadev_addr.qual; 11232 11233 ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT); 11234 11235 /* 11236 * DEVCTL_AP_CONNECT would invoke sata_hba_inst-> 11237 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate(). 11238 * Perform sanity check now. 11239 */ 11240 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) { 11241 /* No physical port activation supported. */ 11242 return (EINVAL); 11243 } 11244 11245 /* Just ask HBA driver to activate port */ 11246 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 11247 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 11248 /* 11249 * Port activation failure. 11250 */ 11251 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 11252 cport)->cport_mutex); 11253 sata_update_port_info(sata_hba_inst, sata_device); 11254 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 11255 if (qual == SATA_ADDR_DCPORT) { 11256 SATA_CPORT_STATE(sata_hba_inst, cport) = 11257 SATA_PSTATE_FAILED; 11258 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 11259 "sata_hba_ioctl: connect: failed to " 11260 "activate SATA port %d", cport); 11261 } else { /* port multiplier device port */ 11262 SATA_PMPORT_STATE(sata_hba_inst, cport, 11263 pmport) = SATA_PSTATE_FAILED; 11264 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 11265 "sata_hba_ioctl: connect: failed to " 11266 "activate SATA port %d:%d", cport, pmport); 11267 11268 } 11269 } 11270 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 11271 cport)->cport_mutex); 11272 SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst, 11273 "sata_hba_ioctl: connect: failed to activate SATA " 11274 "port %d:%d", cport, pmport); 11275 return (EIO); 11276 } 11277 11278 /* Virgin port state - will be updated by the port re-probe. */ 11279 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11280 if (qual == SATA_ADDR_CPORT) 11281 SATA_CPORT_STATE(sata_hba_inst, cport) = 0; 11282 else /* port multiplier device port */ 11283 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0; 11284 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11285 11286 /* 11287 * Probe the port to find its state and attached device. 11288 */ 11289 if (sata_reprobe_port(sata_hba_inst, sata_device, 11290 SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE) 11291 rv = EIO; 11292 11293 /* 11294 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 11295 * without the hint 11296 */ 11297 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 11298 SE_NO_HINT); 11299 11300 /* 11301 * If there is a device attached to the port, emit 11302 * a message. 11303 */ 11304 if (sata_device->satadev_type != SATA_DTYPE_NONE) { 11305 11306 if (qual == SATA_ADDR_CPORT) { 11307 sata_log(sata_hba_inst, CE_WARN, 11308 "SATA device detected at port %d", cport); 11309 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 11310 /* 11311 * A device was not successfully identified 11312 */ 11313 sata_log(sata_hba_inst, CE_WARN, 11314 "Could not identify SATA " 11315 "device at port %d", cport); 11316 } 11317 } else { /* port multiplier device port */ 11318 sata_log(sata_hba_inst, CE_WARN, 11319 "SATA device detected at port %d:%d", 11320 cport, pmport); 11321 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 11322 /* 11323 * A device was not successfully identified 11324 */ 11325 sata_log(sata_hba_inst, CE_WARN, 11326 "Could not identify SATA " 11327 "device at port %d:%d", cport, pmport); 11328 } 11329 } 11330 } 11331 11332 return (rv); 11333 } 11334 11335 11336 /* 11337 * Process sata device unconfigure request. 11338 * The unconfigure operation uses generic nexus operation to 11339 * offline a device. It leaves a target device node attached. 11340 * and obviously sata_drive_info attached as well, because 11341 * from the hardware point of view nothing has changed. 11342 */ 11343 static int 11344 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst, 11345 sata_device_t *sata_device) 11346 { 11347 int rv = 0; 11348 dev_info_t *tdip; 11349 11350 /* We are addressing attached device, not a port */ 11351 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 11352 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 11353 else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) 11354 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 11355 11356 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 11357 &sata_device->satadev_addr)) != NULL) { 11358 11359 if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) { 11360 SATA_LOG_D((sata_hba_inst, CE_WARN, 11361 "sata_hba_ioctl: unconfigure: " 11362 "failed to unconfigure device at SATA port %d:%d", 11363 sata_device->satadev_addr.cport, 11364 sata_device->satadev_addr.pmport)); 11365 rv = EIO; 11366 } 11367 /* 11368 * The target node devi_state should be marked with 11369 * DEVI_DEVICE_OFFLINE by ndi_devi_offline(). 11370 * This would be the indication for cfgadm that 11371 * the AP node occupant state is 'unconfigured'. 11372 */ 11373 11374 } else { 11375 /* 11376 * This would indicate a failure on the part of cfgadm 11377 * to detect correct state of the node prior to this 11378 * call - one cannot unconfigure non-existing device. 11379 */ 11380 SATA_LOG_D((sata_hba_inst, CE_WARN, 11381 "sata_hba_ioctl: unconfigure: " 11382 "attempt to unconfigure non-existing device " 11383 "at SATA port %d:%d", 11384 sata_device->satadev_addr.cport, 11385 sata_device->satadev_addr.pmport)); 11386 rv = ENXIO; 11387 } 11388 return (rv); 11389 } 11390 11391 /* 11392 * Process sata device configure request 11393 * If port is in a failed state, operation is aborted - one has to use 11394 * an explicit connect or port activate request to try to get a port into 11395 * non-failed mode. Port reset wil also work in such situation. 11396 * If the port is in disconnected (shutdown) state, the connect operation is 11397 * attempted prior to any other action. 11398 * When port is in the active state, there is a device attached and the target 11399 * node exists, a device was most likely offlined. 11400 * If target node does not exist, a new target node is created. In both cases 11401 * an attempt is made to online (configure) the device. 11402 * 11403 * NOTE: Port multiplier code is not completed nor tested. 11404 */ 11405 static int 11406 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst, 11407 sata_device_t *sata_device) 11408 { 11409 int cport, pmport, qual; 11410 int rval; 11411 boolean_t target = TRUE; 11412 sata_cport_info_t *cportinfo; 11413 sata_pmport_info_t *pmportinfo = NULL; 11414 dev_info_t *tdip; 11415 sata_drive_info_t *sdinfo; 11416 11417 cport = sata_device->satadev_addr.cport; 11418 pmport = sata_device->satadev_addr.pmport; 11419 qual = sata_device->satadev_addr.qual; 11420 11421 /* Get current port state */ 11422 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 11423 (SATA_DIP(sata_hba_inst), sata_device); 11424 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11425 sata_update_port_info(sata_hba_inst, sata_device); 11426 11427 if (rval != SATA_SUCCESS || 11428 (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) { 11429 /* 11430 * Obviously, device on a failed port is not visible 11431 */ 11432 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11433 return (ENXIO); 11434 } 11435 11436 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11437 if (qual == SATA_ADDR_PMPORT) 11438 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 11439 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11440 11441 if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) { 11442 /* need to activate port */ 11443 target = FALSE; 11444 11445 /* Sanity check */ 11446 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) 11447 return (ENXIO); 11448 11449 /* Just let HBA driver to activate port */ 11450 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 11451 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 11452 /* 11453 * Port activation failure - do not change port state 11454 * unless the state returned by HBA indicates a port 11455 * failure. 11456 */ 11457 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 11458 cport)->cport_mutex); 11459 sata_update_port_info(sata_hba_inst, sata_device); 11460 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 11461 if (qual == SATA_ADDR_PMPORT) 11462 pmportinfo->pmport_state = 11463 SATA_PSTATE_FAILED; 11464 else 11465 cportinfo->cport_state = 11466 SATA_PSTATE_FAILED; 11467 } 11468 mutex_exit(&SATA_CPORT_INFO( 11469 sata_hba_inst, cport)->cport_mutex); 11470 SATA_LOG_D((sata_hba_inst, CE_WARN, 11471 "sata_hba_ioctl: configure: " 11472 "failed to activate SATA port %d:%d", 11473 cport, pmport)); 11474 return (EIO); 11475 } 11476 /* 11477 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 11478 * without the hint. 11479 */ 11480 sata_gen_sysevent(sata_hba_inst, 11481 &sata_device->satadev_addr, SE_NO_HINT); 11482 11483 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 11484 cport_mutex); 11485 /* Virgin port state */ 11486 if (qual == SATA_ADDR_PMPORT) 11487 pmportinfo->pmport_state = 0; 11488 else 11489 cportinfo->cport_state = 0; 11490 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11491 } 11492 /* 11493 * Always reprobe port, to get current device info. 11494 */ 11495 if (sata_reprobe_port(sata_hba_inst, sata_device, 11496 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 11497 return (EIO); 11498 11499 if (sata_device->satadev_type != SATA_DTYPE_NONE && target == FALSE) { 11500 if (qual == SATA_ADDR_PMPORT) { 11501 /* 11502 * That's the transition from "inactive" port 11503 * to active one with device attached. 11504 */ 11505 sata_log(sata_hba_inst, CE_WARN, 11506 "SATA device detected at port %d:%d", 11507 cport, pmport); 11508 } else { 11509 /* 11510 * When PM is attached to the cport and cport is 11511 * activated, every PM device port needs to be reprobed. 11512 * We need to emit message for all devices detected 11513 * at port multiplier's device ports. 11514 * Add such code here. 11515 * For now, just inform about device attached to 11516 * cport. 11517 */ 11518 sata_log(sata_hba_inst, CE_WARN, 11519 "SATA device detected at port %d", cport); 11520 } 11521 } 11522 11523 /* 11524 * This is where real configuration operation starts. 11525 * 11526 * When PM is attached to the cport and cport is activated, 11527 * devices attached PM device ports may have to be configured 11528 * explicitly. This may change when port multiplier is supported. 11529 * For now, configure only disks and other valid target devices. 11530 */ 11531 if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) { 11532 if (qual == SATA_ADDR_CPORT) { 11533 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 11534 /* 11535 * A device was not successfully identified 11536 */ 11537 sata_log(sata_hba_inst, CE_WARN, 11538 "Could not identify SATA " 11539 "device at port %d", cport); 11540 } 11541 } else { /* port multiplier device port */ 11542 if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) { 11543 /* 11544 * A device was not successfully identified 11545 */ 11546 sata_log(sata_hba_inst, CE_WARN, 11547 "Could not identify SATA " 11548 "device at port %d:%d", cport, pmport); 11549 } 11550 } 11551 return (ENXIO); /* No device to configure */ 11552 } 11553 11554 /* 11555 * Here we may have a device in reset condition, 11556 * but because we are just configuring it, there is 11557 * no need to process the reset other than just 11558 * to clear device reset condition in the HBA driver. 11559 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will 11560 * cause a first command sent the HBA driver with the request 11561 * to clear device reset condition. 11562 */ 11563 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11564 if (qual == SATA_ADDR_PMPORT) 11565 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 11566 else 11567 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 11568 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 11569 if (sdinfo == NULL) { 11570 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11571 return (ENXIO); 11572 } 11573 if (sdinfo->satadrv_event_flags & 11574 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 11575 sdinfo->satadrv_event_flags = 0; 11576 } 11577 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 11578 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11579 11580 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 11581 &sata_device->satadev_addr)) != NULL) { 11582 /* 11583 * Target node exists. Verify, that it belongs 11584 * to existing, attached device and not to 11585 * a removed device. 11586 */ 11587 if (sata_check_device_removed(tdip) == B_TRUE) { 11588 if (qual == SATA_ADDR_DPMPORT) 11589 sata_log(sata_hba_inst, CE_WARN, 11590 "SATA device at port %d cannot be " 11591 "configured. " 11592 "Application(s) accessing " 11593 "previously attached device " 11594 "have to release it before newly " 11595 "inserted device can be made accessible.", 11596 cport); 11597 else 11598 sata_log(sata_hba_inst, CE_WARN, 11599 "SATA device at port %d:%d cannot be" 11600 "configured. " 11601 "Application(s) accessing " 11602 "previously attached device " 11603 "have to release it before newly " 11604 "inserted device can be made accessible.", 11605 cport, pmport); 11606 return (EIO); 11607 } 11608 /* 11609 * Device was not removed and re-inserted. 11610 * Try to online it. 11611 */ 11612 if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) { 11613 SATA_LOG_D((sata_hba_inst, CE_WARN, 11614 "sata_hba_ioctl: configure: " 11615 "onlining device at SATA port " 11616 "%d:%d failed", cport, pmport)); 11617 return (EIO); 11618 } 11619 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 11620 cport)->cport_mutex); 11621 11622 if (qual == SATA_ADDR_DPMPORT) 11623 pmportinfo->pmport_tgtnode_clean = B_TRUE; 11624 else 11625 cportinfo-> cport_tgtnode_clean = B_TRUE; 11626 11627 mutex_exit(&SATA_CPORT_INFO( 11628 sata_hba_inst, cport)->cport_mutex); 11629 } else { 11630 /* 11631 * No target node - need to create a new target node. 11632 */ 11633 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 11634 cport_mutex); 11635 if (qual == SATA_ADDR_DPMPORT) 11636 pmportinfo->pmport_tgtnode_clean = B_TRUE; 11637 else 11638 cportinfo-> cport_tgtnode_clean = B_TRUE; 11639 11640 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 11641 cport_mutex); 11642 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst), 11643 sata_hba_inst, &sata_device->satadev_addr); 11644 if (tdip == NULL) { 11645 /* Configure operation failed */ 11646 SATA_LOG_D((sata_hba_inst, CE_WARN, 11647 "sata_hba_ioctl: configure: " 11648 "configuring SATA device at port %d:%d " 11649 "failed", cport, pmport)); 11650 return (EIO); 11651 } 11652 } 11653 return (0); 11654 } 11655 11656 11657 /* 11658 * Process ioctl deactivate port request. 11659 * Arbitrarily unconfigure attached device, if any. 11660 * Even if the unconfigure fails, proceed with the 11661 * port deactivation. 11662 * 11663 * NOTE: Port Multiplier code is not completed and tested. 11664 */ 11665 11666 static int 11667 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst, 11668 sata_device_t *sata_device) 11669 { 11670 int cport, pmport, qual; 11671 int rval, rv = 0; 11672 sata_cport_info_t *cportinfo; 11673 sata_pmport_info_t *pmportinfo = NULL; 11674 dev_info_t *tdip; 11675 sata_drive_info_t *sdinfo = NULL; 11676 11677 /* Sanity check */ 11678 if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) 11679 return (ENOTSUP); 11680 11681 cport = sata_device->satadev_addr.cport; 11682 pmport = sata_device->satadev_addr.pmport; 11683 qual = sata_device->satadev_addr.qual; 11684 11685 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11686 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11687 if (qual == SATA_ADDR_CPORT) { 11688 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 11689 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 11690 /* 11691 * For now, assume that port multiplier is not 11692 * supported, i.e. deal only with valid devices 11693 */ 11694 if ((cportinfo->cport_dev_type & 11695 SATA_VALID_DEV_TYPE) != 0) 11696 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 11697 /* 11698 * If attached device is a port multiplier, we will 11699 * have to unconfigure all devices attached to the 11700 * port multiplier. Add this code here. 11701 */ 11702 } 11703 cportinfo->cport_state &= ~SATA_STATE_READY; 11704 } else { 11705 /* Port multiplier device port */ 11706 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 11707 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 11708 if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE && 11709 (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0) 11710 sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo); 11711 pmportinfo->pmport_state &= ~SATA_STATE_READY; 11712 } 11713 11714 if (sdinfo != NULL) { 11715 /* 11716 * If a target node exists, try to offline a device and 11717 * to remove a target node. 11718 */ 11719 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 11720 cport_mutex); 11721 tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 11722 &sata_device->satadev_addr); 11723 if (tdip != NULL) { 11724 /* target node exist */ 11725 SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst, 11726 "sata_hba_ioctl: port deactivate: " 11727 "target node exists.", NULL); 11728 11729 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != 11730 NDI_SUCCESS) { 11731 SATA_LOG_D((sata_hba_inst, CE_WARN, 11732 "sata_hba_ioctl: port deactivate: " 11733 "failed to unconfigure device at port " 11734 "%d:%d before deactivating the port", 11735 cport, pmport)); 11736 /* 11737 * Set DEVICE REMOVED state in the target 11738 * node. It will prevent an access to 11739 * the device even when a new device is 11740 * attached, until the old target node is 11741 * released, removed and recreated for a new 11742 * device. 11743 */ 11744 sata_set_device_removed(tdip); 11745 11746 /* 11747 * Instruct the event daemon to try the 11748 * target node cleanup later. 11749 */ 11750 sata_set_target_node_cleanup(sata_hba_inst, 11751 &sata_device->satadev_addr); 11752 } 11753 } 11754 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 11755 cport_mutex); 11756 /* 11757 * In any case, remove and release sata_drive_info 11758 * structure. 11759 */ 11760 if (qual == SATA_ADDR_CPORT) { 11761 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 11762 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 11763 } else { /* port multiplier device port */ 11764 SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL; 11765 pmportinfo->pmport_dev_type = SATA_DTYPE_NONE; 11766 } 11767 (void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t)); 11768 } 11769 if (qual == SATA_ADDR_CPORT) { 11770 cportinfo->cport_state &= ~(SATA_STATE_PROBED | 11771 SATA_STATE_PROBING); 11772 } else { /* port multiplier device port */ 11773 pmportinfo->pmport_state &= ~(SATA_STATE_PROBED | 11774 SATA_STATE_PROBING); 11775 } 11776 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11777 11778 /* Just let HBA driver to deactivate port */ 11779 sata_device->satadev_addr.qual = qual; 11780 rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst)) 11781 (SATA_DIP(sata_hba_inst), sata_device); 11782 11783 /* 11784 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 11785 * without the hint 11786 */ 11787 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 11788 SE_NO_HINT); 11789 11790 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11791 sata_update_port_info(sata_hba_inst, sata_device); 11792 if (qual == SATA_ADDR_CPORT) { 11793 if (rval != SATA_SUCCESS) { 11794 /* 11795 * Port deactivation failure - do not change port state 11796 * unless the state returned by HBA indicates a port 11797 * failure. 11798 */ 11799 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 11800 SATA_CPORT_STATE(sata_hba_inst, cport) = 11801 SATA_PSTATE_FAILED; 11802 } 11803 SATA_LOG_D((sata_hba_inst, CE_WARN, 11804 "sata_hba_ioctl: port deactivate: " 11805 "cannot deactivate SATA port %d", cport)); 11806 rv = EIO; 11807 } else { 11808 cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN; 11809 } 11810 } else { 11811 if (rval != SATA_SUCCESS) { 11812 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 11813 SATA_PMPORT_STATE(sata_hba_inst, cport, 11814 pmport) = SATA_PSTATE_FAILED; 11815 } 11816 SATA_LOG_D((sata_hba_inst, CE_WARN, 11817 "sata_hba_ioctl: port deactivate: " 11818 "cannot deactivate SATA port %d:%d", 11819 cport, pmport)); 11820 rv = EIO; 11821 } else { 11822 pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN; 11823 } 11824 } 11825 11826 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11827 11828 return (rv); 11829 } 11830 11831 /* 11832 * Process ioctl port activate request. 11833 * 11834 * NOTE: Port multiplier code is not completed nor tested. 11835 */ 11836 static int 11837 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst, 11838 sata_device_t *sata_device) 11839 { 11840 int cport, pmport, qual; 11841 sata_cport_info_t *cportinfo; 11842 sata_pmport_info_t *pmportinfo = NULL; 11843 boolean_t dev_existed = TRUE; 11844 11845 /* Sanity check */ 11846 if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) 11847 return (ENOTSUP); 11848 11849 cport = sata_device->satadev_addr.cport; 11850 pmport = sata_device->satadev_addr.pmport; 11851 qual = sata_device->satadev_addr.qual; 11852 11853 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11854 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 11855 if (qual == SATA_ADDR_PMPORT) { 11856 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport); 11857 if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN || 11858 pmportinfo->pmport_dev_type == SATA_DTYPE_NONE) 11859 dev_existed = FALSE; 11860 } else { /* cport */ 11861 if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN || 11862 cportinfo->cport_dev_type == SATA_DTYPE_NONE) 11863 dev_existed = FALSE; 11864 } 11865 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11866 11867 /* Just let HBA driver to activate port, if necessary */ 11868 if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst)) 11869 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 11870 /* 11871 * Port activation failure - do not change port state unless 11872 * the state returned by HBA indicates a port failure. 11873 */ 11874 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 11875 cport)->cport_mutex); 11876 sata_update_port_info(sata_hba_inst, sata_device); 11877 if (sata_device->satadev_state & SATA_PSTATE_FAILED) { 11878 if (qual == SATA_ADDR_PMPORT) 11879 pmportinfo->pmport_state = SATA_PSTATE_FAILED; 11880 else 11881 cportinfo->cport_state = SATA_PSTATE_FAILED; 11882 11883 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 11884 cport)->cport_mutex); 11885 SATA_LOG_D((sata_hba_inst, CE_WARN, 11886 "sata_hba_ioctl: port activate: cannot activate " 11887 "SATA port %d:%d", cport, pmport)); 11888 return (EIO); 11889 } 11890 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11891 } 11892 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11893 if (qual == SATA_ADDR_PMPORT) 11894 pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN; 11895 else 11896 cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN; 11897 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 11898 11899 /* 11900 * Re-probe port to find its current state and possibly attached device. 11901 * Port re-probing may change the cportinfo device type if device is 11902 * found attached. 11903 * If port probing failed, the device type would be set to 11904 * SATA_DTYPE_NONE. 11905 */ 11906 (void) sata_reprobe_port(sata_hba_inst, sata_device, 11907 SATA_DEV_IDENTIFY_RETRY); 11908 11909 /* 11910 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 11911 * without the hint. 11912 */ 11913 sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr, 11914 SE_NO_HINT); 11915 11916 if (dev_existed == FALSE) { 11917 if (qual == SATA_ADDR_PMPORT && 11918 pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) { 11919 /* 11920 * That's the transition from the "inactive" port state 11921 * or the active port without a device attached to the 11922 * active port state with a device attached. 11923 */ 11924 sata_log(sata_hba_inst, CE_WARN, 11925 "SATA device detected at port %d:%d", 11926 cport, pmport); 11927 } else if (qual == SATA_ADDR_CPORT && 11928 cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 11929 /* 11930 * That's the transition from the "inactive" port state 11931 * or the active port without a device attached to the 11932 * active port state with a device attached. 11933 */ 11934 if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) { 11935 sata_log(sata_hba_inst, CE_WARN, 11936 "SATA device detected at port %d", cport); 11937 } else { 11938 sata_log(sata_hba_inst, CE_WARN, 11939 "SATA port multiplier detected at port %d", 11940 cport); 11941 /* 11942 * Because the detected device is a port 11943 * multiplier, we need to reprobe every device 11944 * port on the port multiplier and show every 11945 * device found attached. 11946 * Add this code here. 11947 */ 11948 } 11949 } 11950 } 11951 return (0); 11952 } 11953 11954 11955 11956 /* 11957 * Process ioctl reset port request. 11958 * 11959 * NOTE: Port multiplier code is not completed nor tested. 11960 */ 11961 static int 11962 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst, 11963 sata_device_t *sata_device) 11964 { 11965 int cport, pmport, qual; 11966 int rv = 0; 11967 11968 cport = sata_device->satadev_addr.cport; 11969 pmport = sata_device->satadev_addr.pmport; 11970 qual = sata_device->satadev_addr.qual; 11971 11972 /* Sanity check */ 11973 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 11974 SATA_LOG_D((sata_hba_inst, CE_WARN, 11975 "sata_hba_ioctl: sata_hba_tran missing required " 11976 "function sata_tran_reset_dport")); 11977 return (ENOTSUP); 11978 } 11979 11980 /* Ask HBA to reset port */ 11981 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), 11982 sata_device) != SATA_SUCCESS) { 11983 SATA_LOG_D((sata_hba_inst, CE_WARN, 11984 "sata_hba_ioctl: reset port: failed %d:%d", 11985 cport, pmport)); 11986 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 11987 cport_mutex); 11988 sata_update_port_info(sata_hba_inst, sata_device); 11989 if (qual == SATA_ADDR_CPORT) 11990 SATA_CPORT_STATE(sata_hba_inst, cport) = 11991 SATA_PSTATE_FAILED; 11992 else 11993 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 11994 SATA_PSTATE_FAILED; 11995 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 11996 cport_mutex); 11997 rv = EIO; 11998 } 11999 /* 12000 * Beacuse the port was reset, it should be probed and 12001 * attached device reinitialized. At this point the 12002 * port state is unknown - it's state is HBA-specific. 12003 * Re-probe port to get its state. 12004 */ 12005 if (sata_reprobe_port(sata_hba_inst, sata_device, 12006 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) { 12007 rv = EIO; 12008 } 12009 return (rv); 12010 } 12011 12012 /* 12013 * Process ioctl reset device request. 12014 * 12015 * NOTE: Port multiplier code is not completed nor tested. 12016 */ 12017 static int 12018 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst, 12019 sata_device_t *sata_device) 12020 { 12021 sata_drive_info_t *sdinfo; 12022 int cport, pmport; 12023 int rv = 0; 12024 12025 /* Sanity check */ 12026 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 12027 SATA_LOG_D((sata_hba_inst, CE_WARN, 12028 "sata_hba_ioctl: sata_hba_tran missing required " 12029 "function sata_tran_reset_dport")); 12030 return (ENOTSUP); 12031 } 12032 12033 cport = sata_device->satadev_addr.cport; 12034 pmport = sata_device->satadev_addr.pmport; 12035 12036 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 12037 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) { 12038 sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; 12039 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 12040 sata_device->satadev_addr.cport); 12041 } else { /* port multiplier */ 12042 sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT; 12043 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 12044 sata_device->satadev_addr.cport, 12045 sata_device->satadev_addr.pmport); 12046 } 12047 if (sdinfo == NULL) { 12048 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 12049 return (EINVAL); 12050 } 12051 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 12052 12053 /* Ask HBA to reset device */ 12054 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 12055 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 12056 SATA_LOG_D((sata_hba_inst, CE_WARN, 12057 "sata_hba_ioctl: reset device: failed at port %d:%d", 12058 cport, pmport)); 12059 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 12060 cport_mutex); 12061 sata_update_port_info(sata_hba_inst, sata_device); 12062 /* 12063 * Device info structure remains attached. Another device reset 12064 * or port disconnect/connect and re-probing is 12065 * needed to change it's state 12066 */ 12067 sdinfo->satadrv_state &= ~SATA_STATE_READY; 12068 sdinfo->satadrv_state |= SATA_DSTATE_FAILED; 12069 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex); 12070 rv = EIO; 12071 } 12072 /* 12073 * If attached device was a port multiplier, some extra processing 12074 * may be needed, to bring it back (if port re-probing did not handle 12075 * it). Add such code here. 12076 */ 12077 return (rv); 12078 } 12079 12080 12081 /* 12082 * Process ioctl reset all request. 12083 * 12084 * NOTE: Port multiplier code is not completed nor tested. 12085 */ 12086 static int 12087 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst) 12088 { 12089 sata_device_t sata_device; 12090 int rv = 0; 12091 int tcport; 12092 int tpmport = 0; 12093 12094 sata_device.satadev_rev = SATA_DEVICE_REV; 12095 12096 /* 12097 * There is no protection here for configured devices. 12098 */ 12099 /* Sanity check */ 12100 if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) { 12101 SATA_LOG_D((sata_hba_inst, CE_WARN, 12102 "sata_hba_ioctl: sata_hba_tran missing required " 12103 "function sata_tran_reset_dport")); 12104 return (ENOTSUP); 12105 } 12106 12107 /* 12108 * Need to lock all ports, not just one. 12109 * If any port is locked by event processing, fail the whole operation. 12110 * One port is already locked, but for simplicity lock it again. 12111 */ 12112 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) { 12113 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12114 cport_mutex); 12115 if (((SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12116 cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) { 12117 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12118 cport_mutex); 12119 rv = EBUSY; 12120 break; 12121 } else { 12122 SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12123 cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY; 12124 /* 12125 * If there is a port multiplier attached, we may need 12126 * to lock its port as well. If so, add such code here. 12127 */ 12128 } 12129 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12130 cport_mutex); 12131 } 12132 12133 if (rv == 0) { 12134 /* 12135 * All cports were successfully locked. 12136 * Reset main SATA controller only for now - no PMult. 12137 * Set the device address to port 0, to have a valid device 12138 * address. 12139 */ 12140 sata_device.satadev_addr.qual = SATA_ADDR_CNTRL; 12141 sata_device.satadev_addr.cport = 0; 12142 sata_device.satadev_addr.pmport = 0; 12143 12144 if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst)) 12145 (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) { 12146 SATA_LOG_D((sata_hba_inst, CE_WARN, 12147 "sata_hba_ioctl: reset controller failed")); 12148 return (EIO); 12149 } 12150 /* 12151 * Because ports were reset, port states are unknown. 12152 * They should be re-probed to get their state and 12153 * attached devices should be reinitialized. 12154 * Add code here to re-probe port multiplier device ports. 12155 */ 12156 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); 12157 tcport++) { 12158 sata_device.satadev_addr.cport = tcport; 12159 sata_device.satadev_addr.pmport = tpmport; 12160 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 12161 12162 if (sata_reprobe_port(sata_hba_inst, &sata_device, 12163 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 12164 rv = EIO; 12165 } 12166 } 12167 /* 12168 * Unlock all ports 12169 */ 12170 for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) { 12171 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12172 cport_mutex); 12173 SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12174 cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY; 12175 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)-> 12176 cport_mutex); 12177 } 12178 12179 /* 12180 * This operation returns EFAULT if either reset 12181 * controller failed or a re-probing of any port failed. 12182 */ 12183 return (rv); 12184 } 12185 12186 12187 /* 12188 * Process ioctl port self test request. 12189 * 12190 * NOTE: Port multiplier code is not completed nor tested. 12191 */ 12192 static int 12193 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst, 12194 sata_device_t *sata_device) 12195 { 12196 int cport, pmport, qual; 12197 int rv = 0; 12198 12199 /* Sanity check */ 12200 if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL) 12201 return (ENOTSUP); 12202 12203 cport = sata_device->satadev_addr.cport; 12204 pmport = sata_device->satadev_addr.pmport; 12205 qual = sata_device->satadev_addr.qual; 12206 12207 /* 12208 * There is no protection here for a configured 12209 * device attached to this port. 12210 */ 12211 12212 if ((*SATA_SELFTEST_FUNC(sata_hba_inst)) 12213 (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) { 12214 SATA_LOG_D((sata_hba_inst, CE_WARN, 12215 "sata_hba_ioctl: port selftest: " 12216 "failed port %d:%d", cport, pmport)); 12217 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 12218 cport_mutex); 12219 sata_update_port_info(sata_hba_inst, sata_device); 12220 if (qual == SATA_ADDR_CPORT) 12221 SATA_CPORT_STATE(sata_hba_inst, cport) = 12222 SATA_PSTATE_FAILED; 12223 else /* port ultiplier device port */ 12224 SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 12225 SATA_PSTATE_FAILED; 12226 12227 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)-> 12228 cport_mutex); 12229 return (EIO); 12230 } 12231 /* 12232 * Beacuse the port was reset in the course of testing, it should be 12233 * re-probed and attached device state should be restored. At this 12234 * point the port state is unknown - it's state is HBA-specific. 12235 * Force port re-probing to get it into a known state. 12236 */ 12237 if (sata_reprobe_port(sata_hba_inst, sata_device, 12238 SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) 12239 rv = EIO; 12240 return (rv); 12241 } 12242 12243 12244 /* 12245 * sata_cfgadm_state: 12246 * Use the sata port state and state of the target node to figure out 12247 * the cfgadm_state. 12248 * 12249 * The port argument is a value with encoded cport, 12250 * pmport and address qualifier, in the same manner as a scsi target number. 12251 * SCSI_TO_SATA_CPORT macro extracts cport number, 12252 * SCSI_TO_SATA_PMPORT extracts pmport number and 12253 * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag. 12254 * 12255 * For now, support is for cports only - no port multiplier device ports. 12256 */ 12257 12258 static void 12259 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port, 12260 devctl_ap_state_t *ap_state) 12261 { 12262 uint16_t cport; 12263 int port_state; 12264 sata_drive_info_t *sdinfo; 12265 12266 /* Cport only */ 12267 cport = SCSI_TO_SATA_CPORT(port); 12268 12269 port_state = SATA_CPORT_STATE(sata_hba_inst, cport); 12270 if (port_state & SATA_PSTATE_SHUTDOWN || 12271 port_state & SATA_PSTATE_FAILED) { 12272 ap_state->ap_rstate = AP_RSTATE_DISCONNECTED; 12273 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 12274 if (port_state & SATA_PSTATE_FAILED) 12275 ap_state->ap_condition = AP_COND_FAILED; 12276 else 12277 ap_state->ap_condition = AP_COND_UNKNOWN; 12278 12279 return; 12280 } 12281 12282 /* Need to check pmult device port here as well, when supported */ 12283 12284 /* Port is enabled and ready */ 12285 12286 switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) { 12287 case SATA_DTYPE_NONE: 12288 { 12289 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 12290 ap_state->ap_condition = AP_COND_OK; 12291 /* No device attached */ 12292 ap_state->ap_rstate = AP_RSTATE_EMPTY; 12293 break; 12294 } 12295 case SATA_DTYPE_UNKNOWN: 12296 case SATA_DTYPE_ATAPINONCD: 12297 case SATA_DTYPE_PMULT: /* Until PMult is supported */ 12298 case SATA_DTYPE_ATADISK: 12299 case SATA_DTYPE_ATAPICD: 12300 { 12301 dev_info_t *tdip = NULL; 12302 dev_info_t *dip = NULL; 12303 int circ; 12304 12305 dip = SATA_DIP(sata_hba_inst); 12306 tdip = sata_get_target_dip(dip, port); 12307 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 12308 if (tdip != NULL) { 12309 ndi_devi_enter(dip, &circ); 12310 mutex_enter(&(DEVI(tdip)->devi_lock)); 12311 if (DEVI_IS_DEVICE_REMOVED(tdip)) { 12312 /* 12313 * There could be the case where previously 12314 * configured and opened device was removed 12315 * and unknown device was plugged. 12316 * In such case we want to show a device, and 12317 * its configured or unconfigured state but 12318 * indicate unusable condition untill the 12319 * old target node is released and removed. 12320 */ 12321 ap_state->ap_condition = AP_COND_UNUSABLE; 12322 } else { 12323 mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, 12324 cport)); 12325 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 12326 cport); 12327 if (sdinfo != NULL) { 12328 if ((sdinfo->satadrv_state & 12329 SATA_DSTATE_FAILED) != 0) 12330 ap_state->ap_condition = 12331 AP_COND_FAILED; 12332 else 12333 ap_state->ap_condition = 12334 AP_COND_OK; 12335 } else { 12336 ap_state->ap_condition = 12337 AP_COND_UNKNOWN; 12338 } 12339 mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, 12340 cport)); 12341 } 12342 if ((DEVI_IS_DEVICE_OFFLINE(tdip)) || 12343 (DEVI_IS_DEVICE_DOWN(tdip))) { 12344 ap_state->ap_ostate = 12345 AP_OSTATE_UNCONFIGURED; 12346 } else { 12347 ap_state->ap_ostate = 12348 AP_OSTATE_CONFIGURED; 12349 } 12350 mutex_exit(&(DEVI(tdip)->devi_lock)); 12351 ndi_devi_exit(dip, circ); 12352 } else { 12353 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 12354 ap_state->ap_condition = AP_COND_UNKNOWN; 12355 } 12356 break; 12357 } 12358 default: 12359 ap_state->ap_rstate = AP_RSTATE_CONNECTED; 12360 ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED; 12361 ap_state->ap_condition = AP_COND_UNKNOWN; 12362 /* 12363 * This is actually internal error condition (non fatal), 12364 * because we have already checked all defined device types. 12365 */ 12366 SATA_LOG_D((sata_hba_inst, CE_WARN, 12367 "sata_cfgadm_state: Internal error: " 12368 "unknown device type")); 12369 break; 12370 } 12371 } 12372 12373 12374 /* 12375 * Process ioctl get device path request. 12376 * 12377 * NOTE: Port multiplier code is not completed nor tested. 12378 */ 12379 static int 12380 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst, 12381 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 12382 { 12383 char path[MAXPATHLEN]; 12384 uint32_t size; 12385 dev_info_t *tdip; 12386 12387 (void) strcpy(path, "/devices"); 12388 if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst), 12389 &sata_device->satadev_addr)) == NULL) { 12390 /* 12391 * No such device. If this is a request for a size, do not 12392 * return EINVAL for non-existing target, because cfgadm 12393 * will then indicate a meaningless ioctl failure. 12394 * If this is a request for a path, indicate invalid 12395 * argument. 12396 */ 12397 if (ioc->get_size == 0) 12398 return (EINVAL); 12399 } else { 12400 (void) ddi_pathname(tdip, path + strlen(path)); 12401 } 12402 size = strlen(path) + 1; 12403 12404 if (ioc->get_size != 0) { 12405 if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz, 12406 mode) != 0) 12407 return (EFAULT); 12408 } else { 12409 if (ioc->bufsiz != size) 12410 return (EINVAL); 12411 12412 else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz, 12413 mode) != 0) 12414 return (EFAULT); 12415 } 12416 return (0); 12417 } 12418 12419 /* 12420 * Process ioctl get attachment point type request. 12421 * 12422 * NOTE: Port multiplier code is not completed nor tested. 12423 */ 12424 static int 12425 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst, 12426 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 12427 { 12428 uint32_t type_len; 12429 const char *ap_type; 12430 int dev_type; 12431 12432 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 12433 dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, 12434 sata_device->satadev_addr.cport); 12435 else /* pmport */ 12436 dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, 12437 sata_device->satadev_addr.cport, 12438 sata_device->satadev_addr.pmport); 12439 12440 switch (dev_type) { 12441 case SATA_DTYPE_NONE: 12442 ap_type = "port"; 12443 break; 12444 12445 case SATA_DTYPE_ATADISK: 12446 ap_type = "disk"; 12447 break; 12448 12449 case SATA_DTYPE_ATAPICD: 12450 ap_type = "cd/dvd"; 12451 break; 12452 12453 case SATA_DTYPE_PMULT: 12454 ap_type = "pmult"; 12455 break; 12456 12457 case SATA_DTYPE_UNKNOWN: 12458 ap_type = "unknown"; 12459 break; 12460 12461 default: 12462 ap_type = "unsupported"; 12463 break; 12464 12465 } /* end of dev_type switch */ 12466 12467 type_len = strlen(ap_type) + 1; 12468 12469 if (ioc->get_size) { 12470 if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz, 12471 mode) != 0) 12472 return (EFAULT); 12473 } else { 12474 if (ioc->bufsiz != type_len) 12475 return (EINVAL); 12476 12477 if (ddi_copyout((void *)ap_type, ioc->buf, 12478 ioc->bufsiz, mode) != 0) 12479 return (EFAULT); 12480 } 12481 return (0); 12482 12483 } 12484 12485 /* 12486 * Process ioctl get device model info request. 12487 * This operation should return to cfgadm the device model 12488 * information string 12489 * 12490 * NOTE: Port multiplier code is not completed nor tested. 12491 */ 12492 static int 12493 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst, 12494 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 12495 { 12496 sata_drive_info_t *sdinfo; 12497 uint32_t info_len; 12498 char ap_info[SATA_ID_MODEL_LEN + 1]; 12499 12500 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 12501 sata_device->satadev_addr.cport)->cport_mutex); 12502 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 12503 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 12504 sata_device->satadev_addr.cport); 12505 else /* port multiplier */ 12506 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 12507 sata_device->satadev_addr.cport, 12508 sata_device->satadev_addr.pmport); 12509 if (sdinfo == NULL) { 12510 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 12511 sata_device->satadev_addr.cport)->cport_mutex); 12512 return (EINVAL); 12513 } 12514 12515 #ifdef _LITTLE_ENDIAN 12516 swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN); 12517 #else /* _LITTLE_ENDIAN */ 12518 bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN); 12519 #endif /* _LITTLE_ENDIAN */ 12520 12521 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 12522 sata_device->satadev_addr.cport)->cport_mutex); 12523 12524 ap_info[SATA_ID_MODEL_LEN] = '\0'; 12525 12526 info_len = strlen(ap_info) + 1; 12527 12528 if (ioc->get_size) { 12529 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 12530 mode) != 0) 12531 return (EFAULT); 12532 } else { 12533 if (ioc->bufsiz < info_len) 12534 return (EINVAL); 12535 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 12536 mode) != 0) 12537 return (EFAULT); 12538 } 12539 return (0); 12540 } 12541 12542 12543 /* 12544 * Process ioctl get device firmware revision info request. 12545 * This operation should return to cfgadm the device firmware revision 12546 * information string 12547 * 12548 * NOTE: Port multiplier code is not completed nor tested. 12549 */ 12550 static int 12551 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst, 12552 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 12553 { 12554 sata_drive_info_t *sdinfo; 12555 uint32_t info_len; 12556 char ap_info[SATA_ID_FW_LEN + 1]; 12557 12558 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 12559 sata_device->satadev_addr.cport)->cport_mutex); 12560 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 12561 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 12562 sata_device->satadev_addr.cport); 12563 else /* port multiplier */ 12564 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 12565 sata_device->satadev_addr.cport, 12566 sata_device->satadev_addr.pmport); 12567 if (sdinfo == NULL) { 12568 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 12569 sata_device->satadev_addr.cport)->cport_mutex); 12570 return (EINVAL); 12571 } 12572 12573 #ifdef _LITTLE_ENDIAN 12574 swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN); 12575 #else /* _LITTLE_ENDIAN */ 12576 bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN); 12577 #endif /* _LITTLE_ENDIAN */ 12578 12579 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 12580 sata_device->satadev_addr.cport)->cport_mutex); 12581 12582 ap_info[SATA_ID_FW_LEN] = '\0'; 12583 12584 info_len = strlen(ap_info) + 1; 12585 12586 if (ioc->get_size) { 12587 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 12588 mode) != 0) 12589 return (EFAULT); 12590 } else { 12591 if (ioc->bufsiz < info_len) 12592 return (EINVAL); 12593 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 12594 mode) != 0) 12595 return (EFAULT); 12596 } 12597 return (0); 12598 } 12599 12600 12601 /* 12602 * Process ioctl get device serial number info request. 12603 * This operation should return to cfgadm the device serial number string. 12604 * 12605 * NOTE: Port multiplier code is not completed nor tested. 12606 */ 12607 static int 12608 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst, 12609 sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode) 12610 { 12611 sata_drive_info_t *sdinfo; 12612 uint32_t info_len; 12613 char ap_info[SATA_ID_SERIAL_LEN + 1]; 12614 12615 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 12616 sata_device->satadev_addr.cport)->cport_mutex); 12617 if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) 12618 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, 12619 sata_device->satadev_addr.cport); 12620 else /* port multiplier */ 12621 sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, 12622 sata_device->satadev_addr.cport, 12623 sata_device->satadev_addr.pmport); 12624 if (sdinfo == NULL) { 12625 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 12626 sata_device->satadev_addr.cport)->cport_mutex); 12627 return (EINVAL); 12628 } 12629 12630 #ifdef _LITTLE_ENDIAN 12631 swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN); 12632 #else /* _LITTLE_ENDIAN */ 12633 bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN); 12634 #endif /* _LITTLE_ENDIAN */ 12635 12636 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 12637 sata_device->satadev_addr.cport)->cport_mutex); 12638 12639 ap_info[SATA_ID_SERIAL_LEN] = '\0'; 12640 12641 info_len = strlen(ap_info) + 1; 12642 12643 if (ioc->get_size) { 12644 if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz, 12645 mode) != 0) 12646 return (EFAULT); 12647 } else { 12648 if (ioc->bufsiz < info_len) 12649 return (EINVAL); 12650 if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz, 12651 mode) != 0) 12652 return (EFAULT); 12653 } 12654 return (0); 12655 } 12656 12657 12658 /* 12659 * Preset scsi extended sense data (to NO SENSE) 12660 * First 18 bytes of the sense data are preset to current valid sense 12661 * with a key NO SENSE data. 12662 * 12663 * Returns void 12664 */ 12665 static void 12666 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense) 12667 { 12668 sense->es_valid = 1; /* Valid sense */ 12669 sense->es_class = CLASS_EXTENDED_SENSE; /* 0x70 - current err */ 12670 sense->es_key = KEY_NO_SENSE; 12671 sense->es_info_1 = 0; 12672 sense->es_info_2 = 0; 12673 sense->es_info_3 = 0; 12674 sense->es_info_4 = 0; 12675 sense->es_add_len = 10; /* Additional length - replace with a def */ 12676 sense->es_cmd_info[0] = 0; 12677 sense->es_cmd_info[1] = 0; 12678 sense->es_cmd_info[2] = 0; 12679 sense->es_cmd_info[3] = 0; 12680 sense->es_add_code = 0; 12681 sense->es_qual_code = 0; 12682 } 12683 12684 /* 12685 * Register a legacy cmdk-style devid for the target (disk) device. 12686 * 12687 * Note: This function is called only when the HBA devinfo node has the 12688 * property "use-cmdk-devid-format" set. This property indicates that 12689 * devid compatible with old cmdk (target) driver is to be generated 12690 * for any target device attached to this controller. This will take 12691 * precedence over the devid generated by sd (target) driver. 12692 * This function is derived from cmdk_devid_setup() function in cmdk.c. 12693 */ 12694 static void 12695 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo) 12696 { 12697 char *hwid; 12698 int modlen; 12699 int serlen; 12700 int rval; 12701 ddi_devid_t devid; 12702 12703 /* 12704 * device ID is a concatanation of model number, "=", serial number. 12705 */ 12706 hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP); 12707 bcopy(&sdinfo->satadrv_id.ai_model, hwid, 12708 sizeof (sdinfo->satadrv_id.ai_model)); 12709 swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model)); 12710 modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model)); 12711 if (modlen == 0) 12712 goto err; 12713 hwid[modlen++] = '='; 12714 bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen], 12715 sizeof (sdinfo->satadrv_id.ai_drvser)); 12716 swab(&hwid[modlen], &hwid[modlen], 12717 sizeof (sdinfo->satadrv_id.ai_drvser)); 12718 serlen = sata_check_modser(&hwid[modlen], 12719 sizeof (sdinfo->satadrv_id.ai_drvser)); 12720 if (serlen == 0) 12721 goto err; 12722 hwid[modlen + serlen] = 0; /* terminate the hwid string */ 12723 12724 /* initialize/register devid */ 12725 if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL, 12726 (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) 12727 rval = ddi_devid_register(dip, devid); 12728 12729 if (rval != DDI_SUCCESS) 12730 cmn_err(CE_WARN, "sata: failed to create devid for the disk" 12731 " on port %d", sdinfo->satadrv_addr.cport); 12732 err: 12733 kmem_free(hwid, LEGACY_HWID_LEN); 12734 } 12735 12736 /* 12737 * valid model/serial string must contain a non-zero non-space characters. 12738 * trim trailing spaces/NULLs. 12739 */ 12740 static int 12741 sata_check_modser(char *buf, int buf_len) 12742 { 12743 boolean_t ret; 12744 char *s; 12745 int i; 12746 int tb; 12747 char ch; 12748 12749 ret = B_FALSE; 12750 s = buf; 12751 for (i = 0; i < buf_len; i++) { 12752 ch = *s++; 12753 if (ch != ' ' && ch != '\0') 12754 tb = i + 1; 12755 if (ch != ' ' && ch != '\0' && ch != '0') 12756 ret = B_TRUE; 12757 } 12758 12759 if (ret == B_FALSE) 12760 return (0); /* invalid string */ 12761 12762 return (tb); /* return length */ 12763 } 12764 12765 /* 12766 * sata_set_drive_features function compares current device features setting 12767 * with the saved device features settings and, if there is a difference, 12768 * it restores device features setting to the previously saved state. 12769 * It also arbitrarily tries to select the highest supported DMA mode. 12770 * Device Identify or Identify Packet Device data has to be current. 12771 * At the moment read ahead and write cache are considered for all devices. 12772 * For atapi devices, Removable Media Status Notification is set in addition 12773 * to common features. 12774 * 12775 * This function cannot be called in the interrupt context (it may sleep). 12776 * 12777 * The input argument sdinfo should point to the drive info structure 12778 * to be updated after features are set. Note, that only 12779 * device (packet) identify data is updated, not the flags indicating the 12780 * supported features. 12781 * 12782 * Returns SATA_SUCCESS if successful or there was nothing to do. 12783 * Device Identify data in the drive info structure pointed to by the sdinfo 12784 * arguments is updated even when no features were set or changed. 12785 * 12786 * Returns SATA_FAILURE if device features could not be set or DMA mode 12787 * for a disk cannot be set and device identify data cannot be fetched. 12788 * 12789 * Returns SATA_RETRY if device features could not be set (other than disk 12790 * DMA mode) but the device identify data was fetched successfully. 12791 * 12792 * Note: This function may fail the port, making it inaccessible. 12793 * In such case the explicit port disconnect/connect or physical device 12794 * detach/attach is required to re-evaluate port state again. 12795 */ 12796 12797 static int 12798 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst, 12799 sata_drive_info_t *sdinfo, int restore) 12800 { 12801 int rval = SATA_SUCCESS; 12802 int rval_set; 12803 sata_drive_info_t new_sdinfo; 12804 char *finfo = "sata_set_drive_features: cannot"; 12805 char *finfox; 12806 int cache_op; 12807 12808 bzero(&new_sdinfo, sizeof (sata_drive_info_t)); 12809 new_sdinfo.satadrv_addr = sdinfo->satadrv_addr; 12810 new_sdinfo.satadrv_type = sdinfo->satadrv_type; 12811 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 12812 /* 12813 * Cannot get device identification - caller may retry later 12814 */ 12815 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12816 "%s fetch device identify data\n", finfo); 12817 return (SATA_FAILURE); 12818 } 12819 finfox = (restore != 0) ? " restore device features" : 12820 " initialize device features\n"; 12821 12822 if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) { 12823 /* Arbitrarily set UDMA mode */ 12824 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) != 12825 SATA_SUCCESS) { 12826 SATA_LOG_D((sata_hba_inst, CE_WARN, 12827 "%s set UDMA mode\n", finfo)); 12828 return (SATA_FAILURE); 12829 } 12830 } else { /* Assume SATA ATAPI CD/DVD */ 12831 /* Set Removable Media Status Notification, if necessary */ 12832 if ((new_sdinfo.satadrv_id.ai_cmdset83 & 12833 SATA_RM_STATUS_NOTIFIC) != 0 && restore != 0) { 12834 if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) && 12835 (!(new_sdinfo.satadrv_id.ai_features86 & 12836 SATA_RM_STATUS_NOTIFIC))) || 12837 ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) && 12838 (new_sdinfo.satadrv_id.ai_features86 & 12839 SATA_RM_STATUS_NOTIFIC))) { 12840 /* Current setting does not match saved one */ 12841 if (sata_set_rmsn(sata_hba_inst, sdinfo, 12842 sdinfo->satadrv_settings & 12843 SATA_DEV_RMSN) != SATA_SUCCESS) 12844 rval = SATA_FAILURE; 12845 } 12846 } 12847 /* 12848 * We have to set Multiword DMA or UDMA, if it is supported, as 12849 * we want to use DMA transfer mode whenever possible. 12850 * Some devices require explicit setting of the DMA mode. 12851 */ 12852 if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) { 12853 /* Set highest supported DMA mode */ 12854 if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) != 12855 SATA_SUCCESS) { 12856 SATA_LOG_D((sata_hba_inst, CE_WARN, 12857 "%s set UDMA mode\n", finfo)); 12858 rval = SATA_FAILURE; 12859 } 12860 } 12861 } 12862 12863 if (!(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) && 12864 !(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) { 12865 /* None of the features is supported - do nothing */ 12866 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12867 "settable features not supported\n", NULL); 12868 goto update_sdinfo; 12869 } 12870 12871 if (((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) && 12872 (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) && 12873 ((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) && 12874 (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) { 12875 /* Nothing to do */ 12876 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12877 "no device features to set\n", NULL); 12878 goto update_sdinfo; 12879 } 12880 12881 if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) && 12882 (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD))) { 12883 if (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) { 12884 /* Enable read ahead / read cache */ 12885 cache_op = SATAC_SF_ENABLE_READ_AHEAD; 12886 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12887 "enabling read cache\n", NULL); 12888 } else { 12889 /* Disable read ahead / read cache */ 12890 cache_op = SATAC_SF_DISABLE_READ_AHEAD; 12891 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12892 "disabling read cache\n", NULL); 12893 } 12894 12895 /* Try to set read cache mode */ 12896 rval_set = sata_set_cache_mode(sata_hba_inst, &new_sdinfo, 12897 cache_op); 12898 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS) 12899 rval = rval_set; 12900 } 12901 12902 if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) && 12903 (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) { 12904 if (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) { 12905 /* Enable write cache */ 12906 cache_op = SATAC_SF_ENABLE_WRITE_CACHE; 12907 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12908 "enabling write cache\n", NULL); 12909 } else { 12910 /* Disable write cache */ 12911 cache_op = SATAC_SF_DISABLE_WRITE_CACHE; 12912 SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst, 12913 "disabling write cache\n", NULL); 12914 } 12915 /* Try to set write cache mode */ 12916 rval_set = sata_set_cache_mode(sata_hba_inst, &new_sdinfo, 12917 cache_op); 12918 if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS) 12919 rval = rval_set; 12920 } 12921 if (rval != SATA_SUCCESS) 12922 SATA_LOG_D((sata_hba_inst, CE_WARN, 12923 "%s %s", finfo, finfox)); 12924 12925 update_sdinfo: 12926 /* 12927 * We need to fetch Device Identify data again 12928 */ 12929 if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) { 12930 /* 12931 * Cannot get device identification - retry later 12932 */ 12933 SATA_LOG_D((sata_hba_inst, CE_WARN, 12934 "%s re-fetch device identify data\n", finfo)); 12935 rval = SATA_FAILURE; 12936 } 12937 /* Copy device sata info. */ 12938 sdinfo->satadrv_id = new_sdinfo.satadrv_id; 12939 12940 return (rval); 12941 } 12942 12943 12944 /* 12945 * 12946 * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if 12947 * unable to determine. 12948 * 12949 * Cannot be called in an interrupt context. 12950 * 12951 * Called by sata_build_lsense_page_2f() 12952 */ 12953 12954 static int 12955 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst, 12956 sata_drive_info_t *sdinfo) 12957 { 12958 sata_pkt_t *spkt; 12959 sata_cmd_t *scmd; 12960 sata_pkt_txlate_t *spx; 12961 int rval; 12962 12963 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 12964 spx->txlt_sata_hba_inst = sata_hba_inst; 12965 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 12966 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 12967 if (spkt == NULL) { 12968 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 12969 return (-1); 12970 } 12971 /* address is needed now */ 12972 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12973 12974 12975 /* Fill sata_pkt */ 12976 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 12977 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 12978 /* Synchronous mode, no callback */ 12979 spkt->satapkt_comp = NULL; 12980 /* Timeout 30s */ 12981 spkt->satapkt_time = sata_default_pkt_time; 12982 12983 scmd = &spkt->satapkt_cmd; 12984 scmd->satacmd_flags.sata_special_regs = B_TRUE; 12985 scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER; 12986 12987 /* Set up which registers need to be returned */ 12988 scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE; 12989 scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE; 12990 12991 /* Build SMART_RETURN_STATUS cmd in the sata_pkt */ 12992 scmd->satacmd_addr_type = 0; /* N/A */ 12993 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 12994 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 12995 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 12996 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 12997 scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS; 12998 scmd->satacmd_device_reg = 0; /* Always device 0 */ 12999 scmd->satacmd_cmd_reg = SATAC_SMART; 13000 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 13001 sdinfo->satadrv_addr.cport))); 13002 13003 13004 /* Send pkt to SATA HBA driver */ 13005 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 13006 SATA_TRAN_ACCEPTED || 13007 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 13008 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13009 sdinfo->satadrv_addr.cport))); 13010 /* 13011 * Whoops, no SMART RETURN STATUS 13012 */ 13013 rval = -1; 13014 } else { 13015 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13016 sdinfo->satadrv_addr.cport))); 13017 if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) { 13018 rval = -1; 13019 goto fail; 13020 } 13021 if (scmd->satacmd_status_reg & SATA_STATUS_ERR) { 13022 rval = -1; 13023 goto fail; 13024 } 13025 if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) && 13026 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2)) 13027 rval = 0; 13028 else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) && 13029 (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4)) 13030 rval = 1; 13031 else { 13032 rval = -1; 13033 goto fail; 13034 } 13035 } 13036 fail: 13037 /* Free allocated resources */ 13038 sata_pkt_free(spx); 13039 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13040 13041 return (rval); 13042 } 13043 13044 /* 13045 * 13046 * Returns 0 if succeeded, -1 otherwise 13047 * 13048 * Cannot be called in an interrupt context. 13049 * 13050 */ 13051 static int 13052 sata_fetch_smart_data( 13053 sata_hba_inst_t *sata_hba_inst, 13054 sata_drive_info_t *sdinfo, 13055 struct smart_data *smart_data) 13056 { 13057 sata_pkt_t *spkt; 13058 sata_cmd_t *scmd; 13059 sata_pkt_txlate_t *spx; 13060 int rval; 13061 13062 #if ! defined(lint) 13063 ASSERT(sizeof (struct smart_data) == 512); 13064 #endif 13065 13066 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13067 spx->txlt_sata_hba_inst = sata_hba_inst; 13068 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13069 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13070 if (spkt == NULL) { 13071 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13072 return (-1); 13073 } 13074 /* address is needed now */ 13075 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13076 13077 13078 /* Fill sata_pkt */ 13079 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13080 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13081 /* Synchronous mode, no callback */ 13082 spkt->satapkt_comp = NULL; 13083 /* Timeout 30s */ 13084 spkt->satapkt_time = sata_default_pkt_time; 13085 13086 scmd = &spkt->satapkt_cmd; 13087 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 13088 13089 /* 13090 * Allocate buffer for SMART data 13091 */ 13092 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 13093 sizeof (struct smart_data)); 13094 if (scmd->satacmd_bp == NULL) { 13095 sata_pkt_free(spx); 13096 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13097 SATA_LOG_D((sata_hba_inst, CE_WARN, 13098 "sata_fetch_smart_data: " 13099 "cannot allocate buffer")); 13100 return (-1); 13101 } 13102 13103 13104 /* Build SMART_READ_DATA cmd in the sata_pkt */ 13105 scmd->satacmd_addr_type = 0; /* N/A */ 13106 scmd->satacmd_sec_count_lsb = 0; /* N/A */ 13107 scmd->satacmd_lba_low_lsb = 0; /* N/A */ 13108 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 13109 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 13110 scmd->satacmd_features_reg = SATA_SMART_READ_DATA; 13111 scmd->satacmd_device_reg = 0; /* Always device 0 */ 13112 scmd->satacmd_cmd_reg = SATAC_SMART; 13113 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 13114 sdinfo->satadrv_addr.cport))); 13115 13116 /* Send pkt to SATA HBA driver */ 13117 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 13118 SATA_TRAN_ACCEPTED || 13119 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 13120 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13121 sdinfo->satadrv_addr.cport))); 13122 /* 13123 * Whoops, no SMART DATA available 13124 */ 13125 rval = -1; 13126 goto fail; 13127 } else { 13128 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13129 sdinfo->satadrv_addr.cport))); 13130 if (spx->txlt_buf_dma_handle != NULL) { 13131 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 13132 DDI_DMA_SYNC_FORKERNEL); 13133 ASSERT(rval == DDI_SUCCESS); 13134 } 13135 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data, 13136 sizeof (struct smart_data)); 13137 } 13138 13139 fail: 13140 /* Free allocated resources */ 13141 sata_free_local_buffer(spx); 13142 sata_pkt_free(spx); 13143 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13144 13145 return (rval); 13146 } 13147 13148 /* 13149 * Used by LOG SENSE page 0x10 13150 * Reads (in synchronous mode) the self test log data using Read Log Ext cmd. 13151 * Note: cannot be called in the interrupt context. 13152 * 13153 * return 0 for success, -1 otherwise 13154 * 13155 */ 13156 static int 13157 sata_ext_smart_selftest_read_log( 13158 sata_hba_inst_t *sata_hba_inst, 13159 sata_drive_info_t *sdinfo, 13160 struct smart_ext_selftest_log *ext_selftest_log, 13161 uint16_t block_num) 13162 { 13163 sata_pkt_txlate_t *spx; 13164 sata_pkt_t *spkt; 13165 sata_cmd_t *scmd; 13166 int rval; 13167 13168 #if ! defined(lint) 13169 ASSERT(sizeof (struct smart_ext_selftest_log) == 512); 13170 #endif 13171 13172 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13173 spx->txlt_sata_hba_inst = sata_hba_inst; 13174 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13175 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13176 if (spkt == NULL) { 13177 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13178 return (-1); 13179 } 13180 /* address is needed now */ 13181 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13182 13183 13184 /* Fill sata_pkt */ 13185 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13186 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13187 /* Synchronous mode, no callback */ 13188 spkt->satapkt_comp = NULL; 13189 /* Timeout 30s */ 13190 spkt->satapkt_time = sata_default_pkt_time; 13191 13192 scmd = &spkt->satapkt_cmd; 13193 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 13194 13195 /* 13196 * Allocate buffer for SMART extended self-test log 13197 */ 13198 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 13199 sizeof (struct smart_ext_selftest_log)); 13200 if (scmd->satacmd_bp == NULL) { 13201 sata_pkt_free(spx); 13202 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13203 SATA_LOG_D((sata_hba_inst, CE_WARN, 13204 "sata_ext_smart_selftest_log: " 13205 "cannot allocate buffer")); 13206 return (-1); 13207 } 13208 13209 /* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */ 13210 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 13211 scmd->satacmd_sec_count_lsb = 1; /* One sector of selftest log */ 13212 scmd->satacmd_sec_count_msb = 0; /* One sector of selftest log */ 13213 scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE; 13214 scmd->satacmd_lba_low_msb = 0; 13215 scmd->satacmd_lba_mid_lsb = block_num & 0xff; 13216 scmd->satacmd_lba_mid_msb = block_num >> 8; 13217 scmd->satacmd_device_reg = 0; /* Always device 0 */ 13218 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 13219 13220 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 13221 sdinfo->satadrv_addr.cport))); 13222 13223 /* Send pkt to SATA HBA driver */ 13224 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 13225 SATA_TRAN_ACCEPTED || 13226 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 13227 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13228 sdinfo->satadrv_addr.cport))); 13229 13230 /* 13231 * Whoops, no SMART selftest log info available 13232 */ 13233 rval = -1; 13234 goto fail; 13235 } else { 13236 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13237 sdinfo->satadrv_addr.cport))); 13238 13239 if (spx->txlt_buf_dma_handle != NULL) { 13240 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 13241 DDI_DMA_SYNC_FORKERNEL); 13242 ASSERT(rval == DDI_SUCCESS); 13243 } 13244 bcopy(scmd->satacmd_bp->b_un.b_addr, 13245 (uint8_t *)ext_selftest_log, 13246 sizeof (struct smart_ext_selftest_log)); 13247 rval = 0; 13248 } 13249 13250 fail: 13251 /* Free allocated resources */ 13252 sata_free_local_buffer(spx); 13253 sata_pkt_free(spx); 13254 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13255 13256 return (rval); 13257 } 13258 13259 /* 13260 * Returns 0 for success, -1 otherwise 13261 * 13262 * SMART self-test log data is returned in buffer pointed to by selftest_log 13263 */ 13264 static int 13265 sata_smart_selftest_log( 13266 sata_hba_inst_t *sata_hba_inst, 13267 sata_drive_info_t *sdinfo, 13268 struct smart_selftest_log *selftest_log) 13269 { 13270 sata_pkt_t *spkt; 13271 sata_cmd_t *scmd; 13272 sata_pkt_txlate_t *spx; 13273 int rval; 13274 13275 #if ! defined(lint) 13276 ASSERT(sizeof (struct smart_selftest_log) == 512); 13277 #endif 13278 13279 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13280 spx->txlt_sata_hba_inst = sata_hba_inst; 13281 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13282 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13283 if (spkt == NULL) { 13284 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13285 return (-1); 13286 } 13287 /* address is needed now */ 13288 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13289 13290 13291 /* Fill sata_pkt */ 13292 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13293 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13294 /* Synchronous mode, no callback */ 13295 spkt->satapkt_comp = NULL; 13296 /* Timeout 30s */ 13297 spkt->satapkt_time = sata_default_pkt_time; 13298 13299 scmd = &spkt->satapkt_cmd; 13300 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 13301 13302 /* 13303 * Allocate buffer for SMART SELFTEST LOG 13304 */ 13305 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 13306 sizeof (struct smart_selftest_log)); 13307 if (scmd->satacmd_bp == NULL) { 13308 sata_pkt_free(spx); 13309 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13310 SATA_LOG_D((sata_hba_inst, CE_WARN, 13311 "sata_smart_selftest_log: " 13312 "cannot allocate buffer")); 13313 return (-1); 13314 } 13315 13316 /* Build SMART_READ_LOG cmd in the sata_pkt */ 13317 scmd->satacmd_addr_type = 0; /* N/A */ 13318 scmd->satacmd_sec_count_lsb = 1; /* One sector of SMART log */ 13319 scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE; 13320 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 13321 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 13322 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 13323 scmd->satacmd_device_reg = 0; /* Always device 0 */ 13324 scmd->satacmd_cmd_reg = SATAC_SMART; 13325 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 13326 sdinfo->satadrv_addr.cport))); 13327 13328 /* Send pkt to SATA HBA driver */ 13329 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 13330 SATA_TRAN_ACCEPTED || 13331 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 13332 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13333 sdinfo->satadrv_addr.cport))); 13334 /* 13335 * Whoops, no SMART DATA available 13336 */ 13337 rval = -1; 13338 goto fail; 13339 } else { 13340 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13341 sdinfo->satadrv_addr.cport))); 13342 if (spx->txlt_buf_dma_handle != NULL) { 13343 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 13344 DDI_DMA_SYNC_FORKERNEL); 13345 ASSERT(rval == DDI_SUCCESS); 13346 } 13347 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log, 13348 sizeof (struct smart_selftest_log)); 13349 rval = 0; 13350 } 13351 13352 fail: 13353 /* Free allocated resources */ 13354 sata_free_local_buffer(spx); 13355 sata_pkt_free(spx); 13356 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13357 13358 return (rval); 13359 } 13360 13361 13362 /* 13363 * Returns 0 for success, -1 otherwise 13364 * 13365 * SMART READ LOG data is returned in buffer pointed to by smart_log 13366 */ 13367 static int 13368 sata_smart_read_log( 13369 sata_hba_inst_t *sata_hba_inst, 13370 sata_drive_info_t *sdinfo, 13371 uint8_t *smart_log, /* where the data should be returned */ 13372 uint8_t which_log, /* which log should be returned */ 13373 uint8_t log_size) /* # of 512 bytes in log */ 13374 { 13375 sata_pkt_t *spkt; 13376 sata_cmd_t *scmd; 13377 sata_pkt_txlate_t *spx; 13378 int rval; 13379 13380 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13381 spx->txlt_sata_hba_inst = sata_hba_inst; 13382 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13383 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13384 if (spkt == NULL) { 13385 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13386 return (-1); 13387 } 13388 /* address is needed now */ 13389 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13390 13391 13392 /* Fill sata_pkt */ 13393 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13394 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13395 /* Synchronous mode, no callback */ 13396 spkt->satapkt_comp = NULL; 13397 /* Timeout 30s */ 13398 spkt->satapkt_time = sata_default_pkt_time; 13399 13400 scmd = &spkt->satapkt_cmd; 13401 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 13402 13403 /* 13404 * Allocate buffer for SMART READ LOG 13405 */ 13406 scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512); 13407 if (scmd->satacmd_bp == NULL) { 13408 sata_pkt_free(spx); 13409 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13410 SATA_LOG_D((sata_hba_inst, CE_WARN, 13411 "sata_smart_read_log: " "cannot allocate buffer")); 13412 return (-1); 13413 } 13414 13415 /* Build SMART_READ_LOG cmd in the sata_pkt */ 13416 scmd->satacmd_addr_type = 0; /* N/A */ 13417 scmd->satacmd_sec_count_lsb = log_size; /* what the caller asked for */ 13418 scmd->satacmd_lba_low_lsb = which_log; /* which log page */ 13419 scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1; 13420 scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2; 13421 scmd->satacmd_features_reg = SATA_SMART_READ_LOG; 13422 scmd->satacmd_device_reg = 0; /* Always device 0 */ 13423 scmd->satacmd_cmd_reg = SATAC_SMART; 13424 13425 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 13426 sdinfo->satadrv_addr.cport))); 13427 13428 /* Send pkt to SATA HBA driver */ 13429 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 13430 SATA_TRAN_ACCEPTED || 13431 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 13432 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13433 sdinfo->satadrv_addr.cport))); 13434 13435 /* 13436 * Whoops, no SMART DATA available 13437 */ 13438 rval = -1; 13439 goto fail; 13440 } else { 13441 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13442 sdinfo->satadrv_addr.cport))); 13443 13444 if (spx->txlt_buf_dma_handle != NULL) { 13445 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 13446 DDI_DMA_SYNC_FORKERNEL); 13447 ASSERT(rval == DDI_SUCCESS); 13448 } 13449 bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512); 13450 rval = 0; 13451 } 13452 13453 fail: 13454 /* Free allocated resources */ 13455 sata_free_local_buffer(spx); 13456 sata_pkt_free(spx); 13457 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13458 13459 return (rval); 13460 } 13461 13462 /* 13463 * Used by LOG SENSE page 0x10 13464 * 13465 * return 0 for success, -1 otherwise 13466 * 13467 */ 13468 static int 13469 sata_read_log_ext_directory( 13470 sata_hba_inst_t *sata_hba_inst, 13471 sata_drive_info_t *sdinfo, 13472 struct read_log_ext_directory *logdir) 13473 { 13474 sata_pkt_txlate_t *spx; 13475 sata_pkt_t *spkt; 13476 sata_cmd_t *scmd; 13477 int rval; 13478 13479 #if ! defined(lint) 13480 ASSERT(sizeof (struct read_log_ext_directory) == 512); 13481 #endif 13482 13483 spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP); 13484 spx->txlt_sata_hba_inst = sata_hba_inst; 13485 spx->txlt_scsi_pkt = NULL; /* No scsi pkt involved */ 13486 spkt = sata_pkt_alloc(spx, SLEEP_FUNC); 13487 if (spkt == NULL) { 13488 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13489 return (-1); 13490 } 13491 13492 /* Fill sata_pkt */ 13493 spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr; 13494 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13495 /* Synchronous mode, no callback */ 13496 spkt->satapkt_comp = NULL; 13497 /* Timeout 30s */ 13498 spkt->satapkt_time = sata_default_pkt_time; 13499 13500 scmd = &spkt->satapkt_cmd; 13501 scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ; 13502 13503 /* 13504 * Allocate buffer for SMART READ LOG EXTENDED command 13505 */ 13506 scmd->satacmd_bp = sata_alloc_local_buffer(spx, 13507 sizeof (struct read_log_ext_directory)); 13508 if (scmd->satacmd_bp == NULL) { 13509 sata_pkt_free(spx); 13510 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13511 SATA_LOG_D((sata_hba_inst, CE_WARN, 13512 "sata_read_log_ext_directory: " 13513 "cannot allocate buffer")); 13514 return (-1); 13515 } 13516 13517 /* Build READ LOG EXT w/ log directory cmd in the sata_pkt */ 13518 scmd->satacmd_addr_type = ATA_ADDR_LBA48; 13519 scmd->satacmd_sec_count_lsb = 1; /* One sector of directory */ 13520 scmd->satacmd_sec_count_msb = 0; /* One sector of directory */ 13521 scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY; 13522 scmd->satacmd_lba_low_msb = 0; 13523 scmd->satacmd_lba_mid_lsb = 0; 13524 scmd->satacmd_lba_mid_msb = 0; 13525 scmd->satacmd_device_reg = 0; /* Always device 0 */ 13526 scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT; 13527 13528 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, 13529 sdinfo->satadrv_addr.cport))); 13530 13531 /* Send pkt to SATA HBA driver */ 13532 if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) != 13533 SATA_TRAN_ACCEPTED || 13534 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 13535 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13536 sdinfo->satadrv_addr.cport))); 13537 /* 13538 * Whoops, no SMART selftest log info available 13539 */ 13540 rval = -1; 13541 goto fail; 13542 } else { 13543 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, 13544 sdinfo->satadrv_addr.cport))); 13545 if (spx->txlt_buf_dma_handle != NULL) { 13546 rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0, 13547 DDI_DMA_SYNC_FORKERNEL); 13548 ASSERT(rval == DDI_SUCCESS); 13549 } 13550 bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir, 13551 sizeof (struct read_log_ext_directory)); 13552 rval = 0; 13553 } 13554 13555 fail: 13556 /* Free allocated resources */ 13557 sata_free_local_buffer(spx); 13558 sata_pkt_free(spx); 13559 kmem_free(spx, sizeof (sata_pkt_txlate_t)); 13560 13561 return (rval); 13562 } 13563 13564 /* 13565 * Set up error retrieval sata command for NCQ command error data 13566 * recovery. 13567 * 13568 * Returns SATA_SUCCESS when data buffer is allocated and packet set-up, 13569 * returns SATA_FAILURE otherwise. 13570 */ 13571 static int 13572 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo) 13573 { 13574 #ifndef __lock_lint 13575 _NOTE(ARGUNUSED(sdinfo)) 13576 #endif 13577 13578 sata_pkt_t *spkt = spx->txlt_sata_pkt; 13579 sata_cmd_t *scmd; 13580 struct buf *bp; 13581 13582 /* Operation modes are up to the caller */ 13583 spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS; 13584 13585 /* Synchronous mode, no callback - may be changed by the caller */ 13586 spkt->satapkt_comp = NULL; 13587 spkt->satapkt_time = sata_default_pkt_time; 13588 13589 scmd = &spkt->satapkt_cmd; 13590 bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t)); 13591 scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE; 13592 13593 /* 13594 * Allocate dma_able buffer error data. 13595 * Buffer allocation will take care of buffer alignment and other DMA 13596 * attributes. 13597 */ 13598 bp = sata_alloc_local_buffer(spx, 13599 sizeof (struct sata_ncq_error_recovery_page)); 13600 if (bp == NULL) 13601 return (SATA_FAILURE); 13602 13603 bp_mapin(bp); /* make data buffer accessible */ 13604 scmd->satacmd_bp = bp; 13605 13606 /* 13607 * Set-up pointer to the buffer handle, so HBA can sync buffer 13608 * before accessing it. Handle is in usual place in translate struct. 13609 */ 13610 scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle; 13611 13612 ASSERT(scmd->satacmd_num_dma_cookies != 0); 13613 ASSERT(scmd->satacmd_dma_cookie_list != NULL); 13614 13615 return (SATA_SUCCESS); 13616 } 13617 13618 /* 13619 * sata_xlate_errors() is used to translate (S)ATA error 13620 * information to SCSI information returned in the SCSI 13621 * packet. 13622 */ 13623 static void 13624 sata_xlate_errors(sata_pkt_txlate_t *spx) 13625 { 13626 struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt; 13627 struct scsi_extended_sense *sense; 13628 13629 scsipkt->pkt_reason = CMD_INCOMPLETE; 13630 *scsipkt->pkt_scbp = STATUS_CHECK; 13631 sense = sata_arq_sense(spx); 13632 13633 switch (spx->txlt_sata_pkt->satapkt_reason) { 13634 case SATA_PKT_PORT_ERROR: 13635 /* 13636 * We have no device data. Assume no data transfered. 13637 */ 13638 sense->es_key = KEY_HARDWARE_ERROR; 13639 break; 13640 13641 case SATA_PKT_DEV_ERROR: 13642 if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg & 13643 SATA_STATUS_ERR) { 13644 /* 13645 * determine dev error reason from error 13646 * reg content 13647 */ 13648 sata_decode_device_error(spx, sense); 13649 break; 13650 } 13651 /* No extended sense key - no info available */ 13652 break; 13653 13654 case SATA_PKT_TIMEOUT: 13655 scsipkt->pkt_reason = CMD_TIMEOUT; 13656 scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET; 13657 /* No extended sense key */ 13658 break; 13659 13660 case SATA_PKT_ABORTED: 13661 scsipkt->pkt_reason = CMD_ABORTED; 13662 scsipkt->pkt_statistics |= STAT_ABORTED; 13663 /* No extended sense key */ 13664 break; 13665 13666 case SATA_PKT_RESET: 13667 /* 13668 * pkt aborted either by an explicit reset request from 13669 * a host, or due to error recovery 13670 */ 13671 scsipkt->pkt_reason = CMD_RESET; 13672 scsipkt->pkt_statistics |= STAT_DEV_RESET; 13673 break; 13674 13675 default: 13676 scsipkt->pkt_reason = CMD_TRAN_ERR; 13677 break; 13678 } 13679 } 13680 13681 13682 13683 13684 /* 13685 * Log sata message 13686 * dev pathname msg line preceeds the logged message. 13687 */ 13688 13689 static void 13690 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...) 13691 { 13692 char pathname[128]; 13693 dev_info_t *dip; 13694 va_list ap; 13695 13696 mutex_enter(&sata_log_mutex); 13697 13698 va_start(ap, fmt); 13699 (void) vsprintf(sata_log_buf, fmt, ap); 13700 va_end(ap); 13701 13702 if (sata_hba_inst != NULL) { 13703 dip = SATA_DIP(sata_hba_inst); 13704 (void) ddi_pathname(dip, pathname); 13705 } else { 13706 pathname[0] = 0; 13707 } 13708 if (level == CE_CONT) { 13709 if (sata_debug_flags == 0) 13710 cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf); 13711 else 13712 cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf); 13713 } else { 13714 if (level != CE_NOTE) { 13715 cmn_err(level, "%s:\n %s", pathname, sata_log_buf); 13716 } else if (sata_msg) { 13717 cmn_err(level, "%s:\n %s", pathname, 13718 sata_log_buf); 13719 } 13720 } 13721 13722 mutex_exit(&sata_log_mutex); 13723 } 13724 13725 13726 /* ******** Asynchronous HBA events handling & hotplugging support ******** */ 13727 13728 /* 13729 * Start or terminate the thread, depending on flag arg and current state 13730 */ 13731 static void 13732 sata_event_thread_control(int startstop) 13733 { 13734 static int sata_event_thread_terminating = 0; 13735 static int sata_event_thread_starting = 0; 13736 int i; 13737 13738 mutex_enter(&sata_event_mutex); 13739 13740 if (startstop == 0 && (sata_event_thread_starting == 1 || 13741 sata_event_thread_terminating == 1)) { 13742 mutex_exit(&sata_event_mutex); 13743 return; 13744 } 13745 if (startstop == 1 && sata_event_thread_starting == 1) { 13746 mutex_exit(&sata_event_mutex); 13747 return; 13748 } 13749 if (startstop == 1 && sata_event_thread_terminating == 1) { 13750 sata_event_thread_starting = 1; 13751 /* wait til terminate operation completes */ 13752 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 13753 while (sata_event_thread_terminating == 1) { 13754 if (i-- <= 0) { 13755 sata_event_thread_starting = 0; 13756 mutex_exit(&sata_event_mutex); 13757 #ifdef SATA_DEBUG 13758 cmn_err(CE_WARN, "sata_event_thread_control: " 13759 "timeout waiting for thread to terminate"); 13760 #endif 13761 return; 13762 } 13763 mutex_exit(&sata_event_mutex); 13764 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 13765 mutex_enter(&sata_event_mutex); 13766 } 13767 } 13768 if (startstop == 1) { 13769 if (sata_event_thread == NULL) { 13770 sata_event_thread = thread_create(NULL, 0, 13771 (void (*)())sata_event_daemon, 13772 &sata_hba_list, 0, &p0, TS_RUN, minclsyspri); 13773 } 13774 sata_event_thread_starting = 0; 13775 mutex_exit(&sata_event_mutex); 13776 return; 13777 } 13778 13779 /* 13780 * If we got here, thread may need to be terminated 13781 */ 13782 if (sata_event_thread != NULL) { 13783 int i; 13784 /* Signal event thread to go away */ 13785 sata_event_thread_terminating = 1; 13786 sata_event_thread_terminate = 1; 13787 cv_signal(&sata_event_cv); 13788 /* 13789 * Wait til daemon terminates. 13790 */ 13791 i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT; 13792 while (sata_event_thread_terminate == 1) { 13793 mutex_exit(&sata_event_mutex); 13794 if (i-- <= 0) { 13795 /* Daemon did not go away !!! */ 13796 #ifdef SATA_DEBUG 13797 cmn_err(CE_WARN, "sata_event_thread_control: " 13798 "cannot terminate event daemon thread"); 13799 #endif 13800 mutex_enter(&sata_event_mutex); 13801 break; 13802 } 13803 delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT)); 13804 mutex_enter(&sata_event_mutex); 13805 } 13806 sata_event_thread_terminating = 0; 13807 } 13808 ASSERT(sata_event_thread_terminating == 0); 13809 ASSERT(sata_event_thread_starting == 0); 13810 mutex_exit(&sata_event_mutex); 13811 } 13812 13813 13814 /* 13815 * SATA HBA event notification function. 13816 * Events reported by SATA HBA drivers per HBA instance relate to a change in 13817 * a port and/or device state or a controller itself. 13818 * Events for different addresses/addr types cannot be combined. 13819 * A warning message is generated for each event type. 13820 * Events are not processed by this function, so only the 13821 * event flag(s)is set for an affected entity and the event thread is 13822 * waken up. Event daemon thread processes all events. 13823 * 13824 * NOTE: Since more than one event may be reported at the same time, one 13825 * cannot determine a sequence of events when opposite event are reported, eg. 13826 * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing 13827 * is taking precedence over reported events, i.e. may cause ignoring some 13828 * events. 13829 */ 13830 #define SATA_EVENT_MAX_MSG_LENGTH 79 13831 13832 void 13833 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event) 13834 { 13835 sata_hba_inst_t *sata_hba_inst = NULL; 13836 sata_address_t *saddr; 13837 sata_drive_info_t *sdinfo; 13838 sata_port_stats_t *pstats; 13839 sata_cport_info_t *cportinfo; 13840 sata_pmport_info_t *pmportinfo; 13841 int cport, pmport; 13842 char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1]; 13843 char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1]; 13844 char *lcp; 13845 static char *err_msg_evnt_1 = 13846 "sata_hba_event_notify: invalid port event 0x%x "; 13847 static char *err_msg_evnt_2 = 13848 "sata_hba_event_notify: invalid device event 0x%x "; 13849 int linkevent; 13850 13851 /* 13852 * There is a possibility that an event will be generated on HBA 13853 * that has not completed attachment or is detaching. We still want 13854 * to process events until HBA is detached. 13855 */ 13856 mutex_enter(&sata_mutex); 13857 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 13858 sata_hba_inst = sata_hba_inst->satahba_next) { 13859 if (SATA_DIP(sata_hba_inst) == dip) 13860 if (sata_hba_inst->satahba_attached == 1) 13861 break; 13862 } 13863 mutex_exit(&sata_mutex); 13864 if (sata_hba_inst == NULL) 13865 /* HBA not attached */ 13866 return; 13867 13868 ASSERT(sata_device != NULL); 13869 13870 /* 13871 * Validate address before - do not proceed with invalid address. 13872 */ 13873 saddr = &sata_device->satadev_addr; 13874 if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst)) 13875 return; 13876 if (saddr->qual == SATA_ADDR_PMPORT || 13877 saddr->qual == SATA_ADDR_DPMPORT) 13878 /* Port Multiplier not supported yet */ 13879 return; 13880 13881 cport = saddr->cport; 13882 pmport = saddr->pmport; 13883 13884 buf1[0] = buf2[0] = '\0'; 13885 13886 /* 13887 * If event relates to port or device, check port state. 13888 * Port has to be initialized, or we cannot accept an event. 13889 */ 13890 if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT | 13891 SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) != 0) { 13892 if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_DCPORT)) != 0) { 13893 mutex_enter(&sata_hba_inst->satahba_mutex); 13894 cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport); 13895 mutex_exit(&sata_hba_inst->satahba_mutex); 13896 if (cportinfo == NULL || cportinfo->cport_state == 0) 13897 return; 13898 } else { 13899 mutex_enter(&sata_hba_inst->satahba_mutex); 13900 pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, 13901 cport, pmport); 13902 mutex_exit(&sata_hba_inst->satahba_mutex); 13903 if (pmportinfo == NULL || pmportinfo->pmport_state == 0) 13904 return; 13905 } 13906 } 13907 13908 /* 13909 * Events refer to devices, ports and controllers - each has 13910 * unique address. Events for different addresses cannot be combined. 13911 */ 13912 if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) { 13913 13914 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 13915 13916 /* qualify this event(s) */ 13917 if ((event & SATA_EVNT_PORT_EVENTS) == 0) { 13918 /* Invalid event for the device port */ 13919 (void) sprintf(buf2, err_msg_evnt_1, 13920 event & SATA_EVNT_PORT_EVENTS); 13921 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 13922 goto event_info; 13923 } 13924 if (saddr->qual == SATA_ADDR_CPORT) { 13925 /* Controller's device port event */ 13926 13927 (SATA_CPORT_INFO(sata_hba_inst, cport))-> 13928 cport_event_flags |= 13929 event & SATA_EVNT_PORT_EVENTS; 13930 pstats = 13931 &(SATA_CPORT_INFO(sata_hba_inst, cport))-> 13932 cport_stats; 13933 } else { 13934 /* Port multiplier's device port event */ 13935 (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 13936 pmport_event_flags |= 13937 event & SATA_EVNT_PORT_EVENTS; 13938 pstats = 13939 &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))-> 13940 pmport_stats; 13941 } 13942 13943 /* 13944 * Add to statistics and log the message. We have to do it 13945 * here rather than in the event daemon, because there may be 13946 * multiple events occuring before they are processed. 13947 */ 13948 linkevent = event & 13949 (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED); 13950 if (linkevent) { 13951 if (linkevent == (SATA_EVNT_LINK_LOST | 13952 SATA_EVNT_LINK_ESTABLISHED)) { 13953 /* This is likely event combination */ 13954 (void) strlcat(buf1, "link lost/established, ", 13955 SATA_EVENT_MAX_MSG_LENGTH); 13956 13957 if (pstats->link_lost < 0xffffffffffffffffULL) 13958 pstats->link_lost++; 13959 if (pstats->link_established < 13960 0xffffffffffffffffULL) 13961 pstats->link_established++; 13962 linkevent = 0; 13963 } else if (linkevent & SATA_EVNT_LINK_LOST) { 13964 (void) strlcat(buf1, "link lost, ", 13965 SATA_EVENT_MAX_MSG_LENGTH); 13966 13967 if (pstats->link_lost < 0xffffffffffffffffULL) 13968 pstats->link_lost++; 13969 } else { 13970 (void) strlcat(buf1, "link established, ", 13971 SATA_EVENT_MAX_MSG_LENGTH); 13972 if (pstats->link_established < 13973 0xffffffffffffffffULL) 13974 pstats->link_established++; 13975 } 13976 } 13977 if (event & SATA_EVNT_DEVICE_ATTACHED) { 13978 (void) strlcat(buf1, "device attached, ", 13979 SATA_EVENT_MAX_MSG_LENGTH); 13980 if (pstats->device_attached < 0xffffffffffffffffULL) 13981 pstats->device_attached++; 13982 } 13983 if (event & SATA_EVNT_DEVICE_DETACHED) { 13984 (void) strlcat(buf1, "device detached, ", 13985 SATA_EVENT_MAX_MSG_LENGTH); 13986 if (pstats->device_detached < 0xffffffffffffffffULL) 13987 pstats->device_detached++; 13988 } 13989 if (event & SATA_EVNT_PWR_LEVEL_CHANGED) { 13990 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 13991 "port %d power level changed", cport); 13992 if (pstats->port_pwr_changed < 0xffffffffffffffffULL) 13993 pstats->port_pwr_changed++; 13994 } 13995 13996 if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) { 13997 /* There should be no other events for this address */ 13998 (void) sprintf(buf2, err_msg_evnt_1, 13999 event & ~SATA_EVNT_PORT_EVENTS); 14000 } 14001 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 14002 14003 } else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) { 14004 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 14005 14006 /* qualify this event */ 14007 if ((event & SATA_EVNT_DEVICE_RESET) == 0) { 14008 /* Invalid event for a device */ 14009 (void) sprintf(buf2, err_msg_evnt_2, 14010 event & SATA_EVNT_DEVICE_RESET); 14011 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 14012 goto event_info; 14013 } 14014 /* drive event */ 14015 sdinfo = sata_get_device_info(sata_hba_inst, sata_device); 14016 if (sdinfo != NULL) { 14017 if (event & SATA_EVNT_DEVICE_RESET) { 14018 (void) strlcat(buf1, "device reset, ", 14019 SATA_EVENT_MAX_MSG_LENGTH); 14020 if (sdinfo->satadrv_stats.drive_reset < 14021 0xffffffffffffffffULL) 14022 sdinfo->satadrv_stats.drive_reset++; 14023 sdinfo->satadrv_event_flags |= 14024 SATA_EVNT_DEVICE_RESET; 14025 } 14026 } 14027 if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) { 14028 /* Invalid event for a device */ 14029 (void) sprintf(buf2, err_msg_evnt_2, 14030 event & ~SATA_EVNT_DRIVE_EVENTS); 14031 } 14032 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport))); 14033 } else { 14034 if (saddr->qual != SATA_ADDR_NULL) { 14035 /* Wrong address qualifier */ 14036 SATA_LOG_D((sata_hba_inst, CE_WARN, 14037 "sata_hba_event_notify: invalid address 0x%x", 14038 *(uint32_t *)saddr)); 14039 return; 14040 } 14041 if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 || 14042 (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) { 14043 /* Invalid event for the controller */ 14044 SATA_LOG_D((sata_hba_inst, CE_WARN, 14045 "sata_hba_event_notify: invalid event 0x%x for " 14046 "controller", 14047 event & SATA_EVNT_CONTROLLER_EVENTS)); 14048 return; 14049 } 14050 buf1[0] = '\0'; 14051 /* This may be a frequent and not interesting event */ 14052 SATADBG1(SATA_DBG_EVENTS, sata_hba_inst, 14053 "controller power level changed\n", NULL); 14054 14055 mutex_enter(&sata_hba_inst->satahba_mutex); 14056 if (sata_hba_inst->satahba_stats.ctrl_pwr_change < 14057 0xffffffffffffffffULL) 14058 sata_hba_inst->satahba_stats.ctrl_pwr_change++; 14059 14060 sata_hba_inst->satahba_event_flags |= 14061 SATA_EVNT_PWR_LEVEL_CHANGED; 14062 mutex_exit(&sata_hba_inst->satahba_mutex); 14063 } 14064 /* 14065 * If we got here, there is something to do with this HBA 14066 * instance. 14067 */ 14068 mutex_enter(&sata_hba_inst->satahba_mutex); 14069 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 14070 mutex_exit(&sata_hba_inst->satahba_mutex); 14071 mutex_enter(&sata_mutex); 14072 sata_event_pending |= SATA_EVNT_MAIN; /* global event indicator */ 14073 mutex_exit(&sata_mutex); 14074 14075 /* Tickle event thread */ 14076 mutex_enter(&sata_event_mutex); 14077 if (sata_event_thread_active == 0) 14078 cv_signal(&sata_event_cv); 14079 mutex_exit(&sata_event_mutex); 14080 14081 event_info: 14082 if (buf1[0] != '\0') { 14083 lcp = strrchr(buf1, ','); 14084 if (lcp != NULL) 14085 *lcp = '\0'; 14086 } 14087 if (saddr->qual == SATA_ADDR_CPORT || 14088 saddr->qual == SATA_ADDR_DCPORT) { 14089 if (buf1[0] != '\0') { 14090 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 14091 cport, buf1); 14092 } 14093 if (buf2[0] != '\0') { 14094 sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n", 14095 cport, buf2); 14096 } 14097 } else if (saddr->qual == SATA_ADDR_PMPORT || 14098 saddr->qual == SATA_ADDR_DPMPORT) { 14099 if (buf1[0] != '\0') { 14100 sata_log(sata_hba_inst, CE_NOTE, 14101 "port %d pmport %d: %s\n", cport, pmport, buf1); 14102 } 14103 if (buf2[0] != '\0') { 14104 sata_log(sata_hba_inst, CE_NOTE, 14105 "port %d pmport %d: %s\n", cport, pmport, buf2); 14106 } 14107 } 14108 } 14109 14110 14111 /* 14112 * Event processing thread. 14113 * Arg is a pointer to the sata_hba_list pointer. 14114 * It is not really needed, because sata_hba_list is global and static 14115 */ 14116 static void 14117 sata_event_daemon(void *arg) 14118 { 14119 #ifndef __lock_lint 14120 _NOTE(ARGUNUSED(arg)) 14121 #endif 14122 sata_hba_inst_t *sata_hba_inst; 14123 clock_t lbolt; 14124 14125 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 14126 "SATA event daemon started\n", NULL); 14127 loop: 14128 /* 14129 * Process events here. Walk through all registered HBAs 14130 */ 14131 mutex_enter(&sata_mutex); 14132 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 14133 sata_hba_inst = sata_hba_inst->satahba_next) { 14134 ASSERT(sata_hba_inst != NULL); 14135 mutex_enter(&sata_hba_inst->satahba_mutex); 14136 if (sata_hba_inst->satahba_attached == 0 || 14137 (sata_hba_inst->satahba_event_flags & 14138 SATA_EVNT_SKIP) != 0) { 14139 mutex_exit(&sata_hba_inst->satahba_mutex); 14140 continue; 14141 } 14142 if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) { 14143 sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP; 14144 mutex_exit(&sata_hba_inst->satahba_mutex); 14145 mutex_exit(&sata_mutex); 14146 /* Got the controller with pending event */ 14147 sata_process_controller_events(sata_hba_inst); 14148 /* 14149 * Since global mutex was released, there is a 14150 * possibility that HBA list has changed, so start 14151 * over from the top. Just processed controller 14152 * will be passed-over because of the SKIP flag. 14153 */ 14154 goto loop; 14155 } 14156 mutex_exit(&sata_hba_inst->satahba_mutex); 14157 } 14158 /* Clear SKIP flag in all controllers */ 14159 for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL; 14160 sata_hba_inst = sata_hba_inst->satahba_next) { 14161 mutex_enter(&sata_hba_inst->satahba_mutex); 14162 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP; 14163 mutex_exit(&sata_hba_inst->satahba_mutex); 14164 } 14165 mutex_exit(&sata_mutex); 14166 14167 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 14168 "SATA EVENT DAEMON suspending itself", NULL); 14169 14170 #ifdef SATA_DEBUG 14171 if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) { 14172 sata_log(sata_hba_inst, CE_WARN, 14173 "SATA EVENTS PROCESSING DISABLED\n"); 14174 thread_exit(); /* Daemon will not run again */ 14175 } 14176 #endif 14177 mutex_enter(&sata_event_mutex); 14178 sata_event_thread_active = 0; 14179 mutex_exit(&sata_event_mutex); 14180 /* 14181 * Go to sleep/suspend itself and wake up either because new event or 14182 * wait timeout. Exit if there is a termination request (driver 14183 * unload). 14184 */ 14185 do { 14186 lbolt = ddi_get_lbolt(); 14187 lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME); 14188 mutex_enter(&sata_event_mutex); 14189 (void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt); 14190 14191 if (sata_event_thread_active != 0) { 14192 mutex_exit(&sata_event_mutex); 14193 continue; 14194 } 14195 14196 /* Check if it is time to go away */ 14197 if (sata_event_thread_terminate == 1) { 14198 /* 14199 * It is up to the thread setting above flag to make 14200 * sure that this thread is not killed prematurely. 14201 */ 14202 sata_event_thread_terminate = 0; 14203 sata_event_thread = NULL; 14204 mutex_exit(&sata_event_mutex); 14205 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 14206 "SATA_EVENT_DAEMON_TERMINATING", NULL); 14207 thread_exit(); { _NOTE(NOT_REACHED) } 14208 } 14209 mutex_exit(&sata_event_mutex); 14210 } while (!(sata_event_pending & SATA_EVNT_MAIN)); 14211 14212 mutex_enter(&sata_event_mutex); 14213 sata_event_thread_active = 1; 14214 mutex_exit(&sata_event_mutex); 14215 14216 mutex_enter(&sata_mutex); 14217 sata_event_pending &= ~SATA_EVNT_MAIN; 14218 mutex_exit(&sata_mutex); 14219 14220 SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL, 14221 "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL); 14222 14223 goto loop; 14224 } 14225 14226 /* 14227 * Specific HBA instance event processing. 14228 * 14229 * NOTE: At the moment, device event processing is limited to hard disks 14230 * only. 14231 * cports only are supported - no pmports. 14232 */ 14233 static void 14234 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst) 14235 { 14236 int ncport; 14237 uint32_t event_flags; 14238 sata_address_t *saddr; 14239 sata_cport_info_t *cportinfo; 14240 14241 SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst, 14242 "Processing controller %d event(s)", 14243 ddi_get_instance(SATA_DIP(sata_hba_inst))); 14244 14245 mutex_enter(&sata_hba_inst->satahba_mutex); 14246 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN; 14247 event_flags = sata_hba_inst->satahba_event_flags; 14248 mutex_exit(&sata_hba_inst->satahba_mutex); 14249 /* 14250 * Process controller power change first 14251 * HERE 14252 */ 14253 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) 14254 sata_process_cntrl_pwr_level_change(sata_hba_inst); 14255 14256 /* 14257 * Search through ports/devices to identify affected port/device. 14258 * We may have to process events for more than one port/device. 14259 */ 14260 for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) { 14261 /* 14262 * Not all ports may be processed in attach by the time we 14263 * get an event. Check if port info is initialized. 14264 */ 14265 mutex_enter(&sata_hba_inst->satahba_mutex); 14266 cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport); 14267 mutex_exit(&sata_hba_inst->satahba_mutex); 14268 if (cportinfo == NULL || cportinfo->cport_state == NULL) 14269 continue; 14270 14271 /* We have initialized controller port info */ 14272 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 14273 event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 14274 cport_event_flags; 14275 /* Check if port was locked by IOCTL processing */ 14276 if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) { 14277 /* 14278 * We ignore port events because port is busy 14279 * with AP control processing. Set again 14280 * controller and main event flag, so that 14281 * events may be processed by the next daemon 14282 * run. 14283 */ 14284 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 14285 mutex_enter(&sata_hba_inst->satahba_mutex); 14286 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 14287 mutex_exit(&sata_hba_inst->satahba_mutex); 14288 mutex_enter(&sata_mutex); 14289 sata_event_pending |= SATA_EVNT_MAIN; 14290 mutex_exit(&sata_mutex); 14291 SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst, 14292 "Event processing postponed until " 14293 "AP control processing completes", 14294 NULL); 14295 /* Check other ports */ 14296 continue; 14297 } else { 14298 /* 14299 * Set BSY flag so that AP control would not 14300 * interfere with events processing for 14301 * this port. 14302 */ 14303 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 14304 cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY; 14305 } 14306 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 14307 14308 saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr; 14309 14310 if ((event_flags & 14311 (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) { 14312 /* 14313 * Got port event. 14314 * We need some hierarchy of event processing as they 14315 * are affecting each other: 14316 * 1. port failed 14317 * 2. device detached/attached 14318 * 3. link events - link events may trigger device 14319 * detached or device attached events in some 14320 * circumstances. 14321 * 4. port power level changed 14322 */ 14323 if (event_flags & SATA_EVNT_PORT_FAILED) { 14324 sata_process_port_failed_event(sata_hba_inst, 14325 saddr); 14326 } 14327 if (event_flags & SATA_EVNT_DEVICE_DETACHED) { 14328 sata_process_device_detached(sata_hba_inst, 14329 saddr); 14330 } 14331 if (event_flags & SATA_EVNT_DEVICE_ATTACHED) { 14332 sata_process_device_attached(sata_hba_inst, 14333 saddr); 14334 } 14335 if (event_flags & 14336 (SATA_EVNT_LINK_ESTABLISHED | 14337 SATA_EVNT_LINK_LOST)) { 14338 sata_process_port_link_events(sata_hba_inst, 14339 saddr); 14340 } 14341 if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) { 14342 sata_process_port_pwr_change(sata_hba_inst, 14343 saddr); 14344 } 14345 if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) { 14346 sata_process_target_node_cleanup( 14347 sata_hba_inst, saddr); 14348 } 14349 if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) { 14350 sata_process_device_autoonline( 14351 sata_hba_inst, saddr); 14352 } 14353 } 14354 mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 14355 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) != 14356 SATA_DTYPE_NONE) && 14357 (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) { 14358 if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)-> 14359 satadrv_event_flags & 14360 (SATA_EVNT_DEVICE_RESET | 14361 SATA_EVNT_INPROC_DEVICE_RESET)) { 14362 /* Have device event */ 14363 sata_process_device_reset(sata_hba_inst, 14364 saddr); 14365 } 14366 } 14367 /* Release PORT_BUSY flag */ 14368 (SATA_CPORT_INFO(sata_hba_inst, ncport))-> 14369 cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY; 14370 mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport))); 14371 14372 } /* End of loop through the controller SATA ports */ 14373 } 14374 14375 /* 14376 * Process HBA power level change reported by HBA driver. 14377 * Not implemented at this time - event is ignored. 14378 */ 14379 static void 14380 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst) 14381 { 14382 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14383 "Processing controller power level change", NULL); 14384 14385 /* Ignoring it for now */ 14386 mutex_enter(&sata_hba_inst->satahba_mutex); 14387 sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 14388 mutex_exit(&sata_hba_inst->satahba_mutex); 14389 } 14390 14391 /* 14392 * Process port power level change reported by HBA driver. 14393 * Not implemented at this time - event is ignored. 14394 */ 14395 static void 14396 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst, 14397 sata_address_t *saddr) 14398 { 14399 sata_cport_info_t *cportinfo; 14400 14401 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14402 "Processing port power level change", NULL); 14403 14404 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 14405 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14406 /* Reset event flag */ 14407 cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED; 14408 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14409 } 14410 14411 /* 14412 * Process port failure reported by HBA driver. 14413 * cports support only - no pmports. 14414 */ 14415 static void 14416 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst, 14417 sata_address_t *saddr) 14418 { 14419 sata_cport_info_t *cportinfo; 14420 14421 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 14422 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14423 /* Reset event flag first */ 14424 cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED; 14425 /* If the port is in SHUTDOWN or FAILED state, ignore this event. */ 14426 if ((cportinfo->cport_state & 14427 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) { 14428 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14429 cport_mutex); 14430 return; 14431 } 14432 /* Fail the port */ 14433 cportinfo->cport_state = SATA_PSTATE_FAILED; 14434 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14435 sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport); 14436 } 14437 14438 /* 14439 * Device Reset Event processing. 14440 * The seqeunce is managed by 3 stage flags: 14441 * - reset event reported, 14442 * - reset event being processed, 14443 * - request to clear device reset state. 14444 * 14445 * NOTE: This function has to be entered with cport mutex held. It exits with 14446 * mutex held as well, but can release mutex during the processing. 14447 */ 14448 static void 14449 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst, 14450 sata_address_t *saddr) 14451 { 14452 sata_drive_info_t old_sdinfo; /* local copy of the drive info */ 14453 sata_drive_info_t *sdinfo; 14454 sata_cport_info_t *cportinfo; 14455 sata_device_t sata_device; 14456 int rval_probe, rval_set; 14457 14458 /* We only care about host sata cport for now */ 14459 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 14460 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 14461 /* 14462 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED 14463 * state, ignore reset event. 14464 */ 14465 if (((cportinfo->cport_state & 14466 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) || 14467 (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) { 14468 sdinfo->satadrv_event_flags &= 14469 ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET); 14470 return; 14471 } 14472 14473 if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) & 14474 SATA_VALID_DEV_TYPE) == 0) { 14475 /* 14476 * This should not happen - coding error. 14477 * But we can recover, so do not panic, just clean up 14478 * and if in debug mode, log the message. 14479 */ 14480 #ifdef SATA_DEBUG 14481 sata_log(sata_hba_inst, CE_WARN, 14482 "sata_process_device_reset: " 14483 "Invalid device type with sdinfo!", NULL); 14484 #endif 14485 sdinfo->satadrv_event_flags = 0; 14486 return; 14487 } 14488 14489 #ifdef SATA_DEBUG 14490 if ((sdinfo->satadrv_event_flags & 14491 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) { 14492 /* Nothing to do */ 14493 /* Something is weird - why we are processing dev reset? */ 14494 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14495 "No device reset event!!!!", NULL); 14496 14497 return; 14498 } 14499 if ((sdinfo->satadrv_event_flags & 14500 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 14501 (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) { 14502 /* Something is weird - new device reset event */ 14503 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14504 "Overlapping device reset events!", NULL); 14505 } 14506 #endif 14507 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14508 "Processing port %d device reset", saddr->cport); 14509 14510 /* Clear event flag */ 14511 sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET; 14512 14513 /* It seems that we always need to check the port state first */ 14514 sata_device.satadev_rev = SATA_DEVICE_REV; 14515 sata_device.satadev_addr = *saddr; 14516 /* 14517 * We have to exit mutex, because the HBA probe port function may 14518 * block on its own mutex. 14519 */ 14520 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14521 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 14522 (SATA_DIP(sata_hba_inst), &sata_device); 14523 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14524 sata_update_port_info(sata_hba_inst, &sata_device); 14525 if (rval_probe != SATA_SUCCESS) { 14526 /* Something went wrong? Fail the port */ 14527 cportinfo->cport_state = SATA_PSTATE_FAILED; 14528 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 14529 if (sdinfo != NULL) 14530 sdinfo->satadrv_event_flags = 0; 14531 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14532 cport_mutex); 14533 SATA_LOG_D((sata_hba_inst, CE_WARN, 14534 "SATA port %d probing failed", 14535 saddr->cport)); 14536 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 14537 saddr->cport)->cport_mutex); 14538 return; 14539 } 14540 if ((sata_device.satadev_scr.sstatus & 14541 SATA_PORT_DEVLINK_UP_MASK) != 14542 SATA_PORT_DEVLINK_UP || 14543 sata_device.satadev_type == SATA_DTYPE_NONE) { 14544 /* 14545 * No device to process, anymore. Some other event processing 14546 * would or have already performed port info cleanup. 14547 * To be safe (HBA may need it), request clearing device 14548 * reset condition. 14549 */ 14550 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 14551 if (sdinfo != NULL) { 14552 sdinfo->satadrv_event_flags &= 14553 ~SATA_EVNT_INPROC_DEVICE_RESET; 14554 sdinfo->satadrv_event_flags |= 14555 SATA_EVNT_CLEAR_DEVICE_RESET; 14556 } 14557 return; 14558 } 14559 14560 sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport); 14561 if (sdinfo == NULL) { 14562 return; 14563 } 14564 if ((sdinfo->satadrv_event_flags & 14565 SATA_EVNT_INPROC_DEVICE_RESET) == 0) { 14566 /* 14567 * Start tracking time for device feature restoration and 14568 * identification. Save current time (lbolt value). 14569 */ 14570 sdinfo->satadrv_reset_time = ddi_get_lbolt(); 14571 } 14572 /* Mark device reset processing as active */ 14573 sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET; 14574 14575 old_sdinfo = *sdinfo; /* local copy of the drive info */ 14576 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14577 14578 rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1); 14579 14580 if (rval_set != SATA_SUCCESS) { 14581 /* 14582 * Restoring drive setting failed. 14583 * Probe the port first, to check if the port state has changed 14584 */ 14585 sata_device.satadev_rev = SATA_DEVICE_REV; 14586 sata_device.satadev_addr = *saddr; 14587 sata_device.satadev_addr.qual = SATA_ADDR_CPORT; 14588 /* probe port */ 14589 rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 14590 (SATA_DIP(sata_hba_inst), &sata_device); 14591 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14592 cport_mutex); 14593 if (rval_probe == SATA_SUCCESS && 14594 (sata_device.satadev_state & 14595 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 && 14596 (sata_device.satadev_scr.sstatus & 14597 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP && 14598 sata_device.satadev_type != SATA_DTYPE_NONE) { 14599 /* 14600 * We may retry this a bit later - in-process reset 14601 * condition should be already set. 14602 * Track retry time for device identification. 14603 */ 14604 if ((cportinfo->cport_dev_type & 14605 SATA_VALID_DEV_TYPE) != 0 && 14606 SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL && 14607 sdinfo->satadrv_reset_time != 0) { 14608 clock_t cur_time = ddi_get_lbolt(); 14609 /* 14610 * If the retry time limit was not 14611 * exceeded, retry. 14612 */ 14613 if ((cur_time - sdinfo->satadrv_reset_time) < 14614 drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) { 14615 mutex_enter( 14616 &sata_hba_inst->satahba_mutex); 14617 sata_hba_inst->satahba_event_flags |= 14618 SATA_EVNT_MAIN; 14619 mutex_exit( 14620 &sata_hba_inst->satahba_mutex); 14621 mutex_enter(&sata_mutex); 14622 sata_event_pending |= SATA_EVNT_MAIN; 14623 mutex_exit(&sata_mutex); 14624 return; 14625 } 14626 if (rval_set == SATA_RETRY) { 14627 /* 14628 * Setting drive features failed, but 14629 * the drive is still accessible, 14630 * so emit a warning message before 14631 * return. 14632 */ 14633 mutex_exit(&SATA_CPORT_INFO( 14634 sata_hba_inst, 14635 saddr->cport)->cport_mutex); 14636 goto done; 14637 } 14638 } 14639 /* Fail the drive */ 14640 sdinfo->satadrv_state = SATA_DSTATE_FAILED; 14641 14642 sata_log(sata_hba_inst, CE_WARN, 14643 "SATA device at port %d - device failed", 14644 saddr->cport); 14645 } 14646 /* 14647 * No point of retrying - device failed or some other event 14648 * processing or already did or will do port info cleanup. 14649 * To be safe (HBA may need it), 14650 * request clearing device reset condition. 14651 */ 14652 sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET; 14653 sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET; 14654 sdinfo->satadrv_reset_time = 0; 14655 return; 14656 } 14657 done: 14658 /* 14659 * If setting of drive features failed, but the drive is still 14660 * accessible, emit a warning message. 14661 */ 14662 if (rval_set == SATA_RETRY) { 14663 sata_log(sata_hba_inst, CE_WARN, 14664 "SATA device at port %d - desired setting could not be " 14665 "restored after reset. Device may not operate as expected.", 14666 saddr->cport); 14667 } 14668 /* 14669 * Raise the flag indicating that the next sata command could 14670 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device 14671 * reset is reported. 14672 */ 14673 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14674 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 14675 sdinfo->satadrv_reset_time = 0; 14676 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) { 14677 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 14678 sdinfo->satadrv_event_flags &= 14679 ~SATA_EVNT_INPROC_DEVICE_RESET; 14680 sdinfo->satadrv_event_flags |= 14681 SATA_EVNT_CLEAR_DEVICE_RESET; 14682 } 14683 } 14684 } 14685 14686 14687 /* 14688 * Port Link Events processing. 14689 * Every link established event may involve device reset (due to 14690 * COMRESET signal, equivalent of the hard reset) so arbitrarily 14691 * set device reset event for an attached device (if any). 14692 * If the port is in SHUTDOWN or FAILED state, ignore link events. 14693 * 14694 * The link established event processing varies, depending on the state 14695 * of the target node, HBA hotplugging capabilities, state of the port. 14696 * If the link is not active, the link established event is ignored. 14697 * If HBA cannot detect device attachment and there is no target node, 14698 * the link established event triggers device attach event processing. 14699 * Else, link established event triggers device reset event processing. 14700 * 14701 * The link lost event processing varies, depending on a HBA hotplugging 14702 * capability and the state of the port (link active or not active). 14703 * If the link is active, the lost link event is ignored. 14704 * If HBA cannot detect device removal, the lost link event triggers 14705 * device detached event processing after link lost timeout. 14706 * Else, the event is ignored. 14707 * 14708 * NOTE: Only cports are processed for now, i.e. no port multiplier ports 14709 */ 14710 static void 14711 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst, 14712 sata_address_t *saddr) 14713 { 14714 sata_device_t sata_device; 14715 sata_cport_info_t *cportinfo; 14716 sata_drive_info_t *sdinfo; 14717 uint32_t event_flags; 14718 int rval; 14719 14720 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14721 "Processing port %d link event(s)", saddr->cport); 14722 14723 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 14724 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14725 event_flags = cportinfo->cport_event_flags; 14726 14727 /* Reset event flags first */ 14728 cportinfo->cport_event_flags &= 14729 ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST); 14730 14731 /* If the port is in SHUTDOWN or FAILED state, ignore link events. */ 14732 if ((cportinfo->cport_state & 14733 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 14734 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14735 cport_mutex); 14736 return; 14737 } 14738 14739 /* 14740 * For the sanity sake get current port state. 14741 * Set device address only. Other sata_device fields should be 14742 * set by HBA driver. 14743 */ 14744 sata_device.satadev_rev = SATA_DEVICE_REV; 14745 sata_device.satadev_addr = *saddr; 14746 /* 14747 * We have to exit mutex, because the HBA probe port function may 14748 * block on its own mutex. 14749 */ 14750 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14751 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 14752 (SATA_DIP(sata_hba_inst), &sata_device); 14753 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14754 sata_update_port_info(sata_hba_inst, &sata_device); 14755 if (rval != SATA_SUCCESS) { 14756 /* Something went wrong? Fail the port */ 14757 cportinfo->cport_state = SATA_PSTATE_FAILED; 14758 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14759 cport_mutex); 14760 SATA_LOG_D((sata_hba_inst, CE_WARN, 14761 "SATA port %d probing failed", 14762 saddr->cport)); 14763 /* 14764 * We may want to release device info structure, but 14765 * it is not necessary. 14766 */ 14767 return; 14768 } else { 14769 /* port probed successfully */ 14770 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 14771 } 14772 if (event_flags & SATA_EVNT_LINK_ESTABLISHED) { 14773 14774 if ((sata_device.satadev_scr.sstatus & 14775 SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) { 14776 /* Ignore event */ 14777 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14778 "Ignoring port %d link established event - " 14779 "link down", 14780 saddr->cport); 14781 goto linklost; 14782 } 14783 14784 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14785 "Processing port %d link established event", 14786 saddr->cport); 14787 14788 /* 14789 * For the sanity sake check if a device is attached - check 14790 * return state of a port probing. 14791 */ 14792 if (sata_device.satadev_type != SATA_DTYPE_NONE && 14793 sata_device.satadev_type != SATA_DTYPE_PMULT) { 14794 /* 14795 * HBA port probe indicated that there is a device 14796 * attached. Check if the framework had device info 14797 * structure attached for this device. 14798 */ 14799 if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) { 14800 ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) != 14801 NULL); 14802 14803 sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 14804 if ((sdinfo->satadrv_type & 14805 SATA_VALID_DEV_TYPE) != 0) { 14806 /* 14807 * Dev info structure is present. 14808 * If dev_type is set to known type in 14809 * the framework's drive info struct 14810 * then the device existed before and 14811 * the link was probably lost 14812 * momentarily - in such case 14813 * we may want to check device 14814 * identity. 14815 * Identity check is not supported now. 14816 * 14817 * Link established event 14818 * triggers device reset event. 14819 */ 14820 (SATA_CPORTINFO_DRV_INFO(cportinfo))-> 14821 satadrv_event_flags |= 14822 SATA_EVNT_DEVICE_RESET; 14823 } 14824 } else if (cportinfo->cport_dev_type == 14825 SATA_DTYPE_NONE) { 14826 /* 14827 * We got new device attached! If HBA does not 14828 * generate device attached events, trigger it 14829 * here. 14830 */ 14831 if (!(SATA_FEATURES(sata_hba_inst) & 14832 SATA_CTLF_HOTPLUG)) { 14833 cportinfo->cport_event_flags |= 14834 SATA_EVNT_DEVICE_ATTACHED; 14835 } 14836 } 14837 /* Reset link lost timeout */ 14838 cportinfo->cport_link_lost_time = 0; 14839 } 14840 } 14841 linklost: 14842 if (event_flags & SATA_EVNT_LINK_LOST) { 14843 if ((sata_device.satadev_scr.sstatus & 14844 SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) { 14845 /* Ignore event */ 14846 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14847 "Ignoring port %d link lost event - link is up", 14848 saddr->cport); 14849 goto done; 14850 } 14851 #ifdef SATA_DEBUG 14852 if (cportinfo->cport_link_lost_time == 0) { 14853 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14854 "Processing port %d link lost event", 14855 saddr->cport); 14856 } 14857 #endif 14858 /* 14859 * When HBA cannot generate device attached/detached events, 14860 * we need to track link lost time and eventually generate 14861 * device detach event. 14862 */ 14863 if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) { 14864 /* We are tracking link lost time */ 14865 if (cportinfo->cport_link_lost_time == 0) { 14866 /* save current time (lbolt value) */ 14867 cportinfo->cport_link_lost_time = 14868 ddi_get_lbolt(); 14869 /* just keep link lost event */ 14870 cportinfo->cport_event_flags |= 14871 SATA_EVNT_LINK_LOST; 14872 } else { 14873 clock_t cur_time = ddi_get_lbolt(); 14874 if ((cur_time - 14875 cportinfo->cport_link_lost_time) >= 14876 drv_usectohz( 14877 SATA_EVNT_LINK_LOST_TIMEOUT)) { 14878 /* trigger device detach event */ 14879 cportinfo->cport_event_flags |= 14880 SATA_EVNT_DEVICE_DETACHED; 14881 cportinfo->cport_link_lost_time = 0; 14882 SATADBG1(SATA_DBG_EVENTS, 14883 sata_hba_inst, 14884 "Triggering port %d " 14885 "device detached event", 14886 saddr->cport); 14887 } else { 14888 /* keep link lost event */ 14889 cportinfo->cport_event_flags |= 14890 SATA_EVNT_LINK_LOST; 14891 } 14892 } 14893 } 14894 /* 14895 * We could change port state to disable/delay access to 14896 * the attached device until the link is recovered. 14897 */ 14898 } 14899 done: 14900 event_flags = cportinfo->cport_event_flags; 14901 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14902 if (event_flags != 0) { 14903 mutex_enter(&sata_hba_inst->satahba_mutex); 14904 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 14905 mutex_exit(&sata_hba_inst->satahba_mutex); 14906 mutex_enter(&sata_mutex); 14907 sata_event_pending |= SATA_EVNT_MAIN; 14908 mutex_exit(&sata_mutex); 14909 } 14910 } 14911 14912 /* 14913 * Device Detached Event processing. 14914 * Port is probed to find if a device is really gone. If so, 14915 * the device info structure is detached from the SATA port info structure 14916 * and released. 14917 * Port status is updated. 14918 * 14919 * NOTE: Process cports event only, no port multiplier ports. 14920 */ 14921 static void 14922 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst, 14923 sata_address_t *saddr) 14924 { 14925 sata_cport_info_t *cportinfo; 14926 sata_drive_info_t *sdevinfo; 14927 sata_device_t sata_device; 14928 dev_info_t *tdip; 14929 int rval; 14930 14931 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14932 "Processing port %d device detached", saddr->cport); 14933 14934 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 14935 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14936 /* Clear event flag */ 14937 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED; 14938 14939 /* If the port is in SHUTDOWN or FAILED state, ignore detach event. */ 14940 if ((cportinfo->cport_state & 14941 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 14942 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14943 cport_mutex); 14944 return; 14945 } 14946 /* For sanity, re-probe the port */ 14947 sata_device.satadev_rev = SATA_DEVICE_REV; 14948 sata_device.satadev_addr = *saddr; 14949 14950 /* 14951 * We have to exit mutex, because the HBA probe port function may 14952 * block on its own mutex. 14953 */ 14954 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14955 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 14956 (SATA_DIP(sata_hba_inst), &sata_device); 14957 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 14958 sata_update_port_info(sata_hba_inst, &sata_device); 14959 if (rval != SATA_SUCCESS) { 14960 /* Something went wrong? Fail the port */ 14961 cportinfo->cport_state = SATA_PSTATE_FAILED; 14962 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14963 cport_mutex); 14964 SATA_LOG_D((sata_hba_inst, CE_WARN, 14965 "SATA port %d probing failed", 14966 saddr->cport)); 14967 /* 14968 * We may want to release device info structure, but 14969 * it is not necessary. 14970 */ 14971 return; 14972 } else { 14973 /* port probed successfully */ 14974 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 14975 } 14976 /* 14977 * Check if a device is still attached. For sanity, check also 14978 * link status - if no link, there is no device. 14979 */ 14980 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) == 14981 SATA_PORT_DEVLINK_UP && sata_device.satadev_type != 14982 SATA_DTYPE_NONE) { 14983 /* 14984 * Device is still attached - ignore detach event. 14985 */ 14986 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 14987 cport_mutex); 14988 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 14989 "Ignoring detach - device still attached to port %d", 14990 sata_device.satadev_addr.cport); 14991 return; 14992 } 14993 /* 14994 * We need to detach and release device info structure here 14995 */ 14996 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 14997 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 14998 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 14999 (void) kmem_free((void *)sdevinfo, 15000 sizeof (sata_drive_info_t)); 15001 } 15002 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 15003 /* 15004 * Device cannot be reached anymore, even if the target node may be 15005 * still present. 15006 */ 15007 15008 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15009 sata_log(sata_hba_inst, CE_WARN, "SATA device detached at port %d", 15010 sata_device.satadev_addr.cport); 15011 15012 /* 15013 * Try to offline a device and remove target node if it still exists 15014 */ 15015 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport); 15016 if (tdip != NULL) { 15017 /* 15018 * Target node exists. Unconfigure device then remove 15019 * the target node (one ndi operation). 15020 */ 15021 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) { 15022 /* 15023 * PROBLEM - no device, but target node remained 15024 * This happens when the file was open or node was 15025 * waiting for resources. 15026 */ 15027 SATA_LOG_D((sata_hba_inst, CE_WARN, 15028 "sata_process_device_detached: " 15029 "Failed to remove target node for " 15030 "detached SATA device.")); 15031 /* 15032 * Set target node state to DEVI_DEVICE_REMOVED. 15033 * But re-check first that the node still exists. 15034 */ 15035 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 15036 saddr->cport); 15037 if (tdip != NULL) { 15038 sata_set_device_removed(tdip); 15039 /* 15040 * Instruct event daemon to retry the 15041 * cleanup later. 15042 */ 15043 sata_set_target_node_cleanup(sata_hba_inst, 15044 &sata_device.satadev_addr); 15045 } 15046 } 15047 } 15048 /* 15049 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 15050 * with the hint: SE_HINT_REMOVE 15051 */ 15052 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE); 15053 } 15054 15055 15056 /* 15057 * Device Attached Event processing. 15058 * Port state is checked to verify that a device is really attached. If so, 15059 * the device info structure is created and attached to the SATA port info 15060 * structure. 15061 * 15062 * If attached device cannot be identified or set-up, the retry for the 15063 * attach processing is set-up. Subsequent daemon run would try again to 15064 * identify the device, until the time limit is reached 15065 * (SATA_DEV_IDENTIFY_TIMEOUT). 15066 * 15067 * This function cannot be called in interrupt context (it may sleep). 15068 * 15069 * NOTE: Process cports event only, no port multiplier ports. 15070 */ 15071 static void 15072 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst, 15073 sata_address_t *saddr) 15074 { 15075 sata_cport_info_t *cportinfo; 15076 sata_drive_info_t *sdevinfo; 15077 sata_device_t sata_device; 15078 dev_info_t *tdip; 15079 uint32_t event_flags; 15080 int rval; 15081 15082 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15083 "Processing port %d device attached", saddr->cport); 15084 15085 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 15086 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15087 15088 /* Clear attach event flag first */ 15089 cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED; 15090 15091 /* If the port is in SHUTDOWN or FAILED state, ignore event. */ 15092 if ((cportinfo->cport_state & 15093 (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) { 15094 cportinfo->cport_dev_attach_time = 0; 15095 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 15096 cport_mutex); 15097 return; 15098 } 15099 15100 /* 15101 * If the sata_drive_info structure is found attached to the port info, 15102 * despite the fact the device was removed and now it is re-attached, 15103 * the old drive info structure was not removed. 15104 * Arbitrarily release device info structure. 15105 */ 15106 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 15107 sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo); 15108 SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL; 15109 (void) kmem_free((void *)sdevinfo, 15110 sizeof (sata_drive_info_t)); 15111 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15112 "Arbitrarily detaching old device info.", NULL); 15113 } 15114 cportinfo->cport_dev_type = SATA_DTYPE_NONE; 15115 15116 /* For sanity, re-probe the port */ 15117 sata_device.satadev_rev = SATA_DEVICE_REV; 15118 sata_device.satadev_addr = *saddr; 15119 15120 /* 15121 * We have to exit mutex, because the HBA probe port function may 15122 * block on its own mutex. 15123 */ 15124 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15125 rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst)) 15126 (SATA_DIP(sata_hba_inst), &sata_device); 15127 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15128 sata_update_port_info(sata_hba_inst, &sata_device); 15129 if (rval != SATA_SUCCESS) { 15130 /* Something went wrong? Fail the port */ 15131 cportinfo->cport_state = SATA_PSTATE_FAILED; 15132 cportinfo->cport_dev_attach_time = 0; 15133 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 15134 cport_mutex); 15135 SATA_LOG_D((sata_hba_inst, CE_WARN, 15136 "SATA port %d probing failed", 15137 saddr->cport)); 15138 return; 15139 } else { 15140 /* port probed successfully */ 15141 cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY; 15142 } 15143 /* 15144 * Check if a device is still attached. For sanity, check also 15145 * link status - if no link, there is no device. 15146 */ 15147 if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) != 15148 SATA_PORT_DEVLINK_UP || sata_device.satadev_type == 15149 SATA_DTYPE_NONE) { 15150 /* 15151 * No device - ignore attach event. 15152 */ 15153 cportinfo->cport_dev_attach_time = 0; 15154 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 15155 cport_mutex); 15156 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15157 "Ignoring attach - no device connected to port %d", 15158 sata_device.satadev_addr.cport); 15159 return; 15160 } 15161 15162 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15163 /* 15164 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE 15165 * with the hint: SE_HINT_INSERT 15166 */ 15167 sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT); 15168 15169 /* 15170 * Port reprobing will take care of the creation of the device 15171 * info structure and determination of the device type. 15172 */ 15173 sata_device.satadev_addr = *saddr; 15174 (void) sata_reprobe_port(sata_hba_inst, &sata_device, 15175 SATA_DEV_IDENTIFY_NORETRY); 15176 15177 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)-> 15178 cport_mutex); 15179 if ((cportinfo->cport_state & SATA_STATE_READY) && 15180 (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) { 15181 /* Some device is attached to the port */ 15182 if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) { 15183 /* 15184 * A device was not successfully attached. 15185 * Track retry time for device identification. 15186 */ 15187 if (cportinfo->cport_dev_attach_time != 0) { 15188 clock_t cur_time = ddi_get_lbolt(); 15189 /* 15190 * If the retry time limit was not exceeded, 15191 * reinstate attach event. 15192 */ 15193 if ((cur_time - 15194 cportinfo->cport_dev_attach_time) < 15195 drv_usectohz( 15196 SATA_DEV_IDENTIFY_TIMEOUT)) { 15197 /* OK, restore attach event */ 15198 cportinfo->cport_event_flags |= 15199 SATA_EVNT_DEVICE_ATTACHED; 15200 } else { 15201 /* Timeout - cannot identify device */ 15202 cportinfo->cport_dev_attach_time = 0; 15203 sata_log(sata_hba_inst, 15204 CE_WARN, 15205 "Could not identify SATA device " 15206 "at port %d", 15207 saddr->cport); 15208 } 15209 } else { 15210 /* 15211 * Start tracking time for device 15212 * identification. 15213 * Save current time (lbolt value). 15214 */ 15215 cportinfo->cport_dev_attach_time = 15216 ddi_get_lbolt(); 15217 /* Restore attach event */ 15218 cportinfo->cport_event_flags |= 15219 SATA_EVNT_DEVICE_ATTACHED; 15220 } 15221 } else { 15222 /* 15223 * If device was successfully attached, the subsequent 15224 * action depends on a state of the 15225 * sata_auto_online variable. If it is set to zero. 15226 * an explicit 'configure' command will be needed to 15227 * configure it. If its value is non-zero, we will 15228 * attempt to online (configure) the device. 15229 * First, log the message indicating that a device 15230 * was attached. 15231 */ 15232 cportinfo->cport_dev_attach_time = 0; 15233 sata_log(sata_hba_inst, CE_WARN, 15234 "SATA device detected at port %d", saddr->cport); 15235 15236 if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) { 15237 sata_drive_info_t new_sdinfo; 15238 15239 /* Log device info data */ 15240 new_sdinfo = *(SATA_CPORTINFO_DRV_INFO( 15241 cportinfo)); 15242 sata_show_drive_info(sata_hba_inst, 15243 &new_sdinfo); 15244 } 15245 15246 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15247 saddr->cport)->cport_mutex); 15248 15249 /* 15250 * Make sure that there is no target node for that 15251 * device. If so, release it. It should not happen, 15252 * unless we had problem removing the node when 15253 * device was detached. 15254 */ 15255 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), 15256 saddr->cport); 15257 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15258 saddr->cport)->cport_mutex); 15259 if (tdip != NULL) { 15260 15261 #ifdef SATA_DEBUG 15262 if ((cportinfo->cport_event_flags & 15263 SATA_EVNT_TARGET_NODE_CLEANUP) == 0) 15264 sata_log(sata_hba_inst, CE_WARN, 15265 "sata_process_device_attached: " 15266 "old device target node exists!"); 15267 #endif 15268 /* 15269 * target node exists - try to unconfigure 15270 * device and remove the node. 15271 */ 15272 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15273 saddr->cport)->cport_mutex); 15274 rval = ndi_devi_offline(tdip, 15275 NDI_DEVI_REMOVE); 15276 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15277 saddr->cport)->cport_mutex); 15278 15279 if (rval == NDI_SUCCESS) { 15280 cportinfo->cport_event_flags &= 15281 ~SATA_EVNT_TARGET_NODE_CLEANUP; 15282 cportinfo->cport_tgtnode_clean = B_TRUE; 15283 } else { 15284 /* 15285 * PROBLEM - the target node remained 15286 * and it belongs to a previously 15287 * attached device. 15288 * This happens when the file was open 15289 * or the node was waiting for 15290 * resources at the time the 15291 * associated device was removed. 15292 * Instruct event daemon to retry the 15293 * cleanup later. 15294 */ 15295 sata_log(sata_hba_inst, 15296 CE_WARN, 15297 "Application(s) accessing " 15298 "previously attached SATA " 15299 "device have to release " 15300 "it before newly inserted " 15301 "device can be made accessible.", 15302 saddr->cport); 15303 cportinfo->cport_event_flags |= 15304 SATA_EVNT_TARGET_NODE_CLEANUP; 15305 cportinfo->cport_tgtnode_clean = 15306 B_FALSE; 15307 } 15308 } 15309 if (sata_auto_online != 0) { 15310 cportinfo->cport_event_flags |= 15311 SATA_EVNT_AUTOONLINE_DEVICE; 15312 } 15313 15314 } 15315 } else { 15316 cportinfo->cport_dev_attach_time = 0; 15317 } 15318 15319 event_flags = cportinfo->cport_event_flags; 15320 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15321 if (event_flags != 0) { 15322 mutex_enter(&sata_hba_inst->satahba_mutex); 15323 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 15324 mutex_exit(&sata_hba_inst->satahba_mutex); 15325 mutex_enter(&sata_mutex); 15326 sata_event_pending |= SATA_EVNT_MAIN; 15327 mutex_exit(&sata_mutex); 15328 } 15329 } 15330 15331 15332 /* 15333 * Device Target Node Cleanup Event processing. 15334 * If the target node associated with a sata port device is in 15335 * DEVI_DEVICE_REMOVED state, an attempt is made to remove it. 15336 * If the target node cannot be removed, the event flag is left intact, 15337 * so that event daemon may re-run this function later. 15338 * 15339 * This function cannot be called in interrupt context (it may sleep). 15340 * 15341 * NOTE: Processes cport events only, not port multiplier ports. 15342 */ 15343 static void 15344 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, 15345 sata_address_t *saddr) 15346 { 15347 sata_cport_info_t *cportinfo; 15348 dev_info_t *tdip; 15349 15350 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15351 "Processing port %d device target node cleanup", saddr->cport); 15352 15353 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 15354 15355 /* 15356 * Check if there is target node for that device and it is in the 15357 * DEVI_DEVICE_REMOVED state. If so, release it. 15358 */ 15359 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport); 15360 if (tdip != NULL) { 15361 /* 15362 * target node exists - check if it is target node of 15363 * a removed device. 15364 */ 15365 if (sata_check_device_removed(tdip) == B_TRUE) { 15366 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15367 "sata_process_target_node_cleanup: " 15368 "old device target node exists!", NULL); 15369 /* 15370 * Unconfigure and remove the target node 15371 */ 15372 if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) == 15373 NDI_SUCCESS) { 15374 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15375 saddr->cport)->cport_mutex); 15376 cportinfo->cport_event_flags &= 15377 ~SATA_EVNT_TARGET_NODE_CLEANUP; 15378 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15379 saddr->cport)->cport_mutex); 15380 return; 15381 } 15382 /* 15383 * Event daemon will retry the cleanup later. 15384 */ 15385 mutex_enter(&sata_hba_inst->satahba_mutex); 15386 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 15387 mutex_exit(&sata_hba_inst->satahba_mutex); 15388 mutex_enter(&sata_mutex); 15389 sata_event_pending |= SATA_EVNT_MAIN; 15390 mutex_exit(&sata_mutex); 15391 } 15392 } else { 15393 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15394 saddr->cport)->cport_mutex); 15395 cportinfo->cport_event_flags &= 15396 ~SATA_EVNT_TARGET_NODE_CLEANUP; 15397 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15398 saddr->cport)->cport_mutex); 15399 } 15400 } 15401 15402 /* 15403 * Device AutoOnline Event processing. 15404 * If attached device is to be onlined, an attempt is made to online this 15405 * device, but only if there is no lingering (old) target node present. 15406 * If the device cannot be onlined, the event flag is left intact, 15407 * so that event daemon may re-run this function later. 15408 * 15409 * This function cannot be called in interrupt context (it may sleep). 15410 * 15411 * NOTE: Processes cport events only, not port multiplier ports. 15412 */ 15413 static void 15414 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst, 15415 sata_address_t *saddr) 15416 { 15417 sata_cport_info_t *cportinfo; 15418 sata_drive_info_t *sdinfo; 15419 sata_device_t sata_device; 15420 dev_info_t *tdip; 15421 15422 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15423 "Processing port %d attached device auto-onlining", saddr->cport); 15424 15425 cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport); 15426 15427 /* 15428 * Check if device is present and recognized. If not, reset event. 15429 */ 15430 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15431 if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) { 15432 /* Nothing to online */ 15433 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE; 15434 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15435 saddr->cport)->cport_mutex); 15436 return; 15437 } 15438 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15439 15440 /* 15441 * Check if there is target node for this device and if it is in the 15442 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep 15443 * the event for later processing. 15444 */ 15445 tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport); 15446 if (tdip != NULL) { 15447 /* 15448 * target node exists - check if it is target node of 15449 * a removed device. 15450 */ 15451 if (sata_check_device_removed(tdip) == B_TRUE) { 15452 SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst, 15453 "sata_process_device_autoonline: " 15454 "old device target node exists!", NULL); 15455 /* 15456 * Event daemon will retry device onlining later. 15457 */ 15458 mutex_enter(&sata_hba_inst->satahba_mutex); 15459 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 15460 mutex_exit(&sata_hba_inst->satahba_mutex); 15461 mutex_enter(&sata_mutex); 15462 sata_event_pending |= SATA_EVNT_MAIN; 15463 mutex_exit(&sata_mutex); 15464 return; 15465 } 15466 /* 15467 * If the target node is not in the 'removed" state, assume 15468 * that it belongs to this device. There is nothing more to do, 15469 * but reset the event. 15470 */ 15471 } else { 15472 15473 /* 15474 * Try to online the device 15475 * If there is any reset-related event, remove it. We are 15476 * configuring the device and no state restoring is needed. 15477 */ 15478 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, 15479 saddr->cport)->cport_mutex); 15480 sata_device.satadev_addr = *saddr; 15481 if (saddr->qual == SATA_ADDR_CPORT) 15482 sata_device.satadev_addr.qual = SATA_ADDR_DCPORT; 15483 else 15484 sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT; 15485 sdinfo = sata_get_device_info(sata_hba_inst, &sata_device); 15486 if (sdinfo != NULL) { 15487 if (sdinfo->satadrv_event_flags & 15488 (SATA_EVNT_DEVICE_RESET | 15489 SATA_EVNT_INPROC_DEVICE_RESET)) 15490 sdinfo->satadrv_event_flags = 0; 15491 sdinfo->satadrv_event_flags |= 15492 SATA_EVNT_CLEAR_DEVICE_RESET; 15493 15494 /* Need to create a new target node. */ 15495 cportinfo->cport_tgtnode_clean = B_TRUE; 15496 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15497 saddr->cport)->cport_mutex); 15498 tdip = sata_create_target_node(SATA_DIP(sata_hba_inst), 15499 sata_hba_inst, &sata_device.satadev_addr); 15500 if (tdip == NULL) { 15501 /* 15502 * Configure (onlining) failed. 15503 * We will NOT retry 15504 */ 15505 SATA_LOG_D((sata_hba_inst, CE_WARN, 15506 "sata_process_device_autoonline: " 15507 "configuring SATA device at port %d failed", 15508 saddr->cport)); 15509 } 15510 } else { 15511 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15512 saddr->cport)->cport_mutex); 15513 } 15514 15515 } 15516 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15517 cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE; 15518 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, 15519 saddr->cport)->cport_mutex); 15520 } 15521 15522 15523 static void 15524 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr, 15525 int hint) 15526 { 15527 char ap[MAXPATHLEN]; 15528 nvlist_t *ev_attr_list = NULL; 15529 int err; 15530 15531 /* Allocate and build sysevent attribute list */ 15532 err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP); 15533 if (err != 0) { 15534 SATA_LOG_D((sata_hba_inst, CE_WARN, 15535 "sata_gen_sysevent: " 15536 "cannot allocate memory for sysevent attributes\n")); 15537 return; 15538 } 15539 /* Add hint attribute */ 15540 err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint)); 15541 if (err != 0) { 15542 SATA_LOG_D((sata_hba_inst, CE_WARN, 15543 "sata_gen_sysevent: " 15544 "failed to add DR_HINT attr for sysevent")); 15545 nvlist_free(ev_attr_list); 15546 return; 15547 } 15548 /* 15549 * Add AP attribute. 15550 * Get controller pathname and convert it into AP pathname by adding 15551 * a target number. 15552 */ 15553 (void) snprintf(ap, MAXPATHLEN, "/devices"); 15554 (void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap)); 15555 (void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d", 15556 SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual)); 15557 15558 err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap); 15559 if (err != 0) { 15560 SATA_LOG_D((sata_hba_inst, CE_WARN, 15561 "sata_gen_sysevent: " 15562 "failed to add DR_AP_ID attr for sysevent")); 15563 nvlist_free(ev_attr_list); 15564 return; 15565 } 15566 15567 /* Generate/log sysevent */ 15568 err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR, 15569 ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP); 15570 if (err != DDI_SUCCESS) { 15571 SATA_LOG_D((sata_hba_inst, CE_WARN, 15572 "sata_gen_sysevent: " 15573 "cannot log sysevent, err code %x\n", err)); 15574 } 15575 15576 nvlist_free(ev_attr_list); 15577 } 15578 15579 15580 15581 15582 /* 15583 * Set DEVI_DEVICE_REMOVED state in the SATA device target node. 15584 */ 15585 static void 15586 sata_set_device_removed(dev_info_t *tdip) 15587 { 15588 int circ; 15589 15590 ASSERT(tdip != NULL); 15591 15592 ndi_devi_enter(tdip, &circ); 15593 mutex_enter(&DEVI(tdip)->devi_lock); 15594 DEVI_SET_DEVICE_REMOVED(tdip); 15595 mutex_exit(&DEVI(tdip)->devi_lock); 15596 ndi_devi_exit(tdip, circ); 15597 } 15598 15599 15600 /* 15601 * Set internal event instructing event daemon to try 15602 * to perform the target node cleanup. 15603 */ 15604 static void 15605 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, 15606 sata_address_t *saddr) 15607 { 15608 mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15609 SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |= 15610 SATA_EVNT_TARGET_NODE_CLEANUP; 15611 SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_tgtnode_clean = 15612 B_FALSE; 15613 mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex); 15614 mutex_enter(&sata_hba_inst->satahba_mutex); 15615 sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN; 15616 mutex_exit(&sata_hba_inst->satahba_mutex); 15617 mutex_enter(&sata_mutex); 15618 sata_event_pending |= SATA_EVNT_MAIN; 15619 mutex_exit(&sata_mutex); 15620 } 15621 15622 15623 /* 15624 * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state, 15625 * i.e. check if the target node state indicates that it belongs to a removed 15626 * device. 15627 * 15628 * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state, 15629 * B_FALSE otherwise. 15630 * 15631 * NOTE: No port multiplier support. 15632 */ 15633 static boolean_t 15634 sata_check_device_removed(dev_info_t *tdip) 15635 { 15636 ASSERT(tdip != NULL); 15637 15638 if (DEVI_IS_DEVICE_REMOVED(tdip)) 15639 return (B_TRUE); 15640 else 15641 return (B_FALSE); 15642 } 15643 15644 /* ************************ FAULT INJECTTION **************************** */ 15645 15646 #ifdef SATA_INJECT_FAULTS 15647 15648 static uint32_t sata_fault_count = 0; 15649 static uint32_t sata_fault_suspend_count = 0; 15650 15651 /* 15652 * Inject sata pkt fault 15653 * It modifies returned values of the sata packet. 15654 * It returns immediately if: 15655 * pkt fault injection is not enabled (via sata_inject_fault, 15656 * sata_inject_fault_count), or invalid fault is specified (sata_fault_type), 15657 * or pkt does not contain command to be faulted (set in sata_fault_cmd), or 15658 * pkt is not directed to specified fault controller/device 15659 * (sata_fault_ctrl_dev and sata_fault_device). 15660 * If fault controller is not specified, fault injection applies to all 15661 * controllers and devices. 15662 * 15663 * First argument is the pointer to the executed sata packet. 15664 * Second argument is a pointer to a value returned by the HBA tran_start 15665 * function. 15666 * Third argument specifies injected error. Injected sata packet faults 15667 * are the satapkt_reason values. 15668 * SATA_PKT_BUSY -1 Not completed, busy 15669 * SATA_PKT_DEV_ERROR 1 Device reported error 15670 * SATA_PKT_QUEUE_FULL 2 Not accepted, queue full 15671 * SATA_PKT_PORT_ERROR 3 Not completed, port error 15672 * SATA_PKT_CMD_UNSUPPORTED 4 Cmd unsupported 15673 * SATA_PKT_ABORTED 5 Aborted by request 15674 * SATA_PKT_TIMEOUT 6 Operation timeut 15675 * SATA_PKT_RESET 7 Aborted by reset request 15676 * 15677 * Additional global variables affecting the execution: 15678 * 15679 * sata_inject_fault_count variable specifies number of times in row the 15680 * error is injected. Value of -1 specifies permanent fault, ie. every time 15681 * the fault injection point is reached, the fault is injected and a pause 15682 * between fault injection specified by sata_inject_fault_pause_count is 15683 * ignored). Fault injection routine decrements sata_inject_fault_count 15684 * (if greater than zero) until it reaches 0. No fault is injected when 15685 * sata_inject_fault_count is 0 (zero). 15686 * 15687 * sata_inject_fault_pause_count variable specifies number of times a fault 15688 * injection is bypassed (pause between fault injections). 15689 * If set to 0, a fault is injected only a number of times specified by 15690 * sata_inject_fault_count. 15691 * 15692 * The fault counts are static, so for periodic errors they have to be manually 15693 * reset to start repetition sequence from scratch. 15694 * If the original value returned by the HBA tran_start function is not 15695 * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error 15696 * is injected (to avoid masking real problems); 15697 * 15698 * NOTE: In its current incarnation, this function should be invoked only for 15699 * commands executed in SYNCHRONOUS mode. 15700 */ 15701 15702 15703 static void 15704 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault) 15705 { 15706 15707 if (sata_inject_fault != SATA_INJECT_PKT_FAULT) 15708 return; 15709 15710 if (sata_inject_fault_count == 0) 15711 return; 15712 15713 if (fault == 0) 15714 return; 15715 15716 if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg) 15717 return; 15718 15719 if (sata_fault_ctrl != NULL) { 15720 sata_pkt_txlate_t *spx = 15721 (sata_pkt_txlate_t *)spkt->satapkt_framework_private; 15722 15723 if (sata_fault_ctrl != NULL && sata_fault_ctrl != 15724 spx->txlt_sata_hba_inst->satahba_dip) 15725 return; 15726 15727 if (sata_fault_device.satadev_addr.cport != 15728 spkt->satapkt_device.satadev_addr.cport || 15729 sata_fault_device.satadev_addr.pmport != 15730 spkt->satapkt_device.satadev_addr.pmport || 15731 sata_fault_device.satadev_addr.qual != 15732 spkt->satapkt_device.satadev_addr.qual) 15733 return; 15734 } 15735 15736 /* Modify pkt return parameters */ 15737 if (*rval != SATA_TRAN_ACCEPTED || 15738 spkt->satapkt_reason != SATA_PKT_COMPLETED) { 15739 sata_fault_count = 0; 15740 sata_fault_suspend_count = 0; 15741 return; 15742 } 15743 if (sata_fault_count == 0 && sata_fault_suspend_count != 0) { 15744 /* Pause in the injection */ 15745 sata_fault_suspend_count -= 1; 15746 return; 15747 } 15748 15749 if (sata_fault_count == 0 && sata_fault_suspend_count == 0) { 15750 /* 15751 * Init inject fault cycle. If fault count is set to -1, 15752 * it is a permanent fault. 15753 */ 15754 if (sata_inject_fault_count != -1) { 15755 sata_fault_count = sata_inject_fault_count; 15756 sata_fault_suspend_count = 15757 sata_inject_fault_pause_count; 15758 if (sata_fault_suspend_count == 0) 15759 sata_inject_fault_count = 0; 15760 } 15761 } 15762 15763 if (sata_fault_count != 0) 15764 sata_fault_count -= 1; 15765 15766 switch (fault) { 15767 case SATA_PKT_BUSY: 15768 *rval = SATA_TRAN_BUSY; 15769 spkt->satapkt_reason = SATA_PKT_BUSY; 15770 break; 15771 15772 case SATA_PKT_QUEUE_FULL: 15773 *rval = SATA_TRAN_QUEUE_FULL; 15774 spkt->satapkt_reason = SATA_PKT_QUEUE_FULL; 15775 break; 15776 15777 case SATA_PKT_CMD_UNSUPPORTED: 15778 *rval = SATA_TRAN_CMD_UNSUPPORTED; 15779 spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED; 15780 break; 15781 15782 case SATA_PKT_PORT_ERROR: 15783 /* This is "rejected" command */ 15784 *rval = SATA_TRAN_PORT_ERROR; 15785 spkt->satapkt_reason = SATA_PKT_PORT_ERROR; 15786 /* Additional error setup could be done here - port state */ 15787 break; 15788 15789 case SATA_PKT_DEV_ERROR: 15790 spkt->satapkt_reason = SATA_PKT_DEV_ERROR; 15791 /* 15792 * Additional error setup could be done here 15793 */ 15794 break; 15795 15796 case SATA_PKT_ABORTED: 15797 spkt->satapkt_reason = SATA_PKT_ABORTED; 15798 break; 15799 15800 case SATA_PKT_TIMEOUT: 15801 spkt->satapkt_reason = SATA_PKT_TIMEOUT; 15802 /* Additional error setup could be done here */ 15803 break; 15804 15805 case SATA_PKT_RESET: 15806 spkt->satapkt_reason = SATA_PKT_RESET; 15807 /* 15808 * Additional error setup could be done here - device reset 15809 */ 15810 break; 15811 15812 default: 15813 break; 15814 } 15815 } 15816 15817 #endif 15818